blob: 96f85f9d90e5bb696235fc31ac60705904ad2829 [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
Wei Jia53692fa2017-12-11 10:33:46 -080026namespace android {
27
28struct ABuffer;
29struct AMediaCryptoWrapper;
30struct AMessage;
Wei Jia28288fb2017-12-15 13:45:29 -080031struct ANativeWindowWrapper;
Wei Jia53692fa2017-12-11 10:33:46 -080032struct AudioPlaybackRate;
33struct AVSyncSettings;
Wei Jiac2636032018-02-01 09:15:25 -080034struct DataSourceDesc;
Wei Jia53692fa2017-12-11 10:33:46 -080035struct MediaClock;
36struct MediaHTTPService;
37class MetaData;
38struct NuPlayer2Driver;
39
40struct NuPlayer2 : public AHandler {
Wei Jia003fdb52018-02-06 14:44:32 -080041 explicit NuPlayer2(pid_t pid, uid_t uid, const sp<MediaClock> &mediaClock);
Wei Jia53692fa2017-12-11 10:33:46 -080042
43 void setDriver(const wp<NuPlayer2Driver> &driver);
44
Wei Jiac2636032018-02-01 09:15:25 -080045 void setDataSourceAsync(const sp<DataSourceDesc> &dsd);
Wei Jia72bf2a02018-02-06 15:29:23 -080046 void prepareNextDataSourceAsync(const sp<DataSourceDesc> &dsd);
Wei Jia57aeffd2018-02-15 16:01:14 -080047 void playNextDataSource(int64_t srcId);
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;
Wei Jia2409c872018-02-02 10:34:33 -0800109 struct GenericSource2;
110 struct HTTPLiveSource2;
Wei Jia53692fa2017-12-11 10:33:46 -0800111 struct Renderer;
Wei Jia2409c872018-02-02 10:34:33 -0800112 struct RTSPSource2;
Wei Jia53692fa2017-12-11 10:33:46 -0800113 struct Action;
114 struct SeekAction;
115 struct SetSurfaceAction;
116 struct ResumeDecoderAction;
117 struct FlushDecoderAction;
118 struct PostMessageAction;
119 struct SimpleAction;
120
121 enum {
122 kWhatSetDataSource = '=DaS',
123 kWhatPrepare = 'prep',
Wei Jia72bf2a02018-02-06 15:29:23 -0800124 kWhatPrepareNextDataSource = 'pNDS',
Wei Jia57aeffd2018-02-15 16:01:14 -0800125 kWhatPlayNextDataSource = 'plNS',
Wei Jia53692fa2017-12-11 10:33:46 -0800126 kWhatSetVideoSurface = '=VSu',
127 kWhatSetAudioSink = '=AuS',
128 kWhatMoreDataQueued = 'more',
129 kWhatConfigPlayback = 'cfPB',
130 kWhatConfigSync = 'cfSy',
131 kWhatGetPlaybackSettings = 'gPbS',
132 kWhatGetSyncSettings = 'gSyS',
133 kWhatStart = 'strt',
134 kWhatScanSources = 'scan',
135 kWhatVideoNotify = 'vidN',
136 kWhatAudioNotify = 'audN',
137 kWhatClosedCaptionNotify = 'capN',
138 kWhatRendererNotify = 'renN',
139 kWhatReset = 'rset',
140 kWhatNotifyTime = 'nfyT',
141 kWhatSeek = 'seek',
142 kWhatPause = 'paus',
143 kWhatResume = 'rsme',
144 kWhatPollDuration = 'polD',
145 kWhatSourceNotify = 'srcN',
146 kWhatGetTrackInfo = 'gTrI',
147 kWhatGetSelectedTrack = 'gSel',
148 kWhatSelectTrack = 'selT',
149 kWhatGetBufferingSettings = 'gBus',
150 kWhatSetBufferingSettings = 'sBuS',
151 kWhatPrepareDrm = 'pDrm',
152 kWhatReleaseDrm = 'rDrm',
153 };
154
155 wp<NuPlayer2Driver> mDriver;
Wei Jia53692fa2017-12-11 10:33:46 -0800156 pid_t mPID;
Wei Jia003fdb52018-02-06 14:44:32 -0800157 uid_t mUID;
Wei Jia53692fa2017-12-11 10:33:46 -0800158 const sp<MediaClock> mMediaClock;
159 Mutex mSourceLock; // guard |mSource|.
160 sp<Source> mSource;
Wei Jia72bf2a02018-02-06 15:29:23 -0800161 int64_t mSrcId;
Wei Jia53692fa2017-12-11 10:33:46 -0800162 uint32_t mSourceFlags;
Wei Jia72bf2a02018-02-06 15:29:23 -0800163 sp<Source> mNextSource;
164 int64_t mNextSrcId;
165 uint32_t mNextSourceFlags;
Wei Jia28288fb2017-12-15 13:45:29 -0800166 sp<ANativeWindowWrapper> mNativeWindow;
Wei Jia33abcc72018-01-30 09:47:38 -0800167 sp<MediaPlayer2Interface::AudioSink> mAudioSink;
Wei Jia53692fa2017-12-11 10:33:46 -0800168 sp<DecoderBase> mVideoDecoder;
169 bool mOffloadAudio;
170 sp<DecoderBase> mAudioDecoder;
171 sp<CCDecoder> mCCDecoder;
172 sp<Renderer> mRenderer;
173 sp<ALooper> mRendererLooper;
174 int32_t mAudioDecoderGeneration;
175 int32_t mVideoDecoderGeneration;
176 int32_t mRendererGeneration;
177
178 Mutex mPlayingTimeLock;
179 int64_t mLastStartedPlayingTimeNs;
180 void stopPlaybackTimer(const char *where);
181 void startPlaybackTimer(const char *where);
182
183 int64_t mLastStartedRebufferingTimeNs;
184 void startRebufferingTimer();
185 void stopRebufferingTimer(bool exitingPlayback);
186
187 int64_t mPreviousSeekTimeUs;
188
189 List<sp<Action> > mDeferredActions;
190
191 bool mAudioEOS;
192 bool mVideoEOS;
193
194 bool mScanSourcesPending;
195 int32_t mScanSourcesGeneration;
196
197 int32_t mPollDurationGeneration;
198 int32_t mTimedTextGeneration;
199
200 enum FlushStatus {
201 NONE,
202 FLUSHING_DECODER,
203 FLUSHING_DECODER_SHUTDOWN,
204 SHUTTING_DOWN_DECODER,
205 FLUSHED,
206 SHUT_DOWN,
207 };
208
209 enum FlushCommand {
210 FLUSH_CMD_NONE,
211 FLUSH_CMD_FLUSH,
212 FLUSH_CMD_SHUTDOWN,
213 };
214
215 // Status of flush responses from the decoder and renderer.
216 bool mFlushComplete[2][2];
217
218 FlushStatus mFlushingAudio;
219 FlushStatus mFlushingVideo;
220
221 // Status of flush responses from the decoder and renderer.
222 bool mResumePending;
223
224 int32_t mVideoScalingMode;
225
226 AudioPlaybackRate mPlaybackSettings;
227 AVSyncSettings mSyncSettings;
228 float mVideoFpsHint;
229 bool mStarted;
230 bool mPrepared;
231 bool mResetting;
232 bool mSourceStarted;
233 bool mAudioDecoderError;
234 bool mVideoDecoderError;
235
236 // Actual pause state, either as requested by client or due to buffering.
237 bool mPaused;
238
239 // Pause state as requested by client. Note that if mPausedByClient is
240 // true, mPaused is always true; if mPausedByClient is false, mPaused could
241 // still become true, when we pause internally due to buffering.
242 bool mPausedByClient;
243
244 // Pause state as requested by source (internally) due to buffering
245 bool mPausedForBuffering;
246
247 // Modular DRM
248 sp<AMediaCryptoWrapper> mCrypto;
249 bool mIsDrmProtected;
250
251 typedef enum {
252 DATA_SOURCE_TYPE_NONE,
253 DATA_SOURCE_TYPE_HTTP_LIVE,
254 DATA_SOURCE_TYPE_RTSP,
255 DATA_SOURCE_TYPE_GENERIC_URL,
256 DATA_SOURCE_TYPE_GENERIC_FD,
257 DATA_SOURCE_TYPE_MEDIA,
Wei Jia53692fa2017-12-11 10:33:46 -0800258 } DATA_SOURCE_TYPE;
259
260 std::atomic<DATA_SOURCE_TYPE> mDataSourceType;
Wei Jia72bf2a02018-02-06 15:29:23 -0800261 std::atomic<DATA_SOURCE_TYPE> mNextDataSourceType;
Wei Jia53692fa2017-12-11 10:33:46 -0800262
263 inline const sp<DecoderBase> &getDecoder(bool audio) {
264 return audio ? mAudioDecoder : mVideoDecoder;
265 }
266
267 inline void clearFlushComplete() {
268 mFlushComplete[0][0] = false;
269 mFlushComplete[0][1] = false;
270 mFlushComplete[1][0] = false;
271 mFlushComplete[1][1] = false;
272 }
273
Wei Jia57aeffd2018-02-15 16:01:14 -0800274 void disconnectSource();
275
Wei Jia72bf2a02018-02-06 15:29:23 -0800276 status_t createNuPlayer2Source(const sp<DataSourceDesc> &dsd,
277 sp<Source> *source,
278 DATA_SOURCE_TYPE *dataSourceType);
279
Wei Jia53692fa2017-12-11 10:33:46 -0800280 void tryOpenAudioSinkForOffload(
281 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo);
282 void closeAudioSink();
283 void restartAudio(
284 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder);
285 void determineAudioModeChange(const sp<AMessage> &audioFormat);
286
287 status_t instantiateDecoder(
288 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true);
289
Wei Jia53692fa2017-12-11 10:33:46 -0800290 void updateVideoSize(
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800291 int64_t srcId,
Wei Jia53692fa2017-12-11 10:33:46 -0800292 const sp<AMessage> &inputFormat,
293 const sp<AMessage> &outputFormat = NULL);
294
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800295 void notifyListener(int64_t srcId, int msg, int ext1, int ext2, const Parcel *in = NULL);
Wei Jia53692fa2017-12-11 10:33:46 -0800296
297 void handleFlushComplete(bool audio, bool isDecoder);
298 void finishFlushIfPossible();
299
Wei Jia083e9092018-02-12 11:46:04 -0800300 void onStart();
Wei Jia53692fa2017-12-11 10:33:46 -0800301 void onResume();
302 void onPause();
303
304 bool audioDecoderStillNeeded();
305
306 void flushDecoder(bool audio, bool needShutdown);
307
308 void finishResume();
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800309 void notifyDriverSeekComplete(int64_t srcId);
Wei Jia53692fa2017-12-11 10:33:46 -0800310
311 void postScanSources();
312
313 void schedulePollDuration();
314 void cancelPollDuration();
315
316 void processDeferredActions();
317
318 void performSeek(int64_t seekTimeUs, MediaPlayer2SeekMode mode);
319 void performDecoderFlush(FlushCommand audio, FlushCommand video);
320 void performReset();
Wei Jia57aeffd2018-02-15 16:01:14 -0800321 void performPlayNextDataSource();
Wei Jia53692fa2017-12-11 10:33:46 -0800322 void performScanSources();
Wei Jia28288fb2017-12-15 13:45:29 -0800323 void performSetSurface(const sp<ANativeWindowWrapper> &nw);
Wei Jia53692fa2017-12-11 10:33:46 -0800324 void performResumeDecoders(bool needNotify);
325
326 void onSourceNotify(const sp<AMessage> &msg);
327 void onClosedCaptionNotify(const sp<AMessage> &msg);
328
329 void queueDecoderShutdown(
330 bool audio, bool video, const sp<AMessage> &reply);
331
332 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
333 void sendTimedMetaData(const sp<ABuffer> &buffer);
334 void sendTimedTextData(const sp<ABuffer> &buffer);
335
336 void writeTrackInfo(Parcel* reply, const sp<AMessage>& format) const;
337
338 status_t onPrepareDrm(const sp<AMessage> &msg);
339 status_t onReleaseDrm();
340
341 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer2);
342};
343
344} // namespace android
345
346#endif // NU_PLAYER2_H_