2 * Copyright (c) 2013 Lukasz Marek <lukasz.m.luki@gmail.com>
4 * This file is part of FFmpeg.
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.
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.
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
22 #include <pulse/pulseaudio.h>
23 #include <pulse/error.h>
24 #include "libavformat/avformat.h"
25 #include "libavformat/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"
32 typedef struct PulseData
{
36 const char *stream_name
;
39 int buffer_size
; /**< Buffer size in bytes */
40 int buffer_duration
; /**< Buffer size in ms, recalculated to buffer_size */
44 pa_threaded_mainloop
*mainloop
;
49 pa_volume_t base_volume
;
50 pa_volume_t last_volume
;
53 static void pulse_audio_sink_device_cb(pa_context
*ctx
, const pa_sink_info
*dev
,
54 int eol
, void *userdata
)
56 PulseData
*s
= userdata
;
62 pa_threaded_mainloop_signal(s
->mainloop
, 0);
64 if (dev
->flags
& PA_SINK_FLAT_VOLUME
)
65 s
->base_volume
= dev
->base_volume
;
67 s
->base_volume
= PA_VOLUME_NORM
;
68 av_log(s
, AV_LOG_DEBUG
, "base volume: %u\n", s
->base_volume
);
72 /* Mainloop must be locked before calling this function as it uses pa_threaded_mainloop_wait. */
73 static int pulse_update_sink_info(AVFormatContext
*h
)
75 PulseData
*s
= h
->priv_data
;
77 if (!(op
= pa_context_get_sink_info_by_name(s
->ctx
, s
->device
,
78 pulse_audio_sink_device_cb
, s
))) {
79 av_log(s
, AV_LOG_ERROR
, "pa_context_get_sink_info_by_name failed.\n");
80 return AVERROR_EXTERNAL
;
82 while (pa_operation_get_state(op
) == PA_OPERATION_RUNNING
)
83 pa_threaded_mainloop_wait(s
->mainloop
);
84 pa_operation_unref(op
);
88 static void pulse_audio_sink_input_cb(pa_context
*ctx
, const pa_sink_input_info
*i
,
89 int eol
, void *userdata
)
91 AVFormatContext
*h
= userdata
;
92 PulseData
*s
= h
->priv_data
;
99 pa_volume_t vol
= pa_cvolume_avg(&i
->volume
);
100 if (s
->mute
< 0 || (s
->mute
&& !i
->mute
) || (!s
->mute
&& i
->mute
)) {
102 avdevice_dev_to_app_control_message(h
, AV_DEV_TO_APP_MUTE_STATE_CHANGED
, &s
->mute
, sizeof(s
->mute
));
105 vol
= pa_sw_volume_divide(vol
, s
->base_volume
);
106 if (s
->last_volume
!= vol
) {
107 val
= (double)vol
/ PA_VOLUME_NORM
;
108 avdevice_dev_to_app_control_message(h
, AV_DEV_TO_APP_VOLUME_LEVEL_CHANGED
, &val
, sizeof(val
));
109 s
->last_volume
= vol
;
114 /* This function creates new loop so may be called from PA callbacks.
115 Mainloop must be locked before calling this function as it operates on streams. */
116 static int pulse_update_sink_input_info(AVFormatContext
*h
)
118 PulseData
*s
= h
->priv_data
;
120 enum pa_operation_state op_state
;
121 pa_mainloop
*ml
= NULL
;
122 pa_context
*ctx
= NULL
;
125 if ((ret
= ff_pulse_audio_connect_context(&ml
, &ctx
, s
->server
, "Update sink input information")) < 0)
128 if (!(op
= pa_context_get_sink_input_info(ctx
, pa_stream_get_index(s
->stream
),
129 pulse_audio_sink_input_cb
, h
))) {
130 ret
= AVERROR_EXTERNAL
;
134 while ((op_state
= pa_operation_get_state(op
)) == PA_OPERATION_RUNNING
)
135 pa_mainloop_iterate(ml
, 1, NULL
);
136 pa_operation_unref(op
);
137 if (op_state
!= PA_OPERATION_DONE
) {
138 ret
= AVERROR_EXTERNAL
;
143 ff_pulse_audio_disconnect_context(&ml
, &ctx
);
145 av_log(s
, AV_LOG_ERROR
, "pa_context_get_sink_input_info failed.\n");
149 static void pulse_event(pa_context
*ctx
, pa_subscription_event_type_t t
,
150 uint32_t idx
, void *userdata
)
152 AVFormatContext
*h
= userdata
;
153 PulseData
*s
= h
->priv_data
;
158 if ((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SINK_INPUT
) {
159 if ((t
& PA_SUBSCRIPTION_EVENT_TYPE_MASK
) == PA_SUBSCRIPTION_EVENT_CHANGE
)
160 // Calling from mainloop callback. No need to lock mainloop.
161 pulse_update_sink_input_info(h
);
165 static void pulse_stream_writable(pa_stream
*stream
, size_t nbytes
, void *userdata
)
167 AVFormatContext
*h
= userdata
;
168 PulseData
*s
= h
->priv_data
;
169 int64_t val
= nbytes
;
171 if (stream
!= s
->stream
)
174 avdevice_dev_to_app_control_message(h
, AV_DEV_TO_APP_BUFFER_WRITABLE
, &val
, sizeof(val
));
175 pa_threaded_mainloop_signal(s
->mainloop
, 0);
178 static void pulse_overflow(pa_stream
*stream
, void *userdata
)
180 AVFormatContext
*h
= userdata
;
181 avdevice_dev_to_app_control_message(h
, AV_DEV_TO_APP_BUFFER_OVERFLOW
, NULL
, 0);
184 static void pulse_underflow(pa_stream
*stream
, void *userdata
)
186 AVFormatContext
*h
= userdata
;
187 avdevice_dev_to_app_control_message(h
, AV_DEV_TO_APP_BUFFER_UNDERFLOW
, NULL
, 0);
190 static void pulse_stream_state(pa_stream
*stream
, void *userdata
)
192 PulseData
*s
= userdata
;
194 if (stream
!= s
->stream
)
197 switch (pa_stream_get_state(s
->stream
)) {
198 case PA_STREAM_READY
:
199 case PA_STREAM_FAILED
:
200 case PA_STREAM_TERMINATED
:
201 pa_threaded_mainloop_signal(s
->mainloop
, 0);
207 static int pulse_stream_wait(PulseData
*s
)
209 pa_stream_state_t state
;
211 while ((state
= pa_stream_get_state(s
->stream
)) != PA_STREAM_READY
) {
212 if (state
== PA_STREAM_FAILED
|| state
== PA_STREAM_TERMINATED
)
213 return AVERROR_EXTERNAL
;
214 pa_threaded_mainloop_wait(s
->mainloop
);
219 static void pulse_context_state(pa_context
*ctx
, void *userdata
)
221 PulseData
*s
= userdata
;
226 switch (pa_context_get_state(ctx
)) {
227 case PA_CONTEXT_READY
:
228 case PA_CONTEXT_FAILED
:
229 case PA_CONTEXT_TERMINATED
:
230 pa_threaded_mainloop_signal(s
->mainloop
, 0);
236 static int pulse_context_wait(PulseData
*s
)
238 pa_context_state_t state
;
240 while ((state
= pa_context_get_state(s
->ctx
)) != PA_CONTEXT_READY
) {
241 if (state
== PA_CONTEXT_FAILED
|| state
== PA_CONTEXT_TERMINATED
)
242 return AVERROR_EXTERNAL
;
243 pa_threaded_mainloop_wait(s
->mainloop
);
248 static void pulse_stream_result(pa_stream
*stream
, int success
, void *userdata
)
250 PulseData
*s
= userdata
;
252 if (stream
!= s
->stream
)
255 s
->last_result
= success
? 0 : AVERROR_EXTERNAL
;
256 pa_threaded_mainloop_signal(s
->mainloop
, 0);
259 static int pulse_finish_stream_operation(PulseData
*s
, pa_operation
*op
, const char *name
)
262 pa_threaded_mainloop_unlock(s
->mainloop
);
263 av_log(s
, AV_LOG_ERROR
, "%s failed.\n", name
);
264 return AVERROR_EXTERNAL
;
267 while (s
->last_result
== 2)
268 pa_threaded_mainloop_wait(s
->mainloop
);
269 pa_operation_unref(op
);
270 pa_threaded_mainloop_unlock(s
->mainloop
);
271 if (s
->last_result
!= 0)
272 av_log(s
, AV_LOG_ERROR
, "%s failed.\n", name
);
273 return s
->last_result
;
276 static int pulse_set_pause(PulseData
*s
, int pause
)
279 pa_threaded_mainloop_lock(s
->mainloop
);
280 op
= pa_stream_cork(s
->stream
, pause
, pulse_stream_result
, s
);
281 return pulse_finish_stream_operation(s
, op
, "pa_stream_cork");
284 static int pulse_flash_stream(PulseData
*s
)
287 pa_threaded_mainloop_lock(s
->mainloop
);
288 op
= pa_stream_flush(s
->stream
, pulse_stream_result
, s
);
289 return pulse_finish_stream_operation(s
, op
, "pa_stream_flush");
292 static void pulse_context_result(pa_context
*ctx
, int success
, void *userdata
)
294 PulseData
*s
= userdata
;
299 s
->last_result
= success
? 0 : AVERROR_EXTERNAL
;
300 pa_threaded_mainloop_signal(s
->mainloop
, 0);
303 static int pulse_finish_context_operation(PulseData
*s
, pa_operation
*op
, const char *name
)
306 pa_threaded_mainloop_unlock(s
->mainloop
);
307 av_log(s
, AV_LOG_ERROR
, "%s failed.\n", name
);
308 return AVERROR_EXTERNAL
;
311 while (s
->last_result
== 2)
312 pa_threaded_mainloop_wait(s
->mainloop
);
313 pa_operation_unref(op
);
314 pa_threaded_mainloop_unlock(s
->mainloop
);
315 if (s
->last_result
!= 0)
316 av_log(s
, AV_LOG_ERROR
, "%s failed.\n", name
);
317 return s
->last_result
;
320 static int pulse_set_mute(PulseData
*s
)
323 pa_threaded_mainloop_lock(s
->mainloop
);
324 op
= pa_context_set_sink_input_mute(s
->ctx
, pa_stream_get_index(s
->stream
),
325 s
->mute
, pulse_context_result
, s
);
326 return pulse_finish_context_operation(s
, op
, "pa_context_set_sink_input_mute");
329 static int pulse_set_volume(PulseData
*s
, double volume
)
334 const pa_sample_spec
*ss
= pa_stream_get_sample_spec(s
->stream
);
336 vol
= pa_sw_volume_multiply(lround(volume
* PA_VOLUME_NORM
), s
->base_volume
);
337 pa_cvolume_set(&cvol
, ss
->channels
, PA_VOLUME_NORM
);
338 pa_sw_cvolume_multiply_scalar(&cvol
, &cvol
, vol
);
339 pa_threaded_mainloop_lock(s
->mainloop
);
340 op
= pa_context_set_sink_input_volume(s
->ctx
, pa_stream_get_index(s
->stream
),
341 &cvol
, pulse_context_result
, s
);
342 return pulse_finish_context_operation(s
, op
, "pa_context_set_sink_input_volume");
345 static int pulse_subscribe_events(PulseData
*s
)
349 pa_threaded_mainloop_lock(s
->mainloop
);
350 op
= pa_context_subscribe(s
->ctx
, PA_SUBSCRIPTION_MASK_SINK_INPUT
, pulse_context_result
, s
);
351 return pulse_finish_context_operation(s
, op
, "pa_context_subscribe");
354 static void pulse_map_channels_to_pulse(int64_t channel_layout
, pa_channel_map
*channel_map
)
356 channel_map
->channels
= 0;
357 if (channel_layout
& AV_CH_FRONT_LEFT
)
358 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_FRONT_LEFT
;
359 if (channel_layout
& AV_CH_FRONT_RIGHT
)
360 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_FRONT_RIGHT
;
361 if (channel_layout
& AV_CH_FRONT_CENTER
)
362 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_FRONT_CENTER
;
363 if (channel_layout
& AV_CH_LOW_FREQUENCY
)
364 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_LFE
;
365 if (channel_layout
& AV_CH_BACK_LEFT
)
366 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_REAR_LEFT
;
367 if (channel_layout
& AV_CH_BACK_RIGHT
)
368 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_REAR_RIGHT
;
369 if (channel_layout
& AV_CH_FRONT_LEFT_OF_CENTER
)
370 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
;
371 if (channel_layout
& AV_CH_FRONT_RIGHT_OF_CENTER
)
372 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
;
373 if (channel_layout
& AV_CH_BACK_CENTER
)
374 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_REAR_CENTER
;
375 if (channel_layout
& AV_CH_SIDE_LEFT
)
376 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_SIDE_LEFT
;
377 if (channel_layout
& AV_CH_SIDE_RIGHT
)
378 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_SIDE_RIGHT
;
379 if (channel_layout
& AV_CH_TOP_CENTER
)
380 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_TOP_CENTER
;
381 if (channel_layout
& AV_CH_TOP_FRONT_LEFT
)
382 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_TOP_FRONT_LEFT
;
383 if (channel_layout
& AV_CH_TOP_FRONT_CENTER
)
384 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_TOP_FRONT_CENTER
;
385 if (channel_layout
& AV_CH_TOP_FRONT_RIGHT
)
386 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
;
387 if (channel_layout
& AV_CH_TOP_BACK_LEFT
)
388 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_TOP_REAR_LEFT
;
389 if (channel_layout
& AV_CH_TOP_BACK_CENTER
)
390 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
391 if (channel_layout
& AV_CH_TOP_BACK_RIGHT
)
392 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_TOP_REAR_RIGHT
;
393 if (channel_layout
& AV_CH_STEREO_LEFT
)
394 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_FRONT_LEFT
;
395 if (channel_layout
& AV_CH_STEREO_RIGHT
)
396 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_FRONT_RIGHT
;
397 if (channel_layout
& AV_CH_WIDE_LEFT
)
398 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_AUX0
;
399 if (channel_layout
& AV_CH_WIDE_RIGHT
)
400 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_AUX1
;
401 if (channel_layout
& AV_CH_SURROUND_DIRECT_LEFT
)
402 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_AUX2
;
403 if (channel_layout
& AV_CH_SURROUND_DIRECT_RIGHT
)
404 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_AUX3
;
405 if (channel_layout
& AV_CH_LOW_FREQUENCY_2
)
406 channel_map
->map
[channel_map
->channels
++] = PA_CHANNEL_POSITION_LFE
;
409 static av_cold
int pulse_write_trailer(AVFormatContext
*h
)
411 PulseData
*s
= h
->priv_data
;
414 pa_threaded_mainloop_lock(s
->mainloop
);
416 pa_stream_disconnect(s
->stream
);
417 pa_stream_set_state_callback(s
->stream
, NULL
, NULL
);
418 pa_stream_set_write_callback(s
->stream
, NULL
, NULL
);
419 pa_stream_set_overflow_callback(s
->stream
, NULL
, NULL
);
420 pa_stream_set_underflow_callback(s
->stream
, NULL
, NULL
);
421 pa_stream_unref(s
->stream
);
425 pa_context_disconnect(s
->ctx
);
426 pa_context_set_state_callback(s
->ctx
, NULL
, NULL
);
427 pa_context_set_subscribe_callback(s
->ctx
, NULL
, NULL
);
428 pa_context_unref(s
->ctx
);
431 pa_threaded_mainloop_unlock(s
->mainloop
);
432 pa_threaded_mainloop_stop(s
->mainloop
);
433 pa_threaded_mainloop_free(s
->mainloop
);
440 static av_cold
int pulse_write_header(AVFormatContext
*h
)
442 PulseData
*s
= h
->priv_data
;
445 pa_sample_spec sample_spec
;
446 pa_buffer_attr buffer_attributes
= { -1, -1, -1, -1, -1 };
447 pa_channel_map channel_map
;
448 pa_mainloop_api
*mainloop_api
;
449 const char *stream_name
= s
->stream_name
;
450 static const pa_stream_flags_t stream_flags
= PA_STREAM_INTERPOLATE_TIMING
|
451 PA_STREAM_AUTO_TIMING_UPDATE
|
452 PA_STREAM_NOT_MONOTONIC
;
454 if (h
->nb_streams
!= 1 || h
->streams
[0]->codec
->codec_type
!= AVMEDIA_TYPE_AUDIO
) {
455 av_log(s
, AV_LOG_ERROR
, "Only a single audio stream is supported.\n");
456 return AVERROR(EINVAL
);
462 stream_name
= h
->filename
;
464 stream_name
= "Playback";
466 s
->nonblocking
= (h
->flags
& AVFMT_FLAG_NONBLOCK
);
468 if (s
->buffer_duration
) {
469 int64_t bytes
= s
->buffer_duration
;
470 bytes
*= st
->codec
->channels
* st
->codec
->sample_rate
*
471 av_get_bytes_per_sample(st
->codec
->sample_fmt
);
473 buffer_attributes
.tlength
= FFMAX(s
->buffer_size
, av_clip64(bytes
, 0, UINT32_MAX
- 1));
474 av_log(s
, AV_LOG_DEBUG
,
475 "Buffer duration: %ums recalculated into %"PRId64
" bytes buffer.\n",
476 s
->buffer_duration
, bytes
);
477 av_log(s
, AV_LOG_DEBUG
, "Real buffer length is %u bytes\n", buffer_attributes
.tlength
);
478 } else if (s
->buffer_size
)
479 buffer_attributes
.tlength
= s
->buffer_size
;
481 buffer_attributes
.prebuf
= s
->prebuf
;
483 buffer_attributes
.minreq
= s
->minreq
;
485 sample_spec
.format
= ff_codec_id_to_pulse_format(st
->codec
->codec_id
);
486 sample_spec
.rate
= st
->codec
->sample_rate
;
487 sample_spec
.channels
= st
->codec
->channels
;
488 if (!pa_sample_spec_valid(&sample_spec
)) {
489 av_log(s
, AV_LOG_ERROR
, "Invalid sample spec.\n");
490 return AVERROR(EINVAL
);
493 if (sample_spec
.channels
== 1) {
494 channel_map
.channels
= 1;
495 channel_map
.map
[0] = PA_CHANNEL_POSITION_MONO
;
496 } else if (st
->codec
->channel_layout
) {
497 if (av_get_channel_layout_nb_channels(st
->codec
->channel_layout
) != st
->codec
->channels
)
498 return AVERROR(EINVAL
);
499 pulse_map_channels_to_pulse(st
->codec
->channel_layout
, &channel_map
);
500 /* Unknown channel is present in channel_layout, let PulseAudio use its default. */
501 if (channel_map
.channels
!= sample_spec
.channels
) {
502 av_log(s
, AV_LOG_WARNING
, "Unknown channel. Using defaul channel map.\n");
503 channel_map
.channels
= 0;
506 channel_map
.channels
= 0;
508 if (!channel_map
.channels
)
509 av_log(s
, AV_LOG_WARNING
, "Using PulseAudio's default channel map.\n");
510 else if (!pa_channel_map_valid(&channel_map
)) {
511 av_log(s
, AV_LOG_ERROR
, "Invalid channel map.\n");
512 return AVERROR(EINVAL
);
515 /* start main loop */
516 s
->mainloop
= pa_threaded_mainloop_new();
518 av_log(s
, AV_LOG_ERROR
, "Cannot create threaded mainloop.\n");
519 return AVERROR(ENOMEM
);
521 if ((ret
= pa_threaded_mainloop_start(s
->mainloop
)) < 0) {
522 av_log(s
, AV_LOG_ERROR
, "Cannot start threaded mainloop: %s.\n", pa_strerror(ret
));
523 pa_threaded_mainloop_free(s
->mainloop
);
525 return AVERROR_EXTERNAL
;
528 pa_threaded_mainloop_lock(s
->mainloop
);
530 mainloop_api
= pa_threaded_mainloop_get_api(s
->mainloop
);
532 av_log(s
, AV_LOG_ERROR
, "Cannot get mainloop API.\n");
533 ret
= AVERROR_EXTERNAL
;
537 s
->ctx
= pa_context_new(mainloop_api
, s
->name
);
539 av_log(s
, AV_LOG_ERROR
, "Cannot create context.\n");
540 ret
= AVERROR(ENOMEM
);
543 pa_context_set_state_callback(s
->ctx
, pulse_context_state
, s
);
544 pa_context_set_subscribe_callback(s
->ctx
, pulse_event
, h
);
546 if ((ret
= pa_context_connect(s
->ctx
, s
->server
, 0, NULL
)) < 0) {
547 av_log(s
, AV_LOG_ERROR
, "Cannot connect context: %s.\n", pa_strerror(ret
));
548 ret
= AVERROR_EXTERNAL
;
552 if ((ret
= pulse_context_wait(s
)) < 0) {
553 av_log(s
, AV_LOG_ERROR
, "Context failed.\n");
557 s
->stream
= pa_stream_new(s
->ctx
, stream_name
, &sample_spec
,
558 channel_map
.channels
? &channel_map
: NULL
);
560 if ((ret
= pulse_update_sink_info(h
)) < 0) {
561 av_log(s
, AV_LOG_ERROR
, "Updating sink info failed.\n");
566 av_log(s
, AV_LOG_ERROR
, "Cannot create stream.\n");
567 ret
= AVERROR(ENOMEM
);
570 pa_stream_set_state_callback(s
->stream
, pulse_stream_state
, s
);
571 pa_stream_set_write_callback(s
->stream
, pulse_stream_writable
, h
);
572 pa_stream_set_overflow_callback(s
->stream
, pulse_overflow
, h
);
573 pa_stream_set_underflow_callback(s
->stream
, pulse_underflow
, h
);
575 if ((ret
= pa_stream_connect_playback(s
->stream
, s
->device
, &buffer_attributes
,
576 stream_flags
, NULL
, NULL
)) < 0) {
577 av_log(s
, AV_LOG_ERROR
, "pa_stream_connect_playback failed: %s.\n", pa_strerror(ret
));
578 ret
= AVERROR_EXTERNAL
;
582 if ((ret
= pulse_stream_wait(s
)) < 0) {
583 av_log(s
, AV_LOG_ERROR
, "Stream failed.\n");
587 /* read back buffer attributes for future use */
588 buffer_attributes
= *pa_stream_get_buffer_attr(s
->stream
);
589 s
->buffer_size
= buffer_attributes
.tlength
;
590 s
->prebuf
= buffer_attributes
.prebuf
;
591 s
->minreq
= buffer_attributes
.minreq
;
592 av_log(s
, AV_LOG_DEBUG
, "Real buffer attributes: size: %d, prebuf: %d, minreq: %d\n",
593 s
->buffer_size
, s
->prebuf
, s
->minreq
);
595 pa_threaded_mainloop_unlock(s
->mainloop
);
597 if ((ret
= pulse_subscribe_events(s
)) < 0) {
598 av_log(s
, AV_LOG_ERROR
, "Event subscription failed.\n");
599 /* a bit ugly but the simplest to lock here*/
600 pa_threaded_mainloop_lock(s
->mainloop
);
604 /* force control messages */
606 s
->last_volume
= PA_VOLUME_INVALID
;
607 pa_threaded_mainloop_lock(s
->mainloop
);
608 if ((ret
= pulse_update_sink_input_info(h
)) < 0) {
609 av_log(s
, AV_LOG_ERROR
, "Updating sink input info failed.\n");
612 pa_threaded_mainloop_unlock(s
->mainloop
);
614 avpriv_set_pts_info(st
, 64, 1, 1000000); /* 64 bits pts in us */
618 pa_threaded_mainloop_unlock(s
->mainloop
);
619 pulse_write_trailer(h
);
623 static int pulse_write_packet(AVFormatContext
*h
, AVPacket
*pkt
)
625 PulseData
*s
= h
->priv_data
;
627 int64_t writable_size
;
630 return pulse_flash_stream(s
);
632 if (pkt
->dts
!= AV_NOPTS_VALUE
)
633 s
->timestamp
= pkt
->dts
;
636 s
->timestamp
+= pkt
->duration
;
638 AVStream
*st
= h
->streams
[0];
639 AVCodecContext
*codec_ctx
= st
->codec
;
640 AVRational r
= { 1, codec_ctx
->sample_rate
};
641 int64_t samples
= pkt
->size
/ (av_get_bytes_per_sample(codec_ctx
->sample_fmt
) * codec_ctx
->channels
);
642 s
->timestamp
+= av_rescale_q(samples
, r
, st
->time_base
);
645 pa_threaded_mainloop_lock(s
->mainloop
);
646 if (!PA_STREAM_IS_GOOD(pa_stream_get_state(s
->stream
))) {
647 av_log(s
, AV_LOG_ERROR
, "PulseAudio stream is in invalid state.\n");
650 while (pa_stream_writable_size(s
->stream
) < s
->minreq
) {
651 if (s
->nonblocking
) {
652 pa_threaded_mainloop_unlock(s
->mainloop
);
653 return AVERROR(EAGAIN
);
655 pa_threaded_mainloop_wait(s
->mainloop
);
658 if ((ret
= pa_stream_write(s
->stream
, pkt
->data
, pkt
->size
, NULL
, 0, PA_SEEK_RELATIVE
)) < 0) {
659 av_log(s
, AV_LOG_ERROR
, "pa_stream_write failed: %s\n", pa_strerror(ret
));
662 if ((writable_size
= pa_stream_writable_size(s
->stream
)) >= s
->minreq
)
663 avdevice_dev_to_app_control_message(h
, AV_DEV_TO_APP_BUFFER_WRITABLE
, &writable_size
, sizeof(writable_size
));
665 pa_threaded_mainloop_unlock(s
->mainloop
);
669 pa_threaded_mainloop_unlock(s
->mainloop
);
670 return AVERROR_EXTERNAL
;
673 static int pulse_write_frame(AVFormatContext
*h
, int stream_index
,
674 AVFrame
**frame
, unsigned flags
)
678 /* Planar formats are not supported yet. */
679 if (flags
& AV_WRITE_UNCODED_FRAME_QUERY
)
680 return av_sample_fmt_is_planar(h
->streams
[stream_index
]->codec
->sample_fmt
) ?
683 pkt
.data
= (*frame
)->data
[0];
684 pkt
.size
= (*frame
)->nb_samples
* av_get_bytes_per_sample((*frame
)->format
) * av_frame_get_channels(*frame
);
685 pkt
.dts
= (*frame
)->pkt_dts
;
686 pkt
.duration
= av_frame_get_pkt_duration(*frame
);
687 return pulse_write_packet(h
, &pkt
);
691 static void pulse_get_output_timestamp(AVFormatContext
*h
, int stream
, int64_t *dts
, int64_t *wall
)
693 PulseData
*s
= h
->priv_data
;
696 pa_threaded_mainloop_lock(s
->mainloop
);
697 pa_stream_get_latency(s
->stream
, &latency
, &neg
);
698 pa_threaded_mainloop_unlock(s
->mainloop
);
700 *wall
= av_gettime();
702 *dts
= s
->timestamp
- (neg
? -latency
: latency
);
705 static int pulse_get_device_list(AVFormatContext
*h
, AVDeviceInfoList
*device_list
)
707 PulseData
*s
= h
->priv_data
;
708 return ff_pulse_audio_get_devices(device_list
, s
->server
, 1);
711 static int pulse_control_message(AVFormatContext
*h
, int type
,
712 void *data
, size_t data_size
)
714 PulseData
*s
= h
->priv_data
;
718 case AV_APP_TO_DEV_PAUSE
:
719 return pulse_set_pause(s
, 1);
720 case AV_APP_TO_DEV_PLAY
:
721 return pulse_set_pause(s
, 0);
722 case AV_APP_TO_DEV_TOGGLE_PAUSE
:
723 return pulse_set_pause(s
, !pa_stream_is_corked(s
->stream
));
724 case AV_APP_TO_DEV_MUTE
:
727 return pulse_set_mute(s
);
730 case AV_APP_TO_DEV_UNMUTE
:
733 return pulse_set_mute(s
);
736 case AV_APP_TO_DEV_TOGGLE_MUTE
:
738 return pulse_set_mute(s
);
739 case AV_APP_TO_DEV_SET_VOLUME
:
740 return pulse_set_volume(s
, *(double *)data
);
741 case AV_APP_TO_DEV_GET_VOLUME
:
742 s
->last_volume
= PA_VOLUME_INVALID
;
743 pa_threaded_mainloop_lock(s
->mainloop
);
744 ret
= pulse_update_sink_input_info(h
);
745 pa_threaded_mainloop_unlock(s
->mainloop
);
747 case AV_APP_TO_DEV_GET_MUTE
:
749 pa_threaded_mainloop_lock(s
->mainloop
);
750 ret
= pulse_update_sink_input_info(h
);
751 pa_threaded_mainloop_unlock(s
->mainloop
);
756 return AVERROR(ENOSYS
);
759 #define OFFSET(a) offsetof(PulseData, a)
760 #define E AV_OPT_FLAG_ENCODING_PARAM
761 static const AVOption options
[] = {
762 { "server", "set PulseAudio server", OFFSET(server
), AV_OPT_TYPE_STRING
, {.str
= NULL
}, 0, 0, E
},
763 { "name", "set application name", OFFSET(name
), AV_OPT_TYPE_STRING
, {.str
= LIBAVFORMAT_IDENT
}, 0, 0, E
},
764 { "stream_name", "set stream description", OFFSET(stream_name
), AV_OPT_TYPE_STRING
, {.str
= NULL
}, 0, 0, E
},
765 { "device", "set device name", OFFSET(device
), AV_OPT_TYPE_STRING
, {.str
= NULL
}, 0, 0, E
},
766 { "buffer_size", "set buffer size in bytes", OFFSET(buffer_size
), AV_OPT_TYPE_INT
, {.i64
= 0}, 0, INT_MAX
, E
},
767 { "buffer_duration", "set buffer duration in millisecs", OFFSET(buffer_duration
), AV_OPT_TYPE_INT
, {.i64
= 0}, 0, INT_MAX
, E
},
768 { "prebuf", "set pre-buffering size", OFFSET(prebuf
), AV_OPT_TYPE_INT
, {.i64
= 0}, 0, INT_MAX
, E
},
769 { "minreq", "set minimum request size", OFFSET(minreq
), AV_OPT_TYPE_INT
, {.i64
= 0}, 0, INT_MAX
, E
},
773 static const AVClass pulse_muxer_class
= {
774 .class_name
= "PulseAudio muxer",
775 .item_name
= av_default_item_name
,
777 .version
= LIBAVUTIL_VERSION_INT
,
778 .category
= AV_CLASS_CATEGORY_DEVICE_AUDIO_OUTPUT
,
781 AVOutputFormat ff_pulse_muxer
= {
783 .long_name
= NULL_IF_CONFIG_SMALL("Pulse audio output"),
784 .priv_data_size
= sizeof(PulseData
),
785 .audio_codec
= AV_NE(AV_CODEC_ID_PCM_S16BE
, AV_CODEC_ID_PCM_S16LE
),
786 .video_codec
= AV_CODEC_ID_NONE
,
787 .write_header
= pulse_write_header
,
788 .write_packet
= pulse_write_packet
,
789 .write_uncoded_frame
= pulse_write_frame
,
790 .write_trailer
= pulse_write_trailer
,
791 .get_output_timestamp
= pulse_get_output_timestamp
,
792 .get_device_list
= pulse_get_device_list
,
793 .control_message
= pulse_control_message
,
794 .flags
= AVFMT_NOFILE
| AVFMT_ALLOW_FLUSH
,
795 .priv_class
= &pulse_muxer_class
,