blob: 1d74b552067f4d1bb67d47c75ed9acdba912d9c4 [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>
22#include <media/MediaPlayer2Interface.h>
23#include <media/stagefright/foundation/AHandler.h>
24
25namespace android {
26
27struct ABuffer;
28struct AMediaCryptoWrapper;
29struct AMessage;
Wei Jia28288fb2017-12-15 13:45:29 -080030struct ANativeWindowWrapper;
Wei Jia53692fa2017-12-11 10:33:46 -080031struct AudioPlaybackRate;
32struct AVSyncSettings;
Wei Jiac2636032018-02-01 09:15:25 -080033struct DataSourceDesc;
Wei Jia53692fa2017-12-11 10:33:46 -080034class IDataSource;
35struct MediaClock;
36struct MediaHTTPService;
37class MetaData;
38struct NuPlayer2Driver;
39
40struct NuPlayer2 : public AHandler {
41 explicit NuPlayer2(pid_t pid, const sp<MediaClock> &mediaClock);
42
43 void setUID(uid_t uid);
44
45 void setDriver(const wp<NuPlayer2Driver> &driver);
46
Wei Jiac2636032018-02-01 09:15:25 -080047 void setDataSourceAsync(const sp<DataSourceDesc> &dsd);
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',
124 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;
154 bool mUIDValid;
155 uid_t mUID;
156 pid_t mPID;
157 const sp<MediaClock> mMediaClock;
158 Mutex mSourceLock; // guard |mSource|.
159 sp<Source> mSource;
160 uint32_t mSourceFlags;
Wei Jia28288fb2017-12-15 13:45:29 -0800161 sp<ANativeWindowWrapper> mNativeWindow;
Wei Jia33abcc72018-01-30 09:47:38 -0800162 sp<MediaPlayer2Interface::AudioSink> mAudioSink;
Wei Jia53692fa2017-12-11 10:33:46 -0800163 sp<DecoderBase> mVideoDecoder;
164 bool mOffloadAudio;
165 sp<DecoderBase> mAudioDecoder;
166 sp<CCDecoder> mCCDecoder;
167 sp<Renderer> mRenderer;
168 sp<ALooper> mRendererLooper;
169 int32_t mAudioDecoderGeneration;
170 int32_t mVideoDecoderGeneration;
171 int32_t mRendererGeneration;
172
173 Mutex mPlayingTimeLock;
174 int64_t mLastStartedPlayingTimeNs;
175 void stopPlaybackTimer(const char *where);
176 void startPlaybackTimer(const char *where);
177
178 int64_t mLastStartedRebufferingTimeNs;
179 void startRebufferingTimer();
180 void stopRebufferingTimer(bool exitingPlayback);
181
182 int64_t mPreviousSeekTimeUs;
183
184 List<sp<Action> > mDeferredActions;
185
186 bool mAudioEOS;
187 bool mVideoEOS;
188
189 bool mScanSourcesPending;
190 int32_t mScanSourcesGeneration;
191
192 int32_t mPollDurationGeneration;
193 int32_t mTimedTextGeneration;
194
195 enum FlushStatus {
196 NONE,
197 FLUSHING_DECODER,
198 FLUSHING_DECODER_SHUTDOWN,
199 SHUTTING_DOWN_DECODER,
200 FLUSHED,
201 SHUT_DOWN,
202 };
203
204 enum FlushCommand {
205 FLUSH_CMD_NONE,
206 FLUSH_CMD_FLUSH,
207 FLUSH_CMD_SHUTDOWN,
208 };
209
210 // Status of flush responses from the decoder and renderer.
211 bool mFlushComplete[2][2];
212
213 FlushStatus mFlushingAudio;
214 FlushStatus mFlushingVideo;
215
216 // Status of flush responses from the decoder and renderer.
217 bool mResumePending;
218
219 int32_t mVideoScalingMode;
220
221 AudioPlaybackRate mPlaybackSettings;
222 AVSyncSettings mSyncSettings;
223 float mVideoFpsHint;
224 bool mStarted;
225 bool mPrepared;
226 bool mResetting;
227 bool mSourceStarted;
228 bool mAudioDecoderError;
229 bool mVideoDecoderError;
230
231 // Actual pause state, either as requested by client or due to buffering.
232 bool mPaused;
233
234 // Pause state as requested by client. Note that if mPausedByClient is
235 // true, mPaused is always true; if mPausedByClient is false, mPaused could
236 // still become true, when we pause internally due to buffering.
237 bool mPausedByClient;
238
239 // Pause state as requested by source (internally) due to buffering
240 bool mPausedForBuffering;
241
242 // Modular DRM
243 sp<AMediaCryptoWrapper> mCrypto;
244 bool mIsDrmProtected;
245
246 typedef enum {
247 DATA_SOURCE_TYPE_NONE,
248 DATA_SOURCE_TYPE_HTTP_LIVE,
249 DATA_SOURCE_TYPE_RTSP,
250 DATA_SOURCE_TYPE_GENERIC_URL,
251 DATA_SOURCE_TYPE_GENERIC_FD,
252 DATA_SOURCE_TYPE_MEDIA,
253 DATA_SOURCE_TYPE_STREAM,
254 } DATA_SOURCE_TYPE;
255
256 std::atomic<DATA_SOURCE_TYPE> mDataSourceType;
257
258 inline const sp<DecoderBase> &getDecoder(bool audio) {
259 return audio ? mAudioDecoder : mVideoDecoder;
260 }
261
262 inline void clearFlushComplete() {
263 mFlushComplete[0][0] = false;
264 mFlushComplete[0][1] = false;
265 mFlushComplete[1][0] = false;
266 mFlushComplete[1][1] = false;
267 }
268
269 void tryOpenAudioSinkForOffload(
270 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo);
271 void closeAudioSink();
272 void restartAudio(
273 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder);
274 void determineAudioModeChange(const sp<AMessage> &audioFormat);
275
276 status_t instantiateDecoder(
277 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true);
278
279 status_t onInstantiateSecureDecoders();
280
281 void updateVideoSize(
282 const sp<AMessage> &inputFormat,
283 const sp<AMessage> &outputFormat = NULL);
284
285 void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
286
287 void handleFlushComplete(bool audio, bool isDecoder);
288 void finishFlushIfPossible();
289
290 void onStart(
291 int64_t startPositionUs = -1,
292 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC);
293 void onResume();
294 void onPause();
295
296 bool audioDecoderStillNeeded();
297
298 void flushDecoder(bool audio, bool needShutdown);
299
300 void finishResume();
301 void notifyDriverSeekComplete();
302
303 void postScanSources();
304
305 void schedulePollDuration();
306 void cancelPollDuration();
307
308 void processDeferredActions();
309
310 void performSeek(int64_t seekTimeUs, MediaPlayer2SeekMode mode);
311 void performDecoderFlush(FlushCommand audio, FlushCommand video);
312 void performReset();
313 void performScanSources();
Wei Jia28288fb2017-12-15 13:45:29 -0800314 void performSetSurface(const sp<ANativeWindowWrapper> &nw);
Wei Jia53692fa2017-12-11 10:33:46 -0800315 void performResumeDecoders(bool needNotify);
316
317 void onSourceNotify(const sp<AMessage> &msg);
318 void onClosedCaptionNotify(const sp<AMessage> &msg);
319
320 void queueDecoderShutdown(
321 bool audio, bool video, const sp<AMessage> &reply);
322
323 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
324 void sendTimedMetaData(const sp<ABuffer> &buffer);
325 void sendTimedTextData(const sp<ABuffer> &buffer);
326
327 void writeTrackInfo(Parcel* reply, const sp<AMessage>& format) const;
328
329 status_t onPrepareDrm(const sp<AMessage> &msg);
330 status_t onReleaseDrm();
331
332 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer2);
333};
334
335} // namespace android
336
337#endif // NU_PLAYER2_H_