[FFmpeg-devel] [PATCH 1/2] lavd/pulse_audio_enc: replace simple API with async API

Lukasz Marek lukasz.m.luki2 at gmail.com
Sun Apr 6 23:13:58 CEST 2014


Async API allows to use full capabilites of PulseAudio.

Signed-off-by: Lukasz Marek <lukasz.m.luki2 at gmail.com>
---
 libavdevice/pulse_audio_enc.c | 328 ++++++++++++++++++++++++++++++++++++------
 1 file changed, 288 insertions(+), 40 deletions(-)

diff --git a/libavdevice/pulse_audio_enc.c b/libavdevice/pulse_audio_enc.c
index fa2a7cb..6b423ca 100644
--- a/libavdevice/pulse_audio_enc.c
+++ b/libavdevice/pulse_audio_enc.c
@@ -18,13 +18,14 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#include <pulse/simple.h>
+#include <pulse/pulseaudio.h>
 #include <pulse/error.h>
 #include "libavformat/avformat.h"
 #include "libavformat/internal.h"
 #include "libavutil/opt.h"
 #include "libavutil/time.h"
 #include "libavutil/log.h"
+#include "libavutil/attributes.h"
 #include "pulse_audio_common.h"
 
 typedef struct PulseData {
@@ -33,19 +34,137 @@ typedef struct PulseData {
     const char *name;
     const char *stream_name;
     const char *device;
-    pa_simple *pa;
     int64_t timestamp;
     int buffer_size;
     int buffer_duration;
+    int last_result;
+    pa_threaded_mainloop *mainloop;
+    pa_context *ctx;
+    pa_stream *stream;
 } PulseData;
 
+static void pulse_stream_writable(pa_stream *stream, size_t nbytes, void *userdata)
+{
+    PulseData *s = userdata;
+    pa_threaded_mainloop_signal(s->mainloop, 0);
+}
+
+static void pulse_stream_state(pa_stream *stream, void *userdata)
+{
+    PulseData *s = userdata;
+
+    switch (pa_stream_get_state(stream)) {
+        case PA_STREAM_READY:
+        case PA_STREAM_FAILED:
+        case PA_STREAM_TERMINATED:
+            pa_threaded_mainloop_signal(s->mainloop, 0);
+        default:
+            break;
+    }
+}
+
+static int pulse_stream_wait(PulseData *s)
+{
+    pa_stream_state_t state;
+
+    while ((state = pa_stream_get_state(s->stream)) != PA_STREAM_READY) {
+        if (state == PA_STREAM_FAILED || state == PA_STREAM_TERMINATED)
+            return AVERROR_EXTERNAL;
+        pa_threaded_mainloop_wait(s->mainloop);
+    }
+    return 0;
+}
+
+static void pulse_context_state(pa_context *c, void *userdata)
+{
+    PulseData *s = userdata;
+
+    switch (pa_context_get_state(c))
+    {
+        case PA_CONTEXT_READY:
+        case PA_CONTEXT_FAILED:
+        case PA_CONTEXT_TERMINATED:
+            pa_threaded_mainloop_signal(s->mainloop, 0);
+        default:
+            break;
+    }
+}
+
+static int pulse_context_wait(PulseData *s)
+{
+    pa_context_state_t state;
+
+    while ((state = pa_context_get_state(s->ctx)) != PA_CONTEXT_READY) {
+        if (state == PA_CONTEXT_FAILED || state == PA_CONTEXT_TERMINATED)
+            return AVERROR_EXTERNAL;
+        pa_threaded_mainloop_wait(s->mainloop);
+    }
+    return 0;
+}
+
+static void pulse_flash_result(pa_stream *stream, int success, void *userdata)
+{
+    PulseData *s = userdata;
+    s->last_result = success ? 1 : AVERROR_EXTERNAL;
+    pa_threaded_mainloop_signal(s->mainloop, 0);
+}
+
+static int pulse_flash_stream(PulseData *s)
+{
+    pa_operation *op;
+    s->last_result = 2;
+    pa_threaded_mainloop_lock(s->mainloop);
+    if (!(op = pa_stream_flush(s->stream, pulse_flash_result, s))) {
+        pa_threaded_mainloop_unlock(s->mainloop);
+        av_log(s, AV_LOG_ERROR, "pa_stream_flush failed.\n");
+        return AVERROR_EXTERNAL;
+    }
+    while (s->last_result == 2)
+        pa_threaded_mainloop_wait(s->mainloop);
+    pa_operation_unref(op);
+    pa_threaded_mainloop_unlock(s->mainloop);
+    if (s->last_result != 1)
+        av_log(s, AV_LOG_ERROR, "pa_stream_flush failed.\n");
+    return s->last_result;
+}
+
+static av_cold int pulse_write_trailer(AVFormatContext *h)
+{
+    PulseData *s = h->priv_data;
+
+    if (s->mainloop) {
+        pa_threaded_mainloop_lock(s->mainloop);
+        if (s->stream) {
+            pa_stream_disconnect(s->stream);
+            pa_stream_set_state_callback(s->stream, NULL, NULL);
+            pa_stream_set_write_callback(s->stream, NULL, NULL);
+            pa_stream_unref(s->stream);
+            s->stream = NULL;
+        }
+        if (s->ctx) {
+            pa_context_disconnect(s->ctx);
+            pa_context_set_state_callback(s->ctx, NULL, NULL);
+            pa_context_unref(s->ctx);
+            s->ctx = NULL;
+        }
+        pa_threaded_mainloop_unlock(s->mainloop);
+        pa_threaded_mainloop_stop(s->mainloop);
+        pa_threaded_mainloop_free(s->mainloop);
+        s->mainloop = NULL;
+    }
+
+    return 0;
+}
+
 static av_cold int pulse_write_header(AVFormatContext *h)
 {
     PulseData *s = h->priv_data;
     AVStream *st = NULL;
     int ret;
-    pa_sample_spec ss;
+    pa_sample_spec sample_spec;
     pa_buffer_attr attr = { -1, -1, -1, -1, -1 };
+    pa_channel_map channel_map;
+    pa_mainloop_api *mainloop_api;
     const char *stream_name = s->stream_name;
 
     if (h->nb_streams != 1 || h->streams[0]->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
@@ -74,51 +193,164 @@ static av_cold int pulse_write_header(AVFormatContext *h)
     } else if (s->buffer_size)
         attr.tlength = s->buffer_size;
 
-    ss.format = ff_codec_id_to_pulse_format(st->codec->codec_id);
-    ss.rate = st->codec->sample_rate;
-    ss.channels = st->codec->channels;
+    sample_spec.format = ff_codec_id_to_pulse_format(st->codec->codec_id);
+    sample_spec.rate = st->codec->sample_rate;
+    sample_spec.channels = st->codec->channels;
+
+    if (!pa_sample_spec_valid(&sample_spec)) {
+        av_log(s, AV_LOG_ERROR, "Invalid sample spec.\n");
+        return AVERROR(EINVAL);
+    }
 
-    s->pa = pa_simple_new(s->server,                 // Server
-                          s->name,                   // Application name
-                          PA_STREAM_PLAYBACK,
-                          s->device,                 // Device
-                          stream_name,               // Description of a stream
-                          &ss,                       // Sample format
-                          NULL,                      // Use default channel map
-                          &attr,                     // Buffering attributes
-                          &ret);                     // Result
+    if (sample_spec.channels == 1) {
+        channel_map.channels = 1;
+        channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_MONO;
+    } else if (st->codec->channel_layout) {
+        if (av_get_channel_layout_nb_channels(st->codec->channel_layout) != st->codec->channels)
+            return AVERROR(EINVAL);
+        channel_map.channels = 0;
+        if (st->codec->channel_layout & AV_CH_FRONT_LEFT)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
+        if (st->codec->channel_layout & AV_CH_FRONT_RIGHT)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
+        if (st->codec->channel_layout & AV_CH_FRONT_CENTER)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_FRONT_CENTER;
+        if (st->codec->channel_layout & AV_CH_LOW_FREQUENCY)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_LFE;
+        if (st->codec->channel_layout & AV_CH_BACK_LEFT)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_REAR_LEFT;
+        if (st->codec->channel_layout & AV_CH_BACK_RIGHT)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_REAR_RIGHT;
+        if (st->codec->channel_layout & AV_CH_FRONT_LEFT_OF_CENTER)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
+        if (st->codec->channel_layout & AV_CH_FRONT_RIGHT_OF_CENTER)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
+        if (st->codec->channel_layout & AV_CH_BACK_CENTER)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_REAR_CENTER;
+        if (st->codec->channel_layout & AV_CH_SIDE_LEFT)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_SIDE_LEFT;
+        if (st->codec->channel_layout & AV_CH_SIDE_RIGHT)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_SIDE_RIGHT;
+        if (st->codec->channel_layout & AV_CH_TOP_CENTER)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_TOP_CENTER;
+        if (st->codec->channel_layout & AV_CH_TOP_FRONT_LEFT)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_TOP_FRONT_LEFT;
+        if (st->codec->channel_layout & AV_CH_TOP_FRONT_CENTER)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_TOP_FRONT_CENTER;
+        if (st->codec->channel_layout & AV_CH_TOP_FRONT_RIGHT)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_TOP_FRONT_RIGHT;
+        if (st->codec->channel_layout & AV_CH_TOP_BACK_LEFT)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_TOP_REAR_LEFT;
+        if (st->codec->channel_layout & AV_CH_TOP_BACK_CENTER)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_TOP_REAR_CENTER;
+        if (st->codec->channel_layout & AV_CH_TOP_BACK_RIGHT)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
+        if (st->codec->channel_layout & AV_CH_STEREO_LEFT)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_FRONT_LEFT;
+        if (st->codec->channel_layout & AV_CH_STEREO_RIGHT)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_FRONT_RIGHT;
+        if (st->codec->channel_layout & AV_CH_WIDE_LEFT)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_AUX0;
+        if (st->codec->channel_layout & AV_CH_WIDE_RIGHT)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_AUX1;
+        if (st->codec->channel_layout & AV_CH_SURROUND_DIRECT_LEFT)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_AUX2;
+        if (st->codec->channel_layout & AV_CH_SURROUND_DIRECT_RIGHT)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_AUX3;
+        if (st->codec->channel_layout & AV_CH_LOW_FREQUENCY_2)
+            channel_map.map[channel_map.channels++] = PA_CHANNEL_POSITION_LFE;
+        /* Unknown channel is present in channel_layout, let PulseAudio use its default. */
+        if (channel_map.channels != sample_spec.channels) {
+            av_log(s, AV_LOG_WARNING, "Unknown channel. Using defaul channel map.\n");
+            channel_map.channels = 0;
+        }
+    } else
+        channel_map.channels = 0;
 
-    if (!s->pa) {
-        av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n", pa_strerror(ret));
-        return AVERROR(EIO);
+    if (!pa_channel_map_valid(&channel_map)) {
+        av_log(s, AV_LOG_ERROR, "Invalid channel map.\n");
+        return AVERROR(EINVAL);
     }
 
-    avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
+    /* start main loop */
+    s->mainloop = pa_threaded_mainloop_new();
+    if (!s->mainloop) {
+        av_log(s, AV_LOG_ERROR, "Cannot create threaded mainloop.\n");
+        return AVERROR(ENOMEM);
+    }
+    if ((ret = pa_threaded_mainloop_start(s->mainloop)) < 0) {
+        av_log(s, AV_LOG_ERROR, "Cannot start threaded mainloop: %s.\n", pa_strerror(ret));
+        pa_threaded_mainloop_free(s->mainloop);
+        s->mainloop = NULL;
+        return AVERROR_EXTERNAL;
+    }
 
-    return 0;
-}
+    pa_threaded_mainloop_lock(s->mainloop);
+
+    mainloop_api = pa_threaded_mainloop_get_api(s->mainloop);
+    if (!mainloop_api) {
+        av_log(s, AV_LOG_ERROR, "Cannot get mainloop API.\n");
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+    s->ctx = pa_context_new(mainloop_api, s->name);
+    if (!s->ctx) {
+        av_log(s, AV_LOG_ERROR, "Cannot create context.\n");
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+    pa_context_set_state_callback(s->ctx, pulse_context_state, s);
+
+    if ((ret = pa_context_connect(s->ctx, s->server, 0, NULL)) < 0) {
+        av_log(s, AV_LOG_ERROR, "Cannot connect context: %s.\n", pa_strerror(ret));
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+    if ((ret = pulse_context_wait(s)) < 0) {
+        av_log(s, AV_LOG_ERROR, "Context failed.\n");
+        goto fail;
+    }
+
+    s->stream = pa_stream_new(s->ctx, stream_name, &sample_spec, channel_map.channels ? &channel_map : NULL);
+    if (!s->stream) {
+        av_log(s, AV_LOG_ERROR, "Cannot create stream.\n");
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+    pa_stream_set_state_callback(s->stream, pulse_stream_state, s);
+    pa_stream_set_write_callback(s->stream, pulse_stream_writable, s);
+
+    if ((ret = pa_stream_connect_playback(s->stream, s->device, &attr, 0, NULL, NULL)) < 0) {
+        av_log(s, AV_LOG_ERROR, "pa_stream_connect_playback failed: %s.\n", pa_strerror(ret));
+        ret = AVERROR_EXTERNAL;
+        goto fail;
+    }
+
+    if ((ret = pulse_stream_wait(s)) < 0) {
+        av_log(s, AV_LOG_ERROR, "Stream failed.\n");
+        goto fail;
+    }
+
+    pa_threaded_mainloop_unlock(s->mainloop);
+
+    avpriv_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
 
-static av_cold int pulse_write_trailer(AVFormatContext *h)
-{
-    PulseData *s = h->priv_data;
-    pa_simple_flush(s->pa, NULL);
-    pa_simple_free(s->pa);
-    s->pa = NULL;
     return 0;
+  fail:
+    pa_threaded_mainloop_unlock(s->mainloop);
+    pulse_write_trailer(h);
+    return ret;
 }
 
 static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
 {
     PulseData *s = h->priv_data;
-    int error;
+    int ret;
 
-    if (!pkt) {
-        if (pa_simple_flush(s->pa, &error) < 0) {
-            av_log(s, AV_LOG_ERROR, "pa_simple_flush failed: %s\n", pa_strerror(error));
-            return AVERROR(EIO);
-        }
-        return 1;
-    }
+    if (!pkt)
+        return pulse_flash_stream(s);
 
     if (pkt->dts != AV_NOPTS_VALUE)
         s->timestamp = pkt->dts;
@@ -133,12 +365,24 @@ static int pulse_write_packet(AVFormatContext *h, AVPacket *pkt)
         s->timestamp += av_rescale_q(samples, r, st->time_base);
     }
 
-    if (pa_simple_write(s->pa, pkt->data, pkt->size, &error) < 0) {
-        av_log(s, AV_LOG_ERROR, "pa_simple_write failed: %s\n", pa_strerror(error));
-        return AVERROR(EIO);
+    pa_threaded_mainloop_lock(s->mainloop);
+    if (!PA_STREAM_IS_GOOD(pa_stream_get_state(s->stream))) {
+        av_log(s, AV_LOG_ERROR, "PulseAudio stream is in invalid state.\n");
+        goto fail;
+    }
+    while (!pa_stream_writable_size(s->stream))
+        pa_threaded_mainloop_wait(s->mainloop);
+
+    if ((ret = pa_stream_write(s->stream, pkt->data, pkt->size, NULL, 0, PA_SEEK_RELATIVE)) < 0) {
+        av_log(s, AV_LOG_ERROR, "pa_stream_write failed: %s\n", pa_strerror(ret));
+        goto fail;
     }
+    pa_threaded_mainloop_unlock(s->mainloop);
 
     return 0;
+  fail:
+    pa_threaded_mainloop_unlock(s->mainloop);
+    return AVERROR_EXTERNAL;
 }
 
 static int pulse_write_frame(AVFormatContext *h, int stream_index,
@@ -162,9 +406,13 @@ static int pulse_write_frame(AVFormatContext *h, int stream_index,
 static void pulse_get_output_timestamp(AVFormatContext *h, int stream, int64_t *dts, int64_t *wall)
 {
     PulseData *s = h->priv_data;
-    pa_usec_t latency = pa_simple_get_latency(s->pa, NULL);
+    pa_usec_t latency;
+    int neg;
+    pa_threaded_mainloop_lock(s->mainloop);
+    pa_stream_get_latency(s->stream, &latency, &neg);
+    pa_threaded_mainloop_unlock(s->mainloop);
     *wall = av_gettime();
-    *dts = s->timestamp - latency;
+    *dts = s->timestamp - (neg ? -latency : latency);
 }
 
 static int pulse_get_device_list(AVFormatContext *h, AVDeviceInfoList *device_list)
-- 
1.8.3.2



More information about the ffmpeg-devel mailing list