20 #include <freerdp/config.h>
26 #include <winpr/crt.h>
27 #include <winpr/assert.h>
28 #include <winpr/synch.h>
29 #include <winpr/print.h>
30 #include <winpr/stream.h>
31 #include <winpr/cmdline.h>
32 #include <winpr/collections.h>
33 #include <winpr/interlocked.h>
34 #include <winpr/sysinfo.h>
36 #include <freerdp/addin.h>
37 #include <freerdp/primitives.h>
38 #include <freerdp/client/channels.h>
39 #include <freerdp/client/geometry.h>
40 #include <freerdp/client/video.h>
41 #include <freerdp/channels/log.h>
42 #include <freerdp/codec/h264.h>
43 #include <freerdp/codec/yuv.h>
45 #define TAG CHANNELS_TAG("video")
47 #include "video_main.h"
53 IWTSListener* controlListener;
54 IWTSListener* dataListener;
58 VideoClientContext* context;
62 #define XF_VIDEO_UNLIMITED_RATE 31
64 static const BYTE MFVideoFormat_H264[] = {
'H',
'2',
'6',
'4', 0x00, 0x00, 0x10, 0x00,
65 0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 };
69 VideoClientContext* video;
71 UINT32 ScaledWidth, ScaledHeight;
72 MAPPED_GEOMETRY* geometry;
74 UINT64 startTimeStamp;
78 UINT64 lastPublishTime, nextPublishTime;
79 volatile LONG refCounter;
81 } PresentationContext;
87 MAPPED_GEOMETRY* geometry;
91 PresentationContext* presentation;
95 struct s_VideoClientContextPriv
97 VideoClientContext* video;
98 GeometryClientContext* geometry;
101 wBufferPool* surfacePool;
102 UINT32 publishedFrames;
103 UINT32 droppedFrames;
105 UINT64 nextFeedbackTime;
106 PresentationContext* currentPresentation;
109 static void PresentationContext_unref(PresentationContext** presentation);
110 static void VideoClientContextPriv_free(VideoClientContextPriv* priv);
112 static const char* video_command_name(BYTE cmd)
116 case TSMM_START_PRESENTATION:
118 case TSMM_STOP_PRESENTATION:
125 static void video_client_context_set_geometry(VideoClientContext* video,
126 GeometryClientContext* geometry)
129 WINPR_ASSERT(video->priv);
130 video->priv->geometry = geometry;
133 static VideoClientContextPriv* VideoClientContextPriv_new(VideoClientContext* video)
135 VideoClientContextPriv* ret = NULL;
138 ret = calloc(1,
sizeof(*ret));
142 ret->frames = Queue_New(TRUE, 10, 2);
145 WLog_ERR(TAG,
"unable to allocate frames queue");
149 ret->surfacePool = BufferPool_New(FALSE, 0, 16);
150 if (!ret->surfacePool)
152 WLog_ERR(TAG,
"unable to create surface pool");
156 if (!InitializeCriticalSectionAndSpinCount(&ret->framesLock, 4 * 1000))
158 WLog_ERR(TAG,
"unable to initialize frames lock");
167 ret->lastSentRate = 30;
171 VideoClientContextPriv_free(ret);
175 static BOOL PresentationContext_ref(PresentationContext* presentation)
177 WINPR_ASSERT(presentation);
179 InterlockedIncrement(&presentation->refCounter);
183 static PresentationContext* PresentationContext_new(VideoClientContext* video, BYTE PresentationId,
184 UINT32 x, UINT32 y, UINT32 width, UINT32 height)
186 size_t s = 4ULL * width * height;
187 VideoClientContextPriv* priv = NULL;
188 PresentationContext* ret = NULL;
198 ret = calloc(1,
sizeof(*ret));
203 ret->PresentationId = PresentationId;
205 ret->h264 = h264_context_new(FALSE);
208 WLog_ERR(TAG,
"unable to create a h264 context");
211 if (!h264_context_reset(ret->h264, width, height))
214 ret->currentSample = Stream_New(NULL, 4096);
215 if (!ret->currentSample)
217 WLog_ERR(TAG,
"unable to create current packet stream");
221 ret->surface = video->createSurface(video, x, y, width, height);
224 WLog_ERR(TAG,
"unable to create surface");
228 if (!PresentationContext_ref(ret))
234 PresentationContext_unref(&ret);
238 static void PresentationContext_unref(PresentationContext** ppresentation)
240 PresentationContext* presentation = NULL;
241 MAPPED_GEOMETRY* geometry = NULL;
243 WINPR_ASSERT(ppresentation);
245 presentation = *ppresentation;
249 if (InterlockedDecrement(&presentation->refCounter) > 0)
252 geometry = presentation->geometry;
255 geometry->MappedGeometryUpdate = NULL;
256 geometry->MappedGeometryClear = NULL;
257 geometry->custom = NULL;
258 mappedGeometryUnref(geometry);
261 h264_context_free(presentation->h264);
262 Stream_Free(presentation->currentSample, TRUE);
263 presentation->video->deleteSurface(presentation->video, presentation->surface);
265 *ppresentation = NULL;
268 static void VideoFrame_free(VideoFrame** pframe)
270 VideoFrame* frame = NULL;
272 WINPR_ASSERT(pframe);
277 mappedGeometryUnref(frame->geometry);
279 WINPR_ASSERT(frame->presentation);
280 WINPR_ASSERT(frame->presentation->video);
281 WINPR_ASSERT(frame->presentation->video->priv);
282 BufferPool_Return(frame->presentation->video->priv->surfacePool, frame->surfaceData);
283 PresentationContext_unref(&frame->presentation);
288 static VideoFrame* VideoFrame_new(VideoClientContextPriv* priv, PresentationContext* presentation,
289 MAPPED_GEOMETRY* geom)
291 VideoFrame* frame = NULL;
295 WINPR_ASSERT(presentation);
298 surface = presentation->surface;
299 WINPR_ASSERT(surface);
301 frame = calloc(1,
sizeof(VideoFrame));
305 mappedGeometryRef(geom);
307 frame->publishTime = presentation->lastPublishTime;
308 frame->geometry = geom;
309 frame->w = surface->alignedWidth;
310 frame->h = surface->alignedHeight;
311 frame->scanline = surface->scanline;
313 frame->surfaceData = BufferPool_Take(priv->surfacePool, 1ll * frame->scanline * frame->h);
314 if (!frame->surfaceData)
317 frame->presentation = presentation;
318 if (!PresentationContext_ref(frame->presentation))
324 VideoFrame_free(&frame);
328 void VideoClientContextPriv_free(VideoClientContextPriv* priv)
333 EnterCriticalSection(&priv->framesLock);
337 while (Queue_Count(priv->frames))
339 VideoFrame* frame = Queue_Dequeue(priv->frames);
341 VideoFrame_free(&frame);
345 Queue_Free(priv->frames);
346 LeaveCriticalSection(&priv->framesLock);
348 DeleteCriticalSection(&priv->framesLock);
350 if (priv->currentPresentation)
351 PresentationContext_unref(&priv->currentPresentation);
353 BufferPool_Free(priv->surfacePool);
357 static UINT video_control_send_presentation_response(VideoClientContext* context,
360 BYTE buf[12] = { 0 };
362 VIDEO_PLUGIN* video = NULL;
363 IWTSVirtualChannel* channel = NULL;
366 WINPR_ASSERT(context);
369 video = (VIDEO_PLUGIN*)context->handle;
372 s = Stream_New(buf, 12);
374 return CHANNEL_RC_NO_MEMORY;
376 Stream_Write_UINT32(s, 12);
377 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_PRESENTATION_RESPONSE);
378 Stream_Write_UINT8(s, resp->PresentationId);
380 Stream_SealLength(s);
382 channel = video->control_callback->channel_callback->channel;
383 ret = channel->Write(channel, 12, buf, NULL);
384 Stream_Free(s, FALSE);
389 static BOOL video_onMappedGeometryUpdate(MAPPED_GEOMETRY* geometry)
391 PresentationContext* presentation = NULL;
394 WINPR_ASSERT(geometry);
396 presentation = (PresentationContext*)geometry->custom;
397 WINPR_ASSERT(presentation);
399 r = &geometry->geometry.boundingRect;
401 "geometry updated topGeom=(%" PRId32
",%" PRId32
"-%" PRId32
"x%" PRId32
402 ") geom=(%" PRId32
",%" PRId32
"-%" PRId32
"x%" PRId32
") rects=(%" PRId16
",%" PRId16
403 "-%" PRId16
"x%" PRId16
")",
404 geometry->topLevelLeft, geometry->topLevelTop,
405 geometry->topLevelRight - geometry->topLevelLeft,
406 geometry->topLevelBottom - geometry->topLevelTop,
408 geometry->left, geometry->top, geometry->right - geometry->left,
409 geometry->bottom - geometry->top,
411 r->x, r->y, r->width, r->height);
413 presentation->surface->x = geometry->topLevelLeft + geometry->left;
414 presentation->surface->y = geometry->topLevelTop + geometry->top;
419 static BOOL video_onMappedGeometryClear(MAPPED_GEOMETRY* geometry)
421 PresentationContext* presentation = NULL;
423 WINPR_ASSERT(geometry);
425 presentation = (PresentationContext*)geometry->custom;
426 WINPR_ASSERT(presentation);
428 mappedGeometryUnref(presentation->geometry);
429 presentation->geometry = NULL;
433 static UINT video_PresentationRequest(VideoClientContext* video,
436 UINT ret = CHANNEL_RC_OK;
441 VideoClientContextPriv* priv = video->priv;
444 if (req->Command == TSMM_START_PRESENTATION)
446 MAPPED_GEOMETRY* geom = NULL;
449 if (memcmp(req->VideoSubtypeId, MFVideoFormat_H264, 16) != 0)
451 WLog_ERR(TAG,
"not a H264 video, ignoring request");
452 return CHANNEL_RC_OK;
455 if (priv->currentPresentation)
457 if (priv->currentPresentation->PresentationId == req->PresentationId)
459 WLog_ERR(TAG,
"ignoring start request for existing presentation %" PRIu8,
460 req->PresentationId);
461 return CHANNEL_RC_OK;
464 WLog_ERR(TAG,
"releasing current presentation %" PRIu8, req->PresentationId);
465 PresentationContext_unref(&priv->currentPresentation);
470 WLog_ERR(TAG,
"geometry channel not ready, ignoring request");
471 return CHANNEL_RC_OK;
474 geom = HashTable_GetItemValue(priv->geometry->geometries, &(req->GeometryMappingId));
477 WLog_ERR(TAG,
"geometry mapping 0x%" PRIx64
" not registered", req->GeometryMappingId);
478 return CHANNEL_RC_OK;
481 WLog_DBG(TAG,
"creating presentation 0x%x", req->PresentationId);
482 priv->currentPresentation = PresentationContext_new(
483 video, req->PresentationId, geom->topLevelLeft + geom->left,
484 geom->topLevelTop + geom->top, req->SourceWidth, req->SourceHeight);
485 if (!priv->currentPresentation)
487 WLog_ERR(TAG,
"unable to create presentation video");
488 return CHANNEL_RC_NO_MEMORY;
491 mappedGeometryRef(geom);
492 priv->currentPresentation->geometry = geom;
494 priv->currentPresentation->video = video;
495 priv->currentPresentation->ScaledWidth = req->ScaledWidth;
496 priv->currentPresentation->ScaledHeight = req->ScaledHeight;
498 geom->custom = priv->currentPresentation;
499 geom->MappedGeometryUpdate = video_onMappedGeometryUpdate;
500 geom->MappedGeometryClear = video_onMappedGeometryClear;
503 resp.PresentationId = req->PresentationId;
504 ret = video_control_send_presentation_response(video, &resp);
506 else if (req->Command == TSMM_STOP_PRESENTATION)
508 WLog_DBG(TAG,
"stopping presentation 0x%x", req->PresentationId);
509 if (!priv->currentPresentation)
511 WLog_ERR(TAG,
"unknown presentation to stop %" PRIu8, req->PresentationId);
512 return CHANNEL_RC_OK;
515 priv->droppedFrames = 0;
516 priv->publishedFrames = 0;
517 PresentationContext_unref(&priv->currentPresentation);
523 static UINT video_read_tsmm_presentation_req(VideoClientContext* context,
wStream* s)
527 WINPR_ASSERT(context);
530 if (!Stream_CheckAndLogRequiredLength(TAG, s, 60))
531 return ERROR_INVALID_DATA;
533 Stream_Read_UINT8(s, req.PresentationId);
534 Stream_Read_UINT8(s, req.Version);
535 Stream_Read_UINT8(s, req.Command);
536 Stream_Read_UINT8(s, req.FrameRate);
538 Stream_Seek_UINT16(s);
539 Stream_Seek_UINT16(s);
541 Stream_Read_UINT32(s, req.SourceWidth);
542 Stream_Read_UINT32(s, req.SourceHeight);
543 Stream_Read_UINT32(s, req.ScaledWidth);
544 Stream_Read_UINT32(s, req.ScaledHeight);
545 Stream_Read_UINT64(s, req.hnsTimestampOffset);
546 Stream_Read_UINT64(s, req.GeometryMappingId);
547 Stream_Read(s, req.VideoSubtypeId, 16);
549 Stream_Read_UINT32(s, req.cbExtra);
551 if (!Stream_CheckAndLogRequiredLength(TAG, s, req.cbExtra))
552 return ERROR_INVALID_DATA;
554 req.pExtraData = Stream_Pointer(s);
557 "presentationReq: id:%" PRIu8
" version:%" PRIu8
558 " command:%s srcWidth/srcHeight=%" PRIu32
"x%" PRIu32
" scaled Width/Height=%" PRIu32
559 "x%" PRIu32
" timestamp=%" PRIu64
" mappingId=%" PRIx64
"",
560 req.PresentationId, req.Version, video_command_name(req.Command), req.SourceWidth,
561 req.SourceHeight, req.ScaledWidth, req.ScaledHeight, req.hnsTimestampOffset,
562 req.GeometryMappingId);
564 return video_PresentationRequest(context, &req);
572 static UINT video_control_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
575 VIDEO_PLUGIN* video = NULL;
576 VideoClientContext* context = NULL;
577 UINT ret = CHANNEL_RC_OK;
579 UINT32 packetType = 0;
581 WINPR_ASSERT(callback);
584 video = (VIDEO_PLUGIN*)callback->plugin;
587 context = (VideoClientContext*)video->wtsPlugin.pInterface;
588 WINPR_ASSERT(context);
590 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
591 return ERROR_INVALID_DATA;
593 Stream_Read_UINT32(s, cbSize);
596 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected 8", cbSize);
597 return ERROR_INVALID_DATA;
599 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
600 return ERROR_INVALID_DATA;
602 Stream_Read_UINT32(s, packetType);
605 case TSMM_PACKET_TYPE_PRESENTATION_REQUEST:
606 ret = video_read_tsmm_presentation_req(context, s);
609 WLog_ERR(TAG,
"not expecting packet type %" PRIu32
"", packetType);
610 ret = ERROR_UNSUPPORTED_TYPE;
617 static UINT video_control_send_client_notification(VideoClientContext* context,
622 VIDEO_PLUGIN* video = NULL;
623 IWTSVirtualChannel* channel = NULL;
627 WINPR_ASSERT(context);
630 video = (VIDEO_PLUGIN*)context->handle;
633 s = Stream_New(buf, 32);
635 return CHANNEL_RC_NO_MEMORY;
638 Stream_Seek_UINT32(s);
639 Stream_Write_UINT32(s, TSMM_PACKET_TYPE_CLIENT_NOTIFICATION);
640 Stream_Write_UINT8(s, notif->PresentationId);
641 Stream_Write_UINT8(s, notif->NotificationType);
643 if (notif->NotificationType == TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE)
645 Stream_Write_UINT32(s, 16);
648 Stream_Write_UINT32(s, notif->FramerateOverride.Flags);
649 Stream_Write_UINT32(s, notif->FramerateOverride.DesiredFrameRate);
650 Stream_Zero(s, 4ULL * 2ULL);
656 Stream_Write_UINT32(s, 0);
659 Stream_SealLength(s);
660 Stream_SetPosition(s, 0);
661 Stream_Write_UINT32(s, cbSize);
662 Stream_Free(s, FALSE);
664 WINPR_ASSERT(video->control_callback);
665 WINPR_ASSERT(video->control_callback->channel_callback);
667 channel = video->control_callback->channel_callback->channel;
668 WINPR_ASSERT(channel);
669 WINPR_ASSERT(channel->Write);
671 ret = channel->Write(channel, cbSize, buf, NULL);
676 static void video_timer(VideoClientContext* video, UINT64 now)
678 PresentationContext* presentation = NULL;
679 VideoClientContextPriv* priv = NULL;
680 VideoFrame* peekFrame = NULL;
681 VideoFrame* frame = NULL;
688 EnterCriticalSection(&priv->framesLock);
691 peekFrame = (VideoFrame*)Queue_Peek(priv->frames);
695 if (peekFrame->publishTime > now)
700 WLog_DBG(TAG,
"dropping frame @%" PRIu64, frame->publishTime);
701 priv->droppedFrames++;
702 VideoFrame_free(&frame);
705 Queue_Dequeue(priv->frames);
707 LeaveCriticalSection(&priv->framesLock);
712 presentation = frame->presentation;
714 priv->publishedFrames++;
715 memcpy(presentation->surface->data, frame->surfaceData, 1ull * frame->scanline * frame->h);
717 WINPR_ASSERT(video->showSurface);
718 video->showSurface(video, presentation->surface, presentation->ScaledWidth,
719 presentation->ScaledHeight);
721 VideoFrame_free(&frame);
724 if (priv->nextFeedbackTime < now)
729 if (priv->publishedFrames && priv->currentPresentation)
731 UINT32 computedRate = 0;
733 PresentationContext_ref(priv->currentPresentation);
735 if (priv->droppedFrames)
742 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
746 computedRate = priv->lastSentRate - 2;
757 if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
758 computedRate = XF_VIDEO_UNLIMITED_RATE;
761 computedRate = priv->lastSentRate + 2;
762 if (computedRate > XF_VIDEO_UNLIMITED_RATE)
763 computedRate = XF_VIDEO_UNLIMITED_RATE;
767 if (computedRate != priv->lastSentRate)
771 WINPR_ASSERT(priv->currentPresentation);
772 notif.PresentationId = priv->currentPresentation->PresentationId;
773 notif.NotificationType = TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE;
774 if (computedRate == XF_VIDEO_UNLIMITED_RATE)
776 notif.FramerateOverride.Flags = 0x01;
777 notif.FramerateOverride.DesiredFrameRate = 0x00;
781 notif.FramerateOverride.Flags = 0x02;
782 notif.FramerateOverride.DesiredFrameRate = computedRate;
785 video_control_send_client_notification(video, ¬if);
786 priv->lastSentRate = computedRate;
789 "server notified with rate %" PRIu32
" published=%" PRIu32
791 priv->lastSentRate, priv->publishedFrames, priv->droppedFrames);
794 PresentationContext_unref(&priv->currentPresentation);
797 WLog_DBG(TAG,
"currentRate=%" PRIu32
" published=%" PRIu32
" dropped=%" PRIu32,
798 priv->lastSentRate, priv->publishedFrames, priv->droppedFrames);
800 priv->droppedFrames = 0;
801 priv->publishedFrames = 0;
802 priv->nextFeedbackTime = now + 1000;
806 static UINT video_VideoData(VideoClientContext* context,
const TSMM_VIDEO_DATA* data)
808 VideoClientContextPriv* priv = NULL;
809 PresentationContext* presentation = NULL;
812 WINPR_ASSERT(context);
815 priv = context->priv;
818 presentation = priv->currentPresentation;
821 WLog_ERR(TAG,
"no current presentation");
822 return CHANNEL_RC_OK;
825 if (presentation->PresentationId != data->PresentationId)
827 WLog_ERR(TAG,
"current presentation id=%" PRIu8
" doesn't match data id=%" PRIu8,
828 presentation->PresentationId, data->PresentationId);
829 return CHANNEL_RC_OK;
832 if (!Stream_EnsureRemainingCapacity(presentation->currentSample, data->cbSample))
834 WLog_ERR(TAG,
"unable to expand the current packet");
835 return CHANNEL_RC_NO_MEMORY;
838 Stream_Write(presentation->currentSample, data->pSample, data->cbSample);
840 if (data->CurrentPacketIndex == data->PacketsInSample)
843 H264_CONTEXT* h264 = presentation->h264;
844 UINT64 startTime = GetTickCount64();
845 UINT64 timeAfterH264 = 0;
846 MAPPED_GEOMETRY* geom = presentation->geometry;
848 const RECTANGLE_16 rect = { 0, 0, surface->alignedWidth, surface->alignedHeight };
849 Stream_SealLength(presentation->currentSample);
850 Stream_SetPosition(presentation->currentSample, 0);
852 timeAfterH264 = GetTickCount64();
853 if (data->SampleNumber == 1)
855 presentation->lastPublishTime = startTime;
858 presentation->lastPublishTime += (data->hnsDuration / 10000);
859 if (presentation->lastPublishTime <= timeAfterH264 + 10)
863 const size_t len = Stream_Length(presentation->currentSample);
864 if (len > UINT32_MAX)
865 return CHANNEL_RC_OK;
869 avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
870 surface->data, surface->format, surface->scanline,
871 surface->alignedWidth, surface->alignedHeight, &rect, 1);
874 return CHANNEL_RC_OK;
876 WINPR_ASSERT(context->showSurface);
877 context->showSurface(context, presentation->surface, presentation->ScaledWidth,
878 presentation->ScaledHeight);
880 priv->publishedFrames++;
883 EnterCriticalSection(&priv->framesLock);
884 while (Queue_Count(priv->frames) > 0)
886 VideoFrame* frame = Queue_Dequeue(priv->frames);
889 priv->droppedFrames++;
890 VideoFrame_free(&frame);
894 LeaveCriticalSection(&priv->framesLock);
897 WLog_DBG(TAG,
"showing frame (%d dropped)", dropped);
901 const size_t len = Stream_Length(presentation->currentSample);
902 if (len > UINT32_MAX)
903 return CHANNEL_RC_OK;
905 BOOL enqueueResult = 0;
906 VideoFrame* frame = VideoFrame_new(priv, presentation, geom);
909 WLog_ERR(TAG,
"unable to create frame");
910 return CHANNEL_RC_NO_MEMORY;
914 avc420_decompress(h264, Stream_Pointer(presentation->currentSample), (UINT32)len,
915 frame->surfaceData, surface->format, surface->scanline,
916 surface->alignedWidth, surface->alignedHeight, &rect, 1);
919 VideoFrame_free(&frame);
920 return CHANNEL_RC_OK;
923 EnterCriticalSection(&priv->framesLock);
924 enqueueResult = Queue_Enqueue(priv->frames, frame);
925 LeaveCriticalSection(&priv->framesLock);
929 WLog_ERR(TAG,
"unable to enqueue frame");
930 VideoFrame_free(&frame);
931 return CHANNEL_RC_NO_MEMORY;
935 WLog_DBG(TAG,
"scheduling frame in %" PRIu32
" ms", (frame->publishTime - startTime));
939 return CHANNEL_RC_OK;
942 static UINT video_data_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* s)
945 VIDEO_PLUGIN* video = NULL;
946 VideoClientContext* context = NULL;
948 UINT32 packetType = 0;
951 video = (VIDEO_PLUGIN*)callback->plugin;
952 context = (VideoClientContext*)video->wtsPlugin.pInterface;
954 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
955 return ERROR_INVALID_DATA;
957 Stream_Read_UINT32(s, cbSize);
960 WLog_ERR(TAG,
"invalid cbSize %" PRIu32
", expected >= 8", cbSize);
961 return ERROR_INVALID_DATA;
964 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbSize - 4))
965 return ERROR_INVALID_DATA;
967 Stream_Read_UINT32(s, packetType);
968 if (packetType != TSMM_PACKET_TYPE_VIDEO_DATA)
970 WLog_ERR(TAG,
"only expecting VIDEO_DATA on the data channel");
971 return ERROR_INVALID_DATA;
974 if (!Stream_CheckAndLogRequiredLength(TAG, s, 32))
975 return ERROR_INVALID_DATA;
977 Stream_Read_UINT8(s, data.PresentationId);
978 Stream_Read_UINT8(s, data.Version);
979 Stream_Read_UINT8(s, data.Flags);
980 Stream_Seek_UINT8(s);
981 Stream_Read_UINT64(s, data.hnsTimestamp);
982 Stream_Read_UINT64(s, data.hnsDuration);
983 Stream_Read_UINT16(s, data.CurrentPacketIndex);
984 Stream_Read_UINT16(s, data.PacketsInSample);
985 Stream_Read_UINT32(s, data.SampleNumber);
986 Stream_Read_UINT32(s, data.cbSample);
987 if (!Stream_CheckAndLogRequiredLength(TAG, s, data.cbSample))
988 return ERROR_INVALID_DATA;
989 data.pSample = Stream_Pointer(s);
999 return video_VideoData(context, &data);
1007 static UINT video_control_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1009 free(pChannelCallback);
1010 return CHANNEL_RC_OK;
1013 static UINT video_data_on_close(IWTSVirtualChannelCallback* pChannelCallback)
1015 free(pChannelCallback);
1016 return CHANNEL_RC_OK;
1025 static UINT video_control_on_new_channel_connection(IWTSListenerCallback* listenerCallback,
1026 IWTSVirtualChannel* channel, BYTE* Data,
1028 IWTSVirtualChannelCallback** ppCallback)
1035 WINPR_UNUSED(pbAccept);
1040 WLog_ERR(TAG,
"calloc failed!");
1041 return CHANNEL_RC_NO_MEMORY;
1044 callback->iface.OnDataReceived = video_control_on_data_received;
1045 callback->iface.OnClose = video_control_on_close;
1046 callback->plugin = listener_callback->plugin;
1047 callback->channel_mgr = listener_callback->channel_mgr;
1048 callback->channel = channel;
1049 listener_callback->channel_callback = callback;
1051 *ppCallback = (IWTSVirtualChannelCallback*)callback;
1053 return CHANNEL_RC_OK;
1057 static UINT video_data_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
1058 IWTSVirtualChannel* pChannel, BYTE* Data,
1060 IWTSVirtualChannelCallback** ppCallback)
1067 WINPR_UNUSED(pbAccept);
1072 WLog_ERR(TAG,
"calloc failed!");
1073 return CHANNEL_RC_NO_MEMORY;
1076 callback->iface.OnDataReceived = video_data_on_data_received;
1077 callback->iface.OnClose = video_data_on_close;
1078 callback->plugin = listener_callback->plugin;
1079 callback->channel_mgr = listener_callback->channel_mgr;
1080 callback->channel = pChannel;
1081 listener_callback->channel_callback = callback;
1083 *ppCallback = (IWTSVirtualChannelCallback*)callback;
1085 return CHANNEL_RC_OK;
1093 static UINT video_plugin_initialize(IWTSPlugin* plugin, IWTSVirtualChannelManager* channelMgr)
1096 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)plugin;
1099 if (video->initialized)
1101 WLog_ERR(TAG,
"[%s] channel initialized twice, aborting", VIDEO_CONTROL_DVC_CHANNEL_NAME);
1102 return ERROR_INVALID_DATA;
1104 video->control_callback = callback =
1108 WLog_ERR(TAG,
"calloc for control callback failed!");
1109 return CHANNEL_RC_NO_MEMORY;
1112 callback->iface.OnNewChannelConnection = video_control_on_new_channel_connection;
1113 callback->plugin = plugin;
1114 callback->channel_mgr = channelMgr;
1116 status = channelMgr->CreateListener(channelMgr, VIDEO_CONTROL_DVC_CHANNEL_NAME, 0,
1117 &callback->iface, &(video->controlListener));
1119 if (status != CHANNEL_RC_OK)
1121 video->controlListener->pInterface = video->wtsPlugin.pInterface;
1123 video->data_callback = callback =
1127 WLog_ERR(TAG,
"calloc for data callback failed!");
1128 return CHANNEL_RC_NO_MEMORY;
1131 callback->iface.OnNewChannelConnection = video_data_on_new_channel_connection;
1132 callback->plugin = plugin;
1133 callback->channel_mgr = channelMgr;
1135 status = channelMgr->CreateListener(channelMgr, VIDEO_DATA_DVC_CHANNEL_NAME, 0,
1136 &callback->iface, &(video->dataListener));
1138 if (status == CHANNEL_RC_OK)
1139 video->dataListener->pInterface = video->wtsPlugin.pInterface;
1141 video->initialized = status == CHANNEL_RC_OK;
1150 static UINT video_plugin_terminated(IWTSPlugin* pPlugin)
1152 VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)pPlugin;
1154 if (video->control_callback)
1156 IWTSVirtualChannelManager* mgr = video->control_callback->channel_mgr;
1158 IFCALL(mgr->DestroyListener, mgr, video->controlListener);
1160 if (video->data_callback)
1162 IWTSVirtualChannelManager* mgr = video->data_callback->channel_mgr;
1164 IFCALL(mgr->DestroyListener, mgr, video->dataListener);
1168 VideoClientContextPriv_free(video->context->priv);
1170 free(video->control_callback);
1171 free(video->data_callback);
1172 free(video->wtsPlugin.pInterface);
1174 return CHANNEL_RC_OK;
1185 FREERDP_ENTRY_POINT(UINT VCAPITYPE video_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
1187 UINT error = CHANNEL_RC_OK;
1188 VIDEO_PLUGIN* videoPlugin = NULL;
1189 VideoClientContext* videoContext = NULL;
1190 VideoClientContextPriv* priv = NULL;
1192 videoPlugin = (VIDEO_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints,
"video");
1195 videoPlugin = (VIDEO_PLUGIN*)calloc(1,
sizeof(VIDEO_PLUGIN));
1198 WLog_ERR(TAG,
"calloc failed!");
1199 return CHANNEL_RC_NO_MEMORY;
1202 videoPlugin->wtsPlugin.Initialize = video_plugin_initialize;
1203 videoPlugin->wtsPlugin.Connected = NULL;
1204 videoPlugin->wtsPlugin.Disconnected = NULL;
1205 videoPlugin->wtsPlugin.Terminated = video_plugin_terminated;
1207 videoContext = (VideoClientContext*)calloc(1,
sizeof(VideoClientContext));
1210 WLog_ERR(TAG,
"calloc failed!");
1212 return CHANNEL_RC_NO_MEMORY;
1215 priv = VideoClientContextPriv_new(videoContext);
1218 WLog_ERR(TAG,
"VideoClientContextPriv_new failed!");
1221 return CHANNEL_RC_NO_MEMORY;
1224 videoContext->handle = (
void*)videoPlugin;
1225 videoContext->priv = priv;
1226 videoContext->timer = video_timer;
1227 videoContext->setGeometry = video_client_context_set_geometry;
1229 videoPlugin->wtsPlugin.pInterface = (
void*)videoContext;
1230 videoPlugin->context = videoContext;
1232 error = pEntryPoints->RegisterPlugin(pEntryPoints,
"video", &videoPlugin->wtsPlugin);
1236 WLog_ERR(TAG,
"could not get video Plugin.");
1237 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