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))
708 const size_t frameSize = 1ull * frame->scanline * frame->h;
709 const size_t surfaceSize = 1ull * surface->scanline * surface->alignedHeight;
714 if (frameSize > surfaceSize)
715 WLog_WARN(TAG,
"dropping stale frame of %" PRIuz
" bytes, surface holds %" PRIuz,
716 frameSize, surfaceSize);
719 priv->publishedFrames++;
720 memcpy(surface->data, frame->surfaceData, frameSize);
722 WINPR_ASSERT(video->showSurface);
723 if (!video->showSurface(video, surface, presentation->ScaledWidth,
724 presentation->ScaledHeight))
725 WLog_WARN(TAG,
"showSurface failed");
728 VideoFrame_free(priv, frame);
731 if (priv->nextFeedbackTime < now)
736 if (priv->publishedFrames && priv->currentPresentation)
738 UINT32 computedRate = 0;
740 if (!PresentationContext_ref(priv->currentPresentation))
741 WLog_WARN(TAG,
"PresentationContext_ref(priv->currentPresentation) failed");
743 if (priv->droppedFrames)
750 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
754 computedRate = priv->lastSentRate - 2;
765 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
766 computedRate = XF_VIDEO_UNLIMITED_RATE;
769 computedRate = priv->lastSentRate + 2;
770 if (computedRate > XF_VIDEO_UNLIMITED_RATE)
771 computedRate = XF_VIDEO_UNLIMITED_RATE;
775 if (computedRate != priv->lastSentRate)
779 WINPR_ASSERT(priv->currentPresentation);
780 notif.PresentationId = priv->currentPresentation->PresentationId;
781 notif.NotificationType = TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE;
782 if (computedRate == XF_VIDEO_UNLIMITED_RATE)
784 notif.FramerateOverride.Flags = 0x01;
785 notif.FramerateOverride.DesiredFrameRate = 0x00;
789 notif.FramerateOverride.Flags = 0x02;
790 notif.FramerateOverride.DesiredFrameRate = computedRate;
793 video_control_send_client_notification(video, ¬if);
794 priv->lastSentRate = computedRate;
797 "server notified with rate %" PRIu32
" published=%" PRIu32
799 priv->lastSentRate, priv->publishedFrames, priv->droppedFrames);
802 PresentationContext_unref(&priv->currentPresentation);
805 priv->droppedFrames = 0;
806 priv->publishedFrames = 0;
807 priv->nextFeedbackTime = now + 1000;
809 LeaveCriticalSection(&priv->framesLock);
813static UINT video_VideoData(VideoClientContext* context,
const TSMM_VIDEO_DATA* data)
816 UINT res = CHANNEL_RC_OK;
818 WINPR_ASSERT(context);
821 VideoClientContextPriv* priv = context->priv;
824 PresentationContext* presentation = priv->currentPresentation;
827 WLog_ERR(TAG,
"no current presentation");
828 return CHANNEL_RC_OK;
831 if (!PresentationContext_ref(presentation))
832 return ERROR_INTERNAL_ERROR;
834 EnterCriticalSection(&priv->framesLock);
835 if (presentation->PresentationId != data->PresentationId)
837 WLog_ERR(TAG,
"current presentation id=%" PRIu8
" doesn't match data id=%" PRIu8,
838 presentation->PresentationId, data->PresentationId);
842 if (!Stream_EnsureRemainingCapacity(presentation->currentSample, data->cbSample))
844 WLog_ERR(TAG,
"unable to expand the current packet");
845 res = CHANNEL_RC_NO_MEMORY;
849 Stream_Write(presentation->currentSample, data->pSample, data->cbSample);
851 if (data->CurrentPacketIndex == data->PacketsInSample)
854 H264_CONTEXT* h264 = presentation->h264;
855 const UINT64 startTime = winpr_GetTickCount64NS();
856 MAPPED_GEOMETRY* geom = presentation->geometry;
858 const RECTANGLE_16 rect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedWidth),
859 WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedHeight) };
860 Stream_SealLength(presentation->currentSample);
861 Stream_ResetPosition(presentation->currentSample);
863 if (data->SampleNumber == 1)
865 presentation->lastPublishTime = startTime;
868 presentation->lastPublishTime += 100ull * data->hnsDuration;
869 const size_t len = Stream_Length(presentation->currentSample);
870 if (len > UINT32_MAX)
873 BOOL enqueueResult = 0;
874 VideoFrame* frame = VideoFrame_new(priv, presentation, geom);
877 WLog_ERR(TAG,
"unable to create frame");
878 res = CHANNEL_RC_NO_MEMORY;
882 status = avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
883 frame->surfaceData, surface->format, surface->scanline,
884 surface->alignedWidth, surface->alignedHeight, &rect, 1);
887 VideoFrame_free(priv, frame);
891 enqueueResult = Queue_Enqueue(priv->frames, frame);
895 WLog_ERR(TAG,
"unable to enqueue frame");
896 VideoFrame_free(priv, frame);
897 res = CHANNEL_RC_NO_MEMORY;
902 WLog_DBG(TAG,
"scheduling frame in %" PRIu64
" ns", (frame->publishTime - startTime));
906 PresentationContext_unref(&priv->currentPresentation);
907 LeaveCriticalSection(&priv->framesLock);
913static UINT video_data_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
917 UINT32 packetType = 0;
920 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin;
923 VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface;
925 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
926 return ERROR_INVALID_DATA;
928 Stream_Read_UINT32(s, cbSize);
931 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected >= 8", cbSize);
932 return ERROR_INVALID_DATA;
935 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
936 return ERROR_INVALID_DATA;
938 Stream_Read_UINT32(s, packetType);
939 if (packetType != TSMM_PACKET_TYPE_VIDEO_DATA)
941 WLog_ERR(TAG,
"only expecting VIDEO_DATA on the data channel");
942 return ERROR_INVALID_DATA;
945 if (!Stream_CheckAndLogRequiredLength(TAG, s, 32))
946 return ERROR_INVALID_DATA;
948 Stream_Read_UINT8(s, data.PresentationId);
949 Stream_Read_UINT8(s, data.Version);
950 Stream_Read_UINT8(s, data.Flags);
951 Stream_Seek_UINT8(s);
952 Stream_Read_UINT64(s, data.hnsTimestamp);
953 Stream_Read_UINT64(s, data.hnsDuration);
954 Stream_Read_UINT16(s, data.CurrentPacketIndex);
955 Stream_Read_UINT16(s, data.PacketsInSample);
956 Stream_Read_UINT32(s, data.SampleNumber);
957 Stream_Read_UINT32(s, data.cbSample);
958 if (!Stream_CheckAndLogRequiredLength(TAG, s, data.cbSample))
959 return ERROR_INVALID_DATA;
960 data.pSample = Stream_Pointer(s);
970 return video_VideoData(context, &data);
979static UINT video_control_on_close(IWTSVirtualChannelCallback* pChannelCallback)
981 if (pChannelCallback)
984 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
985 if (video && video->control_callback)
987 video->control_callback->channel_callback =
nullptr;
990 free(pChannelCallback);
991 return CHANNEL_RC_OK;
995static UINT video_data_on_close(IWTSVirtualChannelCallback* pChannelCallback)
997 if (pChannelCallback)
1000 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1001 if (video && video->data_callback)
1003 video->data_callback->channel_callback =
nullptr;
1006 free(pChannelCallback);
1007 return CHANNEL_RC_OK;
1017static UINT video_control_on_new_channel_connection(IWTSListenerCallback* listenerCallback,
1018 IWTSVirtualChannel* channel,
1019 WINPR_ATTR_UNUSED BYTE* Data,
1020 WINPR_ATTR_UNUSED BOOL* pbAccept,
1021 IWTSVirtualChannelCallback** ppCallback)
1030 WLog_ERR(TAG,
"calloc failed!");
1031 return CHANNEL_RC_NO_MEMORY;
1034 callback->iface.OnDataReceived = video_control_on_data_received;
1035 callback->iface.OnClose = video_control_on_close;
1036 callback->plugin = listener_callback->plugin;
1037 callback->channel_mgr = listener_callback->channel_mgr;
1038 callback->channel = channel;
1039 listener_callback->channel_callback = callback;
1041 *ppCallback = &callback->iface;
1043 return CHANNEL_RC_OK;
1048static UINT video_data_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1049 IWTSVirtualChannel* pChannel,
1050 WINPR_ATTR_UNUSED BYTE* Data,
1051 WINPR_ATTR_UNUSED BOOL* pbAccept,
1052 IWTSVirtualChannelCallback** ppCallback)
1061 WLog_ERR(TAG,
"calloc failed!");
1062 return CHANNEL_RC_NO_MEMORY;
1065 callback->iface.OnDataReceived = video_data_on_data_received;
1066 callback->iface.OnClose = video_data_on_close;
1067 callback->plugin = listener_callback->plugin;
1068 callback->channel_mgr = listener_callback->channel_mgr;
1069 callback->channel = pChannel;
1070 listener_callback->channel_callback = callback;
1072 *ppCallback = &callback->iface;
1074 return CHANNEL_RC_OK;
1078static uint64_t timer_cb(WINPR_ATTR_UNUSED rdpContext* context,
void* userdata,
1079 WINPR_ATTR_UNUSED FreeRDP_TimerID timerID, uint64_t timestamp,
1082 VideoClientContext* video = userdata;
1088 video->timer(video, timestamp);
1099static UINT video_plugin_initialize(IWTSPlugin* plugin, IWTSVirtualChannelManager* channelMgr)
1102 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)plugin;
1104 if (video->initialized)
1106 WLog_ERR(TAG,
"[%s] channel initialized twice, aborting", VIDEO_CONTROL_DVC_CHANNEL_NAME);
1107 return ERROR_INVALID_DATA;
1115 WLog_ERR(TAG,
"calloc for control callback failed!");
1116 return CHANNEL_RC_NO_MEMORY;
1119 callback->iface.OnNewChannelConnection = video_control_on_new_channel_connection;
1120 callback->plugin = plugin;
1121 callback->channel_mgr = channelMgr;
1123 status = channelMgr->CreateListener(channelMgr, VIDEO_CONTROL_DVC_CHANNEL_NAME, 0,
1124 &callback->iface, &(video->controlListener));
1125 video->control_callback = callback;
1126 if (status != CHANNEL_RC_OK)
1129 video->controlListener->pInterface = video->wtsPlugin.pInterface;
1136 WLog_ERR(TAG,
"calloc for data callback failed!");
1137 return CHANNEL_RC_NO_MEMORY;
1140 callback->iface.OnNewChannelConnection = video_data_on_new_channel_connection;
1141 callback->plugin = plugin;
1142 callback->channel_mgr = channelMgr;
1144 status = channelMgr->CreateListener(channelMgr, VIDEO_DATA_DVC_CHANNEL_NAME, 0,
1145 &callback->iface, &(video->dataListener));
1146 video->data_callback = callback;
1147 if (status == CHANNEL_RC_OK)
1148 video->dataListener->pInterface = video->wtsPlugin.pInterface;
1151 if (status == CHANNEL_RC_OK)
1152 video->context->priv->timerID =
1153 freerdp_timer_add(video->rdpcontext, 20000000, timer_cb, video->context,
true);
1154 video->initialized = video->context->priv->timerID != 0;
1155 if (!video->initialized)
1156 status = ERROR_INTERNAL_ERROR;
1166static UINT video_plugin_terminated(IWTSPlugin* pPlugin)
1168 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)pPlugin;
1170 return CHANNEL_RC_INVALID_INSTANCE;
1172 if (video->context && video->context->priv)
1173 freerdp_timer_remove(video->rdpcontext, video->context->priv->timerID);
1175 if (video->control_callback)
1177 IWTSVirtualChannelManager* mgr = video->control_callback->channel_mgr;
1179 IFCALL(mgr->DestroyListener, mgr, video->controlListener);
1181 if (video->data_callback)
1183 IWTSVirtualChannelManager* mgr = video->data_callback->channel_mgr;
1185 IFCALL(mgr->DestroyListener, mgr, video->dataListener);
1189 VideoClientContextPriv_free(video->context->priv);
1191 free(video->control_callback);
1192 free(video->data_callback);
1193 free(video->wtsPlugin.pInterface);
1195 return CHANNEL_RC_OK;
1207FREERDP_ENTRY_POINT(UINT VCAPITYPE video_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1209 UINT error = ERROR_INTERNAL_ERROR;
1211 VIDEO_PLUGIN* videoPlugin = (VIDEO_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints,
"video");
1214 videoPlugin = (VIDEO_PLUGIN*)calloc(1,
sizeof(VIDEO_PLUGIN));
1217 WLog_ERR(TAG,
"calloc failed!");
1218 return CHANNEL_RC_NO_MEMORY;
1221 videoPlugin->wtsPlugin.Initialize = video_plugin_initialize;
1222 videoPlugin->wtsPlugin.Connected =
nullptr;
1223 videoPlugin->wtsPlugin.Disconnected =
nullptr;
1224 videoPlugin->wtsPlugin.Terminated = video_plugin_terminated;
1226 VideoClientContext* videoContext =
1227 (VideoClientContext*)calloc(1,
sizeof(VideoClientContext));
1230 WLog_ERR(TAG,
"calloc failed!");
1232 return CHANNEL_RC_NO_MEMORY;
1235 VideoClientContextPriv* priv = VideoClientContextPriv_new(videoContext);
1238 WLog_ERR(TAG,
"VideoClientContextPriv_new failed!");
1241 return CHANNEL_RC_NO_MEMORY;
1244 videoContext->handle = (
void*)videoPlugin;
1245 videoContext->priv = priv;
1246 videoContext->timer = video_timer;
1247 videoContext->setGeometry = video_client_context_set_geometry;
1249 videoPlugin->wtsPlugin.pInterface = (
void*)videoContext;
1250 videoPlugin->context = videoContext;
1251 videoPlugin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1252 if (videoPlugin->rdpcontext)
1253 error = pEntryPoints->RegisterPlugin(pEntryPoints,
"video", &videoPlugin->wtsPlugin);
1257 WLog_ERR(TAG,
"could not get video Plugin.");
1258 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