audio.c (84659b24) audio.c (154972af)
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2016 Alex Teaca <iateaca@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 78 unchanged lines hidden (view full) ---

87
88 aud = calloc(1, sizeof(*aud));
89 if (!aud)
90 return NULL;
91
92 if (strlen(dev_name) < sizeof(aud->dev_name))
93 memcpy(aud->dev_name, dev_name, strlen(dev_name) + 1);
94 else {
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2016 Alex Teaca <iateaca@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 78 unchanged lines hidden (view full) ---

87
88 aud = calloc(1, sizeof(*aud));
89 if (!aud)
90 return NULL;
91
92 if (strlen(dev_name) < sizeof(aud->dev_name))
93 memcpy(aud->dev_name, dev_name, strlen(dev_name) + 1);
94 else {
95 DPRINTF("dev_name too big\n");
95 DPRINTF("dev_name too big");
96 free(aud);
97 return NULL;
98 }
99
100 aud->dir = dir;
101
102 aud->fd = open(aud->dev_name, aud->dir ? O_WRONLY : O_RDONLY, 0);
103 if (aud->fd == -1) {
96 free(aud);
97 return NULL;
98 }
99
100 aud->dir = dir;
101
102 aud->fd = open(aud->dev_name, aud->dir ? O_WRONLY : O_RDONLY, 0);
103 if (aud->fd == -1) {
104 DPRINTF("Failed to open dev: %s, errno: %d\n",
104 DPRINTF("Failed to open dev: %s, errno: %d",
105 aud->dev_name, errno);
106 free(aud);
107 return (NULL);
108 }
109
110#ifndef WITHOUT_CAPSICUM
111 cap_rights_init(&rights, CAP_IOCTL, CAP_READ, CAP_WRITE);
112 if (caph_rights_limit(aud->fd, &rights) == -1)

--- 19 unchanged lines hidden (view full) ---

132#if DEBUG_HDA == 1
133 audio_buf_info info;
134#endif
135
136 assert(aud);
137 assert(params);
138
139 if ((audio_fd = aud->fd) < 0) {
105 aud->dev_name, errno);
106 free(aud);
107 return (NULL);
108 }
109
110#ifndef WITHOUT_CAPSICUM
111 cap_rights_init(&rights, CAP_IOCTL, CAP_READ, CAP_WRITE);
112 if (caph_rights_limit(aud->fd, &rights) == -1)

--- 19 unchanged lines hidden (view full) ---

132#if DEBUG_HDA == 1
133 audio_buf_info info;
134#endif
135
136 assert(aud);
137 assert(params);
138
139 if ((audio_fd = aud->fd) < 0) {
140 DPRINTF("Incorrect audio device descriptor for %s\n",
140 DPRINTF("Incorrect audio device descriptor for %s",
141 aud->dev_name);
142 return (-1);
143 }
144
145 /* Reset the device if it was previously opened */
146 if (aud->inited) {
147 err = ioctl(audio_fd, SNDCTL_DSP_RESET, NULL);
148 if (err == -1) {
141 aud->dev_name);
142 return (-1);
143 }
144
145 /* Reset the device if it was previously opened */
146 if (aud->inited) {
147 err = ioctl(audio_fd, SNDCTL_DSP_RESET, NULL);
148 if (err == -1) {
149 DPRINTF("Failed to reset fd: %d, errno: %d\n",
149 DPRINTF("Failed to reset fd: %d, errno: %d",
150 aud->fd, errno);
151 return (-1);
152 }
153 } else
154 aud->inited = 1;
155
156 /* Set the Format (Bits per Sample) */
157 format = params->format;
158 err = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format);
159 if (err == -1) {
150 aud->fd, errno);
151 return (-1);
152 }
153 } else
154 aud->inited = 1;
155
156 /* Set the Format (Bits per Sample) */
157 format = params->format;
158 err = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &format);
159 if (err == -1) {
160 DPRINTF("Fail to set fmt: 0x%x errno: %d\n",
160 DPRINTF("Fail to set fmt: 0x%x errno: %d",
161 params->format, errno);
162 return -1;
163 }
164
165 /* The device does not support the requested audio format */
166 if (format != params->format) {
161 params->format, errno);
162 return -1;
163 }
164
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",
167 DPRINTF("Mismatch format: 0x%x params->format: 0x%x",
168 format, params->format);
169 return -1;
170 }
171
172 /* Set the Number of Channels */
173 channels = params->channels;
174 err = ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &channels);
175 if (err == -1) {
168 format, params->format);
169 return -1;
170 }
171
172 /* Set the Number of Channels */
173 channels = params->channels;
174 err = ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &channels);
175 if (err == -1) {
176 DPRINTF("Fail to set channels: %d errno: %d\n",
176 DPRINTF("Fail to set channels: %d errno: %d",
177 params->channels, errno);
178 return -1;
179 }
180
181 /* The device does not support the requested no. of channels */
182 if (channels != params->channels) {
177 params->channels, errno);
178 return -1;
179 }
180
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",
183 DPRINTF("Mismatch channels: %d params->channels: %d",
184 channels, params->channels);
185 return -1;
186 }
187
188 /* Set the Sample Rate / Speed */
189 rate = params->rate;
190 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &rate);
191 if (err == -1) {
184 channels, params->channels);
185 return -1;
186 }
187
188 /* Set the Sample Rate / Speed */
189 rate = params->rate;
190 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &rate);
191 if (err == -1) {
192 DPRINTF("Fail to set speed: %d errno: %d\n",
192 DPRINTF("Fail to set speed: %d errno: %d",
193 params->rate, errno);
194 return -1;
195 }
196
197 /* The device does not support the requested rate / speed */
198 if (rate != params->rate) {
193 params->rate, errno);
194 return -1;
195 }
196
197 /* The device does not support the requested rate / speed */
198 if (rate != params->rate) {
199 DPRINTF("Mismatch rate: %d params->rate: %d\n",
199 DPRINTF("Mismatch rate: %d params->rate: %d",
200 rate, params->rate);
201 return -1;
202 }
203
204#if DEBUG_HDA == 1
205 err = ioctl(audio_fd, aud->dir ? SNDCTL_DSP_GETOSPACE :
206 SNDCTL_DSP_GETISPACE, &info);
207 if (err == -1) {
200 rate, params->rate);
201 return -1;
202 }
203
204#if DEBUG_HDA == 1
205 err = ioctl(audio_fd, aud->dir ? SNDCTL_DSP_GETOSPACE :
206 SNDCTL_DSP_GETISPACE, &info);
207 if (err == -1) {
208 DPRINTF("Fail to get audio buf info errno: %d\n", errno);
208 DPRINTF("Fail to get audio buf info errno: %d", errno);
209 return -1;
210 }
209 return -1;
210 }
211 DPRINTF("fragstotal: 0x%x fragsize: 0x%x\n",
211 DPRINTF("fragstotal: 0x%x fragsize: 0x%x",
212 info.fragstotal, info.fragsize);
213#endif
214 return 0;
215}
216
217/*
218 * audio_playback - plays samples to the sound device using blocking operations
219 * @aud - the audio player used to play the samples

--- 12 unchanged lines hidden (view full) ---

232
233 audio_fd = aud->fd;
234 assert(audio_fd != -1);
235
236 total = 0;
237 while (total < count) {
238 len = write(audio_fd, buf + total, count - total);
239 if (len == -1) {
212 info.fragstotal, info.fragsize);
213#endif
214 return 0;
215}
216
217/*
218 * audio_playback - plays samples to the sound device using blocking operations
219 * @aud - the audio player used to play the samples

--- 12 unchanged lines hidden (view full) ---

232
233 audio_fd = aud->fd;
234 assert(audio_fd != -1);
235
236 total = 0;
237 while (total < count) {
238 len = write(audio_fd, buf + total, count - total);
239 if (len == -1) {
240 DPRINTF("Fail to write to fd: %d, errno: %d\n",
240 DPRINTF("Fail to write to fd: %d, errno: %d",
241 audio_fd, errno);
242 return -1;
243 }
244
245 total += len;
246 }
247
248 return 0;

--- 19 unchanged lines hidden (view full) ---

268
269 audio_fd = aud->fd;
270 assert(audio_fd != -1);
271
272 total = 0;
273 while (total < count) {
274 len = read(audio_fd, buf + total, count - total);
275 if (len == -1) {
241 audio_fd, errno);
242 return -1;
243 }
244
245 total += len;
246 }
247
248 return 0;

--- 19 unchanged lines hidden (view full) ---

268
269 audio_fd = aud->fd;
270 assert(audio_fd != -1);
271
272 total = 0;
273 while (total < count) {
274 len = read(audio_fd, buf + total, count - total);
275 if (len == -1) {
276 DPRINTF("Fail to write to fd: %d, errno: %d\n",
276 DPRINTF("Fail to write to fd: %d, errno: %d",
277 audio_fd, errno);
278 return -1;
279 }
280
281 total += len;
282 }
283
284 return 0;
285}
277 audio_fd, errno);
278 return -1;
279 }
280
281 total += len;
282 }
283
284 return 0;
285}