qtmobility/plugins/multimedia/symbian/camera/s60camerasession.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <BADESCA.H>
       
    43 
       
    44 #include <QtCore/qdebug.h>
       
    45 #include <QtCore/qstring.h>
       
    46 #include "s60camerasession.h"
       
    47 #include "s60viewfinderwidget.h"
       
    48 #include "s60videoencoder.h"
       
    49 #include "s60cameraservice.h"
       
    50 
       
    51 #include <fbs.h>
       
    52 #include <qglobal.h>
       
    53 #include <QDir>
       
    54 
       
    55 const int KSymbianImageQualityCoefficient = 25;
       
    56 
       
    57 #ifdef Q_CC_NOKIAX86
       
    58 _LIT8(KCameraTemp,"test data");
       
    59 #endif
       
    60 
       
    61 _LIT8(KMimeTypeMPEG4VSPL3, "video/mp4v-es; profile-level-id=3");  // MPEG-4 Visual Simple Profile Level 3
       
    62 _LIT8(KMimeTypeMPEG4VSPL4, "video/mp4v-es; profile-level-id=4");  // MPEG-4 Visual Simple Profile Level 4    
       
    63 
       
    64 
       
    65 S60CameraSession::S60CameraSession(QObject *parent)
       
    66     : QObject(parent)
       
    67     , m_cameraEngine(NULL)
       
    68     , m_advancedSettings(NULL)
       
    69     , m_VFProcessor(NULL)
       
    70     , m_imageQuality(QtMedia::NormalQuality*KSymbianImageQualityCoefficient)
       
    71     , m_videoQuality(QtMedia::LowQuality*KSymbianImageQualityCoefficient)
       
    72     , m_captureSize(QSize())
       
    73     , m_state(QCamera::StoppedState)
       
    74     , m_pixelF(QVideoFrame::Format_RGB24)
       
    75     , m_deviceIndex(NULL)
       
    76     , m_error(NoError)
       
    77     , m_videoUtility(NULL)
       
    78     , m_VFWidgetSize(QSize())
       
    79     , m_captureState(ENotInitialized)
       
    80 {   
       
    81     // set defaults so that camera works with both devices..
       
    82     m_currentcodec = defaultCodec();
       
    83     // create initial camera
       
    84     resetCamera();
       
    85 }
       
    86 
       
    87 S60CameraSession::~S60CameraSession()
       
    88 {
       
    89     delete m_videoUtility;
       
    90     m_videoUtility = NULL;
       
    91     delete m_advancedSettings;
       
    92     m_advancedSettings = NULL;
       
    93     delete m_cameraEngine;
       
    94     m_cameraEngine = NULL;
       
    95 }
       
    96 CCamera::TFormat S60CameraSession::defaultCodec()
       
    97 {
       
    98     if (m_deviceIndex == 0) {
       
    99         return CCamera::EFormatExif;
       
   100     }
       
   101     else {
       
   102         return CCamera::EFormatFbsBitmapColor64K;
       
   103     }
       
   104 }
       
   105 S60CameraSettings* S60CameraSession::advancedSettings()
       
   106 {
       
   107     return m_advancedSettings;
       
   108 }
       
   109 
       
   110 void S60CameraSession::resetCamera()
       
   111 {
       
   112     //qDebug() << "S60CameraSession::resetCamera START";
       
   113     delete m_videoUtility;
       
   114     m_videoUtility = NULL;
       
   115     delete m_advancedSettings;
       
   116     m_advancedSettings = NULL;
       
   117     delete m_cameraEngine;
       
   118     m_cameraEngine = NULL;  
       
   119     m_error = KErrNone;
       
   120     m_state = QCamera::StoppedState;
       
   121     //qDebug() << "S60CameraSession::resetCamera. Creating new camera with index=" << m_deviceIndex;
       
   122     TRAPD(err, 
       
   123         m_cameraEngine = CCameraEngine::NewL(m_deviceIndex, 0, this);
       
   124         m_advancedSettings = new S60CameraSettings(this, m_cameraEngine);
       
   125         m_videoUtility = CVideoRecorderUtility::NewL(*this);
       
   126     );
       
   127     setError(err);
       
   128 
       
   129     updateVideoCaptureCodecs();
       
   130     
       
   131     initializeVideoCaptureSettings();
       
   132     
       
   133     //qDebug() << "S60CameraSession::resetCamera END";
       
   134 }
       
   135 
       
   136 void S60CameraSession::setError(TInt aError)
       
   137 {
       
   138     if (aError == KErrNone)
       
   139         return;
       
   140         
       
   141     m_error = aError;
       
   142     QCamera::Error cameraError = fromSymbianErrorToMultimediaError(m_error);
       
   143     
       
   144     // TODO: fix to user friendly string at some point
       
   145     // These error string are only dev usable
       
   146     QString symbianError; 
       
   147     symbianError.append("Symbian:");
       
   148     symbianError.append(QString::number(m_error));
       
   149     emit error(cameraError, symbianError);
       
   150 }
       
   151 
       
   152 QCamera::Error S60CameraSession::fromSymbianErrorToMultimediaError(int aError)
       
   153 {
       
   154     switch(aError) {
       
   155         case KErrNoMemory:
       
   156         case KErrNotFound:
       
   157         case KErrBadHandle:
       
   158             return QCamera::ServiceMissingError;
       
   159         case KErrNotSupported:
       
   160         case KErrECamSettingNotSupported:
       
   161         case KErrECamParameterNotInRange:
       
   162             return QCamera::NotSupportedFeatureError;
       
   163         case KErrCorrupt:
       
   164         case KErrECamSettingDisabled:
       
   165         case KErrECamNotOptimalFocus:
       
   166         case KErrAccessDenied:
       
   167         case KErrLocked:
       
   168         case KErrPermissionDenied:
       
   169             return QCamera::CameraError;
       
   170         case KErrNotReady:
       
   171         case KErrECamCameraDisabled:
       
   172         case KErrInUse:
       
   173             return QCamera::NotReadyToCaptureError;
       
   174         default:
       
   175             return QCamera::NoError;
       
   176     }
       
   177 }
       
   178 
       
   179 void S60CameraSession::startCamera()
       
   180 {
       
   181     //qDebug() << "S60CameraSession::startCamera START";
       
   182     #ifdef Q_CC_NOKIAX86
       
   183     MceoCameraReady(); // signal that we are ready
       
   184     #endif
       
   185 
       
   186     // create camera engine
       
   187     resetCamera();
       
   188 
       
   189     if (!m_error ) {
       
   190         //qDebug() << "S60CameraSession::startCamera, ReserveAndPowerOn"<< m_error;
       
   191         m_cameraEngine->ReserveAndPowerOn();
       
   192     }
       
   193     //qDebug() << "S60CameraSession::startCamera END";
       
   194 }
       
   195 
       
   196 void S60CameraSession::stopCamera()
       
   197 {
       
   198     //qDebug() <<  "Stopping camera";
       
   199     m_state = QCamera::StoppedState;
       
   200 
       
   201     if (m_cameraEngine) {
       
   202         m_cameraEngine->ReleaseAndPowerOff();
       
   203     }
       
   204     emit stateChanged(m_state);
       
   205 }
       
   206 
       
   207 void S60CameraSession::capture(const QString &fileName)
       
   208 {
       
   209     //qDebug() << "S60CameraSession::capture to file="<< fileName;
       
   210     m_error = KErrNone;
       
   211     m_stillCaptureFileName = fileName;
       
   212     emit readyForCaptureChanged(false);
       
   213 
       
   214     if (m_stillCaptureFileName.isNull() || m_stillCaptureFileName.isEmpty() ) {
       
   215         emit error(QStillImageCapture::ResourceError, QLatin1String("capture outputlocation not set properly"));
       
   216         return;
       
   217     }
       
   218     // check capture size //if not set sets the default one
       
   219     setCaptureSize(m_captureSize);
       
   220 
       
   221     if (m_cameraEngine) {
       
   222         TSize size(m_captureSize.width(), m_captureSize.height());
       
   223         TRAPD(err, 
       
   224                 m_cameraEngine->PrepareL(size, m_currentcodec);       
       
   225                 m_cameraEngine->CaptureL();
       
   226         );
       
   227         setError(err);
       
   228     }
       
   229     else {
       
   230         setError(KErrNotReady);
       
   231     }
       
   232     #ifdef Q_CC_NOKIAX86
       
   233     QImage *snapImage = new QImage(QLatin1String("C:/Data/testimage.jpg"));
       
   234     emit imageCaptured(m_stillCaptureFileName, *snapImage);
       
   235     #endif
       
   236 }
       
   237 
       
   238 bool S60CameraSession::deviceReady()
       
   239 {
       
   240     #ifdef Q_CC_NOKIAX86
       
   241     //qDebug() << "device ready";
       
   242     return true;
       
   243     #endif
       
   244     if ( m_cameraEngine )
       
   245         return m_cameraEngine->IsCameraReady();
       
   246     else
       
   247         return EFalse;
       
   248 }
       
   249 
       
   250 qreal S60CameraSession::framerate()
       
   251 {
       
   252     if (m_videoUtility) {
       
   253         int rate = 0;
       
   254         TRAPD(err, rate = m_videoUtility->VideoFrameRateL());
       
   255         setError(err);
       
   256         return rate;
       
   257     }
       
   258     return -1;
       
   259 }
       
   260 
       
   261 void S60CameraSession::setFrameRate(qreal rate)
       
   262 {
       
   263     if (m_videoUtility) {
       
   264         TRAPD(err, m_videoUtility->SetVideoFrameRateL(rate));
       
   265         setError(err);
       
   266     }
       
   267 }
       
   268 
       
   269 QList<QVideoFrame::PixelFormat> S60CameraSession::supportedPixelFormats()
       
   270 {
       
   271 
       
   272     QList<QVideoFrame::PixelFormat> list;
       
   273     #ifdef Q_CC_NOKIAX86
       
   274     list << QVideoFrame::Format_RGB565;
       
   275     #endif
       
   276     //TODO: fix supportedformats
       
   277     //qDebug() << "S60CameraSession::pixeformat, returning="<<list;
       
   278     return list;
       
   279 }
       
   280 
       
   281 QVideoFrame::PixelFormat S60CameraSession::pixelFormat() const
       
   282 {
       
   283     //qDebug() << "S60CameraSession::pixeformat, returning="<<m_pixelF;
       
   284     return m_pixelF;
       
   285 }
       
   286 
       
   287 void S60CameraSession::setPixelFormat(QVideoFrame::PixelFormat fmt)
       
   288 {
       
   289     //qDebug() << "S60CameraSession::setPixelFormat, format="<<fmt;
       
   290     m_pixelF = fmt;
       
   291 }
       
   292 
       
   293 QList<QSize> S60CameraSession::supportedVideoResolutions()
       
   294 {
       
   295     //qDebug() << "S60CameraSession::supportedVideoResolutions";
       
   296     QList<QSize> list;
       
   297     // if we have cameraengine loaded and we can update camerainfo
       
   298     if (m_cameraEngine && queryCurrentCameraInfo()) {
       
   299         
       
   300         foreach (QString codecName, formatMap().keys()) {
       
   301             int codecIndex = formatMap().value(codecName);
       
   302             CCamera::TFormat format = static_cast<CCamera::TFormat>( codecIndex );
       
   303             CCamera *camera = m_cameraEngine->Camera();
       
   304     
       
   305             TUint32 videoFormatsSupported = m_info.iVideoFrameFormatsSupported;
       
   306     
       
   307             if (videoFormatsSupported&format) {
       
   308                 CCamera *camera = m_cameraEngine->Camera();
       
   309                 for (int i=0; i < m_info.iNumVideoFrameSizesSupported; i++) {
       
   310                     TSize size;
       
   311                     camera->EnumerateVideoFrameSizes(size,i, format );
       
   312                     QSize qSize(size.iWidth, size.iHeight);
       
   313                     if (!list.contains(qSize))
       
   314                         list << QSize(size.iWidth, size.iHeight);
       
   315                 }
       
   316             }
       
   317         }
       
   318     }
       
   319     #ifdef Q_CC_NOKIAX86
       
   320     list << QSize(50, 50);
       
   321     list << QSize(100, 100);
       
   322     list << QSize(800,600);
       
   323     #endif
       
   324     return list;
       
   325 }
       
   326 
       
   327 QList<qreal> S60CameraSession::supportedVideoFrameRates()
       
   328 {
       
   329     //qDebug() << "S60CameraSession::supportedVideoResolutions";
       
   330     QList<qreal> list;
       
   331     // if we have cameraengine loaded and we can update camerainfo
       
   332     if (m_cameraEngine && queryCurrentCameraInfo()) {
       
   333         
       
   334         foreach (QString codecName, formatMap().keys()) {
       
   335             int codecIndex = formatMap().value(codecName);
       
   336             CCamera::TFormat format = static_cast<CCamera::TFormat>( codecIndex );
       
   337             CCamera *camera = m_cameraEngine->Camera();
       
   338     
       
   339             TUint32 videoFormatsSupported = m_info.iVideoFrameFormatsSupported;
       
   340     
       
   341             if (videoFormatsSupported&format) {
       
   342                 CCamera *camera = m_cameraEngine->Camera();
       
   343                 for (int i=0; i < m_info.iNumVideoFrameSizesSupported; i++) {
       
   344                     TReal32 rate;
       
   345                     TInt maxSizeIndex;
       
   346                     camera->EnumerateVideoFrameRates(rate, i,format, maxSizeIndex);
       
   347                     if (!list.contains(rate)) {
       
   348                         if (rate > 0.0)
       
   349                             list << rate;
       
   350                     }
       
   351                 }
       
   352             }
       
   353         }
       
   354     }
       
   355     #ifdef Q_CC_NOKIAX86
       
   356     list << 30.0 << 25.0 << 15.0 << 10.0 << 5.0;
       
   357     #endif
       
   358     return list;
       
   359 }
       
   360 
       
   361 
       
   362 bool S60CameraSession::setOutputLocation(const QUrl &sink)
       
   363 {
       
   364     //qDebug() << "S60CameraSession::setOutputlocation";
       
   365     m_sink = sink;
       
   366     return true;
       
   367 }
       
   368 
       
   369 QUrl S60CameraSession::outputLocation() const
       
   370 {
       
   371     //qDebug() << "S60CameraSession::outputLocation";
       
   372     return m_sink;
       
   373 }
       
   374 
       
   375 qint64 S60CameraSession::position()
       
   376 {
       
   377     //qDebug() << "S60CameraSession::position";
       
   378     qint64 position = 0;
       
   379     if ( (m_captureState == ERecording) && m_videoUtility) {
       
   380         TRAPD(err, position = m_videoUtility->DurationL().Int64() / 1000);
       
   381         setError(err);
       
   382     }
       
   383     return position;
       
   384 }
       
   385 
       
   386 int S60CameraSession::state() const
       
   387 {
       
   388     //qDebug() << "S60CameraSession::state";
       
   389     return m_state;
       
   390 }
       
   391 
       
   392 void S60CameraSession::commitVideoEncoderSettings()
       
   393 {          
       
   394     setVideoResolution(m_videoSettings.resolution());
       
   395     setFrameRate(m_videoSettings.frameRate());    
       
   396     setBitrate(m_videoSettings.bitRate());
       
   397 }
       
   398 
       
   399 void S60CameraSession::setVideoFrameRateFixed(bool fixed)
       
   400 {
       
   401 #ifndef PRE_S60_50_PLATFORM
       
   402     if (m_videoUtility) {
       
   403         TRAPD(err, m_videoUtility->SetVideoFrameRateFixedL(fixed));
       
   404         setError(err);
       
   405     }
       
   406 #endif //PRE_S60_50_PLATFORM
       
   407 }
       
   408 
       
   409 void S60CameraSession::saveVideoEncoderSettings(QVideoEncoderSettings &videoSettings)
       
   410 {
       
   411     m_videoSettings = videoSettings;
       
   412 }
       
   413 
       
   414 void S60CameraSession::getCurrentVideoEncoderSettings(QVideoEncoderSettings &videoSettings)
       
   415 {
       
   416     videoSettings = m_videoSettings;
       
   417 }
       
   418 
       
   419 QtMedia::EncodingQuality S60CameraSession::videoCaptureQuality() const
       
   420 {  
       
   421 #ifndef PRE_S60_50_PLATFORM
       
   422     if (m_videoQuality == EVideoQualityLow) 
       
   423         return QtMedia::LowQuality;
       
   424     else if (m_videoQuality == EVideoQualityNormal)
       
   425         return QtMedia::NormalQuality;
       
   426     else if (m_videoQuality == EVideoQualityHigh)
       
   427         return QtMedia::HighQuality;
       
   428     else if (m_videoQuality == EVideoQualityLossless)
       
   429         return QtMedia::VeryHighQuality;        
       
   430     else
       
   431 #endif //PRE_S60_50_PLATFORM        
       
   432         return QtMedia::VeryLowQuality;       
       
   433 }
       
   434 
       
   435 void S60CameraSession::setVideoCaptureQuality(QtMedia::EncodingQuality quality)
       
   436 {            
       
   437         m_videoQuality = quality*KSymbianImageQualityCoefficient;
       
   438 #ifndef PRE_S60_50_PLATFORM        
       
   439         TRAPD(err, m_videoUtility->SetVideoQualityL(m_videoQuality));
       
   440         setError(err);
       
   441 #endif //PRE_S60_50_PLATFORM
       
   442 }
       
   443 
       
   444 void S60CameraSession::startRecording()
       
   445 {
       
   446     if (m_captureState == ENotInitialized || m_captureState == ERecordComplete) {
       
   447         QString filename = QDir::toNativeSeparators(m_sink.toString());
       
   448         TPtrC16 sink(reinterpret_cast<const TUint16*>(filename.utf16()));
       
   449         
       
   450         m_videoCodec = m_videoSettings.codec();
       
   451     
       
   452         int cameraHandle = m_cameraEngine->Camera()->Handle();
       
   453     
       
   454         TUid controllerUid(TUid::Uid(m_videoControllerMap[m_videoCodec].controllerUid));
       
   455         TUid formatUid(TUid::Uid(m_videoControllerMap[m_videoCodec].formatUid));
       
   456     
       
   457         TRAPD(err, m_videoUtility->OpenFileL(sink, cameraHandle, controllerUid, formatUid, KMimeTypeMPEG4VSPL4, KMMFFourCCCodeAAC));
       
   458         setError(err);
       
   459         m_captureState = EInitialized;
       
   460     } else if (m_captureState == EPaused) {
       
   461         m_videoUtility->Record();
       
   462         m_captureState = ERecording;        
       
   463     }
       
   464 }
       
   465 
       
   466 void S60CameraSession::pauseRecording()
       
   467 {
       
   468     if (m_captureState == ERecording) {
       
   469         TRAPD(err, m_videoUtility->PauseL());
       
   470         setError(err);
       
   471         m_captureState = EPaused;
       
   472     } else if (m_captureState == EPaused) {
       
   473         m_videoUtility->Record();
       
   474         m_captureState = ERecording;
       
   475     }
       
   476 }
       
   477 
       
   478 void S60CameraSession::stopRecording()
       
   479 {
       
   480     m_videoUtility->Stop();
       
   481     m_videoUtility->Close();
       
   482     m_captureState = ERecordComplete;
       
   483 }
       
   484 
       
   485 void S60CameraSession::MceoCameraReady()
       
   486 {
       
   487     //qDebug() << "S60CameraSession::MCeoCameraReady()";
       
   488     m_state = QCamera::ActiveState;
       
   489     emit stateChanged(m_state);
       
   490     if (m_cameraEngine) {
       
   491         m_VFSize =  TSize(m_VFWidgetSize.width(), m_VFWidgetSize.height());
       
   492         TRAPD(err, m_cameraEngine->StartViewFinderL(m_VFSize));
       
   493         if (err == KErrNotReady || err == KErrNoMemory) {
       
   494             emit readyForCaptureChanged(false);
       
   495         }
       
   496         setError(err);
       
   497         emit readyForCaptureChanged(true);
       
   498     }
       
   499 }
       
   500 
       
   501 void S60CameraSession::MceoFocusComplete()
       
   502 {
       
   503     //qDebug() << "S60CameraSession::MCeoFocusComplete()";
       
   504     emit focusStatusChanged(QCamera::FocusReached);
       
   505 }
       
   506 
       
   507 void S60CameraSession::MceoCapturedDataReady(TDesC8* aData)
       
   508 {
       
   509     //qDebug() << "S60CameraSession::MceoCapturedDataReady()";
       
   510     QImage snapImage = QImage::fromData((const uchar *)aData->Ptr(), aData->Length());
       
   511     //qDebug() << "S60CameraSession::MceoCapturedDataReady(), image constructed, byte count="<<snapImage.byteCount();
       
   512     // inform capture done
       
   513     emit imageCaptured(m_stillCaptureFileName, snapImage);
       
   514     // try to save image and inform if it was succcesful
       
   515     if (snapImage.save(m_stillCaptureFileName,0, m_imageQuality))
       
   516         emit imageSaved(m_stillCaptureFileName);
       
   517     // release image resources
       
   518     releaseImageBuffer();
       
   519 }
       
   520 
       
   521 void S60CameraSession::releaseImageBuffer()
       
   522 {
       
   523     if (m_cameraEngine)
       
   524         m_cameraEngine->ReleaseImageBuffer();
       
   525     // inform that we can continue taking more pictures
       
   526     emit readyForCaptureChanged(true);
       
   527 }
       
   528 
       
   529 void S60CameraSession::MceoCapturedBitmapReady(CFbsBitmap* aBitmap)
       
   530 {
       
   531     //qDebug() << "S60CameraSession::MceoCapturedBitmapReady()";
       
   532     if(aBitmap)
       
   533     {
       
   534         TSize size = aBitmap->SizeInPixels();
       
   535         TUint32 sizeInWords = size.iHeight * CFbsBitmap::ScanLineLength(size.iWidth, aBitmap->DisplayMode()) / sizeof( TUint32 );
       
   536         //qDebug() << "S60CameraSession::MceoCapturedDataReady(), image constructed";
       
   537         TUint32 *pixelData = new TUint32[sizeInWords];
       
   538 
       
   539         if (!pixelData)
       
   540             return;
       
   541 
       
   542         // convert to QImage
       
   543         aBitmap->LockHeap();
       
   544         TUint32 *dataPtr = aBitmap->DataAddress();
       
   545         memcpy(pixelData, dataPtr, sizeof(TUint32) * sizeInWords);
       
   546         aBitmap->UnlockHeap();
       
   547 
       
   548         TDisplayMode displayMode = aBitmap->DisplayMode();
       
   549 
       
   550         QImage::Format format = QImage::Format_Invalid;
       
   551         switch(displayMode)
       
   552         {
       
   553             case EColor256:
       
   554                 format = QImage::Format_Indexed8;
       
   555                 break;
       
   556             case EColor4K:
       
   557                 format = QImage::Format_RGB444;
       
   558                 break;
       
   559             case EColor64K:
       
   560                 format = QImage::Format_RGB16;
       
   561                 break;
       
   562             case EColor16M:
       
   563                 format = QImage::Format_RGB666;
       
   564                 break;
       
   565             case EColor16MU:
       
   566                 format = QImage::Format_RGB32;
       
   567                 break;
       
   568             case EColor16MA:
       
   569                 format = QImage::Format_ARGB32;
       
   570                 break;
       
   571             default:
       
   572                 //User::Leave( -1 );
       
   573                 break;
       
   574         }
       
   575 
       
   576         QImage *snapImage = new QImage(
       
   577                 (uchar*)pixelData,
       
   578                 size.iWidth,
       
   579                 size.iHeight,
       
   580                 CFbsBitmap::ScanLineLength(size.iWidth, aBitmap->DisplayMode()),
       
   581                 format);
       
   582         //qDebug() << "S60CameraSession::MceoCapturedDataReady(), image constructed, byte count="<<snapImage->byteCount();
       
   583         aBitmap = NULL;
       
   584 
       
   585         emit imageCaptured(m_stillCaptureFileName, *snapImage);
       
   586         // try to save image and inform if it was succcesful
       
   587         if ( snapImage->save(m_stillCaptureFileName,0, m_imageQuality) )
       
   588             emit imageSaved(m_stillCaptureFileName);
       
   589 
       
   590         releaseImageBuffer();
       
   591     }
       
   592     //todo error handling
       
   593 }
       
   594 
       
   595 void S60CameraSession::MceoViewFinderFrameReady(CFbsBitmap& aFrame)
       
   596 {
       
   597     if (m_VFProcessor) {
       
   598         int bytesPerLine = aFrame.ScanLineLength(m_VFSize.iWidth, aFrame.DisplayMode());
       
   599 
       
   600         QImage image((uchar *)aFrame.DataAddress(), m_VFSize.iWidth,
       
   601                 m_VFSize.iHeight, bytesPerLine, QImage::Format_RGB32);
       
   602 
       
   603         m_VFProcessor->ViewFinderFrameReady(image);
       
   604      }
       
   605 
       
   606      m_cameraEngine->ReleaseViewFinderBuffer();
       
   607 }
       
   608 
       
   609 void S60CameraSession::MceoHandleError(TCameraEngineError aErrorType, TInt aError)
       
   610 {
       
   611     //qDebug() << "S60CameraSession::MceoHandleError, errorType"<<aErrorType;
       
   612     //qDebug() << "S60CameraSession::MceoHandleError, aError"<<aError;
       
   613     Q_UNUSED(aErrorType);
       
   614     setError(aError);
       
   615 }
       
   616 
       
   617 // For S60Cameravideodevicecontrol
       
   618 int S60CameraSession::deviceCount()
       
   619 {
       
   620     //qDebug() << "S60CameraSession::deviceCount(for emulator this is always 1)";
       
   621     #ifdef Q_CC_NOKIAX86
       
   622     return 1;
       
   623     #endif
       
   624     return CCameraEngine::CamerasAvailable();
       
   625 }
       
   626 /**
       
   627  * Some names for cameras with index
       
   628  */
       
   629 QString S60CameraSession::name(const int index)
       
   630 {
       
   631     //TODO: change these to use querycurrentcamerainfo
       
   632     // From where does the naming index start
       
   633     QString cameraName;
       
   634     switch (index) {
       
   635         case 0:
       
   636             cameraName = QLatin1String("Main camera");
       
   637         break;
       
   638         case 1:
       
   639             cameraName = QLatin1String("Secondary camera");
       
   640         break;
       
   641         case 2:
       
   642             cameraName = QLatin1String("Tertiary camera");
       
   643         break;
       
   644         default:
       
   645             cameraName = QLatin1String("Default camera");
       
   646         break;
       
   647     }
       
   648     return cameraName;
       
   649 }
       
   650 QString S60CameraSession::description(const int index)
       
   651 {
       
   652     //TODO: change these to use querycurrentcamerainfo
       
   653     // what information is wanted throuhg this call?
       
   654     QString cameraDesc;
       
   655     switch (index) {
       
   656         case 0:
       
   657             cameraDesc = QLatin1String("Back camera");
       
   658         break;
       
   659         case 1:
       
   660             cameraDesc = QLatin1String("Front camera");
       
   661         break;
       
   662         case 2:
       
   663             cameraDesc = QLatin1String("Tertiary camera description");
       
   664         break;
       
   665         default:
       
   666             cameraDesc = QLatin1String("Default camera description");
       
   667         break;
       
   668     }
       
   669     return cameraDesc;
       
   670 }
       
   671 
       
   672 int S60CameraSession::defaultDevice() const
       
   673 {
       
   674     //First camera is the default
       
   675     const TInt defaultCameraDevice = 0;
       
   676     return defaultCameraDevice;
       
   677 }
       
   678 int S60CameraSession::selectedDevice() const
       
   679 {
       
   680     //qDebug() << "S60CameraSession::selectedDevice returning="<<m_deviceIndex;
       
   681     return m_deviceIndex;
       
   682 }
       
   683 void S60CameraSession::setSelectedDevice(int index)
       
   684 {
       
   685     //qDebug() << "S60CameraSession::setSelectedDevice,index="<<index;
       
   686     m_deviceIndex = index;
       
   687 }
       
   688 
       
   689 /*
       
   690  * Queries all kinds of camera properties
       
   691  * Results are returned to member variable m_info
       
   692  * @return boolean indicating if querying the info was a success
       
   693  */
       
   694 bool S60CameraSession::queryCurrentCameraInfo()
       
   695 {
       
   696     //qDebug() << "S60CameraSession::queryCameraInfo";
       
   697 
       
   698     /** Version number and name of camera hardware. */
       
   699     //TVersion iHardwareVersion;
       
   700     /** Version number and name of camera software (device driver). */
       
   701     //TVersion iSoftwareVersion;
       
   702     /** Orientation of this particular camera device. */
       
   703     //TCameraOrientation iOrientation;
       
   704     bool returnValue = false;
       
   705 
       
   706     if (m_cameraEngine) {
       
   707         CCamera *camera = m_cameraEngine->Camera();
       
   708         if (camera) {
       
   709             camera->CameraInfo(m_info);
       
   710         }
       
   711         returnValue = true;
       
   712     }
       
   713     return returnValue;
       
   714 }
       
   715 
       
   716 // End for S60Cameravideodevicecontrol
       
   717 QSize S60CameraSession::captureSize() const
       
   718 {
       
   719     //qDebug() << "S60CameraSession::captureSize";
       
   720     return m_captureSize;
       
   721 }
       
   722 QSize S60CameraSession::minimumCaptureSize()
       
   723 {
       
   724     //qDebug() << "S60CameraSession::minimunCaptureSize";
       
   725     return supportedCaptureSizesForCodec(formatMap().key(m_currentcodec)).first();
       
   726 }
       
   727 QSize S60CameraSession::maximumCaptureSize()
       
   728 {
       
   729     //qDebug() << "S60CameraSession::maximumCaptureSize";
       
   730     return supportedCaptureSizesForCodec(formatMap().key(m_currentcodec)).last();
       
   731 }
       
   732 
       
   733 void S60CameraSession::setCaptureSize(const QSize &size)
       
   734 {
       
   735     //qDebug() << "S60CameraSession::setCaptureSizes, size="<<size;
       
   736     if (m_captureSize.isNull() || size.isEmpty()) {
       
   737         //an empty QSize indicates the encoder should make an optimal choice based on what is
       
   738         //available from the image source and the limitations of the codec.
       
   739         m_captureSize = supportedCaptureSizesForCodec(formatMap().key(m_currentcodec)).last();
       
   740         //qDebug() << "S60CameraSession::setCaptureSizes, using optimal(last) size="<<m_captureSize;
       
   741     }
       
   742     else
       
   743         m_captureSize = size;
       
   744     //qDebug() << "S60CameraSession::setCaptureSizes, END size="<<size;
       
   745 }
       
   746 
       
   747 QList<QSize> S60CameraSession::supportedCaptureSizesForCodec(const QString &codecName)
       
   748 {
       
   749     //qDebug() << "S60CameraSession::supportedCaptureSizesForCodec, codec="<<codecName;
       
   750     QList<QSize> list;
       
   751     // if we have cameraengine loaded and we can update camerainfo
       
   752     if (m_cameraEngine && queryCurrentCameraInfo()) {
       
   753         int codecIndex = formatMap().value(codecName);
       
   754         CCamera::TFormat format = static_cast<CCamera::TFormat>( codecIndex );
       
   755         CCamera *camera = m_cameraEngine->Camera();
       
   756         for (int i=0; i < m_info.iNumImageSizesSupported; i++) {
       
   757             TSize size;
       
   758             camera->EnumerateVideoFrameSizes(size,i, format );
       
   759             list << QSize(size.iWidth, size.iHeight);
       
   760         }
       
   761     }
       
   762     #ifdef Q_CC_NOKIAX86
       
   763     list << QSize(50, 50);
       
   764     list << QSize(100, 100);
       
   765     list << QSize(800,600);
       
   766     #endif
       
   767     return list;
       
   768 
       
   769 }
       
   770 
       
   771 QMap<QString, int> S60CameraSession::formatMap()
       
   772 {
       
   773     QMap<QString, int> formats;
       
   774     // format list copied from ecam.h CCamera::TFormat
       
   775   
       
   776     formats.insert("Monochrome", 0x0001);
       
   777     formats.insert("16bitRGB444", 0x0002);
       
   778     formats.insert("16BitRGB565", 0x0004);
       
   779     formats.insert("32BitRGB888", 0x0008);
       
   780     formats.insert("Jpeg", 0x0010);
       
   781     formats.insert("Exif", 0x0020);
       
   782     formats.insert("FbsBitmapColor4K", 0x0040);
       
   783     formats.insert("FbsBitmapColor64K", 0x0080);
       
   784     formats.insert("FbsBitmapColor16M", 0x0100);
       
   785     formats.insert("UserDefined", 0x0200);
       
   786     formats.insert("YUV420Interleaved", 0x0400);
       
   787     formats.insert("YUV420Planar", 0x0800);
       
   788     formats.insert("YUV422", 0x1000);
       
   789     formats.insert("YUV422Reversed", 0x2000);
       
   790     formats.insert("YUV444", 0x4000);
       
   791     formats.insert("YUV420SemiPlanar", 0x8000);
       
   792     formats.insert("FbsBitmapColor16MU", 0x00010000);
       
   793     formats.insert("MJPEG", 0x00020000);
       
   794     formats.insert("EncodedH264", 0x00040000);
       
   795 
       
   796     return formats;
       
   797 }
       
   798 QMap<QString, int> S60CameraSession::formatDescMap()
       
   799 {
       
   800     QMap<QString, int> formats;
       
   801     formats.insert("Monochrome", 0x0001);
       
   802     formats.insert("16bitRGB444", 0x0002);
       
   803     formats.insert("16BitRGB565", 0x0004);
       
   804     formats.insert("32BitRGB888", 0x0008);
       
   805     formats.insert("Jpeg", 0x0010);
       
   806     formats.insert("Exif", 0x0020);
       
   807     formats.insert("FbsBitmapColor4K", 0x0040);
       
   808     formats.insert("FbsBitmapColor64K", 0x0080);
       
   809     formats.insert("FbsBitmapColor16M", 0x0100);
       
   810     formats.insert("UserDefined", 0x0200);
       
   811     formats.insert("YUV420Interleaved", 0x0400);
       
   812     formats.insert("YUV420Planar", 0x0800);
       
   813     formats.insert("YUV422", 0x1000);
       
   814     formats.insert("YUV422Reversed", 0x2000);
       
   815     formats.insert("YUV444", 0x4000);
       
   816     formats.insert("YUV420SemiPlanar", 0x8000);
       
   817     formats.insert("FbsBitmapColor16MU", 0x00010000);
       
   818     formats.insert("MJPEG", 0x00020000);
       
   819     formats.insert("EncodedH264", 0x00040000);
       
   820 
       
   821     return formats;
       
   822 }
       
   823 QStringList S60CameraSession::supportedImageCaptureCodecs()
       
   824 {
       
   825     //qDebug() << "S60CameraSession::supportedImageCaptureCodecs";
       
   826     QStringList list;
       
   827     #ifdef Q_CC_NOKIAX86
       
   828     return formatMap().keys();
       
   829     #endif
       
   830 
       
   831     for (int i = 0; i < m_formats.length() ; i++) {
       
   832         list << formatMap().key(m_formats.at(i));
       
   833     }
       
   834     //qDebug()<< "S60CameraSession::supportedImageCaptureCodecs, return formatList.count()="<<list.count();
       
   835     return list;
       
   836 }
       
   837 void S60CameraSession::updateImageCaptureCodecs()
       
   838 {
       
   839     m_formats.clear();
       
   840     //qDebug() << "S60CameraSession::updateImageCaptureCodecs START";
       
   841     if (m_cameraEngine && queryCurrentCameraInfo()) {
       
   842 
       
   843         TUint32 supportedFormats = m_info.iImageFormatsSupported;
       
   844         QStringList allFormats = formatMap().keys();
       
   845         int formatMask = 1;
       
   846 
       
   847         for ( int i = 0; i < allFormats.count() ; ++i ) {
       
   848             if ( supportedFormats & formatMask ) {
       
   849                 //qDebug() << "S60CameraSession::updateImageCaptureCodecs, adding format="<<allFormats.at(i);
       
   850                 m_formats << i; // store index of supported format
       
   851             }
       
   852 
       
   853             formatMask <<= 1;
       
   854         }
       
   855     }
       
   856     //qDebug() << "S60CameraSession::updateImageCaptureCodecs END";
       
   857 }
       
   858 
       
   859 QString S60CameraSession::imageCaptureCodec()
       
   860 {
       
   861     //qDebug() << "S60CameraSession::imageCaptureCodec";
       
   862     return formatMap().key(m_currentcodec);
       
   863 }
       
   864 void S60CameraSession::setImageCaptureCodec(const QString &codecName)
       
   865 {
       
   866     //qDebug() << "S60CameraSession::setImageCaptureCodec, coded="<<codecName;
       
   867     m_currentcodec = static_cast<CCamera::TFormat>( formatMap().value(codecName) );
       
   868 }
       
   869 
       
   870 QString S60CameraSession::imageCaptureCodecDescription(const QString &codecName)
       
   871 {
       
   872     //qDebug() << "S60CameraSession::imageCaptureCodecDescription, codename="<<codecName;
       
   873     return formatDescMap().key(formatMap().value(codecName));
       
   874 }
       
   875 
       
   876 QtMedia::EncodingQuality S60CameraSession::captureQuality() const
       
   877 {
       
   878     //qDebug() << "S60CameraSession::CaptureQuality";
       
   879     if (m_imageQuality <= 1) {
       
   880         return QtMedia::VeryLowQuality;
       
   881     }
       
   882     else
       
   883         return static_cast<QtMedia::EncodingQuality> (m_imageQuality / KSymbianImageQualityCoefficient);
       
   884 
       
   885 }
       
   886 
       
   887 void S60CameraSession::setCaptureQuality(QtMedia::EncodingQuality quality)
       
   888 {
       
   889     //qDebug() << "S60CameraSession::setCaptureQuality, EncodingQuality="<<quality;
       
   890     switch (quality) {
       
   891     case QtMedia::VeryLowQuality:
       
   892         m_imageQuality = 1;
       
   893         break;
       
   894     default:
       
   895         m_imageQuality = quality * KSymbianImageQualityCoefficient;
       
   896         break;
       
   897     }
       
   898     if (m_cameraEngine) {
       
   899         CCamera *camera = m_cameraEngine->Camera();
       
   900         camera->SetJpegQuality(m_imageQuality);
       
   901         camera = NULL;
       
   902     }
       
   903 
       
   904 }
       
   905 
       
   906 void S60CameraSession::setVideoRenderer(QObject *videoOutput)
       
   907 {
       
   908     //qDebug() << "S60CameraSession::setVideoRenderer, videoOutput="<<videoOutput;
       
   909     S60ViewFinderWidgetControl* viewFinderWidgetControl =
       
   910             qobject_cast<S60ViewFinderWidgetControl*>(videoOutput);
       
   911 
       
   912     if (viewFinderWidgetControl) {
       
   913         m_VFProcessor = viewFinderWidgetControl->videoWidget();
       
   914         m_VFWidgetSize = viewFinderWidgetControl->videoWidget()->size();
       
   915     }
       
   916 }
       
   917 
       
   918 void S60CameraSession::setZoomFactor(qreal optical, qreal digital)
       
   919 {
       
   920     //qDebug() << "S60CameraSession::setZoomFactor, value(optical digital): " << optical << digital;
       
   921 
       
   922     if (m_cameraEngine && queryCurrentCameraInfo()) {
       
   923         CCamera *camera = m_cameraEngine->Camera();
       
   924         if (camera) {
       
   925             if (digital > m_info.iMaxZoom && digital <= m_info.iMaxDigitalZoom) { // digitalzoom
       
   926                 TRAPD(err, camera->SetDigitalZoomFactorL(digital));
       
   927                 setError(err);
       
   928                 //qDebug() << "S60CameraSession::setDigitalZoomFactor error: " << m_error;
       
   929                 if (err == KErrNone) {
       
   930                     emit digitalZoomChanged(digital);
       
   931                 }
       
   932             } else if (optical >= m_info.iMinZoom && optical <= m_info.iMaxZoom) { //opticalzoom
       
   933                 TRAPD(err2, camera->SetZoomFactorL(optical));
       
   934                 setError(err2);
       
   935                 //qDebug() << "S60CameraSession::setZoomFactor error: " << m_error;
       
   936                 if (err2 == KErrNone) {
       
   937                     emit opticalZoomChanged(optical);
       
   938                 }
       
   939             }
       
   940         }
       
   941     }
       
   942 }
       
   943 
       
   944 int S60CameraSession::zoomFactor()
       
   945 {
       
   946     //qDebug() << "S60CameraSession::zoomFactor";
       
   947     int factor = 0;
       
   948     if (m_cameraEngine) {
       
   949         CCamera *camera = m_cameraEngine->Camera();
       
   950         return camera->ZoomFactor();
       
   951     }
       
   952     return factor;
       
   953 }
       
   954 
       
   955 int S60CameraSession::digitalZoomFactor()
       
   956 {
       
   957     //qDebug() << "S60CameraSession::digitalZoomFactor";
       
   958     int factor = 0;
       
   959     if (m_cameraEngine) {
       
   960         CCamera *camera = m_cameraEngine->Camera();
       
   961         return camera->DigitalZoomFactor();
       
   962     }
       
   963     return factor;
       
   964 }
       
   965 
       
   966 void S60CameraSession::startFocus()
       
   967 {
       
   968     //qDebug() << "S60CameraSession::startFocus";
       
   969 
       
   970     if (m_cameraEngine) {
       
   971         TRAPD(err, m_cameraEngine->StartFocusL());
       
   972         setError(err);
       
   973     }
       
   974 }
       
   975 
       
   976 void S60CameraSession::cancelFocus()
       
   977 {
       
   978     //qDebug() << "S60CameraSession::cancelFocus";
       
   979     if (m_cameraEngine) {
       
   980         TRAPD(err, m_cameraEngine->FocusCancel());
       
   981         setError(err);
       
   982     }
       
   983 }
       
   984 
       
   985 int S60CameraSession::maximumZoom()
       
   986 {
       
   987     //qDebug() << "S60CameraSession::maximumZoom";
       
   988 
       
   989     if (queryCurrentCameraInfo()) {
       
   990         //qDebug() << "S60CameraSession::maximumZoom value: " << m_info.iMaxZoom;
       
   991         return m_info.iMaxZoom;
       
   992     } else {
       
   993         return 0;
       
   994     }
       
   995 }
       
   996 
       
   997 int S60CameraSession::minZoom()
       
   998 {
       
   999     //qDebug() << "S60CameraSession::minZoom";
       
  1000 
       
  1001     if (queryCurrentCameraInfo()) {
       
  1002         //qDebug() << "S60CameraSession::minZoom value: " << m_info.iMinZoom;
       
  1003         return m_info.iMinZoom;
       
  1004     } else {
       
  1005         return 0;
       
  1006     }
       
  1007 }
       
  1008 
       
  1009 int S60CameraSession::maxDigitalZoom()
       
  1010 {
       
  1011     //qDebug() << "S60CameraSession::maxDigitalZoom";
       
  1012     if (queryCurrentCameraInfo()) {
       
  1013         //qDebug() << "S60CameraSession::maxDigitalZoom value: " << m_info.iMaxDigitalZoom;
       
  1014         return m_info.iMaxDigitalZoom;
       
  1015     } else {
       
  1016         return 0;
       
  1017     }
       
  1018 }
       
  1019 
       
  1020 void S60CameraSession::setFlashMode(QCamera::FlashMode mode)
       
  1021 {
       
  1022     if (m_cameraEngine) {
       
  1023         switch(mode) {
       
  1024             case QCamera::FlashOff:
       
  1025                 m_cameraEngine->SetFlash(CCamera::EFlashNone);
       
  1026                 break;
       
  1027             case QCamera::FlashAuto:
       
  1028                 m_cameraEngine->SetFlash(CCamera::EFlashAuto);
       
  1029                 break;
       
  1030             case QCamera::FlashOn:
       
  1031                 m_cameraEngine->SetFlash(CCamera::EFlashForced);
       
  1032                 break;
       
  1033             case QCamera::FlashRedEyeReduction:
       
  1034                 m_cameraEngine->SetFlash(CCamera::EFlashRedEyeReduce);
       
  1035                 break;
       
  1036             case QCamera::FlashFill:
       
  1037                 m_cameraEngine->SetFlash(CCamera::EFlashFillIn);
       
  1038                 break;
       
  1039             default:
       
  1040                 break;
       
  1041 
       
  1042         }
       
  1043     }
       
  1044 }
       
  1045 
       
  1046 QCamera::FlashMode S60CameraSession::flashMode()
       
  1047 {
       
  1048     if (m_cameraEngine) {
       
  1049         TInt mode = m_cameraEngine->Flash();
       
  1050         switch(mode) {
       
  1051             case CCamera::EFlashAuto:
       
  1052                 return QCamera::FlashAuto;
       
  1053             case CCamera::EFlashForced:
       
  1054                 return QCamera::FlashOn;
       
  1055             case CCamera::EFlashRedEyeReduce:
       
  1056                 return QCamera::FlashRedEyeReduction;
       
  1057             case CCamera::EFlashFillIn:
       
  1058                 return QCamera::FlashFill;
       
  1059             default:
       
  1060                 return QCamera::FlashOff;
       
  1061         }
       
  1062     }
       
  1063     return QCamera::FlashOff;
       
  1064 }
       
  1065 
       
  1066 QCamera::FlashModes S60CameraSession::supportedFlashModes()
       
  1067 {
       
  1068     QCamera::FlashModes modes = QCamera::FlashOff;
       
  1069     if (m_cameraEngine) {
       
  1070         TInt supportedModes =  m_cameraEngine->SupportedFlashModes();
       
  1071         if (supportedModes == 0)
       
  1072             return modes;
       
  1073         if (supportedModes & CCamera::EFlashManual) {
       
  1074              modes |= QCamera::FlashOff;
       
  1075         }
       
  1076         if (supportedModes & CCamera::EFlashForced) {
       
  1077              modes |= QCamera::FlashOn;
       
  1078         }
       
  1079         if (supportedModes & CCamera::EFlashAuto) {
       
  1080              modes |= QCamera::FlashAuto;
       
  1081         }
       
  1082         if (supportedModes & CCamera::EFlashFillIn) {
       
  1083              modes |= QCamera::FlashFill;
       
  1084         }
       
  1085         if (supportedModes & CCamera::EFlashRedEyeReduce) {
       
  1086              modes |= QCamera::FlashRedEyeReduction;
       
  1087         }
       
  1088     }
       
  1089     return modes;
       
  1090 }
       
  1091 
       
  1092 QCamera::ExposureMode S60CameraSession::exposureMode()
       
  1093 {
       
  1094     if (m_cameraEngine) {
       
  1095         CCamera* camera = m_cameraEngine->Camera();
       
  1096         CCamera::TExposure mode2 = camera->Exposure();
       
  1097         switch(mode2) {
       
  1098             case CCamera::EExposureManual:
       
  1099                 return QCamera::ExposureManual;
       
  1100             case CCamera::EExposureAuto:
       
  1101                 return QCamera::ExposureAuto;
       
  1102             case CCamera::EExposureNight:
       
  1103                 return QCamera::ExposureNight;
       
  1104             case CCamera::EExposureBacklight:
       
  1105                 return QCamera::ExposureBacklight;
       
  1106             case CCamera::EExposureSport:
       
  1107                 return QCamera::ExposureSports;
       
  1108             case CCamera::EExposureSnow:
       
  1109                 return QCamera::ExposureSnow;
       
  1110             case CCamera::EExposureBeach:
       
  1111                 return QCamera::ExposureBeach;
       
  1112             default:
       
  1113                 return QCamera::ExposureAuto;
       
  1114         }
       
  1115     }
       
  1116     return QCamera::ExposureAuto;
       
  1117 }
       
  1118 
       
  1119 QCamera::ExposureModes S60CameraSession::supportedExposureModes()
       
  1120 {
       
  1121     QCamera::ExposureModes modes = QCamera::ExposureAuto;
       
  1122     if (m_cameraEngine) {
       
  1123         TInt supportedModes = m_info.iExposureModesSupported;
       
  1124         if (supportedModes == 0) {
       
  1125             return modes;
       
  1126         }
       
  1127         if (supportedModes & CCamera::EExposureManual) {
       
  1128             modes |= QCamera::ExposureManual;
       
  1129         }
       
  1130         if (supportedModes & CCamera::EExposureAuto) {
       
  1131             modes |= QCamera::ExposureAuto;
       
  1132         }
       
  1133         if (supportedModes & CCamera::EExposureNight) {
       
  1134             modes |= QCamera::ExposureNight;
       
  1135         }
       
  1136         if (supportedModes & CCamera::EExposureBacklight) {
       
  1137             modes |= QCamera::ExposureBacklight;
       
  1138         }
       
  1139         if (supportedModes & CCamera::EExposureSport) {
       
  1140             modes |= QCamera::ExposureSports;
       
  1141         }
       
  1142         if (supportedModes & CCamera::EExposureSnow) {
       
  1143             modes |= QCamera::ExposureSnow;
       
  1144         }
       
  1145         if (supportedModes & CCamera::EExposureBeach) {
       
  1146             modes |= QCamera::ExposureBeach;
       
  1147         }
       
  1148     }
       
  1149     return modes;
       
  1150 }
       
  1151 
       
  1152 void S60CameraSession::setExposureMode(QCamera::ExposureMode mode)
       
  1153 {
       
  1154     if (m_cameraEngine) {
       
  1155         switch(mode) {
       
  1156             case QCamera::ExposureManual:
       
  1157                 m_cameraEngine->SetExposure(CCamera::EExposureManual);
       
  1158                 break;
       
  1159             case QCamera::ExposureAuto:
       
  1160                 m_cameraEngine->SetExposure(CCamera::EExposureAuto);
       
  1161                 break;
       
  1162             case QCamera::ExposureNight:
       
  1163                 m_cameraEngine->SetExposure(CCamera::EExposureNight);
       
  1164                 break;
       
  1165             case QCamera::ExposureBacklight:
       
  1166                 m_cameraEngine->SetExposure(CCamera::EExposureBacklight);
       
  1167                 break;
       
  1168             case QCamera::ExposureSports:
       
  1169                 m_cameraEngine->SetExposure(CCamera::EExposureSport);
       
  1170                 break;
       
  1171             case QCamera::ExposureSnow:
       
  1172                 m_cameraEngine->SetExposure(CCamera::EExposureSnow);
       
  1173                 break;
       
  1174             case QCamera::ExposureBeach:
       
  1175                 m_cameraEngine->SetExposure(CCamera::EExposureBeach);
       
  1176                 break;
       
  1177             case QCamera::ExposureLargeAperture:
       
  1178             case QCamera::ExposureSmallAperture:
       
  1179                 //TODO:
       
  1180                 //m_cameraEngine->SetExposure(CCamera::EExposureAperturePriority);
       
  1181                 break;
       
  1182             case QCamera::ExposurePortrait:
       
  1183             case QCamera::ExposureSpotlight:
       
  1184             default:
       
  1185                 // not supported
       
  1186                 break;
       
  1187         }
       
  1188     }
       
  1189 }
       
  1190 
       
  1191 qreal S60CameraSession::contrast() const
       
  1192 {
       
  1193     if (m_cameraEngine) {
       
  1194         CCamera* camera = m_cameraEngine->Camera();
       
  1195         return camera->Contrast();
       
  1196     } else {
       
  1197         return 0;
       
  1198     }
       
  1199 }
       
  1200 
       
  1201 void S60CameraSession::setContrast(qreal value)
       
  1202 {
       
  1203     if (m_cameraEngine) {
       
  1204         CCamera* camera = m_cameraEngine->Camera();
       
  1205         TRAPD(err, camera->SetContrastL(value));
       
  1206         setError(err);
       
  1207         }
       
  1208 }
       
  1209 
       
  1210 
       
  1211 QCamera::WhiteBalanceMode S60CameraSession::whiteBalanceMode()
       
  1212 {
       
  1213     if (m_cameraEngine) {
       
  1214         CCamera* camera = m_cameraEngine->Camera();
       
  1215         CCamera::TWhiteBalance mode = camera->WhiteBalance();
       
  1216         switch(mode) {
       
  1217             case CCamera::EWBAuto:
       
  1218                 return QCamera::WhiteBalanceAuto;
       
  1219             case CCamera::EWBDaylight:
       
  1220                 return QCamera::WhiteBalanceSunlight;
       
  1221             case CCamera::EWBCloudy:
       
  1222                 return QCamera::WhiteBalanceCloudy;
       
  1223             case CCamera::EWBTungsten:
       
  1224                 return QCamera::WhiteBalanceTungsten;
       
  1225             case CCamera::EWBFluorescent:
       
  1226                 return QCamera::WhiteBalanceFluorescent;
       
  1227             case CCamera::EWBFlash:
       
  1228                 return QCamera::WhiteBalanceFlash;
       
  1229             case CCamera::EWBBeach:
       
  1230                 return QCamera::WhiteBalanceSunset;
       
  1231             case CCamera::EWBManual:
       
  1232                 return QCamera::WhiteBalanceManual;
       
  1233             case CCamera::EWBShade:
       
  1234                 return QCamera::WhiteBalanceShade;
       
  1235             default:
       
  1236                 return QCamera::WhiteBalanceAuto;
       
  1237         } 
       
  1238     }
       
  1239     return QCamera::WhiteBalanceAuto;
       
  1240 }
       
  1241 
       
  1242 void S60CameraSession::setWhiteBalanceMode(QCamera::WhiteBalanceMode mode)
       
  1243 {
       
  1244     TRAPD(err, setWhiteBalanceModeL(mode));
       
  1245     setError(err);
       
  1246 }
       
  1247 
       
  1248 void S60CameraSession::setWhiteBalanceModeL(QCamera::WhiteBalanceMode mode)
       
  1249 {
       
  1250     if (m_cameraEngine) {
       
  1251         CCamera* camera = m_cameraEngine->Camera();
       
  1252         switch(mode) {
       
  1253             case QCamera::WhiteBalanceAuto:
       
  1254                 camera->SetWhiteBalanceL(CCamera::EWBAuto);
       
  1255                 break;
       
  1256             case QCamera::WhiteBalanceSunlight:
       
  1257                 camera->SetWhiteBalanceL(CCamera::EWBDaylight);
       
  1258                 break;
       
  1259             case QCamera::WhiteBalanceCloudy:
       
  1260                 camera->SetWhiteBalanceL(CCamera::EWBCloudy);
       
  1261                 break;
       
  1262             case QCamera::WhiteBalanceTungsten:
       
  1263             case QCamera::WhiteBalanceIncandescent:
       
  1264                 camera->SetWhiteBalanceL(CCamera::EWBTungsten);
       
  1265                 break;
       
  1266             case QCamera::WhiteBalanceFluorescent:
       
  1267                 camera->SetWhiteBalanceL(CCamera::EWBFluorescent);
       
  1268                 break;
       
  1269             case QCamera::WhiteBalanceFlash:
       
  1270                 camera->SetWhiteBalanceL(CCamera::EWBFlash);
       
  1271                 break;
       
  1272             case QCamera::WhiteBalanceSunset:
       
  1273                 camera->SetWhiteBalanceL(CCamera::EWBBeach);
       
  1274                 break;
       
  1275             case QCamera::WhiteBalanceManual:
       
  1276                 camera->SetWhiteBalanceL(CCamera::EWBManual);
       
  1277                 break;
       
  1278             case QCamera::WhiteBalanceShade:
       
  1279                 camera->SetWhiteBalanceL(CCamera::EWBShade);
       
  1280                 break;
       
  1281             default:
       
  1282                 // not supported
       
  1283                 break;
       
  1284         }
       
  1285     }
       
  1286 }
       
  1287 
       
  1288 QCamera::WhiteBalanceModes S60CameraSession::supportedWhiteBalanceModes()
       
  1289 {
       
  1290     QCamera::WhiteBalanceModes modes = QCamera::WhiteBalanceAuto;
       
  1291     if (m_cameraEngine) { 
       
  1292         TInt supportedModes = m_info.iWhiteBalanceModesSupported;
       
  1293         if (supportedModes == 0) {
       
  1294             return modes;
       
  1295         }
       
  1296         if (supportedModes & CCamera::EWBAuto) {
       
  1297             modes |= QCamera::WhiteBalanceAuto;          
       
  1298         }
       
  1299         if (supportedModes & CCamera::EWBDaylight) {
       
  1300             modes |= QCamera::WhiteBalanceSunlight;          
       
  1301         }
       
  1302         if (supportedModes & CCamera::EWBCloudy) {
       
  1303             modes |= QCamera::WhiteBalanceCloudy;          
       
  1304         }
       
  1305         if (supportedModes & CCamera::EWBTungsten) {
       
  1306             modes |= QCamera::WhiteBalanceTungsten;  
       
  1307             modes |= QCamera::WhiteBalanceIncandescent;
       
  1308         }
       
  1309         if (supportedModes & CCamera::EWBFluorescent) {
       
  1310             modes |= QCamera::WhiteBalanceFluorescent;          
       
  1311         }
       
  1312         if (supportedModes & CCamera::EWBFlash) {
       
  1313             modes |= QCamera::WhiteBalanceFlash;          
       
  1314         }
       
  1315         if (supportedModes & CCamera::EWBBeach) {
       
  1316             modes |= QCamera::WhiteBalanceSunset;          
       
  1317         }
       
  1318         if (supportedModes & CCamera::EWBManual) {
       
  1319             modes |= QCamera::WhiteBalanceManual;          
       
  1320         }
       
  1321         if (supportedModes & CCamera::EWBShade) {
       
  1322             modes |= QCamera::WhiteBalanceShade;          
       
  1323         }
       
  1324     }
       
  1325     return modes;
       
  1326 }
       
  1327 
       
  1328 void S60CameraSession::updateVideoCaptureCodecs()
       
  1329 {
       
  1330     TRAPD(err, updateVideoCaptureCodecsL());
       
  1331     setError(err);
       
  1332 }
       
  1333 
       
  1334 void S60CameraSession::updateVideoCaptureCodecsL()
       
  1335 {
       
  1336     m_videoControllerMap.clear();
       
  1337 
       
  1338     // Resolve the supported video format and retrieve a list of controllers
       
  1339     CMMFControllerPluginSelectionParameters* pluginParameters =
       
  1340         CMMFControllerPluginSelectionParameters::NewLC();
       
  1341     CMMFFormatSelectionParameters* format =
       
  1342         CMMFFormatSelectionParameters::NewLC();
       
  1343 
       
  1344     // Set the play and record format selection parameters to be blank.
       
  1345     // Format support is only retrieved if requested.
       
  1346     pluginParameters->SetRequiredPlayFormatSupportL(*format);
       
  1347     pluginParameters->SetRequiredRecordFormatSupportL(*format);
       
  1348 
       
  1349     // Set the media ids
       
  1350     RArray<TUid> mediaIds;
       
  1351     CleanupClosePushL(mediaIds);
       
  1352     User::LeaveIfError(mediaIds.Append(KUidMediaTypeVideo));
       
  1353     // Get plugins that support at least video
       
  1354     pluginParameters->SetMediaIdsL(mediaIds,
       
  1355         CMMFPluginSelectionParameters::EAllowOtherMediaIds);
       
  1356     pluginParameters->SetPreferredSupplierL(KNullDesC,
       
  1357         CMMFPluginSelectionParameters::EPreferredSupplierPluginsFirstInList);
       
  1358 
       
  1359     // Array to hold all the controllers support the match data
       
  1360     RMMFControllerImplInfoArray controllers;
       
  1361     CleanupResetAndDestroyPushL(controllers);
       
  1362     pluginParameters->ListImplementationsL(controllers);
       
  1363 
       
  1364     // Find the first controller with at least one record format available
       
  1365     for (TInt index=0; index<controllers.Count(); index++) {
       
  1366         const RMMFFormatImplInfoArray& recordFormats =
       
  1367             controllers[index]->RecordFormats();
       
  1368         for (TInt j=0; j<recordFormats.Count(); j++) {
       
  1369             const CDesC8Array& mimeTypes = recordFormats[j]->SupportedMimeTypes();
       
  1370             TInt count = mimeTypes.Count();
       
  1371             if (count > 0) {
       
  1372                 TPtrC8 mimeType = mimeTypes[0];
       
  1373                 QString type = QString::fromUtf8((char *)mimeType.Ptr(),
       
  1374                         mimeType.Length());
       
  1375                 // Currently only support for video/mp4 due to resolution and frame rate issues.
       
  1376                 if (type == "video/mp4") {
       
  1377                     VideoControllerData data;
       
  1378                     data.controllerUid = controllers[index]->Uid().iUid;
       
  1379                     data.formatUid = recordFormats[j]->Uid().iUid;
       
  1380                     data.formatDescription = QString::fromUtf16(
       
  1381                             recordFormats[j]->DisplayName().Ptr(),
       
  1382                             recordFormats[j]->DisplayName().Length());
       
  1383                     m_videoControllerMap[type] = data;
       
  1384                 }
       
  1385             }
       
  1386         }
       
  1387     }
       
  1388 
       
  1389     CleanupStack::PopAndDestroy(&controllers);
       
  1390     CleanupStack::PopAndDestroy(&mediaIds);
       
  1391     CleanupStack::PopAndDestroy(format);
       
  1392     CleanupStack::PopAndDestroy(pluginParameters);
       
  1393     
       
  1394     //qDebug() << "S60CameraSession::updateVideoCaptureCodecs count: " << m_videoControllerMap.keys().count();
       
  1395 }
       
  1396 
       
  1397 QStringList S60CameraSession::supportedVideoCaptureCodecs()
       
  1398 {
       
  1399     //qDebug() << "S60CameraSession::supportedVideoCaptureCodecs";
       
  1400     return m_videoControllerMap.keys();
       
  1401 }
       
  1402 
       
  1403 bool S60CameraSession::isSupportedVideoCaptureCodec(const QString &codecName)
       
  1404 {
       
  1405     return m_videoControllerMap.keys().contains(codecName);
       
  1406 }
       
  1407 
       
  1408 QString S60CameraSession::videoCaptureCodec()
       
  1409 {
       
  1410     return m_videoCodec;
       
  1411 }
       
  1412 void S60CameraSession::setVideoCaptureCodec(const QString &codecName)
       
  1413 {
       
  1414     if (codecName == m_videoCodec)
       
  1415         return;
       
  1416 
       
  1417     m_videoCodec = codecName;
       
  1418 }
       
  1419 
       
  1420 QString S60CameraSession::videoCaptureCodecDescription(const QString &codecName)
       
  1421 {
       
  1422     return m_videoControllerMap[codecName].formatDescription;
       
  1423 }
       
  1424 
       
  1425 int S60CameraSession::bitrate()
       
  1426 {
       
  1427     if (m_videoUtility) {
       
  1428         TInt rate = 0;
       
  1429         TRAPD(err, rate = m_videoUtility->VideoBitRateL());
       
  1430         setError(err);
       
  1431         return rate;
       
  1432     }
       
  1433     return 0;
       
  1434 }
       
  1435 
       
  1436 void S60CameraSession::setBitrate(const int &bitrate)
       
  1437 {
       
  1438     if (m_videoUtility) {
       
  1439         TRAPD(err, m_videoUtility->SetVideoBitRateL(bitrate));
       
  1440         setError(err);
       
  1441     }
       
  1442 }
       
  1443 
       
  1444 QSize S60CameraSession::videoResolution()
       
  1445 {
       
  1446     TSize size(0,0);
       
  1447     if (m_videoUtility) {
       
  1448         TRAPD(err, m_videoUtility->GetVideoFrameSizeL(size));
       
  1449         setError(err);
       
  1450     }
       
  1451     return QSize(size.iWidth, size.iHeight);
       
  1452 }
       
  1453 
       
  1454 void S60CameraSession::setVideoResolution(const QSize &resolution)
       
  1455 {
       
  1456     if (m_videoUtility) {
       
  1457         TSize size(resolution.width(), resolution.height());
       
  1458         TRAPD(err, m_videoUtility->SetVideoFrameSizeL(size));
       
  1459         setError(err);
       
  1460     }
       
  1461 }
       
  1462 
       
  1463 void S60CameraSession::initializeVideoCaptureSettings()
       
  1464 {
       
  1465     if (m_videoControllerMap.keys().count() > 0)
       
  1466         m_videoSettings.setCodec(m_videoControllerMap.keys()[0]); // Setting the first codec found as initial value
       
  1467     
       
  1468     // set lowest value found as initial value for frame rate
       
  1469     QList<qreal> rates = supportedVideoFrameRates();
       
  1470     qreal minRate = 30.0;
       
  1471     foreach (qreal rate, rates)
       
  1472         minRate = qMin(minRate, rate);
       
  1473     m_videoSettings.setFrameRate(minRate);
       
  1474     
       
  1475     QSize minResolution(176, 144);
       
  1476     m_videoSettings.setResolution(minResolution);
       
  1477     
       
  1478     // use variable bit rate as initial value
       
  1479     m_videoSettings.setBitRate(KMMFVariableVideoBitRate);
       
  1480     
       
  1481     m_videoSettings.setQuality(QtMedia::LowQuality);
       
  1482 }
       
  1483 
       
  1484 
       
  1485 void S60CameraSession::MvruoOpenComplete(TInt aError)
       
  1486 {
       
  1487     //qDebug() << "S60CameraSession::MvruoOpenComplete, error: " << aError;
       
  1488 
       
  1489     if(aError==KErrNone) {
       
  1490         commitVideoEncoderSettings();
       
  1491         m_videoUtility->Prepare();
       
  1492         m_captureState = EOpenCompelete;
       
  1493     } else {
       
  1494         m_captureState = ENotInitialized;
       
  1495     }
       
  1496     setError(aError);
       
  1497 }
       
  1498 
       
  1499 void S60CameraSession::MvruoPrepareComplete(TInt aError)
       
  1500 {
       
  1501     //qDebug() << "S60CameraSession::MvruoPrepareComplete, error: " << aError;
       
  1502     if(aError==KErrNone) {
       
  1503         m_videoUtility->Record();
       
  1504         m_captureState = ERecording;
       
  1505         //qDebug() << "S60CameraSession::MvruoPrepareComplete: Record called";
       
  1506     } else {
       
  1507         m_captureState = ENotInitialized;
       
  1508     }
       
  1509     setError(aError);
       
  1510 
       
  1511 }
       
  1512 
       
  1513 void S60CameraSession::MvruoRecordComplete(TInt aError)
       
  1514 {
       
  1515     //qDebug() << "S60CameraSession::MvruoRecordComplete";
       
  1516     if((aError==KErrNone) || (aError==KErrCompletion)) {
       
  1517         m_videoUtility->Stop();
       
  1518         m_videoUtility->Close();
       
  1519     }
       
  1520     m_captureState = ERecordComplete;
       
  1521     setError(aError);
       
  1522 
       
  1523 }
       
  1524 
       
  1525 void S60CameraSession::MvruoEvent(const TMMFEvent& aEvent)
       
  1526 {
       
  1527 
       
  1528 }
       
  1529 
       
  1530