audio-io.c 18 KB

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