2013-03-16 42 views
0

晚上好,我正在實現在同一臺計算機上運行2個rtp會話的JMF項目。 rtp會話可以初始化並啓動,但我怎麼能打斷其中的一個。不要在同一臺計算機上運行2個rtp會話,有沒有辦法讓JMF中斷/保持一個rtp會話?這些信息有一些代碼。如何中斷JMF中的音頻rtp會話?

的transmitA和transmitB幾乎是其獲得從本地計算機的線性聲,並將其轉換成GSM_RTP或ULAW_RTP相同的代碼,只有端口發送是不一樣的。最後將mediaLocator數據鏈接到其他計算機。

Thread transmitA = new Thread() { 
     public void run() { 

      transmitA(); 
     } 
    }; 

     Thread transmitB = new Thread() { 
     public void run() { 

      transmitB(); 
     } 
    }; 

條件中斷

transmitA.start(); 
trasmitB.start(); 

while (...) { 

     if (...) { 

      //interrupt transmitA 
      transmitA.interrupt(); 
      transmitA.stop(); 

      } /*else if (...) { 
         //interrupt transmitB 
         transmitB.interrupt(); 
         transmitB.stop(); 
        }*/      
      }//notation end here 

好了,中斷線程是不一樣的中斷RTP會話。如何中斷或只是讓一個rtp會話傳輸到另一臺計算機?我會在賽道上做點什麼嗎?或處理器?

transmitA()

public void transmitA(){ 

     // First find a capture device that will capture linear audio 
     // data at 8bit 8Khz 
     AudioFormat format= new AudioFormat(AudioFormat.LINEAR, 
              8000, 
              8, 
              1); 

     Vector devices= CaptureDeviceManager.getDeviceList(format); 

     CaptureDeviceInfo di= null; 

     if (devices.size() > 0) { 
      di = (CaptureDeviceInfo) devices.elementAt(0); 
     } 
     else { 
      // exit if we could not find the relevant capturedevice. 
      System.exit(-1); 
     } 

     // Create a processor for this capturedevice & exit if we 
     // cannot create it 
     Processor processor = null; 
     try { 
       processor = Manager.createProcessor(di.getLocator()); 
     } catch (IOException e) { 
      System.exit(-1); 
     } catch (NoProcessorException e) { 
      System.exit(-1); 
     } 

     // configure the processor 
     processor.configure(); 

     while (processor.getState() != Processor.Configured){ 
      try { 
        Thread.sleep(100); 
      } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } 
     } 

     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. 

     for (int i = 0; i < track.length; i++) { 
      if (!encodingOk && track[i] instanceof FormatControl) { 
       if (((FormatControl)track[i]). 
        setFormat(new AudioFormat(AudioFormat.GSM_RTP, 
               8000, 
               8, 
               1)) == 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 (processor.getState() != Processor.Realized){ 
        try { 
          Thread.sleep(100); 
        } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
        } 
      } 
      // get the output datasource of the processor and exit 
      // if we fail 
      DataSource ds = null; 

      try { 
       ds = processor.getDataOutput(); 
      } catch (NotRealizedError e) { 
       System.exit(-1); 
      } 

      // hand this datasource to manager for creating an RTP 
      // datasink our RTP datasink will multicast the audio 
      try { 
       String url= "rtp://192.168.1.3:22222/audio/16"; 
       //String url= "rtp://224.0.0.1:22224/audio/16"; 
       MediaLocator m = new MediaLocator(url); 

       DataSink d = Manager.createDataSink(ds, m); 
       d.open(); 
       d.start(); 
       processor.start(); 
      } catch (Exception e) { 
       System.out.println("cannot find the receiver address!!!"); 
       System.exit(-1); 
      }  
     } 
    } 

需要先進的一些提示和指導,謝謝^^」

回答

0

努力和嘗試之後,我終於做到了...... 只是刪除了線程和直接使用的方法,我不認爲這是一個很好的編程習慣,但沒有任何建議會真的很感激...

private DataSink d = null; 
private Processor processor = null; 

while (...) { 
    if (...) { 

     //interrupt transmitA 
     processor.stop(); 
     processor.close(); 
     processor.deallocate(); 
     processor = null; 
     d.close(); 
     d = null; 
     transmitA(); 

     } /*else if (...) { 
        //interrupt transmitB 
        processor.stop(); 
        processor.close(); 
        processor.deallocate(); 
        processor = null; 
        d.close(); 
        d = null; 
        transmitB.stop(); 
       }*/      
     }//notation end here 
+0

我真的不擅長JMF編程,但我會學習它。它是我可行的答案,任何更好的答案建議表示讚賞。謝謝。 – 2013-03-29 21:06:09