20#include <freerdp/config.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>
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>
48#define TAG CHANNELS_TAG("video.client")
50#include "video_main.h"
56 IWTSListener* controlListener;
57 IWTSListener* dataListener;
61 VideoClientContext* context;
63 rdpContext* rdpcontext;
68#define XF_VIDEO_UNLIMITED_RATE 31
73#define VIDEO_MAX_SAMPLE_SIZE (32ul * 1024ul * 1024ul)
75static const BYTE MFVideoFormat_H264[] = {
'H',
'2',
'6',
'4', 0x00, 0x00, 0x10, 0x00,
76 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 };
84 UINT64 startTimeStamp;
87 UINT64 lastPublishTime;
88 UINT64 nextPublishTime;
89 volatile LONG refCounter;
92 MAPPED_GEOMETRY* geometry;
93 VideoClientContext* video;
101 MAPPED_GEOMETRY* geometry;
108struct s_VideoClientContextPriv
110 VideoClientContext* video;
111 GeometryClientContext* geometry;
114 wBufferPool* surfacePool;
115 UINT32 publishedFrames;
116 UINT32 droppedFrames;
118 UINT64 nextFeedbackTime;
119 PresentationContext* currentPresentation;
120 FreeRDP_TimerID timerID;
123static void PresentationContext_unref(PresentationContext** presentation);
124static void VideoClientContextPriv_free(VideoClientContextPriv* priv);
127static const char* video_command_name(BYTE cmd)
131 case TSMM_START_PRESENTATION:
133 case TSMM_STOP_PRESENTATION:
140static void video_client_context_set_geometry(VideoClientContext* video,
141 GeometryClientContext* geometry)
144 WINPR_ASSERT(video->priv);
146 video->priv->geometry = geometry;
149WINPR_ATTR_MALLOC(VideoClientContextPriv_free, 1)
150static VideoClientContextPriv* VideoClientContextPriv_new(VideoClientContext* video)
153 VideoClientContextPriv* ret = calloc(1,
sizeof(*ret));
157 ret->frames = Queue_New(TRUE, 10, 2);
160 WLog_ERR(TAG,
"unable to allocate frames queue");
164 ret->surfacePool = BufferPool_New(FALSE, 0, 16);
165 if (!ret->surfacePool)
167 WLog_ERR(TAG,
"unable to create surface pool");
171 if (!InitializeCriticalSectionAndSpinCount(&ret->framesLock, 4 * 1000))
173 WLog_ERR(TAG,
"unable to initialize frames lock");
182 ret->lastSentRate = 30;
186 VideoClientContextPriv_free(ret);
191static BOOL PresentationContext_ref(PresentationContext* presentation)
193 WINPR_ASSERT(presentation);
195 const LONG val = InterlockedIncrement(&presentation->refCounter);
199static void PresentationContext_free(PresentationContext* presentation)
204 MAPPED_GEOMETRY* geometry = presentation->geometry;
207 geometry->MappedGeometryUpdate =
nullptr;
208 geometry->MappedGeometryClear =
nullptr;
209 geometry->custom =
nullptr;
210 mappedGeometryUnref(geometry);
213 h264_context_free(presentation->h264);
214 Stream_Free(presentation->currentSample, TRUE);
215 presentation->video->deleteSurface(presentation->video, presentation->surface);
219WINPR_ATTR_MALLOC(PresentationContext_free, 1)
220static PresentationContext* PresentationContext_new(VideoClientContext* video, BYTE PresentationId,
221 UINT32 x, UINT32 y, UINT32 width, UINT32 height)
223 if ((width == 0) || (height == 0))
225 WLog_ERR(TAG,
"width==%" PRIu32
", height=%" PRIu32, width, height);
229 if (width > SIZE_MAX / height / 4)
232 const size_t s = 4ULL * width * height;
239 PresentationContext* ret = calloc(1,
sizeof(*ret));
244 ret->PresentationId = PresentationId;
246 ret->h264 = h264_context_new(FALSE);
249 WLog_ERR(TAG,
"unable to create a h264 context");
253 VIDEO_PLUGIN* plugin = (VIDEO_PLUGIN*)video->handle;
254 WINPR_ASSERT(plugin);
255 WINPR_ASSERT(plugin->rdpcontext);
256 if (!h264_context_set_option(ret->h264, H264_CONTEXT_OPTION_HW_ACCEL,
258 FreeRDP_SoftwareGdi) == 0))
260 if (!h264_context_reset(ret->h264, width, height))
263 ret->currentSample = Stream_New(
nullptr, 4096);
264 if (!ret->currentSample)
266 WLog_ERR(TAG,
"unable to create current packet stream");
270 ret->surface = video->createSurface(video, x, y, width, height);
273 WLog_ERR(TAG,
"unable to create surface");
277 if (!PresentationContext_ref(ret))
283 PresentationContext_free(ret);
287static void PresentationContext_unref(PresentationContext** ppresentation)
289 WINPR_ASSERT(ppresentation);
291 PresentationContext* presentation = *ppresentation;
295 if (InterlockedDecrement(&presentation->refCounter) > 0)
297 *ppresentation =
nullptr;
299 PresentationContext_free(presentation);
302static void VideoFrame_free(VideoClientContextPriv* priv, VideoFrame* frame)
308 mappedGeometryUnref(frame->geometry);
310 BufferPool_Return(priv->surfacePool, frame->surfaceData);
314WINPR_ATTR_MALLOC(VideoFrame_free, 1)
315static VideoFrame* VideoFrame_new(VideoClientContextPriv* priv, PresentationContext* presentation,
316 MAPPED_GEOMETRY* geom)
319 WINPR_ASSERT(presentation);
323 WINPR_ASSERT(surface);
325 VideoFrame* frame = calloc(1,
sizeof(VideoFrame));
328 frame->PresentationId = presentation->PresentationId;
330 mappedGeometryRef(geom);
332 frame->publishTime = presentation->lastPublishTime;
333 frame->geometry = geom;
334 frame->w = surface->alignedWidth;
335 frame->h = surface->alignedHeight;
336 frame->scanline = surface->scanline;
338 frame->surfaceData = BufferPool_Take(priv->surfacePool, 1ll * frame->scanline * frame->h);
339 if (!frame->surfaceData)
345 VideoFrame_free(priv, frame);
349void VideoClientContextPriv_free(VideoClientContextPriv* priv)
354 EnterCriticalSection(&priv->framesLock);
358 while (Queue_Count(priv->frames))
360 VideoFrame* frame = Queue_Dequeue(priv->frames);
362 VideoFrame_free(priv, frame);
366 Queue_Free(priv->frames);
367 LeaveCriticalSection(&priv->framesLock);
369 DeleteCriticalSection(&priv->framesLock);
371 if (priv->currentPresentation)
372 PresentationContext_unref(&priv->currentPresentation);
374 BufferPool_Free(priv->surfacePool);
379static UINT video_channel_write(VIDEO_PLUGIN* video,
const BYTE* data, UINT32 length)
381 UINT rc = ERROR_BAD_CONFIGURATION;
384 WINPR_ASSERT(video->context);
386 VideoClientContextPriv* priv = video->context->priv;
391 EnterCriticalSection(&priv->framesLock);
392 if (video->control_callback && video->control_callback->channel_callback)
394 IWTSVirtualChannel* channel = video->control_callback->channel_callback->channel;
395 if (channel && channel->Write)
396 rc = channel->Write(channel, length, data,
nullptr);
398 LeaveCriticalSection(&priv->framesLock);
403static UINT video_control_send_presentation_response(VideoClientContext* context,
406 BYTE buf[12] = WINPR_C_ARRAY_INIT;
408 WINPR_ASSERT(context);
411 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)context->handle;
414 wStream* s = Stream_New(buf, 12);
416 return CHANNEL_RC_NO_MEMORY;
418 Stream_Write_UINT32(s, 12);
419 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_PRESENTATION_RESPONSE);
420 Stream_Write_UINT8(s, resp->PresentationId);
422 Stream_SealLength(s);
423 Stream_Free(s, FALSE);
425 return video_channel_write(video, buf,
sizeof(buf));
429static BOOL video_onMappedGeometryUpdate(MAPPED_GEOMETRY* geometry)
431 WINPR_ASSERT(geometry);
433 PresentationContext* presentation = (PresentationContext*)geometry->custom;
434 WINPR_ASSERT(presentation);
436 RDP_RECT* r = &geometry->geometry.boundingRect;
438 "geometry updated topGeom=(%" PRId32
",%" PRId32
"-%" PRId32
"x%" PRId32
439 ") geom=(%" PRId32
",%" PRId32
"-%" PRId32
"x%" PRId32
") rects=(%" PRId16
",%" PRId16
440 "-%" PRId16
"x%" PRId16
")",
441 geometry->topLevelLeft, geometry->topLevelTop,
442 geometry->topLevelRight - geometry->topLevelLeft,
443 geometry->topLevelBottom - geometry->topLevelTop,
445 geometry->left, geometry->top, geometry->right - geometry->left,
446 geometry->bottom - geometry->top,
448 r->x, r->y, r->width, r->height);
450 WINPR_ASSERT(presentation->surface);
451 if (geometry->topLevelLeft < 0)
453 WLog_ERR(TAG,
"geometry->topLevelLeft=%d < 0", geometry->topLevelLeft);
456 if (geometry->left < 0)
458 WLog_ERR(TAG,
"geometry->left=%d < 0", geometry->left);
461 presentation->surface->x = WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelLeft) +
462 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->left);
464 if (geometry->topLevelTop < 0)
466 WLog_ERR(TAG,
"geometry->topLevelTop=%d < 0", geometry->topLevelTop);
469 if (geometry->top < 0)
471 WLog_ERR(TAG,
"geometry->top=%d < 0", geometry->top);
474 presentation->surface->y = WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelTop) +
475 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->top);
481static BOOL video_onMappedGeometryClear(MAPPED_GEOMETRY* geometry)
483 WINPR_ASSERT(geometry);
485 PresentationContext* presentation = (PresentationContext*)geometry->custom;
486 WINPR_ASSERT(presentation);
488 mappedGeometryUnref(presentation->geometry);
489 presentation->geometry =
nullptr;
494static UINT video_PresentationRequest(VideoClientContext* video,
497 UINT ret = CHANNEL_RC_OK;
502 VideoClientContextPriv* priv = video->priv;
505 EnterCriticalSection(&priv->framesLock);
506 if (req->Command == TSMM_START_PRESENTATION)
508 MAPPED_GEOMETRY* geom =
nullptr;
511 if (memcmp(req->VideoSubtypeId, MFVideoFormat_H264, 16) != 0)
513 WLog_ERR(TAG,
"not a H264 video, ignoring request");
517 if (priv->currentPresentation)
519 if (priv->currentPresentation->PresentationId == req->PresentationId)
521 WLog_ERR(TAG,
"ignoring start request for existing presentation %" PRIu8,
522 req->PresentationId);
526 WLog_ERR(TAG,
"releasing current presentation %" PRIu8, req->PresentationId);
527 PresentationContext_unref(&priv->currentPresentation);
532 WLog_ERR(TAG,
"geometry channel not ready, ignoring request");
536 geom = HashTable_GetItemValue(priv->geometry->geometries, &(req->GeometryMappingId));
539 WLog_ERR(TAG,
"geometry mapping 0x%" PRIx64
" not registered", req->GeometryMappingId);
543 WLog_DBG(TAG,
"creating presentation 0x%x", req->PresentationId);
544 if ((geom->topLevelLeft < 0) || (geom->left < 0) || (geom->topLevelTop < 0) ||
548 "geometry: topLevelLeft=%" PRId32
" < 0, left=%" PRId32
549 " < 0, topLevelTop=%" PRId32
" < 0, top=%" PRId32
" < 0",
550 geom->topLevelLeft, geom->left, geom->topLevelTop, geom->top);
554 priv->currentPresentation = PresentationContext_new(
555 video, req->PresentationId,
556 WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelLeft + geom->left),
557 WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelTop + geom->top), req->SourceWidth,
559 if (!priv->currentPresentation)
561 WLog_ERR(TAG,
"unable to create presentation video");
562 ret = CHANNEL_RC_NO_MEMORY;
566 mappedGeometryRef(geom);
567 priv->currentPresentation->geometry = geom;
569 priv->currentPresentation->video = video;
570 priv->currentPresentation->ScaledWidth = req->ScaledWidth;
571 priv->currentPresentation->ScaledHeight = req->ScaledHeight;
573 geom->custom = priv->currentPresentation;
574 geom->MappedGeometryUpdate = video_onMappedGeometryUpdate;
575 geom->MappedGeometryClear = video_onMappedGeometryClear;
578 resp.PresentationId = req->PresentationId;
579 ret = video_control_send_presentation_response(video, &resp);
581 else if (req->Command == TSMM_STOP_PRESENTATION)
583 WLog_DBG(TAG,
"stopping presentation 0x%x", req->PresentationId);
584 if (!priv->currentPresentation)
586 WLog_ERR(TAG,
"unknown presentation to stop %" PRIu8, req->PresentationId);
590 priv->droppedFrames = 0;
591 priv->publishedFrames = 0;
592 PresentationContext_unref(&priv->currentPresentation);
596 LeaveCriticalSection(&priv->framesLock);
601static UINT video_read_tsmm_presentation_req(VideoClientContext* context,
wStream* s)
605 WINPR_ASSERT(context);
608 if (!Stream_CheckAndLogRequiredLength(TAG, s, 60))
609 return ERROR_INVALID_DATA;
611 Stream_Read_UINT8(s, req.PresentationId);
612 Stream_Read_UINT8(s, req.Version);
613 Stream_Read_UINT8(s, req.Command);
614 Stream_Read_UINT8(s, req.FrameRate);
616 Stream_Seek_UINT16(s);
617 Stream_Seek_UINT16(s);
619 Stream_Read_UINT32(s, req.SourceWidth);
620 Stream_Read_UINT32(s, req.SourceHeight);
621 Stream_Read_UINT32(s, req.ScaledWidth);
622 Stream_Read_UINT32(s, req.ScaledHeight);
623 if ((req.ScaledWidth == 0) || (req.SourceHeight == 0) || (req.ScaledWidth == 0) ||
624 (req.ScaledHeight == 0))
627 "SourceWidth=%" PRIu32
", SourceHeight=%" PRIu32
", ScaledWidth=%" PRIu32
628 ", ScaledHeight=%" PRIu32,
629 req.SourceWidth, req.SourceHeight, req.ScaledWidth, req.ScaledHeight);
630 return ERROR_INVALID_DATA;
632 Stream_Read_UINT64(s, req.hnsTimestampOffset);
633 Stream_Read_UINT64(s, req.GeometryMappingId);
634 Stream_Read(s, req.VideoSubtypeId, 16);
636 Stream_Read_UINT32(s, req.cbExtra);
638 if (!Stream_CheckAndLogRequiredLength(TAG, s, req.cbExtra))
639 return ERROR_INVALID_DATA;
641 req.pExtraData = Stream_Pointer(s);
644 "presentationReq: id:%" PRIu8
" version:%" PRIu8
645 " command:%s srcWidth/srcHeight=%" PRIu32
"x%" PRIu32
" scaled Width/Height=%" PRIu32
646 "x%" PRIu32
" timestamp=%" PRIu64
" mappingId=%" PRIx64
"",
647 req.PresentationId, req.Version, video_command_name(req.Command), req.SourceWidth,
648 req.SourceHeight, req.ScaledWidth, req.ScaledHeight, req.hnsTimestampOffset,
649 req.GeometryMappingId);
651 return video_PresentationRequest(context, &req);
660static UINT video_control_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
663 UINT ret = CHANNEL_RC_OK;
665 UINT32 packetType = 0;
667 WINPR_ASSERT(callback);
670 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin;
673 VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface;
674 WINPR_ASSERT(context);
676 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
677 return ERROR_INVALID_DATA;
679 Stream_Read_UINT32(s, cbSize);
682 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected 8", cbSize);
683 return ERROR_INVALID_DATA;
685 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
686 return ERROR_INVALID_DATA;
688 Stream_Read_UINT32(s, packetType);
691 case TSMM_PACKET_TYPE_PRESENTATION_REQUEST:
692 ret = video_read_tsmm_presentation_req(context, s);
695 WLog_ERR(TAG,
"not expecting packet type %" PRIu32
"", packetType);
696 ret = ERROR_UNSUPPORTED_TYPE;
703static UINT video_control_send_client_notification(VideoClientContext* context,
706 BYTE buf[100] = WINPR_C_ARRAY_INIT;
708 WINPR_ASSERT(context);
711 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)context->handle;
714 wStream* s = Stream_New(buf, 32);
716 return CHANNEL_RC_NO_MEMORY;
719 Stream_Seek_UINT32(s);
720 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_CLIENT_NOTIFICATION);
721 Stream_Write_UINT8(s, notif->PresentationId);
722 Stream_Write_UINT8(s, notif->NotificationType);
724 if (notif->NotificationType == TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE)
726 Stream_Write_UINT32(s, 16);
729 Stream_Write_UINT32(s, notif->FramerateOverride.Flags);
730 Stream_Write_UINT32(s, notif->FramerateOverride.DesiredFrameRate);
731 Stream_Zero(s, 4ULL * 2ULL);
737 Stream_Write_UINT32(s, 0);
740 Stream_SealLength(s);
741 Stream_ResetPosition(s);
742 Stream_Write_UINT32(s, cbSize);
743 Stream_Free(s, FALSE);
745 return video_channel_write(video, buf, cbSize);
748static void video_timer(VideoClientContext* video, UINT64 now)
750 VideoFrame* frame =
nullptr;
754 VideoClientContextPriv* priv = video->priv;
757 EnterCriticalSection(&priv->framesLock);
758 PresentationContext* presentation = video->priv->currentPresentation;
761 const VideoFrame* peekFrame = (VideoFrame*)Queue_Peek(priv->frames);
765 if (peekFrame->publishTime > now)
770 WLog_DBG(TAG,
"dropping frame @%" PRIu64, frame->publishTime);
771 priv->droppedFrames++;
772 VideoFrame_free(priv, frame);
774 frame = Queue_Dequeue(priv->frames);
779 if (presentation && (presentation->PresentationId == frame->PresentationId))
782 const size_t frameSize = 1ull * frame->scanline * frame->h;
783 const size_t surfaceSize = 1ull * surface->scanline * surface->alignedHeight;
788 if (frameSize > surfaceSize)
789 WLog_WARN(TAG,
"dropping stale frame of %" PRIuz
" bytes, surface holds %" PRIuz,
790 frameSize, surfaceSize);
793 priv->publishedFrames++;
794 memcpy(surface->data, frame->surfaceData, frameSize);
796 WINPR_ASSERT(video->showSurface);
797 if (!video->showSurface(video, surface, presentation->ScaledWidth,
798 presentation->ScaledHeight))
799 WLog_WARN(TAG,
"showSurface failed");
802 VideoFrame_free(priv, frame);
805 if (priv->nextFeedbackTime < now)
810 if (priv->publishedFrames && priv->currentPresentation)
812 UINT32 computedRate = 0;
814 if (!PresentationContext_ref(priv->currentPresentation))
815 WLog_WARN(TAG,
"PresentationContext_ref(priv->currentPresentation) failed");
817 if (priv->droppedFrames)
824 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
828 computedRate = priv->lastSentRate - 2;
839 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
840 computedRate = XF_VIDEO_UNLIMITED_RATE;
843 computedRate = priv->lastSentRate + 2;
844 if (computedRate > XF_VIDEO_UNLIMITED_RATE)
845 computedRate = XF_VIDEO_UNLIMITED_RATE;
849 if (computedRate != priv->lastSentRate)
853 WINPR_ASSERT(priv->currentPresentation);
854 notif.PresentationId = priv->currentPresentation->PresentationId;
855 notif.NotificationType = TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE;
856 if (computedRate == XF_VIDEO_UNLIMITED_RATE)
858 notif.FramerateOverride.Flags = 0x01;
859 notif.FramerateOverride.DesiredFrameRate = 0x00;
863 notif.FramerateOverride.Flags = 0x02;
864 notif.FramerateOverride.DesiredFrameRate = computedRate;
867 video_control_send_client_notification(video, ¬if);
868 priv->lastSentRate = computedRate;
871 "server notified with rate %" PRIu32
" published=%" PRIu32
873 priv->lastSentRate, priv->publishedFrames, priv->droppedFrames);
876 PresentationContext_unref(&priv->currentPresentation);
879 priv->droppedFrames = 0;
880 priv->publishedFrames = 0;
881 priv->nextFeedbackTime = now + 1000;
883 LeaveCriticalSection(&priv->framesLock);
887static UINT video_VideoData(VideoClientContext* context,
const TSMM_VIDEO_DATA* data)
890 UINT res = CHANNEL_RC_OK;
892 WINPR_ASSERT(context);
895 VideoClientContextPriv* priv = context->priv;
898 EnterCriticalSection(&priv->framesLock);
899 PresentationContext* presentation = priv->currentPresentation;
902 WLog_ERR(TAG,
"no current presentation");
903 LeaveCriticalSection(&priv->framesLock);
904 return CHANNEL_RC_OK;
907 if (!PresentationContext_ref(presentation))
909 LeaveCriticalSection(&priv->framesLock);
910 return ERROR_INTERNAL_ERROR;
913 if (presentation->PresentationId != data->PresentationId)
915 WLog_ERR(TAG,
"current presentation id=%" PRIu8
" doesn't match data id=%" PRIu8,
916 presentation->PresentationId, data->PresentationId);
920 const size_t collected = Stream_GetPosition(presentation->currentSample);
921 if ((collected > VIDEO_MAX_SAMPLE_SIZE) || (data->cbSample > VIDEO_MAX_SAMPLE_SIZE - collected))
923 WLog_ERR(TAG,
"sample %" PRIu32
" too large: %" PRIuz
" + %" PRIu32
" bytes exceed %lu",
924 data->SampleNumber, collected, data->cbSample, VIDEO_MAX_SAMPLE_SIZE);
925 Stream_ResetPosition(presentation->currentSample);
926 res = ERROR_INVALID_DATA;
930 if (!Stream_EnsureRemainingCapacity(presentation->currentSample, data->cbSample))
932 WLog_ERR(TAG,
"unable to expand the current packet");
933 res = CHANNEL_RC_NO_MEMORY;
937 Stream_Write(presentation->currentSample, data->pSample, data->cbSample);
939 if (data->CurrentPacketIndex == data->PacketsInSample)
942 H264_CONTEXT* h264 = presentation->h264;
943 const UINT64 startTime = winpr_GetTickCount64NS();
944 MAPPED_GEOMETRY* geom = presentation->geometry;
948 Stream_SealLength(presentation->currentSample);
949 Stream_ResetPosition(presentation->currentSample);
954 const RECTANGLE_16 rect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedWidth),
955 WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedHeight) };
957 if (data->SampleNumber == 1)
959 presentation->lastPublishTime = startTime;
962 presentation->lastPublishTime += 100ull * data->hnsDuration;
963 const size_t len = Stream_Length(presentation->currentSample);
964 if (len > UINT32_MAX)
967 BOOL enqueueResult = 0;
968 VideoFrame* frame = VideoFrame_new(priv, presentation, geom);
971 WLog_ERR(TAG,
"unable to create frame");
972 res = CHANNEL_RC_NO_MEMORY;
976 status = avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
977 frame->surfaceData, surface->format, surface->scanline,
978 surface->alignedWidth, surface->alignedHeight, &rect, 1);
981 VideoFrame_free(priv, frame);
985 enqueueResult = Queue_Enqueue(priv->frames, frame);
989 WLog_ERR(TAG,
"unable to enqueue frame");
990 VideoFrame_free(priv, frame);
991 res = CHANNEL_RC_NO_MEMORY;
996 WLog_DBG(TAG,
"scheduling frame in %" PRIu64
" ns", (frame->publishTime - startTime));
1000 PresentationContext_unref(&priv->currentPresentation);
1001 LeaveCriticalSection(&priv->framesLock);
1007static UINT video_data_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
1011 UINT32 packetType = 0;
1014 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin;
1015 WINPR_ASSERT(video);
1017 VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface;
1019 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1020 return ERROR_INVALID_DATA;
1022 Stream_Read_UINT32(s, cbSize);
1025 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected >= 8", cbSize);
1026 return ERROR_INVALID_DATA;
1029 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
1030 return ERROR_INVALID_DATA;
1032 Stream_Read_UINT32(s, packetType);
1033 if (packetType != TSMM_PACKET_TYPE_VIDEO_DATA)
1035 WLog_ERR(TAG,
"only expecting VIDEO_DATA on the data channel");
1036 return ERROR_INVALID_DATA;
1039 if (!Stream_CheckAndLogRequiredLength(TAG, s, 32))
1040 return ERROR_INVALID_DATA;
1042 Stream_Read_UINT8(s, data.PresentationId);
1043 Stream_Read_UINT8(s, data.Version);
1044 Stream_Read_UINT8(s, data.Flags);
1045 Stream_Seek_UINT8(s);
1046 Stream_Read_UINT64(s, data.hnsTimestamp);
1047 Stream_Read_UINT64(s, data.hnsDuration);
1048 Stream_Read_UINT16(s, data.CurrentPacketIndex);
1049 Stream_Read_UINT16(s, data.PacketsInSample);
1050 Stream_Read_UINT32(s, data.SampleNumber);
1051 Stream_Read_UINT32(s, data.cbSample);
1052 if (!Stream_CheckAndLogRequiredLength(TAG, s, data.cbSample))
1053 return ERROR_INVALID_DATA;
1054 data.pSample = Stream_Pointer(s);
1064 return video_VideoData(context, &data);
1073static UINT video_control_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1075 if (pChannelCallback)
1078 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1081 WINPR_ASSERT(video->context);
1083 VideoClientContextPriv* priv = video->context->priv;
1088 EnterCriticalSection(&priv->framesLock);
1089 if (video->control_callback)
1090 video->control_callback->channel_callback =
nullptr;
1091 video->connected = FALSE;
1092 LeaveCriticalSection(&priv->framesLock);
1095 free(pChannelCallback);
1096 return CHANNEL_RC_OK;
1100static UINT video_data_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1102 if (pChannelCallback)
1105 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1106 if (video && video->data_callback)
1108 video->data_callback->channel_callback =
nullptr;
1112 video->dataConnected = FALSE;
1114 free(pChannelCallback);
1115 return CHANNEL_RC_OK;
1125static UINT video_control_on_new_channel_connection(IWTSListenerCallback* listenerCallback,
1126 IWTSVirtualChannel* channel,
1127 WINPR_ATTR_UNUSED BYTE* Data,
1128 WINPR_ATTR_UNUSED BOOL* pbAccept,
1129 IWTSVirtualChannelCallback** ppCallback)
1133 WINPR_ASSERT(listener_callback);
1135 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1136 WINPR_ASSERT(video);
1138 if (video->connected)
1140 WLog_ERR(TAG,
"Channel already connected, terminating.");
1141 return ERROR_DEVICE_ALREADY_ATTACHED;
1148 WLog_ERR(TAG,
"calloc failed!");
1149 return CHANNEL_RC_NO_MEMORY;
1152 callback->iface.OnDataReceived = video_control_on_data_received;
1153 callback->iface.OnClose = video_control_on_close;
1154 callback->plugin = listener_callback->plugin;
1155 callback->channel_mgr = listener_callback->channel_mgr;
1156 callback->channel = channel;
1158 WINPR_ASSERT(video->context);
1159 WINPR_ASSERT(video->context->priv);
1160 EnterCriticalSection(&video->context->priv->framesLock);
1161 listener_callback->channel_callback = callback;
1162 LeaveCriticalSection(&video->context->priv->framesLock);
1164 *ppCallback = &callback->iface;
1165 video->connected = TRUE;
1167 return CHANNEL_RC_OK;
1172static UINT video_data_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1173 IWTSVirtualChannel* pChannel,
1174 WINPR_ATTR_UNUSED BYTE* Data,
1175 WINPR_ATTR_UNUSED BOOL* pbAccept,
1176 IWTSVirtualChannelCallback** ppCallback)
1180 WINPR_ASSERT(listener_callback);
1182 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1183 WINPR_ASSERT(video);
1185 if (video->dataConnected)
1187 WLog_ERR(TAG,
"Channel already connected, terminating.");
1188 return ERROR_DEVICE_ALREADY_ATTACHED;
1195 WLog_ERR(TAG,
"calloc failed!");
1196 return CHANNEL_RC_NO_MEMORY;
1199 callback->iface.OnDataReceived = video_data_on_data_received;
1200 callback->iface.OnClose = video_data_on_close;
1201 callback->plugin = listener_callback->plugin;
1202 callback->channel_mgr = listener_callback->channel_mgr;
1203 callback->channel = pChannel;
1204 listener_callback->channel_callback = callback;
1206 *ppCallback = &callback->iface;
1207 video->dataConnected = TRUE;
1209 return CHANNEL_RC_OK;
1213static uint64_t timer_cb(WINPR_ATTR_UNUSED rdpContext* context,
void* userdata,
1214 WINPR_ATTR_UNUSED FreeRDP_TimerID timerID, uint64_t timestamp,
1217 VideoClientContext* video = userdata;
1223 video->timer(video, timestamp);
1234static UINT video_plugin_initialize(IWTSPlugin* plugin, IWTSVirtualChannelManager* channelMgr)
1237 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)plugin;
1239 if (video->initialized)
1241 WLog_ERR(TAG,
"[%s] channel initialized twice, aborting", VIDEO_CONTROL_DVC_CHANNEL_NAME);
1242 return ERROR_INVALID_DATA;
1250 WLog_ERR(TAG,
"calloc for control callback failed!");
1251 return CHANNEL_RC_NO_MEMORY;
1254 callback->iface.OnNewChannelConnection = video_control_on_new_channel_connection;
1255 callback->plugin = plugin;
1256 callback->channel_mgr = channelMgr;
1258 status = channelMgr->CreateListener(channelMgr, VIDEO_CONTROL_DVC_CHANNEL_NAME, 0,
1259 &callback->iface, &(video->controlListener));
1260 video->control_callback = callback;
1261 if (status != CHANNEL_RC_OK)
1264 video->controlListener->pInterface = video->wtsPlugin.pInterface;
1271 WLog_ERR(TAG,
"calloc for data callback failed!");
1272 return CHANNEL_RC_NO_MEMORY;
1275 callback->iface.OnNewChannelConnection = video_data_on_new_channel_connection;
1276 callback->plugin = plugin;
1277 callback->channel_mgr = channelMgr;
1279 status = channelMgr->CreateListener(channelMgr, VIDEO_DATA_DVC_CHANNEL_NAME, 0,
1280 &callback->iface, &(video->dataListener));
1281 video->data_callback = callback;
1282 if (status == CHANNEL_RC_OK)
1283 video->dataListener->pInterface = video->wtsPlugin.pInterface;
1286 if (status == CHANNEL_RC_OK)
1287 video->context->priv->timerID =
1288 freerdp_timer_add(video->rdpcontext, 20000000, timer_cb, video->context,
true);
1289 video->initialized = video->context->priv->timerID != 0;
1290 if (!video->initialized)
1291 status = ERROR_INTERNAL_ERROR;
1301static UINT video_plugin_terminated(IWTSPlugin* pPlugin)
1303 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)pPlugin;
1305 return CHANNEL_RC_INVALID_INSTANCE;
1307 if (video->context && video->context->priv)
1308 freerdp_timer_remove(video->rdpcontext, video->context->priv->timerID);
1310 if (video->control_callback)
1312 IWTSVirtualChannelManager* mgr = video->control_callback->channel_mgr;
1314 IFCALL(mgr->DestroyListener, mgr, video->controlListener);
1316 if (video->data_callback)
1318 IWTSVirtualChannelManager* mgr = video->data_callback->channel_mgr;
1320 IFCALL(mgr->DestroyListener, mgr, video->dataListener);
1324 VideoClientContextPriv_free(video->context->priv);
1326 free(video->control_callback);
1327 free(video->data_callback);
1328 free(video->wtsPlugin.pInterface);
1330 return CHANNEL_RC_OK;
1342FREERDP_ENTRY_POINT(UINT VCAPITYPE video_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1344 UINT error = ERROR_INTERNAL_ERROR;
1346 VIDEO_PLUGIN* videoPlugin = (VIDEO_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints,
"video");
1349 videoPlugin = (VIDEO_PLUGIN*)calloc(1,
sizeof(VIDEO_PLUGIN));
1352 WLog_ERR(TAG,
"calloc failed!");
1353 return CHANNEL_RC_NO_MEMORY;
1356 videoPlugin->wtsPlugin.Initialize = video_plugin_initialize;
1357 videoPlugin->wtsPlugin.Connected =
nullptr;
1358 videoPlugin->wtsPlugin.Disconnected =
nullptr;
1359 videoPlugin->wtsPlugin.Terminated = video_plugin_terminated;
1361 VideoClientContext* videoContext =
1362 (VideoClientContext*)calloc(1,
sizeof(VideoClientContext));
1365 WLog_ERR(TAG,
"calloc failed!");
1367 return CHANNEL_RC_NO_MEMORY;
1370 VideoClientContextPriv* priv = VideoClientContextPriv_new(videoContext);
1373 WLog_ERR(TAG,
"VideoClientContextPriv_new failed!");
1376 return CHANNEL_RC_NO_MEMORY;
1379 videoContext->handle = (
void*)videoPlugin;
1380 videoContext->priv = priv;
1381 videoContext->timer = video_timer;
1382 videoContext->setGeometry = video_client_context_set_geometry;
1384 videoPlugin->wtsPlugin.pInterface = (
void*)videoContext;
1385 videoPlugin->context = videoContext;
1386 videoPlugin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1387 if (videoPlugin->rdpcontext)
1388 error = pEntryPoints->RegisterPlugin(pEntryPoints,
"video", &videoPlugin->wtsPlugin);
1392 WLog_ERR(TAG,
"could not get video Plugin.");
1393 return CHANNEL_RC_BAD_CHANNEL;
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
an implementation of surface used by the video channel