• Loading
    • JMF API Classes And Interface

      Introduction



      Java Media Framework (JMF) Application Programming Interface (API) enables you to embed and process time-based media types in Java applications and applets. Examples of time-based media types include audio and video files.

      In addition to support for playback of the media, the JMF API enables manipulation of the media within applications. It also allows you to capture data from a device such as a video camera or microphone.

      This article provides an overview of the JMF API.

      JMF API Classes


      The various classes of the JMF API perform tasks such as receiving and processing input media and generating output media. These classes are implemented in the javax.media package.

      The MediaLocator Class:-
      A MediaLocator object contains information about the location of the media content such as a video file. This information is used to locate the input media. A MediaLocator object can be constructed from a URL.

      The MediaLocator class has two constructors, one that accepts a string and the other that accepts a URL object as its argument. Both the string and the URL object point to the location of the media.

      Methods of the MediaLocator Class:-
      Method Description
      String getProtocol() Returns the protocol associated with the MediaLocator object.
      URL getURL() Returns the URL associated with the MediaLocator object.
      String toExternalForm() Returns a string from the URL associated with the MediaLocator object.


      How to create a MediaLocator object from a URL?
      Code:
      public MediaLocator getMediaLocator(File file) throws Exception
      {
      	MediaLocator ml;
      	URL url;
      
      	/* convert the file object to a URL using the toURL method of the File class */
      	if ( file != null )
      		url = file.toURL();
      
      	/* create a new MediaLocator object */
      	ml = new MediaLocator(url);
      
      	return ml;
      }

      The DataSource Class:-
      The DataSource class provides information about the media type and its properties. This information is used to play or process the media. The DataSource class has one constructor in addition to the default constructor. This constructor accepts a MediaLocator object as its argument. The MediaLocator object determines the location of the media content that will be represented by the DataSource object.

      Methods of the DataSource Class:-
      Method Description
      abstract void connect() Opens a connection to the location specified by the MediaLocator object.
      abstract void disconnect() Closes the connection to the location specified by the MediaLocator object.
      abstract String getContentType() Returns the content type for the media represented by the DataSource object.
      abstract start() Begins the data transfer.
      abstract stop() Ends the data transfer.

      How to create a DataSource object?

      Code:
      public DataSource getDataSource(MediaLocator ml) throws Exception
      {
      	DataSource ds;
      
      	/* create a new DataSource object providing the MediaLocator as an argument to the constructor */
      	ds = new DataSource(ml);
      
      	return ds;
      }
      The PullDataSource Class:-
      The PullDataSource class represents a data source in which the data transfer is initiated by the client. This means that the client controls the flow of data from this data source. An HTTP server is an example of a pull data source.

      The PullDataSource class extends the DataSource class. It has one default constructor and declares the getStreams method with the prototype:

      Code:
      abstract PullSourceStream[] getStreams()
      The getStreams method returns an array of pull data streams that are managed by the PullDataSource object.

      The PushDataSource Class:-
      The PushDataSource class represents a data source in which the server initiates the data transfer. This means that the server controls the flow of data from this data source to the client. Video on demand is an example of a push data source.

      The PushDataSource class extends the DataSource class. It has one default constructor and declares the getStreams method with the prototype:

      Code:
      abstract PushSourceStream[] getStreams()
      The getStreams method returns an array of push data streams that are managed by the PushDataSource object.

      The Manager Class:-
      The Manager class is at the top of the class hierarchy of the javax.media package. This class provides methods to obtain system-dependent components, such as Player, DataSource, Processor, and DataSink. The Manager class also synchronizes different media streams.
      The Manager class provides the createPlayer and createRealizedPlayer methods to create instances of the Player interface. These methods accept only one argument, which can be a URL, a MediaLocator, or a DataSource object, and return a reference to an instance of the Player interface.
      The Manager class also provides the createProcessor and createRealizedProcessor methods to create instances of the Processor interface. These methods accept a ProcessorModel object as their only argument and return a reference to an instance of the Processor interface. The ProcessorModel object provides information about the input and output requirements of the Processor object.

      JMF API Interfaces


      The JMF API defines interfaces that are implemented by various classes. These interfaces declare methods for various purposes. For example, an interface can be used to playback audio.

      The DataSink Interface:-
      The DataSink interface declares methods defined by a class that accepts media from a DataSource and delivers the media to another destination. This destination can be an audio or a video software or a hardware device.
      The DataSink instance is created using the static createDataSink method of the Manager class. The prototype of the createDataSink method is:

      Code:
      DataSink createDataSink(DataSource ds, MediaLocator ml)
      Methods declared by the DataSink interface:
      Method Description
      void close() Closes the connection to the location specified by the MediaLocator object.
      String getContentType() Returns the content type of the data that is provided as input to the DataSink object.
      void open() Opens a connection to the location specified by the MediaLocator object.
      void start() Begins the data transfer.
      void stop() Ends the data transfer.

      How to create a DataSink object?
      Code:
      public DataSink getDataSink(Processor processor, MediaLocator ml) throws Exception
      {
      	DataSink ds;
      	
      	/* a DataSink is created to act as output component for media processed by a process component */ 
      
      	/* create a new DataSink object, the arguments being the DataSource provided by the getDataOutput
      	   method of the Processor object created to process the input media and the MediaLocator */
      	ds = new DataSink(processor.getDataOutput(),ml);
      
      	return ds;
      The Controller Interface:-
      The Controller interface extends the Clock interface and declares methods for resource allocation and state information. The Clock interface obtains time information from a TimeBase object.

      A Clock instance can be in either of two states - Started or Stopped. You need to provide the values of media start-time, time-base start-time, and rate to the Clock instance before it can be started. If these three values are defined, the Clock instance is in the Started state; otherwise, it is in the Stopped state. Media start-time and time-base start-time represent a common point in time to synchronize the Clock and TimeBase. Rate is a constant value that denotes the ratio of the TimeBase and the actual rate at which the Clock runs.

      The Controller interface categorizes the Stopped state of the Clock interface into five states:

      • Unrealized: The state in which the Controller instance is created. In this state, the Controller instance does not have any information about the resources required to begin functioning.
      • Realizing: The state in which the Controller instance moves from the Unrealized to the Realized state. In this state, the Controller instance identifies the resources it requires for functioning.
      • Realized: In this state, the Controller instance has identified all the resources it needs to start functioning.
      • Prefetching: The state in which the Controller instance moves from the Realized to the Prefetched state. In this state, the Controller instance acquires the shared resources, such as audio and video hardware, required to start functioning.
      • Prefetched: In this state, the Controller has acquired all the resources necessary to start functioning.


      The Controller interface provides event generation methods that provide information about state transition, method acknowledgement, and state and error notification.

      The Player Interface:-
      The Player interface declares methods that a class needs to implement for playing audio and video. These methods start and control the playback of media. The Player interface inherits the Controller interface and adds some methods of its own.
      You use the methods of the Manager class to obtain an instance of the Player interface. This instance is in the Unrealized state but needs to be in the Prefetched state before it can be started.

      The Player interface includes methods to obtain Graphical User Interface (GUI) components for the media components. These GUI components allow the user to control the working of the media components.

      The methods of the Player interface:-
      Method Description
      Component getControlPanelComponent() Returns the default control panel that controls the Player instance.
      GainControl getGainControl() Returns a GainControl object that controls the audio gain of the Player instance.
      Component getVisualComponent() Returns the display component associated with the Player instance.
      void start() Starts the media Player. If the Player is not in the Prefetched state, this method calls the methods required for transition of the Player interface from its current state to the Prefetched state.

      How to obtain a Player instance using the createPlayer method?
      Code:
      public Player getPlayer(File file) throws Exception
      {
      	URL url;
      	Player player;
      
      	/* convert the file object to a URL using the toURL method of the File class */
      	if ( file != null )
      		url = file.toURL();
      
      	/* create the Player instance */
      	player = Manager.createPlayer(url);
      
      	return player;
      }
      The Processor Interface:-
      The Processor interface extends the Player interface and declares methods for processing and controlling input media. The processed media is made available as output media. Input media is processed in three stages:

      1. Individual streams are separated from the multiplexed media stream. This stage is called Demultiplexing.
      2. Data from one format is converted to another format. This stage is called Data Transcoding.
      3. The individual streams are combined into a complex stream. This stage is called Multiplexing.


      The Processor interface divides the Realizing state of the Controller interface into two states - Configuring and Configured. These two states enable an application to process media obtained during the Realizing state. In the Configuring state, the Processor interface collects the necessary information for processing. In the Configured state, the Processor interface is ready for processing.

      The output media of the Processor interface is obtained from its output DataSource, which can be a PullDataSource or a PushDataSource.

      The methods of the Processor interface:-
      Method Description
      void configure() Prepares the Processor interface to be programmed for processing.
      ContentDescriptor getContentDescriptor() Returns the output content-type for the Processor interface.
      DataSource getDataOutput() Returns the output DataSource for the Processor interface.
      TrackControl[] getTrackControls() Returns an array of TrackControl objects. Each object corresponds to a track in the input media.
      ContentDescriptor setContentDescriptor (ContentDescriptor desc) Sets the content type for the output produced by the Processor interface.

      How to obtain a Processor instance using the createProcessor method?
      Code:
      public Processor getProcessor(File file) throws Exception
      {
      	Processor processor;
      	ProcessorModel pm;
      	URL url;
      	MediaLocator ml;
      	DataSource ds;
      
      	/* specify the audio format for the ProcessorModel object */
      	Format formats[] = {new AudioFormat(AudioFormat.MPEG_RTP)};
      
      	/* specify the ContentDescriptor for the ProcessorModel object */
      	ContentDescriptor cd = new ContentDescriptor(ContentDescriptor.RAW_RTP);
      	
      	/* convert the file to a URL using the toURL method of the File class */
      	if ( file != null )
      		url = file.toURL();
      
      	/* create a MediaLocator from the URL */
      	ml = new MediaLocator(url);
      
      	/* create a DataSource from the MediaLocator */
      	ds = new DataSource(ml);
      
      	/* create a new ProcessorModel providing the DataSource, Format and ContentDescriptor as its arguments */
      	pm = new ProcessorModel(ds,formats,cd);
      
      	/* create a Processor instance */
      	processor = Manager.createProcessor(pm);
      	return processor;
      }



    Disclaimer: Users of techforum4u.com are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.