转:使用Java RTP传输声音和视频的程序,绝对经典

好多,不知道哪个是原版了,随便给个地址

http://hi.baidu.com/qinfengling/blog/item/7125f503750265753812bb23.html/cmtid/aa3aa3ec89f82a33269791a2

————————————————————————————————————————————————————————————————————

花了两天的时间,终于弄清楚了RTP传输问题了。拿出来和大家分享一下,使用的测试程序是sun公司提供的AVReceive2.java和AVTransmit2.java,大家可以在网上下载,也可以在我的Blog中下载。

主要说一下注意的关键问题:

1.出现Local Data AddressDoes not belong to any of this hosts local interfaces错误:

主要问题是发送视频和音频的程序需要在服务器上运行,你可以选择Win2000 Server 或者Win2003 Server,问题就解决了。

2.传输声音和视频的方法如下:

a.传输声音或者视频文件

编译完AVTransmit2.java后,再命令提示符中输入命令:java AVTransmit2 file:test.wav 224.224.123.123 22222(注 意文件的类型是.wav 或者.mov 、.mpg的文件,不可以是.mp3、.rmvb等其他不支持的文件。传输支持文件格式有限,我也没有办法,应该在添加相关的插件就行了,希望大家提供帮 助),其中test.wav即传输的文件名,224.224.123.123为多播地址,22222为端口号.

接收方法:编译完AVReceive.java后,在命令提示符中输入命令:java AVReceive 224.224.123.123/22222即可接受到声音文件

b.传输麦克风的音频,在传输之前先检查电脑录音控制的选项是否为麦克风.(步骤:打开桌面任务栏上的音量控制,选择选项---->属性 ----->调节音量选择录音,之后在下面的音量控制属性中选中麦克风。单击确定。接着跳到录音控制,选择麦克风就行了)。使用的命令是:java AVTransmit2 dsound:// 224.224.123.123 22222,此时就开始传输声音了。

接收方法同上

c.传输摄相头视频,使用的命令为:java AVTransmit2 vfw://0 224.224.123.123 22222

接收方法同上

d.关于广播、组播和单播

广播:对于局域网广播你可以使用广播的地址,如你的子网掩码是255.255.225.0,即C类的默认子网掩码,你的广播地址可以是 192.168.3.255。(注:我的局域网ip地址分配为192.168.3.X)。如子网掩码不同,你可以参照相关的网络常识自己推算。

接收时也使用多播地址来接受,假如发送方的机器地址为:192.168.3.46。发送时在发送放的机器上运行java AVTransmit2 file:test.wav 192.168.3.255 22222,接收时使用java AVReceive2 224.224.123.123 22222。才能完成接收.这和网上的说法不同,端口号不要填错,地址任意。按网上的说法,使用的接收地址应为为192.168.3.46,可是我没有成功,总是出现Local Data AddressDoes not belong to any of this hosts local interfaces的错误,而使用多播地址反而成功了。具体的参数我就不多介绍了,有问题可以给我留言。

  组播:使用组播地址发送,组播地址接收即可

  单播:假如你只想给某台机子发送,那么就在发送方输入命令,如:java AVTransmit2 file:test.wav 192.168.3.47 22222,这时你只会将声音流文件发送给47号计算机。而接受时还是使用多播地址,如java AVReceive2 224.224.123.123 22222。这是就听到声音了。

  总之,使用RTP传输,在接受时都在使用多播地址,在发送时根据情况而定,至于ttl你可以不去管它。不只大家是怎么实现的,反正网上的资料让我变的很失望,真正的成功源于实践。下面摘取一段让大家欣赏(尽信书则不如无书)

网上摘取:

   Transmitting Audio and Video over RTP

   The AVTransmit2 class is very similar to the VideoTransmit, but uses RTP managers

   to send the video and audio streams.

   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 either:

   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.

    OR

    Open RTP session and specify <sourcIP> and <port>

How to run this sample

1. Run AVTransmit2 with the required 3 command line parameters

For example, we can use any of the following:

- java AVTransmit2 file:clips/clip01mpg.mpg 224.112.112.112 22222

2. To receive the transmission on the client side use JMStudio:

- use open RTP session and specify group: 224.112.112.112 & port: 22222

AND use FILE -> New Window and open RTP sesssion with port 22224.

OR

- use open URL and specify: rtp://224.112.112.112:22222/video

AND use FILE -> New Window and open URL with 22224/audio

Notes:

  • You should run 1. then 2., otherwise AVTransmit2 will find the port number used.
  • You can also use the program to send only audio or video as follows:

- java AVTransmit2 javasound://0 224.112.112.112 22222 (audio only)

- java AVTransmit2 vfw://0 224.112.112.112 22222 (video only)

In such case create only one instance of JMStudio.

  • Use Unicast:
- java AVTransmit2 file:clip01.mpg 128.82.4.7 22222

Where 128.82.4.7 is the receicver address.

If the sender address is 128.82.4.9 it will use port 22222 as well

to send out data. In this case the receiver (e.g., JMStudio)

should specify the sender as: 128.82.4.9 22222.

Therefore to use unicast you should have two machines since

you can not use the same port for both sender and receiver.

Receiving Audio and Video using RTP

AVReceive2 uses the RTPManager API to receive RTP transmissions.

AVReceive2 performs the following tasks:

  • Open one RTP session per session address given.
  • Listen for the NewReceiveStreamEvent from the ReceiveStreamListener.
  • Create a JMF Player for each stream received for playback.

This sample program can be used in conjunction with JMStudio,

the AVTransmit2 sample or any other RTP compliant transmitter.

  • The IP address should be the address of the computer which transmits the data; or the multicast address if multicast is being used for the transmission.
  • The ports should be the same as what's being used by the transmitter.

How to run this sample

1. Run AVTransmit2 with the required 3 command line parameters

For example:

- java AVTransmit2 file:clips/clip01mpg.mpg 224.112.112.112 1234

2. Run AVReceive2 and specify the RTP session addresses to receive from.

For example:

- java AVReceive2 224.112.112.112/1234 224.112.112.112/1236

to simultaneously receive 2 different RTP sessions (video and audio).

Note: because of port reuse, it must run in this order, 1 then 2.___ ____________________________________________________________________________________________________________________

我在Windows下发送和接收都用单播成功了,但是在ubuntu11.04下单播不行,发送单播/接收组播可以,当然都是组播也可以。

——————————————————————————————————————————————————————————————

另:ubuntu下单播不行,是否跟以下内容有关系:

问题解决了,不是程序的问题,是linux的问题,linux中默认的localhost定义为127.0.0.1,修改配置即可

晕,我通过InetAddress.getLocalHost().getHostAddress()在ubuntu下取出来的“127.0.1.1”,这个绝对有问题,嗯,另开一帖记录。