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 #ifndef A_TS_PARSER_H_ 18 19 #define A_TS_PARSER_H_ 20 21 #include <sys/types.h> 22 23 #include <media/stagefright/foundation/ABase.h> 24 #include <media/stagefright/foundation/AMessage.h> 25 #include <media/stagefright/MediaSource.h> 26 #include <utils/KeyedVector.h> 27 #include <utils/Vector.h> 28 #include <utils/RefBase.h> 29 #include <vector> 30 31 namespace android { 32 namespace media { 33 class ICas; 34 class IDescrambler; 35 }; 36 using namespace media; 37 class ABitReader; 38 struct ABuffer; 39 struct AnotherPacketSource; 40 41 struct ATSParser : public RefBase { 42 enum DiscontinuityType { 43 DISCONTINUITY_NONE = 0, 44 DISCONTINUITY_TIME = 1, 45 DISCONTINUITY_AUDIO_FORMAT = 2, 46 DISCONTINUITY_VIDEO_FORMAT = 4, 47 DISCONTINUITY_ABSOLUTE_TIME = 8, 48 DISCONTINUITY_TIME_OFFSET = 16, 49 50 // For legacy reasons this also implies a time discontinuity. 51 DISCONTINUITY_FORMATCHANGE = 52 DISCONTINUITY_AUDIO_FORMAT 53 | DISCONTINUITY_VIDEO_FORMAT 54 | DISCONTINUITY_TIME, 55 DISCONTINUITY_FORMAT_ONLY = 56 DISCONTINUITY_AUDIO_FORMAT 57 | DISCONTINUITY_VIDEO_FORMAT, 58 }; 59 60 enum Flags { 61 // The 90kHz clock (PTS/DTS) is absolute, i.e. PTS=0 corresponds to 62 // a media time of 0. 63 // If this flag is _not_ specified, the first PTS encountered in a 64 // program of this stream will be assumed to correspond to media time 0 65 // instead. 66 TS_TIMESTAMPS_ARE_ABSOLUTE = 1, 67 // Video PES packets contain exactly one (aligned) access unit. 68 ALIGNED_VIDEO_DATA = 2, 69 }; 70 71 enum SourceType { 72 VIDEO = 0, 73 AUDIO = 1, 74 META = 2, 75 NUM_SOURCE_TYPES = 3 76 }; 77 78 // Event is used to signal sync point event at feedTSPacket(). 79 struct SyncEvent { 80 explicit SyncEvent(off64_t offset); 81 82 void init(off64_t offset, const sp<MediaSource> &source, 83 int64_t timeUs, SourceType type); 84 hasReturnedDataATSParser::SyncEvent85 bool hasReturnedData() const { return mHasReturnedData; } 86 void reset(); getOffsetATSParser::SyncEvent87 off64_t getOffset() const { return mOffset; } getMediaSourceATSParser::SyncEvent88 const sp<MediaSource> &getMediaSource() const { return mMediaSource; } getTimeUsATSParser::SyncEvent89 int64_t getTimeUs() const { return mTimeUs; } getTypeATSParser::SyncEvent90 SourceType getType() const { return mType; } 91 92 private: 93 bool mHasReturnedData; 94 /* 95 * mHasReturnedData == false: the current offset (or undefined if the returned data 96 has been invalidated via reset()) 97 * mHasReturnedData == true: the start offset of sync payload 98 */ 99 off64_t mOffset; 100 /* The media source object for this event. */ 101 sp<MediaSource> mMediaSource; 102 /* The timestamp of the sync frame. */ 103 int64_t mTimeUs; 104 SourceType mType; 105 }; 106 107 explicit ATSParser(uint32_t flags = 0); 108 109 status_t setMediaCas(const sp<ICas> &cas); 110 111 // Feed a TS packet into the parser. uninitialized event with the start 112 // offset of this TS packet goes in, and if the parser detects PES with 113 // a sync frame, the event will be initiailzed with the start offset of the 114 // PES. Note that the offset of the event can be different from what we fed, 115 // as a PES may consist of multiple TS packets. 116 // 117 // Even in the case feedTSPacket() returns non-OK value, event still may be 118 // initialized if the parsing failed after the detection. 119 status_t feedTSPacket( 120 const void *data, size_t size, SyncEvent *event = NULL); 121 122 void signalDiscontinuity( 123 DiscontinuityType type, const sp<AMessage> &extra); 124 125 void signalEOS(status_t finalResult); 126 127 sp<MediaSource> getSource(SourceType type); 128 bool hasSource(SourceType type) const; 129 130 bool PTSTimeDeltaEstablished(); 131 132 int64_t getFirstPTSTimeUs(); 133 134 void signalNewSampleAesKey(const sp<AMessage> &keyItem); 135 136 enum { 137 // From ISO/IEC 13818-1: 2000 (E), Table 2-29 138 STREAMTYPE_RESERVED = 0x00, 139 STREAMTYPE_MPEG1_VIDEO = 0x01, 140 STREAMTYPE_MPEG2_VIDEO = 0x02, 141 STREAMTYPE_MPEG1_AUDIO = 0x03, 142 STREAMTYPE_MPEG2_AUDIO = 0x04, 143 STREAMTYPE_MPEG2_AUDIO_ADTS = 0x0f, 144 STREAMTYPE_MPEG4_VIDEO = 0x10, 145 STREAMTYPE_METADATA = 0x15, 146 STREAMTYPE_H264 = 0x1b, 147 148 // From ATSC A/53 Part 3:2009, 6.7.1 149 STREAMTYPE_AC3 = 0x81, 150 151 // Stream type 0x83 is non-standard, 152 // it could be LPCM or TrueHD AC3 153 STREAMTYPE_LPCM_AC3 = 0x83, 154 155 //Sample Encrypted types 156 STREAMTYPE_H264_ENCRYPTED = 0xDB, 157 STREAMTYPE_AAC_ENCRYPTED = 0xCF, 158 STREAMTYPE_AC3_ENCRYPTED = 0xC1, 159 }; 160 161 protected: 162 virtual ~ATSParser(); 163 164 private: 165 struct Program; 166 struct Stream; 167 struct PSISection; 168 struct CasManager; 169 struct CADescriptor { 170 int32_t mSystemID; 171 unsigned mPID; 172 std::vector<uint8_t> mPrivateData; 173 }; 174 175 sp<CasManager> mCasManager; 176 177 uint32_t mFlags; 178 Vector<sp<Program> > mPrograms; 179 180 // Keyed by PID 181 KeyedVector<unsigned, sp<PSISection> > mPSISections; 182 183 int64_t mAbsoluteTimeAnchorUs; 184 185 bool mTimeOffsetValid; 186 int64_t mTimeOffsetUs; 187 int64_t mLastRecoveredPTS; 188 189 size_t mNumTSPacketsParsed; 190 191 sp<AMessage> mSampleAesKeyItem; 192 193 void parseProgramAssociationTable(ABitReader *br); 194 void parseProgramMap(ABitReader *br); 195 // Parse PES packet where br is pointing to. If the PES contains a sync 196 // frame, set event with the time and the start offset of this PES. 197 // Note that the method itself does not touch event. 198 void parsePES(ABitReader *br, SyncEvent *event); 199 200 // Strip remaining packet headers and pass to appropriate program/stream 201 // to parse the payload. If the payload turns out to be PES and contains 202 // a sync frame, event shall be set with the time and start offset of the 203 // PES. 204 // Note that the method itself does not touch event. 205 status_t parsePID( 206 ABitReader *br, unsigned PID, 207 unsigned continuity_counter, 208 unsigned payload_unit_start_indicator, 209 unsigned transport_scrambling_control, 210 unsigned random_access_indicator, 211 SyncEvent *event); 212 213 status_t parseAdaptationField( 214 ABitReader *br, unsigned PID, unsigned *random_access_indicator); 215 216 // see feedTSPacket(). 217 status_t parseTS(ABitReader *br, SyncEvent *event); 218 219 void updatePCR(unsigned PID, uint64_t PCR, uint64_t byteOffsetFromStart); 220 221 uint64_t mPCR[2]; 222 uint64_t mPCRBytes[2]; 223 int64_t mSystemTimeUs[2]; 224 size_t mNumPCRs; 225 226 DISALLOW_EVIL_CONSTRUCTORS(ATSParser); 227 }; 228 229 } // namespace android 230 231 #endif // A_TS_PARSER_H_ 232