blob: 4db0cbf8ea65b8ddc6d4318f13077c6a0748ff38 [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);
82
83 status_t setVideoScalingMode(int32_t mode);
Dongwon Kang9f631982018-07-10 12:34:41 -070084 status_t getTrackInfo(PlayerMessage* reply) const;
85 status_t getSelectedTrack(int32_t type, PlayerMessage* reply) const;
Wei Jia53692fa2017-12-11 10:33:46 -080086 status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs);
87 status_t getCurrentPosition(int64_t *mediaUs);
88 void getStats(Vector<sp<AMessage> > *mTrackStats);
89
90 sp<MetaData> getFileMeta();
91 float getFrameRate();
92
93 // Modular DRM
94 status_t prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId);
95 status_t releaseDrm();
96
97 const char *getDataSourceType();
98
99protected:
100 virtual ~NuPlayer2();
101
102 virtual void onMessageReceived(const sp<AMessage> &msg);
103
104public:
105 struct StreamListener;
106 struct Source;
107
108private:
109 struct Decoder;
110 struct DecoderBase;
111 struct DecoderPassThrough;
112 struct CCDecoder;
Wei Jia2409c872018-02-02 10:34:33 -0800113 struct GenericSource2;
114 struct HTTPLiveSource2;
Wei Jia53692fa2017-12-11 10:33:46 -0800115 struct Renderer;
Wei Jia2409c872018-02-02 10:34:33 -0800116 struct RTSPSource2;
Wei Jia53692fa2017-12-11 10:33:46 -0800117 struct Action;
118 struct SeekAction;
119 struct SetSurfaceAction;
120 struct ResumeDecoderAction;
121 struct FlushDecoderAction;
122 struct PostMessageAction;
123 struct SimpleAction;
124
125 enum {
126 kWhatSetDataSource = '=DaS',
127 kWhatPrepare = 'prep',
Wei Jia72bf2a02018-02-06 15:29:23 -0800128 kWhatPrepareNextDataSource = 'pNDS',
Wei Jia57aeffd2018-02-15 16:01:14 -0800129 kWhatPlayNextDataSource = 'plNS',
Wei Jia53692fa2017-12-11 10:33:46 -0800130 kWhatSetVideoSurface = '=VSu',
131 kWhatSetAudioSink = '=AuS',
132 kWhatMoreDataQueued = 'more',
133 kWhatConfigPlayback = 'cfPB',
134 kWhatConfigSync = 'cfSy',
135 kWhatGetPlaybackSettings = 'gPbS',
136 kWhatGetSyncSettings = 'gSyS',
137 kWhatStart = 'strt',
138 kWhatScanSources = 'scan',
139 kWhatVideoNotify = 'vidN',
140 kWhatAudioNotify = 'audN',
141 kWhatClosedCaptionNotify = 'capN',
142 kWhatRendererNotify = 'renN',
143 kWhatReset = 'rset',
144 kWhatNotifyTime = 'nfyT',
145 kWhatSeek = 'seek',
146 kWhatPause = 'paus',
147 kWhatResume = 'rsme',
148 kWhatPollDuration = 'polD',
149 kWhatSourceNotify = 'srcN',
150 kWhatGetTrackInfo = 'gTrI',
151 kWhatGetSelectedTrack = 'gSel',
152 kWhatSelectTrack = 'selT',
153 kWhatGetBufferingSettings = 'gBus',
154 kWhatSetBufferingSettings = 'sBuS',
155 kWhatPrepareDrm = 'pDrm',
156 kWhatReleaseDrm = 'rDrm',
157 };
158
Wei Jiaf01e3122018-10-18 11:49:44 -0700159 typedef enum {
160 DATA_SOURCE_TYPE_NONE,
161 DATA_SOURCE_TYPE_HTTP_LIVE,
162 DATA_SOURCE_TYPE_RTSP,
163 DATA_SOURCE_TYPE_GENERIC_URL,
164 DATA_SOURCE_TYPE_GENERIC_FD,
165 DATA_SOURCE_TYPE_MEDIA,
166 } DATA_SOURCE_TYPE;
167
168 struct SourceInfo {
169 SourceInfo();
170
171 sp<Source> mSource;
172 std::atomic<DATA_SOURCE_TYPE> mDataSourceType;
173 int64_t mSrcId;
174 uint32_t mSourceFlags;
175 int64_t mStartTimeUs;
176 int64_t mEndTimeUs;
177 };
178
Wei Jia53692fa2017-12-11 10:33:46 -0800179 wp<NuPlayer2Driver> mDriver;
Wei Jia53692fa2017-12-11 10:33:46 -0800180 pid_t mPID;
Wei Jia003fdb52018-02-06 14:44:32 -0800181 uid_t mUID;
Wei Jia53692fa2017-12-11 10:33:46 -0800182 const sp<MediaClock> mMediaClock;
183 Mutex mSourceLock; // guard |mSource|.
Wei Jiaf01e3122018-10-18 11:49:44 -0700184 SourceInfo mCurrentSourceInfo;
185 SourceInfo mNextSourceInfo;
Wei Jia28288fb2017-12-15 13:45:29 -0800186 sp<ANativeWindowWrapper> mNativeWindow;
Wei Jia33abcc72018-01-30 09:47:38 -0800187 sp<MediaPlayer2Interface::AudioSink> mAudioSink;
Wei Jia53692fa2017-12-11 10:33:46 -0800188 sp<DecoderBase> mVideoDecoder;
189 bool mOffloadAudio;
190 sp<DecoderBase> mAudioDecoder;
191 sp<CCDecoder> mCCDecoder;
192 sp<Renderer> mRenderer;
193 sp<ALooper> mRendererLooper;
194 int32_t mAudioDecoderGeneration;
195 int32_t mVideoDecoderGeneration;
196 int32_t mRendererGeneration;
197
198 Mutex mPlayingTimeLock;
199 int64_t mLastStartedPlayingTimeNs;
200 void stopPlaybackTimer(const char *where);
201 void startPlaybackTimer(const char *where);
202
203 int64_t mLastStartedRebufferingTimeNs;
204 void startRebufferingTimer();
205 void stopRebufferingTimer(bool exitingPlayback);
206
207 int64_t mPreviousSeekTimeUs;
208
209 List<sp<Action> > mDeferredActions;
210
211 bool mAudioEOS;
212 bool mVideoEOS;
213
214 bool mScanSourcesPending;
215 int32_t mScanSourcesGeneration;
216
217 int32_t mPollDurationGeneration;
218 int32_t mTimedTextGeneration;
219
220 enum FlushStatus {
221 NONE,
222 FLUSHING_DECODER,
223 FLUSHING_DECODER_SHUTDOWN,
224 SHUTTING_DOWN_DECODER,
225 FLUSHED,
226 SHUT_DOWN,
227 };
228
229 enum FlushCommand {
230 FLUSH_CMD_NONE,
231 FLUSH_CMD_FLUSH,
232 FLUSH_CMD_SHUTDOWN,
233 };
234
235 // Status of flush responses from the decoder and renderer.
236 bool mFlushComplete[2][2];
237
238 FlushStatus mFlushingAudio;
239 FlushStatus mFlushingVideo;
240
241 // Status of flush responses from the decoder and renderer.
242 bool mResumePending;
243
244 int32_t mVideoScalingMode;
245
246 AudioPlaybackRate mPlaybackSettings;
247 AVSyncSettings mSyncSettings;
248 float mVideoFpsHint;
249 bool mStarted;
250 bool mPrepared;
251 bool mResetting;
252 bool mSourceStarted;
253 bool mAudioDecoderError;
254 bool mVideoDecoderError;
255
256 // Actual pause state, either as requested by client or due to buffering.
257 bool mPaused;
258
259 // Pause state as requested by client. Note that if mPausedByClient is
260 // true, mPaused is always true; if mPausedByClient is false, mPaused could
261 // still become true, when we pause internally due to buffering.
262 bool mPausedByClient;
263
264 // Pause state as requested by source (internally) due to buffering
265 bool mPausedForBuffering;
266
267 // Modular DRM
268 sp<AMediaCryptoWrapper> mCrypto;
269 bool mIsDrmProtected;
270
Wei Jia53692fa2017-12-11 10:33:46 -0800271 inline const sp<DecoderBase> &getDecoder(bool audio) {
272 return audio ? mAudioDecoder : mVideoDecoder;
273 }
274
275 inline void clearFlushComplete() {
276 mFlushComplete[0][0] = false;
277 mFlushComplete[0][1] = false;
278 mFlushComplete[1][0] = false;
279 mFlushComplete[1][1] = false;
280 }
281
Wei Jia57aeffd2018-02-15 16:01:14 -0800282 void disconnectSource();
283
Wei Jia72bf2a02018-02-06 15:29:23 -0800284 status_t createNuPlayer2Source(const sp<DataSourceDesc> &dsd,
285 sp<Source> *source,
286 DATA_SOURCE_TYPE *dataSourceType);
287
Wei Jia53692fa2017-12-11 10:33:46 -0800288 void tryOpenAudioSinkForOffload(
289 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo);
290 void closeAudioSink();
291 void restartAudio(
292 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder);
293 void determineAudioModeChange(const sp<AMessage> &audioFormat);
294
295 status_t instantiateDecoder(
296 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true);
297
Wei Jia53692fa2017-12-11 10:33:46 -0800298 void updateVideoSize(
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800299 int64_t srcId,
Wei Jia53692fa2017-12-11 10:33:46 -0800300 const sp<AMessage> &inputFormat,
301 const sp<AMessage> &outputFormat = NULL);
302
Dongwon Kang41929fb2018-09-09 08:29:56 -0700303 void notifyListener(int64_t srcId, int msg, int ext1, int ext2, const PlayerMessage *in = NULL);
Wei Jia53692fa2017-12-11 10:33:46 -0800304
305 void handleFlushComplete(bool audio, bool isDecoder);
306 void finishFlushIfPossible();
307
Wei Jia6376cd52018-09-26 11:42:55 -0700308 void onStart(bool play);
Wei Jia53692fa2017-12-11 10:33:46 -0800309 void onResume();
310 void onPause();
311
312 bool audioDecoderStillNeeded();
313
314 void flushDecoder(bool audio, bool needShutdown);
315
316 void finishResume();
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800317 void notifyDriverSeekComplete(int64_t srcId);
Wei Jia53692fa2017-12-11 10:33:46 -0800318
319 void postScanSources();
320
321 void schedulePollDuration();
322 void cancelPollDuration();
323
324 void processDeferredActions();
325
326 void performSeek(int64_t seekTimeUs, MediaPlayer2SeekMode mode);
327 void performDecoderFlush(FlushCommand audio, FlushCommand video);
328 void performReset();
Wei Jia57aeffd2018-02-15 16:01:14 -0800329 void performPlayNextDataSource();
Wei Jia53692fa2017-12-11 10:33:46 -0800330 void performScanSources();
Wei Jia28288fb2017-12-15 13:45:29 -0800331 void performSetSurface(const sp<ANativeWindowWrapper> &nw);
Wei Jia53692fa2017-12-11 10:33:46 -0800332 void performResumeDecoders(bool needNotify);
333
334 void onSourceNotify(const sp<AMessage> &msg);
335 void onClosedCaptionNotify(const sp<AMessage> &msg);
336
337 void queueDecoderShutdown(
338 bool audio, bool video, const sp<AMessage> &reply);
339
340 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
341 void sendTimedMetaData(const sp<ABuffer> &buffer);
342 void sendTimedTextData(const sp<ABuffer> &buffer);
343
Dongwon Kang9f631982018-07-10 12:34:41 -0700344 void writeTrackInfo(PlayerMessage* reply, const sp<AMessage>& format) const;
Wei Jia53692fa2017-12-11 10:33:46 -0800345
346 status_t onPrepareDrm(const sp<AMessage> &msg);
347 status_t onReleaseDrm();
348
349 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer2);
350};
351
352} // namespace android
353
354#endif // NU_PLAYER2_H_