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