os/mm/mmlibs/mmfw/tsrc/mmfunittest/DevSoundTest/a3fcistubextn/a3fcistubextn.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include "a3fcistubextn.h"
    17 #include <ecom/ecom.h>
    18 #include <ecom/implementationproxy.h>
    19 #include "a3fcistubextn.hrh"
    20 
    21 #include <a3f/maudiocontext.h>
    22 #include <a3f/maudiostream.h>
    23 #include <a3f/maudioprocessingunit.h>
    24 #include <a3f/maudiogaincontrol.h>
    25 #include <a3f/audioprocessingunittypeuids.h>
    26 
    27 //      
    28 #include "a3fbackdooraccess.h"
    29 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
    30 #include <mmf/common/mmfipcserver.h>
    31 #endif
    32 
    33 
    34 // __________________________________________________________________________
    35 // Client-side extension
    36 
    37 MDevSoundCIClientExtension* CA3fClientCiStubExtn::NewL()
    38 	{
    39 	CA3fClientCiStubExtn* self = new (ELeave) CA3fClientCiStubExtn;
    40 	return self;
    41 	}
    42 	
    43 CA3fClientCiStubExtn::~CA3fClientCiStubExtn()
    44 	{
    45 	delete iDummyBuffer;
    46 	REComSession::DestroyedImplementation(iKey);
    47 	}
    48 	
    49 TInt CA3fClientCiStubExtn::Setup(MCustomCommand& aCustomCommand)
    50 	{
    51 	iCommand = &aCustomCommand;
    52 	iDummyBuffer=KNullDesC().Alloc(); // dummy buffer simulates some allocs
    53 	if (!iDummyBuffer)
    54 		{
    55 		return KErrNoMemory;
    56 		}
    57 	return KErrNone;
    58 	}
    59 	
    60 TInt CA3fClientCiStubExtn::CustomInterfaceExtension(TUid aUid, TAny*& aInterface)
    61 	{
    62 	if (aUid==KUidTestSetVolIf)
    63 		{
    64 		MTestSetVolIf* interface = this;
    65 		aInterface = interface;
    66 		return KErrNone;
    67 		}
    68 	return KErrNotSupported;
    69 	}
    70 	
    71 void CA3fClientCiStubExtn::Release()
    72 	{
    73 	delete this;
    74 	}
    75 
    76 void CA3fClientCiStubExtn::PassDestructorKey(TUid aDestructorKey)
    77 	{
    78 	iKey = aDestructorKey;
    79 	}
    80 	
    81 TInt CA3fClientCiStubExtn::SetVol(TInt aVol)
    82 	{
    83 	// the key here is the destination package, which represets SetVol. Apart from value
    84 	// the rest of the parameters can be ignored
    85 	TPckgBuf<TInt> volumePkg (aVol);
    86 	TMMFMessageDestinationPckg setVolDest (TMMFMessageDestination(KUidTestSetVolIf, 0));
    87 	TInt result = iCommand->CustomCommandSync(setVolDest, ETestSetVolIfSetVolCommand, volumePkg, KNullDesC8);
    88 	return result;
    89 	}
    90 
    91 TInt CA3fClientCiStubExtn::Vol(TInt aMaxVol)
    92     {
    93     // again the key is is the destination package
    94     TPckgBuf<TInt> maxVolPkg (aMaxVol);
    95     TMMFMessageDestinationPckg setVolDest (TMMFMessageDestination(KUidTestSetVolIf, 0));
    96     TInt result = iCommand->CustomCommandSync(setVolDest, ETestSetVolIfVolCommand, maxVolPkg, KNullDesC8);
    97     return result;
    98     }
    99 
   100 // __________________________________________________________________________
   101 // Server-side extension
   102 
   103 MDevSoundCIServerExtension* CA3fServerCiStubExtn::NewL()
   104 	{
   105 	CA3fServerCiStubExtn* self = new (ELeave) CA3fServerCiStubExtn;
   106 	return self;
   107 	}
   108 	
   109 CA3fServerCiStubExtn::~CA3fServerCiStubExtn()
   110 	{
   111 	delete iDummyBuffer;
   112 	delete iSetVol;
   113 	REComSession::DestroyedImplementation(iKey);
   114 	}
   115 	
   116 TInt CA3fServerCiStubExtn::Setup(MCustomInterface& aCustomInterface)
   117 	{
   118 	iInterface = &aCustomInterface;
   119 	iDummyBuffer=KNullDesC().Alloc(); // dummy buffer simulates some allocs
   120 	if (!iDummyBuffer)
   121 		{
   122 		return KErrNoMemory;
   123 		}
   124 	return KErrNone;
   125 	}
   126 	
   127 TInt CA3fServerCiStubExtn::HandleMessageL(const RMmfIpcMessage& aMessage)
   128 	{
   129 	TMMFMessageDestinationPckg destinationPckg;
   130 	MmfMessageUtil::ReadL(aMessage, 0, destinationPckg);
   131 	if (destinationPckg().InterfaceId()==KUidTestSetVolIf)
   132 		{
   133 		CSetVol* volHandler = CSetVol::NewL(*iInterface);
   134 		CleanupStack::PushL(volHandler);
   135 		TInt result = volHandler->HandleMessageL(aMessage);
   136 		CleanupStack::Pop(volHandler);
   137 		return result;
   138 		}
   139 
   140 	return KErrNotSupported;
   141 	}
   142 	
   143 void CA3fServerCiStubExtn::Release()
   144 	{
   145 	delete this;
   146 	}
   147 
   148 void CA3fServerCiStubExtn::PassDestructorKey(TUid aDestructorKey)
   149 	{
   150 	iKey = aDestructorKey;
   151 	}
   152 	
   153 // CSetVol
   154 
   155 CSetVol* CSetVol::NewL(MCustomInterface& aCustomInterface)
   156 	{
   157 	CSetVol* self = new (ELeave) CSetVol(aCustomInterface);
   158 	CleanupStack::PushL(self);
   159 	self->ConstructL();
   160 	CleanupStack::Pop(self);
   161 	return self;
   162 	}
   163 	
   164 CSetVol::CSetVol(MCustomInterface& aCustomInterface):
   165 	iInterface(&aCustomInterface)
   166 	{
   167 	// do nothing
   168 	}
   169 		
   170 void CSetVol::ConstructL()
   171 	{
   172 	iWait = new (ELeave) CActiveSchedulerWait();
   173 	}
   174 	
   175 CSetVol::~CSetVol()
   176 	{
   177 	delete iWait;
   178 	}
   179 	
   180 TInt CSetVol::HandleMessageL(const RMmfIpcMessage& aMessage)
   181 	{
   182     switch (aMessage.Function())
   183         {
   184         case ETestSetVolIfSetVolCommand:
   185             {
   186             TPckgBuf<TInt> volPckg;
   187             MmfMessageUtil::ReadL(aMessage, 1, volPckg);
   188             UpdateA3fPointers(); // grab pointers to context, stream etc
   189             SetVolumeL(volPckg());
   190 
   191             User::LeaveIfError(iContext->RegisterAudioContextObserver(*this));
   192 
   193             TInt error = iContext->Commit();
   194             if (!error)
   195                 {
   196                 iError = KErrNone;
   197                 iWait->Start();
   198                 error = iError;
   199                 }
   200             (void) iContext->UnregisterAudioContextObserver(*this);
   201 
   202             aMessage.Complete(error);
   203             return KErrNone; // KErrNone says we've handled the message
   204             }
   205         case ETestSetVolIfVolCommand:
   206             {
   207             TPckgBuf<TInt> maxVolPckg;
   208             MmfMessageUtil::ReadL(aMessage, 1, maxVolPckg);
   209             UpdateA3fPointers(); // grab pointers to context, stream etc
   210             TInt result = VolumeL(maxVolPckg());
   211             aMessage.Complete(result);
   212             return KErrNone; // KErrNone says we've handled the message
   213             }
   214         default:
   215             return KErrArgument;
   216         }
   217 	}
   218 	
   219 void CSetVol::UpdateA3fPointers()
   220 	{
   221 	MA3FBackdoorAccessIf* backdoor = 
   222 		static_cast<MA3FBackdoorAccessIf*>(iInterface->CustomInterface(KA3FBackdoorAccessIfUid));
   223 	if (backdoor)
   224 		{
   225 		iContext = backdoor->AudioContext();
   226 		iStream = backdoor->AudioStream();
   227 		iGain = backdoor->ProcessingUnit(KUidAudioGainControl);
   228 		}
   229 	else
   230 		{
   231 		iContext = NULL;
   232 		iStream = NULL;
   233 		iGain = NULL;
   234 		}
   235 	}
   236 	
   237 void CSetVol::SetVolumeL(TInt aVolume)
   238 	{
   239 	RArray<TAudioChannelGain>	channelGains;
   240 	CleanupClosePushL(channelGains);
   241 	TAudioChannelGain left;
   242 	TAudioChannelGain right;	
   243 	left.iLocation = TAudioChannelGain::ELeft;
   244 	right.iLocation = TAudioChannelGain::ERight;
   245 	left.iGain = right.iGain = aVolume;
   246 	User::LeaveIfError(channelGains.Append(left)); // assumed element 0 in rest of code
   247 	User::LeaveIfError(channelGains.Append(right)); // assumed element 1 in rest of code
   248 
   249 	MAudioGainControl* gainControl = static_cast<MAudioGainControl*>(iGain->Interface(KUidAudioGainControl));
   250 	User::LeaveIfError(gainControl->SetGain(channelGains));	
   251 	CleanupStack::PopAndDestroy(&channelGains);
   252 	}
   253 
   254 TInt CSetVol::VolumeL(TInt aMaxVolume)
   255     {
   256     RArray<TAudioChannelGain>   channelGains;
   257     CleanupClosePushL(channelGains);
   258     TInt maxGain;
   259 
   260     MAudioGainControl* gainControl = static_cast<MAudioGainControl*>(iGain->Interface(KUidAudioGainControl));
   261     User::LeaveIfError(gainControl->GetGain(channelGains));
   262     User::LeaveIfError(gainControl->GetMaxGain(maxGain));
   263 
   264     TInt basicVolume = (channelGains[0].iGain + channelGains[1].iGain) / 2;
   265     TInt result = basicVolume * aMaxVolume / maxGain; // scale to 0 to maxVolume
   266 
   267     CleanupStack::PopAndDestroy(&channelGains);
   268     return result;
   269     }
   270 	
   271 void CSetVol::ContextEvent(TUid aEvent, TInt aError)
   272 	{
   273 	if (aEvent==KUidA3FContextUpdateComplete || aEvent==KUidA3FContextAbort)
   274 		{
   275 		// we are going to assume the SetGain() works and use the end of
   276 		// the ContextEvent. Really should observe the gain itself
   277 		if (aError)
   278 			{
   279 			iError = aError;
   280 			}
   281 		iWait->AsyncStop();
   282 		}
   283 	}
   284 		
   285 //
   286 // ImplementationTable
   287 //
   288 
   289 const TImplementationProxy ImplementationTable[] = 
   290 	{
   291 	IMPLEMENTATION_PROXY_ENTRY(KUidA3fClientCiStubExtn, CA3fClientCiStubExtn::NewL),
   292 	IMPLEMENTATION_PROXY_ENTRY(KUidA3fServerCiStubExtn, CA3fServerCiStubExtn::NewL),
   293 	};
   294 
   295 
   296 //
   297 // ImplementationGroupProxy
   298 //
   299 //
   300 
   301 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
   302 	{
   303 	aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
   304 
   305 	return ImplementationTable;
   306 	}
   307 
   308