1 /*
2 * Copyright (C) 2010 The Android Open Source Project
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 express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "ARTPSource"
19 #include <utils/Log.h>
20
21 #include "ARTPSource.h"
22
23 #include "AAMRAssembler.h"
24 #include "AAVCAssembler.h"
25 #include "AHEVCAssembler.h"
26 #include "AH263Assembler.h"
27 #include "AMPEG2TSAssembler.h"
28 #include "AMPEG4AudioAssembler.h"
29 #include "AMPEG4ElementaryAssembler.h"
30 #include "ARawAudioAssembler.h"
31 #include "ASessionDescription.h"
32
33 #include <media/stagefright/foundation/ABuffer.h>
34 #include <media/stagefright/foundation/ADebug.h>
35 #include <media/stagefright/foundation/AMessage.h>
36
37 #include <strings.h>
38
39 namespace android {
40
41 static uint32_t kSourceID = 0xdeadbeef;
42
ARTPSource(uint32_t id,const sp<ASessionDescription> & sessionDesc,size_t index,const sp<AMessage> & notify)43 ARTPSource::ARTPSource(
44 uint32_t id,
45 const sp<ASessionDescription> &sessionDesc, size_t index,
46 const sp<AMessage> ¬ify)
47 : mFirstRtpTime(0),
48 mFirstSysTime(0),
49 mClockRate(0),
50 mSysAnchorTime(0),
51 mLastSysAnchorTimeUpdatedUs(0),
52 mFirstSsrc(0),
53 mHighestNackNumber(0),
54 mID(id),
55 mHighestSeqNumber(0),
56 mPrevExpected(0),
57 mBaseSeqNumber(0),
58 mNumBuffersReceived(0),
59 mPrevNumBuffersReceived(0),
60 mPrevExpectedForRR(0),
61 mPrevNumBuffersReceivedForRR(0),
62 mLatestRtpTime(0),
63 mStaticJbTimeMs(kStaticJitterTimeMs),
64 mLastSrRtpTime(0),
65 mLastSrNtpTime(0),
66 mLastSrUpdateTimeUs(0),
67 mIsFirstRtpRtcpGap(true),
68 mAvgRtpRtcpGapMs(0),
69 mAvgUnderlineDelayMs(0),
70 mIssueFIRRequests(false),
71 mIssueFIRByAssembler(false),
72 mLastFIRRequestUs(-1),
73 mNextFIRSeqNo((rand() * 256.0) / RAND_MAX),
74 mNotify(notify) {
75 unsigned long PT;
76 AString desc;
77 AString params;
78 sessionDesc->getFormatType(index, &PT, &desc, ¶ms);
79
80 if (!strncmp(desc.c_str(), "H264/", 5)) {
81 mAssembler = new AAVCAssembler(notify);
82 mIssueFIRRequests = true;
83 } else if (!strncmp(desc.c_str(), "H265/", 5)) {
84 mAssembler = new AHEVCAssembler(notify);
85 mIssueFIRRequests = true;
86 } else if (!strncmp(desc.c_str(), "MP4A-LATM/", 10)) {
87 mAssembler = new AMPEG4AudioAssembler(notify, params);
88 } else if (!strncmp(desc.c_str(), "H263-1998/", 10)
89 || !strncmp(desc.c_str(), "H263-2000/", 10)) {
90 mAssembler = new AH263Assembler(notify);
91 mIssueFIRRequests = true;
92 } else if (!strncmp(desc.c_str(), "AMR/", 4)) {
93 mAssembler = new AAMRAssembler(notify, false /* isWide */, params);
94 } else if (!strncmp(desc.c_str(), "AMR-WB/", 7)) {
95 mAssembler = new AAMRAssembler(notify, true /* isWide */, params);
96 } else if (!strncmp(desc.c_str(), "MP4V-ES/", 8)
97 || !strncasecmp(desc.c_str(), "mpeg4-generic/", 14)) {
98 mAssembler = new AMPEG4ElementaryAssembler(notify, desc, params);
99 mIssueFIRRequests = true;
100 } else if (ARawAudioAssembler::Supports(desc.c_str())) {
101 mAssembler = new ARawAudioAssembler(notify, desc.c_str(), params);
102 } else if (!strncasecmp(desc.c_str(), "MP2T/", 5)) {
103 mAssembler = new AMPEG2TSAssembler(notify, desc.c_str(), params);
104 } else {
105 TRESPASS();
106 }
107
108 if (mAssembler != NULL && !mAssembler->initCheck()) {
109 mAssembler.clear();
110 }
111
112 int32_t clockRate, numChannels;
113 ASessionDescription::ParseFormatDesc(desc.c_str(), &clockRate, &numChannels);
114 mClockRate = clockRate;
115 mLastJbAlarmTimeUs = 0;
116 mJitterCalc = new JitterCalc(mClockRate);
117 }
118
AbsDiff(uint32_t seq1,uint32_t seq2)119 static uint32_t AbsDiff(uint32_t seq1, uint32_t seq2) {
120 return seq1 > seq2 ? seq1 - seq2 : seq2 - seq1;
121 }
122
processRTPPacket(const sp<ABuffer> & buffer)123 void ARTPSource::processRTPPacket(const sp<ABuffer> &buffer) {
124 if (mAssembler != NULL && queuePacket(buffer)) {
125 mAssembler->onPacketReceived(this);
126 }
127 }
128
processRTPPacket()129 void ARTPSource::processRTPPacket() {
130 if (mAssembler != NULL && !mQueue.empty()) {
131 mAssembler->onPacketReceived(this);
132 }
133 }
134
timeUpdate(uint32_t rtpTime,uint64_t ntpTime)135 void ARTPSource::timeUpdate(uint32_t rtpTime, uint64_t ntpTime) {
136 mLastSrRtpTime = rtpTime;
137 mLastSrNtpTime = ntpTime;
138 mLastSrUpdateTimeUs = ALooper::GetNowUs();
139
140 sp<AMessage> notify = mNotify->dup();
141 notify->setInt32("time-update", true);
142 notify->setInt32("rtp-time", rtpTime);
143 notify->setInt64("ntp-time", ntpTime);
144 notify->setInt32("rtcp-event", 1);
145 notify->setInt32("payload-type", RTCP_SR);
146 notify->setInt64("recv-time-us", mLastSrUpdateTimeUs);
147 notify->post();
148 }
149
timeReset()150 void ARTPSource::timeReset() {
151 mFirstRtpTime = 0;
152 mFirstSysTime = 0;
153 mSysAnchorTime = 0;
154 mLastSysAnchorTimeUpdatedUs = 0;
155 mFirstSsrc = 0;
156 mHighestNackNumber = 0;
157 mHighestSeqNumber = 0;
158 mPrevExpected = 0;
159 mBaseSeqNumber = 0;
160 mNumBuffersReceived = 0;
161 mPrevNumBuffersReceived = 0;
162 mPrevExpectedForRR = 0;
163 mPrevNumBuffersReceivedForRR = 0;
164 mLatestRtpTime = 0;
165 mLastSrRtpTime = 0;
166 mLastSrNtpTime = 0;
167 mLastSrUpdateTimeUs = 0;
168 mIsFirstRtpRtcpGap = true;
169 mAvgRtpRtcpGapMs = 0;
170 mAvgUnderlineDelayMs = 0;
171 mIssueFIRByAssembler = false;
172 mLastFIRRequestUs = -1;
173 }
174
calcTimeGapRtpRtcp(const sp<ABuffer> & buffer,int64_t nowUs)175 void ARTPSource::calcTimeGapRtpRtcp(const sp<ABuffer> &buffer, int64_t nowUs) {
176 if (mLastSrUpdateTimeUs == 0) {
177 return;
178 }
179
180 int64_t elapsedMs = (nowUs - mLastSrUpdateTimeUs) / 1000;
181 int64_t elapsedRtpTime = (elapsedMs * (mClockRate / 1000));
182 uint32_t rtpTime;
183 CHECK(buffer->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
184
185 int64_t anchorRtpTime = mLastSrRtpTime + elapsedRtpTime;
186 int64_t rtpTimeGap = anchorRtpTime - rtpTime;
187 // rtpTime can not be faster than it's anchor time.
188 // because rtpTime(of rtp packet) represents it's a frame captured time and
189 // anchorRtpTime(of rtcp:sr packet) represents it's a rtp packetized time.
190 if (rtpTimeGap < 0 || rtpTimeGap > (mClockRate * 60)) {
191 // ignore invalid delay gap such as negative delay or later than 1 min.
192 return;
193 }
194
195 int64_t rtpTimeGapMs = (rtpTimeGap * 1000 / mClockRate);
196 if (mIsFirstRtpRtcpGap) {
197 mIsFirstRtpRtcpGap = false;
198 mAvgRtpRtcpGapMs = rtpTimeGapMs;
199 } else {
200 // This is measuring avg rtp timestamp distance between rtp and rtcp:sr packet.
201 // Rtp timestamp of rtp packet represents it's raw frame captured time.
202 // Rtp timestamp of rtcp:sr packet represents it's packetization time.
203 // So that, this value is showing how much time delayed to be a rtp packet
204 // from a raw frame captured time.
205 // This value maybe referred to know a/v sync and sender's own delay of this media stream.
206 mAvgRtpRtcpGapMs = ((mAvgRtpRtcpGapMs * 15) + rtpTimeGapMs) / 16;
207 }
208 }
209
calcUnderlineDelay(const sp<ABuffer> & buffer,int64_t nowUs)210 void ARTPSource::calcUnderlineDelay(const sp<ABuffer> &buffer, int64_t nowUs) {
211 int64_t elapsedMs = (nowUs - mSysAnchorTime) / 1000;
212 int64_t elapsedRtpTime = (elapsedMs * (mClockRate / 1000));
213 int64_t expectedRtpTime = mFirstRtpTime + elapsedRtpTime;
214
215 int32_t rtpTime;
216 CHECK(buffer->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
217 int32_t delayMs = (expectedRtpTime - rtpTime) / (mClockRate / 1000);
218
219 mAvgUnderlineDelayMs = ((mAvgUnderlineDelayMs * 15) + delayMs) / 16;
220 }
221
adjustAnchorTimeIfRequired(int64_t nowUs)222 void ARTPSource::adjustAnchorTimeIfRequired(int64_t nowUs) {
223 if (nowUs - mLastSysAnchorTimeUpdatedUs < 1000000L) {
224 return;
225 }
226
227 if (mAvgUnderlineDelayMs < -30) {
228 // adjust underline delay a quarter of desired delay like step by step.
229 mSysAnchorTime += (int64_t)(mAvgUnderlineDelayMs * 1000 / 4);
230 ALOGD("anchor time updated: original(%lld), anchor(%lld), diffMs(%lld)",
231 (long long)mFirstSysTime, (long long)mSysAnchorTime,
232 (long long)(mFirstSysTime - mSysAnchorTime) / 1000);
233
234 mAvgUnderlineDelayMs = 0;
235 mLastSysAnchorTimeUpdatedUs = nowUs;
236
237 // reset a jitter stastics since an anchor time adjusted.
238 mJitterCalc->init(mFirstRtpTime, mSysAnchorTime, 0, mStaticJbTimeMs * 1000);
239 }
240 }
241
queuePacket(const sp<ABuffer> & buffer)242 bool ARTPSource::queuePacket(const sp<ABuffer> &buffer) {
243 int64_t nowUs = ALooper::GetNowUs();
244 uint32_t seqNum = (uint32_t)buffer->int32Data();
245 int32_t ssrc = 0, rtpTime = 0;
246
247 buffer->meta()->findInt32("ssrc", &ssrc);
248 CHECK(buffer->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
249 mLatestRtpTime = rtpTime;
250
251 if (mNumBuffersReceived++ == 0 && mFirstSysTime == 0) {
252 mFirstSysTime = nowUs;
253 mSysAnchorTime = nowUs;
254 mLastSysAnchorTimeUpdatedUs = nowUs;
255 mHighestSeqNumber = seqNum;
256 mBaseSeqNumber = seqNum;
257 mFirstRtpTime = rtpTime;
258 mFirstSsrc = ssrc;
259 ALOGD("first-rtp arrived: first-rtp-time=%u, sys-time=%lld, seq-num=%u, ssrc=%d",
260 mFirstRtpTime, (long long)mFirstSysTime, mHighestSeqNumber, mFirstSsrc);
261 mJitterCalc->init(mFirstRtpTime, mFirstSysTime, 0, mStaticJbTimeMs * 1000);
262 if (mQueue.size() > 0) {
263 ALOGD("clearing buffers which belonged to previous timeline"
264 " since a base timeline has been changed.");
265 mQueue.clear();
266 }
267 mQueue.push_back(buffer);
268 return true;
269 }
270
271 if (mFirstSsrc != ssrc) {
272 ALOGW("Discarding a buffer due to unexpected ssrc");
273 return false;
274 }
275
276 calcTimeGapRtpRtcp(buffer, nowUs);
277 calcUnderlineDelay(buffer, nowUs);
278 adjustAnchorTimeIfRequired(nowUs);
279
280 // Only the lower 16-bit of the sequence numbers are transmitted,
281 // derive the high-order bits by choosing the candidate closest
282 // to the highest sequence number (extended to 32 bits) received so far.
283
284 uint32_t seq1 = seqNum | (mHighestSeqNumber & 0xffff0000);
285
286 // non-overflowing version of:
287 // uint32_t seq2 = seqNum | ((mHighestSeqNumber & 0xffff0000) + 0x10000);
288 uint32_t seq2 = seqNum | (((mHighestSeqNumber >> 16) + 1) << 16);
289
290 // non-underflowing version of:
291 // uint32_t seq2 = seqNum | ((mHighestSeqNumber & 0xffff0000) - 0x10000);
292 uint32_t seq3 = seqNum | ((((mHighestSeqNumber >> 16) | 0x10000) - 1) << 16);
293
294 uint32_t diff1 = AbsDiff(seq1, mHighestSeqNumber);
295 uint32_t diff2 = AbsDiff(seq2, mHighestSeqNumber);
296 uint32_t diff3 = AbsDiff(seq3, mHighestSeqNumber);
297
298 if (diff1 < diff2) {
299 if (diff1 < diff3) {
300 // diff1 < diff2 ^ diff1 < diff3
301 seqNum = seq1;
302 } else {
303 // diff3 <= diff1 < diff2
304 seqNum = seq3;
305 }
306 } else if (diff2 < diff3) {
307 // diff2 <= diff1 ^ diff2 < diff3
308 seqNum = seq2;
309 } else {
310 // diff3 <= diff2 <= diff1
311 seqNum = seq3;
312 }
313
314 if (seqNum > mHighestSeqNumber) {
315 mHighestSeqNumber = seqNum;
316 }
317
318 buffer->setInt32Data(seqNum);
319
320 List<sp<ABuffer> >::iterator it = mQueue.begin();
321 while (it != mQueue.end() && (uint32_t)(*it)->int32Data() < seqNum) {
322 ++it;
323 }
324
325 if (it != mQueue.end() && (uint32_t)(*it)->int32Data() == seqNum) {
326 ALOGW("Discarding duplicate buffer");
327 return false;
328 }
329
330 mQueue.insert(it, buffer);
331
332 return true;
333 }
334
byeReceived()335 void ARTPSource::byeReceived() {
336 if (mAssembler != NULL) {
337 mAssembler->onByeReceived();
338 }
339 }
340
addFIR(const sp<ABuffer> & buffer)341 void ARTPSource::addFIR(const sp<ABuffer> &buffer) {
342 if (!mIssueFIRRequests && !mIssueFIRByAssembler) {
343 return;
344 }
345
346 bool send = false;
347 int64_t nowUs = ALooper::GetNowUs();
348 int64_t usecsSinceLastFIR = nowUs - mLastFIRRequestUs;
349 if (mLastFIRRequestUs < 0) {
350 // A first FIR, just send it.
351 send = true;
352 } else if (mIssueFIRByAssembler && (usecsSinceLastFIR > 1000000)) {
353 // A FIR issued by Assembler.
354 // Send it if last FIR is not sent within a sec.
355 send = true;
356 } else if (mIssueFIRRequests && (usecsSinceLastFIR > 5000000)) {
357 // A FIR issued periodically regardless packet loss.
358 // Send it if last FIR is not sent within 5 secs.
359 send = true;
360 }
361
362 if (!send) {
363 return;
364 }
365
366 mLastFIRRequestUs = nowUs;
367
368 if (buffer->size() + 20 > buffer->capacity()) {
369 ALOGW("RTCP buffer too small to accommodate FIR.");
370 return;
371 }
372
373 uint8_t *data = buffer->data() + buffer->size();
374
375 data[0] = 0x80 | 4;
376 data[1] = 206; // PSFB
377 data[2] = 0;
378 data[3] = 4; // total (4+1) * sizeof(int32_t) = 20 bytes
379 data[4] = kSourceID >> 24;
380 data[5] = (kSourceID >> 16) & 0xff;
381 data[6] = (kSourceID >> 8) & 0xff;
382 data[7] = kSourceID & 0xff;
383
384 data[8] = 0x00; // SSRC of media source (unused)
385 data[9] = 0x00;
386 data[10] = 0x00;
387 data[11] = 0x00;
388
389 data[12] = mID >> 24;
390 data[13] = (mID >> 16) & 0xff;
391 data[14] = (mID >> 8) & 0xff;
392 data[15] = mID & 0xff;
393
394 data[16] = mNextFIRSeqNo++; // Seq Nr.
395
396 data[17] = 0x00; // Reserved
397 data[18] = 0x00;
398 data[19] = 0x00;
399
400 buffer->setRange(buffer->offset(), buffer->size() + (data[3] + 1) * sizeof(int32_t));
401
402 mIssueFIRByAssembler = false;
403
404 ALOGV("Added FIR request.");
405 }
406
addReceiverReport(const sp<ABuffer> & buffer)407 void ARTPSource::addReceiverReport(const sp<ABuffer> &buffer) {
408 if (buffer->size() + 32 > buffer->capacity()) {
409 ALOGW("RTCP buffer too small to accommodate RR.");
410 return;
411 }
412
413 uint8_t fraction = 0;
414
415 // According to appendix A.3 in RFC 3550
416 uint32_t expected = mHighestSeqNumber - mBaseSeqNumber + 1;
417 int64_t intervalExpected = expected - mPrevExpectedForRR;
418 int64_t intervalReceived = mNumBuffersReceived - mPrevNumBuffersReceivedForRR;
419 int64_t intervalPacketLost = intervalExpected - intervalReceived;
420
421 if (intervalExpected > 0 && intervalPacketLost > 0) {
422 fraction = (intervalPacketLost << 8) / intervalExpected;
423 }
424
425 mPrevExpectedForRR = expected;
426 mPrevNumBuffersReceivedForRR = mNumBuffersReceived;
427 int32_t cumulativePacketLost = (int32_t)expected - mNumBuffersReceived;
428
429 uint8_t *data = buffer->data() + buffer->size();
430
431 data[0] = 0x80 | 1;
432 data[1] = 201; // RR
433 data[2] = 0;
434 data[3] = 7; // total (7+1) * sizeof(int32_t) = 32 bytes
435 data[4] = kSourceID >> 24;
436 data[5] = (kSourceID >> 16) & 0xff;
437 data[6] = (kSourceID >> 8) & 0xff;
438 data[7] = kSourceID & 0xff;
439
440 data[8] = mID >> 24;
441 data[9] = (mID >> 16) & 0xff;
442 data[10] = (mID >> 8) & 0xff;
443 data[11] = mID & 0xff;
444
445 data[12] = fraction; // fraction lost
446
447 data[13] = cumulativePacketLost >> 16; // cumulative lost
448 data[14] = (cumulativePacketLost >> 8) & 0xff;
449 data[15] = cumulativePacketLost & 0xff;
450
451 data[16] = mHighestSeqNumber >> 24;
452 data[17] = (mHighestSeqNumber >> 16) & 0xff;
453 data[18] = (mHighestSeqNumber >> 8) & 0xff;
454 data[19] = mHighestSeqNumber & 0xff;
455
456 uint32_t jitterTime = 0;
457 data[20] = jitterTime >> 24; // Interarrival jitter
458 data[21] = (jitterTime >> 16) & 0xff;
459 data[22] = (jitterTime >> 8) & 0xff;
460 data[23] = jitterTime & 0xff;
461
462 uint32_t LSR = 0;
463 uint32_t DLSR = 0;
464 if (mLastSrNtpTime != 0) {
465 LSR = (mLastSrNtpTime >> 16) & 0xffffffff;
466
467 DLSR = (uint32_t)
468 ((ALooper::GetNowUs() - mLastSrUpdateTimeUs) * 65536.0 / 1E6);
469 }
470
471 data[24] = LSR >> 24;
472 data[25] = (LSR >> 16) & 0xff;
473 data[26] = (LSR >> 8) & 0xff;
474 data[27] = LSR & 0xff;
475
476 data[28] = DLSR >> 24;
477 data[29] = (DLSR >> 16) & 0xff;
478 data[30] = (DLSR >> 8) & 0xff;
479 data[31] = DLSR & 0xff;
480
481 buffer->setRange(buffer->offset(), buffer->size() + (data[3] + 1) * sizeof(int32_t));
482 }
483
addTMMBR(const sp<ABuffer> & buffer,int32_t targetBitrate)484 void ARTPSource::addTMMBR(const sp<ABuffer> &buffer, int32_t targetBitrate) {
485 if (buffer->size() + 20 > buffer->capacity()) {
486 ALOGW("RTCP buffer too small to accommodate RR.");
487 return;
488 }
489
490 if (targetBitrate <= 0) {
491 return;
492 }
493
494 uint8_t *data = buffer->data() + buffer->size();
495
496 data[0] = 0x80 | 3; // TMMBR
497 data[1] = 205; // TSFB
498 data[2] = 0;
499 data[3] = 4; // total (4+1) * sizeof(int32_t) = 20 bytes
500 data[4] = kSourceID >> 24;
501 data[5] = (kSourceID >> 16) & 0xff;
502 data[6] = (kSourceID >> 8) & 0xff;
503 data[7] = kSourceID & 0xff;
504
505 *(int32_t*)(&data[8]) = 0; // 4 bytes blank
506
507 data[12] = mID >> 24;
508 data[13] = (mID >> 16) & 0xff;
509 data[14] = (mID >> 8) & 0xff;
510 data[15] = mID & 0xff;
511
512 // Find the first bit '1' from left & right side of the value.
513 int32_t leftEnd = 31 - __builtin_clz(targetBitrate);
514 int32_t rightEnd = ffs(targetBitrate) - 1;
515
516 // Mantissa have only 17bit space by RTCP specification.
517 if ((leftEnd - rightEnd) > 16) {
518 rightEnd = leftEnd - 16;
519 }
520 int32_t mantissa = targetBitrate >> rightEnd;
521
522 data[16] = ((rightEnd << 2) & 0xfc) | ((mantissa & 0x18000) >> 15);
523 data[17] = (mantissa & 0x07f80) >> 7;
524 data[18] = (mantissa & 0x0007f) << 1;
525 data[19] = 40; // 40 bytes overhead;
526
527 buffer->setRange(buffer->offset(), buffer->size() + (data[3] + 1) * sizeof(int32_t));
528
529 ALOGI("UE -> Op Req Rx bitrate : %d ", mantissa << rightEnd);
530 }
531
addNACK(const sp<ABuffer> & buffer)532 int ARTPSource::addNACK(const sp<ABuffer> &buffer) {
533 constexpr size_t kMaxFCIs = 10; // max number of FCIs
534 if (buffer->size() + (3 + kMaxFCIs) * sizeof(int32_t) > buffer->capacity()) {
535 ALOGW("RTCP buffer too small to accommodate NACK.");
536 return -1;
537 }
538
539 uint8_t *data = buffer->data() + buffer->size();
540
541 data[0] = 0x80 | 1; // Generic NACK
542 data[1] = 205; // TSFB
543 data[2] = 0;
544 data[3] = 0; // will be decided later
545 data[4] = kSourceID >> 24;
546 data[5] = (kSourceID >> 16) & 0xff;
547 data[6] = (kSourceID >> 8) & 0xff;
548 data[7] = kSourceID & 0xff;
549
550 data[8] = mID >> 24;
551 data[9] = (mID >> 16) & 0xff;
552 data[10] = (mID >> 8) & 0xff;
553 data[11] = mID & 0xff;
554
555 List<int> list;
556 List<int>::iterator it;
557 getSeqNumToNACK(list, kMaxFCIs);
558 size_t cnt = 0;
559
560 int *FCI = (int *)(data + 12);
561 for (it = list.begin(); it != list.end() && cnt < kMaxFCIs; it++) {
562 *(FCI + cnt) = *it;
563 cnt++;
564 }
565
566 data[3] = (3 + cnt) - 1; // total (3 + #ofFCI) * sizeof(int32_t) byte
567
568 buffer->setRange(buffer->offset(), buffer->size() + (data[3] + 1) * sizeof(int32_t));
569
570 return cnt;
571 }
572
getSeqNumToNACK(List<int> & list,int size)573 int ARTPSource::getSeqNumToNACK(List<int>& list, int size) {
574 AutoMutex _l(mMapLock);
575 int cnt = 0;
576
577 std::map<uint16_t, infoNACK>::iterator it;
578 for(it = mNACKMap.begin(); it != mNACKMap.end() && cnt < size; it++) {
579 infoNACK &info_it = it->second;
580 if (info_it.needToNACK) {
581 info_it.needToNACK = false;
582 // switch LSB to MSB for sending N/W
583 uint32_t FCI;
584 uint8_t *temp = (uint8_t *)&FCI;
585 temp[0] = (info_it.seqNum >> 8) & 0xff;
586 temp[1] = (info_it.seqNum) & 0xff;
587 temp[2] = (info_it.mask >> 8) & 0xff;
588 temp[3] = (info_it.mask) & 0xff;
589
590 list.push_back(FCI);
591 cnt++;
592 }
593 }
594
595 return cnt;
596 }
597
setSeqNumToNACK(uint16_t seqNum,uint16_t mask,uint16_t nowJitterHeadSeqNum)598 void ARTPSource::setSeqNumToNACK(uint16_t seqNum, uint16_t mask, uint16_t nowJitterHeadSeqNum) {
599 AutoMutex _l(mMapLock);
600 infoNACK info = {seqNum, mask, nowJitterHeadSeqNum, true};
601 std::map<uint16_t, infoNACK>::iterator it;
602
603 it = mNACKMap.find(seqNum);
604 if (it != mNACKMap.end()) {
605 infoNACK &info_it = it->second;
606 // renew if (mask or head seq) is changed
607 if ((info_it.mask != mask) || (info_it.nowJitterHeadSeqNum != nowJitterHeadSeqNum)) {
608 info_it = info;
609 }
610 } else {
611 mNACKMap[seqNum] = info;
612 }
613
614 // delete all NACK far from current Jitter's first sequence number
615 it = mNACKMap.begin();
616 while (it != mNACKMap.end()) {
617 infoNACK &info_it = it->second;
618
619 int diff = nowJitterHeadSeqNum - info_it.nowJitterHeadSeqNum;
620 if (diff > 100) {
621 ALOGV("Delete %d pkt from NACK map ", info_it.seqNum);
622 it = mNACKMap.erase(it);
623 } else {
624 it++;
625 }
626 }
627
628 }
629
getSelfID()630 uint32_t ARTPSource::getSelfID() {
631 return kSourceID;
632 }
633
setSelfID(const uint32_t selfID)634 void ARTPSource::setSelfID(const uint32_t selfID) {
635 kSourceID = selfID;
636 }
637
setPeriodicFIR(bool enable)638 void ARTPSource::setPeriodicFIR(bool enable) {
639 ALOGD("setPeriodicFIR %d", enable);
640 mIssueFIRRequests = enable;
641 }
642
getStaticJitterTimeMs()643 int32_t ARTPSource::getStaticJitterTimeMs() {
644 return mStaticJbTimeMs;
645 }
646
getBaseJitterTimeMs()647 int32_t ARTPSource::getBaseJitterTimeMs() {
648 return mJitterCalc->getBaseJitterMs();
649 }
650
getInterArrivalJitterTimeMs()651 int32_t ARTPSource::getInterArrivalJitterTimeMs() {
652 return mJitterCalc->getInterArrivalJitterMs();
653 }
654
setStaticJitterTimeMs(const uint32_t jbTimeMs)655 void ARTPSource::setStaticJitterTimeMs(const uint32_t jbTimeMs) {
656 mStaticJbTimeMs = jbTimeMs;
657 }
658
putBaseJitterData(uint32_t timeStamp,int64_t arrivalTime)659 void ARTPSource::putBaseJitterData(uint32_t timeStamp, int64_t arrivalTime) {
660 mJitterCalc->putBaseData(timeStamp, arrivalTime);
661 }
662
putInterArrivalJitterData(uint32_t timeStamp,int64_t arrivalTime)663 void ARTPSource::putInterArrivalJitterData(uint32_t timeStamp, int64_t arrivalTime) {
664 mJitterCalc->putInterArrivalData(timeStamp, arrivalTime);
665 }
666
setJbTimer(const sp<AMessage> timer)667 void ARTPSource::setJbTimer(const sp<AMessage> timer) {
668 mJbTimer = timer;
669 }
670
setJbAlarmTime(int64_t nowTimeUs,int64_t alarmAfterUs)671 void ARTPSource::setJbAlarmTime(int64_t nowTimeUs, int64_t alarmAfterUs) {
672 if (mJbTimer == NULL) {
673 return;
674 }
675 int64_t alarmTimeUs = nowTimeUs + alarmAfterUs;
676 bool alarm = false;
677 if (mLastJbAlarmTimeUs <= nowTimeUs) {
678 // no more alarm in pending.
679 mLastJbAlarmTimeUs = nowTimeUs + alarmAfterUs;
680 alarm = true;
681 } else if (mLastJbAlarmTimeUs > alarmTimeUs + 5000L) {
682 // bring an alarm forward more than 5ms.
683 mLastJbAlarmTimeUs = alarmTimeUs;
684 alarm = true;
685 } else {
686 // would not set alarm if it is close with before one.
687 }
688
689 if (alarm) {
690 sp<AMessage> notify = mJbTimer->dup();
691 notify->setObject("source", this);
692 notify->post(alarmAfterUs);
693 }
694 }
695
isNeedToEarlyNotify()696 bool ARTPSource::isNeedToEarlyNotify() {
697 uint32_t expected = mHighestSeqNumber - mBaseSeqNumber + 1;
698 int32_t intervalExpectedInNow = expected - mPrevExpected;
699 int32_t intervalReceivedInNow = mNumBuffersReceived - mPrevNumBuffersReceived;
700
701 if (intervalExpectedInNow - intervalReceivedInNow > 5)
702 return true;
703 return false;
704 }
705
notifyPktInfo(int32_t bitrate,int64_t nowUs,bool isRegular)706 void ARTPSource::notifyPktInfo(int32_t bitrate, int64_t nowUs, bool isRegular) {
707 int32_t payloadType = isRegular ? RTP_QUALITY : RTP_QUALITY_EMC;
708
709 sp<AMessage> notify = mNotify->dup();
710 notify->setInt32("rtcp-event", 1);
711 notify->setInt32("payload-type", payloadType);
712 notify->setInt32("feedback-type", 0);
713 // sending target bitrate up to application to share rtp quality.
714 notify->setInt32("bit-rate", bitrate);
715 notify->setInt32("highest-seq-num", mHighestSeqNumber);
716 notify->setInt32("base-seq-num", mBaseSeqNumber);
717 notify->setInt32("prev-expected", mPrevExpected);
718 notify->setInt32("num-buf-recv", mNumBuffersReceived);
719 notify->setInt32("prev-num-buf-recv", mPrevNumBuffersReceived);
720 notify->setInt32("latest-rtp-time", mLatestRtpTime);
721 notify->setInt64("recv-time-us", nowUs);
722 notify->setInt32("rtp-jitter-time-ms",
723 std::max(getBaseJitterTimeMs(), getStaticJitterTimeMs()));
724 notify->setInt32("rtp-rtcpsr-time-gap-ms", (int32_t)mAvgRtpRtcpGapMs);
725 notify->post();
726
727 if (isRegular) {
728 uint32_t expected = mHighestSeqNumber - mBaseSeqNumber + 1;
729 mPrevExpected = expected;
730 mPrevNumBuffersReceived = mNumBuffersReceived;
731 }
732 }
733
onIssueFIRByAssembler()734 void ARTPSource::onIssueFIRByAssembler() {
735 mIssueFIRByAssembler = true;
736 }
737
noticeAbandonBuffer(int cnt)738 void ARTPSource::noticeAbandonBuffer(int cnt) {
739 mNumBuffersReceived -= cnt;
740 }
741 } // namespace android
742