基于JMF RTP的音视频传输


import java.io.
* ;
import java.awt.
* ;
import java.net.
* ;
import java.awt.
event . * ;
import java.util.Vector;

import javax.media.
* ;
import javax.media.rtp.
* ;
import javax.media.rtp.
event . * ;
import javax.media.rtp.rtcp.
* ;
import javax.media.protocol.
* ;
import javax.media.protocol.DataSource;
import javax.media.format.AudioFormat;
import javax.media.format.VideoFormat;
import javax.media.Format;
import javax.media.format.FormatChangeEvent;
import javax.media.control.BufferControl;


/* *
 * AVReceive3 to receive RTP transmission using the RTPConnector.
 
*/
public   class  AVReceive3 implements ReceiveStreamListener, SessionListener, 
    ControllerListener
{
    String sessions[] 
=   null ;
    RTPManager mgrs[] 
=   null ;
    Vector playerWindows 
=   null ;

    boolean dataReceived 
=   false ;
    Object dataSync 
=   new  Object();


    
public  AVReceive3(String sessions[]) {
    
this .sessions  =  sessions;
    }

    
protected  boolean initialize() {

        
try  {
        mgrs 
=   new  RTPManager[sessions.length];
        playerWindows 
=   new  Vector();

        SessionLabel session;

        
//  Open the RTP sessions.
         for  ( int  i  =   0 ; i  <  sessions.length; i ++ ) {

         
//  Parse the session addresses.
         try  {
            session 
=   new  SessionLabel(sessions[i]);
        } 
catch  (IllegalArgumentException e) {
            System.err.println(
" Failed to parse the session address given:  "   +  sessions[i]);
            
return   false ;
        }

        System.err.println(
"   - Open RTP session for: addr:  "   +  session.addr  +   "  port:  "   +  session.port  +   "  ttl:  "   +  session.ttl);

        mgrs[i] 
=  (RTPManager) RTPManager.newInstance();
        mgrs[i].addSessionListener(
this );
        mgrs[i].addReceiveStreamListener(
this );

        
//  Initialize the RTPManager with the RTPSocketAdapter
        mgrs[i].initialize( new  RTPSocketAdapter(
                    InetAddress.getByName(session.addr), 
                    session.port, session.ttl));

        
//  You can try out some other buffer size to see
        
//  if you can get better smoothness.
        BufferControl bc  =  (BufferControl)mgrs[i].getControl( " javax.media.control.BufferControl " );
        
if  (bc  !=   null )
            bc.setBufferLength(
350 );
        }

        } 
catch  (Exception e){
            System.err.println(
" Cannot create the RTP Session:  "   +  e.getMessage());
            
return   false ;
        }

    
//  Wait for data to arrive before moving on.

    
long  then  =  System.currentTimeMillis();
    
long  waitingPeriod  =   30000 ;   //  wait for a maximum of 30 secs.

    
try {
        synchronized (dataSync) {
        
while  ( ! dataReceived  &&  
            System.currentTimeMillis() 
-  then  <  waitingPeriod) {
            
if  ( ! dataReceived)
            System.err.println(
"   - Waiting for RTP data to arrive " );
            dataSync.wait(
1000 );
        }
        }
    } 
catch  (Exception e) {}

    
if  ( ! dataReceived) {
        System.err.println(
" No RTP data was received. " );
        close();
        
return   false ;
    }

        
return   true ;
    }


    
public  boolean isDone() {
    
return  playerWindows.size()  ==   0 ;
    }


    
/* *
     * Close the players and the session managers.
     
*/
    
protected   void  close() {

    
for  ( int  i  =   0 ; i  <  playerWindows.size(); i ++ ) {
        
try  {
        ((PlayerWindow)playerWindows.elementAt(i)).close();
        } 
catch  (Exception e) {}
    }

    playerWindows.removeAllElements();

    
//  close the RTP session.
     for  ( int  i  =   0 ; i  <  mgrs.length; i ++ ) {
        
if  (mgrs[i]  !=   null ) {
                mgrs[i].removeTargets( 
" Closing session from AVReceive3 " );
                mgrs[i].dispose();
        mgrs[i] 
=   null ;
        }
    }
    }


    PlayerWindow find(Player p) {
    
for  ( int  i  =   0 ; i  <  playerWindows.size(); i ++ ) {
        PlayerWindow pw 
=  (PlayerWindow)playerWindows.elementAt(i);
        
if  (pw.player  ==  p)
        
return  pw;
    }
    
return   null ;
    }


    PlayerWindow find(ReceiveStream strm) {
    
for  ( int  i  =   0 ; i  <  playerWindows.size(); i ++ ) {
        PlayerWindow pw 
=  (PlayerWindow)playerWindows.elementAt(i);
        
if  (pw.stream  ==  strm)
        
return  pw;
    }
    
return   null ;
    }


    
/* *
     * SessionListener.
     
*/
    
public  synchronized  void  update(SessionEvent evt) {
    
if  (evt instanceof NewParticipantEvent) {
        Participant p 
=  ((NewParticipantEvent)evt).getParticipant();
        System.err.println(
"   - A new participant had just joined:  "   +  p.getCNAME());
    }
    }


    
/* *
     * ReceiveStreamListener
     
*/
    
public  synchronized  void  update( ReceiveStreamEvent evt) {

    RTPManager mgr 
=  (RTPManager)evt.getSource();
    Participant participant 
=  evt.getParticipant();     //  could be null.
    ReceiveStream stream  =  evt.getReceiveStream();   //  could be null.

    
if  (evt instanceof RemotePayloadChangeEvent) {
     
        System.err.println(
"   - Received an RTP PayloadChangeEvent. " );
        System.err.println(
" Sorry, cannot handle payload change. " );
        System.exit(
0 );

    }
    
    
else   if  (evt instanceof NewReceiveStreamEvent) {

        
try  {
        stream 
=  ((NewReceiveStreamEvent)evt).getReceiveStream();
        DataSource ds 
=  stream.getDataSource();

        
//  Find out the formats.
        RTPControl ctl  =  (RTPControl)ds.getControl( " javax.media.rtp.RTPControl " );
        
if  (ctl  !=   null ){
            System.err.println(
"   - Recevied new RTP stream:  "   +  ctl.getFormat());
        } 
else
            System.err.println(
"   - Recevied new RTP stream " );

        
if  (participant  ==   null )
            System.err.println(
"       The sender of this stream had yet to be identified. " );
        
else  {
            System.err.println(
"       The stream comes from:  "   +  participant.getCNAME()); 
        }

        
//  create a player by passing datasource to the Media Manager
        Player p  =  javax.media.Manager.createPlayer(ds);
        
if  (p  ==   null )
            
return ;

        p.addControllerListener(
this );
        p.realize();
        PlayerWindow pw 
=   new  PlayerWindow(p, stream);
        playerWindows.addElement(pw);

        
//  Notify intialize() that a new stream had arrived.
        synchronized (dataSync) {
            dataReceived 
=   true ;
            dataSync.notifyAll();
        }

        } 
catch  (Exception e) {
        System.err.println(
" NewReceiveStreamEvent exception  "   +  e.getMessage());
        
return ;
        }
        
    }

    
else   if  (evt instanceof StreamMappedEvent) {

         
if  (stream  !=   null   &&  stream.getDataSource()  !=   null ) {
        DataSource ds 
=  stream.getDataSource();
        
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值