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