pulse_audio_dec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. pa_channel_map cmap;
  125. pa_channel_map_init_extend(&cmap, pd->channels, PA_CHANNEL_MAP_WAVEEX);
  126. st = avformat_new_stream(s, NULL);
  127. if (!st) {
  128. av_log(s, AV_LOG_ERROR, "Cannot add stream\n");
  129. return AVERROR(ENOMEM);
  130. }
  131. attr.fragsize = pd->fragment_size;
  132. if (s->url[0] != '\0' && strcmp(s->url, "default"))
  133. device = s->url;
  134. if (!(pd->mainloop = pa_threaded_mainloop_new())) {
  135. pulse_close(s);
  136. return AVERROR_EXTERNAL;
  137. }
  138. if (!(pd->context = pa_context_new(pa_threaded_mainloop_get_api(pd->mainloop), pd->name))) {
  139. pulse_close(s);
  140. return AVERROR_EXTERNAL;
  141. }
  142. pa_context_set_state_callback(pd->context, context_state_cb, pd);
  143. if (pa_context_connect(pd->context, pd->server, 0, NULL) < 0) {
  144. pulse_close(s);
  145. return AVERROR(pa_context_errno(pd->context));
  146. }
  147. pa_threaded_mainloop_lock(pd->mainloop);
  148. if (pa_threaded_mainloop_start(pd->mainloop) < 0) {
  149. ret = -1;
  150. goto unlock_and_fail;
  151. }
  152. for (;;) {
  153. pa_context_state_t state;
  154. state = pa_context_get_state(pd->context);
  155. if (state == PA_CONTEXT_READY)
  156. break;
  157. if (!PA_CONTEXT_IS_GOOD(state)) {
  158. ret = AVERROR(pa_context_errno(pd->context));
  159. goto unlock_and_fail;
  160. }
  161. /* Wait until the context is ready */
  162. pa_threaded_mainloop_wait(pd->mainloop);
  163. }
  164. if (!(pd->stream = pa_stream_new(pd->context, pd->stream_name, &ss, &cmap))) {
  165. ret = AVERROR(pa_context_errno(pd->context));
  166. goto unlock_and_fail;
  167. }
  168. pa_stream_set_state_callback(pd->stream, stream_state_cb, pd);
  169. pa_stream_set_read_callback(pd->stream, stream_request_cb, pd);
  170. pa_stream_set_write_callback(pd->stream, stream_request_cb, pd);
  171. pa_stream_set_latency_update_callback(pd->stream, stream_latency_update_cb, pd);
  172. ret = pa_stream_connect_record(pd->stream, device, &attr,
  173. PA_STREAM_INTERPOLATE_TIMING
  174. |PA_STREAM_ADJUST_LATENCY
  175. |PA_STREAM_AUTO_TIMING_UPDATE);
  176. if (ret < 0) {
  177. ret = AVERROR(pa_context_errno(pd->context));
  178. goto unlock_and_fail;
  179. }
  180. for (;;) {
  181. pa_stream_state_t state;
  182. state = pa_stream_get_state(pd->stream);
  183. if (state == PA_STREAM_READY)
  184. break;
  185. if (!PA_STREAM_IS_GOOD(state)) {
  186. ret = AVERROR(pa_context_errno(pd->context));
  187. goto unlock_and_fail;
  188. }
  189. /* Wait until the stream is ready */
  190. pa_threaded_mainloop_wait(pd->mainloop);
  191. }
  192. pa_threaded_mainloop_unlock(pd->mainloop);
  193. /* take real parameters */
  194. st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  195. st->codecpar->codec_id = codec_id;
  196. st->codecpar->sample_rate = pd->sample_rate;
  197. st->codecpar->channels = pd->channels;
  198. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  199. pd->timefilter = ff_timefilter_new(1000000.0 / pd->sample_rate,
  200. 1000, 1.5E-6);
  201. if (!pd->timefilter) {
  202. pulse_close(s);
  203. return AVERROR(ENOMEM);
  204. }
  205. return 0;
  206. unlock_and_fail:
  207. pa_threaded_mainloop_unlock(pd->mainloop);
  208. pulse_close(s);
  209. return ret;
  210. }
  211. static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
  212. {
  213. PulseData *pd = s->priv_data;
  214. int ret;
  215. size_t read_length;
  216. const void *read_data = NULL;
  217. int64_t dts;
  218. pa_usec_t latency;
  219. int negative;
  220. pa_threaded_mainloop_lock(pd->mainloop);
  221. CHECK_DEAD_GOTO(pd, ret, unlock_and_fail);
  222. while (!read_data) {
  223. int r;
  224. r = pa_stream_peek(pd->stream, &read_data, &read_length);
  225. CHECK_SUCCESS_GOTO(ret, r == 0, unlock_and_fail);
  226. if (read_length <= 0) {
  227. pa_threaded_mainloop_wait(pd->mainloop);
  228. CHECK_DEAD_GOTO(pd, ret, unlock_and_fail);
  229. } else if (!read_data) {
  230. /* There's a hole in the stream, skip it. We could generate
  231. * silence, but that wouldn't work for compressed streams. */
  232. r = pa_stream_drop(pd->stream);
  233. CHECK_SUCCESS_GOTO(ret, r == 0, unlock_and_fail);
  234. }
  235. }
  236. if (av_new_packet(pkt, read_length) < 0) {
  237. ret = AVERROR(ENOMEM);
  238. goto unlock_and_fail;
  239. }
  240. dts = av_gettime();
  241. pa_operation_unref(pa_stream_update_timing_info(pd->stream, NULL, NULL));
  242. if (pa_stream_get_latency(pd->stream, &latency, &negative) >= 0) {
  243. enum AVCodecID codec_id =
  244. s->audio_codec_id == AV_CODEC_ID_NONE ? DEFAULT_CODEC_ID : s->audio_codec_id;
  245. int frame_size = ((av_get_bits_per_sample(codec_id) >> 3) * pd->channels);
  246. int frame_duration = read_length / frame_size;
  247. if (negative) {
  248. dts += latency;
  249. } else
  250. dts -= latency;
  251. if (pd->wallclock)
  252. pkt->pts = ff_timefilter_update(pd->timefilter, dts, pd->last_period);
  253. pd->last_period = frame_duration;
  254. } else {
  255. av_log(s, AV_LOG_WARNING, "pa_stream_get_latency() failed\n");
  256. }
  257. memcpy(pkt->data, read_data, read_length);
  258. pa_stream_drop(pd->stream);
  259. pa_threaded_mainloop_unlock(pd->mainloop);
  260. return 0;
  261. unlock_and_fail:
  262. pa_threaded_mainloop_unlock(pd->mainloop);
  263. return ret;
  264. }
  265. static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
  266. {
  267. PulseData *s = h->priv_data;
  268. return ff_pulse_audio_get_devices(device_list, s->server, 0);
  269. }
  270. #define OFFSET(a) offsetof(PulseData, a)
  271. #define D AV_OPT_FLAG_DECODING_PARAM
  272. static const AVOption options[] = {
  273. { "server", "set PulseAudio server", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, D },
  274. { "name", "set application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT}, 0, 0, D },
  275. { "stream_name", "set stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
  276. { "sample_rate", "set sample rate in Hz", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, D },
  277. { "channels", "set number of audio channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, D },
  278. { "frame_size", "set number of bytes per frame", OFFSET(frame_size), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, D },
  279. { "fragment_size", "set buffering size, affects latency and cpu usage", OFFSET(fragment_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, D },
  280. { "wallclock", "set the initial pts using the current time", OFFSET(wallclock), AV_OPT_TYPE_INT, {.i64 = 1}, -1, 1, D },
  281. { NULL },
  282. };
  283. static const AVClass pulse_demuxer_class = {
  284. .class_name = "Pulse indev",
  285. .item_name = av_default_item_name,
  286. .option = options,
  287. .version = LIBAVUTIL_VERSION_INT,
  288. .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_INPUT,
  289. };
  290. AVInputFormat ff_pulse_demuxer = {
  291. .name = "pulse",
  292. .long_name = NULL_IF_CONFIG_SMALL("Pulse audio input"),
  293. .priv_data_size = sizeof(PulseData),
  294. .read_header = pulse_read_header,
  295. .read_packet = pulse_read_packet,
  296. .read_close = pulse_close,
  297. .get_device_list = pulse_get_device_list,
  298. .flags = AVFMT_NOFILE,
  299. .priv_class = &pulse_demuxer_class,
  300. };