-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKinectAudioStream.cpp
459 lines (390 loc) · 11.1 KB
/
KinectAudioStream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//------------------------------------------------------------------------------
// <copyright file="KinectAudioStream.cpp" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <summary>
// Implementation for KinectAudioStream methods.
// KinectAudioStream wraps Kinect audio DMO and captures audio in a dedicated thread
// while clients are allowed to read data from any other thread.
// </summary>
//------------------------------------------------------------------------------
#include "stdafx.h"
#include "SkeletonBasics.h"
#include <stdio.h>
/// <summary>
/// KinectAudioStream constructor.
/// </summary>
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);
}
/// <summary>
/// KinectAudioStream destructor.
/// </summary>
KinectAudioStream::~KinectAudioStream()
{
SafeRelease(m_pKinectDmo);
DeleteCriticalSection(&m_Lock);
}
/// <summary>
/// Starts capturing audio data from Kinect sensor.
/// </summary>
/// <returns>S_OK on success, otherwise failure code.</returns>
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;
}
/// <summary>
/// Starts capturing audio data from Kinect sensor.
/// </summary>
/// <returns>S_OK on success, otherwise failure code.</returns>
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)
{
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
/// <summary>
/// Get the next buffer available for writing audio data.
/// First attempts to find a free buffer in buffer pool and, if none is available, grabs the
/// oldest (most stale) buffer from circular queue of buffers ready for reading and re-uses it.
/// </summary>
/// <returns>Media buffer ready for audio capture, or NULL if none was found.</returns>
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;
}
/// <summary>
/// Release an audio buffer back into buffer pool.
/// </summary>
/// <param name="pBuffer">Buffer to be released.</param>
void KinectAudioStream::ReleaseBuffer(CStaticMediaBuffer* pBuffer)
{
if (pBuffer != NULL)
{
EnterCriticalSection(&m_Lock);
pBuffer->SetLength(0);
m_BufferPool.push(pBuffer);
LeaveCriticalSection(&m_Lock);
}
}
/// <summary>
/// Release all audio buffers back into buffer pool.
/// </summary>
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);
}
/// <summary>
/// Add captured audio data to the circular queue of buffers ready for client reading.
/// </summary>
/// <param name="pData">Pointer to audio data to be added to queue.</param>
/// <param name="cbData">Number of bytes to be added to queue.</param>
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);
}
}
/// <summary>
/// Add buffer full of captured audio data to the circular queue of buffers ready for client reading.
/// </summary>
/// <param name="pBuffer">Buffer holding captured audio data.</param>
void KinectAudioStream::QueueCapturedBuffer(CStaticMediaBuffer *pBuffer)
{
EnterCriticalSection(&m_Lock);
m_ReadBufferQueue.push(pBuffer);
SetEvent(m_hDataReady);
LeaveCriticalSection(&m_Lock);
}
/// <summary>
/// Perform one read operation from current read buffer until either we completely fill the specified
/// client buffer or we read until the end of (exhaust) the current read buffer.
/// If we exhaust the current read buffer, we get the oldest available buffer from circular queue and
/// make that into our current read buffer.
/// </summary>
/// <param name="ppbData">
/// In/Out pointer to client's audio data buffer to be filled.
/// </param>
/// <param name="pcbData">
/// In/Out pointer to number of bytes remaining to be filled in client's audio data buffer.
/// </param>
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);
}
/// <summary>
/// Starting address for audio capture thread.
/// </summary>
/// <param name="pParam">
/// Thread data passed to the function using the lpParameter parameter of the CreateThread function.
/// </param>
/// <returns>Non-zero if thread ended successfully, zero in case of failure</returns>
DWORD WINAPI KinectAudioStream::CaptureThread(LPVOID pParam)
{
KinectAudioStream *pthis = (KinectAudioStream *)pParam;
return pthis->CaptureThread();
}
/// <summary>
/// Audio capture thread. Captures audio data in a loop until it is signaled to stop.
/// </summary>
/// <returns>Non-zero if thread ended successfully, zero in case of failure</returns>
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;
}