bluetoothengine/btmac/src/BTMonoCmdHandler/btmcvolume.cpp
changeset 0 f63038272f30
child 10 0707dd69d236
child 15 00f9ee97d895
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     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:  call status handling. 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "btmcphonestatus.h"
       
    21 #include "btmcvolume.h"
       
    22 #include <mmkeybearerscancodes.h> // for volume pubsub
       
    23 #include <mediakeysinternalpskeys.h>
       
    24 #include <MediaStreamPropertiesPSKeys.h> // for current volume pubsub
       
    25 #include <mmf/server/sounddevice.h>
       
    26 #include <e32math.h>
       
    27 
       
    28 #include "debug.h"
       
    29 
       
    30 
       
    31 const TInt KNoAudioStreaming = -1;
       
    32 const TInt KMaxHfVol = 15;
       
    33 const TInt KSpeakerVolumeSubscribeService = 30;
       
    34 const TInt KDefaultStep = 1000;
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CBtmcVolume::NewL
       
    38 // -----------------------------------------------------------------------------
       
    39 CBtmcVolume* CBtmcVolume::NewL(CBtmcPhoneStatus& aParent)
       
    40     {
       
    41     CBtmcVolume* self = new(ELeave) CBtmcVolume(aParent);
       
    42     CleanupStack::PushL(self);
       
    43     self->ConstructL();
       
    44     CleanupStack::Pop(self);
       
    45     return self;
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CBtmcVolume::~CBtmcVolume
       
    50 // -----------------------------------------------------------------------------
       
    51 CBtmcVolume::~CBtmcVolume()
       
    52     {
       
    53     TRACE_FUNC_ENTRY
       
    54     delete iActive;
       
    55     iVolLevelProperty.Close();
       
    56     iVolKeyEventProperty.Close();
       
    57     TRACE_FUNC_EXIT
       
    58     }
       
    59 
       
    60 void CBtmcVolume::SetSpeakerVolumeL(TInt aHfVol)
       
    61     {
       
    62     TRACE_FUNC
       
    63     TRACE_INFO((_L("phone vol %d, acc vol %d"), iPhnSpkrVol, iAccSpkrVol))
       
    64     iAccSpkrVol = HfToPhoneVolScale(aHfVol);
       
    65     if (iVolCtrlActivated)
       
    66         {
       
    67         TRACE_INFO((_L("to new vol %d"), iAccSpkrVol))
       
    68         TInt vol = GetNewPhoneVol();
       
    69         if (vol != KNoAudioStreaming)
       
    70             {
       
    71             TInt prevPhVol = iPhnSpkrVol;
       
    72             iPhnSpkrVol = vol;
       
    73             DoSetSpeakerVolL( prevPhVol );
       
    74             }
       
    75         }
       
    76     else
       
    77         {
       
    78         TRACE_INFO((_L("volume control inactive!")))
       
    79         }
       
    80     }
       
    81 
       
    82 void CBtmcVolume::SetMicrophoneVolumeL(TInt /*aHfVol*/)
       
    83     {
       
    84     return;
       
    85     }
       
    86 
       
    87 void CBtmcVolume::ActivateRemoteVolumeControl()
       
    88     {
       
    89     if (!iVolCtrlActivated)
       
    90         {
       
    91         TRACE_FUNC
       
    92 		iVolLevelProperty.Subscribe(iActive->iStatus);     
       
    93         iActive->GoActive();
       
    94 
       
    95         iVolCtrlActivated = ETrue;
       
    96         TInt vol = GetNewPhoneVol();
       
    97         TRACE_INFO((_L("current phone vol %d, acc vol %d"), vol, iAccSpkrVol))
       
    98         if (vol != KNoAudioStreaming)
       
    99             {
       
   100             iPhnSpkrVol = vol;
       
   101             if (iAccSpkrVol != vol)
       
   102                 {
       
   103         		TRAP_IGNORE(iParent.HandleSpeakerVolumeEventL(PhoneToHfVolScale(iPhnSpkrVol)));
       
   104         		iAccSpkrVol = iPhnSpkrVol;
       
   105                 }
       
   106             }
       
   107         }
       
   108     }
       
   109 
       
   110 void CBtmcVolume::DeActivateRemoteVolumeControl()
       
   111     {
       
   112     if (iVolCtrlActivated)
       
   113         {
       
   114         iVolCtrlActivated = EFalse;
       
   115         iActive->Cancel();
       
   116         }
       
   117     TRACE_FUNC
       
   118     }
       
   119 
       
   120 void CBtmcVolume::RequestCompletedL(CBtmcActive& aActive, TInt aErr)
       
   121     {
       
   122     TRACE_FUNC_ENTRY
       
   123     TRACE_INFO((_L("aErr %d, INTERNAL ACTION STATE %d"), aErr, iAction))
       
   124     switch (aActive.ServiceId())
       
   125         {
       
   126         case KSpeakerVolumeSubscribeService:
       
   127             {
       
   128             if(!aErr)
       
   129                 {
       
   130 				iVolLevelProperty.Subscribe(aActive.iStatus);
       
   131 				aActive.GoActive();
       
   132 
       
   133                 TInt vol = GetNewPhoneVol();
       
   134                 TRACE_INFO((_L("new phone vol %d, acc vol %d"), vol, iAccSpkrVol))
       
   135                 if (vol != KNoAudioStreaming)
       
   136                     {
       
   137                     TInt prevPhVol = iPhnSpkrVol;
       
   138                     iPhnSpkrVol = vol;
       
   139                     if (iAction == ESpeakerVolSubscribe && iPhnSpkrVol != iAccSpkrVol)
       
   140                         {
       
   141                         iParent.HandleSpeakerVolumeEventL(PhoneToHfVolScale(iPhnSpkrVol));
       
   142                         iAccSpkrVol = iPhnSpkrVol;
       
   143                         }
       
   144                     else if (iAction == ESpeakerVolSet)
       
   145                         {
       
   146                         DoSetSpeakerVolL( prevPhVol );
       
   147                         }
       
   148                     }
       
   149                 }
       
   150             break;
       
   151             }
       
   152         default:
       
   153             break;
       
   154         }
       
   155     TRACE_FUNC_EXIT
       
   156     }
       
   157 
       
   158 void CBtmcVolume::CancelRequest(TInt aServiceId)
       
   159     {
       
   160     switch (aServiceId)
       
   161         {
       
   162         case KSpeakerVolumeSubscribeService:
       
   163             {
       
   164             iVolLevelProperty.Cancel();
       
   165             break;
       
   166             }
       
   167         default:
       
   168             break;
       
   169         }
       
   170     }
       
   171 
       
   172 // -----------------------------------------------------------------------------
       
   173 // CBtmcVolume::CBtmcVolume
       
   174 // -----------------------------------------------------------------------------
       
   175 CBtmcVolume::CBtmcVolume(CBtmcPhoneStatus& aParent)
       
   176     : iParent(aParent), iAccSpkrVol(KNoAudioStreaming), iStep(KDefaultStep)
       
   177     {
       
   178     }
       
   179 
       
   180 // -----------------------------------------------------------------------------
       
   181 // CBtmcVolume::ConstructL
       
   182 // -----------------------------------------------------------------------------
       
   183 void CBtmcVolume::ConstructL()
       
   184     {
       
   185     TRACE_FUNC_ENTRY
       
   186     LEAVE_IF_ERROR(iVolLevelProperty.Attach(KPSUidMediaStreamProperties, KMediaStreamCurrentVolume))
       
   187     LEAVE_IF_ERROR(iVolKeyEventProperty.Attach(KPSUidMediaKeysEventNotifier, KMediaKeysAccessoryVolumeEvent))
       
   188     CMMFDevSound* devSound  = CMMFDevSound::NewL();
       
   189     iMaxSpkrVol = devSound->MaxVolume();
       
   190     TRACE_INFO((_L("Maximum volume is %d"), iMaxSpkrVol))
       
   191     delete devSound;
       
   192     iPhnSpkrVol = GetNewPhoneVol();
       
   193     iActive = CBtmcActive::NewL(*this, CActive::EPriorityStandard, KSpeakerVolumeSubscribeService);
       
   194     TRACE_FUNC_EXIT
       
   195     }
       
   196 
       
   197 // -----------------------------------------------------------------------------
       
   198 // CBtmcVolume::DoSetSpeakerVolL
       
   199 // -----------------------------------------------------------------------------
       
   200 //
       
   201 void CBtmcVolume::DoSetSpeakerVolL( TInt aPrevPhVol )
       
   202     {
       
   203     TRACE_FUNC_ENTRY     
       
   204     TInt volClick( 0 );
       
   205     TInt diff1;
       
   206     TInt diff2;
       
   207     
       
   208     if( ( aPrevPhVol >= iAccSpkrVol && iPhnSpkrVol <= iAccSpkrVol ) ||
       
   209         ( aPrevPhVol <= iAccSpkrVol && iPhnSpkrVol >= iAccSpkrVol ) )
       
   210         {
       
   211         diff1 = Abs( aPrevPhVol - iAccSpkrVol );
       
   212         diff2 = Abs( iPhnSpkrVol - iAccSpkrVol );
       
   213         
       
   214         if( diff2 >= diff1  )
       
   215             {
       
   216             // the previous phone volume is closer to current headset volume setting
       
   217             volClick = ( aPrevPhVol < iPhnSpkrVol ) ? KPSVolumeDownClicked : KPSVolumeUpClicked;
       
   218             }
       
   219         // otherwise we have roughly synchronized vol in both ends.
       
   220         }
       
   221     else if( iAccSpkrVol != iPhnSpkrVol )
       
   222         {
       
   223         TInt step = Abs( iPhnSpkrVol - aPrevPhVol );
       
   224         if( step )
       
   225             {
       
   226             // Only update step value if it is not equal to zero
       
   227             iStep = step;
       
   228             }
       
   229         diff1 = Abs( iPhnSpkrVol - iAccSpkrVol );
       
   230         if( iAccSpkrVol > iPhnSpkrVol )
       
   231             {
       
   232             // We are going upward
       
   233             diff2 = Abs( (iPhnSpkrVol + iStep) - iAccSpkrVol );
       
   234             }
       
   235         else
       
   236             {
       
   237             // We are going downward
       
   238             diff2 = Abs( (iPhnSpkrVol - iStep) - iAccSpkrVol );
       
   239             }
       
   240         if( diff2 <= diff1 )
       
   241             {
       
   242             volClick = (iAccSpkrVol > iPhnSpkrVol) ? KPSVolumeUpClicked : KPSVolumeDownClicked;
       
   243             }        
       
   244         }
       
   245      
       
   246     if( volClick )
       
   247         {
       
   248         iAction = ESpeakerVolSet;
       
   249         TInt err = iVolKeyEventProperty.Set( volClick );
       
   250         if( err )
       
   251             {
       
   252             iAction = ESpeakerVolSubscribe;
       
   253             TRACE_ERROR((_L("Set KMediaKeysVolumeKeyEvent err %d"), err));
       
   254             }
       
   255         }
       
   256     else
       
   257         {        
       
   258         iAction = ESpeakerVolSubscribe;
       
   259         }
       
   260     TRACE_FUNC_EXIT 
       
   261     }
       
   262 
       
   263 // -------------------------------------------------------------------------------
       
   264 // CBtmcVolume::HfToPhoneVolScale
       
   265 // Converts volume level scale used in AT command VGS (0-15) into one used in
       
   266 // DevSound Audio (0-MaxVol).
       
   267 // -------------------------------------------------------------------------------
       
   268 TInt CBtmcVolume::HfToPhoneVolScale(TInt aHfVol)
       
   269     {
       
   270  	TReal tgt;
       
   271  	TReal src(aHfVol);
       
   272  	TReal maxAG(iMaxSpkrVol);
       
   273  	TReal maxHF(KMaxHfVol);
       
   274  	TReal scale = maxAG/maxHF;
       
   275 	Math::Round( tgt, src*scale, 0);
       
   276 	TRACE_INFO((_L("HfToPhoneVolScale: HF %d is AG %d"), aHfVol, TInt(tgt)))
       
   277 	return TInt(tgt);
       
   278 
       
   279     }
       
   280 
       
   281 // -------------------------------------------------------------------------------
       
   282 // CBtmcVolume::PhoneToHfVolScale
       
   283 //
       
   284 // Converts volume level scale used in DevSound Audio (0-10) into one used in AT command
       
   285 // VGS (0-15).
       
   286 // -------------------------------------------------------------------------------
       
   287 TInt CBtmcVolume::PhoneToHfVolScale(TInt aPhoneVol)
       
   288     {
       
   289  	TReal tgt;
       
   290  	TReal src(aPhoneVol);
       
   291  	TReal maxAG(iMaxSpkrVol);
       
   292  	TReal maxHF(KMaxHfVol);
       
   293  	TReal scale = maxHF/maxAG;
       
   294 	Math::Round( tgt, src*scale, 0);
       
   295 	TRACE_INFO((_L("PhoneToHfVolScale: HF %d is AG %d"), TInt(tgt), aPhoneVol))
       
   296 	return TInt(tgt);
       
   297 
       
   298     }
       
   299 
       
   300 TInt CBtmcVolume::GetNewPhoneVol()
       
   301     {
       
   302     TInt vol = KNoAudioStreaming;
       
   303     iVolLevelProperty.Get(vol);
       
   304     TRACE_INFO((_L("phone vol P&S %d"), vol))
       
   305     return vol;
       
   306     }
       
   307 
       
   308 TInt CBtmcVolume::GetVolume()
       
   309 	{
       
   310     iPhnSpkrVol = GetNewPhoneVol();
       
   311 	if (iPhnSpkrVol != KNoAudioStreaming)
       
   312 	    return PhoneToHfVolScale(iPhnSpkrVol);
       
   313 	return iPhnSpkrVol;
       
   314 	}
       
   315 
       
   316 // End of file