]> git.sesse.net Git - ffmpeg/blobdiff - libavdevice/pulse.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavdevice / pulse.c
index 66b62cc99dbba83f257caaa43b4c448c63678e8b..0b60651d08d3c927f8bf5a618314a6be55514bdb 100644 (file)
 
 /**
  * @file
- * Pulseaudio input
+ * PulseAudio input using the simple API.
  * @author Luca Barbato <lu_zero@gentoo.org>
  *
- * This avdevice decoder allows to capture audio from a Pulseaudio device using
- * the simple api.
  */
 
 #include <pulse/simple.h>
@@ -48,6 +46,7 @@ typedef struct PulseData {
     int  fragment_size;
     pa_simple *s;
     int64_t pts;
+    int64_t frame_duration;
 } PulseData;
 
 static pa_sample_format_t codec_id_to_pulse_format(int codec_id) {
@@ -95,9 +94,9 @@ static av_cold int pulse_read_header(AVFormatContext *s,
         device = s->filename;
 
     pd->s = pa_simple_new(pd->server, pd->name,
-                      PA_STREAM_RECORD,
-                      device, pd->stream_name, &ss,
-                      NULL, &attr, &ret);
+                          PA_STREAM_RECORD,
+                          device, pd->stream_name, &ss,
+                          NULL, &attr, &ret);
 
     if (!pd->s) {
         av_log(s, AV_LOG_ERROR, "pa_simple_new failed: %s\n",
@@ -112,6 +111,8 @@ static av_cold int pulse_read_header(AVFormatContext *s,
     av_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
 
     pd->pts = AV_NOPTS_VALUE;
+    pd->frame_duration = (pd->frame_size * 1000000LL * 8) /
+        (pd->sample_rate * pd->channels * av_get_bits_per_sample(codec_id));
 
     return 0;
 }
@@ -121,8 +122,6 @@ static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
     PulseData *pd  = s->priv_data;
     int res;
     pa_usec_t latency;
-    uint64_t frame_duration =
-        (pd->frame_size*1000000LL)/(pd->sample_rate * pd->channels);
 
     if (av_new_packet(pkt, pd->frame_size) < 0) {
         return AVERROR(ENOMEM);
@@ -145,10 +144,10 @@ static int pulse_read_packet(AVFormatContext *s, AVPacket *pkt)
         pd->pts = -latency;
     }
 
-    pd->pts += frame_duration;
-
     pkt->pts = pd->pts;
 
+    pd->pts += pd->frame_duration;
+
     return 0;
 }
 
@@ -163,20 +162,13 @@ static av_cold int pulse_close(AVFormatContext *s)
 #define D AV_OPT_FLAG_DECODING_PARAM
 
 static const AVOption options[] = {
-    { "server",        "pulse server name",
-        OFFSET(server),        AV_OPT_TYPE_STRING, {.str = NULL},     0, 0, D },
-    { "name",          "application name",
-        OFFSET(name),          AV_OPT_TYPE_STRING, {.str = "libav"},  0, 0, D },
-    { "stream_name",   "stream description",
-        OFFSET(stream_name),   AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
-    { "sample_rate",   "",
-        OFFSET(sample_rate),   AV_OPT_TYPE_INT,    {.dbl = 48000},    1, INT_MAX, D },
-    { "channels",      "",
-        OFFSET(channels),      AV_OPT_TYPE_INT,    {.dbl = 2},        1, INT_MAX, D },
-    { "frame_size",    "",
-        OFFSET(frame_size),    AV_OPT_TYPE_INT,    {.dbl = 1024},     1, INT_MAX, D },
-    { "fragment_size", "buffering size, affects latency and cpu usage",
-        OFFSET(fragment_size), AV_OPT_TYPE_INT,    {.dbl = -1},      -1, INT_MAX, D },
+    { "server",        "pulse server name",                              OFFSET(server),        AV_OPT_TYPE_STRING, {.str = NULL},     0, 0, D },
+    { "name",          "application name",                               OFFSET(name),          AV_OPT_TYPE_STRING, {.str = LIBAVFORMAT_IDENT},  0, 0, D },
+    { "stream_name",   "stream description",                             OFFSET(stream_name),   AV_OPT_TYPE_STRING, {.str = "record"}, 0, 0, D },
+    { "sample_rate",   "sample rate in Hz",                              OFFSET(sample_rate),   AV_OPT_TYPE_INT,    {.dbl = 48000},    1, INT_MAX, D },
+    { "channels",      "number of audio channels",                       OFFSET(channels),      AV_OPT_TYPE_INT,    {.dbl = 2},        1, INT_MAX, D },
+    { "frame_size",    "number of bytes per frame",                      OFFSET(frame_size),    AV_OPT_TYPE_INT,    {.dbl = 1024},     1, INT_MAX, D },
+    { "fragment_size", "buffering size, affects latency and cpu usage",  OFFSET(fragment_size), AV_OPT_TYPE_INT,    {.dbl = -1},      -1, INT_MAX, D },
     { NULL },
 };