Blame channels/video/client/video_main.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * Video Optimized Remoting Virtual Channel Extension
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2017 David Fort <contact@hardening-consulting.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <stdio.h>
Packit 1fb8d4
#include <stdlib.h>
Packit 1fb8d4
#include <string.h>
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/synch.h>
Packit 1fb8d4
#include <winpr/print.h>
Packit 1fb8d4
#include <winpr/stream.h>
Packit 1fb8d4
#include <winpr/cmdline.h>
Packit 1fb8d4
#include <winpr/collections.h>
Packit 1fb8d4
#include <winpr/interlocked.h>
Packit 1fb8d4
#include <winpr/sysinfo.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/addin.h>
Packit 1fb8d4
#include <freerdp/primitives.h>
Packit 1fb8d4
#include <freerdp/client/geometry.h>
Packit 1fb8d4
#include <freerdp/client/video.h>
Packit 1fb8d4
#include <freerdp/channels/log.h>
Packit 1fb8d4
#include <freerdp/codec/h264.h>
Packit 1fb8d4
#include <freerdp/codec/yuv.h>
Packit 1fb8d4
Packit 1fb8d4
#define TAG CHANNELS_TAG("video")
Packit 1fb8d4
Packit 1fb8d4
#include "video_main.h"
Packit 1fb8d4
Packit 1fb8d4
struct _VIDEO_CHANNEL_CALLBACK
Packit 1fb8d4
{
Packit 1fb8d4
	IWTSVirtualChannelCallback iface;
Packit 1fb8d4
Packit 1fb8d4
	IWTSPlugin* plugin;
Packit 1fb8d4
	IWTSVirtualChannelManager* channel_mgr;
Packit 1fb8d4
	IWTSVirtualChannel* channel;
Packit 1fb8d4
};
Packit 1fb8d4
typedef struct _VIDEO_CHANNEL_CALLBACK VIDEO_CHANNEL_CALLBACK;
Packit 1fb8d4
Packit 1fb8d4
struct _VIDEO_LISTENER_CALLBACK
Packit 1fb8d4
{
Packit 1fb8d4
	IWTSListenerCallback iface;
Packit 1fb8d4
Packit 1fb8d4
	IWTSPlugin* plugin;
Packit 1fb8d4
	IWTSVirtualChannelManager* channel_mgr;
Packit 1fb8d4
	VIDEO_CHANNEL_CALLBACK* channel_callback;
Packit 1fb8d4
};
Packit 1fb8d4
typedef struct _VIDEO_LISTENER_CALLBACK VIDEO_LISTENER_CALLBACK;
Packit 1fb8d4
Packit 1fb8d4
struct _VIDEO_PLUGIN
Packit 1fb8d4
{
Packit 1fb8d4
	IWTSPlugin wtsPlugin;
Packit 1fb8d4
Packit 1fb8d4
	IWTSListener* controlListener;
Packit 1fb8d4
	IWTSListener* dataListener;
Packit 1fb8d4
	VIDEO_LISTENER_CALLBACK* control_callback;
Packit 1fb8d4
	VIDEO_LISTENER_CALLBACK* data_callback;
Packit 1fb8d4
Packit Service 5a9772
	VideoClientContext* context;
Packit 1fb8d4
};
Packit 1fb8d4
typedef struct _VIDEO_PLUGIN VIDEO_PLUGIN;
Packit 1fb8d4
Packit 1fb8d4
#define XF_VIDEO_UNLIMITED_RATE 31
Packit 1fb8d4
Packit Service 5a9772
static const BYTE MFVideoFormat_H264[] = { 'H',  '2',  '6',  '4',  0x00, 0x00, 0x10, 0x00,
Packit Service 5a9772
	                                       0x80, 0x00, 0x00, 0xAA, 0x00, 0x38, 0x9B, 0x71 };
Packit 1fb8d4
Packit 1fb8d4
typedef struct _PresentationContext PresentationContext;
Packit 1fb8d4
typedef struct _VideoFrame VideoFrame;
Packit 1fb8d4
Packit 1fb8d4
/** @brief private data for the channel */
Packit 1fb8d4
struct _VideoClientContextPriv
Packit 1fb8d4
{
Packit Service 5a9772
	VideoClientContext* video;
Packit Service 5a9772
	GeometryClientContext* geometry;
Packit Service 5a9772
	wQueue* frames;
Packit 1fb8d4
	CRITICAL_SECTION framesLock;
Packit Service 5a9772
	wBufferPool* surfacePool;
Packit 1fb8d4
	UINT32 publishedFrames;
Packit 1fb8d4
	UINT32 droppedFrames;
Packit 1fb8d4
	UINT32 lastSentRate;
Packit 1fb8d4
	UINT64 nextFeedbackTime;
Packit Service 5a9772
	PresentationContext* currentPresentation;
Packit 1fb8d4
};
Packit 1fb8d4
Packit 1fb8d4
/** @brief */
Packit 1fb8d4
struct _VideoFrame
Packit 1fb8d4
{
Packit 1fb8d4
	UINT64 publishTime;
Packit 1fb8d4
	UINT64 hnsDuration;
Packit Service 5a9772
	MAPPED_GEOMETRY* geometry;
Packit 1fb8d4
	UINT32 w, h;
Packit Service 5a9772
	BYTE* surfaceData;
Packit Service 5a9772
	PresentationContext* presentation;
Packit 1fb8d4
};
Packit 1fb8d4
Packit 1fb8d4
/** @brief */
Packit 1fb8d4
struct _PresentationContext
Packit 1fb8d4
{
Packit Service 5a9772
	VideoClientContext* video;
Packit 1fb8d4
	BYTE PresentationId;
Packit 1fb8d4
	UINT32 SourceWidth, SourceHeight;
Packit 1fb8d4
	UINT32 ScaledWidth, ScaledHeight;
Packit Service 5a9772
	MAPPED_GEOMETRY* geometry;
Packit 1fb8d4
Packit 1fb8d4
	UINT64 startTimeStamp;
Packit 1fb8d4
	UINT64 publishOffset;
Packit Service 5a9772
	H264_CONTEXT* h264;
Packit Service 5a9772
	YUV_CONTEXT* yuv;
Packit Service 5a9772
	wStream* currentSample;
Packit 1fb8d4
	UINT64 lastPublishTime, nextPublishTime;
Packit 1fb8d4
	volatile LONG refCounter;
Packit Service 5a9772
	BYTE* surfaceData;
Packit Service 5a9772
	VideoSurface* surface;
Packit 1fb8d4
};
Packit 1fb8d4
Packit Service 5a9772
static const char* video_command_name(BYTE cmd)
Packit 1fb8d4
{
Packit Service 5a9772
	switch (cmd)
Packit 1fb8d4
	{
Packit Service 5a9772
		case TSMM_START_PRESENTATION:
Packit Service 5a9772
			return "start";
Packit Service 5a9772
		case TSMM_STOP_PRESENTATION:
Packit Service 5a9772
			return "stop";
Packit Service 5a9772
		default:
Packit Service 5a9772
			return "<unknown>";
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL yuv_to_rgb(PresentationContext* presentation, BYTE* dest)
Packit 1fb8d4
{
Packit 1fb8d4
	const BYTE* pYUVPoint[3];
Packit Service 5a9772
	H264_CONTEXT* h264 = presentation->h264;
Packit 1fb8d4
Packit 1fb8d4
	BYTE** ppYUVData;
Packit 1fb8d4
	ppYUVData = h264->pYUVData;
Packit 1fb8d4
Packit 1fb8d4
	pYUVPoint[0] = ppYUVData[0];
Packit 1fb8d4
	pYUVPoint[1] = ppYUVData[1];
Packit 1fb8d4
	pYUVPoint[2] = ppYUVData[2];
Packit 1fb8d4
Packit Service 5a9772
	if (!yuv_context_decode(presentation->yuv, pYUVPoint, h264->iStride, PIXEL_FORMAT_BGRX32, dest,
Packit Service 5a9772
	                        h264->width * 4))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "error in yuv_to_rgb conversion");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static void video_client_context_set_geometry(VideoClientContext* video,
Packit Service 5a9772
                                              GeometryClientContext* geometry)
Packit 1fb8d4
{
Packit 1fb8d4
	video->priv->geometry = geometry;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static VideoClientContextPriv* VideoClientContextPriv_new(VideoClientContext* video)
Packit 1fb8d4
{
Packit Service 5a9772
	VideoClientContextPriv* ret = calloc(1, sizeof(*ret));
Packit 1fb8d4
	if (!ret)
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit 1fb8d4
	ret->frames = Queue_New(TRUE, 10, 2);
Packit 1fb8d4
	if (!ret->frames)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "unable to allocate frames queue");
Packit 1fb8d4
		goto error_frames;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	ret->surfacePool = BufferPool_New(FALSE, 0, 16);
Packit 1fb8d4
	if (!ret->surfacePool)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "unable to create surface pool");
Packit 1fb8d4
		goto error_surfacePool;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!InitializeCriticalSectionAndSpinCount(&ret->framesLock, 4 * 1000))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "unable to initialize frames lock");
Packit 1fb8d4
		goto error_spinlock;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	ret->video = video;
Packit 1fb8d4
Packit Service 5a9772
	/* don't set to unlimited so that we have the chance to send a feedback in
Packit Service 5a9772
	 * the first second (for servers that want feedback directly)
Packit Service 5a9772
	 */
Packit 1fb8d4
	ret->lastSentRate = 30;
Packit 1fb8d4
	return ret;
Packit 1fb8d4
Packit 1fb8d4
error_spinlock:
Packit 1fb8d4
	BufferPool_Free(ret->surfacePool);
Packit 1fb8d4
error_surfacePool:
Packit 1fb8d4
	Queue_Free(ret->frames);
Packit 1fb8d4
error_frames:
Packit 1fb8d4
	free(ret);
Packit 1fb8d4
	return NULL;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static PresentationContext* PresentationContext_new(VideoClientContext* video, BYTE PresentationId,
Packit Service 5a9772
                                                    UINT32 x, UINT32 y, UINT32 width, UINT32 height)
Packit 1fb8d4
{
Packit Service 5a9772
	size_t s;
Packit Service 5a9772
	VideoClientContextPriv* priv = video->priv;
Packit Service 5a9772
	PresentationContext* ret;
Packit Service 5a9772
	s = width * height * 4ULL;
Packit Service 5a9772
	if (s > INT32_MAX)
Packit Service 5a9772
		return NULL;
Packit Service 5a9772
Packit Service 5a9772
	ret = calloc(1, sizeof(*ret));
Packit 1fb8d4
	if (!ret)
Packit 1fb8d4
		return NULL;
Packit 1fb8d4
Packit 1fb8d4
	ret->video = video;
Packit 1fb8d4
	ret->PresentationId = PresentationId;
Packit 1fb8d4
Packit 1fb8d4
	ret->h264 = h264_context_new(FALSE);
Packit 1fb8d4
	if (!ret->h264)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "unable to create a h264 context");
Packit 1fb8d4
		goto error_h264;
Packit 1fb8d4
	}
Packit 1fb8d4
	h264_context_reset(ret->h264, width, height);
Packit 1fb8d4
Packit 1fb8d4
	ret->currentSample = Stream_New(NULL, 4096);
Packit 1fb8d4
	if (!ret->currentSample)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "unable to create current packet stream");
Packit 1fb8d4
		goto error_currentSample;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	ret->surfaceData = BufferPool_Take(priv->surfacePool, s);
Packit 1fb8d4
	if (!ret->surfaceData)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "unable to allocate surfaceData");
Packit 1fb8d4
		goto error_surfaceData;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	ret->surface = video->createSurface(video, ret->surfaceData, x, y, width, height);
Packit 1fb8d4
	if (!ret->surface)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "unable to create surface");
Packit 1fb8d4
		goto error_surface;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	ret->yuv = yuv_context_new(FALSE);
Packit 1fb8d4
	if (!ret->yuv)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "unable to create YUV decoder");
Packit 1fb8d4
		goto error_yuv;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	yuv_context_reset(ret->yuv, width, height);
Packit 1fb8d4
	ret->refCounter = 1;
Packit 1fb8d4
	return ret;
Packit 1fb8d4
Packit 1fb8d4
error_yuv:
Packit 1fb8d4
	video->deleteSurface(video, ret->surface);
Packit 1fb8d4
error_surface:
Packit 1fb8d4
	BufferPool_Return(priv->surfacePool, ret->surfaceData);
Packit 1fb8d4
error_surfaceData:
Packit 1fb8d4
	Stream_Free(ret->currentSample, TRUE);
Packit 1fb8d4
error_currentSample:
Packit 1fb8d4
	h264_context_free(ret->h264);
Packit 1fb8d4
error_h264:
Packit 1fb8d4
	free(ret);
Packit 1fb8d4
	return NULL;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static void PresentationContext_unref(PresentationContext* presentation)
Packit 1fb8d4
{
Packit Service 5a9772
	VideoClientContextPriv* priv;
Packit Service 5a9772
	MAPPED_GEOMETRY* geometry;
Packit 1fb8d4
Packit 1fb8d4
	if (!presentation)
Packit 1fb8d4
		return;
Packit 1fb8d4
Packit 1fb8d4
	if (InterlockedDecrement(&presentation->refCounter) != 0)
Packit 1fb8d4
		return;
Packit 1fb8d4
Packit 1fb8d4
	geometry = presentation->geometry;
Packit 1fb8d4
	if (geometry)
Packit 1fb8d4
	{
Packit 1fb8d4
		geometry->MappedGeometryUpdate = NULL;
Packit 1fb8d4
		geometry->MappedGeometryClear = NULL;
Packit 1fb8d4
		geometry->custom = NULL;
Packit 1fb8d4
		mappedGeometryUnref(geometry);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	priv = presentation->video->priv;
Packit 1fb8d4
Packit 1fb8d4
	h264_context_free(presentation->h264);
Packit 1fb8d4
	Stream_Free(presentation->currentSample, TRUE);
Packit 1fb8d4
	presentation->video->deleteSurface(presentation->video, presentation->surface);
Packit 1fb8d4
	BufferPool_Return(priv->surfacePool, presentation->surfaceData);
Packit 1fb8d4
	yuv_context_free(presentation->yuv);
Packit 1fb8d4
	free(presentation);
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static void VideoFrame_free(VideoFrame** pframe)
Packit 1fb8d4
{
Packit Service 5a9772
	VideoFrame* frame = *pframe;
Packit 1fb8d4
Packit 1fb8d4
	mappedGeometryUnref(frame->geometry);
Packit 1fb8d4
	BufferPool_Return(frame->presentation->video->priv->surfacePool, frame->surfaceData);
Packit 1fb8d4
	PresentationContext_unref(frame->presentation);
Packit 1fb8d4
	free(frame);
Packit 1fb8d4
	*pframe = NULL;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static void VideoClientContextPriv_free(VideoClientContextPriv* priv)
Packit 1fb8d4
{
Packit 1fb8d4
	EnterCriticalSection(&priv->framesLock);
Packit 1fb8d4
	while (Queue_Count(priv->frames))
Packit 1fb8d4
	{
Packit Service 5a9772
		VideoFrame* frame = Queue_Dequeue(priv->frames);
Packit 1fb8d4
		if (frame)
Packit 1fb8d4
			VideoFrame_free(&frame);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Queue_Free(priv->frames);
Packit 1fb8d4
	LeaveCriticalSection(&priv->framesLock);
Packit 1fb8d4
Packit 1fb8d4
	DeleteCriticalSection(&priv->framesLock);
Packit 1fb8d4
Packit 1fb8d4
	if (priv->currentPresentation)
Packit 1fb8d4
		PresentationContext_unref(priv->currentPresentation);
Packit 1fb8d4
Packit 1fb8d4
	BufferPool_Free(priv->surfacePool);
Packit 1fb8d4
	free(priv);
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static UINT video_control_send_presentation_response(VideoClientContext* context,
Packit Service 5a9772
                                                     TSMM_PRESENTATION_RESPONSE* resp)
Packit 1fb8d4
{
Packit 1fb8d4
	BYTE buf[12];
Packit Service 5a9772
	wStream* s;
Packit Service 5a9772
	VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)context->handle;
Packit 1fb8d4
	IWTSVirtualChannel* channel;
Packit 1fb8d4
	UINT ret;
Packit 1fb8d4
Packit 1fb8d4
	s = Stream_New(buf, 12);
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
Packit Service 5a9772
	Stream_Write_UINT32(s, 12);                                     /* cbSize */
Packit 1fb8d4
	Stream_Write_UINT32(s, TSMM_PACKET_TYPE_PRESENTATION_RESPONSE); /* PacketType */
Packit 1fb8d4
	Stream_Write_UINT8(s, resp->PresentationId);
Packit 1fb8d4
	Stream_Zero(s, 3);
Packit 1fb8d4
	Stream_SealLength(s);
Packit 1fb8d4
Packit 1fb8d4
	channel = video->control_callback->channel_callback->channel;
Packit 1fb8d4
	ret = channel->Write(channel, 12, buf, NULL);
Packit 1fb8d4
	Stream_Free(s, FALSE);
Packit 1fb8d4
Packit 1fb8d4
	return ret;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL video_onMappedGeometryUpdate(MAPPED_GEOMETRY* geometry)
Packit 1fb8d4
{
Packit Service 5a9772
	PresentationContext* presentation = (PresentationContext*)geometry->custom;
Packit Service 5a9772
	RDP_RECT* r = &geometry->geometry.boundingRect;
Packit 1fb8d4
	WLog_DBG(TAG, "geometry updated topGeom=(%d,%d-%dx%d) geom=(%d,%d-%dx%d) rects=(%d,%d-%dx%d)",
Packit Service 5a9772
	         geometry->topLevelLeft, geometry->topLevelTop,
Packit Service 5a9772
	         geometry->topLevelRight - geometry->topLevelLeft,
Packit Service 5a9772
	         geometry->topLevelBottom - geometry->topLevelTop,
Packit 1fb8d4
Packit Service 5a9772
	         geometry->left, geometry->top, geometry->right - geometry->left,
Packit Service 5a9772
	         geometry->bottom - geometry->top,
Packit 1fb8d4
Packit Service 5a9772
	         r->x, r->y, r->width, r->height);
Packit 1fb8d4
Packit 1fb8d4
	presentation->surface->x = geometry->topLevelLeft + geometry->left;
Packit 1fb8d4
	presentation->surface->y = geometry->topLevelTop + geometry->top;
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static BOOL video_onMappedGeometryClear(MAPPED_GEOMETRY* geometry)
Packit 1fb8d4
{
Packit Service 5a9772
	PresentationContext* presentation = (PresentationContext*)geometry->custom;
Packit 1fb8d4
Packit 1fb8d4
	mappedGeometryUnref(presentation->geometry);
Packit 1fb8d4
	presentation->geometry = NULL;
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static UINT video_PresentationRequest(VideoClientContext* video, TSMM_PRESENTATION_REQUEST* req)
Packit 1fb8d4
{
Packit Service 5a9772
	VideoClientContextPriv* priv = video->priv;
Packit Service 5a9772
	PresentationContext* presentation;
Packit 1fb8d4
	UINT ret = CHANNEL_RC_OK;
Packit 1fb8d4
Packit 1fb8d4
	presentation = priv->currentPresentation;
Packit 1fb8d4
Packit 1fb8d4
	if (req->Command == TSMM_START_PRESENTATION)
Packit 1fb8d4
	{
Packit Service 5a9772
		MAPPED_GEOMETRY* geom;
Packit 1fb8d4
		TSMM_PRESENTATION_RESPONSE resp;
Packit 1fb8d4
Packit 1fb8d4
		if (memcmp(req->VideoSubtypeId, MFVideoFormat_H264, 16) != 0)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "not a H264 video, ignoring request");
Packit 1fb8d4
			return CHANNEL_RC_OK;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (presentation)
Packit 1fb8d4
		{
Packit 1fb8d4
			if (presentation->PresentationId == req->PresentationId)
Packit 1fb8d4
			{
Packit Service 5a9772
				WLog_ERR(TAG, "ignoring start request for existing presentation %d",
Packit Service 5a9772
				         req->PresentationId);
Packit 1fb8d4
				return CHANNEL_RC_OK;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			WLog_ERR(TAG, "releasing current presentation %d", req->PresentationId);
Packit 1fb8d4
			PresentationContext_unref(presentation);
Packit 1fb8d4
			presentation = priv->currentPresentation = NULL;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (!priv->geometry)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "geometry channel not ready, ignoring request");
Packit 1fb8d4
			return CHANNEL_RC_OK;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		geom = HashTable_GetItemValue(priv->geometry->geometries, &(req->GeometryMappingId));
Packit 1fb8d4
		if (!geom)
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "geometry mapping 0x%" PRIx64 " not registered", req->GeometryMappingId);
Packit 1fb8d4
			return CHANNEL_RC_OK;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		WLog_DBG(TAG, "creating presentation 0x%x", req->PresentationId);
Packit Service 5a9772
		presentation = PresentationContext_new(
Packit Service 5a9772
		    video, req->PresentationId, geom->topLevelLeft + geom->left,
Packit Service 5a9772
		    geom->topLevelTop + geom->top, req->SourceWidth, req->SourceHeight);
Packit 1fb8d4
		if (!presentation)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "unable to create presentation video");
Packit 1fb8d4
			return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		mappedGeometryRef(geom);
Packit 1fb8d4
		presentation->geometry = geom;
Packit 1fb8d4
Packit 1fb8d4
		priv->currentPresentation = presentation;
Packit 1fb8d4
		presentation->video = video;
Packit 1fb8d4
		presentation->SourceWidth = req->SourceWidth;
Packit 1fb8d4
		presentation->SourceHeight = req->SourceHeight;
Packit 1fb8d4
		presentation->ScaledWidth = req->ScaledWidth;
Packit 1fb8d4
		presentation->ScaledHeight = req->ScaledHeight;
Packit 1fb8d4
Packit 1fb8d4
		geom->custom = presentation;
Packit 1fb8d4
		geom->MappedGeometryUpdate = video_onMappedGeometryUpdate;
Packit 1fb8d4
		geom->MappedGeometryClear = video_onMappedGeometryClear;
Packit 1fb8d4
Packit 1fb8d4
		/* send back response */
Packit 1fb8d4
		resp.PresentationId = req->PresentationId;
Packit 1fb8d4
		ret = video_control_send_presentation_response(video, &resp);
Packit 1fb8d4
	}
Packit 1fb8d4
	else if (req->Command == TSMM_STOP_PRESENTATION)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_DBG(TAG, "stopping presentation 0x%x", req->PresentationId);
Packit 1fb8d4
		if (!presentation)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "unknown presentation to stop %d", req->PresentationId);
Packit 1fb8d4
			return CHANNEL_RC_OK;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		priv->currentPresentation = NULL;
Packit 1fb8d4
		priv->droppedFrames = 0;
Packit 1fb8d4
		priv->publishedFrames = 0;
Packit 1fb8d4
		PresentationContext_unref(presentation);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	return ret;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static UINT video_read_tsmm_presentation_req(VideoClientContext* context, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	TSMM_PRESENTATION_REQUEST req;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 60)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough bytes for a TSMM_PRESENTATION_REQUEST");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT8(s, req.PresentationId);
Packit 1fb8d4
	Stream_Read_UINT8(s, req.Version);
Packit 1fb8d4
	Stream_Read_UINT8(s, req.Command);
Packit 1fb8d4
	Stream_Read_UINT8(s, req.FrameRate); /* FrameRate - reserved and ignored */
Packit 1fb8d4
Packit 1fb8d4
	Stream_Seek_UINT16(s); /* AverageBitrateKbps reserved and ignored */
Packit 1fb8d4
	Stream_Seek_UINT16(s); /* reserved */
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, req.SourceWidth);
Packit 1fb8d4
	Stream_Read_UINT32(s, req.SourceHeight);
Packit 1fb8d4
	Stream_Read_UINT32(s, req.ScaledWidth);
Packit 1fb8d4
	Stream_Read_UINT32(s, req.ScaledHeight);
Packit 1fb8d4
	Stream_Read_UINT64(s, req.hnsTimestampOffset);
Packit 1fb8d4
	Stream_Read_UINT64(s, req.GeometryMappingId);
Packit 1fb8d4
	Stream_Read(s, req.VideoSubtypeId, 16);
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, req.cbExtra);
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < req.cbExtra)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough bytes for cbExtra of TSMM_PRESENTATION_REQUEST");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	req.pExtraData = Stream_Pointer(s);
Packit 1fb8d4
Packit Service 5a9772
	WLog_DBG(TAG,
Packit Service 5a9772
	         "presentationReq: id:%" PRIu8 " version:%" PRIu8
Packit Service 5a9772
	         " command:%s srcWidth/srcHeight=%" PRIu32 "x%" PRIu32 " scaled Width/Height=%" PRIu32
Packit Service 5a9772
	         "x%" PRIu32 " timestamp=%" PRIu64 " mappingId=%" PRIx64 "",
Packit Service 5a9772
	         req.PresentationId, req.Version, video_command_name(req.Command), req.SourceWidth,
Packit Service 5a9772
	         req.SourceHeight, req.ScaledWidth, req.ScaledHeight, req.hnsTimestampOffset,
Packit Service 5a9772
	         req.GeometryMappingId);
Packit 1fb8d4
Packit 1fb8d4
	return video_PresentationRequest(context, &req;;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit Service 5a9772
static UINT video_control_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* s)
Packit 1fb8d4
{
Packit Service 5a9772
	VIDEO_CHANNEL_CALLBACK* callback = (VIDEO_CHANNEL_CALLBACK*)pChannelCallback;
Packit 1fb8d4
	VIDEO_PLUGIN* video;
Packit Service 5a9772
	VideoClientContext* context;
Packit 1fb8d4
	UINT ret = CHANNEL_RC_OK;
Packit 1fb8d4
	UINT32 cbSize, packetType;
Packit 1fb8d4
Packit Service 5a9772
	video = (VIDEO_PLUGIN*)callback->plugin;
Packit Service 5a9772
	context = (VideoClientContext*)video->wtsPlugin.pInterface;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, cbSize);
Packit Service 5a9772
	if (cbSize < 8 || Stream_GetRemainingLength(s) < (cbSize - 4))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "invalid cbSize");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, packetType);
Packit 1fb8d4
	switch (packetType)
Packit 1fb8d4
	{
Packit Service 5a9772
		case TSMM_PACKET_TYPE_PRESENTATION_REQUEST:
Packit Service 5a9772
			ret = video_read_tsmm_presentation_req(context, s);
Packit Service 5a9772
			break;
Packit Service 5a9772
		default:
Packit Service 5a9772
			WLog_ERR(TAG, "not expecting packet type %" PRIu32 "", packetType);
Packit Service 5a9772
			ret = ERROR_UNSUPPORTED_TYPE;
Packit Service 5a9772
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return ret;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static UINT video_control_send_client_notification(VideoClientContext* context,
Packit Service 5a9772
                                                   TSMM_CLIENT_NOTIFICATION* notif)
Packit 1fb8d4
{
Packit 1fb8d4
	BYTE buf[100];
Packit Service 5a9772
	wStream* s;
Packit Service 5a9772
	VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)context->handle;
Packit 1fb8d4
	IWTSVirtualChannel* channel;
Packit 1fb8d4
	UINT ret;
Packit 1fb8d4
	UINT32 cbSize;
Packit 1fb8d4
Packit 1fb8d4
	s = Stream_New(buf, 30);
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
Packit 1fb8d4
	cbSize = 16;
Packit Service 5a9772
	Stream_Seek_UINT32(s);                                        /* cbSize */
Packit 1fb8d4
	Stream_Write_UINT32(s, TSMM_PACKET_TYPE_CLIENT_NOTIFICATION); /* PacketType */
Packit 1fb8d4
	Stream_Write_UINT8(s, notif->PresentationId);
Packit 1fb8d4
	Stream_Write_UINT8(s, notif->NotificationType);
Packit 1fb8d4
	Stream_Zero(s, 2);
Packit 1fb8d4
	if (notif->NotificationType == TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE)
Packit 1fb8d4
	{
Packit 1fb8d4
		Stream_Write_UINT32(s, 16); /* cbData */
Packit 1fb8d4
Packit 1fb8d4
		/* TSMM_CLIENT_NOTIFICATION_FRAMERATE_OVERRIDE */
Packit 1fb8d4
		Stream_Write_UINT32(s, notif->FramerateOverride.Flags);
Packit 1fb8d4
		Stream_Write_UINT32(s, notif->FramerateOverride.DesiredFrameRate);
Packit 1fb8d4
		Stream_Zero(s, 4 * 2);
Packit 1fb8d4
Packit 1fb8d4
		cbSize += 4 * 4;
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		Stream_Write_UINT32(s, 0); /* cbData */
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_SealLength(s);
Packit 1fb8d4
	Stream_SetPosition(s, 0);
Packit 1fb8d4
	Stream_Write_UINT32(s, cbSize);
Packit 1fb8d4
	Stream_Free(s, FALSE);
Packit 1fb8d4
Packit 1fb8d4
	channel = video->control_callback->channel_callback->channel;
Packit 1fb8d4
	ret = channel->Write(channel, cbSize, buf, NULL);
Packit 1fb8d4
Packit 1fb8d4
	return ret;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static void video_timer(VideoClientContext* video, UINT64 now)
Packit 1fb8d4
{
Packit Service 5a9772
	PresentationContext* presentation;
Packit Service 5a9772
	VideoClientContextPriv* priv = video->priv;
Packit 1fb8d4
	VideoFrame *peekFrame, *frame = NULL;
Packit 1fb8d4
Packit 1fb8d4
	EnterCriticalSection(&priv->framesLock);
Packit 1fb8d4
	do
Packit 1fb8d4
	{
Packit Service 5a9772
		peekFrame = (VideoFrame*)Queue_Peek(priv->frames);
Packit 1fb8d4
		if (!peekFrame)
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		if (peekFrame->publishTime > now)
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		if (frame)
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_DBG(TAG, "dropping frame @%" PRIu64, frame->publishTime);
Packit 1fb8d4
			priv->droppedFrames++;
Packit 1fb8d4
			VideoFrame_free(&frame);
Packit 1fb8d4
		}
Packit 1fb8d4
		frame = peekFrame;
Packit 1fb8d4
		Queue_Dequeue(priv->frames);
Packit Service 5a9772
	} while (1);
Packit 1fb8d4
	LeaveCriticalSection(&priv->framesLock);
Packit 1fb8d4
Packit 1fb8d4
	if (!frame)
Packit 1fb8d4
		goto treat_feedback;
Packit 1fb8d4
Packit 1fb8d4
	presentation = frame->presentation;
Packit 1fb8d4
Packit 1fb8d4
	priv->publishedFrames++;
Packit 1fb8d4
	memcpy(presentation->surfaceData, frame->surfaceData, frame->w * frame->h * 4);
Packit 1fb8d4
Packit 1fb8d4
	video->showSurface(video, presentation->surface);
Packit 1fb8d4
Packit 1fb8d4
	VideoFrame_free(&frame);
Packit 1fb8d4
Packit 1fb8d4
treat_feedback:
Packit 1fb8d4
	if (priv->nextFeedbackTime < now)
Packit 1fb8d4
	{
Packit 1fb8d4
		/* we can compute some feedback only if we have some published frames and
Packit 1fb8d4
		 * a current presentation
Packit 1fb8d4
		 */
Packit 1fb8d4
		if (priv->publishedFrames && priv->currentPresentation)
Packit 1fb8d4
		{
Packit 1fb8d4
			UINT32 computedRate;
Packit 1fb8d4
Packit 1fb8d4
			InterlockedIncrement(&priv->currentPresentation->refCounter);
Packit 1fb8d4
Packit 1fb8d4
			if (priv->droppedFrames)
Packit 1fb8d4
			{
Packit 1fb8d4
				/**
Packit 1fb8d4
				 * some dropped frames, looks like we're asking too many frames per seconds,
Packit 1fb8d4
				 * try lowering rate. We go directly from unlimited rate to 24 frames/seconds
Packit 1fb8d4
				 * otherwise we lower rate by 2 frames by seconds
Packit 1fb8d4
				 */
Packit 1fb8d4
				if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
Packit 1fb8d4
					computedRate = 24;
Packit 1fb8d4
				else
Packit 1fb8d4
				{
Packit 1fb8d4
					computedRate = priv->lastSentRate - 2;
Packit 1fb8d4
					if (!computedRate)
Packit 1fb8d4
						computedRate = 2;
Packit 1fb8d4
				}
Packit 1fb8d4
			}
Packit 1fb8d4
			else
Packit 1fb8d4
			{
Packit 1fb8d4
				/**
Packit 1fb8d4
				 * we treat all frames ok, so either ask the server to send more,
Packit 1fb8d4
				 * or stay unlimited
Packit Service 5a9772
				 */
Packit 1fb8d4
				if (priv->lastSentRate == XF_VIDEO_UNLIMITED_RATE)
Packit 1fb8d4
					computedRate = XF_VIDEO_UNLIMITED_RATE; /* stay unlimited */
Packit 1fb8d4
				else
Packit 1fb8d4
				{
Packit 1fb8d4
					computedRate = priv->lastSentRate + 2;
Packit 1fb8d4
					if (computedRate > XF_VIDEO_UNLIMITED_RATE)
Packit 1fb8d4
						computedRate = XF_VIDEO_UNLIMITED_RATE;
Packit 1fb8d4
				}
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			if (computedRate != priv->lastSentRate)
Packit 1fb8d4
			{
Packit 1fb8d4
				TSMM_CLIENT_NOTIFICATION notif;
Packit 1fb8d4
				notif.PresentationId = priv->currentPresentation->PresentationId;
Packit 1fb8d4
				notif.NotificationType = TSMM_CLIENT_NOTIFICATION_TYPE_FRAMERATE_OVERRIDE;
Packit 1fb8d4
				if (computedRate == XF_VIDEO_UNLIMITED_RATE)
Packit 1fb8d4
				{
Packit 1fb8d4
					notif.FramerateOverride.Flags = 0x01;
Packit 1fb8d4
					notif.FramerateOverride.DesiredFrameRate = 0x00;
Packit 1fb8d4
				}
Packit 1fb8d4
				else
Packit 1fb8d4
				{
Packit 1fb8d4
					notif.FramerateOverride.Flags = 0x02;
Packit 1fb8d4
					notif.FramerateOverride.DesiredFrameRate = computedRate;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				video_control_send_client_notification(video, ¬if;;
Packit 1fb8d4
				priv->lastSentRate = computedRate;
Packit 1fb8d4
Packit Service 5a9772
				WLog_DBG(TAG, "server notified with rate %d published=%d dropped=%d",
Packit Service 5a9772
				         priv->lastSentRate, priv->publishedFrames, priv->droppedFrames);
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			PresentationContext_unref(priv->currentPresentation);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		WLog_DBG(TAG, "currentRate=%d published=%d dropped=%d", priv->lastSentRate,
Packit Service 5a9772
		         priv->publishedFrames, priv->droppedFrames);
Packit 1fb8d4
Packit 1fb8d4
		priv->droppedFrames = 0;
Packit 1fb8d4
		priv->publishedFrames = 0;
Packit 1fb8d4
		priv->nextFeedbackTime = now + 1000;
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static UINT video_VideoData(VideoClientContext* context, TSMM_VIDEO_DATA* data)
Packit 1fb8d4
{
Packit Service 5a9772
	VideoClientContextPriv* priv = context->priv;
Packit Service 5a9772
	PresentationContext* presentation;
Packit 1fb8d4
	int status;
Packit 1fb8d4
Packit 1fb8d4
	presentation = priv->currentPresentation;
Packit 1fb8d4
	if (!presentation)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "no current presentation");
Packit 1fb8d4
		return CHANNEL_RC_OK;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (presentation->PresentationId != data->PresentationId)
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_ERR(TAG, "current presentation id=%d doesn't match data id=%d",
Packit Service 5a9772
		         presentation->PresentationId, data->PresentationId);
Packit 1fb8d4
		return CHANNEL_RC_OK;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!Stream_EnsureRemainingCapacity(presentation->currentSample, data->cbSample))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "unable to expand the current packet");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Write(presentation->currentSample, data->pSample, data->cbSample);
Packit 1fb8d4
Packit 1fb8d4
	if (data->CurrentPacketIndex == data->PacketsInSample)
Packit 1fb8d4
	{
Packit Service 5a9772
		H264_CONTEXT* h264 = presentation->h264;
Packit 1fb8d4
		UINT64 startTime = GetTickCount64(), timeAfterH264;
Packit Service 5a9772
		MAPPED_GEOMETRY* geom = presentation->geometry;
Packit 1fb8d4
Packit 1fb8d4
		Stream_SealLength(presentation->currentSample);
Packit 1fb8d4
		Stream_SetPosition(presentation->currentSample, 0);
Packit 1fb8d4
Packit 1fb8d4
		status = h264->subsystem->Decompress(h264, Stream_Pointer(presentation->currentSample),
Packit Service 5a9772
		                                     Stream_Length(presentation->currentSample));
Packit 1fb8d4
		if (status == 0)
Packit 1fb8d4
			return CHANNEL_RC_OK;
Packit 1fb8d4
Packit 1fb8d4
		if (status < 0)
Packit 1fb8d4
			return CHANNEL_RC_OK;
Packit 1fb8d4
Packit 1fb8d4
		timeAfterH264 = GetTickCount64();
Packit 1fb8d4
		if (data->SampleNumber == 1)
Packit 1fb8d4
		{
Packit 1fb8d4
			presentation->lastPublishTime = startTime;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		presentation->lastPublishTime += (data->hnsDuration / 10000);
Packit 1fb8d4
		if (presentation->lastPublishTime <= timeAfterH264 + 10)
Packit 1fb8d4
		{
Packit 1fb8d4
			int dropped = 0;
Packit 1fb8d4
Packit 1fb8d4
			/* if the frame is to be published in less than 10 ms, let's consider it's now */
Packit 1fb8d4
			yuv_to_rgb(presentation, presentation->surfaceData);
Packit 1fb8d4
Packit 1fb8d4
			context->showSurface(context, presentation->surface);
Packit 1fb8d4
Packit 1fb8d4
			priv->publishedFrames++;
Packit 1fb8d4
Packit 1fb8d4
			/* cleanup previously scheduled frames */
Packit 1fb8d4
			EnterCriticalSection(&priv->framesLock);
Packit 1fb8d4
			while (Queue_Count(priv->frames) > 0)
Packit 1fb8d4
			{
Packit Service 5a9772
				VideoFrame* frame = Queue_Dequeue(priv->frames);
Packit 1fb8d4
				if (frame)
Packit 1fb8d4
				{
Packit 1fb8d4
					priv->droppedFrames++;
Packit 1fb8d4
					VideoFrame_free(&frame);
Packit 1fb8d4
					dropped++;
Packit 1fb8d4
				}
Packit 1fb8d4
			}
Packit 1fb8d4
			LeaveCriticalSection(&priv->framesLock);
Packit 1fb8d4
Packit 1fb8d4
			if (dropped)
Packit 1fb8d4
				WLog_DBG(TAG, "showing frame (%d dropped)", dropped);
Packit 1fb8d4
		}
Packit 1fb8d4
		else
Packit 1fb8d4
		{
Packit 1fb8d4
			BOOL enqueueResult;
Packit Service 5a9772
			VideoFrame* frame = calloc(1, sizeof(*frame));
Packit 1fb8d4
			if (!frame)
Packit 1fb8d4
			{
Packit 1fb8d4
				WLog_ERR(TAG, "unable to create frame");
Packit 1fb8d4
				return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
			}
Packit 1fb8d4
			mappedGeometryRef(geom);
Packit 1fb8d4
Packit 1fb8d4
			frame->presentation = presentation;
Packit 1fb8d4
			frame->publishTime = presentation->lastPublishTime;
Packit 1fb8d4
			frame->geometry = geom;
Packit 1fb8d4
			frame->w = presentation->SourceWidth;
Packit 1fb8d4
			frame->h = presentation->SourceHeight;
Packit 1fb8d4
Packit 1fb8d4
			frame->surfaceData = BufferPool_Take(priv->surfacePool, frame->w * frame->h * 4);
Packit 1fb8d4
			if (!frame->surfaceData)
Packit 1fb8d4
			{
Packit 1fb8d4
				WLog_ERR(TAG, "unable to allocate frame data");
Packit 1fb8d4
				mappedGeometryUnref(geom);
Packit 1fb8d4
				free(frame);
Packit 1fb8d4
				return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			if (!yuv_to_rgb(presentation, frame->surfaceData))
Packit 1fb8d4
			{
Packit 1fb8d4
				WLog_ERR(TAG, "error during YUV->RGB conversion");
Packit 1fb8d4
				BufferPool_Return(priv->surfacePool, frame->surfaceData);
Packit 1fb8d4
				mappedGeometryUnref(geom);
Packit 1fb8d4
				free(frame);
Packit 1fb8d4
				return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			InterlockedIncrement(&presentation->refCounter);
Packit 1fb8d4
Packit 1fb8d4
			EnterCriticalSection(&priv->framesLock);
Packit 1fb8d4
			enqueueResult = Queue_Enqueue(priv->frames, frame);
Packit 1fb8d4
			LeaveCriticalSection(&priv->framesLock);
Packit 1fb8d4
Packit 1fb8d4
			if (!enqueueResult)
Packit 1fb8d4
			{
Packit 1fb8d4
				WLog_ERR(TAG, "unable to enqueue frame");
Packit 1fb8d4
				VideoFrame_free(&frame);
Packit 1fb8d4
				return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit Service 5a9772
			WLog_DBG(TAG, "scheduling frame in %" PRIu32 " ms", (frame->publishTime - startTime));
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static UINT video_data_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* s)
Packit 1fb8d4
{
Packit Service 5a9772
	VIDEO_CHANNEL_CALLBACK* callback = (VIDEO_CHANNEL_CALLBACK*)pChannelCallback;
Packit 1fb8d4
	VIDEO_PLUGIN* video;
Packit Service 5a9772
	VideoClientContext* context;
Packit 1fb8d4
	UINT32 cbSize, packetType;
Packit 1fb8d4
	TSMM_VIDEO_DATA data;
Packit 1fb8d4
Packit Service 5a9772
	video = (VIDEO_PLUGIN*)callback->plugin;
Packit Service 5a9772
	context = (VideoClientContext*)video->wtsPlugin.pInterface;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, cbSize);
Packit Service 5a9772
	if (cbSize < 8 || Stream_GetRemainingLength(s) < (cbSize - 4))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "invalid cbSize");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, packetType);
Packit 1fb8d4
	if (packetType != TSMM_PACKET_TYPE_VIDEO_DATA)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "only expecting VIDEO_DATA on the data channel");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 32)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "not enough bytes for a TSMM_VIDEO_DATA");
Packit 1fb8d4
		return ERROR_INVALID_DATA;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT8(s, data.PresentationId);
Packit 1fb8d4
	Stream_Read_UINT8(s, data.Version);
Packit 1fb8d4
	Stream_Read_UINT8(s, data.Flags);
Packit 1fb8d4
	Stream_Seek_UINT8(s); /* reserved */
Packit 1fb8d4
	Stream_Read_UINT64(s, data.hnsTimestamp);
Packit 1fb8d4
	Stream_Read_UINT64(s, data.hnsDuration);
Packit 1fb8d4
	Stream_Read_UINT16(s, data.CurrentPacketIndex);
Packit 1fb8d4
	Stream_Read_UINT16(s, data.PacketsInSample);
Packit 1fb8d4
	Stream_Read_UINT32(s, data.SampleNumber);
Packit 1fb8d4
	Stream_Read_UINT32(s, data.cbSample);
Packit 1fb8d4
	data.pSample = Stream_Pointer(s);
Packit 1fb8d4
Packit Service 5a9772
	/*
Packit Service 5a9772
	    WLog_DBG(TAG, "videoData: id:%"PRIu8" version:%"PRIu8" flags:0x%"PRIx8" timestamp=%"PRIu64"
Packit Service 5a9772
	   duration=%"PRIu64 " curPacketIndex:%"PRIu16" packetInSample:%"PRIu16" sampleNumber:%"PRIu32"
Packit Service 5a9772
	   cbSample:%"PRIu32"", data.PresentationId, data.Version, data.Flags, data.hnsTimestamp,
Packit Service 5a9772
	   data.hnsDuration, data.CurrentPacketIndex, data.PacketsInSample, data.SampleNumber,
Packit Service 5a9772
	   data.cbSample);
Packit Service 5a9772
	*/
Packit 1fb8d4
Packit 1fb8d4
	return video_VideoData(context, &data);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT video_control_on_close(IWTSVirtualChannelCallback* pChannelCallback)
Packit 1fb8d4
{
Packit 1fb8d4
	free(pChannelCallback);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static UINT video_data_on_close(IWTSVirtualChannelCallback* pChannelCallback)
Packit 1fb8d4
{
Packit 1fb8d4
	free(pChannelCallback);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT video_control_on_new_channel_connection(IWTSListenerCallback* listenerCallback,
Packit Service 5a9772
                                                    IWTSVirtualChannel* channel, BYTE* Data,
Packit Service 5a9772
                                                    BOOL* pbAccept,
Packit Service 5a9772
                                                    IWTSVirtualChannelCallback** ppCallback)
Packit 1fb8d4
{
Packit 1fb8d4
	VIDEO_CHANNEL_CALLBACK* callback;
Packit Service 5a9772
	VIDEO_LISTENER_CALLBACK* listener_callback = (VIDEO_LISTENER_CALLBACK*)listenerCallback;
Packit 1fb8d4
Packit Service 5a9772
	WINPR_UNUSED(Data);
Packit Service 5a9772
	WINPR_UNUSED(pbAccept);
Packit Service 5a9772
Packit Service 5a9772
	callback = (VIDEO_CHANNEL_CALLBACK*)calloc(1, sizeof(VIDEO_CHANNEL_CALLBACK));
Packit 1fb8d4
	if (!callback)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "calloc failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	callback->iface.OnDataReceived = video_control_on_data_received;
Packit 1fb8d4
	callback->iface.OnClose = video_control_on_close;
Packit 1fb8d4
	callback->plugin = listener_callback->plugin;
Packit 1fb8d4
	callback->channel_mgr = listener_callback->channel_mgr;
Packit 1fb8d4
	callback->channel = channel;
Packit 1fb8d4
	listener_callback->channel_callback = callback;
Packit 1fb8d4
Packit Service 5a9772
	*ppCallback = (IWTSVirtualChannelCallback*)callback;
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static UINT video_data_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
Packit Service 5a9772
                                                 IWTSVirtualChannel* pChannel, BYTE* Data,
Packit Service 5a9772
                                                 BOOL* pbAccept,
Packit Service 5a9772
                                                 IWTSVirtualChannelCallback** ppCallback)
Packit 1fb8d4
{
Packit 1fb8d4
	VIDEO_CHANNEL_CALLBACK* callback;
Packit Service 5a9772
	VIDEO_LISTENER_CALLBACK* listener_callback = (VIDEO_LISTENER_CALLBACK*)pListenerCallback;
Packit Service 5a9772
Packit Service 5a9772
	WINPR_UNUSED(Data);
Packit Service 5a9772
	WINPR_UNUSED(pbAccept);
Packit 1fb8d4
Packit Service 5a9772
	callback = (VIDEO_CHANNEL_CALLBACK*)calloc(1, sizeof(VIDEO_CHANNEL_CALLBACK));
Packit 1fb8d4
	if (!callback)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "calloc failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	callback->iface.OnDataReceived = video_data_on_data_received;
Packit 1fb8d4
	callback->iface.OnClose = video_data_on_close;
Packit 1fb8d4
	callback->plugin = listener_callback->plugin;
Packit 1fb8d4
	callback->channel_mgr = listener_callback->channel_mgr;
Packit 1fb8d4
	callback->channel = pChannel;
Packit 1fb8d4
	listener_callback->channel_callback = callback;
Packit 1fb8d4
Packit Service 5a9772
	*ppCallback = (IWTSVirtualChannelCallback*)callback;
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT video_plugin_initialize(IWTSPlugin* plugin, IWTSVirtualChannelManager* channelMgr)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT status;
Packit Service 5a9772
	VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)plugin;
Packit Service 5a9772
	VIDEO_LISTENER_CALLBACK* callback;
Packit 1fb8d4
Packit Service 5a9772
	video->control_callback = callback =
Packit Service 5a9772
	    (VIDEO_LISTENER_CALLBACK*)calloc(1, sizeof(VIDEO_LISTENER_CALLBACK));
Packit 1fb8d4
	if (!callback)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "calloc for control callback failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	callback->iface.OnNewChannelConnection = video_control_on_new_channel_connection;
Packit 1fb8d4
	callback->plugin = plugin;
Packit 1fb8d4
	callback->channel_mgr = channelMgr;
Packit 1fb8d4
Packit 1fb8d4
	status = channelMgr->CreateListener(channelMgr, VIDEO_CONTROL_DVC_CHANNEL_NAME, 0,
Packit Service 5a9772
	                                    &callback->iface, &(video->controlListener));
Packit 1fb8d4
Packit 1fb8d4
	if (status != CHANNEL_RC_OK)
Packit 1fb8d4
		return status;
Packit 1fb8d4
	video->controlListener->pInterface = video->wtsPlugin.pInterface;
Packit 1fb8d4
Packit Service 5a9772
	video->data_callback = callback =
Packit Service 5a9772
	    (VIDEO_LISTENER_CALLBACK*)calloc(1, sizeof(VIDEO_LISTENER_CALLBACK));
Packit 1fb8d4
	if (!callback)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "calloc for data callback failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	callback->iface.OnNewChannelConnection = video_data_on_new_channel_connection;
Packit 1fb8d4
	callback->plugin = plugin;
Packit 1fb8d4
	callback->channel_mgr = channelMgr;
Packit 1fb8d4
Packit 1fb8d4
	status = channelMgr->CreateListener(channelMgr, VIDEO_DATA_DVC_CHANNEL_NAME, 0,
Packit Service 5a9772
	                                    &callback->iface, &(video->dataListener));
Packit 1fb8d4
Packit 1fb8d4
	if (status == CHANNEL_RC_OK)
Packit 1fb8d4
		video->dataListener->pInterface = video->wtsPlugin.pInterface;
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT video_plugin_terminated(IWTSPlugin* pPlugin)
Packit 1fb8d4
{
Packit Service 5a9772
	VIDEO_PLUGIN* video = (VIDEO_PLUGIN*)pPlugin;
Packit Service 5a9772
Packit Service 5a9772
	if (video->control_callback)
Packit Service 5a9772
	{
Packit Service 5a9772
		IWTSVirtualChannelManager* mgr = video->control_callback->channel_mgr;
Packit Service 5a9772
		if (mgr)
Packit Service 5a9772
			IFCALL(mgr->DestroyListener, mgr, video->controlListener);
Packit Service 5a9772
	}
Packit Service 5a9772
	if (video->data_callback)
Packit Service 5a9772
	{
Packit Service 5a9772
		IWTSVirtualChannelManager* mgr = video->data_callback->channel_mgr;
Packit Service 5a9772
		if (mgr)
Packit Service 5a9772
			IFCALL(mgr->DestroyListener, mgr, video->dataListener);
Packit Service 5a9772
	}
Packit 1fb8d4
Packit 1fb8d4
	if (video->context)
Packit 1fb8d4
		VideoClientContextPriv_free(video->context->priv);
Packit 1fb8d4
Packit 1fb8d4
	free(video->control_callback);
Packit 1fb8d4
	free(video->data_callback);
Packit 1fb8d4
	free(video->wtsPlugin.pInterface);
Packit 1fb8d4
	free(pPlugin);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Channel Client Interface
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef BUILTIN_CHANNELS
Packit Service 5a9772
#define DVCPluginEntry video_DVCPluginEntry
Packit 1fb8d4
#else
Packit Service 5a9772
#define DVCPluginEntry FREERDP_API DVCPluginEntry
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
UINT DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
	VIDEO_PLUGIN* videoPlugin;
Packit 1fb8d4
	VideoClientContext* videoContext;
Packit Service 5a9772
	VideoClientContextPriv* priv;
Packit 1fb8d4
Packit Service 5a9772
	videoPlugin = (VIDEO_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints, "video");
Packit 1fb8d4
	if (!videoPlugin)
Packit 1fb8d4
	{
Packit Service 5a9772
		videoPlugin = (VIDEO_PLUGIN*)calloc(1, sizeof(VIDEO_PLUGIN));
Packit 1fb8d4
		if (!videoPlugin)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "calloc failed!");
Packit 1fb8d4
			return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		videoPlugin->wtsPlugin.Initialize = video_plugin_initialize;
Packit 1fb8d4
		videoPlugin->wtsPlugin.Connected = NULL;
Packit 1fb8d4
		videoPlugin->wtsPlugin.Disconnected = NULL;
Packit 1fb8d4
		videoPlugin->wtsPlugin.Terminated = video_plugin_terminated;
Packit 1fb8d4
Packit Service 5a9772
		videoContext = (VideoClientContext*)calloc(1, sizeof(VideoClientContext));
Packit 1fb8d4
		if (!videoContext)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "calloc failed!");
Packit 1fb8d4
			free(videoPlugin);
Packit 1fb8d4
			return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		priv = VideoClientContextPriv_new(videoContext);
Packit 1fb8d4
		if (!priv)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "VideoClientContextPriv_new failed!");
Packit 1fb8d4
			free(videoContext);
Packit 1fb8d4
			free(videoPlugin);
Packit 1fb8d4
			return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		videoContext->handle = (void*)videoPlugin;
Packit 1fb8d4
		videoContext->priv = priv;
Packit 1fb8d4
		videoContext->timer = video_timer;
Packit 1fb8d4
		videoContext->setGeometry = video_client_context_set_geometry;
Packit 1fb8d4
Packit Service 5a9772
		videoPlugin->wtsPlugin.pInterface = (void*)videoContext;
Packit 1fb8d4
		videoPlugin->context = videoContext;
Packit 1fb8d4
Packit Service 5a9772
		error = pEntryPoints->RegisterPlugin(pEntryPoints, "video", (IWTSPlugin*)videoPlugin);
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "could not get video Plugin.");
Packit 1fb8d4
		return CHANNEL_RC_BAD_CHANNEL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return error;
Packit 1fb8d4
}