1 /*
2  * Copyright (C) 2010 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_PLAYER_H_
18 
19 #define NU_PLAYER_H_
20 
21 #include <media/AudioResamplerPublic.h>
22 #include <mediadrm/ICrypto.h>
23 #include <media/MediaPlayerInterface.h>
24 #include <media/stagefright/foundation/AHandler.h>
25 
26 namespace android {
27 
28 struct ABuffer;
29 struct AMessage;
30 struct AVSyncSettings;
31 class IDataSource;
32 struct MediaClock;
33 class MetaData;
34 struct NuPlayerDriver;
35 
36 struct NuPlayer : public AHandler {
37     explicit NuPlayer(pid_t pid, const sp<MediaClock> &mediaClock);
38 
39     void setUID(uid_t uid);
40 
41     void init(const wp<NuPlayerDriver> &driver);
42 
43     void setDataSourceAsync(const sp<IStreamSource> &source);
44 
45     void setDataSourceAsync(
46             const sp<IMediaHTTPService> &httpService,
47             const char *url,
48             const KeyedVector<String8, String8> *headers);
49 
50     void setDataSourceAsync(int fd, int64_t offset, int64_t length);
51 
52     void setDataSourceAsync(const sp<DataSource> &source);
53 
54     void setDataSourceAsync(const String8& rtpParams);
55 
56     status_t getBufferingSettings(BufferingSettings* buffering /* nonnull */);
57     status_t setBufferingSettings(const BufferingSettings& buffering);
58 
59     void prepareAsync();
60 
61     void setVideoSurfaceTextureAsync(
62             const sp<IGraphicBufferProducer> &bufferProducer);
63 
64     void setAudioSink(const sp<MediaPlayerBase::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             MediaPlayerSeekMode mode = MediaPlayerSeekMode::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> > *trackStats);
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 
103     void updateInternalTimers();
104 
105     void setTargetBitrate(int bitrate /* bps */);
106 
107     void dump(AString& logString);
108 
109 protected:
110     virtual ~NuPlayer();
111 
112     virtual void onMessageReceived(const sp<AMessage> &msg);
113 
114 public:
115     struct NuPlayerStreamListener;
116     struct Source;
117 
118 private:
119     struct Decoder;
120     struct DecoderBase;
121     struct DecoderPassThrough;
122     struct CCDecoder;
123     struct GenericSource;
124     struct HTTPLiveSource;
125     struct Renderer;
126     struct RTPSource;
127     struct RTSPSource;
128     struct StreamingSource;
129     struct Action;
130     struct SeekAction;
131     struct SetSurfaceAction;
132     struct ResumeDecoderAction;
133     struct FlushDecoderAction;
134     struct PostMessageAction;
135     struct SimpleAction;
136 
137     enum {
138         kWhatSetDataSource              = '=DaS',
139         kWhatPrepare                    = 'prep',
140         kWhatSetVideoSurface            = '=VSu',
141         kWhatSetAudioSink               = '=AuS',
142         kWhatMoreDataQueued             = 'more',
143         kWhatConfigPlayback             = 'cfPB',
144         kWhatConfigSync                 = 'cfSy',
145         kWhatGetPlaybackSettings        = 'gPbS',
146         kWhatGetSyncSettings            = 'gSyS',
147         kWhatStart                      = 'strt',
148         kWhatScanSources                = 'scan',
149         kWhatVideoNotify                = 'vidN',
150         kWhatAudioNotify                = 'audN',
151         kWhatClosedCaptionNotify        = 'capN',
152         kWhatRendererNotify             = 'renN',
153         kWhatReset                      = 'rset',
154         kWhatNotifyTime                 = 'nfyT',
155         kWhatSeek                       = 'seek',
156         kWhatPause                      = 'paus',
157         kWhatResume                     = 'rsme',
158         kWhatPollDuration               = 'polD',
159         kWhatSourceNotify               = 'srcN',
160         kWhatGetTrackInfo               = 'gTrI',
161         kWhatGetSelectedTrack           = 'gSel',
162         kWhatSelectTrack                = 'selT',
163         kWhatGetBufferingSettings       = 'gBus',
164         kWhatSetBufferingSettings       = 'sBuS',
165         kWhatPrepareDrm                 = 'pDrm',
166         kWhatReleaseDrm                 = 'rDrm',
167         kWhatMediaClockNotify           = 'mckN',
168     };
169 
170     wp<NuPlayerDriver> mDriver;
171     bool mUIDValid;
172     uid_t mUID;
173     pid_t mPID;
174     const sp<MediaClock> mMediaClock;
175     Mutex mSourceLock;  // guard |mSource|.
176     sp<Source> mSource;
177     uint32_t mSourceFlags;
178     sp<Surface> mSurface;
179     sp<MediaPlayerBase::AudioSink> mAudioSink;
180     sp<DecoderBase> mVideoDecoder;
181     bool mOffloadAudio;
182     sp<DecoderBase> mAudioDecoder;
183     Mutex mDecoderLock;  // guard |mAudioDecoder| and |mVideoDecoder|.
184     sp<CCDecoder> mCCDecoder;
185     sp<Renderer> mRenderer;
186     sp<ALooper> mRendererLooper;
187     int32_t mAudioDecoderGeneration;
188     int32_t mVideoDecoderGeneration;
189     int32_t mRendererGeneration;
190 
191     Mutex mPlayingTimeLock;
192     int64_t mLastStartedPlayingTimeNs;
193     void updatePlaybackTimer(bool stopping, const char *where);
194     void startPlaybackTimer(const char *where);
195 
196     int64_t mLastStartedRebufferingTimeNs;
197     void startRebufferingTimer();
198     void updateRebufferingTimer(bool stopping, bool exitingPlayback);
199 
200     int64_t mPreviousSeekTimeUs;
201 
202     List<sp<Action> > mDeferredActions;
203 
204     bool mAudioEOS;
205     bool mVideoEOS;
206 
207     bool mScanSourcesPending;
208     int32_t mScanSourcesGeneration;
209 
210     int32_t mPollDurationGeneration;
211     int32_t mTimedTextGeneration;
212 
213     enum FlushStatus {
214         NONE,
215         FLUSHING_DECODER,
216         FLUSHING_DECODER_SHUTDOWN,
217         SHUTTING_DOWN_DECODER,
218         FLUSHED,
219         SHUT_DOWN,
220     };
221 
222     enum FlushCommand {
223         FLUSH_CMD_NONE,
224         FLUSH_CMD_FLUSH,
225         FLUSH_CMD_SHUTDOWN,
226     };
227 
228     // Status of flush responses from the decoder and renderer.
229     bool mFlushComplete[2][2];
230 
231     FlushStatus mFlushingAudio;
232     FlushStatus mFlushingVideo;
233 
234     // Status of flush responses from the decoder and renderer.
235     bool mResumePending;
236 
237     int32_t mVideoScalingMode;
238 
239     AudioPlaybackRate mPlaybackSettings;
240     AVSyncSettings mSyncSettings;
241     float mVideoFpsHint;
242     bool mStarted;
243     bool mPrepared;
244     bool mResetting;
245     bool mSourceStarted;
246     bool mAudioDecoderError;
247     bool mVideoDecoderError;
248 
249     // Actual pause state, either as requested by client or due to buffering.
250     bool mPaused;
251 
252     // Pause state as requested by client. Note that if mPausedByClient is
253     // true, mPaused is always true; if mPausedByClient is false, mPaused could
254     // still become true, when we pause internally due to buffering.
255     bool mPausedByClient;
256 
257     // Pause state as requested by source (internally) due to buffering
258     bool mPausedForBuffering;
259 
260     // Modular DRM
261     sp<ICrypto> mCrypto;
262     bool mIsDrmProtected;
263 
264     typedef enum {
265         DATA_SOURCE_TYPE_NONE,
266         DATA_SOURCE_TYPE_HTTP_LIVE,
267         DATA_SOURCE_TYPE_RTP,
268         DATA_SOURCE_TYPE_RTSP,
269         DATA_SOURCE_TYPE_GENERIC_URL,
270         DATA_SOURCE_TYPE_GENERIC_FD,
271         DATA_SOURCE_TYPE_MEDIA,
272         DATA_SOURCE_TYPE_STREAM,
273     } DATA_SOURCE_TYPE;
274 
275     std::atomic<DATA_SOURCE_TYPE> mDataSourceType;
276 
getDecoderNuPlayer277     inline const sp<DecoderBase> &getDecoder(bool audio) {
278         return audio ? mAudioDecoder : mVideoDecoder;
279     }
280 
clearFlushCompleteNuPlayer281     inline void clearFlushComplete() {
282         mFlushComplete[0][0] = false;
283         mFlushComplete[0][1] = false;
284         mFlushComplete[1][0] = false;
285         mFlushComplete[1][1] = false;
286     }
287 
288     void tryOpenAudioSinkForOffload(
289             const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo);
290     void closeAudioSink();
291     void restartAudio(
292             int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder);
293     void determineAudioModeChange(const sp<AMessage> &audioFormat);
294 
295     status_t instantiateDecoder(
296             bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true);
297 
298     status_t onInstantiateSecureDecoders();
299 
300     void updateVideoSize(
301             const sp<AMessage> &inputFormat,
302             const sp<AMessage> &outputFormat = NULL);
303 
304     void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
305 
306     void handleFlushComplete(bool audio, bool isDecoder);
307     void finishFlushIfPossible();
308 
309     void onStart(
310             int64_t startPositionUs = -1,
311             MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC);
312     void onResume();
313     void onPause();
314 
315     bool audioDecoderStillNeeded();
316 
317     void flushDecoder(bool audio, bool needShutdown);
318 
319     void finishResume();
320     void notifyDriverSeekComplete();
321 
322     void postScanSources();
323 
324     void schedulePollDuration();
325     void cancelPollDuration();
326 
327     void processDeferredActions();
328 
329     void performSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode);
330     void performDecoderFlush(FlushCommand audio, FlushCommand video);
331     void performReset();
332     void performScanSources();
333     void performSetSurface(const sp<Surface> &wrapper);
334     void performResumeDecoders(bool needNotify);
335 
336     void onSourceNotify(const sp<AMessage> &msg);
337     void onClosedCaptionNotify(const sp<AMessage> &msg);
338 
339     void queueDecoderShutdown(
340             bool audio, bool video, const sp<AMessage> &reply);
341 
342     void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
343     void sendTimedMetaData(const sp<ABuffer> &buffer);
344     void sendTimedTextData(const sp<ABuffer> &buffer);
345     void sendIMSRxNotice(const sp<AMessage> &msg);
346 
347     void writeTrackInfo(Parcel* reply, const sp<AMessage>& format) const;
348 
349     status_t onPrepareDrm(const sp<AMessage> &msg);
350     status_t onReleaseDrm();
351 
352     DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
353 };
354 
355 }  // namespace android
356 
357 #endif  // NU_PLAYER_H_
358