obs-audio.c 17 KB

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