0 Replies Latest reply: Apr 30, 2012 11:29 AM by 934118 RSS

    Problem during video sending using RTP in JMF

    934118
      I have a sender & a receiver program.
      I think there is no problem in the Receiver program. But the Sender throws an 'IOException:Could not connect to capture device'
      The exception is thrown from the line 'processor = Manager.createProcessor(di.getLocator());'
      Now if I create a Player like this:

      Player player = Manager.createRealizedPlayer(di.getLocator());
      player.start();
      Component videoScreen = player.getVisualComponent();

      then it also throws the same exception but live video is played in the Frame captured from the webcam.
      Here is the two programs:

      Sender.java
      ----------------------------------------
      import java.io.IOException;
      import java.util.Vector;
      import javax.media.*;
      import javax.media.control.FormatControl;
      import javax.media.control.TrackControl;
      import javax.media.format.VideoFormat;
      import javax.media.format.YUVFormat;
      import javax.media.protocol.ContentDescriptor;
      import javax.media.protocol.DataSource;

      class Sender{
      public static void main(String args[]){
      Processor processor=null;
      VideoFormat format= new YUVFormat();
      Vector devices= CaptureDeviceManager.getDeviceList( format);
      CaptureDeviceInfo di= null;
      if (devices.size() > 0) {
      di = (CaptureDeviceInfo) devices.elementAt( 0);
      }
      else {
      System.out.println("No capture device found.");
      // exit if we could not find the relevant capturedevice.
      System.exit(-1);
      }

      // Create a processor for this capturedevice & exit if we
      // cannot create it
      try {
      processor = Manager.createProcessor(di.getLocator());
      } catch (IOException e) {
      e.printStackTrace();
      } catch (NoProcessorException e) {
      e.printStackTrace();
      }
      // configure the processor
      processor.configure();
      while(true){         
      if(processor.getState()==Processor.Configured){
      break;
      }
      else if(processor.getState()==Processor.Configuring){
      continue;
      }
      }
      // block until it has been configured

      processor.setContentDescriptor(new ContentDescriptor( ContentDescriptor.RAW));

      TrackControl track[] = processor.getTrackControls();

      boolean encodingOk = false;

      // Go through the tracks and try to program one of them to
      // output gsm data.
      System.out.println(" length: "+track.length);

      for (int i = 0; i < track.length; i++) {
      if (!encodingOk && track[i] instanceof FormatControl) { 
      if ( ( (FormatControl)track).setFormat( new VideoFormat(VideoFormat.JPEG_RTP))==null) {
      track[i].setEnabled(false);
      }
      else {
      encodingOk = true;
      }
      } else {
      // we could not set this track to gsm, so disable it
      track[i].setEnabled(false);
      }
      }

      // At this point, we have determined where we can send out
      // gsm data or not.
      // realize the processor
      if (encodingOk) {
      processor.realize();
      while(true){         
      if(processor.getState()==Processor.Realized){
      break;
      }
      else if(processor.getState()==Processor.Realizing){
      continue;
      }
      }
      // block until realized.
      // get the output datasource of the processor and exit
      // if we fail
      DataSource ds = null;

      try {
      ds = processor.getDataOutput();
      } catch (NotRealizedError e) {
      e.printStackTrace();
      System.exit(-1);
      }
      // hand this datasource to manager for creating an RTP
      // datasink our RTP datasimnk will multicast the audio
      try {
      String url= "rtp://127.144.251.104:49150/video/1";
      MediaLocator m = new MediaLocator(url);
      DataSink d = Manager.createDataSink(ds, m);
      d.open();
      d.start();
      } catch (Exception e) {
      e.printStackTrace();
      }
      }
      }
      }
      ---------------------------------

      Receiver.java
      ---------------------------------
      import java.awt.BorderLayout;
      import java.awt.Component;
      import java.awt.Frame;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;
      import java.io.IOException;
      import java.net.MalformedURLException;
      import javax.media.ControllerEvent;
      import javax.media.ControllerListener;
      import javax.media.Manager;
      import javax.media.MediaLocator;
      import javax.media.NoPlayerException;
      import javax.media.Player;
      import javax.media.RealizeCompleteEvent;

      class Receiver{
      static boolean flag=false;
      public static void main(String args[]){

      Player player;
      Component videoScreen;
      String url= "rtp://127.144.251.104:49150/video/1";
      System.out.println(" -2");
      MediaLocator mrl= new MediaLocator(url);
      System.out.println(" -1");

      if (mrl == null) {
      System.out.println("Can't build MRL for RTP");
      return;
      }

      // Create a player for this rtp session
      try {
      System.out.println(" 0");
      player = Manager.createPlayer(mrl);
      System.out.println(" 1");
      } catch (NoPlayerException e) {
      e.printStackTrace();
      return;
      } catch (MalformedURLException e) {
      e.printStackTrace();
      return;
      } catch (IOException e) {
      e.printStackTrace();
      return;
      }


      if (player != null) {
      player.addControllerListener(new ControllerListener(){

      public void controllerUpdate(ControllerEvent ce) {
      if(ce instanceof RealizeCompleteEvent){
      flag=true;
      }
      }

      });
      player.realize();
      while(true){
      if(flag){
      break;
      }
      }
      player.start();
           
           videoScreen = player.getVisualComponent();
           Frame frm = new Frame();
           frm.setBounds(10, 10, 900, 700);//sets the size of the screen
           
           // setting close operation to the frame
           frm.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
      System.exit(0);
      }
           });
           
           //place player and video screen on the frame
           frm.add(videoScreen, BorderLayout.CENTER);
           frm.add(player.getControlPanelComponent(), BorderLayout.SOUTH);
           frm.setVisible(true);
      }
      }
      }
      ----------------------------