mmserv/metadatautility/Src/MetaDataBufferAgg.cpp
changeset 0 71ca22bcf22a
equal deleted inserted replaced
-1:000000000000 0:71ca22bcf22a
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Active object for buffer aggregation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "MetaDataUtilityBody.h"
       
    21 #include "MDUChunkDataObserver.h"
       
    22 #include "MetaDataBufferAgg.h"
       
    23 
       
    24 // CONSTANTS
       
    25 // (ID3v2 specification found in www.id3.org)
       
    26 // ID3v2 header consists of following parts:
       
    27 // - identifier "ID3", 3 bytes
       
    28 // - version, 2 bytes
       
    29 // - flags, 1 byte
       
    30 // - data length, 4 bytes
       
    31 const TInt KID3v2HeaderLength           = 10;
       
    32 const TInt KID3v2TagLength              = 3;
       
    33 
       
    34 _LIT8( KID3v2Tag, "ID3" );              // ID3 metadata format indication
       
    35 
       
    36 
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CMetaDataBufferAgg::MetaDataBufferAgg
       
    40 // C++ default constructor can NOT contain any code, that
       
    41 // might leave.
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 CMetaDataBufferAgg::CMetaDataBufferAgg( CMetaDataUtilityBody* aBody,
       
    45         MMDUChunkDataObserver& aObserver )  
       
    46 : CActive( CActive::EPriorityStandard ), 
       
    47   iBody( aBody ),
       
    48   iObserver( aObserver ),
       
    49   iState( EFindIdentifier ),
       
    50   iUserChunk( NULL ),
       
    51   iFinalChunk( EFalse ),
       
    52   iMetaDataSize( 0 )  
       
    53     {
       
    54     CActiveScheduler::Add( this );
       
    55     }
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // Two-Phase Constructor
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 CMetaDataBufferAgg* CMetaDataBufferAgg::NewL( CMetaDataUtilityBody* aBody,
       
    62         TDesC8& aMimeType, 
       
    63         MMDUChunkDataObserver& aObserver  )
       
    64     {
       
    65     CMetaDataBufferAgg* self = new(ELeave) CMetaDataBufferAgg( aBody, aObserver );
       
    66     CleanupStack::PushL( self );
       
    67     self->ConstructL( aMimeType );
       
    68     CleanupStack::Pop( self );
       
    69     return self;
       
    70     }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CMetaDataBufferAgg::Construct()
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 void CMetaDataBufferAgg::ConstructL( const TDesC8& aMimeType )
       
    77     {
       
    78     // copy MimeType
       
    79     iMimeType.CreateL( aMimeType.Size() );    
       
    80     iMimeType = aMimeType;
       
    81     
       
    82     // allocate the buffers
       
    83     iPrevBuf.CreateL( 0 );
       
    84     iMetaDataBuf.CreateL ( 0 );
       
    85     
       
    86     }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // CMetaDataBufferAgg::~CMetaDataBufferAgg()
       
    90 // -----------------------------------------------------------------------------
       
    91 //
       
    92 CMetaDataBufferAgg::~CMetaDataBufferAgg()
       
    93     {
       
    94     iMimeType.Close();
       
    95     iPrevBuf.Close();
       
    96     iMetaDataBuf.Close();
       
    97     Cancel();
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CMetaDataBufferAgg::RequestProcessChunkData
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 TInt CMetaDataBufferAgg::RequestProcessChunkData( TDesC8& aChunk, 
       
   105         TBool aFinalChunk )
       
   106     {
       
   107     if (iState == EParseComplete )
       
   108         {
       
   109         return KErrAlreadyExists;
       
   110         }
       
   111     if (iState == EParseData )
       
   112         {
       
   113         return KErrCompletion;
       
   114         }
       
   115     if ( IsActive() )
       
   116         {
       
   117         return KErrNotReady;
       
   118         }
       
   119     
       
   120     iUserChunk = &aChunk;
       
   121     iFinalChunk = aFinalChunk;
       
   122 
       
   123     iStatus = KRequestPending;
       
   124     SetActive();  
       
   125 
       
   126     return KErrNone;
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CMetaDataBufferAgg::RequestParseChunkData
       
   131 // -----------------------------------------------------------------------------
       
   132 //
       
   133 TInt CMetaDataBufferAgg::RequestParseChunkData()
       
   134     {
       
   135     if (iState == EParseComplete )
       
   136         {
       
   137         return KErrAlreadyExists;
       
   138         }
       
   139     if ( IsActive() || iState != EParseData)
       
   140         {
       
   141         return KErrNotReady;
       
   142         }
       
   143 
       
   144     
       
   145     iStatus = KRequestPending;
       
   146     SetActive();  
       
   147 
       
   148     return KErrNone;
       
   149     }
       
   150 
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // CMetaDataBufferAgg::ReadyToGo
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 void CMetaDataBufferAgg::ReadyToGo()
       
   157 {
       
   158     TRequestStatus* status = &iStatus;
       
   159     User::RequestComplete( status, KErrNone );
       
   160 }
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // CMetaDataBufferAgg::DoCancel
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 void CMetaDataBufferAgg::DoCancel()
       
   167     {
       
   168     }
       
   169     
       
   170 // -----------------------------------------------------------------------------
       
   171 // CMetaDataUtility::RunL
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 void CMetaDataBufferAgg::RunL()
       
   175     {
       
   176 
       
   177     switch( iState )
       
   178         {
       
   179         case EFindIdentifier:
       
   180               {
       
   181               TRAPD( err, FindMetaDataIdentifierL( iUserChunk ));
       
   182               DoCallback( err );
       
   183               break;
       
   184               }
       
   185         case ECalculateSize:
       
   186               {
       
   187               TRAPD( err, CalculateMetaDataSizeL( iUserChunk ));
       
   188               DoCallback( err );
       
   189               break;
       
   190               }
       
   191         case ECollectData:
       
   192              {
       
   193              TRAPD( err, CollectMetaDataL( iUserChunk ));  
       
   194              DoCallback( err );
       
   195              break;
       
   196              }
       
   197         case EParseData:
       
   198              {
       
   199              TRAPD( err, iBody->DoParseChunkDataL( iMetaDataBuf, iMimeType )); 
       
   200              iState = EParseComplete;
       
   201              DoCallback( err );
       
   202              break;
       
   203              }
       
   204         default:
       
   205              break;
       
   206         }
       
   207       }
       
   208 
       
   209 
       
   210 // -----------------------------------------------------------------------------
       
   211 // CMetaDataBufferAgg::DoCallback
       
   212 // -----------------------------------------------------------------------------
       
   213 //
       
   214 void CMetaDataBufferAgg::DoCallback( TInt aError)
       
   215     {
       
   216     if ( iFinalChunk )
       
   217         { 
       
   218         switch (iState)
       
   219             {            
       
   220             case EFindIdentifier:
       
   221             case ECalculateSize:
       
   222                 {
       
   223                 iObserver.HandleChunkDataProcessed( KErrNotFound );                                              
       
   224                 break;
       
   225                 }
       
   226             case ECollectData:
       
   227             case EParseData:
       
   228                 { 
       
   229                 iState = EParseData;
       
   230                 iObserver.HandleChunkDataReadyToBeParsed();
       
   231                 break;
       
   232                 }
       
   233             case EParseComplete:
       
   234                 {
       
   235                 iObserver.HandleChunkDataComplete( aError );                                              
       
   236                 break;
       
   237                 }
       
   238             }
       
   239         }
       
   240     else
       
   241         {
       
   242         switch (iState)
       
   243             {            
       
   244             case EFindIdentifier:
       
   245             case ECalculateSize:
       
   246             case ECollectData:
       
   247                 {
       
   248                 iObserver.HandleChunkDataProcessed( KErrNone );                                               
       
   249                 break;
       
   250                 }
       
   251             case EParseData:
       
   252                 { 
       
   253                 iObserver.HandleChunkDataReadyToBeParsed();
       
   254                 break;
       
   255                 }
       
   256             case EParseComplete:
       
   257                 {
       
   258                 iObserver.HandleChunkDataComplete( aError );                                              
       
   259                 break;
       
   260                 }
       
   261             }        
       
   262         }
       
   263         
       
   264     }
       
   265 
       
   266 
       
   267 // -----------------------------------------------------------------------------
       
   268 // CMetaDataBufferAgg::FindMetaDataIdentifier
       
   269 // -----------------------------------------------------------------------------
       
   270 //
       
   271 void CMetaDataBufferAgg::FindMetaDataIdentifierL( TDesC8* aBuf )
       
   272     {
       
   273     // search from client buffer first
       
   274     TInt offset = aBuf->Find( KID3v2Tag );
       
   275     if ( offset != KErrNotFound )
       
   276         {
       
   277         // Identifier is found, copy to iMetaDataBuf
       
   278         TUint8* ptr = const_cast<TUint8*>(aBuf->Ptr());
       
   279         CopyBufferL( &iMetaDataBuf, ptr+offset, aBuf->Size() - offset );
       
   280                       
       
   281         ComputeSize( &iMetaDataBuf, 0 );
       
   282 
       
   283         if ( !iMetaDataSize )
       
   284             {
       
   285             iState = ECalculateSize;              
       
   286             }
       
   287         else
       
   288             {
       
   289             if ( iMetaDataBuf.Size() >= iMetaDataSize )
       
   290                 {
       
   291                 iState = EParseData;      
       
   292                 }  
       
   293             else
       
   294                 {
       
   295                 iState = ECollectData;                              
       
   296                 }
       
   297             }       
       
   298         }
       
   299     else
       
   300         {
       
   301         // Identifier is NOT found, need to process the previous data
       
   302          ProcessPreviousChunkL( aBuf );
       
   303         }
       
   304     }
       
   305     
       
   306 // -----------------------------------------------------------------------------
       
   307 // CMetaDataBufferAgg::FindMetaDataIdentifier
       
   308 // -----------------------------------------------------------------------------
       
   309 //
       
   310 void CMetaDataBufferAgg::ProcessPreviousChunkL( TDesC8* aBuf )
       
   311     {
       
   312     TInt prevSize = iPrevBuf.Size();   
       
   313     TInt bufSize = aBuf->Size();
       
   314     TInt sizeToCopy = 0;
       
   315     
       
   316     //If there is not enough data for 2*KID3v2TagLength
       
   317     //The following lists the posibility of the iPrevBuf content:
       
   318     //      . . . . . .
       
   319     //      . . . . . I
       
   320     //      . . . . I D
       
   321     //      . . I D 3 .
       
   322     //  or it contains "ID3" at different byte
       
   323     if ( prevSize + bufSize < 2*KID3v2TagLength )
       
   324         {
       
   325         TUint8* ptr = const_cast<TUint8*> (aBuf->Ptr());
       
   326         
       
   327         // just do the append
       
   328         AppendBufferL( &iPrevBuf, ptr, bufSize );
       
   329         return;
       
   330         }
       
   331     else
       
   332         {
       
   333         sizeToCopy = 2*KID3v2TagLength - prevSize;
       
   334         }
       
   335        
       
   336     //Add into the previous buf (iPrevBuf)
       
   337     TUint8* ptr = const_cast<TUint8*> (aBuf->Ptr());
       
   338     AppendBufferL( &iPrevBuf, ptr, sizeToCopy );    
       
   339     
       
   340     // search from previous buffer 
       
   341     TInt offset = iPrevBuf.Find( KID3v2Tag );
       
   342     
       
   343     // tag IS found
       
   344     if ( offset != KErrNotFound )
       
   345         {
       
   346         //Found identifier, save previous buf data into iMetaDataBuf
       
   347         ptr = const_cast<TUint8*> (iPrevBuf.Ptr());
       
   348         AppendBufferL( &iMetaDataBuf, ptr+offset, iPrevBuf.Size() - offset );
       
   349         
       
   350         //Append the rest of aBuf
       
   351         ptr = const_cast<TUint8*> (aBuf->Ptr());
       
   352         AppendBufferL( &iMetaDataBuf, ptr+sizeToCopy, aBuf->Size() - sizeToCopy );
       
   353                 
       
   354         ComputeSize( &iMetaDataBuf, 0 );  
       
   355         if ( !iMetaDataSize )
       
   356             {
       
   357             iState = ECalculateSize;              
       
   358             }
       
   359         else
       
   360             {
       
   361             if ( iMetaDataBuf.Size() >= iMetaDataSize )
       
   362                 {
       
   363                 iState = EParseData;      
       
   364                 }  
       
   365             else
       
   366                 {
       
   367                 iState = ECollectData;                              
       
   368                 }
       
   369             }     
       
   370         }
       
   371     
       
   372     // tag NOT found
       
   373     else
       
   374         {
       
   375         //Re-populate iPrevBuf 
       
   376         if ( aBuf->Size() < KID3v2TagLength )
       
   377             {
       
   378             //Append the rest of aBuf into iPrevBuf
       
   379             ptr = const_cast<TUint8*> (aBuf->Ptr());        
       
   380             AppendBufferL( &iPrevBuf, ptr+sizeToCopy, aBuf->Size() - sizeToCopy );
       
   381             
       
   382             //copy the last "KID3v2TagLength" size of aBuf into iPrevBuf
       
   383             iPrevBuf = iPrevBuf.Mid(iPrevBuf.Size() - KID3v2TagLength);
       
   384             }
       
   385         else
       
   386             {
       
   387             //copy the last "KID3v2TagLength" size of aBuf into iPrevBuf
       
   388             offset = aBuf->Size() - KID3v2TagLength;
       
   389             TUint8* ptr = const_cast<TUint8*>(aBuf->Ptr());
       
   390             CopyBufferL( &iPrevBuf, ptr+offset, KID3v2TagLength );
       
   391             }
       
   392         }
       
   393     }
       
   394 
       
   395 // -----------------------------------------------------------------------------
       
   396 // CMetaDataBufferAgg::CalculateMetaDataSize
       
   397 // -----------------------------------------------------------------------------
       
   398 //
       
   399 void CMetaDataBufferAgg::CalculateMetaDataSizeL( TDesC8* aBuf )
       
   400     {
       
   401     TUint8* ptr = const_cast<TUint8*> (aBuf->Ptr());
       
   402     AppendBufferL( &iMetaDataBuf, ptr, aBuf->Size() );    
       
   403 
       
   404     // Try to compute the metadata size
       
   405     ComputeSize( &iMetaDataBuf, 0 );
       
   406 
       
   407     // check whether metadata size is available ( non zero)
       
   408     if ( iMetaDataSize )
       
   409         {
       
   410         iState = ECollectData;              
       
   411         }
       
   412     
       
   413     if ( iMetaDataBuf.Size() >= iMetaDataSize )
       
   414         {
       
   415         iState = EParseData;      
       
   416         }
       
   417     }   
       
   418 
       
   419 // -----------------------------------------------------------------------------
       
   420 // CMetaDataBufferAgg::ComputeSize
       
   421 // -----------------------------------------------------------------------------
       
   422 //
       
   423 void CMetaDataBufferAgg::ComputeSize( TDesC8* aBuf, TInt aOffset )
       
   424     {
       
   425     if ( aBuf->Size() < ( aOffset + KID3v2HeaderLength ) )
       
   426         {
       
   427         iMetaDataSize = 0;
       
   428         return;
       
   429         }
       
   430             
       
   431     // calculate the size
       
   432     for ( TInt i = 6; i < 10; i++ )
       
   433         {
       
   434         iMetaDataSize <<= 7;
       
   435         iMetaDataSize |= (*aBuf)[i+aOffset] & 0x7f;
       
   436         }
       
   437     iMetaDataSize += KID3v2HeaderLength; // add the header for total size
       
   438     
       
   439     }   
       
   440    
       
   441 // -----------------------------------------------------------------------------
       
   442 // CMetaDataBufferAgg::CollectMetaData
       
   443 // -----------------------------------------------------------------------------
       
   444 //
       
   445 void CMetaDataBufferAgg::CollectMetaDataL( TDesC8* aBuf )
       
   446     {
       
   447     TUint8* ptr = const_cast<TUint8*> (aBuf->Ptr());
       
   448     AppendBufferL( &iMetaDataBuf, ptr, aBuf->Size() );            
       
   449     
       
   450     if ( iMetaDataBuf.Size() >= iMetaDataSize )
       
   451         {
       
   452         iState = EParseData;      
       
   453         }
       
   454      }
       
   455 
       
   456 
       
   457 // -----------------------------------------------------------------------------
       
   458 // CMetaDataBufferAgg::CopyBufferL
       
   459 // -----------------------------------------------------------------------------
       
   460 //
       
   461 void CMetaDataBufferAgg::CopyBufferL(RBuf8* aDes, TUint8* aPtr, TInt aSize )
       
   462     {
       
   463      if ( aDes->MaxLength() < aSize)
       
   464         {
       
   465         aDes->ReAllocL( aSize );
       
   466         }
       
   467      aDes->Copy( aPtr, aSize );
       
   468     }
       
   469 
       
   470 
       
   471 // -----------------------------------------------------------------------------
       
   472 // CMetaDataBufferAgg::AppendBufferL
       
   473 // -----------------------------------------------------------------------------
       
   474 //
       
   475 void CMetaDataBufferAgg::AppendBufferL(RBuf8* aDes, TUint8* aPtr, TInt aSize )
       
   476     {
       
   477     TInt size = aDes->Size();
       
   478     if ( aDes->MaxLength() < aSize+size)
       
   479         {
       
   480         aDes->ReAllocL( aSize+size );
       
   481         }
       
   482      aDes->Append( aPtr, aSize );
       
   483     }
       
   484 
       
   485