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;
66#define XF_VIDEO_UNLIMITED_RATE 31
68static const BYTE MFVideoFormat_H264[] = {
'H',
'2',
'6',
'4', 0x00, 0x00, 0x10, 0x00,
69 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 };
77 UINT64 startTimeStamp;
80 UINT64 lastPublishTime;
81 UINT64 nextPublishTime;
82 volatile LONG refCounter;
85 MAPPED_GEOMETRY* geometry;
86 VideoClientContext* video;
94 MAPPED_GEOMETRY* geometry;
101struct s_VideoClientContextPriv
103 VideoClientContext* video;
104 GeometryClientContext* geometry;
107 wBufferPool* surfacePool;
108 UINT32 publishedFrames;
109 UINT32 droppedFrames;
111 UINT64 nextFeedbackTime;
112 PresentationContext* currentPresentation;
113 FreeRDP_TimerID timerID;
116static void PresentationContext_unref(PresentationContext** presentation);
117static void VideoClientContextPriv_free(VideoClientContextPriv* priv);
120static const char* video_command_name(BYTE cmd)
124 case TSMM_START_PRESENTATION:
126 case TSMM_STOP_PRESENTATION:
133static void video_client_context_set_geometry(VideoClientContext* video,
134 GeometryClientContext* geometry)
137 WINPR_ASSERT(video->priv);
139 video->priv->geometry = geometry;
142WINPR_ATTR_MALLOC(VideoClientContextPriv_free, 1)
143static VideoClientContextPriv* VideoClientContextPriv_new(VideoClientContext* video)
146 VideoClientContextPriv* ret = calloc(1,
sizeof(*ret));
150 ret->frames = Queue_New(TRUE, 10, 2);
153 WLog_ERR(TAG,
"unable to allocate frames queue");
157 ret->surfacePool = BufferPool_New(FALSE, 0, 16);
158 if (!ret->surfacePool)
160 WLog_ERR(TAG,
"unable to create surface pool");
164 if (!InitializeCriticalSectionAndSpinCount(&ret->framesLock, 4 * 1000))
166 WLog_ERR(TAG,
"unable to initialize frames lock");
175 ret->lastSentRate = 30;
179 VideoClientContextPriv_free(ret);
184static BOOL PresentationContext_ref(PresentationContext* presentation)
186 WINPR_ASSERT(presentation);
188 const LONG val = InterlockedIncrement(&presentation->refCounter);
192static void PresentationContext_free(PresentationContext* presentation)
197 MAPPED_GEOMETRY* geometry = presentation->geometry;
200 geometry->MappedGeometryUpdate =
nullptr;
201 geometry->MappedGeometryClear =
nullptr;
202 geometry->custom =
nullptr;
203 mappedGeometryUnref(geometry);
206 h264_context_free(presentation->h264);
207 Stream_Free(presentation->currentSample, TRUE);
208 presentation->video->deleteSurface(presentation->video, presentation->surface);
212WINPR_ATTR_MALLOC(PresentationContext_free, 1)
213static PresentationContext* PresentationContext_new(VideoClientContext* video, BYTE PresentationId,
214 UINT32 x, UINT32 y, UINT32 width, UINT32 height)
216 if ((width == 0) || (height == 0))
218 WLog_ERR(TAG,
"width==%" PRIu32
", height=%" PRIu32, width, height);
221 const size_t s = 4ULL * width * height;
228 PresentationContext* ret = calloc(1,
sizeof(*ret));
233 ret->PresentationId = PresentationId;
235 ret->h264 = h264_context_new(FALSE);
238 WLog_ERR(TAG,
"unable to create a h264 context");
242 VIDEO_PLUGIN* plugin = (VIDEO_PLUGIN*)video->handle;
243 WINPR_ASSERT(plugin);
244 WINPR_ASSERT(plugin->rdpcontext);
245 if (!h264_context_set_option(ret->h264, H264_CONTEXT_OPTION_HW_ACCEL,
247 FreeRDP_SoftwareGdi) == 0))
249 if (!h264_context_reset(ret->h264, width, height))
252 ret->currentSample = Stream_New(
nullptr, 4096);
253 if (!ret->currentSample)
255 WLog_ERR(TAG,
"unable to create current packet stream");
259 ret->surface = video->createSurface(video, x, y, width, height);
262 WLog_ERR(TAG,
"unable to create surface");
266 if (!PresentationContext_ref(ret))
272 PresentationContext_free(ret);
276static void PresentationContext_unref(PresentationContext** ppresentation)
278 WINPR_ASSERT(ppresentation);
280 PresentationContext* presentation = *ppresentation;
284 if (InterlockedDecrement(&presentation->refCounter) > 0)
286 *ppresentation =
nullptr;
288 PresentationContext_free(presentation);
291static void VideoFrame_free(VideoClientContextPriv* priv, VideoFrame* frame)
297 mappedGeometryUnref(frame->geometry);
299 BufferPool_Return(priv->surfacePool, frame->surfaceData);
303WINPR_ATTR_MALLOC(VideoFrame_free, 1)
304static VideoFrame* VideoFrame_new(VideoClientContextPriv* priv, PresentationContext* presentation,
305 MAPPED_GEOMETRY* geom)
308 WINPR_ASSERT(presentation);
312 WINPR_ASSERT(surface);
314 VideoFrame* frame = calloc(1,
sizeof(VideoFrame));
317 frame->PresentationId = presentation->PresentationId;
319 mappedGeometryRef(geom);
321 frame->publishTime = presentation->lastPublishTime;
322 frame->geometry = geom;
323 frame->w = surface->alignedWidth;
324 frame->h = surface->alignedHeight;
325 frame->scanline = surface->scanline;
327 frame->surfaceData = BufferPool_Take(priv->surfacePool, 1ll * frame->scanline * frame->h);
328 if (!frame->surfaceData)
334 VideoFrame_free(priv, frame);
338void VideoClientContextPriv_free(VideoClientContextPriv* priv)
343 EnterCriticalSection(&priv->framesLock);
347 while (Queue_Count(priv->frames))
349 VideoFrame* frame = Queue_Dequeue(priv->frames);
351 VideoFrame_free(priv, frame);
355 Queue_Free(priv->frames);
356 LeaveCriticalSection(&priv->framesLock);
358 DeleteCriticalSection(&priv->framesLock);
360 if (priv->currentPresentation)
361 PresentationContext_unref(&priv->currentPresentation);
363 BufferPool_Free(priv->surfacePool);
368static UINT video_channel_write(VIDEO_PLUGIN* video,
const BYTE* data, UINT32 length)
372 if (!video->control_callback || !video->control_callback->channel_callback)
373 return ERROR_BAD_CONFIGURATION;
374 IWTSVirtualChannel* channel = video->control_callback->channel_callback->channel;
375 if (!channel || !channel->Write)
376 return ERROR_BAD_CONFIGURATION;
377 return channel->Write(channel, length, data,
nullptr);
381static UINT video_control_send_presentation_response(VideoClientContext* context,
384 BYTE buf[12] = WINPR_C_ARRAY_INIT;
386 WINPR_ASSERT(context);
389 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)context->handle;
392 wStream* s = Stream_New(buf, 12);
394 return CHANNEL_RC_NO_MEMORY;
396 Stream_Write_UINT32(s, 12);
397 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_PRESENTATION_RESPONSE);
398 Stream_Write_UINT8(s, resp->PresentationId);
400 Stream_SealLength(s);
401 Stream_Free(s, FALSE);
403 return video_channel_write(video, buf,
sizeof(buf));
407static BOOL video_onMappedGeometryUpdate(MAPPED_GEOMETRY* geometry)
409 WINPR_ASSERT(geometry);
411 PresentationContext* presentation = (PresentationContext*)geometry->custom;
412 WINPR_ASSERT(presentation);
414 RDP_RECT* r = &geometry->geometry.boundingRect;
416 "geometry updated topGeom=(%" PRId32
",%" PRId32
"-%" PRId32
"x%" PRId32
417 ") geom=(%" PRId32
",%" PRId32
"-%" PRId32
"x%" PRId32
") rects=(%" PRId16
",%" PRId16
418 "-%" PRId16
"x%" PRId16
")",
419 geometry->topLevelLeft, geometry->topLevelTop,
420 geometry->topLevelRight - geometry->topLevelLeft,
421 geometry->topLevelBottom - geometry->topLevelTop,
423 geometry->left, geometry->top, geometry->right - geometry->left,
424 geometry->bottom - geometry->top,
426 r->x, r->y, r->width, r->height);
428 WINPR_ASSERT(presentation->surface);
429 if (geometry->topLevelLeft < 0)
431 WLog_ERR(TAG,
"geometry->topLevelLeft=%d < 0", geometry->topLevelLeft);
434 if (geometry->left < 0)
436 WLog_ERR(TAG,
"geometry->left=%d < 0", geometry->left);
439 presentation->surface->x = WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelLeft) +
440 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->left);
442 if (geometry->topLevelTop < 0)
444 WLog_ERR(TAG,
"geometry->topLevelTop=%d < 0", geometry->topLevelTop);
447 if (geometry->top < 0)
449 WLog_ERR(TAG,
"geometry->top=%d < 0", geometry->top);
452 presentation->surface->y = WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelTop) +
453 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->top);
459static BOOL video_onMappedGeometryClear(MAPPED_GEOMETRY* geometry)
461 WINPR_ASSERT(geometry);
463 PresentationContext* presentation = (PresentationContext*)geometry->custom;
464 WINPR_ASSERT(presentation);
466 mappedGeometryUnref(presentation->geometry);
467 presentation->geometry =
nullptr;
472static UINT video_PresentationRequest(VideoClientContext* video,
475 UINT ret = CHANNEL_RC_OK;
480 VideoClientContextPriv* priv = video->priv;
483 EnterCriticalSection(&priv->framesLock);
484 if (req->Command == TSMM_START_PRESENTATION)
486 MAPPED_GEOMETRY* geom =
nullptr;
489 if (memcmp(req->VideoSubtypeId, MFVideoFormat_H264, 16) != 0)
491 WLog_ERR(TAG,
"not a H264 video, ignoring request");
495 if (priv->currentPresentation)
497 if (priv->currentPresentation->PresentationId == req->PresentationId)
499 WLog_ERR(TAG,
"ignoring start request for existing presentation %" PRIu8,
500 req->PresentationId);
504 WLog_ERR(TAG,
"releasing current presentation %" PRIu8, req->PresentationId);
505 PresentationContext_unref(&priv->currentPresentation);
510 WLog_ERR(TAG,
"geometry channel not ready, ignoring request");
514 geom = HashTable_GetItemValue(priv->geometry->geometries, &(req->GeometryMappingId));
517 WLog_ERR(TAG,
"geometry mapping 0x%" PRIx64
" not registered", req->GeometryMappingId);
521 WLog_DBG(TAG,
"creating presentation 0x%x", req->PresentationId);
522 if ((geom->topLevelLeft < 0) || (geom->left < 0) || (geom->topLevelTop < 0) ||
526 "geometry: topLevelLeft=%" PRId32
" < 0, left=%" PRId32
527 " < 0, topLevelTop=%" PRId32
" < 0, top=%" PRId32
" < 0",
528 geom->topLevelLeft, geom->left, geom->topLevelTop, geom->top);
532 priv->currentPresentation = PresentationContext_new(
533 video, req->PresentationId,
534 WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelLeft + geom->left),
535 WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelTop + geom->top), req->SourceWidth,
537 if (!priv->currentPresentation)
539 WLog_ERR(TAG,
"unable to create presentation video");
540 ret = CHANNEL_RC_NO_MEMORY;
544 mappedGeometryRef(geom);
545 priv->currentPresentation->geometry = geom;
547 priv->currentPresentation->video = video;
548 priv->currentPresentation->ScaledWidth = req->ScaledWidth;
549 priv->currentPresentation->ScaledHeight = req->ScaledHeight;
551 geom->custom = priv->currentPresentation;
552 geom->MappedGeometryUpdate = video_onMappedGeometryUpdate;
553 geom->MappedGeometryClear = video_onMappedGeometryClear;
556 resp.PresentationId = req->PresentationId;
557 ret = video_control_send_presentation_response(video, &resp);
559 else if (req->Command == TSMM_STOP_PRESENTATION)
561 WLog_DBG(TAG,
"stopping presentation 0x%x", req->PresentationId);
562 if (!priv->currentPresentation)
564 WLog_ERR(TAG,
"unknown presentation to stop %" PRIu8, req->PresentationId);
568 priv->droppedFrames = 0;
569 priv->publishedFrames = 0;
570 PresentationContext_unref(&priv->currentPresentation);
574 LeaveCriticalSection(&priv->framesLock);
579static UINT video_read_tsmm_presentation_req(VideoClientContext* context,
wStream* s)
583 WINPR_ASSERT(context);
586 if (!Stream_CheckAndLogRequiredLength(TAG, s, 60))
587 return ERROR_INVALID_DATA;
589 Stream_Read_UINT8(s, req.PresentationId);
590 Stream_Read_UINT8(s, req.Version);
591 Stream_Read_UINT8(s, req.Command);
592 Stream_Read_UINT8(s, req.FrameRate);
594 Stream_Seek_UINT16(s);
595 Stream_Seek_UINT16(s);
597 Stream_Read_UINT32(s, req.SourceWidth);
598 Stream_Read_UINT32(s, req.SourceHeight);
599 Stream_Read_UINT32(s, req.ScaledWidth);
600 Stream_Read_UINT32(s, req.ScaledHeight);
601 if ((req.ScaledWidth == 0) || (req.SourceHeight == 0) || (req.ScaledWidth == 0) ||
602 (req.ScaledHeight == 0))
605 "SourceWidth=%" PRIu32
", SourceHeight=%" PRIu32
", ScaledWidth=%" PRIu32
606 ", ScaledHeight=%" PRIu32,
607 req.SourceWidth, req.SourceHeight, req.ScaledWidth, req.ScaledHeight);
608 return ERROR_INVALID_DATA;
610 Stream_Read_UINT64(s, req.hnsTimestampOffset);
611 Stream_Read_UINT64(s, req.GeometryMappingId);
612 Stream_Read(s, req.VideoSubtypeId, 16);
614 Stream_Read_UINT32(s, req.cbExtra);
616 if (!Stream_CheckAndLogRequiredLength(TAG, s, req.cbExtra))
617 return ERROR_INVALID_DATA;
619 req.pExtraData = Stream_Pointer(s);
622 "presentationReq: id:%" PRIu8
" version:%" PRIu8
623 " command:%s srcWidth/srcHeight=%" PRIu32
"x%" PRIu32
" scaled Width/Height=%" PRIu32
624 "x%" PRIu32
" timestamp=%" PRIu64
" mappingId=%" PRIx64
"",
625 req.PresentationId, req.Version, video_command_name(req.Command), req.SourceWidth,
626 req.SourceHeight, req.ScaledWidth, req.ScaledHeight, req.hnsTimestampOffset,
627 req.GeometryMappingId);
629 return video_PresentationRequest(context, &req);
638static UINT video_control_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
641 UINT ret = CHANNEL_RC_OK;
643 UINT32 packetType = 0;
645 WINPR_ASSERT(callback);
648 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin;
651 VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface;
652 WINPR_ASSERT(context);
654 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
655 return ERROR_INVALID_DATA;
657 Stream_Read_UINT32(s, cbSize);
660 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected 8", cbSize);
661 return ERROR_INVALID_DATA;
663 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
664 return ERROR_INVALID_DATA;
666 Stream_Read_UINT32(s, packetType);
669 case TSMM_PACKET_TYPE_PRESENTATION_REQUEST:
670 ret = video_read_tsmm_presentation_req(context, s);
673 WLog_ERR(TAG,
"not expecting packet type %" PRIu32
"", packetType);
674 ret = ERROR_UNSUPPORTED_TYPE;
681static UINT video_control_send_client_notification(VideoClientContext* context,
684 BYTE buf[100] = WINPR_C_ARRAY_INIT;
686 WINPR_ASSERT(context);
689 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)context->handle;
692 wStream* s = Stream_New(buf, 32);
694 return CHANNEL_RC_NO_MEMORY;
697 Stream_Seek_UINT32(s);
698 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_CLIENT_NOTIFICATION);
699 Stream_Write_UINT8(s, notif->PresentationId);
700 Stream_Write_UINT8(s, notif->NotificationType);
702 if (notif->NotificationType == TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE)
704 Stream_Write_UINT32(s, 16);
707 Stream_Write_UINT32(s, notif->FramerateOverride.Flags);
708 Stream_Write_UINT32(s, notif->FramerateOverride.DesiredFrameRate);
709 Stream_Zero(s, 4ULL * 2ULL);
715 Stream_Write_UINT32(s, 0);
718 Stream_SealLength(s);
719 Stream_ResetPosition(s);
720 Stream_Write_UINT32(s, cbSize);
721 Stream_Free(s, FALSE);
723 return video_channel_write(video, buf, cbSize);
726static void video_timer(VideoClientContext* video, UINT64 now)
728 VideoFrame* frame =
nullptr;
732 VideoClientContextPriv* priv = video->priv;
735 EnterCriticalSection(&priv->framesLock);
736 PresentationContext* presentation = video->priv->currentPresentation;
739 const VideoFrame* peekFrame = (VideoFrame*)Queue_Peek(priv->frames);
743 if (peekFrame->publishTime > now)
748 WLog_DBG(TAG,
"dropping frame @%" PRIu64, frame->publishTime);
749 priv->droppedFrames++;
750 VideoFrame_free(priv, frame);
752 frame = Queue_Dequeue(priv->frames);
757 if (presentation && (presentation->PresentationId == frame->PresentationId))
760 const size_t frameSize = 1ull * frame->scanline * frame->h;
761 const size_t surfaceSize = 1ull * surface->scanline * surface->alignedHeight;
766 if (frameSize > surfaceSize)
767 WLog_WARN(TAG,
"dropping stale frame of %" PRIuz
" bytes, surface holds %" PRIuz,
768 frameSize, surfaceSize);
771 priv->publishedFrames++;
772 memcpy(surface->data, frame->surfaceData, frameSize);
774 WINPR_ASSERT(video->showSurface);
775 if (!video->showSurface(video, surface, presentation->ScaledWidth,
776 presentation->ScaledHeight))
777 WLog_WARN(TAG,
"showSurface failed");
780 VideoFrame_free(priv, frame);
783 if (priv->nextFeedbackTime < now)
788 if (priv->publishedFrames && priv->currentPresentation)
790 UINT32 computedRate = 0;
792 if (!PresentationContext_ref(priv->currentPresentation))
793 WLog_WARN(TAG,
"PresentationContext_ref(priv->currentPresentation) failed");
795 if (priv->droppedFrames)
802 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
806 computedRate = priv->lastSentRate - 2;
817 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
818 computedRate = XF_VIDEO_UNLIMITED_RATE;
821 computedRate = priv->lastSentRate + 2;
822 if (computedRate > XF_VIDEO_UNLIMITED_RATE)
823 computedRate = XF_VIDEO_UNLIMITED_RATE;
827 if (computedRate != priv->lastSentRate)
831 WINPR_ASSERT(priv->currentPresentation);
832 notif.PresentationId = priv->currentPresentation->PresentationId;
833 notif.NotificationType = TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE;
834 if (computedRate == XF_VIDEO_UNLIMITED_RATE)
836 notif.FramerateOverride.Flags = 0x01;
837 notif.FramerateOverride.DesiredFrameRate = 0x00;
841 notif.FramerateOverride.Flags = 0x02;
842 notif.FramerateOverride.DesiredFrameRate = computedRate;
845 video_control_send_client_notification(video, ¬if);
846 priv->lastSentRate = computedRate;
849 "server notified with rate %" PRIu32
" published=%" PRIu32
851 priv->lastSentRate, priv->publishedFrames, priv->droppedFrames);
854 PresentationContext_unref(&priv->currentPresentation);
857 priv->droppedFrames = 0;
858 priv->publishedFrames = 0;
859 priv->nextFeedbackTime = now + 1000;
861 LeaveCriticalSection(&priv->framesLock);
865static UINT video_VideoData(VideoClientContext* context,
const TSMM_VIDEO_DATA* data)
868 UINT res = CHANNEL_RC_OK;
870 WINPR_ASSERT(context);
873 VideoClientContextPriv* priv = context->priv;
876 EnterCriticalSection(&priv->framesLock);
877 PresentationContext* presentation = priv->currentPresentation;
880 WLog_ERR(TAG,
"no current presentation");
881 LeaveCriticalSection(&priv->framesLock);
882 return CHANNEL_RC_OK;
885 if (!PresentationContext_ref(presentation))
887 LeaveCriticalSection(&priv->framesLock);
888 return ERROR_INTERNAL_ERROR;
891 if (presentation->PresentationId != data->PresentationId)
893 WLog_ERR(TAG,
"current presentation id=%" PRIu8
" doesn't match data id=%" PRIu8,
894 presentation->PresentationId, data->PresentationId);
898 if (!Stream_EnsureRemainingCapacity(presentation->currentSample, data->cbSample))
900 WLog_ERR(TAG,
"unable to expand the current packet");
901 res = CHANNEL_RC_NO_MEMORY;
905 Stream_Write(presentation->currentSample, data->pSample, data->cbSample);
907 if (data->CurrentPacketIndex == data->PacketsInSample)
910 H264_CONTEXT* h264 = presentation->h264;
911 const UINT64 startTime = winpr_GetTickCount64NS();
912 MAPPED_GEOMETRY* geom = presentation->geometry;
916 const RECTANGLE_16 rect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedWidth),
917 WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedHeight) };
918 Stream_SealLength(presentation->currentSample);
919 Stream_ResetPosition(presentation->currentSample);
921 if (data->SampleNumber == 1)
923 presentation->lastPublishTime = startTime;
926 presentation->lastPublishTime += 100ull * data->hnsDuration;
927 const size_t len = Stream_Length(presentation->currentSample);
928 if (len > UINT32_MAX)
931 BOOL enqueueResult = 0;
932 VideoFrame* frame = VideoFrame_new(priv, presentation, geom);
935 WLog_ERR(TAG,
"unable to create frame");
936 res = CHANNEL_RC_NO_MEMORY;
940 status = avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
941 frame->surfaceData, surface->format, surface->scanline,
942 surface->alignedWidth, surface->alignedHeight, &rect, 1);
945 VideoFrame_free(priv, frame);
949 enqueueResult = Queue_Enqueue(priv->frames, frame);
953 WLog_ERR(TAG,
"unable to enqueue frame");
954 VideoFrame_free(priv, frame);
955 res = CHANNEL_RC_NO_MEMORY;
960 WLog_DBG(TAG,
"scheduling frame in %" PRIu64
" ns", (frame->publishTime - startTime));
964 PresentationContext_unref(&priv->currentPresentation);
965 LeaveCriticalSection(&priv->framesLock);
971static UINT video_data_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
975 UINT32 packetType = 0;
978 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin;
981 VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface;
983 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
984 return ERROR_INVALID_DATA;
986 Stream_Read_UINT32(s, cbSize);
989 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected >= 8", cbSize);
990 return ERROR_INVALID_DATA;
993 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
994 return ERROR_INVALID_DATA;
996 Stream_Read_UINT32(s, packetType);
997 if (packetType != TSMM_PACKET_TYPE_VIDEO_DATA)
999 WLog_ERR(TAG,
"only expecting VIDEO_DATA on the data channel");
1000 return ERROR_INVALID_DATA;
1003 if (!Stream_CheckAndLogRequiredLength(TAG, s, 32))
1004 return ERROR_INVALID_DATA;
1006 Stream_Read_UINT8(s, data.PresentationId);
1007 Stream_Read_UINT8(s, data.Version);
1008 Stream_Read_UINT8(s, data.Flags);
1009 Stream_Seek_UINT8(s);
1010 Stream_Read_UINT64(s, data.hnsTimestamp);
1011 Stream_Read_UINT64(s, data.hnsDuration);
1012 Stream_Read_UINT16(s, data.CurrentPacketIndex);
1013 Stream_Read_UINT16(s, data.PacketsInSample);
1014 Stream_Read_UINT32(s, data.SampleNumber);
1015 Stream_Read_UINT32(s, data.cbSample);
1016 if (!Stream_CheckAndLogRequiredLength(TAG, s, data.cbSample))
1017 return ERROR_INVALID_DATA;
1018 data.pSample = Stream_Pointer(s);
1028 return video_VideoData(context, &data);
1037static UINT video_control_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1039 if (pChannelCallback)
1042 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1043 if (video && video->control_callback)
1045 video->control_callback->channel_callback =
nullptr;
1048 free(pChannelCallback);
1049 return CHANNEL_RC_OK;
1053static UINT video_data_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1055 if (pChannelCallback)
1058 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1059 if (video && video->data_callback)
1061 video->data_callback->channel_callback =
nullptr;
1064 free(pChannelCallback);
1065 return CHANNEL_RC_OK;
1075static UINT video_control_on_new_channel_connection(IWTSListenerCallback* listenerCallback,
1076 IWTSVirtualChannel* channel,
1077 WINPR_ATTR_UNUSED BYTE* Data,
1078 WINPR_ATTR_UNUSED BOOL* pbAccept,
1079 IWTSVirtualChannelCallback** ppCallback)
1088 WLog_ERR(TAG,
"calloc failed!");
1089 return CHANNEL_RC_NO_MEMORY;
1092 callback->iface.OnDataReceived = video_control_on_data_received;
1093 callback->iface.OnClose = video_control_on_close;
1094 callback->plugin = listener_callback->plugin;
1095 callback->channel_mgr = listener_callback->channel_mgr;
1096 callback->channel = channel;
1097 listener_callback->channel_callback = callback;
1099 *ppCallback = &callback->iface;
1101 return CHANNEL_RC_OK;
1106static UINT video_data_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1107 IWTSVirtualChannel* pChannel,
1108 WINPR_ATTR_UNUSED BYTE* Data,
1109 WINPR_ATTR_UNUSED BOOL* pbAccept,
1110 IWTSVirtualChannelCallback** ppCallback)
1119 WLog_ERR(TAG,
"calloc failed!");
1120 return CHANNEL_RC_NO_MEMORY;
1123 callback->iface.OnDataReceived = video_data_on_data_received;
1124 callback->iface.OnClose = video_data_on_close;
1125 callback->plugin = listener_callback->plugin;
1126 callback->channel_mgr = listener_callback->channel_mgr;
1127 callback->channel = pChannel;
1128 listener_callback->channel_callback = callback;
1130 *ppCallback = &callback->iface;
1132 return CHANNEL_RC_OK;
1136static uint64_t timer_cb(WINPR_ATTR_UNUSED rdpContext* context,
void* userdata,
1137 WINPR_ATTR_UNUSED FreeRDP_TimerID timerID, uint64_t timestamp,
1140 VideoClientContext* video = userdata;
1146 video->timer(video, timestamp);
1157static UINT video_plugin_initialize(IWTSPlugin* plugin, IWTSVirtualChannelManager* channelMgr)
1160 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)plugin;
1162 if (video->initialized)
1164 WLog_ERR(TAG,
"[%s] channel initialized twice, aborting", VIDEO_CONTROL_DVC_CHANNEL_NAME);
1165 return ERROR_INVALID_DATA;
1173 WLog_ERR(TAG,
"calloc for control callback failed!");
1174 return CHANNEL_RC_NO_MEMORY;
1177 callback->iface.OnNewChannelConnection = video_control_on_new_channel_connection;
1178 callback->plugin = plugin;
1179 callback->channel_mgr = channelMgr;
1181 status = channelMgr->CreateListener(channelMgr, VIDEO_CONTROL_DVC_CHANNEL_NAME, 0,
1182 &callback->iface, &(video->controlListener));
1183 video->control_callback = callback;
1184 if (status != CHANNEL_RC_OK)
1187 video->controlListener->pInterface = video->wtsPlugin.pInterface;
1194 WLog_ERR(TAG,
"calloc for data callback failed!");
1195 return CHANNEL_RC_NO_MEMORY;
1198 callback->iface.OnNewChannelConnection = video_data_on_new_channel_connection;
1199 callback->plugin = plugin;
1200 callback->channel_mgr = channelMgr;
1202 status = channelMgr->CreateListener(channelMgr, VIDEO_DATA_DVC_CHANNEL_NAME, 0,
1203 &callback->iface, &(video->dataListener));
1204 video->data_callback = callback;
1205 if (status == CHANNEL_RC_OK)
1206 video->dataListener->pInterface = video->wtsPlugin.pInterface;
1209 if (status == CHANNEL_RC_OK)
1210 video->context->priv->timerID =
1211 freerdp_timer_add(video->rdpcontext, 20000000, timer_cb, video->context,
true);
1212 video->initialized = video->context->priv->timerID != 0;
1213 if (!video->initialized)
1214 status = ERROR_INTERNAL_ERROR;
1224static UINT video_plugin_terminated(IWTSPlugin* pPlugin)
1226 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)pPlugin;
1228 return CHANNEL_RC_INVALID_INSTANCE;
1230 if (video->context && video->context->priv)
1231 freerdp_timer_remove(video->rdpcontext, video->context->priv->timerID);
1233 if (video->control_callback)
1235 IWTSVirtualChannelManager* mgr = video->control_callback->channel_mgr;
1237 IFCALL(mgr->DestroyListener, mgr, video->controlListener);
1239 if (video->data_callback)
1241 IWTSVirtualChannelManager* mgr = video->data_callback->channel_mgr;
1243 IFCALL(mgr->DestroyListener, mgr, video->dataListener);
1247 VideoClientContextPriv_free(video->context->priv);
1249 free(video->control_callback);
1250 free(video->data_callback);
1251 free(video->wtsPlugin.pInterface);
1253 return CHANNEL_RC_OK;
1265FREERDP_ENTRY_POINT(UINT VCAPITYPE video_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1267 UINT error = ERROR_INTERNAL_ERROR;
1269 VIDEO_PLUGIN* videoPlugin = (VIDEO_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints,
"video");
1272 videoPlugin = (VIDEO_PLUGIN*)calloc(1,
sizeof(VIDEO_PLUGIN));
1275 WLog_ERR(TAG,
"calloc failed!");
1276 return CHANNEL_RC_NO_MEMORY;
1279 videoPlugin->wtsPlugin.Initialize = video_plugin_initialize;
1280 videoPlugin->wtsPlugin.Connected =
nullptr;
1281 videoPlugin->wtsPlugin.Disconnected =
nullptr;
1282 videoPlugin->wtsPlugin.Terminated = video_plugin_terminated;
1284 VideoClientContext* videoContext =
1285 (VideoClientContext*)calloc(1,
sizeof(VideoClientContext));
1288 WLog_ERR(TAG,
"calloc failed!");
1290 return CHANNEL_RC_NO_MEMORY;
1293 VideoClientContextPriv* priv = VideoClientContextPriv_new(videoContext);
1296 WLog_ERR(TAG,
"VideoClientContextPriv_new failed!");
1299 return CHANNEL_RC_NO_MEMORY;
1302 videoContext->handle = (
void*)videoPlugin;
1303 videoContext->priv = priv;
1304 videoContext->timer = video_timer;
1305 videoContext->setGeometry = video_client_context_set_geometry;
1307 videoPlugin->wtsPlugin.pInterface = (
void*)videoContext;
1308 videoPlugin->context = videoContext;
1309 videoPlugin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1310 if (videoPlugin->rdpcontext)
1311 error = pEntryPoints->RegisterPlugin(pEntryPoints,
"video", &videoPlugin->wtsPlugin);
1315 WLog_ERR(TAG,
"could not get video Plugin.");
1316 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