wasapi-output.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. 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,
  58. *frames * blocksize);
  59. if (!monitor->prev_video_ts) {
  60. monitor->prev_video_ts = last_frame_ts;
  61. } else if (monitor->prev_video_ts == last_frame_ts) {
  62. monitor->time_since_prev += (uint64_t)*frames *
  63. 1000000000ULL / (uint64_t)monitor->sample_rate;
  64. } else {
  65. monitor->time_since_prev = 0;
  66. }
  67. while (monitor->delay_buffer.size != 0) {
  68. size_t size;
  69. bool bad_diff;
  70. circlebuf_peek_front(&monitor->delay_buffer, &cur_ts,
  71. sizeof(ts));
  72. front_ts = cur_ts -
  73. ((uint64_t)pad * 1000000000ULL /
  74. (uint64_t)monitor->sample_rate);
  75. diff = (int64_t)front_ts - (int64_t)last_frame_ts;
  76. bad_diff = !last_frame_ts ||
  77. llabs(diff) > 5000000000 ||
  78. monitor->time_since_prev > 100000000ULL;
  79. /* delay audio if rushing */
  80. if (!bad_diff && diff > 75000000) {
  81. #ifdef DEBUG_AUDIO
  82. blog(LOG_INFO, "audio rushing, cutting audio, "
  83. "diff: %lld, delay buffer size: %lu, "
  84. "v: %llu: a: %llu",
  85. diff, (int)monitor->delay_buffer.size,
  86. last_frame_ts, front_ts);
  87. #endif
  88. return false;
  89. }
  90. circlebuf_pop_front(&monitor->delay_buffer, NULL, sizeof(ts));
  91. circlebuf_pop_front(&monitor->delay_buffer, frames,
  92. sizeof(*frames));
  93. size = *frames * blocksize;
  94. da_resize(monitor->buf, size);
  95. circlebuf_pop_front(&monitor->delay_buffer,
  96. monitor->buf.array, size);
  97. /* cut audio if dragging */
  98. if (!bad_diff && diff < -75000000 && monitor->delay_buffer.size > 0) {
  99. #ifdef DEBUG_AUDIO
  100. blog(LOG_INFO, "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 on_audio_playback(void *param, obs_source_t *source,
  114. const struct audio_data *audio_data, bool muted)
  115. {
  116. struct audio_monitor *monitor = param;
  117. IAudioRenderClient *render = monitor->render;
  118. uint8_t *resample_data[MAX_AV_PLANES];
  119. float vol = source->user_volume;
  120. uint32_t resample_frames;
  121. uint64_t ts_offset;
  122. bool success;
  123. BYTE *output;
  124. if (pthread_mutex_trylock(&monitor->playback_mutex) != 0) {
  125. return;
  126. }
  127. if (os_atomic_load_long(&source->activate_refs) == 0) {
  128. goto unlock;
  129. }
  130. success = audio_resampler_resample(monitor->resampler, resample_data,
  131. &resample_frames, &ts_offset,
  132. (const uint8_t *const *)audio_data->data,
  133. (uint32_t)audio_data->frames);
  134. if (!success) {
  135. goto unlock;
  136. }
  137. UINT32 pad = 0;
  138. monitor->client->lpVtbl->GetCurrentPadding(monitor->client, &pad);
  139. if (monitor->source_has_video) {
  140. uint64_t ts = audio_data->timestamp - ts_offset;
  141. if (!process_audio_delay(monitor, (float**)(&resample_data[0]),
  142. &resample_frames, ts, pad)) {
  143. goto unlock;
  144. }
  145. }
  146. HRESULT hr = render->lpVtbl->GetBuffer(render, resample_frames,
  147. &output);
  148. if (FAILED(hr)) {
  149. goto unlock;
  150. }
  151. if (!muted) {
  152. /* apply volume */
  153. if (!close_float(vol, 1.0f, EPSILON)) {
  154. register float *cur = (float*)resample_data[0];
  155. register float *end = cur +
  156. resample_frames * monitor->channels;
  157. while (cur < end)
  158. *(cur++) *= vol;
  159. }
  160. memcpy(output, resample_data[0],
  161. resample_frames * monitor->channels *
  162. sizeof(float));
  163. }
  164. render->lpVtbl->ReleaseBuffer(render, resample_frames,
  165. muted ? AUDCLNT_BUFFERFLAGS_SILENT : 0);
  166. unlock:
  167. pthread_mutex_unlock(&monitor->playback_mutex);
  168. }
  169. static inline void audio_monitor_free(struct audio_monitor *monitor)
  170. {
  171. if (monitor->source) {
  172. obs_source_remove_audio_capture_callback(
  173. monitor->source, on_audio_playback, monitor);
  174. }
  175. if (monitor->client)
  176. monitor->client->lpVtbl->Stop(monitor->client);
  177. safe_release(monitor->device);
  178. safe_release(monitor->client);
  179. safe_release(monitor->render);
  180. audio_resampler_destroy(monitor->resampler);
  181. circlebuf_free(&monitor->delay_buffer);
  182. da_free(monitor->buf);
  183. }
  184. static enum speaker_layout convert_speaker_layout(DWORD layout, WORD channels)
  185. {
  186. switch (layout) {
  187. case KSAUDIO_SPEAKER_QUAD: return SPEAKERS_QUAD;
  188. case KSAUDIO_SPEAKER_2POINT1: return SPEAKERS_2POINT1;
  189. case KSAUDIO_SPEAKER_4POINT1: return SPEAKERS_4POINT1;
  190. case KSAUDIO_SPEAKER_SURROUND: return SPEAKERS_SURROUND;
  191. case KSAUDIO_SPEAKER_5POINT1: return SPEAKERS_5POINT1;
  192. case KSAUDIO_SPEAKER_5POINT1_SURROUND: return SPEAKERS_5POINT1_SURROUND;
  193. case KSAUDIO_SPEAKER_7POINT1: return SPEAKERS_7POINT1;
  194. case KSAUDIO_SPEAKER_7POINT1_SURROUND: return SPEAKERS_7POINT1_SURROUND;
  195. }
  196. return (enum speaker_layout)channels;
  197. }
  198. static bool audio_monitor_init(struct audio_monitor *monitor)
  199. {
  200. IMMDeviceEnumerator *immde = NULL;
  201. WAVEFORMATEX *wfex = NULL;
  202. bool success = false;
  203. UINT32 frames;
  204. HRESULT hr;
  205. const char *id = obs->audio.monitoring_device_id;
  206. if (!id) {
  207. return false;
  208. }
  209. pthread_mutex_init_value(&monitor->playback_mutex);
  210. /* ------------------------------------------ *
  211. * Init device */
  212. hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL,
  213. &IID_IMMDeviceEnumerator, (void**)&immde);
  214. if (FAILED(hr)) {
  215. return false;
  216. }
  217. if (strcmp(id, "default") == 0) {
  218. hr = immde->lpVtbl->GetDefaultAudioEndpoint(immde,
  219. eRender, eConsole, &monitor->device);
  220. } else {
  221. wchar_t w_id[512];
  222. os_utf8_to_wcs(id, 0, w_id, 512);
  223. hr = immde->lpVtbl->GetDevice(immde, w_id, &monitor->device);
  224. }
  225. if (FAILED(hr)) {
  226. goto fail;
  227. }
  228. /* ------------------------------------------ *
  229. * Init client */
  230. hr = monitor->device->lpVtbl->Activate(monitor->device,
  231. &IID_IAudioClient, CLSCTX_ALL, NULL,
  232. (void**)&monitor->client);
  233. if (FAILED(hr)) {
  234. goto fail;
  235. }
  236. hr = monitor->client->lpVtbl->GetMixFormat(monitor->client, &wfex);
  237. if (FAILED(hr)) {
  238. goto fail;
  239. }
  240. hr = monitor->client->lpVtbl->Initialize(monitor->client,
  241. AUDCLNT_SHAREMODE_SHARED, 0,
  242. 10000000, 0, wfex, NULL);
  243. if (FAILED(hr)) {
  244. goto fail;
  245. }
  246. /* ------------------------------------------ *
  247. * Init resampler */
  248. const struct audio_output_info *info = audio_output_get_info(
  249. obs->audio.audio);
  250. WAVEFORMATEXTENSIBLE *ext = (WAVEFORMATEXTENSIBLE*)wfex;
  251. struct resample_info from;
  252. struct resample_info to;
  253. from.samples_per_sec = info->samples_per_sec;
  254. from.speakers = info->speakers;
  255. from.format = AUDIO_FORMAT_FLOAT_PLANAR;
  256. to.samples_per_sec = (uint32_t)wfex->nSamplesPerSec;
  257. to.speakers = convert_speaker_layout(ext->dwChannelMask,
  258. wfex->nChannels);
  259. to.format = AUDIO_FORMAT_FLOAT;
  260. monitor->sample_rate = (uint32_t)wfex->nSamplesPerSec;
  261. monitor->channels = wfex->nChannels;
  262. monitor->resampler = audio_resampler_create(&to, &from);
  263. if (!monitor->resampler) {
  264. goto fail;
  265. }
  266. /* ------------------------------------------ *
  267. * Init client */
  268. hr = monitor->client->lpVtbl->GetBufferSize(monitor->client, &frames);
  269. if (FAILED(hr)) {
  270. goto fail;
  271. }
  272. hr = monitor->client->lpVtbl->GetService(monitor->client,
  273. &IID_IAudioRenderClient, (void**)&monitor->render);
  274. if (FAILED(hr)) {
  275. goto fail;
  276. }
  277. if (pthread_mutex_init(&monitor->playback_mutex, NULL) != 0) {
  278. goto fail;
  279. }
  280. hr = monitor->client->lpVtbl->Start(monitor->client);
  281. if (FAILED(hr)) {
  282. goto fail;
  283. }
  284. success = true;
  285. fail:
  286. safe_release(immde);
  287. if (wfex)
  288. CoTaskMemFree(wfex);
  289. return success;
  290. }
  291. static void audio_monitor_init_final(struct audio_monitor *monitor,
  292. obs_source_t *source)
  293. {
  294. monitor->source = source;
  295. monitor->source_has_video =
  296. (source->info.output_flags & OBS_SOURCE_VIDEO) != 0;
  297. obs_source_add_audio_capture_callback(source, on_audio_playback,
  298. monitor);
  299. }
  300. struct audio_monitor *audio_monitor_create(obs_source_t *source)
  301. {
  302. struct audio_monitor monitor = {0};
  303. struct audio_monitor *out;
  304. if (!audio_monitor_init(&monitor)) {
  305. goto fail;
  306. }
  307. out = bmemdup(&monitor, sizeof(monitor));
  308. pthread_mutex_lock(&obs->audio.monitoring_mutex);
  309. da_push_back(obs->audio.monitors, &out);
  310. pthread_mutex_unlock(&obs->audio.monitoring_mutex);
  311. audio_monitor_init_final(out, source);
  312. return out;
  313. fail:
  314. audio_monitor_free(&monitor);
  315. return NULL;
  316. }
  317. void audio_monitor_reset(struct audio_monitor *monitor)
  318. {
  319. struct audio_monitor new_monitor = {0};
  320. bool success;
  321. pthread_mutex_lock(&monitor->playback_mutex);
  322. success = audio_monitor_init(&new_monitor);
  323. pthread_mutex_unlock(&monitor->playback_mutex);
  324. if (success) {
  325. obs_source_t *source = monitor->source;
  326. audio_monitor_free(monitor);
  327. *monitor = new_monitor;
  328. audio_monitor_init_final(monitor, source);
  329. } else {
  330. audio_monitor_free(&new_monitor);
  331. }
  332. }
  333. void audio_monitor_destroy(struct audio_monitor *monitor)
  334. {
  335. if (monitor) {
  336. audio_monitor_free(monitor);
  337. pthread_mutex_lock(&obs->audio.monitoring_mutex);
  338. da_erase_item(obs->audio.monitors, &monitor);
  339. pthread_mutex_unlock(&obs->audio.monitoring_mutex);
  340. bfree(monitor);
  341. }
  342. }