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