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")
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 };
72 VideoClientContext* video;
74 UINT32 ScaledWidth, ScaledHeight;
75 MAPPED_GEOMETRY* geometry;
77 UINT64 startTimeStamp;
81 UINT64 lastPublishTime, nextPublishTime;
82 volatile LONG refCounter;
90 MAPPED_GEOMETRY* geometry;
94 PresentationContext* presentation;
98struct s_VideoClientContextPriv
100 VideoClientContext* video;
101 GeometryClientContext* geometry;
104 wBufferPool* surfacePool;
105 UINT32 publishedFrames;
106 UINT32 droppedFrames;
108 UINT64 nextFeedbackTime;
109 PresentationContext* currentPresentation;
110 FreeRDP_TimerID timerID;
113static void PresentationContext_unref(PresentationContext** presentation);
114static void VideoClientContextPriv_free(VideoClientContextPriv* priv);
117static const char* video_command_name(BYTE cmd)
121 case TSMM_START_PRESENTATION:
123 case TSMM_STOP_PRESENTATION:
130static void video_client_context_set_geometry(VideoClientContext* video,
131 GeometryClientContext* geometry)
134 WINPR_ASSERT(video->priv);
135 video->priv->geometry = geometry;
138WINPR_ATTR_MALLOC(VideoClientContextPriv_free, 1)
139static VideoClientContextPriv* VideoClientContextPriv_new(VideoClientContext* video)
141 VideoClientContextPriv* ret =
nullptr;
144 ret = calloc(1,
sizeof(*ret));
148 ret->frames = Queue_New(TRUE, 10, 2);
151 WLog_ERR(TAG,
"unable to allocate frames queue");
155 ret->surfacePool = BufferPool_New(FALSE, 0, 16);
156 if (!ret->surfacePool)
158 WLog_ERR(TAG,
"unable to create surface pool");
162 if (!InitializeCriticalSectionAndSpinCount(&ret->framesLock, 4 * 1000))
164 WLog_ERR(TAG,
"unable to initialize frames lock");
173 ret->lastSentRate = 30;
177 VideoClientContextPriv_free(ret);
182static BOOL PresentationContext_ref(PresentationContext* presentation)
184 WINPR_ASSERT(presentation);
186 InterlockedIncrement(&presentation->refCounter);
191static PresentationContext* PresentationContext_new(VideoClientContext* video, BYTE PresentationId,
192 UINT32 x, UINT32 y, UINT32 width, UINT32 height)
194 size_t s = 4ULL * width * height;
195 PresentationContext* ret =
nullptr;
202 ret = calloc(1,
sizeof(*ret));
207 ret->PresentationId = PresentationId;
209 ret->h264 = h264_context_new(FALSE);
212 WLog_ERR(TAG,
"unable to create a h264 context");
215 if (!h264_context_reset(ret->h264, width, height))
218 ret->currentSample = Stream_New(
nullptr, 4096);
219 if (!ret->currentSample)
221 WLog_ERR(TAG,
"unable to create current packet stream");
225 ret->surface = video->createSurface(video, x, y, width, height);
228 WLog_ERR(TAG,
"unable to create surface");
232 if (!PresentationContext_ref(ret))
238 PresentationContext_unref(&ret);
242static void PresentationContext_unref(PresentationContext** ppresentation)
244 WINPR_ASSERT(ppresentation);
246 PresentationContext* presentation = *ppresentation;
250 if (InterlockedDecrement(&presentation->refCounter) > 0)
253 MAPPED_GEOMETRY* geometry = presentation->geometry;
256 geometry->MappedGeometryUpdate =
nullptr;
257 geometry->MappedGeometryClear =
nullptr;
258 geometry->custom =
nullptr;
259 mappedGeometryUnref(geometry);
262 h264_context_free(presentation->h264);
263 Stream_Free(presentation->currentSample, TRUE);
264 presentation->video->deleteSurface(presentation->video, presentation->surface);
266 *ppresentation =
nullptr;
269static void VideoFrame_free(VideoFrame* frame)
274 mappedGeometryUnref(frame->geometry);
276 WINPR_ASSERT(frame->presentation);
277 WINPR_ASSERT(frame->presentation->video);
278 WINPR_ASSERT(frame->presentation->video->priv);
279 BufferPool_Return(frame->presentation->video->priv->surfacePool, frame->surfaceData);
280 PresentationContext_unref(&frame->presentation);
284WINPR_ATTR_MALLOC(VideoFrame_free, 1)
285static VideoFrame* VideoFrame_new(VideoClientContextPriv* priv, PresentationContext* presentation,
286 MAPPED_GEOMETRY* geom)
289 WINPR_ASSERT(presentation);
293 WINPR_ASSERT(surface);
295 VideoFrame* frame = calloc(1,
sizeof(VideoFrame));
299 mappedGeometryRef(geom);
301 frame->publishTime = presentation->lastPublishTime;
302 frame->geometry = geom;
303 frame->w = surface->alignedWidth;
304 frame->h = surface->alignedHeight;
305 frame->scanline = surface->scanline;
307 frame->surfaceData = BufferPool_Take(priv->surfacePool, 1ll * frame->scanline * frame->h);
308 if (!frame->surfaceData)
311 frame->presentation = presentation;
312 if (!PresentationContext_ref(frame->presentation))
318 VideoFrame_free(frame);
322void VideoClientContextPriv_free(VideoClientContextPriv* priv)
327 EnterCriticalSection(&priv->framesLock);
331 while (Queue_Count(priv->frames))
333 VideoFrame* frame = Queue_Dequeue(priv->frames);
335 VideoFrame_free(frame);
339 Queue_Free(priv->frames);
340 LeaveCriticalSection(&priv->framesLock);
342 DeleteCriticalSection(&priv->framesLock);
344 if (priv->currentPresentation)
345 PresentationContext_unref(&priv->currentPresentation);
347 BufferPool_Free(priv->surfacePool);
352static UINT video_channel_write(VIDEO_PLUGIN* video,
const BYTE* data, UINT32 length)
356 if (!video->control_callback || !video->control_callback->channel_callback)
357 return ERROR_BAD_CONFIGURATION;
358 IWTSVirtualChannel* channel = video->control_callback->channel_callback->channel;
359 if (!channel || !channel->Write)
360 return ERROR_BAD_CONFIGURATION;
361 return channel->Write(channel, length, data,
nullptr);
365static UINT video_control_send_presentation_response(VideoClientContext* context,
368 BYTE buf[12] = WINPR_C_ARRAY_INIT;
370 WINPR_ASSERT(context);
373 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)context->handle;
376 wStream* s = Stream_New(buf, 12);
378 return CHANNEL_RC_NO_MEMORY;
380 Stream_Write_UINT32(s, 12);
381 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_PRESENTATION_RESPONSE);
382 Stream_Write_UINT8(s, resp->PresentationId);
384 Stream_SealLength(s);
385 Stream_Free(s, FALSE);
387 return video_channel_write(video, buf,
sizeof(buf));
391static BOOL video_onMappedGeometryUpdate(MAPPED_GEOMETRY* geometry)
393 WINPR_ASSERT(geometry);
395 PresentationContext* presentation = (PresentationContext*)geometry->custom;
396 WINPR_ASSERT(presentation);
398 RDP_RECT* r = &geometry->geometry.boundingRect;
400 "geometry updated topGeom=(%" PRId32
",%" PRId32
"-%" PRId32
"x%" PRId32
401 ") geom=(%" PRId32
",%" PRId32
"-%" PRId32
"x%" PRId32
") rects=(%" PRId16
",%" PRId16
402 "-%" PRId16
"x%" PRId16
")",
403 geometry->topLevelLeft, geometry->topLevelTop,
404 geometry->topLevelRight - geometry->topLevelLeft,
405 geometry->topLevelBottom - geometry->topLevelTop,
407 geometry->left, geometry->top, geometry->right - geometry->left,
408 geometry->bottom - geometry->top,
410 r->x, r->y, r->width, r->height);
412 WINPR_ASSERT(presentation->surface);
413 presentation->surface->x =
414 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelLeft + geometry->left);
415 presentation->surface->y =
416 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelTop + geometry->top);
422static BOOL video_onMappedGeometryClear(MAPPED_GEOMETRY* geometry)
424 WINPR_ASSERT(geometry);
426 PresentationContext* presentation = (PresentationContext*)geometry->custom;
427 WINPR_ASSERT(presentation);
429 mappedGeometryUnref(presentation->geometry);
430 presentation->geometry =
nullptr;
435static UINT video_PresentationRequest(VideoClientContext* video,
438 UINT ret = CHANNEL_RC_OK;
443 VideoClientContextPriv* priv = video->priv;
446 if (req->Command == TSMM_START_PRESENTATION)
448 MAPPED_GEOMETRY* geom =
nullptr;
451 if (memcmp(req->VideoSubtypeId, MFVideoFormat_H264, 16) != 0)
453 WLog_ERR(TAG,
"not a H264 video, ignoring request");
454 return CHANNEL_RC_OK;
457 if (priv->currentPresentation)
459 if (priv->currentPresentation->PresentationId == req->PresentationId)
461 WLog_ERR(TAG,
"ignoring start request for existing presentation %" PRIu8,
462 req->PresentationId);
463 return CHANNEL_RC_OK;
466 WLog_ERR(TAG,
"releasing current presentation %" PRIu8, req->PresentationId);
467 PresentationContext_unref(&priv->currentPresentation);
472 WLog_ERR(TAG,
"geometry channel not ready, ignoring request");
473 return CHANNEL_RC_OK;
476 geom = HashTable_GetItemValue(priv->geometry->geometries, &(req->GeometryMappingId));
479 WLog_ERR(TAG,
"geometry mapping 0x%" PRIx64
" not registered", req->GeometryMappingId);
480 return CHANNEL_RC_OK;
483 WLog_DBG(TAG,
"creating presentation 0x%x", req->PresentationId);
484 priv->currentPresentation = PresentationContext_new(
485 video, req->PresentationId,
486 WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelLeft + geom->left),
487 WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelTop + geom->top), req->SourceWidth,
489 if (!priv->currentPresentation)
491 WLog_ERR(TAG,
"unable to create presentation video");
492 return CHANNEL_RC_NO_MEMORY;
495 mappedGeometryRef(geom);
496 priv->currentPresentation->geometry = geom;
498 priv->currentPresentation->video = video;
499 priv->currentPresentation->ScaledWidth = req->ScaledWidth;
500 priv->currentPresentation->ScaledHeight = req->ScaledHeight;
502 geom->custom = priv->currentPresentation;
503 geom->MappedGeometryUpdate = video_onMappedGeometryUpdate;
504 geom->MappedGeometryClear = video_onMappedGeometryClear;
507 resp.PresentationId = req->PresentationId;
508 ret = video_control_send_presentation_response(video, &resp);
510 else if (req->Command == TSMM_STOP_PRESENTATION)
512 WLog_DBG(TAG,
"stopping presentation 0x%x", req->PresentationId);
513 if (!priv->currentPresentation)
515 WLog_ERR(TAG,
"unknown presentation to stop %" PRIu8, req->PresentationId);
516 return CHANNEL_RC_OK;
519 priv->droppedFrames = 0;
520 priv->publishedFrames = 0;
521 PresentationContext_unref(&priv->currentPresentation);
528static UINT video_read_tsmm_presentation_req(VideoClientContext* context,
wStream* s)
532 WINPR_ASSERT(context);
535 if (!Stream_CheckAndLogRequiredLength(TAG, s, 60))
536 return ERROR_INVALID_DATA;
538 Stream_Read_UINT8(s, req.PresentationId);
539 Stream_Read_UINT8(s, req.Version);
540 Stream_Read_UINT8(s, req.Command);
541 Stream_Read_UINT8(s, req.FrameRate);
543 Stream_Seek_UINT16(s);
544 Stream_Seek_UINT16(s);
546 Stream_Read_UINT32(s, req.SourceWidth);
547 Stream_Read_UINT32(s, req.SourceHeight);
548 Stream_Read_UINT32(s, req.ScaledWidth);
549 Stream_Read_UINT32(s, req.ScaledHeight);
550 Stream_Read_UINT64(s, req.hnsTimestampOffset);
551 Stream_Read_UINT64(s, req.GeometryMappingId);
552 Stream_Read(s, req.VideoSubtypeId, 16);
554 Stream_Read_UINT32(s, req.cbExtra);
556 if (!Stream_CheckAndLogRequiredLength(TAG, s, req.cbExtra))
557 return ERROR_INVALID_DATA;
559 req.pExtraData = Stream_Pointer(s);
562 "presentationReq: id:%" PRIu8
" version:%" PRIu8
563 " command:%s srcWidth/srcHeight=%" PRIu32
"x%" PRIu32
" scaled Width/Height=%" PRIu32
564 "x%" PRIu32
" timestamp=%" PRIu64
" mappingId=%" PRIx64
"",
565 req.PresentationId, req.Version, video_command_name(req.Command), req.SourceWidth,
566 req.SourceHeight, req.ScaledWidth, req.ScaledHeight, req.hnsTimestampOffset,
567 req.GeometryMappingId);
569 return video_PresentationRequest(context, &req);
578static UINT video_control_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
581 UINT ret = CHANNEL_RC_OK;
583 UINT32 packetType = 0;
585 WINPR_ASSERT(callback);
588 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin;
591 VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface;
592 WINPR_ASSERT(context);
594 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
595 return ERROR_INVALID_DATA;
597 Stream_Read_UINT32(s, cbSize);
600 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected 8", cbSize);
601 return ERROR_INVALID_DATA;
603 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
604 return ERROR_INVALID_DATA;
606 Stream_Read_UINT32(s, packetType);
609 case TSMM_PACKET_TYPE_PRESENTATION_REQUEST:
610 ret = video_read_tsmm_presentation_req(context, s);
613 WLog_ERR(TAG,
"not expecting packet type %" PRIu32
"", packetType);
614 ret = ERROR_UNSUPPORTED_TYPE;
621static UINT video_control_send_client_notification(VideoClientContext* context,
624 BYTE buf[100] = WINPR_C_ARRAY_INIT;
626 WINPR_ASSERT(context);
629 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)context->handle;
632 wStream* s = Stream_New(buf, 32);
634 return CHANNEL_RC_NO_MEMORY;
637 Stream_Seek_UINT32(s);
638 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_CLIENT_NOTIFICATION);
639 Stream_Write_UINT8(s, notif->PresentationId);
640 Stream_Write_UINT8(s, notif->NotificationType);
642 if (notif->NotificationType == TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE)
644 Stream_Write_UINT32(s, 16);
647 Stream_Write_UINT32(s, notif->FramerateOverride.Flags);
648 Stream_Write_UINT32(s, notif->FramerateOverride.DesiredFrameRate);
649 Stream_Zero(s, 4ULL * 2ULL);
655 Stream_Write_UINT32(s, 0);
658 Stream_SealLength(s);
659 Stream_ResetPosition(s);
660 Stream_Write_UINT32(s, cbSize);
661 Stream_Free(s, FALSE);
663 return video_channel_write(video, buf, cbSize);
666static void video_timer(VideoClientContext* video, UINT64 now)
668 VideoFrame* frame =
nullptr;
672 VideoClientContextPriv* priv = video->priv;
675 EnterCriticalSection(&priv->framesLock);
678 const VideoFrame* peekFrame = (VideoFrame*)Queue_Peek(priv->frames);
682 if (peekFrame->publishTime > now)
687 WLog_DBG(TAG,
"dropping frame @%" PRIu64, frame->publishTime);
688 priv->droppedFrames++;
689 VideoFrame_free(frame);
691 frame = Queue_Dequeue(priv->frames);
693 LeaveCriticalSection(&priv->framesLock);
697 PresentationContext* presentation = frame->presentation;
699 priv->publishedFrames++;
700 memcpy(presentation->surface->data, frame->surfaceData, 1ull * frame->scanline * frame->h);
702 WINPR_ASSERT(video->showSurface);
703 if (!video->showSurface(video, presentation->surface, presentation->ScaledWidth,
704 presentation->ScaledHeight))
705 WLog_WARN(TAG,
"showSurface failed");
707 VideoFrame_free(frame);
710 if (priv->nextFeedbackTime < now)
715 if (priv->publishedFrames && priv->currentPresentation)
717 UINT32 computedRate = 0;
719 if (!PresentationContext_ref(priv->currentPresentation))
720 WLog_WARN(TAG,
"PresentationContext_ref(priv->currentPresentation) failed");
722 if (priv->droppedFrames)
729 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
733 computedRate = priv->lastSentRate - 2;
744 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
745 computedRate = XF_VIDEO_UNLIMITED_RATE;
748 computedRate = priv->lastSentRate + 2;
749 if (computedRate > XF_VIDEO_UNLIMITED_RATE)
750 computedRate = XF_VIDEO_UNLIMITED_RATE;
754 if (computedRate != priv->lastSentRate)
758 WINPR_ASSERT(priv->currentPresentation);
759 notif.PresentationId = priv->currentPresentation->PresentationId;
760 notif.NotificationType = TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE;
761 if (computedRate == XF_VIDEO_UNLIMITED_RATE)
763 notif.FramerateOverride.Flags = 0x01;
764 notif.FramerateOverride.DesiredFrameRate = 0x00;
768 notif.FramerateOverride.Flags = 0x02;
769 notif.FramerateOverride.DesiredFrameRate = computedRate;
772 video_control_send_client_notification(video, ¬if);
773 priv->lastSentRate = computedRate;
776 "server notified with rate %" PRIu32
" published=%" PRIu32
778 priv->lastSentRate, priv->publishedFrames, priv->droppedFrames);
781 PresentationContext_unref(&priv->currentPresentation);
784 priv->droppedFrames = 0;
785 priv->publishedFrames = 0;
786 priv->nextFeedbackTime = now + 1000;
791static UINT video_VideoData(VideoClientContext* context,
const TSMM_VIDEO_DATA* data)
795 WINPR_ASSERT(context);
798 VideoClientContextPriv* priv = context->priv;
801 PresentationContext* presentation = priv->currentPresentation;
804 WLog_ERR(TAG,
"no current presentation");
805 return CHANNEL_RC_OK;
808 if (presentation->PresentationId != data->PresentationId)
810 WLog_ERR(TAG,
"current presentation id=%" PRIu8
" doesn't match data id=%" PRIu8,
811 presentation->PresentationId, data->PresentationId);
812 return CHANNEL_RC_OK;
815 if (!Stream_EnsureRemainingCapacity(presentation->currentSample, data->cbSample))
817 WLog_ERR(TAG,
"unable to expand the current packet");
818 return CHANNEL_RC_NO_MEMORY;
821 Stream_Write(presentation->currentSample, data->pSample, data->cbSample);
823 if (data->CurrentPacketIndex == data->PacketsInSample)
826 H264_CONTEXT* h264 = presentation->h264;
827 const UINT64 startTime = winpr_GetTickCount64NS();
828 MAPPED_GEOMETRY* geom = presentation->geometry;
830 const RECTANGLE_16 rect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedWidth),
831 WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedHeight) };
832 Stream_SealLength(presentation->currentSample);
833 Stream_ResetPosition(presentation->currentSample);
835 const UINT64 timeAfterH264 = winpr_GetTickCount64NS();
836 if (data->SampleNumber == 1)
838 presentation->lastPublishTime = startTime;
841 presentation->lastPublishTime += 100ull * data->hnsDuration;
842 if (presentation->lastPublishTime <= (10000000ull + timeAfterH264))
846 const size_t len = Stream_Length(presentation->currentSample);
847 if (len > UINT32_MAX)
848 return CHANNEL_RC_OK;
852 avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
853 surface->data, surface->format, surface->scanline,
854 surface->alignedWidth, surface->alignedHeight, &rect, 1);
857 return CHANNEL_RC_OK;
859 WINPR_ASSERT(context->showSurface);
860 if (!context->showSurface(context, presentation->surface, presentation->ScaledWidth,
861 presentation->ScaledHeight))
862 return CHANNEL_RC_NOT_INITIALIZED;
864 priv->publishedFrames++;
867 EnterCriticalSection(&priv->framesLock);
868 while (Queue_Count(priv->frames) > 0)
870 VideoFrame* frame = Queue_Dequeue(priv->frames);
873 priv->droppedFrames++;
874 VideoFrame_free(frame);
878 LeaveCriticalSection(&priv->framesLock);
881 WLog_DBG(TAG,
"showing frame (%d dropped)", dropped);
885 const size_t len = Stream_Length(presentation->currentSample);
886 if (len > UINT32_MAX)
887 return CHANNEL_RC_OK;
889 BOOL enqueueResult = 0;
890 VideoFrame* frame = VideoFrame_new(priv, presentation, geom);
893 WLog_ERR(TAG,
"unable to create frame");
894 return CHANNEL_RC_NO_MEMORY;
898 avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
899 frame->surfaceData, surface->format, surface->scanline,
900 surface->alignedWidth, surface->alignedHeight, &rect, 1);
903 VideoFrame_free(frame);
904 return CHANNEL_RC_OK;
907 EnterCriticalSection(&priv->framesLock);
908 enqueueResult = Queue_Enqueue(priv->frames, frame);
909 LeaveCriticalSection(&priv->framesLock);
913 WLog_ERR(TAG,
"unable to enqueue frame");
914 VideoFrame_free(frame);
915 return CHANNEL_RC_NO_MEMORY;
919 WLog_DBG(TAG,
"scheduling frame in %" PRIu64
" ms", (frame->publishTime - startTime));
923 return CHANNEL_RC_OK;
927static UINT video_data_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
931 UINT32 packetType = 0;
934 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)callback->plugin;
937 VideoClientContext* context = (VideoClientContext*)video->wtsPlugin.pInterface;
939 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
940 return ERROR_INVALID_DATA;
942 Stream_Read_UINT32(s, cbSize);
945 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected >= 8", cbSize);
946 return ERROR_INVALID_DATA;
949 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
950 return ERROR_INVALID_DATA;
952 Stream_Read_UINT32(s, packetType);
953 if (packetType != TSMM_PACKET_TYPE_VIDEO_DATA)
955 WLog_ERR(TAG,
"only expecting VIDEO_DATA on the data channel");
956 return ERROR_INVALID_DATA;
959 if (!Stream_CheckAndLogRequiredLength(TAG, s, 32))
960 return ERROR_INVALID_DATA;
962 Stream_Read_UINT8(s, data.PresentationId);
963 Stream_Read_UINT8(s, data.Version);
964 Stream_Read_UINT8(s, data.Flags);
965 Stream_Seek_UINT8(s);
966 Stream_Read_UINT64(s, data.hnsTimestamp);
967 Stream_Read_UINT64(s, data.hnsDuration);
968 Stream_Read_UINT16(s, data.CurrentPacketIndex);
969 Stream_Read_UINT16(s, data.PacketsInSample);
970 Stream_Read_UINT32(s, data.SampleNumber);
971 Stream_Read_UINT32(s, data.cbSample);
972 if (!Stream_CheckAndLogRequiredLength(TAG, s, data.cbSample))
973 return ERROR_INVALID_DATA;
974 data.pSample = Stream_Pointer(s);
984 return video_VideoData(context, &data);
993static UINT video_control_on_close(IWTSVirtualChannelCallback* pChannelCallback)
995 if (pChannelCallback)
998 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
999 if (video && video->control_callback)
1001 video->control_callback->channel_callback =
nullptr;
1004 free(pChannelCallback);
1005 return CHANNEL_RC_OK;
1009static UINT video_data_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1011 if (pChannelCallback)
1014 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)listener_callback->plugin;
1015 if (video && video->data_callback)
1017 video->data_callback->channel_callback =
nullptr;
1020 free(pChannelCallback);
1021 return CHANNEL_RC_OK;
1031static UINT video_control_on_new_channel_connection(IWTSListenerCallback* listenerCallback,
1032 IWTSVirtualChannel* channel,
1033 WINPR_ATTR_UNUSED BYTE* Data,
1034 WINPR_ATTR_UNUSED BOOL* pbAccept,
1035 IWTSVirtualChannelCallback** ppCallback)
1044 WLog_ERR(TAG,
"calloc failed!");
1045 return CHANNEL_RC_NO_MEMORY;
1048 callback->iface.OnDataReceived = video_control_on_data_received;
1049 callback->iface.OnClose = video_control_on_close;
1050 callback->plugin = listener_callback->plugin;
1051 callback->channel_mgr = listener_callback->channel_mgr;
1052 callback->channel = channel;
1053 listener_callback->channel_callback = callback;
1055 *ppCallback = &callback->iface;
1057 return CHANNEL_RC_OK;
1062static UINT video_data_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1063 IWTSVirtualChannel* pChannel,
1064 WINPR_ATTR_UNUSED BYTE* Data,
1065 WINPR_ATTR_UNUSED BOOL* pbAccept,
1066 IWTSVirtualChannelCallback** ppCallback)
1075 WLog_ERR(TAG,
"calloc failed!");
1076 return CHANNEL_RC_NO_MEMORY;
1079 callback->iface.OnDataReceived = video_data_on_data_received;
1080 callback->iface.OnClose = video_data_on_close;
1081 callback->plugin = listener_callback->plugin;
1082 callback->channel_mgr = listener_callback->channel_mgr;
1083 callback->channel = pChannel;
1084 listener_callback->channel_callback = callback;
1086 *ppCallback = &callback->iface;
1088 return CHANNEL_RC_OK;
1092static uint64_t timer_cb(WINPR_ATTR_UNUSED rdpContext* context,
void* userdata,
1093 WINPR_ATTR_UNUSED FreeRDP_TimerID timerID, uint64_t timestamp,
1096 VideoClientContext* video = userdata;
1102 video->timer(video, timestamp);
1113static UINT video_plugin_initialize(IWTSPlugin* plugin, IWTSVirtualChannelManager* channelMgr)
1116 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)plugin;
1118 if (video->initialized)
1120 WLog_ERR(TAG,
"[%s] channel initialized twice, aborting", VIDEO_CONTROL_DVC_CHANNEL_NAME);
1121 return ERROR_INVALID_DATA;
1129 WLog_ERR(TAG,
"calloc for control callback failed!");
1130 return CHANNEL_RC_NO_MEMORY;
1133 callback->iface.OnNewChannelConnection = video_control_on_new_channel_connection;
1134 callback->plugin = plugin;
1135 callback->channel_mgr = channelMgr;
1137 status = channelMgr->CreateListener(channelMgr, VIDEO_CONTROL_DVC_CHANNEL_NAME, 0,
1138 &callback->iface, &(video->controlListener));
1139 video->control_callback = callback;
1140 if (status != CHANNEL_RC_OK)
1143 video->controlListener->pInterface = video->wtsPlugin.pInterface;
1150 WLog_ERR(TAG,
"calloc for data callback failed!");
1151 return CHANNEL_RC_NO_MEMORY;
1154 callback->iface.OnNewChannelConnection = video_data_on_new_channel_connection;
1155 callback->plugin = plugin;
1156 callback->channel_mgr = channelMgr;
1158 status = channelMgr->CreateListener(channelMgr, VIDEO_DATA_DVC_CHANNEL_NAME, 0,
1159 &callback->iface, &(video->dataListener));
1160 video->data_callback = callback;
1161 if (status == CHANNEL_RC_OK)
1162 video->dataListener->pInterface = video->wtsPlugin.pInterface;
1165 if (status == CHANNEL_RC_OK)
1166 video->context->priv->timerID =
1167 freerdp_timer_add(video->rdpcontext, 20000000, timer_cb, video->context,
true);
1168 video->initialized = video->context->priv->timerID != 0;
1169 if (!video->initialized)
1170 status = ERROR_INTERNAL_ERROR;
1180static UINT video_plugin_terminated(IWTSPlugin* pPlugin)
1182 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)pPlugin;
1184 return CHANNEL_RC_INVALID_INSTANCE;
1186 if (video->context && video->context->priv)
1187 freerdp_timer_remove(video->rdpcontext, video->context->priv->timerID);
1189 if (video->control_callback)
1191 IWTSVirtualChannelManager* mgr = video->control_callback->channel_mgr;
1193 IFCALL(mgr->DestroyListener, mgr, video->controlListener);
1195 if (video->data_callback)
1197 IWTSVirtualChannelManager* mgr = video->data_callback->channel_mgr;
1199 IFCALL(mgr->DestroyListener, mgr, video->dataListener);
1203 VideoClientContextPriv_free(video->context->priv);
1205 free(video->control_callback);
1206 free(video->data_callback);
1207 free(video->wtsPlugin.pInterface);
1209 return CHANNEL_RC_OK;
1221FREERDP_ENTRY_POINT(UINT VCAPITYPE video_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1223 UINT error = ERROR_INTERNAL_ERROR;
1225 VIDEO_PLUGIN* videoPlugin = (VIDEO_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints,
"video");
1228 videoPlugin = (VIDEO_PLUGIN*)calloc(1,
sizeof(VIDEO_PLUGIN));
1231 WLog_ERR(TAG,
"calloc failed!");
1232 return CHANNEL_RC_NO_MEMORY;
1235 videoPlugin->wtsPlugin.Initialize = video_plugin_initialize;
1236 videoPlugin->wtsPlugin.Connected =
nullptr;
1237 videoPlugin->wtsPlugin.Disconnected =
nullptr;
1238 videoPlugin->wtsPlugin.Terminated = video_plugin_terminated;
1240 VideoClientContext* videoContext =
1241 (VideoClientContext*)calloc(1,
sizeof(VideoClientContext));
1244 WLog_ERR(TAG,
"calloc failed!");
1246 return CHANNEL_RC_NO_MEMORY;
1249 VideoClientContextPriv* priv = VideoClientContextPriv_new(videoContext);
1252 WLog_ERR(TAG,
"VideoClientContextPriv_new failed!");
1255 return CHANNEL_RC_NO_MEMORY;
1258 videoContext->handle = (
void*)videoPlugin;
1259 videoContext->priv = priv;
1260 videoContext->timer = video_timer;
1261 videoContext->setGeometry = video_client_context_set_geometry;
1263 videoPlugin->wtsPlugin.pInterface = (
void*)videoContext;
1264 videoPlugin->context = videoContext;
1265 videoPlugin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1266 if (videoPlugin->rdpcontext)
1267 error = pEntryPoints->RegisterPlugin(pEntryPoints,
"video", &videoPlugin->wtsPlugin);
1271 WLog_ERR(TAG,
"could not get video Plugin.");
1272 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