os/kernelhwsrv/kerneltest/e32test/multimedia/t_camera_gen.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) 2004-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 the License "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 // e32test\multimedia\t_camera_gen.cpp
    15 // 
    16 //
    17 
    18 #include <e32test.h>
    19 #include <d32camerasc.h>
    20 #include <e32def.h>
    21 #include <e32def_private.h>
    22 #include "t_camera_display.h"
    23 #include "d_mmcsc.h"
    24 
    25 _LIT(KTstLddFileName,"D_MMCSC.LDD");
    26 _LIT(KCamLddFileName,"ECAMERASC.LDD");
    27 
    28 #ifdef __WINSCW__
    29 _LIT(KCamPddFileName,"_TEMPLATE_CAMERASC.PDD");
    30 #else
    31 _LIT(KCamPddFileName,"CAMERASC.PDD");
    32 #endif
    33 
    34 _LIT(KCamFreePddExtension,".*");
    35 
    36 const TInt KUnit0=0;
    37 const TInt KFrameRate=30;		// Run the test at 30fps
    38 
    39 class CCameraHandler;
    40 
    41 RTest test(_L("T_CAMERA_GEN"));
    42 CCameraHandler* camera;
    43 
    44 /**
    45 Wait for a key press, but timeout so automated tests
    46 are unaffected
    47 */
    48 void util_getch_withtimeout(TUint aSecs)
    49 	{
    50 	TRequestStatus keyStat;
    51 	test.Console()->Read(keyStat);
    52 	RTimer timer;
    53 	test(timer.CreateLocal()==KErrNone);
    54 	TRequestStatus timerStat;
    55 	timer.After(timerStat,aSecs*1000000);
    56 	User::WaitForRequest(timerStat,keyStat);
    57 	if(keyStat!=KRequestPending)
    58 		(void)test.Console()->KeyCode();
    59 	timer.Cancel();
    60 	timer.Close();
    61 	test.Console()->ReadCancel();
    62 	User::WaitForAnyRequest();
    63 	}
    64 
    65 // Define the frame number to capture
    66 // For image capture this should be zero.
    67 const TInt64 KFrameNumberToCapture= 50;
    68 
    69 /// Defines camera handler
    70 class CCameraHandler : public CActive
    71 	{
    72 public:
    73 	static CCameraHandler* NewL();
    74 	~CCameraHandler();
    75 	TInt SetConfig(TDevCamCaptureMode aCaptureMode, SDevCamFrameSize aSize,SDevCamPixelFormat aPixelFormat,TBool aCreateChunk,TInt aNumBuffers);
    76 	void GetConfig(TDevCamCaptureMode aCaptureMode);
    77 	TInt GetCaps();
    78 	TInt SetFirstConfig(TUint aOffset);
    79 	TInt Start(TDevCamCaptureMode aCaptureMode);
    80 	TInt Stop();
    81 	void SetCaptureMode(TDevCamCaptureMode aCaptureMode);
    82 private:
    83 	CCameraHandler();
    84 	virtual void RunL();
    85 	virtual void DoCancel();
    86 	TInt Init();
    87 private:
    88 	TCamDisplayHandler iDispHandler[ECamCaptureModeMax];
    89 	RDevCameraSc iCamera;
    90 	RChunk iChunk[ECamCaptureModeMax];
    91 	TInt iFrameCount;
    92 	TDevCamCaptureMode iCaptureMode;
    93 	TInt iCapsSize;
    94 	TAny* iCapsBufPtr;
    95 	TCameraCapsV02* iCameraCaps;
    96 	};
    97 
    98 CCameraHandler::CCameraHandler() : CActive(EPriorityStandard)
    99 //
   100 // Constructor for the camera handler
   101 //
   102 	{
   103 	// Active object priority is set to normal
   104 	}
   105 
   106 CCameraHandler::~CCameraHandler()
   107 //
   108 // Destructor for the camera handler
   109 //
   110 	{
   111 	for (TInt captureMode=0; captureMode < ECamCaptureModeMax; captureMode++)
   112 		iChunk[captureMode].Close();
   113 	if(iCapsBufPtr)
   114 		User::Free(iCapsBufPtr);
   115 	iCamera.Close();
   116 	// Cancel any active request
   117 	CActive::Cancel();
   118 	}
   119 
   120 void CCameraHandler::DoCancel()
   121 	{
   122 
   123 	}
   124 
   125 CCameraHandler* CCameraHandler::NewL()
   126 //
   127 // Create active camera
   128 //
   129 	{
   130 	test.Printf(_L("NewL\r\n"));
   131 	CCameraHandler *cc = new (ELeave) CCameraHandler;
   132 	TInt r=cc->Init();
   133 	if (r!=KErrNone)
   134 		User::Leave(r);
   135 	CActiveScheduler::Add(cc);
   136 	return(cc);
   137 	}
   138 
   139 TInt CCameraHandler::Init()
   140 //
   141 // Initialise hardware
   142 //
   143 	{
   144 	test.Printf(_L("Init\r\n"));
   145 	TInt r;
   146 	for (TInt captureMode=0; captureMode < ECamCaptureModeMax; captureMode++)
   147 		{
   148 		r=iDispHandler[captureMode].Init();
   149 		if (r!=KErrNone)
   150 			return(r);
   151 		}
   152 
   153 	// Open camera driver channel
   154 	r=iCamera.Open(KUnit0);
   155 	return(r);
   156 	}
   157 
   158 void CCameraHandler::SetCaptureMode(TDevCamCaptureMode aCaptureMode)
   159 	{
   160 	iCaptureMode=aCaptureMode;
   161 	}
   162 
   163 TInt CCameraHandler::GetCaps()
   164 	{
   165 	test.Printf(_L("GetCaps\r\n"));
   166 	iCapsSize=iCamera.CapsSize();
   167 	iCapsBufPtr = User::Alloc(iCapsSize);
   168 	TPtr8 capsPtr( (TUint8*)iCapsBufPtr, iCapsSize, iCapsSize );
   169 	TInt r = iCamera.Caps(capsPtr);
   170 	if(r!=KErrNone)
   171 		return r;
   172 	iCameraCaps = (TCameraCapsV02*) capsPtr.Ptr();
   173 
   174 	test.Printf(_L("Number of supported pixel formats:%d\r\n"),iCameraCaps->iNumVideoPixelFormats);
   175 	return r;
   176 	}
   177 
   178 void CCameraHandler::GetConfig(TDevCamCaptureMode aCaptureMode)
   179 	{
   180 	test.Printf(_L("GetConfig\r\n"));
   181 	// Config camera
   182 	TCameraConfigV02Buf configBuf;
   183 	TCameraConfigV02 &config=configBuf();
   184 
   185 	iCamera.GetCamConfig(aCaptureMode, configBuf);
   186 
   187 	test.Printf(_L("Pixel Format:%d\r\n"),config.iPixelFormat);
   188 	test.Printf(_L("Frame Size  :%d\r\n"),config.iFrameSize);
   189 	test.Printf(_L("Frame rate  :%dfps\r\n"),config.iFrameRate);
   190 	}
   191 
   192 TInt CCameraHandler::SetFirstConfig(TUint aOffset)
   193 	{
   194 	test.Printf(_L("SetFirstConfig\r\n"));
   195 	TInt ret;
   196 	TAny* frameSizeCapsBuf=0;
   197 	SDevCamPixelFormat* pixelFormat;
   198 	SDevCamFrameSize* frameSize;
   199 	TPtr8 frameSizeCapsPtr(0,0,0);
   200 	pixelFormat = (SDevCamPixelFormat*) (iCameraCaps + 1);
   201 
   202 	if(camera->iCameraCaps->iNumVideoPixelFormats)
   203 		{
   204 		pixelFormat = pixelFormat + iCameraCaps->iNumImagePixelFormats + iCameraCaps->iNumVideoPixelFormats;
   205 		frameSizeCapsBuf = User::Alloc(pixelFormat->iNumFrameSizes*sizeof(SDevCamFrameSize));
   206 		new (&frameSizeCapsPtr) TPtr8((TUint8*)frameSizeCapsBuf, pixelFormat->iNumFrameSizes*sizeof(SDevCamFrameSize), pixelFormat->iNumFrameSizes*sizeof(SDevCamFrameSize));
   207 		ret=iCamera.FrameSizeCaps(ECamCaptureModeImage, pixelFormat->iPixelFormat, frameSizeCapsPtr);
   208 		test(ret==KErrNone);
   209 		frameSize = (SDevCamFrameSize*) frameSizeCapsPtr.Ptr();
   210 		if((pixelFormat->iNumFrameSizes>aOffset) && aOffset)
   211 			frameSize += aOffset;
   212 		ret=camera->SetConfig(ECamCaptureModeVideo,*frameSize,*pixelFormat,ETrue,3);
   213 		test(ret==KErrNone);
   214 		User::Free(frameSizeCapsBuf);
   215 		return(KErrNone);
   216 		}
   217 	else
   218 		return(KErrNotSupported);
   219 	}
   220 
   221 TInt CCameraHandler::SetConfig(TDevCamCaptureMode aCaptureMode, SDevCamFrameSize aSize,SDevCamPixelFormat aPixelFormat,TBool aCreateChunk,TInt aNumBuffers)
   222 	{
   223 	test.Printf(_L("SetConfig\r\n"));
   224 	TInt r=iDispHandler[aCaptureMode].SetConfig(aSize,aPixelFormat);
   225 	if (r!=KErrNone)
   226 		return(r);
   227 
   228 	// Config camera
   229 	TCameraConfigV02Buf configBuf;
   230 	TCameraConfigV02 &config=configBuf();
   231 	iCamera.GetCamConfig(aCaptureMode, configBuf);		// Load defaults
   232 
   233 	config.iFrameSize=aSize;
   234 	config.iPixelFormat=aPixelFormat;
   235 	config.iFrameRate=KFrameRate;
   236 
   237 	TMmSharedChunkBufConfig bufferConfig;
   238 	TPckg<TMmSharedChunkBufConfig> bufferConfigBuf(bufferConfig);
   239 
   240 	r=iCamera.SetCamConfig(aCaptureMode,configBuf);
   241 	if (r!=KErrNone)
   242 		return(r);
   243 
   244 	if (aCreateChunk)
   245 		{
   246 		r=iCamera.SetBufConfigChunkCreate(aCaptureMode,aNumBuffers,iChunk[aCaptureMode]);
   247 		if (r!=KErrNone)
   248 			return(r);
   249 		}
   250 	else
   251 		{
   252 		// 'aNumBuffers' is ignored here, D_MMCSC.LDD currently uses 2 buffers
   253 
   254 		RMmCreateSc tstDrv;
   255 		r=tstDrv.Open();
   256 		if (r!=KErrNone)
   257 			return(r);
   258 		r=tstDrv.GetChunkHandle(iChunk[aCaptureMode]);	// Get a handle on the shared chunk created by the test driver
   259 		if (r!=KErrNone)
   260 			return(r);
   261 		r=tstDrv.GetBufInfo(bufferConfigBuf);
   262 		if (r!=KErrNone)
   263 			return(r);
   264 		r=iCamera.SetBufConfigChunkOpen(aCaptureMode,bufferConfigBuf,iChunk[aCaptureMode]);
   265 		if (r!=KErrNone)
   266 			return(r);
   267 
   268 		tstDrv.Close();
   269 		}
   270 
   271 	iCamera.GetBufferConfig(aCaptureMode, bufferConfigBuf);
   272 	PrintBufferConf(bufferConfig,test);
   273 
   274 	return(r);
   275 	}
   276 
   277 TInt CCameraHandler::Start(TDevCamCaptureMode aCaptureMode)
   278 //
   279 // Set object active, start getting images from the camera
   280 //
   281 	{
   282 	test.Printf(_L("Start\r\n"));
   283 	// Set object active
   284 	iFrameCount=0;
   285 	SetActive();
   286 	iCamera.SetCaptureMode(aCaptureMode);
   287 	// Add request for a new image
   288 	TInt r=iCamera.Start();
   289 	if (r==KErrNone)
   290 		iCamera.NotifyNewImage(iStatus);
   291 	return(r);
   292 	}
   293 
   294 void CCameraHandler::RunL()
   295 //
   296 // Handles a new request
   297 //
   298 	{
   299 	TInt retId=iStatus.Int();
   300 	TInt retOffset=-1;
   301 	iCamera.BufferIdToOffset(iCaptureMode,retId,retOffset);
   302 	if (retId>=0)
   303 		{
   304 		TUint8* imgBase=iChunk[iCaptureMode].Base()+retOffset;
   305 		TInt r=iDispHandler[iCaptureMode].Process(imgBase);
   306 		
   307 #ifdef __WINSCW__
   308 		test(r==KErrNotSupported);
   309 #else
   310 		test(r==KErrNone);
   311 #endif
   312 		
   313 		// Release the buffer
   314 		test(iCamera.ReleaseBuffer(retId)==KErrNone);
   315 		iFrameCount++;
   316 		}
   317 	else
   318 		test.Printf(_L("Capture error (%d)\r\n"),retId);
   319 
   320 	if (iFrameCount<KFrameNumberToCapture)
   321 		{
   322 		// Add request for a new image
   323 		iCamera.NotifyNewImage(iStatus);
   324 		// re-set active
   325 		SetActive();
   326 		}
   327 
   328 	else
   329 		{
   330 		CActiveScheduler::Stop();
   331 		}
   332 	}
   333 
   334 TInt CCameraHandler::Stop()
   335 //
   336 // Stops camera
   337 //
   338 	{
   339 	test.Printf(_L("Stop\r\n"));
   340 	CActive::Cancel();
   341 	TInt r=iCamera.Stop();
   342 	return(r);
   343 	}
   344 
   345 //
   346 // Test for recording a certain number of frames in a particular configuration and displaying
   347 // the results.
   348 //
   349 void TestRecording()
   350 	{
   351 	TInt ret;
   352 
   353 	camera->GetConfig(ECamCaptureModeVideo);
   354 	camera->SetCaptureMode(ECamCaptureModeVideo);
   355 	test.Next(_L("Starting camera"));
   356 	ret=camera->Start(ECamCaptureModeVideo);
   357 	test.Printf(_L("Start returned %d\r\n"),ret);
   358 	test(ret==KErrNone);
   359 
   360 	// Calculate frame rate.
   361 	// We store cuurent time before receiving data from the camera and
   362 	// after to have received KFrameNumberToCapture pictures, we calculate
   363 	// time elapsed during the reception.
   364 	TTimeIntervalMicroSeconds microseconds ;
   365 	TTime now;
   366 	TTime iTime;
   367 	now.HomeTime();
   368 
   369 	test.Next(_L("Starting active scheduler"));
   370 	// Start active scheduler
   371 	CActiveScheduler::Start();
   372 
   373 	// We have received all pictures, so we store the new current time
   374 	iTime.HomeTime();
   375 
   376 	// We keep this time in microseconds to be more precise
   377 	microseconds = iTime.MicroSecondsFrom(now) ;
   378 
   379 	TInt64 timeElapsed = microseconds.Int64();
   380 
   381 	// We store in this variable, integer value of the frame rate
   382 	TInt64 intFrameRate = (TInt64)((KFrameNumberToCapture *1000000)/timeElapsed);
   383 
   384 	// We store in this variable, decimal value of the frame rate
   385 	TInt64 milliFrameRate = (TInt64)((KFrameNumberToCapture *1000000)%timeElapsed);
   386 	milliFrameRate = (milliFrameRate*1000) / timeElapsed;
   387 
   388 	test.Printf(_L("   Frame rate for frames received : %d.%03d frames per second\r\n"), static_cast<TInt>(intFrameRate), static_cast<TInt>(milliFrameRate));
   389 	test.Printf(_L("   Frame rate expected            : %d.000 frames per second\r\n"),KFrameRate);
   390 	test.Next(_L("Stopping camera"));
   391 	// Stop Camera
   392 	ret=camera->Stop();
   393 	test(ret==KErrNone);
   394 	}
   395 
   396 //
   397 // Test program main part
   398 //
   399 TInt E32Main()
   400 	{
   401 	__UHEAP_MARK;
   402 
   403 	test.Title();
   404 	test.Start(_L("Camera module GEN test"));
   405 
   406 	test.Next(_L("Loading CAMERA PDD"));
   407 	TInt ret=User::LoadPhysicalDevice(KCamPddFileName);
   408 	test.Printf(_L("Returned %d\r\n"),ret);
   409 
   410 	if (ret==KErrNotFound)
   411 		{
   412 		test.Printf(_L("Shared chunk camera driver not supported - test skipped\r\n"));
   413 		test.End();
   414 		test.Close();
   415 		__UHEAP_MARKEND;
   416 		return(KErrNone);
   417 		}
   418 
   419 	test(ret==KErrNone || ret==KErrAlreadyExists);
   420 
   421 	test.Next(_L("Loading CAMERA LDD"));
   422 	ret=User::LoadLogicalDevice(KCamLddFileName);
   423 	test.Printf(_L("Returned %d\r\n"),ret);
   424 	test(ret==KErrNone || ret==KErrAlreadyExists);
   425 
   426 	test.Next(_L("Loading D_MMCSC LDD"));
   427 	ret=User::LoadLogicalDevice(KTstLddFileName);
   428 	test.Printf(_L("Returned %d\r\n"),ret);
   429 	test(ret==KErrNone||ret==KErrAlreadyExists);
   430 
   431 	__KHEAP_MARK;
   432 
   433 	// Construct and install the active scheduler
   434 	test.Next(_L("Initialising active scheduler"));
   435 	CActiveScheduler *exampleScheduler = new (ELeave) CActiveScheduler();
   436 	// Install as the active scheduler
   437 	CActiveScheduler::Install(exampleScheduler);
   438 
   439 	// Create camera handler
   440 	test.Next(_L("Creating camera handler"));
   441 	camera = CCameraHandler::NewL();
   442 
   443 	test.Next(_L("+ Getting Camera Capabilities"));
   444 	ret=camera->GetCaps();
   445 	test(ret==KErrNone);
   446 
   447 	// SetConfig
   448 	test.Next(_L("Setting Camera Configuration"));
   449 	ret=camera->SetFirstConfig(0);
   450 	test(ret==KErrNone);
   451 	TestRecording();
   452 
   453 	// Repeat with a different configuration
   454 	test.Next(_L("Resetting Camera Configuration"));
   455 	ret=camera->SetFirstConfig(1);
   456 	test(ret==KErrNone);
   457 	TestRecording();
   458 
   459 	delete camera;
   460 	delete exampleScheduler;
   461 
   462 	__KHEAP_MARKEND;
   463 
   464 	// Free the PDDs and LDDs
   465 	test.Next(_L("Freeing ECAMERASC LDD"));
   466 	ret=User::FreeLogicalDevice(KDevCameraScName);
   467 	test(ret==KErrNone);
   468 
   469 	test.Next(_L("Freeing CAMERASC PDD")) ;
   470 	TFindPhysicalDevice fDr;
   471 	TFullName drivName;
   472 	TName searchName;
   473 	searchName.Append(KDevCameraScName);
   474 	searchName.Append(KCamFreePddExtension);
   475 	fDr.Find(searchName);
   476 	ret=fDr.Next(drivName);
   477 	test(ret==KErrNone);
   478 	ret=User::FreePhysicalDevice(drivName);
   479 	test(ret==KErrNone);
   480 
   481 	test.Next(_L("Freeing D_MMCSC LDD"));
   482 	ret=User::FreeLogicalDevice(KDevMmCScName);
   483 	test(ret==KErrNone);
   484 
   485 	test.Printf(_L("Hit any key to continue\r\n"));
   486 	util_getch_withtimeout(5);
   487 
   488 	test.End();
   489 	test.Close();
   490 
   491 	__UHEAP_MARKEND;
   492 
   493 	return KErrNone;
   494 	}