sl@0: // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // source\server\mmfdatapath2.cpp sl@0: // sl@0: // sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "mmfdatapath2.h" sl@0: #include "mmfclientaudiostreamutils.h" sl@0: #include sl@0: #include // KUidMmfCodecAudioSettings sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: static void Panic(TMMFDataPathPanicCode aPanicCode, TInt aSourceLineNumber) sl@0: { sl@0: _LIT(KMMFDataPathPanicCategory, "MMFDataPath2"); sl@0: User::Panic(KMMFDataPathPanicCategory, STATIC_CAST(TInt,aPanicCode) + aSourceLineNumber); sl@0: } sl@0: sl@0: /** sl@0: Allocates and constructs a data path. sl@0: sl@0: Use this function if the codec UID is not already known by CMMFController sl@0: and there is no data path ambiguity - ie only one data path is possible. sl@0: sl@0: Will create codec via fourCC. sl@0: sl@0: @param aEventHandler sl@0: Installs an event handler to provide message passing between clients and sources/sinks. sl@0: sl@0: @return Newly constructed data path object. sl@0: */ sl@0: sl@0: EXPORT_C CMMFDataPath2* CMMFDataPath2::NewL(MAsyncEventHandler& aEventHandler) sl@0: { sl@0: CMMFDataPath2* self = new(ELeave) CMMFDataPath2(TMediaId(), aEventHandler); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: sl@0: /** sl@0: Allocates and constructs a data path according to the specified media ID. sl@0: sl@0: Use this function if the codec UID is not already known by CMMFController sl@0: and there is ambiguity with the data path ie. there is more than one possible data path. sl@0: sl@0: @param aMediaId sl@0: Optional media ID parameter when there are multiple media types. sl@0: @param aEventHandler sl@0: Installs an event handler to provide message passing between clients and sources/sinks. sl@0: sl@0: @return A newly constructed data path object. sl@0: */ sl@0: sl@0: EXPORT_C CMMFDataPath2* CMMFDataPath2::NewL(TMediaId aMediaId, MAsyncEventHandler& aEventHandler) sl@0: { sl@0: CMMFDataPath2* self = new(ELeave) CMMFDataPath2(aMediaId, aEventHandler); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: /** sl@0: Allocates and constructs a data path according to the specified codec UID. sl@0: sl@0: Use this function if the codec UID is already known by CMMFController sl@0: and there is no data path ambiguity ie. only one data path is possible sl@0: will create codec explicitly using the supplied codec Uid sl@0: sl@0: @param aCodecUid sl@0: Optional mediaID parameter when there are multiple media types sl@0: @param aEventHandler sl@0: Installs an event handler to provide message passing between clients and sources/sinks. sl@0: sl@0: @return A newly constructed data path object. sl@0: */ sl@0: sl@0: EXPORT_C CMMFDataPath2* CMMFDataPath2::NewL(TUid aCodecUid, MAsyncEventHandler& aEventHandler) sl@0: { sl@0: CMMFDataPath2* self = new(ELeave) CMMFDataPath2(TMediaId(), aEventHandler); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aCodecUid); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: sl@0: /** sl@0: Allocates and constructs a data path according to the specified codec UID. sl@0: sl@0: Use this function if the codec UID is already known by CMMFController sl@0: and there is ambiguity ie. more than one possible data path. sl@0: TMediaId used to select the path. sl@0: sl@0: @param aCodecUid sl@0: The codec UID. sl@0: @param aMediaId sl@0: Optional mediaID parameter when there are multiple media types. sl@0: @param aEventHandler sl@0: Installs an event handler to provide message passing between clients and sources/sinks. sl@0: sl@0: @return A newly constructed data path object. sl@0: */ sl@0: EXPORT_C CMMFDataPath2* CMMFDataPath2::NewL(TUid aCodecUid, TMediaId aMediaId, MAsyncEventHandler& aEventHandler) sl@0: { sl@0: CMMFDataPath2* self = new(ELeave) CMMFDataPath2(aMediaId, aEventHandler); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aCodecUid); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: CMMFDataPath2::CMMFDataPath2(TMediaId aMediaId, MAsyncEventHandler& aEventHandler) sl@0: : CMMFDataPath(aMediaId, aEventHandler), iTimeLeftToPlayComplete(-1) sl@0: { sl@0: } sl@0: sl@0: void CMMFDataPath2::ConstructL(TUid aCodecUid) sl@0: { sl@0: CMMFDataPath::ConstructL(aCodecUid); sl@0: iRepeatTrailingSilenceTimer = CPeriodic::NewL(CActive::EPriorityStandard); sl@0: } sl@0: sl@0: /** sl@0: Standard destructor. sl@0: */ sl@0: sl@0: CMMFDataPath2::~CMMFDataPath2() sl@0: { sl@0: if(iRepeatTrailingSilenceTimer) sl@0: { sl@0: iRepeatTrailingSilenceTimer->Cancel(); sl@0: delete iRepeatTrailingSilenceTimer; sl@0: } sl@0: } sl@0: sl@0: TInt CMMFDataPath2::RepeatTrailingSilenceTimerComplete(TAny* aDataPath) sl@0: { sl@0: CMMFDataPath2* dataPath = static_cast(aDataPath); sl@0: sl@0: TRAPD(err, dataPath->DoRepeatTrailingSilenceTimerCompleteL()); sl@0: if (err != KErrNone) sl@0: { sl@0: dataPath->DoSendEventToClient(KMMFEventCategoryPlaybackComplete, err); sl@0: } sl@0: return KErrNone; sl@0: } sl@0: sl@0: TInt CMMFDataPath2::DoRepeatTrailingSilenceTimerCompleteL() sl@0: { sl@0: //cancel this periodic timer sl@0: iRepeatTrailingSilenceTimer->Cancel(); sl@0: if(iTimeLeftToPlayComplete.Int64()>0) sl@0: { sl@0: iTimeLeftToPlayComplete=0; sl@0: } sl@0: sl@0: if (iTrailingSilenceLeftToPlay.Int64() > 0) sl@0: { sl@0: PlaySilence(); sl@0: } sl@0: else sl@0: { sl@0: SetPositionL(iPlayWindowStartPosition); sl@0: iTimeLeftToPlayComplete=-1; sl@0: FillSourceBufferL(); sl@0: } sl@0: return KErrNone; sl@0: } sl@0: sl@0: void CMMFDataPath2::PlaySilence() sl@0: { sl@0: // iRepeatTrailingSilenceTimer->After() takes a TTimeIntervalMicroSeconds32 sl@0: // so for longer periods of silence call it repeatedly with KMaxTInt lengths sl@0: TTimeIntervalMicroSeconds32 silence; sl@0: if(iTimeLeftToPlayComplete.Int64() > 0) sl@0: { sl@0: silence = I64INT(iTimeLeftToPlayComplete.Int64()); sl@0: } sl@0: else if (iTrailingSilenceLeftToPlay.Int64() > KMaxTInt) sl@0: { sl@0: silence = KMaxTInt; sl@0: iTrailingSilenceLeftToPlay = iTrailingSilenceLeftToPlay.Int64() - KMaxTInt; sl@0: } sl@0: else sl@0: { sl@0: silence = I64INT(iTrailingSilenceLeftToPlay.Int64()); sl@0: iTrailingSilenceLeftToPlay = 0; sl@0: } sl@0: iRepeatTrailingSilenceTimer->Start(silence, silence , TCallBack(RepeatTrailingSilenceTimerComplete, this)); sl@0: } sl@0: sl@0: /* sl@0: * FillSourceBufferL sl@0: * sl@0: * Function to get data from the datapath's iDataSource sl@0: */ sl@0: sl@0: void CMMFDataPath2::DoFillSourceBufferL() sl@0: { sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::FillSourceBufferL tick-%d (this 0x%x)\n"),User::TickCount(),this); sl@0: #endif sl@0: sl@0: __ASSERT_DEBUG((iState == EPlaying || iState == EConverting || iState == ERecording || (iState == EPrimed && iPauseCalled && iIsUsingResumeSupport)), Panic(EMMFDataPathPanicBadState,__LINE__)); sl@0: sl@0: // clear the no-more-source flag here (as well as in PlayL()) because sl@0: // there may have been a re-position since the last call to BufferFilledL() sl@0: iNoMoreSourceData = EFalse; sl@0: sl@0: if(!iObtainingAsyncSourceBuffer) sl@0: {//this is a normal request for data. sl@0: //If we are getting asynchronous buffers, then can't do this as iSourceBuffer == NULL sl@0: iSourceBuffer->SetFrameNumber(++iCurrentSourceFrameNumber); //so source knows which data to load buffer with sl@0: iSourceBuffer->SetStatus(EBeingFilled); sl@0: iSourceBuffer->SetLastBuffer(EFalse); sl@0: } sl@0: sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP asking for buffer %d - ptr=0x%x (this 0x%x)\n"), iCurrentSourceFrameNumber, iSourceBuffer,this); sl@0: #endif sl@0: sl@0: iSourceBufferWithSource = ETrue; sl@0: sl@0: // wait for BufferFilled callback from source. Do this here as some sources cause sl@0: //re-entrancy into data path via BufferFilledL sl@0: ChangeDataPathTransferState(EWaitSource); sl@0: sl@0: iDataSource->FillBufferL(iSourceBuffer, this, iMediaId); sl@0: sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::FillSourceBufferL - DONE tick-%d (this 0x%x)\n"),User::TickCount(),this); sl@0: #endif sl@0: } sl@0: sl@0: /** sl@0: Runs the clip depending on the current data path and transfer state. sl@0: sl@0: For example, fills the sink buffer if TDataPathState is EPlaying and TTransferState is ENeedSinkData. sl@0: */ sl@0: void CMMFDataPath2::RunL() sl@0: { sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::RunL transfer state %d, iPausedCalled %d, tick-%d (this 0x%x)\n"),iTransferState, iPauseCalled, User::TickCount(),this); sl@0: #endif sl@0: sl@0: switch (iState) sl@0: { sl@0: case EStopped: sl@0: break; sl@0: case EPrimed: // In the paused state we still continue to feed buffers to the sink. The sink (DevSound) handles the logic of whether the buffers should be emptied sl@0: { sl@0: if (!iPauseCalled || !iIsUsingResumeSupport) sl@0: break; sl@0: } sl@0: // fall-through sl@0: case EPlaying: sl@0: case ERecording: sl@0: case EConverting: sl@0: switch (iTransferState) sl@0: { sl@0: case EWaitSink: sl@0: case EWaitSource: sl@0: break; sl@0: case EInitializeSink: sl@0: InitializeSinkL(); sl@0: break; sl@0: case EInitializeSource: sl@0: InitializeSourceL(); sl@0: break; sl@0: case ENeedSourceData: sl@0: FillSourceBufferL(); sl@0: break; sl@0: case ENeedSinkData: sl@0: FillSinkBufferL(); sl@0: break; sl@0: case ENeedToMatchSourceToSink: sl@0: FillSinkBufferL(); sl@0: break; sl@0: case ESendDataToSink: sl@0: EmptySinkBufferL(); sl@0: break; sl@0: case EEndOfData: sl@0: EndOfData(); sl@0: break; sl@0: } sl@0: break; sl@0: default: sl@0: break; sl@0: } sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::RunL DONE\n")); sl@0: #endif sl@0: } sl@0: sl@0: /* sl@0: * FillSourceBufferL sl@0: * sl@0: * Function to get data from the datapath's iDataSource sl@0: */ sl@0: sl@0: void CMMFDataPath2::FillSourceBufferL() sl@0: { sl@0: __ASSERT_DEBUG((iState == EPlaying || iState == EConverting || iState == ERecording || (iState == EPrimed && iPauseCalled && iIsUsingResumeSupport) ), Panic(EMMFDataPathPanicBadState,__LINE__)); sl@0: sl@0: //if the silence timer is active then dont propagate the request sl@0: if(iRepeatTrailingSilenceTimer->IsActive()) sl@0: { sl@0: return; sl@0: } sl@0: sl@0: //play the silence period and dont propagate the request sl@0: if(iTrailingSilenceLeftToPlay>0 || iVerifyPlayComplete) sl@0: { sl@0: if(iVerifyPlayComplete)//case when the trailing silence is zero sl@0: { sl@0: if (!*iDisableAutoIntent && iDrmSource) sl@0: { sl@0: CMMFFile* file = static_cast(iDrmSource); sl@0: TInt err = file->ExecuteIntent(ContentAccess::EPlay); sl@0: if (err != KErrNone) sl@0: { sl@0: DoSendEventToClient(KMMFEventCategoryPlaybackComplete, err); sl@0: return; sl@0: } sl@0: } sl@0: sl@0: //Retrieve the current play time and add "duration-currentplaytime" to the silence period sl@0: //This is to ensure that silence timer is not started before the previous play is actually completed by the devsound sl@0: TTimeIntervalMicroSeconds currentTime = CalculateAudioOutputPosition(); sl@0: if(currentTime.Int64()>iPlayWindowStartPosition.Int64()) sl@0: { sl@0: iTimeLeftToPlayComplete = iPlayWindowEndPosition.Int64()-currentTime.Int64(); sl@0: } sl@0: else sl@0: { sl@0: iTimeLeftToPlayComplete = 0; sl@0: } sl@0: sl@0: iVerifyPlayComplete = EFalse; sl@0: } sl@0: if(iTrailingSilenceLeftToPlay==0 && iTimeLeftToPlayComplete==0) sl@0: { sl@0: SetPositionL(iPlayWindowStartPosition); sl@0: iTimeLeftToPlayComplete=-1; sl@0: } sl@0: else sl@0: { sl@0: PlaySilence(); sl@0: return; sl@0: } sl@0: } sl@0: sl@0: DoFillSourceBufferL(); sl@0: } sl@0: sl@0: sl@0: /** sl@0: Indicates the data source has filled the specified buffer. sl@0: sl@0: Called by the CMMFDataPath2's MDataSource when it has filled the buffer. sl@0: sl@0: @param aBuffer sl@0: A pointer to the filled buffer. sl@0: */ sl@0: void CMMFDataPath2::BufferFilledL(CMMFBuffer* aBuffer) sl@0: { sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::BufferFilledL src has filled buffer %d (ptr=0x%x) with %d bytes EoF = %d tick-%d (this 0x%x)\n"),aBuffer->FrameNumber(),aBuffer, aBuffer->BufferSize(),aBuffer->LastBuffer(), User::TickCount(),this); sl@0: #endif sl@0: sl@0: TBool isInTruePause = (iState == EPrimed && iPauseCalled && iIsUsingResumeSupport); sl@0: //state only used if we are passing data sl@0: __ASSERT_DEBUG((iState == EPlaying || iState == EConverting || iState == ERecording || isInTruePause), Panic(EMMFDataPathPanicBadState,__LINE__)); sl@0: sl@0: __ASSERT_DEBUG((!iNoMoreSourceData), Panic(EMMFDataPathPanicBadState,__LINE__)); sl@0: sl@0: //if we have been asked to repeat and this is the last buffer, reset last buffer flag and send to the device sl@0: if(aBuffer!= NULL && aBuffer->LastBuffer()) sl@0: { sl@0: iNumberOfTimesPlayed++; sl@0: if ((iNumberOfTimesPlayed <= iNumberOfTimesToRepeat) || iNumberOfTimesToRepeat == KMdaRepeatForever) sl@0: { sl@0: aBuffer->SetLastBuffer(EFalse); sl@0: sl@0: //this will trigger the trailing silence timer next time a buffer is requested. sl@0: iTrailingSilenceLeftToPlay = iTrailingSilence; sl@0: iVerifyPlayComplete = ETrue; sl@0: } sl@0: } sl@0: sl@0: iSourceBufferWithSource = EFalse; sl@0: sl@0: //Has the datapath stopped running, if so were not interested in any callbacks. sl@0: if(iState == EStopped || (iState == EPrimed && !isInTruePause)) sl@0: { sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::BufferFilledL called while not expecting callback iState=%d iPauseCalled=%d (this 0x%x)\n"),iState, iPauseCalled,this); sl@0: #endif sl@0: return; sl@0: } sl@0: sl@0: #ifdef REPOSITION_SPEEDUP sl@0: // if the source has been re-positioned, then go & get some more source data now sl@0: if (!iObtainingAsyncSourceBuffer && iSourceBuffer->FrameNumber() != iCurrentSourceFrameNumber) sl@0: { sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::BufferFilledL source was re-positioned re-requesting source data (this 0x%x)\n"),this); sl@0: #endif sl@0: ChangeDataPathTransferState(ENeedSourceData); sl@0: return; sl@0: } sl@0: #endif //REPOSITION_SPEEDUP sl@0: sl@0: //bufer is NULL, indicating no more source data. sl@0: if (!aBuffer) sl@0: { sl@0: //If we only hold a reference to the source buffer, set that to NULL sl@0: if(iSnkBufRef) sl@0: { sl@0: iSourceBuffer = NULL; sl@0: } sl@0: sl@0: iNoMoreSourceData = ETrue; sl@0: sl@0: if(!iCodec || //there's only one buffer and that has been returned as NULL, so must be end of data sl@0: iSinkBufferWithSink) //buffer is with sink, we don't have any more data to put in it, so must be end of data sl@0: { sl@0: ChangeDataPathTransferState(EEndOfData); sl@0: } sl@0: else //sink buffer is with datapath, see if there is anything to send to sink sl@0: { sl@0: ChangeDataPathTransferState(ENeedToMatchSourceToSink); sl@0: } sl@0: sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::BufferFilledL DONE aBuffer==NULL tick-%d (this 0x%x)\n"),User::TickCount(),this); sl@0: #endif sl@0: return; sl@0: } sl@0: sl@0: sl@0: //We were waiting for a response from the source to get an asynchronous buffer. sl@0: //We now have it, and we proceed to transfer this data to the sink. sl@0: if (iObtainingAsyncSourceBuffer) sl@0: { sl@0: iObtainingAsyncSourceBuffer = EFalse; sl@0: } sl@0: sl@0: sl@0: aBuffer->SetStatus(EFull); sl@0: sl@0: if(iSourceBuffer != aBuffer) sl@0: {//buffer has been changed by the source sl@0: iSourceBuffer = aBuffer; sl@0: if (!(iBuffersToUse & ENeedSinkBuffer)) sl@0: {//we only need one buffer and use source sl@0: iSinkBuffer = iSourceBuffer; sl@0: iSnkBufRef = ETrue; sl@0: } sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::BufferFilledL - iSourceBuffer=0x%x ref=%d iSinkBuffer=0x%x ref=%d (this 0x%x)\n"),iSourceBuffer,iSrcBufRef,iSinkBuffer,iSnkBufRef, this); sl@0: #endif sl@0: } sl@0: //Is this the last buffer from the source (0 length or LastBuffer flag set) sl@0: //or have reached the end of the play window; we only look at the play window here sl@0: //if we are converting. For conversion we look at the data we have read. This is then passed onto sl@0: //the source sl@0: if (!iSourceBuffer->BufferSize() || iSourceBuffer->LastBuffer() || sl@0: (((iState == EConverting) || (iState == EPlaying)) && (iPlayWindowEndPosition < iCachedSourceDuration) && ( InputPosition() >= iPlayWindowEndPosition ))) sl@0: { sl@0: //When it resumes in silence , position of the buffer is in end so we need to skip the increament. sl@0: if(!iPauseCalledInsilence) sl@0: { sl@0: iNumberOfTimesPlayed++; sl@0: }else sl@0: { sl@0: iPauseCalledInsilence=EFalse; sl@0: } sl@0: if ((iNumberOfTimesPlayed <= iNumberOfTimesToRepeat) || iNumberOfTimesToRepeat == KMdaRepeatForever) sl@0: { sl@0: iSourceBuffer->SetLastBuffer(EFalse); sl@0: //this will trigger the trailing silence timer next time a buffer is requested. sl@0: iTrailingSilenceLeftToPlay = iTrailingSilence; sl@0: iVerifyPlayComplete = ETrue; sl@0: } sl@0: else sl@0: { sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::BufferFilledL end of input data tick-%d (this 0x%x)\n"),User::TickCount(),this); sl@0: RDebug::Print(_L("iSourceBuffer->BufferSize()=%d\n"),iSourceBuffer->BufferSize()); sl@0: RDebug::Print(_L("iSourceBuffer->LastBuffer()=%d\n"),iSourceBuffer->LastBuffer()); sl@0: RDebug::Print(_L("InputPosition()=%d >= iPlayWindowEndPosition=%d\n"),I64INT(InputPosition().Int64()),I64INT(iPlayWindowEndPosition.Int64())); sl@0: #endif sl@0: iNoMoreSourceData = ETrue; sl@0: iSourceBuffer->SetLastBuffer(ETrue); //just in-case we are terminating on BufferSize == 0 or play window sl@0: } sl@0: } sl@0: sl@0: sl@0: if (!iCodec) sl@0: { sl@0: ChangeDataPathTransferState(ESendDataToSink); sl@0: } sl@0: else if(!iSinkBufferWithSink) //sink buffer is with data path, can try to fill it sl@0: { sl@0: ChangeDataPathTransferState(ENeedToMatchSourceToSink); sl@0: } sl@0: //else wait for sink to return buffer BufferEmptied will send us into ENeedToMatchSourceToSink state sl@0: sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::BufferFilledL - DONE tick-%d (this 0x%x)\n"),User::TickCount(),this); sl@0: #endif sl@0: } sl@0: sl@0: /** sl@0: Sets the data path position. sl@0: sl@0: @param aPosition sl@0: The data path position. sl@0: */ sl@0: void CMMFDataPath2::SetPositionL(const TTimeIntervalMicroSeconds& aPosition) sl@0: {//need to map to source position to frame position sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::SetPositionL = %d ticks-%d (this 0x%x)\n"),I64INT(aPosition.Int64()), User::TickCount(),this); sl@0: #endif sl@0: sl@0: if (iState == EStopped) sl@0: { sl@0: User::Leave(KErrNotReady); //can only set position if primed sl@0: } sl@0: sl@0: if(iGetTimePlayedSupported) sl@0: { sl@0: TTimeIntervalMicroSeconds timePlayed(0); sl@0: if(iState == EPlaying && iDataSink->DataSinkType() == KUidMmfAudioOutput) sl@0: { sl@0: CMMFAudioOutput* audioOutput = STATIC_CAST(CMMFAudioOutput*,iDataSink); sl@0: CMMFDevSound& devSound = audioOutput->SoundDevice(); sl@0: TInt err= devSound.GetTimePlayed(timePlayed); sl@0: if(err == KErrNone) sl@0: { sl@0: iDevSoundRepositionTime = timePlayed.Int64(); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: iDevSoundRepositionTime = 0; sl@0: } sl@0: } sl@0: else //roll back to samplesplayed sl@0: { sl@0: //As this will affect the position, we need to know how many bytes were sl@0: //played when position was updated. Future Position() requests will sl@0: //then use this refernce to determine the current position. sl@0: iReferenceAudioSamplesPlayed = AudioSamplesPlayed(); sl@0: } sl@0: sl@0: // Force the new position to be inside the play window (also within the file duration) sl@0: if ( aPosition < iPlayWindowStartPosition ) sl@0: { sl@0: iStartPosition = iPlayWindowStartPosition; sl@0: } sl@0: else if ( aPosition > iPlayWindowEndPosition ) sl@0: { sl@0: iStartPosition = iPlayWindowEndPosition; //clearly this will cause nothing to be played sl@0: } sl@0: else sl@0: { sl@0: iStartPosition = aPosition; sl@0: } sl@0: sl@0: TTimeIntervalMicroSeconds interval; sl@0: sl@0: //can only set the position on an MDataSource that is a format object sl@0: //Note: position defaults to source if both source & sink are clips sl@0: if (iDataSource->DataSourceType() == KUidMmfFormatDecode) sl@0: { sl@0: //position is not beyond the end of file sl@0: interval = ((CMMFFormatDecode*)iDataSource)->FrameTimeInterval(iMediaId); sl@0: sl@0: // for some reason this code won't compile without these intermediate steps sl@0: TInt64 position = iStartPosition.Int64(); sl@0: TInt64 interval64 = interval.Int64(); sl@0: if (interval64 == 0) sl@0: User::Leave(KErrDivideByZero); sl@0: TInt64 datapos64 = position/interval64; sl@0: iCurrentSourceFrameNumber = I64LOW(datapos64); sl@0: sl@0: sl@0: // Try to set the position directly on the format sl@0: TRAP_IGNORE(((CMMFFormatDecode*)iDataSource)->SetPositionL(iStartPosition)); sl@0: //NB: don't worry about error, since we'll reposition anyway when we get the next buffer sl@0: } sl@0: else if (iDataSink->DataSinkType() == KUidMmfFormatEncode) sl@0: { sl@0: //position is not beyond the end of file sl@0: interval = ((CMMFFormatEncode*)iDataSink)->FrameTimeInterval(iMediaId); sl@0: sl@0: //convert to TUint - for some reason it won't compile without these intermediate steps sl@0: TInt64 position = iStartPosition.Int64(); sl@0: TInt64 interval64 = interval.Int64(); sl@0: if (interval64 == 0) sl@0: User::Leave(KErrDivideByZero); sl@0: TInt64 datapos64 = position/interval64; sl@0: iCurrentSinkFrameNumber = I64LOW(datapos64); sl@0: sl@0: sl@0: // Try to set the position directly on the format sl@0: TRAP_IGNORE(((CMMFFormatEncode*)iDataSink)->SetPositionL(iStartPosition)); sl@0: //NB: don't worry about error, since we'll reposition anyway when we get the next buffer sl@0: } sl@0: else sl@0: {//can only set position if source or sink is a format sl@0: //If both source and sink are formats position is relative to the source sl@0: User::Leave(KErrNotSupported); //can't set position if neither source nor sink are clips sl@0: } sl@0: sl@0: if(iCodec) //we have a real codec, must reset it sl@0: { sl@0: iCodec->ResetL(); // Need to preserve sync when resuming play sl@0: } sl@0: sl@0: // Once we've sent the last buffer to the sink it's too late to start sl@0: // changing the state since we may get a RunError(KErrUnderflow) at any time. sl@0: // Once this happens, the sound driver may have unloaded etc..and recovery sl@0: // would be complicated. sl@0: if (iAllDataSentToSink) sl@0: { sl@0: return; sl@0: } sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::SetPosition - Done iCurrentSourceFrameNumber=%d iStartPosition=%d ticks-%d (this 0x%x)\n"),iCurrentSourceFrameNumber, I64INT(iStartPosition.Int64()), User::TickCount(),this); sl@0: #endif sl@0: } sl@0: sl@0: /** sl@0: Gets the data path position. sl@0: sl@0: @return The data path position. sl@0: */ sl@0: TTimeIntervalMicroSeconds CMMFDataPath2::Position() const sl@0: { sl@0: if ((iState == ERecording) || (iState == EConverting)) sl@0: { sl@0: return InputPosition(); sl@0: } sl@0: else if(iState == EPlaying) sl@0: { sl@0: return OutputPosition(); sl@0: } sl@0: else sl@0: { sl@0: return iStartPosition; sl@0: } sl@0: } sl@0: sl@0: TTimeIntervalMicroSeconds CMMFDataPath2::OutputPosition() const sl@0: { sl@0: TTimeIntervalMicroSeconds interval; sl@0: TTimeIntervalMicroSeconds position; sl@0: sl@0: if (iDataSink->DataSinkType() == KUidMmfAudioOutput) sl@0: { sl@0: position = CalculateAudioOutputPosition(); sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::OutputPosition from audio output= %d\n"),I64INT(position.Int64())); sl@0: #endif sl@0: } sl@0: else if (iDataSink->DataSinkType() == KUidMmfFormatEncode) sl@0: { sl@0: //note Encode format position takes priority if both source & sink are formats? sl@0: // try to get the position directly from the format. If that fails, work it out here sl@0: TRAPD(error, position = ((CMMFFormatEncode*)iDataSink)->PositionL()); sl@0: if (error)//getting the position from the format didn't work so calculate it here sl@0: { sl@0: interval = ((CMMFFormatEncode*)iDataSink)->FrameTimeInterval(iMediaId); sl@0: TInt64 position64 = interval.Int64() * iCurrentSinkFrameNumber; sl@0: position = position64; sl@0: } sl@0: sl@0: TTimeIntervalMicroSeconds duration = CONST_CAST(CMMFDataPath2*,this)->Duration(); //need this to check position doesn't exceed duration sl@0: if (position > duration)//this can happen on last buffer sl@0: { sl@0: position = duration; sl@0: } sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::OutputPosition from format= %d\n"),I64INT(position.Int64())); sl@0: #endif sl@0: } sl@0: else sl@0: {//can only read output position if sink is a format or an audio output sl@0: return TTimeIntervalMicroSeconds(0); sl@0: } sl@0: sl@0: return position; sl@0: } sl@0: sl@0: TTimeIntervalMicroSeconds CMMFDataPath2::CalculateAudioOutputPosition() const sl@0: { sl@0: //This operation can only be carried out on an Audio Output sl@0: __ASSERT_ALWAYS(iDataSink->DataSinkType() == KUidMmfAudioOutput, Panic(EMMFDataPathPanicProgrammingError,__LINE__)); sl@0: sl@0: sl@0: //If we are not playing, simply return where we will play from sl@0: if(iState != EPlaying || iCurrentSinkFrameNumber == 0 || iStartPosition == iPlayWindowEndPosition ) sl@0: { sl@0: return iStartPosition; sl@0: } sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::CalculateAudioOutputDuration from %d\n"),iReferenceAudioSamplesPlayed); sl@0: #endif sl@0: sl@0: CMMFAudioOutput* audioOutput = STATIC_CAST(CMMFAudioOutput*,iDataSink); sl@0: CMMFDevSound& devSound = audioOutput->SoundDevice(); sl@0: sl@0: TTimeIntervalMicroSeconds devSoundTimePlayed(0); sl@0: TInt64 timePlayed(0); sl@0: TInt err = KErrNone; sl@0: if(iGetTimePlayedSupported) sl@0: { sl@0: err= devSound.GetTimePlayed(devSoundTimePlayed); sl@0: if(err == KErrNone) sl@0: { sl@0: timePlayed = devSoundTimePlayed.Int64()-iDevSoundRepositionTime.Int64(); sl@0: } sl@0: } sl@0: else //Roll back to SamplesPlayed sl@0: { sl@0: TReal samplesPlayed = AudioSamplesPlayed() - iReferenceAudioSamplesPlayed; sl@0: TMMFCapabilities devSoundConfig = devSound.Config(); sl@0: TInt samplingFreq = StreamUtils::SampleRateAsValue(devSoundConfig); sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("samplingFreq %d\n"),samplingFreq); sl@0: #endif sl@0: sl@0: TReal timePlayedSeconds = 0; sl@0: if(samplesPlayed) sl@0: { sl@0: timePlayedSeconds = samplesPlayed/samplingFreq; sl@0: } sl@0: timePlayed = I64DOUBLECAST(timePlayedSeconds * 1000000); sl@0: sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("timePlayed %d\n"), I64LOW(timePlayed)); sl@0: #endif sl@0: } sl@0: if(err == KErrNone) sl@0: { sl@0: // When Resume is not supported. Datapath simulates Pause() through DevSound's Stop sl@0: // the time played is lost. So we need to saved the last position sl@0: // On the opposite, when Resume is supported the time played returned by DevSound sl@0: // reflects the real position, so there is no needed to recalculate at least playwindow is being used sl@0: // Finally, if Resume is supported but is not used the position also need to be saved sl@0: if(!iIsUsingResumeSupport || iPlayWindowStartPosition.Int64() > 0) sl@0: { sl@0: timePlayed = timePlayed + iStartPosition.Int64(); sl@0: } sl@0: sl@0: //During repeats. we need to reset the positin manually to playstart once playend is reached sl@0: //this is because the bytes returned by devsound are not accurate in all the cases sl@0: if(iRepeatTrailingSilenceTimer->IsActive() || iTimeLeftToPlayComplete==0)//loop play sl@0: { sl@0: if(iTimeLeftToPlayComplete==0) sl@0: { sl@0: timePlayed = iPlayWindowStartPosition.Int64(); sl@0: } sl@0: } sl@0: else if(timePlayed>=(iPlayWindowEndPosition.Int64()+1)) sl@0: { sl@0: timePlayed = iPlayWindowStartPosition.Int64(); sl@0: } sl@0: } sl@0: sl@0: return TTimeIntervalMicroSeconds(timePlayed); sl@0: } sl@0: sl@0: /** sl@0: Stops playing. sl@0: sl@0: Resets datapath position - currently does not clean up buffers. Sends KMMFErrorCategoryDataPathGeneralError sl@0: to the client if an error occurs. sl@0: */ sl@0: void CMMFDataPath2::Stop() sl@0: { sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::Stop current state=%d tick-%d (this 0x%x)\n"), iTransferState, User::TickCount(),this); sl@0: #endif sl@0: if ((iDataPathCreated) && (iState != EStopped)) sl@0: { sl@0: TRAPD(err, DoStopL()); sl@0: if (err) sl@0: { sl@0: DoSendEventToClient(KMMFErrorCategoryDataPathGeneralError, err); sl@0: } sl@0: } sl@0: } sl@0: sl@0: void CMMFDataPath2::DoStopL() sl@0: { sl@0: CMMFDataPath::DoStopL(); sl@0: iRepeatTrailingSilenceTimer->Cancel(); sl@0: iIsUsingResumeSupport = EFalse; sl@0: } sl@0: sl@0: /** sl@0: Pauses playing. sl@0: sl@0: Sends KMMFErrorCategoryDataPathGeneralError to the client if an error occurs. sl@0: */ sl@0: void CMMFDataPath2::Pause() sl@0: { sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::Pause, on src buff %d sink buf %d (this 0x%x)\n"), iCurrentSourceFrameNumber, iCurrentSinkFrameNumber,this); sl@0: RDebug::Print(_L("DP::Pause current state=%d tick-%d (this 0x%x)\n"),iTransferState, User::TickCount(),this); sl@0: #endif sl@0: sl@0: TRAPD(err, DoPauseL()); sl@0: sl@0: if (err) sl@0: { sl@0: DoSendEventToClient(KMMFErrorCategoryDataPathGeneralError, err); sl@0: } sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::Pause - DONE tick-%d (this 0x%x)\n"),User::TickCount(),this); sl@0: #endif sl@0: } sl@0: sl@0: EXPORT_C void CMMFDataPath2::PreEmptionPause() sl@0: { sl@0: TRAPD(err, DoPreEmptionPauseL()); sl@0: sl@0: if (err) sl@0: { sl@0: DoSendEventToClient(KMMFErrorCategoryDataPathGeneralError, err); sl@0: } sl@0: } sl@0: sl@0: /** sl@0: Pauses playing. sl@0: sl@0: Sends KMMFErrorCategoryDataPathGeneralError to the client if an error occurs. sl@0: */ sl@0: void CMMFDataPath2::DoPreEmptionPauseL() sl@0: { sl@0: sl@0: if ((iDataPathCreated) && ((iState == EPlaying) || (iState == EConverting) && ( iDataSink->DataSinkType() == KUidMmfAudioOutput ))) sl@0: { sl@0: iDataSource->SourcePauseL(); // propagate state change to source sl@0: SetPositionL(Position()); sl@0: iIsUsingResumeSupport = EFalse; sl@0: sl@0: iDataSink->SinkStopL(); sl@0: iPauseCalled = ETrue; // indicate pause is called sl@0: sl@0: iState = EPrimed; // go back to primed state (we're not playing) sl@0: sl@0: iSinkBufferWithSink = EFalse; sl@0: iSourceBufferWithSource = EFalse; sl@0: sl@0: ResetRefBuffers(); // buffer references may not be valid any more sl@0: sl@0: Cancel(); //Stop the state machine sl@0: } sl@0: if(iState == ERecording) sl@0: { sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: iRepeatTrailingSilenceTimer->Cancel(); sl@0: } sl@0: sl@0: void CMMFDataPath2::DoPauseL() sl@0: { sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::DoPauseL tick-%d (this 0x%x)\n"),User::TickCount(),this); sl@0: #endif sl@0: sl@0: sl@0: if ((iDataPathCreated) && ((iState == EPlaying) || (iState == EConverting))) sl@0: { sl@0: if (iDataSink->DataSinkType() == KUidMmfAudioOutput && iIsResumeSupported) sl@0: { sl@0: iDataSink->SinkPauseL(); sl@0: // When true pause is supported only the datapath's position sl@0: // should be updated, MDataSource position should be changed sl@0: iStartPosition = Position(); sl@0: iIsUsingResumeSupport = ETrue; sl@0: if(iRepeatTrailingSilenceTimer->IsActive()) sl@0: { sl@0: iPauseCalledInsilence=ETrue; sl@0: } sl@0: // If we wait for the sink to complete play, then we do not proceed with supplying the buffers to the sink sl@0: // In this case we need to reset the buffers so that InitializeSinkL won't attempt bringing in new ones sl@0: if (iTransferState == EWaitSink) sl@0: { sl@0: ResetRefBuffers(); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: // If we use resume support, then there is no need to pause source as we would continue to supply buffers to the sink sl@0: // Here we are not using resume support, thus we're pausing the source sl@0: iDataSource->SourcePauseL(); sl@0: SetPositionL(Position()); sl@0: iDataSink->SinkStopL(); sl@0: ResetRefBuffers(); // buffer references may not be valid any more sl@0: sl@0: Cancel(); //Stop the state machine sl@0: } sl@0: iPauseCalled = ETrue; // indicate pause is called sl@0: sl@0: iState = EPrimed; // go back to primed state (we're not playing) sl@0: } sl@0: else if(iState == ERecording) sl@0: { sl@0: iPauseCalled = ETrue; sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::DoPauseL Recording, pause datasource\n")); sl@0: #endif sl@0: iDataSource->SourcePauseL(); sl@0: } sl@0: iRepeatTrailingSilenceTimer->Cancel(); sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::DoPauseL - Done iReferenceAudioSamplesPlayed = %d\n"),iReferenceAudioSamplesPlayed); sl@0: RDebug::Print(_L("DP::DoPauseL - Done restart at %d tick-%d (this 0x%x)\n"),I64INT(iStartPosition.Int64()), User::TickCount(),this); sl@0: #endif sl@0: } sl@0: sl@0: /** sl@0: Cancels the silence timer. sl@0: */ sl@0: void CMMFDataPath2::DoCancel() sl@0: { sl@0: //cancel repeats sl@0: iRepeatTrailingSilenceTimer->Cancel(); sl@0: iNumberOfTimesToRepeat=0; sl@0: } sl@0: sl@0: /** sl@0: Allocates buffers in preparation to play. sl@0: sl@0: Must be called before calling PlayL(). sl@0: sl@0: iSnkBufRef and iSrcBufRef contain ETrue if these buffers are created and owned by a MDataSource or MDataSink sl@0: For clean-up purposes, datapath only cleans up buffers allocated directly by PrimeL(). sl@0: */ sl@0: void CMMFDataPath2::PrimeL() sl@0: { sl@0: CMMFDataPath::PrimeL(); sl@0: if(iDataSink->DataSinkType() == KUidMmfAudioOutput) sl@0: { sl@0: CMMFAudioOutput* audioOutput = STATIC_CAST(CMMFAudioOutput*,iDataSink); sl@0: CMMFDevSound& devSound = audioOutput->SoundDevice(); sl@0: iGetTimePlayedSupported = devSound.IsGetTimePlayedSupported(); sl@0: iIsResumeSupported = devSound.IsResumeSupported(); sl@0: iIsUsingResumeSupport = EFalse; sl@0: iPauseCalledInsilence = EFalse; sl@0: } sl@0: } sl@0: sl@0: /** sl@0: Starts an active scheduler 'play' loop. sl@0: sl@0: Can only play from the primed state. sl@0: */ sl@0: void CMMFDataPath2::PlayL() sl@0: { sl@0: sl@0: #if defined(__PROFILING) sl@0: RDebug::ProfileEnd(1); sl@0: #endif // defined(__PROFILING) sl@0: sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::PlayL, on src buff %d sink buf %d (this 0x%x)\n"), iCurrentSourceFrameNumber, iCurrentSinkFrameNumber,this); sl@0: RDebug::Print(_L("iStartPosition = %d\n"), I64INT(iStartPosition.Int64())); sl@0: #endif sl@0: sl@0: if ((iDataPathCreated) && (iState == EPrimed)) sl@0: { sl@0: //can only play from the primed state sl@0: sl@0: if(iPauseCalled) //sink and source will have been stopped, and we will not have been re-primed sl@0: { sl@0: //When pause is called silence,we need to send the buffer while resume so icansendbuffer should enabled sl@0: if(!iPauseCalledInsilence) sl@0: { sl@0: iDataSink->SinkPrimeL(); //propagate change down to sink sl@0: } sl@0: iPauseCalled = EFalse; sl@0: } sl@0: sl@0: iCurrentSourceFrameNumber = 0; //reset to beginning sl@0: iCurrentSinkFrameNumber = 0; //reset to beginning sl@0: sl@0: iSourceBufferWithSource = EFalse; sl@0: iSinkBufferWithSink = EFalse; sl@0: sl@0: iNoMoreSourceData = EFalse; sl@0: iAllDataSentToSink=EFalse; sl@0: iDataPathCompletedErrorCode=KErrNone; sl@0: sl@0: if(!iIsResumeSupported || !iIsUsingResumeSupport) sl@0: { sl@0: SetPositionL( iStartPosition ); sl@0: } sl@0: iReferenceAudioSamplesPlayed = 0; sl@0: iReferenceAudioSamplesRecorded = 0; sl@0: sl@0: //complete a request on iStatus to invoke play code sl@0: iDataSource->SourcePlayL(); //propagate state change to source sl@0: sl@0: // This need to be done always since CMMFAudioOutput::EmptyBuffer sl@0: // doesn't start playback anymore sl@0: iDataSink->SinkPlayL(); //propogate state change to sink sl@0: sl@0: if (iDataSink->DataSinkType() == KUidMmfAudioOutput) sl@0: iState = EPlaying; sl@0: else if(iDataSource->DataSourceType() == KUidMmfAudioInput) sl@0: iState = ERecording; sl@0: else sl@0: iState = EConverting; sl@0: sl@0: //need to re-initialize any buffer(s) that we only own references to sl@0: ChangeDataPathTransferState(EInitializeSink); sl@0: } sl@0: iDevSoundRepositionTime = 0; sl@0: if(!iRetainRepeatInfo) sl@0: { sl@0: iNumberOfTimesPlayed = 0; sl@0: iTimeLeftToPlayComplete = -1; sl@0: iVerifyPlayComplete = EFalse; sl@0: } sl@0: iRetainRepeatInfo = EFalse; sl@0: #ifdef _DP_DEBUG sl@0: RDebug::Print(_L("DP::Play - DONE\n")); sl@0: #endif sl@0: } sl@0: sl@0: /** sl@0: Deletes buffers if this datapath's sources and sinks own the buffers returned by PrimeL(). sl@0: Typically if buffers are created asychronously, the datapath doesn't own the buffer sl@0: so leaves cleanup handling to the owner sources/sinks. sl@0: sl@0: Called when source and sink needs to be de-referenced. Sets iDataPathCreated, iSinkCanReceive, sl@0: iSnkBufRef and iSrcBufRef to EFalse; sets iState to EStopped. sl@0: */ sl@0: void CMMFDataPath2::ResetL() sl@0: { sl@0: CMMFDataPath::ResetL(); sl@0: iDrmSource = NULL; sl@0: } sl@0: sl@0: /** sl@0: Sets the number of times the audio sample is to be repeated during the sl@0: playback operation. sl@0: sl@0: A period of silence can follow each playing of the sample. The audio sl@0: sample can be repeated indefinitely. sl@0: sl@0: @param aRepeatNumberOfTimes sl@0: The number of times the audio sample, together with sl@0: the trailing silence, is to be repeated. If this is sl@0: set to KMdaRepeatForever, then the audio sl@0: sample, together with the trailing silence, is sl@0: repeated indefinitely or until Stop() is sl@0: called. If this is set to zero, then the audio sample sl@0: is not repeated. sl@0: @param aTrailingSilence sl@0: The time interval of the trailing silence in microseconds. sl@0: sl@0: */ sl@0: EXPORT_C void CMMFDataPath2::SetRepeats(TInt aRepeatNumberOfTimes, const TTimeIntervalMicroSeconds& aTrailingSilence) sl@0: { sl@0: iNumberOfTimesToRepeat=aRepeatNumberOfTimes; sl@0: iTrailingSilence=aTrailingSilence; sl@0: } sl@0: sl@0: /** sl@0: Sets the Drm file source and the automatic execute intent flag. This method is used by the controller plugin sl@0: to pass these to the datapath in order to execute the play intent during loop play. sl@0: sl@0: @param aSource sl@0: Data Source on which the play intent needs to be executed. This is usually the CMMFFile source sl@0: @param aDisableAutoIntent sl@0: Boolean variable which states whether the controller plugin or the datapath needs to execute play intent sl@0: automatically or not. sl@0: */ sl@0: EXPORT_C void CMMFDataPath2::SetDrmProperties(MDataSource* aSource, TBool *aDisableAutoIntent) sl@0: { sl@0: iDrmSource = aSource; sl@0: iDisableAutoIntent = aDisableAutoIntent; sl@0: } sl@0: sl@0: /** sl@0: This call indicates PlayL not to reset the iNumberOfTimesPlayed property. This method is used by the controller plugin sl@0: during repositioning. PlayL call during seeking should not reset the iNumberOfTimesPlayed property. sl@0: */ sl@0: EXPORT_C void CMMFDataPath2::RetainRepeatInfo() sl@0: { sl@0: iRetainRepeatInfo = ETrue; sl@0: }