blob: a17015ec03ad0ebdb45cbf85f6ce993046a852c2 [file] [log] [blame]
Chih-Chung Chang99698662011-06-30 14:21:38 +08001/*
2 * 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#define LOG_NDEBUG 1
18#define LOG_TAG "VideoEditorPreviewController"
19#include "VideoEditorPreviewController.h"
20
21namespace android {
22
23#define PREVIEW_THREAD_STACK_SIZE (65536)
24
25VideoEditorPreviewController::VideoEditorPreviewController()
26 : mCurrentPlayer(0),
27 mThreadContext(NULL),
28 mPlayerState(VePlayerIdle),
29 mPrepareReqest(M4OSA_FALSE),
30 mClipList(NULL),
31 mNumberClipsInStoryBoard(0),
32 mNumberClipsToPreview(0),
33 mStartingClipIndex(0),
34 mPreviewLooping(M4OSA_FALSE),
35 mCallBackAfterFrameCnt(0),
36 mEffectsSettings(NULL),
37 mNumberEffects(0),
38 mCurrentClipNumber(-1),
39 mClipTotalDuration(0),
40 mCurrentVideoEffect(VIDEO_EFFECT_NONE),
41 mBackgroundAudioSetting(NULL),
42 mAudioMixPCMFileHandle(NULL),
43 mTarget(NULL),
44 mJniCookie(NULL),
45 mJniCallback(NULL),
46 mCurrentPlayedDuration(0),
47 mCurrentClipDuration(0),
48 mVideoStoryBoardTimeMsUptoFirstPreviewClip(0),
49 mOverlayState(OVERLAY_CLEAR),
50 mActivePlayerIndex(0),
51 mOutputVideoWidth(0),
52 mOutputVideoHeight(0),
53 bStopThreadInProgress(false),
54 mSemThreadWait(NULL) {
55 LOGV("VideoEditorPreviewController");
56 mRenderingMode = M4xVSS_kBlackBorders;
57 mIsFiftiesEffectStarted = false;
58
59 for (int i=0; i<NBPLAYER_INSTANCES; i++) {
60 mVePlayer[i] = NULL;
61 }
62}
63
64VideoEditorPreviewController::~VideoEditorPreviewController() {
65 M4OSA_UInt32 i = 0;
66 M4OSA_ERR err = M4NO_ERROR;
67 LOGV("~VideoEditorPreviewController");
68
69 // Stop the thread if its still running
70 if(mThreadContext != NULL) {
71 err = M4OSA_threadSyncStop(mThreadContext);
72 if(err != M4NO_ERROR) {
73 LOGV("~VideoEditorPreviewController: error 0x%x \
74 in trying to stop thread", err);
75 // Continue even if error
76 }
77
78 err = M4OSA_threadSyncClose(mThreadContext);
79 if(err != M4NO_ERROR) {
80 LOGE("~VideoEditorPreviewController: error 0x%x \
81 in trying to close thread", (unsigned int) err);
82 // Continue even if error
83 }
84
85 mThreadContext = NULL;
86 }
87
88 for (int playerInst=0; playerInst<NBPLAYER_INSTANCES;
89 playerInst++) {
90 if(mVePlayer[playerInst] != NULL) {
91 LOGV("clearing mVePlayer %d", playerInst);
92 mVePlayer[playerInst].clear();
93 }
94 }
95
96 if(mClipList != NULL) {
97 // Clean up
98 for(i=0;i<mNumberClipsInStoryBoard;i++)
99 {
100 if(mClipList[i]->pFile != NULL) {
101 free(mClipList[i]->pFile);
102 mClipList[i]->pFile = NULL;
103 }
104
105 free(mClipList[i]);
106 }
107 free(mClipList);
108 mClipList = NULL;
109 }
110
111 if(mEffectsSettings) {
112 for(i=0;i<mNumberEffects;i++) {
113 if(mEffectsSettings[i].xVSS.pFramingBuffer != NULL) {
114 free(mEffectsSettings[i].xVSS.pFramingBuffer->pac_data);
115
116 free(mEffectsSettings[i].xVSS.pFramingBuffer);
117
118 mEffectsSettings[i].xVSS.pFramingBuffer = NULL;
119 }
120 }
121 free(mEffectsSettings);
122 mEffectsSettings = NULL;
123 }
124
125 if (mAudioMixPCMFileHandle) {
126 err = M4OSA_fileReadClose (mAudioMixPCMFileHandle);
127 mAudioMixPCMFileHandle = M4OSA_NULL;
128 }
129
130 if (mBackgroundAudioSetting != NULL) {
131 free(mBackgroundAudioSetting);
132 mBackgroundAudioSetting = NULL;
133 }
134
135 if(mTarget != NULL) {
136 delete mTarget;
137 mTarget = NULL;
138 }
139
140 mOverlayState = OVERLAY_CLEAR;
141
142 LOGV("~VideoEditorPreviewController returns");
143}
144
145M4OSA_ERR VideoEditorPreviewController::loadEditSettings(
146 M4VSS3GPP_EditSettings* pSettings,M4xVSS_AudioMixingSettings* bgmSettings) {
147
148 M4OSA_UInt32 i = 0, iClipDuration = 0, rgbSize = 0;
149 M4VIFI_UInt8 *tmp = NULL;
150 M4OSA_ERR err = M4NO_ERROR;
151
152 LOGV("loadEditSettings");
153 LOGV("loadEditSettings Channels = %d, sampling Freq %d",
154 bgmSettings->uiNbChannels, bgmSettings->uiSamplingFrequency );
155 bgmSettings->uiSamplingFrequency = 32000;
156
157 LOGV("loadEditSettings Channels = %d, sampling Freq %d",
158 bgmSettings->uiNbChannels, bgmSettings->uiSamplingFrequency );
159 Mutex::Autolock autoLock(mLock);
160
161 // Clean up any previous Edit settings before loading new ones
162 mCurrentVideoEffect = VIDEO_EFFECT_NONE;
163
164 if(mAudioMixPCMFileHandle) {
165 err = M4OSA_fileReadClose (mAudioMixPCMFileHandle);
166 mAudioMixPCMFileHandle = M4OSA_NULL;
167 }
168
169 if(mBackgroundAudioSetting != NULL) {
170 free(mBackgroundAudioSetting);
171 mBackgroundAudioSetting = NULL;
172 }
173
174 if(mClipList != NULL) {
175 // Clean up
176 for(i=0;i<mNumberClipsInStoryBoard;i++)
177 {
178 if(mClipList[i]->pFile != NULL) {
179 free(mClipList[i]->pFile);
180 mClipList[i]->pFile = NULL;
181 }
182
183 free(mClipList[i]);
184 }
185 free(mClipList);
186 mClipList = NULL;
187 }
188
189 if(mEffectsSettings) {
190 for(i=0;i<mNumberEffects;i++) {
191 if(mEffectsSettings[i].xVSS.pFramingBuffer != NULL) {
192 free(mEffectsSettings[i].xVSS.pFramingBuffer->pac_data);
193
194 free(mEffectsSettings[i].xVSS.pFramingBuffer);
195
196 mEffectsSettings[i].xVSS.pFramingBuffer = NULL;
197 }
198 }
199 free(mEffectsSettings);
200 mEffectsSettings = NULL;
201 }
202
203 if(mClipList == NULL) {
204 mNumberClipsInStoryBoard = pSettings->uiClipNumber;
205 LOGV("loadEditSettings: # of Clips = %d", mNumberClipsInStoryBoard);
206
207 mClipList = (M4VSS3GPP_ClipSettings**)M4OSA_32bitAlignedMalloc(
208 sizeof(M4VSS3GPP_ClipSettings*)*pSettings->uiClipNumber, M4VS,
209 (M4OSA_Char*)"LvPP, copy of pClipList");
210
211 if(NULL == mClipList) {
212 LOGE("loadEditSettings: Malloc error");
213 return M4ERR_ALLOC;
214 }
215 memset((void *)mClipList,0,
216 sizeof(M4VSS3GPP_ClipSettings*)*pSettings->uiClipNumber);
217
218 for(i=0;i<pSettings->uiClipNumber;i++) {
219
220 // Allocate current clip
221 mClipList[i] =
222 (M4VSS3GPP_ClipSettings*)M4OSA_32bitAlignedMalloc(
223 sizeof(M4VSS3GPP_ClipSettings),M4VS,(M4OSA_Char*)"clip settings");
224
225 if(mClipList[i] == NULL) {
226
227 LOGE("loadEditSettings: Allocation error for mClipList[%d]", (int)i);
228 return M4ERR_ALLOC;
229 }
230 // Copy plain structure
231 memcpy((void *)mClipList[i],
232 (void *)pSettings->pClipList[i],
233 sizeof(M4VSS3GPP_ClipSettings));
234
235 if(NULL != pSettings->pClipList[i]->pFile) {
236 mClipList[i]->pFile = (M4OSA_Char*)M4OSA_32bitAlignedMalloc(
237 pSettings->pClipList[i]->filePathSize, M4VS,
238 (M4OSA_Char*)"pClipSettingsDest->pFile");
239
240 if(NULL == mClipList[i]->pFile)
241 {
242 LOGE("loadEditSettings : ERROR allocating filename");
243 return M4ERR_ALLOC;
244 }
245
246 memcpy((void *)mClipList[i]->pFile,
247 (void *)pSettings->pClipList[i]->pFile,
248 pSettings->pClipList[i]->filePathSize);
249 }
250 else {
251 LOGE("NULL file path");
252 return M4ERR_PARAMETER;
253 }
254
255 // Calculate total duration of all clips
256 iClipDuration = pSettings->pClipList[i]->uiEndCutTime -
257 pSettings->pClipList[i]->uiBeginCutTime;
258
259 mClipTotalDuration = mClipTotalDuration+iClipDuration;
260 }
261 }
262
263 if(mEffectsSettings == NULL) {
264 mNumberEffects = pSettings->nbEffects;
265 LOGV("loadEditSettings: mNumberEffects = %d", mNumberEffects);
266
267 if(mNumberEffects != 0) {
268 mEffectsSettings = (M4VSS3GPP_EffectSettings*)M4OSA_32bitAlignedMalloc(
269 mNumberEffects*sizeof(M4VSS3GPP_EffectSettings),
270 M4VS, (M4OSA_Char*)"effects settings");
271
272 if(mEffectsSettings == NULL) {
273 LOGE("loadEffectsSettings: Allocation error");
274 return M4ERR_ALLOC;
275 }
276
277 memset((void *)mEffectsSettings,0,
278 mNumberEffects*sizeof(M4VSS3GPP_EffectSettings));
279
280 for(i=0;i<mNumberEffects;i++) {
281
282 mEffectsSettings[i].xVSS.pFramingFilePath = NULL;
283 mEffectsSettings[i].xVSS.pFramingBuffer = NULL;
284 mEffectsSettings[i].xVSS.pTextBuffer = NULL;
285
286 memcpy((void *)&(mEffectsSettings[i]),
287 (void *)&(pSettings->Effects[i]),
288 sizeof(M4VSS3GPP_EffectSettings));
289
290 if(pSettings->Effects[i].VideoEffectType ==
291 (M4VSS3GPP_VideoEffectType)M4xVSS_kVideoEffectType_Framing) {
292 // Allocate the pFraming RGB buffer
293 mEffectsSettings[i].xVSS.pFramingBuffer =
294 (M4VIFI_ImagePlane *)M4OSA_32bitAlignedMalloc(sizeof(M4VIFI_ImagePlane),
295 M4VS, (M4OSA_Char*)"lvpp framing buffer");
296
297 if(mEffectsSettings[i].xVSS.pFramingBuffer == NULL) {
298 LOGE("loadEffectsSettings:Alloc error for pFramingBuf");
299 free(mEffectsSettings);
300 mEffectsSettings = NULL;
301 return M4ERR_ALLOC;
302 }
303
304 // Allocate the pac_data (RGB)
305 if(pSettings->Effects[i].xVSS.rgbType == M4VSS3GPP_kRGB565){
306 rgbSize =
307 pSettings->Effects[i].xVSS.pFramingBuffer->u_width *
308 pSettings->Effects[i].xVSS.pFramingBuffer->u_height*2;
309 }
310 else if(
311 pSettings->Effects[i].xVSS.rgbType == M4VSS3GPP_kRGB888) {
312 rgbSize =
313 pSettings->Effects[i].xVSS.pFramingBuffer->u_width *
314 pSettings->Effects[i].xVSS.pFramingBuffer->u_height*3;
315 }
316 else {
317 LOGE("loadEffectsSettings: wrong RGB type");
318 free(mEffectsSettings);
319 mEffectsSettings = NULL;
320 return M4ERR_PARAMETER;
321 }
322
323 tmp = (M4VIFI_UInt8 *)M4OSA_32bitAlignedMalloc(rgbSize, M4VS,
324 (M4OSA_Char*)"framing buffer pac_data");
325
326 if(tmp == NULL) {
327 LOGE("loadEffectsSettings:Alloc error pFramingBuf pac");
328 free(mEffectsSettings);
329 mEffectsSettings = NULL;
330 free(mEffectsSettings[i].xVSS.pFramingBuffer);
331
332 mEffectsSettings[i].xVSS.pFramingBuffer = NULL;
333 return M4ERR_ALLOC;
334 }
335 /* Initialize the pFramingBuffer*/
336 mEffectsSettings[i].xVSS.pFramingBuffer->pac_data = tmp;
337 mEffectsSettings[i].xVSS.pFramingBuffer->u_height =
338 pSettings->Effects[i].xVSS.pFramingBuffer->u_height;
339
340 mEffectsSettings[i].xVSS.pFramingBuffer->u_width =
341 pSettings->Effects[i].xVSS.pFramingBuffer->u_width;
342
343 mEffectsSettings[i].xVSS.pFramingBuffer->u_stride =
344 pSettings->Effects[i].xVSS.pFramingBuffer->u_stride;
345
346 mEffectsSettings[i].xVSS.pFramingBuffer->u_topleft =
347 pSettings->Effects[i].xVSS.pFramingBuffer->u_topleft;
348
349 mEffectsSettings[i].xVSS.uialphaBlendingStart =
350 pSettings->Effects[i].xVSS.uialphaBlendingStart;
351
352 mEffectsSettings[i].xVSS.uialphaBlendingMiddle =
353 pSettings->Effects[i].xVSS.uialphaBlendingMiddle;
354
355 mEffectsSettings[i].xVSS.uialphaBlendingEnd =
356 pSettings->Effects[i].xVSS.uialphaBlendingEnd;
357
358 mEffectsSettings[i].xVSS.uialphaBlendingFadeInTime =
359 pSettings->Effects[i].xVSS.uialphaBlendingFadeInTime;
360 mEffectsSettings[i].xVSS.uialphaBlendingFadeOutTime =
361 pSettings->Effects[i].xVSS.uialphaBlendingFadeOutTime;
362
363 // Copy the pFraming data
364 memcpy((void *)
365 mEffectsSettings[i].xVSS.pFramingBuffer->pac_data,
366 (void *)pSettings->Effects[i].xVSS.pFramingBuffer->pac_data,
367 rgbSize);
368
369 mEffectsSettings[i].xVSS.rgbType =
370 pSettings->Effects[i].xVSS.rgbType;
371 }
372 }
373 }
374 }
375
376 if (mBackgroundAudioSetting == NULL) {
377
378 mBackgroundAudioSetting = (M4xVSS_AudioMixingSettings*)M4OSA_32bitAlignedMalloc(
379 sizeof(M4xVSS_AudioMixingSettings), M4VS,
380 (M4OSA_Char*)"LvPP, copy of bgmSettings");
381
382 if(NULL == mBackgroundAudioSetting) {
383 LOGE("loadEditSettings: mBackgroundAudioSetting Malloc failed");
384 return M4ERR_ALLOC;
385 }
386
387 memset((void *)mBackgroundAudioSetting, 0,sizeof(M4xVSS_AudioMixingSettings*));
388 memcpy((void *)mBackgroundAudioSetting, (void *)bgmSettings, sizeof(M4xVSS_AudioMixingSettings));
389
390 if ( mBackgroundAudioSetting->pFile != M4OSA_NULL ) {
391
392 mBackgroundAudioSetting->pFile = (M4OSA_Void*) bgmSettings->pPCMFilePath;
393 mBackgroundAudioSetting->uiNbChannels = 2;
394 mBackgroundAudioSetting->uiSamplingFrequency = 32000;
395 }
396
397 // Open the BG file
398 if ( mBackgroundAudioSetting->pFile != M4OSA_NULL ) {
399 err = M4OSA_fileReadOpen(&mAudioMixPCMFileHandle,
400 mBackgroundAudioSetting->pFile, M4OSA_kFileRead);
401
402 if (err != M4NO_ERROR) {
403 LOGE("loadEditSettings: mBackgroundAudio PCM File open failed");
404 return M4ERR_PARAMETER;
405 }
406 }
407 }
408
409 mOutputVideoSize = pSettings->xVSS.outputVideoSize;
410 mFrameStr.pBuffer = M4OSA_NULL;
411 return M4NO_ERROR;
412}
413
414M4OSA_ERR VideoEditorPreviewController::setSurface(const sp<Surface> &surface) {
415 LOGV("setSurface");
416 Mutex::Autolock autoLock(mLock);
417
418 mSurface = surface;
419 return M4NO_ERROR;
420}
421
422M4OSA_ERR VideoEditorPreviewController::startPreview(
423 M4OSA_UInt32 fromMS, M4OSA_Int32 toMs, M4OSA_UInt16 callBackAfterFrameCount,
424 M4OSA_Bool loop) {
425
426 M4OSA_ERR err = M4NO_ERROR;
427 M4OSA_UInt32 i = 0, iIncrementedDuration = 0;
428 LOGV("startPreview");
429
430 if(fromMS > (M4OSA_UInt32)toMs) {
431 LOGE("startPreview: fromMS > toMs");
432 return M4ERR_PARAMETER;
433 }
434
435 if(toMs == 0) {
436 LOGE("startPreview: toMs is 0");
437 return M4ERR_PARAMETER;
438 }
439
440 // If already started, then stop preview first
441 for(int playerInst=0; playerInst<NBPLAYER_INSTANCES; playerInst++) {
442 if(mVePlayer[playerInst] != NULL) {
443 LOGV("startPreview: stopping previously started preview playback");
444 stopPreview();
445 break;
446 }
447 }
448
449 // If renderPreview was called previously, then delete Renderer object first
450 if(mTarget != NULL) {
451 LOGV("startPreview: delete previous PreviewRenderer");
452 delete mTarget;
453 mTarget = NULL;
454 }
455
456 // Create Audio player to be used for entire
457 // storyboard duration
458 mVEAudioSink = new VideoEditorPlayer::VeAudioOutput();
459 mVEAudioPlayer = new VideoEditorAudioPlayer(mVEAudioSink);
460 mVEAudioPlayer->setAudioMixSettings(mBackgroundAudioSetting);
461 mVEAudioPlayer->setAudioMixPCMFileHandle(mAudioMixPCMFileHandle);
462
Chih-Chung Chang43fcc392011-08-02 16:17:39 +0800463 // Create Video Renderer to be used for the entire storyboard duration.
464 uint32_t width, height;
465 getVideoSizeByResolution(mOutputVideoSize, &width, &height);
466 mNativeWindowRenderer = new NativeWindowRenderer(mSurface, width, height);
467
Chih-Chung Chang99698662011-06-30 14:21:38 +0800468 LOGV("startPreview: loop = %d", loop);
469 mPreviewLooping = loop;
470
471 LOGV("startPreview: callBackAfterFrameCount = %d", callBackAfterFrameCount);
472 mCallBackAfterFrameCnt = callBackAfterFrameCount;
473
474 for (int playerInst=0; playerInst<NBPLAYER_INSTANCES; playerInst++) {
Chih-Chung Chang43fcc392011-08-02 16:17:39 +0800475 mVePlayer[playerInst] = new VideoEditorPlayer(mNativeWindowRenderer);
Chih-Chung Chang99698662011-06-30 14:21:38 +0800476 if(mVePlayer[playerInst] == NULL) {
477 LOGE("startPreview:Error creating VideoEditorPlayer %d",playerInst);
478 return M4ERR_ALLOC;
479 }
480 LOGV("startPreview: object created");
481
482 mVePlayer[playerInst]->setNotifyCallback(this,(notify_callback_f)notify);
483 LOGV("startPreview: notify callback set");
484
485 mVePlayer[playerInst]->loadEffectsSettings(mEffectsSettings,
486 mNumberEffects);
487 LOGV("startPreview: effects settings loaded");
488
489 mVePlayer[playerInst]->loadAudioMixSettings(mBackgroundAudioSetting);
490 LOGV("startPreview: AudioMixSettings settings loaded");
491
492 mVePlayer[playerInst]->setAudioMixPCMFileHandle(mAudioMixPCMFileHandle);
493 LOGV("startPreview: AudioMixPCMFileHandle set");
494
495 mVePlayer[playerInst]->setProgressCallbackInterval(
496 mCallBackAfterFrameCnt);
497 LOGV("startPreview: setProgressCallBackInterval");
498 }
499
500 mPlayerState = VePlayerIdle;
501 mPrepareReqest = M4OSA_FALSE;
502
503 if(fromMS == 0) {
504 mCurrentClipNumber = -1;
505 // Save original value
506 mFirstPreviewClipBeginTime = mClipList[0]->uiBeginCutTime;
507 mVideoStoryBoardTimeMsUptoFirstPreviewClip = 0;
508 }
509 else {
510 LOGV("startPreview: fromMS=%d", fromMS);
511 if(fromMS >= mClipTotalDuration) {
512 LOGE("startPreview: fromMS >= mClipTotalDuration");
513 return M4ERR_PARAMETER;
514 }
515 for(i=0;i<mNumberClipsInStoryBoard;i++) {
516 if(fromMS < (iIncrementedDuration + (mClipList[i]->uiEndCutTime -
517 mClipList[i]->uiBeginCutTime))) {
518 // Set to 1 index below,
519 // as threadProcess first increments the clip index
520 // and then processes clip in thread loop
521 mCurrentClipNumber = i-1;
522 LOGV("startPreview:mCurrentClipNumber = %d fromMS=%d",i,fromMS);
523
524 // Save original value
525 mFirstPreviewClipBeginTime = mClipList[i]->uiBeginCutTime;
526
527 // Set correct begin time to start playback
528 if((fromMS+mClipList[i]->uiBeginCutTime) >
529 (iIncrementedDuration+mClipList[i]->uiBeginCutTime)) {
530
531 mClipList[i]->uiBeginCutTime =
532 mClipList[i]->uiBeginCutTime +
533 (fromMS - iIncrementedDuration);
534 }
535 break;
536 }
537 else {
538 iIncrementedDuration = iIncrementedDuration +
539 (mClipList[i]->uiEndCutTime - mClipList[i]->uiBeginCutTime);
540 }
541 }
542 mVideoStoryBoardTimeMsUptoFirstPreviewClip = iIncrementedDuration;
543 }
544
545 for (int playerInst=0; playerInst<NBPLAYER_INSTANCES; playerInst++) {
546 mVePlayer[playerInst]->setAudioMixStoryBoardParam(fromMS,
547 mFirstPreviewClipBeginTime,
548 mClipList[i]->ClipProperties.uiClipAudioVolumePercentage);
549
550 LOGV("startPreview:setAudioMixStoryBoardSkimTimeStamp set %d cuttime \
551 %d", fromMS, mFirstPreviewClipBeginTime);
552 }
553
554 mStartingClipIndex = mCurrentClipNumber+1;
555
556 // Start playing with player instance 0
557 mCurrentPlayer = 0;
558 mActivePlayerIndex = 0;
559
560 if(toMs == -1) {
561 LOGV("startPreview: Preview till end of storyboard");
562 mNumberClipsToPreview = mNumberClipsInStoryBoard;
563 // Save original value
564 mLastPreviewClipEndTime =
565 mClipList[mNumberClipsToPreview-1]->uiEndCutTime;
566 }
567 else {
568 LOGV("startPreview: toMs=%d", toMs);
569 if((M4OSA_UInt32)toMs > mClipTotalDuration) {
570 LOGE("startPreview: toMs > mClipTotalDuration");
571 return M4ERR_PARAMETER;
572 }
573
574 iIncrementedDuration = 0;
575
576 for(i=0;i<mNumberClipsInStoryBoard;i++) {
577 if((M4OSA_UInt32)toMs <= (iIncrementedDuration +
578 (mClipList[i]->uiEndCutTime - mClipList[i]->uiBeginCutTime))) {
579 // Save original value
580 mLastPreviewClipEndTime = mClipList[i]->uiEndCutTime;
581 // Set the end cut time of clip index i to toMs
582 mClipList[i]->uiEndCutTime = toMs;
583
584 // Number of clips to be previewed is from index 0 to i
585 // increment by 1 as i starts from 0
586 mNumberClipsToPreview = i+1;
587 break;
588 }
589 else {
590 iIncrementedDuration = iIncrementedDuration +
591 (mClipList[i]->uiEndCutTime - mClipList[i]->uiBeginCutTime);
592 }
593 }
594 }
595
596 // Open the thread semaphore
597 M4OSA_semaphoreOpen(&mSemThreadWait, 1);
598
599 // Open the preview process thread
600 err = M4OSA_threadSyncOpen(&mThreadContext, (M4OSA_ThreadDoIt)threadProc);
601 if (M4NO_ERROR != err) {
602 LOGE("VideoEditorPreviewController:M4OSA_threadSyncOpen error %d", (int) err);
603 return err;
604 }
605
606 // Set the stacksize
607 err = M4OSA_threadSyncSetOption(mThreadContext, M4OSA_ThreadStackSize,
608 (M4OSA_DataOption)PREVIEW_THREAD_STACK_SIZE);
609
610 if (M4NO_ERROR != err) {
611 LOGE("VideoEditorPreviewController: threadSyncSetOption error %d", (int) err);
612 M4OSA_threadSyncClose(mThreadContext);
613 mThreadContext = NULL;
614 return err;
615 }
616
617 // Start the thread
618 err = M4OSA_threadSyncStart(mThreadContext, (M4OSA_Void*)this);
619 if (M4NO_ERROR != err) {
620 LOGE("VideoEditorPreviewController: threadSyncStart error %d", (int) err);
621 M4OSA_threadSyncClose(mThreadContext);
622 mThreadContext = NULL;
623 return err;
624 }
625 bStopThreadInProgress = false;
626
627 LOGV("startPreview: process thread started");
628 return M4NO_ERROR;
629}
630
631M4OSA_UInt32 VideoEditorPreviewController::stopPreview() {
632 M4OSA_ERR err = M4NO_ERROR;
633 uint32_t lastRenderedFrameTimeMs = 0;
634 LOGV("stopPreview");
635
636 // Stop the thread
637 if(mThreadContext != NULL) {
638 bStopThreadInProgress = true;
639 {
640 Mutex::Autolock autoLock(mLockSem);
641 if (mSemThreadWait != NULL) {
642 err = M4OSA_semaphorePost(mSemThreadWait);
643 }
644 }
645
646 err = M4OSA_threadSyncStop(mThreadContext);
647 if(err != M4NO_ERROR) {
648 LOGV("stopPreview: error 0x%x in trying to stop thread", err);
649 // Continue even if error
650 }
651
652 err = M4OSA_threadSyncClose(mThreadContext);
653 if(err != M4NO_ERROR) {
654 LOGE("stopPreview: error 0x%x in trying to close thread", (unsigned int)err);
655 // Continue even if error
656 }
657
658 mThreadContext = NULL;
659 }
660
661 // Close the semaphore first
662 {
663 Mutex::Autolock autoLock(mLockSem);
664 if(mSemThreadWait != NULL) {
665 err = M4OSA_semaphoreClose(mSemThreadWait);
666 LOGV("stopPreview: close semaphore returns 0x%x", err);
667 mSemThreadWait = NULL;
668 }
669 }
670
671 for (int playerInst=0; playerInst<NBPLAYER_INSTANCES; playerInst++) {
672 if(mVePlayer[playerInst] != NULL) {
673 if(mVePlayer[playerInst]->isPlaying()) {
674 LOGV("stop the player first");
675 mVePlayer[playerInst]->stop();
676 }
677 if (playerInst == mActivePlayerIndex) {
678 // Return the last rendered frame time stamp
679 mVePlayer[mActivePlayerIndex]->getLastRenderedTimeMs(&lastRenderedFrameTimeMs);
680 }
Raghavender Pallafa31daf2011-03-18 22:32:51 -0700681
682 //This is used to syncronize onStreamDone() in PreviewPlayer and
Chih-Chung Chang99698662011-06-30 14:21:38 +0800683 //stopPreview() in PreviewController
Raghavender Pallafa31daf2011-03-18 22:32:51 -0700684 sp<VideoEditorPlayer> temp = mVePlayer[playerInst];
685 temp->acquireLock();
686 LOGV("stopPreview: clearing mVePlayer");
687 mVePlayer[playerInst].clear();
Chih-Chung Chang99698662011-06-30 14:21:38 +0800688 mVePlayer[playerInst] = NULL;
Raghavender Pallafa31daf2011-03-18 22:32:51 -0700689 temp->releaseLock();
Chih-Chung Chang99698662011-06-30 14:21:38 +0800690 }
Raghavender Pallafa31daf2011-03-18 22:32:51 -0700691 }
Chih-Chung Chang99698662011-06-30 14:21:38 +0800692 LOGV("stopPreview: clear audioSink and audioPlayer");
693 mVEAudioSink.clear();
694 if (mVEAudioPlayer) {
695 delete mVEAudioPlayer;
696 mVEAudioPlayer = NULL;
697 }
698
Chih-Chung Chang43fcc392011-08-02 16:17:39 +0800699 delete mNativeWindowRenderer;
700 mNativeWindowRenderer = NULL;
701
Chih-Chung Chang99698662011-06-30 14:21:38 +0800702 // If image file playing, then free the buffer pointer
703 if(mFrameStr.pBuffer != M4OSA_NULL) {
704 free(mFrameStr.pBuffer);
705 mFrameStr.pBuffer = M4OSA_NULL;
706 }
707
708 // Reset original begin cuttime of first previewed clip*/
709 mClipList[mStartingClipIndex]->uiBeginCutTime = mFirstPreviewClipBeginTime;
710 // Reset original end cuttime of last previewed clip*/
711 mClipList[mNumberClipsToPreview-1]->uiEndCutTime = mLastPreviewClipEndTime;
712
713 mPlayerState = VePlayerIdle;
714 mPrepareReqest = M4OSA_FALSE;
715
716 mCurrentPlayedDuration = 0;
717 mCurrentClipDuration = 0;
718 mRenderingMode = M4xVSS_kBlackBorders;
719 mOutputVideoWidth = 0;
720 mOutputVideoHeight = 0;
721
722 LOGV("stopPreview() lastRenderedFrameTimeMs %ld", lastRenderedFrameTimeMs);
723 return lastRenderedFrameTimeMs;
724}
725
726M4OSA_ERR VideoEditorPreviewController::clearSurface(
727 const sp<Surface> &surface, VideoEditor_renderPreviewFrameStr* pFrameInfo) {
728
729 M4OSA_ERR err = M4NO_ERROR;
730 VideoEditor_renderPreviewFrameStr* pFrameStr = pFrameInfo;
731 M4OSA_UInt32 outputBufferWidth =0, outputBufferHeight=0;
732 M4VIFI_ImagePlane planeOut[3];
733 LOGV("Inside preview clear frame");
734
735 Mutex::Autolock autoLock(mLock);
736
737 // Delete previous renderer instance
738 if(mTarget != NULL) {
739 delete mTarget;
740 mTarget = NULL;
741 }
742
743 outputBufferWidth = pFrameStr->uiFrameWidth;
744 outputBufferHeight = pFrameStr->uiFrameHeight;
745
746 // Initialize the renderer
747 if(mTarget == NULL) {
748
749 mTarget = PreviewRenderer::CreatePreviewRenderer(
750 OMX_COLOR_FormatYUV420Planar, surface, outputBufferWidth, outputBufferHeight,
751 outputBufferWidth, outputBufferHeight, 0);
752
753 if(mTarget == NULL) {
754 LOGE("renderPreviewFrame: cannot create PreviewRenderer");
755 return M4ERR_ALLOC;
756 }
757 }
758
759 // Out plane
760 uint8_t* outBuffer;
761 size_t outBufferStride = 0;
762
763 LOGV("doMediaRendering CALL getBuffer()");
764 mTarget->getBufferYV12(&outBuffer, &outBufferStride);
765
766 // Set the output YUV420 plane to be compatible with YV12 format
767 //In YV12 format, sizes must be even
768 M4OSA_UInt32 yv12PlaneWidth = ((outputBufferWidth +1)>>1)<<1;
769 M4OSA_UInt32 yv12PlaneHeight = ((outputBufferHeight+1)>>1)<<1;
770
771 prepareYV12ImagePlane(planeOut, yv12PlaneWidth, yv12PlaneHeight,
772 (M4OSA_UInt32)outBufferStride, (M4VIFI_UInt8 *)outBuffer);
773
774 /* Fill the surface with black frame */
775 memset((void *)planeOut[0].pac_data,0x00,planeOut[0].u_width *
776 planeOut[0].u_height * 1.5);
777 memset((void *)planeOut[1].pac_data,128,planeOut[1].u_width *
778 planeOut[1].u_height);
779 memset((void *)planeOut[2].pac_data,128,planeOut[2].u_width *
780 planeOut[2].u_height);
781
782 mTarget->renderYV12();
783 return err;
784}
785
786M4OSA_ERR VideoEditorPreviewController::renderPreviewFrame(
787 const sp<Surface> &surface,
788 VideoEditor_renderPreviewFrameStr* pFrameInfo,
789 VideoEditorCurretEditInfo *pCurrEditInfo) {
790
791 M4OSA_ERR err = M4NO_ERROR;
792 M4OSA_UInt32 i = 0, iIncrementedDuration = 0, tnTimeMs=0, framesize =0;
793 VideoEditor_renderPreviewFrameStr* pFrameStr = pFrameInfo;
794 M4VIFI_UInt8 *pixelArray = NULL;
795 Mutex::Autolock autoLock(mLock);
796
797 if (pCurrEditInfo != NULL) {
798 pCurrEditInfo->overlaySettingsIndex = -1;
799 }
800 // Delete previous renderer instance
801 if(mTarget != NULL) {
802 delete mTarget;
803 mTarget = NULL;
804 }
805
806 if(mOutputVideoWidth == 0) {
807 mOutputVideoWidth = pFrameStr->uiFrameWidth;
808 }
809
810 if(mOutputVideoHeight == 0) {
811 mOutputVideoHeight = pFrameStr->uiFrameHeight;
812 }
813
814 // Initialize the renderer
815 if(mTarget == NULL) {
816 /*mTarget = new PreviewRenderer(
817 OMX_COLOR_FormatYUV420Planar, surface, mOutputVideoWidth, mOutputVideoHeight,
818 mOutputVideoWidth, mOutputVideoHeight, 0);*/
819
820 mTarget = PreviewRenderer::CreatePreviewRenderer(
821 OMX_COLOR_FormatYUV420Planar, surface, mOutputVideoWidth, mOutputVideoHeight,
822 mOutputVideoWidth, mOutputVideoHeight, 0);
823
824 if(mTarget == NULL) {
825 LOGE("renderPreviewFrame: cannot create PreviewRenderer");
826 return M4ERR_ALLOC;
827 }
828 }
829
830 pixelArray = NULL;
831
832 // Postprocessing (apply video effect)
833 if(pFrameStr->bApplyEffect == M4OSA_TRUE) {
834
835 for(i=0;i<mNumberEffects;i++) {
836 // First check if effect starttime matches the clip being previewed
837 if((mEffectsSettings[i].uiStartTime < pFrameStr->clipBeginCutTime)
838 ||(mEffectsSettings[i].uiStartTime >= pFrameStr->clipEndCutTime)) {
839 // This effect doesn't belong to this clip, check next one
840 continue;
841 }
842 if((mEffectsSettings[i].uiStartTime <= pFrameStr->timeMs) &&
843 ((mEffectsSettings[i].uiStartTime+mEffectsSettings[i].uiDuration) >=
844 pFrameStr->timeMs) && (mEffectsSettings[i].uiDuration != 0)) {
845 setVideoEffectType(mEffectsSettings[i].VideoEffectType, TRUE);
846 }
847 else {
848 setVideoEffectType(mEffectsSettings[i].VideoEffectType, FALSE);
849 }
850 }
851
852 //Provide the overlay Update indication when there is an overlay effect
853 if (mCurrentVideoEffect & VIDEO_EFFECT_FRAMING) {
854 M4OSA_UInt32 index;
855 mCurrentVideoEffect &= ~VIDEO_EFFECT_FRAMING; //never apply framing here.
856
857 // Find the effect in effectSettings array
858 for (index = 0; index < mNumberEffects; index++) {
859 if(mEffectsSettings[index].VideoEffectType ==
860 (M4VSS3GPP_VideoEffectType)M4xVSS_kVideoEffectType_Framing) {
861
862 if((mEffectsSettings[index].uiStartTime <= pFrameInfo->timeMs) &&
863 ((mEffectsSettings[index].uiStartTime+
864 mEffectsSettings[index].uiDuration) >= pFrameInfo->timeMs))
865 {
866 break;
867 }
868 }
869 }
870 if ((index < mNumberEffects) && (pCurrEditInfo != NULL)) {
871 pCurrEditInfo->overlaySettingsIndex = index;
872 LOGV("Framing index = %d", index);
873 } else {
874 LOGV("No framing effects found");
875 }
876 }
877
878 if(mCurrentVideoEffect != VIDEO_EFFECT_NONE) {
879 err = applyVideoEffect((M4OSA_Void *)pFrameStr->pBuffer,
880 OMX_COLOR_FormatYUV420Planar, pFrameStr->uiFrameWidth,
881 pFrameStr->uiFrameHeight, pFrameStr->timeMs,
882 (M4OSA_Void *)pixelArray);
883
884 if(err != M4NO_ERROR) {
885 LOGE("renderPreviewFrame: applyVideoEffect error 0x%x", (unsigned int)err);
886 delete mTarget;
887 mTarget = NULL;
888 free(pixelArray);
889 pixelArray = NULL;
890 return err;
891 }
892 mCurrentVideoEffect = VIDEO_EFFECT_NONE;
893 }
894 else {
895 // Apply the rendering mode
896 err = doImageRenderingMode((M4OSA_Void *)pFrameStr->pBuffer,
897 OMX_COLOR_FormatYUV420Planar, pFrameStr->uiFrameWidth,
898 pFrameStr->uiFrameHeight, (M4OSA_Void *)pixelArray);
899
900 if(err != M4NO_ERROR) {
901 LOGE("renderPreviewFrame:doImageRenderingMode error 0x%x", (unsigned int)err);
902 delete mTarget;
903 mTarget = NULL;
904 free(pixelArray);
905 pixelArray = NULL;
906 return err;
907 }
908 }
909 }
910 else {
911 // Apply the rendering mode
912 err = doImageRenderingMode((M4OSA_Void *)pFrameStr->pBuffer,
913 OMX_COLOR_FormatYUV420Planar, pFrameStr->uiFrameWidth,
914 pFrameStr->uiFrameHeight, (M4OSA_Void *)pixelArray);
915
916 if(err != M4NO_ERROR) {
917 LOGE("renderPreviewFrame: doImageRenderingMode error 0x%x", (unsigned int)err);
918 delete mTarget;
919 mTarget = NULL;
920 free(pixelArray);
921 pixelArray = NULL;
922 return err;
923 }
924 }
925
926 mTarget->renderYV12();
927 return err;
928}
929
930M4OSA_Void VideoEditorPreviewController::setJniCallback(void* cookie,
931 jni_progress_callback_fct callbackFct) {
932 //LOGV("setJniCallback");
933 mJniCookie = cookie;
934 mJniCallback = callbackFct;
935}
936
937M4OSA_ERR VideoEditorPreviewController::preparePlayer(
938 void* param, int playerInstance, int index) {
939
940 M4OSA_ERR err = M4NO_ERROR;
941 VideoEditorPreviewController *pController =
942 (VideoEditorPreviewController *)param;
943
944 LOGV("preparePlayer: instance %d file %d", playerInstance, index);
945
946 pController->mVePlayer[playerInstance]->setDataSource(
947 (const char *)pController->mClipList[index]->pFile, NULL);
948 LOGV("preparePlayer: setDataSource instance %s",
949 (const char *)pController->mClipList[index]->pFile);
950
951 pController->mVePlayer[playerInstance]->setVideoSurface(
952 pController->mSurface);
953 LOGV("preparePlayer: setVideoSurface");
954
955 pController->mVePlayer[playerInstance]->setMediaRenderingMode(
956 pController->mClipList[index]->xVSS.MediaRendering,
957 pController->mOutputVideoSize);
958 LOGV("preparePlayer: setMediaRenderingMode");
959
960 if((M4OSA_UInt32)index == pController->mStartingClipIndex) {
961 pController->mVePlayer[playerInstance]->setPlaybackBeginTime(
962 pController->mFirstPreviewClipBeginTime);
963 }
964 else {
965 pController->mVePlayer[playerInstance]->setPlaybackBeginTime(
966 pController->mClipList[index]->uiBeginCutTime);
967 }
968 LOGV("preparePlayer: setPlaybackBeginTime(%d)",
969 pController->mClipList[index]->uiBeginCutTime);
970
971 pController->mVePlayer[playerInstance]->setPlaybackEndTime(
972 pController->mClipList[index]->uiEndCutTime);
973 LOGV("preparePlayer: setPlaybackEndTime(%d)",
974 pController->mClipList[index]->uiEndCutTime);
975
976 if(pController->mClipList[index]->FileType == M4VIDEOEDITING_kFileType_ARGB8888) {
977 pController->mVePlayer[playerInstance]->setImageClipProperties(
978 pController->mClipList[index]->ClipProperties.uiVideoWidth,
979 pController->mClipList[index]->ClipProperties.uiVideoHeight);
980 LOGV("preparePlayer: setImageClipProperties");
981 }
982
983 pController->mVePlayer[playerInstance]->prepare();
984 LOGV("preparePlayer: prepared");
985
986 if(pController->mClipList[index]->uiBeginCutTime > 0) {
987 pController->mVePlayer[playerInstance]->seekTo(
988 pController->mClipList[index]->uiBeginCutTime);
989
990 LOGV("preparePlayer: seekTo(%d)",
991 pController->mClipList[index]->uiBeginCutTime);
992 }
993 pController->mVePlayer[pController->mCurrentPlayer]->setAudioPlayer(pController->mVEAudioPlayer);
994
995 pController->mVePlayer[playerInstance]->readFirstVideoFrame();
996 LOGV("preparePlayer: readFirstVideoFrame of clip");
997
998 return err;
999}
1000
1001M4OSA_ERR VideoEditorPreviewController::threadProc(M4OSA_Void* param) {
1002 M4OSA_ERR err = M4NO_ERROR;
1003 M4OSA_Int32 index = 0;
1004 VideoEditorPreviewController *pController =
1005 (VideoEditorPreviewController *)param;
1006
1007 LOGV("inside threadProc");
1008 if(pController->mPlayerState == VePlayerIdle) {
1009 (pController->mCurrentClipNumber)++;
1010
1011 LOGV("threadProc: playing file index %d total clips %d",
1012 pController->mCurrentClipNumber, pController->mNumberClipsToPreview);
1013
1014 if((M4OSA_UInt32)pController->mCurrentClipNumber >=
1015 pController->mNumberClipsToPreview) {
1016
1017 LOGV("All clips previewed");
1018
1019 pController->mCurrentPlayedDuration = 0;
1020 pController->mCurrentClipDuration = 0;
1021 pController->mCurrentPlayer = 0;
1022
1023 if(pController->mPreviewLooping == M4OSA_TRUE) {
1024 pController->mCurrentClipNumber =
1025 pController->mStartingClipIndex;
1026
1027 LOGV("Preview looping TRUE, restarting from clip index %d",
1028 pController->mCurrentClipNumber);
1029
1030 // Reset the story board timestamp inside the player
1031 for (int playerInst=0; playerInst<NBPLAYER_INSTANCES;
1032 playerInst++) {
1033 pController->mVePlayer[playerInst]->resetJniCallbackTimeStamp();
1034 }
1035 }
1036 else {
1037 M4OSA_UInt32 endArgs = 0;
1038 if(pController->mJniCallback != NULL) {
1039 pController->mJniCallback(
1040 pController->mJniCookie, MSG_TYPE_PREVIEW_END, &endArgs);
1041 }
1042 pController->mPlayerState = VePlayerAutoStop;
1043
1044 // Reset original begin cuttime of first previewed clip
1045 pController->mClipList[pController->mStartingClipIndex]->uiBeginCutTime =
1046 pController->mFirstPreviewClipBeginTime;
1047 // Reset original end cuttime of last previewed clip
1048 pController->mClipList[pController->mNumberClipsToPreview-1]->uiEndCutTime =
1049 pController->mLastPreviewClipEndTime;
1050
1051 // Return a warning to M4OSA thread handler
1052 // so that thread is moved from executing state to open state
1053 return M4WAR_NO_MORE_STREAM;
1054 }
1055 }
1056
1057 index=pController->mCurrentClipNumber;
1058 if((M4OSA_UInt32)pController->mCurrentClipNumber == pController->mStartingClipIndex) {
1059 pController->mCurrentPlayedDuration +=
1060 pController->mVideoStoryBoardTimeMsUptoFirstPreviewClip;
1061
1062 pController->mCurrentClipDuration =
1063 pController->mClipList[pController->mCurrentClipNumber]->uiEndCutTime
1064 - pController->mFirstPreviewClipBeginTime;
1065
1066 preparePlayer((void*)pController, pController->mCurrentPlayer, index);
1067 }
1068 else {
1069 pController->mCurrentPlayedDuration +=
1070 pController->mCurrentClipDuration;
1071
1072 pController->mCurrentClipDuration =
1073 pController->mClipList[pController->mCurrentClipNumber]->uiEndCutTime -
1074 pController->mClipList[pController->mCurrentClipNumber]->uiBeginCutTime;
1075 }
1076
1077 pController->mVePlayer[pController->mCurrentPlayer]->setStoryboardStartTime(
1078 pController->mCurrentPlayedDuration);
1079 LOGV("threadProc: setStoryboardStartTime");
1080
1081 // Set the next clip duration for Audio mix here
1082 if((M4OSA_UInt32)pController->mCurrentClipNumber != pController->mStartingClipIndex) {
1083
1084 pController->mVePlayer[pController->mCurrentPlayer]->setAudioMixStoryBoardParam(
1085 pController->mCurrentPlayedDuration,
1086 pController->mClipList[index]->uiBeginCutTime,
1087 pController->mClipList[index]->ClipProperties.uiClipAudioVolumePercentage);
1088
1089 LOGV("threadProc: setAudioMixStoryBoardParam fromMS %d \
1090 ClipBeginTime %d", pController->mCurrentPlayedDuration +
1091 pController->mClipList[index]->uiBeginCutTime,
1092 pController->mClipList[index]->uiBeginCutTime,
1093 pController->mClipList[index]->ClipProperties.uiClipAudioVolumePercentage);
1094 }
1095 // Capture the active player being used
1096 pController->mActivePlayerIndex = pController->mCurrentPlayer;
1097
1098 pController->mVePlayer[pController->mCurrentPlayer]->start();
1099 LOGV("threadProc: started");
1100
1101 pController->mPlayerState = VePlayerBusy;
1102
1103 } else if(pController->mPlayerState == VePlayerAutoStop) {
1104 LOGV("Preview completed..auto stop the player");
1105 } else if ((pController->mPlayerState == VePlayerBusy) && (pController->mPrepareReqest)) {
1106 // Prepare the player here
1107 pController->mPrepareReqest = M4OSA_FALSE;
1108 preparePlayer((void*)pController, pController->mCurrentPlayer,
1109 pController->mCurrentClipNumber+1);
1110 if (pController->mSemThreadWait != NULL) {
1111 err = M4OSA_semaphoreWait(pController->mSemThreadWait,
1112 M4OSA_WAIT_FOREVER);
1113 }
1114 } else {
1115 if (!pController->bStopThreadInProgress) {
1116 LOGV("threadProc: state busy...wait for sem");
1117 if (pController->mSemThreadWait != NULL) {
1118 err = M4OSA_semaphoreWait(pController->mSemThreadWait,
1119 M4OSA_WAIT_FOREVER);
1120 }
1121 }
1122 LOGV("threadProc: sem wait returned err = 0x%x", err);
1123 }
1124
1125 //Always return M4NO_ERROR to ensure the thread keeps running
1126 return M4NO_ERROR;
1127}
1128
1129void VideoEditorPreviewController::notify(
1130 void* cookie, int msg, int ext1, int ext2)
1131{
1132 VideoEditorPreviewController *pController =
1133 (VideoEditorPreviewController *)cookie;
1134
1135 M4OSA_ERR err = M4NO_ERROR;
1136 uint32_t clipDuration = 0;
1137 switch (msg) {
1138 case MEDIA_NOP: // interface test message
1139 LOGV("MEDIA_NOP");
1140 break;
1141 case MEDIA_PREPARED:
1142 LOGV("MEDIA_PREPARED");
1143 break;
1144 case MEDIA_PLAYBACK_COMPLETE:
1145 {
1146 LOGV("notify:MEDIA_PLAYBACK_COMPLETE");
1147 pController->mPlayerState = VePlayerIdle;
1148
1149 //send progress callback with last frame timestamp
1150 if((M4OSA_UInt32)pController->mCurrentClipNumber ==
1151 pController->mStartingClipIndex) {
1152 clipDuration =
1153 pController->mClipList[pController->mCurrentClipNumber]->uiEndCutTime
1154 - pController->mFirstPreviewClipBeginTime;
1155 }
1156 else {
1157 clipDuration =
1158 pController->mClipList[pController->mCurrentClipNumber]->uiEndCutTime
1159 - pController->mClipList[pController->mCurrentClipNumber]->uiBeginCutTime;
1160 }
1161
1162 M4OSA_UInt32 playedDuration = clipDuration+pController->mCurrentPlayedDuration;
1163 pController->mJniCallback(
1164 pController->mJniCookie, MSG_TYPE_PROGRESS_INDICATION,
1165 &playedDuration);
1166
1167 if ((pController->mOverlayState == OVERLAY_UPDATE) &&
1168 ((M4OSA_UInt32)pController->mCurrentClipNumber !=
1169 (pController->mNumberClipsToPreview-1))) {
1170 VideoEditorCurretEditInfo *pEditInfo =
1171 (VideoEditorCurretEditInfo*)M4OSA_32bitAlignedMalloc(sizeof(VideoEditorCurretEditInfo),
1172 M4VS, (M4OSA_Char*)"Current Edit info");
1173 pEditInfo->overlaySettingsIndex = ext2;
1174 pEditInfo->clipIndex = pController->mCurrentClipNumber;
1175 pController->mOverlayState == OVERLAY_CLEAR;
1176 if (pController->mJniCallback != NULL) {
1177 pController->mJniCallback(pController->mJniCookie,
1178 MSG_TYPE_OVERLAY_CLEAR, pEditInfo);
1179 }
1180 free(pEditInfo);
1181 }
1182 {
1183 Mutex::Autolock autoLock(pController->mLockSem);
1184 if (pController->mSemThreadWait != NULL) {
Raghavender Pallafa31daf2011-03-18 22:32:51 -07001185 M4OSA_semaphorePost(pController->mSemThreadWait);
1186 return;
Chih-Chung Chang99698662011-06-30 14:21:38 +08001187 }
1188 }
1189
1190 break;
1191 }
1192 case MEDIA_ERROR:
1193 {
1194 int err_val = ext1;
1195 // Always log errors.
1196 // ext1: Media framework error code.
1197 // ext2: Implementation dependant error code.
1198 LOGE("MEDIA_ERROR; error (%d, %d)", ext1, ext2);
1199 if(pController->mJniCallback != NULL) {
1200 pController->mJniCallback(pController->mJniCookie,
1201 MSG_TYPE_PLAYER_ERROR, &err_val);
1202 }
1203 break;
1204 }
1205 case MEDIA_INFO:
1206 {
1207 int info_val = ext2;
1208 // ext1: Media framework error code.
1209 // ext2: Implementation dependant error code.
1210 //LOGW("MEDIA_INFO; info/warning (%d, %d)", ext1, ext2);
1211 if(pController->mJniCallback != NULL) {
1212 pController->mJniCallback(pController->mJniCookie,
1213 MSG_TYPE_PROGRESS_INDICATION, &info_val);
1214 }
1215 break;
1216 }
1217 case MEDIA_SEEK_COMPLETE:
1218 LOGV("MEDIA_SEEK_COMPLETE; Received seek complete");
1219 break;
1220 case MEDIA_BUFFERING_UPDATE:
1221 LOGV("MEDIA_BUFFERING_UPDATE; buffering %d", ext1);
1222 break;
1223 case MEDIA_SET_VIDEO_SIZE:
1224 LOGV("MEDIA_SET_VIDEO_SIZE; New video size %d x %d", ext1, ext2);
1225 break;
1226 case 0xAAAAAAAA:
1227 LOGV("VIDEO PLAYBACK ALMOST over, prepare next player");
1228 // Select next player and prepare it
1229 // If there is a clip after this one
1230 if ((M4OSA_UInt32)(pController->mCurrentClipNumber+1) <
1231 pController->mNumberClipsToPreview) {
1232 pController->mPrepareReqest = M4OSA_TRUE;
1233 pController->mCurrentPlayer++;
1234 if (pController->mCurrentPlayer >= NBPLAYER_INSTANCES) {
1235 pController->mCurrentPlayer = 0;
1236 }
1237 // Prepare the first clip to be played
1238 {
1239 Mutex::Autolock autoLock(pController->mLockSem);
1240 if (pController->mSemThreadWait != NULL) {
1241 M4OSA_semaphorePost(pController->mSemThreadWait);
1242 }
1243 }
1244 }
1245 break;
1246 case 0xBBBBBBBB:
1247 {
1248 LOGV("VIDEO PLAYBACK, Update Overlay");
1249 int overlayIndex = ext2;
1250 VideoEditorCurretEditInfo *pEditInfo =
1251 (VideoEditorCurretEditInfo*)M4OSA_32bitAlignedMalloc(sizeof(VideoEditorCurretEditInfo),
1252 M4VS, (M4OSA_Char*)"Current Edit info");
1253 //ext1 = 1; start the overlay display
1254 // = 2; Clear the overlay.
1255 pEditInfo->overlaySettingsIndex = ext2;
1256 pEditInfo->clipIndex = pController->mCurrentClipNumber;
1257 LOGV("pController->mCurrentClipNumber = %d",pController->mCurrentClipNumber);
1258 if (pController->mJniCallback != NULL) {
1259 if (ext1 == 1) {
1260 pController->mOverlayState = OVERLAY_UPDATE;
1261 pController->mJniCallback(pController->mJniCookie,
1262 MSG_TYPE_OVERLAY_UPDATE, pEditInfo);
1263 } else {
1264 pController->mOverlayState = OVERLAY_CLEAR;
1265 pController->mJniCallback(pController->mJniCookie,
1266 MSG_TYPE_OVERLAY_CLEAR, pEditInfo);
1267 }
1268 }
1269 free(pEditInfo);
1270 break;
1271 }
1272 default:
1273 LOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2);
1274 break;
1275 }
1276}
1277
1278void VideoEditorPreviewController::setVideoEffectType(
1279 M4VSS3GPP_VideoEffectType type, M4OSA_Bool enable) {
1280
1281 M4OSA_UInt32 effect = VIDEO_EFFECT_NONE;
1282
1283 // map M4VSS3GPP_VideoEffectType to local enum
1284 switch(type) {
1285 case M4VSS3GPP_kVideoEffectType_FadeFromBlack:
1286 effect = VIDEO_EFFECT_FADEFROMBLACK;
1287 break;
1288
1289 case M4VSS3GPP_kVideoEffectType_FadeToBlack:
1290 effect = VIDEO_EFFECT_FADETOBLACK;
1291 break;
1292
Chih-Chung Chang99698662011-06-30 14:21:38 +08001293 case M4xVSS_kVideoEffectType_BlackAndWhite:
1294 effect = VIDEO_EFFECT_BLACKANDWHITE;
1295 break;
1296
1297 case M4xVSS_kVideoEffectType_Pink:
1298 effect = VIDEO_EFFECT_PINK;
1299 break;
1300
1301 case M4xVSS_kVideoEffectType_Green:
1302 effect = VIDEO_EFFECT_GREEN;
1303 break;
1304
1305 case M4xVSS_kVideoEffectType_Sepia:
1306 effect = VIDEO_EFFECT_SEPIA;
1307 break;
1308
1309 case M4xVSS_kVideoEffectType_Negative:
1310 effect = VIDEO_EFFECT_NEGATIVE;
1311 break;
1312
1313 case M4xVSS_kVideoEffectType_Framing:
1314 effect = VIDEO_EFFECT_FRAMING;
1315 break;
1316
1317 case M4xVSS_kVideoEffectType_Fifties:
1318 effect = VIDEO_EFFECT_FIFTIES;
1319 break;
1320
1321 case M4xVSS_kVideoEffectType_ColorRGB16:
1322 effect = VIDEO_EFFECT_COLOR_RGB16;
1323 break;
1324
1325 case M4xVSS_kVideoEffectType_Gradient:
1326 effect = VIDEO_EFFECT_GRADIENT;
1327 break;
1328
1329 default:
1330 effect = VIDEO_EFFECT_NONE;
1331 break;
1332 }
1333
1334 if(enable == M4OSA_TRUE) {
1335 // If already set, then no need to set again
1336 if(!(mCurrentVideoEffect & effect))
1337 mCurrentVideoEffect |= effect;
1338 if(effect == VIDEO_EFFECT_FIFTIES) {
1339 mIsFiftiesEffectStarted = true;
1340 }
1341 }
1342 else {
1343 // Reset only if already set
1344 if(mCurrentVideoEffect & effect)
1345 mCurrentVideoEffect &= ~effect;
1346 }
1347
1348 return;
1349}
1350
1351
1352M4OSA_ERR VideoEditorPreviewController::applyVideoEffect(
1353 M4OSA_Void * dataPtr, M4OSA_UInt32 colorFormat, M4OSA_UInt32 videoWidth,
1354 M4OSA_UInt32 videoHeight, M4OSA_UInt32 timeMs, M4OSA_Void* outPtr) {
1355
1356 M4OSA_ERR err = M4NO_ERROR;
1357 vePostProcessParams postProcessParams;
1358
1359 postProcessParams.vidBuffer = (M4VIFI_UInt8*)dataPtr;
1360 postProcessParams.videoWidth = videoWidth;
1361 postProcessParams.videoHeight = videoHeight;
1362 postProcessParams.timeMs = timeMs;
1363 postProcessParams.timeOffset = 0; //Since timeMS already takes care of offset in this case
1364 postProcessParams.effectsSettings = mEffectsSettings;
1365 postProcessParams.numberEffects = mNumberEffects;
1366 postProcessParams.outVideoWidth = mOutputVideoWidth;
1367 postProcessParams.outVideoHeight = mOutputVideoHeight;
1368 postProcessParams.currentVideoEffect = mCurrentVideoEffect;
1369 postProcessParams.renderingMode = mRenderingMode;
1370 if(mIsFiftiesEffectStarted == M4OSA_TRUE) {
1371 postProcessParams.isFiftiesEffectStarted = M4OSA_TRUE;
1372 mIsFiftiesEffectStarted = M4OSA_FALSE;
1373 }
1374 else {
1375 postProcessParams.isFiftiesEffectStarted = M4OSA_FALSE;
1376 }
1377 //postProcessParams.renderer = mTarget;
1378 postProcessParams.overlayFrameRGBBuffer = NULL;
1379 postProcessParams.overlayFrameYUVBuffer = NULL;
1380
1381 mTarget->getBufferYV12(&(postProcessParams.pOutBuffer), &(postProcessParams.outBufferStride));
1382
1383 err = applyEffectsAndRenderingMode(&postProcessParams, videoWidth, videoHeight);
1384 return err;
1385}
1386
Hong Teng8806b702011-07-06 18:29:28 -07001387status_t VideoEditorPreviewController::setPreviewFrameRenderingMode(
Chih-Chung Chang99698662011-06-30 14:21:38 +08001388 M4xVSS_MediaRendering mode, M4VIDEOEDITING_VideoFrameSize outputVideoSize) {
1389
1390 LOGV("setMediaRenderingMode: outputVideoSize = %d", outputVideoSize);
1391 mRenderingMode = mode;
1392
Hong Teng8806b702011-07-06 18:29:28 -07001393 status_t err = OK;
1394 /* get the video width and height by resolution */
1395 err = getVideoSizeByResolution(outputVideoSize,
1396 &mOutputVideoWidth, &mOutputVideoHeight);
Chih-Chung Chang99698662011-06-30 14:21:38 +08001397
Hong Teng8806b702011-07-06 18:29:28 -07001398 return err;
Chih-Chung Chang99698662011-06-30 14:21:38 +08001399}
1400
1401M4OSA_ERR VideoEditorPreviewController::doImageRenderingMode(
1402 M4OSA_Void * dataPtr, M4OSA_UInt32 colorFormat, M4OSA_UInt32 videoWidth,
1403 M4OSA_UInt32 videoHeight, M4OSA_Void* outPtr) {
1404
1405 M4OSA_ERR err = M4NO_ERROR;
1406 M4VIFI_ImagePlane planeIn[3], planeOut[3];
1407 M4VIFI_UInt8 *inBuffer = M4OSA_NULL;
1408 M4OSA_UInt32 outputBufferWidth =0, outputBufferHeight=0;
1409
1410 //frameSize = (videoWidth*videoHeight*3) >> 1;
1411 inBuffer = (M4OSA_UInt8 *)dataPtr;
1412
1413 // In plane
1414 prepareYUV420ImagePlane(planeIn, videoWidth,
1415 videoHeight, (M4VIFI_UInt8 *)inBuffer, videoWidth, videoHeight);
1416
1417 outputBufferWidth = mOutputVideoWidth;
1418 outputBufferHeight = mOutputVideoHeight;
1419
1420 // Out plane
1421 uint8_t* outBuffer;
1422 size_t outBufferStride = 0;
1423
1424 LOGV("doMediaRendering CALL getBuffer()");
1425 mTarget->getBufferYV12(&outBuffer, &outBufferStride);
1426
1427 // Set the output YUV420 plane to be compatible with YV12 format
1428 //In YV12 format, sizes must be even
1429 M4OSA_UInt32 yv12PlaneWidth = ((mOutputVideoWidth +1)>>1)<<1;
1430 M4OSA_UInt32 yv12PlaneHeight = ((mOutputVideoHeight+1)>>1)<<1;
1431
1432 prepareYV12ImagePlane(planeOut, yv12PlaneWidth, yv12PlaneHeight,
1433 (M4OSA_UInt32)outBufferStride, (M4VIFI_UInt8 *)outBuffer);
1434
1435 err = applyRenderingMode(planeIn, planeOut, mRenderingMode);
1436 if(err != M4NO_ERROR) {
1437 LOGE("doImageRenderingMode: applyRenderingMode returned err=0x%x", (unsigned int)err);
1438 }
1439 return err;
1440}
1441
1442} //namespace android