pulse_audio_dec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Pulseaudio input
  3. * Copyright (c) 2011 Luca Barbato <lu_zero@gentoo.org>
  4. * Copyright 2004-2006 Lennart Poettering
  5. * Copyright (c) 2014 Michael Niedermayer <michaelni@gmx.at>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <pulse/rtclock.h>
  24. #include <pulse/error.h>
  25. #include "libavformat/avformat.h"
  26. #include "libavformat/internal.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/time.h"
  29. #include "pulse_audio_common.h"
  30. #include "timefilter.h"
  31. #define DEFAULT_CODEC_ID AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE)
  32. typedef struct PulseData {
  33. AVClass *class;
  34. char *server;
  35. char *name;
  36. char *stream_name;
  37. int sample_rate;
  38. int channels;
  39. int frame_size;
  40. int fragment_size;
  41. pa_threaded_mainloop *mainloop;
  42. pa_context *context;
  43. pa_stream *stream;
  44. TimeFilter *timefilter;
  45. int last_period;
  46. } PulseData;
  47. #define CHECK_SUCCESS_GOTO(rerror, expression, label) \
  48. do { \
  49. if (!(expression)) { \
  50. rerror = AVERROR_EXTERNAL; \
  51. goto label; \
  52. } \
  53. } while(0);
  54. #define CHECK_DEAD_GOTO(p, rerror, label) \
  55. do { \
  56. if (!(p)->context || !PA_CONTEXT_IS_GOOD(pa_context_get_state((p)->context)) || \
  57. !(p)->stream || !PA_STREAM_IS_GOOD(pa_stream_get_state((p)->stream))) { \
  58. rerror = AVERROR_EXTERNAL; \
  59. goto label; \
  60. } \
  61. } while(0);
  62. static void context_state_cb(pa_context *c, void *userdata) {
  63. PulseData *p = userdata;
  64. switch (pa_context_get_state(c)) {
  65. case PA_CONTEXT_READY:
  66. case PA_CONTEXT_TERMINATED:
  67. case PA_CONTEXT_FAILED:
  68. pa_threaded_mainloop_signal(p->mainloop, 0);
  69. break;
  70. }
  71. }
  72. static void stream_state_cb(pa_stream *s, void * userdata) {
  73. PulseData *p = userdata;
  74. switch (pa_stream_get_state(s)) {
  75. case PA_STREAM_READY:
  76. case PA_STREAM_FAILED:
  77. case PA_STREAM_TERMINATED:
  78. pa_threaded_mainloop_signal(p->mainloop, 0);
  79. break;
  80. }
  81. }
  82. static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
  83. PulseData *p = userdata;
  84. pa_threaded_mainloop_signal(p->mainloop, 0);
  85. }
  86. static void stream_latency_update_cb(pa_stream *s, void *userdata) {
  87. PulseData *p = userdata;
  88. pa_threaded_mainloop_signal(p->mainloop, 0);
  89. }
  90. static av_cold int pulse_close(AVFormatContext *s)
  91. {
  92. PulseData *pd = s->priv_data;
  93. if (pd->mainloop)
  94. pa_threaded_mainloop_stop(pd->mainloop);
  95. if (pd->stream)
  96. pa_stream_unref(pd->stream);
  97. pd->stream = NULL;
  98. if (pd->context) {
  99. pa_context_disconnect(pd->context);
  100. pa_context_unref(pd->context);
  101. }
  102. pd->context = NULL;
  103. if (pd->mainloop)
  104. pa_threaded_mainloop_free(pd->mainloop);
  105. pd->mainloop = NULL;
  106. ff_timefilter_destroy(pd->timefilter);
  107. pd->timefilter = NULL;
  108. return 0;
  109. }
  110. static av_cold int pulse_read_header(AVFormatContext *s)
  111. {
  112. PulseData *pd = s->priv_data;
  113. AVStream *st;
  114. char *device = NULL;
  115. int ret;
  116. enum AVCodecID codec_id =
  117. s->audio_codec_id == AV_CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id;
  118. const pa_sample_spec ss = { ff_codec_id_to_pulse_format(codec_id),
  119. pd->sample_rate,
  120. pd->channels };
  121. pa_buffer_attr attr = { -1 };
  122. st = avformat_new_stream(s, NULL);
  123. if (!st) {
  124. av_log(s, AV_LOG_ERROR, "Cannot add stream\n");
  125. return AVERROR(ENOMEM);
  126. }
  127. attr.fragsize = pd->fragment_size;
  128. if (strcmp(s->filename, "default"))
  129. device = s->filename;
  130. if (!(pd->mainloop = pa_threaded_mainloop_new())) {
  131. pulse_close(s);
  132. return AVERROR_EXTERNAL;
  133. }
  134. if (!(pd->context = pa_context_new(pa_threaded_mainloop_get_api(pd->mainloop), pd->name))) {
  135. pulse_close(s);
  136. return AVERROR_EXTERNAL;
  137. }
  138. pa_context_set_state_callback(pd->context, context_state_cb, pd);
  139. if (pa_context_connect(pd->context, pd->server, 0, NULL) < 0) {
  140. pulse_close(s);
  141. return AVERROR(pa_context_errno(pd->context));
  142. }
  143. pa_threaded_mainloop_lock(pd->mainloop);
  144. if (pa_threaded_mainloop_start(pd->mainloop) < 0) {
  145. ret = -1;
  146. goto unlock_and_fail;
  147. }
  148. for (;;) {
  149. pa_context_state_t state;
  150. state = pa_context_get_state(pd->context);
  151. if (state == PA_CONTEXT_READY)
  152. break;
  153. if (!PA_CONTEXT_IS_GOOD(state)) {
  154. ret = AVERROR(pa_context_errno(pd->context));
  155. goto unlock_and_fail;
  156. }
  157. /* Wait until the context is ready */
  158. pa_threaded_mainloop_wait(pd->mainloop);
  159. }
  160. if (!(pd->stream = pa_stream_new(pd->context, pd->stream_name, &ss, NULL))) {
  161. ret = AVERROR(pa_context_errno(pd->context));
  162. goto unlock_and_fail;
  163. }
  164. pa_stream_set_state_callback(pd->stream, stream_state_cb, pd);
  165. pa_stream_set_read_callback(pd->stream, stream_request_cb, pd);
  166. pa_stream_set_write_callback(pd->stream, stream_request_cb, pd);
  167. pa_stream_set_latency_update_callback(pd->stream, stream_latency_update_cb, pd);
  168. ret = pa_stream_connect_record(pd->stream, device, &attr,
  169. PA_STREAM_INTERPOLATE_TIMING
  170. |PA_STREAM_ADJUST_LATENCY
  171. |PA_STREAM_AUTO_TIMING_UPDATE);
  172. if (ret < 0) {
  173. ret = AVERROR(pa_context_errno(pd->context));
  174. goto unlock_and_fail;
  175. }
  176. for (;;) {
  177. pa_stream_state_t state;
  178. state = pa_stream_get_state(pd->stream);
  179. if (state == PA_STREAM_READY)
  180. break;
  181. if (!PA_STREAM_IS_GOOD(state)) {
  182. ret = AVERROR(pa_context_errno(pd->context));
  183. goto unlock_and_fail;
  184. }
  185. /* Wait until the stream is ready */
  186. pa_threaded_mainloop_wait(pd->mainloop);
  187. }
  188. pa_threaded_mainloop_unlock(pd->mainloop);
  189. /* take real parameters */
  190. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  191. st->codec->codec_id = codec_id;
  192. st->codec->sample_rate = pd->sample_rate;
  193. st->codec->channels = pd->channels;
  194. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  195. pd->timefilter = ff_timefilter_new(1000000.0 / pd->sample_rate,
  196. 1000, 1.5E-6);
  197. if (!pd->timefilter) {
  198. pulse_close(s);
  199. return AVERROR(ENOMEM);
  200. }
  201. return 0;
  202. unlock_and_fail:
  203. pa_threaded_mainloop_unlock(pd->mainloop);
  204. pulse_close(s);
  205. return ret;
  206. }
  207. static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
  208. {
  209. PulseData *pd = s->priv_data;
  210. int ret;
  211. size_t read_length;
  212. const void *read_data = NULL;
  213. int64_t dts;
  214. pa_usec_t latency;
  215. int negative;
  216. pa_threaded_mainloop_lock(pd->mainloop);
  217. CHECK_DEAD_GOTO(pd, ret, unlock_and_fail);
  218. while (!read_data) {
  219. int r;
  220. r = pa_stream_peek(pd->stream, &read_data, &read_length);
  221. CHECK_SUCCESS_GOTO(ret, r == 0, unlock_and_fail);
  222. if (read_length <= 0) {
  223. pa_threaded_mainloop_wait(pd->mainloop);
  224. CHECK_DEAD_GOTO(pd, ret, unlock_and_fail);
  225. } else if (!read_data) {
  226. /* There's a hole in the stream, skip it. We could generate
  227. * silence, but that wouldn't work for compressed streams. */
  228. r = pa_stream_drop(pd->stream);
  229. CHECK_SUCCESS_GOTO(ret, r == 0, unlock_and_fail);
  230. }
  231. }
  232. if (av_new_packet(pkt, read_length) < 0) {
  233. ret = AVERROR(ENOMEM);
  234. goto unlock_and_fail;
  235. }
  236. dts = av_gettime();
  237. pa_operation_unref(pa_stream_update_timing_info(pd->stream, NULL, NULL));
  238. if (pa_stream_get_latency(pd->stream, &latency, &negative) >= 0) {
  239. enum AVCodecID codec_id =
  240. s->audio_codec_id == AV_CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id;
  241. int frame_size = ((av_get_bits_per_sample(codec_id) >> 3) * pd->channels);
  242. int frame_duration = read_length / frame_size;
  243. if (negative) {
  244. dts += latency;
  245. } else
  246. dts -= latency;
  247. pkt->pts = ff_timefilter_update(pd->timefilter, dts, pd->last_period);
  248. pd->last_period = frame_duration;
  249. } else {
  250. av_log(s, AV_LOG_WARNING, "pa_stream_get_latency() failed\n");
  251. }
  252. memcpy(pkt->data, read_data, read_length);
  253. pa_stream_drop(pd->stream);
  254. pa_threaded_mainloop_unlock(pd->mainloop);
  255. return 0;
  256. unlock_and_fail:
  257. pa_threaded_mainloop_unlock(pd->mainloop);
  258. return ret;
  259. }
  260. static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
  261. {
  262. PulseData *s = h->priv_data;
  263. return ff_pulse_audio_get_devices(device_list, s->server, 0);
  264. }
  265. #define OFFSET(a) offsetof(PulseData, a)
  266. #define D AV_OPT_FLAG_DECODING_PARAM
  267. static const AVOption options[] = {
  268. { "server", "set PulseAudio server", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, D },
  269. { "name", "set application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT}, 0, 0, D },
  270. { "stream_name", "set stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
  271. { "sample_rate", "set sample rate in Hz", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, D },
  272. { "channels", "set number of audio channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, D },
  273. { "frame_size", "set number of bytes per frame", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, D },
  274. { "fragment_size", "set buffering size, affects latency and cpu usage", OFFSET(fragment_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D },
  275. { NULL },
  276. };
  277. static const AVClass pulse_demuxer_class = {
  278. .class_name = "Pulse demuxer",
  279. .item_name = av_default_item_name,
  280. .option = options,
  281. .version = LIBAVUTIL_VERSION_INT,
  282. .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
  283. };
  284. AVInputFormat ff_pulse_demuxer = {
  285. .name = "pulse",
  286. .long_name = NULL_IF_CONFIG_SMALL("Pulse audio input"),
  287. .priv_data_size = sizeof(PulseData),
  288. .read_header = pulse_read_header,
  289. .read_packet = pulse_read_packet,
  290. .read_close = pulse_close,
  291. .get_device_list = pulse_get_device_list,
  292. .flags = AVFMT_NOFILE,
  293. .priv_class = &pulse_demuxer_class,
  294. };