可以先了解一下RTSP/RTP/RTCP的概念與區別:RTP與RTCP協議介紹(轉載)。
在調試vlc-android時,熟悉了RTSP的會話流程。C表示RTSP客戶端,S表示RTSP服務端:
- 第一步:查詢服務器端可用方法
1.C->S:OPTIONrequest //詢問S有哪些方法可用
1.S->C:OPTIONresponse //S回應信息的public頭字段中包括提供的所有可用方法
- 第二步:得到媒體描述信息
2.C->S:DESCRIBE request //要求得到S提供的媒體描述信息
2.S->C:DESCRIBE response //S回應媒體描述信息,一般是sdp信息
- 第三步:建立RTSP會話
3.C->S:SETUPrequest //通過Transport頭字段列出可接受的傳輸選項,請求S建立會話
3.S->C:SETUPresponse //S建立會話,通過Transport頭字段返回選擇的具體轉輸選項,並返回建立的Session ID;
- 第四步:請求開始傳送數據
4.C->S:PLAY request //C請求S開始發送數據
4.S->C:PLAYresponse //S回應該請求的信息
- 第五步: 數據傳送播放中
S->C:發送流媒體數據 // 通過RTP協議傳送數據
- 第六步:關閉會話,退出
6.C->S:TEARDOWN request //C請求關閉會話
6.S->C:TEARDOWN response //S回應該請求
上述的過程只是標准的、友好的rtsp流程,但實際的需求中並不一定按此過程。其中第三和第四步是必需的!第一步,只要服務器客戶端約定好,有哪些方法可用,則option請求可以不要。第二步,如果我們有其他途徑得到媒體初始化描述信息(比如http請求等等),則我們也不需要通過rtsp中的describe請求來完成。
RTSP服務器默認端口是554,在客戶端SETUP的時候會把自身的RTP和RTCP端口告知服務器。在RTSP的session建立后,會使用RTP/RTCP在約定好的端口上傳輸數據。從調試本地IPC打印的日志可以看出基本的RTSP流程,其中包含了注釋,以#開頭,建立流程基本是按照上述步驟來的:
#點擊播放后,java層打印的信息 V/VLC/PlaybackService: Loading position 0 in [org.videolan.medialibrary.media.MediaWrapper@2fa087e4] D/VLC: [b48cf028/5d6] core input: Creating an input for 'rtsp://10.3.20.185/onvif-media/media.amp' D/VLC: [b48cf028/5f5] core input: using timeshift granularity of 50 MiB D/VLC: [b48cf028/5f5] core input: using default timeshift path D/VLC: [b48cf028/5f5] core input: `rtsp://admin:admin@10.3.20.185/onvif-media/media.amp?profile=profile_1_h264&sessiontimeout=60&streamtype=unicast' gives access `rtsp' demux `any' path `admin:admin@10.3.20.185/onvif-media/media.amp?profile=profile_1_h264&sessiontimeout=60&streamtype=unicast' D/VLC: [a095a588/5f5] core input source: creating demux: access='rtsp' demux='any' location='admin:admin@10.3.20.185/onvif-media/media.amp?profile=profile_1_h264&sessiontimeout=60&streamtype=unicast' file='(null)' D/VLC: [a0829228/5f5] core demux: looking for access_demux module matching "rtsp": 6 candidates D/VLC: [a0829228/5f5] live555 demux: version 2016.11.28 W/VLC: [a0829228/5f5] core demux: Password in a URI is DEPRECATED #[vlc-android/vlc/contrib/contrib-android-arm-linux-androideabi/live555/liveMedia/RTSPClient.cpp/connectToServer()] #使用RTSPClient嘗試連接RTSPServer E/VLC-std: Opening connection to 10.3.20.185, port 554... #[vlc-android/vlc/contrib/contrib-android-arm-linux-androideabi/live555/liveMedia/RTSPClient.cpp/connectionHandler1()] #接收到遠程的回復 E/VLC-std: ...remote connection opened #[vlc-android/vlc/contrib/contrib-android-arm-linux-androideabi/live555/liveMedia/RTSPClient.cpp/sendRequest()] #sendRequest函數打印出來的消息 Sending request: OPTIONS rtsp://10.3.20.185:554/onvif-media/media.amp?profile=profile_1_h264&sessiontimeout=60&streamtype=unicast RTSP/1.0 CSeq: 2 User-Agent: LibVLC/3.0.0-git (LIVE555 Streaming Media v2016.11.28) I/VLC/BitmapCache: LRUCache size set to 107374182 I/VLC/PlaybackService: Media.Event.MetaChanged: 12 I/VLC/PlaybackService: Media.Event.MetaChanged: 12 #[vlc-android/vlc/contrib/contrib-android-arm-linux-androideabi/live555/liveMedia/RTSPClient.cpp/handleResponseBytes()] #接收到遠程的回復信息 E/VLC-std: Received 143 new bytes of response data. E/VLC-std: Received a complete OPTIONS response: RTSP/1.0 200 OK CSeq: 2 #回應信息的public頭字段中包括Server提供的所有可用方法 Public: DESCRIBE, GET_PARAMETER, PAUSE, PLAY, SETUP, SET_PARAMETER, TEARDOWN Date: Wed, 19 Apr 2017 07:21:53 GMT E/VLC-std: Sending request: DESCRIBE rtsp://10.3.20.185:554/onvif-media/media.amp?profile=profile_1_h264&sessiontimeout=60&streamtype=unicast RTSP/1.0 CSeq: 3 User-Agent: LibVLC/3.0.0-git (LIVE555 Streaming Media v2016.11.28) Accept: application/sdp E/VLC-std: Received 199 new bytes of response data. #Server回復需要認證 E/VLC-std: Received a complete DESCRIBE response: RTSP/1.0 401 Unauthorized CSeq: 3 WWW-Authenticate: Digest realm="AXIS_WS_ACCC8E41A320", nonce="00058c7aY52967318f73844d686966f513cfdc97f2b1db", stale=FALSE Date: Wed, 19 Apr 2017 07:21:53 GMT E/VLC-std: Resending... #Client發送認證信息 E/VLC-std: Sending request: DESCRIBE rtsp://10.3.20.185:554/onvif-media/media.amp?profile=profile_1_h264&sessiontimeout=60&streamtype=unicast RTSP/1.0 CSeq: 4 Authorization: Digest username="admin", realm="AXIS_WS_ACCC8E41A320", nonce="00058c7aY52967318f73844d686966f513cfdc97f2b1db", uri="rtsp://10.3.20.185:554/onvif-media/media.amp?profile=profile_1_h264&sessiontimeout=60&streamtype=unicast", response="79c25155ce602bdda837ece72c5f5508" User-Agent: LibVLC/3.0.0-git (LIVE555 Streaming Media v2016.11.28) Accept: application/sdp E/VLC-std: Received 856 new bytes of response data. E/VLC-std: Received a complete DESCRIBE response: RTSP/1.0 200 OK CSeq: 4 Content-Type: application/sdp Content-Base: rtsp://10.3.20.185:554/onvif-media/media.amp/ Date: Wed, 19 Apr 2017 07:21:55 GMT Content-Length: 678 v=0 #version,SDP協議的version o=- 1492586515743424 1492586515743424 IN IP4 10.3.20.185 s=Media Presentation #session name e=NONE #email b=AS:50000 t=0 0 a=control:rtsp://10.3.20.185:554/onvif-media/media.amp?profile=profile_1_h264&sessiontimeout=60&streamtype=unicast a=range:npt=0.000000- m=video 0 RTP/AVP 96 #流媒體格式 c=IN IP4 0.0.0.0 #connect data連接的一些數據 b=AS:50000 a=framerate:30.0 a=transform:1.000000,0.000000,0.000000;0.000000,0.995192,0.000000;0.000000,0.000000,1.000000 a=control:rtsp://10.3.20.185:554/onvif-media/media.amp/trackID=1?profile=profile_1_h264&sessiontimeout=60&streamtype=unicast a=rtpmap:96 H264/90000 a=fmtp:96 packetization-mode=1; profile-level-id=4D4029; sprop-parameter-sets=Z01AKZpmA8ARPy4C1AQEBBen,aO48gA== D/VLC: [a0829228/5f5] live555 demux: RTP subsession 'video/H264' #請求Server建立會話 E/VLC-std: Sending request: SETUP rtsp://10.3.20.185:554/onvif-media/media.amp/trackID=1?profile=profile_1_h264&sessiontimeout=60&streamtype=unicast RTSP/1.0 CSeq: 5 Authorization: Digest username="admin", realm="AXIS_WS_ACCC8E41A320", nonce="00058c7aY52967318f73844d686966f513cfdc97f2b1db", uri="rtsp://10.3.20.185:554/onvif-media/media.amp/", response="0f81c42f0f6f9b380e429a0b5adef2ef" User-Agent: LibVLC/3.0.0-git (LIVE555 Streaming Media v2016.11.28) Transport: RTP/AVP;unicast;client_port=54584-54585 #RTP-RTCP端口號 D/VLC: [b48cf028/5f5] core input: selecting program id=0 D/VLC: [a0829228/5f5] live555 demux: setup start: 0.000000 stop:0.000000 #Server回復會話已建立 E/VLC-std: Received 198 new bytes of response data. Received a complete SETUP response: RTSP/1.0 200 OK CSeq: 5 Session: CF14A038; timeout=60 Transport: RTP/AVP;unicast;client_port=54584-54585;server_port=50092-50093;ssrc=534B2131;mode="PLAY" Date: Wed, 19 Apr 2017 07:21:56 GMT #Client請求Server傳送數據 Sending request: PLAY rtsp://10.3.20.185:554/onvif-media/media.amp?profile=profile_1_h264&sessiontimeout=60&streamtype=unicast RTSP/1.0 CSeq: 6 Authorization: Digest username="admin", realm="AXIS_WS_ACCC8E41A320", nonce="00058c7aY52967318f73844d686966f513cfdc97f2b1db", uri="rtsp://10.3.20.185:554/onvif-media/media.amp/", response="96a7a81455dad1a9d73958699cbd69a0" User-Agent: LibVLC/3.0.0-git (LIVE555 Streaming Media v2016.11.28) Session: CF14A038 Range: npt=0.000- D/VLC/VideoPlayerActivity: MediaRouter information : android.media.MediaRouter@1a0d4467 I/VLC/VideoPlayerActivity: No secondary display detected W/MediaRouter: removeCallback(org.videolan.vlc.gui.video.VideoPlayerActivity$1@d47b986): callback not registered D/VLC/VideoPlayerActivity: Continuing playback from PlaybackService at index 0 E/VLC-std: Received 256 new bytes of response data. #Server回復數據傳送信息 E/VLC-std: Received a complete PLAY response: RTSP/1.0 200 OK CSeq: 6 Session: CF14A038 Range: npt=0- RTP-Info: url=rtsp://10.3.20.185:554/onvif-media/media.amp/trackID=1?profile=profile_1_h264&sessiontimeout=60&streamtype=unicast;seq=30405;rtptime=29935497 Date: Wed, 19 Apr 2017 07:21:57 GMT D/VLC: [a0829228/5f5] live555 demux: We have a timeout of 60 seconds D/VLC: [a0829228/5f5] live555 demux: play start: 0.000000 stop:0.000000 D/VLC: [a0829228/5f5] core demux: using access_demux module "live555" D/VLC: [a0a54228/5f5] core packetizer: looking for packetizer module matching "any": 24 candidates D/VLC: [a0a54228/5f5] h264 packetizer: found NAL_SPS (sps_id=0) D/VLC: [a0a54228/5f5] h264 packetizer: found NAL_PPS (pps_id=0 sps_id=0) D/VLC: [a0a54228/5f5] core packetizer: using packetizer module "h264" D/VLC: [a0a53ea8/5f5] core decoder: looking for decoder module matching "mediacodec_ndk,all": 41 candidates W/VideoCapabilities: Unsupported profile 4 for video/avc W/VideoCapabilities: Unsupported profile 4 for video/avc W/VideoCapabilities: Unsupported profile 4 for video/avc
......
2017.5.7日更新:
最近用wireshark抓了下RTSP相關的數據,如下圖所示,可以清楚的看到,播放攝像頭畫面經歷了TCP連接建立的3次握手,RTSP會話建立過程和RTP傳輸數據過程,RTCP數據會每隔一段時間發送一次。
參考資料:
http://blog.csdn.net/leixiaohua1020/article/details/11955341