1 /* 2 3 * Copyright (C) 2010 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #ifndef MPEG2_TS_EXTRACTOR_H_ 19 20 #define MPEG2_TS_EXTRACTOR_H_ 21 22 #include <media/stagefright/foundation/ABase.h> 23 #include <media/MediaExtractorPluginApi.h> 24 #include <media/MediaExtractorPluginHelper.h> 25 #include <media/stagefright/MetaDataBase.h> 26 #include <mpeg2ts/ATSParser.h> 27 #include <utils/threads.h> 28 #include <utils/KeyedVector.h> 29 #include <utils/Vector.h> 30 31 namespace android { 32 33 struct AMessage; 34 struct AnotherPacketSource; 35 struct ATSParser; 36 struct CDataSource; 37 struct MPEG2TSSource; 38 class String8; 39 40 struct MPEG2TSExtractor : public MediaExtractorPluginHelper { 41 explicit MPEG2TSExtractor(DataSourceHelper *source); 42 43 virtual size_t countTracks(); 44 virtual MediaTrackHelper *getTrack(size_t index); 45 virtual media_status_t getTrackMetaData(AMediaFormat *meta, size_t index, uint32_t flags); 46 47 virtual media_status_t getMetaData(AMediaFormat *meta); 48 49 virtual media_status_t setMediaCas(const uint8_t* /*casToken*/, size_t /*size*/) override; 50 51 virtual uint32_t flags() const; nameMPEG2TSExtractor52 virtual const char * name() { return "MPEG2TSExtractor"; } 53 54 protected: 55 virtual ~MPEG2TSExtractor(); 56 57 private: 58 friend struct MPEG2TSSource; 59 60 mutable Mutex mLock; 61 62 DataSourceHelper *mDataSource; 63 64 sp<ATSParser> mParser; 65 66 // Used to remember SyncEvent occurred in feedMore() when called from init(), 67 // because init() needs to update |mSourceImpls| before adding SyncPoint. 68 ATSParser::SyncEvent mLastSyncEvent; 69 70 Vector<sp<AnotherPacketSource> > mSourceImpls; 71 72 Vector<KeyedVector<int64_t, off64_t> > mSyncPoints; 73 // Sync points used for seeking --- normally one for video track is used. 74 // If no video track is present, audio track will be used instead. 75 KeyedVector<int64_t, off64_t> *mSeekSyncPoints; 76 77 off64_t mOffset; 78 79 static bool isScrambledFormat(MetaDataBase &format); 80 81 void init(); 82 void addSource(const sp<AnotherPacketSource> &impl); 83 // Try to feed more data from source to parser. 84 // |isInit| means this function is called inside init(). This is a signal to 85 // save SyncEvent so that init() can add SyncPoint after it updates |mSourceImpls|. 86 // This function returns OK if expected amount of data is fed from DataSourceHelper to 87 // parser and is successfully parsed. Otherwise, various error codes could be 88 // returned, e.g., ERROR_END_OF_STREAM, or no data availalbe from DataSourceHelper, or 89 // the data has syntax error during parsing, etc. 90 status_t feedMore(bool isInit = false); 91 status_t seek(int64_t seekTimeUs, 92 const MediaTrackHelper::ReadOptions::SeekMode& seekMode); 93 status_t queueDiscontinuityForSeek(int64_t actualSeekTimeUs); 94 status_t seekBeyond(int64_t seekTimeUs); 95 96 status_t feedUntilBufferAvailable(const sp<AnotherPacketSource> &impl); 97 status_t findIndexOfSource(const sp<AnotherPacketSource> &impl, size_t *index); 98 99 // Add a SynPoint derived from |event|. 100 void addSyncPoint_l(const ATSParser::SyncEvent &event); 101 102 status_t estimateDurationsFromTimesUsAtEnd(); 103 104 size_t mHeaderSkip; 105 DISALLOW_EVIL_CONSTRUCTORS(MPEG2TSExtractor); 106 }; 107 108 bool SniffMPEG2TS(DataSourceHelper *source, float *confidence); 109 110 } // namespace android 111 112 #endif // MPEG2_TS_EXTRACTOR_H_ 113