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 <media/stagefright/foundation/ABase.h>
21#include <media/stagefright/MediaSource.h>
22#include <media/IMediaExtractor.h>
23#include <utils/Errors.h>
24#include <utils/KeyedVector.h>
25#include <utils/RefBase.h>
26#include <utils/String8.h>
27#include <utils/threads.h>
28#include <utils/Vector.h>
29
30namespace android {
31
32struct ABuffer;
33struct AMessage;
34class DataSource;
35struct IMediaHTTPService;
36class MediaBuffer;
37struct MediaExtractor;
38struct MediaSource;
39class MetaData;
40
41struct NuMediaExtractor : public RefBase {
42    enum SampleFlags {
43        SAMPLE_FLAG_SYNC        = 1,
44        SAMPLE_FLAG_ENCRYPTED   = 2,
45    };
46
47    // identical to IMediaExtractor::GetTrackMetaDataFlags
48    enum GetTrackFormatFlags {
49        kIncludeExtensiveMetaData = 1, // reads sample table and possibly stream headers
50    };
51
52    NuMediaExtractor();
53
54    status_t setDataSource(
55            const sp<IMediaHTTPService> &httpService,
56            const char *path,
57            const KeyedVector<String8, String8> *headers = NULL);
58
59    status_t setDataSource(int fd, off64_t offset, off64_t size);
60
61    status_t setDataSource(const sp<DataSource> &datasource);
62
63    size_t countTracks() const;
64    status_t getTrackFormat(size_t index, sp<AMessage> *format, uint32_t flags = 0) const;
65
66    status_t getFileFormat(sp<AMessage> *format) const;
67
68    status_t selectTrack(size_t index);
69    status_t unselectTrack(size_t index);
70
71    status_t seekTo(
72            int64_t timeUs,
73            MediaSource::ReadOptions::SeekMode mode =
74                MediaSource::ReadOptions::SEEK_CLOSEST_SYNC);
75
76    status_t advance();
77    status_t readSampleData(const sp<ABuffer> &buffer);
78    status_t getSampleTrackIndex(size_t *trackIndex);
79    status_t getSampleTime(int64_t *sampleTimeUs);
80    status_t getSampleMeta(sp<MetaData> *sampleMeta);
81
82    bool getCachedDuration(int64_t *durationUs, bool *eos) const;
83
84protected:
85    virtual ~NuMediaExtractor();
86
87private:
88    enum TrackFlags {
89        kIsVorbis       = 1,
90    };
91
92    enum {
93        kMaxTrackCount = 16384,
94    };
95
96    struct TrackInfo {
97        sp<IMediaSource> mSource;
98        size_t mTrackIndex;
99        status_t mFinalResult;
100        MediaBuffer *mSample;
101        int64_t mSampleTimeUs;
102
103        uint32_t mTrackFlags;  // bitmask of "TrackFlags"
104    };
105
106    mutable Mutex mLock;
107
108    sp<DataSource> mDataSource;
109
110    sp<IMediaExtractor> mImpl;
111    bool mIsWidevineExtractor;
112
113    Vector<TrackInfo> mSelectedTracks;
114    int64_t mTotalBitrate;  // in bits/sec
115    int64_t mDurationUs;
116
117    ssize_t fetchTrackSamples(
118            int64_t seekTimeUs = -1ll,
119            MediaSource::ReadOptions::SeekMode mode =
120                MediaSource::ReadOptions::SEEK_CLOSEST_SYNC);
121
122    void releaseTrackSamples();
123
124    bool getTotalBitrate(int64_t *bitRate) const;
125    status_t updateDurationAndBitrate();
126    status_t appendVorbisNumPageSamples(TrackInfo *info, const sp<ABuffer> &buffer);
127
128    DISALLOW_EVIL_CONSTRUCTORS(NuMediaExtractor);
129};
130
131}  // namespace android
132
133#endif  // NU_MEDIA_EXTRACTOR_H_
134
135