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