Introduction. Part 1 : Streaming Media using JMStudio

Advanced Networks [Lab 7:Multimedia : Java Media Framework (JMF)-2] Multimedia lab Java Media Framework (JMF)-2 Introduction In this lab we will us...
Author: Shon Miles
0 downloads 2 Views 773KB Size
Advanced Networks

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

Multimedia lab Java Media Framework (JMF)-2

Introduction In this lab we will use JMF to transfer media along the network by two ways. First, we will transfer media from microphone and webcam form one PC to one or many PCs on the same network using JMStudio. Second, we will do that using JAVA code by implementing RTP sender and receiver form the scratch. After that we will use WireShark to see the traffic generated in the preceding parts. This lab will be cool !

Part 1 : Streaming Media using JMStudio

JMStudio is an important tool that allows you to playback, capture, store, transmit or receive media. When JMF is installed a JMStudio icon is automatically created in the desktop. In Windows environment you can invoke it though the menus: programs .. Java Media FrameWork 2.1.1 .. JMStudio. Transmission To transmit media using JMStudio go to the menu file of JMStudio and use the transmit option. Now you will be given two options. You can either transmit a file or transmit a captured media. If you click the button “file”, a file dialog will let you choose the file you want to transmit. Similarly the button “capture” will let you choose a capture device(s) and the format of the captured media.

1

Advanced Networks

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

After choosing the file/ capture device(s) press the “Next” button. You will be prompted to specify the content type and parameters for the output. There will be one tab pane for each track. For example in case you want to transmit a .mpg file there will be one track for audio and another track for video. You can enable/disable each of the tracks. Press the “Next” button after specifying the content type and parameters for the output. Now you will be prompted to enter the session address and the port number for each track. The session address should be the IP address of the machine which is supposed to receive the media.

The port number should be an unused port number supported in the destination machine. You may try an integer in the range 7000 to 12,000. You may give the value of the TTL field as "1".

Step 1

2

Advanced Networks

Choose file if you wish to transmit media from a file like mpg

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

Choose capture if you wish to transmit media from webcam or microphone

You may stick to the defaults here

3

Advanced Networks

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

Used port Destination IP address

Reception To receive media using JMStudio use the option “open RTP session” under the file menu. You will be prompted to specify the session address, port number and TTL. Give the IP address of the machine whose transmission you wish to receive as the session address. The port number of the “open RTP session” should be the port number to which the transmitter is transmitting. Address of the sender that we want to listen to

4

Advanced Networks

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

Part 2 : Implementing RTP Sender and Receiver Using Java The code is well-commented so kindly reed the comments to understand the code. MediaSender.java 1 import java.io.IOException; 2 import java.net.MalformedURLException; 3 import javax.media.*; 4 import javax.media.format.VideoFormat; 5 import javax.media.protocol.*; 6 7 public class MediaSender { 8 9 //webcam formats needed to build RTP stream 10 static final Format[] FORMATS = new Format[] {new VideoFormat(VideoFormat.RGB)}; 11 static final ContentDescriptor CONTENT_DESCRIPTOR =new ContentDescriptor(ContentDescriptor.RAW_RTP); 12 13 14 public static void main(String[] args) throws MalformedURLException, IOException, NoDataSourceException, NoProcessorException, CannotRealizeException, NoDataSinkException { 15 // this code is for viewing the capture devices , we won't need it but it could be useful for you 16 /* Vector devices=CaptureDeviceManager.getDeviceList ( null ); 17 18 CaptureDeviceInfo captureDeviceInfo= (CaptureDeviceInfo) devices.elementAt ( 0 ); 19 20 MediaLocator camDeviceMediaLocator = captureDeviceInfo.getLocator(); 21 */ 22 // here, we are creating a device info for camera 23 CaptureDeviceInfo webcamInfooo = new CaptureDeviceInfo("Camera", new MediaLocator("vfw://0"),null); 24 25 // simply,creating a locator for the cam 26 MediaLocator camDeviceMediaLocator =webcamInfooo.getLocator();//MediaLocator for Camera 27 28 // creating a source that will be used in creating the processor 29 DataSource source = Manager.createDataSource(camDeviceMediaLocator); 30 31 //creating the processor form the source and formats that we want (RTP) 32 Processor mediaProcessor = Manager.createRealizedProcessor( new ProcessorModel(source, FORMATS, CONTENT_DESCRIPTOR)); 33 34 // this is the output medialocator : ip, port and data type 35 MediaLocator outputMediaLocator = new MediaLocator("rtp://192.168.1.4:10000/video"); 36 37 // now , we are creating a datasink from the processor's output datasource and send it to output locator 38 DataSink dataSink = Manager.createDataSink(mediaProcessor.getDataOutput(),outputMediaLocator); 39 40 // start the processor 41 mediaProcessor.start(); 42 43 //open connection 44 dataSink.open();

5

Advanced Networks

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

45 46 //start streaming the RTP data 47 dataSink.start(); 48 49 } 50 }

If you want to transmit to a different destination, change the IP at line 35. However, if you want to broadcast to many receivers send your stream to 192.168.1.255 in our example (remember, this is a broadcast domain). Recevier.java 1 import java.awt.BorderLayout; 2 import javax.media.*; 3 import javax.swing.JFrame; 4 5 public class Recevier { 6 7 public static void main(String[] args) { 8 try { 9 Player Player; 10 // medialocator to receive data from this url : includes the sender that we want to receive data from 11 MediaLocator url = new MediaLocator("rtp://192.168.1.2:10000/video"); 12 13 //creating a player to receive data 14 Player = Manager.createRealizedPlayer(url); 15 16 JFrame f = new JFrame("test :)"); 17 f.setLayout(new BorderLayout()); 18 19 // well, that's important: we are adding the player's visual component to the frame 20 f.add(Player.getVisualComponent(), BorderLayout.CENTER); 21 22 // important: here we are adding the contorl compnent of the player 23 f.add(Player.getControlPanelComponent(), BorderLayout.SOUTH); 24 25 f.setSize(400, 400); 26 f.setLocationRelativeTo(null); 27 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 28 f.setVisible(true); 29 30 Player.start(); 31 } catch (Exception ex) { 32 } 33 } 34 } 35

6

Advanced Networks

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

What about audio? Sender side : Now if we want to transfer voice instead of video, we can simply change the formats(line 10,11 in the sender) to : static final Format[] FORMATS = new Format[] {new AudioFormat(AudioFormat.ULAW_RTP)}; static final ContentDescriptor CONTENT_DESCRIPTOR =new ContentDescriptor(ContentDescriptor.RAW_RTP);

and also line 23 to the following : CaptureDeviceInfo webcamInfooo =new CaptureDeviceInfo("DirectSoundCapture", new MediaLocator ("dsound://") ,null);

Finally we have to indicate to the receiver that he is receiving audio ! line 35 MediaLocator outputMediaLocator = new MediaLocator("rtp://192.168.1.255:10000/audio");

Receiver side : More simple since it's just audio ! Recevier.java 1 import javax.media.*; 2 3 public class Recevier { 4 5 public static void main(String[] args) { 6 try { 7 Player Player; 8 // medialocator to receive data from this url : includes the sender that we want to receive data from 9 MediaLocator url = new MediaLocator("rtp://192.168.1.4:10000/audio"); 10 11 //creating a player to receive data 12 Player = Manager.createRealizedPlayer(url); 13 Player.start(); 14 } catch (Exception ex) { 15 } 16 } 17 } 18

7

Advanced Networks

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

Part 3 : Capturing the stream using wireshark This should be a piece of cake to you ! this is some example. But note that we will inform wireshark that this is an RTP stream ! follow the steps

8

Advanced Networks

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

Now wireshark can decode this stream as RTP stream:

9