wasapi-output.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #include "../../media-io/audio-resampler.h"
  2. #include "../../util/circlebuf.h"
  3. #include "../../util/platform.h"
  4. #include "../../util/darray.h"
  5. #include "../../obs-internal.h"
  6. #include "wasapi-output.h"
  7. #define ACTUALLY_DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
  8. EXTERN_C const GUID DECLSPEC_SELECTANY name \
  9. = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
  10. ACTUALLY_DEFINE_GUID(CLSID_MMDeviceEnumerator,
  11. 0xBCDE0395, 0xE52F, 0x467C,
  12. 0x8E, 0x3D, 0xC4, 0x57, 0x92, 0x91, 0x69, 0x2E);
  13. ACTUALLY_DEFINE_GUID(IID_IMMDeviceEnumerator,
  14. 0xA95664D2, 0x9614, 0x4F35,
  15. 0xA7, 0x46, 0xDE, 0x8D, 0xB6, 0x36, 0x17, 0xE6);
  16. ACTUALLY_DEFINE_GUID(IID_IAudioClient,
  17. 0x1CB9AD4C, 0xDBFA, 0x4C32,
  18. 0xB1, 0x78, 0xC2, 0xF5, 0x68, 0xA7, 0x03, 0xB2);
  19. ACTUALLY_DEFINE_GUID(IID_IAudioRenderClient,
  20. 0xF294ACFC, 0x3146, 0x4483,
  21. 0xA7, 0xBF, 0xAD, 0xDC, 0xA7, 0xC2, 0x60, 0xE2);
  22. struct audio_monitor {
  23. obs_source_t *source;
  24. IMMDevice *device;
  25. IAudioClient *client;
  26. IAudioRenderClient *render;
  27. uint64_t last_recv_time;
  28. uint64_t prev_video_ts;
  29. uint64_t time_since_prev;
  30. audio_resampler_t *resampler;
  31. uint32_t sample_rate;
  32. uint32_t channels;
  33. bool source_has_video : 1;
  34. int64_t lowest_audio_offset;
  35. struct circlebuf delay_buffer;
  36. uint32_t delay_size;
  37. DARRAY(float) buf;
  38. pthread_mutex_t playback_mutex;
  39. };
  40. /* #define DEBUG_AUDIO */
  41. static bool process_audio_delay(struct audio_monitor *monitor,
  42. float **data, uint32_t *frames, uint64_t ts, uint32_t pad)
  43. {
  44. obs_source_t *s = monitor->source;
  45. uint64_t last_frame_ts = s->last_frame_ts;
  46. uint64_t cur_time = os_gettime_ns();
  47. uint64_t front_ts;
  48. uint64_t cur_ts;
  49. int64_t diff;
  50. uint32_t blocksize = monitor->channels * sizeof(float);
  51. /* cut off audio if long-since leftover audio in delay buffer */
  52. if (cur_time - monitor->last_recv_time > 1000000000)
  53. circlebuf_free(&monitor->delay_buffer);
  54. monitor->last_recv_time = cur_time;
  55. ts += monitor->source->sync_offset;
  56. circlebuf_push_back(&monitor->delay_buffer, &ts, sizeof(ts));
  57. circlebuf_push_back(&monitor->delay_buffer, frames, sizeof(*frames));
  58. circlebuf_push_back(&monitor->delay_buffer, *data,
  59. *frames * blocksize);
  60. if (!monitor->prev_video_ts) {
  61. monitor->prev_video_ts = last_frame_ts;
  62. } else if (monitor->prev_video_ts == last_frame_ts) {
  63. monitor->time_since_prev += (uint64_t)*frames *
  64. 1000000000ULL / (uint64_t)monitor->sample_rate;
  65. } else {
  66. monitor->time_since_prev = 0;
  67. }
  68. while (monitor->delay_buffer.size != 0) {
  69. size_t size;
  70. bool bad_diff;
  71. circlebuf_peek_front(&monitor->delay_buffer, &cur_ts,
  72. sizeof(ts));
  73. front_ts = cur_ts -
  74. ((uint64_t)pad * 1000000000ULL /
  75. (uint64_t)monitor->sample_rate);
  76. diff = (int64_t)front_ts - (int64_t)last_frame_ts;
  77. bad_diff = !last_frame_ts ||
  78. llabs(diff) > 5000000000 ||
  79. monitor->time_since_prev > 100000000ULL;
  80. /* delay audio if rushing */
  81. if (!bad_diff && diff > 75000000) {
  82. #ifdef DEBUG_AUDIO
  83. blog(LOG_INFO, "audio rushing, cutting audio, "
  84. "diff: %lld, delay buffer size: %lu, "
  85. "v: %llu: a: %llu",
  86. diff, (int)monitor->delay_buffer.size,
  87. last_frame_ts, front_ts);
  88. #endif
  89. return false;
  90. }
  91. circlebuf_pop_front(&monitor->delay_buffer, NULL, sizeof(ts));
  92. circlebuf_pop_front(&monitor->delay_buffer, frames,
  93. sizeof(*frames));
  94. size = *frames * blocksize;
  95. da_resize(monitor->buf, size);
  96. circlebuf_pop_front(&monitor->delay_buffer,
  97. monitor->buf.array, size);
  98. /* cut audio if dragging */
  99. if (!bad_diff && diff < -75000000 && monitor->delay_buffer.size > 0) {
  100. #ifdef DEBUG_AUDIO
  101. blog(LOG_INFO, "audio dragging, cutting audio, "
  102. "diff: %lld, delay buffer size: %lu, "
  103. "v: %llu: a: %llu",
  104. diff, (int)monitor->delay_buffer.size,
  105. last_frame_ts, front_ts);
  106. #endif
  107. continue;
  108. }
  109. *data = monitor->buf.array;
  110. return true;
  111. }
  112. return false;
  113. }
  114. static void on_audio_playback(void *param, obs_source_t *source,
  115. const struct audio_data *audio_data, bool muted)
  116. {
  117. struct audio_monitor *monitor = param;
  118. IAudioRenderClient *render = monitor->render;
  119. uint8_t *resample_data[MAX_AV_PLANES];
  120. float vol = source->user_volume;
  121. uint32_t resample_frames;
  122. uint64_t ts_offset;
  123. bool success;
  124. BYTE *output;
  125. if (pthread_mutex_trylock(&monitor->playback_mutex) != 0) {
  126. return;
  127. }
  128. if (os_atomic_load_long(&source->activate_refs) == 0) {
  129. goto unlock;
  130. }
  131. success = audio_resampler_resample(monitor->resampler, resample_data,
  132. &resample_frames, &ts_offset,
  133. (const uint8_t *const *)audio_data->data,
  134. (uint32_t)audio_data->frames);
  135. if (!success) {
  136. goto unlock;
  137. }
  138. UINT32 pad = 0;
  139. monitor->client->lpVtbl->GetCurrentPadding(monitor->client, &pad);
  140. if (monitor->source_has_video) {
  141. uint64_t ts = audio_data->timestamp - ts_offset;
  142. if (!process_audio_delay(monitor, (float**)(&resample_data[0]),
  143. &resample_frames, ts, pad)) {
  144. goto unlock;
  145. }
  146. }
  147. HRESULT hr = render->lpVtbl->GetBuffer(render, resample_frames,
  148. &output);
  149. if (FAILED(hr)) {
  150. goto unlock;
  151. }
  152. if (!muted) {
  153. /* apply volume */
  154. if (!close_float(vol, 1.0f, EPSILON)) {
  155. register float *cur = (float*)resample_data[0];
  156. register float *end = cur +
  157. resample_frames * monitor->channels;
  158. while (cur < end)
  159. *(cur++) *= vol;
  160. }
  161. memcpy(output, resample_data[0],
  162. resample_frames * monitor->channels *
  163. sizeof(float));
  164. }
  165. render->lpVtbl->ReleaseBuffer(render, resample_frames,
  166. muted ? AUDCLNT_BUFFERFLAGS_SILENT : 0);
  167. unlock:
  168. pthread_mutex_unlock(&monitor->playback_mutex);
  169. }
  170. static inline void audio_monitor_free(struct audio_monitor *monitor)
  171. {
  172. if (monitor->source) {
  173. obs_source_remove_audio_capture_callback(
  174. monitor->source, on_audio_playback, monitor);
  175. }
  176. if (monitor->client)
  177. monitor->client->lpVtbl->Stop(monitor->client);
  178. safe_release(monitor->device);
  179. safe_release(monitor->client);
  180. safe_release(monitor->render);
  181. audio_resampler_destroy(monitor->resampler);
  182. circlebuf_free(&monitor->delay_buffer);
  183. da_free(monitor->buf);
  184. }
  185. static enum speaker_layout convert_speaker_layout(DWORD layout, WORD channels)
  186. {
  187. switch (layout) {
  188. case KSAUDIO_SPEAKER_QUAD: return SPEAKERS_QUAD;
  189. case KSAUDIO_SPEAKER_2POINT1: return SPEAKERS_2POINT1;
  190. case KSAUDIO_SPEAKER_4POINT1: return SPEAKERS_4POINT1;
  191. case KSAUDIO_SPEAKER_SURROUND: return SPEAKERS_SURROUND;
  192. case KSAUDIO_SPEAKER_5POINT1: return SPEAKERS_5POINT1;
  193. case KSAUDIO_SPEAKER_5POINT1_SURROUND: return SPEAKERS_5POINT1_SURROUND;
  194. case KSAUDIO_SPEAKER_7POINT1: return SPEAKERS_7POINT1;
  195. case KSAUDIO_SPEAKER_7POINT1_SURROUND: return SPEAKERS_7POINT1_SURROUND;
  196. }
  197. return (enum speaker_layout)channels;
  198. }
  199. static bool audio_monitor_init(struct audio_monitor *monitor)
  200. {
  201. IMMDeviceEnumerator *immde = NULL;
  202. WAVEFORMATEX *wfex = NULL;
  203. bool success = false;
  204. UINT32 frames;
  205. HRESULT hr;
  206. const char *id = obs->audio.monitoring_device_id;
  207. if (!id) {
  208. return false;
  209. }
  210. pthread_mutex_init_value(&monitor->playback_mutex);
  211. /* ------------------------------------------ *
  212. * Init device */
  213. hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL,
  214. &IID_IMMDeviceEnumerator, (void**)&immde);
  215. if (FAILED(hr)) {
  216. return false;
  217. }
  218. if (strcmp(id, "default") == 0) {
  219. hr = immde->lpVtbl->GetDefaultAudioEndpoint(immde,
  220. eRender, eConsole, &monitor->device);
  221. } else {
  222. wchar_t w_id[512];
  223. os_utf8_to_wcs(id, 0, w_id, 512);
  224. hr = immde->lpVtbl->GetDevice(immde, w_id, &monitor->device);
  225. }
  226. if (FAILED(hr)) {
  227. goto fail;
  228. }
  229. /* ------------------------------------------ *
  230. * Init client */
  231. hr = monitor->device->lpVtbl->Activate(monitor->device,
  232. &IID_IAudioClient, CLSCTX_ALL, NULL,
  233. (void**)&monitor->client);
  234. if (FAILED(hr)) {
  235. goto fail;
  236. }
  237. hr = monitor->client->lpVtbl->GetMixFormat(monitor->client, &wfex);
  238. if (FAILED(hr)) {
  239. goto fail;
  240. }
  241. hr = monitor->client->lpVtbl->Initialize(monitor->client,
  242. AUDCLNT_SHAREMODE_SHARED, 0,
  243. 10000000, 0, wfex, NULL);
  244. if (FAILED(hr)) {
  245. goto fail;
  246. }
  247. /* ------------------------------------------ *
  248. * Init resampler */
  249. const struct audio_output_info *info = audio_output_get_info(
  250. obs->audio.audio);
  251. WAVEFORMATEXTENSIBLE *ext = (WAVEFORMATEXTENSIBLE*)wfex;
  252. struct resample_info from;
  253. struct resample_info to;
  254. from.samples_per_sec = info->samples_per_sec;
  255. from.speakers = info->speakers;
  256. from.format = AUDIO_FORMAT_FLOAT_PLANAR;
  257. to.samples_per_sec = (uint32_t)wfex->nSamplesPerSec;
  258. to.speakers = convert_speaker_layout(ext->dwChannelMask,
  259. wfex->nChannels);
  260. to.format = AUDIO_FORMAT_FLOAT;
  261. monitor->sample_rate = (uint32_t)wfex->nSamplesPerSec;
  262. monitor->channels = wfex->nChannels;
  263. monitor->resampler = audio_resampler_create(&to, &from);
  264. if (!monitor->resampler) {
  265. goto fail;
  266. }
  267. /* ------------------------------------------ *
  268. * Init client */
  269. hr = monitor->client->lpVtbl->GetBufferSize(monitor->client, &frames);
  270. if (FAILED(hr)) {
  271. goto fail;
  272. }
  273. hr = monitor->client->lpVtbl->GetService(monitor->client,
  274. &IID_IAudioRenderClient, (void**)&monitor->render);
  275. if (FAILED(hr)) {
  276. goto fail;
  277. }
  278. if (pthread_mutex_init(&monitor->playback_mutex, NULL) != 0) {
  279. goto fail;
  280. }
  281. hr = monitor->client->lpVtbl->Start(monitor->client);
  282. if (FAILED(hr)) {
  283. goto fail;
  284. }
  285. success = true;
  286. fail:
  287. safe_release(immde);
  288. if (wfex)
  289. CoTaskMemFree(wfex);
  290. return success;
  291. }
  292. static void audio_monitor_init_final(struct audio_monitor *monitor,
  293. obs_source_t *source)
  294. {
  295. monitor->source = source;
  296. monitor->source_has_video =
  297. (source->info.output_flags & OBS_SOURCE_VIDEO) != 0;
  298. obs_source_add_audio_capture_callback(source, on_audio_playback,
  299. monitor);
  300. }
  301. struct audio_monitor *audio_monitor_create(obs_source_t *source)
  302. {
  303. struct audio_monitor monitor = {0};
  304. struct audio_monitor *out;
  305. if (!audio_monitor_init(&monitor)) {
  306. goto fail;
  307. }
  308. out = bmemdup(&monitor, sizeof(monitor));
  309. pthread_mutex_lock(&obs->audio.monitoring_mutex);
  310. da_push_back(obs->audio.monitors, &out);
  311. pthread_mutex_unlock(&obs->audio.monitoring_mutex);
  312. audio_monitor_init_final(out, source);
  313. return out;
  314. fail:
  315. audio_monitor_free(&monitor);
  316. return NULL;
  317. }
  318. void audio_monitor_reset(struct audio_monitor *monitor)
  319. {
  320. struct audio_monitor new_monitor = {0};
  321. bool success;
  322. pthread_mutex_lock(&monitor->playback_mutex);
  323. success = audio_monitor_init(&new_monitor);
  324. pthread_mutex_unlock(&monitor->playback_mutex);
  325. if (success) {
  326. obs_source_t *source = monitor->source;
  327. audio_monitor_free(monitor);
  328. *monitor = new_monitor;
  329. audio_monitor_init_final(monitor, source);
  330. } else {
  331. audio_monitor_free(&new_monitor);
  332. }
  333. }
  334. void audio_monitor_destroy(struct audio_monitor *monitor)
  335. {
  336. if (monitor) {
  337. audio_monitor_free(monitor);
  338. pthread_mutex_lock(&obs->audio.monitoring_mutex);
  339. da_erase_item(obs->audio.monitors, &monitor);
  340. pthread_mutex_unlock(&obs->audio.monitoring_mutex);
  341. bfree(monitor);
  342. }
  343. }