LIVE555再学习 -- testOnDemandRTSPServer 源码分析
一、簡介
先看一下官網上的介紹:
testOnDemandRTSPServer?creates a RTSP server that can stream, via RTP unicast, from various types of media file, on demand. (Supported media types include: MPEG-1 or 2 audio or video (elementary stream), including MP3 audio; MPEG-4 video (elementary stream); H.264 video (elementary stream); H.265 video (elementary stream); MPEG Program or Transport streams, including VOB files; DV video; AMR audio; WAV (PCM) audio.) The server can also stream from a?Matroska?or?WebM?file (by demultiplexing and streaming the tracks within the file). MPEG Transport Streams can also be streamed over raw UDP, if requested - e.g., by a set-top box.
- This server application also demonstrates how to deliver - via RTSP - a MPEG Transport Stream that arrived at the server as a UDP (raw-UDP or RTP/UDP) multicast or unicast stream. In particular, it is set up, by default, to accept input from the "testMPEG2TransportStreamer" demo application.
翻譯一下:
testOnDemandRTSPServer 創建一個 RTSP 服務器,可以根據需要通過 RTP 單播從各種類型的媒體文件流式傳輸。 (支持的媒體類型包括:MPEG-1或2音頻或視頻(基本流),包括MP3音頻; MPEG-4視頻(基本流); H.264視頻(基本流); H.265視頻(基本流) MPEG程序或傳輸流,包括VOB文件; DV視頻; AMR音頻; WAV(PCM)音頻。)服務器還可以從 Matroska 或 WebM 文件流(通過解復用和流式傳輸文件中的軌道)。 如果需要,MPEG傳輸流也可以通過原始UDP流傳輸,例如通過機頂盒。
該服務器應用程序還演示了如何通過 RTSP 傳送作為 UDP(原始UDP或RTP / UDP)組播或單播流到達服務器的MPEG傳輸流。 特別地,默認情況下,它設置為接受來自“testMPEG2TransportStreamer”演示應用程序的輸入。
二、源碼分析
參看:live555 testOnDemandRTSPServer例程解析
此例程是關于構建live555 RTSP服務器的,可以點播很多類型的文件,這里只講解H264的,本例程是點播,用的單播unicast的形式
#include "liveMedia.hh" #include "BasicUsageEnvironment.hh" //創建交互環境,用來打印相關信息的 UsageEnvironment* env; // To make the second and subsequent client for each stream reuse the same // input stream as the first client (rather than playing the file from the // start for each client), change the following "False" to "True": Boolean reuseFirstSource = False; // To stream *only* MPEG-1 or 2 video "I" frames // (e.g., to reduce network bandwidth), // change the following "False" to "True": Boolean iFramesOnly = False; //打印相關信息的函數 static void announceStream(RTSPServer* rtspServer, ServerMediaSession* sms, char const* streamName, char const* inputFileName); // fwd int main(int argc, char** argv) { // Begin by setting up our usage environment: // 1.創建任務調度器,createNew其實就是創建類的實例 TaskScheduler* scheduler = BasicTaskScheduler::createNew(); // 2. 創建交互環境 env = BasicUsageEnvironment::createNew(*scheduler); //以下為權限控制的代碼,設置后沒有權限的客戶端無法進行連接 UserAuthenticationDatabase* authDB = NULL; #ifdef ACCESS_CONTROL // To implement client access control to the RTSP server, do the following: authDB = new UserAuthenticationDatabase; authDB->addUserRecord("username1", "password1"); // replace these with real strings // Repeat the above with each <username>, <password> that you wish to allow // access to the server. #endif // 3. Create the RTSP server:此時就一直處于監聽??蛻舳说倪B接 RTSPServer* rtspServer = RTSPServer::createNew(*env, 8554, authDB); if (rtspServer == NULL) { *env << "Failed to create RTSP server: " << env->getResultMsg() << "\n"; exit(1); } char const* descriptionString = "Session streamed by \"testOnDemandRTSPServer\""; // Set up each of the possible streams that can be served by the // RTSP server. Each such stream is implemented using a // "ServerMediaSession" object, plus one or more // "ServerMediaSubsession" objects for each audio/video substream. // A H.264 video elementary stream: { char const* streamName = "H264unicast";//流名字,媒體名 char const* inputFileName = "test.264";//文件名,當客戶端輸入的流名字為h264ESVideoTest時,實際上打開的是test.264文件 // 4.創建媒體會話 //當客戶點播時,要輸入流名字streamName,告訴RTSP服務器點播的是哪個流。 //流名字和文件名的對應關系是通過增加子會話建立起來的(流名字streamName不是文件名inputFileName)。媒體會話對會話描述、會話持續時間、流名字等與會話有關的信息進行管理 //第二個參數:媒體名、三:媒體信息、四:媒體描述 ServerMediaSession* sms = ServerMediaSession::createNew(*env, streamName, streamName, descriptionString); //5.添加264子會話 這里的文件名才是真正打開文件的名字 //reuseFirstSource: //這里的H264VideoFileS...類派生自FileServerMediaSubsession派生自OnDemandServerMediaSubsession //而OnDemandServerMediaSubsession和PassiveMediaSubsession共同派生自ServerMediaSubsession //關于讀取文件之類都在這個類中實現的,如果要將點播改為直播就是要新建類繼承此類然后添加新的方法 sms->addSubsession(H264VideoFileServerMediaSubsession ::createNew(*env, inputFileName, reuseFirstSource)); //6.為rtspserver添加session rtspServer->addServerMediaSession(sms); //打印信息到標準輸出 announceStream(rtspServer, sms, streamName, inputFileName); } // Also, attempt to create a HTTP server for RTSP-over-HTTP tunneling. // Try first with the default HTTP port (80), and then with the alternative HTTP // port numbers (8000 and 8080). if (rtspServer->setUpTunnelingOverHTTP(80) || rtspServer->setUpTunnelingOverHTTP(8000) || rtspServer->setUpTunnelingOverHTTP(8080)) { *env << "\n(We use port " << rtspServer->httpServerPortNum() << " for optional RTSP-over-HTTP tunneling.)\n"; } else { *env << "\n(RTSP-over-HTTP tunneling is not available.)\n"; } //執行循環方法,來執行循環方法,對套接字的讀取事件和對媒體文件的延時發送操作都在這個循環中完成。 env->taskScheduler().doEventLoop(); // does not return return 0; // only to prevent compiler warning } static void announceStream(RTSPServer* rtspServer, ServerMediaSession* sms, char const* streamName, char const* inputFileName) { char* url = rtspServer->rtspURL(sms); UsageEnvironment& env = rtspServer->envir(); env << "\n\"" << streamName << "\" stream, from the file \"" << inputFileName << "\"\n"; env << "Play this stream using the URL \"" << url << "\"\n"; delete[] url; } 上面的源碼分析很清楚了,和官方源碼對比一下一目了然。
然后你可以跟?LIVE555再學習 -- testH264VideoStreamer 源碼分析?里的代碼做一下比較。
瑪德 豁然開朗,單播原來也就是這么回事。
想了解更多,參看:Live555學習之(二)------- testOnDemandRTSPServer
這部分看來有點必要后面再認真的講一下吧
其中的?doEventLoop 函數有點意思 詳解參看:LIVE555再學習 -- live555實現RTSP直播服務器 分析
三、測試
在 testProgs 目錄下放入 test.264,執行 ./testOnDemandRTSPServer?
可以看到有提示嘛,在 VLC 上輸入URL?rtsp://192.168.2.xx:8554/h264ESVideoTest
OK,有視頻顯示。此時,我還沒看單播和多播區別,也沒看testOnDemandRTSPServer的源碼,不過好奇怪為什么
上面測試,現在了解到?testOnDemandRTSPServer 是單播,傳輸現有的 test.264 有是點播。
總結
以上是生活随笔為你收集整理的LIVE555再学习 -- testOnDemandRTSPServer 源码分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第一部分Calendar介绍
- 下一篇: C语言一元二次方程