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