audio-io.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. while (os_event_try(audio->stop_event) == EAGAIN) {
  277. os_sleep_ms(AUDIO_WAIT_TIME);
  278. pthread_mutex_lock(&audio->line_mutex);
  279. audio_time = os_gettime_ns() - buffer_time;
  280. audio_time = mix_and_output(audio, audio_time, prev_time);
  281. prev_time = audio_time;
  282. pthread_mutex_unlock(&audio->line_mutex);
  283. }
  284. return NULL;
  285. }
  286. /* ------------------------------------------------------------------------- */
  287. static size_t audio_get_input_idx(const audio_t *video,
  288. void (*callback)(void *param, struct audio_data *data),
  289. void *param)
  290. {
  291. for (size_t i = 0; i < video->inputs.num; i++) {
  292. struct audio_input *input = video->inputs.array+i;
  293. if (input->callback == callback && input->param == param)
  294. return i;
  295. }
  296. return DARRAY_INVALID;
  297. }
  298. static inline bool audio_input_init(struct audio_input *input,
  299. struct audio_output *audio)
  300. {
  301. if (input->conversion.format != audio->info.format ||
  302. input->conversion.samples_per_sec != audio->info.samples_per_sec ||
  303. input->conversion.speakers != audio->info.speakers) {
  304. struct resample_info from = {
  305. .format = audio->info.format,
  306. .samples_per_sec = audio->info.samples_per_sec,
  307. .speakers = audio->info.speakers
  308. };
  309. struct resample_info to = {
  310. .format = input->conversion.format,
  311. .samples_per_sec = input->conversion.samples_per_sec,
  312. .speakers = input->conversion.speakers
  313. };
  314. input->resampler = audio_resampler_create(&to, &from);
  315. if (!input->resampler) {
  316. blog(LOG_ERROR, "audio_input_init: Failed to "
  317. "create resampler");
  318. return false;
  319. }
  320. } else {
  321. input->resampler = NULL;
  322. }
  323. return true;
  324. }
  325. bool audio_output_connect(audio_t *audio,
  326. const struct audio_convert_info *conversion,
  327. void (*callback)(void *param, struct audio_data *data),
  328. void *param)
  329. {
  330. bool success = false;
  331. if (!audio) return false;
  332. pthread_mutex_lock(&audio->input_mutex);
  333. if (audio_get_input_idx(audio, callback, param) == DARRAY_INVALID) {
  334. struct audio_input input;
  335. input.callback = callback;
  336. input.param = param;
  337. if (conversion) {
  338. input.conversion = *conversion;
  339. } else {
  340. input.conversion.format = audio->info.format;
  341. input.conversion.speakers = audio->info.speakers;
  342. input.conversion.samples_per_sec =
  343. audio->info.samples_per_sec;
  344. }
  345. if (input.conversion.format == AUDIO_FORMAT_UNKNOWN)
  346. input.conversion.format = audio->info.format;
  347. if (input.conversion.speakers == SPEAKERS_UNKNOWN)
  348. input.conversion.speakers = audio->info.speakers;
  349. if (input.conversion.samples_per_sec == 0)
  350. input.conversion.samples_per_sec =
  351. audio->info.samples_per_sec;
  352. success = audio_input_init(&input, audio);
  353. if (success)
  354. da_push_back(audio->inputs, &input);
  355. }
  356. pthread_mutex_unlock(&audio->input_mutex);
  357. return success;
  358. }
  359. void audio_output_disconnect(audio_t *audio,
  360. void (*callback)(void *param, struct audio_data *data),
  361. void *param)
  362. {
  363. if (!audio) return;
  364. pthread_mutex_lock(&audio->input_mutex);
  365. size_t idx = audio_get_input_idx(audio, callback, param);
  366. if (idx != DARRAY_INVALID) {
  367. audio_input_free(audio->inputs.array+idx);
  368. da_erase(audio->inputs, idx);
  369. }
  370. pthread_mutex_unlock(&audio->input_mutex);
  371. }
  372. static inline bool valid_audio_params(const struct audio_output_info *info)
  373. {
  374. return info->format && info->name && info->samples_per_sec > 0 &&
  375. info->speakers > 0;
  376. }
  377. int audio_output_open(audio_t **audio, struct audio_output_info *info)
  378. {
  379. struct audio_output *out;
  380. pthread_mutexattr_t attr;
  381. bool planar = is_audio_planar(info->format);
  382. if (!valid_audio_params(info))
  383. return AUDIO_OUTPUT_INVALIDPARAM;
  384. out = bzalloc(sizeof(struct audio_output));
  385. if (!out)
  386. goto fail;
  387. memcpy(&out->info, info, sizeof(struct audio_output_info));
  388. pthread_mutex_init_value(&out->line_mutex);
  389. out->channels = get_audio_channels(info->speakers);
  390. out->planes = planar ? out->channels : 1;
  391. out->block_size = (planar ? 1 : out->channels) *
  392. get_audio_bytes_per_channel(info->format);
  393. if (pthread_mutexattr_init(&attr) != 0)
  394. goto fail;
  395. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  396. goto fail;
  397. if (pthread_mutex_init(&out->line_mutex, &attr) != 0)
  398. goto fail;
  399. if (pthread_mutex_init(&out->input_mutex, NULL) != 0)
  400. goto fail;
  401. if (os_event_init(&out->stop_event, OS_EVENT_TYPE_MANUAL) != 0)
  402. goto fail;
  403. if (pthread_create(&out->thread, NULL, audio_thread, out) != 0)
  404. goto fail;
  405. out->initialized = true;
  406. *audio = out;
  407. return AUDIO_OUTPUT_SUCCESS;
  408. fail:
  409. audio_output_close(out);
  410. return AUDIO_OUTPUT_FAIL;
  411. }
  412. void audio_output_close(audio_t *audio)
  413. {
  414. void *thread_ret;
  415. struct audio_line *line;
  416. if (!audio)
  417. return;
  418. if (audio->initialized) {
  419. os_event_signal(audio->stop_event);
  420. pthread_join(audio->thread, &thread_ret);
  421. }
  422. line = audio->first_line;
  423. while (line) {
  424. struct audio_line *next = line->next;
  425. audio_line_destroy_data(line);
  426. line = next;
  427. }
  428. for (size_t i = 0; i < audio->inputs.num; i++)
  429. audio_input_free(audio->inputs.array+i);
  430. for (size_t i = 0; i < MAX_AV_PLANES; i++)
  431. da_free(audio->mix_buffers[i]);
  432. da_free(audio->inputs);
  433. os_event_destroy(audio->stop_event);
  434. pthread_mutex_destroy(&audio->line_mutex);
  435. bfree(audio);
  436. }
  437. audio_line_t *audio_output_create_line(audio_t *audio, const char *name)
  438. {
  439. if (!audio) return NULL;
  440. struct audio_line *line = bzalloc(sizeof(struct audio_line));
  441. line->alive = true;
  442. line->audio = audio;
  443. if (pthread_mutex_init(&line->mutex, NULL) != 0) {
  444. blog(LOG_ERROR, "audio_output_createline: Failed to create "
  445. "mutex");
  446. bfree(line);
  447. return NULL;
  448. }
  449. pthread_mutex_lock(&audio->line_mutex);
  450. if (audio->first_line) {
  451. audio->first_line->prev_next = &line->next;
  452. line->next = audio->first_line;
  453. }
  454. line->prev_next = &audio->first_line;
  455. audio->first_line = line;
  456. pthread_mutex_unlock(&audio->line_mutex);
  457. line->name = bstrdup(name ? name : "(unnamed audio line)");
  458. return line;
  459. }
  460. const struct audio_output_info *audio_output_get_info(const audio_t *audio)
  461. {
  462. return audio ? &audio->info : NULL;
  463. }
  464. void audio_line_destroy(struct audio_line *line)
  465. {
  466. if (line) {
  467. if (!line->buffers[0].size)
  468. audio_output_removeline(line->audio, line);
  469. else
  470. line->alive = false;
  471. }
  472. }
  473. bool audio_output_active(const audio_t *audio)
  474. {
  475. if (!audio) return false;
  476. return audio->inputs.num != 0;
  477. }
  478. size_t audio_output_get_block_size(const audio_t *audio)
  479. {
  480. return audio ? audio->block_size : 0;
  481. }
  482. size_t audio_output_get_planes(const audio_t *audio)
  483. {
  484. return audio ? audio->planes : 0;
  485. }
  486. size_t audio_output_get_channels(const audio_t *audio)
  487. {
  488. return audio ? audio->channels : 0;
  489. }
  490. uint32_t audio_output_get_sample_rate(const audio_t *audio)
  491. {
  492. return audio ? audio->info.samples_per_sec : 0;
  493. }
  494. /* TODO: optimize these two functions */
  495. static inline void mul_vol_float(float *array, float volume, size_t count)
  496. {
  497. for (size_t i = 0; i < count; i++)
  498. array[i] *= volume;
  499. }
  500. static void audio_line_place_data_pos(struct audio_line *line,
  501. const struct audio_data *data, size_t position)
  502. {
  503. bool planar = line->audio->planes > 1;
  504. size_t total_num = data->frames * (planar ? 1 : line->audio->channels);
  505. size_t total_size = data->frames * line->audio->block_size;
  506. for (size_t i = 0; i < line->audio->planes; i++) {
  507. da_copy_array(line->volume_buffers[i], data->data[i],
  508. total_size);
  509. uint8_t *array = line->volume_buffers[i].array;
  510. switch (line->audio->info.format) {
  511. case AUDIO_FORMAT_FLOAT:
  512. case AUDIO_FORMAT_FLOAT_PLANAR:
  513. mul_vol_float((float*)array, data->volume, total_num);
  514. break;
  515. default:
  516. blog(LOG_ERROR, "audio_line_place_data_pos: "
  517. "Unsupported or unknown format");
  518. break;
  519. }
  520. circlebuf_place(&line->buffers[i], position,
  521. line->volume_buffers[i].array, total_size);
  522. }
  523. }
  524. static inline uint64_t smooth_ts(struct audio_line *line, uint64_t timestamp)
  525. {
  526. if (!line->next_ts_min)
  527. return timestamp;
  528. bool ts_under = (timestamp < line->next_ts_min);
  529. uint64_t diff = ts_under ?
  530. (line->next_ts_min - timestamp) :
  531. (timestamp - line->next_ts_min);
  532. return (diff < TS_SMOOTHING_THRESHOLD) ? line->next_ts_min : timestamp;
  533. }
  534. static void audio_line_place_data(struct audio_line *line,
  535. const struct audio_data *data)
  536. {
  537. size_t pos;
  538. uint64_t timestamp = smooth_ts(line, data->timestamp);
  539. pos = ts_diff_bytes(line->audio, timestamp, line->base_timestamp);
  540. line->next_ts_min =
  541. timestamp + conv_frames_to_time(line->audio, data->frames);
  542. #ifdef DEBUG_AUDIO
  543. blog(LOG_DEBUG, "data->timestamp: %llu, line->base_timestamp: %llu, "
  544. "pos: %lu, bytes: %lu, buf size: %lu",
  545. timestamp, line->base_timestamp, pos,
  546. data->frames * line->audio->block_size,
  547. line->buffers[0].size);
  548. #endif
  549. audio_line_place_data_pos(line, data, pos);
  550. }
  551. #define MAX_DELAY_NS 6000000000ULL
  552. /* prevent insertation of data too far away from expected audio timing */
  553. static inline bool valid_timestamp_range(struct audio_line *line, uint64_t ts)
  554. {
  555. uint64_t buffer_ns = 1000000ULL * line->audio->info.buffer_ms;
  556. uint64_t max_ts = line->base_timestamp + buffer_ns + MAX_DELAY_NS;
  557. return ts >= line->base_timestamp && ts < max_ts;
  558. }
  559. void audio_line_output(audio_line_t *line, const struct audio_data *data)
  560. {
  561. if (!line || !data) return;
  562. pthread_mutex_lock(&line->mutex);
  563. if (!line->buffers[0].size) {
  564. line->base_timestamp = data->timestamp -
  565. line->audio->info.buffer_ms * 1000000;
  566. audio_line_place_data(line, data);
  567. } else if (valid_timestamp_range(line, data->timestamp)) {
  568. audio_line_place_data(line, data);
  569. } else {
  570. blog(LOG_DEBUG, "Bad timestamp for audio line '%s', "
  571. "data->timestamp: %"PRIu64", "
  572. "line->base_timestamp: %"PRIu64". This can "
  573. "sometimes happen when there's a pause in "
  574. "the threads.", line->name, data->timestamp,
  575. line->base_timestamp);
  576. }
  577. pthread_mutex_unlock(&line->mutex);
  578. }