blob: e884b92b7e3e7c195ce95dd7b20d92436733c157 [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 Jia53692fa2017-12-11 10:33:46 -080047
48 status_t getBufferingSettings(BufferingSettings* buffering /* nonnull */);
49 status_t setBufferingSettings(const BufferingSettings& buffering);
50
51 void prepareAsync();
52
Wei Jia28288fb2017-12-15 13:45:29 -080053 void setVideoSurfaceTextureAsync(const sp<ANativeWindowWrapper> &nww);
Wei Jia53692fa2017-12-11 10:33:46 -080054
Wei Jia33abcc72018-01-30 09:47:38 -080055 void setAudioSink(const sp<MediaPlayer2Interface::AudioSink> &sink);
Wei Jia53692fa2017-12-11 10:33:46 -080056 status_t setPlaybackSettings(const AudioPlaybackRate &rate);
57 status_t getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */);
58 status_t setSyncSettings(const AVSyncSettings &sync, float videoFpsHint);
59 status_t getSyncSettings(AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */);
60
61 void start();
62
63 void pause();
64
65 // Will notify the driver through "notifyResetComplete" once finished.
66 void resetAsync();
67
68 // Request a notification when specified media time is reached.
69 status_t notifyAt(int64_t mediaTimeUs);
70
71 // Will notify the driver through "notifySeekComplete" once finished
72 // and needNotify is true.
73 void seekToAsync(
74 int64_t seekTimeUs,
75 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC,
76 bool needNotify = false);
77
78 status_t setVideoScalingMode(int32_t mode);
79 status_t getTrackInfo(Parcel* reply) const;
80 status_t getSelectedTrack(int32_t type, Parcel* reply) const;
81 status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs);
82 status_t getCurrentPosition(int64_t *mediaUs);
83 void getStats(Vector<sp<AMessage> > *mTrackStats);
84
85 sp<MetaData> getFileMeta();
86 float getFrameRate();
87
88 // Modular DRM
89 status_t prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId);
90 status_t releaseDrm();
91
92 const char *getDataSourceType();
93
94protected:
95 virtual ~NuPlayer2();
96
97 virtual void onMessageReceived(const sp<AMessage> &msg);
98
99public:
100 struct StreamListener;
101 struct Source;
102
103private:
104 struct Decoder;
105 struct DecoderBase;
106 struct DecoderPassThrough;
107 struct CCDecoder;
Wei Jia2409c872018-02-02 10:34:33 -0800108 struct GenericSource2;
109 struct HTTPLiveSource2;
Wei Jia53692fa2017-12-11 10:33:46 -0800110 struct Renderer;
Wei Jia2409c872018-02-02 10:34:33 -0800111 struct RTSPSource2;
Wei Jia53692fa2017-12-11 10:33:46 -0800112 struct Action;
113 struct SeekAction;
114 struct SetSurfaceAction;
115 struct ResumeDecoderAction;
116 struct FlushDecoderAction;
117 struct PostMessageAction;
118 struct SimpleAction;
119
120 enum {
121 kWhatSetDataSource = '=DaS',
122 kWhatPrepare = 'prep',
Wei Jia72bf2a02018-02-06 15:29:23 -0800123 kWhatPrepareNextDataSource = 'pNDS',
Wei Jia53692fa2017-12-11 10:33:46 -0800124 kWhatSetVideoSurface = '=VSu',
125 kWhatSetAudioSink = '=AuS',
126 kWhatMoreDataQueued = 'more',
127 kWhatConfigPlayback = 'cfPB',
128 kWhatConfigSync = 'cfSy',
129 kWhatGetPlaybackSettings = 'gPbS',
130 kWhatGetSyncSettings = 'gSyS',
131 kWhatStart = 'strt',
132 kWhatScanSources = 'scan',
133 kWhatVideoNotify = 'vidN',
134 kWhatAudioNotify = 'audN',
135 kWhatClosedCaptionNotify = 'capN',
136 kWhatRendererNotify = 'renN',
137 kWhatReset = 'rset',
138 kWhatNotifyTime = 'nfyT',
139 kWhatSeek = 'seek',
140 kWhatPause = 'paus',
141 kWhatResume = 'rsme',
142 kWhatPollDuration = 'polD',
143 kWhatSourceNotify = 'srcN',
144 kWhatGetTrackInfo = 'gTrI',
145 kWhatGetSelectedTrack = 'gSel',
146 kWhatSelectTrack = 'selT',
147 kWhatGetBufferingSettings = 'gBus',
148 kWhatSetBufferingSettings = 'sBuS',
149 kWhatPrepareDrm = 'pDrm',
150 kWhatReleaseDrm = 'rDrm',
151 };
152
153 wp<NuPlayer2Driver> mDriver;
Wei Jia53692fa2017-12-11 10:33:46 -0800154 pid_t mPID;
Wei Jia003fdb52018-02-06 14:44:32 -0800155 uid_t mUID;
Wei Jia53692fa2017-12-11 10:33:46 -0800156 const sp<MediaClock> mMediaClock;
157 Mutex mSourceLock; // guard |mSource|.
158 sp<Source> mSource;
Wei Jia72bf2a02018-02-06 15:29:23 -0800159 int64_t mSrcId;
Wei Jia53692fa2017-12-11 10:33:46 -0800160 uint32_t mSourceFlags;
Wei Jia72bf2a02018-02-06 15:29:23 -0800161 sp<Source> mNextSource;
162 int64_t mNextSrcId;
163 uint32_t mNextSourceFlags;
Wei Jia28288fb2017-12-15 13:45:29 -0800164 sp<ANativeWindowWrapper> mNativeWindow;
Wei Jia33abcc72018-01-30 09:47:38 -0800165 sp<MediaPlayer2Interface::AudioSink> mAudioSink;
Wei Jia53692fa2017-12-11 10:33:46 -0800166 sp<DecoderBase> mVideoDecoder;
167 bool mOffloadAudio;
168 sp<DecoderBase> mAudioDecoder;
169 sp<CCDecoder> mCCDecoder;
170 sp<Renderer> mRenderer;
171 sp<ALooper> mRendererLooper;
172 int32_t mAudioDecoderGeneration;
173 int32_t mVideoDecoderGeneration;
174 int32_t mRendererGeneration;
175
176 Mutex mPlayingTimeLock;
177 int64_t mLastStartedPlayingTimeNs;
178 void stopPlaybackTimer(const char *where);
179 void startPlaybackTimer(const char *where);
180
181 int64_t mLastStartedRebufferingTimeNs;
182 void startRebufferingTimer();
183 void stopRebufferingTimer(bool exitingPlayback);
184
185 int64_t mPreviousSeekTimeUs;
186
187 List<sp<Action> > mDeferredActions;
188
189 bool mAudioEOS;
190 bool mVideoEOS;
191
192 bool mScanSourcesPending;
193 int32_t mScanSourcesGeneration;
194
195 int32_t mPollDurationGeneration;
196 int32_t mTimedTextGeneration;
197
198 enum FlushStatus {
199 NONE,
200 FLUSHING_DECODER,
201 FLUSHING_DECODER_SHUTDOWN,
202 SHUTTING_DOWN_DECODER,
203 FLUSHED,
204 SHUT_DOWN,
205 };
206
207 enum FlushCommand {
208 FLUSH_CMD_NONE,
209 FLUSH_CMD_FLUSH,
210 FLUSH_CMD_SHUTDOWN,
211 };
212
213 // Status of flush responses from the decoder and renderer.
214 bool mFlushComplete[2][2];
215
216 FlushStatus mFlushingAudio;
217 FlushStatus mFlushingVideo;
218
219 // Status of flush responses from the decoder and renderer.
220 bool mResumePending;
221
222 int32_t mVideoScalingMode;
223
224 AudioPlaybackRate mPlaybackSettings;
225 AVSyncSettings mSyncSettings;
226 float mVideoFpsHint;
227 bool mStarted;
228 bool mPrepared;
229 bool mResetting;
230 bool mSourceStarted;
231 bool mAudioDecoderError;
232 bool mVideoDecoderError;
233
234 // Actual pause state, either as requested by client or due to buffering.
235 bool mPaused;
236
237 // Pause state as requested by client. Note that if mPausedByClient is
238 // true, mPaused is always true; if mPausedByClient is false, mPaused could
239 // still become true, when we pause internally due to buffering.
240 bool mPausedByClient;
241
242 // Pause state as requested by source (internally) due to buffering
243 bool mPausedForBuffering;
244
245 // Modular DRM
246 sp<AMediaCryptoWrapper> mCrypto;
247 bool mIsDrmProtected;
248
249 typedef enum {
250 DATA_SOURCE_TYPE_NONE,
251 DATA_SOURCE_TYPE_HTTP_LIVE,
252 DATA_SOURCE_TYPE_RTSP,
253 DATA_SOURCE_TYPE_GENERIC_URL,
254 DATA_SOURCE_TYPE_GENERIC_FD,
255 DATA_SOURCE_TYPE_MEDIA,
Wei Jia53692fa2017-12-11 10:33:46 -0800256 } DATA_SOURCE_TYPE;
257
258 std::atomic<DATA_SOURCE_TYPE> mDataSourceType;
Wei Jia72bf2a02018-02-06 15:29:23 -0800259 std::atomic<DATA_SOURCE_TYPE> mNextDataSourceType;
Wei Jia53692fa2017-12-11 10:33:46 -0800260
261 inline const sp<DecoderBase> &getDecoder(bool audio) {
262 return audio ? mAudioDecoder : mVideoDecoder;
263 }
264
265 inline void clearFlushComplete() {
266 mFlushComplete[0][0] = false;
267 mFlushComplete[0][1] = false;
268 mFlushComplete[1][0] = false;
269 mFlushComplete[1][1] = false;
270 }
271
Wei Jia72bf2a02018-02-06 15:29:23 -0800272 status_t createNuPlayer2Source(const sp<DataSourceDesc> &dsd,
273 sp<Source> *source,
274 DATA_SOURCE_TYPE *dataSourceType);
275
Wei Jia53692fa2017-12-11 10:33:46 -0800276 void tryOpenAudioSinkForOffload(
277 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo);
278 void closeAudioSink();
279 void restartAudio(
280 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder);
281 void determineAudioModeChange(const sp<AMessage> &audioFormat);
282
283 status_t instantiateDecoder(
284 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true);
285
Wei Jia53692fa2017-12-11 10:33:46 -0800286 void updateVideoSize(
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800287 int64_t srcId,
Wei Jia53692fa2017-12-11 10:33:46 -0800288 const sp<AMessage> &inputFormat,
289 const sp<AMessage> &outputFormat = NULL);
290
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800291 void notifyListener(int64_t srcId, int msg, int ext1, int ext2, const Parcel *in = NULL);
Wei Jia53692fa2017-12-11 10:33:46 -0800292
293 void handleFlushComplete(bool audio, bool isDecoder);
294 void finishFlushIfPossible();
295
296 void onStart(
297 int64_t startPositionUs = -1,
298 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC);
299 void onResume();
300 void onPause();
301
302 bool audioDecoderStillNeeded();
303
304 void flushDecoder(bool audio, bool needShutdown);
305
306 void finishResume();
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800307 void notifyDriverSeekComplete(int64_t srcId);
Wei Jia53692fa2017-12-11 10:33:46 -0800308
309 void postScanSources();
310
311 void schedulePollDuration();
312 void cancelPollDuration();
313
314 void processDeferredActions();
315
316 void performSeek(int64_t seekTimeUs, MediaPlayer2SeekMode mode);
317 void performDecoderFlush(FlushCommand audio, FlushCommand video);
318 void performReset();
319 void performScanSources();
Wei Jia28288fb2017-12-15 13:45:29 -0800320 void performSetSurface(const sp<ANativeWindowWrapper> &nw);
Wei Jia53692fa2017-12-11 10:33:46 -0800321 void performResumeDecoders(bool needNotify);
322
323 void onSourceNotify(const sp<AMessage> &msg);
324 void onClosedCaptionNotify(const sp<AMessage> &msg);
325
326 void queueDecoderShutdown(
327 bool audio, bool video, const sp<AMessage> &reply);
328
329 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
330 void sendTimedMetaData(const sp<ABuffer> &buffer);
331 void sendTimedTextData(const sp<ABuffer> &buffer);
332
333 void writeTrackInfo(Parcel* reply, const sp<AMessage>& format) const;
334
335 status_t onPrepareDrm(const sp<AMessage> &msg);
336 status_t onReleaseDrm();
337
338 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer2);
339};
340
341} // namespace android
342
343#endif // NU_PLAYER2_H_