blob: cdb9a4f45801202e39d524072a1a0245f312ec5b [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 -080035class IDataSource;
36struct MediaClock;
37struct MediaHTTPService;
38class MetaData;
39struct NuPlayer2Driver;
40
41struct NuPlayer2 : public AHandler {
Wei Jia003fdb52018-02-06 14:44:32 -080042 explicit NuPlayer2(pid_t pid, uid_t uid, const sp<MediaClock> &mediaClock);
Wei Jia53692fa2017-12-11 10:33:46 -080043
44 void setDriver(const wp<NuPlayer2Driver> &driver);
45
Wei Jiac2636032018-02-01 09:15:25 -080046 void setDataSourceAsync(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',
123 kWhatSetVideoSurface = '=VSu',
124 kWhatSetAudioSink = '=AuS',
125 kWhatMoreDataQueued = 'more',
126 kWhatConfigPlayback = 'cfPB',
127 kWhatConfigSync = 'cfSy',
128 kWhatGetPlaybackSettings = 'gPbS',
129 kWhatGetSyncSettings = 'gSyS',
130 kWhatStart = 'strt',
131 kWhatScanSources = 'scan',
132 kWhatVideoNotify = 'vidN',
133 kWhatAudioNotify = 'audN',
134 kWhatClosedCaptionNotify = 'capN',
135 kWhatRendererNotify = 'renN',
136 kWhatReset = 'rset',
137 kWhatNotifyTime = 'nfyT',
138 kWhatSeek = 'seek',
139 kWhatPause = 'paus',
140 kWhatResume = 'rsme',
141 kWhatPollDuration = 'polD',
142 kWhatSourceNotify = 'srcN',
143 kWhatGetTrackInfo = 'gTrI',
144 kWhatGetSelectedTrack = 'gSel',
145 kWhatSelectTrack = 'selT',
146 kWhatGetBufferingSettings = 'gBus',
147 kWhatSetBufferingSettings = 'sBuS',
148 kWhatPrepareDrm = 'pDrm',
149 kWhatReleaseDrm = 'rDrm',
150 };
151
152 wp<NuPlayer2Driver> mDriver;
Wei Jia53692fa2017-12-11 10:33:46 -0800153 pid_t mPID;
Wei Jia003fdb52018-02-06 14:44:32 -0800154 uid_t mUID;
Wei Jia53692fa2017-12-11 10:33:46 -0800155 const sp<MediaClock> mMediaClock;
156 Mutex mSourceLock; // guard |mSource|.
157 sp<Source> mSource;
158 uint32_t mSourceFlags;
Wei Jia28288fb2017-12-15 13:45:29 -0800159 sp<ANativeWindowWrapper> mNativeWindow;
Wei Jia33abcc72018-01-30 09:47:38 -0800160 sp<MediaPlayer2Interface::AudioSink> mAudioSink;
Wei Jia53692fa2017-12-11 10:33:46 -0800161 sp<DecoderBase> mVideoDecoder;
162 bool mOffloadAudio;
163 sp<DecoderBase> mAudioDecoder;
164 sp<CCDecoder> mCCDecoder;
165 sp<Renderer> mRenderer;
166 sp<ALooper> mRendererLooper;
167 int32_t mAudioDecoderGeneration;
168 int32_t mVideoDecoderGeneration;
169 int32_t mRendererGeneration;
170
171 Mutex mPlayingTimeLock;
172 int64_t mLastStartedPlayingTimeNs;
173 void stopPlaybackTimer(const char *where);
174 void startPlaybackTimer(const char *where);
175
176 int64_t mLastStartedRebufferingTimeNs;
177 void startRebufferingTimer();
178 void stopRebufferingTimer(bool exitingPlayback);
179
180 int64_t mPreviousSeekTimeUs;
181
182 List<sp<Action> > mDeferredActions;
183
184 bool mAudioEOS;
185 bool mVideoEOS;
186
187 bool mScanSourcesPending;
188 int32_t mScanSourcesGeneration;
189
190 int32_t mPollDurationGeneration;
191 int32_t mTimedTextGeneration;
192
193 enum FlushStatus {
194 NONE,
195 FLUSHING_DECODER,
196 FLUSHING_DECODER_SHUTDOWN,
197 SHUTTING_DOWN_DECODER,
198 FLUSHED,
199 SHUT_DOWN,
200 };
201
202 enum FlushCommand {
203 FLUSH_CMD_NONE,
204 FLUSH_CMD_FLUSH,
205 FLUSH_CMD_SHUTDOWN,
206 };
207
208 // Status of flush responses from the decoder and renderer.
209 bool mFlushComplete[2][2];
210
211 FlushStatus mFlushingAudio;
212 FlushStatus mFlushingVideo;
213
214 // Status of flush responses from the decoder and renderer.
215 bool mResumePending;
216
217 int32_t mVideoScalingMode;
218
219 AudioPlaybackRate mPlaybackSettings;
220 AVSyncSettings mSyncSettings;
221 float mVideoFpsHint;
222 bool mStarted;
223 bool mPrepared;
224 bool mResetting;
225 bool mSourceStarted;
226 bool mAudioDecoderError;
227 bool mVideoDecoderError;
228
229 // Actual pause state, either as requested by client or due to buffering.
230 bool mPaused;
231
232 // Pause state as requested by client. Note that if mPausedByClient is
233 // true, mPaused is always true; if mPausedByClient is false, mPaused could
234 // still become true, when we pause internally due to buffering.
235 bool mPausedByClient;
236
237 // Pause state as requested by source (internally) due to buffering
238 bool mPausedForBuffering;
239
240 // Modular DRM
241 sp<AMediaCryptoWrapper> mCrypto;
242 bool mIsDrmProtected;
243
244 typedef enum {
245 DATA_SOURCE_TYPE_NONE,
246 DATA_SOURCE_TYPE_HTTP_LIVE,
247 DATA_SOURCE_TYPE_RTSP,
248 DATA_SOURCE_TYPE_GENERIC_URL,
249 DATA_SOURCE_TYPE_GENERIC_FD,
250 DATA_SOURCE_TYPE_MEDIA,
251 DATA_SOURCE_TYPE_STREAM,
252 } DATA_SOURCE_TYPE;
253
254 std::atomic<DATA_SOURCE_TYPE> mDataSourceType;
255
256 inline const sp<DecoderBase> &getDecoder(bool audio) {
257 return audio ? mAudioDecoder : mVideoDecoder;
258 }
259
260 inline void clearFlushComplete() {
261 mFlushComplete[0][0] = false;
262 mFlushComplete[0][1] = false;
263 mFlushComplete[1][0] = false;
264 mFlushComplete[1][1] = false;
265 }
266
267 void tryOpenAudioSinkForOffload(
268 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo);
269 void closeAudioSink();
270 void restartAudio(
271 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder);
272 void determineAudioModeChange(const sp<AMessage> &audioFormat);
273
274 status_t instantiateDecoder(
275 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true);
276
277 status_t onInstantiateSecureDecoders();
278
279 void updateVideoSize(
280 const sp<AMessage> &inputFormat,
281 const sp<AMessage> &outputFormat = NULL);
282
283 void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
284
285 void handleFlushComplete(bool audio, bool isDecoder);
286 void finishFlushIfPossible();
287
288 void onStart(
289 int64_t startPositionUs = -1,
290 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC);
291 void onResume();
292 void onPause();
293
294 bool audioDecoderStillNeeded();
295
296 void flushDecoder(bool audio, bool needShutdown);
297
298 void finishResume();
299 void notifyDriverSeekComplete();
300
301 void postScanSources();
302
303 void schedulePollDuration();
304 void cancelPollDuration();
305
306 void processDeferredActions();
307
308 void performSeek(int64_t seekTimeUs, MediaPlayer2SeekMode mode);
309 void performDecoderFlush(FlushCommand audio, FlushCommand video);
310 void performReset();
311 void performScanSources();
Wei Jia28288fb2017-12-15 13:45:29 -0800312 void performSetSurface(const sp<ANativeWindowWrapper> &nw);
Wei Jia53692fa2017-12-11 10:33:46 -0800313 void performResumeDecoders(bool needNotify);
314
315 void onSourceNotify(const sp<AMessage> &msg);
316 void onClosedCaptionNotify(const sp<AMessage> &msg);
317
318 void queueDecoderShutdown(
319 bool audio, bool video, const sp<AMessage> &reply);
320
321 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
322 void sendTimedMetaData(const sp<ABuffer> &buffer);
323 void sendTimedTextData(const sp<ABuffer> &buffer);
324
325 void writeTrackInfo(Parcel* reply, const sp<AMessage>& format) const;
326
327 status_t onPrepareDrm(const sp<AMessage> &msg);
328 status_t onReleaseDrm();
329
330 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer2);
331};
332
333} // namespace android
334
335#endif // NU_PLAYER2_H_