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(
246 ret->h264, H264_CONTEXT_OPTION_HW_ACCEL,
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 =
440 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelLeft + 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 =
453 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelTop + 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 PresentationContext* presentation = priv->currentPresentation;
879 WLog_ERR(TAG,
"no current presentation");
880 return CHANNEL_RC_OK;
883 if (!PresentationContext_ref(presentation))
884 return ERROR_INTERNAL_ERROR;
886 EnterCriticalSection(&priv->framesLock);
887 if (presentation->PresentationId != data->PresentationId)
889 WLog_ERR(TAG,
"current presentation id=%" PRIu8
" doesn't match data id=%" PRIu8,
890 presentation->PresentationId, data->PresentationId);
894 if (!Stream_EnsureRemainingCapacity(presentation->currentSample, data->cbSample))
896 WLog_ERR(TAG,
"unable to expand the current packet");
897 res = CHANNEL_RC_NO_MEMORY;
901 Stream_Write(presentation->currentSample, data->pSample, data->cbSample);
903 if (data->CurrentPacketIndex == data->PacketsInSample)
906 H264_CONTEXT* h264 = presentation->h264;
907 const UINT64 startTime = winpr_GetTickCount64NS();
908 MAPPED_GEOMETRY* geom = presentation->geometry;
910 const RECTANGLE_16 rect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedWidth),
911 WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedHeight) };
912 Stream_SealLength(presentation->currentSample);
913 Stream_ResetPosition(presentation->currentSample);
915 if (data->SampleNumber == 1)
917 presentation->lastPublishTime = startTime;
920 presentation->lastPublishTime += 100ull * data->hnsDuration;
921 const size_t len = Stream_Length(presentation->currentSample);
922 if (len > UINT32_MAX)
925 BOOL enqueueResult = 0;
926 VideoFrame* frame = VideoFrame_new(priv, presentation, geom);
929 WLog_ERR(TAG,
"unable to create frame");
930 res = CHANNEL_RC_NO_MEMORY;
934 status = avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
935 frame->surfaceData, surface->format, surface->scanline,
936 surface->alignedWidth, surface->alignedHeight, &rect, 1);
939 VideoFrame_free(priv, frame);
943 enqueueResult = Queue_Enqueue(priv->frames, frame);
947 WLog_ERR(TAG,
"unable to enqueue frame");
948 VideoFrame_free(priv, frame);
949 res = CHANNEL_RC_NO_MEMORY;
954 WLog_DBG(TAG,
"scheduling frame in %" PRIu64
" ns", (frame->publishTime - startTime));
958 PresentationContext_unref(&priv->currentPresentation);
959 LeaveCriticalSection(&priv->framesLock);
965static UINT video_data_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
969 UINT32 packetType = 0;
972 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin;
975 VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface;
977 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
978 return ERROR_INVALID_DATA;
980 Stream_Read_UINT32(s, cbSize);
983 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected >= 8", cbSize);
984 return ERROR_INVALID_DATA;
987 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
988 return ERROR_INVALID_DATA;
990 Stream_Read_UINT32(s, packetType);
991 if (packetType != TSMM_PACKET_TYPE_VIDEO_DATA)
993 WLog_ERR(TAG,
"only expecting VIDEO_DATA on the data channel");
994 return ERROR_INVALID_DATA;
997 if (!Stream_CheckAndLogRequiredLength(TAG, s, 32))
998 return ERROR_INVALID_DATA;
1000 Stream_Read_UINT8(s, data.PresentationId);
1001 Stream_Read_UINT8(s, data.Version);
1002 Stream_Read_UINT8(s, data.Flags);
1003 Stream_Seek_UINT8(s);
1004 Stream_Read_UINT64(s, data.hnsTimestamp);
1005 Stream_Read_UINT64(s, data.hnsDuration);
1006 Stream_Read_UINT16(s, data.CurrentPacketIndex);
1007 Stream_Read_UINT16(s, data.PacketsInSample);
1008 Stream_Read_UINT32(s, data.SampleNumber);
1009 Stream_Read_UINT32(s, data.cbSample);
1010 if (!Stream_CheckAndLogRequiredLength(TAG, s, data.cbSample))
1011 return ERROR_INVALID_DATA;
1012 data.pSample = Stream_Pointer(s);
1022 return video_VideoData(context, &data);
1031static UINT video_control_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1033 if (pChannelCallback)
1036 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1037 if (video && video->control_callback)
1039 video->control_callback->channel_callback =
nullptr;
1042 free(pChannelCallback);
1043 return CHANNEL_RC_OK;
1047static UINT video_data_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1049 if (pChannelCallback)
1052 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1053 if (video && video->data_callback)
1055 video->data_callback->channel_callback =
nullptr;
1058 free(pChannelCallback);
1059 return CHANNEL_RC_OK;
1069static UINT video_control_on_new_channel_connection(IWTSListenerCallback* listenerCallback,
1070 IWTSVirtualChannel* channel,
1071 WINPR_ATTR_UNUSED BYTE* Data,
1072 WINPR_ATTR_UNUSED BOOL* pbAccept,
1073 IWTSVirtualChannelCallback** ppCallback)
1082 WLog_ERR(TAG,
"calloc failed!");
1083 return CHANNEL_RC_NO_MEMORY;
1086 callback->iface.OnDataReceived = video_control_on_data_received;
1087 callback->iface.OnClose = video_control_on_close;
1088 callback->plugin = listener_callback->plugin;
1089 callback->channel_mgr = listener_callback->channel_mgr;
1090 callback->channel = channel;
1091 listener_callback->channel_callback = callback;
1093 *ppCallback = &callback->iface;
1095 return CHANNEL_RC_OK;
1100static UINT video_data_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1101 IWTSVirtualChannel* pChannel,
1102 WINPR_ATTR_UNUSED BYTE* Data,
1103 WINPR_ATTR_UNUSED BOOL* pbAccept,
1104 IWTSVirtualChannelCallback** ppCallback)
1113 WLog_ERR(TAG,
"calloc failed!");
1114 return CHANNEL_RC_NO_MEMORY;
1117 callback->iface.OnDataReceived = video_data_on_data_received;
1118 callback->iface.OnClose = video_data_on_close;
1119 callback->plugin = listener_callback->plugin;
1120 callback->channel_mgr = listener_callback->channel_mgr;
1121 callback->channel = pChannel;
1122 listener_callback->channel_callback = callback;
1124 *ppCallback = &callback->iface;
1126 return CHANNEL_RC_OK;
1130static uint64_t timer_cb(WINPR_ATTR_UNUSED rdpContext* context,
void* userdata,
1131 WINPR_ATTR_UNUSED FreeRDP_TimerID timerID, uint64_t timestamp,
1134 VideoClientContext* video = userdata;
1140 video->timer(video, timestamp);
1151static UINT video_plugin_initialize(IWTSPlugin* plugin, IWTSVirtualChannelManager* channelMgr)
1154 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)plugin;
1156 if (video->initialized)
1158 WLog_ERR(TAG,
"[%s] channel initialized twice, aborting", VIDEO_CONTROL_DVC_CHANNEL_NAME);
1159 return ERROR_INVALID_DATA;
1167 WLog_ERR(TAG,
"calloc for control callback failed!");
1168 return CHANNEL_RC_NO_MEMORY;
1171 callback->iface.OnNewChannelConnection = video_control_on_new_channel_connection;
1172 callback->plugin = plugin;
1173 callback->channel_mgr = channelMgr;
1175 status = channelMgr->CreateListener(channelMgr, VIDEO_CONTROL_DVC_CHANNEL_NAME, 0,
1176 &callback->iface, &(video->controlListener));
1177 video->control_callback = callback;
1178 if (status != CHANNEL_RC_OK)
1181 video->controlListener->pInterface = video->wtsPlugin.pInterface;
1188 WLog_ERR(TAG,
"calloc for data callback failed!");
1189 return CHANNEL_RC_NO_MEMORY;
1192 callback->iface.OnNewChannelConnection = video_data_on_new_channel_connection;
1193 callback->plugin = plugin;
1194 callback->channel_mgr = channelMgr;
1196 status = channelMgr->CreateListener(channelMgr, VIDEO_DATA_DVC_CHANNEL_NAME, 0,
1197 &callback->iface, &(video->dataListener));
1198 video->data_callback = callback;
1199 if (status == CHANNEL_RC_OK)
1200 video->dataListener->pInterface = video->wtsPlugin.pInterface;
1203 if (status == CHANNEL_RC_OK)
1204 video->context->priv->timerID =
1205 freerdp_timer_add(video->rdpcontext, 20000000, timer_cb, video->context,
true);
1206 video->initialized = video->context->priv->timerID != 0;
1207 if (!video->initialized)
1208 status = ERROR_INTERNAL_ERROR;
1218static UINT video_plugin_terminated(IWTSPlugin* pPlugin)
1220 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)pPlugin;
1222 return CHANNEL_RC_INVALID_INSTANCE;
1224 if (video->context && video->context->priv)
1225 freerdp_timer_remove(video->rdpcontext, video->context->priv->timerID);
1227 if (video->control_callback)
1229 IWTSVirtualChannelManager* mgr = video->control_callback->channel_mgr;
1231 IFCALL(mgr->DestroyListener, mgr, video->controlListener);
1233 if (video->data_callback)
1235 IWTSVirtualChannelManager* mgr = video->data_callback->channel_mgr;
1237 IFCALL(mgr->DestroyListener, mgr, video->dataListener);
1241 VideoClientContextPriv_free(video->context->priv);
1243 free(video->control_callback);
1244 free(video->data_callback);
1245 free(video->wtsPlugin.pInterface);
1247 return CHANNEL_RC_OK;
1259FREERDP_ENTRY_POINT(UINT VCAPITYPE video_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1261 UINT error = ERROR_INTERNAL_ERROR;
1263 VIDEO_PLUGIN* videoPlugin = (VIDEO_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints,
"video");
1266 videoPlugin = (VIDEO_PLUGIN*)calloc(1,
sizeof(VIDEO_PLUGIN));
1269 WLog_ERR(TAG,
"calloc failed!");
1270 return CHANNEL_RC_NO_MEMORY;
1273 videoPlugin->wtsPlugin.Initialize = video_plugin_initialize;
1274 videoPlugin->wtsPlugin.Connected =
nullptr;
1275 videoPlugin->wtsPlugin.Disconnected =
nullptr;
1276 videoPlugin->wtsPlugin.Terminated = video_plugin_terminated;
1278 VideoClientContext* videoContext =
1279 (VideoClientContext*)calloc(1,
sizeof(VideoClientContext));
1282 WLog_ERR(TAG,
"calloc failed!");
1284 return CHANNEL_RC_NO_MEMORY;
1287 VideoClientContextPriv* priv = VideoClientContextPriv_new(videoContext);
1290 WLog_ERR(TAG,
"VideoClientContextPriv_new failed!");
1293 return CHANNEL_RC_NO_MEMORY;
1296 videoContext->handle = (
void*)videoPlugin;
1297 videoContext->priv = priv;
1298 videoContext->timer = video_timer;
1299 videoContext->setGeometry = video_client_context_set_geometry;
1301 videoPlugin->wtsPlugin.pInterface = (
void*)videoContext;
1302 videoPlugin->context = videoContext;
1303 videoPlugin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1304 if (videoPlugin->rdpcontext)
1305 error = pEntryPoints->RegisterPlugin(pEntryPoints,
"video", &videoPlugin->wtsPlugin);
1309 WLog_ERR(TAG,
"could not get video Plugin.");
1310 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