audio-io.c 19 KB

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