7075ef8d5c34c43756f0674ca1f1e648b3484787
[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 PresentationModel 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         , __pOverlayRegion(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
211         const IList* pInfoList = null;
212         const VideoStreamInfo* pInfo = null;
213         MediaStreamInfo* pStreamInfo = null;
214
215         if (__pPlayer->GetState() == PLAYER_STATE_PLAYING)
216         {
217                 pStreamInfo = __pPlayer->GetCurrentMediaStreamInfoN();
218                 pInfoList = pStreamInfo->GetVideoStreamInfoList();
219
220                 pInfo = (const VideoStreamInfo*)pInfoList->GetAt(0);
221
222                 rect.width = pInfo->GetWidth();
223                 rect.height = pInfo->GetHeight();
224
225                 __mediaWidth = pInfo->GetWidth();
226                 __mediaHeight = pInfo->GetHeight();
227
228                 delete pStreamInfo;
229         }
230         else
231         {
232                 rect.width = __mediaWidth;
233                 rect.height = __mediaHeight;
234         }
235
236         return rect;
237 }
238
239 result
240 VideoPlayerPresentationModel::ConstructPlayerInstanceWithBufferInfo(void)
241 {
242         result r = E_FAILURE;
243         BufferInfo bufferInfo;
244
245         r = __pOverlayRegion->GetBackgroundBufferInfo(bufferInfo);
246         TryCatch(r == E_SUCCESS, , "__pOverlayRegion->GetBackgroundBufferInfo() failed:%s", GetErrorMessage(r));
247
248         delete __pPlayer;
249
250         __pPlayer = new (std::nothrow) Player();
251
252         __pPlayer->Construct(*this, &bufferInfo);
253
254         AppLogDebug("__pPlayer [%x]", __pPlayer);
255
256         return r;
257
258 CATCH:
259         return r;
260 }
261
262 result
263 VideoPlayerPresentationModel::SeekTo(long msTime)
264 {
265         result r = E_FAILURE;
266
267         PlayerState playState = GetState();
268
269         switch (playState)
270         {
271         case PLAYER_STATE_PLAYING:
272                 // fall through
273         case PLAYER_STATE_PAUSED:
274                 {
275                         r = __pPlayer->SeekTo(msTime);
276                         TryCatch(r == E_SUCCESS, , "__pPlayer->SeekTo failed:%s", GetErrorMessage(r));
277                 }
278                 break;
279
280         default:
281                 break;
282         }
283
284         return r;
285
286 CATCH:
287         return r;
288 }
289
290 void
291 VideoPlayerPresentationModel::OnPlayerSeekCompleted(result r)
292 {
293         AppLogDebug("OnPlayerSeekCompleted");
294
295         __playerSeekCompleted = true;
296
297         if (__playerLastSeekCompleted == true)
298         {
299                 if (__playAfterSeek == true)
300                 {
301                         result r = __pPlayer->Play();
302                         TryReturnVoid(r == E_SUCCESS, "__pPlayer->Play():%s", GetErrorMessage(r));
303
304                         CallOnPlayStateChanged(__pPlayer->GetState());
305
306                         __pTimer->Start(PROGRESS_TIMER_DURATION);
307                 }
308
309                 __playerLastSeekCompleted = false;
310         }
311 }
312
313 void
314 VideoPlayerPresentationModel::SetPlayerBuffer(const BufferInfo& bufferInfo)
315 {
316         result r = E_FAILURE;
317         r = __pPlayer->SetRenderingBuffer(bufferInfo);
318         TryReturnVoid(r == E_SUCCESS, "__pPlayer->SetRenderingBuffer failed:%s", GetErrorMessage(r));
319 }
320
321 result
322 VideoPlayerPresentationModel::StartPlay(void)
323 {
324         result r = E_FAILURE;
325
326         PlayerState playState = __pPlayer->GetState();
327         AppLogDebug("StartPlay : %d", playState);
328
329         switch (playState)
330         {
331         case PLAYER_STATE_ENDOFCLIP:
332                 // fall through
333         case PLAYER_STATE_INITIALIZED:
334                 // fall through
335         case PLAYER_STATE_CLOSED:
336                 {
337                         String mediaPath;
338                         String mediaName;
339                         String delim = L"/";
340
341                         mediaPath = GetMediaPathName();
342
343                         StringTokenizer strTok(mediaPath, delim);
344
345                         while (strTok.HasMoreTokens())
346                         {
347                                 strTok.GetNextToken(mediaName);
348                         }
349
350                         if (mediaPath.StartsWith(L"http://", 0) == true
351                                 || mediaPath.StartsWith(L"https://", 0) == true
352                                 || mediaPath.StartsWith(L"rtsp://", 0) == true
353                                 || mediaPath.StartsWith(L"rtp://", 0) == true)
354                         {
355                                 Uri mediaUri;
356                                 r = mediaUri.SetUri(mediaPath);
357
358                                 if (r == E_SUCCESS)
359                                 {
360                                         r = __pPlayer->OpenUrl(mediaUri);
361                                 }
362                         }
363                         else
364                         {
365                                 r = __pPlayer->OpenFile(mediaPath);
366                         }
367
368                         if (r != E_SUCCESS)
369                         {
370                                 AppResource* pAppResource = Application::GetInstance()->GetAppResource();
371                                 String messageBoxString;
372                                 pAppResource->GetString(IDS_COM_POP_UNSUPPORTED_FILE_TYPE, messageBoxString);
373                                 MessageBox messageBox;
374                                 messageBox.Construct(L"", messageBoxString, MSGBOX_STYLE_OK, 3000);
375                                 int modalResult = 0;
376                                 messageBox.ShowAndWait(modalResult);
377
378                                 UiApp* pApp = UiApp::GetInstance();
379                                 pApp->Terminate();
380                         }
381                         else
382                         {
383                                 r = __pPlayer->Play();
384                                 TryCatch(r == E_SUCCESS, , "__pPlayer->Play() failed:%s", GetErrorMessage(r));
385
386                                 CallOnPlayContentChanged(mediaName);
387
388                                 r = __pTimer->Start(PROGRESS_TIMER_DURATION);
389                                 TryCatch(r == E_SUCCESS, , "__pTimer->Start() failed:%s", GetErrorMessage(r));
390                         }
391                 }
392                 break;
393
394         case PLAYER_STATE_PAUSED:
395                 {
396                         r = __pPlayer->Play();
397                         TryCatch(r == E_SUCCESS, , "__pPlayer->Play() failed:%s", GetErrorMessage(r));
398
399                         r = __pTimer->Start(PROGRESS_TIMER_DURATION);
400                         TryCatch(r == E_SUCCESS, , "__pTimer->Start() failed:%s", GetErrorMessage(r));
401                 }
402                 break;
403
404         default:
405                 break;
406         }
407
408         CallOnPlayStateChanged(__pPlayer->GetState());
409
410 CATCH:
411         return r;
412 }
413
414 result
415 VideoPlayerPresentationModel::StopPlay(void)
416 {
417         result r = E_FAILURE;
418
419         PlayerState playState = GetState();
420         AppLogDebug("StopPlay : %d", playState);
421
422         switch (playState)
423         {
424         case PLAYER_STATE_PLAYING:
425                 // fall through
426         case PLAYER_STATE_PAUSED:
427                 {
428                         r = __pPlayer->Stop();
429                         TryCatch(r == E_SUCCESS, , "__pPlayer->Stop() failed:%s", GetErrorMessage(r));
430                 }
431                 break;
432
433         default:
434                 break;
435         }
436
437 CATCH:
438         return r;
439 }
440
441 result
442 VideoPlayerPresentationModel::PausePlay(void)
443 {
444         result r = E_FAILURE;
445         PlayerState playState = GetState();
446         AppLogDebug("PausePlay : %d", playState);
447
448         if (playState == PLAYER_STATE_PLAYING)
449         {
450                 r = __pPlayer->Pause();
451                 TryCatch(r == E_SUCCESS, , "__pPlayer->Pause() failed:%s", GetErrorMessage(r));
452         }
453         else
454         {
455                 AppLogDebug("Invalid state of player");
456         }
457
458         CallOnPlayStateChanged(__pPlayer->GetState());
459
460 CATCH:
461         return r;
462 }
463
464 result
465 VideoPlayerPresentationModel::ClosePlay(void)
466 {
467         result r = E_FAILURE;
468         PlayerState playState = GetState();
469
470         switch (playState)
471         {
472         case PLAYER_STATE_OPENED:
473                 // fall through
474         case PLAYER_STATE_STOPPED:
475                 // fall through
476         case PLAYER_STATE_ENDOFCLIP:
477                 {
478                         r = __pPlayer->Close();
479                         TryCatch(r == E_SUCCESS, , "__pPlayer->Close() failed:%s", GetErrorMessage(r));
480                 }
481                 break;
482
483         default:
484                 break;
485         }
486
487 CATCH:
488         return r;
489 }
490
491 PlayerState
492 VideoPlayerPresentationModel::GetState(void) const
493 {
494         return __pPlayer->GetState();
495 }
496
497 long
498 VideoPlayerPresentationModel::GetDuration(void) const
499 {
500         return __pPlayer->GetDuration();
501 }
502
503 long
504 VideoPlayerPresentationModel::GetPosition(void) const
505 {
506         return __pPlayer->GetPosition();
507 }
508
509 void
510 VideoPlayerPresentationModel::SetVideoPlayerEventListener(IVideoPlayerEventListener* pPlayerListener)
511 {
512         __pVideoPlayerEventListener = pPlayerListener;
513 }
514
515 void
516 VideoPlayerPresentationModel::SetOverlayRegion(OverlayRegion* pOverlay)
517 {
518         __pOverlayRegion = pOverlay;
519 }
520
521 void
522 VideoPlayerPresentationModel::SetRenderingBuffer(void)
523 {
524         BufferInfo bufferInfo;
525
526         __pOverlayRegion->GetBackgroundBufferInfo(bufferInfo);
527         __pPlayer->SetRenderingBuffer(bufferInfo);
528
529         if (__pPlayer->GetState() == PLAYER_STATE_PAUSED)
530         {
531                 __pPlayer->SeekTo(__pPlayer->GetPosition());
532         }
533 }
534
535 void
536 VideoPlayerPresentationModel::OnPlayerOpened(result r)
537 {
538         AppLogDebug("OnPlayerOpened");
539         __pVideoPlayerEventListener->OnPlayOpened(r);
540 }
541
542 void
543 VideoPlayerPresentationModel::OnPlayerEndOfClip(void)
544 {
545         bool playNextContent = false;
546
547         AppLogDebug("OnPlayerEndOfClip");
548
549         __pTimer->Cancel();
550         __pFastForwardTimer->Cancel();
551         __pFastRewindTimer->Cancel();
552
553         __playerSeekCompleted = true;
554
555         if (__pMediaPathArrayList->GetCount() < MULTI_CONTENT_COUNT)
556         {
557                 __previousMediaIndex = INIT_CONTENT_INDEX;
558                 __currentMediaIndex = INIT_CONTENT_INDEX;
559                 __nextMediaIndex = INIT_CONTENT_INDEX;
560         }
561         else
562         {
563                 if (__currentMediaIndex == __pMediaPathArrayList->GetCount() - 1)
564                 {
565                         __previousMediaIndex = __currentMediaIndex;
566                         __currentMediaIndex = INIT_CONTENT_INDEX;
567                         __nextMediaIndex = __currentMediaIndex + 1;
568                 }
569                 else
570                 {
571                         __previousMediaIndex = __currentMediaIndex;
572                         __currentMediaIndex = __nextMediaIndex;
573                         __nextMediaIndex = __currentMediaIndex + 1;
574                 }
575         }
576
577         if ((__currentMediaIndex > __previousMediaIndex)
578                 && (__currentMediaIndex < __pMediaPathArrayList->GetCount()))
579         {
580                 playNextContent = true;
581         }
582         else
583         {
584                 playNextContent = false;
585         }
586
587         __pVideoPlayerEventListener->OnPlayEndOfClip(playNextContent);
588 }
589
590 void
591 VideoPlayerPresentationModel::OnPlayerBuffering(int percent)
592 {
593         AppLogDebug("OnPlayerBuffering");
594         __pVideoPlayerEventListener->OnPlayBuffering(percent);
595 }
596
597 void
598 VideoPlayerPresentationModel::OnPlayerErrorOccurred(PlayerErrorReason r)
599 {
600         AppLogDebug("OnPlayerErrorOccurred");
601         __pVideoPlayerEventListener->OnPlayErrorOccurred(r);
602 }
603
604 void
605 VideoPlayerPresentationModel::OnPlayerInterrupted(void)
606 {
607         AppLogDebug("OnPlayerInterrupted");
608         __pVideoPlayerEventListener->OnPlayInterrupted();
609
610         CallOnPlayStateChanged(__pPlayer->GetState());
611 }
612
613 void
614 VideoPlayerPresentationModel::OnPlayerAudioFocusChanged(void)
615 {
616         AppLog("OnPlayerAudioFocusChanged");
617
618         CallOnPlayStateChanged(__pPlayer->GetState());
619 }
620
621 void
622 VideoPlayerPresentationModel::OnPlayerReleased(void)
623 {
624         AppLogDebug("OnPlayerReleased");
625         __pVideoPlayerEventListener->OnPlayReleased();
626 }
627
628 void
629 VideoPlayerPresentationModel::CallOnPlayContentChanged(const String& fileName)
630 {
631         AppLogDebug("CallOnPlayContentChanged");
632
633         __pVideoPlayerEventListener->OnPlayContentChanged(fileName);
634 }
635
636 void
637 VideoPlayerPresentationModel::CallOnPlayTimeChanged(int currentProgressPos, String& currentPlayTime)
638 {
639         AppLogDebug("CallOnPlayTimeChanged");
640
641         __pVideoPlayerEventListener->OnPlayTimeChanged(currentProgressPos, currentPlayTime);
642 }
643
644 void
645 VideoPlayerPresentationModel::CallOnPlayStateChanged(PlayerState playState)
646 {
647         AppLogDebug("CallOnPlayStateChanged");
648
649         __pVideoPlayerEventListener->OnPlayStateChanged(playState);
650 }
651
652 void
653 VideoPlayerPresentationModel::InitTimer(void)
654 {
655         AppLogDebug("InitTimer");
656
657         __pTimer = new (std::nothrow) Timer();
658         __pTimer->Construct(*this);
659
660         __pFastForwardTimer = new (std::nothrow) Timer();
661         __pFastForwardTimer->Construct(*this);
662
663         __pFastRewindTimer = new (std::nothrow) Timer();
664         __pFastRewindTimer->Construct(*this);
665 }
666
667 void
668 VideoPlayerPresentationModel::DeleteTimer(void)
669 {
670         AppLogDebug("DeleteTimer");
671
672         __pTimer->Cancel();
673         delete __pTimer;
674         __pTimer = null;
675
676         __pFastForwardTimer->Cancel();
677         delete __pFastForwardTimer;
678         __pFastForwardTimer = null;
679
680         __pFastRewindTimer->Cancel();
681         delete __pFastRewindTimer;
682         __pFastRewindTimer = null;
683 }
684
685 void
686 VideoPlayerPresentationModel::OnTimerExpired(Timer& timer)
687 {
688         result r = E_FAILURE;
689         long msTime = 0;
690         int currentProgressPos = 0;
691         DateTime dateTime;
692         String currentPlayTime;
693
694         if (timer.GetHashCode() == __pTimer->GetHashCode())
695         {
696                 if (__pPlayer->GetState() == PLAYER_STATE_INITIALIZED || __pPlayer->GetState() == PLAYER_STATE_PLAYING)
697                 {
698                         dateTime.AddSeconds(__pPlayer->GetPosition() / MILLISECOND);
699                         currentPlayTime.Format(20, L"%02d:%02d:%02d", dateTime.GetHour(), dateTime.GetMinute(), dateTime.GetSecond());
700
701                         currentProgressPos = static_cast<int>(__pPlayer->GetPosition() / (__pPlayer->GetDuration() / MAX_PROGRESS_RANGE));
702
703                         CallOnPlayTimeChanged(currentProgressPos, currentPlayTime);
704
705                         __pTimer->Start(PROGRESS_TIMER_DURATION);
706                 }
707         }
708         else if (timer.GetHashCode() == __pFastForwardTimer->GetHashCode())
709         {
710                 msTime = __pPlayer->GetPosition();
711
712                 msTime = msTime + (TWO_SEC * MILLISECOND);
713
714                 if (msTime < __pPlayer->GetDuration())
715                 {
716                         if (__playerSeekCompleted == true)
717                         {
718                                 r = SeekTo(msTime);
719                                 TryReturnVoid(r == E_SUCCESS, "VideoPlayerPresentationModel::SeekTo() failed:%s", GetErrorMessage(r));
720
721                                 __playerSeekCompleted = false;
722
723                                 dateTime.AddSeconds(msTime / MILLISECOND);
724                                 currentPlayTime.Format(20, L"%02d:%02d:%02d", dateTime.GetHour(), dateTime.GetMinute(), dateTime.GetSecond());
725
726                                 currentProgressPos = static_cast<int>(__pPlayer->GetPosition() / (__pPlayer->GetDuration() / MAX_PROGRESS_RANGE));
727
728                                 CallOnPlayTimeChanged(currentProgressPos, currentPlayTime);
729                         }
730                 }
731                 else
732                 {
733                         if (__playerSeekCompleted == true)
734                         {
735                                 r = SeekTo(__pPlayer->GetDuration());
736                                 TryReturnVoid(r == E_SUCCESS, "VideoPlayerPresentationModel::SeekTo() failed:%s", GetErrorMessage(r));
737                                 __playerSeekCompleted = false;
738
739                                 dateTime.AddSeconds(__pPlayer->GetDuration() / MILLISECOND);
740                                 currentPlayTime.Format(20, L"%02d:%02d:%02d", dateTime.GetHour(), dateTime.GetMinute(), dateTime.GetSecond());
741
742                                 currentProgressPos = static_cast<int>(__pPlayer->GetPosition() / (__pPlayer->GetDuration() / MAX_PROGRESS_RANGE));
743
744                                 CallOnPlayTimeChanged(currentProgressPos, currentPlayTime);
745                         }
746                 }
747                 __pFastForwardTimer->Start(LONGKEY_TIMER_DURATION);
748         }
749         else if (timer.GetHashCode() == __pFastRewindTimer->GetHashCode())
750         {
751                 msTime = __pPlayer->GetPosition();
752
753                 msTime = msTime - (TWO_SEC * MILLISECOND);
754
755                 if (msTime > START_TIME)
756                 {
757                         if (__playerSeekCompleted == true)
758                         {
759                                 r = SeekTo(msTime);
760                                 TryReturnVoid(r == E_SUCCESS, "VideoPlayerPresentationModel::SeekTo() failed:%s", GetErrorMessage(r));
761
762                                 __playerSeekCompleted = false;
763
764                                 dateTime.AddSeconds(msTime / MILLISECOND);
765                                 currentPlayTime.Format(20, L"%02d:%02d:%02d", dateTime.GetHour(), dateTime.GetMinute(), dateTime.GetSecond());
766
767                                 currentProgressPos = static_cast<int>(__pPlayer->GetPosition() / (__pPlayer->GetDuration() / MAX_PROGRESS_RANGE));
768
769                                 CallOnPlayTimeChanged(currentProgressPos, currentPlayTime);
770                         }
771                 }
772                 else
773                 {
774                         if (__playerSeekCompleted == true)
775                         {
776                                 r = SeekTo(START_TIME);
777                                 TryReturnVoid(r == E_SUCCESS, "VideoPlayerPresentationModel::SeekTo() failed:%s", GetErrorMessage(r));
778
779                                 __playerSeekCompleted = false;
780
781                                 currentPlayTime.Format(20, L"%02d:%02d:%02d", START_TIME, START_TIME, START_TIME);
782
783                                 currentProgressPos = START_TIME;
784
785                                 CallOnPlayTimeChanged(currentProgressPos, currentPlayTime);
786                         }
787                 }
788                 __pFastRewindTimer->Start(LONGKEY_TIMER_DURATION);
789         }
790 }
791
792 void
793 VideoPlayerPresentationModel::Forward(void)
794 {
795         result r = E_FAILURE;
796
797         long msTime = 0;
798         int currentProgressPos = 0;
799         DateTime dateTime;
800         String currentPlayTime;
801
802         AppLogDebug("Forward");
803
804         if (__pPlayer->GetState() != PLAYER_STATE_ENDOFCLIP)
805         {
806                 msTime = __pPlayer->GetPosition();
807
808                 msTime = msTime + (TEN_SEC * MILLISECOND);
809
810                 if (msTime < __pPlayer->GetDuration())
811                 {
812                         if (__playerSeekCompleted == true)
813                         {
814                                 r = SeekTo(msTime);
815                                 TryReturnVoid(r == E_SUCCESS, "SeekTo() failed:%s", GetErrorMessage(r));
816
817                                 dateTime.AddSeconds(msTime / MILLISECOND);
818                                 currentPlayTime.Format(20, L"%02d:%02d:%02d", dateTime.GetHour(), dateTime.GetMinute(), dateTime.GetSecond());
819
820                                 currentProgressPos = static_cast<int>(__pPlayer->GetPosition() / (__pPlayer->GetDuration() / MAX_PROGRESS_RANGE));
821                                 CallOnPlayTimeChanged(currentProgressPos, currentPlayTime);
822
823                                 __playerSeekCompleted = false;
824                         }
825                 }
826                 else
827                 {
828                         if (__playerSeekCompleted == true)
829                         {
830                                 r = SeekTo(__pPlayer->GetDuration());
831                                 TryReturnVoid(r == E_SUCCESS, "SeekTo() failed:%s", GetErrorMessage(r));
832
833                                 dateTime.AddSeconds(__pPlayer->GetDuration() / MILLISECOND);
834                                 currentPlayTime.Format(20, L"%02d:%02d:%02d", dateTime.GetHour(), dateTime.GetMinute(), dateTime.GetSecond());
835
836                                 currentProgressPos = static_cast<int>(__pPlayer->GetPosition() / (__pPlayer->GetDuration() / MAX_PROGRESS_RANGE));
837                                 CallOnPlayTimeChanged(currentProgressPos, currentPlayTime);
838
839                                 __playerSeekCompleted = false;
840                         }
841                 }
842         }
843 }
844
845 void
846 VideoPlayerPresentationModel::Rewind(void)
847 {
848         result r = E_FAILURE;
849
850         long msTime = 0;
851         int currentProgressPos = 0;
852         DateTime dateTime;
853         String currentPlayTime;
854
855         AppLogDebug("Rewind");
856
857         if (__pPlayer->GetState() != PLAYER_STATE_ENDOFCLIP)
858         {
859                 msTime = __pPlayer->GetPosition();
860
861                 msTime = msTime - (TEN_SEC * MILLISECOND);
862
863                 if (msTime < START_TIME)
864                 {
865                         if (__playerSeekCompleted == true)
866                         {
867                                 r = SeekTo(START_TIME);
868                                 TryReturnVoid(r == E_SUCCESS, "SeekTo() failed:%s", GetErrorMessage(r));
869
870                                 currentPlayTime.Format(20, L"%02d:%02d:%02d", START_TIME, START_TIME, START_TIME);
871
872                                 currentProgressPos = static_cast<int>(__pPlayer->GetPosition() / (__pPlayer->GetDuration() / MAX_PROGRESS_RANGE));
873                                 CallOnPlayTimeChanged(currentProgressPos, currentPlayTime);
874
875                                 __playerSeekCompleted = false;
876                         }
877                 }
878                 else
879                 {
880                         if (__playerSeekCompleted == true)
881                         {
882                                 r = SeekTo(msTime);
883                                 TryReturnVoid(r == E_SUCCESS, "SeekTo() failed:%s", GetErrorMessage(r));
884
885                                 dateTime.AddSeconds(msTime / MILLISECOND);
886                                 currentPlayTime.Format(20, L"%02d:%02d:%02d", dateTime.GetHour(), dateTime.GetMinute(), dateTime.GetSecond());
887
888                                 currentProgressPos = static_cast<int>(__pPlayer->GetPosition() / (__pPlayer->GetDuration() / MAX_PROGRESS_RANGE));
889                                 CallOnPlayTimeChanged(currentProgressPos, currentPlayTime);
890
891                                 __playerSeekCompleted = false;
892                         }
893                 }
894         }
895 }
896
897 void
898 VideoPlayerPresentationModel::FastForward(bool playAfterSeek)
899 {
900         AppLogDebug("FastForward");
901
902         if (__pPlayer->GetState() != PLAYER_STATE_ENDOFCLIP)
903         {
904                 PausePlay();
905                 __playAfterSeek = playAfterSeek;
906                 __pFastForwardTimer->Start(LONGKEY_TIMER_DURATION);
907         }
908 }
909
910 void
911 VideoPlayerPresentationModel::FastRewind(bool playAfterSeek)
912 {
913         AppLogDebug("FastRewind");
914
915         if (__pPlayer->GetState() != PLAYER_STATE_ENDOFCLIP)
916         {
917                 PausePlay();
918                 __playAfterSeek = playAfterSeek;
919                 __pFastRewindTimer->Start(LONGKEY_TIMER_DURATION);
920         }
921 }
922
923 void
924 VideoPlayerPresentationModel::StopFastForwardRewind(void)
925 {
926         AppLogDebug("StopFastForwardRewind");
927
928         if (__pPlayer->GetState() != PLAYER_STATE_ENDOFCLIP)
929         {
930                 __pFastForwardTimer->Cancel();
931                 __pFastRewindTimer->Cancel();
932
933                 if (__playerSeekCompleted == true)
934                 {
935                         if (__playAfterSeek == true)
936                         {
937                                 result r = __pPlayer->Play();
938                                 TryReturnVoid(r == E_SUCCESS, "__pPlayer->Play() failed:%s", GetErrorMessage(r));
939
940                                 CallOnPlayStateChanged(__pPlayer->GetState());
941
942                                 __pTimer->Start(PROGRESS_TIMER_DURATION);
943                         }
944                 }
945                 else
946                 {
947                         __playerLastSeekCompleted = true;
948                 }
949         }
950 }
951
952 String
953 VideoPlayerPresentationModel::GetMediaPathName(void) const
954 {
955         AppLogDebug("GetMediaPathName");
956
957         String* pMediaName = null;
958
959         pMediaName = static_cast<String*>(__pMediaPathArrayList->GetAt(__currentMediaIndex));
960         return *pMediaName;
961 }
962
963 int
964 VideoPlayerPresentationModel::GetMediaCount(void) const
965 {
966         return __pMediaPathArrayList->GetCount();
967 }
968
969 int
970 VideoPlayerPresentationModel::GetCurrentMediaIndex(void) const
971 {
972         return __currentMediaIndex;
973 }
974
975 int
976 VideoPlayerPresentationModel::GetPreviousMediaIndex(void) const
977 {
978         return __previousMediaIndex;
979 }
980
981 result
982 VideoPlayerPresentationModel::InitializePlayer(void)
983 {
984         BufferInfo bufferInfo;
985
986         AppLogDebug("InitializePlayer");
987         delete __pPlayer;
988
989         __pOverlayRegion->GetBackgroundBufferInfo(bufferInfo);
990
991         __pPlayer = new (std::nothrow) Player();
992         result r = __pPlayer->Construct(*this, &bufferInfo);
993
994         AppLogDebug("__pPlayer [%x]", __pPlayer);
995
996         return r;
997 }