Home | History | Annotate | Download | only in include
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 #ifndef PVMF_RTCP_PROTO_IMPL_H_INCLUDED
     19 #define PVMF_RTCP_PROTO_IMPL_H_INCLUDED
     20 
     21 #ifndef OSCL_BASE_H_INCLUDED
     22 #include "oscl_base.h"
     23 #endif
     24 #ifndef OSCL_VECTOR_H_INCLUDED
     25 #include "oscl_vector.h"
     26 #endif
     27 #ifndef OSCL_STRING_CONTAINERS_H_INCLUDED
     28 #include "oscl_string_containers.h"
     29 #endif
     30 #ifndef PVMF_RETURN_CODES_H_INCLUDED
     31 #include "pvmf_return_codes.h"
     32 #endif
     33 #ifndef PVMF_MEDIA_MSG_H_INCLUDED
     34 #include "pvmf_media_msg.h"
     35 #endif
     36 
     37 #ifndef PVMF_SIMPLE_MEDIA_BUFFER_H_INCLUDED
     38 #include "pvmf_simple_media_buffer.h"
     39 #endif
     40 #ifndef PVMF_MEDIA_DATA_H_INCLUDED
     41 #include "pvmf_media_data.h"
     42 #endif
     43 #ifndef PVMF_SM_TUNABLES_H_INCLUDED
     44 #include "pvmf_sm_tunables.h"
     45 #endif
     46 
     47 #ifndef PVMF_JITTER_BUFFER_H_INCLUDED
     48 #include "pvmf_jitter_buffer.h"
     49 #endif
     50 
     51 #ifndef PVMF_RTCP_TIMER_H_INCLUDED
     52 #include "pvmf_rtcp_timer.h"
     53 #endif
     54 
     55 #ifndef PVMF_JB_EVENT_NOTIFIER_H
     56 #include "pvmf_jb_event_notifier.h"
     57 #endif
     58 
     59 #ifndef OSCL_RAND_H_INCLUDED
     60 #include "oscl_rand.h"
     61 #endif
     62 
     63 #ifndef RTCP_H
     64 #include "rtcp.h"
     65 #endif
     66 
     67 #ifndef PVLOGGER_H_INCLUDED
     68 #include "pvlogger.h"
     69 #endif
     70 
     71 #ifndef PVMF_RESIZABLE_SIMPLE_MEDIAMSG_H_INCLUDED
     72 #include "pvmf_resizable_simple_mediamsg.h"
     73 #endif
     74 
     75 #ifndef PVMF_PORT_INTERFACE_H_INCLUDED
     76 #include "pvmf_port_interface.h"
     77 #endif
     78 
     79 //May be can add a separate config file
     80 #define MAX_RTCP_SOURCES 31
     81 #define MAX_RTCP_BLOCK_SIZE 512
     82 #define DEFAULT_RTCP_MEM_POOL_BUFFERS 4
     83 #define RTCP_RAND_SEED 666
     84 
     85 #define MIN_RTSP_SERVER_INACTIVITY_TIMEOUT_IN_SEC   1
     86 #define MAX_RTSP_SERVER_INACTIVITY_TIMEOUT_IN_SEC   300
     87 
     88 // RTCP INTERVAL related
     89 #define DEFAULT_RTCP_INTERVAL_USEC 1000000
     90 #define DEFAULT_RTCP_INTERVAL_SEC  1
     91 #define RTCP_FIXED_INTERVAL_MODE   1
     92 #define PVMF_MEDIA_DATA_CLASS_SIZE 128
     93 
     94 ///////////////////////////////////////////////////////////////////////////////
     95 //PVMFRTCPStats
     96 ///////////////////////////////////////////////////////////////////////////////
     97 class PVMFRTCPStats
     98 {
     99     public:
    100         PVMFRTCPStats()
    101         {
    102             Reset();
    103         }
    104 
    105         PVMFRTCPStats(const PVMFRTCPStats& aSrc)
    106         {
    107             lastSenderReportNTP = aSrc.lastSenderReportNTP;
    108             lastSenderReportRTP = aSrc.lastSenderReportRTP;
    109             lastSenderReportTS = aSrc.lastSenderReportTS;
    110             lastSenderReportRecvTime = aSrc.lastSenderReportRecvTime;
    111             packetLossUptoThisRR = aSrc.packetLossUptoThisRR;
    112             maxSeqNumRecievedUptoThisRR = aSrc.maxSeqNumRecievedUptoThisRR;
    113             lastRRGenTime = aSrc.lastRRGenTime;
    114             iLastSenderReportSSRC = aSrc.iLastSenderReportSSRC;
    115             avg_rtcp_compound_pkt_size = aSrc.avg_rtcp_compound_pkt_size;
    116             oSRRecvd = aSrc.oSRRecvd;
    117             oRTCPByeRecvd = aSrc.oRTCPByeRecvd;
    118         };
    119 
    120         PVMFRTCPStats& operator=(const PVMFRTCPStats& a)
    121         {
    122             if (&a != this)
    123             {
    124                 lastSenderReportNTP = a.lastSenderReportNTP;
    125                 lastSenderReportRTP = a.lastSenderReportRTP;
    126                 lastSenderReportTS = a.lastSenderReportTS;
    127                 lastSenderReportRecvTime = a.lastSenderReportRecvTime;
    128                 packetLossUptoThisRR = a.packetLossUptoThisRR;
    129                 maxSeqNumRecievedUptoThisRR = a.maxSeqNumRecievedUptoThisRR;
    130                 lastRRGenTime = a.lastRRGenTime;
    131                 iLastSenderReportSSRC = a.iLastSenderReportSSRC;
    132                 avg_rtcp_compound_pkt_size = a.avg_rtcp_compound_pkt_size;
    133                 oSRRecvd = a.oSRRecvd;
    134                 oRTCPByeRecvd = a.oRTCPByeRecvd;
    135             }
    136             return *this;
    137         }
    138 
    139         void Reset()
    140         {
    141             lastSenderReportNTP = 0;
    142             lastSenderReportRTP = 0;
    143             lastSenderReportTS = 0;
    144             lastSenderReportRecvTime = 0;
    145             packetLossUptoThisRR = 0;
    146             maxSeqNumRecievedUptoThisRR = 0;
    147             lastRRGenTime = 0;
    148             iLastSenderReportSSRC = 0;
    149             avg_rtcp_compound_pkt_size = 0;
    150             oSRRecvd = false;
    151             oRTCPByeRecvd = false;
    152         }
    153 
    154 
    155 
    156         uint64  lastSenderReportNTP;
    157         uint32  lastSenderReportRTP;
    158         uint32  lastSenderReportTS;
    159         uint64  lastSenderReportRecvTime;
    160         int32   packetLossUptoThisRR;
    161         int32   maxSeqNumRecievedUptoThisRR;
    162         uint64  lastRRGenTime;
    163         uint32  iLastSenderReportSSRC;
    164         float   avg_rtcp_compound_pkt_size;
    165         bool    oSRRecvd;
    166         bool    oRTCPByeRecvd;
    167 };
    168 
    169 class RTPRateAdaptationInfo
    170 {
    171     public:
    172         RTPRateAdaptationInfo(): iRateAdaptation(false)
    173                 , iRateAdaptationFeedBackFrequency(0)
    174                 , iRateAdaptationRTCPRRCount(0)
    175                 , iRateAdaptationFreeBufferSpaceInBytes(0)
    176         {
    177         }
    178         bool    iRateAdaptation;
    179         uint32  iRateAdaptationFeedBackFrequency;
    180         uint32  iRateAdaptationRTCPRRCount;
    181         uint32  iRateAdaptationFreeBufferSpaceInBytes;
    182 };
    183 
    184 class PVRTCPChannelController;
    185 ///////////////////////////////////////////////////////////////////////////////
    186 //PVRTCPChannelControllerObserver
    187 ///////////////////////////////////////////////////////////////////////////////
    188 class PVRTCPChannelControllerObserver
    189 {
    190     public:
    191         virtual PVMFStatus RTCPSRReveived(PVRTCPChannelController* channelController) = 0;
    192         virtual PVMFStatus RTCPByeReceived(PVRTCPChannelController* channelController) = 0;
    193         virtual PVMFStatus RTCPRRReadyToSend(PVMFPortInterface*& aPort, PVMFSharedMediaMsgPtr& aMessage) = 0;
    194 };
    195 
    196 ///////////////////////////////////////////////////////////////////////////////
    197 //PVRTCPChannelController
    198 ///////////////////////////////////////////////////////////////////////////////
    199 class PVRTCPChannelController: public PvmfRtcpTimerObserver
    200 {
    201     public:
    202         static PVRTCPChannelController* New(PVRTCPChannelControllerObserver* aObserver, PVMFJitterBuffer& aRTPJitterBuffer, PVMFPortInterface* aFeedbackPort, PVMFMediaClock& aClientPlaybackClock, PVMFMediaClock& aRTCPClock);
    203         ~PVRTCPChannelController();
    204 
    205         void Reset();
    206 
    207         PVMFJitterBuffer& GetJitterBuffer()
    208         {
    209             return irRTPDataJitterBuffer;
    210         }
    211 
    212         void StartRTCPMessageExchange();
    213         void PauseRTCPMessageExchange();
    214         void StopRTCPMessageExchange();
    215 
    216         void SetRateAdaptation(bool aRateAdaptation, uint32 aRateAdaptationFeedBackFrequency, uint32 aRateAdaptationFreeBufferSpaceInBytes);
    217         void SetRTCPIntervalInMicroSecs(uint32 aRTCPInterval);
    218         bool SetPortRTCPParams(int aNumSenders, uint32 aRR, uint32 aRS)
    219         {
    220             iNumSenders = aNumSenders;
    221             iRR = aRR;
    222             iRS = aRS;
    223             iRtcpBwConfigured = true;
    224             return true;
    225         }
    226         PVMFStatus ProcessRTCPReport(PVMFSharedMediaMsgPtr& aMsg);
    227         bool CheckAssociatedFeedbackPort(const PVMFPortInterface* aFeedbackPort)const
    228         {
    229             return (ipFeedbackPort == aFeedbackPort);
    230         }
    231 
    232         void SetMediaClockConverter(MediaClockConverter* aClockConverter)
    233         {
    234             ipMediaClockConverter = aClockConverter;
    235         }
    236 
    237         void Prepare(bool aAfterSeek)
    238         {
    239             if (aAfterSeek)
    240             {
    241                 iRTCPStats.oRTCPByeRecvd = false;
    242             }
    243             if (iRtcpBwConfigured && (iRR == 0))
    244             {
    245                 ipRTCPTimer->setRTCPInterval(0);
    246                 return;
    247             }
    248         }
    249 
    250         const PVMFRTCPStats& GetRTCPStats()
    251         {
    252             return iRTCPStats;
    253         }
    254 
    255         virtual void RtcpTimerEvent();
    256     private:
    257         void Construct();
    258         PVRTCPChannelController(PVRTCPChannelControllerObserver* aObserver, PVMFJitterBuffer& aRTPJitterBuffer, PVMFPortInterface* aFeedbackPort, PVMFMediaClock& aClientPlaybackClock, PVMFMediaClock& aRTCPClock);
    259         PVRTCPChannelController& operator =(const PVRTCPChannelController&);
    260         PVRTCPChannelController(const PVRTCPChannelController&);
    261         void ResetParams(bool aMemoryCleanUp = false);
    262         PVMFStatus ComposeFeedBackPacket();
    263         uint32 CalcRtcpInterval();
    264         PVMFStatus GenerateRTCPRR();
    265         PVMFResizableSimpleMediaMsgAlloc*   CreateRTCPRRBufAllocReSize();
    266 
    267         PVRTCPChannelControllerObserver*    ipObserver;
    268         PVMFJitterBuffer&                   irRTPDataJitterBuffer;
    269         PVMFPortInterface*                  ipFeedbackPort;
    270         PVMFMediaClock&                     irClientPlayBackClock;
    271         PVMFMediaClock&                     irRTCPClock;
    272 
    273         RTPRateAdaptationInfo               iRTPChannelRateAdaptationInfo;
    274         PVMFRTCPStats                       iRTCPStats;
    275         int                                 iNumSenders;
    276         uint32                              iRR;
    277         uint32                              iRS;
    278         bool                                iRtcpBwConfigured;
    279         bool                                iInitialRTCPIntervalComputation;
    280         uint32                              iRTCPIntervalInMicroSeconds;
    281         bool                                iInitialRTCPPacket;
    282         PvmfRtcpTimer*                      ipRTCPTimer;
    283         OsclRand                            iRandGen;
    284         MediaClockConverter*                ipMediaClockConverter;
    285 
    286         PVLogger*                           ipRTCPDataPathLoggerIn;
    287         PVLogger*                           ipRTCPDataPathLoggerOut;
    288 };
    289 
    290 ///////////////////////////////////////////////////////////////////////////////
    291 //PVRTCPProtoImplementorObserver
    292 ///////////////////////////////////////////////////////////////////////////////
    293 class PVRTCPProtoImplementorObserver
    294 {
    295     public:
    296         virtual PVMFStatus RTCPPacketReceived(RTCPPacketType aPacketType, PVRTCPChannelController* aController) = 0;
    297         virtual PVMFStatus RTCPReportReadyToSend(PVMFPortInterface*& aPort, PVMFSharedMediaMsgPtr& aMessage) = 0;
    298         virtual PVMFStatus ProcessInfoEvent(PVMFAsyncEvent& aEvent) = 0;
    299 };
    300 
    301 ///////////////////////////////////////////////////////////////////////////////
    302 //PVRTCPProtoImplementor
    303 ///////////////////////////////////////////////////////////////////////////////
    304 class PVRTCPProtoImplementor: public PVRTCPChannelControllerObserver
    305 {
    306     public:
    307         OSCL_IMPORT_REF static PVRTCPProtoImplementor* New(PVMFMediaClock& aClientPlayBackClock, PVMFMediaClock& aRTCPClock, PVRTCPProtoImplementorObserver* aObs, bool aBroadcastSession);
    308         OSCL_IMPORT_REF ~PVRTCPProtoImplementor();
    309 
    310         OSCL_EXPORT_REF void AddPVRTCPChannelController(PVRTCPChannelController* aPVRTCPChannelController);
    311         OSCL_EXPORT_REF void RemoveAllRTCPChannelControllers();
    312         OSCL_EXPORT_REF PVRTCPChannelController* GetRTCPChannelController(const PVMFPortInterface* aPort) const;
    313 
    314         OSCL_IMPORT_REF void StartRTCPMessageExchange();
    315         OSCL_IMPORT_REF void PauseRTCPMessageExchange();
    316         OSCL_IMPORT_REF void StopRTCPMessageExchange();
    317 
    318         OSCL_IMPORT_REF void Reset();
    319 
    320         OSCL_IMPORT_REF PVMFStatus ProcessRTCPReport(PVMFPortInterface* aFeedbackPort, PVMFSharedMediaMsgPtr& aMsg);
    321         OSCL_IMPORT_REF void SetRTCPIntervalInMicroSecs(uint32 aRTCPInterval);
    322         OSCL_IMPORT_REF bool SetPortRTCPParams(PVMFPortInterface* aPort, int aNumSenders, uint32 aRR, uint32 aRS);
    323 
    324         OSCL_IMPORT_REF void Prepare(bool aPlayAfterSeek);
    325 
    326 
    327         bool RTCPByeReceivedOnAllControllers()
    328         {
    329             PVRTCPChannelController* rtcpChannelController = NULL;
    330             Oscl_Vector<PVRTCPChannelController*, OsclMemAllocator>::iterator iter;
    331             for (iter = iPVRTCPChannelController.begin(); iter !=  iPVRTCPChannelController.end(); iter++)
    332             {
    333                 rtcpChannelController = *iter;
    334                 if (!(rtcpChannelController->GetRTCPStats().oRTCPByeRecvd))
    335                 {
    336 
    337                     return false;
    338                 }
    339             }
    340             return true;
    341         }
    342 
    343         //Implementation of PVRTCPChannelControllerObserver
    344         PVMFStatus RTCPSRReveived(PVRTCPChannelController* aChannelController);
    345         PVMFStatus RTCPByeReceived(PVRTCPChannelController* aChannelController);
    346         PVMFStatus RTCPRRReadyToSend(PVMFPortInterface*& aPort, PVMFSharedMediaMsgPtr& aMessage);
    347     private:
    348         PVRTCPProtoImplementor(PVMFMediaClock& aClientPlayBackClock, PVMFMediaClock& aRTCPClock, PVRTCPProtoImplementorObserver* aObs, bool aBroadcastSession);
    349         void ResetParams(bool aMemoryCleanUp = false);
    350         bool ProcessRTCPSRforAVSync();
    351         void Construct();
    352 
    353         const bool                      iBroadcastSession;
    354         PVMFMediaClock&                 irClientPlayBackClock;
    355         PVMFMediaClock&                 irRTCPClock;
    356         PVRTCPProtoImplementorObserver* ipObserver;
    357 
    358         bool                            iPerformRTCPBasedAVSync;
    359         bool                            iRTCPAVSyncProcessed;
    360         bool                            iPlayStopTimeAvailable;
    361         Oscl_Vector<PVRTCPChannelController*, OsclMemAllocator> iPVRTCPChannelController;
    362 
    363         PVLogger*   ipRTCPDataPathLoggerIn;
    364         PVLogger*   ipRTCPAVSyncLogger;
    365 };
    366 
    367 #endif
    368