blob: 45f3fc26f009c2c7bb08fc55275ac64886cc14b6 [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;
James Dongc9dedc42011-05-01 12:36:22 -070033struct AudioPlayerBase;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080034struct MediaExtractor;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080035
James Dong00f742c2012-01-13 17:34:42 -080036struct PreviewPlayer {
Chih-Chung Chang43fcc392011-08-02 16:17:39 +080037 PreviewPlayer(NativeWindowRenderer* renderer);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080038 ~PreviewPlayer();
39
James Dong00f742c2012-01-13 17:34:42 -080040 void setListener(const wp<MediaPlayerBase> &listener);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080041 void reset();
42
43 status_t play();
James Dong00f742c2012-01-13 17:34:42 -080044 status_t pause();
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080045
James Dong00f742c2012-01-13 17:34:42 -080046 bool isPlaying() const;
47 void setSurface(const sp<Surface> &surface);
48 void setSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080049 status_t seekTo(int64_t timeUs);
50
51 status_t getVideoDimensions(int32_t *width, int32_t *height) const;
52
James Dong00f742c2012-01-13 17:34:42 -080053
54 // FIXME: Sync between ...
Raghavender Pallafa31daf2011-03-18 22:32:51 -070055 void acquireLock();
56 void releaseLock();
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080057
58 status_t prepare();
James Dong00f742c2012-01-13 17:34:42 -080059 status_t prepareAsync();
James Dongdaeb5b32012-01-12 12:12:40 -080060 status_t setDataSource(const char *path);
James Dong00f742c2012-01-13 17:34:42 -080061 status_t setDataSource(const sp<IStreamSource> &source);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080062
James Dong00f742c2012-01-13 17:34:42 -080063 void setAudioSink(const sp<MediaPlayerBase::AudioSink> &audioSink);
64 status_t setLooping(bool shouldLoop);
65 status_t getDuration(int64_t *durationUs);
66 status_t getPosition(int64_t *positionUs);
67
68 uint32_t getSourceSeekFlags() const;
69
70 void postAudioEOS(int64_t delayUs = 0ll);
71 void postAudioSeekComplete();
72
Dharmaray Kundargi35cb2de2011-01-19 19:09:27 -080073 status_t loadEffectsSettings(M4VSS3GPP_EffectSettings* pEffectSettings,
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080074 int nEffects);
75 status_t loadAudioMixSettings(M4xVSS_AudioMixingSettings* pAudioMixSettings);
76 status_t setAudioMixPCMFileHandle(M4OSA_Context pAudioMixPCMFileHandle);
77 status_t setAudioMixStoryBoardParam(M4OSA_UInt32 audioMixStoryBoardTS,
78 M4OSA_UInt32 currentMediaBeginCutTime,
79 M4OSA_UInt32 currentMediaVolumeVol);
80
81 status_t setPlaybackBeginTime(uint32_t msec);
82 status_t setPlaybackEndTime(uint32_t msec);
83 status_t setStoryboardStartTime(uint32_t msec);
84 status_t setProgressCallbackInterval(uint32_t cbInterval);
85 status_t setMediaRenderingMode(M4xVSS_MediaRendering mode,
86 M4VIDEOEDITING_VideoFrameSize outputVideoSize);
87
88 status_t resetJniCallbackTimeStamp();
89 status_t setImageClipProperties(uint32_t width, uint32_t height);
90 status_t readFirstVideoFrame();
Santosh Madhavab2d6e0f2011-02-16 22:24:42 -080091 status_t getLastRenderedTimeMs(uint32_t *lastRenderedTimeMs);
James Dongc9dedc42011-05-01 12:36:22 -070092 status_t setAudioPlayer(AudioPlayerBase *audioPlayer);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080093
94private:
Dharmaray Kundargi643290d2011-01-16 16:02:42 -080095 enum {
96 PLAYING = 1,
97 LOOPING = 2,
98 FIRST_FRAME = 4,
99 PREPARING = 8,
100 PREPARED = 16,
101 AT_EOS = 32,
102 PREPARE_CANCELLED = 64,
103 CACHE_UNDERRUN = 128,
104 AUDIO_AT_EOS = 256,
105 VIDEO_AT_EOS = 512,
106 AUTO_LOOPING = 1024,
Hong Tenge0180232011-09-28 18:40:38 -0700107 INFORMED_AV_EOS = 2048,
James Dong00f742c2012-01-13 17:34:42 -0800108
109 // We are basically done preparing but are currently buffering
110 // sufficient data to begin playback and finish the preparation phase
111 // for good.
112 PREPARING_CONNECTED = 2048,
113
114 // We're triggering a single video event to display the first frame
115 // after the seekpoint.
116 SEEK_PREVIEW = 4096,
117
118 AUDIO_RUNNING = 8192,
119 AUDIOPLAYER_STARTED = 16384,
120
121 INCOGNITO = 32768,
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800122 };
123
James Dong00f742c2012-01-13 17:34:42 -0800124 mutable Mutex mLock;
125
126 OMXClient mClient;
127 TimedEventQueue mQueue;
128 bool mQueueStarted;
129 wp<MediaPlayerBase> mListener;
130
131 sp<Surface> mSurface;
132 sp<ANativeWindow> mNativeWindow;
133 sp<MediaPlayerBase::AudioSink> mAudioSink;
134
135 SystemTimeSource mSystemTimeSource;
136 TimeSource *mTimeSource;
137
138 String8 mUri;
139
140 sp<MediaSource> mVideoTrack;
141 sp<MediaSource> mVideoSource;
142 bool mVideoRendererIsPreview;
143
144 sp<MediaSource> mAudioTrack;
145 sp<MediaSource> mAudioSource;
146 AudioPlayerBase *mAudioPlayer;
147 int64_t mDurationUs;
148
149 int32_t mDisplayWidth;
150 int32_t mDisplayHeight;
151
152 uint32_t mFlags;
153 uint32_t mExtractorFlags;
154
155 int64_t mTimeSourceDeltaUs;
156 int64_t mVideoTimeUs;
157
158 enum SeekType {
159 NO_SEEK,
160 SEEK,
161 SEEK_VIDEO_ONLY
162 };
163 SeekType mSeeking;
164
165 bool mSeekNotificationSent;
166 int64_t mSeekTimeUs;
167
168 int64_t mBitrate; // total bitrate of the file (in bps) or -1 if unknown.
169
170 bool mWatchForAudioSeekComplete;
171 bool mWatchForAudioEOS;
172
173 sp<TimedEventQueue::Event> mVideoEvent;
174 bool mVideoEventPending;
175 sp<TimedEventQueue::Event> mStreamDoneEvent;
176 bool mStreamDoneEventPending;
177 sp<TimedEventQueue::Event> mCheckAudioStatusEvent;
178 bool mAudioStatusEventPending;
179 sp<TimedEventQueue::Event> mVideoLagEvent;
180 bool mVideoLagEventPending;
181
182 sp<TimedEventQueue::Event> mAsyncPrepareEvent;
183 Condition mPreparedCondition;
184 bool mIsAsyncPrepare;
185 status_t mPrepareResult;
186 status_t mStreamDoneStatus;
187
188 MediaBuffer *mVideoBuffer;
189 int64_t mLastVideoTimeUs;
190 ARect mCropRect;
191 int32_t mGivenWidth, mGivenHeight;
192
193
Rajneesh Chowdury1c97d9a2011-02-21 15:43:33 -0800194 bool mIsChangeSourceRequired;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800195
Chih-Chung Chang43fcc392011-08-02 16:17:39 +0800196 NativeWindowRenderer *mNativeWindowRenderer;
197 RenderInput *mVideoRenderer;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800198
199 int32_t mVideoWidth, mVideoHeight;
200
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800201 //Data structures used for audio and video effects
202 M4VSS3GPP_EffectSettings* mEffectsSettings;
203 M4xVSS_AudioMixingSettings* mPreviewPlayerAudioMixSettings;
204 M4OSA_Context mAudioMixPCMFileHandle;
205 M4OSA_UInt32 mAudioMixStoryBoardTS;
206 M4OSA_UInt32 mCurrentMediaBeginCutTime;
207 M4OSA_UInt32 mCurrentMediaVolumeValue;
Dharmaray Kundargie6c07502011-01-21 16:58:31 -0800208 M4OSA_UInt32 mCurrFramingEffectIndex;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800209
210 uint32_t mNumberEffects;
211 uint32_t mPlayBeginTimeMsec;
212 uint32_t mPlayEndTimeMsec;
213 uint64_t mDecodedVideoTs; // timestamp of current decoded video frame buffer
214 uint64_t mDecVideoTsStoryBoard; // timestamp of frame relative to storyboard
215 uint32_t mCurrentVideoEffect;
216 uint32_t mProgressCbInterval;
217 uint32_t mNumberDecVideoFrames; // Counter of number of video frames decoded
218 sp<TimedEventQueue::Event> mProgressCbEvent;
219 bool mProgressCbEventPending;
Dharmaray Kundargie6c07502011-01-21 16:58:31 -0800220 sp<TimedEventQueue::Event> mOverlayUpdateEvent;
221 bool mOverlayUpdateEventPending;
222 bool mOverlayUpdateEventPosted;
223
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800224 M4xVSS_MediaRendering mRenderingMode;
225 uint32_t mOutputVideoWidth;
226 uint32_t mOutputVideoHeight;
227
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800228 uint32_t mStoryboardStartTimeMsec;
229
230 bool mIsVideoSourceJpg;
231 bool mIsFiftiesEffectStarted;
232 int64_t mImageFrameTimeUs;
233 bool mStartNextPlayer;
Raghavender Pallafa31daf2011-03-18 22:32:51 -0700234 mutable Mutex mLockControl;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800235
236 M4VIFI_UInt8* mFrameRGBBuffer;
237 M4VIFI_UInt8* mFrameYUVBuffer;
James Dong00f742c2012-01-13 17:34:42 -0800238 VideoEditorAudioPlayer *mVeAudioPlayer;
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800239
James Dong00f742c2012-01-13 17:34:42 -0800240 void cancelPlayerEvents_l(bool updateProgressCb = false);
241 status_t setDataSource_l(const sp<MediaExtractor> &extractor);
242 status_t setDataSource_l(const char *path);
243 void setNativeWindow_l(const sp<ANativeWindow> &native);
244 void reset_l();
245 void clear_l();
246 status_t play_l();
247 status_t pause_l(bool at_eos = false);
248 status_t initRenderer_l();
249 status_t initAudioDecoder_l();
250 status_t initVideoDecoder_l(uint32_t flags = 0);
251 void notifyVideoSize_l();
252 void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0);
253 void onVideoEvent();
254 void onVideoLagUpdate();
255 void onStreamDone();
256 void onCheckAudioStatus();
257 void onPrepareAsyncEvent();
258
259 void finishAsyncPrepare_l();
260 void abortPrepare(status_t err);
261
262 status_t startAudioPlayer_l();
263 void setVideoSource(const sp<MediaSource>& source);
264 status_t finishSetDataSource_l();
265 void setAudioSource(const sp<MediaSource>& source);
266
267 status_t seekTo_l(int64_t timeUs);
268 void seekAudioIfNecessary_l();
269 void finishSeekIfNecessary(int64_t videoTimeUs);
270
271 void postCheckAudioStatusEvent_l(int64_t delayUs);
272 void postVideoLagEvent_l();
273 void postStreamDoneEvent_l(status_t status);
274 void postVideoEvent_l(int64_t delayUs = -1);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800275 void setVideoPostProcessingNode(
276 M4VSS3GPP_VideoEffectType type, M4OSA_Bool enable);
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800277 void postProgressCallbackEvent_l();
James Dong00f742c2012-01-13 17:34:42 -0800278 void shutdownVideoDecoder_l();
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800279 void onProgressCbEvent();
280
Dharmaray Kundargie6c07502011-01-21 16:58:31 -0800281 void postOverlayUpdateEvent_l();
282 void onUpdateOverlayEvent();
283
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800284 status_t setDataSource_l_jpg();
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800285 status_t prepare_l();
286 status_t prepareAsync_l();
James Dong00f742c2012-01-13 17:34:42 -0800287 void updateBatteryUsage_l();
Chih-Chung Chang43fcc392011-08-02 16:17:39 +0800288 void updateSizeToRender(sp<MetaData> meta);
289
James Dong00f742c2012-01-13 17:34:42 -0800290 void setDuration_l(int64_t durationUs);
291 void setPosition_l(int64_t timeUs);
Dheeraj Sharma5bc7fb42011-02-13 20:31:27 -0800292
Dharmaray Kundargi643290d2011-01-16 16:02:42 -0800293 PreviewPlayer(const PreviewPlayer &);
294 PreviewPlayer &operator=(const PreviewPlayer &);
295};
296
297} // namespace android
298
299#endif // PREVIEW_PLAYER_H_
300