audio-io.c 19 KB

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