FreeRDP
Loading...
Searching...
No Matches
server/camera_device_main.c
1
20#include <winpr/cast.h>
21
22#include <freerdp/config.h>
23
24#include <freerdp/freerdp.h>
25#include <freerdp/channels/log.h>
26#include <freerdp/server/rdpecam.h>
27
28#include "rdpecam-utils.h"
29
30#define TAG CHANNELS_TAG("rdpecam.server")
31
32typedef enum
33{
34 CAMERA_DEVICE_INITIAL,
35 CAMERA_DEVICE_OPENED,
36} eCameraDeviceChannelState;
37
38typedef struct
39{
40 CameraDeviceServerContext context;
41
42 HANDLE stopEvent;
43
44 HANDLE thread;
45 void* device_channel;
46
47 DWORD SessionId;
48
49 BOOL isOpened;
50 BOOL externalThread;
51
52 /* Channel state */
53 eCameraDeviceChannelState state;
54
55 wStream* buffer;
56} device_server;
57
58static UINT device_server_initialize(CameraDeviceServerContext* context, BOOL externalThread)
59{
60 UINT error = CHANNEL_RC_OK;
61 device_server* device = (device_server*)context;
62
63 WINPR_ASSERT(device);
64
65 if (device->isOpened)
66 {
67 WLog_WARN(TAG, "Application error: Camera channel already initialized, "
68 "calling in this state is not possible!");
69 return ERROR_INVALID_STATE;
70 }
71
72 device->externalThread = externalThread;
73
74 return error;
75}
76
77static UINT device_server_open_channel(device_server* device)
78{
79 CameraDeviceServerContext* context = &device->context;
80 DWORD Error = ERROR_SUCCESS;
81 HANDLE hEvent = NULL;
82 DWORD BytesReturned = 0;
83 PULONG pSessionId = NULL;
84 UINT32 channelId = 0;
85 BOOL status = TRUE;
86
87 WINPR_ASSERT(device);
88
89 if (WTSQuerySessionInformationA(device->context.vcm, WTS_CURRENT_SESSION, WTSSessionId,
90 (LPSTR*)&pSessionId, &BytesReturned) == FALSE)
91 {
92 WLog_ERR(TAG, "WTSQuerySessionInformationA failed!");
93 return ERROR_INTERNAL_ERROR;
94 }
95
96 device->SessionId = (DWORD)*pSessionId;
97 WTSFreeMemory(pSessionId);
98 hEvent = WTSVirtualChannelManagerGetEventHandle(device->context.vcm);
99
100 if (WaitForSingleObject(hEvent, 1000) == WAIT_FAILED)
101 {
102 Error = GetLastError();
103 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", Error);
104 return Error;
105 }
106
107 device->device_channel = WTSVirtualChannelOpenEx(device->SessionId, context->virtualChannelName,
108 WTS_CHANNEL_OPTION_DYNAMIC);
109 if (!device->device_channel)
110 {
111 Error = GetLastError();
112 WLog_ERR(TAG, "WTSVirtualChannelOpenEx failed with error %" PRIu32 "!", Error);
113 return Error;
114 }
115
116 channelId = WTSChannelGetIdByHandle(device->device_channel);
117
118 IFCALLRET(context->ChannelIdAssigned, status, context, channelId);
119 if (!status)
120 {
121 WLog_ERR(TAG, "context->ChannelIdAssigned failed!");
122 return ERROR_INTERNAL_ERROR;
123 }
124
125 return Error;
126}
127
128static UINT device_server_handle_success_response(CameraDeviceServerContext* context,
129 WINPR_ATTR_UNUSED wStream* s,
130 const CAM_SHARED_MSG_HEADER* header)
131{
132 CAM_SUCCESS_RESPONSE pdu = { 0 };
133 UINT error = CHANNEL_RC_OK;
134
135 WINPR_ASSERT(context);
136 WINPR_ASSERT(header);
137
138 pdu.Header = *header;
139
140 IFCALLRET(context->SuccessResponse, error, context, &pdu);
141 if (error)
142 WLog_ERR(TAG, "context->SuccessResponse failed with error %" PRIu32 "", error);
143
144 return error;
145}
146
147static UINT device_server_recv_error_response(CameraDeviceServerContext* context, wStream* s,
148 const CAM_SHARED_MSG_HEADER* header)
149{
150 CAM_ERROR_RESPONSE pdu = { 0 };
151 UINT error = CHANNEL_RC_OK;
152
153 WINPR_ASSERT(context);
154 WINPR_ASSERT(header);
155
156 pdu.Header = *header;
157
158 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
159 return ERROR_NO_DATA;
160
161 {
162 const UINT32 val = Stream_Get_UINT32(s);
163 if (!rdpecam_valid_CamErrorCode(val))
164 return ERROR_INVALID_DATA;
165 pdu.ErrorCode = (CAM_ERROR_CODE)val;
166 }
167
168 IFCALLRET(context->ErrorResponse, error, context, &pdu);
169 if (error)
170 WLog_ERR(TAG, "context->ErrorResponse failed with error %" PRIu32 "", error);
171
172 return error;
173}
174
175static UINT device_server_recv_stream_list_response(CameraDeviceServerContext* context, wStream* s,
176 const CAM_SHARED_MSG_HEADER* header)
177{
178 CAM_STREAM_LIST_RESPONSE pdu = { 0 };
179 UINT error = CHANNEL_RC_OK;
180
181 WINPR_ASSERT(context);
182 WINPR_ASSERT(header);
183
184 pdu.Header = *header;
185
186 if (!Stream_CheckAndLogRequiredLength(TAG, s, 5))
187 return ERROR_NO_DATA;
188
189 pdu.N_Descriptions = 255;
190 const size_t len = Stream_GetRemainingLength(s) / 5;
191 if (len < 255)
192 pdu.N_Descriptions = (BYTE)len;
193
194 for (BYTE i = 0; i < pdu.N_Descriptions; ++i)
195 {
196 CAM_STREAM_DESCRIPTION* StreamDescription = &pdu.StreamDescriptions[i];
197
198 {
199 const UINT16 val = Stream_Get_UINT16(s);
200 if (!rdpecam_valid_CamStreamFrameSourceType(val))
201 return ERROR_INVALID_DATA;
202 StreamDescription->FrameSourceTypes = (CAM_STREAM_FRAME_SOURCE_TYPES)val;
203 }
204 {
205 const UINT8 val = Stream_Get_UINT8(s);
206 if (!rdpecam_valid_CamStreamCategory(val))
207 return ERROR_INVALID_DATA;
208 StreamDescription->StreamCategory = (CAM_STREAM_CATEGORY)val;
209 }
210 Stream_Read_UINT8(s, StreamDescription->Selected);
211 Stream_Read_UINT8(s, StreamDescription->CanBeShared);
212 }
213
214 IFCALLRET(context->StreamListResponse, error, context, &pdu);
215 if (error)
216 WLog_ERR(TAG, "context->StreamListResponse failed with error %" PRIu32 "", error);
217
218 return error;
219}
220
221static UINT device_server_recv_media_type_list_response(CameraDeviceServerContext* context,
222 wStream* s,
223 const CAM_SHARED_MSG_HEADER* header)
224{
226 UINT error = CHANNEL_RC_OK;
227
228 WINPR_ASSERT(context);
229 WINPR_ASSERT(header);
230
231 pdu.Header = *header;
232
233 if (!Stream_CheckAndLogRequiredLength(TAG, s, 26))
234 return ERROR_NO_DATA;
235
236 pdu.N_Descriptions = Stream_GetRemainingLength(s) / 26;
237
238 pdu.MediaTypeDescriptions = calloc(pdu.N_Descriptions, sizeof(CAM_MEDIA_TYPE_DESCRIPTION));
239 if (!pdu.MediaTypeDescriptions)
240 {
241 WLog_ERR(TAG, "Failed to allocate %zu CAM_MEDIA_TYPE_DESCRIPTION structs",
242 pdu.N_Descriptions);
243 return ERROR_NOT_ENOUGH_MEMORY;
244 }
245
246 for (size_t i = 0; i < pdu.N_Descriptions; ++i)
247 {
248 CAM_MEDIA_TYPE_DESCRIPTION* MediaTypeDescriptions = &pdu.MediaTypeDescriptions[i];
249
250 {
251 const UINT8 val = Stream_Get_UINT8(s);
252 if (!rdpecam_valid_CamMediaFormat(val))
253 {
254 free(pdu.MediaTypeDescriptions);
255 return ERROR_INVALID_DATA;
256 }
257 MediaTypeDescriptions->Format = (CAM_MEDIA_FORMAT)val;
258 }
259 Stream_Read_UINT32(s, MediaTypeDescriptions->Width);
260 Stream_Read_UINT32(s, MediaTypeDescriptions->Height);
261 Stream_Read_UINT32(s, MediaTypeDescriptions->FrameRateNumerator);
262 Stream_Read_UINT32(s, MediaTypeDescriptions->FrameRateDenominator);
263 Stream_Read_UINT32(s, MediaTypeDescriptions->PixelAspectRatioNumerator);
264 Stream_Read_UINT32(s, MediaTypeDescriptions->PixelAspectRatioDenominator);
265 {
266 const UINT8 val = Stream_Get_UINT8(s);
267 if (!rdpecam_valid_MediaTypeDescriptionFlags(val))
268 {
269 free(pdu.MediaTypeDescriptions);
270 return ERROR_INVALID_DATA;
271 }
272 MediaTypeDescriptions->Flags = (CAM_MEDIA_TYPE_DESCRIPTION_FLAGS)val;
273 }
274 }
275
276 IFCALLRET(context->MediaTypeListResponse, error, context, &pdu);
277 if (error)
278 WLog_ERR(TAG, "context->MediaTypeListResponse failed with error %" PRIu32 "", error);
279
280 free(pdu.MediaTypeDescriptions);
281
282 return error;
283}
284
285static UINT device_server_recv_current_media_type_response(CameraDeviceServerContext* context,
286 wStream* s,
287 const CAM_SHARED_MSG_HEADER* header)
288{
290 UINT error = CHANNEL_RC_OK;
291
292 WINPR_ASSERT(context);
293 WINPR_ASSERT(header);
294
295 pdu.Header = *header;
296
297 if (!Stream_CheckAndLogRequiredLength(TAG, s, 26))
298 return ERROR_NO_DATA;
299
300 {
301 const UINT8 val = Stream_Get_UINT8(s);
302 if (!rdpecam_valid_CamMediaFormat(val))
303 return ERROR_INVALID_DATA;
304 pdu.MediaTypeDescription.Format = (CAM_MEDIA_FORMAT)val;
305 }
306
307 Stream_Read_UINT32(s, pdu.MediaTypeDescription.Width);
308 Stream_Read_UINT32(s, pdu.MediaTypeDescription.Height);
309 Stream_Read_UINT32(s, pdu.MediaTypeDescription.FrameRateNumerator);
310 Stream_Read_UINT32(s, pdu.MediaTypeDescription.FrameRateDenominator);
311 Stream_Read_UINT32(s, pdu.MediaTypeDescription.PixelAspectRatioNumerator);
312 Stream_Read_UINT32(s, pdu.MediaTypeDescription.PixelAspectRatioDenominator);
313 {
314 const UINT8 val = Stream_Get_UINT8(s);
315 if (!rdpecam_valid_MediaTypeDescriptionFlags(val))
316 return ERROR_INVALID_DATA;
317 pdu.MediaTypeDescription.Flags = (CAM_MEDIA_TYPE_DESCRIPTION_FLAGS)val;
318 }
319
320 IFCALLRET(context->CurrentMediaTypeResponse, error, context, &pdu);
321 if (error)
322 WLog_ERR(TAG, "context->CurrentMediaTypeResponse failed with error %" PRIu32 "", error);
323
324 return error;
325}
326
327static UINT device_server_recv_sample_response(CameraDeviceServerContext* context, wStream* s,
328 const CAM_SHARED_MSG_HEADER* header)
329{
330 CAM_SAMPLE_RESPONSE pdu = { 0 };
331 UINT error = CHANNEL_RC_OK;
332
333 WINPR_ASSERT(context);
334 WINPR_ASSERT(header);
335
336 pdu.Header = *header;
337
338 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
339 return ERROR_NO_DATA;
340
341 Stream_Read_UINT8(s, pdu.StreamIndex);
342
343 pdu.SampleSize = Stream_GetRemainingLength(s);
344 pdu.Sample = Stream_Pointer(s);
345
346 IFCALLRET(context->SampleResponse, error, context, &pdu);
347 if (error)
348 WLog_ERR(TAG, "context->SampleResponse failed with error %" PRIu32 "", error);
349
350 return error;
351}
352
353static UINT device_server_recv_sample_error_response(CameraDeviceServerContext* context, wStream* s,
354 const CAM_SHARED_MSG_HEADER* header)
355{
356 CAM_SAMPLE_ERROR_RESPONSE pdu = { 0 };
357 UINT error = CHANNEL_RC_OK;
358
359 WINPR_ASSERT(context);
360 WINPR_ASSERT(header);
361
362 pdu.Header = *header;
363
364 if (!Stream_CheckAndLogRequiredLength(TAG, s, 5))
365 return ERROR_NO_DATA;
366
367 Stream_Read_UINT8(s, pdu.StreamIndex);
368 {
369 const UINT32 val = Stream_Get_UINT32(s);
370 if (!rdpecam_valid_CamErrorCode(val))
371 return ERROR_INVALID_DATA;
372 pdu.ErrorCode = (CAM_ERROR_CODE)val;
373 }
374
375 IFCALLRET(context->SampleErrorResponse, error, context, &pdu);
376 if (error)
377 WLog_ERR(TAG, "context->SampleErrorResponse failed with error %" PRIu32 "", error);
378
379 return error;
380}
381
382static UINT device_server_recv_property_list_response(CameraDeviceServerContext* context,
383 wStream* s,
384 const CAM_SHARED_MSG_HEADER* header)
385{
386 CAM_PROPERTY_LIST_RESPONSE pdu = { 0 };
387 UINT error = ERROR_INVALID_DATA;
388
389 WINPR_ASSERT(context);
390 WINPR_ASSERT(header);
391
392 pdu.Header = *header;
393
394 pdu.N_Properties = Stream_GetRemainingLength(s) / 19;
395
396 if (pdu.N_Properties > 0)
397 {
398 pdu.Properties = calloc(pdu.N_Properties, sizeof(CAM_PROPERTY_DESCRIPTION));
399 if (!pdu.Properties)
400 {
401 WLog_ERR(TAG, "Failed to allocate %zu CAM_PROPERTY_DESCRIPTION structs",
402 pdu.N_Properties);
403 return ERROR_NOT_ENOUGH_MEMORY;
404 }
405
406 for (size_t i = 0; i < pdu.N_Properties; ++i)
407 {
408 CAM_PROPERTY_DESCRIPTION* cur = &pdu.Properties[i];
409 {
410 const UINT8 val = Stream_Get_UINT8(s);
411 if (!rdpecam_valid_CamPropertySet(val))
412 goto fail;
413 cur->PropertySet = (CAM_PROPERTY_SET)val;
414 }
415 cur->PropertyId = Stream_Get_UINT8(s);
416 cur->Capabilities = Stream_Get_UINT8(s);
417 if (!rdpecam_valid_CamPropertyCapabilities(cur->Capabilities))
418 goto fail;
419 cur->MinValue = Stream_Get_INT32(s);
420 cur->MaxValue = Stream_Get_INT32(s);
421 cur->Step = Stream_Get_INT32(s);
422 cur->DefaultValue = Stream_Get_INT32(s);
423 }
424 }
425
426 error = IFCALLRESULT(CHANNEL_RC_OK, context->PropertyListResponse, context, &pdu);
427
428fail:
429 if (error)
430 WLog_ERR(TAG, "context->PropertyListResponse failed with error %" PRIu32 "", error);
431
432 free(pdu.Properties);
433
434 return error;
435}
436
437static UINT device_server_recv_property_value_response(CameraDeviceServerContext* context,
438 wStream* s,
439 const CAM_SHARED_MSG_HEADER* header)
440{
441 CAM_PROPERTY_VALUE_RESPONSE pdu = { 0 };
442 UINT error = CHANNEL_RC_OK;
443
444 WINPR_ASSERT(context);
445 WINPR_ASSERT(header);
446
447 pdu.Header = *header;
448
449 if (!Stream_CheckAndLogRequiredLength(TAG, s, 5))
450 return ERROR_NO_DATA;
451
452 {
453 const UINT8 val = Stream_Get_UINT8(s);
454 if (!rdpecam_valid_CamPropertyMode(val))
455 return ERROR_INVALID_DATA;
456 pdu.PropertyValue.Mode = (CAM_PROPERTY_MODE)val;
457 }
458
459 Stream_Read_INT32(s, pdu.PropertyValue.Value);
460
461 IFCALLRET(context->PropertyValueResponse, error, context, &pdu);
462 if (error)
463 WLog_ERR(TAG, "context->PropertyValueResponse failed with error %" PRIu32 "", error);
464
465 return error;
466}
467
468static UINT device_process_message(device_server* device)
469{
470 BOOL rc = 0;
471 UINT error = ERROR_INTERNAL_ERROR;
472 ULONG BytesReturned = 0;
473 CAM_SHARED_MSG_HEADER header = { 0 };
474 wStream* s = NULL;
475
476 WINPR_ASSERT(device);
477 WINPR_ASSERT(device->device_channel);
478
479 s = device->buffer;
480 WINPR_ASSERT(s);
481
482 Stream_SetPosition(s, 0);
483 rc = WTSVirtualChannelRead(device->device_channel, 0, NULL, 0, &BytesReturned);
484 if (!rc)
485 goto out;
486
487 if (BytesReturned < 1)
488 {
489 error = CHANNEL_RC_OK;
490 goto out;
491 }
492
493 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
494 {
495 WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
496 error = CHANNEL_RC_NO_MEMORY;
497 goto out;
498 }
499
500 if (WTSVirtualChannelRead(device->device_channel, 0, Stream_BufferAs(s, char),
501 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
502 {
503 WLog_ERR(TAG, "WTSVirtualChannelRead failed!");
504 goto out;
505 }
506
507 Stream_SetLength(s, BytesReturned);
508 if (!Stream_CheckAndLogRequiredLength(TAG, s, CAM_HEADER_SIZE))
509 return ERROR_NO_DATA;
510
511 Stream_Read_UINT8(s, header.Version);
512 {
513 const UINT8 id = Stream_Get_UINT8(s);
514 if (!rdpecam_valid_messageId(id))
515 return ERROR_INVALID_DATA;
516 header.MessageId = (CAM_MSG_ID)id;
517 }
518
519 switch (header.MessageId)
520 {
521 case CAM_MSG_ID_SuccessResponse:
522 error = device_server_handle_success_response(&device->context, s, &header);
523 break;
524 case CAM_MSG_ID_ErrorResponse:
525 error = device_server_recv_error_response(&device->context, s, &header);
526 break;
527 case CAM_MSG_ID_StreamListResponse:
528 error = device_server_recv_stream_list_response(&device->context, s, &header);
529 break;
530 case CAM_MSG_ID_MediaTypeListResponse:
531 error = device_server_recv_media_type_list_response(&device->context, s, &header);
532 break;
533 case CAM_MSG_ID_CurrentMediaTypeResponse:
534 error = device_server_recv_current_media_type_response(&device->context, s, &header);
535 break;
536 case CAM_MSG_ID_SampleResponse:
537 error = device_server_recv_sample_response(&device->context, s, &header);
538 break;
539 case CAM_MSG_ID_SampleErrorResponse:
540 error = device_server_recv_sample_error_response(&device->context, s, &header);
541 break;
542 case CAM_MSG_ID_PropertyListResponse:
543 error = device_server_recv_property_list_response(&device->context, s, &header);
544 break;
545 case CAM_MSG_ID_PropertyValueResponse:
546 error = device_server_recv_property_value_response(&device->context, s, &header);
547 break;
548 default:
549 WLog_ERR(TAG, "device_process_message: unknown or invalid MessageId %" PRIu8 "",
550 header.MessageId);
551 break;
552 }
553
554out:
555 if (error)
556 WLog_ERR(TAG, "Response failed with error %" PRIu32 "!", error);
557
558 return error;
559}
560
561static UINT device_server_context_poll_int(CameraDeviceServerContext* context)
562{
563 device_server* device = (device_server*)context;
564 UINT error = ERROR_INTERNAL_ERROR;
565
566 WINPR_ASSERT(device);
567
568 switch (device->state)
569 {
570 case CAMERA_DEVICE_INITIAL:
571 error = device_server_open_channel(device);
572 if (error)
573 WLog_ERR(TAG, "device_server_open_channel failed with error %" PRIu32 "!", error);
574 else
575 device->state = CAMERA_DEVICE_OPENED;
576 break;
577 case CAMERA_DEVICE_OPENED:
578 error = device_process_message(device);
579 break;
580 default:
581 break;
582 }
583
584 return error;
585}
586
587static HANDLE device_server_get_channel_handle(device_server* device)
588{
589 void* buffer = NULL;
590 DWORD BytesReturned = 0;
591 HANDLE ChannelEvent = NULL;
592
593 WINPR_ASSERT(device);
594
595 if (WTSVirtualChannelQuery(device->device_channel, WTSVirtualEventHandle, &buffer,
596 &BytesReturned) == TRUE)
597 {
598 if (BytesReturned == sizeof(HANDLE))
599 ChannelEvent = *(HANDLE*)buffer;
600
601 WTSFreeMemory(buffer);
602 }
603
604 return ChannelEvent;
605}
606
607static DWORD WINAPI device_server_thread_func(LPVOID arg)
608{
609 DWORD nCount = 0;
610 HANDLE events[2] = { 0 };
611 device_server* device = (device_server*)arg;
612 UINT error = CHANNEL_RC_OK;
613 DWORD status = 0;
614
615 WINPR_ASSERT(device);
616
617 nCount = 0;
618 events[nCount++] = device->stopEvent;
619
620 while ((error == CHANNEL_RC_OK) && (WaitForSingleObject(events[0], 0) != WAIT_OBJECT_0))
621 {
622 switch (device->state)
623 {
624 case CAMERA_DEVICE_INITIAL:
625 error = device_server_context_poll_int(&device->context);
626 if (error == CHANNEL_RC_OK)
627 {
628 events[1] = device_server_get_channel_handle(device);
629 nCount = 2;
630 }
631 break;
632 case CAMERA_DEVICE_OPENED:
633 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
634 switch (status)
635 {
636 case WAIT_OBJECT_0:
637 break;
638 case WAIT_OBJECT_0 + 1:
639 case WAIT_TIMEOUT:
640 error = device_server_context_poll_int(&device->context);
641 break;
642
643 case WAIT_FAILED:
644 default:
645 error = ERROR_INTERNAL_ERROR;
646 break;
647 }
648 break;
649 default:
650 break;
651 }
652 }
653
654 (void)WTSVirtualChannelClose(device->device_channel);
655 device->device_channel = NULL;
656
657 if (error && device->context.rdpcontext)
658 setChannelError(device->context.rdpcontext, error,
659 "device_server_thread_func reported an error");
660
661 ExitThread(error);
662 return error;
663}
664
665static UINT device_server_open(CameraDeviceServerContext* context)
666{
667 device_server* device = (device_server*)context;
668
669 WINPR_ASSERT(device);
670
671 if (!device->externalThread && (device->thread == NULL))
672 {
673 device->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
674 if (!device->stopEvent)
675 {
676 WLog_ERR(TAG, "CreateEvent failed!");
677 return ERROR_INTERNAL_ERROR;
678 }
679
680 device->thread = CreateThread(NULL, 0, device_server_thread_func, device, 0, NULL);
681 if (!device->thread)
682 {
683 WLog_ERR(TAG, "CreateThread failed!");
684 (void)CloseHandle(device->stopEvent);
685 device->stopEvent = NULL;
686 return ERROR_INTERNAL_ERROR;
687 }
688 }
689 device->isOpened = TRUE;
690
691 return CHANNEL_RC_OK;
692}
693
694static UINT device_server_close(CameraDeviceServerContext* context)
695{
696 UINT error = CHANNEL_RC_OK;
697 device_server* device = (device_server*)context;
698
699 WINPR_ASSERT(device);
700
701 if (!device->externalThread && device->thread)
702 {
703 (void)SetEvent(device->stopEvent);
704
705 if (WaitForSingleObject(device->thread, INFINITE) == WAIT_FAILED)
706 {
707 error = GetLastError();
708 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
709 return error;
710 }
711
712 (void)CloseHandle(device->thread);
713 (void)CloseHandle(device->stopEvent);
714 device->thread = NULL;
715 device->stopEvent = NULL;
716 }
717 if (device->externalThread)
718 {
719 if (device->state != CAMERA_DEVICE_INITIAL)
720 {
721 (void)WTSVirtualChannelClose(device->device_channel);
722 device->device_channel = NULL;
723 device->state = CAMERA_DEVICE_INITIAL;
724 }
725 }
726 device->isOpened = FALSE;
727
728 return error;
729}
730
731static UINT device_server_context_poll(CameraDeviceServerContext* context)
732{
733 device_server* device = (device_server*)context;
734
735 WINPR_ASSERT(device);
736
737 if (!device->externalThread)
738 return ERROR_INTERNAL_ERROR;
739
740 return device_server_context_poll_int(context);
741}
742
743static BOOL device_server_context_handle(CameraDeviceServerContext* context, HANDLE* handle)
744{
745 device_server* device = (device_server*)context;
746
747 WINPR_ASSERT(device);
748 WINPR_ASSERT(handle);
749
750 if (!device->externalThread)
751 return FALSE;
752 if (device->state == CAMERA_DEVICE_INITIAL)
753 return FALSE;
754
755 *handle = device_server_get_channel_handle(device);
756
757 return TRUE;
758}
759
760static wStream* device_server_packet_new(size_t size, BYTE version, BYTE messageId)
761{
762 wStream* s = NULL;
763
764 /* Allocate what we need plus header bytes */
765 s = Stream_New(NULL, size + CAM_HEADER_SIZE);
766 if (!s)
767 {
768 WLog_ERR(TAG, "Stream_New failed!");
769 return NULL;
770 }
771
772 Stream_Write_UINT8(s, version);
773 Stream_Write_UINT8(s, messageId);
774
775 return s;
776}
777
778static UINT device_server_packet_send(CameraDeviceServerContext* context, wStream* s)
779{
780 device_server* device = (device_server*)context;
781 UINT error = CHANNEL_RC_OK;
782 ULONG written = 0;
783
784 WINPR_ASSERT(context);
785 WINPR_ASSERT(s);
786
787 const size_t len = Stream_GetPosition(s);
788 WINPR_ASSERT(len <= UINT32_MAX);
789 if (!WTSVirtualChannelWrite(device->device_channel, Stream_BufferAs(s, char), (UINT32)len,
790 &written))
791 {
792 WLog_ERR(TAG, "WTSVirtualChannelWrite failed!");
793 error = ERROR_INTERNAL_ERROR;
794 goto out;
795 }
796
797 if (written < Stream_GetPosition(s))
798 {
799 WLog_WARN(TAG, "Unexpected bytes written: %" PRIu32 "/%" PRIuz "", written,
800 Stream_GetPosition(s));
801 }
802
803out:
804 Stream_Free(s, TRUE);
805 return error;
806}
807
808static UINT device_server_write_and_send_header(CameraDeviceServerContext* context, BYTE messageId)
809{
810 wStream* s = NULL;
811
812 WINPR_ASSERT(context);
813
814 s = device_server_packet_new(0, context->protocolVersion, messageId);
815 if (!s)
816 return ERROR_NOT_ENOUGH_MEMORY;
817
818 return device_server_packet_send(context, s);
819}
820
821static UINT device_send_activate_device_request_pdu(
822 CameraDeviceServerContext* context,
823 WINPR_ATTR_UNUSED const CAM_ACTIVATE_DEVICE_REQUEST* activateDeviceRequest)
824{
825 WINPR_ASSERT(context);
826
827 return device_server_write_and_send_header(context, CAM_MSG_ID_ActivateDeviceRequest);
828}
829
830static UINT device_send_deactivate_device_request_pdu(
831 CameraDeviceServerContext* context,
832 WINPR_ATTR_UNUSED const CAM_DEACTIVATE_DEVICE_REQUEST* deactivateDeviceRequest)
833{
834 WINPR_ASSERT(context);
835
836 return device_server_write_and_send_header(context, CAM_MSG_ID_DeactivateDeviceRequest);
837}
838
839static UINT device_send_stream_list_request_pdu(
840 CameraDeviceServerContext* context,
841 WINPR_ATTR_UNUSED const CAM_STREAM_LIST_REQUEST* streamListRequest)
842{
843 WINPR_ASSERT(context);
844
845 return device_server_write_and_send_header(context, CAM_MSG_ID_StreamListRequest);
846}
847
848static UINT
849device_send_media_type_list_request_pdu(CameraDeviceServerContext* context,
850 const CAM_MEDIA_TYPE_LIST_REQUEST* mediaTypeListRequest)
851{
852 wStream* s = NULL;
853
854 WINPR_ASSERT(context);
855 WINPR_ASSERT(mediaTypeListRequest);
856
857 s = device_server_packet_new(1, context->protocolVersion, CAM_MSG_ID_MediaTypeListRequest);
858 if (!s)
859 return ERROR_NOT_ENOUGH_MEMORY;
860
861 Stream_Write_UINT8(s, mediaTypeListRequest->StreamIndex);
862
863 return device_server_packet_send(context, s);
864}
865
866static UINT device_send_current_media_type_request_pdu(
867 CameraDeviceServerContext* context,
868 const CAM_CURRENT_MEDIA_TYPE_REQUEST* currentMediaTypeRequest)
869{
870 wStream* s = NULL;
871
872 WINPR_ASSERT(context);
873 WINPR_ASSERT(currentMediaTypeRequest);
874
875 s = device_server_packet_new(1, context->protocolVersion, CAM_MSG_ID_CurrentMediaTypeRequest);
876 if (!s)
877 return ERROR_NOT_ENOUGH_MEMORY;
878
879 Stream_Write_UINT8(s, currentMediaTypeRequest->StreamIndex);
880
881 return device_server_packet_send(context, s);
882}
883
884static UINT
885device_send_start_streams_request_pdu(CameraDeviceServerContext* context,
886 const CAM_START_STREAMS_REQUEST* startStreamsRequest)
887{
888 wStream* s = NULL;
889
890 WINPR_ASSERT(context);
891 WINPR_ASSERT(startStreamsRequest);
892
893 s = device_server_packet_new(startStreamsRequest->N_Infos * 27ul, context->protocolVersion,
894 CAM_MSG_ID_StartStreamsRequest);
895 if (!s)
896 return ERROR_NOT_ENOUGH_MEMORY;
897
898 for (size_t i = 0; i < startStreamsRequest->N_Infos; ++i)
899 {
900 const CAM_START_STREAM_INFO* info = &startStreamsRequest->StartStreamsInfo[i];
901 const CAM_MEDIA_TYPE_DESCRIPTION* description = &info->MediaTypeDescription;
902
903 Stream_Write_UINT8(s, info->StreamIndex);
904
905 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, description->Format));
906 Stream_Write_UINT32(s, description->Width);
907 Stream_Write_UINT32(s, description->Height);
908 Stream_Write_UINT32(s, description->FrameRateNumerator);
909 Stream_Write_UINT32(s, description->FrameRateDenominator);
910 Stream_Write_UINT32(s, description->PixelAspectRatioNumerator);
911 Stream_Write_UINT32(s, description->PixelAspectRatioDenominator);
912 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, description->Flags));
913 }
914
915 return device_server_packet_send(context, s);
916}
917
918static UINT device_send_stop_streams_request_pdu(
919 CameraDeviceServerContext* context,
920 WINPR_ATTR_UNUSED const CAM_STOP_STREAMS_REQUEST* stopStreamsRequest)
921{
922 WINPR_ASSERT(context);
923
924 return device_server_write_and_send_header(context, CAM_MSG_ID_StopStreamsRequest);
925}
926
927static UINT device_send_sample_request_pdu(CameraDeviceServerContext* context,
928 const CAM_SAMPLE_REQUEST* sampleRequest)
929{
930 wStream* s = NULL;
931
932 WINPR_ASSERT(context);
933 WINPR_ASSERT(sampleRequest);
934
935 s = device_server_packet_new(1, context->protocolVersion, CAM_MSG_ID_SampleRequest);
936 if (!s)
937 return ERROR_NOT_ENOUGH_MEMORY;
938
939 Stream_Write_UINT8(s, sampleRequest->StreamIndex);
940
941 return device_server_packet_send(context, s);
942}
943
944static UINT device_send_property_list_request_pdu(
945 CameraDeviceServerContext* context,
946 WINPR_ATTR_UNUSED const CAM_PROPERTY_LIST_REQUEST* propertyListRequest)
947{
948 WINPR_ASSERT(context);
949
950 return device_server_write_and_send_header(context, CAM_MSG_ID_PropertyListRequest);
951}
952
953static UINT
954device_send_property_value_request_pdu(CameraDeviceServerContext* context,
955 const CAM_PROPERTY_VALUE_REQUEST* propertyValueRequest)
956{
957 wStream* s = NULL;
958
959 WINPR_ASSERT(context);
960 WINPR_ASSERT(propertyValueRequest);
961
962 s = device_server_packet_new(2, context->protocolVersion, CAM_MSG_ID_PropertyValueRequest);
963 if (!s)
964 return ERROR_NOT_ENOUGH_MEMORY;
965
966 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, propertyValueRequest->PropertySet));
967 Stream_Write_UINT8(s, propertyValueRequest->PropertyId);
968
969 return device_server_packet_send(context, s);
970}
971
972static UINT device_send_set_property_value_request_pdu(
973 CameraDeviceServerContext* context,
974 const CAM_SET_PROPERTY_VALUE_REQUEST* setPropertyValueRequest)
975{
976 wStream* s = NULL;
977
978 WINPR_ASSERT(context);
979 WINPR_ASSERT(setPropertyValueRequest);
980
981 s = device_server_packet_new(2 + 5, context->protocolVersion,
982 CAM_MSG_ID_SetPropertyValueRequest);
983 if (!s)
984 return ERROR_NOT_ENOUGH_MEMORY;
985
986 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, setPropertyValueRequest->PropertySet));
987 Stream_Write_UINT8(s, setPropertyValueRequest->PropertyId);
988
989 Stream_Write_UINT8(
990 s, WINPR_ASSERTING_INT_CAST(uint8_t, setPropertyValueRequest->PropertyValue.Mode));
991 Stream_Write_INT32(s, setPropertyValueRequest->PropertyValue.Value);
992
993 return device_server_packet_send(context, s);
994}
995
996CameraDeviceServerContext* camera_device_server_context_new(HANDLE vcm)
997{
998 device_server* device = (device_server*)calloc(1, sizeof(device_server));
999
1000 if (!device)
1001 return NULL;
1002
1003 device->context.vcm = vcm;
1004 device->context.Initialize = device_server_initialize;
1005 device->context.Open = device_server_open;
1006 device->context.Close = device_server_close;
1007 device->context.Poll = device_server_context_poll;
1008 device->context.ChannelHandle = device_server_context_handle;
1009
1010 device->context.ActivateDeviceRequest = device_send_activate_device_request_pdu;
1011 device->context.DeactivateDeviceRequest = device_send_deactivate_device_request_pdu;
1012
1013 device->context.StreamListRequest = device_send_stream_list_request_pdu;
1014 device->context.MediaTypeListRequest = device_send_media_type_list_request_pdu;
1015 device->context.CurrentMediaTypeRequest = device_send_current_media_type_request_pdu;
1016
1017 device->context.StartStreamsRequest = device_send_start_streams_request_pdu;
1018 device->context.StopStreamsRequest = device_send_stop_streams_request_pdu;
1019 device->context.SampleRequest = device_send_sample_request_pdu;
1020
1021 device->context.PropertyListRequest = device_send_property_list_request_pdu;
1022 device->context.PropertyValueRequest = device_send_property_value_request_pdu;
1023 device->context.SetPropertyValueRequest = device_send_set_property_value_request_pdu;
1024
1025 device->buffer = Stream_New(NULL, 4096);
1026 if (!device->buffer)
1027 goto fail;
1028
1029 return &device->context;
1030fail:
1031 WINPR_PRAGMA_DIAG_PUSH
1032 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1033 camera_device_server_context_free(&device->context);
1034 WINPR_PRAGMA_DIAG_POP
1035 return NULL;
1036}
1037
1038void camera_device_server_context_free(CameraDeviceServerContext* context)
1039{
1040 device_server* device = (device_server*)context;
1041
1042 if (device)
1043 {
1044 device_server_close(context);
1045 Stream_Free(device->buffer, TRUE);
1046 }
1047
1048 free(context->virtualChannelName);
1049
1050 free(device);
1051}