javauis/mmapi_akn/baseline/src.dsa/cmmasnapshot.cpp
branchRCL_3
changeset 24 6c158198356e
equal deleted inserted replaced
23:e5618cc85d74 24:6c158198356e
       
     1 /*
       
     2 * Copyright (c) 2002-2007 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:  This class takes snapshot and resizes bitmap if needed.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 #include <jdebug.h>
       
    21 
       
    22 #include "cmmasnapshot.h"
       
    23 #include "mmmaguiplayer.h"
       
    24 #include "mmmasnapshotreadycallback.h"
       
    25 #include "mmmasnapshot.h"
       
    26 
       
    27 // CONSTANTS
       
    28 const TInt KIgnoreSize = -1;
       
    29 
       
    30 
       
    31 // CONSTRUCTION
       
    32 CMMASnapshot* CMMASnapshot::NewL(MMMAGuiPlayer* aGuiPlayer,
       
    33                                  MMMASnapshotReadyCallback& aCallBack)
       
    34 {
       
    35     CMMASnapshot* self = new(ELeave) CMMASnapshot(aGuiPlayer,
       
    36             aCallBack);
       
    37     CleanupStack::PushL(self);
       
    38     self->ConstructL();
       
    39     CleanupStack::Pop(); // self
       
    40     return self;
       
    41 }
       
    42 
       
    43 CMMASnapshot::~CMMASnapshot()
       
    44 {
       
    45     if (iScaler)
       
    46     {
       
    47         iScaler->Cancel();
       
    48         delete iScaler;
       
    49     }
       
    50     if (iEncoder)
       
    51     {
       
    52         iEncoder->Cancel();
       
    53         delete iEncoder;
       
    54     }
       
    55     delete iBitmap;
       
    56     delete iBuffer;
       
    57     delete iSettings;
       
    58 }
       
    59 
       
    60 void CMMASnapshot::ConstructL()
       
    61 {
       
    62     CActiveScheduler::Add(this);
       
    63     iScaler = CBitmapScaler::NewL();
       
    64 }
       
    65 
       
    66 CMMASnapshot::CMMASnapshot(MMMAGuiPlayer* aGuiPlayer,
       
    67                            MMMASnapshotReadyCallback& aCallBack):
       
    68         CActive(EPriorityStandard),
       
    69         iEncoding(MMMASnapshot::EBitmap),
       
    70         iCallBack(aCallBack),
       
    71         iState(EIdle)
       
    72 {
       
    73     iGUIPlayer = aGuiPlayer;
       
    74 }
       
    75 
       
    76 void CMMASnapshot::TakeSnapShotL(const TDesC& aProperties)
       
    77 {
       
    78     // snapshot sequence is not finished
       
    79     __ASSERT_DEBUG(iState == EIdle, User::Invariant());
       
    80     // image buffer must be taken with ImageBuffer before taking new snapshot
       
    81     __ASSERT_DEBUG(iBuffer == NULL, User::Invariant());
       
    82 
       
    83     CMMAImageSettings* settings
       
    84     = TMMAParameterValidator::ValidateImagePropertiesL(aProperties);
       
    85     CleanupStack::PushL(settings);
       
    86 
       
    87     delete iSettings;
       
    88     CleanupStack::Pop(settings);
       
    89     iSettings = settings;
       
    90 
       
    91     // take snapshot from player. RunL is called when image is ready
       
    92     // or error occures
       
    93     iState = ETakingSnapshot;
       
    94     TSize snapshotSize(iSettings->iWidth, iSettings->iHeight);
       
    95     iEncoding = iGUIPlayer->SnapshoterL()->TakeSnapshotL(&iStatus,
       
    96                 snapshotSize,
       
    97                 *iSettings);
       
    98     SetActive();
       
    99 }
       
   100 
       
   101 HBufC8* CMMASnapshot::ImageBuffer()
       
   102 {
       
   103     // this must not be called when snapshot sequence is running
       
   104     __ASSERT_DEBUG(iState == EIdle, User::Invariant());
       
   105     HBufC8* buffer = iBuffer;
       
   106     // caller takes ownership of the buffer
       
   107     iBuffer = NULL;
       
   108     return buffer;
       
   109 }
       
   110 
       
   111 void CMMASnapshot::ResizeL()
       
   112 {
       
   113     iState = EResizing;
       
   114     TSize imageSize(iBitmap->SizeInPixels());
       
   115     if (iSettings->iWidth != KIgnoreSize)
       
   116     {
       
   117         imageSize.iWidth = iSettings->iWidth;
       
   118     }
       
   119     if (iSettings->iHeight != KIgnoreSize)
       
   120     {
       
   121         imageSize.iHeight = iSettings->iHeight;
       
   122     }
       
   123     if (imageSize == iBitmap->SizeInPixels())
       
   124     {
       
   125         // no user resizing needed, continue sequence
       
   126         EncodeL();
       
   127     }
       
   128     else
       
   129     {
       
   130         iScaler->Scale(&iStatus, *iBitmap, imageSize, EFalse);
       
   131         SetActive();
       
   132     }
       
   133 }
       
   134 
       
   135 void CMMASnapshot::EncodeL()
       
   136 {
       
   137     // CImageEncoder cannot be reused, so have to make it every time
       
   138     CImageEncoder* encoder = CImageEncoder::DataNewL(iBuffer, *iSettings->iMimeType);
       
   139     delete iEncoder;
       
   140     iEncoder = encoder;
       
   141 
       
   142     iState = EEncoding;
       
   143     iEncoder->Convert(&iStatus, *iBitmap, iSettings->iImageData);
       
   144     SetActive();
       
   145 }
       
   146 
       
   147 void CMMASnapshot::Completed(TInt aError)
       
   148 {
       
   149     iStatus = aError;
       
   150     iCallBack.SnapshotReady();
       
   151 }
       
   152 
       
   153 void CMMASnapshot::RunL()
       
   154 {
       
   155     if (iStatus != KErrNone)
       
   156     {
       
   157         // Error has occured, inform java side and change state
       
   158         iState = EIdle;
       
   159         Completed(iStatus.Int());
       
   160         return;
       
   161     }
       
   162 
       
   163     switch (iState)
       
   164     {
       
   165     case ETakingSnapshot:
       
   166     {
       
   167         DEBUG_INT("MMA::CMMASnapshot::RunL: iEncoding = %d", iEncoding);
       
   168         if (iEncoding == MMMASnapshot::EEncoded)
       
   169         {
       
   170             // take encoded image from player.
       
   171             // Ownership transfers to this class.
       
   172             iBuffer = iGUIPlayer->SnapshoterL()->SnapshotEncoded();
       
   173             if (!iBuffer)
       
   174             {
       
   175                 // error has occured with taking image
       
   176                 Completed(KErrNotFound);
       
   177             }
       
   178             // Image is ready, update internal state and inform listener
       
   179             // Encoded images are not resized
       
   180             iState = EIdle;
       
   181             Completed(KErrNone);
       
   182         }
       
   183         else
       
   184         {
       
   185             // take bitmap from player.
       
   186             // Ownership transfers to this class.
       
   187             iBitmap = iGUIPlayer->SnapshoterL()->SnapshotBitmap();
       
   188             if (!iBitmap)
       
   189             {
       
   190                 // error has occured with taking image
       
   191                 Completed(KErrNotFound);
       
   192             }
       
   193             // Continue to next state
       
   194             ResizeL();
       
   195         }
       
   196         break;
       
   197     }
       
   198     case EResizing:
       
   199     {
       
   200         // Continue to next state
       
   201         EncodeL();
       
   202         break;
       
   203     }
       
   204     case EEncoding:
       
   205     {
       
   206         delete iEncoder;
       
   207         iEncoder = NULL;
       
   208 
       
   209         delete iBitmap;
       
   210         iBitmap = NULL;
       
   211         iState = EIdle;
       
   212         // encoding is ready, inform listener
       
   213         Completed(KErrNone);
       
   214         break;
       
   215     }
       
   216     default:
       
   217     {
       
   218         // unknown state
       
   219         __ASSERT_DEBUG(EFalse, User::Invariant());
       
   220     }
       
   221     }
       
   222 }
       
   223 
       
   224 TInt CMMASnapshot::RunError(TInt aError)
       
   225 {
       
   226     // Reset state
       
   227     iState = EIdle;
       
   228     // Pass error code to observer
       
   229     Completed(aError);
       
   230 
       
   231     return KErrNone;
       
   232 }
       
   233 
       
   234 void CMMASnapshot::DoCancel()
       
   235 {
       
   236     // snapshot taking cannot be cancelled
       
   237     if (iScaler)
       
   238     {
       
   239         iScaler->Cancel();
       
   240     }
       
   241     if (iEncoder)
       
   242     {
       
   243         iEncoder->Cancel();
       
   244     }
       
   245     iState = EIdle;
       
   246 }
       
   247 
       
   248 //  END OF FILE