multimediacommsengine/mmcecli/src/mceaudiostream.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2005 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:    
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "mceaudiostream.h"
       
    22 #include "mcemanager.h"
       
    23 #include "mceaudiocodec.h"
       
    24 #include "mcecomaudiostream.h"
       
    25 #include "mceserial.h"
       
    26 #include "mceevents.h"
       
    27 #include "mcefilesource.h"
       
    28 #include "mcecomfilesource.h"
       
    29 #include "mcertpsource.h"
       
    30 #include "mceclilogs.h"
       
    31 #include "mcertpsink.h"
       
    32 #include "cleanupresetanddestroy.h"
       
    33 
       
    34 #define _FLAT_DATA static_cast<CMceComAudioStream*>( iFlatData )
       
    35 
       
    36 
       
    37 // ============================ MEMBER FUNCTIONS ===============================
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CMceAudioStream::NewL
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 EXPORT_C CMceAudioStream* CMceAudioStream::NewL()
       
    44     {
       
    45     CMceAudioStream* self = NewLC();
       
    46     CleanupStack::Pop( self );
       
    47     return self; 
       
    48     }
       
    49    
       
    50 // -----------------------------------------------------------------------------
       
    51 // CMceAudioStream::NewLC
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 EXPORT_C CMceAudioStream* CMceAudioStream::NewLC()
       
    55     {
       
    56     CMceAudioStream* self = new (ELeave) CMceAudioStream();
       
    57     CleanupStack::PushL( self );
       
    58     self->ConstructL();
       
    59     return self;    
       
    60     }
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CMceAudioStream::~CMceAudioStream
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 EXPORT_C CMceAudioStream::~CMceAudioStream()
       
    67     {
       
    68     iCodecs.ResetAndDestroy();
       
    69     }
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // CMceAudioStream::Codecs
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 EXPORT_C const RPointerArray<CMceAudioCodec>& CMceAudioStream::Codecs()
       
    76     {
       
    77     return iCodecs;
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // CMceAudioStream::AddCodecL
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 EXPORT_C void CMceAudioStream::AddCodecL(CMceAudioCodec* aCodec)
       
    85     {
       
    86     __ASSERT_ALWAYS(aCodec, User::Leave(KErrArgument));
       
    87     iCodecs.AppendL(aCodec);
       
    88     aCodec->Attach( *this );
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CMceAudioStream::RemoveCodecL
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 EXPORT_C void CMceAudioStream::RemoveCodecL(CMceAudioCodec& aCodec)
       
    96     {
       
    97     RemoveCodecL( aCodec, Binder() );
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CMceAudioStream::ReplaceCodecsL
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 EXPORT_C void CMceAudioStream::ReplaceCodecsL(
       
   105     RPointerArray<CMceAudioCodec>* /*aCodecs*/ )
       
   106     {
       
   107     User::Leave( KErrNotSupported );
       
   108     }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CMceAudioStream::CMceAudioStream
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 CMceAudioStream::CMceAudioStream()
       
   115     :CMceMediaStream()
       
   116     {
       
   117     }
       
   118 
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // CMceAudioStream::ConstructL
       
   122 // -----------------------------------------------------------------------------
       
   123 //
       
   124 void CMceAudioStream::ConstructL()
       
   125     {
       
   126     CMceComAudioStream* stream = CMceComAudioStream::NewLC();
       
   127     CMceMediaStream::ConstructL( stream );
       
   128     CleanupStack::Pop( stream );
       
   129     }
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // CMceAudioStream::InitializeL
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 void CMceAudioStream::InitializeL(
       
   136     CMceSession& aParent, 
       
   137     TBool aDiscardUnusedCodecs )
       
   138     {
       
   139     CMceMediaStream::InitializeL( aParent, aDiscardUnusedCodecs );
       
   140             
       
   141     for( TInt i = 0; i < iCodecs.Count(); i++ )
       
   142         {
       
   143         iCodecs[i]->InitializeL( *this );
       
   144         }
       
   145     }
       
   146 
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 // CMceAudioStream::InitializeL
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 void CMceAudioStream::InitializeL( CMceManager* aManager, CMceSession& aSession )
       
   153     {
       
   154     
       
   155     __ASSERT_ALWAYS( aManager, User::Leave( KErrArgument ) );
       
   156     
       
   157     CMceMediaStream::InitializeL( aManager, aSession );
       
   158     
       
   159     if ( iCodecs.Count() == 0 )
       
   160         {
       
   161         const RPointerArray<const CMceAudioCodec>& supportedCodes = 
       
   162                                       aManager->SupportedAudioCodecs();
       
   163                                       
       
   164         for( TInt i = 0; i < supportedCodes.Count(); i++ )
       
   165             {
       
   166             CMceAudioCodec* codec = supportedCodes[i]->CloneL();
       
   167             CleanupStack::PushL( codec );
       
   168             AddCodecL( codec );
       
   169             CleanupStack::Pop( codec );
       
   170             MCECLI_DEBUG_SVALUE("CMceAudioStream::InitializeL, \
       
   171 Added supported codec", codec->SdpName() );
       
   172             }
       
   173                                       
       
   174         }
       
   175         
       
   176     }
       
   177     
       
   178 
       
   179 // -----------------------------------------------------------------------------
       
   180 // CMceAudioStream::SynchronizeWithFileL
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 void CMceAudioStream::SynchronizeWithFileL( CMceFileSource& aFile )
       
   184     {
       
   185     MCECLI_DEBUG("CMceAudioStream::SynchronizeWithFileL, Entry");
       
   186     
       
   187     const TDesC8& audioCodec = aFile.SupportedAudio();
       
   188     
       
   189     TBool transcodingRequired( EFalse );
       
   190     
       
   191     CMceManager* manager = aFile.Manager();
       
   192     
       
   193     if ( iCodecs.Count() == 0 && manager )
       
   194         {
       
   195         const RPointerArray<const CMceAudioCodec>& supportedCodecs =
       
   196             manager->SupportedAudioCodecs();
       
   197          
       
   198         CMceAudioCodec* codec = NULL;
       
   199         
       
   200         for( TInt j = 0; j < supportedCodecs.Count() && !codec; j++ )
       
   201             {
       
   202             if ( audioCodec.CompareF( supportedCodecs[ j ]->SdpName() ) == 0 )
       
   203                 {
       
   204                 codec = supportedCodecs[ j ]->CloneL();
       
   205                 CleanupStack::PushL( codec );
       
   206                 AddCodecL( codec );
       
   207                 CleanupStack::Pop( codec );
       
   208                 MCECLI_DEBUG_SVALUE("CMceAudioStream::SynchronizeWithFileL, \
       
   209 Added supported codec", codec->SdpName() );
       
   210                 }
       
   211             }
       
   212         }
       
   213       
       
   214     for( TInt i = 0; i < iCodecs.Count(); i++ )
       
   215         {
       
   216         CMceAudioCodec* codec = iCodecs[i];
       
   217         if ( audioCodec.CompareF( codec->SdpName() ) == 0 )
       
   218             {
       
   219             const TMceFileInfo& info = aFile.FileInfo();
       
   220             
       
   221             TUint allowedBitrates = 0;
       
   222             TInt resolveError = 
       
   223                  static_cast<CMceComCodec*>( codec->FlatData() )->
       
   224                     ResolveAllowedBitrates( info.iAudioBitRate, allowedBitrates );
       
   225             
       
   226             if ( resolveError )
       
   227                 {
       
   228                 allowedBitrates = 0xFF;
       
   229                 }
       
   230                 
       
   231             codec->SetAllowedBitrates( allowedBitrates );
       
   232 
       
   233             TInt bitrateError = codec->SetBitrate( info.iAudioBitRate );
       
   234             
       
   235             if ( resolveError || bitrateError )
       
   236                 {
       
   237                 // Currently too high audio bitrate will not lead to transcoding
       
   238                 // needed state change
       
   239                 //transcodingRequired = ETrue;
       
   240                 }
       
   241             }
       
   242         else
       
   243             {
       
   244             delete iCodecs[ i ];
       
   245             iCodecs.Remove( i );
       
   246             i--;
       
   247             }
       
   248         }
       
   249         
       
   250     if ( iCodecs.Count() == 0 || transcodingRequired )
       
   251         {
       
   252         MCECLI_DEBUG("CMceAudioStream::SynchronizeWithFileL, transcoding required" );
       
   253         SetState( CMceMediaStream::ETranscodingRequired );
       
   254         }
       
   255     else
       
   256         {
       
   257         SetState( CMceMediaStream::EInitialized );
       
   258         }
       
   259 	TState state = State();
       
   260     MCECLI_DEBUG_MEDIASTATE("CMceAudioStream::SynchronizeWithFileL, state", state );
       
   261     MCECLI_DEBUG("CMceAudioStream::SynchronizeWithFileL, Exit");
       
   262     }
       
   263 
       
   264 // -----------------------------------------------------------------------------
       
   265 // CMceAudioStream::FindCodec
       
   266 // -----------------------------------------------------------------------------
       
   267 //
       
   268 CMceCodec* CMceAudioStream::FindCodec( CMceCodec& aCodec )
       
   269     {
       
   270     const RPointerArray<CMceCodec>& codecs = 
       
   271         reinterpret_cast<const RPointerArray<CMceCodec>&>( Codecs() );
       
   272     return CMceMediaStream::FindCodec( codecs, aCodec );
       
   273     }
       
   274     
       
   275 // -----------------------------------------------------------------------------
       
   276 // CMceAudioStream::ReorderCodecsByPreferenceL
       
   277 // -----------------------------------------------------------------------------
       
   278 //   
       
   279 void CMceAudioStream::ReorderCodecsByPreferenceL( TBool aDiscardUnusedCodecs )
       
   280     {
       
   281     // First remove all disabled codecs
       
   282     if ( aDiscardUnusedCodecs )
       
   283         {
       
   284         TInt lastIndex( iCodecs.Count() - 1 );
       
   285         for( TInt i = lastIndex; i >= 0; i-- )
       
   286             {
       
   287             if ( iCodecs[i]->State() == CMceCodec::EDisabled )
       
   288                 {
       
   289                 RemoveCodecL( *iCodecs[ i ] );
       
   290                 }
       
   291             }
       
   292         }
       
   293     
       
   294     // Then change order    
       
   295     TBool inPreferenceOrder = ETrue; 
       
   296     
       
   297     for( TInt i = 0; i < iCodecs.Count() - 1 && inPreferenceOrder; i++ )
       
   298         {
       
   299         if ( iCodecs[i]->Preference() > iCodecs[i+1]->Preference() )
       
   300             {
       
   301             inPreferenceOrder = EFalse;   
       
   302             }
       
   303         }
       
   304        
       
   305     if ( !inPreferenceOrder ) 
       
   306         {
       
   307         RPointerArray<CMceAudioCodec> updated;
       
   308         
       
   309         MceCleanupResetAndDestroyPushL( updated );
       
   310         
       
   311         TLinearOrder<CMceAudioCodec> order( CMceAudioCodec::ComparePreferences );
       
   312                     
       
   313         for( TInt i = iCodecs.Count() -1; i >= 0; i-- )
       
   314             {
       
   315             CMceAudioCodec* codec = iCodecs[i];
       
   316             iCodecs.Remove( i );
       
   317             User::LeaveIfError( updated.InsertInOrderAllowRepeats( codec, order ) );
       
   318             }
       
   319            
       
   320         iCodecs.ResetAndDestroy();
       
   321          
       
   322         for( TInt i = updated.Count() -1; i >= 0; i-- )
       
   323             {
       
   324             CMceAudioCodec* newcodec = updated[i];
       
   325             updated.Remove( i );
       
   326             User::LeaveIfError( iCodecs.InsertInOrderAllowRepeats( newcodec, order ) );
       
   327             }
       
   328        
       
   329         CleanupStack::PopAndDestroy();//updated;   
       
   330         }   
       
   331     }
       
   332 
       
   333 // -----------------------------------------------------------------------------
       
   334 // CMceAudioStream::BaseCodecs
       
   335 // -----------------------------------------------------------------------------
       
   336 //
       
   337 const RPointerArray<CMceCodec>& CMceAudioStream::BaseCodecs()
       
   338     {
       
   339     const RPointerArray<CMceCodec>& codecs = 
       
   340         reinterpret_cast<const RPointerArray<CMceCodec>&>( Codecs() );
       
   341     return codecs;
       
   342     }
       
   343     
       
   344 // -----------------------------------------------------------------------------
       
   345 // CMceAudioStream::FindDtmfRelatedComponents
       
   346 // -----------------------------------------------------------------------------
       
   347 //
       
   348 void CMceAudioStream::FindDtmfRelatedComponents( 
       
   349     TBool& aRtpSourceExists, 
       
   350     TBool& aRtpSinkExists, 
       
   351     TBool& aDtmfCodecExists,
       
   352     TBool aBinderOriginated )
       
   353     {
       
   354     if ( !aRtpSourceExists && Source() && Source()->Type() == KMceRTPSource )
       
   355         {
       
   356         aRtpSourceExists = ETrue;
       
   357         }
       
   358         
       
   359     for ( TInt i = 0; i < Sinks().Count() && !aRtpSinkExists; i++ )
       
   360         {
       
   361         if ( Sinks()[ i ]->Type() == KMceRTPSink )
       
   362             {
       
   363             aRtpSinkExists = ETrue;
       
   364             }
       
   365         }
       
   366         
       
   367     for ( TInt j = 0; j < Codecs().Count() && !aDtmfCodecExists; j++ )
       
   368         {
       
   369         if ( Codecs()[ j ]->SdpName() == KMceSDPNameDtmf )
       
   370             {
       
   371             aDtmfCodecExists = ETrue;
       
   372             }
       
   373         }
       
   374     
       
   375     TBool commandBound = aBinderOriginated ? ( Binder() ) : ( !Binder() );
       
   376     if ( iLinkedStream && commandBound )
       
   377         {
       
   378         aBinderOriginated = EFalse;
       
   379         static_cast<CMceAudioStream*>( 
       
   380             iLinkedStream )->FindDtmfRelatedComponents( aRtpSourceExists, 
       
   381                                                         aRtpSinkExists,
       
   382                                                         aDtmfCodecExists,
       
   383                                                         aBinderOriginated );
       
   384         }
       
   385     }
       
   386 
       
   387 
       
   388 // -----------------------------------------------------------------------------
       
   389 // CMceAudioStream::RemoveCodecL
       
   390 // -----------------------------------------------------------------------------
       
   391 //
       
   392 void CMceAudioStream::RemoveCodecL( CMceAudioCodec& aCodec, TBool aBinderOriginated )
       
   393     {
       
   394     MCECLI_DEBUG_SVALUE( "CMceAudioStream::RemoveCodecL, sdp name:", aCodec.SdpName() )
       
   395     
       
   396     TBool removed( EFalse );
       
   397     for(int i = 0; i < iCodecs.Count() && !removed; i++ )
       
   398         {
       
   399         if( iCodecs[i] == &aCodec || 
       
   400             iCodecs[i]->SdpName().CompareF( aCodec.SdpName() ) == 0 )
       
   401             {
       
   402             MCECLI_DEBUG( "CMceAudioStream::RemoveCodecL, removing" )
       
   403             
       
   404             TBool commandBound = aBinderOriginated ? ( Binder() ) : ( !Binder() );
       
   405             if ( BoundStream() && commandBound )
       
   406                 {
       
   407                 static_cast<CMceAudioStream*>( 
       
   408                     iLinkedStream )->RemoveCodecL( aCodec, aBinderOriginated );
       
   409                 }
       
   410                 
       
   411             delete iCodecs[i];
       
   412             iCodecs.Remove( i );
       
   413             removed = ETrue;
       
   414             }
       
   415         }
       
   416         
       
   417     MCECLI_DEBUG( "CMceAudioStream::RemoveCodecL, Exit" )
       
   418     }
       
   419         
       
   420 // -----------------------------------------------------------------------------
       
   421 // CMceAudioStream::InternalizeL
       
   422 // -----------------------------------------------------------------------------
       
   423 //
       
   424 void CMceAudioStream::InternalizeL( MMceComSerializationContext& aSerCtx )
       
   425     {
       
   426     CMceMediaStream::InternalizeL( aSerCtx );
       
   427     
       
   428     TMceAudioStreamSerializer<CMceAudioStream> serial( *this );
       
   429     serial.InternalizeL( aSerCtx );
       
   430 
       
   431     }
       
   432     
       
   433 // -----------------------------------------------------------------------------
       
   434 // CMceAudioStream::ExternalizeL
       
   435 // -----------------------------------------------------------------------------
       
   436 //
       
   437 void CMceAudioStream::ExternalizeL( MMceComSerializationContext& aSerCtx )
       
   438     {
       
   439     CMceMediaStream::ExternalizeL( aSerCtx );
       
   440 
       
   441     TMceAudioStreamSerializer<CMceAudioStream> serial( *this );
       
   442     serial.ExternalizeL( aSerCtx );
       
   443     
       
   444     }
       
   445 
       
   446 
       
   447 
       
   448 
       
   449 // -----------------------------------------------------------------------------
       
   450 // CMceAudioStream::EventReceivedL
       
   451 // -----------------------------------------------------------------------------
       
   452 //
       
   453 TInt CMceAudioStream::EventReceivedL( TMceEvent& aEvent )
       
   454     {
       
   455 
       
   456     TInt status = CMceMediaStream::EventReceivedL( aEvent );
       
   457     if ( status != KMceEventNotConsumed )
       
   458         {
       
   459         return status;
       
   460         }
       
   461 
       
   462     //try codecs
       
   463     if ( aEvent.Id().IsCodecId() )
       
   464         {
       
   465         TInt codecStatus = status;
       
   466         TInt j = 0;
       
   467         while ( codecStatus != KMceEventConsumed && j < iCodecs.Count() )
       
   468             {
       
   469             CMceAudioCodec* codec = iCodecs[j]; 
       
   470             codecStatus = codec->EventReceivedL( aEvent );
       
   471             if ( codecStatus == KMceEventUpdate )
       
   472                 {
       
   473                 MCECLI_DEBUG("CMceAudioStream::EventReceivedL, \
       
   474 update codec by replacing old version with new one");
       
   475                 CMceMsgObject<CMceAudioCodec>* updateMsg = 
       
   476                     static_cast<CMceMsgObject<CMceAudioCodec>*>( aEvent.Message() );
       
   477                 CMceAudioCodec* updateCodec = updateMsg->Object();
       
   478                 CleanupStack::PushL( updateCodec );
       
   479                 iCodecs.Remove( j );
       
   480                 delete codec;
       
   481                 updateCodec->InitializeL( *this );
       
   482                 iCodecs.AppendL( updateCodec );
       
   483                 CleanupStack::Pop( updateCodec );
       
   484                 updateCodec->Updated();
       
   485                 codecStatus = KMceEventConsumed;
       
   486                 }
       
   487             j++;                
       
   488             }
       
   489         status = codecStatus == KMceEventNotOwner ? KMceEventNotConsumed : codecStatus;
       
   490         }
       
   491 
       
   492     return status;
       
   493     
       
   494     }