blob: 96306db43d842290c971ff67af5a2a8fbcd899fc [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;
Andreas Huberf9334412010-12-15 15:17:42 -080028struct MetaData;
Andreas Huber43c3e6c2011-01-05 12:17:08 -080029struct NuPlayerDriver;
Andreas Huberf9334412010-12-15 15:17:42 -080030
31struct NuPlayer : public AHandler {
32 NuPlayer();
33
Andreas Huber9b80c2b2011-06-30 15:47:02 -070034 void setUID(uid_t uid);
35
Andreas Huber43c3e6c2011-01-05 12:17:08 -080036 void setDriver(const wp<NuPlayerDriver> &driver);
Andreas Huberf9334412010-12-15 15:17:42 -080037
Andreas Huber9575c962013-02-05 13:59:56 -080038 void setDataSourceAsync(const sp<IStreamSource> &source);
Andreas Huber5bc087c2010-12-23 10:27:40 -080039
Andreas Huber9575c962013-02-05 13:59:56 -080040 void setDataSourceAsync(
Andreas Huber1b86fe02014-01-29 11:13:26 -080041 const sp<IMediaHTTPService> &httpService,
42 const char *url,
43 const KeyedVector<String8, String8> *headers);
Andreas Huber5bc087c2010-12-23 10:27:40 -080044
Andreas Huber9575c962013-02-05 13:59:56 -080045 void setDataSourceAsync(int fd, int64_t offset, int64_t length);
46
47 void prepareAsync();
Andreas Huberafed0e12011-09-20 15:39:58 -070048
Andreas Huber57a339c2012-12-03 11:18:00 -080049 void setVideoSurfaceTextureAsync(
Andy McFadden8ba01022012-12-18 09:46:54 -080050 const sp<IGraphicBufferProducer> &bufferProducer);
Andreas Huber57a339c2012-12-03 11:18:00 -080051
Andreas Huberf9334412010-12-15 15:17:42 -080052 void setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink);
53 void start();
54
Andreas Huber43c3e6c2011-01-05 12:17:08 -080055 void pause();
56 void resume();
57
58 // Will notify the driver through "notifyResetComplete" once finished.
Andreas Huber1aef2112011-01-04 14:01:29 -080059 void resetAsync();
60
Andreas Huber43c3e6c2011-01-05 12:17:08 -080061 // Will notify the driver through "notifySeekComplete" once finished.
62 void seekToAsync(int64_t seekTimeUs);
63
James Dong0d268a32012-08-31 12:18:27 -070064 status_t setVideoScalingMode(int32_t mode);
Chong Zhangdcb89b32013-08-06 09:44:47 -070065 status_t getTrackInfo(Parcel* reply) const;
Robert Shih7c4f0d72014-07-09 18:53:31 -070066 status_t getSelectedTrack(int32_t type, Parcel* reply) const;
Chong Zhangdcb89b32013-08-06 09:44:47 -070067 status_t selectTrack(size_t trackIndex, bool select);
James Dong0d268a32012-08-31 12:18:27 -070068
Andreas Huberf9334412010-12-15 15:17:42 -080069protected:
70 virtual ~NuPlayer();
71
72 virtual void onMessageReceived(const sp<AMessage> &msg);
73
Andreas Huber84066782011-08-16 09:34:26 -070074public:
75 struct NuPlayerStreamListener;
76 struct Source;
77
Andreas Huberf9334412010-12-15 15:17:42 -080078private:
Andreas Huberf9334412010-12-15 15:17:42 -080079 struct Decoder;
Wei Jiabc2fb722014-07-08 16:37:57 -070080 struct DecoderPassThrough;
Chong Zhanga7fa1d92014-06-11 14:49:23 -070081 struct CCDecoder;
Andreas Huberafed0e12011-09-20 15:39:58 -070082 struct GenericSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -080083 struct HTTPLiveSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -080084 struct Renderer;
Andreas Huberafed0e12011-09-20 15:39:58 -070085 struct RTSPSource;
Andreas Huber5bc087c2010-12-23 10:27:40 -080086 struct StreamingSource;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080087 struct Action;
88 struct SeekAction;
Andreas Huber57a339c2012-12-03 11:18:00 -080089 struct SetSurfaceAction;
Andreas Huber14f76722013-01-15 09:04:18 -080090 struct ShutdownDecoderAction;
91 struct PostMessageAction;
Andreas Hubera1f8ab02012-11-30 10:53:22 -080092 struct SimpleAction;
Andreas Huberf9334412010-12-15 15:17:42 -080093
94 enum {
Andreas Huber078cfcf2011-09-15 12:25:04 -070095 kWhatSetDataSource = '=DaS',
Andreas Huber9575c962013-02-05 13:59:56 -080096 kWhatPrepare = 'prep',
Andreas Huber078cfcf2011-09-15 12:25:04 -070097 kWhatSetVideoNativeWindow = '=NaW',
98 kWhatSetAudioSink = '=AuS',
99 kWhatMoreDataQueued = 'more',
100 kWhatStart = 'strt',
101 kWhatScanSources = 'scan',
102 kWhatVideoNotify = 'vidN',
103 kWhatAudioNotify = 'audN',
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700104 kWhatClosedCaptionNotify = 'capN',
Andreas Huber078cfcf2011-09-15 12:25:04 -0700105 kWhatRendererNotify = 'renN',
106 kWhatReset = 'rset',
107 kWhatSeek = 'seek',
108 kWhatPause = 'paus',
109 kWhatResume = 'rsme',
Andreas Huberb7c8e912012-11-27 15:02:53 -0800110 kWhatPollDuration = 'polD',
Andreas Huberb5f25f02013-02-05 10:14:26 -0800111 kWhatSourceNotify = 'srcN',
Chong Zhangdcb89b32013-08-06 09:44:47 -0700112 kWhatGetTrackInfo = 'gTrI',
Robert Shih7c4f0d72014-07-09 18:53:31 -0700113 kWhatGetSelectedTrack = 'gSel',
Chong Zhangdcb89b32013-08-06 09:44:47 -0700114 kWhatSelectTrack = 'selT',
Andreas Huberf9334412010-12-15 15:17:42 -0800115 };
116
Andreas Huber43c3e6c2011-01-05 12:17:08 -0800117 wp<NuPlayerDriver> mDriver;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700118 bool mUIDValid;
119 uid_t mUID;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800120 sp<Source> mSource;
Andreas Huber9575c962013-02-05 13:59:56 -0800121 uint32_t mSourceFlags;
Glenn Kasten11731182011-02-08 17:26:17 -0800122 sp<NativeWindowWrapper> mNativeWindow;
Andreas Huberf9334412010-12-15 15:17:42 -0800123 sp<MediaPlayerBase::AudioSink> mAudioSink;
Andreas Huberf9334412010-12-15 15:17:42 -0800124 sp<Decoder> mVideoDecoder;
Andreas Huber3fe62152011-09-16 15:09:22 -0700125 bool mVideoIsAVC;
Wei Jiabc2fb722014-07-08 16:37:57 -0700126 bool mOffloadAudio;
Andy Hung282a7e32014-08-14 15:56:34 -0700127 audio_offload_info_t mCurrentOffloadInfo;
Andreas Huberf9334412010-12-15 15:17:42 -0800128 sp<Decoder> mAudioDecoder;
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700129 sp<CCDecoder> mCCDecoder;
Andreas Huberf9334412010-12-15 15:17:42 -0800130 sp<Renderer> mRenderer;
Lajos Molnar09524832014-07-17 14:29:51 -0700131 sp<ALooper> mRendererLooper;
Wei Jia88703c32014-08-06 11:24:07 -0700132 int32_t mAudioDecoderGeneration;
133 int32_t mVideoDecoderGeneration;
Andreas Huberf9334412010-12-15 15:17:42 -0800134
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800135 List<sp<Action> > mDeferredActions;
136
Andreas Huberf9334412010-12-15 15:17:42 -0800137 bool mAudioEOS;
138 bool mVideoEOS;
139
Andreas Huber5bc087c2010-12-23 10:27:40 -0800140 bool mScanSourcesPending;
Andreas Huber1aef2112011-01-04 14:01:29 -0800141 int32_t mScanSourcesGeneration;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800142
Andreas Huberb7c8e912012-11-27 15:02:53 -0800143 int32_t mPollDurationGeneration;
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700144 int32_t mTimedTextGeneration;
Andreas Huberb7c8e912012-11-27 15:02:53 -0800145
Andreas Huberf9334412010-12-15 15:17:42 -0800146 enum FlushStatus {
147 NONE,
Andreas Huberf9334412010-12-15 15:17:42 -0800148 FLUSHING_DECODER,
Andreas Huber1aef2112011-01-04 14:01:29 -0800149 FLUSHING_DECODER_SHUTDOWN,
Andreas Huber3831a062010-12-21 10:22:33 -0800150 SHUTTING_DOWN_DECODER,
151 FLUSHED,
152 SHUT_DOWN,
Andreas Huberf9334412010-12-15 15:17:42 -0800153 };
154
Andreas Huber6e3d3112011-11-28 12:36:11 -0800155 // Once the current flush is complete this indicates whether the
156 // notion of time has changed.
157 bool mTimeDiscontinuityPending;
158
Andreas Huberf9334412010-12-15 15:17:42 -0800159 FlushStatus mFlushingAudio;
160 FlushStatus mFlushingVideo;
161
Andreas Huber32f3cef2011-03-02 15:34:46 -0800162 int64_t mSkipRenderingAudioUntilMediaTimeUs;
163 int64_t mSkipRenderingVideoUntilMediaTimeUs;
164
Andreas Huber3fe62152011-09-16 15:09:22 -0700165 int64_t mVideoLateByUs;
166 int64_t mNumFramesTotal, mNumFramesDropped;
167
James Dong0d268a32012-08-31 12:18:27 -0700168 int32_t mVideoScalingMode;
169
Andreas Huber57a339c2012-12-03 11:18:00 -0800170 bool mStarted;
171
Andy Hung282a7e32014-08-14 15:56:34 -0700172 void openAudioSink(const sp<AMessage> &format, bool offloadOnly);
173 void closeAudioSink();
174
Andreas Huber5bc087c2010-12-23 10:27:40 -0800175 status_t instantiateDecoder(bool audio, sp<Decoder> *decoder);
Andreas Huberf9334412010-12-15 15:17:42 -0800176
Chong Zhangced1c2f2014-08-08 15:22:35 -0700177 void updateVideoSize(
178 const sp<AMessage> &inputFormat,
179 const sp<AMessage> &outputFormat = NULL);
180
Andreas Huberf9334412010-12-15 15:17:42 -0800181 status_t feedDecoderInputData(bool audio, const sp<AMessage> &msg);
182 void renderBuffer(bool audio, const sp<AMessage> &msg);
183
Chong Zhangdcb89b32013-08-06 09:44:47 -0700184 void notifyListener(int msg, int ext1, int ext2, const Parcel *in = NULL);
Andreas Huberf9334412010-12-15 15:17:42 -0800185
Andreas Huber3831a062010-12-21 10:22:33 -0800186 void finishFlushIfPossible();
187
Andreas Huber1aef2112011-01-04 14:01:29 -0800188 void flushDecoder(bool audio, bool needShutdown);
189
190 static bool IsFlushingState(FlushStatus state, bool *needShutdown = NULL);
191
Andreas Huber1aef2112011-01-04 14:01:29 -0800192 void postScanSources();
Andreas Huber53df1a42010-12-22 10:03:04 -0800193
Andreas Huberb7c8e912012-11-27 15:02:53 -0800194 void schedulePollDuration();
195 void cancelPollDuration();
196
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800197 void processDeferredActions();
198
199 void performSeek(int64_t seekTimeUs);
200 void performDecoderFlush();
Andreas Huber14f76722013-01-15 09:04:18 -0800201 void performDecoderShutdown(bool audio, bool video);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800202 void performReset();
203 void performScanSources();
Andreas Huber57a339c2012-12-03 11:18:00 -0800204 void performSetSurface(const sp<NativeWindowWrapper> &wrapper);
Andreas Hubera1f8ab02012-11-30 10:53:22 -0800205
Andreas Huber9575c962013-02-05 13:59:56 -0800206 void onSourceNotify(const sp<AMessage> &msg);
Chong Zhanga7fa1d92014-06-11 14:49:23 -0700207 void onClosedCaptionNotify(const sp<AMessage> &msg);
Andreas Huber9575c962013-02-05 13:59:56 -0800208
Andreas Huber14f76722013-01-15 09:04:18 -0800209 void queueDecoderShutdown(
210 bool audio, bool video, const sp<AMessage> &reply);
211
Chong Zhang404fced2014-06-11 14:45:31 -0700212 void sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex);
Robert Shihd3b0bbb2014-07-23 15:00:25 -0700213 void sendTimedTextData(const sp<ABuffer> &buffer);
Chong Zhang404fced2014-06-11 14:45:31 -0700214
215 void writeTrackInfo(Parcel* reply, const sp<AMessage> format) const;
216
Andreas Huberf9334412010-12-15 15:17:42 -0800217 DISALLOW_EVIL_CONSTRUCTORS(NuPlayer);
218};
219
220} // namespace android
221
222#endif // NU_PLAYER_H_