obs-audio.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /******************************************************************************
  2. Copyright (C) 2015 by Hugh 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. #define MAX_BUFFERING_TICKS 45
  24. static void push_audio_tree(obs_source_t *parent, obs_source_t *source, void *p)
  25. {
  26. struct obs_core_audio *audio = p;
  27. if (da_find(audio->render_order, &source, 0) == DARRAY_INVALID) {
  28. obs_source_t *s = obs_source_get_ref(source);
  29. if (s)
  30. da_push_back(audio->render_order, &s);
  31. }
  32. UNUSED_PARAMETER(parent);
  33. }
  34. static inline size_t convert_time_to_frames(size_t sample_rate, uint64_t t)
  35. {
  36. return (size_t)util_mul_div64(t, sample_rate, 1000000000ULL);
  37. }
  38. static inline void mix_audio(struct audio_output_data *mixes,
  39. obs_source_t *source, size_t channels,
  40. size_t sample_rate, struct ts_info *ts)
  41. {
  42. size_t total_floats = AUDIO_OUTPUT_FRAMES;
  43. size_t start_point = 0;
  44. if (source->audio_ts < ts->start || ts->end <= source->audio_ts)
  45. return;
  46. if (source->audio_ts != ts->start) {
  47. start_point = convert_time_to_frames(
  48. sample_rate, source->audio_ts - ts->start);
  49. if (start_point == AUDIO_OUTPUT_FRAMES)
  50. return;
  51. total_floats -= start_point;
  52. }
  53. for (size_t mix_idx = 0; mix_idx < MAX_AUDIO_MIXES; mix_idx++) {
  54. for (size_t ch = 0; ch < channels; ch++) {
  55. register float *mix = mixes[mix_idx].data[ch];
  56. register float *aud =
  57. source->audio_output_buf[mix_idx][ch];
  58. register float *end;
  59. mix += start_point;
  60. end = aud + total_floats;
  61. while (aud < end)
  62. *(mix++) += *(aud++);
  63. }
  64. }
  65. }
  66. static bool ignore_audio(obs_source_t *source, size_t channels,
  67. size_t sample_rate, uint64_t start_ts)
  68. {
  69. size_t num_floats = source->audio_input_buf[0].size / sizeof(float);
  70. const char *name = obs_source_get_name(source);
  71. if (!source->audio_ts && num_floats) {
  72. #if DEBUG_LAGGED_AUDIO == 1
  73. blog(LOG_DEBUG, "[src: %s] no timestamp, but audio available?",
  74. name);
  75. #endif
  76. for (size_t ch = 0; ch < channels; ch++)
  77. circlebuf_pop_front(&source->audio_input_buf[ch], NULL,
  78. source->audio_input_buf[0].size);
  79. source->last_audio_input_buf_size = 0;
  80. return false;
  81. }
  82. if (num_floats) {
  83. /* round up the number of samples to drop */
  84. size_t drop = util_mul_div64(start_ts - source->audio_ts - 1,
  85. sample_rate, 1000000000ULL) +
  86. 1;
  87. if (drop > num_floats)
  88. drop = num_floats;
  89. #if DEBUG_LAGGED_AUDIO == 1
  90. blog(LOG_DEBUG,
  91. "[src: %s] ignored %" PRIu64 "/%" PRIu64 " samples", name,
  92. (uint64_t)drop, (uint64_t)num_floats);
  93. #endif
  94. for (size_t ch = 0; ch < channels; ch++)
  95. circlebuf_pop_front(&source->audio_input_buf[ch], NULL,
  96. drop * sizeof(float));
  97. source->last_audio_input_buf_size = 0;
  98. source->audio_ts +=
  99. util_mul_div64(drop, 1000000000ULL, sample_rate);
  100. blog(LOG_DEBUG, "[src: %s] ts lag after ignoring: %" PRIu64,
  101. name, start_ts - source->audio_ts);
  102. /* rounding error, adjust */
  103. if (source->audio_ts == (start_ts - 1))
  104. source->audio_ts = start_ts;
  105. /* source is back in sync */
  106. if (source->audio_ts >= start_ts)
  107. return true;
  108. } else {
  109. #if DEBUG_LAGGED_AUDIO == 1
  110. blog(LOG_DEBUG, "[src: %s] no samples to ignore! ts = %" PRIu64,
  111. name, source->audio_ts);
  112. #endif
  113. }
  114. if (!source->audio_pending || num_floats) {
  115. blog(LOG_WARNING,
  116. "Source %s audio is lagging (over by %.02f ms) "
  117. "at max audio buffering. Restarting source audio.",
  118. name, (start_ts - source->audio_ts) / 1000000.);
  119. }
  120. source->audio_pending = true;
  121. source->audio_ts = 0;
  122. /* tell the timestamp adjustment code in source_output_audio_data to
  123. * reset everything, and hopefully fix the timestamps */
  124. source->timing_set = false;
  125. return false;
  126. }
  127. static bool discard_if_stopped(obs_source_t *source, size_t channels)
  128. {
  129. size_t last_size;
  130. size_t size;
  131. last_size = source->last_audio_input_buf_size;
  132. size = source->audio_input_buf[0].size;
  133. if (!size)
  134. return false;
  135. /* if perpetually pending data, it means the audio has stopped,
  136. * so clear the audio data */
  137. if (last_size == size) {
  138. if (!source->pending_stop) {
  139. source->pending_stop = true;
  140. #if DEBUG_AUDIO == 1
  141. blog(LOG_DEBUG, "doing pending stop trick: '%s'",
  142. source->context.name);
  143. #endif
  144. return false;
  145. }
  146. for (size_t ch = 0; ch < channels; ch++)
  147. circlebuf_pop_front(&source->audio_input_buf[ch], NULL,
  148. source->audio_input_buf[ch].size);
  149. source->pending_stop = false;
  150. source->audio_ts = 0;
  151. source->last_audio_input_buf_size = 0;
  152. #if DEBUG_AUDIO == 1
  153. blog(LOG_DEBUG, "source audio data appears to have "
  154. "stopped, clearing");
  155. #endif
  156. return true;
  157. } else {
  158. source->last_audio_input_buf_size = size;
  159. return false;
  160. }
  161. }
  162. #define MAX_AUDIO_SIZE (AUDIO_OUTPUT_FRAMES * sizeof(float))
  163. static inline void discard_audio(struct obs_core_audio *audio,
  164. obs_source_t *source, size_t channels,
  165. size_t sample_rate, struct ts_info *ts)
  166. {
  167. size_t total_floats = AUDIO_OUTPUT_FRAMES;
  168. size_t size;
  169. /* debug assert only */
  170. UNUSED_PARAMETER(audio);
  171. #if DEBUG_AUDIO == 1
  172. bool is_audio_source = source->info.output_flags & OBS_SOURCE_AUDIO;
  173. #endif
  174. if (source->info.audio_render) {
  175. source->audio_ts = 0;
  176. return;
  177. }
  178. if (ts->end <= source->audio_ts) {
  179. #if DEBUG_AUDIO == 1
  180. blog(LOG_DEBUG,
  181. "can't discard, source "
  182. "timestamp (%" PRIu64 ") >= "
  183. "end timestamp (%" PRIu64 ")",
  184. source->audio_ts, ts->end);
  185. #endif
  186. return;
  187. }
  188. if (source->audio_ts < (ts->start - 1)) {
  189. if (source->audio_pending &&
  190. source->audio_input_buf[0].size < MAX_AUDIO_SIZE &&
  191. discard_if_stopped(source, channels))
  192. return;
  193. #if DEBUG_AUDIO == 1
  194. if (is_audio_source) {
  195. blog(LOG_DEBUG,
  196. "can't discard, source "
  197. "timestamp (%" PRIu64 ") < "
  198. "start timestamp (%" PRIu64 ")",
  199. source->audio_ts, ts->start);
  200. }
  201. /* ignore_audio should have already run and marked this source
  202. * pending, unless we *just* added buffering */
  203. assert(audio->total_buffering_ticks < MAX_BUFFERING_TICKS ||
  204. source->audio_pending || !source->audio_ts ||
  205. audio->buffering_wait_ticks);
  206. #endif
  207. return;
  208. }
  209. if (source->audio_ts != ts->start &&
  210. source->audio_ts != (ts->start - 1)) {
  211. size_t start_point = convert_time_to_frames(
  212. sample_rate, source->audio_ts - ts->start);
  213. if (start_point == AUDIO_OUTPUT_FRAMES) {
  214. #if DEBUG_AUDIO == 1
  215. if (is_audio_source)
  216. blog(LOG_DEBUG, "can't discard, start point is "
  217. "at audio frame count");
  218. #endif
  219. return;
  220. }
  221. total_floats -= start_point;
  222. }
  223. size = total_floats * sizeof(float);
  224. if (source->audio_input_buf[0].size < size) {
  225. if (discard_if_stopped(source, channels))
  226. return;
  227. #if DEBUG_AUDIO == 1
  228. if (is_audio_source)
  229. blog(LOG_DEBUG, "can't discard, data still pending");
  230. #endif
  231. source->audio_ts = ts->end;
  232. return;
  233. }
  234. for (size_t ch = 0; ch < channels; ch++)
  235. circlebuf_pop_front(&source->audio_input_buf[ch], NULL, size);
  236. source->last_audio_input_buf_size = 0;
  237. #if DEBUG_AUDIO == 1
  238. if (is_audio_source)
  239. blog(LOG_DEBUG, "audio discarded, new ts: %" PRIu64, ts->end);
  240. #endif
  241. source->pending_stop = false;
  242. source->audio_ts = ts->end;
  243. }
  244. static void add_audio_buffering(struct obs_core_audio *audio,
  245. size_t sample_rate, struct ts_info *ts,
  246. uint64_t min_ts, const char *buffering_name)
  247. {
  248. struct ts_info new_ts;
  249. uint64_t offset;
  250. uint64_t frames;
  251. size_t total_ms;
  252. size_t ms;
  253. int ticks;
  254. if (audio->total_buffering_ticks == MAX_BUFFERING_TICKS)
  255. return;
  256. if (!audio->buffering_wait_ticks)
  257. audio->buffered_ts = ts->start;
  258. offset = ts->start - min_ts;
  259. frames = ns_to_audio_frames(sample_rate, offset);
  260. ticks = (int)((frames + AUDIO_OUTPUT_FRAMES - 1) / AUDIO_OUTPUT_FRAMES);
  261. audio->total_buffering_ticks += ticks;
  262. if (audio->total_buffering_ticks >= MAX_BUFFERING_TICKS) {
  263. ticks -= audio->total_buffering_ticks - MAX_BUFFERING_TICKS;
  264. audio->total_buffering_ticks = MAX_BUFFERING_TICKS;
  265. blog(LOG_WARNING, "Max audio buffering reached!");
  266. }
  267. ms = ticks * AUDIO_OUTPUT_FRAMES * 1000 / sample_rate;
  268. total_ms = audio->total_buffering_ticks * AUDIO_OUTPUT_FRAMES * 1000 /
  269. sample_rate;
  270. blog(LOG_INFO,
  271. "adding %d milliseconds of audio buffering, total "
  272. "audio buffering is now %d milliseconds"
  273. " (source: %s)\n",
  274. (int)ms, (int)total_ms, buffering_name);
  275. #if DEBUG_AUDIO == 1
  276. blog(LOG_DEBUG,
  277. "min_ts (%" PRIu64 ") < start timestamp "
  278. "(%" PRIu64 ")",
  279. min_ts, ts->start);
  280. blog(LOG_DEBUG, "old buffered ts: %" PRIu64 "-%" PRIu64, ts->start,
  281. ts->end);
  282. #endif
  283. new_ts.start =
  284. audio->buffered_ts -
  285. audio_frames_to_ns(sample_rate, audio->buffering_wait_ticks *
  286. AUDIO_OUTPUT_FRAMES);
  287. while (ticks--) {
  288. int cur_ticks = ++audio->buffering_wait_ticks;
  289. new_ts.end = new_ts.start;
  290. new_ts.start =
  291. audio->buffered_ts -
  292. audio_frames_to_ns(sample_rate,
  293. cur_ticks * AUDIO_OUTPUT_FRAMES);
  294. #if DEBUG_AUDIO == 1
  295. blog(LOG_DEBUG, "add buffered ts: %" PRIu64 "-%" PRIu64,
  296. new_ts.start, new_ts.end);
  297. #endif
  298. circlebuf_push_front(&audio->buffered_timestamps, &new_ts,
  299. sizeof(new_ts));
  300. }
  301. *ts = new_ts;
  302. }
  303. static bool audio_buffer_insuffient(struct obs_source *source,
  304. size_t sample_rate, uint64_t min_ts)
  305. {
  306. size_t total_floats = AUDIO_OUTPUT_FRAMES;
  307. size_t size;
  308. if (source->info.audio_render || source->audio_pending ||
  309. !source->audio_ts) {
  310. return false;
  311. }
  312. if (source->audio_ts != min_ts && source->audio_ts != (min_ts - 1)) {
  313. size_t start_point = convert_time_to_frames(
  314. sample_rate, source->audio_ts - min_ts);
  315. if (start_point >= AUDIO_OUTPUT_FRAMES)
  316. return false;
  317. total_floats -= start_point;
  318. }
  319. size = total_floats * sizeof(float);
  320. if (source->audio_input_buf[0].size < size) {
  321. source->audio_pending = true;
  322. return true;
  323. }
  324. return false;
  325. }
  326. static inline const char *find_min_ts(struct obs_core_data *data,
  327. uint64_t *min_ts)
  328. {
  329. obs_source_t *buffering_source = NULL;
  330. struct obs_source *source = data->first_audio_source;
  331. while (source) {
  332. if (!source->audio_pending && source->audio_ts &&
  333. source->audio_ts < *min_ts) {
  334. *min_ts = source->audio_ts;
  335. buffering_source = source;
  336. }
  337. source = (struct obs_source *)source->next_audio_source;
  338. }
  339. return buffering_source ? obs_source_get_name(buffering_source) : NULL;
  340. }
  341. static inline bool mark_invalid_sources(struct obs_core_data *data,
  342. size_t sample_rate, uint64_t min_ts)
  343. {
  344. bool recalculate = false;
  345. struct obs_source *source = data->first_audio_source;
  346. while (source) {
  347. recalculate |=
  348. audio_buffer_insuffient(source, sample_rate, min_ts);
  349. source = (struct obs_source *)source->next_audio_source;
  350. }
  351. return recalculate;
  352. }
  353. static inline const char *calc_min_ts(struct obs_core_data *data,
  354. size_t sample_rate, uint64_t *min_ts)
  355. {
  356. const char *buffering_name = find_min_ts(data, min_ts);
  357. if (mark_invalid_sources(data, sample_rate, *min_ts))
  358. buffering_name = find_min_ts(data, min_ts);
  359. return buffering_name;
  360. }
  361. static inline void release_audio_sources(struct obs_core_audio *audio)
  362. {
  363. for (size_t i = 0; i < audio->render_order.num; i++)
  364. obs_source_release(audio->render_order.array[i]);
  365. }
  366. bool audio_callback(void *param, uint64_t start_ts_in, uint64_t end_ts_in,
  367. uint64_t *out_ts, uint32_t mixers,
  368. struct audio_output_data *mixes)
  369. {
  370. struct obs_core_data *data = &obs->data;
  371. struct obs_core_audio *audio = &obs->audio;
  372. struct obs_source *source;
  373. size_t sample_rate = audio_output_get_sample_rate(audio->audio);
  374. size_t channels = audio_output_get_channels(audio->audio);
  375. struct ts_info ts = {start_ts_in, end_ts_in};
  376. size_t audio_size;
  377. uint64_t min_ts;
  378. da_resize(audio->render_order, 0);
  379. da_resize(audio->root_nodes, 0);
  380. circlebuf_push_back(&audio->buffered_timestamps, &ts, sizeof(ts));
  381. circlebuf_peek_front(&audio->buffered_timestamps, &ts, sizeof(ts));
  382. min_ts = ts.start;
  383. audio_size = AUDIO_OUTPUT_FRAMES * sizeof(float);
  384. #if DEBUG_AUDIO == 1
  385. blog(LOG_DEBUG, "ts %llu-%llu", ts.start, ts.end);
  386. #endif
  387. /* ------------------------------------------------ */
  388. /* build audio render order
  389. * NOTE: these are source channels, not audio channels */
  390. for (uint32_t i = 0; i < MAX_CHANNELS; i++) {
  391. obs_source_t *source = obs_get_output_source(i);
  392. if (source) {
  393. obs_source_enum_active_tree(source, push_audio_tree,
  394. audio);
  395. push_audio_tree(NULL, source, audio);
  396. da_push_back(audio->root_nodes, &source);
  397. obs_source_release(source);
  398. }
  399. }
  400. pthread_mutex_lock(&data->audio_sources_mutex);
  401. source = data->first_audio_source;
  402. while (source) {
  403. push_audio_tree(NULL, source, audio);
  404. source = (struct obs_source *)source->next_audio_source;
  405. }
  406. pthread_mutex_unlock(&data->audio_sources_mutex);
  407. /* ------------------------------------------------ */
  408. /* render audio data */
  409. for (size_t i = 0; i < audio->render_order.num; i++) {
  410. obs_source_t *source = audio->render_order.array[i];
  411. obs_source_audio_render(source, mixers, channels, sample_rate,
  412. audio_size);
  413. /* if a source has gone backward in time and we can no
  414. * longer buffer, drop some or all of its audio */
  415. if (audio->total_buffering_ticks == MAX_BUFFERING_TICKS &&
  416. source->audio_ts < ts.start) {
  417. if (source->info.audio_render) {
  418. blog(LOG_DEBUG,
  419. "render audio source %s timestamp has "
  420. "gone backwards",
  421. obs_source_get_name(source));
  422. /* just avoid further damage */
  423. source->audio_pending = true;
  424. #if DEBUG_AUDIO == 1
  425. /* this should really be fixed */
  426. assert(false);
  427. #endif
  428. } else {
  429. pthread_mutex_lock(&source->audio_buf_mutex);
  430. bool rerender = ignore_audio(source, channels,
  431. sample_rate,
  432. ts.start);
  433. pthread_mutex_unlock(&source->audio_buf_mutex);
  434. /* if we (potentially) recovered, re-render */
  435. if (rerender)
  436. obs_source_audio_render(source, mixers,
  437. channels,
  438. sample_rate,
  439. audio_size);
  440. }
  441. }
  442. }
  443. /* ------------------------------------------------ */
  444. /* get minimum audio timestamp */
  445. pthread_mutex_lock(&data->audio_sources_mutex);
  446. const char *buffering_name = calc_min_ts(data, sample_rate, &min_ts);
  447. pthread_mutex_unlock(&data->audio_sources_mutex);
  448. /* ------------------------------------------------ */
  449. /* if a source has gone backward in time, buffer */
  450. if (min_ts < ts.start)
  451. add_audio_buffering(audio, sample_rate, &ts, min_ts,
  452. buffering_name);
  453. /* ------------------------------------------------ */
  454. /* mix audio */
  455. if (!audio->buffering_wait_ticks) {
  456. for (size_t i = 0; i < audio->root_nodes.num; i++) {
  457. obs_source_t *source = audio->root_nodes.array[i];
  458. if (source->audio_pending)
  459. continue;
  460. pthread_mutex_lock(&source->audio_buf_mutex);
  461. if (source->audio_output_buf[0][0] && source->audio_ts)
  462. mix_audio(mixes, source, channels, sample_rate,
  463. &ts);
  464. pthread_mutex_unlock(&source->audio_buf_mutex);
  465. }
  466. }
  467. /* ------------------------------------------------ */
  468. /* discard audio */
  469. pthread_mutex_lock(&data->audio_sources_mutex);
  470. source = data->first_audio_source;
  471. while (source) {
  472. pthread_mutex_lock(&source->audio_buf_mutex);
  473. discard_audio(audio, source, channels, sample_rate, &ts);
  474. pthread_mutex_unlock(&source->audio_buf_mutex);
  475. source = (struct obs_source *)source->next_audio_source;
  476. }
  477. pthread_mutex_unlock(&data->audio_sources_mutex);
  478. /* ------------------------------------------------ */
  479. /* release audio sources */
  480. release_audio_sources(audio);
  481. circlebuf_pop_front(&audio->buffered_timestamps, NULL, sizeof(ts));
  482. *out_ts = ts.start;
  483. if (audio->buffering_wait_ticks) {
  484. audio->buffering_wait_ticks--;
  485. return false;
  486. }
  487. UNUSED_PARAMETER(param);
  488. return true;
  489. }