blob: c69835fca2b08b8f475e4168ed0d82b52ffaa2b4 [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
2 * Copyright (C) 2010 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_PLAYER_H_
18
19#define NU_PLAYER_H_
20
Lajos Molnar3a474aa2015-04-24 17:10:07 -070021#include <media/AudioResamplerPublic.h>
Hassan Shojaniacefac142017-02-06 21:02:02 -080022#include <media/ICrypto.h>
Andreas Huberf9334412010-12-15 15:17:42 -080023#include <media/MediaPlayerInterface.h>
24#include <media/stagefright/foundation/AHandler.h>
25
Andreas Huberf9334412010-12-15 15:17:42 -080026namespace android {
27
Chong Zhang404fced2014-06-11 14:45:31 -070028struct ABuffer;
Lajos Molnar87603c02014-08-20 19:25:30 -070029struct AMessage;
Lajos Molnar3a474aa2015-04-24 17:10:07 -070030struct AudioPlaybackRate;
31struct AVSyncSettings;
Lajos Molnaree4e1b12015-04-17 13:46:19 -070032class IDataSource;
Bernhard Rosenkränzer86a58ca2014-11-17 20:14:08 +010033class MetaData;
Andreas Huber43c3e6c2011-01-05 12:17:08 -080034struct NuPlayerDriver;
Andreas Huberf9334412010-12-15 15:17:42 -080035
36struct NuPlayer : public AHandler {
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -070037 explicit NuPlayer(pid_t pid);
Andreas Huberf9334412010-12-15 15:17:42 -080038
Andreas Huber9b80c2b2011-06-30 15:47:02 -070039 void setUID(uid_t uid);
40
Andreas Huber43c3e6c2011-01-05 12:17:08 -080041 void setDriver(const wp<NuPlayerDriver> &driver);
Andreas Huberf9334412010-12-15 15:17:42 -080042
Andreas Huber9575c962013-02-05 13:59:56 -080043 void setDataSourceAsync(const sp<IStreamSource> &source);
Andreas Huber5bc087c2010-12-23 10:27:40 -080044
Andreas Huber9575c962013-02-05 13:59:56 -080045 void setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -080046 const sp<IMediaHTTPService> &httpService,
47 const char *url,
48 const KeyedVector<String8, String8> *headers);
Andreas Huber5bc087c2010-12-23 10:27:40 -080049
Andreas Huber9575c962013-02-05 13:59:56 -080050 void setDataSourceAsync(int fd, int64_t offset, int64_t length);
51
Chris Watkins99f31602015-03-20 13:06:33 -070052 void setDataSourceAsync(const sp<DataSource> &source);
53
Wei Jia48fa06d2016-12-20 15:30:49 -080054 status_t getDefaultBufferingSettings(BufferingSettings* buffering /* nonnull */);
55 status_t setBufferingSettings(const BufferingSettings& buffering);
56
Andreas Huber9575c962013-02-05 13:59:56 -080057 void prepareAsync();
Andreas Huberafed0e12011-09-20 15:39:58 -070058
Andreas Huber57a339c2012-12-03 11:18:00 -080059 void setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -080060 const sp<IGraphicBufferProducer> &bufferProducer);
Andreas Huber57a339c2012-12-03 11:18:00 -080061
Andreas Huberf9334412010-12-15 15:17:42 -080062 void setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink);
Lajos Molnar3a474aa2015-04-24 17:10:07 -070063 status_t setPlaybackSettings(const AudioPlaybackRate &rate);
64 status_t getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */);
65 status_t setSyncSettings(const AVSyncSettings &sync, float videoFpsHint);
66 status_t getSyncSettings(AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */);
67
Andreas Huberf9334412010-12-15 15:17:42 -080068 void start();
69
Andreas Huber43c3e6c2011-01-05 12:17:08 -080070 void pause();
Andreas Huber43c3e6c2011-01-05 12:17:08 -080071
72 // Will notify the driver through "notifyResetComplete" once finished.
Andreas Huber1aef2112011-01-04 14:01:29 -080073 void resetAsync();
74
Wei Jiae427abf2014-09-22 15:21:11 -070075 // Will notify the driver through "notifySeekComplete" once finished
76 // and needNotify is true.
Wei Jiac5de0912016-11-18 10:22:14 -080077 void seekToAsync(
78 int64_t seekTimeUs,
79 MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC,
80 bool needNotify = false);
Andreas Huber43c3e6c2011-01-05 12:17:08 -080081
James Dong0d268a32012-08-31 12:18:27 -070082 status_t setVideoScalingMode(int32_t mode);
Chong Zhangdcb89b32013-08-06 09:44:47 -070083 status_t getTrackInfo(Parcel* reply) const;
Robert Shih7c4f0d72014-07-09 18:53:31 -070084 status_t getSelectedTrack(int32_t type, Parcel* reply) const;
Robert Shih6ffb1fd2014-10-29 16:24:32 -070085 status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs);
Ronghua Wua73d9e02014-10-08 15:13:29 -070086 status_t getCurrentPosition(int64_t *mediaUs);
Praveen Chavane1e5d7a2015-05-19 19:09:48 -070087 void getStats(Vector<sp<AMessage> > *mTrackStats);
James Dong0d268a32012-08-31 12:18:27 -070088
Marco Nelissenf0b72b52014-09-16 15:43:44 -070089 sp<MetaData> getFileMeta();
Ronghua Wuc8a70d32015-04-29 16:26:34 -070090 float getFrameRate();
Marco Nelissenf0b72b52014-09-16 15:43:44 -070091
Hassan Shojaniacefac142017-02-06 21:02:02 -080092 // Modular DRM
93 status_t prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId);
94 status_t releaseDrm();
95
Hassan Shojaniaff63de72017-04-26 15:10:42 -070096 const char *getDataSourceType();
97
Andreas Huberf9334412010-12-15 15:17:42 -080098protected:
99 virtual ~NuPlayer();
100
101 virtual void onMessageReceived(const sp<AMessage> &msg);
102
Andreas Huber84066782011-08-16 09:34:26 -0700103public:
104 struct NuPlayerStreamListener;
105 struct Source;
106
Andreas Huberf9334412010-12-15 15:17:42 -0800107private:
Andreas Huberf9334412010-12-15 15:17:42 -0800108 struct Decoder;
Chong Zhang7137ec72014-11-12 16:41:05 -0800109 struct DecoderBase;
Wei Jiabc2fb722014-07-08 16:37:57 -0700110 struct DecoderPassThrough;
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700111 struct CCDecoder;
Andreas Huberafed0e12011-09-20 15:39:58 -0700112 struct GenericSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800113 struct HTTPLiveSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800114 struct Renderer;
Andreas Huberafed0e12011-09-20 15:39:58 -0700115 struct RTSPSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800116 struct StreamingSource;
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800117 struct Action;
118 struct SeekAction;
Andreas Huber57a339c2012-12-03 11:18:00 -0800119 struct SetSurfaceAction;
Chong Zhangf8d71772014-11-26 15:08:34 -0800120 struct ResumeDecoderAction;
Wei Jiafef808d2014-10-31 17:57:05 -0700121 struct FlushDecoderAction;
Andreas Huber14f76722013-01-15 09:04:18 -0800122 struct PostMessageAction;
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800123 struct SimpleAction;
Andreas Huberf9334412010-12-15 15:17:42 -0800124
125 enum {
Andreas Huber078cfcf2011-09-15 12:25:04 -0700126 kWhatSetDataSource = '=DaS',
Andreas Huber9575c962013-02-05 13:59:56 -0800127 kWhatPrepare = 'prep',
Lajos Molnar1de1e252015-04-30 18:18:34 -0700128 kWhatSetVideoSurface = '=VSu',
Andreas Huber078cfcf2011-09-15 12:25:04 -0700129 kWhatSetAudioSink = '=AuS',
130 kWhatMoreDataQueued = 'more',
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700131 kWhatConfigPlayback = 'cfPB',
132 kWhatConfigSync = 'cfSy',
133 kWhatGetPlaybackSettings = 'gPbS',
134 kWhatGetSyncSettings = 'gSyS',
Andreas Huber078cfcf2011-09-15 12:25:04 -0700135 kWhatStart = 'strt',
136 kWhatScanSources = 'scan',
137 kWhatVideoNotify = 'vidN',
138 kWhatAudioNotify = 'audN',
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700139 kWhatClosedCaptionNotify = 'capN',
Andreas Huber078cfcf2011-09-15 12:25:04 -0700140 kWhatRendererNotify = 'renN',
141 kWhatReset = 'rset',
142 kWhatSeek = 'seek',
143 kWhatPause = 'paus',
144 kWhatResume = 'rsme',
Andreas Huberb7c8e912012-11-27 15:02:53 -0800145 kWhatPollDuration = 'polD',
Andreas Huberb5f25f02013-02-05 10:14:26 -0800146 kWhatSourceNotify = 'srcN',
Chong Zhangdcb89b32013-08-06 09:44:47 -0700147 kWhatGetTrackInfo = 'gTrI',
Robert Shih7c4f0d72014-07-09 18:53:31 -0700148 kWhatGetSelectedTrack = 'gSel',
Chong Zhangdcb89b32013-08-06 09:44:47 -0700149 kWhatSelectTrack = 'selT',
Wei Jia48fa06d2016-12-20 15:30:49 -0800150 kWhatGetDefaultBufferingSettings = 'gDBS',
151 kWhatSetBufferingSettings = 'sBuS',
Hassan Shojaniacefac142017-02-06 21:02:02 -0800152 kWhatPrepareDrm = 'pDrm',
153 kWhatReleaseDrm = 'rDrm',
Andreas Huberf9334412010-12-15 15:17:42 -0800154 };
155
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800156 wp<NuPlayerDriver> mDriver;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700157 bool mUIDValid;
158 uid_t mUID;
Ronghua Wu68845c12015-07-21 09:50:48 -0700159 pid_t mPID;
Wei Jiac45a4b22016-04-15 15:30:23 -0700160 Mutex mSourceLock; // guard |mSource|.
Andreas Huber5bc087c2010-12-23 10:27:40 -0800161 sp<Source> mSource;
Andreas Huber9575c962013-02-05 13:59:56 -0800162 uint32_t mSourceFlags;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700163 sp<Surface> mSurface;
Andreas Huberf9334412010-12-15 15:17:42 -0800164 sp<MediaPlayerBase::AudioSink> mAudioSink;
Chong Zhang7137ec72014-11-12 16:41:05 -0800165 sp<DecoderBase> mVideoDecoder;
Wei Jiabc2fb722014-07-08 16:37:57 -0700166 bool mOffloadAudio;
Chong Zhang7137ec72014-11-12 16:41:05 -0800167 sp<DecoderBase> mAudioDecoder;
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700168 sp<CCDecoder> mCCDecoder;
Andreas Huberf9334412010-12-15 15:17:42 -0800169 sp<Renderer> mRenderer;
Lajos Molnar09524832014-07-17 14:29:51 -0700170 sp<ALooper> mRendererLooper;
Wei Jia88703c32014-08-06 11:24:07 -0700171 int32_t mAudioDecoderGeneration;
172 int32_t mVideoDecoderGeneration;
Wei Jia57568df2014-09-22 10:16:29 -0700173 int32_t mRendererGeneration;
Andreas Huberf9334412010-12-15 15:17:42 -0800174
Ray Essickd4d00612017-01-03 09:36:27 -0800175 int64_t mLastStartedPlayingTimeNs;
176
Robert Shih1a5c8592015-08-04 18:07:44 -0700177 int64_t mPreviousSeekTimeUs;
178
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800179 List<sp<Action> > mDeferredActions;
180
Andreas Huberf9334412010-12-15 15:17:42 -0800181 bool mAudioEOS;
182 bool mVideoEOS;
183
Andreas Huber5bc087c2010-12-23 10:27:40 -0800184 bool mScanSourcesPending;
Andreas Huber1aef2112011-01-04 14:01:29 -0800185 int32_t mScanSourcesGeneration;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800186
Andreas Huberb7c8e912012-11-27 15:02:53 -0800187 int32_t mPollDurationGeneration;
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700188 int32_t mTimedTextGeneration;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800189
Andreas Huberf9334412010-12-15 15:17:42 -0800190 enum FlushStatus {
191 NONE,
Andreas Huberf9334412010-12-15 15:17:42 -0800192 FLUSHING_DECODER,
Andreas Huber1aef2112011-01-04 14:01:29 -0800193 FLUSHING_DECODER_SHUTDOWN,
Andreas Huber3831a062010-12-21 10:22:33 -0800194 SHUTTING_DOWN_DECODER,
195 FLUSHED,
196 SHUT_DOWN,
Andreas Huberf9334412010-12-15 15:17:42 -0800197 };
198
Wei Jiafef808d2014-10-31 17:57:05 -0700199 enum FlushCommand {
200 FLUSH_CMD_NONE,
201 FLUSH_CMD_FLUSH,
202 FLUSH_CMD_SHUTDOWN,
203 };
204
Andy Hung8d121d42014-10-03 09:53:53 -0700205 // Status of flush responses from the decoder and renderer.
206 bool mFlushComplete[2][2];
207
Andreas Huberf9334412010-12-15 15:17:42 -0800208 FlushStatus mFlushingAudio;
209 FlushStatus mFlushingVideo;
210
Chong Zhangf8d71772014-11-26 15:08:34 -0800211 // Status of flush responses from the decoder and renderer.
212 bool mResumePending;
213
James Dong0d268a32012-08-31 12:18:27 -0700214 int32_t mVideoScalingMode;
215
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700216 AudioPlaybackRate mPlaybackSettings;
217 AVSyncSettings mSyncSettings;
218 float mVideoFpsHint;
Andreas Huber57a339c2012-12-03 11:18:00 -0800219 bool mStarted;
Wei Jia8a092d32016-06-03 14:57:24 -0700220 bool mPrepared;
Ronghua Wu64c2d172015-10-07 16:52:19 -0700221 bool mResetting;
Robert Shih0c61a0d2015-07-06 15:09:10 -0700222 bool mSourceStarted;
Wei Jia686e8e52017-04-03 14:08:01 -0700223 bool mAudioDecoderError;
224 bool mVideoDecoderError;
Andreas Huber57a339c2012-12-03 11:18:00 -0800225
Chong Zhangefbb6192015-01-30 17:13:27 -0800226 // Actual pause state, either as requested by client or due to buffering.
227 bool mPaused;
228
229 // Pause state as requested by client. Note that if mPausedByClient is
230 // true, mPaused is always true; if mPausedByClient is false, mPaused could
231 // still become true, when we pause internally due to buffering.
232 bool mPausedByClient;
233
Chong Zhang8a048332015-05-06 15:16:28 -0700234 // Pause state as requested by source (internally) due to buffering
235 bool mPausedForBuffering;
236
Hassan Shojaniacefac142017-02-06 21:02:02 -0800237 // Modular DRM
238 sp<ICrypto> mCrypto;
239 bool mIsDrmProtected;
240
Hassan Shojaniaff63de72017-04-26 15:10:42 -0700241 typedef enum {
242 DATA_SOURCE_TYPE_NONE,
243 DATA_SOURCE_TYPE_HTTP_LIVE,
244 DATA_SOURCE_TYPE_RTSP,
245 DATA_SOURCE_TYPE_GENERIC_URL,
246 DATA_SOURCE_TYPE_GENERIC_FD,
247 DATA_SOURCE_TYPE_MEDIA,
248 DATA_SOURCE_TYPE_STREAM,
249 } DATA_SOURCE_TYPE;
250
251 std::atomic<DATA_SOURCE_TYPE> mDataSourceType;
252
Chong Zhang7137ec72014-11-12 16:41:05 -0800253 inline const sp<DecoderBase> &getDecoder(bool audio) {
Lajos Molnar87603c02014-08-20 19:25:30 -0700254 return audio ? mAudioDecoder : mVideoDecoder;
255 }
256
Andy Hung8d121d42014-10-03 09:53:53 -0700257 inline void clearFlushComplete() {
258 mFlushComplete[0][0] = false;
259 mFlushComplete[0][1] = false;
260 mFlushComplete[1][0] = false;
261 mFlushComplete[1][1] = false;
262 }
263
Wei Jia41cd4632016-05-13 10:53:30 -0700264 void tryOpenAudioSinkForOffload(
265 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo);
Andy Hung282a7e32014-08-14 15:56:34 -0700266 void closeAudioSink();
Wei Jia5031b2f2016-02-25 11:19:31 -0800267 void restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -0700268 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder);
Wei Jia41cd4632016-05-13 10:53:30 -0700269 void determineAudioModeChange(const sp<AMessage> &audioFormat);
Andy Hung282a7e32014-08-14 15:56:34 -0700270
Wei Jiaa05f1e32016-03-25 16:31:22 -0700271 status_t instantiateDecoder(
272 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true);
Andreas Huberf9334412010-12-15 15:17:42 -0800273
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700274 status_t onInstantiateSecureDecoders();
275
Chong Zhangced1c2f2014-08-08 15:22:35 -0700276 void updateVideoSize(
277 const sp<AMessage> &inputFormat,
278 const sp<AMessage> &outputFormat = NULL);
279
Chong Zhangdcb89b32013-08-06 09:44:47 -0700280 void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
Andreas Huberf9334412010-12-15 15:17:42 -0800281
Andy Hung8d121d42014-10-03 09:53:53 -0700282 void handleFlushComplete(bool audio, bool isDecoder);
Andreas Huber3831a062010-12-21 10:22:33 -0800283 void finishFlushIfPossible();
284
Wei Jiac5de0912016-11-18 10:22:14 -0800285 void onStart(
286 int64_t startPositionUs = -1,
287 MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC);
Wei Jia94211742014-10-28 17:09:06 -0700288 void onResume();
Chong Zhangefbb6192015-01-30 17:13:27 -0800289 void onPause();
Wei Jia94211742014-10-28 17:09:06 -0700290
Ronghua Wud7988b12014-10-03 15:19:10 -0700291 bool audioDecoderStillNeeded();
292
Chong Zhang7137ec72014-11-12 16:41:05 -0800293 void flushDecoder(bool audio, bool needShutdown);
Andreas Huber1aef2112011-01-04 14:01:29 -0800294
Chong Zhangf8d71772014-11-26 15:08:34 -0800295 void finishResume();
Wei Jia1061c9c2015-05-19 16:02:17 -0700296 void notifyDriverSeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -0800297
Andreas Huber1aef2112011-01-04 14:01:29 -0800298 void postScanSources();
Andreas Huber53df1a42010-12-22 10:03:04 -0800299
Andreas Huberb7c8e912012-11-27 15:02:53 -0800300 void schedulePollDuration();
301 void cancelPollDuration();
302
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800303 void processDeferredActions();
304
Wei Jiac5de0912016-11-18 10:22:14 -0800305 void performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode);
Wei Jiafef808d2014-10-31 17:57:05 -0700306 void performDecoderFlush(FlushCommand audio, FlushCommand video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800307 void performReset();
308 void performScanSources();
Lajos Molnar1de1e252015-04-30 18:18:34 -0700309 void performSetSurface(const sp<Surface> &wrapper);
Chong Zhangf8d71772014-11-26 15:08:34 -0800310 void performResumeDecoders(bool needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800311
Andreas Huber9575c962013-02-05 13:59:56 -0800312 void onSourceNotify(const sp<AMessage> &msg);
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700313 void onClosedCaptionNotify(const sp<AMessage> &msg);
Andreas Huber9575c962013-02-05 13:59:56 -0800314
Andreas Huber14f76722013-01-15 09:04:18 -0800315 void queueDecoderShutdown(
316 bool audio, bool video, const sp<AMessage> &reply);
317
Chong Zhang404fced2014-06-11 14:45:31 -0700318 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
Robert Shih08528432015-04-08 09:06:54 -0700319 void sendTimedMetaData(const sp<ABuffer> &buffer);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700320 void sendTimedTextData(const sp<ABuffer> &buffer);
Chong Zhang404fced2014-06-11 14:45:31 -0700321
Chih-Hung Hsiehe964d4e2016-08-09 14:31:32 -0700322 void writeTrackInfo(Parcel* reply, const sp<AMessage>& format) const;
Chong Zhang404fced2014-06-11 14:45:31 -0700323
Hassan Shojaniacefac142017-02-06 21:02:02 -0800324 status_t onPrepareDrm(const sp<AMessage> &msg);
325 status_t onReleaseDrm();
326
Andreas Huberf9334412010-12-15 15:17:42 -0800327 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
328};
329
330} // namespace android
331
332#endif // NU_PLAYER_H_