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