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