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 "AH263Assembler.h"
26 #include "AMPEG2TSAssembler.h"
27 #include "AMPEG4AudioAssembler.h"
28 #include "AMPEG4ElementaryAssembler.h"
29 #include "ARawAudioAssembler.h"
30 #include "ASessionDescription.h"
31
32 #include <media/stagefright/foundation/ABuffer.h>
33 #include <media/stagefright/foundation/ADebug.h>
34 #include <media/stagefright/foundation/AMessage.h>
35
36 namespace android {
37
38 static const uint32_t kSourceID = 0xdeadbeef;
39
ARTPSource(uint32_t id,const sp<ASessionDescription> & sessionDesc,size_t index,const sp<AMessage> & notify)40 ARTPSource::ARTPSource(
41 uint32_t id,
42 const sp<ASessionDescription> &sessionDesc, size_t index,
43 const sp<AMessage> ¬ify)
44 : mID(id),
45 mHighestSeqNumber(0),
46 mPrevExpected(0),
47 mBaseSeqNumber(0),
48 mNumBuffersReceived(0),
49 mPrevNumBuffersReceived(0),
50 mLastNTPTime(0),
51 mLastNTPTimeUpdateUs(0),
52 mIssueFIRRequests(false),
53 mLastFIRRequestUs(-1),
54 mNextFIRSeqNo((rand() * 256.0) / RAND_MAX),
55 mNotify(notify) {
56 unsigned long PT;
57 AString desc;
58 AString params;
59 sessionDesc->getFormatType(index, &PT, &desc, ¶ms);
60
61 if (!strncmp(desc.c_str(), "H264/", 5)) {
62 mAssembler = new AAVCAssembler(notify);
63 mIssueFIRRequests = true;
64 } else if (!strncmp(desc.c_str(), "MP4A-LATM/", 10)) {
65 mAssembler = new AMPEG4AudioAssembler(notify, params);
66 } else if (!strncmp(desc.c_str(), "H263-1998/", 10)
67 || !strncmp(desc.c_str(), "H263-2000/", 10)) {
68 mAssembler = new AH263Assembler(notify);
69 mIssueFIRRequests = true;
70 } else if (!strncmp(desc.c_str(), "AMR/", 4)) {
71 mAssembler = new AAMRAssembler(notify, false /* isWide */, params);
72 } else if (!strncmp(desc.c_str(), "AMR-WB/", 7)) {
73 mAssembler = new AAMRAssembler(notify, true /* isWide */, params);
74 } else if (!strncmp(desc.c_str(), "MP4V-ES/", 8)
75 || !strncasecmp(desc.c_str(), "mpeg4-generic/", 14)) {
76 mAssembler = new AMPEG4ElementaryAssembler(notify, desc, params);
77 mIssueFIRRequests = true;
78 } else if (ARawAudioAssembler::Supports(desc.c_str())) {
79 mAssembler = new ARawAudioAssembler(notify, desc.c_str(), params);
80 } else if (!strncasecmp(desc.c_str(), "MP2T/", 5)) {
81 mAssembler = new AMPEG2TSAssembler(notify, desc.c_str(), params);
82 } else {
83 TRESPASS();
84 }
85
86 if (mAssembler != NULL && !mAssembler->initCheck()) {
87 mAssembler.clear();
88 }
89 }
90
AbsDiff(uint32_t seq1,uint32_t seq2)91 static uint32_t AbsDiff(uint32_t seq1, uint32_t seq2) {
92 return seq1 > seq2 ? seq1 - seq2 : seq2 - seq1;
93 }
94
processRTPPacket(const sp<ABuffer> & buffer)95 void ARTPSource::processRTPPacket(const sp<ABuffer> &buffer) {
96 if (mAssembler != NULL && queuePacket(buffer)) {
97 mAssembler->onPacketReceived(this);
98 }
99 }
100
timeUpdate(uint32_t rtpTime,uint64_t ntpTime)101 void ARTPSource::timeUpdate(uint32_t rtpTime, uint64_t ntpTime) {
102 mLastNTPTime = ntpTime;
103 mLastNTPTimeUpdateUs = ALooper::GetNowUs();
104
105 sp<AMessage> notify = mNotify->dup();
106 notify->setInt32("time-update", true);
107 notify->setInt32("rtp-time", rtpTime);
108 notify->setInt64("ntp-time", ntpTime);
109 notify->post();
110 }
111
queuePacket(const sp<ABuffer> & buffer)112 bool ARTPSource::queuePacket(const sp<ABuffer> &buffer) {
113 uint32_t seqNum = (uint32_t)buffer->int32Data();
114
115 if (mNumBuffersReceived++ == 0) {
116 mHighestSeqNumber = seqNum;
117 mBaseSeqNumber = seqNum;
118 mQueue.push_back(buffer);
119 return true;
120 }
121
122 // Only the lower 16-bit of the sequence numbers are transmitted,
123 // derive the high-order bits by choosing the candidate closest
124 // to the highest sequence number (extended to 32 bits) received so far.
125
126 uint32_t seq1 = seqNum | (mHighestSeqNumber & 0xffff0000);
127
128 // non-overflowing version of:
129 // uint32_t seq2 = seqNum | ((mHighestSeqNumber & 0xffff0000) + 0x10000);
130 uint32_t seq2 = seqNum | (((mHighestSeqNumber >> 16) + 1) << 16);
131
132 // non-underflowing version of:
133 // uint32_t seq2 = seqNum | ((mHighestSeqNumber & 0xffff0000) - 0x10000);
134 uint32_t seq3 = seqNum | ((((mHighestSeqNumber >> 16) | 0x10000) - 1) << 16);
135
136 uint32_t diff1 = AbsDiff(seq1, mHighestSeqNumber);
137 uint32_t diff2 = AbsDiff(seq2, mHighestSeqNumber);
138 uint32_t diff3 = AbsDiff(seq3, mHighestSeqNumber);
139
140 if (diff1 < diff2) {
141 if (diff1 < diff3) {
142 // diff1 < diff2 ^ diff1 < diff3
143 seqNum = seq1;
144 } else {
145 // diff3 <= diff1 < diff2
146 seqNum = seq3;
147 }
148 } else if (diff2 < diff3) {
149 // diff2 <= diff1 ^ diff2 < diff3
150 seqNum = seq2;
151 } else {
152 // diff3 <= diff2 <= diff1
153 seqNum = seq3;
154 }
155
156 if (seqNum > mHighestSeqNumber) {
157 mHighestSeqNumber = seqNum;
158 }
159
160 buffer->setInt32Data(seqNum);
161
162 List<sp<ABuffer> >::iterator it = mQueue.begin();
163 while (it != mQueue.end() && (uint32_t)(*it)->int32Data() < seqNum) {
164 ++it;
165 }
166
167 if (it != mQueue.end() && (uint32_t)(*it)->int32Data() == seqNum) {
168 ALOGW("Discarding duplicate buffer");
169 return false;
170 }
171
172 mQueue.insert(it, buffer);
173
174 return true;
175 }
176
byeReceived()177 void ARTPSource::byeReceived() {
178 if (mAssembler != NULL) {
179 mAssembler->onByeReceived();
180 }
181 }
182
addFIR(const sp<ABuffer> & buffer)183 void ARTPSource::addFIR(const sp<ABuffer> &buffer) {
184 if (!mIssueFIRRequests) {
185 return;
186 }
187
188 int64_t nowUs = ALooper::GetNowUs();
189 if (mLastFIRRequestUs >= 0 && mLastFIRRequestUs + 5000000LL > nowUs) {
190 // Send FIR requests at most every 5 secs.
191 return;
192 }
193
194 mLastFIRRequestUs = nowUs;
195
196 if (buffer->size() + 20 > buffer->capacity()) {
197 ALOGW("RTCP buffer too small to accomodate FIR.");
198 return;
199 }
200
201 uint8_t *data = buffer->data() + buffer->size();
202
203 data[0] = 0x80 | 4;
204 data[1] = 206; // PSFB
205 data[2] = 0;
206 data[3] = 4;
207 data[4] = kSourceID >> 24;
208 data[5] = (kSourceID >> 16) & 0xff;
209 data[6] = (kSourceID >> 8) & 0xff;
210 data[7] = kSourceID & 0xff;
211
212 data[8] = 0x00; // SSRC of media source (unused)
213 data[9] = 0x00;
214 data[10] = 0x00;
215 data[11] = 0x00;
216
217 data[12] = mID >> 24;
218 data[13] = (mID >> 16) & 0xff;
219 data[14] = (mID >> 8) & 0xff;
220 data[15] = mID & 0xff;
221
222 data[16] = mNextFIRSeqNo++; // Seq Nr.
223
224 data[17] = 0x00; // Reserved
225 data[18] = 0x00;
226 data[19] = 0x00;
227
228 buffer->setRange(buffer->offset(), buffer->size() + 20);
229
230 ALOGV("Added FIR request.");
231 }
232
addReceiverReport(const sp<ABuffer> & buffer)233 void ARTPSource::addReceiverReport(const sp<ABuffer> &buffer) {
234 if (buffer->size() + 32 > buffer->capacity()) {
235 ALOGW("RTCP buffer too small to accomodate RR.");
236 return;
237 }
238
239 uint8_t fraction = 0;
240
241 // According to appendix A.3 in RFC 3550
242 uint32_t expected = mHighestSeqNumber - mBaseSeqNumber + 1;
243 int64_t intervalExpected = expected - mPrevExpected;
244 int64_t intervalReceived = mNumBuffersReceived - mPrevNumBuffersReceived;
245 int64_t intervalPacketLost = intervalExpected - intervalReceived;
246
247 if (intervalExpected > 0 && intervalPacketLost > 0) {
248 fraction = (intervalPacketLost << 8) / intervalExpected;
249 }
250
251 mPrevExpected = expected;
252 mPrevNumBuffersReceived = mNumBuffersReceived;
253 int32_t cumulativePacketLost = (int32_t)expected - mNumBuffersReceived;
254
255 uint8_t *data = buffer->data() + buffer->size();
256
257 data[0] = 0x80 | 1;
258 data[1] = 201; // RR
259 data[2] = 0;
260 data[3] = 7;
261 data[4] = kSourceID >> 24;
262 data[5] = (kSourceID >> 16) & 0xff;
263 data[6] = (kSourceID >> 8) & 0xff;
264 data[7] = kSourceID & 0xff;
265
266 data[8] = mID >> 24;
267 data[9] = (mID >> 16) & 0xff;
268 data[10] = (mID >> 8) & 0xff;
269 data[11] = mID & 0xff;
270
271 data[12] = fraction; // fraction lost
272
273 data[13] = cumulativePacketLost >> 16; // cumulative lost
274 data[14] = (cumulativePacketLost >> 8) & 0xff;
275 data[15] = cumulativePacketLost & 0xff;
276
277 data[16] = mHighestSeqNumber >> 24;
278 data[17] = (mHighestSeqNumber >> 16) & 0xff;
279 data[18] = (mHighestSeqNumber >> 8) & 0xff;
280 data[19] = mHighestSeqNumber & 0xff;
281
282 data[20] = 0x00; // Interarrival jitter
283 data[21] = 0x00;
284 data[22] = 0x00;
285 data[23] = 0x00;
286
287 uint32_t LSR = 0;
288 uint32_t DLSR = 0;
289 if (mLastNTPTime != 0) {
290 LSR = (mLastNTPTime >> 16) & 0xffffffff;
291
292 DLSR = (uint32_t)
293 ((ALooper::GetNowUs() - mLastNTPTimeUpdateUs) * 65536.0 / 1E6);
294 }
295
296 data[24] = LSR >> 24;
297 data[25] = (LSR >> 16) & 0xff;
298 data[26] = (LSR >> 8) & 0xff;
299 data[27] = LSR & 0xff;
300
301 data[28] = DLSR >> 24;
302 data[29] = (DLSR >> 16) & 0xff;
303 data[30] = (DLSR >> 8) & 0xff;
304 data[31] = DLSR & 0xff;
305
306 buffer->setRange(buffer->offset(), buffer->size() + 32);
307 }
308
309 } // namespace android
310
311
312