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/addin.h>
38#include <freerdp/primitives.h>
39#include <freerdp/client/channels.h>
40#include <freerdp/client/geometry.h>
41#include <freerdp/client/video.h>
42#include <freerdp/channels/log.h>
43#include <freerdp/codec/h264.h>
44#include <freerdp/codec/yuv.h>
45#include <freerdp/timer.h>
47#define TAG CHANNELS_TAG("video.client")
49#include "video_main.h"
55 IWTSListener* controlListener;
56 IWTSListener* dataListener;
60 VideoClientContext* context;
62 rdpContext* rdpcontext;
65#define XF_VIDEO_UNLIMITED_RATE 31
67static const BYTE MFVideoFormat_H264[] = {
'H',
'2',
'6',
'4', 0x00, 0x00, 0x10, 0x00,
68 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 };
76 UINT64 startTimeStamp;
79 UINT64 lastPublishTime;
80 UINT64 nextPublishTime;
81 volatile LONG refCounter;
84 MAPPED_GEOMETRY* geometry;
85 VideoClientContext* video;
93 MAPPED_GEOMETRY* geometry;
100struct s_VideoClientContextPriv
102 VideoClientContext* video;
103 GeometryClientContext* geometry;
106 wBufferPool* surfacePool;
107 UINT32 publishedFrames;
108 UINT32 droppedFrames;
110 UINT64 nextFeedbackTime;
111 PresentationContext* currentPresentation;
112 FreeRDP_TimerID timerID;
115static void PresentationContext_unref(PresentationContext** presentation);
116static void VideoClientContextPriv_free(VideoClientContextPriv* priv);
119static const char* video_command_name(BYTE cmd)
123 case TSMM_START_PRESENTATION:
125 case TSMM_STOP_PRESENTATION:
132static void video_client_context_set_geometry(VideoClientContext* video,
133 GeometryClientContext* geometry)
136 WINPR_ASSERT(video->priv);
137 video->priv->geometry = geometry;
140WINPR_ATTR_MALLOC(VideoClientContextPriv_free, 1)
141static VideoClientContextPriv* VideoClientContextPriv_new(VideoClientContext* video)
143 VideoClientContextPriv* ret =
nullptr;
146 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 size_t s = 4ULL * width * height;
217 PresentationContext* ret =
nullptr;
224 ret = calloc(1,
sizeof(*ret));
229 ret->PresentationId = PresentationId;
231 ret->h264 = h264_context_new(FALSE);
234 WLog_ERR(TAG,
"unable to create a h264 context");
237 if (!h264_context_reset(ret->h264, width, height))
240 ret->currentSample = Stream_New(
nullptr, 4096);
241 if (!ret->currentSample)
243 WLog_ERR(TAG,
"unable to create current packet stream");
247 ret->surface = video->createSurface(video, x, y, width, height);
250 WLog_ERR(TAG,
"unable to create surface");
254 if (!PresentationContext_ref(ret))
260 PresentationContext_free(ret);
264static void PresentationContext_unref(PresentationContext** ppresentation)
266 WINPR_ASSERT(ppresentation);
268 PresentationContext* presentation = *ppresentation;
272 if (InterlockedDecrement(&presentation->refCounter) > 0)
274 *ppresentation =
nullptr;
276 PresentationContext_free(presentation);
279static void VideoFrame_free(VideoClientContextPriv* priv, VideoFrame* frame)
285 mappedGeometryUnref(frame->geometry);
287 BufferPool_Return(priv->surfacePool, frame->surfaceData);
291WINPR_ATTR_MALLOC(VideoFrame_free, 1)
292static VideoFrame* VideoFrame_new(VideoClientContextPriv* priv, PresentationContext* presentation,
293 MAPPED_GEOMETRY* geom)
296 WINPR_ASSERT(presentation);
300 WINPR_ASSERT(surface);
302 VideoFrame* frame = calloc(1,
sizeof(VideoFrame));
305 frame->PresentationId = presentation->PresentationId;
307 mappedGeometryRef(geom);
309 frame->publishTime = presentation->lastPublishTime;
310 frame->geometry = geom;
311 frame->w = surface->alignedWidth;
312 frame->h = surface->alignedHeight;
313 frame->scanline = surface->scanline;
315 frame->surfaceData = BufferPool_Take(priv->surfacePool, 1ll * frame->scanline * frame->h);
316 if (!frame->surfaceData)
322 VideoFrame_free(priv, frame);
326void VideoClientContextPriv_free(VideoClientContextPriv* priv)
331 EnterCriticalSection(&priv->framesLock);
335 while (Queue_Count(priv->frames))
337 VideoFrame* frame = Queue_Dequeue(priv->frames);
339 VideoFrame_free(priv, frame);
343 Queue_Free(priv->frames);
344 LeaveCriticalSection(&priv->framesLock);
346 DeleteCriticalSection(&priv->framesLock);
348 if (priv->currentPresentation)
349 PresentationContext_unref(&priv->currentPresentation);
351 BufferPool_Free(priv->surfacePool);
356static UINT video_channel_write(VIDEO_PLUGIN* video,
const BYTE* data, UINT32 length)
360 if (!video->control_callback || !video->control_callback->channel_callback)
361 return ERROR_BAD_CONFIGURATION;
362 IWTSVirtualChannel* channel = video->control_callback->channel_callback->channel;
363 if (!channel || !channel->Write)
364 return ERROR_BAD_CONFIGURATION;
365 return channel->Write(channel, length, data,
nullptr);
369static UINT video_control_send_presentation_response(VideoClientContext* context,
372 BYTE buf[12] = WINPR_C_ARRAY_INIT;
374 WINPR_ASSERT(context);
377 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)context->handle;
380 wStream* s = Stream_New(buf, 12);
382 return CHANNEL_RC_NO_MEMORY;
384 Stream_Write_UINT32(s, 12);
385 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_PRESENTATION_RESPONSE);
386 Stream_Write_UINT8(s, resp->PresentationId);
388 Stream_SealLength(s);
389 Stream_Free(s, FALSE);
391 return video_channel_write(video, buf,
sizeof(buf));
395static BOOL video_onMappedGeometryUpdate(MAPPED_GEOMETRY* geometry)
397 WINPR_ASSERT(geometry);
399 PresentationContext* presentation = (PresentationContext*)geometry->custom;
400 WINPR_ASSERT(presentation);
402 RDP_RECT* r = &geometry->geometry.boundingRect;
404 "geometry updated topGeom=(%" PRId32
",%" PRId32
"-%" PRId32
"x%" PRId32
405 ") geom=(%" PRId32
",%" PRId32
"-%" PRId32
"x%" PRId32
") rects=(%" PRId16
",%" PRId16
406 "-%" PRId16
"x%" PRId16
")",
407 geometry->topLevelLeft, geometry->topLevelTop,
408 geometry->topLevelRight - geometry->topLevelLeft,
409 geometry->topLevelBottom - geometry->topLevelTop,
411 geometry->left, geometry->top, geometry->right - geometry->left,
412 geometry->bottom - geometry->top,
414 r->x, r->y, r->width, r->height);
416 WINPR_ASSERT(presentation->surface);
417 presentation->surface->x =
418 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelLeft + geometry->left);
419 presentation->surface->y =
420 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelTop + geometry->top);
426static BOOL video_onMappedGeometryClear(MAPPED_GEOMETRY* geometry)
428 WINPR_ASSERT(geometry);
430 PresentationContext* presentation = (PresentationContext*)geometry->custom;
431 WINPR_ASSERT(presentation);
433 mappedGeometryUnref(presentation->geometry);
434 presentation->geometry =
nullptr;
439static UINT video_PresentationRequest(VideoClientContext* video,
442 UINT ret = CHANNEL_RC_OK;
447 VideoClientContextPriv* priv = video->priv;
450 EnterCriticalSection(&priv->framesLock);
451 if (req->Command == TSMM_START_PRESENTATION)
453 MAPPED_GEOMETRY* geom =
nullptr;
456 if (memcmp(req->VideoSubtypeId, MFVideoFormat_H264, 16) != 0)
458 WLog_ERR(TAG,
"not a H264 video, ignoring request");
462 if (priv->currentPresentation)
464 if (priv->currentPresentation->PresentationId == req->PresentationId)
466 WLog_ERR(TAG,
"ignoring start request for existing presentation %" PRIu8,
467 req->PresentationId);
471 WLog_ERR(TAG,
"releasing current presentation %" PRIu8, req->PresentationId);
472 PresentationContext_unref(&priv->currentPresentation);
477 WLog_ERR(TAG,
"geometry channel not ready, ignoring request");
481 geom = HashTable_GetItemValue(priv->geometry->geometries, &(req->GeometryMappingId));
484 WLog_ERR(TAG,
"geometry mapping 0x%" PRIx64
" not registered", req->GeometryMappingId);
488 WLog_DBG(TAG,
"creating presentation 0x%x", req->PresentationId);
489 priv->currentPresentation = PresentationContext_new(
490 video, req->PresentationId,
491 WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelLeft + geom->left),
492 WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelTop + geom->top), req->SourceWidth,
494 if (!priv->currentPresentation)
496 WLog_ERR(TAG,
"unable to create presentation video");
497 ret = CHANNEL_RC_NO_MEMORY;
501 mappedGeometryRef(geom);
502 priv->currentPresentation->geometry = geom;
504 priv->currentPresentation->video = video;
505 priv->currentPresentation->ScaledWidth = req->ScaledWidth;
506 priv->currentPresentation->ScaledHeight = req->ScaledHeight;
508 geom->custom = priv->currentPresentation;
509 geom->MappedGeometryUpdate = video_onMappedGeometryUpdate;
510 geom->MappedGeometryClear = video_onMappedGeometryClear;
513 resp.PresentationId = req->PresentationId;
514 ret = video_control_send_presentation_response(video, &resp);
516 else if (req->Command == TSMM_STOP_PRESENTATION)
518 WLog_DBG(TAG,
"stopping presentation 0x%x", req->PresentationId);
519 if (!priv->currentPresentation)
521 WLog_ERR(TAG,
"unknown presentation to stop %" PRIu8, req->PresentationId);
525 priv->droppedFrames = 0;
526 priv->publishedFrames = 0;
527 PresentationContext_unref(&priv->currentPresentation);
531 LeaveCriticalSection(&priv->framesLock);
536static UINT video_read_tsmm_presentation_req(VideoClientContext* context,
wStream* s)
540 WINPR_ASSERT(context);
543 if (!Stream_CheckAndLogRequiredLength(TAG, s, 60))
544 return ERROR_INVALID_DATA;
546 Stream_Read_UINT8(s, req.PresentationId);
547 Stream_Read_UINT8(s, req.Version);
548 Stream_Read_UINT8(s, req.Command);
549 Stream_Read_UINT8(s, req.FrameRate);
551 Stream_Seek_UINT16(s);
552 Stream_Seek_UINT16(s);
554 Stream_Read_UINT32(s, req.SourceWidth);
555 Stream_Read_UINT32(s, req.SourceHeight);
556 Stream_Read_UINT32(s, req.ScaledWidth);
557 Stream_Read_UINT32(s, req.ScaledHeight);
558 Stream_Read_UINT64(s, req.hnsTimestampOffset);
559 Stream_Read_UINT64(s, req.GeometryMappingId);
560 Stream_Read(s, req.VideoSubtypeId, 16);
562 Stream_Read_UINT32(s, req.cbExtra);
564 if (!Stream_CheckAndLogRequiredLength(TAG, s, req.cbExtra))
565 return ERROR_INVALID_DATA;
567 req.pExtraData = Stream_Pointer(s);
570 "presentationReq: id:%" PRIu8
" version:%" PRIu8
571 " command:%s srcWidth/srcHeight=%" PRIu32
"x%" PRIu32
" scaled Width/Height=%" PRIu32
572 "x%" PRIu32
" timestamp=%" PRIu64
" mappingId=%" PRIx64
"",
573 req.PresentationId, req.Version, video_command_name(req.Command), req.SourceWidth,
574 req.SourceHeight, req.ScaledWidth, req.ScaledHeight, req.hnsTimestampOffset,
575 req.GeometryMappingId);
577 return video_PresentationRequest(context, &req);
586static UINT video_control_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
589 UINT ret = CHANNEL_RC_OK;
591 UINT32 packetType = 0;
593 WINPR_ASSERT(callback);
596 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin;
599 VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface;
600 WINPR_ASSERT(context);
602 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
603 return ERROR_INVALID_DATA;
605 Stream_Read_UINT32(s, cbSize);
608 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected 8", cbSize);
609 return ERROR_INVALID_DATA;
611 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
612 return ERROR_INVALID_DATA;
614 Stream_Read_UINT32(s, packetType);
617 case TSMM_PACKET_TYPE_PRESENTATION_REQUEST:
618 ret = video_read_tsmm_presentation_req(context, s);
621 WLog_ERR(TAG,
"not expecting packet type %" PRIu32
"", packetType);
622 ret = ERROR_UNSUPPORTED_TYPE;
629static UINT video_control_send_client_notification(VideoClientContext* context,
632 BYTE buf[100] = WINPR_C_ARRAY_INIT;
634 WINPR_ASSERT(context);
637 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)context->handle;
640 wStream* s = Stream_New(buf, 32);
642 return CHANNEL_RC_NO_MEMORY;
645 Stream_Seek_UINT32(s);
646 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_CLIENT_NOTIFICATION);
647 Stream_Write_UINT8(s, notif->PresentationId);
648 Stream_Write_UINT8(s, notif->NotificationType);
650 if (notif->NotificationType == TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE)
652 Stream_Write_UINT32(s, 16);
655 Stream_Write_UINT32(s, notif->FramerateOverride.Flags);
656 Stream_Write_UINT32(s, notif->FramerateOverride.DesiredFrameRate);
657 Stream_Zero(s, 4ULL * 2ULL);
663 Stream_Write_UINT32(s, 0);
666 Stream_SealLength(s);
667 Stream_ResetPosition(s);
668 Stream_Write_UINT32(s, cbSize);
669 Stream_Free(s, FALSE);
671 return video_channel_write(video, buf, cbSize);
674static void video_timer(VideoClientContext* video, UINT64 now)
676 VideoFrame* frame =
nullptr;
680 VideoClientContextPriv* priv = video->priv;
683 EnterCriticalSection(&priv->framesLock);
684 PresentationContext* presentation = video->priv->currentPresentation;
687 const VideoFrame* peekFrame = (VideoFrame*)Queue_Peek(priv->frames);
691 if (peekFrame->publishTime > now)
696 WLog_DBG(TAG,
"dropping frame @%" PRIu64, frame->publishTime);
697 priv->droppedFrames++;
698 VideoFrame_free(priv, frame);
700 frame = Queue_Dequeue(priv->frames);
705 if (presentation && (presentation->PresentationId == frame->PresentationId))
707 priv->publishedFrames++;
708 memcpy(presentation->surface->data, frame->surfaceData,
709 1ull * frame->scanline * frame->h);
711 WINPR_ASSERT(video->showSurface);
712 if (!video->showSurface(video, presentation->surface, presentation->ScaledWidth,
713 presentation->ScaledHeight))
714 WLog_WARN(TAG,
"showSurface failed");
716 VideoFrame_free(priv, frame);
719 if (priv->nextFeedbackTime < now)
724 if (priv->publishedFrames && priv->currentPresentation)
726 UINT32 computedRate = 0;
728 if (!PresentationContext_ref(priv->currentPresentation))
729 WLog_WARN(TAG,
"PresentationContext_ref(priv->currentPresentation) failed");
731 if (priv->droppedFrames)
738 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
742 computedRate = priv->lastSentRate - 2;
753 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
754 computedRate = XF_VIDEO_UNLIMITED_RATE;
757 computedRate = priv->lastSentRate + 2;
758 if (computedRate > XF_VIDEO_UNLIMITED_RATE)
759 computedRate = XF_VIDEO_UNLIMITED_RATE;
763 if (computedRate != priv->lastSentRate)
767 WINPR_ASSERT(priv->currentPresentation);
768 notif.PresentationId = priv->currentPresentation->PresentationId;
769 notif.NotificationType = TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE;
770 if (computedRate == XF_VIDEO_UNLIMITED_RATE)
772 notif.FramerateOverride.Flags = 0x01;
773 notif.FramerateOverride.DesiredFrameRate = 0x00;
777 notif.FramerateOverride.Flags = 0x02;
778 notif.FramerateOverride.DesiredFrameRate = computedRate;
781 video_control_send_client_notification(video, ¬if);
782 priv->lastSentRate = computedRate;
785 "server notified with rate %" PRIu32
" published=%" PRIu32
787 priv->lastSentRate, priv->publishedFrames, priv->droppedFrames);
790 PresentationContext_unref(&priv->currentPresentation);
793 priv->droppedFrames = 0;
794 priv->publishedFrames = 0;
795 priv->nextFeedbackTime = now + 1000;
797 LeaveCriticalSection(&priv->framesLock);
801static UINT video_VideoData(VideoClientContext* context,
const TSMM_VIDEO_DATA* data)
804 UINT res = CHANNEL_RC_OK;
806 WINPR_ASSERT(context);
809 VideoClientContextPriv* priv = context->priv;
812 PresentationContext* presentation = priv->currentPresentation;
815 WLog_ERR(TAG,
"no current presentation");
816 return CHANNEL_RC_OK;
819 if (!PresentationContext_ref(presentation))
820 return ERROR_INTERNAL_ERROR;
822 EnterCriticalSection(&priv->framesLock);
823 if (presentation->PresentationId != data->PresentationId)
825 WLog_ERR(TAG,
"current presentation id=%" PRIu8
" doesn't match data id=%" PRIu8,
826 presentation->PresentationId, data->PresentationId);
830 if (!Stream_EnsureRemainingCapacity(presentation->currentSample, data->cbSample))
832 WLog_ERR(TAG,
"unable to expand the current packet");
833 res = CHANNEL_RC_NO_MEMORY;
837 Stream_Write(presentation->currentSample, data->pSample, data->cbSample);
839 if (data->CurrentPacketIndex == data->PacketsInSample)
842 H264_CONTEXT* h264 = presentation->h264;
843 const UINT64 startTime = winpr_GetTickCount64NS();
844 MAPPED_GEOMETRY* geom = presentation->geometry;
846 const RECTANGLE_16 rect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedWidth),
847 WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedHeight) };
848 Stream_SealLength(presentation->currentSample);
849 Stream_ResetPosition(presentation->currentSample);
851 if (data->SampleNumber == 1)
853 presentation->lastPublishTime = startTime;
856 presentation->lastPublishTime += 100ull * data->hnsDuration;
857 const size_t len = Stream_Length(presentation->currentSample);
858 if (len > UINT32_MAX)
861 BOOL enqueueResult = 0;
862 VideoFrame* frame = VideoFrame_new(priv, presentation, geom);
865 WLog_ERR(TAG,
"unable to create frame");
866 res = CHANNEL_RC_NO_MEMORY;
870 status = avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
871 frame->surfaceData, surface->format, surface->scanline,
872 surface->alignedWidth, surface->alignedHeight, &rect, 1);
875 VideoFrame_free(priv, frame);
879 enqueueResult = Queue_Enqueue(priv->frames, frame);
883 WLog_ERR(TAG,
"unable to enqueue frame");
884 VideoFrame_free(priv, frame);
885 res = CHANNEL_RC_NO_MEMORY;
890 WLog_DBG(TAG,
"scheduling frame in %" PRIu64
" ns", (frame->publishTime - startTime));
894 PresentationContext_unref(&priv->currentPresentation);
895 LeaveCriticalSection(&priv->framesLock);
901static UINT video_data_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
905 UINT32 packetType = 0;
908 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin;
911 VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface;
913 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
914 return ERROR_INVALID_DATA;
916 Stream_Read_UINT32(s, cbSize);
919 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected >= 8", cbSize);
920 return ERROR_INVALID_DATA;
923 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
924 return ERROR_INVALID_DATA;
926 Stream_Read_UINT32(s, packetType);
927 if (packetType != TSMM_PACKET_TYPE_VIDEO_DATA)
929 WLog_ERR(TAG,
"only expecting VIDEO_DATA on the data channel");
930 return ERROR_INVALID_DATA;
933 if (!Stream_CheckAndLogRequiredLength(TAG, s, 32))
934 return ERROR_INVALID_DATA;
936 Stream_Read_UINT8(s, data.PresentationId);
937 Stream_Read_UINT8(s, data.Version);
938 Stream_Read_UINT8(s, data.Flags);
939 Stream_Seek_UINT8(s);
940 Stream_Read_UINT64(s, data.hnsTimestamp);
941 Stream_Read_UINT64(s, data.hnsDuration);
942 Stream_Read_UINT16(s, data.CurrentPacketIndex);
943 Stream_Read_UINT16(s, data.PacketsInSample);
944 Stream_Read_UINT32(s, data.SampleNumber);
945 Stream_Read_UINT32(s, data.cbSample);
946 if (!Stream_CheckAndLogRequiredLength(TAG, s, data.cbSample))
947 return ERROR_INVALID_DATA;
948 data.pSample = Stream_Pointer(s);
958 return video_VideoData(context, &data);
967static UINT video_control_on_close(IWTSVirtualChannelCallback* pChannelCallback)
969 if (pChannelCallback)
972 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
973 if (video && video->control_callback)
975 video->control_callback->channel_callback =
nullptr;
978 free(pChannelCallback);
979 return CHANNEL_RC_OK;
983static UINT video_data_on_close(IWTSVirtualChannelCallback* pChannelCallback)
985 if (pChannelCallback)
988 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
989 if (video && video->data_callback)
991 video->data_callback->channel_callback =
nullptr;
994 free(pChannelCallback);
995 return CHANNEL_RC_OK;
1005static UINT video_control_on_new_channel_connection(IWTSListenerCallback* listenerCallback,
1006 IWTSVirtualChannel* channel,
1007 WINPR_ATTR_UNUSED BYTE* Data,
1008 WINPR_ATTR_UNUSED BOOL* pbAccept,
1009 IWTSVirtualChannelCallback** ppCallback)
1018 WLog_ERR(TAG,
"calloc failed!");
1019 return CHANNEL_RC_NO_MEMORY;
1022 callback->iface.OnDataReceived = video_control_on_data_received;
1023 callback->iface.OnClose = video_control_on_close;
1024 callback->plugin = listener_callback->plugin;
1025 callback->channel_mgr = listener_callback->channel_mgr;
1026 callback->channel = channel;
1027 listener_callback->channel_callback = callback;
1029 *ppCallback = &callback->iface;
1031 return CHANNEL_RC_OK;
1036static UINT video_data_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1037 IWTSVirtualChannel* pChannel,
1038 WINPR_ATTR_UNUSED BYTE* Data,
1039 WINPR_ATTR_UNUSED BOOL* pbAccept,
1040 IWTSVirtualChannelCallback** ppCallback)
1049 WLog_ERR(TAG,
"calloc failed!");
1050 return CHANNEL_RC_NO_MEMORY;
1053 callback->iface.OnDataReceived = video_data_on_data_received;
1054 callback->iface.OnClose = video_data_on_close;
1055 callback->plugin = listener_callback->plugin;
1056 callback->channel_mgr = listener_callback->channel_mgr;
1057 callback->channel = pChannel;
1058 listener_callback->channel_callback = callback;
1060 *ppCallback = &callback->iface;
1062 return CHANNEL_RC_OK;
1066static uint64_t timer_cb(WINPR_ATTR_UNUSED rdpContext* context,
void* userdata,
1067 WINPR_ATTR_UNUSED FreeRDP_TimerID timerID, uint64_t timestamp,
1070 VideoClientContext* video = userdata;
1076 video->timer(video, timestamp);
1087static UINT video_plugin_initialize(IWTSPlugin* plugin, IWTSVirtualChannelManager* channelMgr)
1090 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)plugin;
1092 if (video->initialized)
1094 WLog_ERR(TAG,
"[%s] channel initialized twice, aborting", VIDEO_CONTROL_DVC_CHANNEL_NAME);
1095 return ERROR_INVALID_DATA;
1103 WLog_ERR(TAG,
"calloc for control callback failed!");
1104 return CHANNEL_RC_NO_MEMORY;
1107 callback->iface.OnNewChannelConnection = video_control_on_new_channel_connection;
1108 callback->plugin = plugin;
1109 callback->channel_mgr = channelMgr;
1111 status = channelMgr->CreateListener(channelMgr, VIDEO_CONTROL_DVC_CHANNEL_NAME, 0,
1112 &callback->iface, &(video->controlListener));
1113 video->control_callback = callback;
1114 if (status != CHANNEL_RC_OK)
1117 video->controlListener->pInterface = video->wtsPlugin.pInterface;
1124 WLog_ERR(TAG,
"calloc for data callback failed!");
1125 return CHANNEL_RC_NO_MEMORY;
1128 callback->iface.OnNewChannelConnection = video_data_on_new_channel_connection;
1129 callback->plugin = plugin;
1130 callback->channel_mgr = channelMgr;
1132 status = channelMgr->CreateListener(channelMgr, VIDEO_DATA_DVC_CHANNEL_NAME, 0,
1133 &callback->iface, &(video->dataListener));
1134 video->data_callback = callback;
1135 if (status == CHANNEL_RC_OK)
1136 video->dataListener->pInterface = video->wtsPlugin.pInterface;
1139 if (status == CHANNEL_RC_OK)
1140 video->context->priv->timerID =
1141 freerdp_timer_add(video->rdpcontext, 20000000, timer_cb, video->context,
true);
1142 video->initialized = video->context->priv->timerID != 0;
1143 if (!video->initialized)
1144 status = ERROR_INTERNAL_ERROR;
1154static UINT video_plugin_terminated(IWTSPlugin* pPlugin)
1156 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)pPlugin;
1158 return CHANNEL_RC_INVALID_INSTANCE;
1160 if (video->context && video->context->priv)
1161 freerdp_timer_remove(video->rdpcontext, video->context->priv->timerID);
1163 if (video->control_callback)
1165 IWTSVirtualChannelManager* mgr = video->control_callback->channel_mgr;
1167 IFCALL(mgr->DestroyListener, mgr, video->controlListener);
1169 if (video->data_callback)
1171 IWTSVirtualChannelManager* mgr = video->data_callback->channel_mgr;
1173 IFCALL(mgr->DestroyListener, mgr, video->dataListener);
1177 VideoClientContextPriv_free(video->context->priv);
1179 free(video->control_callback);
1180 free(video->data_callback);
1181 free(video->wtsPlugin.pInterface);
1183 return CHANNEL_RC_OK;
1195FREERDP_ENTRY_POINT(UINT VCAPITYPE video_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1197 UINT error = ERROR_INTERNAL_ERROR;
1199 VIDEO_PLUGIN* videoPlugin = (VIDEO_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints,
"video");
1202 videoPlugin = (VIDEO_PLUGIN*)calloc(1,
sizeof(VIDEO_PLUGIN));
1205 WLog_ERR(TAG,
"calloc failed!");
1206 return CHANNEL_RC_NO_MEMORY;
1209 videoPlugin->wtsPlugin.Initialize = video_plugin_initialize;
1210 videoPlugin->wtsPlugin.Connected =
nullptr;
1211 videoPlugin->wtsPlugin.Disconnected =
nullptr;
1212 videoPlugin->wtsPlugin.Terminated = video_plugin_terminated;
1214 VideoClientContext* videoContext =
1215 (VideoClientContext*)calloc(1,
sizeof(VideoClientContext));
1218 WLog_ERR(TAG,
"calloc failed!");
1220 return CHANNEL_RC_NO_MEMORY;
1223 VideoClientContextPriv* priv = VideoClientContextPriv_new(videoContext);
1226 WLog_ERR(TAG,
"VideoClientContextPriv_new failed!");
1229 return CHANNEL_RC_NO_MEMORY;
1232 videoContext->handle = (
void*)videoPlugin;
1233 videoContext->priv = priv;
1234 videoContext->timer = video_timer;
1235 videoContext->setGeometry = video_client_context_set_geometry;
1237 videoPlugin->wtsPlugin.pInterface = (
void*)videoContext;
1238 videoPlugin->context = videoContext;
1239 videoPlugin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1240 if (videoPlugin->rdpcontext)
1241 error = pEntryPoints->RegisterPlugin(pEntryPoints,
"video", &videoPlugin->wtsPlugin);
1245 WLog_ERR(TAG,
"could not get video Plugin.");
1246 return CHANNEL_RC_BAD_CHANNEL;
a client to server notification struct
presentation request struct
response to a TSMM_PRESENTATION_REQUEST
an implementation of surface used by the video channel