1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/mm/mmdevicefw/mdf/src/audio/mdasoundadapter/mdasoundadapterbody.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,1944 @@
1.4 +// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +#include "mdasoundadapterconsts.h"
1.19 +#include "mdasoundadapterbody.h"
1.20 +#include <e32debug.h>
1.21 +
1.22 +#include "mmf/utils/rateconvert.h" // if we need to resample
1.23 +
1.24 +#include <hal.h>
1.25 +
1.26 +_LIT(KPddFileName,"SOUNDSC.PDD");
1.27 +_LIT(KLddFileName,"ESOUNDSC.LDD");
1.28 +
1.29 +
1.30 +const TInt KBytesPerSample = 2;
1.31 +const TInt KMinBufferSize = 2;
1.32 +
1.33 +/**
1.34 +This function raises a panic
1.35 +EDeviceNotOpened is raised when any of the RMdaDevSound APIs are called before opening the device.
1.36 +*/
1.37 +GLDEF_C void Panic(TSoundAdapterPanicCodes aPanicCode)
1.38 + {
1.39 + User::Panic(KSoundAdapterPanicCategory, aPanicCode);
1.40 + }
1.41 +
1.42 +
1.43 +const TText8 *RMdaDevSound::CBody::TState::Name() const
1.44 + {
1.45 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.46 + switch(iState)
1.47 + {
1.48 + case ENotReady: return _S8("ENotReady");
1.49 + case EStopped: return _S8("EStopped");
1.50 + case ERecording: return _S8("ERecording");
1.51 + case ERecordingPausedInHw: return _S8("ERecordingPausedInHw");
1.52 + case ERecordingPausedInSw: return _S8("ERecordingPausedInSw");
1.53 + case EPlaying: return _S8("EPlaying");
1.54 + case EPlayingPausedInHw: return _S8("EPlayingPausedInHw");
1.55 + case EPlayingPausedInSw: return _S8("EPlayingPausedInSw");
1.56 + case EPlayingUnderrun: return _S8("EPlayingUnderrun");
1.57 + }
1.58 + return _S8("CorruptState");
1.59 + #else
1.60 + return _S8("");
1.61 + #endif
1.62 + }
1.63 +
1.64 +
1.65 +
1.66 +RMdaDevSound::CBody::TState &RMdaDevSound::CBody::TState::operator=(TStateEnum aNewState)
1.67 + {
1.68 + if(iState != aNewState)
1.69 + {
1.70 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.71 + RDebug::Printf("RMdaDevSound state %s -> %s", Name(), TState(aNewState).Name());
1.72 + #endif
1.73 + iState = aNewState;
1.74 + }
1.75 + return *this;
1.76 + }
1.77 +
1.78 +RMdaDevSound::CBody* RMdaDevSound::CBody::NewL()
1.79 + {
1.80 + CBody* self = new(ELeave) CBody();
1.81 + CleanupStack::PushL(self);
1.82 + self->ConstructL();
1.83 + CleanupStack::Pop();
1.84 + return self;
1.85 + }
1.86 +
1.87 +RMdaDevSound::CBody::~CBody()
1.88 + {
1.89 + for(TInt i = 0; i < KPlaySharedChunkBuffers; i++)
1.90 + {
1.91 + delete iPlayers[i];
1.92 + iPlayers[i] = NULL;
1.93 + }
1.94 + delete iRecorder;
1.95 + iRecorder = NULL;
1.96 + delete iPlayFormatData.iConverter;
1.97 + delete iRecordFormatData.iConverter;
1.98 + iPlayChunk.Close();
1.99 + iPlaySoundDevice.Close();
1.100 + iRecordChunk.Close();
1.101 + iRecordSoundDevice.Close();
1.102 + iConvertedPlayData.Close();
1.103 + iSavedTrailingData.Close();
1.104 + iBufferedRecordData.Close();
1.105 + }
1.106 +
1.107 +RMdaDevSound::CBody::CBody()
1.108 + :iState(ENotReady), iBufferOffset(-1)
1.109 + {
1.110 +
1.111 + }
1.112 +
1.113 +TVersion RMdaDevSound::CBody::VersionRequired() const
1.114 + {
1.115 + if(iPlaySoundDevice.Handle())
1.116 + {
1.117 + return iPlaySoundDevice.VersionRequired();
1.118 + }
1.119 + else
1.120 + {
1.121 + return TVersion();
1.122 + }
1.123 + }
1.124 +
1.125 +TInt RMdaDevSound::CBody::IsMdaSound()
1.126 + {
1.127 + return ETrue;
1.128 + }
1.129 +
1.130 +void RMdaDevSound::CBody::ConstructL()
1.131 + {
1.132 + // Try to load the audio physical driver
1.133 + TInt err = User::LoadPhysicalDevice(KPddFileName);
1.134 + if ((err!=KErrNone) && (err!=KErrAlreadyExists))
1.135 + {
1.136 + User::Leave(err);
1.137 + }
1.138 + // Try to load the audio logical driver
1.139 + err = User::LoadLogicalDevice(KLddFileName);
1.140 + if ((err!=KErrNone) && (err!=KErrAlreadyExists))
1.141 + {
1.142 + User::Leave(err);
1.143 + }
1.144 + for(TInt i=0; i<KPlaySharedChunkBuffers; i++)
1.145 + {
1.146 + iPlayers[i] = new(ELeave) CPlayer(CActive::EPriorityUserInput, *this, i);
1.147 + iFreePlayers.Push(iPlayers[i]);
1.148 + }
1.149 +
1.150 + iRecorder = new(ELeave) CRecorder(CActive::EPriorityUserInput, *this);
1.151 +
1.152 + TInt tmp;
1.153 + User::LeaveIfError(HAL::Get(HAL::ENanoTickPeriod, tmp));
1.154 + iNTickPeriodInUsec = tmp;
1.155 + }
1.156 +
1.157 +TInt RMdaDevSound::CBody::Open(TInt /*aUnit*/)
1.158 + {
1.159 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.160 + RDebug::Print(_L("RMdaDevSound::CBody::Open "));
1.161 + #endif
1.162 + TInt err = KErrNone;
1.163 + //Default behavior of this method is to open both the play and record audio devices.
1.164 + if(!iPlaySoundDevice.Handle() && !iRecordSoundDevice.Handle())
1.165 + {
1.166 + err = iPlaySoundDevice.Open(KSoundScTxUnit0);
1.167 + if(err == KErrNone)
1.168 + {
1.169 + err = iRecordSoundDevice.Open(KSoundScRxUnit0);
1.170 + }
1.171 + }
1.172 + if(err != KErrNone)
1.173 + {
1.174 + Close();
1.175 + }
1.176 + else
1.177 + {
1.178 + TSoundFormatsSupportedV02Buf capsBuf;
1.179 + iPlaySoundDevice.Caps(capsBuf);
1.180 + TInt minBufferSize = KMinBufferSize;
1.181 + #ifdef SYMBIAN_FORCE_32BIT_LENGTHS
1.182 + minBufferSize = Max(minBufferSize, 4); // force to 32-bit buffer align
1.183 + #endif
1.184 + iRequestMinSize = Max(capsBuf().iRequestMinSize, minBufferSize);
1.185 + // work out mask so that x&iRequestMinMask is equiv to x/iRequestMinSize*iRequestMinSize
1.186 + iRequestMinMask = ~(iRequestMinSize-1); // assume iRequestMinSize is power of 2
1.187 + iSavedTrailingData.Close();
1.188 + iSavedTrailingData.Create(iRequestMinSize);
1.189 +
1.190 + iState = EStopped;
1.191 + iBytesPlayed = 0;
1.192 + }
1.193 +
1.194 + return err;
1.195 + }
1.196 +
1.197 +TInt RMdaDevSound::CBody::PlayVolume()
1.198 + {
1.199 + __ASSERT_DEBUG(iPlaySoundDevice.Handle(), Panic(EDeviceNotOpened));
1.200 + return iPlaySoundDevice.Volume();
1.201 + }
1.202 +
1.203 +void RMdaDevSound::CBody::SetPlayVolume(TInt aVolume)
1.204 + {
1.205 + __ASSERT_DEBUG(iPlaySoundDevice.Handle(), Panic(EDeviceNotOpened));
1.206 + if(aVolume >=0 && aVolume<=KSoundMaxVolume)
1.207 + {
1.208 + iPlaySoundDevice.SetVolume(KLinerToDbConstantLookup[aVolume].iDBValue);
1.209 + }
1.210 + }
1.211 +void RMdaDevSound::CBody::SetVolume(TInt aLogarithmicVolume)
1.212 + {
1.213 + __ASSERT_DEBUG(iPlaySoundDevice.Handle(), Panic(EDeviceNotOpened));
1.214 + if(aLogarithmicVolume >= 0 && aLogarithmicVolume <= KSoundMaxVolume)
1.215 + {
1.216 + iPlaySoundDevice.SetVolume(aLogarithmicVolume);
1.217 + }
1.218 + }
1.219 +
1.220 +void RMdaDevSound::CBody::CancelPlayData()
1.221 + {
1.222 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.223 + RDebug::Printf("RMdaDevSound::CBody::CancelPlayData: state %s", iState.Name());
1.224 + #endif
1.225 + __ASSERT_DEBUG(iPlaySoundDevice.Handle(), Panic(EDeviceNotOpened));
1.226 +
1.227 + // If there is a client request, cancel it
1.228 + // Must do this before canceling players because otherwise they may just restart!
1.229 + if(iClientPlayStatus)
1.230 + {
1.231 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.232 + RDebug::Printf("msp PlayCancelled complete iClientPlayStatus");
1.233 + #endif
1.234 + User::RequestComplete(iClientPlayStatus, KErrCancel); // Call also sets iClientPlayStatus to NULL
1.235 + }
1.236 +
1.237 + // Discard any buffered data
1.238 + iClientPlayData.Set(0,0);
1.239 + // Discard any saved trailing data (ie. data saved due driver requiring all requests to be a multiple of iRequestMinSize).
1.240 + iSavedTrailingData.SetLength(0);
1.241 +
1.242 + // Emulator RSoundSc PDD when running without a soundcard has a major
1.243 + // issue with cancelling whilst paused. It will not clear the pending
1.244 + // list (because the timer is not active) and therefore this list will
1.245 + // later overflow causing hep corruption.
1.246 + // This means that, for now, we MUST Resume before calling CancelPlayData
1.247 + // to avoid kernel panics...
1.248 +
1.249 + // The device driver will not cancel a request which is in progress...
1.250 + // So, if we are paused in hw, we must resume before cancelling the
1.251 + // player otherwise it will hang in CActive::Cancel
1.252 + if(iState == EPlayingPausedInHw)
1.253 + {
1.254 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.255 + RDebug::Printf("msp Resume to avoid hang");
1.256 + #endif
1.257 + (void) iPlaySoundDevice.Resume();
1.258 + }
1.259 +
1.260 + // Update state
1.261 + iState = EStopped;
1.262 +
1.263 +
1.264 + // The RSoundSc driver will not cancel a request which is in progress (or paused).
1.265 + // If we just loop across the players, cancelling each individual request and waiting for it to complete,
1.266 + // several of them will actually play, which is both wrong and time consuming....
1.267 + // Issue a block cancel upfront to avoid this
1.268 + iPlaySoundDevice.CancelPlayData();
1.269 +
1.270 + // Cancel all players
1.271 + for (TUint playerIndex=0; playerIndex<KPlaySharedChunkBuffers; ++playerIndex)
1.272 + {
1.273 + // If the player is active it will call PlayRequestCompleted with aDueToCancelCommand true
1.274 + // to update the iFreePlayers and iActivePlayRequestSizes FIFOs.
1.275 + iPlayers[playerIndex]->Cancel();
1.276 + }
1.277 +
1.278 + iBufferOffset = -1;
1.279 + iBufferLength = 0;
1.280 +
1.281 + return;
1.282 + }
1.283 +
1.284 +TInt RMdaDevSound::CBody::RecordLevel()
1.285 + {
1.286 + __ASSERT_DEBUG(iRecordSoundDevice.Handle(), Panic(EDeviceNotOpened));
1.287 + return iRecordSoundDevice.Volume();
1.288 + }
1.289 +
1.290 +void RMdaDevSound::CBody::SetRecordLevel(TInt aLevel)
1.291 + {
1.292 + __ASSERT_DEBUG(iRecordSoundDevice.Handle(), Panic(EDeviceNotOpened));
1.293 + iRecordSoundDevice.SetVolume(aLevel);
1.294 + }
1.295 +
1.296 +void RMdaDevSound::CBody::CancelRecordData()
1.297 + {
1.298 + __ASSERT_DEBUG(iRecordSoundDevice.Handle(), Panic(EDeviceNotOpened));
1.299 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.300 + RDebug::Printf("RMdaDevSound::CBody::CancelRecordData: state %s", iState.Name());
1.301 + #endif
1.302 +
1.303 + // Stop recorder object (and its request)
1.304 + iRecorder->Cancel();
1.305 +
1.306 + // Stop driver from recording
1.307 + iRecordSoundDevice.CancelRecordData();
1.308 +
1.309 + // If there is a client request, cancel it
1.310 + if(iClientRecordStatus)
1.311 + {
1.312 + User::RequestComplete(iClientRecordStatus, KErrNone); // Call also sets iClientPlayStatus to NULL
1.313 + }
1.314 +
1.315 + iState = EStopped;
1.316 + return;
1.317 + }
1.318 +
1.319 +void RMdaDevSound::CBody::FlushRecordBuffer()
1.320 + {
1.321 + __ASSERT_DEBUG(iRecordSoundDevice.Handle(), Panic(EDeviceNotOpened));
1.322 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.323 + RDebug::Print(_L("RMdaDevSound::CBody::FlushRecordBuffer - implemented by calling PauseRecordBuffer"));
1.324 + #endif
1.325 +
1.326 + PauseRecordBuffer();
1.327 + }
1.328 +
1.329 +TInt RMdaDevSound::CBody::BytesPlayed()
1.330 + {
1.331 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.332 + RDebug::Printf("RMdaDevSound::BytesPlayed %s", iState.Name());
1.333 + #endif
1.334 +
1.335 + return I64LOW(BytesPlayed64());
1.336 + }
1.337 +
1.338 +
1.339 +TUint64 RMdaDevSound::CBody::BytesPlayed64()
1.340 + {
1.341 + __ASSERT_DEBUG(iPlaySoundDevice.Handle(), Panic(EDeviceNotOpened));
1.342 +
1.343 + TUint64 currentBytesPlayed = KMaxTUint64;
1.344 +
1.345 + switch(iState)
1.346 + {
1.347 + case ENotReady:
1.348 + Panic(EDeviceNotOpened);
1.349 + break;
1.350 +
1.351 + case EStopped:
1.352 + currentBytesPlayed = iBytesPlayed;
1.353 + break;
1.354 +
1.355 + case ERecording:
1.356 + case ERecordingPausedInHw:
1.357 + case ERecordingPausedInSw:
1.358 + Panic(EBadState);
1.359 + break;
1.360 +
1.361 + case EPlayingPausedInHw: // ie. Play request pending on h/w and paused
1.362 + // Paused, so use pause time
1.363 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.364 + RDebug::Printf("EPlayingPausedInHw: iPausedBytes %x %x", I64HIGH(iPausedBytesPlayed), I64LOW(iPausedBytesPlayed));
1.365 + #endif
1.366 + currentBytesPlayed = iPausedBytesPlayed;
1.367 + break;
1.368 +
1.369 + case EPlayingPausedInSw: // ie. Driver not playing or paused
1.370 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.371 + RDebug::Printf("EPlayingPausedInSw: iPausedBytesPlayed %x %x", I64HIGH(iPausedBytesPlayed), I64LOW(iPausedBytesPlayed));
1.372 + #endif
1.373 + currentBytesPlayed = iPausedBytesPlayed;
1.374 + break;
1.375 + case EPlayingUnderrun:
1.376 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.377 + RDebug::Printf("EPlayingUnderrun: iBytesPlayed %x %x", I64HIGH(iBytesPlayed), I64LOW(iBytesPlayed));
1.378 + #endif
1.379 + currentBytesPlayed = iBytesPlayed;
1.380 + break;
1.381 +
1.382 + case EPlaying:
1.383 + {
1.384 + // Playing so calculate time since last update to iBytesPlayed
1.385 + TUint32 curTime = CurrentTimeInMsec();
1.386 + TUint32 curRequestSize = iActivePlayRequestSizes.Peek();
1.387 +
1.388 + TUint32 extraPlayTime = (curTime >= iStartTime) ? (curTime-iStartTime) : (KMaxTUint32 - (iStartTime-curTime));
1.389 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.390 + RDebug::Printf("iStartTime %d curTime %d extraPlayTime %d", iStartTime, curTime, extraPlayTime);
1.391 +
1.392 + RDebug::Printf("iPlayFormatData.iSampleRate %d KBytesPerSample %d iNTickPeriodInUsec %d",
1.393 + iPlayFormatData.iSampleRate, KBytesPerSample, iNTickPeriodInUsec);
1.394 + #endif
1.395 + TUint32 extraBytesPlayed = TUint32((TUint64(extraPlayTime) * iPlayFormatData.iSampleRate * iPlayFormatData.iRequestedChannels * KBytesPerSample)/1000);
1.396 + if(extraBytesPlayed > curRequestSize)
1.397 + {
1.398 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.399 + RDebug::Printf("caping extraBytes played from %d to %d", extraBytesPlayed, curRequestSize);
1.400 + #endif
1.401 + extraBytesPlayed = curRequestSize;
1.402 + }
1.403 +
1.404 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.405 + RDebug::Printf("iBytesPlayed %d extraBytesPlayed %d (curRequestSize %d) -> currentBytesPlayed %x %x",
1.406 + iBytesPlayed, extraBytesPlayed, curRequestSize, I64HIGH(currentBytesPlayed), I64LOW(currentBytesPlayed));
1.407 + #endif
1.408 +
1.409 + currentBytesPlayed = iBytesPlayed + extraBytesPlayed;
1.410 + break;
1.411 + }
1.412 +
1.413 + default:
1.414 + Panic(EBadState);
1.415 + break;
1.416 + }
1.417 +
1.418 +
1.419 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.420 + RDebug::Printf("iPlayFormatData.iConverter %x", iPlayFormatData.iConverter);
1.421 + #endif
1.422 +
1.423 + if (iPlayFormatData.iConverter)
1.424 + {
1.425 + // need to scale bytes played to fit with requested rate and channels, not actual
1.426 + if (iPlayFormatData.iActualChannels != iPlayFormatData.iRequestedChannels)
1.427 + {
1.428 + if (iPlayFormatData.iActualChannels == 2)
1.429 + {
1.430 + // requested was mono, we have stereo
1.431 + currentBytesPlayed /= 2;
1.432 + }
1.433 + else
1.434 + {
1.435 + // requested was stereo, we have mono
1.436 + currentBytesPlayed *= 2;
1.437 + }
1.438 + }
1.439 + if (iPlayFormatData.iSampleRate != iPlayFormatData.iActualRate)
1.440 + {
1.441 + currentBytesPlayed = TUint64(currentBytesPlayed*
1.442 + TReal(iPlayFormatData.iSampleRate)/TReal(iPlayFormatData.iActualRate)); // don't round
1.443 + }
1.444 + }
1.445 +
1.446 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.447 + RDebug::Printf("currentBytesPlayed %x %x", I64HIGH(currentBytesPlayed), I64LOW(currentBytesPlayed));
1.448 + #endif
1.449 + return currentBytesPlayed;
1.450 + }
1.451 +
1.452 +void RMdaDevSound::CBody::ResetBytesPlayed()
1.453 + {
1.454 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.455 + RDebug::Printf("RMdaDevSound::CBody::ResetBytesPlayed %s", iState.Name());
1.456 + #endif
1.457 + __ASSERT_DEBUG(iPlaySoundDevice.Handle(), Panic(EDeviceNotOpened));
1.458 + iBytesPlayed = 0;
1.459 + iPlaySoundDevice.ResetBytesTransferred();
1.460 + return;
1.461 + }
1.462 +
1.463 +void RMdaDevSound::CBody::PausePlayBuffer()
1.464 + {
1.465 +#ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.466 + RDebug::Printf("RMdaDevSound::CBody::PausePlayBuffer %s", iState.Name());
1.467 +#endif
1.468 + switch(iState)
1.469 + {
1.470 + case ENotReady:
1.471 + Panic(EDeviceNotOpened);
1.472 + break;
1.473 +
1.474 + case EStopped:
1.475 + // Driver is not playing so pause in s/w
1.476 + break;
1.477 +
1.478 + case ERecording:
1.479 + case ERecordingPausedInHw:
1.480 + case ERecordingPausedInSw:
1.481 + Panic(EBadState);
1.482 + break;
1.483 +
1.484 + case EPlayingPausedInHw: // ie. Play request pending on h/w and paused
1.485 + case EPlayingPausedInSw: // ie. Driver not playing or paused
1.486 + // Already paused so nothing to do.
1.487 + break;
1.488 + case EPlayingUnderrun:
1.489 + iState = EPlayingPausedInSw;
1.490 + break;
1.491 +
1.492 + case EPlaying:
1.493 + {
1.494 + iPauseTime = CurrentTimeInMsec();
1.495 + iPausedBytesPlayed = BytesPlayed64();
1.496 + TInt res = iPlaySoundDevice.Pause();
1.497 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.498 + RDebug::Printf("iPlaySoundDevice.Pause res = %d", res);
1.499 + #endif
1.500 + if(res == KErrNone)
1.501 + {
1.502 + iState = EPlayingPausedInHw;
1.503 + }
1.504 + else
1.505 + {
1.506 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.507 + RDebug::Printf("msp PausePlayBuffer hw pause unexpectedly failed, doing sw pause");
1.508 + #endif
1.509 + iState = EPlayingPausedInSw;
1.510 + }
1.511 + break;
1.512 + }
1.513 +
1.514 + default:
1.515 + Panic(EBadState);
1.516 + break;
1.517 + }
1.518 +
1.519 + return;
1.520 + }
1.521 +
1.522 +void RMdaDevSound::CBody::ResumePlaying()
1.523 + {
1.524 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.525 + RDebug::Printf("RMdaDevSound::CBody::ResumePlaying %s", iState.Name());
1.526 + #endif
1.527 + __ASSERT_DEBUG(iPlaySoundDevice.Handle(), Panic(EDeviceNotOpened));
1.528 +
1.529 + switch(iState)
1.530 + {
1.531 + case ENotReady:
1.532 + Panic(EDeviceNotOpened);
1.533 + break;
1.534 +
1.535 + case EStopped:
1.536 + // No change
1.537 + break;
1.538 +
1.539 + case ERecording:
1.540 + case ERecordingPausedInHw:
1.541 + case ERecordingPausedInSw:
1.542 + Panic(EBadState);
1.543 + break;
1.544 +
1.545 + case EPlaying:
1.546 + // No change
1.547 + break;
1.548 +
1.549 + case EPlayingPausedInHw: // ie. Play request pending on h/w and paused
1.550 + {
1.551 + // Re-enable reporting of KErrUnderflow (will re-raise KErrUnderflow if nothing to start playing).
1.552 + iUnderFlowReportedSinceLastPlayOrRecordRequest = EFalse;
1.553 +
1.554 + TInt res = iPlaySoundDevice.Resume();
1.555 +#ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.556 + RDebug::Printf("ResumePlayBuffer EPlayingPausedInHw res = %d", res);
1.557 +#endif
1.558 + if(res == KErrNone)
1.559 + {
1.560 + // Resume ok so a pending request will complete
1.561 + iState = EPlaying;
1.562 + // Update iStartTime to allow for time spent paused
1.563 + TUint32 curTime = CurrentTimeInMsec();
1.564 + TUint32 timePaused = (curTime >= iPauseTime) ? (curTime-iPauseTime) : (KMaxTUint32 - (iPauseTime-curTime));
1.565 + iStartTime += timePaused; // nb. It is harmless if this wraps.
1.566 + }
1.567 + else
1.568 + {
1.569 + // Resume failed, therefore driver is not playing
1.570 + // No need to update iStartTime/iPauseTime because these are only used within a driver request
1.571 + // Change state to Stopped
1.572 + iState = EStopped;
1.573 + // Attempt to start a new (pending) request.
1.574 + StartPlayersAndUpdateState();
1.575 + }
1.576 + break;
1.577 + }
1.578 + case EPlayingPausedInSw: // ie. Driver not playing/paused
1.579 + {
1.580 + // Driver not playing
1.581 + // Re-enable reporting of KErrUnderflow (will re-raise KErrUnderflow if nothing to start playing).
1.582 + iUnderFlowReportedSinceLastPlayOrRecordRequest = EFalse;
1.583 + // No need to update iStartTime/iPauseTime because these are only used within a driver request
1.584 + // Change state to Stopped
1.585 + iState = EStopped;
1.586 + // Attempt to start a new (pending) request.
1.587 + StartPlayersAndUpdateState();
1.588 + break;
1.589 + }
1.590 + case EPlayingUnderrun:
1.591 + break;
1.592 +
1.593 + default:
1.594 + Panic(EBadState);
1.595 + break;
1.596 + }
1.597 +
1.598 + return;
1.599 + }
1.600 +
1.601 +void RMdaDevSound::CBody::PauseRecordBuffer()
1.602 + {
1.603 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.604 + RDebug::Printf("RMdaDevSound::CBody::PauseRecordBuffer %s", iState.Name());
1.605 + #endif
1.606 +
1.607 + switch(iState)
1.608 + {
1.609 + case ENotReady:
1.610 + Panic(EDeviceNotOpened);
1.611 + break;
1.612 +
1.613 + case EStopped:
1.614 + // Driver is not recording so pause in s/w
1.615 + // Do not pause because that will cause problems when CAudioDevice::Pause calls
1.616 + break;
1.617 +
1.618 + case ERecording:
1.619 + {
1.620 + TInt res = iRecordSoundDevice.Pause();
1.621 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.622 + RDebug::Printf("PauseRecordBuffer EPlaying res = %d", res);
1.623 + #endif
1.624 + if(res == KErrNone)
1.625 + {
1.626 + iState = ERecordingPausedInHw;
1.627 + }
1.628 + else
1.629 + {
1.630 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.631 + RDebug::Printf("PauseRecordBuffer hw pause unexpectedly failed, doing sw pause");
1.632 + #endif
1.633 + iState = ERecordingPausedInSw;
1.634 + }
1.635 + break;
1.636 + }
1.637 +
1.638 + case ERecordingPausedInHw:
1.639 + case ERecordingPausedInSw:
1.640 + // Already paused so nothing to do.
1.641 + break;
1.642 +
1.643 + case EPlaying:
1.644 + case EPlayingPausedInHw: // ie. Play request pending on h/w and paused
1.645 + Panic(EBadState);
1.646 + break;
1.647 +
1.648 + case EPlayingPausedInSw:
1.649 + // This is an ugly hack to maintain compatibility with CAudioDevice::Pause which
1.650 + // calls both PausePlayBuffer and PauseRecordBuffer whilst in stopped, then later calls ResumePlaying
1.651 + break;
1.652 + case EPlayingUnderrun: // ie. Play request pending on h/w and paused
1.653 + Panic(EBadState);
1.654 + break;
1.655 +
1.656 + default:
1.657 + Panic(EBadState);
1.658 + break;
1.659 + }
1.660 +
1.661 + return;
1.662 + }
1.663 +
1.664 +void RMdaDevSound::CBody::ResumeRecording()
1.665 + {
1.666 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.667 + RDebug::Printf("RMdaDevSound::CBody::ResumeRecording %s", iState.Name());
1.668 + #endif
1.669 + __ASSERT_DEBUG(iPlaySoundDevice.Handle(), Panic(EDeviceNotOpened));
1.670 +
1.671 + switch(iState)
1.672 + {
1.673 + case ENotReady:
1.674 + Panic(EDeviceNotOpened);
1.675 + break;
1.676 +
1.677 + case EStopped:
1.678 + // No change
1.679 + break;
1.680 +
1.681 + case ERecording:
1.682 + // No change
1.683 + break;
1.684 +
1.685 + case ERecordingPausedInHw:
1.686 + {
1.687 + TInt res = iRecordSoundDevice.Resume();
1.688 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.689 + RDebug::Printf("ResumeRecordBuffer ERecordingPausedInHw res = %d", res);
1.690 + #endif
1.691 + if(res == KErrNone)
1.692 + {
1.693 + // Resume ok so a pending request will complete
1.694 + iState = ERecording;
1.695 + }
1.696 + else
1.697 + {
1.698 + iState = EStopped;
1.699 + // Resume failed, so attempt to start a new (pending) request.
1.700 + // If this works, it will update the state to ERecording.
1.701 + StartRecordRequest();
1.702 + }
1.703 + break;
1.704 + }
1.705 + case ERecordingPausedInSw:
1.706 + {
1.707 + // Update state to stopped and attempt to start any pending request
1.708 + iState = EStopped;
1.709 + // If this works, it will update the state to ERecording.
1.710 + StartRecordRequest();
1.711 + break;
1.712 + }
1.713 +
1.714 + case EPlaying:
1.715 + case EPlayingPausedInHw: // ie. Play request pending on h/w and paused
1.716 + case EPlayingPausedInSw: // ie. Driver not playing/paused
1.717 + case EPlayingUnderrun:
1.718 + default:
1.719 + Panic(EBadState);
1.720 + break;
1.721 + }
1.722 +
1.723 + return;
1.724 +
1.725 +
1.726 + }
1.727 +
1.728 +TInt RMdaDevSound::CBody::GetTimePlayed(TTimeIntervalMicroSeconds& aTimePlayed)
1.729 + {
1.730 + __ASSERT_DEBUG(iPlaySoundDevice.Handle(), Panic(EDeviceNotOpened));
1.731 +
1.732 +
1.733 + TUint64 bytesPlayed = BytesPlayed64();
1.734 +
1.735 + TUint64 timePlayed = 1000 * 1000 * bytesPlayed / (iPlayFormatData.iSampleRate * iPlayFormatData.iRequestedChannels * KBytesPerSample);
1.736 +
1.737 + aTimePlayed = TTimeIntervalMicroSeconds(timePlayed);
1.738 +
1.739 + return KErrNone;
1.740 + }
1.741 +
1.742 +
1.743 +void RMdaDevSound::CBody::FormatsSupported(TSoundFormatsSupportedBuf& aFormatsSupported, RSoundSc& aSoundDevice)
1.744 + {
1.745 + TSoundFormatsSupportedV02Buf supportedFormat;
1.746 + aSoundDevice.Caps(supportedFormat);
1.747 + TUint32 rates = supportedFormat().iRates;
1.748 +
1.749 + for(TInt i = KNumSampleRates-1; i > 0 ;i--)//min to max
1.750 + {
1.751 + if(rates & KRateEnumLookup[i].iRateConstant)
1.752 + {
1.753 + aFormatsSupported().iMinRate = KRateEnumLookup[i].iRate;
1.754 + break;
1.755 + }
1.756 + }
1.757 + for(TInt i = 0; i < KNumSampleRates; i++)//max to min
1.758 + {
1.759 + if(rates & KRateEnumLookup[i].iRateConstant)
1.760 + {
1.761 + aFormatsSupported().iMaxRate = KRateEnumLookup[i].iRate;
1.762 + break;
1.763 + }
1.764 + }
1.765 + TUint32 enc = supportedFormat().iEncodings;
1.766 +
1.767 + if (enc & KSoundEncoding16BitPCM)
1.768 + {
1.769 + aFormatsSupported().iEncodings = EMdaSoundEncoding16BitPCM;// Always defaults to this
1.770 + }
1.771 + if (enc & KSoundEncoding8BitPCM)
1.772 + {
1.773 + aFormatsSupported().iEncodings |= EMdaSoundEncoding8BitPCM;
1.774 + }
1.775 + TUint32 channels = supportedFormat().iChannels;
1.776 +
1.777 + if (channels & KSoundStereoChannel)
1.778 + {
1.779 + aFormatsSupported().iChannels = 2;
1.780 + }
1.781 + else
1.782 + {
1.783 + aFormatsSupported().iChannels = 1;
1.784 + }
1.785 + aFormatsSupported().iMinBufferSize = supportedFormat().iRequestMinSize;
1.786 + aFormatsSupported().iMaxBufferSize = KMaxBufferSize;
1.787 + aFormatsSupported().iMinVolume = 0;
1.788 + aFormatsSupported().iMaxVolume = KSoundMaxVolume;
1.789 + }
1.790 +
1.791 +void RMdaDevSound::CBody::GetFormat(TCurrentSoundFormatBuf& aFormat,
1.792 + RSoundSc& /*aSoundDevice*/,
1.793 + const TFormatData &aFormatData)
1.794 + {
1.795 + // always return the requested, or the initial, not current device setting
1.796 + aFormat().iChannels = aFormatData.iRequestedChannels; // never clear if this is bitmap or value, but effectively the same
1.797 + aFormat().iRate = aFormatData.iSampleRate;
1.798 + }
1.799 +
1.800 +void RMdaDevSound::CBody::StartPlayersAndUpdateState()
1.801 + {
1.802 + __ASSERT_DEBUG(iPlaySoundDevice.Handle(), Panic(EDeviceNotOpened));
1.803 +
1.804 + switch(iState)
1.805 + {
1.806 + case ENotReady:
1.807 + Panic(EDeviceNotOpened);
1.808 + break;
1.809 +
1.810 + case EStopped:
1.811 + // Allow following code to queue more driver play requests and check for stopped
1.812 + break;
1.813 +
1.814 + case ERecording:
1.815 + case ERecordingPausedInHw:
1.816 + case ERecordingPausedInSw:
1.817 + Panic(EBadState);
1.818 + break;
1.819 +
1.820 + case EPlaying:
1.821 + // Allow following code to queue more driver play requests and check for stopped
1.822 + break;
1.823 + case EPlayingPausedInHw: // ie. Play request pending on h/w and paused
1.824 + // Allow following code to queue more driver play requests
1.825 + break;
1.826 +
1.827 + case EPlayingPausedInSw:
1.828 + // Paused but driver not playing+paused, therefore do not queue new requests until ResumePlaying
1.829 + return;
1.830 + case EPlayingUnderrun:
1.831 + break;
1.832 +
1.833 + default:
1.834 + Panic(EBadState);
1.835 + break;
1.836 + }
1.837 +
1.838 + // iState is now either EStopped, EPlaying or EPlayingPausedInHw
1.839 + __ASSERT_DEBUG(((iState == EStopped) || (iState == EPlaying) || (iState == EPlayingPausedInHw) || (iState == EPlayingUnderrun)), Panic(EBadState));
1.840 +
1.841 + while( (iClientPlayData.Length() != 0) && (! iFreePlayers.IsEmpty()))
1.842 + {
1.843 + // More data to play and more players, so issue another request
1.844 +
1.845 + bool wasIdle = iFreePlayers.IsFull();
1.846 + // Get a free player
1.847 + CPlayer *player = iFreePlayers.Pop();
1.848 + // Calculate length of request
1.849 + TUint32 lengthToPlay = iClientPlayData.Length();
1.850 + if(lengthToPlay > iDeviceBufferLength)
1.851 + {
1.852 + lengthToPlay = iDeviceBufferLength;
1.853 + }
1.854 +
1.855 + // Remember request length, so we can update bytes played when it finishes
1.856 + iActivePlayRequestSizes.Push(lengthToPlay);
1.857 +
1.858 + // Find offset to copy data to
1.859 + TUint playerIndex = player->GetPlayerIndex();
1.860 + ASSERT(playerIndex < KPlaySharedChunkBuffers);
1.861 + TUint chunkOffset = iPlayBufferConfig.iBufferOffsetList[playerIndex];
1.862 +
1.863 + // Copy data
1.864 + TPtr8 destPtr(iPlayChunk.Base()+ chunkOffset, 0, iDeviceBufferLength);
1.865 + destPtr.Copy(iClientPlayData.Mid(0, lengthToPlay));
1.866 +
1.867 + // Update iClientPlayData to remove the data just queued
1.868 + iClientPlayData.Set(iClientPlayData.Right(iClientPlayData.Length()-lengthToPlay));
1.869 +
1.870 + // Start the CPlayer
1.871 + player->PlayData(chunkOffset, lengthToPlay);
1.872 + if(wasIdle)
1.873 + {
1.874 + iState = EPlaying;
1.875 + iStartTime = CurrentTimeInMsec();
1.876 +
1.877 + }
1.878 +
1.879 + }
1.880 +
1.881 + // Check if the client request is now complete
1.882 + if(iClientPlayData.Length() == 0 && iClientPlayStatus)
1.883 + {
1.884 + // We have queued all the client play data to the driver so we can now complete the client request.
1.885 + // If actual playback fails, we will notify the client via the Play Error notification mechanism.
1.886 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.887 + RDebug::Printf("RMdaDevSound::CBody::StartPlayersAndUpdateState completing client request");
1.888 + #endif
1.889 + User::RequestComplete(iClientPlayStatus, KErrNone); // This call also sets iClientPlayStatus to NULL
1.890 + }
1.891 +
1.892 + //nb. iState is now either EStopped, EPlaying or EPlayingPausedInHw (see previous switch and assert)
1.893 + if(iState != EPlayingPausedInHw)
1.894 + {
1.895 + if(iFreePlayers.IsFull())
1.896 + {
1.897 + // Free fifo is full, therefore there are no active players
1.898 + iState = EPlayingUnderrun;
1.899 + if(! iUnderFlowReportedSinceLastPlayOrRecordRequest)
1.900 + {
1.901 + // We report KErrUnderflow if we have not already reported it since the last PlayData call.
1.902 + // Note that
1.903 + // i) We do NOT report driver underflows.
1.904 + // ii) We report underflow when we run out of data to pass to the driver.
1.905 + // iii) We throttle this reporting
1.906 + // iv) We WILL report KErrUnderflow if already stopped and asked to play a zero length buffer
1.907 + // The last point is required because the client maps a manual stop command into a devsound play with a
1.908 + // zero length buffer and the last buffer flag set, this in turn is mapped to a Playdata calll with an empty buffer
1.909 + // which is expected to complete ok and cause a KErrUnderflow error to be reported...
1.910 + iUnderFlowReportedSinceLastPlayOrRecordRequest = ETrue;
1.911 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.912 + RDebug::Printf("RMdaDevSound::CBody::StartPlayersAndUpdateState stopped and iUnderFlowReportedSinceLastPlayOrRecordRequest false so raising KErrUnderflow");
1.913 + #endif
1.914 +
1.915 + // Discard any saved trailing data (ie. data saved due driver requiring all requests to be a multiple of iRequestMinSize).
1.916 + // This maybe because client is delibrately letting us underflow to play silence. In that case we do not want to
1.917 + // play the trailing data at the beginning of the new data issued after the silence...
1.918 + iSavedTrailingData.SetLength(0);
1.919 +
1.920 + SoundDeviceError(KErrUnderflow);
1.921 + }
1.922 + }
1.923 + else
1.924 + {
1.925 + // Free fifo not full, therefore there are active players
1.926 + iState = EPlaying;
1.927 + }
1.928 + }
1.929 + return;
1.930 + }
1.931 +
1.932 +TInt RMdaDevSound::CBody::SetFormat(const TCurrentSoundFormatBuf& aFormat,
1.933 + RSoundSc& aSoundDevice,
1.934 + TFormatData &aFormatData)
1.935 + {
1.936 + TInt err = KErrNotFound;
1.937 + TCurrentSoundFormatV02Buf formatBuf;
1.938 +
1.939 + delete aFormatData.iConverter;
1.940 + aFormatData.iConverter = NULL; // setting this to NULL indicates we are not using converter. No other flag
1.941 + iConvertedPlayData.Close();
1.942 +
1.943 + TInt wantedRate = aFormat().iRate;
1.944 + for(TInt index = 0; index < KNumSampleRates; index++ )
1.945 + {
1.946 + if(wantedRate == KRateEnumLookup[index].iRate)
1.947 + {
1.948 + formatBuf().iRate = KRateEnumLookup[index].iRateEnum;
1.949 + aFormatData.iSampleRate = wantedRate;
1.950 + err = KErrNone;
1.951 + break;
1.952 + }
1.953 + }
1.954 +
1.955 + if(err == KErrNone)
1.956 + {
1.957 + // Assume, for now, we support the requested channels and rate
1.958 + aFormatData.iActualChannels = aFormatData.iRequestedChannels;
1.959 + aFormatData.iActualRate = aFormatData.iSampleRate;
1.960 +
1.961 + // Attempt to configure driver
1.962 + formatBuf().iChannels = aFormat().iChannels;
1.963 + formatBuf().iEncoding = ESoundEncoding16BitPCM;
1.964 + formatBuf().iDataFormat = ESoundDataFormatInterleaved;
1.965 + err = aSoundDevice.SetAudioFormat(formatBuf);
1.966 + #if defined(SYMBIAN_SOUNDADAPTER_FORCECDRATES) || defined (SYMBIAN_SOUNDADAPTER_FORCESTEREO)
1.967 + err = KErrNotSupported; // force Negotiate - for debugging
1.968 + #endif
1.969 + if (err==KErrNotSupported)
1.970 + {
1.971 + // don't support directly. Perhaps can rate convert?
1.972 + err = NegotiateFormat(aFormat, aSoundDevice, aFormatData);
1.973 + }
1.974 + }
1.975 + return err;
1.976 + }
1.977 +
1.978 +TInt RMdaDevSound::CBody::NegotiateFormat(const TCurrentSoundFormatBuf& aFormat,
1.979 + RSoundSc& aSoundDevice,
1.980 + TFormatData &aFormatData)
1.981 + {
1.982 + ASSERT(!aFormatData.iConverter); // we don't clear on fail - so assuming NULL to start with
1.983 +
1.984 + TInt err = KErrNotFound;
1.985 + TCurrentSoundFormatV02Buf formatBuf;
1.986 +
1.987 + // find out first what the driver supports
1.988 + TSoundFormatsSupportedV02Buf supportedFormat;
1.989 + aSoundDevice.Caps(supportedFormat);
1.990 + TUint32 supportedRates = supportedFormat().iRates;
1.991 + #ifdef SYMBIAN_SOUNDADAPTER_FORCECDRATES
1.992 + supportedRates &= KSoundRate11025Hz| KSoundRate22050Hz | KSoundRate44100Hz; // only use CD rates - for debugging
1.993 + #endif
1.994 +
1.995 + // For PlayCase:
1.996 + // first try to find the first rate below or equal to the requested that is supported
1.997 + // initially go down and be fussy, but if we pass the requested rate find the first that
1.998 + // is supported
1.999 + // For RecordCase:
1.1000 + // We want the next rate above consistently - we go down from this to the requested rate.
1.1001 + // If there is one, we don't support - we _never_ upsample.
1.1002 + // note that the table is given in descending order, so we start with the highest
1.1003 + TInt wantedRate = aFormat().iRate;
1.1004 + TInt takeTheFirst = EFalse;
1.1005 + TInt nextUpValidIndex = -1;
1.1006 + for(TInt index = 0; index < KNumSampleRates; index++ )
1.1007 + {
1.1008 + TBool lookingAtRequestedRate = wantedRate == KRateEnumLookup[index].iRate;
1.1009 + TSoundRate wantedEnum = KRateEnumLookup[index].iRateEnum;
1.1010 + TUint32 equivBitmap = KRateEnumLookup[index].iRateConstant;
1.1011 + TBool isSupported = (equivBitmap & supportedRates) != EFalse;
1.1012 + if (lookingAtRequestedRate || takeTheFirst)
1.1013 + {
1.1014 + if (isSupported)
1.1015 + {
1.1016 + // this rate is supported
1.1017 + formatBuf().iRate = wantedEnum;
1.1018 + aFormatData.iActualRate = KRateEnumLookup[index].iRate;
1.1019 + err = KErrNone;
1.1020 + break;
1.1021 + }
1.1022 + }
1.1023 + else if (!takeTheFirst)
1.1024 + {
1.1025 + // while we are still looking for the rate, want to cache any supported index
1.1026 + // at end of loop, this will be the first rate above ours that is supported
1.1027 + // use for fallback if required
1.1028 + if (isSupported)
1.1029 + {
1.1030 + nextUpValidIndex = index;
1.1031 + }
1.1032 + }
1.1033 + if (lookingAtRequestedRate)
1.1034 + {
1.1035 + // if we get this far we've gone passed the wanted rate. For play we enable
1.1036 + // "takeTheFirst". For record we just abort.
1.1037 + if (&aSoundDevice==&iPlaySoundDevice)
1.1038 + {
1.1039 + takeTheFirst = ETrue;
1.1040 + }
1.1041 + else
1.1042 + {
1.1043 + break;
1.1044 + }
1.1045 + }
1.1046 + }
1.1047 +
1.1048 + if (err)
1.1049 + {
1.1050 + // if there is one above the requested rate, use that
1.1051 + if (nextUpValidIndex>=0)
1.1052 + {
1.1053 + TSoundRate wantedEnum = KRateEnumLookup[nextUpValidIndex].iRateEnum;
1.1054 + formatBuf().iRate = wantedEnum;
1.1055 + aFormatData.iActualRate = KRateEnumLookup[nextUpValidIndex].iRate;
1.1056 + err = KErrNone;
1.1057 + }
1.1058 + }
1.1059 +
1.1060 + if (err)
1.1061 + {
1.1062 + // should have something!
1.1063 + return err;
1.1064 + }
1.1065 +
1.1066 + aFormatData.iSampleRate = wantedRate; // iSampleRate is our requested/apparent rate, not the device rate.
1.1067 +
1.1068 + TUint32 channelsSupported = supportedFormat().iChannels;
1.1069 + #ifdef SYMBIAN_SOUNDADAPTER_FORCESTEREO
1.1070 + channelsSupported &= KSoundStereoChannel; // don't use mono - for debugging
1.1071 + #endif
1.1072 + if(KSoundAdapterForceStereo==1)
1.1073 + {
1.1074 + channelsSupported &= KSoundStereoChannel;
1.1075 +#ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1076 + RDebug::Print(_L("Added stereo support."));
1.1077 +#endif
1.1078 + }
1.1079 + if (aFormat().iChannels == 1)
1.1080 + {
1.1081 + aFormatData.iRequestedChannels = 1;
1.1082 + // want mono
1.1083 + if (channelsSupported & KSoundMonoChannel)
1.1084 + {
1.1085 + // mono is supported, as usual
1.1086 + aFormatData.iActualChannels = 1;
1.1087 + }
1.1088 + else if (channelsSupported & KSoundStereoChannel)
1.1089 + {
1.1090 + aFormatData.iActualChannels = 2;
1.1091 + }
1.1092 + else
1.1093 + {
1.1094 + return KErrNotSupported; // should not get this far for real
1.1095 + }
1.1096 + }
1.1097 + else if (aFormat().iChannels == 2)
1.1098 + {
1.1099 + aFormatData.iRequestedChannels = 2;
1.1100 + // want stereo
1.1101 + if (channelsSupported & KSoundStereoChannel)
1.1102 + {
1.1103 + // stereo is supported, as usual
1.1104 + aFormatData.iActualChannels = 2;
1.1105 + }
1.1106 + else if (channelsSupported & KSoundMonoChannel)
1.1107 + {
1.1108 + aFormatData.iActualChannels = 1;
1.1109 + }
1.1110 + else
1.1111 + {
1.1112 + return KErrNotSupported; // should not get this far for real
1.1113 + }
1.1114 + }
1.1115 + else
1.1116 + {
1.1117 + return KErrNotSupported; // unknown number of channels requested!
1.1118 + }
1.1119 +
1.1120 + formatBuf().iChannels = aFormatData.iActualChannels;
1.1121 +
1.1122 + formatBuf().iEncoding = ESoundEncoding16BitPCM;
1.1123 + formatBuf().iDataFormat = ESoundDataFormatInterleaved;
1.1124 + err = aSoundDevice.SetAudioFormat(formatBuf);
1.1125 +
1.1126 + if (!err)
1.1127 + {
1.1128 + ASSERT(!aFormatData.iConverter); // pre-condition at top of function anyway
1.1129 + if (&aSoundDevice==&iPlaySoundDevice)
1.1130 + {
1.1131 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1132 + RDebug::Print(_L("RMdaDevSound::CBody::NegotiateFormat: Convert:CreateL from %d/%d to %d/%d"),
1.1133 + aFormatData.iSampleRate, aFormatData.iRequestedChannels,
1.1134 + aFormatData.iActualRate, aFormatData.iActualChannels);
1.1135 + #endif
1.1136 + // when playing we convert from requested to actual
1.1137 + TRAP(err, aFormatData.iConverter = CChannelAndSampleRateConverter::CreateL(aFormatData.iSampleRate,
1.1138 + aFormatData.iRequestedChannels,
1.1139 + aFormatData.iActualRate,
1.1140 + aFormatData.iActualChannels));
1.1141 + }
1.1142 + else
1.1143 + {
1.1144 + // when recording we convert from actual to requested
1.1145 + TInt outputRateToUse = aFormatData.iSampleRate;
1.1146 + #ifdef SYMBIAN_SKIP_RESAMPLE_ON_RECORD
1.1147 + // with this macro just channel convert at most
1.1148 + outputRateToUse = aFormatData.iActualRate;
1.1149 + #endif
1.1150 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1151 + RDebug::Print(_L("RMdaDevSound::CBody::NegotiateFormat: Convert:CreateL from %d/%d to %d/%d"),
1.1152 + aFormatData.iActualRate, aFormatData.iActualChannels,
1.1153 + aFormatData.iSampleRate, aFormatData.iRequestedChannels);
1.1154 + #endif
1.1155 + TRAP(err, aFormatData.iConverter = CChannelAndSampleRateConverter::CreateL(aFormatData.iActualRate,
1.1156 + aFormatData.iActualChannels,
1.1157 + outputRateToUse,
1.1158 + aFormatData.iRequestedChannels));
1.1159 + }
1.1160 + }
1.1161 + if(err != KErrNone)
1.1162 + {
1.1163 + delete aFormatData.iConverter;
1.1164 + aFormatData.iConverter= NULL;
1.1165 + iConvertedPlayData.Close();
1.1166 + }
1.1167 +
1.1168 + return err;
1.1169 + }
1.1170 +
1.1171 +void RMdaDevSound::CBody::StartRecordRequest()
1.1172 + {
1.1173 + __ASSERT_DEBUG(iRecordSoundDevice.Handle(), Panic(EDeviceNotOpened));
1.1174 +
1.1175 + iRecorder->RecordData(iBufferLength);
1.1176 + }
1.1177 +
1.1178 +// Note both InRecordMode and InPlayMode return EFalse for ENotReady and EStopped
1.1179 +TBool RMdaDevSound::CBody::InRecordMode()const
1.1180 + {
1.1181 + switch(iState)
1.1182 + {
1.1183 + case ENotReady:
1.1184 + case EStopped:
1.1185 + return EFalse;
1.1186 +
1.1187 + case ERecording:
1.1188 + case ERecordingPausedInHw:
1.1189 + case ERecordingPausedInSw:
1.1190 + return ETrue;
1.1191 +
1.1192 + case EPlaying:
1.1193 + case EPlayingPausedInHw:
1.1194 + case EPlayingPausedInSw:
1.1195 + case EPlayingUnderrun:
1.1196 + return EFalse;
1.1197 +
1.1198 + default:
1.1199 + Panic(EBadState);
1.1200 + break;
1.1201 + }
1.1202 + return EFalse;
1.1203 + }
1.1204 +
1.1205 +TBool RMdaDevSound::CBody::InPlayMode() const
1.1206 + {
1.1207 + switch(iState)
1.1208 + {
1.1209 + case ENotReady:
1.1210 + case EStopped:
1.1211 + return EFalse;
1.1212 +
1.1213 + case ERecording:
1.1214 + case ERecordingPausedInHw:
1.1215 + case ERecordingPausedInSw:
1.1216 + return EFalse;
1.1217 +
1.1218 + case EPlaying:
1.1219 + case EPlayingPausedInHw:
1.1220 + case EPlayingPausedInSw:
1.1221 + case EPlayingUnderrun:
1.1222 + return ETrue;
1.1223 +
1.1224 + default:
1.1225 + Panic(EBadState);
1.1226 + break;
1.1227 + }
1.1228 +
1.1229 + return EFalse;
1.1230 + }
1.1231 +
1.1232 +
1.1233 +TUint32 RMdaDevSound::CBody::CurrentTimeInMsec() const
1.1234 + {
1.1235 + TUint64 tmp = User::NTickCount();
1.1236 + tmp *= iNTickPeriodInUsec;
1.1237 + tmp /= 1000;
1.1238 + return TUint32(tmp);
1.1239 + }
1.1240 +
1.1241 +void RMdaDevSound::CBody::PlayFormatsSupported(TSoundFormatsSupportedBuf& aFormatsSupported)
1.1242 + {
1.1243 + __ASSERT_DEBUG(iPlaySoundDevice.Handle(), Panic(EDeviceNotOpened));
1.1244 + FormatsSupported(aFormatsSupported, iPlaySoundDevice);
1.1245 + }
1.1246 +
1.1247 +void RMdaDevSound::CBody::GetPlayFormat(TCurrentSoundFormatBuf& aFormat)
1.1248 + {
1.1249 + __ASSERT_DEBUG(iPlaySoundDevice.Handle(), Panic(EDeviceNotOpened));
1.1250 + GetFormat(aFormat, iPlaySoundDevice, iPlayFormatData);
1.1251 + }
1.1252 +
1.1253 +TInt RMdaDevSound::CBody::SetPlayFormat(const TCurrentSoundFormatBuf& aFormat)
1.1254 + {
1.1255 + __ASSERT_DEBUG(iPlaySoundDevice.Handle(), Panic(EDeviceNotOpened));
1.1256 + return SetFormat(aFormat, iPlaySoundDevice, iPlayFormatData);
1.1257 + }
1.1258 +
1.1259 +void RMdaDevSound::CBody::RecordFormatsSupported(TSoundFormatsSupportedBuf& aFormatsSupported)
1.1260 + {
1.1261 + __ASSERT_DEBUG(iRecordSoundDevice.Handle(), Panic(EDeviceNotOpened));
1.1262 + FormatsSupported(aFormatsSupported, iRecordSoundDevice);
1.1263 + }
1.1264 +
1.1265 +void RMdaDevSound::CBody::GetRecordFormat(TCurrentSoundFormatBuf& aFormat)
1.1266 + {
1.1267 + __ASSERT_DEBUG(iRecordSoundDevice.Handle(), Panic(EDeviceNotOpened));
1.1268 + GetFormat(aFormat, iRecordSoundDevice, iRecordFormatData);
1.1269 + }
1.1270 +
1.1271 +TInt RMdaDevSound::CBody::SetRecordFormat(const TCurrentSoundFormatBuf& aFormat)
1.1272 + {
1.1273 + __ASSERT_DEBUG(iRecordSoundDevice.Handle(), Panic(EDeviceNotOpened));
1.1274 + return SetFormat(aFormat, iRecordSoundDevice, iRecordFormatData);
1.1275 + }
1.1276 +
1.1277 +void RMdaDevSound::CBody::Close()
1.1278 + {
1.1279 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1280 + RDebug::Printf("void RMdaDevSound::CBody::Close() started");
1.1281 + #endif
1.1282 + iBufferOffset = -1;
1.1283 + iBufferLength = 0;
1.1284 +
1.1285 + if(iPlaySoundDevice.Handle() != KNullHandle)
1.1286 + {
1.1287 + // Make sure all player objects are idle
1.1288 + CancelPlayData();
1.1289 + iPlayChunk.Close();
1.1290 + iPlaySoundDevice.Close();
1.1291 + }
1.1292 +
1.1293 + if(iRecordSoundDevice.Handle() != KNullHandle)
1.1294 + {
1.1295 + CancelRecordData();
1.1296 + iRecordChunk.Close();
1.1297 + iRecordSoundDevice.Close();
1.1298 + }
1.1299 +
1.1300 + iState = ENotReady;
1.1301 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1302 + RDebug::Printf("void RMdaDevSound::CBody::Close() ended");
1.1303 + #endif
1.1304 + }
1.1305 +
1.1306 +TInt RMdaDevSound::CBody::Handle()
1.1307 + {//This method is actually used to check whether the device is opened. Below logic should work
1.1308 + if(iPlaySoundDevice.Handle())
1.1309 + {
1.1310 + return iPlaySoundDevice.Handle();
1.1311 + }
1.1312 + if(iRecordSoundDevice.Handle())
1.1313 + {
1.1314 + return iRecordSoundDevice.Handle();
1.1315 + }
1.1316 + return 0;
1.1317 + }
1.1318 +
1.1319 +
1.1320 +void RMdaDevSound::CBody::PlayData(TRequestStatus& aStatus, const TDesC8& aData)
1.1321 + {
1.1322 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1323 + RDebug::Printf("RMdaDevSound::CBody::PlayData(0x%x,%d) State=%s Handle=%d.",&aStatus,
1.1324 + aData.Length(), iState.Name(), iPlayChunk.Handle());
1.1325 + #endif
1.1326 +
1.1327 + __ASSERT_DEBUG(iPlaySoundDevice.Handle(), Panic(EDeviceNotOpened));
1.1328 + aStatus = KRequestPending;
1.1329 +
1.1330 + if((iClientPlayStatus != NULL) || InRecordMode())
1.1331 + {
1.1332 + // We only support one outstanding request
1.1333 + // No support for simultaneous play and record in RMdaDevSound
1.1334 + TRequestStatus *pRequest = &aStatus;
1.1335 + User::RequestComplete(pRequest, KErrInUse);
1.1336 + return;
1.1337 + }
1.1338 + iClientPlayStatus = &aStatus;//store the status of datapath player
1.1339 +
1.1340 + if(iPlayFormatData.iConverter || iSavedTrailingData.Length() != 0)
1.1341 + {
1.1342 + // Need a conversion buffer
1.1343 + // Needs to hold any trailing data truncated from the previous request (due
1.1344 + // to alignment requirements) and either the new data, or the new rate adapted data
1.1345 + TUint32 spaceRequired = iSavedTrailingData.Length();
1.1346 + if(iPlayFormatData.iConverter)
1.1347 + {
1.1348 + // Doing rate conversion so also need space for the converted data
1.1349 + spaceRequired += iPlayFormatData.iConverter->MaxConvertBufferSize(aData.Length());
1.1350 + }
1.1351 + else
1.1352 + {
1.1353 + // Not doing rate adaptation therefore only need to allow for the new incoming data
1.1354 + spaceRequired += aData.Length();
1.1355 + }
1.1356 + // Check if existing buffer exists and is big enough
1.1357 + if(iConvertedPlayData.MaxLength() < spaceRequired)
1.1358 + {
1.1359 + iConvertedPlayData.Close();
1.1360 + TInt err = iConvertedPlayData.Create(spaceRequired);
1.1361 + if(err)
1.1362 + {
1.1363 + User::RequestComplete(iClientPlayStatus, err);
1.1364 + return;
1.1365 + }
1.1366 + }
1.1367 +
1.1368 + // Truncate iConvertedPlayData and copy in saved trailing data (if any)
1.1369 + iConvertedPlayData = iSavedTrailingData;
1.1370 + iSavedTrailingData.SetLength(0);
1.1371 +
1.1372 + // Now append rate adapted data or incoming data
1.1373 + if (iPlayFormatData.iConverter)
1.1374 + {
1.1375 + // The convertor will panic if it fails to convert any data, therefore
1.1376 + // we avoid passing it an empty source buffer
1.1377 + if(aData.Length() != 0)
1.1378 + {
1.1379 + TPtr8 destPtr((TUint8 *)iConvertedPlayData.Ptr()+iConvertedPlayData.Length(), 0, iConvertedPlayData.MaxLength()-iConvertedPlayData.Length());
1.1380 + TInt len = iPlayFormatData.iConverter->Convert(aData, destPtr);
1.1381 + iConvertedPlayData.SetLength(iConvertedPlayData.Length() + destPtr.Length());
1.1382 + if(len != aData.Length())
1.1383 + {
1.1384 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1385 + RDebug::Printf("RMdaDevSound::CBody::PlayData converted %d but expected to convert %d", len, aData.Length());
1.1386 + #endif
1.1387 + }
1.1388 + }
1.1389 + }
1.1390 + else
1.1391 + {
1.1392 + iConvertedPlayData.Append(aData);
1.1393 + }
1.1394 + iClientPlayData.Set(iConvertedPlayData);
1.1395 + }
1.1396 + else
1.1397 + {
1.1398 + // Do not need a conversion buffer so just aim the descriptor at the data
1.1399 + iClientPlayData.Set(aData);
1.1400 + }
1.1401 + iUnderFlowReportedSinceLastPlayOrRecordRequest = EFalse;
1.1402 +
1.1403 + // All driver requests must be an exact multiple of iRequestMinSize
1.1404 + TUint32 trailingDataLen = iClientPlayData.Length() % iRequestMinSize;
1.1405 + if(trailingDataLen)
1.1406 + {
1.1407 + // Not a multiple of iRequestMinSize, so need to truncate current request, and save trailing bytes for
1.1408 + // inclusion at the beginning of the next request
1.1409 + iSavedTrailingData = iClientPlayData.Right(trailingDataLen);
1.1410 + iClientPlayData.Set(iClientPlayData.Left(iClientPlayData.Length()-trailingDataLen));
1.1411 + }
1.1412 +
1.1413 + #ifdef SYMBIAN_FORCE_32BIT_LENGTHS
1.1414 + if (iClientPlayData.Length()%4 != 0)
1.1415 + {
1.1416 + // simulate the limitation of some hardware, where -6 is generated if the
1.1417 + // buffer length is not divisible by 4.
1.1418 + TRequestStatus *pRequest = &aStatus;
1.1419 + User::RequestComplete(pRequest, KErrArgument);
1.1420 + }
1.1421 + #endif
1.1422 +
1.1423 + iRecordChunk.Close();
1.1424 + if(!iPlayChunk.Handle())
1.1425 + {
1.1426 + //This is where we setup to play.
1.1427 + //Configure the shared chunk for two buffers with iBufferSize each
1.1428 + iPlayBufferConfig.iNumBuffers = KPlaySharedChunkBuffers;
1.1429 + iDeviceBufferLength = KPlaySharedChunkBufferSize;
1.1430 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1431 + RDebug::Printf("iDeviceBufferLength %d", iDeviceBufferLength);
1.1432 + #endif
1.1433 + iPlayBufferConfig.iFlags = 0;//data will be continuous
1.1434 + // If required, use rate converter etc
1.1435 + iPlayBufferConfig.iBufferSizeInBytes = iDeviceBufferLength;
1.1436 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1437 + RDebug::Printf("number of buffers: [%d]",iPlayBufferConfig.iNumBuffers);
1.1438 + RDebug::Printf("BufferSize in Bytes [%d]",iPlayBufferConfig.iBufferSizeInBytes);
1.1439 + #endif
1.1440 + TPckg<TPlaySharedChunkBufConfig> bufferConfigBuf(iPlayBufferConfig);
1.1441 + TInt error = iPlaySoundDevice.SetBufferChunkCreate(bufferConfigBuf,iPlayChunk);
1.1442 + if(error == KErrNone)
1.1443 + {
1.1444 + iPlaySoundDevice.GetBufferConfig(bufferConfigBuf);
1.1445 + }
1.1446 + if (error)
1.1447 + {
1.1448 + SoundDeviceError(error);
1.1449 + return;
1.1450 + }
1.1451 + }
1.1452 +
1.1453 + StartPlayersAndUpdateState();
1.1454 +
1.1455 + return;
1.1456 + }
1.1457 +
1.1458 +void RMdaDevSound::CBody::RecordData(TRequestStatus& aStatus, TDes8& aData)
1.1459 + {
1.1460 + __ASSERT_DEBUG(iRecordSoundDevice.Handle(), Panic(EDeviceNotOpened));
1.1461 + aStatus = KRequestPending;
1.1462 + if((iClientPlayStatus != NULL) || InPlayMode())
1.1463 + {
1.1464 + // We only support one outstanding request
1.1465 + // No support for simultaneous play and record in RMdaDevSound
1.1466 + TRequestStatus *pRequest = &aStatus;
1.1467 + User::RequestComplete(pRequest, KErrInUse);
1.1468 + return;
1.1469 + }
1.1470 + iClientRecordStatus = &aStatus;
1.1471 + iClientRecordData = &aData;
1.1472 + iUnderFlowReportedSinceLastPlayOrRecordRequest = EFalse;
1.1473 +
1.1474 + iPlayChunk.Close();
1.1475 + if(!iRecordChunk.Handle())
1.1476 + {
1.1477 + //Configure the shared chunk for two buffers with iBufferSize each
1.1478 + iRecordBufferConfig.iNumBuffers = KRecordMaxSharedChunkBuffers;
1.1479 + iDeviceBufferLength = KRecordSharedChunkBufferSize; // initial size - resize if needs be
1.1480 + if (iRecordFormatData.iConverter)
1.1481 + {
1.1482 + // if number of channels used differs from request, resize buffer
1.1483 + // assume we have nice rounded values for buffer.
1.1484 + if (iRecordFormatData.iActualChannels>iRecordFormatData.iRequestedChannels)
1.1485 + {
1.1486 + iDeviceBufferLength *= 2; // will record at stereo and convert to mono
1.1487 + }
1.1488 + else if (iRecordFormatData.iActualChannels<iRecordFormatData.iRequestedChannels)
1.1489 + {
1.1490 + iDeviceBufferLength /= 2; // will record at mono and convert to stereo
1.1491 + }
1.1492 + }
1.1493 + iRecordBufferConfig.iBufferSizeInBytes = iDeviceBufferLength;
1.1494 + iRecordBufferConfig.iFlags = 0;
1.1495 + TPckg<TRecordSharedChunkBufConfig> bufferConfigBuf(iRecordBufferConfig);
1.1496 + TInt error = iRecordSoundDevice.SetBufferChunkCreate(bufferConfigBuf,iRecordChunk);
1.1497 + if(error == KErrNone)
1.1498 + {
1.1499 + iRecordSoundDevice.GetBufferConfig(bufferConfigBuf);
1.1500 + }
1.1501 + else
1.1502 + {
1.1503 + SoundDeviceError(error);
1.1504 + return;
1.1505 + }
1.1506 + iState = ERecording;
1.1507 + }
1.1508 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1509 + RDebug::Printf("RMdaDevSound::CBody::RecordData,iBufferOffset[%d]",iBufferOffset);
1.1510 + #endif
1.1511 +
1.1512 + switch(iState)
1.1513 + {
1.1514 + case ENotReady:
1.1515 + Panic(EBadState);
1.1516 + break;
1.1517 +
1.1518 + case EStopped:
1.1519 + case ERecording:
1.1520 + // Either idle or recording is in progress, therefore we can issue another request
1.1521 + StartRecordRequest();
1.1522 + break;
1.1523 +
1.1524 + case ERecordingPausedInHw:
1.1525 + // Driver is paused, therefore we can issue a request which will immediately return buffered data
1.1526 + // or be aborted (in the driver) with KErrCancelled if there is no more data). nb. That KErrCancelled should not be
1.1527 + // returned to the client because the old RMdaDevSound driver would have completed with KErrNone and zero data length.
1.1528 + StartRecordRequest();
1.1529 + break;
1.1530 +
1.1531 + case ERecordingPausedInSw:
1.1532 + // Paused in s/w but driver is not paused, therefore can not issue a new request to driver because
1.1533 + // it would re-start recording.
1.1534 + // This implies we were paused whilst the h/w was not recording, so there is no buffered data.
1.1535 +
1.1536 + // Complete the request with KErrNone and no data.
1.1537 + iClientRecordData->SetLength(0);
1.1538 + User::RequestComplete(iClientRecordStatus, KErrNone);
1.1539 + break;
1.1540 +
1.1541 + case EPlaying:
1.1542 + case EPlayingPausedInHw:
1.1543 + case EPlayingPausedInSw:
1.1544 + case EPlayingUnderrun:
1.1545 + Panic(EBadState);
1.1546 + break;
1.1547 +
1.1548 + default:
1.1549 + Panic(EBadState);
1.1550 + break;
1.1551 + }
1.1552 + }
1.1553 +
1.1554 +/**
1.1555 + Notify client of error.
1.1556 +
1.1557 + Note that we continue playing/recording if possible.
1.1558 +
1.1559 + We do not maintain information which could map the error back to a particular client play/record request
1.1560 + and therefore we have to notify the client of error every time it happens.
1.1561 +
1.1562 + nb. A client play/record request is completed with KErrNone if it queues ok - All errors are reported via the Notify*Error
1.1563 + mechanism.
1.1564 + */
1.1565 +void RMdaDevSound::CBody::SoundDeviceError(TInt aError)
1.1566 + {
1.1567 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1568 + RDebug::Printf("RMdaDevSound::CBody::SoundDeviceError: Error[%d] state %s", aError, iState.Name());
1.1569 + #endif
1.1570 +
1.1571 + ASSERT(aError != KErrNone);
1.1572 +
1.1573 + if(iClientPlayErrorStatus)
1.1574 + {
1.1575 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1576 + RDebug::Printf("RMdaDevSound::CBody::SoundDeviceError Completing iPlayerErrorStatus");
1.1577 + #endif
1.1578 +
1.1579 + User::RequestComplete(iClientPlayErrorStatus, aError); // nb call also sets iClientPlayErrorStatus to NULL
1.1580 + }
1.1581 +
1.1582 + if(iClientRecordErrorStatus)
1.1583 + {
1.1584 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1585 + RDebug::Printf("RMdaDevSound::CBody::SoundDeviceError Completing iClientRecordErrorStatus");
1.1586 + #endif
1.1587 + User::RequestComplete(iClientRecordErrorStatus, aError); // nb call also sets iClientRecordErrorStatus to NULL
1.1588 + }
1.1589 +
1.1590 + return;
1.1591 + }
1.1592 +
1.1593 +void RMdaDevSound::CBody::NotifyRecordError(TRequestStatus& aStatus)
1.1594 + {
1.1595 + aStatus = KRequestPending;
1.1596 + iClientRecordErrorStatus = &aStatus;
1.1597 + }
1.1598 +
1.1599 +void RMdaDevSound::CBody::NotifyPlayError(TRequestStatus& aStatus)
1.1600 + {
1.1601 + aStatus = KRequestPending;
1.1602 + iClientPlayErrorStatus = &aStatus;
1.1603 + }
1.1604 +
1.1605 +void RMdaDevSound::CBody::CancelNotifyPlayError()
1.1606 + {
1.1607 + if(iClientPlayErrorStatus)
1.1608 + {
1.1609 + User::RequestComplete(iClientPlayErrorStatus, KErrCancel);
1.1610 + }
1.1611 + }
1.1612 +
1.1613 +void RMdaDevSound::CBody::CancelNotifyRecordError()
1.1614 + {
1.1615 + if(iClientRecordErrorStatus)
1.1616 + {
1.1617 + User::RequestComplete(iClientRecordErrorStatus, KErrCancel);
1.1618 + }
1.1619 + else
1.1620 + {
1.1621 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1622 + RDebug::Printf("msp BufferEmptied but iClientPlayStatus==NULL");
1.1623 + #endif
1.1624 + }
1.1625 + }
1.1626 +
1.1627 +void RMdaDevSound::CBody::FlushPlayBuffer()
1.1628 + {
1.1629 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1630 + RDebug::Printf("RMdaDevSound::CBody::FlushPlayBuffer calling CancelPlayData");
1.1631 + #endif
1.1632 + CancelPlayData();
1.1633 + }
1.1634 +
1.1635 +RSoundSc& RMdaDevSound::CBody::PlaySoundDevice()
1.1636 + {
1.1637 + return iPlaySoundDevice;
1.1638 + }
1.1639 +
1.1640 +RSoundSc& RMdaDevSound::CBody::RecordSoundDevice()
1.1641 + {
1.1642 + return iRecordSoundDevice;
1.1643 + }
1.1644 +
1.1645 +const RMdaDevSound::CBody::TState &RMdaDevSound::CBody::State() const
1.1646 + {
1.1647 + return iState;
1.1648 + }
1.1649 +
1.1650 +
1.1651 +void RMdaDevSound::CBody::BufferFilled(TInt aBufferOffset)
1.1652 + {
1.1653 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1654 + RDebug::Print(_L("RMdaDevSound::CBody::BufferFilled:"));
1.1655 + #endif
1.1656 +
1.1657 + ASSERT(aBufferOffset>=0 || aBufferOffset==KErrCancel);
1.1658 + ASSERT(iClientRecordData); // request should not get this without
1.1659 +
1.1660 + if(aBufferOffset==KErrCancel)
1.1661 + {
1.1662 + //we can get KErrCancel when we call pause and there is no more data left with the driver
1.1663 + //we send the empty buffer to the HwDevice, where this should trigger the shutdown mechanism
1.1664 + iClientRecordData->SetLength(0);
1.1665 + User::RequestComplete(iClientRecordStatus, KErrNone);
1.1666 + iClientRecordStatus = NULL;
1.1667 + return;
1.1668 + }
1.1669 +
1.1670 + iBufferOffset = aBufferOffset;
1.1671 + //when last buffer is flushed, new driver sometimes gives buffer size of odd number. One of our codecs
1.1672 + //expects that the buffer size should always be even. Base suggested that we fix in multimedia
1.1673 + //as it is quite complicated to fix in overthere.
1.1674 + iBufferLength = iBufferLength & 0xfffffffe;
1.1675 + TPtr8 dataPtr(iRecordChunk.Base()+ iBufferOffset, iBufferLength, iClientRecordData->MaxLength());
1.1676 + if (iRecordFormatData.iConverter)
1.1677 + {
1.1678 + iRecordFormatData.iConverter->Convert(dataPtr, *iClientRecordData);
1.1679 + }
1.1680 + else
1.1681 + {
1.1682 + iClientRecordData->Copy(dataPtr);
1.1683 + }
1.1684 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1685 + RDebug::Print(_L("RMdaDevSound::CBody::BufferFilled: BufferOffset[%d] BufferLen[%d]"), iBufferOffset, iBufferLength);
1.1686 + #endif
1.1687 + if(iBufferOffset >= 0)
1.1688 + {
1.1689 + iRecordSoundDevice.ReleaseBuffer(iBufferOffset);
1.1690 + }
1.1691 + if(iClientRecordStatus)
1.1692 + {
1.1693 + User::RequestComplete(iClientRecordStatus, KErrNone);
1.1694 + iClientRecordStatus = NULL;
1.1695 + }
1.1696 + else
1.1697 + {
1.1698 + RDebug::Printf("msp PlayCancelled but iClientPlayStatus==NULL");
1.1699 + }
1.1700 + }
1.1701 +
1.1702 +/*
1.1703 + This function is called to notify us that a CPlayer's request has completed and what its status was.
1.1704 +
1.1705 + It is important to note that:-
1.1706 + 1) RSoundSc driver PlayData requests are guaranteed to complete in order, oldest first
1.1707 + 2) If we are overloaded, it is possible for more than one request to complete before any CPlayer::RunL is ran. In
1.1708 + this situation the CPlayer::RunL functions, and hence this callback, maybe invoked in non-oldest first order
1.1709 +
1.1710 + but
1.1711 +
1.1712 + a) It is impossible for callback for the second oldest CPlayer to occur before the driver request for the oldest has
1.1713 + been complete (because of 1)
1.1714 + b) We will always get exactly one callback for every complete request.
1.1715 +
1.1716 + Therefore this callback notifies us of two subtly separate things:-
1.1717 +
1.1718 + i) The oldest request has been completed (so we can reduce can increase the bytes played counter by its length
1.1719 + ii) CPlayer aPlayerIndex is free for re-use
1.1720 +
1.1721 + but we can not assume that aPlayerIndex is the oldest request, therefore we save the play request lengths outside of
1.1722 + the CPlayer object.
1.1723 +*/
1.1724 +void RMdaDevSound::CBody::PlayRequestHasCompleted(CPlayer *aPlayer, TInt aStatus, TBool aDueToCancelCommand)
1.1725 + {
1.1726 + // CPlayer is done so put it on the free queue
1.1727 + iFreePlayers.Push(aPlayer);
1.1728 +
1.1729 + TUint32 bytesPlayed = iActivePlayRequestSizes.Pop();
1.1730 + // Request has finished therefore now timing the following request to simulate bytes played
1.1731 + iStartTime = CurrentTimeInMsec();
1.1732 + if(aDueToCancelCommand)
1.1733 + {
1.1734 + // Callback due to CPlayer::Cancel/DoCancel being called, therefore we
1.1735 + // do not want to update bytes played, process state, report a error or start new players
1.1736 + return;
1.1737 + }
1.1738 +
1.1739 + // Update iBytesPlayed by the length of the oldest request (which might not be the one that CPlayer was
1.1740 + // handling).
1.1741 + iBytesPlayed += bytesPlayed;
1.1742 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1743 + RDebug::Printf("PlayRequestHasCompleted increasing iBytesPlayed by %d to %d", bytesPlayed, iBytesPlayed);
1.1744 + #endif
1.1745 +
1.1746 + // Process state
1.1747 + switch(iState)
1.1748 + {
1.1749 + case ENotReady:
1.1750 + Panic(EDeviceNotOpened);
1.1751 + break;
1.1752 +
1.1753 + case EStopped:
1.1754 + // Will happen if we are doing CancelPlayData processing with active players
1.1755 + break;
1.1756 +
1.1757 + case ERecording:
1.1758 + case ERecordingPausedInHw:
1.1759 + case ERecordingPausedInSw:
1.1760 + Panic(EBadState);
1.1761 + break;
1.1762 +
1.1763 + case EPlaying:
1.1764 + // Normal situation
1.1765 + break;
1.1766 +
1.1767 + case EPlayingPausedInHw:
1.1768 + // H/W was/is paused, but there must have been an already complete request that we had not
1.1769 + // processed yet.
1.1770 + // There must be at least one more pending request on h/w, otherwise the h/w would have refused to pause
1.1771 + // I would expect this be rare, but it happens quite often...
1.1772 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1773 + ASSERT(iActivePlayRequestSizes.Length() != 0);
1.1774 + #endif
1.1775 + // Need to update the start and pause time to now because we have just updated the actual iBytesPlayed
1.1776 + // and logically the h/w is paused at the beginning of the next request
1.1777 + iStartTime = CurrentTimeInMsec();
1.1778 + iPauseTime = iStartTime;
1.1779 + break;
1.1780 +
1.1781 + case EPlayingPausedInSw:
1.1782 + // This will happen if there is only a single hw request outstanding, and the hardware has finished it, but the
1.1783 + // corresponding RunL has not run yet (in which case PausePlayBuffer will have attempted to use h/w pause,
1.1784 + // but the driver call would have failed, and the state changed to EPlayingPausedInSw).
1.1785 + iStartTime = CurrentTimeInMsec();
1.1786 + iPauseTime = iStartTime;
1.1787 + return;
1.1788 + case EPlayingUnderrun:
1.1789 + break;
1.1790 +
1.1791 + default:
1.1792 + Panic(EBadState);
1.1793 + break;
1.1794 + }
1.1795 +
1.1796 +
1.1797 + // If we have an error, report it to the client
1.1798 + // We NEVER report driver underflow, instead we report KErrUnderflow if we run out of data to pass to driver.
1.1799 + if( (aStatus != KErrNone) && (aStatus != KErrUnderflow) )
1.1800 + {
1.1801 + SoundDeviceError(aStatus);
1.1802 + }
1.1803 +
1.1804 + // If appropriate start more players
1.1805 + StartPlayersAndUpdateState();
1.1806 + return;
1.1807 + }
1.1808 +
1.1809 +RMdaDevSound::CBody::CPlayer::CPlayer(TInt aPriority, RMdaDevSound::CBody& aParent, TInt aIndex):
1.1810 + CActive(aPriority), iParent(aParent), iIndex(aIndex), iBufferOffset(-1), iBufferLength(0)
1.1811 + {
1.1812 + CActiveScheduler::Add(this);
1.1813 + }
1.1814 +
1.1815 +RMdaDevSound::CBody::CPlayer::~CPlayer()
1.1816 + {
1.1817 + Cancel();
1.1818 + }
1.1819 +
1.1820 +
1.1821 +void RMdaDevSound::CBody::CPlayer::RunL()
1.1822 + {
1.1823 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1824 + RDebug::Printf("****RMdaDevSound::CBody::CPlayer(%d)::RunL: Error[%d] ParentState[%s]",
1.1825 + iIndex, iStatus.Int(), iParent.State().Name());
1.1826 + RDebug::Printf("iActivePlayRequestSizes.Length() = %d iFreePlayers.Length() = %d (including this one as active)",
1.1827 + iParent.iActivePlayRequestSizes.Length(),
1.1828 + iParent.iFreePlayers.Length());
1.1829 + #endif
1.1830 + iParent.PlayRequestHasCompleted(this, iStatus.Int(), EFalse);
1.1831 + return;
1.1832 + }
1.1833 +
1.1834 +TInt RMdaDevSound::CBody::CPlayer::RunError(TInt aError)
1.1835 + {
1.1836 + iParent.PlayRequestHasCompleted(this, aError, EFalse);
1.1837 + return KErrNone;
1.1838 + }
1.1839 +
1.1840 +void RMdaDevSound::CBody::CPlayer::DoCancel()
1.1841 + {
1.1842 +#ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1843 + RDebug::Printf("RMdaDevSound::CBody::CPlayer(%d)::DoCancel", iIndex);
1.1844 +#endif
1.1845 + if(iStatus == KRequestPending)
1.1846 + {
1.1847 + // Avoid cancelling requests which have already completed.
1.1848 + // It wastes time, and might provoke a sound driver problem
1.1849 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1850 + RDebug::Printf("RMdaDevSound::CBody::CPlayer::DoCancel - would have cancelled driver request");
1.1851 + #endif
1.1852 + iParent.PlaySoundDevice().Cancel(iStatus);
1.1853 + }
1.1854 + iParent.PlayRequestHasCompleted(this, KErrCancel, ETrue);
1.1855 + }
1.1856 +
1.1857 +void RMdaDevSound::CBody::CPlayer::PlayData(TUint aChunkOffset, TInt aLength)
1.1858 + {
1.1859 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1860 + RDebug::Print(_L("RMdaDevSound::CBody::CPlayer(%d)::PlayData : IsActive[%d]"),
1.1861 + iIndex, IsActive());
1.1862 + RDebug::Printf("iActivePlayRequestSizes.Length() = %d iFreePlayers.Length() = %d (inc this player)",
1.1863 + iParent.iActivePlayRequestSizes.Length(),
1.1864 + iParent.iFreePlayers.Length());
1.1865 + #endif
1.1866 +
1.1867 + iBufferOffset = aChunkOffset;
1.1868 + iBufferLength = aLength;
1.1869 +
1.1870 + //Make sure the length is a multiple of 4 to work around an h6 limitation.
1.1871 + iBufferLength = iBufferLength & 0xfffffffc;
1.1872 +
1.1873 + // Issue the RSoundSc request
1.1874 + iParent.PlaySoundDevice().PlayData(iStatus, iBufferOffset, iBufferLength, EFalse);
1.1875 + SetActive();
1.1876 + return;
1.1877 + }
1.1878 +
1.1879 +TUint RMdaDevSound::CBody::CPlayer::GetPlayerIndex() const
1.1880 + {
1.1881 + return iIndex;
1.1882 + }
1.1883 +
1.1884 +RMdaDevSound::CBody::CRecorder::CRecorder(TInt aPriority, RMdaDevSound::CBody& aParent):
1.1885 + CActive(aPriority), iParent(aParent), iBufferOffset(-1), iBufferLength(0)
1.1886 + {
1.1887 + CActiveScheduler::Add(this);
1.1888 + }
1.1889 +
1.1890 +RMdaDevSound::CBody::CRecorder::~CRecorder()
1.1891 + {
1.1892 + Cancel();
1.1893 + }
1.1894 +
1.1895 +void RMdaDevSound::CBody::CRecorder::RecordData(TInt& aBufferLength)
1.1896 + {
1.1897 + if (!IsActive())
1.1898 + {
1.1899 + iStatus = KRequestPending;
1.1900 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1901 + RDebug::Printf("Recording request: BufferLength[%d]", aBufferLength);
1.1902 + #endif
1.1903 + iParent.RecordSoundDevice().RecordData(iStatus, aBufferLength);
1.1904 + SetActive();
1.1905 + }
1.1906 + }
1.1907 +
1.1908 +void RMdaDevSound::CBody::CRecorder::RunL()
1.1909 + {
1.1910 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1911 + RDebug::Printf("****RMdaDevSound::CBody::CRecorder()::RunL: Error[%d] ParentState[%s]",
1.1912 + iStatus.Int(), iParent.State().Name());
1.1913 + #endif
1.1914 +
1.1915 +
1.1916 + TInt error = iStatus.Int();
1.1917 +
1.1918 + if((error >= 0) || (error == KErrCancel))
1.1919 + {//we can get KErrCancel when we call pause and there is no more data left with the driver
1.1920 + iParent.BufferFilled(error);
1.1921 + }
1.1922 + else
1.1923 + {
1.1924 + #ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1925 + RDebug::Print(_L("RMdaDevSound::CBody::CPlayer()::RunL: Error[%d]"), error);
1.1926 + #endif
1.1927 + iParent.SoundDeviceError(error);
1.1928 + }
1.1929 + }
1.1930 +
1.1931 +
1.1932 +TInt RMdaDevSound::CBody::CRecorder::RunError(TInt aError)
1.1933 + {
1.1934 + iParent.SoundDeviceError(aError);
1.1935 + return KErrNone;
1.1936 + }
1.1937 +
1.1938 +void RMdaDevSound::CBody::CRecorder::DoCancel()
1.1939 + {
1.1940 +#ifdef SYMBIAN_SOUNDADAPTER_DEBUG
1.1941 + RDebug::Printf("RMdaDevSound::CBody::CRecorder()::DoCancel");
1.1942 +#endif
1.1943 + iParent.RecordSoundDevice().Cancel(iStatus);
1.1944 + }
1.1945 +
1.1946 +
1.1947 +// End of file