blob: 2bc20d7afb184b75b7e3a0b7476e08bbb940c8c9 [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
21#include <media/MediaPlayerInterface.h>
22#include <media/stagefright/foundation/AHandler.h>
Glenn Kasten11731182011-02-08 17:26:17 -080023#include <media/stagefright/NativeWindowWrapper.h>
Andreas Huberf9334412010-12-15 15:17:42 -080024
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;
Bernhard Rosenkränzer86a58ca2014-11-17 20:14:08 +010029class MetaData;
Andreas Huber43c3e6c2011-01-05 12:17:08 -080030struct NuPlayerDriver;
Andreas Huberf9334412010-12-15 15:17:42 -080031
32struct NuPlayer : public AHandler {
33 NuPlayer();
34
Andreas Huber9b80c2b2011-06-30 15:47:02 -070035 void setUID(uid_t uid);
36
Andreas Huber43c3e6c2011-01-05 12:17:08 -080037 void setDriver(const wp<NuPlayerDriver> &driver);
Andreas Huberf9334412010-12-15 15:17:42 -080038
Andreas Huber9575c962013-02-05 13:59:56 -080039 void setDataSourceAsync(const sp<IStreamSource> &source);
Andreas Huber5bc087c2010-12-23 10:27:40 -080040
Andreas Huber9575c962013-02-05 13:59:56 -080041 void setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -080042 const sp<IMediaHTTPService> &httpService,
43 const char *url,
44 const KeyedVector<String8, String8> *headers);
Andreas Huber5bc087c2010-12-23 10:27:40 -080045
Andreas Huber9575c962013-02-05 13:59:56 -080046 void setDataSourceAsync(int fd, int64_t offset, int64_t length);
47
48 void prepareAsync();
Andreas Huberafed0e12011-09-20 15:39:58 -070049
Andreas Huber57a339c2012-12-03 11:18:00 -080050 void setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -080051 const sp<IGraphicBufferProducer> &bufferProducer);
Andreas Huber57a339c2012-12-03 11:18:00 -080052
Andreas Huberf9334412010-12-15 15:17:42 -080053 void setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink);
Wei Jia98160162015-02-04 17:01:11 -080054 void setPlaybackRate(float rate);
Andreas Huberf9334412010-12-15 15:17:42 -080055 void start();
56
Andreas Huber43c3e6c2011-01-05 12:17:08 -080057 void pause();
Andreas Huber43c3e6c2011-01-05 12:17:08 -080058
59 // Will notify the driver through "notifyResetComplete" once finished.
Andreas Huber1aef2112011-01-04 14:01:29 -080060 void resetAsync();
61
Wei Jiae427abf2014-09-22 15:21:11 -070062 // Will notify the driver through "notifySeekComplete" once finished
63 // and needNotify is true.
64 void seekToAsync(int64_t seekTimeUs, bool needNotify = false);
Andreas Huber43c3e6c2011-01-05 12:17:08 -080065
James Dong0d268a32012-08-31 12:18:27 -070066 status_t setVideoScalingMode(int32_t mode);
Chong Zhangdcb89b32013-08-06 09:44:47 -070067 status_t getTrackInfo(Parcel* reply) const;
Robert Shih7c4f0d72014-07-09 18:53:31 -070068 status_t getSelectedTrack(int32_t type, Parcel* reply) const;
Robert Shih6ffb1fd2014-10-29 16:24:32 -070069 status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs);
Ronghua Wua73d9e02014-10-08 15:13:29 -070070 status_t getCurrentPosition(int64_t *mediaUs);
71 void getStats(int64_t *mNumFramesTotal, int64_t *mNumFramesDropped);
James Dong0d268a32012-08-31 12:18:27 -070072
Marco Nelissenf0b72b52014-09-16 15:43:44 -070073 sp<MetaData> getFileMeta();
74
Andreas Huberf9334412010-12-15 15:17:42 -080075protected:
76 virtual ~NuPlayer();
77
78 virtual void onMessageReceived(const sp<AMessage> &msg);
79
Andreas Huber84066782011-08-16 09:34:26 -070080public:
81 struct NuPlayerStreamListener;
82 struct Source;
83
Andreas Huberf9334412010-12-15 15:17:42 -080084private:
Andreas Huberf9334412010-12-15 15:17:42 -080085 struct Decoder;
Chong Zhang7137ec72014-11-12 16:41:05 -080086 struct DecoderBase;
Wei Jiabc2fb722014-07-08 16:37:57 -070087 struct DecoderPassThrough;
Chong Zhanga7fa1d92014-06-11 14:49:23 -070088 struct CCDecoder;
Andreas Huberafed0e12011-09-20 15:39:58 -070089 struct GenericSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -080090 struct HTTPLiveSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -080091 struct Renderer;
Andreas Huberafed0e12011-09-20 15:39:58 -070092 struct RTSPSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -080093 struct StreamingSource;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080094 struct Action;
95 struct SeekAction;
Andreas Huber57a339c2012-12-03 11:18:00 -080096 struct SetSurfaceAction;
Chong Zhangf8d71772014-11-26 15:08:34 -080097 struct ResumeDecoderAction;
Wei Jiafef808d2014-10-31 17:57:05 -070098 struct FlushDecoderAction;
Andreas Huber14f76722013-01-15 09:04:18 -080099 struct PostMessageAction;
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800100 struct SimpleAction;
Andreas Huberf9334412010-12-15 15:17:42 -0800101
102 enum {
Andreas Huber078cfcf2011-09-15 12:25:04 -0700103 kWhatSetDataSource = '=DaS',
Andreas Huber9575c962013-02-05 13:59:56 -0800104 kWhatPrepare = 'prep',
Andreas Huber078cfcf2011-09-15 12:25:04 -0700105 kWhatSetVideoNativeWindow = '=NaW',
106 kWhatSetAudioSink = '=AuS',
107 kWhatMoreDataQueued = 'more',
Wei Jia98160162015-02-04 17:01:11 -0800108 kWhatSetRate = 'setR',
Andreas Huber078cfcf2011-09-15 12:25:04 -0700109 kWhatStart = 'strt',
110 kWhatScanSources = 'scan',
111 kWhatVideoNotify = 'vidN',
112 kWhatAudioNotify = 'audN',
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700113 kWhatClosedCaptionNotify = 'capN',
Andreas Huber078cfcf2011-09-15 12:25:04 -0700114 kWhatRendererNotify = 'renN',
115 kWhatReset = 'rset',
116 kWhatSeek = 'seek',
117 kWhatPause = 'paus',
118 kWhatResume = 'rsme',
Andreas Huberb7c8e912012-11-27 15:02:53 -0800119 kWhatPollDuration = 'polD',
Andreas Huberb5f25f02013-02-05 10:14:26 -0800120 kWhatSourceNotify = 'srcN',
Chong Zhangdcb89b32013-08-06 09:44:47 -0700121 kWhatGetTrackInfo = 'gTrI',
Robert Shih7c4f0d72014-07-09 18:53:31 -0700122 kWhatGetSelectedTrack = 'gSel',
Chong Zhangdcb89b32013-08-06 09:44:47 -0700123 kWhatSelectTrack = 'selT',
Andreas Huberf9334412010-12-15 15:17:42 -0800124 };
125
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800126 wp<NuPlayerDriver> mDriver;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700127 bool mUIDValid;
128 uid_t mUID;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800129 sp<Source> mSource;
Andreas Huber9575c962013-02-05 13:59:56 -0800130 uint32_t mSourceFlags;
Glenn Kasten11731182011-02-08 17:26:17 -0800131 sp<NativeWindowWrapper> mNativeWindow;
Andreas Huberf9334412010-12-15 15:17:42 -0800132 sp<MediaPlayerBase::AudioSink> mAudioSink;
Chong Zhang7137ec72014-11-12 16:41:05 -0800133 sp<DecoderBase> mVideoDecoder;
Wei Jiabc2fb722014-07-08 16:37:57 -0700134 bool mOffloadAudio;
Chong Zhang7137ec72014-11-12 16:41:05 -0800135 sp<DecoderBase> mAudioDecoder;
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700136 sp<CCDecoder> mCCDecoder;
Andreas Huberf9334412010-12-15 15:17:42 -0800137 sp<Renderer> mRenderer;
Lajos Molnar09524832014-07-17 14:29:51 -0700138 sp<ALooper> mRendererLooper;
Wei Jia88703c32014-08-06 11:24:07 -0700139 int32_t mAudioDecoderGeneration;
140 int32_t mVideoDecoderGeneration;
Wei Jia57568df2014-09-22 10:16:29 -0700141 int32_t mRendererGeneration;
Andreas Huberf9334412010-12-15 15:17:42 -0800142
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800143 List<sp<Action> > mDeferredActions;
144
Andreas Huberf9334412010-12-15 15:17:42 -0800145 bool mAudioEOS;
146 bool mVideoEOS;
147
Andreas Huber5bc087c2010-12-23 10:27:40 -0800148 bool mScanSourcesPending;
Andreas Huber1aef2112011-01-04 14:01:29 -0800149 int32_t mScanSourcesGeneration;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800150
Andreas Huberb7c8e912012-11-27 15:02:53 -0800151 int32_t mPollDurationGeneration;
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700152 int32_t mTimedTextGeneration;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800153
Andreas Huberf9334412010-12-15 15:17:42 -0800154 enum FlushStatus {
155 NONE,
Andreas Huberf9334412010-12-15 15:17:42 -0800156 FLUSHING_DECODER,
Andreas Huber1aef2112011-01-04 14:01:29 -0800157 FLUSHING_DECODER_SHUTDOWN,
Andreas Huber3831a062010-12-21 10:22:33 -0800158 SHUTTING_DOWN_DECODER,
159 FLUSHED,
160 SHUT_DOWN,
Andreas Huberf9334412010-12-15 15:17:42 -0800161 };
162
Wei Jiafef808d2014-10-31 17:57:05 -0700163 enum FlushCommand {
164 FLUSH_CMD_NONE,
165 FLUSH_CMD_FLUSH,
166 FLUSH_CMD_SHUTDOWN,
167 };
168
Andy Hung8d121d42014-10-03 09:53:53 -0700169 // Status of flush responses from the decoder and renderer.
170 bool mFlushComplete[2][2];
171
Andreas Huberf9334412010-12-15 15:17:42 -0800172 FlushStatus mFlushingAudio;
173 FlushStatus mFlushingVideo;
174
Chong Zhangf8d71772014-11-26 15:08:34 -0800175 // Status of flush responses from the decoder and renderer.
176 bool mResumePending;
177
James Dong0d268a32012-08-31 12:18:27 -0700178 int32_t mVideoScalingMode;
179
Wei Jia98160162015-02-04 17:01:11 -0800180 float mPlaybackRate;
Andreas Huber57a339c2012-12-03 11:18:00 -0800181 bool mStarted;
182
Chong Zhangefbb6192015-01-30 17:13:27 -0800183 // Actual pause state, either as requested by client or due to buffering.
184 bool mPaused;
185
186 // Pause state as requested by client. Note that if mPausedByClient is
187 // true, mPaused is always true; if mPausedByClient is false, mPaused could
188 // still become true, when we pause internally due to buffering.
189 bool mPausedByClient;
190
Chong Zhang7137ec72014-11-12 16:41:05 -0800191 inline const sp<DecoderBase> &getDecoder(bool audio) {
Lajos Molnar87603c02014-08-20 19:25:30 -0700192 return audio ? mAudioDecoder : mVideoDecoder;
193 }
194
Andy Hung8d121d42014-10-03 09:53:53 -0700195 inline void clearFlushComplete() {
196 mFlushComplete[0][0] = false;
197 mFlushComplete[0][1] = false;
198 mFlushComplete[1][0] = false;
199 mFlushComplete[1][1] = false;
200 }
201
Andy Hung202bce12014-12-03 11:47:36 -0800202 void tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo);
Andy Hung282a7e32014-08-14 15:56:34 -0700203 void closeAudioSink();
204
Chong Zhang7137ec72014-11-12 16:41:05 -0800205 status_t instantiateDecoder(bool audio, sp<DecoderBase> *decoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800206
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700207 status_t onInstantiateSecureDecoders();
208
Chong Zhangced1c2f2014-08-08 15:22:35 -0700209 void updateVideoSize(
210 const sp<AMessage> &inputFormat,
211 const sp<AMessage> &outputFormat = NULL);
212
Chong Zhangdcb89b32013-08-06 09:44:47 -0700213 void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
Andreas Huberf9334412010-12-15 15:17:42 -0800214
Andy Hung8d121d42014-10-03 09:53:53 -0700215 void handleFlushComplete(bool audio, bool isDecoder);
Andreas Huber3831a062010-12-21 10:22:33 -0800216 void finishFlushIfPossible();
217
Wei Jia94211742014-10-28 17:09:06 -0700218 void onStart();
219 void onResume();
Chong Zhangefbb6192015-01-30 17:13:27 -0800220 void onPause();
Wei Jia94211742014-10-28 17:09:06 -0700221
Ronghua Wud7988b12014-10-03 15:19:10 -0700222 bool audioDecoderStillNeeded();
223
Chong Zhang7137ec72014-11-12 16:41:05 -0800224 void flushDecoder(bool audio, bool needShutdown);
Andreas Huber1aef2112011-01-04 14:01:29 -0800225
Chong Zhangf8d71772014-11-26 15:08:34 -0800226 void finishResume();
227
Andreas Huber1aef2112011-01-04 14:01:29 -0800228 void postScanSources();
Andreas Huber53df1a42010-12-22 10:03:04 -0800229
Andreas Huberb7c8e912012-11-27 15:02:53 -0800230 void schedulePollDuration();
231 void cancelPollDuration();
232
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800233 void processDeferredActions();
234
Wei Jiae427abf2014-09-22 15:21:11 -0700235 void performSeek(int64_t seekTimeUs, bool needNotify);
Wei Jiafef808d2014-10-31 17:57:05 -0700236 void performDecoderFlush(FlushCommand audio, FlushCommand video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800237 void performReset();
238 void performScanSources();
Andreas Huber57a339c2012-12-03 11:18:00 -0800239 void performSetSurface(const sp<NativeWindowWrapper> &wrapper);
Chong Zhangf8d71772014-11-26 15:08:34 -0800240 void performResumeDecoders(bool needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800241
Andreas Huber9575c962013-02-05 13:59:56 -0800242 void onSourceNotify(const sp<AMessage> &msg);
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700243 void onClosedCaptionNotify(const sp<AMessage> &msg);
Andreas Huber9575c962013-02-05 13:59:56 -0800244
Andreas Huber14f76722013-01-15 09:04:18 -0800245 void queueDecoderShutdown(
246 bool audio, bool video, const sp<AMessage> &reply);
247
Chong Zhang404fced2014-06-11 14:45:31 -0700248 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700249 void sendTimedTextData(const sp<ABuffer> &buffer);
Chong Zhang404fced2014-06-11 14:45:31 -0700250
251 void writeTrackInfo(Parcel* reply, const sp<AMessage> format) const;
252
Andreas Huberf9334412010-12-15 15:17:42 -0800253 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
254};
255
256} // namespace android
257
258#endif // NU_PLAYER_H_