freerdp: purge deprecated stream utils
[platform/upstream/freerdp.git] / channels / rdpsnd / client / rdpsnd_main.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Implementation
3  * Audio Output Virtual Channel
4  *
5  * Copyright 2009-2011 Jay Sorg
6  * Copyright 2010-2011 Vic Lee
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #ifndef _WIN32
26 #include <sys/time.h>
27 #include <signal.h>
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <winpr/crt.h>
35 #include <winpr/synch.h>
36 #include <winpr/print.h>
37 #include <winpr/cmdline.h>
38 #include <winpr/sysinfo.h>
39 #include <winpr/collections.h>
40
41 #include <freerdp/types.h>
42 #include <freerdp/addin.h>
43 #include <freerdp/constants.h>
44 #include <winpr/stream.h>
45 #include <freerdp/utils/signal.h>
46 #include <freerdp/utils/svc_plugin.h>
47
48 #include "rdpsnd_main.h"
49
50 #define TIME_DELAY_MS   65
51
52 struct rdpsnd_plugin
53 {
54         rdpSvcPlugin plugin;
55
56         HANDLE thread;
57         wMessageQueue* queue;
58
59         BYTE cBlockNo;
60         int wCurrentFormatNo;
61
62         AUDIO_FORMAT* ServerFormats;
63         UINT16 NumberOfServerFormats;
64
65         AUDIO_FORMAT* ClientFormats;
66         UINT16 NumberOfClientFormats;
67
68         BOOL expectingWave;
69         BYTE waveData[4];
70         UINT16 waveDataSize;
71         UINT32 wTimeStamp;
72
73         int latency;
74         BOOL isOpen;
75         UINT16 fixedFormat;
76         UINT16 fixedChannel;
77         UINT32 fixedRate;
78
79         char* subsystem;
80         char* device_name;
81
82         /* Device plugin */
83         rdpsndDevicePlugin* device;
84 };
85
86 void rdpsnd_send_wave_confirm_pdu(rdpsndPlugin* rdpsnd, UINT16 wTimeStamp, BYTE cConfirmedBlockNo);
87
88 static void* rdpsnd_schedule_thread(void* arg)
89 {
90         wMessage message;
91         UINT16 wTimeDiff;
92         UINT16 wTimeStamp;
93         UINT16 wCurrentTime;
94         RDPSND_WAVE* wave;
95         rdpsndPlugin* rdpsnd = (rdpsndPlugin*) arg;
96
97         while (1)
98         {
99                 if (!MessageQueue_Wait(rdpsnd->queue))
100                         break;
101
102                 if (!MessageQueue_Peek(rdpsnd->queue, &message, TRUE))
103                         break;
104
105                 if (message.id == WMQ_QUIT)
106                         break;
107
108                 wave = (RDPSND_WAVE*) message.wParam;
109                 wCurrentTime = (UINT16) GetTickCount();
110                 wTimeStamp = wave->wLocalTimeB;
111
112                 if (wCurrentTime <= wTimeStamp)
113                 {
114                         wTimeDiff = wTimeStamp - wCurrentTime;
115                         Sleep(wTimeDiff);
116                 }
117
118                 rdpsnd_send_wave_confirm_pdu(rdpsnd, wave->wTimeStampB, wave->cBlockNo);
119                 free(wave);
120         }
121
122         return NULL;
123 }
124
125 void rdpsnd_send_quality_mode_pdu(rdpsndPlugin* rdpsnd)
126 {
127         wStream* pdu;
128
129         pdu = Stream_New(NULL, 8);
130         Stream_Write_UINT8(pdu, SNDC_QUALITYMODE); /* msgType */
131         Stream_Write_UINT8(pdu, 0); /* bPad */
132         Stream_Write_UINT16(pdu, 4); /* BodySize */
133         Stream_Write_UINT16(pdu, HIGH_QUALITY); /* wQualityMode */
134         Stream_Write_UINT16(pdu, 0); /* Reserved */
135
136         svc_plugin_send((rdpSvcPlugin*) rdpsnd, pdu);
137 }
138
139 void rdpsnd_select_supported_audio_formats(rdpsndPlugin* rdpsnd)
140 {
141         int index;
142         AUDIO_FORMAT* serverFormat;
143         AUDIO_FORMAT* clientFormat;
144
145         rdpsnd_free_audio_formats(rdpsnd->ClientFormats, rdpsnd->NumberOfClientFormats);
146         rdpsnd->NumberOfClientFormats = 0;
147         rdpsnd->ClientFormats = NULL;
148
149         rdpsnd->ClientFormats = (AUDIO_FORMAT*) malloc(sizeof(AUDIO_FORMAT) * rdpsnd->NumberOfServerFormats);
150
151         for (index = 0; index < (int) rdpsnd->NumberOfServerFormats; index++)
152         {
153                 serverFormat = &rdpsnd->ServerFormats[index];
154
155                 if (rdpsnd->fixedFormat > 0 && (rdpsnd->fixedFormat != serverFormat->wFormatTag))
156                         continue;
157
158                 if (rdpsnd->fixedChannel > 0 && (rdpsnd->fixedChannel != serverFormat->nChannels))
159                         continue;
160
161                 if (rdpsnd->fixedRate > 0 && (rdpsnd->fixedRate != serverFormat->nSamplesPerSec))
162                         continue;
163
164                 if (rdpsnd->device && rdpsnd->device->FormatSupported(rdpsnd->device, serverFormat))
165                 {
166                         clientFormat = &rdpsnd->ClientFormats[rdpsnd->NumberOfClientFormats++];
167
168                         CopyMemory(clientFormat, serverFormat, sizeof(AUDIO_FORMAT));
169                         clientFormat->cbSize = 0;
170
171                         if (serverFormat->cbSize > 0)
172                         {
173                                 clientFormat->data = (BYTE*) malloc(serverFormat->cbSize);
174                                 CopyMemory(clientFormat->data, serverFormat->data, serverFormat->cbSize);
175                                 clientFormat->cbSize = serverFormat->cbSize;
176                         }
177                 }
178         }
179
180 #if 0
181         fprintf(stderr, "Server ");
182         rdpsnd_print_audio_formats(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
183         fprintf(stderr, "\n");
184
185         fprintf(stderr, "Client ");
186         rdpsnd_print_audio_formats(rdpsnd->ClientFormats, rdpsnd->NumberOfClientFormats);
187         fprintf(stderr, "\n");
188 #endif
189 }
190
191 void rdpsnd_send_client_audio_formats(rdpsndPlugin* rdpsnd)
192 {
193         int index;
194         wStream* pdu;
195         UINT16 length;
196         UINT32 dwVolume;
197         UINT16 dwVolumeLeft;
198         UINT16 dwVolumeRight;
199         UINT16 wNumberOfFormats;
200         AUDIO_FORMAT* clientFormat;
201
202         if (rdpsnd->device->GetVolume)
203         {
204                 dwVolume = rdpsnd->device->GetVolume(rdpsnd->device);
205         }
206         else
207         {
208                 dwVolumeLeft = ((50 * 0xFFFF) / 100); /* 50% */
209                 dwVolumeRight = ((50 * 0xFFFF) / 100); /* 50% */
210                 dwVolume = (dwVolumeLeft << 16) | dwVolumeRight;
211         }
212
213         wNumberOfFormats = rdpsnd->NumberOfClientFormats;
214
215         length = 4 + 20;
216
217         for (index = 0; index < (int) wNumberOfFormats; index++)
218                 length += (18 + rdpsnd->ClientFormats[index].cbSize);
219
220         pdu = Stream_New(NULL, length);
221
222         Stream_Write_UINT8(pdu, SNDC_FORMATS); /* msgType */
223         Stream_Write_UINT8(pdu, 0); /* bPad */
224         Stream_Write_UINT16(pdu, length - 4); /* BodySize */
225
226         Stream_Write_UINT32(pdu, TSSNDCAPS_ALIVE | TSSNDCAPS_VOLUME); /* dwFlags */
227         Stream_Write_UINT32(pdu, dwVolume); /* dwVolume */
228         Stream_Write_UINT32(pdu, 0); /* dwPitch */
229         Stream_Write_UINT16(pdu, 0); /* wDGramPort */
230         Stream_Write_UINT16(pdu, wNumberOfFormats); /* wNumberOfFormats */
231         Stream_Write_UINT8(pdu, 0); /* cLastBlockConfirmed */
232         Stream_Write_UINT16(pdu, 6); /* wVersion */
233         Stream_Write_UINT8(pdu, 0); /* bPad */
234
235         for (index = 0; index < (int) wNumberOfFormats; index++)
236         {
237                 clientFormat = &rdpsnd->ClientFormats[index];
238
239                 Stream_Write_UINT16(pdu, clientFormat->wFormatTag);
240                 Stream_Write_UINT16(pdu, clientFormat->nChannels);
241                 Stream_Write_UINT32(pdu, clientFormat->nSamplesPerSec);
242                 Stream_Write_UINT32(pdu, clientFormat->nAvgBytesPerSec);
243                 Stream_Write_UINT16(pdu, clientFormat->nBlockAlign);
244                 Stream_Write_UINT16(pdu, clientFormat->wBitsPerSample);
245                 Stream_Write_UINT16(pdu, clientFormat->cbSize);
246
247                 if (clientFormat->cbSize > 0)
248                         Stream_Write(pdu, clientFormat->data, clientFormat->cbSize);
249         }
250
251         svc_plugin_send((rdpSvcPlugin*) rdpsnd, pdu);
252 }
253
254 void rdpsnd_recv_server_audio_formats_pdu(rdpsndPlugin* rdpsnd, wStream* s)
255 {
256         int index;
257         UINT16 wVersion;
258         AUDIO_FORMAT* format;
259         UINT16 wNumberOfFormats;
260
261         rdpsnd_free_audio_formats(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
262         rdpsnd->NumberOfServerFormats = 0;
263         rdpsnd->ServerFormats = NULL;
264
265         Stream_Seek_UINT32(s); /* dwFlags */
266         Stream_Seek_UINT32(s); /* dwVolume */
267         Stream_Seek_UINT32(s); /* dwPitch */
268         Stream_Seek_UINT16(s); /* wDGramPort */
269         Stream_Read_UINT16(s, wNumberOfFormats);
270         Stream_Read_UINT8(s, rdpsnd->cBlockNo); /* cLastBlockConfirmed */
271         Stream_Read_UINT16(s, wVersion); /* wVersion */
272         Stream_Seek_UINT8(s); /* bPad */
273
274         rdpsnd->NumberOfServerFormats = wNumberOfFormats;
275         rdpsnd->ServerFormats = (AUDIO_FORMAT*) malloc(sizeof(AUDIO_FORMAT) * wNumberOfFormats);
276
277         for (index = 0; index < (int) wNumberOfFormats; index++)
278         {
279                 format = &rdpsnd->ServerFormats[index];
280
281                 Stream_Read_UINT16(s, format->wFormatTag); /* wFormatTag */
282                 Stream_Read_UINT16(s, format->nChannels); /* nChannels */
283                 Stream_Read_UINT32(s, format->nSamplesPerSec); /* nSamplesPerSec */
284                 Stream_Read_UINT32(s, format->nAvgBytesPerSec); /* nAvgBytesPerSec */
285                 Stream_Read_UINT16(s, format->nBlockAlign); /* nBlockAlign */
286                 Stream_Read_UINT16(s, format->wBitsPerSample); /* wBitsPerSample */
287                 Stream_Read_UINT16(s, format->cbSize); /* cbSize */
288
289                 format->data = (BYTE*) malloc(format->cbSize);
290                 Stream_Read(s, format->data, format->cbSize);
291         }
292
293         rdpsnd_select_supported_audio_formats(rdpsnd);
294
295         rdpsnd_send_client_audio_formats(rdpsnd);
296
297         if (wVersion >= 6)
298                 rdpsnd_send_quality_mode_pdu(rdpsnd);
299 }
300
301 void rdpsnd_send_training_confirm_pdu(rdpsndPlugin* rdpsnd, UINT16 wTimeStamp, UINT16 wPackSize)
302 {
303         wStream* pdu;
304
305         pdu = Stream_New(NULL, 8);
306         Stream_Write_UINT8(pdu, SNDC_TRAINING); /* msgType */
307         Stream_Write_UINT8(pdu, 0); /* bPad */
308         Stream_Write_UINT16(pdu, 4); /* BodySize */
309         Stream_Write_UINT16(pdu, wTimeStamp);
310         Stream_Write_UINT16(pdu, wPackSize);
311
312         svc_plugin_send((rdpSvcPlugin*) rdpsnd, pdu);
313 }
314
315 static void rdpsnd_recv_training_pdu(rdpsndPlugin* rdpsnd, wStream* s)
316 {
317         UINT16 wTimeStamp;
318         UINT16 wPackSize;
319
320         Stream_Read_UINT16(s, wTimeStamp);
321         Stream_Read_UINT16(s, wPackSize);
322
323         rdpsnd_send_training_confirm_pdu(rdpsnd, wTimeStamp, wPackSize);
324 }
325
326 static void rdpsnd_recv_wave_info_pdu(rdpsndPlugin* rdpsnd, wStream* s, UINT16 BodySize)
327 {
328         UINT16 wFormatNo;
329         AUDIO_FORMAT* format;
330
331         rdpsnd->expectingWave = TRUE;
332
333         Stream_Read_UINT16(s, rdpsnd->wTimeStamp);
334         Stream_Read_UINT16(s, wFormatNo);
335         Stream_Read_UINT8(s, rdpsnd->cBlockNo);
336         Stream_Seek(s, 3); /* bPad */
337         Stream_Read(s, rdpsnd->waveData, 4);
338
339         rdpsnd->waveDataSize = BodySize - 8;
340
341         format = &rdpsnd->ClientFormats[wFormatNo];
342
343         if (!rdpsnd->isOpen)
344         {
345                 rdpsnd->isOpen = TRUE;
346                 rdpsnd->wCurrentFormatNo = wFormatNo;
347
348                 //rdpsnd_print_audio_format(format);
349
350                 if (rdpsnd->device)
351                 {
352                         IFCALL(rdpsnd->device->Open, rdpsnd->device, format, rdpsnd->latency);
353                 }
354         }
355         else if (wFormatNo != rdpsnd->wCurrentFormatNo)
356         {
357                 rdpsnd->wCurrentFormatNo = wFormatNo;
358
359                 if (rdpsnd->device)
360                 {
361                         IFCALL(rdpsnd->device->SetFormat, rdpsnd->device, format, rdpsnd->latency);
362                 }
363         }
364 }
365
366 void rdpsnd_send_wave_confirm_pdu(rdpsndPlugin* rdpsnd, UINT16 wTimeStamp, BYTE cConfirmedBlockNo)
367 {
368         wStream* pdu;
369
370         pdu = Stream_New(NULL, 8);
371         Stream_Write_UINT8(pdu, SNDC_WAVECONFIRM);
372         Stream_Write_UINT8(pdu, 0);
373         Stream_Write_UINT16(pdu, 4);
374         Stream_Write_UINT16(pdu, wTimeStamp);
375         Stream_Write_UINT8(pdu, cConfirmedBlockNo); /* cConfirmedBlockNo */
376         Stream_Write_UINT8(pdu, 0); /* bPad */
377
378         svc_plugin_send((rdpSvcPlugin*) rdpsnd, pdu);
379 }
380
381 void rdpsnd_device_send_wave_confirm_pdu(rdpsndDevicePlugin* device, RDPSND_WAVE* wave)
382 {
383         MessageQueue_Post(device->rdpsnd->queue, NULL, 0, (void*) wave, NULL);
384 }
385
386 static void rdpsnd_recv_wave_pdu(rdpsndPlugin* rdpsnd, wStream* s)
387 {
388         int size;
389         BYTE* data;
390         RDPSND_WAVE* wave;
391         AUDIO_FORMAT* format;
392
393         rdpsnd->expectingWave = FALSE;
394
395         /**
396          * The Wave PDU is a special case: it is always sent after a Wave Info PDU,
397          * and we do not process its header. Instead, the header is pad that needs
398          * to be filled with the first four bytes of the audio sample data sent as
399          * part of the preceding Wave Info PDU.
400          */
401
402         CopyMemory(Stream_Buffer(s), rdpsnd->waveData, 4);
403
404         data = Stream_Buffer(s);
405         size = Stream_Capacity(s);
406
407         wave = (RDPSND_WAVE*) malloc(sizeof(RDPSND_WAVE));
408
409         wave->wLocalTimeA = GetTickCount();
410         wave->wTimeStampA = rdpsnd->wTimeStamp;
411         wave->wFormatNo = rdpsnd->wCurrentFormatNo;
412         wave->cBlockNo = rdpsnd->cBlockNo;
413
414         wave->data = data;
415         wave->length = size;
416
417         format = &rdpsnd->ClientFormats[rdpsnd->wCurrentFormatNo];
418         wave->wAudioLength = rdpsnd_compute_audio_time_length(format, size);
419
420         if (!rdpsnd->device)
421                 return;
422
423         if (rdpsnd->device->WaveDecode)
424         {
425                 IFCALL(rdpsnd->device->WaveDecode, rdpsnd->device, wave);
426         }
427
428         if (rdpsnd->device->WavePlay)
429         {
430                 IFCALL(rdpsnd->device->WavePlay, rdpsnd->device, wave);
431         }
432         else
433         {
434                 IFCALL(rdpsnd->device->Play, rdpsnd->device, data, size);
435         }
436
437         if (!rdpsnd->device->WavePlay)
438         {
439                 wave->wTimeStampB = rdpsnd->wTimeStamp + wave->wAudioLength + TIME_DELAY_MS;
440                 wave->wLocalTimeB = wave->wLocalTimeA + wave->wAudioLength + TIME_DELAY_MS;
441                 rdpsnd->device->WaveConfirm(rdpsnd->device, wave);
442         }
443 }
444
445 static void rdpsnd_recv_close_pdu(rdpsndPlugin* rdpsnd)
446 {
447         DEBUG_SVC("server closes.");
448
449         if (rdpsnd->device)
450         {
451                 IFCALL(rdpsnd->device->Close, rdpsnd->device);
452         }
453
454         rdpsnd->isOpen = FALSE;
455 }
456
457 static void rdpsnd_recv_volume_pdu(rdpsndPlugin* rdpsnd, wStream* s)
458 {
459         UINT32 dwVolume;
460
461         Stream_Read_UINT32(s, dwVolume);
462         DEBUG_SVC("dwVolume 0x%X", dwVolume);
463
464         if (rdpsnd->device)
465         {
466                 IFCALL(rdpsnd->device->SetVolume, rdpsnd->device, dwVolume);
467         }
468 }
469
470 static void rdpsnd_recv_pdu(rdpSvcPlugin* plugin, wStream* s)
471 {
472         BYTE msgType;
473         UINT16 BodySize;
474         rdpsndPlugin* rdpsnd = (rdpsndPlugin*) plugin;
475
476         if (rdpsnd->expectingWave)
477         {
478                 rdpsnd_recv_wave_pdu(rdpsnd, s);
479                 Stream_Free(s, TRUE);
480                 return;
481         }
482
483         Stream_Read_UINT8(s, msgType); /* msgType */
484         Stream_Seek_UINT8(s); /* bPad */
485         Stream_Read_UINT16(s, BodySize);
486
487         //fprintf(stderr, "msgType %d BodySize %d\n", msgType, BodySize);
488
489         switch (msgType)
490         {
491                 case SNDC_FORMATS:
492                         rdpsnd_recv_server_audio_formats_pdu(rdpsnd, s);
493                         break;
494
495                 case SNDC_TRAINING:
496                         rdpsnd_recv_training_pdu(rdpsnd, s);
497                         break;
498
499                 case SNDC_WAVE:
500                         rdpsnd_recv_wave_info_pdu(rdpsnd, s, BodySize);
501                         break;
502
503                 case SNDC_CLOSE:
504                         rdpsnd_recv_close_pdu(rdpsnd);
505                         break;
506
507                 case SNDC_SETVOLUME:
508                         rdpsnd_recv_volume_pdu(rdpsnd, s);
509                         break;
510
511                 default:
512                         DEBUG_WARN("unknown msgType %d", msgType);
513                         break;
514         }
515
516         Stream_Free(s, TRUE);
517 }
518
519 static void rdpsnd_register_device_plugin(rdpsndPlugin* rdpsnd, rdpsndDevicePlugin* device)
520 {
521         if (rdpsnd->device)
522         {
523                 DEBUG_WARN("existing device, abort.");
524                 return;
525         }
526
527         rdpsnd->device = device;
528         device->rdpsnd = rdpsnd;
529
530         device->WaveConfirm = rdpsnd_device_send_wave_confirm_pdu;
531 }
532
533 static BOOL rdpsnd_load_device_plugin(rdpsndPlugin* rdpsnd, const char* name, ADDIN_ARGV* args)
534 {
535         PFREERDP_RDPSND_DEVICE_ENTRY entry;
536         FREERDP_RDPSND_DEVICE_ENTRY_POINTS entryPoints;
537
538         entry = (PFREERDP_RDPSND_DEVICE_ENTRY) freerdp_load_channel_addin_entry("rdpsnd", (LPSTR) name, NULL, 0);
539
540         if (!entry)
541                 return FALSE;
542
543         entryPoints.rdpsnd = rdpsnd;
544         entryPoints.pRegisterRdpsndDevice = rdpsnd_register_device_plugin;
545         entryPoints.args = args;
546
547         if (entry(&entryPoints) != 0)
548         {
549                 DEBUG_WARN("%s entry returns error.", name);
550                 return FALSE;
551         }
552
553         return TRUE;
554 }
555
556 void rdpsnd_set_subsystem(rdpsndPlugin* rdpsnd, char* subsystem)
557 {
558         if (rdpsnd->subsystem)
559                 free(rdpsnd->subsystem);
560
561         rdpsnd->subsystem = _strdup(subsystem);
562 }
563
564 void rdpsnd_set_device_name(rdpsndPlugin* rdpsnd, char* device_name)
565 {
566         if (rdpsnd->device_name)
567                 free(rdpsnd->device_name);
568
569         rdpsnd->device_name = _strdup(device_name);
570 }
571
572 COMMAND_LINE_ARGUMENT_A rdpsnd_args[] =
573 {
574         { "sys", COMMAND_LINE_VALUE_REQUIRED, "<subsystem>", NULL, NULL, -1, NULL, "subsystem" },
575         { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", NULL, NULL, -1, NULL, "device" },
576         { "format", COMMAND_LINE_VALUE_REQUIRED, "<format>", NULL, NULL, -1, NULL, "format" },
577         { "rate", COMMAND_LINE_VALUE_REQUIRED, "<rate>", NULL, NULL, -1, NULL, "rate" },
578         { "channel", COMMAND_LINE_VALUE_REQUIRED, "<channel>", NULL, NULL, -1, NULL, "channel" },
579         { "latency", COMMAND_LINE_VALUE_REQUIRED, "<latency>", NULL, NULL, -1, NULL, "latency" },
580         { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
581 };
582
583 static void rdpsnd_process_addin_args(rdpsndPlugin* rdpsnd, ADDIN_ARGV* args)
584 {
585         int status;
586         DWORD flags;
587         COMMAND_LINE_ARGUMENT_A* arg;
588
589         flags = COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON;
590
591         status = CommandLineParseArgumentsA(args->argc, (const char**) args->argv,
592                         rdpsnd_args, flags, rdpsnd, NULL, NULL);
593
594         arg = rdpsnd_args;
595
596         do
597         {
598                 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
599                         continue;
600
601                 CommandLineSwitchStart(arg)
602
603                 CommandLineSwitchCase(arg, "sys")
604                 {
605                         rdpsnd_set_subsystem(rdpsnd, arg->Value);
606                 }
607                 CommandLineSwitchCase(arg, "dev")
608                 {
609                         rdpsnd_set_device_name(rdpsnd, arg->Value);
610                 }
611                 CommandLineSwitchCase(arg, "format")
612                 {
613                         rdpsnd->fixedFormat = atoi(arg->Value);
614                 }
615                 CommandLineSwitchCase(arg, "rate")
616                 {
617                         rdpsnd->fixedRate = atoi(arg->Value);
618                 }
619                 CommandLineSwitchCase(arg, "channel")
620                 {
621                         rdpsnd->fixedChannel = atoi(arg->Value);
622                 }
623                 CommandLineSwitchCase(arg, "latency")
624                 {
625                         rdpsnd->latency = atoi(arg->Value);
626                 }
627                 CommandLineSwitchDefault(arg)
628                 {
629
630                 }
631
632                 CommandLineSwitchEnd(arg)
633         }
634         while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
635 }
636
637 static void rdpsnd_process_connect(rdpSvcPlugin* plugin)
638 {
639         ADDIN_ARGV* args;
640         rdpsndPlugin* rdpsnd = (rdpsndPlugin*) plugin;
641
642         DEBUG_SVC("connecting");
643
644         rdpsnd->latency = -1;
645         rdpsnd->queue = MessageQueue_New();
646         rdpsnd->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) rdpsnd_schedule_thread, (void*) plugin, 0, NULL);
647
648         args = (ADDIN_ARGV*) plugin->channel_entry_points.pExtendedData;
649
650         if (args)
651                 rdpsnd_process_addin_args(rdpsnd, args);
652
653         if (rdpsnd->subsystem)
654         {
655                 if (strcmp(rdpsnd->subsystem, "fake") == 0)
656                         return;
657
658                 rdpsnd_load_device_plugin(rdpsnd, rdpsnd->subsystem, args);
659         }
660
661         if (!rdpsnd->device)
662         {
663                 rdpsnd_set_subsystem(rdpsnd, "pulse");
664                 rdpsnd_set_device_name(rdpsnd, "");
665                 rdpsnd_load_device_plugin(rdpsnd, rdpsnd->subsystem, args);
666         }
667
668         if (!rdpsnd->device)
669         {
670                 rdpsnd_set_subsystem(rdpsnd, "alsa");
671                 rdpsnd_set_device_name(rdpsnd, "default");
672                 rdpsnd_load_device_plugin(rdpsnd, rdpsnd->subsystem, args);
673         }
674
675         if (!rdpsnd->device)
676         {
677                 rdpsnd_set_subsystem(rdpsnd, "macaudio");
678                 rdpsnd_set_device_name(rdpsnd, "default");
679                 rdpsnd_load_device_plugin(rdpsnd, rdpsnd->subsystem, args);
680         }
681
682         if (!rdpsnd->device)
683         {
684                 rdpsnd_set_subsystem(rdpsnd, "winmm");
685                 rdpsnd_set_device_name(rdpsnd, "");
686                 rdpsnd_load_device_plugin(rdpsnd, rdpsnd->subsystem, args);
687         }
688
689         if (!rdpsnd->device)
690         {
691                 DEBUG_WARN("no sound device.");
692                 return;
693         }
694 }
695
696 static void rdpsnd_process_event(rdpSvcPlugin* plugin, wMessage* event)
697 {
698         freerdp_event_free(event);
699 }
700
701 static void rdpsnd_process_terminate(rdpSvcPlugin* plugin)
702 {
703         rdpsndPlugin* rdpsnd = (rdpsndPlugin*) plugin;
704
705         if (rdpsnd->device)
706                 IFCALL(rdpsnd->device->Free, rdpsnd->device);
707
708         MessageQueue_PostQuit(rdpsnd->queue, 0);
709         WaitForSingleObject(rdpsnd->thread, INFINITE);
710
711         MessageQueue_Free(rdpsnd->queue);
712         CloseHandle(rdpsnd->thread);
713
714         if (rdpsnd->subsystem)
715                 free(rdpsnd->subsystem);
716
717         if (rdpsnd->device_name)
718                 free(rdpsnd->device_name);
719
720         rdpsnd_free_audio_formats(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats);
721         rdpsnd->NumberOfServerFormats = 0;
722         rdpsnd->ServerFormats = NULL;
723
724         rdpsnd_free_audio_formats(rdpsnd->ClientFormats, rdpsnd->NumberOfClientFormats);
725         rdpsnd->NumberOfClientFormats = 0;
726         rdpsnd->ClientFormats = NULL;
727 }
728
729 /* rdpsnd is always built-in */
730 #define VirtualChannelEntry     rdpsnd_VirtualChannelEntry
731
732 int VirtualChannelEntry(PCHANNEL_ENTRY_POINTS pEntryPoints)
733 {
734         rdpsndPlugin* _p;
735
736         _p = (rdpsndPlugin*) malloc(sizeof(rdpsndPlugin));
737         ZeroMemory(_p, sizeof(rdpsndPlugin));
738
739         _p->plugin.channel_def.options =
740                         CHANNEL_OPTION_INITIALIZED |
741                         CHANNEL_OPTION_ENCRYPT_RDP;
742
743         strcpy(_p->plugin.channel_def.name, "rdpsnd");
744
745         _p->plugin.connect_callback = rdpsnd_process_connect;
746         _p->plugin.receive_callback = rdpsnd_recv_pdu;
747         _p->plugin.event_callback = rdpsnd_process_event;
748         _p->plugin.terminate_callback = rdpsnd_process_terminate;
749
750 #if !defined(_WIN32) && !defined(ANDROID)
751         {
752                 sigset_t mask;
753                 sigemptyset(&mask);
754                 sigaddset(&mask, SIGIO);
755                 pthread_sigmask(SIG_BLOCK, &mask, NULL);
756         }
757 #endif
758
759         svc_plugin_init((rdpSvcPlugin*) _p, pEntryPoints);
760
761         return 1;
762 }