libstreaming 源码分析一之RTSP连接

来源:互联网 发布:东华大学网络教育平台 编辑:程序博客网 时间:2024/06/05 08:20
libstreaming(github下载网站: https://github.com/fyhertz/libstreaming  )

一、libstreaming概述
 
功能介绍:实现android视频直播的库,主要实现的功能有RTSP协议、H263/H264编码、RTP/RTCP协议打包等。

libstreaming is an API that allows you, with only a few lines of code, to stream the camera and/or microphone of an android powered device using RTP over UDP.

  • Android 4.0 or more recent is required.
  • Supported encoders include H.264, H.263, AAC and AMR.

The first step you will need to achieve to start a streaming session to some peer is called 'signaling'. During this step you will contact the receiver and send a description of the incomming streams. You have three ways to do that with libstreaming.

  • With the RTSP client: if you want to stream to a Wowza Media Server, it's the way to go. The example 3illustrates that use case.
  • With the RTSP server: in that case the phone will act as a RTSP server and wait for a RTSP client to request a stream. This use case is illustated in the example 1.
  • Or you use libstreaming without using the RTSP protocol at all, and signal the session using SDP over a protocol you like. The example 2 illustrates that use case.

The full javadoc documentation of the API is available here: http://majorkernelpanic.com/libstreaming/doc-v4

二、视频直播模型(图一)


三、example 示例
libstreaming 库的作者提供了3个示例demo,见(一)。
example1 是将libstreaming作为 RTSP server ,在手机上安装并启动net.majorkernelpanic.streaming.rtsp.RtspServer,在PC上用vlc打开rtsp://your rtspserver ip:8086,即可实现视频直播。
example2 是利用其它协议实现视频直播,未研究,此略。
example3 是将libstreaming作为 RTSP client ,通过net.majorkernelpanic.streaming.rtsp.RtspCilent将视频流push到wowza服务器,在PC上用VLC打开rtsp://your wowzastreaming ip:1935/live/test.stream,即可实现视频直播。

注意RtspServer与RtspClient的区别,也就是流媒体pull与push的区别。见下图。





 

因为可能有多个视频采集端,我们需要采集端主动推送视频流至流媒体服务器。
注意:libstreaming中RtspClient协议只能成功连接wowza流媒体服务器,若要rtsp连接至dss,需要修改RtspClient。

四、example3流程

example3只要一个MainActivity.java。主要是为Session、RtspClient设置初值,如分辨率、帧率、编码格式等。
除了MainActivity.java,其他java文件包名均为“net.majorkernelpanic.streaming”。大致流程如下:

(1)开启相机预览

MainActivity.java


Session.java


VideoStream.java:开启相机预览



(2)RTSP连接

MainActivity.java: 点击button “R.id.start”,进入 toggleStream 方法,关键代码:

前面已经根据输入内容,设置了连接wowza服务器的地址、端口、用户名和密码。


RtspClient.java:发送rtsp协议,关键代码





其中,sendRequestAnnounce() 、 sendRequestSetup() 、 sendRequestRecord() 分别发送RTSP中的 ANNOUNCE、SETUP、RECORD方法至wowza。下面是打印的log:


从log信息可以看出rtsp连接成功,只需在播放端用vlc播放地址:rtsp://wowza ip:1935/live/test.stream 。关于RTSP协议的分析,此处不做详细说明。
0 0