obs-audio.c 22 KB

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