dvrengine/CommonRecordingEngine/DvrRtpClipHandler/src/CRtpClipRepairer.cpp
branchRCL_3
changeset 48 13a33d82ad98
parent 0 822a42b6c3f1
equal deleted inserted replaced
47:826cea16efd9 48:13a33d82ad98
       
     1 /*
       
     2 * Copyright (c) 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 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:    Implementation of the common recording engine file repairer class.*
       
    15 */
       
    16 
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CRtpClipRepairer.h"
       
    22 #include <ipvideo/CRtpMetaHeader.h>
       
    23 #include <ipvideo/CRtpUtil.h>
       
    24 #include <e32math.h>
       
    25 #include <bsp.h>
       
    26 #include "videoserviceutilsLogger.h"
       
    27 
       
    28 // CONSTANTS
       
    29 const TInt KMaxGroupTime( 4000 ); // 4s
       
    30 
       
    31 // ============================ MEMBER FUNCTIONS ===============================
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // CRtpClipRepairer::NewL
       
    35 // Static two-phased constructor. Leaves object to cleanup stack.
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 CRtpClipRepairer* CRtpClipRepairer::NewL( MRtpClipRepairObserver* aObs )
       
    39     {
       
    40     CRtpClipRepairer* self = new( ELeave ) CRtpClipRepairer( aObs );
       
    41     CleanupStack::PushL( self );
       
    42     self->ConstructL();
       
    43     CleanupStack::Pop( self );
       
    44     return self;
       
    45     }
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CRtpClipRepairer::CRtpClipRepairer
       
    49 // C++ default constructor can NOT contain any code, that might leave.
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CRtpClipRepairer::CRtpClipRepairer( MRtpClipRepairObserver* aObs )
       
    53   : CRtpFileBase(),
       
    54     iObs( aObs ),
       
    55     iSeekArrayPoint( KErrNotFound )
       
    56     {
       
    57     // None
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CRtpClipRepairer::ConstructL
       
    62 // Symbian 2nd phase constructor can leave.
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 void CRtpClipRepairer::ConstructL()
       
    66     {
       
    67     LOG( "CRtpClipRepairer::ConstructL()" );
       
    68 
       
    69     CRtpFileBase::ConstructL();
       
    70     }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // Destructor
       
    74 //
       
    75 CRtpClipRepairer::~CRtpClipRepairer()
       
    76 // -----------------------------------------------------------------------------
       
    77     {
       
    78     LOG( "CRtpClipRepairer::~CRtpClipRepairer()" );
       
    79     
       
    80     Cancel();
       
    81     delete iFileData;
       
    82     delete iMetaHeader;
       
    83     }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CRtpClipRepairer::CurrentClipName
       
    87 // Getter for the clip name under repairing.
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 TPtrC CRtpClipRepairer::CurrentClipName()
       
    91     {
       
    92     if ( iCurrentPath )
       
    93         {
       
    94         return iCurrentPath->Des();
       
    95         }
       
    96     
       
    97     return KNullDesC();
       
    98     }
       
    99     
       
   100 // -----------------------------------------------------------------------------
       
   101 // CRtpClipRepairer::CheckMetaHeaderL
       
   102 // Checks if corrupted meta header of clip is possible to fix.
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 void CRtpClipRepairer::CheckMetaHeaderL( const TDesC& aClipName )
       
   106     {
       
   107     LOG1( "CRtpClipRepairer::CheckMetaHeaderL(), aClipName: %S", &aClipName );
       
   108     
       
   109     // Only one repair at the time
       
   110     if ( iMetaHeader || iCurrentPath )
       
   111         {
       
   112         User::Leave( KErrAlreadyExists );
       
   113         }
       
   114     
       
   115     // Open clip and read the meta header
       
   116     delete iCurrentPath; iCurrentPath = NULL;
       
   117     iCurrentPath = aClipName.AllocL();
       
   118     if ( !iFs.Handle() )
       
   119         {
       
   120         User::LeaveIfError( iFs.Connect() );
       
   121         }
       
   122     iFile.Close();
       
   123     User::LeaveIfError( iFile.Open( iFs, aClipName,
       
   124                         EFileShareExclusive | EFileStream | EFileWrite ) );
       
   125     delete iMetaHeader; iMetaHeader = NULL;
       
   126     iMetaHeader = CRtpMetaHeader::NewL( iFile, CRtpMetaHeader::EMetaUpdate );
       
   127     
       
   128     // Attributes
       
   129     CRtpMetaHeader::SAttributes att;
       
   130     TRAPD( err, iMetaHeader->ReadAttributesL( att ) );
       
   131     
       
   132     // Verify that clip version not too old?
       
   133     if ( att.iVersion < KMinValidClipVersion )
       
   134         {
       
   135         LOG( "CRtpClipRepairer::CheckMetaHeaderL(), Not Valid Clip Version" );
       
   136         User::Leave( KErrGeneral ); 
       
   137         }
       
   138     
       
   139     // If error or rec ongoing -> recording interrupted ie. battery removed
       
   140     if ( err || att.iOngoing )
       
   141         {
       
   142         att.iOngoing = EFalse;
       
   143         att.iPlayCount = 0;
       
   144         att.iPlaySpot = KErrNone;
       
   145         iMetaHeader->WriteAttributesL( att );
       
   146         }
       
   147     
       
   148     // Duration
       
   149     TInt duration( 0 );
       
   150     iMetaHeader->ReadDurationL( duration );
       
   151     LOG1( "CRtpClipRepairer::CheckMetaHeaderL(), duration: %d", duration );
       
   152 
       
   153     // Update seek array
       
   154     if ( ( !duration || duration > KSeekArrayInterval ) && !ValidSeekHeaderL() )
       
   155         {
       
   156         UpdateSeekArrayL();
       
   157         }
       
   158     else
       
   159         {
       
   160         // Set start time to file date
       
   161         TTime startTime( 0 );
       
   162         iMetaHeader->ReadStartTimeL( startTime );
       
   163         iFile.SetModified( startTime );
       
   164         iFile.Close();
       
   165         }
       
   166     }
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CRtpClipRepairer::RunL
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 void CRtpClipRepairer::RunL()
       
   173     {
       
   174     User::LeaveIfError( iStatus.Int() );
       
   175     const TUint prevTime( iGroupTime );
       
   176     UpdateGroupHeaderVariablesL( iDataPtr );
       
   177     delete iFileData; iFileData = NULL;
       
   178 
       
   179     // Current header valid?
       
   180     if ( iThisGroup > iNextGroupPoint || iThisGroup < iPrevGroupPoint ||
       
   181          iGroupTime < prevTime || iGroupTime > ( prevTime + KMaxGroupTime ) )
       
   182         {
       
   183         iLastSeekAddr = iPrevGroupPoint;
       
   184         iThisGroup = iSeekArrayPoint;
       
   185         }
       
   186     else
       
   187         {
       
   188         // Handle readed group
       
   189         GroupToSeekArrayL();
       
   190         }
       
   191     
       
   192     // Continue with next group if repair asyncronous?
       
   193     if ( iObs )
       
   194         {
       
   195         if ( iThisGroup < iSeekArrayPoint )
       
   196             {
       
   197             // Asyncronous ( normal clip repairing )
       
   198             ReadNextGroupHeaderFromFileL();
       
   199             }
       
   200         else
       
   201             {
       
   202             // All done, finalize the clip
       
   203             AddSpecialPacketL( MRtpFileWriteObserver::ERtpClipEnd );
       
   204             FinalizeSeekArrayL( KErrNone );
       
   205             iObs->RtpClipRepaired( KErrNone );
       
   206             }        
       
   207         }
       
   208     }
       
   209     
       
   210 // -----------------------------------------------------------------------------
       
   211 // CRtpClipRepairer::RunError
       
   212 // Returns: System wide error code of indication send leave reason
       
   213 // -----------------------------------------------------------------------------
       
   214 //
       
   215 TInt CRtpClipRepairer::RunError( TInt aError )
       
   216     {
       
   217     LOG1( "CRtpClipRepairer::RunError(), RunL Leaved: %d", aError );
       
   218 
       
   219     TRAP_IGNORE( FinalizeSeekArrayL( aError ) );
       
   220     if ( iObs )
       
   221         {
       
   222         iObs->RtpClipRepaired( ( iGroupsTotalCount )? KErrNone: aError );
       
   223         }
       
   224     
       
   225     return KErrNone;
       
   226     }
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // CRtpClipRepairer::DoCancel
       
   230 // -----------------------------------------------------------------------------
       
   231 //
       
   232 void CRtpClipRepairer::DoCancel()
       
   233     {
       
   234     LOG( "CRtpClipRepairer::DoCancel()" );
       
   235     }
       
   236 
       
   237 // -----------------------------------------------------------------------------
       
   238 // CRtpClipRepairer::ValidSeekHeaderL
       
   239 // Verifies if seek header and seek array are valid.
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 TBool CRtpClipRepairer::ValidSeekHeaderL()
       
   243     {
       
   244     // Seek header
       
   245     iSeekHeaderPoint = iMetaHeader->SeekHeaderPoint();
       
   246     ReadSeekHeaderL();
       
   247     
       
   248     // Seek array point
       
   249     iMetaHeader->ReadSeekArrayPointL( iSeekArrayPoint );
       
   250     
       
   251     // Verify seek array
       
   252     if ( iLastSeekAddr > iFirstSeekAddr && iSeekArrayPoint > iLastSeekAddr )
       
   253         {
       
   254         TInt count( KErrNotFound );
       
   255         TRAPD( err, count = ReadSeekArrayL( iSeekArrayPoint ) );
       
   256         if ( !err && count > 0 )
       
   257             {
       
   258             // Seek array is ok
       
   259             return ETrue; 
       
   260             }
       
   261         }
       
   262     
       
   263     return EFalse;
       
   264     }
       
   265     
       
   266 // -----------------------------------------------------------------------------
       
   267 // CRtpClipRepairer::UpdateSeekArrayL
       
   268 // Scans all packet groups in clip and updates seek array.
       
   269 // -----------------------------------------------------------------------------
       
   270 //
       
   271 void CRtpClipRepairer::UpdateSeekArrayL()
       
   272     {
       
   273     LOG( "CRtpClipRepairer::UpdateSeekArrayL()" );
       
   274 
       
   275     // Scan complete clip for seek array
       
   276     ResetSeekArray();
       
   277     User::LeaveIfError( iFile.Size( iSeekArrayPoint ) );
       
   278     iThisGroup = iFirstSeekAddr;
       
   279     iGroupTime = 0;
       
   280     iNextGroupPoint = 0;
       
   281     iGroupsTotalCount = 0;
       
   282     
       
   283     // Start reading
       
   284     if ( iObs )
       
   285         {
       
   286         // Asyncronous
       
   287         ReadNextGroupHeaderFromFileL();
       
   288         }
       
   289     else
       
   290         {
       
   291         // Syncronous
       
   292         TInt err( KErrNone );
       
   293         do
       
   294             {
       
   295             ReadNextGroupHeaderFromFileL();
       
   296             TRAP( err, RunL() );
       
   297             }
       
   298             while ( !err && iThisGroup < iSeekArrayPoint );
       
   299 
       
   300         // All done, finalize the clip
       
   301         AddSpecialPacketL( MRtpFileWriteObserver::ERtpClipPause );
       
   302         FinalizeSeekArrayL( KErrNone );
       
   303         }
       
   304     }
       
   305     
       
   306 // -----------------------------------------------------------------------------
       
   307 // CRtpClipRepairer::ReadNextGroupHeaderFromFileL
       
   308 // Reads RTP payload from a file.
       
   309 // Payload is allways after data header, so read position set is not needed.
       
   310 // -----------------------------------------------------------------------------
       
   311 //
       
   312 void CRtpClipRepairer::ReadNextGroupHeaderFromFileL()
       
   313     {
       
   314     iLastSeekAddr = iThisGroup;
       
   315 
       
   316     iFileData = HBufC8::NewL( KGroupHeaderBytes );
       
   317     iDataPtr.Set( iFileData->Des() );
       
   318     if ( iObs )
       
   319         {
       
   320         iFile.Read( iThisGroup, iDataPtr, KGroupHeaderBytes, iStatus );
       
   321         SetActive();
       
   322         }
       
   323     else
       
   324         {
       
   325         iStatus = iFile.Read( iThisGroup, iDataPtr, KGroupHeaderBytes );
       
   326         }
       
   327     }
       
   328     
       
   329 // -----------------------------------------------------------------------------
       
   330 // CRtpClipRepairer::GroupToSeekArrayL
       
   331 // Appends next rec group's group time to seek array if interval time exeeded.
       
   332 // -----------------------------------------------------------------------------
       
   333 //
       
   334 void CRtpClipRepairer::GroupToSeekArrayL()
       
   335     {
       
   336     iGroupsTotalCount++;
       
   337     const TInt delta( iGroupTime - iLastGroupTime );
       
   338     if ( delta > KSeekArrayInterval )
       
   339         {
       
   340         LOG2( "CRtpClipRepairer::GroupToSeekArrayL(), iGroupsTotalCount: %d, iThisGroup: %d", 
       
   341                                                       iGroupsTotalCount, iThisGroup );
       
   342         iLastGroupTime = iGroupTime;
       
   343         AppendSeekArrayL( iGroupTime, iThisGroup );
       
   344         }
       
   345     
       
   346     iPrevGroupPoint = iThisGroup;
       
   347     iThisGroup = iNextGroupPoint;
       
   348     }
       
   349     
       
   350 // -----------------------------------------------------------------------------
       
   351 // CRtpClipRepairer::AddSpecialPacketL
       
   352 // Adds special packet to a new group to the end of clip.
       
   353 // -----------------------------------------------------------------------------
       
   354 //
       
   355 void CRtpClipRepairer::AddSpecialPacketL( 
       
   356     const MRtpFileWriteObserver::TRtpType aType )
       
   357     {
       
   358     LOG1( "CRtpClipRepairer::AddSpecialPacketL(), aType: %d", aType );
       
   359     
       
   360     // Read last group header
       
   361     iThisGroup = iPrevGroupPoint;
       
   362     ReadGroupHeaderL();
       
   363 
       
   364     // End packet
       
   365     HBufC8* bytes = CRtpUtil::SpecialPacketL( aType );
       
   366     CleanupStack::PushL( bytes );
       
   367     iFile.Write( iThisGroup + iGroupTotalLen, bytes->Des(), KSpecialPacketLength );
       
   368     CleanupStack::PopAndDestroy( bytes );
       
   369 
       
   370     // Update group total size (GTS)
       
   371     iGroupTotalLen+= KSpecialPacketLength;
       
   372     bytes = CRtpUtil::MakeBytesLC( iGroupTotalLen );
       
   373     iFile.Write( iThisGroup, bytes->Des(), KIntegerBytes );
       
   374     CleanupStack::PopAndDestroy( bytes );
       
   375     
       
   376     // Update next group point (NGP)
       
   377     iNextGroupPoint+= KSpecialPacketLength;
       
   378     bytes = CRtpUtil::MakeBytesLC( iNextGroupPoint );
       
   379     iFile.Write( iThisGroup + KIntegerBytes, bytes->Des(), KIntegerBytes );
       
   380     CleanupStack::PopAndDestroy( bytes );
       
   381     
       
   382     // Read packets total count (PTC)
       
   383     bytes = HBufC8::NewLC( KPacketsCountBytes );
       
   384     TPtr8 ptr( bytes->Des() );
       
   385     iFile.Read( iThisGroup + KGroupHeaderBytes, ptr, KPacketsCountBytes );
       
   386     const TInt packetsCount( CRtpUtil::GetValueL( ptr ) );
       
   387     User::LeaveIfError( packetsCount );
       
   388     CleanupStack::PopAndDestroy( bytes );
       
   389     
       
   390     // Increment packets tolal count (PTC) by one
       
   391     bytes = CRtpUtil::MakeBytesLC( packetsCount + 1 );
       
   392     iFile.Write( iThisGroup + KGroupHeaderBytes, bytes->Des(), KPacketsCountBytes );
       
   393     CleanupStack::PopAndDestroy( bytes );
       
   394     }
       
   395     
       
   396 // -----------------------------------------------------------------------------
       
   397 // CRtpClipRepairer::FinalizeSeekArrayL
       
   398 // Appends next rec group to seek array.
       
   399 // -----------------------------------------------------------------------------
       
   400 //
       
   401 void CRtpClipRepairer::FinalizeSeekArrayL( const TInt aError )
       
   402     {
       
   403     LOG1( "CRtpClipRepairer::FinalizeSeekArrayL(), aError: %d", aError );
       
   404 
       
   405     // New seek array point
       
   406     iMetaHeader->WriteSeekArrayPointL( iNextGroupPoint );
       
   407 
       
   408     // Update duration
       
   409     iMetaHeader->WriteDurationL( iGroupTime );
       
   410 
       
   411     // Last group time
       
   412     CRtpMetaHeader::SAttributes att;
       
   413     iMetaHeader->ReadAttributesL( att );
       
   414     att.iFailed = !( !aError );
       
   415     att.iOngoing = EFalse;
       
   416     att.iPlayCount = 0;
       
   417     att.iPlaySpot = KErrNone;
       
   418     iMetaHeader->WriteAttributesL( att );
       
   419     
       
   420     // Finalise
       
   421     SaveSeekArrayL();
       
   422     WriteSeekHeaderL();
       
   423     iFile.Flush();
       
   424 
       
   425     // Set start time to file date
       
   426     TTime startTime( 0 );
       
   427     iMetaHeader->ReadStartTimeL( startTime );
       
   428     iFile.SetModified( startTime );
       
   429     iFile.Close();
       
   430     }
       
   431     
       
   432 // End of File