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