blob: b8fb988056d49f24668cb9abd08636bcb7784ddb [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>
Dichen Zhangfc9f40f2019-01-04 14:15:28 -080025#include <mediaplayer2/JObjectHolder.h>
Wei Jia51b69562018-02-05 16:17:13 -080026
Dongwon Kang9f631982018-07-10 12:34:41 -070027#include "mediaplayer2.pb.h"
28
29using android::media::MediaPlayer2Proto::PlayerMessage;
30
Wei Jia53692fa2017-12-11 10:33:46 -080031namespace android {
32
33struct ABuffer;
34struct AMediaCryptoWrapper;
35struct AMessage;
Wei Jia28288fb2017-12-15 13:45:29 -080036struct ANativeWindowWrapper;
Wei Jia53692fa2017-12-11 10:33:46 -080037struct AudioPlaybackRate;
38struct AVSyncSettings;
Wei Jiac2636032018-02-01 09:15:25 -080039struct DataSourceDesc;
Wei Jia53692fa2017-12-11 10:33:46 -080040struct MediaClock;
41struct MediaHTTPService;
42class MetaData;
43struct NuPlayer2Driver;
44
45struct NuPlayer2 : public AHandler {
Dichen Zhangfc9f40f2019-01-04 14:15:28 -080046 explicit NuPlayer2(pid_t pid, uid_t uid,
47 const sp<MediaClock> &mediaClock, const sp<JObjectHolder> &context);
Wei Jia53692fa2017-12-11 10:33:46 -080048
49 void setDriver(const wp<NuPlayer2Driver> &driver);
50
Wei Jiac2636032018-02-01 09:15:25 -080051 void setDataSourceAsync(const sp<DataSourceDesc> &dsd);
Wei Jia72bf2a02018-02-06 15:29:23 -080052 void prepareNextDataSourceAsync(const sp<DataSourceDesc> &dsd);
Wei Jia57aeffd2018-02-15 16:01:14 -080053 void playNextDataSource(int64_t srcId);
Wei Jia53692fa2017-12-11 10:33:46 -080054
55 status_t getBufferingSettings(BufferingSettings* buffering /* nonnull */);
56 status_t setBufferingSettings(const BufferingSettings& buffering);
57
58 void prepareAsync();
59
Wei Jia28288fb2017-12-15 13:45:29 -080060 void setVideoSurfaceTextureAsync(const sp<ANativeWindowWrapper> &nww);
Wei Jia53692fa2017-12-11 10:33:46 -080061
Wei Jia33abcc72018-01-30 09:47:38 -080062 void setAudioSink(const sp<MediaPlayer2Interface::AudioSink> &sink);
Wei Jia53692fa2017-12-11 10:33:46 -080063 status_t setPlaybackSettings(const AudioPlaybackRate &rate);
64 status_t getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */);
65 status_t setSyncSettings(const AVSyncSettings &sync, float videoFpsHint);
66 status_t getSyncSettings(AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */);
67
68 void start();
69
70 void pause();
71
72 // Will notify the driver through "notifyResetComplete" once finished.
73 void resetAsync();
74
75 // Request a notification when specified media time is reached.
76 status_t notifyAt(int64_t mediaTimeUs);
77
78 // Will notify the driver through "notifySeekComplete" once finished
79 // and needNotify is true.
80 void seekToAsync(
81 int64_t seekTimeUs,
82 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC,
83 bool needNotify = false);
Wei Jiad1864f92018-10-19 12:34:56 -070084 void rewind();
Wei Jia53692fa2017-12-11 10:33:46 -080085
86 status_t setVideoScalingMode(int32_t mode);
Wei Jia17944af2018-12-13 18:13:10 -080087 status_t getTrackInfo(int64_t srcId, PlayerMessage* reply) const;
88 status_t getSelectedTrack(int64_t srcId, int32_t type, PlayerMessage* reply) const;
89 status_t selectTrack(int64_t srcId, size_t trackIndex, bool select, int64_t timeUs);
Wei Jia53692fa2017-12-11 10:33:46 -080090 status_t getCurrentPosition(int64_t *mediaUs);
91 void getStats(Vector<sp<AMessage> > *mTrackStats);
92
93 sp<MetaData> getFileMeta();
94 float getFrameRate();
95
96 // Modular DRM
Robert Shih3c3728d2018-12-04 17:06:36 -080097 status_t prepareDrm(int64_t srcId, const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId);
98 status_t releaseDrm(int64_t srcId);
Wei Jia53692fa2017-12-11 10:33:46 -080099
100 const char *getDataSourceType();
101
102protected:
103 virtual ~NuPlayer2();
104
105 virtual void onMessageReceived(const sp<AMessage> &msg);
106
107public:
108 struct StreamListener;
109 struct Source;
110
111private:
112 struct Decoder;
113 struct DecoderBase;
114 struct DecoderPassThrough;
115 struct CCDecoder;
Wei Jia2409c872018-02-02 10:34:33 -0800116 struct GenericSource2;
117 struct HTTPLiveSource2;
Wei Jia53692fa2017-12-11 10:33:46 -0800118 struct Renderer;
Wei Jia2409c872018-02-02 10:34:33 -0800119 struct RTSPSource2;
Wei Jia53692fa2017-12-11 10:33:46 -0800120 struct Action;
121 struct SeekAction;
122 struct SetSurfaceAction;
123 struct ResumeDecoderAction;
124 struct FlushDecoderAction;
125 struct PostMessageAction;
126 struct SimpleAction;
127
128 enum {
129 kWhatSetDataSource = '=DaS',
130 kWhatPrepare = 'prep',
Wei Jia72bf2a02018-02-06 15:29:23 -0800131 kWhatPrepareNextDataSource = 'pNDS',
Wei Jia57aeffd2018-02-15 16:01:14 -0800132 kWhatPlayNextDataSource = 'plNS',
Wei Jia53692fa2017-12-11 10:33:46 -0800133 kWhatSetVideoSurface = '=VSu',
134 kWhatSetAudioSink = '=AuS',
135 kWhatMoreDataQueued = 'more',
136 kWhatConfigPlayback = 'cfPB',
137 kWhatConfigSync = 'cfSy',
138 kWhatGetPlaybackSettings = 'gPbS',
139 kWhatGetSyncSettings = 'gSyS',
140 kWhatStart = 'strt',
141 kWhatScanSources = 'scan',
142 kWhatVideoNotify = 'vidN',
143 kWhatAudioNotify = 'audN',
144 kWhatClosedCaptionNotify = 'capN',
145 kWhatRendererNotify = 'renN',
146 kWhatReset = 'rset',
147 kWhatNotifyTime = 'nfyT',
148 kWhatSeek = 'seek',
149 kWhatPause = 'paus',
150 kWhatResume = 'rsme',
151 kWhatPollDuration = 'polD',
152 kWhatSourceNotify = 'srcN',
153 kWhatGetTrackInfo = 'gTrI',
154 kWhatGetSelectedTrack = 'gSel',
155 kWhatSelectTrack = 'selT',
156 kWhatGetBufferingSettings = 'gBus',
157 kWhatSetBufferingSettings = 'sBuS',
158 kWhatPrepareDrm = 'pDrm',
159 kWhatReleaseDrm = 'rDrm',
Wei Jiad1864f92018-10-19 12:34:56 -0700160 kWhatRewind = 'reWd',
161 kWhatEOSMonitor = 'eosM',
Wei Jia53692fa2017-12-11 10:33:46 -0800162 };
163
Wei Jiaf01e3122018-10-18 11:49:44 -0700164 typedef enum {
165 DATA_SOURCE_TYPE_NONE,
166 DATA_SOURCE_TYPE_HTTP_LIVE,
167 DATA_SOURCE_TYPE_RTSP,
168 DATA_SOURCE_TYPE_GENERIC_URL,
169 DATA_SOURCE_TYPE_GENERIC_FD,
170 DATA_SOURCE_TYPE_MEDIA,
171 } DATA_SOURCE_TYPE;
172
173 struct SourceInfo {
174 SourceInfo();
Wei Jiad1864f92018-10-19 12:34:56 -0700175 SourceInfo &operator=(const SourceInfo &);
Wei Jiaf01e3122018-10-18 11:49:44 -0700176
177 sp<Source> mSource;
178 std::atomic<DATA_SOURCE_TYPE> mDataSourceType;
179 int64_t mSrcId;
180 uint32_t mSourceFlags;
181 int64_t mStartTimeUs;
182 int64_t mEndTimeUs;
Robert Shihc3fca0e2018-12-04 17:08:04 -0800183 // Modular DRM
184 sp<AMediaCryptoWrapper> mCrypto;
185 bool mIsDrmProtected = false;
Wei Jiaf01e3122018-10-18 11:49:44 -0700186 };
187
Wei Jia53692fa2017-12-11 10:33:46 -0800188 wp<NuPlayer2Driver> mDriver;
Wei Jia53692fa2017-12-11 10:33:46 -0800189 pid_t mPID;
Wei Jia003fdb52018-02-06 14:44:32 -0800190 uid_t mUID;
Wei Jia53692fa2017-12-11 10:33:46 -0800191 const sp<MediaClock> mMediaClock;
192 Mutex mSourceLock; // guard |mSource|.
Wei Jiaf01e3122018-10-18 11:49:44 -0700193 SourceInfo mCurrentSourceInfo;
194 SourceInfo mNextSourceInfo;
Wei Jia28288fb2017-12-15 13:45:29 -0800195 sp<ANativeWindowWrapper> mNativeWindow;
Wei Jia33abcc72018-01-30 09:47:38 -0800196 sp<MediaPlayer2Interface::AudioSink> mAudioSink;
Wei Jia53692fa2017-12-11 10:33:46 -0800197 sp<DecoderBase> mVideoDecoder;
198 bool mOffloadAudio;
199 sp<DecoderBase> mAudioDecoder;
Ray Essick372e8f22019-02-11 11:38:31 -0800200 Mutex mDecoderLock; // guard |mAudioDecoder| and |mVideoDecoder|.
Wei Jia53692fa2017-12-11 10:33:46 -0800201 sp<CCDecoder> mCCDecoder;
202 sp<Renderer> mRenderer;
203 sp<ALooper> mRendererLooper;
204 int32_t mAudioDecoderGeneration;
205 int32_t mVideoDecoderGeneration;
206 int32_t mRendererGeneration;
Wei Jiad1864f92018-10-19 12:34:56 -0700207 int32_t mEOSMonitorGeneration;
Wei Jia53692fa2017-12-11 10:33:46 -0800208
209 Mutex mPlayingTimeLock;
210 int64_t mLastStartedPlayingTimeNs;
211 void stopPlaybackTimer(const char *where);
212 void startPlaybackTimer(const char *where);
213
214 int64_t mLastStartedRebufferingTimeNs;
215 void startRebufferingTimer();
216 void stopRebufferingTimer(bool exitingPlayback);
217
218 int64_t mPreviousSeekTimeUs;
219
220 List<sp<Action> > mDeferredActions;
221
222 bool mAudioEOS;
223 bool mVideoEOS;
224
225 bool mScanSourcesPending;
226 int32_t mScanSourcesGeneration;
227
228 int32_t mPollDurationGeneration;
229 int32_t mTimedTextGeneration;
230
231 enum FlushStatus {
232 NONE,
233 FLUSHING_DECODER,
234 FLUSHING_DECODER_SHUTDOWN,
235 SHUTTING_DOWN_DECODER,
236 FLUSHED,
237 SHUT_DOWN,
238 };
239
240 enum FlushCommand {
241 FLUSH_CMD_NONE,
242 FLUSH_CMD_FLUSH,
243 FLUSH_CMD_SHUTDOWN,
244 };
245
246 // Status of flush responses from the decoder and renderer.
247 bool mFlushComplete[2][2];
248
249 FlushStatus mFlushingAudio;
250 FlushStatus mFlushingVideo;
251
252 // Status of flush responses from the decoder and renderer.
253 bool mResumePending;
254
255 int32_t mVideoScalingMode;
256
257 AudioPlaybackRate mPlaybackSettings;
258 AVSyncSettings mSyncSettings;
259 float mVideoFpsHint;
260 bool mStarted;
261 bool mPrepared;
262 bool mResetting;
263 bool mSourceStarted;
264 bool mAudioDecoderError;
265 bool mVideoDecoderError;
266
267 // Actual pause state, either as requested by client or due to buffering.
268 bool mPaused;
269
270 // Pause state as requested by client. Note that if mPausedByClient is
271 // true, mPaused is always true; if mPausedByClient is false, mPaused could
272 // still become true, when we pause internally due to buffering.
273 bool mPausedByClient;
274
275 // Pause state as requested by source (internally) due to buffering
276 bool mPausedForBuffering;
277
Dichen Zhangfc9f40f2019-01-04 14:15:28 -0800278 // Passed from JAVA
279 const sp<JObjectHolder> mContext;
280
Wei Jia53692fa2017-12-11 10:33:46 -0800281 inline const sp<DecoderBase> &getDecoder(bool audio) {
282 return audio ? mAudioDecoder : mVideoDecoder;
283 }
284
285 inline void clearFlushComplete() {
286 mFlushComplete[0][0] = false;
287 mFlushComplete[0][1] = false;
288 mFlushComplete[1][0] = false;
289 mFlushComplete[1][1] = false;
290 }
291
Wei Jia57aeffd2018-02-15 16:01:14 -0800292 void disconnectSource();
293
Wei Jia72bf2a02018-02-06 15:29:23 -0800294 status_t createNuPlayer2Source(const sp<DataSourceDesc> &dsd,
295 sp<Source> *source,
296 DATA_SOURCE_TYPE *dataSourceType);
297
Wei Jia53692fa2017-12-11 10:33:46 -0800298 void tryOpenAudioSinkForOffload(
299 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo);
300 void closeAudioSink();
301 void restartAudio(
302 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder);
303 void determineAudioModeChange(const sp<AMessage> &audioFormat);
304
305 status_t instantiateDecoder(
306 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true);
307
Wei Jia53692fa2017-12-11 10:33:46 -0800308 void updateVideoSize(
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800309 int64_t srcId,
Wei Jia53692fa2017-12-11 10:33:46 -0800310 const sp<AMessage> &inputFormat,
311 const sp<AMessage> &outputFormat = NULL);
312
Dongwon Kang41929fb2018-09-09 08:29:56 -0700313 void notifyListener(int64_t srcId, int msg, int ext1, int ext2, const PlayerMessage *in = NULL);
Wei Jia53692fa2017-12-11 10:33:46 -0800314
Wei Jiad1864f92018-10-19 12:34:56 -0700315 void addEndTimeMonitor();
316
Wei Jia53692fa2017-12-11 10:33:46 -0800317 void handleFlushComplete(bool audio, bool isDecoder);
318 void finishFlushIfPossible();
319
Wei Jia6376cd52018-09-26 11:42:55 -0700320 void onStart(bool play);
Wei Jia53692fa2017-12-11 10:33:46 -0800321 void onResume();
322 void onPause();
323
324 bool audioDecoderStillNeeded();
325
326 void flushDecoder(bool audio, bool needShutdown);
327
328 void finishResume();
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800329 void notifyDriverSeekComplete(int64_t srcId);
Wei Jia53692fa2017-12-11 10:33:46 -0800330
331 void postScanSources();
332
333 void schedulePollDuration();
334 void cancelPollDuration();
335
336 void processDeferredActions();
337
338 void performSeek(int64_t seekTimeUs, MediaPlayer2SeekMode mode);
339 void performDecoderFlush(FlushCommand audio, FlushCommand video);
340 void performReset();
Wei Jia57aeffd2018-02-15 16:01:14 -0800341 void performPlayNextDataSource();
Wei Jia53692fa2017-12-11 10:33:46 -0800342 void performScanSources();
Wei Jia28288fb2017-12-15 13:45:29 -0800343 void performSetSurface(const sp<ANativeWindowWrapper> &nw);
Wei Jia53692fa2017-12-11 10:33:46 -0800344 void performResumeDecoders(bool needNotify);
345
346 void onSourceNotify(const sp<AMessage> &msg);
347 void onClosedCaptionNotify(const sp<AMessage> &msg);
348
349 void queueDecoderShutdown(
350 bool audio, bool video, const sp<AMessage> &reply);
351
352 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
353 void sendTimedMetaData(const sp<ABuffer> &buffer);
354 void sendTimedTextData(const sp<ABuffer> &buffer);
355
Dongwon Kang9f631982018-07-10 12:34:41 -0700356 void writeTrackInfo(PlayerMessage* reply, const sp<AMessage>& format) const;
Wei Jia53692fa2017-12-11 10:33:46 -0800357
358 status_t onPrepareDrm(const sp<AMessage> &msg);
Robert Shihc3fca0e2018-12-04 17:08:04 -0800359 status_t onReleaseDrm(const sp<AMessage> &msg);
360
361 SourceInfo* getSourceInfoByIdInMsg(const sp<AMessage> &msg);
362 void resetSourceInfo(SourceInfo &srcInfo);
Wei Jia53692fa2017-12-11 10:33:46 -0800363
364 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer2);
365};
366
367} // namespace android
368
369#endif // NU_PLAYER2_H_