2 * Copyright (c) 2016 Alex Teaca <iateaca@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #ifndef WITHOUT_CAPSICUM
32 #include <sys/capsicum.h>
33 #include <capsicum_helpers.h>
39 #include <sys/ioctl.h>
50 * Audio Player internal data structures
61 * Audio Player module function definitions
65 * audio_init - initialize an instance of audio player
66 * @dev_name - the backend sound device used to play / capture
67 * @dir - dir = 1 for write mode, dir = 0 for read mode
70 audio_init(const char *dev_name, uint8_t dir)
72 struct audio *aud = NULL;
73 #ifndef WITHOUT_CAPSICUM
75 cap_ioctl_t cmds[] = {
76 SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT, SNDCTL_DSP_CHANNELS,
79 SNDCTL_DSP_GETOSPACE, SNDCTL_DSP_GETISPACE,
86 aud = calloc(1, sizeof(*aud));
90 if (strlen(dev_name) < sizeof(aud->dev_name))
91 memcpy(aud->dev_name, dev_name, strlen(dev_name) + 1);
93 DPRINTF("dev_name too big\n");
100 aud->fd = open(aud->dev_name, aud->dir ? O_WRONLY : O_RDONLY, 0);
102 DPRINTF("Failed to open dev: %s, errno: %d\n",
103 aud->dev_name, errno);
107 #ifndef WITHOUT_CAPSICUM
108 cap_rights_init(&rights, CAP_IOCTL, CAP_READ, CAP_WRITE);
109 if (caph_rights_limit(aud->fd, &rights) == -1)
110 errx(EX_OSERR, "Unable to apply rights for sandbox");
111 if (caph_ioctls_limit(aud->fd, cmds, nitems(cmds)) == -1)
112 errx(EX_OSERR, "Unable to limit ioctl rights for sandbox");
119 * audio_set_params - reset the sound device and set the audio params
120 * @aud - the audio player to be configured
121 * @params - the audio parameters to be set
124 audio_set_params(struct audio *aud, struct audio_params *params)
127 int format, channels, rate;
136 if ((audio_fd = aud->fd) < 0) {
137 DPRINTF("Incorrect audio device descriptor for %s\n",
142 /* Reset the device if it was previously opened */
144 err = ioctl(audio_fd, SNDCTL_DSP_RESET, NULL);
146 DPRINTF("Failed to reset fd: %d, errno: %d\n",
153 /* Set the Format (Bits per Sample) */
154 format = params->format;
155 err = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format);
157 DPRINTF("Fail to set fmt: 0x%x errno: %d\n",
158 params->format, errno);
162 /* The device does not support the requested audio format */
163 if (format != params->format) {
164 DPRINTF("Mismatch format: 0x%x params->format: 0x%x\n",
165 format, params->format);
169 /* Set the Number of Channels */
170 channels = params->channels;
171 err = ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &channels);
173 DPRINTF("Fail to set channels: %d errno: %d\n",
174 params->channels, errno);
178 /* The device does not support the requested no. of channels */
179 if (channels != params->channels) {
180 DPRINTF("Mismatch channels: %d params->channels: %d\n",
181 channels, params->channels);
185 /* Set the Sample Rate / Speed */
187 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &rate);
189 DPRINTF("Fail to set speed: %d errno: %d\n",
190 params->rate, errno);
194 /* The device does not support the requested rate / speed */
195 if (rate != params->rate) {
196 DPRINTF("Mismatch rate: %d params->rate: %d\n",
202 err = ioctl(audio_fd, aud->dir ? SNDCTL_DSP_GETOSPACE :
203 SNDCTL_DSP_GETISPACE, &info);
205 DPRINTF("Fail to get audio buf info errno: %d\n", errno);
208 DPRINTF("fragstotal: 0x%x fragsize: 0x%x\n",
209 info.fragstotal, info.fragsize);
215 * audio_playback - plays samples to the sound device using blocking operations
216 * @aud - the audio player used to play the samples
217 * @buf - the buffer containing the samples
218 * @count - the number of bytes in buffer
221 audio_playback(struct audio *aud, const void *buf, size_t count)
224 ssize_t len = 0, total = 0;
231 assert(audio_fd != -1);
234 while (total < count) {
235 len = write(audio_fd, buf + total, count - total);
237 DPRINTF("Fail to write to fd: %d, errno: %d\n",
249 * audio_record - records samples from the sound device using
250 * blocking operations.
251 * @aud - the audio player used to capture the samples
252 * @buf - the buffer to receive the samples
253 * @count - the number of bytes to capture in buffer
254 * Returns -1 on error and 0 on success
257 audio_record(struct audio *aud, void *buf, size_t count)
260 ssize_t len = 0, total = 0;
267 assert(audio_fd != -1);
270 while (total < count) {
271 len = read(audio_fd, buf + total, count - total);
273 DPRINTF("Fail to write to fd: %d, errno: %d\n",