obs-audio.c 13 KB

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