blob: 9f4c462b140b715176ea714b3f328a21fff2743d [file] [log] [blame]
Andreas Huberf9334412010-12-15 15:17:42 -08001/*
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
Lajos Molnar3a474aa2015-04-24 17:10:07 -070021#include <media/AudioResamplerPublic.h>
Andreas Huberf9334412010-12-15 15:17:42 -080022#include <media/MediaPlayerInterface.h>
23#include <media/stagefright/foundation/AHandler.h>
24
Andreas Huberf9334412010-12-15 15:17:42 -080025namespace android {
26
Chong Zhang404fced2014-06-11 14:45:31 -070027struct ABuffer;
Lajos Molnar87603c02014-08-20 19:25:30 -070028struct AMessage;
Lajos Molnar3a474aa2015-04-24 17:10:07 -070029struct AudioPlaybackRate;
30struct AVSyncSettings;
Lajos Molnaree4e1b12015-04-17 13:46:19 -070031class IDataSource;
Bernhard Rosenkränzer86a58ca2014-11-17 20:14:08 +010032class MetaData;
Andreas Huber43c3e6c2011-01-05 12:17:08 -080033struct NuPlayerDriver;
Andreas Huberf9334412010-12-15 15:17:42 -080034
35struct NuPlayer : public AHandler {
36 NuPlayer();
37
Andreas Huber9b80c2b2011-06-30 15:47:02 -070038 void setUID(uid_t uid);
39
Andreas Huber43c3e6c2011-01-05 12:17:08 -080040 void setDriver(const wp<NuPlayerDriver> &driver);
Andreas Huberf9334412010-12-15 15:17:42 -080041
Andreas Huber9575c962013-02-05 13:59:56 -080042 void setDataSourceAsync(const sp<IStreamSource> &source);
Andreas Huber5bc087c2010-12-23 10:27:40 -080043
Andreas Huber9575c962013-02-05 13:59:56 -080044 void setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -080045 const sp<IMediaHTTPService> &httpService,
46 const char *url,
47 const KeyedVector<String8, String8> *headers);
Andreas Huber5bc087c2010-12-23 10:27:40 -080048
Andreas Huber9575c962013-02-05 13:59:56 -080049 void setDataSourceAsync(int fd, int64_t offset, int64_t length);
50
Chris Watkins99f31602015-03-20 13:06:33 -070051 void setDataSourceAsync(const sp<DataSource> &source);
52
Andreas Huber9575c962013-02-05 13:59:56 -080053 void prepareAsync();
Andreas Huberafed0e12011-09-20 15:39:58 -070054
Andreas Huber57a339c2012-12-03 11:18:00 -080055 void setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -080056 const sp<IGraphicBufferProducer> &bufferProducer);
Andreas Huber57a339c2012-12-03 11:18:00 -080057
Andreas Huberf9334412010-12-15 15:17:42 -080058 void setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink);
Lajos Molnar3a474aa2015-04-24 17:10:07 -070059 status_t setPlaybackSettings(const AudioPlaybackRate &rate);
60 status_t getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */);
61 status_t setSyncSettings(const AVSyncSettings &sync, float videoFpsHint);
62 status_t getSyncSettings(AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */);
63
Andreas Huberf9334412010-12-15 15:17:42 -080064 void start();
65
Andreas Huber43c3e6c2011-01-05 12:17:08 -080066 void pause();
Andreas Huber43c3e6c2011-01-05 12:17:08 -080067
68 // Will notify the driver through "notifyResetComplete" once finished.
Andreas Huber1aef2112011-01-04 14:01:29 -080069 void resetAsync();
70
Wei Jiae427abf2014-09-22 15:21:11 -070071 // Will notify the driver through "notifySeekComplete" once finished
72 // and needNotify is true.
73 void seekToAsync(int64_t seekTimeUs, bool needNotify = false);
Andreas Huber43c3e6c2011-01-05 12:17:08 -080074
James Dong0d268a32012-08-31 12:18:27 -070075 status_t setVideoScalingMode(int32_t mode);
Chong Zhangdcb89b32013-08-06 09:44:47 -070076 status_t getTrackInfo(Parcel* reply) const;
Robert Shih7c4f0d72014-07-09 18:53:31 -070077 status_t getSelectedTrack(int32_t type, Parcel* reply) const;
Robert Shih6ffb1fd2014-10-29 16:24:32 -070078 status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs);
Ronghua Wua73d9e02014-10-08 15:13:29 -070079 status_t getCurrentPosition(int64_t *mediaUs);
Praveen Chavane1e5d7a2015-05-19 19:09:48 -070080 void getStats(Vector<sp<AMessage> > *mTrackStats);
James Dong0d268a32012-08-31 12:18:27 -070081
Marco Nelissenf0b72b52014-09-16 15:43:44 -070082 sp<MetaData> getFileMeta();
Ronghua Wuc8a70d32015-04-29 16:26:34 -070083 float getFrameRate();
Marco Nelissenf0b72b52014-09-16 15:43:44 -070084
Andreas Huberf9334412010-12-15 15:17:42 -080085protected:
86 virtual ~NuPlayer();
87
88 virtual void onMessageReceived(const sp<AMessage> &msg);
89
Andreas Huber84066782011-08-16 09:34:26 -070090public:
91 struct NuPlayerStreamListener;
92 struct Source;
93
Andreas Huberf9334412010-12-15 15:17:42 -080094private:
Andreas Huberf9334412010-12-15 15:17:42 -080095 struct Decoder;
Chong Zhang7137ec72014-11-12 16:41:05 -080096 struct DecoderBase;
Wei Jiabc2fb722014-07-08 16:37:57 -070097 struct DecoderPassThrough;
Chong Zhanga7fa1d92014-06-11 14:49:23 -070098 struct CCDecoder;
Andreas Huberafed0e12011-09-20 15:39:58 -070099 struct GenericSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800100 struct HTTPLiveSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800101 struct Renderer;
Andreas Huberafed0e12011-09-20 15:39:58 -0700102 struct RTSPSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800103 struct StreamingSource;
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800104 struct Action;
105 struct SeekAction;
Andreas Huber57a339c2012-12-03 11:18:00 -0800106 struct SetSurfaceAction;
Chong Zhangf8d71772014-11-26 15:08:34 -0800107 struct ResumeDecoderAction;
Wei Jiafef808d2014-10-31 17:57:05 -0700108 struct FlushDecoderAction;
Andreas Huber14f76722013-01-15 09:04:18 -0800109 struct PostMessageAction;
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800110 struct SimpleAction;
Andreas Huberf9334412010-12-15 15:17:42 -0800111
112 enum {
Andreas Huber078cfcf2011-09-15 12:25:04 -0700113 kWhatSetDataSource = '=DaS',
Andreas Huber9575c962013-02-05 13:59:56 -0800114 kWhatPrepare = 'prep',
Lajos Molnar1de1e252015-04-30 18:18:34 -0700115 kWhatSetVideoSurface = '=VSu',
Andreas Huber078cfcf2011-09-15 12:25:04 -0700116 kWhatSetAudioSink = '=AuS',
117 kWhatMoreDataQueued = 'more',
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700118 kWhatConfigPlayback = 'cfPB',
119 kWhatConfigSync = 'cfSy',
120 kWhatGetPlaybackSettings = 'gPbS',
121 kWhatGetSyncSettings = 'gSyS',
Andreas Huber078cfcf2011-09-15 12:25:04 -0700122 kWhatStart = 'strt',
123 kWhatScanSources = 'scan',
124 kWhatVideoNotify = 'vidN',
125 kWhatAudioNotify = 'audN',
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700126 kWhatClosedCaptionNotify = 'capN',
Andreas Huber078cfcf2011-09-15 12:25:04 -0700127 kWhatRendererNotify = 'renN',
128 kWhatReset = 'rset',
129 kWhatSeek = 'seek',
130 kWhatPause = 'paus',
131 kWhatResume = 'rsme',
Andreas Huberb7c8e912012-11-27 15:02:53 -0800132 kWhatPollDuration = 'polD',
Andreas Huberb5f25f02013-02-05 10:14:26 -0800133 kWhatSourceNotify = 'srcN',
Chong Zhangdcb89b32013-08-06 09:44:47 -0700134 kWhatGetTrackInfo = 'gTrI',
Robert Shih7c4f0d72014-07-09 18:53:31 -0700135 kWhatGetSelectedTrack = 'gSel',
Chong Zhangdcb89b32013-08-06 09:44:47 -0700136 kWhatSelectTrack = 'selT',
Andreas Huberf9334412010-12-15 15:17:42 -0800137 };
138
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800139 wp<NuPlayerDriver> mDriver;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700140 bool mUIDValid;
141 uid_t mUID;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800142 sp<Source> mSource;
Andreas Huber9575c962013-02-05 13:59:56 -0800143 uint32_t mSourceFlags;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700144 sp<Surface> mSurface;
Andreas Huberf9334412010-12-15 15:17:42 -0800145 sp<MediaPlayerBase::AudioSink> mAudioSink;
Chong Zhang7137ec72014-11-12 16:41:05 -0800146 sp<DecoderBase> mVideoDecoder;
Wei Jiabc2fb722014-07-08 16:37:57 -0700147 bool mOffloadAudio;
Chong Zhang7137ec72014-11-12 16:41:05 -0800148 sp<DecoderBase> mAudioDecoder;
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700149 sp<CCDecoder> mCCDecoder;
Andreas Huberf9334412010-12-15 15:17:42 -0800150 sp<Renderer> mRenderer;
Lajos Molnar09524832014-07-17 14:29:51 -0700151 sp<ALooper> mRendererLooper;
Wei Jia88703c32014-08-06 11:24:07 -0700152 int32_t mAudioDecoderGeneration;
153 int32_t mVideoDecoderGeneration;
Wei Jia57568df2014-09-22 10:16:29 -0700154 int32_t mRendererGeneration;
Andreas Huberf9334412010-12-15 15:17:42 -0800155
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800156 List<sp<Action> > mDeferredActions;
157
Andreas Huberf9334412010-12-15 15:17:42 -0800158 bool mAudioEOS;
159 bool mVideoEOS;
160
Andreas Huber5bc087c2010-12-23 10:27:40 -0800161 bool mScanSourcesPending;
Andreas Huber1aef2112011-01-04 14:01:29 -0800162 int32_t mScanSourcesGeneration;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800163
Andreas Huberb7c8e912012-11-27 15:02:53 -0800164 int32_t mPollDurationGeneration;
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700165 int32_t mTimedTextGeneration;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800166
Andreas Huberf9334412010-12-15 15:17:42 -0800167 enum FlushStatus {
168 NONE,
Andreas Huberf9334412010-12-15 15:17:42 -0800169 FLUSHING_DECODER,
Andreas Huber1aef2112011-01-04 14:01:29 -0800170 FLUSHING_DECODER_SHUTDOWN,
Andreas Huber3831a062010-12-21 10:22:33 -0800171 SHUTTING_DOWN_DECODER,
172 FLUSHED,
173 SHUT_DOWN,
Andreas Huberf9334412010-12-15 15:17:42 -0800174 };
175
Wei Jiafef808d2014-10-31 17:57:05 -0700176 enum FlushCommand {
177 FLUSH_CMD_NONE,
178 FLUSH_CMD_FLUSH,
179 FLUSH_CMD_SHUTDOWN,
180 };
181
Andy Hung8d121d42014-10-03 09:53:53 -0700182 // Status of flush responses from the decoder and renderer.
183 bool mFlushComplete[2][2];
184
Andreas Huberf9334412010-12-15 15:17:42 -0800185 FlushStatus mFlushingAudio;
186 FlushStatus mFlushingVideo;
187
Chong Zhangf8d71772014-11-26 15:08:34 -0800188 // Status of flush responses from the decoder and renderer.
189 bool mResumePending;
190
James Dong0d268a32012-08-31 12:18:27 -0700191 int32_t mVideoScalingMode;
192
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700193 AudioPlaybackRate mPlaybackSettings;
194 AVSyncSettings mSyncSettings;
195 float mVideoFpsHint;
Andreas Huber57a339c2012-12-03 11:18:00 -0800196 bool mStarted;
Robert Shih0c61a0d2015-07-06 15:09:10 -0700197 bool mSourceStarted;
Andreas Huber57a339c2012-12-03 11:18:00 -0800198
Chong Zhangefbb6192015-01-30 17:13:27 -0800199 // Actual pause state, either as requested by client or due to buffering.
200 bool mPaused;
201
202 // Pause state as requested by client. Note that if mPausedByClient is
203 // true, mPaused is always true; if mPausedByClient is false, mPaused could
204 // still become true, when we pause internally due to buffering.
205 bool mPausedByClient;
206
Chong Zhang8a048332015-05-06 15:16:28 -0700207 // Pause state as requested by source (internally) due to buffering
208 bool mPausedForBuffering;
209
Chong Zhang7137ec72014-11-12 16:41:05 -0800210 inline const sp<DecoderBase> &getDecoder(bool audio) {
Lajos Molnar87603c02014-08-20 19:25:30 -0700211 return audio ? mAudioDecoder : mVideoDecoder;
212 }
213
Andy Hung8d121d42014-10-03 09:53:53 -0700214 inline void clearFlushComplete() {
215 mFlushComplete[0][0] = false;
216 mFlushComplete[0][1] = false;
217 mFlushComplete[1][0] = false;
218 mFlushComplete[1][1] = false;
219 }
220
Andy Hung202bce12014-12-03 11:47:36 -0800221 void tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo);
Andy Hung282a7e32014-08-14 15:56:34 -0700222 void closeAudioSink();
Wei Jiae4d18c72015-07-13 17:58:11 -0700223 void determineAudioModeChange();
Andy Hung282a7e32014-08-14 15:56:34 -0700224
Chong Zhang7137ec72014-11-12 16:41:05 -0800225 status_t instantiateDecoder(bool audio, sp<DecoderBase> *decoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800226
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700227 status_t onInstantiateSecureDecoders();
228
Chong Zhangced1c2f2014-08-08 15:22:35 -0700229 void updateVideoSize(
230 const sp<AMessage> &inputFormat,
231 const sp<AMessage> &outputFormat = NULL);
232
Chong Zhangdcb89b32013-08-06 09:44:47 -0700233 void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
Andreas Huberf9334412010-12-15 15:17:42 -0800234
Andy Hung8d121d42014-10-03 09:53:53 -0700235 void handleFlushComplete(bool audio, bool isDecoder);
Andreas Huber3831a062010-12-21 10:22:33 -0800236 void finishFlushIfPossible();
237
Wei Jia1061c9c2015-05-19 16:02:17 -0700238 void onStart(int64_t startPositionUs = -1);
Wei Jia94211742014-10-28 17:09:06 -0700239 void onResume();
Chong Zhangefbb6192015-01-30 17:13:27 -0800240 void onPause();
Wei Jia94211742014-10-28 17:09:06 -0700241
Ronghua Wud7988b12014-10-03 15:19:10 -0700242 bool audioDecoderStillNeeded();
243
Chong Zhang7137ec72014-11-12 16:41:05 -0800244 void flushDecoder(bool audio, bool needShutdown);
Andreas Huber1aef2112011-01-04 14:01:29 -0800245
Chong Zhangf8d71772014-11-26 15:08:34 -0800246 void finishResume();
Wei Jia1061c9c2015-05-19 16:02:17 -0700247 void notifyDriverSeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -0800248
Andreas Huber1aef2112011-01-04 14:01:29 -0800249 void postScanSources();
Andreas Huber53df1a42010-12-22 10:03:04 -0800250
Andreas Huberb7c8e912012-11-27 15:02:53 -0800251 void schedulePollDuration();
252 void cancelPollDuration();
253
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800254 void processDeferredActions();
255
Wei Jia29840802015-05-15 17:11:38 -0700256 void performSeek(int64_t seekTimeUs);
Wei Jiafef808d2014-10-31 17:57:05 -0700257 void performDecoderFlush(FlushCommand audio, FlushCommand video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800258 void performReset();
259 void performScanSources();
Lajos Molnar1de1e252015-04-30 18:18:34 -0700260 void performSetSurface(const sp<Surface> &wrapper);
Chong Zhangf8d71772014-11-26 15:08:34 -0800261 void performResumeDecoders(bool needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800262
Andreas Huber9575c962013-02-05 13:59:56 -0800263 void onSourceNotify(const sp<AMessage> &msg);
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700264 void onClosedCaptionNotify(const sp<AMessage> &msg);
Andreas Huber9575c962013-02-05 13:59:56 -0800265
Andreas Huber14f76722013-01-15 09:04:18 -0800266 void queueDecoderShutdown(
267 bool audio, bool video, const sp<AMessage> &reply);
268
Chong Zhang404fced2014-06-11 14:45:31 -0700269 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
Robert Shih08528432015-04-08 09:06:54 -0700270 void sendTimedMetaData(const sp<ABuffer> &buffer);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700271 void sendTimedTextData(const sp<ABuffer> &buffer);
Chong Zhang404fced2014-06-11 14:45:31 -0700272
273 void writeTrackInfo(Parcel* reply, const sp<AMessage> format) const;
274
Andreas Huberf9334412010-12-15 15:17:42 -0800275 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
276};
277
278} // namespace android
279
280#endif // NU_PLAYER_H_