audio-io.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /******************************************************************************
  2. Copyright (C) 2013 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 <math.h>
  15. #include <inttypes.h>
  16. #include "../util/threading.h"
  17. #include "../util/darray.h"
  18. #include "../util/circlebuf.h"
  19. #include "../util/platform.h"
  20. #include "audio-io.h"
  21. #include "audio-resampler.h"
  22. /* #define DEBUG_AUDIO */
  23. #define nop() do {int invalid = 0;} while(0)
  24. struct audio_input {
  25. struct audio_convert_info conversion;
  26. audio_resampler_t *resampler;
  27. void (*callback)(void *param, struct audio_data *data);
  28. void *param;
  29. };
  30. static inline void audio_input_free(struct audio_input *input)
  31. {
  32. audio_resampler_destroy(input->resampler);
  33. }
  34. struct audio_line {
  35. char *name;
  36. struct audio_output *audio;
  37. struct circlebuf buffers[MAX_AV_PLANES];
  38. pthread_mutex_t mutex;
  39. DARRAY(uint8_t) volume_buffers[MAX_AV_PLANES];
  40. uint64_t base_timestamp;
  41. uint64_t last_timestamp;
  42. uint64_t next_ts_min;
  43. /* states whether this line is still being used. if not, then when the
  44. * buffer is depleted, it's destroyed */
  45. bool alive;
  46. struct audio_line **prev_next;
  47. struct audio_line *next;
  48. };
  49. static inline void audio_line_destroy_data(struct audio_line *line)
  50. {
  51. for (size_t i = 0; i < MAX_AV_PLANES; i++) {
  52. circlebuf_free(&line->buffers[i]);
  53. da_free(line->volume_buffers[i]);
  54. }
  55. pthread_mutex_destroy(&line->mutex);
  56. bfree(line->name);
  57. bfree(line);
  58. }
  59. struct audio_output {
  60. struct audio_output_info info;
  61. size_t block_size;
  62. size_t channels;
  63. size_t planes;
  64. pthread_t thread;
  65. os_event_t *stop_event;
  66. DARRAY(uint8_t) mix_buffers[MAX_AV_PLANES];
  67. bool initialized;
  68. pthread_mutex_t line_mutex;
  69. struct audio_line *first_line;
  70. pthread_mutex_t input_mutex;
  71. DARRAY(struct audio_input) inputs;
  72. };
  73. static inline void audio_output_removeline(struct audio_output *audio,
  74. struct audio_line *line)
  75. {
  76. pthread_mutex_lock(&audio->line_mutex);
  77. if (line->prev_next)
  78. *line->prev_next = line->next;
  79. if (line->next)
  80. line->next->prev_next = line->prev_next;
  81. pthread_mutex_unlock(&audio->line_mutex);
  82. audio_line_destroy_data(line);
  83. }
  84. /* ------------------------------------------------------------------------- */
  85. /* the following functions are used to calculate frame offsets based upon
  86. * timestamps. this will actually work accurately as long as you handle the
  87. * values correctly */
  88. static inline double ts_to_frames(const audio_t *audio, uint64_t ts)
  89. {
  90. double audio_offset_d = (double)ts;
  91. audio_offset_d /= 1000000000.0;
  92. audio_offset_d *= (double)audio->info.samples_per_sec;
  93. return audio_offset_d;
  94. }
  95. static inline double positive_round(double val)
  96. {
  97. return floor(val+0.5);
  98. }
  99. static size_t ts_diff_frames(const audio_t *audio, uint64_t ts1, uint64_t ts2)
  100. {
  101. double diff = ts_to_frames(audio, ts1) - ts_to_frames(audio, ts2);
  102. return (size_t)positive_round(diff);
  103. }
  104. static size_t ts_diff_bytes(const audio_t *audio, uint64_t ts1, uint64_t ts2)
  105. {
  106. return ts_diff_frames(audio, ts1, ts2) * audio->block_size;
  107. }
  108. /* unless the value is 3+ hours worth of frames, this won't overflow */
  109. static inline uint64_t conv_frames_to_time(const audio_t *audio,
  110. uint32_t frames)
  111. {
  112. return (uint64_t)frames * 1000000000ULL /
  113. (uint64_t)audio->info.samples_per_sec;
  114. }
  115. /* ------------------------------------------------------------------------- */
  116. /* this only really happens with the very initial data insertion. can be
  117. * ignored safely. */
  118. static inline void clear_excess_audio_data(struct audio_line *line,
  119. uint64_t prev_time)
  120. {
  121. size_t size = ts_diff_bytes(line->audio, prev_time,
  122. line->base_timestamp);
  123. /*blog(LOG_DEBUG, "Excess audio data for audio line '%s', somehow "
  124. "audio data went back in time by %"PRIu32" bytes. "
  125. "prev_time: %"PRIu64", line->base_timestamp: %"PRIu64,
  126. line->name, (uint32_t)size,
  127. prev_time, line->base_timestamp);*/
  128. for (size_t i = 0; i < line->audio->planes; i++) {
  129. size_t clear_size = (size < line->buffers[i].size) ?
  130. size : line->buffers[i].size;
  131. circlebuf_pop_front(&line->buffers[i], NULL, clear_size);
  132. }
  133. }
  134. static inline uint64_t min_uint64(uint64_t a, uint64_t b)
  135. {
  136. return a < b ? a : b;
  137. }
  138. static inline size_t min_size(size_t a, size_t b)
  139. {
  140. return a < b ? a : b;
  141. }
  142. #ifndef CLAMP
  143. #define CLAMP(val, minval, maxval) \
  144. ((val > maxval) ? maxval : ((val < minval) ? minval : val))
  145. #endif
  146. #define MIX_BUFFER_SIZE 256
  147. /* TODO: optimize mixing */
  148. static void mix_float(uint8_t *mix_in, struct circlebuf *buf, size_t size)
  149. {
  150. float *mix = (float*)mix_in;
  151. float vals[MIX_BUFFER_SIZE];
  152. register float mix_val;
  153. while (size) {
  154. size_t pop_count = min_size(size, sizeof(vals));
  155. size -= pop_count;
  156. circlebuf_pop_front(buf, vals, pop_count);
  157. pop_count /= sizeof(float);
  158. /* This sequence provides hints for MSVC to use packed SSE
  159. * instructions addps, minps, maxps, etc. */
  160. for (size_t i = 0; i < pop_count; i++) {
  161. mix_val = *mix + vals[i];
  162. /* clamp confuses the optimisation */
  163. mix_val = (mix_val > 1.0f) ? 1.0f : mix_val;
  164. mix_val = (mix_val < -1.0f) ? -1.0f : mix_val;
  165. *(mix++) = mix_val;
  166. }
  167. }
  168. }
  169. static inline bool mix_audio_line(struct audio_output *audio,
  170. struct audio_line *line, size_t size, uint64_t timestamp)
  171. {
  172. size_t time_offset = ts_diff_bytes(audio,
  173. line->base_timestamp, timestamp);
  174. if (time_offset > size)
  175. return false;
  176. size -= time_offset;
  177. #ifdef DEBUG_AUDIO
  178. blog(LOG_DEBUG, "shaved off %lu bytes", size);
  179. #endif
  180. for (size_t i = 0; i < audio->planes; i++) {
  181. size_t pop_size = min_size(size, line->buffers[i].size);
  182. mix_float(audio->mix_buffers[i].array + time_offset,
  183. &line->buffers[i], pop_size);
  184. }
  185. return true;
  186. }
  187. static bool resample_audio_output(struct audio_input *input,
  188. struct audio_data *data)
  189. {
  190. bool success = true;
  191. if (input->resampler) {
  192. uint8_t *output[MAX_AV_PLANES];
  193. uint32_t frames;
  194. uint64_t offset;
  195. memset(output, 0, sizeof(output));
  196. success = audio_resampler_resample(input->resampler,
  197. output, &frames, &offset,
  198. (const uint8_t *const *)data->data,
  199. data->frames);
  200. for (size_t i = 0; i < MAX_AV_PLANES; i++)
  201. data->data[i] = output[i];
  202. data->frames = frames;
  203. data->timestamp -= offset;
  204. }
  205. return success;
  206. }
  207. static inline void do_audio_output(struct audio_output *audio,
  208. uint64_t timestamp, uint32_t frames)
  209. {
  210. struct audio_data data;
  211. for (size_t i = 0; i < MAX_AV_PLANES; i++)
  212. data.data[i] = audio->mix_buffers[i].array;
  213. data.frames = frames;
  214. data.timestamp = timestamp;
  215. data.volume = 1.0f;
  216. pthread_mutex_lock(&audio->input_mutex);
  217. for (size_t i = 0; i < audio->inputs.num; i++) {
  218. struct audio_input *input = audio->inputs.array+i;
  219. if (resample_audio_output(input, &data))
  220. input->callback(input->param, &data);
  221. }
  222. pthread_mutex_unlock(&audio->input_mutex);
  223. }
  224. static uint64_t mix_and_output(struct audio_output *audio, uint64_t audio_time,
  225. uint64_t prev_time)
  226. {
  227. struct audio_line *line = audio->first_line;
  228. uint32_t frames = (uint32_t)ts_diff_frames(audio, audio_time,
  229. prev_time);
  230. size_t bytes = frames * audio->block_size;
  231. #ifdef DEBUG_AUDIO
  232. blog(LOG_DEBUG, "audio_time: %llu, prev_time: %llu, bytes: %lu",
  233. audio_time, prev_time, bytes);
  234. #endif
  235. /* return an adjusted audio_time according to the amount
  236. * of data that was sampled to ensure seamless transmission */
  237. audio_time = prev_time + conv_frames_to_time(audio, frames);
  238. /* resize and clear mix buffers */
  239. for (size_t i = 0; i < audio->planes; i++) {
  240. da_resize(audio->mix_buffers[i], bytes);
  241. memset(audio->mix_buffers[i].array, 0, bytes);
  242. }
  243. /* mix audio lines */
  244. while (line) {
  245. struct audio_line *next = line->next;
  246. /* if line marked for removal, destroy and move to the next */
  247. if (!line->buffers[0].size) {
  248. if (!line->alive) {
  249. audio_output_removeline(audio, line);
  250. line = next;
  251. continue;
  252. }
  253. }
  254. pthread_mutex_lock(&line->mutex);
  255. if (line->buffers[0].size && line->base_timestamp < prev_time) {
  256. clear_excess_audio_data(line, prev_time);
  257. line->base_timestamp = prev_time;
  258. }
  259. if (mix_audio_line(audio, line, bytes, prev_time))
  260. line->base_timestamp = audio_time;
  261. pthread_mutex_unlock(&line->mutex);
  262. line = next;
  263. }
  264. /* output */
  265. do_audio_output(audio, prev_time, frames);
  266. return audio_time;
  267. }
  268. /* sample audio 40 times a second */
  269. #define AUDIO_WAIT_TIME (1000/40)
  270. static void *audio_thread(void *param)
  271. {
  272. struct audio_output *audio = param;
  273. uint64_t buffer_time = audio->info.buffer_ms * 1000000;
  274. uint64_t prev_time = os_gettime_ns() - buffer_time;
  275. uint64_t audio_time;
  276. os_set_thread_name("audio-io: audio thread");
  277. while (os_event_try(audio->stop_event) == EAGAIN) {
  278. os_sleep_ms(AUDIO_WAIT_TIME);
  279. pthread_mutex_lock(&audio->line_mutex);
  280. audio_time = os_gettime_ns() - buffer_time;
  281. audio_time = mix_and_output(audio, audio_time, prev_time);
  282. prev_time = audio_time;
  283. pthread_mutex_unlock(&audio->line_mutex);
  284. }
  285. return NULL;
  286. }
  287. /* ------------------------------------------------------------------------- */
  288. static size_t audio_get_input_idx(const audio_t *video,
  289. void (*callback)(void *param, struct audio_data *data),
  290. void *param)
  291. {
  292. for (size_t i = 0; i < video->inputs.num; i++) {
  293. struct audio_input *input = video->inputs.array+i;
  294. if (input->callback == callback && input->param == param)
  295. return i;
  296. }
  297. return DARRAY_INVALID;
  298. }
  299. static inline bool audio_input_init(struct audio_input *input,
  300. struct audio_output *audio)
  301. {
  302. if (input->conversion.format != audio->info.format ||
  303. input->conversion.samples_per_sec != audio->info.samples_per_sec ||
  304. input->conversion.speakers != audio->info.speakers) {
  305. struct resample_info from = {
  306. .format = audio->info.format,
  307. .samples_per_sec = audio->info.samples_per_sec,
  308. .speakers = audio->info.speakers
  309. };
  310. struct resample_info to = {
  311. .format = input->conversion.format,
  312. .samples_per_sec = input->conversion.samples_per_sec,
  313. .speakers = input->conversion.speakers
  314. };
  315. input->resampler = audio_resampler_create(&to, &from);
  316. if (!input->resampler) {
  317. blog(LOG_ERROR, "audio_input_init: Failed to "
  318. "create resampler");
  319. return false;
  320. }
  321. } else {
  322. input->resampler = NULL;
  323. }
  324. return true;
  325. }
  326. bool audio_output_connect(audio_t *audio,
  327. const struct audio_convert_info *conversion,
  328. void (*callback)(void *param, struct audio_data *data),
  329. void *param)
  330. {
  331. bool success = false;
  332. if (!audio) return false;
  333. pthread_mutex_lock(&audio->input_mutex);
  334. if (audio_get_input_idx(audio, callback, param) == DARRAY_INVALID) {
  335. struct audio_input input;
  336. input.callback = callback;
  337. input.param = param;
  338. if (conversion) {
  339. input.conversion = *conversion;
  340. } else {
  341. input.conversion.format = audio->info.format;
  342. input.conversion.speakers = audio->info.speakers;
  343. input.conversion.samples_per_sec =
  344. audio->info.samples_per_sec;
  345. }
  346. if (input.conversion.format == AUDIO_FORMAT_UNKNOWN)
  347. input.conversion.format = audio->info.format;
  348. if (input.conversion.speakers == SPEAKERS_UNKNOWN)
  349. input.conversion.speakers = audio->info.speakers;
  350. if (input.conversion.samples_per_sec == 0)
  351. input.conversion.samples_per_sec =
  352. audio->info.samples_per_sec;
  353. success = audio_input_init(&input, audio);
  354. if (success)
  355. da_push_back(audio->inputs, &input);
  356. }
  357. pthread_mutex_unlock(&audio->input_mutex);
  358. return success;
  359. }
  360. void audio_output_disconnect(audio_t *audio,
  361. void (*callback)(void *param, struct audio_data *data),
  362. void *param)
  363. {
  364. if (!audio) return;
  365. pthread_mutex_lock(&audio->input_mutex);
  366. size_t idx = audio_get_input_idx(audio, callback, param);
  367. if (idx != DARRAY_INVALID) {
  368. audio_input_free(audio->inputs.array+idx);
  369. da_erase(audio->inputs, idx);
  370. }
  371. pthread_mutex_unlock(&audio->input_mutex);
  372. }
  373. static inline bool valid_audio_params(const struct audio_output_info *info)
  374. {
  375. return info->format && info->name && info->samples_per_sec > 0 &&
  376. info->speakers > 0;
  377. }
  378. int audio_output_open(audio_t **audio, struct audio_output_info *info)
  379. {
  380. struct audio_output *out;
  381. pthread_mutexattr_t attr;
  382. bool planar = is_audio_planar(info->format);
  383. if (!valid_audio_params(info))
  384. return AUDIO_OUTPUT_INVALIDPARAM;
  385. out = bzalloc(sizeof(struct audio_output));
  386. if (!out)
  387. goto fail;
  388. memcpy(&out->info, info, sizeof(struct audio_output_info));
  389. pthread_mutex_init_value(&out->line_mutex);
  390. out->channels = get_audio_channels(info->speakers);
  391. out->planes = planar ? out->channels : 1;
  392. out->block_size = (planar ? 1 : out->channels) *
  393. get_audio_bytes_per_channel(info->format);
  394. if (pthread_mutexattr_init(&attr) != 0)
  395. goto fail;
  396. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  397. goto fail;
  398. if (pthread_mutex_init(&out->line_mutex, &attr) != 0)
  399. goto fail;
  400. if (pthread_mutex_init(&out->input_mutex, NULL) != 0)
  401. goto fail;
  402. if (os_event_init(&out->stop_event, OS_EVENT_TYPE_MANUAL) != 0)
  403. goto fail;
  404. if (pthread_create(&out->thread, NULL, audio_thread, out) != 0)
  405. goto fail;
  406. out->initialized = true;
  407. *audio = out;
  408. return AUDIO_OUTPUT_SUCCESS;
  409. fail:
  410. audio_output_close(out);
  411. return AUDIO_OUTPUT_FAIL;
  412. }
  413. void audio_output_close(audio_t *audio)
  414. {
  415. void *thread_ret;
  416. struct audio_line *line;
  417. if (!audio)
  418. return;
  419. if (audio->initialized) {
  420. os_event_signal(audio->stop_event);
  421. pthread_join(audio->thread, &thread_ret);
  422. }
  423. line = audio->first_line;
  424. while (line) {
  425. struct audio_line *next = line->next;
  426. audio_line_destroy_data(line);
  427. line = next;
  428. }
  429. for (size_t i = 0; i < audio->inputs.num; i++)
  430. audio_input_free(audio->inputs.array+i);
  431. for (size_t i = 0; i < MAX_AV_PLANES; i++)
  432. da_free(audio->mix_buffers[i]);
  433. da_free(audio->inputs);
  434. os_event_destroy(audio->stop_event);
  435. pthread_mutex_destroy(&audio->line_mutex);
  436. bfree(audio);
  437. }
  438. audio_line_t *audio_output_create_line(audio_t *audio, const char *name)
  439. {
  440. if (!audio) return NULL;
  441. struct audio_line *line = bzalloc(sizeof(struct audio_line));
  442. line->alive = true;
  443. line->audio = audio;
  444. if (pthread_mutex_init(&line->mutex, NULL) != 0) {
  445. blog(LOG_ERROR, "audio_output_createline: Failed to create "
  446. "mutex");
  447. bfree(line);
  448. return NULL;
  449. }
  450. pthread_mutex_lock(&audio->line_mutex);
  451. if (audio->first_line) {
  452. audio->first_line->prev_next = &line->next;
  453. line->next = audio->first_line;
  454. }
  455. line->prev_next = &audio->first_line;
  456. audio->first_line = line;
  457. pthread_mutex_unlock(&audio->line_mutex);
  458. line->name = bstrdup(name ? name : "(unnamed audio line)");
  459. return line;
  460. }
  461. const struct audio_output_info *audio_output_get_info(const audio_t *audio)
  462. {
  463. return audio ? &audio->info : NULL;
  464. }
  465. void audio_line_destroy(struct audio_line *line)
  466. {
  467. if (line) {
  468. if (!line->buffers[0].size)
  469. audio_output_removeline(line->audio, line);
  470. else
  471. line->alive = false;
  472. }
  473. }
  474. bool audio_output_active(const audio_t *audio)
  475. {
  476. if (!audio) return false;
  477. return audio->inputs.num != 0;
  478. }
  479. size_t audio_output_get_block_size(const audio_t *audio)
  480. {
  481. return audio ? audio->block_size : 0;
  482. }
  483. size_t audio_output_get_planes(const audio_t *audio)
  484. {
  485. return audio ? audio->planes : 0;
  486. }
  487. size_t audio_output_get_channels(const audio_t *audio)
  488. {
  489. return audio ? audio->channels : 0;
  490. }
  491. uint32_t audio_output_get_sample_rate(const audio_t *audio)
  492. {
  493. return audio ? audio->info.samples_per_sec : 0;
  494. }
  495. /* TODO: optimize these two functions */
  496. static inline void mul_vol_float(float *array, float volume, size_t count)
  497. {
  498. for (size_t i = 0; i < count; i++)
  499. array[i] *= volume;
  500. }
  501. static void audio_line_place_data_pos(struct audio_line *line,
  502. const struct audio_data *data, size_t position)
  503. {
  504. bool planar = line->audio->planes > 1;
  505. size_t total_num = data->frames * (planar ? 1 : line->audio->channels);
  506. size_t total_size = data->frames * line->audio->block_size;
  507. for (size_t i = 0; i < line->audio->planes; i++) {
  508. da_copy_array(line->volume_buffers[i], data->data[i],
  509. total_size);
  510. uint8_t *array = line->volume_buffers[i].array;
  511. switch (line->audio->info.format) {
  512. case AUDIO_FORMAT_FLOAT:
  513. case AUDIO_FORMAT_FLOAT_PLANAR:
  514. mul_vol_float((float*)array, data->volume, total_num);
  515. break;
  516. default:
  517. blog(LOG_ERROR, "audio_line_place_data_pos: "
  518. "Unsupported or unknown format");
  519. break;
  520. }
  521. circlebuf_place(&line->buffers[i], position,
  522. line->volume_buffers[i].array, total_size);
  523. }
  524. }
  525. static inline uint64_t smooth_ts(struct audio_line *line, uint64_t timestamp)
  526. {
  527. if (!line->next_ts_min)
  528. return timestamp;
  529. bool ts_under = (timestamp < line->next_ts_min);
  530. uint64_t diff = ts_under ?
  531. (line->next_ts_min - timestamp) :
  532. (timestamp - line->next_ts_min);
  533. #ifdef DEBUG_AUDIO
  534. if (diff >= TS_SMOOTHING_THRESHOLD)
  535. blog(LOG_DEBUG, "above TS smoothing threshold by %"PRIu64,
  536. diff);
  537. #endif
  538. return (diff < TS_SMOOTHING_THRESHOLD) ? line->next_ts_min : timestamp;
  539. }
  540. static void audio_line_place_data(struct audio_line *line,
  541. const struct audio_data *data)
  542. {
  543. size_t pos;
  544. uint64_t timestamp = smooth_ts(line, data->timestamp);
  545. pos = ts_diff_bytes(line->audio, timestamp, line->base_timestamp);
  546. line->next_ts_min =
  547. timestamp + conv_frames_to_time(line->audio, data->frames);
  548. #ifdef DEBUG_AUDIO
  549. blog(LOG_DEBUG, "data->timestamp: %llu, line->base_timestamp: %llu, "
  550. "pos: %lu, bytes: %lu, buf size: %lu",
  551. timestamp, line->base_timestamp, pos,
  552. data->frames * line->audio->block_size,
  553. line->buffers[0].size);
  554. #endif
  555. audio_line_place_data_pos(line, data, pos);
  556. }
  557. #define MAX_DELAY_NS 6000000000ULL
  558. /* prevent insertation of data too far away from expected audio timing */
  559. static inline bool valid_timestamp_range(struct audio_line *line, uint64_t ts)
  560. {
  561. uint64_t buffer_ns = 1000000ULL * line->audio->info.buffer_ms;
  562. uint64_t max_ts = line->base_timestamp + buffer_ns + MAX_DELAY_NS;
  563. return ts >= line->base_timestamp && ts < max_ts;
  564. }
  565. void audio_line_output(audio_line_t *line, const struct audio_data *data)
  566. {
  567. if (!line || !data) return;
  568. pthread_mutex_lock(&line->mutex);
  569. if (!line->buffers[0].size) {
  570. line->base_timestamp = data->timestamp -
  571. line->audio->info.buffer_ms * 1000000;
  572. audio_line_place_data(line, data);
  573. } else if (valid_timestamp_range(line, data->timestamp)) {
  574. audio_line_place_data(line, data);
  575. } else {
  576. blog(LOG_DEBUG, "Bad timestamp for audio line '%s', "
  577. "data->timestamp: %"PRIu64", "
  578. "line->base_timestamp: %"PRIu64". This can "
  579. "sometimes happen when there's a pause in "
  580. "the threads.", line->name, data->timestamp,
  581. line->base_timestamp);
  582. }
  583. pthread_mutex_unlock(&line->mutex);
  584. }