GenericSource.h revision f0b72b509ab1147a2a0925aced970dd68fd7fa4f
1/*
2 * Copyright (C) 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 GENERIC_SOURCE_H_
18
19#define GENERIC_SOURCE_H_
20
21#include "NuPlayer.h"
22#include "NuPlayerSource.h"
23
24#include "ATSParser.h"
25
26#include <media/mediaplayer.h>
27
28namespace android {
29
30class DecryptHandle;
31class DrmManagerClient;
32struct AnotherPacketSource;
33struct ARTSPController;
34struct DataSource;
35struct IMediaHTTPService;
36struct MediaSource;
37class MediaBuffer;
38struct NuCachedSource2;
39struct WVMExtractor;
40
41struct NuPlayer::GenericSource : public NuPlayer::Source {
42    GenericSource(const sp<AMessage> &notify, bool uidValid, uid_t uid);
43
44    status_t setDataSource(
45            const sp<IMediaHTTPService> &httpService,
46            const char *url,
47            const KeyedVector<String8, String8> *headers);
48
49    status_t setDataSource(int fd, int64_t offset, int64_t length);
50
51    virtual void prepareAsync();
52
53    virtual void start();
54    virtual void stop();
55    virtual void pause();
56    virtual void resume();
57
58    virtual status_t feedMoreTSData();
59
60    virtual sp<MetaData> getFileFormatMeta() const;
61
62    virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
63
64    virtual status_t getDuration(int64_t *durationUs);
65    virtual size_t getTrackCount() const;
66    virtual sp<AMessage> getTrackInfo(size_t trackIndex) const;
67    virtual ssize_t getSelectedTrack(media_track_type type) const;
68    virtual status_t selectTrack(size_t trackIndex, bool select);
69    virtual status_t seekTo(int64_t seekTimeUs);
70
71    virtual status_t setBuffers(bool audio, Vector<MediaBuffer *> &buffers);
72
73protected:
74    virtual ~GenericSource();
75
76    virtual void onMessageReceived(const sp<AMessage> &msg);
77
78    virtual sp<MetaData> getFormatMeta(bool audio);
79
80private:
81    enum {
82        kWhatPrepareAsync,
83        kWhatFetchSubtitleData,
84        kWhatFetchTimedTextData,
85        kWhatSendSubtitleData,
86        kWhatSendTimedTextData,
87        kWhatChangeAVSource,
88        kWhatPollBuffering,
89        kWhatGetFormat,
90        kWhatGetSelectedTrack,
91        kWhatSelectTrack,
92        kWhatSeek,
93        kWhatReadBuffer,
94    };
95
96    Vector<sp<MediaSource> > mSources;
97
98    struct Track {
99        size_t mIndex;
100        sp<MediaSource> mSource;
101        sp<AnotherPacketSource> mPackets;
102    };
103
104    Track mAudioTrack;
105    int64_t mAudioTimeUs;
106    Track mVideoTrack;
107    int64_t mVideoTimeUs;
108    Track mSubtitleTrack;
109    Track mTimedTextTrack;
110
111    int32_t mFetchSubtitleDataGeneration;
112    int32_t mFetchTimedTextDataGeneration;
113    int64_t mDurationUs;
114    bool mAudioIsVorbis;
115    bool mIsWidevine;
116    bool mUIDValid;
117    uid_t mUID;
118    sp<IMediaHTTPService> mHTTPService;
119    AString mUri;
120    KeyedVector<String8, String8> mUriHeaders;
121    int mFd;
122    int64_t mOffset;
123    int64_t mLength;
124
125    sp<DataSource> mDataSource;
126    sp<NuCachedSource2> mCachedSource;
127    sp<WVMExtractor> mWVMExtractor;
128    sp<MetaData> mFileMeta;
129    DrmManagerClient *mDrmManagerClient;
130    sp<DecryptHandle> mDecryptHandle;
131    bool mStarted;
132    String8 mContentType;
133    AString mSniffedMIME;
134    off64_t mMetaDataSize;
135    int64_t mBitrate;
136    int32_t mPollBufferingGeneration;
137    uint32_t mPendingReadBufferTypes;
138    mutable Mutex mReadBufferLock;
139
140    sp<ALooper> mLooper;
141
142    void resetDataSource();
143
144    status_t initFromDataSource();
145    void checkDrmStatus(const sp<DataSource>& dataSource);
146    int64_t getLastReadPosition();
147    void setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position);
148
149    status_t prefillCacheIfNecessary();
150
151    void notifyPreparedAndCleanup(status_t err);
152
153    void onGetFormatMeta(sp<AMessage> msg) const;
154    sp<MetaData> doGetFormatMeta(bool audio) const;
155
156    void onGetSelectedTrack(sp<AMessage> msg) const;
157    ssize_t doGetSelectedTrack(media_track_type type) const;
158
159    void onSelectTrack(sp<AMessage> msg);
160    status_t doSelectTrack(size_t trackIndex, bool select);
161
162    void onSeek(sp<AMessage> msg);
163    status_t doSeek(int64_t seekTimeUs);
164
165    void onPrepareAsync();
166
167    void fetchTextData(
168            uint32_t what, media_track_type type,
169            int32_t curGen, sp<AnotherPacketSource> packets, sp<AMessage> msg);
170
171    void sendTextData(
172            uint32_t what, media_track_type type,
173            int32_t curGen, sp<AnotherPacketSource> packets, sp<AMessage> msg);
174
175    sp<ABuffer> mediaBufferToABuffer(
176            MediaBuffer *mbuf,
177            media_track_type trackType,
178            int64_t *actualTimeUs = NULL);
179
180    void postReadBuffer(media_track_type trackType);
181    void onReadBuffer(sp<AMessage> msg);
182    void readBuffer(
183            media_track_type trackType,
184            int64_t seekTimeUs = -1ll, int64_t *actualTimeUs = NULL, bool formatChange = false);
185
186    void schedulePollBuffering();
187    void cancelPollBuffering();
188    void onPollBuffering();
189    void notifyBufferingUpdate(int percentage);
190
191    DISALLOW_EVIL_CONSTRUCTORS(GenericSource);
192};
193
194}  // namespace android
195
196#endif  // GENERIC_SOURCE_H_
197