FreeRDP
Loading...
Searching...
No Matches
audin.c
1
23#include <freerdp/config.h>
24
25#include <winpr/crt.h>
26#include <winpr/assert.h>
27#include <winpr/synch.h>
28#include <winpr/thread.h>
29#include <winpr/stream.h>
30
31#include <freerdp/freerdp.h>
32#include <freerdp/server/server-common.h>
33#include <freerdp/server/audin.h>
34#include <freerdp/channels/log.h>
35
36#define AUDIN_TAG CHANNELS_TAG("audin.server")
37
38#define SNDIN_HEADER_SIZE 1
39
40typedef enum
41{
42 MSG_SNDIN_VERSION = 0x01,
43 MSG_SNDIN_FORMATS = 0x02,
44 MSG_SNDIN_OPEN = 0x03,
45 MSG_SNDIN_OPEN_REPLY = 0x04,
46 MSG_SNDIN_DATA_INCOMING = 0x05,
47 MSG_SNDIN_DATA = 0x06,
48 MSG_SNDIN_FORMATCHANGE = 0x07,
49} MSG_SNDIN;
50
51typedef struct
52{
53 audin_server_context context;
54
55 HANDLE stopEvent;
56
57 HANDLE thread;
58 void* audin_channel;
59
60 DWORD SessionId;
61
62 AUDIO_FORMAT* audin_server_formats;
63 UINT32 audin_n_server_formats;
64 AUDIO_FORMAT* audin_negotiated_format;
65 UINT32 audin_client_format_idx;
66 wLog* log;
67} audin_server;
68
69static UINT audin_server_recv_version(audin_server_context* context, wStream* s,
70 const SNDIN_PDU* header)
71{
72 audin_server* audin = (audin_server*)context;
73 SNDIN_VERSION pdu = { 0 };
74 UINT error = CHANNEL_RC_OK;
75
76 WINPR_ASSERT(context);
77 WINPR_ASSERT(header);
78
79 pdu.Header = *header;
80
81 if (!Stream_CheckAndLogRequiredLengthWLog(audin->log, s, 4))
82 return ERROR_NO_DATA;
83
84 {
85 const UINT32 version = Stream_Get_UINT32(s);
86 switch (version)
87 {
88 case SNDIN_VERSION_Version_1:
89 pdu.Version = SNDIN_VERSION_Version_1;
90 break;
91 case SNDIN_VERSION_Version_2:
92 pdu.Version = SNDIN_VERSION_Version_2;
93 break;
94 default:
95 pdu.Version = SNDIN_VERSION_Version_2;
96 WLog_Print(audin->log, WLOG_WARN,
97 "Received unsupported channel version %" PRIu32
98 ", using highest supported version %u",
99 version, pdu.Version);
100 break;
101 }
102 }
103
104 IFCALLRET(context->ReceiveVersion, error, context, &pdu);
105 if (error)
106 WLog_Print(audin->log, WLOG_ERROR, "context->ReceiveVersion failed with error %" PRIu32 "",
107 error);
108
109 return error;
110}
111
112static UINT audin_server_recv_formats(audin_server_context* context, wStream* s,
113 const SNDIN_PDU* header)
114{
115 audin_server* audin = (audin_server*)context;
116 SNDIN_FORMATS pdu = { 0 };
117 UINT error = CHANNEL_RC_OK;
118
119 WINPR_ASSERT(context);
120 WINPR_ASSERT(header);
121
122 pdu.Header = *header;
123
124 /* Implementations MUST, at a minimum, support WAVE_FORMAT_PCM (0x0001) */
125 if (!Stream_CheckAndLogRequiredLengthWLog(audin->log, s, 4 + 4 + 18))
126 return ERROR_NO_DATA;
127
128 Stream_Read_UINT32(s, pdu.NumFormats);
129 Stream_Read_UINT32(s, pdu.cbSizeFormatsPacket);
130
131 if (pdu.NumFormats == 0)
132 {
133 WLog_Print(audin->log, WLOG_ERROR, "Sound Formats PDU contains no formats");
134 return ERROR_INVALID_DATA;
135 }
136
137 pdu.SoundFormats = audio_formats_new(pdu.NumFormats);
138 if (!pdu.SoundFormats)
139 {
140 WLog_Print(audin->log, WLOG_ERROR, "Failed to allocate %u SoundFormats", pdu.NumFormats);
141 return ERROR_NOT_ENOUGH_MEMORY;
142 }
143
144 for (UINT32 i = 0; i < pdu.NumFormats; ++i)
145 {
146 AUDIO_FORMAT* format = &pdu.SoundFormats[i];
147
148 if (!audio_format_read(s, format))
149 {
150 WLog_Print(audin->log, WLOG_ERROR, "Failed to read audio format");
151 audio_formats_free(pdu.SoundFormats, i + i);
152 return ERROR_INVALID_DATA;
153 }
154
155 audio_format_print(audin->log, WLOG_DEBUG, format);
156 }
157
158 if (pdu.cbSizeFormatsPacket != Stream_GetPosition(s))
159 {
160 WLog_Print(audin->log, WLOG_WARN,
161 "cbSizeFormatsPacket is invalid! Expected: %u Got: %zu. Fixing size",
162 pdu.cbSizeFormatsPacket, Stream_GetPosition(s));
163 const size_t pos = Stream_GetPosition(s);
164 if (pos > UINT32_MAX)
165 {
166 WLog_Print(audin->log, WLOG_ERROR, "Stream too long, %" PRIuz " exceeds UINT32_MAX",
167 pos);
168 error = ERROR_INVALID_PARAMETER;
169 goto fail;
170 }
171 pdu.cbSizeFormatsPacket = (UINT32)pos;
172 }
173
174 pdu.ExtraDataSize = Stream_GetRemainingLength(s);
175
176 IFCALLRET(context->ReceiveFormats, error, context, &pdu);
177 if (error)
178 WLog_Print(audin->log, WLOG_ERROR, "context->ReceiveFormats failed with error %" PRIu32 "",
179 error);
180
181fail:
182 audio_formats_free(pdu.SoundFormats, pdu.NumFormats);
183
184 return error;
185}
186
187static UINT audin_server_recv_open_reply(audin_server_context* context, wStream* s,
188 const SNDIN_PDU* header)
189{
190 audin_server* audin = (audin_server*)context;
191 SNDIN_OPEN_REPLY pdu = { 0 };
192 UINT error = CHANNEL_RC_OK;
193
194 WINPR_ASSERT(context);
195 WINPR_ASSERT(header);
196
197 pdu.Header = *header;
198
199 if (!Stream_CheckAndLogRequiredLengthWLog(audin->log, s, 4))
200 return ERROR_NO_DATA;
201
202 Stream_Read_UINT32(s, pdu.Result);
203
204 IFCALLRET(context->OpenReply, error, context, &pdu);
205 if (error)
206 WLog_Print(audin->log, WLOG_ERROR, "context->OpenReply failed with error %" PRIu32 "",
207 error);
208
209 return error;
210}
211
212static UINT audin_server_recv_data_incoming(audin_server_context* context,
213 WINPR_ATTR_UNUSED wStream* s, const SNDIN_PDU* header)
214{
215 audin_server* audin = (audin_server*)context;
216 SNDIN_DATA_INCOMING pdu = { 0 };
217 UINT error = CHANNEL_RC_OK;
218
219 WINPR_ASSERT(context);
220 WINPR_ASSERT(header);
221
222 pdu.Header = *header;
223
224 IFCALLRET(context->IncomingData, error, context, &pdu);
225 if (error)
226 WLog_Print(audin->log, WLOG_ERROR, "context->IncomingData failed with error %" PRIu32 "",
227 error);
228
229 return error;
230}
231
232static UINT audin_server_recv_data(audin_server_context* context, wStream* s,
233 const SNDIN_PDU* header)
234{
235 audin_server* audin = (audin_server*)context;
236 SNDIN_DATA pdu = { 0 };
237 wStream dataBuffer = { 0 };
238 UINT error = CHANNEL_RC_OK;
239
240 WINPR_ASSERT(context);
241 WINPR_ASSERT(header);
242
243 pdu.Header = *header;
244
245 pdu.Data = Stream_StaticInit(&dataBuffer, Stream_Pointer(s), Stream_GetRemainingLength(s));
246
247 IFCALLRET(context->Data, error, context, &pdu);
248 if (error)
249 WLog_Print(audin->log, WLOG_ERROR, "context->Data failed with error %" PRIu32 "", error);
250
251 return error;
252}
253
254static UINT audin_server_recv_format_change(audin_server_context* context, wStream* s,
255 const SNDIN_PDU* header)
256{
257 audin_server* audin = (audin_server*)context;
258 SNDIN_FORMATCHANGE pdu = { 0 };
259 UINT error = CHANNEL_RC_OK;
260
261 WINPR_ASSERT(context);
262 WINPR_ASSERT(header);
263
264 pdu.Header = *header;
265
266 if (!Stream_CheckAndLogRequiredLengthWLog(audin->log, s, 4))
267 return ERROR_NO_DATA;
268
269 Stream_Read_UINT32(s, pdu.NewFormat);
270
271 IFCALLRET(context->ReceiveFormatChange, error, context, &pdu);
272 if (error)
273 WLog_Print(audin->log, WLOG_ERROR,
274 "context->ReceiveFormatChange failed with error %" PRIu32 "", error);
275
276 return error;
277}
278
279static DWORD WINAPI audin_server_thread_func(LPVOID arg)
280{
281 wStream* s = NULL;
282 void* buffer = NULL;
283 DWORD nCount = 0;
284 HANDLE events[8] = { 0 };
285 BOOL ready = FALSE;
286 HANDLE ChannelEvent = NULL;
287 DWORD BytesReturned = 0;
288 audin_server* audin = (audin_server*)arg;
289 UINT error = CHANNEL_RC_OK;
290 DWORD status = ERROR_INTERNAL_ERROR;
291
292 WINPR_ASSERT(audin);
293
294 if (WTSVirtualChannelQuery(audin->audin_channel, WTSVirtualEventHandle, &buffer,
295 &BytesReturned) == TRUE)
296 {
297 if (BytesReturned == sizeof(HANDLE))
298 ChannelEvent = *(HANDLE*)buffer;
299
300 WTSFreeMemory(buffer);
301 }
302 else
303 {
304 WLog_Print(audin->log, WLOG_ERROR, "WTSVirtualChannelQuery failed");
305 error = ERROR_INTERNAL_ERROR;
306 goto out;
307 }
308
309 nCount = 0;
310 events[nCount++] = audin->stopEvent;
311 events[nCount++] = ChannelEvent;
312
313 /* Wait for the client to confirm that the Audio Input dynamic channel is ready */
314
315 while (1)
316 {
317 status = WaitForMultipleObjects(nCount, events, FALSE, 100);
318
319 if (status == WAIT_FAILED)
320 {
321 error = GetLastError();
322 WLog_Print(audin->log, WLOG_ERROR,
323 "WaitForMultipleObjects failed with error %" PRIu32 "", error);
324 goto out;
325 }
326 if (status == WAIT_OBJECT_0)
327 goto out;
328
329 if (WTSVirtualChannelQuery(audin->audin_channel, WTSVirtualChannelReady, &buffer,
330 &BytesReturned) == FALSE)
331 {
332 WLog_Print(audin->log, WLOG_ERROR, "WTSVirtualChannelQuery failed");
333 error = ERROR_INTERNAL_ERROR;
334 goto out;
335 }
336
337 ready = *((BOOL*)buffer);
338 WTSFreeMemory(buffer);
339
340 if (ready)
341 break;
342 }
343
344 s = Stream_New(NULL, 4096);
345
346 if (!s)
347 {
348 WLog_Print(audin->log, WLOG_ERROR, "Stream_New failed!");
349 error = CHANNEL_RC_NO_MEMORY;
350 goto out;
351 }
352
353 if (ready)
354 {
355 SNDIN_VERSION version = { 0 };
356
357 version.Version = audin->context.serverVersion;
358
359 if ((error = audin->context.SendVersion(&audin->context, &version)))
360 {
361 WLog_Print(audin->log, WLOG_ERROR, "SendVersion failed with error %" PRIu32 "!", error);
362 goto out_capacity;
363 }
364 }
365
366 while (ready)
367 {
368 SNDIN_PDU header = { 0 };
369
370 if ((status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE)) == WAIT_OBJECT_0)
371 break;
372
373 if (status == WAIT_FAILED)
374 {
375 error = GetLastError();
376 WLog_Print(audin->log, WLOG_ERROR,
377 "WaitForMultipleObjects failed with error %" PRIu32 "", error);
378 break;
379 }
380 if (status == WAIT_OBJECT_0)
381 break;
382
383 Stream_SetPosition(s, 0);
384
385 if (!WTSVirtualChannelRead(audin->audin_channel, 0, NULL, 0, &BytesReturned))
386 {
387 WLog_Print(audin->log, WLOG_ERROR, "WTSVirtualChannelRead failed!");
388 error = ERROR_INTERNAL_ERROR;
389 break;
390 }
391
392 if (BytesReturned < 1)
393 continue;
394
395 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
396 break;
397
398 WINPR_ASSERT(Stream_Capacity(s) <= UINT32_MAX);
399 if (WTSVirtualChannelRead(audin->audin_channel, 0, Stream_BufferAs(s, char),
400 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
401 {
402 WLog_Print(audin->log, WLOG_ERROR, "WTSVirtualChannelRead failed!");
403 error = ERROR_INTERNAL_ERROR;
404 break;
405 }
406
407 Stream_SetLength(s, BytesReturned);
408 if (!Stream_CheckAndLogRequiredLengthWLog(audin->log, s, SNDIN_HEADER_SIZE))
409 {
410 error = ERROR_INTERNAL_ERROR;
411 break;
412 }
413
414 Stream_Read_UINT8(s, header.MessageId);
415
416 switch (header.MessageId)
417 {
418 case MSG_SNDIN_VERSION:
419 error = audin_server_recv_version(&audin->context, s, &header);
420 break;
421 case MSG_SNDIN_FORMATS:
422 error = audin_server_recv_formats(&audin->context, s, &header);
423 break;
424 case MSG_SNDIN_OPEN_REPLY:
425 error = audin_server_recv_open_reply(&audin->context, s, &header);
426 break;
427 case MSG_SNDIN_DATA_INCOMING:
428 error = audin_server_recv_data_incoming(&audin->context, s, &header);
429 break;
430 case MSG_SNDIN_DATA:
431 error = audin_server_recv_data(&audin->context, s, &header);
432 break;
433 case MSG_SNDIN_FORMATCHANGE:
434 error = audin_server_recv_format_change(&audin->context, s, &header);
435 break;
436 default:
437 WLog_Print(audin->log, WLOG_ERROR,
438 "audin_server_thread_func: unknown or invalid MessageId %" PRIu8 "",
439 header.MessageId);
440 error = ERROR_INVALID_DATA;
441 break;
442 }
443 if (error)
444 break;
445 }
446
447out_capacity:
448 Stream_Free(s, TRUE);
449out:
450 (void)WTSVirtualChannelClose(audin->audin_channel);
451 audin->audin_channel = NULL;
452
453 if (error && audin->context.rdpcontext)
454 setChannelError(audin->context.rdpcontext, error,
455 "audin_server_thread_func reported an error");
456
457 ExitThread(error);
458 return error;
459}
460
461static BOOL audin_server_open(audin_server_context* context)
462{
463 audin_server* audin = (audin_server*)context;
464
465 WINPR_ASSERT(audin);
466 if (!audin->thread)
467 {
468 PULONG pSessionId = NULL;
469 DWORD BytesReturned = 0;
470 audin->SessionId = WTS_CURRENT_SESSION;
471 UINT32 channelId = 0;
472 BOOL status = TRUE;
473
474 if (WTSQuerySessionInformationA(context->vcm, WTS_CURRENT_SESSION, WTSSessionId,
475 (LPSTR*)&pSessionId, &BytesReturned))
476 {
477 audin->SessionId = (DWORD)*pSessionId;
478 WTSFreeMemory(pSessionId);
479 }
480
481 audin->audin_channel = WTSVirtualChannelOpenEx(audin->SessionId, AUDIN_DVC_CHANNEL_NAME,
482 WTS_CHANNEL_OPTION_DYNAMIC);
483
484 if (!audin->audin_channel)
485 {
486 WLog_Print(audin->log, WLOG_ERROR, "WTSVirtualChannelOpenEx failed!");
487 return FALSE;
488 }
489
490 channelId = WTSChannelGetIdByHandle(audin->audin_channel);
491
492 IFCALLRET(context->ChannelIdAssigned, status, context, channelId);
493 if (!status)
494 {
495 WLog_Print(audin->log, WLOG_ERROR, "context->ChannelIdAssigned failed!");
496 return FALSE;
497 }
498
499 if (!(audin->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
500 {
501 WLog_Print(audin->log, WLOG_ERROR, "CreateEvent failed!");
502 return FALSE;
503 }
504
505 if (!(audin->thread =
506 CreateThread(NULL, 0, audin_server_thread_func, (void*)audin, 0, NULL)))
507 {
508 WLog_Print(audin->log, WLOG_ERROR, "CreateThread failed!");
509 (void)CloseHandle(audin->stopEvent);
510 audin->stopEvent = NULL;
511 return FALSE;
512 }
513
514 return TRUE;
515 }
516
517 WLog_Print(audin->log, WLOG_ERROR, "thread already running!");
518 return FALSE;
519}
520
521static BOOL audin_server_is_open(audin_server_context* context)
522{
523 audin_server* audin = (audin_server*)context;
524
525 WINPR_ASSERT(audin);
526 return audin->thread != NULL;
527}
528
529static BOOL audin_server_close(audin_server_context* context)
530{
531 audin_server* audin = (audin_server*)context;
532 WINPR_ASSERT(audin);
533
534 if (audin->thread)
535 {
536 (void)SetEvent(audin->stopEvent);
537
538 if (WaitForSingleObject(audin->thread, INFINITE) == WAIT_FAILED)
539 {
540 WLog_Print(audin->log, WLOG_ERROR, "WaitForSingleObject failed with error %" PRIu32 "",
541 GetLastError());
542 return FALSE;
543 }
544
545 (void)CloseHandle(audin->thread);
546 (void)CloseHandle(audin->stopEvent);
547 audin->thread = NULL;
548 audin->stopEvent = NULL;
549 }
550
551 if (audin->audin_channel)
552 {
553 (void)WTSVirtualChannelClose(audin->audin_channel);
554 audin->audin_channel = NULL;
555 }
556
557 audin->audin_negotiated_format = NULL;
558
559 return TRUE;
560}
561
562static wStream* audin_server_packet_new(wLog* log, size_t size, BYTE MessageId)
563{
564 WINPR_ASSERT(log);
565
566 /* Allocate what we need plus header bytes */
567 wStream* s = Stream_New(NULL, size + SNDIN_HEADER_SIZE);
568 if (!s)
569 {
570 WLog_Print(log, WLOG_ERROR, "Stream_New failed!");
571 return NULL;
572 }
573
574 Stream_Write_UINT8(s, MessageId);
575
576 return s;
577}
578
579static UINT audin_server_packet_send(audin_server_context* context, wStream* s)
580{
581 audin_server* audin = (audin_server*)context;
582 UINT error = CHANNEL_RC_OK;
583 ULONG written = 0;
584
585 WINPR_ASSERT(context);
586 WINPR_ASSERT(s);
587
588 const size_t pos = Stream_GetPosition(s);
589 WINPR_ASSERT(pos <= UINT32_MAX);
590 if (!WTSVirtualChannelWrite(audin->audin_channel, Stream_BufferAs(s, char), (UINT32)pos,
591 &written))
592 {
593 WLog_Print(audin->log, WLOG_ERROR, "WTSVirtualChannelWrite failed!");
594 error = ERROR_INTERNAL_ERROR;
595 goto out;
596 }
597
598 if (written < Stream_GetPosition(s))
599 {
600 WLog_Print(audin->log, WLOG_WARN, "Unexpected bytes written: %" PRIu32 "/%" PRIuz "",
601 written, Stream_GetPosition(s));
602 }
603
604out:
605 Stream_Free(s, TRUE);
606 return error;
607}
608
609static UINT audin_server_send_version(audin_server_context* context, const SNDIN_VERSION* version)
610{
611 audin_server* audin = (audin_server*)context;
612
613 WINPR_ASSERT(context);
614 WINPR_ASSERT(version);
615
616 wStream* s = audin_server_packet_new(audin->log, 4, MSG_SNDIN_VERSION);
617 if (!s)
618 return ERROR_NOT_ENOUGH_MEMORY;
619
620 Stream_Write_UINT32(s, version->Version);
621
622 return audin_server_packet_send(context, s);
623}
624
625static UINT audin_server_send_formats(audin_server_context* context, const SNDIN_FORMATS* formats)
626{
627 audin_server* audin = (audin_server*)context;
628
629 WINPR_ASSERT(audin);
630 WINPR_ASSERT(formats);
631
632 wStream* s = audin_server_packet_new(audin->log, 4 + 4 + 18, MSG_SNDIN_FORMATS);
633 if (!s)
634 return ERROR_NOT_ENOUGH_MEMORY;
635
636 Stream_Write_UINT32(s, formats->NumFormats);
637 Stream_Write_UINT32(s, formats->cbSizeFormatsPacket);
638
639 for (UINT32 i = 0; i < formats->NumFormats; ++i)
640 {
641 AUDIO_FORMAT* format = &formats->SoundFormats[i];
642
643 if (!audio_format_write(s, format))
644 {
645 WLog_Print(audin->log, WLOG_ERROR, "Failed to write audio format");
646 Stream_Free(s, TRUE);
647 return CHANNEL_RC_NO_MEMORY;
648 }
649 }
650
651 return audin_server_packet_send(context, s);
652}
653
654static UINT audin_server_send_open(audin_server_context* context, const SNDIN_OPEN* open)
655{
656 audin_server* audin = (audin_server*)context;
657 WINPR_ASSERT(audin);
658 WINPR_ASSERT(open);
659
660 wStream* s = audin_server_packet_new(audin->log, 4 + 4 + 18 + 22, MSG_SNDIN_OPEN);
661 if (!s)
662 return ERROR_NOT_ENOUGH_MEMORY;
663
664 Stream_Write_UINT32(s, open->FramesPerPacket);
665 Stream_Write_UINT32(s, open->initialFormat);
666
667 Stream_Write_UINT16(s, open->captureFormat.wFormatTag);
668 Stream_Write_UINT16(s, open->captureFormat.nChannels);
669 Stream_Write_UINT32(s, open->captureFormat.nSamplesPerSec);
670 Stream_Write_UINT32(s, open->captureFormat.nAvgBytesPerSec);
671 Stream_Write_UINT16(s, open->captureFormat.nBlockAlign);
672 Stream_Write_UINT16(s, open->captureFormat.wBitsPerSample);
673
674 if (open->ExtraFormatData)
675 {
676 Stream_Write_UINT16(s, 22); /* cbSize */
677
678 Stream_Write_UINT16(s, open->ExtraFormatData->Samples.wReserved);
679 Stream_Write_UINT32(s, open->ExtraFormatData->dwChannelMask);
680
681 Stream_Write_UINT32(s, open->ExtraFormatData->SubFormat.Data1);
682 Stream_Write_UINT16(s, open->ExtraFormatData->SubFormat.Data2);
683 Stream_Write_UINT16(s, open->ExtraFormatData->SubFormat.Data3);
684 Stream_Write_UINT8(s, open->ExtraFormatData->SubFormat.Data4[0]);
685 Stream_Write_UINT8(s, open->ExtraFormatData->SubFormat.Data4[1]);
686 Stream_Write_UINT8(s, open->ExtraFormatData->SubFormat.Data4[2]);
687 Stream_Write_UINT8(s, open->ExtraFormatData->SubFormat.Data4[3]);
688 Stream_Write_UINT8(s, open->ExtraFormatData->SubFormat.Data4[4]);
689 Stream_Write_UINT8(s, open->ExtraFormatData->SubFormat.Data4[5]);
690 Stream_Write_UINT8(s, open->ExtraFormatData->SubFormat.Data4[6]);
691 Stream_Write_UINT8(s, open->ExtraFormatData->SubFormat.Data4[7]);
692 }
693 else
694 {
695 WINPR_ASSERT(open->captureFormat.wFormatTag != WAVE_FORMAT_EXTENSIBLE);
696
697 Stream_Write_UINT16(s, 0); /* cbSize */
698 }
699
700 return audin_server_packet_send(context, s);
701}
702
703static UINT audin_server_send_format_change(audin_server_context* context,
704 const SNDIN_FORMATCHANGE* format_change)
705{
706 audin_server* audin = (audin_server*)context;
707
708 WINPR_ASSERT(context);
709 WINPR_ASSERT(format_change);
710
711 wStream* s = audin_server_packet_new(audin->log, 4, MSG_SNDIN_FORMATCHANGE);
712 if (!s)
713 return ERROR_NOT_ENOUGH_MEMORY;
714
715 Stream_Write_UINT32(s, format_change->NewFormat);
716
717 return audin_server_packet_send(context, s);
718}
719
720static UINT audin_server_receive_version_default(audin_server_context* audin_ctx,
721 const SNDIN_VERSION* version)
722{
723 audin_server* audin = (audin_server*)audin_ctx;
724 SNDIN_FORMATS formats = { 0 };
725
726 WINPR_ASSERT(audin);
727 WINPR_ASSERT(version);
728
729 if (version->Version == 0)
730 {
731 WLog_Print(audin->log, WLOG_ERROR, "Received invalid AUDIO_INPUT version from client");
732 return ERROR_INVALID_DATA;
733 }
734
735 WLog_Print(audin->log, WLOG_DEBUG, "AUDIO_INPUT version of client: %u", version->Version);
736
737 formats.NumFormats = audin->audin_n_server_formats;
738 formats.SoundFormats = audin->audin_server_formats;
739
740 return audin->context.SendFormats(&audin->context, &formats);
741}
742
743static UINT send_open(audin_server* audin)
744{
745 SNDIN_OPEN open = { 0 };
746
747 WINPR_ASSERT(audin);
748
749 open.FramesPerPacket = 441;
750 open.initialFormat = audin->audin_client_format_idx;
751 open.captureFormat.wFormatTag = WAVE_FORMAT_PCM;
752 open.captureFormat.nChannels = 2;
753 open.captureFormat.nSamplesPerSec = 44100;
754 open.captureFormat.nAvgBytesPerSec = 44100 * 2 * 2;
755 open.captureFormat.nBlockAlign = 4;
756 open.captureFormat.wBitsPerSample = 16;
757
758 WINPR_ASSERT(audin->context.SendOpen);
759 return audin->context.SendOpen(&audin->context, &open);
760}
761
762static UINT audin_server_receive_formats_default(audin_server_context* context,
763 const SNDIN_FORMATS* formats)
764{
765 audin_server* audin = (audin_server*)context;
766 WINPR_ASSERT(audin);
767 WINPR_ASSERT(formats);
768
769 if (audin->audin_negotiated_format)
770 {
771 WLog_Print(audin->log, WLOG_ERROR,
772 "Received client formats, but negotiation was already done");
773 return ERROR_INVALID_DATA;
774 }
775
776 for (UINT32 i = 0; i < audin->audin_n_server_formats; ++i)
777 {
778 for (UINT32 j = 0; j < formats->NumFormats; ++j)
779 {
780 if (audio_format_compatible(&audin->audin_server_formats[i], &formats->SoundFormats[j]))
781 {
782 audin->audin_negotiated_format = &audin->audin_server_formats[i];
783 audin->audin_client_format_idx = i;
784 return send_open(audin);
785 }
786 }
787 }
788
789 WLog_Print(audin->log, WLOG_ERROR, "Could not agree on a audio format with the server");
790
791 return ERROR_INVALID_DATA;
792}
793
794static UINT audin_server_receive_format_change_default(audin_server_context* context,
795 const SNDIN_FORMATCHANGE* format_change)
796{
797 audin_server* audin = (audin_server*)context;
798
799 WINPR_ASSERT(audin);
800 WINPR_ASSERT(format_change);
801
802 if (format_change->NewFormat != audin->audin_client_format_idx)
803 {
804 WLog_Print(audin->log, WLOG_ERROR,
805 "NewFormat in FormatChange differs from requested format");
806 return ERROR_INVALID_DATA;
807 }
808
809 WLog_Print(audin->log, WLOG_DEBUG, "Received Format Change PDU: %u", format_change->NewFormat);
810
811 return CHANNEL_RC_OK;
812}
813
814static UINT
815audin_server_incoming_data_default(audin_server_context* context,
816 WINPR_ATTR_UNUSED const SNDIN_DATA_INCOMING* data_incoming)
817{
818 audin_server* audin = (audin_server*)context;
819 WINPR_ASSERT(audin);
820 WINPR_ASSERT(data_incoming);
821
822 /* TODO: Implement bandwidth measure of clients uplink */
823 WLog_Print(audin->log, WLOG_DEBUG, "Received Incoming Data PDU");
824 return CHANNEL_RC_OK;
825}
826
827static UINT audin_server_open_reply_default(audin_server_context* context,
828 const SNDIN_OPEN_REPLY* open_reply)
829{
830 audin_server* audin = (audin_server*)context;
831 WINPR_ASSERT(audin);
832 WINPR_ASSERT(open_reply);
833
834 /* TODO: Implement failure handling */
835 WLog_Print(audin->log, WLOG_DEBUG, "Open Reply PDU: Result: %" PRIu32, open_reply->Result);
836 return CHANNEL_RC_OK;
837}
838
839audin_server_context* audin_server_context_new(HANDLE vcm)
840{
841 audin_server* audin = (audin_server*)calloc(1, sizeof(audin_server));
842
843 if (!audin)
844 {
845 WLog_ERR(AUDIN_TAG, "calloc failed!");
846 return NULL;
847 }
848 audin->log = WLog_Get(AUDIN_TAG);
849 audin->context.vcm = vcm;
850 audin->context.Open = audin_server_open;
851 audin->context.IsOpen = audin_server_is_open;
852 audin->context.Close = audin_server_close;
853
854 audin->context.SendVersion = audin_server_send_version;
855 audin->context.SendFormats = audin_server_send_formats;
856 audin->context.SendOpen = audin_server_send_open;
857 audin->context.SendFormatChange = audin_server_send_format_change;
858
859 /* Default values */
860 audin->context.serverVersion = SNDIN_VERSION_Version_2;
861 audin->context.ReceiveVersion = audin_server_receive_version_default;
862 audin->context.ReceiveFormats = audin_server_receive_formats_default;
863 audin->context.ReceiveFormatChange = audin_server_receive_format_change_default;
864 audin->context.IncomingData = audin_server_incoming_data_default;
865 audin->context.OpenReply = audin_server_open_reply_default;
866
867 return &audin->context;
868}
869
870void audin_server_context_free(audin_server_context* context)
871{
872 audin_server* audin = (audin_server*)context;
873
874 if (!audin)
875 return;
876
877 audin_server_close(context);
878 audio_formats_free(audin->audin_server_formats, audin->audin_n_server_formats);
879 audin->audin_server_formats = NULL;
880 free(audin);
881}
882
883BOOL audin_server_set_formats(audin_server_context* context, SSIZE_T count,
884 const AUDIO_FORMAT* formats)
885{
886 audin_server* audin = (audin_server*)context;
887 WINPR_ASSERT(audin);
888
889 audio_formats_free(audin->audin_server_formats, audin->audin_n_server_formats);
890 audin->audin_n_server_formats = 0;
891 audin->audin_server_formats = NULL;
892 audin->audin_negotiated_format = NULL;
893
894 if (count < 0)
895 {
896 const size_t audin_n_server_formats =
897 server_audin_get_formats(&audin->audin_server_formats);
898 WINPR_ASSERT(audin_n_server_formats <= UINT32_MAX);
899
900 audin->audin_n_server_formats = (UINT32)audin_n_server_formats;
901 }
902 else
903 {
904 const size_t scount = (size_t)count;
905 AUDIO_FORMAT* audin_server_formats = audio_formats_new(scount);
906 if (!audin_server_formats)
907 return count == 0;
908
909 for (SSIZE_T x = 0; x < count; x++)
910 {
911 if (!audio_format_copy(&formats[x], &audin_server_formats[x]))
912 {
913 audio_formats_free(audin_server_formats, scount);
914 return FALSE;
915 }
916 }
917
918 WINPR_ASSERT(count <= UINT32_MAX);
919 audin->audin_server_formats = audin_server_formats;
920 audin->audin_n_server_formats = (UINT32)count;
921 }
922 return audin->audin_n_server_formats > 0;
923}
924
925const AUDIO_FORMAT* audin_server_get_negotiated_format(const audin_server_context* context)
926{
927 const audin_server* audin = (const audin_server*)context;
928 WINPR_ASSERT(audin);
929
930 return audin->audin_negotiated_format;
931}