devsound/devsoundpluginsupport/src/CustomInterfaces/CMMFDevSoundCIBitRate.cpp
changeset 29 eb1e5d7cc688
parent 26 c36d67f77f75
child 30 9707f1c07383
equal deleted inserted replaced
26:c36d67f77f75 29:eb1e5d7cc688
     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 #include "cmmfdevsoundcibitrateimplementationuid.hrh"
       
    20 
       
    21 #include <ecom/implementationproxy.h>
       
    22 #include <ecom/ecom.h>
       
    23 #include <s32mem.h>
       
    24 
       
    25 #include "cmmfdevsoundcibitrate.h"
       
    26 
       
    27 // __________________________________________________________________________
       
    28 // Implementation
       
    29 
       
    30 ////////////////////////////////////////// MUX /////////////////////
       
    31 
       
    32 TInt CMMFDevSoundCIBitRateMux::OpenInterface(TUid /*aInterfaceId*/)
       
    33 	{
       
    34 	// attempt to open the interface link with the
       
    35 	// remote slave device
       
    36 	iRemoteHandle = -1;
       
    37 	TUid slaveId = {KMmfUidCustomInterfaceBitRateDeMux};
       
    38 		
       
    39 	TInt handle = iUtility->OpenSlave(slaveId, KNullDesC8);
       
    40 	if (handle >= 0)
       
    41 		{
       
    42 		iRemoteHandle = handle;
       
    43 		}
       
    44 		
       
    45 	return iRemoteHandle;
       
    46 	}
       
    47 
       
    48 void CMMFDevSoundCIBitRateMux::Release()
       
    49 	{
       
    50 	// close the slave device if it exists
       
    51 	if (iRemoteHandle != -1)
       
    52 		{
       
    53 		// we assume the slave is closed correctly
       
    54 		iUtility->CloseSlave(iRemoteHandle);
       
    55 		}
       
    56 	
       
    57 	TUid key = iKey;
       
    58 	delete this;
       
    59 	
       
    60 	// tell ECom to destroy us
       
    61 	REComSession::DestroyedImplementation(key);
       
    62 	}
       
    63 	
       
    64 void CMMFDevSoundCIBitRateMux::PassDestructorKey(TUid aDestructorKey)
       
    65 	{
       
    66 	// store the destructor key
       
    67 	iKey = aDestructorKey;
       
    68 	}
       
    69 
       
    70 void CMMFDevSoundCIBitRateMux::CompleteConstructL(MMMFDevSoundCustomInterfaceMuxUtility* aCustomUtility)
       
    71 	{
       
    72 	// store a pointer to the utility
       
    73 	iUtility = aCustomUtility;
       
    74 	}
       
    75 	
       
    76 MMMFDevSoundCustomInterfaceMuxPlugin* CMMFDevSoundCIBitRateMux::NewL()
       
    77 	{
       
    78 	CMMFDevSoundCIBitRateMux* self = new (ELeave) CMMFDevSoundCIBitRateMux;
       
    79 	return self;
       
    80 	}
       
    81 	
       
    82 TAny* CMMFDevSoundCIBitRateMux::CustomInterface(TUid /*aInterfaceId*/)
       
    83 	{
       
    84 	MMMFDevSoundCustomInterfaceBitRate* interface = this;
       
    85 	return interface;
       
    86 	}
       
    87 	
       
    88 CMMFDevSoundCIBitRateMux::CMMFDevSoundCIBitRateMux() :
       
    89 	iRemoteHandle(-1)
       
    90 	{	
       
    91 	}
       
    92 
       
    93 CMMFDevSoundCIBitRateMux::~CMMFDevSoundCIBitRateMux()
       
    94 	{	
       
    95 	}
       
    96 
       
    97 // from MMMFDevSoundCustomInterfaceBitRate
       
    98 void CMMFDevSoundCIBitRateMux::GetSupportedBitRatesL(RArray<TInt>& aSupportedBitRates)
       
    99 	{
       
   100 	if (iRemoteHandle == -1)
       
   101 		{
       
   102 		User::Leave(KErrNotReady);
       
   103 		}
       
   104 	
       
   105 	// first clear out the array
       
   106 	aSupportedBitRates.Reset();
       
   107 	
       
   108 	// now fetch the count from the server
       
   109 	TInt count = -1;
       
   110 	count = iUtility->SendSlaveSyncCommand(iRemoteHandle, EMMFDevSoundCIBitRateGetSupportedBitRates, KNullDesC8);
       
   111 	
       
   112 	// if count is negative then the server side left with an error
       
   113 	if (count < 0)
       
   114 		{
       
   115 		User::Leave(count);
       
   116 		}
       
   117 	
       
   118 	// no point getting the data if the count is zero
       
   119 	if (count != 0)
       
   120 		{
       
   121 		// allocate a temporary buffer to hold the bitrates
       
   122 		HBufC8* buf = HBufC8::NewLC(count * sizeof(TInt32));
       
   123 		TPtr8 ptr = buf->Des();
       
   124 	
       
   125 		// fetch the bitrates - but send over the received count to be sure
       
   126 		TPckgBuf<TInt> countBuf(count);
       
   127 		User::LeaveIfError(iUtility->SendSlaveSyncCommandResult(
       
   128 											 iRemoteHandle, 
       
   129 											 EMMFDevSoundCIBitRateGetSupportedBitRatesArray,
       
   130 											 countBuf, ptr));
       
   131 	
       
   132 		// stream data into the pointer
       
   133 		RDesReadStream stream(ptr);
       
   134 		CleanupClosePushL(stream);
       
   135 				
       
   136 		TInt err = KErrNone;
       
   137 		for (TInt i = 0; i < count; i++)
       
   138 			{
       
   139 			// note we don't destroy array because we don't own it
       
   140 			// but we do reset it as it is incomplete
       
   141 			err = aSupportedBitRates.Append(stream.ReadInt32L());
       
   142 			if (err != KErrNone)
       
   143 				{
       
   144 				aSupportedBitRates.Reset();
       
   145 				User::Leave(KErrCorrupt);
       
   146 				}
       
   147 			}
       
   148 		
       
   149 		CleanupStack::PopAndDestroy(2, buf);// stream, buf
       
   150 		}
       
   151 	}
       
   152 	
       
   153 TInt CMMFDevSoundCIBitRateMux::BitRateL()
       
   154 	{
       
   155 	if (iRemoteHandle == -1)
       
   156 		{
       
   157 		User::Leave(KErrNotReady);
       
   158 		}
       
   159 	
       
   160 	// send EMMFDevSoundCIBitRateBitRate command to slave
       
   161 	TInt bitrate = 0;
       
   162 	bitrate = iUtility->SendSlaveSyncCommand(iRemoteHandle, EMMFDevSoundCIBitRateBitRate, KNullDesC8);
       
   163 	
       
   164 	// if bitrate is negative then remote side left with an error
       
   165 	if (bitrate < 0)
       
   166 		{
       
   167 		User::Leave(bitrate);
       
   168 		}
       
   169 	
       
   170 	return bitrate;
       
   171 	}
       
   172 	
       
   173 void CMMFDevSoundCIBitRateMux::SetBitRateL(TInt aBitRate)
       
   174 	{
       
   175 	if (iRemoteHandle == -1)
       
   176 		{
       
   177 		User::Leave(KErrNotReady);
       
   178 		}
       
   179 	
       
   180 	// send the bitrate in the sync command
       
   181 	TPckgBuf<TInt> bitBuffer(aBitRate);
       
   182 	
       
   183 	// any return code other than zero is an error
       
   184 	User::LeaveIfError(iUtility->SendSlaveSyncCommand(iRemoteHandle, EMMFDevSoundCIBitRateSetBitRate, bitBuffer));
       
   185 	}
       
   186 
       
   187 /////////////////////////////////////// DEMUX //////////////////////	
       
   188 	
       
   189 
       
   190 TInt CMMFDevSoundCIBitRateDeMux::OpenInterface(TUid /*aInterfaceId*/)
       
   191 	{
       
   192 	return KErrNone;
       
   193 	}
       
   194 	
       
   195 void CMMFDevSoundCIBitRateDeMux::Release()
       
   196 	{
       
   197 	TUid key = iKey;
       
   198 	
       
   199 	delete this;
       
   200 	
       
   201 	// tell ECom to destroy us
       
   202 	REComSession::DestroyedImplementation(key);
       
   203 	}
       
   204 	
       
   205 void CMMFDevSoundCIBitRateDeMux::PassDestructorKey(TUid aDestructorKey)
       
   206 	{
       
   207 	// store the destructor key
       
   208 	iKey = aDestructorKey;
       
   209 	}
       
   210 	
       
   211 void CMMFDevSoundCIBitRateDeMux::SetInterfaceTarget(MMMFDevSoundCustomInterfaceTarget* aTarget)
       
   212 	{
       
   213 	iTarget = aTarget;
       
   214 	}
       
   215 	
       
   216 void CMMFDevSoundCIBitRateDeMux::CompleteConstructL(MMMFDevSoundCustomInterfaceDeMuxUtility* aCustomUtility)
       
   217 	{
       
   218 	// store a pointer to the utility
       
   219 	iUtility = aCustomUtility;
       
   220 	}
       
   221 
       
   222 void CMMFDevSoundCIBitRateDeMux::RefreshL()
       
   223 	{
       
   224 	// refetch the bitrate custom interface if we already have a target
       
   225 	if (iTarget)
       
   226 		{
       
   227 		MMMFDevSoundCustomInterfaceBitRate* ptr = NULL;
       
   228 		ptr = static_cast <MMMFDevSoundCustomInterfaceBitRate*> (iTarget->CustomInterface(KUidCustomInterfaceDevSoundBitRate));
       
   229 	
       
   230 		if (!ptr)
       
   231 			{
       
   232 			iBitRateInterface = NULL;
       
   233 			User::Leave(KErrNotSupported);
       
   234 			}
       
   235 		else
       
   236 			{
       
   237 			iBitRateInterface = ptr;
       
   238 			}	
       
   239 		}
       
   240 	}
       
   241 
       
   242 
       
   243 MMMFDevSoundCustomInterfaceDeMuxPlugin* CMMFDevSoundCIBitRateDeMux::NewL()
       
   244 	{
       
   245 	CMMFDevSoundCIBitRateDeMux* self = new (ELeave) CMMFDevSoundCIBitRateDeMux;
       
   246 	return self;
       
   247 	}
       
   248 	
       
   249 CMMFDevSoundCIBitRateDeMux::CMMFDevSoundCIBitRateDeMux()
       
   250 	{
       
   251 	
       
   252 	}
       
   253 
       
   254 CMMFDevSoundCIBitRateDeMux::~CMMFDevSoundCIBitRateDeMux()
       
   255 	{
       
   256 	iBitRateArray.Reset();
       
   257 	iBitRateArray.Close();
       
   258 	}
       
   259 
       
   260 
       
   261 TInt CMMFDevSoundCIBitRateDeMux::DoOpenSlaveL(TUid /*aInterface*/, const TDesC8& /*aPackageBuf*/)
       
   262 	{
       
   263 	// fetch the bitrate custom interface
       
   264 	MMMFDevSoundCustomInterfaceBitRate* ptr = NULL;
       
   265 	ptr = static_cast <MMMFDevSoundCustomInterfaceBitRate*> (iTarget->CustomInterface(KUidCustomInterfaceDevSoundBitRate));
       
   266 	
       
   267 	if (!ptr)
       
   268 		{
       
   269 		iBitRateInterface = NULL;
       
   270 		User::Leave(KErrNotSupported);
       
   271 		}
       
   272 	else
       
   273 		{
       
   274 		iBitRateInterface = ptr;
       
   275 		}
       
   276 	return KErrNone;
       
   277 	}
       
   278 	
       
   279 void CMMFDevSoundCIBitRateDeMux::DoCloseSlaveL(TInt /*aHandle*/)
       
   280 	{
       
   281 	// nothing to do
       
   282 	}
       
   283 
       
   284 // original RMessage is supplied so that remote demux plugin can extract necessary details
       
   285 // using DeMux utility
       
   286 TInt CMMFDevSoundCIBitRateDeMux::DoSendSlaveSyncCommandL(const RMmfIpcMessage& aMessage)
       
   287 	{
       
   288 	TMMFDevSoundCIMessageData data;
       
   289 	
       
   290 	// decode message
       
   291 	iUtility->GetSyncMessageDataL(aMessage, data);
       
   292 	TInt retVal = -1;
       
   293 	
       
   294 	switch (data.iCommand)
       
   295 		{
       
   296 		case EMMFDevSoundCIBitRateBitRate:
       
   297 			{
       
   298 			retVal = DoBitRateL();
       
   299 			break;
       
   300 			}
       
   301 		case EMMFDevSoundCIBitRateSetBitRate:
       
   302 			{
       
   303 			// we know that offset 2 contains a TInt
       
   304 			TPckgBuf<TInt> bitBuffer;
       
   305 			iUtility->ReadFromInputDesL(aMessage, &bitBuffer);
       
   306 			
       
   307 			DoSetBitRateL(bitBuffer());
       
   308 			retVal = KErrNone;
       
   309 			break;
       
   310 			}
       
   311 		case EMMFDevSoundCIBitRateGetSupportedBitRates:
       
   312 			{
       
   313 			// reset the current bitrate array
       
   314 			iBitRateArray.Reset();
       
   315 			DoGetSupportedBitRatesL(iBitRateArray);
       
   316 			
       
   317 			// send back the array count
       
   318 			TInt count = iBitRateArray.Count();
       
   319 			retVal = count;
       
   320 			break;
       
   321 			}
       
   322 		default:
       
   323 			{
       
   324 			User::Leave(KErrNotSupported);
       
   325 			}
       
   326 		}
       
   327 		
       
   328 	return retVal;
       
   329 	}
       
   330 	
       
   331 TInt CMMFDevSoundCIBitRateDeMux::DoSendSlaveSyncCommandResultL(const RMmfIpcMessage& aMessage)
       
   332 	{
       
   333 	TMMFDevSoundCIMessageData data;
       
   334 	
       
   335 	// decode message
       
   336 	iUtility->GetSyncMessageDataL(aMessage, data);
       
   337 	
       
   338 	switch (data.iCommand)
       
   339 		{
       
   340 		case EMMFDevSoundCIBitRateGetSupportedBitRatesArray:
       
   341 			{
       
   342 			DoCopyBitRateBufferToClientL(aMessage);		
       
   343 			break;
       
   344 			}
       
   345 		default:
       
   346 			{
       
   347 			User::Leave(KErrNotSupported);
       
   348 			}
       
   349 		}
       
   350 	return KErrNone;
       
   351 	}
       
   352 	
       
   353 void CMMFDevSoundCIBitRateDeMux::DoSendSlaveAsyncCommandL(const RMmfIpcMessage& /*aMessage*/)
       
   354 	{
       
   355 	// not used in this interface
       
   356 	}
       
   357 	
       
   358 void CMMFDevSoundCIBitRateDeMux::DoSendSlaveAsyncCommandResultL(const RMmfIpcMessage& /*aMessage*/)
       
   359 	{
       
   360 	// not used in this interface
       
   361 	}
       
   362 
       
   363 // bitrate custom interface implementation
       
   364 void CMMFDevSoundCIBitRateDeMux::DoGetSupportedBitRatesL(RArray<TInt>& aSupportedBitRates)
       
   365 	{
       
   366 	if (!iBitRateInterface)
       
   367 		{
       
   368 		User::Leave(KErrNotReady);
       
   369 		}
       
   370 		
       
   371 	iBitRateInterface->GetSupportedBitRatesL(aSupportedBitRates);
       
   372 	}
       
   373 
       
   374 
       
   375 void CMMFDevSoundCIBitRateDeMux::DoCopyBitRateBufferToClientL(const RMmfIpcMessage& aMessage)
       
   376 	{
       
   377 	if (!iBitRateInterface)
       
   378 		{
       
   379 		User::Leave(KErrNotReady);
       
   380 		}
       
   381 		
       
   382 	// check our count is the same as the client's
       
   383 	TPckgBuf<TInt> countBuffer;
       
   384 	iUtility->ReadFromInputDesL(aMessage, &countBuffer);
       
   385 
       
   386 	TInt count = countBuffer();
       
   387 	if (count != iBitRateArray.Count())
       
   388 		{
       
   389 		User::Leave(KErrCorrupt);
       
   390 		}
       
   391 			
       
   392 	// send back the array - the client has the count already
       
   393 	const TInt KBufExpandSize8 = 8; //two TInt's
       
   394 	CBufFlat* dataCopyBuffer = CBufFlat::NewL(KBufExpandSize8);
       
   395 	CleanupStack::PushL(dataCopyBuffer);
       
   396 	RBufWriteStream stream;
       
   397 	stream.Open(*dataCopyBuffer);
       
   398 	CleanupClosePushL(stream);
       
   399 	
       
   400 	for (TInt i = 0; i < count; i++)
       
   401 		{
       
   402 		stream.WriteInt32L(iBitRateArray[i]);
       
   403 		}
       
   404 	
       
   405 	// write the data to the supplied descriptor buffer
       
   406 	TPtr8 ptrBuf = dataCopyBuffer->Ptr(0);
       
   407 	iUtility->WriteToOutputDesL(aMessage, ptrBuf);
       
   408 	stream.Close();
       
   409 
       
   410 	CleanupStack::PopAndDestroy(2); // iDataCopyBuffer, stream
       
   411 	}
       
   412 
       
   413 
       
   414 TInt CMMFDevSoundCIBitRateDeMux::DoBitRateL()
       
   415 	{
       
   416 	if (!iBitRateInterface)
       
   417 		{
       
   418 		User::Leave(KErrNotReady);
       
   419 		}
       
   420 	return iBitRateInterface->BitRateL();
       
   421 	}
       
   422 
       
   423 void CMMFDevSoundCIBitRateDeMux::DoSetBitRateL(TInt aBitRate)
       
   424 	{
       
   425 	if (!iBitRateInterface)
       
   426 		{
       
   427 		User::Leave(KErrNotReady);
       
   428 		}
       
   429 			
       
   430 	// set the bitrate		
       
   431 	iBitRateInterface->SetBitRateL(aBitRate);
       
   432 	}
       
   433 
       
   434 
       
   435 
       
   436 //
       
   437 // ImplementationTable
       
   438 //
       
   439 
       
   440 const TImplementationProxy ImplementationTable[] = 
       
   441 	{
       
   442 	IMPLEMENTATION_PROXY_ENTRY(KMmfUidCustomInterfaceBitRateMux,	CMMFDevSoundCIBitRateMux::NewL),
       
   443 	IMPLEMENTATION_PROXY_ENTRY(KMmfUidCustomInterfaceBitRateDeMux,	CMMFDevSoundCIBitRateDeMux::NewL),
       
   444 	};
       
   445 
       
   446 
       
   447 //
       
   448 // ImplementationGroupProxy
       
   449 //
       
   450 ////////////////////////////////////////////////////////////////////////////////
       
   451 
       
   452 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
       
   453 	{
       
   454 	aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
       
   455 
       
   456 	return ImplementationTable;
       
   457 	}
       
   458 
       
   459