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