FileDocCategorySizeDatePackage
Handler.javaAPI DocJMF 2.1.1e14308Mon May 12 12:21:00 BST 2003com.sun.media.processor.rtp

Handler

public class Handler extends BasicProcessor implements ReceiveStreamListener

Fields Summary
RTPSessionMgr[]
mgrs
DataSource[]
sources
Processor
processor
Format[]
formats
Vector
locators
Object
dataLock
boolean
dataReady
private boolean
closed
private boolean
audioEnabled
private boolean
videoEnabled
private static JMFSecurity
jmfSecurity
private static boolean
securityPrivelege
private Method[]
m
private Class[]
cl
private Object[]
args
String
sessionError
Object
closeSync
Constructors Summary
public Handler()

    


      
        framePositioning = false;
    
Methods Summary
protected booleanaudioEnabled()

	return audioEnabled;
    
private voidcloseSessions()


       
	synchronized (closeSync) {
	    for (int i = 0; i < mgrs.length; i++) {
		if (mgrs[i] != null) {
		    mgrs[i].removeTargets( "Closing session from the RTP Handler");
		    mgrs[i].dispose();
		    mgrs[i] = null;
		}
	    }
	}
    
protected voidcompleteConfigure()

	state = javax.media.Processor.Configured;
	super.completeConfigure();
    
protected voidcompleteRealize()

	state = Realized;
	super.completeRealize();
    
protected voiddoClose()

	closed = true;

	synchronized (dataLock) {
	    dataLock.notify();
	}

	stop();

	if( processor != null) {
	    processor.close();
	}
	
	closeSessions();

	super.doClose();
    
protected synchronized booleandoConfigure()

    
        

        super.doConfigure();
 
        try {
            if (source instanceof RTPSocket) {
                // this constructor will take care of initliasing and
                // starting the session as well as updating the encodings
                // from the RTPControl
		
		mgrs = new RTPSessionMgr[1];

                mgrs[1] = new RTPSessionMgr((RTPSocket)source);
		mgrs[1].addReceiveStreamListener(this);

		sources = new DataSource[1];
		sources[0] = source;
		formats = new Format[1];
		dataReady = false;

            } else {

		RTPMediaLocator rml;
		InetAddress ipAddr;
		SessionAddress localAddr = new SessionAddress();
		SessionAddress destAddr;

		mgrs = new RTPSessionMgr[locators.size()];
		sources = new DataSource[locators.size()];
		formats = new Format[locators.size()];
		dataReady = false;

		for (int i = 0; i < locators.size(); i++) {
		    rml = (RTPMediaLocator)locators.elementAt(i);

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

		    ipAddr = InetAddress.getByName(rml.getSessionAddress());

		    if( ipAddr.isMulticastAddress()) {
			// local and remote address pairs are identical:
			localAddr= new SessionAddress( ipAddr,
						   rml.getSessionPort(),
						   rml.getTTL());

			destAddr = new SessionAddress( ipAddr,
						   rml.getSessionPort(),
						   rml.getTTL());
			    
		    } else {
			localAddr= new SessionAddress( InetAddress.getLocalHost(),
			  		           rml.getSessionPort());

                        destAddr = new SessionAddress( ipAddr,
						   rml.getSessionPort());
		    }
			
		    mgrs[ i].initialize( localAddr);
    		    mgrs[i].addTarget(destAddr);
		}
	    }
            
        } catch (Exception e){
            Log.error("Cannot create the RTP Session: " + e.getMessage());
	    processError = sessionError;
            return false;
        }

	// dont configure this meta player until the internal processor
	// is configured
	try{
	    synchronized (dataLock) {
		while (!dataReady && !isInterrupted() && !closed)
		    dataLock.wait();
	    }
	} catch (Exception e) {}

	// If configure is being interrupted, return failure from configure.
	if (closed || isInterrupted()) {
	    resetInterrupt();
	    processError = "no RTP data was received.";
	    return false;
	}
        
        return true;

    
protected voiddoDeallocate()

	processor.deallocate();
	synchronized (dataLock) {
	    dataLock.notifyAll();
	}
    
protected voiddoFailedConfigure()

	closeSessions();
	super.doFailedConfigure();
    
protected voiddoFailedRealize()

	closeSessions();
	super.doFailedRealize();
    
protected booleandoRealize()

	return waitForRealize(processor);
    
protected voiddoStart()

	super.doStart();
	waitForStart(processor);
    
protected voiddoStop()

	super.doStop();
	waitForStop(processor);
    
public javax.media.protocol.ContentDescriptorgetContentDescriptor()

	super.getContentDescriptor();
	return processor.getContentDescriptor();
    
public javax.media.Control[]getControls()
Return the list of controls from its slave controllers plus the ones that this player supports.

return
the list of controls supported by this player.

	return processor.getControls();
    
public javax.media.protocol.DataSourcegetDataOutput()

	super.getDataOutput();
	return processor.getDataOutput();
    
protected javax.media.TimeBasegetMasterTimeBase()

	return new SystemTimeBase();
    
public javax.media.protocol.ContentDescriptor[]getSupportedContentDescriptors()

	super.getSupportedContentDescriptors();
	return processor.getSupportedContentDescriptors();
    
public javax.media.control.TrackControl[]getTrackControls()

	super.getTrackControls();
	return processor.getTrackControls();
    
public java.awt.ComponentgetVisualComponent()
Obtain the visiual component from the media engine.

        /**
         * Call the superclass method to ensure that restrictions
         * on player methods are enforced
         */
        super.getVisualComponent();
        return processor.getVisualComponent();
    
private voidinvalidateComp()

	controlComp = null;
	controls = null;

    
private voidsendMyEvent(javax.media.ControllerEvent e)

	super.sendEvent(e);
    
public javax.media.protocol.ContentDescriptorsetContentDescriptor(javax.media.protocol.ContentDescriptor ocd)

	super.setContentDescriptor(ocd);
	return processor.setContentDescriptor(ocd);
    
public voidsetSource(javax.media.protocol.DataSource source)


            super.setSource(source);
            if (source instanceof com.sun.media.protocol.rtp.DataSource){
                MediaLocator ml = source.getLocator();
		String mlStr = ml.getRemainder();
		String str;
		int start, idx;

		start = 0;
		while (mlStr.charAt(start) == '/")
		    start++;
	 	locators = new Vector();
		RTPMediaLocator rml;
		try {
		    while (start < mlStr.length() &&
		 	    (idx = mlStr.indexOf("&", start)) != -1) {
			str = mlStr.substring(start, idx);
                	rml = new RTPMediaLocator("rtp://" + str);
			locators.addElement(rml);
			start = idx+1;
		    }
		    if (start != 0)
			str = mlStr.substring(start);
		    else
			str = mlStr;
                    rml = new RTPMediaLocator("rtp://" + str);
		    locators.addElement(rml);

                } catch (Exception e) {
		    throw new IncompatibleSourceException();
		}

            } else if (!(source instanceof RTPSocket)) {
                throw new IncompatibleSourceException();
	    }
		
            // we will set the dynamic encoding for the one
            // encoding we dont use from static RTP payloads
            RTPControl ctl = (RTPControl)
                        source.getControl("javax.media.rtp.RTPControl");
                if (ctl != null)
                    ctl.addFormat(new AudioFormat(AudioFormat.DVI_RTP,
                                     	          44100,
                                                  4,
                                                  1),
                                      18);
    
public voidsetTimeBase(javax.media.TimeBase tb)

    
public voidupdate(javax.media.rtp.event.ReceiveStreamEvent event)


	RTPSessionMgr mgr = (RTPSessionMgr)event.getSource();
	int idx;

	for (idx = 0; idx < mgrs.length; idx++) {
	    if (mgrs[idx] == mgr)
		break;
	}

	if (idx >= mgrs.length) {
	    // Something's wrong.
	    System.err.println("Unknown manager: " + mgr);
	    return;
	}

	if (event instanceof RemotePayloadChangeEvent) {

	    Log.comment("Received an RTP PayloadChangeEvent");
	    Log.error("The RTP processor cannot handle mid-stream payload change.\n");
	    sendEvent(new ControllerErrorEvent(this, "Cannot handle mid-stream payload change."));
	    close();

	}// payload change event
    
	if (event instanceof NewReceiveStreamEvent){

	    if (sources[idx] != null) {
		// We've already gotten a source from this session.
		return;
	    }

	    ReceiveStream stream = null;
	    try{
		// get a handle over the ReceiveStream
		stream =((NewReceiveStreamEvent)event).getReceiveStream();
		sources[idx] = stream.getDataSource();

		RTPControl ctl = (RTPControl)sources[idx].getControl("javax.media.rtp.RTPControl");
		if (ctl != null){
		    formats[idx] = ctl.getFormat();
		    if (formats[idx] instanceof AudioFormat)
			audioEnabled = true;
		    if (formats[idx] instanceof VideoFormat)
			videoEnabled = true;
		}

		if (source instanceof RTPSocket)
		    ((RTPSocket)source).setChild(sources[idx]);
		else
		    ((com.sun.media.protocol.rtp.DataSource)source).
			setChild((com.sun.media.protocol.rtp.DataSource)sources[idx]);

		for (int i = 0; i < sources.length; i++) {
		    // Return if not all sessions had yielded a source.
		    if (sources[i] == null)
			return;
		}

		// We've received all the sources, let create the processor.

		DataSource mixDS;

		try {
		    mixDS = javax.media.Manager.createMergingDataSource(sources);
		} catch (Exception e) {
		    System.err.println("Cannot merge data sources.");
		    return;
		}

		try {
		    processor = javax.media.Manager.createProcessor(mixDS);
		} catch (Exception e) {
		    System.err.println("Cannot create the mix processor.");
		    return;
		}

		if (!waitForConfigure(processor))
		    return;

		// We are done generating the internal processor.
		synchronized(dataLock) {
                    dataReady = true;
                    dataLock.notifyAll();
		}

	    } catch (Exception e){
		System.err.println("NewReceiveStreamEvent exception " +
		                     e.getMessage());
		  return;
	    }
        
	}// instanceof newReceiveStreamEvent
    
public voidupdateStats()

        if ( processor != null) {
            ((BasicProcessor) processor).updateStats();
        }
    
protected booleanvideoEnabled()

	return videoEnabled;
    
private voidwaitForClose(javax.media.Player p)

	(new StateWaiter()).waitForClose(p);
    
private booleanwaitForConfigure(javax.media.Processor p)

	return (new StateWaiter()).waitForConfigure(p);
    
private booleanwaitForRealize(javax.media.Processor p)

	return (new StateWaiter()).waitForRealize(p);
    
private voidwaitForStart(javax.media.Player p)

	(new StateWaiter()).waitForStart(p, true);
    
private voidwaitForStop(javax.media.Player p)

	(new StateWaiter()).waitForStart(p, false);