Lab 7:Multimedia : Java Media Framework (JMF)

sizzledgooseSoftware and s/w Development

Nov 3, 2013 (3 years and 9 months ago)

120 views

Advanced N
etworks


[
Lab 7:Multimedia : Java Media Framework (JMF)
]




1



Multimedia lab

Java Media Framework (JMF)


Introduction


With the growth of Internet there has been a radical change in the method of
software design and deployment.


Software applications are becoming more and more distributed. Java has
emerged as an

ideal programming language for developing Internet
applications.



There has been a growth in the multimedia traffic over the Internet in recent
years. Some of the promising multimedia applications that are deployable over
the Internet include


(i) video

conferencing,

(ii) media
-
on
-
demand,

(iii) access to multimedia database over Internet,

(iv) interactive distance education,


To provide a fully integrated web
-
based multimedia solution the JavaSoft along
with its industry partners have developed a
family of APIs called the Java
Media APIs. The Java Media APIs is a set of APIs that enables Java
programmers to add multimedia to their applications
.


What is JMF?


JMF is a framework for handling streaming media in Java programs. JMF is an
optional package of Java 2 standard platform. JMF provides a unified
architecture and messaging protocol for managing the acquisition, processing
and delivery of time
-
based media.






Advanced N
etworks


[
Lab 7:Multimedia : Java Media Framework (JMF)
]




2



JMF architecture and components

Representing media


All multimedia contents are invariably stored in a compressed form using one
of the various standard formats. Each format basically defines the method used
to encode the media. Therefore we need
a class to define the format of the
multi
media contents we are handling.


To this end JMF defines the class
Format

that specifies the common attributes
of the media Format. The class Format is further specialized into the classes
AudioFormat

and
VideoFormat
.


Specifying the source of media


The next most important support an API should offer is the ability to specify
the media data source
.
Using an
URL

object

we can
specify the media source
for some file.



JMF provides another class called
MediaLocator

to locate a media source of
any hardware device like microphone or webcam.


The source of the media can be of varying nature. The JMF class

DataSource
” abstracts a source of media and offers a simple connect
-
protocol
to access the media data.



Specifying the media destination


A
DataSink

abstracts the location of the media destination and provides a
simple protocol for rendering media into destination.



A DataSink can read the media from a DataSource and render the media to a
file or a
stream




Advanced N
etworks


[
Lab 7:Multimedia : Java Media Framework (JMF)
]




3


Important
components

Player


A
Player

takes as input a stream of audio or video data and rend
ers it to a
speaker or a screen.


much like a CD player reads a CD and outputs music to the speaker. A
Player

can have states, which exist naturally because a
Player

has to
prepare itself and its data source before it can start playing the media.

Java
Player
has many methods like

:

getVis
ualComponent();


getControlPanelComponent()
;

start();

stop();

dealloc
ate();


we will use and explain all of them during the practical part.


Processor

A Processor is a type of Player. In the JMF API, a Processor interface extends
Player. As such, a Processor supports the same presentation controls as a
Player.

Unlike

a Playe
r
, a Processor has control over what processing is performed on
the input media stream.

In addition to rendering a data source, a Processor can also output media data
through a DataSource so it can be presented by another Player or Processor
.







Advanced N
etworks


[
Lab 7:Multimedia : Java Media Framework (JMF)
]




4



Manager

Manager class is used to create players, processors, datasinks and so on. You
can imagine it as a mapper between JMF components.
























Advanced N
etworks


[
Lab 7:Multimedia : Java Media Framework (JMF)
]




5


As you can see in the figure above


which actually
summarize
all the lab!
-
,
that we can get our data from
URL

or
MediaLocator

:



If you are getting your media from some
file

then use URL.



If you are getting your media from some
hardware

device

:
microphone or a webcam for example, then use MediaLocator

After you choose this option you have to extract your
DataSource

form them
and use it in the creation of either
Player

or
Processor
.



If you want only to display your data then you can use Player.



If you want to make any
changes

in the data and then display it or if
you want to
send

it anywhere using network or
save

it to some
file then
you have to use Processor
.

But don't worry !

we will go through all these scenarios.

In
this lab

we are going to take data from File (e.g. using URL) and from
webcam (e.g. using MediaLocator)

and display them(data is plural
word!
)

using a Player.

Next lab
, we will transmit live voice chat over the network using only Java
code and then transmit video
from a webcam using JMStudio.

Part 0 : Installation and needed programs



1
.

In order to work throughout this lab

you need to have JDK and some IDE
for java, I recommend that you install the latest version of them form Oracle
website on this link
here
.

2
.

After you finish installing your JDK and IDE (netbeans) you have to

3
.

configure java path

in the environment variable as shown below.












Step 2: Advanced system settings

Step 1: properties

Advanced N
etworks


[
Lab 7:Multimedia : Java Media Framework (JMF)
]




6
















4
.

Now you have to download JMF software from
here

and its download
steps is straightforward.


Now you should be ready to go!

Remember

Before you start using JMF in netbeans you have to add some jar files first,
follow

steps below:

1
.

Right click on your project name then click
properties
.

2
.

Go to
Libraries

(second item on left).

3
.

Click
Add Jar/Folder
.

4
.

Navigate to bin folder of JMF installation and add all jars there !. (e.g. :
C:
\
Program Files
\
JMF2.1.1e
\
lib
)

Files are :
"customizer.jar" "jmf.jar" "mediaplayer.jar" "multiplayer.jar"
"sound.jar"





Step 3: Enviroment Variables

Step 4: JDK path

Step 5: Path for JMStudio

Advanced N
etworks


[
Lab 7:Multimedia : Java Media Framework (JMF)
]




7



Part 1 : Playing a movie using JMF

In this part we are going to view

a movie using Java code
.

N
ote
that we will
use URL in order to get data from the file.















PlayFileUsingJMF.java


2
import

java.awt.*;


3
import

java.awt.event.*;


4
import

java.net.URL;


5
import

javax.media.*;


6
import

javax.media.rtp.*;


7
import

javax.swing.*;


8


9
public

class

PlayFileUsingJMF

{


10


11 @SuppressWarnings(
"UseSpecificCatch"
)


12
public

static

void

main
(String[] args)
throws

In
validSessionAddressException {


13
//code below is used to make JAVA programs look more friendly


14
try

{


15 UIManager.
setLookAndFeel
(UIManager.
getSystemLookAndFeelClassName
());


16 }
catch

(Exception ex) {


17 }


18


19
// we are creating file chooser to load some media file


20 JFileChooser file =
new

JFileChooser();


21
int

option = file.showOpenDialog(
null
);


22
try

{


23 URL url =
null
;


24



25
// now after the choic
e is made, we extract the URL from it


26
if

(option == JFileChooser.
APPROVE_OPTION
) {


27


28 url = file.getSelectedFile().toURL();


29 }


30


31
final

Player pla
yer;


32
// here we are setting our manager to light waight components e.g. swing

VisualComponent

䍯ntrolPane
l

䍯m灯ne湴

Advanced N
etworks


[
Lab 7:Multimedia : Java Media Framework (JMF)
]




8



33
// components.


34 Manager.
setHint
(Manager.
LIGHTWEIGHT_RENDERER
,
true
);


35


36
// here we are using Manager class

to

c
r
eate a
Player from our URL


37 player = Manager.
createRealizedPlayer
(url);


38


39
// now we are simply constructing a frame to display our player


40 JFrame f =
new

JFrame(
"test :)"
);


41 f.setLayout(
new

BorderLayout());


42


43
// well, that's important: we are adding the player's visual component to the frame


44 f.add(player.getVisualComponent(), BorderLayout.
CENTER
);


45


46
// important: here we are adding the control component of the pl
ayer


47 f.add(player.getControlPanelComponent(), BorderLayout.
SOUTH
);


48


49
// TO here we are ready to go ! the buttons are optional but we want to


50
// show them, in case someone doesn't like to use the default controller



51


52
// here we are defining some buttons that we will use


53 JPanel pan =
new

JPanel();


54 JButton start =
new

JButton(
"start"
);


55 JButton stop =
new

JButton(
"stop"
);


56 JBut
ton pause =
new

JButton(
"pause"
);


57


58 pan.add(start);


59 pan.add(stop);


60 pan.add(pause);


61


62
// f.
62
add(pan, BorderLayout.SOUTH);


63


64 start.addActionListener(
new

ActionListener() {


65


66 @Override


67
public

void

actionPerformed
(ActionEvent ae) {


68
// when play button is pressed we simply start the player!


69 player.start();


70 }


71 });


72


73 stop.addActionListener(
new

ActionListener() {


74


75 @Override


76
public

void

actionPerformed
(ActionEvent ae) {


77 player.stop();


78 playe
r.deallocate();
// release resources too


79 }


80 });


81


82 pause.addActionListener(
new

ActionListener() {


83


84 @Override


85
public

void

actionPerformed
(ActionEvent ae) {


86 player.stop();


87 }


88 });


89


90
// initializing our frame


91 f.setSize(400, 400);


92 f.setLocationRelativeTo(
null
);

Advanced N
etworks


[
Lab 7:Multimedia : Java Media Framework (JMF)
]




9



93 f.setDefaultCloseOperation(JFrame
.
EXIT_ON_CLOSE
);


94 f.setVisible(
true
);


95
// player.start();


96 }
catch

(Exception ex) {


97 }


98


99 }

100 }

101



Hints :



To use out buttons comment line 47 and uncomment line 62
.



To start the
player as soon as the frame loads uncomment line 95.


Part 2 : Controlling a
webcam


I
n
this Part of the lab
we are going to
ca
pture the
webcam data and hence we
will use mediaLocator to
achieve

t
his.


C
aptureCAM.java


1


2
import

java.awt.*;


3
import

java.net.MalformedURLException;


4
import

javax.media.*;


5
import

javax.swing.JFrame;


6


7
public

class

captureCAM

{


8


9 @SuppressWarnings(
"UseSpecificCatch"
)

10
public

static

void

main
(String[] args)
throws

MalformedURLException {

11

12
/* // this code can be used to deternime the connected media devices and use it

13

14

Vector v = CaptureDeviceManager.getDeviceList(null);

15

16

for (Iterator it = v.iterator(); it.hasNext();) {

17

18

System.out.println(it.next().toString());

19

20

}

21

22

String str1 = "vfw://0"
;

23

24

CaptureDeviceInfo di = CaptureDeviceManager.getDevice(str1);//Getting Camera

25

*/

26

27
//here we are connecting to the camera

28 CaptureDeviceInfo webcamInfooo =
new

CaptureDeviceInfo(
"Camera"
,
new

MediaLocat
or(
"vfw://0"
),
null
);

29

30
//now we are extracting the medialoctor of this device

31 MediaLocator webcamMediaLocator =webcamInfooo.getLocator();
//MediaLocator for Camera

32

33 Player player;

Advanced N
etworks


[
Lab 7:Multimedia : Java Media Framework (JMF)
]




10


34

35
try

{

3
6
// now we are creating the player but this time using the medialocator of the cam

37 player = Manager.
createRealizedPlayer
(webcamMediaLocator);
//Creating Player

38

39 Component comp;
//for Getting Visual Player Component of Camera

40
if

((comp = player.getVisualComponent()) !=
null
) {

41 JFrame f =
new

JFrame(
"test :)"
);

42 f.setLayout(
new

BorderLayout());

43
f.add(comp, BorderLayout.
CENTER
);

44 f.add(player.getControlPanelComponent(), BorderLayout.
SOUTH
);

45

46 f.setSize(400, 400);

47 f.setLocationRelativeTo(
null
);

48 f.setDefaultCloseOperation(J
Frame.
EXIT_ON_CLOSE
);

49 f.setVisible(
true
);

50 player.start();

51 }

52 }
catch

(Exception x) {

53

54 }

55 }

56 }

57






Advanced N
etworks


[
Lab 7:Multimedia : Java Media Framework (JMF)
]




11


Next lab


Part 3 : Streaming Media using JMStudio


Part 4 : Implementing RTP Sender and Receiver Using Java


Part
5

:
C
apturing
the stream using wireshar
k