/src/FreeRDP/channels/audin/client/oss/audin_oss.c
Line | Count | Source |
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] = WINPR_C_ARRAY_INIT; |
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 == nullptr || format == nullptr) |
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 == nullptr || format == nullptr) |
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 | int pcm_handle = -1; |
145 | 0 | BYTE* buffer = nullptr; |
146 | 0 | unsigned long tmp = 0; |
147 | 0 | size_t buffer_size = 0; |
148 | 0 | AudinOSSDevice* oss = (AudinOSSDevice*)arg; |
149 | 0 | UINT error = 0; |
150 | 0 | DWORD status = 0; |
151 | |
|
152 | 0 | if (oss == nullptr) |
153 | 0 | { |
154 | 0 | error = ERROR_INVALID_PARAMETER; |
155 | 0 | goto err_out; |
156 | 0 | } |
157 | | |
158 | 0 | if (oss->dev_unit != -1) |
159 | 0 | (void)sprintf_s(dev_name, (PATH_MAX - 1), "/dev/dsp%i", oss->dev_unit); |
160 | |
|
161 | 0 | WLog_INFO(TAG, "open: %s", dev_name); |
162 | |
|
163 | 0 | if ((pcm_handle = open(dev_name, O_RDONLY)) < 0) |
164 | 0 | { |
165 | 0 | OSS_LOG_ERR("sound dev open failed", errno); |
166 | 0 | error = ERROR_INTERNAL_ERROR; |
167 | 0 | goto err_out; |
168 | 0 | } |
169 | | |
170 | | /* Set format. */ |
171 | 0 | tmp = audin_oss_get_format(&oss->format); |
172 | |
|
173 | 0 | if (ioctl(pcm_handle, SNDCTL_DSP_SETFMT, &tmp) == -1) |
174 | 0 | OSS_LOG_ERR("SNDCTL_DSP_SETFMT failed", errno); |
175 | |
|
176 | 0 | tmp = oss->format.nChannels; |
177 | |
|
178 | 0 | if (ioctl(pcm_handle, SNDCTL_DSP_CHANNELS, &tmp) == -1) |
179 | 0 | OSS_LOG_ERR("SNDCTL_DSP_CHANNELS failed", errno); |
180 | |
|
181 | 0 | tmp = oss->format.nSamplesPerSec; |
182 | |
|
183 | 0 | if (ioctl(pcm_handle, SNDCTL_DSP_SPEED, &tmp) == -1) |
184 | 0 | OSS_LOG_ERR("SNDCTL_DSP_SPEED failed", errno); |
185 | |
|
186 | 0 | tmp = oss->format.nBlockAlign; |
187 | |
|
188 | 0 | if (ioctl(pcm_handle, SNDCTL_DSP_SETFRAGMENT, &tmp) == -1) |
189 | 0 | OSS_LOG_ERR("SNDCTL_DSP_SETFRAGMENT failed", errno); |
190 | |
|
191 | 0 | buffer_size = |
192 | 0 | (1ull * oss->FramesPerPacket * oss->format.nChannels * (oss->format.wBitsPerSample / 8ull)); |
193 | 0 | if (buffer_size > INT32_MAX) |
194 | 0 | goto err_out; |
195 | | |
196 | 0 | buffer = (BYTE*)calloc((buffer_size + sizeof(void*)), sizeof(BYTE)); |
197 | |
|
198 | 0 | if (nullptr == buffer) |
199 | 0 | { |
200 | 0 | OSS_LOG_ERR("malloc() fail", errno); |
201 | 0 | error = ERROR_NOT_ENOUGH_MEMORY; |
202 | 0 | goto err_out; |
203 | 0 | } |
204 | | |
205 | 0 | while (1) |
206 | 0 | { |
207 | 0 | SSIZE_T stmp = -1; |
208 | 0 | status = WaitForSingleObject(oss->stopEvent, 0); |
209 | |
|
210 | 0 | if (status == WAIT_FAILED) |
211 | 0 | { |
212 | 0 | error = GetLastError(); |
213 | 0 | WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error); |
214 | 0 | goto err_out; |
215 | 0 | } |
216 | | |
217 | 0 | if (status == WAIT_OBJECT_0) |
218 | 0 | break; |
219 | | |
220 | 0 | stmp = read(pcm_handle, buffer, buffer_size); |
221 | | |
222 | | /* Error happen. */ |
223 | 0 | if (stmp < 0) |
224 | 0 | { |
225 | 0 | OSS_LOG_ERR("read() error", errno); |
226 | 0 | continue; |
227 | 0 | } |
228 | | |
229 | 0 | if ((size_t)stmp < buffer_size) /* Not enough data. */ |
230 | 0 | continue; |
231 | | |
232 | 0 | if ((error = oss->receive(&oss->format, buffer, buffer_size, oss->user_data))) |
233 | 0 | { |
234 | 0 | WLog_ERR(TAG, "oss->receive failed with error %" PRIu32 "", error); |
235 | 0 | break; |
236 | 0 | } |
237 | 0 | } |
238 | | |
239 | 0 | err_out: |
240 | |
|
241 | 0 | if (error && oss && oss->rdpcontext) |
242 | 0 | setChannelError(oss->rdpcontext, error, "audin_oss_thread_func reported an error"); |
243 | |
|
244 | 0 | if (pcm_handle != -1) |
245 | 0 | { |
246 | 0 | WLog_INFO(TAG, "close: %s", dev_name); |
247 | 0 | close(pcm_handle); |
248 | 0 | } |
249 | |
|
250 | 0 | free(buffer); |
251 | 0 | ExitThread(error); |
252 | 0 | return error; |
253 | 0 | } |
254 | | |
255 | | /** |
256 | | * Function description |
257 | | * |
258 | | * @return 0 on success, otherwise a Win32 error code |
259 | | */ |
260 | | static UINT audin_oss_open(IAudinDevice* device, AudinReceive receive, void* user_data) |
261 | 0 | { |
262 | 0 | AudinOSSDevice* oss = (AudinOSSDevice*)device; |
263 | 0 | oss->receive = receive; |
264 | 0 | oss->user_data = user_data; |
265 | |
|
266 | 0 | if (!(oss->stopEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr))) |
267 | 0 | { |
268 | 0 | WLog_ERR(TAG, "CreateEvent failed!"); |
269 | 0 | return ERROR_INTERNAL_ERROR; |
270 | 0 | } |
271 | | |
272 | 0 | if (!(oss->thread = CreateThread(nullptr, 0, audin_oss_thread_func, oss, 0, nullptr))) |
273 | 0 | { |
274 | 0 | WLog_ERR(TAG, "CreateThread failed!"); |
275 | 0 | (void)CloseHandle(oss->stopEvent); |
276 | 0 | oss->stopEvent = nullptr; |
277 | 0 | return ERROR_INTERNAL_ERROR; |
278 | 0 | } |
279 | | |
280 | 0 | return CHANNEL_RC_OK; |
281 | 0 | } |
282 | | |
283 | | /** |
284 | | * Function description |
285 | | * |
286 | | * @return 0 on success, otherwise a Win32 error code |
287 | | */ |
288 | | static UINT audin_oss_close(IAudinDevice* device) |
289 | 0 | { |
290 | 0 | UINT error = 0; |
291 | 0 | AudinOSSDevice* oss = (AudinOSSDevice*)device; |
292 | |
|
293 | 0 | if (device == nullptr) |
294 | 0 | return ERROR_INVALID_PARAMETER; |
295 | | |
296 | 0 | if (oss->stopEvent != nullptr) |
297 | 0 | { |
298 | 0 | (void)SetEvent(oss->stopEvent); |
299 | |
|
300 | 0 | if (WaitForSingleObject(oss->thread, INFINITE) == WAIT_FAILED) |
301 | 0 | { |
302 | 0 | error = GetLastError(); |
303 | 0 | WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error); |
304 | 0 | return error; |
305 | 0 | } |
306 | | |
307 | 0 | (void)CloseHandle(oss->stopEvent); |
308 | 0 | oss->stopEvent = nullptr; |
309 | 0 | (void)CloseHandle(oss->thread); |
310 | 0 | oss->thread = nullptr; |
311 | 0 | } |
312 | | |
313 | 0 | oss->receive = nullptr; |
314 | 0 | oss->user_data = nullptr; |
315 | 0 | return CHANNEL_RC_OK; |
316 | 0 | } |
317 | | |
318 | | /** |
319 | | * Function description |
320 | | * |
321 | | * @return 0 on success, otherwise a Win32 error code |
322 | | */ |
323 | | static UINT audin_oss_free(IAudinDevice* device) |
324 | 0 | { |
325 | 0 | AudinOSSDevice* oss = (AudinOSSDevice*)device; |
326 | 0 | UINT error = 0; |
327 | |
|
328 | 0 | if (device == nullptr) |
329 | 0 | return ERROR_INVALID_PARAMETER; |
330 | | |
331 | 0 | if ((error = audin_oss_close(device))) |
332 | 0 | { |
333 | 0 | WLog_ERR(TAG, "audin_oss_close failed with error code %" PRIu32 "!", error); |
334 | 0 | } |
335 | |
|
336 | 0 | free(oss); |
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_parse_addin_args(AudinOSSDevice* device, const ADDIN_ARGV* args) |
346 | 0 | { |
347 | 0 | int status = 0; |
348 | 0 | char* str_num = nullptr; |
349 | 0 | char* eptr = nullptr; |
350 | 0 | DWORD flags = 0; |
351 | 0 | const COMMAND_LINE_ARGUMENT_A* arg = nullptr; |
352 | 0 | AudinOSSDevice* oss = device; |
353 | 0 | COMMAND_LINE_ARGUMENT_A audin_oss_args[] = { |
354 | 0 | { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", nullptr, nullptr, -1, nullptr, |
355 | 0 | "audio device name" }, |
356 | 0 | { nullptr, 0, nullptr, nullptr, nullptr, -1, nullptr, nullptr } |
357 | 0 | }; |
358 | |
|
359 | 0 | flags = |
360 | 0 | COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD; |
361 | 0 | status = CommandLineParseArgumentsA(args->argc, args->argv, audin_oss_args, flags, oss, nullptr, |
362 | 0 | nullptr); |
363 | |
|
364 | 0 | if (status < 0) |
365 | 0 | return ERROR_INVALID_PARAMETER; |
366 | | |
367 | 0 | arg = audin_oss_args; |
368 | 0 | errno = 0; |
369 | |
|
370 | 0 | do |
371 | 0 | { |
372 | 0 | if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT)) |
373 | 0 | continue; |
374 | | |
375 | 0 | CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "dev") |
376 | 0 | { |
377 | 0 | str_num = _strdup(arg->Value); |
378 | |
|
379 | 0 | if (!str_num) |
380 | 0 | { |
381 | 0 | WLog_ERR(TAG, "_strdup failed!"); |
382 | 0 | return CHANNEL_RC_NO_MEMORY; |
383 | 0 | } |
384 | | |
385 | 0 | { |
386 | 0 | long val = strtol(str_num, &eptr, 10); |
387 | |
|
388 | 0 | if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX)) |
389 | 0 | { |
390 | 0 | free(str_num); |
391 | 0 | return CHANNEL_RC_NULL_DATA; |
392 | 0 | } |
393 | | |
394 | 0 | oss->dev_unit = (INT32)val; |
395 | 0 | } |
396 | | |
397 | 0 | if (oss->dev_unit < 0 || *eptr != '\0') |
398 | 0 | oss->dev_unit = -1; |
399 | |
|
400 | 0 | free(str_num); |
401 | 0 | } |
402 | 0 | CommandLineSwitchEnd(arg) |
403 | 0 | } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr); |
404 | | |
405 | 0 | return CHANNEL_RC_OK; |
406 | 0 | } |
407 | | |
408 | | /** |
409 | | * Function description |
410 | | * |
411 | | * @return 0 on success, otherwise a Win32 error code |
412 | | */ |
413 | | FREERDP_ENTRY_POINT(UINT VCAPITYPE oss_freerdp_audin_client_subsystem_entry( |
414 | | PFREERDP_AUDIN_DEVICE_ENTRY_POINTS pEntryPoints)) |
415 | 0 | { |
416 | 0 | const ADDIN_ARGV* args = nullptr; |
417 | 0 | AudinOSSDevice* oss = nullptr; |
418 | 0 | UINT error = 0; |
419 | 0 | oss = (AudinOSSDevice*)calloc(1, sizeof(AudinOSSDevice)); |
420 | |
|
421 | 0 | if (!oss) |
422 | 0 | { |
423 | 0 | WLog_ERR(TAG, "calloc failed!"); |
424 | 0 | return CHANNEL_RC_NO_MEMORY; |
425 | 0 | } |
426 | | |
427 | 0 | oss->iface.Open = audin_oss_open; |
428 | 0 | oss->iface.FormatSupported = audin_oss_format_supported; |
429 | 0 | oss->iface.SetFormat = audin_oss_set_format; |
430 | 0 | oss->iface.Close = audin_oss_close; |
431 | 0 | oss->iface.Free = audin_oss_free; |
432 | 0 | oss->rdpcontext = pEntryPoints->rdpcontext; |
433 | 0 | oss->dev_unit = -1; |
434 | 0 | args = pEntryPoints->args; |
435 | |
|
436 | 0 | if ((error = audin_oss_parse_addin_args(oss, args))) |
437 | 0 | { |
438 | 0 | WLog_ERR(TAG, "audin_oss_parse_addin_args failed with errorcode %" PRIu32 "!", error); |
439 | 0 | goto error_out; |
440 | 0 | } |
441 | | |
442 | 0 | if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, (IAudinDevice*)oss))) |
443 | 0 | { |
444 | 0 | WLog_ERR(TAG, "RegisterAudinDevice failed with error %" PRIu32 "!", error); |
445 | 0 | goto error_out; |
446 | 0 | } |
447 | | |
448 | 0 | return CHANNEL_RC_OK; |
449 | 0 | error_out: |
450 | 0 | free(oss); |
451 | 0 | return error; |
452 | 0 | } |