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 MPEG2_TS_EXTRACTOR_H_
18 
19 #define MPEG2_TS_EXTRACTOR_H_
20 
21 #include <media/stagefright/foundation/ABase.h>
22 #include <media/stagefright/MediaExtractor.h>
23 #include <media/stagefright/MediaSource.h>
24 #include <utils/threads.h>
25 #include <utils/KeyedVector.h>
26 #include <utils/Vector.h>
27 
28 #include "mpeg2ts/ATSParser.h"
29 
30 namespace android {
31 
32 struct AMessage;
33 struct AnotherPacketSource;
34 struct ATSParser;
35 class DataSource;
36 struct MPEG2TSSource;
37 class String8;
38 
39 struct MPEG2TSExtractor : public MediaExtractor {
40     MPEG2TSExtractor(const sp<DataSource> &source);
41 
42     virtual size_t countTracks();
43     virtual sp<IMediaSource> getTrack(size_t index);
44     virtual sp<MetaData> getTrackMetaData(size_t index, uint32_t flags);
45 
46     virtual sp<MetaData> getMetaData();
47 
48     virtual uint32_t flags() const;
nameMPEG2TSExtractor49     virtual const char * name() { return "MPEG2TSExtractor"; }
50 
51 private:
52     friend struct MPEG2TSSource;
53 
54     mutable Mutex mLock;
55 
56     sp<DataSource> mDataSource;
57 
58     sp<ATSParser> mParser;
59 
60     // Used to remember SyncEvent occurred in feedMore() when called from init(),
61     // because init() needs to update |mSourceImpls| before adding SyncPoint.
62     ATSParser::SyncEvent mLastSyncEvent;
63 
64     Vector<sp<AnotherPacketSource> > mSourceImpls;
65 
66     Vector<KeyedVector<int64_t, off64_t> > mSyncPoints;
67     // Sync points used for seeking --- normally one for video track is used.
68     // If no video track is present, audio track will be used instead.
69     KeyedVector<int64_t, off64_t> *mSeekSyncPoints;
70 
71     off64_t mOffset;
72 
73     void init();
74     // Try to feed more data from source to parser.
75     // |isInit| means this function is called inside init(). This is a signal to
76     // save SyncEvent so that init() can add SyncPoint after it updates |mSourceImpls|.
77     // This function returns OK if expected amount of data is fed from DataSource to
78     // parser and is successfully parsed. Otherwise, various error codes could be
79     // returned, e.g., ERROR_END_OF_STREAM, or no data availalbe from DataSource, or
80     // the data has syntax error during parsing, etc.
81     status_t feedMore(bool isInit = false);
82     status_t seek(int64_t seekTimeUs,
83             const MediaSource::ReadOptions::SeekMode& seekMode);
84     status_t queueDiscontinuityForSeek(int64_t actualSeekTimeUs);
85     status_t seekBeyond(int64_t seekTimeUs);
86 
87     status_t feedUntilBufferAvailable(const sp<AnotherPacketSource> &impl);
88 
89     // Add a SynPoint derived from |event|.
90     void addSyncPoint_l(const ATSParser::SyncEvent &event);
91 
92     DISALLOW_EVIL_CONSTRUCTORS(MPEG2TSExtractor);
93 };
94 
95 bool SniffMPEG2TS(
96         const sp<DataSource> &source, String8 *mimeType, float *confidence,
97         sp<AMessage> *);
98 
99 }  // namespace android
100 
101 #endif  // MPEG2_TS_EXTRACTOR_H_
102