blob: a002f6f308aaff97d26a92ef3ef8b5329a8e85cb [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 {
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -070036 explicit NuPlayer(pid_t pid);
Andreas Huberf9334412010-12-15 15:17:42 -080037
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;
Ronghua Wu68845c12015-07-21 09:50:48 -0700142 pid_t mPID;
Wei Jiac45a4b22016-04-15 15:30:23 -0700143 Mutex mSourceLock; // guard |mSource|.
Andreas Huber5bc087c2010-12-23 10:27:40 -0800144 sp<Source> mSource;
Andreas Huber9575c962013-02-05 13:59:56 -0800145 uint32_t mSourceFlags;
Lajos Molnar1de1e252015-04-30 18:18:34 -0700146 sp<Surface> mSurface;
Andreas Huberf9334412010-12-15 15:17:42 -0800147 sp<MediaPlayerBase::AudioSink> mAudioSink;
Chong Zhang7137ec72014-11-12 16:41:05 -0800148 sp<DecoderBase> mVideoDecoder;
Wei Jiabc2fb722014-07-08 16:37:57 -0700149 bool mOffloadAudio;
Chong Zhang7137ec72014-11-12 16:41:05 -0800150 sp<DecoderBase> mAudioDecoder;
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700151 sp<CCDecoder> mCCDecoder;
Andreas Huberf9334412010-12-15 15:17:42 -0800152 sp<Renderer> mRenderer;
Lajos Molnar09524832014-07-17 14:29:51 -0700153 sp<ALooper> mRendererLooper;
Wei Jia88703c32014-08-06 11:24:07 -0700154 int32_t mAudioDecoderGeneration;
155 int32_t mVideoDecoderGeneration;
Wei Jia57568df2014-09-22 10:16:29 -0700156 int32_t mRendererGeneration;
Andreas Huberf9334412010-12-15 15:17:42 -0800157
Robert Shih1a5c8592015-08-04 18:07:44 -0700158 int64_t mPreviousSeekTimeUs;
159
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800160 List<sp<Action> > mDeferredActions;
161
Andreas Huberf9334412010-12-15 15:17:42 -0800162 bool mAudioEOS;
163 bool mVideoEOS;
164
Andreas Huber5bc087c2010-12-23 10:27:40 -0800165 bool mScanSourcesPending;
Andreas Huber1aef2112011-01-04 14:01:29 -0800166 int32_t mScanSourcesGeneration;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800167
Andreas Huberb7c8e912012-11-27 15:02:53 -0800168 int32_t mPollDurationGeneration;
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700169 int32_t mTimedTextGeneration;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800170
Andreas Huberf9334412010-12-15 15:17:42 -0800171 enum FlushStatus {
172 NONE,
Andreas Huberf9334412010-12-15 15:17:42 -0800173 FLUSHING_DECODER,
Andreas Huber1aef2112011-01-04 14:01:29 -0800174 FLUSHING_DECODER_SHUTDOWN,
Andreas Huber3831a062010-12-21 10:22:33 -0800175 SHUTTING_DOWN_DECODER,
176 FLUSHED,
177 SHUT_DOWN,
Andreas Huberf9334412010-12-15 15:17:42 -0800178 };
179
Wei Jiafef808d2014-10-31 17:57:05 -0700180 enum FlushCommand {
181 FLUSH_CMD_NONE,
182 FLUSH_CMD_FLUSH,
183 FLUSH_CMD_SHUTDOWN,
184 };
185
Andy Hung8d121d42014-10-03 09:53:53 -0700186 // Status of flush responses from the decoder and renderer.
187 bool mFlushComplete[2][2];
188
Andreas Huberf9334412010-12-15 15:17:42 -0800189 FlushStatus mFlushingAudio;
190 FlushStatus mFlushingVideo;
191
Chong Zhangf8d71772014-11-26 15:08:34 -0800192 // Status of flush responses from the decoder and renderer.
193 bool mResumePending;
194
James Dong0d268a32012-08-31 12:18:27 -0700195 int32_t mVideoScalingMode;
196
Lajos Molnar3a474aa2015-04-24 17:10:07 -0700197 AudioPlaybackRate mPlaybackSettings;
198 AVSyncSettings mSyncSettings;
199 float mVideoFpsHint;
Andreas Huber57a339c2012-12-03 11:18:00 -0800200 bool mStarted;
Wei Jia8a092d32016-06-03 14:57:24 -0700201 bool mPrepared;
Ronghua Wu64c2d172015-10-07 16:52:19 -0700202 bool mResetting;
Robert Shih0c61a0d2015-07-06 15:09:10 -0700203 bool mSourceStarted;
Andreas Huber57a339c2012-12-03 11:18:00 -0800204
Chong Zhangefbb6192015-01-30 17:13:27 -0800205 // Actual pause state, either as requested by client or due to buffering.
206 bool mPaused;
207
208 // Pause state as requested by client. Note that if mPausedByClient is
209 // true, mPaused is always true; if mPausedByClient is false, mPaused could
210 // still become true, when we pause internally due to buffering.
211 bool mPausedByClient;
212
Chong Zhang8a048332015-05-06 15:16:28 -0700213 // Pause state as requested by source (internally) due to buffering
214 bool mPausedForBuffering;
215
Chong Zhang7137ec72014-11-12 16:41:05 -0800216 inline const sp<DecoderBase> &getDecoder(bool audio) {
Lajos Molnar87603c02014-08-20 19:25:30 -0700217 return audio ? mAudioDecoder : mVideoDecoder;
218 }
219
Andy Hung8d121d42014-10-03 09:53:53 -0700220 inline void clearFlushComplete() {
221 mFlushComplete[0][0] = false;
222 mFlushComplete[0][1] = false;
223 mFlushComplete[1][0] = false;
224 mFlushComplete[1][1] = false;
225 }
226
Wei Jia41cd4632016-05-13 10:53:30 -0700227 void tryOpenAudioSinkForOffload(
228 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo);
Andy Hung282a7e32014-08-14 15:56:34 -0700229 void closeAudioSink();
Wei Jia5031b2f2016-02-25 11:19:31 -0800230 void restartAudio(
Wei Jiaa05f1e32016-03-25 16:31:22 -0700231 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder);
Wei Jia41cd4632016-05-13 10:53:30 -0700232 void determineAudioModeChange(const sp<AMessage> &audioFormat);
Andy Hung282a7e32014-08-14 15:56:34 -0700233
Wei Jiaa05f1e32016-03-25 16:31:22 -0700234 status_t instantiateDecoder(
235 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange = true);
Andreas Huberf9334412010-12-15 15:17:42 -0800236
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700237 status_t onInstantiateSecureDecoders();
238
Chong Zhangced1c2f2014-08-08 15:22:35 -0700239 void updateVideoSize(
240 const sp<AMessage> &inputFormat,
241 const sp<AMessage> &outputFormat = NULL);
242
Chong Zhangdcb89b32013-08-06 09:44:47 -0700243 void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
Andreas Huberf9334412010-12-15 15:17:42 -0800244
Andy Hung8d121d42014-10-03 09:53:53 -0700245 void handleFlushComplete(bool audio, bool isDecoder);
Andreas Huber3831a062010-12-21 10:22:33 -0800246 void finishFlushIfPossible();
247
Wei Jia1061c9c2015-05-19 16:02:17 -0700248 void onStart(int64_t startPositionUs = -1);
Wei Jia94211742014-10-28 17:09:06 -0700249 void onResume();
Chong Zhangefbb6192015-01-30 17:13:27 -0800250 void onPause();
Wei Jia94211742014-10-28 17:09:06 -0700251
Ronghua Wud7988b12014-10-03 15:19:10 -0700252 bool audioDecoderStillNeeded();
253
Chong Zhang7137ec72014-11-12 16:41:05 -0800254 void flushDecoder(bool audio, bool needShutdown);
Andreas Huber1aef2112011-01-04 14:01:29 -0800255
Chong Zhangf8d71772014-11-26 15:08:34 -0800256 void finishResume();
Wei Jia1061c9c2015-05-19 16:02:17 -0700257 void notifyDriverSeekComplete();
Chong Zhangf8d71772014-11-26 15:08:34 -0800258
Andreas Huber1aef2112011-01-04 14:01:29 -0800259 void postScanSources();
Andreas Huber53df1a42010-12-22 10:03:04 -0800260
Andreas Huberb7c8e912012-11-27 15:02:53 -0800261 void schedulePollDuration();
262 void cancelPollDuration();
263
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800264 void processDeferredActions();
265
Wei Jia29840802015-05-15 17:11:38 -0700266 void performSeek(int64_t seekTimeUs);
Wei Jiafef808d2014-10-31 17:57:05 -0700267 void performDecoderFlush(FlushCommand audio, FlushCommand video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800268 void performReset();
269 void performScanSources();
Lajos Molnar1de1e252015-04-30 18:18:34 -0700270 void performSetSurface(const sp<Surface> &wrapper);
Chong Zhangf8d71772014-11-26 15:08:34 -0800271 void performResumeDecoders(bool needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800272
Andreas Huber9575c962013-02-05 13:59:56 -0800273 void onSourceNotify(const sp<AMessage> &msg);
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700274 void onClosedCaptionNotify(const sp<AMessage> &msg);
Andreas Huber9575c962013-02-05 13:59:56 -0800275
Andreas Huber14f76722013-01-15 09:04:18 -0800276 void queueDecoderShutdown(
277 bool audio, bool video, const sp<AMessage> &reply);
278
Chong Zhang404fced2014-06-11 14:45:31 -0700279 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
Robert Shih08528432015-04-08 09:06:54 -0700280 void sendTimedMetaData(const sp<ABuffer> &buffer);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700281 void sendTimedTextData(const sp<ABuffer> &buffer);
Chong Zhang404fced2014-06-11 14:45:31 -0700282
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -0700283 void writeTrackInfo(Parcel* reply, const sp<AMessage>& format) const;
Chong Zhang404fced2014-06-11 14:45:31 -0700284
Andreas Huberf9334412010-12-15 15:17:42 -0800285 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
286};
287
288} // namespace android
289
290#endif // NU_PLAYER_H_