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_rtp_jitter_buffer_impl.h"
20 #endif
21
22 #ifndef OSCL_BYTE_ORDER_H_INCLUDED
23 #include "oscl_byte_order.h"
24 #endif
25
26 #ifndef PVMF_SM_NODE_EVENTS_H_INCLUDED
27 #include "pvmf_sm_node_events.h"
28 #endif
29
30
31 #ifndef PVMF_MEDIA_CMD_H_INCLUDED
32 #include "pvmf_media_cmd.h"
33 #endif
34
35 // Define entry point for this DLL
36 OSCL_DLL_ENTRY_POINT_DEFAULT()
37
38 #define PVMF_JITTER_BUFFER_ROLL_OVER_THRESHOLD_16BIT 2000
39
40 const int32 iEstimatedServerKeepAheadInMilliSeconds = 2000;
41
42 /* RTP HEADER CONSTANTS */
43 #define SUPPORTED_RTP_HEADER_VERSION 2
44 #define RTP_FIXED_HEADER_SIZE 12
45 #define RTP_HEADER_V_BIT_MASK 0xC0
46 #define RTP_HEADER_V_BIT_OFFSET 6
47 #define RTP_HEADER_P_BIT_MASK 0x20
48 #define RTP_HEADER_P_BIT_OFFSET 5
49 #define RTP_HEADER_X_BIT_MASK 0x10
50 #define RTP_HEADER_X_BIT_OFFSET 4
51 #define RTP_HEADER_CC_BIT_MASK 0x0F
52 #define RTP_HEADER_M_BIT_MASK 0x80
53 #define RTP_HEADER_M_BIT_OFFSET 7
54 #define RTP_HEADER_PT_MASK 0x7F
55
New(const PVMFJitterBufferConstructParams & aCreationData)56 OSCL_EXPORT_REF PVMFJitterBuffer* PVMFRTPJitterBufferImpl::New(const PVMFJitterBufferConstructParams& aCreationData)
57 {
58 PVMFRTPJitterBufferImpl* ptr = OSCL_NEW(PVMFRTPJitterBufferImpl, (aCreationData));
59 if (ptr)
60 {
61 ptr->Construct();
62 }
63 return ptr;
64 }
65
~PVMFRTPJitterBufferImpl()66 OSCL_EXPORT_REF PVMFRTPJitterBufferImpl::~PVMFRTPJitterBufferImpl()
67 {
68 if (iPacketArrivalClock)
69 {
70 iPacketArrivalClock->Stop();
71 OSCL_DELETE(iPacketArrivalClock);
72 }
73
74 if (iBurstClock)
75 {
76 iBurstClock->Stop();
77 OSCL_DELETE(iBurstClock);
78 }
79 }
80
StreamingSessionStarted()81 OSCL_EXPORT_REF void PVMFRTPJitterBufferImpl::StreamingSessionStarted()
82 {
83 if (!iEOSSignalled)
84 {
85 irEstimatedServerClock.Start();
86 iBurstClock->Start();
87 CancelEventCallBack(JB_BUFFERING_DURATION_COMPLETE);
88 RequestEventCallBack(JB_BUFFERING_DURATION_COMPLETE);
89 }
90 PVMFJitterBufferImpl::StreamingSessionStarted();
91 }
92
PVMFRTPJitterBufferImpl(const PVMFJitterBufferConstructParams & aCreationData)93 PVMFRTPJitterBufferImpl::PVMFRTPJitterBufferImpl(const PVMFJitterBufferConstructParams& aCreationData): PVMFJitterBufferImpl(aCreationData)
94 {
95 if (iInPlaceProcessing)
96 {
97 iOnePacketPerMediaMsg = true;
98 }
99 else
100 {
101 iOnePacketPerMediaMsg = false;
102 }
103
104 iOnePacketPerFragment = true;
105 iHeaderPreParsed = false;
106
107 seqNumLock = false;
108 iPrevPacketTS = 0;
109 isPrevNptTimeSet = false;
110 iPrevNptTimeInRTPTimeScale = 0;
111 iInterArrivalJitterD = 0;
112 iPrevPacketRecvTime = 0;
113 isPrevRtpTimeSet = false;
114 iPrevRtpTimeBase = 0;
115 iRTPTimeScale = 0;
116 iOverflowFlag = false;
117
118 iPlayListRTPTimeBase = 0;
119 iPlayListRTPTimeBaseSet = false;
120
121 iBurstDetect = false;
122 iBurstStartTimestamp = 0;
123 iEstServerClockBurstStartTimestamp = 0;
124 iRTPDataArrived = false;
125 iEarlyDecodingTime = 0;
126 iServerBurst = false;
127 iBurstThreshold = 1.5;
128 iBurstDetectDurationInMilliSec = 0;
129 iInitialBuffering = true;
130 iBurstClock = NULL;
131 }
132
Construct()133 void PVMFRTPJitterBufferImpl::Construct()
134 {
135 iPacketArrivalClock = OSCL_NEW(PVMFMediaClock, ());
136 iPacketArrivalClock->SetClockTimebase(iPacketArrivalTimeBase);
137 iPacketArrivalClock->Start();
138 PVMFJitterBufferImpl::Construct();
139
140 iBurstClock = OSCL_NEW(PVMFMediaClock, ());
141 iBurstClock->SetClockTimebase(iBurstClockTimeBase);
142
143 bool overflowFlag = false;
144 uint32 start = 0;
145 iBurstClock->Stop();
146 iBurstClock->SetStartTime32(start, PVMF_MEDIA_CLOCK_MSEC, overflowFlag);
147 }
148
ResetJitterBuffer()149 OSCL_EXPORT_REF void PVMFRTPJitterBufferImpl::ResetJitterBuffer()
150 {
151 if (iInPlaceProcessing)
152 {
153 iOnePacketPerMediaMsg = true;
154 }
155 else
156 {
157 iOnePacketPerMediaMsg = false;
158 }
159
160 iOnePacketPerFragment = true;
161 iHeaderPreParsed = false;
162
163 seqNumLock = false;
164 iPrevPacketTS = 0;
165 isPrevNptTimeSet = false;
166 iPrevNptTimeInRTPTimeScale = 0;
167 iInterArrivalJitterD = 0;
168 iPrevPacketRecvTime = 0;
169 isPrevRtpTimeSet = false;
170 iPrevRtpTimeBase = 0;
171 iRTPTimeScale = 0;
172
173 iPlayListRTPTimeBaseSet = false;
174 iPlayListRTPTimeBase = 0;
175
176 iOverflowFlag = false;
177
178 iBurstDetect = false;
179 iBurstStartTimestamp = 0;
180 iEstServerClockBurstStartTimestamp = 0;
181 iRTPDataArrived = false;
182 iEarlyDecodingTime = 0;
183 iServerBurst = false;
184 iBurstThreshold = 1.5;
185 iBurstDetectDurationInMilliSec = 0;
186 iInitialBuffering = true;
187
188 if (iPacketArrivalClock)
189 {
190 iPacketArrivalClock->Reset();
191 }
192
193 if (iBurstClock)
194 {
195 iBurstClock->Reset();
196 }
197
198 PVMFJitterBufferImpl::ResetJitterBuffer();
199 }
200
201 ///////////////////////////////////////////////////////////////////////////////
202 ///////////////////////////////////////////////////////////////////////////////
AdjustRTPTimeStamp()203 OSCL_EXPORT_REF void PVMFRTPJitterBufferImpl::AdjustRTPTimeStamp()
204 {
205 // By the time this function is called, iMonotonicTimeStamp should be already normalized
206 // with the corresponding values from other jitterbuffers
207 iMaxAdjustedRTPTS = Oscl_Int64_Utils::get_uint64_lower32(iMonotonicTimeStamp);
208 UpdateEstimatedServerClock(true);
209 }
210
setRTPInfoParams(PVMFRTPInfoParams rtpInfo,bool oPlayAfterASeek)211 OSCL_EXPORT_REF void PVMFRTPJitterBufferImpl::setRTPInfoParams(PVMFRTPInfoParams rtpInfo, bool oPlayAfterASeek)
212 {
213 iJitterBuffer->setSeqNumBase(rtpInfo.seqNum);
214 PVMFRTPInfoParams iRTPInfoParams;
215 iRTPInfoParams.seqNumBaseSet = rtpInfo.seqNumBaseSet;
216 if (rtpInfo.seqNumBaseSet == true)
217 {
218 iRTPInfoParams.seqNum = rtpInfo.seqNum;
219 }
220 iRTPInfoParams.rtpTimeBaseSet = rtpInfo.rtpTimeBaseSet;
221 if (rtpInfo.rtpTimeBaseSet == true)
222 {
223 iRTPInfoParams.rtpTime = rtpInfo.rtpTime;
224 }
225 iRTPInfoParams.rtpTimeScale = rtpInfo.rtpTimeScale;
226 iRTPTimeScale = rtpInfo.rtpTimeScale;
227 iEstServClockMediaClockConvertor.set_timescale(iRTPTimeScale);
228 iMediaClockConvertor.set_timescale(1000);
229 iMediaClockConvertor.set_clock_other_timescale(0, iRTPInfoParams.rtpTimeScale);
230 iRTPInfoParams.nptTimeBaseSet = rtpInfo.nptTimeBaseSet;
231 if (iRTPInfoParams.nptTimeBaseSet == true)
232 {
233 iRTPInfoParams.nptTimeInMS = rtpInfo.nptTimeInMS;
234 iMediaClockConvertor.update_clock(iRTPInfoParams.nptTimeInMS);
235 iRTPInfoParams.nptTimeInRTPTimeScale =
236 iMediaClockConvertor.get_converted_ts(iRTPInfoParams.rtpTimeScale);
237 }
238 /* In case this is the first rtp info set TS calc variables */
239 if (iRTPInfoParamsVec.size() == 0)
240 {
241 if (iRTPInfoParams.rtpTimeBaseSet)
242 {
243 iPrevTSOut = iRTPInfoParams.rtpTime;
244 iPrevTSIn = iRTPInfoParams.rtpTime;
245 iPrevAdjustedRTPTS = iRTPInfoParams.rtpTime;
246 if (iPlayListRTPTimeBaseSet == false)
247 {
248 iPlayListRTPTimeBaseSet = true;
249 iPlayListRTPTimeBase = iRTPInfoParams.rtpTime;
250 }
251
252 }
253 else
254 {
255 /* Use the value from the first packet */
256 if (seqNumLock)
257 {
258 iPrevTSOut = seqLockTimeStamp;
259 iPrevTSIn = seqLockTimeStamp;
260 iPrevAdjustedRTPTS = seqLockTimeStamp;
261 }
262 }
263
264 if (iRTPInfoParams.seqNumBaseSet)
265 {
266 iPrevSeqNumBaseOut = iRTPInfoParams.seqNum;
267 iPrevSeqNumBaseIn = iRTPInfoParams.seqNum;
268 }
269 else
270 {
271 /* Use the value from the first packet */
272 if (seqNumLock)
273 {
274 iPrevSeqNumBaseOut = iFirstSeqNum;
275 iPrevSeqNumBaseIn = iFirstSeqNum;
276 }
277 }
278 /* Initialize the variables used for ts calculation between pause and resume */
279 if (iRTPInfoParams.nptTimeBaseSet)
280 {
281 iPrevNptTimeInRTPTimeScale = iRTPInfoParams.nptTimeInRTPTimeScale;
282 isPrevNptTimeSet = true;
283 }
284 if (iRTPInfoParams.rtpTimeBaseSet)
285 {
286 iPrevRtpTimeBase = iRTPInfoParams.rtpTime;
287 isPrevRtpTimeSet = true;
288 }
289 }
290 else
291 {
292 iRTPInfoParams.isPlayAfterPause = !oPlayAfterASeek;
293 }
294 if (iRTPInfoParams.rtpTimeBaseSet)
295 {
296 iPrevAdjustedRTPTS = iRTPInfoParams.rtpTime;
297 }
298
299 if (oPlayAfterASeek)
300 {
301 //Clean everything in the vector before pushing new RTPInfo
302 iRTPInfoParamsVec.clear();
303 }
304
305 iRTPInfoParamsVec.push_back(iRTPInfoParams);
306 }
307
getInterArrivalJitter()308 OSCL_EXPORT_REF uint32 PVMFRTPJitterBufferImpl::getInterArrivalJitter()
309 {
310 return iInterArrivalJitter;
311 }
312
313 OSCL_EXPORT_REF void
PurgeElementsWithSeqNumsLessThan(uint32 aSeqNum,uint32 aPlayerClockMS)314 PVMFRTPJitterBufferImpl::PurgeElementsWithSeqNumsLessThan(uint32 aSeqNum,
315 uint32 aPlayerClockMS)
316 {
317 iJitterBuffer->PurgeElementsWithSeqNumsLessThan(aSeqNum, iPrevSeqNumBaseOut);
318
319 {
320 iMaxAdjustedRTPTS =
321 Oscl_Int64_Utils::get_uint64_lower32(iMonotonicTimeStamp);
322 /*
323 * In case of 3GPP streaming this clock adjustment is performed
324 * at a later point, via the "AdjustRTPTimeStamp" API call from
325 * jitter buffer node.
326 */
327 OSCL_UNUSED_ARG(aPlayerClockMS);
328 }
329
330 PVMF_JB_LOGDATATRAFFIC_IN((0, "PVMFJitterBufferImpl::PurgeElementsWithSeqNumsLessThan - SeqNum=%d",
331 aSeqNum));
332 }
333
334 OSCL_EXPORT_REF void
PurgeElementsWithTimestampLessThan(PVMFTimestamp aTS)335 PVMFRTPJitterBufferImpl::PurgeElementsWithTimestampLessThan(PVMFTimestamp aTS)
336 {
337 PVMFTimestamp rtpTS;
338 if (iPlayListRTPTimeBaseSet)
339 {
340 rtpTS = iPlayListRTPTimeBase + aTS;
341 }
342 else
343 {
344 rtpTS = aTS;
345 }
346
347 iJitterBuffer->PurgeElementsWithTimestampLessThan(rtpTS);
348 iMaxAdjustedRTPTS = aTS;
349 //iPrevAdjustedRTPTS = rtpTS;
350 UpdateEstimatedServerClock(true);
351 iMonotonicTimeStamp = aTS;
352 iPrevTSOut = rtpTS;
353 PVMF_JB_LOGDATATRAFFIC_IN((0, "PVMFJitterBufferImpl::PurgeElementsWithTimestampLessThan - ntpTS=%u, rtpTS=%u",
354 aTS, rtpTS));
355 }
356
357 /**
358 */
GetFirstDataPacket(void)359 OSCL_EXPORT_REF PVMFSharedMediaDataPtr& PVMFRTPJitterBufferImpl::GetFirstDataPacket(void)
360 {
361 return firstDataPacket;
362 }
363
364 /**
365 */
GetRTPTimeStampOffset(uint32 & aTimeStampOffset)366 OSCL_EXPORT_REF bool PVMFRTPJitterBufferImpl::GetRTPTimeStampOffset(uint32& aTimeStampOffset)
367 {
368 if (seqNumLock)
369 aTimeStampOffset = seqLockTimeStamp;
370
371 return seqNumLock;
372 }
373
374 /**
375 */
SetRTPTimeStampOffset(uint32 newTSBase)376 OSCL_EXPORT_REF void PVMFRTPJitterBufferImpl::SetRTPTimeStampOffset(uint32 newTSBase)
377 {
378 // This function must be used only to offset the RTP TB
379 // for broadcast streaming.
380 // Based on that, the following is assumed:
381 // 1) seqLockTimeStamp is valid
382 // 2) newTSBase <= seqLockTimeStamp
383
384 iMonotonicTimeStamp += (seqLockTimeStamp - newTSBase);
385 iMaxAdjustedRTPTS += (seqLockTimeStamp - newTSBase);
386 }
387
388
389
390 /**
391 */
NotifyFreeSpaceAvailable()392 OSCL_EXPORT_REF bool PVMFRTPJitterBufferImpl::NotifyFreeSpaceAvailable()
393 {
394
395 if (iMediaDataGroupAlloc)
396 {
397 iMediaDataGroupAlloc->notifyfreechunkavailable(*this);
398 return true;
399 }
400 return false;
401 }
402
SetEarlyDecodingTimeInMilliSeconds(uint32 aDuration)403 void PVMFRTPJitterBufferImpl::SetEarlyDecodingTimeInMilliSeconds(uint32 aDuration)
404 {
405 if (aDuration > 0)
406 {
407 iBurstDetect = true;
408 iEarlyDecodingTime = aDuration;
409 iBurstDetectDurationInMilliSec = (aDuration / 2);
410 }
411 else
412 {
413 iBurstDetect = false;
414 iEarlyDecodingTime = 0;
415 iBurstDetectDurationInMilliSec = 0;
416 }
417 }
418
SetBurstThreshold(float aBurstThreshold)419 void PVMFRTPJitterBufferImpl::SetBurstThreshold(float aBurstThreshold)
420 {
421 iBurstThreshold = aBurstThreshold;
422 }
423
424
IsDelayEstablished(uint32 & aClockDiff)425 bool PVMFRTPJitterBufferImpl::IsDelayEstablished(uint32& aClockDiff)
426 {
427 aClockDiff = iDurationInMilliSeconds;
428 if (GetState() == PVMF_JITTER_BUFFER_IN_TRANSITION)
429 {
430 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Jitter Buffer In Transition - Preparing for Seek"));
431 irDelayEstablished = false;
432 irJitterDelayPercent = 0;
433 return irDelayEstablished;
434 }
435
436 uint32 timebase32 = 0;
437 uint32 estServerClock = 0;
438 uint32 clientClock = 0;
439 bool overflowFlag = false;
440
441 irEstimatedServerClock.GetCurrentTime32(estServerClock, overflowFlag, PVMF_MEDIA_CLOCK_MSEC, timebase32);
442 irClientPlayBackClock.GetCurrentTime32(clientClock, overflowFlag, PVMF_MEDIA_CLOCK_MSEC, timebase32);
443
444 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Check - EstServClock=%d", estServerClock));
445 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Check - ClientClock=%d", clientClock));
446
447 if (iEOSSignalled)
448 {
449 /*
450 * No check needed - We are past the clip time, just play out the last
451 * bit in the jitter buffer
452 */
453 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Session Duration Expired"));
454 if (irDelayEstablished == false)
455 {
456 /*
457 * Coming out of rebuffering in case we had gone into
458 * rebuffering just before
459 */
460 irJitterDelayPercent = 100;
461 PVMFAsyncEvent jbEvent(PVMFInfoEvent, PVMFInfoDataReady, NULL, NULL);
462 ReportJBInfoEvent(jbEvent);
463 }
464 irDelayEstablished = true;
465 }
466 else
467 {
468 uint32 diff32ms = 0;
469 bool isEarly = PVTimeComparisonUtils::IsEarlier(estServerClock, clientClock, diff32ms);
470 if (isEarly && diff32ms > 0)
471 {
472 /* Could happen during repositioning */
473 if (irDelayEstablished == true)
474 {
475 aClockDiff = 0;
476 irDelayEstablished = false;
477 irJitterDelayPercent = 0;
478 /* Start timer */
479 PVMFAsyncEvent jbEvent(PVMFInfoEvent, PVMFInfoUnderflow, NULL, NULL);
480 ReportJBInfoEvent(jbEvent);
481 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Check - EstServClock=%d",
482 Oscl_Int64_Utils::get_uint64_lower32(estServerClock)));
483 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Check - ClientClock=%d",
484 Oscl_Int64_Utils::get_uint64_lower32(clientClock)));
485 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Estimated Serv Clock Less Than ClientClock!!!!"));
486 PVMF_JB_LOGCLOCK_REBUFF((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Check - EstServClock=%d",
487 Oscl_Int64_Utils::get_uint64_lower32(estServerClock)));
488 PVMF_JB_LOGCLOCK_REBUFF((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Check - ClientClock=%d",
489 Oscl_Int64_Utils::get_uint64_lower32(clientClock)));
490 PVMF_JB_LOGCLOCK_REBUFF((0, "PVMFJitterBufferNode::IsDelayEstablished - Estimated Serv Clock Less Than ClientClock!!!!"));
491 }
492 return irDelayEstablished;
493 }
494 aClockDiff = diff32ms;
495
496 // Make sure RTP packets have arrived before starting burst measurement
497 if (iRTPDataArrived && iBurstDetect)
498 {
499 uint32 timebase32 = 0;
500 uint32 aCurrentRealTime = 0;
501 uint32 aRealTimeDelta = 0;
502 uint32 aServerClientClockDelta = 0;
503 float aBurstRateSrvClnt = 0;
504 bool overFlowFlagBurst;
505
506 // Save burst start time only if its current value is 0
507 if (iBurstStartTimestamp == 0)
508 {
509 iBurstClock->GetCurrentTime32(iBurstStartTimestamp, overFlowFlagBurst, PVMF_MEDIA_CLOCK_MSEC, timebase32);
510 iEstServerClockBurstStartTimestamp = estServerClock;
511 }
512
513 // Find current real time
514 iBurstClock->GetCurrentTime32(aCurrentRealTime, overFlowFlagBurst, PVMF_MEDIA_CLOCK_MSEC, timebase32);
515
516 // Compute real time clk delta and est srv clk delta
517 aRealTimeDelta = aCurrentRealTime - iBurstStartTimestamp;
518
519 if (aRealTimeDelta >= iBurstDetectDurationInMilliSec)
520 {
521 // Est srv clk - client clk is used to calculate burst rate
522 if (!iInitialBuffering)
523 {
524 aServerClientClockDelta = estServerClock - iEstServerClockBurstStartTimestamp;
525 }
526 else
527 {
528 aServerClientClockDelta = diff32ms;
529 }
530
531 // Compute burst rate
532 if (aRealTimeDelta)
533 {
534 aBurstRateSrvClnt = (float)(aServerClientClockDelta) /
535 (float)(aRealTimeDelta);
536 }
537
538 iBurstDetect = false;
539 if (aBurstRateSrvClnt > iBurstThreshold)
540 {
541 iServerBurst = true;
542 }
543 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Real time clk:: ref val: %2d,", iBurstStartTimestamp));
544 if (iInitialBuffering)
545 {
546 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - client clk:: ref val: %d", clientClock));
547 }
548 else
549 {
550 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - est srv:: ref val: %d", iEstServerClockBurstStartTimestamp));
551 }
552 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - real time clk:: cur val: %2d", aCurrentRealTime));
553 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - est srv clk:: cur val: %2d", estServerClock));
554 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - real time delta: %2d", aRealTimeDelta));
555 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - est srv client clk delta: %2d", aServerClientClockDelta));
556 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - burst rate * 100: %d", (uint32)(100.0 * aBurstRateSrvClnt)));
557 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - burst threshold * 100: %d", (uint32)(100.0 * iBurstThreshold)));
558 }
559 }
560 // Check if SM has set the early decoding flag. If true, and if burst rate exceeds threshold,
561 // signal buffering completion.
562 if (iServerBurst && iEarlyDecodingTime && (diff32ms >= iEarlyDecodingTime))
563 {
564 iServerBurst = false;
565 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Cancelling Jitter Buffer Duration Timer"));
566 irDelayEstablished = true;
567 irJitterDelayPercent = 100;
568 PVMFAsyncEvent jbEvent(PVMFInfoEvent, PVMFInfoDataReady, NULL, NULL);
569 ReportJBInfoEvent(jbEvent);
570 }
571 else
572 {
573 if (diff32ms >= iDurationInMilliSeconds)
574 {
575 if (iBufferAlloc)
576 {
577 uint32 jbSize = iBufferAlloc->getBufferSize();
578 uint32 largestContiguousFreeBlockSize = iBufferAlloc->getLargestContiguousFreeBlockSize();
579 uint32 minPercentOccupancy = 100;
580 if ((largestContiguousFreeBlockSize*100 / jbSize) < minPercentOccupancy)
581 {
582 minPercentOccupancy = (uint32)(largestContiguousFreeBlockSize * 100 / jbSize);
583 }
584
585 if ((prevMinPercentOccupancy < MIN_PERCENT_OCCUPANCY_THRESHOLD) && (minPercentOccupancy < MIN_PERCENT_OCCUPANCY_THRESHOLD))
586 {
587 consecutiveLowBufferCount++;
588 }
589 else
590 {
591 consecutiveLowBufferCount = 0;
592 }
593
594 prevMinPercentOccupancy = minPercentOccupancy;
595 PVMF_JB_LOGCLOCK_REBUFF((0, "PVMFJitterBufferNode::IsDelayEstablished - minPercentOccupancy=%d, consecutiveLowBufferCount=%d jbSize%d largestContiguousFreeBlockSize %d",
596 minPercentOccupancy,
597 consecutiveLowBufferCount, jbSize, largestContiguousFreeBlockSize));
598
599
600 if ((diff32ms > JITTER_BUFFER_DURATION_MULTIPLIER_THRESHOLD*iDurationInMilliSeconds) && !iOverflowFlag && (consecutiveLowBufferCount > CONSECUTIVE_LOW_BUFFER_COUNT_THRESHOLD))
601 {
602 iOverflowFlag = true;
603 PVMFAsyncEvent jbEvent(PVMFInfoEvent, PVMFInfoOverflow, NULL, NULL);
604 ReportJBInfoEvent(jbEvent);
605 PVMF_JB_LOGCLOCK_REBUFF((0, "PVMFJitterBufferNode::IsDelayEstablished - minPercentOccupancy=%d, consecutiveLowBufferCount=%d, diff32ms = %d, largestContiguousFreeBlockSize = %d",
606 minPercentOccupancy,
607 consecutiveLowBufferCount, diff32ms, largestContiguousFreeBlockSize));
608 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Established - EstServClock=%d", estServerClock));
609 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Established - ClientClock=%d", clientClock));
610 PVMF_JB_LOGCLOCK_REBUFF((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Established - EstServClock=%d",
611 estServerClock));
612 PVMF_JB_LOGCLOCK_REBUFF((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Established - ClientClock=%d",
613 clientClock));
614 }
615 }
616 if (irDelayEstablished == false)
617 {
618 if (CheckNumElements())
619 {
620 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Cancelling Jitter Buffer Duration Timer"));
621 irDelayEstablished = true;
622 irJitterDelayPercent = 100;
623 PVMFAsyncEvent jbEvent(PVMFInfoEvent, PVMFInfoDataReady, NULL, NULL);
624 ReportJBInfoEvent(jbEvent);
625 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Established - EstServClock=%d", estServerClock));
626 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Established - ClientClock=%d", clientClock));
627 PVMF_JB_LOGCLOCK_REBUFF((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Established - EstServClock=%d",
628 estServerClock));
629 PVMF_JB_LOGCLOCK_REBUFF((0, "PVMFJitterBufferNode::IsDelayEstablished - Time Delay Established - ClientClock=%d",
630 clientClock));
631 }
632 else
633 {
634 irJitterDelayPercent = 0;
635 }
636 }
637 else
638 {
639 irJitterDelayPercent = 100;
640 }
641 }
642 else
643 {
644 /*
645 * Update the buffering percent - to be used while sending buffering
646 * status events, in case we go into rebuffering or if we are in buffering
647 * state.
648 */
649 irJitterDelayPercent = ((diff32ms * 100) / iDurationInMilliSeconds);
650 if (irDelayEstablished == true)
651 {
652 if (diff32ms <= iRebufferingThresholdInMilliSeconds)
653 {
654 /* Implies that we are going into rebuffering */
655 if (!iEOSSignalled)
656 {
657 irDelayEstablished = false;
658 PVMFAsyncEvent jbEvent(PVMFInfoEvent, PVMFInfoUnderflow, NULL, NULL);
659 ReportJBInfoEvent(jbEvent);
660 LOGCLIENTANDESTIMATEDSERVCLK_REBUFF;
661 }
662 /* we are past the end of the clip, no more rebuffering */
663 irClientPlayBackClock.Pause();
664 }
665 }
666 if (irDelayEstablished == false && CheckNumElements() == false)
667 {
668 irJitterDelayPercent = 0;
669 }
670 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferNode::IsDelayEstablished: Delay Percent = %d", irJitterDelayPercent));
671 }
672 }
673 /* if we are not rebuffering check for flow control */
674 PerformFlowControl(false);
675 }
676 if (irDelayEstablished)
677 {
678 iInitialBuffering = false;
679 }
680 return (irDelayEstablished);
681 }
682
EOSCmdReceived()683 void PVMFRTPJitterBufferImpl::EOSCmdReceived()
684 {
685 /*
686 * Check for upstream EOS. Set the boolean if it is. EOS from jitter buffer
687 * is sent when jitter buffer is all empty. We do not really expect EOS from
688 * upstream nodes in case of RTP processing. So ignore it.
689 */
690 PVMF_JB_LOGERROR((0, "PVMFRTPJitterBufferImpl::ProcessIncomingMsgRTP - Unexpected EOS"));
691 PVMF_JB_LOGDATATRAFFIC_IN((0, "PVMFRTPJitterBufferImpl::ProcessIncomingMsgRTP - Unexpected EOS"));
692 }
693
694 //////////////////////////////////////////////////////////////////////////////////////////////////
695 //_______________________________________________________________________________________________
696 //00 01|02|03|04 05 06 07|08|09 10 11 12 13 14 15|16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31|
697 //_______________________________________________________________________________________________
698 // Ver |P |X | CC |M | PT | Sequence Number |
699 //_______________________________________________________________________________________________
700 // Timestamp |
701 //_______________________________________________________________________________________________
702 // SSRC |
703 //_______________________________________________________________________________________________
704 // CSRC [0..15] ::: |
705 //_______________________________________________________________________________________________
706 //////////////////////////////////////////////////////////////////////////////////////////////////
ParsePacketHeaderAndUpdateJBStats(PVMFSharedMediaDataPtr & rtpPacketContainer,PVMFSharedMediaDataPtr & aOutDataPacket,uint32 aFragIndex)707 PVMFJBPacketParsingAndStatUpdationStatus PVMFRTPJitterBufferImpl::ParsePacketHeaderAndUpdateJBStats(PVMFSharedMediaDataPtr& rtpPacketContainer,
708 PVMFSharedMediaDataPtr& aOutDataPacket,
709 uint32 aFragIndex)
710 {
711 //Initial validations
712 OsclSharedPtr<PVMFMediaDataImpl> mediaDataIn;
713 if (!rtpPacketContainer->getMediaDataImpl(mediaDataIn))
714 {
715 return PVMF_JB_ERR_NO_PACKET;
716 }
717
718 uint32 SSRC = 0;
719 uint32 markerInfo = 0;
720 uint32 seqNum = 0;
721 PVMFTimestamp rtpTimeStamp = 0;
722 OsclRefCounterMemFrag rtpPacket;
723 rtpPacketContainer->getMediaFragment(aFragIndex, rtpPacket);
724 uint8* rtpHeaderOffset = (uint8*)(rtpPacket.getMemFrag().ptr);
725 uint32 rtpPacketLenExcludingHeader = rtpPacket.getMemFrag().len;
726
727
728 if (!iHeaderPreParsed) //RTP packet
729 {
730 //Validate the packet as per streaming requirements
731 //Update jitter buffer stats like, packet arrival latency
732 /* Get start of RTP packet */
733 uint8* rtpHeader = (uint8*)(rtpPacket.getMemFrag().ptr);
734 uint32 rtpPacketLen = rtpPacket.getMemFrag().len;
735
736 /* is this a legal data packet? */
737 if (rtpPacketLen <= RTP_FIXED_HEADER_SIZE)
738 {
739 return PVMF_JB_ERR_TRUNCATED_HDR;
740 }
741
742
743 if (!iRTPDataArrived)
744 {
745 iRTPDataArrived = true;
746 }
747
748
749 /* Parse RTP version */
750 uint8 rtpVersion = (((*rtpHeader) & RTP_HEADER_V_BIT_MASK) >> RTP_HEADER_V_BIT_OFFSET);
751 if (rtpVersion != SUPPORTED_RTP_HEADER_VERSION)
752 {
753 PVMF_JB_LOGERROR((0, "0x%x RTPPacketHeaderParser::ParseRTPHeader: illegal rtp version", this));
754 return PVMF_JB_ERR_CORRUPT_HDR;
755 }
756
757 /* Check for padding */
758 uint8 pbit = (((*rtpHeader) & RTP_HEADER_P_BIT_MASK) >> RTP_HEADER_P_BIT_OFFSET);
759 uint8 numPaddingOctets = 0;
760 if (pbit)
761 {
762 numPaddingOctets = *(rtpHeader + (rtpPacketLen - 1));
763 }
764
765 /* Check for header extension */
766 uint8 xbit = (((*rtpHeader) & RTP_HEADER_X_BIT_MASK) >> RTP_HEADER_X_BIT_OFFSET);
767
768 /* Check for CSRC */
769 uint8 csrcCount = ((*rtpHeader) & RTP_HEADER_CC_BIT_MASK);
770
771 rtpHeader++;
772
773 /* Parse M bit */
774 uint8 mbit = (((*rtpHeader) & RTP_HEADER_M_BIT_MASK) >> RTP_HEADER_M_BIT_OFFSET);
775
776 rtpHeader++;
777
778 /* Parse sequence number */
779 uint16 seqNum16 = 0;
780 oscl_memcpy((char *)&seqNum16, rtpHeader, sizeof(seqNum16));
781 big_endian_to_host((char *)&seqNum16, sizeof(seqNum16));
782 seqNum = (uint32)seqNum16;
783 rtpHeader += 2;
784
785 /* Parse rtp time stamp */
786 uint32 ts32 = 0;
787 oscl_memcpy((char *)&ts32, rtpHeader, sizeof(ts32));
788 big_endian_to_host((char *)&ts32, sizeof(ts32));
789 rtpTimeStamp = (PVMFTimestamp)ts32;
790 rtpHeader += 4;
791
792 /* Parse SSRC */
793 uint32 ssrc32 = 0;
794 oscl_memcpy((char *)&ssrc32, rtpHeader, sizeof(ssrc32));
795 big_endian_to_host((char *)&ssrc32, sizeof(ssrc32));
796 SSRC = ssrc32;
797 rtpHeader += 4;
798
799 rtpPacketLen -= RTP_FIXED_HEADER_SIZE;
800
801 /* Check for CSRC list - If present skip over */
802 if (csrcCount)
803 {
804 if ((uint32)(csrcCount*4) > rtpPacketLen)
805 {
806 PVMF_JB_LOGERROR((0, "0x%x RTPPacketHeaderParser::ParseRTPHeader: Corrupt CSRC", this));
807 return PVMF_JB_ERR_CORRUPT_HDR;
808 }
809 rtpHeader += (csrcCount * 4);
810 rtpPacketLen -= (csrcCount * 4);
811 }
812
813 /* Check for extended RTP header - If present skip over */
814 if (xbit)
815 {
816 rtpHeader += 2;
817 uint16 len16 = 0;
818 oscl_memcpy((char *)&len16, rtpHeader, sizeof(len16));
819 big_endian_to_host((char *)&len16, sizeof(len16));
820 uint32 extensionHeaderLen = (uint32)len16;
821 rtpPacketLen -= 4;
822 if ((extensionHeaderLen*4) > rtpPacketLen)
823 {
824 PVMF_JB_LOGERROR((0, "0x%x RTPPacketHeaderParser::ParseRTPHeader: Corrupt Extended Header Length", this));
825 return PVMF_JB_ERR_CORRUPT_HDR;
826 }
827 rtpHeader += (extensionHeaderLen * 4);
828 rtpPacketLen -= (extensionHeaderLen * 4);
829 }
830
831 /* Ignore padding octets */
832 if (numPaddingOctets)
833 {
834 if ((uint32)numPaddingOctets > rtpPacketLen)
835 {
836 PVMF_JB_LOGERROR((0, "0x%x RTPPacketHeaderParser::ParseRTPHeader: Corrupt Padding Length", this));
837 return PVMF_JB_ERR_CORRUPT_HDR;
838 }
839 rtpPacketLen -= numPaddingOctets;
840 }
841
842 if (mbit == 1) markerInfo |= PVMF_MEDIA_DATA_MARKER_INFO_M_BIT;
843 rtpHeaderOffset = rtpHeader;
844 rtpPacketLenExcludingHeader = rtpPacketLen;
845 }
846 else
847 {
848 if (aFragIndex != 0)
849 {
850 return PVMF_JB_ERR_INVALID_CONFIGURATION;
851 }
852 rtpTimeStamp = rtpPacketContainer->getTimestamp();
853 seqNum = rtpPacketContainer->getSeqNum();
854 rtpTimeStamp = rtpPacketContainer->getTimestamp();
855 SSRC = rtpPacketContainer->getStreamID();
856 markerInfo = rtpPacketContainer->getMarkerInfo();
857 }
858
859 if (iInPlaceProcessing == false)
860 {
861 OsclSharedPtr<PVMFMediaDataImpl> mediaDataOut;
862 bool status = Allocate(mediaDataOut);
863 if (status == false)
864 {
865 PVMF_JB_LOGERROR((0, "0x%x RTPPacketHeaderParser::ParseRTPHeader: Jitter Buffer Full", this));
866 return PVMF_JB_ERR_INSUFFICIENT_MEM_TO_PACKETIZE;
867 }
868
869 OsclRefCounterMemFrag memFragOut(rtpPacket);
870 memFragOut.getMemFrag().ptr = rtpHeaderOffset;
871 memFragOut.getMemFrag().len = rtpPacketLenExcludingHeader;
872
873 mediaDataOut->appendMediaFragment(memFragOut);
874 mediaDataOut->setMarkerInfo(markerInfo);
875
876 bool retVal = CreateMediaData(aOutDataPacket, mediaDataOut);
877
878 if (retVal == false)
879 {
880 PVMF_JB_LOGERROR((0, "0x%x RTPPacketHeaderParser::ParseRTPHeader: Jitter Buffer Full", this));
881 return PVMF_JB_ERR_INSUFFICIENT_MEM_TO_PACKETIZE;
882 }
883
884 aOutDataPacket->setTimestamp(rtpTimeStamp);
885 aOutDataPacket->setStreamID(SSRC);
886 aOutDataPacket->setSeqNum(seqNum);
887 }
888 else
889 {
890 if (aFragIndex != 0)
891 {
892 return PVMF_JB_ERR_INVALID_CONFIGURATION;
893 }
894
895 rtpPacket.getMemFrag().ptr = rtpHeaderOffset;
896 rtpPacket.getMemFrag().len = rtpPacketLenExcludingHeader;
897 mediaDataIn->clearMediaFragments();
898 mediaDataIn->appendMediaFragment(rtpPacket);
899 rtpPacketContainer->setMarkerInfo(markerInfo);
900 rtpPacketContainer->setTimestamp(rtpTimeStamp);
901 rtpPacketContainer->setStreamID(SSRC);
902 rtpPacketContainer->setSeqNum(seqNum);
903 }
904
905 PVMF_JB_LOGDATATRAFFIC_IN((0, "RTPPacketHeaderParser::ParseRTPHeader: SSRC=%u, rtpSeqNum=%d, rtpTs=%u, rtpPacketLen=%d",
906 SSRC, seqNum, rtpTimeStamp, rtpPacketLenExcludingHeader));
907
908
909 //Validate the packet for Ts, Seq num etc... <only if seq num is locked>
910 if (seqNumLock)
911 {
912 //First packet is always considered to be valid
913 if (iFirstSeqNum != seqNum)
914 {
915 PVMFJitterBufferStats& jbStats = getJitterBufferStats();
916
917 if (iBroadCastSession == true)
918 {
919 /*
920 * This can happen when using prerecorded transport streams that loop
921 * If this happens, just signal an unexpected data event
922 */
923 if (rtpTimeStamp < jbStats.maxTimeStampRegistered)
924 {
925 return PVMF_JB_ERR_UNEXPECTED_PKT;
926 }
927 }
928 if (!IsSeqTsValidForPkt(seqNum, rtpTimeStamp, jbStats))
929 return PVMF_JB_ERR_LATE_PACKET;
930 }
931 }
932
933 if (iInPlaceProcessing)
934 {
935 UpdatePacketArrivalStats(rtpPacketContainer);
936 }
937 else
938 {
939 UpdatePacketArrivalStats(aOutDataPacket);
940 }
941
942 return PVMF_JB_PACKET_PARSING_SUCCESS;
943 }
944
IsSeqTsValidForPkt(uint32 aSeqNum,uint32 rtpTimeStamp,PVMFJitterBufferStats & jbStats)945 OSCL_EXPORT_REF bool PVMFRTPJitterBufferImpl::IsSeqTsValidForPkt(uint32 aSeqNum, uint32 rtpTimeStamp, PVMFJitterBufferStats& jbStats)
946 {
947
948 //Validate the packet for
949 //- Late Packet <based on seqnum of the last packet sent out of JB>
950 //- Roll Over <based on max seqnum of the packet registered with the JB>
951 uint16 seqNum = (uint16)aSeqNum;
952
953 uint16 delta = 0;
954 uint16 referenceSeqNum = 0;
955 if (jbStats.totalNumPacketsRetrieved > 0)
956 {
957 referenceSeqNum = OSCL_STATIC_CAST(uint16, (jbStats.lastRetrievedSeqNum + 1));
958 if (IsSequenceNumEarlier(seqNum, referenceSeqNum, delta))
959 {
960 PVMF_JB_LOGDATATRAFFIC_IN_E((0, "PVMFRTPJitterBufferImpl::IsSeqTsValidForPkt - Late packet seqNum%u, Ts%u, LastRetrievedSeqNum%u, MaxSeqReg%u", seqNum, rtpTimeStamp, jbStats.lastRetrievedSeqNum, jbStats.maxSeqNumRegistered));
961 return false;
962 }
963 }
964
965 //Check for packet rollover <based on the max seq num registered with the jitter buffer>
966 delta = 0;
967 if ((seqNum < jbStats.maxSeqNumRegistered) && IsSequenceNumEarlier(OSCL_STATIC_CAST(uint16, jbStats.maxSeqNumRegistered), seqNum, delta))
968 {
969 PVMF_JB_LOGDATATRAFFIC_IN_E((0, "PVMFRTPJitterBufferImpl::IsSeqTsValidForPkt - Rollover seqNum%u, Ts%u, LastRetrievedSeqNum%u, MaxSeqReg%u", seqNum, rtpTimeStamp, jbStats.lastRetrievedSeqNum, jbStats.maxSeqNumRegistered));
970 /* Reset these variables to acct for seqnum rollover */
971 jbStats.maxSeqNumRegistered = seqNum;
972 jbStats.maxSeqNumReceived = seqNum;
973 jbStats.maxTimeStampRegistered = rtpTimeStamp;
974 }
975 return true;
976 }
977
UpdatePacketArrivalStats(PVMFSharedMediaDataPtr & aArrivedPacket)978 void PVMFRTPJitterBufferImpl::UpdatePacketArrivalStats(PVMFSharedMediaDataPtr& aArrivedPacket)
979 {
980 //Update interarrival jitter
981 /* D(i-1,i) = (RecvT(i) - RTP_TS(i)) -
982 (RecvT(i-1) - RTP_TS(i-1)) */
983
984 uint32 currPacketRecvTime32;
985 bool overflowFlag = false;
986 iPacketArrivalClock->GetCurrentTime32(currPacketRecvTime32, overflowFlag,
987 PVMF_MEDIA_CLOCK_MSEC);
988
989 const PVMFTimestamp rtpTimeStamp = aArrivedPacket->getTimestamp();
990 int32 ts_diff = rtpTimeStamp - iPrevPacketTS;
991 int32 arrival_diff = currPacketRecvTime32 - iPrevPacketRecvTime;
992
993 int32 arrivalJitter = ts_diff - arrival_diff;
994 if (ts_diff < arrival_diff)
995 arrivalJitter = arrival_diff - ts_diff;
996
997 PVMF_JB_LOGDATATRAFFIC_IN((0, "PVMFRTPJitterBufferImpl::UpdatePacketArrivalStats [%s] - new packet Ts %d prev packet ts %d , curr time %d prev time %d iInterArrivalJitterD %d", irMimeType.get_cstr(), rtpTimeStamp, iPrevPacketTS, currPacketRecvTime32, iPrevPacketRecvTime, iInterArrivalJitterD));
998
999 /* J(i) = J(i-1) + (ABS(D(i-1,i)) - J(i-1))/16.0 */
1000 iInterArrivalJitterD += OSCL_STATIC_CAST(double, ((arrivalJitter - iInterArrivalJitterD) / 16.0));
1001 /* Round up */
1002 iInterArrivalJitter = (uint32)(iInterArrivalJitterD + 0.5);
1003
1004 /* Update variables */
1005 iPrevPacketTS = rtpTimeStamp;
1006 iPrevPacketRecvTime = currPacketRecvTime32;
1007 }
1008
CanRetrievePacket()1009 bool PVMFRTPJitterBufferImpl::CanRetrievePacket()
1010 {
1011 LOGCLIENTANDESTIMATEDSERVCLK_DATAPATH_OUT;
1012
1013 uint32 clockDiff;
1014 bool delayEstablished = IsDelayEstablished(clockDiff);
1015 if (!delayEstablished)
1016 {
1017 return false;
1018 }
1019 else
1020 {
1021 if (HasPendingCommand() == true)
1022 {
1023 return true;
1024 }
1025
1026 PVMFJitterBufferStats stats = getJitterBufferStats();
1027 if (stats.currentOccupancy > 0)
1028 {
1029 if (iJitterBuffer->CheckCurrentReadPosition() == false)
1030 {
1031 uint32 in_wrap_count = 0;
1032 /*
1033 * Peek Next timestamp
1034 */
1035 PVMFTimestamp ts = peekNextElementTimeStamp();
1036
1037 /*
1038 * Convert Time stamp to milliseconds
1039 */
1040
1041 ipMediaClockConverter->set_clock(ts, in_wrap_count);
1042 PVMFTimestamp converted_ts =
1043 ipMediaClockConverter->get_converted_ts(1000);
1044 /*
1045 * Get current client playback clock in milliseconds
1046 */
1047 uint32 clientClock;
1048 bool overflowFlag = false;
1049
1050 irClientPlayBackClock.GetCurrentTime32(clientClock, overflowFlag, PVMF_MEDIA_CLOCK_MSEC);
1051
1052
1053 uint32 estimatedServClock = 0;
1054 overflowFlag = false;
1055 irEstimatedServerClock.GetCurrentTime32(estimatedServClock, overflowFlag, PVMF_MEDIA_CLOCK_MSEC);
1056 uint32 delta = 0;
1057 if (!iEOSSignalled && (PVTimeComparisonUtils::IsEarlier(estimatedServClock, converted_ts + iEstimatedServerKeepAheadInMilliSeconds, delta) && (delta > 0)))
1058 {
1059 //hold the available data packet, and wait for hole in the JB due to OOO packet to be filled
1060 if (!IsCallbackPending(JB_NOTIFY_WAIT_FOR_OOO_PACKET_COMPLETE, NULL))
1061 {
1062 PVMF_JB_LOGDATATRAFFIC_OUT((0, "PVMFJitterBufferNode::SendData Detected Hole in JB PeekTs[%d] clientClock[%d] , ClientClockState [%d], estimatedServClock[%d], EstimatedServClockState[%d], oSessionDurationExpired[%d] MimeStr[%s]", converted_ts, clientClock, irClientPlayBackClock.GetState(), estimatedServClock, irEstimatedServerClock.GetState(), iEOSSignalled, irMimeType.get_cstr()));
1063 RequestEventCallBack(JB_NOTIFY_WAIT_FOR_OOO_PACKET_COMPLETE , delta, NULL);
1064 }
1065 return false;
1066 }
1067 }
1068 return true;
1069 }
1070 else
1071 {
1072 if (iEOSSignalled && !iEOSSent)
1073 {
1074 return true;
1075 }
1076 else
1077 {
1078 CancelEventCallBack(JB_MONITOR_REBUFFERING);
1079 RequestEventCallBack(JB_MONITOR_REBUFFERING, (clockDiff - iRebufferingThresholdInMilliSeconds));
1080 return false;
1081 }
1082 }
1083 }
1084 }
1085
CanRetrievePacket(PVMFSharedMediaMsgPtr & aMediaOutMsg,bool & aCmdPacket)1086 bool PVMFRTPJitterBufferImpl::CanRetrievePacket(PVMFSharedMediaMsgPtr& aMediaOutMsg, bool& aCmdPacket)
1087 {
1088 aCmdPacket = false;
1089 LOGCLIENTANDESTIMATEDSERVCLK_DATAPATH_OUT;
1090
1091 uint32 clockDiff = 0;
1092 //There's data in the Jb and delay is established with no hole in the next read position ||
1093 //JB is empty, EOS is already signalled, and EOS is not yet pushed out
1094
1095 bool delayEstablished = IsDelayEstablished(clockDiff);
1096 if (!delayEstablished)
1097 {
1098 return false;
1099 }
1100 else
1101 {
1102 if (GetPendingCommand(aMediaOutMsg) == true)
1103 {
1104 aCmdPacket = true;
1105 return true;
1106 }
1107
1108 PVMFJitterBufferStats stats = getJitterBufferStats();
1109 if (stats.currentOccupancy > 0)
1110 {
1111 if (iJitterBuffer->CheckCurrentReadPosition() == false)
1112 {
1113 uint32 in_wrap_count = 0;
1114 /*
1115 * Peek Next timestamp
1116 */
1117 PVMFTimestamp ts = peekNextElementTimeStamp();
1118
1119 /*
1120 * Convert Time stamp to milliseconds
1121 */
1122 //todo mediaclockconverter
1123
1124 ipMediaClockConverter->set_clock(ts, in_wrap_count);
1125 PVMFTimestamp converted_ts =
1126 ipMediaClockConverter->get_converted_ts(1000);
1127 /*
1128 * Get current client playback clock in milliseconds
1129 */
1130 uint32 clientClock;
1131 bool overflowFlag = false;
1132
1133 irClientPlayBackClock.GetCurrentTime32(clientClock, overflowFlag, PVMF_MEDIA_CLOCK_MSEC);
1134
1135
1136 uint32 estimatedServClock = 0;
1137 overflowFlag = false;
1138 irEstimatedServerClock.GetCurrentTime32(estimatedServClock, overflowFlag, PVMF_MEDIA_CLOCK_MSEC);
1139 uint32 delta = 0;
1140 if (!iEOSSignalled && (PVTimeComparisonUtils::IsEarlier(estimatedServClock, converted_ts + iEstimatedServerKeepAheadInMilliSeconds, delta) && (delta > 0)))
1141 {
1142 //hold the available data packet, and wait for hole in the JB due to OOO packet to be filled
1143 if (!IsCallbackPending(JB_NOTIFY_WAIT_FOR_OOO_PACKET_COMPLETE, NULL))
1144 {
1145 PVMF_JB_LOGDATATRAFFIC_OUT_E((0, "PVMFJitterBufferNode::SendData Detected Hole in JB PeekTs[%d] clientClock[%d] , ClientClockState [%d], estimatedServClock[%d], EstimatedServClockState[%d], oSessionDurationExpired[%d] MimeStr[%s]", converted_ts, clientClock, irClientPlayBackClock.GetState(), estimatedServClock, irEstimatedServerClock.GetState(), iEOSSignalled, irMimeType.get_cstr()));
1146 PVMF_JB_LOGDATATRAFFIC_OUT((0, "PVMFJitterBufferNode::SendData Detected Hole in JB PeekTs[%d] clientClock[%d] , ClientClockState [%d], estimatedServClock[%d], EstimatedServClockState[%d], oSessionDurationExpired[%d] MimeStr[%s]", converted_ts, clientClock, irClientPlayBackClock.GetState(), estimatedServClock, irEstimatedServerClock.GetState(), iEOSSignalled, irMimeType.get_cstr()));
1147 RequestEventCallBack(JB_NOTIFY_WAIT_FOR_OOO_PACKET_COMPLETE , delta, NULL);
1148 }
1149 return false;
1150 }
1151 }
1152 //Retrieve element from JB
1153 //Cancel pending sequencing OOO packet callback (if any)
1154 if (IsCallbackPending(JB_NOTIFY_WAIT_FOR_OOO_PACKET_COMPLETE, NULL))
1155 {
1156 CancelEventCallBack(JB_NOTIFY_WAIT_FOR_OOO_PACKET_COMPLETE, NULL);
1157 }
1158 PVMFTimestamp ts;
1159 uint32 converted_ts;
1160 uint32 in_wrap_count = 0;
1161
1162 PVMFSharedMediaDataPtr mediaOut = RetrievePacketPayload();
1163
1164 if (mediaOut.GetRep() != NULL)
1165 {
1166 ts = mediaOut->getTimestamp();
1167
1168 ipMediaClockConverter->set_clock(ts, in_wrap_count);
1169 converted_ts =
1170 ipMediaClockConverter->get_converted_ts(1000);
1171 mediaOut->setTimestamp(converted_ts);
1172 iLastPacketOutTs = converted_ts;
1173 mediaOut->setFormatSpecificInfo(iTrackConfig);
1174 convertToPVMFMediaMsg(aMediaOutMsg, mediaOut);
1175 }
1176
1177 LOGCLIENTANDESTIMATEDSERVCLK_DATAPATH_OUT;
1178 PVMF_JB_LOGDATATRAFFIC_OUT((0, "PVMFJitterBufferNode::SendData: Mime=%s, SSRC=%d, TS=%d, SEQNUM= %d",
1179 irMimeType.get_cstr(),
1180 aMediaOutMsg->getStreamID(),
1181 aMediaOutMsg->getTimestamp(),
1182 aMediaOutMsg->getSeqNum()));
1183
1184 PVMF_JB_LOGDATATRAFFIC_FLOWCTRL((0, "PVMFJBNode::SendData:"
1185 "MaxSNReg=%d, MaxTSReg=%u, LastSNRet=%d, LastTSRet=%u, NumMsgsInJB=%d",
1186 stats.maxSeqNumRegistered, stats.maxTimeStampRegistered,
1187 stats.lastRetrievedSeqNum, stats.maxTimeStampRetrieved,
1188 (stats.maxSeqNumRegistered - stats.lastRetrievedSeqNum)));
1189 return true;
1190
1191
1192
1193 }
1194 else
1195 {
1196 if (iEOSSignalled && !iEOSSent)
1197 {
1198 GenerateAndSendEOSCommand(aMediaOutMsg, aCmdPacket);
1199 iEOSSent = true;
1200 return true;
1201 }
1202 else
1203 {
1204 if (!IsCallbackPending(JB_MONITOR_REBUFFERING, NULL) && !iEOSSent)
1205 {
1206 RequestEventCallBack(JB_MONITOR_REBUFFERING, (clockDiff - iRebufferingThresholdInMilliSeconds));
1207 }
1208 return false;
1209 }
1210 }
1211 }
1212 }
1213
1214 //////////////////////////////////////////////////////////////////////////////////////////////////
1215 //Utility functions
1216 //////////////////////////////////////////////////////////////////////////////////////////////////
1217
UpdateEstimatedServerClock(bool oFreshStart)1218 void PVMFRTPJitterBufferImpl::UpdateEstimatedServerClock(bool oFreshStart)
1219 {
1220 uint32 rtpTSInMS;
1221 uint32 currentTime32 = 0;
1222 uint32 currentTimeBase32 = 0;
1223 uint32 adjustTime32 = 0;
1224 bool overflowFlag = false;
1225
1226 if (oFreshStart)
1227 {
1228 uint32 in_wrap_count = 0;
1229 iEstServClockMediaClockConvertor.set_clock(iMaxAdjustedRTPTS, in_wrap_count);
1230 rtpTSInMS = iEstServClockMediaClockConvertor.get_converted_ts(1000);
1231 irEstimatedServerClock.Stop();
1232
1233 irEstimatedServerClock.SetStartTime32(rtpTSInMS,
1234 PVMF_MEDIA_CLOCK_MSEC, overflowFlag);
1235 irEstimatedServerClock.Start();
1236 PVMF_JB_LOGCLOCK((0, "PVMFJitterBufferImpl::UpdateEstimatedServerClock - Setting start time - MaxAdjustedRTPTS=%u, StartTime=%d",
1237 iMaxAdjustedRTPTS, rtpTSInMS));
1238 }
1239 else
1240 {
1241 iEstServClockMediaClockConvertor.update_clock(iMaxAdjustedRTPTS);
1242 rtpTSInMS = iEstServClockMediaClockConvertor.get_converted_ts(1000);
1243 adjustTime32 = rtpTSInMS;
1244 bool overflowFlag = false;
1245 irEstimatedServerClock.GetCurrentTime32(currentTime32, overflowFlag,
1246 PVMF_MEDIA_CLOCK_MSEC,
1247 currentTimeBase32);
1248
1249 irEstimatedServerClock.AdjustClockTime32(currentTime32,
1250 currentTimeBase32,
1251 adjustTime32,
1252 PVMF_MEDIA_CLOCK_MSEC,
1253 overflowFlag);
1254
1255 irEstimatedServerClock.GetCurrentTime32(currentTime32, overflowFlag,
1256 PVMF_MEDIA_CLOCK_MSEC,
1257 currentTimeBase32);
1258
1259 PVMF_JB_LOGINFO((0, "PVMFJitterBufferImpl::UpdateEstimatedServerClock - Mime=%s",
1260 irMimeType.get_cstr()));
1261 PVMF_JB_LOGINFO((0, "PVMFJitterBufferImpl::UpdateEstimatedServerClock - EstServClock=%d",
1262 currentTime32));
1263 PVMF_JB_LOGINFO((0, "PVMFJitterBufferImpl::UpdateEstimatedServerClock - RTPTime32=%u",
1264 adjustTime32));
1265 PVMF_JB_LOGINFO((0, "PVMFJitterBufferImpl::UpdateEstimatedServerClock - Adjusting Clock - iMaxAdjustedRTPTS=%u, currentTimeBase32=%d",
1266 iMaxAdjustedRTPTS, currentTimeBase32));
1267 }
1268
1269 }
1270
ComputeMaxAdjustedRTPTS()1271 void PVMFRTPJitterBufferImpl::ComputeMaxAdjustedRTPTS()
1272 {
1273 PVMFJitterBufferStats jbStats = getJitterBufferStats();
1274
1275 uint32 aSeqNum = jbStats.maxSeqNumReceived;
1276 PVMFTimestamp aTS = jbStats.maxTimeStampRegistered;
1277
1278 PVMFRTPInfoParams* rtpInfoParams = FindRTPInfoParams(aSeqNum);
1279 if (rtpInfoParams == NULL)
1280 {
1281 PVMF_JB_LOGERROR((0, "PVMFJitterBufferImpl::ComputeMaxAdjustedRTPTS: illegal seq number"));
1282 OSCL_LEAVE(OsclErrArgument);
1283 }
1284
1285 PVMF_JB_LOGINFO((0, "PVMFJitterBufferImpl::ComputeMaxAdjustedRTPTS - maxSeqNumReceived=%d, rtpInfoParams->seqNum=%d, iPrevSeqNumBaseIn=%d, Mime=%s",
1286 aSeqNum, rtpInfoParams->seqNum, iPrevSeqNumBaseIn, irMimeType.get_cstr()));
1287
1288 uint16 diff = 0;
1289 if (rtpInfoParams->seqNumBaseSet && IsSequenceNumEarlier(OSCL_STATIC_CAST(uint16, iPrevSeqNumBaseIn), OSCL_STATIC_CAST(uint16, rtpInfoParams->seqNum), diff))
1290 {
1291 iPrevSeqNumBaseIn = rtpInfoParams->seqNum;
1292 iPrevTSIn = rtpInfoParams->rtpTime;
1293 }
1294 iMaxAdjustedRTPTS += (aTS - iPrevAdjustedRTPTS);
1295 iPrevAdjustedRTPTS = aTS;
1296
1297 PVMF_JB_LOGINFO((0, "PVMFJitterBufferImpl::ComputeMaxAdjustedRTPTS - maxTimeStampRegistered=%u, iPrevAdjustedRTPTS=%u, iMaxAdjustedRTPTS=%u, Mime=%s",
1298 aTS, iPrevAdjustedRTPTS, iMaxAdjustedRTPTS, irMimeType.get_cstr()));
1299
1300 UpdateEstimatedServerClock();
1301 }
1302
DeterminePrevTimeStampPeek(uint32 aSeqNum,PVMFTimestamp & aPrevTS)1303 void PVMFRTPJitterBufferImpl::DeterminePrevTimeStampPeek(uint32 aSeqNum,
1304 PVMFTimestamp& aPrevTS)
1305 {
1306 PVMFRTPInfoParams* rtpInfoParams = FindRTPInfoParams(aSeqNum);
1307 if (rtpInfoParams == NULL)
1308 {
1309 if (iRTPInfoParamsVec.size() == 0)
1310 {
1311 PVMF_JB_LOGWARNING((0, "PVMFJitterBufferImpl::DeterminePrevTimeStamp: RTPInfoVec Empty"));
1312 /* Use the value from the first packet */
1313 aPrevTS = seqLockTimeStamp;
1314 }
1315 else
1316 {
1317 PVMF_JB_LOGERROR((0, "PVMFJitterBufferImpl::DeterminePrevTimeStampPeek: illegal seq number"));
1318 OSCL_LEAVE(OsclErrArgument);
1319 }
1320 return; // no need to continue here
1321 }
1322 else
1323 {
1324 uint16 diff32 = 0;
1325 if (rtpInfoParams->seqNumBaseSet && IsSequenceNumEarlier(OSCL_STATIC_CAST(uint16, iPrevSeqNumBaseOut), OSCL_STATIC_CAST(uint16, rtpInfoParams->seqNum), diff32))
1326 {
1327 aPrevTS = rtpInfoParams->rtpTime;
1328 }
1329 else
1330 {
1331 aPrevTS = iPrevTSOut;
1332 }
1333 }
1334
1335 }
1336
IsSequenceNumEarlier(uint16 aSeqNumToComp,uint16 aBaseSeqNum,uint16 & aDiff)1337 bool PVMFRTPJitterBufferImpl::IsSequenceNumEarlier(uint16 aSeqNumToComp, uint16 aBaseSeqNum, uint16& aDiff)
1338 {
1339 aDiff = 0;
1340 if (aSeqNumToComp < aBaseSeqNum)
1341 return true;
1342
1343 aDiff = aSeqNumToComp - aBaseSeqNum;
1344 if (aDiff < PVMF_JITTER_BUFFER_ROLL_OVER_THRESHOLD_16BIT)
1345 {
1346 return false;
1347 }
1348
1349 return true;
1350 }
1351
PerformFlowControl(bool aIncomingPacket)1352 PVMFStatus PVMFRTPJitterBufferImpl::PerformFlowControl(bool aIncomingPacket)
1353 {
1354 OSCL_UNUSED_ARG(aIncomingPacket);
1355 return PVMFSuccess;
1356 }
1357
DeterminePrevTimeStamp(uint32 aSeqNum)1358 void PVMFRTPJitterBufferImpl::DeterminePrevTimeStamp(uint32 aSeqNum)
1359 {
1360
1361 PVMFRTPInfoParams* rtpInfoParams = FindRTPInfoParams(aSeqNum);
1362 if (rtpInfoParams == NULL)
1363 {
1364 if (iRTPInfoParamsVec.size() == 0)
1365 {
1366 PVMF_JB_LOGWARNING((0, "PVMFJitterBufferImpl::DeterminePrevTimeStamp: RTPInfoVec Empty"));
1367 /* Use the value from the first packet */
1368 iPrevTSOut = seqLockTimeStamp;
1369 iPrevTSIn = seqLockTimeStamp;
1370 iPrevAdjustedRTPTS = seqLockTimeStamp;
1371 iPrevSeqNumBaseOut = iFirstSeqNum;
1372 iPrevSeqNumBaseIn = iFirstSeqNum;
1373
1374 PVMF_JB_LOGERROR((0, "PVMFJitterBufferImpl::DeterminePrevTimeStamp: illegal seq number = %d", aSeqNum));
1375 return;
1376 }
1377 else
1378 {
1379 PVMF_JB_LOGERROR((0, "PVMFJitterBufferImpl::DeterminePrevTimeStamp: illegal seq number = %d", aSeqNum));
1380 OSCL_LEAVE(OsclErrArgument);
1381 }
1382 }
1383 uint16 diff = 0;
1384 if (rtpInfoParams->seqNumBaseSet && IsSequenceNumEarlier(OSCL_STATIC_CAST(uint16, iPrevSeqNumBaseOut), OSCL_STATIC_CAST(uint16, rtpInfoParams->seqNum), diff))
1385 {
1386 /* We need to adjust iMonotonicTimeStamp as well for resume */
1387 if (rtpInfoParams->isPlayAfterPause && rtpInfoParams->nptTimeBaseSet && isPrevNptTimeSet && isPrevRtpTimeSet)
1388 {
1389 uint64 CurrNptTime, PrevNptTime;
1390 Oscl_Int64_Utils::set_uint64(CurrNptTime, 0, rtpInfoParams->nptTimeInRTPTimeScale);
1391 Oscl_Int64_Utils::set_uint64(PrevNptTime, 0, iPrevNptTimeInRTPTimeScale + iPrevTSOut - iPrevRtpTimeBase);
1392 iMonotonicTimeStamp = iMonotonicTimeStamp + CurrNptTime - PrevNptTime;
1393 PVMF_JB_LOGINFO((0, "PVMFJitterBufferImpl::DeterminePrevTimeStamp: TS adjustment for resume"
1394 "nptTimeInRTPTimeScale=%d, iPrevNptTimeInRTPTimeScale=%d, iPrevRtpTimeBase=%d, iPrevTSOut=%d", rtpInfoParams->nptTimeInRTPTimeScale, iPrevNptTimeInRTPTimeScale, iPrevRtpTimeBase, iPrevTSOut));
1395 }
1396 if (rtpInfoParams->nptTimeBaseSet)
1397 {
1398 iPrevNptTimeInRTPTimeScale = rtpInfoParams->nptTimeInRTPTimeScale;
1399 isPrevNptTimeSet = true;
1400 }
1401 else
1402 {
1403 isPrevNptTimeSet = false;
1404 }
1405 if (rtpInfoParams->rtpTimeBaseSet)
1406 {
1407 iPrevRtpTimeBase = rtpInfoParams->rtpTime;
1408 isPrevRtpTimeSet = true;
1409 }
1410 else
1411 {
1412 isPrevRtpTimeSet = false;
1413 }
1414 PVMF_JB_LOGINFO((0, "PVMFJitterBufferImpl::DeterminePrevTimeStamp: TS update for "
1415 "iPrevNptTimeInRTPTimeScale=%d, iPrevRtpTimeBase=%d", rtpInfoParams->nptTimeInRTPTimeScale, rtpInfoParams->rtpTime));
1416 iPrevSeqNumBaseOut = rtpInfoParams->seqNum;
1417 iPrevTSOut = rtpInfoParams->rtpTime;
1418 }
1419 PVMF_JB_LOGINFO((0, "PVMFJitterBufferImpl::DeterminePrevTimeStamp: RTPInfoSeqNum=%d, iPrevSeqNumBaseOut=%d, iPrevTSOut=%u",
1420 rtpInfoParams->seqNum, iPrevSeqNumBaseOut, iPrevTSOut));
1421
1422 }
1423
ReportJBInfoEvent(PVMFAsyncEvent & aEvent)1424 void PVMFRTPJitterBufferImpl::ReportJBInfoEvent(PVMFAsyncEvent& aEvent)
1425 {
1426 if (PVMFInfoUnderflow == aEvent.GetEventType())
1427 {
1428 CancelEventCallBack(JB_BUFFERING_DURATION_COMPLETE);
1429 RequestEventCallBack(JB_BUFFERING_DURATION_COMPLETE);
1430 }
1431 else if (PVMFInfoDataReady == aEvent.GetEventType())
1432 {
1433 CancelEventCallBack(JB_BUFFERING_DURATION_COMPLETE);
1434 }
1435 PVMFJitterBufferImpl::ReportJBInfoEvent(aEvent);
1436 }
1437