pulse_audio_enc.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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 = av_rescale(s->buffer_duration,
  418. st->codecpar->ch_layout.nb_channels *
  419. (int64_t)st->codecpar->sample_rate *
  420. av_get_bytes_per_sample(st->codecpar->format),
  421. 1000);
  422. buffer_attributes.tlength = FFMAX(s->buffer_size, av_clip64(bytes, 0, UINT32_MAX - 1));
  423. av_log(s, AV_LOG_DEBUG,
  424. "Buffer duration: %ums recalculated into %"PRId64" bytes buffer.\n",
  425. s->buffer_duration, bytes);
  426. av_log(s, AV_LOG_DEBUG, "Real buffer length is %u bytes\n", buffer_attributes.tlength);
  427. } else if (s->buffer_size)
  428. buffer_attributes.tlength = s->buffer_size;
  429. if (s->prebuf)
  430. buffer_attributes.prebuf = s->prebuf;
  431. if (s->minreq)
  432. buffer_attributes.minreq = s->minreq;
  433. sample_spec.format = ff_codec_id_to_pulse_format(st->codecpar->codec_id);
  434. sample_spec.rate = st->codecpar->sample_rate;
  435. sample_spec.channels = st->codecpar->ch_layout.nb_channels;
  436. if (!pa_sample_spec_valid(&sample_spec)) {
  437. av_log(s, AV_LOG_ERROR, "Invalid sample spec.\n");
  438. return AVERROR(EINVAL);
  439. }
  440. if (sample_spec.channels == 1) {
  441. channel_map.channels = 1;
  442. channel_map.map[0] = PA_CHANNEL_POSITION_MONO;
  443. } else if (st->codecpar->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
  444. if (!av_channel_layout_check(&st->codecpar->ch_layout))
  445. return AVERROR(EINVAL);
  446. pulse_map_channels_to_pulse(&st->codecpar->ch_layout, &channel_map);
  447. /* Unknown channel is present in channel_layout, let PulseAudio use its default. */
  448. if (channel_map.channels != sample_spec.channels) {
  449. av_log(s, AV_LOG_WARNING, "Unknown channel. Using defaul channel map.\n");
  450. channel_map.channels = 0;
  451. }
  452. } else
  453. channel_map.channels = 0;
  454. if (!channel_map.channels)
  455. av_log(s, AV_LOG_WARNING, "Using PulseAudio's default channel map.\n");
  456. else if (!pa_channel_map_valid(&channel_map)) {
  457. av_log(s, AV_LOG_ERROR, "Invalid channel map.\n");
  458. return AVERROR(EINVAL);
  459. }
  460. /* start main loop */
  461. s->mainloop = pa_threaded_mainloop_new();
  462. if (!s->mainloop) {
  463. av_log(s, AV_LOG_ERROR, "Cannot create threaded mainloop.\n");
  464. return AVERROR(ENOMEM);
  465. }
  466. if ((ret = pa_threaded_mainloop_start(s->mainloop)) < 0) {
  467. av_log(s, AV_LOG_ERROR, "Cannot start threaded mainloop: %s.\n", pa_strerror(ret));
  468. pa_threaded_mainloop_free(s->mainloop);
  469. s->mainloop = NULL;
  470. return AVERROR_EXTERNAL;
  471. }
  472. pa_threaded_mainloop_lock(s->mainloop);
  473. mainloop_api = pa_threaded_mainloop_get_api(s->mainloop);
  474. if (!mainloop_api) {
  475. av_log(s, AV_LOG_ERROR, "Cannot get mainloop API.\n");
  476. ret = AVERROR_EXTERNAL;
  477. goto fail;
  478. }
  479. s->ctx = pa_context_new(mainloop_api, s->name);
  480. if (!s->ctx) {
  481. av_log(s, AV_LOG_ERROR, "Cannot create context.\n");
  482. ret = AVERROR(ENOMEM);
  483. goto fail;
  484. }
  485. pa_context_set_state_callback(s->ctx, pulse_context_state, s);
  486. pa_context_set_subscribe_callback(s->ctx, pulse_event, h);
  487. if ((ret = pa_context_connect(s->ctx, s->server, 0, NULL)) < 0) {
  488. av_log(s, AV_LOG_ERROR, "Cannot connect context: %s.\n", pa_strerror(ret));
  489. ret = AVERROR_EXTERNAL;
  490. goto fail;
  491. }
  492. if ((ret = pulse_context_wait(s)) < 0) {
  493. av_log(s, AV_LOG_ERROR, "Context failed.\n");
  494. goto fail;
  495. }
  496. s->stream = pa_stream_new(s->ctx, stream_name, &sample_spec,
  497. channel_map.channels ? &channel_map : NULL);
  498. if ((ret = pulse_update_sink_info(h)) < 0) {
  499. av_log(s, AV_LOG_ERROR, "Updating sink info failed.\n");
  500. goto fail;
  501. }
  502. if (!s->stream) {
  503. av_log(s, AV_LOG_ERROR, "Cannot create stream.\n");
  504. ret = AVERROR(ENOMEM);
  505. goto fail;
  506. }
  507. pa_stream_set_state_callback(s->stream, pulse_stream_state, s);
  508. pa_stream_set_write_callback(s->stream, pulse_stream_writable, h);
  509. pa_stream_set_overflow_callback(s->stream, pulse_overflow, h);
  510. pa_stream_set_underflow_callback(s->stream, pulse_underflow, h);
  511. if ((ret = pa_stream_connect_playback(s->stream, s->device, &buffer_attributes,
  512. stream_flags, NULL, NULL)) < 0) {
  513. av_log(s, AV_LOG_ERROR, "pa_stream_connect_playback failed: %s.\n", pa_strerror(ret));
  514. ret = AVERROR_EXTERNAL;
  515. goto fail;
  516. }
  517. if ((ret = pulse_stream_wait(s)) < 0) {
  518. av_log(s, AV_LOG_ERROR, "Stream failed.\n");
  519. goto fail;
  520. }
  521. /* read back buffer attributes for future use */
  522. buffer_attributes = *pa_stream_get_buffer_attr(s->stream);
  523. s->buffer_size = buffer_attributes.tlength;
  524. s->prebuf = buffer_attributes.prebuf;
  525. s->minreq = buffer_attributes.minreq;
  526. av_log(s, AV_LOG_DEBUG, "Real buffer attributes: size: %d, prebuf: %d, minreq: %d\n",
  527. s->buffer_size, s->prebuf, s->minreq);
  528. pa_threaded_mainloop_unlock(s->mainloop);
  529. if ((ret = pulse_subscribe_events(s)) < 0) {
  530. av_log(s, AV_LOG_ERROR, "Event subscription failed.\n");
  531. /* a bit ugly but the simplest to lock here*/
  532. pa_threaded_mainloop_lock(s->mainloop);
  533. goto fail;
  534. }
  535. /* force control messages */
  536. s->mute = -1;
  537. s->last_volume = PA_VOLUME_INVALID;
  538. pa_threaded_mainloop_lock(s->mainloop);
  539. if ((ret = pulse_update_sink_input_info(h)) < 0) {
  540. av_log(s, AV_LOG_ERROR, "Updating sink input info failed.\n");
  541. goto fail;
  542. }
  543. pa_threaded_mainloop_unlock(s->mainloop);
  544. avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
  545. return 0;
  546. fail:
  547. pa_threaded_mainloop_unlock(s->mainloop);
  548. pulse_write_trailer(h);
  549. return ret;
  550. }
  551. static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
  552. {
  553. PulseData *s = h->priv_data;
  554. int ret;
  555. int64_t writable_size;
  556. if (!pkt)
  557. return pulse_flash_stream(s);
  558. if (pkt->dts != AV_NOPTS_VALUE)
  559. s->timestamp = pkt->dts;
  560. if (pkt->duration) {
  561. s->timestamp += pkt->duration;
  562. } else {
  563. AVStream *st = h->streams[0];
  564. AVRational r = { 1, st->codecpar->sample_rate };
  565. int64_t samples = pkt->size / (av_get_bytes_per_sample(st->codecpar->format) * st->codecpar->ch_layout.nb_channels);
  566. s->timestamp += av_rescale_q(samples, r, st->time_base);
  567. }
  568. pa_threaded_mainloop_lock(s->mainloop);
  569. if (!PA_STREAM_IS_GOOD(pa_stream_get_state(s->stream))) {
  570. av_log(s, AV_LOG_ERROR, "PulseAudio stream is in invalid state.\n");
  571. goto fail;
  572. }
  573. while (pa_stream_writable_size(s->stream) < s->minreq) {
  574. if (s->nonblocking) {
  575. pa_threaded_mainloop_unlock(s->mainloop);
  576. return AVERROR(EAGAIN);
  577. } else
  578. pa_threaded_mainloop_wait(s->mainloop);
  579. }
  580. if ((ret = pa_stream_write(s->stream, pkt->data, pkt->size, NULL, 0, PA_SEEK_RELATIVE)) < 0) {
  581. av_log(s, AV_LOG_ERROR, "pa_stream_write failed: %s\n", pa_strerror(ret));
  582. goto fail;
  583. }
  584. if ((writable_size = pa_stream_writable_size(s->stream)) >= s->minreq)
  585. avdevice_dev_to_app_control_message(h, AV_DEV_TO_APP_BUFFER_WRITABLE, &writable_size, sizeof(writable_size));
  586. pa_threaded_mainloop_unlock(s->mainloop);
  587. return 0;
  588. fail:
  589. pa_threaded_mainloop_unlock(s->mainloop);
  590. return AVERROR_EXTERNAL;
  591. }
  592. static int pulse_write_frame(AVFormatContext *h, int stream_index,
  593. AVFrame **frame, unsigned flags)
  594. {
  595. AVPacket pkt;
  596. /* Planar formats are not supported yet. */
  597. if (flags & AV_WRITE_UNCODED_FRAME_QUERY)
  598. return av_sample_fmt_is_planar(h->streams[stream_index]->codecpar->format) ?
  599. AVERROR(EINVAL) : 0;
  600. pkt.data = (*frame)->data[0];
  601. pkt.size = (*frame)->nb_samples * av_get_bytes_per_sample((*frame)->format) * (*frame)->ch_layout.nb_channels;
  602. pkt.dts = (*frame)->pkt_dts;
  603. pkt.duration = (*frame)->pkt_duration;
  604. return pulse_write_packet(h, &pkt);
  605. }
  606. static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall)
  607. {
  608. PulseData *s = h->priv_data;
  609. pa_usec_t latency;
  610. int neg;
  611. pa_threaded_mainloop_lock(s->mainloop);
  612. pa_stream_get_latency(s->stream, &latency, &neg);
  613. pa_threaded_mainloop_unlock(s->mainloop);
  614. if (wall)
  615. *wall = av_gettime();
  616. if (dts)
  617. *dts = s->timestamp - (neg ? -latency : latency);
  618. }
  619. static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
  620. {
  621. PulseData *s = h->priv_data;
  622. return ff_pulse_audio_get_devices(device_list, s->server, 1);
  623. }
  624. static int pulse_control_message(AVFormatContext *h, int type,
  625. void *data, size_t data_size)
  626. {
  627. PulseData *s = h->priv_data;
  628. int ret;
  629. switch(type) {
  630. case AV_APP_TO_DEV_PAUSE:
  631. return pulse_set_pause(s, 1);
  632. case AV_APP_TO_DEV_PLAY:
  633. return pulse_set_pause(s, 0);
  634. case AV_APP_TO_DEV_TOGGLE_PAUSE:
  635. return pulse_set_pause(s, !pa_stream_is_corked(s->stream));
  636. case AV_APP_TO_DEV_MUTE:
  637. if (!s->mute) {
  638. s->mute = 1;
  639. return pulse_set_mute(s);
  640. }
  641. return 0;
  642. case AV_APP_TO_DEV_UNMUTE:
  643. if (s->mute) {
  644. s->mute = 0;
  645. return pulse_set_mute(s);
  646. }
  647. return 0;
  648. case AV_APP_TO_DEV_TOGGLE_MUTE:
  649. s->mute = !s->mute;
  650. return pulse_set_mute(s);
  651. case AV_APP_TO_DEV_SET_VOLUME:
  652. return pulse_set_volume(s, *(double *)data);
  653. case AV_APP_TO_DEV_GET_VOLUME:
  654. s->last_volume = PA_VOLUME_INVALID;
  655. pa_threaded_mainloop_lock(s->mainloop);
  656. ret = pulse_update_sink_input_info(h);
  657. pa_threaded_mainloop_unlock(s->mainloop);
  658. return ret;
  659. case AV_APP_TO_DEV_GET_MUTE:
  660. s->mute = -1;
  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. default:
  666. break;
  667. }
  668. return AVERROR(ENOSYS);
  669. }
  670. #define OFFSET(a) offsetof(PulseData, a)
  671. #define E AV_OPT_FLAG_ENCODING_PARAM
  672. static const AVOption options[] = {
  673. { "server", "set PulseAudio server", OFFSET(server), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  674. { "name", "set application name", OFFSET(name), AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT}, 0, 0, E },
  675. { "stream_name", "set stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  676. { "device", "set device name", OFFSET(device), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  677. { "buffer_size", "set buffer size in bytes", OFFSET(buffer_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  678. { "buffer_duration", "set buffer duration in millisecs", OFFSET(buffer_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  679. { "prebuf", "set pre-buffering size", OFFSET(prebuf), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  680. { "minreq", "set minimum request size", OFFSET(minreq), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
  681. { NULL }
  682. };
  683. static const AVClass pulse_muxer_class = {
  684. .class_name = "PulseAudio outdev",
  685. .item_name = av_default_item_name,
  686. .option = options,
  687. .version = LIBAVUTIL_VERSION_INT,
  688. .category = AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT,
  689. };
  690. const AVOutputFormat ff_pulse_muxer = {
  691. .name = "pulse",
  692. .long_name = NULL_IF_CONFIG_SMALL("Pulse audio output"),
  693. .priv_data_size = sizeof(PulseData),
  694. .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
  695. .video_codec = AV_CODEC_ID_NONE,
  696. .write_header = pulse_write_header,
  697. .write_packet = pulse_write_packet,
  698. .write_uncoded_frame = pulse_write_frame,
  699. .write_trailer = pulse_write_trailer,
  700. .get_output_timestamp = pulse_get_output_timestamp,
  701. .get_device_list = pulse_get_device_list,
  702. .control_message = pulse_control_message,
  703. .flags = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
  704. .priv_class = &pulse_muxer_class,
  705. };