multimediacommscontroller/mmccdtmfpayloadformat/src/dtmfpayloadformatread.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2006-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 "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:    Contains functionality for DTMF and line event 
       
    15 *                decoding and playing.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDES
       
    23 #include "dtmfpayloadformatread.h"
       
    24 #include "dtmfpayloaddecoder.h"
       
    25 #include "dtmfeventpayloadinfo.h"
       
    26 #include "dtmftonepayloadinfo.h"
       
    27 #include "mccuids.hrh"
       
    28 
       
    29 // ======== MEMBER FUNCTIONS ========
       
    30 
       
    31 
       
    32 // ---------------------------------------------------------------------------
       
    33 // CDTMFPayloadFormatRead::CDTMFPayloadFormatRead
       
    34 // C++ default constructor can NOT contain any code, that
       
    35 // might leave.
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 CDTMFPayloadFormatRead::CDTMFPayloadFormatRead()
       
    39     {
       
    40     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::CDTMFPayloadFormatRead") );
       
    41     }
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // CDTMFPayloadFormatRead::ConstructL
       
    45 // Symbian 2nd phase constructor can leave.
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 void CDTMFPayloadFormatRead::ConstructL( MDataSource* aSource )
       
    49     {
       
    50     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::ConstructL") );
       
    51     
       
    52     iClip = aSource;
       
    53     iBufferToReadExists = EFalse;
       
    54 
       
    55     // Initialize decoding state machine
       
    56     iStateMachine = CFormatDecodeStateMachine::NewL( this );
       
    57     iStateMachine->ChangeState( EDecodeIdle );
       
    58 
       
    59     iPayloadDecoder = CDTMFPayloadDecoder::NewL();
       
    60 
       
    61     iTonePlayer = CMdaAudioToneUtility::NewL( *this );
       
    62     TInt maxVol = iTonePlayer->MaxVolume();
       
    63     // Use intermediate volume value
       
    64     iTonePlayer->SetVolume( ( maxVol / 2 ) );
       
    65     }
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // CDTMFPayloadFormatRead::NewL
       
    69 // Two-phased constructor.
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 CDTMFPayloadFormatRead* CDTMFPayloadFormatRead::NewL( MDataSource* aSource )
       
    73     {
       
    74     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::NewL") );
       
    75     
       
    76     __ASSERT_ALWAYS( aSource, User::Leave( KErrArgument ) );
       
    77 
       
    78     CDTMFPayloadFormatRead* self = new( ELeave ) CDTMFPayloadFormatRead;
       
    79 
       
    80     CleanupStack::PushL( self );
       
    81     self->ConstructL( aSource );
       
    82     CleanupStack::Pop( self );
       
    83 
       
    84     return self;
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // CDTMFPayloadFormatRead::~CDTMFPayloadFormatRead
       
    89 // Destructor.
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 CDTMFPayloadFormatRead::~CDTMFPayloadFormatRead()
       
    93     {
       
    94     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::~CDTMFPayloadFormatRead") );
       
    95 
       
    96     if ( iSourceBufOwnership )
       
    97         {
       
    98         delete iSourceBuffer;
       
    99         }
       
   100     
       
   101     iSourceBuffer = NULL;
       
   102         
       
   103     delete iPayloadDecoder;
       
   104 
       
   105     if ( iStateMachine )
       
   106         {
       
   107         iStateMachine->Cancel();
       
   108         delete iStateMachine;
       
   109         }
       
   110 
       
   111     delete iTonePlayer;
       
   112 
       
   113     // for PC_LINT #1740
       
   114     iClip = NULL;
       
   115 
       
   116     iEventPayloadInfo.Close();
       
   117     iTonePayloadInfo.Close();
       
   118     }
       
   119 
       
   120 // ---------------------------------------------------------------------------
       
   121 // CDTMFPayloadFormatRead::Streams
       
   122 // Not applicable for DTMF payload formatter. Pure virtual method
       
   123 // implementation needed anyway.
       
   124 // ---------------------------------------------------------------------------
       
   125 //
       
   126 TUint CDTMFPayloadFormatRead::Streams( TUid aMediaType ) const
       
   127     {
       
   128     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::Streams") );
       
   129 
       
   130     if ( KUidMediaTypeAudio == aMediaType )
       
   131         {
       
   132         return 1;
       
   133         }
       
   134     else
       
   135         {
       
   136         return 0;
       
   137         }
       
   138     }
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // CDTMFPayloadFormatRead::FrameTimeInterval
       
   142 // Not applicable for DTMF payload formatter. Pure virtual method
       
   143 // implementation needed anyway.
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 TTimeIntervalMicroSeconds CDTMFPayloadFormatRead::FrameTimeInterval( 
       
   147     TMediaId /*aMediaType*/ ) const
       
   148     {
       
   149     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::FrameTimeInterval") );
       
   150 
       
   151     return TTimeIntervalMicroSeconds( TInt64( 0 ) );
       
   152     }
       
   153 
       
   154 // ---------------------------------------------------------------------------
       
   155 // CDTMFPayloadFormatRead::Duration
       
   156 // Not applicable for DTMF payload formatter. Pure virtual method
       
   157 // implementation needed anyway.
       
   158 // ---------------------------------------------------------------------------
       
   159 //
       
   160 TTimeIntervalMicroSeconds CDTMFPayloadFormatRead::Duration( 
       
   161     TMediaId /*aMediaType*/ ) const
       
   162     {
       
   163     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::Duration") );
       
   164     
       
   165     return TTimeIntervalMicroSeconds( TInt64( 0 ) );
       
   166     }
       
   167 
       
   168 // ---------------------------------------------------------------------------
       
   169 // CDTMFPayloadFormatRead::CanCreateSourceBuffer
       
   170 // SourceBuffer creation is not supported, because there is no need to
       
   171 // exchange DTMF data with datapath. DTMF payload formatter handles DTMF
       
   172 // playing independently through tone player.
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 TBool CDTMFPayloadFormatRead::CanCreateSourceBuffer()
       
   176     {
       
   177     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::CanCreateSourceBuffer") );
       
   178     
       
   179     return EFalse;
       
   180     }
       
   181         
       
   182 // ---------------------------------------------------------------------------
       
   183 // CDTMFPayloadFormatRead::CreateSourceBufferL
       
   184 // Should not be used.
       
   185 // ---------------------------------------------------------------------------
       
   186 //
       
   187 CMMFBuffer* CDTMFPayloadFormatRead::CreateSourceBufferL( TMediaId /*aMediaId*/,
       
   188     TBool& /*aReference*/ )
       
   189     {
       
   190     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::CreateSourceBufferL") );
       
   191     
       
   192     User::Leave( KErrNotSupported );
       
   193     return NULL;
       
   194     }
       
   195 
       
   196 // ---------------------------------------------------------------------------
       
   197 // CDTMFPayloadFormatRead::SourceDataTypeCode
       
   198 // Returns source data type code.
       
   199 // ---------------------------------------------------------------------------
       
   200 //
       
   201 TFourCC CDTMFPayloadFormatRead::SourceDataTypeCode( TMediaId aMediaId )
       
   202     {
       
   203     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::SourceDataTypeCode() ") );
       
   204     
       
   205     if ( KUidMediaTypeAudio == aMediaId.iMediaType )
       
   206         {
       
   207         return TFourCC( KMccFourCCIdDTMF );
       
   208         }
       
   209     else
       
   210         {
       
   211          // Defaults to 'NULL' fourCC
       
   212         return TFourCC();
       
   213         }
       
   214     }
       
   215 
       
   216 // ---------------------------------------------------------------------------
       
   217 // CDTMFPayloadFormatRead::SinkDataTypeCode
       
   218 // Returns the current datatype FourCC code.
       
   219 // ---------------------------------------------------------------------------
       
   220 //
       
   221 TFourCC CDTMFPayloadFormatRead::SinkDataTypeCode( TMediaId aMediaId )
       
   222     {
       
   223     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::SinkDataTypeCode") );
       
   224     
       
   225     if ( KUidMediaTypeAudio == aMediaId.iMediaType )
       
   226         {
       
   227         return TFourCC( KMccFourCCIdDTMF );
       
   228         }
       
   229     else
       
   230         {
       
   231          // Defaults to 'NULL' fourCC
       
   232         return TFourCC();
       
   233         }
       
   234     }
       
   235 
       
   236 // ---------------------------------------------------------------------------
       
   237 // CDTMFPayloadFormatRead::SetPayloadFormat
       
   238 // Sets payload format used in DTMF decoding.
       
   239 // ---------------------------------------------------------------------------
       
   240 //
       
   241 TInt CDTMFPayloadFormatRead::SetPayloadFormat( 
       
   242     TDTMFPayloadFormat aPayloadFormat )
       
   243     {
       
   244     DP_DTMF_READ2( _L("CDTMFPayloadFormatWrite::SetPayloadFormat - Format: %d"),
       
   245         aPayloadFormat );
       
   246 
       
   247     TInt err = iPayloadDecoder->SetPayloadFormat( aPayloadFormat );
       
   248     if ( KErrNone == err )
       
   249         {
       
   250         iPayloadFormat = aPayloadFormat;
       
   251         }
       
   252 
       
   253     return err;
       
   254     }
       
   255 
       
   256 // ---------------------------------------------------------------------------
       
   257 // CDTMFPayloadFormatRead::FillBufferL
       
   258 // No need for data exchange with DataPath. DTMF receiving and processing
       
   259 // cycle is managed by SourcePrimeL(), SourcePlayL(), SourcePauseL(),
       
   260 // SourceStopL() methods.
       
   261 // ---------------------------------------------------------------------------
       
   262 //
       
   263 void CDTMFPayloadFormatRead::FillBufferL( CMMFBuffer* /*aBuffer*/, 
       
   264                                           MDataSink* /*aConsumer*/, 
       
   265                                           TMediaId /*aMediaId*/ )
       
   266     {
       
   267     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::FillBufferL") );
       
   268     
       
   269     User::Leave( KErrNotSupported );
       
   270     }
       
   271 
       
   272 // ---------------------------------------------------------------------------
       
   273 // CDTMFPayloadFormatRead::FillSourceBufferL
       
   274 // Called by state machine. Send fill buffer request to the RTP Data Source.
       
   275 // ---------------------------------------------------------------------------
       
   276 //
       
   277 void CDTMFPayloadFormatRead::FillSourceBufferL()
       
   278     {
       
   279     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::FillSourceBufferL") );
       
   280     
       
   281     iClip->FillBufferL( iSourceBuffer, this, KUidMediaTypeAudio ); 
       
   282     }
       
   283     
       
   284 // ---------------------------------------------------------------------------
       
   285 // CDTMFPayloadFormatRead::FillSinkBuffer
       
   286 // Called by state machine. Decode payload to DTMF string and fill
       
   287 // sink buffer.
       
   288 // ---------------------------------------------------------------------------
       
   289 //
       
   290 void CDTMFPayloadFormatRead::FillSinkBufferL()
       
   291     {
       
   292     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::FillSinkBufferL") );
       
   293 
       
   294     if ( iBufferToReadExists )
       
   295         {
       
   296         // Decode payload here and send data for continued handling in 
       
   297         // SendDataToSinkL()
       
   298         switch ( iPayloadFormat )
       
   299             {
       
   300             case EDTMFPayloadFormatEvent:
       
   301             case EDTMFPayloadFormatRedEvents:
       
   302                 iPayloadDecoder
       
   303                     ->DecodeEventPayload( iSourceBuffer, iEventPayloadInfo );
       
   304                 break;
       
   305             case EDTMFPayloadFormatTone:
       
   306                 iPayloadDecoder
       
   307                     ->DecodeTonePayload( iSourceBuffer, iTonePayloadInfo );
       
   308                 break;
       
   309             default:
       
   310                 // Undefined Payload format
       
   311                 DP_DTMF_READ( _L("Unsupported payload format!") );
       
   312                 ASSERT( EFalse );
       
   313                 break;
       
   314             }
       
   315         
       
   316         iStateMachine->ChangeState( EEmptyDataToSink );
       
   317         }
       
   318     else
       
   319         {
       
   320         DP_DTMF_READ( _L("No DTMF packets waiting at Data source.") );
       
   321         
       
   322         FillSourceBufferL();
       
   323         }
       
   324     }
       
   325     
       
   326 // ---------------------------------------------------------------------------
       
   327 // CDTMFPayloadFormatRead::SendDataToSinkL
       
   328 // Called by state machine. Send decoded payload to sound device for playback.
       
   329 // ---------------------------------------------------------------------------
       
   330 //
       
   331 void CDTMFPayloadFormatRead::SendDataToSinkL()
       
   332     {
       
   333     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::SendDataToSinkL") );
       
   334     
       
   335     if ( iBufferToReadExists )
       
   336         {
       
   337         iBufferToReadExists = EFalse;
       
   338         
       
   339         if ( EDTMFPayloadFormatEvent == iPayloadFormat 
       
   340              || EDTMFPayloadFormatRedEvents == iPayloadFormat )
       
   341             {
       
   342             HandleDTMFEventsL();
       
   343             }
       
   344         else if ( EDTMFPayloadFormatTone == iPayloadFormat )
       
   345             {
       
   346             HandleDTMFTones();
       
   347             }
       
   348         else
       
   349             {
       
   350             // undefined payload format
       
   351             DP_DTMF_READ( _L("Unsupported payload format!") );
       
   352             ASSERT( EFalse );
       
   353             }
       
   354         }
       
   355     
       
   356     FillSourceBufferL();
       
   357     }
       
   358 
       
   359 // ---------------------------------------------------------------------------
       
   360 // CDTMFPayloadFormatRead::HandleDTMFEvents
       
   361 // Handle and play DTMF events using tone player.
       
   362 // ---------------------------------------------------------------------------
       
   363 //
       
   364 void CDTMFPayloadFormatRead::HandleDTMFEventsL()
       
   365     {
       
   366     DP_DTMF_READ3( _L("CDTMFPayloadFormatRead::HandleDTMFEventsL, EVENTCOUNT: %d, tick = %u"),
       
   367         iEventPayloadInfo.Count(), User::NTickCount() );
       
   368     
       
   369     const TUint eventCount( iEventPayloadInfo.Count() );
       
   370     
       
   371     if ( 1 == eventCount )
       
   372         {
       
   373         // Stop DTMF playing when end bit is set or new
       
   374         // event has been received
       
   375         if ( iEventPayloadInfo[0].EndBit() )
       
   376             {
       
   377             DP_DTMF_READ( _L("END BIT SET, CANCEL") );
       
   378             iTonePlayer->CancelPlay();
       
   379             }
       
   380         else
       
   381             {
       
   382             TBool prepareToPlay( EFalse );
       
   383 
       
   384             if ( iFirstPacket )
       
   385                 {
       
   386                 DP_DTMF_READ( _L("M BIT SET, CANCEL") );
       
   387                 iFirstPacket = EFalse;
       
   388                 prepareToPlay = ETrue;
       
   389                 }
       
   390             else if ( iCurrentEvent != iEventPayloadInfo[0].Event()
       
   391                 || iTimeStampOfPrevEvent != iEventPayloadInfo[0].TimeStamp() )
       
   392                 {
       
   393                 DP_DTMF_READ( _L("NEW EVENT, CANCEL") );
       
   394                 prepareToPlay = ETrue;
       
   395                 }
       
   396             else
       
   397                 {
       
   398                 // Make PC-LINT happy
       
   399                 }                
       
   400             
       
   401             iTimeStampOfPrevEvent = iEventPayloadInfo[0].TimeStamp();
       
   402 
       
   403             if ( prepareToPlay )
       
   404                 {
       
   405                 DP_DTMF_READ( _L("PREPARING TO PLAY NEW DIGIT") );
       
   406                 iTonePlayer->CancelPlay();
       
   407                 iCurrentEvent = iEventPayloadInfo[0].Event();
       
   408                 
       
   409                 // Play DTMF until we have received final packet
       
   410                 TBuf<1> buf;
       
   411                 buf.Append( iEventPayloadInfo[0].Event() );
       
   412                 iTonePlayer->SetDTMFLengths( KDTMFMaxTonePlaybackTime,
       
   413                                              KDTMFDefToneOffLengthInUs,
       
   414                                              KDTMFDefPauseLengthInUs );
       
   415                 iTonePlayer->PrepareToPlayDTMFString( buf );
       
   416                 }
       
   417             }            
       
   418         }
       
   419     else if ( 1 < eventCount )
       
   420         {
       
   421         // Play out contiguous (mutually exclusive) events
       
   422         TBuf<KMaxDtmfRedCount> buf;
       
   423         for ( TUint i = 0; i < KMaxDtmfRedCount; i++ )
       
   424             {
       
   425             buf.Append( iEventPayloadInfo[i].Event() );
       
   426             }
       
   427         
       
   428         // Playing with default settings
       
   429         iTonePlayer->PrepareToPlayDTMFString( buf );
       
   430         }
       
   431     else
       
   432         {
       
   433         // Some error, no events
       
   434         }
       
   435         
       
   436     // Reset for a new RTP packet and to save memory
       
   437     iEventPayloadInfo.Reset();
       
   438     }
       
   439 
       
   440 // ---------------------------------------------------------------------------
       
   441 // CDTMFPayloadFormatRead::HandleDTMFTones
       
   442 // NOT SUPPORTED. Handle and play DTMF tone payload.
       
   443 // ---------------------------------------------------------------------------
       
   444 //
       
   445 void CDTMFPayloadFormatRead::HandleDTMFTones()
       
   446     {
       
   447     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::HandleDTMFTones") );
       
   448     
       
   449     const TUint toneCount( iTonePayloadInfo.Count() );
       
   450 
       
   451     // Loop is needed for subscriber line event construction, which may have
       
   452     // longer cycles than one tone play out. E.g. "special information tone".
       
   453     // TBD: PlayDualToneL may stop previous tone playback
       
   454     for ( TUint i = 0; i < toneCount; i++ )
       
   455         {
       
   456         iTonePlayer->SetVolume( iTonePayloadInfo[i].Volume() );
       
   457         iTonePlayer->PrepareToPlayDualTone( 
       
   458                                         iTonePayloadInfo[i].LowFrequency(),
       
   459                                         iTonePayloadInfo[i].HighFrequency(),
       
   460                                         iTonePayloadInfo[i].ToneOnPeriod() );
       
   461         }
       
   462     }
       
   463 
       
   464 // ---------------------------------------------------------------------------
       
   465 // CDTMFPayloadFormatRead::SourceThreadLogon
       
   466 // Passes logon request to the RTP data source.
       
   467 // ---------------------------------------------------------------------------
       
   468 //
       
   469 TInt CDTMFPayloadFormatRead::SourceThreadLogon(
       
   470     MAsyncEventHandler& aEventHandler )
       
   471     {
       
   472     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::SourceThreadLogon") );
       
   473 
       
   474     if ( iClip )
       
   475         {
       
   476         iClip->SourceThreadLogon( aEventHandler );
       
   477         return KErrNone;
       
   478         }
       
   479     else
       
   480         {
       
   481         return KErrNotReady;
       
   482         }
       
   483     }
       
   484 
       
   485 // ---------------------------------------------------------------------------
       
   486 // CDTMFPayloadFormatRead::SourceThreadLogoff
       
   487 // Passes log out request to the RTP data source.
       
   488 // ---------------------------------------------------------------------------
       
   489 //
       
   490 void CDTMFPayloadFormatRead::SourceThreadLogoff( )
       
   491     {
       
   492     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::SourceThreadLogoff") );
       
   493     
       
   494     iClip->SourceThreadLogoff( );
       
   495     }
       
   496         
       
   497 // ---------------------------------------------------------------------------
       
   498 // CDTMFPayloadFormatRead::SourcePrimeL
       
   499 // Passes prime transition to the RTP data source.
       
   500 // ---------------------------------------------------------------------------
       
   501 //
       
   502 void CDTMFPayloadFormatRead::SourcePrimeL()
       
   503     {
       
   504     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::SourcePrimeL") );
       
   505     
       
   506     iClip->SourcePrimeL();
       
   507     }
       
   508 
       
   509 // ---------------------------------------------------------------------------
       
   510 // CDTMFPayloadFormatRead::SourcePlayL
       
   511 // Passes play transition to the RTP data source.
       
   512 // ---------------------------------------------------------------------------
       
   513 //
       
   514 void CDTMFPayloadFormatRead::SourcePlayL()
       
   515     {
       
   516     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::SourcePlayL") );
       
   517     
       
   518     iClip->SourcePlayL();
       
   519     FillSourceBufferL();
       
   520     }
       
   521 
       
   522 // ---------------------------------------------------------------------------
       
   523 // CDTMFPayloadFormatRead::SourcePauseL
       
   524 // Passes pause transition to the RTP data source.
       
   525 // ---------------------------------------------------------------------------
       
   526 //
       
   527 void CDTMFPayloadFormatRead::SourcePauseL()
       
   528     {
       
   529     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::SourcePauseL") );
       
   530     
       
   531     iClip->SourcePauseL();
       
   532     }
       
   533 
       
   534 // ---------------------------------------------------------------------------
       
   535 // CDTMFPayloadFormatRead::SourceStopL
       
   536 // Passes stop transition to the RTP data source.
       
   537 // ---------------------------------------------------------------------------
       
   538 //
       
   539 void CDTMFPayloadFormatRead::SourceStopL()
       
   540     {
       
   541     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::SourceStopL") );
       
   542       
       
   543     iClip->SourceStopL();
       
   544     iStateMachine->Cancel( );
       
   545     iStateMachine->ChangeState( EDecodeIdle );
       
   546     iBufferToReadExists = EFalse;
       
   547     }
       
   548 
       
   549 // ---------------------------------------------------------------------------
       
   550 // CDTMFPayloadFormatRead::BufferFilledL
       
   551 // Called by DataSource after it has been filled the buffer.
       
   552 // ---------------------------------------------------------------------------
       
   553 //
       
   554 void CDTMFPayloadFormatRead::DataBufferFilledL( CMMFBuffer* aBuffer, 
       
   555     const TRtpRecvHeader& aRtpHeader )
       
   556     {
       
   557     DP_DTMF_READ3( _L("CDTMFPayloadFormatRead::DataBufferFilledL, TSTAMP: %u, tick = %u"),
       
   558         aRtpHeader.iTimestamp, User::NTickCount() );
       
   559     
       
   560     if ( iCInfo.iPayloadType != aRtpHeader.iPayloadType )
       
   561         {
       
   562         // We are not interested about this RTP packet. Ask another.
       
   563         FillSourceBufferL();
       
   564         return;
       
   565         }
       
   566     
       
   567     // Do other checks, null ptr and buffer type.
       
   568     __ASSERT_ALWAYS( aBuffer, User::Leave( KErrArgument ) );
       
   569     if ( KUidMmfDataBuffer != aBuffer->Type() )
       
   570         {
       
   571         User::Leave( KErrNotSupported );
       
   572         }
       
   573     
       
   574     if ( iStateMachine->IsActive() )
       
   575         {
       
   576         DP_DTMF_READ( _L("CDTMFPayloadFormatRead::BufferFilledL - REJECTED") );
       
   577         // We are processing former events, do not mind incoming packets
       
   578         // but ask for more.
       
   579         FillSourceBufferL();
       
   580         }
       
   581     else
       
   582         {
       
   583         CMMFDataBuffer* dataBuffer = static_cast<CMMFDataBuffer*>( aBuffer );
       
   584         if ( !dataBuffer->Data().Length() )
       
   585             {
       
   586             // Do not handle packet, ask for more
       
   587             DP_DTMF_READ( _L("CDTMFPayloadFormatRead::BufferFilledL - NULL PAYLOAD") );
       
   588             FillSourceBufferL();
       
   589             }
       
   590         else
       
   591             {
       
   592             // If we have already received the first packet from this event,
       
   593             // then we should not decode and thus restart the playback because
       
   594             // this will do unnecessary stop, prepare and play sequence to
       
   595             // the tone player and causes distortions to DTMF playback.
       
   596             // Normally events with marker bit set in RTP header are sent three
       
   597             // times as specified in RFC2833 in order the implementation to be
       
   598             // robust.
       
   599             if ( !CompareFirstPacketRtpHeaders( aRtpHeader ) )
       
   600                 {
       
   601                 DP_DTMF_READ( _L("CDTMFPayloadFormatRead::BufferFilledL - NEW DATA") );
       
   602                 
       
   603                 iBufferToReadExists = ETrue;
       
   604                 iFirstPacket = aRtpHeader.iMarker;
       
   605             
       
   606                 // Copy received buffer
       
   607                 iSourceBuffer->Data().Copy( dataBuffer->Data() );
       
   608                 iPayloadDecoder->SetCurTimeStamp( aRtpHeader.iTimestamp );
       
   609 
       
   610                 // Whenever BufferFilledL is called from RtpSourceSink
       
   611                 // set the state machine to fillsinkbuffer.
       
   612                 iStateMachine->ChangeState( ESourceDataReady );
       
   613                 }
       
   614             else
       
   615                 {
       
   616                 DP_DTMF_READ( _L("CDTMFPayloadFormatRead::BufferFilledL - OLD DATA") );
       
   617                 
       
   618                 // Ask more data.
       
   619                 FillSourceBufferL();
       
   620                 }
       
   621             }
       
   622         }
       
   623     }
       
   624 
       
   625 // ---------------------------------------------------------------------------
       
   626 // CDTMFPayloadFormatRead::SetSourcePrioritySettings
       
   627 //
       
   628 // ---------------------------------------------------------------------------
       
   629 //
       
   630 void CDTMFPayloadFormatRead::SetSourcePrioritySettings( 
       
   631                             const TMMFPrioritySettings& aPrioritySettings )
       
   632     {
       
   633     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::SetSinkPrioritySettings") );
       
   634 
       
   635     TMMFPrioritySettings settings = aPrioritySettings;
       
   636     settings.iState = EMMFStateTonePlaying;
       
   637     iTonePlayer->SetPriority( settings.iPriority, settings.iPref );
       
   638     }
       
   639     
       
   640 // ---------------------------------------------------------------------------
       
   641 // CDTMFPayloadFormatRead::MatoPrepareComplete
       
   642 // 
       
   643 // ---------------------------------------------------------------------------
       
   644 //
       
   645 void CDTMFPayloadFormatRead::MatoPrepareComplete( TInt aError )
       
   646     {
       
   647     if ( aError )
       
   648         {
       
   649         // Some error, do not try to play DTMF
       
   650         DP_DTMF_READ2( _L("CDTMFPayloadFormatRead::MatoPrepareComplete - ERR: %d "), aError );
       
   651         }
       
   652     else
       
   653         {
       
   654         DP_DTMF_READ2( _L("CDTMFPayloadFormatRead::MatoPrepareComplete, tick = %u"),
       
   655             User::NTickCount() );
       
   656         iTonePlayer->Play();
       
   657         }    
       
   658     }
       
   659     
       
   660 // ---------------------------------------------------------------------------
       
   661 // CDTMFPayloadFormatRead::MatoPlayComplete
       
   662 // 
       
   663 // ---------------------------------------------------------------------------
       
   664 //
       
   665 void CDTMFPayloadFormatRead::MatoPlayComplete( TInt aError )
       
   666     {
       
   667     if ( KErrNone != aError )
       
   668         {
       
   669         DP_DTMF_READ2( _L(
       
   670             "CDTMFPayloadFormatRead::MatoPlayComplete - ERR: %d"), aError );
       
   671         }
       
   672     else
       
   673         {
       
   674         DP_DTMF_READ2( _L("CDTMFPayloadFormatRead::MatoPlayComplete KErrNone, tick = %u"),
       
   675             User::NTickCount() );
       
   676         }
       
   677     }
       
   678 
       
   679 // ---------------------------------------------------------------------------
       
   680 // CDTMFPayloadFormatRead::ConfigurePayloadFormatL
       
   681 // Configure payload decoding parameters.
       
   682 // ---------------------------------------------------------------------------
       
   683 //
       
   684 void CDTMFPayloadFormatRead::ConfigurePayloadFormatL( 
       
   685     const TDesC8& aConfigParams )
       
   686     {
       
   687     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::ConfigurePayloadFormatL") );
       
   688 
       
   689     if ( aConfigParams.Size() == sizeof( TMccCodecInfo ) )
       
   690         {
       
   691         TMccCodecInfoBuffer infoBuffer;
       
   692         infoBuffer.Copy( aConfigParams );
       
   693         iCInfo = infoBuffer();
       
   694         
       
   695         if ( !iPayloadDecoder )
       
   696             {
       
   697             iPayloadDecoder = CDTMFPayloadDecoder::NewL();
       
   698             }
       
   699 
       
   700         if ( 0 < iCInfo.iRedundancyCount
       
   701             && KMaxDtmfRedCount >= TInt( iCInfo.iRedundancyCount ) )
       
   702             {
       
   703             if ( EGenRedUsed == iCInfo.iAlgoUsed )
       
   704                 {
       
   705                 iPayloadDecoder->SetPayloadFormat( 
       
   706                                                 EDTMFPayloadFormatRedEvents );
       
   707                 }
       
   708             else
       
   709                 {
       
   710                 User::Leave( KErrArgument );
       
   711                 }
       
   712             }
       
   713         }
       
   714     else
       
   715         {
       
   716         User::Leave( KErrArgument );
       
   717         }
       
   718     
       
   719     CreateClipBufferL();
       
   720     
       
   721     DP_DTMF_READ( _L( "CDTMFPayloadFormatRead::ConfigurePayloadFormatL OUT") );
       
   722     }
       
   723     
       
   724 // -----------------------------------------------------------------------------
       
   725 // CDTMFPayloadFormatRead::CreateClipBufferL
       
   726 // Creates buffer needed in data transfer with format readers clip.
       
   727 // -----------------------------------------------------------------------------
       
   728 //
       
   729 void CDTMFPayloadFormatRead::CreateClipBufferL()
       
   730     {
       
   731     // If we have a source buffer already and it's size is correct, then this
       
   732     // one is a NOP
       
   733     if ( iSourceBuffer && KDTMFDefaultPayloadSize ==
       
   734          iSourceBuffer->Data().MaxLength() )
       
   735         {
       
   736         DP_DTMF_READ( _L("CDTMFPayloadFormatRead::CreateClipBufferL NOP") );
       
   737         
       
   738         return;
       
   739         }
       
   740     // We already have source buffer, own it and it can be reallocated.
       
   741     else if ( iSourceBuffer && iSourceBufOwnership &&
       
   742               KUidMmfDescriptorBuffer == iSourceBuffer->Type() )
       
   743         {
       
   744         DP_DTMF_READ( _L("CDTMFPayloadFormatRead::CreateClipBufferL ReAlloc") );
       
   745         
       
   746         CMMFDescriptorBuffer* desBuf =
       
   747             static_cast<CMMFDescriptorBuffer*>( iSourceBuffer );
       
   748         desBuf->ReAllocBufferL( KDTMFDefaultPayloadSize );
       
   749         desBuf = NULL;
       
   750         return;
       
   751         }
       
   752     // everything else...
       
   753     
       
   754     DP_DTMF_READ( _L("CDTMFPayloadFormatRead::CreateClipBufferL") );
       
   755     
       
   756     if ( iSourceBufOwnership )
       
   757         {
       
   758         delete iSourceBuffer;
       
   759         iSourceBuffer = NULL;
       
   760         }
       
   761     
       
   762     DP_DTMF_READ2( _L("CDTMFPayloadFormatRead::CreateClipBufferL 0x%x"), 
       
   763         iClip->DataSourceType().iUid );
       
   764     
       
   765     // We are normally dealing with multiplexer, so check against it.
       
   766     if ( iClip->CanCreateSourceBuffer() &&
       
   767          TUid::Uid( KDllUidMccMultiplexer ) == iClip->DataSourceType() )
       
   768         {
       
   769         static_cast<CMMFFormatDecode*>( iClip )->SuggestSourceBufferSize(
       
   770             KDTMFDefaultPayloadSize );
       
   771         
       
   772         TBool reference( EFalse );
       
   773         CMMFBuffer* sourceBuf 
       
   774             = iClip->CreateSourceBufferL( KUidMediaTypeAudio, reference );
       
   775         TBool isSupportedBuf 
       
   776             = CMMFBuffer::IsSupportedDataBuffer( sourceBuf->Type() );
       
   777         TBool isOwnBuffer = reference ? EFalse : ETrue;
       
   778         
       
   779         if ( !isSupportedBuf )
       
   780             {
       
   781             if ( isOwnBuffer )
       
   782                 {
       
   783                 delete sourceBuf;
       
   784                 }
       
   785             
       
   786             User::Leave( KErrNotSupported );
       
   787             }
       
   788         
       
   789         iSourceBufOwnership = isOwnBuffer;
       
   790         iSourceBuffer = static_cast<CMMFDataBuffer*>( sourceBuf );
       
   791         }
       
   792     else
       
   793         {
       
   794         DP_DTMF_READ( _L("CDTMFPayloadFormatRead::CreateClipBufferL own buffer") );
       
   795         
       
   796         iSourceBufOwnership = ETrue;
       
   797         iSourceBuffer = CMMFDataBuffer::NewL( KDTMFDefaultPayloadSize );
       
   798         }
       
   799     
       
   800     // If we are already waiting for source data we must renew that request
       
   801     // as the pointer to our buffer goes dangling in other components, esp.
       
   802     // multiplexer.
       
   803     FillSourceBufferL();
       
   804     
       
   805     DP_DTMF_READ2( _L("CDTMFPayloadFormatRead::CreateClipBufferL ownership: %d"),
       
   806         iSourceBufOwnership );
       
   807     }
       
   808 
       
   809 // ---------------------------------------------------------------------------
       
   810 // From class MPayloadFormatRead.
       
   811 //
       
   812 // ---------------------------------------------------------------------------
       
   813 //
       
   814 TInt CDTMFPayloadFormatRead::HandleError( TInt aError, 
       
   815     TFormatDecodeState /*aState*/ )
       
   816     {
       
   817     return aError;
       
   818     }
       
   819 
       
   820 // ---------------------------------------------------------------------------
       
   821 // CDTMFPayloadFormatRead::CompareFirstPacketRtpHeaders
       
   822 // ---------------------------------------------------------------------------
       
   823 //
       
   824 TBool CDTMFPayloadFormatRead::CompareFirstPacketRtpHeaders(
       
   825     const TRtpRecvHeader& aRtpheader )
       
   826     {
       
   827     const TRtpSequence next = iReceivedHeader.iSeqNum + 1;
       
   828     TBool ret = EFalse;
       
   829     const TUint8 KMarkerSet = 1;
       
   830     
       
   831     if ( next == aRtpheader.iSeqNum && 
       
   832         iReceivedHeader.iPayloadType == aRtpheader.iPayloadType &&
       
   833         iReceivedHeader.iTimestamp == aRtpheader.iTimestamp &&
       
   834         KMarkerSet == iReceivedHeader.iMarker &&
       
   835         KMarkerSet == aRtpheader.iMarker )
       
   836         {
       
   837         DP_DTMF_READ( _L("CDTMFPayloadFormatRead::CompareFirstPacketRtpHeaders MATCH") )
       
   838         ret = ETrue;
       
   839         }
       
   840     // else we return EFalse as set when declaring ret.
       
   841     
       
   842     iReceivedHeader = aRtpheader;
       
   843     return ret;
       
   844     }
       
   845 
       
   846 //  End of File