blob: 24c06c96e7b683f70df162aa9d89891f4a36c1e5 [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);
54 void start();
55
Andreas Huber43c3e6c2011-01-05 12:17:08 -080056 void pause();
57 void resume();
58
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;
Chong Zhangdcb89b32013-08-06 09:44:47 -070069 status_t selectTrack(size_t trackIndex, bool select);
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
Phil Burkc5cc2e22014-09-09 20:08:39 -070075 static const size_t kAggregateBufferSizeBytes;
76
Andreas Huberf9334412010-12-15 15:17:42 -080077protected:
78 virtual ~NuPlayer();
79
80 virtual void onMessageReceived(const sp<AMessage> &msg);
81
Andreas Huber84066782011-08-16 09:34:26 -070082public:
83 struct NuPlayerStreamListener;
84 struct Source;
85
Andreas Huberf9334412010-12-15 15:17:42 -080086private:
Andreas Huberf9334412010-12-15 15:17:42 -080087 struct Decoder;
Wei Jiabc2fb722014-07-08 16:37:57 -070088 struct DecoderPassThrough;
Chong Zhanga7fa1d92014-06-11 14:49:23 -070089 struct CCDecoder;
Andreas Huberafed0e12011-09-20 15:39:58 -070090 struct GenericSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -080091 struct HTTPLiveSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -080092 struct Renderer;
Andreas Huberafed0e12011-09-20 15:39:58 -070093 struct RTSPSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -080094 struct StreamingSource;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080095 struct Action;
96 struct SeekAction;
Andreas Huber57a339c2012-12-03 11:18:00 -080097 struct SetSurfaceAction;
Andreas Huber14f76722013-01-15 09:04:18 -080098 struct ShutdownDecoderAction;
99 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',
108 kWhatStart = 'strt',
109 kWhatScanSources = 'scan',
110 kWhatVideoNotify = 'vidN',
111 kWhatAudioNotify = 'audN',
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700112 kWhatClosedCaptionNotify = 'capN',
Andreas Huber078cfcf2011-09-15 12:25:04 -0700113 kWhatRendererNotify = 'renN',
114 kWhatReset = 'rset',
115 kWhatSeek = 'seek',
116 kWhatPause = 'paus',
117 kWhatResume = 'rsme',
Andreas Huberb7c8e912012-11-27 15:02:53 -0800118 kWhatPollDuration = 'polD',
Andreas Huberb5f25f02013-02-05 10:14:26 -0800119 kWhatSourceNotify = 'srcN',
Chong Zhangdcb89b32013-08-06 09:44:47 -0700120 kWhatGetTrackInfo = 'gTrI',
Robert Shih7c4f0d72014-07-09 18:53:31 -0700121 kWhatGetSelectedTrack = 'gSel',
Chong Zhangdcb89b32013-08-06 09:44:47 -0700122 kWhatSelectTrack = 'selT',
Andreas Huberf9334412010-12-15 15:17:42 -0800123 };
124
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800125 wp<NuPlayerDriver> mDriver;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700126 bool mUIDValid;
127 uid_t mUID;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800128 sp<Source> mSource;
Andreas Huber9575c962013-02-05 13:59:56 -0800129 uint32_t mSourceFlags;
Glenn Kasten11731182011-02-08 17:26:17 -0800130 sp<NativeWindowWrapper> mNativeWindow;
Andreas Huberf9334412010-12-15 15:17:42 -0800131 sp<MediaPlayerBase::AudioSink> mAudioSink;
Andreas Huberf9334412010-12-15 15:17:42 -0800132 sp<Decoder> mVideoDecoder;
Andreas Huber3fe62152011-09-16 15:09:22 -0700133 bool mVideoIsAVC;
Wei Jiabc2fb722014-07-08 16:37:57 -0700134 bool mOffloadAudio;
Andreas Huberf9334412010-12-15 15:17:42 -0800135 sp<Decoder> 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
Andreas Huber6e3d3112011-11-28 12:36:11 -0800163 // Once the current flush is complete this indicates whether the
164 // notion of time has changed.
165 bool mTimeDiscontinuityPending;
166
Andy Hung8d121d42014-10-03 09:53:53 -0700167 // Status of flush responses from the decoder and renderer.
168 bool mFlushComplete[2][2];
169
Phil Burkc5cc2e22014-09-09 20:08:39 -0700170 // Used by feedDecoderInputData to aggregate small buffers into
171 // one large buffer.
Phil Burk9f526492014-09-03 15:04:12 -0700172 sp<ABuffer> mPendingAudioAccessUnit;
173 status_t mPendingAudioErr;
Phil Burkc5cc2e22014-09-09 20:08:39 -0700174 sp<ABuffer> mAggregateBuffer;
Phil Burk9f526492014-09-03 15:04:12 -0700175
Andreas Huberf9334412010-12-15 15:17:42 -0800176 FlushStatus mFlushingAudio;
177 FlushStatus mFlushingVideo;
178
Andreas Huber32f3cef2011-03-02 15:34:46 -0800179 int64_t mSkipRenderingAudioUntilMediaTimeUs;
180 int64_t mSkipRenderingVideoUntilMediaTimeUs;
181
Andreas Huber3fe62152011-09-16 15:09:22 -0700182 int64_t mNumFramesTotal, mNumFramesDropped;
183
James Dong0d268a32012-08-31 12:18:27 -0700184 int32_t mVideoScalingMode;
185
Andreas Huber57a339c2012-12-03 11:18:00 -0800186 bool mStarted;
187
Lajos Molnar87603c02014-08-20 19:25:30 -0700188 inline const sp<Decoder> &getDecoder(bool audio) {
189 return audio ? mAudioDecoder : mVideoDecoder;
190 }
191
Andy Hung8d121d42014-10-03 09:53:53 -0700192 inline void clearFlushComplete() {
193 mFlushComplete[0][0] = false;
194 mFlushComplete[0][1] = false;
195 mFlushComplete[1][0] = false;
196 mFlushComplete[1][1] = false;
197 }
198
Andy Hung282a7e32014-08-14 15:56:34 -0700199 void openAudioSink(const sp<AMessage> &format, bool offloadOnly);
200 void closeAudioSink();
201
Andreas Huber5bc087c2010-12-23 10:27:40 -0800202 status_t instantiateDecoder(bool audio, sp<Decoder> *decoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800203
Chong Zhangced1c2f2014-08-08 15:22:35 -0700204 void updateVideoSize(
205 const sp<AMessage> &inputFormat,
206 const sp<AMessage> &outputFormat = NULL);
207
Andreas Huberf9334412010-12-15 15:17:42 -0800208 status_t feedDecoderInputData(bool audio, const sp<AMessage> &msg);
209 void renderBuffer(bool audio, const sp<AMessage> &msg);
210
Chong Zhangdcb89b32013-08-06 09:44:47 -0700211 void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
Andreas Huberf9334412010-12-15 15:17:42 -0800212
Andy Hung8d121d42014-10-03 09:53:53 -0700213 void handleFlushComplete(bool audio, bool isDecoder);
Andreas Huber3831a062010-12-21 10:22:33 -0800214 void finishFlushIfPossible();
215
Ronghua Wud7988b12014-10-03 15:19:10 -0700216 bool audioDecoderStillNeeded();
217
Lajos Molnar87603c02014-08-20 19:25:30 -0700218 void flushDecoder(
219 bool audio, bool needShutdown, const sp<AMessage> &newFormat = NULL);
220 void updateDecoderFormatWithoutFlush(bool audio, const sp<AMessage> &format);
Andreas Huber1aef2112011-01-04 14:01:29 -0800221
Andreas Huber1aef2112011-01-04 14:01:29 -0800222 void postScanSources();
Andreas Huber53df1a42010-12-22 10:03:04 -0800223
Andreas Huberb7c8e912012-11-27 15:02:53 -0800224 void schedulePollDuration();
225 void cancelPollDuration();
226
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800227 void processDeferredActions();
228
Wei Jiae427abf2014-09-22 15:21:11 -0700229 void performSeek(int64_t seekTimeUs, bool needNotify);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800230 void performDecoderFlush();
Andreas Huber14f76722013-01-15 09:04:18 -0800231 void performDecoderShutdown(bool audio, bool video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800232 void performReset();
233 void performScanSources();
Andreas Huber57a339c2012-12-03 11:18:00 -0800234 void performSetSurface(const sp<NativeWindowWrapper> &wrapper);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800235
Andreas Huber9575c962013-02-05 13:59:56 -0800236 void onSourceNotify(const sp<AMessage> &msg);
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700237 void onClosedCaptionNotify(const sp<AMessage> &msg);
Andreas Huber9575c962013-02-05 13:59:56 -0800238
Andreas Huber14f76722013-01-15 09:04:18 -0800239 void queueDecoderShutdown(
240 bool audio, bool video, const sp<AMessage> &reply);
241
Chong Zhang404fced2014-06-11 14:45:31 -0700242 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700243 void sendTimedTextData(const sp<ABuffer> &buffer);
Chong Zhang404fced2014-06-11 14:45:31 -0700244
245 void writeTrackInfo(Parcel* reply, const sp<AMessage> format) const;
246
Andreas Huberf9334412010-12-15 15:17:42 -0800247 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
248};
249
250} // namespace android
251
252#endif // NU_PLAYER_H_