sl@0: // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32test\multimedia\t_camera_gen.cpp sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "t_camera_display.h" sl@0: #include "d_mmcsc.h" sl@0: sl@0: _LIT(KTstLddFileName,"D_MMCSC.LDD"); sl@0: _LIT(KCamLddFileName,"ECAMERASC.LDD"); sl@0: sl@0: #ifdef __WINSCW__ sl@0: _LIT(KCamPddFileName,"_TEMPLATE_CAMERASC.PDD"); sl@0: #else sl@0: _LIT(KCamPddFileName,"CAMERASC.PDD"); sl@0: #endif sl@0: sl@0: _LIT(KCamFreePddExtension,".*"); sl@0: sl@0: const TInt KUnit0=0; sl@0: const TInt KFrameRate=30; // Run the test at 30fps sl@0: sl@0: class CCameraHandler; sl@0: sl@0: RTest test(_L("T_CAMERA_GEN")); sl@0: CCameraHandler* camera; sl@0: sl@0: /** sl@0: Wait for a key press, but timeout so automated tests sl@0: are unaffected sl@0: */ sl@0: void util_getch_withtimeout(TUint aSecs) sl@0: { sl@0: TRequestStatus keyStat; sl@0: test.Console()->Read(keyStat); sl@0: RTimer timer; sl@0: test(timer.CreateLocal()==KErrNone); sl@0: TRequestStatus timerStat; sl@0: timer.After(timerStat,aSecs*1000000); sl@0: User::WaitForRequest(timerStat,keyStat); sl@0: if(keyStat!=KRequestPending) sl@0: (void)test.Console()->KeyCode(); sl@0: timer.Cancel(); sl@0: timer.Close(); sl@0: test.Console()->ReadCancel(); sl@0: User::WaitForAnyRequest(); sl@0: } sl@0: sl@0: // Define the frame number to capture sl@0: // For image capture this should be zero. sl@0: const TInt64 KFrameNumberToCapture= 50; sl@0: sl@0: /// Defines camera handler sl@0: class CCameraHandler : public CActive sl@0: { sl@0: public: sl@0: static CCameraHandler* NewL(); sl@0: ~CCameraHandler(); sl@0: TInt SetConfig(TDevCamCaptureMode aCaptureMode, SDevCamFrameSize aSize,SDevCamPixelFormat aPixelFormat,TBool aCreateChunk,TInt aNumBuffers); sl@0: void GetConfig(TDevCamCaptureMode aCaptureMode); sl@0: TInt GetCaps(); sl@0: TInt SetFirstConfig(TUint aOffset); sl@0: TInt Start(TDevCamCaptureMode aCaptureMode); sl@0: TInt Stop(); sl@0: void SetCaptureMode(TDevCamCaptureMode aCaptureMode); sl@0: private: sl@0: CCameraHandler(); sl@0: virtual void RunL(); sl@0: virtual void DoCancel(); sl@0: TInt Init(); sl@0: private: sl@0: TCamDisplayHandler iDispHandler[ECamCaptureModeMax]; sl@0: RDevCameraSc iCamera; sl@0: RChunk iChunk[ECamCaptureModeMax]; sl@0: TInt iFrameCount; sl@0: TDevCamCaptureMode iCaptureMode; sl@0: TInt iCapsSize; sl@0: TAny* iCapsBufPtr; sl@0: TCameraCapsV02* iCameraCaps; sl@0: }; sl@0: sl@0: CCameraHandler::CCameraHandler() : CActive(EPriorityStandard) sl@0: // sl@0: // Constructor for the camera handler sl@0: // sl@0: { sl@0: // Active object priority is set to normal sl@0: } sl@0: sl@0: CCameraHandler::~CCameraHandler() sl@0: // sl@0: // Destructor for the camera handler sl@0: // sl@0: { sl@0: for (TInt captureMode=0; captureMode < ECamCaptureModeMax; captureMode++) sl@0: iChunk[captureMode].Close(); sl@0: if(iCapsBufPtr) sl@0: User::Free(iCapsBufPtr); sl@0: iCamera.Close(); sl@0: // Cancel any active request sl@0: CActive::Cancel(); sl@0: } sl@0: sl@0: void CCameraHandler::DoCancel() sl@0: { sl@0: sl@0: } sl@0: sl@0: CCameraHandler* CCameraHandler::NewL() sl@0: // sl@0: // Create active camera sl@0: // sl@0: { sl@0: test.Printf(_L("NewL\r\n")); sl@0: CCameraHandler *cc = new (ELeave) CCameraHandler; sl@0: TInt r=cc->Init(); sl@0: if (r!=KErrNone) sl@0: User::Leave(r); sl@0: CActiveScheduler::Add(cc); sl@0: return(cc); sl@0: } sl@0: sl@0: TInt CCameraHandler::Init() sl@0: // sl@0: // Initialise hardware sl@0: // sl@0: { sl@0: test.Printf(_L("Init\r\n")); sl@0: TInt r; sl@0: for (TInt captureMode=0; captureMode < ECamCaptureModeMax; captureMode++) sl@0: { sl@0: r=iDispHandler[captureMode].Init(); sl@0: if (r!=KErrNone) sl@0: return(r); sl@0: } sl@0: sl@0: // Open camera driver channel sl@0: r=iCamera.Open(KUnit0); sl@0: return(r); sl@0: } sl@0: sl@0: void CCameraHandler::SetCaptureMode(TDevCamCaptureMode aCaptureMode) sl@0: { sl@0: iCaptureMode=aCaptureMode; sl@0: } sl@0: sl@0: TInt CCameraHandler::GetCaps() sl@0: { sl@0: test.Printf(_L("GetCaps\r\n")); sl@0: iCapsSize=iCamera.CapsSize(); sl@0: iCapsBufPtr = User::Alloc(iCapsSize); sl@0: TPtr8 capsPtr( (TUint8*)iCapsBufPtr, iCapsSize, iCapsSize ); sl@0: TInt r = iCamera.Caps(capsPtr); sl@0: if(r!=KErrNone) sl@0: return r; sl@0: iCameraCaps = (TCameraCapsV02*) capsPtr.Ptr(); sl@0: sl@0: test.Printf(_L("Number of supported pixel formats:%d\r\n"),iCameraCaps->iNumVideoPixelFormats); sl@0: return r; sl@0: } sl@0: sl@0: void CCameraHandler::GetConfig(TDevCamCaptureMode aCaptureMode) sl@0: { sl@0: test.Printf(_L("GetConfig\r\n")); sl@0: // Config camera sl@0: TCameraConfigV02Buf configBuf; sl@0: TCameraConfigV02 &config=configBuf(); sl@0: sl@0: iCamera.GetCamConfig(aCaptureMode, configBuf); sl@0: sl@0: test.Printf(_L("Pixel Format:%d\r\n"),config.iPixelFormat); sl@0: test.Printf(_L("Frame Size :%d\r\n"),config.iFrameSize); sl@0: test.Printf(_L("Frame rate :%dfps\r\n"),config.iFrameRate); sl@0: } sl@0: sl@0: TInt CCameraHandler::SetFirstConfig(TUint aOffset) sl@0: { sl@0: test.Printf(_L("SetFirstConfig\r\n")); sl@0: TInt ret; sl@0: TAny* frameSizeCapsBuf=0; sl@0: SDevCamPixelFormat* pixelFormat; sl@0: SDevCamFrameSize* frameSize; sl@0: TPtr8 frameSizeCapsPtr(0,0,0); sl@0: pixelFormat = (SDevCamPixelFormat*) (iCameraCaps + 1); sl@0: sl@0: if(camera->iCameraCaps->iNumVideoPixelFormats) sl@0: { sl@0: pixelFormat = pixelFormat + iCameraCaps->iNumImagePixelFormats + iCameraCaps->iNumVideoPixelFormats; sl@0: frameSizeCapsBuf = User::Alloc(pixelFormat->iNumFrameSizes*sizeof(SDevCamFrameSize)); sl@0: new (&frameSizeCapsPtr) TPtr8((TUint8*)frameSizeCapsBuf, pixelFormat->iNumFrameSizes*sizeof(SDevCamFrameSize), pixelFormat->iNumFrameSizes*sizeof(SDevCamFrameSize)); sl@0: ret=iCamera.FrameSizeCaps(ECamCaptureModeImage, pixelFormat->iPixelFormat, frameSizeCapsPtr); sl@0: test(ret==KErrNone); sl@0: frameSize = (SDevCamFrameSize*) frameSizeCapsPtr.Ptr(); sl@0: if((pixelFormat->iNumFrameSizes>aOffset) && aOffset) sl@0: frameSize += aOffset; sl@0: ret=camera->SetConfig(ECamCaptureModeVideo,*frameSize,*pixelFormat,ETrue,3); sl@0: test(ret==KErrNone); sl@0: User::Free(frameSizeCapsBuf); sl@0: return(KErrNone); sl@0: } sl@0: else sl@0: return(KErrNotSupported); sl@0: } sl@0: sl@0: TInt CCameraHandler::SetConfig(TDevCamCaptureMode aCaptureMode, SDevCamFrameSize aSize,SDevCamPixelFormat aPixelFormat,TBool aCreateChunk,TInt aNumBuffers) sl@0: { sl@0: test.Printf(_L("SetConfig\r\n")); sl@0: TInt r=iDispHandler[aCaptureMode].SetConfig(aSize,aPixelFormat); sl@0: if (r!=KErrNone) sl@0: return(r); sl@0: sl@0: // Config camera sl@0: TCameraConfigV02Buf configBuf; sl@0: TCameraConfigV02 &config=configBuf(); sl@0: iCamera.GetCamConfig(aCaptureMode, configBuf); // Load defaults sl@0: sl@0: config.iFrameSize=aSize; sl@0: config.iPixelFormat=aPixelFormat; sl@0: config.iFrameRate=KFrameRate; sl@0: sl@0: TMmSharedChunkBufConfig bufferConfig; sl@0: TPckg bufferConfigBuf(bufferConfig); sl@0: sl@0: r=iCamera.SetCamConfig(aCaptureMode,configBuf); sl@0: if (r!=KErrNone) sl@0: return(r); sl@0: sl@0: if (aCreateChunk) sl@0: { sl@0: r=iCamera.SetBufConfigChunkCreate(aCaptureMode,aNumBuffers,iChunk[aCaptureMode]); sl@0: if (r!=KErrNone) sl@0: return(r); sl@0: } sl@0: else sl@0: { sl@0: // 'aNumBuffers' is ignored here, D_MMCSC.LDD currently uses 2 buffers sl@0: sl@0: RMmCreateSc tstDrv; sl@0: r=tstDrv.Open(); sl@0: if (r!=KErrNone) sl@0: return(r); sl@0: r=tstDrv.GetChunkHandle(iChunk[aCaptureMode]); // Get a handle on the shared chunk created by the test driver sl@0: if (r!=KErrNone) sl@0: return(r); sl@0: r=tstDrv.GetBufInfo(bufferConfigBuf); sl@0: if (r!=KErrNone) sl@0: return(r); sl@0: r=iCamera.SetBufConfigChunkOpen(aCaptureMode,bufferConfigBuf,iChunk[aCaptureMode]); sl@0: if (r!=KErrNone) sl@0: return(r); sl@0: sl@0: tstDrv.Close(); sl@0: } sl@0: sl@0: iCamera.GetBufferConfig(aCaptureMode, bufferConfigBuf); sl@0: PrintBufferConf(bufferConfig,test); sl@0: sl@0: return(r); sl@0: } sl@0: sl@0: TInt CCameraHandler::Start(TDevCamCaptureMode aCaptureMode) sl@0: // sl@0: // Set object active, start getting images from the camera sl@0: // sl@0: { sl@0: test.Printf(_L("Start\r\n")); sl@0: // Set object active sl@0: iFrameCount=0; sl@0: SetActive(); sl@0: iCamera.SetCaptureMode(aCaptureMode); sl@0: // Add request for a new image sl@0: TInt r=iCamera.Start(); sl@0: if (r==KErrNone) sl@0: iCamera.NotifyNewImage(iStatus); sl@0: return(r); sl@0: } sl@0: sl@0: void CCameraHandler::RunL() sl@0: // sl@0: // Handles a new request sl@0: // sl@0: { sl@0: TInt retId=iStatus.Int(); sl@0: TInt retOffset=-1; sl@0: iCamera.BufferIdToOffset(iCaptureMode,retId,retOffset); sl@0: if (retId>=0) sl@0: { sl@0: TUint8* imgBase=iChunk[iCaptureMode].Base()+retOffset; sl@0: TInt r=iDispHandler[iCaptureMode].Process(imgBase); sl@0: sl@0: #ifdef __WINSCW__ sl@0: test(r==KErrNotSupported); sl@0: #else sl@0: test(r==KErrNone); sl@0: #endif sl@0: sl@0: // Release the buffer sl@0: test(iCamera.ReleaseBuffer(retId)==KErrNone); sl@0: iFrameCount++; sl@0: } sl@0: else sl@0: test.Printf(_L("Capture error (%d)\r\n"),retId); sl@0: sl@0: if (iFrameCountGetConfig(ECamCaptureModeVideo); sl@0: camera->SetCaptureMode(ECamCaptureModeVideo); sl@0: test.Next(_L("Starting camera")); sl@0: ret=camera->Start(ECamCaptureModeVideo); sl@0: test.Printf(_L("Start returned %d\r\n"),ret); sl@0: test(ret==KErrNone); sl@0: sl@0: // Calculate frame rate. sl@0: // We store cuurent time before receiving data from the camera and sl@0: // after to have received KFrameNumberToCapture pictures, we calculate sl@0: // time elapsed during the reception. sl@0: TTimeIntervalMicroSeconds microseconds ; sl@0: TTime now; sl@0: TTime iTime; sl@0: now.HomeTime(); sl@0: sl@0: test.Next(_L("Starting active scheduler")); sl@0: // Start active scheduler sl@0: CActiveScheduler::Start(); sl@0: sl@0: // We have received all pictures, so we store the new current time sl@0: iTime.HomeTime(); sl@0: sl@0: // We keep this time in microseconds to be more precise sl@0: microseconds = iTime.MicroSecondsFrom(now) ; sl@0: sl@0: TInt64 timeElapsed = microseconds.Int64(); sl@0: sl@0: // We store in this variable, integer value of the frame rate sl@0: TInt64 intFrameRate = (TInt64)((KFrameNumberToCapture *1000000)/timeElapsed); sl@0: sl@0: // We store in this variable, decimal value of the frame rate sl@0: TInt64 milliFrameRate = (TInt64)((KFrameNumberToCapture *1000000)%timeElapsed); sl@0: milliFrameRate = (milliFrameRate*1000) / timeElapsed; sl@0: sl@0: test.Printf(_L(" Frame rate for frames received : %d.%03d frames per second\r\n"), static_cast(intFrameRate), static_cast(milliFrameRate)); sl@0: test.Printf(_L(" Frame rate expected : %d.000 frames per second\r\n"),KFrameRate); sl@0: test.Next(_L("Stopping camera")); sl@0: // Stop Camera sl@0: ret=camera->Stop(); sl@0: test(ret==KErrNone); sl@0: } sl@0: sl@0: // sl@0: // Test program main part sl@0: // sl@0: TInt E32Main() sl@0: { sl@0: __UHEAP_MARK; sl@0: sl@0: test.Title(); sl@0: test.Start(_L("Camera module GEN test")); sl@0: sl@0: test.Next(_L("Loading CAMERA PDD")); sl@0: TInt ret=User::LoadPhysicalDevice(KCamPddFileName); sl@0: test.Printf(_L("Returned %d\r\n"),ret); sl@0: sl@0: if (ret==KErrNotFound) sl@0: { sl@0: test.Printf(_L("Shared chunk camera driver not supported - test skipped\r\n")); sl@0: test.End(); sl@0: test.Close(); sl@0: __UHEAP_MARKEND; sl@0: return(KErrNone); sl@0: } sl@0: sl@0: test(ret==KErrNone || ret==KErrAlreadyExists); sl@0: sl@0: test.Next(_L("Loading CAMERA LDD")); sl@0: ret=User::LoadLogicalDevice(KCamLddFileName); sl@0: test.Printf(_L("Returned %d\r\n"),ret); sl@0: test(ret==KErrNone || ret==KErrAlreadyExists); sl@0: sl@0: test.Next(_L("Loading D_MMCSC LDD")); sl@0: ret=User::LoadLogicalDevice(KTstLddFileName); sl@0: test.Printf(_L("Returned %d\r\n"),ret); sl@0: test(ret==KErrNone||ret==KErrAlreadyExists); sl@0: sl@0: __KHEAP_MARK; sl@0: sl@0: // Construct and install the active scheduler sl@0: test.Next(_L("Initialising active scheduler")); sl@0: CActiveScheduler *exampleScheduler = new (ELeave) CActiveScheduler(); sl@0: // Install as the active scheduler sl@0: CActiveScheduler::Install(exampleScheduler); sl@0: sl@0: // Create camera handler sl@0: test.Next(_L("Creating camera handler")); sl@0: camera = CCameraHandler::NewL(); sl@0: sl@0: test.Next(_L("+ Getting Camera Capabilities")); sl@0: ret=camera->GetCaps(); sl@0: test(ret==KErrNone); sl@0: sl@0: // SetConfig sl@0: test.Next(_L("Setting Camera Configuration")); sl@0: ret=camera->SetFirstConfig(0); sl@0: test(ret==KErrNone); sl@0: TestRecording(); sl@0: sl@0: // Repeat with a different configuration sl@0: test.Next(_L("Resetting Camera Configuration")); sl@0: ret=camera->SetFirstConfig(1); sl@0: test(ret==KErrNone); sl@0: TestRecording(); sl@0: sl@0: delete camera; sl@0: delete exampleScheduler; sl@0: sl@0: __KHEAP_MARKEND; sl@0: sl@0: // Free the PDDs and LDDs sl@0: test.Next(_L("Freeing ECAMERASC LDD")); sl@0: ret=User::FreeLogicalDevice(KDevCameraScName); sl@0: test(ret==KErrNone); sl@0: sl@0: test.Next(_L("Freeing CAMERASC PDD")) ; sl@0: TFindPhysicalDevice fDr; sl@0: TFullName drivName; sl@0: TName searchName; sl@0: searchName.Append(KDevCameraScName); sl@0: searchName.Append(KCamFreePddExtension); sl@0: fDr.Find(searchName); sl@0: ret=fDr.Next(drivName); sl@0: test(ret==KErrNone); sl@0: ret=User::FreePhysicalDevice(drivName); sl@0: test(ret==KErrNone); sl@0: sl@0: test.Next(_L("Freeing D_MMCSC LDD")); sl@0: ret=User::FreeLogicalDevice(KDevMmCScName); sl@0: test(ret==KErrNone); sl@0: sl@0: test.Printf(_L("Hit any key to continue\r\n")); sl@0: util_getch_withtimeout(5); sl@0: sl@0: test.End(); sl@0: test.Close(); sl@0: sl@0: __UHEAP_MARKEND; sl@0: sl@0: return KErrNone; sl@0: }