blob: 638b25941ad656441f88987e76d07ebbb6fe3d7a [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;
33class IDataSource;
34struct MediaClock;
35struct MediaHTTPService;
36class MetaData;
37struct NuPlayer2Driver;
38
39struct NuPlayer2 : public AHandler {
40 explicit NuPlayer2(pid_t pid, const sp<MediaClock> &mediaClock);
41
42 void setUID(uid_t uid);
43
44 void setDriver(const wp<NuPlayer2Driver> &driver);
45
46 void setDataSourceAsync(const sp<IStreamSource> &source);
47
48 void setDataSourceAsync(
49 const sp<MediaHTTPService> &httpService,
50 const char *url,
51 const KeyedVector<String8, String8> *headers);
52
53 void setDataSourceAsync(int fd, int64_t offset, int64_t length);
54
55 void setDataSourceAsync(const sp<DataSource> &source);
56
57 status_t getBufferingSettings(BufferingSettings* buffering /* nonnull */);
58 status_t setBufferingSettings(const BufferingSettings& buffering);
59
60 void prepareAsync();
61
Wei Jia28288fb2017-12-15 13:45:29 -080062 void setVideoSurfaceTextureAsync(const sp<ANativeWindowWrapper> &nww);
Wei Jia53692fa2017-12-11 10:33:46 -080063
64 void setAudioSink(const sp<MediaPlayer2Base::AudioSink> &sink);
65 status_t setPlaybackSettings(const AudioPlaybackRate &rate);
66 status_t getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */);
67 status_t setSyncSettings(const AVSyncSettings &sync, float videoFpsHint);
68 status_t getSyncSettings(AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */);
69
70 void start();
71
72 void pause();
73
74 // Will notify the driver through "notifyResetComplete" once finished.
75 void resetAsync();
76
77 // Request a notification when specified media time is reached.
78 status_t notifyAt(int64_t mediaTimeUs);
79
80 // Will notify the driver through "notifySeekComplete" once finished
81 // and needNotify is true.
82 void seekToAsync(
83 int64_t seekTimeUs,
84 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC,
85 bool needNotify = false);
86
87 status_t setVideoScalingMode(int32_t mode);
88 status_t getTrackInfo(Parcel* reply) const;
89 status_t getSelectedTrack(int32_t type, Parcel* reply) const;
90 status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs);
91 status_t getCurrentPosition(int64_t *mediaUs);
92 void getStats(Vector<sp<AMessage> > *mTrackStats);
93
94 sp<MetaData> getFileMeta();
95 float getFrameRate();
96
97 // Modular DRM
98 status_t prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId);
99 status_t releaseDrm();
100
101 const char *getDataSourceType();
102
103protected:
104 virtual ~NuPlayer2();
105
106 virtual void onMessageReceived(const sp<AMessage> &msg);
107
108public:
109 struct StreamListener;
110 struct Source;
111
112private:
113 struct Decoder;
114 struct DecoderBase;
115 struct DecoderPassThrough;
116 struct CCDecoder;
117 struct GenericSource;
118 struct HTTPLiveSource;
119 struct Renderer;
120 struct RTSPSource;
121 struct StreamingSource;
122 struct Action;
123 struct SeekAction;
124 struct SetSurfaceAction;
125 struct ResumeDecoderAction;
126 struct FlushDecoderAction;
127 struct PostMessageAction;
128 struct SimpleAction;
129
130 enum {
131 kWhatSetDataSource = '=DaS',
132 kWhatPrepare = 'prep',
133 kWhatSetVideoSurface = '=VSu',
134 kWhatSetAudioSink = '=AuS',
135 kWhatMoreDataQueued = 'more',
136 kWhatConfigPlayback = 'cfPB',
137 kWhatConfigSync = 'cfSy',
138 kWhatGetPlaybackSettings = 'gPbS',
139 kWhatGetSyncSettings = 'gSyS',
140 kWhatStart = 'strt',
141 kWhatScanSources = 'scan',
142 kWhatVideoNotify = 'vidN',
143 kWhatAudioNotify = 'audN',
144 kWhatClosedCaptionNotify = 'capN',
145 kWhatRendererNotify = 'renN',
146 kWhatReset = 'rset',
147 kWhatNotifyTime = 'nfyT',
148 kWhatSeek = 'seek',
149 kWhatPause = 'paus',
150 kWhatResume = 'rsme',
151 kWhatPollDuration = 'polD',
152 kWhatSourceNotify = 'srcN',
153 kWhatGetTrackInfo = 'gTrI',
154 kWhatGetSelectedTrack = 'gSel',
155 kWhatSelectTrack = 'selT',
156 kWhatGetBufferingSettings = 'gBus',
157 kWhatSetBufferingSettings = 'sBuS',
158 kWhatPrepareDrm = 'pDrm',
159 kWhatReleaseDrm = 'rDrm',
160 };
161
162 wp<NuPlayer2Driver> mDriver;
163 bool mUIDValid;
164 uid_t mUID;
165 pid_t mPID;
166 const sp<MediaClock> mMediaClock;
167 Mutex mSourceLock; // guard |mSource|.
168 sp<Source> mSource;
169 uint32_t mSourceFlags;
Wei Jia28288fb2017-12-15 13:45:29 -0800170 sp<ANativeWindowWrapper> mNativeWindow;
Wei Jia53692fa2017-12-11 10:33:46 -0800171 sp<MediaPlayer2Base::AudioSink> mAudioSink;
172 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,
262 DATA_SOURCE_TYPE_STREAM,
263 } DATA_SOURCE_TYPE;
264
265 std::atomic<DATA_SOURCE_TYPE> mDataSourceType;
266
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
278 void tryOpenAudioSinkForOffload(
279 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo);
280 void closeAudioSink();
281 void restartAudio(
282 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder);
283 void determineAudioModeChange(const sp<AMessage> &audioFormat);
284
285 status_t instantiateDecoder(
286 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true);
287
288 status_t onInstantiateSecureDecoders();
289
290 void updateVideoSize(
291 const sp<AMessage> &inputFormat,
292 const sp<AMessage> &outputFormat = NULL);
293
294 void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
295
296 void handleFlushComplete(bool audio, bool isDecoder);
297 void finishFlushIfPossible();
298
299 void onStart(
300 int64_t startPositionUs = -1,
301 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC);
302 void onResume();
303 void onPause();
304
305 bool audioDecoderStillNeeded();
306
307 void flushDecoder(bool audio, bool needShutdown);
308
309 void finishResume();
310 void notifyDriverSeekComplete();
311
312 void postScanSources();
313
314 void schedulePollDuration();
315 void cancelPollDuration();
316
317 void processDeferredActions();
318
319 void performSeek(int64_t seekTimeUs, MediaPlayer2SeekMode mode);
320 void performDecoderFlush(FlushCommand audio, FlushCommand video);
321 void performReset();
322 void performScanSources();
Wei Jia28288fb2017-12-15 13:45:29 -0800323 void performSetSurface(const sp<ANativeWindowWrapper> &nw);
Wei Jia53692fa2017-12-11 10:33:46 -0800324 void performResumeDecoders(bool needNotify);
325
326 void onSourceNotify(const sp<AMessage> &msg);
327 void onClosedCaptionNotify(const sp<AMessage> &msg);
328
329 void queueDecoderShutdown(
330 bool audio, bool video, const sp<AMessage> &reply);
331
332 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
333 void sendTimedMetaData(const sp<ABuffer> &buffer);
334 void sendTimedTextData(const sp<ABuffer> &buffer);
335
336 void writeTrackInfo(Parcel* reply, const sp<AMessage>& format) const;
337
338 status_t onPrepareDrm(const sp<AMessage> &msg);
339 status_t onReleaseDrm();
340
341 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer2);
342};
343
344} // namespace android
345
346#endif // NU_PLAYER2_H_