blob: 14bdb01b6922482470a73ee5f4ebc238cf5cd19b [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;
Lajos Molnaree4e1b12015-04-17 13:46:19 -070029class IDataSource;
Bernhard Rosenkränzer86a58ca2014-11-17 20:14:08 +010030class MetaData;
Andreas Huber43c3e6c2011-01-05 12:17:08 -080031struct NuPlayerDriver;
Andreas Huberf9334412010-12-15 15:17:42 -080032
33struct NuPlayer : public AHandler {
34 NuPlayer();
35
Andreas Huber9b80c2b2011-06-30 15:47:02 -070036 void setUID(uid_t uid);
37
Andreas Huber43c3e6c2011-01-05 12:17:08 -080038 void setDriver(const wp<NuPlayerDriver> &driver);
Andreas Huberf9334412010-12-15 15:17:42 -080039
Andreas Huber9575c962013-02-05 13:59:56 -080040 void setDataSourceAsync(const sp<IStreamSource> &source);
Andreas Huber5bc087c2010-12-23 10:27:40 -080041
Andreas Huber9575c962013-02-05 13:59:56 -080042 void setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -080043 const sp<IMediaHTTPService> &httpService,
44 const char *url,
45 const KeyedVector<String8, String8> *headers);
Andreas Huber5bc087c2010-12-23 10:27:40 -080046
Andreas Huber9575c962013-02-05 13:59:56 -080047 void setDataSourceAsync(int fd, int64_t offset, int64_t length);
48
Chris Watkins99f31602015-03-20 13:06:33 -070049 void setDataSourceAsync(const sp<DataSource> &source);
50
Andreas Huber9575c962013-02-05 13:59:56 -080051 void prepareAsync();
Andreas Huberafed0e12011-09-20 15:39:58 -070052
Andreas Huber57a339c2012-12-03 11:18:00 -080053 void setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -080054 const sp<IGraphicBufferProducer> &bufferProducer);
Andreas Huber57a339c2012-12-03 11:18:00 -080055
Andreas Huberf9334412010-12-15 15:17:42 -080056 void setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink);
Wei Jia98160162015-02-04 17:01:11 -080057 void setPlaybackRate(float rate);
Andreas Huberf9334412010-12-15 15:17:42 -080058 void start();
59
Andreas Huber43c3e6c2011-01-05 12:17:08 -080060 void pause();
Andreas Huber43c3e6c2011-01-05 12:17:08 -080061
62 // Will notify the driver through "notifyResetComplete" once finished.
Andreas Huber1aef2112011-01-04 14:01:29 -080063 void resetAsync();
64
Wei Jiae427abf2014-09-22 15:21:11 -070065 // Will notify the driver through "notifySeekComplete" once finished
66 // and needNotify is true.
67 void seekToAsync(int64_t seekTimeUs, bool needNotify = false);
Andreas Huber43c3e6c2011-01-05 12:17:08 -080068
James Dong0d268a32012-08-31 12:18:27 -070069 status_t setVideoScalingMode(int32_t mode);
Chong Zhangdcb89b32013-08-06 09:44:47 -070070 status_t getTrackInfo(Parcel* reply) const;
Robert Shih7c4f0d72014-07-09 18:53:31 -070071 status_t getSelectedTrack(int32_t type, Parcel* reply) const;
Robert Shih6ffb1fd2014-10-29 16:24:32 -070072 status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs);
Ronghua Wua73d9e02014-10-08 15:13:29 -070073 status_t getCurrentPosition(int64_t *mediaUs);
74 void getStats(int64_t *mNumFramesTotal, int64_t *mNumFramesDropped);
James Dong0d268a32012-08-31 12:18:27 -070075
Marco Nelissenf0b72b52014-09-16 15:43:44 -070076 sp<MetaData> getFileMeta();
77
Andreas Huberf9334412010-12-15 15:17:42 -080078protected:
79 virtual ~NuPlayer();
80
81 virtual void onMessageReceived(const sp<AMessage> &msg);
82
Andreas Huber84066782011-08-16 09:34:26 -070083public:
84 struct NuPlayerStreamListener;
85 struct Source;
86
Andreas Huberf9334412010-12-15 15:17:42 -080087private:
Andreas Huberf9334412010-12-15 15:17:42 -080088 struct Decoder;
Chong Zhang7137ec72014-11-12 16:41:05 -080089 struct DecoderBase;
Wei Jiabc2fb722014-07-08 16:37:57 -070090 struct DecoderPassThrough;
Chong Zhanga7fa1d92014-06-11 14:49:23 -070091 struct CCDecoder;
Andreas Huberafed0e12011-09-20 15:39:58 -070092 struct GenericSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -080093 struct HTTPLiveSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -080094 struct Renderer;
Andreas Huberafed0e12011-09-20 15:39:58 -070095 struct RTSPSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -080096 struct StreamingSource;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080097 struct Action;
98 struct SeekAction;
Andreas Huber57a339c2012-12-03 11:18:00 -080099 struct SetSurfaceAction;
Chong Zhangf8d71772014-11-26 15:08:34 -0800100 struct ResumeDecoderAction;
Wei Jiafef808d2014-10-31 17:57:05 -0700101 struct FlushDecoderAction;
Andreas Huber14f76722013-01-15 09:04:18 -0800102 struct PostMessageAction;
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800103 struct SimpleAction;
Andreas Huberf9334412010-12-15 15:17:42 -0800104
105 enum {
Andreas Huber078cfcf2011-09-15 12:25:04 -0700106 kWhatSetDataSource = '=DaS',
Andreas Huber9575c962013-02-05 13:59:56 -0800107 kWhatPrepare = 'prep',
Andreas Huber078cfcf2011-09-15 12:25:04 -0700108 kWhatSetVideoNativeWindow = '=NaW',
109 kWhatSetAudioSink = '=AuS',
110 kWhatMoreDataQueued = 'more',
Wei Jia98160162015-02-04 17:01:11 -0800111 kWhatSetRate = 'setR',
Andreas Huber078cfcf2011-09-15 12:25:04 -0700112 kWhatStart = 'strt',
113 kWhatScanSources = 'scan',
114 kWhatVideoNotify = 'vidN',
115 kWhatAudioNotify = 'audN',
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700116 kWhatClosedCaptionNotify = 'capN',
Andreas Huber078cfcf2011-09-15 12:25:04 -0700117 kWhatRendererNotify = 'renN',
118 kWhatReset = 'rset',
119 kWhatSeek = 'seek',
120 kWhatPause = 'paus',
121 kWhatResume = 'rsme',
Andreas Huberb7c8e912012-11-27 15:02:53 -0800122 kWhatPollDuration = 'polD',
Andreas Huberb5f25f02013-02-05 10:14:26 -0800123 kWhatSourceNotify = 'srcN',
Chong Zhangdcb89b32013-08-06 09:44:47 -0700124 kWhatGetTrackInfo = 'gTrI',
Robert Shih7c4f0d72014-07-09 18:53:31 -0700125 kWhatGetSelectedTrack = 'gSel',
Chong Zhangdcb89b32013-08-06 09:44:47 -0700126 kWhatSelectTrack = 'selT',
Andreas Huberf9334412010-12-15 15:17:42 -0800127 };
128
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800129 wp<NuPlayerDriver> mDriver;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700130 bool mUIDValid;
131 uid_t mUID;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800132 sp<Source> mSource;
Andreas Huber9575c962013-02-05 13:59:56 -0800133 uint32_t mSourceFlags;
Glenn Kasten11731182011-02-08 17:26:17 -0800134 sp<NativeWindowWrapper> mNativeWindow;
Andreas Huberf9334412010-12-15 15:17:42 -0800135 sp<MediaPlayerBase::AudioSink> mAudioSink;
Chong Zhang7137ec72014-11-12 16:41:05 -0800136 sp<DecoderBase> mVideoDecoder;
Wei Jiabc2fb722014-07-08 16:37:57 -0700137 bool mOffloadAudio;
Chong Zhang7137ec72014-11-12 16:41:05 -0800138 sp<DecoderBase> mAudioDecoder;
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700139 sp<CCDecoder> mCCDecoder;
Andreas Huberf9334412010-12-15 15:17:42 -0800140 sp<Renderer> mRenderer;
Lajos Molnar09524832014-07-17 14:29:51 -0700141 sp<ALooper> mRendererLooper;
Wei Jia88703c32014-08-06 11:24:07 -0700142 int32_t mAudioDecoderGeneration;
143 int32_t mVideoDecoderGeneration;
Wei Jia57568df2014-09-22 10:16:29 -0700144 int32_t mRendererGeneration;
Andreas Huberf9334412010-12-15 15:17:42 -0800145
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800146 List<sp<Action> > mDeferredActions;
147
Andreas Huberf9334412010-12-15 15:17:42 -0800148 bool mAudioEOS;
149 bool mVideoEOS;
150
Andreas Huber5bc087c2010-12-23 10:27:40 -0800151 bool mScanSourcesPending;
Andreas Huber1aef2112011-01-04 14:01:29 -0800152 int32_t mScanSourcesGeneration;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800153
Andreas Huberb7c8e912012-11-27 15:02:53 -0800154 int32_t mPollDurationGeneration;
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700155 int32_t mTimedTextGeneration;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800156
Andreas Huberf9334412010-12-15 15:17:42 -0800157 enum FlushStatus {
158 NONE,
Andreas Huberf9334412010-12-15 15:17:42 -0800159 FLUSHING_DECODER,
Andreas Huber1aef2112011-01-04 14:01:29 -0800160 FLUSHING_DECODER_SHUTDOWN,
Andreas Huber3831a062010-12-21 10:22:33 -0800161 SHUTTING_DOWN_DECODER,
162 FLUSHED,
163 SHUT_DOWN,
Andreas Huberf9334412010-12-15 15:17:42 -0800164 };
165
Wei Jiafef808d2014-10-31 17:57:05 -0700166 enum FlushCommand {
167 FLUSH_CMD_NONE,
168 FLUSH_CMD_FLUSH,
169 FLUSH_CMD_SHUTDOWN,
170 };
171
Andy Hung8d121d42014-10-03 09:53:53 -0700172 // Status of flush responses from the decoder and renderer.
173 bool mFlushComplete[2][2];
174
Andreas Huberf9334412010-12-15 15:17:42 -0800175 FlushStatus mFlushingAudio;
176 FlushStatus mFlushingVideo;
177
Chong Zhangf8d71772014-11-26 15:08:34 -0800178 // Status of flush responses from the decoder and renderer.
179 bool mResumePending;
180
James Dong0d268a32012-08-31 12:18:27 -0700181 int32_t mVideoScalingMode;
182
Wei Jia98160162015-02-04 17:01:11 -0800183 float mPlaybackRate;
Andreas Huber57a339c2012-12-03 11:18:00 -0800184 bool mStarted;
185
Chong Zhangefbb6192015-01-30 17:13:27 -0800186 // Actual pause state, either as requested by client or due to buffering.
187 bool mPaused;
188
189 // Pause state as requested by client. Note that if mPausedByClient is
190 // true, mPaused is always true; if mPausedByClient is false, mPaused could
191 // still become true, when we pause internally due to buffering.
192 bool mPausedByClient;
193
Chong Zhang7137ec72014-11-12 16:41:05 -0800194 inline const sp<DecoderBase> &getDecoder(bool audio) {
Lajos Molnar87603c02014-08-20 19:25:30 -0700195 return audio ? mAudioDecoder : mVideoDecoder;
196 }
197
Andy Hung8d121d42014-10-03 09:53:53 -0700198 inline void clearFlushComplete() {
199 mFlushComplete[0][0] = false;
200 mFlushComplete[0][1] = false;
201 mFlushComplete[1][0] = false;
202 mFlushComplete[1][1] = false;
203 }
204
Andy Hung202bce12014-12-03 11:47:36 -0800205 void tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo);
Andy Hung282a7e32014-08-14 15:56:34 -0700206 void closeAudioSink();
207
Chong Zhang7137ec72014-11-12 16:41:05 -0800208 status_t instantiateDecoder(bool audio, sp<DecoderBase> *decoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800209
Lajos Molnarfcd3e942015-03-31 10:06:48 -0700210 status_t onInstantiateSecureDecoders();
211
Chong Zhangced1c2f2014-08-08 15:22:35 -0700212 void updateVideoSize(
213 const sp<AMessage> &inputFormat,
214 const sp<AMessage> &outputFormat = NULL);
215
Chong Zhangdcb89b32013-08-06 09:44:47 -0700216 void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
Andreas Huberf9334412010-12-15 15:17:42 -0800217
Andy Hung8d121d42014-10-03 09:53:53 -0700218 void handleFlushComplete(bool audio, bool isDecoder);
Andreas Huber3831a062010-12-21 10:22:33 -0800219 void finishFlushIfPossible();
220
Wei Jia94211742014-10-28 17:09:06 -0700221 void onStart();
222 void onResume();
Chong Zhangefbb6192015-01-30 17:13:27 -0800223 void onPause();
Wei Jia94211742014-10-28 17:09:06 -0700224
Ronghua Wud7988b12014-10-03 15:19:10 -0700225 bool audioDecoderStillNeeded();
226
Chong Zhang7137ec72014-11-12 16:41:05 -0800227 void flushDecoder(bool audio, bool needShutdown);
Andreas Huber1aef2112011-01-04 14:01:29 -0800228
Chong Zhangf8d71772014-11-26 15:08:34 -0800229 void finishResume();
230
Andreas Huber1aef2112011-01-04 14:01:29 -0800231 void postScanSources();
Andreas Huber53df1a42010-12-22 10:03:04 -0800232
Andreas Huberb7c8e912012-11-27 15:02:53 -0800233 void schedulePollDuration();
234 void cancelPollDuration();
235
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800236 void processDeferredActions();
237
Wei Jiae427abf2014-09-22 15:21:11 -0700238 void performSeek(int64_t seekTimeUs, bool needNotify);
Wei Jiafef808d2014-10-31 17:57:05 -0700239 void performDecoderFlush(FlushCommand audio, FlushCommand video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800240 void performReset();
241 void performScanSources();
Andreas Huber57a339c2012-12-03 11:18:00 -0800242 void performSetSurface(const sp<NativeWindowWrapper> &wrapper);
Chong Zhangf8d71772014-11-26 15:08:34 -0800243 void performResumeDecoders(bool needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800244
Andreas Huber9575c962013-02-05 13:59:56 -0800245 void onSourceNotify(const sp<AMessage> &msg);
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700246 void onClosedCaptionNotify(const sp<AMessage> &msg);
Andreas Huber9575c962013-02-05 13:59:56 -0800247
Andreas Huber14f76722013-01-15 09:04:18 -0800248 void queueDecoderShutdown(
249 bool audio, bool video, const sp<AMessage> &reply);
250
Chong Zhang404fced2014-06-11 14:45:31 -0700251 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
Robert Shih08528432015-04-08 09:06:54 -0700252 void sendTimedMetaData(const sp<ABuffer> &buffer);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700253 void sendTimedTextData(const sp<ABuffer> &buffer);
Chong Zhang404fced2014-06-11 14:45:31 -0700254
255 void writeTrackInfo(Parcel* reply, const sp<AMessage> format) const;
256
Andreas Huberf9334412010-12-15 15:17:42 -0800257 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
258};
259
260} // namespace android
261
262#endif // NU_PLAYER_H_