/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 | 0 | } |
89 | | |
90 | 0 | break; |
91 | | |
92 | 0 | case WAVE_FORMAT_ALAW: |
93 | 0 | return AFMT_A_LAW; |
94 | | |
95 | 0 | case WAVE_FORMAT_MULAW: |
96 | 0 | return AFMT_MU_LAW; |
97 | 0 | } |
98 | | |
99 | 0 | return 0; |
100 | 0 | } |
101 | | |
102 | | static BOOL audin_oss_format_supported(IAudinDevice* device, const AUDIO_FORMAT* format) |
103 | 0 | { |
104 | 0 | if (device == NULL || format == NULL) |
105 | 0 | return FALSE; |
106 | | |
107 | 0 | switch (format->wFormatTag) |
108 | 0 | { |
109 | 0 | case WAVE_FORMAT_PCM: |
110 | 0 | if (format->cbSize != 0 || format->nSamplesPerSec > 48000 || |
111 | 0 | (format->wBitsPerSample != 8 && format->wBitsPerSample != 16) || |
112 | 0 | (format->nChannels != 1 && format->nChannels != 2)) |
113 | 0 | return FALSE; |
114 | | |
115 | 0 | break; |
116 | | |
117 | 0 | default: |
118 | 0 | return FALSE; |
119 | 0 | } |
120 | | |
121 | 0 | return TRUE; |
122 | 0 | } |
123 | | |
124 | | /** |
125 | | * Function description |
126 | | * |
127 | | * @return 0 on success, otherwise a Win32 error code |
128 | | */ |
129 | | static UINT audin_oss_set_format(IAudinDevice* device, const AUDIO_FORMAT* format, |
130 | | UINT32 FramesPerPacket) |
131 | 0 | { |
132 | 0 | AudinOSSDevice* oss = (AudinOSSDevice*)device; |
133 | |
|
134 | 0 | if (device == NULL || format == NULL) |
135 | 0 | return ERROR_INVALID_PARAMETER; |
136 | | |
137 | 0 | oss->FramesPerPacket = FramesPerPacket; |
138 | 0 | oss->format = *format; |
139 | 0 | return CHANNEL_RC_OK; |
140 | 0 | } |
141 | | |
142 | | static DWORD WINAPI audin_oss_thread_func(LPVOID arg) |
143 | 0 | { |
144 | 0 | char dev_name[PATH_MAX] = "/dev/dsp"; |
145 | 0 | char mixer_name[PATH_MAX] = "/dev/mixer"; |
146 | 0 | int pcm_handle = -1; |
147 | 0 | int mixer_handle = 0; |
148 | 0 | BYTE* buffer = NULL; |
149 | 0 | unsigned long tmp = 0; |
150 | 0 | size_t buffer_size = 0; |
151 | 0 | AudinOSSDevice* oss = (AudinOSSDevice*)arg; |
152 | 0 | UINT error = 0; |
153 | 0 | DWORD status = 0; |
154 | |
|
155 | 0 | if (oss == NULL) |
156 | 0 | { |
157 | 0 | error = ERROR_INVALID_PARAMETER; |
158 | 0 | goto err_out; |
159 | 0 | } |
160 | | |
161 | 0 | if (oss->dev_unit != -1) |
162 | 0 | { |
163 | 0 | sprintf_s(dev_name, (PATH_MAX - 1), "/dev/dsp%i", oss->dev_unit); |
164 | 0 | sprintf_s(mixer_name, PATH_MAX - 1, "/dev/mixer%i", oss->dev_unit); |
165 | 0 | } |
166 | |
|
167 | 0 | WLog_INFO(TAG, "open: %s", dev_name); |
168 | |
|
169 | 0 | if ((pcm_handle = open(dev_name, O_RDONLY)) < 0) |
170 | 0 | { |
171 | 0 | OSS_LOG_ERR("sound dev open failed", errno); |
172 | 0 | error = ERROR_INTERNAL_ERROR; |
173 | 0 | goto err_out; |
174 | 0 | } |
175 | | |
176 | | /* Set rec volume to 100%. */ |
177 | 0 | if ((mixer_handle = open(mixer_name, O_RDWR)) < 0) |
178 | 0 | { |
179 | 0 | OSS_LOG_ERR("mixer open failed, not critical", errno); |
180 | 0 | } |
181 | 0 | else |
182 | 0 | { |
183 | 0 | tmp = (100 | (100 << 8)); |
184 | |
|
185 | 0 | if (ioctl(mixer_handle, MIXER_WRITE(SOUND_MIXER_MIC), &tmp) == -1) |
186 | 0 | OSS_LOG_ERR("WRITE_MIXER - SOUND_MIXER_MIC, not critical", errno); |
187 | |
|
188 | 0 | tmp = (100 | (100 << 8)); |
189 | |
|
190 | 0 | if (ioctl(mixer_handle, MIXER_WRITE(SOUND_MIXER_RECLEV), &tmp) == -1) |
191 | 0 | OSS_LOG_ERR("WRITE_MIXER - SOUND_MIXER_RECLEV, not critical", errno); |
192 | |
|
193 | 0 | close(mixer_handle); |
194 | 0 | } |
195 | |
|
196 | | #if 0 /* FreeBSD OSS implementation at this moment (2015.03) does not set PCM_CAP_INPUT flag. */ |
197 | | tmp = 0; |
198 | | |
199 | | if (ioctl(pcm_handle, SNDCTL_DSP_GETCAPS, &tmp) == -1) |
200 | | { |
201 | | OSS_LOG_ERR("SNDCTL_DSP_GETCAPS failed, try ignory", errno); |
202 | | } |
203 | | else if ((tmp & PCM_CAP_INPUT) == 0) |
204 | | { |
205 | | OSS_LOG_ERR("Device does not supports playback", EOPNOTSUPP); |
206 | | goto err_out; |
207 | | } |
208 | | |
209 | | #endif |
210 | | /* Set format. */ |
211 | 0 | tmp = audin_oss_get_format(&oss->format); |
212 | |
|
213 | 0 | if (ioctl(pcm_handle, SNDCTL_DSP_SETFMT, &tmp) == -1) |
214 | 0 | OSS_LOG_ERR("SNDCTL_DSP_SETFMT failed", errno); |
215 | |
|
216 | 0 | tmp = oss->format.nChannels; |
217 | |
|
218 | 0 | if (ioctl(pcm_handle, SNDCTL_DSP_CHANNELS, &tmp) == -1) |
219 | 0 | OSS_LOG_ERR("SNDCTL_DSP_CHANNELS failed", errno); |
220 | |
|
221 | 0 | tmp = oss->format.nSamplesPerSec; |
222 | |
|
223 | 0 | if (ioctl(pcm_handle, SNDCTL_DSP_SPEED, &tmp) == -1) |
224 | 0 | OSS_LOG_ERR("SNDCTL_DSP_SPEED failed", errno); |
225 | |
|
226 | 0 | tmp = oss->format.nBlockAlign; |
227 | |
|
228 | 0 | if (ioctl(pcm_handle, SNDCTL_DSP_SETFRAGMENT, &tmp) == -1) |
229 | 0 | OSS_LOG_ERR("SNDCTL_DSP_SETFRAGMENT failed", errno); |
230 | |
|
231 | 0 | buffer_size = |
232 | 0 | (1ull * oss->FramesPerPacket * oss->format.nChannels * (oss->format.wBitsPerSample / 8ull)); |
233 | 0 | buffer = (BYTE*)calloc((buffer_size + sizeof(void*)), sizeof(BYTE)); |
234 | |
|
235 | 0 | if (NULL == buffer) |
236 | 0 | { |
237 | 0 | OSS_LOG_ERR("malloc() fail", errno); |
238 | 0 | error = ERROR_NOT_ENOUGH_MEMORY; |
239 | 0 | goto err_out; |
240 | 0 | } |
241 | | |
242 | 0 | while (1) |
243 | 0 | { |
244 | 0 | SSIZE_T stmp = -1; |
245 | 0 | status = WaitForSingleObject(oss->stopEvent, 0); |
246 | |
|
247 | 0 | if (status == WAIT_FAILED) |
248 | 0 | { |
249 | 0 | error = GetLastError(); |
250 | 0 | WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error); |
251 | 0 | goto err_out; |
252 | 0 | } |
253 | | |
254 | 0 | if (status == WAIT_OBJECT_0) |
255 | 0 | break; |
256 | | |
257 | 0 | stmp = read(pcm_handle, buffer, buffer_size); |
258 | | |
259 | | /* Error happen. */ |
260 | 0 | if (stmp < 0) |
261 | 0 | { |
262 | 0 | OSS_LOG_ERR("read() error", errno); |
263 | 0 | continue; |
264 | 0 | } |
265 | | |
266 | 0 | if ((size_t)stmp < buffer_size) /* Not enouth data. */ |
267 | 0 | continue; |
268 | | |
269 | 0 | if ((error = oss->receive(&oss->format, buffer, buffer_size, oss->user_data))) |
270 | 0 | { |
271 | 0 | WLog_ERR(TAG, "oss->receive failed with error %" PRIu32 "", error); |
272 | 0 | break; |
273 | 0 | } |
274 | 0 | } |
275 | | |
276 | 0 | err_out: |
277 | |
|
278 | 0 | if (error && oss && oss->rdpcontext) |
279 | 0 | setChannelError(oss->rdpcontext, error, "audin_oss_thread_func reported an error"); |
280 | |
|
281 | 0 | if (pcm_handle != -1) |
282 | 0 | { |
283 | 0 | WLog_INFO(TAG, "close: %s", dev_name); |
284 | 0 | close(pcm_handle); |
285 | 0 | } |
286 | |
|
287 | 0 | free(buffer); |
288 | 0 | ExitThread(error); |
289 | 0 | return error; |
290 | 0 | } |
291 | | |
292 | | /** |
293 | | * Function description |
294 | | * |
295 | | * @return 0 on success, otherwise a Win32 error code |
296 | | */ |
297 | | static UINT audin_oss_open(IAudinDevice* device, AudinReceive receive, void* user_data) |
298 | 0 | { |
299 | 0 | AudinOSSDevice* oss = (AudinOSSDevice*)device; |
300 | 0 | oss->receive = receive; |
301 | 0 | oss->user_data = user_data; |
302 | |
|
303 | 0 | if (!(oss->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL))) |
304 | 0 | { |
305 | 0 | WLog_ERR(TAG, "CreateEvent failed!"); |
306 | 0 | return ERROR_INTERNAL_ERROR; |
307 | 0 | } |
308 | | |
309 | 0 | if (!(oss->thread = CreateThread(NULL, 0, audin_oss_thread_func, oss, 0, NULL))) |
310 | 0 | { |
311 | 0 | WLog_ERR(TAG, "CreateThread failed!"); |
312 | 0 | CloseHandle(oss->stopEvent); |
313 | 0 | oss->stopEvent = NULL; |
314 | 0 | return ERROR_INTERNAL_ERROR; |
315 | 0 | } |
316 | | |
317 | 0 | return CHANNEL_RC_OK; |
318 | 0 | } |
319 | | |
320 | | /** |
321 | | * Function description |
322 | | * |
323 | | * @return 0 on success, otherwise a Win32 error code |
324 | | */ |
325 | | static UINT audin_oss_close(IAudinDevice* device) |
326 | 0 | { |
327 | 0 | UINT error = 0; |
328 | 0 | AudinOSSDevice* oss = (AudinOSSDevice*)device; |
329 | |
|
330 | 0 | if (device == NULL) |
331 | 0 | return ERROR_INVALID_PARAMETER; |
332 | | |
333 | 0 | if (oss->stopEvent != NULL) |
334 | 0 | { |
335 | 0 | SetEvent(oss->stopEvent); |
336 | |
|
337 | 0 | if (WaitForSingleObject(oss->thread, INFINITE) == WAIT_FAILED) |
338 | 0 | { |
339 | 0 | error = GetLastError(); |
340 | 0 | WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error); |
341 | 0 | return error; |
342 | 0 | } |
343 | | |
344 | 0 | CloseHandle(oss->stopEvent); |
345 | 0 | oss->stopEvent = NULL; |
346 | 0 | CloseHandle(oss->thread); |
347 | 0 | oss->thread = NULL; |
348 | 0 | } |
349 | | |
350 | 0 | oss->receive = NULL; |
351 | 0 | oss->user_data = NULL; |
352 | 0 | return CHANNEL_RC_OK; |
353 | 0 | } |
354 | | |
355 | | /** |
356 | | * Function description |
357 | | * |
358 | | * @return 0 on success, otherwise a Win32 error code |
359 | | */ |
360 | | static UINT audin_oss_free(IAudinDevice* device) |
361 | 0 | { |
362 | 0 | AudinOSSDevice* oss = (AudinOSSDevice*)device; |
363 | 0 | UINT error = 0; |
364 | |
|
365 | 0 | if (device == NULL) |
366 | 0 | return ERROR_INVALID_PARAMETER; |
367 | | |
368 | 0 | if ((error = audin_oss_close(device))) |
369 | 0 | { |
370 | 0 | WLog_ERR(TAG, "audin_oss_close failed with error code %" PRIu32 "!", error); |
371 | 0 | } |
372 | |
|
373 | 0 | free(oss); |
374 | 0 | return CHANNEL_RC_OK; |
375 | 0 | } |
376 | | |
377 | | /** |
378 | | * Function description |
379 | | * |
380 | | * @return 0 on success, otherwise a Win32 error code |
381 | | */ |
382 | | static UINT audin_oss_parse_addin_args(AudinOSSDevice* device, const ADDIN_ARGV* args) |
383 | 0 | { |
384 | 0 | int status = 0; |
385 | 0 | char* str_num = NULL; |
386 | 0 | char* eptr = NULL; |
387 | 0 | DWORD flags = 0; |
388 | 0 | const COMMAND_LINE_ARGUMENT_A* arg = NULL; |
389 | 0 | AudinOSSDevice* oss = (AudinOSSDevice*)device; |
390 | 0 | COMMAND_LINE_ARGUMENT_A audin_oss_args[] = { { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", |
391 | 0 | NULL, NULL, -1, NULL, "audio device name" }, |
392 | 0 | { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL } }; |
393 | |
|
394 | 0 | flags = |
395 | 0 | COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD; |
396 | 0 | status = |
397 | 0 | CommandLineParseArgumentsA(args->argc, args->argv, audin_oss_args, flags, oss, NULL, NULL); |
398 | |
|
399 | 0 | if (status < 0) |
400 | 0 | return ERROR_INVALID_PARAMETER; |
401 | | |
402 | 0 | arg = audin_oss_args; |
403 | 0 | errno = 0; |
404 | |
|
405 | 0 | do |
406 | 0 | { |
407 | 0 | if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT)) |
408 | 0 | continue; |
409 | | |
410 | 0 | CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "dev") |
411 | 0 | { |
412 | 0 | str_num = _strdup(arg->Value); |
413 | |
|
414 | 0 | if (!str_num) |
415 | 0 | { |
416 | 0 | WLog_ERR(TAG, "_strdup failed!"); |
417 | 0 | return CHANNEL_RC_NO_MEMORY; |
418 | 0 | } |
419 | | |
420 | 0 | { |
421 | 0 | long val = strtol(str_num, &eptr, 10); |
422 | |
|
423 | 0 | if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX)) |
424 | 0 | { |
425 | 0 | free(str_num); |
426 | 0 | return CHANNEL_RC_NULL_DATA; |
427 | 0 | } |
428 | | |
429 | 0 | oss->dev_unit = (INT32)val; |
430 | 0 | } |
431 | | |
432 | 0 | if (oss->dev_unit < 0 || *eptr != '\0') |
433 | 0 | oss->dev_unit = -1; |
434 | |
|
435 | 0 | free(str_num); |
436 | 0 | } |
437 | 0 | CommandLineSwitchEnd(arg) |
438 | 0 | } while ((arg = CommandLineFindNextArgumentA(arg)) != NULL); |
439 | | |
440 | 0 | return CHANNEL_RC_OK; |
441 | 0 | } |
442 | | |
443 | | /** |
444 | | * Function description |
445 | | * |
446 | | * @return 0 on success, otherwise a Win32 error code |
447 | | */ |
448 | | FREERDP_ENTRY_POINT( |
449 | | UINT oss_freerdp_audin_client_subsystem_entry(PFREERDP_AUDIN_DEVICE_ENTRY_POINTS pEntryPoints)) |
450 | 0 | { |
451 | 0 | const ADDIN_ARGV* args = NULL; |
452 | 0 | AudinOSSDevice* oss = NULL; |
453 | 0 | UINT error = 0; |
454 | 0 | oss = (AudinOSSDevice*)calloc(1, sizeof(AudinOSSDevice)); |
455 | |
|
456 | 0 | if (!oss) |
457 | 0 | { |
458 | 0 | WLog_ERR(TAG, "calloc failed!"); |
459 | 0 | return CHANNEL_RC_NO_MEMORY; |
460 | 0 | } |
461 | | |
462 | 0 | oss->iface.Open = audin_oss_open; |
463 | 0 | oss->iface.FormatSupported = audin_oss_format_supported; |
464 | 0 | oss->iface.SetFormat = audin_oss_set_format; |
465 | 0 | oss->iface.Close = audin_oss_close; |
466 | 0 | oss->iface.Free = audin_oss_free; |
467 | 0 | oss->rdpcontext = pEntryPoints->rdpcontext; |
468 | 0 | oss->dev_unit = -1; |
469 | 0 | args = pEntryPoints->args; |
470 | |
|
471 | 0 | if ((error = audin_oss_parse_addin_args(oss, args))) |
472 | 0 | { |
473 | 0 | WLog_ERR(TAG, "audin_oss_parse_addin_args failed with errorcode %" PRIu32 "!", error); |
474 | 0 | goto error_out; |
475 | 0 | } |
476 | | |
477 | 0 | if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, (IAudinDevice*)oss))) |
478 | 0 | { |
479 | 0 | WLog_ERR(TAG, "RegisterAudinDevice failed with error %" PRIu32 "!", error); |
480 | 0 | goto error_out; |
481 | 0 | } |
482 | | |
483 | 0 | return CHANNEL_RC_OK; |
484 | 0 | error_out: |
485 | 0 | free(oss); |
486 | 0 | return error; |
487 | 0 | } |