blob: 0c7f5313b5539f2618c933c103127d364640d491 [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;
Andreas Huberf9334412010-12-15 15:17:42 -080029struct 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
Andreas Huber43c3e6c2011-01-05 12:17:08 -080062 // Will notify the driver through "notifySeekComplete" once finished.
63 void seekToAsync(int64_t seekTimeUs);
64
James Dong0d268a32012-08-31 12:18:27 -070065 status_t setVideoScalingMode(int32_t mode);
Chong Zhangdcb89b32013-08-06 09:44:47 -070066 status_t getTrackInfo(Parcel* reply) const;
Robert Shih7c4f0d72014-07-09 18:53:31 -070067 status_t getSelectedTrack(int32_t type, Parcel* reply) const;
Chong Zhangdcb89b32013-08-06 09:44:47 -070068 status_t selectTrack(size_t trackIndex, bool select);
James Dong0d268a32012-08-31 12:18:27 -070069
Andreas Huberf9334412010-12-15 15:17:42 -080070protected:
71 virtual ~NuPlayer();
72
73 virtual void onMessageReceived(const sp<AMessage> &msg);
74
Andreas Huber84066782011-08-16 09:34:26 -070075public:
76 struct NuPlayerStreamListener;
77 struct Source;
78
Andreas Huberf9334412010-12-15 15:17:42 -080079private:
Andreas Huberf9334412010-12-15 15:17:42 -080080 struct Decoder;
Wei Jiabc2fb722014-07-08 16:37:57 -070081 struct DecoderPassThrough;
Chong Zhanga7fa1d92014-06-11 14:49:23 -070082 struct CCDecoder;
Andreas Huberafed0e12011-09-20 15:39:58 -070083 struct GenericSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -080084 struct HTTPLiveSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -080085 struct Renderer;
Andreas Huberafed0e12011-09-20 15:39:58 -070086 struct RTSPSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -080087 struct StreamingSource;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080088 struct Action;
89 struct SeekAction;
Andreas Huber57a339c2012-12-03 11:18:00 -080090 struct SetSurfaceAction;
Andreas Huber14f76722013-01-15 09:04:18 -080091 struct ShutdownDecoderAction;
92 struct PostMessageAction;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080093 struct SimpleAction;
Andreas Huberf9334412010-12-15 15:17:42 -080094
95 enum {
Andreas Huber078cfcf2011-09-15 12:25:04 -070096 kWhatSetDataSource = '=DaS',
Andreas Huber9575c962013-02-05 13:59:56 -080097 kWhatPrepare = 'prep',
Andreas Huber078cfcf2011-09-15 12:25:04 -070098 kWhatSetVideoNativeWindow = '=NaW',
99 kWhatSetAudioSink = '=AuS',
100 kWhatMoreDataQueued = 'more',
101 kWhatStart = 'strt',
102 kWhatScanSources = 'scan',
103 kWhatVideoNotify = 'vidN',
104 kWhatAudioNotify = 'audN',
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700105 kWhatClosedCaptionNotify = 'capN',
Andreas Huber078cfcf2011-09-15 12:25:04 -0700106 kWhatRendererNotify = 'renN',
107 kWhatReset = 'rset',
108 kWhatSeek = 'seek',
109 kWhatPause = 'paus',
110 kWhatResume = 'rsme',
Andreas Huberb7c8e912012-11-27 15:02:53 -0800111 kWhatPollDuration = 'polD',
Andreas Huberb5f25f02013-02-05 10:14:26 -0800112 kWhatSourceNotify = 'srcN',
Chong Zhangdcb89b32013-08-06 09:44:47 -0700113 kWhatGetTrackInfo = 'gTrI',
Robert Shih7c4f0d72014-07-09 18:53:31 -0700114 kWhatGetSelectedTrack = 'gSel',
Chong Zhangdcb89b32013-08-06 09:44:47 -0700115 kWhatSelectTrack = 'selT',
Andreas Huberf9334412010-12-15 15:17:42 -0800116 };
117
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800118 wp<NuPlayerDriver> mDriver;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700119 bool mUIDValid;
120 uid_t mUID;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800121 sp<Source> mSource;
Andreas Huber9575c962013-02-05 13:59:56 -0800122 uint32_t mSourceFlags;
Glenn Kasten11731182011-02-08 17:26:17 -0800123 sp<NativeWindowWrapper> mNativeWindow;
Wei Jiaac428aa2014-09-02 19:01:34 -0700124 int64_t mCurrentPositionUs;
Andreas Huberf9334412010-12-15 15:17:42 -0800125 sp<MediaPlayerBase::AudioSink> mAudioSink;
Andreas Huberf9334412010-12-15 15:17:42 -0800126 sp<Decoder> mVideoDecoder;
Andreas Huber3fe62152011-09-16 15:09:22 -0700127 bool mVideoIsAVC;
Wei Jiabc2fb722014-07-08 16:37:57 -0700128 bool mOffloadAudio;
Andy Hung282a7e32014-08-14 15:56:34 -0700129 audio_offload_info_t mCurrentOffloadInfo;
Andreas Huberf9334412010-12-15 15:17:42 -0800130 sp<Decoder> mAudioDecoder;
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700131 sp<CCDecoder> mCCDecoder;
Andreas Huberf9334412010-12-15 15:17:42 -0800132 sp<Renderer> mRenderer;
Lajos Molnar09524832014-07-17 14:29:51 -0700133 sp<ALooper> mRendererLooper;
Wei Jia88703c32014-08-06 11:24:07 -0700134 int32_t mAudioDecoderGeneration;
135 int32_t mVideoDecoderGeneration;
Andreas Huberf9334412010-12-15 15:17:42 -0800136
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800137 List<sp<Action> > mDeferredActions;
138
Andreas Huberf9334412010-12-15 15:17:42 -0800139 bool mAudioEOS;
140 bool mVideoEOS;
141
Andreas Huber5bc087c2010-12-23 10:27:40 -0800142 bool mScanSourcesPending;
Andreas Huber1aef2112011-01-04 14:01:29 -0800143 int32_t mScanSourcesGeneration;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800144
Andreas Huberb7c8e912012-11-27 15:02:53 -0800145 int32_t mPollDurationGeneration;
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700146 int32_t mTimedTextGeneration;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800147
Andreas Huberf9334412010-12-15 15:17:42 -0800148 enum FlushStatus {
149 NONE,
Andreas Huberf9334412010-12-15 15:17:42 -0800150 FLUSHING_DECODER,
Andreas Huber1aef2112011-01-04 14:01:29 -0800151 FLUSHING_DECODER_SHUTDOWN,
Andreas Huber3831a062010-12-21 10:22:33 -0800152 SHUTTING_DOWN_DECODER,
153 FLUSHED,
154 SHUT_DOWN,
Andreas Huberf9334412010-12-15 15:17:42 -0800155 };
156
Andreas Huber6e3d3112011-11-28 12:36:11 -0800157 // Once the current flush is complete this indicates whether the
158 // notion of time has changed.
159 bool mTimeDiscontinuityPending;
160
Andreas Huberf9334412010-12-15 15:17:42 -0800161 FlushStatus mFlushingAudio;
162 FlushStatus mFlushingVideo;
163
Andreas Huber32f3cef2011-03-02 15:34:46 -0800164 int64_t mSkipRenderingAudioUntilMediaTimeUs;
165 int64_t mSkipRenderingVideoUntilMediaTimeUs;
166
Andreas Huber3fe62152011-09-16 15:09:22 -0700167 int64_t mVideoLateByUs;
168 int64_t mNumFramesTotal, mNumFramesDropped;
169
James Dong0d268a32012-08-31 12:18:27 -0700170 int32_t mVideoScalingMode;
171
Andreas Huber57a339c2012-12-03 11:18:00 -0800172 bool mStarted;
173
Lajos Molnar87603c02014-08-20 19:25:30 -0700174 inline const sp<Decoder> &getDecoder(bool audio) {
175 return audio ? mAudioDecoder : mVideoDecoder;
176 }
177
Andy Hung282a7e32014-08-14 15:56:34 -0700178 void openAudioSink(const sp<AMessage> &format, bool offloadOnly);
179 void closeAudioSink();
180
Andreas Huber5bc087c2010-12-23 10:27:40 -0800181 status_t instantiateDecoder(bool audio, sp<Decoder> *decoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800182
Chong Zhangced1c2f2014-08-08 15:22:35 -0700183 void updateVideoSize(
184 const sp<AMessage> &inputFormat,
185 const sp<AMessage> &outputFormat = NULL);
186
Andreas Huberf9334412010-12-15 15:17:42 -0800187 status_t feedDecoderInputData(bool audio, const sp<AMessage> &msg);
188 void renderBuffer(bool audio, const sp<AMessage> &msg);
189
Chong Zhangdcb89b32013-08-06 09:44:47 -0700190 void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
Andreas Huberf9334412010-12-15 15:17:42 -0800191
Andreas Huber3831a062010-12-21 10:22:33 -0800192 void finishFlushIfPossible();
193
Lajos Molnar87603c02014-08-20 19:25:30 -0700194 void flushDecoder(
195 bool audio, bool needShutdown, const sp<AMessage> &newFormat = NULL);
196 void updateDecoderFormatWithoutFlush(bool audio, const sp<AMessage> &format);
Andreas Huber1aef2112011-01-04 14:01:29 -0800197
198 static bool IsFlushingState(FlushStatus state, bool *needShutdown = NULL);
199
Andreas Huber1aef2112011-01-04 14:01:29 -0800200 void postScanSources();
Andreas Huber53df1a42010-12-22 10:03:04 -0800201
Andreas Huberb7c8e912012-11-27 15:02:53 -0800202 void schedulePollDuration();
203 void cancelPollDuration();
204
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800205 void processDeferredActions();
206
207 void performSeek(int64_t seekTimeUs);
208 void performDecoderFlush();
Andreas Huber14f76722013-01-15 09:04:18 -0800209 void performDecoderShutdown(bool audio, bool video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800210 void performReset();
211 void performScanSources();
Andreas Huber57a339c2012-12-03 11:18:00 -0800212 void performSetSurface(const sp<NativeWindowWrapper> &wrapper);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800213
Andreas Huber9575c962013-02-05 13:59:56 -0800214 void onSourceNotify(const sp<AMessage> &msg);
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700215 void onClosedCaptionNotify(const sp<AMessage> &msg);
Andreas Huber9575c962013-02-05 13:59:56 -0800216
Andreas Huber14f76722013-01-15 09:04:18 -0800217 void queueDecoderShutdown(
218 bool audio, bool video, const sp<AMessage> &reply);
219
Chong Zhang404fced2014-06-11 14:45:31 -0700220 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700221 void sendTimedTextData(const sp<ABuffer> &buffer);
Chong Zhang404fced2014-06-11 14:45:31 -0700222
223 void writeTrackInfo(Parcel* reply, const sp<AMessage> format) const;
224
Andreas Huberf9334412010-12-15 15:17:42 -0800225 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
226};
227
228} // namespace android
229
230#endif // NU_PLAYER_H_