GenericSource.h revision c5de09127e9e0d5df7aa587be317e1487d793245
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(
75        int64_t seekTimeUs,
76        MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC) override;
77
78    virtual status_t setBuffers(bool audio, Vector<MediaBuffer *> &buffers);
79
80    virtual bool isStreaming() const;
81
82    virtual void setOffloadAudio(bool offload);
83
84protected:
85    virtual ~GenericSource();
86
87    virtual void onMessageReceived(const sp<AMessage> &msg);
88
89    virtual sp<MetaData> getFormatMeta(bool audio);
90
91private:
92    enum {
93        kWhatPrepareAsync,
94        kWhatFetchSubtitleData,
95        kWhatFetchTimedTextData,
96        kWhatSendSubtitleData,
97        kWhatSendGlobalTimedTextData,
98        kWhatSendTimedTextData,
99        kWhatChangeAVSource,
100        kWhatPollBuffering,
101        kWhatGetFormat,
102        kWhatGetSelectedTrack,
103        kWhatSelectTrack,
104        kWhatSeek,
105        kWhatReadBuffer,
106        kWhatStopWidevine,
107        kWhatStart,
108        kWhatResume,
109        kWhatSecureDecodersInstantiated,
110    };
111
112    struct Track {
113        size_t mIndex;
114        sp<IMediaSource> mSource;
115        sp<AnotherPacketSource> mPackets;
116    };
117
118    // Helper to monitor buffering status. The polling happens every second.
119    // When necessary, it will send out buffering events to the player.
120    struct BufferingMonitor : public AHandler {
121    public:
122        explicit BufferingMonitor(const sp<AMessage> &notify);
123
124        // Set up state.
125        void prepare(const sp<NuCachedSource2> &cachedSource,
126                const sp<WVMExtractor> &wvmExtractor,
127                int64_t durationUs,
128                int64_t bitrate,
129                bool isStreaming);
130        // Stop and reset buffering monitor.
131        void stop();
132        // Cancel the current monitor task.
133        void cancelPollBuffering();
134        // Restart the monitor task.
135        void restartPollBuffering();
136        // Stop buffering task and send out corresponding events.
137        void stopBufferingIfNecessary();
138        // Make sure data source is getting data.
139        void ensureCacheIsFetching();
140        // Update media time of just extracted buffer from data source.
141        void updateQueuedTime(bool isAudio, int64_t timeUs);
142
143        // Set the offload mode.
144        void setOffloadAudio(bool offload);
145        // Update media time of last dequeued buffer which is sent to the decoder.
146        void updateDequeuedBufferTime(int64_t mediaUs);
147
148    protected:
149        virtual ~BufferingMonitor();
150        virtual void onMessageReceived(const sp<AMessage> &msg);
151
152    private:
153        enum {
154            kWhatPollBuffering,
155        };
156
157        sp<AMessage> mNotify;
158
159        sp<NuCachedSource2> mCachedSource;
160        sp<WVMExtractor> mWVMExtractor;
161        int64_t mDurationUs;
162        int64_t mBitrate;
163        bool mIsStreaming;
164
165        int64_t mAudioTimeUs;
166        int64_t mVideoTimeUs;
167        int32_t mPollBufferingGeneration;
168        bool mPrepareBuffering;
169        bool mBuffering;
170        int32_t mPrevBufferPercentage;
171
172        mutable Mutex mLock;
173
174        bool mOffloadAudio;
175        int64_t mFirstDequeuedBufferRealUs;
176        int64_t mFirstDequeuedBufferMediaUs;
177        int64_t mlastDequeuedBufferMediaUs;
178
179        void prepare_l(const sp<NuCachedSource2> &cachedSource,
180                const sp<WVMExtractor> &wvmExtractor,
181                int64_t durationUs,
182                int64_t bitrate,
183                bool isStreaming);
184        void cancelPollBuffering_l();
185        void notifyBufferingUpdate_l(int32_t percentage);
186        void startBufferingIfNecessary_l();
187        void stopBufferingIfNecessary_l();
188        void sendCacheStats_l();
189        void ensureCacheIsFetching_l();
190        int64_t getLastReadPosition_l();
191        void onPollBuffering_l();
192        void schedulePollBuffering_l();
193    };
194
195    Vector<sp<IMediaSource> > mSources;
196    Track mAudioTrack;
197    int64_t mAudioTimeUs;
198    int64_t mAudioLastDequeueTimeUs;
199    Track mVideoTrack;
200    int64_t mVideoTimeUs;
201    int64_t mVideoLastDequeueTimeUs;
202    Track mSubtitleTrack;
203    Track mTimedTextTrack;
204
205    int32_t mFetchSubtitleDataGeneration;
206    int32_t mFetchTimedTextDataGeneration;
207    int64_t mDurationUs;
208    bool mAudioIsVorbis;
209    bool mIsWidevine;
210    bool mIsSecure;
211    bool mIsStreaming;
212    bool mUIDValid;
213    uid_t mUID;
214    sp<IMediaHTTPService> mHTTPService;
215    AString mUri;
216    KeyedVector<String8, String8> mUriHeaders;
217    int mFd;
218    int64_t mOffset;
219    int64_t mLength;
220
221    sp<DataSource> mDataSource;
222    sp<NuCachedSource2> mCachedSource;
223    sp<DataSource> mHttpSource;
224    sp<WVMExtractor> mWVMExtractor;
225    sp<MetaData> mFileMeta;
226    DrmManagerClient *mDrmManagerClient;
227    sp<DecryptHandle> mDecryptHandle;
228    bool mStarted;
229    bool mStopRead;
230    int64_t mBitrate;
231    sp<BufferingMonitor> mBufferingMonitor;
232    uint32_t mPendingReadBufferTypes;
233    sp<ABuffer> mGlobalTimedText;
234
235    mutable Mutex mReadBufferLock;
236    mutable Mutex mDisconnectLock;
237
238    sp<ALooper> mLooper;
239    sp<ALooper> mBufferingMonitorLooper;
240
241    void resetDataSource();
242
243    status_t initFromDataSource();
244    void checkDrmStatus(const sp<DataSource>& dataSource);
245    int64_t getLastReadPosition();
246    void setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position);
247
248    void notifyPreparedAndCleanup(status_t err);
249    void onSecureDecodersInstantiated(status_t err);
250    void finishPrepareAsync();
251    status_t startSources();
252
253    void onGetFormatMeta(const sp<AMessage>& msg) const;
254    sp<MetaData> doGetFormatMeta(bool audio) const;
255
256    void onGetSelectedTrack(const sp<AMessage>& msg) const;
257    ssize_t doGetSelectedTrack(media_track_type type) const;
258
259    void onSelectTrack(const sp<AMessage>& msg);
260    status_t doSelectTrack(size_t trackIndex, bool select, int64_t timeUs);
261
262    void onSeek(const sp<AMessage>& msg);
263    status_t doSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode);
264
265    void onPrepareAsync();
266
267    void fetchTextData(
268            uint32_t what, media_track_type type,
269            int32_t curGen, const sp<AnotherPacketSource>& packets, const sp<AMessage>& msg);
270
271    void sendGlobalTextData(
272            uint32_t what,
273            int32_t curGen, sp<AMessage> msg);
274
275    void sendTextData(
276            uint32_t what, media_track_type type,
277            int32_t curGen, const sp<AnotherPacketSource>& packets, const sp<AMessage>& msg);
278
279    sp<ABuffer> mediaBufferToABuffer(
280            MediaBuffer *mbuf,
281            media_track_type trackType);
282
283    void postReadBuffer(media_track_type trackType);
284    void onReadBuffer(const sp<AMessage>& msg);
285    // When |mode| is MediaPlayerSeekMode::SEEK_CLOSEST, the buffer read shall
286    // include an item indicating skipping rendering all buffers with timestamp
287    // earlier than |seekTimeUs|.
288    // For other modes, the buffer read will not include the item as above in order
289    // to facilitate fast seek operation.
290    void readBuffer(
291            media_track_type trackType,
292            int64_t seekTimeUs = -1ll,
293            MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC,
294            int64_t *actualTimeUs = NULL, bool formatChange = false);
295
296    void queueDiscontinuityIfNeeded(
297            bool seeking, bool formatChange, media_track_type trackType, Track *track);
298
299    DISALLOW_EVIL_CONSTRUCTORS(GenericSource);
300};
301
302}  // namespace android
303
304#endif  // GENERIC_SOURCE_H_
305