Transmitting Audio and Video using RTP

来源:互联网 发布:开淘宝店进什么货 编辑:程序博客网 时间:2024/05/02 20:03

http://java.sun.com/javase/technologies/desktop/media/jmf/2.1.1/solutions/AVTransmit.html

 

Problem
How to transmit media over the network using JMF and JMF/RTP. You may want to transmit media from one computer to another. Or you may want to broadcast a live feed to your company's intranet. The source of the media can be a file,  live media using a capture device or any other source supported by JMF.

Solution
The AVTransmit2 class is similar to VideoTransmit except that it attempts to transmit each of the tracks within the given input media. To do that, an RTP session is created for each media track. For that purpose, the RTP Session API instead of the DataSink API is used for flexibility. For JMF 2.1.1 users, an alternative sample that uses the new RTPManager API is also provided. The default (first available) RTP format is set for each track. For video, special attention is taken to ensure that the input sizes are usable for RTP transmission. Real-time scaling is applied when necessary.

Note that due to limitations of the JMF 2.1 implementation, audio and video are not in tight synchronization.

The AVTransmit2 class takes three parameters in the constructor - the source locator, the destination IP address and a base destination port number for all the media tracks.

The source locator can be -

  • a file name such as "file:/C:/media/speech.mov",
  • an http locator such as "http://mediacentral.com/speech.avi",
  • or a capture datasource such as "javasound://8000"

The IP address should be the IP address of the computer that should receive your transmission. If you want all the computers on your subnet to receive the transmission, then use 255 as the last number in your IP address. For example, "129.130.131.132" can be used for a point to point transmission and "129.130.131.255" can be used for all computers that are on your subnet. Consult your system administrator if you're not sure what these IP addresses mean. You can also refer to the JMF RTP documentation in the JMF Programmer's Guide for more information on RTP transmission and reception.

The base Port number can be any port number that is not in use by any other service on your computer. For example, you can use "22222". Make sure that it is an even number. Once again, refer to the Programmer's Guide. The first media track will be transmitted from the base port number. The next track will go to base port number plus 2 and so on.

The sample program can be used as a utility class as well as standalone. There is a main method that takes three command line parameters. If all goes well, the media will be sent out for 300 seconds and then the application will exit. This 300 seconds limit is because there is no GUI that allows you to specify when to stop the transmission.

Requirements Platform:JDK 1.1.6 or laterJMF API:2.0 or laterImplementation:AJ, WPP, SPP *Hardware: [optional]Capture card required for live capture.

* AJ = All Java, WPP = Windows Performance Pack, SPP = Solaris Performance Pack Related Classes 

  • javax.media.Manager
  • javax.media.Processor
  • javax.media.protocol.DataSource
  • javax.media.rtp.SessionManager
 How to run this sample 
  1. Download and compile the source file.
  2. Run AVTransmit2 with the required 3 command line parameters
    For example,

    java AVTransmit2 file:/avmedia.mov 129.144.251.182 42050
     
  3. Or write your own little program that uses the AVTransmit2 class to create, start and stop an RTP transmit session.
  4. Since the media tracks are transmitted in multiple sessions, you'll need to use one Player per track on the receive side. Using JMStudio, you can start multiple Players from the "File" menu using the "New Window" item. Then use the "Open URL..." item to open one RTP session per track. The URL to use is:

    rtp://<sourceIP>:<port>/media

    Where <sourceIP> is the IP address of the RTP session and the port number is the same one that is used on the transmitting side.

SourceCode: