wasapi-output.c 12 KB

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