1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 #ifndef PVMF_RTCP_PROTO_IMPL_H_INCLUDED 19 #include "pvmf_rtcp_proto_impl.h" 20 #endif 21 22 #ifndef RTCP_DECODER_H 23 #include "rtcp_decoder.h" 24 #endif 25 #ifndef RTCP_ENCODER_H 26 #include "rtcp_encoder.h" 27 #endif 28 29 #ifndef OSCL_EXCLUSIVE_PTR_H_INCLUDED 30 #include "oscl_exclusive_ptr.h" 31 #endif 32 33 #ifndef PVMF_SM_NODE_EVENTS_H_INCLUDED 34 #include "pvmf_sm_node_events.h" 35 #endif 36 37 /////////////////////////////////////////////////////////////////////////////// 38 //PVRTCPChannelController Implementation 39 /////////////////////////////////////////////////////////////////////////////// 40 PVRTCPChannelController* PVRTCPChannelController::New(PVRTCPChannelControllerObserver* aObserver, PVMFJitterBuffer& aRTPJitterBuffer, PVMFPortInterface* aFeedbackPort, PVMFMediaClock& aClientPlaybackClock, PVMFMediaClock& aRTCPClock) 41 { 42 PVRTCPChannelController* rtcpChannelController = NULL; 43 int32 err = OsclErrNone; 44 OSCL_TRY(err, rtcpChannelController = OSCL_NEW(PVRTCPChannelController, (aObserver, aRTPJitterBuffer, aFeedbackPort, aClientPlaybackClock, aRTCPClock)); 45 rtcpChannelController->Construct();); 46 if (err != OsclErrNone && rtcpChannelController) 47 { 48 OSCL_DELETE(rtcpChannelController); 49 rtcpChannelController = NULL; 50 } 51 return rtcpChannelController; 52 } 53 54 PVRTCPChannelController::PVRTCPChannelController(PVRTCPChannelControllerObserver* aObserver, PVMFJitterBuffer& aRTPJitterBuffer, PVMFPortInterface* aFeedbackPort, PVMFMediaClock& aClientPlaybackClock, PVMFMediaClock& aRTCPClock) 55 : ipObserver(aObserver) 56 , irRTPDataJitterBuffer(aRTPJitterBuffer) 57 , ipFeedbackPort(aFeedbackPort) 58 , irClientPlayBackClock(aClientPlaybackClock) 59 , irRTCPClock(aRTCPClock) 60 { 61 ipRTCPDataPathLoggerIn = PVLogger::GetLoggerObject("datapath.sourcenode.jitterbuffer.rtcp.in"); 62 ipRTCPDataPathLoggerOut = PVLogger::GetLoggerObject("datapath.sourcenode.jitterbuffer.rtcp.out"); 63 64 ipRTCPTimer = NULL; 65 iRandGen.Seed(RTCP_RAND_SEED); 66 ResetParams(); 67 } 68 69 void PVRTCPChannelController::ResetParams(bool aMemoryCleanUp) 70 { 71 OSCL_UNUSED_ARG(aMemoryCleanUp); 72 iRTPChannelRateAdaptationInfo.iRateAdaptation = false; 73 iRTPChannelRateAdaptationInfo.iRateAdaptationFeedBackFrequency = 0; 74 iRTPChannelRateAdaptationInfo.iRateAdaptationRTCPRRCount = 0; 75 iRTPChannelRateAdaptationInfo.iRateAdaptationFreeBufferSpaceInBytes = 0; 76 iRTCPStats.Reset(); 77 iNumSenders = 0; 78 iRR = 0; 79 iRS = 0; 80 iRtcpBwConfigured = false; 81 iInitialRTCPIntervalComputation = false; 82 iRTCPIntervalInMicroSeconds = DEFAULT_RTCP_INTERVAL_USEC; 83 iInitialRTCPPacket = true; 84 ipMediaClockConverter = NULL; 85 } 86 87 void PVRTCPChannelController::Construct() 88 { 89 int32 err = OsclErrNone; 90 OSCL_TRY(err, ipRTCPTimer = OSCL_NEW(PvmfRtcpTimer, (this));); 91 if (err != OsclErrNone || !ipRTCPTimer) 92 { 93 OSCL_LEAVE(PVMFErrNoResources); 94 } 95 96 } 97 98 PVRTCPChannelController::~PVRTCPChannelController() 99 { 100 Reset(); 101 if (ipRTCPTimer) 102 OSCL_DELETE(ipRTCPTimer); 103 } 104 void PVRTCPChannelController::StartRTCPMessageExchange() 105 { 106 //noop 107 iRTCPIntervalInMicroSeconds = CalcRtcpInterval(); 108 ipRTCPTimer->setRTCPInterval(iRTCPIntervalInMicroSeconds); 109 ipRTCPTimer->Start(); 110 } 111 112 void PVRTCPChannelController::PauseRTCPMessageExchange() 113 { 114 if (ipRTCPTimer) 115 ipRTCPTimer->Cancel(); 116 } 117 118 void PVRTCPChannelController::StopRTCPMessageExchange() 119 { 120 if (ipRTCPTimer) 121 ipRTCPTimer->Cancel(); 122 } 123 124 void PVRTCPChannelController::Reset() 125 { 126 if (ipRTCPTimer) 127 ipRTCPTimer->Stop(); 128 ResetParams(true); 129 } 130 131 void PVRTCPChannelController::SetRateAdaptation(bool aRateAdaptation, uint32 aRateAdaptationFeedBackFrequency, uint32 aRateAdaptationFreeBufferSpaceInBytes) 132 { 133 iRTPChannelRateAdaptationInfo.iRateAdaptation = aRateAdaptation; 134 iRTPChannelRateAdaptationInfo.iRateAdaptationFeedBackFrequency = aRateAdaptationFeedBackFrequency; 135 iRTPChannelRateAdaptationInfo.iRateAdaptationFreeBufferSpaceInBytes = aRateAdaptationFreeBufferSpaceInBytes; 136 } 137 138 void PVRTCPChannelController::SetRTCPIntervalInMicroSecs(uint32 aRTCPInterval) 139 { 140 iRTCPIntervalInMicroSeconds = aRTCPInterval; 141 } 142 143 PVMFStatus PVRTCPChannelController::ProcessRTCPReport(PVMFSharedMediaMsgPtr& aMsg) 144 { 145 RTCP_Decoder rtcpDec; 146 RTCP_SR rtcpSR; 147 RTCP_BYE rtcpBye; 148 RTCP_Decoder::Error_t retval; 149 int32 max_array_size = MAX_RTCP_SOURCES; 150 RTCPPacketType array_of_packet_types[MAX_RTCP_SOURCES]; 151 OsclMemoryFragment array_of_packets[MAX_RTCP_SOURCES]; 152 int32 filled_size = 0; 153 154 PVMFSharedMediaDataPtr rtcpDataIn; 155 convertToPVMFMediaData(rtcpDataIn, aMsg); 156 157 const uint32 numFrags = rtcpDataIn->getNumFragments(); 158 159 //Process each RTCP packet. 160 //Typically, only one is received at a time. 161 for (uint32 i = 0; i < numFrags; i++) 162 { 163 OsclRefCounterMemFrag memfrag; 164 165 //Get the next memory fragment from the media message. 166 if (rtcpDataIn->getMediaFragment(i, memfrag) == false) 167 { 168 return PVMFFailure; 169 } 170 171 //Get the pointer to the packet. 172 OsclMemoryFragment receivedMsg = memfrag.getMemFrag(); 173 174 /* Find out what type of RTCP packet we have */ 175 //This populates the variables "filled_size", "array_of_packet_types", and "array_of_packets" 176 //by breaking up compound RTCP packets into individual reports. 177 rtcpDec.scan_compound_packet(receivedMsg, 178 max_array_size, 179 filled_size, 180 array_of_packet_types, 181 array_of_packets); 182 183 // update packet size averages - we treat the compound packet 184 // as a single packet 185 iRTCPStats.avg_rtcp_compound_pkt_size = OSCL_STATIC_CAST(float, (receivedMsg.len + 15.0 * iRTCPStats.avg_rtcp_compound_pkt_size) / 16.0); 186 187 //Process each individual report. 188 for (int32 ii = 0; ii < filled_size; ii++) 189 { 190 /* Use the appropriate decoder */ 191 192 //If the RTCP type indicates a Sender Report... 193 if (SR_RTCP_PACKET == array_of_packet_types[ii]) 194 { 195 PVMF_JB_LOG_RTCPDATATRAFFIC_IN((0, "PVMFJitterBufferNode::ProcessIncomingRTCPReport - Sender Report")); 196 //Decode the Sender Report. 197 retval = rtcpDec.DecodeSR(array_of_packets[ii], rtcpSR); 198 if (RTCP_Decoder::FAIL == retval) 199 { 200 return PVMFFailure; 201 } 202 203 uint32 tsOffset = 0; 204 if (irRTPDataJitterBuffer.GetRTPTimeStampOffset(tsOffset)) 205 { 206 if (rtcpSR.senderSSRC != irRTPDataJitterBuffer.GetSSRC()) 207 { 208 PVMF_JB_LOG_RTCPDATATRAFFIC_IN((0, "PVRTCPChannelController::ProcessRTCPReport - RTCP ssrc %u JB SSRC %u RTCP report is invalid (from some other session), ignoring it", rtcpSR.senderSSRC, irRTPDataJitterBuffer.GetSSRC())); 209 return PVMFSuccess; 210 } 211 } 212 213 iRTCPStats.iLastSenderReportSSRC = rtcpSR.senderSSRC; 214 215 iRTCPStats.lastSenderReportTS = 216 (rtcpSR.NTP_timestamp_high << 16) | 217 ((rtcpSR.NTP_timestamp_low >> 16) & 0x0000ffff); 218 219 /* 220 * Get RTCP Recv Time in milliseconds 221 */ 222 uint32 srRecvTime; 223 bool overflowFlag = false; 224 irRTCPClock.GetCurrentTime32(srRecvTime, overflowFlag, PVMF_MEDIA_CLOCK_MSEC); 225 226 iRTCPStats.lastSenderReportRecvTime = srRecvTime; 227 228 //Save the NTP and RTP timestamps for later calculations... 229 iRTCPStats.lastSenderReportNTP = 230 (((uint64)rtcpSR.NTP_timestamp_high) << 32) + (uint64)rtcpSR.NTP_timestamp_low; 231 iRTCPStats.lastSenderReportRTP = rtcpSR.RTP_timestamp; 232 PVMF_JB_LOG_RTCPDATATRAFFIC_IN((0, "PVMFJitterBufferNode::ProcessIncomingRTCPReport - Sender Report TS iRTCPStats.lastSenderReportRTP %u, iRTCPStats.iLastSenderReportSSRC %u ", rtcpSR.RTP_timestamp, iRTCPStats.iLastSenderReportSSRC)); 233 PVMF_JB_LOG_RTCPDATATRAFFIC_IN((0, "PVMFJitterBufferNode::ProcessIncomingRTCPReport - Sender Report NPT rtcpSR.NTP_timestamp_high %u rtcpSR.NTP_timestamp_low %u SR Ts %u ", rtcpSR.NTP_timestamp_high, rtcpSR.NTP_timestamp_low , iRTCPStats.lastSenderReportTS)); 234 235 ipObserver->RTCPSRReveived(this); 236 } 237 238 //If the RTCP type is BYE, set the end-of-stream flag. 239 if (BYE_RTCP_PACKET == array_of_packet_types[ii]) 240 { 241 PVMF_JB_LOG_RTCPDATATRAFFIC_IN_E((0, "PVMFJitterBufferNode::ProcessIncomingRTCPReport- RTCP_BYE_RECVD: Mime=%s", irRTPDataJitterBuffer.GetMimeType())); 242 retval = rtcpDec.DecodeBYE(array_of_packets[ii], rtcpBye); 243 if (RTCP_Decoder::FAIL == retval) 244 { 245 return PVMFFailure; 246 } 247 /* The packet is a RTCP BYE, set the end of stream flag */ 248 else if (retval == RTCP_Decoder::RTCP_SUCCESS) 249 { 250 iRTCPStats.oRTCPByeRecvd = true; 251 ipObserver->RTCPByeReceived(this); 252 } 253 } 254 255 //All other RTCP types (Receiver Reports) are ignored. 256 iRTCPStats.oSRRecvd = true; 257 } 258 } 259 return PVMFSuccess; 260 } 261 262 uint32 PVRTCPChannelController::CalcRtcpInterval() 263 { 264 float interval; 265 266 #if RTCP_FIXED_INTERVAL_MODE 267 interval = DEFAULT_RTCP_INTERVAL_SEC; 268 #else 269 float rtcp_min_time = iInitialRTCPIntervalComputation ? (float)DEFAULT_RTCP_INTERVAL_SEC / 2.0 : 270 (float)DEFAULT_RTCP_INTERVAL_SEC; 271 272 if (iRtcpBwConfigured && (iRR > 0)) 273 { 274 float divisor = (float)iRR; 275 if (iRR > iRS) 276 { 277 divisor = (float)iRS; 278 } 279 interval = iRTCPStats.avg_rtcp_compound_pkt_size * 8 * 280 ((float)iNumSenders + 1) / divisor; 281 282 if (interval < rtcp_min_time) 283 { 284 interval = rtcp_min_time; 285 } 286 } 287 else 288 { 289 interval = rtcp_min_time; 290 } 291 #endif 292 293 // generate a random number on [0, 1000) 294 uint32 n = iRandGen.Rand() % 1000; 295 296 // map the number onto the range [0.5, 1.5) 297 float window = OSCL_STATIC_CAST(float, (0.5 + ((float)n) / 1000.0)); 298 299 // generate the actual interval, in seconds 300 float interval_scaled = OSCL_STATIC_CAST(float, interval * window / 1.21828); 301 302 // return the interval in microseconds 303 return (uint32)(interval_scaled * 1000000); 304 } 305 306 void PVRTCPChannelController::RtcpTimerEvent() 307 { 308 if (iRTCPStats.oRTCPByeRecvd == false) 309 { 310 // timer reconsideration 311 uint32 timer = CalcRtcpInterval(); 312 PVMF_JB_LOG_RTCPDATATRAFFIC_IN((0, "%x timer %d iRTCPIntervalInMicroSeconds %d", this, timer, iRTCPIntervalInMicroSeconds)); 313 if (timer > iRTCPIntervalInMicroSeconds) 314 { 315 ipRTCPTimer->setRTCPInterval(timer - iRTCPIntervalInMicroSeconds); 316 ipRTCPTimer->Start(); 317 iRTCPIntervalInMicroSeconds = timer; 318 return; 319 } 320 else 321 { 322 GenerateRTCPRR(); 323 } 324 } 325 } 326 327 PVMFStatus PVRTCPChannelController::GenerateRTCPRR() 328 { 329 if (ipFeedbackPort->IsOutgoingQueueBusy() == false) 330 { 331 ComposeFeedBackPacket(); 332 } 333 334 /* Reschedule the RTCP timer for the next interval */ 335 iRTCPIntervalInMicroSeconds = CalcRtcpInterval(); 336 PVMF_JB_LOG_RTCPDATATRAFFIC_IN((0, "%x GeneratedRTCPRR", this)); 337 ipRTCPTimer->setRTCPInterval(iRTCPIntervalInMicroSeconds); 338 ipRTCPTimer->Start(); 339 return PVMFSuccess; 340 } 341 342 PVMFStatus PVRTCPChannelController::ComposeFeedBackPacket() 343 { 344 uint32 senderSSRC; 345 RTCP_Encoder rtcpEncode; 346 RTCP_ReportBlock *reportBlock = NULL; 347 RTCP_RR *pRR = NULL; 348 OsclExclusivePtr<RTCP_RR> rtcpRRAutoPtr; 349 350 pRR = OSCL_NEW(RTCP_RR, (1)); 351 352 if (NULL == pRR) 353 { 354 return PVMFErrNoMemory; 355 } 356 rtcpRRAutoPtr.set(pRR); 357 358 reportBlock = pRR->get_report_block(0); 359 if (NULL == reportBlock) 360 { 361 return PVMFErrNoMemory; 362 } 363 364 /* Get Jitter Buffer Stats from RTP port */ 365 PVMFJitterBufferStats jbStats = 366 irRTPDataJitterBuffer.getJitterBufferStats(); 367 368 /* Get InterArrivalJitter from RTP port */ 369 uint32 interArrivalJitter = 370 irRTPDataJitterBuffer.getInterArrivalJitter(); 371 372 uint32 sourceSSRC32 = jbStats.ssrc; 373 senderSSRC = (sourceSSRC32 >> 10) | (sourceSSRC32 << 22); 374 375 pRR->senderSSRC = senderSSRC; 376 rtcpEncode.SetSSRC(senderSSRC); 377 reportBlock->sourceSSRC = sourceSSRC32; 378 379 /* Compute packet loss fraction */ 380 if (iRTCPStats.maxSeqNumRecievedUptoThisRR == 0) 381 { 382 iRTCPStats.maxSeqNumRecievedUptoThisRR = 383 jbStats.seqNumBase; 384 } 385 if (jbStats.maxSeqNumReceived - 386 iRTCPStats.maxSeqNumRecievedUptoThisRR) 387 { 388 reportBlock->fractionLost = 389 (int8)(((jbStats.totalPacketsLost - iRTCPStats.packetLossUptoThisRR) * 256) / 390 (jbStats.maxSeqNumReceived - iRTCPStats.maxSeqNumRecievedUptoThisRR)); 391 } 392 else 393 { 394 reportBlock->fractionLost = 0; 395 } 396 397 reportBlock->cumulativeNumberOfPacketsLost = jbStats.totalPacketsLost; 398 reportBlock->highestSequenceNumberReceived = jbStats.maxSeqNumReceived; 399 reportBlock->interarrivalJitter = interArrivalJitter; 400 reportBlock->lastSR = iRTCPStats.lastSenderReportTS; 401 402 if (iRTCPStats.oSRRecvd) 403 { 404 uint32 currRRGenTime; 405 bool overflowFlag = false; 406 407 irRTCPClock.GetCurrentTime32(currRRGenTime, overflowFlag, PVMF_MEDIA_CLOCK_MSEC); 408 409 uint32 lastSenderReportRecvTime = (uint32) 410 iRTCPStats.lastSenderReportRecvTime; 411 412 uint32 delaySinceLastSR64 = 413 (currRRGenTime - lastSenderReportRecvTime); 414 415 uint32 delaySinceLastSR32 = delaySinceLastSR64; 416 417 reportBlock->delaySinceLastSR = (delaySinceLastSR32 << 16) / 1000; 418 419 iRTCPStats.lastRRGenTime = currRRGenTime; 420 } 421 422 /* Update variables for the next RR cycle */ 423 iRTCPStats.maxSeqNumRecievedUptoThisRR = 424 jbStats.maxSeqNumReceived; 425 iRTCPStats.packetLossUptoThisRR = 426 jbStats.totalPacketsLost; 427 428 429 PVMFSharedMediaDataPtr rtcpOut; 430 OsclSharedPtr<PVMFMediaDataImpl> mediaDataImpl; 431 PVMFRTCPMemPool* rtcpBufAlloc = ipRTCPTimer->getRTCPBuffAlloc(); 432 if (!rtcpBufAlloc->ipMediaDataMemPool) 433 { 434 return PVMFErrNoMemory; 435 } 436 int32 err; 437 438 OSCL_TRY(err, 439 mediaDataImpl = rtcpBufAlloc->GetMediaDataImpl(MAX_RTCP_BLOCK_SIZE); 440 rtcpOut = PVMFMediaData::createMediaData(mediaDataImpl, 441 (rtcpBufAlloc->ipMediaDataMemPool)); 442 ); 443 444 if (err != OsclErrNone) 445 { 446 return PVMFErrNoMemory; 447 } 448 449 /* Retrieve memory fragment to write to */ 450 OsclRefCounterMemFrag refCtrMemFragOut; 451 rtcpOut->getMediaFragment(0, refCtrMemFragOut); 452 453 OsclMemoryFragment memFrag = refCtrMemFragOut.getMemFrag(); 454 memFrag.len = MAX_RTCP_BLOCK_SIZE; 455 456 RTCP_APP *appPtr = NULL; 457 RTCP_APP App; 458 459 PVMF_JB_LOG_RTCPDATATRAFFIC_OUT((0, "RTCP_PKT: Mime=%s, MaxSNRecvd=%d, MaxTSRecvd=%d, MaxSNRet=%d, MaxTSRet=%d", irRTPDataJitterBuffer.GetMimeType(), jbStats.maxSeqNumReceived, jbStats.maxTimeStampRegistered, jbStats.lastRetrievedSeqNum, jbStats.maxTimeStampRetrieved)); 460 /* 461 * If Rate Adaptation is enabled and we have received some RTP packets, then send NADU APP packet, 462 * if frequency criteria is met 463 */ 464 if (iRTPChannelRateAdaptationInfo.iRateAdaptation && (jbStats.totalNumPacketsReceived > 0)) 465 { 466 iRTPChannelRateAdaptationInfo.iRateAdaptationRTCPRRCount++; 467 if (iRTPChannelRateAdaptationInfo.iRateAdaptationRTCPRRCount == 468 iRTPChannelRateAdaptationInfo.iRateAdaptationFeedBackFrequency) 469 { 470 oscl_memcpy(App.type, PSS0_APP_RTCP_NAME, oscl_strlen(PSS0_APP_RTCP_NAME)); 471 App.ssrc = senderSSRC; 472 App.subtype = RTCP_NADU_APP_SUBTYPE; 473 App.pss0_app_data.sourcessrc = sourceSSRC32; 474 PVMFTimestamp converted_ts = 0; 475 //set playoutdelay to 0xffff by default, if JB is empty we will use this 476 uint32 diff32 = RTCP_NADU_APP_DEFAULT_PLAYOUT_DELAY; 477 uint32 clientClock32 = 0; 478 uint32 timebase32 = 0; 479 bool overflowFlag = false; 480 481 irClientPlayBackClock.GetCurrentTime32(clientClock32, overflowFlag, 482 PVMF_MEDIA_CLOCK_MSEC, 483 timebase32); 484 PVMFTimestamp tsOfNextPacketToBeDecoded = jbStats.maxTimeStampRetrievedWithoutRTPOffset; 485 if (jbStats.currentOccupancy > 0) 486 { 487 tsOfNextPacketToBeDecoded = 488 irRTPDataJitterBuffer.peekNextElementTimeStamp(); 489 490 uint32 in_wrap_count = 0; 491 /* 492 * Convert Time stamp to milliseconds 493 */ 494 ipMediaClockConverter->set_clock(tsOfNextPacketToBeDecoded, in_wrap_count); 495 converted_ts = 496 ipMediaClockConverter->get_converted_ts(1000); 497 498 //ts should be ahead of clock 499 //if not we are falling behind on one track, so set playout delay to zero 500 diff32 = 0; 501 bool clkEarly = 502 PVTimeComparisonUtils::IsEarlier(clientClock32, 503 converted_ts, 504 diff32); 505 if (clkEarly == false) 506 { 507 diff32 = 0; 508 } 509 } 510 else //If Jb is empty we have some data in the pipeline that is sent out to the peer node. 511 { 512 diff32 = tsOfNextPacketToBeDecoded - clientClock32; 513 } 514 515 PVMF_JB_LOG_RTCPDATATRAFFIC_OUT((0, "RTCP_PKT: Mime=%s, RTP_TS=%d, C_CLOCK=%d, DIFF=%d", irRTPDataJitterBuffer.GetMimeType(), converted_ts, clientClock32, diff32)); 516 App.pss0_app_data.playoutdelayinms = (uint16)diff32; 517 App.pss0_app_data.nsn = OSCL_STATIC_CAST(uint16, (jbStats.lastRetrievedSeqNum + 1)); 518 if (0 == jbStats.lastRetrievedSeqNum) 519 { 520 App.pss0_app_data.nsn = OSCL_STATIC_CAST(uint16, jbStats.seqNumBase); 521 } 522 App.pss0_app_data.nun = RTCP_NADU_APP_DEFAULT_NUN; 523 524 uint32 fbsInBytes = 0; 525 if (jbStats.packetSizeInBytesLeftInBuffer < iRTPChannelRateAdaptationInfo.iRateAdaptationFreeBufferSpaceInBytes) 526 { 527 fbsInBytes = 528 (iRTPChannelRateAdaptationInfo.iRateAdaptationFreeBufferSpaceInBytes - jbStats.packetSizeInBytesLeftInBuffer); 529 } 530 PVMF_JB_LOG_RTCPDATATRAFFIC_OUT((0, "PVMFJitterBufferNode::ComposeAndSendFeedBackPacket: Total=%d, Occ=%d, freebufferspace = %d", 531 iRTPChannelRateAdaptationInfo.iRateAdaptationFreeBufferSpaceInBytes, 532 jbStats.packetSizeInBytesLeftInBuffer, 533 fbsInBytes)); 534 App.pss0_app_data.freebufferspace = OSCL_STATIC_CAST(uint16, (fbsInBytes) / 64); 535 iRTPChannelRateAdaptationInfo.iRateAdaptationRTCPRRCount = 0; 536 appPtr = &App; 537 PVMF_JB_LOG_RTCPDATATRAFFIC_OUT((0, "NADU_PKT: Mime=%s, PDelay=%d, FBS_BYTES=%d, FBS=%d, NSN=%d", 538 irRTPDataJitterBuffer.GetMimeType(), 539 App.pss0_app_data.playoutdelayinms, 540 fbsInBytes, 541 App.pss0_app_data.freebufferspace, 542 App.pss0_app_data.nsn)); 543 } 544 } 545 if (rtcpEncode.EncodeCompoundRR(*pRR, 546 memFrag, 547 appPtr) != RTCP_Encoder::RTCP_SUCCESS) 548 { 549 PVMF_JB_LOG_RTCPDATATRAFFIC_OUT((0, "0x%x PVMFJitterBufferNode::ComposeAndSendFeedBackPacket: EncodeCompoundRR failed", this)); 550 return PVMFFailure; 551 } 552 rtcpOut->setMediaFragFilledLen(0, memFrag.len); 553 554 555 // update average packet length - treat compound packets as single 556 iRTCPStats.avg_rtcp_compound_pkt_size = OSCL_STATIC_CAST(float, (memFrag.len + 15.0 * iRTCPStats.avg_rtcp_compound_pkt_size) / 16.0); 557 558 559 560 PVMFSharedMediaMsgPtr rtcpMsg; 561 convertToPVMFMediaMsg(rtcpMsg, rtcpOut); 562 563 ipObserver->RTCPRRReadyToSend(ipFeedbackPort, rtcpMsg); 564 565 iInitialRTCPPacket = false; 566 return PVMFSuccess; 567 } 568 569 /////////////////////////////////////////////////////////////////////////////// 570 //PVRTCPProtoImplementor Implementation 571 /////////////////////////////////////////////////////////////////////////////// 572 OSCL_EXPORT_REF PVRTCPProtoImplementor* PVRTCPProtoImplementor::New(PVMFMediaClock& aClientPlayBackClock, PVMFMediaClock& aRTCPClock, PVRTCPProtoImplementorObserver* aEventNotifier, bool aBroadcastSession) 573 { 574 int32 err = OsclErrNone; 575 PVRTCPProtoImplementor* pRtcpProtoImplementor = NULL; 576 OSCL_TRY(err, pRtcpProtoImplementor = OSCL_NEW(PVRTCPProtoImplementor, (aClientPlayBackClock, aRTCPClock, aEventNotifier, aBroadcastSession)); 577 if (pRtcpProtoImplementor) 578 { 579 pRtcpProtoImplementor->Construct(); 580 } 581 ); 582 583 if (OsclErrNone != err && pRtcpProtoImplementor) 584 { 585 if (pRtcpProtoImplementor) 586 OSCL_DELETE(pRtcpProtoImplementor); 587 588 pRtcpProtoImplementor = NULL; 589 } 590 591 return pRtcpProtoImplementor; 592 } 593 594 PVRTCPProtoImplementor::PVRTCPProtoImplementor(PVMFMediaClock& aClientPlayBackClock, PVMFMediaClock& aRTCPClock, PVRTCPProtoImplementorObserver* aObserver, bool aBroadcastSession) 595 : iBroadcastSession(aBroadcastSession) 596 , irClientPlayBackClock(aClientPlayBackClock) 597 , irRTCPClock(aRTCPClock) 598 , ipObserver(aObserver) 599 { 600 ipRTCPDataPathLoggerIn = NULL; 601 ipRTCPAVSyncLogger = NULL; 602 ResetParams(); 603 } 604 605 void PVRTCPProtoImplementor::ResetParams(bool aMemoryCleanUp) 606 { 607 OSCL_UNUSED_ARG(aMemoryCleanUp); 608 iPerformRTCPBasedAVSync = iBroadcastSession ? true : false; 609 iRTCPAVSyncProcessed = false; 610 iPlayStopTimeAvailable = false; 611 } 612 613 void PVRTCPProtoImplementor::Construct() 614 { 615 ipRTCPDataPathLoggerIn = PVLogger::GetLoggerObject("datapath.sourcenode.jitterbuffer.rtcp.in"); 616 ipRTCPAVSyncLogger = PVLogger::GetLoggerObject("PVRTCPProtoImplementor.rtcp.avsync"); 617 } 618 619 OSCL_EXPORT_REF PVRTCPProtoImplementor::~PVRTCPProtoImplementor() 620 { 621 Reset(); 622 } 623 624 OSCL_EXPORT_REF void PVRTCPProtoImplementor::AddPVRTCPChannelController(PVRTCPChannelController* aPVRTCPChannelController) 625 { 626 iPVRTCPChannelController.push_back(aPVRTCPChannelController); 627 } 628 629 OSCL_EXPORT_REF void PVRTCPProtoImplementor::RemoveAllRTCPChannelControllers() 630 { 631 Oscl_Vector<PVRTCPChannelController*, OsclMemAllocator>::iterator iter; 632 for (iter = iPVRTCPChannelController.end() - 1; iter >= iPVRTCPChannelController.begin(); iter--) 633 { 634 OSCL_DELETE(*iter); 635 } 636 iPVRTCPChannelController.clear(); 637 } 638 639 OSCL_EXPORT_REF PVRTCPChannelController* PVRTCPProtoImplementor::GetRTCPChannelController(const PVMFPortInterface* aPort) const 640 { 641 PVRTCPChannelController* rtcpChannelController = NULL; 642 Oscl_Vector<PVRTCPChannelController*, OsclMemAllocator>::iterator iter; 643 for (iter = iPVRTCPChannelController.begin(); iter != iPVRTCPChannelController.end(); iter++) 644 { 645 PVRTCPChannelController* currRtcpChannelController = *iter; 646 if (currRtcpChannelController->CheckAssociatedFeedbackPort(aPort)) 647 { 648 rtcpChannelController = currRtcpChannelController; 649 break; 650 } 651 } 652 return rtcpChannelController; 653 } 654 655 OSCL_EXPORT_REF void PVRTCPProtoImplementor::StartRTCPMessageExchange() 656 { 657 Oscl_Vector<PVRTCPChannelController*, OsclMemAllocator>::iterator iter; 658 for (iter = iPVRTCPChannelController.begin(); iter != iPVRTCPChannelController.end(); iter++) 659 { 660 PVRTCPChannelController* rtcpChannelController = *iter; 661 rtcpChannelController->StartRTCPMessageExchange(); 662 } 663 } 664 665 OSCL_EXPORT_REF void PVRTCPProtoImplementor::PauseRTCPMessageExchange() 666 { 667 Oscl_Vector<PVRTCPChannelController*, OsclMemAllocator>::iterator iter; 668 for (iter = iPVRTCPChannelController.begin(); iter != iPVRTCPChannelController.end(); iter++) 669 { 670 PVRTCPChannelController* rtcpChannelController = *iter; 671 rtcpChannelController->PauseRTCPMessageExchange(); 672 } 673 } 674 675 OSCL_EXPORT_REF void PVRTCPProtoImplementor::StopRTCPMessageExchange() 676 { 677 Oscl_Vector<PVRTCPChannelController*, OsclMemAllocator>::iterator iter; 678 for (iter = iPVRTCPChannelController.begin(); iter != iPVRTCPChannelController.end(); iter++) 679 { 680 PVRTCPChannelController* rtcpChannelController = *iter; 681 rtcpChannelController->StopRTCPMessageExchange(); 682 } 683 } 684 685 OSCL_EXPORT_REF void PVRTCPProtoImplementor::Reset() 686 { 687 Oscl_Vector<PVRTCPChannelController*, OsclMemAllocator>::iterator iter; 688 for (iter = iPVRTCPChannelController.begin(); iter != iPVRTCPChannelController.end(); iter++) 689 { 690 PVRTCPChannelController* rtcpChannelController = *iter; 691 rtcpChannelController->Reset(); 692 } 693 ResetParams(true); 694 } 695 696 OSCL_EXPORT_REF PVMFStatus PVRTCPProtoImplementor::ProcessRTCPReport(PVMFPortInterface* aFeedbackPort, PVMFSharedMediaMsgPtr& aMsg) 697 { 698 PVMFStatus status = PVMFFailure; 699 PVRTCPChannelController* rtcpChannelController = GetRTCPChannelController(aFeedbackPort); 700 if (rtcpChannelController) 701 { 702 status = rtcpChannelController->ProcessRTCPReport(aMsg); 703 } 704 return status; 705 } 706 707 OSCL_EXPORT_REF void PVRTCPProtoImplementor::SetRTCPIntervalInMicroSecs(uint32 aRTCPInterval) 708 { 709 Oscl_Vector<PVRTCPChannelController*, OsclMemAllocator>::iterator iter; 710 for (iter = iPVRTCPChannelController.begin(); iter != iPVRTCPChannelController.end(); iter++) 711 { 712 PVRTCPChannelController* rtcpChannelController = *iter; 713 rtcpChannelController->SetRTCPIntervalInMicroSecs(aRTCPInterval); 714 } 715 } 716 717 OSCL_EXPORT_REF bool PVRTCPProtoImplementor::SetPortRTCPParams(PVMFPortInterface* aPort, int aNumSenders, uint32 aRR, uint32 aRS) 718 { 719 bool retval = false; 720 PVRTCPChannelController* rtcpChannelController = GetRTCPChannelController(aPort); 721 if (rtcpChannelController) 722 { 723 rtcpChannelController->SetPortRTCPParams(aNumSenders, aRR, aRS); 724 retval = true; 725 } 726 return retval; 727 } 728 729 OSCL_EXPORT_REF void PVRTCPProtoImplementor::Prepare(bool aPlayAfterSeek) 730 { 731 Oscl_Vector<PVRTCPChannelController*, OsclMemAllocator>::iterator iter; 732 for (iter = iPVRTCPChannelController.begin(); iter != iPVRTCPChannelController.end(); iter++) 733 { 734 PVRTCPChannelController* pRTCPChannelController = *iter; 735 if (pRTCPChannelController) 736 { 737 pRTCPChannelController->Prepare(aPlayAfterSeek); 738 } 739 } 740 } 741 742 PVMFStatus PVRTCPProtoImplementor::RTCPSRReveived(PVRTCPChannelController* aChannelController) 743 { 744 OSCL_UNUSED_ARG(aChannelController); 745 PVMFStatus status = PVMFSuccess; 746 if (iPerformRTCPBasedAVSync && !iRTCPAVSyncProcessed) 747 { 748 bool ret = ProcessRTCPSRforAVSync(); 749 if (ret == false) 750 { 751 // No need to return error as perhaps there's not enough information yet 752 // to attempt a/v sync 753 return PVMFSuccess; 754 } 755 } 756 return status; 757 } 758 759 bool PVRTCPProtoImplementor::ProcessRTCPSRforAVSync() 760 { 761 // The following criteria must hold before the RTCP SRs can be processed for a/v sync 762 // a) The Jitter Buffers of all tracks have received at least one packet 763 // b) At least one RTCP report has been received for each track 764 // c) The wall clock value of the RTCP SRs is not zero 765 766 // Check the criteria 767 for (uint32 ii = 0; ii < iPVRTCPChannelController.size(); ii++) 768 { 769 PVRTCPChannelController* rtcpChannelController = iPVRTCPChannelController[ii]; 770 uint32 tsOffset = 0; 771 if (rtcpChannelController->GetJitterBuffer().GetRTPTimeStampOffset(tsOffset) == false) 772 { 773 return false; 774 } 775 776 if (rtcpChannelController->GetRTCPStats().lastSenderReportRecvTime == OSCL_STATIC_CAST(uint64, 0)) 777 { 778 return false; 779 } 780 781 if (rtcpChannelController->GetRTCPStats().lastSenderReportNTP == OSCL_STATIC_CAST(uint64, 0)) 782 { 783 return false; 784 } 785 } 786 787 // temporary vector to save the calculated init ntp for each track 788 Oscl_Vector<uint64, OsclMemAllocator> initNtpTracks; 789 790 // temporary vector to save the calculated rtp timebase of each track 791 Oscl_Vector<uint32, OsclMemAllocator> RTPTBArray; 792 793 // Initialize temporary vectors 794 int32 err = OsclErrNone; 795 OSCL_TRY(err, initNtpTracks.reserve(iPVRTCPChannelController.size())); 796 if (err != OsclErrNone) 797 return false; 798 799 OSCL_TRY(err, RTPTBArray.push_back(iPVRTCPChannelController.size())); 800 if (err != OsclErrNone) 801 return false; 802 803 for (uint32 tt = 0; tt < iPVRTCPChannelController.size(); tt++) 804 { 805 initNtpTracks.push_back(0); 806 RTPTBArray.push_back(0); 807 } 808 809 810 // Find the track whose first rtp packet correspond to the smallest NTP 811 uint32 lowestNTPIndex = 0; 812 uint64 lowestNTP = 0; 813 for (uint32 jj = 0; jj < iPVRTCPChannelController.size(); jj++) 814 { 815 uint32 firstRTP; 816 iPVRTCPChannelController[jj]->GetJitterBuffer().GetRTPTimeStampOffset(firstRTP); 817 uint32 timescale = iPVRTCPChannelController[jj]->GetJitterBuffer().GetTimeScale(); 818 uint32 srRTP = iPVRTCPChannelController[jj]->GetRTCPStats().lastSenderReportRTP; 819 uint64 srNTP = iPVRTCPChannelController[jj]->GetRTCPStats().lastSenderReportNTP; 820 821 uint32 deltaRTP = 0; 822 if (srRTP >= firstRTP) 823 { 824 deltaRTP = srRTP - firstRTP; 825 } 826 else 827 { 828 deltaRTP = firstRTP - srRTP; 829 } 830 831 uint64 deltaRTPInNTPFormat = ((uint64) deltaRTP / (uint64)timescale) << 32; 832 deltaRTPInNTPFormat += ((uint64) deltaRTP % (uint64)timescale) * (uint64)0xFFFFFFFF / (uint64)timescale; 833 834 uint64 initNTP = 0; 835 if (srRTP >= firstRTP) 836 { 837 initNTP = srNTP - deltaRTPInNTPFormat; 838 } 839 else 840 { 841 initNTP = srNTP + deltaRTPInNTPFormat; 842 } 843 844 845 if (jj == 0) 846 { 847 lowestNTPIndex = jj; 848 lowestNTP = initNTP; 849 } 850 else if (initNTP < lowestNTP) 851 { 852 lowestNTPIndex = jj; 853 lowestNTP = initNTP; 854 } 855 856 // Save the reference ntp value 857 initNtpTracks[jj] = initNTP; 858 859 PVMF_JB_LOG_RTCP_AVSYNC((0, 860 "PVMFJitterBufferNode::ProcessRTCPSRforAVSync(): srRTP=%d, firstRTP=%d, timescale=%d srNTPHigh=0x%x, srNTPLow=0x%x initNTPHigh=0x%x initNTPLow=0x%x deltaRTPHigh=0x%x deltaRTPLow=0x%x", 861 srRTP, firstRTP, timescale, Oscl_Int64_Utils::get_uint64_upper32(srNTP), Oscl_Int64_Utils::get_uint64_lower32(srNTP), 862 Oscl_Int64_Utils::get_uint64_upper32(initNTP), Oscl_Int64_Utils::get_uint64_lower32(initNTP), 863 Oscl_Int64_Utils::get_uint64_upper32(deltaRTPInNTPFormat), Oscl_Int64_Utils::get_uint64_lower32(deltaRTPInNTPFormat))); 864 865 } 866 867 868 // Calculate the new timebase for all tracks 869 for (uint32 kk = 0; kk < iPVRTCPChannelController.size(); kk++) 870 { 871 872 uint32 firstRTP; 873 iPVRTCPChannelController[kk]->GetJitterBuffer().GetRTPTimeStampOffset(firstRTP); 874 875 if (kk == lowestNTPIndex) 876 { 877 // Just set the RTP TB to the first rtp packet 878 RTPTBArray[kk] = firstRTP; 879 } 880 else 881 { 882 uint64 initNTP = initNtpTracks[kk]; 883 uint32 timescale = iPVRTCPChannelController[kk]->GetJitterBuffer().GetTimeScale(); 884 885 OSCL_ASSERT(lowestNTP <= initNTP); 886 887 uint64 deltaNTP = initNTP - lowestNTP; 888 uint32 deltaNTPInRTPUnits = OSCL_STATIC_CAST(uint32, ((deltaNTP * (uint64)timescale) + (uint64)0x80000000) >> 32); 889 uint32 rtpTimeBase = firstRTP - deltaNTPInRTPUnits; 890 RTPTBArray[kk] = rtpTimeBase; 891 } 892 } 893 894 #if (PVLOGGER_INST_LEVEL > PVLOGMSG_INST_LLDBG) 895 // Log parameters 896 for (uint32 mm = 0; mm < iPVRTCPChannelController.size(); mm++) 897 { 898 uint32 firstRTP; 899 iPVRTCPChannelController[mm]->GetJitterBuffer().GetRTPTimeStampOffset(firstRTP); 900 uint32 timescale = iPVRTCPChannelController[mm]->GetJitterBuffer().GetTimeScale(); 901 uint32 srRTP = iPVRTCPChannelController[mm]->GetRTCPStats().lastSenderReportRTP; 902 uint64 srNTP = iPVRTCPChannelController[mm]->GetRTCPStats().lastSenderReportNTP; 903 int32 delta = ((firstRTP - RTPTBArray[mm]) * 1000) / timescale; 904 uint32 srNTPHigh = Oscl_Int64_Utils::get_uint64_upper32(srNTP); 905 srNTP = srNTP & uint64(0xffffffff); 906 srNTP *= uint64(1000000); 907 srNTP += uint64(500000); 908 srNTP = srNTP / uint64(0xffffffff); 909 uint32 srNTPLow = Oscl_Int64_Utils::get_uint64_lower32(srNTP); 910 911 PVMF_JB_LOG_RTCP_AVSYNC((0, 912 "Stream %d: mime=%s timeScale=%uHz firstTS=%u RTCP.RTP=%u RTCP.NTP=%u.%06u newTB=%u delta=%dms\n", 913 mm, 914 iPVRTCPChannelController[mm]->GetJitterBuffer().GetMimeType(), 915 timescale, 916 firstRTP, 917 srRTP, 918 srNTPHigh, 919 srNTPLow, 920 RTPTBArray[mm], 921 delta 922 ) 923 ); 924 925 } 926 #endif 927 928 // Adjust the RTP TB 929 for (uint32 ll = 0; ll < iPVRTCPChannelController.size(); ll++) 930 { 931 iPVRTCPChannelController[ll]->GetJitterBuffer().SetRTPTimeStampOffset(RTPTBArray[ll]); 932 } 933 934 //Notify SM plugin that RTP TB data is available for PVR purposes 935 // No need to create a public class to publish the format of the information sent in this event 936 // Just define this structure internally. The only client of this event is the SM broadcast 937 // plugin, so it's the only component that needs to be aware of this format 938 struct RTPTBInfoEventData 939 { 940 const PvmfMimeString* mimeType; 941 uint32 rtpTB; 942 }; 943 944 for (uint32 nn = 0; nn < iPVRTCPChannelController.size(); nn++) 945 { 946 OSCL_HeapString<OsclMemAllocator> mimeStr(iPVRTCPChannelController[nn]->GetJitterBuffer().GetMimeType()); 947 948 RTPTBInfoEventData infoData; 949 infoData.mimeType = &mimeStr; 950 infoData.rtpTB = RTPTBArray[nn]; 951 PVMFAsyncEvent infoEvent(PVMFInfoEvent, PVMFJitterBufferNodeRTCPDataProcessed, NULL, (OsclAny*)(&infoData)); 952 ipObserver->ProcessInfoEvent(infoEvent); 953 } 954 955 iRTCPAVSyncProcessed = true; 956 return true; 957 } 958 959 PVMFStatus PVRTCPProtoImplementor::RTCPByeReceived(PVRTCPChannelController* aChannelController) 960 { 961 PVMF_JB_LOG_RTCPDATATRAFFIC_IN_E((0, "PVRTCPProtoImplementor::RTCPByeReceived for mime %s", aChannelController->GetJitterBuffer().GetMimeType())); 962 ipObserver->RTCPPacketReceived(BYE_RTCP_PACKET, aChannelController); 963 aChannelController->GetJitterBuffer().SetEOS(true); 964 return PVMFSuccess; 965 } 966 967 PVMFStatus PVRTCPProtoImplementor::RTCPRRReadyToSend(PVMFPortInterface*& aPort, PVMFSharedMediaMsgPtr& aMessage) 968 { 969 ipObserver->RTCPReportReadyToSend(aPort, aMessage); 970 return PVMFSuccess; 971 } 972