blob: e9b5f116358d39e0f749bf8a7303fc14cc2d77be [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
Robert Shih3c3728d2018-12-04 17:06:36 -080095 status_t prepareDrm(int64_t srcId, const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId);
96 status_t releaseDrm(int64_t srcId);
Wei Jia53692fa2017-12-11 10:33:46 -080097
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;
Robert Shihc3fca0e2018-12-04 17:08:04 -0800181 // Modular DRM
182 sp<AMediaCryptoWrapper> mCrypto;
183 bool mIsDrmProtected = false;
Wei Jiaf01e3122018-10-18 11:49:44 -0700184 };
185
Wei Jia53692fa2017-12-11 10:33:46 -0800186 wp<NuPlayer2Driver> mDriver;
Wei Jia53692fa2017-12-11 10:33:46 -0800187 pid_t mPID;
Wei Jia003fdb52018-02-06 14:44:32 -0800188 uid_t mUID;
Wei Jia53692fa2017-12-11 10:33:46 -0800189 const sp<MediaClock> mMediaClock;
190 Mutex mSourceLock; // guard |mSource|.
Wei Jiaf01e3122018-10-18 11:49:44 -0700191 SourceInfo mCurrentSourceInfo;
192 SourceInfo mNextSourceInfo;
Wei Jia28288fb2017-12-15 13:45:29 -0800193 sp<ANativeWindowWrapper> mNativeWindow;
Wei Jia33abcc72018-01-30 09:47:38 -0800194 sp<MediaPlayer2Interface::AudioSink> mAudioSink;
Wei Jia53692fa2017-12-11 10:33:46 -0800195 sp<DecoderBase> mVideoDecoder;
196 bool mOffloadAudio;
197 sp<DecoderBase> mAudioDecoder;
198 sp<CCDecoder> mCCDecoder;
199 sp<Renderer> mRenderer;
200 sp<ALooper> mRendererLooper;
201 int32_t mAudioDecoderGeneration;
202 int32_t mVideoDecoderGeneration;
203 int32_t mRendererGeneration;
Wei Jiad1864f92018-10-19 12:34:56 -0700204 int32_t mEOSMonitorGeneration;
Wei Jia53692fa2017-12-11 10:33:46 -0800205
206 Mutex mPlayingTimeLock;
207 int64_t mLastStartedPlayingTimeNs;
208 void stopPlaybackTimer(const char *where);
209 void startPlaybackTimer(const char *where);
210
211 int64_t mLastStartedRebufferingTimeNs;
212 void startRebufferingTimer();
213 void stopRebufferingTimer(bool exitingPlayback);
214
215 int64_t mPreviousSeekTimeUs;
216
217 List<sp<Action> > mDeferredActions;
218
219 bool mAudioEOS;
220 bool mVideoEOS;
221
222 bool mScanSourcesPending;
223 int32_t mScanSourcesGeneration;
224
225 int32_t mPollDurationGeneration;
226 int32_t mTimedTextGeneration;
227
228 enum FlushStatus {
229 NONE,
230 FLUSHING_DECODER,
231 FLUSHING_DECODER_SHUTDOWN,
232 SHUTTING_DOWN_DECODER,
233 FLUSHED,
234 SHUT_DOWN,
235 };
236
237 enum FlushCommand {
238 FLUSH_CMD_NONE,
239 FLUSH_CMD_FLUSH,
240 FLUSH_CMD_SHUTDOWN,
241 };
242
243 // Status of flush responses from the decoder and renderer.
244 bool mFlushComplete[2][2];
245
246 FlushStatus mFlushingAudio;
247 FlushStatus mFlushingVideo;
248
249 // Status of flush responses from the decoder and renderer.
250 bool mResumePending;
251
252 int32_t mVideoScalingMode;
253
254 AudioPlaybackRate mPlaybackSettings;
255 AVSyncSettings mSyncSettings;
256 float mVideoFpsHint;
257 bool mStarted;
258 bool mPrepared;
259 bool mResetting;
260 bool mSourceStarted;
261 bool mAudioDecoderError;
262 bool mVideoDecoderError;
263
264 // Actual pause state, either as requested by client or due to buffering.
265 bool mPaused;
266
267 // Pause state as requested by client. Note that if mPausedByClient is
268 // true, mPaused is always true; if mPausedByClient is false, mPaused could
269 // still become true, when we pause internally due to buffering.
270 bool mPausedByClient;
271
272 // Pause state as requested by source (internally) due to buffering
273 bool mPausedForBuffering;
274
Wei Jia53692fa2017-12-11 10:33:46 -0800275 inline const sp<DecoderBase> &getDecoder(bool audio) {
276 return audio ? mAudioDecoder : mVideoDecoder;
277 }
278
279 inline void clearFlushComplete() {
280 mFlushComplete[0][0] = false;
281 mFlushComplete[0][1] = false;
282 mFlushComplete[1][0] = false;
283 mFlushComplete[1][1] = false;
284 }
285
Wei Jia57aeffd2018-02-15 16:01:14 -0800286 void disconnectSource();
287
Wei Jia72bf2a02018-02-06 15:29:23 -0800288 status_t createNuPlayer2Source(const sp<DataSourceDesc> &dsd,
289 sp<Source> *source,
290 DATA_SOURCE_TYPE *dataSourceType);
291
Wei Jia53692fa2017-12-11 10:33:46 -0800292 void tryOpenAudioSinkForOffload(
293 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo);
294 void closeAudioSink();
295 void restartAudio(
296 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder);
297 void determineAudioModeChange(const sp<AMessage> &audioFormat);
298
299 status_t instantiateDecoder(
300 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true);
301
Wei Jia53692fa2017-12-11 10:33:46 -0800302 void updateVideoSize(
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800303 int64_t srcId,
Wei Jia53692fa2017-12-11 10:33:46 -0800304 const sp<AMessage> &inputFormat,
305 const sp<AMessage> &outputFormat = NULL);
306
Dongwon Kang41929fb2018-09-09 08:29:56 -0700307 void notifyListener(int64_t srcId, int msg, int ext1, int ext2, const PlayerMessage *in = NULL);
Wei Jia53692fa2017-12-11 10:33:46 -0800308
Wei Jiad1864f92018-10-19 12:34:56 -0700309 void addEndTimeMonitor();
310
Wei Jia53692fa2017-12-11 10:33:46 -0800311 void handleFlushComplete(bool audio, bool isDecoder);
312 void finishFlushIfPossible();
313
Wei Jia6376cd52018-09-26 11:42:55 -0700314 void onStart(bool play);
Wei Jia53692fa2017-12-11 10:33:46 -0800315 void onResume();
316 void onPause();
317
318 bool audioDecoderStillNeeded();
319
320 void flushDecoder(bool audio, bool needShutdown);
321
322 void finishResume();
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800323 void notifyDriverSeekComplete(int64_t srcId);
Wei Jia53692fa2017-12-11 10:33:46 -0800324
325 void postScanSources();
326
327 void schedulePollDuration();
328 void cancelPollDuration();
329
330 void processDeferredActions();
331
332 void performSeek(int64_t seekTimeUs, MediaPlayer2SeekMode mode);
333 void performDecoderFlush(FlushCommand audio, FlushCommand video);
334 void performReset();
Wei Jia57aeffd2018-02-15 16:01:14 -0800335 void performPlayNextDataSource();
Wei Jia53692fa2017-12-11 10:33:46 -0800336 void performScanSources();
Wei Jia28288fb2017-12-15 13:45:29 -0800337 void performSetSurface(const sp<ANativeWindowWrapper> &nw);
Wei Jia53692fa2017-12-11 10:33:46 -0800338 void performResumeDecoders(bool needNotify);
339
340 void onSourceNotify(const sp<AMessage> &msg);
341 void onClosedCaptionNotify(const sp<AMessage> &msg);
342
343 void queueDecoderShutdown(
344 bool audio, bool video, const sp<AMessage> &reply);
345
346 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
347 void sendTimedMetaData(const sp<ABuffer> &buffer);
348 void sendTimedTextData(const sp<ABuffer> &buffer);
349
Dongwon Kang9f631982018-07-10 12:34:41 -0700350 void writeTrackInfo(PlayerMessage* reply, const sp<AMessage>& format) const;
Wei Jia53692fa2017-12-11 10:33:46 -0800351
352 status_t onPrepareDrm(const sp<AMessage> &msg);
Robert Shihc3fca0e2018-12-04 17:08:04 -0800353 status_t onReleaseDrm(const sp<AMessage> &msg);
354
355 SourceInfo* getSourceInfoByIdInMsg(const sp<AMessage> &msg);
356 void resetSourceInfo(SourceInfo &srcInfo);
Wei Jia53692fa2017-12-11 10:33:46 -0800357
358 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer2);
359};
360
361} // namespace android
362
363#endif // NU_PLAYER2_H_