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 == NULL || format == NULL)
50 return FALSE;
51
52 if (sndio->hdl != NULL)
53 return TRUE;
54
55 sndio->hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0);
56 if (sndio->hdl == NULL)
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 == NULL)
91 return;
92
93 if (sndio->hdl != NULL)
94 {
95 sio_stop(sndio->hdl);
96 sio_close(sndio->hdl);
97 sndio->hdl = NULL;
98 }
99}
100
101static BOOL rdpsnd_sndio_set_volume(rdpsndDevicePlugin* device, UINT32 value)
102{
103 rdpsndSndioPlugin* sndio = (rdpsndSndioPlugin*)device;
104
105 if (device == NULL || sndio->hdl == NULL)
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 == NULL)
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 == NULL)
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 == NULL || sndio->hdl == NULL)
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[] = { { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL } };
156 flags =
157 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
158 status = CommandLineParseArgumentsA(args->argc, (const char**)args->argv, rdpsnd_sndio_args,
159 flags, sndio, NULL, NULL);
160
161 if (status < 0)
162 return ERROR_INVALID_DATA;
163
164 arg = rdpsnd_sndio_args;
165
166 do
167 {
168 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
169 continue;
170
171 CommandLineSwitchStart(arg) CommandLineSwitchEnd(arg)
172 } while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
173
174 return CHANNEL_RC_OK;
175}
176
182FREERDP_ENTRY_POINT(UINT VCAPITYPE sndio_freerdp_rdpsnd_client_subsystem_entry(
184{
185 ADDIN_ARGV* args;
186 rdpsndSndioPlugin* sndio;
187 UINT ret = CHANNEL_RC_OK;
188 sndio = (rdpsndSndioPlugin*)calloc(1, sizeof(rdpsndSndioPlugin));
189
190 if (sndio == NULL)
191 return CHANNEL_RC_NO_MEMORY;
192
193 sndio->device.Open = rdpsnd_sndio_open;
194 sndio->device.FormatSupported = rdpsnd_sndio_format_supported;
195 sndio->device.SetVolume = rdpsnd_sndio_set_volume;
196 sndio->device.Play = rdpsnd_sndio_play;
197 sndio->device.Close = rdpsnd_sndio_close;
198 sndio->device.Free = rdpsnd_sndio_free;
199 args = pEntryPoints->args;
200
201 if (args->argc > 1)
202 {
203 ret = rdpsnd_sndio_parse_addin_args((rdpsndDevicePlugin*)sndio, args);
204
205 if (ret != CHANNEL_RC_OK)
206 {
207 WLog_ERR(TAG, "error parsing arguments");
208 goto error;
209 }
210 }
211
212 pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, &sndio->device);
213 return ret;
214error:
215 rdpsnd_sndio_free(&sndio->device);
216 return ret;
217}