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>
46#define TAG CHANNELS_TAG("video")
48#include "video_main.h"
54 IWTSListener* controlListener;
55 IWTSListener* dataListener;
59 VideoClientContext* context;
63#define XF_VIDEO_UNLIMITED_RATE 31
65static const BYTE MFVideoFormat_H264[] = {
'H',
'2',
'6',
'4', 0x00, 0x00, 0x10, 0x00,
66 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 };
70 VideoClientContext* video;
72 UINT32 ScaledWidth, ScaledHeight;
73 MAPPED_GEOMETRY* geometry;
75 UINT64 startTimeStamp;
79 UINT64 lastPublishTime, nextPublishTime;
80 volatile LONG refCounter;
88 MAPPED_GEOMETRY* geometry;
92 PresentationContext* presentation;
96struct s_VideoClientContextPriv
98 VideoClientContext* video;
99 GeometryClientContext* geometry;
102 wBufferPool* surfacePool;
103 UINT32 publishedFrames;
104 UINT32 droppedFrames;
106 UINT64 nextFeedbackTime;
107 PresentationContext* currentPresentation;
110static void PresentationContext_unref(PresentationContext** presentation);
111static void VideoClientContextPriv_free(VideoClientContextPriv* priv);
113static const char* video_command_name(BYTE cmd)
117 case TSMM_START_PRESENTATION:
119 case TSMM_STOP_PRESENTATION:
126static void video_client_context_set_geometry(VideoClientContext* video,
127 GeometryClientContext* geometry)
130 WINPR_ASSERT(video->priv);
131 video->priv->geometry = geometry;
134static VideoClientContextPriv* VideoClientContextPriv_new(VideoClientContext* video)
136 VideoClientContextPriv* ret = NULL;
139 ret = calloc(1,
sizeof(*ret));
143 ret->frames = Queue_New(TRUE, 10, 2);
146 WLog_ERR(TAG,
"unable to allocate frames queue");
150 ret->surfacePool = BufferPool_New(FALSE, 0, 16);
151 if (!ret->surfacePool)
153 WLog_ERR(TAG,
"unable to create surface pool");
157 if (!InitializeCriticalSectionAndSpinCount(&ret->framesLock, 4 * 1000))
159 WLog_ERR(TAG,
"unable to initialize frames lock");
168 ret->lastSentRate = 30;
172 VideoClientContextPriv_free(ret);
176static BOOL PresentationContext_ref(PresentationContext* presentation)
178 WINPR_ASSERT(presentation);
180 InterlockedIncrement(&presentation->refCounter);
184static PresentationContext* PresentationContext_new(VideoClientContext* video, BYTE PresentationId,
185 UINT32 x, UINT32 y, UINT32 width, UINT32 height)
187 size_t s = 4ULL * width * height;
188 PresentationContext* ret = NULL;
195 ret = calloc(1,
sizeof(*ret));
200 ret->PresentationId = PresentationId;
202 ret->h264 = h264_context_new(FALSE);
205 WLog_ERR(TAG,
"unable to create a h264 context");
208 if (!h264_context_reset(ret->h264, width, height))
211 ret->currentSample = Stream_New(NULL, 4096);
212 if (!ret->currentSample)
214 WLog_ERR(TAG,
"unable to create current packet stream");
218 ret->surface = video->createSurface(video, x, y, width, height);
221 WLog_ERR(TAG,
"unable to create surface");
225 if (!PresentationContext_ref(ret))
231 PresentationContext_unref(&ret);
235static void PresentationContext_unref(PresentationContext** ppresentation)
237 PresentationContext* presentation = NULL;
238 MAPPED_GEOMETRY* geometry = NULL;
240 WINPR_ASSERT(ppresentation);
242 presentation = *ppresentation;
246 if (InterlockedDecrement(&presentation->refCounter) > 0)
249 geometry = presentation->geometry;
252 geometry->MappedGeometryUpdate = NULL;
253 geometry->MappedGeometryClear = NULL;
254 geometry->custom = NULL;
255 mappedGeometryUnref(geometry);
258 h264_context_free(presentation->h264);
259 Stream_Free(presentation->currentSample, TRUE);
260 presentation->video->deleteSurface(presentation->video, presentation->surface);
262 *ppresentation = NULL;
265static void VideoFrame_free(VideoFrame** pframe)
267 VideoFrame* frame = NULL;
269 WINPR_ASSERT(pframe);
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);
285static VideoFrame* VideoFrame_new(VideoClientContextPriv* priv, PresentationContext* presentation,
286 MAPPED_GEOMETRY* geom)
288 VideoFrame* frame = NULL;
292 WINPR_ASSERT(presentation);
295 surface = presentation->surface;
296 WINPR_ASSERT(surface);
298 frame = calloc(1,
sizeof(VideoFrame));
302 mappedGeometryRef(geom);
304 frame->publishTime = presentation->lastPublishTime;
305 frame->geometry = geom;
306 frame->w = surface->alignedWidth;
307 frame->h = surface->alignedHeight;
308 frame->scanline = surface->scanline;
310 frame->surfaceData = BufferPool_Take(priv->surfacePool, 1ll * frame->scanline * frame->h);
311 if (!frame->surfaceData)
314 frame->presentation = presentation;
315 if (!PresentationContext_ref(frame->presentation))
321 VideoFrame_free(&frame);
325void VideoClientContextPriv_free(VideoClientContextPriv* priv)
330 EnterCriticalSection(&priv->framesLock);
334 while (Queue_Count(priv->frames))
336 VideoFrame* frame = Queue_Dequeue(priv->frames);
338 VideoFrame_free(&frame);
342 Queue_Free(priv->frames);
343 LeaveCriticalSection(&priv->framesLock);
345 DeleteCriticalSection(&priv->framesLock);
347 if (priv->currentPresentation)
348 PresentationContext_unref(&priv->currentPresentation);
350 BufferPool_Free(priv->surfacePool);
354static UINT video_control_send_presentation_response(VideoClientContext* context,
357 BYTE buf[12] = { 0 };
359 VIDEO_PLUGIN* video = NULL;
360 IWTSVirtualChannel* channel = NULL;
363 WINPR_ASSERT(context);
366 video = (VIDEO_PLUGIN*)context->handle;
369 s = Stream_New(buf, 12);
371 return CHANNEL_RC_NO_MEMORY;
373 Stream_Write_UINT32(s, 12);
374 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_PRESENTATION_RESPONSE);
375 Stream_Write_UINT8(s, resp->PresentationId);
377 Stream_SealLength(s);
379 channel = video->control_callback->channel_callback->channel;
380 ret = channel->Write(channel, 12, buf, NULL);
381 Stream_Free(s, FALSE);
386static BOOL video_onMappedGeometryUpdate(MAPPED_GEOMETRY* geometry)
388 PresentationContext* presentation = NULL;
391 WINPR_ASSERT(geometry);
393 presentation = (PresentationContext*)geometry->custom;
394 WINPR_ASSERT(presentation);
396 r = &geometry->geometry.boundingRect;
398 "geometry updated topGeom=(%" PRId32
",%" PRId32
"-%" PRId32
"x%" PRId32
399 ") geom=(%" PRId32
",%" PRId32
"-%" PRId32
"x%" PRId32
") rects=(%" PRId16
",%" PRId16
400 "-%" PRId16
"x%" PRId16
")",
401 geometry->topLevelLeft, geometry->topLevelTop,
402 geometry->topLevelRight - geometry->topLevelLeft,
403 geometry->topLevelBottom - geometry->topLevelTop,
405 geometry->left, geometry->top, geometry->right - geometry->left,
406 geometry->bottom - geometry->top,
408 r->x, r->y, r->width, r->height);
410 presentation->surface->x =
411 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelLeft + geometry->left);
412 presentation->surface->y =
413 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelTop + geometry->top);
418static BOOL video_onMappedGeometryClear(MAPPED_GEOMETRY* geometry)
420 PresentationContext* presentation = NULL;
422 WINPR_ASSERT(geometry);
424 presentation = (PresentationContext*)geometry->custom;
425 WINPR_ASSERT(presentation);
427 mappedGeometryUnref(presentation->geometry);
428 presentation->geometry = NULL;
432static UINT video_PresentationRequest(VideoClientContext* video,
435 UINT ret = CHANNEL_RC_OK;
440 VideoClientContextPriv* priv = video->priv;
443 if (req->Command == TSMM_START_PRESENTATION)
445 MAPPED_GEOMETRY* geom = NULL;
448 if (memcmp(req->VideoSubtypeId, MFVideoFormat_H264, 16) != 0)
450 WLog_ERR(TAG,
"not a H264 video, ignoring request");
451 return CHANNEL_RC_OK;
454 if (priv->currentPresentation)
456 if (priv->currentPresentation->PresentationId == req->PresentationId)
458 WLog_ERR(TAG,
"ignoring start request for existing presentation %" PRIu8,
459 req->PresentationId);
460 return CHANNEL_RC_OK;
463 WLog_ERR(TAG,
"releasing current presentation %" PRIu8, req->PresentationId);
464 PresentationContext_unref(&priv->currentPresentation);
469 WLog_ERR(TAG,
"geometry channel not ready, ignoring request");
470 return CHANNEL_RC_OK;
473 geom = HashTable_GetItemValue(priv->geometry->geometries, &(req->GeometryMappingId));
476 WLog_ERR(TAG,
"geometry mapping 0x%" PRIx64
" not registered", req->GeometryMappingId);
477 return CHANNEL_RC_OK;
480 WLog_DBG(TAG,
"creating presentation 0x%x", req->PresentationId);
481 priv->currentPresentation = PresentationContext_new(
482 video, req->PresentationId,
483 WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelLeft + geom->left),
484 WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelTop + geom->top), req->SourceWidth,
486 if (!priv->currentPresentation)
488 WLog_ERR(TAG,
"unable to create presentation video");
489 return CHANNEL_RC_NO_MEMORY;
492 mappedGeometryRef(geom);
493 priv->currentPresentation->geometry = geom;
495 priv->currentPresentation->video = video;
496 priv->currentPresentation->ScaledWidth = req->ScaledWidth;
497 priv->currentPresentation->ScaledHeight = req->ScaledHeight;
499 geom->custom = priv->currentPresentation;
500 geom->MappedGeometryUpdate = video_onMappedGeometryUpdate;
501 geom->MappedGeometryClear = video_onMappedGeometryClear;
504 resp.PresentationId = req->PresentationId;
505 ret = video_control_send_presentation_response(video, &resp);
507 else if (req->Command == TSMM_STOP_PRESENTATION)
509 WLog_DBG(TAG,
"stopping presentation 0x%x", req->PresentationId);
510 if (!priv->currentPresentation)
512 WLog_ERR(TAG,
"unknown presentation to stop %" PRIu8, req->PresentationId);
513 return CHANNEL_RC_OK;
516 priv->droppedFrames = 0;
517 priv->publishedFrames = 0;
518 PresentationContext_unref(&priv->currentPresentation);
524static UINT video_read_tsmm_presentation_req(VideoClientContext* context,
wStream* s)
528 WINPR_ASSERT(context);
531 if (!Stream_CheckAndLogRequiredLength(TAG, s, 60))
532 return ERROR_INVALID_DATA;
534 Stream_Read_UINT8(s, req.PresentationId);
535 Stream_Read_UINT8(s, req.Version);
536 Stream_Read_UINT8(s, req.Command);
537 Stream_Read_UINT8(s, req.FrameRate);
539 Stream_Seek_UINT16(s);
540 Stream_Seek_UINT16(s);
542 Stream_Read_UINT32(s, req.SourceWidth);
543 Stream_Read_UINT32(s, req.SourceHeight);
544 Stream_Read_UINT32(s, req.ScaledWidth);
545 Stream_Read_UINT32(s, req.ScaledHeight);
546 Stream_Read_UINT64(s, req.hnsTimestampOffset);
547 Stream_Read_UINT64(s, req.GeometryMappingId);
548 Stream_Read(s, req.VideoSubtypeId, 16);
550 Stream_Read_UINT32(s, req.cbExtra);
552 if (!Stream_CheckAndLogRequiredLength(TAG, s, req.cbExtra))
553 return ERROR_INVALID_DATA;
555 req.pExtraData = Stream_Pointer(s);
558 "presentationReq: id:%" PRIu8
" version:%" PRIu8
559 " command:%s srcWidth/srcHeight=%" PRIu32
"x%" PRIu32
" scaled Width/Height=%" PRIu32
560 "x%" PRIu32
" timestamp=%" PRIu64
" mappingId=%" PRIx64
"",
561 req.PresentationId, req.Version, video_command_name(req.Command), req.SourceWidth,
562 req.SourceHeight, req.ScaledWidth, req.ScaledHeight, req.hnsTimestampOffset,
563 req.GeometryMappingId);
565 return video_PresentationRequest(context, &req);
573static UINT video_control_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
576 VIDEO_PLUGIN* video = NULL;
577 VideoClientContext* context = NULL;
578 UINT ret = CHANNEL_RC_OK;
580 UINT32 packetType = 0;
582 WINPR_ASSERT(callback);
585 video = (VIDEO_PLUGIN*)callback->plugin;
588 context = (VideoClientContext*)video->wtsPlugin.pInterface;
589 WINPR_ASSERT(context);
591 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
592 return ERROR_INVALID_DATA;
594 Stream_Read_UINT32(s, cbSize);
597 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected 8", cbSize);
598 return ERROR_INVALID_DATA;
600 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
601 return ERROR_INVALID_DATA;
603 Stream_Read_UINT32(s, packetType);
606 case TSMM_PACKET_TYPE_PRESENTATION_REQUEST:
607 ret = video_read_tsmm_presentation_req(context, s);
610 WLog_ERR(TAG,
"not expecting packet type %" PRIu32
"", packetType);
611 ret = ERROR_UNSUPPORTED_TYPE;
618static UINT video_control_send_client_notification(VideoClientContext* context,
623 VIDEO_PLUGIN* video = NULL;
624 IWTSVirtualChannel* channel = NULL;
628 WINPR_ASSERT(context);
631 video = (VIDEO_PLUGIN*)context->handle;
634 s = Stream_New(buf, 32);
636 return CHANNEL_RC_NO_MEMORY;
639 Stream_Seek_UINT32(s);
640 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_CLIENT_NOTIFICATION);
641 Stream_Write_UINT8(s, notif->PresentationId);
642 Stream_Write_UINT8(s, notif->NotificationType);
644 if (notif->NotificationType == TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE)
646 Stream_Write_UINT32(s, 16);
649 Stream_Write_UINT32(s, notif->FramerateOverride.Flags);
650 Stream_Write_UINT32(s, notif->FramerateOverride.DesiredFrameRate);
651 Stream_Zero(s, 4ULL * 2ULL);
657 Stream_Write_UINT32(s, 0);
660 Stream_SealLength(s);
661 Stream_SetPosition(s, 0);
662 Stream_Write_UINT32(s, cbSize);
663 Stream_Free(s, FALSE);
665 WINPR_ASSERT(video->control_callback);
666 WINPR_ASSERT(video->control_callback->channel_callback);
668 channel = video->control_callback->channel_callback->channel;
669 WINPR_ASSERT(channel);
670 WINPR_ASSERT(channel->Write);
672 ret = channel->Write(channel, cbSize, buf, NULL);
677static void video_timer(VideoClientContext* video, UINT64 now)
679 PresentationContext* presentation = NULL;
680 VideoClientContextPriv* priv = NULL;
681 VideoFrame* peekFrame = NULL;
682 VideoFrame* frame = NULL;
689 EnterCriticalSection(&priv->framesLock);
692 peekFrame = (VideoFrame*)Queue_Peek(priv->frames);
696 if (peekFrame->publishTime > now)
701 WLog_DBG(TAG,
"dropping frame @%" PRIu64, frame->publishTime);
702 priv->droppedFrames++;
703 VideoFrame_free(&frame);
706 Queue_Dequeue(priv->frames);
708 LeaveCriticalSection(&priv->framesLock);
713 presentation = frame->presentation;
715 priv->publishedFrames++;
716 memcpy(presentation->surface->data, frame->surfaceData, 1ull * frame->scanline * frame->h);
718 WINPR_ASSERT(video->showSurface);
719 video->showSurface(video, presentation->surface, presentation->ScaledWidth,
720 presentation->ScaledHeight);
722 VideoFrame_free(&frame);
725 if (priv->nextFeedbackTime < now)
730 if (priv->publishedFrames && priv->currentPresentation)
732 UINT32 computedRate = 0;
734 PresentationContext_ref(priv->currentPresentation);
736 if (priv->droppedFrames)
743 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
747 computedRate = priv->lastSentRate - 2;
758 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
759 computedRate = XF_VIDEO_UNLIMITED_RATE;
762 computedRate = priv->lastSentRate + 2;
763 if (computedRate > XF_VIDEO_UNLIMITED_RATE)
764 computedRate = XF_VIDEO_UNLIMITED_RATE;
768 if (computedRate != priv->lastSentRate)
772 WINPR_ASSERT(priv->currentPresentation);
773 notif.PresentationId = priv->currentPresentation->PresentationId;
774 notif.NotificationType = TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE;
775 if (computedRate == XF_VIDEO_UNLIMITED_RATE)
777 notif.FramerateOverride.Flags = 0x01;
778 notif.FramerateOverride.DesiredFrameRate = 0x00;
782 notif.FramerateOverride.Flags = 0x02;
783 notif.FramerateOverride.DesiredFrameRate = computedRate;
786 video_control_send_client_notification(video, ¬if);
787 priv->lastSentRate = computedRate;
790 "server notified with rate %" PRIu32
" published=%" PRIu32
792 priv->lastSentRate, priv->publishedFrames, priv->droppedFrames);
795 PresentationContext_unref(&priv->currentPresentation);
798 WLog_DBG(TAG,
"currentRate=%" PRIu32
" published=%" PRIu32
" dropped=%" PRIu32,
799 priv->lastSentRate, priv->publishedFrames, priv->droppedFrames);
801 priv->droppedFrames = 0;
802 priv->publishedFrames = 0;
803 priv->nextFeedbackTime = now + 1000;
807static UINT video_VideoData(VideoClientContext* context,
const TSMM_VIDEO_DATA* data)
809 VideoClientContextPriv* priv = NULL;
810 PresentationContext* presentation = NULL;
813 WINPR_ASSERT(context);
816 priv = context->priv;
819 presentation = priv->currentPresentation;
822 WLog_ERR(TAG,
"no current presentation");
823 return CHANNEL_RC_OK;
826 if (presentation->PresentationId != data->PresentationId)
828 WLog_ERR(TAG,
"current presentation id=%" PRIu8
" doesn't match data id=%" PRIu8,
829 presentation->PresentationId, data->PresentationId);
830 return CHANNEL_RC_OK;
833 if (!Stream_EnsureRemainingCapacity(presentation->currentSample, data->cbSample))
835 WLog_ERR(TAG,
"unable to expand the current packet");
836 return CHANNEL_RC_NO_MEMORY;
839 Stream_Write(presentation->currentSample, data->pSample, data->cbSample);
841 if (data->CurrentPacketIndex == data->PacketsInSample)
844 H264_CONTEXT* h264 = presentation->h264;
845 UINT64 startTime = GetTickCount64();
846 UINT64 timeAfterH264 = 0;
847 MAPPED_GEOMETRY* geom = presentation->geometry;
849 const RECTANGLE_16 rect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedWidth),
850 WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedHeight) };
851 Stream_SealLength(presentation->currentSample);
852 Stream_SetPosition(presentation->currentSample, 0);
854 timeAfterH264 = GetTickCount64();
855 if (data->SampleNumber == 1)
857 presentation->lastPublishTime = startTime;
860 presentation->lastPublishTime += (data->hnsDuration / 10000);
861 if (presentation->lastPublishTime <= timeAfterH264 + 10)
865 const size_t len = Stream_Length(presentation->currentSample);
866 if (len > UINT32_MAX)
867 return CHANNEL_RC_OK;
871 avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
872 surface->data, surface->format, surface->scanline,
873 surface->alignedWidth, surface->alignedHeight, &rect, 1);
876 return CHANNEL_RC_OK;
878 WINPR_ASSERT(context->showSurface);
879 context->showSurface(context, presentation->surface, presentation->ScaledWidth,
880 presentation->ScaledHeight);
882 priv->publishedFrames++;
885 EnterCriticalSection(&priv->framesLock);
886 while (Queue_Count(priv->frames) > 0)
888 VideoFrame* frame = Queue_Dequeue(priv->frames);
891 priv->droppedFrames++;
892 VideoFrame_free(&frame);
896 LeaveCriticalSection(&priv->framesLock);
899 WLog_DBG(TAG,
"showing frame (%d dropped)", dropped);
903 const size_t len = Stream_Length(presentation->currentSample);
904 if (len > UINT32_MAX)
905 return CHANNEL_RC_OK;
907 BOOL enqueueResult = 0;
908 VideoFrame* frame = VideoFrame_new(priv, presentation, geom);
911 WLog_ERR(TAG,
"unable to create frame");
912 return CHANNEL_RC_NO_MEMORY;
916 avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
917 frame->surfaceData, surface->format, surface->scanline,
918 surface->alignedWidth, surface->alignedHeight, &rect, 1);
921 VideoFrame_free(&frame);
922 return CHANNEL_RC_OK;
925 EnterCriticalSection(&priv->framesLock);
926 enqueueResult = Queue_Enqueue(priv->frames, frame);
927 LeaveCriticalSection(&priv->framesLock);
931 WLog_ERR(TAG,
"unable to enqueue frame");
932 VideoFrame_free(&frame);
933 return CHANNEL_RC_NO_MEMORY;
937 WLog_DBG(TAG,
"scheduling frame in %" PRIu32
" ms", (frame->publishTime - startTime));
941 return CHANNEL_RC_OK;
944static UINT video_data_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
947 VIDEO_PLUGIN* video = NULL;
948 VideoClientContext* context = NULL;
950 UINT32 packetType = 0;
953 video = (VIDEO_PLUGIN*)callback->plugin;
954 context = (VideoClientContext*)video->wtsPlugin.pInterface;
956 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
957 return ERROR_INVALID_DATA;
959 Stream_Read_UINT32(s, cbSize);
962 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected >= 8", cbSize);
963 return ERROR_INVALID_DATA;
966 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
967 return ERROR_INVALID_DATA;
969 Stream_Read_UINT32(s, packetType);
970 if (packetType != TSMM_PACKET_TYPE_VIDEO_DATA)
972 WLog_ERR(TAG,
"only expecting VIDEO_DATA on the data channel");
973 return ERROR_INVALID_DATA;
976 if (!Stream_CheckAndLogRequiredLength(TAG, s, 32))
977 return ERROR_INVALID_DATA;
979 Stream_Read_UINT8(s, data.PresentationId);
980 Stream_Read_UINT8(s, data.Version);
981 Stream_Read_UINT8(s, data.Flags);
982 Stream_Seek_UINT8(s);
983 Stream_Read_UINT64(s, data.hnsTimestamp);
984 Stream_Read_UINT64(s, data.hnsDuration);
985 Stream_Read_UINT16(s, data.CurrentPacketIndex);
986 Stream_Read_UINT16(s, data.PacketsInSample);
987 Stream_Read_UINT32(s, data.SampleNumber);
988 Stream_Read_UINT32(s, data.cbSample);
989 if (!Stream_CheckAndLogRequiredLength(TAG, s, data.cbSample))
990 return ERROR_INVALID_DATA;
991 data.pSample = Stream_Pointer(s);
1001 return video_VideoData(context, &data);
1009static UINT video_control_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1011 free(pChannelCallback);
1012 return CHANNEL_RC_OK;
1015static UINT video_data_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1017 free(pChannelCallback);
1018 return CHANNEL_RC_OK;
1027static UINT video_control_on_new_channel_connection(IWTSListenerCallback* listenerCallback,
1028 IWTSVirtualChannel* channel, BYTE* Data,
1030 IWTSVirtualChannelCallback** ppCallback)
1037 WINPR_UNUSED(pbAccept);
1042 WLog_ERR(TAG,
"calloc failed!");
1043 return CHANNEL_RC_NO_MEMORY;
1046 callback->iface.OnDataReceived = video_control_on_data_received;
1047 callback->iface.OnClose = video_control_on_close;
1048 callback->plugin = listener_callback->plugin;
1049 callback->channel_mgr = listener_callback->channel_mgr;
1050 callback->channel = channel;
1051 listener_callback->channel_callback = callback;
1053 *ppCallback = (IWTSVirtualChannelCallback*)callback;
1055 return CHANNEL_RC_OK;
1059static UINT video_data_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1060 IWTSVirtualChannel* pChannel, BYTE* Data,
1062 IWTSVirtualChannelCallback** ppCallback)
1069 WINPR_UNUSED(pbAccept);
1074 WLog_ERR(TAG,
"calloc failed!");
1075 return CHANNEL_RC_NO_MEMORY;
1078 callback->iface.OnDataReceived = video_data_on_data_received;
1079 callback->iface.OnClose = video_data_on_close;
1080 callback->plugin = listener_callback->plugin;
1081 callback->channel_mgr = listener_callback->channel_mgr;
1082 callback->channel = pChannel;
1083 listener_callback->channel_callback = callback;
1085 *ppCallback = (IWTSVirtualChannelCallback*)callback;
1087 return CHANNEL_RC_OK;
1095static UINT video_plugin_initialize(IWTSPlugin* plugin, IWTSVirtualChannelManager* channelMgr)
1098 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)plugin;
1101 if (video->initialized)
1103 WLog_ERR(TAG,
"[%s] channel initialized twice, aborting", VIDEO_CONTROL_DVC_CHANNEL_NAME);
1104 return ERROR_INVALID_DATA;
1106 video->control_callback = callback =
1110 WLog_ERR(TAG,
"calloc for control callback failed!");
1111 return CHANNEL_RC_NO_MEMORY;
1114 callback->iface.OnNewChannelConnection = video_control_on_new_channel_connection;
1115 callback->plugin = plugin;
1116 callback->channel_mgr = channelMgr;
1118 status = channelMgr->CreateListener(channelMgr, VIDEO_CONTROL_DVC_CHANNEL_NAME, 0,
1119 &callback->iface, &(video->controlListener));
1121 if (status != CHANNEL_RC_OK)
1123 video->controlListener->pInterface = video->wtsPlugin.pInterface;
1125 video->data_callback = callback =
1129 WLog_ERR(TAG,
"calloc for data callback failed!");
1130 return CHANNEL_RC_NO_MEMORY;
1133 callback->iface.OnNewChannelConnection = video_data_on_new_channel_connection;
1134 callback->plugin = plugin;
1135 callback->channel_mgr = channelMgr;
1137 status = channelMgr->CreateListener(channelMgr, VIDEO_DATA_DVC_CHANNEL_NAME, 0,
1138 &callback->iface, &(video->dataListener));
1140 if (status == CHANNEL_RC_OK)
1141 video->dataListener->pInterface = video->wtsPlugin.pInterface;
1143 video->initialized = status == CHANNEL_RC_OK;
1152static UINT video_plugin_terminated(IWTSPlugin* pPlugin)
1154 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)pPlugin;
1156 if (video->control_callback)
1158 IWTSVirtualChannelManager* mgr = video->control_callback->channel_mgr;
1160 IFCALL(mgr->DestroyListener, mgr, video->controlListener);
1162 if (video->data_callback)
1164 IWTSVirtualChannelManager* mgr = video->data_callback->channel_mgr;
1166 IFCALL(mgr->DestroyListener, mgr, video->dataListener);
1170 VideoClientContextPriv_free(video->context->priv);
1172 free(video->control_callback);
1173 free(video->data_callback);
1174 free(video->wtsPlugin.pInterface);
1176 return CHANNEL_RC_OK;
1187FREERDP_ENTRY_POINT(UINT VCAPITYPE video_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1189 UINT error = CHANNEL_RC_OK;
1190 VIDEO_PLUGIN* videoPlugin = NULL;
1191 VideoClientContext* videoContext = NULL;
1192 VideoClientContextPriv* priv = NULL;
1194 videoPlugin = (VIDEO_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints,
"video");
1197 videoPlugin = (VIDEO_PLUGIN*)calloc(1,
sizeof(VIDEO_PLUGIN));
1200 WLog_ERR(TAG,
"calloc failed!");
1201 return CHANNEL_RC_NO_MEMORY;
1204 videoPlugin->wtsPlugin.Initialize = video_plugin_initialize;
1205 videoPlugin->wtsPlugin.Connected = NULL;
1206 videoPlugin->wtsPlugin.Disconnected = NULL;
1207 videoPlugin->wtsPlugin.Terminated = video_plugin_terminated;
1209 videoContext = (VideoClientContext*)calloc(1,
sizeof(VideoClientContext));
1212 WLog_ERR(TAG,
"calloc failed!");
1214 return CHANNEL_RC_NO_MEMORY;
1217 priv = VideoClientContextPriv_new(videoContext);
1220 WLog_ERR(TAG,
"VideoClientContextPriv_new failed!");
1223 return CHANNEL_RC_NO_MEMORY;
1226 videoContext->handle = (
void*)videoPlugin;
1227 videoContext->priv = priv;
1228 videoContext->timer = video_timer;
1229 videoContext->setGeometry = video_client_context_set_geometry;
1231 videoPlugin->wtsPlugin.pInterface = (
void*)videoContext;
1232 videoPlugin->context = videoContext;
1234 error = pEntryPoints->RegisterPlugin(pEntryPoints,
"video", &videoPlugin->wtsPlugin);
1238 WLog_ERR(TAG,
"could not get video Plugin.");
1239 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