Java media framework basic tutorial (3)

Source: Internet
Author: User
Tags rfc dedicated ip
Section 5. transmitting and receiving media
JMF and real-time transmission protocol (RTP)
Many friendly network features are directly built in JMF, which makes it easy for client programs to transmit and receive media over the network. When a user on a network wants to receive media streams of any type, it does not need to wait for all broadcasts to be downloaded to the machine before watching the media; users can watch broadcasts in real time. This concept is put forward in streaming media. Through Streaming Media, a network client can receive audio broadcast from other machines or even obtain live video broadcasts that are happening.
Ietf rfc 1889 defines the real-time transmission protocol (RTP ). Rapid and reliable data transmission over the network is extremely time sensitive. RTP is used in JMF to provide users with methods to transmit media streams to other network nodes.
In this section, we will learn about our last example program. Here, you will learn how to transfer an MP3 file stored on one machine to another machine on the same network. The actual MP3 source file is not removed from the master computer, nor is it copied to another machine. In fact, it will be converted into a file format transmitted using RTP and sent over the network. Once received by a client, the source file (now in the form of RTP packets) can be transmitted again. This is a format that can be played on the receiving machine.
View the source code in the mediatransmitter. Java file and learn the following exercises.
Set processing mode
We can discuss the final example based on the processing mode defined in the previous chapter. On the transmission machine, the processing mode looks like this:
In fact, the source code of the mediatransmitter object includes the following three lines:
Private medialocator = NULL;
Private datasink = NULL;
Private processor mediaprocessor = NULL;
The three instance variables can be directly mapped to the preceding processing mode chart, as shown below:
· The mediaprocessor variable is our processor. It will be responsible for converting audio files from the MP3 file mode to a format suitable for transmission through the RTP protocol.
· The datasink variable is our output block.
· When creating a datasink, We need to specify a medialocator file, which is the target file of datasink.

When we run datasink, our processed media will be transmitted to the location specified in medialocator.

RTP medialocator
In the previous two exercises, we created a medialocator instance through a URL obtained from the file. In this exercise, we must create a medialocator to describe the media transmission output stream on the network. In other words, we must create a medialocator that can be used for our audio transmission destination. A rtp medialocator conforms to the following rules and looks like a typical URL:

RTP: // address: Port/Content-Type

Let's take a look at each section of the above URL specification:
· Address is the address of the media to be transmitted. Transmission in Unicast mode (a dedicated IP address). The IP address will be the IP address of the machine you want to receive. Spread in broadcast mode (to all machines in the subnet), The address will be 255 as the last subnet address. For example, if I can specify the IP address 192.168.1 In the subnet and want to spread it to all nodes in the subnet, I can specify 192.168.1.255 as the address, so that each node in the subnet can listen to broadcast media.
· Port must be a port allowed by both the transmitter and receiver.
· Content-type is the media stream type. In our case, this will be audio.
The following example of a simple RTP propagation medialocator allows all machines in the specified network to receive a media stream:
RTP: // 192.168.1.255: 49150/Audio
Create a processor
In the setdatasource () method, we first need to create a processor instance.
The following processor is responsible for converting MP3 audio media into an RTP representation:
Public void setdatasource (datasource DS) throws ioexception,
Noprocessorexception, cannotrealizeexception, nodatasinkexception {
Mediaprocessor = manager. createrealizedprocessor (
New processormodel (DS, formats, content_descriptor ));
In the manager class, we can create a processor object by either of the following methods:
Createprocessor () or createrealizedprocessor (). You may notice that the display of the two methods is similar to the method used to create a player in the previous example. In the current example, we will create an implemented processor. We do this because the application we use is very simple. When processo is in the realized state, we do not need to care about any real work.
Create a processormodel
To create an implemented processor, we need to create a processormodel instance that describes the input and output media types for the processor. To create a processormodel, we need the following:
· A datasource that processes the media (input file ).
· A javax. Media. Format array that describes the format of the input media.
· A javax. Media. Protocol. contentdescriptor instance describes the output format for our processor. The datasource of the transmitter is passed to this method through a parameter.
Define input and output formats
Because our mediatransmitter class is often used to convert the input media format (mp3) into an output format (audio RTP), the middle school object is defined as static. We create a new javax. Media. format. audioformat instance to describe the media input type (view available formats in the Java help documentation ). This is why our processor can obtain MP3 audio files.
We also create a javax. Media. Protocol. contentdescriptor instance to describe what the processor outputs. In our case, this is an RTP media stream.
This is why our processor can only create RTP streams.
The following code snippet shows how to set the format and content descriptor variables to create a processormodel object:
Private Static final format [] formats = new format [] {
New audioformat (audioformat. mpeg_rtp )};
Private Static final contentdescriptor content_descriptor =
New contentdescriptor (contentdescriptor. raw_rtp );
Connect input, processor, and output
Now we have a processor in the realized state. We need to set datasink to actually spread RTP media. Creating a datasink is a simple way to call another manager object, as shown below:
Datasink = manager. createdatasink (mediaprocessor. getdataoutput (),
Medialocator );
The createdatasink () method is used to obtain the output (as a datasource parameter) and medialocator objects of the new processor. We create both the mediatransmitter object and the mediatransmitter object. In this way, you can start how our different components are linked in the processing mode: We get the output from a processor and use them as input to other components. In this special application, processor outputs an input for the datasink used to transmit the media.
Create a datasource instance
At this point, we have all done and set up Broadcast Transmission for our media players.
We need to create a datasource object, which is used to create a processor (that is, in our mediatransmitter, the parameter is passed to the setdatasource () method ). The following code creates a datasource instance:
File mediafile = new file (ARGs [1]);
Datasource source = manager. createdatasource (New medialocator (
Mediafile. tourl ()));
This code is the vmain () method in the mediatransmitter object. Here, we create a file object using the second parameter input from the command line. We create a medialocator file, and then create a datasource through location. The newly created datasource is an input file involving the transmitter. We can use this datasource to initialize the transmitter.
Start and Stop mediatransmitter
Call starttransmitting () to start mediatransmitter, as shown below: Public void starttransmitting () throws ioexception {
Mediaprocessor. Start ();
Datasink. open ();
Datasink. Start ();
}
This method first enables the processor, and then enables and starts datasink. After this call, the receiving machine can listen on the media transmitter.
It is very easy to stop the transmitter. Run the following code to stop and close both datasink and processor:
Public void stoptransmitting () throws ioexception {
Datasink. Stop ();
Datasink. Close ();
Mediaprocessor. Stop ();
Mediaprocessor. Close ();
}
Compile and run mediatransmitter
By entering javac mediatransmitter. Java in the command line to compile the example program, you can generate a. Class file with the same name in your working directory.
To run the sample program, enter the following code in the command prompt line:

Java mediatransmitter rtpmedialocator audiofile
This example creates a media broadcast for the myaudioworkflow file. Do not forget to replace rtpmedialocator with a media transmission rtp url, as discussed previously.
You also need to replace audiofile with the audio file name of your local machine.
All relative file names are relative to the current working directory. You will see some information indicating the file being played. Press enter to stop playing.

For example, the command line interaction for the transmitter is as follows:
Java mediatransmitter RTP: // 192.168.1.255: 49150/audio myaudioaudio
If the initial editing fails, make sure that the jmf jar file contains the classpath environment variable. To explore this program and practice, please refer to the mediatransmitter source code.

Receiving transmitted Media
Now you may ask, "If no one can watch or listen to it, what's the best about the media ?"
Fortunately, you only need to make minor changes to the mediaplayerframe source code of the second program to set a client to receive the media.
The mediaplayerframe class requires a very small adjustment to receive and play audio files. In the main () method, You Need To comment out the following line:

MPF. setmedialocator (New medialocator (new file (ARGs [0]). tourl ()));
Enter the following line:
MPF. setmedialocator (New medialocator (ARGs [0]);
This simple change allows us to create a medialocator object through string, instead of creating a file to create medialocator.
All other codes are the same.
Specify RTP URL
The 12-page description shows how to compile and run the mediaplayerframe sample program. The only difference is that you need to specify the rtp url for the transmitter. For the receiver's example, the command line interaction is as follows:
Java mediaplayerframe RTP :/// 192.168.1.255: 49150/Audio
Precautions for running network media Transmitter
If you only have the permission to use one machine on the network, you can still run the transfer program. When you start the transmitter, you can use the rtp url or the address of the machine on which you work. To adjust transmission, the receiver must use the same rtp url exactly before the start.
If you run the real online version of this example, JMF needs to be installed on each machine you use, whether it is transmitting or receiving media streams. This is required because JMF APIs are used in a large number in both the transmitter and receiver programs.
In either case, make sure that the same address and port are used in the specified rtp url; otherwise, media transmission will not work.Section 6 constraints and resources
Summary
I hope this Guide can provide you with useful views on how to use JMF APIs.
We have created three small applications for playing local audio and video, and transmitting and receiving media through the network. The source code of these applications contains many javadoc-style annotations. This helps you understand your remaining problems.
Many JMF topics are not covered in this Guide. In fact, we are more concerned with the basic concepts and applications of JMF. On this basis, we can easily expand other areas of learning. To dive deeper into JMF applications, you may want to learn the topics mentioned in the panel below. Take a closer look at the topics in this Guide and refer to the 23-page resource.

Advanced topic
A large number of exercises worth doing are beyond the scope of this Guide. In a simple explanation, you can further learn about it. You can expand our application code, or demonstrate your knowledge about JMF. Start with the following exercise:
· Media capture: JMF contains a wide range of APIs to capture media data. If you are interested in using JMF to capture media, you can use the javax. Media. capturedevicemanager class and javax. Media. Protocol. capturedevice APIs to learn. For an advanced exercise, consider using the capturedevicemanager and capturedevice interfaces to add the media capture function to the GUI version of the media playback application.
· Session management: As this Guide describes JMF, we make the output very simple and only implement javax. Media. datasink output.
In addition, the output indicates that javax. Media. RTP. sessionmanager is used. This management class allows clients to create and monitor their RTP streams and connections. Using sessionmanager and subsequently creating a stream, it may very close to monitoring RTP sessions. As an advanced exercise, transform the three demo programs to use sessionmanager, and then listen to the clients that are listening to the outbound RTP stream.
· Use JMF multi-point transfer: our broadcast demonstration Application explains how to transfer media from one network to one or more machines in another network. It may also use the multi-point transmission protocol in JMF to provide more complex and multi-user networks.
The JMF User Guide provides a more in-depth discussion of the multicast protocol using JMF. Further track this topic to view 23 pages of resources.
· Video transmission: Our last demonstration Application focuses on how to transmit an MP3 audio file, but JMF can also transmit video over the network. Follow the format and contentdescriptor classes in the API documentation to learn how to use better methods.
· Import/export RTP media streams: JMF allows you to save RTP streams as files for future use. For example, a remote Telecommunications Conference can be saved and viewed later.
Because the stream has been saved and then in the RTP format, it does not need to be converted again, which can lead to performance improvement of the Transmission program. Set the input/output medialocator in the datasink object through a file instead of a URL. You will find a deeper topic discussion in the JMF user guide.

Resources
JMF
· Download mediaplayer. jar, the complete sample source code used in this Guide.
· JMF homepage (http://www.javasoft.com/jmf) is the best way to explore more information resources of JMF.
· You can find the JMF Statement (http://java.sun.com/products/java-Media/JMF/2.1.1/specdownload.html), and then the Java developer alliance includes the API documentation and JMF user guide. You must have the right to use all of these resources, if you want to do any more in-depth JMF programming.
· Official JMF support file format pages
(Http://java.sun.com/products/java-media/jmf/2.1.1/formats.html) lists all file formats that can be recognized and played by JMF. This file format page also includes learning more about Capture Devices and RTP formats.
· MPEG-4 video for JMF (http://www.alphaworks.ibm.com/tech/mpeg-4) from IBM
AlphaWorks is a JMF video decoder.
RTP
Ietf rtp rfc (http://www.ietf.org/rfc/rfc1889.txt) describes the RTP protocol in great detail.
· View jmf api Guide
(Http://java.sun.com/products/java-media/jmf/2.1.1/specdownload.html), there are a lot of RTP protocols and descriptions and how it is applied on JMF.
· Emy of Columbia has a useful rtp faq (http://www.cs.columbia.edu /~ HgS/RTP/faq.html ).
Java Technology
· Java 2 platform, Standard Edition (http://java.sun.com/j2se/) available from Sun.
· Sun's guide on jfc/swing (http://java.sun.com/docs/books/tutorial/uiswing/index.html)
And AWT (http://java.sun.com/docs/books/tutorial/information/download.html#OLDui) is a great place to learn a lot about GUI programming in Java programming languages.
· Another sun guide to learn the basics of Network Programming
Http://java.sun.com/docs/books/tutorial/networking/index.html ).
Multi-Point transmission protocol
· Explicit multicast (xcast)
(Http://oss.software.ibm.com/developerworks/opensource/xcast/) is a form of IP multi-point transport that provides upgrading support for a very large number of multi-point Transport Group designs, represented by a small number of participants in these groups. The xcast code has been recognized by IBM common public license.
· Todd Montgomery's MTP page (http://www.nard.net /~ Tmont/rm-links.html ),
Here you can find a list that involves a wide range of multi-point transmission protocols.
Additional resources
· You can
Developerworks Java Technology Zone (http://www-106.ibm.com/developerworks/java/) find a lot of content about Java in all aspects.
· View the developerworks tutorials page
(Http://www-105.ibm.com/developerworks/education.nsf/dw/java-onlinecourse-bytitle? OpenDocument & COUNT = for a complete listing of free tutorials.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.