loadgen/src/loadgen_photocapture.cpp
branchRCL_3
changeset 22 fad26422216a
equal deleted inserted replaced
21:b3cee849fa46 22:fad26422216a
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "loadgen_photocapture.h"
       
    21 #include "loadgen_model.h"
       
    22 #include "loadgen.hrh"
       
    23 #include "loadgen_traces.h"
       
    24 #include <loadgen.rsg>
       
    25 #include <e32hal.h>
       
    26 #include <u32hal.h>
       
    27 #include <e32math.h>
       
    28 #include <eikenv.h>
       
    29 
       
    30 _LIT(KThreadName, "PhotoCapture %d");
       
    31 
       
    32 const TInt KDefaultStart = 50;
       
    33 const TInt KDefaultPeriod = 5000000;
       
    34 
       
    35 //TInt CPhotoCapture::iImagesReady = 0;
       
    36     
       
    37 // ===================================== MEMBER FUNCTIONS =====================================
       
    38 
       
    39 CPhotoCapture* CPhotoCapture::NewL(TPhotoCaptureAttributes& aAttributes, TInt aReferenceNumber)
       
    40     {
       
    41     CPhotoCapture* self = new(ELeave) CPhotoCapture(aAttributes, aReferenceNumber);
       
    42     CleanupStack::PushL(self);
       
    43     self->ConstructL();
       
    44     CleanupStack::Pop(self);
       
    45     return self;    
       
    46     }
       
    47 
       
    48 // --------------------------------------------------------------------------------------------
       
    49 
       
    50 CPhotoCapture::~CPhotoCapture()
       
    51     {    
       
    52     Close();
       
    53     }
       
    54 
       
    55 // --------------------------------------------------------------------------------------------
       
    56 
       
    57 CPhotoCapture::CPhotoCapture(TPhotoCaptureAttributes& aAttributes, TInt aReferenceNumber) : iAttributes(aAttributes)
       
    58     {    
       
    59     iAttributes.iId = aReferenceNumber;    
       
    60     }
       
    61 
       
    62 // --------------------------------------------------------------------------------------------
       
    63 
       
    64 void CPhotoCapture::ConstructL()
       
    65     {    
       
    66     CLoadBase::ConstructL();
       
    67     
       
    68     iType = ELoadGenCmdNewLoadPhotoCaptures;
       
    69     
       
    70     TBuf<64> threadName;
       
    71     threadName.Format(KThreadName, iAttributes.iId);
       
    72     
       
    73     // create a thread
       
    74     User::LeaveIfError(iThread.Create(threadName, ThreadFunction, KDefaultStackSize*2, KMinHeapSize, 1024*KMinHeapSize, (TAny*) &iAttributes ));
       
    75     
       
    76     // set priority of the thread
       
    77     SetPriority();
       
    78     }
       
    79 
       
    80 // --------------------------------------------------------------------------------------------
       
    81 
       
    82 TInt CPhotoCapture::ThreadFunction(TAny* aThreadArg)
       
    83     {
       
    84     TInt err = KErrNone;
       
    85     
       
    86     CTrapCleanup* pC = CTrapCleanup::New();
       
    87     CActiveScheduler* pS = new CActiveScheduler;
       
    88     CActiveScheduler::Install(pS);
       
    89 
       
    90     // start generating load, pass pointer to arguments
       
    91     GenerateLoad(*((TPhotoCaptureAttributes*) aThreadArg));
       
    92 
       
    93     delete pS;
       
    94     delete pC;
       
    95     
       
    96     return err;
       
    97     }
       
    98 
       
    99 // --------------------------------------------------------------------------------------------
       
   100 
       
   101 void CPhotoCapture::GenerateLoad(TPhotoCaptureAttributes& aAttributes)
       
   102     {    
       
   103     CCameraManager* cameraManager = 0;
       
   104     TRAPD(err, cameraManager = CCameraManager::NewL(aAttributes));
       
   105     if (err == KErrNone) CActiveScheduler::Start();
       
   106     delete cameraManager;    
       
   107     }
       
   108 
       
   109 // --------------------------------------------------------------------------------------------
       
   110 
       
   111 void CPhotoCapture::Resume()
       
   112     {    
       
   113     CLoadBase::Resume();    
       
   114     iThread.Resume();
       
   115     }
       
   116 
       
   117 // --------------------------------------------------------------------------------------------
       
   118 
       
   119 void CPhotoCapture::Suspend()
       
   120     {    
       
   121     CLoadBase::Suspend();    
       
   122     iThread.Suspend();
       
   123     }
       
   124 
       
   125 // --------------------------------------------------------------------------------------------
       
   126 
       
   127 void CPhotoCapture::SetPriority()
       
   128     {    
       
   129     CLoadBase::SetPriority();    
       
   130     iThread.SetPriority(CLoadGenModel::SettingItemToThreadPriority(iAttributes.iPriority));
       
   131     }
       
   132     
       
   133 // --------------------------------------------------------------------------------------------
       
   134 
       
   135 void CPhotoCapture::Close()
       
   136     {    
       
   137     CLoadBase::Close();
       
   138 
       
   139     // kill the thread immediately
       
   140     iThread.Kill(0);   
       
   141 
       
   142     iThread.Close();
       
   143     }
       
   144     
       
   145 // --------------------------------------------------------------------------------------------
       
   146 
       
   147 TPtrC CPhotoCapture::Description()
       
   148     {    
       
   149     TBuf<256> buf;
       
   150     TBuf<16> prioBuf;
       
   151     CLoadGenModel::SettingItemToThreadDescription(iAttributes.iPriority, prioBuf);
       
   152         
       
   153     _LIT(KPhotoCaptureEntry, "[%d] PhotoCapture camera=%d prio=%S idle=%dms random=%d%%");
       
   154     buf.Format(KPhotoCaptureEntry, iAttributes.iId, iAttributes.iCamera, &prioBuf, iAttributes.iIdle, iAttributes.iRandomVariance);
       
   155        
       
   156     return TPtrC(buf);
       
   157     }               
       
   158 
       
   159 // --------------------------------------------------------------------------------------------
       
   160 
       
   161 
       
   162 // --------------------------------------------------------------------------------------------
       
   163 CCameraManager* CCameraManager::NewL(TPhotoCaptureAttributes& aAttrs)
       
   164     {
       
   165     CCameraManager* self = new (ELeave) CCameraManager(aAttrs);
       
   166     CleanupStack::PushL(self);
       
   167     self->ConstructL();
       
   168     CleanupStack::Pop(self);
       
   169     return self;
       
   170     }
       
   171  
       
   172 // --------------------------------------------------------------------------------------------
       
   173 CCameraManager::~CCameraManager()
       
   174     {    
       
   175     delete iCamera;
       
   176     if (iPeriodicTimer)
       
   177         {
       
   178         iPeriodicTimer->Cancel();
       
   179         delete iPeriodicTimer;
       
   180         }
       
   181     }
       
   182  
       
   183 // --------------------------------------------------------------------------------------------
       
   184 CCameraManager::CCameraManager(TPhotoCaptureAttributes& aAttrs) 
       
   185 : 
       
   186 CActive(EPriorityStandard),
       
   187 iAttributes(aAttrs)
       
   188     {        
       
   189     iState = ENotReady;
       
   190     iNumOfPics = 0;
       
   191     }
       
   192  
       
   193 // --------------------------------------------------------------------------------------------
       
   194 void CCameraManager::ConstructL()
       
   195     {   
       
   196     CActiveScheduler::Add(this);    
       
   197     // set the status as pending
       
   198     iStatus = KRequestPending;
       
   199     SetActive();
       
   200         
       
   201     // set the death status pointer point to the request status of this ao
       
   202     iAttributes.iDeathStatus = &iStatus;
       
   203     
       
   204     // start timer    
       
   205     iPeriodicTimer = CPeriodic::NewL(CActive::EPriorityStandard);
       
   206     iPeriodicTimer->Start(KDefaultStart, KDefaultPeriod, TCallBack(PeriodicTimerCallBack, this));
       
   207     }
       
   208 
       
   209 // --------------------------------------------------------------------------------------------
       
   210 void CCameraManager::DoCancel()
       
   211     {    
       
   212     }
       
   213 
       
   214 // --------------------------------------------------------------------------------------------
       
   215 void CCameraManager::RunL()
       
   216     {    
       
   217     // request status has completed by the main thread meaning that we need to stop now
       
   218     CActiveScheduler::Stop();
       
   219     }
       
   220  
       
   221 // --------------------------------------------------------------------------------------------
       
   222 void CCameraManager::ReserveCameraL()
       
   223     {
       
   224     LOGSTRING2("LoadGen: CCameraManager::ReserveCameraL() - Using Camera %d", iAttributes.iCamera);
       
   225     if( iCamera )
       
   226         {
       
   227         iCamera->Release();
       
   228         }
       
   229     delete iCamera;
       
   230     iCamera = 0;
       
   231     TRAPD( err, iCamera = CCamera::NewL(*this, iAttributes.iCamera));
       
   232     iCurrentCamera = iAttributes.iCamera;
       
   233     if(err != KErrNone)
       
   234         {
       
   235         LOGSTRING2("LoadGen: CCamera::NewL leaves with error: %d", err);
       
   236         User::Leave(err);
       
   237         }
       
   238     iCamera->CameraInfo(iCameraInfo);
       
   239     iCamera->Reserve();    
       
   240     }
       
   241  
       
   242 // --------------------------------------------------------------------------------------------
       
   243 void CCameraManager::ReserveComplete(TInt aError)
       
   244     {
       
   245     if( aError != KErrNone )
       
   246         LOGSTRING2("LoadGen: CCameraManager::ReserveComplete() - Error: %d", aError);
       
   247     
       
   248     if( aError == KErrNone )
       
   249         {
       
   250         iState = ECameraReserved;
       
   251         iCamera->PowerOn();
       
   252         }
       
   253     }
       
   254  
       
   255 // --------------------------------------------------------------------------------------------
       
   256 void CCameraManager::PowerOnComplete(TInt aError)
       
   257     {
       
   258     if( aError != KErrNone )
       
   259         {
       
   260         LOGSTRING2("LoadGen: CCameraManager::PowerOnComplete() - Error: %d", aError);
       
   261         return;
       
   262         }
       
   263 
       
   264     // Define image format, try JPEG images first:
       
   265     CCamera::TFormat format = CCamera::EFormatExif;
       
   266 
       
   267     if(iCameraInfo.iImageFormatsSupported & CCamera::EFormatExif)
       
   268         {
       
   269         LOGSTRING("LoadGen: CCameraManager::PowerOnComplete() - EXIF JPEG supported");
       
   270         format = CCamera::EFormatExif;
       
   271         }
       
   272     else if( iCameraInfo.iImageFormatsSupported & CCamera::EFormatJpeg )
       
   273         {
       
   274         LOGSTRING("LoadGen: CCameraManager::PowerOnComplete() - JFIF JPEG supported");
       
   275         format = CCamera::EFormatJpeg;
       
   276         }
       
   277     else if( iCameraInfo.iImageFormatsSupported & CCamera::EFormatFbsBitmapColor4K )
       
   278         {
       
   279         LOGSTRING("LoadGen: CCameraManager::PowerOnComplete() - Bitmap 4K Color supported");
       
   280         format = CCamera::EFormatFbsBitmapColor4K;
       
   281         }
       
   282     else if( iCameraInfo.iImageFormatsSupported & CCamera::EFormatFbsBitmapColor64K )
       
   283         {
       
   284         LOGSTRING("LoadGen: CCameraManager::PowerOnComplete() - Bitmap 64K Color supported");
       
   285         format = CCamera::EFormatFbsBitmapColor64K;
       
   286         }
       
   287     else if( iCameraInfo.iImageFormatsSupported & CCamera::EFormatFbsBitmapColor16M )
       
   288         {
       
   289         LOGSTRING("LoadGen: CCameraManager::PowerOnComplete() - Bitmap 16M Color supported");
       
   290         format = CCamera::EFormatFbsBitmapColor16M;
       
   291         }
       
   292     else
       
   293         {
       
   294         LOGSTRING2("LoadGen: CCameraManager::PowerOnComplete() - JPEGs not supported, trying \"User defined\" (Supported formats: %d)", iCameraInfo.iImageFormatsSupported);
       
   295         format = CCamera::EFormatUserDefined;
       
   296         }
       
   297     const TInt KImageSizeIndex = 0;  // Largest image size
       
   298     TRAPD( err, iCamera->PrepareImageCaptureL(format, KImageSizeIndex));
       
   299     if( err != KErrNone )
       
   300         {
       
   301         LOGSTRING2("LoadGen: CCameraManager::PowerOnComplete() - Error while preparing camera: %d", err);        
       
   302         }
       
   303     // Camera ready:
       
   304     iState = EIdle;
       
   305     // Take photo:
       
   306     TRAP(err, CapturePhotoL());
       
   307     if( err != KErrNone )
       
   308         {
       
   309         LOGSTRING2("LoadGen: CCameraManager::PowerOnComplete() - Photo capture error: %d", err);
       
   310         }
       
   311     }
       
   312  
       
   313 // --------------------------------------------------------------------------------------------
       
   314 void CCameraManager::ViewFinderFrameReady(CFbsBitmap& /*aFrame*/)
       
   315     {
       
   316     }
       
   317 
       
   318 // --------------------------------------------------------------------------------------------
       
   319 void CCameraManager::ImageReady(CFbsBitmap* aBitmap, HBufC8* aData, TInt aError)
       
   320     {    
       
   321     if( aError != KErrNone )
       
   322         {
       
   323         LOGSTRING2("LoadGen: CCameraManager::ImageReady() - Error: %d", aError);
       
   324         }    
       
   325     else
       
   326         {
       
   327         iNumOfPics++;    
       
   328         LOGSTRING2("LoadGen: CCameraManager::ImageReady() - Images successfully captured: %d", iNumOfPics);
       
   329         }
       
   330     delete aBitmap;
       
   331     delete aData;
       
   332     iState = EIdle;
       
   333     }
       
   334  
       
   335 // --------------------------------------------------------------------------------------------
       
   336 void CCameraManager::FrameBufferReady(MFrameBuffer* /*aFrameBuffer*/, TInt aError)
       
   337     {
       
   338     if( aError != KErrNone )
       
   339         LOGSTRING2("LoadGen: CCameraManager::FrameBufferReady() - Error: %d", aError);
       
   340     }
       
   341  
       
   342 // --------------------------------------------------------------------------------------------
       
   343 void CCameraManager::CapturePhotoL()
       
   344     {    
       
   345     // Camera Manager must be in idle state when taking a photo
       
   346     if( iState == ENotReady )
       
   347         {
       
   348         // Not ready, camera must be reserved:
       
   349         ReserveCameraL();
       
   350         }
       
   351     else if (iState == EIdle)
       
   352         {
       
   353         if( iCurrentCamera != iAttributes.iCamera )
       
   354             {
       
   355             LOGSTRING3("LoadGen: CCameraManager::CapturePhotoL() - Switching from Camera %d to Camera %d", iCurrentCamera, iAttributes.iCamera);
       
   356             ReserveCameraL();
       
   357             }
       
   358         else
       
   359             {
       
   360             iState = ECapture;
       
   361             if( iCameraInfo.iFlashModesSupported & CCamera::EFlashForced)
       
   362                 {
       
   363                 iCamera->SetFlashL(CCamera::EFlashForced);
       
   364                 }
       
   365             iCamera->CaptureImage();
       
   366             iPeriodicTimer->Start( CLoadGenModel::MilliSecondsToMicroSeconds( iAttributes.iIdle,
       
   367                 iAttributes.iRandomVariance ), KDefaultPeriod, 
       
   368                 TCallBack( PeriodicTimerCallBack, this ) );
       
   369             }
       
   370         }
       
   371     else
       
   372         {
       
   373         iPeriodicTimer->Start( CLoadGenModel::MilliSecondsToMicroSeconds( iAttributes.iIdle,
       
   374                                     iAttributes.iRandomVariance ), KDefaultPeriod, 
       
   375                                     TCallBack( PeriodicTimerCallBack, this ) );
       
   376         }
       
   377     }
       
   378 
       
   379 // --------------------------------------------------------------------------------------------
       
   380 
       
   381 TInt CCameraManager::PeriodicTimerCallBack(TAny* aAny)
       
   382     {
       
   383     CCameraManager* self = static_cast<CCameraManager*>( aAny );
       
   384     self->iPeriodicTimer->Cancel();
       
   385     TRAP_IGNORE(self->CapturePhotoL());   
       
   386     return KErrNone;
       
   387     }
       
   388 
       
   389 // End of File