1. 程式人生 > >libstreaming 原始碼分析一之RTSP連線

libstreaming 原始碼分析一之RTSP連線

一、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 3
    illustrates 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.
二、視訊直播模型(圖一)
三、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協議的分析,此處不做詳細說明。