/src/FreeRDP/channels/audin/client/oss/audin_oss.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Audio Input Redirection Virtual Channel - OSS implementation |
4 | | * |
5 | | * Copyright (c) 2015 Rozhuk Ivan <rozhuk.im@gmail.com> |
6 | | * Copyright 2015 Thincast Technologies GmbH |
7 | | * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com> |
8 | | * |
9 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | * you may not use this file except in compliance with the License. |
11 | | * You may obtain a copy of the License at |
12 | | * |
13 | | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | | * |
15 | | * Unless required by applicable law or agreed to in writing, software |
16 | | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | * See the License for the specific language governing permissions and |
19 | | * limitations under the License. |
20 | | */ |
21 | | |
22 | | #include <freerdp/config.h> |
23 | | |
24 | | #include <stdio.h> |
25 | | #include <stdlib.h> |
26 | | #include <string.h> |
27 | | |
28 | | #include <winpr/crt.h> |
29 | | #include <winpr/synch.h> |
30 | | #include <winpr/string.h> |
31 | | #include <winpr/thread.h> |
32 | | #include <winpr/cmdline.h> |
33 | | |
34 | | #include <err.h> |
35 | | #include <errno.h> |
36 | | #include <fcntl.h> |
37 | | #include <libgen.h> |
38 | | #include <limits.h> |
39 | | #include <unistd.h> |
40 | | #include <oss-includes.h> |
41 | | #include <sys/ioctl.h> |
42 | | |
43 | | #include <freerdp/freerdp.h> |
44 | | #include <freerdp/addin.h> |
45 | | #include <freerdp/channels/rdpsnd.h> |
46 | | |
47 | | #include "audin_main.h" |
48 | | |
49 | | typedef struct |
50 | | { |
51 | | IAudinDevice iface; |
52 | | |
53 | | HANDLE thread; |
54 | | HANDLE stopEvent; |
55 | | |
56 | | AUDIO_FORMAT format; |
57 | | UINT32 FramesPerPacket; |
58 | | int dev_unit; |
59 | | |
60 | | AudinReceive receive; |
61 | | void* user_data; |
62 | | |
63 | | rdpContext* rdpcontext; |
64 | | } AudinOSSDevice; |
65 | | |
66 | | static void OSS_LOG_ERR(const char* _text, int _error) |
67 | 0 | { |
68 | 0 | if ((_error) != 0) |
69 | 0 | { |
70 | 0 | char buffer[256] = { 0 }; |
71 | 0 | WLog_ERR(TAG, "%s: %i - %s\n", (_text), (_error), |
72 | 0 | winpr_strerror((_error), buffer, sizeof(buffer))); |
73 | 0 | } |
74 | 0 | } |
75 | | |
76 | | static UINT32 audin_oss_get_format(const AUDIO_FORMAT* format) |
77 | 0 | { |
78 | 0 | switch (format->wFormatTag) |
79 | 0 | { |
80 | 0 | case WAVE_FORMAT_PCM: |
81 | 0 | switch (format->wBitsPerSample) |
82 | 0 | { |
83 | 0 | case 8: |
84 | 0 | return AFMT_S8; |
85 | | |
86 | 0 | case 16: |
87 | 0 | return AFMT_S16_LE; |
88 | | |
89 | 0 | default: |
90 | 0 | break; |
91 | 0 | } |
92 | | |
93 | 0 | break; |
94 | 0 | default: |
95 | 0 | break; |
96 | 0 | } |
97 | | |
98 | 0 | return 0; |
99 | 0 | } |
100 | | |
101 | | static BOOL audin_oss_format_supported(IAudinDevice* device, const AUDIO_FORMAT* format) |
102 | 0 | { |
103 | 0 | if (device == NULL || format == NULL) |
104 | 0 | return FALSE; |
105 | | |
106 | 0 | switch (format->wFormatTag) |
107 | 0 | { |
108 | 0 | case WAVE_FORMAT_PCM: |
109 | 0 | if (format->cbSize != 0 || format->nSamplesPerSec > 48000 || |
110 | 0 | (format->wBitsPerSample != 8 && format->wBitsPerSample != 16) || |
111 | 0 | (format->nChannels != 1 && format->nChannels != 2)) |
112 | 0 | return FALSE; |
113 | | |
114 | 0 | break; |
115 | | |
116 | 0 | default: |
117 | 0 | return FALSE; |
118 | 0 | } |
119 | | |
120 | 0 | return TRUE; |
121 | 0 | } |
122 | | |
123 | | /** |
124 | | * Function description |
125 | | * |
126 | | * @return 0 on success, otherwise a Win32 error code |
127 | | */ |
128 | | static UINT audin_oss_set_format(IAudinDevice* device, const AUDIO_FORMAT* format, |
129 | | UINT32 FramesPerPacket) |
130 | 0 | { |
131 | 0 | AudinOSSDevice* oss = (AudinOSSDevice*)device; |
132 | |
|
133 | 0 | if (device == NULL || format == NULL) |
134 | 0 | return ERROR_INVALID_PARAMETER; |
135 | | |
136 | 0 | oss->FramesPerPacket = FramesPerPacket; |
137 | 0 | oss->format = *format; |
138 | 0 | return CHANNEL_RC_OK; |
139 | 0 | } |
140 | | |
141 | | static DWORD WINAPI audin_oss_thread_func(LPVOID arg) |
142 | 0 | { |
143 | 0 | char dev_name[PATH_MAX] = "/dev/dsp"; |
144 | 0 | char mixer_name[PATH_MAX] = "/dev/mixer"; |
145 | 0 | int pcm_handle = -1; |
146 | 0 | int mixer_handle = 0; |
147 | 0 | BYTE* buffer = NULL; |
148 | 0 | unsigned long tmp = 0; |
149 | 0 | size_t buffer_size = 0; |
150 | 0 | AudinOSSDevice* oss = (AudinOSSDevice*)arg; |
151 | 0 | UINT error = 0; |
152 | 0 | DWORD status = 0; |
153 | |
|
154 | 0 | if (oss == NULL) |
155 | 0 | { |
156 | 0 | error = ERROR_INVALID_PARAMETER; |
157 | 0 | goto err_out; |
158 | 0 | } |
159 | | |
160 | 0 | if (oss->dev_unit != -1) |
161 | 0 | { |
162 | 0 | (void)sprintf_s(dev_name, (PATH_MAX - 1), "/dev/dsp%i", oss->dev_unit); |
163 | 0 | (void)sprintf_s(mixer_name, PATH_MAX - 1, "/dev/mixer%i", oss->dev_unit); |
164 | 0 | } |
165 | |
|
166 | 0 | WLog_INFO(TAG, "open: %s", dev_name); |
167 | |
|
168 | 0 | if ((pcm_handle = open(dev_name, O_RDONLY)) < 0) |
169 | 0 | { |
170 | 0 | OSS_LOG_ERR("sound dev open failed", errno); |
171 | 0 | error = ERROR_INTERNAL_ERROR; |
172 | 0 | goto err_out; |
173 | 0 | } |
174 | | |
175 | | /* Set rec volume to 100%. */ |
176 | 0 | if ((mixer_handle = open(mixer_name, O_RDWR)) < 0) |
177 | 0 | { |
178 | 0 | OSS_LOG_ERR("mixer open failed, not critical", errno); |
179 | 0 | } |
180 | 0 | else |
181 | 0 | { |
182 | 0 | tmp = (100 | (100 << 8)); |
183 | |
|
184 | 0 | if (ioctl(mixer_handle, MIXER_WRITE(SOUND_MIXER_MIC), &tmp) == -1) |
185 | 0 | OSS_LOG_ERR("WRITE_MIXER - SOUND_MIXER_MIC, not critical", errno); |
186 | |
|
187 | 0 | tmp = (100 | (100 << 8)); |
188 | |
|
189 | 0 | if (ioctl(mixer_handle, MIXER_WRITE(SOUND_MIXER_RECLEV), &tmp) == -1) |
190 | 0 | OSS_LOG_ERR("WRITE_MIXER - SOUND_MIXER_RECLEV, not critical", errno); |
191 | |
|
192 | 0 | close(mixer_handle); |
193 | 0 | } |
194 | | |
195 | | /* Set format. */ |
196 | 0 | tmp = audin_oss_get_format(&oss->format); |
197 | |
|
198 | 0 | if (ioctl(pcm_handle, SNDCTL_DSP_SETFMT, &tmp) == -1) |
199 | 0 | OSS_LOG_ERR("SNDCTL_DSP_SETFMT failed", errno); |
200 | |
|
201 | 0 | tmp = oss->format.nChannels; |
202 | |
|
203 | 0 | if (ioctl(pcm_handle, SNDCTL_DSP_CHANNELS, &tmp) == -1) |
204 | 0 | OSS_LOG_ERR("SNDCTL_DSP_CHANNELS failed", errno); |
205 | |
|
206 | 0 | tmp = oss->format.nSamplesPerSec; |
207 | |
|
208 | 0 | if (ioctl(pcm_handle, SNDCTL_DSP_SPEED, &tmp) == -1) |
209 | 0 | OSS_LOG_ERR("SNDCTL_DSP_SPEED failed", errno); |
210 | |
|
211 | 0 | tmp = oss->format.nBlockAlign; |
212 | |
|
213 | 0 | if (ioctl(pcm_handle, SNDCTL_DSP_SETFRAGMENT, &tmp) == -1) |
214 | 0 | OSS_LOG_ERR("SNDCTL_DSP_SETFRAGMENT failed", errno); |
215 | |
|
216 | 0 | buffer_size = |
217 | 0 | (1ull * oss->FramesPerPacket * oss->format.nChannels * (oss->format.wBitsPerSample / 8ull)); |
218 | 0 | buffer = (BYTE*)calloc((buffer_size + sizeof(void*)), sizeof(BYTE)); |
219 | |
|
220 | 0 | if (NULL == buffer) |
221 | 0 | { |
222 | 0 | OSS_LOG_ERR("malloc() fail", errno); |
223 | 0 | error = ERROR_NOT_ENOUGH_MEMORY; |
224 | 0 | goto err_out; |
225 | 0 | } |
226 | | |
227 | 0 | while (1) |
228 | 0 | { |
229 | 0 | SSIZE_T stmp = -1; |
230 | 0 | status = WaitForSingleObject(oss->stopEvent, 0); |
231 | |
|
232 | 0 | if (status == WAIT_FAILED) |
233 | 0 | { |
234 | 0 | error = GetLastError(); |
235 | 0 | WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error); |
236 | 0 | goto err_out; |
237 | 0 | } |
238 | | |
239 | 0 | if (status == WAIT_OBJECT_0) |
240 | 0 | break; |
241 | | |
242 | 0 | stmp = read(pcm_handle, buffer, buffer_size); |
243 | | |
244 | | /* Error happen. */ |
245 | 0 | if (stmp < 0) |
246 | 0 | { |
247 | 0 | OSS_LOG_ERR("read() error", errno); |
248 | 0 | continue; |
249 | 0 | } |
250 | | |
251 | 0 | if ((size_t)stmp < buffer_size) /* Not enough data. */ |
252 | 0 | continue; |
253 | | |
254 | 0 | if ((error = oss->receive(&oss->format, buffer, buffer_size, oss->user_data))) |
255 | 0 | { |
256 | 0 | WLog_ERR(TAG, "oss->receive failed with error %" PRIu32 "", error); |
257 | 0 | break; |
258 | 0 | } |
259 | 0 | } |
260 | | |
261 | 0 | err_out: |
262 | |
|
263 | 0 | if (error && oss && oss->rdpcontext) |
264 | 0 | setChannelError(oss->rdpcontext, error, "audin_oss_thread_func reported an error"); |
265 | |
|
266 | 0 | if (pcm_handle != -1) |
267 | 0 | { |
268 | 0 | WLog_INFO(TAG, "close: %s", dev_name); |
269 | 0 | close(pcm_handle); |
270 | 0 | } |
271 | |
|
272 | 0 | free(buffer); |
273 | 0 | ExitThread(error); |
274 | 0 | return error; |
275 | 0 | } |
276 | | |
277 | | /** |
278 | | * Function description |
279 | | * |
280 | | * @return 0 on success, otherwise a Win32 error code |
281 | | */ |
282 | | static UINT audin_oss_open(IAudinDevice* device, AudinReceive receive, void* user_data) |
283 | 0 | { |
284 | 0 | AudinOSSDevice* oss = (AudinOSSDevice*)device; |
285 | 0 | oss->receive = receive; |
286 | 0 | oss->user_data = user_data; |
287 | |
|
288 | 0 | if (!(oss->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL))) |
289 | 0 | { |
290 | 0 | WLog_ERR(TAG, "CreateEvent failed!"); |
291 | 0 | return ERROR_INTERNAL_ERROR; |
292 | 0 | } |
293 | | |
294 | 0 | if (!(oss->thread = CreateThread(NULL, 0, audin_oss_thread_func, oss, 0, NULL))) |
295 | 0 | { |
296 | 0 | WLog_ERR(TAG, "CreateThread failed!"); |
297 | 0 | (void)CloseHandle(oss->stopEvent); |
298 | 0 | oss->stopEvent = NULL; |
299 | 0 | return ERROR_INTERNAL_ERROR; |
300 | 0 | } |
301 | | |
302 | 0 | return CHANNEL_RC_OK; |
303 | 0 | } |
304 | | |
305 | | /** |
306 | | * Function description |
307 | | * |
308 | | * @return 0 on success, otherwise a Win32 error code |
309 | | */ |
310 | | static UINT audin_oss_close(IAudinDevice* device) |
311 | 0 | { |
312 | 0 | UINT error = 0; |
313 | 0 | AudinOSSDevice* oss = (AudinOSSDevice*)device; |
314 | |
|
315 | 0 | if (device == NULL) |
316 | 0 | return ERROR_INVALID_PARAMETER; |
317 | | |
318 | 0 | if (oss->stopEvent != NULL) |
319 | 0 | { |
320 | 0 | (void)SetEvent(oss->stopEvent); |
321 | |
|
322 | 0 | if (WaitForSingleObject(oss->thread, INFINITE) == WAIT_FAILED) |
323 | 0 | { |
324 | 0 | error = GetLastError(); |
325 | 0 | WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error); |
326 | 0 | return error; |
327 | 0 | } |
328 | | |
329 | 0 | (void)CloseHandle(oss->stopEvent); |
330 | 0 | oss->stopEvent = NULL; |
331 | 0 | (void)CloseHandle(oss->thread); |
332 | 0 | oss->thread = NULL; |
333 | 0 | } |
334 | | |
335 | 0 | oss->receive = NULL; |
336 | 0 | oss->user_data = NULL; |
337 | 0 | return CHANNEL_RC_OK; |
338 | 0 | } |
339 | | |
340 | | /** |
341 | | * Function description |
342 | | * |
343 | | * @return 0 on success, otherwise a Win32 error code |
344 | | */ |
345 | | static UINT audin_oss_free(IAudinDevice* device) |
346 | 0 | { |
347 | 0 | AudinOSSDevice* oss = (AudinOSSDevice*)device; |
348 | 0 | UINT error = 0; |
349 | |
|
350 | 0 | if (device == NULL) |
351 | 0 | return ERROR_INVALID_PARAMETER; |
352 | | |
353 | 0 | if ((error = audin_oss_close(device))) |
354 | 0 | { |
355 | 0 | WLog_ERR(TAG, "audin_oss_close failed with error code %" PRIu32 "!", error); |
356 | 0 | } |
357 | |
|
358 | 0 | free(oss); |
359 | 0 | return CHANNEL_RC_OK; |
360 | 0 | } |
361 | | |
362 | | /** |
363 | | * Function description |
364 | | * |
365 | | * @return 0 on success, otherwise a Win32 error code |
366 | | */ |
367 | | static UINT audin_oss_parse_addin_args(AudinOSSDevice* device, const ADDIN_ARGV* args) |
368 | 0 | { |
369 | 0 | int status = 0; |
370 | 0 | char* str_num = NULL; |
371 | 0 | char* eptr = NULL; |
372 | 0 | DWORD flags = 0; |
373 | 0 | const COMMAND_LINE_ARGUMENT_A* arg = NULL; |
374 | 0 | AudinOSSDevice* oss = device; |
375 | 0 | COMMAND_LINE_ARGUMENT_A audin_oss_args[] = { { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", |
376 | 0 | NULL, NULL, -1, NULL, "audio device name" }, |
377 | 0 | { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL } }; |
378 | |
|
379 | 0 | flags = |
380 | 0 | COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD; |
381 | 0 | status = |
382 | 0 | CommandLineParseArgumentsA(args->argc, args->argv, audin_oss_args, flags, oss, NULL, NULL); |
383 | |
|
384 | 0 | if (status < 0) |
385 | 0 | return ERROR_INVALID_PARAMETER; |
386 | | |
387 | 0 | arg = audin_oss_args; |
388 | 0 | errno = 0; |
389 | |
|
390 | 0 | do |
391 | 0 | { |
392 | 0 | if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT)) |
393 | 0 | continue; |
394 | | |
395 | 0 | CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "dev") |
396 | 0 | { |
397 | 0 | str_num = _strdup(arg->Value); |
398 | |
|
399 | 0 | if (!str_num) |
400 | 0 | { |
401 | 0 | WLog_ERR(TAG, "_strdup failed!"); |
402 | 0 | return CHANNEL_RC_NO_MEMORY; |
403 | 0 | } |
404 | | |
405 | 0 | { |
406 | 0 | long val = strtol(str_num, &eptr, 10); |
407 | |
|
408 | 0 | if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX)) |
409 | 0 | { |
410 | 0 | free(str_num); |
411 | 0 | return CHANNEL_RC_NULL_DATA; |
412 | 0 | } |
413 | | |
414 | 0 | oss->dev_unit = (INT32)val; |
415 | 0 | } |
416 | | |
417 | 0 | if (oss->dev_unit < 0 || *eptr != '\0') |
418 | 0 | oss->dev_unit = -1; |
419 | |
|
420 | 0 | free(str_num); |
421 | 0 | } |
422 | 0 | CommandLineSwitchEnd(arg) |
423 | 0 | } while ((arg = CommandLineFindNextArgumentA(arg)) != NULL); |
424 | | |
425 | 0 | return CHANNEL_RC_OK; |
426 | 0 | } |
427 | | |
428 | | /** |
429 | | * Function description |
430 | | * |
431 | | * @return 0 on success, otherwise a Win32 error code |
432 | | */ |
433 | | FREERDP_ENTRY_POINT(UINT VCAPITYPE oss_freerdp_audin_client_subsystem_entry( |
434 | | PFREERDP_AUDIN_DEVICE_ENTRY_POINTS pEntryPoints)) |
435 | 0 | { |
436 | 0 | const ADDIN_ARGV* args = NULL; |
437 | 0 | AudinOSSDevice* oss = NULL; |
438 | 0 | UINT error = 0; |
439 | 0 | oss = (AudinOSSDevice*)calloc(1, sizeof(AudinOSSDevice)); |
440 | |
|
441 | 0 | if (!oss) |
442 | 0 | { |
443 | 0 | WLog_ERR(TAG, "calloc failed!"); |
444 | 0 | return CHANNEL_RC_NO_MEMORY; |
445 | 0 | } |
446 | | |
447 | 0 | oss->iface.Open = audin_oss_open; |
448 | 0 | oss->iface.FormatSupported = audin_oss_format_supported; |
449 | 0 | oss->iface.SetFormat = audin_oss_set_format; |
450 | 0 | oss->iface.Close = audin_oss_close; |
451 | 0 | oss->iface.Free = audin_oss_free; |
452 | 0 | oss->rdpcontext = pEntryPoints->rdpcontext; |
453 | 0 | oss->dev_unit = -1; |
454 | 0 | args = pEntryPoints->args; |
455 | |
|
456 | 0 | if ((error = audin_oss_parse_addin_args(oss, args))) |
457 | 0 | { |
458 | 0 | WLog_ERR(TAG, "audin_oss_parse_addin_args failed with errorcode %" PRIu32 "!", error); |
459 | 0 | goto error_out; |
460 | 0 | } |
461 | | |
462 | 0 | if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, (IAudinDevice*)oss))) |
463 | 0 | { |
464 | 0 | WLog_ERR(TAG, "RegisterAudinDevice failed with error %" PRIu32 "!", error); |
465 | 0 | goto error_out; |
466 | 0 | } |
467 | | |
468 | 0 | return CHANNEL_RC_OK; |
469 | 0 | error_out: |
470 | 0 | free(oss); |
471 | 0 | return error; |
472 | 0 | } |