pulse_audio_enc.c 30 KB

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