Coverage Report

Created: 2025-08-29 06:49

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