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
22 #include <pulse/pulseaudio.h>
23 #include <pulse/error.h>
32
53
55 int eol, void *userdata)
56 {
58
60 return;
61
62 if (eol) {
63 pa_threaded_mainloop_signal(s->
mainloop, 0);
64 } else {
65 if (dev->flags & PA_SINK_FLAT_VOLUME)
67 else
70 }
71 }
72
73 /* Mainloop must be locked before calling this function as it uses pa_threaded_mainloop_wait. */
75 {
78 if (!(op = pa_context_get_sink_info_by_name(s->
ctx, s->
device,
82 }
83 while (pa_operation_get_state(op) == PA_OPERATION_RUNNING)
84 pa_threaded_mainloop_wait(s->
mainloop);
85 pa_operation_unref(op);
86 return 0;
87 }
88
90 int eol, void *userdata)
91 {
94
96 return;
97
98 if (!eol) {
100 pa_volume_t vol = pa_cvolume_avg(&i->volume);
101 if (s->
mute < 0 || (s->
mute && !i->mute) || (!s->
mute && i->mute)) {
104 }
105
108 val = (double)vol / PA_VOLUME_NORM;
111 }
112 }
113 }
114
115 /* This function creates new loop so may be called from PA callbacks.
116 Mainloop must be locked before calling this function as it operates on streams. */
118 {
121 enum pa_operation_state op_state;
122 pa_mainloop *ml =
NULL;
124 int ret = 0;
125
127 return ret;
128
129 if (!(op = pa_context_get_sink_input_info(ctx, pa_stream_get_index(s->
stream),
133 }
134
135 while ((op_state = pa_operation_get_state(op)) == PA_OPERATION_RUNNING)
136 pa_mainloop_iterate(ml, 1,
NULL);
137 pa_operation_unref(op);
138 if (op_state != PA_OPERATION_DONE) {
141 }
142
145 if (ret)
147 return ret;
148 }
149
151 uint32_t idx, void *userdata)
152 {
155
157 return;
158
159 if ((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_SINK_INPUT) {
160 if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) == PA_SUBSCRIPTION_EVENT_CHANGE)
161 // Calling from mainloop callback. No need to lock mainloop.
163 }
164 }
165
167 {
170 int64_t
val = nbytes;
171
173 return;
174
176 pa_threaded_mainloop_signal(s->
mainloop, 0);
177 }
178
180 {
183 }
184
186 {
189 }
190
192 {
194
196 return;
197
198 switch (pa_stream_get_state(s->
stream)) {
199 case PA_STREAM_READY:
200 case PA_STREAM_FAILED:
201 case PA_STREAM_TERMINATED:
202 pa_threaded_mainloop_signal(s->
mainloop, 0);
203 default:
204 break;
205 }
206 }
207
209 {
210 pa_stream_state_t
state;
211
212 while ((state = pa_stream_get_state(s->
stream)) != PA_STREAM_READY) {
213 if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED)
215 pa_threaded_mainloop_wait(s->
mainloop);
216 }
217 return 0;
218 }
219
221 {
223
225 return;
226
227 switch (pa_context_get_state(ctx)) {
228 case PA_CONTEXT_READY:
229 case PA_CONTEXT_FAILED:
230 case PA_CONTEXT_TERMINATED:
231 pa_threaded_mainloop_signal(s->
mainloop, 0);
232 default:
233 break;
234 }
235 }
236
238 {
239 pa_context_state_t
state;
240
241 while ((state = pa_context_get_state(s->
ctx)) != PA_CONTEXT_READY) {
242 if (state == PA_CONTEXT_FAILED || state == PA_CONTEXT_TERMINATED)
244 pa_threaded_mainloop_wait(s->
mainloop);
245 }
246 return 0;
247 }
248
250 {
252
254 return;
255
257 pa_threaded_mainloop_signal(s->
mainloop, 0);
258 }
259
261 {
262 if (!op) {
263 pa_threaded_mainloop_unlock(s->
mainloop);
266 }
269 pa_threaded_mainloop_wait(s->
mainloop);
270 pa_operation_unref(op);
271 pa_threaded_mainloop_unlock(s->
mainloop);
275 }
276
278 {
280 pa_threaded_mainloop_lock(s->
mainloop);
283 }
284
286 {
288 pa_threaded_mainloop_lock(s->
mainloop);
291 }
292
294 {
296
298 return;
299
301 pa_threaded_mainloop_signal(s->
mainloop, 0);
302 }
303
305 {
306 if (!op) {
307 pa_threaded_mainloop_unlock(s->
mainloop);
310 }
313 pa_threaded_mainloop_wait(s->
mainloop);
314 pa_operation_unref(op);
315 pa_threaded_mainloop_unlock(s->
mainloop);
319 }
320
322 {
324 pa_threaded_mainloop_lock(s->
mainloop);
325 op = pa_context_set_sink_input_mute(s->
ctx, pa_stream_get_index(s->
stream),
328 }
329
331 {
333 pa_cvolume cvol;
334 pa_volume_t vol;
335 const pa_sample_spec *
ss = pa_stream_get_sample_spec(s->
stream);
336
337 vol = pa_sw_volume_multiply(
lrint(volume * PA_VOLUME_NORM), s->
base_volume);
338 pa_cvolume_set(&cvol, ss->channels, PA_VOLUME_NORM);
339 pa_sw_cvolume_multiply_scalar(&cvol, &cvol, vol);
340 pa_threaded_mainloop_lock(s->
mainloop);
341 op = pa_context_set_sink_input_volume(s->
ctx, pa_stream_get_index(s->
stream),
344 }
345
347 {
349
350 pa_threaded_mainloop_lock(s->
mainloop);
353 }
354
356 {
357 channel_map->channels = 0;
359 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
361 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
363 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
365 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_LFE;
367 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
369 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
371 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
373 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
375 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
377 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
379 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
381 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_CENTER;
383 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_LEFT;
385 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_CENTER;
387 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_FRONT_RIGHT;
389 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_LEFT;
391 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_CENTER;
393 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
395 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
397 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
399 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX0;
401 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX1;
403 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX2;
405 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_AUX3;
407 channel_map->map[channel_map->channels++] = PA_CHANNEL_POSITION_LFE;
408 }
409
411 {
413
415 pa_threaded_mainloop_lock(s->
mainloop);
417 pa_stream_disconnect(s->
stream);
422 pa_stream_unref(s->
stream);
424 }
426 pa_context_disconnect(s->
ctx);
428 pa_context_set_subscribe_callback(s->
ctx,
NULL,
NULL);
429 pa_context_unref(s->
ctx);
431 }
432 pa_threaded_mainloop_unlock(s->
mainloop);
433 pa_threaded_mainloop_stop(s->
mainloop);
434 pa_threaded_mainloop_free(s->
mainloop);
436 }
437
438 return 0;
439 }
440
442 {
445 int ret;
446 pa_sample_spec sample_spec;
447 pa_buffer_attr buffer_attributes = { -1, -1, -1, -1, -1 };
448 pa_channel_map channel_map;
449 pa_mainloop_api *mainloop_api;
451 static const pa_stream_flags_t stream_flags = PA_STREAM_INTERPOLATE_TIMING |
452 PA_STREAM_AUTO_TIMING_UPDATE |
453 PA_STREAM_NOT_MONOTONIC;
454
458 }
460
461 if (!stream_name) {
463 stream_name = h->
url;
464 else
465 stream_name = "Playback";
466 }
468
473 bytes /= 1000;
474 buffer_attributes.tlength =
FFMAX(s->
buffer_size, av_clip64(bytes, 0, UINT32_MAX - 1));
476 "Buffer duration: %ums recalculated into %"PRId64" bytes buffer.\n",
478 av_log(s,
AV_LOG_DEBUG,
"Real buffer length is %u bytes\n", buffer_attributes.tlength);
482 buffer_attributes.prebuf = s->
prebuf;
484 buffer_attributes.minreq = s->
minreq;
485
489 if (!pa_sample_spec_valid(&sample_spec)) {
492 }
493
494 if (sample_spec.channels == 1) {
495 channel_map.channels = 1;
496 channel_map.map[0] = PA_CHANNEL_POSITION_MONO;
501 /* Unknown channel is present in channel_layout, let PulseAudio use its default. */
502 if (channel_map.channels != sample_spec.channels) {
504 channel_map.channels = 0;
505 }
506 } else
507 channel_map.channels = 0;
508
509 if (!channel_map.channels)
511 else if (!pa_channel_map_valid(&channel_map)) {
514 }
515
516 /* start main loop */
517 s->
mainloop = pa_threaded_mainloop_new();
521 }
522 if ((ret = pa_threaded_mainloop_start(s->
mainloop)) < 0) {
524 pa_threaded_mainloop_free(s->
mainloop);
527 }
528
529 pa_threaded_mainloop_lock(s->
mainloop);
530
531 mainloop_api = pa_threaded_mainloop_get_api(s->
mainloop);
532 if (!mainloop_api) {
536 }
537
538 s->
ctx = pa_context_new(mainloop_api, s->
name);
543 }
546
547 if ((ret = pa_context_connect(s->
ctx, s->
server, 0,
NULL)) < 0) {
551 }
552
556 }
557
558 s->
stream = pa_stream_new(s->
ctx, stream_name, &sample_spec,
559 channel_map.channels ? &channel_map :
NULL);
560
564 }
565
570 }
575
576 if ((ret = pa_stream_connect_playback(s->
stream, s->
device, &buffer_attributes,
577 stream_flags, NULL, NULL)) < 0) {
578 av_log(s,
AV_LOG_ERROR,
"pa_stream_connect_playback failed: %s.\n", pa_strerror(ret));
581 }
582
586 }
587
588 /* read back buffer attributes for future use */
589 buffer_attributes = *pa_stream_get_buffer_attr(s->
stream);
591 s->
prebuf = buffer_attributes.prebuf;
592 s->
minreq = buffer_attributes.minreq;
595
596 pa_threaded_mainloop_unlock(s->
mainloop);
597
600 /* a bit ugly but the simplest to lock here*/
601 pa_threaded_mainloop_lock(s->
mainloop);
603 }
604
605 /* force control messages */
608 pa_threaded_mainloop_lock(s->
mainloop);
612 }
613 pa_threaded_mainloop_unlock(s->
mainloop);
614
616
617 return 0;
619 pa_threaded_mainloop_unlock(s->
mainloop);
621 return ret;
622 }
623
625 {
627 int ret;
628 int64_t writable_size;
629
630 if (!pkt)
632
635
638 } else {
643 }
644
645 pa_threaded_mainloop_lock(s->
mainloop);
646 if (!PA_STREAM_IS_GOOD(pa_stream_get_state(s->
stream))) {
649 }
650 while (pa_stream_writable_size(s->
stream) < s->
minreq) {
652 pa_threaded_mainloop_unlock(s->
mainloop);
654 } else
655 pa_threaded_mainloop_wait(s->
mainloop);
656 }
657
658 if ((ret = pa_stream_write(s->
stream, pkt->
data, pkt->
size,
NULL, 0, PA_SEEK_RELATIVE)) < 0) {
661 }
662 if ((writable_size = pa_stream_writable_size(s->
stream)) >= s->
minreq)
664
665 pa_threaded_mainloop_unlock(s->
mainloop);
666
667 return 0;
669 pa_threaded_mainloop_unlock(s->
mainloop);
671 }
672
675 {
677
678 /* Planar formats are not supported yet. */
682
683 pkt.
data = (*frame)->data[0];
685 pkt.
dts = (*frame)->pkt_dts;
686 pkt.
duration = (*frame)->pkt_duration;
688 }
689
690
692 {
694 pa_usec_t latency;
695 int neg;
696 pa_threaded_mainloop_lock(s->
mainloop);
697 pa_stream_get_latency(s->
stream, &latency, &neg);
698 pa_threaded_mainloop_unlock(s->
mainloop);
699 if (wall)
701 if (dts)
702 *dts = s->
timestamp - (neg ? -latency : latency);
703 }
704
706 {
709 }
710
712 void *
data,
size_t data_size)
713 {
715 int ret;
716
717 switch(type) {
728 }
729 return 0;
734 }
735 return 0;
743 pa_threaded_mainloop_lock(s->
mainloop);
745 pa_threaded_mainloop_unlock(s->
mainloop);
746 return ret;
749 pa_threaded_mainloop_lock(s->
mainloop);
751 pa_threaded_mainloop_unlock(s->
mainloop);
752 return ret;
753 default:
754 break;
755 }
757 }
758
759 #define OFFSET(a) offsetof(PulseData, a)
760 #define E AV_OPT_FLAG_ENCODING_PARAM
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 },
771 };
772
779 };
780
796 };
const char const char void * val
static void pulse_map_channels_to_pulse(int64_t channel_layout, pa_channel_map *channel_map)
This structure describes decoded (raw) audio or video data.
static int pulse_flash_stream(PulseData *s)
#define AV_CH_TOP_FRONT_RIGHT
ptrdiff_t const GLvoid * data
static void pulse_context_result(pa_context *ctx, int success, void *userdata)
static int pulse_context_wait(PulseData *s)
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
#define AV_CH_TOP_FRONT_LEFT
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
#define AV_CH_TOP_FRONT_CENTER
static int pulse_finish_stream_operation(PulseData *s, pa_operation *op, const char *name)
const char * av_default_item_name(void *ptr)
Return the context name.
Volume level change message.
#define AV_CH_LOW_FREQUENCY_2
static void pulse_overflow(pa_stream *stream, void *userdata)
#define AV_CH_SURROUND_DIRECT_RIGHT
Buffer fullness status messages.
Macro definitions for various function/variable attributes.
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static void pulse_stream_state(pa_stream *stream, void *userdata)
int avdevice_dev_to_app_control_message(struct AVFormatContext *s, enum AVDevToAppMessageType type, void *data, size_t data_size)
Send control message from device to application.
#define AV_CH_TOP_BACK_LEFT
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
#define AV_CH_TOP_BACK_CENTER
static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
static int pulse_control_message(AVFormatContext *h, int type, void *data, size_t data_size)
#define AV_CH_LOW_FREQUENCY
AVStream ** streams
A list of all streams in the file.
static const AVClass pulse_muxer_class
static av_cold int pulse_write_trailer(AVFormatContext *h)
static void pulse_audio_sink_input_cb(pa_context *ctx, const pa_sink_input_info *i, int eol, void *userdata)
static int pulse_stream_wait(PulseData *s)
Get volume/mute messages.
int flags
Flags modifying the (de)muxer behaviour.
int ff_pulse_audio_connect_context(pa_mainloop **pa_ml, pa_context **pa_ctx, const char *server, const char *description)
static int pulse_write_frame(AVFormatContext *h, int stream_index, AVFrame **frame, unsigned flags)
uint64_t channel_layout
Audio only.
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
static void pulse_underflow(pa_stream *stream, void *userdata)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int pulse_set_volume(PulseData *s, double volume)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
char * url
input or output URL.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static void pulse_event(pa_context *ctx, pa_subscription_event_type_t t, uint32_t idx, void *userdata)
enum AVMediaType codec_type
General type of the encoded data.
common internal API header
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
#define AV_CH_STEREO_RIGHT
See AV_CH_STEREO_LEFT.
static const AVOption options[]
AVOutputFormat ff_pulse_muxer
static int pulse_set_pause(PulseData *s, int pause)
void ff_pulse_audio_disconnect_context(pa_mainloop **pa_ml, pa_context **pa_ctx)
static int pulse_update_sink_input_info(AVFormatContext *h)
static int pulse_subscribe_events(PulseData *s)
pa_sample_format_t av_cold ff_codec_id_to_pulse_format(enum AVCodecID codec_id)
#define AV_CH_FRONT_LEFT_OF_CENTER
#define AV_CH_FRONT_CENTER
#define AV_CH_FRONT_RIGHT_OF_CENTER
int64_t av_gettime(void)
Get the current time in microseconds.
int buffer_duration
Buffer size in ms, recalculated to buffer_size.
#define AV_CH_TOP_BACK_RIGHT
Describe the class of an AVClass context structure.
Rational number (pair of numerator and denominator).
int ff_pulse_audio_get_devices(AVDeviceInfoList *devices, const char *server, int output)
static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
Mute state change message.
static int pulse_finish_context_operation(PulseData *s, pa_operation *op, const char *name)
static void pulse_audio_sink_device_cb(pa_context *ctx, const pa_sink_info *dev, int eol, void *userdata)
#define AV_CH_BACK_CENTER
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
int sample_rate
Audio only.
static av_cold int pulse_write_header(AVFormatContext *h)
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
static int pulse_set_mute(PulseData *s)
static void pulse_stream_writable(pa_stream *stream, size_t nbytes, void *userdata)
static int pulse_update_sink_info(AVFormatContext *h)
static void pulse_context_state(pa_context *ctx, void *userdata)
void * priv_data
Format private data.
#define AV_CH_SURROUND_DIRECT_LEFT
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
#define AV_CH_FRONT_RIGHT
pa_threaded_mainloop * mainloop
AVCodecParameters * codecpar
Codec parameters associated with this stream.
int buffer_size
Buffer size in bytes.
static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
#define AVERROR_EXTERNAL
Generic error in an external library.
This structure stores compressed data.
static void pulse_stream_result(pa_stream *stream, int success, void *userdata)
#define AV_NOPTS_VALUE
Undefined timestamp value.
#define AV_CH_STEREO_LEFT
Stereo downmix.