Merge with master
[apps/osp/VideoPlayer.git] / src / VpVideoPlayerPresentationModel.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 /**
18  * @file                VpVideoPlayerPresentationModel.cpp
19  * @brief               This is the implementation file for VideoPlayerPresentationModel class.
20  */
21
22 #include <cstdlib>
23 #include <FIo.h>
24 #include "VpTypes.h"
25 #include "VpVideoPlayerApp.h"
26 #include "VpVideoPlayerPresentationModel.h"
27
28 using namespace Tizen::App;
29 using namespace Tizen::Base;
30 using namespace Tizen::Base::Collection;
31 using namespace Tizen::Base::Runtime;
32 using namespace Tizen::Base::Utility;
33 using namespace Tizen::Content;
34 using namespace Tizen::Graphics;
35 using namespace Tizen::Io;
36 using namespace Tizen::Media;
37 using namespace Tizen::Ui::Controls;
38
39 static const int PROGRESS_TIMER_DURATION = 250;
40 static const int LONGKEY_TIMER_DURATION = 200;
41
42 static const int MILLISECOND = 1000;
43 static const int TEN_SEC = 10;
44 static const int TWO_SEC = 2;
45 static const int START_TIME = 0;
46
47 static const int INIT_CONTENT_INDEX = 0;
48 static const int MULTI_CONTENT_COUNT = 2;
49 static const int MAX_PROGRESS_RANGE = 100;
50
51 VideoPlayerPresentationModel*
52 VideoPlayerPresentationModel::__pVideoPlayerPresentationModel = null;
53
54 VideoPlayerPresentationModel::VideoPlayerPresentationModel(void)
55         : __pPlayer(null)
56         , __pVideoPlayerEventListener(null)
57         , __pOverlayPanel(null)
58         , __pMediaPathArrayList(null)
59         , __pTimer(null)
60         , __pFastForwardTimer(null)
61         , __pFastRewindTimer(null)
62         , __playerSeekCompleted(true)
63         , __playerLastSeekCompleted(false)
64         , __playAfterSeek(true)
65         , __mediaWidth(0)
66         , __mediaHeight(0)
67         , __currentMediaIndex(INIT_CONTENT_INDEX)
68         , __nextMediaIndex(INIT_CONTENT_INDEX)
69         , __previousMediaIndex(INIT_CONTENT_INDEX)
70 {
71 }
72
73 VideoPlayerPresentationModel::~VideoPlayerPresentationModel(void)
74 {
75         DeleteTimer();
76         __pPlayer->Stop();
77         __pPlayer->Close();
78         delete __pPlayer;
79         delete __pMediaPathArrayList;
80 }
81
82 VideoPlayerPresentationModel*
83 VideoPlayerPresentationModel::GetInstance(void)
84 {
85         AppLogDebug("GetInstance");
86
87         if (__pVideoPlayerPresentationModel == null)
88         {
89                 CreateInstance();
90         }
91
92         return __pVideoPlayerPresentationModel;
93 }
94
95 result
96 VideoPlayerPresentationModel::Construct(void)
97 {
98         result r = E_FAILURE;
99         bool fileExist = false;
100         int loopCount = 0;
101         String currentPath;
102         String tempPath;
103         String delim = L";";
104         String* mediaName = null;
105         wchar_t tempChar[2] = L"";
106
107         VideoPlayerApp* pVideoPlayerApp = static_cast<VideoPlayerApp*>(VideoPlayerApp::GetInstance());
108
109         String uriData = pVideoPlayerApp->GetUriData();
110         const String* pPath = null;
111         if (uriData.IsEmpty() == true)
112         {
113                 const IMap* pArguments = pVideoPlayerApp->GetAppControlArguments();
114                 pPath = static_cast<const String*>(pArguments->GetValue(String(APPCONTROL_KEY_PATH)));
115
116                 __pMediaPathArrayList = new (std::nothrow) ArrayList();
117                 r = __pMediaPathArrayList->Construct();
118                 TryCatch(r == E_SUCCESS, , "__pMediaPathArrayList->Construct() failed:%s", GetErrorMessage(r));
119
120                 if (pPath != null)
121                 {
122                         currentPath = *pPath;
123                         loopCount = currentPath.GetLength();
124
125                         for (int i = 0; i < loopCount; ++i)
126                         {
127                                 currentPath.GetCharAt(i, *tempChar);
128
129                                 if (delim != tempChar)
130                                 {
131                                         tempPath += tempChar;
132                                 }
133                                 else
134                                 {
135                                         fileExist = File::IsFileExist(tempPath);
136                                         if (fileExist == true)
137                                         {
138                                                 AppLogDebug("Path : %ls", tempPath.GetPointer());
139                                                 __pMediaPathArrayList->Add((new (std::nothrow) String(tempPath)));
140                                                 tempPath.Clear();
141                                         }
142                                         else
143                                         {
144                                                 tempPath += tempChar;
145                                         }
146                                 }
147                         }
148                         AppLogDebug("Path : %ls", tempPath.GetPointer());
149                         __pMediaPathArrayList->Add((new (std::nothrow) String(tempPath)));
150                 }
151
152                 mediaName = static_cast<String*>(__pMediaPathArrayList->GetAt(INIT_CONTENT_INDEX));
153                 __mediaPathName = *mediaName;
154
155                 if (__pMediaPathArrayList->GetCount() < MULTI_CONTENT_COUNT)
156                 {
157                         __previousMediaIndex = INIT_CONTENT_INDEX;
158                         __currentMediaIndex = INIT_CONTENT_INDEX;
159                         __nextMediaIndex = INIT_CONTENT_INDEX;
160                 }
161                 else
162                 {
163                         __previousMediaIndex = INIT_CONTENT_INDEX;
164                         __currentMediaIndex = INIT_CONTENT_INDEX;
165                         __nextMediaIndex = __currentMediaIndex + 1;
166                 }
167         }
168         else
169         {
170                 __mediaPathName = uriData;
171         }
172
173         InitTimer();
174
175         return r;
176
177 CATCH:
178         delete __pMediaPathArrayList;
179         __pMediaPathArrayList = null;
180         return r;
181 }
182
183 void
184 VideoPlayerPresentationModel::CreateInstance(void)
185 {
186         AppLogDebug("CreateInstance");
187
188         __pVideoPlayerPresentationModel = new (std::nothrow) VideoPlayerPresentationModel();
189         result r = __pVideoPlayerPresentationModel->Construct();
190
191         if (IsFailed(r))
192         {
193                 delete __pVideoPlayerPresentationModel;
194                 __pVideoPlayerPresentationModel = null;
195                 return;
196         }
197 }
198
199 void
200 VideoPlayerPresentationModel::DestroyInstance(void)
201 {
202         delete __pVideoPlayerPresentationModel;
203         __pVideoPlayerPresentationModel = null;
204 }
205
206 Rectangle
207 VideoPlayerPresentationModel::GetVideoInfoN(const String& mediaLocalPath)
208 {
209         Rectangle rect;
210         result r = E_FAILURE;
211
212         const IList* pInfoList = null;
213         const VideoStreamInfo* pInfo = null;
214         MediaStreamInfo* pStreamInfo = null;
215
216         if (__pPlayer->GetState() == PLAYER_STATE_PLAYING)
217         {
218                 pStreamInfo = __pPlayer->GetCurrentMediaStreamInfoN();
219                 if (pStreamInfo == null)
220                 {
221                         TryCatch(pStreamInfo != null, r = E_FAILURE, "__pPlayer->GetCurrentMediaStreamInfoN() is null");
222                 }
223
224                 pInfoList = pStreamInfo->GetVideoStreamInfoList();
225                 if (pInfoList == null)
226                 {
227                         TryCatch(pInfoList != null, r = E_FAILURE, "pStreamInfo->GetVideoStreamInfoList() is null");
228                 }
229
230                 pInfo = (const VideoStreamInfo*)pInfoList->GetAt(0);
231                 if (pInfo == null)
232                 {
233                         TryCatch(pInfo != null, r = E_FAILURE, "pInfoList->GetAt() is null");
234                 }
235
236                 rect.width = pInfo->GetWidth();
237                 rect.height = pInfo->GetHeight();
238
239                 __mediaWidth = pInfo->GetWidth();
240                 __mediaHeight = pInfo->GetHeight();
241
242                 delete pStreamInfo;
243         }
244         else
245         {
246                 rect.width = __mediaWidth;
247                 rect.height = __mediaHeight;
248         }
249
250         return rect;
251
252 CATCH:
253         delete pStreamInfo;
254
255         rect.width = __mediaWidth;
256         rect.height = __mediaHeight;
257
258         return rect;
259 }
260
261 result
262 VideoPlayerPresentationModel::ConstructPlayerInstanceWithBufferInfo(void)
263 {
264         result r = E_FAILURE;
265         BufferInfo bufferInfo;
266
267         r = __pOverlayPanel->GetBackgroundBufferInfo(bufferInfo);
268         TryCatch(r == E_SUCCESS, , "__pOverlayRegion->GetBackgroundBufferInfo() failed:%s", GetErrorMessage(r));
269
270         delete __pPlayer;
271
272         __pPlayer = new (std::nothrow) Player();
273
274         __pPlayer->Construct(*this, &bufferInfo);
275
276         AppLogDebug("__pPlayer [%x]", __pPlayer);
277
278         return r;
279
280 CATCH:
281         return r;
282 }
283
284 result
285 VideoPlayerPresentationModel::SeekTo(long msTime)
286 {
287         result r = E_FAILURE;
288
289         PlayerState playState = GetState();
290
291         switch (playState)
292         {
293         case PLAYER_STATE_PLAYING:
294                 // fall through
295         case PLAYER_STATE_PAUSED:
296                 {
297                         r = __pPlayer->SeekTo(msTime);
298                         TryCatch(r == E_SUCCESS, , "__pPlayer->SeekTo failed:%s", GetErrorMessage(r));
299                 }
300                 break;
301
302         default:
303                 break;
304         }
305
306         return r;
307
308 CATCH:
309         return r;
310 }
311
312 void
313 VideoPlayerPresentationModel::OnPlayerSeekCompleted(result r)
314 {
315         AppLogDebug("OnPlayerSeekCompleted");
316
317         __playerSeekCompleted = true;
318
319         if (__playerLastSeekCompleted == true)
320         {
321                 if (__playAfterSeek == true)
322                 {
323                         result r = __pPlayer->Play();
324                         TryReturnVoid(r == E_SUCCESS, "__pPlayer->Play():%s", GetErrorMessage(r));
325
326                         CallOnPlayStateChanged(__pPlayer->GetState());
327
328                         __pTimer->Start(PROGRESS_TIMER_DURATION);
329                 }
330
331                 __playerLastSeekCompleted = false;
332         }
333 }
334
335 void
336 VideoPlayerPresentationModel::SetPlayerBuffer(const BufferInfo& bufferInfo)
337 {
338         result r = E_FAILURE;
339         r = __pPlayer->SetRenderingBuffer(bufferInfo);
340         TryReturnVoid(r == E_SUCCESS, "__pPlayer->SetRenderingBuffer failed:%s", GetErrorMessage(r));
341 }
342
343 result
344 VideoPlayerPresentationModel::StartPlay(void)
345 {
346         result r = E_FAILURE;
347
348         PlayerState playState = __pPlayer->GetState();
349         AppLogDebug("StartPlay : %d", playState);
350
351         switch (playState)
352         {
353         case PLAYER_STATE_ENDOFCLIP:
354                 // fall through
355         case PLAYER_STATE_INITIALIZED:
356                 // fall through
357         case PLAYER_STATE_CLOSED:
358                 {
359                         String mediaPath;
360                         String mediaName;
361                         String delim = L"/";
362
363                         mediaPath = GetMediaPathName();
364
365                         StringTokenizer strTok(mediaPath, delim);
366
367                         while (strTok.HasMoreTokens())
368                         {
369                                 strTok.GetNextToken(mediaName);
370                         }
371
372                         if (mediaPath.StartsWith(L"http://", 0) == true
373                                 || mediaPath.StartsWith(L"https://", 0) == true
374                                 || mediaPath.StartsWith(L"rtsp://", 0) == true
375                                 || mediaPath.StartsWith(L"rtp://", 0) == true)
376                         {
377                                 Uri mediaUri;
378                                 r = mediaUri.SetUri(mediaPath);
379
380                                 if (r == E_SUCCESS)
381                                 {
382                                         r = __pPlayer->OpenUrl(mediaUri);
383                                 }
384                         }
385                         else
386                         {
387                                 r = __pPlayer->OpenFile(mediaPath);
388                         }
389
390                         if (r != E_SUCCESS)
391                         {
392                                 AppResource* pAppResource = Application::GetInstance()->GetAppResource();
393                                 String messageBoxString;
394                                 pAppResource->GetString(IDS_COM_POP_UNSUPPORTED_FILE_TYPE, messageBoxString);
395                                 MessageBox messageBox;
396                                 messageBox.Construct(L"", messageBoxString, MSGBOX_STYLE_OK, 3000);
397                                 int modalResult = 0;
398                                 messageBox.ShowAndWait(modalResult);
399
400                                 UiApp* pApp = UiApp::GetInstance();
401                                 pApp->Terminate();
402                         }
403                         else
404                         {
405                                 r = __pPlayer->Play();
406                                 TryCatch(r == E_SUCCESS, , "__pPlayer->Play() failed:%s", GetErrorMessage(r));
407
408                                 CallOnPlayContentChanged(mediaName);
409
410                                 r = __pTimer->Start(PROGRESS_TIMER_DURATION);
411                                 TryCatch(r == E_SUCCESS, , "__pTimer->Start() failed:%s", GetErrorMessage(r));
412                         }
413                 }
414                 break;
415
416         case PLAYER_STATE_PAUSED:
417                 {
418                         r = __pPlayer->Play();
419                         TryCatch(r == E_SUCCESS, , "__pPlayer->Play() failed:%s", GetErrorMessage(r));
420
421                         r = __pTimer->Start(PROGRESS_TIMER_DURATION);
422                         TryCatch(r == E_SUCCESS, , "__pTimer->Start() failed:%s", GetErrorMessage(r));
423                 }
424                 break;
425
426         default:
427                 break;
428         }
429
430         CallOnPlayStateChanged(__pPlayer->GetState());
431
432 CATCH:
433         return r;
434 }
435
436 result
437 VideoPlayerPresentationModel::StopPlay(void)
438 {
439         result r = E_FAILURE;
440
441         PlayerState playState = GetState();
442         AppLogDebug("StopPlay : %d", playState);
443
444         switch (playState)
445         {
446         case PLAYER_STATE_PLAYING:
447                 // fall through
448         case PLAYER_STATE_PAUSED:
449                 {
450                         r = __pPlayer->Stop();
451                         TryCatch(r == E_SUCCESS, , "__pPlayer->Stop() failed:%s", GetErrorMessage(r));
452                 }
453                 break;
454
455         default:
456                 break;
457         }
458
459 CATCH:
460         return r;
461 }
462
463 result
464 VideoPlayerPresentationModel::PausePlay(void)
465 {
466         result r = E_FAILURE;
467         PlayerState playState = GetState();
468         AppLogDebug("PausePlay : %d", playState);
469
470         if (playState == PLAYER_STATE_PLAYING)
471         {
472                 r = __pPlayer->Pause();
473                 TryCatch(r == E_SUCCESS, , "__pPlayer->Pause() failed:%s", GetErrorMessage(r));
474         }
475         else
476         {
477                 AppLogDebug("Invalid state of player");
478         }
479
480         CallOnPlayStateChanged(__pPlayer->GetState());
481
482 CATCH:
483         return r;
484 }
485
486 result
487 VideoPlayerPresentationModel::ClosePlay(void)
488 {
489         result r = E_FAILURE;
490         PlayerState playState = GetState();
491
492         switch (playState)
493         {
494         case PLAYER_STATE_OPENED:
495                 // fall through
496         case PLAYER_STATE_STOPPED:
497                 // fall through
498         case PLAYER_STATE_ENDOFCLIP:
499                 {
500                         r = __pPlayer->Close();
501                         TryCatch(r == E_SUCCESS, , "__pPlayer->Close() failed:%s", GetErrorMessage(r));
502                 }
503                 break;
504
505         default:
506                 break;
507         }
508
509 CATCH:
510         return r;
511 }
512
513 PlayerState
514 VideoPlayerPresentationModel::GetState(void) const
515 {
516         return __pPlayer->GetState();
517 }
518
519 long
520 VideoPlayerPresentationModel::GetDuration(void) const
521 {
522         return __pPlayer->GetDuration();
523 }
524
525 long
526 VideoPlayerPresentationModel::GetPosition(void) const
527 {
528         return __pPlayer->GetPosition();
529 }
530
531 void
532 VideoPlayerPresentationModel::SetVideoPlayerEventListener(IVideoPlayerEventListener* pPlayerListener)
533 {
534         __pVideoPlayerEventListener = pPlayerListener;
535 }
536
537 void
538 VideoPlayerPresentationModel::SetOverlayPanel(OverlayPanel* pOverlay)
539 {
540         __pOverlayPanel = pOverlay;
541 }
542
543 void
544 VideoPlayerPresentationModel::SetRenderingBuffer(void)
545 {
546         BufferInfo bufferInfo;
547
548         __pOverlayPanel->GetBackgroundBufferInfo(bufferInfo);
549         __pPlayer->SetRenderingBuffer(bufferInfo);
550 }
551
552 void
553 VideoPlayerPresentationModel::OnPlayerOpened(result r)
554 {
555         AppLogDebug("OnPlayerOpened");
556         __pVideoPlayerEventListener->OnPlayOpened(r);
557 }
558
559 void
560 VideoPlayerPresentationModel::OnPlayerEndOfClip(void)
561 {
562         bool playNextContent = false;
563
564         AppLogDebug("OnPlayerEndOfClip");
565
566         __pTimer->Cancel();
567         __pFastForwardTimer->Cancel();
568         __pFastRewindTimer->Cancel();
569
570         __playerSeekCompleted = true;
571
572         if (__pMediaPathArrayList->GetCount() < MULTI_CONTENT_COUNT)
573         {
574                 __previousMediaIndex = INIT_CONTENT_INDEX;
575                 __currentMediaIndex = INIT_CONTENT_INDEX;
576                 __nextMediaIndex = INIT_CONTENT_INDEX;
577         }
578         else
579         {
580                 if (__currentMediaIndex == __pMediaPathArrayList->GetCount() - 1)
581                 {
582                         __previousMediaIndex = __currentMediaIndex;
583                         __currentMediaIndex = INIT_CONTENT_INDEX;
584                         __nextMediaIndex = __currentMediaIndex + 1;
585                 }
586                 else
587                 {
588                         __previousMediaIndex = __currentMediaIndex;
589                         __currentMediaIndex = __nextMediaIndex;
590                         __nextMediaIndex = __currentMediaIndex + 1;
591                 }
592         }
593
594         if ((__currentMediaIndex > __previousMediaIndex)
595                 && (__currentMediaIndex < __pMediaPathArrayList->GetCount()))
596         {
597                 playNextContent = true;
598         }
599         else
600         {
601                 playNextContent = false;
602         }
603
604         __pVideoPlayerEventListener->OnPlayEndOfClip(playNextContent);
605 }
606
607 void
608 VideoPlayerPresentationModel::OnPlayerBuffering(int percent)
609 {
610         AppLogDebug("OnPlayerBuffering");
611         __pVideoPlayerEventListener->OnPlayBuffering(percent);
612 }
613
614 void
615 VideoPlayerPresentationModel::OnPlayerErrorOccurred(PlayerErrorReason r)
616 {
617         AppLogDebug("OnPlayerErrorOccurred");
618         __pVideoPlayerEventListener->OnPlayErrorOccurred(r);
619 }
620
621 void
622 VideoPlayerPresentationModel::OnPlayerInterrupted(void)
623 {
624         AppLogDebug("OnPlayerInterrupted");
625         __pVideoPlayerEventListener->OnPlayInterrupted();
626
627         CallOnPlayStateChanged(__pPlayer->GetState());
628 }
629
630 void
631 VideoPlayerPresentationModel::OnPlayerAudioFocusChanged(void)
632 {
633         AppLog("OnPlayerAudioFocusChanged");
634
635         CallOnPlayStateChanged(__pPlayer->GetState());
636 }
637
638 void
639 VideoPlayerPresentationModel::OnPlayerReleased(void)
640 {
641         AppLogDebug("OnPlayerReleased");
642         __pVideoPlayerEventListener->OnPlayReleased();
643 }
644
645 void
646 VideoPlayerPresentationModel::CallOnPlayContentChanged(const String& fileName)
647 {
648         AppLogDebug("CallOnPlayContentChanged");
649
650         __pVideoPlayerEventListener->OnPlayContentChanged(fileName);
651 }
652
653 void
654 VideoPlayerPresentationModel::CallOnPlayTimeChanged(int currentProgressPos, String& currentPlayTime)
655 {
656         AppLogDebug("CallOnPlayTimeChanged");
657
658         __pVideoPlayerEventListener->OnPlayTimeChanged(currentProgressPos, currentPlayTime);
659 }
660
661 void
662 VideoPlayerPresentationModel::CallOnPlayStateChanged(PlayerState playState)
663 {
664         AppLogDebug("CallOnPlayStateChanged");
665
666         __pVideoPlayerEventListener->OnPlayStateChanged(playState);
667 }
668
669 void
670 VideoPlayerPresentationModel::InitTimer(void)
671 {
672         AppLogDebug("InitTimer");
673
674         __pTimer = new (std::nothrow) Timer();
675         __pTimer->Construct(*this);
676
677         __pFastForwardTimer = new (std::nothrow) Timer();
678         __pFastForwardTimer->Construct(*this);
679
680         __pFastRewindTimer = new (std::nothrow) Timer();
681         __pFastRewindTimer->Construct(*this);
682 }
683
684 void
685 VideoPlayerPresentationModel::DeleteTimer(void)
686 {
687         AppLogDebug("DeleteTimer");
688
689         __pTimer->Cancel();
690         delete __pTimer;
691         __pTimer = null;
692
693         __pFastForwardTimer->Cancel();
694         delete __pFastForwardTimer;
695         __pFastForwardTimer = null;
696
697         __pFastRewindTimer->Cancel();
698         delete __pFastRewindTimer;
699         __pFastRewindTimer = null;
700 }
701
702 void
703 VideoPlayerPresentationModel::OnTimerExpired(Timer& timer)
704 {
705         result r = E_FAILURE;
706         long msTime = 0;
707         int currentProgressPos = 0;
708         DateTime dateTime;
709         String currentPlayTime;
710
711         if (timer.GetHashCode() == __pTimer->GetHashCode())
712         {
713                 if (__pPlayer->GetState() == PLAYER_STATE_INITIALIZED || __pPlayer->GetState() == PLAYER_STATE_PLAYING)
714                 {
715                         dateTime.AddSeconds(__pPlayer->GetPosition() / MILLISECOND);
716                         currentPlayTime.Format(20, L"%02d:%02d:%02d", dateTime.GetHour(), dateTime.GetMinute(), dateTime.GetSecond());
717
718                         currentProgressPos = static_cast<int>(__pPlayer->GetPosition() / (__pPlayer->GetDuration() / MAX_PROGRESS_RANGE));
719
720                         CallOnPlayTimeChanged(currentProgressPos, currentPlayTime);
721
722                         __pTimer->Start(PROGRESS_TIMER_DURATION);
723                 }
724         }
725         else if (timer.GetHashCode() == __pFastForwardTimer->GetHashCode())
726         {
727                 msTime = __pPlayer->GetPosition();
728
729                 msTime = msTime + (TWO_SEC * MILLISECOND);
730
731                 if (msTime < __pPlayer->GetDuration())
732                 {
733                         if (__playerSeekCompleted == true)
734                         {
735                                 r = SeekTo(msTime);
736                                 TryReturnVoid(r == E_SUCCESS, "VideoPlayerPresentationModel::SeekTo() failed:%s", GetErrorMessage(r));
737
738                                 __playerSeekCompleted = false;
739
740                                 dateTime.AddSeconds(msTime / MILLISECOND);
741                                 currentPlayTime.Format(20, L"%02d:%02d:%02d", dateTime.GetHour(), dateTime.GetMinute(), dateTime.GetSecond());
742
743                                 currentProgressPos = static_cast<int>(__pPlayer->GetPosition() / (__pPlayer->GetDuration() / MAX_PROGRESS_RANGE));
744
745                                 CallOnPlayTimeChanged(currentProgressPos, currentPlayTime);
746                         }
747                 }
748                 else
749                 {
750                         if (__playerSeekCompleted == true)
751                         {
752                                 r = SeekTo(__pPlayer->GetDuration());
753                                 TryReturnVoid(r == E_SUCCESS, "VideoPlayerPresentationModel::SeekTo() failed:%s", GetErrorMessage(r));
754                                 __playerSeekCompleted = false;
755
756                                 dateTime.AddSeconds(__pPlayer->GetDuration() / MILLISECOND);
757                                 currentPlayTime.Format(20, L"%02d:%02d:%02d", dateTime.GetHour(), dateTime.GetMinute(), dateTime.GetSecond());
758
759                                 currentProgressPos = static_cast<int>(__pPlayer->GetPosition() / (__pPlayer->GetDuration() / MAX_PROGRESS_RANGE));
760
761                                 CallOnPlayTimeChanged(currentProgressPos, currentPlayTime);
762                         }
763                 }
764                 __pFastForwardTimer->Start(LONGKEY_TIMER_DURATION);
765         }
766         else if (timer.GetHashCode() == __pFastRewindTimer->GetHashCode())
767         {
768                 msTime = __pPlayer->GetPosition();
769
770                 msTime = msTime - (TWO_SEC * MILLISECOND);
771
772                 if (msTime > START_TIME)
773                 {
774                         if (__playerSeekCompleted == true)
775                         {
776                                 r = SeekTo(msTime);
777                                 TryReturnVoid(r == E_SUCCESS, "VideoPlayerPresentationModel::SeekTo() failed:%s", GetErrorMessage(r));
778
779                                 __playerSeekCompleted = false;
780
781                                 dateTime.AddSeconds(msTime / MILLISECOND);
782                                 currentPlayTime.Format(20, L"%02d:%02d:%02d", dateTime.GetHour(), dateTime.GetMinute(), dateTime.GetSecond());
783
784                                 currentProgressPos = static_cast<int>(__pPlayer->GetPosition() / (__pPlayer->GetDuration() / MAX_PROGRESS_RANGE));
785
786                                 CallOnPlayTimeChanged(currentProgressPos, currentPlayTime);
787                         }
788                 }
789                 else
790                 {
791                         if (__playerSeekCompleted == true)
792                         {
793                                 r = SeekTo(START_TIME);
794                                 TryReturnVoid(r == E_SUCCESS, "VideoPlayerPresentationModel::SeekTo() failed:%s", GetErrorMessage(r));
795
796                                 __playerSeekCompleted = false;
797
798                                 currentPlayTime.Format(20, L"%02d:%02d:%02d", START_TIME, START_TIME, START_TIME);
799
800                                 currentProgressPos = START_TIME;
801
802                                 CallOnPlayTimeChanged(currentProgressPos, currentPlayTime);
803                         }
804                 }
805                 __pFastRewindTimer->Start(LONGKEY_TIMER_DURATION);
806         }
807 }
808
809 void
810 VideoPlayerPresentationModel::Forward(void)
811 {
812         result r = E_FAILURE;
813
814         long msTime = 0;
815         int currentProgressPos = 0;
816         DateTime dateTime;
817         String currentPlayTime;
818
819         AppLogDebug("Forward");
820
821         if (__pPlayer->GetState() != PLAYER_STATE_ENDOFCLIP)
822         {
823                 msTime = __pPlayer->GetPosition();
824
825                 msTime = msTime + (TEN_SEC * MILLISECOND);
826
827                 if (msTime < __pPlayer->GetDuration())
828                 {
829                         if (__playerSeekCompleted == true)
830                         {
831                                 r = SeekTo(msTime);
832                                 TryReturnVoid(r == E_SUCCESS, "SeekTo() failed:%s", GetErrorMessage(r));
833
834                                 dateTime.AddSeconds(msTime / MILLISECOND);
835                                 currentPlayTime.Format(20, L"%02d:%02d:%02d", dateTime.GetHour(), dateTime.GetMinute(), dateTime.GetSecond());
836
837                                 currentProgressPos = static_cast<int>(__pPlayer->GetPosition() / (__pPlayer->GetDuration() / MAX_PROGRESS_RANGE));
838                                 CallOnPlayTimeChanged(currentProgressPos, currentPlayTime);
839
840                                 __playerSeekCompleted = false;
841                         }
842                 }
843                 else
844                 {
845                         if (__playerSeekCompleted == true)
846                         {
847                                 r = SeekTo(__pPlayer->GetDuration());
848                                 TryReturnVoid(r == E_SUCCESS, "SeekTo() failed:%s", GetErrorMessage(r));
849
850                                 dateTime.AddSeconds(__pPlayer->GetDuration() / MILLISECOND);
851                                 currentPlayTime.Format(20, L"%02d:%02d:%02d", dateTime.GetHour(), dateTime.GetMinute(), dateTime.GetSecond());
852
853                                 currentProgressPos = static_cast<int>(__pPlayer->GetPosition() / (__pPlayer->GetDuration() / MAX_PROGRESS_RANGE));
854                                 CallOnPlayTimeChanged(currentProgressPos, currentPlayTime);
855
856                                 __playerSeekCompleted = false;
857                         }
858                 }
859         }
860 }
861
862 void
863 VideoPlayerPresentationModel::Rewind(void)
864 {
865         result r = E_FAILURE;
866
867         long msTime = 0;
868         int currentProgressPos = 0;
869         DateTime dateTime;
870         String currentPlayTime;
871
872         AppLogDebug("Rewind");
873
874         if (__pPlayer->GetState() != PLAYER_STATE_ENDOFCLIP)
875         {
876                 msTime = __pPlayer->GetPosition();
877
878                 msTime = msTime - (TEN_SEC * MILLISECOND);
879
880                 if (msTime < START_TIME)
881                 {
882                         if (__playerSeekCompleted == true)
883                         {
884                                 r = SeekTo(START_TIME);
885                                 TryReturnVoid(r == E_SUCCESS, "SeekTo() failed:%s", GetErrorMessage(r));
886
887                                 currentPlayTime.Format(20, L"%02d:%02d:%02d", START_TIME, START_TIME, START_TIME);
888
889                                 currentProgressPos = static_cast<int>(__pPlayer->GetPosition() / (__pPlayer->GetDuration() / MAX_PROGRESS_RANGE));
890                                 CallOnPlayTimeChanged(currentProgressPos, currentPlayTime);
891
892                                 __playerSeekCompleted = false;
893                         }
894                 }
895                 else
896                 {
897                         if (__playerSeekCompleted == true)
898                         {
899                                 r = SeekTo(msTime);
900                                 TryReturnVoid(r == E_SUCCESS, "SeekTo() failed:%s", GetErrorMessage(r));
901
902                                 dateTime.AddSeconds(msTime / MILLISECOND);
903                                 currentPlayTime.Format(20, L"%02d:%02d:%02d", dateTime.GetHour(), dateTime.GetMinute(), dateTime.GetSecond());
904
905                                 currentProgressPos = static_cast<int>(__pPlayer->GetPosition() / (__pPlayer->GetDuration() / MAX_PROGRESS_RANGE));
906                                 CallOnPlayTimeChanged(currentProgressPos, currentPlayTime);
907
908                                 __playerSeekCompleted = false;
909                         }
910                 }
911         }
912 }
913
914 void
915 VideoPlayerPresentationModel::FastForward(bool playAfterSeek)
916 {
917         AppLogDebug("FastForward");
918
919         if (__pPlayer->GetState() != PLAYER_STATE_ENDOFCLIP)
920         {
921                 PausePlay();
922                 __playAfterSeek = playAfterSeek;
923                 __pFastForwardTimer->Start(LONGKEY_TIMER_DURATION);
924         }
925 }
926
927 void
928 VideoPlayerPresentationModel::FastRewind(bool playAfterSeek)
929 {
930         AppLogDebug("FastRewind");
931
932         if (__pPlayer->GetState() != PLAYER_STATE_ENDOFCLIP)
933         {
934                 PausePlay();
935                 __playAfterSeek = playAfterSeek;
936                 __pFastRewindTimer->Start(LONGKEY_TIMER_DURATION);
937         }
938 }
939
940 void
941 VideoPlayerPresentationModel::StopFastForwardRewind(void)
942 {
943         AppLogDebug("StopFastForwardRewind");
944
945         if (__pPlayer->GetState() != PLAYER_STATE_ENDOFCLIP)
946         {
947                 __pFastForwardTimer->Cancel();
948                 __pFastRewindTimer->Cancel();
949
950                 if (__playerSeekCompleted == true)
951                 {
952                         if (__playAfterSeek == true)
953                         {
954                                 result r = __pPlayer->Play();
955                                 TryReturnVoid(r == E_SUCCESS, "__pPlayer->Play() failed:%s", GetErrorMessage(r));
956
957                                 CallOnPlayStateChanged(__pPlayer->GetState());
958
959                                 __pTimer->Start(PROGRESS_TIMER_DURATION);
960                         }
961                 }
962                 else
963                 {
964                         __playerLastSeekCompleted = true;
965                 }
966         }
967 }
968
969 String
970 VideoPlayerPresentationModel::GetMediaPathName(void) const
971 {
972         AppLogDebug("GetMediaPathName");
973
974         String* pMediaName = null;
975
976         pMediaName = static_cast<String*>(__pMediaPathArrayList->GetAt(__currentMediaIndex));
977         return *pMediaName;
978 }
979
980 int
981 VideoPlayerPresentationModel::GetMediaCount(void) const
982 {
983         return __pMediaPathArrayList->GetCount();
984 }
985
986 int
987 VideoPlayerPresentationModel::GetCurrentMediaIndex(void) const
988 {
989         return __currentMediaIndex;
990 }
991
992 int
993 VideoPlayerPresentationModel::GetPreviousMediaIndex(void) const
994 {
995         return __previousMediaIndex;
996 }
997
998 result
999 VideoPlayerPresentationModel::InitializePlayer(void)
1000 {
1001         BufferInfo bufferInfo;
1002
1003         AppLogDebug("InitializePlayer");
1004         delete __pPlayer;
1005
1006         __pOverlayPanel->GetBackgroundBufferInfo(bufferInfo);
1007
1008         __pPlayer = new (std::nothrow) Player();
1009         result r = __pPlayer->Construct(*this, &bufferInfo);
1010
1011         AppLogDebug("__pPlayer [%x]", __pPlayer);
1012
1013         return r;
1014 }