FreeRDP
Loading...
Searching...
No Matches
camera_device_enum_main.c
1
20#include <winpr/assert.h>
21#include <winpr/cast.h>
22
23#include "camera.h"
24
25#define TAG CHANNELS_TAG("rdpecam-enum.client")
26
32UINT ecam_channel_send_error_response(CameraPlugin* ecam, GENERIC_CHANNEL_CALLBACK* hchannel,
33 CAM_ERROR_CODE code)
34{
35 CAM_MSG_ID msg = CAM_MSG_ID_ErrorResponse;
36
37 WINPR_ASSERT(ecam);
38
39 wStream* s = Stream_New(NULL, CAM_HEADER_SIZE + 4);
40 if (!s)
41 {
42 WLog_ERR(TAG, "Stream_New failed!");
43 return ERROR_NOT_ENOUGH_MEMORY;
44 }
45
46 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, ecam->version));
47 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, msg));
48 Stream_Write_UINT32(s, code);
49
50 return ecam_channel_write(ecam, hchannel, msg, s, TRUE);
51}
52
58UINT ecam_channel_send_generic_msg(CameraPlugin* ecam, GENERIC_CHANNEL_CALLBACK* hchannel,
59 CAM_MSG_ID msg)
60{
61 WINPR_ASSERT(ecam);
62
63 wStream* s = Stream_New(NULL, CAM_HEADER_SIZE);
64 if (!s)
65 {
66 WLog_ERR(TAG, "Stream_New failed!");
67 return ERROR_NOT_ENOUGH_MEMORY;
68 }
69
70 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, ecam->version));
71 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, msg));
72
73 return ecam_channel_write(ecam, hchannel, msg, s, TRUE);
74}
75
81UINT ecam_channel_write(WINPR_ATTR_UNUSED CameraPlugin* ecam, GENERIC_CHANNEL_CALLBACK* hchannel,
82 CAM_MSG_ID msg, wStream* out, BOOL freeStream)
83{
84 if (!hchannel || !out)
85 return ERROR_INVALID_PARAMETER;
86
87 Stream_SealLength(out);
88 WINPR_ASSERT(Stream_Length(out) <= UINT32_MAX);
89
90 WLog_DBG(TAG, "ChannelId=%d, MessageId=0x%02" PRIx8 ", Length=%d",
91 hchannel->channel_mgr->GetChannelId(hchannel->channel), msg, Stream_Length(out));
92
93 const UINT error = hchannel->channel->Write(hchannel->channel, (ULONG)Stream_Length(out),
94 Stream_Buffer(out), NULL);
95
96 if (freeStream)
97 Stream_Free(out, TRUE);
98
99 return error;
100}
101
107static UINT ecam_send_device_added_notification(CameraPlugin* ecam,
108 GENERIC_CHANNEL_CALLBACK* hchannel,
109 const char* deviceName, const char* channelName)
110{
111 CAM_MSG_ID msg = CAM_MSG_ID_DeviceAddedNotification;
112
113 WINPR_ASSERT(ecam);
114
115 wStream* s = Stream_New(NULL, 256);
116 if (!s)
117 {
118 WLog_ERR(TAG, "Stream_New failed!");
119 return ERROR_NOT_ENOUGH_MEMORY;
120 }
121
122 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, ecam->version));
123 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, msg));
124
125 size_t devNameLen = strlen(deviceName);
126 if (Stream_Write_UTF16_String_From_UTF8(s, devNameLen + 1, deviceName, devNameLen, TRUE) < 0)
127 {
128 Stream_Free(s, TRUE);
129 return ERROR_INTERNAL_ERROR;
130 }
131 Stream_Write(s, channelName, strlen(channelName) + 1);
132
133 return ecam_channel_write(ecam, hchannel, msg, s, TRUE);
134}
135
141static UINT ecam_ihal_device_added_callback(CameraPlugin* ecam, GENERIC_CHANNEL_CALLBACK* hchannel,
142 const char* deviceId, const char* deviceName)
143{
144 WLog_DBG(TAG, "deviceId=%s, deviceName=%s", deviceId, deviceName);
145
146 if (!HashTable_ContainsKey(ecam->devices, deviceId))
147 {
148 CameraDevice* dev = ecam_dev_create(ecam, deviceId, deviceName);
149 if (!HashTable_Insert(ecam->devices, deviceId, dev))
150 {
151 ecam_dev_destroy(dev);
152 return ERROR_INTERNAL_ERROR;
153 }
154 }
155 else
156 {
157 WLog_DBG(TAG, "Device %s already exists", deviceId);
158 }
159
160 ecam_send_device_added_notification(ecam, hchannel, deviceName, deviceId /*channelName*/);
161
162 return CHANNEL_RC_OK;
163}
164
170static UINT ecam_enumerate_devices(CameraPlugin* ecam, GENERIC_CHANNEL_CALLBACK* hchannel)
171{
172 ecam->ihal->Enumerate(ecam->ihal, ecam_ihal_device_added_callback, ecam, hchannel);
173
174 return CHANNEL_RC_OK;
175}
176
182static UINT ecam_process_select_version_response(CameraPlugin* ecam,
183 GENERIC_CHANNEL_CALLBACK* hchannel,
184 WINPR_ATTR_UNUSED wStream* s, BYTE serverVersion)
185{
186 const BYTE clientVersion = ECAM_PROTO_VERSION;
187
188 /* check remaining s capacity */
189
190 WLog_DBG(TAG, "ServerVersion=%" PRIu8 ", ClientVersion=%" PRIu8, serverVersion, clientVersion);
191
192 if (serverVersion > clientVersion)
193 {
194 WLog_ERR(TAG,
195 "Incompatible protocol version server=%" PRIu8 ", client supports version=%" PRIu8,
196 serverVersion, clientVersion);
197 return CHANNEL_RC_OK;
198 }
199 ecam->version = serverVersion;
200
201 if (ecam->ihal)
202 ecam_enumerate_devices(ecam, hchannel);
203 else
204 WLog_ERR(TAG, "No HAL registered");
205
206 return CHANNEL_RC_OK;
207}
208
214static UINT ecam_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
215{
216 UINT error = CHANNEL_RC_OK;
217 BYTE version = 0;
218 BYTE messageId = 0;
219 GENERIC_CHANNEL_CALLBACK* hchannel = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
220
221 if (!hchannel || !data)
222 return ERROR_INVALID_PARAMETER;
223
224 CameraPlugin* ecam = (CameraPlugin*)hchannel->plugin;
225
226 if (!ecam)
227 return ERROR_INTERNAL_ERROR;
228
229 if (!Stream_CheckAndLogRequiredCapacity(TAG, data, CAM_HEADER_SIZE))
230 return ERROR_NO_DATA;
231
232 Stream_Read_UINT8(data, version);
233 Stream_Read_UINT8(data, messageId);
234 WLog_DBG(TAG, "ChannelId=%d, MessageId=0x%02" PRIx8 ", Version=%d",
235 hchannel->channel_mgr->GetChannelId(hchannel->channel), messageId, version);
236
237 switch (messageId)
238 {
239 case CAM_MSG_ID_SelectVersionResponse:
240 error = ecam_process_select_version_response(ecam, hchannel, data, version);
241 break;
242
243 default:
244 WLog_WARN(TAG, "unknown MessageId=0x%02" PRIx8 "", messageId);
245 error = ERROR_INVALID_DATA;
246 ecam_channel_send_error_response(ecam, hchannel, CAM_ERROR_CODE_OperationNotSupported);
247 break;
248 }
249
250 return error;
251}
252
258static UINT ecam_on_open(IWTSVirtualChannelCallback* pChannelCallback)
259{
260 GENERIC_CHANNEL_CALLBACK* hchannel = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
261 WINPR_ASSERT(hchannel);
262
263 CameraPlugin* ecam = (CameraPlugin*)hchannel->plugin;
264 WINPR_ASSERT(ecam);
265
266 WLog_DBG(TAG, "entered");
267 return ecam_channel_send_generic_msg(ecam, hchannel, CAM_MSG_ID_SelectVersionRequest);
268}
269
275static UINT ecam_on_close(IWTSVirtualChannelCallback* pChannelCallback)
276{
277 GENERIC_CHANNEL_CALLBACK* hchannel = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
278 WINPR_ASSERT(hchannel);
279
280 WLog_DBG(TAG, "entered");
281
282 free(hchannel);
283 return CHANNEL_RC_OK;
284}
285
291static UINT ecam_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
292 IWTSVirtualChannel* pChannel,
293 WINPR_ATTR_UNUSED BYTE* Data,
294 WINPR_ATTR_UNUSED BOOL* pbAccept,
295 IWTSVirtualChannelCallback** ppCallback)
296{
297 GENERIC_LISTENER_CALLBACK* hlistener = (GENERIC_LISTENER_CALLBACK*)pListenerCallback;
298
299 if (!hlistener || !hlistener->plugin)
300 return ERROR_INTERNAL_ERROR;
301
302 WLog_DBG(TAG, "entered");
303 GENERIC_CHANNEL_CALLBACK* hchannel =
305
306 if (!hchannel)
307 {
308 WLog_ERR(TAG, "calloc failed!");
309 return CHANNEL_RC_NO_MEMORY;
310 }
311
312 hchannel->iface.OnDataReceived = ecam_on_data_received;
313 hchannel->iface.OnOpen = ecam_on_open;
314 hchannel->iface.OnClose = ecam_on_close;
315 hchannel->plugin = hlistener->plugin;
316 hchannel->channel_mgr = hlistener->channel_mgr;
317 hchannel->channel = pChannel;
318 *ppCallback = (IWTSVirtualChannelCallback*)hchannel;
319 return CHANNEL_RC_OK;
320}
321
322static void ecam_dev_destroy_pv(void* obj)
323{
324 CameraDevice* dev = obj;
325 ecam_dev_destroy(dev);
326}
327
333static UINT ecam_plugin_initialize(IWTSPlugin* pPlugin, IWTSVirtualChannelManager* pChannelMgr)
334{
335 CameraPlugin* ecam = (CameraPlugin*)pPlugin;
336
337 WLog_DBG(TAG, "entered");
338
339 if (!ecam || !pChannelMgr)
340 return ERROR_INVALID_PARAMETER;
341
342 if (ecam->initialized)
343 {
344 WLog_ERR(TAG, "[%s] plugin initialized twice, aborting", RDPECAM_CONTROL_DVC_CHANNEL_NAME);
345 return ERROR_INVALID_DATA;
346 }
347
348 ecam->version = ECAM_PROTO_VERSION;
349
350 ecam->devices = HashTable_New(FALSE);
351 if (!ecam->devices)
352 {
353 WLog_ERR(TAG, "HashTable_New failed!");
354 return CHANNEL_RC_NO_MEMORY;
355 }
356
357 HashTable_SetupForStringData(ecam->devices, FALSE);
358
359 wObject* obj = HashTable_ValueObject(ecam->devices);
360 WINPR_ASSERT(obj);
361 obj->fnObjectFree = ecam_dev_destroy_pv;
362
363 ecam->hlistener = (GENERIC_LISTENER_CALLBACK*)calloc(1, sizeof(GENERIC_LISTENER_CALLBACK));
364
365 if (!ecam->hlistener)
366 {
367 WLog_ERR(TAG, "calloc failed!");
368 return CHANNEL_RC_NO_MEMORY;
369 }
370
371 ecam->hlistener->iface.OnNewChannelConnection = ecam_on_new_channel_connection;
372 ecam->hlistener->plugin = pPlugin;
373 ecam->hlistener->channel_mgr = pChannelMgr;
374 const UINT rc = pChannelMgr->CreateListener(pChannelMgr, RDPECAM_CONTROL_DVC_CHANNEL_NAME, 0,
375 &ecam->hlistener->iface, &ecam->listener);
376
377 ecam->initialized = (rc == CHANNEL_RC_OK);
378 return rc;
379}
380
386static UINT ecam_plugin_terminated(IWTSPlugin* pPlugin)
387{
388 CameraPlugin* ecam = (CameraPlugin*)pPlugin;
389
390 if (!ecam)
391 return ERROR_INVALID_DATA;
392
393 WLog_DBG(TAG, "entered");
394
395 if (ecam->hlistener)
396 {
397 IWTSVirtualChannelManager* mgr = ecam->hlistener->channel_mgr;
398 if (mgr)
399 IFCALL(mgr->DestroyListener, mgr, ecam->listener);
400 }
401
402 free(ecam->hlistener);
403
404 HashTable_Free(ecam->devices);
405
406 if (ecam->ihal)
407 ecam->ihal->Free(ecam->ihal);
408
409 free(ecam);
410 return CHANNEL_RC_OK;
411}
412
418static UINT ecam_plugin_attached(IWTSPlugin* pPlugin)
419{
420 CameraPlugin* ecam = (CameraPlugin*)pPlugin;
421 UINT error = CHANNEL_RC_OK;
422
423 if (!ecam)
424 return ERROR_INVALID_PARAMETER;
425
426 ecam->attached = TRUE;
427 return error;
428}
429
435static UINT ecam_plugin_detached(IWTSPlugin* pPlugin)
436{
437 CameraPlugin* ecam = (CameraPlugin*)pPlugin;
438 UINT error = CHANNEL_RC_OK;
439
440 if (!ecam)
441 return ERROR_INVALID_PARAMETER;
442
443 ecam->attached = FALSE;
444 return error;
445}
446
452static UINT ecam_register_hal_plugin(IWTSPlugin* pPlugin, ICamHal* ihal)
453{
454 CameraPlugin* ecam = (CameraPlugin*)pPlugin;
455
456 WINPR_ASSERT(ecam);
457
458 if (ecam->ihal)
459 {
460 WLog_DBG(TAG, "already registered");
461 return ERROR_ALREADY_EXISTS;
462 }
463
464 WLog_DBG(TAG, "HAL registered");
465 ecam->ihal = ihal;
466 return CHANNEL_RC_OK;
467}
468
474static UINT ecam_load_hal_plugin(CameraPlugin* ecam, const char* name, const ADDIN_ARGV* args)
475{
476 WINPR_ASSERT(ecam);
477
478 FREERDP_CAMERA_HAL_ENTRY_POINTS entryPoints = { 0 };
479 UINT error = ERROR_INTERNAL_ERROR;
480 union
481 {
482 PVIRTUALCHANNELENTRY pvce;
483 const PFREERDP_CAMERA_HAL_ENTRY entry;
484 } cnv;
485 cnv.pvce = freerdp_load_channel_addin_entry(RDPECAM_CHANNEL_NAME, name, NULL, 0);
486
487 if (cnv.entry == NULL)
488 {
489 WLog_ERR(TAG,
490 "freerdp_load_channel_addin_entry did not return any function pointers for %s ",
491 name);
492 return ERROR_INVALID_FUNCTION;
493 }
494
495 entryPoints.plugin = &ecam->iface;
496 entryPoints.pRegisterCameraHal = ecam_register_hal_plugin;
497 entryPoints.args = args;
498 entryPoints.ecam = ecam;
499
500 error = cnv.entry(&entryPoints);
501 if (error)
502 {
503 WLog_ERR(TAG, "%s entry returned error %" PRIu32 ".", name, error);
504 return error;
505 }
506
507 WLog_INFO(TAG, "Loaded %s HAL for ecam", name);
508 return CHANNEL_RC_OK;
509}
510
516FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpecam_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
517{
518 UINT error = CHANNEL_RC_INITIALIZATION_ERROR;
519
520 WINPR_ASSERT(pEntryPoints);
521 WINPR_ASSERT(pEntryPoints->GetPlugin);
522 CameraPlugin* ecam = (CameraPlugin*)pEntryPoints->GetPlugin(pEntryPoints, RDPECAM_CHANNEL_NAME);
523
524 if (ecam != NULL)
525 return CHANNEL_RC_ALREADY_INITIALIZED;
526
527 ecam = (CameraPlugin*)calloc(1, sizeof(CameraPlugin));
528
529 if (!ecam)
530 {
531 WLog_ERR(TAG, "calloc failed!");
532 return CHANNEL_RC_NO_MEMORY;
533 }
534
535 ecam->attached = TRUE;
536 ecam->iface.Initialize = ecam_plugin_initialize;
537 ecam->iface.Connected = NULL; /* server connects to client */
538 ecam->iface.Disconnected = NULL;
539 ecam->iface.Terminated = ecam_plugin_terminated;
540 ecam->iface.Attached = ecam_plugin_attached;
541 ecam->iface.Detached = ecam_plugin_detached;
542
543 /* TODO: camera redirect only supported for platforms with Video4Linux */
544#if defined(WITH_V4L)
545 ecam->subsystem = "v4l";
546#else
547 ecam->subsystem = NULL;
548#endif
549
550 if (ecam->subsystem)
551 {
552 if ((error = ecam_load_hal_plugin(ecam, ecam->subsystem, NULL /*args*/)))
553 {
554 WLog_ERR(TAG,
555 "Unable to load camera redirection subsystem %s because of error %" PRIu32 "",
556 ecam->subsystem, error);
557 goto out;
558 }
559 }
560
561 error = pEntryPoints->RegisterPlugin(pEntryPoints, RDPECAM_CHANNEL_NAME, &ecam->iface);
562 if (error == CHANNEL_RC_OK)
563 return error;
564
565out:
566 ecam_plugin_terminated(&ecam->iface);
567 return error;
568}
Definition camera.h:175
This struct contains function pointer to initialize/free objects.
Definition collections.h:57