Lab 7:Multimedia : Java Media Framework (JMF)]
Total Page:16
File Type:pdf, Size:1020Kb
Advanced Networks [Lab 7:Multimedia : Java Media Framework (JMF)] 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. 1 Advanced Networks [Lab 7:Multimedia : Java Media Framework (JMF)] 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 multimedia 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 2 Advanced Networks [Lab 7:Multimedia : Java Media Framework (JMF)] Important components Player A Player takes as input a stream of audio or video data and renders 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 : getVisualComponent(); getControlPanelComponent(); start(); stop(); deallocate(); 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 Player, 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. 3 Advanced Networks [Lab 7:Multimedia : Java Media Framework (JMF)] Manager Manager class is used to create players, processors, datasinks and so on. You can imagine it as a mapper between JMF components. 4 Advanced Networks [Lab 7:Multimedia : Java Media Framework (JMF)] 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 5 Advanced Networks [Lab 7:Multimedia : Java Media Framework (JMF)] Step 4: JDK path Step 5: Path for JMStudio Step 3: Enviroment Variables 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" 6 Advanced Networks [Lab 7:Multimedia : Java Media Framework (JMF)] Part 1 : Playing a movie using JMF In this part we are going to view a movie using Java code. Note that we will use URL in order to get data from the file. VisualComponent ControlPanel Component 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 InvalidSessionAddressException { 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 choice 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 player; 32 // here we are setting our manager to light waight components e.g. swing 7 Advanced Networks [Lab 7:Multimedia : Java Media Framework (JMF)] 33 // components. 34 Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true); 35 36 // here we are using Manager class to create 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 player 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 JButton pause = new JButton("pause"); 57 58 pan.add(start); 59 pan.add(stop); 60 pan.add(pause); 61 62 // f.62add(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 player.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); 8 Advanced Networks [Lab 7:Multimedia : Java Media Framework (JMF)] 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 In this Part of the lab we are going to capture the webcam data and hence we will use mediaLocator to achieve this. CaptureCAM.java 1 2 import java.awt.*; 3 import java.net.MalformedURLException; 4 import javax.media.*; 5 import