• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 namespace android {
29 
30 class DecryptHandle;
31 class DrmManagerClient;
32 struct AnotherPacketSource;
33 struct ARTSPController;
34 class DataSource;
35 class IDataSource;
36 struct IMediaHTTPService;
37 struct MediaSource;
38 class MediaBuffer;
39 struct NuCachedSource2;
40 class WVMExtractor;
41 
42 struct 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 
80 protected:
81     virtual ~GenericSource();
82 
83     virtual void onMessageReceived(const sp<AMessage> &msg);
84 
85     virtual sp<MetaData> getFormatMeta(bool audio);
86 
87 private:
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     mutable Mutex mDisconnectLock;
157 
158     sp<ALooper> mLooper;
159 
160     void resetDataSource();
161 
162     status_t initFromDataSource();
163     void checkDrmStatus(const sp<DataSource>& dataSource);
164     int64_t getLastReadPosition();
165     void setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position);
166 
167     void notifyPreparedAndCleanup(status_t err);
168     void onSecureDecodersInstantiated(status_t err);
169     void finishPrepareAsync();
170     status_t startSources();
171 
172     void onGetFormatMeta(sp<AMessage> msg) const;
173     sp<MetaData> doGetFormatMeta(bool audio) const;
174 
175     void onGetSelectedTrack(sp<AMessage> msg) const;
176     ssize_t doGetSelectedTrack(media_track_type type) const;
177 
178     void onSelectTrack(sp<AMessage> msg);
179     status_t doSelectTrack(size_t trackIndex, bool select, int64_t timeUs);
180 
181     void onSeek(sp<AMessage> msg);
182     status_t doSeek(int64_t seekTimeUs);
183 
184     void onPrepareAsync();
185 
186     void fetchTextData(
187             uint32_t what, media_track_type type,
188             int32_t curGen, sp<AnotherPacketSource> packets, sp<AMessage> msg);
189 
190     void sendTextData(
191             uint32_t what, media_track_type type,
192             int32_t curGen, sp<AnotherPacketSource> packets, sp<AMessage> msg);
193 
194     sp<ABuffer> mediaBufferToABuffer(
195             MediaBuffer *mbuf,
196             media_track_type trackType,
197             int64_t seekTimeUs,
198             int64_t *actualTimeUs = NULL);
199 
200     void postReadBuffer(media_track_type trackType);
201     void onReadBuffer(sp<AMessage> msg);
202     void readBuffer(
203             media_track_type trackType,
204             int64_t seekTimeUs = -1ll, int64_t *actualTimeUs = NULL, bool formatChange = false);
205 
206     void queueDiscontinuityIfNeeded(
207             bool seeking, bool formatChange, media_track_type trackType, Track *track);
208 
209     void schedulePollBuffering();
210     void cancelPollBuffering();
211     void restartPollBuffering();
212     void onPollBuffering();
213     void notifyBufferingUpdate(int32_t percentage);
214     void startBufferingIfNecessary();
215     void stopBufferingIfNecessary();
216     void sendCacheStats();
217     void ensureCacheIsFetching();
218 
219     DISALLOW_EVIL_CONSTRUCTORS(GenericSource);
220 };
221 
222 }  // namespace android
223 
224 #endif  // GENERIC_SOURCE_H_
225