FreeRDP
Loading...
Searching...
No Matches
tsmf_main.c
1
21
22#include <freerdp/config.h>
23
24#include <winpr/crt.h>
25#include <winpr/stream.h>
26#include <winpr/cmdline.h>
27
28#include <freerdp/client/tsmf.h>
29
30#include "tsmf_types.h"
31#include "tsmf_constants.h"
32#include "tsmf_ifman.h"
33#include "tsmf_media.h"
34
35#include "tsmf_main.h"
36
37BOOL tsmf_send_eos_response(IWTSVirtualChannelCallback* pChannelCallback, UINT32 message_id)
38{
39 ssize_t status = -1;
40 TSMF_CHANNEL_CALLBACK* callback = (TSMF_CHANNEL_CALLBACK*)pChannelCallback;
41
42 if (!callback)
43 {
44 DEBUG_TSMF("No callback reference - unable to send eos response!");
45 return FALSE;
46 }
47
48 if (callback && callback->stream_id && callback->channel && callback->channel->Write)
49 {
50 wStream* s = Stream_New(nullptr, 24);
51
52 if (!s)
53 return FALSE;
54
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); /* FunctionId */
58 Stream_Write_UINT32(s, callback->stream_id); /* StreamId */
59 Stream_Write_UINT32(s, TSMM_CLIENT_EVENT_ENDOFSTREAM); /* EventId */
60 Stream_Write_UINT32(s, 0); /* cbData */
61 const size_t pos = Stream_GetPosition(s);
62 DEBUG_TSMF("EOS response size %" PRIuz "", pos);
63 WINPR_ASSERT(pos <= UINT32_MAX);
64 status =
65 callback->channel->Write(callback->channel, (UINT32)pos, Stream_Buffer(s), nullptr);
66
67 if (status)
68 {
69 WLog_ERR(TAG, "response error %" PRId32, WINPR_CXX_COMPAT_CAST(int32_t, status));
70 }
71
72 Stream_Free(s, TRUE);
73 }
74
75 return (status == 0);
76}
77
78BOOL tsmf_playback_ack(IWTSVirtualChannelCallback* pChannelCallback, UINT32 message_id,
79 UINT64 duration, UINT32 data_size)
80{
81 ssize_t status = -1;
82 TSMF_CHANNEL_CALLBACK* callback = (TSMF_CHANNEL_CALLBACK*)pChannelCallback;
83
84 if (!callback)
85 return FALSE;
86
87 wStream* s = Stream_New(nullptr, 32);
88
89 if (!s)
90 return FALSE;
91
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); /* FunctionId */
95 Stream_Write_UINT32(s, callback->stream_id); /* StreamId */
96 Stream_Write_UINT64(s, duration); /* DataDuration */
97 Stream_Write_UINT64(s, data_size); /* cbData */
98
99 const size_t pos = Stream_GetPosition(s);
100 DEBUG_TSMF("ACK response size %" PRIuz "", pos);
101
102 if (!callback->channel || !callback->channel->Write)
103 {
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));
107 }
108 else
109 {
110 status = callback->channel->Write(
111 callback->channel, WINPR_ASSERTING_INT_CAST(uint32_t, pos), Stream_Buffer(s), nullptr);
112 }
113
114 if (status)
115 {
116 WLog_ERR(TAG, "response error %" PRId32, WINPR_CXX_COMPAT_CAST(int32_t, status));
117 }
118
119 Stream_Free(s, TRUE);
120 return (status == 0);
121}
122
128static UINT tsmf_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
129{
130 wStream* input = nullptr;
131 wStream* output = nullptr;
132 UINT error = CHANNEL_RC_OK;
133 BOOL processed = FALSE;
134 TSMF_IFMAN ifman = WINPR_C_ARRAY_INIT;
135 UINT32 MessageId = 0;
136 UINT32 FunctionId = 0;
137 UINT32 InterfaceId = 0;
138 TSMF_CHANNEL_CALLBACK* callback = (TSMF_CHANNEL_CALLBACK*)pChannelCallback;
139 const size_t cbSize = Stream_GetRemainingLength(data);
140
141 /* 2.2.1 Shared Message Header (SHARED_MSG_HEADER) */
142 if (!Stream_CheckAndLogRequiredLength(TAG, data, 12) || (cbSize > UINT32_MAX))
143 return ERROR_INVALID_DATA;
144
145 input = data;
146 output = Stream_New(nullptr, 256);
147
148 if (!output)
149 return ERROR_OUTOFMEMORY;
150
151 Stream_Seek(output, 8);
152 Stream_Read_UINT32(input, InterfaceId); /* InterfaceId (4 bytes) */
153 Stream_Read_UINT32(input, MessageId); /* MessageId (4 bytes) */
154 Stream_Read_UINT32(input, FunctionId); /* FunctionId (4 bytes) */
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;
165 ifman.input = input;
166 ifman.input_size = (UINT32)(cbSize - 12U);
167 ifman.output = output;
168 ifman.output_pending = FALSE;
169 ifman.output_interface_id = InterfaceId;
170
171 // (void)fprintf(stderr, "InterfaceId: 0x%08"PRIX32" MessageId: 0x%08"PRIX32" FunctionId:
172 // 0x%08"PRIX32"\n", InterfaceId, MessageId, FunctionId);
173
174 switch (InterfaceId)
175 {
176 case TSMF_INTERFACE_CAPABILITIES | STREAM_ID_NONE:
177 switch (FunctionId)
178 {
179 case RIM_EXCHANGE_CAPABILITY_REQUEST:
180 error = tsmf_ifman_rim_exchange_capability_request(&ifman);
181 processed = TRUE;
182 break;
183
184 case RIMCALL_RELEASE:
185 case RIMCALL_QUERYINTERFACE:
186 break;
187
188 default:
189 break;
190 }
191
192 break;
193
194 case TSMF_INTERFACE_DEFAULT | STREAM_ID_PROXY:
195 switch (FunctionId)
196 {
197 case SET_CHANNEL_PARAMS:
198 if (!Stream_CheckAndLogRequiredLength(TAG, input, GUID_SIZE + 4))
199 {
200 error = ERROR_INVALID_DATA;
201 goto out;
202 }
203
204 /* Allow setting only once */
205 const UUID zero = WINPR_C_ARRAY_INIT;
206 if (memcmp(&zero, callback->presentation_id, sizeof(zero)) != 0)
207 {
208 error = ERROR_INVALID_DATA;
209 goto out;
210 }
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;
216 processed = TRUE;
217 break;
218
219 case EXCHANGE_CAPABILITIES_REQ:
220 error = tsmf_ifman_exchange_capability_request(&ifman);
221 processed = TRUE;
222 break;
223
224 case CHECK_FORMAT_SUPPORT_REQ:
225 error = tsmf_ifman_check_format_support_request(&ifman);
226 processed = TRUE;
227 break;
228
229 case ON_NEW_PRESENTATION:
230 error = tsmf_ifman_on_new_presentation(&ifman);
231 processed = TRUE;
232 break;
233
234 case ADD_STREAM:
235 error =
236 tsmf_ifman_add_stream(&ifman, ((TSMF_PLUGIN*)callback->plugin)->rdpcontext);
237 processed = TRUE;
238 break;
239
240 case SET_TOPOLOGY_REQ:
241 error = tsmf_ifman_set_topology_request(&ifman);
242 processed = TRUE;
243 break;
244
245 case REMOVE_STREAM:
246 error = tsmf_ifman_remove_stream(&ifman);
247 processed = TRUE;
248 break;
249
250 case SET_SOURCE_VIDEO_RECT:
251 error = tsmf_ifman_set_source_video_rect(&ifman);
252 processed = TRUE;
253 break;
254
255 case SHUTDOWN_PRESENTATION_REQ:
256 error = tsmf_ifman_shutdown_presentation(&ifman);
257 processed = TRUE;
258 break;
259
260 case ON_STREAM_VOLUME:
261 error = tsmf_ifman_on_stream_volume(&ifman);
262 processed = TRUE;
263 break;
264
265 case ON_CHANNEL_VOLUME:
266 error = tsmf_ifman_on_channel_volume(&ifman);
267 processed = TRUE;
268 break;
269
270 case SET_VIDEO_WINDOW:
271 error = tsmf_ifman_set_video_window(&ifman);
272 processed = TRUE;
273 break;
274
275 case UPDATE_GEOMETRY_INFO:
276 error = tsmf_ifman_update_geometry_info(&ifman);
277 processed = TRUE;
278 break;
279
280 case SET_ALLOCATOR:
281 error = tsmf_ifman_set_allocator(&ifman);
282 processed = TRUE;
283 break;
284
285 case NOTIFY_PREROLL:
286 error = tsmf_ifman_notify_preroll(&ifman);
287 processed = TRUE;
288 break;
289
290 case ON_SAMPLE:
291 error = tsmf_ifman_on_sample(&ifman);
292 processed = TRUE;
293 break;
294
295 case ON_FLUSH:
296 error = tsmf_ifman_on_flush(&ifman);
297 processed = TRUE;
298 break;
299
300 case ON_END_OF_STREAM:
301 error = tsmf_ifman_on_end_of_stream(&ifman);
302 processed = TRUE;
303 break;
304
305 case ON_PLAYBACK_STARTED:
306 error = tsmf_ifman_on_playback_started(&ifman);
307 processed = TRUE;
308 break;
309
310 case ON_PLAYBACK_PAUSED:
311 error = tsmf_ifman_on_playback_paused(&ifman);
312 processed = TRUE;
313 break;
314
315 case ON_PLAYBACK_RESTARTED:
316 error = tsmf_ifman_on_playback_restarted(&ifman);
317 processed = TRUE;
318 break;
319
320 case ON_PLAYBACK_STOPPED:
321 error = tsmf_ifman_on_playback_stopped(&ifman);
322 processed = TRUE;
323 break;
324
325 case ON_PLAYBACK_RATE_CHANGED:
326 error = tsmf_ifman_on_playback_rate_changed(&ifman);
327 processed = TRUE;
328 break;
329
330 case RIMCALL_RELEASE:
331 case RIMCALL_QUERYINTERFACE:
332 break;
333
334 default:
335 break;
336 }
337
338 break;
339
340 default:
341 break;
342 }
343
344 input = nullptr;
345 ifman.input = nullptr;
346
347 if (error)
348 {
349 WLog_ERR(TAG, "ifman data received processing error %" PRIu32 "", error);
350 }
351
352 if (!processed)
353 {
354 switch (FunctionId)
355 {
356 case RIMCALL_RELEASE:
357 /* [MS-RDPEXPS] 2.2.2.2 Interface Release (IFACE_RELEASE)
358 This message does not require a reply. */
359 processed = TRUE;
360 ifman.output_pending = 1;
361 break;
362
363 case RIMCALL_QUERYINTERFACE:
364 /* [MS-RDPEXPS] 2.2.2.1.2 Query Interface Response (QI_RSP)
365 This message is not supported in this channel. */
366 processed = TRUE;
367 break;
368 default:
369 break;
370 }
371
372 if (!processed)
373 {
374 WLog_ERR(TAG,
375 "Unknown InterfaceId: 0x%08" PRIX32 " MessageId: 0x%08" PRIX32
376 " FunctionId: 0x%08" PRIX32 "\n",
377 InterfaceId, MessageId, FunctionId);
378 /* When a request is not implemented we return empty response indicating error */
379 }
380
381 processed = TRUE;
382 }
383
384 if (processed && !ifman.output_pending)
385 {
386 /* Response packet does not have FunctionId */
387 const size_t length = Stream_GetPosition(output);
388 if (length > UINT32_MAX)
389 goto out;
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),
395 nullptr);
396
397 if (error)
398 {
399 WLog_ERR(TAG, "response error %" PRIu32 "", error);
400 }
401 }
402
403out:
404 Stream_Free(output, TRUE);
405 return error;
406}
407
413static UINT tsmf_on_close(IWTSVirtualChannelCallback* pChannelCallback)
414{
415 TSMF_STREAM* stream = nullptr;
416 TSMF_PRESENTATION* presentation = nullptr;
417 TSMF_CHANNEL_CALLBACK* callback = (TSMF_CHANNEL_CALLBACK*)pChannelCallback;
418 DEBUG_TSMF("");
419
420 if (callback->stream_id)
421 {
422 presentation = tsmf_presentation_find_by_id(callback->presentation_id);
423
424 if (presentation)
425 {
426 stream = tsmf_stream_find_by_id(presentation, callback->stream_id);
427
428 if (stream)
429 tsmf_stream_free(stream);
430 }
431 }
432
433 free(pChannelCallback);
434 return CHANNEL_RC_OK;
435}
436
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)
447{
448 TSMF_CHANNEL_CALLBACK* callback = nullptr;
449 TSMF_LISTENER_CALLBACK* listener_callback = (TSMF_LISTENER_CALLBACK*)pListenerCallback;
450 DEBUG_TSMF("");
451 callback = (TSMF_CHANNEL_CALLBACK*)calloc(1, sizeof(TSMF_CHANNEL_CALLBACK));
452
453 if (!callback)
454 return CHANNEL_RC_NO_MEMORY;
455
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;
464}
465
471static UINT tsmf_plugin_initialize(IWTSPlugin* pPlugin, IWTSVirtualChannelManager* pChannelMgr)
472{
473 UINT status = 0;
474 TSMF_PLUGIN* tsmf = (TSMF_PLUGIN*)pPlugin;
475 DEBUG_TSMF("");
476 tsmf->listener_callback = (TSMF_LISTENER_CALLBACK*)calloc(1, sizeof(TSMF_LISTENER_CALLBACK));
477
478 if (!tsmf->listener_callback)
479 return CHANNEL_RC_NO_MEMORY;
480
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;
487 return status;
488}
489
495static UINT tsmf_plugin_terminated(IWTSPlugin* pPlugin)
496{
497 TSMF_PLUGIN* tsmf = (TSMF_PLUGIN*)pPlugin;
498 DEBUG_TSMF("");
499 free(tsmf->listener_callback);
500 free(tsmf);
501 return CHANNEL_RC_OK;
502}
503
509static UINT tsmf_process_addin_args(IWTSPlugin* pPlugin, const ADDIN_ARGV* args)
510{
511 int status = 0;
512 DWORD flags = 0;
513 const COMMAND_LINE_ARGUMENT_A* arg = nullptr;
514 TSMF_PLUGIN* tsmf = (TSMF_PLUGIN*)pPlugin;
515 COMMAND_LINE_ARGUMENT_A tsmf_args[] = { { "sys", COMMAND_LINE_VALUE_REQUIRED, "<subsystem>",
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,
522 nullptr } };
523 flags = COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON;
524 status = CommandLineParseArgumentsA(args->argc, args->argv, tsmf_args, flags, tsmf, nullptr,
525 nullptr);
526
527 if (status != 0)
528 return ERROR_INVALID_DATA;
529
530 arg = tsmf_args;
531
532 do
533 {
534 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
535 continue;
536
537 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "sys")
538 {
539 tsmf->audio_name = _strdup(arg->Value);
540
541 if (!tsmf->audio_name)
542 return ERROR_OUTOFMEMORY;
543 }
544 CommandLineSwitchCase(arg, "dev")
545 {
546 tsmf->audio_device = _strdup(arg->Value);
547
548 if (!tsmf->audio_device)
549 return ERROR_OUTOFMEMORY;
550 }
551 CommandLineSwitchCase(arg, "decoder")
552 {
553 tsmf->decoder_name = _strdup(arg->Value);
554
555 if (!tsmf->decoder_name)
556 return ERROR_OUTOFMEMORY;
557 }
558 CommandLineSwitchDefault(arg)
559 {
560 }
561 CommandLineSwitchEnd(arg)
562 } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr);
563
564 return CHANNEL_RC_OK;
565}
566
572FREERDP_ENTRY_POINT(UINT VCAPITYPE tsmf_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
573{
574 UINT status = 0;
575 TSMF_PLUGIN* tsmf = nullptr;
576 TsmfClientContext* context = nullptr;
577 UINT error = CHANNEL_RC_NO_MEMORY;
578 tsmf = (TSMF_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints, "tsmf");
579
580 if (!tsmf)
581 {
582 tsmf = (TSMF_PLUGIN*)calloc(1, sizeof(TSMF_PLUGIN));
583
584 if (!tsmf)
585 {
586 WLog_ERR(TAG, "calloc failed!");
587 return CHANNEL_RC_NO_MEMORY;
588 }
589
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));
596
597 if (!context)
598 {
599 WLog_ERR(TAG, "calloc failed!");
600 goto error_context;
601 }
602
603 context->handle = (void*)tsmf;
604 tsmf->iface.pInterface = (void*)context;
605
606 if (!tsmf_media_init())
607 {
608 error = ERROR_INVALID_OPERATION;
609 goto error_init;
610 }
611
612 status = pEntryPoints->RegisterPlugin(pEntryPoints, "tsmf", &tsmf->iface);
613 }
614
615 if (status == CHANNEL_RC_OK)
616 {
617 status = tsmf_process_addin_args(&tsmf->iface, pEntryPoints->GetPluginData(pEntryPoints));
618 }
619
620 return status;
621error_init:
622 free(context);
623error_context:
624 free(tsmf);
625 return error;
626}
Definition wtypes.h:263