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