Browse code

sndio support for playback and record

Signed-off-by: Anton Khirnov <anton@khirnov.net>

Brad authored on 2011/03/27 08:03:41
Showing 11 changed files
... ...
@@ -79,6 +79,7 @@ version <next>:
79 79
 - Bink version 'b' audio and video decoder
80 80
 - Bitmap Brothers JV playback system
81 81
 - Apple HTTP Live Streaming protocol handler
82
+- sndio support for playback and record
82 83
 
83 84
 
84 85
 version 0.6:
... ...
@@ -1094,6 +1094,7 @@ HAVE_LIST="
1094 1094
     sdl
1095 1095
     sdl_video_size
1096 1096
     setmode
1097
+    sndio_h
1097 1098
     socklen_t
1098 1099
     soundcard_h
1099 1100
     poll_h
... ...
@@ -1433,6 +1434,8 @@ jack_indev_deps="jack_jack_h"
1433 1433
 libdc1394_indev_deps="libdc1394"
1434 1434
 oss_indev_deps_any="soundcard_h sys_soundcard_h"
1435 1435
 oss_outdev_deps_any="soundcard_h sys_soundcard_h"
1436
+sndio_indev_deps="sndio_h"
1437
+sndio_outdev_deps="sndio_h"
1436 1438
 v4l_indev_deps="linux_videodev_h"
1437 1439
 v4l2_indev_deps_any="linux_videodev2_h sys_videoio_h"
1438 1440
 vfwcap_indev_deps="capCreateCaptureWindow vfwcap_defines"
... ...
@@ -2912,6 +2915,7 @@ check_cpp_condition vfw.h "WM_CAP_DRIVER_CONNECT > WM_USER" && enable vfwcap_def
2912 2912
   check_header dev/video/bktr/ioctl_bt848.h; } ||
2913 2913
 check_header dev/ic/bt8xx.h
2914 2914
 
2915
+check_header sndio.h
2915 2916
 check_header sys/soundcard.h
2916 2917
 check_header soundcard.h
2917 2918
 
... ...
@@ -2919,6 +2923,8 @@ enabled_any alsa_indev alsa_outdev && check_lib2 alsa/asoundlib.h snd_pcm_htimes
2919 2919
 
2920 2920
 enabled jack_indev && check_lib2 jack/jack.h jack_client_open -ljack
2921 2921
 
2922
+enabled_any sndio_indev sndio_outdev && check_lib2 sndio.h sio_open -lsndio
2923
+
2922 2924
 enabled x11grab                         &&
2923 2925
 check_header X11/Xlib.h                 &&
2924 2926
 check_header X11/extensions/XShm.h      &&
... ...
@@ -129,6 +129,23 @@ ffmpeg -f oss -i /dev/dsp /tmp/oss.wav
129 129
 For more information about OSS see:
130 130
 @url{http://manuals.opensound.com/usersguide/dsp.html}
131 131
 
132
+@section sndio
133
+
134
+sndio input device.
135
+
136
+To enable this input device during configuration you need libsndio
137
+installed on your system.
138
+
139
+The filename to provide to the input device is the device node
140
+representing the sndio input device, and is usually set to
141
+@file{/dev/audio0}.
142
+
143
+For example to grab from @file{/dev/audio0} using @file{ffmpeg} use the
144
+command:
145
+@example
146
+ffmpeg -f sndio -i /dev/audio0 /tmp/oss.wav
147
+@end example
148
+
132 149
 @section video4linux and video4linux2
133 150
 
134 151
 Video4Linux and Video4Linux2 input video devices.
... ...
@@ -26,4 +26,8 @@ ALSA (Advanced Linux Sound Architecture) output device.
26 26
 
27 27
 OSS (Open Sound System) output device.
28 28
 
29
+@section sndio
30
+
31
+sndio audio output device.
32
+
29 33
 @c man end OUTPUT DEVICES
... ...
@@ -17,6 +17,8 @@ OBJS-$(CONFIG_DV1394_INDEV)              += dv1394.o
17 17
 OBJS-$(CONFIG_JACK_INDEV)                += jack_audio.o
18 18
 OBJS-$(CONFIG_OSS_INDEV)                 += oss_audio.o
19 19
 OBJS-$(CONFIG_OSS_OUTDEV)                += oss_audio.o
20
+OBJS-$(CONFIG_SNDIO_INDEV)               += sndio_common.o sndio_dec.o
21
+OBJS-$(CONFIG_SNDIO_OUTDEV)              += sndio_common.o sndio_enc.o
20 22
 OBJS-$(CONFIG_V4L2_INDEV)                += v4l2.o
21 23
 OBJS-$(CONFIG_V4L_INDEV)                 += v4l.o
22 24
 OBJS-$(CONFIG_VFWCAP_INDEV)              += vfwcap.o
... ...
@@ -26,5 +28,6 @@ OBJS-$(CONFIG_X11_GRAB_DEVICE_INDEV)     += x11grab.o
26 26
 OBJS-$(CONFIG_LIBDC1394_INDEV)           += libdc1394.o
27 27
 
28 28
 SKIPHEADERS-$(HAVE_ALSA_ASOUNDLIB_H)     += alsa-audio.h
29
+SKIPHEADERS-$(HAVE_SNDIO_H)              += sndio_common.h
29 30
 
30 31
 include $(SUBDIR)../subdir.mak
... ...
@@ -44,6 +44,7 @@ void avdevice_register_all(void)
44 44
     REGISTER_INDEV    (DV1394, dv1394);
45 45
     REGISTER_INDEV    (JACK, jack);
46 46
     REGISTER_INOUTDEV (OSS, oss);
47
+    REGISTER_INOUTDEV (SNDIO, sndio);
47 48
     REGISTER_INDEV    (V4L2, v4l2);
48 49
     REGISTER_INDEV    (V4L, v4l);
49 50
     REGISTER_INDEV    (VFWCAP, vfwcap);
... ...
@@ -22,8 +22,8 @@
22 22
 #include "libavutil/avutil.h"
23 23
 
24 24
 #define LIBAVDEVICE_VERSION_MAJOR 52
25
-#define LIBAVDEVICE_VERSION_MINOR  2
26
-#define LIBAVDEVICE_VERSION_MICRO  3
25
+#define LIBAVDEVICE_VERSION_MINOR  3
26
+#define LIBAVDEVICE_VERSION_MICRO  0
27 27
 
28 28
 #define LIBAVDEVICE_VERSION_INT AV_VERSION_INT(LIBAVDEVICE_VERSION_MAJOR, \
29 29
                                                LIBAVDEVICE_VERSION_MINOR, \
30 30
new file mode 100644
... ...
@@ -0,0 +1,120 @@
0
+/*
1
+ * sndio play and grab interface
2
+ * Copyright (c) 2010 Jacob Meuser
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav 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
+ * Libav 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 Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include <stdint.h>
22
+#include <sndio.h>
23
+
24
+#include "libavformat/avformat.h"
25
+
26
+#include "sndio_common.h"
27
+
28
+static inline void movecb(void *addr, int delta)
29
+{
30
+    SndioData *s = addr;
31
+
32
+    s->hwpos += delta * s->channels * s->bps;
33
+}
34
+
35
+av_cold int ff_sndio_open(AVFormatContext *s1, int is_output,
36
+                          const char *audio_device)
37
+{
38
+    SndioData *s = s1->priv_data;
39
+    struct sio_hdl *hdl;
40
+    struct sio_par par;
41
+
42
+    hdl = sio_open(audio_device, is_output ? SIO_PLAY : SIO_REC, 0);
43
+    if (!hdl) {
44
+        av_log(s1, AV_LOG_ERROR, "Could not open sndio device\n");
45
+        return AVERROR(EIO);
46
+    }
47
+
48
+    sio_initpar(&par);
49
+
50
+    par.bits = 16;
51
+    par.sig  = 1;
52
+    par.le   = SIO_LE_NATIVE;
53
+
54
+    if (is_output)
55
+        par.pchan = s->channels;
56
+    else
57
+        par.rchan = s->channels;
58
+    par.rate = s->sample_rate;
59
+
60
+    if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
61
+        av_log(s1, AV_LOG_ERROR, "Impossible to set sndio parameters, "
62
+               "channels: %d sample rate: %d\n", s->channels, s->sample_rate);
63
+        goto fail;
64
+    }
65
+
66
+    if (par.bits != 16 || par.sig != 1 || par.le != SIO_LE_NATIVE ||
67
+        (is_output  && (par.pchan != s->channels)) ||
68
+        (!is_output && (par.rchan != s->channels)) ||
69
+        (par.rate != s->sample_rate)) {
70
+        av_log(s1, AV_LOG_ERROR, "Could not set appropriate sndio parameters, "
71
+               "channels: %d sample rate: %d\n", s->channels, s->sample_rate);
72
+        goto fail;
73
+    }
74
+
75
+    s->buffer_size = par.round * par.bps *
76
+                     (is_output ? par.pchan : par.rchan);
77
+
78
+    if (is_output) {
79
+        s->buffer = av_malloc(s->buffer_size);
80
+        if (!s->buffer) {
81
+            av_log(s1, AV_LOG_ERROR, "Could not allocate buffer\n");
82
+            goto fail;
83
+        }
84
+    }
85
+
86
+    s->codec_id    = par.le ? CODEC_ID_PCM_S16LE : CODEC_ID_PCM_S16BE;
87
+    s->channels    = is_output ? par.pchan : par.rchan;
88
+    s->sample_rate = par.rate;
89
+    s->bps         = par.bps;
90
+
91
+    sio_onmove(hdl, movecb, s);
92
+
93
+    if (!sio_start(hdl)) {
94
+        av_log(s1, AV_LOG_ERROR, "Could not start sndio\n");
95
+        goto fail;
96
+    }
97
+
98
+    s->hdl = hdl;
99
+
100
+    return 0;
101
+
102
+fail:
103
+    av_freep(&s->buffer);
104
+
105
+    if (hdl)
106
+        sio_close(hdl);
107
+
108
+    return AVERROR(EIO);
109
+}
110
+
111
+int ff_sndio_close(SndioData *s)
112
+{
113
+    av_freep(&s->buffer);
114
+
115
+    if (s->hdl)
116
+        sio_close(s->hdl);
117
+
118
+    return 0;
119
+}
0 120
new file mode 100644
... ...
@@ -0,0 +1,46 @@
0
+/*
1
+ * sndio play and grab interface
2
+ * Copyright (c) 2010 Jacob Meuser
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav 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
+ * Libav 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 Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#ifndef AVDEVICE_SNDIO_COMMON_H
22
+#define AVDEVICE_SNDIO_COMMON_H
23
+
24
+#include <stdint.h>
25
+#include <sndio.h>
26
+
27
+#include "libavformat/avformat.h"
28
+
29
+typedef struct {
30
+    struct sio_hdl *hdl;
31
+    enum CodecID codec_id;
32
+    int64_t hwpos;
33
+    int64_t softpos;
34
+    uint8_t *buffer;
35
+    int bps;
36
+    int buffer_size;
37
+    int buffer_offset;
38
+    int channels;
39
+    int sample_rate;
40
+} SndioData;
41
+
42
+int ff_sndio_open(AVFormatContext *s1, int is_output, const char *audio_device);
43
+int ff_sndio_close(SndioData *s);
44
+
45
+#endif /* AVDEVICE_SNDIO_COMMON_H */
0 46
new file mode 100644
... ...
@@ -0,0 +1,108 @@
0
+/*
1
+ * sndio play and grab interface
2
+ * Copyright (c) 2010 Jacob Meuser
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav 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
+ * Libav 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 Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include <stdint.h>
22
+#include <sndio.h>
23
+
24
+#include "libavformat/avformat.h"
25
+
26
+#include "sndio_common.h"
27
+
28
+static av_cold int audio_read_header(AVFormatContext *s1,
29
+                                     AVFormatParameters *ap)
30
+{
31
+    SndioData *s = s1->priv_data;
32
+    AVStream *st;
33
+    int ret;
34
+
35
+    if (ap->sample_rate <= 0 || ap->channels <= 0)
36
+        return AVERROR(EINVAL);
37
+
38
+    st = av_new_stream(s1, 0);
39
+    if (!st)
40
+        return AVERROR(ENOMEM);
41
+
42
+    s->sample_rate = ap->sample_rate;
43
+    s->channels    = ap->channels;
44
+
45
+    ret = ff_sndio_open(s1, 0, s1->filename);
46
+    if (ret < 0)
47
+        return ret;
48
+
49
+    /* take real parameters */
50
+    st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
51
+    st->codec->codec_id    = s->codec_id;
52
+    st->codec->sample_rate = s->sample_rate;
53
+    st->codec->channels    = s->channels;
54
+
55
+    av_set_pts_info(st, 64, 1, 1000000);  /* 64 bits pts in us */
56
+
57
+    return 0;
58
+}
59
+
60
+static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
61
+{
62
+    SndioData *s = s1->priv_data;
63
+    int64_t bdelay, cur_time;
64
+    int ret;
65
+
66
+    if ((ret = av_new_packet(pkt, s->buffer_size)) < 0)
67
+        return ret;
68
+
69
+    ret = sio_read(s->hdl, pkt->data, pkt->size);
70
+    if (ret == 0 || sio_eof(s->hdl)) {
71
+        av_free_packet(pkt);
72
+        return AVERROR_EOF;
73
+    }
74
+
75
+    pkt->size   = ret;
76
+    s->softpos += ret;
77
+
78
+    /* compute pts of the start of the packet */
79
+    cur_time = av_gettime();
80
+
81
+    bdelay = ret + s->hwpos - s->softpos;
82
+
83
+    /* convert to pts */
84
+    pkt->pts = cur_time - ((bdelay * 1000000) /
85
+        (s->bps * s->channels * s->sample_rate));
86
+
87
+    return 0;
88
+}
89
+
90
+static av_cold int audio_read_close(AVFormatContext *s1)
91
+{
92
+    SndioData *s = s1->priv_data;
93
+
94
+    ff_sndio_close(s);
95
+
96
+    return 0;
97
+}
98
+
99
+AVInputFormat ff_sndio_demuxer = {
100
+    .name           = "sndio",
101
+    .long_name      = NULL_IF_CONFIG_SMALL("sndio audio capture"),
102
+    .priv_data_size = sizeof(SndioData),
103
+    .read_header    = audio_read_header,
104
+    .read_packet    = audio_read_packet,
105
+    .read_close     = audio_read_close,
106
+    .flags          = AVFMT_NOFILE,
107
+};
0 108
new file mode 100644
... ...
@@ -0,0 +1,95 @@
0
+/*
1
+ * sndio play and grab interface
2
+ * Copyright (c) 2010 Jacob Meuser
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav 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
+ * Libav 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 Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include <stdint.h>
22
+#include <sndio.h>
23
+
24
+#include "libavformat/avformat.h"
25
+
26
+#include "sndio_common.h"
27
+
28
+static av_cold int audio_write_header(AVFormatContext *s1)
29
+{
30
+    SndioData *s = s1->priv_data;
31
+    AVStream *st;
32
+    int ret;
33
+
34
+    st             = s1->streams[0];
35
+    s->sample_rate = st->codec->sample_rate;
36
+    s->channels    = st->codec->channels;
37
+
38
+    ret = ff_sndio_open(s1, 1, s1->filename);
39
+
40
+    return ret;
41
+}
42
+
43
+static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
44
+{
45
+    SndioData *s = s1->priv_data;
46
+    uint8_t *buf= pkt->data;
47
+    int size = pkt->size;
48
+    int len, ret;
49
+
50
+    while (size > 0) {
51
+        len = s->buffer_size - s->buffer_offset;
52
+        if (len > size)
53
+            len = size;
54
+        memcpy(s->buffer + s->buffer_offset, buf, len);
55
+        buf  += len;
56
+        size -= len;
57
+        s->buffer_offset += len;
58
+        if (s->buffer_offset >= s->buffer_size) {
59
+            ret = sio_write(s->hdl, s->buffer, s->buffer_size);
60
+            if (ret == 0 || sio_eof(s->hdl))
61
+                return AVERROR(EIO);
62
+            s->softpos      += ret;
63
+            s->buffer_offset = 0;
64
+        }
65
+    }
66
+
67
+    return 0;
68
+}
69
+
70
+static int audio_write_trailer(AVFormatContext *s1)
71
+{
72
+    SndioData *s = s1->priv_data;
73
+
74
+    sio_write(s->hdl, s->buffer, s->buffer_offset);
75
+
76
+    ff_sndio_close(s);
77
+
78
+    return 0;
79
+}
80
+
81
+AVOutputFormat ff_sndio_muxer = {
82
+    .name           = "sndio",
83
+    .long_name      = NULL_IF_CONFIG_SMALL("sndio audio playback"),
84
+    .priv_data_size = sizeof(SndioData),
85
+    /* XXX: we make the assumption that the soundcard accepts this format */
86
+    /* XXX: find better solution with "preinit" method, needed also in
87
+       other formats */
88
+    .audio_codec    = AV_NE(CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE),
89
+    .video_codec    = CODEC_ID_NONE,
90
+    .write_header   = audio_write_header,
91
+    .write_packet   = audio_write_packet,
92
+    .write_trailer  = audio_write_trailer,
93
+    .flags          = AVFMT_NOFILE,
94
+};