Browse code

lavd: pulse audio encoder

Signed-off-by: Lukasz Marek <lukasz.m.luki@gmail.com>
Reviewed-by: Stefano Sabatini <stefasab@gmail.com>
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Lukasz Marek authored on 2013/10/04 18:49:07
Showing 7 changed files
... ...
@@ -39,6 +39,7 @@ version <next>
39 39
 - native VP9 decoder
40 40
 - dpx parser
41 41
 - max_error_rate parameter in ffmpeg
42
+- PulseAudio output device
42 43
 
43 44
 
44 45
 version 2.0:
... ...
@@ -2134,6 +2134,7 @@ openal_indev_deps="openal"
2134 2134
 oss_indev_deps_any="soundcard_h sys_soundcard_h"
2135 2135
 oss_outdev_deps_any="soundcard_h sys_soundcard_h"
2136 2136
 pulse_indev_deps="libpulse"
2137
+pulse_outdev_deps="libpulse"
2137 2138
 sdl_outdev_deps="sdl"
2138 2139
 sndio_indev_deps="sndio_h"
2139 2140
 sndio_outdev_deps="sndio_h"
... ...
@@ -108,6 +108,41 @@ ffmpeg -i INPUT -pix_fmt rgb24 -f caca -list_dither colors -
108 108
 
109 109
 OSS (Open Sound System) output device.
110 110
 
111
+@section pulse
112
+
113
+PulseAudio output device.
114
+
115
+To enable this output device you need to configure FFmpeg with @code{--enable-libpulse}.
116
+
117
+More information about PulseAudio can be found on @url{http://www.pulseaudio.org}
118
+
119
+@subsection Options
120
+@table @option
121
+
122
+@item server
123
+Connect to a specific PulseAudio server, specified by an IP address.
124
+Default server is used when not provided.
125
+
126
+@item name
127
+Specify the application name PulseAudio will use when showing active clients,
128
+by default it is the @code{LIBAVFORMAT_IDENT} string.
129
+
130
+@item stream_name
131
+Specify the stream name PulseAudio will use when showing active streams,
132
+by default it is set to the specified output name.
133
+
134
+@item device
135
+Specify the device to use. Default device is used when not provided.
136
+List of output devices can be obtained with command @command{pactl list sinks}.
137
+
138
+@end table
139
+
140
+@subsection Examples
141
+Play a file on default device on default server:
142
+@example
143
+ffmpeg  -i INPUT -f pulse "stream name"
144
+@end example
145
+
111 146
 @section sdl
112 147
 
113 148
 SDL (Simple DirectMedia Layer) output device.
... ...
@@ -31,6 +31,7 @@ OBJS-$(CONFIG_OPENAL_INDEV)              += openal-dec.o
31 31
 OBJS-$(CONFIG_OSS_INDEV)                 += oss_audio.o
32 32
 OBJS-$(CONFIG_OSS_OUTDEV)                += oss_audio.o
33 33
 OBJS-$(CONFIG_PULSE_INDEV)               += pulse.o
34
+OBJS-$(CONFIG_PULSE_OUTDEV)              += pulse_audio_enc.o
34 35
 OBJS-$(CONFIG_SDL_OUTDEV)                += sdl.o
35 36
 OBJS-$(CONFIG_SNDIO_INDEV)               += sndio_common.o sndio_dec.o
36 37
 OBJS-$(CONFIG_SNDIO_OUTDEV)              += sndio_common.o sndio_enc.o
... ...
@@ -57,7 +57,7 @@ void avdevice_register_all(void)
57 57
     REGISTER_INDEV   (LAVFI,            lavfi);
58 58
     REGISTER_INDEV   (OPENAL,           openal);
59 59
     REGISTER_INOUTDEV(OSS,              oss);
60
-    REGISTER_INDEV   (PULSE,            pulse);
60
+    REGISTER_INOUTDEV(PULSE,            pulse);
61 61
     REGISTER_OUTDEV  (SDL,              sdl);
62 62
     REGISTER_INOUTDEV(SNDIO,            sndio);
63 63
     REGISTER_INOUTDEV(V4L2,             v4l2);
64 64
new file mode 100644
... ...
@@ -0,0 +1,171 @@
0
+/*
1
+ * Copyright (c) 2013 Lukasz Marek <lukasz.m.luki@gmail.com>
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include <pulse/simple.h>
21
+#include <pulse/error.h>
22
+#include "libavutil/opt.h"
23
+#include "libavutil/time.h"
24
+#include "libavutil/log.h"
25
+#include "libavformat/avformat.h"
26
+#include "libavformat/internal.h"
27
+
28
+typedef struct PulseData {
29
+    AVClass *class;
30
+    const char *server;
31
+    const char *name;
32
+    const char *stream_name;
33
+    const char *device;
34
+    pa_simple *pa;
35
+    unsigned int stream_index;
36
+} PulseData;
37
+
38
+static pa_sample_format_t codec_id_to_pulse_format(enum AVCodecID codec_id)
39
+{
40
+    switch (codec_id) {
41
+    case AV_CODEC_ID_PCM_U8:    return PA_SAMPLE_U8;
42
+    case AV_CODEC_ID_PCM_ALAW:  return PA_SAMPLE_ALAW;
43
+    case AV_CODEC_ID_PCM_MULAW: return PA_SAMPLE_ULAW;
44
+    case AV_CODEC_ID_PCM_S16LE: return PA_SAMPLE_S16LE;
45
+    case AV_CODEC_ID_PCM_S16BE: return PA_SAMPLE_S16BE;
46
+    case AV_CODEC_ID_PCM_F32LE: return PA_SAMPLE_FLOAT32LE;
47
+    case AV_CODEC_ID_PCM_F32BE: return PA_SAMPLE_FLOAT32BE;
48
+    case AV_CODEC_ID_PCM_S32LE: return PA_SAMPLE_S32LE;
49
+    case AV_CODEC_ID_PCM_S32BE: return PA_SAMPLE_S32BE;
50
+    case AV_CODEC_ID_PCM_S24LE: return PA_SAMPLE_S24LE;
51
+    case AV_CODEC_ID_PCM_S24BE: return PA_SAMPLE_S24BE;
52
+    default:                    return PA_SAMPLE_INVALID;
53
+    }
54
+}
55
+
56
+static av_cold int pulse_write_header(AVFormatContext *h)
57
+{
58
+    PulseData *s = h->priv_data;
59
+    AVStream *st = NULL;
60
+    int ret;
61
+    pa_sample_spec ss;
62
+    pa_buffer_attr attr = { -1, -1, -1, -1, -1 };
63
+    const char *stream_name = s->stream_name;
64
+
65
+    for (unsigned i = 0; i < h->nb_streams; i++) {
66
+        if (h->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
67
+            st = h->streams[i];
68
+            s->stream_index = i;
69
+            break;
70
+        }
71
+    }
72
+
73
+    if (!st) {
74
+        av_log(s, AV_LOG_ERROR, "No audio stream found.\n");
75
+        return AVERROR(EINVAL);
76
+    }
77
+
78
+    if (!stream_name)
79
+        stream_name = h->filename;
80
+
81
+    ss.format = codec_id_to_pulse_format(st->codec->codec_id);
82
+    ss.rate = st->codec->sample_rate;
83
+    ss.channels = st->codec->channels;
84
+
85
+    s->pa = pa_simple_new(s->server,                 // Server
86
+                          s->name,                   // Application name
87
+                          PA_STREAM_PLAYBACK,
88
+                          s->device,                 // Device
89
+                          stream_name,               // Description of a stream
90
+                          &ss,                       // Sample format
91
+                          NULL,                      // Use default channel map
92
+                          &attr,                     // Buffering attributes
93
+                          &ret);                     // Result
94
+
95
+    if (!s->pa) {
96
+        av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n", pa_strerror(ret));
97
+        return AVERROR(EIO);
98
+    }
99
+
100
+    avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
101
+
102
+    return 0;
103
+}
104
+
105
+static av_cold int pulse_write_trailer(AVFormatContext *h)
106
+{
107
+    PulseData *s = h->priv_data;
108
+    pa_simple_flush(s->pa, NULL);
109
+    pa_simple_free(s->pa);
110
+    s->pa = NULL;
111
+    return 0;
112
+}
113
+
114
+static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
115
+{
116
+    PulseData *s = h->priv_data;
117
+    int size     = pkt->size;
118
+    uint8_t *buf = pkt->data;
119
+    int error;
120
+
121
+    if (s->stream_index != pkt->stream_index)
122
+        return 0;
123
+
124
+    if ((error = pa_simple_write(s->pa, buf, size, &error))) {
125
+        av_log(s, AV_LOG_ERROR, "pa_simple_write failed: %s\n", pa_strerror(error));
126
+        return AVERROR(EIO);
127
+    }
128
+
129
+    return 0;
130
+}
131
+
132
+static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall)
133
+{
134
+    PulseData *s = h->priv_data;
135
+    pa_usec_t latency = pa_simple_get_latency(s->pa, NULL);
136
+    *wall = av_gettime();
137
+    *dts = h->streams[0]->cur_dts - latency;
138
+}
139
+
140
+#define OFFSET(a) offsetof(PulseData, a)
141
+#define E AV_OPT_FLAG_ENCODING_PARAM
142
+
143
+static const AVOption options[] = {
144
+    { "server",        "set PulseAudio server",  OFFSET(server),      AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
145
+    { "name",          "set application name",   OFFSET(name),        AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT},  0, 0, E },
146
+    { "stream_name",   "set stream description", OFFSET(stream_name), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
147
+    { "device",        "set device name",        OFFSET(device),      AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
148
+    { NULL }
149
+};
150
+
151
+static const AVClass pulse_muxer_class = {
152
+    .class_name     = "Pulse muxer",
153
+    .item_name      = av_default_item_name,
154
+    .option         = options,
155
+    .version        = LIBAVUTIL_VERSION_INT,
156
+};
157
+
158
+AVOutputFormat ff_pulse_muxer = {
159
+    .name           = "pulse",
160
+    .long_name      = NULL_IF_CONFIG_SMALL("Pulse audio output"),
161
+    .priv_data_size = sizeof(PulseData),
162
+    .audio_codec    = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
163
+    .video_codec    = AV_CODEC_ID_NONE,
164
+    .write_header   = pulse_write_header,
165
+    .write_packet   = pulse_write_packet,
166
+    .write_trailer  = pulse_write_trailer,
167
+    .get_output_timestamp = pulse_get_output_timestamp,
168
+    .flags          = AVFMT_NOFILE,
169
+    .priv_class     = &pulse_muxer_class,
170
+};
... ...
@@ -28,7 +28,7 @@
28 28
 #include "libavutil/avutil.h"
29 29
 
30 30
 #define LIBAVDEVICE_VERSION_MAJOR  55
31
-#define LIBAVDEVICE_VERSION_MINOR   3
31
+#define LIBAVDEVICE_VERSION_MINOR   4
32 32
 #define LIBAVDEVICE_VERSION_MICRO 100
33 33
 
34 34
 #define LIBAVDEVICE_VERSION_INT AV_VERSION_INT(LIBAVDEVICE_VERSION_MAJOR, \