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