connectivitymodules/SeCon/services/pcd/src/sconvideoparser.cpp
changeset 0 d0791faffa3f
child 1 f8e15b44d440
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 /*
       
     2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  CSConVideoParser implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <coedef.h> 
       
    20 #include <bautils.h>
       
    21 #include <imageconversion.h>
       
    22 #include "sconvideoparser.h"
       
    23 #include "debug.h"
       
    24 
       
    25 _LIT( KMimeType, "MimeType" );
       
    26 _LIT8( KImageEncoderMimeType, "image/jpeg" );
       
    27 _LIT( KMimeTypeAudio, "audio/*" );
       
    28 _LIT( KMimeTypeVideo, "video/*" );
       
    29 
       
    30 const TInt KVideoClipTimeout( 10000000 ); // 10 sec.
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 // CSConVideoParser::CSConVideoParser()
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 CSConVideoParser::CSConVideoParser()
       
    37     {
       
    38     }
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CSConVideoParser::~CSConVideoParser()
       
    42 // Destructor
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CSConVideoParser::~CSConVideoParser()
       
    46     {
       
    47     TRACE_FUNC_ENTRY;
       
    48     delete iTimeOut;
       
    49     if ( iVideoClip )
       
    50         {
       
    51         iVideoClip->CancelThumb();
       
    52         }
       
    53     delete iVideoClip;
       
    54     
       
    55     if ( iVideoUtil )
       
    56         {
       
    57         iVideoUtil->Close();
       
    58         }
       
    59     delete iVideoUtil;
       
    60     delete iScreen;
       
    61     iWsSession.Close();
       
    62     
       
    63     delete iThumbnail;
       
    64     delete iVideoMimeType;
       
    65     delete iAudioMimeType;
       
    66     TRACE_FUNC_EXIT;
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CSConVideoParser::NewLC()
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 CSConVideoParser* CSConVideoParser::NewLC()
       
    74     {
       
    75     CSConVideoParser* self = new (ELeave)CSConVideoParser();
       
    76     CleanupStack::PushL( self );
       
    77     self->ConstructL();
       
    78     return self;
       
    79     }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // CSConVideoParser::ConstructL()
       
    83 // Constructor
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 void CSConVideoParser::ConstructL()
       
    87     {
       
    88     TRACE_FUNC_ENTRY;
       
    89     
       
    90     User::LeaveIfError( iWsSession.Connect() );
       
    91     
       
    92     iScreen = new(ELeave) CWsScreenDevice( iWsSession );
       
    93     iScreen->Construct();
       
    94     
       
    95     RWindowGroup wg( iWsSession );
       
    96     User::LeaveIfError( wg.Construct(reinterpret_cast<TUint32>(&wg), EFalse) );
       
    97     CleanupClosePushL( wg );
       
    98     
       
    99     CWindowGc* gc;
       
   100     User::LeaveIfError( iScreen->CreateContext(gc) );
       
   101     CleanupStack::PushL(gc);
       
   102     
       
   103     RWindow window( iWsSession );
       
   104     User::LeaveIfError( window.Construct(wg, reinterpret_cast<TUint32>(&wg) + 1) );
       
   105     CleanupClosePushL( window );
       
   106     
       
   107     TRect temp(0,0,320,240); // dummy parameter
       
   108     iVideoUtil = CVideoPlayerUtility::NewL(*this, EMdaPriorityNormal, EMdaPriorityPreferenceNone, iWsSession, *(iScreen), window, temp, temp);
       
   109     
       
   110     CleanupStack::PopAndDestroy( &window );
       
   111     CleanupStack::PopAndDestroy( gc );
       
   112     CleanupStack::PopAndDestroy( &wg );
       
   113     
       
   114     iTimeOut = CSconTimeOut::NewL( *this );
       
   115     
       
   116     TRACE_FUNC_EXIT;
       
   117     }
       
   118 
       
   119 // -----------------------------------------------------------------------------
       
   120 // CSConVideoParser::OpenFileL( const RFs& aFs, const TDesC& aFileName )
       
   121 // Opens video file
       
   122 // -----------------------------------------------------------------------------
       
   123 //
       
   124 void CSConVideoParser::OpenFileL( const RFs& aFs, const TDesC& aFileName )
       
   125     {
       
   126     TRACE_FUNC_ENTRY;
       
   127     
       
   128     iVideoUtilReady = EFalse;
       
   129     iVideoClipReady = EFalse;
       
   130     iVideoUtilErr = KErrNone;
       
   131     iVideoClipErr = KErrNone;
       
   132     
       
   133     delete iThumbnail;
       
   134     iThumbnail = NULL;
       
   135     delete iVideoMimeType;
       
   136     iVideoMimeType=NULL;
       
   137     delete iAudioMimeType;
       
   138     iAudioMimeType=NULL;
       
   139     
       
   140     if ( iVideoClip )
       
   141         {
       
   142         iVideoClip->CancelThumb();
       
   143         delete iVideoClip;
       
   144         iVideoClip = NULL;
       
   145         }
       
   146     if ( iVideoUtil )
       
   147         {
       
   148         iVideoUtil->Close();
       
   149         }
       
   150     
       
   151     TBool fileExist = BaflUtils::FileExists( aFs, aFileName );
       
   152     if ( !fileExist )
       
   153         {
       
   154         User::Leave( KErrNotFound );
       
   155         }
       
   156     
       
   157     iVideoClip = CTNEVideoClipInfo::NewL( aFileName, *this );
       
   158     
       
   159     iVideoUtil->OpenFileL( aFileName );
       
   160     
       
   161     
       
   162     LOGGER_WRITE("iWait.Start()");
       
   163     iWait.Start();
       
   164     
       
   165     TRACE_FUNC_EXIT;
       
   166     }
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CSConVideoParser::Thumbnail()
       
   170 // returns video thumbnail in jpg format
       
   171 // -----------------------------------------------------------------------------
       
   172 //
       
   173 const TDesC8& CSConVideoParser::Thumbnail() const
       
   174     {
       
   175     TRACE_FUNC;
       
   176     if ( !iThumbnail )
       
   177         {
       
   178         return KNullDesC8;
       
   179         }
       
   180     return *iThumbnail;
       
   181     }
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 // CSConVideoParser::VideoFrameRateL()
       
   185 // returns video frame rate
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 TReal32 CSConVideoParser::VideoFrameRateL() const
       
   189     {
       
   190     TRACE_FUNC;
       
   191     if ( !iVideoUtil )
       
   192         {
       
   193         User::Leave( KErrNotReady );
       
   194         }
       
   195     return iVideoUtil->VideoFrameRateL();
       
   196     }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CSConVideoParser::VideoFrameSizeL()
       
   200 // returns video frame size
       
   201 // -----------------------------------------------------------------------------
       
   202 //
       
   203 void CSConVideoParser::VideoFrameSizeL(TSize& aSize) const
       
   204     {
       
   205     TRACE_FUNC;
       
   206     if ( !iVideoUtil )
       
   207         {
       
   208         User::Leave( KErrNotReady );
       
   209         }
       
   210     return iVideoUtil->VideoFrameSizeL( aSize );
       
   211     }
       
   212 
       
   213 // -----------------------------------------------------------------------------
       
   214 // CSConVideoParser::VideoFormatMimeTypeL()
       
   215 // returns video format
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 const TDesC8& CSConVideoParser::VideoFormatMimeTypeL() const 
       
   219     {
       
   220     TRACE_FUNC;
       
   221     if ( !iVideoUtil )
       
   222         {
       
   223         User::Leave( KErrNotReady );
       
   224         }
       
   225     return iVideoUtil->VideoFormatMimeType();
       
   226     }
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // CSConVideoParser::VideoBitRateL()
       
   230 // returns videostream bitrate
       
   231 // -----------------------------------------------------------------------------
       
   232 //
       
   233 TInt CSConVideoParser::VideoBitRateL() const
       
   234     {
       
   235     TRACE_FUNC;
       
   236     if ( !iVideoUtil )
       
   237         {
       
   238         User::Leave( KErrNotReady );
       
   239         }
       
   240     return iVideoUtil->VideoBitRateL();
       
   241     }
       
   242 
       
   243 // -----------------------------------------------------------------------------
       
   244 // CSConVideoParser::AudioBitRateL()
       
   245 // returns audiostream bitrate
       
   246 // -----------------------------------------------------------------------------
       
   247 //
       
   248 TInt CSConVideoParser::AudioBitRateL() const
       
   249     {
       
   250     TRACE_FUNC;
       
   251     if ( !iVideoUtil )
       
   252         {
       
   253         User::Leave( KErrNotReady );
       
   254         }
       
   255     return iVideoUtil->AudioBitRateL();
       
   256     }
       
   257 
       
   258 // -----------------------------------------------------------------------------
       
   259 // CSConVideoParser::DurationL()
       
   260 // returns video duration in milliseconds
       
   261 // -----------------------------------------------------------------------------
       
   262 //
       
   263 TInt CSConVideoParser::DurationL() const
       
   264     {
       
   265     TRACE_FUNC;
       
   266     if ( !iVideoUtil )
       
   267         {
       
   268         User::Leave( KErrNotReady );
       
   269         }
       
   270     return iVideoUtil->DurationL().Int64()/1000;
       
   271     }
       
   272 
       
   273 // -----------------------------------------------------------------------------
       
   274 // CSConVideoParser::VideoMimeTypeL()
       
   275 // returns video mime type
       
   276 // -----------------------------------------------------------------------------
       
   277 //
       
   278 const TDesC& CSConVideoParser::VideoMimeTypeL()
       
   279     {
       
   280     TRACE_FUNC;
       
   281     if ( !iVideoUtil )
       
   282         {
       
   283         User::Leave( KErrNotReady );
       
   284         }
       
   285     delete iVideoMimeType;
       
   286     iVideoMimeType = NULL;
       
   287     
       
   288     TInt metadataEntries = iVideoUtil->NumberOfMetaDataEntriesL();
       
   289     LOGGER_WRITE_1( "metadataEntries: %d", metadataEntries);
       
   290     
       
   291     for (TInt i=0; i < metadataEntries ; i++)
       
   292         {
       
   293         CMMFMetaDataEntry* entry = iVideoUtil->MetaDataEntryL(i);
       
   294         CleanupStack::PushL( entry );
       
   295         if ( entry->Name().Compare( KMimeType ) == 0 )
       
   296             {
       
   297             if ( entry->Value().MatchF( KMimeTypeVideo ) == 0 )
       
   298                 {
       
   299                 LOGGER_WRITE_1( "video mimetype found: %S", &entry->Value());
       
   300                 iVideoMimeType = HBufC::NewL(entry->Value().Length());
       
   301                 TPtr videoMimeTypePtr = iVideoMimeType->Des();
       
   302                 videoMimeTypePtr.Copy( entry->Value() );
       
   303                 CleanupStack::PopAndDestroy( entry );
       
   304                 return *iVideoMimeType;
       
   305                 }
       
   306             }
       
   307         CleanupStack::PopAndDestroy( entry );
       
   308         }
       
   309     return KNullDesC;
       
   310     }
       
   311 
       
   312 // -----------------------------------------------------------------------------
       
   313 // CSConVideoParser::AudioMimeTypeL()
       
   314 // returns audio mime type
       
   315 // -----------------------------------------------------------------------------
       
   316 //
       
   317 const TDesC& CSConVideoParser::AudioMimeTypeL()
       
   318     {
       
   319     TRACE_FUNC;
       
   320     if ( !iVideoUtil )
       
   321         {
       
   322         User::Leave( KErrNotReady );
       
   323         }
       
   324     delete iAudioMimeType;
       
   325     iAudioMimeType = NULL;
       
   326     TInt metadataEntries = iVideoUtil->NumberOfMetaDataEntriesL();
       
   327     LOGGER_WRITE_1( "metadataEntries: %d", metadataEntries);
       
   328     
       
   329     for (TInt i=0; i < metadataEntries ; i++)
       
   330         {
       
   331         CMMFMetaDataEntry* entry = iVideoUtil->MetaDataEntryL(i);
       
   332         CleanupStack::PushL( entry );
       
   333         
       
   334         if ( entry->Name().Compare( KMimeType ) == 0 )
       
   335             {
       
   336             if ( entry->Value().MatchF( KMimeTypeAudio ) == 0 )
       
   337                 {
       
   338                 LOGGER_WRITE_1( "video mimetype found: %S", &entry->Value());
       
   339                 iAudioMimeType = HBufC::NewL(entry->Value().Length());
       
   340                 TPtr audioMimeTypePtr = iAudioMimeType->Des();
       
   341                 audioMimeTypePtr.Copy( entry->Value() );
       
   342                 CleanupStack::PopAndDestroy( entry );
       
   343                 return *iAudioMimeType;
       
   344                 }
       
   345             }
       
   346         CleanupStack::PopAndDestroy( entry );
       
   347         }
       
   348     return KNullDesC;
       
   349     }
       
   350 
       
   351 // -----------------------------------------------------------------------------
       
   352 // CSConVideoParser::MvpuoOpenComplete( TInt aError )
       
   353 // MVideoPlayerUtilityObserver implementation
       
   354 // -----------------------------------------------------------------------------
       
   355 //
       
   356 void CSConVideoParser::MvpuoOpenComplete( TInt aError )
       
   357     {
       
   358     TRACE_FUNC_ENTRY;
       
   359     LOGGER_WRITE_1( "aError: %d", aError );
       
   360     if ( aError == KErrNone )
       
   361         {
       
   362         iVideoUtil->Prepare();
       
   363         }
       
   364     else
       
   365         {
       
   366         iVideoUtilReady = ETrue;
       
   367         iVideoUtilErr = aError;
       
   368         }
       
   369     
       
   370     if ( iVideoUtilReady && iVideoClipReady )
       
   371         {
       
   372         LOGGER_WRITE("AsyncStop");
       
   373         iWait.AsyncStop();
       
   374         }
       
   375     TRACE_FUNC_EXIT;
       
   376     }
       
   377 
       
   378 // -----------------------------------------------------------------------------
       
   379 // CSConVideoParser::MvpuoPrepareComplete( TInt aError )
       
   380 // MVideoPlayerUtilityObserver implementation
       
   381 // -----------------------------------------------------------------------------
       
   382 //
       
   383 void CSConVideoParser::MvpuoPrepareComplete( TInt aError )
       
   384     {
       
   385     TRACE_FUNC_ENTRY;
       
   386     LOGGER_WRITE_1( "aError: %d", aError );
       
   387     
       
   388     iVideoUtilReady = ETrue;
       
   389     iVideoUtilErr = aError;
       
   390     
       
   391     if ( iVideoUtilReady && iVideoClipReady )
       
   392         {
       
   393         LOGGER_WRITE("AsyncStop");
       
   394         iWait.AsyncStop();
       
   395         }
       
   396     TRACE_FUNC_EXIT;
       
   397     }
       
   398 
       
   399 // -----------------------------------------------------------------------------
       
   400 // CSConVideoParser::MvpuoFrameReady()
       
   401 // MVideoPlayerUtilityObserver implementation
       
   402 // -----------------------------------------------------------------------------
       
   403 //
       
   404 void CSConVideoParser::MvpuoFrameReady( CFbsBitmap& /*aFrame*/,TInt /*aError*/ )
       
   405     {
       
   406     }
       
   407 
       
   408 // -----------------------------------------------------------------------------
       
   409 // CSConVideoParser::MvpuoPlayComplete()
       
   410 // MVideoPlayerUtilityObserver implementation
       
   411 // -----------------------------------------------------------------------------
       
   412 //
       
   413 void CSConVideoParser::MvpuoPlayComplete( TInt /*aError*/ )
       
   414     {
       
   415     }
       
   416 
       
   417 // -----------------------------------------------------------------------------
       
   418 // CSConVideoParser::MvpuoEvent()
       
   419 // MVideoPlayerUtilityObserver implementation
       
   420 // -----------------------------------------------------------------------------
       
   421 //
       
   422 void CSConVideoParser::MvpuoEvent( const TMMFEvent& /*aEvent*/ )
       
   423     {
       
   424     }
       
   425 
       
   426 // -----------------------------------------------------------------------------
       
   427 // CSConVideoParser::NotifyVideoClipInfoReady(CTNEVideoClipInfo& aInfo, TInt aError)
       
   428 // MTNEVideoClipInfoObserver implementation
       
   429 // -----------------------------------------------------------------------------
       
   430 //
       
   431 void CSConVideoParser::NotifyVideoClipInfoReady(CTNEVideoClipInfo& aInfo, TInt aError)
       
   432     {
       
   433     TRACE_FUNC_ENTRY;
       
   434     LOGGER_WRITE_1("aError: %d", aError);
       
   435     if ( aError == KErrNone )
       
   436         {
       
   437         TSize resolution(320,240);
       
   438         TRAPD( err, aInfo.GetThumbL(*this,KBestThumbIndex, &resolution ) );
       
   439         
       
   440         if ( err )
       
   441             {
       
   442             LOGGER_WRITE_1("aInfo.GetThumbL err: %d", err);
       
   443             iVideoClipReady = ETrue;
       
   444             iVideoClipErr = err;
       
   445             }
       
   446         else
       
   447             {
       
   448             LOGGER_WRITE("Start timeout");
       
   449             iTimeOut->Start( KVideoClipTimeout );
       
   450             }
       
   451         }
       
   452     else
       
   453         {
       
   454         iVideoClipReady = ETrue;
       
   455         iVideoClipErr = aError;
       
   456         }
       
   457     
       
   458     if ( iVideoUtilReady && iVideoClipReady )
       
   459         {
       
   460         LOGGER_WRITE("AsyncStop");
       
   461         iWait.AsyncStop();
       
   462         }
       
   463     TRACE_FUNC_EXIT;
       
   464     }
       
   465 
       
   466 // -----------------------------------------------------------------------------
       
   467 // CSConVideoParser::NotifyVideoClipThumbCompleted()
       
   468 // MTNEVideoClipInfoObserver implementation
       
   469 // -----------------------------------------------------------------------------
       
   470 //
       
   471 void CSConVideoParser::NotifyVideoClipThumbCompleted(CTNEVideoClipInfo& /*aInfo*/, 
       
   472         TInt aError, 
       
   473         CFbsBitmap* aThumb)
       
   474     {
       
   475     TRACE_FUNC_ENTRY;
       
   476     LOGGER_WRITE_1("aError: %d", aError);
       
   477     iTimeOut->Cancel();
       
   478     if ( aError == KErrNone)
       
   479         {
       
   480         delete iThumbnail;
       
   481         iThumbnail = NULL;
       
   482         LOGGER_WRITE("create CImageEncoder");
       
   483         CImageEncoder* encoder = NULL;
       
   484         TRAPD( err, encoder = CImageEncoder::DataNewL( iThumbnail, KImageEncoderMimeType, CImageEncoder::EOptionAlwaysThread ) );
       
   485         LOGGER_WRITE_1("err: %d", err );
       
   486         if ( err == KErrNone )
       
   487             {
       
   488             LOGGER_WRITE("Start Convert");
       
   489             TRequestStatus status;
       
   490             encoder->Convert( &status, *aThumb, NULL );
       
   491             
       
   492             User::WaitForRequest( status );
       
   493             LOGGER_WRITE_1("Convert status: %d", status.Int());
       
   494             LOGGER_WRITE_1("buf Length: %d", iThumbnail->Length());
       
   495             err = status.Int();
       
   496             }
       
   497         delete encoder;
       
   498         }
       
   499     delete aThumb;
       
   500     iVideoClipReady = ETrue;
       
   501     iVideoClipErr = aError;
       
   502     
       
   503     if ( iVideoUtilReady && iVideoClipReady )
       
   504         {
       
   505         LOGGER_WRITE("AsyncStop");
       
   506         iWait.AsyncStop();
       
   507         }
       
   508     
       
   509     TRACE_FUNC_EXIT;
       
   510     }
       
   511 
       
   512 // -----------------------------------------------------------------------------
       
   513 // CSConVideoParser::TimeOut()
       
   514 // Called when timeout has occured, cancels videoclip generation
       
   515 // -----------------------------------------------------------------------------
       
   516 //
       
   517 void CSConVideoParser::TimeOut()
       
   518     {
       
   519     TRACE_FUNC_ENTRY;
       
   520     if ( !iVideoClipReady )
       
   521         {
       
   522         iVideoClip->CancelThumb();
       
   523         LOGGER_WRITE("videoclip cancelled");
       
   524         iVideoClipReady = ETrue;
       
   525         iVideoClipErr = KErrCancel;
       
   526         }
       
   527     
       
   528     if ( iVideoUtilReady && iVideoClipReady )
       
   529         {
       
   530         LOGGER_WRITE("AsyncStop");
       
   531         iWait.AsyncStop();
       
   532         }
       
   533     TRACE_FUNC_EXIT;
       
   534     }