os/graphics/graphicstest/uibench/s60/src/tests_ui_video/tuiandvideo.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2005-2008 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 /**
    20  @file
    21  @test
    22  @internalComponent - Internal Symbian test code 
    23 */
    24 
    25 /**
    26 This test depends on the following environment:
    27 
    28 ==========================================================================
    29 == Required step - Install Xvid codec
    30 ==========================================================================
    31 Note that these drivers are distributed under the GNU GENERAL PUBLIC LICENSE.
    32 
    33 ** Go to:
    34 http://developer.symbian.com/wiki/display/pub/Open+source+projects
    35 and get the XviD source zip:
    36 http://developer.symbian.com/wiki/download/attachments/1154/xvid.zip
    37 
    38 ** Unzip this file and build the source in 
    39 xvidhwdevice\group
    40 xvidpu\group
    41 
    42 
    43 ==========================================================================
    44 == Required step - Running on emulator - Modify epoc.ini and wsini.ini
    45 ==========================================================================
    46 
    47 ** Update your epoc.ini so that GCE is enabled. Add:
    48 ...
    49 SYMBIAN_GRAPHICS_USE_GCE ON
    50 SYMBIAN_BASE_USE_GCE ON
    51 ...
    52 
    53 ** Update your wsini.ini so that the following is present:
    54 ...
    55 WINDOWMODE COLOR16MAP
    56 KEYCLICKPLUGIN KeyClickRef
    57 TRANSPARENCY
    58 CHROMAKEYCOLOR 0xFFFF00FF
    59 ...
    60 
    61 
    62 ==========================================================================
    63 == Required step - Running on hardware - Buildrom command
    64 ==========================================================================
    65 
    66 ** When building the rom, make sure you use the following flags
    67  buildrom 
    68  	<options> 
    69  	-DSYMBIAN_BASE_USE_GCE 
    70  	-DSYMBIAN_GRAPHICS_USE_GCE 
    71  	-DUSE_24BPP_DISPLAY_VARIANT
    72  	<further options/flags>
    73  	videoplay.iby
    74  	<further oby/iby files>
    75  
    76 	 the xvid video drivers being installed in the runtime environment.
    77 */
    78 
    79 #include "tuiandvideo.h"
    80 
    81 #include <mmf/common/mmferrors.h>
    82 
    83 #ifdef __WINS__
    84 _LIT(KDefaultDirName, "c:\\videoplay\\");
    85 #else
    86 _LIT(KDefaultDirName, "e:\\videoplay\\");
    87 #endif
    88 
    89 _LIT(KDefaultFileExt, "*.avi");
    90 _LIT(KDefaultFile, "skiing2.avi");
    91 
    92 const TTimeIntervalMicroSeconds KTapTimeOut(300*1000);
    93 const TTimeIntervalMicroSeconds32 KIdleTimeOut(1*1000*1000);
    94 const TTimeIntervalMicroSeconds32 KPlayTimeOut(2*1000*1000);
    95 const TTimeIntervalMicroSeconds32 KButtonTimeOut(2*1000*1000);
    96 
    97 _LIT(KVideoAppBitmapsFile, "z:\\resource\\apps\\videoplay2.mbm");
    98 
    99 //From videoplay.hrh
   100 enum TTestCommands
   101     {
   102     ETestCmdOpen = 1000,
   103     ETestCmdStop,
   104     ETestCmdPlayFile
   105     };
   106 
   107 //UIBench test step commands
   108 enum TTestStepCommand
   109 	{
   110     ETestCmdInitialised = 0,
   111     ETestCmdPlay,
   112     ETestCmdPause,
   113     ETestCmdBounce,
   114     ETestCmdJump,
   115     ETestCmdStill,
   116     ETestCmdExit
   117     };
   118 
   119 // From videoplay.mbg
   120 enum TMbmVideoplay
   121 	{
   122 	EMbmVideoplayBackground,
   123 	EMbmVideoplayBackground_mask,
   124 	};
   125 
   126 
   127 /**
   128 A command handler to act as an interface through which to drive the test.
   129 @param aController	View controller, in the classic sense of MVC. This object
   130 					is used to drive the test.
   131 */
   132 CCommandSink::CCommandSink (CTestAppView* aTestView) :
   133 	CActive(EPriorityStandard),
   134 	iTestView (aTestView)
   135 	{
   136 	CActiveScheduler::Add(this);
   137 	iStatus = KRequestPending;
   138  	SetActive();
   139 	}
   140 
   141 void CCommandSink::DoCancel()
   142 	{
   143 	TRequestStatus* status = &iStatus;
   144 	User::RequestComplete(status, KErrNone);
   145 	}
   146 
   147 CCommandSink::~CCommandSink()
   148 	{
   149 	Cancel();
   150 	}
   151 
   152 void CCommandSink::Init(RThread* aTargetThread, TRequestStatus* aTargetStatus)
   153 	{
   154 	iTargetThread = aTargetThread;
   155 	iTargetStatus = aTargetStatus;
   156 	}
   157 
   158 /**
   159 Signals the control thread (target) with a command. There must be a protocol between the
   160 controller and the control (the test thread) in terms of communication of command 
   161 signals. 
   162 */
   163 void CCommandSink::RunL ()
   164 	{
   165 	switch (iStatus.Int())
   166 		{
   167 		case ETestCmdPlay:
   168 			iTestView->PlayVideo();
   169 			break;
   170 		case ETestCmdPause:
   171 			iTestView->PauseVideo();
   172 			break;
   173 		case ETestCmdBounce:
   174 		case ETestCmdJump:
   175 		case ETestCmdStill:
   176 			iTestView->SetRunMode(iStatus.Int());
   177 			break;
   178 		}
   179 	
   180 	iStatus = KRequestPending;
   181  	SetActive();
   182 	}
   183 
   184 CTUiAndVideo::CTUiAndVideo()
   185 	{
   186 	SetTestStepName(KTUiAndVideo);
   187 	}
   188 
   189 void CTUiAndVideo::IssueCommand (TUint aCommand)
   190 	{
   191 	TRequestStatus* targetStatus = iAppUi->CommandSink();
   192 	ConeThread()->RequestComplete(targetStatus, aCommand);
   193 	}
   194 
   195 void CTUiAndVideo::StepState (TUint aNextState, TUint aPause)
   196 	{
   197 	User::After(aPause);
   198 	TRequestStatus* testState = &iTestState;
   199 	User::RequestComplete (testState, aNextState);
   200 	}
   201 
   202 /**
   203    @SYMTestCaseID GRAPHICS-UI-BENCH-XXXX
   204    @SYMTestCaseDesc Put description here
   205    @SYMTestActions Put description here
   206    @SYMTestExpectedResults Put description here
   207 */
   208 TVerdict CTUiAndVideo::doTestStepL()
   209 	{
   210 	iProfiler->InitResults();
   211 
   212 	StepState(ETestCmdInitialised, 0);
   213    	
   214 	TBool continueTest = ETrue;
   215 	while (continueTest)
   216 		{
   217 		User::WaitForRequest(iTestState);
   218 		switch (iTestState.Int())
   219 			{
   220 			case ETestCmdInitialised:
   221 				User::After(5000000);
   222 			case ETestCmdPlay:
   223 				IssueCommand(ETestCmdPlay);
   224 				StepState(ETestCmdBounce, 5000000);
   225 				break;
   226 			case ETestCmdPause:
   227 				break;
   228 			case ETestCmdBounce:
   229 				IssueCommand(ETestCmdBounce);
   230 				StepState(ETestCmdJump, 15000000);
   231 				break;
   232 			case ETestCmdJump:
   233 				IssueCommand(ETestCmdJump);
   234 				StepState(ETestCmdExit, 15000000);
   235 			case ETestCmdExit:
   236 				continueTest = EFalse;
   237 				break;
   238 			default:
   239 				continueTest = EFalse;
   240 				RDebug::Printf("Invalid test state!");
   241 			}
   242 		}
   243 
   244 	return TestStepResult();
   245 	}
   246 
   247 /**
   248 Runs on test thread.
   249 */
   250 void CTUiAndVideo::InitUIL(CCoeEnv* aCoeEnv)
   251 	{
   252 	iAppUi = new CTUiAndVideoAppUi();
   253 	
   254    	iAppUi->ConstructL(iTestState);
   255    	aCoeEnv->SetAppUi(iAppUi);
   256   	}
   257 
   258 //############################################################################################################
   259 //############################################################################################################
   260 //############################################################################################################
   261 //############################################################################################################
   262 
   263 //************************************************************************************************************
   264 // Application UI implementation
   265 // -- most control logic is here
   266 //************************************************************************************************************
   267 
   268 void SetCenter(TRect& aRect, const TPoint& aCenterPoint)
   269 	{
   270 	const TSize size = aRect.Size();
   271 	const TPoint pos(aCenterPoint-TSize(size.iWidth/2,size.iHeight/2).AsPoint());
   272 	aRect.SetRect(pos, size);
   273 	}
   274 
   275 void CTUiAndVideoAppUi::ConstructL(TRequestStatus /*iCommandSource*/)
   276 	{
   277 	BaseConstructL(ENoAppResourceFile);
   278 	
   279 	// Calculate the size of the application window
   280 	// Make the player square, slightly (one 16th) smaller than 
   281 	// the smallest side of the screen
   282 	TRect rect (0, 0, 300, 150);
   283 	
   284 	TRect screenRect = iCoeEnv->ScreenDevice()->SizeInPixels();
   285 	SetCenter(rect, screenRect.Center());	// Center the rect on the screen
   286 
   287 	// Create the application view
   288 	iAppView = CTestAppView::NewL(rect);
   289 	AddToStackL(iAppView);
   290 	
   291    	iCommandSink = new (ELeave) CCommandSink(iAppView);
   292    	
   293 	// Enable iIdleTimer to make the app return to foreground after a set delay
   294 	iIdleTimer = CPeriodic::NewL(CActive::EPriorityStandard);
   295 	// iUIMover moves the UI around the display.
   296 	iUIMover   = CPeriodic::NewL(CActive::EPriorityStandard);
   297 	iUIMover->Start(KIdleTimeOut, 50000, TCallBack(UIMoverCallBack, this));
   298 	// Enable iPlayTimer to make the video pause a little while before starting playing again
   299 	iPlayTimer = CPeriodic::NewL(CActive::EPriorityStandard);	
   300 
   301 	iAppView->SetPos(&rect.iTl);
   302 	iAppView->ActivateL();
   303 	iAppView->DrawNow();
   304 	
   305 	// Load default video clip	
   306 	if (KErrNone == PopulateDirFilesL(KDefaultDirName))
   307 		{
   308 		OpenSelectedFileL(KDefaultFile);
   309 		}
   310 	}
   311 
   312 TRequestStatus* CTUiAndVideoAppUi::CommandSink ()
   313 	{
   314 	return &(iCommandSink->iStatus);
   315 	}
   316 
   317 CTUiAndVideoAppUi::~CTUiAndVideoAppUi()
   318 	{
   319 	if(iAppView)
   320 		{
   321 		RemoveFromStack(iAppView);
   322 		delete iAppView;
   323 		}
   324 	delete iCommandSink;
   325 	delete iIdleTimer;
   326 	delete iUIMover;
   327 	delete iPlayTimer;
   328 	delete iLoadingPlayer;
   329 	delete iDirList;
   330 	}
   331 
   332 TInt CTUiAndVideoAppUi::UIMoverCallBack(TAny* aSelf)
   333 	{
   334 	CTUiAndVideoAppUi* self = static_cast<CTUiAndVideoAppUi*>(aSelf);
   335 	self->SetPos();
   336 	return KErrNone;
   337 	}
   338 
   339 void CTUiAndVideoAppUi::SetPos()
   340 	{
   341 	iAppView->SetPos(NULL);
   342 	}
   343 
   344 TInt CTUiAndVideoAppUi::PopulateDirFilesL(const TDesC& aDir)
   345 	{
   346 	// Load files in default folder
   347 	RFs fs;
   348 	User::LeaveIfError(fs.Connect());
   349 	
   350 	// Get the file list, sorted by name - Only with KDefaultFileExt
   351 	TFileName dir(aDir);
   352 	dir.Append(KDefaultFileExt);
   353 	
   354 	delete iDirList;
   355 	iDirList = NULL;
   356 	TInt err = fs.GetDir(dir, KEntryAttMaskSupported, ESortByName, iDirList);
   357 	fs.Close();
   358 	
   359 	if ((err == KErrPathNotFound) || (err == KErrNone && iDirList->Count()==0))
   360 		{
   361 		LogError(KNullDesC, _L("Default clip not found!"));
   362 		return KErrNotFound;
   363 		}
   364 	__ASSERT_DEBUG(err==KErrNone, User::Panic(_L("PopulateDirFiles"), err));
   365 
   366 	// set the current file with the current folder (for the time being)
   367 	iCurrentFile.Zero();
   368 	iCurrentFile.Append(aDir);
   369 	return KErrNone;
   370 	}
   371 
   372 void CTUiAndVideoAppUi::OpenSelectedFileL(const TDesC& aFile)
   373 	{
   374 	__ASSERT_DEBUG(iDirList, User::Panic(_L("iDirList"), 0));
   375 	TBool defaultFound = EFalse;
   376 	for (iCurrentIndex=0; iCurrentIndex < iDirList->Count(); iCurrentIndex++)
   377 		{
   378  		TFileName fileName = (*iDirList)[iCurrentIndex].iName;
   379  		if (fileName.Compare(aFile) ==0)
   380  			{
   381  			defaultFound = ETrue;
   382 			break;
   383 			}
   384 		}
   385 	if (!defaultFound)
   386 		{
   387 		// To avoid linking with eikcore.lib
   388 		//iEikonEnv->InfoMsg(_L("Default file not found..."));
   389 		iCurrentIndex = 0; // if no default, pick the first movie on the list
   390 		}
   391 
   392 	iCurrentFile.Append((*iDirList)[iCurrentIndex].iName);
   393 	// play the selected file.
   394 	HandleCommandL(ETestCmdOpen);
   395 	}
   396 
   397 TInt CTUiAndVideoAppUi::PlayNextFileL()
   398 	{
   399 	__ASSERT_DEBUG(iDirList, User::Panic(_L("iDirList"), 0));
   400 	if (iCurrentIndex == (iDirList->Count()-1))
   401 		{
   402 		return KErrNotFound;
   403 		}
   404 	iCurrentIndex++;
   405 	TParse file;
   406 	file.Set(iCurrentFile, NULL, NULL);
   407 	iCurrentFile = file.DriveAndPath();
   408 	iCurrentFile.Append((*iDirList)[iCurrentIndex].iName);
   409 	HandleCommandL(ETestCmdOpen);
   410 	return KErrNone;
   411 	}
   412 
   413 TInt CTUiAndVideoAppUi::PlayPreviousFileL()
   414 	{
   415 	__ASSERT_DEBUG(iDirList, User::Panic(_L("iDirList"), 0));
   416 	if (iCurrentIndex == 0)
   417 		{
   418 		return KErrNotFound;
   419 		}
   420 	iCurrentIndex--;
   421 	TParse file;
   422 	file.Set(iCurrentFile, NULL, NULL);
   423 	iCurrentFile = file.DriveAndPath();
   424 	iCurrentFile.Append((*iDirList)[iCurrentIndex].iName);
   425 	HandleCommandL(ETestCmdOpen);
   426 	return KErrNone;
   427 	}
   428 
   429 void CTUiAndVideoAppUi::LogError(const TDesC& /* aFileName */, const TDesC& /* aErrorMsg */)
   430 	{
   431 	// TODO: Fix this logging strategy.  Printf uses C-like pointers, not Symbian
   432 	// descriptors
   433 	//RDebug::Printf("Error: %s %s", aFileName, aErrorMsg);
   434 	if(iAppView)
   435 		iAppView->DisableVideo();
   436 	}
   437 
   438 void CTUiAndVideoAppUi::HandleForegroundEventL(TBool aForeground)
   439 	{
   440 	if(!aForeground && iIdleTimer)
   441 		{
   442 		if(!iIdleTimer->IsActive())
   443 			iIdleTimer->Start(KIdleTimeOut, 0, TCallBack(BackgroundCallBack, this) );
   444 		}
   445 	}
   446 
   447 TInt CTUiAndVideoAppUi::BackgroundCallBack(TAny* aSelf)
   448 	{
   449 	CTUiAndVideoAppUi* self = static_cast<CTUiAndVideoAppUi*>(aSelf);
   450 	self->BringToForeground();
   451 	return KErrNone;
   452 	}
   453 
   454 void CTUiAndVideoAppUi::BringToForeground()
   455 	{
   456 	iIdleTimer->Cancel();
   457 	const TInt err = iCoeEnv->WsSession().SetWindowGroupOrdinalPosition(iCoeEnv->RootWin().Identifier(), 0);
   458 	}
   459 
   460 TInt CTUiAndVideoAppUi::PlayTimerCallBack(TAny* aSelf)
   461 	{
   462 	CTUiAndVideoAppUi* self = static_cast<CTUiAndVideoAppUi*>(aSelf);
   463 	self->iPlayTimer->Cancel();
   464 	self->iAppView->PlayVideo();
   465 	self->iInitialised = ETrue;
   466 	
   467 	return KErrNone;
   468 	}
   469 
   470 void CTUiAndVideoAppUi::HandleCommandL(TInt aCommand)
   471 	{
   472 	switch (aCommand)
   473 		{
   474 		case ETestCmdOpen: // Open and prepare
   475 			{
   476 			iAppView->DisableVideo();
   477 			if(iLoadingPlayer)
   478 				{
   479 				iLoadingPlayer->RemoveDisplayWindow(const_cast<RWindow&>(iAppView->TheWindow()));
   480 				iLoadingPlayer->Close();
   481 				delete iLoadingPlayer;
   482 				iLoadingPlayer = NULL;
   483 				}	
   484 			iLoadingPlayer = CVideoPlayerUtility2::NewL(*this, EMdaPriorityNormal, EMdaPriorityPreferenceNone);
   485 			iLoadingPlayer->OpenFileL(iCurrentFile);
   486 			break;
   487 			}
   488 		case ETestCmdPlay: // Start playback
   489 			iAppView->PlayVideo();
   490 			break;
   491 		case ETestCmdStop: // Stop playback
   492 			iAppView->StopVideo();
   493 			break;
   494 		case ETestCmdPlayFile:
   495 			break;
   496 		case EEikCmdExit: // Exit
   497 			if(iLoadingPlayer)
   498 				iLoadingPlayer->RemoveDisplayWindow(const_cast<RWindow&>(iAppView->TheWindow()));
   499 //			Exit();
   500 			break;
   501 		}
   502 	}
   503 
   504 // Video player utility callback: open complete
   505 void CTUiAndVideoAppUi::MvpuoOpenComplete(TInt aError)
   506 	{
   507 	if (aError == KErrNone )
   508 		{
   509 		iLoadingPlayer->Prepare();
   510 		}
   511 	else if(aError == KErrNotFound)
   512 		LogError(iCurrentFile, _L("Clip not found!"));
   513 	else if(aError == KErrNotReady)
   514 		LogError(iCurrentFile, _L("Clip not ready!"));
   515 	else if(aError == KErrNotSupported)
   516 		LogError(iCurrentFile, _L("Clip format not supported!"));
   517 	else
   518 		{
   519 		User::Panic(_L("MvpuoOpen"), aError);
   520 		}
   521 	}
   522 
   523 // Video player utility callback: prepare complete
   524 void CTUiAndVideoAppUi::MvpuoPrepareComplete(TInt aError)
   525 	{
   526 	switch ( aError )
   527 		{
   528 		case KErrNone:
   529 			{
   530 			TBuf<256> loadingMsg(_L("Loading "));
   531 			TParse file;
   532 			file.Set(iCurrentFile, NULL, NULL);
   533 			loadingMsg.Append(file.Name());
   534 			
   535 			TRAPD(err, iLoadingPlayer->AddDisplayWindowL(iEikonEnv->WsSession(), *iEikonEnv->ScreenDevice(), iAppView->TheWindow()));
   536 			if(err == KErrInUse)
   537 				LogError(iCurrentFile, _L("Window Display in use!"));
   538 			else if(err == KErrNotReady)
   539 				LogError(iCurrentFile, _L("Window Display not ready!"));
   540 			else if(err)
   541 				User::Panic(_L("PrepareComplete1"), err);
   542 
   543 			iAppView->EnableVideo(iLoadingPlayer);
   544 			}
   545 			break;
   546 
   547 		case KErrMMPartialPlayback:
   548 			LogError(iCurrentFile, _L("Partial playback"));
   549 			break;
   550 
   551 		default:
   552 			User::Panic(_L("Prepare"), aError);
   553 		}
   554 	}
   555 
   556 // Video player utility callback, not used
   557 void CTUiAndVideoAppUi::MvpuoFrameReady(CFbsBitmap& /*aFrame*/, TInt aError)
   558 	{
   559 	User::Panic(_L("FrameReady"), aError);
   560 	}
   561 
   562 // Video player utility callback: play complete (error or EOF)
   563 void CTUiAndVideoAppUi::MvpuoPlayComplete(TInt aError)
   564 	{
   565 	iAppView->PlayComplete(aError);
   566 	
   567 	if (aError == KErrNone)
   568 		{
   569 		if(iPlayTimer && !iPlayTimer->IsActive())
   570 			iPlayTimer->Start(KPlayTimeOut, 0, TCallBack(PlayTimerCallBack, this));
   571 		}
   572 	else if(aError == KErrNotSupported) 
   573 		LogError(iCurrentFile, _L("Clip format not supported!"));
   574 	else if(aError == KErrTooBig)
   575 		LogError(iCurrentFile, _L("The clip is too big!"));
   576 	else if(aError == KErrNoMemory)
   577 		LogError(KNullDesC, _L("Out of memory. Likely memory leak!"));
   578 	else if(aError == KErrNotReady)
   579 		LogError(KNullDesC, _L("Clip not ready. Removed MMC?!"));
   580 	else if (aError)
   581 		User::Panic(_L("PlayComplete"), aError);
   582 	}
   583 
   584 //  Video player utility callback: event (not used?)
   585 void CTUiAndVideoAppUi::MvpuoEvent(const TMMFEvent& aEvent)
   586 	{
   587 	User::Panic(_L("Event"), aEvent.iErrorCode);
   588 	}
   589 
   590 
   591 //************************************************************************************************************
   592 // View implementation
   593 // -- draws the display
   594 //************************************************************************************************************
   595 
   596 CTestAppView* CTestAppView::NewL(const TRect& aRect)
   597 	{
   598 	CTestAppView* self = new(ELeave) CTestAppView();
   599 	CleanupStack::PushL(self);
   600 	self->ConstructL(aRect);
   601 	CleanupStack::Pop();
   602 	return self;
   603 	}
   604 
   605 CTestAppView::~CTestAppView()
   606 	{
   607 	if(iCurrentPlayer)
   608 		{
   609 		iCurrentPlayer->RemoveDisplayWindow(TheWindow());
   610 		iCurrentPlayer->Close();
   611 		delete iCurrentPlayer;
   612 		}
   613 	delete iBkgrdImage;
   614 	delete iBkgrdMask;
   615 	}
   616 
   617 CTestAppView::CTestAppView() :
   618 	iIncrement (2, 2),
   619 	iDirection (1, 1)
   620 	{
   621 	}
   622 
   623 RWindow& CTestAppView::TheWindow()
   624 	{
   625 	__ASSERT_ALWAYS(iVideoPane, User::Panic(_L("TheWindow"), 0));	
   626 	return iVideoPane->TheWindow();
   627 	}
   628 
   629 void CTestAppView::ConstructL(const TRect& aWindowedRect)
   630 	{
   631 	iWindowedViewRect = aWindowedRect;	// The rect used when the view is not full-screen
   632 	CreateWindowL();
   633 	EnableWindowTransparency();
   634 	EnableDragEvents();
   635 	Window().SetPointerGrab(ETrue);
   636 
   637 	InitComponentArrayL();
   638 
   639 	iVideoPane = new (ELeave) CVideoPane();
   640 	Components().AppendLC(iVideoPane, KVideoControlID);
   641 	iVideoPane->ConstructL(*this);
   642 	CleanupStack::Pop(iVideoPane);
   643 
   644 	SetRect(iWindowedViewRect);
   645 
   646 	iBkgrdImage = new(ELeave) CFbsBitmap;
   647 	User::LeaveIfError(iBkgrdImage->Load(KVideoAppBitmapsFile, EMbmVideoplayBackground));
   648 
   649 	iBkgrdMask = new(ELeave) CFbsBitmap;
   650 	User::LeaveIfError(iBkgrdMask->Load(KVideoAppBitmapsFile, EMbmVideoplayBackground_mask));
   651 	
   652 	ActivateL();
   653 	}
   654 
   655 void CTestAppView::SizeChanged() 
   656 	{
   657 	const TRect reservedVideoRect = VideoPaneRect();
   658 	const TRect usedVideoRect = VideoPaneRect();
   659 
   660 	if(iVideoPane)
   661 		{
   662 		iVideoPane->BlackoutPane();
   663 		iVideoPane->SetRect(reservedVideoRect);
   664 		if(iCurrentPlayer)
   665 			SetVideoSize(*iCurrentPlayer);
   666 		}
   667 	}
   668 
   669 /**
   670 Sets the size and stretch characteristics of the video.
   671 */
   672 void CTestAppView::SetVideoSize(CVideoPlayerUtility2& aVideo) const
   673 	{
   674 	const TRect videoRect(iVideoPane->Rect());
   675 
   676 	CTUiAndVideoAppUi* myAppUi = dynamic_cast<CTUiAndVideoAppUi *>(iCoeEnv->AppUi());
   677 	
   678 	TRAPD(err, aVideo.SetWindowClipRectL(TheWindow(), videoRect));
   679 	if(err == KErrArgument)
   680 		myAppUi->LogError(KNullDesC, _L("Clip rect not within window!"));
   681 	else if(err == KErrNotReady)
   682 		myAppUi->LogError(KNullDesC, _L("Video not ready!"));
   683 	else if(err)
   684 		User::Panic(_L("SetVideoSize2"), err);
   685 #if 1	
   686 	TRAP(err, aVideo.SetAutoScaleL(TheWindow(), EAutoScaleStretch));
   687 	if(err == KErrNotFound)
   688 		myAppUi->LogError(KNullDesC, _L("Window not added to iLoadingPlayer!"));
   689 	else if(err == KErrNotReady)
   690 		myAppUi->LogError(KNullDesC, _L("Controller not been opened!"));
   691 	else if(err)
   692 		User::Panic(_L("SetVideoSize2"), err);
   693 #endif
   694 	iVideoPane->SetVideoUseRect(VideoPaneRect());
   695 	}
   696 
   697 /**
   698 @return The rect of the video pane, relative to the view.
   699 */ 
   700 TRect CTestAppView::VideoPaneRect() const
   701 	{
   702 	TRect result = Rect();
   703 	
   704 	// Make the video screen rect 10% smaller than the whole app window
   705 	result.Shrink(result.Width()/10, result.Height()/10);
   706 	return result;
   707 	}
   708 
   709 void CTestAppView::HandlePointerEventL(const TPointerEvent& aPointerEvent)
   710 	{
   711 	// call base class to dispatch any possible child events first
   712 	// except we don't want to enter into a loop if this method has alraedy been
   713 	// called from the video child control....
   714 	if (!iVideoPane->IsVideoPanePointerEvent())
   715 		CCoeControl::HandlePointerEventL(aPointerEvent);
   716 	
   717 	if(aPointerEvent.iType == TPointerEvent::EButton1Down)
   718 		{
   719 		// get the position (we may need to get the parent's if event came from video child control)
   720 		iPointerDownPos = iVideoPane->IsVideoPanePointerEvent() ? aPointerEvent.iParentPosition : aPointerEvent.iPosition;
   721 		TTime now;
   722 		now.UniversalTime();
   723 #if 0
   724 		if(now.MicroSecondsFrom(iPointerDownTime) < KTapTimeOut)	// If quick double-tap
   725 			{
   726 			IgnoreEventsUntilNextPointerUp();
   727 			ToggleViewSize();
   728 			}
   729 #endif
   730 		iPointerDownTime.UniversalTime();
   731 		}
   732 	else if(aPointerEvent.iType == TPointerEvent::EDrag)
   733 		{
   734 		// Set new position relative to the amount of movement with pointer down event
   735 		TPoint newPos = (iVideoPane->IsVideoPanePointerEvent() ? aPointerEvent.iParentPosition : aPointerEvent.iPosition) - iPointerDownPos;
   736 		if(newPos != TPoint())
   737 			SetPosition(Position()+newPos);
   738 		}
   739 	}
   740 
   741 TKeyResponse CTestAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
   742 	{
   743 	if(aType == EEventKey)
   744 		{
   745 		switch(aKeyEvent.iCode)
   746 			{
   747 			case EKeyEscape:
   748 			// To avoid linking with eikcore.lib
   749 			//if(iEikonEnv->QueryWinL(_L("Exit?"), _L("Sure you want to exit?")))
   750 				{
   751 				iEikonEnv->EikAppUi()->HandleCommandL(EEikCmdExit); 
   752 				return EKeyWasConsumed;
   753 				}
   754 			case EKeyLeftArrow:
   755 				PlayPrevious();
   756 				return EKeyWasConsumed;
   757 			case EKeyRightArrow:
   758 				PlayNext();
   759 				return EKeyWasConsumed;
   760 			case EKeySpace:
   761 				if (iIsPlaying)
   762 					PauseVideo();
   763 				else
   764 					PlayVideo();
   765 				
   766 				return EKeyWasConsumed;
   767 			case EKeyEnter:
   768 				iRunMode = ETestCmdStill; 
   769 				return EKeyWasConsumed;
   770 			case EKeyTab:
   771 				return EKeyWasConsumed;
   772 			default:
   773 				break;	
   774 			}
   775 		}
   776 		
   777 	return EKeyWasNotConsumed;	
   778 	}
   779 
   780 void CTestAppView::Draw(const TRect& /*aRect*/) const
   781 	{
   782 	CWindowGc& gc = SystemGc();
   783 
   784 	// Fill the window solid
   785 	gc.SetDrawMode(CGraphicsContext::EDrawModePEN);
   786 	gc.SetBrushStyle(CGraphicsContext::ENullBrush);
   787 	gc.DrawBitmapMasked(Rect(), iBkgrdImage, iBkgrdImage->SizeInPixels(), iBkgrdMask, EFalse);
   788 	}
   789 
   790 void CTestAppView::HandleControlEventL(CCoeControl */*aControl*/, TCoeEvent /*aEventType*/)
   791 	{
   792 	return;
   793 	}
   794 
   795 // Enable video
   796 void CTestAppView::EnableVideo(CVideoPlayerUtility2*& aPlayer)
   797 	{
   798 	if(iCurrentPlayer)
   799 		{
   800 		iCurrentPlayer->RemoveDisplayWindow(TheWindow());
   801 		iCurrentPlayer->Close();
   802 		delete iCurrentPlayer;
   803 		}	
   804 	
   805 	iCurrentPlayer = aPlayer;
   806 	aPlayer = NULL;	// Take ownership
   807 	const TSize oldSize = iNativeVideoSize;
   808 	iCurrentPlayer->VideoFrameSizeL(iNativeVideoSize);
   809 	SetVideoSize(*iCurrentPlayer);
   810 	if (oldSize!=iNativeVideoSize)
   811 		SizeChanged();
   812 
   813 #if 0 
   814 	// Was getting a crash at startup on the emulator...not sure why.
   815 	// Not currently a requirement.
   816 #ifdef SYMBIAN_MULTIMEDIA_SUBTITLE_SUPPORT
   817 	if (iCurrentPlayer->SubtitlesAvailable())
   818 		{
   819 		iCurrentPlayer->EnableSubtitlesL();
   820 		iVideoPane->EnableSubtitle(iCurrentPlayer);
   821 		}
   822 #endif		
   823 #endif
   824 	PlayVideo();
   825 	}
   826 
   827 // Disable video
   828 void CTestAppView::DisableVideo()
   829 	{
   830 	iVideoPane->BlackoutPane();
   831 	PauseVideo();	// Stop playing
   832 	
   833 	if(iCurrentPlayer)
   834 		{
   835 		iCurrentPlayer->RemoveDisplayWindow(TheWindow());
   836 		iCurrentPlayer->Close();
   837 		delete iCurrentPlayer;
   838 		}	
   839 	iVideoPane->EnableSubtitle(NULL);
   840 	iCurrentPlayer = NULL;
   841  	iNativeVideoSize = TSize();
   842 	}
   843 
   844 void CTestAppView::SetRunMode (TUint32 aMode)
   845 	{
   846 	iRunMode = aMode;
   847 	}
   848 
   849 void CTestAppView::PlayVideo()
   850 	{
   851 	if(iCurrentPlayer && !iIsPlaying)
   852 		{
   853 		iCurrentPlayer->Play();
   854 		iIsPlaying = ETrue;
   855 		}
   856 	else
   857 		iIsPlaying	= EFalse;	// Can't play if there's no player
   858 		
   859 	UpdateButtonState();
   860 	DrawNow();
   861 	}
   862 	
   863 void CTestAppView::PauseVideo()
   864 	{
   865 	if(iCurrentPlayer && iIsPlaying)
   866 		{
   867 		TRAPD(err, iCurrentPlayer->PauseL());
   868 		if(err)
   869 			iCurrentPlayer->Stop();
   870 		}
   871 	
   872 	iIsPlaying = EFalse;	
   873 	UpdateButtonState();
   874 	DrawNow();
   875 	}
   876 	
   877 void CTestAppView::StopVideo()
   878 	{
   879 	if(iCurrentPlayer && iIsPlaying)
   880 		{
   881 		iCurrentPlayer->Stop();
   882 		iIsPlaying = EFalse;
   883 		}
   884 	else
   885 		iIsPlaying	= ETrue;	// Can't play if there's no player
   886 		
   887 	UpdateButtonState();
   888 	DrawNow();
   889 	}
   890 
   891 void CTestAppView::PlayComplete(TInt /*aError*/)
   892 	{
   893 	iIsPlaying	= EFalse;
   894 	UpdateButtonState();
   895 	DrawNow();
   896 	}
   897 
   898 void CTestAppView::PlayPrevious()
   899 	{
   900 	CTUiAndVideoAppUi* myAppUi = dynamic_cast<CTUiAndVideoAppUi *>(iCoeEnv->AppUi());
   901 	if (myAppUi->PlayPreviousFileL() != KErrNone)
   902 		{
   903 		// To avoid linking with eikcore.lib
   904 		//EikonEnv->InfoMsg(_L("First clip"));
   905 		}
   906 	}
   907 	
   908 void CTestAppView::PlayNext()
   909 	{
   910 	CTUiAndVideoAppUi* myAppUi = dynamic_cast<CTUiAndVideoAppUi*>(iCoeEnv->AppUi());
   911 	if (myAppUi->PlayNextFileL() != KErrNone)
   912 		{
   913 		// To avoid linking with eikcore.lib
   914 		//iEikonEnv->InfoMsg(_L("Last clip"));
   915 		}
   916 	}
   917 
   918 void CTestAppView::UpdateButtonState()
   919 	{
   920 	}
   921 
   922 /**
   923 Calculate and set the position of the video UI. If iAutoPosition is zero then this
   924 method is a no-op. 
   925 */
   926 void CTestAppView::SetPos(TPoint* aPos)
   927 	{
   928 	if (!aPos)
   929 		{
   930 		TRect screenRect = iCoeEnv->ScreenDevice()->SizeInPixels();
   931 		
   932 		TSize currentSize = Size();
   933 		TPoint currentPos = Position();
   934 
   935 		switch (iRunMode)
   936 			{
   937 			case (ETestCmdBounce):
   938 				{
   939 				if ((currentPos.iX + (currentSize.iWidth >> 1)) > screenRect.iBr.iX)
   940 					iDirection.iX = -1;
   941 			
   942 				if ((currentPos.iY + (currentSize.iHeight >> 1)) > screenRect.iBr.iY)
   943 					iDirection.iY = -1;
   944 			
   945 				if ((currentPos.iX + (currentSize.iWidth >> 1)) < 0)
   946 					iDirection.iX = 1;
   947 			
   948 				if ((currentPos.iY + (currentSize.iHeight >> 1)) < 0)
   949 					iDirection.iY = 1;
   950 				
   951 				iVidPos.iX += iIncrement.iX* iDirection.iX;
   952 				iVidPos.iY += iIncrement.iY * iDirection.iY;
   953 				break;
   954 				}
   955 			case (ETestCmdJump):
   956 				{
   957 				iVidPos.iX = Math::Random() % 100;//(screenRect.iBr.iX + (currentSize.iWidth >> 1));
   958 				iVidPos.iY = Math::Random() % 100;//(screenRect.iBr.iY + (currentSize.iHeight >> 1));
   959 				break;
   960 				}
   961 			default:
   962 				{} // Do nothing.			
   963 		
   964 			}
   965 		}
   966 	else
   967 		{
   968 		iVidPos = *aPos;
   969 		}
   970 	
   971 	SetPosition(iVidPos);
   972 	}
   973 
   974 //************************************************************************************************************
   975 //** CVideoPane class - place 'video' window.
   976 //************************************************************************************************************
   977 
   978 /**
   979 This controll is used so that the video is not rendered onto transparency.  
   980 */
   981 CVideoPane::~CVideoPane()
   982 	{
   983 	}
   984 
   985 CVideoPane::CVideoPane()
   986 	{
   987 	}
   988 
   989 void CVideoPane::ConstructL(CCoeControl& aParent)
   990 	{
   991 	CreateWindowL(&aParent);
   992 	EnableDragEvents();
   993 	Window().SetPointerGrab(ETrue);
   994 	}
   995 
   996 void CVideoPane::HandlePointerEventL(const TPointerEvent& aPointerEvent)
   997 	{
   998 	// we need to set this flag to true, which is checked in the parent to avoid loops
   999 	iIsVideoPanePointerEvent = ETrue;
  1000 	TRAPD(err, Parent()->HandlePointerEventL(aPointerEvent));
  1001 	iIsVideoPanePointerEvent = EFalse;
  1002 	User::LeaveIfError(err);
  1003 	}
  1004 
  1005 
  1006 void CVideoPane::BlackoutPane()
  1007 	{
  1008 	iVideoUseRect = TRect();	// reset
  1009 	}
  1010 
  1011 void CVideoPane::SetVideoUseRect(const TRect& aRect)
  1012 	{
  1013 	iVideoUseRect = aRect;
  1014 	iVideoUseRect.Move(-Position());	// Compensate for the fact that the pane is window owning
  1015 	}
  1016 	
  1017 void CVideoPane::SizeChanged() 
  1018 	{
  1019 	CCoeControl::SizeChanged();
  1020 	iVideoUseRect = TRect();	// reset
  1021 	}
  1022 
  1023 TBool CVideoPane::IsVideoPanePointerEvent()
  1024 	{
  1025 	return iIsVideoPanePointerEvent;
  1026 	}
  1027 
  1028 void CVideoPane::EnableSubtitle(CVideoPlayerUtility2* aSubtitlePlayer)
  1029 	{
  1030 	iSubtitlePlayer = aSubtitlePlayer;
  1031 	}
  1032 
  1033 void CVideoPane::Draw(const TRect& aRect) const
  1034 	{
  1035 	(void)aRect;	//to remove warning if SYMBIAN_MULTIMEDIA_SUBTITLE_SUPPORT not defined
  1036 	CWindowGc& gc=SystemGc();
  1037 #ifdef _DEBUG
  1038 	gc.SetBrushColor(KRgbRed);
  1039 #else
  1040 	gc.SetBrushColor(KRgbBlack);
  1041 #endif
  1042 	DrawUtils::ClearBetweenRects(gc, Rect(), iVideoUseRect);
  1043 
  1044 #ifdef SYMBIAN_MULTIMEDIA_SUBTITLE_SUPPORT
  1045 	if (iSubtitlePlayer)
  1046 		iSubtitlePlayer->RedrawSubtitle(Window(), aRect);
  1047 #endif
  1048 	}