转:使用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”,这个绝对有问题,嗯,另开一帖记录。

 

 

转载于:https://www.cnblogs.com/cuizhf/archive/2011/10/20/2218673.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java使用RTP协议发送视频需要经过以下几个步骤: 1. 创建RTP会话:首先,我们需要创建一个RTP会话对象。可以使用Java中的相关库,如JMF(Java Media Framework)或Xuggler等,来创建和管理RTP会话。 2. 初始化发送端:通过RTP会话对象,我们可以初始化RTP发送端。这一步骤包括设置IP地址、端口号等相关信息,以便发送视频数据。 3. 打开视频源:通过Java的多媒体库,我们可以打开视频源文件或者设备,如相机或视频文件。可以使用JMF库中的相关类来打开视频源。 4. 读取视频数据:通过视频源,我们可以读取视频数据。这些数据可以是视频文件中的连续帧,或者是由实时视频传输而来的帧。 5. 封装RTP包:将读取到的视频数据封装为RTP数据包。RTP数据包包括视频帧数据以及相关的时间戳、序列号等信息。 6. 发送RTP包:通过之前初始化的RTP发送端,将封装好的RTP包发送到目标地址。发送过程可以使用Java中的网络传输相关的类和方法来完成。 7. 循环读取和发送:重复步骤4到步骤6,以持续发送视频数据。视频源会源源不断地提供新的视频帧,我们需要读取并发送这些帧,以实现持续的视频传输。 需要注意的是,RTP协议只负责对视频帧进行传输,不涉及编解码等其他处理。因此,我们需要在发送端和接收端分别进行相应的编解码操作,以使接收端能够正确地解析和播放接收到的视频数据。Java中可以使用FFmpeg、VLC或者JCodec等库来进行视频编解码操作。 总结起来,使用Java发送视频需要创建RTP会话,初始化发送端,打开视频源,读取视频数据,封装为RTP包,发送RTP包,并持续循环读取和发送视频数据。同时,在发送端和接收端还需要进行相应的视频编解码操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值