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

Handler

public class Handler extends BasicProcessor implements ReceiveStreamListener

Fields Summary
private final int
INITIALIZED
private final int
REALIZED
private final int
PLAYING
private final int
PAUSING
private DataSource[]
data_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
private boolean
first_pass
RtspUtil
rtspUtil
String
sessionError
Constructors Summary
public Handler()

    
      
        framePositioning = false;

	rtspUtil = new com.sun.media.content.rtsp.RtspUtil( this);
	
	locators = new Vector();
    
Methods Summary
protected booleanaudioEnabled()

	return audioEnabled;
    
private voidcloseSessions()

	RTPManager mgrs[]= rtspUtil.getRTPManagers();
	
	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();
	processor.close();
	closeSessions();
	super.doClose();
    
protected synchronized booleandoConfigure()

    
        
        boolean configured = super.doConfigure();

	if (configured) {
	    configured = initRtspSession();
	}

	return configured;
    
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 booleaninitRtspSession()

        boolean realized = false;
	
	MediaLocator ml=  (MediaLocator) locators.elementAt( 0);

	rtspUtil.setUrl( ml.toString());
	
        String ipAddress = rtspUtil.getServerIpAddress();

        if (ipAddress == null) {
            System.out.println( "Invalid server address.");

            realized = false;
        } else {
	    rtspUtil.setUrl( ml.toString());

	    realized= rtspUtil.createConnection();

	    if( realized) {
                realized = rtspUtil.rtspSetup();

                try {
                    InetAddress destaddr = InetAddress.getByName(ipAddress);

		    int server_ports[]= rtspUtil.getServerPorts();
		    
		    for( int i = 0; i < rtspUtil.getNumberOfTracks(); i++) {
                        SessionAddress remoteAddress =
                                new SessionAddress(destaddr, server_ports[ i]);

			
                        rtspUtil.getRTPManager( i).addTarget( remoteAddress);

			// Set 3/4 sec worth of buffering.
			BufferControl bc = (BufferControl)rtspUtil.getRTPManager( i).getControl("javax.media.control.BufferControl");

			String mediaType= rtspUtil.getMediaType( i);
			
			if( mediaType.equals( "audio")) {
			    bc.setBufferLength(250);
			    bc.setMinimumThreshold(125);
			} else if( mediaType.equals( "video")) {
			    bc.setBufferLength(1500);
			    bc.setMinimumThreshold(250);
			}
		    }
                } catch (Exception e) {
                    Log.error(e.getMessage());

                    return realized;
                }
            }
        }

        if (realized) {
            state = REALIZED;

	    int size= rtspUtil.getNumberOfTracks();

	    data_sources = new DataSource[ size];
	    formats= new Format[ size];
	    
	    // Start the server.
            if (!rtspUtil.rtspStart()) {
		if( first_pass &&
		    rtspUtil.getStatusCode() ==
		    com.sun.media.rtsp.protocol.StatusCode.SESSION_NOT_FOUND) {

		    first_pass= false;

		    return initRtspSession();
		}
		
		return false;
	    }
	    
	    // Wait for the initial filling of the data buffers and
	    // the RTP players to fully realized.
            waitForData();

	    // Now the players are all started.  
	    // Stop the server momentarily.  Rewind the media
	    // so the next start will start from the beginning again.
	    // This is sort of inefficient but will get by the initial
	    // buffering problem.
            // rtspStop();
            // startPos = 0;

	    // Now we need to flush the datasources to make sure
	    // the rewind starts from the very beginning.

	    // for( int i = 0; i < numberOfTracks; i++) {
		// data_sources[ i].flush();
	    // }
        }
	
        return realized;
    
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.rtsp.DataSource){
            MediaLocator ml = source.getLocator();

	    locators.addElement(ml);
        } else {
            throw new IncompatibleSourceException();
        }
    
public voidsetTimeBase(javax.media.TimeBase tb)

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

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

	// return if the data sources have not been allocated yet.
	// this may happen if data is coming in from a different
	// source before the PLAY message has been issued.
	if( data_sources == null) {
	    return;
	}
	
	RTPManager mgrs[]= rtspUtil.getRTPManagers();
	
	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();
	}

	if (event instanceof NewReceiveStreamEvent) {
	    if (data_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();
		data_sources[idx] = stream.getDataSource();

		RTPControl ctl = (RTPControl)data_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;
		    }
		}

		/* this can't be done
		if (source instanceof RTPSocket) {
		    ((RTPSocket)source).setChild(data_sources[idx]);
		} else {
		    ((com.sun.media.protocol.rtp.DataSource)source).
			setChild((com.sun.media.protocol.rtp.DataSource)data_sources[idx]);
		}
		*/

		for (int i = 0; i < data_sources.length; i++) {
		    // Return if not all sessions had yielded a source.
		    if (data_sources[i] == null) {
			return;
		    }
		}
		
		// We've received all the sources, let create the processor.

		DataSource mixDS;

		try {
		    mixDS = javax.media.Manager.createMergingDataSource(data_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;
	    }        
	}
    
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 synchronized booleanwaitForData()

                
        try {
            synchronized (dataLock) {
                while (!dataReady) {
                    dataLock.wait();
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return dataReady;
    
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);