• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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/stagefright/foundation/AHandler.h>
23 
24 #include <mediaplayer2/MediaPlayer2Interface.h>
25 #include <mediaplayer2/JObjectHolder.h>
26 
27 #include "mediaplayer2.pb.h"
28 
29 using android::media::MediaPlayer2Proto::PlayerMessage;
30 
31 namespace android {
32 
33 struct ABuffer;
34 struct AMediaCryptoWrapper;
35 struct AMessage;
36 struct ANativeWindowWrapper;
37 struct AudioPlaybackRate;
38 struct AVSyncSettings;
39 struct DataSourceDesc;
40 struct MediaClock;
41 struct MediaHTTPService;
42 class MetaData;
43 struct NuPlayer2Driver;
44 
45 struct NuPlayer2 : public AHandler {
46     explicit NuPlayer2(pid_t pid, uid_t uid,
47             const sp<MediaClock> &mediaClock, const sp<JObjectHolder> &context);
48 
49     void setDriver(const wp<NuPlayer2Driver> &driver);
50 
51     void setDataSourceAsync(const sp<DataSourceDesc> &dsd);
52     void prepareNextDataSourceAsync(const sp<DataSourceDesc> &dsd);
53     void playNextDataSource(int64_t srcId);
54 
55     status_t getBufferingSettings(BufferingSettings* buffering /* nonnull */);
56     status_t setBufferingSettings(const BufferingSettings& buffering);
57 
58     void prepareAsync();
59 
60     void setVideoSurfaceTextureAsync(const sp<ANativeWindowWrapper> &nww);
61 
62     void setAudioSink(const sp<MediaPlayer2Interface::AudioSink> &sink);
63     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 
68     void start();
69 
70     void pause();
71 
72     // Will notify the driver through "notifyResetComplete" once finished.
73     void resetAsync();
74 
75     // Request a notification when specified media time is reached.
76     status_t notifyAt(int64_t mediaTimeUs);
77 
78     // Will notify the driver through "notifySeekComplete" once finished
79     // and needNotify is true.
80     void seekToAsync(
81             int64_t seekTimeUs,
82             MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC,
83             bool needNotify = false);
84     void rewind();
85 
86     status_t setVideoScalingMode(int32_t mode);
87     status_t getTrackInfo(int64_t srcId, PlayerMessage* reply) const;
88     status_t getSelectedTrack(int64_t srcId, int32_t type, PlayerMessage* reply) const;
89     status_t selectTrack(int64_t srcId, size_t trackIndex, bool select, int64_t timeUs);
90     status_t getCurrentPosition(int64_t *mediaUs);
91     void getStats(Vector<sp<AMessage> > *mTrackStats);
92 
93     sp<MetaData> getFileMeta();
94     float getFrameRate();
95 
96     // Modular DRM
97     status_t prepareDrm(int64_t srcId, const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId);
98     status_t releaseDrm(int64_t srcId);
99 
100     const char *getDataSourceType();
101 
102 protected:
103     virtual ~NuPlayer2();
104 
105     virtual void onMessageReceived(const sp<AMessage> &msg);
106 
107 public:
108     struct StreamListener;
109     struct Source;
110 
111 private:
112     struct Decoder;
113     struct DecoderBase;
114     struct DecoderPassThrough;
115     struct CCDecoder;
116     struct GenericSource2;
117     struct HTTPLiveSource2;
118     struct Renderer;
119     struct RTSPSource2;
120     struct Action;
121     struct SeekAction;
122     struct SetSurfaceAction;
123     struct ResumeDecoderAction;
124     struct FlushDecoderAction;
125     struct PostMessageAction;
126     struct SimpleAction;
127 
128     enum {
129         kWhatSetDataSource              = '=DaS',
130         kWhatPrepare                    = 'prep',
131         kWhatPrepareNextDataSource      = 'pNDS',
132         kWhatPlayNextDataSource         = 'plNS',
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         kWhatRewind                     = 'reWd',
161         kWhatEOSMonitor                 = 'eosM',
162     };
163 
164     typedef enum {
165         DATA_SOURCE_TYPE_NONE,
166         DATA_SOURCE_TYPE_HTTP_LIVE,
167         DATA_SOURCE_TYPE_RTSP,
168         DATA_SOURCE_TYPE_GENERIC_URL,
169         DATA_SOURCE_TYPE_GENERIC_FD,
170         DATA_SOURCE_TYPE_MEDIA,
171     } DATA_SOURCE_TYPE;
172 
173     struct SourceInfo {
174         SourceInfo();
175         SourceInfo &operator=(const SourceInfo &);
176 
177         sp<Source> mSource;
178         std::atomic<DATA_SOURCE_TYPE> mDataSourceType;
179         int64_t mSrcId;
180         uint32_t mSourceFlags;
181         int64_t mStartTimeUs;
182         int64_t mEndTimeUs;
183         // Modular DRM
184         sp<AMediaCryptoWrapper> mCrypto;
185         bool mIsDrmProtected = false;
186     };
187 
188     wp<NuPlayer2Driver> mDriver;
189     pid_t mPID;
190     uid_t mUID;
191     const sp<MediaClock> mMediaClock;
192     Mutex mSourceLock;  // guard |mSource|.
193     SourceInfo mCurrentSourceInfo;
194     SourceInfo mNextSourceInfo;
195     sp<ANativeWindowWrapper> mNativeWindow;
196     sp<MediaPlayer2Interface::AudioSink> mAudioSink;
197     sp<DecoderBase> mVideoDecoder;
198     bool mOffloadAudio;
199     sp<DecoderBase> mAudioDecoder;
200     Mutex mDecoderLock;  // guard |mAudioDecoder| and |mVideoDecoder|.
201     sp<CCDecoder> mCCDecoder;
202     sp<Renderer> mRenderer;
203     sp<ALooper> mRendererLooper;
204     int32_t mAudioDecoderGeneration;
205     int32_t mVideoDecoderGeneration;
206     int32_t mRendererGeneration;
207     int32_t mEOSMonitorGeneration;
208 
209     Mutex mPlayingTimeLock;
210     int64_t mLastStartedPlayingTimeNs;
211     void stopPlaybackTimer(const char *where);
212     void startPlaybackTimer(const char *where);
213 
214     int64_t mLastStartedRebufferingTimeNs;
215     void startRebufferingTimer();
216     void stopRebufferingTimer(bool exitingPlayback);
217 
218     int64_t mPreviousSeekTimeUs;
219 
220     List<sp<Action> > mDeferredActions;
221 
222     bool mAudioEOS;
223     bool mVideoEOS;
224 
225     bool mScanSourcesPending;
226     int32_t mScanSourcesGeneration;
227 
228     int32_t mPollDurationGeneration;
229     int32_t mTimedTextGeneration;
230 
231     enum FlushStatus {
232         NONE,
233         FLUSHING_DECODER,
234         FLUSHING_DECODER_SHUTDOWN,
235         SHUTTING_DOWN_DECODER,
236         FLUSHED,
237         SHUT_DOWN,
238     };
239 
240     enum FlushCommand {
241         FLUSH_CMD_NONE,
242         FLUSH_CMD_FLUSH,
243         FLUSH_CMD_SHUTDOWN,
244     };
245 
246     // Status of flush responses from the decoder and renderer.
247     bool mFlushComplete[2][2];
248 
249     FlushStatus mFlushingAudio;
250     FlushStatus mFlushingVideo;
251 
252     // Status of flush responses from the decoder and renderer.
253     bool mResumePending;
254 
255     int32_t mVideoScalingMode;
256 
257     AudioPlaybackRate mPlaybackSettings;
258     AVSyncSettings mSyncSettings;
259     float mVideoFpsHint;
260     bool mStarted;
261     bool mPrepared;
262     bool mResetting;
263     bool mSourceStarted;
264     bool mAudioDecoderError;
265     bool mVideoDecoderError;
266 
267     // Actual pause state, either as requested by client or due to buffering.
268     bool mPaused;
269 
270     // Pause state as requested by client. Note that if mPausedByClient is
271     // true, mPaused is always true; if mPausedByClient is false, mPaused could
272     // still become true, when we pause internally due to buffering.
273     bool mPausedByClient;
274 
275     // Pause state as requested by source (internally) due to buffering
276     bool mPausedForBuffering;
277 
278     // Passed from JAVA
279     const sp<JObjectHolder> mContext;
280 
getDecoderNuPlayer2281     inline const sp<DecoderBase> &getDecoder(bool audio) {
282         return audio ? mAudioDecoder : mVideoDecoder;
283     }
284 
clearFlushCompleteNuPlayer2285     inline void clearFlushComplete() {
286         mFlushComplete[0][0] = false;
287         mFlushComplete[0][1] = false;
288         mFlushComplete[1][0] = false;
289         mFlushComplete[1][1] = false;
290     }
291 
292     void disconnectSource();
293 
294     status_t createNuPlayer2Source(const sp<DataSourceDesc> &dsd,
295                                    sp<Source> *source,
296                                    DATA_SOURCE_TYPE *dataSourceType);
297 
298     void tryOpenAudioSinkForOffload(
299             const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo);
300     void closeAudioSink();
301     void restartAudio(
302             int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder);
303     void determineAudioModeChange(const sp<AMessage> &audioFormat);
304 
305     status_t instantiateDecoder(
306             bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true);
307 
308     void updateVideoSize(
309             int64_t srcId,
310             const sp<AMessage> &inputFormat,
311             const sp<AMessage> &outputFormat = NULL);
312 
313     void notifyListener(int64_t srcId, int msg, int ext1, int ext2, const PlayerMessage *in = NULL);
314 
315     void addEndTimeMonitor();
316 
317     void handleFlushComplete(bool audio, bool isDecoder);
318     void finishFlushIfPossible();
319 
320     void onStart(bool play);
321     void onResume();
322     void onPause();
323 
324     bool audioDecoderStillNeeded();
325 
326     void flushDecoder(bool audio, bool needShutdown);
327 
328     void finishResume();
329     void notifyDriverSeekComplete(int64_t srcId);
330 
331     void postScanSources();
332 
333     void schedulePollDuration();
334     void cancelPollDuration();
335 
336     void processDeferredActions();
337 
338     void performSeek(int64_t seekTimeUs, MediaPlayer2SeekMode mode);
339     void performDecoderFlush(FlushCommand audio, FlushCommand video);
340     void performReset();
341     void performPlayNextDataSource();
342     void performScanSources();
343     void performSetSurface(const sp<ANativeWindowWrapper> &nw);
344     void performResumeDecoders(bool needNotify);
345 
346     void onSourceNotify(const sp<AMessage> &msg);
347     void onClosedCaptionNotify(const sp<AMessage> &msg);
348 
349     void queueDecoderShutdown(
350             bool audio, bool video, const sp<AMessage> &reply);
351 
352     void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
353     void sendTimedMetaData(const sp<ABuffer> &buffer);
354     void sendTimedTextData(const sp<ABuffer> &buffer);
355 
356     void writeTrackInfo(PlayerMessage* reply, const sp<AMessage>& format) const;
357 
358     status_t onPrepareDrm(const sp<AMessage> &msg);
359     status_t onReleaseDrm(const sp<AMessage> &msg);
360 
361     SourceInfo* getSourceInfoByIdInMsg(const sp<AMessage> &msg);
362     void resetSourceInfo(SourceInfo &srcInfo);
363 
364     DISALLOW_EVIL_CONSTRUCTORS(NuPlayer2);
365 };
366 
367 }  // namespace android
368 
369 #endif  // NU_PLAYER2_H_
370