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"
53#define QUEUE_SIZE_LIMIT 64u
59 IWTSListener* controlListener;
60 IWTSListener* dataListener;
64 VideoClientContext* context;
66 rdpContext* rdpcontext;
71#define XF_VIDEO_UNLIMITED_RATE 31
76#define VIDEO_MAX_SAMPLE_SIZE (32ul * 1024ul * 1024ul)
78static const BYTE MFVideoFormat_H264[] = {
'H',
'2',
'6',
'4', 0x00, 0x00, 0x10, 0x00,
79 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 };
87 UINT64 startTimeStamp;
90 UINT64 lastPublishTime;
91 UINT64 nextPublishTime;
92 volatile LONG refCounter;
95 MAPPED_GEOMETRY* geometry;
96 VideoClientContext* video;
104 MAPPED_GEOMETRY* geometry;
111struct s_VideoClientContextPriv
113 VideoClientContext* video;
114 GeometryClientContext* geometry;
116 CRITICAL_SECTION framesLock;
117 wBufferPool* surfacePool;
118 UINT32 publishedFrames;
119 UINT32 droppedFrames;
121 UINT64 nextFeedbackTime;
122 PresentationContext* currentPresentation;
123 FreeRDP_TimerID timerID;
126static void PresentationContext_unref(PresentationContext** presentation);
127static void VideoClientContextPriv_free(VideoClientContextPriv* priv);
130static const char* video_command_name(BYTE cmd)
134 case TSMM_START_PRESENTATION:
136 case TSMM_STOP_PRESENTATION:
143static void video_client_context_set_geometry(VideoClientContext* video,
144 GeometryClientContext* geometry)
147 WINPR_ASSERT(video->priv);
149 video->priv->geometry = geometry;
152WINPR_ATTR_MALLOC(VideoClientContextPriv_free, 1)
153static VideoClientContextPriv* VideoClientContextPriv_new(VideoClientContext* video)
156 VideoClientContextPriv* ret = calloc(1,
sizeof(*ret));
160 ret->frames = Queue_New(TRUE, 10, 2);
163 WLog_ERR(TAG,
"unable to allocate frames queue");
167 ret->surfacePool = BufferPool_New(FALSE, 0, 16);
168 if (!ret->surfacePool)
170 WLog_ERR(TAG,
"unable to create surface pool");
174 if (!InitializeCriticalSectionAndSpinCount(&ret->framesLock, 4 * 1000))
176 WLog_ERR(TAG,
"unable to initialize frames lock");
185 ret->lastSentRate = 30;
189 VideoClientContextPriv_free(ret);
194static BOOL PresentationContext_ref(PresentationContext* presentation)
196 WINPR_ASSERT(presentation);
198 const LONG val = InterlockedIncrement(&presentation->refCounter);
202static void PresentationContext_free(PresentationContext* presentation)
207 MAPPED_GEOMETRY* geometry = presentation->geometry;
210 geometry->MappedGeometryUpdate =
nullptr;
211 geometry->MappedGeometryClear =
nullptr;
212 geometry->custom =
nullptr;
213 mappedGeometryUnref(geometry);
216 h264_context_free(presentation->h264);
217 Stream_Free(presentation->currentSample, TRUE);
218 presentation->video->deleteSurface(presentation->video, presentation->surface);
222WINPR_ATTR_MALLOC(PresentationContext_free, 1)
223static PresentationContext* PresentationContext_new(VideoClientContext* video, BYTE PresentationId,
224 UINT32 x, UINT32 y, UINT32 width, UINT32 height)
226 if ((width == 0) || (height == 0))
228 WLog_ERR(TAG,
"width==%" PRIu32
", height=%" PRIu32, width, height);
232 if (width > SIZE_MAX / height / 4)
235 const size_t s = 4ULL * width * height;
242 PresentationContext* ret = calloc(1,
sizeof(*ret));
247 ret->PresentationId = PresentationId;
249 ret->h264 = h264_context_new(FALSE);
252 WLog_ERR(TAG,
"unable to create a h264 context");
256 VIDEO_PLUGIN* plugin = (VIDEO_PLUGIN*)video->handle;
257 WINPR_ASSERT(plugin);
258 WINPR_ASSERT(plugin->rdpcontext);
259 if (!h264_context_set_option(ret->h264, H264_CONTEXT_OPTION_HW_ACCEL,
261 FreeRDP_SoftwareGdi) == 0))
263 if (!h264_context_reset(ret->h264, width, height))
266 ret->currentSample = Stream_New(
nullptr, 4096);
267 if (!ret->currentSample)
269 WLog_ERR(TAG,
"unable to create current packet stream");
273 ret->surface = video->createSurface(video, x, y, width, height);
276 WLog_ERR(TAG,
"unable to create surface");
280 if (!PresentationContext_ref(ret))
286 PresentationContext_free(ret);
290static void PresentationContext_unref(PresentationContext** ppresentation)
292 WINPR_ASSERT(ppresentation);
294 PresentationContext* presentation = *ppresentation;
298 if (InterlockedDecrement(&presentation->refCounter) > 0)
300 *ppresentation =
nullptr;
302 PresentationContext_free(presentation);
305static void VideoFrame_free(VideoClientContextPriv* priv, VideoFrame* frame)
311 mappedGeometryUnref(frame->geometry);
313 BufferPool_Return(priv->surfacePool, frame->surfaceData);
317WINPR_ATTR_MALLOC(VideoFrame_free, 1)
318static VideoFrame* VideoFrame_new(VideoClientContextPriv* priv, PresentationContext* presentation,
319 MAPPED_GEOMETRY* geom)
322 WINPR_ASSERT(presentation);
326 WINPR_ASSERT(surface);
328 VideoFrame* frame = calloc(1,
sizeof(VideoFrame));
331 frame->PresentationId = presentation->PresentationId;
333 mappedGeometryRef(geom);
335 frame->publishTime = presentation->lastPublishTime;
336 frame->geometry = geom;
337 frame->w = surface->alignedWidth;
338 frame->h = surface->alignedHeight;
339 frame->scanline = surface->scanline;
341 frame->surfaceData = BufferPool_Take(priv->surfacePool, 1ll * frame->scanline * frame->h);
342 if (!frame->surfaceData)
348 VideoFrame_free(priv, frame);
352static void drain_queue(VideoClientContextPriv* priv,
size_t limit)
354 while (Queue_Count(priv->frames) > limit)
356 VideoFrame* frame = Queue_Dequeue(priv->frames);
357 VideoFrame_free(priv, frame);
361void VideoClientContextPriv_free(VideoClientContextPriv* priv)
366 EnterCriticalSection(&priv->framesLock);
369 drain_queue(priv, 0);
371 Queue_Free(priv->frames);
372 LeaveCriticalSection(&priv->framesLock);
374 DeleteCriticalSection(&priv->framesLock);
376 if (priv->currentPresentation)
377 PresentationContext_unref(&priv->currentPresentation);
379 BufferPool_Free(priv->surfacePool);
384static UINT video_channel_write(VIDEO_PLUGIN* video,
const BYTE* data, UINT32 length)
386 UINT rc = ERROR_BAD_CONFIGURATION;
389 WINPR_ASSERT(video->context);
391 VideoClientContextPriv* priv = video->context->priv;
396 EnterCriticalSection(&priv->framesLock);
397 if (video->control_callback && video->control_callback->channel_callback)
399 IWTSVirtualChannel* channel = video->control_callback->channel_callback->channel;
400 if (channel && channel->Write)
401 rc = channel->Write(channel, length, data,
nullptr);
403 LeaveCriticalSection(&priv->framesLock);
408static UINT video_control_send_presentation_response(VideoClientContext* context,
411 BYTE buf[12] = WINPR_C_ARRAY_INIT;
413 WINPR_ASSERT(context);
416 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)context->handle;
419 wStream* s = Stream_New(buf, 12);
421 return CHANNEL_RC_NO_MEMORY;
423 Stream_Write_UINT32(s, 12);
424 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_PRESENTATION_RESPONSE);
425 Stream_Write_UINT8(s, resp->PresentationId);
427 Stream_SealLength(s);
428 Stream_Free(s, FALSE);
430 return video_channel_write(video, buf,
sizeof(buf));
434static BOOL video_onMappedGeometryUpdate(MAPPED_GEOMETRY* geometry)
436 WINPR_ASSERT(geometry);
438 PresentationContext* presentation = (PresentationContext*)geometry->custom;
439 WINPR_ASSERT(presentation);
441 RDP_RECT* r = &geometry->geometry.boundingRect;
443 "geometry updated topGeom=(%" PRId32
",%" PRId32
"-%" PRId32
"x%" PRId32
444 ") geom=(%" PRId32
",%" PRId32
"-%" PRId32
"x%" PRId32
") rects=(%" PRId16
",%" PRId16
445 "-%" PRId16
"x%" PRId16
")",
446 geometry->topLevelLeft, geometry->topLevelTop,
447 geometry->topLevelRight - geometry->topLevelLeft,
448 geometry->topLevelBottom - geometry->topLevelTop,
450 geometry->left, geometry->top, geometry->right - geometry->left,
451 geometry->bottom - geometry->top,
453 r->x, r->y, r->width, r->height);
455 WINPR_ASSERT(presentation->surface);
456 if (geometry->topLevelLeft < 0)
458 WLog_ERR(TAG,
"geometry->topLevelLeft=%d < 0", geometry->topLevelLeft);
461 if (geometry->left < 0)
463 WLog_ERR(TAG,
"geometry->left=%d < 0", geometry->left);
466 presentation->surface->x = WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelLeft) +
467 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->left);
469 if (geometry->topLevelTop < 0)
471 WLog_ERR(TAG,
"geometry->topLevelTop=%d < 0", geometry->topLevelTop);
474 if (geometry->top < 0)
476 WLog_ERR(TAG,
"geometry->top=%d < 0", geometry->top);
479 presentation->surface->y = WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelTop) +
480 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->top);
486static BOOL video_onMappedGeometryClear(MAPPED_GEOMETRY* geometry)
488 WINPR_ASSERT(geometry);
490 PresentationContext* presentation = (PresentationContext*)geometry->custom;
491 WINPR_ASSERT(presentation);
493 mappedGeometryUnref(presentation->geometry);
494 presentation->geometry =
nullptr;
499static UINT video_PresentationRequest(VideoClientContext* video,
502 UINT ret = CHANNEL_RC_OK;
507 VideoClientContextPriv* priv = video->priv;
510 EnterCriticalSection(&priv->framesLock);
511 if (req->Command == TSMM_START_PRESENTATION)
513 MAPPED_GEOMETRY* geom =
nullptr;
516 if (memcmp(req->VideoSubtypeId, MFVideoFormat_H264, 16) != 0)
518 WLog_ERR(TAG,
"not a H264 video, ignoring request");
522 if (priv->currentPresentation)
524 if (priv->currentPresentation->PresentationId == req->PresentationId)
526 WLog_ERR(TAG,
"ignoring start request for existing presentation %" PRIu8,
527 req->PresentationId);
531 WLog_ERR(TAG,
"releasing current presentation %" PRIu8, req->PresentationId);
532 PresentationContext_unref(&priv->currentPresentation);
537 WLog_ERR(TAG,
"geometry channel not ready, ignoring request");
541 geom = HashTable_GetItemValue(priv->geometry->geometries, &(req->GeometryMappingId));
544 WLog_ERR(TAG,
"geometry mapping 0x%" PRIx64
" not registered", req->GeometryMappingId);
548 WLog_DBG(TAG,
"creating presentation 0x%x", req->PresentationId);
549 if ((geom->topLevelLeft < 0) || (geom->left < 0) || (geom->topLevelTop < 0) ||
553 "geometry: topLevelLeft=%" PRId32
" < 0, left=%" PRId32
554 " < 0, topLevelTop=%" PRId32
" < 0, top=%" PRId32
" < 0",
555 geom->topLevelLeft, geom->left, geom->topLevelTop, geom->top);
559 priv->currentPresentation = PresentationContext_new(
560 video, req->PresentationId,
561 WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelLeft + geom->left),
562 WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelTop + geom->top), req->SourceWidth,
564 if (!priv->currentPresentation)
566 WLog_ERR(TAG,
"unable to create presentation video");
567 ret = CHANNEL_RC_NO_MEMORY;
571 mappedGeometryRef(geom);
572 priv->currentPresentation->geometry = geom;
574 priv->currentPresentation->video = video;
575 priv->currentPresentation->ScaledWidth = req->ScaledWidth;
576 priv->currentPresentation->ScaledHeight = req->ScaledHeight;
578 geom->custom = priv->currentPresentation;
579 geom->MappedGeometryUpdate = video_onMappedGeometryUpdate;
580 geom->MappedGeometryClear = video_onMappedGeometryClear;
583 resp.PresentationId = req->PresentationId;
584 ret = video_control_send_presentation_response(video, &resp);
586 else if (req->Command == TSMM_STOP_PRESENTATION)
588 WLog_DBG(TAG,
"stopping presentation 0x%x", req->PresentationId);
589 if (!priv->currentPresentation)
591 WLog_ERR(TAG,
"unknown presentation to stop %" PRIu8, req->PresentationId);
595 priv->droppedFrames = 0;
596 priv->publishedFrames = 0;
597 PresentationContext_unref(&priv->currentPresentation);
601 LeaveCriticalSection(&priv->framesLock);
606static UINT video_read_tsmm_presentation_req(VideoClientContext* context,
wStream* s)
610 WINPR_ASSERT(context);
613 if (!Stream_CheckAndLogRequiredLength(TAG, s, 60))
614 return ERROR_INVALID_DATA;
616 Stream_Read_UINT8(s, req.PresentationId);
617 Stream_Read_UINT8(s, req.Version);
618 Stream_Read_UINT8(s, req.Command);
619 Stream_Read_UINT8(s, req.FrameRate);
621 Stream_Seek_UINT16(s);
622 Stream_Seek_UINT16(s);
624 Stream_Read_UINT32(s, req.SourceWidth);
625 Stream_Read_UINT32(s, req.SourceHeight);
626 Stream_Read_UINT32(s, req.ScaledWidth);
627 Stream_Read_UINT32(s, req.ScaledHeight);
628 if ((req.ScaledWidth == 0) || (req.SourceHeight == 0) || (req.ScaledWidth == 0) ||
629 (req.ScaledHeight == 0))
632 "SourceWidth=%" PRIu32
", SourceHeight=%" PRIu32
", ScaledWidth=%" PRIu32
633 ", ScaledHeight=%" PRIu32,
634 req.SourceWidth, req.SourceHeight, req.ScaledWidth, req.ScaledHeight);
635 return ERROR_INVALID_DATA;
637 Stream_Read_UINT64(s, req.hnsTimestampOffset);
638 Stream_Read_UINT64(s, req.GeometryMappingId);
639 Stream_Read(s, req.VideoSubtypeId, 16);
641 Stream_Read_UINT32(s, req.cbExtra);
643 if (!Stream_CheckAndLogRequiredLength(TAG, s, req.cbExtra))
644 return ERROR_INVALID_DATA;
646 req.pExtraData = Stream_Pointer(s);
649 "presentationReq: id:%" PRIu8
" version:%" PRIu8
650 " command:%s srcWidth/srcHeight=%" PRIu32
"x%" PRIu32
" scaled Width/Height=%" PRIu32
651 "x%" PRIu32
" timestamp=%" PRIu64
" mappingId=%" PRIx64
"",
652 req.PresentationId, req.Version, video_command_name(req.Command), req.SourceWidth,
653 req.SourceHeight, req.ScaledWidth, req.ScaledHeight, req.hnsTimestampOffset,
654 req.GeometryMappingId);
656 return video_PresentationRequest(context, &req);
665static UINT video_control_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
668 UINT ret = CHANNEL_RC_OK;
670 UINT32 packetType = 0;
672 WINPR_ASSERT(callback);
675 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin;
678 VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface;
679 WINPR_ASSERT(context);
681 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
682 return ERROR_INVALID_DATA;
684 Stream_Read_UINT32(s, cbSize);
687 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected 8", cbSize);
688 return ERROR_INVALID_DATA;
690 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
691 return ERROR_INVALID_DATA;
693 Stream_Read_UINT32(s, packetType);
696 case TSMM_PACKET_TYPE_PRESENTATION_REQUEST:
697 ret = video_read_tsmm_presentation_req(context, s);
700 WLog_ERR(TAG,
"not expecting packet type %" PRIu32
"", packetType);
701 ret = ERROR_UNSUPPORTED_TYPE;
708static UINT video_control_send_client_notification(VideoClientContext* context,
711 BYTE buf[100] = WINPR_C_ARRAY_INIT;
713 WINPR_ASSERT(context);
716 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)context->handle;
719 wStream* s = Stream_New(buf, 32);
721 return CHANNEL_RC_NO_MEMORY;
724 Stream_Seek_UINT32(s);
725 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_CLIENT_NOTIFICATION);
726 Stream_Write_UINT8(s, notif->PresentationId);
727 Stream_Write_UINT8(s, notif->NotificationType);
729 if (notif->NotificationType == TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE)
731 Stream_Write_UINT32(s, 16);
734 Stream_Write_UINT32(s, notif->FramerateOverride.Flags);
735 Stream_Write_UINT32(s, notif->FramerateOverride.DesiredFrameRate);
736 Stream_Zero(s, 4ULL * 2ULL);
742 Stream_Write_UINT32(s, 0);
745 Stream_SealLength(s);
746 Stream_ResetPosition(s);
747 Stream_Write_UINT32(s, cbSize);
748 Stream_Free(s, FALSE);
750 return video_channel_write(video, buf, cbSize);
753static void video_timer(VideoClientContext* video, UINT64 now)
755 VideoFrame* frame =
nullptr;
759 VideoClientContextPriv* priv = video->priv;
762 EnterCriticalSection(&priv->framesLock);
763 PresentationContext* presentation = video->priv->currentPresentation;
766 const VideoFrame* peekFrame = (VideoFrame*)Queue_Peek(priv->frames);
770 if (peekFrame->publishTime > now)
775 WLog_DBG(TAG,
"dropping frame @%" PRIu64, frame->publishTime);
776 priv->droppedFrames++;
777 VideoFrame_free(priv, frame);
779 frame = Queue_Dequeue(priv->frames);
784 if (presentation && (presentation->PresentationId == frame->PresentationId))
787 const size_t frameSize = 1ull * frame->scanline * frame->h;
788 const size_t surfaceSize = 1ull * surface->scanline * surface->alignedHeight;
793 if (frameSize > surfaceSize)
794 WLog_WARN(TAG,
"dropping stale frame of %" PRIuz
" bytes, surface holds %" PRIuz,
795 frameSize, surfaceSize);
798 priv->publishedFrames++;
799 memcpy(surface->data, frame->surfaceData, frameSize);
801 WINPR_ASSERT(video->showSurface);
802 if (!video->showSurface(video, surface, presentation->ScaledWidth,
803 presentation->ScaledHeight))
804 WLog_WARN(TAG,
"showSurface failed");
807 VideoFrame_free(priv, frame);
810 if (priv->nextFeedbackTime < now)
815 if (priv->publishedFrames && priv->currentPresentation)
817 UINT32 computedRate = 0;
819 if (!PresentationContext_ref(priv->currentPresentation))
820 WLog_WARN(TAG,
"PresentationContext_ref(priv->currentPresentation) failed");
822 if (priv->droppedFrames)
829 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
833 computedRate = priv->lastSentRate - 2;
844 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
845 computedRate = XF_VIDEO_UNLIMITED_RATE;
848 computedRate = priv->lastSentRate + 2;
849 if (computedRate > XF_VIDEO_UNLIMITED_RATE)
850 computedRate = XF_VIDEO_UNLIMITED_RATE;
854 if (computedRate != priv->lastSentRate)
858 WINPR_ASSERT(priv->currentPresentation);
859 notif.PresentationId = priv->currentPresentation->PresentationId;
860 notif.NotificationType = TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE;
861 if (computedRate == XF_VIDEO_UNLIMITED_RATE)
863 notif.FramerateOverride.Flags = 0x01;
864 notif.FramerateOverride.DesiredFrameRate = 0x00;
868 notif.FramerateOverride.Flags = 0x02;
869 notif.FramerateOverride.DesiredFrameRate = computedRate;
872 video_control_send_client_notification(video, ¬if);
873 priv->lastSentRate = computedRate;
876 "server notified with rate %" PRIu32
" published=%" PRIu32
878 priv->lastSentRate, priv->publishedFrames, priv->droppedFrames);
881 PresentationContext_unref(&priv->currentPresentation);
884 priv->droppedFrames = 0;
885 priv->publishedFrames = 0;
886 priv->nextFeedbackTime = now + 1000;
888 LeaveCriticalSection(&priv->framesLock);
892static UINT video_VideoData(VideoClientContext* context,
const TSMM_VIDEO_DATA* data)
895 UINT res = CHANNEL_RC_OK;
897 WINPR_ASSERT(context);
900 VideoClientContextPriv* priv = context->priv;
903 EnterCriticalSection(&priv->framesLock);
904 PresentationContext* presentation = priv->currentPresentation;
907 WLog_ERR(TAG,
"no current presentation");
908 LeaveCriticalSection(&priv->framesLock);
909 return CHANNEL_RC_OK;
912 if (!PresentationContext_ref(presentation))
914 LeaveCriticalSection(&priv->framesLock);
915 return ERROR_INTERNAL_ERROR;
918 if (presentation->PresentationId != data->PresentationId)
920 WLog_ERR(TAG,
"current presentation id=%" PRIu8
" doesn't match data id=%" PRIu8,
921 presentation->PresentationId, data->PresentationId);
925 const size_t collected = Stream_GetPosition(presentation->currentSample);
926 if ((collected > VIDEO_MAX_SAMPLE_SIZE) || (data->cbSample > VIDEO_MAX_SAMPLE_SIZE - collected))
928 WLog_ERR(TAG,
"sample %" PRIu32
" too large: %" PRIuz
" + %" PRIu32
" bytes exceed %lu",
929 data->SampleNumber, collected, data->cbSample, VIDEO_MAX_SAMPLE_SIZE);
930 Stream_ResetPosition(presentation->currentSample);
931 res = ERROR_INVALID_DATA;
935 if (!Stream_EnsureRemainingCapacity(presentation->currentSample, data->cbSample))
937 WLog_ERR(TAG,
"unable to expand the current packet");
938 res = CHANNEL_RC_NO_MEMORY;
942 Stream_Write(presentation->currentSample, data->pSample, data->cbSample);
944 if (data->CurrentPacketIndex == data->PacketsInSample)
947 H264_CONTEXT* h264 = presentation->h264;
948 const UINT64 startTime = winpr_GetTickCount64NS();
949 MAPPED_GEOMETRY* geom = presentation->geometry;
953 Stream_SealLength(presentation->currentSample);
954 Stream_ResetPosition(presentation->currentSample);
959 const RECTANGLE_16 rect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedWidth),
960 WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedHeight) };
962 if (data->SampleNumber == 1)
964 presentation->lastPublishTime = startTime;
967 presentation->lastPublishTime += 100ull * data->hnsDuration;
968 const size_t len = Stream_Length(presentation->currentSample);
969 if (len > UINT32_MAX)
972 BOOL enqueueResult = 0;
973 VideoFrame* frame = VideoFrame_new(priv, presentation, geom);
976 WLog_ERR(TAG,
"unable to create frame");
977 res = CHANNEL_RC_NO_MEMORY;
981 status = avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
982 frame->surfaceData, surface->format, surface->scanline,
983 surface->alignedWidth, surface->alignedHeight, &rect, 1);
986 VideoFrame_free(priv, frame);
990 Queue_Lock(priv->frames);
991 drain_queue(priv, QUEUE_SIZE_LIMIT);
992 enqueueResult = Queue_Enqueue(priv->frames, frame);
993 Queue_Unlock(priv->frames);
997 WLog_ERR(TAG,
"unable to enqueue frame");
998 VideoFrame_free(priv, frame);
999 res = CHANNEL_RC_NO_MEMORY;
1004 WLog_DBG(TAG,
"scheduling frame in %" PRIu64
" ns", (frame->publishTime - startTime));
1008 PresentationContext_unref(&priv->currentPresentation);
1009 LeaveCriticalSection(&priv->framesLock);
1015static UINT video_data_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
1019 UINT32 packetType = 0;
1022 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin;
1023 WINPR_ASSERT(video);
1025 VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface;
1027 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1028 return ERROR_INVALID_DATA;
1030 Stream_Read_UINT32(s, cbSize);
1033 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected >= 8", cbSize);
1034 return ERROR_INVALID_DATA;
1037 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
1038 return ERROR_INVALID_DATA;
1040 Stream_Read_UINT32(s, packetType);
1041 if (packetType != TSMM_PACKET_TYPE_VIDEO_DATA)
1043 WLog_ERR(TAG,
"only expecting VIDEO_DATA on the data channel");
1044 return ERROR_INVALID_DATA;
1047 if (!Stream_CheckAndLogRequiredLength(TAG, s, 32))
1048 return ERROR_INVALID_DATA;
1050 Stream_Read_UINT8(s, data.PresentationId);
1051 Stream_Read_UINT8(s, data.Version);
1052 Stream_Read_UINT8(s, data.Flags);
1053 Stream_Seek_UINT8(s);
1054 Stream_Read_UINT64(s, data.hnsTimestamp);
1055 Stream_Read_UINT64(s, data.hnsDuration);
1056 Stream_Read_UINT16(s, data.CurrentPacketIndex);
1057 Stream_Read_UINT16(s, data.PacketsInSample);
1058 Stream_Read_UINT32(s, data.SampleNumber);
1059 Stream_Read_UINT32(s, data.cbSample);
1060 if (!Stream_CheckAndLogRequiredLength(TAG, s, data.cbSample))
1061 return ERROR_INVALID_DATA;
1062 data.pSample = Stream_Pointer(s);
1072 return video_VideoData(context, &data);
1081static UINT video_control_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1083 if (pChannelCallback)
1086 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1089 WINPR_ASSERT(video->context);
1091 VideoClientContextPriv* priv = video->context->priv;
1096 EnterCriticalSection(&priv->framesLock);
1097 if (video->control_callback)
1098 video->control_callback->channel_callback =
nullptr;
1099 video->connected = FALSE;
1100 LeaveCriticalSection(&priv->framesLock);
1103 free(pChannelCallback);
1104 return CHANNEL_RC_OK;
1108static UINT video_data_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1110 if (pChannelCallback)
1113 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1114 if (video && video->data_callback)
1116 video->data_callback->channel_callback =
nullptr;
1120 video->dataConnected = FALSE;
1122 free(pChannelCallback);
1123 return CHANNEL_RC_OK;
1133static UINT video_control_on_new_channel_connection(IWTSListenerCallback* listenerCallback,
1134 IWTSVirtualChannel* channel,
1135 WINPR_ATTR_UNUSED BYTE* Data,
1136 WINPR_ATTR_UNUSED BOOL* pbAccept,
1137 IWTSVirtualChannelCallback** ppCallback)
1141 WINPR_ASSERT(listener_callback);
1143 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1144 WINPR_ASSERT(video);
1146 if (video->connected)
1148 WLog_ERR(TAG,
"Channel already connected, terminating.");
1149 return ERROR_DEVICE_ALREADY_ATTACHED;
1156 WLog_ERR(TAG,
"calloc failed!");
1157 return CHANNEL_RC_NO_MEMORY;
1160 callback->iface.OnDataReceived = video_control_on_data_received;
1161 callback->iface.OnClose = video_control_on_close;
1162 callback->plugin = listener_callback->plugin;
1163 callback->channel_mgr = listener_callback->channel_mgr;
1164 callback->channel = channel;
1166 WINPR_ASSERT(video->context);
1167 WINPR_ASSERT(video->context->priv);
1168 EnterCriticalSection(&video->context->priv->framesLock);
1169 listener_callback->channel_callback = callback;
1170 LeaveCriticalSection(&video->context->priv->framesLock);
1172 *ppCallback = &callback->iface;
1173 video->connected = TRUE;
1175 return CHANNEL_RC_OK;
1180static UINT video_data_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1181 IWTSVirtualChannel* pChannel,
1182 WINPR_ATTR_UNUSED BYTE* Data,
1183 WINPR_ATTR_UNUSED BOOL* pbAccept,
1184 IWTSVirtualChannelCallback** ppCallback)
1188 WINPR_ASSERT(listener_callback);
1190 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1191 WINPR_ASSERT(video);
1193 if (video->dataConnected)
1195 WLog_ERR(TAG,
"Channel already connected, terminating.");
1196 return ERROR_DEVICE_ALREADY_ATTACHED;
1203 WLog_ERR(TAG,
"calloc failed!");
1204 return CHANNEL_RC_NO_MEMORY;
1207 callback->iface.OnDataReceived = video_data_on_data_received;
1208 callback->iface.OnClose = video_data_on_close;
1209 callback->plugin = listener_callback->plugin;
1210 callback->channel_mgr = listener_callback->channel_mgr;
1211 callback->channel = pChannel;
1212 listener_callback->channel_callback = callback;
1214 *ppCallback = &callback->iface;
1215 video->dataConnected = TRUE;
1217 return CHANNEL_RC_OK;
1221static uint64_t timer_cb(WINPR_ATTR_UNUSED rdpContext* context,
void* userdata,
1222 WINPR_ATTR_UNUSED FreeRDP_TimerID timerID, uint64_t timestamp,
1225 VideoClientContext* video = userdata;
1231 video->timer(video, timestamp);
1242static UINT video_plugin_initialize(IWTSPlugin* plugin, IWTSVirtualChannelManager* channelMgr)
1245 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)plugin;
1247 if (video->initialized)
1249 WLog_ERR(TAG,
"[%s] channel initialized twice, aborting", VIDEO_CONTROL_DVC_CHANNEL_NAME);
1250 return ERROR_INVALID_DATA;
1258 WLog_ERR(TAG,
"calloc for control callback failed!");
1259 return CHANNEL_RC_NO_MEMORY;
1262 callback->iface.OnNewChannelConnection = video_control_on_new_channel_connection;
1263 callback->plugin = plugin;
1264 callback->channel_mgr = channelMgr;
1266 status = channelMgr->CreateListener(channelMgr, VIDEO_CONTROL_DVC_CHANNEL_NAME, 0,
1267 &callback->iface, &(video->controlListener));
1268 video->control_callback = callback;
1269 if (status != CHANNEL_RC_OK)
1272 video->controlListener->pInterface = video->wtsPlugin.pInterface;
1279 WLog_ERR(TAG,
"calloc for data callback failed!");
1280 return CHANNEL_RC_NO_MEMORY;
1283 callback->iface.OnNewChannelConnection = video_data_on_new_channel_connection;
1284 callback->plugin = plugin;
1285 callback->channel_mgr = channelMgr;
1287 status = channelMgr->CreateListener(channelMgr, VIDEO_DATA_DVC_CHANNEL_NAME, 0,
1288 &callback->iface, &(video->dataListener));
1289 video->data_callback = callback;
1290 if (status == CHANNEL_RC_OK)
1291 video->dataListener->pInterface = video->wtsPlugin.pInterface;
1294 if (status == CHANNEL_RC_OK)
1295 video->context->priv->timerID =
1296 freerdp_timer_add(video->rdpcontext, 20000000, timer_cb, video->context,
true);
1297 video->initialized = video->context->priv->timerID != 0;
1298 if (!video->initialized)
1299 status = ERROR_INTERNAL_ERROR;
1309static UINT video_plugin_terminated(IWTSPlugin* pPlugin)
1311 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)pPlugin;
1313 return CHANNEL_RC_INVALID_INSTANCE;
1315 if (video->context && video->context->priv)
1316 freerdp_timer_remove(video->rdpcontext, video->context->priv->timerID);
1318 if (video->control_callback)
1320 IWTSVirtualChannelManager* mgr = video->control_callback->channel_mgr;
1322 IFCALL(mgr->DestroyListener, mgr, video->controlListener);
1324 if (video->data_callback)
1326 IWTSVirtualChannelManager* mgr = video->data_callback->channel_mgr;
1328 IFCALL(mgr->DestroyListener, mgr, video->dataListener);
1332 VideoClientContextPriv_free(video->context->priv);
1334 free(video->control_callback);
1335 free(video->data_callback);
1336 free(video->wtsPlugin.pInterface);
1338 return CHANNEL_RC_OK;
1350FREERDP_ENTRY_POINT(UINT VCAPITYPE video_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1352 UINT error = ERROR_INTERNAL_ERROR;
1354 VIDEO_PLUGIN* videoPlugin = (VIDEO_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints,
"video");
1357 videoPlugin = (VIDEO_PLUGIN*)calloc(1,
sizeof(VIDEO_PLUGIN));
1360 WLog_ERR(TAG,
"calloc failed!");
1361 return CHANNEL_RC_NO_MEMORY;
1364 videoPlugin->wtsPlugin.Initialize = video_plugin_initialize;
1365 videoPlugin->wtsPlugin.Connected =
nullptr;
1366 videoPlugin->wtsPlugin.Disconnected =
nullptr;
1367 videoPlugin->wtsPlugin.Terminated = video_plugin_terminated;
1369 VideoClientContext* videoContext =
1370 (VideoClientContext*)calloc(1,
sizeof(VideoClientContext));
1373 WLog_ERR(TAG,
"calloc failed!");
1375 return CHANNEL_RC_NO_MEMORY;
1378 VideoClientContextPriv* priv = VideoClientContextPriv_new(videoContext);
1381 WLog_ERR(TAG,
"VideoClientContextPriv_new failed!");
1384 return CHANNEL_RC_NO_MEMORY;
1387 videoContext->handle = (
void*)videoPlugin;
1388 videoContext->priv = priv;
1389 videoContext->timer = video_timer;
1390 videoContext->setGeometry = video_client_context_set_geometry;
1392 videoPlugin->wtsPlugin.pInterface = (
void*)videoContext;
1393 videoPlugin->context = videoContext;
1394 videoPlugin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1395 if (videoPlugin->rdpcontext)
1396 error = pEntryPoints->RegisterPlugin(pEntryPoints,
"video", &videoPlugin->wtsPlugin);
1400 WLog_ERR(TAG,
"could not get video Plugin.");
1401 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