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);
138 video->priv->geometry = geometry;
141WINPR_ATTR_MALLOC(VideoClientContextPriv_free, 1)
142static VideoClientContextPriv* VideoClientContextPriv_new(VideoClientContext* video)
144 VideoClientContextPriv* ret =
nullptr;
147 ret = calloc(1,
sizeof(*ret));
151 ret->frames = Queue_New(TRUE, 10, 2);
154 WLog_ERR(TAG,
"unable to allocate frames queue");
158 ret->surfacePool = BufferPool_New(FALSE, 0, 16);
159 if (!ret->surfacePool)
161 WLog_ERR(TAG,
"unable to create surface pool");
165 if (!InitializeCriticalSectionAndSpinCount(&ret->framesLock, 4 * 1000))
167 WLog_ERR(TAG,
"unable to initialize frames lock");
176 ret->lastSentRate = 30;
180 VideoClientContextPriv_free(ret);
185static BOOL PresentationContext_ref(PresentationContext* presentation)
187 WINPR_ASSERT(presentation);
189 const LONG val = InterlockedIncrement(&presentation->refCounter);
193static void PresentationContext_free(PresentationContext* presentation)
198 MAPPED_GEOMETRY* geometry = presentation->geometry;
201 geometry->MappedGeometryUpdate =
nullptr;
202 geometry->MappedGeometryClear =
nullptr;
203 geometry->custom =
nullptr;
204 mappedGeometryUnref(geometry);
207 h264_context_free(presentation->h264);
208 Stream_Free(presentation->currentSample, TRUE);
209 presentation->video->deleteSurface(presentation->video, presentation->surface);
213WINPR_ATTR_MALLOC(PresentationContext_free, 1)
214static PresentationContext* PresentationContext_new(VideoClientContext* video, BYTE PresentationId,
215 UINT32 x, UINT32 y, UINT32 width, UINT32 height)
217 size_t s = 4ULL * width * height;
218 PresentationContext* ret =
nullptr;
225 ret = calloc(1,
sizeof(*ret));
230 ret->PresentationId = PresentationId;
232 ret->h264 = h264_context_new(FALSE);
235 WLog_ERR(TAG,
"unable to create a h264 context");
239 VIDEO_PLUGIN* plugin = (VIDEO_PLUGIN*)video->handle;
240 WINPR_ASSERT(plugin);
241 WINPR_ASSERT(plugin->rdpcontext);
242 if (!h264_context_set_option(
243 ret->h264, H264_CONTEXT_OPTION_HW_ACCEL,
246 if (!h264_context_reset(ret->h264, width, height))
249 ret->currentSample = Stream_New(
nullptr, 4096);
250 if (!ret->currentSample)
252 WLog_ERR(TAG,
"unable to create current packet stream");
256 ret->surface = video->createSurface(video, x, y, width, height);
259 WLog_ERR(TAG,
"unable to create surface");
263 if (!PresentationContext_ref(ret))
269 PresentationContext_free(ret);
273static void PresentationContext_unref(PresentationContext** ppresentation)
275 WINPR_ASSERT(ppresentation);
277 PresentationContext* presentation = *ppresentation;
281 if (InterlockedDecrement(&presentation->refCounter) > 0)
283 *ppresentation =
nullptr;
285 PresentationContext_free(presentation);
288static void VideoFrame_free(VideoClientContextPriv* priv, VideoFrame* frame)
294 mappedGeometryUnref(frame->geometry);
296 BufferPool_Return(priv->surfacePool, frame->surfaceData);
300WINPR_ATTR_MALLOC(VideoFrame_free, 1)
301static VideoFrame* VideoFrame_new(VideoClientContextPriv* priv, PresentationContext* presentation,
302 MAPPED_GEOMETRY* geom)
305 WINPR_ASSERT(presentation);
309 WINPR_ASSERT(surface);
311 VideoFrame* frame = calloc(1,
sizeof(VideoFrame));
314 frame->PresentationId = presentation->PresentationId;
316 mappedGeometryRef(geom);
318 frame->publishTime = presentation->lastPublishTime;
319 frame->geometry = geom;
320 frame->w = surface->alignedWidth;
321 frame->h = surface->alignedHeight;
322 frame->scanline = surface->scanline;
324 frame->surfaceData = BufferPool_Take(priv->surfacePool, 1ll * frame->scanline * frame->h);
325 if (!frame->surfaceData)
331 VideoFrame_free(priv, frame);
335void VideoClientContextPriv_free(VideoClientContextPriv* priv)
340 EnterCriticalSection(&priv->framesLock);
344 while (Queue_Count(priv->frames))
346 VideoFrame* frame = Queue_Dequeue(priv->frames);
348 VideoFrame_free(priv, frame);
352 Queue_Free(priv->frames);
353 LeaveCriticalSection(&priv->framesLock);
355 DeleteCriticalSection(&priv->framesLock);
357 if (priv->currentPresentation)
358 PresentationContext_unref(&priv->currentPresentation);
360 BufferPool_Free(priv->surfacePool);
365static UINT video_channel_write(VIDEO_PLUGIN* video,
const BYTE* data, UINT32 length)
369 if (!video->control_callback || !video->control_callback->channel_callback)
370 return ERROR_BAD_CONFIGURATION;
371 IWTSVirtualChannel* channel = video->control_callback->channel_callback->channel;
372 if (!channel || !channel->Write)
373 return ERROR_BAD_CONFIGURATION;
374 return channel->Write(channel, length, data,
nullptr);
378static UINT video_control_send_presentation_response(VideoClientContext* context,
381 BYTE buf[12] = WINPR_C_ARRAY_INIT;
383 WINPR_ASSERT(context);
386 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)context->handle;
389 wStream* s = Stream_New(buf, 12);
391 return CHANNEL_RC_NO_MEMORY;
393 Stream_Write_UINT32(s, 12);
394 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_PRESENTATION_RESPONSE);
395 Stream_Write_UINT8(s, resp->PresentationId);
397 Stream_SealLength(s);
398 Stream_Free(s, FALSE);
400 return video_channel_write(video, buf,
sizeof(buf));
404static BOOL video_onMappedGeometryUpdate(MAPPED_GEOMETRY* geometry)
406 WINPR_ASSERT(geometry);
408 PresentationContext* presentation = (PresentationContext*)geometry->custom;
409 WINPR_ASSERT(presentation);
411 RDP_RECT* r = &geometry->geometry.boundingRect;
413 "geometry updated topGeom=(%" PRId32
",%" PRId32
"-%" PRId32
"x%" PRId32
414 ") geom=(%" PRId32
",%" PRId32
"-%" PRId32
"x%" PRId32
") rects=(%" PRId16
",%" PRId16
415 "-%" PRId16
"x%" PRId16
")",
416 geometry->topLevelLeft, geometry->topLevelTop,
417 geometry->topLevelRight - geometry->topLevelLeft,
418 geometry->topLevelBottom - geometry->topLevelTop,
420 geometry->left, geometry->top, geometry->right - geometry->left,
421 geometry->bottom - geometry->top,
423 r->x, r->y, r->width, r->height);
425 WINPR_ASSERT(presentation->surface);
426 presentation->surface->x =
427 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelLeft + geometry->left);
428 presentation->surface->y =
429 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelTop + geometry->top);
435static BOOL video_onMappedGeometryClear(MAPPED_GEOMETRY* geometry)
437 WINPR_ASSERT(geometry);
439 PresentationContext* presentation = (PresentationContext*)geometry->custom;
440 WINPR_ASSERT(presentation);
442 mappedGeometryUnref(presentation->geometry);
443 presentation->geometry =
nullptr;
448static UINT video_PresentationRequest(VideoClientContext* video,
451 UINT ret = CHANNEL_RC_OK;
456 VideoClientContextPriv* priv = video->priv;
459 EnterCriticalSection(&priv->framesLock);
460 if (req->Command == TSMM_START_PRESENTATION)
462 MAPPED_GEOMETRY* geom =
nullptr;
465 if (memcmp(req->VideoSubtypeId, MFVideoFormat_H264, 16) != 0)
467 WLog_ERR(TAG,
"not a H264 video, ignoring request");
471 if (priv->currentPresentation)
473 if (priv->currentPresentation->PresentationId == req->PresentationId)
475 WLog_ERR(TAG,
"ignoring start request for existing presentation %" PRIu8,
476 req->PresentationId);
480 WLog_ERR(TAG,
"releasing current presentation %" PRIu8, req->PresentationId);
481 PresentationContext_unref(&priv->currentPresentation);
486 WLog_ERR(TAG,
"geometry channel not ready, ignoring request");
490 geom = HashTable_GetItemValue(priv->geometry->geometries, &(req->GeometryMappingId));
493 WLog_ERR(TAG,
"geometry mapping 0x%" PRIx64
" not registered", req->GeometryMappingId);
497 WLog_DBG(TAG,
"creating presentation 0x%x", req->PresentationId);
498 priv->currentPresentation = PresentationContext_new(
499 video, req->PresentationId,
500 WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelLeft + geom->left),
501 WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelTop + geom->top), req->SourceWidth,
503 if (!priv->currentPresentation)
505 WLog_ERR(TAG,
"unable to create presentation video");
506 ret = CHANNEL_RC_NO_MEMORY;
510 mappedGeometryRef(geom);
511 priv->currentPresentation->geometry = geom;
513 priv->currentPresentation->video = video;
514 priv->currentPresentation->ScaledWidth = req->ScaledWidth;
515 priv->currentPresentation->ScaledHeight = req->ScaledHeight;
517 geom->custom = priv->currentPresentation;
518 geom->MappedGeometryUpdate = video_onMappedGeometryUpdate;
519 geom->MappedGeometryClear = video_onMappedGeometryClear;
522 resp.PresentationId = req->PresentationId;
523 ret = video_control_send_presentation_response(video, &resp);
525 else if (req->Command == TSMM_STOP_PRESENTATION)
527 WLog_DBG(TAG,
"stopping presentation 0x%x", req->PresentationId);
528 if (!priv->currentPresentation)
530 WLog_ERR(TAG,
"unknown presentation to stop %" PRIu8, req->PresentationId);
534 priv->droppedFrames = 0;
535 priv->publishedFrames = 0;
536 PresentationContext_unref(&priv->currentPresentation);
540 LeaveCriticalSection(&priv->framesLock);
545static UINT video_read_tsmm_presentation_req(VideoClientContext* context,
wStream* s)
549 WINPR_ASSERT(context);
552 if (!Stream_CheckAndLogRequiredLength(TAG, s, 60))
553 return ERROR_INVALID_DATA;
555 Stream_Read_UINT8(s, req.PresentationId);
556 Stream_Read_UINT8(s, req.Version);
557 Stream_Read_UINT8(s, req.Command);
558 Stream_Read_UINT8(s, req.FrameRate);
560 Stream_Seek_UINT16(s);
561 Stream_Seek_UINT16(s);
563 Stream_Read_UINT32(s, req.SourceWidth);
564 Stream_Read_UINT32(s, req.SourceHeight);
565 Stream_Read_UINT32(s, req.ScaledWidth);
566 Stream_Read_UINT32(s, req.ScaledHeight);
567 Stream_Read_UINT64(s, req.hnsTimestampOffset);
568 Stream_Read_UINT64(s, req.GeometryMappingId);
569 Stream_Read(s, req.VideoSubtypeId, 16);
571 Stream_Read_UINT32(s, req.cbExtra);
573 if (!Stream_CheckAndLogRequiredLength(TAG, s, req.cbExtra))
574 return ERROR_INVALID_DATA;
576 req.pExtraData = Stream_Pointer(s);
579 "presentationReq: id:%" PRIu8
" version:%" PRIu8
580 " command:%s srcWidth/srcHeight=%" PRIu32
"x%" PRIu32
" scaled Width/Height=%" PRIu32
581 "x%" PRIu32
" timestamp=%" PRIu64
" mappingId=%" PRIx64
"",
582 req.PresentationId, req.Version, video_command_name(req.Command), req.SourceWidth,
583 req.SourceHeight, req.ScaledWidth, req.ScaledHeight, req.hnsTimestampOffset,
584 req.GeometryMappingId);
586 return video_PresentationRequest(context, &req);
595static UINT video_control_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
598 UINT ret = CHANNEL_RC_OK;
600 UINT32 packetType = 0;
602 WINPR_ASSERT(callback);
605 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin;
608 VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface;
609 WINPR_ASSERT(context);
611 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
612 return ERROR_INVALID_DATA;
614 Stream_Read_UINT32(s, cbSize);
617 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected 8", cbSize);
618 return ERROR_INVALID_DATA;
620 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
621 return ERROR_INVALID_DATA;
623 Stream_Read_UINT32(s, packetType);
626 case TSMM_PACKET_TYPE_PRESENTATION_REQUEST:
627 ret = video_read_tsmm_presentation_req(context, s);
630 WLog_ERR(TAG,
"not expecting packet type %" PRIu32
"", packetType);
631 ret = ERROR_UNSUPPORTED_TYPE;
638static UINT video_control_send_client_notification(VideoClientContext* context,
641 BYTE buf[100] = WINPR_C_ARRAY_INIT;
643 WINPR_ASSERT(context);
646 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)context->handle;
649 wStream* s = Stream_New(buf, 32);
651 return CHANNEL_RC_NO_MEMORY;
654 Stream_Seek_UINT32(s);
655 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_CLIENT_NOTIFICATION);
656 Stream_Write_UINT8(s, notif->PresentationId);
657 Stream_Write_UINT8(s, notif->NotificationType);
659 if (notif->NotificationType == TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE)
661 Stream_Write_UINT32(s, 16);
664 Stream_Write_UINT32(s, notif->FramerateOverride.Flags);
665 Stream_Write_UINT32(s, notif->FramerateOverride.DesiredFrameRate);
666 Stream_Zero(s, 4ULL * 2ULL);
672 Stream_Write_UINT32(s, 0);
675 Stream_SealLength(s);
676 Stream_ResetPosition(s);
677 Stream_Write_UINT32(s, cbSize);
678 Stream_Free(s, FALSE);
680 return video_channel_write(video, buf, cbSize);
683static void video_timer(VideoClientContext* video, UINT64 now)
685 VideoFrame* frame =
nullptr;
689 VideoClientContextPriv* priv = video->priv;
692 EnterCriticalSection(&priv->framesLock);
693 PresentationContext* presentation = video->priv->currentPresentation;
696 const VideoFrame* peekFrame = (VideoFrame*)Queue_Peek(priv->frames);
700 if (peekFrame->publishTime > now)
705 WLog_DBG(TAG,
"dropping frame @%" PRIu64, frame->publishTime);
706 priv->droppedFrames++;
707 VideoFrame_free(priv, frame);
709 frame = Queue_Dequeue(priv->frames);
714 if (presentation && (presentation->PresentationId == frame->PresentationId))
717 const size_t frameSize = 1ull * frame->scanline * frame->h;
718 const size_t surfaceSize = 1ull * surface->scanline * surface->alignedHeight;
723 if (frameSize > surfaceSize)
724 WLog_WARN(TAG,
"dropping stale frame of %" PRIuz
" bytes, surface holds %" PRIuz,
725 frameSize, surfaceSize);
728 priv->publishedFrames++;
729 memcpy(surface->data, frame->surfaceData, frameSize);
731 WINPR_ASSERT(video->showSurface);
732 if (!video->showSurface(video, surface, presentation->ScaledWidth,
733 presentation->ScaledHeight))
734 WLog_WARN(TAG,
"showSurface failed");
737 VideoFrame_free(priv, frame);
740 if (priv->nextFeedbackTime < now)
745 if (priv->publishedFrames && priv->currentPresentation)
747 UINT32 computedRate = 0;
749 if (!PresentationContext_ref(priv->currentPresentation))
750 WLog_WARN(TAG,
"PresentationContext_ref(priv->currentPresentation) failed");
752 if (priv->droppedFrames)
759 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
763 computedRate = priv->lastSentRate - 2;
774 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
775 computedRate = XF_VIDEO_UNLIMITED_RATE;
778 computedRate = priv->lastSentRate + 2;
779 if (computedRate > XF_VIDEO_UNLIMITED_RATE)
780 computedRate = XF_VIDEO_UNLIMITED_RATE;
784 if (computedRate != priv->lastSentRate)
788 WINPR_ASSERT(priv->currentPresentation);
789 notif.PresentationId = priv->currentPresentation->PresentationId;
790 notif.NotificationType = TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE;
791 if (computedRate == XF_VIDEO_UNLIMITED_RATE)
793 notif.FramerateOverride.Flags = 0x01;
794 notif.FramerateOverride.DesiredFrameRate = 0x00;
798 notif.FramerateOverride.Flags = 0x02;
799 notif.FramerateOverride.DesiredFrameRate = computedRate;
802 video_control_send_client_notification(video, ¬if);
803 priv->lastSentRate = computedRate;
806 "server notified with rate %" PRIu32
" published=%" PRIu32
808 priv->lastSentRate, priv->publishedFrames, priv->droppedFrames);
811 PresentationContext_unref(&priv->currentPresentation);
814 priv->droppedFrames = 0;
815 priv->publishedFrames = 0;
816 priv->nextFeedbackTime = now + 1000;
818 LeaveCriticalSection(&priv->framesLock);
822static UINT video_VideoData(VideoClientContext* context,
const TSMM_VIDEO_DATA* data)
825 UINT res = CHANNEL_RC_OK;
827 WINPR_ASSERT(context);
830 VideoClientContextPriv* priv = context->priv;
833 PresentationContext* presentation = priv->currentPresentation;
836 WLog_ERR(TAG,
"no current presentation");
837 return CHANNEL_RC_OK;
840 if (!PresentationContext_ref(presentation))
841 return ERROR_INTERNAL_ERROR;
843 EnterCriticalSection(&priv->framesLock);
844 if (presentation->PresentationId != data->PresentationId)
846 WLog_ERR(TAG,
"current presentation id=%" PRIu8
" doesn't match data id=%" PRIu8,
847 presentation->PresentationId, data->PresentationId);
851 if (!Stream_EnsureRemainingCapacity(presentation->currentSample, data->cbSample))
853 WLog_ERR(TAG,
"unable to expand the current packet");
854 res = CHANNEL_RC_NO_MEMORY;
858 Stream_Write(presentation->currentSample, data->pSample, data->cbSample);
860 if (data->CurrentPacketIndex == data->PacketsInSample)
863 H264_CONTEXT* h264 = presentation->h264;
864 const UINT64 startTime = winpr_GetTickCount64NS();
865 MAPPED_GEOMETRY* geom = presentation->geometry;
867 const RECTANGLE_16 rect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedWidth),
868 WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedHeight) };
869 Stream_SealLength(presentation->currentSample);
870 Stream_ResetPosition(presentation->currentSample);
872 if (data->SampleNumber == 1)
874 presentation->lastPublishTime = startTime;
877 presentation->lastPublishTime += 100ull * data->hnsDuration;
878 const size_t len = Stream_Length(presentation->currentSample);
879 if (len > UINT32_MAX)
882 BOOL enqueueResult = 0;
883 VideoFrame* frame = VideoFrame_new(priv, presentation, geom);
886 WLog_ERR(TAG,
"unable to create frame");
887 res = CHANNEL_RC_NO_MEMORY;
891 status = avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
892 frame->surfaceData, surface->format, surface->scanline,
893 surface->alignedWidth, surface->alignedHeight, &rect, 1);
896 VideoFrame_free(priv, frame);
900 enqueueResult = Queue_Enqueue(priv->frames, frame);
904 WLog_ERR(TAG,
"unable to enqueue frame");
905 VideoFrame_free(priv, frame);
906 res = CHANNEL_RC_NO_MEMORY;
911 WLog_DBG(TAG,
"scheduling frame in %" PRIu64
" ns", (frame->publishTime - startTime));
915 PresentationContext_unref(&priv->currentPresentation);
916 LeaveCriticalSection(&priv->framesLock);
922static UINT video_data_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
926 UINT32 packetType = 0;
929 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin;
932 VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface;
934 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
935 return ERROR_INVALID_DATA;
937 Stream_Read_UINT32(s, cbSize);
940 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected >= 8", cbSize);
941 return ERROR_INVALID_DATA;
944 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
945 return ERROR_INVALID_DATA;
947 Stream_Read_UINT32(s, packetType);
948 if (packetType != TSMM_PACKET_TYPE_VIDEO_DATA)
950 WLog_ERR(TAG,
"only expecting VIDEO_DATA on the data channel");
951 return ERROR_INVALID_DATA;
954 if (!Stream_CheckAndLogRequiredLength(TAG, s, 32))
955 return ERROR_INVALID_DATA;
957 Stream_Read_UINT8(s, data.PresentationId);
958 Stream_Read_UINT8(s, data.Version);
959 Stream_Read_UINT8(s, data.Flags);
960 Stream_Seek_UINT8(s);
961 Stream_Read_UINT64(s, data.hnsTimestamp);
962 Stream_Read_UINT64(s, data.hnsDuration);
963 Stream_Read_UINT16(s, data.CurrentPacketIndex);
964 Stream_Read_UINT16(s, data.PacketsInSample);
965 Stream_Read_UINT32(s, data.SampleNumber);
966 Stream_Read_UINT32(s, data.cbSample);
967 if (!Stream_CheckAndLogRequiredLength(TAG, s, data.cbSample))
968 return ERROR_INVALID_DATA;
969 data.pSample = Stream_Pointer(s);
979 return video_VideoData(context, &data);
988static UINT video_control_on_close(IWTSVirtualChannelCallback* pChannelCallback)
990 if (pChannelCallback)
993 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
994 if (video && video->control_callback)
996 video->control_callback->channel_callback =
nullptr;
999 free(pChannelCallback);
1000 return CHANNEL_RC_OK;
1004static UINT video_data_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1006 if (pChannelCallback)
1009 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1010 if (video && video->data_callback)
1012 video->data_callback->channel_callback =
nullptr;
1015 free(pChannelCallback);
1016 return CHANNEL_RC_OK;
1026static UINT video_control_on_new_channel_connection(IWTSListenerCallback* listenerCallback,
1027 IWTSVirtualChannel* channel,
1028 WINPR_ATTR_UNUSED BYTE* Data,
1029 WINPR_ATTR_UNUSED BOOL* pbAccept,
1030 IWTSVirtualChannelCallback** ppCallback)
1039 WLog_ERR(TAG,
"calloc failed!");
1040 return CHANNEL_RC_NO_MEMORY;
1043 callback->iface.OnDataReceived = video_control_on_data_received;
1044 callback->iface.OnClose = video_control_on_close;
1045 callback->plugin = listener_callback->plugin;
1046 callback->channel_mgr = listener_callback->channel_mgr;
1047 callback->channel = channel;
1048 listener_callback->channel_callback = callback;
1050 *ppCallback = &callback->iface;
1052 return CHANNEL_RC_OK;
1057static UINT video_data_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1058 IWTSVirtualChannel* pChannel,
1059 WINPR_ATTR_UNUSED BYTE* Data,
1060 WINPR_ATTR_UNUSED BOOL* pbAccept,
1061 IWTSVirtualChannelCallback** ppCallback)
1070 WLog_ERR(TAG,
"calloc failed!");
1071 return CHANNEL_RC_NO_MEMORY;
1074 callback->iface.OnDataReceived = video_data_on_data_received;
1075 callback->iface.OnClose = video_data_on_close;
1076 callback->plugin = listener_callback->plugin;
1077 callback->channel_mgr = listener_callback->channel_mgr;
1078 callback->channel = pChannel;
1079 listener_callback->channel_callback = callback;
1081 *ppCallback = &callback->iface;
1083 return CHANNEL_RC_OK;
1087static uint64_t timer_cb(WINPR_ATTR_UNUSED rdpContext* context,
void* userdata,
1088 WINPR_ATTR_UNUSED FreeRDP_TimerID timerID, uint64_t timestamp,
1091 VideoClientContext* video = userdata;
1097 video->timer(video, timestamp);
1108static UINT video_plugin_initialize(IWTSPlugin* plugin, IWTSVirtualChannelManager* channelMgr)
1111 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)plugin;
1113 if (video->initialized)
1115 WLog_ERR(TAG,
"[%s] channel initialized twice, aborting", VIDEO_CONTROL_DVC_CHANNEL_NAME);
1116 return ERROR_INVALID_DATA;
1124 WLog_ERR(TAG,
"calloc for control callback failed!");
1125 return CHANNEL_RC_NO_MEMORY;
1128 callback->iface.OnNewChannelConnection = video_control_on_new_channel_connection;
1129 callback->plugin = plugin;
1130 callback->channel_mgr = channelMgr;
1132 status = channelMgr->CreateListener(channelMgr, VIDEO_CONTROL_DVC_CHANNEL_NAME, 0,
1133 &callback->iface, &(video->controlListener));
1134 video->control_callback = callback;
1135 if (status != CHANNEL_RC_OK)
1138 video->controlListener->pInterface = video->wtsPlugin.pInterface;
1145 WLog_ERR(TAG,
"calloc for data callback failed!");
1146 return CHANNEL_RC_NO_MEMORY;
1149 callback->iface.OnNewChannelConnection = video_data_on_new_channel_connection;
1150 callback->plugin = plugin;
1151 callback->channel_mgr = channelMgr;
1153 status = channelMgr->CreateListener(channelMgr, VIDEO_DATA_DVC_CHANNEL_NAME, 0,
1154 &callback->iface, &(video->dataListener));
1155 video->data_callback = callback;
1156 if (status == CHANNEL_RC_OK)
1157 video->dataListener->pInterface = video->wtsPlugin.pInterface;
1160 if (status == CHANNEL_RC_OK)
1161 video->context->priv->timerID =
1162 freerdp_timer_add(video->rdpcontext, 20000000, timer_cb, video->context,
true);
1163 video->initialized = video->context->priv->timerID != 0;
1164 if (!video->initialized)
1165 status = ERROR_INTERNAL_ERROR;
1175static UINT video_plugin_terminated(IWTSPlugin* pPlugin)
1177 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)pPlugin;
1179 return CHANNEL_RC_INVALID_INSTANCE;
1181 if (video->context && video->context->priv)
1182 freerdp_timer_remove(video->rdpcontext, video->context->priv->timerID);
1184 if (video->control_callback)
1186 IWTSVirtualChannelManager* mgr = video->control_callback->channel_mgr;
1188 IFCALL(mgr->DestroyListener, mgr, video->controlListener);
1190 if (video->data_callback)
1192 IWTSVirtualChannelManager* mgr = video->data_callback->channel_mgr;
1194 IFCALL(mgr->DestroyListener, mgr, video->dataListener);
1198 VideoClientContextPriv_free(video->context->priv);
1200 free(video->control_callback);
1201 free(video->data_callback);
1202 free(video->wtsPlugin.pInterface);
1204 return CHANNEL_RC_OK;
1216FREERDP_ENTRY_POINT(UINT VCAPITYPE video_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1218 UINT error = ERROR_INTERNAL_ERROR;
1220 VIDEO_PLUGIN* videoPlugin = (VIDEO_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints,
"video");
1223 videoPlugin = (VIDEO_PLUGIN*)calloc(1,
sizeof(VIDEO_PLUGIN));
1226 WLog_ERR(TAG,
"calloc failed!");
1227 return CHANNEL_RC_NO_MEMORY;
1230 videoPlugin->wtsPlugin.Initialize = video_plugin_initialize;
1231 videoPlugin->wtsPlugin.Connected =
nullptr;
1232 videoPlugin->wtsPlugin.Disconnected =
nullptr;
1233 videoPlugin->wtsPlugin.Terminated = video_plugin_terminated;
1235 VideoClientContext* videoContext =
1236 (VideoClientContext*)calloc(1,
sizeof(VideoClientContext));
1239 WLog_ERR(TAG,
"calloc failed!");
1241 return CHANNEL_RC_NO_MEMORY;
1244 VideoClientContextPriv* priv = VideoClientContextPriv_new(videoContext);
1247 WLog_ERR(TAG,
"VideoClientContextPriv_new failed!");
1250 return CHANNEL_RC_NO_MEMORY;
1253 videoContext->handle = (
void*)videoPlugin;
1254 videoContext->priv = priv;
1255 videoContext->timer = video_timer;
1256 videoContext->setGeometry = video_client_context_set_geometry;
1258 videoPlugin->wtsPlugin.pInterface = (
void*)videoContext;
1259 videoPlugin->context = videoContext;
1260 videoPlugin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1261 if (videoPlugin->rdpcontext)
1262 error = pEntryPoints->RegisterPlugin(pEntryPoints,
"video", &videoPlugin->wtsPlugin);
1266 WLog_ERR(TAG,
"could not get video Plugin.");
1267 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