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