mmfenh/enhancedmediaclient/Client/src/Components/AudioEffects/RoomLevelEffectImpl.cpp
changeset 0 71ca22bcf22a
equal deleted inserted replaced
-1:000000000000 0:71ca22bcf22a
       
     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:  Project file for EnhancedMediaClient Utility
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "RoomLevelEffectImpl.h"
       
    20 #include <RoomLevelBase.h>
       
    21 #include <ControlObserver.h>
       
    22 #include "ReverbEffectImpl.h"
       
    23 
       
    24 #ifdef _DEBUG
       
    25 #include <e32debug.h>
       
    26 #define _U(s) _L(s)
       
    27 #define RET_ERR_IF_ERR(err) if (err!=KErrNone) { \
       
    28     RDebug::Print(_L("RET_ERR_IF_ERR:Line[%d]Err[%d]"), __LINE__, err); \
       
    29     RDebug::Print(_U( __FILE__ )); \
       
    30     return err; }
       
    31 #else
       
    32 #define RET_ERR_IF_ERR(err) if (err!=KErrNone) return err
       
    33 #endif // _DEBUG
       
    34 
       
    35 using namespace multimedia;
       
    36 
       
    37 CRoomLevelEffect::CRoomLevelEffect()
       
    38     {
       
    39     // No Impl
       
    40     }
       
    41 
       
    42 CRoomLevelEffect::~CRoomLevelEffect()
       
    43     {
       
    44 	delete iPrevRoomLevelProxy;
       
    45 	delete  iRoomLevelProxy;
       
    46 	iObservers.Close();
       
    47     }
       
    48 
       
    49 TInt CRoomLevelEffect::PostConstructor()
       
    50     {
       
    51     TRAPD( status, CEffectControlBase::ConstructL(KUidRoomLevelEffect) );
       
    52     return status;
       
    53     }
       
    54 
       
    55 // From MControl begins
       
    56 TInt CRoomLevelEffect::AddObserver( MControlObserver& aObserver )
       
    57     {
       
    58     return iObservers.Append(&aObserver);
       
    59     }
       
    60 
       
    61 TInt CRoomLevelEffect::RemoveObserver( MControlObserver& aObserver )
       
    62     {
       
    63 	TInt index = iObservers.Find(&aObserver);
       
    64 	if( index != KErrNotFound )
       
    65 		{
       
    66 		iObservers.Remove(index);
       
    67 		}
       
    68 	return index;
       
    69     }
       
    70 
       
    71 TUid CRoomLevelEffect::Type()
       
    72     {
       
    73     return KRoomLevelEffectControl;
       
    74     }
       
    75 
       
    76 TControlType CRoomLevelEffect::ControlType()
       
    77     {
       
    78 	return EEffectControl;
       
    79     }
       
    80 
       
    81 // From MControl ends
       
    82 
       
    83 // From MEffectControl begins
       
    84 TInt CRoomLevelEffect::Apply()
       
    85     {
       
    86     return DoApply();
       
    87     }
       
    88 
       
    89 // From MEffectControl ends
       
    90 
       
    91 TInt CRoomLevelEffect::DoApply()
       
    92     {
       
    93     TInt error(KErrNone);    
       
    94 	if(iRoomLevelProxy)
       
    95 		{
       
    96 		TRAP(error,iRoomLevelProxy->ApplyL());	
       
    97 		}	
       
    98 	else
       
    99 		{
       
   100 		error = KErrNotReady;	
       
   101 		}			
       
   102     return error;    
       
   103     }
       
   104 
       
   105 // From MRoomLevelEffect begins
       
   106 /**
       
   107 * Gets the RoomLevel current level in mB
       
   108 * @since 5.0
       
   109 * @return RoomLevel current level
       
   110 */
       
   111 TInt CRoomLevelEffect::Level(TInt& aLevel)
       
   112 	{
       
   113 	TInt status(KErrNone);
       
   114 	if(iRoomLevelProxy)
       
   115 		{
       
   116 		aLevel = iRoomLevelProxy->Level();	
       
   117 		}	
       
   118 	else
       
   119 		{
       
   120 		status = KErrNotReady;	
       
   121 		}			
       
   122 	return status;						
       
   123 	}
       
   124 
       
   125 /**
       
   126 * Gets the RoomLevel current level maximum and minimum in mB
       
   127 * @since 5.0
       
   128 * @param aMin Minimum current level
       
   129 * @param aMax Maximum current level
       
   130 * @return -
       
   131 */
       
   132 TInt CRoomLevelEffect::LevelRange( TInt& aMin, TInt& aMax )
       
   133 	{
       
   134 	TInt status(KErrNone);
       
   135 	if(iRoomLevelProxy)
       
   136 		{
       
   137 		TInt32 min,max;	
       
   138 		iRoomLevelProxy->LevelRange(min,max);
       
   139 		aMin = min;
       
   140 		aMax = max;	
       
   141 		}	
       
   142 	else
       
   143 		{
       
   144 		status = KErrNotReady;	
       
   145 		}			
       
   146 	return status;						
       
   147 	}
       
   148 
       
   149 /**
       
   150 * Sets the RoomLevel level, it will leave if aRoomLevel is not within range of Min and Max
       
   151 * @since 5.0
       
   152 * @param aRoomLevelLevel The RoomLevel level in mB
       
   153 */
       
   154 TInt CRoomLevelEffect::SetRoomLevel( TInt aRoomLevel )
       
   155 	{
       
   156 	TInt status(KErrNone);
       
   157 	if(iRoomLevelProxy)
       
   158 		{
       
   159 		TRAP(status,iRoomLevelProxy->SetRoomLevelL(aRoomLevel));	
       
   160 		}	
       
   161 	else
       
   162 		{
       
   163 		status = KErrNotReady;	
       
   164 		}			
       
   165 	return status;				
       
   166 	}
       
   167 
       
   168 // From MRoomLevelEffect ends
       
   169 
       
   170 // From MAudioEffectControl
       
   171 /**
       
   172 * Disable the effect
       
   173 * @since 5.0
       
   174 */
       
   175 TInt CRoomLevelEffect::Disable()
       
   176 	{
       
   177 	TInt status(KErrNone);
       
   178 	if(iRoomLevelProxy)
       
   179 		{
       
   180 		TRAP(status,iRoomLevelProxy->DisableL());	
       
   181 		}	
       
   182 	else
       
   183 		{
       
   184 		status = KErrNotReady;	
       
   185 		}			
       
   186 	return status;				
       
   187 	}
       
   188 
       
   189 /**
       
   190 * Enable the effect
       
   191 * @since 5.0
       
   192 */
       
   193 TInt CRoomLevelEffect::Enable()
       
   194 	{
       
   195 	TInt status(KErrNone);
       
   196 	RDebug::Print(_L("CRoomLevelEffect::Enable() [%x]"),iRoomLevelProxy);
       
   197 	if(iRoomLevelProxy)
       
   198 		{
       
   199 		TRAP(status,iRoomLevelProxy->EnableL());	
       
   200 		}	
       
   201 	else
       
   202 		{
       
   203 		status = KErrNotReady;	
       
   204 		}			
       
   205 	return status;					
       
   206 	}
       
   207 
       
   208 /**
       
   209 * Enforce the effect.
       
   210 * @since 5.0
       
   211 * @param aEnforced Indicate the effect is to be enforced or not. ETrue = Enforced.
       
   212 */
       
   213 TInt CRoomLevelEffect::Enforce( TBool &aEnforced )
       
   214 	{
       
   215 	TInt status(KErrNone);
       
   216 	if(iRoomLevelProxy)
       
   217 		{
       
   218 		TRAP(status,iRoomLevelProxy->EnforceL(aEnforced));	
       
   219 		}	
       
   220 	else
       
   221 		{
       
   222 		status = KErrNotReady;	
       
   223 		}					
       
   224 	return status;		
       
   225 	}
       
   226 
       
   227 /**
       
   228 * Check if this effect object currently has update rights.
       
   229 * A client can lose update rights in some hardware platforms where there are a limited
       
   230 * number of instances of an effect that can exist at the same time. When an effect instance
       
   231 * has lost update rights the user can still change settings, but any calls to Apply the
       
   232 * settings will be deferred until update rights are regained.
       
   233 * @since 5.0
       
   234 * @return ETrue if this object currently has rights to update the settings of this effect,
       
   235 *         EFalse otherwise.
       
   236 */
       
   237 TInt CRoomLevelEffect::HaveUpdateRights(TBool &aHaveUpdateRights)
       
   238 	{
       
   239 	TInt status(KErrNone);
       
   240 	if(iRoomLevelProxy)
       
   241 		{
       
   242 		aHaveUpdateRights = iRoomLevelProxy->HaveUpdateRights();	
       
   243 		}	
       
   244 	else
       
   245 		{
       
   246 		status = KErrNotReady;	
       
   247 		}			
       
   248 	return status;	
       
   249 	}
       
   250 
       
   251 /**
       
   252 * Check if the effect is enabled
       
   253 * @since 5.0
       
   254 * @return ETrue if the effect is enabled, EFalse if the effect is disabled.
       
   255 */
       
   256 TInt CRoomLevelEffect::IsEnabled(TBool &aEnabled)
       
   257 	{
       
   258 	TInt status(KErrNone);
       
   259 	if(iRoomLevelProxy)
       
   260 		{
       
   261 		aEnabled = iRoomLevelProxy->IsEnabled();	
       
   262 		}	
       
   263 	else
       
   264 		{
       
   265 		status = KErrNotReady;	
       
   266 		}			
       
   267 	return status;			
       
   268 	}
       
   269 
       
   270 /**
       
   271 * Check if the effect is enforced.
       
   272 * @since 5.0
       
   273 * @return ETrue if the effect is enforced, EFalse if the effect isn ot enforced.
       
   274 */
       
   275 TInt CRoomLevelEffect::IsEnforced(TBool &aEnforced)
       
   276 	{
       
   277 	TInt status(KErrNone);
       
   278 	if(iRoomLevelProxy)
       
   279 		{
       
   280 		aEnforced = iRoomLevelProxy->IsEnforced();	
       
   281 		}	
       
   282 	else
       
   283 		{
       
   284 		status = KErrNotReady;	
       
   285 		}			
       
   286 	return status;			
       
   287 	}
       
   288 
       
   289 /*
       
   290 * Get the unique identifier of the audio effect
       
   291 * @since 5.0
       
   292 * @return Unique identifier of the audio effect object.
       
   293 */
       
   294 TInt CRoomLevelEffect::Uid(TUid &aUid)
       
   295 	{
       
   296 	TInt status(KErrNone);
       
   297 	if(iRoomLevelProxy)
       
   298 		{
       
   299 		aUid = iRoomLevelProxy->Uid();	
       
   300 		}	
       
   301 	else
       
   302 		{
       
   303 		status = KErrNotReady;	
       
   304 		}			
       
   305 	return status;			
       
   306 	}
       
   307 // From MAudioEffectControl Ends
       
   308 
       
   309 
       
   310 // From CEffectControlBase begins
       
   311 void CRoomLevelEffect::Event( TEffectControlEvent aEvent )
       
   312     {
       
   313 	TInt status(KErrNone);    	
       
   314     // Controller Loaded with ECIBuilderCreated
       
   315     if ( aEvent == ECIBuilderCreated )
       
   316         {
       
   317     	//RDebug::Print(_L("Deleting Proxy"));	
       
   318 		status = DeleteEffectProxy();
       
   319 		//RDebug::Print(_L("Deleting Proxy [%d]"),status);	
       
   320 		status = CreateEffectProxy();	
       
   321 		//RDebug::Print(_L("Creating Proxy [%d]"),status);	
       
   322 		if(status == KErrNone)
       
   323 			{
       
   324 			SavePreviousSettings();	
       
   325 			}
       
   326 		else
       
   327 			{
       
   328 			for ( TInt i = 0; i < iObservers.Count(); i++ )
       
   329 				{
       
   330 				iObservers[i]->Event(this,MAudioEffectControl::KDisabled,NULL);
       
   331 				} 		
       
   332 			}	
       
   333         }
       
   334     else if ( aEvent == EMessageHandlerDeleted )
       
   335         {
       
   336         if(status == KErrNone)
       
   337         	{
       
   338         	DeleteEffectProxy();	
       
   339         	}
       
   340         }
       
   341     }
       
   342 
       
   343 TInt CRoomLevelEffect::AttachReverb( MReverbControl& aReverb )
       
   344     {
       
   345     TInt status(KErrNone);
       
   346     if ( iCReverbEffect )
       
   347         {
       
   348         status = KErrAlreadyExists;
       
   349         }
       
   350     RET_ERR_IF_ERR( status );
       
   351 
       
   352     if ( aReverb.Type() != KReverbEffectControl )
       
   353         {
       
   354         status = KErrArgument;
       
   355         }
       
   356     RET_ERR_IF_ERR( status );
       
   357 
       
   358     iCReverbEffect = dynamic_cast<CReverbEffect*>(&aReverb);
       
   359     status = KErrNone;
       
   360     return status;
       
   361     }
       
   362 
       
   363 TInt CRoomLevelEffect::DetachReverb( MReverbControl& aReverb )
       
   364     {
       
   365     TInt status(KErrNone);
       
   366     if ( aReverb.Type() != KReverbEffectControl )
       
   367         {
       
   368         status = KErrArgument;
       
   369         }
       
   370     RET_ERR_IF_ERR( status );
       
   371 
       
   372     status = KErrNotFound;
       
   373     iCReverbEffect = dynamic_cast<CReverbEffect*>(&aReverb);
       
   374     if ( iCReverbEffect == dynamic_cast<CReverbEffect*>(&aReverb) )
       
   375         {
       
   376         status = KErrNone;
       
   377         
       
   378         // Make sure CRoomLevelEffect::AttachReverb is called before proxy gets created.
       
   379         if ( !iCReverbEffect )
       
   380             {
       
   381             status = KErrNotReady;  
       
   382             }
       
   383         RET_ERR_IF_ERR( status );
       
   384 
       
   385         // Detach reverb proxy here
       
   386         CEnvironmentalReverb* aReverb(NULL);
       
   387         status = iCReverbEffect->GetCEnvironmentalReverb(aReverb);
       
   388         RET_ERR_IF_ERR( status );
       
   389 
       
   390         status = iRoomLevelProxy->DettachReverb(*aReverb);
       
   391         RET_ERR_IF_ERR( status );
       
   392 
       
   393         iCReverbEffect = NULL;
       
   394         }
       
   395     return status;
       
   396     }
       
   397 
       
   398 // From CEffectControlBase ends
       
   399 
       
   400 TInt CRoomLevelEffect::CreateEffectProxy()
       
   401 	{
       
   402 	TInt status(KErrNone);
       
   403 	status = GetMessageHandle(iMsgHndlrHandlePckg);
       
   404     RET_ERR_IF_ERR( status );
       
   405 
       
   406     iCustomCommand = GetCustomCommand();		
       
   407 	if(!iCustomCommand)
       
   408 		{
       
   409 		status = KErrNotReady;	
       
   410 		}
       
   411     RET_ERR_IF_ERR( status );
       
   412     
       
   413     // Make sure CRoomLevelEffect::AttachReverb is called before proxy gets created.
       
   414     if ( !iCReverbEffect )
       
   415         {
       
   416         status = KErrNotReady;  
       
   417         }
       
   418     RET_ERR_IF_ERR( status );
       
   419 
       
   420 	TRAP(status,iRoomLevelProxy = CRoomLevelProxy::NewL(iMsgHndlrHandlePckg, *iCustomCommand, NULL));
       
   421     RET_ERR_IF_ERR( status );
       
   422 
       
   423     // Attach reverb proxy here
       
   424     CEnvironmentalReverb* aReverb(NULL);
       
   425     status = iCReverbEffect->GetCEnvironmentalReverb(aReverb);
       
   426     RET_ERR_IF_ERR( status );
       
   427 
       
   428     status = iRoomLevelProxy->AttachReverb(*aReverb);
       
   429     RET_ERR_IF_ERR( status );
       
   430     
       
   431     TRAP(status,iRoomLevelProxy->RegisterObserverL(*this));	
       
   432 
       
   433 	return status;
       
   434 	}
       
   435 
       
   436 TInt CRoomLevelEffect::DeleteEffectProxy()
       
   437 	{
       
   438 	TInt status(KErrNone);	
       
   439 	if(iRoomLevelProxy)
       
   440 		{
       
   441 		iPrevRoomLevelProxy = iRoomLevelProxy;	
       
   442 		}
       
   443 	iRoomLevelProxy = NULL;	
       
   444 	return status;
       
   445 	}
       
   446 
       
   447 TInt CRoomLevelEffect::SavePreviousSettings()
       
   448 	{
       
   449 	TInt status(KErrNone);	
       
   450 	if(iPrevRoomLevelProxy)
       
   451 		{
       
   452 		TBool enforce = iPrevRoomLevelProxy->IsEnforced();
       
   453 		iRoomLevelProxy->EnforceL(enforce);	
       
   454 
       
   455 		TBool enable = iPrevRoomLevelProxy->IsEnabled();
       
   456 		if(enable)
       
   457 			{
       
   458 			iRoomLevelProxy->EnableL();	
       
   459 			}
       
   460 
       
   461 		delete iPrevRoomLevelProxy;
       
   462 		iPrevRoomLevelProxy = NULL;
       
   463 		}				
       
   464 	return status;	
       
   465 	}
       
   466 
       
   467  void CRoomLevelEffect::EffectChanged( const CAudioEffect* /*aObservedEffect*/, TUint8 aEvent )
       
   468  	{
       
   469 	for ( TInt i = 0; i < iObservers.Count(); i++ )
       
   470 		{
       
   471 		iObservers[i]->Event(this,aEvent,NULL);
       
   472 		} 		
       
   473  	}
       
   474 
       
   475 // End of file