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