What you will learn:
What Java Media Framework can do
Concepts related to JMF
Process modelling in JMF
Creating a sample audio player
Creating a sample video player
A word on Real-Time Transport Protocol
What you should know:
Knowledge on Java language
Using an IDE (in this article I have used Netbeans)
Need of using media with Java application varies from simply integrating an audio player to streaming video. Still it’s not common to add an audio or a video player with Java simply with the help of an IDE. Therefore, for that purpose you need to get the help of a 3rd party library. One solution for this problem is to use Java Media Framework, simply refer as JMF in this article.
According to Oracle’s official page  “The Java Media Framework API (JMF) enables audio, video and other time-based media to be added to applications and applets built on Java technology. This optional package, which can capture, playback, stream, and transcode multiple media formats, extends the Java 2 Platform, Standard Edition (J2SE) for multimedia developers by providing a powerful toolkit to develop scalable, cross-platform technology.” Which means you should be able to build almost all the implementations that media involve. But, through experience, there are limitations. For an example video player may not be able to play all kinds of video file formats, and webcam may not support all the types. Regardless of limitations, I’ve heard many successful stories of using JMF for different media purposes.
Through this article, at first I’m going to introduce few important facts of JMF and then go through a tutorial session which explains about creating a simple application using JMF.
Use cases of JMF
1. Play various types of media files .avi, .wav (file types have some limitations)
2. Transmit and Play stream videos to and from internet
3. Video and audio capturing from input devices (limitations with devices)
4. Time-based media processing
Understanding JMF Architecture
Before going to implement a video player or any other using JMF, it’s necessary to understand the architecture of the Java Media Framework. The JMF has few components and relations between those. Knowing those prior will be make it easy to understand the sample codes and even build your own applications.
JMF has few key things and you may be able to see the real-world video players are quite align with these concepts.
Capture device refers to the devices which can take media inputs to the system. Depending on the purpose of the application, you can use different kinds of capturing devices. For an example, to record an audio you can use microphone, while for an application for surveillances camera you need to act camera as the capturing device. A capturing device can be a push or pull. Push devices are continuously providing data while with pull devices, user can determine when to capture.
Data Source refers to where the media data coming from. It can be a local file or files in a network or a live internet broadcast. You need to mention where to fetch data, but once you mention the location, DataSource object takes care about the underlying complex things. Therefore DataSource object can be an audio, a video or both. Once you created the right Datasource, you can fed it to the player for rendering.
Pull Datasources – Client starts the datasource when need.
Push Datasources – Server starts the datasource.
Datasink basically read the data it gets (eg: from Datasource) and send it to some destination. For example, to save media as a file, this will call to a file writer or would broadcast over a network.
Both DataSource and DataSink use MediaLocator object to locate the media which may be a file in the local system or a desired point in the network.
Player takes care of rendering the media content. It takes the audio and video data for rendering. Special thing about this player is, it has steps. You need to have a good understanding on those steps of the player, else you may not get the desired outcome correctly. This is similar to a real-world CD player, where when you insert a disk, it take some time to start operation. Six steps of the Player are;
At this stage the Player object has just been initialized and has known nothing about the external environment of the application.
This phase starts once you call the method “realize()” on the Player object. Then the player starts realizing the state and is in the process of recognizing the resource requirements.
At this stage, the player knows its state and knows about the type of media it presents. Therefore it can provide visual components, controls and its connections to other objects.
Player steps in to this phase with the call of “prefetch()” method. During prefetching phase, the player is preparing its media to present by preloads its media data.
At this stage, the player has done everything to play the media content it has.
With this stage, the player is now ready to render the media data. Player enters this stage by calling “start()” method on the player.
A Processor is a type of Player, but compared to Player, it has control on what is processing. Additionally Processor can be a source to another Processor or a Player. That can be used for converting media in to another format. Along with the six states mentioned in Player, a Processor has two more states.
A Processor enters this state by calling configure() method and the final step of this state is connecting to a DataSource and being able to access the information about the format of the input data.
After connecting to the DataSource, Processor moves from the Configuring state to the Configured state.
The Manager is to integrate implementations of key interfaces, which can be used by existing classes. JMF comes with four types of Managers: Manager, PackageManager, CaptureDeviceManager, and PlugInManager.
Format object is to represent the exact media format. It describes the encoding name and the type of data that format needs. Format can be broadly categorize as AudioFormat and VideoFormat. Furthermore Video format has following sub areas:
Setting up the Java Media Framework on your machine
The easiest way of setting up JMF on a Windows machine is to use the setup provided. Download page  mentioned the latest version of the JMF. Additionally it guides you on how to install JMF on different platforms other than Windows
A Word on Real-Time Transport Protocol
To transfer real-time media needs its own protocol to transfer data such as audio, video over multicast or unicast services. Therefore Real-Time Transport Protocol (RTP) has designed in a way that, network protocol independent. Often it uses UDP (User Datagram Protocol), which does not guarantee about delivering packets. Therefore there should be a special way of handling lost packets. As a solution Real-Time Transport Control Protocol (RTCP) was introduced.
For those kinds of implementations JMF provides “javax.media.rtp”, “javax.media.rtp.event”, and “javax.media.rtp.rtcp” packages. These packages supports RTP stream playback and transmission. With the aid of above mentioned players, processors, capturing devices, JMF can help to develop applications with rich Real-Time Transportation.
Java Media framework is design in a way such that it can be applied to every aspect of media handling. From playing an audio track to streaming videos across internet can be handled from this framework. As its name implied JMF provides a framework, so you need to aggregate components and develop a process model to suite your needs. Initially, I have described the concepts in JMF architecture and stages of player. With the aid of descriptions on concepts, you will be able to come-up with your own model for your applications. Through this article, I’ve shown you how to develop a simple audio player and a video player, which may help you to take the first step of JMF.