⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 audio_session-mda.cpp

📁 著名的 helix realplayer 基于手机 symbian 系统的 播放器全套源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        }
    }
}

void HXSymbianAudioSession::DoCleanup()
{
    while(!m_bufferList.IsEmpty())
    {
        IHXBuffer* pBuf = (IHXBuffer*)m_bufferList.RemoveHead();
        HX_RELEASE(pBuf);
    }

    if (m_wantsNotify)
    {
        m_notifyRequest.Complete(KErrCancel);
        m_wantsNotify = false;
    }

    if (m_pStream)
    {
        m_pStream->Stop();
        delete m_pStream;
        m_pStream = 0;
        m_open = false;
    }

    // remove session from server
    if( m_pServer)
    {
        m_pServer->DelSession();
        m_pServer = 0;
    }
}

//
// HXSymbianAudioSession::Play
//
void HXSymbianAudioSession::Play()
{
    if (m_reason != KErrNone)
        m_reason = KErrNone;

    // reset audio properties in case they changed on us
    TRAPD(error, m_pStream->SetAudioPropertiesL(m_sampleRate,
                                                m_channels));
    m_pStream->SetPriority(KClientPriority, KPriorityPref);
    m_timeline.OnPlay();
    m_bPaused = FALSE;

    if (ReadyToWrite())
    {
        error = WriteNextBuffer();
    }

    Message().Complete(error);
}

//
// HXSymbianAudioSession::Pause
//
void HXSymbianAudioSession::Pause()
{
    m_timeline.OnPauseOrUnderflow();
    m_bPaused = TRUE;

    if (m_pStream)
    {
        m_pStream->Stop();
    }
    Message().Complete(KErrNone);
}

//
// HXSymbianAudioSession::Write
//
//
void HXSymbianAudioSession::Write()
{
    TInt result = KErrArgument;
    IHXBuffer* pAudioBuf = (IHXBuffer*)Message().Ptr0();

    if (pAudioBuf)
    {
        if (m_bufferList.AddTail(pAudioBuf))
        {
            pAudioBuf->AddRef();

            if (ReadyToWrite())
            {
                result = WriteNextBuffer();

                if (KErrNone != result)
                {
                    // Remove the buffer we just appended
                    // to the list
                    m_bufferList.RemoveTail();

                    // Release our reference to the buffer
                    HX_RELEASE(pAudioBuf);
                }
            }
            else
            {
                result = KErrNone;
            }
        }
        else
        {
            result = KErrNoMemory;
        }
        
    }

    if( m_bufferList.GetCount() < WriteBufferDepth ||
        KErrNone != result )
    {
        //We have slots free and no errors so we can complete the
        //message.
        m_writeComplete = true;
        Message().Complete(result);
    }
    else
    {
        // We need to delay notifying the caller until the current
        // write completes
        m_writeComplete = false;
        m_writeMessage = Message();
    }
    
}

//
// HXSymbianAudioSession::GetTime
//
// Return the current playback position -- converts from
// microseconds to milliseconds
//
void HXSymbianAudioSession::GetTime()
{
    Message().Complete(m_timeline.GetPlaybackTime());
}


//
// HXSymbianAudioSession::GetBlocksBuffered
//
// Return the number of blocks buffered by this object.
//
void HXSymbianAudioSession::GetBlocksBuffered()
{
    Message().Complete(m_bufferList.GetCount());
}

//
// HXSymbianAudioSession::SetVolume
//
// set the volume -- convert from 0-100 to 0-max range
//
void HXSymbianAudioSession::SetVolume()
{
    if (m_pStream)
    {
        m_pStream->SetVolume(Message().Int0());
    }
    Message().Complete(0);
}

//
// HXSymbianAudioSession::GetVolume
//
// get the current volume normalized to 0-100 range
//
void HXSymbianAudioSession::GetVolume()
{
    TInt vol = 0;
    if (m_pStream)
    {
        vol = m_pStream->Volume();
    }
    Message().Complete(vol);
}

//
// HXSymbianAudioSession::GetMaxVolume
//
// get the maxium device volume
//
void HXSymbianAudioSession::GetMaxVolume()
{
    TInt maxVol = 0;
    if (m_pStream)
    {
        maxVol = m_pStream->MaxVolume();
    }

    Message().Complete(maxVol);
}

//
// HXSymbianAudioSession::GetMinVolume
//
// get the minimum device volume
//
void HXSymbianAudioSession::GetMinVolume()
{
    Message().Complete(0);
}


//
// HXSymbianAudioSession::Stop
//
// stop playback
//
void HXSymbianAudioSession::Stop()
{
    m_bPaused = TRUE;
    if(m_pStream)
    {
        m_pStream->Stop();
    }
    
    // Cleanup any remaining buffers
    while(!m_bufferList.IsEmpty())
    {
        IHXBuffer* pBuf = (IHXBuffer*)m_bufferList.RemoveHead();
        HX_RELEASE(pBuf);
    }

    m_timeline.Reset(GetByteRate());

    Message().Complete(KErrNone);
}

void
HXSymbianAudioSession::RequestDeviceTakenNotification()
{
    m_wantsNotify = true;
    m_notifyRequest = Message();
}

void
HXSymbianAudioSession::CancelDeviceTakenNotification()
{
    if (m_wantsNotify)
    {
        m_notifyRequest.Complete(KErrCancel);
        m_wantsNotify = false;
    }
}

//
// HXSymbianAudioSession::NotifyDeviceTaken
//
// notify the client that the audio device has been taken if a
// notification requrest has been made 
//
void HXSymbianAudioSession::NotifyDeviceTaken()
{
    if (m_wantsNotify)
    {
        m_notifyRequest.Complete(m_reason);
        m_wantsNotify = false;
    }
}

//
// callbacks
//

void HXSymbianAudioSession::MaoscOpenComplete(TInt error)
{
    if (error == KErrNone)
    {
        m_open = true;

        TRAP(error, m_pStream->SetAudioPropertiesL(m_sampleRate,
                                                   m_channels));
        m_pStream->SetPriority(KClientPriority, KPriorityPref);
    }

    Message().Complete(error);
}

void HXSymbianAudioSession::MaoscBufferCopied(TInt error, const TDesC8& buf)
{
    m_bWritePending = FALSE;

    // Check to see if we need a device time
    if (m_timeline.NeedDeviceTime())
    {
        m_timeline.SetDeviceTime(GetDeviceMs());
    }

    if (!m_bufferList.IsEmpty())
    {
        // We should always enter here because the
        // last buffer written is at the head of the list.

        // We want to remove this buffer from the list since
        // this call is the completion of last WriteL() call.
        IHXBuffer* pBuf = (IHXBuffer*)m_bufferList.RemoveHead();
        HX_RELEASE(pBuf);
    }

    if (ReadyToWrite())
    {
        error = WriteNextBuffer();
    }

    if( !m_writeComplete &&
        (m_bufferList.GetCount() < WriteBufferDepth))
    {
        m_writeComplete = true;
        m_writeMessage.Complete(error);
    }

    
}

void HXSymbianAudioSession::MaoscPlayComplete(TInt error)
{
    if (KErrUnderflow == error)
    {
        m_timeline.OnPauseOrUnderflow();
    }

    //If the media server is stopping for any reason make sure we
    //honor any outstanding completes.
    if( !m_writeComplete )
    {
        m_writeComplete = true;
        m_writeMessage.Complete(error);
    }

    int resetErr;
    TRAP(resetErr, m_pStream->SetAudioPropertiesL(m_sampleRate,
                                                  m_channels));
    m_pStream->SetPriority(KClientPriority, KPriorityPref);

    if (error == KErrAbort)
    {
        m_reason = error;
        m_pServer->NotifyDeviceTaken();
    }

}

UINT32 HXSymbianAudioSession::GetByteRate() const
{
    return 2 * FlagToNumber(m_sampleRate) * FlagToNumber(m_channels);
}


UINT32 HXSymbianAudioSession::GetDeviceMs()
{
    UINT32 ulRet = 0;

    if (m_pStream)
    {
        TTimeIntervalMicroSeconds pos = m_pStream->Position();
        TInt64 millisecs = pos.Int64() / 1000;
   
        ulRet = millisecs.Low();
    }

    return ulRet;
}

BOOL HXSymbianAudioSession::ReadyToWrite() const
{
    return !m_bWritePending && !m_bPaused && !m_bufferList.IsEmpty();
}

TInt HXSymbianAudioSession::WriteNextBuffer()
{
    // Write the next buffer in the list
    IHXBuffer* pBuffer = (IHXBuffer*)m_bufferList.GetHead();

    TInt result = KErrNone;

    if (pBuffer)
    {
        if( m_pStream )
        {
            int len = pBuffer->GetSize();
            m_pData.Set((TUint8*)pBuffer->GetBuffer(), len, len);
            TRAP(result, m_pStream->WriteL(m_pData));
        }
        else
        {
            // oom earlier?
            result = KErrNotReady;
        }
    }

    if (KErrNone == result)
    {
        m_timeline.OnWrite(pBuffer->GetSize());
        m_bWritePending = TRUE;
    }
    else
    {
        m_bWritePending = FALSE;
    }

    return result;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -