obs-audio.c 11 KB

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