• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define PVMF_JITTER_BUFFER_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 OSCL_MEM_MEMPOOL_H_INCLUDED
31 #include "oscl_mem_mempool.h"
32 #endif
33 #ifndef PVLOGGER_H_INCLUDED
34 #include "pvlogger.h"
35 #endif
36 #ifndef PVMF_TIMESTAMP_H_INCLUDED
37 #include "pvmf_timestamp.h"
38 #endif
39 #ifndef PVMF_MEDIA_DATA_H_INCLUDED
40 #include "pvmf_media_data.h"
41 #endif
42 #ifndef PVMF_MEDIA_FRAG_GROUP_H_INCLUDED
43 #include "pvmf_media_frag_group.h"
44 #endif
45 #ifndef PVMF_SM_TUNABLES_H_INCLUDED
46 #include "pvmf_sm_tunables.h"
47 #endif
48 #ifndef __MEDIA_CLOCK_CONVERTER_H
49 #include "media_clock_converter.h"
50 #endif
51 #ifndef PVMF_MEDIA_MSG_FORMAT_IDS_H_INCLUDED
52 #include "pvmf_media_msg_format_ids.h"
53 #endif
54 
55 #ifndef PVMF_EVENT_HANDLING_H_INCLUDED
56 #include "pvmf_event_handling.h"
57 #endif
58 
59 #ifndef PVMF_JITTER_BUFFER_COMMON_TYPES_H_INCLUDED
60 #include "pvmf_jitter_buffer_common_types.h"
61 #endif
62 
63 #ifndef PVMF_MEDIA_CLOCK_H_INCLUDED
64 #include "pvmf_media_clock.h"
65 #endif
66 
67 #ifndef PVMF_JITTER_BUFFER_COMMON_INTERNAL_H
68 #include "pvmf_jitter_buffer_common_internal.h"
69 #endif
70 #ifndef OSCL_DLL_H_INCLUDED
71 #include "oscl_dll.h"
72 #endif
73 
74 #ifndef PVMF_JB_EVENT_NOTIFIER_H
75 #include "pvmf_jb_event_notifier.h"
76 #endif
77 
78 #ifndef PVMF_FORMAT_TYPE_H_INCLUDED
79 #include "pvmf_format_type.h"
80 #endif
81 
82 class PVMFSMSharedBufferAllocWithReSize;
83 
84 
85 class PVMFJitterBufferStats
86 {
87     public:
PVMFJitterBufferStats()88         PVMFJitterBufferStats()
89         {
90             ResetStats();
91         }
92 
ResetStats()93         void ResetStats()
94         {
95             totalNumPacketsReceived = 0;
96             totalNumPacketsRegistered = 0;
97             totalPacketsLost = 0;
98             totalNumPacketsRetrieved = 0;
99             //Timestamp info
100             maxTimeStampRegistered = 0;
101             maxTimeStampRetrieved = 0;
102 
103             //Occupancy related
104             maxOccupancy = 0;
105             currentOccupancy = 0;
106             totalNumBytesRecvd = 0;
107             packetSizeInBytesLeftInBuffer = 0;
108 
109             //Seq num info
110             maxSeqNumReceived = 0;
111             maxSeqNumRegistered = 0;
112             lastRegisteredSeqNum = 0;
113             lastRetrievedSeqNum = 0;
114             seqNumBase = 0;
115 
116             maxTimeStampRetrievedWithoutRTPOffset = 0;
117             ssrc = 0;
118         }
119 
120         //Packet info
121         uint32 totalNumPacketsReceived;
122         uint32 totalNumPacketsRegistered;
123         uint32 totalPacketsLost;
124         uint32 totalNumPacketsRetrieved;
125 
126         //Timestamp info
127         PVMFTimestamp maxTimeStampRegistered;
128         PVMFTimestamp maxTimeStampRetrieved;
129 
130         //Occupancy related
131         uint32 maxOccupancy;
132         uint32 currentOccupancy;
133         uint32 totalNumBytesRecvd;
134         uint32 packetSizeInBytesLeftInBuffer;
135 
136         //Seq num info
137         uint32 maxSeqNumReceived;
138         uint32 maxSeqNumRegistered;
139         uint32 lastRegisteredSeqNum;
140         uint32 lastRetrievedSeqNum;
141         uint32 seqNumBase;
142 
143         PVMFTimestamp maxTimeStampRetrievedWithoutRTPOffset;
144         uint32 ssrc;
145 };
146 
147 class MediaCommandMsgHolder
148 {
149     public:
MediaCommandMsgHolder()150         MediaCommandMsgHolder()
151         {
152             iPreceedingMediaMsgSeqNumber = 0xFFFFFFFF;
153         };
154 
MediaCommandMsgHolder(const MediaCommandMsgHolder & a)155         MediaCommandMsgHolder(const MediaCommandMsgHolder& a)
156         {
157             iPreceedingMediaMsgSeqNumber = a.iPreceedingMediaMsgSeqNumber;
158             iCmdMsg = a.iCmdMsg ;
159         }
160 
161         MediaCommandMsgHolder& operator=(const MediaCommandMsgHolder& a)
162         {
163             if (&a != this)
164             {
165                 iPreceedingMediaMsgSeqNumber = a.iPreceedingMediaMsgSeqNumber;
166                 iCmdMsg = a.iCmdMsg ;
167             }
168             return (*this);
169         }
170 
171         uint32 iPreceedingMediaMsgSeqNumber;
172         PVMFSharedMediaMsgPtr iCmdMsg;
173 };
174 
175 typedef enum
176 {
177     PVMF_JITTER_BUFFER_ADD_ELEM_ERROR,
178     PVMF_JITTER_BUFFER_ADD_ELEM_PACKET_OVERWRITE,
179     PVMF_JITTER_BUFFER_ADD_ELEM_SUCCESS
180 } PVMFJitterBufferAddElemStatus;
181 
182 template<class Alloc>
183 class PVMFDynamicCircularArray
184 {
185     public:
PVMFDynamicCircularArray()186         PVMFDynamicCircularArray()
187         {
188             iNumElems = 0;
189             iArraySize = 0;
190             iMaxSeqNumAdded = 0;
191             iLastRetrievedSeqNum = 0;
192             iLastRetrievedTS = 0;
193             iReadOffset = 0;
194             iFirstSeqNumAdded = 0;
195 
196             ipLogger = PVLogger::GetLoggerObject("PVMFDynamicCircularArray");
197             ipDataPathLoggerIn = PVLogger::GetLoggerObject("datapath.sourcenode.jitterbuffer.in");
198             ipDataPathLoggerOut = PVLogger::GetLoggerObject("datapath.sourcenode.jitterbuffer.out");
199         }
200 
PVMFDynamicCircularArray(uint32 n)201         PVMFDynamicCircularArray(uint32 n)
202         {
203             iNumElems = 0;
204             iArraySize = n;
205             iMaxSeqNumAdded = 0;
206             iLastRetrievedSeqNum = 0;
207             iLastRetrievedTS = 0;
208             iReadOffset = 0;
209             iFirstSeqNumAdded = 0;
210 
211             iMediaPtrVec.reserve(iArraySize);
212             InitVector(iArraySize);
213 
214             ipLogger = PVLogger::GetLoggerObject("PVMFDynamicCircularArray");
215             ipDataPathLoggerIn = PVLogger::GetLoggerObject("datapath.sourcenode.jitterbuffer.in");
216             ipDataPathLoggerOut = PVLogger::GetLoggerObject("datapath.sourcenode.jitterbuffer.out");
217         }
218 
~PVMFDynamicCircularArray()219         virtual ~PVMFDynamicCircularArray()
220         {
221             if (!iMediaPtrVec.empty())
222             {
223                 typedef typename Oscl_Vector<PVMFSharedMediaDataPtr, Alloc>::iterator iterator_type;
224                 iterator_type it;
225 
226                 for (it = iMediaPtrVec.begin(); it != iMediaPtrVec.end(); it++)
227                 {
228                     if (it->GetRep() != NULL)
229                     {
230                         it->Unbind();
231                     }
232                 }
233             }
234         }
235 
Clear()236         void Clear()
237         {
238             PVMF_JB_LOGINFO((0, "PVMFDynamicCircularArray::Clear - Cleared Jitter Buffer"));
239             typedef typename Oscl_Vector<PVMFSharedMediaDataPtr, Alloc>::iterator iterator_type;
240             iterator_type it;
241             for (it = iMediaPtrVec.begin(); it != iMediaPtrVec.end(); it++)
242             {
243                 if (it->GetRep() != NULL)
244                 {
245                     it->Unbind();
246                 }
247             }
248             iNumElems = 0;
249             iMediaPtrVec.clear();
250             InitVector(iArraySize);
251 
252             iMaxSeqNumAdded = 0;
253             iLastRetrievedSeqNum = 0;
254             iLastRetrievedTS = 0;
255             iReadOffset = 0;
256             iFirstSeqNumAdded = 0;
257             iJitterBufferStats.currentOccupancy = 0;
258             iJitterBufferStats.packetSizeInBytesLeftInBuffer = 0;
259         }
260 
ResetJitterBufferStats()261         void ResetJitterBufferStats()
262         {
263             iJitterBufferStats.ResetStats();
264         }
265 
getNumElements()266         uint32 getNumElements()
267         {
268             return iNumElems;
269         }
270 
getArraySize()271         uint32 getArraySize()
272         {
273             return iArraySize;
274         }
275 
growCircularArray(uint32 newSize)276         void growCircularArray(uint32 newSize)
277         {
278             if (newSize > iArraySize)
279             {
280                 /*
281                  * This transfers the existing contents
282                  * as well
283                  */
284                 iMediaPtrVec.reserve(newSize);
285                 /* Initialize the new space */
286                 InitVector((newSize - iArraySize));
287                 iArraySize = newSize;
288             }
289         }
290 
setFirstSeqNumAdded(uint32 aFirstSeqNumAdded)291         void setFirstSeqNumAdded(uint32 aFirstSeqNumAdded)
292         {
293             iFirstSeqNumAdded = aFirstSeqNumAdded;
294         }
295 
296         //validations on timestamp and seqnum has to be done by the user of the dynamic circular array.
addElement(PVMFSharedMediaDataPtr & elem,uint32 aSeqNumBase)297         PVMFJitterBufferAddElemStatus addElement(PVMFSharedMediaDataPtr& elem, uint32 aSeqNumBase)
298         {
299             PVMFJitterBufferAddElemStatus oRet = PVMF_JITTER_BUFFER_ADD_ELEM_SUCCESS;
300 
301             iJitterBufferStats.totalNumPacketsReceived++;
302             iJitterBufferStats.ssrc = elem->getStreamID();
303             uint32 seqNum = elem->getSeqNum();
304             /* Get packet size */
305             uint32 size = 0;
306             uint32 numFragments = elem->getNumFragments();
307             for (uint32 i = 0; i < numFragments; i++)
308             {
309                 OsclRefCounterMemFrag memFragIn;
310                 elem->getMediaFragment(i, memFragIn);
311                 size += memFragIn.getMemFrag().len;
312             }
313             iJitterBufferStats.totalNumBytesRecvd += size;
314             iJitterBufferStats.packetSizeInBytesLeftInBuffer += size;
315 
316             //Assumptions:
317             //- Validations based on ts and SeqNum are performed by the calling code,
318             //- By the time the code flows at this point we had already resetted the
319             //  maxSeqNumRegistered to rolled over value in the derived implementation
320 
321             if (seqNum > iJitterBufferStats.maxSeqNumRegistered)
322             {
323                 iJitterBufferStats.maxSeqNumReceived = seqNum;
324             }
325             uint32 offset = (seqNum - aSeqNumBase) % iArraySize;
326 
327             PVMFSharedMediaDataPtr currElem = iMediaPtrVec[offset];
328             if (currElem.GetRep() == NULL)
329             {
330                 /* Register Packet */
331                 iMediaPtrVec[offset] = elem;
332                 iNumElems++;
333                 iJitterBufferStats.totalNumPacketsRegistered++;
334                 iJitterBufferStats.lastRegisteredSeqNum = seqNum;
335                 if (seqNum > iJitterBufferStats.maxSeqNumRegistered)
336                 {
337                     iJitterBufferStats.maxSeqNumRegistered = seqNum;
338                     iJitterBufferStats.maxTimeStampRegistered = elem->getTimestamp();
339                 }
340                 iJitterBufferStats.currentOccupancy = iNumElems;
341                 PVMF_JB_LOGDATATRAFFIC_IN((0, "AddElement seqNum %d iNumElems %d", seqNum, iNumElems));
342                 return (oRet);
343             }
344             else if (currElem->getSeqNum() != seqNum)
345             {
346                 PVMF_JB_LOGDATATRAFFIC_IN((0, "[0x%x] JB OW: iReadOffset=%d, iNumElemsLeft=%d, iLastRetrievedSeqNum=%d, old seqNum=%d, new seqNum=%d",
347                                            this,
348                                            iReadOffset,
349                                            iNumElems,
350                                            iLastRetrievedSeqNum,
351                                            seqNum,
352                                            currElem->getSeqNum()));
353                 /* Overwrite existing data */
354                 currElem.Unbind();
355                 /* Register Packet */
356                 iMediaPtrVec[offset] = elem;
357                 iJitterBufferStats.totalNumPacketsRegistered++;
358                 iJitterBufferStats.lastRegisteredSeqNum = seqNum;
359                 if (seqNum > iJitterBufferStats.maxSeqNumRegistered)
360                 {
361                     iJitterBufferStats.maxSeqNumRegistered = seqNum;
362                     iJitterBufferStats.maxTimeStampRegistered = elem->getTimestamp();
363                 }
364                 iJitterBufferStats.currentOccupancy = iNumElems;
365                 oRet = PVMF_JITTER_BUFFER_ADD_ELEM_PACKET_OVERWRITE;
366                 return (oRet);
367             }
368             else
369             {
370                 PVMF_JB_LOGDATATRAFFIC_IN((0, "[0x%x]Duplicate packet iNumElems %d", this, iNumElems));
371             }
372             /* Duplicate Packet - Ignore */
373             return (oRet);
374         }
375 
retrieveElement()376         PVMFSharedMediaDataPtr retrieveElement()
377         {
378             PVMFSharedMediaDataPtr dataPkt;
379             uint32 count = 0;
380             while (dataPkt.GetRep() == NULL)
381             {
382                 /* No data */
383                 if (count > iArraySize)
384                 {
385                     dataPkt.Unbind();
386                     return dataPkt;
387                 }
388                 /* Wrap around */
389                 if (iReadOffset >= iArraySize)
390                 {
391                     iReadOffset = 0;
392                 }
393                 dataPkt = iMediaPtrVec[iReadOffset];
394                 if (dataPkt.GetRep() == NULL)
395                 {
396                     PVMF_JB_LOGDATATRAFFIC_IN_E((0, "[0x%x] Hole in Jb at index %u", this, iReadOffset));
397                 }
398                 iReadOffset++;
399                 count++;
400             }
401 
402             iNumElems--;
403             /* Mark the retrieved element location as free */
404             PVMFSharedMediaDataPtr retElem = iMediaPtrVec[iReadOffset-1];
405             retElem.Unbind();
406             iMediaPtrVec[iReadOffset-1] = retElem;
407             iLastRetrievedSeqNum = (int32)(dataPkt.GetRep()->getSeqNum());
408             /* Check and register packet loss */
409             iJitterBufferStats.totalPacketsLost += (count - 1);
410             iJitterBufferStats.maxTimeStampRetrieved = dataPkt->getTimestamp();
411             iJitterBufferStats.currentOccupancy = iNumElems;
412             iJitterBufferStats.totalNumPacketsRetrieved++;
413             iJitterBufferStats.lastRetrievedSeqNum = iLastRetrievedSeqNum;
414             /* Get packet size */
415             uint32 size = 0;
416             uint32 numFragments = dataPkt->getNumFragments();
417             for (uint32 i = 0; i < numFragments; i++)
418             {
419                 OsclRefCounterMemFrag memFragIn;
420                 dataPkt->getMediaFragment(i, memFragIn);
421                 size += memFragIn.getMemFrag().len;
422             }
423             iJitterBufferStats.packetSizeInBytesLeftInBuffer -= size;
424             PVMF_JB_LOGDATATRAFFIC_OUT((0, "[0x%x]PVMFDynamicCircularArray::retrieveElement: iReadOffset=%d, iNumElemsLeft=%d, SeqNum=%d",
425                                         this,
426                                         iReadOffset,
427                                         iNumElems,
428                                         iLastRetrievedSeqNum));
429 
430             return (dataPkt);
431         }
432 
getStats()433         PVMFJitterBufferStats& getStats()
434         {
435             return iJitterBufferStats;
436         }
437 
getStatsPtr()438         PVMFJitterBufferStats* getStatsPtr()
439         {
440             return &iJitterBufferStats;
441         }
442 
peekNextElementTimeStamp(PVMFTimestamp & aTS,uint32 & aSeqNum)443         void peekNextElementTimeStamp(PVMFTimestamp& aTS,
444                                       uint32& aSeqNum)
445         {
446             PVMFSharedMediaDataPtr dataPkt;
447 
448             uint32 peekOffset = iReadOffset;
449             uint32 count = 0;
450 
451             while (dataPkt.GetRep() == NULL)
452             {
453                 if (count > iNumElems)
454                 {
455                     aTS = 0xFFFFFFFF;
456                 }
457                 if (peekOffset >= iArraySize)
458                 {
459                     peekOffset = 0;
460                 }
461                 dataPkt = iMediaPtrVec[peekOffset];
462                 peekOffset++;
463                 count++;
464             }
465             aTS = dataPkt.GetRep()->getTimestamp();
466             aSeqNum = dataPkt.GetRep()->getSeqNum();
467             return;
468         }
469 
peekMaxElementTimeStamp(PVMFTimestamp & aTS,uint32 & aSeqNum)470         void peekMaxElementTimeStamp(PVMFTimestamp& aTS,
471                                      uint32& aSeqNum)
472         {
473             aTS = iJitterBufferStats.maxTimeStampRegistered;
474             aSeqNum = iJitterBufferStats.maxSeqNumRegistered;
475             return;
476         }
477 
CheckCurrentReadPosition()478         bool CheckCurrentReadPosition()
479         {
480             uint32 offset = iReadOffset;
481 
482             if (offset >= iArraySize)
483             {
484                 offset = 0;
485             }
486 
487             PVMFSharedMediaDataPtr dataPkt =
488                 iMediaPtrVec[offset];
489 
490             if (dataPkt.GetRep() == NULL)
491             {
492                 return false;
493             }
494             return true;
495         }
496 
497         bool CheckSpaceAvailability(uint32 aNumElements = 1)
498         {
499             if ((iArraySize - iNumElems) > aNumElements)
500             {
501                 return true;
502             }
503             return false;
504         }
505 
setSeqNumBase(uint32 aSeqNum)506         void setSeqNumBase(uint32 aSeqNum)
507         {
508             iJitterBufferStats.seqNumBase = aSeqNum;
509         }
510 
511         //setRTPInfoParams replaced by setSeqNumBase
512 
getElementAt(uint32 aIndex)513         PVMFSharedMediaDataPtr getElementAt(uint32 aIndex)
514         {
515             if (aIndex > iArraySize) OSCL_LEAVE(OsclErrArgument);
516             return (iMediaPtrVec[aIndex]);
517         }
518 
AddElementAt(PVMFSharedMediaDataPtr aMediaPtr,uint32 aIndex)519         void AddElementAt(PVMFSharedMediaDataPtr aMediaPtr,
520                           uint32 aIndex)
521         {
522             if (aIndex > iArraySize) OSCL_LEAVE(OsclErrArgument);
523             iMediaPtrVec[aIndex] = aMediaPtr;
524         }
525 
PurgeElementsWithSeqNumsLessThan(uint32 aSeqNum,uint32 aPrevSeqNumBaseOut)526         void PurgeElementsWithSeqNumsLessThan(uint32 aSeqNum, uint32 aPrevSeqNumBaseOut)
527         {
528             PVMF_JB_LOGINFO((0, "PVMFDynamicCircularArray::PurgeElementsWithSeqNumsLessThan SeqNum %d aPrevSeqNumBaseOut %d", aSeqNum, aPrevSeqNumBaseOut));
529             if (!iMediaPtrVec.empty())
530             {
531                 if (aSeqNum < iLastRetrievedSeqNum)
532                 {
533                     typedef typename Oscl_Vector<PVMFSharedMediaDataPtr, Alloc>::iterator iterator_type;
534                     iterator_type it;
535                     for (it = iMediaPtrVec.begin(); it != iMediaPtrVec.end(); it++)
536                     {
537                         if (it->GetRep() != NULL)
538                         {
539                             /* Get packet size */
540                             uint32 size = 0;
541                             uint32 numFragments = it->GetRep()->getNumFragments();
542                             for (uint32 i = 0; i < numFragments; i++)
543                             {
544                                 OsclRefCounterMemFrag memFragIn;
545                                 it->GetRep()->getMediaFragment(i, memFragIn);
546                                 size += memFragIn.getMemFrag().len;
547                             }
548                             iJitterBufferStats.packetSizeInBytesLeftInBuffer -= size;
549                             it->Unbind();
550                         }
551                     }
552                     iNumElems = 0;
553                     /* If after purging all elements, we want to determine the TS of the previous element
554                      * (with DeterminePrevTimeStampPeek()), it will give as false information if the
555                      * seqnum has wrapped around. So because of that, we set aPrevSeqNumBaseOut to be smaller
556                      * than the current seqnum.
557                      */
558                     aPrevSeqNumBaseOut = aSeqNum - 1;
559 
560                 }
561                 else if (aSeqNum > iLastRetrievedSeqNum)
562                 {
563                     /*
564                      * Start from last retrieved seq num and offset it by
565                      * first seq number added
566                      * This guarantees that we deallocate in the allocation
567                      * sequence.
568                      */
569                     uint32 startoffset = ((iLastRetrievedSeqNum - iFirstSeqNumAdded) + 1) % iArraySize;
570 
571                     PVMF_JB_LOGDATATRAFFIC_OUT((0, "[0x%x]PVMFDynamicCircularArray::PurgeElementsWithSeqNumsLessThan:  SeqNum=%d, StartOffset=%d, iArraySize=%d",
572                                                 this,
573                                                 aSeqNum,
574                                                 startoffset,
575                                                 iArraySize));
576 
577                     for (uint32 i = 0; i < aSeqNum - (iLastRetrievedSeqNum + 1); i++)
578                     {
579                         uint32 offset = (startoffset + i) % iArraySize;
580                         /* Mark the retrieved element location as free */
581                         PVMFSharedMediaDataPtr elem = iMediaPtrVec[offset];
582                         if (elem.GetRep() != NULL)
583                         {
584                             if (elem->getSeqNum() < aSeqNum)
585                             {
586                                 /* Get packet size */
587                                 uint32 size = 0;
588                                 uint32 numFragments = elem->getNumFragments();
589                                 for (uint32 i = 0; i < numFragments; i++)
590                                 {
591                                     OsclRefCounterMemFrag memFragIn;
592                                     elem->getMediaFragment(i, memFragIn);
593                                     size += memFragIn.getMemFrag().len;
594                                 }
595                                 iJitterBufferStats.packetSizeInBytesLeftInBuffer -= size;
596                                 elem.Unbind();
597                                 iMediaPtrVec[offset] = elem;
598                                 iNumElems--;
599                             }
600                         }
601                     }
602                 }
603             }
604             /* To prevent us from registering any old packets */
605             iLastRetrievedSeqNum = aSeqNum - 1;
606             iJitterBufferStats.lastRetrievedSeqNum = iLastRetrievedSeqNum;
607             iJitterBufferStats.currentOccupancy = iNumElems;
608             SetReadOffset(aSeqNum);
609         }
610 
PurgeElementsWithTimestampLessThan(PVMFTimestamp aTS)611         void PurgeElementsWithTimestampLessThan(PVMFTimestamp aTS)
612         {
613             if (!iMediaPtrVec.empty())
614             {
615                 while (iNumElems > 0)
616                 {
617                     /* Wrap around */
618                     if (iReadOffset >= iArraySize)
619                     {
620                         iReadOffset = 0;
621                     }
622                     PVMFSharedMediaDataPtr dataPkt = iMediaPtrVec[iReadOffset];
623                     if (dataPkt.GetRep() != NULL)
624                     {
625                         PVMF_JB_LOGDATATRAFFIC_IN((0, "[0x%x]JB Purge:ReadOffset=%d, NumElemsLeft=%d, lastRetrievedSeqNum=%d, seqNum=%d",
626                                                    this,
627                                                    iReadOffset,
628                                                    iNumElems,
629                                                    iLastRetrievedSeqNum,
630                                                    dataPkt->getSeqNum()));
631                         PVMFTimestamp tmpTS = dataPkt.GetRep()->getTimestamp();
632                         if (tmpTS >= aTS)
633                             break;
634 
635                         /* Get packet size */
636                         uint32 size = 0;
637                         uint32 numFragments = dataPkt->getNumFragments();
638                         for (uint32 i = 0; i < numFragments; i++)
639                         {
640                             OsclRefCounterMemFrag memFragIn;
641                             dataPkt->getMediaFragment(i, memFragIn);
642                             size += memFragIn.getMemFrag().len;
643                         }
644                         iJitterBufferStats.packetSizeInBytesLeftInBuffer -= size;
645                         (iMediaPtrVec[iReadOffset]).Unbind();
646                         iNumElems--;
647                     }
648                     iReadOffset++;
649                 }
650             }
651             /* To prevent us from registering any old packets */
652             iLastRetrievedTS = aTS;
653             iJitterBufferStats.currentOccupancy = iNumElems;
654         }
655 
SetReadOffset(uint32 aSeqNum)656         void SetReadOffset(uint32 aSeqNum)
657         {
658             iReadOffset = (aSeqNum - iFirstSeqNumAdded) % iArraySize;
659         }
660 
661     private:
InitVector(uint32 size)662         void InitVector(uint32 size)
663         {
664             for (uint32 i = 0; i < size; i++)
665             {
666                 PVMFSharedMediaDataPtr sharedMediaPtr;
667                 iMediaPtrVec.push_back(sharedMediaPtr);
668             }
669         }
670 
671         uint32 iNumElems;
672         uint32 iArraySize;
673 
674         uint32 iReadOffset;
675         uint32 iLastRetrievedSeqNum;
676         PVMFTimestamp iLastRetrievedTS;
677         uint32 iMaxSeqNumAdded;
678         uint32 iFirstSeqNumAdded;
679 
680         PVMFJitterBufferStats iJitterBufferStats;
681         Oscl_Vector<PVMFSharedMediaDataPtr, Alloc> iMediaPtrVec;
682 
683         PVLogger* ipLogger;
684         PVLogger* ipDataPathLoggerIn;
685         PVLogger* ipDataPathLoggerOut;
686 };
687 
688 class PVMFJitterBuffer;
689 
690 class PVMFJitterBufferObserver
691 {
692     public:
~PVMFJitterBufferObserver()693         virtual ~PVMFJitterBufferObserver() {}
694         /**
695             It is possible that the allocator used for allocating the memory
696             for packetizing the packets may run out of memory.In this case
697             RegisterMediaMsg will return the err code
698             "PVMF_JB_ERR_INSUFFICIENT_MEM_TO_PACKETIZE"
699             In this case user of the JB is expected to make the function call
700             "NotifyFreeSpaceAvailable". When allocator recovers some memory later,
701             then it makes callback to the JB observer using the func
702             "JitterBufferFreeSpaceAvailable"
703             aContext [out]: The value of this will be same as the one provided
704             by the owner of JB while creating the Jitter buffer, retieved via
705             [PVMFJitterBufferConstructParams::GetContextData()] in JB
706         */
707         virtual void JitterBufferFreeSpaceAvailable(OsclAny* aContext) = 0;
708 
709         /**
710             In case of RTSP streaming, SSRC of the session is retrieved
711             from the RTSP SetUp response.This SSRC is set with the JB by
712             its owner/observer using the function call "setSSRC"
713             However, if JB is not provided with the SSRC. Then JB looks thru the
714             RTP packets that are registered with it and establishes the SSRC for
715             the session. This SSRC is communicated to the observer of the JB using
716             the callback MediaTrackSSRCEstablished.
717             aJitterBuffer [out]: Pointer to the jitter buffer.
718             aSSRC[out]: SSRC assiciated with the RTP session.
719         */
720         virtual void MediaTrackSSRCEstablished(PVMFJitterBuffer* aJitterBuffer, uint32 aSSRC) = 0;
721 
722         /**
723             JB uses this callback to notify the the JB user about some info event.
724         */
725         virtual void ProcessJBInfoEvent(PVMFAsyncEvent& aEvent) = 0;
726 
727         /**
728             Callback for the Async function NotifyCanRetrievePacket of the JB.
729         */
730         virtual void PacketReadyToBeRetrieved(OsclAny* aContext) = 0;
731 
732         /**
733             Notifies the user of the JB that EOS packet is provided to the JB.
734         */
735         virtual void EndOfStreamSignalled(OsclAny* aContext) = 0;
736 };
737 
738 class PVMFJitterBuffer
739 {
740     public:
~PVMFJitterBuffer()741         virtual ~PVMFJitterBuffer() {}
742         /**
743             This API will be called when Streaming will be started, i.e. when
744             we are attempting to set up the transport for communication with the server
745             (firewall packet xchange) So, the jitter buffer should be in a state to
746             start accepting the *valid* packets from the server.
747             This API will start/initialize the estimation of the server clock
748             aka estimated server clock.
749             Can Leave: No
750         */
751         virtual void StreamingSessionStarted() = 0;
752 
753         /**
754             This API will be called when Streaming will be paused, i.e. we expect the server to stop
755             sending the data. Because of async nature of the SDK, there may be possibility of receiving
756             some data from the server. So, Jb will continue to accept any valid data.
757             Can Leave: No
758         */
759         virtual void StreamingSessionPaused() = 0;
760 
761         /**
762             This API will be called when Streaming session is considered terminated (from server/client),
763             Jb will not accept any packet from the server after this call and will flush itself.
764             Can Leave: No
765         */
766         virtual void StreamingSessionStopped() = 0;
767 
768         /**
769             To have smooth playback, jitter buffer needs to have sufficient amount of data ahead of the
770             current playback position. As data flows out of the jitter buffer, the occupancy of the jitter
771             buffer will decrease. To order to have sufficient data in it, jitter buffer will go into
772             rebuffering once differnce between the estimated server clock and playback clock becomes
773             less than *RebufferingThreshold*. The value of this "RebufferingThreshold" is
774             initialized/modified by this API.
775             params:
776             aRebufferingThresholdInMilliSeconds:[in] uint32
777             Can Leave: No
778             Constraint:
779             This value ought to be less than "Delay/Duration" specified by "SetDurationInMilliSeconds" API
780         */
781         virtual void SetRebufferingThresholdInMilliSeconds(uint32 aRebufferingThresholdInMilliSeconds) = 0;
782 
783         /**
784             To have smooth playback, before letting any data out of it, jitter buffer accumulates sufficient
785             amount of data ahead of the current playback position.
786             The amoount of data that Jb should acculate is discated by this API.
787             "aDuration" specifies the amount of data(in millisec) Jb should accumuate before letting any data out of it.
788             params:
789             aDuration:[in] uint32
790             Can Leave: No
791             Constraint:
792             This value ought to be more than "RebufferingThreshold" specified by "SetRebufferingThresholdInMilliSeconds" API
793         */
794         virtual void SetDurationInMilliSeconds(uint32 aDuration) = 0;
795 
796         /**
797             This API will reset any EOS flag signalled by the user of the JB.
798             This will reinitialize the streaming segment.
799             Jitter buffer will go into transition state and will remian in that state unless
800             SetPlayRange API is called on it.
801             Jitter buffer will neither accept nor let out any packet out of it.
802             Can Leave: No
803             Constraint: N/A
804         */
805         virtual void PrepareForRepositioning() = 0;
806 
807         /**
808             Provides the observer with the mime type of the data that is persisted in JB.
809         */
810         virtual const char* GetMimeType() const = 0;
811 
812         /**
813             Will let the observer of JB the current state of the JB.
814             Can Leave: No
815             Constraint: N/A
816         */
817         virtual PVMFJitterBufferDataState GetState() const = 0;
818 
819         /**
820             This API will set the state of the Jitter buffer.
821             Param(s):[in] aState
822             Can Leave: No
823             Constraint: N/A
824         */
825         virtual void SetJitterBufferState(PVMFJitterBufferDataState aState) = 0;
826 
827         /**
828             Jitter buffer maintains the pointers to the chunks of the data retrieved from the server.
829             The chunks of memory that are used to persist the data retrieved from the server are allocated
830             by the allocator created in some other component (in socket node for RTSP and the HTTP streaming)
831             Jitter buffer need to know the occupancy of the memory pool for saving itself from overflowing.
832             SetJitterBufferChunkAllocator provides jitter buffer the access to the allocator that allocates
833             chunks for persisting the media.
834             Param(s):[in] aDataBufferAllocator
835             Can Leave: No
836             Constraint: N/A
837         */
838         virtual void SetJitterBufferChunkAllocator(OsclMemPoolResizableAllocator* aDataBufferAllocator) = 0;
839 
840         /**
841             This API provides the info about the memory pool that is allocated for persisting the media
842             from the server.
843             Param(s):[in] uint32 aSize              - Size of memory pool
844                      [in] uint32 aResizeSize        - If Jb needed to be extented, this will specify the mempool
845                                                       chunk that will be added up to the existing mempool.
846                      [in] uint32 aMaxNumResizes     - Specifies the max number of times memory chunks of size
847                                                       "aResizeSize" can be appended to the mempool.
848                      [in] uint32 aExpectedNumberOfBlocksPerBuffer   - Specifies the estimated max number of chunks
849                                                       of media that are expected to be in the Jitter buffer at
850                                                       any instant of time.
851             Can Leave: No
852             Constraint: N/A
853         */
854         virtual void SetJitterBufferMemPoolInfo(uint32 aSize,
855                                                 uint32 aResizeSize,
856                                                 uint32 aMaxNumResizes,
857                                                 uint32 aExpectedNumberOfBlocksPerBuffer) = 0;
858 
859         /**
860             This API provides the let the observer of JB know about the memory pool stats that is allocated
861             for persisting the media received from the server.
862             Param(s):[out] uint32 aSize             - Size of memory pool
863                      [out] uint32 aResizeSize       - If Jb needed to be extented, this will specify the mempool
864                                                       chunk that will be added up to the existing mempool.
865                      [out] uint32 aMaxNumResizes        - Specifies the max number of times memory chunks of size
866                                                       "aResizeSize" can be appended to the mempool.
867                      [out] uint32 aExpectedNumberOfBlocksPerBuffer  - Specifies the estimated max number of chunks
868                                                       of media that are expected to be in the Jitter buffer at
869                                                       any instant of time.
870             Can Leave: No
871             Constraint: N/A
872         */
873         virtual void GetJitterBufferMemPoolInfo(uint32& aSize,
874                                                 uint32& aResizeSize,
875                                                 uint32& aMaxNumResizes,
876                                                 uint32& aExpectedNumberOfBlocksPerBuffer) const  = 0;
877 
878 
879         /**
880             Registers the packet with the jitter buffer.
881             If inplace processing is false, then each fragment in the media msg is treated as a separate packet
882             and is internally wrapped in separate media msg by this functions and persisted in the jitter buffer.
883             This method may fail with the return value PVMF_JITTER_BUFFER_ADD_PKT_INSUFFICIENT_MEMORY
884             In this case, the user of the jitter buffer is expected to make async call NotifyFreeSpaceAvailable
885             call with the jitter buffer and wait for JitterBufferFreeSpaceAvailable callback before
886             requesting to register the packet again.
887             Param(s):[in] PVMFSharedMediaMsgPtr& aMsg - Media msg to be registered with the JB
888             Can Leave: No
889             Constraint: N/A
890         */
891         virtual PVMFJitterBufferRegisterMediaMsgStatus RegisterMediaMsg(PVMFSharedMediaMsgPtr& aMsg) = 0;
892 
893         /**
894             Specifies the initialization of new streaming segement.
895         */
896         virtual bool QueueBOSCommand(uint32 aStreamId) = 0;
897 
898         /**
899             Specifies the termination of the streaming.
900             Param(s):[out] PVMFSharedMediaMsgPtr& aMsg - Cmd Msg signifying EOS
901                      [out] aCmdPacket:  aMsg is considered to be valid if aCmdPacket is true
902             Can Leave: No
903             Constraint: N/A
904         */
905         virtual PVMFStatus GenerateAndSendEOSCommand(PVMFSharedMediaMsgPtr& aMediaOutMsg, bool& aCmdPacket) = 0;
906 
907         /**
908             Specifies if sufficient amount of data is available with the jitter buffer.
909             Delay is considered to be established:
910                 - If estimated server clock is ahead of playback clock by duration specified by
911                 "SetDurationInMilliSeconds"
912                 - or, EOS is signalled by the user of the JB
913                 - and JB is not in *transition* state.
914 
915             Param(s):[out] uint32& aClockDiff - Difference between the estimated server clock and client playback clock
916             Can Leave: No
917             Constraint: N/A
918 
919         */
920         virtual bool IsDelayEstablished(uint32& aClockDiff) = 0;
921 
922         /**
923             This API will provide the user of the Jitter buffer with the media msg from the Jitter buffer.
924             The packet retrieved from Jb can be
925             - media packet
926             - command packet
927             Param(s):[out] PVMFSharedMediaMsgPtr& aMediaMsgPtr - Media data retrieved from the jitter buffer
928                      [out] bool& aCmdPacket - retrieved pkt from Jb is command packet
929             Can Leave: No
930             Constraint: N/A
931             Return value:PVMFStatus [PVMFSuccess/PVMFErrNotReady]
932                     PVMFSuccess: If it is possible to retieve the media msg from the JB
933                     PVMFErrNotReady: If packet cannot be retrieved from the Jb as of now.
934                     User is Jb is expected to requet a callback via NotifyCanRetrievePacket API to get notification
935                     about the readiness of JB to send out the packet.
936         */
937         virtual PVMFStatus RetrievePacket(PVMFSharedMediaMsgPtr& aMediaMsgPtr, bool& aCmdPacket) = 0;
938 
939         virtual PVMFStatus SetInputPacketHeaderPreparsed(bool aPreParsed) = 0;
940 
941         /**
942             Request the JB to signal when user can retrieve the packet from it.
943         */
944         virtual void NotifyCanRetrievePacket() = 0;
945 
946         /**
947             Cancel the previously made request(if any) with the jitter buffer.
948         */
949         virtual void CancelNotifyCanRetrievePacket() = 0;
950 
951 
952         /**
953             Returns the PVMFJitterBufferStats structure.
954         */
955         virtual PVMFJitterBufferStats& getJitterBufferStats() = 0;
956 
957         /**
958             Cleans up the jitter buffer. All the data in the Jb is considered invalid and
959         */
960         virtual void FlushJitterBuffer() = 0;
961 
962         /**
963         */
964         virtual void ResetJitterBuffer() = 0;
965 
966 
967         /**
968             This function returns the mts of the next packet that is expected to be retrieved
969             from the jitter buffer.
970         */
971         virtual PVMFTimestamp peekNextElementTimeStamp() = 0;
972 
973         /**
974 
975         */
976         virtual PVMFTimestamp peekMaxElementTimeStamp() = 0;
977 
978         /**
979             Returns true if there is no media in the JB to be retrieved.
980             else returns false
981         */
982         virtual bool IsEmpty() = 0;
983 
984         /**
985             Signals that EOS is received, and no media data is expected anymore.
986         */
987         virtual void SetEOS(bool aVal) = 0;
988 
989         /**
990             Checks if EOS is received.
991         */
992         virtual bool GetEOS() = 0;
993 
994         /**
995             This function is used to specify the play range, i.e. session params for the
996             current streaming segment
997         */
998         virtual void SetPlayRange(int32 aStartTimeInMS, bool aPlayAfterSeek, bool aStopTimeAvailable = false, int32 aStopTimeInMS = 0) = 0;
999 
1000 
1001         /**
1002         */
1003         virtual void PurgeElementsWithSeqNumsLessThan(uint32 aSeqNum, uint32 aPlayerClockMS) = 0;
1004 
1005         /**
1006         */
1007         virtual void PurgeElementsWithTimestampLessThan(PVMFTimestamp aTS) = 0;
1008 
1009         /**
1010             Sets the mode of processing the input packets.
1011             If "aInPlaceProcessing" boolean if set to true means that media msg wrappers
1012             that carry the RTP packets from upstream node can be reused.
1013             If "aInPlaceProcessing" boolean if set to false, then inside "RegisterPacket"
1014             We allocate a new media msg wrapper and transfer the memory fragments from "inputDataPacket"
1015             to "dataPacket".
1016             If there are multiple media packets in "inputDataPacket" then we have to allocate new
1017             media msg wrappers for each one of these. So "aInPlaceProcessing" cannot be true if
1018             downstream node packs multiple media packets in a single media msg.
1019         */
1020         virtual void SetInPlaceProcessingMode(bool aInPlaceProcessingMode) = 0;
1021 
1022         /**
1023         */
1024         virtual bool addMediaCommand(PVMFSharedMediaMsgPtr& aMediaCmd) = 0;
1025 
1026         /**
1027         */
1028         virtual void SetAdjustedTSInMS(PVMFTimestamp aAdjustedTS) = 0;
1029 
1030         /**
1031         */
1032         virtual void SetBroadCastSession() = 0;
1033 
1034         /**
1035             This function is only implemented for RTSP based streaming to set the source identifier.
1036         */
1037         virtual void setSSRC(uint32 aSSRC) = 0;
1038 
1039         /**
1040             This function is only implemented for RTSP based streaming to get the source identifier.
1041         */
1042         virtual uint32 GetSSRC() const = 0;
1043 
1044         /**
1045             This function is specific to the RTSP based streaming.
1046             This is used to set the RTP info.
1047             RTP info is genreally obtained in response to the play request sent to the 3GPP server
1048         */
1049         /**
1050         */
1051         virtual void AdjustRTPTimeStamp() = 0;
1052 
1053         virtual void setRTPInfoParams(PVMFRTPInfoParams rtpInfoParams, bool oPlayAfterASeek) = 0;
1054 
1055         virtual PVMFRTPInfoParams& GetRTPInfoParams() = 0;
1056 
1057         /**
1058             This functiosn is RTSP streaming specific and is used to detrmine the interarriavl jitter
1059         */
1060         virtual uint32 getInterArrivalJitter() = 0;
1061 
1062         /**
1063         */
1064         virtual bool GetRTPTimeStampOffset(uint32& aTimeStampOffset) = 0;
1065 
1066 
1067         /**
1068         */
1069         virtual void SetRTPTimeStampOffset(uint32 newTSBase) = 0;
1070 
1071         /**
1072         */
1073         virtual PVMFSharedMediaDataPtr& GetFirstDataPacket(void) = 0;
1074         /**
1075             Jitter Buffer persists packets in it as PVMFSharedMediaDataPtr and
1076             each MediaData is expected to include only one packet in it.
1077             If the upstream node is providing multiple packets in the input
1078             media msg while making the function call RegisterMediaMsg, then JB,
1079             while registering packets in it is expected to retrieve packet(s)
1080             from the Input Media Msg, repackage it into the individual media
1081             data msgs and persist it with JB.
1082             It is possible that the allocator used for allocating the memory
1083             for packetizing the packets may run out of memory.In this case
1084             RegisterMediaMsg will return the err code
1085             "PVMF_JB_ERR_INSUFFICIENT_MEM_TO_PACKETIZE"
1086             In this case user of the JB is expected to make the function call
1087             "NotifyFreeSpaceAvailable". This function will set the callback with JB
1088             to notify the JB observer about the availability of the memory with the
1089             allocator.
1090         */
1091         virtual bool NotifyFreeSpaceAvailable() = 0;
1092 
1093         virtual void SetTrackConfig(OsclRefCounterMemFrag& aConfig) = 0;
1094         virtual void SetMediaClockConverter(MediaClockConverter* aConverter) = 0;
1095         virtual void SetTimeScale(uint32 aTimeScale) = 0;
1096         virtual uint32 GetTimeScale() const = 0;
1097         virtual void SetEarlyDecodingTimeInMilliSeconds(uint32 duration) = 0;
1098         virtual void SetBurstThreshold(float burstThreshold) = 0;
1099 };
1100 
1101 ///////////////////////////////////////////////////////////////////////////////
1102 // Contains implementation of functions common to all streaming types
1103 ///////////////////////////////////////////////////////////////////////////////
1104 typedef enum
1105 {
1106     PVMF_JB_ERR_INSUFFICIENT_MEM_TO_PACKETIZE,
1107     PVMF_JB_ERR_INVALID_CONFIGURATION,
1108     PVMF_JB_ERR_CORRUPT_HDR,
1109     PVMF_JB_ERR_TRUNCATED_HDR,
1110     PVMF_JB_ERR_UNEXPECTED_PKT,
1111     PVMF_JB_ERR_LATE_PACKET,
1112     PVMF_JB_ERR_NO_PACKET,
1113     PVMF_JB_PACKET_PARSING_SUCCESS
1114 } PVMFJBPacketParsingAndStatUpdationStatus;
1115 
1116 class PVMFJitterBufferImpl : public PVMFJitterBuffer
1117         , public OsclMemPoolFixedChunkAllocatorObserver
1118         , public PVMFJBEventNotifierObserver
1119 {
1120     public:
1121         OSCL_IMPORT_REF virtual ~PVMFJitterBufferImpl();
1122         OSCL_IMPORT_REF virtual void StreamingSessionStarted();
1123         OSCL_IMPORT_REF virtual void StreamingSessionPaused();
1124         OSCL_IMPORT_REF virtual void StreamingSessionStopped();
1125         OSCL_IMPORT_REF virtual void SetRebufferingThresholdInMilliSeconds(uint32 aRebufferingThresholdInMilliSeconds);
1126         OSCL_IMPORT_REF virtual void SetDurationInMilliSeconds(uint32 aDuration);
1127         OSCL_IMPORT_REF void PrepareForRepositioning();
1128         OSCL_IMPORT_REF PVMFJitterBufferDataState GetState() const;
1129         OSCL_IMPORT_REF virtual void SetJitterBufferState(PVMFJitterBufferDataState aState);
1130         OSCL_IMPORT_REF virtual void SetJitterBufferChunkAllocator(OsclMemPoolResizableAllocator* aDataBufferAllocator);
1131         OSCL_IMPORT_REF virtual const char* GetMimeType() const;
1132         OSCL_IMPORT_REF virtual PVMFJitterBufferRegisterMediaMsgStatus RegisterMediaMsg(PVMFSharedMediaMsgPtr& msg);
1133         OSCL_IMPORT_REF bool QueueBOSCommand(uint32 aStreamId);
1134         OSCL_IMPORT_REF void SetInPlaceProcessingMode(bool aInPlaceProcessingMode);
1135         OSCL_IMPORT_REF PVMFStatus GenerateAndSendEOSCommand(PVMFSharedMediaMsgPtr& aMediaOutMsg, bool& aCmdPacket);
1136         OSCL_IMPORT_REF virtual void NotifyCanRetrievePacket();
1137         OSCL_IMPORT_REF virtual void CancelNotifyCanRetrievePacket();
1138         OSCL_IMPORT_REF virtual PVMFStatus RetrievePacket(PVMFSharedMediaMsgPtr& aMediaMsgPtr, bool& aCmdPacket);
1139         OSCL_IMPORT_REF virtual PVMFStatus SetInputPacketHeaderPreparsed(bool aPreParsed);
1140         OSCL_IMPORT_REF virtual PVMFJitterBufferStats& getJitterBufferStats();
1141         OSCL_IMPORT_REF virtual PVMFTimestamp peekNextElementTimeStamp();
1142         OSCL_IMPORT_REF virtual PVMFTimestamp peekMaxElementTimeStamp();
1143         OSCL_IMPORT_REF bool IsEmpty();
1144         OSCL_IMPORT_REF void SetEOS(bool aVal);
1145         OSCL_IMPORT_REF bool GetEOS();
1146         OSCL_IMPORT_REF void SetPlayRange(int32 aStartTimeInMS, bool aPlayAfterSeek, bool aStopTimeAvailable = false, int32 aStopTimeInMS = 0);
1147         OSCL_IMPORT_REF bool CheckForHighWaterMark();
1148         OSCL_IMPORT_REF bool CheckForLowWaterMark();
1149         OSCL_IMPORT_REF bool CheckNumElements();
1150         OSCL_IMPORT_REF bool addMediaCommand(PVMFSharedMediaMsgPtr& aMediaCmd);
1151         OSCL_IMPORT_REF virtual bool GetPendingCommand(PVMFSharedMediaMsgPtr& aCmdMsg);
1152         OSCL_IMPORT_REF virtual bool HasPendingCommand();
1153         OSCL_IMPORT_REF void SetAdjustedTSInMS(PVMFTimestamp aAdjustedTSInMS);
1154         OSCL_IMPORT_REF virtual void SetBroadCastSession();
1155         OSCL_IMPORT_REF virtual PVMFRTPInfoParams& GetRTPInfoParams();
1156         OSCL_IMPORT_REF bool GetRTPTimeStampOffset(uint32& aTimeStampOffset);
1157         OSCL_IMPORT_REF bool NotifyFreeSpaceAvailable();
1158         OSCL_IMPORT_REF void freechunkavailable(OsclAny*);
1159         OSCL_IMPORT_REF virtual void FlushJitterBuffer();
1160         OSCL_IMPORT_REF virtual void ResetJitterBuffer();
1161         OSCL_IMPORT_REF bool CheckSpaceAvailability(PVMFSharedMediaMsgPtr& aMsg);
1162         OSCL_IMPORT_REF bool CheckSpaceAvailability();
1163         OSCL_IMPORT_REF void setSSRC(uint32 aSSRC);
1164         OSCL_IMPORT_REF uint32 GetSSRC() const;
1165         OSCL_IMPORT_REF virtual void SetJitterBufferMemPoolInfo(uint32 aSize,
1166                 uint32 aResizeSize,
1167                 uint32 aMaxNumResizes,
1168                 uint32 aExpectedNumberOfBlocksPerBuffer);
1169         OSCL_IMPORT_REF void GetJitterBufferMemPoolInfo(uint32& aSize, uint32& aResizeSize, uint32& aMaxNumResizes, uint32& aExpectedNumberOfBlocksPerBuffer) const;
1170         OSCL_IMPORT_REF virtual void SetTrackConfig(OsclRefCounterMemFrag& aConfig);
1171         OSCL_IMPORT_REF virtual void SetMediaClockConverter(MediaClockConverter* aConverter);
1172         OSCL_IMPORT_REF virtual void SetTimeScale(uint32 aTimeScale);
1173         OSCL_IMPORT_REF virtual uint32 GetTimeScale() const ;
1174         OSCL_IMPORT_REF bool IsDelayEstablished(uint32& aClockDiff);
1175     protected:
1176 
1177         OSCL_IMPORT_REF void LogClientAndEstimatedServerClock(PVLogger*& aLogger);
1178         OSCL_IMPORT_REF bool RequestEventCallBack(JB_NOTIFY_CALLBACK aEventType, uint32 aDelay = 0, OsclAny* aContext = NULL);
1179         OSCL_IMPORT_REF void CancelEventCallBack(JB_NOTIFY_CALLBACK aEventType, OsclAny* aContext = NULL);
1180         OSCL_IMPORT_REF bool IsCallbackPending(JB_NOTIFY_CALLBACK aEventType, OsclAny* aContext);
1181         OSCL_IMPORT_REF void ProcessCallback(CLOCK_NOTIFICATION_INTF_TYPE aClockNotificationInterfaceType, uint32 aCallBkId, const OsclAny* aContext, PVMFStatus aStatus);
1182 
1183         OSCL_IMPORT_REF PVMFJitterBufferImpl(const PVMFJitterBufferConstructParams& aJBCreationData);
1184 
1185         typedef enum
1186         {
1187             STREAMINGSTATE_UNKNOWN,
1188             STREAMINGSTATE_STARTED,
1189             STREAMINGSTATE_PAUSED,
1190             STREAMINGSTATE_STOPPED
1191         } StreamingState;
1192 
1193 
1194         virtual bool IsSeqTsValidForPkt(uint32 aSeqNum, uint32 aTs, PVMFJitterBufferStats& jbStats) = 0;
1195         OSCL_IMPORT_REF virtual uint32 GetNumOfPackets(PVMFSharedMediaMsgPtr& aMsg) const;
1196         OSCL_IMPORT_REF virtual void ReportJBInfoEvent(PVMFAsyncEvent& aEvent);
1197         OSCL_IMPORT_REF virtual PVMFSharedMediaDataPtr RetrievePacketPayload();
1198 
1199         OSCL_IMPORT_REF void Construct();
1200         OSCL_IMPORT_REF bool Allocate(OsclSharedPtr<PVMFMediaDataImpl>& mediaDataOut);
1201         OSCL_IMPORT_REF bool CreateMediaData(PVMFSharedMediaDataPtr& dataPacket, OsclSharedPtr<PVMFMediaDataImpl>& mediaDataOut);
1202 
1203         //pure virtuals to be overridden by the derived implementation
1204         virtual PVMFJBPacketParsingAndStatUpdationStatus ParsePacketHeaderAndUpdateJBStats(PVMFSharedMediaDataPtr& inDataPacket,
1205                 PVMFSharedMediaDataPtr& outDataPacket,
1206                 uint32 aFragIndex = 0) = 0;
1207         virtual void EOSCmdReceived() = 0;
1208         virtual void DeterminePrevTimeStampPeek(uint32 aSeqNum,
1209                                                 PVMFTimestamp& aPrevTS) = 0;
1210 
1211         virtual void ComputeMaxAdjustedRTPTS() = 0;
1212         virtual void CheckForRTPTimeAndRTPSeqNumberBase() = 0;
1213         virtual bool CanRetrievePacket(PVMFSharedMediaMsgPtr& aMediaOutMsg, bool& aCmdPacket) = 0;
1214         virtual bool CanRetrievePacket() = 0;
1215         virtual void DeterminePrevTimeStamp(uint32 aSeqNum) = 0;
1216         OSCL_IMPORT_REF virtual PVMFStatus PerformFlowControl(bool aIncomingMedia);
1217         uint32 iSeqNum;
1218         class JitterBufferMemPoolInfo
1219         {
1220             public:
JitterBufferMemPoolInfo()1221                 JitterBufferMemPoolInfo(): iSize(0), iResizeSize(0), iMaxNumResizes(0), iExpectedNumberOfBlocksPerBuffer(0) {}
1222                 void Init(uint32 aSize = 0, uint32 aExpectedNumberOfBlocksPerBuffer = 0, uint32 aResizeSize = 0, uint32 aMaxNumResizes = 0)
1223                 {
1224                     iSize = aSize;
1225                     iResizeSize = aResizeSize;
1226                     iMaxNumResizes = aMaxNumResizes;
1227                     iExpectedNumberOfBlocksPerBuffer = aExpectedNumberOfBlocksPerBuffer;
1228                 }
1229                 uint32 iSize;
1230                 uint32 iResizeSize;
1231                 uint32 iMaxNumResizes;
1232                 uint32 iExpectedNumberOfBlocksPerBuffer;
1233         };
1234         JitterBufferMemPoolInfo iJitterBufferMemPoolInfo;
1235         PVMFSharedMediaDataPtr firstDataPacket;
1236         Oscl_Vector<PVMFSharedMediaDataPtr, OsclMemAllocator> iFirstDataPackets;
1237 
1238         StreamingState    iStreamingState;
1239         bool    iPlayingAfterSeek;
1240         bool    iReportCanRetrievePacket;
1241 
1242         bool    iInPlaceProcessing;
1243         bool    iOnePacketPerFragment;
1244         bool    iOnePacketPerMediaMsg;
1245 
1246         uint32 iLastPacketOutTs;
1247 
1248         bool iOverflowFlag;
1249         /* Media Command related */
1250         Oscl_Vector<MediaCommandMsgHolder, OsclMemAllocator> iMediaCmdVec;
1251         //Allocators [Will be created only when PVMFJitterBufferImpl::iInPlaceProcessing == false]
1252         PVMFMediaFragGroupCombinedAlloc<OsclMemPoolFixedChunkAllocator>* iMediaDataGroupAlloc;
1253         OsclMemPoolFixedChunkAllocator* iMediaDataImplMemPool;
1254         OsclMemPoolFixedChunkAllocator* iMediaMsgMemPool;
1255 
1256         int32 iStartTimeInMS;
1257         int32 iStopTimeInMS;
1258         bool iPlayStopTimeAvailable;
1259         bool    iBroadCastSession;
1260 
1261         PVMFTimestamp iMaxAdjustedRTPTS;
1262 
1263         bool    iSessionDurationExpired;
1264         uint32  iDurationInMilliSeconds;
1265         uint32  iRebufferingThresholdInMilliSeconds;
1266 
1267         uint64 iMonotonicTimeStamp;
1268         uint32 iFirstSeqNum;
1269         typedef PVMFDynamicCircularArray<OsclMemAllocator> PVMFDynamicCircularArrayType;
1270         PVMFDynamicCircularArrayType* iJitterBuffer;
1271         Oscl_Vector<PVMFRTPInfoParams, OsclMemAllocator> iRTPInfoParamsVec;
1272         bool    iEOSSignalled;
1273         bool    iEOSSent;
1274         uint32 iStreamID;
1275         PVMFTimestamp iMaxAdjustedTS; //[iMaxAdjustedRTPTS]
1276         PVMFTimestamp iPrevAdjustedTS;//[iPrevAdjustedRTPTS]
1277         PVMFMediaClock& irEstimatedServerClock;
1278         PVMFMediaClock& irClientPlayBackClock;
1279         PVMFJBEventNotifier& irJBEventNotifier;
1280         OSCL_HeapString<OsclMemAllocator> irMimeType;
1281         bool&   irDelayEstablished;
1282         int&    irJitterDelayPercent;
1283         PVMFJitterBufferDataState&  irDataState;
1284         bool iInProcessingMode;
1285         bool iHeaderPreParsed;
1286 
1287         uint32 iRTPTimeScale;
1288         PVMFTimestamp  iPrevTSOut;
1289         MediaClockConverter iEstServClockMediaClockConvertor;
1290         PVMFJitterBufferObserver* const iObserver;
1291         OsclAny* const iObserverContext;
1292 
1293         bool   seqNumLock;
1294 
1295         uint32 iInterArrivalJitter;
1296         bool   oFirstPacket;
1297 
1298         OsclRefCounterMemFrag       iTrackConfig;
1299         uint32 iTimeScale;
1300         MediaClockConverter*        ipMediaClockConverter;
1301 
1302         uint32 SSRCLock;
1303         bool   oSSRCFromSetUpResponseAvailable;
1304         uint32 SSRCFromSetUpResponse;
1305 
1306         uint32 iPrevSeqNumBaseOut;
1307         PVMFTimestamp seqLockTimeStamp;
1308 
1309         PVMFTimestamp iPrevAdjustedRTPTS;
1310         PVMFTimestamp iPrevTSIn;
1311         uint32 iPrevSeqNumBaseIn;
1312 
1313         OsclMemPoolResizableAllocator* iBufferAlloc;
1314         uint32 prevMinPercentOccupancy;
1315         uint32 consecutiveLowBufferCount;
1316         uint32 iNumUnderFlow;
1317 
1318         bool   iMonitorReBufferingCallBkPending;
1319         bool   iWaitForOOOPacketCallBkPending;
1320         bool   iJitterBufferDurationCallBkPending;
1321 
1322         uint32 iWaitForOOOPacketCallBkId;
1323         uint32 iMonitorReBufferingCallBkId;
1324         uint32 iJitterBufferDurationCallBkId;
1325 
1326         PVLogger* ipLogger;
1327         PVLogger* ipClockLoggerSessionDuration;
1328         PVLogger* ipDataPathLogger;
1329         PVLogger* ipMaxRTPTsLogger;
1330         PVLogger* ipDataPathLoggerIn;
1331         PVLogger* ipDataPathLoggerOut;
1332         PVLogger* ipClockLogger;
1333         PVLogger* ipClockLoggerRebuff;
1334         PVLogger* ipDataPathLoggerFlowCtrl;
1335         PVLogger* ipJBEventsClockLogger;
1336         PVLogger* ipRTCPDataPathLoggerIn;
1337         PVLogger* ipRTCPDataPathLoggerOut;
1338     private:
1339         void CreateAllocators();
1340         void DestroyAllocators();
1341         PVMFJitterBufferRegisterMediaMsgStatus AddPacket(PVMFSharedMediaDataPtr& aDataPacket);
1342         OSCL_IMPORT_REF virtual PVMFJitterBufferRegisterMediaMsgStatus RegisterCmdPacket(PVMFSharedMediaMsgPtr& aMediaCmd);
1343         OSCL_IMPORT_REF virtual bool CanRegisterMediaMsg();
1344         PVMFJitterBufferRegisterMediaMsgStatus RegisterDataPacket(PVMFSharedMediaDataPtr& aDataPacket);
1345         void ResetParams(bool aReleaseMemory = true);
1346         void HandleEvent_MonitorReBuffering(const OsclAny* aContext);
1347         void HandleEvent_NotifyWaitForOOOPacketComplete(const OsclAny* aContext);
1348         void HandleEvent_JitterBufferBufferingDurationComplete();
1349 };
1350 
1351 #endif
1352