1
0

obs-audio.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <inttypes.h>
  15. #include "obs-internal.h"
  16. #include "util/util_uint64.h"
  17. struct ts_info {
  18. uint64_t start;
  19. uint64_t end;
  20. };
  21. #define DEBUG_AUDIO 0
  22. #define DEBUG_LAGGED_AUDIO 0
  23. static void push_audio_tree(obs_source_t *parent, obs_source_t *source, void *p)
  24. {
  25. struct obs_core_audio *audio = p;
  26. if (da_find(audio->render_order, &source, 0) == DARRAY_INVALID) {
  27. obs_source_t *s = obs_source_get_ref(source);
  28. if (s) {
  29. da_push_back(audio->render_order, &s);
  30. s->audio_is_duplicated = false;
  31. }
  32. }
  33. UNUSED_PARAMETER(parent);
  34. }
  35. static inline bool is_individual_audio_source(obs_source_t *source)
  36. {
  37. return source->info.type == OBS_SOURCE_TYPE_INPUT && (source->info.output_flags & OBS_SOURCE_AUDIO) &&
  38. !(source->info.output_flags & OBS_SOURCE_COMPOSITE);
  39. }
  40. static inline void check_audio_output_source_is_monitoring_device(obs_source_t *s, void *p)
  41. {
  42. struct obs_core_audio *audio = p;
  43. if (!audio->monitoring_device_name)
  44. return;
  45. if (strcmp(s->info.id, "wasapi_output_capture") == 0 || strcmp(s->info.id, "pulse_output_capture") == 0 ||
  46. strcmp(s->info.id, "coreaudio_output_capture") == 0) {
  47. const char *dev_id = NULL;
  48. obs_data_t *settings = obs_source_get_settings(s);
  49. if (settings) {
  50. dev_id = obs_data_get_string(settings, "device_id");
  51. if (strcmp(dev_id, audio->monitoring_device_id) == 0) {
  52. audio->prevent_monitoring_duplication = true;
  53. audio->monitoring_duplicating_source = s;
  54. if (!audio->monitoring_duplication_prevented_on_prev_tick)
  55. blog(LOG_INFO,
  56. "Device for 'Audio Output Capture' source is also used for audio"
  57. " monitoring:\nDeduplication logic is being applied to all monitored"
  58. " sources.\n");
  59. }
  60. obs_data_release(settings);
  61. }
  62. }
  63. }
  64. /*
  65. * This version of push_audio_tree checks whether any source is an Audio Output Capture source ('Desktop Audio',
  66. * 'wasapi_output_capture' on Windows, 'pulse_output_capture' on Linux, 'coreaudio_output_capture' on macOS), & if the
  67. * corresponding device is the monitoring device. It then sets the core audio bool 'prevent_monitoring_duplication' to
  68. * true, which will silence all monitored sources (unless the Audio Output Capture source is muted).
  69. * Moreover, it has the purpose of detecting sources which appear several times in the audio tree. They are then tagged
  70. * as such to avoid their mixing in scenes and transitions and mixed directly as root_nodes.
  71. */
  72. static void push_audio_tree2(obs_source_t *parent, obs_source_t *source, void *p)
  73. {
  74. struct obs_core_audio *audio = p;
  75. size_t idx = da_find(audio->render_order, &source, 0);
  76. if (idx == DARRAY_INVALID) {
  77. /* First time we see this source → add to render order */
  78. obs_source_t *s = obs_source_get_ref(source);
  79. if (s) {
  80. da_push_back(audio->render_order, &s);
  81. s->audio_is_duplicated = false;
  82. check_audio_output_source_is_monitoring_device(s, audio);
  83. }
  84. } else {
  85. /* Source already present in tree → mark as duplicated if applicable */
  86. obs_source_t *s = audio->render_order.array[idx];
  87. if (is_individual_audio_source(s) && !s->audio_is_duplicated) {
  88. da_push_back(audio->root_nodes, &source);
  89. s->audio_is_duplicated = true;
  90. }
  91. }
  92. UNUSED_PARAMETER(parent);
  93. }
  94. static inline size_t convert_time_to_frames(size_t sample_rate, uint64_t t)
  95. {
  96. return (size_t)util_mul_div64(t, sample_rate, 1000000000ULL);
  97. }
  98. static inline void mix_audio(struct audio_output_data *mixes, obs_source_t *source, size_t channels, size_t sample_rate,
  99. struct ts_info *ts)
  100. {
  101. size_t total_floats = AUDIO_OUTPUT_FRAMES;
  102. size_t start_point = 0;
  103. if (source->audio_ts < ts->start || ts->end <= source->audio_ts)
  104. return;
  105. if (source->audio_ts != ts->start) {
  106. start_point = convert_time_to_frames(sample_rate, source->audio_ts - ts->start);
  107. if (start_point == AUDIO_OUTPUT_FRAMES)
  108. return;
  109. total_floats -= start_point;
  110. }
  111. for (size_t mix_idx = 0; mix_idx < MAX_AUDIO_MIXES; mix_idx++) {
  112. for (size_t ch = 0; ch < channels; ch++) {
  113. register float *mix = mixes[mix_idx].data[ch];
  114. register float *aud = source->audio_output_buf[mix_idx][ch];
  115. register float *end;
  116. mix += start_point;
  117. end = aud + total_floats;
  118. while (aud < end)
  119. *(mix++) += *(aud++);
  120. }
  121. }
  122. }
  123. static bool ignore_audio(obs_source_t *source, size_t channels, size_t sample_rate, uint64_t start_ts)
  124. {
  125. size_t num_floats = source->audio_input_buf[0].size / sizeof(float);
  126. const char *name = obs_source_get_name(source);
  127. if (!source->audio_ts && num_floats) {
  128. #if DEBUG_LAGGED_AUDIO == 1
  129. blog(LOG_DEBUG, "[src: %s] no timestamp, but audio available?", name);
  130. #endif
  131. for (size_t ch = 0; ch < channels; ch++)
  132. deque_pop_front(&source->audio_input_buf[ch], NULL, source->audio_input_buf[0].size);
  133. source->last_audio_input_buf_size = 0;
  134. return false;
  135. }
  136. if (num_floats) {
  137. /* round up the number of samples to drop */
  138. size_t drop = (size_t)util_mul_div64(start_ts - source->audio_ts - 1, sample_rate, 1000000000ULL) + 1;
  139. if (drop > num_floats)
  140. drop = num_floats;
  141. #if DEBUG_LAGGED_AUDIO == 1
  142. blog(LOG_DEBUG, "[src: %s] ignored %" PRIu64 "/%" PRIu64 " samples", name, (uint64_t)drop,
  143. (uint64_t)num_floats);
  144. #endif
  145. for (size_t ch = 0; ch < channels; ch++)
  146. deque_pop_front(&source->audio_input_buf[ch], NULL, drop * sizeof(float));
  147. source->last_audio_input_buf_size = 0;
  148. source->audio_ts += util_mul_div64(drop, 1000000000ULL, sample_rate);
  149. blog(LOG_DEBUG, "[src: %s] ts lag after ignoring: %" PRIu64, name, start_ts - source->audio_ts);
  150. /* rounding error, adjust */
  151. if (source->audio_ts == (start_ts - 1))
  152. source->audio_ts = start_ts;
  153. /* source is back in sync */
  154. if (source->audio_ts >= start_ts)
  155. return true;
  156. } else {
  157. #if DEBUG_LAGGED_AUDIO == 1
  158. blog(LOG_DEBUG, "[src: %s] no samples to ignore! ts = %" PRIu64, name, source->audio_ts);
  159. #endif
  160. }
  161. if (!source->audio_pending || num_floats) {
  162. blog(LOG_WARNING,
  163. "Source %s audio is lagging (over by %.02f ms) "
  164. "at max audio buffering. Restarting source audio.",
  165. name, (start_ts - source->audio_ts) / 1000000.);
  166. }
  167. source->audio_pending = true;
  168. source->audio_ts = 0;
  169. /* tell the timestamp adjustment code in source_output_audio_data to
  170. * reset everything, and hopefully fix the timestamps */
  171. source->timing_set = false;
  172. return false;
  173. }
  174. static bool discard_if_stopped(obs_source_t *source, size_t channels)
  175. {
  176. size_t last_size;
  177. size_t size;
  178. last_size = source->last_audio_input_buf_size;
  179. size = source->audio_input_buf[0].size;
  180. if (!size)
  181. return false;
  182. /* if perpetually pending data, it means the audio has stopped,
  183. * so clear the audio data */
  184. if (last_size == size) {
  185. if (!source->pending_stop) {
  186. source->pending_stop = true;
  187. #if DEBUG_AUDIO == 1
  188. blog(LOG_DEBUG, "doing pending stop trick: '%s'", source->context.name);
  189. #endif
  190. return false;
  191. }
  192. for (size_t ch = 0; ch < channels; ch++)
  193. deque_pop_front(&source->audio_input_buf[ch], NULL, source->audio_input_buf[ch].size);
  194. source->pending_stop = false;
  195. source->audio_ts = 0;
  196. source->last_audio_input_buf_size = 0;
  197. #if DEBUG_AUDIO == 1
  198. blog(LOG_DEBUG, "source audio data appears to have "
  199. "stopped, clearing");
  200. #endif
  201. return true;
  202. } else {
  203. source->last_audio_input_buf_size = size;
  204. return false;
  205. }
  206. }
  207. #define MAX_AUDIO_SIZE (AUDIO_OUTPUT_FRAMES * sizeof(float))
  208. static inline void discard_audio(struct obs_core_audio *audio, obs_source_t *source, size_t channels,
  209. size_t sample_rate, struct ts_info *ts)
  210. {
  211. size_t total_floats = AUDIO_OUTPUT_FRAMES;
  212. size_t size;
  213. /* debug assert only */
  214. UNUSED_PARAMETER(audio);
  215. #if DEBUG_AUDIO == 1
  216. bool is_audio_source = source->info.output_flags & OBS_SOURCE_AUDIO;
  217. #endif
  218. if (source->info.audio_render) {
  219. source->audio_ts = 0;
  220. return;
  221. }
  222. if (ts->end <= source->audio_ts) {
  223. #if DEBUG_AUDIO == 1
  224. blog(LOG_DEBUG,
  225. "can't discard, source "
  226. "timestamp (%" PRIu64 ") >= "
  227. "end timestamp (%" PRIu64 ")",
  228. source->audio_ts, ts->end);
  229. #endif
  230. return;
  231. }
  232. if (source->audio_ts < (ts->start - 1)) {
  233. if (source->audio_pending && source->audio_input_buf[0].size < MAX_AUDIO_SIZE &&
  234. discard_if_stopped(source, channels))
  235. return;
  236. #if DEBUG_AUDIO == 1
  237. if (is_audio_source) {
  238. blog(LOG_DEBUG,
  239. "can't discard, source "
  240. "timestamp (%" PRIu64 ") < "
  241. "start timestamp (%" PRIu64 ")",
  242. source->audio_ts, ts->start);
  243. }
  244. /* ignore_audio should have already run and marked this source
  245. * pending, unless we *just* added buffering */
  246. assert(audio->total_buffering_ticks < audio->max_buffering_ticks || source->audio_pending ||
  247. !source->audio_ts || audio->buffering_wait_ticks);
  248. #endif
  249. return;
  250. }
  251. if (source->audio_ts != ts->start && source->audio_ts != (ts->start - 1)) {
  252. size_t start_point = convert_time_to_frames(sample_rate, source->audio_ts - ts->start);
  253. if (start_point == AUDIO_OUTPUT_FRAMES) {
  254. #if DEBUG_AUDIO == 1
  255. if (is_audio_source)
  256. blog(LOG_DEBUG, "can't discard, start point is "
  257. "at audio frame count");
  258. #endif
  259. return;
  260. }
  261. total_floats -= start_point;
  262. }
  263. size = total_floats * sizeof(float);
  264. if (source->audio_input_buf[0].size < size) {
  265. if (discard_if_stopped(source, channels))
  266. return;
  267. #if DEBUG_AUDIO == 1
  268. if (is_audio_source)
  269. blog(LOG_DEBUG, "can't discard, data still pending");
  270. #endif
  271. source->audio_ts = ts->end;
  272. return;
  273. }
  274. for (size_t ch = 0; ch < channels; ch++)
  275. deque_pop_front(&source->audio_input_buf[ch], NULL, size);
  276. source->last_audio_input_buf_size = 0;
  277. #if DEBUG_AUDIO == 1
  278. if (is_audio_source)
  279. blog(LOG_DEBUG, "audio discarded, new ts: %" PRIu64, ts->end);
  280. #endif
  281. source->pending_stop = false;
  282. source->audio_ts = ts->end;
  283. }
  284. static inline bool audio_buffering_maxed(struct obs_core_audio *audio)
  285. {
  286. return audio->total_buffering_ticks == audio->max_buffering_ticks;
  287. }
  288. static void set_fixed_audio_buffering(struct obs_core_audio *audio, size_t sample_rate, struct ts_info *ts)
  289. {
  290. struct ts_info new_ts;
  291. size_t total_ms;
  292. int ticks;
  293. if (audio_buffering_maxed(audio))
  294. return;
  295. if (!audio->buffering_wait_ticks)
  296. audio->buffered_ts = ts->start;
  297. ticks = audio->max_buffering_ticks - audio->total_buffering_ticks;
  298. audio->total_buffering_ticks += ticks;
  299. total_ms = audio->total_buffering_ticks * AUDIO_OUTPUT_FRAMES * 1000 / sample_rate;
  300. blog(LOG_INFO,
  301. "Enabling fixed audio buffering, total "
  302. "audio buffering is now %d milliseconds",
  303. (int)total_ms);
  304. new_ts.start =
  305. audio->buffered_ts - audio_frames_to_ns(sample_rate, audio->buffering_wait_ticks * AUDIO_OUTPUT_FRAMES);
  306. while (ticks--) {
  307. const uint64_t cur_ticks = ++audio->buffering_wait_ticks;
  308. new_ts.end = new_ts.start;
  309. new_ts.start = audio->buffered_ts - audio_frames_to_ns(sample_rate, cur_ticks * AUDIO_OUTPUT_FRAMES);
  310. #if DEBUG_AUDIO == 1
  311. blog(LOG_DEBUG, "add buffered ts: %" PRIu64 "-%" PRIu64, new_ts.start, new_ts.end);
  312. #endif
  313. deque_push_front(&audio->buffered_timestamps, &new_ts, sizeof(new_ts));
  314. }
  315. *ts = new_ts;
  316. }
  317. static void add_audio_buffering(struct obs_core_audio *audio, size_t sample_rate, struct ts_info *ts, uint64_t min_ts,
  318. const char *buffering_name)
  319. {
  320. struct ts_info new_ts;
  321. uint64_t offset;
  322. uint64_t frames;
  323. size_t total_ms;
  324. size_t ms;
  325. int ticks;
  326. if (audio_buffering_maxed(audio))
  327. return;
  328. if (!audio->buffering_wait_ticks)
  329. audio->buffered_ts = ts->start;
  330. offset = ts->start - min_ts;
  331. frames = ns_to_audio_frames(sample_rate, offset);
  332. ticks = (int)((frames + AUDIO_OUTPUT_FRAMES - 1) / AUDIO_OUTPUT_FRAMES);
  333. audio->total_buffering_ticks += ticks;
  334. if (audio->total_buffering_ticks >= audio->max_buffering_ticks) {
  335. ticks -= audio->total_buffering_ticks - audio->max_buffering_ticks;
  336. audio->total_buffering_ticks = audio->max_buffering_ticks;
  337. blog(LOG_WARNING, "Max audio buffering reached!");
  338. }
  339. ms = ticks * AUDIO_OUTPUT_FRAMES * 1000 / sample_rate;
  340. total_ms = audio->total_buffering_ticks * AUDIO_OUTPUT_FRAMES * 1000 / sample_rate;
  341. blog(LOG_INFO,
  342. "adding %d milliseconds of audio buffering, total "
  343. "audio buffering is now %d milliseconds"
  344. " (source: %s)\n",
  345. (int)ms, (int)total_ms, buffering_name);
  346. #if DEBUG_AUDIO == 1
  347. blog(LOG_DEBUG,
  348. "min_ts (%" PRIu64 ") < start timestamp "
  349. "(%" PRIu64 ")",
  350. min_ts, ts->start);
  351. blog(LOG_DEBUG, "old buffered ts: %" PRIu64 "-%" PRIu64, ts->start, ts->end);
  352. #endif
  353. new_ts.start =
  354. audio->buffered_ts - audio_frames_to_ns(sample_rate, audio->buffering_wait_ticks * AUDIO_OUTPUT_FRAMES);
  355. while (ticks--) {
  356. const uint64_t cur_ticks = ++audio->buffering_wait_ticks;
  357. new_ts.end = new_ts.start;
  358. new_ts.start = audio->buffered_ts - audio_frames_to_ns(sample_rate, cur_ticks * AUDIO_OUTPUT_FRAMES);
  359. #if DEBUG_AUDIO == 1
  360. blog(LOG_DEBUG, "add buffered ts: %" PRIu64 "-%" PRIu64, new_ts.start, new_ts.end);
  361. #endif
  362. deque_push_front(&audio->buffered_timestamps, &new_ts, sizeof(new_ts));
  363. }
  364. *ts = new_ts;
  365. }
  366. static bool audio_buffer_insufficient(struct obs_source *source, size_t sample_rate, uint64_t min_ts)
  367. {
  368. size_t total_floats = AUDIO_OUTPUT_FRAMES;
  369. size_t size;
  370. if (source->info.audio_render || source->audio_pending || !source->audio_ts) {
  371. return false;
  372. }
  373. if (source->audio_ts != min_ts && source->audio_ts != (min_ts - 1)) {
  374. size_t start_point = convert_time_to_frames(sample_rate, source->audio_ts - min_ts);
  375. if (start_point >= AUDIO_OUTPUT_FRAMES)
  376. return false;
  377. total_floats -= start_point;
  378. }
  379. size = total_floats * sizeof(float);
  380. if (source->audio_input_buf[0].size < size) {
  381. source->audio_pending = true;
  382. return true;
  383. }
  384. return false;
  385. }
  386. static inline const char *find_min_ts(struct obs_core_data *data, uint64_t *min_ts)
  387. {
  388. obs_source_t *buffering_source = NULL;
  389. struct obs_source *source = data->first_audio_source;
  390. while (source) {
  391. if (!source->audio_pending && source->audio_ts && source->audio_ts < *min_ts) {
  392. *min_ts = source->audio_ts;
  393. buffering_source = source;
  394. }
  395. source = (struct obs_source *)source->next_audio_source;
  396. }
  397. return buffering_source ? obs_source_get_name(buffering_source) : NULL;
  398. }
  399. static inline bool mark_invalid_sources(struct obs_core_data *data, size_t sample_rate, uint64_t min_ts)
  400. {
  401. bool recalculate = false;
  402. struct obs_source *source = data->first_audio_source;
  403. while (source) {
  404. recalculate |= audio_buffer_insufficient(source, sample_rate, min_ts);
  405. source = (struct obs_source *)source->next_audio_source;
  406. }
  407. return recalculate;
  408. }
  409. static inline const char *calc_min_ts(struct obs_core_data *data, size_t sample_rate, uint64_t *min_ts)
  410. {
  411. const char *buffering_name = find_min_ts(data, min_ts);
  412. if (mark_invalid_sources(data, sample_rate, *min_ts))
  413. buffering_name = find_min_ts(data, min_ts);
  414. return buffering_name;
  415. }
  416. static inline void release_audio_sources(struct obs_core_audio *audio)
  417. {
  418. for (size_t i = 0; i < audio->render_order.num; i++)
  419. obs_source_release(audio->render_order.array[i]);
  420. }
  421. static inline void execute_audio_tasks(void)
  422. {
  423. struct obs_core_audio *audio = &obs->audio;
  424. bool tasks_remaining = true;
  425. while (tasks_remaining) {
  426. pthread_mutex_lock(&audio->task_mutex);
  427. if (audio->tasks.size) {
  428. struct obs_task_info info;
  429. deque_pop_front(&audio->tasks, &info, sizeof(info));
  430. info.task(info.param);
  431. }
  432. tasks_remaining = !!audio->tasks.size;
  433. pthread_mutex_unlock(&audio->task_mutex);
  434. }
  435. }
  436. /* In case monitoring and an 'Audio Output Capture' source have the same device, one silences all the monitored
  437. * sources unless the 'Audio Output Capture' is muted.
  438. * The syncing between the mute state of the 'Audio Output Capture' source set in UI and libobs is a bit tricky. There
  439. * is an intrinsic positive delay of the 'Audio Output Capture' source with respect to monitored sources due to the
  440. * audio OS processing (wasapi, pulseaudio or coreaudio). Unfortunately, the delay is machine dependent and we can only
  441. * mitigate its effects. During tests, src->user_muted worked better than src->muted, maybe because it is set earlier
  442. * than the muted flag which allows to keep a better sync. With src->muted, unmuting the 'Audio Output Capture' led to
  443. * a systematic level increase during one tick during testing for a sine tone (about +3 dBFS). In general we
  444. * don't expect much difference though between user_muted and muted.
  445. */
  446. static inline bool should_silence_monitored_source(obs_source_t *source, struct obs_core_audio *audio)
  447. {
  448. if (!audio->monitoring_duplicating_source)
  449. return false;
  450. bool output_capture_unmuted = !audio->monitoring_duplicating_source->user_muted;
  451. if (audio->prevent_monitoring_duplication && output_capture_unmuted) {
  452. if (source->monitoring_type == OBS_MONITORING_TYPE_MONITOR_AND_OUTPUT &&
  453. source != audio->monitoring_duplicating_source) {
  454. return true;
  455. }
  456. }
  457. return false;
  458. }
  459. static inline void clear_audio_output_buf(obs_source_t *source)
  460. {
  461. for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
  462. for (size_t ch = 0; ch < MAX_AUDIO_CHANNELS; ch++) {
  463. float *buf = source->audio_output_buf[mix][ch];
  464. if (buf)
  465. memset(buf, 0, AUDIO_OUTPUT_FRAMES * sizeof(float));
  466. }
  467. }
  468. }
  469. bool audio_callback(void *param, uint64_t start_ts_in, uint64_t end_ts_in, uint64_t *out_ts, uint32_t mixers,
  470. struct audio_output_data *mixes)
  471. {
  472. struct obs_core_data *data = &obs->data;
  473. struct obs_core_audio *audio = &obs->audio;
  474. struct obs_source *source;
  475. size_t sample_rate = audio_output_get_sample_rate(audio->audio);
  476. size_t channels = audio_output_get_channels(audio->audio);
  477. struct ts_info ts = {start_ts_in, end_ts_in};
  478. size_t audio_size;
  479. uint64_t min_ts;
  480. da_resize(audio->render_order, 0);
  481. da_resize(audio->root_nodes, 0);
  482. audio->monitoring_duplication_prevented_on_prev_tick = audio->prevent_monitoring_duplication;
  483. audio->prevent_monitoring_duplication = false;
  484. audio->monitoring_duplicating_source = NULL;
  485. deque_push_back(&audio->buffered_timestamps, &ts, sizeof(ts));
  486. deque_peek_front(&audio->buffered_timestamps, &ts, sizeof(ts));
  487. min_ts = ts.start;
  488. audio_size = AUDIO_OUTPUT_FRAMES * sizeof(float);
  489. #if DEBUG_AUDIO == 1
  490. blog(LOG_DEBUG, "ts %llu-%llu", ts.start, ts.end);
  491. #endif
  492. /* ------------------------------------------------ */
  493. /* build audio render order */
  494. pthread_mutex_lock(&obs->video.mixes_mutex);
  495. for (size_t j = 0; j < obs->video.mixes.num; j++) {
  496. struct obs_view *view = obs->video.mixes.array[j]->view;
  497. if (!view)
  498. continue;
  499. pthread_mutex_lock(&view->channels_mutex);
  500. /* NOTE: these are source channels, not audio channels */
  501. for (uint32_t i = 0; i < MAX_CHANNELS; i++) {
  502. obs_source_t *source = view->channels[i];
  503. if (!source)
  504. continue;
  505. if (!obs_source_active(source))
  506. continue;
  507. /* first, add top - level sources as root_nodes */
  508. if (obs->video.mixes.array[j]->mix_audio)
  509. da_push_back(audio->root_nodes, &source);
  510. /* Build audio tree, track Audio Output Capture sources and tag duplicate individual sources */
  511. obs_source_enum_active_tree(source, push_audio_tree2, audio);
  512. /* add top - level sources to audio tree */
  513. push_audio_tree(NULL, source, audio);
  514. /* Check whether the source is an 'Audio Output Capture' and coincides with monitoring device */
  515. check_audio_output_source_is_monitoring_device(source, audio);
  516. }
  517. pthread_mutex_unlock(&view->channels_mutex);
  518. }
  519. pthread_mutex_unlock(&obs->video.mixes_mutex);
  520. pthread_mutex_lock(&data->audio_sources_mutex);
  521. source = data->first_audio_source;
  522. while (source) {
  523. push_audio_tree(NULL, source, audio);
  524. source = (struct obs_source *)source->next_audio_source;
  525. }
  526. pthread_mutex_unlock(&data->audio_sources_mutex);
  527. /* ------------------------------------------------ */
  528. /* render audio data */
  529. for (size_t i = 0; i < audio->render_order.num; i++) {
  530. obs_source_t *source = audio->render_order.array[i];
  531. obs_source_audio_render(source, mixers, channels, sample_rate, audio_size);
  532. if (should_silence_monitored_source(source, audio))
  533. clear_audio_output_buf(source);
  534. /* if a source has gone backward in time and we can no
  535. * longer buffer, drop some or all of its audio */
  536. if (audio_buffering_maxed(audio) && source->audio_ts != 0 && source->audio_ts < ts.start) {
  537. if (source->info.audio_render) {
  538. blog(LOG_DEBUG,
  539. "render audio source %s timestamp has "
  540. "gone backwards",
  541. obs_source_get_name(source));
  542. /* just avoid further damage */
  543. source->audio_pending = true;
  544. #if DEBUG_AUDIO == 1
  545. /* this should really be fixed */
  546. assert(false);
  547. #endif
  548. } else {
  549. pthread_mutex_lock(&source->audio_buf_mutex);
  550. bool rerender = ignore_audio(source, channels, sample_rate, ts.start);
  551. pthread_mutex_unlock(&source->audio_buf_mutex);
  552. /* if we (potentially) recovered, re-render */
  553. if (rerender)
  554. obs_source_audio_render(source, mixers, channels, sample_rate, audio_size);
  555. }
  556. }
  557. }
  558. /* ------------------------------------------------ */
  559. /* get minimum audio timestamp */
  560. pthread_mutex_lock(&data->audio_sources_mutex);
  561. const char *buffering_name = calc_min_ts(data, sample_rate, &min_ts);
  562. pthread_mutex_unlock(&data->audio_sources_mutex);
  563. /* ------------------------------------------------ */
  564. /* if a source has gone backward in time, buffer */
  565. if (audio->fixed_buffer) {
  566. if (!audio_buffering_maxed(audio)) {
  567. set_fixed_audio_buffering(audio, sample_rate, &ts);
  568. }
  569. } else if (min_ts < ts.start) {
  570. add_audio_buffering(audio, sample_rate, &ts, min_ts, buffering_name);
  571. }
  572. /* ------------------------------------------------ */
  573. /* mix audio */
  574. if (!audio->buffering_wait_ticks) {
  575. for (size_t i = 0; i < audio->root_nodes.num; i++) {
  576. obs_source_t *source = audio->root_nodes.array[i];
  577. if (source->audio_pending)
  578. continue;
  579. pthread_mutex_lock(&source->audio_buf_mutex);
  580. if (source->audio_output_buf[0][0] && source->audio_ts)
  581. mix_audio(mixes, source, channels, sample_rate, &ts);
  582. pthread_mutex_unlock(&source->audio_buf_mutex);
  583. }
  584. }
  585. /* ------------------------------------------------ */
  586. /* discard audio */
  587. pthread_mutex_lock(&data->audio_sources_mutex);
  588. source = data->first_audio_source;
  589. while (source) {
  590. pthread_mutex_lock(&source->audio_buf_mutex);
  591. discard_audio(audio, source, channels, sample_rate, &ts);
  592. pthread_mutex_unlock(&source->audio_buf_mutex);
  593. source = (struct obs_source *)source->next_audio_source;
  594. }
  595. pthread_mutex_unlock(&data->audio_sources_mutex);
  596. /* ------------------------------------------------ */
  597. /* release audio sources */
  598. release_audio_sources(audio);
  599. deque_pop_front(&audio->buffered_timestamps, NULL, sizeof(ts));
  600. *out_ts = ts.start;
  601. if (audio->buffering_wait_ticks) {
  602. audio->buffering_wait_ticks--;
  603. return false;
  604. }
  605. execute_audio_tasks();
  606. UNUSED_PARAMETER(param);
  607. return true;
  608. }