Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F162584108
D53749.id166400.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
5 KB
Referenced Files
None
Subscribers
None
D53749.id166400.diff
View Options
Index: share/examples/sound/mmap.c
===================================================================
--- /dev/null
+++ share/examples/sound/mmap.c
@@ -0,0 +1,149 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 Goran Mekić
+ * Copyright (c) 2024 The FreeBSD Foundation
+ *
+ * Portions of this software were developed by Christos Margiolis
+ * <christos@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
+ *
+ * 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 <sys/mman.h>
+#include <sys/event.h>
+
+#include <fcntl.h>
+
+#include "oss.h"
+
+static int
+add_to_sync_group(int fd, int group, int out) {
+ oss_syncgroup sync_group = {0, 0, {0}};
+ sync_group.id = group;
+ if (out)
+ sync_group.mode |= PCM_ENABLE_OUTPUT;
+ else
+ sync_group.mode |= PCM_ENABLE_INPUT;
+ if (ioctl(fd, SNDCTL_DSP_SYNCGROUP, &sync_group) == 0)
+ return sync_group.id;
+ err(1, "Failed to add %d to syncgroup", sync_group.id);
+}
+
+static void
+start_sync_group(int fd, int group) {
+ int err = ioctl(fd, SNDCTL_DSP_SYNCSTART, &group);
+ if (err != 0)
+ errx(1, "Starting sync group failed");
+}
+
+int
+main(int argc, char *argv[])
+{
+ int rc, bytes, kq;
+ int group = 0;
+ uint8_t *buf;
+ 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);
+ config_in.buf = mmap(NULL, config_in.buffer_info.bytes, PROT_READ, MAP_SHARED, config_in.fd, 0);
+ if (config_in.buf == MAP_FAILED)
+ err(1, "Memory map failed");
+ config_out.buf = mmap(NULL, config_out.buffer_info.bytes, PROT_WRITE, MAP_SHARED, config_out.fd, 0);
+ if (config_out.buf == MAP_FAILED)
+ err(1, "Memory map failed");
+
+ /* 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");
+
+ group = add_to_sync_group(config_in.fd, group, 0);
+ group = add_to_sync_group(config_out.fd, group, 1);
+ start_sync_group(config_in.fd, group);
+
+ /* 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);
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Jul 15, 7:08 PM (4 h, 30 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
35110861
Default Alt Text
D53749.id166400.diff (5 KB)
Attached To
Mode
D53749: sound examples: Add mmap example
Attached
Detach File
Event Timeline
Log In to Comment