videofeeds/utils/src/CIptvThumbnailImpl.cpp
changeset 0 96612d01cf9f
equal deleted inserted replaced
-1:000000000000 0:96612d01cf9f
       
     1 /*
       
     2 * Copyright (c) 2006-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 the License "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:    Thumbnail generation implementation*
       
    15 */
       
    16 
       
    17 
       
    18 
       
    19 
       
    20 // System includes
       
    21 #include "IptvDebug.h"
       
    22 #include <imageconversion.h>
       
    23 #include <apgcli.h>
       
    24 #include <bautils.h>
       
    25 
       
    26 // User includes
       
    27 #include "CIptvUtil.h"
       
    28 #include "CIptvThumbnailImpl.h"
       
    29 #include "CIptvThumbnailObserver.h"
       
    30 #include "CIptvRtpTo3gpConverter.h"
       
    31 
       
    32 // Module constants
       
    33 _LIT(KTempClipFilename,"thumbtmp.3gp");
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // NewLC
       
    37 // ---------------------------------------------------------------------------
       
    38 CIptvThumbnailImpl* CIptvThumbnailImpl::NewLC( MIptvThumbnailObserver& aObserver, TInt aPriority )
       
    39     {
       
    40     CIptvThumbnailImpl* self = new (ELeave) CIptvThumbnailImpl( aObserver, aPriority );
       
    41     CleanupStack::PushL( self );
       
    42     self->ConstructL();
       
    43     return self;
       
    44     }
       
    45 
       
    46 // ---------------------------------------------------------------------------
       
    47 // CIptvThumbnailImpl
       
    48 // ---------------------------------------------------------------------------
       
    49 CIptvThumbnailImpl::CIptvThumbnailImpl( MIptvThumbnailObserver& aObserver, TInt aPriority )
       
    50     : CIptvThumbnailGenerator(aPriority), iObserver(aObserver), iPriority(aPriority)
       
    51     {
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // ConstructL
       
    56 // ---------------------------------------------------------------------------
       
    57 void CIptvThumbnailImpl::ConstructL()
       
    58     {
       
    59     User::LeaveIfError( iFs.Connect() );
       
    60     CActiveScheduler::Add( this );
       
    61     //iRtpConverter = CIptvRtpTo3gpConverter::NewL( iFs, *this, iPriority );
       
    62     CIptvUtil::GetPathL( iFs, EIptvPathTmp, iTempClipFilename );
       
    63     iTempClipFilename.Append( KTempClipFilename );
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // DoCleanup
       
    68 // ---------------------------------------------------------------------------
       
    69 void CIptvThumbnailImpl::DoCleanup()
       
    70     {
       
    71     IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::DoCleanup IN" );
       
    72     if( iImageEncoder )
       
    73         {
       
    74         iImageEncoder->Cancel();
       
    75         delete iImageEncoder;
       
    76         iImageEncoder = NULL;
       
    77         }
       
    78 
       
    79     delete iThumbBitmap;
       
    80     iThumbBitmap = NULL;
       
    81 
       
    82     delete iThumbFilename;
       
    83     iThumbFilename = NULL;
       
    84 
       
    85     if( iTempClipFilename.Length() )
       
    86         {
       
    87         // delete temp file ignoring errors (NotFound,...)
       
    88         (void)iFs.Delete( iTempClipFilename );
       
    89         }
       
    90 
       
    91     if( iClipInfo )
       
    92         {
       
    93         // Save iClipInfo to local var and set to NULL to avoid recursive cancellation
       
    94         CTNEVideoClipInfo* info = iClipInfo;
       
    95         iClipInfo = NULL;
       
    96         info->CancelThumb();
       
    97         delete info;
       
    98         }
       
    99 
       
   100     IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::DoCleanup OUT" );
       
   101     }
       
   102 
       
   103 // ---------------------------------------------------------------------------
       
   104 // ~CIptvThumbnailImpl
       
   105 // ---------------------------------------------------------------------------
       
   106 CIptvThumbnailImpl::~CIptvThumbnailImpl()
       
   107     {
       
   108     Cancel();
       
   109     DoCleanup();
       
   110 
       
   111     //delete iRtpConverter;
       
   112     //iRtpConverter = NULL;
       
   113 
       
   114     iFs.Close();
       
   115     }
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 // GenerateThumbnailL
       
   119 // ---------------------------------------------------------------------------
       
   120 void CIptvThumbnailImpl::GenerateThumbnailL( const TDesC& aClipFilename, const TDesC& aThumbFilename, const TSize aResolution )
       
   121     {
       
   122     IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::GenerateThumbnailL IN");
       
   123     IPTVLOGSTRING2_LOW_LEVEL( "clip-file='%S'", &aClipFilename );
       
   124     IPTVLOGSTRING2_LOW_LEVEL( "thumb-file='%S'", &aThumbFilename );
       
   125 
       
   126     // Check that no thumbnail generation is already ongoing.
       
   127     if ( IsActive() )
       
   128         {
       
   129         IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::GenerateThumbnailL object is active, leaving!");
       
   130         User::Leave( KErrInUse );
       
   131         }
       
   132 
       
   133     // Everything must be clean before starting generation
       
   134     DoCleanup();
       
   135 
       
   136     // 1. create VideoClipInfo object, actual info availability is
       
   137     // signaled by NotifyVideoClipInfoReady
       
   138     iThumbFilename = aThumbFilename.AllocL();
       
   139     iResolution = aResolution;
       
   140 
       
   141     DoStartThumbnailL( aClipFilename );
       
   142 
       
   143     IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::GenerateThumbnailL OUT" );
       
   144     }
       
   145 
       
   146 // ---------------------------------------------------------------------------
       
   147 // CancelThumbnail
       
   148 // ---------------------------------------------------------------------------
       
   149 void CIptvThumbnailImpl::CancelThumbnail()
       
   150     {
       
   151     // Cancel on going thumb creation
       
   152     IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::CancelThumbnailL IN" );
       
   153     if ( IsActive() )
       
   154         {
       
   155         Cancel();
       
   156         // DoCleanup is done in DoCancel called by cancel.
       
   157         }
       
   158     else 
       
   159         {
       
   160         DoCleanup();
       
   161         }
       
   162 
       
   163     IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::CancelThumbnailL OUT" );
       
   164     }
       
   165 
       
   166 // ---------------------------------------------------------------------------
       
   167 // NotifyVideoClipInfoReady
       
   168 // ---------------------------------------------------------------------------
       
   169 void CIptvThumbnailImpl::NotifyVideoClipInfoReady( CTNEVideoClipInfo& aInfo, TInt aError )
       
   170     {
       
   171     IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::NotifyVideoClipInfoReady IN" );
       
   172 
       
   173     // Check for errors on info creation, like file not found as so on
       
   174     if( aError != KErrNone )
       
   175         {
       
   176         IPTVLOGSTRING2_LOW_LEVEL( "CIptvThumbnailImpl::NotifyVideoClipInfoReady: CVedVideoClipInfo::NewL FAILED, aError=%d", aError );
       
   177         iObserver.ThumbnailCompleted( aError );
       
   178         IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::NotifyVideoClipInfoReady OUT" );
       
   179         return;
       
   180         }
       
   181 
       
   182     // 2. ClipInfo is ready, request actual thumbnail. Notified by NotifyVideoClipFrameCompleted.
       
   183     TRAPD( error, aInfo.GetThumbL(*this, KBestThumbIndex, &iResolution, ENone, EFalse, iPriority) );
       
   184 
       
   185     if( error != KErrNone )
       
   186         {
       
   187         IPTVLOGSTRING2_LOW_LEVEL( "CIptvThumbnailImpl::NotifyVideoClipInfoReady: GetFrameL FAILED, leave=%d", error );
       
   188         iObserver.ThumbnailCompleted( error );
       
   189         IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::NotifyVideoClipInfoReady OUT" );
       
   190         return;
       
   191         }
       
   192 
       
   193     IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::NotifyVideoClipInfoReady OK" );
       
   194     }
       
   195 
       
   196 // ---------------------------------------------------------------------------
       
   197 // NotifyVideoClipFrameCompleted
       
   198 // ---------------------------------------------------------------------------
       
   199 void CIptvThumbnailImpl::NotifyVideoClipThumbCompleted( CTNEVideoClipInfo&, TInt aError, CFbsBitmap* aThumbBitmap )
       
   200     {
       
   201     IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::NotifyVideoClipFrameCompleted IN" );
       
   202 
       
   203     // Ownership transfer: manage aThumbBitmap from now on
       
   204     // Note: Object needs clean-up also in error case.
       
   205     iThumbBitmap = aThumbBitmap;
       
   206 
       
   207     // Check for errors on thumb creation, like format not supported, codec not found
       
   208     if( aError != KErrNone )
       
   209         {
       
   210         // Note: Must not cleanup here. VED library (or Thumbnail Engine library) uses its 
       
   211         // own member variables immediately after this event has finished. Would cause crash.
       
   212         // In this case we are trusting on clean-up in next thumbnail or class destructor.
       
   213         IPTVLOGSTRING2_LOW_LEVEL( "CIptvThumbnailImpl::NotifyVideoClipFrameCompleted: GetFrameL FAILED, aError=%d", aError );
       
   214         iObserver.ThumbnailCompleted( aError );
       
   215         IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::NotifyVideoClipFrameCompleted OUT" );
       
   216         return;
       
   217         }
       
   218 
       
   219     // 3. Request thumbnail encoding to file
       
   220     _LIT8( KJpgMimeType, "image/jpeg" );
       
   221     TRAPD( error, iImageEncoder = CImageEncoder::FileNewL(iFs, iThumbFilename->Des(), KJpgMimeType) );
       
   222     if( error != KErrNone )
       
   223         {
       
   224         IPTVLOGSTRING2_LOW_LEVEL( "CIptvThumbnailImpl::NotifyVideoClipFrameCompleted: FileNewL FAILED, aError=%d", error );
       
   225         iObserver.ThumbnailCompleted( error );
       
   226         IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::NotifyVideoClipFrameCompleted OUT" );
       
   227         return;
       
   228         }
       
   229 
       
   230     iImageEncoder->Convert( &iStatus, *iThumbBitmap );
       
   231     SetActive();
       
   232     IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::NotifyVideoClipFrameCompleted OUT" );
       
   233     }
       
   234 
       
   235 // ---------------------------------------------------------------------------
       
   236 // RunL
       
   237 // ---------------------------------------------------------------------------
       
   238 void CIptvThumbnailImpl::RunL()
       
   239     {
       
   240     IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::RunL IN" );
       
   241 
       
   242     // Check for errors on image conversion and file save
       
   243     if( iStatus.Int() != KErrNone )
       
   244         {
       
   245         IPTVLOGSTRING2_LOW_LEVEL( "CIptvThumbnailImpl::RunL CImageEncoder::Convert FAILED, error=%d", iStatus.Int() );
       
   246         DoCleanup();
       
   247         iObserver.ThumbnailCompleted( iStatus.Int() );
       
   248         return;
       
   249         }
       
   250 
       
   251     // 4. Everything went fine, signal user
       
   252     DoCleanup();
       
   253     iObserver.ThumbnailCompleted( KErrNone );
       
   254     IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::RunL OUT" );
       
   255     }
       
   256 
       
   257 // ---------------------------------------------------------------------------
       
   258 // DoCancel
       
   259 // ---------------------------------------------------------------------------
       
   260 void CIptvThumbnailImpl::DoCancel()
       
   261     {
       
   262     IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::DoCancel IN" );
       
   263     DoCleanup();
       
   264     IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::DoCancel OUT" );
       
   265     }
       
   266 
       
   267 // ---------------------------------------------------------------------------
       
   268 // DoStartThumbnailL
       
   269 // ---------------------------------------------------------------------------
       
   270 void CIptvThumbnailImpl::DoStartThumbnailL( const TDesC& aClipFilename )
       
   271     {
       
   272     IPTVLOGSTRING2_LOW_LEVEL( "CIptvThumbnailImpl::DoStartThumbnailL IN, clip-filename='%S'", &aClipFilename );
       
   273     iClipInfo = CTNEVideoClipInfo::NewL( aClipFilename, *this );
       
   274     IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::DoStartThumbnailL OUT" );
       
   275     }
       
   276 
       
   277 // ---------------------------------------------------------------------------
       
   278 // RtpTo3gpConversionReady
       
   279 // ---------------------------------------------------------------------------
       
   280 void CIptvThumbnailImpl::RtpTo3gpConversionReady( CIptvRtpTo3gpConverter&, TInt aError )
       
   281     {
       
   282     IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::RtpTo3gpConversionReady IN" );
       
   283 
       
   284     // Check for errors in conversion
       
   285     if( aError!=KErrNone )
       
   286         {
       
   287         IPTVLOGSTRING2_LOW_LEVEL( "CIptvThumbnailImpl::RtpTo3gpConversionReady: ConvertL FAILED, error=%d", aError );
       
   288         DoCleanup();
       
   289         iObserver.ThumbnailCompleted( aError );
       
   290         IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::RtpTo3gpConversionReady OUT [1]" );
       
   291         return;
       
   292         }
       
   293 
       
   294     // Start actual thumbnail generation
       
   295     TRAPD( error, DoStartThumbnailL(iTempClipFilename) );
       
   296     if( error!=KErrNone )
       
   297         {
       
   298         IPTVLOGSTRING2_LOW_LEVEL( "CIptvThumbnailImpl::RtpTo3gpConversionReady: DoStartThumbnailL FAILED, error=%d", error );
       
   299         DoCleanup();
       
   300         iObserver.ThumbnailCompleted( error );
       
   301         IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::RtpTo3gpConversionReady OUT [2]" );
       
   302         return;
       
   303         }
       
   304 
       
   305     IPTVLOGSTRING_LOW_LEVEL( "CIptvThumbnailImpl::RtpTo3gpConversionReady OUT [0]" );
       
   306     }