3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.1 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 #include <FMediaImageBuffer.h>
21 #include "MediaPlayerForm.h"
22 #include "MultiForm.h"
24 using namespace Tizen::Base;
25 using namespace Tizen::Base::Utility;
26 using namespace Tizen::Ui::Controls;
27 using namespace Tizen::Media;
28 using namespace Tizen::Graphics;
29 using namespace Tizen::Base::Runtime;
30 using namespace Tizen::Io;
31 using namespace Tizen::App;
32 using namespace Tizen::System;
33 using namespace Tizen::Ui::Scenes;
36 static const int SPACE_FOR_PROGRESS_BAR = 160;
37 static const int SPACE_FOR_EDIT_AREA = 200;
38 static const int HEIGHT_OF_PROGRESS_BAR = 40;
39 static const int HEIGHT_OF_TIME_LABLE = 40;
40 static const int WIDTH_OF_TIME_LABLE = 160;
41 static const int FONT_SIZE_OF_TIME = 25;
42 static const int RANGE_OF_PROGRESS_BAR = 100;
43 static const int PROGRESS_BAR_X = 5;
44 static const int KILO_BYTE = 1024;
45 static const int MAX_ERROR_MSG_LENGTH = 3000;
46 static const int ASPECT_RATIO_WIDTH = 4;
47 static const int ASPECT_RATIO_HEIGHT = 3;
48 static const int REQUIRE_WIDTH_PERCENTAGE_LANDSCAPE = 85;
49 static const int MILI_SEC = 1000;
50 static const int SEC = 60;
51 static const int MAX_TIME = 10;
53 static const int DEVICE_PORTRAIT = 0;
54 static const int DEVICE_PORTRAIT_REVERSE = 1;
55 static const int DEVICE_LANDSCAPE = 3;
56 static const int DEVICE_LANDSCAPE_REVERSE = 2;
58 static const int TIMER_INTERVAL = 500;
59 static const int RESIZE_WIDTH = 320;
60 static const int RESIZE_HEIGHT = 240;
62 static const wchar_t* FORM_ID = L"MediaPlayerForm";
63 static const wchar_t* SCENE_ID = L"MediaPlayerScene";
65 MediaPlayerForm::MediaPlayerForm(void)
66 :__isMediaAudio(false),
67 __isMediaVideo(false),
68 __isMediaImage(false),
70 __progressBarWidth(0),
71 __isPausedInInterrupt(false),
72 __isPausedInBackGround(false),
73 __isPausedBeforeSeek(false),
86 MediaPlayerForm::MediaPlayerForm(Form* pForm, String* pStreamType, String* pfilePath)
87 :__isMediaAudio(false),
88 __isMediaVideo(false),
89 __isMediaImage(false),
91 __progressBarWidth(0),
92 __isPausedInInterrupt(false),
93 __isPausedInBackGround(false),
94 __isPausedBeforeSeek(false),
99 __pCalledForm (pForm),
100 __pFilePath(pfilePath),
108 if (pStreamType != null)
110 if (pStreamType->Equals(L"AUDIO", false))
112 __isMediaAudio = true;
114 else if (pStreamType->Equals(L"VIDEO", false))
116 __isMediaVideo = true;
118 else if (pStreamType->Equals(L"IMAGE", false))
120 __isMediaImage = true;
125 MediaPlayerForm::~MediaPlayerForm(void)
127 AppLog("~MediaPlayerForm ++");
129 if (__pPlayer != null)
135 if (__pRegion != null)
142 if (__pTimer != null)
149 AppLog("~MediaPlayerForm --");
153 MediaPlayerForm::CreateFormN(const Tizen::Ui::Scenes::SceneId &sceneId)
155 AppLog("CreateFormN called");
156 SceneManager* pSceneManager = SceneManager::GetInstance();
157 MediaPlayerForm* pForm = new (std::nothrow) MediaPlayerForm();
159 pSceneManager->AddSceneEventListener(sceneId, *(pForm->GetListener()));
163 MultiForm* MediaPlayerForm::FormCreateFunc()
165 MediaPlayerForm* pForm = MediaPlayerForm::CreateFormN(SCENE_ID);
169 String MediaPlayerForm::GetFormId()
174 SceneId MediaPlayerForm::GetSceneId()
180 MediaPlayerForm::GetListener(void)
187 MediaPlayerForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,
188 const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
190 TryReturnVoid(pArgs != null, "Arguments received are null");
191 TryReturnVoid(pArgs->GetCount() > 0, "Arguments GetCount is 0");
194 result r = E_SUCCESS;
195 Integer* pDeviceOrientation;
197 AppLog("pArgs->GetCount() is %d", pArgs->GetCount());
199 pMediaType = (String*)pArgs->GetAt(0);
200 AppLog("Media Type is %ls",pMediaType->GetPointer());
201 __pFilePath = (String*)pArgs->GetAt(1);
202 AppLog("pFilePath is %ls",__pFilePath->GetPointer());
203 pDeviceOrientation = ((Integer*)pArgs->GetAt(2));
205 SetOrientationInfo(pDeviceOrientation->ToInt());
207 if (pMediaType != null)
209 if (pMediaType->Equals(L"AUDIO", false))
211 __isMediaAudio = true;
212 SetFormStyle(FORM_STYLE_INDICATOR | FORM_STYLE_NORMAL | FORM_STYLE_HEADER | FORM_STYLE_FOOTER);
214 else if (pMediaType->Equals(L"VIDEO", false))
216 __isMediaVideo = true;
217 SetFormStyle(FORM_STYLE_INDICATOR | FORM_STYLE_NORMAL | FORM_STYLE_HEADER | FORM_STYLE_FOOTER);
219 else if (pMediaType->Equals(L"IMAGE", false))
221 __isMediaImage = true;
225 r = InitializeMediaPlayer();
226 TryReturnVoid(r == E_SUCCESS, "[%s] InitializeMediaPlayer failed", GetErrorMessage(r));
228 SAFE_DELETE_ARRAYLIST(pArgs);
231 MediaPlayerForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
232 const Tizen::Ui::Scenes::SceneId& nextSceneId)
234 AppLog("OnSceneDeactivated called");
238 MediaPlayerForm::SetOrientationInfo(int orientation)
242 case DEVICE_PORTRAIT:
243 __orientationInfo = CAMERA_EXIF_ORIENTATION_RIGHT_TOP;
246 case DEVICE_PORTRAIT_REVERSE:
247 __orientationInfo = CAMERA_EXIF_ORIENTATION_LEFT_BOTTOM;
250 case DEVICE_LANDSCAPE:
251 __orientationInfo = CAMERA_EXIF_ORIENTATION_TOP_LEFT;
254 case DEVICE_LANDSCAPE_REVERSE:
255 __orientationInfo = CAMERA_EXIF_ORIENTATION_BOTTOM_RIGHT;
259 __orientationInfo = 0;
263 AppLog("SetOrientationInfo is %d", orientation);
267 MediaPlayerForm::CreateProgressBar(void)
269 result r = E_SUCCESS;
270 unsigned int progBarY = ((GetClientAreaBounds().height - HEIGHT_OF_PROGRESS_BAR));
271 unsigned int timeLableY = ((GetClientAreaBounds().height - (HEIGHT_OF_PROGRESS_BAR + HEIGHT_OF_TIME_LABLE)));
273 __progressBarWidth = (GetClientAreaBounds().width - PROGRESS_BAR_X);
275 __pProgress = new (std::nothrow) Progress();
276 TryReturn(__pProgress != null, false,"E_OUT_OF_MEMORY in CreateProgressBar");
278 r = __pProgress->Construct(Rectangle(PROGRESS_BAR_X, progBarY, __progressBarWidth, HEIGHT_OF_PROGRESS_BAR), 0, RANGE_OF_PROGRESS_BAR);
279 TryReturn(r == E_SUCCESS, showPopupBool(L"Creation of Progress bar failed"),"[%s] __pProgress->Construct", GetErrorMessage(r));
281 __pProgress->SetValue(0);
282 __pProgress->SetRange(0, RANGE_OF_PROGRESS_BAR);
283 __pProgress->AddTouchEventListener(*this);
285 AddControl(__pProgress);
288 __startTime.Append(L"00");
289 __startTime.Append(L"");
290 __startTime.Append(L":");
291 __startTime.Append(L"00");
292 __startTime.Append(L"");
293 __startTime.Append(L":");
294 __startTime.Append(L"00");
296 //Create elapsed and end time lable
297 __pElapsedTimeLable = new (std::nothrow) Label();
298 TryReturn(__pElapsedTimeLable != null, false,"E_OUT_OF_MEMORY for __pElapsedTimeLable");
300 __pElapsedTimeLable->Construct(Rectangle(0, timeLableY, WIDTH_OF_TIME_LABLE, HEIGHT_OF_TIME_LABLE), L"");
301 __pElapsedTimeLable->SetTextConfig(FONT_SIZE_OF_TIME, LABEL_TEXT_STYLE_ITALIC);
302 AddControl(__pElapsedTimeLable);
304 __pEndTimeLable = new (std::nothrow) Label();
305 TryReturn(__pEndTimeLable != null, false,"E_OUT_OF_MEMORY for __pEndTimeLable");
307 __pEndTimeLable->Construct(Rectangle((GetClientAreaBounds().width - WIDTH_OF_TIME_LABLE), timeLableY, WIDTH_OF_TIME_LABLE, HEIGHT_OF_TIME_LABLE), L"");
308 __pEndTimeLable->SetTextConfig(FONT_SIZE_OF_TIME, LABEL_TEXT_STYLE_ITALIC);
309 AddControl(__pEndTimeLable);
311 __pElapsedTimeLable->SetText(__startTime);
312 __pEndTimeLable->SetText(__startTime);
319 MediaPlayerForm::Initialize(void)
321 result r = E_SUCCESS;
322 r = Form::Construct(FORM_STYLE_INDICATOR | FORM_STYLE_NORMAL | FORM_STYLE_HEADER);
323 TryReturn(r == E_SUCCESS, false,"[%s] Create() has failed", GetErrorMessage(r));
329 MediaPlayerForm::InitializeMediaPlayer(void)
333 if (__isMediaVideo == true)
335 SetHeaderText(L"Video Player");
337 else if (__isMediaAudio == true)
339 SetHeaderText(L"Audio Player");
341 else if (__isMediaImage == true)
343 SetHeaderText(L"Image Viewer");
344 SetFooterStyle(FOOTER_STYLE_BUTTON_TEXT, true, this);
345 SetFormBackEventListener(this);
347 if (InitPlayer() == false)
349 AppLog("InitPlayer failed");
350 showPopup(L"Creation of form failed");
354 if (Start() == false)
356 showPopupBool(L"MediaPlayerStart() failed");
362 SetFooterStyle(FOOTER_STYLE_BUTTON_TEXT, true, this);
363 AddFooterItem(L"Stop", IDC_BUTTON_STOP);
364 AddFooterItem(L"Play", IDC_BUTTON_PLAY);
365 SetFormBackEventListener(this);
366 Tizen::System::PowerManager::KeepScreenOnState(false, true);
367 if (InitPlayer() == false)
369 AppLog("InitPlayer failed");
370 showPopup(L"Creation of form failed");
374 if (Start() == false)
376 showPopupBool(L"MediaPlayerStart() failed");
383 MediaPlayerForm::OnInitializing(void)
385 result r = E_SUCCESS;
387 AppLog("OnInitializing called");
392 MediaPlayerForm::OnTimerExpired(Timer& timer)
394 if (__pPlayer != null)
396 AppLog("OnTimerExpired called");
398 long position = __pPlayer->GetPosition();
399 long clipDuration = __pPlayer->GetDuration();
401 if ( (clipDuration > 0) && (position <= clipDuration))
403 currStep = (RANGE_OF_PROGRESS_BAR * position) / (long) clipDuration;
406 __pProgress->SetValue(currStep);
407 __pProgress->RequestRedraw();
410 if (__pPlayer->GetState() == PLAYER_STATE_PLAYING)
412 __pTimer->Start(TIMER_INTERVAL);
418 MediaPlayerForm::OnTouchReleased(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition, const Tizen::Ui::TouchEventInfo& touchInfo)
423 MediaPlayerForm::InitPlayer(void)
425 result r = E_SUCCESS;
426 Tizen::Graphics::BufferInfo bufferInfo;
429 if (__isMediaImage == true)
431 if (CreateImageViewerArea() == false)
433 AppLog("CreateImageViewerArea() failed");
437 if (CreateImageEditArea() == false)
439 AppLog("CreateImageEditArea() failed");
443 r = Tizen::Io::File::GetAttributes(*__pFilePath, attr);
444 TryReturn(r == E_SUCCESS, false,"[%s] InitPlayer- File::GetAttributes", GetErrorMessage(r));
446 __fileSize = attr.GetFileSize();
451 if (__isMediaVideo == true || __isMediaAudio == true)
453 __pPlayer = new (std::nothrow) Player();
454 TryReturn(__pPlayer != null, false,"E_OUT_OF_MEMORY - Player creation failed");
456 __pTimer = new (std::nothrow) Timer();
457 TryReturn(__pTimer != null, false,"E_OUT_OF_MEMORY - __pTimer creation failed");
459 r = __pTimer->Construct(*this);
460 TryReturn(r == E_SUCCESS, false,"[%s] __pTimer->Construct", GetErrorMessage(r));
462 if (CreateProgressBar() == false)
464 AppLog("InitPlayer->createProgressBar returned false");
469 if (__isMediaVideo == true)
471 if (CreateOverlayRegion() == false)
473 AppLog("InitPlayer->CreateOverlayRegion returned false");
477 r = __pRegion->GetBackgroundBufferInfo(bufferInfo);
478 TryReturn(r == E_SUCCESS, false,"[%s] __pRegion->GetBackgroundBufferInfo", GetErrorMessage(r));
480 r = __pPlayer->Construct(*this, &bufferInfo);
481 TryReturn(r == E_SUCCESS, false,"[%s] __pPlayer->Construct", GetErrorMessage(r));
484 else if (__isMediaAudio == true)
486 if (CreateAudioFileLogo() == false)
488 AppLog("InitPlayer->CreateAudioFileLogo returned false");
491 r = __pPlayer->Construct(*this, null);
492 TryReturn(r == E_SUCCESS, false,"[%s] __pPlayer->Construct", GetErrorMessage(r));
498 MediaPlayerForm::CreateBitmapFromByteBufferN(ByteBuffer* pBuffer, int width, int height)
500 Dimension dim(width, height);
501 Bitmap* pBmp = new (std::nothrow) Bitmap;
502 TryReturn(pBmp != null, pBmp,"E_OUT_OF_MEMORY Bitmap creation failed");
503 result r = pBmp->Construct(*pBuffer, dim, Tizen::Graphics::BITMAP_PIXEL_FORMAT_ARGB8888);
504 TryReturn(r == E_SUCCESS, pBmp,"[%s] Bitmap->Construct", GetErrorMessage(r));
509 MediaPlayerForm::GetBitMapFromImageN(void) const
511 result r = E_SUCCESS;
512 Bitmap* pNormalBitmap = null;
515 r = image.Construct();
516 TryReturn(r == E_SUCCESS, null,"[%s] image.Construct()", GetErrorMessage(r));
518 pNormalBitmap = image.DecodeN(*__pFilePath, BITMAP_PIXEL_FORMAT_ARGB8888);
519 TryReturn(pNormalBitmap != null, null,"[%s] image.DecodeN failed pBitmap is %x", GetErrorMessage(r), pNormalBitmap);
521 AppLog("Width is %d , height is %d", __imageWidth, __imageHeight);
523 return pNormalBitmap;
527 MediaPlayerForm::RotateImageN(void)
529 result r = E_SUCCESS;
530 Bitmap* pNormalBitmap = null;
531 Bitmap* pRotatedBitmap = null;
532 ByteBuffer* pNormalByteBuff = null;
533 ByteBuffer* pRotatedByteBuff = null;
534 ByteBuffer* pResizeByteBuffer = null;
538 ImageFormat imgFormat = IMG_FORMAT_NONE;
541 r = image.Construct();
542 TryReturn(r == E_SUCCESS, null,"[%s] image.Construct()", GetErrorMessage(r));
544 r = ImageBuffer::GetImageInfo(*__pFilePath, imgFormat, width, height);
545 TryReturn(r == E_SUCCESS, null,"[%s] ImageBuffer::GetImageInfo", GetErrorMessage(r));
547 __imageWidth = width;
548 __imageHeight = height;
550 AppLog("Width is %d , height is %d", __imageWidth, __imageHeight);
552 //If orientation is in potrait mode or reverse Landscape mode then only rotate the image
553 if (__orientationInfo == CAMERA_EXIF_ORIENTATION_RIGHT_TOP ||
554 __orientationInfo == CAMERA_EXIF_ORIENTATION_LEFT_BOTTOM ||
555 __orientationInfo == CAMERA_EXIF_ORIENTATION_TOP_LEFT)
557 pNormalByteBuff = image.DecodeToBufferN(*__pFilePath, BITMAP_PIXEL_FORMAT_ARGB8888, __imageWidth, __imageHeight);
558 TryCatch(pNormalByteBuff != null, ,"pNormalByteBuff is null, no memory");
560 resizeDim.width = RESIZE_WIDTH;
561 resizeDim.height = RESIZE_HEIGHT;
563 pResizeByteBuffer = new (std::nothrow) ByteBuffer;
564 TryCatch(pResizeByteBuffer != null, ,"pResizeByteBuffer is null, no memory");
566 r = pResizeByteBuffer->Construct(resizeDim.width * resizeDim.height * 4);
567 TryCatch(r == E_SUCCESS, ,"[%s] pResizeByteBuffer Construct", GetErrorMessage(r));
569 r = ImageUtil::Resize(*pNormalByteBuff, *pResizeByteBuffer, Dimension(__imageWidth, __imageHeight), resizeDim, MEDIA_PIXEL_FORMAT_RGBA8888);
570 TryCatch(r == E_SUCCESS, ,"[%s] ImageUtil::Resize", GetErrorMessage(r));
572 SAFE_DELETE(pNormalByteBuff);
574 pRotatedByteBuff = new (std::nothrow) ByteBuffer;
575 TryCatch(pRotatedByteBuff != null, ,"pRotatedByteBuff is null, no memory");
577 r = pRotatedByteBuff->Construct(resizeDim.width * resizeDim.height * 4);
578 TryCatch(r == E_SUCCESS, ,"[%s] pRotatedByteBuff Construct", GetErrorMessage(r));
581 if (__orientationInfo == CAMERA_EXIF_ORIENTATION_RIGHT_TOP)
583 AppLog("Rotated by 90");
584 r = ImageUtil::Rotate(*pResizeByteBuffer, *pRotatedByteBuff, resizeDim, IMAGE_ROTATION_90, MEDIA_PIXEL_FORMAT_RGBA8888);
585 TryCatch(r == E_SUCCESS, ,"[%s] ImageUtil::Rotate 90", GetErrorMessage(r));
587 pRotatedBitmap = CreateBitmapFromByteBufferN(pRotatedByteBuff, resizeDim.height, resizeDim.width);
590 //Reverse potrait mode
591 else if (__orientationInfo == CAMERA_EXIF_ORIENTATION_LEFT_BOTTOM)
593 AppLog("Rotated by 270");
594 r = ImageUtil::Rotate(*pResizeByteBuffer, *pRotatedByteBuff, resizeDim, IMAGE_ROTATION_270, MEDIA_PIXEL_FORMAT_RGBA8888);
595 TryCatch(r == E_SUCCESS, ,"[%s] ImageUtil::Rotate 270", GetErrorMessage(r));
597 pRotatedBitmap = CreateBitmapFromByteBufferN(pRotatedByteBuff, resizeDim.height, resizeDim.width);
601 else if (__orientationInfo == CAMERA_EXIF_ORIENTATION_TOP_LEFT)
603 AppLog("Rotated by 180");
604 r = ImageUtil::Rotate(*pResizeByteBuffer, *pRotatedByteBuff, resizeDim, IMAGE_ROTATION_180, MEDIA_PIXEL_FORMAT_RGBA8888);
605 TryCatch(r == E_SUCCESS, ,"[%s] ImageUtil::Rotate 180", GetErrorMessage(r));
607 pRotatedBitmap = CreateBitmapFromByteBufferN(pRotatedByteBuff, resizeDim.width, resizeDim.height);
611 //delete local create bytebuffers
612 if (pRotatedByteBuff != null)
614 delete pRotatedByteBuff;
615 pRotatedByteBuff = null;
617 if (pNormalBitmap != null)
619 delete pNormalBitmap;
620 pNormalBitmap = null;
622 if (pResizeByteBuffer != null)
624 delete pResizeByteBuffer;
625 pResizeByteBuffer = null;
628 return pRotatedBitmap;
632 pNormalBitmap = image.DecodeN(*__pFilePath, BITMAP_PIXEL_FORMAT_ARGB8888);
633 TryCatch(pNormalBitmap != null, ,"pNormalBitmap = image.DecodeN, no memory");
635 __imageWidth = pNormalBitmap->GetWidth();
636 __imageHeight = pNormalBitmap->GetHeight();
638 return pNormalBitmap;
643 if (pRotatedByteBuff != null)
645 delete pRotatedByteBuff;
646 pRotatedByteBuff = null;
648 if (pNormalBitmap != null)
650 delete pNormalBitmap;
651 pNormalBitmap = null;
653 if (pResizeByteBuffer != null)
655 delete pResizeByteBuffer;
656 pResizeByteBuffer = null;
663 MediaPlayerForm::DisplayImage(void)
665 result r = E_SUCCESS;
666 Rectangle imageLableRect;
667 Bitmap* pBitmap = RotateImageN();
668 TryReturn(pBitmap != null, false,"RotateImageN failed");
670 if (__pImageLable != null)
672 imageLableRect = __pImageLable->GetBounds();
673 //scale bitmap to image lable width and height
674 r = pBitmap->Scale(Dimension(imageLableRect.width, imageLableRect.height));
677 AppLog("bitmap.Scale failed %s", GetErrorMessage(r));
687 __pImageLable->SetBackgroundBitmap(*pBitmap);
688 AddControl(__pImageLable);
689 __pImageLable->RequestRedraw();
692 //display file information
693 if (__pDisplayText != null)
695 __pDisplayText->Clear();
696 __pDisplayText->Log(L"\nFileName: ");
697 GetOnlyFileName(__pFilePath);
698 __pDisplayText->Log(*__pFilePath);
699 __pDisplayText->Log("\nFileSize: %dKB\nDimension: %dx%d\n", __fileSize / KILO_BYTE, __imageWidth, __imageHeight);
712 MediaPlayerForm::GetOnlyFileName(String* pFilePath) const
716 // Create a StringTokenizer instance
717 StringTokenizer strTok(*pFilePath, delim);
720 //Iterate to the later token and extract the string
721 while (strTok.HasMoreTokens())
723 strTok.GetNextToken(token);
727 pFilePath->Append(token);
731 MediaPlayerForm::CreateImageEditArea(void)
733 result r = E_SUCCESS;
735 __pDisplayText = new (std::nothrow) LogControl;
736 if (__pDisplayText == null)
738 AppLog("__pDisplayText new (std::nothrow) failed, out of memory");
741 TryReturn(__pDisplayText != null, false,"LogControl, no memory");
743 r = __pDisplayText->Construct(Rectangle(0, GetClientAreaBounds().height - SPACE_FOR_EDIT_AREA, GetClientAreaBounds().width, SPACE_FOR_EDIT_AREA));
744 TryReturn(r == E_SUCCESS, false,"[%s] __pDisplayText->Construct", GetErrorMessage(r));
746 r = __pDisplayText->SetEnabled(false);
747 TryReturn(r == E_SUCCESS, false,"[%s] __pDisplayText->SetEnabled", GetErrorMessage(r));
749 AddControl(__pDisplayText);
755 MediaPlayerForm::CreateImageViewerArea(void)
757 result r = E_SUCCESS;
761 double aspectRatio = 0;
763 Bitmap* pBitmap = null;
764 Rectangle clientRect = GetClientAreaBounds();
767 int requireHeight = 0;
768 int requireImgWidth = 0;
770 r = image.Construct();
771 TryReturn(r == E_SUCCESS, false,"[%s] image.Construct()", GetErrorMessage(r));
773 pBitmap = image.DecodeN(*__pFilePath, BITMAP_PIXEL_FORMAT_ARGB8888);
774 TryReturn(pBitmap != null, false,"[%s] image.DecodeN failed pBitmap is %x", GetErrorMessage(r), pBitmap);
776 width = pBitmap->GetWidth();
777 height = pBitmap->GetHeight();
779 requireImgWidth = ((double) (clientRect.width * REQUIRE_WIDTH_PERCENTAGE_LANDSCAPE)) / 100;
780 aspectRatio = ((double) width / (double) height);
782 AppLog("Actual image Aspect ratio %f", aspectRatio);
784 //Calculate the required height and width of the captured image to be displayer w.r.t aspect ratio
785 requireHeight = ((double) requireImgWidth / aspectRatio);
787 //If requiredHeight is more than client Area, round it off
788 if (requireHeight > (clientRect.height - SPACE_FOR_EDIT_AREA))
790 requireHeight = (clientRect.height - SPACE_FOR_EDIT_AREA);
793 AppLog("New image label width is %d actual %d", requireImgWidth, clientRect.width);
794 AppLog("New image label height is %d actual %d", requireHeight, clientRect.height - SPACE_FOR_EDIT_AREA);
797 marginX = (clientRect.width - requireImgWidth) / 2;
798 marginY = (clientRect.height - SPACE_FOR_EDIT_AREA - requireHeight) / 2;
809 __pImageLable = new (std::nothrow) Label;
810 TryReturn(__pImageLable != null, false,"__pImageLable creation failed, no memory");
812 r = __pImageLable->Construct(Rectangle(0, 0, requireImgWidth, requireHeight), L"");
813 TryCatch(r == E_SUCCESS, ,"[%s] __pImageLable->Construct()", GetErrorMessage(r));
815 AppLog("margin is %d %d", marginX, marginY);
817 __pImageLable->SetPosition(marginX, marginY);
818 __pImageLable->SetBackgroundColor(Color(0xFF, 0xFF, 0xFF));
840 MediaPlayerForm::CreateAudioFileLogo(void)
842 result r = E_SUCCESS;
843 Rectangle imageLabelRect;
845 Bitmap* pBitmap = null;
847 r = image.Construct();
848 TryCatch(r == E_SUCCESS, ,"[%s] image.Construct()", GetErrorMessage(r));
850 pBitmap = image.DecodeN(App::GetInstance()->GetAppRootPath() + L"/res/audio-icon.png", BITMAP_PIXEL_FORMAT_ARGB8888);
851 TryCatch(pBitmap != null, ,"image.DecodeN failed pBitmap is %x", pBitmap);
853 __pImageLable = new (std::nothrow) Label;
854 TryCatch(__pImageLable != null, ,"_pImageLable new failed, out of memory");
856 imageLabelRect = GetClientAreaBounds();
857 imageLabelRect.height -= SPACE_FOR_PROGRESS_BAR;
858 imageLabelRect.x = imageLabelRect.width / 5;
859 imageLabelRect.y = imageLabelRect.height / 5;
860 imageLabelRect.width = imageLabelRect.width * 3 / 5;
861 imageLabelRect.height = imageLabelRect.height * 3 / 5;
864 r = __pImageLable->Construct(imageLabelRect, L"");
865 TryCatch(r == E_SUCCESS, ,"[%s] __pImageLable->Construct()", GetErrorMessage(r));
867 __pImageLable->SetBackgroundColor(Color(0xFF, 0xFF, 0xFF));
869 imageLabelRect = __pImageLable->GetBounds();
871 r = pBitmap->Scale(Dimension(imageLabelRect.width, imageLabelRect.height));
872 TryCatch(r == E_SUCCESS, ,"[%s] pBitmap->Scale", GetErrorMessage(r));
874 __pImageLable->SetBackgroundBitmap(*pBitmap);
875 AddControl(__pImageLable);
876 __pImageLable->RequestRedraw();
895 MediaPlayerForm::CreateOverlayRegion(void)
897 int overlayRegionHeight = (GetClientAreaBounds().width * ASPECT_RATIO_HEIGHT / ASPECT_RATIO_WIDTH);
899 if (overlayRegionHeight > GetClientAreaBounds().height - SPACE_FOR_PROGRESS_BAR)
901 overlayRegionHeight = GetClientAreaBounds().height - SPACE_FOR_PROGRESS_BAR;
904 Tizen::Graphics::Rectangle rect = Rectangle(0, GetClientAreaBounds().y, GetClientAreaBounds().width, overlayRegionHeight);
906 bool isModified = false;
907 bool isValidRect = OverlayRegion::EvaluateBounds(OVERLAY_REGION_EVALUATION_OPTION_LESS_THAN, rect, isModified);
908 TryReturn(isValidRect != false, false,"EvaluateBounds() has failed: [%s]", GetErrorMessage(GetLastResult()));
910 __pRegion = GetOverlayRegionN(rect, OVERLAY_REGION_TYPE_NORMAL);
911 TryReturn(__pRegion != null, false,"pRegion is null : %d %d %d %d", rect.x, rect.y, rect.width, rect.height);
917 MediaPlayerForm::OnTerminating(void)
919 AppLog("OnTerminating++");
920 if (__pPlayer != null)
922 if (__pPlayer->GetState() == PLAYER_STATE_STOPPED)
930 AppLog("OnTerminating--");
936 MediaPlayerForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
938 Tizen::System::BatteryLevel batteryLevel;
939 Tizen::System::Battery::GetCurrentLevel(batteryLevel);
941 if (batteryLevel == BATTERY_CRITICAL || batteryLevel == BATTERY_EMPTY || batteryLevel == BATTERY_LOW)
943 bool isCharging = false;
944 Tizen::System::RuntimeInfo::GetValue(String(L"IsCharging"), isCharging);
945 if (isCharging == false && __pPlayer!= null)
948 showPopup(L"Low Battery: Please charge to use");
955 case IDC_BUTTON_PLAY:
961 case IDC_BUTTON_PAUSE:
967 case IDC_BUTTON_STOP:
981 MediaPlayerForm::PlayerOpen(void)
983 result r = E_SUCCESS;
984 TryReturn(__pPlayer != null, r, "Player handle is null");
985 TryReturn(__pFilePath != null, r, "__pFilePath handle is null");
987 r = __pPlayer->OpenFile(*__pFilePath); // Sync open, at once
988 TryReturn(r == E_SUCCESS, showPopupResult(L"Opening file failed file path"),"[%s] pPlayer->OpenFile has failed", GetErrorMessage(r));
994 MediaPlayerForm::PlayerPause(void)
996 if (__pPlayer != null)
998 result r = E_SUCCESS;
999 // Pause the playing.
1000 if (__pPlayer->GetState() == PLAYER_STATE_PLAYING)
1002 AppLog("Player paused");
1003 r = __pPlayer->Pause();
1006 AppLog("(Player::PlayerPause) Pause has failed: %s\n", GetErrorMessage(r));
1007 showPopup(L"Player Pause failed");
1008 SendUserEvent(PLAYER_ERROR_OCCURED, null);
1012 EnableFooterItem(IDC_BUTTON_STOP, true);
1013 SetFooterItem(1, L"Play", IDC_BUTTON_PLAY);
1014 EnableFooterItem(IDC_BUTTON_PLAY, true);
1022 MediaPlayerForm::PlayerSeek(long int position)
1024 if (__pPlayer != null)
1026 result r = E_SUCCESS;
1028 //Pause the playback if its in PLAYING state
1029 if (__pPlayer->GetState() == PLAYER_STATE_PLAYING)
1031 AppLog("Player paused");
1032 r = __pPlayer->Pause();
1035 AppLog("__pPlayer->Pause() failed");
1036 showPopup(L"Player Pause failed");
1037 SendUserEvent(PLAYER_ERROR_OCCURED, null);
1041 __isPausedBeforeSeek = true;
1044 //Perform Seek and wait for OnPlayerSeekCompleted callback before resuming Play()
1045 AppLog("Player Seeked position - %d duration - %d", position, __pPlayer->GetDuration());
1046 r = __pPlayer->SeekTo(position);
1049 AppLog("__pPlayer->SeekTo() failed");
1050 showPopup(L"Player Seek failed");
1051 SendUserEvent(PLAYER_ERROR_OCCURED, null);
1058 MediaPlayerForm::OnPlayerAudioFocusChanged(void)
1060 AppLog("OnPlayerAudioFocusChanged");
1061 if (__pPlayer->GetState() == PLAYER_STATE_PAUSED)
1063 ShowMessagePopup(String(L"Info"), String(L"Press Play to resume Playback"), ID_ERROR_POPUP);
1064 EnableFooterItem(IDC_BUTTON_STOP, true);
1065 SetFooterItem(1, L"Play", IDC_BUTTON_PLAY);
1066 EnableFooterItem(IDC_BUTTON_PLAY, true);
1071 MediaPlayerForm::OnPlayerSeekCompleted(result r)
1073 AppLog("OnPlayerSeekCompleted called, result is %s", GetErrorMessage(r));
1075 if (__isPausedBeforeSeek == true)
1077 result r = E_SUCCESS;
1079 //Also check if player state is paused
1080 if (__pPlayer->GetState() == PLAYER_STATE_PAUSED)
1082 //After seek is completed, start the playback
1083 AppLog("Player Play");
1084 r = __pPlayer->Play();
1087 AppLog("__pPlayer->Play() failed");
1088 showPopup(L"Player Play failed");
1089 SendUserEvent(PLAYER_ERROR_OCCURED, null);
1094 //Reinitialise flag to false
1095 __isPausedBeforeSeek = false;
1100 MediaPlayerForm::PlayerPlay(void)
1102 if (__pPlayer != null)
1104 result r = E_SUCCESS;
1105 PlayerState nowState = __pPlayer->GetState();
1106 AppLog("PlayerPlay, player state is %d", nowState);
1107 // Re-Play the playing.
1108 if (nowState == PLAYER_STATE_ENDOFCLIP || nowState == PLAYER_STATE_OPENED ||
1109 nowState == PLAYER_STATE_INITIALIZED || nowState == PLAYER_STATE_STOPPED ||
1110 nowState == PLAYER_STATE_PAUSED)
1112 AppLog("Player Play");
1113 r = __pPlayer->Play();
1116 AppLog("(PlayerPlay Play has failed: %s\n", GetErrorMessage(r));
1117 if (nowState == PLAYER_STATE_ERROR)
1119 showPopup(L"Player Play failed");
1120 AppLog("nowState == PLAYER_STATE_ERROR");
1121 SendUserEvent(PLAYER_ERROR_OCCURED, null);
1128 //situation is call, player will notice E_DEVICE_BUSY error
1129 showPopup(L"Cannot play during call - player is paused");
1131 case E_INVALID_STATE:
1132 showPopup(L"player state is not valid");
1141 EnableFooterItem(IDC_BUTTON_STOP, true);
1142 SetFooterItem(1, L"Pause", IDC_BUTTON_PAUSE);
1143 EnableFooterItem(IDC_BUTTON_PAUSE, true);
1145 r = __pTimer->Start(TIMER_INTERVAL);
1146 AppLog("Timer return = %s", GetErrorMessage(r));
1152 MediaPlayerForm::PlayerStop(void)
1154 if (__pPlayer != null)
1156 result r = E_SUCCESS;
1157 PlayerState nowState = __pPlayer->GetState();
1160 if (nowState == PLAYER_STATE_PLAYING || nowState == PLAYER_STATE_PAUSED)
1164 AppLog("Player Stop");
1165 r = __pPlayer->Stop();
1168 AppLog("Stop has failed: %s\n", GetErrorMessage(r));
1169 showPopup(L"Player Stop failed");
1170 SendUserEvent(PLAYER_ERROR_OCCURED, null);
1174 //need not enable stop button, as its stopped
1175 EnableFooterItem(IDC_BUTTON_STOP, false);
1176 SetFooterItem(1, L"Play", IDC_BUTTON_PLAY);
1177 EnableFooterItem(IDC_BUTTON_PLAY, true);
1180 UpdateTimeElapsed();
1182 __pProgress->SetValue(0);
1183 __pProgress->RequestRedraw();
1189 MediaPlayerForm::OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs)
1193 case PLAYER_ERROR_OCCURED:
1195 AppLog("OnUserEventReceivedN PLAYER_ERROR_OCCURED");
1196 HandlePlayerError();
1204 MediaPlayerForm::ResetTimeElapsed(void)
1206 if ( (__pElapsedTimeLable != null) && (__pEndTimeLable != null))
1208 __pElapsedTimeLable->SetText(__startTime);
1209 __pElapsedTimeLable->RequestRedraw();
1214 MediaPlayerForm::UpdateTimeElapsed(void)
1220 long clipDuration = 0;
1222 if (__pPlayer != null)
1224 position = __pPlayer->GetPosition();
1225 clipDuration = __pPlayer->GetDuration();
1228 sec = position / MILI_SEC;
1230 sec = sec - min * SEC;
1232 min = min - hours * SEC;
1234 String elapsedTime = L"";
1235 if (hours < MAX_TIME)
1237 elapsedTime.Append(L"0");
1239 elapsedTime.Append(hours);
1240 elapsedTime.Append(L"");
1241 elapsedTime.Append(L":");
1244 elapsedTime.Append(L"0");
1246 elapsedTime.Append(min);
1247 elapsedTime.Append(L"");
1248 elapsedTime.Append(L":");
1251 elapsedTime.Append(L"0");
1253 elapsedTime.Append(sec);
1255 sec = clipDuration / MILI_SEC;
1257 sec = sec - min * SEC;
1259 min = min - hours * SEC;
1262 String endTime = L"";
1264 if (hours < MAX_TIME)
1266 endTime.Append(L"0");
1268 endTime.Append(hours);
1269 endTime.Append(L"");
1270 endTime.Append(L":");
1273 endTime.Append(L"0");
1275 endTime.Append(min);
1276 endTime.Append(L"");
1277 endTime.Append(L":");
1280 endTime.Append(L"0");
1282 endTime.Append(sec);
1285 if ( (__pElapsedTimeLable != null) && (__pEndTimeLable != null) )
1287 __pElapsedTimeLable->SetText(elapsedTime);
1288 __pElapsedTimeLable->RequestRedraw();
1289 __pEndTimeLable->SetText(endTime);
1290 __pEndTimeLable->RequestRedraw();
1295 MediaPlayerForm::Start(void)
1297 Tizen::System::PowerManager::KeepScreenOnState(true, false);
1299 if (__isMediaImage == true)
1301 if (DisplayImage() == false)
1303 showPopup(L"Displaying Image failed");
1309 if (PlayerOpen() != E_SUCCESS)
1314 AppLog("Player duration is %d",__pPlayer->GetDuration());
1316 UpdateTimeElapsed();
1325 MediaPlayerForm::OnFrameTerminating(const Tizen::Ui::Controls::Frame& source)
1327 // When playing video clip, it's recommanded to call Player::Stop() on OnFrameTerminating().
1329 if (__pPlayer != null)
1331 PlayerState nowState = __pPlayer->GetState();
1332 if (nowState == PLAYER_STATE_ENDOFCLIP || nowState == PLAYER_STATE_PLAYING || nowState == PLAYER_STATE_OPENED || nowState == PLAYER_STATE_PAUSED)
1341 MediaPlayerForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
1343 AppLog("OnFormBackRequested");
1344 if (IsProcessedEvent())
1346 AppLog("Processed Event");
1347 ResetProcessedEvent();
1351 AppLog("Deactivate start");
1356 MediaPlayerForm::OnPlayerOpened(result r)
1361 MediaPlayerForm::OnPlayerEndOfClip(void)
1363 AppLog("OnPlayerEndOfClip reached");
1364 EnableFooterItem(IDC_BUTTON_STOP, false);
1365 SetFooterItem(1, L"Play", IDC_BUTTON_PLAY);
1366 EnableFooterItem(IDC_BUTTON_PLAY, true);
1368 __pProgress->SetValue(100);
1372 UpdateTimeElapsed();
1374 __pProgress->SetValue(0);
1375 __pProgress->RequestRedraw();
1380 MediaPlayerForm::HandlePlayerError(void)
1382 result res = E_SUCCESS;
1383 Tizen::Graphics::BufferInfo bufferInfo;
1384 AppLog("HandlePlayerError called");
1386 //Reset flags and elapsed time
1387 __isPausedBeforeSeek = false;
1389 EnableFooterItem(IDC_BUTTON_STOP, false);
1390 SetFooterItem(1, L"Play", IDC_BUTTON_PLAY);
1391 EnableFooterItem(IDC_BUTTON_PLAY, true);
1396 __pProgress->SetValue(0);
1397 __pProgress->RequestRedraw();
1399 //When error occurs , delete the player and construct new (std::nothrow)
1400 if (__pPlayer != null)
1402 AppLog("player state is %d", __pPlayer->GetState());
1407 if (__pRegion != null)
1409 res = __pRegion->GetBackgroundBufferInfo(bufferInfo);
1410 TryReturn(res == E_SUCCESS, showPopup(L"Region's GetBackgroundBufferInfo failed"),"GetBackgroundBufferInfo has failed");
1412 __pPlayer = new (std::nothrow) Player();
1413 if (__pPlayer == null)
1415 AppLog("pPlayer = new (std::nothrow) Player() has failed\n");
1416 showPopup(L"Player new (std::nothrow) failed, out of memory");
1419 TryReturn(__pPlayer != null, showPopup(L"Player new failed, out of memory"),"pPlayer = new Player() has failed");
1421 res = __pPlayer->Construct(*this, &bufferInfo);
1422 TryReturn(res == E_SUCCESS, showPopup(L"Player Construct failed"),"pPlayer->Construct has failed");
1425 if (PlayerOpen() != E_SUCCESS)
1432 MediaPlayerForm::OnPlayerBuffering(int percent)
1437 MediaPlayerForm::OnPlayerErrorOccurred(PlayerErrorReason r)
1439 AppLog("OnPlayerErrorOccurred called");
1440 HandlePlayerError();
1444 MediaPlayerForm::OnPlayerInterrupted(void)
1446 __isPausedInInterrupt = true;
1450 MediaPlayerForm::OnPlayerReleased(void)
1452 TryReturnVoid(__pPlayer != null, "Player handle is null");
1454 if ((__pPlayer->GetState() == PLAYER_STATE_PAUSED) && (__isPausedInInterrupt == true))
1458 __isPausedInInterrupt = false;
1462 MediaPlayerForm::OnForeground(void)
1464 AppLog("OnForeground called");
1465 TryReturnVoid(__pPlayer != null, "Player handle is null");
1467 PlayerState nowState = __pPlayer->GetState();
1468 if ((nowState == PLAYER_STATE_PAUSED || nowState == PLAYER_STATE_OPENED) && (__isPausedInBackGround == true))
1472 __isPausedInBackGround = false;
1477 MediaPlayerForm::FreeResource(void)
1479 TryReturnVoid(__pPlayer != null, "Player handle is null");
1480 TryReturnVoid(__pRegion != null, "overlayRegion handle is null");
1481 TryReturnVoid(__pTimer != null, "Timer handle is null");
1486 SAFE_DELETE(__pPlayer);
1487 SAFE_DELETE(__pRegion);
1488 SAFE_DELETE(__pTimer);
1493 MediaPlayerForm::OnBatteryLevelChanged(Tizen::System::BatteryLevel batteryLevel)
1495 if (batteryLevel == BATTERY_CRITICAL || batteryLevel == BATTERY_EMPTY || batteryLevel == BATTERY_LOW)
1497 bool isCharging = false;
1498 Tizen::System::RuntimeInfo::GetValue(String(L"IsCharging"), isCharging);
1499 if (isCharging == false && __pPlayer!= null)
1507 MediaPlayerForm::OnDeviceStateChanged(Tizen::System::DeviceType deviceType, const Tizen::Base::String &state)
1509 if(deviceType == Charger)
1512 showPopup(L"Low Battery: Please charge to use");
1517 MediaPlayerForm::OnBackground(void)
1519 AppLog("OnBackground called");
1520 TryReturnVoid(__pPlayer != null, "Player handle is null");
1521 if (__pPlayer->GetState() == PLAYER_STATE_PLAYING)
1524 __isPausedInBackGround = true;
1529 MediaPlayerForm::HandleLowBattery(void)
1531 if (__pPlayer != null)
1533 //dont close the player
1540 MediaPlayerForm::SetHeaderText(const Tizen::Base::String& title)
1542 Header* pHeader = GetHeader();
1543 TryReturn(pHeader != null, E_FAILURE,"[%s] GetHeader failed", GetErrorMessage(GetLastResult()));
1545 pHeader->SetTitleText(title);
1546 pHeader->RequestRedraw();
1554 MediaPlayerForm::SetFooterStyle(Tizen::Ui::Controls::FooterStyle style, bool showBack, Tizen::Ui::IActionEventListener* pListener)
1556 result r = E_SUCCESS;
1557 Footer* pFooter = GetFooter();
1558 TryReturn(pFooter != null, E_FAILURE,"[%s] GetFooter failed", GetErrorMessage(GetLastResult()));
1560 pFooter->SetStyle(style);
1562 if (pListener != null)
1564 pFooter->AddActionEventListener(*pListener);
1572 MediaPlayerForm::AddFooterItem(const Tizen::Base::String& text, int actionId)
1574 result r = E_SUCCESS;
1576 Footer* pFooter = GetFooter();
1579 TryReturn(pFooter != null, E_FAILURE,"[%s] GetFooter failed", GetErrorMessage(GetLastResult()));
1581 item.Construct(actionId);
1584 index = pFooter->GetItemCount();
1585 TryReturn(index >= 0, E_FAILURE, "pFooter->GetItemCount() is less than 0 %d", index);
1587 __footerActionId[index] = actionId;
1589 pFooter->AddItem(item);
1595 MediaPlayerForm::SetFooterItem(int index, const Tizen::Base::String& text, int actionId)
1597 result r = E_SUCCESS;
1599 Footer* pFooter = GetFooter();
1601 TryReturn(pFooter != null, E_FAILURE,"[%s] GetFooter failed", GetErrorMessage(GetLastResult()));
1603 item.Construct(actionId);
1606 __footerActionId[index] = actionId;
1608 pFooter->SetItemAt(index, item);
1614 MediaPlayerForm::EnableFooterItem(int actionId, bool enable)
1616 result r = E_SUCCESS;
1617 Footer* pFooter = GetFooter();
1618 unsigned int count = (sizeof(__footerActionId) / sizeof(__footerActionId[0]));
1620 TryReturn(pFooter != null, E_FAILURE,"[%s] GetFooter failed", GetErrorMessage(GetLastResult()));
1622 for (unsigned int i = 0; i < count; i++)
1624 if (__footerActionId[i] == actionId)
1626 pFooter->SetItemEnabled(i, enable);
1627 pFooter->RequestRedraw();
1636 MediaPlayerForm::showPopup(String message)
1638 AppLog("dispalying message");
1639 MessageBox messageBox;
1640 int modalResult = 0;
1641 messageBox.Construct(L"Info", message, MSGBOX_STYLE_OK, MAX_ERROR_MSG_LENGTH);
1642 messageBox.ShowAndWait(modalResult);
1646 MediaPlayerForm::showPopupResult(String message)
1648 AppLog("dispalying message");
1649 MessageBox messageBox;
1650 int modalResult = 0;
1651 messageBox.Construct(L"Info", message, MSGBOX_STYLE_OK, MAX_ERROR_MSG_LENGTH);
1652 messageBox.ShowAndWait(modalResult);
1657 MediaPlayerForm::showPopupBool(String message)
1659 AppLog("dispalying message");
1660 MessageBox messageBox;
1661 int modalResult = 0;
1662 messageBox.Construct(L"Info", message, MSGBOX_STYLE_OK, MAX_ERROR_MSG_LENGTH);
1663 messageBox.ShowAndWait(modalResult);