• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <android/IMediaExtractor.h>
25 #include <media/stagefright/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     typedef IMediaExtractor::EntryPoint EntryPoint;
51 
52     // identical to IMediaExtractor::GetTrackMetaDataFlags
53     enum GetTrackFormatFlags {
54         kIncludeExtensiveMetaData = 1, // reads sample table and possibly stream headers
55     };
56 
57     explicit NuMediaExtractor(EntryPoint entryPoint);
58 
59     status_t setDataSource(
60             const sp<MediaHTTPService> &httpService,
61             const char *path,
62             const KeyedVector<String8, String8> *headers = NULL);
63 
64     status_t setDataSource(int fd, off64_t offset, off64_t size);
65 
66     status_t setDataSource(const sp<DataSource> &datasource);
67 
68     status_t setMediaCas(const HInterfaceToken &casToken);
69 
70     size_t countTracks() const;
71     status_t getTrackFormat(size_t index, sp<AMessage> *format, uint32_t flags = 0) const;
72 
73     status_t getFileFormat(sp<AMessage> *format) const;
74 
75     status_t getExifOffsetSize(off64_t *offset, size_t *size) const;
76 
77     status_t selectTrack(size_t index, int64_t startTimeUs = -1ll,
78             MediaSource::ReadOptions::SeekMode mode =
79                 MediaSource::ReadOptions::SEEK_CLOSEST_SYNC);
80     status_t unselectTrack(size_t index);
81 
82     status_t seekTo(
83             int64_t timeUs,
84             MediaSource::ReadOptions::SeekMode mode =
85                 MediaSource::ReadOptions::SEEK_CLOSEST_SYNC);
86 
87     // Each selected track has a read pointer.
88     // advance() advances the read pointer with the lowest timestamp.
89     status_t advance();
90     // readSampleData() reads the sample with the lowest timestamp.
91     status_t readSampleData(const sp<ABuffer> &buffer);
92 
93     status_t getSampleSize(size_t *sampleSize);
94     status_t getSampleTrackIndex(size_t *trackIndex);
95     status_t getSampleTime(int64_t *sampleTimeUs);
96     status_t getSampleMeta(sp<MetaData> *sampleMeta);
97     status_t getMetrics(Parcel *reply);
98 
99     bool getCachedDuration(int64_t *durationUs, bool *eos) const;
100 
101     status_t getAudioPresentations(size_t trackIdx, AudioPresentationCollection *presentations);
102 
103     status_t setLogSessionId(const String8& logSessionId);
104 
105     const char* getName() const;
106 
107 protected:
108     virtual ~NuMediaExtractor();
109 
110 private:
111     enum TrackFlags {
112         kIsVorbis       = 1,
113     };
114 
115     enum {
116         kMaxTrackCount = 16384,
117     };
118 
119     struct Sample {
120         Sample();
121         Sample(MediaBufferBase *buffer, int64_t timeUs);
122         MediaBufferBase *mBuffer;
123         int64_t mSampleTimeUs;
124     };
125 
126     struct TrackInfo {
127         sp<IMediaSource> mSource;
128         size_t mTrackIndex;
129         media_track_type mTrackType;
130         size_t mMaxFetchCount;
131         status_t mFinalResult;
132         std::list<Sample> mSamples;
133 
134         uint32_t mTrackFlags;  // bitmask of "TrackFlags"
135     };
136 
137     const EntryPoint mEntryPoint;
138 
139     mutable Mutex mLock;
140 
141     sp<DataSource> mDataSource;
142 
143     sp<IMediaExtractor> mImpl;
144     HInterfaceToken mCasToken;
145 
146     Vector<TrackInfo> mSelectedTracks;
147     int64_t mTotalBitrate;  // in bits/sec
148     int64_t mDurationUs;
149     String8 mName;
150 
151     void setEntryPointToRemoteMediaExtractor();
152 
153     ssize_t fetchAllTrackSamples(
154             int64_t seekTimeUs = -1ll,
155             MediaSource::ReadOptions::SeekMode mode =
156                 MediaSource::ReadOptions::SEEK_CLOSEST_SYNC);
157     void fetchTrackSamples(
158             TrackInfo *info,
159             int64_t seekTimeUs = -1ll,
160             MediaSource::ReadOptions::SeekMode mode =
161                 MediaSource::ReadOptions::SEEK_CLOSEST_SYNC);
162 
163     void releaseTrackSamples(TrackInfo *info);
164     void releaseAllTrackSamples();
165 
166     bool getTotalBitrate(int64_t *bitRate) const;
167     status_t updateDurationAndBitrate();
168     status_t appendVorbisNumPageSamples(MediaBufferBase *mbuf, const sp<ABuffer> &buffer);
169     status_t initMediaExtractor(const sp<DataSource>& dataSource);
170 
171     DISALLOW_EVIL_CONSTRUCTORS(NuMediaExtractor);
172 };
173 
174 }  // namespace android
175 
176 #endif  // NU_MEDIA_EXTRACTOR_H_
177 
178