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