blob: e55cdbe30f26be4d4c1d951a12f05af3952f11d4 [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
159 wp<NuPlayer2Driver> mDriver;
Wei Jia53692fa2017-12-11 10:33:46 -0800160 pid_t mPID;
Wei Jia003fdb52018-02-06 14:44:32 -0800161 uid_t mUID;
Wei Jia53692fa2017-12-11 10:33:46 -0800162 const sp<MediaClock> mMediaClock;
163 Mutex mSourceLock; // guard |mSource|.
164 sp<Source> mSource;
Wei Jia72bf2a02018-02-06 15:29:23 -0800165 int64_t mSrcId;
Wei Jia53692fa2017-12-11 10:33:46 -0800166 uint32_t mSourceFlags;
Wei Jia72bf2a02018-02-06 15:29:23 -0800167 sp<Source> mNextSource;
168 int64_t mNextSrcId;
169 uint32_t mNextSourceFlags;
Wei Jia28288fb2017-12-15 13:45:29 -0800170 sp<ANativeWindowWrapper> mNativeWindow;
Wei Jia33abcc72018-01-30 09:47:38 -0800171 sp<MediaPlayer2Interface::AudioSink> mAudioSink;
Wei Jia53692fa2017-12-11 10:33:46 -0800172 sp<DecoderBase> mVideoDecoder;
173 bool mOffloadAudio;
174 sp<DecoderBase> mAudioDecoder;
175 sp<CCDecoder> mCCDecoder;
176 sp<Renderer> mRenderer;
177 sp<ALooper> mRendererLooper;
178 int32_t mAudioDecoderGeneration;
179 int32_t mVideoDecoderGeneration;
180 int32_t mRendererGeneration;
181
182 Mutex mPlayingTimeLock;
183 int64_t mLastStartedPlayingTimeNs;
184 void stopPlaybackTimer(const char *where);
185 void startPlaybackTimer(const char *where);
186
187 int64_t mLastStartedRebufferingTimeNs;
188 void startRebufferingTimer();
189 void stopRebufferingTimer(bool exitingPlayback);
190
191 int64_t mPreviousSeekTimeUs;
192
193 List<sp<Action> > mDeferredActions;
194
195 bool mAudioEOS;
196 bool mVideoEOS;
197
198 bool mScanSourcesPending;
199 int32_t mScanSourcesGeneration;
200
201 int32_t mPollDurationGeneration;
202 int32_t mTimedTextGeneration;
203
204 enum FlushStatus {
205 NONE,
206 FLUSHING_DECODER,
207 FLUSHING_DECODER_SHUTDOWN,
208 SHUTTING_DOWN_DECODER,
209 FLUSHED,
210 SHUT_DOWN,
211 };
212
213 enum FlushCommand {
214 FLUSH_CMD_NONE,
215 FLUSH_CMD_FLUSH,
216 FLUSH_CMD_SHUTDOWN,
217 };
218
219 // Status of flush responses from the decoder and renderer.
220 bool mFlushComplete[2][2];
221
222 FlushStatus mFlushingAudio;
223 FlushStatus mFlushingVideo;
224
225 // Status of flush responses from the decoder and renderer.
226 bool mResumePending;
227
228 int32_t mVideoScalingMode;
229
230 AudioPlaybackRate mPlaybackSettings;
231 AVSyncSettings mSyncSettings;
232 float mVideoFpsHint;
233 bool mStarted;
234 bool mPrepared;
235 bool mResetting;
236 bool mSourceStarted;
237 bool mAudioDecoderError;
238 bool mVideoDecoderError;
239
240 // Actual pause state, either as requested by client or due to buffering.
241 bool mPaused;
242
243 // Pause state as requested by client. Note that if mPausedByClient is
244 // true, mPaused is always true; if mPausedByClient is false, mPaused could
245 // still become true, when we pause internally due to buffering.
246 bool mPausedByClient;
247
248 // Pause state as requested by source (internally) due to buffering
249 bool mPausedForBuffering;
250
251 // Modular DRM
252 sp<AMediaCryptoWrapper> mCrypto;
253 bool mIsDrmProtected;
254
255 typedef enum {
256 DATA_SOURCE_TYPE_NONE,
257 DATA_SOURCE_TYPE_HTTP_LIVE,
258 DATA_SOURCE_TYPE_RTSP,
259 DATA_SOURCE_TYPE_GENERIC_URL,
260 DATA_SOURCE_TYPE_GENERIC_FD,
261 DATA_SOURCE_TYPE_MEDIA,
Wei Jia53692fa2017-12-11 10:33:46 -0800262 } DATA_SOURCE_TYPE;
263
264 std::atomic<DATA_SOURCE_TYPE> mDataSourceType;
Wei Jia72bf2a02018-02-06 15:29:23 -0800265 std::atomic<DATA_SOURCE_TYPE> mNextDataSourceType;
Wei Jia53692fa2017-12-11 10:33:46 -0800266
267 inline const sp<DecoderBase> &getDecoder(bool audio) {
268 return audio ? mAudioDecoder : mVideoDecoder;
269 }
270
271 inline void clearFlushComplete() {
272 mFlushComplete[0][0] = false;
273 mFlushComplete[0][1] = false;
274 mFlushComplete[1][0] = false;
275 mFlushComplete[1][1] = false;
276 }
277
Wei Jia57aeffd2018-02-15 16:01:14 -0800278 void disconnectSource();
279
Wei Jia72bf2a02018-02-06 15:29:23 -0800280 status_t createNuPlayer2Source(const sp<DataSourceDesc> &dsd,
281 sp<Source> *source,
282 DATA_SOURCE_TYPE *dataSourceType);
283
Wei Jia53692fa2017-12-11 10:33:46 -0800284 void tryOpenAudioSinkForOffload(
285 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo);
286 void closeAudioSink();
287 void restartAudio(
288 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder);
289 void determineAudioModeChange(const sp<AMessage> &audioFormat);
290
291 status_t instantiateDecoder(
292 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true);
293
Wei Jia53692fa2017-12-11 10:33:46 -0800294 void updateVideoSize(
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800295 int64_t srcId,
Wei Jia53692fa2017-12-11 10:33:46 -0800296 const sp<AMessage> &inputFormat,
297 const sp<AMessage> &outputFormat = NULL);
298
Dongwon Kang41929fb2018-09-09 08:29:56 -0700299 void notifyListener(int64_t srcId, int msg, int ext1, int ext2, const PlayerMessage *in = NULL);
Wei Jia53692fa2017-12-11 10:33:46 -0800300
301 void handleFlushComplete(bool audio, bool isDecoder);
302 void finishFlushIfPossible();
303
Wei Jia6376cd52018-09-26 11:42:55 -0700304 void onStart(bool play);
Wei Jia53692fa2017-12-11 10:33:46 -0800305 void onResume();
306 void onPause();
307
308 bool audioDecoderStillNeeded();
309
310 void flushDecoder(bool audio, bool needShutdown);
311
312 void finishResume();
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800313 void notifyDriverSeekComplete(int64_t srcId);
Wei Jia53692fa2017-12-11 10:33:46 -0800314
315 void postScanSources();
316
317 void schedulePollDuration();
318 void cancelPollDuration();
319
320 void processDeferredActions();
321
322 void performSeek(int64_t seekTimeUs, MediaPlayer2SeekMode mode);
323 void performDecoderFlush(FlushCommand audio, FlushCommand video);
324 void performReset();
Wei Jia57aeffd2018-02-15 16:01:14 -0800325 void performPlayNextDataSource();
Wei Jia53692fa2017-12-11 10:33:46 -0800326 void performScanSources();
Wei Jia28288fb2017-12-15 13:45:29 -0800327 void performSetSurface(const sp<ANativeWindowWrapper> &nw);
Wei Jia53692fa2017-12-11 10:33:46 -0800328 void performResumeDecoders(bool needNotify);
329
330 void onSourceNotify(const sp<AMessage> &msg);
331 void onClosedCaptionNotify(const sp<AMessage> &msg);
332
333 void queueDecoderShutdown(
334 bool audio, bool video, const sp<AMessage> &reply);
335
336 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
337 void sendTimedMetaData(const sp<ABuffer> &buffer);
338 void sendTimedTextData(const sp<ABuffer> &buffer);
339
Dongwon Kang9f631982018-07-10 12:34:41 -0700340 void writeTrackInfo(PlayerMessage* reply, const sp<AMessage>& format) const;
Wei Jia53692fa2017-12-11 10:33:46 -0800341
342 status_t onPrepareDrm(const sp<AMessage> &msg);
343 status_t onReleaseDrm();
344
345 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer2);
346};
347
348} // namespace android
349
350#endif // NU_PLAYER2_H_