FileDocCategorySizeDatePackage
JMFUtils.javaAPI DocJMF 2.1.1e11263Mon May 12 12:20:32 BST 2003jmapps.util

JMFUtils

public class JMFUtils extends Object

Fields Summary
Constructors Summary
Methods Summary
private static voidassignEffect(javax.media.control.TrackControl tc, java.lang.String effect)

	try {
	    Class cl = Class.forName(effect);
	    Effect ef = (Effect) cl.newInstance();
	    Codec [] codecs = new Codec[] {ef};
	    tc.setCodecChain(codecs);
	} catch (Throwable t) {
	    if (t instanceof ThreadDeath)
		throw (ThreadDeath) t;
	}
    
public static javax.media.protocol.DataSourcecreateCaptureDataSource(java.lang.String strAudioDevice, javax.media.Format audioFormat, java.lang.String strVideoDevice, javax.media.Format videoFormat)

    	DataSource  dsOne = null;
        DataSource  dsTwo = null;
        DataSource  sources [];
        DataSource  merged = null;

        if (strAudioDevice == null  &&  strVideoDevice == null)
            return null;

        if ( strAudioDevice == null  ||  strVideoDevice == null ) {
	    // Case 1 : Only one capture device

            // Figure out which device - audio or video - and create a datasource
            if (strAudioDevice != null)
            	dsOne = initializeCaptureDataSource ( null, strAudioDevice, audioFormat );
            else
                dsOne = initializeCaptureDataSource ( null, strVideoDevice, videoFormat );

        }
        else if ( !strAudioDevice.equals(strVideoDevice) ) {
	    // Case 2 : Different capture devices

            // create the ds for audio
            dsOne = initializeCaptureDataSource ( null, strAudioDevice, audioFormat );
            if ( dsOne == null )
            	return null;

            // create the ds for video
            dsTwo = initializeCaptureDataSource ( null, strVideoDevice, videoFormat );
            if (dsTwo == null)
            	return null;

            // Merge the two
            sources = new DataSource [] { dsOne, dsTwo };
            try {
            	merged = javax.media.Manager.createMergingDataSource ( sources );
            }
            catch (javax.media.IncompatibleSourceException ise) {
            }
            dsOne = merged;
        } else {
	    // Case 3 : Both device names are the same.

            dsOne = initializeCaptureDataSource ( null, strAudioDevice, audioFormat);
            if (dsOne == null)
            	return null;
            dsOne = initializeCaptureDataSource ( dsOne, strVideoDevice, videoFormat);
        }

        if ( dsOne == null )
            return null;

    	try {
            dsOne.connect();
        }
        catch (java.io.IOException ioe) {
            return null;
        }
        return dsOne;
    
public static javax.media.bean.playerbean.MediaPlayercreateMediaPlayer(java.lang.String nameUrl, java.awt.Frame frame, java.lang.String audioEffect, java.lang.String videoEffect)

        MediaLocator    mediaLocator = null;
        MediaPlayer     mediaPlayer = null;
	Player player = null;
	
        mediaLocator = new MediaLocator ( nameUrl );
        if ( mediaLocator == null  ||  nameUrl.equals("") ) {
            MessageDialog.createErrorDialog ( frame, JMFI18N.getResource("jmstudio.error.buildurlfor") + " " + nameUrl );
            return ( null );
        }
	if (audioEffect != null || videoEffect != null) {
	    try {
		Processor proc = javax.media.Manager.createProcessor(mediaLocator);
		StateHelper sh = new StateHelper(proc);
		if (sh.configure()) {
		    TrackControl [] tc = proc.getTrackControls();
		    for (int i = 0; i < tc.length; i++) {
			if (audioEffect != null && tc[i].isEnabled() &&
			    tc[i].getFormat() instanceof AudioFormat) {
			    assignEffect(tc[i], audioEffect);
			} else if (videoEffect != null && tc[i].isEnabled() &&
				   tc[i].getFormat() instanceof VideoFormat) {
			    assignEffect(tc[i], videoEffect);
			}
		    }
		    proc.setContentDescriptor(null);
		    if (sh.realize()) {
			player = proc;
		    }
		}
	    } catch (Throwable t) {
		if (t instanceof ThreadDeath)
		    throw (ThreadDeath) t;
	    }
	}
	
        mediaPlayer = new MediaPlayer ();
	if (player == null)
	    mediaPlayer.setMediaLocator ( mediaLocator );
	else {
	    mediaPlayer.setControlPanelVisible(false);
	    mediaPlayer.setPlayer(player);
	}
        if ( mediaPlayer.getPlayer() == null ) {
            MessageDialog.createErrorDialog ( frame, JMFI18N.getResource("jmstudio.error.player.createfor") + " " + nameUrl );
            return ( null );
        }

        return ( mediaPlayer );
    
public static javax.media.bean.playerbean.MediaPlayercreateMediaPlayer(javax.media.protocol.DataSource dataSource, java.awt.Frame frame)

        MediaPlayer     mediaPlayer = null;

        if ( dataSource == null ) {
            MessageDialog.createErrorDialog ( frame, JMFI18N.getResource("jmstudio.error.player.createfor") + " " + dataSource );
            return ( null );
        }

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDataSource( dataSource );
        if ( mediaPlayer.getPlayer() == null ) {
            MessageDialog.createErrorDialog ( frame, JMFI18N.getResource("jmstudio.error.player.createfor") + " " + dataSource );
            return ( null );
        }

        return ( mediaPlayer );
    
public static javax.media.bean.playerbean.MediaPlayercreateMediaPlayer(javax.media.Player player, java.awt.Frame frame)

        MediaPlayer     mediaPlayer = null;

        if ( player == null ) {
            MessageDialog.createErrorDialog ( frame, JMFI18N.getResource("jmstudio.error.player.createfor") + " " + player );
            return ( null );
        }

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setPlayer ( player );
        if ( mediaPlayer.getPlayer() == null ) {
            MessageDialog.createErrorDialog ( frame, JMFI18N.getResource("jmstudio.error.player.createfor") + " " + player );
            return ( null );
        }

        return ( mediaPlayer );
    
public static com.sun.media.rtp.RTPSessionMgrcreateSessionManager(java.lang.String strAddress, java.lang.String strPort, java.lang.String strTtl, javax.media.rtp.ReceiveStreamListener listener)

        int             nPort;
        int             nTtl;
        RTPSessionMgr   mngrSession;

        nPort = Integer.valueOf(strPort).intValue();
        nTtl = Integer.valueOf(strTtl).intValue();
        mngrSession = createSessionManager ( strAddress, nPort, nTtl, listener );
        return ( mngrSession );
    
public static com.sun.media.rtp.RTPSessionMgrcreateSessionManager(java.lang.String strAddress, int nPort, int nTtl, javax.media.rtp.ReceiveStreamListener listener)

        RTPSessionMgr       mngrSession;
        String              nameUser = null;
        String              cname;
        SessionAddress      addrLocal;
        InetAddress         addrDest;
        SessionAddress      addrSession;
        SourceDescription   arrUserDescr [];

	mngrSession = (RTPSessionMgr) RTPManager.newInstance();
	
        if ( mngrSession == null )
            return null;

	if (listener != null)
	    mngrSession.addReceiveStreamListener(listener);

        // ask RTPSM to generate the local participants CNAME
        cname = mngrSession.generateCNAME ();
        try {
            nameUser = System.getProperty("user.name");
        } catch (SecurityException e){
            nameUser = "jmf-user";
        }

        // create our local Session Address
        addrLocal = new SessionAddress();
        try {
            addrDest = InetAddress.getByName ( strAddress );
            addrSession = new SessionAddress ( addrDest, nPort, addrDest, nPort + 1 );

	    SessionAddress localAddr, destAddr;
		
	    if( addrDest.isMulticastAddress()) {
	    	// local and remote address pairs are identical:
		localAddr= new SessionAddress( addrDest,
					           nPort,
						   nTtl);
		destAddr = new SessionAddress( addrDest,
		    				   nPort,
		      				   nTtl);
			    
	    } else {
	       	localAddr= new SessionAddress( InetAddress.getLocalHost(),
	       		  		           nPort);
                destAddr = new SessionAddress( addrDest,
	       					   nPort);			    
	    }
			
	    mngrSession.initialize( localAddr);
	    mngrSession.addTarget( destAddr);
        }
        catch ( Exception e ) {
//            e.printStackTrace ();
            return null;
        }

        return mngrSession;
    
public static javax.media.protocol.DataSourceinitializeCaptureDataSource(javax.media.protocol.DataSource ds, java.lang.String deviceName, javax.media.Format format)

    	MediaLocator        deviceURL;
        CaptureDeviceInfo   cdi;
        DataSource          dataSource = ds;
        FormatControl       formatControls [];
        Format              formats [];

        if ( ds == null ) {
            cdi = CaptureDeviceManager.getDevice ( deviceName );
            if (cdi == null)
            	return null;
            deviceURL = cdi.getLocator();

            try {
            	dataSource = javax.media.Manager.createDataSource(deviceURL);
                if (dataSource == null)
                    return null;
            } catch (NoDataSourceException ndse) {
            	return null;
            } catch (java.io.IOException ioe) {
            	return null;
            }
        }

        if ( format == null )
            return dataSource;

        if ( format != null  &&  !(dataSource instanceof CaptureDevice) )
            return null;

        formatControls = ((CaptureDevice) dataSource).getFormatControls();
        if ( formatControls == null  ||  formatControls.length == 0 )
            return null;
        for ( int i = 0; i < formatControls.length; i++ ) {
            if ( formatControls[i] == null )
            	continue;
            formats = formatControls[i].getSupportedFormats();
            if (formats == null)
            	return null;
//            System.err.println("Trying format " + format);
            if (matches(format, formats) != null) {
            	formatControls[i].setFormat(format);
//                System.err.println("Matching format = " + format);
                return dataSource;
            }
        }
        return null;
    
private static javax.media.Formatmatches(javax.media.Format format, javax.media.Format[] supported)

        if ( supported == null )
            return null;
        for ( int i = 0;  i < supported.length;  i++ ) {
            if ( supported[i].matches(format) )
                return supported[i];
        }
        return null;