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 = WINPR_C_ARRAY_INIT;
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 = WINPR_C_ARRAY_INIT;
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 error = ERROR_INVALID_DATA;
152 goto fail;
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 = WINPR_C_ARRAY_INIT;
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 = WINPR_C_ARRAY_INIT;
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 = WINPR_C_ARRAY_INIT;
237 wStream dataBuffer = WINPR_C_ARRAY_INIT;
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 = WINPR_C_ARRAY_INIT;
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 = nullptr;
282 void* buffer = nullptr;
283 DWORD nCount = 0;
284 HANDLE events[8] = WINPR_C_ARRAY_INIT;
285 BOOL ready = FALSE;
286 HANDLE ChannelEvent = nullptr;
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(nullptr, 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 = WINPR_C_ARRAY_INIT;
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 = WINPR_C_ARRAY_INIT;
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_ResetPosition(s);
384
385 if (!WTSVirtualChannelRead(audin->audin_channel, 0, nullptr, 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 const ULONG len = WINPR_ASSERTING_INT_CAST(ULONG, Stream_Capacity(s));
399 if (WTSVirtualChannelRead(audin->audin_channel, 0, Stream_BufferAs(s, char), len,
400 &BytesReturned) == FALSE)
401 {
402 WLog_Print(audin->log, WLOG_ERROR, "WTSVirtualChannelRead failed!");
403 error = ERROR_INTERNAL_ERROR;
404 break;
405 }
406
407 if (!Stream_SafeSeek(s, BytesReturned))
408 {
409 error = ERROR_INTERNAL_ERROR;
410 break;
411 }
412
413 if (!Stream_CheckAndLogRequiredLengthWLog(audin->log, s, SNDIN_HEADER_SIZE))
414 {
415 error = ERROR_INTERNAL_ERROR;
416 break;
417 }
418
419 Stream_Read_UINT8(s, header.MessageId);
420
421 switch (header.MessageId)
422 {
423 case MSG_SNDIN_VERSION:
424 error = audin_server_recv_version(&audin->context, s, &header);
425 break;
426 case MSG_SNDIN_FORMATS:
427 error = audin_server_recv_formats(&audin->context, s, &header);
428 break;
429 case MSG_SNDIN_OPEN_REPLY:
430 error = audin_server_recv_open_reply(&audin->context, s, &header);
431 break;
432 case MSG_SNDIN_DATA_INCOMING:
433 error = audin_server_recv_data_incoming(&audin->context, s, &header);
434 break;
435 case MSG_SNDIN_DATA:
436 error = audin_server_recv_data(&audin->context, s, &header);
437 break;
438 case MSG_SNDIN_FORMATCHANGE:
439 error = audin_server_recv_format_change(&audin->context, s, &header);
440 break;
441 default:
442 WLog_Print(audin->log, WLOG_ERROR,
443 "audin_server_thread_func: unknown or invalid MessageId %" PRIu8 "",
444 header.MessageId);
445 error = ERROR_INVALID_DATA;
446 break;
447 }
448 if (error)
449 break;
450 }
451
452out_capacity:
453 Stream_Free(s, TRUE);
454out:
455 (void)WTSVirtualChannelClose(audin->audin_channel);
456 audin->audin_channel = nullptr;
457
458 if (error && audin->context.rdpcontext)
459 setChannelError(audin->context.rdpcontext, error,
460 "audin_server_thread_func reported an error");
461
462 ExitThread(error);
463 return error;
464}
465
466static BOOL audin_server_open(audin_server_context* context)
467{
468 audin_server* audin = (audin_server*)context;
469
470 WINPR_ASSERT(audin);
471 if (!audin->thread)
472 {
473 PULONG pSessionId = nullptr;
474 DWORD BytesReturned = 0;
475 audin->SessionId = WTS_CURRENT_SESSION;
476 UINT32 channelId = 0;
477 BOOL status = TRUE;
478
479 if (WTSQuerySessionInformationA(context->vcm, WTS_CURRENT_SESSION, WTSSessionId,
480 (LPSTR*)&pSessionId, &BytesReturned))
481 {
482 audin->SessionId = (DWORD)*pSessionId;
483 WTSFreeMemory(pSessionId);
484 }
485
486 audin->audin_channel = WTSVirtualChannelOpenEx(audin->SessionId, AUDIN_DVC_CHANNEL_NAME,
487 WTS_CHANNEL_OPTION_DYNAMIC);
488
489 if (!audin->audin_channel)
490 {
491 WLog_Print(audin->log, WLOG_ERROR, "WTSVirtualChannelOpenEx failed!");
492 return FALSE;
493 }
494
495 channelId = WTSChannelGetIdByHandle(audin->audin_channel);
496
497 IFCALLRET(context->ChannelIdAssigned, status, context, channelId);
498 if (!status)
499 {
500 WLog_Print(audin->log, WLOG_ERROR, "context->ChannelIdAssigned failed!");
501 return FALSE;
502 }
503
504 if (!(audin->stopEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr)))
505 {
506 WLog_Print(audin->log, WLOG_ERROR, "CreateEvent failed!");
507 return FALSE;
508 }
509
510 if (!(audin->thread =
511 CreateThread(nullptr, 0, audin_server_thread_func, (void*)audin, 0, nullptr)))
512 {
513 WLog_Print(audin->log, WLOG_ERROR, "CreateThread failed!");
514 (void)CloseHandle(audin->stopEvent);
515 audin->stopEvent = nullptr;
516 return FALSE;
517 }
518
519 return TRUE;
520 }
521
522 WLog_Print(audin->log, WLOG_ERROR, "thread already running!");
523 return FALSE;
524}
525
526static BOOL audin_server_is_open(audin_server_context* context)
527{
528 audin_server* audin = (audin_server*)context;
529
530 WINPR_ASSERT(audin);
531 return audin->thread != nullptr;
532}
533
534static BOOL audin_server_close(audin_server_context* context)
535{
536 audin_server* audin = (audin_server*)context;
537 WINPR_ASSERT(audin);
538
539 if (audin->thread)
540 {
541 (void)SetEvent(audin->stopEvent);
542
543 if (WaitForSingleObject(audin->thread, INFINITE) == WAIT_FAILED)
544 {
545 WLog_Print(audin->log, WLOG_ERROR, "WaitForSingleObject failed with error %" PRIu32 "",
546 GetLastError());
547 return FALSE;
548 }
549
550 (void)CloseHandle(audin->thread);
551 (void)CloseHandle(audin->stopEvent);
552 audin->thread = nullptr;
553 audin->stopEvent = nullptr;
554 }
555
556 if (audin->audin_channel)
557 {
558 (void)WTSVirtualChannelClose(audin->audin_channel);
559 audin->audin_channel = nullptr;
560 }
561
562 audin->audin_negotiated_format = nullptr;
563
564 return TRUE;
565}
566
567static wStream* audin_server_packet_new(wLog* log, size_t size, BYTE MessageId)
568{
569 WINPR_ASSERT(log);
570
571 /* Allocate what we need plus header bytes */
572 wStream* s = Stream_New(nullptr, size + SNDIN_HEADER_SIZE);
573 if (!s)
574 {
575 WLog_Print(log, WLOG_ERROR, "Stream_New failed!");
576 return nullptr;
577 }
578
579 Stream_Write_UINT8(s, MessageId);
580
581 return s;
582}
583
584static UINT audin_server_packet_send(audin_server_context* context, wStream* s)
585{
586 audin_server* audin = (audin_server*)context;
587 UINT error = CHANNEL_RC_OK;
588 ULONG written = 0;
589
590 WINPR_ASSERT(context);
591 WINPR_ASSERT(s);
592
593 const size_t pos = Stream_GetPosition(s);
594 WINPR_ASSERT(pos <= UINT32_MAX);
595 if (!WTSVirtualChannelWrite(audin->audin_channel, Stream_BufferAs(s, char), (UINT32)pos,
596 &written))
597 {
598 WLog_Print(audin->log, WLOG_ERROR, "WTSVirtualChannelWrite failed!");
599 error = ERROR_INTERNAL_ERROR;
600 goto out;
601 }
602
603 if (written < Stream_GetPosition(s))
604 {
605 WLog_Print(audin->log, WLOG_WARN, "Unexpected bytes written: %" PRIu32 "/%" PRIuz "",
606 written, Stream_GetPosition(s));
607 }
608
609out:
610 Stream_Free(s, TRUE);
611 return error;
612}
613
614static UINT audin_server_send_version(audin_server_context* context, const SNDIN_VERSION* version)
615{
616 audin_server* audin = (audin_server*)context;
617
618 WINPR_ASSERT(context);
619 WINPR_ASSERT(version);
620
621 wStream* s = audin_server_packet_new(audin->log, 4, MSG_SNDIN_VERSION);
622 if (!s)
623 return ERROR_NOT_ENOUGH_MEMORY;
624
625 Stream_Write_UINT32(s, version->Version);
626
627 return audin_server_packet_send(context, s);
628}
629
630static UINT audin_server_send_formats(audin_server_context* context, const SNDIN_FORMATS* formats)
631{
632 audin_server* audin = (audin_server*)context;
633
634 WINPR_ASSERT(audin);
635 WINPR_ASSERT(formats);
636
637 wStream* s = audin_server_packet_new(audin->log, 4 + 4 + 18, MSG_SNDIN_FORMATS);
638 if (!s)
639 return ERROR_NOT_ENOUGH_MEMORY;
640
641 Stream_Write_UINT32(s, formats->NumFormats);
642 Stream_Write_UINT32(s, formats->cbSizeFormatsPacket);
643
644 for (UINT32 i = 0; i < formats->NumFormats; ++i)
645 {
646 AUDIO_FORMAT* format = &formats->SoundFormats[i];
647
648 if (!audio_format_write(s, format))
649 {
650 WLog_Print(audin->log, WLOG_ERROR, "Failed to write audio format");
651 Stream_Free(s, TRUE);
652 return CHANNEL_RC_NO_MEMORY;
653 }
654 }
655
656 return audin_server_packet_send(context, s);
657}
658
659static UINT audin_server_send_open(audin_server_context* context, const SNDIN_OPEN* open)
660{
661 audin_server* audin = (audin_server*)context;
662 WINPR_ASSERT(audin);
663 WINPR_ASSERT(open);
664
665 wStream* s = audin_server_packet_new(audin->log, 4 + 4 + 18 + 22, MSG_SNDIN_OPEN);
666 if (!s)
667 return ERROR_NOT_ENOUGH_MEMORY;
668
669 Stream_Write_UINT32(s, open->FramesPerPacket);
670 Stream_Write_UINT32(s, open->initialFormat);
671
672 Stream_Write_UINT16(s, open->captureFormat.wFormatTag);
673 Stream_Write_UINT16(s, open->captureFormat.nChannels);
674 Stream_Write_UINT32(s, open->captureFormat.nSamplesPerSec);
675 Stream_Write_UINT32(s, open->captureFormat.nAvgBytesPerSec);
676 Stream_Write_UINT16(s, open->captureFormat.nBlockAlign);
677 Stream_Write_UINT16(s, open->captureFormat.wBitsPerSample);
678
679 if (open->ExtraFormatData)
680 {
681 Stream_Write_UINT16(s, 22); /* cbSize */
682
683 Stream_Write_UINT16(s, open->ExtraFormatData->Samples.wReserved);
684 Stream_Write_UINT32(s, open->ExtraFormatData->dwChannelMask);
685
686 Stream_Write_UINT32(s, open->ExtraFormatData->SubFormat.Data1);
687 Stream_Write_UINT16(s, open->ExtraFormatData->SubFormat.Data2);
688 Stream_Write_UINT16(s, open->ExtraFormatData->SubFormat.Data3);
689 Stream_Write_UINT8(s, open->ExtraFormatData->SubFormat.Data4[0]);
690 Stream_Write_UINT8(s, open->ExtraFormatData->SubFormat.Data4[1]);
691 Stream_Write_UINT8(s, open->ExtraFormatData->SubFormat.Data4[2]);
692 Stream_Write_UINT8(s, open->ExtraFormatData->SubFormat.Data4[3]);
693 Stream_Write_UINT8(s, open->ExtraFormatData->SubFormat.Data4[4]);
694 Stream_Write_UINT8(s, open->ExtraFormatData->SubFormat.Data4[5]);
695 Stream_Write_UINT8(s, open->ExtraFormatData->SubFormat.Data4[6]);
696 Stream_Write_UINT8(s, open->ExtraFormatData->SubFormat.Data4[7]);
697 }
698 else
699 {
700 WINPR_ASSERT(open->captureFormat.wFormatTag != WAVE_FORMAT_EXTENSIBLE);
701
702 Stream_Write_UINT16(s, 0); /* cbSize */
703 }
704
705 return audin_server_packet_send(context, s);
706}
707
708static UINT audin_server_send_format_change(audin_server_context* context,
709 const SNDIN_FORMATCHANGE* format_change)
710{
711 audin_server* audin = (audin_server*)context;
712
713 WINPR_ASSERT(context);
714 WINPR_ASSERT(format_change);
715
716 wStream* s = audin_server_packet_new(audin->log, 4, MSG_SNDIN_FORMATCHANGE);
717 if (!s)
718 return ERROR_NOT_ENOUGH_MEMORY;
719
720 Stream_Write_UINT32(s, format_change->NewFormat);
721
722 return audin_server_packet_send(context, s);
723}
724
725static UINT audin_server_receive_version_default(audin_server_context* audin_ctx,
726 const SNDIN_VERSION* version)
727{
728 audin_server* audin = (audin_server*)audin_ctx;
729 SNDIN_FORMATS formats = WINPR_C_ARRAY_INIT;
730
731 WINPR_ASSERT(audin);
732 WINPR_ASSERT(version);
733
734 if (version->Version == 0)
735 {
736 WLog_Print(audin->log, WLOG_ERROR, "Received invalid AUDIO_INPUT version from client");
737 return ERROR_INVALID_DATA;
738 }
739
740 WLog_Print(audin->log, WLOG_DEBUG, "AUDIO_INPUT version of client: %u", version->Version);
741
742 formats.NumFormats = audin->audin_n_server_formats;
743 formats.SoundFormats = audin->audin_server_formats;
744
745 return audin->context.SendFormats(&audin->context, &formats);
746}
747
748static UINT send_open(audin_server* audin)
749{
750 SNDIN_OPEN open = WINPR_C_ARRAY_INIT;
751
752 WINPR_ASSERT(audin);
753
754 open.FramesPerPacket = 441;
755 open.initialFormat = audin->audin_client_format_idx;
756 open.captureFormat.wFormatTag = WAVE_FORMAT_PCM;
757 open.captureFormat.nChannels = 2;
758 open.captureFormat.nSamplesPerSec = 44100;
759 open.captureFormat.nAvgBytesPerSec = 44100 * 2 * 2;
760 open.captureFormat.nBlockAlign = 4;
761 open.captureFormat.wBitsPerSample = 16;
762
763 WINPR_ASSERT(audin->context.SendOpen);
764 return audin->context.SendOpen(&audin->context, &open);
765}
766
767static UINT audin_server_receive_formats_default(audin_server_context* context,
768 const SNDIN_FORMATS* formats)
769{
770 audin_server* audin = (audin_server*)context;
771 WINPR_ASSERT(audin);
772 WINPR_ASSERT(formats);
773
774 if (audin->audin_negotiated_format)
775 {
776 WLog_Print(audin->log, WLOG_ERROR,
777 "Received client formats, but negotiation was already done");
778 return ERROR_INVALID_DATA;
779 }
780
781 for (UINT32 i = 0; i < audin->audin_n_server_formats; ++i)
782 {
783 for (UINT32 j = 0; j < formats->NumFormats; ++j)
784 {
785 if (audio_format_compatible(&audin->audin_server_formats[i], &formats->SoundFormats[j]))
786 {
787 audin->audin_negotiated_format = &audin->audin_server_formats[i];
788 audin->audin_client_format_idx = i;
789 return send_open(audin);
790 }
791 }
792 }
793
794 WLog_Print(audin->log, WLOG_ERROR, "Could not agree on a audio format with the server");
795
796 return ERROR_INVALID_DATA;
797}
798
799static UINT audin_server_receive_format_change_default(audin_server_context* context,
800 const SNDIN_FORMATCHANGE* format_change)
801{
802 audin_server* audin = (audin_server*)context;
803
804 WINPR_ASSERT(audin);
805 WINPR_ASSERT(format_change);
806
807 if (format_change->NewFormat != audin->audin_client_format_idx)
808 {
809 WLog_Print(audin->log, WLOG_ERROR,
810 "NewFormat in FormatChange differs from requested format");
811 return ERROR_INVALID_DATA;
812 }
813
814 WLog_Print(audin->log, WLOG_DEBUG, "Received Format Change PDU: %u", format_change->NewFormat);
815
816 return CHANNEL_RC_OK;
817}
818
819static UINT
820audin_server_incoming_data_default(audin_server_context* context,
821 WINPR_ATTR_UNUSED const SNDIN_DATA_INCOMING* data_incoming)
822{
823 audin_server* audin = (audin_server*)context;
824 WINPR_ASSERT(audin);
825 WINPR_ASSERT(data_incoming);
826
827 /* TODO: Implement bandwidth measure of clients uplink */
828 WLog_Print(audin->log, WLOG_DEBUG, "Received Incoming Data PDU");
829 return CHANNEL_RC_OK;
830}
831
832static UINT audin_server_open_reply_default(audin_server_context* context,
833 const SNDIN_OPEN_REPLY* open_reply)
834{
835 audin_server* audin = (audin_server*)context;
836 WINPR_ASSERT(audin);
837 WINPR_ASSERT(open_reply);
838
839 /* TODO: Implement failure handling */
840 WLog_Print(audin->log, WLOG_DEBUG, "Open Reply PDU: Result: %" PRIu32, open_reply->Result);
841 return CHANNEL_RC_OK;
842}
843
844audin_server_context* audin_server_context_new(HANDLE vcm)
845{
846 audin_server* audin = (audin_server*)calloc(1, sizeof(audin_server));
847
848 if (!audin)
849 {
850 WLog_ERR(AUDIN_TAG, "calloc failed!");
851 return nullptr;
852 }
853 audin->log = WLog_Get(AUDIN_TAG);
854 audin->context.vcm = vcm;
855 audin->context.Open = audin_server_open;
856 audin->context.IsOpen = audin_server_is_open;
857 audin->context.Close = audin_server_close;
858
859 audin->context.SendVersion = audin_server_send_version;
860 audin->context.SendFormats = audin_server_send_formats;
861 audin->context.SendOpen = audin_server_send_open;
862 audin->context.SendFormatChange = audin_server_send_format_change;
863
864 /* Default values */
865 audin->context.serverVersion = SNDIN_VERSION_Version_2;
866 audin->context.ReceiveVersion = audin_server_receive_version_default;
867 audin->context.ReceiveFormats = audin_server_receive_formats_default;
868 audin->context.ReceiveFormatChange = audin_server_receive_format_change_default;
869 audin->context.IncomingData = audin_server_incoming_data_default;
870 audin->context.OpenReply = audin_server_open_reply_default;
871
872 return &audin->context;
873}
874
875void audin_server_context_free(audin_server_context* context)
876{
877 audin_server* audin = (audin_server*)context;
878
879 if (!audin)
880 return;
881
882 audin_server_close(context);
883 audio_formats_free(audin->audin_server_formats, audin->audin_n_server_formats);
884 audin->audin_server_formats = nullptr;
885 free(audin);
886}
887
888BOOL audin_server_set_formats(audin_server_context* context, SSIZE_T count,
889 const AUDIO_FORMAT* formats)
890{
891 audin_server* audin = (audin_server*)context;
892 WINPR_ASSERT(audin);
893
894 audio_formats_free(audin->audin_server_formats, audin->audin_n_server_formats);
895 audin->audin_n_server_formats = 0;
896 audin->audin_server_formats = nullptr;
897 audin->audin_negotiated_format = nullptr;
898
899 if (count < 0)
900 {
901 const size_t audin_n_server_formats =
902 server_audin_get_formats(&audin->audin_server_formats);
903 WINPR_ASSERT(audin_n_server_formats <= UINT32_MAX);
904
905 audin->audin_n_server_formats = (UINT32)audin_n_server_formats;
906 }
907 else
908 {
909 const size_t scount = (size_t)count;
910 AUDIO_FORMAT* audin_server_formats = audio_formats_new(scount);
911 if (!audin_server_formats)
912 return count == 0;
913
914 for (SSIZE_T x = 0; x < count; x++)
915 {
916 if (!audio_format_copy(&formats[x], &audin_server_formats[x]))
917 {
918 audio_formats_free(audin_server_formats, scount);
919 return FALSE;
920 }
921 }
922
923 WINPR_ASSERT(count <= UINT32_MAX);
924 audin->audin_server_formats = audin_server_formats;
925 audin->audin_n_server_formats = (UINT32)count;
926 }
927 return audin->audin_n_server_formats > 0;
928}
929
930const AUDIO_FORMAT* audin_server_get_negotiated_format(const audin_server_context* context)
931{
932 const audin_server* audin = (const audin_server*)context;
933 WINPR_ASSERT(audin);
934
935 return audin->audin_negotiated_format;
936}