pulse_audio_dec.c 12 KB

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