FreeRDP
Loading...
Searching...
No Matches
camera_gst.c
1
20/* A pseudo camera whose frames come from a GStreamer pipeline, selected with
21 * /dvc:rdpecam,sys:gst,pipeline:<pipeline>|pipeline-file:<path>|file:<path>
22 * [,name:<camera name>][,loop:<on|off>]
23 *
24 * The pipeline is given in gst-launch syntax and must produce video, the backend appends the
25 * conversion to I420 and the sink. The only media type announced is what the pipeline
26 * negotiates (cropped to even dimensions). A pipeline reaching its end is played again unless
27 * loop:off is given, live sources (pipewiresrc, v4l2src, ...) never end. */
28
29#include <winpr/assert.h>
30#include <winpr/cast.h>
31#include <winpr/cmdline.h>
32#include <winpr/file.h>
33#include <winpr/string.h>
34#include <winpr/synch.h>
35#include <winpr/thread.h>
36
37#include <gst/gst.h>
38#include <gst/app/gstappsink.h>
39#include <gst/video/video.h>
40
41#include "camera.h"
42
43#define TAG CHANNELS_TAG("rdpecam-gst.client")
44
45/* the channel name of the device, what a Windows client would use for its first camera */
46#define CAM_GST_DEVICE_ID "RDCamera_Device_0"
47
48#define CAM_GST_SINK_NAME "freerdpcamsink"
49
50#define CAM_GST_FRAMERATE_NUMERATOR_DEFAULT 30
51#define CAM_GST_FRAMERATE_DENOMINATOR_DEFAULT 1
52
53/* how long the pipeline may take to start, to preroll or to produce its first frame */
54#define CAM_GST_START_TIMEOUT (10 * GST_SECOND)
55
56/* longest wait for a frame, so that a stop request is seen quickly */
57#define CAM_GST_PULL_TIMEOUT (100 * GST_MSECOND)
58
59typedef struct
60{
61 ICamHal iHal;
62
63 char* description; /* the complete pipeline, sink included */
64 char* name;
65 BOOL loop;
66 BOOL live;
67
68 /* characteristics negotiated by the pipeline, the only media type announced */
69 UINT32 width;
70 UINT32 height;
71 UINT32 frameRateNumerator;
72 UINT32 frameRateDenominator;
73
74 GstElement* pipeline;
75 GstAppSink* sink;
76
78 BOOL streaming;
79 HANDLE thread;
80
81 CameraDevice* dev;
82 size_t streamIndex;
83 ICamHalSampleCapturedCallback sampleCallback;
84
85 BYTE* sample;
86 size_t sampleSize;
87} CamGstHal;
88
90static BOOL cam_gst_log_errors(CamGstHal* hal)
91{
92 WINPR_ASSERT(hal);
93
94 BOOL found = FALSE;
95 GstBus* bus = gst_element_get_bus(hal->pipeline);
96 if (!bus)
97 return FALSE;
98
99 GstMessage* msg = nullptr;
100 while ((msg = gst_bus_pop_filtered(bus, GST_MESSAGE_ERROR)) != nullptr)
101 {
102 GError* error = nullptr;
103 gchar* debug = nullptr;
104 gst_message_parse_error(msg, &error, &debug);
105 WLog_ERR(TAG, "Pipeline error from %s: %s (%s)", GST_OBJECT_NAME(GST_MESSAGE_SRC(msg)),
106 error ? error->message : "unknown", debug ? debug : "no details");
107 g_clear_error(&error);
108 g_free(debug);
109 gst_message_unref(msg);
110 found = TRUE;
111 }
112 gst_object_unref(bus);
113 return found;
114}
115
116static void cam_gst_pipeline_destroy(CamGstHal* hal)
117{
118 WINPR_ASSERT(hal);
119
120 if (hal->sink)
121 {
122 gst_object_unref(hal->sink);
123 hal->sink = nullptr;
124 }
125 if (hal->pipeline)
126 {
127 (void)gst_element_set_state(hal->pipeline, GST_STATE_NULL);
128 gst_object_unref(hal->pipeline);
129 hal->pipeline = nullptr;
130 }
131}
132
135WINPR_ATTR_NODISCARD
136static BOOL cam_gst_pipeline_create(CamGstHal* hal)
137{
138 WINPR_ASSERT(hal);
139 WINPR_ASSERT(!hal->pipeline);
140
141 GError* error = nullptr;
142 hal->pipeline =
143 gst_parse_launch_full(hal->description, nullptr, GST_PARSE_FLAG_FATAL_ERRORS, &error);
144 if (!hal->pipeline || error)
145 {
146 WLog_ERR(TAG, "Invalid pipeline \"%s\": %s", hal->description,
147 error ? error->message : "unknown error");
148 g_clear_error(&error);
149 cam_gst_pipeline_destroy(hal);
150 return FALSE;
151 }
152
153 GstElement* sink = GST_IS_BIN(hal->pipeline)
154 ? gst_bin_get_by_name(GST_BIN(hal->pipeline), CAM_GST_SINK_NAME)
155 : nullptr;
156 if (!sink)
157 {
158 WLog_ERR(TAG, "The pipeline has no %s element", CAM_GST_SINK_NAME);
159 cam_gst_pipeline_destroy(hal);
160 return FALSE;
161 }
162 hal->sink = GST_APP_SINK(sink);
163 return TRUE;
164}
165
167WINPR_ATTR_NODISCARD
168static BOOL cam_gst_set_state(CamGstHal* hal, GstState state, GstStateChangeReturn* pResult)
169{
170 WINPR_ASSERT(hal);
171
172 GstStateChangeReturn rc = gst_element_set_state(hal->pipeline, state);
173 if (rc == GST_STATE_CHANGE_ASYNC)
174 rc = gst_element_get_state(hal->pipeline, nullptr, nullptr, CAM_GST_START_TIMEOUT);
175
176 if (pResult)
177 *pResult = rc;
178
179 switch (rc)
180 {
181 case GST_STATE_CHANGE_SUCCESS:
182 case GST_STATE_CHANGE_NO_PREROLL:
183 return TRUE;
184 case GST_STATE_CHANGE_ASYNC:
185 WLog_ERR(TAG, "Pipeline did not reach %s in time", gst_element_state_get_name(state));
186 return FALSE;
187 case GST_STATE_CHANGE_FAILURE:
188 default:
189 if (!cam_gst_log_errors(hal))
190 WLog_ERR(TAG, "Pipeline failed to go to %s", gst_element_state_get_name(state));
191 return FALSE;
192 }
193}
194
196WINPR_ATTR_NODISCARD
197static BOOL cam_gst_convert_sample(CamGstHal* hal, GstSample* sample)
198{
199 WINPR_ASSERT(hal);
200 WINPR_ASSERT(sample);
201
202 GstVideoInfo info;
203 GstCaps* caps = gst_sample_get_caps(sample);
204 GstBuffer* buffer = gst_sample_get_buffer(sample);
205 if (!caps || !buffer || !gst_video_info_from_caps(&info, caps))
206 {
207 WLog_WARN(TAG, "Skipping a sample without video caps");
208 return FALSE;
209 }
210
211 if ((GST_VIDEO_INFO_FORMAT(&info) != GST_VIDEO_FORMAT_I420) ||
212 (GST_VIDEO_INFO_WIDTH(&info) < 0) || (GST_VIDEO_INFO_HEIGHT(&info) < 0) ||
213 ((UINT32)GST_VIDEO_INFO_WIDTH(&info) < hal->width) ||
214 ((UINT32)GST_VIDEO_INFO_HEIGHT(&info) < hal->height))
215 {
216 WLog_WARN(
217 TAG, "Skipping a %s %dx%d frame, the announced format is I420 %" PRIu32 "x%" PRIu32,
218 gst_video_format_to_string(GST_VIDEO_INFO_FORMAT(&info)), GST_VIDEO_INFO_WIDTH(&info),
219 GST_VIDEO_INFO_HEIGHT(&info), hal->width, hal->height);
220 return FALSE;
221 }
222
223 GstVideoFrame frame;
224 if (!gst_video_frame_map(&frame, &info, buffer, GST_MAP_READ))
225 {
226 WLog_WARN(TAG, "Unable to map a frame");
227 return FALSE;
228 }
229
230 /* planes may be padded, and larger than announced when cropping an odd size */
231 BYTE* dst = hal->sample;
232 for (guint plane = 0; plane < 3; plane++)
233 {
234 const size_t rowSize = (plane == 0) ? hal->width : hal->width / 2;
235 const size_t rows = (plane == 0) ? hal->height : hal->height / 2;
236 const BYTE* src = GST_VIDEO_FRAME_PLANE_DATA(&frame, plane);
237 const size_t stride = (size_t)GST_VIDEO_FRAME_PLANE_STRIDE(&frame, plane);
238
239 for (size_t y = 0; y < rows; y++)
240 {
241 memcpy(dst, &src[y * stride], rowSize);
242 dst += rowSize;
243 }
244 }
245
246 gst_video_frame_unmap(&frame);
247 return TRUE;
248}
249
252static void cam_gst_release(CamGstHal* hal)
253{
254 WINPR_ASSERT(hal);
255
256 if (hal->live || !cam_gst_set_state(hal, GST_STATE_PAUSED, nullptr))
257 cam_gst_pipeline_destroy(hal);
258}
259
260static DWORD WINAPI cam_gst_capture_thread(LPVOID param)
261{
262 CamGstHal* hal = (CamGstHal*)param;
263 WINPR_ASSERT(hal);
264
265 BOOL endLogged = FALSE;
266 for (;;)
267 {
268 EnterCriticalSection(&hal->lock);
269 const BOOL streaming = hal->streaming;
270 LeaveCriticalSection(&hal->lock);
271 if (!streaming)
272 break;
273
274 GstSample* sample = gst_app_sink_try_pull_sample(hal->sink, CAM_GST_PULL_TIMEOUT);
275 if (!sample)
276 {
277 if (cam_gst_log_errors(hal))
278 break;
279
280 if (!gst_app_sink_is_eos(hal->sink))
281 continue;
282
283 if (hal->loop && !hal->live)
284 {
285 WLog_DBG(TAG, "End of stream, playing it again");
286 if (!gst_element_seek_simple(
287 hal->pipeline, GST_FORMAT_TIME,
288 (GstSeekFlags)(GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_KEY_UNIT), 0))
289 {
290 WLog_ERR(TAG, "Unable to rewind the pipeline");
291 break;
292 }
293 }
294 else if (!endLogged)
295 {
296 /* the camera keeps its last frame until the stream is stopped */
297 WLog_INFO(TAG, "End of stream");
298 endLogged = TRUE;
299 }
300 continue;
301 }
302
303 const BOOL converted = cam_gst_convert_sample(hal, sample);
304 gst_sample_unref(sample);
305 if (!converted)
306 continue;
307
308 const UINT error =
309 hal->sampleCallback(hal->dev, hal->streamIndex, hal->sample, hal->sampleSize);
310 if (error == ECAM_SAMPLE_CAPTURE_DRAINED)
311 {
312 WLog_DBG(TAG, "No more sample requests, releasing the pipeline");
313 cam_gst_release(hal);
314 break;
315 }
316 else if (error != CHANNEL_RC_OK)
317 WLog_ERR(TAG, "Failure in sampleCallback: %" PRIu32, error);
318 }
319
320 EnterCriticalSection(&hal->lock);
321 hal->streaming = FALSE;
322 LeaveCriticalSection(&hal->lock);
323 return CHANNEL_RC_OK;
324}
325
326static void cam_gst_join(CamGstHal* hal)
327{
328 WINPR_ASSERT(hal);
329
330 EnterCriticalSection(&hal->lock);
331 hal->streaming = FALSE;
332 LeaveCriticalSection(&hal->lock);
333
334 if (hal->thread)
335 {
336 (void)WaitForSingleObject(hal->thread, INFINITE);
337 (void)CloseHandle(hal->thread);
338 hal->thread = nullptr;
339 }
340}
341
342static BOOL cam_gst_activate(WINPR_ATTR_UNUSED ICamHal* ihal,
343 WINPR_ATTR_UNUSED const char* deviceId, CAM_ERROR_CODE* errorCode)
344{
345 *errorCode = CAM_ERROR_CODE_None;
346 return TRUE;
347}
348
349static BOOL cam_gst_deactivate(WINPR_ATTR_UNUSED ICamHal* ihal,
350 WINPR_ATTR_UNUSED const char* deviceId, CAM_ERROR_CODE* errorCode)
351{
352 *errorCode = CAM_ERROR_CODE_None;
353 return TRUE;
354}
355
356static UINT cam_gst_enumerate(ICamHal* ihal, ICamHalEnumCallback callback, CameraPlugin* ecam,
357 GENERIC_CHANNEL_CALLBACK* hchannel)
358{
359 CamGstHal* hal = (CamGstHal*)ihal;
360 WINPR_ASSERT(hal);
361
362 IFCALL(callback, ecam, hchannel, CAM_GST_DEVICE_ID, hal->name);
363 return 1;
364}
365
372static INT16 cam_gst_get_media_type_descriptions(
373 ICamHal* ihal, WINPR_ATTR_UNUSED const char* deviceId, WINPR_ATTR_UNUSED size_t streamIndex,
374 const CAM_MEDIA_FORMAT_INFO* supportedFormats, size_t nSupportedFormats,
375 CAM_MEDIA_TYPE_DESCRIPTION* mediaTypes, size_t* nMediaTypes)
376{
377 CamGstHal* hal = (CamGstHal*)ihal;
378 WINPR_ASSERT(hal);
379 WINPR_ASSERT(nMediaTypes);
380
381 const size_t maxMediaTypes = *nMediaTypes;
382 *nMediaTypes = 0;
383 if (maxMediaTypes == 0)
384 return -1;
385
386 /* the pipeline always ends with I420, take the preferred encoding of it */
387 for (size_t formatIndex = 0; formatIndex < nSupportedFormats; formatIndex++)
388 {
389 if (supportedFormats[formatIndex].inputFormat != CAM_MEDIA_FORMAT_I420)
390 continue;
391 if (formatIndex > INT16_MAX)
392 break;
393
394 *mediaTypes = (CAM_MEDIA_TYPE_DESCRIPTION){
395 .Format = CAM_MEDIA_FORMAT_I420,
396 .Width = hal->width,
397 .Height = hal->height,
398 .FrameRateNumerator = hal->frameRateNumerator,
399 .FrameRateDenominator = hal->frameRateDenominator,
400 .PixelAspectRatioNumerator = 1,
401 .PixelAspectRatioDenominator = 1,
402 };
403 *nMediaTypes = 1;
404 return (INT16)formatIndex;
405 }
406
407 WLog_ERR(TAG, "I420 is not a supported input format");
408 return -1;
409}
410
411static CAM_ERROR_CODE cam_gst_stream_start(ICamHal* ihal, CameraDevice* dev, size_t streamIndex,
412 const CAM_MEDIA_TYPE_DESCRIPTION* mediaType,
413 ICamHalSampleCapturedCallback callback)
414{
415 CamGstHal* hal = (CamGstHal*)ihal;
416 WINPR_ASSERT(hal);
417 WINPR_ASSERT(dev);
418 WINPR_ASSERT(mediaType);
419
420 if ((mediaType->Format != CAM_MEDIA_FORMAT_I420) || (mediaType->Width != hal->width) ||
421 (mediaType->Height != hal->height))
422 {
423 WLog_ERR(TAG, "Unexpected media type %u %" PRIu32 "x%" PRIu32, mediaType->Format,
424 mediaType->Width, mediaType->Height);
425 return CAM_ERROR_CODE_InvalidMediaType;
426 }
427
428 EnterCriticalSection(&hal->lock);
429 const BOOL streaming = hal->streaming;
430 LeaveCriticalSection(&hal->lock);
431
432 if (streaming)
433 {
434 WLog_ERR(TAG, "Streaming already in progress, device %s, streamIndex %" PRIuz,
435 dev->deviceId, streamIndex);
436 return CAM_ERROR_CODE_UnexpectedError;
437 }
438
439 /* the thread of a capture released for lack of sample requests */
440 cam_gst_join(hal);
441
442 hal->dev = dev;
443 hal->streamIndex = streamIndex;
444 hal->sampleCallback = callback;
445
446 if (!hal->pipeline && !cam_gst_pipeline_create(hal))
447 return CAM_ERROR_CODE_UnexpectedError;
448
449 if (!cam_gst_set_state(hal, GST_STATE_PLAYING, nullptr))
450 {
451 cam_gst_pipeline_destroy(hal);
452 return CAM_ERROR_CODE_UnexpectedError;
453 }
454
455 EnterCriticalSection(&hal->lock);
456 hal->streaming = TRUE;
457 LeaveCriticalSection(&hal->lock);
458
459 hal->thread = CreateThread(nullptr, 0, cam_gst_capture_thread, hal, 0, nullptr);
460 if (!hal->thread)
461 {
462 WLog_ERR(TAG, "CreateThread failure");
463 EnterCriticalSection(&hal->lock);
464 hal->streaming = FALSE;
465 LeaveCriticalSection(&hal->lock);
466 cam_gst_pipeline_destroy(hal);
467 return CAM_ERROR_CODE_OutOfMemory;
468 }
469
470 WLog_INFO(TAG, "Pipeline playing as camera: %" PRIu32 "x%" PRIu32 ", fps: %" PRIu32 "/%" PRIu32,
471 hal->width, hal->height, hal->frameRateNumerator, hal->frameRateDenominator);
472 return CAM_ERROR_CODE_None;
473}
474
475static CAM_ERROR_CODE cam_gst_stream_stop(ICamHal* ihal, WINPR_ATTR_UNUSED const char* deviceId,
476 WINPR_ATTR_UNUSED size_t streamIndex)
477{
478 CamGstHal* hal = (CamGstHal*)ihal;
479 WINPR_ASSERT(hal);
480
481 cam_gst_join(hal);
482
483 /* closes the source, the next stream starts from the beginning */
484 cam_gst_pipeline_destroy(hal);
485 return CAM_ERROR_CODE_None;
486}
487
488static CAM_ERROR_CODE cam_gst_free(ICamHal* ihal)
489{
490 CamGstHal* hal = (CamGstHal*)ihal;
491
492 if (!hal)
493 return CAM_ERROR_CODE_NotInitialized;
494
495 cam_gst_join(hal);
496 cam_gst_pipeline_destroy(hal);
497 DeleteCriticalSection(&hal->lock);
498
499 free(hal->sample);
500 free(hal->description);
501 free(hal->name);
502 free(hal);
503 return CAM_ERROR_CODE_None;
504}
505
508WINPR_ATTR_NODISCARD
509static BOOL cam_gst_probe(CamGstHal* hal)
510{
511 WINPR_ASSERT(hal);
512
513 WLog_DBG(TAG, "Pipeline: %s", hal->description);
514
515 if (!cam_gst_pipeline_create(hal))
516 return FALSE;
517
518 /* a live source does not preroll, it has to play to show its format */
519 BOOL rc = FALSE;
520 GstSample* sample = nullptr;
521 GstStateChangeReturn state = GST_STATE_CHANGE_FAILURE;
522 if (!cam_gst_set_state(hal, GST_STATE_PAUSED, &state))
523 goto out;
524
525 hal->live = (state == GST_STATE_CHANGE_NO_PREROLL);
526 if (hal->live)
527 {
528 if (!cam_gst_set_state(hal, GST_STATE_PLAYING, nullptr))
529 goto out;
530 sample = gst_app_sink_try_pull_sample(hal->sink, CAM_GST_START_TIMEOUT);
531 }
532 else
533 sample = gst_app_sink_try_pull_preroll(hal->sink, CAM_GST_START_TIMEOUT);
534
535 if (!sample)
536 {
537 if (!cam_gst_log_errors(hal))
538 WLog_ERR(TAG, "The pipeline did not produce any frame");
539 goto out;
540 }
541
542 GstVideoInfo info;
543 GstCaps* caps = gst_sample_get_caps(sample);
544 if (!caps || !gst_video_info_from_caps(&info, caps))
545 {
546 WLog_ERR(TAG, "The pipeline does not produce video");
547 goto out;
548 }
549
550 /* I420 needs even dimensions: crop the last column or row if needed */
551 const int width = GST_VIDEO_INFO_WIDTH(&info);
552 const int height = GST_VIDEO_INFO_HEIGHT(&info);
553 if ((width < 2) || (height < 2))
554 {
555 WLog_ERR(TAG, "Invalid video size %dx%d", width, height);
556 goto out;
557 }
558 hal->width = (UINT32)width & ~1u;
559 hal->height = (UINT32)height & ~1u;
560
561 /* 0/1 is a variable frame rate */
562 if ((GST_VIDEO_INFO_FPS_N(&info) > 0) && (GST_VIDEO_INFO_FPS_D(&info) > 0))
563 {
564 hal->frameRateNumerator = (UINT32)GST_VIDEO_INFO_FPS_N(&info);
565 hal->frameRateDenominator = (UINT32)GST_VIDEO_INFO_FPS_D(&info);
566 }
567 else
568 {
569 WLog_WARN(TAG, "Variable frame rate, announcing %d fps",
570 CAM_GST_FRAMERATE_NUMERATOR_DEFAULT);
571 hal->frameRateNumerator = CAM_GST_FRAMERATE_NUMERATOR_DEFAULT;
572 hal->frameRateDenominator = CAM_GST_FRAMERATE_DENOMINATOR_DEFAULT;
573 }
574
575 hal->sampleSize = 3ull * hal->width * hal->height / 2;
576 hal->sample = calloc(1, hal->sampleSize);
577 if (!hal->sample)
578 goto out;
579
580 WLog_INFO(TAG,
581 "%s pipeline: %dx%d, announced as %" PRIu32 "x%" PRIu32 " at %" PRIu32 "/%" PRIu32
582 " fps",
583 hal->live ? "Live" : "Non live", width, height, hal->width, hal->height,
584 hal->frameRateNumerator, hal->frameRateDenominator);
585 rc = TRUE;
586
587out:
588 if (sample)
589 gst_sample_unref(sample);
590
591 /* nothing runs until the server starts the camera */
592 cam_gst_pipeline_destroy(hal);
593 return rc;
594}
595
596static const char* cam_gst_basename(const char* path)
597{
598 const char* name = path;
599 for (const char* cur = path; *cur; cur++)
600 {
601 if ((*cur == '/') || (*cur == '\\'))
602 name = cur + 1;
603 }
604 return name;
605}
606
607WINPR_ATTR_MALLOC(free, 1)
608static char* cam_gst_read_file(const char* path)
609{
610 WINPR_ASSERT(path);
611
612 char* content = nullptr;
613 FILE* fp = winpr_fopen(path, "rb");
614 if (!fp)
615 {
616 WLog_ERR(TAG, "Unable to open pipeline file %s", path);
617 return nullptr;
618 }
619
620 if (_fseeki64(fp, 0, SEEK_END) != 0)
621 goto out;
622 const INT64 size = _ftelli64(fp);
623 if ((size <= 0) || (size > 1024ll * 1024ll) || (_fseeki64(fp, 0, SEEK_SET) != 0))
624 goto out;
625
626 content = calloc(1, (size_t)size + 1);
627 if (!content)
628 goto out;
629
630 if (fread(content, 1, (size_t)size, fp) != (size_t)size)
631 {
632 free(content);
633 content = nullptr;
634 }
635
636out:
637 if (!content)
638 WLog_ERR(TAG, "Unable to read pipeline file %s", path);
639 (void)fclose(fp);
640 return content;
641}
642
643WINPR_ATTR_NODISCARD
644static BOOL cam_gst_parse_bool(const char* value, BOOL* result)
645{
646 WINPR_ASSERT(value);
647 WINPR_ASSERT(result);
648
649 if ((_stricmp(value, "on") == 0) || (_stricmp(value, "true") == 0) || (strcmp(value, "1") == 0))
650 *result = TRUE;
651 else if ((_stricmp(value, "off") == 0) || (_stricmp(value, "false") == 0) ||
652 (strcmp(value, "0") == 0))
653 *result = FALSE;
654 else
655 return FALSE;
656 return TRUE;
657}
658
659WINPR_ATTR_NODISCARD
660static BOOL cam_gst_parse_args(CamGstHal* hal, const ADDIN_ARGV* args)
661{
662 WINPR_ASSERT(hal);
663
664 COMMAND_LINE_ARGUMENT_A gst_args[] = {
665 { "pipeline", COMMAND_LINE_VALUE_REQUIRED, "<pipeline>", nullptr, nullptr, -1, nullptr,
666 "pipeline producing video, in gst-launch syntax" },
667 { "pipeline-file", COMMAND_LINE_VALUE_REQUIRED, "<path>", nullptr, nullptr, -1, nullptr,
668 "file containing the pipeline" },
669 { "file", COMMAND_LINE_VALUE_REQUIRED, "<path>", nullptr, nullptr, -1, nullptr,
670 "media file to play" },
671 { "name", COMMAND_LINE_VALUE_REQUIRED, "<name>", nullptr, nullptr, -1, nullptr,
672 "camera name" },
673 { "loop", COMMAND_LINE_VALUE_REQUIRED, "<on|off>", nullptr, nullptr, -1, nullptr,
674 "play the pipeline again when it ends" },
675 { nullptr, 0, nullptr, nullptr, nullptr, -1, nullptr, nullptr }
676 };
677
678 char* source = nullptr;
679 char* defaultName = nullptr;
680 size_t sources = 0;
681 size_t len = 0;
682 BOOL rc = FALSE;
683
684 if (args && (args->argc > 1))
685 {
686 const DWORD flags = COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON |
687 COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
688 const int status = CommandLineParseArgumentsA(args->argc, args->argv, gst_args, flags, hal,
689 nullptr, nullptr);
690 if (status != 0)
691 return FALSE;
692
693 const COMMAND_LINE_ARGUMENT_A* arg = gst_args;
694 do
695 {
696 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
697 continue;
698
699 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "pipeline")
700 {
701 sources++;
702 free(source);
703 source = _strdup(arg->Value);
704 if (!source)
705 goto out;
706 }
707 CommandLineSwitchCase(arg, "pipeline-file")
708 {
709 sources++;
710 free(source);
711 source = cam_gst_read_file(arg->Value);
712 if (!source)
713 goto out;
714 }
715 CommandLineSwitchCase(arg, "file")
716 {
717 sources++;
718 free(source);
719 source = nullptr;
720
721 GError* error = nullptr;
722 gchar* uri = gst_filename_to_uri(arg->Value, &error);
723 if (!uri)
724 {
725 WLog_ERR(TAG, "Invalid file %s: %s", arg->Value,
726 error ? error->message : "unknown error");
727 g_clear_error(&error);
728 goto out;
729 }
730 /* only decode the video streams, the other ones are exposed undecoded and stay
731 * unlinked. Don't add expose-all-streams=false: combined with these caps,
732 * decodebin stops finding decoders at all ("Missing decoder: H.264") */
733 winpr_asprintf(&source, &len, "uridecodebin uri=\"%s\" caps=video/x-raw", uri);
734 g_free(uri);
735 if (!source)
736 goto out;
737
738 free(defaultName);
739 defaultName = nullptr;
740 winpr_asprintf(&defaultName, &len, "Movie: %s", cam_gst_basename(arg->Value));
741 if (!defaultName)
742 goto out;
743 }
744 CommandLineSwitchCase(arg, "name")
745 {
746 free(hal->name);
747 hal->name = _strdup(arg->Value);
748 if (!hal->name)
749 goto out;
750 }
751 CommandLineSwitchCase(arg, "loop")
752 {
753 if (!cam_gst_parse_bool(arg->Value, &hal->loop))
754 {
755 WLog_ERR(TAG, "Invalid loop value %s, expecting on or off", arg->Value);
756 goto out;
757 }
758 }
759 CommandLineSwitchDefault(arg)
760 {
761 }
762 CommandLineSwitchEnd(arg)
763 } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr);
764 }
765
766 if (sources != 1)
767 {
768 WLog_ERR(TAG, "The gst backend needs one of pipeline:<pipeline>, pipeline-file:<path> or "
769 "file:<path>");
770 goto out;
771 }
772
773 winpr_asprintf(&hal->description, &len,
774 "%s ! videoconvert ! video/x-raw,format=I420 ! appsink name=" CAM_GST_SINK_NAME
775 " sync=true max-buffers=1 drop=true",
776 source);
777 if (!hal->description)
778 goto out;
779
780 if (!hal->name)
781 {
782 hal->name = defaultName ? defaultName : _strdup("GStreamer camera");
783 defaultName = nullptr;
784 if (!hal->name)
785 goto out;
786 }
787 rc = TRUE;
788
789out:
790 free(source);
791 free(defaultName);
792 return rc;
793}
794
800FREERDP_ENTRY_POINT(UINT VCAPITYPE gst_freerdp_rdpecam_client_subsystem_entry(
802{
803 UINT ret = ERROR_INVALID_PARAMETER;
804 WINPR_ASSERT(pEntryPoints);
805
806 GError* error = nullptr;
807 if (!gst_init_check(nullptr, nullptr, &error))
808 {
809 WLog_ERR(TAG, "Unable to initialize GStreamer: %s",
810 error ? error->message : "unknown error");
811 g_clear_error(&error);
812 return ERROR_INTERNAL_ERROR;
813 }
814
815 CamGstHal* hal = (CamGstHal*)calloc(1, sizeof(CamGstHal));
816 if (!hal)
817 return CHANNEL_RC_NO_MEMORY;
818
819 hal->iHal.Enumerate = cam_gst_enumerate;
820 hal->iHal.RequestDrivenCapture = TRUE;
821 hal->iHal.GetMediaTypeDescriptions = cam_gst_get_media_type_descriptions;
822 hal->iHal.Activate = cam_gst_activate;
823 hal->iHal.Deactivate = cam_gst_deactivate;
824 hal->iHal.StartStream = cam_gst_stream_start;
825 hal->iHal.StopStream = cam_gst_stream_stop;
826 hal->iHal.Free = cam_gst_free;
827 hal->loop = TRUE;
828
829 if (!InitializeCriticalSectionEx(&hal->lock, 0, 0))
830 {
831 free(hal);
832 return CHANNEL_RC_NO_MEMORY;
833 }
834
835 if (!cam_gst_parse_args(hal, pEntryPoints->args))
836 goto error;
837
838 /* fail now rather than when the server opens the camera */
839 if (!cam_gst_probe(hal))
840 goto error;
841
842 ret = pEntryPoints->pRegisterCameraHal(pEntryPoints->plugin, &hal->iHal);
843 if (ret != CHANNEL_RC_OK)
844 {
845 WLog_ERR(TAG, "RegisterCameraHal failed with error %" PRIu32 "", ret);
846 goto error;
847 }
848
849 return ret;
850
851error:
852 cam_gst_free(&hal->iHal);
853 return ret;
854}
Definition camera.h:237