os/mm/mmdevicefw/mdfunittest/codecapi/video/src/videoplayfile.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include <mmf/devvideo/videoplayhwdevice.h>
    17 #include <mmf/server/mmfdatabuffer.h>
    18 #include "videoplayfile.h"
    19 
    20 #include "../../PU/video/src/Plugin/VideoTestDecoderPU/videotestdecoderpu.hrh"
    21 #include "../../PU/video/src/Plugin/VideoTestEncoderPU/videotestencoderpu.hrh"
    22 
    23 const TInt KInputBufferSize = 8192;
    24 const TInt KFilePositionZero = 0;
    25 const TInt KTBufSize = 256;
    26 const TInt KTRegionFixSize = 1;
    27 
    28 // **************************************************
    29 // instructs the Hw Device Adapter to play a file	
    30 // **************************************************
    31 CPlayVideoFile::CPlayVideoFile(RTestStepVideoCodecs* aParent) : 
    32 	CActive(EPriorityNormal),
    33 	iState(EHwDeviceInit),
    34 	iParent(aParent)
    35 	{
    36 	}
    37 
    38 void  CPlayVideoFile::ConstructL()
    39 	{
    40 	CActiveScheduler::Add(this);
    41 	User::LeaveIfError(RFbsSession::Connect());
    42 
    43 	TInt err = KErrNone;
    44 	TRAP(err, iScreenDevice = CFbsScreenDevice::NewL(_L(""),EColor16MA));
    45 	if (err == KErrNotSupported)
    46 		{
    47 		TRAP(err, iScreenDevice = CFbsScreenDevice::NewL(_L(""),EColor16M));
    48 		}
    49 	if (err == KErrNotSupported)
    50 		{
    51 		TRAP(err, iScreenDevice = CFbsScreenDevice::NewL(_L(""),EColor64K));
    52 		}
    53 	if (err == KErrNotSupported)
    54 		{
    55 		TRAP(err, iScreenDevice = CFbsScreenDevice::NewL(_L(""),EColor4K));
    56 		}
    57 	if (err == KErrNotSupported)
    58 		{
    59 		TRAP(err, iScreenDevice = CFbsScreenDevice::NewL(_L(""),EColor256));
    60 		}
    61 	if (err == KErrNotSupported)
    62 		{
    63 		iScreenDevice = CFbsScreenDevice::NewL(_L(""),EColor16MAP);
    64 		}		
    65 	else
    66 		{
    67 		User::LeaveIfError(err);		
    68 		}
    69 	}
    70 
    71 CPlayVideoFile* CPlayVideoFile::NewL(RTestStepVideoCodecs* aParent)
    72 	{
    73 	CPlayVideoFile* self = new (ELeave) CPlayVideoFile(aParent);
    74 	CleanupStack::PushL(self);
    75 	self->ConstructL();
    76 	CleanupStack::Pop(self);
    77 	return self;
    78 	}
    79 	
    80 	
    81 void CPlayVideoFile::DoCancel()
    82 	{
    83 	}
    84 
    85 CPlayVideoFile::~CPlayVideoFile()
    86 	{
    87 	iFile.Close();
    88 	iFs.Close();
    89 	
    90 	delete iVideoHwDevice;
    91 	delete iScreenDevice;
    92 	RFbsSession::Disconnect();
    93 	}
    94 	
    95 	
    96 void CPlayVideoFile::LoadCodecL()	
    97 	{
    98 	iParent->InfoMessage(_L("Loading Hw Device Adapter and PU Codec"));
    99 
   100 	RImplInfoPtrArray array;
   101 	REComSession::ListImplementationsL(TUid::Uid(KUidMdfProcessingUnit), array);
   102 	TBool found = EFalse;
   103 	CImplementationInformation* info = NULL;
   104 	for (TInt i=0;i<array.Count() && !found;i++)
   105 		{
   106 		info = array[i];
   107 		if (info->ImplementationUid() == TUid::Uid(KUidVideoTestDecoderPu))
   108 			{
   109 			found = ETrue;
   110 			}
   111 		}
   112 	TInt err = KErrNotFound;
   113 	if (found)
   114 		{
   115 		TRAP(err, iVideoHwDevice = CMMFVideoDecodeHwDevice::NewPuAdapterL(*info,*this));
   116 		}
   117 	else
   118 		{
   119 		err = KErrNotFound;
   120 		}
   121 	array.ResetAndDestroy();
   122 	User::LeaveIfError(err);
   123 
   124 	}
   125 	
   126 void CPlayVideoFile::SetDSA(TBool aUseDSA)	
   127 	{
   128 	// Need new version of this to load from a PU based uid
   129 	iUseDSA = aUseDSA;
   130 	}
   131 	
   132 void CPlayVideoFile::StartPlaybackL()	
   133 	{
   134 	OpenTestFileL();
   135 	SetState(EHwDeviceInit);
   136 	CActiveScheduler::Start();
   137 	}
   138 
   139 void CPlayVideoFile::SetState(TPlayVideoFileState aState)
   140 	{
   141 	iState = aState;
   142 	SetActive();	
   143 	TRequestStatus* status = &iStatus;
   144 	User::RequestComplete(status, KErrNone);	
   145 	}
   146 
   147 void CPlayVideoFile::OpenTestFileL()
   148 	{
   149 	TBuf<KTBufSize> buf;
   150 	buf.Format(_L("Opening test input file %S"), &KTestPlaybackFile);
   151 	iParent->InfoMessage(buf);
   152 	User::LeaveIfError(iFs.Connect());
   153 	User::LeaveIfError(iFile.Open(iFs, KTestPlaybackFile, EFileRead));
   154 	}
   155 
   156 void CPlayVideoFile::RunL()
   157 	{
   158 	switch (iState)
   159 		{
   160 		case EHwDeviceInit:
   161 			{
   162 			iParent->InfoMessage(_L("State: EHwDeviceInit"));
   163 			TRAPD(err, InitializeL());
   164 			if (err != KErrNone)
   165 				{
   166 				CleanupAndSetDeviceError(_L("Cannot intialize HwDevice"));
   167 				break;
   168 				}
   169 			break;
   170 			}
   171 		case EHwDeviceStartDecode:
   172 			{
   173 			// if we are playing a file, first we have to open the file
   174 			iParent->InfoMessage(_L("State: EHwDeviceStartDecode"));
   175 			TRAPD(err, StartDecodeL());
   176 			if (err != KErrNone)
   177 				{
   178 				CleanupAndSetDeviceError(_L("Cannot start decoding"));
   179 				break;
   180 				}
   181 			break;
   182 			}
   183 		case EHwDeviceAllowToComplete:
   184 			iParent->InfoMessage(_L("State: EHwDeviceAllowToComplete"));
   185 			break;
   186 		case EHwDeviceDone:
   187 			{
   188 			iParent->InfoMessage(_L("State: EHwDeviceDeviceDone"));
   189 			Cancel();
   190 			CActiveScheduler::Stop();
   191 			break;
   192 			}				
   193 		case EHwDeviceError:
   194 			{
   195 			TBuf<KTBufSize> buf;
   196 			buf.Format(_L("State: EHwDeviceDeviceError %d"), iError);
   197 			iParent->SetVerdict(buf, EFail);
   198 			
   199 			CActiveScheduler::Stop();
   200 			break;
   201 			}
   202 		default:
   203 			{
   204 			CleanupAndSetDeviceError(_L("Unknown CPlayVideoFile iState"));
   205 			break;
   206 			}
   207 		}
   208 	}
   209 	
   210 	
   211 void CPlayVideoFile::InitializeL()
   212 	{
   213 	iParent->InfoMessage(_L("InitializeL()"));
   214 
   215 	TUncompressedVideoFormat reqFormat;
   216 	reqFormat.iDataFormat = ERgbFbsBitmap;
   217 	reqFormat.iRgbFormat = EFbsBitmapColor16M;
   218 	RArray<TUncompressedVideoFormat> decodeFormats;
   219 	iVideoHwDevice->GetOutputFormatListL(decodeFormats);
   220 	CleanupClosePushL(decodeFormats);
   221 	User::LeaveIfError(decodeFormats.Find(reqFormat));
   222 
   223 	iParent->InfoMessage(_L("Call VideoHwDevice->SetOutputFormat()"));
   224 	iVideoHwDevice->SetOutputFormatL(reqFormat);
   225 	CleanupStack::PopAndDestroy(&decodeFormats);	
   226 	
   227 	CMMFDescriptorBuffer* buffer = CMMFDescriptorBuffer::NewL(KInputBufferSize);
   228 	CleanupStack::PushL(buffer);
   229 	TVideoInputBuffer inputBuffer;
   230 	
   231 	TDes8& des = buffer->Data();
   232 	// read header data from file
   233 	User::LeaveIfError(iFile.Read(des));
   234 	TInt pos = KFilePositionZero;
   235 	// seek back to start
   236 	User::LeaveIfError(iFile.Seek(ESeekStart, pos));
   237 
   238 	TBuf<KTBufSize> buf;
   239 	buf.Format(_L("Read header of size %d"),inputBuffer.iData.Length());
   240 	iParent->InfoMessage(buf);
   241 	
   242 	if (des.Length()>0)
   243 		{
   244 		// Set the pointer
   245 		inputBuffer.iData.Set(&des[0],des.Length(),des.MaxLength());
   246 		
   247 		iParent->InfoMessage(_L("Call VideoHwDevice GetHeaderInformationL()"));
   248 		TVideoPictureHeader* header = iVideoHwDevice->GetHeaderInformationL(EDuArbitraryStreamSection, 
   249 												EDuElementaryStream,
   250 												&inputBuffer);
   251 												
   252 		iFrameSize = header->iSizeInMemory;
   253 		iVideoHwDevice->ReturnHeader(header);
   254 		}
   255 	else
   256 		{
   257 		// Cannot read any data from file, so no point in continuing
   258 		User::Leave(KErrCorrupt);
   259 		}
   260 		
   261 	CleanupStack::PopAndDestroy(buffer);	
   262 	
   263 	iParent->InfoMessage(_L("Call VideoHwDevice SetVideoDestScreenL()"));
   264 	iVideoHwDevice->SetVideoDestScreenL(iUseDSA);
   265 	
   266 	iParent->InfoMessage(_L("Call VideoHwDevice->Initialize()"));
   267 	iVideoHwDevice->Initialize();
   268 	}
   269 	
   270 	
   271 void CPlayVideoFile::StartDecodeL()
   272 	{
   273 	iParent->InfoMessage(_L("StartDecodeL()"));
   274 
   275 	if (iUseDSA)
   276 		{
   277 		StartDirectScreenAccessL();
   278 		}
   279 	// tell the HwDeviceAdapter to play the file
   280 	iParent->InfoMessage(_L("Call VideoHwDevice->Start()"));
   281 	iVideoHwDevice->Start();
   282 	}
   283 	
   284 	
   285 void CPlayVideoFile::ReadNextBufferL()
   286 	{
   287 	// check that we aren't waiting for the input buffer to be returned
   288 	iParent->InfoMessage(_L("ReadNextBufferL()"));
   289 	TVideoInputBuffer* inputBuffer = iVideoHwDevice->GetBufferL(KInputBufferSize);	
   290 	if (inputBuffer)
   291 		{
   292 		TBuf<KTBufSize> buf;
   293 		buf.Format(_L("inputBuffer size %d"),inputBuffer->iData.Length());
   294 		iParent->InfoMessage(buf);
   295 		User::LeaveIfError(iFile.Read(inputBuffer->iData));
   296 		buf.Format(_L("Read from file %d bytes"),inputBuffer->iData.Length());
   297 		iParent->InfoMessage(buf);
   298 		if (inputBuffer->iData.Length()>0)
   299 			{
   300 			iParent->InfoMessage(_L("Call HwDevice->WriteCodedData()"));
   301 			iVideoHwDevice->WriteCodedDataL(inputBuffer);
   302 			}
   303 		else
   304 			{
   305 			iParent->InfoMessage(_L("End of input stream"));
   306 			iParent->InfoMessage(_L("Call HwDevice->InputEnd()"));
   307 			iVideoHwDevice->InputEnd();
   308 			}
   309 		}
   310 	}
   311 		
   312 void CPlayVideoFile::CleanupAndSetDeviceError(TPtrC16 aText)
   313 	{
   314 	iParent->SetVerdict(aText, EFail);
   315 	delete iVideoHwDevice;
   316 	SetState(EHwDeviceError);
   317 	}
   318 	
   319 void CPlayVideoFile::MdvppNewPicture(TVideoPicture* aPicture)
   320 	{
   321 	iParent->InfoMessage(_L("MdvppNewPicture - Received Frame"));
   322 	iVideoHwDevice->ReturnPicture(aPicture);
   323 	iParent->InfoMessage(_L("MdvppNewPicture - finished returning Frame"));
   324 	}
   325 
   326 void CPlayVideoFile::MdvppNewBuffers()
   327 	{
   328 	iParent->InfoMessage(_L("MdvppNewBuffers()"));
   329 	TRAP(iError, ReadNextBufferL());
   330 	if (iError != KErrNone)
   331 		{
   332 		iParent->SetVerdict(_L("Error reading next buffer"),EFail);		
   333 		iVideoHwDevice->Stop();
   334 		SetState(EHwDeviceError);
   335 		}
   336 	}
   337 
   338 void CPlayVideoFile::MdvppReturnPicture(TVideoPicture* /*aPicture*/)
   339 	{
   340 	iParent->InfoMessage(_L("MdvppReturnPicture()"));
   341 	}
   342 	
   343 void CPlayVideoFile::MdvppSupplementalInformation(const TDesC8& /*aData*/, 
   344 	const TTimeIntervalMicroSeconds& /*aTimestamp*/, const TPictureId& /*aPictureId*/)
   345 	{
   346 	}
   347 	
   348 void CPlayVideoFile::MdvppPictureLoss()
   349 	{
   350 	iParent->InfoMessage(_L("MdvppPictureLoss()"));
   351 	}
   352 	
   353 void CPlayVideoFile::MdvppPictureLoss(const TArray<TPictureId>& /*aPictures*/)
   354 	{
   355 	iParent->InfoMessage(_L("MdvppPictureLoss()"));
   356 	}
   357 	
   358 void CPlayVideoFile::MdvppSliceLoss(TUint /*aFirstMacroblock*/, TUint /*aNumMacroblocks*/, const TPictureId& /*aPicture*/)
   359 	{
   360 	iParent->InfoMessage(_L("MdvppSliceLoss()"));
   361 	}
   362 	
   363 void CPlayVideoFile::MdvppReferencePictureSelection(const TDesC8& /*aSelectionData*/)
   364 	{
   365 	}
   366 	
   367 void CPlayVideoFile::MdvppTimedSnapshotComplete(TInt /*aError*/, TPictureData* /*aPictureData*/, 
   368 	const TTimeIntervalMicroSeconds& /*aPresentationTimestamp*/, const TPictureId& /*aPictureId*/)
   369 	{
   370 	}
   371 	
   372 void CPlayVideoFile::MdvppFatalError(CMMFVideoHwDevice* /*aDevice*/, TInt aError)
   373 	{
   374 	iError = aError;
   375 	iParent->InfoMessage(_L("MdvppFatalError()"));
   376 	SetState(EHwDeviceError);
   377 	}
   378 	
   379 void CPlayVideoFile::MdvppInitializeComplete(CMMFVideoHwDevice* /*aDevice*/, TInt aError)
   380 	{
   381 	iParent->InfoMessage(_L("MdvppInitializeComplete()"));
   382 	iError = aError;
   383 	if (iError == KErrNone)
   384 		{
   385 		iParent->InfoMessage(_L("The Hw Device Adapter initialised correctly"));
   386 		SetState(EHwDeviceStartDecode);
   387 		}
   388 	else
   389 		{
   390 		iParent->InfoMessage(_L("Failure intialising the hw device adapter"));
   391 		SetState(EHwDeviceError);
   392 		}
   393 	}
   394 	
   395 void CPlayVideoFile::MdvppStreamEnd()
   396 	{
   397 	iParent->InfoMessage(_L("MdvppStreamEnd()"));
   398 	SetState(EHwDeviceDone);
   399 	}
   400 
   401 void CPlayVideoFile::StartDirectScreenAccessL()
   402 	{
   403 	TRegionFix<KTRegionFixSize> reg;
   404 	TRect pos(iFrameSize);
   405 	reg.AddRect(pos);
   406 	
   407 	iVideoHwDevice->StartDirectScreenAccessL(pos, *iScreenDevice, reg);
   408 	}