GenericSource.h revision b2487f03f12dcafdb801fc0007c8df8412397f44
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;
34class DataSource;
35class IDataSource;
36struct IMediaHTTPService;
37struct MediaSource;
38class MediaBuffer;
39struct NuCachedSource2;
40class WVMExtractor;
41
42struct NuPlayer::GenericSource : public NuPlayer::Source {
43    GenericSource(const sp<AMessage> &notify, bool uidValid, uid_t uid);
44
45    status_t setDataSource(
46            const sp<IMediaHTTPService> &httpService,
47            const char *url,
48            const KeyedVector<String8, String8> *headers);
49
50    status_t setDataSource(int fd, int64_t offset, int64_t length);
51
52    status_t setDataSource(const sp<DataSource>& dataSource);
53
54    virtual void prepareAsync();
55
56    virtual void start();
57    virtual void stop();
58    virtual void pause();
59    virtual void resume();
60
61    virtual void disconnect();
62
63    virtual status_t feedMoreTSData();
64
65    virtual sp<MetaData> getFileFormatMeta() const;
66
67    virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit);
68
69    virtual status_t getDuration(int64_t *durationUs);
70    virtual size_t getTrackCount() const;
71    virtual sp<AMessage> getTrackInfo(size_t trackIndex) const;
72    virtual ssize_t getSelectedTrack(media_track_type type) const;
73    virtual status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs);
74    virtual status_t seekTo(int64_t seekTimeUs);
75
76    virtual status_t setBuffers(bool audio, Vector<MediaBuffer *> &buffers);
77
78    virtual bool isStreaming() const;
79
80protected:
81    virtual ~GenericSource();
82
83    virtual void onMessageReceived(const sp<AMessage> &msg);
84
85    virtual sp<MetaData> getFormatMeta(bool audio);
86
87private:
88    enum {
89        kWhatPrepareAsync,
90        kWhatFetchSubtitleData,
91        kWhatFetchTimedTextData,
92        kWhatSendSubtitleData,
93        kWhatSendGlobalTimedTextData,
94        kWhatSendTimedTextData,
95        kWhatChangeAVSource,
96        kWhatPollBuffering,
97        kWhatGetFormat,
98        kWhatGetSelectedTrack,
99        kWhatSelectTrack,
100        kWhatSeek,
101        kWhatReadBuffer,
102        kWhatStopWidevine,
103        kWhatStart,
104        kWhatResume,
105        kWhatSecureDecodersInstantiated,
106    };
107
108    struct Track {
109        size_t mIndex;
110        sp<IMediaSource> mSource;
111        sp<AnotherPacketSource> mPackets;
112    };
113
114    Vector<sp<IMediaSource> > mSources;
115    Track mAudioTrack;
116    int64_t mAudioTimeUs;
117    int64_t mAudioLastDequeueTimeUs;
118    Track mVideoTrack;
119    int64_t mVideoTimeUs;
120    int64_t mVideoLastDequeueTimeUs;
121    Track mSubtitleTrack;
122    Track mTimedTextTrack;
123
124    int32_t mFetchSubtitleDataGeneration;
125    int32_t mFetchTimedTextDataGeneration;
126    int64_t mDurationUs;
127    bool mAudioIsVorbis;
128    bool mIsWidevine;
129    bool mIsSecure;
130    bool mIsStreaming;
131    bool mUIDValid;
132    uid_t mUID;
133    sp<IMediaHTTPService> mHTTPService;
134    AString mUri;
135    KeyedVector<String8, String8> mUriHeaders;
136    int mFd;
137    int64_t mOffset;
138    int64_t mLength;
139
140    sp<DataSource> mDataSource;
141    sp<NuCachedSource2> mCachedSource;
142    sp<DataSource> mHttpSource;
143    sp<WVMExtractor> mWVMExtractor;
144    sp<MetaData> mFileMeta;
145    DrmManagerClient *mDrmManagerClient;
146    sp<DecryptHandle> mDecryptHandle;
147    bool mStarted;
148    bool mStopRead;
149    int64_t mBitrate;
150    int32_t mPollBufferingGeneration;
151    uint32_t mPendingReadBufferTypes;
152    bool mBuffering;
153    bool mPrepareBuffering;
154    int32_t mPrevBufferPercentage;
155    sp<ABuffer> mGlobalTimedText;
156
157    mutable Mutex mReadBufferLock;
158    mutable Mutex mDisconnectLock;
159
160    sp<ALooper> mLooper;
161
162    void resetDataSource();
163
164    status_t initFromDataSource();
165    void checkDrmStatus(const sp<DataSource>& dataSource);
166    int64_t getLastReadPosition();
167    void setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position);
168
169    void notifyPreparedAndCleanup(status_t err);
170    void onSecureDecodersInstantiated(status_t err);
171    void finishPrepareAsync();
172    status_t startSources();
173
174    void onGetFormatMeta(sp<AMessage> msg) const;
175    sp<MetaData> doGetFormatMeta(bool audio) const;
176
177    void onGetSelectedTrack(sp<AMessage> msg) const;
178    ssize_t doGetSelectedTrack(media_track_type type) const;
179
180    void onSelectTrack(sp<AMessage> msg);
181    status_t doSelectTrack(size_t trackIndex, bool select, int64_t timeUs);
182
183    void onSeek(sp<AMessage> msg);
184    status_t doSeek(int64_t seekTimeUs);
185
186    void onPrepareAsync();
187
188    void fetchTextData(
189            uint32_t what, media_track_type type,
190            int32_t curGen, sp<AnotherPacketSource> packets, sp<AMessage> msg);
191
192    void sendGlobalTextData(
193            uint32_t what,
194            int32_t curGen, sp<AMessage> msg);
195
196    void sendTextData(
197            uint32_t what, media_track_type type,
198            int32_t curGen, sp<AnotherPacketSource> packets, sp<AMessage> msg);
199
200    sp<ABuffer> mediaBufferToABuffer(
201            MediaBuffer *mbuf,
202            media_track_type trackType,
203            int64_t seekTimeUs,
204            int64_t *actualTimeUs = NULL);
205
206    void postReadBuffer(media_track_type trackType);
207    void onReadBuffer(sp<AMessage> msg);
208    void readBuffer(
209            media_track_type trackType,
210            int64_t seekTimeUs = -1ll, int64_t *actualTimeUs = NULL, bool formatChange = false);
211
212    void queueDiscontinuityIfNeeded(
213            bool seeking, bool formatChange, media_track_type trackType, Track *track);
214
215    void schedulePollBuffering();
216    void cancelPollBuffering();
217    void restartPollBuffering();
218    void onPollBuffering();
219    void notifyBufferingUpdate(int32_t percentage);
220    void startBufferingIfNecessary();
221    void stopBufferingIfNecessary();
222    void sendCacheStats();
223    void ensureCacheIsFetching();
224
225    DISALLOW_EVIL_CONSTRUCTORS(GenericSource);
226};
227
228}  // namespace android
229
230#endif  // GENERIC_SOURCE_H_
231