1 /*
2  * Copyright 2012, 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 NU_MEDIA_EXTRACTOR_H_
18 #define NU_MEDIA_EXTRACTOR_H_
19 
20 #include <list>
21 #include <media/mediaplayer.h>
22 #include <media/stagefright/foundation/ABase.h>
23 #include <media/IMediaExtractor.h>
24 #include <media/MediaSource.h>
25 #include <utils/Errors.h>
26 #include <utils/KeyedVector.h>
27 #include <utils/RefBase.h>
28 #include <utils/String8.h>
29 #include <utils/threads.h>
30 #include <utils/Vector.h>
31 
32 namespace android {
33 
34 struct ABuffer;
35 struct AMessage;
36 class DataSource;
37 struct MediaHTTPService;
38 class MediaBuffer;
39 class MediaExtractor;
40 struct MediaSource;
41 class MetaData;
42 
43 struct NuMediaExtractor : public RefBase {
44     enum SampleFlags {
45         SAMPLE_FLAG_SYNC        = 1,
46         SAMPLE_FLAG_ENCRYPTED   = 2,
47     };
48 
49     // identical to IMediaExtractor::GetTrackMetaDataFlags
50     enum GetTrackFormatFlags {
51         kIncludeExtensiveMetaData = 1, // reads sample table and possibly stream headers
52     };
53 
54     NuMediaExtractor();
55 
56     status_t setDataSource(
57             const sp<MediaHTTPService> &httpService,
58             const char *path,
59             const KeyedVector<String8, String8> *headers = NULL);
60 
61     status_t setDataSource(int fd, off64_t offset, off64_t size);
62 
63     status_t setDataSource(const sp<DataSource> &datasource);
64 
65     status_t setMediaCas(const HInterfaceToken &casToken);
66 
67     void disconnect();
68 
69     size_t countTracks() const;
70     status_t getTrackFormat(size_t index, sp<AMessage> *format, uint32_t flags = 0) const;
71 
72     status_t getFileFormat(sp<AMessage> *format) const;
73 
74     status_t getExifOffsetSize(off64_t *offset, size_t *size) const;
75 
76     status_t selectTrack(size_t index, int64_t startTimeUs = -1ll,
77             MediaSource::ReadOptions::SeekMode mode =
78                 MediaSource::ReadOptions::SEEK_CLOSEST_SYNC);
79     status_t unselectTrack(size_t index);
80 
81     status_t seekTo(
82             int64_t timeUs,
83             MediaSource::ReadOptions::SeekMode mode =
84                 MediaSource::ReadOptions::SEEK_CLOSEST_SYNC);
85 
86     // Each selected track has a read pointer.
87     // advance() advances the read pointer with the lowest timestamp.
88     status_t advance();
89     // readSampleData() reads the sample with the lowest timestamp.
90     status_t readSampleData(const sp<ABuffer> &buffer);
91 
92     status_t getSampleSize(size_t *sampleSize);
93     status_t getSampleTrackIndex(size_t *trackIndex);
94     status_t getSampleTime(int64_t *sampleTimeUs);
95     status_t getSampleMeta(sp<MetaData> *sampleMeta);
96     status_t getMetrics(Parcel *reply);
97 
98     bool getCachedDuration(int64_t *durationUs, bool *eos) const;
99 
100 protected:
101     virtual ~NuMediaExtractor();
102 
103 private:
104     enum TrackFlags {
105         kIsVorbis       = 1,
106     };
107 
108     enum {
109         kMaxTrackCount = 16384,
110     };
111 
112     struct Sample {
113         Sample();
114         Sample(MediaBufferBase *buffer, int64_t timeUs);
115         MediaBufferBase *mBuffer;
116         int64_t mSampleTimeUs;
117     };
118 
119     struct TrackInfo {
120         sp<IMediaSource> mSource;
121         size_t mTrackIndex;
122         media_track_type mTrackType;
123         size_t mMaxFetchCount;
124         status_t mFinalResult;
125         std::list<Sample> mSamples;
126 
127         uint32_t mTrackFlags;  // bitmask of "TrackFlags"
128     };
129 
130     mutable Mutex mLock;
131 
132     sp<DataSource> mDataSource;
133 
134     sp<IMediaExtractor> mImpl;
135     HInterfaceToken mCasToken;
136 
137     Vector<TrackInfo> mSelectedTracks;
138     int64_t mTotalBitrate;  // in bits/sec
139     int64_t mDurationUs;
140 
141     ssize_t fetchAllTrackSamples(
142             int64_t seekTimeUs = -1ll,
143             MediaSource::ReadOptions::SeekMode mode =
144                 MediaSource::ReadOptions::SEEK_CLOSEST_SYNC);
145     void fetchTrackSamples(
146             TrackInfo *info,
147             int64_t seekTimeUs = -1ll,
148             MediaSource::ReadOptions::SeekMode mode =
149                 MediaSource::ReadOptions::SEEK_CLOSEST_SYNC);
150 
151     void releaseOneSample(TrackInfo *info);
152     void releaseTrackSamples(TrackInfo *info);
153     void releaseAllTrackSamples();
154 
155     bool getTotalBitrate(int64_t *bitRate) const;
156     status_t updateDurationAndBitrate();
157     status_t appendVorbisNumPageSamples(MediaBufferBase *mbuf, const sp<ABuffer> &buffer);
158 
159     DISALLOW_EVIL_CONSTRUCTORS(NuMediaExtractor);
160 };
161 
162 }  // namespace android
163 
164 #endif  // NU_MEDIA_EXTRACTOR_H_
165 
166