libstreaming 源碼分析一之RTSP連接

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協議的分析,此處不做詳細說明。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章