FreeRDP
Loading...
Searching...
No Matches
video_main.c
1
19
20#include <freerdp/config.h>
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <winpr/crt.h>
27#include <winpr/assert.h>
28#include <winpr/cast.h>
29#include <winpr/synch.h>
30#include <winpr/print.h>
31#include <winpr/stream.h>
32#include <winpr/cmdline.h>
33#include <winpr/collections.h>
34#include <winpr/interlocked.h>
35#include <winpr/sysinfo.h>
36
37#include <freerdp/freerdp.h>
38#include <freerdp/addin.h>
39#include <freerdp/primitives.h>
40#include <freerdp/client/channels.h>
41#include <freerdp/client/geometry.h>
42#include <freerdp/client/video.h>
43#include <freerdp/channels/log.h>
44#include <freerdp/codec/h264.h>
45#include <freerdp/codec/yuv.h>
46#include <freerdp/timer.h>
47
48#define TAG CHANNELS_TAG("video.client")
49
50#include "video_main.h"
51
52/* Maximum number of frames in the queue. If the queue grows too large late frames are discarded. */
53#define QUEUE_SIZE_LIMIT 64u
54
55typedef struct
56{
57 IWTSPlugin wtsPlugin;
58
59 IWTSListener* controlListener;
60 IWTSListener* dataListener;
61 GENERIC_LISTENER_CALLBACK* control_callback;
62 GENERIC_LISTENER_CALLBACK* data_callback;
63
64 VideoClientContext* context;
65 BOOL initialized;
66 rdpContext* rdpcontext;
67 BOOL connected;
68 BOOL dataConnected;
69} VIDEO_PLUGIN;
70
71#define XF_VIDEO_UNLIMITED_RATE 31
72
73/* Upper limit for the reassembled packets of one sample (a single H264 access unit).
74 * The server controls CurrentPacketIndex/PacketsInSample, without a limit it can make
75 * the client collect an arbitrarily large sample. */
76#define VIDEO_MAX_SAMPLE_SIZE (32ul * 1024ul * 1024ul)
77
78static const BYTE MFVideoFormat_H264[] = { 'H', '2', '6', '4', 0x00, 0x00, 0x10, 0x00,
79 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 };
80
81typedef struct
82{
83 BYTE PresentationId;
84 UINT32 ScaledWidth;
85 UINT32 ScaledHeight;
86
87 UINT64 startTimeStamp;
88 UINT64 publishOffset;
89 wStream* currentSample;
90 UINT64 lastPublishTime;
91 UINT64 nextPublishTime;
92 volatile LONG refCounter;
93 H264_CONTEXT* h264;
94 VideoSurface* surface;
95 MAPPED_GEOMETRY* geometry;
96 VideoClientContext* video;
97} PresentationContext;
98
99typedef struct
100{
101 BYTE PresentationId;
102 UINT64 publishTime;
103 UINT64 hnsDuration;
104 MAPPED_GEOMETRY* geometry;
105 UINT32 w, h;
106 UINT32 scanline;
107 BYTE* surfaceData;
108} VideoFrame;
109
111struct s_VideoClientContextPriv
112{
113 VideoClientContext* video;
114 GeometryClientContext* geometry;
115 wQueue* frames;
116 CRITICAL_SECTION framesLock;
117 wBufferPool* surfacePool;
118 UINT32 publishedFrames;
119 UINT32 droppedFrames;
120 UINT32 lastSentRate;
121 UINT64 nextFeedbackTime;
122 PresentationContext* currentPresentation;
123 FreeRDP_TimerID timerID;
124};
125
126static void PresentationContext_unref(PresentationContext** presentation);
127static void VideoClientContextPriv_free(VideoClientContextPriv* priv);
128
129WINPR_ATTR_NODISCARD
130static const char* video_command_name(BYTE cmd)
131{
132 switch (cmd)
133 {
134 case TSMM_START_PRESENTATION:
135 return "start";
136 case TSMM_STOP_PRESENTATION:
137 return "stop";
138 default:
139 return "<unknown>";
140 }
141}
142
143static void video_client_context_set_geometry(VideoClientContext* video,
144 GeometryClientContext* geometry)
145{
146 WINPR_ASSERT(video);
147 WINPR_ASSERT(video->priv);
148
149 video->priv->geometry = geometry;
150}
151
152WINPR_ATTR_MALLOC(VideoClientContextPriv_free, 1)
153static VideoClientContextPriv* VideoClientContextPriv_new(VideoClientContext* video)
154{
155 WINPR_ASSERT(video);
156 VideoClientContextPriv* ret = calloc(1, sizeof(*ret));
157 if (!ret)
158 return nullptr;
159
160 ret->frames = Queue_New(TRUE, 10, 2);
161 if (!ret->frames)
162 {
163 WLog_ERR(TAG, "unable to allocate frames queue");
164 goto fail;
165 }
166
167 ret->surfacePool = BufferPool_New(FALSE, 0, 16);
168 if (!ret->surfacePool)
169 {
170 WLog_ERR(TAG, "unable to create surface pool");
171 goto fail;
172 }
173
174 if (!InitializeCriticalSectionAndSpinCount(&ret->framesLock, 4 * 1000))
175 {
176 WLog_ERR(TAG, "unable to initialize frames lock");
177 goto fail;
178 }
179
180 ret->video = video;
181
182 /* don't set to unlimited so that we have the chance to send a feedback in
183 * the first second (for servers that want feedback directly)
184 */
185 ret->lastSentRate = 30;
186 return ret;
187
188fail:
189 VideoClientContextPriv_free(ret);
190 return nullptr;
191}
192
193WINPR_ATTR_NODISCARD
194static BOOL PresentationContext_ref(PresentationContext* presentation)
195{
196 WINPR_ASSERT(presentation);
197
198 const LONG val = InterlockedIncrement(&presentation->refCounter);
199 return val > 0;
200}
201
202static void PresentationContext_free(PresentationContext* presentation)
203{
204 if (!presentation)
205 return;
206
207 MAPPED_GEOMETRY* geometry = presentation->geometry;
208 if (geometry)
209 {
210 geometry->MappedGeometryUpdate = nullptr;
211 geometry->MappedGeometryClear = nullptr;
212 geometry->custom = nullptr;
213 mappedGeometryUnref(geometry);
214 }
215
216 h264_context_free(presentation->h264);
217 Stream_Free(presentation->currentSample, TRUE);
218 presentation->video->deleteSurface(presentation->video, presentation->surface);
219 free(presentation);
220}
221
222WINPR_ATTR_MALLOC(PresentationContext_free, 1)
223static PresentationContext* PresentationContext_new(VideoClientContext* video, BYTE PresentationId,
224 UINT32 x, UINT32 y, UINT32 width, UINT32 height)
225{
226 if ((width == 0) || (height == 0))
227 {
228 WLog_ERR(TAG, "width==%" PRIu32 ", height=%" PRIu32, width, height);
229 return nullptr;
230 }
231
232 if (width > SIZE_MAX / height / 4)
233 return nullptr;
234
235 const size_t s = 4ULL * width * height;
236
237 WINPR_ASSERT(video);
238
239 if (s > INT32_MAX)
240 return nullptr;
241
242 PresentationContext* ret = calloc(1, sizeof(*ret));
243 if (!ret)
244 return nullptr;
245
246 ret->video = video;
247 ret->PresentationId = PresentationId;
248
249 ret->h264 = h264_context_new(FALSE);
250 if (!ret->h264)
251 {
252 WLog_ERR(TAG, "unable to create a h264 context");
253 goto fail;
254 }
255
256 VIDEO_PLUGIN* plugin = (VIDEO_PLUGIN*)video->handle;
257 WINPR_ASSERT(plugin);
258 WINPR_ASSERT(plugin->rdpcontext);
259 if (!h264_context_set_option(ret->h264, H264_CONTEXT_OPTION_HW_ACCEL,
260 (UINT32)freerdp_settings_get_bool(plugin->rdpcontext->settings,
261 FreeRDP_SoftwareGdi) == 0))
262 goto fail;
263 if (!h264_context_reset(ret->h264, width, height))
264 goto fail;
265
266 ret->currentSample = Stream_New(nullptr, 4096);
267 if (!ret->currentSample)
268 {
269 WLog_ERR(TAG, "unable to create current packet stream");
270 goto fail;
271 }
272
273 ret->surface = video->createSurface(video, x, y, width, height);
274 if (!ret->surface)
275 {
276 WLog_ERR(TAG, "unable to create surface");
277 goto fail;
278 }
279
280 if (!PresentationContext_ref(ret))
281 goto fail;
282
283 return ret;
284
285fail:
286 PresentationContext_free(ret);
287 return nullptr;
288}
289
290static void PresentationContext_unref(PresentationContext** ppresentation)
291{
292 WINPR_ASSERT(ppresentation);
293
294 PresentationContext* presentation = *ppresentation;
295 if (!presentation)
296 return;
297
298 if (InterlockedDecrement(&presentation->refCounter) > 0)
299 return;
300 *ppresentation = nullptr;
301
302 PresentationContext_free(presentation);
303}
304
305static void VideoFrame_free(VideoClientContextPriv* priv, VideoFrame* frame)
306{
307 WINPR_ASSERT(priv);
308 if (!frame)
309 return;
310
311 mappedGeometryUnref(frame->geometry);
312
313 BufferPool_Return(priv->surfacePool, frame->surfaceData);
314 free(frame);
315}
316
317WINPR_ATTR_MALLOC(VideoFrame_free, 1)
318static VideoFrame* VideoFrame_new(VideoClientContextPriv* priv, PresentationContext* presentation,
319 MAPPED_GEOMETRY* geom)
320{
321 WINPR_ASSERT(priv);
322 WINPR_ASSERT(presentation);
323 WINPR_ASSERT(geom);
324
325 const VideoSurface* surface = presentation->surface;
326 WINPR_ASSERT(surface);
327
328 VideoFrame* frame = calloc(1, sizeof(VideoFrame));
329 if (!frame)
330 goto fail;
331 frame->PresentationId = presentation->PresentationId;
332
333 mappedGeometryRef(geom);
334
335 frame->publishTime = presentation->lastPublishTime;
336 frame->geometry = geom;
337 frame->w = surface->alignedWidth;
338 frame->h = surface->alignedHeight;
339 frame->scanline = surface->scanline;
340
341 frame->surfaceData = BufferPool_Take(priv->surfacePool, 1ll * frame->scanline * frame->h);
342 if (!frame->surfaceData)
343 goto fail;
344
345 return frame;
346
347fail:
348 VideoFrame_free(priv, frame);
349 return nullptr;
350}
351
352static void drain_queue(VideoClientContextPriv* priv, size_t limit)
353{
354 while (Queue_Count(priv->frames) > limit)
355 {
356 VideoFrame* frame = Queue_Dequeue(priv->frames);
357 VideoFrame_free(priv, frame);
358 }
359}
360
361void VideoClientContextPriv_free(VideoClientContextPriv* priv)
362{
363 if (!priv)
364 return;
365
366 EnterCriticalSection(&priv->framesLock);
367
368 if (priv->frames)
369 drain_queue(priv, 0);
370
371 Queue_Free(priv->frames);
372 LeaveCriticalSection(&priv->framesLock);
373
374 DeleteCriticalSection(&priv->framesLock);
375
376 if (priv->currentPresentation)
377 PresentationContext_unref(&priv->currentPresentation);
378
379 BufferPool_Free(priv->surfacePool);
380 free(priv);
381}
382
383WINPR_ATTR_NODISCARD
384static UINT video_channel_write(VIDEO_PLUGIN* video, const BYTE* data, UINT32 length)
385{
386 UINT rc = ERROR_BAD_CONFIGURATION;
387
388 WINPR_ASSERT(video);
389 WINPR_ASSERT(video->context);
390
391 VideoClientContextPriv* priv = video->context->priv;
392 WINPR_ASSERT(priv);
393
394 /* video_timer writes from the main loop thread while the drdynvc thread may close (and then
395 * free) the control channel: hold the lock so video_control_on_close waits for the write. */
396 EnterCriticalSection(&priv->framesLock);
397 if (video->control_callback && video->control_callback->channel_callback)
398 {
399 IWTSVirtualChannel* channel = video->control_callback->channel_callback->channel;
400 if (channel && channel->Write)
401 rc = channel->Write(channel, length, data, nullptr);
402 }
403 LeaveCriticalSection(&priv->framesLock);
404 return rc;
405}
406
407WINPR_ATTR_NODISCARD
408static UINT video_control_send_presentation_response(VideoClientContext* context,
410{
411 BYTE buf[12] = WINPR_C_ARRAY_INIT;
412
413 WINPR_ASSERT(context);
414 WINPR_ASSERT(resp);
415
416 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)context->handle;
417 WINPR_ASSERT(video);
418
419 wStream* s = Stream_New(buf, 12);
420 if (!s)
421 return CHANNEL_RC_NO_MEMORY;
422
423 Stream_Write_UINT32(s, 12); /* cbSize */
424 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_PRESENTATION_RESPONSE); /* PacketType */
425 Stream_Write_UINT8(s, resp->PresentationId);
426 Stream_Zero(s, 3);
427 Stream_SealLength(s);
428 Stream_Free(s, FALSE);
429
430 return video_channel_write(video, buf, sizeof(buf));
431}
432
433WINPR_ATTR_NODISCARD
434static BOOL video_onMappedGeometryUpdate(MAPPED_GEOMETRY* geometry)
435{
436 WINPR_ASSERT(geometry);
437
438 PresentationContext* presentation = (PresentationContext*)geometry->custom;
439 WINPR_ASSERT(presentation);
440
441 RDP_RECT* r = &geometry->geometry.boundingRect;
442 WLog_DBG(TAG,
443 "geometry updated topGeom=(%" PRId32 ",%" PRId32 "-%" PRId32 "x%" PRId32
444 ") geom=(%" PRId32 ",%" PRId32 "-%" PRId32 "x%" PRId32 ") rects=(%" PRId16 ",%" PRId16
445 "-%" PRId16 "x%" PRId16 ")",
446 geometry->topLevelLeft, geometry->topLevelTop,
447 geometry->topLevelRight - geometry->topLevelLeft,
448 geometry->topLevelBottom - geometry->topLevelTop,
449
450 geometry->left, geometry->top, geometry->right - geometry->left,
451 geometry->bottom - geometry->top,
452
453 r->x, r->y, r->width, r->height);
454
455 WINPR_ASSERT(presentation->surface);
456 if (geometry->topLevelLeft < 0)
457 {
458 WLog_ERR(TAG, "geometry->topLevelLeft=%d < 0", geometry->topLevelLeft);
459 return FALSE;
460 }
461 if (geometry->left < 0)
462 {
463 WLog_ERR(TAG, "geometry->left=%d < 0", geometry->left);
464 return FALSE;
465 }
466 presentation->surface->x = WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelLeft) +
467 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->left);
468
469 if (geometry->topLevelTop < 0)
470 {
471 WLog_ERR(TAG, "geometry->topLevelTop=%d < 0", geometry->topLevelTop);
472 return FALSE;
473 }
474 if (geometry->top < 0)
475 {
476 WLog_ERR(TAG, "geometry->top=%d < 0", geometry->top);
477 return FALSE;
478 }
479 presentation->surface->y = WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelTop) +
480 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->top);
481
482 return TRUE;
483}
484
485WINPR_ATTR_NODISCARD
486static BOOL video_onMappedGeometryClear(MAPPED_GEOMETRY* geometry)
487{
488 WINPR_ASSERT(geometry);
489
490 PresentationContext* presentation = (PresentationContext*)geometry->custom;
491 WINPR_ASSERT(presentation);
492
493 mappedGeometryUnref(presentation->geometry);
494 presentation->geometry = nullptr;
495 return TRUE;
496}
497
498WINPR_ATTR_NODISCARD
499static UINT video_PresentationRequest(VideoClientContext* video,
500 const TSMM_PRESENTATION_REQUEST* req)
501{
502 UINT ret = CHANNEL_RC_OK;
503
504 WINPR_ASSERT(video);
505 WINPR_ASSERT(req);
506
507 VideoClientContextPriv* priv = video->priv;
508 WINPR_ASSERT(priv);
509
510 EnterCriticalSection(&priv->framesLock);
511 if (req->Command == TSMM_START_PRESENTATION)
512 {
513 MAPPED_GEOMETRY* geom = nullptr;
514 TSMM_PRESENTATION_RESPONSE resp = WINPR_C_ARRAY_INIT;
515
516 if (memcmp(req->VideoSubtypeId, MFVideoFormat_H264, 16) != 0)
517 {
518 WLog_ERR(TAG, "not a H264 video, ignoring request");
519 goto fail;
520 }
521
522 if (priv->currentPresentation)
523 {
524 if (priv->currentPresentation->PresentationId == req->PresentationId)
525 {
526 WLog_ERR(TAG, "ignoring start request for existing presentation %" PRIu8,
527 req->PresentationId);
528 goto fail;
529 }
530
531 WLog_ERR(TAG, "releasing current presentation %" PRIu8, req->PresentationId);
532 PresentationContext_unref(&priv->currentPresentation);
533 }
534
535 if (!priv->geometry)
536 {
537 WLog_ERR(TAG, "geometry channel not ready, ignoring request");
538 goto fail;
539 }
540
541 geom = HashTable_GetItemValue(priv->geometry->geometries, &(req->GeometryMappingId));
542 if (!geom)
543 {
544 WLog_ERR(TAG, "geometry mapping 0x%" PRIx64 " not registered", req->GeometryMappingId);
545 goto fail;
546 }
547
548 WLog_DBG(TAG, "creating presentation 0x%x", req->PresentationId);
549 if ((geom->topLevelLeft < 0) || (geom->left < 0) || (geom->topLevelTop < 0) ||
550 (geom->top < 0))
551 {
552 WLog_ERR(TAG,
553 "geometry: topLevelLeft=%" PRId32 " < 0, left=%" PRId32
554 " < 0, topLevelTop=%" PRId32 " < 0, top=%" PRId32 " < 0",
555 geom->topLevelLeft, geom->left, geom->topLevelTop, geom->top);
556 goto fail;
557 }
558
559 priv->currentPresentation = PresentationContext_new(
560 video, req->PresentationId,
561 WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelLeft + geom->left),
562 WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelTop + geom->top), req->SourceWidth,
563 req->SourceHeight);
564 if (!priv->currentPresentation)
565 {
566 WLog_ERR(TAG, "unable to create presentation video");
567 ret = CHANNEL_RC_NO_MEMORY;
568 goto fail;
569 }
570
571 mappedGeometryRef(geom);
572 priv->currentPresentation->geometry = geom;
573
574 priv->currentPresentation->video = video;
575 priv->currentPresentation->ScaledWidth = req->ScaledWidth;
576 priv->currentPresentation->ScaledHeight = req->ScaledHeight;
577
578 geom->custom = priv->currentPresentation;
579 geom->MappedGeometryUpdate = video_onMappedGeometryUpdate;
580 geom->MappedGeometryClear = video_onMappedGeometryClear;
581
582 /* send back response */
583 resp.PresentationId = req->PresentationId;
584 ret = video_control_send_presentation_response(video, &resp);
585 }
586 else if (req->Command == TSMM_STOP_PRESENTATION)
587 {
588 WLog_DBG(TAG, "stopping presentation 0x%x", req->PresentationId);
589 if (!priv->currentPresentation)
590 {
591 WLog_ERR(TAG, "unknown presentation to stop %" PRIu8, req->PresentationId);
592 goto fail;
593 }
594
595 priv->droppedFrames = 0;
596 priv->publishedFrames = 0;
597 PresentationContext_unref(&priv->currentPresentation);
598 }
599
600fail:
601 LeaveCriticalSection(&priv->framesLock);
602 return ret;
603}
604
605WINPR_ATTR_NODISCARD
606static UINT video_read_tsmm_presentation_req(VideoClientContext* context, wStream* s)
607{
608 TSMM_PRESENTATION_REQUEST req = WINPR_C_ARRAY_INIT;
609
610 WINPR_ASSERT(context);
611 WINPR_ASSERT(s);
612
613 if (!Stream_CheckAndLogRequiredLength(TAG, s, 60))
614 return ERROR_INVALID_DATA;
615
616 Stream_Read_UINT8(s, req.PresentationId);
617 Stream_Read_UINT8(s, req.Version);
618 Stream_Read_UINT8(s, req.Command);
619 Stream_Read_UINT8(s, req.FrameRate); /* FrameRate - reserved and ignored */
620
621 Stream_Seek_UINT16(s); /* AverageBitrateKbps reserved and ignored */
622 Stream_Seek_UINT16(s); /* reserved */
623
624 Stream_Read_UINT32(s, req.SourceWidth);
625 Stream_Read_UINT32(s, req.SourceHeight);
626 Stream_Read_UINT32(s, req.ScaledWidth);
627 Stream_Read_UINT32(s, req.ScaledHeight);
628 if ((req.ScaledWidth == 0) || (req.SourceHeight == 0) || (req.ScaledWidth == 0) ||
629 (req.ScaledHeight == 0))
630 {
631 WLog_ERR(TAG,
632 "SourceWidth=%" PRIu32 ", SourceHeight=%" PRIu32 ", ScaledWidth=%" PRIu32
633 ", ScaledHeight=%" PRIu32,
634 req.SourceWidth, req.SourceHeight, req.ScaledWidth, req.ScaledHeight);
635 return ERROR_INVALID_DATA;
636 }
637 Stream_Read_UINT64(s, req.hnsTimestampOffset);
638 Stream_Read_UINT64(s, req.GeometryMappingId);
639 Stream_Read(s, req.VideoSubtypeId, 16);
640
641 Stream_Read_UINT32(s, req.cbExtra);
642
643 if (!Stream_CheckAndLogRequiredLength(TAG, s, req.cbExtra))
644 return ERROR_INVALID_DATA;
645
646 req.pExtraData = Stream_Pointer(s);
647
648 WLog_DBG(TAG,
649 "presentationReq: id:%" PRIu8 " version:%" PRIu8
650 " command:%s srcWidth/srcHeight=%" PRIu32 "x%" PRIu32 " scaled Width/Height=%" PRIu32
651 "x%" PRIu32 " timestamp=%" PRIu64 " mappingId=%" PRIx64 "",
652 req.PresentationId, req.Version, video_command_name(req.Command), req.SourceWidth,
653 req.SourceHeight, req.ScaledWidth, req.ScaledHeight, req.hnsTimestampOffset,
654 req.GeometryMappingId);
655
656 return video_PresentationRequest(context, &req);
657}
658
664WINPR_ATTR_NODISCARD
665static UINT video_control_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* s)
666{
667 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
668 UINT ret = CHANNEL_RC_OK;
669 UINT32 cbSize = 0;
670 UINT32 packetType = 0;
671
672 WINPR_ASSERT(callback);
673 WINPR_ASSERT(s);
674
675 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin;
676 WINPR_ASSERT(video);
677
678 VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface;
679 WINPR_ASSERT(context);
680
681 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
682 return ERROR_INVALID_DATA;
683
684 Stream_Read_UINT32(s, cbSize);
685 if (cbSize < 8)
686 {
687 WLog_ERR(TAG, "invalid cbSize %" PRIu32 ", expected 8", cbSize);
688 return ERROR_INVALID_DATA;
689 }
690 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
691 return ERROR_INVALID_DATA;
692
693 Stream_Read_UINT32(s, packetType);
694 switch (packetType)
695 {
696 case TSMM_PACKET_TYPE_PRESENTATION_REQUEST:
697 ret = video_read_tsmm_presentation_req(context, s);
698 break;
699 default:
700 WLog_ERR(TAG, "not expecting packet type %" PRIu32 "", packetType);
701 ret = ERROR_UNSUPPORTED_TYPE;
702 break;
703 }
704
705 return ret;
706}
707
708static UINT video_control_send_client_notification(VideoClientContext* context,
709 const TSMM_CLIENT_NOTIFICATION* notif)
710{
711 BYTE buf[100] = WINPR_C_ARRAY_INIT;
712
713 WINPR_ASSERT(context);
714 WINPR_ASSERT(notif);
715
716 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)context->handle;
717 WINPR_ASSERT(video);
718
719 wStream* s = Stream_New(buf, 32);
720 if (!s)
721 return CHANNEL_RC_NO_MEMORY;
722
723 UINT32 cbSize = 16;
724 Stream_Seek_UINT32(s); /* cbSize */
725 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_CLIENT_NOTIFICATION); /* PacketType */
726 Stream_Write_UINT8(s, notif->PresentationId);
727 Stream_Write_UINT8(s, notif->NotificationType);
728 Stream_Zero(s, 2);
729 if (notif->NotificationType == TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE)
730 {
731 Stream_Write_UINT32(s, 16); /* cbData */
732
733 /* TSMM_CLIENT_NOTIFICATION_FRAMERATE_OVERRIDE */
734 Stream_Write_UINT32(s, notif->FramerateOverride.Flags);
735 Stream_Write_UINT32(s, notif->FramerateOverride.DesiredFrameRate);
736 Stream_Zero(s, 4ULL * 2ULL);
737
738 cbSize += 4UL * 4UL;
739 }
740 else
741 {
742 Stream_Write_UINT32(s, 0); /* cbData */
743 }
744
745 Stream_SealLength(s);
746 Stream_ResetPosition(s);
747 Stream_Write_UINT32(s, cbSize);
748 Stream_Free(s, FALSE);
749
750 return video_channel_write(video, buf, cbSize);
751}
752
753static void video_timer(VideoClientContext* video, UINT64 now)
754{
755 VideoFrame* frame = nullptr;
756
757 WINPR_ASSERT(video);
758
759 VideoClientContextPriv* priv = video->priv;
760 WINPR_ASSERT(priv);
761
762 EnterCriticalSection(&priv->framesLock);
763 PresentationContext* presentation = video->priv->currentPresentation;
764 do
765 {
766 const VideoFrame* peekFrame = (VideoFrame*)Queue_Peek(priv->frames);
767 if (!peekFrame)
768 break;
769
770 if (peekFrame->publishTime > now)
771 break;
772
773 if (frame)
774 {
775 WLog_DBG(TAG, "dropping frame @%" PRIu64, frame->publishTime);
776 priv->droppedFrames++;
777 VideoFrame_free(priv, frame);
778 }
779 frame = Queue_Dequeue(priv->frames);
780 } while (1);
781
782 if (frame)
783 {
784 if (presentation && (presentation->PresentationId == frame->PresentationId))
785 {
786 VideoSurface* surface = presentation->surface;
787 const size_t frameSize = 1ull * frame->scanline * frame->h;
788 const size_t surfaceSize = 1ull * surface->scanline * surface->alignedHeight;
789
790 /* the presentation id is reused by the server across presentations of different
791 * sizes, so a frame queued for a previous, larger presentation can outlive it in
792 * the queue. copying it would write past the smaller surface->data buffer. */
793 if (frameSize > surfaceSize)
794 WLog_WARN(TAG, "dropping stale frame of %" PRIuz " bytes, surface holds %" PRIuz,
795 frameSize, surfaceSize);
796 else
797 {
798 priv->publishedFrames++;
799 memcpy(surface->data, frame->surfaceData, frameSize);
800
801 WINPR_ASSERT(video->showSurface);
802 if (!video->showSurface(video, surface, presentation->ScaledWidth,
803 presentation->ScaledHeight))
804 WLog_WARN(TAG, "showSurface failed");
805 }
806 }
807 VideoFrame_free(priv, frame);
808 }
809
810 if (priv->nextFeedbackTime < now)
811 {
812 /* we can compute some feedback only if we have some published frames and
813 * a current presentation
814 */
815 if (priv->publishedFrames && priv->currentPresentation)
816 {
817 UINT32 computedRate = 0;
818
819 if (!PresentationContext_ref(priv->currentPresentation))
820 WLog_WARN(TAG, "PresentationContext_ref(priv->currentPresentation) failed");
821
822 if (priv->droppedFrames)
823 {
829 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
830 computedRate = 24;
831 else
832 {
833 computedRate = priv->lastSentRate - 2;
834 if (!computedRate)
835 computedRate = 2;
836 }
837 }
838 else
839 {
844 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
845 computedRate = XF_VIDEO_UNLIMITED_RATE; /* stay unlimited */
846 else
847 {
848 computedRate = priv->lastSentRate + 2;
849 if (computedRate > XF_VIDEO_UNLIMITED_RATE)
850 computedRate = XF_VIDEO_UNLIMITED_RATE;
851 }
852 }
853
854 if (computedRate != priv->lastSentRate)
855 {
856 TSMM_CLIENT_NOTIFICATION notif = WINPR_C_ARRAY_INIT;
857
858 WINPR_ASSERT(priv->currentPresentation);
859 notif.PresentationId = priv->currentPresentation->PresentationId;
860 notif.NotificationType = TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE;
861 if (computedRate == XF_VIDEO_UNLIMITED_RATE)
862 {
863 notif.FramerateOverride.Flags = 0x01;
864 notif.FramerateOverride.DesiredFrameRate = 0x00;
865 }
866 else
867 {
868 notif.FramerateOverride.Flags = 0x02;
869 notif.FramerateOverride.DesiredFrameRate = computedRate;
870 }
871
872 video_control_send_client_notification(video, &notif);
873 priv->lastSentRate = computedRate;
874
875 WLog_VRB(TAG,
876 "server notified with rate %" PRIu32 " published=%" PRIu32
877 " dropped=%" PRIu32,
878 priv->lastSentRate, priv->publishedFrames, priv->droppedFrames);
879 }
880
881 PresentationContext_unref(&priv->currentPresentation);
882 }
883
884 priv->droppedFrames = 0;
885 priv->publishedFrames = 0;
886 priv->nextFeedbackTime = now + 1000;
887 }
888 LeaveCriticalSection(&priv->framesLock);
889}
890
891WINPR_ATTR_NODISCARD
892static UINT video_VideoData(VideoClientContext* context, const TSMM_VIDEO_DATA* data)
893{
894 int status = 0;
895 UINT res = CHANNEL_RC_OK;
896
897 WINPR_ASSERT(context);
898 WINPR_ASSERT(data);
899
900 VideoClientContextPriv* priv = context->priv;
901 WINPR_ASSERT(priv);
902
903 EnterCriticalSection(&priv->framesLock);
904 PresentationContext* presentation = priv->currentPresentation;
905 if (!presentation)
906 {
907 WLog_ERR(TAG, "no current presentation");
908 LeaveCriticalSection(&priv->framesLock);
909 return CHANNEL_RC_OK;
910 }
911
912 if (!PresentationContext_ref(presentation))
913 {
914 LeaveCriticalSection(&priv->framesLock);
915 return ERROR_INTERNAL_ERROR;
916 }
917
918 if (presentation->PresentationId != data->PresentationId)
919 {
920 WLog_ERR(TAG, "current presentation id=%" PRIu8 " doesn't match data id=%" PRIu8,
921 presentation->PresentationId, data->PresentationId);
922 goto out;
923 }
924
925 const size_t collected = Stream_GetPosition(presentation->currentSample);
926 if ((collected > VIDEO_MAX_SAMPLE_SIZE) || (data->cbSample > VIDEO_MAX_SAMPLE_SIZE - collected))
927 {
928 WLog_ERR(TAG, "sample %" PRIu32 " too large: %" PRIuz " + %" PRIu32 " bytes exceed %lu",
929 data->SampleNumber, collected, data->cbSample, VIDEO_MAX_SAMPLE_SIZE);
930 Stream_ResetPosition(presentation->currentSample);
931 res = ERROR_INVALID_DATA;
932 goto out;
933 }
934
935 if (!Stream_EnsureRemainingCapacity(presentation->currentSample, data->cbSample))
936 {
937 WLog_ERR(TAG, "unable to expand the current packet");
938 res = CHANNEL_RC_NO_MEMORY;
939 goto out;
940 }
941
942 Stream_Write(presentation->currentSample, data->pSample, data->cbSample);
943
944 if (data->CurrentPacketIndex == data->PacketsInSample)
945 {
946 VideoSurface* surface = presentation->surface;
947 H264_CONTEXT* h264 = presentation->h264;
948 const UINT64 startTime = winpr_GetTickCount64NS();
949 MAPPED_GEOMETRY* geom = presentation->geometry;
950
951 /* the sample is complete, the next one starts at the beginning of the buffer
952 * regardless of whether this one can be decoded */
953 Stream_SealLength(presentation->currentSample);
954 Stream_ResetPosition(presentation->currentSample);
955
956 if (!geom)
957 goto out;
958
959 const RECTANGLE_16 rect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedWidth),
960 WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedHeight) };
961
962 if (data->SampleNumber == 1)
963 {
964 presentation->lastPublishTime = startTime;
965 }
966
967 presentation->lastPublishTime += 100ull * data->hnsDuration;
968 const size_t len = Stream_Length(presentation->currentSample);
969 if (len > UINT32_MAX)
970 goto out;
971
972 BOOL enqueueResult = 0;
973 VideoFrame* frame = VideoFrame_new(priv, presentation, geom);
974 if (!frame)
975 {
976 WLog_ERR(TAG, "unable to create frame");
977 res = CHANNEL_RC_NO_MEMORY;
978 goto out;
979 }
980
981 status = avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
982 frame->surfaceData, surface->format, surface->scanline,
983 surface->alignedWidth, surface->alignedHeight, &rect, 1);
984 if (status < 0)
985 {
986 VideoFrame_free(priv, frame);
987 goto out;
988 }
989
990 Queue_Lock(priv->frames);
991 drain_queue(priv, QUEUE_SIZE_LIMIT);
992 enqueueResult = Queue_Enqueue(priv->frames, frame);
993 Queue_Unlock(priv->frames);
994
995 if (!enqueueResult)
996 {
997 WLog_ERR(TAG, "unable to enqueue frame");
998 VideoFrame_free(priv, frame);
999 res = CHANNEL_RC_NO_MEMORY;
1000 goto out;
1001 }
1002
1003 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): Queue_Enqueue owns frame
1004 WLog_DBG(TAG, "scheduling frame in %" PRIu64 " ns", (frame->publishTime - startTime));
1005 }
1006
1007out:
1008 PresentationContext_unref(&priv->currentPresentation);
1009 LeaveCriticalSection(&priv->framesLock);
1010
1011 return res;
1012}
1013
1014WINPR_ATTR_NODISCARD
1015static UINT video_data_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* s)
1016{
1017 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
1018 UINT32 cbSize = 0;
1019 UINT32 packetType = 0;
1020 TSMM_VIDEO_DATA data;
1021
1022 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin;
1023 WINPR_ASSERT(video);
1024
1025 VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface;
1026
1027 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1028 return ERROR_INVALID_DATA;
1029
1030 Stream_Read_UINT32(s, cbSize);
1031 if (cbSize < 8)
1032 {
1033 WLog_ERR(TAG, "invalid cbSize %" PRIu32 ", expected >= 8", cbSize);
1034 return ERROR_INVALID_DATA;
1035 }
1036
1037 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
1038 return ERROR_INVALID_DATA;
1039
1040 Stream_Read_UINT32(s, packetType);
1041 if (packetType != TSMM_PACKET_TYPE_VIDEO_DATA)
1042 {
1043 WLog_ERR(TAG, "only expecting VIDEO_DATA on the data channel");
1044 return ERROR_INVALID_DATA;
1045 }
1046
1047 if (!Stream_CheckAndLogRequiredLength(TAG, s, 32))
1048 return ERROR_INVALID_DATA;
1049
1050 Stream_Read_UINT8(s, data.PresentationId);
1051 Stream_Read_UINT8(s, data.Version);
1052 Stream_Read_UINT8(s, data.Flags);
1053 Stream_Seek_UINT8(s); /* reserved */
1054 Stream_Read_UINT64(s, data.hnsTimestamp);
1055 Stream_Read_UINT64(s, data.hnsDuration);
1056 Stream_Read_UINT16(s, data.CurrentPacketIndex);
1057 Stream_Read_UINT16(s, data.PacketsInSample);
1058 Stream_Read_UINT32(s, data.SampleNumber);
1059 Stream_Read_UINT32(s, data.cbSample);
1060 if (!Stream_CheckAndLogRequiredLength(TAG, s, data.cbSample))
1061 return ERROR_INVALID_DATA;
1062 data.pSample = Stream_Pointer(s);
1063
1064 /*
1065 WLog_DBG(TAG, "videoData: id:%"PRIu8" version:%"PRIu8" flags:0x%"PRIx8" timestamp=%"PRIu64"
1066 duration=%"PRIu64 " curPacketIndex:%"PRIu16" packetInSample:%"PRIu16" sampleNumber:%"PRIu32"
1067 cbSample:%"PRIu32"", data.PresentationId, data.Version, data.Flags, data.hnsTimestamp,
1068 data.hnsDuration, data.CurrentPacketIndex, data.PacketsInSample, data.SampleNumber,
1069 data.cbSample);
1070 */
1071
1072 return video_VideoData(context, &data);
1073}
1074
1080WINPR_ATTR_NODISCARD
1081static UINT video_control_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1082{
1083 if (pChannelCallback)
1084 {
1085 GENERIC_CHANNEL_CALLBACK* listener_callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
1086 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1087 if (video)
1088 {
1089 WINPR_ASSERT(video->context);
1090
1091 VideoClientContextPriv* priv = video->context->priv;
1092 WINPR_ASSERT(priv);
1093
1094 /* The callback is freed below and the channel by the caller: wait for a write that
1095 * is still using them, see video_channel_write */
1096 EnterCriticalSection(&priv->framesLock);
1097 if (video->control_callback)
1098 video->control_callback->channel_callback = nullptr;
1099 video->connected = FALSE;
1100 LeaveCriticalSection(&priv->framesLock);
1101 }
1102 }
1103 free(pChannelCallback);
1104 return CHANNEL_RC_OK;
1105}
1106
1107WINPR_ATTR_NODISCARD
1108static UINT video_data_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1109{
1110 if (pChannelCallback)
1111 {
1112 GENERIC_CHANNEL_CALLBACK* listener_callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
1113 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1114 if (video && video->data_callback)
1115 {
1116 video->data_callback->channel_callback = nullptr;
1117 }
1118
1119 if (video)
1120 video->dataConnected = FALSE;
1121 }
1122 free(pChannelCallback);
1123 return CHANNEL_RC_OK;
1124}
1125
1131// NOLINTBEGIN(readability-non-const-parameter)
1132WINPR_ATTR_NODISCARD
1133static UINT video_control_on_new_channel_connection(IWTSListenerCallback* listenerCallback,
1134 IWTSVirtualChannel* channel,
1135 WINPR_ATTR_UNUSED BYTE* Data,
1136 WINPR_ATTR_UNUSED BOOL* pbAccept,
1137 IWTSVirtualChannelCallback** ppCallback)
1138// NOLINTEND(readability-non-const-parameter)
1139{
1140 GENERIC_LISTENER_CALLBACK* listener_callback = (GENERIC_LISTENER_CALLBACK*)listenerCallback;
1141 WINPR_ASSERT(listener_callback);
1142
1143 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1144 WINPR_ASSERT(video);
1145
1146 if (video->connected)
1147 {
1148 WLog_ERR(TAG, "Channel already connected, terminating.");
1149 return ERROR_DEVICE_ALREADY_ATTACHED;
1150 }
1151
1152 GENERIC_CHANNEL_CALLBACK* callback =
1154 if (!callback)
1155 {
1156 WLog_ERR(TAG, "calloc failed!");
1157 return CHANNEL_RC_NO_MEMORY;
1158 }
1159
1160 callback->iface.OnDataReceived = video_control_on_data_received;
1161 callback->iface.OnClose = video_control_on_close;
1162 callback->plugin = listener_callback->plugin;
1163 callback->channel_mgr = listener_callback->channel_mgr;
1164 callback->channel = channel;
1165
1166 WINPR_ASSERT(video->context);
1167 WINPR_ASSERT(video->context->priv);
1168 EnterCriticalSection(&video->context->priv->framesLock);
1169 listener_callback->channel_callback = callback;
1170 LeaveCriticalSection(&video->context->priv->framesLock);
1171
1172 *ppCallback = &callback->iface;
1173 video->connected = TRUE;
1174
1175 return CHANNEL_RC_OK;
1176}
1177
1178// NOLINTBEGIN(readability-non-const-parameter)
1179WINPR_ATTR_NODISCARD
1180static UINT video_data_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1181 IWTSVirtualChannel* pChannel,
1182 WINPR_ATTR_UNUSED BYTE* Data,
1183 WINPR_ATTR_UNUSED BOOL* pbAccept,
1184 IWTSVirtualChannelCallback** ppCallback)
1185// NOLINTEND(readability-non-const-parameter)
1186{
1187 GENERIC_LISTENER_CALLBACK* listener_callback = (GENERIC_LISTENER_CALLBACK*)pListenerCallback;
1188 WINPR_ASSERT(listener_callback);
1189
1190 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1191 WINPR_ASSERT(video);
1192
1193 if (video->dataConnected)
1194 {
1195 WLog_ERR(TAG, "Channel already connected, terminating.");
1196 return ERROR_DEVICE_ALREADY_ATTACHED;
1197 }
1198
1199 GENERIC_CHANNEL_CALLBACK* callback =
1201 if (!callback)
1202 {
1203 WLog_ERR(TAG, "calloc failed!");
1204 return CHANNEL_RC_NO_MEMORY;
1205 }
1206
1207 callback->iface.OnDataReceived = video_data_on_data_received;
1208 callback->iface.OnClose = video_data_on_close;
1209 callback->plugin = listener_callback->plugin;
1210 callback->channel_mgr = listener_callback->channel_mgr;
1211 callback->channel = pChannel;
1212 listener_callback->channel_callback = callback;
1213
1214 *ppCallback = &callback->iface;
1215 video->dataConnected = TRUE;
1216
1217 return CHANNEL_RC_OK;
1218}
1219
1220WINPR_ATTR_NODISCARD
1221static uint64_t timer_cb(WINPR_ATTR_UNUSED rdpContext* context, void* userdata,
1222 WINPR_ATTR_UNUSED FreeRDP_TimerID timerID, uint64_t timestamp,
1223 uint64_t interval)
1224{
1225 VideoClientContext* video = userdata;
1226 if (!video)
1227 return 0;
1228 if (!video->timer)
1229 return 0;
1230
1231 video->timer(video, timestamp);
1232
1233 return interval;
1234}
1235
1241WINPR_ATTR_NODISCARD
1242static UINT video_plugin_initialize(IWTSPlugin* plugin, IWTSVirtualChannelManager* channelMgr)
1243{
1244 UINT status = 0;
1245 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)plugin;
1246
1247 if (video->initialized)
1248 {
1249 WLog_ERR(TAG, "[%s] channel initialized twice, aborting", VIDEO_CONTROL_DVC_CHANNEL_NAME);
1250 return ERROR_INVALID_DATA;
1251 }
1252
1253 {
1254 GENERIC_LISTENER_CALLBACK* callback =
1256 if (!callback)
1257 {
1258 WLog_ERR(TAG, "calloc for control callback failed!");
1259 return CHANNEL_RC_NO_MEMORY;
1260 }
1261
1262 callback->iface.OnNewChannelConnection = video_control_on_new_channel_connection;
1263 callback->plugin = plugin;
1264 callback->channel_mgr = channelMgr;
1265
1266 status = channelMgr->CreateListener(channelMgr, VIDEO_CONTROL_DVC_CHANNEL_NAME, 0,
1267 &callback->iface, &(video->controlListener));
1268 video->control_callback = callback;
1269 if (status != CHANNEL_RC_OK)
1270 return status;
1271 }
1272 video->controlListener->pInterface = video->wtsPlugin.pInterface;
1273
1274 {
1275 GENERIC_LISTENER_CALLBACK* callback =
1277 if (!callback)
1278 {
1279 WLog_ERR(TAG, "calloc for data callback failed!");
1280 return CHANNEL_RC_NO_MEMORY;
1281 }
1282
1283 callback->iface.OnNewChannelConnection = video_data_on_new_channel_connection;
1284 callback->plugin = plugin;
1285 callback->channel_mgr = channelMgr;
1286
1287 status = channelMgr->CreateListener(channelMgr, VIDEO_DATA_DVC_CHANNEL_NAME, 0,
1288 &callback->iface, &(video->dataListener));
1289 video->data_callback = callback;
1290 if (status == CHANNEL_RC_OK)
1291 video->dataListener->pInterface = video->wtsPlugin.pInterface;
1292 }
1293
1294 if (status == CHANNEL_RC_OK)
1295 video->context->priv->timerID =
1296 freerdp_timer_add(video->rdpcontext, 20000000, timer_cb, video->context, true);
1297 video->initialized = video->context->priv->timerID != 0;
1298 if (!video->initialized)
1299 status = ERROR_INTERNAL_ERROR;
1300 return status;
1301}
1302
1308WINPR_ATTR_NODISCARD
1309static UINT video_plugin_terminated(IWTSPlugin* pPlugin)
1310{
1311 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)pPlugin;
1312 if (!video)
1313 return CHANNEL_RC_INVALID_INSTANCE;
1314
1315 if (video->context && video->context->priv)
1316 freerdp_timer_remove(video->rdpcontext, video->context->priv->timerID);
1317
1318 if (video->control_callback)
1319 {
1320 IWTSVirtualChannelManager* mgr = video->control_callback->channel_mgr;
1321 if (mgr)
1322 IFCALL(mgr->DestroyListener, mgr, video->controlListener);
1323 }
1324 if (video->data_callback)
1325 {
1326 IWTSVirtualChannelManager* mgr = video->data_callback->channel_mgr;
1327 if (mgr)
1328 IFCALL(mgr->DestroyListener, mgr, video->dataListener);
1329 }
1330
1331 if (video->context)
1332 VideoClientContextPriv_free(video->context->priv);
1333
1334 free(video->control_callback);
1335 free(video->data_callback);
1336 free(video->wtsPlugin.pInterface);
1337 free(pPlugin);
1338 return CHANNEL_RC_OK;
1339}
1340
1349WINPR_ATTR_NODISCARD
1350FREERDP_ENTRY_POINT(UINT VCAPITYPE video_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1351{
1352 UINT error = ERROR_INTERNAL_ERROR;
1353
1354 VIDEO_PLUGIN* videoPlugin = (VIDEO_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints, "video");
1355 if (!videoPlugin)
1356 {
1357 videoPlugin = (VIDEO_PLUGIN*)calloc(1, sizeof(VIDEO_PLUGIN));
1358 if (!videoPlugin)
1359 {
1360 WLog_ERR(TAG, "calloc failed!");
1361 return CHANNEL_RC_NO_MEMORY;
1362 }
1363
1364 videoPlugin->wtsPlugin.Initialize = video_plugin_initialize;
1365 videoPlugin->wtsPlugin.Connected = nullptr;
1366 videoPlugin->wtsPlugin.Disconnected = nullptr;
1367 videoPlugin->wtsPlugin.Terminated = video_plugin_terminated;
1368
1369 VideoClientContext* videoContext =
1370 (VideoClientContext*)calloc(1, sizeof(VideoClientContext));
1371 if (!videoContext)
1372 {
1373 WLog_ERR(TAG, "calloc failed!");
1374 free(videoPlugin);
1375 return CHANNEL_RC_NO_MEMORY;
1376 }
1377
1378 VideoClientContextPriv* priv = VideoClientContextPriv_new(videoContext);
1379 if (!priv)
1380 {
1381 WLog_ERR(TAG, "VideoClientContextPriv_new failed!");
1382 free(videoContext);
1383 free(videoPlugin);
1384 return CHANNEL_RC_NO_MEMORY;
1385 }
1386
1387 videoContext->handle = (void*)videoPlugin;
1388 videoContext->priv = priv;
1389 videoContext->timer = video_timer;
1390 videoContext->setGeometry = video_client_context_set_geometry;
1391
1392 videoPlugin->wtsPlugin.pInterface = (void*)videoContext;
1393 videoPlugin->context = videoContext;
1394 videoPlugin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1395 if (videoPlugin->rdpcontext)
1396 error = pEntryPoints->RegisterPlugin(pEntryPoints, "video", &videoPlugin->wtsPlugin);
1397 }
1398 else
1399 {
1400 WLog_ERR(TAG, "could not get video Plugin.");
1401 return CHANNEL_RC_BAD_CHANNEL;
1402 }
1403
1404 return error;
1405}
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
a client to server notification struct
presentation request struct
response to a TSMM_PRESENTATION_REQUEST
a video data packet
an implementation of surface used by the video channel