os/mm/mmhais/refacladapt/src/tonehwdevice/tonehwdevice.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // tonehwdevice.cpp
     2 // Copyright (c) 2006-2009 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 FILES
    20 #include <mmf/server/mmfhwdevice.h>
    21 #include "tonehwdevice.hrh" //for KUidToneHwDevice
    22 #include <ecom/implementationproxy.h> // For making it ECom plugin
    23 #include "tonehwdevice.h"
    24 
    25 
    26 // EXTERNAL DATA STRUCTURES
    27 
    28 // EXTERNAL FUNCTION PROTOTYPES   
    29 
    30 // CONSTANTS
    31 const TImplementationProxy ImplementationTable[] =
    32 	{
    33 	IMPLEMENTATION_PROXY_ENTRY(KUidToneHwDevice,  CToneHwDevice::NewL),
    34 	};
    35 //current supported sample rate
    36 const TInt KSupportedSampleRate = 8000;
    37 
    38 // ---------------------------------------------------------------------------
    39 // Default constructor
    40 // ---------------------------------------------------------------------------
    41 //
    42 CToneHwDevice::CToneHwDevice()
    43 	{
    44 	DP_CONTEXT(CToneHwDevice::CToneHwDevice *CD1*, CtxDevSound, DPLOCAL);
    45 	DP_IN();
    46 	DP_OUT();
    47 	}
    48 
    49 // -----------------------------------------------------------------------------
    50 // Symbian 2nd phase constructor
    51 // -----------------------------------------------------------------------------
    52 //
    53 void CToneHwDevice::ConstructL()
    54 	{
    55 	DP_CONTEXT(CToneHwDevice::ConstructL *CD0*, CtxDevSound, DPLOCAL);
    56 	DP_IN();
    57 
    58 	iHwDataBufferFill = CMMFDataBuffer::NewL(sizeof(TToneData));
    59 	iHwDataBufferFill->SetLastBuffer(EFalse);
    60 	iHwDataBufferFill->Data().SetLength(sizeof(TToneData));
    61 	iHwDataBufferFill->SetRequestSizeL(sizeof(TToneData));
    62 
    63 	iCodec = new(ELeave)CToneCodec();
    64 
    65 	DP_OUT();
    66 	}
    67 
    68 // -----------------------------------------------------------------------------
    69 // Symbian constructor
    70 // -----------------------------------------------------------------------------
    71 //
    72 CToneHwDevice* CToneHwDevice::NewL()
    73 	{
    74 	DP_STATIC_CONTEXT(CToneHwDevice::NewL *CD0*, CtxDevSound, DPLOCAL);
    75 	DP_IN();
    76 	CToneHwDevice* self = new (ELeave) CToneHwDevice();
    77 	CleanupStack::PushL(self);
    78 	self->ConstructL();
    79 	CleanupStack::Pop();
    80 	DP0_RET(self, "0x%x");
    81 	}
    82 
    83 // ---------------------------------------------------------------------------
    84 // Destructor
    85 // ---------------------------------------------------------------------------
    86 //
    87 CToneHwDevice::~CToneHwDevice()
    88 	{
    89 	DP_CONTEXT(CToneHwDevice::~CToneHwDevice *CD0*, CtxDevSound, DPLOCAL);
    90 	DP_IN();
    91 	delete iHwDataBufferFill;
    92 	delete iCodec;
    93 	delete iPlayCustomInterface;
    94 
    95 	if(iDataPath)
    96 		{
    97 		delete iDataPath;
    98 		}
    99 
   100 	if(iToneBuffer1)
   101 		{
   102 		delete iToneBuffer1;
   103 		}
   104 	if(iToneBuffer2)
   105 		{
   106 		delete iToneBuffer2;
   107 		}
   108 	DP_OUT();
   109 	}
   110 
   111 /**
   112 *
   113 * Codec
   114 *
   115 */
   116 CToneCodec& CToneHwDevice::Codec()
   117 	{
   118 	return *iCodec;
   119 	}
   120 
   121 // ---------------------------------------------------------------------------
   122 // from class CMMFHwDevice
   123 // CToneHwDevice::Init
   124 // ---------------------------------------------------------------------------
   125 //
   126 TInt CToneHwDevice::Init(THwDeviceInitParams& aDevInfo)
   127 	{
   128 	DP_CONTEXT(CToneHwDevice::Init *CD1*, CtxDevSound, DPLOCAL);
   129 	DP_IN();
   130 
   131 	if (!iToneInitialized) // Check if tones is not initialized yet.
   132 		{
   133 		iToneInitialized = ETrue;
   134 		}
   135 
   136 	// [ precondition that aDevInfo has a valid observer ]
   137 	if (!aDevInfo.iHwDeviceObserver) 
   138 		{
   139 		DP0_RET(KErrArgument, "%d");
   140 		}
   141 
   142 	iHwDeviceObserver = aDevInfo.iHwDeviceObserver;
   143 
   144 	//[ assert the post condition ]
   145 	if (!iCodec)
   146 		{
   147 		DP0_RET(KErrNotSupported, "%d");
   148 		}
   149 
   150 	DP0_RET(KErrNone, "%d");
   151 	}
   152 
   153 // ---------------------------------------------------------------------------
   154 // from class CMMFHwDevice
   155 // CToneHwDevice::Start
   156 // ---------------------------------------------------------------------------
   157 //
   158 TInt CToneHwDevice::Start(TDeviceFunc /*aFuncCmd*/, TDeviceFlow /*aFlowCmd*/)
   159 	{
   160 	DP_CONTEXT(CToneHwDevice::Start *CD1*, CtxDevSound, DPLOCAL);
   161 	DP_IN();
   162 
   163 	TInt error = KErrNone;
   164 		
   165 	// Start for first time OR resuming
   166 	if (!iTonePlaying || iDataPath->State() == CToneDataPath::EPaused)
   167 		{
   168 		iLastBuffer = EFalse;
   169 		//[ assert precondition that play custom interface is present]
   170 		//if there is no tone play custom interface then the user of the CToneHwDevice
   171 		//cannot have set any of the custom settings such as sample rate.
   172 		if (!iPlayCustomInterface)
   173 			{
   174 			DP0_RET(KErrNotReady, "%d");
   175 			}
   176 	
   177 		//play
   178 		if (!iDataPath)
   179 			{
   180 			//create a datapath
   181 			TRAP(error,iDataPath = CToneDataPath::NewL());
   182 			if ((iDataPath)&&(error == KErrNone))
   183 				{
   184 				ASSERT(iHwDeviceObserver);
   185 				iDataPath->SetObserver(*this);
   186 				error = iDataPath->AddCodec(*iCodec);
   187 				if (error == KErrNone)
   188 					{
   189 					iDeviceBufferSize = (iCodec->SinkBufferSize());
   190 					}
   191 				}
   192 			}
   193 		if ((error == KErrNone) && (iDataPath->State() != CToneDataPath::EPlaying))
   194 			{
   195 			//datapath was created ok and we are not playing
   196 			if (iDataPath->State() == CToneDataPath::EStopped)
   197 				{
   198 				// starting from 'fresh so set sound device settings
   199 				if (!iDataPath->Device().Handle())
   200 					{
   201 					//if Device() is called then we need a valid sound device handle
   202 					error = iDataPath->Device().Open();
   203 					if (error != KErrNone)
   204 						{
   205 						DP0_RET(error, "%d");
   206 						}
   207 						
   208 					}
   209 				static_cast<TToneCustomInterface*>(iPlayCustomInterface)->SetDevice(&(iDataPath->Device()));
   210 				
   211 				TUint iVol = iPlayCustomInterface->Volume();
   212 				iDataPath->Device().SetPlayVolume(iVol);
   213 							
   214 				soundDeviceSettings().iRate = iSampleRate;
   215 			
   216 				//this would normally be pcm16
   217 				soundDeviceSettings().iEncoding = RMdaDevSound::EMdaSoundEncoding16BitPCM;
   218 			
   219 				//1 = mono 2 = stereo
   220 				soundDeviceSettings().iChannels = iChannels;
   221 			
   222 				//tell sound driver what buffer size to expect
   223 				//it is up the the implementor to make use the device can support
   224 				//the required buffer size
   225 				soundDeviceSettings().iBufferSize = iDeviceBufferSize;
   226 				error = iDataPath->Device().SetPlayFormat(soundDeviceSettings);
   227 			
   228 				} // End of iDataPath->State() == CToneDataPath::EStopped
   229 			
   230 			//else resuming from pause	
   231 			if ((error == KErrNone)||(error == KErrInUse))
   232 				{
   233 				// Hw device hasn't played anything yet so don't change
   234 				// active buffer. This is checked in FillThisHwBuffer.
   235 				if(iDataPath->State() != CToneDataPath::EPaused)
   236 					{
   237 					iFirstCallFromHwDevice = ETrue;		
   238 					}
   239 				iTonePlaying = ETrue;
   240 				error = iDataPath->Start();
   241 				}
   242 			}//status == KErrNone       
   243 		}
   244 	else // if tone playback is already ongoing do nothing
   245 		{
   246 		DP0(DLINFO,"Previous tone call is not completed yet");
   247 		}
   248 	DP0_RET(error, "%d");
   249 	}
   250 
   251 // ---------------------------------------------------------------------------
   252 // from class CMMFHwDevice
   253 // CToneHwDevice::Stop
   254 // ---------------------------------------------------------------------------
   255 TInt CToneHwDevice::Stop()
   256 	{
   257 	DP_CONTEXT(CToneHwDevice::Stop *CD1*, CtxDevSound, DPLOCAL);
   258 	DP_IN();
   259 	if (iTonePlaying) 
   260 		{
   261 		iTonePlaying = EFalse;
   262 		}
   263 	
   264 	if (!iDataPath)
   265 		{
   266 		DP0_RET(KErrNotReady, "%d");
   267 		}
   268 		
   269 	delete iDataPath;
   270 	iDataPath = NULL;
   271 	
   272 	//Setting device to NULL since after stop it doesn't exists any more
   273 	if(iPlayCustomInterface)
   274 		{
   275 		static_cast<TToneCustomInterface*>(iPlayCustomInterface)->SetDevice(NULL);
   276 		}
   277 	
   278 	DP0_RET(KErrNone, "%d");
   279 	}
   280 
   281 // ---------------------------------------------------------------------------
   282 // from class CMMFHwDevice
   283 // CToneHwDevice::Pause
   284 // ---------------------------------------------------------------------------
   285 //
   286 TInt CToneHwDevice::Pause()
   287 	{
   288 	DP_CONTEXT(CToneHwDevice::Pause *CD1*, CtxDevSound, DPLOCAL);
   289 	DP_IN();
   290 
   291 	if (!iDataPath) 
   292 		{
   293 		DP0_RET(KErrNotReady, "%d");
   294 		}
   295 	iDataPath->Pause();
   296 	
   297 	DP0_RET(KErrNone, "%d");
   298 	}
   299 
   300 // ---------------------------------------------------------------------------
   301 // from class CMMFHwDevice
   302 // CToneHwDevice::StopAndDeleteCodec
   303 // ---------------------------------------------------------------------------
   304 //
   305 TInt CToneHwDevice::StopAndDeleteCodec()
   306 	{
   307 	DP_CONTEXT(CToneHwDevice::StopAndDeleteCodec *CD1*, CtxDevSound, DPLOCAL);
   308 	DP_IN();
   309 	DP0_RET(KErrNotSupported, "%d");
   310 	}
   311 
   312 // ---------------------------------------------------------------------------
   313 // from class CMMFHwDevice
   314 // CToneHwDevice::DeleteCodec
   315 // ---------------------------------------------------------------------------
   316 //
   317 TInt CToneHwDevice::DeleteCodec()
   318 	{
   319 	DP_CONTEXT(CToneHwDevice::DeleteCodec *CD1*, CtxDevSound, DPLOCAL);
   320 	DP_IN();
   321 	DP0_RET(KErrNotSupported, "%d");
   322 	}
   323 
   324 // ---------------------------------------------------------------------------
   325 // from class CMMFHwDevice
   326 // CToneHwDevice::SetConfig
   327 // ---------------------------------------------------------------------------
   328 //
   329 TInt CToneHwDevice::SetConfig(TTaskConfig& aConfig)
   330 	{
   331 	DP_CONTEXT(CToneHwDevice::SetConfig *CD1*, CtxDevSound, DPLOCAL);
   332 	DP_IN();
   333 
   334 	if (aConfig.iUid != KUidRefDevSoundTaskConfig)
   335 		{
   336 		DP0_RET(KErrArgument, "%d");
   337 		}
   338 
   339 	if (aConfig.iRate != KSupportedSampleRate )
   340 		{
   341 		DP0_RET(KErrNotSupported, "%d");
   342 		}
   343 
   344 	iSampleRate = aConfig.iRate;
   345 
   346 	if (aConfig.iStereoMode == ETaskMono)
   347 		{
   348 		iChannels = 1;
   349 		}
   350 	else if (aConfig.iStereoMode == ETaskInterleaved || aConfig.iStereoMode == ETaskNonInterleaved)
   351 		{
   352 		iChannels = 2;
   353 		}
   354 	else
   355 		{
   356 		DP0_RET(KErrArgument, "%d");
   357 		}
   358 		
   359 	DP0_RET(KErrNone, "%d");
   360 	}
   361 
   362 // CToneHwDevice::FillThisHwBuffer
   363 // ---------------------------------------------------------------------------
   364 TInt CToneHwDevice::FillThisHwBuffer(CMMFBuffer& aHwBuffer)
   365 	{
   366 	DP_CONTEXT(CToneHwDevice::FillThisHwBuffer *CD1*, CtxDevSound, DPLOCAL);
   367 	DP_IN();
   368 	
   369 	TInt err(KErrNone);
   370 	
   371 	if(iFirstCallFromHwDevice)
   372 		{
   373 		err = iHwDeviceObserver->FillThisHwBuffer(aHwBuffer);
   374 		}
   375 	else
   376 		{
   377 		err = ThisHwBufferFilled(aHwBuffer);
   378 		}
   379 	DP0_RET(err,"%d");
   380 	}
   381 
   382 // ---------------------------------------------------------------------------
   383 // from class CMMFHwDevice
   384 // CToneHwDevice::ThisHwBufferFilled
   385 // ---------------------------------------------------------------------------
   386 //
   387 TInt CToneHwDevice::ThisHwBufferFilled(CMMFBuffer& aMmfBuffer)
   388 	{
   389 	DP_CONTEXT(CToneHwDevice::ThisHwBufferFilled *CD1*, CtxDevSound, DPLOCAL);
   390 	DP_IN();
   391 
   392 	TInt err = KErrNone;
   393 	CMMFDataBuffer* myBuffer = static_cast<CMMFDataBuffer*> (&aMmfBuffer);
   394 	// Set the request length, From HwDevice this comes with buffer
   395 	// length.
   396 	TInt len = myBuffer->Data().MaxLength();
   397 	// Ignore error. since buffer size = Buffer Length 
   398 	TRAP(err, myBuffer->SetRequestSizeL(len));
   399 
   400 	if(iFirstCallFromHwDevice)
   401 		{
   402 		myBuffer->SetLastBuffer(EFalse);
   403 		
   404 		Mem::Copy((TAny*)(&myToneData), (TAny*)(myBuffer->Data().Ptr()), sizeof(TToneData));
   405 		
   406 		err = ReadToneData();
   407 		if(err==KErrNone)
   408 			{
   409 			err= GenerateBufferData();
   410 			}
   411 		}
   412 	else
   413 		{
   414 		// Hw device will call this method right after its Start was called.
   415 		// When it calls this for the first time it hasn't played one single
   416 		// buffer yet so check that.
   417 		// In this case there's no need to set the active buffer as it's already
   418 		// waiting to be played.
   419 		SetActiveToneBuffer();
   420 		}
   421 
   422 	if (err == KErrNone)
   423 		{
   424 		// If there is no data in the active buffer, tone play is finished.
   425 		// DevSound just have to wait for completion event from audio device.
   426 		if (iActiveToneBuffer->Data().Length() == 0)
   427 			{
   428 			iActiveToneBuffer->SetLastBuffer(ETrue);
   429 			myBuffer->SetLastBuffer(ETrue);
   430 			iLastBuffer=ETrue;
   431 			}
   432 
   433 		TInt tonelen = iActiveToneBuffer->Data().Length();
   434 
   435 		// don't enter more data than can be handled by the receiving buffer
   436 		if (len >= tonelen)
   437 			{
   438 			len = tonelen;
   439 			}
   440 
   441 		// Copy data from tone buffer to hw device buffer
   442 		Mem::Copy((TAny*)(myBuffer->Data().Ptr()), (TAny*)(iActiveToneBuffer->Data().Ptr()), len);
   443 			
   444 		myBuffer->Data().SetLength(len);
   445 
   446 		//Play data and try to generate next data block
   447 		TRAP(err,iDataPath->BufferFilledL(static_cast<CMMFDataBuffer&> (*myBuffer)));
   448 		if(err == KErrNone)
   449 			{
   450 			if(iLastBuffer)
   451 				{
   452 				// coverity[check_after_deref]
   453 				if(myBuffer)
   454 					{
   455 					myBuffer = NULL;
   456 					}
   457 				FreeBuffers();
   458 				iFirstCallFromHwDevice = EFalse;
   459 				}
   460 			else
   461 				{
   462 				// Check again whether this is the first call from Hw device.
   463 				// FillFreeToneBuffer assumes the iActiveToneBuffer has already
   464 				// been played.
   465 				if (!iFirstCallFromHwDevice)
   466 					{
   467 					err = FillFreeToneBuffer();
   468 					}
   469 				else
   470 					{
   471 					iFirstCallFromHwDevice = EFalse;  // Reset flag 
   472 					}
   473 				}
   474 			}
   475 		}
   476 	if ( err != KErrNone )
   477 		{
   478 		myBuffer->SetLastBuffer(ETrue);
   479 		myBuffer->Data().SetLength(0);
   480 		//Use error additional variable for sending last buffer so can still send Error(err)
   481 		TRAPD(datapathErr, iDataPath->BufferFilledL(static_cast<CMMFDataBuffer&> (*myBuffer)));
   482 		// coverity[check_after_deref]
   483 		if(myBuffer)
   484 			{
   485 			myBuffer = NULL;
   486 			}
   487 		FreeBuffers();
   488 		iFirstCallFromHwDevice = EFalse;
   489 		if ( datapathErr != KErrNone )
   490 			{
   491 			iHwDeviceObserver->Error(datapathErr);
   492 			DP0_RET(datapathErr, "%d");
   493 			}
   494 		iHwDeviceObserver->Error(err);
   495 		}
   496 	DP0_RET(err, "%d");
   497 	}
   498 
   499 // ---------------------------------------------------------------------------
   500 // from class MMMFHwDeviceObserver
   501 // CToneHwDevice::ThisHwBufferEmptied
   502 // ---------------------------------------------------------------------------
   503 TInt CToneHwDevice::ThisHwBufferEmptied(CMMFBuffer& /*aMmfBuffer*/)
   504 	{
   505 	DP_CONTEXT(CToneHwDevice::ThisHwBufferEmptied *CD1*, CtxDevSound, DPLOCAL);
   506 	DP_IN();
   507 	DP0_RET(KErrNotSupported, "%d");
   508 	}
   509 
   510 // ---------------------------------------------------------------------------
   511 // from class MMMFHwDeviceObserver
   512 // CToneHwDevice::EmptyThisHwBuffer
   513 // ---------------------------------------------------------------------------
   514 TInt CToneHwDevice::EmptyThisHwBuffer(CMMFBuffer& /*aMmfBuffer*/)
   515 	{
   516 	DP_CONTEXT(CToneHwDevice::EmptyThisHwBuffer *CD1*, CtxDevSound, DPLOCAL);
   517 	DP_IN();
   518 	DP0_RET(KErrNotSupported, "%d");
   519 	}
   520 
   521 // ---------------------------------------------------------------------------
   522 // from class MMMFHwDeviceObserver
   523 // CToneHwDevice::MsgFromHwDevice
   524 // ---------------------------------------------------------------------------
   525 TInt CToneHwDevice::MsgFromHwDevice(TUid aMessageType, const TDesC8& aMsg)
   526 	{
   527 	DP_CONTEXT(CToneHwDevice::MsgFromHwDevice *CD1*, CtxDevSound, DPLOCAL);
   528 	DP_IN();
   529 	TInt err(KErrNone);
   530 	err = iHwDeviceObserver->MsgFromHwDevice(aMessageType, aMsg);
   531 	DP0_RET(err, "%d");
   532 	}
   533 
   534 // ---------------------------------------------------------------------------
   535 // from class MMMFHwDeviceObserver
   536 // CToneHwDevice::Stopped
   537 // ---------------------------------------------------------------------------
   538 void CToneHwDevice::Stopped()
   539 	{
   540 	DP_CONTEXT(CToneHwDevice::Stopped *CD1*, CtxDevSound, DPLOCAL);
   541 	DP_IN();
   542 	iHwDeviceObserver->Stopped();
   543 	DP_OUT();
   544 	}
   545 
   546 // ---------------------------------------------------------------------------
   547 // from class MMMFHwDeviceObserver
   548 // CToneHwDevice::Error
   549 // ---------------------------------------------------------------------------
   550 void CToneHwDevice::Error(TInt aError)
   551 	{
   552 	DP_CONTEXT(CToneHwDevice::Error *CD1*, CtxDevSound, DPLOCAL);
   553 	DP_IN();
   554 	iHwDeviceObserver->Error(aError);
   555 	DP_OUT();
   556 	}
   557 
   558 // ---------------------------------------------------------------------------
   559 // from class CMMFHwDevice
   560 // CToneHwDevice::CustomInterface
   561 // ---------------------------------------------------------------------------
   562 //
   563 TAny* CToneHwDevice::CustomInterface(TUid aInterfaceUid)
   564 	{
   565 	DP_CONTEXT(CToneHwDevice::CustomInterface *CD1*, CtxDevSound, DPLOCAL);
   566 	DP_IN();
   567 
   568 	TAny* ret = NULL;
   569 	TInt err = KErrNone;
   570 
   571 	if (aInterfaceUid.iUid == KMmfPlaySettingsCustomInterface)
   572 		{
   573 		if (!iPlayCustomInterface)
   574 			{
   575 			TRAP(err,iPlayCustomInterface = new(ELeave)TToneCustomInterface());
   576 			}
   577 		if (err)
   578 			{
   579 			ret = NULL;
   580 			}
   581 		else
   582 			{
   583 			ret = static_cast<TAny*>(iPlayCustomInterface);
   584 			}
   585 		}
   586 	else if (aInterfaceUid == KIgnoreUnderflowCustomInterfaceTypeUid)
   587 		{
   588 		if (!iDataPath)
   589 			{
   590 			ret = NULL;			
   591 			}
   592 		else
   593 			{
   594 			ret = static_cast<CToneDataPath*>(iDataPath)->CustomInterface(aInterfaceUid);	
   595 			}	
   596 		}
   597 
   598 	DP_OUT();
   599 	return ret;
   600 	}
   601 
   602 TInt CToneHwDevice::ReadToneData()
   603 	{
   604 	DP_CONTEXT(CToneHwDevice::ReadToneData *CD1*, CtxDevSound, DPLOCAL);
   605 	DP_IN();
   606 
   607 	TUint vol;
   608 	myToneData.GetType(iToneType);
   609 	TInt64 zeroInt64(0);
   610 	iFrequency1 = myToneData.GetFrequencyOne();
   611 	iFrequency2 = myToneData.GetFrequencyTwo();
   612 	myToneData.GetDuration(iDuration);
   613 
   614 	myToneData.GetRepeatTrailingSilence(iRepeatTrailingSilence);
   615 	iRepeatCount = myToneData.GetRepeatCount();
   616 	iRampDuration = iPlayCustomInterface->VolumeRamp();
   617 	vol = iPlayCustomInterface->Volume();
   618 	iDataPath->Device().SetPlayVolume(vol);
   619 	switch (iToneType)
   620 		{
   621 		case TToneData::ESimple:
   622 			DP0(DLINFO, "Playing simple tone");
   623 			iDataPath->Device().GetPlayFormat(soundDeviceSettings);
   624 			if((iFrequency1<0) || (iDuration.Int64() < zeroInt64))
   625 				{
   626 				iHwDeviceObserver->Error(KErrArgument);
   627 				DP0_RET(KErrArgument, "%d");
   628 				}
   629 			iToneGen.SetFrequencyAndDuration(iFrequency1,iDuration);
   630 			// Configure tone generator
   631 			iToneGen.Configure(
   632 				soundDeviceSettings().iRate,
   633 				soundDeviceSettings().iChannels,
   634 				iRepeatCount,
   635 				I64LOW((iRepeatTrailingSilence.Int64()*soundDeviceSettings().iRate)/1000000),
   636 				I64LOW((iRampDuration.Int64()*soundDeviceSettings().iRate)/1000000)
   637 				);
   638 			iCurrentGenerator = &iToneGen;
   639 			break;
   640 		case TToneData::EDual:
   641 			DP0(DLINFO, "Playing dual tone");
   642 			iDataPath->Device().GetPlayFormat(soundDeviceSettings);
   643 			if((iFrequency1<0) || (iFrequency2<0) || (iDuration.Int64() < zeroInt64))
   644 				{
   645 				iHwDeviceObserver->Error(KErrArgument);
   646 				DP0_RET(KErrArgument, "%d");
   647 				}
   648 			iDualToneGen.SetFrequencyAndDuration(iFrequency1, iFrequency2, iDuration);
   649 			// Configure dual tone generator
   650 			iDualToneGen.Configure(
   651 				soundDeviceSettings().iRate,
   652 				soundDeviceSettings().iChannels,
   653 				iRepeatCount,
   654 				I64LOW((iRepeatTrailingSilence.Int64()*soundDeviceSettings().iRate)/KOneMillionMicroSeconds),
   655 				I64LOW((iRampDuration.Int64()*soundDeviceSettings().iRate)/KOneMillionMicroSeconds)
   656 				);
   657 			iCurrentGenerator = &iDualToneGen;
   658 			break;
   659 		case TToneData::EDtmfString:
   660 			DP0(DLINFO, "Playing DTMF string");
   661 			myToneData.GetDtmfLenghts(myToneOnLength, myToneOffLength, myPauseLength);
   662 			iDTMFGen.SetToneDurations(myToneOnLength, myToneOffLength, myPauseLength);
   663 			iDTMFString = myToneData.GetDTMFString();
   664 			if(!ValidDTMFString(const_cast<TDesC&>(*iDTMFString)))
   665 				{
   666 				DP0(DLINFO, "Invalid DTMF String");
   667 				iHwDeviceObserver->Error(KErrCorrupt);
   668 				DP0_RET(KErrCorrupt, "%d");
   669 				}
   670 			iDTMFGen.SetString(const_cast<TDesC&>(*iDTMFString));
   671 			iDataPath->Device().GetPlayFormat(soundDeviceSettings);
   672 			iDTMFGen.Configure(
   673 				soundDeviceSettings().iRate,
   674 				soundDeviceSettings().iChannels,
   675 				iRepeatCount,
   676 				I64LOW((iRepeatTrailingSilence.Int64()*soundDeviceSettings().iRate)/1000000),
   677 				I64LOW((iRampDuration.Int64()*soundDeviceSettings().iRate)/1000000)
   678 				);
   679 			iCurrentGenerator = &iDTMFGen;
   680 			break;
   681 		case TToneData::ESequence:
   682 			DP0(DLINFO, "Playing tone sequence");
   683 			iSequenceData = myToneData.GetSequenceData();
   684 			// Check whether the sequence is signed or not
   685 			if (!RecognizeSequence(*iSequenceData))
   686 				{
   687 				DP0(DLINFO, "Invalid Sequence Sign");
   688 				iHwDeviceObserver->Error(KErrCorrupt);
   689 				DP0_RET(KErrCorrupt, "%d");
   690 				}
   691 			iSequenceGen.SetSequenceData(*iSequenceData);
   692 			iDataPath->Device().GetPlayFormat(soundDeviceSettings);
   693 			iSequenceGen.Configure(
   694 				soundDeviceSettings().iRate,
   695 				soundDeviceSettings().iChannels,
   696 				iRepeatCount,
   697 				I64LOW((iRepeatTrailingSilence.Int64()*soundDeviceSettings().iRate)/1000000),
   698 				I64LOW((iRampDuration.Int64()*soundDeviceSettings().iRate)/1000000)
   699 				);
   700 			iCurrentGenerator = &iSequenceGen;
   701 			break;
   702 		case TToneData::EFixedSequence:
   703 			DP0(DLINFO, "Playing FixedSequnce");
   704 			iHwDeviceObserver->Error(KErrNotSupported);
   705 			DP0_RET(KErrNotSupported, "%d");
   706 		default:
   707 			DP0_RET(KErrNotSupported, "%d");
   708 		}
   709 	DP0_RET(KErrNone, "%d");
   710 	}
   711 
   712 /*
   713  *
   714  *	Creates buffer and begin playback using the specified tone generator.
   715  *
   716  */
   717 TInt CToneHwDevice::GenerateBufferData()
   718 	{
   719 	DP_CONTEXT(CToneHwDevice::GenerateBufferData *CD1*, CtxDevSound, DPLOCAL);
   720 	DP_IN();
   721 	
   722 	TInt err;
   723 	err = KErrNone;
   724 	
   725 	// Delete any buffer from previous call and try to create maximum buffer 
   726 	// size. Double Buffer the Tone data.
   727 	if (iToneBuffer1)
   728 		{
   729 		delete iToneBuffer1;
   730 		iToneBuffer1 = NULL;
   731 		}
   732 	//note the tone buffer needs to be the same as the pcm16->pcm16 'null'
   733 	//hw device plugin
   734 	// Buffer size = (SampleRate * BytesPerSample * Channels) / 4
   735 	TInt useBufferOfSize = ((SamplingFrequency() * 2 * NumberOfChannels())/KDevSoundFramesPerSecond + (KDevSoundDeltaFrameSize-1)) &~ (KDevSoundDeltaFrameSize-1);
   736 	//clamp buffer to desired limits
   737 	if(useBufferOfSize < KDevSoundMinFrameSize) 
   738 		{
   739 		useBufferOfSize = KDevSoundMinFrameSize;
   740 		}
   741 	else if(useBufferOfSize > KDevSoundMaxFrameSize) 
   742 		{
   743 		useBufferOfSize = KDevSoundMaxFrameSize;
   744 		}
   745 
   746 	TRAP(err, iToneBuffer1 = CMMFDataBuffer::NewL(useBufferOfSize));
   747 	if ( err != KErrNone )
   748 		{
   749 		DP0_RET(err, "%d");
   750 		}
   751 	
   752 	err = iCurrentGenerator->FillBuffer(iToneBuffer1->Data());
   753 	if(err!=KErrNone)
   754 		{
   755 		DP0_RET(err, "%d");
   756 		}
   757 
   758 	if (iToneBuffer2)
   759 		{
   760 		delete iToneBuffer2; 
   761 		iToneBuffer2 = NULL;
   762 		}
   763 		
   764 	TRAP(err, iToneBuffer2 = CMMFDataBuffer::NewL(useBufferOfSize));
   765 	if ( err != KErrNone )
   766 		{
   767 		DP0_RET(err, "%d");
   768 		}
   769 	
   770 	err = iCurrentGenerator->FillBuffer(iToneBuffer2->Data());
   771 	if(err!=KErrNone)
   772 		{
   773 		DP0_RET(err, "%d");
   774 		}
   775 
   776 	// Assign active buffer
   777 	iActiveToneBuffer = iToneBuffer1;
   778 	DP0_RET(KErrNone, "%d");
   779 	}
   780 
   781 /*
   782  *
   783  *	This method assigns the other buffer as active buffer. The tone audio 
   784  *	generator should fill data in the other buffer by now.
   785  *
   786  */
   787 void CToneHwDevice::SetActiveToneBuffer()
   788 	{
   789 	if (iActiveToneBuffer == iToneBuffer1)
   790 		iActiveToneBuffer = iToneBuffer2;
   791 	else if (iActiveToneBuffer == iToneBuffer2)
   792 		iActiveToneBuffer = iToneBuffer1;
   793 	}
   794 
   795 	
   796 void CToneHwDevice::FreeBuffers()
   797 	{
   798 	if(iToneBuffer1)
   799 		{
   800 		delete iToneBuffer1;
   801 		iToneBuffer1 = NULL;
   802 		}
   803 	if(iToneBuffer2)
   804 		{
   805 		delete iToneBuffer2;
   806 		iToneBuffer2 = NULL;
   807 		}
   808 	}
   809 	
   810 	/*
   811  *
   812  *	Returns an integer representing Sampling Frequency the device is currently
   813  *	configured to.
   814  *
   815  *	@return	"TInt"
   816  *			Sampling Frequency.
   817  *
   818  */
   819 TInt CToneHwDevice::SamplingFrequency()
   820 	{
   821 	return iSampleRate;
   822 	}
   823 	
   824 /*
   825  *
   826  *	Returns an integer representing number of channels the device is currently
   827  *	configured to.
   828  *
   829  *	@return	"TInt"
   830  *			Number of audio channels 1 if mono, 2 if stereo.
   831  *
   832  */
   833 TInt CToneHwDevice::NumberOfChannels()
   834 	{
   835 	if(iChannels == EMMFMono)
   836 		return 1;
   837 	else
   838 		return 2;
   839 	}
   840 	
   841 	/*
   842  *
   843  *	This method fills data into the free buffer.
   844  *
   845  *	@return	"TInt"
   846  *			Error code. KErrNone if success.
   847  *
   848  */
   849 TInt CToneHwDevice::FillFreeToneBuffer()
   850 	{
   851 	TInt err(KErrNone);
   852 	if (iActiveToneBuffer == iToneBuffer1)
   853 		{
   854 		err = iCurrentGenerator->FillBuffer(iToneBuffer2->Data());
   855 		}
   856 	else if (iActiveToneBuffer == iToneBuffer2)
   857 		{
   858 		err = iCurrentGenerator->FillBuffer(iToneBuffer1->Data());
   859 		}
   860 	return err;
   861 	}
   862 
   863 TBool CToneHwDevice::RecognizeSequence(const TDesC8& aData)
   864 	{
   865 	// Reference plug-in only supports its own sequence format
   866 	_LIT8(KSequenceSignature,"SQNC");
   867 	if (aData.Length() > 4)
   868 		{
   869 		if (aData.Left(4) == KSequenceSignature)
   870 			{
   871 			return ETrue;
   872 			}
   873 		}
   874 	// Didn't recognise
   875 	return EFalse;
   876 	}
   877 
   878 TBool CToneHwDevice::ValidDTMFString(const TDesC& aDTMFString)
   879 	{
   880 	const TDesC* stringDTMF = &aDTMFString;;
   881 	TInt stringPos = 0;
   882 	if (stringPos == stringDTMF->Length())
   883 		{
   884 		return EFalse; // Finished. Nothing to do
   885 		}
   886 	do
   887 		{
   888 		TChar c((*stringDTMF)[stringPos++]);
   889 		if (static_cast<TUint> (c)=='#' || static_cast<TUint> (c)=='*' || static_cast<TUint> (c)==',' || c.IsHexDigit() || c.IsSpace())
   890 			{
   891 			//Do nothing, valid character
   892 			}
   893 		else
   894 			{
   895 			return EFalse;
   896 			}
   897 
   898 		}
   899 	while(stringPos < stringDTMF->Length());
   900 	return ETrue;
   901 	}
   902 
   903 
   904 /************************************************************************
   905  *				TToneCustomInterface     								*
   906  ************************************************************************/
   907 /**
   908  * This method is not be exported as it is only 
   909  * intended to be called within this DLL.
   910  * It's purpose is to assign an RMdaDevSound to the play
   911  * custom interface
   912  * @internalComponent
   913  */
   914 void TToneCustomInterface::SetDevice(RMdaDevSound* aDevice)
   915 	{
   916 	iDevice = aDevice;
   917 	}
   918 
   919 void TToneCustomInterface::SetVolume(TUint aVolume)
   920 	{
   921 	iVolume = aVolume;
   922 	if (iDevice && iDevice->Handle()!=0)
   923 		{
   924 		iDevice->SetPlayVolume(aVolume);
   925 		}
   926 	}
   927 	
   928 /**
   929  * Procedure to get the number of bytes played by the device driver
   930  * If there is no handle available to the device driver then the 
   931  * procedure returns the last known value
   932  * @released
   933  * @return number of bytes played
   934  */
   935 TUint TToneCustomInterface::BytesPlayed()
   936 	{
   937 	if(iDevice)
   938 		{
   939 		if (iDevice->Handle())
   940 			{
   941 			iBytesPlayed = iDevice->BytesPlayed();
   942 			}
   943 		}
   944 	return iBytesPlayed;
   945 	}
   946 
   947 
   948 //
   949 //       class CToneCodec            //
   950 //
   951 
   952 // ---------------------------------------------------------------------------
   953 // from class CToneCodec
   954 // CToneCodec::CToneCodec
   955 // ---------------------------------------------------------------------------
   956 //
   957 CToneCodec::CToneCodec()
   958 	{
   959 	}
   960 	
   961 // ---------------------------------------------------------------------------
   962 // from class CToneCodec
   963 // CToneCodec::~CToneCodec
   964 // ---------------------------------------------------------------------------
   965 //	
   966 CToneCodec::~CToneCodec()
   967 	{
   968 	}
   969 
   970 // ---------------------------------------------------------------------------
   971 // from class CToneCodec
   972 // CToneCodec::ConstructL
   973 // ---------------------------------------------------------------------------
   974 //	
   975 void CToneCodec::ConstructL()
   976 	{
   977 	}
   978 	
   979 
   980 // ---------------------------------------------------------------------------
   981 // from class CToneCodec
   982 // CToneCodec::ProcessL
   983 // ---------------------------------------------------------------------------
   984 //
   985 CToneCodec::TCodecProcessResult CToneCodec::ProcessL(const CMMFBuffer& /*aSource*/, CMMFBuffer& /*aDest*/)
   986 	{
   987 	//no processing required for null codec
   988 	User::Leave(KErrNotSupported); 
   989 	//to keep compiler happy
   990 	TCodecProcessResult result;
   991 	result.iCodecProcessStatus = TCodecProcessResult::EEndOfData;
   992 	result.iSrcBytesProcessed = 0;
   993 	result.iDstBytesAdded = 0;
   994 	return result;
   995 	}
   996 
   997 // ---------------------------------------------------------------------------
   998 // from class CToneCodec
   999 // CToneCodec::SourceBufferSize
  1000 // ---------------------------------------------------------------------------
  1001 //
  1002 TUint CToneCodec::SourceBufferSize()
  1003 	{
  1004 	return KPCM16ToPCM16BufferSize;
  1005 	}
  1006 
  1007 // ---------------------------------------------------------------------------
  1008 // from class CToneCodec
  1009 // CToneCodec::SinkBufferSize
  1010 // ---------------------------------------------------------------------------
  1011 //
  1012 TUint CToneCodec::SinkBufferSize()
  1013 	{
  1014 	return KPCM16ToPCM16BufferSize;
  1015 	}
  1016 
  1017 
  1018 // ========================== OTHER EXPORTED FUNCTIONS =========================
  1019 /**
  1020 * ImplementationGroupProxy
  1021 * is called to get a pointer to the plugin's implementation table, or table
  1022 * of functions used to instantiate the plugin.
  1023 * @since
  1024 * @param aTableCount returns the number of functions in the table.
  1025 * @return retuns a pointer to the table.
  1026 */
  1027 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
  1028 	{
  1029 	aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
  1030 	return ImplementationTable;
  1031 	}