plugin_wasapi_producer_audio.cxx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. /*Copyright (C) 2013 Mamadou Diop
  2. * Copyright (C) 2013 Doubango Telecom <http://www.doubango.org>
  3. *
  4. * This file is part of Open Source Doubango Framework.
  5. *
  6. * DOUBANGO is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * DOUBANGO is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with DOUBANGO.
  18. */
  19. /**@file plugin_wasapi_producer_audio.cxx
  20. * @brief Microsoft Windows Audio Session API (WASAPI) producer.
  21. * http://msdn.microsoft.com/en-us/library/windows/desktop/dd316551(v=vs.85).aspx
  22. */
  23. #include "plugin_wasapi_utils.h"
  24. #include "tinydav/audio/tdav_producer_audio.h"
  25. #include "tsk_thread.h"
  26. #include "tsk_memory.h"
  27. #include "tsk_string.h"
  28. #include "tsk_debug.h"
  29. #include <windows.h>
  30. #include <audioclient.h>
  31. #if PLUGIN_WASAPI_UNDER_WINDOWS_PHONE
  32. # include <phoneaudioclient.h>
  33. #else
  34. # include <Mmdeviceapi.h>
  35. #endif
  36. #include <initguid.h>
  37. #include <speex/speex_buffer.h>
  38. static const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
  39. static const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
  40. static const IID IID_IAudioClient = __uuidof(IAudioClient);
  41. static const IID IID_IAudioCaptureClient = __uuidof(IAudioCaptureClient);
  42. #if !defined(PLUGIN_WASAPI_PRODUCER_NOTIF_POS_COUNT)
  43. # define PLUGIN_WASAPI_PRODUCER_NOTIF_POS_COUNT 10
  44. #endif
  45. #define WASAPI_MILLIS_TO_100NS(MILLIS) (((LONGLONG)(MILLIS)) * 10000ui64)
  46. #define WASAPI_100NS_TO_MILLIS(NANOS) (((LONGLONG)(NANOS)) / 10000ui64)
  47. struct plugin_wasapi_producer_audio_s;
  48. class AudioCapture
  49. {
  50. public:
  51. AudioCapture();
  52. virtual ~AudioCapture();
  53. int Prepare(struct plugin_wasapi_producer_audio_s* wasapi, const tmedia_codec_t* codec);
  54. int UnPrepare();
  55. int Start();
  56. int Stop();
  57. int Pause();
  58. private:
  59. static void* TSK_STDCALL AsyncThread(void *pArg);
  60. private:
  61. tsk_mutex_handle_t* m_hMutex;
  62. #if PLUGIN_WASAPI_UNDER_WINDOWS_PHONE
  63. IAudioClient2* m_pDevice;
  64. #else
  65. IAudioClient* m_pDevice;
  66. #endif
  67. IAudioCaptureClient* m_pClient;
  68. HANDLE m_hCaptureEvent;
  69. HANDLE m_hShutdownEvent;
  70. tsk_thread_handle_t* m_ppTread[1];
  71. INT32 m_nBytesPerNotif;
  72. INT32 m_nSourceFrameSizeInBytes;
  73. struct {
  74. tmedia_producer_enc_cb_f fn;
  75. const void* pcData;
  76. } m_callback;
  77. struct {
  78. struct {
  79. void* buffer;
  80. tsk_size_t size;
  81. } chunck;
  82. SpeexBuffer* buffer;
  83. tsk_size_t size;
  84. } m_ring;
  85. bool m_bStarted;
  86. bool m_bPrepared;
  87. bool m_bPaused;
  88. };
  89. typedef struct plugin_wasapi_producer_audio_s {
  90. TDAV_DECLARE_PRODUCER_AUDIO;
  91. AudioCapture* pAudioCapture;
  92. }
  93. plugin_wasapi_producer_audio_t;
  94. /* ============ Media Producer Interface ================= */
  95. static int plugin_wasapi_producer_audio_set(tmedia_producer_t* self, const tmedia_param_t* param)
  96. {
  97. plugin_wasapi_producer_audio_t* wasapi = (plugin_wasapi_producer_audio_t*)self;
  98. if(param->plugin_type == tmedia_ppt_producer) {
  99. if(param->value_type == tmedia_pvt_int32) {
  100. if(tsk_striequals(param->key, "volume")) {
  101. return 0;
  102. }
  103. else if(tsk_striequals(param->key, "mute")) {
  104. //wasapi->mute = (TSK_TO_INT32((uint8_t*)param->value) != 0);
  105. #if !FIXME_SEND_SILENCE_ON_MUTE
  106. //if(wasapi->started){
  107. // if(wasapi->mute){
  108. //IDirectSoundCaptureBuffer_Stop(wasapi->captureBuffer);
  109. // }
  110. // else{
  111. //IDirectSoundCaptureBuffer_Start(wasapi->captureBuffer, DSBPLAY_LOOPING);
  112. // }
  113. //}
  114. #endif
  115. return 0;
  116. }
  117. }
  118. }
  119. return tdav_producer_audio_set(TDAV_PRODUCER_AUDIO(self), param);
  120. }
  121. static int plugin_wasapi_producer_audio_prepare(tmedia_producer_t* self, const tmedia_codec_t* codec)
  122. {
  123. plugin_wasapi_producer_audio_t* wasapi = (plugin_wasapi_producer_audio_t*)self;
  124. if(!wasapi || !codec || !wasapi->pAudioCapture) {
  125. TSK_DEBUG_ERROR("Invalid parameter");
  126. return -1;
  127. }
  128. /* codec should have ptime */
  129. TMEDIA_PRODUCER(wasapi)->audio.channels = TMEDIA_CODEC_CHANNELS_AUDIO_ENCODING(codec);
  130. TMEDIA_PRODUCER(wasapi)->audio.rate = TMEDIA_CODEC_RATE_ENCODING(codec);
  131. TMEDIA_PRODUCER(wasapi)->audio.ptime = TMEDIA_CODEC_PTIME_AUDIO_ENCODING(codec);
  132. TSK_DEBUG_INFO("WASAPI producer: channels=%d, rate=%d, ptime=%d",
  133. TMEDIA_PRODUCER(wasapi)->audio.channels,
  134. TMEDIA_PRODUCER(wasapi)->audio.rate,
  135. TMEDIA_PRODUCER(wasapi)->audio.ptime);
  136. return wasapi->pAudioCapture->Prepare(wasapi, codec);
  137. }
  138. static int plugin_wasapi_producer_audio_start(tmedia_producer_t* self)
  139. {
  140. plugin_wasapi_producer_audio_t* wasapi = (plugin_wasapi_producer_audio_t*)self;
  141. TSK_DEBUG_INFO("plugin_wasapi_producer_audio_start()");
  142. if(!wasapi || !wasapi->pAudioCapture) {
  143. TSK_DEBUG_ERROR("Invalid parameter");
  144. return -1;
  145. }
  146. return wasapi->pAudioCapture->Start();
  147. }
  148. static int plugin_wasapi_producer_audio_pause(tmedia_producer_t* self)
  149. {
  150. plugin_wasapi_producer_audio_t* wasapi = (plugin_wasapi_producer_audio_t*)self;
  151. if(!wasapi || !wasapi->pAudioCapture) {
  152. TSK_DEBUG_ERROR("Invalid parameter");
  153. return -1;
  154. }
  155. return wasapi->pAudioCapture->Pause();
  156. }
  157. static int plugin_wasapi_producer_audio_stop(tmedia_producer_t* self)
  158. {
  159. plugin_wasapi_producer_audio_t* wasapi = (plugin_wasapi_producer_audio_t*)self;
  160. TSK_DEBUG_INFO("plugin_wasapi_producer_audio_stop()");
  161. if(!wasapi || !wasapi->pAudioCapture) {
  162. TSK_DEBUG_ERROR("Invalid parameter");
  163. return -1;
  164. }
  165. return wasapi->pAudioCapture->Stop();
  166. }
  167. AudioCapture::AudioCapture()
  168. : m_pDevice(NULL)
  169. , m_hMutex(NULL)
  170. , m_pClient(NULL)
  171. , m_hCaptureEvent(NULL)
  172. , m_hShutdownEvent(NULL)
  173. , m_nBytesPerNotif(0)
  174. , m_nSourceFrameSizeInBytes(0)
  175. , m_bStarted(false)
  176. , m_bPrepared(false)
  177. , m_bPaused(false)
  178. {
  179. m_ppTread[0] = NULL;
  180. memset(&m_ring, 0, sizeof(m_ring));
  181. m_callback.fn = NULL, m_callback.pcData = NULL;
  182. if(!(m_hMutex = tsk_mutex_create())) {
  183. TSK_DEBUG_ERROR("Failed to create mutex");
  184. }
  185. }
  186. AudioCapture::~AudioCapture()
  187. {
  188. Stop();
  189. UnPrepare();
  190. tsk_mutex_destroy(&m_hMutex);
  191. }
  192. int AudioCapture::Prepare(plugin_wasapi_producer_audio_t* wasapi, const tmedia_codec_t* codec)
  193. {
  194. HRESULT hr = S_OK;
  195. int ret = 0;
  196. WAVEFORMATEX wfx = {0};
  197. #if PLUGIN_WASAPI_UNDER_WINDOWS_PHONE
  198. AudioClientProperties properties = {0};
  199. #endif
  200. IMMDeviceEnumerator *pEnumerator = NULL;
  201. LPCWSTR pwstrCaptureId = NULL;
  202. IMMDevice *pDevice = NULL;
  203. tsk_mutex_lock(m_hMutex);
  204. if(m_bPrepared) {
  205. TSK_DEBUG_INFO("#WASAPI: Audio producer already prepared");
  206. goto bail;
  207. }
  208. if(!wasapi || !codec) {
  209. TSK_DEBUG_ERROR("Invalid parameter");
  210. CHECK_HR(hr = E_FAIL);
  211. }
  212. if(m_pDevice || m_pClient) {
  213. TSK_DEBUG_ERROR("Producer already prepared");
  214. CHECK_HR(hr = E_FAIL);
  215. }
  216. #if PLUGIN_WASAPI_UNDER_WINDOWS_PHONE
  217. pwstrCaptureId = GetDefaultAudioCaptureId(AudioDeviceRole::Communications);
  218. if (NULL == pwstrCaptureId) {
  219. PLUGIN_WASAPI_ERROR("GetDefaultAudioCaptureId", HRESULT_FROM_WIN32(GetLastError()));
  220. CHECK_HR(hr = E_FAIL);
  221. }
  222. CHECK_HR(hr = ActivateAudioInterface(pwstrCaptureId, __uuidof(IAudioClient2), (void**)&m_pDevice));
  223. // Win8 or WP8 only
  224. properties.cbSize = sizeof AudioClientProperties;
  225. properties.eCategory = AudioCategory_Communications;
  226. CHECK_HR(hr = m_pDevice->SetClientProperties(&properties));
  227. #else
  228. CHECK_HR(hr = CoCreateInstance(
  229. CLSID_MMDeviceEnumerator, NULL,
  230. CLSCTX_ALL, IID_IMMDeviceEnumerator,
  231. (void**)&pEnumerator));
  232. CHECK_HR(hr = pEnumerator->GetDefaultAudioEndpoint(
  233. eCapture, eCommunications, &pDevice));
  234. CHECK_HR(hr = pDevice->Activate(
  235. IID_IAudioClient, CLSCTX_ALL,
  236. NULL, (void**)&m_pDevice));
  237. #endif
  238. /* Set best format */
  239. {
  240. wfx.wFormatTag = WAVE_FORMAT_PCM;
  241. wfx.nChannels = TMEDIA_PRODUCER(wasapi)->audio.channels;
  242. wfx.nSamplesPerSec = TMEDIA_PRODUCER(wasapi)->audio.rate;
  243. wfx.wBitsPerSample = TMEDIA_PRODUCER(wasapi)->audio.bits_per_sample;
  244. wfx.nBlockAlign = (wfx.nChannels * wfx.wBitsPerSample/8);
  245. wfx.nAvgBytesPerSec = (wfx.nSamplesPerSec * wfx.nBlockAlign);
  246. PWAVEFORMATEX pwfxClosestMatch = NULL;
  247. hr = m_pDevice->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, &wfx, &pwfxClosestMatch);
  248. if(hr != S_OK && hr != S_FALSE) {
  249. CHECK_HR(hr);
  250. }
  251. if(hr == S_FALSE) {
  252. if(!pwfxClosestMatch) {
  253. CHECK_HR(hr = E_OUTOFMEMORY);
  254. }
  255. wfx.nChannels = pwfxClosestMatch->nChannels;
  256. wfx.nSamplesPerSec = pwfxClosestMatch->nSamplesPerSec;
  257. #if 0
  258. wfx.wBitsPerSample = pwfxClosestMatch->wBitsPerSample;
  259. #endif
  260. wfx.nBlockAlign = wfx.nChannels * (wfx.wBitsPerSample / 8);
  261. wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
  262. // Request resampler
  263. TMEDIA_PRODUCER(wasapi)->audio.rate = (uint32_t)wfx.nSamplesPerSec;
  264. TMEDIA_PRODUCER(wasapi)->audio.bits_per_sample = (uint8_t)wfx.wBitsPerSample;
  265. TMEDIA_PRODUCER(wasapi)->audio.channels = (uint8_t)wfx.nChannels;
  266. TSK_DEBUG_INFO("Audio device format fallback: rate=%d, bps=%d, channels=%d", wfx.nSamplesPerSec, wfx.wBitsPerSample, wfx.nChannels);
  267. }
  268. if(pwfxClosestMatch) {
  269. CoTaskMemFree(pwfxClosestMatch);
  270. }
  271. }
  272. m_nSourceFrameSizeInBytes = (wfx.wBitsPerSample >> 3) * wfx.nChannels;
  273. m_nBytesPerNotif = ((wfx.nAvgBytesPerSec * TMEDIA_PRODUCER(wasapi)->audio.ptime)/1000) * wfx.nChannels;
  274. // Initialize
  275. CHECK_HR(hr = m_pDevice->Initialize(
  276. AUDCLNT_SHAREMODE_SHARED,
  277. AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
  278. (PLUGIN_WASAPI_PRODUCER_NOTIF_POS_COUNT * WASAPI_MILLIS_TO_100NS(TMEDIA_PRODUCER(wasapi)->audio.ptime)),
  279. 0,
  280. &wfx,
  281. NULL));
  282. REFERENCE_TIME DefaultDevicePeriod, MinimumDevicePeriod;
  283. CHECK_HR(hr = m_pDevice->GetDevicePeriod(&DefaultDevicePeriod, &MinimumDevicePeriod));
  284. TSK_DEBUG_INFO("#WASAPI(Capture): DefaultDevicePeriod=%lld ms, MinimumDevicePeriod=%lldms", WASAPI_100NS_TO_MILLIS(DefaultDevicePeriod), WASAPI_100NS_TO_MILLIS(MinimumDevicePeriod));
  285. if(!m_hCaptureEvent) {
  286. if(!(m_hCaptureEvent = CreateEventEx(NULL, NULL, 0, EVENT_ALL_ACCESS))) {
  287. PLUGIN_WASAPI_ERROR(HRESULT_FROM_WIN32(GetLastError()));
  288. CHECK_HR(hr = E_FAIL);
  289. }
  290. }
  291. if(!m_hShutdownEvent) {
  292. if(!(m_hShutdownEvent = CreateEventEx(NULL, NULL, CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS))) {
  293. PLUGIN_WASAPI_ERROR(HRESULT_FROM_WIN32(GetLastError()));
  294. CHECK_HR(hr = E_FAIL);
  295. }
  296. }
  297. CHECK_HR(hr = m_pDevice->SetEventHandle(m_hCaptureEvent));
  298. CHECK_HR(hr = m_pDevice->GetService(__uuidof(IAudioCaptureClient), (void**)&m_pClient));
  299. int packetperbuffer = (1000 / TMEDIA_PRODUCER(wasapi)->audio.ptime);
  300. m_ring.chunck.size = (wfx.nSamplesPerSec * (wfx.wBitsPerSample >> 3) / packetperbuffer) * wfx.nChannels;
  301. TSK_DEBUG_INFO("#WASAPI: Audio producer ring chunk size = %u", m_ring.chunck.size);
  302. // allocate our chunck buffer
  303. if(!(m_ring.chunck.buffer = tsk_realloc(m_ring.chunck.buffer, m_ring.chunck.size))) {
  304. TSK_DEBUG_ERROR("Failed to allocate new buffer");
  305. CHECK_HR(hr = E_OUTOFMEMORY);
  306. }
  307. // create ringbuffer
  308. m_ring.size = PLUGIN_WASAPI_PRODUCER_NOTIF_POS_COUNT * m_ring.chunck.size;
  309. TSK_DEBUG_INFO("#WASAPI: Audio producer ring size = %u", m_ring.size);
  310. if(!m_ring.buffer) {
  311. m_ring.buffer = speex_buffer_init(m_ring.size);
  312. }
  313. else {
  314. int sret;
  315. if((sret = speex_buffer_resize(m_ring.buffer, m_ring.size)) < 0) {
  316. TSK_DEBUG_ERROR("speex_buffer_resize(%d) failed with error code=%d", m_ring.size, sret);
  317. CHECK_HR(hr = E_OUTOFMEMORY);
  318. }
  319. }
  320. if(!m_ring.buffer) {
  321. TSK_DEBUG_ERROR("Failed to create a new ring buffer with size = %d", m_ring.size);
  322. CHECK_HR(hr = E_OUTOFMEMORY);
  323. }
  324. m_callback.fn = TMEDIA_PRODUCER(wasapi)->enc_cb.callback;
  325. m_callback.pcData = TMEDIA_PRODUCER(wasapi)->enc_cb.callback_data;
  326. bail:
  327. ret = SUCCEEDED(hr) ? 0 : -1;
  328. if (pwstrCaptureId) {
  329. CoTaskMemFree((LPVOID)pwstrCaptureId);
  330. }
  331. if(ret != 0) {
  332. UnPrepare();
  333. }
  334. m_bPrepared = (ret == 0);
  335. tsk_mutex_unlock(m_hMutex);
  336. SafeRelease(&pEnumerator);
  337. SafeRelease(&pDevice);
  338. return ret;
  339. }
  340. int AudioCapture::UnPrepare()
  341. {
  342. tsk_mutex_lock(m_hMutex);
  343. if(m_hCaptureEvent) {
  344. CloseHandle(m_hCaptureEvent), m_hCaptureEvent = NULL;
  345. }
  346. if(m_hShutdownEvent) {
  347. CloseHandle(m_hShutdownEvent), m_hShutdownEvent = NULL;
  348. }
  349. if(m_pDevice) {
  350. m_pDevice->Release(), m_pDevice = NULL;
  351. }
  352. if(m_pClient) {
  353. m_pClient->Release(), m_pClient = NULL;
  354. }
  355. TSK_FREE(m_ring.chunck.buffer);
  356. if(m_ring.buffer) {
  357. speex_buffer_destroy(m_ring.buffer);
  358. m_ring.buffer = NULL;
  359. }
  360. m_callback.fn = NULL;
  361. m_callback.pcData = NULL;
  362. m_bPrepared = false;
  363. tsk_mutex_unlock(m_hMutex);
  364. return 0;
  365. }
  366. int AudioCapture::Start()
  367. {
  368. tsk_mutex_lock(m_hMutex);
  369. if(m_bStarted) {
  370. TSK_DEBUG_INFO("#WASAPI: Audio producer already started");
  371. goto bail;
  372. }
  373. if(!m_bPrepared) {
  374. TSK_DEBUG_ERROR("Audio producer not prepared");
  375. goto bail;
  376. }
  377. m_bStarted = true;
  378. if(!m_ppTread[0] && tsk_thread_create(m_ppTread, AudioCapture::AsyncThread, this) != 0) {
  379. m_bStarted = false;
  380. goto bail;
  381. }
  382. HRESULT hr = m_pDevice->Start();
  383. if(!SUCCEEDED(hr)) {
  384. PLUGIN_WASAPI_ERROR(hr);
  385. Stop();
  386. }
  387. m_bPaused = false;
  388. bail:
  389. tsk_mutex_unlock(m_hMutex);
  390. return (m_bStarted ? 0 : -2);
  391. }
  392. int AudioCapture::Stop()
  393. {
  394. m_bStarted = false;
  395. tsk_mutex_lock(m_hMutex);
  396. if (m_hShutdownEvent) {
  397. SetEvent(m_hShutdownEvent);
  398. }
  399. if (m_ppTread[0]) {
  400. tsk_thread_join(m_ppTread);
  401. }
  402. if(m_pDevice) {
  403. m_pDevice->Stop();
  404. }
  405. // will be prepared again before next start()
  406. UnPrepare();
  407. tsk_mutex_unlock(m_hMutex);
  408. return 0;
  409. }
  410. int AudioCapture::Pause()
  411. {
  412. tsk_mutex_lock(m_hMutex);
  413. m_bPaused = true;
  414. tsk_mutex_unlock(m_hMutex);
  415. return 0;
  416. }
  417. void* TSK_STDCALL AudioCapture::AsyncThread(void *pArg)
  418. {
  419. HRESULT hr = S_OK;
  420. BYTE* pbData = NULL;
  421. UINT32 nFrames = 0;
  422. DWORD dwFlags = 0;
  423. UINT32 incomingBufferSize;
  424. INT32 avail;
  425. UINT32 nNextPacketSize;
  426. AudioCapture* This = (AudioCapture*)pArg;
  427. HANDLE eventHandles[] = {
  428. This->m_hCaptureEvent, // WAIT_OBJECT0
  429. This->m_hShutdownEvent // WAIT_OBJECT1
  430. };
  431. TSK_DEBUG_INFO("#WASAPI: __record_thread -- START");
  432. #define BREAK_WHILE tsk_mutex_unlock(This->m_hMutex); break;
  433. while(This->m_bStarted && SUCCEEDED(hr)) {
  434. DWORD waitResult = WaitForMultipleObjectsEx(SIZEOF_ARRAY(eventHandles), eventHandles, FALSE, INFINITE, FALSE);
  435. tsk_mutex_lock(This->m_hMutex);
  436. if(!This->m_bStarted) {
  437. BREAK_WHILE;
  438. }
  439. if(waitResult == WAIT_OBJECT_0 && This->m_callback.fn) {
  440. hr = This->m_pClient->GetNextPacketSize(&nNextPacketSize);
  441. while(SUCCEEDED(hr) && nNextPacketSize >0) {
  442. hr = This->m_pClient->GetBuffer(&pbData, &nFrames, &dwFlags, NULL, NULL);
  443. if(SUCCEEDED(hr) && pbData && nFrames) {
  444. if((dwFlags & AUDCLNT_BUFFERFLAGS_SILENT) != AUDCLNT_BUFFERFLAGS_SILENT) {
  445. incomingBufferSize = nFrames * This->m_nSourceFrameSizeInBytes;
  446. speex_buffer_write(This->m_ring.buffer, pbData, incomingBufferSize);
  447. avail = speex_buffer_get_available(This->m_ring.buffer);
  448. while (This->m_bStarted && avail >= (INT32)This->m_ring.chunck.size) {
  449. avail -= speex_buffer_read(This->m_ring.buffer, This->m_ring.chunck.buffer, This->m_ring.chunck.size);
  450. #if 0
  451. {
  452. static FILE* f = fopen("./wasapi_producer.raw", "w+");
  453. fwrite(This->m_ring.chunck.buffer, 1, This->m_ring.chunck.size, f);
  454. }
  455. #endif
  456. This->m_callback.fn(This->m_callback.pcData, This->m_ring.chunck.buffer, This->m_ring.chunck.size);
  457. }
  458. }
  459. if (SUCCEEDED(hr)) {
  460. hr = This->m_pClient->ReleaseBuffer(nFrames);
  461. }
  462. if (SUCCEEDED(hr)) {
  463. hr = This->m_pClient->GetNextPacketSize(&nNextPacketSize);
  464. }
  465. }
  466. }
  467. }
  468. else if(waitResult != WAIT_OBJECT_0) {
  469. BREAK_WHILE;
  470. }
  471. tsk_mutex_unlock(This->m_hMutex);
  472. }// end-of-while
  473. if (!SUCCEEDED(hr)) {
  474. PLUGIN_WASAPI_ERROR(hr);
  475. }
  476. TSK_DEBUG_INFO("WASAPI: __record_thread(%s) -- STOP", SUCCEEDED(hr) ? "OK": "NOK");
  477. return NULL;
  478. }
  479. //
  480. // WaveAPI producer object definition
  481. //
  482. /* constructor */
  483. static tsk_object_t* plugin_wasapi_producer_audio_ctor(tsk_object_t * self, va_list * app)
  484. {
  485. plugin_wasapi_producer_audio_t *wasapi = (plugin_wasapi_producer_audio_t*)self;
  486. if(wasapi) {
  487. WASAPIUtils::Startup();
  488. /* init base */
  489. tdav_producer_audio_init(TDAV_PRODUCER_AUDIO(wasapi));
  490. /* init self */
  491. wasapi->pAudioCapture = new AudioCapture();
  492. if(!wasapi->pAudioCapture) {
  493. TSK_DEBUG_ERROR("Failed to create Audio capture device");
  494. return tsk_null;
  495. }
  496. }
  497. return self;
  498. }
  499. /* destructor */
  500. static tsk_object_t* plugin_wasapi_producer_audio_dtor(tsk_object_t * self)
  501. {
  502. plugin_wasapi_producer_audio_t *wasapi = (plugin_wasapi_producer_audio_t*)self;
  503. if(wasapi) {
  504. /* stop */
  505. plugin_wasapi_producer_audio_stop((tmedia_producer_t*)self);
  506. /* deinit base */
  507. tdav_producer_audio_deinit(TDAV_PRODUCER_AUDIO(wasapi));
  508. /* deinit self */
  509. if(wasapi->pAudioCapture) {
  510. delete wasapi->pAudioCapture;
  511. wasapi->pAudioCapture = NULL;
  512. }
  513. }
  514. return self;
  515. }
  516. /* object definition */
  517. static const tsk_object_def_t plugin_wasapi_producer_audio_def_s = {
  518. sizeof(plugin_wasapi_producer_audio_t),
  519. plugin_wasapi_producer_audio_ctor,
  520. plugin_wasapi_producer_audio_dtor,
  521. tdav_producer_audio_cmp,
  522. };
  523. /* plugin definition*/
  524. static const tmedia_producer_plugin_def_t plugin_wasapi_producer_audio_plugin_def_s = {
  525. &plugin_wasapi_producer_audio_def_s,
  526. tmedia_audio,
  527. "Microsoft Windows Audio Session API (WASAPI) producer",
  528. plugin_wasapi_producer_audio_set,
  529. plugin_wasapi_producer_audio_prepare,
  530. plugin_wasapi_producer_audio_start,
  531. plugin_wasapi_producer_audio_pause,
  532. plugin_wasapi_producer_audio_stop
  533. };
  534. const tmedia_producer_plugin_def_t *plugin_wasapi_producer_audio_plugin_def_t = &plugin_wasapi_producer_audio_plugin_def_s;