23#include <freerdp/config.h>
35#include <winpr/synch.h>
36#include <winpr/string.h>
37#include <winpr/thread.h>
38#include <winpr/stream.h>
39#include <winpr/collections.h>
41#include <freerdp/freerdp.h>
42#include <freerdp/client/tsmf.h>
44#include "tsmf_constants.h"
45#include "tsmf_types.h"
46#include "tsmf_decoder.h"
47#include "tsmf_audio.h"
49#include "tsmf_codec.h"
50#include "tsmf_media.h"
52#define AUDIO_TOLERANCE 10000000LL
55#define VIDEO_ADJUST_MAX (10ULL * 1000ULL * 1000ULL)
57#define MAX_ACK_TIME 666667
59#define AUDIO_MIN_BUFFER_LEVEL 3
60#define AUDIO_MAX_BUFFER_LEVEL 6
62#define VIDEO_MIN_BUFFER_LEVEL 10
63#define VIDEO_MAX_BUFFER_LEVEL 30
65struct S_TSMF_PRESENTATION
67 BYTE presentation_id[GUID_SIZE];
69 const char* audio_name;
70 const char* audio_device;
72 IWTSVirtualChannelCallback* channel_callback;
74 UINT64 audio_start_time;
75 UINT64 audio_end_time;
80 wArrayList* stream_list;
92 TSMF_PRESENTATION* presentation;
94 ITSMFDecoder* decoder;
98 UINT32 eos_message_id;
103 ITSMFAudioDevice* audio;
106 UINT32 bits_per_sample;
109 UINT64 last_start_time;
111 UINT64 last_end_time;
113 UINT64 next_start_time;
115 UINT32 minBufferLevel;
116 UINT32 maxBufferLevel;
117 UINT32 currentBufferLevel;
125 wQueue* sample_ack_list;
126 rdpContext* rdpcontext;
143 BOOL invalidTimestamps;
148static wArrayList* presentation_list =
nullptr;
149static int TERMINATING = 0;
151static void s_tsmf_presentation_free(
void* obj);
152static void s_tsmf_stream_free(
void* obj);
154static UINT64 get_current_time(
void)
157 gettimeofday(&tp, 0);
158 return ((UINT64)tp.tv_sec) * 10000000LL + ((UINT64)tp.tv_usec) * 10LL;
161static TSMF_SAMPLE* tsmf_stream_pop_sample(TSMF_STREAM* stream,
int sync)
163 TSMF_SAMPLE* sample =
nullptr;
164 BOOL pending = FALSE;
169 TSMF_PRESENTATION* presentation = stream->presentation;
171 if (Queue_Count(stream->sample_list) < 1)
178 if (stream->decoder->GetDecodedData)
180 if (stream->major_type == TSMF_MAJOR_TYPE_AUDIO)
187 if (stream->last_start_time > AUDIO_TOLERANCE)
189 ArrayList_Lock(presentation->stream_list);
190 const size_t count = ArrayList_Count(presentation->stream_list);
192 for (
size_t index = 0; index < count; index++)
195 (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
200 if (s != stream && !s->eos && s->last_start_time &&
201 s->last_start_time < stream->last_start_time - AUDIO_TOLERANCE)
203 DEBUG_TSMF(
"Pending due to audio tolerance");
209 ArrayList_Unlock(presentation->stream_list);
217 if (stream->last_start_time > presentation->audio_start_time)
219 DEBUG_TSMF(
"Pending due to stream start time > audio start time");
230 sample = (TSMF_SAMPLE*)Queue_Dequeue(stream->sample_list);
234 if (sample && (sample->end_time > stream->last_end_time) && (!sample->invalidTimestamps))
235 stream->last_end_time = sample->end_time;
239 if (sample && (sample->start_time > stream->last_start_time) && (!sample->invalidTimestamps))
240 stream->last_start_time = sample->start_time;
245static void tsmf_sample_free(
void* arg)
247 TSMF_SAMPLE* sample = arg;
256static BOOL tsmf_sample_ack(TSMF_STREAM* stream,
const TSMF_SAMPLE* sample)
258 if (!sample || !stream)
261 return tsmf_playback_ack(stream->presentation->channel_callback, sample->sample_id,
262 sample->duration, sample->data_size);
265static BOOL tsmf_sample_queue_ack(TSMF_STREAM* stream, TSMF_SAMPLE* sample)
273 return Queue_Enqueue(stream->sample_ack_list, sample);
279static BOOL tsmf_stream_process_ack(
void* arg, BOOL force)
281 TSMF_STREAM* stream = arg;
282 TSMF_SAMPLE* sample =
nullptr;
289 Queue_Lock(stream->sample_ack_list);
290 sample = (TSMF_SAMPLE*)Queue_Peek(stream->sample_ack_list);
301 if (stream->decoder && stream->decoder->BufferLevel)
304 if (stream->currentBufferLevel > stream->maxBufferLevel)
307 else if (stream->currentBufferLevel < stream->minBufferLevel)
312 ack_time = get_current_time();
314 if (sample->ack_time > ack_time)
319 sample = Queue_Dequeue(stream->sample_ack_list);
323 tsmf_sample_ack(stream, sample);
324 tsmf_sample_free(sample);
328 Queue_Unlock(stream->sample_ack_list);
332const IWTSVirtualChannelCallback*
333tsmf_presentation_get_callback(
const TSMF_PRESENTATION* presentation)
337 return presentation->channel_callback;
340TSMF_PRESENTATION* tsmf_presentation_new(
const BYTE* guid,
341 IWTSVirtualChannelCallback* pChannelCallback)
344 TSMF_PRESENTATION* presentation =
nullptr;
346 if (!guid || !pChannelCallback)
349 presentation = (TSMF_PRESENTATION*)calloc(1,
sizeof(TSMF_PRESENTATION));
353 WLog_ERR(TAG,
"calloc failed");
357 CopyMemory(presentation->presentation_id, guid, GUID_SIZE);
358 presentation->channel_callback = pChannelCallback;
359 presentation->volume = 5000;
360 presentation->muted = 0;
362 if (!(presentation->stream_list = ArrayList_New(TRUE)))
363 goto error_stream_list;
365 obj = ArrayList_Object(presentation->stream_list);
368 obj->fnObjectFree = s_tsmf_stream_free;
370 if (!ArrayList_Append(presentation_list, presentation))
375 ArrayList_Free(presentation->stream_list);
381static char* guid_to_string(
const BYTE* guid,
char* str,
size_t len)
386 for (
size_t i = 0; i < GUID_SIZE && (len > 2 * i); i++)
387 (
void)sprintf_s(str + (2 * i), len - 2 * i,
"%02" PRIX8
"", guid[i]);
392TSMF_PRESENTATION* tsmf_presentation_find_by_id(
const BYTE* guid)
395 char guid_str[GUID_SIZE * 2ull + 1] = WINPR_C_ARRAY_INIT;
396 TSMF_PRESENTATION* presentation =
nullptr;
397 ArrayList_Lock(presentation_list);
398 const size_t count = ArrayList_Count(presentation_list);
400 for (
size_t index = 0; index < count; index++)
402 presentation = (TSMF_PRESENTATION*)ArrayList_GetItem(presentation_list, index);
404 if (memcmp(presentation->presentation_id, guid, GUID_SIZE) == 0)
411 ArrayList_Unlock(presentation_list);
414 WLog_WARN(TAG,
"presentation id %s not found",
415 guid_to_string(guid, guid_str,
sizeof(guid_str)));
417 return (found) ? presentation :
nullptr;
420static BOOL tsmf_sample_playback_video(TSMF_STREAM* stream, TSMF_SAMPLE* sample)
423 WINPR_ASSERT(sample);
424 WINPR_ASSERT(stream);
426 TSMF_PRESENTATION* presentation = stream->presentation;
429 TsmfClientContext* tsmf = (TsmfClientContext*)callback->plugin->pInterface;
430 DEBUG_TSMF(
"MessageId %" PRIu32
" EndTime %" PRIu64
" data_size %" PRIu32
" consumed.",
431 sample->sample_id, sample->end_time, sample->data_size);
436 const UINT64 t = get_current_time();
441 if (stream->next_start_time > t &&
442 ((sample->start_time >= presentation->audio_start_time) ||
443 ((sample->start_time < stream->last_start_time) && (!sample->invalidTimestamps))))
445 size_t delay = (stream->next_start_time - t) / 10;
448 const UINT32 d = (delay > UINT32_MAX) ? UINT32_MAX : (UINT32)delay;
454 if (stream->width > INT16_MAX)
456 if (stream->height > INT16_MAX)
458 if (presentation->rect.left > INT16_MAX)
460 if (presentation->rect.top > INT16_MAX)
462 if (presentation->rect.width > INT16_MAX)
464 if (presentation->rect.height > INT16_MAX)
466 if (presentation->nr_rects > UINT16_MAX)
469 stream->next_start_time = t + sample->duration - 50000;
472 event.frameData = sample->data;
473 event.frameSize = sample->decoded_size;
474 event.framePixFmt = sample->pixfmt;
475 event.frameWidth = (INT16)stream->width;
476 event.frameHeight = (INT16)stream->height;
477 event.x = (INT16)presentation->rect.left;
478 event.y = (INT16)presentation->rect.top;
479 event.width = (INT16)presentation->rect.width;
480 event.height = (INT16)presentation->rect.height;
482 if (presentation->nr_rects > 0)
484 event.numVisibleRects = (UINT16)presentation->nr_rects;
487 if (!event.visibleRects)
489 WLog_ERR(TAG,
"can't allocate memory for copy rectangles");
493 for (
size_t x = 0; x < presentation->nr_rects; x++)
497 if ((cur->left > UINT16_MAX) || (cur->top > UINT16_MAX) ||
498 (cur->width > UINT16_MAX) || (cur->height > UINT16_MAX))
501 dst->right = dst->left = (UINT16)cur->left;
502 dst->bottom = dst->top = (UINT16)cur->top;
503 dst->right += (UINT16)cur->width;
504 dst->bottom += (UINT16)cur->height;
506 memcpy(event.visibleRects, presentation->rects,
508 presentation->nr_rects = 0;
513 sample->data =
nullptr;
514 sample->decoded_size = 0;
516 if (tsmf->FrameEvent)
518 if (tsmf->FrameEvent(tsmf, &event) < 0)
524 free(event.frameData);
525 free(event.visibleRects);
526 ArrayList_Unlock(presentation->stream_list);
531static BOOL tsmf_sample_playback_audio(TSMF_STREAM* stream, TSMF_SAMPLE* sample)
535 DEBUG_TSMF(
"MessageId %" PRIu32
" EndTime %" PRIu64
" consumed.", sample->sample_id,
538 if (stream->audio && sample->data)
540 ret = stream->audio->Play(stream->audio, sample->data, sample->decoded_size);
542 sample->data =
nullptr;
543 sample->decoded_size = 0;
545 if (stream->audio->GetLatency)
546 latency = stream->audio->GetLatency(stream->audio);
554 sample->ack_time = latency + get_current_time();
557 if (!sample->invalidTimestamps)
559 stream->last_start_time = sample->start_time + latency;
560 stream->last_end_time = sample->end_time + latency;
561 stream->presentation->audio_start_time = sample->start_time + latency;
562 stream->presentation->audio_end_time = sample->end_time + latency;
568static BOOL tsmf_sample_playback(TSMF_STREAM* stream, TSMF_SAMPLE* sample)
577 if (stream->decoder->DecodeEx)
584 if (stream->major_type == TSMF_MAJOR_TYPE_VIDEO)
586 TSMF_STREAM* temp_stream =
nullptr;
587 TSMF_PRESENTATION* presentation = stream->presentation;
588 ArrayList_Lock(presentation->stream_list);
589 const size_t count = ArrayList_Count(presentation->stream_list);
591 for (
size_t index = 0; index < count; index++)
593 UINT64 time_diff = 0;
594 temp_stream = (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
596 if (temp_stream->major_type == TSMF_MAJOR_TYPE_AUDIO)
598 UINT64 video_time = stream->decoder->GetRunningTime(stream->decoder);
600 temp_stream->decoder->GetRunningTime(temp_stream->decoder);
601 UINT64 max_adjust = VIDEO_ADJUST_MAX;
603 if (video_time < audio_time)
604 max_adjust = -VIDEO_ADJUST_MAX;
606 if (video_time > audio_time)
607 time_diff = video_time - audio_time;
609 time_diff = audio_time - video_time;
611 time_diff = time_diff < VIDEO_ADJUST_MAX ? time_diff : max_adjust;
612 sample->start_time += time_diff;
613 sample->end_time += time_diff;
618 ArrayList_Unlock(presentation->stream_list);
621 ret = stream->decoder->DecodeEx(stream->decoder, sample->data, sample->data_size,
622 sample->extensions, sample->start_time,
623 sample->end_time, sample->duration);
627 ret = stream->decoder->Decode(stream->decoder, sample->data, sample->data_size,
634 WLog_ERR(TAG,
"decode error, queue ack anyways");
636 if (!tsmf_sample_queue_ack(stream, sample))
638 WLog_ERR(TAG,
"error queuing sample for ack");
646 sample->data =
nullptr;
648 if (stream->major_type == TSMF_MAJOR_TYPE_VIDEO)
650 if (stream->decoder->GetDecodedFormat)
652 pixfmt = stream->decoder->GetDecodedFormat(stream->decoder);
654 if (pixfmt == ((UINT32)-1))
656 WLog_ERR(TAG,
"unable to decode video format");
658 if (!tsmf_sample_queue_ack(stream, sample))
660 WLog_ERR(TAG,
"error queuing sample for ack");
666 sample->pixfmt = pixfmt;
669 if (stream->decoder->GetDecodedDimension)
671 ret = stream->decoder->GetDecodedDimension(stream->decoder, &width, &height);
673 if (ret && (width != stream->width || height != stream->height))
675 DEBUG_TSMF(
"video dimension changed to %" PRIu32
" x %" PRIu32
"", width, height);
676 stream->width = width;
677 stream->height = height;
682 if (stream->decoder->GetDecodedData)
684 sample->data = stream->decoder->GetDecodedData(stream->decoder, &sample->decoded_size);
686 switch (stream->major_type)
688 case TSMF_MAJOR_TYPE_VIDEO:
689 ret = tsmf_sample_playback_video(stream, sample) &&
690 tsmf_sample_queue_ack(stream, sample);
693 case TSMF_MAJOR_TYPE_AUDIO:
694 ret = tsmf_sample_playback_audio(stream, sample) &&
695 tsmf_sample_queue_ack(stream, sample);
703 UINT64 ack_anticipation_time = get_current_time();
705 ack_anticipation_time +=
706 (sample->duration / 2 < MAX_ACK_TIME) ? sample->duration / 2 : MAX_ACK_TIME;
708 switch (stream->major_type)
710 case TSMF_MAJOR_TYPE_VIDEO:
715 case TSMF_MAJOR_TYPE_AUDIO:
723 sample->ack_time = ack_anticipation_time;
725 if (!tsmf_sample_queue_ack(stream, sample))
727 WLog_ERR(TAG,
"error queuing sample for ack");
735static DWORD WINAPI tsmf_stream_ack_func(LPVOID arg)
738 TSMF_STREAM* stream = (TSMF_STREAM*)arg;
739 UINT error = CHANNEL_RC_OK;
740 DEBUG_TSMF(
"in %" PRIu32
"", stream->stream_id);
741 hdl[0] = stream->stopEvent;
742 hdl[1] = Queue_Event(stream->sample_ack_list);
746 DWORD ev = WaitForMultipleObjects(2, hdl, FALSE, 1000);
748 if (ev == WAIT_FAILED)
750 error = GetLastError();
751 WLog_ERR(TAG,
"WaitForMultipleObjects failed with error %" PRIu32
"!", error);
756 if (stream->decoder->BufferLevel)
757 stream->currentBufferLevel = stream->decoder->BufferLevel(stream->decoder);
761 while ((stream->currentBufferLevel > 0) && !(tsmf_stream_process_ack(stream, TRUE)))
763 DEBUG_TSMF(
"END OF STREAM PROCESSING!");
765 if (stream->decoder && stream->decoder->BufferLevel)
766 stream->currentBufferLevel = stream->decoder->BufferLevel(stream->decoder);
768 stream->currentBufferLevel = 1;
773 tsmf_send_eos_response(stream->presentation->channel_callback, stream->eos_message_id);
776 if (stream->delayed_stop)
778 DEBUG_TSMF(
"Finishing delayed stream stop, now that eos has processed.");
779 tsmf_stream_flush(stream);
781 if (stream->decoder && stream->decoder->Control)
782 stream->decoder->Control(stream->decoder, Control_Stop,
nullptr);
787 if (ev == WAIT_OBJECT_0)
789 DEBUG_TSMF(
"ack: Stream stopped!");
793 if (tsmf_stream_process_ack(stream, TRUE))
802 if (tsmf_stream_process_ack(stream, FALSE))
805 if (stream->currentBufferLevel > stream->minBufferLevel)
809 if (error && stream->rdpcontext)
810 setChannelError(stream->rdpcontext, error,
"tsmf_stream_ack_func reported an error");
812 DEBUG_TSMF(
"out %" PRIu32
"", stream->stream_id);
817static DWORD WINAPI tsmf_stream_playback_func(LPVOID arg)
820 TSMF_SAMPLE* sample =
nullptr;
821 TSMF_STREAM* stream = (TSMF_STREAM*)arg;
822 TSMF_PRESENTATION* presentation = stream->presentation;
823 UINT error = CHANNEL_RC_OK;
825 DEBUG_TSMF(
"in %" PRIu32
"", stream->stream_id);
827 if (stream->major_type == TSMF_MAJOR_TYPE_AUDIO && stream->sample_rate && stream->channels &&
828 stream->bits_per_sample)
832 if (stream->decoder->GetDecodedData)
834 stream->audio = tsmf_load_audio_device(
835 presentation->audio_name && presentation->audio_name[0]
836 ? presentation->audio_name
838 presentation->audio_device && presentation->audio_device[0]
839 ? presentation->audio_device
844 stream->audio->SetFormat(stream->audio, stream->sample_rate, stream->channels,
845 stream->bits_per_sample);
851 hdl[0] = stream->stopEvent;
852 hdl[1] = Queue_Event(stream->sample_list);
856 status = WaitForMultipleObjects(2, hdl, FALSE, 1000);
858 if (status == WAIT_FAILED)
860 error = GetLastError();
861 WLog_ERR(TAG,
"WaitForMultipleObjects failed with error %" PRIu32
"!", error);
865 status = WaitForSingleObject(stream->stopEvent, 0);
867 if (status == WAIT_FAILED)
869 error = GetLastError();
870 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", error);
874 if (status == WAIT_OBJECT_0)
878 if (stream->decoder->BufferLevel)
879 stream->currentBufferLevel = stream->decoder->BufferLevel(stream->decoder);
881 sample = tsmf_stream_pop_sample(stream, 0);
883 if (sample && !tsmf_sample_playback(stream, sample))
885 WLog_ERR(TAG,
"error playing sample");
886 error = ERROR_INTERNAL_ERROR;
890 if (stream->currentBufferLevel > stream->minBufferLevel)
896 stream->audio->Free(stream->audio);
897 stream->audio =
nullptr;
900 if (error && stream->rdpcontext)
901 setChannelError(stream->rdpcontext, error,
"tsmf_stream_playback_func reported an error");
903 DEBUG_TSMF(
"out %" PRIu32
"", stream->stream_id);
908static BOOL tsmf_stream_start(TSMF_STREAM* stream)
910 if (!stream || !stream->presentation || !stream->decoder || !stream->decoder->Control)
914 return stream->decoder->Control(stream->decoder, Control_Restart,
nullptr);
917static BOOL tsmf_stream_stop(TSMF_STREAM* stream)
919 if (!stream || !stream->decoder || !stream->decoder->Control)
928 DEBUG_TSMF(
"Setting up a delayed stop for once the eos has been processed.");
929 stream->delayed_stop = 1;
935 DEBUG_TSMF(
"Stop with no pending eos response, so do it immediately.");
936 tsmf_stream_flush(stream);
937 return stream->decoder->Control(stream->decoder, Control_Stop,
nullptr);
941static BOOL tsmf_stream_pause(TSMF_STREAM* stream)
943 if (!stream || !stream->decoder || !stream->decoder->Control)
946 return stream->decoder->Control(stream->decoder, Control_Pause,
nullptr);
949static BOOL tsmf_stream_restart(TSMF_STREAM* stream)
951 if (!stream || !stream->decoder || !stream->decoder->Control)
955 return stream->decoder->Control(stream->decoder, Control_Restart,
nullptr);
958static BOOL tsmf_stream_change_volume(TSMF_STREAM* stream, UINT32 newVolume, UINT32 muted)
960 if (!stream || !stream->decoder)
963 if (stream->decoder !=
nullptr && stream->decoder->ChangeVolume)
965 return stream->decoder->ChangeVolume(stream->decoder, newVolume, muted);
967 else if (stream->audio !=
nullptr && stream->audio->ChangeVolume)
969 return stream->audio->ChangeVolume(stream->audio, newVolume, muted);
975BOOL tsmf_presentation_volume_changed(TSMF_PRESENTATION* presentation, UINT32 newVolume,
978 TSMF_STREAM* stream =
nullptr;
980 presentation->volume = newVolume;
981 presentation->muted = muted;
982 ArrayList_Lock(presentation->stream_list);
983 size_t count = ArrayList_Count(presentation->stream_list);
985 for (
size_t index = 0; index < count; index++)
987 stream = (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
988 ret &= tsmf_stream_change_volume(stream, newVolume, muted);
991 ArrayList_Unlock(presentation->stream_list);
995BOOL tsmf_presentation_paused(TSMF_PRESENTATION* presentation)
997 TSMF_STREAM* stream =
nullptr;
999 ArrayList_Lock(presentation->stream_list);
1000 size_t count = ArrayList_Count(presentation->stream_list);
1002 for (
size_t index = 0; index < count; index++)
1004 stream = (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
1005 ret &= tsmf_stream_pause(stream);
1008 ArrayList_Unlock(presentation->stream_list);
1012BOOL tsmf_presentation_restarted(TSMF_PRESENTATION* presentation)
1014 TSMF_STREAM* stream =
nullptr;
1016 ArrayList_Lock(presentation->stream_list);
1017 size_t count = ArrayList_Count(presentation->stream_list);
1019 for (
size_t index = 0; index < count; index++)
1021 stream = (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
1022 ret &= tsmf_stream_restart(stream);
1025 ArrayList_Unlock(presentation->stream_list);
1029BOOL tsmf_presentation_start(TSMF_PRESENTATION* presentation)
1031 TSMF_STREAM* stream =
nullptr;
1033 ArrayList_Lock(presentation->stream_list);
1034 size_t count = ArrayList_Count(presentation->stream_list);
1036 for (
size_t index = 0; index < count; index++)
1038 stream = (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
1039 ret &= tsmf_stream_start(stream);
1042 ArrayList_Unlock(presentation->stream_list);
1051UINT tsmf_presentation_sync(TSMF_PRESENTATION* presentation)
1054 ArrayList_Lock(presentation->stream_list);
1055 size_t count = ArrayList_Count(presentation->stream_list);
1057 for (
size_t index = 0; index < count; index++)
1059 TSMF_STREAM* stream = (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
1061 if (WaitForSingleObject(stream->ready, 500) == WAIT_FAILED)
1063 error = GetLastError();
1064 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", error);
1069 ArrayList_Unlock(presentation->stream_list);
1070 return CHANNEL_RC_OK;
1073BOOL tsmf_presentation_stop(TSMF_PRESENTATION* presentation)
1075 TSMF_STREAM* stream =
nullptr;
1077 ArrayList_Lock(presentation->stream_list);
1078 size_t count = ArrayList_Count(presentation->stream_list);
1080 for (
size_t index = 0; index < count; index++)
1082 stream = (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
1083 ret &= tsmf_stream_stop(stream);
1086 ArrayList_Unlock(presentation->stream_list);
1087 presentation->audio_start_time = 0;
1088 presentation->audio_end_time = 0;
1092BOOL tsmf_presentation_set_geometry_info(TSMF_PRESENTATION* presentation, UINT32 x, UINT32 y,
1093 UINT32 width, UINT32 height, UINT32 num_rects,
1096 TSMF_STREAM* stream =
nullptr;
1101 if (!width || !height)
1104 ArrayList_Lock(presentation->stream_list);
1110 presentation->rect.left = x;
1111 presentation->rect.top = y;
1112 presentation->rect.width = width;
1113 presentation->rect.height = height;
1114 void* tmp_rects = realloc(presentation->rects,
sizeof(
RECTANGLE_32) * num_rects);
1116 if (!tmp_rects && num_rects)
1118 ArrayList_Unlock(presentation->stream_list);
1122 presentation->nr_rects = num_rects;
1124 if (presentation->rects)
1125 CopyMemory(presentation->rects, rects,
sizeof(
RECTANGLE_32) * num_rects);
1127 size_t count = ArrayList_Count(presentation->stream_list);
1129 for (
size_t index = 0; index < count; index++)
1131 stream = (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
1133 if (!stream->decoder)
1136 if (stream->decoder->UpdateRenderingArea)
1138 ret = stream->decoder->UpdateRenderingArea(stream->decoder, x, y, width, height,
1143 ArrayList_Unlock(presentation->stream_list);
1147void tsmf_presentation_set_audio_device(TSMF_PRESENTATION* presentation,
const char* name,
1150 presentation->audio_name = name;
1151 presentation->audio_device = device;
1154BOOL tsmf_stream_flush(TSMF_STREAM* stream)
1161 ret = stream->audio->Flush(stream->audio);
1164 stream->eos_message_id = 0;
1165 stream->delayed_stop = 0;
1166 stream->last_end_time = 0;
1167 stream->next_start_time = 0;
1168 Queue_Clear(stream->sample_ack_list);
1170 if (stream->major_type == TSMF_MAJOR_TYPE_AUDIO)
1172 stream->presentation->audio_start_time = 0;
1173 stream->presentation->audio_end_time = 0;
1179void s_tsmf_presentation_free(
void* obj)
1181 TSMF_PRESENTATION* presentation = (TSMF_PRESENTATION*)obj;
1185 tsmf_presentation_stop(presentation);
1186 ArrayList_Clear(presentation->stream_list);
1187 ArrayList_Free(presentation->stream_list);
1188 free(presentation->rects);
1189 ZeroMemory(presentation,
sizeof(TSMF_PRESENTATION));
1194void tsmf_presentation_free(TSMF_PRESENTATION* presentation)
1196 ArrayList_Remove(presentation_list, presentation);
1199TSMF_STREAM* tsmf_stream_new(TSMF_PRESENTATION* presentation, UINT32 stream_id,
1200 rdpContext* rdpcontext)
1203 TSMF_STREAM* stream =
nullptr;
1204 stream = tsmf_stream_find_by_id(presentation, stream_id);
1208 WLog_ERR(TAG,
"duplicated stream id %" PRIu32
"!", stream_id);
1212 stream = (TSMF_STREAM*)calloc(1,
sizeof(TSMF_STREAM));
1216 WLog_ERR(TAG,
"Calloc failed");
1220 stream->minBufferLevel = VIDEO_MIN_BUFFER_LEVEL;
1221 stream->maxBufferLevel = VIDEO_MAX_BUFFER_LEVEL;
1222 stream->currentBufferLevel = 1;
1223 stream->seeking = FALSE;
1225 stream->eos_message_id = 0;
1226 stream->stream_id = stream_id;
1227 stream->presentation = presentation;
1228 stream->stopEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr);
1230 if (!stream->stopEvent)
1231 goto error_stopEvent;
1233 stream->ready = CreateEvent(
nullptr, TRUE, TRUE,
nullptr);
1238 stream->sample_list = Queue_New(TRUE, -1, -1);
1240 if (!stream->sample_list)
1241 goto error_sample_list;
1243 obj = Queue_Object(stream->sample_list);
1245 goto error_sample_ack_list;
1246 obj->fnObjectFree = tsmf_sample_free;
1248 stream->sample_ack_list = Queue_New(TRUE, -1, -1);
1250 if (!stream->sample_ack_list)
1251 goto error_sample_ack_list;
1253 obj = Queue_Object(stream->sample_ack_list);
1255 goto error_play_thread;
1256 obj->fnObjectFree = tsmf_sample_free;
1258 stream->play_thread =
1259 CreateThread(
nullptr, 0, tsmf_stream_playback_func, stream, CREATE_SUSPENDED,
nullptr);
1261 if (!stream->play_thread)
1262 goto error_play_thread;
1264 stream->ack_thread =
1265 CreateThread(
nullptr, 0, tsmf_stream_ack_func, stream, CREATE_SUSPENDED,
nullptr);
1267 if (!stream->ack_thread)
1268 goto error_ack_thread;
1270 if (!ArrayList_Append(presentation->stream_list, stream))
1273 stream->rdpcontext = rdpcontext;
1276 (void)SetEvent(stream->stopEvent);
1278 if (WaitForSingleObject(stream->ack_thread, INFINITE) == WAIT_FAILED)
1279 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", GetLastError());
1282 (void)SetEvent(stream->stopEvent);
1284 if (WaitForSingleObject(stream->play_thread, INFINITE) == WAIT_FAILED)
1285 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", GetLastError());
1288 Queue_Free(stream->sample_ack_list);
1289error_sample_ack_list:
1290 Queue_Free(stream->sample_list);
1292 (void)CloseHandle(stream->ready);
1294 (void)CloseHandle(stream->stopEvent);
1300void tsmf_stream_start_threads(TSMF_STREAM* stream)
1302 ResumeThread(stream->play_thread);
1303 ResumeThread(stream->ack_thread);
1306TSMF_STREAM* tsmf_stream_find_by_id(TSMF_PRESENTATION* presentation, UINT32 stream_id)
1309 TSMF_STREAM* stream =
nullptr;
1310 ArrayList_Lock(presentation->stream_list);
1311 size_t count = ArrayList_Count(presentation->stream_list);
1313 for (
size_t index = 0; index < count; index++)
1315 stream = (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
1317 if (stream->stream_id == stream_id)
1324 ArrayList_Unlock(presentation->stream_list);
1325 return (found) ? stream :
nullptr;
1328static void tsmf_stream_resync(
void* arg)
1330 TSMF_STREAM* stream = arg;
1331 (void)ResetEvent(stream->ready);
1334BOOL tsmf_stream_set_format(TSMF_STREAM* stream,
const char* name,
wStream* s)
1339 if (stream->decoder)
1341 WLog_ERR(TAG,
"duplicated call");
1345 if (!tsmf_codec_parse_media_type(&mediatype, s))
1347 WLog_ERR(TAG,
"unable to parse media type");
1351 if (mediatype.MajorType == TSMF_MAJOR_TYPE_VIDEO)
1353 DEBUG_TSMF(
"video width %" PRIu32
" height %" PRIu32
" bit_rate %" PRIu32
1354 " frame_rate %f codec_data %" PRIu32
"",
1355 mediatype.Width, mediatype.Height, mediatype.BitRate,
1356 (
double)mediatype.SamplesPerSecond.Numerator /
1357 (
double)mediatype.SamplesPerSecond.Denominator,
1358 mediatype.ExtraDataSize);
1359 stream->minBufferLevel = VIDEO_MIN_BUFFER_LEVEL;
1360 stream->maxBufferLevel = VIDEO_MAX_BUFFER_LEVEL;
1362 else if (mediatype.MajorType == TSMF_MAJOR_TYPE_AUDIO)
1364 DEBUG_TSMF(
"audio channel %" PRIu32
" sample_rate %" PRIu32
" bits_per_sample %" PRIu32
1365 " codec_data %" PRIu32
"",
1366 mediatype.Channels, mediatype.SamplesPerSecond.Numerator,
1367 mediatype.BitsPerSample, mediatype.ExtraDataSize);
1368 stream->sample_rate = mediatype.SamplesPerSecond.Numerator;
1369 stream->channels = mediatype.Channels;
1370 stream->bits_per_sample = mediatype.BitsPerSample;
1372 if (stream->bits_per_sample == 0)
1373 stream->bits_per_sample = 16;
1375 stream->minBufferLevel = AUDIO_MIN_BUFFER_LEVEL;
1376 stream->maxBufferLevel = AUDIO_MAX_BUFFER_LEVEL;
1379 stream->major_type = mediatype.MajorType;
1380 stream->width = mediatype.Width;
1381 stream->height = mediatype.Height;
1382 stream->decoder = tsmf_load_decoder(name, &mediatype);
1383 ret &= tsmf_stream_change_volume(stream, stream->presentation->volume,
1384 stream->presentation->muted);
1386 if (!stream->decoder)
1389 if (stream->decoder->SetAckFunc)
1390 ret &= stream->decoder->SetAckFunc(stream->decoder, tsmf_stream_process_ack, stream);
1392 if (stream->decoder->SetSyncFunc)
1393 ret &= stream->decoder->SetSyncFunc(stream->decoder, tsmf_stream_resync, stream);
1398void tsmf_stream_end(TSMF_STREAM* stream, UINT32 message_id)
1404 stream->eos_message_id = message_id;
1407void s_tsmf_stream_free(
void* obj)
1409 TSMF_STREAM* stream = (TSMF_STREAM*)obj;
1414 tsmf_stream_stop(stream);
1415 (void)SetEvent(stream->stopEvent);
1417 if (stream->play_thread)
1419 if (WaitForSingleObject(stream->play_thread, INFINITE) == WAIT_FAILED)
1421 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", GetLastError());
1425 (void)CloseHandle(stream->play_thread);
1426 stream->play_thread =
nullptr;
1429 if (stream->ack_thread)
1431 if (WaitForSingleObject(stream->ack_thread, INFINITE) == WAIT_FAILED)
1433 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", GetLastError());
1437 (void)CloseHandle(stream->ack_thread);
1438 stream->ack_thread =
nullptr;
1441 Queue_Free(stream->sample_list);
1442 Queue_Free(stream->sample_ack_list);
1444 if (stream->decoder && stream->decoder->Free)
1446 stream->decoder->Free(stream->decoder);
1447 stream->decoder =
nullptr;
1450 (void)CloseHandle(stream->stopEvent);
1451 (void)CloseHandle(stream->ready);
1452 ZeroMemory(stream,
sizeof(TSMF_STREAM));
1456void tsmf_stream_free(TSMF_STREAM* stream)
1458 TSMF_PRESENTATION* presentation = stream->presentation;
1459 ArrayList_Remove(presentation->stream_list, stream);
1462BOOL tsmf_stream_push_sample(TSMF_STREAM* stream, UINT32 sample_id, UINT64 start_time,
1463 UINT64 end_time, UINT64 duration, UINT32 extensions, UINT32 data_size,
1466 WINPR_ASSERT(stream);
1468 (void)SetEvent(stream->ready);
1473 TSMF_SAMPLE* sample = (TSMF_SAMPLE*)calloc(1,
sizeof(TSMF_SAMPLE));
1477 WLog_ERR(TAG,
"calloc sample failed!");
1481 sample->sample_id = sample_id;
1482 sample->start_time = start_time;
1483 sample->end_time = end_time;
1484 sample->duration = duration;
1485 sample->extensions = extensions;
1487 if ((sample->extensions & 0x00000080) || (sample->extensions & 0x00000040))
1488 sample->invalidTimestamps = TRUE;
1490 sample->invalidTimestamps = FALSE;
1492 sample->data_size = data_size;
1493 sample->data = calloc(1, data_size + TSMF_BUFFER_PADDING_SIZE);
1498 CopyMemory(sample->data, data, data_size);
1499 if (!Queue_Enqueue(stream->sample_list, sample))
1514static void tsmf_signal_handler(
int s)
1517 ArrayList_Free(presentation_list);
1521 (void)signal(s, SIG_DFL);
1524 else if (s == SIGUSR1)
1526 (void)signal(s, SIG_DFL);
1532BOOL tsmf_media_init(
void)
1536 struct sigaction sigtrap;
1537 sigtrap.sa_handler = tsmf_signal_handler;
1538 sigemptyset(&sigtrap.sa_mask);
1539 sigtrap.sa_flags = 0;
1540 sigaction(SIGINT, &sigtrap, 0);
1541 sigaction(SIGUSR1, &sigtrap, 0);
1544 if (!presentation_list)
1546 presentation_list = ArrayList_New(TRUE);
1548 if (!presentation_list)
1551 obj = ArrayList_Object(presentation_list);
1554 obj->fnObjectFree = s_tsmf_presentation_free;
This struct contains function pointer to initialize/free objects.