blob: 3ecdb0123921e0817fb18c29ebbc71bbd3bdaade [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>
Wei Jia53692fa2017-12-11 10:33:46 -080022#include <media/stagefright/foundation/AHandler.h>
23
Wei Jia51b69562018-02-05 16:17:13 -080024#include <mediaplayer2/MediaPlayer2Interface.h>
25
Dongwon Kang9f631982018-07-10 12:34:41 -070026#include "mediaplayer2.pb.h"
27
28using android::media::MediaPlayer2Proto::PlayerMessage;
29
Wei Jia53692fa2017-12-11 10:33:46 -080030namespace android {
31
32struct ABuffer;
33struct AMediaCryptoWrapper;
34struct AMessage;
Wei Jia28288fb2017-12-15 13:45:29 -080035struct ANativeWindowWrapper;
Wei Jia53692fa2017-12-11 10:33:46 -080036struct AudioPlaybackRate;
37struct AVSyncSettings;
Wei Jiac2636032018-02-01 09:15:25 -080038struct DataSourceDesc;
Wei Jia53692fa2017-12-11 10:33:46 -080039struct MediaClock;
40struct MediaHTTPService;
41class MetaData;
42struct NuPlayer2Driver;
43
44struct NuPlayer2 : public AHandler {
Wei Jia003fdb52018-02-06 14:44:32 -080045 explicit NuPlayer2(pid_t pid, uid_t uid, const sp<MediaClock> &mediaClock);
Wei Jia53692fa2017-12-11 10:33:46 -080046
47 void setDriver(const wp<NuPlayer2Driver> &driver);
48
Wei Jiac2636032018-02-01 09:15:25 -080049 void setDataSourceAsync(const sp<DataSourceDesc> &dsd);
Wei Jia72bf2a02018-02-06 15:29:23 -080050 void prepareNextDataSourceAsync(const sp<DataSourceDesc> &dsd);
Wei Jia57aeffd2018-02-15 16:01:14 -080051 void playNextDataSource(int64_t srcId);
Wei Jia53692fa2017-12-11 10:33:46 -080052
53 status_t getBufferingSettings(BufferingSettings* buffering /* nonnull */);
54 status_t setBufferingSettings(const BufferingSettings& buffering);
55
56 void prepareAsync();
57
Wei Jia28288fb2017-12-15 13:45:29 -080058 void setVideoSurfaceTextureAsync(const sp<ANativeWindowWrapper> &nww);
Wei Jia53692fa2017-12-11 10:33:46 -080059
Wei Jia33abcc72018-01-30 09:47:38 -080060 void setAudioSink(const sp<MediaPlayer2Interface::AudioSink> &sink);
Wei Jia53692fa2017-12-11 10:33:46 -080061 status_t setPlaybackSettings(const AudioPlaybackRate &rate);
62 status_t getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */);
63 status_t setSyncSettings(const AVSyncSettings &sync, float videoFpsHint);
64 status_t getSyncSettings(AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */);
65
66 void start();
67
68 void pause();
69
70 // Will notify the driver through "notifyResetComplete" once finished.
71 void resetAsync();
72
73 // Request a notification when specified media time is reached.
74 status_t notifyAt(int64_t mediaTimeUs);
75
76 // Will notify the driver through "notifySeekComplete" once finished
77 // and needNotify is true.
78 void seekToAsync(
79 int64_t seekTimeUs,
80 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC,
81 bool needNotify = false);
Wei Jiad1864f92018-10-19 12:34:56 -070082 void rewind();
Wei Jia53692fa2017-12-11 10:33:46 -080083
84 status_t setVideoScalingMode(int32_t mode);
Dongwon Kang9f631982018-07-10 12:34:41 -070085 status_t getTrackInfo(PlayerMessage* reply) const;
86 status_t getSelectedTrack(int32_t type, PlayerMessage* reply) const;
Wei Jia53692fa2017-12-11 10:33:46 -080087 status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs);
88 status_t getCurrentPosition(int64_t *mediaUs);
89 void getStats(Vector<sp<AMessage> > *mTrackStats);
90
91 sp<MetaData> getFileMeta();
92 float getFrameRate();
93
94 // Modular DRM
95 status_t prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId);
96 status_t releaseDrm();
97
98 const char *getDataSourceType();
99
100protected:
101 virtual ~NuPlayer2();
102
103 virtual void onMessageReceived(const sp<AMessage> &msg);
104
105public:
106 struct StreamListener;
107 struct Source;
108
109private:
110 struct Decoder;
111 struct DecoderBase;
112 struct DecoderPassThrough;
113 struct CCDecoder;
Wei Jia2409c872018-02-02 10:34:33 -0800114 struct GenericSource2;
115 struct HTTPLiveSource2;
Wei Jia53692fa2017-12-11 10:33:46 -0800116 struct Renderer;
Wei Jia2409c872018-02-02 10:34:33 -0800117 struct RTSPSource2;
Wei Jia53692fa2017-12-11 10:33:46 -0800118 struct Action;
119 struct SeekAction;
120 struct SetSurfaceAction;
121 struct ResumeDecoderAction;
122 struct FlushDecoderAction;
123 struct PostMessageAction;
124 struct SimpleAction;
125
126 enum {
127 kWhatSetDataSource = '=DaS',
128 kWhatPrepare = 'prep',
Wei Jia72bf2a02018-02-06 15:29:23 -0800129 kWhatPrepareNextDataSource = 'pNDS',
Wei Jia57aeffd2018-02-15 16:01:14 -0800130 kWhatPlayNextDataSource = 'plNS',
Wei Jia53692fa2017-12-11 10:33:46 -0800131 kWhatSetVideoSurface = '=VSu',
132 kWhatSetAudioSink = '=AuS',
133 kWhatMoreDataQueued = 'more',
134 kWhatConfigPlayback = 'cfPB',
135 kWhatConfigSync = 'cfSy',
136 kWhatGetPlaybackSettings = 'gPbS',
137 kWhatGetSyncSettings = 'gSyS',
138 kWhatStart = 'strt',
139 kWhatScanSources = 'scan',
140 kWhatVideoNotify = 'vidN',
141 kWhatAudioNotify = 'audN',
142 kWhatClosedCaptionNotify = 'capN',
143 kWhatRendererNotify = 'renN',
144 kWhatReset = 'rset',
145 kWhatNotifyTime = 'nfyT',
146 kWhatSeek = 'seek',
147 kWhatPause = 'paus',
148 kWhatResume = 'rsme',
149 kWhatPollDuration = 'polD',
150 kWhatSourceNotify = 'srcN',
151 kWhatGetTrackInfo = 'gTrI',
152 kWhatGetSelectedTrack = 'gSel',
153 kWhatSelectTrack = 'selT',
154 kWhatGetBufferingSettings = 'gBus',
155 kWhatSetBufferingSettings = 'sBuS',
156 kWhatPrepareDrm = 'pDrm',
157 kWhatReleaseDrm = 'rDrm',
Wei Jiad1864f92018-10-19 12:34:56 -0700158 kWhatRewind = 'reWd',
159 kWhatEOSMonitor = 'eosM',
Wei Jia53692fa2017-12-11 10:33:46 -0800160 };
161
Wei Jiaf01e3122018-10-18 11:49:44 -0700162 typedef enum {
163 DATA_SOURCE_TYPE_NONE,
164 DATA_SOURCE_TYPE_HTTP_LIVE,
165 DATA_SOURCE_TYPE_RTSP,
166 DATA_SOURCE_TYPE_GENERIC_URL,
167 DATA_SOURCE_TYPE_GENERIC_FD,
168 DATA_SOURCE_TYPE_MEDIA,
169 } DATA_SOURCE_TYPE;
170
171 struct SourceInfo {
172 SourceInfo();
Wei Jiad1864f92018-10-19 12:34:56 -0700173 SourceInfo &operator=(const SourceInfo &);
Wei Jiaf01e3122018-10-18 11:49:44 -0700174
175 sp<Source> mSource;
176 std::atomic<DATA_SOURCE_TYPE> mDataSourceType;
177 int64_t mSrcId;
178 uint32_t mSourceFlags;
179 int64_t mStartTimeUs;
180 int64_t mEndTimeUs;
181 };
182
Wei Jia53692fa2017-12-11 10:33:46 -0800183 wp<NuPlayer2Driver> mDriver;
Wei Jia53692fa2017-12-11 10:33:46 -0800184 pid_t mPID;
Wei Jia003fdb52018-02-06 14:44:32 -0800185 uid_t mUID;
Wei Jia53692fa2017-12-11 10:33:46 -0800186 const sp<MediaClock> mMediaClock;
187 Mutex mSourceLock; // guard |mSource|.
Wei Jiaf01e3122018-10-18 11:49:44 -0700188 SourceInfo mCurrentSourceInfo;
189 SourceInfo mNextSourceInfo;
Wei Jia28288fb2017-12-15 13:45:29 -0800190 sp<ANativeWindowWrapper> mNativeWindow;
Wei Jia33abcc72018-01-30 09:47:38 -0800191 sp<MediaPlayer2Interface::AudioSink> mAudioSink;
Wei Jia53692fa2017-12-11 10:33:46 -0800192 sp<DecoderBase> mVideoDecoder;
193 bool mOffloadAudio;
194 sp<DecoderBase> mAudioDecoder;
195 sp<CCDecoder> mCCDecoder;
196 sp<Renderer> mRenderer;
197 sp<ALooper> mRendererLooper;
198 int32_t mAudioDecoderGeneration;
199 int32_t mVideoDecoderGeneration;
200 int32_t mRendererGeneration;
Wei Jiad1864f92018-10-19 12:34:56 -0700201 int32_t mEOSMonitorGeneration;
Wei Jia53692fa2017-12-11 10:33:46 -0800202
203 Mutex mPlayingTimeLock;
204 int64_t mLastStartedPlayingTimeNs;
205 void stopPlaybackTimer(const char *where);
206 void startPlaybackTimer(const char *where);
207
208 int64_t mLastStartedRebufferingTimeNs;
209 void startRebufferingTimer();
210 void stopRebufferingTimer(bool exitingPlayback);
211
212 int64_t mPreviousSeekTimeUs;
213
214 List<sp<Action> > mDeferredActions;
215
216 bool mAudioEOS;
217 bool mVideoEOS;
218
219 bool mScanSourcesPending;
220 int32_t mScanSourcesGeneration;
221
222 int32_t mPollDurationGeneration;
223 int32_t mTimedTextGeneration;
224
225 enum FlushStatus {
226 NONE,
227 FLUSHING_DECODER,
228 FLUSHING_DECODER_SHUTDOWN,
229 SHUTTING_DOWN_DECODER,
230 FLUSHED,
231 SHUT_DOWN,
232 };
233
234 enum FlushCommand {
235 FLUSH_CMD_NONE,
236 FLUSH_CMD_FLUSH,
237 FLUSH_CMD_SHUTDOWN,
238 };
239
240 // Status of flush responses from the decoder and renderer.
241 bool mFlushComplete[2][2];
242
243 FlushStatus mFlushingAudio;
244 FlushStatus mFlushingVideo;
245
246 // Status of flush responses from the decoder and renderer.
247 bool mResumePending;
248
249 int32_t mVideoScalingMode;
250
251 AudioPlaybackRate mPlaybackSettings;
252 AVSyncSettings mSyncSettings;
253 float mVideoFpsHint;
254 bool mStarted;
255 bool mPrepared;
256 bool mResetting;
257 bool mSourceStarted;
258 bool mAudioDecoderError;
259 bool mVideoDecoderError;
260
261 // Actual pause state, either as requested by client or due to buffering.
262 bool mPaused;
263
264 // Pause state as requested by client. Note that if mPausedByClient is
265 // true, mPaused is always true; if mPausedByClient is false, mPaused could
266 // still become true, when we pause internally due to buffering.
267 bool mPausedByClient;
268
269 // Pause state as requested by source (internally) due to buffering
270 bool mPausedForBuffering;
271
272 // Modular DRM
273 sp<AMediaCryptoWrapper> mCrypto;
274 bool mIsDrmProtected;
275
Wei Jia53692fa2017-12-11 10:33:46 -0800276 inline const sp<DecoderBase> &getDecoder(bool audio) {
277 return audio ? mAudioDecoder : mVideoDecoder;
278 }
279
280 inline void clearFlushComplete() {
281 mFlushComplete[0][0] = false;
282 mFlushComplete[0][1] = false;
283 mFlushComplete[1][0] = false;
284 mFlushComplete[1][1] = false;
285 }
286
Wei Jia57aeffd2018-02-15 16:01:14 -0800287 void disconnectSource();
288
Wei Jia72bf2a02018-02-06 15:29:23 -0800289 status_t createNuPlayer2Source(const sp<DataSourceDesc> &dsd,
290 sp<Source> *source,
291 DATA_SOURCE_TYPE *dataSourceType);
292
Wei Jia53692fa2017-12-11 10:33:46 -0800293 void tryOpenAudioSinkForOffload(
294 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo);
295 void closeAudioSink();
296 void restartAudio(
297 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder);
298 void determineAudioModeChange(const sp<AMessage> &audioFormat);
299
300 status_t instantiateDecoder(
301 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true);
302
Wei Jia53692fa2017-12-11 10:33:46 -0800303 void updateVideoSize(
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800304 int64_t srcId,
Wei Jia53692fa2017-12-11 10:33:46 -0800305 const sp<AMessage> &inputFormat,
306 const sp<AMessage> &outputFormat = NULL);
307
Dongwon Kang41929fb2018-09-09 08:29:56 -0700308 void notifyListener(int64_t srcId, int msg, int ext1, int ext2, const PlayerMessage *in = NULL);
Wei Jia53692fa2017-12-11 10:33:46 -0800309
Wei Jiad1864f92018-10-19 12:34:56 -0700310 void addEndTimeMonitor();
311
Wei Jia53692fa2017-12-11 10:33:46 -0800312 void handleFlushComplete(bool audio, bool isDecoder);
313 void finishFlushIfPossible();
314
Wei Jia6376cd52018-09-26 11:42:55 -0700315 void onStart(bool play);
Wei Jia53692fa2017-12-11 10:33:46 -0800316 void onResume();
317 void onPause();
318
319 bool audioDecoderStillNeeded();
320
321 void flushDecoder(bool audio, bool needShutdown);
322
323 void finishResume();
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800324 void notifyDriverSeekComplete(int64_t srcId);
Wei Jia53692fa2017-12-11 10:33:46 -0800325
326 void postScanSources();
327
328 void schedulePollDuration();
329 void cancelPollDuration();
330
331 void processDeferredActions();
332
333 void performSeek(int64_t seekTimeUs, MediaPlayer2SeekMode mode);
334 void performDecoderFlush(FlushCommand audio, FlushCommand video);
335 void performReset();
Wei Jia57aeffd2018-02-15 16:01:14 -0800336 void performPlayNextDataSource();
Wei Jia53692fa2017-12-11 10:33:46 -0800337 void performScanSources();
Wei Jia28288fb2017-12-15 13:45:29 -0800338 void performSetSurface(const sp<ANativeWindowWrapper> &nw);
Wei Jia53692fa2017-12-11 10:33:46 -0800339 void performResumeDecoders(bool needNotify);
340
341 void onSourceNotify(const sp<AMessage> &msg);
342 void onClosedCaptionNotify(const sp<AMessage> &msg);
343
344 void queueDecoderShutdown(
345 bool audio, bool video, const sp<AMessage> &reply);
346
347 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
348 void sendTimedMetaData(const sp<ABuffer> &buffer);
349 void sendTimedTextData(const sp<ABuffer> &buffer);
350
Dongwon Kang9f631982018-07-10 12:34:41 -0700351 void writeTrackInfo(PlayerMessage* reply, const sp<AMessage>& format) const;
Wei Jia53692fa2017-12-11 10:33:46 -0800352
353 status_t onPrepareDrm(const sp<AMessage> &msg);
354 status_t onReleaseDrm();
355
356 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer2);
357};
358
359} // namespace android
360
361#endif // NU_PLAYER2_H_