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