2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2016 Alex Teaca <iateaca@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #ifndef WITHOUT_CAPSICUM
34 #include <sys/capsicum.h>
35 #include <capsicum_helpers.h>
41 #include <sys/ioctl.h>
52 * Audio Player internal data structures
63 * Audio Player module function definitions
67 * audio_init - initialize an instance of audio player
68 * @dev_name - the backend sound device used to play / capture
69 * @dir - dir = 1 for write mode, dir = 0 for read mode
72 audio_init(const char *dev_name, uint8_t dir)
74 struct audio *aud = NULL;
75 #ifndef WITHOUT_CAPSICUM
77 cap_ioctl_t cmds[] = {
78 SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT, SNDCTL_DSP_CHANNELS,
81 SNDCTL_DSP_GETOSPACE, SNDCTL_DSP_GETISPACE,
88 aud = calloc(1, sizeof(*aud));
92 if (strlen(dev_name) < sizeof(aud->dev_name))
93 memcpy(aud->dev_name, dev_name, strlen(dev_name) + 1);
95 DPRINTF("dev_name too big\n");
102 aud->fd = open(aud->dev_name, aud->dir ? O_WRONLY : O_RDONLY, 0);
104 DPRINTF("Failed to open dev: %s, errno: %d\n",
105 aud->dev_name, errno);
110 #ifndef WITHOUT_CAPSICUM
111 cap_rights_init(&rights, CAP_IOCTL, CAP_READ, CAP_WRITE);
112 if (caph_rights_limit(aud->fd, &rights) == -1)
113 errx(EX_OSERR, "Unable to apply rights for sandbox");
114 if (caph_ioctls_limit(aud->fd, cmds, nitems(cmds)) == -1)
115 errx(EX_OSERR, "Unable to limit ioctl rights for sandbox");
122 * audio_set_params - reset the sound device and set the audio params
123 * @aud - the audio player to be configured
124 * @params - the audio parameters to be set
127 audio_set_params(struct audio *aud, struct audio_params *params)
130 int format, channels, rate;
139 if ((audio_fd = aud->fd) < 0) {
140 DPRINTF("Incorrect audio device descriptor for %s\n",
145 /* Reset the device if it was previously opened */
147 err = ioctl(audio_fd, SNDCTL_DSP_RESET, NULL);
149 DPRINTF("Failed to reset fd: %d, errno: %d\n",
156 /* Set the Format (Bits per Sample) */
157 format = params->format;
158 err = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format);
160 DPRINTF("Fail to set fmt: 0x%x errno: %d\n",
161 params->format, errno);
165 /* The device does not support the requested audio format */
166 if (format != params->format) {
167 DPRINTF("Mismatch format: 0x%x params->format: 0x%x\n",
168 format, params->format);
172 /* Set the Number of Channels */
173 channels = params->channels;
174 err = ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &channels);
176 DPRINTF("Fail to set channels: %d errno: %d\n",
177 params->channels, errno);
181 /* The device does not support the requested no. of channels */
182 if (channels != params->channels) {
183 DPRINTF("Mismatch channels: %d params->channels: %d\n",
184 channels, params->channels);
188 /* Set the Sample Rate / Speed */
190 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &rate);
192 DPRINTF("Fail to set speed: %d errno: %d\n",
193 params->rate, errno);
197 /* The device does not support the requested rate / speed */
198 if (rate != params->rate) {
199 DPRINTF("Mismatch rate: %d params->rate: %d\n",
205 err = ioctl(audio_fd, aud->dir ? SNDCTL_DSP_GETOSPACE :
206 SNDCTL_DSP_GETISPACE, &info);
208 DPRINTF("Fail to get audio buf info errno: %d\n", errno);
211 DPRINTF("fragstotal: 0x%x fragsize: 0x%x\n",
212 info.fragstotal, info.fragsize);
218 * audio_playback - plays samples to the sound device using blocking operations
219 * @aud - the audio player used to play the samples
220 * @buf - the buffer containing the samples
221 * @count - the number of bytes in buffer
224 audio_playback(struct audio *aud, const void *buf, size_t count)
227 ssize_t len = 0, total = 0;
234 assert(audio_fd != -1);
237 while (total < count) {
238 len = write(audio_fd, buf + total, count - total);
240 DPRINTF("Fail to write to fd: %d, errno: %d\n",
252 * audio_record - records samples from the sound device using
253 * blocking operations.
254 * @aud - the audio player used to capture the samples
255 * @buf - the buffer to receive the samples
256 * @count - the number of bytes to capture in buffer
257 * Returns -1 on error and 0 on success
260 audio_record(struct audio *aud, void *buf, size_t count)
263 ssize_t len = 0, total = 0;
270 assert(audio_fd != -1);
273 while (total < count) {
274 len = read(audio_fd, buf + total, count - total);
276 DPRINTF("Fail to write to fd: %d, errno: %d\n",