obs-audio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. struct ts_info {
  17. uint64_t start;
  18. uint64_t end;
  19. };
  20. #define DEBUG_AUDIO 0
  21. #define MAX_BUFFERING_TICKS 45
  22. static void push_audio_tree(obs_source_t *parent, obs_source_t *source, void *p)
  23. {
  24. struct obs_core_audio *audio = p;
  25. if (da_find(audio->render_order, &source, 0) == DARRAY_INVALID) {
  26. obs_source_addref(source);
  27. da_push_back(audio->render_order, &source);
  28. }
  29. UNUSED_PARAMETER(parent);
  30. }
  31. static inline size_t convert_time_to_frames(size_t sample_rate, uint64_t t)
  32. {
  33. return (size_t)(t * (uint64_t)sample_rate / 1000000000ULL);
  34. }
  35. static inline void mix_audio(struct audio_output_data *mixes,
  36. obs_source_t *source, size_t channels, size_t sample_rate,
  37. struct ts_info *ts)
  38. {
  39. size_t total_floats = AUDIO_OUTPUT_FRAMES;
  40. size_t start_point = 0;
  41. if (source->audio_ts < ts->start || ts->end <= source->audio_ts)
  42. return;
  43. if (source->audio_ts != ts->start) {
  44. start_point = convert_time_to_frames(sample_rate,
  45. source->audio_ts - ts->start);
  46. if (start_point == AUDIO_OUTPUT_FRAMES)
  47. return;
  48. total_floats -= start_point;
  49. }
  50. for (size_t mix_idx = 0; mix_idx < MAX_AUDIO_MIXES; mix_idx++) {
  51. for (size_t ch = 0; ch < channels; ch++) {
  52. register float *mix = mixes[mix_idx].data[ch];
  53. register float *aud =
  54. source->audio_output_buf[mix_idx][ch];
  55. register float *end;
  56. mix += start_point;
  57. end = aud + total_floats;
  58. while (aud < end)
  59. *(mix++) += *(aud++);
  60. }
  61. }
  62. }
  63. static void ignore_audio(obs_source_t *source, size_t channels,
  64. size_t sample_rate)
  65. {
  66. size_t num_floats = source->audio_input_buf[0].size / sizeof(float);
  67. if (num_floats) {
  68. for (size_t ch = 0; ch < channels; ch++)
  69. circlebuf_pop_front(&source->audio_input_buf[ch], NULL,
  70. source->audio_input_buf[ch].size);
  71. source->last_audio_input_buf_size = 0;
  72. source->audio_ts += (uint64_t)num_floats * 1000000000ULL /
  73. (uint64_t)sample_rate;
  74. }
  75. }
  76. static bool discard_if_stopped(obs_source_t *source, size_t channels)
  77. {
  78. size_t last_size;
  79. size_t size;
  80. last_size = source->last_audio_input_buf_size;
  81. size = source->audio_input_buf[0].size;
  82. if (!size)
  83. return false;
  84. /* if perpetually pending data, it means the audio has stopped,
  85. * so clear the audio data */
  86. if (last_size == size) {
  87. if (!source->pending_stop) {
  88. source->pending_stop = true;
  89. #if DEBUG_AUDIO == 1
  90. blog(LOG_DEBUG, "doing pending stop trick: '%s'",
  91. source->context.name);
  92. #endif
  93. return true;
  94. }
  95. for (size_t ch = 0; ch < channels; ch++)
  96. circlebuf_pop_front(&source->audio_input_buf[ch], NULL,
  97. source->audio_input_buf[ch].size);
  98. source->pending_stop = false;
  99. source->audio_ts = 0;
  100. source->last_audio_input_buf_size = 0;
  101. #if DEBUG_AUDIO == 1
  102. blog(LOG_DEBUG, "source audio data appears to have "
  103. "stopped, clearing");
  104. #endif
  105. return true;
  106. } else {
  107. source->last_audio_input_buf_size = size;
  108. return false;
  109. }
  110. }
  111. #define MAX_AUDIO_SIZE (AUDIO_OUTPUT_FRAMES * sizeof(float))
  112. static inline void discard_audio(struct obs_core_audio *audio,
  113. obs_source_t *source, size_t channels, size_t sample_rate,
  114. struct ts_info *ts)
  115. {
  116. size_t total_floats = AUDIO_OUTPUT_FRAMES;
  117. size_t size;
  118. #if DEBUG_AUDIO == 1
  119. bool is_audio_source = source->info.output_flags & OBS_SOURCE_AUDIO;
  120. #endif
  121. if (source->info.audio_render) {
  122. source->audio_ts = 0;
  123. return;
  124. }
  125. if (ts->end <= source->audio_ts) {
  126. #if DEBUG_AUDIO == 1
  127. blog(LOG_DEBUG, "can't discard, source "
  128. "timestamp (%"PRIu64") >= "
  129. "end timestamp (%"PRIu64")",
  130. source->audio_ts, ts->end);
  131. #endif
  132. return;
  133. }
  134. if (source->audio_ts < (ts->start - 1)) {
  135. if (source->audio_pending &&
  136. source->audio_input_buf[0].size < MAX_AUDIO_SIZE &&
  137. discard_if_stopped(source, channels))
  138. return;
  139. #if DEBUG_AUDIO == 1
  140. if (is_audio_source) {
  141. blog(LOG_DEBUG, "can't discard, source "
  142. "timestamp (%"PRIu64") < "
  143. "start timestamp (%"PRIu64")",
  144. source->audio_ts, ts->start);
  145. }
  146. #endif
  147. if (audio->total_buffering_ticks == MAX_BUFFERING_TICKS)
  148. ignore_audio(source, channels, sample_rate);
  149. return;
  150. }
  151. if (source->audio_ts != ts->start &&
  152. source->audio_ts != (ts->start - 1)) {
  153. size_t start_point = convert_time_to_frames(sample_rate,
  154. source->audio_ts - ts->start);
  155. if (start_point == AUDIO_OUTPUT_FRAMES) {
  156. #if DEBUG_AUDIO == 1
  157. if (is_audio_source)
  158. blog(LOG_DEBUG, "can't dicard, start point is "
  159. "at audio frame count");
  160. #endif
  161. return;
  162. }
  163. total_floats -= start_point;
  164. }
  165. size = total_floats * sizeof(float);
  166. if (source->audio_input_buf[0].size < size) {
  167. if (discard_if_stopped(source, channels))
  168. return;
  169. #if DEBUG_AUDIO == 1
  170. if (is_audio_source)
  171. blog(LOG_DEBUG, "can't discard, data still pending");
  172. #endif
  173. return;
  174. }
  175. for (size_t ch = 0; ch < channels; ch++)
  176. circlebuf_pop_front(&source->audio_input_buf[ch], NULL, size);
  177. source->last_audio_input_buf_size = 0;
  178. #if DEBUG_AUDIO == 1
  179. if (is_audio_source)
  180. blog(LOG_DEBUG, "audio discarded, new ts: %"PRIu64,
  181. ts->end);
  182. #endif
  183. source->pending_stop = false;
  184. source->audio_ts = ts->end;
  185. }
  186. static void add_audio_buffering(struct obs_core_audio *audio,
  187. size_t sample_rate, struct ts_info *ts, uint64_t min_ts)
  188. {
  189. struct ts_info new_ts;
  190. uint64_t offset;
  191. uint64_t frames;
  192. size_t total_ms;
  193. size_t ms;
  194. int ticks;
  195. if (audio->total_buffering_ticks == MAX_BUFFERING_TICKS)
  196. return;
  197. if (!audio->buffering_wait_ticks)
  198. audio->buffered_ts = ts->start;
  199. offset = ts->start - min_ts;
  200. frames = ns_to_audio_frames(sample_rate, offset);
  201. ticks = (int)((frames + AUDIO_OUTPUT_FRAMES - 1) / AUDIO_OUTPUT_FRAMES);
  202. audio->total_buffering_ticks += ticks;
  203. if (audio->total_buffering_ticks >= MAX_BUFFERING_TICKS) {
  204. ticks -= audio->total_buffering_ticks - MAX_BUFFERING_TICKS;
  205. audio->total_buffering_ticks = MAX_BUFFERING_TICKS;
  206. blog(LOG_WARNING, "Max audio buffering reached!");
  207. }
  208. ms = ticks * AUDIO_OUTPUT_FRAMES * 1000 / sample_rate;
  209. total_ms = audio->total_buffering_ticks * AUDIO_OUTPUT_FRAMES * 1000 /
  210. sample_rate;
  211. blog(LOG_INFO, "adding %d milliseconds of audio buffering, total "
  212. "audio buffering is now %d milliseconds",
  213. (int)ms, (int)total_ms);
  214. #if DEBUG_AUDIO == 1
  215. blog(LOG_DEBUG, "min_ts (%"PRIu64") < start timestamp "
  216. "(%"PRIu64")", min_ts, ts->start);
  217. blog(LOG_DEBUG, "old buffered ts: %"PRIu64"-%"PRIu64,
  218. ts->start, ts->end);
  219. #endif
  220. new_ts.start = audio->buffered_ts - audio_frames_to_ns(sample_rate,
  221. audio->buffering_wait_ticks * AUDIO_OUTPUT_FRAMES);
  222. while (ticks--) {
  223. int cur_ticks = ++audio->buffering_wait_ticks;
  224. new_ts.end = new_ts.start;
  225. new_ts.start = audio->buffered_ts - audio_frames_to_ns(
  226. sample_rate,
  227. cur_ticks * AUDIO_OUTPUT_FRAMES);
  228. #if DEBUG_AUDIO == 1
  229. blog(LOG_DEBUG, "add buffered ts: %"PRIu64"-%"PRIu64,
  230. new_ts.start, new_ts.end);
  231. #endif
  232. circlebuf_push_front(&audio->buffered_timestamps, &new_ts,
  233. sizeof(new_ts));
  234. }
  235. *ts = new_ts;
  236. }
  237. static bool audio_buffer_insuffient(struct obs_source *source,
  238. size_t sample_rate, uint64_t min_ts)
  239. {
  240. size_t total_floats = AUDIO_OUTPUT_FRAMES;
  241. size_t size;
  242. if (source->info.audio_render || source->audio_pending ||
  243. !source->audio_ts) {
  244. return false;
  245. }
  246. if (source->audio_ts != min_ts &&
  247. source->audio_ts != (min_ts - 1)) {
  248. size_t start_point = convert_time_to_frames(sample_rate,
  249. source->audio_ts - min_ts);
  250. if (start_point >= AUDIO_OUTPUT_FRAMES)
  251. return false;
  252. total_floats -= start_point;
  253. }
  254. size = total_floats * sizeof(float);
  255. if (source->audio_input_buf[0].size < size) {
  256. source->audio_pending = true;
  257. return true;
  258. }
  259. return false;
  260. }
  261. static inline void find_min_ts(struct obs_core_data *data,
  262. uint64_t *min_ts)
  263. {
  264. struct obs_source *source = data->first_audio_source;
  265. while (source) {
  266. if (!source->audio_pending && source->audio_ts &&
  267. source->audio_ts < *min_ts)
  268. *min_ts = source->audio_ts;
  269. source = (struct obs_source*)source->next_audio_source;
  270. }
  271. }
  272. static inline bool mark_invalid_sources(struct obs_core_data *data,
  273. size_t sample_rate, uint64_t min_ts)
  274. {
  275. bool recalculate = false;
  276. struct obs_source *source = data->first_audio_source;
  277. while (source) {
  278. recalculate |= audio_buffer_insuffient(source, sample_rate,
  279. min_ts);
  280. source = (struct obs_source*)source->next_audio_source;
  281. }
  282. return recalculate;
  283. }
  284. static inline void calc_min_ts(struct obs_core_data *data,
  285. size_t sample_rate, uint64_t *min_ts)
  286. {
  287. find_min_ts(data, min_ts);
  288. if (mark_invalid_sources(data, sample_rate, *min_ts))
  289. find_min_ts(data, min_ts);
  290. }
  291. static inline void release_audio_sources(struct obs_core_audio *audio)
  292. {
  293. for (size_t i = 0; i < audio->render_order.num; i++)
  294. obs_source_release(audio->render_order.array[i]);
  295. }
  296. bool audio_callback(void *param,
  297. uint64_t start_ts_in, uint64_t end_ts_in, uint64_t *out_ts,
  298. uint32_t mixers, struct audio_output_data *mixes)
  299. {
  300. struct obs_core_data *data = &obs->data;
  301. struct obs_core_audio *audio = &obs->audio;
  302. struct obs_source *source;
  303. size_t sample_rate = audio_output_get_sample_rate(audio->audio);
  304. size_t channels = audio_output_get_channels(audio->audio);
  305. struct ts_info ts = {start_ts_in, end_ts_in};
  306. size_t audio_size;
  307. uint64_t min_ts;
  308. da_resize(audio->render_order, 0);
  309. da_resize(audio->root_nodes, 0);
  310. circlebuf_push_back(&audio->buffered_timestamps, &ts, sizeof(ts));
  311. circlebuf_peek_front(&audio->buffered_timestamps, &ts, sizeof(ts));
  312. min_ts = ts.start;
  313. audio_size = AUDIO_OUTPUT_FRAMES * sizeof(float);
  314. #if DEBUG_AUDIO == 1
  315. blog(LOG_DEBUG, "ts %llu-%llu", ts.start, ts.end);
  316. #endif
  317. /* ------------------------------------------------ */
  318. /* build audio render order
  319. * NOTE: these are source channels, not audio channels */
  320. for (uint32_t i = 0; i < MAX_CHANNELS; i++) {
  321. obs_source_t *source = obs_get_output_source(i);
  322. if (source) {
  323. obs_source_enum_active_tree(source, push_audio_tree,
  324. audio);
  325. push_audio_tree(NULL, source, audio);
  326. da_push_back(audio->root_nodes, &source);
  327. obs_source_release(source);
  328. }
  329. }
  330. pthread_mutex_lock(&data->audio_sources_mutex);
  331. source = data->first_audio_source;
  332. while (source) {
  333. push_audio_tree(NULL, source, audio);
  334. source = (struct obs_source*)source->next_audio_source;
  335. }
  336. pthread_mutex_unlock(&data->audio_sources_mutex);
  337. /* ------------------------------------------------ */
  338. /* render audio data */
  339. for (size_t i = 0; i < audio->render_order.num; i++) {
  340. obs_source_t *source = audio->render_order.array[i];
  341. obs_source_audio_render(source, mixers, channels, sample_rate,
  342. audio_size);
  343. }
  344. /* ------------------------------------------------ */
  345. /* get minimum audio timestamp */
  346. pthread_mutex_lock(&data->audio_sources_mutex);
  347. calc_min_ts(data, sample_rate, &min_ts);
  348. pthread_mutex_unlock(&data->audio_sources_mutex);
  349. /* ------------------------------------------------ */
  350. /* if a source has gone backward in time, buffer */
  351. if (min_ts < ts.start)
  352. add_audio_buffering(audio, sample_rate, &ts, min_ts);
  353. /* ------------------------------------------------ */
  354. /* mix audio */
  355. if (!audio->buffering_wait_ticks) {
  356. for (size_t i = 0; i < audio->root_nodes.num; i++) {
  357. obs_source_t *source = audio->root_nodes.array[i];
  358. if (source->audio_pending)
  359. continue;
  360. pthread_mutex_lock(&source->audio_buf_mutex);
  361. if (source->audio_output_buf[0][0] && source->audio_ts)
  362. mix_audio(mixes, source, channels, sample_rate,
  363. &ts);
  364. pthread_mutex_unlock(&source->audio_buf_mutex);
  365. }
  366. }
  367. /* ------------------------------------------------ */
  368. /* discard audio */
  369. pthread_mutex_lock(&data->audio_sources_mutex);
  370. source = data->first_audio_source;
  371. while (source) {
  372. pthread_mutex_lock(&source->audio_buf_mutex);
  373. discard_audio(audio, source, channels, sample_rate, &ts);
  374. pthread_mutex_unlock(&source->audio_buf_mutex);
  375. source = (struct obs_source*)source->next_audio_source;
  376. }
  377. pthread_mutex_unlock(&data->audio_sources_mutex);
  378. /* ------------------------------------------------ */
  379. /* release audio sources */
  380. release_audio_sources(audio);
  381. circlebuf_pop_front(&audio->buffered_timestamps, NULL, sizeof(ts));
  382. *out_ts = ts.start;
  383. if (audio->buffering_wait_ticks) {
  384. audio->buffering_wait_ticks--;
  385. return false;
  386. }
  387. UNUSED_PARAMETER(param);
  388. return true;
  389. }