blob: 5a13b58a0d47e2e06c33a68f4f96ad1b89af7bf3 [file] [log] [blame]
Dharmaray Kundargi643290d2011-01-16 16:02:42 -08001/*
Dharmaray Kundargi643290d2011-01-16 16:02:42 -08002 * Copyright (C) 2011 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 PREVIEW_PLAYER_H_
18
19#define PREVIEW_PLAYER_H_
20
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080021#include "TimedEventQueue.h"
22#include "VideoEditorAudioPlayer.h"
23
24#include <media/MediaPlayerInterface.h>
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080025#include <media/stagefright/OMXClient.h>
26#include <media/stagefright/TimeSource.h>
27#include <utils/threads.h>
Chih-Chung Chang43fcc392011-08-02 16:17:39 +080028#include "NativeWindowRenderer.h"
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080029
30namespace android {
31
James Dong00f742c2012-01-13 17:34:42 -080032struct VideoEditorAudioPlayer;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080033struct MediaExtractor;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080034
James Dong00f742c2012-01-13 17:34:42 -080035struct PreviewPlayer {
Chih-Chung Chang43fcc392011-08-02 16:17:39 +080036 PreviewPlayer(NativeWindowRenderer* renderer);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080037 ~PreviewPlayer();
38
James Dong00f742c2012-01-13 17:34:42 -080039 void setListener(const wp<MediaPlayerBase> &listener);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080040 void reset();
41
42 status_t play();
James Dong00f742c2012-01-13 17:34:42 -080043 status_t pause();
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080044
James Dong00f742c2012-01-13 17:34:42 -080045 bool isPlaying() const;
46 void setSurface(const sp<Surface> &surface);
Andy McFadden8ba01022012-12-18 09:46:54 -080047 void setSurfaceTexture(const sp<IGraphicBufferProducer> &bufferProducer);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080048 status_t seekTo(int64_t timeUs);
49
50 status_t getVideoDimensions(int32_t *width, int32_t *height) const;
51
James Dong00f742c2012-01-13 17:34:42 -080052
53 // FIXME: Sync between ...
Raghavender Pallafa31daf2011-03-18 22:32:51 -070054 void acquireLock();
55 void releaseLock();
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080056
57 status_t prepare();
James Dong00f742c2012-01-13 17:34:42 -080058 status_t prepareAsync();
James Dongdaeb5b32012-01-12 12:12:40 -080059 status_t setDataSource(const char *path);
James Dong00f742c2012-01-13 17:34:42 -080060 status_t setDataSource(const sp<IStreamSource> &source);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080061
James Dong00f742c2012-01-13 17:34:42 -080062 void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink);
63 status_t setLooping(bool shouldLoop);
64 status_t getDuration(int64_t *durationUs);
65 status_t getPosition(int64_t *positionUs);
66
67 uint32_t getSourceSeekFlags() const;
68
69 void postAudioEOS(int64_t delayUs = 0ll);
70 void postAudioSeekComplete();
71
Dharmaray Kundargi35cb2de2011-01-19 19:09:27 -080072 status_t loadEffectsSettings(M4VSS3GPP_EffectSettings* pEffectSettings,
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080073 int nEffects);
74 status_t loadAudioMixSettings(M4xVSS_AudioMixingSettings* pAudioMixSettings);
75 status_t setAudioMixPCMFileHandle(M4OSA_Context pAudioMixPCMFileHandle);
76 status_t setAudioMixStoryBoardParam(M4OSA_UInt32 audioMixStoryBoardTS,
77 M4OSA_UInt32 currentMediaBeginCutTime,
78 M4OSA_UInt32 currentMediaVolumeVol);
79
80 status_t setPlaybackBeginTime(uint32_t msec);
81 status_t setPlaybackEndTime(uint32_t msec);
82 status_t setStoryboardStartTime(uint32_t msec);
83 status_t setProgressCallbackInterval(uint32_t cbInterval);
84 status_t setMediaRenderingMode(M4xVSS_MediaRendering mode,
85 M4VIDEOEDITING_VideoFrameSize outputVideoSize);
86
87 status_t resetJniCallbackTimeStamp();
88 status_t setImageClipProperties(uint32_t width, uint32_t height);
89 status_t readFirstVideoFrame();
Santosh Madhavab2d6e0f2011-02-16 22:24:42 -080090 status_t getLastRenderedTimeMs(uint32_t *lastRenderedTimeMs);
James Dong727f9e12012-01-20 13:09:13 -080091 status_t setAudioPlayer(VideoEditorAudioPlayer *audioPlayer);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080092
93private:
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080094 enum {
95 PLAYING = 1,
96 LOOPING = 2,
97 FIRST_FRAME = 4,
98 PREPARING = 8,
99 PREPARED = 16,
100 AT_EOS = 32,
101 PREPARE_CANCELLED = 64,
102 CACHE_UNDERRUN = 128,
103 AUDIO_AT_EOS = 256,
104 VIDEO_AT_EOS = 512,
105 AUTO_LOOPING = 1024,
Hong Tenge0180232011-09-28 18:40:38 -0700106 INFORMED_AV_EOS = 2048,
James Dong00f742c2012-01-13 17:34:42 -0800107
108 // We are basically done preparing but are currently buffering
109 // sufficient data to begin playback and finish the preparation phase
110 // for good.
111 PREPARING_CONNECTED = 2048,
112
113 // We're triggering a single video event to display the first frame
114 // after the seekpoint.
115 SEEK_PREVIEW = 4096,
116
117 AUDIO_RUNNING = 8192,
118 AUDIOPLAYER_STARTED = 16384,
119
120 INCOGNITO = 32768,
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800121 };
122
James Dong00f742c2012-01-13 17:34:42 -0800123 mutable Mutex mLock;
124
125 OMXClient mClient;
126 TimedEventQueue mQueue;
127 bool mQueueStarted;
128 wp<MediaPlayerBase> mListener;
129
130 sp<Surface> mSurface;
131 sp<ANativeWindow> mNativeWindow;
132 sp<MediaPlayerBase::AudioSink> mAudioSink;
133
134 SystemTimeSource mSystemTimeSource;
135 TimeSource *mTimeSource;
136
137 String8 mUri;
138
139 sp<MediaSource> mVideoTrack;
140 sp<MediaSource> mVideoSource;
141 bool mVideoRendererIsPreview;
142
143 sp<MediaSource> mAudioTrack;
144 sp<MediaSource> mAudioSource;
James Dong727f9e12012-01-20 13:09:13 -0800145 VideoEditorAudioPlayer *mAudioPlayer;
James Dong00f742c2012-01-13 17:34:42 -0800146 int64_t mDurationUs;
147
148 int32_t mDisplayWidth;
149 int32_t mDisplayHeight;
150
151 uint32_t mFlags;
152 uint32_t mExtractorFlags;
153
154 int64_t mTimeSourceDeltaUs;
155 int64_t mVideoTimeUs;
156
157 enum SeekType {
158 NO_SEEK,
159 SEEK,
160 SEEK_VIDEO_ONLY
161 };
162 SeekType mSeeking;
163
164 bool mSeekNotificationSent;
165 int64_t mSeekTimeUs;
166
167 int64_t mBitrate; // total bitrate of the file (in bps) or -1 if unknown.
168
169 bool mWatchForAudioSeekComplete;
170 bool mWatchForAudioEOS;
171
172 sp<TimedEventQueue::Event> mVideoEvent;
173 bool mVideoEventPending;
174 sp<TimedEventQueue::Event> mStreamDoneEvent;
175 bool mStreamDoneEventPending;
176 sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
177 bool mAudioStatusEventPending;
178 sp<TimedEventQueue::Event> mVideoLagEvent;
179 bool mVideoLagEventPending;
180
181 sp<TimedEventQueue::Event> mAsyncPrepareEvent;
182 Condition mPreparedCondition;
183 bool mIsAsyncPrepare;
184 status_t mPrepareResult;
185 status_t mStreamDoneStatus;
186
187 MediaBuffer *mVideoBuffer;
188 int64_t mLastVideoTimeUs;
189 ARect mCropRect;
190 int32_t mGivenWidth, mGivenHeight;
191
192
Rajneesh Chowdury1c97d9a2011-02-21 15:43:33 -0800193 bool mIsChangeSourceRequired;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800194
Chih-Chung Chang43fcc392011-08-02 16:17:39 +0800195 NativeWindowRenderer *mNativeWindowRenderer;
196 RenderInput *mVideoRenderer;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800197
198 int32_t mVideoWidth, mVideoHeight;
199
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800200 //Data structures used for audio and video effects
201 M4VSS3GPP_EffectSettings* mEffectsSettings;
202 M4xVSS_AudioMixingSettings* mPreviewPlayerAudioMixSettings;
203 M4OSA_Context mAudioMixPCMFileHandle;
204 M4OSA_UInt32 mAudioMixStoryBoardTS;
205 M4OSA_UInt32 mCurrentMediaBeginCutTime;
206 M4OSA_UInt32 mCurrentMediaVolumeValue;
Dharmaray Kundargie6c07502011-01-21 16:58:31 -0800207 M4OSA_UInt32 mCurrFramingEffectIndex;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800208
209 uint32_t mNumberEffects;
210 uint32_t mPlayBeginTimeMsec;
211 uint32_t mPlayEndTimeMsec;
212 uint64_t mDecodedVideoTs; // timestamp of current decoded video frame buffer
213 uint64_t mDecVideoTsStoryBoard; // timestamp of frame relative to storyboard
214 uint32_t mCurrentVideoEffect;
215 uint32_t mProgressCbInterval;
216 uint32_t mNumberDecVideoFrames; // Counter of number of video frames decoded
217 sp<TimedEventQueue::Event> mProgressCbEvent;
218 bool mProgressCbEventPending;
Dharmaray Kundargie6c07502011-01-21 16:58:31 -0800219 sp<TimedEventQueue::Event> mOverlayUpdateEvent;
220 bool mOverlayUpdateEventPending;
221 bool mOverlayUpdateEventPosted;
222
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800223 M4xVSS_MediaRendering mRenderingMode;
224 uint32_t mOutputVideoWidth;
225 uint32_t mOutputVideoHeight;
226
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800227 uint32_t mStoryboardStartTimeMsec;
228
229 bool mIsVideoSourceJpg;
230 bool mIsFiftiesEffectStarted;
231 int64_t mImageFrameTimeUs;
232 bool mStartNextPlayer;
Raghavender Pallafa31daf2011-03-18 22:32:51 -0700233 mutable Mutex mLockControl;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800234
235 M4VIFI_UInt8* mFrameRGBBuffer;
236 M4VIFI_UInt8* mFrameYUVBuffer;
237
James Dong00f742c2012-01-13 17:34:42 -0800238 void cancelPlayerEvents_l(bool updateProgressCb = false);
239 status_t setDataSource_l(const sp<MediaExtractor> &extractor);
240 status_t setDataSource_l(const char *path);
241 void setNativeWindow_l(const sp<ANativeWindow> &native);
242 void reset_l();
243 void clear_l();
244 status_t play_l();
245 status_t pause_l(bool at_eos = false);
246 status_t initRenderer_l();
247 status_t initAudioDecoder_l();
248 status_t initVideoDecoder_l(uint32_t flags = 0);
249 void notifyVideoSize_l();
250 void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
251 void onVideoEvent();
252 void onVideoLagUpdate();
253 void onStreamDone();
254 void onCheckAudioStatus();
255 void onPrepareAsyncEvent();
256
257 void finishAsyncPrepare_l();
258 void abortPrepare(status_t err);
259
260 status_t startAudioPlayer_l();
261 void setVideoSource(const sp<MediaSource>& source);
262 status_t finishSetDataSource_l();
263 void setAudioSource(const sp<MediaSource>& source);
264
265 status_t seekTo_l(int64_t timeUs);
266 void seekAudioIfNecessary_l();
267 void finishSeekIfNecessary(int64_t videoTimeUs);
268
269 void postCheckAudioStatusEvent_l(int64_t delayUs);
270 void postVideoLagEvent_l();
271 void postStreamDoneEvent_l(status_t status);
272 void postVideoEvent_l(int64_t delayUs = -1);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800273 void setVideoPostProcessingNode(
274 M4VSS3GPP_VideoEffectType type, M4OSA_Bool enable);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800275 void postProgressCallbackEvent_l();
James Dong00f742c2012-01-13 17:34:42 -0800276 void shutdownVideoDecoder_l();
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800277 void onProgressCbEvent();
278
Dharmaray Kundargie6c07502011-01-21 16:58:31 -0800279 void postOverlayUpdateEvent_l();
280 void onUpdateOverlayEvent();
281
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800282 status_t setDataSource_l_jpg();
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800283 status_t prepare_l();
284 status_t prepareAsync_l();
James Dong00f742c2012-01-13 17:34:42 -0800285 void updateBatteryUsage_l();
Chih-Chung Chang43fcc392011-08-02 16:17:39 +0800286 void updateSizeToRender(sp<MetaData> meta);
287
James Dong00f742c2012-01-13 17:34:42 -0800288 void setDuration_l(int64_t durationUs);
289 void setPosition_l(int64_t timeUs);
Dheeraj Sharma5bc7fb42011-02-13 20:31:27 -0800290
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800291 PreviewPlayer(const PreviewPlayer &);
292 PreviewPlayer &operator=(const PreviewPlayer &);
293};
294
295} // namespace android
296
297#endif // PREVIEW_PLAYER_H_
298