camerauis/cameraxui/cxengine/src/cxecameradevicecontrolsymbian.cpp
branchRCL_3
changeset 53 61bc0f252b2b
equal deleted inserted replaced
50:f54ad444594d 53:61bc0f252b2b
       
     1 /*
       
     2 * Copyright (c) 2009-2010 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 #include "cxecameradevicecontrolsymbian.h"
       
    18 #include "cxesettingsimp.h"
       
    19 #include "cxefeaturemanagerimp.h"
       
    20 #include "cxutils.h"
       
    21 #include "cxenamespace.h"
       
    22 #include "cxeerrormappingsymbian.h"
       
    23 #include "cxestate.h"
       
    24 
       
    25 #include "OstTraceDefinitions.h"
       
    26 #ifdef OST_TRACE_COMPILER_IN_USE
       
    27 #include "cxecameradevicecontrolsymbianTraces.h"
       
    28 #endif
       
    29 
       
    30 
       
    31 
       
    32 CxeCameraDeviceControlSymbian::CxeCameraDeviceControlSymbian()
       
    33     : CxeStateMachine("CxeCameraDeviceControlSymbian"),
       
    34     mCameraDevice(NULL),
       
    35     mCameraIndex(Cxe::PrimaryCameraIndex),
       
    36     mCameraMode(Cxe::ImageMode)
       
    37 {
       
    38     CX_DEBUG_ENTER_FUNCTION();
       
    39 
       
    40 #ifdef FORCE_SECONDARY_CAMERA
       
    41     mCameraIndex = Cxe::SecondaryCameraIndex;
       
    42 #endif
       
    43     qRegisterMetaType<CxeCameraDeviceControl::State>();
       
    44     initializeStates();
       
    45 
       
    46     mCameraDevice = new CxeCameraDevice();
       
    47 
       
    48     //!@todo Temporary delay before reserving camera to avoid timing issues
       
    49     connect(&mReserveTimer, SIGNAL(timeout()), this, SLOT(doReserve()));
       
    50     mReserveTimer.setSingleShot(true);
       
    51 
       
    52     CX_DEBUG_EXIT_FUNCTION();
       
    53 }
       
    54 
       
    55 CxeCameraDeviceControlSymbian::~CxeCameraDeviceControlSymbian()
       
    56 {
       
    57     CX_DEBUG_ENTER_FUNCTION();
       
    58 
       
    59     // release current camera
       
    60     releaseCurrentCamera();
       
    61 
       
    62     delete mCameraDevice;
       
    63 
       
    64     CX_DEBUG_EXIT_FUNCTION();
       
    65 }
       
    66 
       
    67 void CxeCameraDeviceControlSymbian::init()
       
    68 {
       
    69     CX_DEBUG_ENTER_FUNCTION();
       
    70 
       
    71     CxeError::Id err = mCameraDevice->newCamera(mCameraIndex, this);
       
    72 
       
    73     if (err) {
       
    74         emit initModeComplete(err);
       
    75     }
       
    76 
       
    77     CX_DEBUG_EXIT_FUNCTION();
       
    78 }
       
    79 
       
    80 void CxeCameraDeviceControlSymbian::reserve()
       
    81 {
       
    82     CX_DEBUG_ENTER_FUNCTION();
       
    83     CX_ASSERT_ALWAYS(mCameraDevice);
       
    84 
       
    85     if (state() == Idle) {
       
    86         setState(Initializing);
       
    87         //!@todo Temporary delay before reserving camera to avoid timing issues
       
    88         mReserveTimer.start(2000);
       
    89         OstTrace0(camerax_performance, CXECAMERADEVICECONTROLSYMBIAN_RESERVE, "msg: e_CX_RESERVE 1");
       
    90         CX_DEBUG(("HACK: Waiting for 2000ms before really doing reserve()"));
       
    91     } else if (state() == PendingRelease) {
       
    92         // if we get a reserve request and if there is a pending release
       
    93         // it is just fine to continue the init operation.
       
    94         setState(Initializing);
       
    95     } else {
       
    96         // nothing to do
       
    97     }
       
    98 
       
    99     CX_DEBUG_EXIT_FUNCTION();
       
   100 }
       
   101 
       
   102 void CxeCameraDeviceControlSymbian::doReserve()
       
   103 {
       
   104     CX_DEBUG_ENTER_FUNCTION();
       
   105     CX_ASSERT_ALWAYS(mCameraDevice);
       
   106     mCameraDevice->reserveCamera();
       
   107     CX_DEBUG_EXIT_FUNCTION();
       
   108 }
       
   109 
       
   110 
       
   111 void CxeCameraDeviceControlSymbian::powerOn()
       
   112 {
       
   113     CX_DEBUG_ENTER_FUNCTION();
       
   114 
       
   115     CX_DEBUG_ASSERT(mCameraDevice && mCameraDevice->camera());
       
   116     if (state() == Initializing) {
       
   117         mCameraDevice->camera()->PowerOn();
       
   118         OstTrace0(camerax_performance, CXECAMERADEVICECONTROLSYMBIAN_POWERON, "msg: e_CX_POWER_ON 1");
       
   119     }
       
   120     CX_DEBUG_EXIT_FUNCTION();
       
   121 }
       
   122 
       
   123 void CxeCameraDeviceControlSymbian::HandleEvent(const TECAMEvent &aEvent)
       
   124 {
       
   125     doHandleEvent( aEvent );
       
   126     emit cameraEvent( aEvent.iEventType.iUid, aEvent.iErrorCode );
       
   127 }
       
   128 
       
   129 void CxeCameraDeviceControlSymbian::doHandleEvent(const TECAMEvent &aEvent)
       
   130 {
       
   131     CX_DEBUG_ENTER_FUNCTION();
       
   132 
       
   133     CX_DEBUG(("Cxe: event 0x%08x, %d", aEvent.iEventType.iUid, aEvent.iErrorCode));
       
   134 
       
   135     if(aEvent.iEventType == KUidECamEventReserveComplete) {
       
   136         handleReserveComplete(aEvent.iErrorCode);
       
   137     }
       
   138     else if(aEvent.iEventType == KUidECamEventPowerOnComplete) {
       
   139         handlePowerOnComplete(aEvent.iErrorCode);
       
   140     }
       
   141     else if(aEvent.iEventType == KUidECamEventCameraNoLongerReserved)
       
   142         {
       
   143         handleNoLongerReserved(aEvent.iErrorCode);
       
   144         }
       
   145     else
       
   146         {
       
   147         // We don't care about this event -- ignore
       
   148         }
       
   149 
       
   150     CX_DEBUG_EXIT_FUNCTION();
       
   151 }
       
   152 
       
   153 void CxeCameraDeviceControlSymbian::handleReserveComplete(int error)
       
   154 {
       
   155     CX_DEBUG_ENTER_FUNCTION();
       
   156     OstTrace0( camerax_performance, CXECAMERADEVICECONTROLSYMBIAN_HANDLERESERVECOMPLETE, "msg: e_CX_RESERVE 0" );
       
   157 
       
   158     if(state() == PendingRelease) {
       
   159         release();
       
   160     } else {
       
   161         if (!error) {
       
   162             powerOn();
       
   163         } else {
       
   164             emit initModeComplete(CxeErrorHandlingSymbian::map(error));
       
   165             setState(Idle);
       
   166         }
       
   167     }
       
   168 
       
   169     CX_DEBUG_EXIT_FUNCTION();
       
   170 }
       
   171 
       
   172 void CxeCameraDeviceControlSymbian::handlePowerOnComplete(int error)
       
   173 {
       
   174     CX_DEBUG_ENTER_FUNCTION();
       
   175     OstTrace0(camerax_performance, CXECAMERADEVICECONTROLSYMBIAN_HANDLEPOWERONCOMPLETE, "msg: e_CX_POWER_ON 0");
       
   176 
       
   177     if(state() == PendingRelease) {
       
   178         release();
       
   179     } else {
       
   180         if (!error) {
       
   181             setState(Ready);
       
   182             emit deviceReady(); // this will trigger prepare for either still or video mode
       
   183         } else {
       
   184             setState(PendingRelease);
       
   185             release();
       
   186             emit initModeComplete(CxeErrorHandlingSymbian::map(error));
       
   187         }
       
   188     }
       
   189 
       
   190     CX_DEBUG_EXIT_FUNCTION();
       
   191 }
       
   192 
       
   193 void CxeCameraDeviceControlSymbian::handleNoLongerReserved(int /* error */)
       
   194 {
       
   195     setState( Idle );
       
   196     //! @todo: handle cases where camera is lost
       
   197 }
       
   198 
       
   199 
       
   200 /*
       
   201 * switch between camera <-> primary <-> secondary
       
   202 */
       
   203 
       
   204 CxeError::Id CxeCameraDeviceControlSymbian::switchCamera(Cxe::CameraIndex cameraIndex)
       
   205 {
       
   206     CX_DEBUG_ENTER_FUNCTION();
       
   207 
       
   208 #ifdef FORCE_SECONDARY_CAMERA
       
   209     // 2nd camera forced use, make us believe camera never needs to be swithed.
       
   210     CX_DEBUG(("CxeCameraDeviceControlSymbian::switchCamera() second camera forced, skip switch"));
       
   211     cameraIndex = mCameraIndex;
       
   212 #endif
       
   213 
       
   214     // during mode change, we have to re-create camera reference and release resources.
       
   215     // in all other cases if the camera indexes are same, it means that camera reference
       
   216     // is already created and hence we can return immediately.
       
   217     if (cameraIndex == mCameraIndex) {
       
   218         CX_DEBUG_EXIT_FUNCTION();
       
   219         return CxeError::None;
       
   220     }
       
   221 
       
   222     releaseCurrentCamera();
       
   223 
       
   224     CxeError::Id err = mCameraDevice->newCamera(cameraIndex, this);
       
   225 
       
   226     if (err == CxeError::None){
       
   227         // CCamera object created, so make sure that appropriate index is used
       
   228         mCameraIndex = cameraIndex;
       
   229     }
       
   230 
       
   231     if (!err) { // if there is no error creating new camera, then start Reserve
       
   232         reserve();
       
   233     }
       
   234 
       
   235     CX_DEBUG_EXIT_FUNCTION();
       
   236     return err;
       
   237 }
       
   238 
       
   239 void CxeCameraDeviceControlSymbian::release()
       
   240 {
       
   241     CX_DEBUG_ENTER_FUNCTION();
       
   242 
       
   243     //!@todo Temporary delay before reserving camera to avoid timing issues
       
   244     if (mReserveTimer.isActive()) {
       
   245         // release() was called when the timer was active
       
   246         mReserveTimer.stop();
       
   247         mCameraDevice->releaseCamera();
       
   248         setState(Idle);
       
   249         CX_DEBUG_EXIT_FUNCTION();
       
   250         return;
       
   251     }
       
   252 
       
   253     if (state() == Idle) {
       
   254         // nothing to do
       
   255         return;
       
   256     }
       
   257 
       
   258     if (state() == Initializing) {
       
   259         setState(PendingRelease);
       
   260     } else {
       
   261         mCameraDevice->releaseCamera();
       
   262         setState(Idle);
       
   263     }
       
   264 
       
   265     CX_DEBUG_EXIT_FUNCTION();
       
   266 }
       
   267 
       
   268 /*
       
   269 * Release resources w.r.t current camera
       
   270 */
       
   271 void CxeCameraDeviceControlSymbian::releaseCurrentCamera()
       
   272 {
       
   273     CX_DEBUG_ENTER_FUNCTION();
       
   274 
       
   275     release();
       
   276     // delete old camera instance, this will inturn generates "prepareForCameraDelete" event
       
   277     // across all interested classes which wants to do cleanup for new camera reference change
       
   278     mCameraDevice->deleteCamera();
       
   279 
       
   280     CX_DEBUG_EXIT_FUNCTION();
       
   281 }
       
   282 
       
   283 void CxeCameraDeviceControlSymbian::ViewFinderReady(MCameraBuffer &aCameraBuffer, TInt aError)
       
   284 {
       
   285     CX_DEBUG_ENTER_FUNCTION();
       
   286     emit vfFrameReady(&aCameraBuffer, aError);
       
   287     CX_DEBUG_EXIT_FUNCTION();
       
   288 }
       
   289 
       
   290 void CxeCameraDeviceControlSymbian::ImageBufferReady(MCameraBuffer& aCameraBuffer, TInt aError)
       
   291 {
       
   292     CX_DEBUG_ENTER_FUNCTION();
       
   293 
       
   294     emit imageBufferReady( &aCameraBuffer, aError );
       
   295 
       
   296     CX_DEBUG_EXIT_FUNCTION();
       
   297 }
       
   298 
       
   299 void CxeCameraDeviceControlSymbian::VideoBufferReady(MCameraBuffer &/*aCameraBuffer*/, TInt /*aError*/)
       
   300 {
       
   301     CX_DEBUG_IN_FUNCTION();
       
   302 }
       
   303 
       
   304 Cxe::CameraIndex CxeCameraDeviceControlSymbian::cameraIndex() const
       
   305 {
       
   306     return mCameraIndex;
       
   307 }
       
   308 
       
   309 Cxe::CameraMode CxeCameraDeviceControlSymbian::mode() const
       
   310 {
       
   311     return mCameraMode;
       
   312 }
       
   313 
       
   314 void CxeCameraDeviceControlSymbian::setMode(Cxe::CameraMode mode)
       
   315 {
       
   316     mCameraMode = mode;
       
   317 }
       
   318 
       
   319 CxeCameraDeviceControl::State CxeCameraDeviceControlSymbian::state() const
       
   320 {
       
   321     return static_cast<State>(stateId());
       
   322 }
       
   323 
       
   324 void CxeCameraDeviceControlSymbian::handleStateChanged(int newStateId, CxeError::Id error)
       
   325 {
       
   326     emit stateChanged(static_cast<State>(newStateId), error);
       
   327 }
       
   328 
       
   329 void CxeCameraDeviceControlSymbian::initializeStates()
       
   330 {
       
   331     // addState( id, name, allowed next states )
       
   332     addState(new CxeState( Idle , "Idle", Initializing));
       
   333     addState(new CxeState( Initializing, "Initializing", PendingRelease | Ready | Idle));
       
   334     addState(new CxeState( Ready, "Ready", Idle));
       
   335     addState(new CxeState( PendingRelease, "PendingRelease", Initializing | Idle));
       
   336 
       
   337     setInitialState(Idle);
       
   338 }
       
   339 
       
   340 CxeCameraDevice *CxeCameraDeviceControlSymbian::cameraDevice()
       
   341 {
       
   342     return mCameraDevice;
       
   343 }
       
   344 
       
   345 // end of file