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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\multimedia\t_camera_gen.cpp
19 #include <d32camerasc.h>
21 #include <e32def_private.h>
22 #include "t_camera_display.h"
25 _LIT(KTstLddFileName,"D_MMCSC.LDD");
26 _LIT(KCamLddFileName,"ECAMERASC.LDD");
29 _LIT(KCamPddFileName,"_TEMPLATE_CAMERASC.PDD");
31 _LIT(KCamPddFileName,"CAMERASC.PDD");
34 _LIT(KCamFreePddExtension,".*");
37 const TInt KFrameRate=30; // Run the test at 30fps
41 RTest test(_L("T_CAMERA_GEN"));
42 CCameraHandler* camera;
45 Wait for a key press, but timeout so automated tests
48 void util_getch_withtimeout(TUint aSecs)
50 TRequestStatus keyStat;
51 test.Console()->Read(keyStat);
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();
61 test.Console()->ReadCancel();
62 User::WaitForAnyRequest();
65 // Define the frame number to capture
66 // For image capture this should be zero.
67 const TInt64 KFrameNumberToCapture= 50;
69 /// Defines camera handler
70 class CCameraHandler : public CActive
73 static CCameraHandler* NewL();
75 TInt SetConfig(TDevCamCaptureMode aCaptureMode, SDevCamFrameSize aSize,SDevCamPixelFormat aPixelFormat,TBool aCreateChunk,TInt aNumBuffers);
76 void GetConfig(TDevCamCaptureMode aCaptureMode);
78 TInt SetFirstConfig(TUint aOffset);
79 TInt Start(TDevCamCaptureMode aCaptureMode);
81 void SetCaptureMode(TDevCamCaptureMode aCaptureMode);
85 virtual void DoCancel();
88 TCamDisplayHandler iDispHandler[ECamCaptureModeMax];
90 RChunk iChunk[ECamCaptureModeMax];
92 TDevCamCaptureMode iCaptureMode;
95 TCameraCapsV02* iCameraCaps;
98 CCameraHandler::CCameraHandler() : CActive(EPriorityStandard)
100 // Constructor for the camera handler
103 // Active object priority is set to normal
106 CCameraHandler::~CCameraHandler()
108 // Destructor for the camera handler
111 for (TInt captureMode=0; captureMode < ECamCaptureModeMax; captureMode++)
112 iChunk[captureMode].Close();
114 User::Free(iCapsBufPtr);
116 // Cancel any active request
120 void CCameraHandler::DoCancel()
125 CCameraHandler* CCameraHandler::NewL()
127 // Create active camera
130 test.Printf(_L("NewL\r\n"));
131 CCameraHandler *cc = new (ELeave) CCameraHandler;
135 CActiveScheduler::Add(cc);
139 TInt CCameraHandler::Init()
141 // Initialise hardware
144 test.Printf(_L("Init\r\n"));
146 for (TInt captureMode=0; captureMode < ECamCaptureModeMax; captureMode++)
148 r=iDispHandler[captureMode].Init();
153 // Open camera driver channel
154 r=iCamera.Open(KUnit0);
158 void CCameraHandler::SetCaptureMode(TDevCamCaptureMode aCaptureMode)
160 iCaptureMode=aCaptureMode;
163 TInt CCameraHandler::GetCaps()
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);
172 iCameraCaps = (TCameraCapsV02*) capsPtr.Ptr();
174 test.Printf(_L("Number of supported pixel formats:%d\r\n"),iCameraCaps->iNumVideoPixelFormats);
178 void CCameraHandler::GetConfig(TDevCamCaptureMode aCaptureMode)
180 test.Printf(_L("GetConfig\r\n"));
182 TCameraConfigV02Buf configBuf;
183 TCameraConfigV02 &config=configBuf();
185 iCamera.GetCamConfig(aCaptureMode, configBuf);
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);
192 TInt CCameraHandler::SetFirstConfig(TUint aOffset)
194 test.Printf(_L("SetFirstConfig\r\n"));
196 TAny* frameSizeCapsBuf=0;
197 SDevCamPixelFormat* pixelFormat;
198 SDevCamFrameSize* frameSize;
199 TPtr8 frameSizeCapsPtr(0,0,0);
200 pixelFormat = (SDevCamPixelFormat*) (iCameraCaps + 1);
202 if(camera->iCameraCaps->iNumVideoPixelFormats)
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);
209 frameSize = (SDevCamFrameSize*) frameSizeCapsPtr.Ptr();
210 if((pixelFormat->iNumFrameSizes>aOffset) && aOffset)
211 frameSize += aOffset;
212 ret=camera->SetConfig(ECamCaptureModeVideo,*frameSize,*pixelFormat,ETrue,3);
214 User::Free(frameSizeCapsBuf);
218 return(KErrNotSupported);
221 TInt CCameraHandler::SetConfig(TDevCamCaptureMode aCaptureMode, SDevCamFrameSize aSize,SDevCamPixelFormat aPixelFormat,TBool aCreateChunk,TInt aNumBuffers)
223 test.Printf(_L("SetConfig\r\n"));
224 TInt r=iDispHandler[aCaptureMode].SetConfig(aSize,aPixelFormat);
229 TCameraConfigV02Buf configBuf;
230 TCameraConfigV02 &config=configBuf();
231 iCamera.GetCamConfig(aCaptureMode, configBuf); // Load defaults
233 config.iFrameSize=aSize;
234 config.iPixelFormat=aPixelFormat;
235 config.iFrameRate=KFrameRate;
237 TMmSharedChunkBufConfig bufferConfig;
238 TPckg<TMmSharedChunkBufConfig> bufferConfigBuf(bufferConfig);
240 r=iCamera.SetCamConfig(aCaptureMode,configBuf);
246 r=iCamera.SetBufConfigChunkCreate(aCaptureMode,aNumBuffers,iChunk[aCaptureMode]);
252 // 'aNumBuffers' is ignored here, D_MMCSC.LDD currently uses 2 buffers
258 r=tstDrv.GetChunkHandle(iChunk[aCaptureMode]); // Get a handle on the shared chunk created by the test driver
261 r=tstDrv.GetBufInfo(bufferConfigBuf);
264 r=iCamera.SetBufConfigChunkOpen(aCaptureMode,bufferConfigBuf,iChunk[aCaptureMode]);
271 iCamera.GetBufferConfig(aCaptureMode, bufferConfigBuf);
272 PrintBufferConf(bufferConfig,test);
277 TInt CCameraHandler::Start(TDevCamCaptureMode aCaptureMode)
279 // Set object active, start getting images from the camera
282 test.Printf(_L("Start\r\n"));
286 iCamera.SetCaptureMode(aCaptureMode);
287 // Add request for a new image
288 TInt r=iCamera.Start();
290 iCamera.NotifyNewImage(iStatus);
294 void CCameraHandler::RunL()
296 // Handles a new request
299 TInt retId=iStatus.Int();
301 iCamera.BufferIdToOffset(iCaptureMode,retId,retOffset);
304 TUint8* imgBase=iChunk[iCaptureMode].Base()+retOffset;
305 TInt r=iDispHandler[iCaptureMode].Process(imgBase);
308 test(r==KErrNotSupported);
313 // Release the buffer
314 test(iCamera.ReleaseBuffer(retId)==KErrNone);
318 test.Printf(_L("Capture error (%d)\r\n"),retId);
320 if (iFrameCount<KFrameNumberToCapture)
322 // Add request for a new image
323 iCamera.NotifyNewImage(iStatus);
330 CActiveScheduler::Stop();
334 TInt CCameraHandler::Stop()
339 test.Printf(_L("Stop\r\n"));
341 TInt r=iCamera.Stop();
346 // Test for recording a certain number of frames in a particular configuration and displaying
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);
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 ;
369 test.Next(_L("Starting active scheduler"));
370 // Start active scheduler
371 CActiveScheduler::Start();
373 // We have received all pictures, so we store the new current time
376 // We keep this time in microseconds to be more precise
377 microseconds = iTime.MicroSecondsFrom(now) ;
379 TInt64 timeElapsed = microseconds.Int64();
381 // We store in this variable, integer value of the frame rate
382 TInt64 intFrameRate = (TInt64)((KFrameNumberToCapture *1000000)/timeElapsed);
384 // We store in this variable, decimal value of the frame rate
385 TInt64 milliFrameRate = (TInt64)((KFrameNumberToCapture *1000000)%timeElapsed);
386 milliFrameRate = (milliFrameRate*1000) / timeElapsed;
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"));
397 // Test program main part
404 test.Start(_L("Camera module GEN test"));
406 test.Next(_L("Loading CAMERA PDD"));
407 TInt ret=User::LoadPhysicalDevice(KCamPddFileName);
408 test.Printf(_L("Returned %d\r\n"),ret);
410 if (ret==KErrNotFound)
412 test.Printf(_L("Shared chunk camera driver not supported - test skipped\r\n"));
419 test(ret==KErrNone || ret==KErrAlreadyExists);
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);
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);
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);
439 // Create camera handler
440 test.Next(_L("Creating camera handler"));
441 camera = CCameraHandler::NewL();
443 test.Next(_L("+ Getting Camera Capabilities"));
444 ret=camera->GetCaps();
448 test.Next(_L("Setting Camera Configuration"));
449 ret=camera->SetFirstConfig(0);
453 // Repeat with a different configuration
454 test.Next(_L("Resetting Camera Configuration"));
455 ret=camera->SetFirstConfig(1);
460 delete exampleScheduler;
464 // Free the PDDs and LDDs
465 test.Next(_L("Freeing ECAMERASC LDD"));
466 ret=User::FreeLogicalDevice(KDevCameraScName);
469 test.Next(_L("Freeing CAMERASC PDD")) ;
470 TFindPhysicalDevice fDr;
473 searchName.Append(KDevCameraScName);
474 searchName.Append(KCamFreePddExtension);
475 fDr.Find(searchName);
476 ret=fDr.Next(drivName);
478 ret=User::FreePhysicalDevice(drivName);
481 test.Next(_L("Freeing D_MMCSC LDD"));
482 ret=User::FreeLogicalDevice(KDevMmCScName);
485 test.Printf(_L("Hit any key to continue\r\n"));
486 util_getch_withtimeout(5);