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