22#include <freerdp/config.h>
25#include <winpr/stream.h>
26#include <winpr/cmdline.h>
28#include <freerdp/client/tsmf.h>
30#include "tsmf_types.h"
31#include "tsmf_constants.h"
32#include "tsmf_ifman.h"
33#include "tsmf_media.h"
37BOOL tsmf_send_eos_response(IWTSVirtualChannelCallback* pChannelCallback, UINT32 message_id)
44 DEBUG_TSMF(
"No callback reference - unable to send eos response!");
48 if (callback && callback->stream_id && callback->channel && callback->channel->Write)
50 wStream* s = Stream_New(
nullptr, 24);
55 Stream_Write_UINT32(s, TSMF_INTERFACE_CLIENT_NOTIFICATIONS | STREAM_ID_PROXY);
56 Stream_Write_UINT32(s, message_id);
57 Stream_Write_UINT32(s, CLIENT_EVENT_NOTIFICATION);
58 Stream_Write_UINT32(s, callback->stream_id);
59 Stream_Write_UINT32(s, TSMM_CLIENT_EVENT_ENDOFSTREAM);
60 Stream_Write_UINT32(s, 0);
61 const size_t pos = Stream_GetPosition(s);
62 DEBUG_TSMF(
"EOS response size %" PRIuz
"", pos);
63 WINPR_ASSERT(pos <= UINT32_MAX);
65 callback->channel->Write(callback->channel, (UINT32)pos, Stream_Buffer(s),
nullptr);
69 WLog_ERR(TAG,
"response error %" PRId32, WINPR_CXX_COMPAT_CAST(int32_t, status));
78BOOL tsmf_playback_ack(IWTSVirtualChannelCallback* pChannelCallback, UINT32 message_id,
79 UINT64 duration, UINT32 data_size)
87 wStream* s = Stream_New(
nullptr, 32);
92 Stream_Write_UINT32(s, TSMF_INTERFACE_CLIENT_NOTIFICATIONS | STREAM_ID_PROXY);
93 Stream_Write_UINT32(s, message_id);
94 Stream_Write_UINT32(s, PLAYBACK_ACK);
95 Stream_Write_UINT32(s, callback->stream_id);
96 Stream_Write_UINT64(s, duration);
97 Stream_Write_UINT64(s, data_size);
99 const size_t pos = Stream_GetPosition(s);
100 DEBUG_TSMF(
"ACK response size %" PRIuz
"", pos);
102 if (!callback->channel || !callback->channel->Write)
104 WLog_ERR(TAG,
"channel=%p, write=%p", WINPR_CXX_COMPAT_CAST(
const void*, callback->channel),
105 WINPR_CXX_COMPAT_CAST(
const void*,
106 callback->channel ? callback->channel->Write :
nullptr));
110 status = callback->channel->Write(
111 callback->channel, WINPR_ASSERTING_INT_CAST(uint32_t, pos), Stream_Buffer(s),
nullptr);
116 WLog_ERR(TAG,
"response error %" PRId32, WINPR_CXX_COMPAT_CAST(int32_t, status));
119 Stream_Free(s, TRUE);
120 return (status == 0);
128static UINT tsmf_on_data_received(IWTSVirtualChannelCallback* pChannelCallback,
wStream* data)
132 UINT error = CHANNEL_RC_OK;
133 BOOL processed = FALSE;
135 UINT32 MessageId = 0;
136 UINT32 FunctionId = 0;
137 UINT32 InterfaceId = 0;
139 const size_t cbSize = Stream_GetRemainingLength(data);
142 if (!Stream_CheckAndLogRequiredLength(TAG, data, 12) || (cbSize > UINT32_MAX))
143 return ERROR_INVALID_DATA;
146 output = Stream_New(
nullptr, 256);
149 return ERROR_OUTOFMEMORY;
151 Stream_Seek(output, 8);
152 Stream_Read_UINT32(input, InterfaceId);
153 Stream_Read_UINT32(input, MessageId);
154 Stream_Read_UINT32(input, FunctionId);
155 DEBUG_TSMF(
"cbSize=%" PRIu32
" InterfaceId=0x%" PRIX32
" MessageId=0x%" PRIX32
156 " FunctionId=0x%" PRIX32
"",
157 cbSize, InterfaceId, MessageId, FunctionId);
158 ifman.channel_callback = pChannelCallback;
159 ifman.decoder_name = ((
TSMF_PLUGIN*)callback->plugin)->decoder_name;
160 ifman.audio_name = ((
TSMF_PLUGIN*)callback->plugin)->audio_name;
161 ifman.audio_device = ((
TSMF_PLUGIN*)callback->plugin)->audio_device;
162 CopyMemory(ifman.presentation_id, callback->presentation_id, GUID_SIZE);
163 ifman.stream_id = callback->stream_id;
164 ifman.message_id = MessageId;
166 ifman.input_size = (UINT32)(cbSize - 12U);
167 ifman.output = output;
168 ifman.output_pending = FALSE;
169 ifman.output_interface_id = InterfaceId;
176 case TSMF_INTERFACE_CAPABILITIES | STREAM_ID_NONE:
179 case RIM_EXCHANGE_CAPABILITY_REQUEST:
180 error = tsmf_ifman_rim_exchange_capability_request(&ifman);
184 case RIMCALL_RELEASE:
185 case RIMCALL_QUERYINTERFACE:
194 case TSMF_INTERFACE_DEFAULT | STREAM_ID_PROXY:
197 case SET_CHANNEL_PARAMS:
198 if (!Stream_CheckAndLogRequiredLength(TAG, input, GUID_SIZE + 4))
200 error = ERROR_INVALID_DATA;
205 const UUID zero = WINPR_C_ARRAY_INIT;
206 if (memcmp(&zero, callback->presentation_id,
sizeof(zero)) != 0)
208 error = ERROR_INVALID_DATA;
211 CopyMemory(callback->presentation_id, Stream_Pointer(input), GUID_SIZE);
212 Stream_Seek(input, GUID_SIZE);
213 Stream_Read_UINT32(input, callback->stream_id);
214 DEBUG_TSMF(
"SET_CHANNEL_PARAMS StreamId=%" PRIu32
"", callback->stream_id);
215 ifman.output_pending = TRUE;
219 case EXCHANGE_CAPABILITIES_REQ:
220 error = tsmf_ifman_exchange_capability_request(&ifman);
224 case CHECK_FORMAT_SUPPORT_REQ:
225 error = tsmf_ifman_check_format_support_request(&ifman);
229 case ON_NEW_PRESENTATION:
230 error = tsmf_ifman_on_new_presentation(&ifman);
236 tsmf_ifman_add_stream(&ifman, ((
TSMF_PLUGIN*)callback->plugin)->rdpcontext);
240 case SET_TOPOLOGY_REQ:
241 error = tsmf_ifman_set_topology_request(&ifman);
246 error = tsmf_ifman_remove_stream(&ifman);
250 case SET_SOURCE_VIDEO_RECT:
251 error = tsmf_ifman_set_source_video_rect(&ifman);
255 case SHUTDOWN_PRESENTATION_REQ:
256 error = tsmf_ifman_shutdown_presentation(&ifman);
260 case ON_STREAM_VOLUME:
261 error = tsmf_ifman_on_stream_volume(&ifman);
265 case ON_CHANNEL_VOLUME:
266 error = tsmf_ifman_on_channel_volume(&ifman);
270 case SET_VIDEO_WINDOW:
271 error = tsmf_ifman_set_video_window(&ifman);
275 case UPDATE_GEOMETRY_INFO:
276 error = tsmf_ifman_update_geometry_info(&ifman);
281 error = tsmf_ifman_set_allocator(&ifman);
286 error = tsmf_ifman_notify_preroll(&ifman);
291 error = tsmf_ifman_on_sample(&ifman);
296 error = tsmf_ifman_on_flush(&ifman);
300 case ON_END_OF_STREAM:
301 error = tsmf_ifman_on_end_of_stream(&ifman);
305 case ON_PLAYBACK_STARTED:
306 error = tsmf_ifman_on_playback_started(&ifman);
310 case ON_PLAYBACK_PAUSED:
311 error = tsmf_ifman_on_playback_paused(&ifman);
315 case ON_PLAYBACK_RESTARTED:
316 error = tsmf_ifman_on_playback_restarted(&ifman);
320 case ON_PLAYBACK_STOPPED:
321 error = tsmf_ifman_on_playback_stopped(&ifman);
325 case ON_PLAYBACK_RATE_CHANGED:
326 error = tsmf_ifman_on_playback_rate_changed(&ifman);
330 case RIMCALL_RELEASE:
331 case RIMCALL_QUERYINTERFACE:
345 ifman.input =
nullptr;
349 WLog_ERR(TAG,
"ifman data received processing error %" PRIu32
"", error);
356 case RIMCALL_RELEASE:
360 ifman.output_pending = 1;
363 case RIMCALL_QUERYINTERFACE:
375 "Unknown InterfaceId: 0x%08" PRIX32
" MessageId: 0x%08" PRIX32
376 " FunctionId: 0x%08" PRIX32
"\n",
377 InterfaceId, MessageId, FunctionId);
384 if (processed && !ifman.output_pending)
387 const size_t length = Stream_GetPosition(output);
388 if (length > UINT32_MAX)
390 Stream_ResetPosition(output);
391 Stream_Write_UINT32(output, ifman.output_interface_id);
392 Stream_Write_UINT32(output, MessageId);
393 DEBUG_TSMF(
"response size %d", length);
394 error = callback->channel->Write(callback->channel, (UINT32)length, Stream_Buffer(output),
399 WLog_ERR(TAG,
"response error %" PRIu32
"", error);
404 Stream_Free(output, TRUE);
413static UINT tsmf_on_close(IWTSVirtualChannelCallback* pChannelCallback)
415 TSMF_STREAM* stream =
nullptr;
416 TSMF_PRESENTATION* presentation =
nullptr;
420 if (callback->stream_id)
422 presentation = tsmf_presentation_find_by_id(callback->presentation_id);
426 stream = tsmf_stream_find_by_id(presentation, callback->stream_id);
429 tsmf_stream_free(stream);
433 free(pChannelCallback);
434 return CHANNEL_RC_OK;
442static UINT tsmf_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
443 IWTSVirtualChannel* pChannel,
444 WINPR_ATTR_UNUSED BYTE* Data,
445 WINPR_ATTR_UNUSED BOOL* pbAccept,
446 IWTSVirtualChannelCallback** ppCallback)
454 return CHANNEL_RC_NO_MEMORY;
456 callback->iface.OnDataReceived = tsmf_on_data_received;
457 callback->iface.OnClose = tsmf_on_close;
458 callback->iface.OnOpen =
nullptr;
459 callback->plugin = listener_callback->plugin;
460 callback->channel_mgr = listener_callback->channel_mgr;
461 callback->channel = pChannel;
462 *ppCallback = (IWTSVirtualChannelCallback*)callback;
463 return CHANNEL_RC_OK;
471static UINT tsmf_plugin_initialize(IWTSPlugin* pPlugin, IWTSVirtualChannelManager* pChannelMgr)
478 if (!tsmf->listener_callback)
479 return CHANNEL_RC_NO_MEMORY;
481 tsmf->listener_callback->iface.OnNewChannelConnection = tsmf_on_new_channel_connection;
482 tsmf->listener_callback->plugin = pPlugin;
483 tsmf->listener_callback->channel_mgr = pChannelMgr;
484 status = pChannelMgr->CreateListener(
485 pChannelMgr,
"TSMF", 0, (IWTSListenerCallback*)tsmf->listener_callback, &(tsmf->listener));
486 tsmf->listener->pInterface = tsmf->iface.pInterface;
495static UINT tsmf_plugin_terminated(IWTSPlugin* pPlugin)
499 free(tsmf->listener_callback);
501 return CHANNEL_RC_OK;
509static UINT tsmf_process_addin_args(IWTSPlugin* pPlugin,
const ADDIN_ARGV* args)
516 nullptr,
nullptr, -1,
nullptr,
"audio subsystem" },
517 {
"dev", COMMAND_LINE_VALUE_REQUIRED,
"<device>",
518 nullptr,
nullptr, -1,
nullptr,
"audio device name" },
519 {
"decoder", COMMAND_LINE_VALUE_REQUIRED,
"<subsystem>",
520 nullptr,
nullptr, -1,
nullptr,
"decoder subsystem" },
521 {
nullptr, 0,
nullptr,
nullptr,
nullptr, -1,
nullptr,
523 flags = COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON;
524 status = CommandLineParseArgumentsA(args->argc, args->argv, tsmf_args, flags, tsmf,
nullptr,
528 return ERROR_INVALID_DATA;
534 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
537 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"sys")
539 tsmf->audio_name = _strdup(arg->Value);
541 if (!tsmf->audio_name)
542 return ERROR_OUTOFMEMORY;
544 CommandLineSwitchCase(arg,
"dev")
546 tsmf->audio_device = _strdup(arg->Value);
548 if (!tsmf->audio_device)
549 return ERROR_OUTOFMEMORY;
551 CommandLineSwitchCase(arg,
"decoder")
553 tsmf->decoder_name = _strdup(arg->Value);
555 if (!tsmf->decoder_name)
556 return ERROR_OUTOFMEMORY;
558 CommandLineSwitchDefault(arg)
561 CommandLineSwitchEnd(arg)
562 }
while ((arg = CommandLineFindNextArgumentA(arg)) !=
nullptr);
564 return CHANNEL_RC_OK;
572FREERDP_ENTRY_POINT(UINT VCAPITYPE tsmf_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
576 TsmfClientContext* context =
nullptr;
577 UINT error = CHANNEL_RC_NO_MEMORY;
578 tsmf = (
TSMF_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints,
"tsmf");
586 WLog_ERR(TAG,
"calloc failed!");
587 return CHANNEL_RC_NO_MEMORY;
590 tsmf->iface.Initialize = tsmf_plugin_initialize;
591 tsmf->iface.Connected =
nullptr;
592 tsmf->iface.Disconnected =
nullptr;
593 tsmf->iface.Terminated = tsmf_plugin_terminated;
594 tsmf->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
595 context = (TsmfClientContext*)calloc(1,
sizeof(TsmfClientContext));
599 WLog_ERR(TAG,
"calloc failed!");
603 context->handle = (
void*)tsmf;
604 tsmf->iface.pInterface = (
void*)context;
606 if (!tsmf_media_init())
608 error = ERROR_INVALID_OPERATION;
612 status = pEntryPoints->RegisterPlugin(pEntryPoints,
"tsmf", &tsmf->iface);
615 if (status == CHANNEL_RC_OK)
617 status = tsmf_process_addin_args(&tsmf->iface, pEntryPoints->GetPluginData(pEntryPoints));