diff --git a/share/examples/Makefile b/share/examples/Makefile --- a/share/examples/Makefile +++ b/share/examples/Makefile @@ -321,6 +321,7 @@ SE_SOUND= \ kqueue.c \ midi.c \ + mmap.c \ oss.h \ poll.c \ select.c \ diff --git a/share/examples/sound/mmap.c b/share/examples/sound/mmap.c new file mode 100644 --- /dev/null +++ b/share/examples/sound/mmap.c @@ -0,0 +1,124 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2025 Goran Mekić + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include + +#include "oss.h" + +int +main(int argc, char *argv[]) +{ + int rc, bytes, kq; + uint8_t *buf; + oss_syncgroup sync_group = {0, 0, {0}}; + struct kevent event = {}; + struct config config_in = { + .device = "/dev/dsp", + .mode = O_RDONLY | O_EXCL | O_NONBLOCK, + .format = AFMT_S32_NE, + .sample_rate = 48000, + .mmap = 1, + }; + struct config config_out = { + .device = "/dev/dsp", + .mode = O_WRONLY | O_EXCL | O_NONBLOCK, + .format = AFMT_S32_NE, + .sample_rate = 48000, + .mmap = 1, + }; + + oss_init(&config_in); + oss_init(&config_out); + + /* Allocate and mmap buffers */ + bytes = config_in.buffer_info.bytes < config_out.buffer_info.bytes + ? config_in.buffer_info.bytes + : config_out.buffer_info.bytes; + buf = malloc(bytes); + + /* Initialize kqueue */ + kq = kqueue(); + if (kq == -1) + err(1, "Failed to allocate kqueue"); + EV_SET(&event, config_in.fd, EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, 0); + event.udata = &config_in; + if (kevent(kq, &event, 1, NULL, 0, NULL) < 0) + err(1, "Failed to register kevent"); + EV_SET(&event, config_out.fd, EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, 0); + event.udata = &config_out; + if (kevent(kq, &event, 1, NULL, 0, NULL) < 0) + err(1, "Failed to register kevent"); + + /* Configure and start sync group */ + sync_group.mode = PCM_ENABLE_INPUT; + if (ioctl(config_in.fd, SNDCTL_DSP_SYNCGROUP, &sync_group) < 0) + err(1, "Failed to add input to syncgroup"); + sync_group.mode = PCM_ENABLE_OUTPUT; + if (ioctl(config_out.fd, SNDCTL_DSP_SYNCGROUP, &sync_group) < 0) + err(1, "Failed to add output to syncgroup"); + if (ioctl(config_in.fd, SNDCTL_DSP_SYNCSTART, &sync_group.id) < 0) + err(1, "Starting sync group failed"); + + /* Main loop */ + for (;;) { + rc = kevent(kq, NULL, 0, &event, 1, NULL); + if (rc == -1 || event.data == 0) { + warn("Event error"); + break; + } + if (event.flags & EV_ERROR) { + warn("Event error: %s", strerror(event.data)); + break; + } + if (event.udata == &config_out) + memcpy(config_out.buf, buf, bytes); + else + memcpy(buf, config_in.buf, bytes); + } + + /* Cleanup */ + free(buf); + if (munmap(config_in.buf, config_in.buffer_info.bytes) != 0) + err(1, "Memory unmap failed"); + config_in.buf = NULL; + if (munmap(config_out.buf, config_out.buffer_info.bytes) != 0) + err(1, "Memory unmap failed"); + config_out.buf = NULL; + EV_SET(&event, config_in.fd, EVFILT_READ, EV_DELETE, 0, 0, 0); + if (kevent(kq, &event, 1, NULL, 0, NULL) < 0) + err(1, "Failed to unregister input kevent"); + EV_SET(&event, config_out.fd, EVFILT_WRITE, EV_DELETE, 0, 0, 0); + if (kevent(kq, &event, 1, NULL, 0, NULL) < 0) + err(1, "Failed to unregister input kevent"); + close(kq); + close(config_in.fd); + close(config_out.fd); + + return (0); +} diff --git a/share/examples/sound/oss.h b/share/examples/sound/oss.h --- a/share/examples/sound/oss.h +++ b/share/examples/sound/oss.h @@ -25,6 +25,7 @@ * SUCH DAMAGE. */ +#include #include #include @@ -84,7 +85,7 @@ oss_init(struct config *config) { unsigned long request = SNDCTL_DSP_GETOSPACE; - int tmp = 0; + int tmp = 0, prot = 0; if ((config->fd = open(config->device, config->mode)) < 0) err(1, "Error opening the device %s", config->device); @@ -194,7 +195,6 @@ } config->sample_count = config->buffer_info.bytes / config->sample_size; config->chsamples = config->sample_count / config->audio_info.max_channels; - config->buf = malloc(config->buffer_info.bytes); printf("bytes: %d, fragments: %d, fragsize: %d, fragstotal: %d, " "samples: %d\n", @@ -202,21 +202,34 @@ config->buffer_info.fragsize, config->buffer_info.fragstotal, config->sample_count); - /* Set the trigger */ + /* Set trigger direction and mmap protection */ switch (config->mode & O_ACCMODE) { case O_RDONLY: tmp = PCM_ENABLE_INPUT; + prot = PROT_READ; break; case O_WRONLY: tmp = PCM_ENABLE_OUTPUT; + prot = PROT_WRITE; break; case O_RDWR: tmp = PCM_ENABLE_INPUT | PCM_ENABLE_OUTPUT; + prot = PROT_READ | PROT_WRITE; break; default: errx(1, "Invalid mode %d", config->mode); break; } + + /* Map or allocate the buffer */ + if (config->mmap) { + config->buf = mmap(NULL, config->buffer_info.bytes, prot, MAP_SHARED, config->fd, 0); + if (config->buf == MAP_FAILED) + err(1, "Memory map failed"); + } else + config->buf = malloc(config->buffer_info.bytes); + + /* Set the trigger */ if (ioctl(config->fd, SNDCTL_DSP_SETTRIGGER, &tmp) < 0) err(1, "Failed to set trigger"); }