convergedcallengine/csplugin/src/cspaudiostreams.cpp
branchRCL_3
changeset 20 987c9837762f
parent 0 ff3b6d0fd310
equal deleted inserted replaced
19:7d48bed6ce0c 20:987c9837762f
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Starts and stops audio streams.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "cspaudiostreams.h"
       
    20 #include "cspmicrophone.h"
       
    21 #include "cspspeaker.h"
       
    22 #include "csplogger.h"
       
    23 #include "mcspaudiostreamobserver.h"
       
    24 #include "csptimer.h"
       
    25 
       
    26 #include <TelephonyAudioRouting.h>
       
    27 
       
    28 /**
       
    29 * Timeout initial value.
       
    30 */
       
    31 const TInt KTimeoutInitial = 200000; // 0.2s
       
    32 
       
    33 /**
       
    34 * Double the timeout for every retry.
       
    35 */
       
    36 const TInt KTimeoutMultiplier = 2;
       
    37 
       
    38 // ======== MEMBER FUNCTIONS ========
       
    39    
       
    40 // ---------------------------------------------------------------------------
       
    41 // Static constructor
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 CSPAudioStreams* CSPAudioStreams::NewL( )
       
    45     {
       
    46     CSPAudioStreams* self = 
       
    47         CSPAudioStreams::NewLC();
       
    48     CleanupStack::Pop( self );
       
    49     return self;
       
    50     }
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // Static constructor
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 CSPAudioStreams* CSPAudioStreams::NewLC(  )
       
    57     {
       
    58     CSPAudioStreams* self = new( ELeave ) CSPAudioStreams( );
       
    59     CleanupStack::PushL( self );
       
    60     self->ConstructL( );
       
    61     return self;
       
    62     }
       
    63 
       
    64 // ---------------------------------------------------------------------------
       
    65 // Destructor
       
    66 // ---------------------------------------------------------------------------
       
    67 //
       
    68 CSPAudioStreams::~CSPAudioStreams()
       
    69     {
       
    70     if (iTimer)
       
    71         {
       
    72         iTimer->CancelNotify();    
       
    73         delete iTimer;
       
    74         }
       
    75     
       
    76     delete iSpeaker;
       
    77     delete iMic;
       
    78     delete iAudioRouting;
       
    79     }
       
    80 
       
    81 // ---------------------------------------------------------------------------
       
    82 // Sets observer for audio stream.
       
    83 // ---------------------------------------------------------------------------
       
    84 //
       
    85 void CSPAudioStreams::SetAudioStreamObserver( 
       
    86     MCSPAudioStreamObserver& aObserver )
       
    87     {
       
    88     iStreamObserver = &aObserver;
       
    89     }
       
    90     
       
    91 // ---------------------------------------------------------------------------
       
    92 // Gives access to mic
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 CSPMicrophone& CSPAudioStreams::Mic() const
       
    96     {
       
    97     return *iMic;
       
    98     }
       
    99 
       
   100 // ---------------------------------------------------------------------------
       
   101 // Gives access to speaker
       
   102 // ---------------------------------------------------------------------------
       
   103 //
       
   104 CSPSpeaker& CSPAudioStreams::Speaker() const
       
   105     {
       
   106     return *iSpeaker;
       
   107     }
       
   108     
       
   109 // ---------------------------------------------------------------------------
       
   110 // From class MCSPAudioStream
       
   111 // Activates mic and speaker.
       
   112 // ---------------------------------------------------------------------------
       
   113 //
       
   114 void CSPAudioStreams::StartStreams() 
       
   115     {
       
   116     CSPLOGSTRING(CSPINT, "CSPAudioStreams::StartStreams");
       
   117     
       
   118     if( !IsMicAndSpeakerStarted() )    
       
   119         {
       
   120         StartMicAndSpeaker(); 
       
   121         
       
   122         if( iStreamObserver )
       
   123             {
       
   124             iStreamObserver->AudioStreamsStarted();
       
   125             }
       
   126         }
       
   127     }
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // From class MCSPAudioStream
       
   131 // Deactivates mic and speaker if the streams are active or they are 
       
   132 // activating. 
       
   133 // ---------------------------------------------------------------------------
       
   134 //
       
   135 void CSPAudioStreams::StopStreams() 
       
   136     {
       
   137     CSPLOGSTRING(CSPINT, "CSPAudioStreams::StopStreams");
       
   138     
       
   139     if( IsMicAndSpeakerStarted() )
       
   140         {
       
   141             CSPLOGSTRING(CSPINT, "CSPAudioStreams::StopStreams Stopping");
       
   142         iTimer->CancelNotify(); 
       
   143         iTimeout = KTimeoutInitial;
       
   144         if( iStreamObserver ) 
       
   145             {
       
   146             iStreamObserver->AudioStreamsGoingToStop();
       
   147             }
       
   148         iMic->Deactivate();
       
   149         iSpeaker->Deactivate();
       
   150         }
       
   151     }
       
   152 
       
   153 // ---------------------------------------------------------------------------
       
   154 // CSPAudioStreams::VolumeChangedEar
       
   155 // ---------------------------------------------------------------------------
       
   156 //
       
   157 void CSPAudioStreams::VolumeChangedEar(TInt aVolume)
       
   158     {
       
   159     CSPLOGSTRING2(CSPINT, "CSPAudioStreams::SetVolume %d", aVolume);
       
   160     iVolumeEar = aVolume;
       
   161     iSpeaker->SetVolume( aVolume );
       
   162     }
       
   163 
       
   164 // ---------------------------------------------------------------------------
       
   165 // CSPAudioStreams::VolumeChangedLoudspeaker
       
   166 // ---------------------------------------------------------------------------
       
   167 //
       
   168 void CSPAudioStreams::VolumeChangedLoudspeaker(TInt aVolume)
       
   169     {
       
   170     CSPLOGSTRING2(CSPINT, "CSPAudioStreams::SetVolume %d", aVolume);
       
   171     iVolumeLoudspeaker = aVolume;
       
   172     iSpeaker->SetVolume( aVolume );
       
   173     }
       
   174 
       
   175 // ---------------------------------------------------------------------------
       
   176 // CSPAudioStreams::StoreVolumes
       
   177 // ---------------------------------------------------------------------------
       
   178 //
       
   179 void CSPAudioStreams::StoreVolumes(TInt aVolumeEar, TInt aVolumeLoudspeaker )
       
   180     {
       
   181     iVolumeEar = aVolumeEar;
       
   182     iVolumeLoudspeaker = aVolumeLoudspeaker;
       
   183     }
       
   184     
       
   185 // ---------------------------------------------------------------------------
       
   186 // CSPAudioStreams::ApplyVolume
       
   187 // ---------------------------------------------------------------------------
       
   188 //
       
   189 void CSPAudioStreams::ApplyVolume(TInt aVolumeEar, TInt aVolumeLoudspeaker )
       
   190     {
       
   191     CTelephonyAudioRouting::TAudioOutput current = iAudioRouting->Output();
       
   192     if ( current == CTelephonyAudioRouting::ELoudspeaker )
       
   193         {
       
   194         CSPLOGSTRING( CSPINT, "CSPAudioStreams::ApplyVolume Loudspeaker Active: SetVolume" );
       
   195         iSpeaker->SetVolume( aVolumeLoudspeaker );
       
   196         }
       
   197         // else: ear volume should be used
       
   198      else if ( current == CTelephonyAudioRouting::EHandset )
       
   199         {
       
   200         CSPLOGSTRING( CSPINT, "CSPAudioStreams::ApplyVolume Ear Active: SetVolume" );
       
   201         iSpeaker->SetVolume( aVolumeEar );
       
   202         }
       
   203     else
       
   204         {
       
   205         CSPLOGSTRING2( CSPINT, "CSPAudioStreams::ApplyVolume UNKNOWN AUDIO OUTPUT MODE %d", current );
       
   206         // No volume setting
       
   207         iSpeaker->SetVolume( aVolumeEar );
       
   208         }
       
   209     }
       
   210 
       
   211 // ---------------------------------------------------------------------------
       
   212 // CSPAudioStreams::SetMuted
       
   213 // ---------------------------------------------------------------------------
       
   214 //  
       
   215 void CSPAudioStreams::SetMuted() 
       
   216     {
       
   217     iMic->SetMuted();
       
   218     }
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 // CSPAudioStreams::SetUnmuted
       
   222 // ---------------------------------------------------------------------------
       
   223 //  
       
   224 void CSPAudioStreams::SetUnmuted() 
       
   225     {
       
   226     iMic->SetUnmuted();
       
   227     }
       
   228 
       
   229 // ---------------------------------------------------------------------------
       
   230 // From class MCSPAudioStreamObserver
       
   231 // If speaker is already active then the streams are active.
       
   232 // If speker is not active and it is not activating then speaker then
       
   233 // activation has failed and start retry timer.
       
   234 // ---------------------------------------------------------------------------
       
   235 //   
       
   236 void CSPAudioStreams::MicActivatedSuccessfully()
       
   237     {
       
   238     CSPLOGSTRING(CSPINT, "CSPAudioStreams::MicActivatedSuccessfully" );
       
   239     if( iSpeaker->IsActive() )
       
   240         {
       
   241         // Mic and speaker are active.
       
   242         AudioStreamsStarted();
       
   243         }
       
   244     else if( !iSpeaker->IsActivationOngoing() ) 
       
   245         {
       
   246         // Start retry timer for activating speaker again
       
   247         StartTimer();
       
   248         }
       
   249     }
       
   250     
       
   251 // ---------------------------------------------------------------------------
       
   252 // From class MCSPAudioStreamObserver
       
   253 // If mic is already active then streams are active.
       
   254 // If mic is not active and it is not activating then mic activation has
       
   255 // failed and start retry timer.
       
   256 // ---------------------------------------------------------------------------
       
   257 //   
       
   258 void CSPAudioStreams::SpeakerActivatedSuccessfully()
       
   259     {
       
   260     CSPLOGSTRING(CSPINT, "CSPAudioStreams::SpeakerActivatedSuccessfully" );
       
   261     if( iMic->IsActive() ) 
       
   262         {
       
   263         // Mic and speaker are active.
       
   264         AudioStreamsStarted();
       
   265         }
       
   266     else if( !iMic->IsActivationOngoing() ) 
       
   267         {
       
   268         // Start retry timer for activating mic again.
       
   269         StartTimer();
       
   270         }
       
   271     }
       
   272 
       
   273 // ---------------------------------------------------------------------------
       
   274 // From class MCSPAudioStreamObserver
       
   275 // Starts timer for trying activation again.
       
   276 // ---------------------------------------------------------------------------
       
   277 //    
       
   278 void CSPAudioStreams::MicActivationFailed()
       
   279     {
       
   280     CSPLOGSTRING(CSPINT, "CSPAudioStreams::MicActivationFailed" );
       
   281    
       
   282     // Dont start timer until speaker has stopped activation.
       
   283     if( !iSpeaker->IsActivationOngoing() ) 
       
   284         {
       
   285         StartTimer();
       
   286         }
       
   287     }
       
   288     
       
   289 // ---------------------------------------------------------------------------
       
   290 // From class MCSPAudioStreamObserver
       
   291 // Starts timer for trying activation again.
       
   292 // ---------------------------------------------------------------------------
       
   293 // 
       
   294 void CSPAudioStreams::SpeakerActivationFailed()
       
   295     {
       
   296     CSPLOGSTRING(CSPINT, "PE.AudioStreams::SpeakerActivationFailed" );
       
   297     
       
   298     // Dont start timer until mic has stopped activation.
       
   299     if( !iMic->IsActivationOngoing() )
       
   300         {
       
   301         StartTimer();
       
   302         }
       
   303     }
       
   304     
       
   305 // ---------------------------------------------------------------------------
       
   306 // From class MCSPTimerObserver
       
   307 // Notify from CSPTimer that timeout passed. Try to start mic and
       
   308 // speaker again.
       
   309 // ---------------------------------------------------------------------------
       
   310 // 
       
   311 void CSPAudioStreams::TimerEvent()
       
   312     {
       
   313     CSPLOGSTRING(CSPINT, "CSPAudioStreams.TimerEvent" );
       
   314     iTimeout *= KTimeoutMultiplier;
       
   315     StartMicAndSpeaker();
       
   316     }
       
   317 
       
   318 // -----------------------------------------------------------------------------
       
   319 // CTSEAudioRouteObserver::AvailableOutputsChanged
       
   320 // -----------------------------------------------------------------------------
       
   321 //
       
   322 void CSPAudioStreams::AvailableOutputsChanged( 
       
   323         CTelephonyAudioRouting& /*aTelephonyAudioRouting*/ )
       
   324     {
       
   325     CSPLOGSTRING(CSPINT, "CSPAudioStreams::AvailableOutputsChanged" );
       
   326     }
       
   327     
       
   328 // -----------------------------------------------------------------------------
       
   329 // CTSEAudioRouteObserver::OutputChanged
       
   330 // -----------------------------------------------------------------------------
       
   331 //
       
   332 void CSPAudioStreams::OutputChanged( 
       
   333         CTelephonyAudioRouting& /*aTelephonyAudioRouting*/ )
       
   334     {
       
   335     CSPLOGSTRING(CSPINT, "CSPAudioStreams::OutputChanged" );
       
   336     ApplyVolume(iVolumeEar, iVolumeLoudspeaker);
       
   337     }
       
   338 
       
   339 // -----------------------------------------------------------------------------
       
   340 // CSPAudioStreams::SetOutputComplete
       
   341 // -----------------------------------------------------------------------------
       
   342 //
       
   343 void CSPAudioStreams::SetOutputComplete( CTelephonyAudioRouting& /*aTelephonyAudioRouting*/,
       
   344                                          TInt /*aError*/ )
       
   345    {
       
   346    }  
       
   347 
       
   348 // ---------------------------------------------------------------------------
       
   349 // Constructor
       
   350 // ---------------------------------------------------------------------------
       
   351 //
       
   352 CSPAudioStreams::CSPAudioStreams( ): iTimeout(KTimeoutInitial)
       
   353     {
       
   354     }
       
   355 
       
   356 // ---------------------------------------------------------------------------
       
   357 // Second phase constructor
       
   358 // ---------------------------------------------------------------------------
       
   359 //
       
   360 void CSPAudioStreams::ConstructL( )
       
   361     {
       
   362     CSPLOGSTRING(CSPINT, "CSPAudioStreams::ConstructL");
       
   363 
       
   364     iTimer = CSPTimer::NewL();
       
   365     iMic = CSPMicrophone::NewL( *this );
       
   366     iSpeaker = CSPSpeaker::NewL( *this );
       
   367     
       
   368     #if !defined(__WINSCW__)
       
   369     iAudioRouting = CTelephonyAudioRouting::NewL( *this );
       
   370     #endif //__WINSCW__        
       
   371     }
       
   372     
       
   373 // ---------------------------------------------------------------------------
       
   374 // Resets timer
       
   375 // ---------------------------------------------------------------------------
       
   376 //
       
   377 void CSPAudioStreams::AudioStreamsStarted()
       
   378     {
       
   379     CSPLOGSTRING(CSPINT, "CSPAudioStreams::AudioStreamsStarted" );
       
   380     iTimeout = KTimeoutInitial; 
       
   381     iTimer->CancelNotify();
       
   382     }
       
   383 
       
   384 // ---------------------------------------------------------------------------
       
   385 // Starts timer
       
   386 // ---------------------------------------------------------------------------
       
   387 //
       
   388 void CSPAudioStreams::StartTimer() 
       
   389     {
       
   390     CSPLOGSTRING(CSPINT, "CSPAudioStreams::StartTimer" );
       
   391     iTimer->NotifyAfter( iTimeout, *this );
       
   392     }
       
   393     
       
   394 // ---------------------------------------------------------------------------
       
   395 // Starts mic and speaker
       
   396 // ---------------------------------------------------------------------------
       
   397 //
       
   398 void CSPAudioStreams::StartMicAndSpeaker()
       
   399     {
       
   400     // if speaker and mic is active then activation does not
       
   401     // cause any actions.
       
   402     iSpeaker->Activate();
       
   403     iMic->Activate();   
       
   404     }
       
   405 
       
   406 // ---------------------------------------------------------------------------
       
   407 // Indicated if mic and speaker are started or starting up.
       
   408 // ---------------------------------------------------------------------------
       
   409 //    
       
   410 TBool CSPAudioStreams::IsMicAndSpeakerStarted()
       
   411     {
       
   412     TBool areStreamsActive( iSpeaker->IsActive() && iMic->IsActive() );
       
   413     TBool areStreamsActivating( iMic->IsActivationOngoing() ||  
       
   414         iSpeaker->IsActivationOngoing() || iTimer->IsNotifyOngoing() );
       
   415         
       
   416     return areStreamsActive || areStreamsActivating;
       
   417     }
       
   418 
       
   419 //  End of File