camerauis/cameraxui/cxengine/src/cxeviewfindercontrolsymbian.cpp
changeset 19 d9aefe59d544
child 21 fa6d9f75d6a6
equal deleted inserted replaced
3:8b2d6d0384b0 19:d9aefe59d544
       
     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 #include <w32std.h> // RWindow, RWsSession.
       
    18 #include <coemain.h> // CCoeEnv
       
    19 #include <coecntrl.h>
       
    20 #include <ecam/ecamdirectviewfinder.h>
       
    21 #include "cxutils.h"
       
    22 #include "cxecameradevicecontrolsymbian.h" // CxeCameraDevice
       
    23 #include "cxeviewfindercontrolsymbian.h"
       
    24 #include "cxesettings.h"
       
    25 #include "cxesettingsmappersymbian.h"
       
    26 #include "cxeerrormappingsymbian.h"
       
    27 #include "cxestate.h"
       
    28 #include "cxevideocontainer.h"
       
    29 #include "OstTraceDefinitions.h"
       
    30 #ifdef OST_TRACE_COMPILER_IN_USE
       
    31 #include "cxeviewfindercontrolsymbianTraces.h"
       
    32 #endif
       
    33 
       
    34 
       
    35 
       
    36 /**
       
    37 * CxeViewfinderControlSymbian::CxeViewfinderControlSymbian
       
    38 */
       
    39 CxeViewfinderControlSymbian::CxeViewfinderControlSymbian( CxeCameraDevice &cameraDevice,
       
    40         CxeCameraDeviceControl &cameraDeviceControl )
       
    41     : CxeStateMachine("CxeViewfinderControlSymbian"),
       
    42       mCameraDevice( cameraDevice ),
       
    43       mCameraDeviceControl( cameraDeviceControl ),
       
    44       mUiWindow(NULL),
       
    45       mVideoWindow(NULL),
       
    46       mVideoContainer(NULL),
       
    47       mDirectViewfinder( NULL ),
       
    48       mDirectViewfinderInUse( true )
       
    49 {
       
    50     CX_DEBUG_ENTER_FUNCTION();
       
    51 
       
    52     qRegisterMetaType<CxeViewfinderControlSymbian::State>();
       
    53     initializeStates();
       
    54 
       
    55     // connect signals from cameraDevice, so we recieve events when camera reference changes
       
    56     connect(&mCameraDevice, SIGNAL(prepareForCameraDelete()),
       
    57             this,SLOT(prepareForCameraDelete()));
       
    58 
       
    59     connect(&mCameraDevice, SIGNAL(prepareForRelease()),
       
    60             this,SLOT(prepareForRelease()));
       
    61 
       
    62     connect(&mCameraDevice, SIGNAL(cameraAllocated(CxeError::Id)),
       
    63             this,SLOT(handleCameraAllocated(CxeError::Id)));
       
    64 
       
    65     CX_DEBUG_EXIT_FUNCTION();
       
    66 }
       
    67 
       
    68 
       
    69 
       
    70 /**
       
    71 * CxeViewfinderControlSymbian::~CxeViewfinderControlSymbian()
       
    72 */
       
    73 CxeViewfinderControlSymbian::~CxeViewfinderControlSymbian()
       
    74 {
       
    75     CX_DEBUG_ENTER_FUNCTION();
       
    76 
       
    77     // do something
       
    78     stop(); // stop the vf
       
    79     releaseCurrentViewfinder(); // release resoruces
       
    80 
       
    81     delete mVideoContainer;
       
    82     mUiWindow = NULL;
       
    83     mVideoWindow = NULL;
       
    84 
       
    85     CX_DEBUG_EXIT_FUNCTION();
       
    86 }
       
    87 
       
    88 
       
    89 
       
    90 
       
    91 /**
       
    92 * CxeViewfinderControlSymbian::setWindow
       
    93 */
       
    94 void CxeViewfinderControlSymbian::setWindow(WId windowId)
       
    95 {
       
    96     CX_DEBUG_ENTER_FUNCTION();
       
    97 
       
    98     mUiWindow = static_cast<CCoeControl*>(windowId)->DrawableWindow();
       
    99 
       
   100     CX_DEBUG(("mUiWindow is 0x%08x", mUiWindow));
       
   101     TSize windowSize = mUiWindow->Size();
       
   102 
       
   103     CX_DEBUG(("mUiWindow size %dx%d", windowSize.iWidth, windowSize.iHeight));
       
   104 
       
   105     // Set the window rect
       
   106     TPoint point = TPoint(0,0);
       
   107     mWindowRect = TRect(point, windowSize);
       
   108 
       
   109     CX_DEBUG(("CxeViewfinderControlSymbian::setWindow() mWindowRect iTl=(%d, %d) iBr=(%d, %d)",
       
   110               mWindowRect.iTl.iX, mWindowRect.iTl.iY, mWindowRect.iBr.iX, mWindowRect.iBr.iY));
       
   111 
       
   112     CX_DEBUG_EXIT_FUNCTION();
       
   113 }
       
   114 
       
   115 
       
   116 /*!
       
   117 * Returns Device's Display resolution
       
   118 */
       
   119 QSize CxeViewfinderControlSymbian::deviceDisplayResolution() const
       
   120 {
       
   121     TSize windowSize(640, 360); // magic: default size if mUiWindow is NULL
       
   122 
       
   123     if (mUiWindow) {
       
   124         windowSize = mUiWindow->Size();
       
   125     }
       
   126 
       
   127     QSize displaySize = QSize(windowSize.iWidth, windowSize.iHeight);
       
   128 
       
   129     if (displaySize.height() > displaySize.width()) {
       
   130         // Window server orientation might differ from the Orbit UI orientation.
       
   131         // Swap width and height if needed.
       
   132         displaySize.transpose();
       
   133     }
       
   134 
       
   135     CX_DEBUG(("deviceDisplayResolution returning %dx%d", displaySize.width(),
       
   136                                                          displaySize.height()));
       
   137 
       
   138     return displaySize;
       
   139 }
       
   140 
       
   141 
       
   142 /**
       
   143 * Stop viewfinder
       
   144 */
       
   145 void CxeViewfinderControlSymbian::stop()
       
   146 {
       
   147     CX_DEBUG_ENTER_FUNCTION();
       
   148 
       
   149     if (state() == Running) {
       
   150         CX_DEBUG_ASSERT( mCameraDevice.camera() );
       
   151         CX_DEBUG( ( "Viewfinder is running stopping it" ) );
       
   152         mCameraDevice.camera()->StopViewFinder();
       
   153         setState( Ready );
       
   154     }
       
   155 
       
   156     CX_DEBUG_EXIT_FUNCTION();
       
   157 }
       
   158 
       
   159 
       
   160 /**!
       
   161 * Start the viewfinder
       
   162 */
       
   163 CxeError::Id CxeViewfinderControlSymbian::start()
       
   164 {
       
   165     CX_DEBUG_ENTER_FUNCTION();
       
   166     TInt err = KErrNone;
       
   167 
       
   168     if ( state() == Running ) {
       
   169         OstTrace0( camerax_performance, CXEVIEWFINDERCONTROLSYMBIAN_START, "msg: e_CX_GO_TO_VIDEO_MODE 0" );
       
   170         CX_DEBUG( ( "Viewfinder already running - ignored start()" ) );
       
   171         CX_DEBUG_EXIT_FUNCTION();
       
   172         return CxeError::None;
       
   173     }
       
   174 
       
   175     if (!mVideoWindow) {
       
   176         TRAP(err, createViewfinderWindowL());
       
   177         if (err != KErrNone) {
       
   178             CX_DEBUG_EXIT_FUNCTION();
       
   179             return CxeErrorHandlingSymbian::map(err);
       
   180         }
       
   181     }
       
   182 
       
   183     if (state() == Uninitialized) {
       
   184         err = initViewfinder();
       
   185     }
       
   186 
       
   187     // apply any settings here
       
   188     if ( mDirectViewfinderInUse && !err && mDirectViewfinder && state() == Ready ) {
       
   189 
       
   190         // for now only direct vf
       
   191         switch( mDirectViewfinder->ViewFinderState() ) {
       
   192         case CCamera::CCameraDirectViewFinder::EViewFinderInActive:
       
   193             {
       
   194             CX_DEBUG(("Calling StartViewFinderDirectL"));
       
   195 
       
   196             // Make UI surface transparent so viewfinder is visible
       
   197             if (mUiWindow) {
       
   198                 mUiWindow->SetSurfaceTransparency(ETrue);
       
   199             }
       
   200 
       
   201             // A local copy of the viewfinder coordinates (in WServ
       
   202             // coordinates). Needed because calling StartViewFinderDirectL()
       
   203             // will modify the rectangle and we don't want mWindowRect to be
       
   204             // modified.
       
   205             TRect activeViewfinderRect = mWindowRect;
       
   206 
       
   207             CX_DEBUG(("activeViewfinderRect iTl=(%d, %d) iBr=(%d, %d)",
       
   208                 activeViewfinderRect.iTl.iX, activeViewfinderRect.iTl.iY,
       
   209                 activeViewfinderRect.iBr.iX, activeViewfinderRect.iBr.iY));
       
   210 
       
   211             TRAP(err, mCameraDevice.camera()->StartViewFinderDirectL(
       
   212                 CCoeEnv::Static()->WsSession(),
       
   213                 *CCoeEnv::Static()->ScreenDevice(),
       
   214                 *mVideoWindow, activeViewfinderRect));
       
   215             OstTrace0(camerax_performance, DUP1_CXEVIEWFINDERCONTROLSYMBIAN_START, "msg: e_CX_STARTUP 0");
       
   216             setState(Running);
       
   217             break;
       
   218             }
       
   219         case CCamera::CCameraDirectViewFinder::EViewFinderPause:
       
   220             TRAP( err, mDirectViewfinder->ResumeViewFinderDirectL() );
       
   221             if (!err) {
       
   222                 setState( Running );
       
   223             }
       
   224             break;
       
   225         case CCamera::CCameraDirectViewFinder::EViewFinderActive:
       
   226             // Already running. Not considered as error.
       
   227             setState( Running );
       
   228             break;
       
   229         default:
       
   230             err = KErrNotSupported;
       
   231         }
       
   232     }
       
   233     else if ( !mDirectViewfinder ) {
       
   234         // Start bmp vf
       
   235         //TSize size(320, 240);
       
   236         TSize size(mWindowRect.Width(), mWindowRect.Height());
       
   237         CX_DEBUG(("Starting bitmap vf with size %d x %d", size.iWidth, size.iHeight));
       
   238         TRAP( err, mCameraDevice.camera()->StartViewFinderBitmapsL( size ) );
       
   239         CX_DEBUG(("Bitmap viewfinder modified to size %d x %d", size.iWidth, size.iHeight));
       
   240         if ( err ) {
       
   241             CX_DEBUG( ("StartViewfinderBitmapsL - err:%d", err) );
       
   242         }
       
   243         else {
       
   244             setState( Running );
       
   245             TRAP( err, mCameraDevice.camera()->SetViewFinderMirrorL(true) );
       
   246         }
       
   247     }
       
   248     else {
       
   249         // vf already running. nothing to do
       
   250     }
       
   251 
       
   252     CX_DEBUG( ("CxeViewfinderControlSymbian::start symbian error code : %d", err ) );
       
   253     CX_DEBUG_EXIT_FUNCTION();
       
   254     return CxeErrorHandlingSymbian::map(err);
       
   255 }
       
   256 
       
   257 
       
   258 /**
       
   259 * Intialize the viewfinder based on the VF mode
       
   260 */
       
   261 int CxeViewfinderControlSymbian::initViewfinder()
       
   262 {
       
   263     CX_DEBUG_ENTER_FUNCTION();
       
   264 
       
   265     TInt err = KErrNone;
       
   266     if (state() != Uninitialized) {
       
   267         return err;
       
   268     }
       
   269 
       
   270     // For now only direct vf. If there is any need for supporting other VF modes, condition checks
       
   271     // and handling of new states are needed here.
       
   272 
       
   273     // release first, the prev/current view finder instance.
       
   274     releaseCurrentViewfinder();
       
   275 
       
   276     mDirectViewfinderInUse = true;
       
   277     /*
       
   278     if ( mCameraDeviceControl.cameraIndex() == Cxe::PrimaryCameraIndex ) {
       
   279         mDirectViewfinderInUse = true;
       
   280     }
       
   281     else {
       
   282         mDirectViewfinderInUse = false;
       
   283     }*/
       
   284 
       
   285     if ( mDirectViewfinderInUse ) {
       
   286         // creating an new instance of vf
       
   287         TRAP( err, mDirectViewfinder = CCamera::CCameraDirectViewFinder::NewL( *mCameraDevice.camera() ) );
       
   288     }
       
   289 
       
   290     if ( !err ) {
       
   291         setState( Ready );
       
   292     }
       
   293     else {
       
   294         CX_DEBUG( ("error: %d", err ) );
       
   295     }
       
   296 
       
   297 #if defined(CXE_USE_DUMMY_CAMERA) || defined(__WINSCW__)
       
   298     //! @todo Temporary code for WINSCW, because CCamera::CCameraDirectViewFinder is not yet supported by dummy engine
       
   299     setState( Ready );
       
   300 #endif
       
   301 
       
   302     CX_DEBUG_EXIT_FUNCTION();
       
   303     return err;
       
   304 }
       
   305 
       
   306 
       
   307 /**
       
   308 * Create the window for viewfinder to render into.
       
   309 */
       
   310 void CxeViewfinderControlSymbian::createViewfinderWindowL()
       
   311 {
       
   312     CX_DEBUG_ENTER_FUNCTION();
       
   313 
       
   314     if (!mUiWindow) {
       
   315         CX_DEBUG( ( "mUiWindow not set - cannot create VF window!" ) );
       
   316         User::Leave(KErrNotReady);
       
   317     }
       
   318 
       
   319     // Make UI surface transparent so viewfinder is visible
       
   320     if (mUiWindow) {
       
   321         mUiWindow->SetSurfaceTransparency(ETrue);
       
   322     }
       
   323 
       
   324     delete mVideoContainer;
       
   325     mVideoContainer = NULL;
       
   326     mVideoContainer = new (ELeave) CxeVideoContainer();
       
   327     mVideoContainer->ConstructL();
       
   328     CX_DEBUG(("Viewfinder container created ok"));
       
   329 
       
   330     mVideoContainer->SetRect(mWindowRect);
       
   331     mVideoWindow = mVideoContainer->DrawableWindow();
       
   332     CX_DEBUG(("mVideoWindow is 0x%08x", mVideoWindow));
       
   333     CX_DEBUG(("mUiWindow ordinal position is: %d", mUiWindow->OrdinalPosition()));
       
   334 
       
   335     // Make sure UI window is on top of viewfinder
       
   336     CX_DEBUG(("Set viewfinder window ordinal.."));
       
   337     mVideoWindow->SetOrdinalPosition(-1);
       
   338     mUiWindow->SetOrdinalPosition(0);
       
   339 
       
   340     CX_DEBUG(("mVideoWindow ordinal position is: %d", mVideoWindow->OrdinalPosition()));
       
   341     CX_DEBUG(("mUiWindow ordinal position is: %d", mUiWindow->OrdinalPosition()));
       
   342 
       
   343     CX_DEBUG_EXIT_FUNCTION();
       
   344 }
       
   345 
       
   346 
       
   347 void CxeViewfinderControlSymbian::prepareForRelease()
       
   348 {
       
   349     CX_DEBUG_ENTER_FUNCTION();
       
   350     stop();
       
   351     CX_DEBUG_EXIT_FUNCTION();
       
   352 }
       
   353 
       
   354 /**
       
   355 * Release Vf resources
       
   356 */
       
   357 void CxeViewfinderControlSymbian::releaseCurrentViewfinder()
       
   358 {
       
   359     CX_DEBUG_ENTER_FUNCTION();
       
   360 
       
   361     delete mDirectViewfinder;
       
   362     mDirectViewfinder = NULL;
       
   363     setState( Uninitialized );
       
   364 
       
   365     CX_DEBUG_EXIT_FUNCTION();
       
   366 }
       
   367 
       
   368 /**
       
   369 * Ecam reference changing, release resources
       
   370 */
       
   371 void CxeViewfinderControlSymbian::prepareForCameraDelete()
       
   372 {
       
   373     CX_DEBUG_ENTER_FUNCTION();
       
   374     stop(); // first stop the viewfinder
       
   375     releaseCurrentViewfinder();
       
   376     CX_DEBUG_EXIT_FUNCTION();
       
   377 }
       
   378 
       
   379 
       
   380 /**
       
   381 * Handle new camera instance.
       
   382 */
       
   383 void CxeViewfinderControlSymbian::handleCameraAllocated(CxeError::Id error)
       
   384 {
       
   385     CX_DEBUG_ENTER_FUNCTION();
       
   386     if (!error) {
       
   387         initViewfinder();
       
   388     }
       
   389     CX_DEBUG_EXIT_FUNCTION();
       
   390 }
       
   391 
       
   392 CxeViewfinderControl::State CxeViewfinderControlSymbian::state() const
       
   393 {
       
   394     return static_cast<State>( stateId() );
       
   395 }
       
   396 
       
   397 void CxeViewfinderControlSymbian::handleStateChanged( int newStateId, CxeError::Id error )
       
   398 {
       
   399     emit stateChanged( static_cast<State>( newStateId ), error );
       
   400 }
       
   401 
       
   402 void CxeViewfinderControlSymbian::initializeStates()
       
   403 {
       
   404     // addState( id, name, allowed next states )
       
   405     addState( new CxeState( Uninitialized , "Uninitialized", Ready ) );
       
   406     addState( new CxeState( Ready , "Ready", Uninitialized | Running ) );
       
   407     addState( new CxeState( Running , "Running", Uninitialized | Ready ) );
       
   408 
       
   409     setInitialState( Uninitialized );
       
   410 }
       
   411 
       
   412 void CxeViewfinderControlSymbian::handleVfFrame(MCameraBuffer* /*buffer*/, int /*error*/)
       
   413 {
       
   414     CX_DEBUG_IN_FUNCTION();
       
   415     CX_DEBUG( ( "Bitmap viewfinder not supported" ) );
       
   416     CX_ASSERT_ALWAYS(false);
       
   417 }
       
   418 
       
   419 // end of file