blob: 0764ec3b70aaddfef2ed950fa9af6f645a0cf054 [file] [log] [blame]
Wei Jia53692fa2017-12-11 10:33:46 -08001/*
2 * Copyright 2017 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 NU_PLAYER2_H_
18
19#define NU_PLAYER2_H_
20
21#include <media/AudioResamplerPublic.h>
22#include <media/MediaPlayer2Interface.h>
23#include <media/stagefright/foundation/AHandler.h>
24
25namespace android {
26
27struct ABuffer;
28struct AMediaCryptoWrapper;
29struct AMessage;
Wei Jia28288fb2017-12-15 13:45:29 -080030struct ANativeWindowWrapper;
Wei Jia53692fa2017-12-11 10:33:46 -080031struct AudioPlaybackRate;
32struct AVSyncSettings;
Wei Jiac2636032018-02-01 09:15:25 -080033struct DataSourceDesc;
Wei Jia53692fa2017-12-11 10:33:46 -080034class IDataSource;
35struct MediaClock;
36struct MediaHTTPService;
37class MetaData;
38struct NuPlayer2Driver;
39
40struct NuPlayer2 : public AHandler {
41 explicit NuPlayer2(pid_t pid, const sp<MediaClock> &mediaClock);
42
43 void setUID(uid_t uid);
44
45 void setDriver(const wp<NuPlayer2Driver> &driver);
46
Wei Jiac2636032018-02-01 09:15:25 -080047 void setDataSourceAsync(const sp<DataSourceDesc> &dsd);
Wei Jia53692fa2017-12-11 10:33:46 -080048
49 status_t getBufferingSettings(BufferingSettings* buffering /* nonnull */);
50 status_t setBufferingSettings(const BufferingSettings& buffering);
51
52 void prepareAsync();
53
Wei Jia28288fb2017-12-15 13:45:29 -080054 void setVideoSurfaceTextureAsync(const sp<ANativeWindowWrapper> &nww);
Wei Jia53692fa2017-12-11 10:33:46 -080055
Wei Jia33abcc72018-01-30 09:47:38 -080056 void setAudioSink(const sp<MediaPlayer2Interface::AudioSink> &sink);
Wei Jia53692fa2017-12-11 10:33:46 -080057 status_t setPlaybackSettings(const AudioPlaybackRate &rate);
58 status_t getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */);
59 status_t setSyncSettings(const AVSyncSettings &sync, float videoFpsHint);
60 status_t getSyncSettings(AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */);
61
62 void start();
63
64 void pause();
65
66 // Will notify the driver through "notifyResetComplete" once finished.
67 void resetAsync();
68
69 // Request a notification when specified media time is reached.
70 status_t notifyAt(int64_t mediaTimeUs);
71
72 // Will notify the driver through "notifySeekComplete" once finished
73 // and needNotify is true.
74 void seekToAsync(
75 int64_t seekTimeUs,
76 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC,
77 bool needNotify = false);
78
79 status_t setVideoScalingMode(int32_t mode);
80 status_t getTrackInfo(Parcel* reply) const;
81 status_t getSelectedTrack(int32_t type, Parcel* reply) const;
82 status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs);
83 status_t getCurrentPosition(int64_t *mediaUs);
84 void getStats(Vector<sp<AMessage> > *mTrackStats);
85
86 sp<MetaData> getFileMeta();
87 float getFrameRate();
88
89 // Modular DRM
90 status_t prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId);
91 status_t releaseDrm();
92
93 const char *getDataSourceType();
94
95protected:
96 virtual ~NuPlayer2();
97
98 virtual void onMessageReceived(const sp<AMessage> &msg);
99
100public:
101 struct StreamListener;
102 struct Source;
103
104private:
105 struct Decoder;
106 struct DecoderBase;
107 struct DecoderPassThrough;
108 struct CCDecoder;
109 struct GenericSource;
110 struct HTTPLiveSource;
111 struct Renderer;
112 struct RTSPSource;
113 struct StreamingSource;
114 struct Action;
115 struct SeekAction;
116 struct SetSurfaceAction;
117 struct ResumeDecoderAction;
118 struct FlushDecoderAction;
119 struct PostMessageAction;
120 struct SimpleAction;
121
122 enum {
123 kWhatSetDataSource = '=DaS',
124 kWhatPrepare = 'prep',
125 kWhatSetVideoSurface = '=VSu',
126 kWhatSetAudioSink = '=AuS',
127 kWhatMoreDataQueued = 'more',
128 kWhatConfigPlayback = 'cfPB',
129 kWhatConfigSync = 'cfSy',
130 kWhatGetPlaybackSettings = 'gPbS',
131 kWhatGetSyncSettings = 'gSyS',
132 kWhatStart = 'strt',
133 kWhatScanSources = 'scan',
134 kWhatVideoNotify = 'vidN',
135 kWhatAudioNotify = 'audN',
136 kWhatClosedCaptionNotify = 'capN',
137 kWhatRendererNotify = 'renN',
138 kWhatReset = 'rset',
139 kWhatNotifyTime = 'nfyT',
140 kWhatSeek = 'seek',
141 kWhatPause = 'paus',
142 kWhatResume = 'rsme',
143 kWhatPollDuration = 'polD',
144 kWhatSourceNotify = 'srcN',
145 kWhatGetTrackInfo = 'gTrI',
146 kWhatGetSelectedTrack = 'gSel',
147 kWhatSelectTrack = 'selT',
148 kWhatGetBufferingSettings = 'gBus',
149 kWhatSetBufferingSettings = 'sBuS',
150 kWhatPrepareDrm = 'pDrm',
151 kWhatReleaseDrm = 'rDrm',
152 };
153
154 wp<NuPlayer2Driver> mDriver;
155 bool mUIDValid;
156 uid_t mUID;
157 pid_t mPID;
158 const sp<MediaClock> mMediaClock;
159 Mutex mSourceLock; // guard |mSource|.
160 sp<Source> mSource;
161 uint32_t mSourceFlags;
Wei Jia28288fb2017-12-15 13:45:29 -0800162 sp<ANativeWindowWrapper> mNativeWindow;
Wei Jia33abcc72018-01-30 09:47:38 -0800163 sp<MediaPlayer2Interface::AudioSink> mAudioSink;
Wei Jia53692fa2017-12-11 10:33:46 -0800164 sp<DecoderBase> mVideoDecoder;
165 bool mOffloadAudio;
166 sp<DecoderBase> mAudioDecoder;
167 sp<CCDecoder> mCCDecoder;
168 sp<Renderer> mRenderer;
169 sp<ALooper> mRendererLooper;
170 int32_t mAudioDecoderGeneration;
171 int32_t mVideoDecoderGeneration;
172 int32_t mRendererGeneration;
173
174 Mutex mPlayingTimeLock;
175 int64_t mLastStartedPlayingTimeNs;
176 void stopPlaybackTimer(const char *where);
177 void startPlaybackTimer(const char *where);
178
179 int64_t mLastStartedRebufferingTimeNs;
180 void startRebufferingTimer();
181 void stopRebufferingTimer(bool exitingPlayback);
182
183 int64_t mPreviousSeekTimeUs;
184
185 List<sp<Action> > mDeferredActions;
186
187 bool mAudioEOS;
188 bool mVideoEOS;
189
190 bool mScanSourcesPending;
191 int32_t mScanSourcesGeneration;
192
193 int32_t mPollDurationGeneration;
194 int32_t mTimedTextGeneration;
195
196 enum FlushStatus {
197 NONE,
198 FLUSHING_DECODER,
199 FLUSHING_DECODER_SHUTDOWN,
200 SHUTTING_DOWN_DECODER,
201 FLUSHED,
202 SHUT_DOWN,
203 };
204
205 enum FlushCommand {
206 FLUSH_CMD_NONE,
207 FLUSH_CMD_FLUSH,
208 FLUSH_CMD_SHUTDOWN,
209 };
210
211 // Status of flush responses from the decoder and renderer.
212 bool mFlushComplete[2][2];
213
214 FlushStatus mFlushingAudio;
215 FlushStatus mFlushingVideo;
216
217 // Status of flush responses from the decoder and renderer.
218 bool mResumePending;
219
220 int32_t mVideoScalingMode;
221
222 AudioPlaybackRate mPlaybackSettings;
223 AVSyncSettings mSyncSettings;
224 float mVideoFpsHint;
225 bool mStarted;
226 bool mPrepared;
227 bool mResetting;
228 bool mSourceStarted;
229 bool mAudioDecoderError;
230 bool mVideoDecoderError;
231
232 // Actual pause state, either as requested by client or due to buffering.
233 bool mPaused;
234
235 // Pause state as requested by client. Note that if mPausedByClient is
236 // true, mPaused is always true; if mPausedByClient is false, mPaused could
237 // still become true, when we pause internally due to buffering.
238 bool mPausedByClient;
239
240 // Pause state as requested by source (internally) due to buffering
241 bool mPausedForBuffering;
242
243 // Modular DRM
244 sp<AMediaCryptoWrapper> mCrypto;
245 bool mIsDrmProtected;
246
247 typedef enum {
248 DATA_SOURCE_TYPE_NONE,
249 DATA_SOURCE_TYPE_HTTP_LIVE,
250 DATA_SOURCE_TYPE_RTSP,
251 DATA_SOURCE_TYPE_GENERIC_URL,
252 DATA_SOURCE_TYPE_GENERIC_FD,
253 DATA_SOURCE_TYPE_MEDIA,
254 DATA_SOURCE_TYPE_STREAM,
255 } DATA_SOURCE_TYPE;
256
257 std::atomic<DATA_SOURCE_TYPE> mDataSourceType;
258
259 inline const sp<DecoderBase> &getDecoder(bool audio) {
260 return audio ? mAudioDecoder : mVideoDecoder;
261 }
262
263 inline void clearFlushComplete() {
264 mFlushComplete[0][0] = false;
265 mFlushComplete[0][1] = false;
266 mFlushComplete[1][0] = false;
267 mFlushComplete[1][1] = false;
268 }
269
270 void tryOpenAudioSinkForOffload(
271 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo);
272 void closeAudioSink();
273 void restartAudio(
274 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder);
275 void determineAudioModeChange(const sp<AMessage> &audioFormat);
276
277 status_t instantiateDecoder(
278 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true);
279
280 status_t onInstantiateSecureDecoders();
281
282 void updateVideoSize(
283 const sp<AMessage> &inputFormat,
284 const sp<AMessage> &outputFormat = NULL);
285
286 void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
287
288 void handleFlushComplete(bool audio, bool isDecoder);
289 void finishFlushIfPossible();
290
291 void onStart(
292 int64_t startPositionUs = -1,
293 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC);
294 void onResume();
295 void onPause();
296
297 bool audioDecoderStillNeeded();
298
299 void flushDecoder(bool audio, bool needShutdown);
300
301 void finishResume();
302 void notifyDriverSeekComplete();
303
304 void postScanSources();
305
306 void schedulePollDuration();
307 void cancelPollDuration();
308
309 void processDeferredActions();
310
311 void performSeek(int64_t seekTimeUs, MediaPlayer2SeekMode mode);
312 void performDecoderFlush(FlushCommand audio, FlushCommand video);
313 void performReset();
314 void performScanSources();
Wei Jia28288fb2017-12-15 13:45:29 -0800315 void performSetSurface(const sp<ANativeWindowWrapper> &nw);
Wei Jia53692fa2017-12-11 10:33:46 -0800316 void performResumeDecoders(bool needNotify);
317
318 void onSourceNotify(const sp<AMessage> &msg);
319 void onClosedCaptionNotify(const sp<AMessage> &msg);
320
321 void queueDecoderShutdown(
322 bool audio, bool video, const sp<AMessage> &reply);
323
324 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
325 void sendTimedMetaData(const sp<ABuffer> &buffer);
326 void sendTimedTextData(const sp<ABuffer> &buffer);
327
328 void writeTrackInfo(Parcel* reply, const sp<AMessage>& format) const;
329
330 status_t onPrepareDrm(const sp<AMessage> &msg);
331 status_t onReleaseDrm();
332
333 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer2);
334};
335
336} // namespace android
337
338#endif // NU_PLAYER2_H_