pulse_audio_enc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /*
  2. * Copyright (c) 2013 Lukasz Marek <lukasz.m.luki@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <math.h>
  21. #include <pulse/pulseaudio.h>
  22. #include <pulse/error.h>
  23. #include "libavformat/avformat.h"
  24. #include "libavformat/internal.h"
  25. #include "libavutil/internal.h"
  26. #include "libavutil/opt.h"
  27. #include "libavutil/time.h"
  28. #include "libavutil/log.h"
  29. #include "libavutil/attributes.h"
  30. #include "pulse_audio_common.h"
  31. typedef struct PulseData {
  32. AVClass *class;
  33. const char *server;
  34. const char *name;
  35. const char *stream_name;
  36. const char *device;
  37. int64_t timestamp;
  38. int buffer_size; /**< Buffer size in bytes */
  39. int buffer_duration; /**< Buffer size in ms, recalculated to buffer_size */
  40. int prebuf;
  41. int minreq;
  42. int last_result;
  43. pa_threaded_mainloop *mainloop;
  44. pa_context *ctx;
  45. pa_stream *stream;
  46. int nonblocking;
  47. int mute;
  48. pa_volume_t base_volume;
  49. pa_volume_t last_volume;
  50. } PulseData;
  51. static void pulse_audio_sink_device_cb(pa_context *ctx, const pa_sink_info *dev,
  52. int eol, void *userdata)
  53. {
  54. PulseData *s = userdata;
  55. if (s->ctx != ctx)
  56. return;
  57. if (eol) {
  58. pa_threaded_mainloop_signal(s->mainloop, 0);
  59. } else {
  60. if (dev->flags & PA_SINK_FLAT_VOLUME)
  61. s->base_volume = dev->base_volume;
  62. else
  63. s->base_volume = PA_VOLUME_NORM;
  64. av_log(s, AV_LOG_DEBUG, "base volume: %u\n", s->base_volume);
  65. }
  66. }
  67. /* Mainloop must be locked before calling this function as it uses pa_threaded_mainloop_wait. */
  68. static int pulse_update_sink_info(AVFormatContext *h)
  69. {
  70. PulseData *s = h->priv_data;
  71. pa_operation *op;
  72. if (!(op = pa_context_get_sink_info_by_name(s->ctx, s->device,
  73. pulse_audio_sink_device_cb, s))) {
  74. av_log(s, AV_LOG_ERROR, "pa_context_get_sink_info_by_name failed.\n");
  75. return AVERROR_EXTERNAL;
  76. }
  77. while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
  78. pa_threaded_mainloop_wait(s->mainloop);
  79. pa_operation_unref(op);
  80. return 0;
  81. }
  82. static void pulse_audio_sink_input_cb(pa_context *ctx, const pa_sink_input_info *i,
  83. int eol, void *userdata)
  84. {
  85. AVFormatContext *h = userdata;
  86. PulseData *s = h->priv_data;
  87. if (s->ctx != ctx)
  88. return;
  89. if (!eol) {
  90. double val;
  91. pa_volume_t vol = pa_cvolume_avg(&i->volume);
  92. if (s->mute < 0 || (s->mute && !i->mute) || (!s->mute && i->mute)) {
  93. s->mute = i->mute;
  94. avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_MUTE_STATE_CHANGED, &s->mute, sizeof(s->mute));
  95. }
  96. vol = pa_sw_volume_divide(vol, s->base_volume);
  97. if (s->last_volume != vol) {
  98. val = (double)vol / PA_VOLUME_NORM;
  99. avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_VOLUME_LEVEL_CHANGED, &val, sizeof(val));
  100. s->last_volume = vol;
  101. }
  102. }
  103. }
  104. /* This function creates new loop so may be called from PA callbacks.
  105. Mainloop must be locked before calling this function as it operates on streams. */
  106. static int pulse_update_sink_input_info(AVFormatContext *h)
  107. {
  108. PulseData *s = h->priv_data;
  109. pa_operation *op;
  110. enum pa_operation_state op_state;
  111. pa_mainloop *ml = NULL;
  112. pa_context *ctx = NULL;
  113. int ret = 0;
  114. if ((ret = ff_pulse_audio_connect_context(&ml, &ctx, s->server, "Update sink input information")) < 0)
  115. return ret;
  116. if (!(op = pa_context_get_sink_input_info(ctx, pa_stream_get_index(s->stream),
  117. pulse_audio_sink_input_cb, h))) {
  118. ret = AVERROR_EXTERNAL;
  119. goto fail;
  120. }
  121. while ((op_state = pa_operation_get_state(op)) == PA_OPERATION_RUNNING)
  122. pa_mainloop_iterate(ml, 1, NULL);
  123. pa_operation_unref(op);
  124. if (op_state != PA_OPERATION_DONE) {
  125. ret = AVERROR_EXTERNAL;
  126. goto fail;
  127. }
  128. fail:
  129. ff_pulse_audio_disconnect_context(&ml, &ctx);
  130. if (ret)
  131. av_log(s, AV_LOG_ERROR, "pa_context_get_sink_input_info failed.\n");
  132. return ret;
  133. }
  134. static void pulse_event(pa_context *ctx, pa_subscription_event_type_t t,
  135. uint32_t idx, void *userdata)
  136. {
  137. AVFormatContext *h = userdata;
  138. PulseData *s = h->priv_data;
  139. if (s->ctx != ctx)
  140. return;
  141. if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) {
  142. if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE)
  143. // Calling from mainloop callback. No need to lock mainloop.
  144. pulse_update_sink_input_info(h);
  145. }
  146. }
  147. static void pulse_stream_writable(pa_stream *stream, size_t nbytes, void *userdata)
  148. {
  149. AVFormatContext *h = userdata;
  150. PulseData *s = h->priv_data;
  151. int64_t val = nbytes;
  152. if (stream != s->stream)
  153. return;
  154. avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_BUFFER_WRITABLE, &val, sizeof(val));
  155. pa_threaded_mainloop_signal(s->mainloop, 0);
  156. }
  157. static void pulse_overflow(pa_stream *stream, void *userdata)
  158. {
  159. AVFormatContext *h = userdata;
  160. avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_BUFFER_OVERFLOW, NULL, 0);
  161. }
  162. static void pulse_underflow(pa_stream *stream, void *userdata)
  163. {
  164. AVFormatContext *h = userdata;
  165. avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_BUFFER_UNDERFLOW, NULL, 0);
  166. }
  167. static void pulse_stream_state(pa_stream *stream, void *userdata)
  168. {
  169. PulseData *s = userdata;
  170. if (stream != s->stream)
  171. return;
  172. switch (pa_stream_get_state(s->stream)) {
  173. case PA_STREAM_READY:
  174. case PA_STREAM_FAILED:
  175. case PA_STREAM_TERMINATED:
  176. pa_threaded_mainloop_signal(s->mainloop, 0);
  177. default:
  178. break;
  179. }
  180. }
  181. static int pulse_stream_wait(PulseData *s)
  182. {
  183. pa_stream_state_t state;
  184. while ((state = pa_stream_get_state(s->stream)) != PA_STREAM_READY) {
  185. if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED)
  186. return AVERROR_EXTERNAL;
  187. pa_threaded_mainloop_wait(s->mainloop);
  188. }
  189. return 0;
  190. }
  191. static void pulse_context_state(pa_context *ctx, void *userdata)
  192. {
  193. PulseData *s = userdata;
  194. if (s->ctx != ctx)
  195. return;
  196. switch (pa_context_get_state(ctx)) {
  197. case PA_CONTEXT_READY:
  198. case PA_CONTEXT_FAILED:
  199. case PA_CONTEXT_TERMINATED:
  200. pa_threaded_mainloop_signal(s->mainloop, 0);
  201. default:
  202. break;
  203. }
  204. }
  205. static int pulse_context_wait(PulseData *s)
  206. {
  207. pa_context_state_t state;
  208. while ((state = pa_context_get_state(s->ctx)) != PA_CONTEXT_READY) {
  209. if (state == PA_CONTEXT_FAILED || state == PA_CONTEXT_TERMINATED)
  210. return AVERROR_EXTERNAL;
  211. pa_threaded_mainloop_wait(s->mainloop);
  212. }
  213. return 0;
  214. }
  215. static void pulse_stream_result(pa_stream *stream, int success, void *userdata)
  216. {
  217. PulseData *s = userdata;
  218. if (stream != s->stream)
  219. return;
  220. s->last_result = success ? 0 : AVERROR_EXTERNAL;
  221. pa_threaded_mainloop_signal(s->mainloop, 0);
  222. }
  223. static int pulse_finish_stream_operation(PulseData *s, pa_operation *op, const char *name)
  224. {
  225. if (!op) {
  226. pa_threaded_mainloop_unlock(s->mainloop);
  227. av_log(s, AV_LOG_ERROR, "%s failed.\n", name);
  228. return AVERROR_EXTERNAL;
  229. }
  230. s->last_result = 2;
  231. while (s->last_result == 2)
  232. pa_threaded_mainloop_wait(s->mainloop);
  233. pa_operation_unref(op);
  234. pa_threaded_mainloop_unlock(s->mainloop);
  235. if (s->last_result != 0)
  236. av_log(s, AV_LOG_ERROR, "%s failed.\n", name);
  237. return s->last_result;
  238. }
  239. static int pulse_set_pause(PulseData *s, int pause)
  240. {
  241. pa_operation *op;
  242. pa_threaded_mainloop_lock(s->mainloop);
  243. op = pa_stream_cork(s->stream, pause, pulse_stream_result, s);
  244. return pulse_finish_stream_operation(s, op, "pa_stream_cork");
  245. }
  246. static int pulse_flash_stream(PulseData *s)
  247. {
  248. pa_operation *op;
  249. pa_threaded_mainloop_lock(s->mainloop);
  250. op = pa_stream_flush(s->stream, pulse_stream_result, s);
  251. return pulse_finish_stream_operation(s, op, "pa_stream_flush");
  252. }
  253. static void pulse_context_result(pa_context *ctx, int success, void *userdata)
  254. {
  255. PulseData *s = userdata;
  256. if (s->ctx != ctx)
  257. return;
  258. s->last_result = success ? 0 : AVERROR_EXTERNAL;
  259. pa_threaded_mainloop_signal(s->mainloop, 0);
  260. }
  261. static int pulse_finish_context_operation(PulseData *s, pa_operation *op, const char *name)
  262. {
  263. if (!op) {
  264. pa_threaded_mainloop_unlock(s->mainloop);
  265. av_log(s, AV_LOG_ERROR, "%s failed.\n", name);
  266. return AVERROR_EXTERNAL;
  267. }
  268. s->last_result = 2;
  269. while (s->last_result == 2)
  270. pa_threaded_mainloop_wait(s->mainloop);
  271. pa_operation_unref(op);
  272. pa_threaded_mainloop_unlock(s->mainloop);
  273. if (s->last_result != 0)
  274. av_log(s, AV_LOG_ERROR, "%s failed.\n", name);
  275. return s->last_result;
  276. }
  277. static int pulse_set_mute(PulseData *s)
  278. {
  279. pa_operation *op;
  280. pa_threaded_mainloop_lock(s->mainloop);
  281. op = pa_context_set_sink_input_mute(s->ctx, pa_stream_get_index(s->stream),
  282. s->mute, pulse_context_result, s);
  283. return pulse_finish_context_operation(s, op, "pa_context_set_sink_input_mute");
  284. }
  285. static int pulse_set_volume(PulseData *s, double volume)
  286. {
  287. pa_operation *op;
  288. pa_cvolume cvol;
  289. pa_volume_t vol;
  290. const pa_sample_spec *ss = pa_stream_get_sample_spec(s->stream);
  291. vol = pa_sw_volume_multiply(lrint(volume * PA_VOLUME_NORM), s->base_volume);
  292. pa_cvolume_set(&cvol, ss->channels, PA_VOLUME_NORM);
  293. pa_sw_cvolume_multiply_scalar(&cvol, &cvol, vol);
  294. pa_threaded_mainloop_lock(s->mainloop);
  295. op = pa_context_set_sink_input_volume(s->ctx, pa_stream_get_index(s->stream),
  296. &cvol, pulse_context_result, s);
  297. return pulse_finish_context_operation(s, op, "pa_context_set_sink_input_volume");
  298. }
  299. static int pulse_subscribe_events(PulseData *s)
  300. {
  301. pa_operation *op;
  302. pa_threaded_mainloop_lock(s->mainloop);
  303. op = pa_context_subscribe(s->ctx, PA_SUBSCRIPTION_MASK_SINK_INPUT, pulse_context_result, s);
  304. return pulse_finish_context_operation(s, op, "pa_context_subscribe");
  305. }
  306. static void pulse_map_channels_to_pulse(int64_t channel_layout, pa_channel_map *channel_map)
  307. {
  308. channel_map->channels = 0;
  309. if (channel_layout & AV_CH_FRONT_LEFT)
  310. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
  311. if (channel_layout & AV_CH_FRONT_RIGHT)
  312. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
  313. if (channel_layout & AV_CH_FRONT_CENTER)
  314. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
  315. if (channel_layout & AV_CH_LOW_FREQUENCY)
  316. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_LFE;
  317. if (channel_layout & AV_CH_BACK_LEFT)
  318. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
  319. if (channel_layout & AV_CH_BACK_RIGHT)
  320. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
  321. if (channel_layout & AV_CH_FRONT_LEFT_OF_CENTER)
  322. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
  323. if (channel_layout & AV_CH_FRONT_RIGHT_OF_CENTER)
  324. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
  325. if (channel_layout & AV_CH_BACK_CENTER)
  326. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
  327. if (channel_layout & AV_CH_SIDE_LEFT)
  328. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
  329. if (channel_layout & AV_CH_SIDE_RIGHT)
  330. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
  331. if (channel_layout & AV_CH_TOP_CENTER)
  332. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_CENTER;
  333. if (channel_layout & AV_CH_TOP_FRONT_LEFT)
  334. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_LEFT;
  335. if (channel_layout & AV_CH_TOP_FRONT_CENTER)
  336. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_CENTER;
  337. if (channel_layout & AV_CH_TOP_FRONT_RIGHT)
  338. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_RIGHT;
  339. if (channel_layout & AV_CH_TOP_BACK_LEFT)
  340. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_LEFT;
  341. if (channel_layout & AV_CH_TOP_BACK_CENTER)
  342. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_CENTER;
  343. if (channel_layout & AV_CH_TOP_BACK_RIGHT)
  344. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
  345. if (channel_layout & AV_CH_STEREO_LEFT)
  346. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
  347. if (channel_layout & AV_CH_STEREO_RIGHT)
  348. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
  349. if (channel_layout & AV_CH_WIDE_LEFT)
  350. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX0;
  351. if (channel_layout & AV_CH_WIDE_RIGHT)
  352. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX1;
  353. if (channel_layout & AV_CH_SURROUND_DIRECT_LEFT)
  354. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX2;
  355. if (channel_layout & AV_CH_SURROUND_DIRECT_RIGHT)
  356. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX3;
  357. if (channel_layout & AV_CH_LOW_FREQUENCY_2)
  358. channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_LFE;
  359. }
  360. static av_cold int pulse_write_trailer(AVFormatContext *h)
  361. {
  362. PulseData *s = h->priv_data;
  363. if (s->mainloop) {
  364. pa_threaded_mainloop_lock(s->mainloop);
  365. if (s->stream) {
  366. pa_stream_disconnect(s->stream);
  367. pa_stream_set_state_callback(s->stream, NULL, NULL);
  368. pa_stream_set_write_callback(s->stream, NULL, NULL);
  369. pa_stream_set_overflow_callback(s->stream, NULL, NULL);
  370. pa_stream_set_underflow_callback(s->stream, NULL, NULL);
  371. pa_stream_unref(s->stream);
  372. s->stream = NULL;
  373. }
  374. if (s->ctx) {
  375. pa_context_disconnect(s->ctx);
  376. pa_context_set_state_callback(s->ctx, NULL, NULL);
  377. pa_context_set_subscribe_callback(s->ctx, NULL, NULL);
  378. pa_context_unref(s->ctx);
  379. s->ctx = NULL;
  380. }
  381. pa_threaded_mainloop_unlock(s->mainloop);
  382. pa_threaded_mainloop_stop(s->mainloop);
  383. pa_threaded_mainloop_free(s->mainloop);
  384. s->mainloop = NULL;
  385. }
  386. return 0;
  387. }
  388. static av_cold int pulse_write_header(AVFormatContext *h)
  389. {
  390. PulseData *s = h->priv_data;
  391. AVStream *st = NULL;
  392. int ret;
  393. pa_sample_spec sample_spec;
  394. pa_buffer_attr buffer_attributes = { -1, -1, -1, -1, -1 };
  395. pa_channel_map channel_map;
  396. pa_mainloop_api *mainloop_api;
  397. const char *stream_name = s->stream_name;
  398. static const pa_stream_flags_t stream_flags = PA_STREAM_INTERPOLATE_TIMING |
  399. PA_STREAM_AUTO_TIMING_UPDATE |
  400. PA_STREAM_NOT_MONOTONIC;
  401. if (h->nb_streams != 1 || h->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
  402. av_log(s, AV_LOG_ERROR, "Only a single audio stream is supported.\n");
  403. return AVERROR(EINVAL);
  404. }
  405. st = h->streams[0];
  406. if (!stream_name) {
  407. if (h->filename[0])
  408. stream_name = h->filename;
  409. else
  410. stream_name = "Playback";
  411. }
  412. s->nonblocking = (h->flags & AVFMT_FLAG_NONBLOCK);
  413. if (s->buffer_duration) {
  414. int64_t bytes = s->buffer_duration;
  415. bytes *= st->codecpar->channels * st->codecpar->sample_rate *
  416. av_get_bytes_per_sample(st->codecpar->format);
  417. bytes /= 1000;
  418. buffer_attributes.tlength = FFMAX(s->buffer_size, av_clip64(bytes, 0, UINT32_MAX - 1));
  419. av_log(s, AV_LOG_DEBUG,
  420. "Buffer duration: %ums recalculated into %"PRId64" bytes buffer.\n",
  421. s->buffer_duration, bytes);
  422. av_log(s, AV_LOG_DEBUG, "Real buffer length is %u bytes\n", buffer_attributes.tlength);
  423. } else if (s->buffer_size)
  424. buffer_attributes.tlength = s->buffer_size;
  425. if (s->prebuf)
  426. buffer_attributes.prebuf = s->prebuf;
  427. if (s->minreq)
  428. buffer_attributes.minreq = s->minreq;
  429. sample_spec.format = ff_codec_id_to_pulse_format(st->codecpar->codec_id);
  430. sample_spec.rate = st->codecpar->sample_rate;
  431. sample_spec.channels = st->codecpar->channels;
  432. if (!pa_sample_spec_valid(&sample_spec)) {
  433. av_log(s, AV_LOG_ERROR, "Invalid sample spec.\n");
  434. return AVERROR(EINVAL);
  435. }
  436. if (sample_spec.channels == 1) {
  437. channel_map.channels = 1;
  438. channel_map.map[0] = PA_CHANNEL_POSITION_MONO;
  439. } else if (st->codecpar->channel_layout) {
  440. if (av_get_channel_layout_nb_channels(st->codecpar->channel_layout) != st->codecpar->channels)
  441. return AVERROR(EINVAL);
  442. pulse_map_channels_to_pulse(st->codecpar->channel_layout, &channel_map);
  443. /* Unknown channel is present in channel_layout, let PulseAudio use its default. */
  444. if (channel_map.channels != sample_spec.channels) {
  445. av_log(s, AV_LOG_WARNING, "Unknown channel. Using defaul channel map.\n");
  446. channel_map.channels = 0;
  447. }
  448. } else
  449. channel_map.channels = 0;
  450. if (!channel_map.channels)
  451. av_log(s, AV_LOG_WARNING, "Using PulseAudio's default channel map.\n");
  452. else if (!pa_channel_map_valid(&channel_map)) {
  453. av_log(s, AV_LOG_ERROR, "Invalid channel map.\n");
  454. return AVERROR(EINVAL);
  455. }
  456. /* start main loop */
  457. s->mainloop = pa_threaded_mainloop_new();
  458. if (!s->mainloop) {
  459. av_log(s, AV_LOG_ERROR, "Cannot create threaded mainloop.\n");
  460. return AVERROR(ENOMEM);
  461. }
  462. if ((ret = pa_threaded_mainloop_start(s->mainloop)) < 0) {
  463. av_log(s, AV_LOG_ERROR, "Cannot start threaded mainloop: %s.\n", pa_strerror(ret));
  464. pa_threaded_mainloop_free(s->mainloop);
  465. s->mainloop = NULL;
  466. return AVERROR_EXTERNAL;
  467. }
  468. pa_threaded_mainloop_lock(s->mainloop);
  469. mainloop_api = pa_threaded_mainloop_get_api(s->mainloop);
  470. if (!mainloop_api) {
  471. av_log(s, AV_LOG_ERROR, "Cannot get mainloop API.\n");
  472. ret = AVERROR_EXTERNAL;
  473. goto fail;
  474. }
  475. s->ctx = pa_context_new(mainloop_api, s->name);
  476. if (!s->ctx) {
  477. av_log(s, AV_LOG_ERROR, "Cannot create context.\n");
  478. ret = AVERROR(ENOMEM);
  479. goto fail;
  480. }
  481. pa_context_set_state_callback(s->ctx, pulse_context_state, s);
  482. pa_context_set_subscribe_callback(s->ctx, pulse_event, h);
  483. if ((ret = pa_context_connect(s->ctx, s->server, 0, NULL)) < 0) {
  484. av_log(s, AV_LOG_ERROR, "Cannot connect context: %s.\n", pa_strerror(ret));
  485. ret = AVERROR_EXTERNAL;
  486. goto fail;
  487. }
  488. if ((ret = pulse_context_wait(s)) < 0) {
  489. av_log(s, AV_LOG_ERROR, "Context failed.\n");
  490. goto fail;
  491. }
  492. s->stream = pa_stream_new(s->ctx, stream_name, &sample_spec,
  493. channel_map.channels ? &channel_map : NULL);
  494. if ((ret = pulse_update_sink_info(h)) < 0) {
  495. av_log(s, AV_LOG_ERROR, "Updating sink info failed.\n");
  496. goto fail;
  497. }
  498. if (!s->stream) {
  499. av_log(s, AV_LOG_ERROR, "Cannot create stream.\n");
  500. ret = AVERROR(ENOMEM);
  501. goto fail;
  502. }
  503. pa_stream_set_state_callback(s->stream, pulse_stream_state, s);
  504. pa_stream_set_write_callback(s->stream, pulse_stream_writable, h);
  505. pa_stream_set_overflow_callback(s->stream, pulse_overflow, h);
  506. pa_stream_set_underflow_callback(s->stream, pulse_underflow, h);
  507. if ((ret = pa_stream_connect_playback(s->stream, s->device, &buffer_attributes,
  508. stream_flags, NULL, NULL)) < 0) {
  509. av_log(s, AV_LOG_ERROR, "pa_stream_connect_playback failed: %s.\n", pa_strerror(ret));
  510. ret = AVERROR_EXTERNAL;
  511. goto fail;
  512. }
  513. if ((ret = pulse_stream_wait(s)) < 0) {
  514. av_log(s, AV_LOG_ERROR, "Stream failed.\n");
  515. goto fail;
  516. }
  517. /* read back buffer attributes for future use */
  518. buffer_attributes = *pa_stream_get_buffer_attr(s->stream);
  519. s->buffer_size = buffer_attributes.tlength;
  520. s->prebuf = buffer_attributes.prebuf;
  521. s->minreq = buffer_attributes.minreq;
  522. av_log(s, AV_LOG_DEBUG, "Real buffer attributes: size: %d, prebuf: %d, minreq: %d\n",
  523. s->buffer_size, s->prebuf, s->minreq);
  524. pa_threaded_mainloop_unlock(s->mainloop);
  525. if ((ret = pulse_subscribe_events(s)) < 0) {
  526. av_log(s, AV_LOG_ERROR, "Event subscription failed.\n");
  527. /* a bit ugly but the simplest to lock here*/
  528. pa_threaded_mainloop_lock(s->mainloop);
  529. goto fail;
  530. }
  531. /* force control messages */
  532. s->mute = -1;
  533. s->last_volume = PA_VOLUME_INVALID;
  534. pa_threaded_mainloop_lock(s->mainloop);
  535. if ((ret = pulse_update_sink_input_info(h)) < 0) {
  536. av_log(s, AV_LOG_ERROR, "Updating sink input info failed.\n");
  537. goto fail;
  538. }
  539. pa_threaded_mainloop_unlock(s->mainloop);
  540. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  541. return 0;
  542. fail:
  543. pa_threaded_mainloop_unlock(s->mainloop);
  544. pulse_write_trailer(h);
  545. return ret;
  546. }
  547. static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
  548. {
  549. PulseData *s = h->priv_data;
  550. int ret;
  551. int64_t writable_size;
  552. if (!pkt)
  553. return pulse_flash_stream(s);
  554. if (pkt->dts != AV_NOPTS_VALUE)
  555. s->timestamp = pkt->dts;
  556. if (pkt->duration) {
  557. s->timestamp += pkt->duration;
  558. } else {
  559. AVStream *st = h->streams[0];
  560. AVRational r = { 1, st->codecpar->sample_rate };
  561. int64_t samples = pkt->size / (av_get_bytes_per_sample(st->codecpar->format) * st->codecpar->channels);
  562. s->timestamp += av_rescale_q(samples, r, st->time_base);
  563. }
  564. pa_threaded_mainloop_lock(s->mainloop);
  565. if (!PA_STREAM_IS_GOOD(pa_stream_get_state(s->stream))) {
  566. av_log(s, AV_LOG_ERROR, "PulseAudio stream is in invalid state.\n");
  567. goto fail;
  568. }
  569. while (pa_stream_writable_size(s->stream) < s->minreq) {
  570. if (s->nonblocking) {
  571. pa_threaded_mainloop_unlock(s->mainloop);
  572. return AVERROR(EAGAIN);
  573. } else
  574. pa_threaded_mainloop_wait(s->mainloop);
  575. }
  576. if ((ret = pa_stream_write(s->stream, pkt->data, pkt->size, NULL, 0, PA_SEEK_RELATIVE)) < 0) {
  577. av_log(s, AV_LOG_ERROR, "pa_stream_write failed: %s\n", pa_strerror(ret));
  578. goto fail;
  579. }
  580. if ((writable_size = pa_stream_writable_size(s->stream)) >= s->minreq)
  581. avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_BUFFER_WRITABLE, &writable_size, sizeof(writable_size));
  582. pa_threaded_mainloop_unlock(s->mainloop);
  583. return 0;
  584. fail:
  585. pa_threaded_mainloop_unlock(s->mainloop);
  586. return AVERROR_EXTERNAL;
  587. }
  588. static int pulse_write_frame(AVFormatContext *h, int stream_index,
  589. AVFrame **frame, unsigned flags)
  590. {
  591. AVPacket pkt;
  592. /* Planar formats are not supported yet. */
  593. if (flags & AV_WRITE_UNCODED_FRAME_QUERY)
  594. return av_sample_fmt_is_planar(h->streams[stream_index]->codecpar->format) ?
  595. AVERROR(EINVAL) : 0;
  596. pkt.data = (*frame)->data[0];
  597. pkt.size = (*frame)->nb_samples * av_get_bytes_per_sample((*frame)->format) * av_frame_get_channels(*frame);
  598. pkt.dts = (*frame)->pkt_dts;
  599. pkt.duration = av_frame_get_pkt_duration(*frame);
  600. return pulse_write_packet(h, &pkt);
  601. }
  602. static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall)
  603. {
  604. PulseData *s = h->priv_data;
  605. pa_usec_t latency;
  606. int neg;
  607. pa_threaded_mainloop_lock(s->mainloop);
  608. pa_stream_get_latency(s->stream, &latency, &neg);
  609. pa_threaded_mainloop_unlock(s->mainloop);
  610. if (wall)
  611. *wall = av_gettime();
  612. if (dts)
  613. *dts = s->timestamp - (neg ? -latency : latency);
  614. }
  615. static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
  616. {
  617. PulseData *s = h->priv_data;
  618. return ff_pulse_audio_get_devices(device_list, s->server, 1);
  619. }
  620. static int pulse_control_message(AVFormatContext *h, int type,
  621. void *data, size_t data_size)
  622. {
  623. PulseData *s = h->priv_data;
  624. int ret;
  625. switch(type) {
  626. case AV_APP_TO_DEV_PAUSE:
  627. return pulse_set_pause(s, 1);
  628. case AV_APP_TO_DEV_PLAY:
  629. return pulse_set_pause(s, 0);
  630. case AV_APP_TO_DEV_TOGGLE_PAUSE:
  631. return pulse_set_pause(s, !pa_stream_is_corked(s->stream));
  632. case AV_APP_TO_DEV_MUTE:
  633. if (!s->mute) {
  634. s->mute = 1;
  635. return pulse_set_mute(s);
  636. }
  637. return 0;
  638. case AV_APP_TO_DEV_UNMUTE:
  639. if (s->mute) {
  640. s->mute = 0;
  641. return pulse_set_mute(s);
  642. }
  643. return 0;
  644. case AV_APP_TO_DEV_TOGGLE_MUTE:
  645. s->mute = !s->mute;
  646. return pulse_set_mute(s);
  647. case AV_APP_TO_DEV_SET_VOLUME:
  648. return pulse_set_volume(s, *(double *)data);
  649. case AV_APP_TO_DEV_GET_VOLUME:
  650. s->last_volume = PA_VOLUME_INVALID;
  651. pa_threaded_mainloop_lock(s->mainloop);
  652. ret = pulse_update_sink_input_info(h);
  653. pa_threaded_mainloop_unlock(s->mainloop);
  654. return ret;
  655. case AV_APP_TO_DEV_GET_MUTE:
  656. s->mute = -1;
  657. pa_threaded_mainloop_lock(s->mainloop);
  658. ret = pulse_update_sink_input_info(h);
  659. pa_threaded_mainloop_unlock(s->mainloop);
  660. return ret;
  661. default:
  662. break;
  663. }
  664. return AVERROR(ENOSYS);
  665. }
  666. #define OFFSET(a) offsetof(PulseData, a)
  667. #define E AV_OPT_FLAG_ENCODING_PARAM
  668. static const AVOption options[] = {
  669. { "server", "set PulseAudio server", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  670. { "name", "set application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT}, 0, 0, E },
  671. { "stream_name", "set stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  672. { "device", "set device name", OFFSET(device), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  673. { "buffer_size", "set buffer size in bytes", OFFSET(buffer_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  674. { "buffer_duration", "set buffer duration in millisecs", OFFSET(buffer_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  675. { "prebuf", "set pre-buffering size", OFFSET(prebuf), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  676. { "minreq", "set minimum request size", OFFSET(minreq), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  677. { NULL }
  678. };
  679. static const AVClass pulse_muxer_class = {
  680. .class_name = "PulseAudio muxer",
  681. .item_name = av_default_item_name,
  682. .option = options,
  683. .version = LIBAVUTIL_VERSION_INT,
  684. .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT,
  685. };
  686. AVOutputFormat ff_pulse_muxer = {
  687. .name = "pulse",
  688. .long_name = NULL_IF_CONFIG_SMALL("Pulse audio output"),
  689. .priv_data_size = sizeof(PulseData),
  690. .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
  691. .video_codec = AV_CODEC_ID_NONE,
  692. .write_header = pulse_write_header,
  693. .write_packet = pulse_write_packet,
  694. .write_uncoded_frame = pulse_write_frame,
  695. .write_trailer = pulse_write_trailer,
  696. .get_output_timestamp = pulse_get_output_timestamp,
  697. .get_device_list = pulse_get_device_list,
  698. .control_message = pulse_control_message,
  699. .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
  700. .priv_class = &pulse_muxer_class,
  701. };