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 = CHANNEL_RC_OK;
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 {
413 free(pdu.Properties);
414 return ERROR_INVALID_DATA;
415 }
416 cur->PropertySet = (CAM_PROPERTY_SET)val;
417 }
418 Stream_Read_UINT8(s, cur->PropertyId);
419 {
420 const UINT8 val = Stream_Get_UINT8(s);
421 if (!rdpecam_valid_CamPropertyCapabilities(val))
422 {
423 free(pdu.Properties);
424 return ERROR_INVALID_DATA;
425 }
426 cur->Capabilities = (CAM_PROPERTY_CAPABILITIES)val;
427 }
428 Stream_Read_INT32(s, cur->MinValue);
429 Stream_Read_INT32(s, cur->MaxValue);
430 Stream_Read_INT32(s, cur->Step);
431 Stream_Read_INT32(s, cur->DefaultValue);
432 }
433 }
434
435 IFCALLRET(context->PropertyListResponse, error, context, &pdu);
436 if (error)
437 WLog_ERR(TAG, "context->PropertyListResponse failed with error %" PRIu32 "", error);
438
439 free(pdu.Properties);
440
441 return error;
442}
443
444static UINT device_server_recv_property_value_response(CameraDeviceServerContext* context,
445 wStream* s,
446 const CAM_SHARED_MSG_HEADER* header)
447{
448 CAM_PROPERTY_VALUE_RESPONSE pdu = { 0 };
449 UINT error = CHANNEL_RC_OK;
450
451 WINPR_ASSERT(context);
452 WINPR_ASSERT(header);
453
454 pdu.Header = *header;
455
456 if (!Stream_CheckAndLogRequiredLength(TAG, s, 5))
457 return ERROR_NO_DATA;
458
459 {
460 const UINT8 val = Stream_Get_UINT8(s);
461 if (!rdpecam_valid_CamPropertyMode(val))
462 return ERROR_INVALID_DATA;
463 pdu.PropertyValue.Mode = (CAM_PROPERTY_MODE)val;
464 }
465
466 Stream_Read_INT32(s, pdu.PropertyValue.Value);
467
468 IFCALLRET(context->PropertyValueResponse, error, context, &pdu);
469 if (error)
470 WLog_ERR(TAG, "context->PropertyValueResponse failed with error %" PRIu32 "", error);
471
472 return error;
473}
474
475static UINT device_process_message(device_server* device)
476{
477 BOOL rc = 0;
478 UINT error = ERROR_INTERNAL_ERROR;
479 ULONG BytesReturned = 0;
480 CAM_SHARED_MSG_HEADER header = { 0 };
481 wStream* s = NULL;
482
483 WINPR_ASSERT(device);
484 WINPR_ASSERT(device->device_channel);
485
486 s = device->buffer;
487 WINPR_ASSERT(s);
488
489 Stream_SetPosition(s, 0);
490 rc = WTSVirtualChannelRead(device->device_channel, 0, NULL, 0, &BytesReturned);
491 if (!rc)
492 goto out;
493
494 if (BytesReturned < 1)
495 {
496 error = CHANNEL_RC_OK;
497 goto out;
498 }
499
500 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
501 {
502 WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
503 error = CHANNEL_RC_NO_MEMORY;
504 goto out;
505 }
506
507 if (WTSVirtualChannelRead(device->device_channel, 0, Stream_BufferAs(s, char),
508 (ULONG)Stream_Capacity(s), &BytesReturned) == FALSE)
509 {
510 WLog_ERR(TAG, "WTSVirtualChannelRead failed!");
511 goto out;
512 }
513
514 Stream_SetLength(s, BytesReturned);
515 if (!Stream_CheckAndLogRequiredLength(TAG, s, CAM_HEADER_SIZE))
516 return ERROR_NO_DATA;
517
518 Stream_Read_UINT8(s, header.Version);
519 {
520 const UINT8 id = Stream_Get_UINT8(s);
521 if (!rdpecam_valid_messageId(id))
522 return ERROR_INVALID_DATA;
523 header.MessageId = (CAM_MSG_ID)id;
524 }
525
526 switch (header.MessageId)
527 {
528 case CAM_MSG_ID_SuccessResponse:
529 error = device_server_handle_success_response(&device->context, s, &header);
530 break;
531 case CAM_MSG_ID_ErrorResponse:
532 error = device_server_recv_error_response(&device->context, s, &header);
533 break;
534 case CAM_MSG_ID_StreamListResponse:
535 error = device_server_recv_stream_list_response(&device->context, s, &header);
536 break;
537 case CAM_MSG_ID_MediaTypeListResponse:
538 error = device_server_recv_media_type_list_response(&device->context, s, &header);
539 break;
540 case CAM_MSG_ID_CurrentMediaTypeResponse:
541 error = device_server_recv_current_media_type_response(&device->context, s, &header);
542 break;
543 case CAM_MSG_ID_SampleResponse:
544 error = device_server_recv_sample_response(&device->context, s, &header);
545 break;
546 case CAM_MSG_ID_SampleErrorResponse:
547 error = device_server_recv_sample_error_response(&device->context, s, &header);
548 break;
549 case CAM_MSG_ID_PropertyListResponse:
550 error = device_server_recv_property_list_response(&device->context, s, &header);
551 break;
552 case CAM_MSG_ID_PropertyValueResponse:
553 error = device_server_recv_property_value_response(&device->context, s, &header);
554 break;
555 default:
556 WLog_ERR(TAG, "device_process_message: unknown or invalid MessageId %" PRIu8 "",
557 header.MessageId);
558 break;
559 }
560
561out:
562 if (error)
563 WLog_ERR(TAG, "Response failed with error %" PRIu32 "!", error);
564
565 return error;
566}
567
568static UINT device_server_context_poll_int(CameraDeviceServerContext* context)
569{
570 device_server* device = (device_server*)context;
571 UINT error = ERROR_INTERNAL_ERROR;
572
573 WINPR_ASSERT(device);
574
575 switch (device->state)
576 {
577 case CAMERA_DEVICE_INITIAL:
578 error = device_server_open_channel(device);
579 if (error)
580 WLog_ERR(TAG, "device_server_open_channel failed with error %" PRIu32 "!", error);
581 else
582 device->state = CAMERA_DEVICE_OPENED;
583 break;
584 case CAMERA_DEVICE_OPENED:
585 error = device_process_message(device);
586 break;
587 default:
588 break;
589 }
590
591 return error;
592}
593
594static HANDLE device_server_get_channel_handle(device_server* device)
595{
596 void* buffer = NULL;
597 DWORD BytesReturned = 0;
598 HANDLE ChannelEvent = NULL;
599
600 WINPR_ASSERT(device);
601
602 if (WTSVirtualChannelQuery(device->device_channel, WTSVirtualEventHandle, &buffer,
603 &BytesReturned) == TRUE)
604 {
605 if (BytesReturned == sizeof(HANDLE))
606 ChannelEvent = *(HANDLE*)buffer;
607
608 WTSFreeMemory(buffer);
609 }
610
611 return ChannelEvent;
612}
613
614static DWORD WINAPI device_server_thread_func(LPVOID arg)
615{
616 DWORD nCount = 0;
617 HANDLE events[2] = { 0 };
618 device_server* device = (device_server*)arg;
619 UINT error = CHANNEL_RC_OK;
620 DWORD status = 0;
621
622 WINPR_ASSERT(device);
623
624 nCount = 0;
625 events[nCount++] = device->stopEvent;
626
627 while ((error == CHANNEL_RC_OK) && (WaitForSingleObject(events[0], 0) != WAIT_OBJECT_0))
628 {
629 switch (device->state)
630 {
631 case CAMERA_DEVICE_INITIAL:
632 error = device_server_context_poll_int(&device->context);
633 if (error == CHANNEL_RC_OK)
634 {
635 events[1] = device_server_get_channel_handle(device);
636 nCount = 2;
637 }
638 break;
639 case CAMERA_DEVICE_OPENED:
640 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
641 switch (status)
642 {
643 case WAIT_OBJECT_0:
644 break;
645 case WAIT_OBJECT_0 + 1:
646 case WAIT_TIMEOUT:
647 error = device_server_context_poll_int(&device->context);
648 break;
649
650 case WAIT_FAILED:
651 default:
652 error = ERROR_INTERNAL_ERROR;
653 break;
654 }
655 break;
656 default:
657 break;
658 }
659 }
660
661 (void)WTSVirtualChannelClose(device->device_channel);
662 device->device_channel = NULL;
663
664 if (error && device->context.rdpcontext)
665 setChannelError(device->context.rdpcontext, error,
666 "device_server_thread_func reported an error");
667
668 ExitThread(error);
669 return error;
670}
671
672static UINT device_server_open(CameraDeviceServerContext* context)
673{
674 device_server* device = (device_server*)context;
675
676 WINPR_ASSERT(device);
677
678 if (!device->externalThread && (device->thread == NULL))
679 {
680 device->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
681 if (!device->stopEvent)
682 {
683 WLog_ERR(TAG, "CreateEvent failed!");
684 return ERROR_INTERNAL_ERROR;
685 }
686
687 device->thread = CreateThread(NULL, 0, device_server_thread_func, device, 0, NULL);
688 if (!device->thread)
689 {
690 WLog_ERR(TAG, "CreateThread failed!");
691 (void)CloseHandle(device->stopEvent);
692 device->stopEvent = NULL;
693 return ERROR_INTERNAL_ERROR;
694 }
695 }
696 device->isOpened = TRUE;
697
698 return CHANNEL_RC_OK;
699}
700
701static UINT device_server_close(CameraDeviceServerContext* context)
702{
703 UINT error = CHANNEL_RC_OK;
704 device_server* device = (device_server*)context;
705
706 WINPR_ASSERT(device);
707
708 if (!device->externalThread && device->thread)
709 {
710 (void)SetEvent(device->stopEvent);
711
712 if (WaitForSingleObject(device->thread, INFINITE) == WAIT_FAILED)
713 {
714 error = GetLastError();
715 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
716 return error;
717 }
718
719 (void)CloseHandle(device->thread);
720 (void)CloseHandle(device->stopEvent);
721 device->thread = NULL;
722 device->stopEvent = NULL;
723 }
724 if (device->externalThread)
725 {
726 if (device->state != CAMERA_DEVICE_INITIAL)
727 {
728 (void)WTSVirtualChannelClose(device->device_channel);
729 device->device_channel = NULL;
730 device->state = CAMERA_DEVICE_INITIAL;
731 }
732 }
733 device->isOpened = FALSE;
734
735 return error;
736}
737
738static UINT device_server_context_poll(CameraDeviceServerContext* context)
739{
740 device_server* device = (device_server*)context;
741
742 WINPR_ASSERT(device);
743
744 if (!device->externalThread)
745 return ERROR_INTERNAL_ERROR;
746
747 return device_server_context_poll_int(context);
748}
749
750static BOOL device_server_context_handle(CameraDeviceServerContext* context, HANDLE* handle)
751{
752 device_server* device = (device_server*)context;
753
754 WINPR_ASSERT(device);
755 WINPR_ASSERT(handle);
756
757 if (!device->externalThread)
758 return FALSE;
759 if (device->state == CAMERA_DEVICE_INITIAL)
760 return FALSE;
761
762 *handle = device_server_get_channel_handle(device);
763
764 return TRUE;
765}
766
767static wStream* device_server_packet_new(size_t size, BYTE version, BYTE messageId)
768{
769 wStream* s = NULL;
770
771 /* Allocate what we need plus header bytes */
772 s = Stream_New(NULL, size + CAM_HEADER_SIZE);
773 if (!s)
774 {
775 WLog_ERR(TAG, "Stream_New failed!");
776 return NULL;
777 }
778
779 Stream_Write_UINT8(s, version);
780 Stream_Write_UINT8(s, messageId);
781
782 return s;
783}
784
785static UINT device_server_packet_send(CameraDeviceServerContext* context, wStream* s)
786{
787 device_server* device = (device_server*)context;
788 UINT error = CHANNEL_RC_OK;
789 ULONG written = 0;
790
791 WINPR_ASSERT(context);
792 WINPR_ASSERT(s);
793
794 const size_t len = Stream_GetPosition(s);
795 WINPR_ASSERT(len <= UINT32_MAX);
796 if (!WTSVirtualChannelWrite(device->device_channel, Stream_BufferAs(s, char), (UINT32)len,
797 &written))
798 {
799 WLog_ERR(TAG, "WTSVirtualChannelWrite failed!");
800 error = ERROR_INTERNAL_ERROR;
801 goto out;
802 }
803
804 if (written < Stream_GetPosition(s))
805 {
806 WLog_WARN(TAG, "Unexpected bytes written: %" PRIu32 "/%" PRIuz "", written,
807 Stream_GetPosition(s));
808 }
809
810out:
811 Stream_Free(s, TRUE);
812 return error;
813}
814
815static UINT device_server_write_and_send_header(CameraDeviceServerContext* context, BYTE messageId)
816{
817 wStream* s = NULL;
818
819 WINPR_ASSERT(context);
820
821 s = device_server_packet_new(0, context->protocolVersion, messageId);
822 if (!s)
823 return ERROR_NOT_ENOUGH_MEMORY;
824
825 return device_server_packet_send(context, s);
826}
827
828static UINT device_send_activate_device_request_pdu(
829 CameraDeviceServerContext* context,
830 WINPR_ATTR_UNUSED const CAM_ACTIVATE_DEVICE_REQUEST* activateDeviceRequest)
831{
832 WINPR_ASSERT(context);
833
834 return device_server_write_and_send_header(context, CAM_MSG_ID_ActivateDeviceRequest);
835}
836
837static UINT device_send_deactivate_device_request_pdu(
838 CameraDeviceServerContext* context,
839 WINPR_ATTR_UNUSED const CAM_DEACTIVATE_DEVICE_REQUEST* deactivateDeviceRequest)
840{
841 WINPR_ASSERT(context);
842
843 return device_server_write_and_send_header(context, CAM_MSG_ID_DeactivateDeviceRequest);
844}
845
846static UINT device_send_stream_list_request_pdu(
847 CameraDeviceServerContext* context,
848 WINPR_ATTR_UNUSED const CAM_STREAM_LIST_REQUEST* streamListRequest)
849{
850 WINPR_ASSERT(context);
851
852 return device_server_write_and_send_header(context, CAM_MSG_ID_StreamListRequest);
853}
854
855static UINT
856device_send_media_type_list_request_pdu(CameraDeviceServerContext* context,
857 const CAM_MEDIA_TYPE_LIST_REQUEST* mediaTypeListRequest)
858{
859 wStream* s = NULL;
860
861 WINPR_ASSERT(context);
862 WINPR_ASSERT(mediaTypeListRequest);
863
864 s = device_server_packet_new(1, context->protocolVersion, CAM_MSG_ID_MediaTypeListRequest);
865 if (!s)
866 return ERROR_NOT_ENOUGH_MEMORY;
867
868 Stream_Write_UINT8(s, mediaTypeListRequest->StreamIndex);
869
870 return device_server_packet_send(context, s);
871}
872
873static UINT device_send_current_media_type_request_pdu(
874 CameraDeviceServerContext* context,
875 const CAM_CURRENT_MEDIA_TYPE_REQUEST* currentMediaTypeRequest)
876{
877 wStream* s = NULL;
878
879 WINPR_ASSERT(context);
880 WINPR_ASSERT(currentMediaTypeRequest);
881
882 s = device_server_packet_new(1, context->protocolVersion, CAM_MSG_ID_CurrentMediaTypeRequest);
883 if (!s)
884 return ERROR_NOT_ENOUGH_MEMORY;
885
886 Stream_Write_UINT8(s, currentMediaTypeRequest->StreamIndex);
887
888 return device_server_packet_send(context, s);
889}
890
891static UINT
892device_send_start_streams_request_pdu(CameraDeviceServerContext* context,
893 const CAM_START_STREAMS_REQUEST* startStreamsRequest)
894{
895 wStream* s = NULL;
896
897 WINPR_ASSERT(context);
898 WINPR_ASSERT(startStreamsRequest);
899
900 s = device_server_packet_new(startStreamsRequest->N_Infos * 27ul, context->protocolVersion,
901 CAM_MSG_ID_StartStreamsRequest);
902 if (!s)
903 return ERROR_NOT_ENOUGH_MEMORY;
904
905 for (size_t i = 0; i < startStreamsRequest->N_Infos; ++i)
906 {
907 const CAM_START_STREAM_INFO* info = &startStreamsRequest->StartStreamsInfo[i];
908 const CAM_MEDIA_TYPE_DESCRIPTION* description = &info->MediaTypeDescription;
909
910 Stream_Write_UINT8(s, info->StreamIndex);
911
912 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, description->Format));
913 Stream_Write_UINT32(s, description->Width);
914 Stream_Write_UINT32(s, description->Height);
915 Stream_Write_UINT32(s, description->FrameRateNumerator);
916 Stream_Write_UINT32(s, description->FrameRateDenominator);
917 Stream_Write_UINT32(s, description->PixelAspectRatioNumerator);
918 Stream_Write_UINT32(s, description->PixelAspectRatioDenominator);
919 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, description->Flags));
920 }
921
922 return device_server_packet_send(context, s);
923}
924
925static UINT device_send_stop_streams_request_pdu(
926 CameraDeviceServerContext* context,
927 WINPR_ATTR_UNUSED const CAM_STOP_STREAMS_REQUEST* stopStreamsRequest)
928{
929 WINPR_ASSERT(context);
930
931 return device_server_write_and_send_header(context, CAM_MSG_ID_StopStreamsRequest);
932}
933
934static UINT device_send_sample_request_pdu(CameraDeviceServerContext* context,
935 const CAM_SAMPLE_REQUEST* sampleRequest)
936{
937 wStream* s = NULL;
938
939 WINPR_ASSERT(context);
940 WINPR_ASSERT(sampleRequest);
941
942 s = device_server_packet_new(1, context->protocolVersion, CAM_MSG_ID_SampleRequest);
943 if (!s)
944 return ERROR_NOT_ENOUGH_MEMORY;
945
946 Stream_Write_UINT8(s, sampleRequest->StreamIndex);
947
948 return device_server_packet_send(context, s);
949}
950
951static UINT device_send_property_list_request_pdu(
952 CameraDeviceServerContext* context,
953 WINPR_ATTR_UNUSED const CAM_PROPERTY_LIST_REQUEST* propertyListRequest)
954{
955 WINPR_ASSERT(context);
956
957 return device_server_write_and_send_header(context, CAM_MSG_ID_PropertyListRequest);
958}
959
960static UINT
961device_send_property_value_request_pdu(CameraDeviceServerContext* context,
962 const CAM_PROPERTY_VALUE_REQUEST* propertyValueRequest)
963{
964 wStream* s = NULL;
965
966 WINPR_ASSERT(context);
967 WINPR_ASSERT(propertyValueRequest);
968
969 s = device_server_packet_new(2, context->protocolVersion, CAM_MSG_ID_PropertyValueRequest);
970 if (!s)
971 return ERROR_NOT_ENOUGH_MEMORY;
972
973 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, propertyValueRequest->PropertySet));
974 Stream_Write_UINT8(s, propertyValueRequest->PropertyId);
975
976 return device_server_packet_send(context, s);
977}
978
979static UINT device_send_set_property_value_request_pdu(
980 CameraDeviceServerContext* context,
981 const CAM_SET_PROPERTY_VALUE_REQUEST* setPropertyValueRequest)
982{
983 wStream* s = NULL;
984
985 WINPR_ASSERT(context);
986 WINPR_ASSERT(setPropertyValueRequest);
987
988 s = device_server_packet_new(2 + 5, context->protocolVersion,
989 CAM_MSG_ID_SetPropertyValueRequest);
990 if (!s)
991 return ERROR_NOT_ENOUGH_MEMORY;
992
993 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, setPropertyValueRequest->PropertySet));
994 Stream_Write_UINT8(s, setPropertyValueRequest->PropertyId);
995
996 Stream_Write_UINT8(
997 s, WINPR_ASSERTING_INT_CAST(uint8_t, setPropertyValueRequest->PropertyValue.Mode));
998 Stream_Write_INT32(s, setPropertyValueRequest->PropertyValue.Value);
999
1000 return device_server_packet_send(context, s);
1001}
1002
1003CameraDeviceServerContext* camera_device_server_context_new(HANDLE vcm)
1004{
1005 device_server* device = (device_server*)calloc(1, sizeof(device_server));
1006
1007 if (!device)
1008 return NULL;
1009
1010 device->context.vcm = vcm;
1011 device->context.Initialize = device_server_initialize;
1012 device->context.Open = device_server_open;
1013 device->context.Close = device_server_close;
1014 device->context.Poll = device_server_context_poll;
1015 device->context.ChannelHandle = device_server_context_handle;
1016
1017 device->context.ActivateDeviceRequest = device_send_activate_device_request_pdu;
1018 device->context.DeactivateDeviceRequest = device_send_deactivate_device_request_pdu;
1019
1020 device->context.StreamListRequest = device_send_stream_list_request_pdu;
1021 device->context.MediaTypeListRequest = device_send_media_type_list_request_pdu;
1022 device->context.CurrentMediaTypeRequest = device_send_current_media_type_request_pdu;
1023
1024 device->context.StartStreamsRequest = device_send_start_streams_request_pdu;
1025 device->context.StopStreamsRequest = device_send_stop_streams_request_pdu;
1026 device->context.SampleRequest = device_send_sample_request_pdu;
1027
1028 device->context.PropertyListRequest = device_send_property_list_request_pdu;
1029 device->context.PropertyValueRequest = device_send_property_value_request_pdu;
1030 device->context.SetPropertyValueRequest = device_send_set_property_value_request_pdu;
1031
1032 device->buffer = Stream_New(NULL, 4096);
1033 if (!device->buffer)
1034 goto fail;
1035
1036 return &device->context;
1037fail:
1038 WINPR_PRAGMA_DIAG_PUSH
1039 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1040 camera_device_server_context_free(&device->context);
1041 WINPR_PRAGMA_DIAG_POP
1042 return NULL;
1043}
1044
1045void camera_device_server_context_free(CameraDeviceServerContext* context)
1046{
1047 device_server* device = (device_server*)context;
1048
1049 if (device)
1050 {
1051 device_server_close(context);
1052 Stream_Free(device->buffer, TRUE);
1053 }
1054
1055 free(context->virtualChannelName);
1056
1057 free(device);
1058}