Live555的核心在Live Media,Live Media的核心在Source和Sink。服務(wù)器大致的服務(wù)流程是調(diào)用Sink的continuePlaying()來向客戶發(fā)送流媒體數(shù)據(jù),而 continuePlaying()內(nèi)調(diào)用了Source類的getNextFrame()函數(shù),來獲取幀數(shù)據(jù)。簡單來說,Source類主要用于獲取要發(fā)送的數(shù)據(jù),而Sink類主要用于發(fā)送數(shù)據(jù)。 一 幾個(gè)重要的類 1.RTPInterface類 該類的主要作用是從一個(gè)Socket讀取數(shù)據(jù)到一個(gè)buffer中,是數(shù)據(jù)的源頭所在。 注意到它最重要的一個(gè)函數(shù)是:Boolean handleRead (unsigned char *buffer, unsigned bufferMaxSize, unsigned &bytesRead , struct sockaddr_in &fromAddress, Boolean &packetReadWasIncomplete),它將fromAddress中的數(shù)據(jù)讀入到buffer中。 2.BufferedPacket類 該類用于存儲(chǔ)媒體數(shù)據(jù)的RTP包內(nèi)容,它的子類具體到媒體類型,如H264BufferedPacket類。值得注意的有兩點(diǎn): 1)構(gòu)造函數(shù)BufferedPacket()中申請了MAX_PACKET_SIZE(10000)大小的unsigned char數(shù)組。 2)一個(gè)重要函數(shù)是 Boolean fillInData (RTPInterface &rtpInterface, Boolean &packetReadWasIncomplete) { rtpInterface.handleRead(&fBuf); } 實(shí)現(xiàn)了將rtpInterface指向的數(shù)據(jù)存入fBuf中的功能。 4.ReorderingPacketBuffer類 該類用于存放多個(gè)BufferedPacket對象(可能是對象指針鏈表,有待考察),作為Source類中組織多個(gè)BufferedPacket對象的場所。 二 Source類的繼承關(guān)系 Medium<-MediaSource<-FramedSource<-RTPSource<-MultiFramedRTPSource<-H264<-VideoRTPSource 其中,我們要重點(diǎn)關(guān)注的類是下面幾個(gè): FramedSource,RTPSource,MultiFramedRTPSource。 1.FramedSource類 class FramedSource { void getNextFrame (unsigned char *to,......) { ...... doGetNextFrame(); } virtual void doGetNextFrame ()=0; } 2.RTPSource類 該類并未實(shí)現(xiàn)doGetNextFrame()函數(shù),因而getNextFrame()與doGetNextFrame()與FramedSource類中定義完全相同。需要注意該類相比父類FramedSource,其protected成員多了一個(gè)RTPInterface fRTPInterface。 3.MultiFramedRTPSource類 非常重要的類,實(shí)現(xiàn)了doGetNextFrame()函數(shù)。 class MultiFramedRTPSource { public: void getNextFrame (unsigned char *to,......) { ...... doGetNextFrame(); } void doGetNextFrame () { ...... fRTPInterface.startNetworkReading(); doGetNextFrame1() { BufferedPacket* nextPacket = fReorderingBuffer->getNextCompletedPacket(); };
} void networkReadHandler1 () { ...... do { bPacket->fillInData(fRTPInterface); fReorderingBuffer->storePacket(bPacket); }while(0); } protected: RTPInterface fRTPInterface; private: ReorderingPacketBuffer *fReorderingBuffer; } 三 RTP包的接收以及發(fā)送流程 了解了以上幾個(gè)類以及重要的函數(shù),大概的RTP包處理流程推斷如下:某個(gè)Sink類調(diào)用continuePlaying()函數(shù),而continuePlaying()函數(shù)中調(diào)用Source類(以MultiFramedRTPSource為例,因?yàn)樗詫?shí)現(xiàn)doGetFrame()函數(shù))的getNextFrame()函數(shù)以得到發(fā)送數(shù)據(jù),而getNextFrame()是通過調(diào)用doGetNextFrame(),繼而是doGetNextFrame1(),最終在doNextFrame1中由語句fReorderingBuffer->getNextCompletedPacket()將存放在fReorderingBuffer中的數(shù)據(jù)取出交給Sink類來發(fā)送。 那么,fReorderingBuffer中的數(shù)據(jù)是從何而來呢?推測是通過MultiFramedRTPSource類的networkReadHandler1()函數(shù)不斷從fRTPInterface所指向的socket讀入, 最終執(zhí)行語句fReorderingBuffer->storePacket(bPacket);實(shí)現(xiàn)。 |
|