GenericSource.h revision 55e2f4ca5bb23ce32fbdd627b0f648c27106b98e
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<MediaSource> mSource;
111        sp<AnotherPacketSource> mPackets;
112    };
113
114    Vector<sp<MediaSource> > 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
159    sp<ALooper> mLooper;
160
161    void resetDataSource();
162
163    status_t initFromDataSource();
164    void checkDrmStatus(const sp<DataSource>& dataSource);
165    int64_t getLastReadPosition();
166    void setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position);
167
168    void notifyPreparedAndCleanup(status_t err);
169    void onSecureDecodersInstantiated(status_t err);
170    void finishPrepareAsync();
171    status_t startSources();
172
173    void onGetFormatMeta(sp<AMessage> msg) const;
174    sp<MetaData> doGetFormatMeta(bool audio) const;
175
176    void onGetSelectedTrack(sp<AMessage> msg) const;
177    ssize_t doGetSelectedTrack(media_track_type type) const;
178
179    void onSelectTrack(sp<AMessage> msg);
180    status_t doSelectTrack(size_t trackIndex, bool select, int64_t timeUs);
181
182    void onSeek(sp<AMessage> msg);
183    status_t doSeek(int64_t seekTimeUs);
184
185    void onPrepareAsync();
186
187    void fetchTextData(
188            uint32_t what, media_track_type type,
189            int32_t curGen, sp<AnotherPacketSource> packets, sp<AMessage> msg);
190
191    void sendGlobalTextData(
192            uint32_t what,
193            int32_t curGen, sp<AMessage> msg);
194
195    void sendTextData(
196            uint32_t what, media_track_type type,
197            int32_t curGen, sp<AnotherPacketSource> packets, sp<AMessage> msg);
198
199    sp<ABuffer> mediaBufferToABuffer(
200            MediaBuffer *mbuf,
201            media_track_type trackType,
202            int64_t seekTimeUs,
203            int64_t *actualTimeUs = NULL);
204
205    void postReadBuffer(media_track_type trackType);
206    void onReadBuffer(sp<AMessage> msg);
207    void readBuffer(
208            media_track_type trackType,
209            int64_t seekTimeUs = -1ll, int64_t *actualTimeUs = NULL, bool formatChange = false);
210
211    void queueDiscontinuityIfNeeded(
212            bool seeking, bool formatChange, media_track_type trackType, Track *track);
213
214    void schedulePollBuffering();
215    void cancelPollBuffering();
216    void restartPollBuffering();
217    void onPollBuffering();
218    void notifyBufferingUpdate(int32_t percentage);
219    void startBufferingIfNecessary();
220    void stopBufferingIfNecessary();
221    void sendCacheStats();
222    void ensureCacheIsFetching();
223
224    DISALLOW_EVIL_CONSTRUCTORS(GenericSource);
225};
226
227}  // namespace android
228
229#endif  // GENERIC_SOURCE_H_
230