FreeRDP
Loading...
Searching...
No Matches
rdpsnd_sndio.c
1
22#include <freerdp/config.h>
23
24#include <sndio.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29#include <winpr/crt.h>
30#include <winpr/stream.h>
31#include <winpr/cmdline.h>
32
33#include <freerdp/types.h>
34
35#include "rdpsnd_main.h"
36
37typedef struct
38{
39 rdpsndDevicePlugin device;
40
41 struct sio_hdl* hdl;
42 struct sio_par par;
43} rdpsndSndioPlugin;
44
45static BOOL rdpsnd_sndio_open(rdpsndDevicePlugin* device, AUDIO_FORMAT* format, int latency)
46{
47 rdpsndSndioPlugin* sndio = (rdpsndSndioPlugin*)device;
48
49 if (device == nullptr || format == nullptr)
50 return FALSE;
51
52 if (sndio->hdl != nullptr)
53 return TRUE;
54
55 sndio->hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0);
56 if (sndio->hdl == nullptr)
57 {
58 WLog_ERR(TAG, "could not open audio device");
59 return FALSE;
60 }
61
62 sio_initpar(&sndio->par);
63 sndio->par.bits = format->wBitsPerSample;
64 sndio->par.pchan = format->nChannels;
65 sndio->par.rate = format->nSamplesPerSec;
66 if (!sio_setpar(sndio->hdl, &sndio->par))
67 {
68 WLog_ERR(TAG, "could not set audio parameters");
69 return FALSE;
70 }
71 if (!sio_getpar(sndio->hdl, &sndio->par))
72 {
73 WLog_ERR(TAG, "could not get audio parameters");
74 return FALSE;
75 }
76
77 if (!sio_start(sndio->hdl))
78 {
79 WLog_ERR(TAG, "could not start audio device");
80 return FALSE;
81 }
82
83 return TRUE;
84}
85
86static void rdpsnd_sndio_close(rdpsndDevicePlugin* device)
87{
88 rdpsndSndioPlugin* sndio = (rdpsndSndioPlugin*)device;
89
90 if (device == nullptr)
91 return;
92
93 if (sndio->hdl != nullptr)
94 {
95 sio_stop(sndio->hdl);
96 sio_close(sndio->hdl);
97 sndio->hdl = nullptr;
98 }
99}
100
101static BOOL rdpsnd_sndio_set_volume(rdpsndDevicePlugin* device, UINT32 value)
102{
103 rdpsndSndioPlugin* sndio = (rdpsndSndioPlugin*)device;
104
105 if (device == nullptr || sndio->hdl == nullptr)
106 return FALSE;
107
108 /*
109 * Low-order word contains the left-channel volume setting.
110 * We ignore the right-channel volume setting in the high-order word.
111 */
112 return sio_setvol(sndio->hdl, ((value & 0xFFFF) * SIO_MAXVOL) / 0xFFFF);
113}
114
115static void rdpsnd_sndio_free(rdpsndDevicePlugin* device)
116{
117 rdpsndSndioPlugin* sndio = (rdpsndSndioPlugin*)device;
118
119 if (device == nullptr)
120 return;
121
122 rdpsnd_sndio_close(device);
123 free(sndio);
124}
125
126static BOOL rdpsnd_sndio_format_supported(rdpsndDevicePlugin* device, AUDIO_FORMAT* format)
127{
128 if (format == nullptr)
129 return FALSE;
130
131 return (format->wFormatTag == WAVE_FORMAT_PCM);
132}
133
134static void rdpsnd_sndio_play(rdpsndDevicePlugin* device, BYTE* data, int size)
135{
136 rdpsndSndioPlugin* sndio = (rdpsndSndioPlugin*)device;
137
138 if (device == nullptr || sndio->hdl == nullptr)
139 return;
140
141 sio_write(sndio->hdl, data, size);
142}
143
149static UINT rdpsnd_sndio_parse_addin_args(rdpsndDevicePlugin* device, ADDIN_ARGV* args)
150{
151 int status;
152 DWORD flags;
154 rdpsndSndioPlugin* sndio = (rdpsndSndioPlugin*)device;
155 COMMAND_LINE_ARGUMENT_A rdpsnd_sndio_args[] = { { nullptr, 0, nullptr, nullptr, nullptr, -1,
156 nullptr, nullptr } };
157 flags =
158 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
159 status = CommandLineParseArgumentsA(args->argc, (const char**)args->argv, rdpsnd_sndio_args,
160 flags, sndio, nullptr, nullptr);
161
162 if (status < 0)
163 return ERROR_INVALID_DATA;
164
165 arg = rdpsnd_sndio_args;
166
167 do
168 {
169 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
170 continue;
171
172 CommandLineSwitchStart(arg) CommandLineSwitchEnd(arg)
173 } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr);
174
175 return CHANNEL_RC_OK;
176}
177
183FREERDP_ENTRY_POINT(UINT VCAPITYPE sndio_freerdp_rdpsnd_client_subsystem_entry(
185{
186 ADDIN_ARGV* args;
187 rdpsndSndioPlugin* sndio;
188 UINT ret = CHANNEL_RC_OK;
189 sndio = (rdpsndSndioPlugin*)calloc(1, sizeof(rdpsndSndioPlugin));
190
191 if (sndio == nullptr)
192 return CHANNEL_RC_NO_MEMORY;
193
194 sndio->device.Open = rdpsnd_sndio_open;
195 sndio->device.FormatSupported = rdpsnd_sndio_format_supported;
196 sndio->device.SetVolume = rdpsnd_sndio_set_volume;
197 sndio->device.Play = rdpsnd_sndio_play;
198 sndio->device.Close = rdpsnd_sndio_close;
199 sndio->device.Free = rdpsnd_sndio_free;
200 args = pEntryPoints->args;
201
202 if (args->argc > 1)
203 {
204 ret = rdpsnd_sndio_parse_addin_args((rdpsndDevicePlugin*)sndio, args);
205
206 if (ret != CHANNEL_RC_OK)
207 {
208 WLog_ERR(TAG, "error parsing arguments");
209 goto error;
210 }
211 }
212
213 pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, &sndio->device);
214 return ret;
215error:
216 rdpsnd_sndio_free(&sndio->device);
217 return ret;
218}