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(
246 ret->h264, H264_CONTEXT_OPTION_HW_ACCEL,
247 (UINT32)freerdp_settings_get_bool(plugin->rdpcontext->settings, FreeRDP_SoftwareGdi)))
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 =
440 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelLeft + 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 =
453 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelTop + 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 PresentationContext* presentation = priv->currentPresentation;
877 if (!presentation)
878 {
879 WLog_ERR(TAG, "no current presentation");
880 return CHANNEL_RC_OK;
881 }
882
883 if (!PresentationContext_ref(presentation))
884 return ERROR_INTERNAL_ERROR;
885
886 EnterCriticalSection(&priv->framesLock);
887 if (presentation->PresentationId != data->PresentationId)
888 {
889 WLog_ERR(TAG, "current presentation id=%" PRIu8 " doesn't match data id=%" PRIu8,
890 presentation->PresentationId, data->PresentationId);
891 goto out;
892 }
893
894 if (!Stream_EnsureRemainingCapacity(presentation->currentSample, data->cbSample))
895 {
896 WLog_ERR(TAG, "unable to expand the current packet");
897 res = CHANNEL_RC_NO_MEMORY;
898 goto out;
899 }
900
901 Stream_Write(presentation->currentSample, data->pSample, data->cbSample);
902
903 if (data->CurrentPacketIndex == data->PacketsInSample)
904 {
905 VideoSurface* surface = presentation->surface;
906 H264_CONTEXT* h264 = presentation->h264;
907 const UINT64 startTime = winpr_GetTickCount64NS();
908 MAPPED_GEOMETRY* geom = presentation->geometry;
909
910 const RECTANGLE_16 rect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedWidth),
911 WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedHeight) };
912 Stream_SealLength(presentation->currentSample);
913 Stream_ResetPosition(presentation->currentSample);
914
915 if (data->SampleNumber == 1)
916 {
917 presentation->lastPublishTime = startTime;
918 }
919
920 presentation->lastPublishTime += 100ull * data->hnsDuration;
921 const size_t len = Stream_Length(presentation->currentSample);
922 if (len > UINT32_MAX)
923 goto out;
924
925 BOOL enqueueResult = 0;
926 VideoFrame* frame = VideoFrame_new(priv, presentation, geom);
927 if (!frame)
928 {
929 WLog_ERR(TAG, "unable to create frame");
930 res = CHANNEL_RC_NO_MEMORY;
931 goto out;
932 }
933
934 status = avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
935 frame->surfaceData, surface->format, surface->scanline,
936 surface->alignedWidth, surface->alignedHeight, &rect, 1);
937 if (status < 0)
938 {
939 VideoFrame_free(priv, frame);
940 goto out;
941 }
942
943 enqueueResult = Queue_Enqueue(priv->frames, frame);
944
945 if (!enqueueResult)
946 {
947 WLog_ERR(TAG, "unable to enqueue frame");
948 VideoFrame_free(priv, frame);
949 res = CHANNEL_RC_NO_MEMORY;
950 goto out;
951 }
952
953 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): Queue_Enqueue owns frame
954 WLog_DBG(TAG, "scheduling frame in %" PRIu64 " ns", (frame->publishTime - startTime));
955 }
956
957out:
958 PresentationContext_unref(&priv->currentPresentation);
959 LeaveCriticalSection(&priv->framesLock);
960
961 return res;
962}
963
964WINPR_ATTR_NODISCARD
965static UINT video_data_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* s)
966{
967 GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
968 UINT32 cbSize = 0;
969 UINT32 packetType = 0;
970 TSMM_VIDEO_DATA data;
971
972 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin;
973 WINPR_ASSERT(video);
974
975 VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface;
976
977 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
978 return ERROR_INVALID_DATA;
979
980 Stream_Read_UINT32(s, cbSize);
981 if (cbSize < 8)
982 {
983 WLog_ERR(TAG, "invalid cbSize %" PRIu32 ", expected >= 8", cbSize);
984 return ERROR_INVALID_DATA;
985 }
986
987 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
988 return ERROR_INVALID_DATA;
989
990 Stream_Read_UINT32(s, packetType);
991 if (packetType != TSMM_PACKET_TYPE_VIDEO_DATA)
992 {
993 WLog_ERR(TAG, "only expecting VIDEO_DATA on the data channel");
994 return ERROR_INVALID_DATA;
995 }
996
997 if (!Stream_CheckAndLogRequiredLength(TAG, s, 32))
998 return ERROR_INVALID_DATA;
999
1000 Stream_Read_UINT8(s, data.PresentationId);
1001 Stream_Read_UINT8(s, data.Version);
1002 Stream_Read_UINT8(s, data.Flags);
1003 Stream_Seek_UINT8(s); /* reserved */
1004 Stream_Read_UINT64(s, data.hnsTimestamp);
1005 Stream_Read_UINT64(s, data.hnsDuration);
1006 Stream_Read_UINT16(s, data.CurrentPacketIndex);
1007 Stream_Read_UINT16(s, data.PacketsInSample);
1008 Stream_Read_UINT32(s, data.SampleNumber);
1009 Stream_Read_UINT32(s, data.cbSample);
1010 if (!Stream_CheckAndLogRequiredLength(TAG, s, data.cbSample))
1011 return ERROR_INVALID_DATA;
1012 data.pSample = Stream_Pointer(s);
1013
1014 /*
1015 WLog_DBG(TAG, "videoData: id:%"PRIu8" version:%"PRIu8" flags:0x%"PRIx8" timestamp=%"PRIu64"
1016 duration=%"PRIu64 " curPacketIndex:%"PRIu16" packetInSample:%"PRIu16" sampleNumber:%"PRIu32"
1017 cbSample:%"PRIu32"", data.PresentationId, data.Version, data.Flags, data.hnsTimestamp,
1018 data.hnsDuration, data.CurrentPacketIndex, data.PacketsInSample, data.SampleNumber,
1019 data.cbSample);
1020 */
1021
1022 return video_VideoData(context, &data);
1023}
1024
1030WINPR_ATTR_NODISCARD
1031static UINT video_control_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1032{
1033 if (pChannelCallback)
1034 {
1035 GENERIC_CHANNEL_CALLBACK* listener_callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
1036 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1037 if (video && video->control_callback)
1038 {
1039 video->control_callback->channel_callback = nullptr;
1040 }
1041 }
1042 free(pChannelCallback);
1043 return CHANNEL_RC_OK;
1044}
1045
1046WINPR_ATTR_NODISCARD
1047static UINT video_data_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1048{
1049 if (pChannelCallback)
1050 {
1051 GENERIC_CHANNEL_CALLBACK* listener_callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback;
1052 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1053 if (video && video->data_callback)
1054 {
1055 video->data_callback->channel_callback = nullptr;
1056 }
1057 }
1058 free(pChannelCallback);
1059 return CHANNEL_RC_OK;
1060}
1061
1067// NOLINTBEGIN(readability-non-const-parameter)
1068WINPR_ATTR_NODISCARD
1069static UINT video_control_on_new_channel_connection(IWTSListenerCallback* listenerCallback,
1070 IWTSVirtualChannel* channel,
1071 WINPR_ATTR_UNUSED BYTE* Data,
1072 WINPR_ATTR_UNUSED BOOL* pbAccept,
1073 IWTSVirtualChannelCallback** ppCallback)
1074// NOLINTEND(readability-non-const-parameter)
1075{
1076 GENERIC_LISTENER_CALLBACK* listener_callback = (GENERIC_LISTENER_CALLBACK*)listenerCallback;
1077
1078 GENERIC_CHANNEL_CALLBACK* callback =
1080 if (!callback)
1081 {
1082 WLog_ERR(TAG, "calloc failed!");
1083 return CHANNEL_RC_NO_MEMORY;
1084 }
1085
1086 callback->iface.OnDataReceived = video_control_on_data_received;
1087 callback->iface.OnClose = video_control_on_close;
1088 callback->plugin = listener_callback->plugin;
1089 callback->channel_mgr = listener_callback->channel_mgr;
1090 callback->channel = channel;
1091 listener_callback->channel_callback = callback;
1092
1093 *ppCallback = &callback->iface;
1094
1095 return CHANNEL_RC_OK;
1096}
1097
1098// NOLINTBEGIN(readability-non-const-parameter)
1099WINPR_ATTR_NODISCARD
1100static UINT video_data_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1101 IWTSVirtualChannel* pChannel,
1102 WINPR_ATTR_UNUSED BYTE* Data,
1103 WINPR_ATTR_UNUSED BOOL* pbAccept,
1104 IWTSVirtualChannelCallback** ppCallback)
1105// NOLINTEND(readability-non-const-parameter)
1106{
1107 GENERIC_LISTENER_CALLBACK* listener_callback = (GENERIC_LISTENER_CALLBACK*)pListenerCallback;
1108
1109 GENERIC_CHANNEL_CALLBACK* callback =
1111 if (!callback)
1112 {
1113 WLog_ERR(TAG, "calloc failed!");
1114 return CHANNEL_RC_NO_MEMORY;
1115 }
1116
1117 callback->iface.OnDataReceived = video_data_on_data_received;
1118 callback->iface.OnClose = video_data_on_close;
1119 callback->plugin = listener_callback->plugin;
1120 callback->channel_mgr = listener_callback->channel_mgr;
1121 callback->channel = pChannel;
1122 listener_callback->channel_callback = callback;
1123
1124 *ppCallback = &callback->iface;
1125
1126 return CHANNEL_RC_OK;
1127}
1128
1129WINPR_ATTR_NODISCARD
1130static uint64_t timer_cb(WINPR_ATTR_UNUSED rdpContext* context, void* userdata,
1131 WINPR_ATTR_UNUSED FreeRDP_TimerID timerID, uint64_t timestamp,
1132 uint64_t interval)
1133{
1134 VideoClientContext* video = userdata;
1135 if (!video)
1136 return 0;
1137 if (!video->timer)
1138 return 0;
1139
1140 video->timer(video, timestamp);
1141
1142 return interval;
1143}
1144
1150WINPR_ATTR_NODISCARD
1151static UINT video_plugin_initialize(IWTSPlugin* plugin, IWTSVirtualChannelManager* channelMgr)
1152{
1153 UINT status = 0;
1154 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)plugin;
1155
1156 if (video->initialized)
1157 {
1158 WLog_ERR(TAG, "[%s] channel initialized twice, aborting", VIDEO_CONTROL_DVC_CHANNEL_NAME);
1159 return ERROR_INVALID_DATA;
1160 }
1161
1162 {
1163 GENERIC_LISTENER_CALLBACK* callback =
1165 if (!callback)
1166 {
1167 WLog_ERR(TAG, "calloc for control callback failed!");
1168 return CHANNEL_RC_NO_MEMORY;
1169 }
1170
1171 callback->iface.OnNewChannelConnection = video_control_on_new_channel_connection;
1172 callback->plugin = plugin;
1173 callback->channel_mgr = channelMgr;
1174
1175 status = channelMgr->CreateListener(channelMgr, VIDEO_CONTROL_DVC_CHANNEL_NAME, 0,
1176 &callback->iface, &(video->controlListener));
1177 video->control_callback = callback;
1178 if (status != CHANNEL_RC_OK)
1179 return status;
1180 }
1181 video->controlListener->pInterface = video->wtsPlugin.pInterface;
1182
1183 {
1184 GENERIC_LISTENER_CALLBACK* callback =
1186 if (!callback)
1187 {
1188 WLog_ERR(TAG, "calloc for data callback failed!");
1189 return CHANNEL_RC_NO_MEMORY;
1190 }
1191
1192 callback->iface.OnNewChannelConnection = video_data_on_new_channel_connection;
1193 callback->plugin = plugin;
1194 callback->channel_mgr = channelMgr;
1195
1196 status = channelMgr->CreateListener(channelMgr, VIDEO_DATA_DVC_CHANNEL_NAME, 0,
1197 &callback->iface, &(video->dataListener));
1198 video->data_callback = callback;
1199 if (status == CHANNEL_RC_OK)
1200 video->dataListener->pInterface = video->wtsPlugin.pInterface;
1201 }
1202
1203 if (status == CHANNEL_RC_OK)
1204 video->context->priv->timerID =
1205 freerdp_timer_add(video->rdpcontext, 20000000, timer_cb, video->context, true);
1206 video->initialized = video->context->priv->timerID != 0;
1207 if (!video->initialized)
1208 status = ERROR_INTERNAL_ERROR;
1209 return status;
1210}
1211
1217WINPR_ATTR_NODISCARD
1218static UINT video_plugin_terminated(IWTSPlugin* pPlugin)
1219{
1220 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)pPlugin;
1221 if (!video)
1222 return CHANNEL_RC_INVALID_INSTANCE;
1223
1224 if (video->context && video->context->priv)
1225 freerdp_timer_remove(video->rdpcontext, video->context->priv->timerID);
1226
1227 if (video->control_callback)
1228 {
1229 IWTSVirtualChannelManager* mgr = video->control_callback->channel_mgr;
1230 if (mgr)
1231 IFCALL(mgr->DestroyListener, mgr, video->controlListener);
1232 }
1233 if (video->data_callback)
1234 {
1235 IWTSVirtualChannelManager* mgr = video->data_callback->channel_mgr;
1236 if (mgr)
1237 IFCALL(mgr->DestroyListener, mgr, video->dataListener);
1238 }
1239
1240 if (video->context)
1241 VideoClientContextPriv_free(video->context->priv);
1242
1243 free(video->control_callback);
1244 free(video->data_callback);
1245 free(video->wtsPlugin.pInterface);
1246 free(pPlugin);
1247 return CHANNEL_RC_OK;
1248}
1249
1258WINPR_ATTR_NODISCARD
1259FREERDP_ENTRY_POINT(UINT VCAPITYPE video_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1260{
1261 UINT error = ERROR_INTERNAL_ERROR;
1262
1263 VIDEO_PLUGIN* videoPlugin = (VIDEO_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints, "video");
1264 if (!videoPlugin)
1265 {
1266 videoPlugin = (VIDEO_PLUGIN*)calloc(1, sizeof(VIDEO_PLUGIN));
1267 if (!videoPlugin)
1268 {
1269 WLog_ERR(TAG, "calloc failed!");
1270 return CHANNEL_RC_NO_MEMORY;
1271 }
1272
1273 videoPlugin->wtsPlugin.Initialize = video_plugin_initialize;
1274 videoPlugin->wtsPlugin.Connected = nullptr;
1275 videoPlugin->wtsPlugin.Disconnected = nullptr;
1276 videoPlugin->wtsPlugin.Terminated = video_plugin_terminated;
1277
1278 VideoClientContext* videoContext =
1279 (VideoClientContext*)calloc(1, sizeof(VideoClientContext));
1280 if (!videoContext)
1281 {
1282 WLog_ERR(TAG, "calloc failed!");
1283 free(videoPlugin);
1284 return CHANNEL_RC_NO_MEMORY;
1285 }
1286
1287 VideoClientContextPriv* priv = VideoClientContextPriv_new(videoContext);
1288 if (!priv)
1289 {
1290 WLog_ERR(TAG, "VideoClientContextPriv_new failed!");
1291 free(videoContext);
1292 free(videoPlugin);
1293 return CHANNEL_RC_NO_MEMORY;
1294 }
1295
1296 videoContext->handle = (void*)videoPlugin;
1297 videoContext->priv = priv;
1298 videoContext->timer = video_timer;
1299 videoContext->setGeometry = video_client_context_set_geometry;
1300
1301 videoPlugin->wtsPlugin.pInterface = (void*)videoContext;
1302 videoPlugin->context = videoContext;
1303 videoPlugin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1304 if (videoPlugin->rdpcontext)
1305 error = pEntryPoints->RegisterPlugin(pEntryPoints, "video", &videoPlugin->wtsPlugin);
1306 }
1307 else
1308 {
1309 WLog_ERR(TAG, "could not get video Plugin.");
1310 return CHANNEL_RC_BAD_CHANNEL;
1311 }
1312
1313 return error;
1314}
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