Home | History | Annotate | Download | only in src
      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_JITTER_BUFFER_H_INCLUDED
     19 #include "pvmf_jitter_buffer.h"
     20 #endif
     21 
     22 #ifndef PVMF_MEDIA_CMD_H_INCLUDED
     23 #include "pvmf_media_cmd.h"
     24 #endif
     25 
     26 // Define entry point for this DLL
     27 OSCL_DLL_ENTRY_POINT_DEFAULT()
     28 
     29 #define PVMF_JITTER_BUFFER_ROLL_OVER_THRESHOLD_16BIT 2000
     30 void PVMFJitterBufferImpl::ResetParams(bool aReleaseMemory)
     31 {
     32     if (aReleaseMemory)
     33     {
     34         DestroyAllocators();
     35         OSCL_DELETE(iJitterBuffer);
     36         if (iBufferAlloc)
     37             iBufferAlloc->removeRef();
     38     }
     39 
     40     iStreamingState = STREAMINGSTATE_UNKNOWN;
     41     iSeqNum = 0;
     42     iReportCanRetrievePacket = false;
     43 
     44     iInPlaceProcessing = true;
     45     iOnePacketPerFragment = true;
     46     iOnePacketPerMediaMsg = true;
     47 
     48     iLastPacketOutTs = 0;
     49 
     50     iMediaDataGroupAlloc = NULL;
     51     iMediaDataImplMemPool = NULL;
     52     iMediaMsgMemPool = NULL;
     53 
     54     iStartTimeInMS = 0;
     55     iStopTimeInMS = 0;
     56     iPlayStopTimeAvailable = 0;
     57     iBroadCastSession = false;
     58 
     59     iMaxAdjustedRTPTS = 0;
     60 
     61     iSessionDurationExpired = false;
     62     iDurationInMilliSeconds = 0;
     63     iRebufferingThresholdInMilliSeconds = 0;
     64 
     65     iMonotonicTimeStamp = 0;
     66     iFirstSeqNum = 0;
     67     iJitterBuffer = NULL;
     68     iEOSSignalled = false;
     69     iEOSSent = false;
     70     iStreamID = 0;
     71     iMaxAdjustedTS = 0;
     72     iPrevAdjustedTS = 0;
     73     iHeaderPreParsed = false;
     74 
     75     iRTPTimeScale = 0;
     76     iPrevTSOut = 0;
     77 
     78     seqNumLock = false;
     79 
     80     iInterArrivalJitter = 0;
     81     oFirstPacket = false;
     82 
     83 
     84     SSRCLock = 0;
     85     oSSRCFromSetUpResponseAvailable = false;
     86     SSRCFromSetUpResponse = 0;
     87 
     88     iPrevSeqNumBaseOut = 0;
     89     seqLockTimeStamp = 0;
     90 
     91     iPrevAdjustedRTPTS = 0;
     92     iPrevTSIn = 0;
     93     iPrevSeqNumBaseIn = 0;
     94 
     95     iBufferAlloc = NULL;
     96     prevMinPercentOccupancy = 100;
     97     consecutiveLowBufferCount = 0;
     98     iNumUnderFlow = 0;
     99 
    100     iWaitForOOOPacketCallBkId = 0;
    101     iMonitorReBufferingCallBkId = 0;
    102     iJitterBufferDurationCallBkId = 0;
    103 
    104     iMonitorReBufferingCallBkPending = false;
    105     iWaitForOOOPacketCallBkPending = false;
    106     iJitterBufferDurationCallBkPending = false;
    107 
    108     ipMediaClockConverter = NULL;
    109     iTimeScale  = 0;
    110 }
    111 
    112 void PVMFJitterBufferImpl::DestroyAllocators()
    113 {
    114     /* Destroy allocators if present */
    115     if (iMediaDataGroupAlloc != NULL)
    116     {
    117         iMediaDataGroupAlloc->CancelFreeChunkAvailableCallback();
    118         iMediaDataGroupAlloc->removeRef();
    119         iMediaDataGroupAlloc = NULL;
    120     }
    121     if (iMediaDataImplMemPool != NULL)
    122     {
    123         iMediaDataImplMemPool->removeRef();
    124         iMediaDataImplMemPool = NULL;
    125     }
    126     if (iMediaMsgMemPool != NULL)
    127     {
    128         iMediaMsgMemPool->removeRef();
    129         iMediaMsgMemPool = NULL;
    130     }
    131 }
    132 
    133 OSCL_EXPORT_REF void PVMFJitterBufferImpl::StreamingSessionStarted()
    134 {
    135     iStreamingState = STREAMINGSTATE_STARTED;
    136     iNumUnderFlow = 0;
    137 }
    138 
    139 OSCL_EXPORT_REF void PVMFJitterBufferImpl::StreamingSessionPaused()
    140 {
    141     iStreamingState =   STREAMINGSTATE_PAUSED;
    142     CancelEventCallBack(JB_NOTIFY_WAIT_FOR_OOO_PACKET_COMPLETE);
    143     CancelEventCallBack(JB_MONITOR_REBUFFERING);
    144     CancelEventCallBack(JB_BUFFERING_DURATION_COMPLETE);
    145 }
    146 
    147 OSCL_EXPORT_REF void PVMFJitterBufferImpl::StreamingSessionStopped()
    148 {
    149     iStreamingState =   STREAMINGSTATE_STOPPED;
    150     CancelEventCallBack(JB_NOTIFY_WAIT_FOR_OOO_PACKET_COMPLETE);
    151     CancelEventCallBack(JB_MONITOR_REBUFFERING);
    152     CancelEventCallBack(JB_BUFFERING_DURATION_COMPLETE);
    153     FlushJitterBuffer();
    154 }
    155 
    156 OSCL_EXPORT_REF void PVMFJitterBufferImpl::SetRebufferingThresholdInMilliSeconds(uint32 aRebufferingThresholdInMilliSeconds)
    157 {
    158     iRebufferingThresholdInMilliSeconds = aRebufferingThresholdInMilliSeconds;
    159 }
    160 
    161 OSCL_EXPORT_REF void PVMFJitterBufferImpl::SetDurationInMilliSeconds(uint32 aDuration)
    162 {
    163     iDurationInMilliSeconds = aDuration;
    164 }
    165 
    166 OSCL_EXPORT_REF void PVMFJitterBufferImpl::PrepareForRepositioning()
    167 {
    168     irDataState = PVMF_JITTER_BUFFER_IN_TRANSITION;
    169     iEOSSignalled = false;
    170     iEOSSent = false;
    171     irDelayEstablished = false;
    172 }
    173 
    174 OSCL_EXPORT_REF PVMFJitterBufferDataState PVMFJitterBufferImpl::GetState() const
    175 {
    176     return irDataState;
    177 }
    178 
    179 OSCL_EXPORT_REF void PVMFJitterBufferImpl::SetJitterBufferState(PVMFJitterBufferDataState aState)
    180 {
    181     irDataState = aState;
    182 }
    183 
    184 OSCL_EXPORT_REF void PVMFJitterBufferImpl::SetJitterBufferChunkAllocator(OsclMemPoolResizableAllocator* aDataBufferAllocator)
    185 {
    186     iBufferAlloc = aDataBufferAllocator;
    187     iBufferAlloc->addRef();
    188 }
    189 
    190 OSCL_EXPORT_REF const char* PVMFJitterBufferImpl::GetMimeType() const
    191 {
    192     return irMimeType.get_cstr();
    193 }
    194 
    195 
    196 
    197 ///////////////////////////////////////////////////////////////////////////////
    198 // Creation and cleanup functions
    199 ///////////////////////////////////////////////////////////////////////////////
    200 
    201 OSCL_EXPORT_REF PVMFJitterBufferImpl::PVMFJitterBufferImpl(const PVMFJitterBufferConstructParams& aJBConstructParams)
    202         : irEstimatedServerClock(aJBConstructParams.GetEstimatedServerClock())
    203         , irClientPlayBackClock(aJBConstructParams.GetClientPlaybackClock())
    204         , irJBEventNotifier(aJBConstructParams.GetEventNotifier())
    205         , irMimeType(aJBConstructParams.GetMimeType())
    206         , irDelayEstablished(aJBConstructParams.GetDelayEstablishStatus())
    207         , irJitterDelayPercent(aJBConstructParams.GetJBDelayPercent())
    208         , irDataState(aJBConstructParams.GetJitterBufferState())
    209         , iObserver(aJBConstructParams.GetJBObserver())
    210         , iObserverContext(aJBConstructParams.GetContextData())
    211 {
    212     iMediaMsgMemPool = NULL;
    213     iMediaDataImplMemPool = NULL;
    214     iMediaDataGroupAlloc = NULL;
    215 
    216     ipLogger = NULL;
    217     ipClockLoggerSessionDuration = NULL;
    218     ipDataPathLogger = NULL;
    219     ipDataPathLoggerIn = NULL;
    220     ipDataPathLoggerOut = NULL;
    221     ipClockLogger = NULL;
    222     ipRTCPDataPathLoggerIn = NULL;
    223     ipRTCPDataPathLoggerOut = NULL;
    224     ipClockLoggerRebuff = NULL;
    225     ipDataPathLoggerFlowCtrl = NULL;
    226     ipJBEventsClockLogger = NULL;
    227 
    228     ResetParams(false);
    229 }
    230 
    231 
    232 
    233 OSCL_EXPORT_REF PVMFJitterBufferImpl::~PVMFJitterBufferImpl()
    234 {
    235     ResetJitterBuffer();
    236     ResetParams();
    237     DestroyAllocators();
    238 }
    239 
    240 OSCL_EXPORT_REF void PVMFJitterBufferImpl::Construct()
    241 {
    242     ipLogger = PVLogger::GetLoggerObject("JitterBuffer");
    243     ipClockLoggerSessionDuration = PVLogger::GetLoggerObject("clock.streaming_manager.sessionduration");
    244     ipDataPathLogger = PVLogger::GetLoggerObject("datapath.sourcenode.jitterbuffer");
    245     ipDataPathLoggerIn = PVLogger::GetLoggerObject("datapath.sourcenode.jitterbuffer.in");
    246     ipDataPathLoggerOut = PVLogger::GetLoggerObject("datapath.sourcenode.jitterbuffer.out");
    247     ipClockLogger = PVLogger::GetLoggerObject("clock.jitterbuffer");
    248     ipRTCPDataPathLoggerIn = PVLogger::GetLoggerObject("datapath.sourcenode.jitterbuffer.rtcp.in");
    249     ipRTCPDataPathLoggerOut = PVLogger::GetLoggerObject("datapath.sourcenode.jitterbuffer.rtcp.out");
    250     ipMaxRTPTsLogger = PVLogger::GetLoggerObject("MaxRTPTs");
    251     ipClockLoggerRebuff = PVLogger::GetLoggerObject("clock.jitterbuffernode.rebuffer");
    252     ipDataPathLoggerFlowCtrl = PVLogger::GetLoggerObject("datapath.sourcenode.jitterbuffer.flowctrl");
    253     ipJBEventsClockLogger = PVLogger::GetLoggerObject("eventnotifier.PVMFJitterBufferImpl");
    254 
    255     if (!iInPlaceProcessing)
    256     {
    257         CreateAllocators();
    258     }
    259 
    260     uint32 numNodes = 0;
    261 
    262 #if (PMVF_JITTER_BUFFER_NODE_USE_NO_RESIZE_ALLOC)
    263     numNodes = PVMF_JB_NO_RESIZE_ALLOC_NUM_CHUNKS_RTP;
    264 #else
    265     numNodes = DEFAULT_NUM_MEDIA_MSGS_IN_JITTER_BUFFER;
    266 #endif
    267     iJitterBuffer = OSCL_NEW(PVMFDynamicCircularArrayType,
    268                              (numNodes));
    269 
    270 
    271 }
    272 
    273 ///////////////////////////////////////////////////////////////////////////////
    274 // Interface functions
    275 ///////////////////////////////////////////////////////////////////////////////
    276 OSCL_EXPORT_REF PVMFJitterBufferRegisterMediaMsgStatus PVMFJitterBufferImpl::RegisterMediaMsg(PVMFSharedMediaMsgPtr& aMsg)
    277 {
    278     PVMFJitterBufferRegisterMediaMsgStatus retval = PVMF_JB_REGISTER_MEDIA_MSG_SUCCESS;
    279     PVMFSharedMediaDataPtr dataPacket;
    280 
    281     if (aMsg->getFormatID() > PVMF_MEDIA_CMD_FORMAT_IDS_START)  //IsCmdMsg
    282     {
    283         RegisterCmdPacket(aMsg);
    284     }
    285     else
    286     {
    287         LOGCLIENTANDESTIMATEDSERVCLK_DATAPATH;
    288         //1. Parse the header, and validate the packet
    289         //2. Create the packet (if iInPlaceProcessing is false)
    290         //3. Update statistics
    291         //4. Register the packet
    292         //5. Check for flow control [JB specific]
    293         PVMFSharedMediaDataPtr inputDataPacket;
    294         convertToPVMFMediaData(inputDataPacket, aMsg);
    295         OsclSharedPtr<PVMFMediaDataImpl> mediaDataIn;
    296         if (!inputDataPacket->getMediaDataImpl(mediaDataIn))
    297         {
    298             PVMF_JB_LOGERROR((0, "PVMFJitterBufferImpl::ProcessIncomingMsgRTP: corrupt input media msg"));
    299             PVMF_JB_LOGDATATRAFFIC_IN((0, "PVMFJitterBufferImpl::ProcessIncomingMsgRTP: corrupt input media msg"));
    300             retval = PVMF_JB_REGISTER_MEDIA_MSG_ERR_CORRUPT_PACKET;
    301         }
    302         else
    303         {
    304             if (iOnePacketPerMediaMsg)
    305             {
    306                 PVMFSharedMediaDataPtr dataPacket;
    307                 PVMFJBPacketParsingAndStatUpdationStatus status = ParsePacketHeaderAndUpdateJBStats(inputDataPacket, dataPacket);
    308                 if (PVMF_JB_PACKET_PARSING_SUCCESS == status)
    309                 {
    310                     if (CanRegisterMediaMsg())
    311                     {
    312                         if (CheckSpaceAvailability())
    313                         {
    314                             retval = RegisterDataPacket(inputDataPacket);
    315 
    316                         }
    317                         else
    318                         {
    319                             retval = PVMF_JB_REGISTER_MEDIA_MSG_FAILURE_JB_FULL;
    320                         }
    321                     }
    322                     else
    323                     {
    324                         if (dataPacket.GetRep())
    325                         {
    326 #if (PVLOGGER_INST_LEVEL > PVLOGMSG_INST_LLDBG)
    327                             PVMF_JB_LOGDATATRAFFIC_FLOWCTRL_E((0, "PVMFJitterBufferNode::ProcessIncomingMsgRTP: - Ignoring - Wrong State"
    328                                                                "Size=%d, SSRC=%d", inputDataPacket->getFilledSize(), inputDataPacket->getStreamID()));
    329                             PVMF_JB_LOGDATATRAFFIC_IN((0, "PVMFJitterBufferNode::ProcessIncomingMsg: - Ignoring - Wrong State"
    330                                                        "Size=%d, SSRC=%d", inputDataPacket->getFilledSize(), inputDataPacket->getStreamID()));
    331                             PVMF_JB_LOGERROR((0, "PVMFJitterBufferNode::ProcessIncomingMsg: - Ignoring - Wrong State"
    332                                               "Size=%d, SSRC=%d", inputDataPacket->getFilledSize(), inputDataPacket->getStreamID()));
    333 #endif
    334                         }
    335                         PVMF_JB_LOGERROR((0, "PVMFJitterBufferImpl::RegisterMediaMsg: Invalid State to accept any packet"));
    336                         PVMF_JB_LOGDATATRAFFIC_IN((0, "PVMFJitterBufferImpl::RegisterMediaMsg: Invalid State to accept any packet"));
    337                         retval = PVMF_JB_REGISTER_MEDIA_MSG_ERR_CORRUPT_PACKET;
    338                     }
    339                 }
    340                 else
    341                 {
    342                     if (PVMF_JB_ERR_INSUFFICIENT_MEM_TO_PACKETIZE == status)
    343                     {
    344                         retval = PVMF_JB_REGISTER_MEDIA_MSG_FAILURE_INSUFFICIENT_MEMORY_FOR_PACKETIZATION;
    345                     }
    346                     if (PVMF_JB_ERR_INVALID_CONFIGURATION == status)
    347                     {
    348                         retval = PVMF_JB_BUFFER_REGISTER_MEDIA_MSG_ERR_CONFIG_NOT_SUPPORTED;
    349                     }
    350                     if ((PVMF_JB_ERR_CORRUPT_HDR == status) || (PVMF_JB_ERR_TRUNCATED_HDR == status))
    351                     {
    352                         retval = PVMF_JB_REGISTER_MEDIA_MSG_ERR_INVALID_HEADER;
    353                     }
    354                     if (PVMF_JB_ERR_UNEXPECTED_PKT == status)
    355                     {
    356                         retval = PVMF_JB_REGISTER_MEDIA_MSG_ERR_UNEXPECTED_DATA;
    357                     }
    358                     if (PVMF_JB_ERR_LATE_PACKET == status)
    359                     {
    360                         retval = PVMF_JB_REGISTER_MEDIA_MSG_ERR_LATE_MSG;
    361                     }
    362                     PVMF_JB_LOGERROR((0, "PVMFJitterBufferImpl::RegisterMediaMsg: ParsePacketHeader failed retval %d", retval));
    363                 }
    364             }
    365             else
    366             {
    367                 if (iOnePacketPerFragment)
    368                 {
    369                     const uint32 numOfFragments = inputDataPacket->getNumFragments();
    370                     for (uint ii = 0; ii < numOfFragments ; ii++)
    371                     {
    372                         PVMFSharedMediaDataPtr dataPacket;
    373                         PVMFJBPacketParsingAndStatUpdationStatus status = ParsePacketHeaderAndUpdateJBStats(inputDataPacket, dataPacket, ii);
    374                         if (PVMF_JB_PACKET_PARSING_SUCCESS == status)
    375                         {
    376                             if (CanRegisterMediaMsg())
    377                             {
    378                                 if (CheckSpaceAvailability())
    379                                 {
    380                                     retval = RegisterDataPacket(dataPacket);
    381                                 }
    382                                 else
    383                                 {
    384                                     retval = PVMF_JB_REGISTER_MEDIA_MSG_FAILURE_JB_FULL;
    385                                 }
    386                             }
    387                             else
    388                             {
    389                                 if (dataPacket.GetRep())
    390                                 {
    391 #if (PVLOGGER_INST_LEVEL > PVLOGMSG_INST_LLDBG)
    392                                     PVMF_JB_LOGDATATRAFFIC_FLOWCTRL_E((0, "PVMFJitterBufferNode::ProcessIncomingMsgRTP: - Ignoring - Wrong State"
    393                                                                        "Size=%d, SSRC=%d", inputDataPacket->getFilledSize(), dataPacket->getStreamID()));
    394                                     PVMF_JB_LOGDATATRAFFIC_IN((0, "PVMFJitterBufferNode::ProcessIncomingMsg: - Ignoring - Wrong State"
    395                                                                "Size=%d, SSRC=%d", inputDataPacket->getFilledSize(), dataPacket->getStreamID()));
    396                                     PVMF_JB_LOGERROR((0, "PVMFJitterBufferNode::ProcessIncomingMsg: - Ignoring - Wrong State"
    397                                                       "Size=%d, SSRC=%d", inputDataPacket->getFilledSize(), dataPacket->getStreamID()));
    398 #endif
    399                                 }
    400                                 PVMF_JB_LOGERROR((0, "PVMFJitterBufferImpl::RegisterMediaMsg: Invalid State to accept any packet"));
    401                                 PVMF_JB_LOGDATATRAFFIC_IN((0, "PVMFJitterBufferImpl::RegisterMediaMsg: Invalid State to accept any packet"));
    402                                 retval = PVMF_JB_REGISTER_MEDIA_MSG_ERR_CORRUPT_PACKET;
    403                             }
    404                         }
    405                         else
    406                         {
    407                             if (PVMF_JB_ERR_INSUFFICIENT_MEM_TO_PACKETIZE == status)
    408                             {
    409                                 retval = PVMF_JB_REGISTER_MEDIA_MSG_FAILURE_INSUFFICIENT_MEMORY_FOR_PACKETIZATION;
    410                             }
    411                             if (PVMF_JB_ERR_INVALID_CONFIGURATION == status)
    412                             {
    413                                 retval = PVMF_JB_BUFFER_REGISTER_MEDIA_MSG_ERR_CONFIG_NOT_SUPPORTED;
    414                             }
    415                             if ((PVMF_JB_ERR_CORRUPT_HDR == status) || (PVMF_JB_ERR_TRUNCATED_HDR == status))
    416                             {
    417                                 retval = PVMF_JB_REGISTER_MEDIA_MSG_ERR_INVALID_HEADER;
    418                             }
    419                             if (PVMF_JB_ERR_UNEXPECTED_PKT == status)
    420                             {
    421                                 retval = PVMF_JB_REGISTER_MEDIA_MSG_ERR_UNEXPECTED_DATA;
    422                             }
    423                             if (PVMF_JB_ERR_LATE_PACKET == status)
    424                             {
    425                                 retval = PVMF_JB_REGISTER_MEDIA_MSG_ERR_LATE_MSG;
    426                             }
    427                             PVMF_JB_LOGERROR((0, "PVMFJitterBufferImpl::RegisterMediaMsg: ParsePacketHeader failed retval %d", retval));
    428                         }
    429                         if (retval == PVMF_JB_REGISTER_MEDIA_MSG_FAILURE_INSUFFICIENT_MEMORY_FOR_PACKETIZATION || retval == PVMF_JB_REGISTER_MEDIA_MSG_FAILURE_JB_FULL)
    430                         {
    431                             //Serious err.. drop the packets... May be leave and let the obs trap...?
    432                             break;
    433                         }
    434                     }
    435                 }
    436                 else
    437                 {
    438                     //invalid use case, not expected, cause exception(leave) here
    439                     OSCL_LEAVE(JBPacketRegistrationConfigurationCorrupted);
    440                 }
    441             }
    442         }
    443     }
    444     return retval;
    445 }
    446 
    447 OSCL_EXPORT_REF PVMFStatus PVMFJitterBufferImpl::RetrievePacket(PVMFSharedMediaMsgPtr& aMediaMsgPtr, bool& aCmdPacket)
    448 {
    449     aCmdPacket = false;
    450     PVMFSharedMediaMsgPtr mediaMsg;
    451     if (CanRetrievePacket(aMediaMsgPtr, aCmdPacket))
    452     {
    453         return PVMFSuccess;
    454     }
    455     else
    456     {
    457         return PVMFErrNotReady;
    458     }
    459 }
    460 
    461 OSCL_EXPORT_REF PVMFStatus PVMFJitterBufferImpl::SetInputPacketHeaderPreparsed(bool aPreParsed)
    462 {
    463     iHeaderPreParsed = aPreParsed;
    464     return PVMFSuccess;
    465 }
    466 
    467 OSCL_EXPORT_REF PVMFJitterBufferStats& PVMFJitterBufferImpl::getJitterBufferStats()
    468 {
    469     OSCL_ASSERT(iJitterBuffer);
    470     return (iJitterBuffer->getStats());
    471 }
    472 
    473 OSCL_EXPORT_REF void PVMFJitterBufferImpl::FlushJitterBuffer()
    474 {
    475     iFirstDataPackets.clear();
    476     iJitterBuffer->Clear();
    477     iJitterBuffer->ResetJitterBufferStats();
    478     iRTPInfoParamsVec.clear();
    479 }
    480 
    481 OSCL_EXPORT_REF void PVMFJitterBufferImpl::ResetJitterBuffer()
    482 {
    483     FlushJitterBuffer();
    484     iJitterBuffer->ResetJitterBufferStats();
    485 
    486     iRTPInfoParamsVec.clear();
    487     iMonotonicTimeStamp = 0;
    488     iPrevTSOut = 0;
    489     iMaxAdjustedRTPTS = 0;
    490 }
    491 
    492 OSCL_EXPORT_REF PVMFTimestamp PVMFJitterBufferImpl::peekNextElementTimeStamp()
    493 {
    494     if (iJitterBuffer->getNumElements() > 0)
    495     {
    496         PVMFTimestamp currTS;
    497         PVMFTimestamp prevTS;
    498         uint32 aSeqNum;
    499         iJitterBuffer->peekNextElementTimeStamp(currTS, aSeqNum);
    500         DeterminePrevTimeStampPeek(aSeqNum, prevTS);
    501         uint64 ts64 = iMonotonicTimeStamp;
    502         ts64 += (currTS - prevTS);
    503         PVMFTimestamp adjTS =
    504             (PVMFTimestamp)(Oscl_Int64_Utils::get_uint64_lower32(ts64));
    505         return (adjTS);
    506     }
    507     else
    508     {
    509         PVMFTimestamp adjTS =
    510             (PVMFTimestamp)(Oscl_Int64_Utils::get_uint64_lower32(iMonotonicTimeStamp));
    511         return (adjTS);
    512     }
    513 }
    514 
    515 OSCL_EXPORT_REF PVMFTimestamp PVMFJitterBufferImpl::peekMaxElementTimeStamp()
    516 {
    517     if (iJitterBuffer->getNumElements() > 0)
    518     {
    519         PVMFTimestamp currTS;
    520         PVMFTimestamp maxTS;
    521         PVMFTimestamp prevTS;
    522         uint32 aSeqNum;
    523         iJitterBuffer->peekNextElementTimeStamp(currTS, aSeqNum);
    524         DeterminePrevTimeStampPeek(aSeqNum, prevTS);
    525         iJitterBuffer->peekMaxElementTimeStamp(maxTS, aSeqNum);
    526         uint64 ts64 = iMonotonicTimeStamp;
    527         ts64 += (maxTS - prevTS);
    528         PVMFTimestamp adjTS =
    529             (PVMFTimestamp)(Oscl_Int64_Utils::get_uint64_lower32(ts64));
    530         return (adjTS);
    531     }
    532     else
    533     {
    534         PVMFTimestamp adjTS =
    535             (PVMFTimestamp)(Oscl_Int64_Utils::get_uint64_lower32(iMonotonicTimeStamp));
    536         return (adjTS);
    537     }
    538 }
    539 
    540 ///////////////////////////////////////////////////////////////////////////////
    541 ///////////////////////////////////////////////////////////////////////////////
    542 
    543 OSCL_EXPORT_REF PVMFJitterBufferRegisterMediaMsgStatus PVMFJitterBufferImpl::RegisterCmdPacket(PVMFSharedMediaMsgPtr& aMediaCmd)
    544 {
    545     if (aMediaCmd->getFormatID() == PVMF_MEDIA_CMD_EOS_FORMAT_ID)
    546     {
    547         PVMF_JB_LOGERROR((0, "PVMFJitterBufferImpl::RegisterCmdPacket - EOS Received"));
    548         PVMF_JB_LOGDATATRAFFIC_IN((0, "PVMFJitterBufferImpl::RegisterCmdPacket - EOS Received"));
    549         EOSCmdReceived();
    550     }
    551     else /* unknown command */
    552     {
    553         PVMF_JB_LOGERROR((0, "PVMFJitterBufferImpl::ProcessIncomingMsgRTP - Unknown Cmd Recvd"));
    554         PVMF_JB_LOGDATATRAFFIC_IN((0, "PVMFJitterBufferImpl::ProcessIncomingMsgRTP - Unknown Cmd Recvd"));
    555         PVMFJitterBufferStats stats = getJitterBufferStats();
    556         MediaCommandMsgHolder cmdHolder;
    557         cmdHolder.iPreceedingMediaMsgSeqNumber = stats.lastRegisteredSeqNum;
    558         cmdHolder.iCmdMsg = aMediaCmd;
    559         iMediaCmdVec.push_back(cmdHolder);
    560     }
    561     return PVMF_JB_REGISTER_MEDIA_MSG_SUCCESS;
    562 }
    563 
    564 PVMFJitterBufferRegisterMediaMsgStatus PVMFJitterBufferImpl::RegisterDataPacket(PVMFSharedMediaDataPtr& aDataPacket)
    565 {
    566     PVMFJitterBufferRegisterMediaMsgStatus retval = AddPacket(aDataPacket);
    567     if (retval == PVMF_JB_REGISTER_MEDIA_MSG_SUCCESS)
    568     {
    569         PerformFlowControl(true);
    570     }
    571     uint32 aClockDiff = 0;
    572     bool delayEstablished = IsDelayEstablished(aClockDiff);//To update delay percent
    573     if (iReportCanRetrievePacket && delayEstablished)
    574     {
    575         //Check if delay is established
    576         //There's no hole for for min threshold time
    577         if (CanRetrievePacket())
    578         {
    579             iObserver->PacketReadyToBeRetrieved(iObserverContext);
    580             iReportCanRetrievePacket = false;
    581         }
    582     }
    583     return retval;
    584 }
    585 
    586 PVMFJitterBufferRegisterMediaMsgStatus PVMFJitterBufferImpl::AddPacket(PVMFSharedMediaDataPtr& aDataPacket)
    587 {
    588     PVMFJitterBufferRegisterMediaMsgStatus retval = PVMF_JB_REGISTER_MEDIA_MSG_SUCCESS;
    589     if (iEOSSent)
    590     {
    591         /* EOS received - do not register packet */
    592         PVMF_JB_LOGWARNING((0, "0x%x PVMFJitterBufferImpl::addPacket: After EOS Reached!!!", this));
    593         return PVMF_JB_REGISTER_MEDIA_MSG_ERR_EOS_SIGNALLED;
    594     }
    595 
    596     if (seqNumLock)
    597     {
    598         if (oSSRCFromSetUpResponseAvailable == false)
    599         {
    600             oSSRCFromSetUpResponseAvailable = true;
    601             SSRCLock = aDataPacket->getStreamID();
    602             iObserver->MediaTrackSSRCEstablished(this, SSRCLock);
    603             PVMF_JB_LOGDATATRAFFIC_IN((0, "PVMFJitterBufferImpl::addPacket: Ser No SSRC, set to 1st pkt SSRC %d", SSRCLock));
    604         }
    605         /* Filter based on SSRC */
    606         if (aDataPacket->getStreamID() == SSRCLock)
    607         {
    608             PVMFJitterBufferAddElemStatus status;
    609 
    610             status = iJitterBuffer->addElement(aDataPacket, iFirstSeqNum);
    611             if (status == PVMF_JITTER_BUFFER_ADD_ELEM_SUCCESS)
    612             {
    613                 PVMF_JB_LOGDATATRAFFIC_IN((0, "PVMFJitterBufferImpl::addPacket: MimeType=%s TS=%u, SEQNUM= %d",
    614                                            irMimeType.get_cstr(), aDataPacket->getTimestamp(), aDataPacket->getSeqNum()));
    615 
    616                 if (iRTPInfoParamsVec.size() > 0)
    617                 {
    618                     /*
    619                      * Calculate adjusted RTP TS - Will be used to update
    620                      * the estimated server clock, if any only if some rtp-info
    621                      * params have been set
    622                      */
    623                     ComputeMaxAdjustedRTPTS();
    624                 }
    625             }
    626             else if (status == PVMF_JITTER_BUFFER_ADD_ELEM_PACKET_OVERWRITE)
    627             {
    628                 PVMF_JB_LOGDATATRAFFIC_IN_E((0, "PVMFJitterBufferImpl::addPacket - OverWrite: MimeType=%s TS=%d, SEQNUM= %d",
    629                                              irMimeType.get_cstr(), aDataPacket->getTimestamp(), aDataPacket->getSeqNum()));
    630                 retval = PVMF_JB_REGISTER_MEDIA_MSG_ERR_UNKNOWN;
    631             }
    632         }
    633     }
    634     else
    635     {
    636         if (oSSRCFromSetUpResponseAvailable && (aDataPacket->getStreamID() != SSRCFromSetUpResponse))
    637         {//discard packet
    638             PVMF_JB_LOGERROR((0, "PVMFJitterBufferImpl::addPacket: ERROR wrong ssrc %d", aDataPacket->getStreamID()));
    639             return PVMF_JB_REGISTER_MEDIA_MSG_SUCCESS;
    640         }
    641         // Add packet to temporary array
    642         iFirstDataPackets.push_back(aDataPacket);
    643 
    644         const uint cPktNeededForVote = 5;
    645         if (iFirstDataPackets.size() < cPktNeededForVote)
    646             return PVMF_JB_REGISTER_MEDIA_MSG_SUCCESS;
    647 
    648         //Majortiy vote for SSRC first
    649         //0 count; 1 ssrc; 2 seqbase; 3 timestamp; uint32 PVMFTimestamp;
    650         uint32 my_ssrc[cPktNeededForVote][4];
    651         for (uint32 i = 0; i < cPktNeededForVote; i++)
    652             my_ssrc[i][0] = my_ssrc[i][1] = my_ssrc[i][2] = my_ssrc[i][3] = 0;
    653 
    654         {
    655             // 1. vote
    656             for (uint32 i = 0; i < cPktNeededForVote; i++)
    657             {
    658                 uint32 ssrc = iFirstDataPackets[i]->getStreamID();
    659                 for (uint32 j = 0; j < cPktNeededForVote; j++)
    660                 {
    661                     if (my_ssrc[j][0] > 0)
    662                     {
    663                         if (ssrc ==  my_ssrc[j][1])
    664                         {
    665                             my_ssrc[j][0]++;
    666                             if (iFirstDataPackets[i]->getSeqNum() < my_ssrc[j][2])
    667                             {
    668                                 my_ssrc[j][2] = iFirstDataPackets[i]->getSeqNum();
    669                                 my_ssrc[j][3] = iFirstDataPackets[i]->getTimestamp();
    670                             }
    671                             break;
    672                         }
    673                     }
    674                     else
    675                     {
    676                         my_ssrc[j][0]++; //my_ssrc[j][0]=1
    677                         my_ssrc[j][1] = ssrc;
    678                         my_ssrc[j][2] = iFirstDataPackets[i]->getSeqNum();
    679                         my_ssrc[j][3] = iFirstDataPackets[i]->getTimestamp();
    680                         break;
    681                     }
    682                 }
    683             }
    684         }
    685 
    686         {// 2. poll ssrc
    687             uint32 first_ssrc_index = 0, second_ssrc_index = 1;
    688             if (my_ssrc[0][0] < my_ssrc[1][0])
    689             {
    690                 first_ssrc_index = 1;
    691                 second_ssrc_index = 0;
    692             }
    693             for (uint32 i = 2; i < cPktNeededForVote; i++)
    694             {
    695                 if (my_ssrc[i][0] > first_ssrc_index)
    696                 {
    697                     second_ssrc_index = first_ssrc_index;
    698                     first_ssrc_index = i;
    699                 }
    700                 else if (my_ssrc[i][0] > second_ssrc_index)
    701                 {
    702                     second_ssrc_index =  i;
    703                 }
    704             }
    705             if (my_ssrc[first_ssrc_index][0] <= my_ssrc[second_ssrc_index][0])
    706             {
    707                 //got a tie. should rarely happen
    708                 //for now, just pick the first one
    709                 PVMF_JB_LOGERROR((0, "PVMFJitterBufferImpl::addPacket: ERROR first %d second %d", first_ssrc_index, second_ssrc_index));
    710             }
    711             SSRCLock = my_ssrc[first_ssrc_index][1];
    712             seqNumLock = true;
    713 
    714             //if we got RTPInfo by now, we should still use it
    715             if (iRTPInfoParamsVec.size() > 0)
    716             {
    717                 Oscl_Vector<PVMFRTPInfoParams, OsclMemAllocator>::iterator it;
    718                 it = iRTPInfoParamsVec.begin();
    719                 iFirstSeqNum = (it->seqNumBaseSet) ? it->seqNum : my_ssrc[first_ssrc_index][2];
    720                 seqLockTimeStamp = (it->rtpTimeBaseSet) ? it->rtpTime : my_ssrc[first_ssrc_index][3];
    721             }
    722             else
    723             {
    724                 iFirstSeqNum = my_ssrc[first_ssrc_index][2];
    725                 seqLockTimeStamp = my_ssrc[first_ssrc_index][3];
    726             }
    727             // iFirstSeqNum must be initialized when we come here
    728             iJitterBuffer->setFirstSeqNumAdded(iFirstSeqNum);
    729             CheckForRTPTimeAndRTPSeqNumberBase();
    730         }
    731         // 3.throw away the pkt not belong to current session and register packets
    732 
    733         bool bNoErr = true;
    734         Oscl_Vector<PVMFSharedMediaDataPtr, OsclMemAllocator>::iterator it;
    735         for (it = iFirstDataPackets.begin(); it != iFirstDataPackets.end();     it++)
    736         {
    737             if ((*it)->getStreamID() == SSRCLock)
    738             {
    739                 //Validate the packet before trying to push
    740 
    741                 PVMFSharedMediaDataPtr& elem = *it;
    742                 uint32 seqNum = elem->getSeqNum();
    743                 PVMFTimestamp packetTs = elem->getTimestamp();
    744 
    745                 if (iFirstSeqNum != seqNum)
    746                 {
    747                     PVMFJitterBufferStats& jbStats = getJitterBufferStats();
    748 
    749                     if (iBroadCastSession == true)
    750                     {
    751                         /*
    752                          * This can happen when using prerecorded transport streams that loop
    753                          * If this happens, just signal an unexpected data event
    754                          */
    755                         if (packetTs < jbStats.maxTimeStampRegistered)
    756                         {
    757                             return PVMF_JB_REGISTER_MEDIA_MSG_ERR_UNEXPECTED_DATA;
    758                         }
    759                     }
    760 
    761                     if (!IsSeqTsValidForPkt(seqNum, packetTs, jbStats))
    762                     {
    763                         return PVMF_JB_REGISTER_MEDIA_MSG_ERR_LATE_MSG;
    764                     }
    765                 }
    766 
    767                 if (! iJitterBuffer->addElement(*it, iFirstSeqNum))
    768                 {
    769                     PVMF_JB_LOGERROR((0, "PVMFJitterBufferImpl::addPacket: addElement failed"));
    770                     bNoErr = false;
    771                 }
    772                 else
    773                 {
    774                     PVMF_JB_LOGDATATRAFFIC_IN((0, "PVMFJitterBufferImpl::addPacket: MimeType=%s TS=%d, SEQNUM= %d",
    775                                                irMimeType.get_cstr(), (*it)->getTimestamp(), (*it)->getSeqNum()));
    776                 }
    777             }
    778         }
    779 
    780         iFirstDataPackets.clear();
    781         if (iRTPInfoParamsVec.size() > 0)
    782         {
    783             /*
    784             * Calculate adjusted RTP TS - Will be used to update
    785             * the estimated server clock, if any only if some rtp-info
    786             * params have been set
    787                 */
    788             ComputeMaxAdjustedRTPTS();
    789         }
    790 
    791         if (!bNoErr)
    792             return PVMF_JB_REGISTER_MEDIA_MSG_ERR_UNKNOWN;
    793 
    794     }
    795 
    796     PVMFJitterBufferStats stats = getJitterBufferStats();
    797     if (stats.totalPacketsLost > 0)
    798     {
    799         PVMF_JB_LOGWARNING((0, "Packet Loss: MimeType=%s, NumPacketsLost=%d", irMimeType.get_cstr(), stats.totalPacketsLost));
    800     }
    801     PVMF_JB_LOGINFO((0, "PVMFJitterBufferImpl::addPacket - JB Occup Stats - MimeType=%s, MaxSize=%d, CurrOccupany=%d", irMimeType.get_cstr(), iJitterBuffer->getArraySize(), iJitterBuffer->getNumElements()));
    802     return retval;
    803 }
    804 
    805 ///////////////////////////////////////////////////////////////////////////////
    806 //Prtected functions to implement the interface functions
    807 ///////////////////////////////////////////////////////////////////////////////
    808 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::CanRegisterMediaMsg()
    809 {
    810     if ((STREAMINGSTATE_STARTED == iStreamingState) || (STREAMINGSTATE_PAUSED == iStreamingState))
    811     {
    812         return true;
    813     }
    814     return false;
    815 }
    816 
    817 void PVMFJitterBufferImpl::CreateAllocators()
    818 {
    819     //Create allocators
    820     iMediaMsgMemPool = OSCL_NEW(OsclMemPoolFixedChunkAllocator,
    821                                 (DEFAULT_NUM_MEDIA_MSGS_IN_JITTER_BUFFER));
    822     iMediaDataImplMemPool = OSCL_NEW(OsclMemPoolFixedChunkAllocator,
    823                                      (DEFAULT_NUM_MEDIA_MSGS_IN_JITTER_BUFFER));
    824     iMediaDataGroupAlloc = OSCL_NEW(PVMFMediaFragGroupCombinedAlloc<OsclMemPoolFixedChunkAllocator>,
    825                                     (DEFAULT_NUM_MEDIA_MSGS_IN_JITTER_BUFFER,
    826                                      DEFAULT_NUM_FRAGMENTS_IN_MEDIA_MSG,
    827                                      iMediaDataImplMemPool));
    828     iMediaDataGroupAlloc->create();
    829 }
    830 
    831 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::CreateMediaData(PVMFSharedMediaDataPtr& dataPacket, OsclSharedPtr<PVMFMediaDataImpl>& mediaDataOut)
    832 {
    833     int32 err;
    834 
    835     OSCL_TRY(err,
    836              dataPacket = PVMFMediaData::createMediaData(mediaDataOut,
    837                           iMediaMsgMemPool););
    838 
    839     if (err != OsclErrNone)
    840     {
    841         return false;
    842     }
    843 
    844     return true;
    845 }
    846 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::Allocate(OsclSharedPtr<PVMFMediaDataImpl>& mediaDataOut)
    847 {
    848     int32 err;
    849     OSCL_TRY(err,
    850              mediaDataOut = iMediaDataGroupAlloc->allocate());
    851 
    852     if (err != OsclErrNone)
    853     {
    854         return false;
    855     }
    856 
    857     // If there is no memory left return false
    858     if (mediaDataOut.GetRep() == NULL)
    859     {
    860         return false;
    861     }
    862 
    863     return true;
    864 }
    865 
    866 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::RequestEventCallBack(JB_NOTIFY_CALLBACK aEventType, uint32 aDelay, OsclAny* aContext)
    867 {
    868     OSCL_UNUSED_ARG(aContext);
    869     bool retval = false;
    870     switch (aEventType)
    871     {
    872         case JB_NOTIFY_WAIT_FOR_OOO_PACKET_COMPLETE :
    873         {
    874             PVMFJBEventNotificationRequestInfo eventRequestInfo(CLOCK_NOTIFICATION_INTF_TYPE_ESTIMATEDSERVER, this, NULL);
    875             retval = irJBEventNotifier.RequestCallBack(eventRequestInfo, aDelay, iWaitForOOOPacketCallBkId);
    876             if (retval)
    877             {
    878                 PVMF_JB_LOG_EVENTS_CLOCK((0, "PVMFJitterBufferNode::RequestEventCallBack In OOO CallBackId [%d] Mime %s", iWaitForOOOPacketCallBkId, irMimeType.get_cstr()));
    879                 iWaitForOOOPacketCallBkPending = true;
    880             }
    881         }
    882         break;
    883         case JB_MONITOR_REBUFFERING:
    884         {
    885             PVMFJBEventNotificationRequestInfo eventRequestInfo(CLOCK_NOTIFICATION_INTF_TYPE_CLIENTPLAYBACK, this, NULL);
    886             retval = irJBEventNotifier.RequestCallBack(eventRequestInfo, aDelay, iMonitorReBufferingCallBkId);
    887             if (retval)
    888             {
    889                 PVMF_JB_LOG_EVENTS_CLOCK((0, "PVMFJitterBufferNode::RequestEventCallBack In Rebuffering CallBackId [%d] Mime %s", iMonitorReBufferingCallBkId, irMimeType.get_cstr()));
    890                 iMonitorReBufferingCallBkPending = true;
    891             }
    892         }
    893         break;
    894         case JB_BUFFERING_DURATION_COMPLETE:
    895         {
    896             PVMFJBEventNotificationRequestInfo eventRequestInfo(CLOCK_NOTIFICATION_INTF_TYPE_NONDECREASING, this, NULL);
    897             retval = irJBEventNotifier.RequestCallBack(eventRequestInfo, iDurationInMilliSeconds, iJitterBufferDurationCallBkId);
    898             if (retval)
    899             {
    900                 iJitterBufferDurationCallBkPending = true;
    901             }
    902         }
    903         break;
    904         default:
    905         {
    906             //Log it
    907         }
    908     }
    909 
    910     return retval;
    911 }
    912 
    913 OSCL_EXPORT_REF void PVMFJitterBufferImpl::CancelEventCallBack(JB_NOTIFY_CALLBACK aEventType, OsclAny* aContext)
    914 {
    915     OSCL_UNUSED_ARG(aContext);
    916     switch (aEventType)
    917     {
    918         case JB_NOTIFY_WAIT_FOR_OOO_PACKET_COMPLETE:
    919         {
    920             PVMFJBEventNotificationRequestInfo eventRequestInfo(CLOCK_NOTIFICATION_INTF_TYPE_ESTIMATEDSERVER, this, NULL);
    921             irJBEventNotifier.CancelCallBack(eventRequestInfo, iWaitForOOOPacketCallBkId);
    922             iWaitForOOOPacketCallBkPending = false;
    923         }
    924         break;
    925         case JB_MONITOR_REBUFFERING:
    926         {
    927             PVMFJBEventNotificationRequestInfo eventRequestInfo(CLOCK_NOTIFICATION_INTF_TYPE_CLIENTPLAYBACK, this, NULL);
    928             irJBEventNotifier.CancelCallBack(eventRequestInfo, iMonitorReBufferingCallBkId);
    929             iMonitorReBufferingCallBkPending = false;
    930         }
    931         break;
    932         case JB_BUFFERING_DURATION_COMPLETE:
    933         {
    934             PVMFJBEventNotificationRequestInfo eventRequestInfo(CLOCK_NOTIFICATION_INTF_TYPE_NONDECREASING, this, NULL);
    935             irJBEventNotifier.CancelCallBack(eventRequestInfo, iJitterBufferDurationCallBkId);
    936             iJitterBufferDurationCallBkPending = false;
    937         }
    938         break;
    939 
    940         default:
    941         {
    942             //Log it
    943         }
    944     }
    945     return;
    946 }
    947 
    948 OSCL_EXPORT_REF void PVMFJitterBufferImpl::ProcessCallback(CLOCK_NOTIFICATION_INTF_TYPE aClockNotificationInterfaceType, uint32 aCallBkId, const OsclAny* aContext, PVMFStatus aStatus)
    949 {
    950     OSCL_UNUSED_ARG(aClockNotificationInterfaceType);
    951     PVMF_JB_LOG_EVENTS_CLOCK((0, "PVMFJitterBufferNode::ProcessCallBack In CallBackId [%d] Mime %s", aCallBkId, irMimeType.get_cstr()));
    952 
    953     if (PVMFSuccess == aStatus)
    954     {
    955         if (aCallBkId == iWaitForOOOPacketCallBkId)
    956         {
    957             iWaitForOOOPacketCallBkPending = false;
    958             HandleEvent_NotifyWaitForOOOPacketComplete(aContext);
    959         }
    960 
    961         if (aCallBkId == iMonitorReBufferingCallBkId)
    962         {
    963             iMonitorReBufferingCallBkPending = false;
    964             HandleEvent_MonitorReBuffering(aContext);
    965         }
    966         else if (aCallBkId == iJitterBufferDurationCallBkId)
    967         {
    968             iJitterBufferDurationCallBkPending = false;
    969             HandleEvent_JitterBufferBufferingDurationComplete();
    970         }
    971     }
    972     else
    973     {
    974         //Log it
    975     }
    976     PVMF_JB_LOG_EVENTS_CLOCK((0, "PVMFJitterBufferNode::ProcessCallBack Out"));
    977 }
    978 
    979 void PVMFJitterBufferImpl::HandleEvent_MonitorReBuffering(const OsclAny* aContext)
    980 {
    981     OSCL_UNUSED_ARG(aContext);
    982     uint32 timebase32 = 0;
    983     uint32 estServerClock = 0;
    984     uint32 clientClock = 0;
    985     bool overflowFlag = false;
    986     irEstimatedServerClock.GetCurrentTime32(estServerClock, overflowFlag, PVMF_MEDIA_CLOCK_MSEC, timebase32);
    987     irClientPlayBackClock.GetCurrentTime32(clientClock, overflowFlag, PVMF_MEDIA_CLOCK_MSEC, timebase32);
    988 
    989 
    990     PVMF_JB_LOG_EVENTS_CLOCK((0, "PVMFJitterBufferNode::IsJitterBufferReady - Time Delay Check - EstServClock=%d", estServerClock));
    991     PVMF_JB_LOG_EVENTS_CLOCK((0, "PVMFJitterBufferNode::IsJitterBufferReady - Time Delay Check - ClientClock=%d", clientClock));
    992 
    993     uint32 clockDiff;
    994     IsDelayEstablished(clockDiff); //just to evaluate the rebuiffering condition.
    995 }
    996 
    997 void PVMFJitterBufferImpl::HandleEvent_NotifyWaitForOOOPacketComplete(const OsclAny* aContext)
    998 {
    999     OSCL_UNUSED_ARG(aContext);
   1000     if (iReportCanRetrievePacket)
   1001     {
   1002         iObserver->PacketReadyToBeRetrieved(iObserverContext);
   1003     }
   1004 }
   1005 
   1006 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::IsCallbackPending(JB_NOTIFY_CALLBACK aEventType, OsclAny* aContext)
   1007 {
   1008     bool*  callBackPending = NULL;
   1009     switch (aEventType)
   1010     {
   1011         case JB_MONITOR_REBUFFERING:
   1012         {
   1013             callBackPending = &iMonitorReBufferingCallBkPending;
   1014         }
   1015         break;
   1016         case JB_NOTIFY_WAIT_FOR_OOO_PACKET_COMPLETE:
   1017         {
   1018             callBackPending = &iWaitForOOOPacketCallBkPending;
   1019         }
   1020         break;
   1021         case JB_BUFFERING_DURATION_COMPLETE:
   1022         {
   1023             callBackPending = &iJitterBufferDurationCallBkPending;
   1024         }
   1025         break;
   1026         default:
   1027             OSCL_ASSERT(false);
   1028     }
   1029     // PVMF_JB_LOG_EVENTS_CLOCK((0, "PVMFJitterBufferNode::IsCallbackPending - Event Type[%d] CallBackPending [%d] aContext[0x%x]", aEventType, *callBackPending, aContext));
   1030     return *callBackPending;
   1031 }
   1032 
   1033 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::QueueBOSCommand(uint32 aStreamId)
   1034 {
   1035     iStreamID = aStreamId;
   1036     PVMFSharedMediaCmdPtr sharedMediaCmdPtr = PVMFMediaCmd::createMediaCmd();
   1037     // Set the formatID, timestamp, sequenceNumber and streamID for the media message
   1038     sharedMediaCmdPtr->setFormatID(PVMF_MEDIA_CMD_BOS_FORMAT_ID);
   1039     uint32 seqNum = 0;
   1040     sharedMediaCmdPtr->setSeqNum(seqNum);
   1041     sharedMediaCmdPtr->setStreamID(iStreamID);
   1042 
   1043     PVMFSharedMediaMsgPtr mediaMsgOut;
   1044     convertToPVMFMediaCmdMsg(mediaMsgOut, sharedMediaCmdPtr);
   1045 
   1046 
   1047     addMediaCommand(mediaMsgOut);
   1048 
   1049     PVLOGGER_LOGMSG(PVLOGMSG_INST_LLDBG, ipLogger, PVLOGMSG_STACK_TRACE, (0, "PVMFJitterBufferNode::QueueBOSCommand() MIME=%s StreamID=%d", irMimeType.get_cstr(), iStreamID));
   1050     return true;
   1051 }
   1052 
   1053 OSCL_EXPORT_REF void PVMFJitterBufferImpl::SetInPlaceProcessingMode(bool aInPlaceProcessingMode)
   1054 {
   1055     iInPlaceProcessing = aInPlaceProcessingMode;
   1056     if (iInPlaceProcessing)
   1057     {
   1058         iOnePacketPerMediaMsg = true;
   1059     }
   1060     else
   1061     {
   1062         iOnePacketPerMediaMsg = false;
   1063     }
   1064 
   1065     iOnePacketPerFragment = true;
   1066 
   1067     if (iInPlaceProcessing == false)
   1068     {
   1069         CreateAllocators();
   1070     }
   1071     else
   1072     {
   1073         DestroyAllocators();
   1074     }
   1075 }
   1076 
   1077 OSCL_EXPORT_REF PVMFStatus
   1078 PVMFJitterBufferImpl::GenerateAndSendEOSCommand(PVMFSharedMediaMsgPtr& aMediaOutMsg, bool& aCmdPacket)
   1079 {
   1080     PVMF_JB_LOGINFO((0, "PVMFJitterBufferNode::GenerateAndSendEOSCommand"));
   1081     PVMF_JB_LOG_RTCPDATATRAFFIC_OUT((0, "PVMFJitterBufferImpl::GenerateAndSendEOSCommand In Mime[%s]-", irMimeType.get_cstr()));
   1082 
   1083     if (iEOSSent == false)
   1084     {
   1085         aCmdPacket = true;
   1086         PVMFSharedMediaCmdPtr sharedMediaCmdPtr =
   1087             PVMFMediaCmd::createMediaCmd();
   1088 
   1089         sharedMediaCmdPtr->setFormatID(PVMF_MEDIA_CMD_EOS_FORMAT_ID);
   1090 
   1091         sharedMediaCmdPtr->setStreamID(iStreamID);
   1092 
   1093         convertToPVMFMediaCmdMsg(aMediaOutMsg, sharedMediaCmdPtr);
   1094 
   1095         aMediaOutMsg->setTimestamp(iLastPacketOutTs);
   1096 
   1097         uint32 timebase32 = 0;
   1098         uint32 clientClock32 = 0;
   1099         bool overflowFlag = false;
   1100 
   1101         irClientPlayBackClock.GetCurrentTime32(clientClock32, overflowFlag, PVMF_MEDIA_CLOCK_MSEC, timebase32);
   1102         timebase32 = 0;
   1103         uint32 estServClock32 = 0;
   1104         irEstimatedServerClock.GetCurrentTime32(estServClock32, overflowFlag, PVMF_MEDIA_CLOCK_MSEC, timebase32);
   1105         PVMF_JB_LOGDATATRAFFIC_OUT((0, "PVMFJitterBufferNode::GenerateAndSendEOSCommand: MimeType=%s, StreamID=%d",
   1106                                     irMimeType.get_cstr(),
   1107                                     aMediaOutMsg->getStreamID()));
   1108         PVMF_JB_LOGDATATRAFFIC_OUT((0, "PVMFJitterBufferNode::GenerateAndSendEOSCommand: ClientClock=%d",
   1109                                     clientClock32));
   1110         PVMF_JB_LOGDATATRAFFIC_OUT((0, "PVMFJitterBufferNode::GenerateAndSendEOSCommand: EstServClock=%d",
   1111                                     estServClock32));
   1112         PVMF_JB_LOGCLOCK_SESSION_DURATION((0, "PVMFJitterBufferNode::GenerateAndSendEOSCommand: MimeType=%s, StreamID=%d",
   1113                                            irMimeType.get_cstr(),
   1114                                            aMediaOutMsg->getStreamID()));
   1115         PVMF_JB_LOGCLOCK_SESSION_DURATION((0, "PVMFJitterBufferNode::GenerateAndSendEOSCommand: ClientClock=%d",
   1116                                            clientClock32));
   1117         PVMF_JB_LOGCLOCK_SESSION_DURATION((0, "PVMFJitterBufferNode::GenerateAndSendEOSCommand: EstServClock=%d",
   1118                                            estServClock32));
   1119         iEOSSent = true;
   1120         return PVMFSuccess;
   1121     }
   1122     else
   1123     {
   1124         PVMF_JB_LOGINFO((0, "PVMFJitterBufferNode::GenerateAndSendEOSCommand - EOS already sent..."));
   1125     }
   1126 
   1127     return PVMFSuccess;
   1128 }
   1129 
   1130 OSCL_EXPORT_REF void PVMFJitterBufferImpl::NotifyCanRetrievePacket()
   1131 {
   1132     PVMF_JB_LOGDATATRAFFIC_OUT_E((0, "PVMFJitterBufferImpl::NotifyCanRetrievePacket Mime %s", irMimeType.get_cstr()));
   1133     iReportCanRetrievePacket = true;
   1134 }
   1135 
   1136 OSCL_EXPORT_REF void PVMFJitterBufferImpl::CancelNotifyCanRetrievePacket()
   1137 {
   1138     iReportCanRetrievePacket = false;
   1139 }
   1140 
   1141 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::IsDelayEstablished(uint32& aClockDiff)
   1142 {
   1143     aClockDiff = iDurationInMilliSeconds;
   1144     if (GetState() == PVMF_JITTER_BUFFER_IN_TRANSITION)
   1145     {
   1146         PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Jitter Buffer In Transition - Preparing for Seek"));
   1147         irDelayEstablished = false;
   1148         irJitterDelayPercent = 0;
   1149         return irDelayEstablished;
   1150     }
   1151 
   1152     uint32 timebase32 = 0;
   1153     uint32 estServerClock = 0;
   1154     uint32 clientClock = 0;
   1155     bool overflowFlag = false;
   1156 
   1157     irEstimatedServerClock.GetCurrentTime32(estServerClock, overflowFlag, PVMF_MEDIA_CLOCK_MSEC, timebase32);
   1158     irClientPlayBackClock.GetCurrentTime32(clientClock, overflowFlag, PVMF_MEDIA_CLOCK_MSEC, timebase32);
   1159 
   1160     PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Check - EstServClock=%d", estServerClock));
   1161     PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Check - ClientClock=%d", clientClock));
   1162 
   1163     if (iEOSSignalled)
   1164     {
   1165         /*
   1166         * No check needed - We are past the clip time, just play out the last
   1167         * bit in the jitter buffer
   1168         */
   1169         PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Session Duration Expired"));
   1170         if (irDelayEstablished == false)
   1171         {
   1172             /*
   1173             * Coming out of rebuffering in case we had gone into
   1174             * rebuffering just before
   1175             */
   1176             irJitterDelayPercent = 100;
   1177             PVMFAsyncEvent jbEvent(PVMFInfoEvent, PVMFInfoDataReady, NULL, NULL);
   1178             ReportJBInfoEvent(jbEvent);
   1179         }
   1180         irDelayEstablished = true;
   1181     }
   1182     else
   1183     {
   1184         uint32 diff32ms = 0;
   1185         bool isEarly = PVTimeComparisonUtils::IsEarlier(estServerClock, clientClock, diff32ms);
   1186         if (isEarly && diff32ms > 0)
   1187         {
   1188             /* Could happen during repositioning */
   1189             if (irDelayEstablished == true)
   1190             {
   1191                 aClockDiff = 0;
   1192                 irDelayEstablished = false;
   1193                 irJitterDelayPercent = 0;
   1194                 /* Start timer */
   1195                 PVMFAsyncEvent jbEvent(PVMFInfoEvent, PVMFInfoUnderflow, NULL, NULL);
   1196                 ReportJBInfoEvent(jbEvent);
   1197                 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Check - EstServClock=%d",
   1198                                   Oscl_Int64_Utils::get_uint64_lower32(estServerClock)));
   1199                 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Check - ClientClock=%d",
   1200                                   Oscl_Int64_Utils::get_uint64_lower32(clientClock)));
   1201                 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Estimated Serv Clock Less Than ClientClock!!!!"));
   1202                 PVMF_JB_LOGCLOCK_REBUFF((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Check - EstServClock=%d",
   1203                                          Oscl_Int64_Utils::get_uint64_lower32(estServerClock)));
   1204                 PVMF_JB_LOGCLOCK_REBUFF((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Check - ClientClock=%d",
   1205                                          Oscl_Int64_Utils::get_uint64_lower32(clientClock)));
   1206                 PVMF_JB_LOGCLOCK_REBUFF((0, "PVMFJitterBufferNode::IsDelayEstablished - Estimated Serv Clock Less Than ClientClock!!!!"));
   1207             }
   1208             return irDelayEstablished;
   1209         }
   1210         aClockDiff = diff32ms;
   1211         if (diff32ms >= iDurationInMilliSeconds)
   1212         {
   1213             uint32 jbSize = iBufferAlloc->getBufferSize();
   1214             uint32 largestContiguousFreeBlockSize = iBufferAlloc->getLargestContiguousFreeBlockSize();
   1215             uint32 minPercentOccupancy = 100;
   1216             if ((largestContiguousFreeBlockSize*100 / jbSize) < minPercentOccupancy)
   1217             {
   1218                 minPercentOccupancy = (uint32)(largestContiguousFreeBlockSize * 100 / jbSize);
   1219             }
   1220 
   1221             if ((prevMinPercentOccupancy < MIN_PERCENT_OCCUPANCY_THRESHOLD) && (minPercentOccupancy < MIN_PERCENT_OCCUPANCY_THRESHOLD))
   1222             {
   1223                 consecutiveLowBufferCount++;
   1224             }
   1225             else
   1226             {
   1227                 consecutiveLowBufferCount = 0;
   1228             }
   1229 
   1230             prevMinPercentOccupancy = minPercentOccupancy;
   1231             PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - minPercentOccupancy=%d, consecutiveLowBufferCount=%d",
   1232                               minPercentOccupancy,
   1233                               consecutiveLowBufferCount));
   1234 
   1235 
   1236             if ((diff32ms > JITTER_BUFFER_DURATION_MULTIPLIER_THRESHOLD*iDurationInMilliSeconds) && !iOverflowFlag && (consecutiveLowBufferCount > CONSECUTIVE_LOW_BUFFER_COUNT_THRESHOLD))
   1237             {
   1238                 iOverflowFlag = true;
   1239                 PVMFAsyncEvent jbEvent(PVMFInfoEvent, PVMFInfoOverflow, NULL, NULL);
   1240                 ReportJBInfoEvent(jbEvent);
   1241                 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished reporting PVMFInfoSourceOverflow"));
   1242             }
   1243 
   1244             if (irDelayEstablished == false)
   1245             {
   1246                 if (CheckNumElements())
   1247                 {
   1248                     PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Cancelling Jitter Buffer Duration Timer"));
   1249                     irDelayEstablished = true;
   1250                     irJitterDelayPercent = 100;
   1251                     PVMFAsyncEvent jbEvent(PVMFInfoEvent, PVMFInfoDataReady, NULL, NULL);
   1252                     ReportJBInfoEvent(jbEvent);
   1253                     PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Established - EstServClock=%d", estServerClock));
   1254                     PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Established - ClientClock=%d",  clientClock));
   1255                     PVMF_JB_LOGCLOCK_REBUFF((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Established - EstServClock=%d",
   1256                                              estServerClock));
   1257                     PVMF_JB_LOGCLOCK_REBUFF((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Established - ClientClock=%d",
   1258                                              clientClock));
   1259                 }
   1260                 else
   1261                 {
   1262                     irJitterDelayPercent = 0;
   1263                 }
   1264             }
   1265             else
   1266             {
   1267                 irJitterDelayPercent = 100;
   1268             }
   1269         }
   1270         else
   1271         {
   1272             /*
   1273             * Update the buffering percent - to be used while sending buffering
   1274             * status events, in case we go into rebuffering or if we are in buffering
   1275             * state.
   1276             */
   1277             irJitterDelayPercent = ((diff32ms * 100) / iDurationInMilliSeconds);
   1278             if (irDelayEstablished == true)
   1279             {
   1280                 if (diff32ms <= iRebufferingThresholdInMilliSeconds)
   1281                 {
   1282                     /* Implies that we are going into rebuffering */
   1283                     if (!iEOSSignalled)
   1284                     {
   1285                         irDelayEstablished = false;
   1286                         PVMFAsyncEvent jbEvent(PVMFInfoEvent, PVMFInfoUnderflow, NULL, NULL);
   1287                         ReportJBInfoEvent(jbEvent);
   1288                         LOGCLIENTANDESTIMATEDSERVCLK_REBUFF;
   1289                     }
   1290                     /* we are past the end of the clip, no more rebuffering */
   1291                     irClientPlayBackClock.Pause();
   1292                 }
   1293             }
   1294             if (irDelayEstablished == false && CheckNumElements() == false)
   1295             {
   1296                 irJitterDelayPercent = 0;
   1297             }
   1298             PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished: Delay Percent = %d", irJitterDelayPercent));
   1299         }
   1300         /* if we are not rebuffering check for flow control */
   1301         PerformFlowControl(false);
   1302     }
   1303     return (irDelayEstablished);
   1304 }
   1305 
   1306 OSCL_EXPORT_REF PVMFSharedMediaDataPtr PVMFJitterBufferImpl::RetrievePacketPayload()
   1307 {
   1308     PVMF_JB_LOGINFO((0, "PVMFJitterBufferImpl::retrievePacket - JB Occup Stats - MimeType=%s, MaxSize=%d, CurrOccupany=%d", irMimeType.get_cstr(), iJitterBuffer->getArraySize(), iJitterBuffer->getNumElements()));
   1309 
   1310     PVMFSharedMediaDataPtr elem = iJitterBuffer->retrieveElement();
   1311     if (elem.GetRep() != NULL)
   1312     {
   1313         /*
   1314          * Adjust TimeStamp - Goal is to provide a monotonically increasing
   1315          * timestamp.
   1316          */
   1317         PVMFTimestamp currTS = elem->getTimestamp();
   1318         DeterminePrevTimeStamp(elem->getSeqNum());
   1319         iMonotonicTimeStamp += (currTS - iPrevTSOut);
   1320         PVMFTimestamp adjustedTS =
   1321             (PVMFTimestamp)(Oscl_Int64_Utils::get_uint64_lower32(iMonotonicTimeStamp));
   1322         elem->setTimestamp(adjustedTS);
   1323 
   1324         PVMFJitterBufferStats* jbStatsPtr = iJitterBuffer->getStatsPtr();
   1325         jbStatsPtr->maxTimeStampRetrievedWithoutRTPOffset = adjustedTS;
   1326 
   1327         PVMF_JB_LOGDATATRAFFIC_OUT((0, "PVMFJitterBufferImpl::retrievePacket: MimeType=%s, TS=%d, SEQNUM= %d",
   1328                                     irMimeType.get_cstr(),
   1329                                     elem->getTimestamp(),
   1330                                     elem->getSeqNum()));
   1331         iPrevTSOut = currTS;
   1332     }
   1333     return elem;
   1334 }
   1335 
   1336 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::IsEmpty()
   1337 {
   1338     uint32 elems = iJitterBuffer->getNumElements();
   1339     if (elems == 0)
   1340     {
   1341         return true;
   1342     }
   1343     return false;
   1344 }
   1345 
   1346 OSCL_EXPORT_REF void PVMFJitterBufferImpl::SetEOS(bool aVal)
   1347 {
   1348     PVMF_JB_LOG_RTCPDATATRAFFIC_OUT((0, "PVMFJitterBufferImpl::SetEOS Mime[%s]", irMimeType.get_cstr()));
   1349     PVMF_JB_LOGDATATRAFFIC_OUT((0, "PVMFJitterBufferImpl::SetEOS Mime[%s]", irMimeType.get_cstr()));
   1350 
   1351     if (aVal && !iEOSSignalled)
   1352     {
   1353         LOGCLIENTANDESTIMATEDSERVCLK_DATAPATH_OUT;
   1354         iEOSSignalled = aVal;
   1355         iObserver->EndOfStreamSignalled(iObserverContext);
   1356     }
   1357 
   1358     if (iReportCanRetrievePacket)
   1359     {
   1360         if (CanRetrievePacket())
   1361         {
   1362             iObserver->PacketReadyToBeRetrieved(iObserverContext);
   1363         }
   1364     }
   1365 }
   1366 
   1367 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::GetEOS()
   1368 {
   1369     return iEOSSignalled;
   1370 }
   1371 
   1372 OSCL_EXPORT_REF void PVMFJitterBufferImpl::SetPlayRange(int32 aStartTimeInMS, bool aPlayAfterSeek, bool aStopTimeAvailable, int32 aStopTimeInMS)
   1373 {
   1374     iStartTimeInMS = aStartTimeInMS;
   1375     if (aStopTimeAvailable)
   1376     {
   1377         iPlayStopTimeAvailable  = aStopTimeAvailable;
   1378         iStopTimeInMS = aStopTimeInMS;
   1379     }
   1380 
   1381     if (aPlayAfterSeek)
   1382     {
   1383         iEOSSignalled = false;
   1384         iEOSSent = false;
   1385     }
   1386 }
   1387 
   1388 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::CheckForHighWaterMark()
   1389 {
   1390     PVMFJitterBufferStats stats = iJitterBuffer->getStats();
   1391     uint32 currOccupancy = stats.currentOccupancy;
   1392     uint32 maxOccupancy = stats.maxOccupancy;
   1393 
   1394     PVMF_JB_LOGINFO((0, "PVMFJitterBufferImpl::CheckForHighWaterMark: CurrOccupancy = %d", currOccupancy));
   1395     if (currOccupancy >=
   1396             maxOccupancy*DEFAULT_JITTER_BUFFER_HIGH_WATER_MARK)
   1397     {
   1398         return true;
   1399     }
   1400     return false;
   1401 }
   1402 
   1403 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::CheckForLowWaterMark()
   1404 {
   1405     PVMFJitterBufferStats stats = iJitterBuffer->getStats();
   1406     uint32 currOccupancy = stats.currentOccupancy;
   1407     uint32 maxOccupancy = stats.maxOccupancy;
   1408 
   1409     PVMF_JB_LOGINFO((0, "PVMFJitterBufferImpl::CheckForLowWaterMark: CurrOccupancy = %d", currOccupancy));
   1410     if (currOccupancy <=
   1411             maxOccupancy*DEFAULT_JITTER_BUFFER_LOW_WATER_MARK)
   1412     {
   1413         return true;
   1414     }
   1415     return false;
   1416 }
   1417 
   1418 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::CheckNumElements()
   1419 {
   1420     if (iJitterBuffer->getNumElements() > 0)
   1421     {
   1422         return true;
   1423     }
   1424     else
   1425     {
   1426         return false;
   1427     }
   1428 }
   1429 
   1430 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::addMediaCommand(PVMFSharedMediaMsgPtr& aMediaCmd)
   1431 {
   1432     MediaCommandMsgHolder cmdHolder;
   1433     cmdHolder.iPreceedingMediaMsgSeqNumber = getJitterBufferStats().lastRegisteredSeqNum;
   1434     cmdHolder.iCmdMsg = aMediaCmd;
   1435     iMediaCmdVec.push_back(cmdHolder);
   1436     return true;
   1437 }
   1438 
   1439 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::GetPendingCommand(PVMFSharedMediaMsgPtr& aCmdMsg)
   1440 {
   1441     /*
   1442      * Parse the command queue to see if it is time to send out
   1443      * any pending ones. if last retrieved seq num is same as
   1444      * the seq num of media msg that preceeds this cmd, then
   1445      * it is time to send this cmd out
   1446      */
   1447     PVMFJitterBufferStats stats = getJitterBufferStats();
   1448     Oscl_Vector<MediaCommandMsgHolder, OsclMemAllocator>::iterator it;
   1449     for (it = iMediaCmdVec.begin(); it != iMediaCmdVec.end(); it++)
   1450     {
   1451         if (it->iCmdMsg->getFormatID() == PVMF_MEDIA_CMD_BOS_FORMAT_ID)
   1452         {
   1453             aCmdMsg = it->iCmdMsg;
   1454             iMediaCmdVec.erase(it);
   1455             return true;
   1456         }
   1457         if (it->iPreceedingMediaMsgSeqNumber == stats.lastRetrievedSeqNum)
   1458         {
   1459             aCmdMsg = it->iCmdMsg;
   1460             iMediaCmdVec.erase(it);
   1461             return true;
   1462         }
   1463     }
   1464     return false;
   1465 }
   1466 
   1467 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::HasPendingCommand()
   1468 {
   1469     /*
   1470      * Parse the command queue to see if it is time to send out
   1471      * any pending ones. if last retrieved seq num is same as
   1472      * the seq num of media msg that preceeds this cmd, then
   1473      * it is time to send this cmd out
   1474      */
   1475     PVMFJitterBufferStats stats = getJitterBufferStats();
   1476     Oscl_Vector<MediaCommandMsgHolder, OsclMemAllocator>::iterator it;
   1477     for (it = iMediaCmdVec.begin(); it != iMediaCmdVec.end(); it++)
   1478     {
   1479         if (it->iCmdMsg->getFormatID() == PVMF_MEDIA_CMD_BOS_FORMAT_ID)
   1480         {
   1481             return true;
   1482         }
   1483         if (it->iPreceedingMediaMsgSeqNumber == stats.lastRetrievedSeqNum)
   1484         {
   1485             return true;
   1486         }
   1487     }
   1488     return false;
   1489 }
   1490 
   1491 
   1492 OSCL_EXPORT_REF void PVMFJitterBufferImpl::SetAdjustedTSInMS(PVMFTimestamp aAdjustedTSInMS)
   1493 {
   1494     // convert adjustedTS to RTP Timescale
   1495     uint32 in_wrap_count = 0;
   1496     MediaClockConverter clockConvertor;
   1497     clockConvertor.set_timescale(1000);
   1498     clockConvertor.set_clock(aAdjustedTSInMS, in_wrap_count);
   1499     uint32 adjustedTSInRTPTS = clockConvertor.get_converted_ts(iRTPTimeScale);
   1500     iMonotonicTimeStamp = adjustedTSInRTPTS;
   1501     PVMF_JB_LOGDATATRAFFIC_OUT((0, "PVMFJitterBufferImpl::SetAdjustedTS(): adjustedTSInRTPTS=%d, iMonotonicTS=%d", adjustedTSInRTPTS, Oscl_Int64_Utils::get_uint64_lower32(iMonotonicTimeStamp)));
   1502 }
   1503 
   1504 OSCL_EXPORT_REF void PVMFJitterBufferImpl::SetBroadCastSession()
   1505 {
   1506     iBroadCastSession = true;
   1507 }
   1508 
   1509 OSCL_EXPORT_REF PVMFRTPInfoParams& PVMFJitterBufferImpl::GetRTPInfoParams()
   1510 {
   1511     return *iRTPInfoParamsVec.end();
   1512 }
   1513 
   1514 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::GetRTPTimeStampOffset(uint32& aTimeStampOffset)
   1515 {
   1516     if (seqNumLock)
   1517         aTimeStampOffset = seqLockTimeStamp;
   1518 
   1519     return seqNumLock;
   1520 }
   1521 
   1522 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::NotifyFreeSpaceAvailable()
   1523 {
   1524     if (iMediaDataGroupAlloc)
   1525     {
   1526         iMediaDataGroupAlloc->notifyfreechunkavailable(*this);
   1527         return true;
   1528     }
   1529     return false;
   1530 }
   1531 
   1532 OSCL_EXPORT_REF void PVMFJitterBufferImpl::freechunkavailable(OsclAny*)
   1533 {
   1534     if (iObserver)
   1535     {
   1536         iObserver->JitterBufferFreeSpaceAvailable(iObserverContext);
   1537     }
   1538 }
   1539 
   1540 
   1541 
   1542 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::CheckSpaceAvailability(PVMFSharedMediaMsgPtr& aMsg)
   1543 {
   1544     if (iJitterBuffer)
   1545     {
   1546         return (iJitterBuffer->CheckSpaceAvailability(GetNumOfPackets(aMsg)));
   1547     }
   1548     return false;
   1549 }
   1550 
   1551 OSCL_EXPORT_REF bool PVMFJitterBufferImpl::CheckSpaceAvailability()
   1552 {
   1553     return (iJitterBuffer->CheckSpaceAvailability());
   1554 }
   1555 
   1556 OSCL_EXPORT_REF void PVMFJitterBufferImpl::setSSRC(uint32 aSSRC)
   1557 {
   1558     PVMF_JB_LOGDATATRAFFIC_IN((0, "PVMFJitterBufferImpl::setSSRC: Setting SSRCFromSetUpResponse - MimeType=%s, SSRC=%d", irMimeType.get_cstr(), aSSRC));
   1559     oSSRCFromSetUpResponseAvailable = true;
   1560     SSRCFromSetUpResponse = aSSRC;
   1561     SSRCLock = aSSRC;
   1562 }
   1563 
   1564 OSCL_EXPORT_REF uint32 PVMFJitterBufferImpl::GetSSRC() const
   1565 {
   1566     return SSRCLock;
   1567 }
   1568 
   1569 /**
   1570 */
   1571 //virtual bool CancelServerClockNotificationUpdates();
   1572 
   1573 
   1574 OSCL_EXPORT_REF void PVMFJitterBufferImpl::SetJitterBufferMemPoolInfo(uint32 aSize,
   1575         uint32 aResizeSize,
   1576         uint32 aMaxNumResizes,
   1577         uint32 aExpectedNumberOfBlocksPerBuffer)
   1578 {
   1579     iJitterBufferMemPoolInfo.Init(aSize, aResizeSize, aMaxNumResizes, aExpectedNumberOfBlocksPerBuffer);
   1580 }
   1581 
   1582 OSCL_EXPORT_REF void PVMFJitterBufferImpl::GetJitterBufferMemPoolInfo(uint32& aSize, uint32& aResizeSize, uint32& aMaxNumResizes, uint32& aExpectedNumberOfBlocksPerBuffer) const
   1583 {
   1584     aSize = iJitterBufferMemPoolInfo.iSize;
   1585     aResizeSize = iJitterBufferMemPoolInfo.iResizeSize;
   1586     aMaxNumResizes = iJitterBufferMemPoolInfo.iMaxNumResizes;
   1587     aExpectedNumberOfBlocksPerBuffer = iJitterBufferMemPoolInfo.iExpectedNumberOfBlocksPerBuffer;
   1588 }
   1589 
   1590 OSCL_EXPORT_REF void PVMFJitterBufferImpl::SetTrackConfig(OsclRefCounterMemFrag& aConfig)
   1591 {
   1592     iTrackConfig = aConfig;
   1593 }
   1594 
   1595 OSCL_EXPORT_REF void PVMFJitterBufferImpl::SetTimeScale(uint32 aTimeScale)
   1596 {
   1597     iTimeScale = aTimeScale;
   1598 }
   1599 
   1600 OSCL_EXPORT_REF uint32 PVMFJitterBufferImpl::GetTimeScale() const
   1601 {
   1602     return iTimeScale;
   1603 }
   1604 
   1605 OSCL_EXPORT_REF void PVMFJitterBufferImpl::SetMediaClockConverter(MediaClockConverter* aConverter)
   1606 {
   1607     ipMediaClockConverter = aConverter;
   1608 }
   1609 
   1610 OSCL_EXPORT_REF PVMFStatus PVMFJitterBufferImpl::PerformFlowControl(bool aIncomingMedia)
   1611 {
   1612     OSCL_UNUSED_ARG(aIncomingMedia);
   1613     return PVMFSuccess;
   1614 }
   1615 
   1616 
   1617 OSCL_EXPORT_REF void PVMFJitterBufferImpl::LogClientAndEstimatedServerClock(PVLogger*& aLogger)
   1618 {
   1619     uint32 timebase32 = 0;
   1620     uint32 estServerClock = 0;
   1621     uint32 clientClock = 0;
   1622     bool overflowFlag = false;
   1623     irEstimatedServerClock.GetCurrentTime32(estServerClock, overflowFlag, PVMF_MEDIA_CLOCK_MSEC, timebase32);
   1624     irClientPlayBackClock.GetCurrentTime32(clientClock, overflowFlag, PVMF_MEDIA_CLOCK_MSEC, timebase32);
   1625     PVLOGGER_LOGMSG(PVLOGMSG_INST_REL, aLogger, PVLOGMSG_INFO, (0, "The value of estimated server clock is [%d] and client clock is [%d]", estServerClock, clientClock));
   1626 }
   1627 
   1628 OSCL_EXPORT_REF uint32 PVMFJitterBufferImpl::GetNumOfPackets(PVMFSharedMediaMsgPtr& aMsg) const
   1629 {
   1630     if (iOnePacketPerFragment)
   1631     {
   1632         PVMFSharedMediaDataPtr inputDataPacket;
   1633         convertToPVMFMediaData(inputDataPacket, aMsg);
   1634         return inputDataPacket->getNumFragments();
   1635     }
   1636     else
   1637         return 1;
   1638 }
   1639 
   1640 OSCL_EXPORT_REF void PVMFJitterBufferImpl::ReportJBInfoEvent(PVMFAsyncEvent& aEvent)
   1641 {
   1642     iObserver->ProcessJBInfoEvent(aEvent);
   1643 }
   1644 
   1645 void PVMFJitterBufferImpl::HandleEvent_JitterBufferBufferingDurationComplete()
   1646 {
   1647     PVMF_JB_LOG_EVENTS_CLOCK((0, "PVMFJitterBufferNode::HandleEvent_JitterBufferBufferingDurationComplete In"));
   1648 
   1649     if (!irDelayEstablished)
   1650     {
   1651         uint32 aClockDiff = 0;
   1652         bool delayEstablished = IsDelayEstablished(aClockDiff);//To update delay percent
   1653         if (iReportCanRetrievePacket && delayEstablished)
   1654         {
   1655             //Check if delay is established
   1656             //There's no hole for for min threshold time
   1657             if (CanRetrievePacket())
   1658             {
   1659                 iObserver->PacketReadyToBeRetrieved(iObserverContext);
   1660                 iReportCanRetrievePacket = false;
   1661             }
   1662         }
   1663         else
   1664         {
   1665             RequestEventCallBack(JB_BUFFERING_DURATION_COMPLETE);
   1666         }
   1667     }
   1668 
   1669     PVMF_JB_LOG_EVENTS_CLOCK((0, "PVMFJitterBufferNode::HandleEvent_JitterBufferBufferingDurationComplete Out"));
   1670 }
   1671