pulse_audio_dec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. int wallclock;
  47. } PulseData;
  48. #define CHECK_SUCCESS_GOTO(rerror, expression, label) \
  49. do { \
  50. if (!(expression)) { \
  51. rerror = AVERROR_EXTERNAL; \
  52. goto label; \
  53. } \
  54. } while (0)
  55. #define CHECK_DEAD_GOTO(p, rerror, label) \
  56. do { \
  57. if (!(p)->context || !PA_CONTEXT_IS_GOOD(pa_context_get_state((p)->context)) || \
  58. !(p)->stream || !PA_STREAM_IS_GOOD(pa_stream_get_state((p)->stream))) { \
  59. rerror = AVERROR_EXTERNAL; \
  60. goto label; \
  61. } \
  62. } while (0)
  63. static void context_state_cb(pa_context *c, void *userdata) {
  64. PulseData *p = userdata;
  65. switch (pa_context_get_state(c)) {
  66. case PA_CONTEXT_READY:
  67. case PA_CONTEXT_TERMINATED:
  68. case PA_CONTEXT_FAILED:
  69. pa_threaded_mainloop_signal(p->mainloop, 0);
  70. break;
  71. }
  72. }
  73. static void stream_state_cb(pa_stream *s, void * userdata) {
  74. PulseData *p = userdata;
  75. switch (pa_stream_get_state(s)) {
  76. case PA_STREAM_READY:
  77. case PA_STREAM_FAILED:
  78. case PA_STREAM_TERMINATED:
  79. pa_threaded_mainloop_signal(p->mainloop, 0);
  80. break;
  81. }
  82. }
  83. static void stream_request_cb(pa_stream *s, size_t length, void *userdata) {
  84. PulseData *p = userdata;
  85. pa_threaded_mainloop_signal(p->mainloop, 0);
  86. }
  87. static void stream_latency_update_cb(pa_stream *s, void *userdata) {
  88. PulseData *p = userdata;
  89. pa_threaded_mainloop_signal(p->mainloop, 0);
  90. }
  91. static av_cold int pulse_close(AVFormatContext *s)
  92. {
  93. PulseData *pd = s->priv_data;
  94. if (pd->mainloop)
  95. pa_threaded_mainloop_stop(pd->mainloop);
  96. if (pd->stream)
  97. pa_stream_unref(pd->stream);
  98. pd->stream = NULL;
  99. if (pd->context) {
  100. pa_context_disconnect(pd->context);
  101. pa_context_unref(pd->context);
  102. }
  103. pd->context = NULL;
  104. if (pd->mainloop)
  105. pa_threaded_mainloop_free(pd->mainloop);
  106. pd->mainloop = NULL;
  107. ff_timefilter_destroy(pd->timefilter);
  108. pd->timefilter = NULL;
  109. return 0;
  110. }
  111. static av_cold int pulse_read_header(AVFormatContext *s)
  112. {
  113. PulseData *pd = s->priv_data;
  114. AVStream *st;
  115. char *device = NULL;
  116. int ret;
  117. enum AVCodecID codec_id =
  118. s->audio_codec_id == AV_CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id;
  119. const pa_sample_spec ss = { ff_codec_id_to_pulse_format(codec_id),
  120. pd->sample_rate,
  121. pd->channels };
  122. pa_buffer_attr attr = { -1 };
  123. st = avformat_new_stream(s, NULL);
  124. if (!st) {
  125. av_log(s, AV_LOG_ERROR, "Cannot add stream\n");
  126. return AVERROR(ENOMEM);
  127. }
  128. attr.fragsize = pd->fragment_size;
  129. if (s->filename[0] != '\0' && strcmp(s->filename, "default"))
  130. device = s->filename;
  131. if (!(pd->mainloop = pa_threaded_mainloop_new())) {
  132. pulse_close(s);
  133. return AVERROR_EXTERNAL;
  134. }
  135. if (!(pd->context = pa_context_new(pa_threaded_mainloop_get_api(pd->mainloop), pd->name))) {
  136. pulse_close(s);
  137. return AVERROR_EXTERNAL;
  138. }
  139. pa_context_set_state_callback(pd->context, context_state_cb, pd);
  140. if (pa_context_connect(pd->context, pd->server, 0, NULL) < 0) {
  141. pulse_close(s);
  142. return AVERROR(pa_context_errno(pd->context));
  143. }
  144. pa_threaded_mainloop_lock(pd->mainloop);
  145. if (pa_threaded_mainloop_start(pd->mainloop) < 0) {
  146. ret = -1;
  147. goto unlock_and_fail;
  148. }
  149. for (;;) {
  150. pa_context_state_t state;
  151. state = pa_context_get_state(pd->context);
  152. if (state == PA_CONTEXT_READY)
  153. break;
  154. if (!PA_CONTEXT_IS_GOOD(state)) {
  155. ret = AVERROR(pa_context_errno(pd->context));
  156. goto unlock_and_fail;
  157. }
  158. /* Wait until the context is ready */
  159. pa_threaded_mainloop_wait(pd->mainloop);
  160. }
  161. if (!(pd->stream = pa_stream_new(pd->context, pd->stream_name, &ss, NULL))) {
  162. ret = AVERROR(pa_context_errno(pd->context));
  163. goto unlock_and_fail;
  164. }
  165. pa_stream_set_state_callback(pd->stream, stream_state_cb, pd);
  166. pa_stream_set_read_callback(pd->stream, stream_request_cb, pd);
  167. pa_stream_set_write_callback(pd->stream, stream_request_cb, pd);
  168. pa_stream_set_latency_update_callback(pd->stream, stream_latency_update_cb, pd);
  169. ret = pa_stream_connect_record(pd->stream, device, &attr,
  170. PA_STREAM_INTERPOLATE_TIMING
  171. |PA_STREAM_ADJUST_LATENCY
  172. |PA_STREAM_AUTO_TIMING_UPDATE);
  173. if (ret < 0) {
  174. ret = AVERROR(pa_context_errno(pd->context));
  175. goto unlock_and_fail;
  176. }
  177. for (;;) {
  178. pa_stream_state_t state;
  179. state = pa_stream_get_state(pd->stream);
  180. if (state == PA_STREAM_READY)
  181. break;
  182. if (!PA_STREAM_IS_GOOD(state)) {
  183. ret = AVERROR(pa_context_errno(pd->context));
  184. goto unlock_and_fail;
  185. }
  186. /* Wait until the stream is ready */
  187. pa_threaded_mainloop_wait(pd->mainloop);
  188. }
  189. pa_threaded_mainloop_unlock(pd->mainloop);
  190. /* take real parameters */
  191. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  192. st->codec->codec_id = codec_id;
  193. st->codec->sample_rate = pd->sample_rate;
  194. st->codec->channels = pd->channels;
  195. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  196. pd->timefilter = ff_timefilter_new(1000000.0 / pd->sample_rate,
  197. 1000, 1.5E-6);
  198. if (!pd->timefilter) {
  199. pulse_close(s);
  200. return AVERROR(ENOMEM);
  201. }
  202. return 0;
  203. unlock_and_fail:
  204. pa_threaded_mainloop_unlock(pd->mainloop);
  205. pulse_close(s);
  206. return ret;
  207. }
  208. static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
  209. {
  210. PulseData *pd = s->priv_data;
  211. int ret;
  212. size_t read_length;
  213. const void *read_data = NULL;
  214. int64_t dts;
  215. pa_usec_t latency;
  216. int negative;
  217. pa_threaded_mainloop_lock(pd->mainloop);
  218. CHECK_DEAD_GOTO(pd, ret, unlock_and_fail);
  219. while (!read_data) {
  220. int r;
  221. r = pa_stream_peek(pd->stream, &read_data, &read_length);
  222. CHECK_SUCCESS_GOTO(ret, r == 0, unlock_and_fail);
  223. if (read_length <= 0) {
  224. pa_threaded_mainloop_wait(pd->mainloop);
  225. CHECK_DEAD_GOTO(pd, ret, unlock_and_fail);
  226. } else if (!read_data) {
  227. /* There's a hole in the stream, skip it. We could generate
  228. * silence, but that wouldn't work for compressed streams. */
  229. r = pa_stream_drop(pd->stream);
  230. CHECK_SUCCESS_GOTO(ret, r == 0, unlock_and_fail);
  231. }
  232. }
  233. if (av_new_packet(pkt, read_length) < 0) {
  234. ret = AVERROR(ENOMEM);
  235. goto unlock_and_fail;
  236. }
  237. dts = av_gettime();
  238. pa_operation_unref(pa_stream_update_timing_info(pd->stream, NULL, NULL));
  239. if (pa_stream_get_latency(pd->stream, &latency, &negative) >= 0) {
  240. enum AVCodecID codec_id =
  241. s->audio_codec_id == AV_CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id;
  242. int frame_size = ((av_get_bits_per_sample(codec_id) >> 3) * pd->channels);
  243. int frame_duration = read_length / frame_size;
  244. if (negative) {
  245. dts += latency;
  246. } else
  247. dts -= latency;
  248. if (pd->wallclock)
  249. pkt->pts = ff_timefilter_update(pd->timefilter, dts, pd->last_period);
  250. pd->last_period = frame_duration;
  251. } else {
  252. av_log(s, AV_LOG_WARNING, "pa_stream_get_latency() failed\n");
  253. }
  254. memcpy(pkt->data, read_data, read_length);
  255. pa_stream_drop(pd->stream);
  256. pa_threaded_mainloop_unlock(pd->mainloop);
  257. return 0;
  258. unlock_and_fail:
  259. pa_threaded_mainloop_unlock(pd->mainloop);
  260. return ret;
  261. }
  262. static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
  263. {
  264. PulseData *s = h->priv_data;
  265. return ff_pulse_audio_get_devices(device_list, s->server, 0);
  266. }
  267. #define OFFSET(a) offsetof(PulseData, a)
  268. #define D AV_OPT_FLAG_DECODING_PARAM
  269. static const AVOption options[] = {
  270. { "server", "set PulseAudio server", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, D },
  271. { "name", "set application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT}, 0, 0, D },
  272. { "stream_name", "set stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
  273. { "sample_rate", "set sample rate in Hz", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, D },
  274. { "channels", "set number of audio channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, D },
  275. { "frame_size", "set number of bytes per frame", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, D },
  276. { "fragment_size", "set buffering size, affects latency and cpu usage", OFFSET(fragment_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D },
  277. { "wallclock", "set the initial pts using the current time", OFFSET(wallclock), AV_OPT_TYPE_INT, {.i64 = 1}, -1, 1, D },
  278. { NULL },
  279. };
  280. static const AVClass pulse_demuxer_class = {
  281. .class_name = "Pulse demuxer",
  282. .item_name = av_default_item_name,
  283. .option = options,
  284. .version = LIBAVUTIL_VERSION_INT,
  285. .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
  286. };
  287. AVInputFormat ff_pulse_demuxer = {
  288. .name = "pulse",
  289. .long_name = NULL_IF_CONFIG_SMALL("Pulse audio input"),
  290. .priv_data_size = sizeof(PulseData),
  291. .read_header = pulse_read_header,
  292. .read_packet = pulse_read_packet,
  293. .read_close = pulse_close,
  294. .get_device_list = pulse_get_device_list,
  295. .flags = AVFMT_NOFILE,
  296. .priv_class = &pulse_demuxer_class,
  297. };