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 "ATSParser"
19 #include <utils/Log.h>
20 #include "ATSParser.h"
21 #include "AnotherPacketSource.h"
22 #include "CasManager.h"
23 #include "ESQueue.h"
24
25 #include <android/hardware/cas/native/1.0/IDescrambler.h>
26 #include <android/hidl/memory/1.0/IMemory.h>
27 #include <cutils/ashmem.h>
28 #include <cutils/native_handle.h>
29 #include <hidlmemory/mapping.h>
30 #include <media/cas/DescramblerAPI.h>
31 #include <media/stagefright/foundation/ABitReader.h>
32 #include <media/stagefright/foundation/ABuffer.h>
33 #include <media/stagefright/foundation/ADebug.h>
34 #include <media/stagefright/foundation/AMessage.h>
35 #include <media/stagefright/foundation/ByteUtils.h>
36 #include <media/stagefright/foundation/MediaKeys.h>
37 #include <media/stagefright/foundation/avc_utils.h>
38 #include <media/stagefright/foundation/hexdump.h>
39 #include <media/stagefright/MediaDefs.h>
40 #include <media/stagefright/MediaErrors.h>
41 #include <media/stagefright/MetaData.h>
42 #include <media/IStreamSource.h>
43 #include <utils/KeyedVector.h>
44 #include <utils/Vector.h>
45
46 #include <inttypes.h>
47
48 namespace android {
49 using hardware::hidl_handle;
50 using hardware::hidl_string;
51 using hardware::hidl_vec;
52 using hardware::hidl_memory;
53 using namespace hardware::cas::V1_0;
54 using namespace hardware::cas::native::V1_0;
55 typedef hidl::memory::V1_0::IMemory TMemory;
56
57 // I want the expression "y" evaluated even if verbose logging is off.
58 #define MY_LOGV(x, y) \
59 do { unsigned tmp = y; ALOGV(x, tmp); } while (0)
60
61 static const size_t kTSPacketSize = 188;
62
63 struct ATSParser::Program : public RefBase {
64 Program(ATSParser *parser, unsigned programNumber, unsigned programMapPID,
65 int64_t lastRecoveredPTS);
66
67 bool parsePSISection(
68 unsigned pid, ABitReader *br, status_t *err);
69
70 // Pass to appropriate stream according to pid, and set event if it's a PES
71 // with a sync frame.
72 // Note that the method itself does not touch event.
73 bool parsePID(
74 unsigned pid, unsigned continuity_counter,
75 unsigned payload_unit_start_indicator,
76 unsigned transport_scrambling_control,
77 unsigned random_access_indicator,
78 ABitReader *br, status_t *err, SyncEvent *event);
79
80 void signalDiscontinuity(
81 DiscontinuityType type, const sp<AMessage> &extra);
82
83 void signalEOS(status_t finalResult);
84
85 sp<AnotherPacketSource> getSource(SourceType type);
86 bool hasSource(SourceType type) const;
87
88 int64_t convertPTSToTimestamp(uint64_t PTS);
89
PTSTimeDeltaEstablishedandroid::ATSParser::Program90 bool PTSTimeDeltaEstablished() const {
91 return mFirstPTSValid;
92 }
93
numberandroid::ATSParser::Program94 unsigned number() const { return mProgramNumber; }
95
updateProgramMapPIDandroid::ATSParser::Program96 void updateProgramMapPID(unsigned programMapPID) {
97 mProgramMapPID = programMapPID;
98 }
99
programMapPIDandroid::ATSParser::Program100 unsigned programMapPID() const {
101 return mProgramMapPID;
102 }
103
parserFlagsandroid::ATSParser::Program104 uint32_t parserFlags() const {
105 return mParser->mFlags;
106 }
107
casManagerandroid::ATSParser::Program108 sp<CasManager> casManager() const {
109 return mParser->mCasManager;
110 }
111
firstPTSandroid::ATSParser::Program112 uint64_t firstPTS() const {
113 return mFirstPTS;
114 }
115
116 void updateCasSessions();
117
118 void signalNewSampleAesKey(const sp<AMessage> &keyItem);
119
120 private:
121
122 ATSParser *mParser;
123 unsigned mProgramNumber;
124 unsigned mProgramMapPID;
125 uint32_t mPMTVersion;
126 uint32_t mPMT_CRC;
127 KeyedVector<unsigned, sp<Stream> > mStreams;
128 bool mFirstPTSValid;
129 uint64_t mFirstPTS;
130 int64_t mLastRecoveredPTS;
131 sp<AMessage> mSampleAesKeyItem;
132
133 status_t parseProgramMap(ABitReader *br);
134 int64_t recoverPTS(uint64_t PTS_33bit);
135 bool findCADescriptor(
136 ABitReader *br, unsigned infoLength, CADescriptor *caDescriptor);
137 bool switchPIDs(const Vector<StreamInfo> &infos);
138
139 DISALLOW_EVIL_CONSTRUCTORS(Program);
140 };
141
142 struct ATSParser::Stream : public RefBase {
143 Stream(Program *program, unsigned PCR_PID, const StreamInfo &info);
144
typeandroid::ATSParser::Stream145 unsigned type() const { return mStreamType; }
typeExtandroid::ATSParser::Stream146 unsigned typeExt() const { return mStreamTypeExt; }
pidandroid::ATSParser::Stream147 unsigned pid() const { return mElementaryPID; }
setPIDandroid::ATSParser::Stream148 void setPID(unsigned pid) { mElementaryPID = pid; }
setAudioPresentationsandroid::ATSParser::Stream149 void setAudioPresentations(AudioPresentationCollection audioPresentations) {
150 mAudioPresentations = audioPresentations;
151 }
152
153 void setCasInfo(
154 int32_t systemId,
155 const sp<IDescrambler> &descrambler,
156 const std::vector<uint8_t> &sessionId);
157
158 // Parse the payload and set event when PES with a sync frame is detected.
159 // This method knows when a PES starts; so record mPesStartOffsets in that
160 // case.
161 status_t parse(
162 unsigned continuity_counter,
163 unsigned payload_unit_start_indicator,
164 unsigned transport_scrambling_control,
165 unsigned random_access_indicator,
166 ABitReader *br,
167 SyncEvent *event);
168
169 void signalDiscontinuity(
170 DiscontinuityType type, const sp<AMessage> &extra);
171
172 void signalEOS(status_t finalResult);
173
174 SourceType getSourceType();
175 sp<AnotherPacketSource> getSource(SourceType type);
176
177 bool isAudio() const;
178 bool isVideo() const;
179 bool isMeta() const;
180
181 void signalNewSampleAesKey(const sp<AMessage> &keyItem);
182
183 protected:
184 virtual ~Stream();
185
186 private:
187 struct SubSampleInfo {
188 size_t subSampleSize;
189 unsigned transport_scrambling_mode;
190 unsigned random_access_indicator;
191 };
192 Program *mProgram;
193 unsigned mElementaryPID;
194 unsigned mStreamType;
195 unsigned mStreamTypeExt;
196 unsigned mPCR_PID;
197 int32_t mExpectedContinuityCounter;
198
199 sp<ABuffer> mBuffer;
200 sp<AnotherPacketSource> mSource;
201 bool mPayloadStarted;
202 bool mEOSReached;
203
204 uint64_t mPrevPTS;
205 List<off64_t> mPesStartOffsets;
206
207 ElementaryStreamQueue *mQueue;
208
209 bool mScrambled;
210 bool mSampleEncrypted;
211 sp<AMessage> mSampleAesKeyItem;
212 sp<TMemory> mHidlMemory;
213 hardware::cas::native::V1_0::SharedBuffer mDescramblerSrcBuffer;
214 sp<ABuffer> mDescrambledBuffer;
215 List<SubSampleInfo> mSubSamples;
216 sp<IDescrambler> mDescrambler;
217 AudioPresentationCollection mAudioPresentations;
218
219 // Send audio presentations along with access units.
220 void addAudioPresentations(const sp<ABuffer> &buffer);
221
222 // Flush accumulated payload if necessary --- i.e. at EOS or at the start of
223 // another payload. event is set if the flushed payload is PES with a sync
224 // frame.
225 status_t flush(SyncEvent *event);
226
227 // Flush accumulated payload for scrambled streams if necessary --- i.e. at
228 // EOS or at the start of another payload. event is set if the flushed
229 // payload is PES with a sync frame.
230 status_t flushScrambled(SyncEvent *event);
231
232 // Check if a PES packet is scrambled at PES level.
233 uint32_t getPesScramblingControl(ABitReader *br, int32_t *pesOffset);
234
235 // Strip and parse PES headers and pass remaining payload into onPayload
236 // with parsed metadata. event is set if the PES contains a sync frame.
237 status_t parsePES(ABitReader *br, SyncEvent *event);
238
239 // Feed the payload into mQueue and if a packet is identified, queue it
240 // into mSource. If the packet is a sync frame. set event with start offset
241 // and timestamp of the packet.
242 void onPayloadData(
243 unsigned PTS_DTS_flags, uint64_t PTS, uint64_t DTS,
244 unsigned PES_scrambling_control,
245 const uint8_t *data, size_t size,
246 int32_t payloadOffset, SyncEvent *event);
247
248 // Ensure internal buffers can hold specified size, and will re-allocate
249 // as needed.
250 bool ensureBufferCapacity(size_t size);
251
252 DISALLOW_EVIL_CONSTRUCTORS(Stream);
253 };
254
255 struct ATSParser::PSISection : public RefBase {
256 PSISection();
257
258 status_t append(const void *data, size_t size);
259 void setSkipBytes(uint8_t skip);
260 void clear();
261
262 bool isComplete() const;
263 bool isEmpty() const;
264 bool isCRCOkay() const;
265
266 const uint8_t *data() const;
267 size_t size() const;
268
269 protected:
270 virtual ~PSISection();
271
272 private:
273 sp<ABuffer> mBuffer;
274 uint8_t mSkipBytes;
275 static uint32_t CRC_TABLE[];
276
277 DISALLOW_EVIL_CONSTRUCTORS(PSISection);
278 };
279
SyncEvent(off64_t offset)280 ATSParser::SyncEvent::SyncEvent(off64_t offset)
281 : mHasReturnedData(false), mOffset(offset), mTimeUs(0) {}
282
init(off64_t offset,const sp<AnotherPacketSource> & source,int64_t timeUs,SourceType type)283 void ATSParser::SyncEvent::init(off64_t offset, const sp<AnotherPacketSource> &source,
284 int64_t timeUs, SourceType type) {
285 mHasReturnedData = true;
286 mOffset = offset;
287 mMediaSource = source;
288 mTimeUs = timeUs;
289 mType = type;
290 }
291
reset()292 void ATSParser::SyncEvent::reset() {
293 mHasReturnedData = false;
294 }
295 ////////////////////////////////////////////////////////////////////////////////
296
Program(ATSParser * parser,unsigned programNumber,unsigned programMapPID,int64_t lastRecoveredPTS)297 ATSParser::Program::Program(
298 ATSParser *parser, unsigned programNumber, unsigned programMapPID,
299 int64_t lastRecoveredPTS)
300 : mParser(parser),
301 mProgramNumber(programNumber),
302 mProgramMapPID(programMapPID),
303 mPMTVersion(0xffffffff),
304 mPMT_CRC(0xffffffff),
305 mFirstPTSValid(false),
306 mFirstPTS(0),
307 mLastRecoveredPTS(lastRecoveredPTS) {
308 ALOGV("new program number %u", programNumber);
309 }
310
parsePSISection(unsigned pid,ABitReader * br,status_t * err)311 bool ATSParser::Program::parsePSISection(
312 unsigned pid, ABitReader *br, status_t *err) {
313 *err = OK;
314
315 if (pid != mProgramMapPID) {
316 return false;
317 }
318
319 *err = parseProgramMap(br);
320
321 return true;
322 }
323
parsePID(unsigned pid,unsigned continuity_counter,unsigned payload_unit_start_indicator,unsigned transport_scrambling_control,unsigned random_access_indicator,ABitReader * br,status_t * err,SyncEvent * event)324 bool ATSParser::Program::parsePID(
325 unsigned pid, unsigned continuity_counter,
326 unsigned payload_unit_start_indicator,
327 unsigned transport_scrambling_control,
328 unsigned random_access_indicator,
329 ABitReader *br, status_t *err, SyncEvent *event) {
330 *err = OK;
331
332 ssize_t index = mStreams.indexOfKey(pid);
333 if (index < 0) {
334 return false;
335 }
336
337 *err = mStreams.editValueAt(index)->parse(
338 continuity_counter,
339 payload_unit_start_indicator,
340 transport_scrambling_control,
341 random_access_indicator,
342 br, event);
343
344 return true;
345 }
346
signalDiscontinuity(DiscontinuityType type,const sp<AMessage> & extra)347 void ATSParser::Program::signalDiscontinuity(
348 DiscontinuityType type, const sp<AMessage> &extra) {
349 int64_t mediaTimeUs;
350 if ((type & DISCONTINUITY_TIME)
351 && extra != NULL
352 && extra->findInt64(
353 kATSParserKeyMediaTimeUs, &mediaTimeUs)) {
354 mFirstPTSValid = false;
355 }
356
357 for (size_t i = 0; i < mStreams.size(); ++i) {
358 mStreams.editValueAt(i)->signalDiscontinuity(type, extra);
359 }
360 }
361
signalEOS(status_t finalResult)362 void ATSParser::Program::signalEOS(status_t finalResult) {
363 for (size_t i = 0; i < mStreams.size(); ++i) {
364 mStreams.editValueAt(i)->signalEOS(finalResult);
365 }
366 }
367
switchPIDs(const Vector<StreamInfo> & infos)368 bool ATSParser::Program::switchPIDs(const Vector<StreamInfo> &infos) {
369 bool success = false;
370
371 if (mStreams.size() == infos.size()) {
372 // build type->PIDs map for old and new mapping
373 size_t i;
374 KeyedVector<int32_t, Vector<int32_t> > oldType2PIDs, newType2PIDs;
375 for (i = 0; i < mStreams.size(); ++i) {
376 ssize_t index = oldType2PIDs.indexOfKey(mStreams[i]->type());
377 if (index < 0) {
378 oldType2PIDs.add(mStreams[i]->type(), Vector<int32_t>());
379 }
380 oldType2PIDs.editValueFor(mStreams[i]->type()).push_back(mStreams[i]->pid());
381 }
382 for (i = 0; i < infos.size(); ++i) {
383 ssize_t index = newType2PIDs.indexOfKey(infos[i].mType);
384 if (index < 0) {
385 newType2PIDs.add(infos[i].mType, Vector<int32_t>());
386 }
387 newType2PIDs.editValueFor(infos[i].mType).push_back(infos[i].mPID);
388 }
389
390 // we can recover if the number of streams for each type hasn't changed
391 if (oldType2PIDs.size() == newType2PIDs.size()) {
392 success = true;
393 for (i = 0; i < oldType2PIDs.size(); ++i) {
394 // KeyedVector is sorted, we just compare key and size of each index
395 if (oldType2PIDs.keyAt(i) != newType2PIDs.keyAt(i)
396 || oldType2PIDs[i].size() != newType2PIDs[i].size()) {
397 success = false;
398 break;
399 }
400 }
401 }
402
403 if (success) {
404 // save current streams to temp
405 KeyedVector<int32_t, sp<Stream> > temp;
406 for (i = 0; i < mStreams.size(); ++i) {
407 temp.add(mStreams.keyAt(i), mStreams.editValueAt(i));
408 }
409
410 mStreams.clear();
411 for (i = 0; i < temp.size(); ++i) {
412 // The two checks below shouldn't happen,
413 // we already checked above the stream count matches
414 ssize_t index = newType2PIDs.indexOfKey(temp[i]->type());
415 if (index < 0) {
416 return false;
417 }
418 Vector<int32_t> &newPIDs = newType2PIDs.editValueAt(index);
419 if (newPIDs.isEmpty()) {
420 return false;
421 }
422
423 // get the next PID for temp[i]->type() in the new PID map
424 Vector<int32_t>::iterator it = newPIDs.begin();
425
426 // change the PID of the stream, and add it back
427 temp.editValueAt(i)->setPID(*it);
428 mStreams.add(temp[i]->pid(), temp.editValueAt(i));
429
430 // removed the used PID
431 newPIDs.erase(it);
432 }
433 }
434 }
435 return success;
436 }
437
findCADescriptor(ABitReader * br,unsigned infoLength,ATSParser::CADescriptor * caDescriptor)438 bool ATSParser::Program::findCADescriptor(
439 ABitReader *br, unsigned infoLength,
440 ATSParser::CADescriptor *caDescriptor) {
441 bool found = false;
442 while (infoLength > 2) {
443 if (br->numBitsLeft() < 16) {
444 ALOGE("Not enough data left in bitreader");
445 return false;
446 }
447 unsigned descriptor_tag = br->getBits(8);
448 ALOGV(" tag = 0x%02x", descriptor_tag);
449
450 unsigned descriptor_length = br->getBits(8);
451 ALOGV(" len = %u", descriptor_length);
452
453 infoLength -= 2;
454 if (descriptor_length > infoLength) {
455 break;
456 }
457 if (descriptor_tag == DESCRIPTOR_CA && descriptor_length >= 4) {
458 found = true;
459 if (br->numBitsLeft() < 32) {
460 ALOGE("Not enough data left in bitreader");
461 return false;
462 }
463 caDescriptor->mSystemID = br->getBits(16);
464 caDescriptor->mPID = br->getBits(16) & 0x1fff;
465 infoLength -= 4;
466 caDescriptor->mPrivateData.assign(
467 br->data(), br->data() + descriptor_length - 4);
468 break;
469 } else {
470 infoLength -= descriptor_length;
471 if (!br->skipBits(descriptor_length * 8)) {
472 ALOGE("Not enough data left in bitreader");
473 return false;
474 }
475 }
476 }
477 if (!br->skipBits(infoLength * 8)) {
478 ALOGE("Not enough data left in bitreader");
479 return false;
480 }
481 return found;
482 }
483
parseProgramMap(ABitReader * br)484 status_t ATSParser::Program::parseProgramMap(ABitReader *br) {
485 if (br->numBitsLeft() < 10) {
486 ALOGE("Not enough data left in bitreader!");
487 return ERROR_MALFORMED;
488 }
489 unsigned table_id = br->getBits(8);
490 ALOGV(" table_id = %u", table_id);
491 if (table_id != 0x02u) {
492 ALOGE("PMT data error!");
493 return ERROR_MALFORMED;
494 }
495 unsigned section_syntax_indicator = br->getBits(1);
496 ALOGV(" section_syntax_indicator = %u", section_syntax_indicator);
497 if (section_syntax_indicator != 1u) {
498 ALOGE("PMT data error!");
499 return ERROR_MALFORMED;
500 }
501
502 br->skipBits(1); // '0'
503 if (br->numBitsLeft() < 86) {
504 ALOGE("Not enough data left in bitreader!");
505 return ERROR_MALFORMED;
506 }
507 MY_LOGV(" reserved = %u", br->getBits(2));
508
509 unsigned section_length = br->getBits(12);
510 ALOGV(" section_length = %u", section_length);
511
512 MY_LOGV(" program_number = %u", br->getBits(16));
513 MY_LOGV(" reserved = %u", br->getBits(2));
514 bool audioPresentationsChanged = false;
515 unsigned pmtVersion = br->getBits(5);
516 if (pmtVersion != mPMTVersion) {
517 audioPresentationsChanged = true;
518 mPMTVersion = pmtVersion;
519 }
520 MY_LOGV(" version_number = %u", pmtVersion);
521 MY_LOGV(" current_next_indicator = %u", br->getBits(1));
522 MY_LOGV(" section_number = %u", br->getBits(8));
523 MY_LOGV(" last_section_number = %u", br->getBits(8));
524 MY_LOGV(" reserved = %u", br->getBits(3));
525
526 unsigned PCR_PID = br->getBits(13);
527 ALOGV(" PCR_PID = 0x%04x", PCR_PID);
528
529 MY_LOGV(" reserved = %u", br->getBits(4));
530
531 unsigned program_info_length = br->getBits(12);
532 ALOGV(" program_info_length = %u", program_info_length);
533
534 // descriptors
535 CADescriptor programCA;
536 bool hasProgramCA = findCADescriptor(br, program_info_length, &programCA);
537 if (hasProgramCA && !mParser->mCasManager->addProgram(
538 mProgramNumber, programCA)) {
539 return ERROR_MALFORMED;
540 }
541
542 Vector<StreamInfo> infos;
543
544 // infoBytesRemaining is the number of bytes that make up the
545 // variable length section of ES_infos. It does not include the
546 // final CRC.
547 int32_t infoBytesRemaining = section_length - 9 - program_info_length - 4;
548
549 while (infoBytesRemaining >= 5) {
550 StreamInfo info;
551 if (br->numBitsLeft() < 40) {
552 ALOGE("Not enough data left in bitreader!");
553 return ERROR_MALFORMED;
554 }
555 info.mType = br->getBits(8);
556 ALOGV(" stream_type = 0x%02x", info.mType);
557 MY_LOGV(" reserved = %u", br->getBits(3));
558
559 info.mPID = br->getBits(13);
560 ALOGV(" elementary_PID = 0x%04x", info.mPID);
561
562 MY_LOGV(" reserved = %u", br->getBits(4));
563
564 unsigned ES_info_length = br->getBits(12);
565 ALOGV(" ES_info_length = %u", ES_info_length);
566 infoBytesRemaining -= 5 + ES_info_length;
567
568 CADescriptor streamCA;
569 info.mTypeExt = EXT_DESCRIPTOR_DVB_RESERVED_MAX;
570
571 info.mAudioPresentations.clear();
572 bool hasStreamCA = false;
573 while (ES_info_length > 2 && infoBytesRemaining >= 0) {
574 if (br->numBitsLeft() < 16) {
575 ALOGE("Not enough data left in bitreader!");
576 return ERROR_MALFORMED;
577 }
578 unsigned descriptor_tag = br->getBits(8);
579 ALOGV(" tag = 0x%02x", descriptor_tag);
580
581 unsigned descriptor_length = br->getBits(8);
582 ALOGV(" len = %u", descriptor_length);
583
584 ES_info_length -= 2;
585 if (descriptor_length > ES_info_length) {
586 return ERROR_MALFORMED;
587 }
588
589 // The DTS descriptor is used in the PSI PMT to identify streams which carry
590 // DTS audio(core only). If a DTS descriptor is present, a DTS-HD or DTS-UHD
591 // descriptors shall not be present in the same ES_info descriptor loop.
592 if (descriptor_tag == DESCRIPTOR_DTS) {
593 info.mType = STREAMTYPE_DTS;
594 ES_info_length -= descriptor_length;
595 if (!br->skipBits(descriptor_length * 8)) {
596 ALOGE("Not enough data left in bitreader!");
597 return ERROR_MALFORMED;
598 }
599 } else if (descriptor_tag == DESCRIPTOR_CA && descriptor_length >= 4) {
600 hasStreamCA = true;
601 if (br->numBitsLeft() < 32) {
602 ALOGE("Not enough data left in bitreader!");
603 return ERROR_MALFORMED;
604 }
605 streamCA.mSystemID = br->getBits(16);
606 streamCA.mPID = br->getBits(16) & 0x1fff;
607 ES_info_length -= descriptor_length;
608 descriptor_length -= 4;
609 streamCA.mPrivateData.assign(br->data(), br->data() + descriptor_length);
610 if (!br->skipBits(descriptor_length * 8)) {
611 ALOGE("Not enough data left in bitreader!");
612 return ERROR_MALFORMED;
613 }
614 } else if (info.mType == STREAMTYPE_PES_PRIVATE_DATA &&
615 descriptor_tag == DESCRIPTOR_DVB_EXTENSION && descriptor_length >= 1) {
616 if (br->numBitsLeft() < 8) {
617 ALOGE("Not enough data left in bitreader!");
618 return ERROR_MALFORMED;
619 }
620 unsigned descTagExt = br->getBits(8);
621 ALOGV(" tag_ext = 0x%02x", descTagExt);
622 ES_info_length -= descriptor_length;
623 descriptor_length--;
624 if (br->numBitsLeft() < (descriptor_length * 8)) {
625 ALOGE("Not enough data left in bitreader!");
626 return ERROR_MALFORMED;
627 }
628 // The AC4 descriptor is used in the PSI PMT to identify streams which carry AC4
629 // audio.
630 if (descTagExt == EXT_DESCRIPTOR_DVB_AC4) {
631 info.mTypeExt = EXT_DESCRIPTOR_DVB_AC4;
632 br->skipBits(descriptor_length * 8);
633 } else if (descTagExt == EXT_DESCRIPTOR_DVB_DTS_HD) {
634 // DTS HD extended descriptor which can accommodate core only formats
635 // as well as extension only and core + extension combinations.
636 info.mTypeExt = EXT_DESCRIPTOR_DVB_DTS_HD;
637 br->skipBits(descriptor_length * 8);
638 } else if (descTagExt == EXT_DESCRIPTOR_DVB_DTS_UHD) {
639 // The DTS-UHD descriptor is used in the PSI PMT to identify streams
640 // which carry DTS-UHD audio
641 info.mTypeExt = EXT_DESCRIPTOR_DVB_DTS_UHD;
642 br->skipBits(descriptor_length * 8);
643 } else if (descTagExt == EXT_DESCRIPTOR_DVB_AUDIO_PRESELECTION &&
644 descriptor_length >= 1) {
645 if (br->numBitsLeft() < 8) {
646 ALOGE("Not enough data left in bitreader!");
647 return ERROR_MALFORMED;
648 }
649 // DVB BlueBook A038 Table 110
650 unsigned num_preselections = br->getBits(5);
651 br->skipBits(3); // reserved
652 for (unsigned i = 0; i < num_preselections; ++i) {
653 if (br->numBitsLeft() < 16) {
654 ALOGE("Not enough data left in bitreader!");
655 return ERROR_MALFORMED;
656 }
657 AudioPresentationV1 ap;
658 ap.mPresentationId = br->getBits(5); // preselection_id
659
660 // audio_rendering_indication
661 ap.mMasteringIndication = static_cast<MasteringIndication>(br->getBits(3));
662 ap.mAudioDescriptionAvailable = (br->getBits(1) == 1);
663 ap.mSpokenSubtitlesAvailable = (br->getBits(1) == 1);
664 ap.mDialogueEnhancementAvailable = (br->getBits(1) == 1);
665
666 bool interactivity_enabled = (br->getBits(1) == 1);
667 MY_LOGV(" interactivity_enabled = %d", interactivity_enabled);
668
669 bool language_code_present = (br->getBits(1) == 1);
670 bool text_label_present = (br->getBits(1) == 1);
671
672 bool multi_stream_info_present = (br->getBits(1) == 1);
673 bool future_extension = (br->getBits(1) == 1);
674 if (language_code_present) {
675 if (br->numBitsLeft() < 24) {
676 ALOGE("Not enough data left in bitreader!");
677 return ERROR_MALFORMED;
678 }
679 char language[4];
680 language[0] = br->getBits(8);
681 language[1] = br->getBits(8);
682 language[2] = br->getBits(8);
683 language[3] = 0;
684 ap.mLanguage = String8(language);
685 }
686
687 // This maps the presentation id to the message id in the
688 // EXT_DESCRIPTOR_DVB_MESSAGE so that we can get the presentation label.
689 if (text_label_present) {
690 if (br->numBitsLeft() < 8) {
691 ALOGE("Not enough data left in bitreader!");
692 return ERROR_MALFORMED;
693 }
694 unsigned message_id = br->getBits(8);
695 MY_LOGV(" message_id = %u", message_id);
696 }
697
698 if (multi_stream_info_present) {
699 if (br->numBitsLeft() < 8) {
700 ALOGE("Not enough data left in bitreader!");
701 return ERROR_MALFORMED;
702 }
703 unsigned num_aux_components = br->getBits(3);
704 br->skipBits(5); // reserved
705 if (br->numBitsLeft() < (num_aux_components * 8)) {
706 ALOGE("Not enough data left in bitreader!");
707 return ERROR_MALFORMED;
708 }
709 br->skipBits(num_aux_components * 8); // component_tag
710 }
711 if (future_extension) {
712 if (br->numBitsLeft() < 8) {
713 return ERROR_MALFORMED;
714 }
715 br->skipBits(3); // reserved
716 unsigned future_extension_length = br->getBits(5);
717 if (br->numBitsLeft() < (future_extension_length * 8)) {
718 ALOGE("Not enough data left in bitreader!");
719 return ERROR_MALFORMED;
720 }
721 br->skipBits(future_extension_length * 8); // future_extension_byte
722 }
723 info.mAudioPresentations.push_back(std::move(ap));
724 }
725 } else {
726 if (!br->skipBits(descriptor_length * 8)) {
727 ALOGE("Not enough data left in bitreader!");
728 return ERROR_MALFORMED;
729 }
730 }
731 } else {
732 ES_info_length -= descriptor_length;
733 if (!br->skipBits(descriptor_length * 8)) {
734 ALOGE("Not enough data left in bitreader!");
735 return ERROR_MALFORMED;
736 }
737 }
738 }
739 if (hasStreamCA && !mParser->mCasManager->addStream(
740 mProgramNumber, info.mPID, streamCA)) {
741 return ERROR_MALFORMED;
742 }
743 if (hasProgramCA) {
744 info.mCADescriptor = programCA;
745 } else if (hasStreamCA) {
746 info.mCADescriptor = streamCA;
747 }
748
749 infos.push(info);
750 }
751
752 if (infoBytesRemaining != 0) {
753 ALOGW("Section data remains unconsumed");
754 }
755 if (br->numBitsLeft() < 32) {
756 ALOGE("Not enough data left in bitreader!");
757 return ERROR_MALFORMED;
758 }
759 unsigned crc = br->getBits(32);
760 if (crc != mPMT_CRC) {
761 audioPresentationsChanged = true;
762 mPMT_CRC = crc;
763 }
764 MY_LOGV(" CRC = 0x%08x", crc);
765
766 bool PIDsChanged = false;
767 for (size_t i = 0; i < infos.size(); ++i) {
768 StreamInfo &info = infos.editItemAt(i);
769
770 ssize_t index = mStreams.indexOfKey(info.mPID);
771
772 if (index >= 0 && mStreams.editValueAt(index)->type() != info.mType) {
773 ALOGI("uh oh. stream PIDs have changed.");
774 PIDsChanged = true;
775 break;
776 }
777 }
778
779 if (PIDsChanged) {
780 #if 0
781 ALOGI("before:");
782 for (size_t i = 0; i < mStreams.size(); ++i) {
783 sp<Stream> stream = mStreams.editValueAt(i);
784
785 ALOGI("PID 0x%08x => type 0x%02x", stream->pid(), stream->type());
786 }
787
788 ALOGI("after:");
789 for (size_t i = 0; i < infos.size(); ++i) {
790 StreamInfo &info = infos.editItemAt(i);
791
792 ALOGI("PID 0x%08x => type 0x%02x", info.mPID, info.mType);
793 }
794 #endif
795
796 // we can recover if number of streams for each type remain the same
797 bool success = switchPIDs(infos);
798
799 if (!success) {
800 ALOGI("Stream PIDs changed and we cannot recover.");
801 return ERROR_MALFORMED;
802 }
803 }
804
805 bool isAddingScrambledStream = false;
806 for (size_t i = 0; i < infos.size(); ++i) {
807 StreamInfo &info = infos.editItemAt(i);
808
809 if (mParser->mCasManager->isCAPid(info.mPID)) {
810 // skip CA streams (EMM/ECM)
811 continue;
812 }
813 ssize_t index = mStreams.indexOfKey(info.mPID);
814
815 if (index < 0) {
816 sp<Stream> stream = new Stream(this, PCR_PID, info);
817
818 if (mSampleAesKeyItem != NULL) {
819 stream->signalNewSampleAesKey(mSampleAesKeyItem);
820 }
821
822 isAddingScrambledStream |= info.mCADescriptor.mSystemID >= 0;
823 mStreams.add(info.mPID, stream);
824 }
825 else if (index >= 0 && mStreams.editValueAt(index)->isAudio()
826 && audioPresentationsChanged) {
827 mStreams.editValueAt(index)->setAudioPresentations(info.mAudioPresentations);
828 }
829 }
830
831 if (isAddingScrambledStream) {
832 ALOGI("Receiving scrambled streams without descrambler!");
833 return ERROR_DRM_DECRYPT_UNIT_NOT_INITIALIZED;
834 }
835 return OK;
836 }
837
recoverPTS(uint64_t PTS_33bit)838 int64_t ATSParser::Program::recoverPTS(uint64_t PTS_33bit) {
839 // We only have the lower 33-bit of the PTS. It could overflow within a
840 // reasonable amount of time. To handle the wrap-around, use fancy math
841 // to get an extended PTS that is within [-0xffffffff, 0xffffffff]
842 // of the latest recovered PTS.
843 if (mLastRecoveredPTS < 0LL) {
844 // Use the original 33bit number for 1st frame, the reason is that
845 // if 1st frame wraps to negative that's far away from 0, we could
846 // never start. Only start wrapping around from 2nd frame.
847 mLastRecoveredPTS = static_cast<int64_t>(PTS_33bit);
848 } else {
849 mLastRecoveredPTS = static_cast<int64_t>(
850 ((mLastRecoveredPTS - static_cast<int64_t>(PTS_33bit) + 0x100000000LL)
851 & 0xfffffffe00000000ull) | PTS_33bit);
852 // We start from 0, but recovered PTS could be slightly below 0.
853 // Clamp it to 0 as rest of the pipeline doesn't take negative pts.
854 // (eg. video is read first and starts at 0, but audio starts at 0xfffffff0)
855 if (mLastRecoveredPTS < 0LL) {
856 ALOGI("Clamping negative recovered PTS (%" PRId64 ") to 0", mLastRecoveredPTS);
857 mLastRecoveredPTS = 0LL;
858 }
859 }
860
861 return mLastRecoveredPTS;
862 }
863
getSource(SourceType type)864 sp<AnotherPacketSource> ATSParser::Program::getSource(SourceType type) {
865 for (size_t i = 0; i < mStreams.size(); ++i) {
866 sp<AnotherPacketSource> source = mStreams.editValueAt(i)->getSource(type);
867 if (source != NULL) {
868 return source;
869 }
870 }
871
872 return NULL;
873 }
874
hasSource(SourceType type) const875 bool ATSParser::Program::hasSource(SourceType type) const {
876 for (size_t i = 0; i < mStreams.size(); ++i) {
877 const sp<Stream> &stream = mStreams.valueAt(i);
878 if (type == AUDIO && stream->isAudio()) {
879 return true;
880 } else if (type == VIDEO && stream->isVideo()) {
881 return true;
882 } else if (type == META && stream->isMeta()) {
883 return true;
884 }
885 }
886
887 return false;
888 }
889
convertPTSToTimestamp(uint64_t PTS)890 int64_t ATSParser::Program::convertPTSToTimestamp(uint64_t PTS) {
891 PTS = recoverPTS(PTS);
892
893 if (!(mParser->mFlags & TS_TIMESTAMPS_ARE_ABSOLUTE)) {
894 if (!mFirstPTSValid) {
895 mFirstPTSValid = true;
896 mFirstPTS = PTS;
897 PTS = 0;
898 } else if (PTS < mFirstPTS) {
899 PTS = 0;
900 } else {
901 PTS -= mFirstPTS;
902 }
903 }
904
905 int64_t timeUs = (PTS * 100) / 9;
906
907 if (mParser->mAbsoluteTimeAnchorUs >= 0LL) {
908 timeUs += mParser->mAbsoluteTimeAnchorUs;
909 }
910
911 if (mParser->mTimeOffsetValid) {
912 timeUs += mParser->mTimeOffsetUs;
913 }
914
915 return timeUs;
916 }
917
updateCasSessions()918 void ATSParser::Program::updateCasSessions() {
919 for (size_t i = 0; i < mStreams.size(); ++i) {
920 sp<Stream> &stream = mStreams.editValueAt(i);
921 sp<IDescrambler> descrambler;
922 std::vector<uint8_t> sessionId;
923 int32_t systemId;
924 if (mParser->mCasManager->getCasInfo(mProgramNumber, stream->pid(),
925 &systemId, &descrambler, &sessionId)) {
926 stream->setCasInfo(systemId, descrambler, sessionId);
927 }
928 }
929 }
930
931 ////////////////////////////////////////////////////////////////////////////////
932 static const size_t kInitialStreamBufferSize = 192 * 1024;
933
Stream(Program * program,unsigned PCR_PID,const StreamInfo & info)934 ATSParser::Stream::Stream(
935 Program *program, unsigned PCR_PID, const StreamInfo &info)
936 : mProgram(program),
937 mElementaryPID(info.mPID),
938 mStreamType(info.mType),
939 mStreamTypeExt(info.mTypeExt),
940 mPCR_PID(PCR_PID),
941 mExpectedContinuityCounter(-1),
942 mPayloadStarted(false),
943 mEOSReached(false),
944 mPrevPTS(0),
945 mQueue(NULL),
946 mScrambled(info.mCADescriptor.mSystemID >= 0),
947 mAudioPresentations(info.mAudioPresentations) {
948 mSampleEncrypted =
949 mStreamType == STREAMTYPE_H264_ENCRYPTED ||
950 mStreamType == STREAMTYPE_AAC_ENCRYPTED ||
951 mStreamType == STREAMTYPE_AC3_ENCRYPTED;
952
953 ALOGV("new stream PID 0x%02x, type 0x%02x, scrambled %d, SampleEncrypted: %d",
954 info.mPID, info.mType, mScrambled, mSampleEncrypted);
955
956 uint32_t flags = 0;
957 if (((isVideo() || isAudio()) && mScrambled)) {
958 flags = ElementaryStreamQueue::kFlag_ScrambledData;
959 } else if (mSampleEncrypted) {
960 flags = ElementaryStreamQueue::kFlag_SampleEncryptedData;
961 }
962
963 ElementaryStreamQueue::Mode mode = ElementaryStreamQueue::INVALID;
964
965 switch (mStreamType) {
966 case STREAMTYPE_H264:
967 case STREAMTYPE_H264_ENCRYPTED:
968 mode = ElementaryStreamQueue::H264;
969 flags |= (mProgram->parserFlags() & ALIGNED_VIDEO_DATA) ?
970 ElementaryStreamQueue::kFlag_AlignedData : 0;
971 break;
972
973 case STREAMTYPE_MPEG2_AUDIO_ADTS:
974 case STREAMTYPE_AAC_ENCRYPTED:
975 mode = ElementaryStreamQueue::AAC;
976 break;
977
978 case STREAMTYPE_MPEG1_AUDIO:
979 case STREAMTYPE_MPEG2_AUDIO:
980 mode = ElementaryStreamQueue::MPEG_AUDIO;
981 break;
982
983 case STREAMTYPE_MPEG1_VIDEO:
984 case STREAMTYPE_MPEG2_VIDEO:
985 mode = ElementaryStreamQueue::MPEG_VIDEO;
986 break;
987
988 case STREAMTYPE_MPEG4_VIDEO:
989 mode = ElementaryStreamQueue::MPEG4_VIDEO;
990 break;
991
992 case STREAMTYPE_LPCM_AC3:
993 case STREAMTYPE_AC3:
994 case STREAMTYPE_AC3_ENCRYPTED:
995 mode = ElementaryStreamQueue::AC3;
996 break;
997
998 case STREAMTYPE_EAC3:
999 mode = ElementaryStreamQueue::EAC3;
1000 break;
1001
1002 case STREAMTYPE_DTS:
1003 mode = ElementaryStreamQueue::DTS;
1004 break;
1005
1006 case STREAMTYPE_PES_PRIVATE_DATA:
1007 if (mStreamTypeExt == EXT_DESCRIPTOR_DVB_AC4) {
1008 mode = ElementaryStreamQueue::AC4;
1009 } else if (mStreamTypeExt == EXT_DESCRIPTOR_DVB_DTS_HD) {
1010 mode = ElementaryStreamQueue::DTS_HD;
1011 } else if (mStreamTypeExt == EXT_DESCRIPTOR_DVB_DTS_UHD) {
1012 mode = ElementaryStreamQueue::DTS_UHD;
1013 }
1014 break;
1015
1016 case STREAMTYPE_METADATA:
1017 mode = ElementaryStreamQueue::METADATA;
1018 break;
1019
1020 default:
1021 ALOGE("stream PID 0x%02x has invalid stream type 0x%02x",
1022 info.mPID, info.mType);
1023 return;
1024 }
1025
1026 mQueue = new ElementaryStreamQueue(mode, flags);
1027
1028 if (mQueue != NULL) {
1029 if (mSampleAesKeyItem != NULL) {
1030 mQueue->signalNewSampleAesKey(mSampleAesKeyItem);
1031 }
1032
1033 ensureBufferCapacity(kInitialStreamBufferSize);
1034
1035 if (mScrambled && (isAudio() || isVideo())) {
1036 // Set initial format to scrambled
1037 sp<MetaData> meta = new MetaData();
1038 meta->setCString(kKeyMIMEType,
1039 isAudio() ? MEDIA_MIMETYPE_AUDIO_SCRAMBLED
1040 : MEDIA_MIMETYPE_VIDEO_SCRAMBLED);
1041 // for MediaExtractor.CasInfo
1042 const CADescriptor &descriptor = info.mCADescriptor;
1043 meta->setInt32(kKeyCASystemID, descriptor.mSystemID);
1044
1045 meta->setData(kKeyCAPrivateData, 0,
1046 descriptor.mPrivateData.data(),
1047 descriptor.mPrivateData.size());
1048
1049 mSource = new AnotherPacketSource(meta);
1050 }
1051 }
1052 }
1053
~Stream()1054 ATSParser::Stream::~Stream() {
1055 delete mQueue;
1056 mQueue = NULL;
1057 }
1058
ensureBufferCapacity(size_t neededSize)1059 bool ATSParser::Stream::ensureBufferCapacity(size_t neededSize) {
1060 if (mBuffer != NULL && mBuffer->capacity() >= neededSize) {
1061 return true;
1062 }
1063
1064 ALOGV("ensureBufferCapacity: current size %zu, new size %zu, scrambled %d",
1065 mBuffer == NULL ? 0 : mBuffer->capacity(), neededSize, mScrambled);
1066
1067 sp<ABuffer> newBuffer, newScrambledBuffer;
1068 sp<TMemory> newMem;
1069 if (mScrambled) {
1070 int fd = ashmem_create_region("mediaATS", neededSize);
1071 if (fd < 0) {
1072 ALOGE("[stream %d] create_ashmem_region failed for size %zu. FD returned: %d",
1073 mElementaryPID, neededSize, fd);
1074 return false;
1075 }
1076
1077 native_handle_t* handle = native_handle_create(1 /*numFds*/, 0/*numInts*/);
1078 if (handle == nullptr) {
1079 ALOGE("[stream %d] failed to create a native_handle_t", mElementaryPID);
1080 if (close(fd)) {
1081 ALOGE("[stream %d] failed to close ashmem fd. errno: %s", mElementaryPID,
1082 strerror(errno));
1083 }
1084
1085 return false;
1086 }
1087
1088 handle->data[0] = fd;
1089 hidl_handle memHandle;
1090 memHandle.setTo(handle, true /*shouldOwn*/);
1091 hidl_memory hidlMemToken("ashmem", memHandle, neededSize);
1092
1093 newMem = mapMemory(hidlMemToken);
1094 if (newMem == nullptr || newMem->getPointer() == nullptr) {
1095 ALOGE("[stream %d] hidl failed to map memory", mElementaryPID);
1096 return false;
1097 }
1098
1099 newScrambledBuffer = new ABuffer(newMem->getPointer(), newMem->getSize());
1100
1101 if (mDescrambledBuffer != NULL) {
1102 memcpy(newScrambledBuffer->data(),
1103 mDescrambledBuffer->data(), mDescrambledBuffer->size());
1104 newScrambledBuffer->setRange(0, mDescrambledBuffer->size());
1105 } else {
1106 newScrambledBuffer->setRange(0, 0);
1107 }
1108 mHidlMemory = newMem;
1109 mDescrambledBuffer = newScrambledBuffer;
1110
1111 mDescramblerSrcBuffer.heapBase = hidlMemToken;
1112 mDescramblerSrcBuffer.offset = 0ULL;
1113 mDescramblerSrcBuffer.size = (uint64_t)neededSize;
1114
1115 ALOGD("[stream %d] created shared buffer for descrambling, size %zu",
1116 mElementaryPID, neededSize);
1117 } else {
1118 // Align to multiples of 64K.
1119 neededSize = (neededSize + 65535) & ~65535;
1120 }
1121
1122 newBuffer = new ABuffer(neededSize);
1123 if (mBuffer != NULL) {
1124 memcpy(newBuffer->data(), mBuffer->data(), mBuffer->size());
1125 newBuffer->setRange(0, mBuffer->size());
1126 } else {
1127 newBuffer->setRange(0, 0);
1128 }
1129 mBuffer = newBuffer;
1130 return true;
1131 }
1132
parse(unsigned continuity_counter,unsigned payload_unit_start_indicator,unsigned transport_scrambling_control,unsigned random_access_indicator,ABitReader * br,SyncEvent * event)1133 status_t ATSParser::Stream::parse(
1134 unsigned continuity_counter,
1135 unsigned payload_unit_start_indicator,
1136 unsigned transport_scrambling_control,
1137 unsigned random_access_indicator,
1138 ABitReader *br, SyncEvent *event) {
1139 if (mQueue == NULL) {
1140 return OK;
1141 }
1142
1143 if (mExpectedContinuityCounter >= 0
1144 && (unsigned)mExpectedContinuityCounter != continuity_counter) {
1145 ALOGI("discontinuity on stream pid 0x%04x", mElementaryPID);
1146
1147 mPayloadStarted = false;
1148 mPesStartOffsets.clear();
1149 mBuffer->setRange(0, 0);
1150 mSubSamples.clear();
1151 mExpectedContinuityCounter = -1;
1152
1153 #if 0
1154 // Uncomment this if you'd rather see no corruption whatsoever on
1155 // screen and suspend updates until we come across another IDR frame.
1156
1157 if (mStreamType == STREAMTYPE_H264) {
1158 ALOGI("clearing video queue");
1159 mQueue->clear(true /* clearFormat */);
1160 }
1161 #endif
1162
1163 if (!payload_unit_start_indicator) {
1164 return OK;
1165 }
1166 }
1167
1168 mExpectedContinuityCounter = (continuity_counter + 1) & 0x0f;
1169
1170 if (payload_unit_start_indicator) {
1171 off64_t offset = (event != NULL) ? event->getOffset() : 0;
1172 if (mPayloadStarted) {
1173 // Otherwise we run the danger of receiving the trailing bytes
1174 // of a PES packet that we never saw the start of and assuming
1175 // we have a a complete PES packet.
1176
1177 status_t err = flush(event);
1178
1179 if (err != OK) {
1180 ALOGW("Error (%08x) happened while flushing; we simply discard "
1181 "the PES packet and continue.", err);
1182 }
1183 }
1184
1185 mPayloadStarted = true;
1186 // There should be at most 2 elements in |mPesStartOffsets|.
1187 while (mPesStartOffsets.size() >= 2) {
1188 mPesStartOffsets.erase(mPesStartOffsets.begin());
1189 }
1190 mPesStartOffsets.push_back(offset);
1191 }
1192
1193 if (!mPayloadStarted) {
1194 return OK;
1195 }
1196
1197 size_t payloadSizeBits = br->numBitsLeft();
1198 if (payloadSizeBits % 8 != 0u) {
1199 ALOGE("Wrong value");
1200 return BAD_VALUE;
1201 }
1202
1203 size_t neededSize = mBuffer->size() + payloadSizeBits / 8;
1204 if (!ensureBufferCapacity(neededSize)) {
1205 return NO_MEMORY;
1206 }
1207
1208 memcpy(mBuffer->data() + mBuffer->size(), br->data(), payloadSizeBits / 8);
1209 mBuffer->setRange(0, mBuffer->size() + payloadSizeBits / 8);
1210
1211 if (mScrambled) {
1212 mSubSamples.push_back({payloadSizeBits / 8,
1213 transport_scrambling_control, random_access_indicator});
1214 }
1215
1216 return OK;
1217 }
1218
isVideo() const1219 bool ATSParser::Stream::isVideo() const {
1220 switch (mStreamType) {
1221 case STREAMTYPE_H264:
1222 case STREAMTYPE_H264_ENCRYPTED:
1223 case STREAMTYPE_MPEG1_VIDEO:
1224 case STREAMTYPE_MPEG2_VIDEO:
1225 case STREAMTYPE_MPEG4_VIDEO:
1226 return true;
1227
1228 default:
1229 return false;
1230 }
1231 }
1232
isAudio() const1233 bool ATSParser::Stream::isAudio() const {
1234 switch (mStreamType) {
1235 case STREAMTYPE_MPEG1_AUDIO:
1236 case STREAMTYPE_MPEG2_AUDIO:
1237 case STREAMTYPE_MPEG2_AUDIO_ADTS:
1238 case STREAMTYPE_LPCM_AC3:
1239 case STREAMTYPE_AC3:
1240 case STREAMTYPE_EAC3:
1241 case STREAMTYPE_AAC_ENCRYPTED:
1242 case STREAMTYPE_AC3_ENCRYPTED:
1243 case STREAMTYPE_DTS:
1244 return true;
1245 case STREAMTYPE_PES_PRIVATE_DATA:
1246 return (mStreamTypeExt == EXT_DESCRIPTOR_DVB_AC4
1247 || mStreamTypeExt == EXT_DESCRIPTOR_DVB_DTS_HD
1248 || mStreamTypeExt == EXT_DESCRIPTOR_DVB_DTS_UHD);
1249
1250 default:
1251 return false;
1252 }
1253 }
1254
isMeta() const1255 bool ATSParser::Stream::isMeta() const {
1256 if (mStreamType == STREAMTYPE_METADATA) {
1257 return true;
1258 }
1259 return false;
1260 }
1261
signalDiscontinuity(DiscontinuityType type,const sp<AMessage> & extra)1262 void ATSParser::Stream::signalDiscontinuity(
1263 DiscontinuityType type, const sp<AMessage> &extra) {
1264 mExpectedContinuityCounter = -1;
1265
1266 if (mQueue == NULL) {
1267 return;
1268 }
1269
1270 mPayloadStarted = false;
1271 mPesStartOffsets.clear();
1272 mEOSReached = false;
1273 mBuffer->setRange(0, 0);
1274 mSubSamples.clear();
1275
1276 bool clearFormat = false;
1277 if (isAudio()) {
1278 if (type & DISCONTINUITY_AUDIO_FORMAT) {
1279 clearFormat = true;
1280 }
1281 } else {
1282 if (type & DISCONTINUITY_VIDEO_FORMAT) {
1283 clearFormat = true;
1284 }
1285 }
1286
1287 mQueue->clear(clearFormat);
1288
1289 if (type & DISCONTINUITY_TIME) {
1290 uint64_t resumeAtPTS;
1291 if (extra != NULL
1292 && extra->findInt64(
1293 kATSParserKeyResumeAtPTS,
1294 (int64_t *)&resumeAtPTS)) {
1295 int64_t resumeAtMediaTimeUs =
1296 mProgram->convertPTSToTimestamp(resumeAtPTS);
1297
1298 extra->setInt64("resume-at-mediaTimeUs", resumeAtMediaTimeUs);
1299 }
1300 }
1301
1302 if (mSource != NULL) {
1303 sp<MetaData> meta = mSource->getFormat();
1304 const char* mime;
1305 if (clearFormat && meta != NULL && meta->findCString(kKeyMIMEType, &mime)
1306 && (!strncasecmp(mime, MEDIA_MIMETYPE_AUDIO_SCRAMBLED, 15)
1307 || !strncasecmp(mime, MEDIA_MIMETYPE_VIDEO_SCRAMBLED, 15))){
1308 mSource->clear();
1309 } else {
1310 mSource->queueDiscontinuity(type, extra, true);
1311 }
1312 }
1313 }
1314
signalEOS(status_t finalResult)1315 void ATSParser::Stream::signalEOS(status_t finalResult) {
1316 if (mSource != NULL) {
1317 mSource->signalEOS(finalResult);
1318 }
1319 mEOSReached = true;
1320 flush(NULL);
1321 }
1322
parsePES(ABitReader * br,SyncEvent * event)1323 status_t ATSParser::Stream::parsePES(ABitReader *br, SyncEvent *event) {
1324 const uint8_t *basePtr = br->data();
1325
1326 if (br->numBitsLeft() < 48) {
1327 ALOGE("Not enough data left in bitreader!");
1328 return ERROR_MALFORMED;
1329 }
1330 unsigned packet_startcode_prefix = br->getBits(24);
1331
1332 ALOGV("packet_startcode_prefix = 0x%08x", packet_startcode_prefix);
1333
1334 if (packet_startcode_prefix != 1) {
1335 ALOGV("Supposedly payload_unit_start=1 unit does not start "
1336 "with startcode.");
1337
1338 return ERROR_MALFORMED;
1339 }
1340
1341 unsigned stream_id = br->getBits(8);
1342 ALOGV("stream_id = 0x%02x", stream_id);
1343
1344 unsigned PES_packet_length = br->getBits(16);
1345 ALOGV("PES_packet_length = %u", PES_packet_length);
1346
1347 if (stream_id != 0xbc // program_stream_map
1348 && stream_id != 0xbe // padding_stream
1349 && stream_id != 0xbf // private_stream_2
1350 && stream_id != 0xf0 // ECM
1351 && stream_id != 0xf1 // EMM
1352 && stream_id != 0xff // program_stream_directory
1353 && stream_id != 0xf2 // DSMCC
1354 && stream_id != 0xf8) { // H.222.1 type E
1355 if (br->numBitsLeft() < 2 || br->getBits(2) != 2u) {
1356 return ERROR_MALFORMED;
1357 }
1358
1359 if (br->numBitsLeft() < 22) {
1360 ALOGE("Not enough data left in bitreader!");
1361 return ERROR_MALFORMED;
1362 }
1363 unsigned PES_scrambling_control = br->getBits(2);
1364 ALOGV("PES_scrambling_control = %u", PES_scrambling_control);
1365
1366 MY_LOGV("PES_priority = %u", br->getBits(1));
1367 MY_LOGV("data_alignment_indicator = %u", br->getBits(1));
1368 MY_LOGV("copyright = %u", br->getBits(1));
1369 MY_LOGV("original_or_copy = %u", br->getBits(1));
1370
1371 unsigned PTS_DTS_flags = br->getBits(2);
1372 ALOGV("PTS_DTS_flags = %u", PTS_DTS_flags);
1373
1374 unsigned ESCR_flag = br->getBits(1);
1375 ALOGV("ESCR_flag = %u", ESCR_flag);
1376
1377 unsigned ES_rate_flag = br->getBits(1);
1378 ALOGV("ES_rate_flag = %u", ES_rate_flag);
1379
1380 unsigned DSM_trick_mode_flag = br->getBits(1);
1381 ALOGV("DSM_trick_mode_flag = %u", DSM_trick_mode_flag);
1382
1383 unsigned additional_copy_info_flag = br->getBits(1);
1384 ALOGV("additional_copy_info_flag = %u", additional_copy_info_flag);
1385
1386 MY_LOGV("PES_CRC_flag = %u", br->getBits(1));
1387 MY_LOGV("PES_extension_flag = %u", br->getBits(1));
1388
1389 unsigned PES_header_data_length = br->getBits(8);
1390 ALOGV("PES_header_data_length = %u", PES_header_data_length);
1391
1392 unsigned optional_bytes_remaining = PES_header_data_length;
1393
1394 uint64_t PTS = 0, DTS = 0;
1395
1396 if (PTS_DTS_flags == 2 || PTS_DTS_flags == 3) {
1397 if (optional_bytes_remaining < 5u) {
1398 return ERROR_MALFORMED;
1399 }
1400
1401 if (br->numBitsLeft() < 7 || br->getBits(4) != PTS_DTS_flags) {
1402 return ERROR_MALFORMED;
1403 }
1404 PTS = ((uint64_t)br->getBits(3)) << 30;
1405 if (br->numBitsLeft() < 16 || br->getBits(1) != 1u) {
1406 return ERROR_MALFORMED;
1407 }
1408 PTS |= ((uint64_t)br->getBits(15)) << 15;
1409 if (br->numBitsLeft() < 16 || br->getBits(1) != 1u) {
1410 return ERROR_MALFORMED;
1411 }
1412 PTS |= br->getBits(15);
1413 if (br->numBitsLeft() < 1 || br->getBits(1) != 1u) {
1414 return ERROR_MALFORMED;
1415 }
1416
1417 ALOGV("PTS = 0x%016" PRIx64 " (%.2f)", PTS, PTS / 90000.0);
1418
1419 optional_bytes_remaining -= 5;
1420
1421 if (PTS_DTS_flags == 3) {
1422 if (optional_bytes_remaining < 5u) {
1423 return ERROR_MALFORMED;
1424 }
1425
1426 if (br->numBitsLeft() < 7 || br->getBits(4) != 1u) {
1427 return ERROR_MALFORMED;
1428 }
1429
1430 DTS = ((uint64_t)br->getBits(3)) << 30;
1431 if (br->numBitsLeft() < 16 || br->getBits(1) != 1u) {
1432 return ERROR_MALFORMED;
1433 }
1434 DTS |= ((uint64_t)br->getBits(15)) << 15;
1435 if (br->numBitsLeft() < 16 || br->getBits(1) != 1u) {
1436 return ERROR_MALFORMED;
1437 }
1438 DTS |= br->getBits(15);
1439 if (br->numBitsLeft() < 1 || br->getBits(1) != 1u) {
1440 return ERROR_MALFORMED;
1441 }
1442
1443 ALOGV("DTS = %" PRIu64, DTS);
1444
1445 optional_bytes_remaining -= 5;
1446 }
1447 }
1448
1449 if (ESCR_flag) {
1450 if (optional_bytes_remaining < 6u) {
1451 return ERROR_MALFORMED;
1452 }
1453
1454 if (br->numBitsLeft() < 5) {
1455 ALOGE("Not enough data left in bitreader!");
1456 return ERROR_MALFORMED;
1457 }
1458 br->getBits(2);
1459
1460 uint64_t ESCR = ((uint64_t)br->getBits(3)) << 30;
1461 if (br->numBitsLeft() < 16 || br->getBits(1) != 1u) {
1462 return ERROR_MALFORMED;
1463 }
1464 ESCR |= ((uint64_t)br->getBits(15)) << 15;
1465 if (br->numBitsLeft() < 16 || br->getBits(1) != 1u) {
1466 return ERROR_MALFORMED;
1467 }
1468 ESCR |= br->getBits(15);
1469 if (br->numBitsLeft() < 1 || br->getBits(1) != 1u) {
1470 return ERROR_MALFORMED;
1471 }
1472
1473 ALOGV("ESCR = %" PRIu64, ESCR);
1474 if (br->numBitsLeft() < 10) {
1475 ALOGE("Not enough data left in bitreader!");
1476 return ERROR_MALFORMED;
1477 }
1478 MY_LOGV("ESCR_extension = %u", br->getBits(9));
1479
1480 if (br->getBits(1) != 1u) {
1481 return ERROR_MALFORMED;
1482 }
1483
1484 optional_bytes_remaining -= 6;
1485 }
1486
1487 if (ES_rate_flag) {
1488 if (optional_bytes_remaining < 3u) {
1489 return ERROR_MALFORMED;
1490 }
1491
1492 if (br->numBitsLeft() < 1 || br->getBits(1) != 1u) {
1493 return ERROR_MALFORMED;
1494 }
1495 if (br->numBitsLeft() < 22) {
1496 ALOGE("Not enough data left in bitreader!");
1497 return ERROR_MALFORMED;
1498 }
1499 MY_LOGV("ES_rate = %u", br->getBits(22));
1500 if (br->numBitsLeft() < 1 || br->getBits(1) != 1u) {
1501 return ERROR_MALFORMED;
1502 }
1503
1504 optional_bytes_remaining -= 3;
1505 }
1506
1507 if (!br->skipBits(optional_bytes_remaining * 8)) {
1508 ALOGE("Not enough data left in bitreader!");
1509 return ERROR_MALFORMED;
1510 }
1511
1512 // ES data follows.
1513 int32_t pesOffset = br->data() - basePtr;
1514
1515 if (PES_packet_length != 0) {
1516 if (PES_packet_length < PES_header_data_length + 3) {
1517 return ERROR_MALFORMED;
1518 }
1519
1520 unsigned dataLength =
1521 PES_packet_length - 3 - PES_header_data_length;
1522
1523 if (br->numBitsLeft() < dataLength * 8) {
1524 ALOGE("PES packet does not carry enough data to contain "
1525 "payload. (numBitsLeft = %zu, required = %u)",
1526 br->numBitsLeft(), dataLength * 8);
1527
1528 return ERROR_MALFORMED;
1529 }
1530
1531 ALOGV("There's %u bytes of payload, PES_packet_length=%u, offset=%d",
1532 dataLength, PES_packet_length, pesOffset);
1533
1534 onPayloadData(
1535 PTS_DTS_flags, PTS, DTS, PES_scrambling_control,
1536 br->data(), dataLength, pesOffset, event);
1537
1538 if (!br->skipBits(dataLength * 8)) {
1539 ALOGE("Not enough data left in bitreader!");
1540 return ERROR_MALFORMED;
1541 }
1542 } else {
1543 onPayloadData(
1544 PTS_DTS_flags, PTS, DTS, PES_scrambling_control,
1545 br->data(), br->numBitsLeft() / 8, pesOffset, event);
1546
1547 size_t payloadSizeBits = br->numBitsLeft();
1548 if (payloadSizeBits % 8 != 0u) {
1549 return ERROR_MALFORMED;
1550 }
1551
1552 ALOGV("There's %zu bytes of payload, offset=%d",
1553 payloadSizeBits / 8, pesOffset);
1554 }
1555 } else if (stream_id == 0xbe) { // padding_stream
1556 if (PES_packet_length == 0u || !br->skipBits(PES_packet_length * 8)) {
1557 return ERROR_MALFORMED;
1558 }
1559 } else {
1560 if (PES_packet_length == 0u || !br->skipBits(PES_packet_length * 8)) {
1561 return ERROR_MALFORMED;
1562 }
1563 }
1564
1565 return OK;
1566 }
1567
getPesScramblingControl(ABitReader * br,int32_t * pesOffset)1568 uint32_t ATSParser::Stream::getPesScramblingControl(
1569 ABitReader *br, int32_t *pesOffset) {
1570 if (br->numBitsLeft() < 24) {
1571 ALOGE("Not enough data left in bitreader!");
1572 return 0;
1573 }
1574 unsigned packet_startcode_prefix = br->getBits(24);
1575
1576 ALOGV("packet_startcode_prefix = 0x%08x", packet_startcode_prefix);
1577
1578 if (packet_startcode_prefix != 1) {
1579 ALOGV("unit does not start with startcode.");
1580 return 0;
1581 }
1582
1583 if (br->numBitsLeft() < 48) {
1584 ALOGE("Not enough data left in bitreader!");
1585 return 0;
1586 }
1587
1588 unsigned stream_id = br->getBits(8);
1589 ALOGV("stream_id = 0x%02x", stream_id);
1590
1591 br->skipBits(16); // PES_packet_length
1592
1593 if (stream_id != 0xbc // program_stream_map
1594 && stream_id != 0xbe // padding_stream
1595 && stream_id != 0xbf // private_stream_2
1596 && stream_id != 0xf0 // ECM
1597 && stream_id != 0xf1 // EMM
1598 && stream_id != 0xff // program_stream_directory
1599 && stream_id != 0xf2 // DSMCC
1600 && stream_id != 0xf8) { // H.222.1 type E
1601 if (br->getBits(2) != 2u) {
1602 return 0;
1603 }
1604
1605 unsigned PES_scrambling_control = br->getBits(2);
1606 ALOGV("PES_scrambling_control = %u", PES_scrambling_control);
1607
1608 if (PES_scrambling_control == 0) {
1609 return 0;
1610 }
1611
1612 br->skipBits(12); // don't care
1613
1614 unsigned PES_header_data_length = br->getBits(8);
1615 ALOGV("PES_header_data_length = %u", PES_header_data_length);
1616
1617 if (PES_header_data_length * 8 > br->numBitsLeft()) {
1618 return 0;
1619 }
1620
1621 *pesOffset = 9 + PES_header_data_length;
1622 ALOGD("found PES_scrambling_control=%d, PES offset=%d",
1623 PES_scrambling_control, *pesOffset);
1624 return PES_scrambling_control;
1625 }
1626
1627 return 0;
1628 }
1629
flushScrambled(SyncEvent * event)1630 status_t ATSParser::Stream::flushScrambled(SyncEvent *event) {
1631 if (mDescrambler == NULL) {
1632 ALOGE("received scrambled packets without descrambler!");
1633 return UNKNOWN_ERROR;
1634 }
1635
1636 if (mDescrambledBuffer == NULL || mHidlMemory == NULL) {
1637 ALOGE("received scrambled packets without shared memory!");
1638
1639 return UNKNOWN_ERROR;
1640 }
1641
1642 int32_t pesOffset = 0;
1643 int32_t descrambleSubSamples = 0, descrambleBytes = 0;
1644 uint32_t tsScramblingControl = 0, pesScramblingControl = 0;
1645
1646 // First, go over subsamples to find TS-level scrambling key id, and
1647 // calculate how many subsample we need to descramble (assuming we don't
1648 // have PES-level scrambling).
1649 for (auto it = mSubSamples.begin(); it != mSubSamples.end(); it++) {
1650 if (it->transport_scrambling_mode != 0) {
1651 // TODO: handle keyId change, use the first non-zero keyId for now.
1652 if (tsScramblingControl == 0) {
1653 tsScramblingControl = it->transport_scrambling_mode;
1654 }
1655 }
1656 if (tsScramblingControl == 0 || descrambleSubSamples == 0
1657 || !mQueue->isScrambled()) {
1658 descrambleSubSamples++;
1659 descrambleBytes += it->subSampleSize;
1660 }
1661 }
1662 // If not scrambled at TS-level, check PES-level scrambling
1663 if (tsScramblingControl == 0) {
1664 ABitReader br(mBuffer->data(), mBuffer->size());
1665 pesScramblingControl = getPesScramblingControl(&br, &pesOffset);
1666 // If not scrambled at PES-level either, or scrambled at PES-level but
1667 // requires output to remain scrambled, we don't need to descramble
1668 // anything.
1669 if (pesScramblingControl == 0 || mQueue->isScrambled()) {
1670 descrambleSubSamples = 0;
1671 descrambleBytes = 0;
1672 }
1673 }
1674
1675 uint32_t sctrl = tsScramblingControl != 0 ?
1676 tsScramblingControl : pesScramblingControl;
1677 if (mQueue->isScrambled()) {
1678 sctrl |= DescramblerPlugin::kScrambling_Flag_PesHeader;
1679 }
1680
1681 // Perform the 1st pass descrambling if needed
1682 if (descrambleBytes > 0) {
1683 memcpy(mDescrambledBuffer->data(), mBuffer->data(), descrambleBytes);
1684 mDescrambledBuffer->setRange(0, mBuffer->size());
1685
1686 hidl_vec<SubSample> subSamples;
1687 subSamples.resize(descrambleSubSamples);
1688
1689 int32_t i = 0;
1690 for (auto it = mSubSamples.begin();
1691 it != mSubSamples.end() && i < descrambleSubSamples; it++, i++) {
1692 if (it->transport_scrambling_mode != 0 || pesScramblingControl != 0) {
1693 subSamples[i].numBytesOfClearData = 0;
1694 subSamples[i].numBytesOfEncryptedData = it->subSampleSize;
1695 } else {
1696 subSamples[i].numBytesOfClearData = it->subSampleSize;
1697 subSamples[i].numBytesOfEncryptedData = 0;
1698 }
1699 }
1700
1701 // If scrambled at PES-level, PES header is in the clear
1702 if (pesScramblingControl != 0) {
1703 subSamples[0].numBytesOfClearData = pesOffset;
1704 subSamples[0].numBytesOfEncryptedData -= pesOffset;
1705 }
1706
1707 Status status = Status::OK;
1708 uint32_t bytesWritten = 0;
1709 hidl_string detailedError;
1710
1711 DestinationBuffer dstBuffer;
1712 dstBuffer.type = BufferType::SHARED_MEMORY;
1713 dstBuffer.nonsecureMemory = mDescramblerSrcBuffer;
1714
1715 auto returnVoid = mDescrambler->descramble(
1716 (ScramblingControl) sctrl,
1717 subSamples,
1718 mDescramblerSrcBuffer,
1719 0 /*srcOffset*/,
1720 dstBuffer,
1721 0 /*dstOffset*/,
1722 [&status, &bytesWritten, &detailedError] (
1723 Status _status, uint32_t _bytesWritten,
1724 const hidl_string& _detailedError) {
1725 status = _status;
1726 bytesWritten = _bytesWritten;
1727 detailedError = _detailedError;
1728 });
1729
1730 if (!returnVoid.isOk() || status != Status::OK) {
1731 ALOGE("[stream %d] descramble failed, trans=%s, status=%d",
1732 mElementaryPID, returnVoid.description().c_str(), status);
1733 return UNKNOWN_ERROR;
1734 }
1735
1736 ALOGV("[stream %d] descramble succeeded, %d bytes",
1737 mElementaryPID, bytesWritten);
1738
1739 // Set descrambleBytes to the returned result.
1740 // Note that this might be smaller than the total length of input data.
1741 // (eg. when we're descrambling the PES header portion of a secure stream,
1742 // the plugin might cut it off right after the PES header.)
1743 descrambleBytes = bytesWritten;
1744 }
1745
1746 // |buffer| points to the buffer from which we'd parse the PES header.
1747 // When the output stream is scrambled, it points to mDescrambledBuffer
1748 // (unless all packets in this PES are actually clear, in which case,
1749 // it points to mBuffer since we never copied into mDescrambledBuffer).
1750 // When the output stream is clear, it points to mBuffer, and we'll
1751 // copy all descrambled data back to mBuffer.
1752 sp<ABuffer> buffer = mBuffer;
1753 if (mQueue->isScrambled()) {
1754 // Queue subSample info for scrambled queue
1755 sp<ABuffer> clearSizesBuffer = new ABuffer(mSubSamples.size() * 4);
1756 sp<ABuffer> encSizesBuffer = new ABuffer(mSubSamples.size() * 4);
1757 int32_t *clearSizePtr = (int32_t*)clearSizesBuffer->data();
1758 int32_t *encSizePtr = (int32_t*)encSizesBuffer->data();
1759 int32_t isSync = 0;
1760 int32_t i = 0;
1761 for (auto it = mSubSamples.begin();
1762 it != mSubSamples.end(); it++, i++) {
1763 if ((it->transport_scrambling_mode == 0
1764 && pesScramblingControl == 0)) {
1765 clearSizePtr[i] = it->subSampleSize;
1766 encSizePtr[i] = 0;
1767 } else {
1768 clearSizePtr[i] = 0;
1769 encSizePtr[i] = it->subSampleSize;
1770 }
1771 isSync |= it->random_access_indicator;
1772 }
1773
1774 // If scrambled at PES-level, PES header is in the clear
1775 if (pesScramblingControl != 0) {
1776 clearSizePtr[0] = pesOffset;
1777 encSizePtr[0] -= pesOffset;
1778 }
1779 // Pass the original TS subsample size now. The PES header adjust
1780 // will be applied when the scrambled AU is dequeued.
1781 // Note that if descrambleBytes is 0, it means this PES contains only
1782 // all ts packets, leadingClearBytes is entire buffer size.
1783 mQueue->appendScrambledData(
1784 mBuffer->data(), mBuffer->size(),
1785 (descrambleBytes > 0) ? descrambleBytes : mBuffer->size(),
1786 sctrl, isSync, clearSizesBuffer, encSizesBuffer);
1787
1788 if (descrambleBytes > 0) {
1789 buffer = mDescrambledBuffer;
1790 }
1791 } else {
1792 memcpy(mBuffer->data(), mDescrambledBuffer->data(), descrambleBytes);
1793 }
1794
1795 ABitReader br(buffer->data(), buffer->size());
1796 status_t err = parsePES(&br, event);
1797
1798 if (err != OK) {
1799 ALOGE("[stream %d] failed to parse descrambled PES, err=%d",
1800 mElementaryPID, err);
1801 }
1802
1803 return err;
1804 }
1805
1806
flush(SyncEvent * event)1807 status_t ATSParser::Stream::flush(SyncEvent *event) {
1808 if (mBuffer == NULL || mBuffer->size() == 0) {
1809 return OK;
1810 }
1811
1812 ALOGV("flushing stream 0x%04x size = %zu", mElementaryPID, mBuffer->size());
1813
1814 status_t err = OK;
1815 if (mScrambled) {
1816 err = flushScrambled(event);
1817 mSubSamples.clear();
1818 } else {
1819 ABitReader br(mBuffer->data(), mBuffer->size());
1820 err = parsePES(&br, event);
1821 }
1822
1823 mBuffer->setRange(0, 0);
1824
1825 return err;
1826 }
1827
addAudioPresentations(const sp<ABuffer> & buffer)1828 void ATSParser::Stream::addAudioPresentations(const sp<ABuffer> &buffer) {
1829 std::ostringstream outStream(std::ios::out);
1830 serializeAudioPresentations(mAudioPresentations, &outStream);
1831 sp<ABuffer> ap = ABuffer::CreateAsCopy(outStream.str().data(), outStream.str().size());
1832 buffer->meta()->setBuffer("audio-presentation-info", ap);
1833 }
1834
onPayloadData(unsigned PTS_DTS_flags,uint64_t PTS,uint64_t,unsigned PES_scrambling_control,const uint8_t * data,size_t size,int32_t payloadOffset,SyncEvent * event)1835 void ATSParser::Stream::onPayloadData(
1836 unsigned PTS_DTS_flags, uint64_t PTS, uint64_t /* DTS */,
1837 unsigned PES_scrambling_control,
1838 const uint8_t *data, size_t size,
1839 int32_t payloadOffset, SyncEvent *event) {
1840 #if 0
1841 ALOGI("payload streamType 0x%02x, PTS = 0x%016llx, dPTS = %lld",
1842 mStreamType,
1843 PTS,
1844 (int64_t)PTS - mPrevPTS);
1845 mPrevPTS = PTS;
1846 #endif
1847
1848 ALOGV("onPayloadData mStreamType=0x%02x size: %zu", mStreamType, size);
1849
1850 int64_t timeUs = 0LL; // no presentation timestamp available.
1851 if (PTS_DTS_flags == 2 || PTS_DTS_flags == 3) {
1852 timeUs = mProgram->convertPTSToTimestamp(PTS);
1853 }
1854
1855 status_t err = mQueue->appendData(
1856 data, size, timeUs, payloadOffset, PES_scrambling_control);
1857
1858 if (mEOSReached) {
1859 mQueue->signalEOS();
1860 }
1861
1862 if (err != OK) {
1863 return;
1864 }
1865
1866 sp<ABuffer> accessUnit;
1867 bool found = false;
1868 while ((accessUnit = mQueue->dequeueAccessUnit()) != NULL) {
1869 if (mSource == NULL) {
1870 sp<MetaData> meta = mQueue->getFormat();
1871
1872 if (meta != NULL) {
1873 ALOGV("Stream PID 0x%08x of type 0x%02x now has data.",
1874 mElementaryPID, mStreamType);
1875
1876 const char *mime;
1877 if (meta->findCString(kKeyMIMEType, &mime)
1878 && !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC)) {
1879 int32_t sync = 0;
1880 if (!accessUnit->meta()->findInt32("isSync", &sync) || !sync) {
1881 continue;
1882 }
1883 }
1884 mSource = new AnotherPacketSource(meta);
1885 if (mAudioPresentations.size() > 0) {
1886 addAudioPresentations(accessUnit);
1887 }
1888 mSource->queueAccessUnit(accessUnit);
1889 ALOGV("onPayloadData: created AnotherPacketSource PID 0x%08x of type 0x%02x",
1890 mElementaryPID, mStreamType);
1891 }
1892 } else if (mQueue->getFormat() != NULL) {
1893 // After a discontinuity we invalidate the queue's format
1894 // and won't enqueue any access units to the source until
1895 // the queue has reestablished the new format.
1896
1897 if (mSource->getFormat() == NULL) {
1898 mSource->setFormat(mQueue->getFormat());
1899 }
1900 if (mAudioPresentations.size() > 0) {
1901 addAudioPresentations(accessUnit);
1902 }
1903 mSource->queueAccessUnit(accessUnit);
1904 }
1905
1906 // Every access unit has a pesStartOffset queued in |mPesStartOffsets|.
1907 off64_t pesStartOffset = -1;
1908 if (!mPesStartOffsets.empty()) {
1909 pesStartOffset = *mPesStartOffsets.begin();
1910 mPesStartOffsets.erase(mPesStartOffsets.begin());
1911 }
1912
1913 if (pesStartOffset >= 0 && (event != NULL) && !found && mQueue->getFormat() != NULL) {
1914 int32_t sync = 0;
1915 if (accessUnit->meta()->findInt32("isSync", &sync) && sync) {
1916 int64_t timeUs;
1917 if (accessUnit->meta()->findInt64("timeUs", &timeUs)) {
1918 found = true;
1919 event->init(pesStartOffset, mSource, timeUs, getSourceType());
1920 }
1921 }
1922 }
1923 }
1924 }
1925
getSourceType()1926 ATSParser::SourceType ATSParser::Stream::getSourceType() {
1927 if (isVideo()) {
1928 return VIDEO;
1929 } else if (isAudio()) {
1930 return AUDIO;
1931 } else if (isMeta()) {
1932 return META;
1933 }
1934 return NUM_SOURCE_TYPES;
1935 }
1936
getSource(SourceType type)1937 sp<AnotherPacketSource> ATSParser::Stream::getSource(SourceType type) {
1938 switch (type) {
1939 case VIDEO:
1940 {
1941 if (isVideo()) {
1942 return mSource;
1943 }
1944 break;
1945 }
1946
1947 case AUDIO:
1948 {
1949 if (isAudio()) {
1950 return mSource;
1951 }
1952 break;
1953 }
1954
1955 case META:
1956 {
1957 if (isMeta()) {
1958 return mSource;
1959 }
1960 break;
1961 }
1962
1963 default:
1964 break;
1965 }
1966
1967 return NULL;
1968 }
1969
setCasInfo(int32_t systemId,const sp<IDescrambler> & descrambler,const std::vector<uint8_t> & sessionId)1970 void ATSParser::Stream::setCasInfo(
1971 int32_t systemId, const sp<IDescrambler> &descrambler,
1972 const std::vector<uint8_t> &sessionId) {
1973 if (mSource != NULL && mDescrambler == NULL && descrambler != NULL) {
1974 signalDiscontinuity(DISCONTINUITY_FORMAT_ONLY, NULL);
1975 mDescrambler = descrambler;
1976 if (mQueue->isScrambled()) {
1977 mQueue->setCasInfo(systemId, sessionId);
1978 }
1979 }
1980 }
1981
1982 ////////////////////////////////////////////////////////////////////////////////
1983
ATSParser(uint32_t flags)1984 ATSParser::ATSParser(uint32_t flags)
1985 : mFlags(flags),
1986 mAbsoluteTimeAnchorUs(-1LL),
1987 mTimeOffsetValid(false),
1988 mTimeOffsetUs(0LL),
1989 mLastRecoveredPTS(-1LL),
1990 mNumTSPacketsParsed(0),
1991 mNumPCRs(0) {
1992 mPSISections.add(0 /* PID */, new PSISection);
1993 mCasManager = new CasManager();
1994 }
1995
~ATSParser()1996 ATSParser::~ATSParser() {
1997 }
1998
feedTSPacket(const void * data,size_t size,SyncEvent * event)1999 status_t ATSParser::feedTSPacket(const void *data, size_t size,
2000 SyncEvent *event) {
2001 if (size != kTSPacketSize) {
2002 ALOGE("Wrong TS packet size");
2003 return BAD_VALUE;
2004 }
2005
2006 ABitReader br((const uint8_t *)data, kTSPacketSize);
2007 return parseTS(&br, event);
2008 }
2009
setMediaCas(const sp<ICas> & cas)2010 status_t ATSParser::setMediaCas(const sp<ICas> &cas) {
2011 status_t err = mCasManager->setMediaCas(cas);
2012 if (err != OK) {
2013 return err;
2014 }
2015 for (size_t i = 0; i < mPrograms.size(); ++i) {
2016 mPrograms.editItemAt(i)->updateCasSessions();
2017 }
2018 return OK;
2019 }
2020
signalDiscontinuity(DiscontinuityType type,const sp<AMessage> & extra)2021 void ATSParser::signalDiscontinuity(
2022 DiscontinuityType type, const sp<AMessage> &extra) {
2023 int64_t mediaTimeUs;
2024 if ((type & DISCONTINUITY_TIME) && extra != NULL) {
2025 if (extra->findInt64(kATSParserKeyMediaTimeUs, &mediaTimeUs)) {
2026 mAbsoluteTimeAnchorUs = mediaTimeUs;
2027 }
2028 if ((mFlags & TS_TIMESTAMPS_ARE_ABSOLUTE)
2029 && extra->findInt64(
2030 kATSParserKeyRecentMediaTimeUs, &mediaTimeUs)) {
2031 if (mAbsoluteTimeAnchorUs >= 0LL) {
2032 mediaTimeUs -= mAbsoluteTimeAnchorUs;
2033 }
2034 if (mTimeOffsetValid) {
2035 mediaTimeUs -= mTimeOffsetUs;
2036 }
2037 mLastRecoveredPTS = (mediaTimeUs * 9) / 100;
2038 }
2039 } else if (type == DISCONTINUITY_ABSOLUTE_TIME) {
2040 int64_t timeUs;
2041 if (!extra->findInt64("timeUs", &timeUs)) {
2042 ALOGE("timeUs not found");
2043 return;
2044 }
2045
2046 if (!mPrograms.empty()) {
2047 ALOGE("mPrograms is not empty");
2048 return;
2049 }
2050 mAbsoluteTimeAnchorUs = timeUs;
2051 return;
2052 } else if (type == DISCONTINUITY_TIME_OFFSET) {
2053 int64_t offset;
2054 if (!extra->findInt64("offset", &offset)) {
2055 ALOGE("offset not found");
2056 return;
2057 }
2058
2059 mTimeOffsetValid = true;
2060 mTimeOffsetUs = offset;
2061 return;
2062 }
2063
2064 for (size_t i = 0; i < mPrograms.size(); ++i) {
2065 mPrograms.editItemAt(i)->signalDiscontinuity(type, extra);
2066 }
2067 }
2068
signalEOS(status_t finalResult)2069 void ATSParser::signalEOS(status_t finalResult) {
2070 if (finalResult == (status_t) OK) {
2071 ALOGE("finalResult not OK");
2072 return;
2073 }
2074
2075 for (size_t i = 0; i < mPrograms.size(); ++i) {
2076 mPrograms.editItemAt(i)->signalEOS(finalResult);
2077 }
2078 }
2079
parseProgramAssociationTable(ABitReader * br)2080 void ATSParser::parseProgramAssociationTable(ABitReader *br) {
2081 if (br->numBitsLeft() < 8) {
2082 ALOGE("Not enough data left in bitreader!");
2083 return;
2084 }
2085 unsigned table_id = br->getBits(8);
2086 ALOGV(" table_id = %u", table_id);
2087 if (table_id != 0x00u) {
2088 ALOGE("PAT data error!");
2089 return ;
2090 }
2091 if (br->numBitsLeft() < 56) {
2092 ALOGE("Not enough data left in bitreader!");
2093 return;
2094 }
2095 unsigned section_syntax_indictor = br->getBits(1);
2096 ALOGV(" section_syntax_indictor = %u", section_syntax_indictor);
2097
2098 br->skipBits(1); // '0'
2099 MY_LOGV(" reserved = %u", br->getBits(2));
2100
2101 unsigned section_length = br->getBits(12);
2102 ALOGV(" section_length = %u", section_length);
2103
2104 MY_LOGV(" transport_stream_id = %u", br->getBits(16));
2105 MY_LOGV(" reserved = %u", br->getBits(2));
2106 MY_LOGV(" version_number = %u", br->getBits(5));
2107 MY_LOGV(" current_next_indicator = %u", br->getBits(1));
2108 MY_LOGV(" section_number = %u", br->getBits(8));
2109 MY_LOGV(" last_section_number = %u", br->getBits(8));
2110
2111 // check for unsigned integer overflow before assigning it to numProgramBytes
2112 if (section_length < 9) {
2113 return;
2114 }
2115 size_t numProgramBytes = (section_length - 5 /* header */ - 4 /* crc */);
2116
2117 for (size_t i = 0; i < numProgramBytes / 4; ++i) {
2118 if (br->numBitsLeft() < 32) {
2119 ALOGE("Not enough data left in bitreader!");
2120 return;
2121 }
2122 unsigned program_number = br->getBits(16);
2123 ALOGV(" program_number = %u", program_number);
2124
2125 MY_LOGV(" reserved = %u", br->getBits(3));
2126
2127 if (program_number == 0) {
2128 MY_LOGV(" network_PID = 0x%04x", br->getBits(13));
2129 } else {
2130 unsigned programMapPID = br->getBits(13);
2131
2132 ALOGV(" program_map_PID = 0x%04x", programMapPID);
2133
2134 bool found = false;
2135 for (size_t index = 0; index < mPrograms.size(); ++index) {
2136 const sp<Program> &program = mPrograms.itemAt(index);
2137
2138 if (program->number() == program_number) {
2139 program->updateProgramMapPID(programMapPID);
2140 found = true;
2141 break;
2142 }
2143 }
2144
2145 if (!found) {
2146 mPrograms.push(
2147 new Program(this, program_number, programMapPID, mLastRecoveredPTS));
2148 if (mSampleAesKeyItem != NULL) {
2149 mPrograms.top()->signalNewSampleAesKey(mSampleAesKeyItem);
2150 }
2151 }
2152
2153 if (mPSISections.indexOfKey(programMapPID) < 0) {
2154 mPSISections.add(programMapPID, new PSISection);
2155 }
2156 }
2157 }
2158
2159 if (br->numBitsLeft() < 32) {
2160 ALOGE("Not enough data left in bitreader!");
2161 return;
2162 }
2163 MY_LOGV(" CRC = 0x%08x", br->getBits(32));
2164 }
2165
parsePID(ABitReader * br,unsigned PID,unsigned continuity_counter,unsigned payload_unit_start_indicator,unsigned transport_scrambling_control,unsigned random_access_indicator,SyncEvent * event)2166 status_t ATSParser::parsePID(
2167 ABitReader *br, unsigned PID,
2168 unsigned continuity_counter,
2169 unsigned payload_unit_start_indicator,
2170 unsigned transport_scrambling_control,
2171 unsigned random_access_indicator,
2172 SyncEvent *event) {
2173 ssize_t sectionIndex = mPSISections.indexOfKey(PID);
2174
2175 if (sectionIndex >= 0) {
2176 sp<PSISection> section = mPSISections.valueAt(sectionIndex);
2177
2178 if (payload_unit_start_indicator) {
2179 if (!section->isEmpty()) {
2180 ALOGW("parsePID encounters payload_unit_start_indicator when section is not empty");
2181 section->clear();
2182 }
2183
2184 if (br->numBitsLeft() < 8) {
2185 ALOGE("Not enough data left in bitreader!");
2186 return ERROR_MALFORMED;
2187 }
2188 unsigned skip = br->getBits(8);
2189 section->setSkipBytes(skip + 1); // skip filler bytes + pointer field itself
2190 if (!br->skipBits(skip * 8)) {
2191 ALOGE("Not enough data left in bitreader!");
2192 return ERROR_MALFORMED;
2193 }
2194 }
2195
2196 if (br->numBitsLeft() % 8 != 0) {
2197 return ERROR_MALFORMED;
2198 }
2199 status_t err = section->append(br->data(), br->numBitsLeft() / 8);
2200
2201 if (err != OK) {
2202 return err;
2203 }
2204
2205 if (!section->isComplete()) {
2206 return OK;
2207 }
2208
2209 if (!section->isCRCOkay()) {
2210 return BAD_VALUE;
2211 }
2212 ABitReader sectionBits(section->data(), section->size());
2213
2214 if (PID == 0) {
2215 parseProgramAssociationTable(§ionBits);
2216 } else {
2217 bool handled = false;
2218 for (size_t i = 0; i < mPrograms.size(); ++i) {
2219 status_t err;
2220 if (!mPrograms.editItemAt(i)->parsePSISection(
2221 PID, §ionBits, &err)) {
2222 continue;
2223 }
2224
2225 if (err != OK) {
2226 return err;
2227 }
2228
2229 handled = true;
2230 break;
2231 }
2232
2233 if (!handled) {
2234 mPSISections.removeItem(PID);
2235 section.clear();
2236 }
2237 }
2238
2239 if (section != NULL) {
2240 section->clear();
2241 }
2242
2243 return OK;
2244 }
2245
2246 bool handled = false;
2247 for (size_t i = 0; i < mPrograms.size(); ++i) {
2248 status_t err;
2249 if (mPrograms.editItemAt(i)->parsePID(
2250 PID, continuity_counter,
2251 payload_unit_start_indicator,
2252 transport_scrambling_control,
2253 random_access_indicator,
2254 br, &err, event)) {
2255 if (err != OK) {
2256 return err;
2257 }
2258
2259 handled = true;
2260 break;
2261 }
2262 }
2263
2264 if (!handled) {
2265 handled = mCasManager->parsePID(br, PID);
2266 }
2267
2268 if (!handled) {
2269 ALOGV("PID 0x%04x not handled.", PID);
2270 }
2271
2272 return OK;
2273 }
2274
parseAdaptationField(ABitReader * br,unsigned PID,unsigned * random_access_indicator)2275 status_t ATSParser::parseAdaptationField(
2276 ABitReader *br, unsigned PID, unsigned *random_access_indicator) {
2277 *random_access_indicator = 0;
2278 if (br->numBitsLeft() < 8) {
2279 ALOGE("Not enough data left in bitreader!");
2280 return ERROR_MALFORMED;
2281 }
2282 unsigned adaptation_field_length = br->getBits(8);
2283
2284 if (adaptation_field_length > 0) {
2285 if (adaptation_field_length * 8 > br->numBitsLeft()) {
2286 ALOGV("Adaptation field should be included in a single TS packet.");
2287 return ERROR_MALFORMED;
2288 }
2289
2290 unsigned discontinuity_indicator = br->getBits(1);
2291
2292 if (discontinuity_indicator) {
2293 ALOGV("PID 0x%04x: discontinuity_indicator = 1 (!!!)", PID);
2294 }
2295
2296 *random_access_indicator = br->getBits(1);
2297 if (*random_access_indicator) {
2298 ALOGV("PID 0x%04x: random_access_indicator = 1", PID);
2299 }
2300
2301 unsigned elementary_stream_priority_indicator = br->getBits(1);
2302 if (elementary_stream_priority_indicator) {
2303 ALOGV("PID 0x%04x: elementary_stream_priority_indicator = 1", PID);
2304 }
2305
2306 unsigned PCR_flag = br->getBits(1);
2307
2308 size_t numBitsRead = 4;
2309
2310 if (PCR_flag) {
2311 if (adaptation_field_length * 8 < 52) {
2312 return ERROR_MALFORMED;
2313 }
2314 br->skipBits(4);
2315 uint64_t PCR_base = br->getBits(32);
2316 PCR_base = (PCR_base << 1) | br->getBits(1);
2317
2318 br->skipBits(6);
2319 unsigned PCR_ext = br->getBits(9);
2320
2321 // The number of bytes from the start of the current
2322 // MPEG2 transport stream packet up and including
2323 // the final byte of this PCR_ext field.
2324 size_t byteOffsetFromStartOfTSPacket =
2325 (188 - br->numBitsLeft() / 8);
2326
2327 uint64_t PCR = PCR_base * 300 + PCR_ext;
2328
2329 ALOGV("PID 0x%04x: PCR = 0x%016" PRIx64 " (%.2f)",
2330 PID, PCR, PCR / 27E6);
2331
2332 // The number of bytes received by this parser up to and
2333 // including the final byte of this PCR_ext field.
2334 uint64_t byteOffsetFromStart =
2335 uint64_t(mNumTSPacketsParsed) * 188 + byteOffsetFromStartOfTSPacket;
2336
2337 for (size_t i = 0; i < mPrograms.size(); ++i) {
2338 updatePCR(PID, PCR, byteOffsetFromStart);
2339 }
2340
2341 numBitsRead += 52;
2342 }
2343
2344 br->skipBits(adaptation_field_length * 8 - numBitsRead);
2345 }
2346 return OK;
2347 }
2348
parseTS(ABitReader * br,SyncEvent * event)2349 status_t ATSParser::parseTS(ABitReader *br, SyncEvent *event) {
2350 ALOGV("---");
2351
2352 if (br->numBitsLeft() < 32) {
2353 ALOGE("Not enough data left in bitreader!");
2354 return ERROR_MALFORMED;
2355 }
2356 unsigned sync_byte = br->getBits(8);
2357 if (sync_byte != 0x47u) {
2358 ALOGE("[error] parseTS: return error as sync_byte=0x%x", sync_byte);
2359 return BAD_VALUE;
2360 }
2361
2362 if (br->getBits(1)) { // transport_error_indicator
2363 // silently ignore.
2364 return OK;
2365 }
2366
2367 unsigned payload_unit_start_indicator = br->getBits(1);
2368 ALOGV("payload_unit_start_indicator = %u", payload_unit_start_indicator);
2369
2370 MY_LOGV("transport_priority = %u", br->getBits(1));
2371
2372 unsigned PID = br->getBits(13);
2373 ALOGV("PID = 0x%04x", PID);
2374
2375 unsigned transport_scrambling_control = br->getBits(2);
2376 ALOGV("transport_scrambling_control = %u", transport_scrambling_control);
2377
2378 unsigned adaptation_field_control = br->getBits(2);
2379 ALOGV("adaptation_field_control = %u", adaptation_field_control);
2380
2381 unsigned continuity_counter = br->getBits(4);
2382 ALOGV("PID = 0x%04x, continuity_counter = %u", PID, continuity_counter);
2383
2384 // ALOGI("PID = 0x%04x, continuity_counter = %u", PID, continuity_counter);
2385
2386 status_t err = OK;
2387
2388 unsigned random_access_indicator = 0;
2389 if (adaptation_field_control == 2 || adaptation_field_control == 3) {
2390 err = parseAdaptationField(br, PID, &random_access_indicator);
2391 }
2392 if (err == OK) {
2393 if (adaptation_field_control == 1 || adaptation_field_control == 3) {
2394 err = parsePID(br, PID, continuity_counter,
2395 payload_unit_start_indicator,
2396 transport_scrambling_control,
2397 random_access_indicator,
2398 event);
2399 }
2400 }
2401
2402 ++mNumTSPacketsParsed;
2403
2404 return err;
2405 }
2406
getSource(SourceType type)2407 sp<AnotherPacketSource> ATSParser::getSource(SourceType type) {
2408 sp<AnotherPacketSource> firstSourceFound;
2409 for (size_t i = 0; i < mPrograms.size(); ++i) {
2410 const sp<Program> &program = mPrograms.editItemAt(i);
2411 sp<AnotherPacketSource> source = program->getSource(type);
2412 if (source == NULL) {
2413 continue;
2414 }
2415 if (firstSourceFound == NULL) {
2416 firstSourceFound = source;
2417 }
2418 // Prefer programs with both audio/video
2419 switch (type) {
2420 case VIDEO: {
2421 if (program->hasSource(AUDIO)) {
2422 return source;
2423 }
2424 break;
2425 }
2426
2427 case AUDIO: {
2428 if (program->hasSource(VIDEO)) {
2429 return source;
2430 }
2431 break;
2432 }
2433
2434 default:
2435 return source;
2436 }
2437 }
2438
2439 return firstSourceFound;
2440 }
2441
hasSource(SourceType type) const2442 bool ATSParser::hasSource(SourceType type) const {
2443 for (size_t i = 0; i < mPrograms.size(); ++i) {
2444 const sp<Program> &program = mPrograms.itemAt(i);
2445 if (program->hasSource(type)) {
2446 return true;
2447 }
2448 }
2449
2450 return false;
2451 }
2452
PTSTimeDeltaEstablished()2453 bool ATSParser::PTSTimeDeltaEstablished() {
2454 if (mPrograms.isEmpty()) {
2455 return false;
2456 }
2457
2458 return mPrograms.editItemAt(0)->PTSTimeDeltaEstablished();
2459 }
2460
getFirstPTSTimeUs()2461 int64_t ATSParser::getFirstPTSTimeUs() {
2462 for (size_t i = 0; i < mPrograms.size(); ++i) {
2463 sp<ATSParser::Program> program = mPrograms.itemAt(i);
2464 if (program->PTSTimeDeltaEstablished()) {
2465 return (program->firstPTS() * 100) / 9;
2466 }
2467 }
2468 return -1;
2469 }
2470
2471 __attribute__((no_sanitize("integer")))
updatePCR(unsigned,uint64_t PCR,uint64_t byteOffsetFromStart)2472 void ATSParser::updatePCR(
2473 unsigned /* PID */, uint64_t PCR, uint64_t byteOffsetFromStart) {
2474 ALOGV("PCR 0x%016" PRIx64 " @ %" PRIx64, PCR, byteOffsetFromStart);
2475
2476 if (mNumPCRs == 2) {
2477 mPCR[0] = mPCR[1];
2478 mPCRBytes[0] = mPCRBytes[1];
2479 mSystemTimeUs[0] = mSystemTimeUs[1];
2480 mNumPCRs = 1;
2481 }
2482
2483 mPCR[mNumPCRs] = PCR;
2484 mPCRBytes[mNumPCRs] = byteOffsetFromStart;
2485 mSystemTimeUs[mNumPCRs] = ALooper::GetNowUs();
2486
2487 ++mNumPCRs;
2488
2489 if (mNumPCRs == 2) {
2490 /* Unsigned overflow here */
2491 double transportRate =
2492 (mPCRBytes[1] - mPCRBytes[0]) * 27E6 / (mPCR[1] - mPCR[0]);
2493
2494 ALOGV("transportRate = %.2f bytes/sec", transportRate);
2495 }
2496 }
2497
2498 ////////////////////////////////////////////////////////////////////////////////
2499
2500
2501 // CRC32 used for PSI section. The table was generated by following command:
2502 // $ python pycrc.py --model crc-32-mpeg --algorithm table-driven --generate c
2503 // Visit http://www.tty1.net/pycrc/index_en.html for more details.
2504 uint32_t ATSParser::PSISection::CRC_TABLE[] = {
2505 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9,
2506 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005,
2507 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
2508 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd,
2509 0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9,
2510 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
2511 0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011,
2512 0x791d4014, 0x7ddc5da3, 0x709f7b7a, 0x745e66cd,
2513 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039,
2514 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5,
2515 0xbe2b5b58, 0xbaea46ef, 0xb7a96036, 0xb3687d81,
2516 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
2517 0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49,
2518 0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95,
2519 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1,
2520 0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d,
2521 0x34867077, 0x30476dc0, 0x3d044b19, 0x39c556ae,
2522 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
2523 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16,
2524 0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca,
2525 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde,
2526 0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02,
2527 0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1, 0x53dc6066,
2528 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
2529 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e,
2530 0xbfa1b04b, 0xbb60adfc, 0xb6238b25, 0xb2e29692,
2531 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6,
2532 0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a,
2533 0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e,
2534 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
2535 0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686,
2536 0xd5b88683, 0xd1799b34, 0xdc3abded, 0xd8fba05a,
2537 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637,
2538 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb,
2539 0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f,
2540 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
2541 0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47,
2542 0x36194d42, 0x32d850f5, 0x3f9b762c, 0x3b5a6b9b,
2543 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff,
2544 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623,
2545 0xf12f560e, 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7,
2546 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
2547 0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f,
2548 0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3,
2549 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7,
2550 0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b,
2551 0x9b3660c6, 0x9ff77d71, 0x92b45ba8, 0x9675461f,
2552 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
2553 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640,
2554 0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c,
2555 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8,
2556 0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24,
2557 0x119b4be9, 0x155a565e, 0x18197087, 0x1cd86d30,
2558 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
2559 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088,
2560 0x2497d08d, 0x2056cd3a, 0x2d15ebe3, 0x29d4f654,
2561 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0,
2562 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c,
2563 0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18,
2564 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
2565 0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0,
2566 0x9abc8bd5, 0x9e7d9662, 0x933eb0bb, 0x97ffad0c,
2567 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
2568 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
2569 };
2570
PSISection()2571 ATSParser::PSISection::PSISection() :
2572 mSkipBytes(0) {
2573 }
2574
~PSISection()2575 ATSParser::PSISection::~PSISection() {
2576 }
2577
append(const void * data,size_t size)2578 status_t ATSParser::PSISection::append(const void *data, size_t size) {
2579 if (mBuffer == NULL || mBuffer->size() + size > mBuffer->capacity()) {
2580 size_t newCapacity =
2581 (mBuffer == NULL) ? size : mBuffer->capacity() + size;
2582
2583 newCapacity = (newCapacity + 1023) & ~1023;
2584
2585 sp<ABuffer> newBuffer = new ABuffer(newCapacity);
2586
2587 if (mBuffer != NULL) {
2588 memcpy(newBuffer->data(), mBuffer->data(), mBuffer->size());
2589 newBuffer->setRange(0, mBuffer->size());
2590 } else {
2591 newBuffer->setRange(0, 0);
2592 }
2593
2594 mBuffer = newBuffer;
2595 }
2596
2597 memcpy(mBuffer->data() + mBuffer->size(), data, size);
2598 mBuffer->setRange(0, mBuffer->size() + size);
2599
2600 return OK;
2601 }
2602
setSkipBytes(uint8_t skip)2603 void ATSParser::PSISection::setSkipBytes(uint8_t skip) {
2604 mSkipBytes = skip;
2605 }
2606
clear()2607 void ATSParser::PSISection::clear() {
2608 if (mBuffer != NULL) {
2609 mBuffer->setRange(0, 0);
2610 }
2611 mSkipBytes = 0;
2612 }
2613
isComplete() const2614 bool ATSParser::PSISection::isComplete() const {
2615 if (mBuffer == NULL || mBuffer->size() < 3) {
2616 return false;
2617 }
2618
2619 unsigned sectionLength = U16_AT(mBuffer->data() + 1) & 0xfff;
2620 return mBuffer->size() >= sectionLength + 3;
2621 }
2622
isEmpty() const2623 bool ATSParser::PSISection::isEmpty() const {
2624 return mBuffer == NULL || mBuffer->size() == 0;
2625 }
2626
data() const2627 const uint8_t *ATSParser::PSISection::data() const {
2628 return mBuffer == NULL ? NULL : mBuffer->data();
2629 }
2630
size() const2631 size_t ATSParser::PSISection::size() const {
2632 return mBuffer == NULL ? 0 : mBuffer->size();
2633 }
2634
isCRCOkay() const2635 bool ATSParser::PSISection::isCRCOkay() const {
2636 if (!isComplete()) {
2637 return false;
2638 }
2639 uint8_t* data = mBuffer->data();
2640
2641 // Return true if section_syntax_indicator says no section follows the field section_length.
2642 if ((data[1] & 0x80) == 0) {
2643 return true;
2644 }
2645
2646 unsigned sectionLength = U16_AT(data + 1) & 0xfff;
2647 ALOGV("sectionLength %u, skip %u", sectionLength, mSkipBytes);
2648
2649
2650 if(sectionLength < mSkipBytes) {
2651 ALOGE("b/28333006");
2652 android_errorWriteLog(0x534e4554, "28333006");
2653 return false;
2654 }
2655
2656 // Skip the preceding field present when payload start indicator is on.
2657 sectionLength -= mSkipBytes;
2658
2659 uint32_t crc = 0xffffffff;
2660 for(unsigned i = 0; i < sectionLength + 4 /* crc */; i++) {
2661 uint8_t b = data[i];
2662 int index = ((crc >> 24) ^ (b & 0xff)) & 0xff;
2663 crc = CRC_TABLE[index] ^ (crc << 8);
2664 }
2665 ALOGV("crc: %08x\n", crc);
2666 return (crc == 0);
2667 }
2668
2669 // SAMPLE_AES key handling
2670 // TODO: Merge these to their respective class after Widevine-HLS
signalNewSampleAesKey(const sp<AMessage> & keyItem)2671 void ATSParser::signalNewSampleAesKey(const sp<AMessage> &keyItem) {
2672 ALOGD("signalNewSampleAesKey: %p", keyItem.get());
2673
2674 mSampleAesKeyItem = keyItem;
2675
2676 // a NULL key item will propagate to existing ElementaryStreamQueues
2677 for (size_t i = 0; i < mPrograms.size(); ++i) {
2678 mPrograms[i]->signalNewSampleAesKey(keyItem);
2679 }
2680 }
2681
signalNewSampleAesKey(const sp<AMessage> & keyItem)2682 void ATSParser::Program::signalNewSampleAesKey(const sp<AMessage> &keyItem) {
2683 ALOGD("Program::signalNewSampleAesKey: %p", keyItem.get());
2684
2685 mSampleAesKeyItem = keyItem;
2686
2687 // a NULL key item will propagate to existing ElementaryStreamQueues
2688 for (size_t i = 0; i < mStreams.size(); ++i) {
2689 mStreams[i]->signalNewSampleAesKey(keyItem);
2690 }
2691 }
2692
signalNewSampleAesKey(const sp<AMessage> & keyItem)2693 void ATSParser::Stream::signalNewSampleAesKey(const sp<AMessage> &keyItem) {
2694 ALOGD("Stream::signalNewSampleAesKey: 0x%04x size = %zu keyItem: %p",
2695 mElementaryPID, mBuffer->size(), keyItem.get());
2696
2697 // a NULL key item will propagate to existing ElementaryStreamQueues
2698 mSampleAesKeyItem = keyItem;
2699
2700 flush(NULL);
2701 mQueue->signalNewSampleAesKey(keyItem);
2702 }
2703
2704 } // namespace android
2705