obs-audio.c 12 KB

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