20 #include <freerdp/config.h>
26 #include <winpr/crt.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
65 static 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;
82 } PresentationContext;
88 MAPPED_GEOMETRY* geometry;
92 PresentationContext* presentation;
96 struct s_VideoClientContextPriv
98 VideoClientContext* video;
99 GeometryClientContext* geometry;
102 wBufferPool* surfacePool;
103 UINT32 publishedFrames;
104 UINT32 droppedFrames;
106 UINT64 nextFeedbackTime;
107 PresentationContext* currentPresentation;
110 static void PresentationContext_unref(PresentationContext** presentation);
111 static void VideoClientContextPriv_free(VideoClientContextPriv* priv);
113 static const char* video_command_name(BYTE cmd)
117 case TSMM_START_PRESENTATION:
119 case TSMM_STOP_PRESENTATION:
126 static void video_client_context_set_geometry(VideoClientContext* video,
127 GeometryClientContext* geometry)
130 WINPR_ASSERT(video->priv);
131 video->priv->geometry = geometry;
134 static 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);
176 static BOOL PresentationContext_ref(PresentationContext* presentation)
178 WINPR_ASSERT(presentation);
180 InterlockedIncrement(&presentation->refCounter);
184 static PresentationContext* PresentationContext_new(VideoClientContext* video, BYTE PresentationId,
185 UINT32 x, UINT32 y, UINT32 width, UINT32 height)
187 size_t s = 4ULL * width * height;
188 VideoClientContextPriv* priv = NULL;
189 PresentationContext* ret = NULL;
199 ret = calloc(1,
sizeof(*ret));
204 ret->PresentationId = PresentationId;
206 ret->h264 = h264_context_new(FALSE);
209 WLog_ERR(TAG,
"unable to create a h264 context");
212 if (!h264_context_reset(ret->h264, width, height))
215 ret->currentSample = Stream_New(NULL, 4096);
216 if (!ret->currentSample)
218 WLog_ERR(TAG,
"unable to create current packet stream");
222 ret->surface = video->createSurface(video, x, y, width, height);
225 WLog_ERR(TAG,
"unable to create surface");
229 if (!PresentationContext_ref(ret))
235 PresentationContext_unref(&ret);
239 static void PresentationContext_unref(PresentationContext** ppresentation)
241 PresentationContext* presentation = NULL;
242 MAPPED_GEOMETRY* geometry = NULL;
244 WINPR_ASSERT(ppresentation);
246 presentation = *ppresentation;
250 if (InterlockedDecrement(&presentation->refCounter) > 0)
253 geometry = presentation->geometry;
256 geometry->MappedGeometryUpdate = NULL;
257 geometry->MappedGeometryClear = NULL;
258 geometry->custom = NULL;
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 = NULL;
269 static void VideoFrame_free(VideoFrame** pframe)
271 VideoFrame* frame = NULL;
273 WINPR_ASSERT(pframe);
278 mappedGeometryUnref(frame->geometry);
280 WINPR_ASSERT(frame->presentation);
281 WINPR_ASSERT(frame->presentation->video);
282 WINPR_ASSERT(frame->presentation->video->priv);
283 BufferPool_Return(frame->presentation->video->priv->surfacePool, frame->surfaceData);
284 PresentationContext_unref(&frame->presentation);
289 static VideoFrame* VideoFrame_new(VideoClientContextPriv* priv, PresentationContext* presentation,
290 MAPPED_GEOMETRY* geom)
292 VideoFrame* frame = NULL;
296 WINPR_ASSERT(presentation);
299 surface = presentation->surface;
300 WINPR_ASSERT(surface);
302 frame = calloc(1,
sizeof(VideoFrame));
306 mappedGeometryRef(geom);
308 frame->publishTime = presentation->lastPublishTime;
309 frame->geometry = geom;
310 frame->w = surface->alignedWidth;
311 frame->h = surface->alignedHeight;
312 frame->scanline = surface->scanline;
314 frame->surfaceData = BufferPool_Take(priv->surfacePool, 1ll * frame->scanline * frame->h);
315 if (!frame->surfaceData)
318 frame->presentation = presentation;
319 if (!PresentationContext_ref(frame->presentation))
325 VideoFrame_free(&frame);
329 void VideoClientContextPriv_free(VideoClientContextPriv* priv)
334 EnterCriticalSection(&priv->framesLock);
338 while (Queue_Count(priv->frames))
340 VideoFrame* frame = Queue_Dequeue(priv->frames);
342 VideoFrame_free(&frame);
346 Queue_Free(priv->frames);
347 LeaveCriticalSection(&priv->framesLock);
349 DeleteCriticalSection(&priv->framesLock);
351 if (priv->currentPresentation)
352 PresentationContext_unref(&priv->currentPresentation);
354 BufferPool_Free(priv->surfacePool);
358 static UINT video_control_send_presentation_response(VideoClientContext* context,
361 BYTE buf[12] = { 0 };
363 VIDEO_PLUGIN* video = NULL;
364 IWTSVirtualChannel* channel = NULL;
367 WINPR_ASSERT(context);
370 video = (VIDEO_PLUGIN*)context->handle;
373 s = Stream_New(buf, 12);
375 return CHANNEL_RC_NO_MEMORY;
377 Stream_Write_UINT32(s, 12);
378 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_PRESENTATION_RESPONSE);
379 Stream_Write_UINT8(s, resp->PresentationId);
381 Stream_SealLength(s);
383 channel = video->control_callback->channel_callback->channel;
384 ret = channel->Write(channel, 12, buf, NULL);
385 Stream_Free(s, FALSE);
390 static BOOL video_onMappedGeometryUpdate(MAPPED_GEOMETRY* geometry)
392 PresentationContext* presentation = NULL;
395 WINPR_ASSERT(geometry);
397 presentation = (PresentationContext*)geometry->custom;
398 WINPR_ASSERT(presentation);
400 r = &geometry->geometry.boundingRect;
402 "geometry updated topGeom=(%" PRId32
",%" PRId32
"-%" PRId32
"x%" PRId32
403 ") geom=(%" PRId32
",%" PRId32
"-%" PRId32
"x%" PRId32
") rects=(%" PRId16
",%" PRId16
404 "-%" PRId16
"x%" PRId16
")",
405 geometry->topLevelLeft, geometry->topLevelTop,
406 geometry->topLevelRight - geometry->topLevelLeft,
407 geometry->topLevelBottom - geometry->topLevelTop,
409 geometry->left, geometry->top, geometry->right - geometry->left,
410 geometry->bottom - geometry->top,
412 r->x, r->y, r->width, r->height);
414 presentation->surface->x =
415 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelLeft + geometry->left);
416 presentation->surface->y =
417 WINPR_ASSERTING_INT_CAST(uint32_t, geometry->topLevelTop + geometry->top);
422 static BOOL video_onMappedGeometryClear(MAPPED_GEOMETRY* geometry)
424 PresentationContext* presentation = NULL;
426 WINPR_ASSERT(geometry);
428 presentation = (PresentationContext*)geometry->custom;
429 WINPR_ASSERT(presentation);
431 mappedGeometryUnref(presentation->geometry);
432 presentation->geometry = NULL;
436 static UINT video_PresentationRequest(VideoClientContext* video,
439 UINT ret = CHANNEL_RC_OK;
444 VideoClientContextPriv* priv = video->priv;
447 if (req->Command == TSMM_START_PRESENTATION)
449 MAPPED_GEOMETRY* geom = NULL;
452 if (memcmp(req->VideoSubtypeId, MFVideoFormat_H264, 16) != 0)
454 WLog_ERR(TAG,
"not a H264 video, ignoring request");
455 return CHANNEL_RC_OK;
458 if (priv->currentPresentation)
460 if (priv->currentPresentation->PresentationId == req->PresentationId)
462 WLog_ERR(TAG,
"ignoring start request for existing presentation %" PRIu8,
463 req->PresentationId);
464 return CHANNEL_RC_OK;
467 WLog_ERR(TAG,
"releasing current presentation %" PRIu8, req->PresentationId);
468 PresentationContext_unref(&priv->currentPresentation);
473 WLog_ERR(TAG,
"geometry channel not ready, ignoring request");
474 return CHANNEL_RC_OK;
477 geom = HashTable_GetItemValue(priv->geometry->geometries, &(req->GeometryMappingId));
480 WLog_ERR(TAG,
"geometry mapping 0x%" PRIx64
" not registered", req->GeometryMappingId);
481 return CHANNEL_RC_OK;
484 WLog_DBG(TAG,
"creating presentation 0x%x", req->PresentationId);
485 priv->currentPresentation = PresentationContext_new(
486 video, req->PresentationId,
487 WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelLeft + geom->left),
488 WINPR_ASSERTING_INT_CAST(uint32_t, geom->topLevelTop + geom->top), req->SourceWidth,
490 if (!priv->currentPresentation)
492 WLog_ERR(TAG,
"unable to create presentation video");
493 return CHANNEL_RC_NO_MEMORY;
496 mappedGeometryRef(geom);
497 priv->currentPresentation->geometry = geom;
499 priv->currentPresentation->video = video;
500 priv->currentPresentation->ScaledWidth = req->ScaledWidth;
501 priv->currentPresentation->ScaledHeight = req->ScaledHeight;
503 geom->custom = priv->currentPresentation;
504 geom->MappedGeometryUpdate = video_onMappedGeometryUpdate;
505 geom->MappedGeometryClear = video_onMappedGeometryClear;
508 resp.PresentationId = req->PresentationId;
509 ret = video_control_send_presentation_response(video, &resp);
511 else if (req->Command == TSMM_STOP_PRESENTATION)
513 WLog_DBG(TAG,
"stopping presentation 0x%x", req->PresentationId);
514 if (!priv->currentPresentation)
516 WLog_ERR(TAG,
"unknown presentation to stop %" PRIu8, req->PresentationId);
517 return CHANNEL_RC_OK;
520 priv->droppedFrames = 0;
521 priv->publishedFrames = 0;
522 PresentationContext_unref(&priv->currentPresentation);
528 static 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);
577 static UINT video_control_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
580 VIDEO_PLUGIN* video = NULL;
581 VideoClientContext* context = NULL;
582 UINT ret = CHANNEL_RC_OK;
584 UINT32 packetType = 0;
586 WINPR_ASSERT(callback);
589 video = (VIDEO_PLUGIN*)callback->plugin;
592 context = (VideoClientContext*)video->wtsPlugin.pInterface;
593 WINPR_ASSERT(context);
595 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
596 return ERROR_INVALID_DATA;
598 Stream_Read_UINT32(s, cbSize);
601 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected 8", cbSize);
602 return ERROR_INVALID_DATA;
604 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
605 return ERROR_INVALID_DATA;
607 Stream_Read_UINT32(s, packetType);
610 case TSMM_PACKET_TYPE_PRESENTATION_REQUEST:
611 ret = video_read_tsmm_presentation_req(context, s);
614 WLog_ERR(TAG,
"not expecting packet type %" PRIu32
"", packetType);
615 ret = ERROR_UNSUPPORTED_TYPE;
622 static UINT video_control_send_client_notification(VideoClientContext* context,
627 VIDEO_PLUGIN* video = NULL;
628 IWTSVirtualChannel* channel = NULL;
632 WINPR_ASSERT(context);
635 video = (VIDEO_PLUGIN*)context->handle;
638 s = Stream_New(buf, 32);
640 return CHANNEL_RC_NO_MEMORY;
643 Stream_Seek_UINT32(s);
644 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_CLIENT_NOTIFICATION);
645 Stream_Write_UINT8(s, notif->PresentationId);
646 Stream_Write_UINT8(s, notif->NotificationType);
648 if (notif->NotificationType == TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE)
650 Stream_Write_UINT32(s, 16);
653 Stream_Write_UINT32(s, notif->FramerateOverride.Flags);
654 Stream_Write_UINT32(s, notif->FramerateOverride.DesiredFrameRate);
655 Stream_Zero(s, 4ULL * 2ULL);
661 Stream_Write_UINT32(s, 0);
664 Stream_SealLength(s);
665 Stream_SetPosition(s, 0);
666 Stream_Write_UINT32(s, cbSize);
667 Stream_Free(s, FALSE);
669 WINPR_ASSERT(video->control_callback);
670 WINPR_ASSERT(video->control_callback->channel_callback);
672 channel = video->control_callback->channel_callback->channel;
673 WINPR_ASSERT(channel);
674 WINPR_ASSERT(channel->Write);
676 ret = channel->Write(channel, cbSize, buf, NULL);
681 static void video_timer(VideoClientContext* video, UINT64 now)
683 PresentationContext* presentation = NULL;
684 VideoClientContextPriv* priv = NULL;
685 VideoFrame* peekFrame = NULL;
686 VideoFrame* frame = NULL;
693 EnterCriticalSection(&priv->framesLock);
696 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(&frame);
710 Queue_Dequeue(priv->frames);
712 LeaveCriticalSection(&priv->framesLock);
717 presentation = frame->presentation;
719 priv->publishedFrames++;
720 memcpy(presentation->surface->data, frame->surfaceData, 1ull * frame->scanline * frame->h);
722 WINPR_ASSERT(video->showSurface);
723 video->showSurface(video, presentation->surface, presentation->ScaledWidth,
724 presentation->ScaledHeight);
726 VideoFrame_free(&frame);
729 if (priv->nextFeedbackTime < now)
734 if (priv->publishedFrames && priv->currentPresentation)
736 UINT32 computedRate = 0;
738 PresentationContext_ref(priv->currentPresentation);
740 if (priv->droppedFrames)
747 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
751 computedRate = priv->lastSentRate - 2;
762 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
763 computedRate = XF_VIDEO_UNLIMITED_RATE;
766 computedRate = priv->lastSentRate + 2;
767 if (computedRate > XF_VIDEO_UNLIMITED_RATE)
768 computedRate = XF_VIDEO_UNLIMITED_RATE;
772 if (computedRate != priv->lastSentRate)
776 WINPR_ASSERT(priv->currentPresentation);
777 notif.PresentationId = priv->currentPresentation->PresentationId;
778 notif.NotificationType = TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE;
779 if (computedRate == XF_VIDEO_UNLIMITED_RATE)
781 notif.FramerateOverride.Flags = 0x01;
782 notif.FramerateOverride.DesiredFrameRate = 0x00;
786 notif.FramerateOverride.Flags = 0x02;
787 notif.FramerateOverride.DesiredFrameRate = computedRate;
790 video_control_send_client_notification(video, ¬if);
791 priv->lastSentRate = computedRate;
794 "server notified with rate %" PRIu32
" published=%" PRIu32
796 priv->lastSentRate, priv->publishedFrames, priv->droppedFrames);
799 PresentationContext_unref(&priv->currentPresentation);
802 WLog_DBG(TAG,
"currentRate=%" PRIu32
" published=%" PRIu32
" dropped=%" PRIu32,
803 priv->lastSentRate, priv->publishedFrames, priv->droppedFrames);
805 priv->droppedFrames = 0;
806 priv->publishedFrames = 0;
807 priv->nextFeedbackTime = now + 1000;
811 static UINT video_VideoData(VideoClientContext* context,
const TSMM_VIDEO_DATA* data)
813 VideoClientContextPriv* priv = NULL;
814 PresentationContext* presentation = NULL;
817 WINPR_ASSERT(context);
820 priv = context->priv;
823 presentation = priv->currentPresentation;
826 WLog_ERR(TAG,
"no current presentation");
827 return CHANNEL_RC_OK;
830 if (presentation->PresentationId != data->PresentationId)
832 WLog_ERR(TAG,
"current presentation id=%" PRIu8
" doesn't match data id=%" PRIu8,
833 presentation->PresentationId, data->PresentationId);
834 return CHANNEL_RC_OK;
837 if (!Stream_EnsureRemainingCapacity(presentation->currentSample, data->cbSample))
839 WLog_ERR(TAG,
"unable to expand the current packet");
840 return CHANNEL_RC_NO_MEMORY;
843 Stream_Write(presentation->currentSample, data->pSample, data->cbSample);
845 if (data->CurrentPacketIndex == data->PacketsInSample)
848 H264_CONTEXT* h264 = presentation->h264;
849 UINT64 startTime = GetTickCount64();
850 UINT64 timeAfterH264 = 0;
851 MAPPED_GEOMETRY* geom = presentation->geometry;
853 const RECTANGLE_16 rect = { 0, 0, WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedWidth),
854 WINPR_ASSERTING_INT_CAST(UINT16, surface->alignedHeight) };
855 Stream_SealLength(presentation->currentSample);
856 Stream_SetPosition(presentation->currentSample, 0);
858 timeAfterH264 = GetTickCount64();
859 if (data->SampleNumber == 1)
861 presentation->lastPublishTime = startTime;
864 presentation->lastPublishTime += (data->hnsDuration / 10000);
865 if (presentation->lastPublishTime <= timeAfterH264 + 10)
869 const size_t len = Stream_Length(presentation->currentSample);
870 if (len > UINT32_MAX)
871 return CHANNEL_RC_OK;
875 avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
876 surface->data, surface->format, surface->scanline,
877 surface->alignedWidth, surface->alignedHeight, &rect, 1);
880 return CHANNEL_RC_OK;
882 WINPR_ASSERT(context->showSurface);
883 context->showSurface(context, presentation->surface, presentation->ScaledWidth,
884 presentation->ScaledHeight);
886 priv->publishedFrames++;
889 EnterCriticalSection(&priv->framesLock);
890 while (Queue_Count(priv->frames) > 0)
892 VideoFrame* frame = Queue_Dequeue(priv->frames);
895 priv->droppedFrames++;
896 VideoFrame_free(&frame);
900 LeaveCriticalSection(&priv->framesLock);
903 WLog_DBG(TAG,
"showing frame (%d dropped)", dropped);
907 const size_t len = Stream_Length(presentation->currentSample);
908 if (len > UINT32_MAX)
909 return CHANNEL_RC_OK;
911 BOOL enqueueResult = 0;
912 VideoFrame* frame = VideoFrame_new(priv, presentation, geom);
915 WLog_ERR(TAG,
"unable to create frame");
916 return CHANNEL_RC_NO_MEMORY;
920 avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
921 frame->surfaceData, surface->format, surface->scanline,
922 surface->alignedWidth, surface->alignedHeight, &rect, 1);
925 VideoFrame_free(&frame);
926 return CHANNEL_RC_OK;
929 EnterCriticalSection(&priv->framesLock);
930 enqueueResult = Queue_Enqueue(priv->frames, frame);
931 LeaveCriticalSection(&priv->framesLock);
935 WLog_ERR(TAG,
"unable to enqueue frame");
936 VideoFrame_free(&frame);
937 return CHANNEL_RC_NO_MEMORY;
941 WLog_DBG(TAG,
"scheduling frame in %" PRIu32
" ms", (frame->publishTime - startTime));
945 return CHANNEL_RC_OK;
948 static UINT video_data_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
951 VIDEO_PLUGIN* video = NULL;
952 VideoClientContext* context = NULL;
954 UINT32 packetType = 0;
957 video = (VIDEO_PLUGIN*)callback->plugin;
958 context = (VideoClientContext*)video->wtsPlugin.pInterface;
960 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
961 return ERROR_INVALID_DATA;
963 Stream_Read_UINT32(s, cbSize);
966 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected >= 8", cbSize);
967 return ERROR_INVALID_DATA;
970 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
971 return ERROR_INVALID_DATA;
973 Stream_Read_UINT32(s, packetType);
974 if (packetType != TSMM_PACKET_TYPE_VIDEO_DATA)
976 WLog_ERR(TAG,
"only expecting VIDEO_DATA on the data channel");
977 return ERROR_INVALID_DATA;
980 if (!Stream_CheckAndLogRequiredLength(TAG, s, 32))
981 return ERROR_INVALID_DATA;
983 Stream_Read_UINT8(s, data.PresentationId);
984 Stream_Read_UINT8(s, data.Version);
985 Stream_Read_UINT8(s, data.Flags);
986 Stream_Seek_UINT8(s);
987 Stream_Read_UINT64(s, data.hnsTimestamp);
988 Stream_Read_UINT64(s, data.hnsDuration);
989 Stream_Read_UINT16(s, data.CurrentPacketIndex);
990 Stream_Read_UINT16(s, data.PacketsInSample);
991 Stream_Read_UINT32(s, data.SampleNumber);
992 Stream_Read_UINT32(s, data.cbSample);
993 if (!Stream_CheckAndLogRequiredLength(TAG, s, data.cbSample))
994 return ERROR_INVALID_DATA;
995 data.pSample = Stream_Pointer(s);
1005 return video_VideoData(context, &data);
1013 static UINT video_control_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1015 free(pChannelCallback);
1016 return CHANNEL_RC_OK;
1019 static UINT video_data_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1021 free(pChannelCallback);
1022 return CHANNEL_RC_OK;
1031 static UINT video_control_on_new_channel_connection(IWTSListenerCallback* listenerCallback,
1032 IWTSVirtualChannel* channel, BYTE* Data,
1034 IWTSVirtualChannelCallback** ppCallback)
1041 WINPR_UNUSED(pbAccept);
1046 WLog_ERR(TAG,
"calloc failed!");
1047 return CHANNEL_RC_NO_MEMORY;
1050 callback->iface.OnDataReceived = video_control_on_data_received;
1051 callback->iface.OnClose = video_control_on_close;
1052 callback->plugin = listener_callback->plugin;
1053 callback->channel_mgr = listener_callback->channel_mgr;
1054 callback->channel = channel;
1055 listener_callback->channel_callback = callback;
1057 *ppCallback = (IWTSVirtualChannelCallback*)callback;
1059 return CHANNEL_RC_OK;
1063 static UINT video_data_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1064 IWTSVirtualChannel* pChannel, BYTE* Data,
1066 IWTSVirtualChannelCallback** ppCallback)
1073 WINPR_UNUSED(pbAccept);
1078 WLog_ERR(TAG,
"calloc failed!");
1079 return CHANNEL_RC_NO_MEMORY;
1082 callback->iface.OnDataReceived = video_data_on_data_received;
1083 callback->iface.OnClose = video_data_on_close;
1084 callback->plugin = listener_callback->plugin;
1085 callback->channel_mgr = listener_callback->channel_mgr;
1086 callback->channel = pChannel;
1087 listener_callback->channel_callback = callback;
1089 *ppCallback = (IWTSVirtualChannelCallback*)callback;
1091 return CHANNEL_RC_OK;
1099 static UINT video_plugin_initialize(IWTSPlugin* plugin, IWTSVirtualChannelManager* channelMgr)
1102 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)plugin;
1105 if (video->initialized)
1107 WLog_ERR(TAG,
"[%s] channel initialized twice, aborting", VIDEO_CONTROL_DVC_CHANNEL_NAME);
1108 return ERROR_INVALID_DATA;
1110 video->control_callback = callback =
1114 WLog_ERR(TAG,
"calloc for control callback failed!");
1115 return CHANNEL_RC_NO_MEMORY;
1118 callback->iface.OnNewChannelConnection = video_control_on_new_channel_connection;
1119 callback->plugin = plugin;
1120 callback->channel_mgr = channelMgr;
1122 status = channelMgr->CreateListener(channelMgr, VIDEO_CONTROL_DVC_CHANNEL_NAME, 0,
1123 &callback->iface, &(video->controlListener));
1125 if (status != CHANNEL_RC_OK)
1127 video->controlListener->pInterface = video->wtsPlugin.pInterface;
1129 video->data_callback = callback =
1133 WLog_ERR(TAG,
"calloc for data callback failed!");
1134 return CHANNEL_RC_NO_MEMORY;
1137 callback->iface.OnNewChannelConnection = video_data_on_new_channel_connection;
1138 callback->plugin = plugin;
1139 callback->channel_mgr = channelMgr;
1141 status = channelMgr->CreateListener(channelMgr, VIDEO_DATA_DVC_CHANNEL_NAME, 0,
1142 &callback->iface, &(video->dataListener));
1144 if (status == CHANNEL_RC_OK)
1145 video->dataListener->pInterface = video->wtsPlugin.pInterface;
1147 video->initialized = status == CHANNEL_RC_OK;
1156 static UINT video_plugin_terminated(IWTSPlugin* pPlugin)
1158 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)pPlugin;
1160 if (video->control_callback)
1162 IWTSVirtualChannelManager* mgr = video->control_callback->channel_mgr;
1164 IFCALL(mgr->DestroyListener, mgr, video->controlListener);
1166 if (video->data_callback)
1168 IWTSVirtualChannelManager* mgr = video->data_callback->channel_mgr;
1170 IFCALL(mgr->DestroyListener, mgr, video->dataListener);
1174 VideoClientContextPriv_free(video->context->priv);
1176 free(video->control_callback);
1177 free(video->data_callback);
1178 free(video->wtsPlugin.pInterface);
1180 return CHANNEL_RC_OK;
1191 FREERDP_ENTRY_POINT(UINT VCAPITYPE video_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1193 UINT error = CHANNEL_RC_OK;
1194 VIDEO_PLUGIN* videoPlugin = NULL;
1195 VideoClientContext* videoContext = NULL;
1196 VideoClientContextPriv* priv = NULL;
1198 videoPlugin = (VIDEO_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints,
"video");
1201 videoPlugin = (VIDEO_PLUGIN*)calloc(1,
sizeof(VIDEO_PLUGIN));
1204 WLog_ERR(TAG,
"calloc failed!");
1205 return CHANNEL_RC_NO_MEMORY;
1208 videoPlugin->wtsPlugin.Initialize = video_plugin_initialize;
1209 videoPlugin->wtsPlugin.Connected = NULL;
1210 videoPlugin->wtsPlugin.Disconnected = NULL;
1211 videoPlugin->wtsPlugin.Terminated = video_plugin_terminated;
1213 videoContext = (VideoClientContext*)calloc(1,
sizeof(VideoClientContext));
1216 WLog_ERR(TAG,
"calloc failed!");
1218 return CHANNEL_RC_NO_MEMORY;
1221 priv = VideoClientContextPriv_new(videoContext);
1224 WLog_ERR(TAG,
"VideoClientContextPriv_new failed!");
1227 return CHANNEL_RC_NO_MEMORY;
1230 videoContext->handle = (
void*)videoPlugin;
1231 videoContext->priv = priv;
1232 videoContext->timer = video_timer;
1233 videoContext->setGeometry = video_client_context_set_geometry;
1235 videoPlugin->wtsPlugin.pInterface = (
void*)videoContext;
1236 videoPlugin->context = videoContext;
1238 error = pEntryPoints->RegisterPlugin(pEntryPoints,
"video", &videoPlugin->wtsPlugin);
1242 WLog_ERR(TAG,
"could not get video Plugin.");
1243 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