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