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