devices/devicemocap/speech/kinectaudiostream.cxx

devices/devicemocap/speech/kinectaudiostream.cxx
/***************************************************************************************
Autodesk(R) Open Reality(R) Samples
(C) 2013 Autodesk, Inc. and/or its licensors
All rights reserved.
AUTODESK SOFTWARE LICENSE AGREEMENT
Autodesk, Inc. licenses this Software to you only upon the condition that
you accept all of the terms contained in the Software License Agreement ("Agreement")
that is embedded in or that is delivered with this Software. By selecting
the "I ACCEPT" button at the end of the Agreement or by copying, installing,
uploading, accessing or using all or any portion of the Software you agree
to enter into the Agreement. A contract is then formed between Autodesk and
either you personally, if you acquire the Software for yourself, or the company
or other legal entity for which you are acquiring the software.
AUTODESK, INC., MAKES NO WARRANTY, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
PURPOSE REGARDING THESE MATERIALS, AND MAKES SUCH MATERIALS AVAILABLE SOLELY ON AN
"AS-IS" BASIS.
IN NO EVENT SHALL AUTODESK, INC., BE LIABLE TO ANYONE FOR SPECIAL, COLLATERAL,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES IN CONNECTION WITH OR ARISING OUT OF PURCHASE
OR USE OF THESE MATERIALS. THE SOLE AND EXCLUSIVE LIABILITY TO AUTODESK, INC.,
REGARDLESS OF THE FORM OF ACTION, SHALL NOT EXCEED THE PURCHASE PRICE OF THE
MATERIALS DESCRIBED HEREIN.
Autodesk, Inc., reserves the right to revise and improve its products as it sees fit.
Autodesk and Open Reality are registered trademarks or trademarks of Autodesk, Inc.,
in the U.S.A. and/or other countries. All other brand names, product names, or
trademarks belong to their respective holders.
GOVERNMENT USE
Use, duplication, or disclosure by the U.S. Government is subject to restrictions as
set forth in FAR 12.212 (Commercial Computer Software-Restricted Rights) and
DFAR 227.7202 (Rights in Technical Data and Computer Software), as applicable.
Manufacturer is Autodesk, Inc., 10 Duke Street, Montreal, Quebec, Canada, H3C 2L7.
***************************************************************************************/
#include "stdafx.h"
#include "kinectaudiostream.h"
#include <stdio.h>
KinectAudioStream::KinectAudioStream(IMediaObject *pKinectDmo) :
m_cRef(1),
m_CurrentWriteBuffer(NULL),
m_CurrentReadBuffer(NULL),
m_CurrentReadBufferIndex(0),
m_BytesRead(0),
m_hStopEvent(NULL),
m_hDataReady(NULL),
m_hCaptureThread(NULL)
{
pKinectDmo->AddRef();
m_pKinectDmo = pKinectDmo;
InitializeCriticalSection(&m_Lock);
}
KinectAudioStream::~KinectAudioStream()
{
SafeRelease(m_pKinectDmo);
DeleteCriticalSection(&m_Lock);
}
HRESULT KinectAudioStream::StartCapture()
{
HRESULT hr = S_OK;
m_hStopEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
m_hDataReady = CreateEvent( NULL, FALSE, FALSE, NULL );
m_BytesRead = 0;
for (UINT i = 0; i < NumBuffers; i++)
{
CStaticMediaBuffer *pBuf = new CStaticMediaBuffer();
m_BufferPool.push(pBuf);
}
m_CurrentWriteBuffer = NULL;
m_hCaptureThread = CreateThread(NULL, 0, CaptureThread, this, 0, NULL);
return hr;
}
HRESULT KinectAudioStream::StopCapture()
{
HRESULT hr = S_OK;
if ( NULL != m_hStopEvent )
{
// Signal the thread
SetEvent(m_hStopEvent);
// Wait for thread to stop
if ( NULL != m_hCaptureThread )
{
WaitForSingleObject( m_hCaptureThread, INFINITE );
CloseHandle( m_hCaptureThread );
m_hCaptureThread = NULL;
}
CloseHandle( m_hStopEvent );
m_hStopEvent = NULL;
}
if (NULL != m_hDataReady)
{
SetEvent(m_hDataReady);
CloseHandle(m_hDataReady);
m_hDataReady = NULL;
}
// Release all buffers to buffer pool and then free all buffers in pool
ReleaseAllBuffers();
while (!m_BufferPool.empty())
{
CStaticMediaBuffer* mediaBuffer = m_BufferPool.top();
delete mediaBuffer;
m_BufferPool.pop();
}
return hr;
}
// IStream methods
STDMETHODIMP KinectAudioStream::Read(void *pBuffer, ULONG cbBuffer, ULONG *pcbRead)
{
HRESULT hr = S_OK;
if (pcbRead == NULL)
{
return E_INVALIDARG;
}
ULONG bytesPendingToRead = cbBuffer;
while (bytesPendingToRead > 0 && IsCapturing())
{
ReadOneBuffer((BYTE**)&pBuffer, &bytesPendingToRead);
if (NULL == m_CurrentReadBuffer) //no data, wait ...
{
WaitForSingleObject(m_hDataReady, INFINITE);
}
}
ULONG bytesRead = cbBuffer - bytesPendingToRead;
m_BytesRead += bytesRead;
*pcbRead = bytesRead;
return hr;
}
STDMETHODIMP KinectAudioStream::Write(const void *,ULONG,ULONG *)
{
return E_NOTIMPL;
}
STDMETHODIMP KinectAudioStream::Seek(LARGE_INTEGER dlibMove,DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition )
{
dwOrigin = dwOrigin;
if (plibNewPosition != NULL)
{
plibNewPosition->QuadPart = m_BytesRead + dlibMove.QuadPart;
}
return S_OK;
}
STDMETHODIMP KinectAudioStream::SetSize(ULARGE_INTEGER)
{
return E_NOTIMPL;
}
STDMETHODIMP KinectAudioStream::CopyTo(IStream *,ULARGE_INTEGER,ULARGE_INTEGER *,ULARGE_INTEGER *)
{
return E_NOTIMPL;
}
STDMETHODIMP KinectAudioStream::Commit(DWORD)
{
return E_NOTIMPL;
}
STDMETHODIMP KinectAudioStream::Revert()
{
return E_NOTIMPL;
}
STDMETHODIMP KinectAudioStream::LockRegion(ULARGE_INTEGER,ULARGE_INTEGER,DWORD)
{
return E_NOTIMPL;
}
STDMETHODIMP KinectAudioStream::UnlockRegion(ULARGE_INTEGER,ULARGE_INTEGER,DWORD)
{
return E_NOTIMPL;
}
STDMETHODIMP KinectAudioStream::Stat(STATSTG *,DWORD)
{
return E_NOTIMPL;
}
STDMETHODIMP KinectAudioStream::Clone(IStream **)
{
return E_NOTIMPL;
}
// Private KinectAudioStream methods
CStaticMediaBuffer *KinectAudioStream::GetWriteBuffer()
{
CStaticMediaBuffer *pBuf = NULL;
EnterCriticalSection(&m_Lock);
//Get a free buffer if available. Otherwise, get the oldest buffer
//from the read queue. This is a way of overwriting the oldest data
if (m_BufferPool.size() > 0)
{
pBuf = m_BufferPool.top();
m_BufferPool.pop();
pBuf->SetLength(0);
}
else if (m_ReadBufferQueue.size() > 0)
{
pBuf = m_ReadBufferQueue.front();
m_ReadBufferQueue.pop();
pBuf->SetLength(0);
}
LeaveCriticalSection(&m_Lock);
return pBuf;
}
void KinectAudioStream::ReleaseBuffer(CStaticMediaBuffer* pBuffer)
{
if (pBuffer != NULL)
{
EnterCriticalSection(&m_Lock);
pBuffer->SetLength(0);
m_BufferPool.push(pBuffer);
LeaveCriticalSection(&m_Lock);
}
}
void KinectAudioStream::ReleaseAllBuffers()
{
EnterCriticalSection(&m_Lock);
while (m_ReadBufferQueue.size() > 0)
{
CStaticMediaBuffer *pBuf = m_ReadBufferQueue.front();
m_ReadBufferQueue.pop();
ReleaseBuffer(pBuf);
}
if (m_CurrentReadBuffer != NULL)
{
ReleaseBuffer(m_CurrentReadBuffer);
}
m_CurrentReadBufferIndex = 0;
m_CurrentReadBuffer = NULL;
LeaveCriticalSection(&m_Lock);
}
void KinectAudioStream::QueueCapturedData(BYTE *pData, UINT cbData)
{
BYTE *pWriteData = NULL;
DWORD cbWriteData = 0;
DWORD cbMaxLength = 0;
if (cbData <= 0)
{
return;
}
if (NULL == m_CurrentWriteBuffer)
{
m_CurrentWriteBuffer = GetWriteBuffer();
}
m_CurrentWriteBuffer->GetBufferAndLength(&pWriteData, &cbWriteData);
m_CurrentWriteBuffer->GetMaxLength(&cbMaxLength);
if (cbWriteData + cbData < cbMaxLength)
{
memcpy(pWriteData + cbWriteData, pData, cbData);
m_CurrentWriteBuffer->SetLength(cbWriteData + cbData);
}
else
{
QueueCapturedBuffer(m_CurrentWriteBuffer);
m_CurrentWriteBuffer = GetWriteBuffer();
m_CurrentWriteBuffer->GetBufferAndLength(&pWriteData, &cbWriteData);
memcpy(pWriteData, pData, cbData);
m_CurrentWriteBuffer->SetLength(cbData);
}
}
void KinectAudioStream::QueueCapturedBuffer(CStaticMediaBuffer *pBuffer)
{
EnterCriticalSection(&m_Lock);
m_ReadBufferQueue.push(pBuffer);
SetEvent(m_hDataReady);
LeaveCriticalSection(&m_Lock);
}
void KinectAudioStream::ReadOneBuffer(BYTE **ppbData, ULONG* pcbData)
{
EnterCriticalSection(&m_Lock);
//Do we already have a buffer we are reading from? Otherwise grab one from the queue
if (m_CurrentReadBuffer == NULL)
{
if(m_ReadBufferQueue.size() != 0)
{
m_CurrentReadBuffer = m_ReadBufferQueue.front();
m_ReadBufferQueue.pop();
}
}
if (m_CurrentReadBuffer != NULL)
{
//Copy as much data as we can or need
BYTE *pData = NULL;
DWORD dwDataLength = 0;
m_CurrentReadBuffer->GetBufferAndLength(&pData, &dwDataLength);
ULONG cbToCopy = min(dwDataLength - m_CurrentReadBufferIndex, *pcbData);
memcpy(*ppbData, pData + m_CurrentReadBufferIndex, cbToCopy);
*ppbData = (*ppbData)+cbToCopy;
*pcbData = (*pcbData)-cbToCopy;
m_CurrentReadBufferIndex += cbToCopy;
//If we are done with this buffer put it back in the queue
if (m_CurrentReadBufferIndex >= dwDataLength)
{
ReleaseBuffer(m_CurrentReadBuffer);
m_CurrentReadBuffer = NULL;
m_CurrentReadBufferIndex = 0;
if(m_ReadBufferQueue.size() != 0)
{
m_CurrentReadBuffer = m_ReadBufferQueue.front();
m_ReadBufferQueue.pop();
}
}
}
LeaveCriticalSection(&m_Lock);
}
DWORD WINAPI KinectAudioStream::CaptureThread(LPVOID pParam)
{
KinectAudioStream *pthis = (KinectAudioStream *) pParam;
return pthis->CaptureThread();
}
DWORD WINAPI KinectAudioStream::CaptureThread()
{
HANDLE mmHandle = NULL;
DWORD mmTaskIndex = 0;
HRESULT hr = S_OK;
bool bContinue = true;
BYTE *pbOutputBuffer = NULL;
CStaticMediaBuffer outputBuffer;
DMO_OUTPUT_DATA_BUFFER OutputBufferStruct = {0};
OutputBufferStruct.pBuffer = &outputBuffer;
DWORD dwStatus = 0;
ULONG cbProduced = 0;
// Set high priority to avoid getting preempted while capturing sound
mmHandle = AvSetMmThreadCharacteristics(L"Audio", &mmTaskIndex);
while (bContinue)
{
if (WaitForSingleObject(m_hStopEvent, 0) == WAIT_OBJECT_0)
{
bContinue = false;
continue;
}
do
{
outputBuffer.Init(0);
OutputBufferStruct.dwStatus = 0;
hr = m_pKinectDmo->ProcessOutput(0, 1, &OutputBufferStruct, &dwStatus);
if (FAILED(hr))
{
bContinue = false;
break;
}
if (hr == S_FALSE)
{
cbProduced = 0;
}
else
{
outputBuffer.GetBufferAndLength(&pbOutputBuffer, &cbProduced);
}
// Queue audio data to be read by IStream client
if (cbProduced > 0)
{
QueueCapturedData(pbOutputBuffer, cbProduced);
}
} while (OutputBufferStruct.dwStatus & DMO_OUTPUT_DATA_BUFFERF_INCOMPLETE);
Sleep(10); //sleep 10ms
}
SetEvent(m_hDataReady);
AvRevertMmThreadCharacteristics(mmHandle);
if (FAILED(hr))
{
return 0;
}
return 1;
}