dvrengine/CommonRecordingEngine/DvrRtpClipHandler/src/CRtpClipHandler.cpp
branchRCL_3
changeset 47 826cea16efd9
parent 45 798ee5f1972c
child 48 13a33d82ad98
equal deleted inserted replaced
45:798ee5f1972c 47:826cea16efd9
     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 RTP clip handler class.*
       
    15 */
       
    16 
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <ipvideo/CRtpClipHandler.h>
       
    22 #include "CRtpToFile.h"
       
    23 #include "CRtpFromFile.h"
       
    24 #include "videoserviceutilsLogger.h"
       
    25 
       
    26 // CONSTANTS
       
    27 const TInt KDvrMaxTimeshiftDelta( 1 * 60 * 60 ); //  1 hour
       
    28 
       
    29 // ============================ MEMBER FUNCTIONS ===============================
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // CRtpClipHandler::NewL
       
    33 // Two-phased constructor.
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 EXPORT_C CRtpClipHandler* CRtpClipHandler::NewL()
       
    37     {
       
    38     CRtpClipHandler* self = new( ELeave ) CRtpClipHandler();
       
    39     CleanupStack::PushL( self );
       
    40     self->ConstructL();
       
    41     CleanupStack::Pop( self );
       
    42     return self;
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CRtpClipHandler::CRtpClipHandler
       
    47 // C++ default constructor can NOT contain any code, that might leave.
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 CRtpClipHandler::CRtpClipHandler()
       
    51   : iClipVersion( 0 ),
       
    52     iSaveNameIndex( KErrNotFound ),
       
    53     iReadNameIndex( KErrNotFound ),
       
    54     iTsPauseState( EFalse )
       
    55     {
       
    56     // None
       
    57     }
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CRtpClipHandler::ConstructL
       
    61 // Symbian 2nd phase constructor can leave.
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 void CRtpClipHandler::ConstructL()
       
    65     {
       
    66     LOG( "CRtpClipHandler::ConstructL()" );
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CRtpClipHandler::~CRtpClipHandler
       
    71 // Destructor.
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 EXPORT_C CRtpClipHandler::~CRtpClipHandler()
       
    75     {
       
    76     LOG( "CRtpClipHandler::~CRtpClipHandler() in" );
       
    77     
       
    78     StopRecording( KErrCancel );
       
    79     StopPlayBack( KErrCancel, 0 );
       
    80 
       
    81     LOG( "CRtpClipHandler::~CRtpClipHandler() out" );
       
    82     }
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // CRtpClipHandler::RegisterWriteObserver
       
    86 //
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 EXPORT_C void CRtpClipHandler::RegisterWriteObserver(
       
    90     MRtpFileWriteObserver* aObs )
       
    91     {
       
    92     LOG1( "CRtpClipHandler::RegisterWriteObserver(), aObs: %d", aObs );
       
    93     iWriteObs = aObs;
       
    94     }
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CRtpClipHandler::RegisterReadObserver
       
    98 //
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 EXPORT_C void CRtpClipHandler::RegisterReadObserver( MRtpFileReadObserver* aObs )
       
   102     {
       
   103     LOG1( "CRtpClipHandler::RegisterReadObserver(), aObs: %d", aObs );
       
   104 
       
   105     iReadObs = aObs;
       
   106     }
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CRtpClipHandler::StartRecordingL
       
   110 //
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 EXPORT_C void CRtpClipHandler::StartRecordingL(
       
   114     const MRtpFileWriteObserver::SRtpRecParams& aRecParams,
       
   115     const MRtpFileWriteObserver::TRtpSaveAction& aAction )
       
   116     {
       
   117     LOG1( "CRtpClipHandler::StartRecordingL() in, aAction: %d", aAction );
       
   118     
       
   119     // Stop possible existing recording
       
   120     StopRecording( KErrNone );
       
   121     iSaveNameIndex = 0;
       
   122     
       
   123     // If InitRtpSaveL leaves, iRtpSave is not NULL, but is in undefined state.
       
   124     // This causes problems - may crash when StopRecording is called.
       
   125     // Better to trap and cleanup here.
       
   126     TRAPD( err,
       
   127          // Init recording ( iRtpSave deleted in StopRecording() )
       
   128          iRtpSave = CRtpToFile::NewL( *this, *iWriteObs );
       
   129          iRtpSave->InitRtpSaveL( aRecParams, aAction );
       
   130          );
       
   131     if ( err )
       
   132         {
       
   133         delete iRtpSave; iRtpSave = NULL;
       
   134         User::Leave( err );
       
   135         }
       
   136     
       
   137     LOG( "CRtpClipHandler::StartRecordingL() out" );
       
   138     }
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // CRtpClipHandler::SaveNextGroupL
       
   142 //
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 EXPORT_C void CRtpClipHandler::SaveNextGroupL(
       
   146     TPtr8& aGroup,
       
   147     TUint aGroupLength,
       
   148     const MRtpFileWriteObserver::TRtpSaveAction& aAction )
       
   149     {
       
   150     User::LeaveIfNull( iWriteObs );
       
   151     User::LeaveIfNull( iRtpSave );
       
   152     
       
   153     // Passes save action to ring buffer
       
   154     const TUint seekPoint( iRtpSave->SaveNextGroupL( 
       
   155                            aGroup, aGroupLength, aAction ) ); 
       
   156     
       
   157     // aGroupLength is set to non zero if time shift ongoing?
       
   158     if ( aGroupLength > 0 )
       
   159         {
       
   160         CRtpFileBase::STimeShiftSeek shiftSeek;
       
   161         shiftSeek.iGroupTime = aGroupLength;
       
   162         shiftSeek.iSeekpoint = seekPoint;
       
   163         shiftSeek.iNameIndex = iSaveNameIndex;
       
   164         iShiftSeek.Append( shiftSeek );
       
   165         
       
   166         // Time shift max length
       
   167         if ( ( ( iShiftSeek.Count() - 1 ) *  KNormalRecGroupLength ) > 
       
   168                                              KDvrMaxTimeshiftDelta )
       
   169             {
       
   170             LOG( "CRtpClipHandler::SaveNextGroupL(), Timeshift max time !" );
       
   171             }
       
   172         }
       
   173     }
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CRtpClipHandler::WritingActive
       
   177 //
       
   178 // -----------------------------------------------------------------------------
       
   179 //
       
   180 EXPORT_C TBool CRtpClipHandler::WritingActive( void ) const
       
   181     {
       
   182     if ( iRtpSave )
       
   183         {
       
   184         return iRtpSave->IsActive();
       
   185         }
       
   186     
       
   187     return EFalse;
       
   188     }
       
   189     
       
   190 // -----------------------------------------------------------------------------
       
   191 // CRtpClipHandler::GetCurrentLength
       
   192 //
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 EXPORT_C TUint CRtpClipHandler::GetCurrentLength( void ) const
       
   196     {
       
   197     TUint length( 0 );
       
   198     if ( iRtpRead )
       
   199         {
       
   200         length = iRtpRead->Duration();
       
   201         }
       
   202     else
       
   203         {
       
   204         if ( iRtpSave )
       
   205             {
       
   206             const TInt count( iShiftSeek.Count() );
       
   207             if ( count > 0 )
       
   208                 {
       
   209                 // Time shift
       
   210                 for ( TInt i( 0 ); i < count; i++ )
       
   211                     {
       
   212                     length += iShiftSeek[i].iGroupTime;
       
   213                     }
       
   214                 }
       
   215             else
       
   216                 {
       
   217                 // Rec ongoing
       
   218                 length = iRtpSave->GetCurrentLength();
       
   219                 }
       
   220             }
       
   221         }
       
   222 
       
   223     return length;
       
   224     }
       
   225 
       
   226 // -----------------------------------------------------------------------------
       
   227 // CRtpClipHandler::TimeShiftPauseL
       
   228 //
       
   229 // -----------------------------------------------------------------------------
       
   230 //
       
   231 EXPORT_C void CRtpClipHandler::TimeShiftPauseL()
       
   232     {
       
   233     LOG2( "CRtpClipHandler::TimeShiftPauseL(), iRtpSave: %d, iTsPauseState: %d", 
       
   234                                                iRtpSave, iTsPauseState ); 
       
   235     User::LeaveIfNull( iRtpSave );
       
   236     if ( !iTsPauseState )
       
   237         {
       
   238         // Use clip as a ring buffer
       
   239         iRtpSave->ActivateGroupsReuseL();
       
   240         iTsPauseState = ETrue;
       
   241         }
       
   242     else
       
   243         {
       
   244         // Switch to next file
       
   245         iSaveNameIndex++;
       
   246         MRtpFileWriteObserver::SRtpRecParams recParams;
       
   247         DefaultRecParams( recParams, iSaveNameIndex );
       
   248         iRtpSave->SwapClipL( recParams );
       
   249         iTsPauseState = EFalse;
       
   250         }
       
   251     }
       
   252     
       
   253 // -----------------------------------------------------------------------------
       
   254 // CRtpClipHandler::DefaultRecParams
       
   255 //
       
   256 // -----------------------------------------------------------------------------
       
   257 //
       
   258 EXPORT_C void CRtpClipHandler::DefaultRecParams(
       
   259     MRtpFileWriteObserver::SRtpRecParams& aParams,
       
   260     const TInt aIndex )
       
   261     {
       
   262     aParams.iClipPath = KDvrTimeShiftFile;
       
   263     aParams.iClipPath.AppendNum( aIndex );
       
   264     aParams.iSdpData.Set( NULL, 0 );
       
   265     aParams.iService.Set( KNullDesC );
       
   266     aParams.iProgram.Set( KNullDesC );
       
   267     aParams.iPostRule = 0;
       
   268     aParams.iParental = 0;
       
   269     aParams.iStartTime = 0;
       
   270     aParams.iEndTime = TInt64( KDvrMaximumTimeShift ) * 1e6;
       
   271     }
       
   272     
       
   273 // -----------------------------------------------------------------------------
       
   274 // CRtpClipHandler::UpdateRecordEndTime
       
   275 //
       
   276 // -----------------------------------------------------------------------------
       
   277 //
       
   278 EXPORT_C void CRtpClipHandler::UpdateRecordEndTime( const TTime& aEndTime ) 
       
   279     {
       
   280     if ( iRtpSave )
       
   281         {
       
   282         iRtpSave->UpdateRecordEndTime( aEndTime );
       
   283         }
       
   284     }
       
   285 
       
   286 // -----------------------------------------------------------------------------
       
   287 // CRtpClipHandler::ResumeRecordingL
       
   288 //
       
   289 // -----------------------------------------------------------------------------
       
   290 //
       
   291 EXPORT_C void CRtpClipHandler::ResumeRecordingL( void )
       
   292     {
       
   293     LOG1( "CRtpClipHandler::ResumeRecordingL(), iRtpSave: %d", iRtpSave ); 
       
   294     
       
   295     if ( iRtpSave )
       
   296         {
       
   297         iRtpSave->UpdatePreviousTimeL();
       
   298         }
       
   299     }
       
   300     
       
   301 // -----------------------------------------------------------------------------
       
   302 // CRtpClipHandler::StopRecording
       
   303 //
       
   304 // -----------------------------------------------------------------------------
       
   305 //
       
   306 EXPORT_C void CRtpClipHandler::StopRecording( const TInt aError )
       
   307     {
       
   308     LOG2( "CRtpClipHandler::StopRecording(), aError: %d, iRtpSave: %d",
       
   309                                              aError, iRtpSave );
       
   310     
       
   311     if ( iRtpSave )
       
   312         {
       
   313         // Update clip end point if watching sametime
       
   314         if ( iRtpRead )
       
   315             {
       
   316             iRtpRead->UpdateLastSeekAddr();
       
   317             }
       
   318         
       
   319         // Stop recording
       
   320         iRtpSave->StopRtpSave( aError );
       
   321 
       
   322         // Delete possible time shift files
       
   323         if ( !iRtpRead )
       
   324             {
       
   325             iRtpSave->DeleteTimeShiftFiles( iShiftSeek );
       
   326             }
       
   327         }
       
   328     
       
   329     delete iRtpSave; iRtpSave = NULL;
       
   330     }
       
   331 
       
   332 // -----------------------------------------------------------------------------
       
   333 // CRtpClipHandler::StartPlayBackL
       
   334 //
       
   335 // -----------------------------------------------------------------------------
       
   336 //
       
   337 EXPORT_C void CRtpClipHandler::StartPlayBackL(
       
   338     const SCRRtpPlayParams& aParams,
       
   339     const TBool aTimeShift )
       
   340     {
       
   341     LOG1( "CRtpClipHandler::StartPlayBackL(), path: %S", &aParams.iFileName );
       
   342     LOG1( "CRtpClipHandler::StartPlayBackL(), aTimeShift: %d", aTimeShift );
       
   343 
       
   344     // Stop possible existing
       
   345     StopPlayBack( KErrNone, 0 );
       
   346     
       
   347     // Time shift mode?
       
   348     if ( aTimeShift )
       
   349         {
       
   350         // Open file for time shift mode
       
   351         User::LeaveIfNull( iRtpSave );
       
   352         iRtpRead = CRtpFromFile::NewL( *iReadObs, iRtpSave );
       
   353         iReadNameIndex = ( iShiftSeek.Count() )? iShiftSeek[0].iNameIndex: 0;
       
   354         TPath clipPath( KDvrTimeShiftFile );
       
   355         clipPath.AppendNum( iReadNameIndex );
       
   356         iRtpRead->InitRtpReadL( clipPath, iClipVersion, aTimeShift );
       
   357         }
       
   358     else
       
   359         {
       
   360         // Open file, during recording?
       
   361         if ( iRtpSave && iRtpSave->ClipPath() )
       
   362             {
       
   363             if ( !aParams.iFileName.Compare( iRtpSave->ClipPath()->Des() ) )
       
   364                 {
       
   365                 iRtpRead = CRtpFromFile::NewL( *iReadObs, iRtpSave );
       
   366                 }
       
   367             }
       
   368         
       
   369         // Open file, normal playback?
       
   370         if ( !iRtpRead )
       
   371             {
       
   372             iRtpRead = CRtpFromFile::NewL( *iReadObs, NULL );
       
   373             }
       
   374         
       
   375         iRtpRead->InitRtpReadL( aParams.iFileName, iClipVersion, EFalse );
       
   376         }
       
   377     }
       
   378 
       
   379 // -----------------------------------------------------------------------------
       
   380 // CRtpClipHandler::StartPlayBackL
       
   381 //
       
   382 // -----------------------------------------------------------------------------
       
   383 //
       
   384 EXPORT_C void CRtpClipHandler::StartPlayBackL( const RFile& aFileHandle )
       
   385     {
       
   386     LOG( "CRtpClipHandler::StartPlayBackL(), with handle" );
       
   387 
       
   388     // Stop possible existing
       
   389     StopPlayBack( KErrNone, 0 );
       
   390     
       
   391     // Open file, during recording?
       
   392     if ( iRtpSave && iRtpSave->ClipPath() )
       
   393         {
       
   394         TPath name( KNullDesC );
       
   395         aFileHandle.FullName( name );
       
   396         if ( !name.Compare( iRtpSave->ClipPath()->Des() ) )
       
   397             {
       
   398             iRtpRead = CRtpFromFile::NewL( *iReadObs, iRtpSave );
       
   399             }
       
   400         }
       
   401     
       
   402     // Open file, normal playback?
       
   403     if ( !iRtpRead )
       
   404         {
       
   405         iRtpRead = CRtpFromFile::NewL( *iReadObs, NULL );
       
   406         }
       
   407     
       
   408     iRtpRead->InitRtpReadL( aFileHandle, iClipVersion );
       
   409     }
       
   410 
       
   411 // -----------------------------------------------------------------------------
       
   412 // CRtpClipHandler::GetClipSdpL
       
   413 //
       
   414 // -----------------------------------------------------------------------------
       
   415 //
       
   416 EXPORT_C HBufC8* CRtpClipHandler::GetClipSdpL( void ) const
       
   417     {
       
   418     HBufC8* sdp = NULL;
       
   419     if ( iRtpRead )
       
   420         {
       
   421         sdp = iRtpRead->GetClipSdpL();
       
   422         }
       
   423     
       
   424     return sdp;
       
   425     }
       
   426     
       
   427 // -----------------------------------------------------------------------------
       
   428 // CRtpClipHandler::NextClipGroupL
       
   429 //
       
   430 // -----------------------------------------------------------------------------
       
   431 //
       
   432 EXPORT_C void CRtpClipHandler::NextClipGroupL( void ) 
       
   433     {
       
   434     User::LeaveIfNull( iRtpRead );
       
   435     if ( !iRtpRead->IsTimeShift() )
       
   436         {
       
   437         iRtpRead->ReadNextGroupL( KErrNotFound );
       
   438         }
       
   439     else 
       
   440         {
       
   441         // Handle time shift
       
   442         TInt err( KErrNotFound );
       
   443         if ( iShiftSeek.Count() )
       
   444             {
       
   445             // Need swap file?
       
   446             SwapClipIfNeededL();
       
   447 
       
   448             // Read next group
       
   449             TRAP( err, iRtpRead->ReadNextGroupL( iShiftSeek[0].iSeekpoint ) );
       
   450             if ( err != KErrInUse  )
       
   451                 {
       
   452                 iShiftSeek.Remove( 0 );
       
   453                 }
       
   454             }
       
   455         
       
   456         // End time shift?
       
   457         if ( err == KErrEof )
       
   458             {
       
   459             LOG( "CRtpClipHandler::NextClipGroupL(), Time shift play reached live !" );
       
   460             User::LeaveIfNull( iReadObs );
       
   461             iReadObs->ReadStatus( MRtpFileReadObserver::ERtpTimeShifTEnd );
       
   462             }
       
   463         }
       
   464     }
       
   465 
       
   466 // -----------------------------------------------------------------------------
       
   467 // CRtpClipHandler::SetSeekPointL
       
   468 // -----------------------------------------------------------------------------
       
   469 //
       
   470 EXPORT_C void CRtpClipHandler::SetSeekPointL( const TUint aTime ) 
       
   471     {
       
   472     LOG1( "CRtpClipHandler::SetSeekPointL(), aTime: %u", aTime );
       
   473 
       
   474     User::LeaveIfNull( iRtpRead );
       
   475     if ( !iShiftSeek.Count() )
       
   476         {
       
   477         iRtpRead->SetSeekPointL( aTime );
       
   478         }
       
   479     else
       
   480         {
       
   481         const TInt count( iShiftSeek.Count() );
       
   482         if ( count > 0 )
       
   483             {
       
   484             TUint total( 0 );
       
   485             for ( TInt index( 0 ); index < count && aTime > total; index++ )
       
   486                 {
       
   487                 total += iShiftSeek[0].iGroupTime;
       
   488                 SwapClipIfNeededL();
       
   489                 iShiftSeek.Remove( 0 );
       
   490                 }
       
   491             }
       
   492         }
       
   493     }
       
   494 
       
   495 // -----------------------------------------------------------------------------
       
   496 // CRtpClipHandler::StopPlayBack
       
   497 // -----------------------------------------------------------------------------
       
   498 //
       
   499 EXPORT_C void CRtpClipHandler::StopPlayBack(
       
   500     const TInt aError,
       
   501     const TUint aPlayerBuf )
       
   502     {
       
   503     LOG2( "CRtpClipHandler::StopPlayBack(), aError: %d, iRtpRead: %d",
       
   504                                             aError, iRtpRead );
       
   505     if ( iRtpRead )
       
   506         {
       
   507         iRtpRead->StopRtpRead( aError, aPlayerBuf );
       
   508 
       
   509         // Delete possible time shift files
       
   510         if ( !iRtpSave )
       
   511             {
       
   512             iRtpRead->DeleteTimeShiftFiles( iShiftSeek );
       
   513             }
       
   514         }
       
   515 
       
   516     delete iRtpRead; iRtpRead = NULL;
       
   517     }
       
   518 
       
   519 // -----------------------------------------------------------------------------
       
   520 // CRtpClipHandler::RtpGroupSaved
       
   521 // Indicates that RPT packet save is ready.
       
   522 // -----------------------------------------------------------------------------
       
   523 //
       
   524 void CRtpClipHandler::RtpGroupSaved( const TInt aAction ) 
       
   525     {
       
   526     // Inform file reader that new group saved
       
   527     if ( iRtpRead && aAction >= KErrNone )
       
   528         {
       
   529         iRtpRead->ReadSkippedGroup();
       
   530         }
       
   531 
       
   532     // Group saved
       
   533     if ( iWriteObs )
       
   534         {
       
   535         if ( aAction != MRtpFileWriteObserver::ESaveEnd )
       
   536             {
       
   537             // Ready for next group
       
   538             iWriteObs->GroupSaved();
       
   539             }
       
   540         else
       
   541             {
       
   542             // Recording full time
       
   543             iWriteObs->WriteStatus( KErrNone );
       
   544             }
       
   545         }
       
   546     }
       
   547     
       
   548 // -----------------------------------------------------------------------------
       
   549 // CRtpClipHandler::CurrentFileReadPoint
       
   550 // Getter for file reader current point.
       
   551 // -----------------------------------------------------------------------------
       
   552 //
       
   553 TInt CRtpClipHandler::CurrentFileReadPoint( const TInt aIndex )
       
   554     {
       
   555     if ( iShiftSeek.Count() > aIndex )
       
   556         {
       
   557         if ( iShiftSeek[aIndex].iNameIndex == iSaveNameIndex )
       
   558             {
       
   559             return iShiftSeek[aIndex].iSeekpoint;
       
   560             }
       
   561         
       
   562         return KErrNone;
       
   563         }
       
   564 
       
   565     return KErrNotFound;
       
   566     }
       
   567     
       
   568 // -----------------------------------------------------------------------------
       
   569 // CRtpClipHandler::SwapClipIfNeededL
       
   570 // Swap to next available clip in time shift array if needed.
       
   571 // -----------------------------------------------------------------------------
       
   572 //
       
   573 void CRtpClipHandler::SwapClipIfNeededL( void )
       
   574     {
       
   575     if ( iShiftSeek[0].iNameIndex != iReadNameIndex )
       
   576         {
       
   577         iReadNameIndex = iShiftSeek[0].iNameIndex;
       
   578         TPath clipPath( KDvrTimeShiftFile );
       
   579         clipPath.AppendNum( iReadNameIndex );
       
   580         iRtpRead->SwapClipL( clipPath );
       
   581         }
       
   582     }
       
   583     
       
   584 //  End of File