23#include <freerdp/config.h>
32#include <winpr/stream.h>
34#include "tsmf_types.h"
35#include "tsmf_constants.h"
36#include "tsmf_media.h"
37#include "tsmf_codec.h"
39#include "tsmf_ifman.h"
46UINT tsmf_ifman_rim_exchange_capability_request(
TSMF_IFMAN* ifman)
48 UINT32 CapabilityValue = 0;
50 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->input, 4))
51 return ERROR_INVALID_DATA;
53 Stream_Read_UINT32(ifman->input, CapabilityValue);
54 DEBUG_TSMF(
"server CapabilityValue %" PRIu32
"", CapabilityValue);
56 if (!Stream_EnsureRemainingCapacity(ifman->output, 8))
57 return ERROR_INVALID_DATA;
59 Stream_Write_UINT32(ifman->output, 1);
60 Stream_Write_UINT32(ifman->output, 0);
69UINT tsmf_ifman_exchange_capability_request(
TSMF_IFMAN* ifman)
71 UINT32 CapabilityType = 0;
72 UINT32 cbCapabilityLength = 0;
73 UINT32 numHostCapabilities = 0;
76 if (!Stream_EnsureRemainingCapacity(ifman->output, ifman->input_size + 4))
77 return ERROR_OUTOFMEMORY;
79 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->input, ifman->input_size))
80 return ERROR_INVALID_DATA;
82 const size_t xpos = Stream_GetPosition(ifman->output);
83 Stream_Copy(ifman->input, ifman->output, ifman->input_size);
84 Stream_SetPosition(ifman->output, xpos);
86 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->output, 4))
87 return ERROR_INVALID_DATA;
89 Stream_Read_UINT32(ifman->output, numHostCapabilities);
91 for (UINT32 i = 0; i < numHostCapabilities; i++)
93 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->output, 8))
94 return ERROR_INVALID_DATA;
96 Stream_Read_UINT32(ifman->output, CapabilityType);
97 Stream_Read_UINT32(ifman->output, cbCapabilityLength);
99 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->output, cbCapabilityLength))
100 return ERROR_INVALID_DATA;
102 const size_t pos = Stream_GetPosition(ifman->output);
104 switch (CapabilityType)
108 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->output, 4))
109 return ERROR_INVALID_DATA;
111 const UINT32 v = Stream_Get_UINT32(ifman->output);
113 DEBUG_TSMF(
"server protocol version %" PRIu32
"", v);
119 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->output, 4))
120 return ERROR_INVALID_DATA;
122 const UINT32 v = Stream_Get_UINT32(ifman->output);
124 DEBUG_TSMF(
"server supported platform %" PRIu32
"", v);
126 Stream_Write_UINT32(ifman->output, MMREDIR_CAPABILITY_PLATFORM_MF |
127 MMREDIR_CAPABILITY_PLATFORM_DSHOW);
132 WLog_ERR(TAG,
"skipping unknown capability type %" PRIu32
"", CapabilityType);
136 Stream_SetPosition(ifman->output, pos + cbCapabilityLength);
139 Stream_Write_UINT32(ifman->output, 0);
140 ifman->output_interface_id = TSMF_INTERFACE_DEFAULT | STREAM_ID_STUB;
141 return CHANNEL_RC_OK;
149UINT tsmf_ifman_check_format_support_request(
TSMF_IFMAN* ifman)
151 UINT32 numMediaType = 0;
152 UINT32 PlatformCookie = 0;
153 UINT32 FormatSupported = 1;
155 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->input, 12))
156 return ERROR_INVALID_DATA;
158 Stream_Read_UINT32(ifman->input, PlatformCookie);
159 Stream_Seek_UINT32(ifman->input);
160 Stream_Read_UINT32(ifman->input, numMediaType);
161 DEBUG_TSMF(
"PlatformCookie %" PRIu32
" numMediaType %" PRIu32
"", PlatformCookie, numMediaType);
163 if (!tsmf_codec_check_media_type(ifman->decoder_name, ifman->input))
167 DEBUG_TSMF(
"format ok.");
169 if (!Stream_EnsureRemainingCapacity(ifman->output, 12))
172 Stream_Write_UINT32(ifman->output, FormatSupported);
173 Stream_Write_UINT32(ifman->output, PlatformCookie);
174 Stream_Write_UINT32(ifman->output, 0);
175 ifman->output_interface_id = TSMF_INTERFACE_DEFAULT | STREAM_ID_STUB;
176 return CHANNEL_RC_OK;
184UINT tsmf_ifman_on_new_presentation(
TSMF_IFMAN* ifman)
186 UINT status = CHANNEL_RC_OK;
187 TSMF_PRESENTATION* presentation = NULL;
190 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->input, GUID_SIZE))
191 return ERROR_INVALID_DATA;
193 presentation = tsmf_presentation_find_by_id(Stream_Pointer(ifman->input));
197 DEBUG_TSMF(
"Presentation already exists");
198 ifman->output_pending = FALSE;
199 return CHANNEL_RC_OK;
202 presentation = tsmf_presentation_new(Stream_Pointer(ifman->input), ifman->channel_callback);
205 status = ERROR_OUTOFMEMORY;
207 tsmf_presentation_set_audio_device(presentation, ifman->audio_name, ifman->audio_device);
209 ifman->output_pending = TRUE;
218UINT tsmf_ifman_add_stream(
TSMF_IFMAN* ifman, rdpContext* rdpcontext)
221 UINT status = CHANNEL_RC_OK;
222 TSMF_STREAM* stream = NULL;
223 TSMF_PRESENTATION* presentation = NULL;
226 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->input, GUID_SIZE + 8))
227 return ERROR_INVALID_DATA;
229 presentation = tsmf_presentation_find_by_id(Stream_Pointer(ifman->input));
230 Stream_Seek(ifman->input, GUID_SIZE);
234 WLog_ERR(TAG,
"unknown presentation id");
235 status = ERROR_NOT_FOUND;
239 Stream_Read_UINT32(ifman->input, StreamId);
240 Stream_Seek_UINT32(ifman->input);
241 stream = tsmf_stream_new(presentation, StreamId, rdpcontext);
245 WLog_ERR(TAG,
"failed to create stream");
246 return ERROR_OUTOFMEMORY;
249 if (!tsmf_stream_set_format(stream, ifman->decoder_name, ifman->input))
251 WLog_ERR(TAG,
"failed to set stream format");
252 return ERROR_OUTOFMEMORY;
255 tsmf_stream_start_threads(stream);
258 ifman->output_pending = TRUE;
267UINT tsmf_ifman_set_topology_request(
TSMF_IFMAN* ifman)
271 if (!Stream_EnsureRemainingCapacity(ifman->output, 8))
272 return ERROR_OUTOFMEMORY;
274 Stream_Write_UINT32(ifman->output, 1);
275 Stream_Write_UINT32(ifman->output, 0);
276 ifman->output_interface_id = TSMF_INTERFACE_DEFAULT | STREAM_ID_STUB;
277 return CHANNEL_RC_OK;
285UINT tsmf_ifman_remove_stream(
TSMF_IFMAN* ifman)
287 int status = CHANNEL_RC_OK;
289 TSMF_STREAM* stream = NULL;
290 TSMF_PRESENTATION* presentation = NULL;
293 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->input, 20))
294 return ERROR_INVALID_DATA;
296 presentation = tsmf_presentation_find_by_id(Stream_Pointer(ifman->input));
297 Stream_Seek(ifman->input, GUID_SIZE);
301 status = ERROR_NOT_FOUND;
305 Stream_Read_UINT32(ifman->input, StreamId);
306 stream = tsmf_stream_find_by_id(presentation, StreamId);
309 tsmf_stream_free(stream);
311 status = ERROR_NOT_FOUND;
314 ifman->output_pending = TRUE;
318static float tsmf_stream_read_float(
wStream* s)
322 Stream_Read_UINT32(s, iValue);
323 CopyMemory(&fValue, &iValue, 4);
332UINT tsmf_ifman_set_source_video_rect(
TSMF_IFMAN* ifman)
334 UINT status = CHANNEL_RC_OK;
335 TSMF_PRESENTATION* presentation = NULL;
338 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->input, 32))
339 return ERROR_INVALID_DATA;
341 presentation = tsmf_presentation_find_by_id(Stream_Pointer(ifman->input));
342 Stream_Seek(ifman->input, GUID_SIZE);
346 status = ERROR_NOT_FOUND;
350#ifdef WITH_DEBUG_TSMF
351 const float Left = tsmf_stream_read_float(ifman->input);
352 const float Top = tsmf_stream_read_float(ifman->input);
353 const float Right = tsmf_stream_read_float(ifman->input);
354 const float Bottom = tsmf_stream_read_float(ifman->input);
355 DEBUG_TSMF(
"SetSourceVideoRect: Left: %f Top: %f Right: %f Bottom: %f", Left, Top, Right,
360 ifman->output_pending = TRUE;
369UINT tsmf_ifman_shutdown_presentation(
TSMF_IFMAN* ifman)
371 TSMF_PRESENTATION* presentation = NULL;
374 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->input, GUID_SIZE))
375 return ERROR_INVALID_DATA;
377 presentation = tsmf_presentation_find_by_id(Stream_Pointer(ifman->input));
380 tsmf_presentation_free(presentation);
383 WLog_ERR(TAG,
"unknown presentation id");
384 return ERROR_NOT_FOUND;
387 if (!Stream_EnsureRemainingCapacity(ifman->output, 4))
388 return ERROR_OUTOFMEMORY;
390 Stream_Write_UINT32(ifman->output, 0);
391 ifman->output_interface_id = TSMF_INTERFACE_DEFAULT | STREAM_ID_STUB;
392 return CHANNEL_RC_OK;
400UINT tsmf_ifman_on_stream_volume(
TSMF_IFMAN* ifman)
402 TSMF_PRESENTATION* presentation = NULL;
403 UINT32 newVolume = 0;
405 DEBUG_TSMF(
"on stream volume");
407 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->input, GUID_SIZE + 8))
408 return ERROR_INVALID_DATA;
410 presentation = tsmf_presentation_find_by_id(Stream_Pointer(ifman->input));
414 WLog_ERR(TAG,
"unknown presentation id");
415 return ERROR_NOT_FOUND;
418 Stream_Seek(ifman->input, 16);
419 Stream_Read_UINT32(ifman->input, newVolume);
420 DEBUG_TSMF(
"on stream volume: new volume=[%" PRIu32
"]", newVolume);
421 Stream_Read_UINT32(ifman->input, muted);
422 DEBUG_TSMF(
"on stream volume: muted=[%" PRIu32
"]", muted);
424 if (!tsmf_presentation_volume_changed(presentation, newVolume, muted))
425 return ERROR_INVALID_OPERATION;
427 ifman->output_pending = TRUE;
436UINT tsmf_ifman_on_channel_volume(
TSMF_IFMAN* ifman)
438 TSMF_PRESENTATION* presentation = NULL;
439 DEBUG_TSMF(
"on channel volume");
441 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->input, GUID_SIZE + 8))
442 return ERROR_INVALID_DATA;
444 presentation = tsmf_presentation_find_by_id(Stream_Pointer(ifman->input));
448 UINT32 channelVolume = 0;
449 UINT32 changedChannel = 0;
450 Stream_Seek(ifman->input, 16);
451 Stream_Read_UINT32(ifman->input, channelVolume);
452 DEBUG_TSMF(
"on channel volume: channel volume=[%" PRIu32
"]", channelVolume);
453 Stream_Read_UINT32(ifman->input, changedChannel);
454 DEBUG_TSMF(
"on stream volume: changed channel=[%" PRIu32
"]", changedChannel);
457 ifman->output_pending = TRUE;
458 return CHANNEL_RC_OK;
466UINT tsmf_ifman_set_video_window(
TSMF_IFMAN* ifman)
469 ifman->output_pending = TRUE;
470 return CHANNEL_RC_OK;
478UINT tsmf_ifman_update_geometry_info(
TSMF_IFMAN* ifman)
480 TSMF_PRESENTATION* presentation = NULL;
481 UINT32 numGeometryInfo = 0;
486 UINT32 cbVisibleRect = 0;
488 UINT error = CHANNEL_RC_OK;
491 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->input, GUID_SIZE + 32))
492 return ERROR_INVALID_DATA;
494 presentation = tsmf_presentation_find_by_id(Stream_Pointer(ifman->input));
497 return ERROR_NOT_FOUND;
499 Stream_Seek(ifman->input, 16);
500 Stream_Read_UINT32(ifman->input, numGeometryInfo);
501 pos = Stream_GetPosition(ifman->input);
502 Stream_Seek(ifman->input, 12);
503 Stream_Read_UINT32(ifman->input, Width);
504 Stream_Read_UINT32(ifman->input, Height);
505 Stream_Read_UINT32(ifman->input, Left);
506 Stream_Read_UINT32(ifman->input, Top);
507 Stream_SetPosition(ifman->input, pos + numGeometryInfo);
508 Stream_Read_UINT32(ifman->input, cbVisibleRect);
509 const UINT32 num_rects = cbVisibleRect / 16;
510 DEBUG_TSMF(
"numGeometryInfo %" PRIu32
" Width %" PRIu32
" Height %" PRIu32
" Left %" PRIu32
511 " Top %" PRIu32
" cbVisibleRect %" PRIu32
" num_rects %d",
512 numGeometryInfo, Width, Height, Left, Top, cbVisibleRect, num_rects);
518 for (
size_t i = 0; i < num_rects; i++)
520 Stream_Read_UINT32(ifman->input, rects[i].top);
521 Stream_Read_UINT32(ifman->input, rects[i].left);
522 Stream_Read_UINT32(ifman->input, rects[i].height);
523 Stream_Read_UINT32(ifman->input, rects[i].width);
524 rects[i].width -= rects[i].left;
525 rects[i].height -= rects[i].top;
526 DEBUG_TSMF(
"rect %d: %" PRId16
" %" PRId16
" %" PRId16
" %" PRId16
"", i, rects[i].x,
527 rects[i].y, rects[i].width, rects[i].height);
531 const BOOL rc = tsmf_presentation_set_geometry_info(presentation, Left, Top, Width, Height,
535 return ERROR_INVALID_OPERATION;
537 ifman->output_pending = TRUE;
546UINT tsmf_ifman_set_allocator(
TSMF_IFMAN* ifman)
549 ifman->output_pending = TRUE;
550 return CHANNEL_RC_OK;
558UINT tsmf_ifman_notify_preroll(
TSMF_IFMAN* ifman)
561 tsmf_ifman_on_playback_paused(ifman);
562 ifman->output_pending = TRUE;
563 return CHANNEL_RC_OK;
573 TSMF_PRESENTATION* presentation = NULL;
574 TSMF_STREAM* stream = NULL;
576 UINT64 SampleStartTime = 0;
577 UINT64 SampleEndTime = 0;
578 UINT64 ThrottleDuration = 0;
579 UINT32 SampleExtensions = 0;
583 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->input, 60))
584 return ERROR_INVALID_DATA;
586 Stream_Seek(ifman->input, 16);
587 Stream_Read_UINT32(ifman->input, StreamId);
588 Stream_Seek_UINT32(ifman->input);
589 Stream_Read_UINT64(ifman->input, SampleStartTime);
590 Stream_Read_UINT64(ifman->input, SampleEndTime);
591 Stream_Read_UINT64(ifman->input, ThrottleDuration);
592 Stream_Seek_UINT32(ifman->input);
593 Stream_Read_UINT32(ifman->input, SampleExtensions);
594 Stream_Read_UINT32(ifman->input, cbData);
596 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->input, cbData))
597 return ERROR_INVALID_DATA;
599 DEBUG_TSMF(
"MessageId %" PRIu32
" StreamId %" PRIu32
" SampleStartTime %" PRIu64
600 " SampleEndTime %" PRIu64
" "
601 "ThrottleDuration %" PRIu64
" SampleExtensions %" PRIu32
" cbData %" PRIu32
"",
602 ifman->message_id, StreamId, SampleStartTime, SampleEndTime, ThrottleDuration,
603 SampleExtensions, cbData);
604 presentation = tsmf_presentation_find_by_id(ifman->presentation_id);
608 WLog_ERR(TAG,
"unknown presentation id");
609 return ERROR_NOT_FOUND;
612 stream = tsmf_stream_find_by_id(presentation, StreamId);
616 WLog_ERR(TAG,
"unknown stream id");
617 return ERROR_NOT_FOUND;
620 if (!tsmf_stream_push_sample(stream, ifman->channel_callback, ifman->message_id,
621 SampleStartTime, SampleEndTime, ThrottleDuration, SampleExtensions,
622 cbData, Stream_Pointer(ifman->input)))
624 WLog_ERR(TAG,
"unable to push sample");
625 return ERROR_OUTOFMEMORY;
628 if ((error = tsmf_presentation_sync(presentation)))
630 WLog_ERR(TAG,
"tsmf_presentation_sync failed with error %" PRIu32
"", error);
634 ifman->output_pending = TRUE;
635 return CHANNEL_RC_OK;
646 TSMF_PRESENTATION* presentation = NULL;
647 TSMF_STREAM* stream = NULL;
649 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->input, 20))
650 return ERROR_INVALID_DATA;
652 Stream_Seek(ifman->input, 16);
653 Stream_Read_UINT32(ifman->input, StreamId);
654 DEBUG_TSMF(
"StreamId %" PRIu32
"", StreamId);
655 presentation = tsmf_presentation_find_by_id(ifman->presentation_id);
659 WLog_ERR(TAG,
"unknown presentation id");
660 return ERROR_NOT_FOUND;
666 stream = tsmf_stream_find_by_id(presentation, StreamId);
670 if (!tsmf_stream_flush(stream))
671 return ERROR_INVALID_OPERATION;
674 WLog_ERR(TAG,
"unknown stream id");
676 ifman->output_pending = TRUE;
677 return CHANNEL_RC_OK;
685UINT tsmf_ifman_on_end_of_stream(
TSMF_IFMAN* ifman)
688 TSMF_STREAM* stream = NULL;
689 TSMF_PRESENTATION* presentation = NULL;
691 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->input, 20))
692 return ERROR_INVALID_DATA;
694 presentation = tsmf_presentation_find_by_id(Stream_Pointer(ifman->input));
695 Stream_Seek(ifman->input, 16);
696 Stream_Read_UINT32(ifman->input, StreamId);
700 stream = tsmf_stream_find_by_id(presentation, StreamId);
703 tsmf_stream_end(stream, ifman->message_id, ifman->channel_callback);
706 DEBUG_TSMF(
"StreamId %" PRIu32
"", StreamId);
707 ifman->output_pending = TRUE;
708 ifman->output_interface_id = TSMF_INTERFACE_CLIENT_NOTIFICATIONS | STREAM_ID_PROXY;
709 return CHANNEL_RC_OK;
717UINT tsmf_ifman_on_playback_started(
TSMF_IFMAN* ifman)
719 TSMF_PRESENTATION* presentation = NULL;
722 if (!Stream_CheckAndLogRequiredLength(TAG, ifman->input, 16))
723 return ERROR_INVALID_DATA;
725 presentation = tsmf_presentation_find_by_id(Stream_Pointer(ifman->input));
728 tsmf_presentation_start(presentation);
730 WLog_ERR(TAG,
"unknown presentation id");
732 if (!Stream_EnsureRemainingCapacity(ifman->output, 16))
733 return ERROR_OUTOFMEMORY;
735 Stream_Write_UINT32(ifman->output, CLIENT_EVENT_NOTIFICATION);
736 Stream_Write_UINT32(ifman->output, 0);
737 Stream_Write_UINT32(ifman->output, TSMM_CLIENT_EVENT_START_COMPLETED);
738 Stream_Write_UINT32(ifman->output, 0);
739 ifman->output_interface_id = TSMF_INTERFACE_CLIENT_NOTIFICATIONS | STREAM_ID_PROXY;
740 return CHANNEL_RC_OK;
748UINT tsmf_ifman_on_playback_paused(
TSMF_IFMAN* ifman)
750 TSMF_PRESENTATION* presentation = NULL;
752 ifman->output_pending = TRUE;
754 presentation = tsmf_presentation_find_by_id(Stream_Pointer(ifman->input));
758 if (!tsmf_presentation_paused(presentation))
759 return ERROR_INVALID_OPERATION;
762 WLog_ERR(TAG,
"unknown presentation id");
764 return CHANNEL_RC_OK;
772UINT tsmf_ifman_on_playback_restarted(
TSMF_IFMAN* ifman)
774 TSMF_PRESENTATION* presentation = NULL;
776 ifman->output_pending = TRUE;
778 presentation = tsmf_presentation_find_by_id(Stream_Pointer(ifman->input));
782 if (!tsmf_presentation_restarted(presentation))
783 return ERROR_INVALID_OPERATION;
786 WLog_ERR(TAG,
"unknown presentation id");
788 return CHANNEL_RC_OK;
796UINT tsmf_ifman_on_playback_stopped(
TSMF_IFMAN* ifman)
798 TSMF_PRESENTATION* presentation = NULL;
800 presentation = tsmf_presentation_find_by_id(Stream_Pointer(ifman->input));
804 if (!tsmf_presentation_stop(presentation))
805 return ERROR_INVALID_OPERATION;
808 WLog_ERR(TAG,
"unknown presentation id");
810 if (!Stream_EnsureRemainingCapacity(ifman->output, 16))
811 return ERROR_OUTOFMEMORY;
813 Stream_Write_UINT32(ifman->output, CLIENT_EVENT_NOTIFICATION);
814 Stream_Write_UINT32(ifman->output, 0);
815 Stream_Write_UINT32(ifman->output, TSMM_CLIENT_EVENT_STOP_COMPLETED);
816 Stream_Write_UINT32(ifman->output, 0);
817 ifman->output_interface_id = TSMF_INTERFACE_CLIENT_NOTIFICATIONS | STREAM_ID_PROXY;
818 return CHANNEL_RC_OK;
826UINT tsmf_ifman_on_playback_rate_changed(
TSMF_IFMAN* ifman)
830 if (!Stream_EnsureRemainingCapacity(ifman->output, 16))
831 return ERROR_OUTOFMEMORY;
833 Stream_Write_UINT32(ifman->output, CLIENT_EVENT_NOTIFICATION);
834 Stream_Write_UINT32(ifman->output, 0);
835 Stream_Write_UINT32(ifman->output, TSMM_CLIENT_EVENT_MONITORCHANGED);
836 Stream_Write_UINT32(ifman->output, 0);
837 ifman->output_interface_id = TSMF_INTERFACE_CLIENT_NOTIFICATIONS | STREAM_ID_PROXY;
838 return CHANNEL_RC_OK;