2 * sndio play and grab interface
3 * Copyright (c) 2010 Jacob Meuser
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "sndio_common.h"
29 static inline void movecb(void *addr, int delta)
33 s->hwpos += delta * s->channels * s->bps;
36 av_cold int ff_sndio_open(AVFormatContext *s1, int is_output,
37 const char *audio_device)
39 SndioData *s = s1->priv_data;
43 hdl = sio_open(audio_device, is_output ? SIO_PLAY : SIO_REC, 0);
45 av_log(s1, AV_LOG_ERROR, "Could not open sndio device\n");
53 par.le = SIO_LE_NATIVE;
56 par.pchan = s->channels;
58 par.rchan = s->channels;
59 par.rate = s->sample_rate;
61 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
62 av_log(s1, AV_LOG_ERROR, "Impossible to set sndio parameters, "
63 "channels: %d sample rate: %d\n", s->channels, s->sample_rate);
67 if (par.bits != 16 || par.sig != 1 ||
68 (is_output && (par.pchan != s->channels)) ||
69 (!is_output && (par.rchan != s->channels)) ||
70 (par.rate != s->sample_rate)) {
71 av_log(s1, AV_LOG_ERROR, "Could not set appropriate sndio parameters, "
72 "channels: %d sample rate: %d\n", s->channels, s->sample_rate);
76 s->buffer_size = par.round * par.bps *
77 (is_output ? par.pchan : par.rchan);
80 s->buffer = av_malloc(s->buffer_size);
82 av_log(s1, AV_LOG_ERROR, "Could not allocate buffer\n");
87 s->codec_id = par.le ? AV_CODEC_ID_PCM_S16LE : AV_CODEC_ID_PCM_S16BE;
88 s->channels = is_output ? par.pchan : par.rchan;
89 s->sample_rate = par.rate;
92 sio_onmove(hdl, movecb, s);
94 if (!sio_start(hdl)) {
95 av_log(s1, AV_LOG_ERROR, "Could not start sndio\n");
104 av_freep(&s->buffer);
112 int ff_sndio_close(SndioData *s)
114 av_freep(&s->buffer);