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