obs-audio.c 23 KB

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