/**
 * FreeRDP: A Remote Desktop Protocol Implementation
 * Video Capture Virtual Channel Extension
 *
 * Copyright 2022 Pascal Nowack <Pascal.Nowack@gmx.de>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	 http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef FREERDP_CHANNEL_CAMERA_DEVICE_SERVER_CAMERA_DEVICE_H
#define FREERDP_CHANNEL_CAMERA_DEVICE_SERVER_CAMERA_DEVICE_H

#include <freerdp/channels/rdpecam.h>
#include <freerdp/channels/wtsvc.h>

#ifdef __cplusplus
extern "C"
{
#endif

	typedef struct camera_device_server_context CameraDeviceServerContext;

	typedef UINT (*psCameraDeviceServerOpen)(CameraDeviceServerContext* context);
	typedef UINT (*psCameraDeviceServerClose)(CameraDeviceServerContext* context);

	typedef BOOL (*psCameraDeviceServerChannelIdAssigned)(CameraDeviceServerContext* context,
	                                                      UINT32 channelId);

	typedef UINT (*psCameraDeviceServerInitialize)(CameraDeviceServerContext* context,
	                                               BOOL externalThread);
	typedef UINT (*psCameraDeviceServerPoll)(CameraDeviceServerContext* context);
	typedef BOOL (*psCameraDeviceServerChannelHandle)(CameraDeviceServerContext* context,
	                                                  HANDLE* handle);

	typedef UINT (*psCameraDeviceServerSuccessResponse)(
	    CameraDeviceServerContext* context, const CAM_SUCCESS_RESPONSE* successResponse);
	typedef UINT (*psCameraDeviceServerErrorResponse)(CameraDeviceServerContext* context,
	                                                  const CAM_ERROR_RESPONSE* errorResponse);

	typedef UINT (*psCameraDeviceServerActivateDeviceRequest)(
	    CameraDeviceServerContext* context,
	    const CAM_ACTIVATE_DEVICE_REQUEST* activateDeviceRequest);
	typedef UINT (*psCameraDeviceServerDeactivateDeviceRequest)(
	    CameraDeviceServerContext* context,
	    const CAM_DEACTIVATE_DEVICE_REQUEST* deactivateDeviceRequest);

	typedef UINT (*psCameraDeviceServerStreamListRequest)(
	    CameraDeviceServerContext* context, const CAM_STREAM_LIST_REQUEST* streamListRequest);
	typedef UINT (*psCameraDeviceServerStreamListResponse)(
	    CameraDeviceServerContext* context, const CAM_STREAM_LIST_RESPONSE* streamListResponse);

	typedef UINT (*psCameraDeviceServerMediaTypeListRequest)(
	    CameraDeviceServerContext* context,
	    const CAM_MEDIA_TYPE_LIST_REQUEST* mediaTypeListRequest);
	typedef UINT (*psCameraDeviceServerMediaTypeListResponse)(
	    CameraDeviceServerContext* context,
	    const CAM_MEDIA_TYPE_LIST_RESPONSE* mediaTypeListResponse);

	typedef UINT (*psCameraDeviceServerCurrentMediaTypeRequest)(
	    CameraDeviceServerContext* context,
	    const CAM_CURRENT_MEDIA_TYPE_REQUEST* currentMediaTypeRequest);
	typedef UINT (*psCameraDeviceServerCurrentMediaTypeResponse)(
	    CameraDeviceServerContext* context,
	    const CAM_CURRENT_MEDIA_TYPE_RESPONSE* currentMediaTypeResponse);

	typedef UINT (*psCameraDeviceServerStartStreamsRequest)(
	    CameraDeviceServerContext* context, const CAM_START_STREAMS_REQUEST* startStreamsRequest);
	typedef UINT (*psCameraDeviceServerStopStreamsRequest)(
	    CameraDeviceServerContext* context, const CAM_STOP_STREAMS_REQUEST* stopStreamsRequest);

	typedef UINT (*psCameraDeviceServerSampleRequest)(CameraDeviceServerContext* context,
	                                                  const CAM_SAMPLE_REQUEST* sampleRequest);
	typedef UINT (*psCameraDeviceServerSampleResponse)(CameraDeviceServerContext* context,
	                                                   const CAM_SAMPLE_RESPONSE* sampleResponse);
	typedef UINT (*psCameraDeviceServerSampleErrorResponse)(
	    CameraDeviceServerContext* context, const CAM_SAMPLE_ERROR_RESPONSE* sampleErrorResponse);

	typedef UINT (*psCameraDeviceServerPropertyListRequest)(
	    CameraDeviceServerContext* context, const CAM_PROPERTY_LIST_REQUEST* propertyListRequest);
	typedef UINT (*psCameraDeviceServerPropertyListResponse)(
	    CameraDeviceServerContext* context, const CAM_PROPERTY_LIST_RESPONSE* propertyListResponse);

	typedef UINT (*psCameraDeviceServerPropertyValueRequest)(
	    CameraDeviceServerContext* context, const CAM_PROPERTY_VALUE_REQUEST* propertyValueRequest);
	typedef UINT (*psCameraDeviceServerPropertyValueResponse)(
	    CameraDeviceServerContext* context,
	    const CAM_PROPERTY_VALUE_RESPONSE* propertyValueResponse);

	typedef UINT (*psCameraDeviceServerSetPropertyValueRequest)(
	    CameraDeviceServerContext* context,
	    const CAM_SET_PROPERTY_VALUE_REQUEST* setPropertyValueRequest);

	struct camera_device_server_context
	{
		HANDLE vcm;

		/* Server self-defined pointer. */
		void* userdata;

		/**
		 * Name of the virtual channel. Pointer owned by the CameraDeviceServerContext,
		 * meaning camera_device_server_context_free() takes care of freeing the pointer.
		 *
		 * Server implementations should sanitize the virtual channel name for invalid
		 * names, like names for other known channels
		 * ("ECHO", "AUDIO_PLAYBACK_DVC", etc.)
		 */
		char* virtualChannelName;

		/**
		 * Protocol version to be used. Every sent server to client PDU has the
		 * version value in the Header set to the following value.
		 */
		BYTE protocolVersion;

		/*** APIs called by the server. ***/

		/**
		 * Optional: Set thread handling.
		 * When externalThread=TRUE, the application is responsible to call
		 * Poll() periodically to process channel events.
		 *
		 * Defaults to externalThread=FALSE
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerInitialize Initialize;

		/**
		 * Open the camera device channel.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerOpen Open;

		/**
		 * Close the camera device channel.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerClose Close;

		/**
		 * Poll
		 * When externalThread=TRUE, call Poll() periodically from your main loop.
		 * If externalThread=FALSE do not call.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerPoll Poll;

		/**
		 * Retrieve the channel handle for use in conjunction with Poll().
		 * If externalThread=FALSE do not call.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerChannelHandle ChannelHandle;

		/**
		 * For the following server to client PDUs,
		 * the message header does not have to be set.
		 */

		/**
		 * Send a Activate Device Request PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerActivateDeviceRequest ActivateDeviceRequest;

		/**
		 * Send a Deactivate Device Request PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerDeactivateDeviceRequest DeactivateDeviceRequest;

		/**
		 * Send a Stream List Request PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerStreamListRequest StreamListRequest;

		/**
		 * Send a Media Type List Request PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerMediaTypeListRequest MediaTypeListRequest;

		/**
		 * Send a Current Media Type Request PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerCurrentMediaTypeRequest CurrentMediaTypeRequest;

		/**
		 * Send a Start Streams Request PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerStartStreamsRequest StartStreamsRequest;

		/**
		 * Send a Stop Streams Request PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerStopStreamsRequest StopStreamsRequest;

		/**
		 * Send a Sample Request PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerSampleRequest SampleRequest;

		/**
		 * Send a Property List Request PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerPropertyListRequest PropertyListRequest;

		/**
		 * Send a Property Value Request PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerPropertyValueRequest PropertyValueRequest;

		/**
		 * Send a Set Property Value Request PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerSetPropertyValueRequest SetPropertyValueRequest;

		/*** Callbacks registered by the server. ***/

		/**
		 * Callback, when the channel got its id assigned.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerChannelIdAssigned ChannelIdAssigned;

		/**
		 * Callback for the Success Response PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerSuccessResponse SuccessResponse;

		/**
		 * Callback for the Error Response PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerErrorResponse ErrorResponse;

		/**
		 * Callback for the Stream List Response PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerStreamListResponse StreamListResponse;

		/**
		 * Callback for the Media Type List Response PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerMediaTypeListResponse MediaTypeListResponse;

		/**
		 * Callback for the Current Media Type Response PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerCurrentMediaTypeResponse CurrentMediaTypeResponse;

		/**
		 * Callback for the Sample Response PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerSampleResponse SampleResponse;

		/**
		 * Callback for the Sample Error Response PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerSampleErrorResponse SampleErrorResponse;

		/**
		 * Callback for the Property List Response PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerPropertyListResponse PropertyListResponse;

		/**
		 * Callback for the Property Value Response PDU.
		 */
		WINPR_ATTR_NODISCARD psCameraDeviceServerPropertyValueResponse PropertyValueResponse;

		rdpContext* rdpcontext;
	};

	FREERDP_API void camera_device_server_context_free(CameraDeviceServerContext* context);

	WINPR_ATTR_MALLOC(camera_device_server_context_free, 1)
	WINPR_ATTR_NODISCARD
	FREERDP_API CameraDeviceServerContext* camera_device_server_context_new(HANDLE vcm);

#ifdef __cplusplus
}
#endif

#endif /* FREERDP_CHANNEL_CAMERA_DEVICE_SERVER_CAMERA_DEVICE_H */
