FreeRDP
Loading...
Searching...
No Matches
tsmf_media.c
1
22
23#include <freerdp/config.h>
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <signal.h>
29
30#ifndef _WIN32
31#include <sys/time.h>
32#endif
33
34#include <winpr/crt.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>
40
41#include <freerdp/freerdp.h>
42#include <freerdp/client/tsmf.h>
43
44#include "tsmf_constants.h"
45#include "tsmf_types.h"
46#include "tsmf_decoder.h"
47#include "tsmf_audio.h"
48#include "tsmf_main.h"
49#include "tsmf_codec.h"
50#include "tsmf_media.h"
51
52#define AUDIO_TOLERANCE 10000000LL
53
54/* 1 second = 10,000,000 100ns units*/
55#define VIDEO_ADJUST_MAX (10ULL * 1000ULL * 1000ULL)
56
57#define MAX_ACK_TIME 666667
58
59#define AUDIO_MIN_BUFFER_LEVEL 3
60#define AUDIO_MAX_BUFFER_LEVEL 6
61
62#define VIDEO_MIN_BUFFER_LEVEL 10
63#define VIDEO_MAX_BUFFER_LEVEL 30
64
65struct S_TSMF_PRESENTATION
66{
67 BYTE presentation_id[GUID_SIZE];
68
69 const char* audio_name;
70 const char* audio_device;
71
72 IWTSVirtualChannelCallback* channel_callback;
73
74 UINT64 audio_start_time;
75 UINT64 audio_end_time;
76
77 UINT32 volume;
78 UINT32 muted;
79
80 wArrayList* stream_list;
81
82 RECTANGLE_32 rect;
83
84 UINT32 nr_rects;
85 RECTANGLE_32* rects;
86};
87
88struct S_TSMF_STREAM
89{
90 UINT32 stream_id;
91
92 TSMF_PRESENTATION* presentation;
93
94 ITSMFDecoder* decoder;
95
96 int major_type;
97 int eos;
98 UINT32 eos_message_id;
99 int delayed_stop;
100 UINT32 width;
101 UINT32 height;
102
103 ITSMFAudioDevice* audio;
104 UINT32 sample_rate;
105 UINT32 channels;
106 UINT32 bits_per_sample;
107
108 /* The start time of last played sample */
109 UINT64 last_start_time;
110 /* The end_time of last played sample */
111 UINT64 last_end_time;
112 /* Next sample should not start before this system time. */
113 UINT64 next_start_time;
114
115 UINT32 minBufferLevel;
116 UINT32 maxBufferLevel;
117 UINT32 currentBufferLevel;
118
119 HANDLE play_thread;
120 HANDLE ack_thread;
121 HANDLE stopEvent;
122 HANDLE ready;
123
124 wQueue* sample_list;
125 wQueue* sample_ack_list;
126 rdpContext* rdpcontext;
127
128 BOOL seeking;
129};
130
131struct S_TSMF_SAMPLE
132{
133 UINT32 sample_id;
134 UINT64 start_time;
135 UINT64 end_time;
136 UINT64 duration;
137 UINT32 extensions;
138 UINT32 data_size;
139 BYTE* data;
140 UINT32 decoded_size;
141 UINT32 pixfmt;
142
143 BOOL invalidTimestamps;
144
145 UINT64 ack_time;
146};
147
148static wArrayList* presentation_list = nullptr;
149static int TERMINATING = 0;
150
151static void s_tsmf_presentation_free(void* obj);
152static void s_tsmf_stream_free(void* obj);
153
154static UINT64 get_current_time(void)
155{
156 struct timeval tp;
157 gettimeofday(&tp, 0);
158 return ((UINT64)tp.tv_sec) * 10000000LL + ((UINT64)tp.tv_usec) * 10LL;
159}
160
161static TSMF_SAMPLE* tsmf_stream_pop_sample(TSMF_STREAM* stream, int sync)
162{
163 TSMF_SAMPLE* sample = nullptr;
164 BOOL pending = FALSE;
165
166 if (!stream)
167 return nullptr;
168
169 TSMF_PRESENTATION* presentation = stream->presentation;
170
171 if (Queue_Count(stream->sample_list) < 1)
172 return nullptr;
173
174 if (sync)
175 {
176 if (stream->decoder)
177 {
178 if (stream->decoder->GetDecodedData)
179 {
180 if (stream->major_type == TSMF_MAJOR_TYPE_AUDIO)
181 {
182 /* Check if some other stream has earlier sample that needs to be played first
183 */
184 /* Start time is more reliable than end time as some stream types seem to have
185 * incorrect end times from the server
186 */
187 if (stream->last_start_time > AUDIO_TOLERANCE)
188 {
189 ArrayList_Lock(presentation->stream_list);
190 const size_t count = ArrayList_Count(presentation->stream_list);
191
192 for (size_t index = 0; index < count; index++)
193 {
194 TSMF_STREAM* s =
195 (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
196
197 /* Start time is more reliable than end time as some stream types seem
198 * to have incorrect end times from the server
199 */
200 if (s != stream && !s->eos && s->last_start_time &&
201 s->last_start_time < stream->last_start_time - AUDIO_TOLERANCE)
202 {
203 DEBUG_TSMF("Pending due to audio tolerance");
204 pending = TRUE;
205 break;
206 }
207 }
208
209 ArrayList_Unlock(presentation->stream_list);
210 }
211 }
212 else
213 {
214 /* Start time is more reliable than end time as some stream types seem to have
215 * incorrect end times from the server
216 */
217 if (stream->last_start_time > presentation->audio_start_time)
218 {
219 DEBUG_TSMF("Pending due to stream start time > audio start time");
220 pending = TRUE;
221 }
222 }
223 }
224 }
225 }
226
227 if (pending)
228 return nullptr;
229
230 sample = (TSMF_SAMPLE*)Queue_Dequeue(stream->sample_list);
231
232 /* Only update stream last end time if the sample end time is valid and greater than the current
233 * stream end time */
234 if (sample && (sample->end_time > stream->last_end_time) && (!sample->invalidTimestamps))
235 stream->last_end_time = sample->end_time;
236
237 /* Only update stream last start time if the sample start time is valid and greater than the
238 * current stream start time */
239 if (sample && (sample->start_time > stream->last_start_time) && (!sample->invalidTimestamps))
240 stream->last_start_time = sample->start_time;
241
242 return sample;
243}
244
245static void tsmf_sample_free(void* arg)
246{
247 TSMF_SAMPLE* sample = arg;
248
249 if (!sample)
250 return;
251
252 free(sample->data);
253 free(sample);
254}
255
256static BOOL tsmf_sample_ack(TSMF_STREAM* stream, const TSMF_SAMPLE* sample)
257{
258 if (!sample || !stream)
259 return FALSE;
260
261 return tsmf_playback_ack(stream->presentation->channel_callback, sample->sample_id,
262 sample->duration, sample->data_size);
263}
264
265static BOOL tsmf_sample_queue_ack(TSMF_STREAM* stream, TSMF_SAMPLE* sample)
266{
267 if (!sample)
268 return FALSE;
269
270 if (!stream)
271 return FALSE;
272
273 return Queue_Enqueue(stream->sample_ack_list, sample);
274}
275
276/* Returns TRUE if no more samples are currently available
277 * Returns FALSE otherwise
278 */
279static BOOL tsmf_stream_process_ack(void* arg, BOOL force)
280{
281 TSMF_STREAM* stream = arg;
282 TSMF_SAMPLE* sample = nullptr;
283 UINT64 ack_time = 0;
284 BOOL rc = FALSE;
285
286 if (!stream)
287 return TRUE;
288
289 Queue_Lock(stream->sample_ack_list);
290 sample = (TSMF_SAMPLE*)Queue_Peek(stream->sample_ack_list);
291
292 if (!sample)
293 {
294 rc = TRUE;
295 goto finally;
296 }
297
298 if (!force)
299 {
300 /* Do some min/max ack limiting if we have access to Buffer level information */
301 if (stream->decoder && stream->decoder->BufferLevel)
302 {
303 /* Try to keep buffer level below max by withholding acks */
304 if (stream->currentBufferLevel > stream->maxBufferLevel)
305 goto finally;
306 /* Try to keep buffer level above min by pushing acks through quickly */
307 else if (stream->currentBufferLevel < stream->minBufferLevel)
308 goto dequeue;
309 }
310
311 /* Time based acks only */
312 ack_time = get_current_time();
313
314 if (sample->ack_time > ack_time)
315 goto finally;
316 }
317
318dequeue:
319 sample = Queue_Dequeue(stream->sample_ack_list);
320
321 if (sample)
322 {
323 tsmf_sample_ack(stream, sample);
324 tsmf_sample_free(sample);
325 }
326
327finally:
328 Queue_Unlock(stream->sample_ack_list);
329 return rc;
330}
331
332const IWTSVirtualChannelCallback*
333tsmf_presentation_get_callback(const TSMF_PRESENTATION* presentation)
334{
335 if (!presentation)
336 return nullptr;
337 return presentation->channel_callback;
338}
339
340TSMF_PRESENTATION* tsmf_presentation_new(const BYTE* guid,
341 IWTSVirtualChannelCallback* pChannelCallback)
342{
343 wObject* obj = nullptr;
344 TSMF_PRESENTATION* presentation = nullptr;
345
346 if (!guid || !pChannelCallback)
347 return nullptr;
348
349 presentation = (TSMF_PRESENTATION*)calloc(1, sizeof(TSMF_PRESENTATION));
350
351 if (!presentation)
352 {
353 WLog_ERR(TAG, "calloc failed");
354 return nullptr;
355 }
356
357 CopyMemory(presentation->presentation_id, guid, GUID_SIZE);
358 presentation->channel_callback = pChannelCallback;
359 presentation->volume = 5000; /* 50% */
360 presentation->muted = 0;
361
362 if (!(presentation->stream_list = ArrayList_New(TRUE)))
363 goto error_stream_list;
364
365 obj = ArrayList_Object(presentation->stream_list);
366 if (!obj)
367 goto error_add;
368 obj->fnObjectFree = s_tsmf_stream_free;
369
370 if (!ArrayList_Append(presentation_list, presentation))
371 goto error_add;
372
373 return presentation;
374error_add:
375 ArrayList_Free(presentation->stream_list);
376error_stream_list:
377 free(presentation);
378 return nullptr;
379}
380
381static char* guid_to_string(const BYTE* guid, char* str, size_t len)
382{
383 if (!guid || !str)
384 return nullptr;
385
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]);
388
389 return str;
390}
391
392TSMF_PRESENTATION* tsmf_presentation_find_by_id(const BYTE* guid)
393{
394 BOOL found = FALSE;
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);
399
400 for (size_t index = 0; index < count; index++)
401 {
402 presentation = (TSMF_PRESENTATION*)ArrayList_GetItem(presentation_list, index);
403
404 if (memcmp(presentation->presentation_id, guid, GUID_SIZE) == 0)
405 {
406 found = TRUE;
407 break;
408 }
409 }
410
411 ArrayList_Unlock(presentation_list);
412
413 if (!found)
414 WLog_WARN(TAG, "presentation id %s not found",
415 guid_to_string(guid, guid_str, sizeof(guid_str)));
416
417 return (found) ? presentation : nullptr;
418}
419
420static BOOL tsmf_sample_playback_video(TSMF_STREAM* stream, TSMF_SAMPLE* sample)
421{
422 BOOL rc = FALSE;
423 WINPR_ASSERT(sample);
424 WINPR_ASSERT(stream);
425
426 TSMF_PRESENTATION* presentation = stream->presentation;
427 TSMF_CHANNEL_CALLBACK* callback =
428 (TSMF_CHANNEL_CALLBACK*)stream->presentation->channel_callback;
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);
432
433 TSMF_VIDEO_FRAME_EVENT event = WINPR_C_ARRAY_INIT;
434 if (sample->data)
435 {
436 const UINT64 t = get_current_time();
437
438 /* Start time is more reliable than end time as some stream types seem to have incorrect
439 * end times from the server
440 */
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))))
444 {
445 size_t delay = (stream->next_start_time - t) / 10;
446 while (delay > 0)
447 {
448 const UINT32 d = (delay > UINT32_MAX) ? UINT32_MAX : (UINT32)delay;
449 USleep(d);
450 delay -= d;
451 }
452 }
453
454 if (stream->width > INT16_MAX)
455 goto fail;
456 if (stream->height > INT16_MAX)
457 goto fail;
458 if (presentation->rect.left > INT16_MAX)
459 goto fail;
460 if (presentation->rect.top > INT16_MAX)
461 goto fail;
462 if (presentation->rect.width > INT16_MAX)
463 goto fail;
464 if (presentation->rect.height > INT16_MAX)
465 goto fail;
466 if (presentation->nr_rects > UINT16_MAX)
467 goto fail;
468
469 stream->next_start_time = t + sample->duration - 50000;
470
471 TSMF_VIDEO_FRAME_EVENT event = WINPR_C_ARRAY_INIT;
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;
481
482 if (presentation->nr_rects > 0)
483 {
484 event.numVisibleRects = (UINT16)presentation->nr_rects;
485 event.visibleRects = (RECTANGLE_16*)calloc(event.numVisibleRects, sizeof(RECTANGLE_16));
486
487 if (!event.visibleRects)
488 {
489 WLog_ERR(TAG, "can't allocate memory for copy rectangles");
490 goto fail;
491 }
492
493 for (size_t x = 0; x < presentation->nr_rects; x++)
494 {
495 const RECTANGLE_32* cur = &presentation->rects[x];
496 RECTANGLE_16* dst = &event.visibleRects[x];
497 if ((cur->left > UINT16_MAX) || (cur->top > UINT16_MAX) ||
498 (cur->width > UINT16_MAX) || (cur->height > UINT16_MAX))
499 goto fail;
500
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;
505 }
506 memcpy(event.visibleRects, presentation->rects,
507 presentation->nr_rects * sizeof(RECTANGLE_16));
508 presentation->nr_rects = 0;
509 }
510
511 /* The frame data ownership is passed to the event object, and is freed after the event is
512 * processed. */
513 sample->data = nullptr;
514 sample->decoded_size = 0;
515
516 if (tsmf->FrameEvent)
517 {
518 if (tsmf->FrameEvent(tsmf, &event) < 0)
519 goto fail;
520 }
521 }
522 rc = TRUE;
523fail:
524 free(event.frameData);
525 free(event.visibleRects);
526 ArrayList_Unlock(presentation->stream_list);
527
528 return rc;
529}
530
531static BOOL tsmf_sample_playback_audio(TSMF_STREAM* stream, TSMF_SAMPLE* sample)
532{
533 UINT64 latency = 0;
534 BOOL ret = 0;
535 DEBUG_TSMF("MessageId %" PRIu32 " EndTime %" PRIu64 " consumed.", sample->sample_id,
536 sample->end_time);
537
538 if (stream->audio && sample->data)
539 {
540 ret = stream->audio->Play(stream->audio, sample->data, sample->decoded_size);
541 free(sample->data);
542 sample->data = nullptr;
543 sample->decoded_size = 0;
544
545 if (stream->audio->GetLatency)
546 latency = stream->audio->GetLatency(stream->audio);
547 }
548 else
549 {
550 ret = TRUE;
551 latency = 0;
552 }
553
554 sample->ack_time = latency + get_current_time();
555
556 /* Only update stream times if the sample timestamps are valid */
557 if (!sample->invalidTimestamps)
558 {
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;
563 }
564
565 return ret;
566}
567
568static BOOL tsmf_sample_playback(TSMF_STREAM* stream, TSMF_SAMPLE* sample)
569{
570 BOOL ret = FALSE;
571 UINT32 width = 0;
572 UINT32 height = 0;
573 UINT32 pixfmt = 0;
574
575 if (stream->decoder)
576 {
577 if (stream->decoder->DecodeEx)
578 {
579 /* Try to "sync" video buffers to audio buffers by looking at the running time for each
580 * stream The difference between the two running times causes an offset between audio
581 * and video actual render times. So, we try to adjust timestamps on the video buffer to
582 * match those on the audio buffer.
583 */
584 if (stream->major_type == TSMF_MAJOR_TYPE_VIDEO)
585 {
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);
590
591 for (size_t index = 0; index < count; index++)
592 {
593 UINT64 time_diff = 0;
594 temp_stream = (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
595
596 if (temp_stream->major_type == TSMF_MAJOR_TYPE_AUDIO)
597 {
598 UINT64 video_time = stream->decoder->GetRunningTime(stream->decoder);
599 UINT64 audio_time =
600 temp_stream->decoder->GetRunningTime(temp_stream->decoder);
601 UINT64 max_adjust = VIDEO_ADJUST_MAX;
602
603 if (video_time < audio_time)
604 max_adjust = -VIDEO_ADJUST_MAX;
605
606 if (video_time > audio_time)
607 time_diff = video_time - audio_time;
608 else
609 time_diff = audio_time - video_time;
610
611 time_diff = time_diff < VIDEO_ADJUST_MAX ? time_diff : max_adjust;
612 sample->start_time += time_diff;
613 sample->end_time += time_diff;
614 break;
615 }
616 }
617
618 ArrayList_Unlock(presentation->stream_list);
619 }
620
621 ret = stream->decoder->DecodeEx(stream->decoder, sample->data, sample->data_size,
622 sample->extensions, sample->start_time,
623 sample->end_time, sample->duration);
624 }
625 else
626 {
627 ret = stream->decoder->Decode(stream->decoder, sample->data, sample->data_size,
628 sample->extensions);
629 }
630 }
631
632 if (!ret)
633 {
634 WLog_ERR(TAG, "decode error, queue ack anyways");
635
636 if (!tsmf_sample_queue_ack(stream, sample))
637 {
638 WLog_ERR(TAG, "error queuing sample for ack");
639 return FALSE;
640 }
641
642 return TRUE;
643 }
644
645 free(sample->data);
646 sample->data = nullptr;
647
648 if (stream->major_type == TSMF_MAJOR_TYPE_VIDEO)
649 {
650 if (stream->decoder->GetDecodedFormat)
651 {
652 pixfmt = stream->decoder->GetDecodedFormat(stream->decoder);
653
654 if (pixfmt == ((UINT32)-1))
655 {
656 WLog_ERR(TAG, "unable to decode video format");
657
658 if (!tsmf_sample_queue_ack(stream, sample))
659 {
660 WLog_ERR(TAG, "error queuing sample for ack");
661 }
662
663 return FALSE;
664 }
665
666 sample->pixfmt = pixfmt;
667 }
668
669 if (stream->decoder->GetDecodedDimension)
670 {
671 ret = stream->decoder->GetDecodedDimension(stream->decoder, &width, &height);
672
673 if (ret && (width != stream->width || height != stream->height))
674 {
675 DEBUG_TSMF("video dimension changed to %" PRIu32 " x %" PRIu32 "", width, height);
676 stream->width = width;
677 stream->height = height;
678 }
679 }
680 }
681
682 if (stream->decoder->GetDecodedData)
683 {
684 sample->data = stream->decoder->GetDecodedData(stream->decoder, &sample->decoded_size);
685
686 switch (stream->major_type)
687 {
688 case TSMF_MAJOR_TYPE_VIDEO:
689 ret = tsmf_sample_playback_video(stream, sample) &&
690 tsmf_sample_queue_ack(stream, sample);
691 break;
692
693 case TSMF_MAJOR_TYPE_AUDIO:
694 ret = tsmf_sample_playback_audio(stream, sample) &&
695 tsmf_sample_queue_ack(stream, sample);
696 break;
697 default:
698 break;
699 }
700 }
701 else
702 {
703 UINT64 ack_anticipation_time = get_current_time();
704
705 ack_anticipation_time +=
706 (sample->duration / 2 < MAX_ACK_TIME) ? sample->duration / 2 : MAX_ACK_TIME;
707
708 switch (stream->major_type)
709 {
710 case TSMF_MAJOR_TYPE_VIDEO:
711 {
712 break;
713 }
714
715 case TSMF_MAJOR_TYPE_AUDIO:
716 {
717 break;
718 }
719 default:
720 break;
721 }
722
723 sample->ack_time = ack_anticipation_time;
724
725 if (!tsmf_sample_queue_ack(stream, sample))
726 {
727 WLog_ERR(TAG, "error queuing sample for ack");
728 ret = FALSE;
729 }
730 }
731
732 return ret;
733}
734
735static DWORD WINAPI tsmf_stream_ack_func(LPVOID arg)
736{
737 HANDLE hdl[2];
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);
743
744 while (1)
745 {
746 DWORD ev = WaitForMultipleObjects(2, hdl, FALSE, 1000);
747
748 if (ev == WAIT_FAILED)
749 {
750 error = GetLastError();
751 WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "!", error);
752 break;
753 }
754
755 if (stream->decoder)
756 if (stream->decoder->BufferLevel)
757 stream->currentBufferLevel = stream->decoder->BufferLevel(stream->decoder);
758
759 if (stream->eos)
760 {
761 while ((stream->currentBufferLevel > 0) && !(tsmf_stream_process_ack(stream, TRUE)))
762 {
763 DEBUG_TSMF("END OF STREAM PROCESSING!");
764
765 if (stream->decoder && stream->decoder->BufferLevel)
766 stream->currentBufferLevel = stream->decoder->BufferLevel(stream->decoder);
767 else
768 stream->currentBufferLevel = 1;
769
770 USleep(1000);
771 }
772
773 tsmf_send_eos_response(stream->presentation->channel_callback, stream->eos_message_id);
774 stream->eos = 0;
775
776 if (stream->delayed_stop)
777 {
778 DEBUG_TSMF("Finishing delayed stream stop, now that eos has processed.");
779 tsmf_stream_flush(stream);
780
781 if (stream->decoder && stream->decoder->Control)
782 stream->decoder->Control(stream->decoder, Control_Stop, nullptr);
783 }
784 }
785
786 /* Stream stopped force all of the acks to happen */
787 if (ev == WAIT_OBJECT_0)
788 {
789 DEBUG_TSMF("ack: Stream stopped!");
790
791 while (1)
792 {
793 if (tsmf_stream_process_ack(stream, TRUE))
794 break;
795
796 USleep(1000);
797 }
798
799 break;
800 }
801
802 if (tsmf_stream_process_ack(stream, FALSE))
803 continue;
804
805 if (stream->currentBufferLevel > stream->minBufferLevel)
806 USleep(1000);
807 }
808
809 if (error && stream->rdpcontext)
810 setChannelError(stream->rdpcontext, error, "tsmf_stream_ack_func reported an error");
811
812 DEBUG_TSMF("out %" PRIu32 "", stream->stream_id);
813 ExitThread(error);
814 return error;
815}
816
817static DWORD WINAPI tsmf_stream_playback_func(LPVOID arg)
818{
819 HANDLE hdl[2];
820 TSMF_SAMPLE* sample = nullptr;
821 TSMF_STREAM* stream = (TSMF_STREAM*)arg;
822 TSMF_PRESENTATION* presentation = stream->presentation;
823 UINT error = CHANNEL_RC_OK;
824 DWORD status = 0;
825 DEBUG_TSMF("in %" PRIu32 "", stream->stream_id);
826
827 if (stream->major_type == TSMF_MAJOR_TYPE_AUDIO && stream->sample_rate && stream->channels &&
828 stream->bits_per_sample)
829 {
830 if (stream->decoder)
831 {
832 if (stream->decoder->GetDecodedData)
833 {
834 stream->audio = tsmf_load_audio_device(
835 presentation->audio_name && presentation->audio_name[0]
836 ? presentation->audio_name
837 : nullptr,
838 presentation->audio_device && presentation->audio_device[0]
839 ? presentation->audio_device
840 : nullptr);
841
842 if (stream->audio)
843 {
844 stream->audio->SetFormat(stream->audio, stream->sample_rate, stream->channels,
845 stream->bits_per_sample);
846 }
847 }
848 }
849 }
850
851 hdl[0] = stream->stopEvent;
852 hdl[1] = Queue_Event(stream->sample_list);
853
854 while (1)
855 {
856 status = WaitForMultipleObjects(2, hdl, FALSE, 1000);
857
858 if (status == WAIT_FAILED)
859 {
860 error = GetLastError();
861 WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "!", error);
862 break;
863 }
864
865 status = WaitForSingleObject(stream->stopEvent, 0);
866
867 if (status == WAIT_FAILED)
868 {
869 error = GetLastError();
870 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", error);
871 break;
872 }
873
874 if (status == WAIT_OBJECT_0)
875 break;
876
877 if (stream->decoder)
878 if (stream->decoder->BufferLevel)
879 stream->currentBufferLevel = stream->decoder->BufferLevel(stream->decoder);
880
881 sample = tsmf_stream_pop_sample(stream, 0);
882
883 if (sample && !tsmf_sample_playback(stream, sample))
884 {
885 WLog_ERR(TAG, "error playing sample");
886 error = ERROR_INTERNAL_ERROR;
887 break;
888 }
889
890 if (stream->currentBufferLevel > stream->minBufferLevel)
891 USleep(1000);
892 }
893
894 if (stream->audio)
895 {
896 stream->audio->Free(stream->audio);
897 stream->audio = nullptr;
898 }
899
900 if (error && stream->rdpcontext)
901 setChannelError(stream->rdpcontext, error, "tsmf_stream_playback_func reported an error");
902
903 DEBUG_TSMF("out %" PRIu32 "", stream->stream_id);
904 ExitThread(error);
905 return error;
906}
907
908static BOOL tsmf_stream_start(TSMF_STREAM* stream)
909{
910 if (!stream || !stream->presentation || !stream->decoder || !stream->decoder->Control)
911 return TRUE;
912
913 stream->eos = 0;
914 return stream->decoder->Control(stream->decoder, Control_Restart, nullptr);
915}
916
917static BOOL tsmf_stream_stop(TSMF_STREAM* stream)
918{
919 if (!stream || !stream->decoder || !stream->decoder->Control)
920 return TRUE;
921
922 /* If stopping after eos - we delay until the eos has been processed
923 * this allows us to process any buffers that have been acked even though
924 * they have not actually been completely processes by the decoder
925 */
926 if (stream->eos)
927 {
928 DEBUG_TSMF("Setting up a delayed stop for once the eos has been processed.");
929 stream->delayed_stop = 1;
930 return TRUE;
931 }
932 /* Otherwise force stop immediately */
933 else
934 {
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);
938 }
939}
940
941static BOOL tsmf_stream_pause(TSMF_STREAM* stream)
942{
943 if (!stream || !stream->decoder || !stream->decoder->Control)
944 return TRUE;
945
946 return stream->decoder->Control(stream->decoder, Control_Pause, nullptr);
947}
948
949static BOOL tsmf_stream_restart(TSMF_STREAM* stream)
950{
951 if (!stream || !stream->decoder || !stream->decoder->Control)
952 return TRUE;
953
954 stream->eos = 0;
955 return stream->decoder->Control(stream->decoder, Control_Restart, nullptr);
956}
957
958static BOOL tsmf_stream_change_volume(TSMF_STREAM* stream, UINT32 newVolume, UINT32 muted)
959{
960 if (!stream || !stream->decoder)
961 return TRUE;
962
963 if (stream->decoder != nullptr && stream->decoder->ChangeVolume)
964 {
965 return stream->decoder->ChangeVolume(stream->decoder, newVolume, muted);
966 }
967 else if (stream->audio != nullptr && stream->audio->ChangeVolume)
968 {
969 return stream->audio->ChangeVolume(stream->audio, newVolume, muted);
970 }
971
972 return TRUE;
973}
974
975BOOL tsmf_presentation_volume_changed(TSMF_PRESENTATION* presentation, UINT32 newVolume,
976 UINT32 muted)
977{
978 TSMF_STREAM* stream = nullptr;
979 BOOL ret = TRUE;
980 presentation->volume = newVolume;
981 presentation->muted = muted;
982 ArrayList_Lock(presentation->stream_list);
983 size_t count = ArrayList_Count(presentation->stream_list);
984
985 for (size_t index = 0; index < count; index++)
986 {
987 stream = (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
988 ret &= tsmf_stream_change_volume(stream, newVolume, muted);
989 }
990
991 ArrayList_Unlock(presentation->stream_list);
992 return ret;
993}
994
995BOOL tsmf_presentation_paused(TSMF_PRESENTATION* presentation)
996{
997 TSMF_STREAM* stream = nullptr;
998 BOOL ret = TRUE;
999 ArrayList_Lock(presentation->stream_list);
1000 size_t count = ArrayList_Count(presentation->stream_list);
1001
1002 for (size_t index = 0; index < count; index++)
1003 {
1004 stream = (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
1005 ret &= tsmf_stream_pause(stream);
1006 }
1007
1008 ArrayList_Unlock(presentation->stream_list);
1009 return ret;
1010}
1011
1012BOOL tsmf_presentation_restarted(TSMF_PRESENTATION* presentation)
1013{
1014 TSMF_STREAM* stream = nullptr;
1015 BOOL ret = TRUE;
1016 ArrayList_Lock(presentation->stream_list);
1017 size_t count = ArrayList_Count(presentation->stream_list);
1018
1019 for (size_t index = 0; index < count; index++)
1020 {
1021 stream = (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
1022 ret &= tsmf_stream_restart(stream);
1023 }
1024
1025 ArrayList_Unlock(presentation->stream_list);
1026 return ret;
1027}
1028
1029BOOL tsmf_presentation_start(TSMF_PRESENTATION* presentation)
1030{
1031 TSMF_STREAM* stream = nullptr;
1032 BOOL ret = TRUE;
1033 ArrayList_Lock(presentation->stream_list);
1034 size_t count = ArrayList_Count(presentation->stream_list);
1035
1036 for (size_t index = 0; index < count; index++)
1037 {
1038 stream = (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
1039 ret &= tsmf_stream_start(stream);
1040 }
1041
1042 ArrayList_Unlock(presentation->stream_list);
1043 return ret;
1044}
1045
1051UINT tsmf_presentation_sync(TSMF_PRESENTATION* presentation)
1052{
1053 UINT error = 0;
1054 ArrayList_Lock(presentation->stream_list);
1055 size_t count = ArrayList_Count(presentation->stream_list);
1056
1057 for (size_t index = 0; index < count; index++)
1058 {
1059 TSMF_STREAM* stream = (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
1060
1061 if (WaitForSingleObject(stream->ready, 500) == WAIT_FAILED)
1062 {
1063 error = GetLastError();
1064 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", error);
1065 return error;
1066 }
1067 }
1068
1069 ArrayList_Unlock(presentation->stream_list);
1070 return CHANNEL_RC_OK;
1071}
1072
1073BOOL tsmf_presentation_stop(TSMF_PRESENTATION* presentation)
1074{
1075 TSMF_STREAM* stream = nullptr;
1076 BOOL ret = TRUE;
1077 ArrayList_Lock(presentation->stream_list);
1078 size_t count = ArrayList_Count(presentation->stream_list);
1079
1080 for (size_t index = 0; index < count; index++)
1081 {
1082 stream = (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
1083 ret &= tsmf_stream_stop(stream);
1084 }
1085
1086 ArrayList_Unlock(presentation->stream_list);
1087 presentation->audio_start_time = 0;
1088 presentation->audio_end_time = 0;
1089 return ret;
1090}
1091
1092BOOL tsmf_presentation_set_geometry_info(TSMF_PRESENTATION* presentation, UINT32 x, UINT32 y,
1093 UINT32 width, UINT32 height, UINT32 num_rects,
1094 const RECTANGLE_32* rects)
1095{
1096 TSMF_STREAM* stream = nullptr;
1097 BOOL ret = TRUE;
1098
1099 /* The server may send messages with invalid width / height.
1100 * Ignore those messages. */
1101 if (!width || !height)
1102 return TRUE;
1103
1104 ArrayList_Lock(presentation->stream_list);
1105 /* Streams can be added/removed from the presentation and the server will resend geometry info
1106 * when a new stream is added to the presentation. Also, num_rects is used to indicate whether
1107 * or not the window is visible. So, always process a valid message with unchanged position/size
1108 * and/or no visibility rects.
1109 */
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);
1115
1116 if (!tmp_rects && num_rects)
1117 {
1118 ArrayList_Unlock(presentation->stream_list);
1119 return FALSE;
1120 }
1121
1122 presentation->nr_rects = num_rects;
1123 presentation->rects = (RECTANGLE_32*)tmp_rects;
1124 if (presentation->rects)
1125 CopyMemory(presentation->rects, rects, sizeof(RECTANGLE_32) * num_rects);
1126
1127 size_t count = ArrayList_Count(presentation->stream_list);
1128
1129 for (size_t index = 0; index < count; index++)
1130 {
1131 stream = (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
1132
1133 if (!stream->decoder)
1134 continue;
1135
1136 if (stream->decoder->UpdateRenderingArea)
1137 {
1138 ret = stream->decoder->UpdateRenderingArea(stream->decoder, x, y, width, height,
1139 num_rects, rects);
1140 }
1141 }
1142
1143 ArrayList_Unlock(presentation->stream_list);
1144 return ret;
1145}
1146
1147void tsmf_presentation_set_audio_device(TSMF_PRESENTATION* presentation, const char* name,
1148 const char* device)
1149{
1150 presentation->audio_name = name;
1151 presentation->audio_device = device;
1152}
1153
1154BOOL tsmf_stream_flush(TSMF_STREAM* stream)
1155{
1156 BOOL ret = TRUE;
1157
1158 // TSMF_SAMPLE* sample;
1159 /* TODO: free lists */
1160 if (stream->audio)
1161 ret = stream->audio->Flush(stream->audio);
1162
1163 stream->eos = 0;
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);
1169
1170 if (stream->major_type == TSMF_MAJOR_TYPE_AUDIO)
1171 {
1172 stream->presentation->audio_start_time = 0;
1173 stream->presentation->audio_end_time = 0;
1174 }
1175
1176 return ret;
1177}
1178
1179void s_tsmf_presentation_free(void* obj)
1180{
1181 TSMF_PRESENTATION* presentation = (TSMF_PRESENTATION*)obj;
1182
1183 if (presentation)
1184 {
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));
1190 free(presentation);
1191 }
1192}
1193
1194void tsmf_presentation_free(TSMF_PRESENTATION* presentation)
1195{
1196 ArrayList_Remove(presentation_list, presentation);
1197}
1198
1199TSMF_STREAM* tsmf_stream_new(TSMF_PRESENTATION* presentation, UINT32 stream_id,
1200 rdpContext* rdpcontext)
1201{
1202 wObject* obj = nullptr;
1203 TSMF_STREAM* stream = nullptr;
1204 stream = tsmf_stream_find_by_id(presentation, stream_id);
1205
1206 if (stream)
1207 {
1208 WLog_ERR(TAG, "duplicated stream id %" PRIu32 "!", stream_id);
1209 return nullptr;
1210 }
1211
1212 stream = (TSMF_STREAM*)calloc(1, sizeof(TSMF_STREAM));
1213
1214 if (!stream)
1215 {
1216 WLog_ERR(TAG, "Calloc failed");
1217 return nullptr;
1218 }
1219
1220 stream->minBufferLevel = VIDEO_MIN_BUFFER_LEVEL;
1221 stream->maxBufferLevel = VIDEO_MAX_BUFFER_LEVEL;
1222 stream->currentBufferLevel = 1;
1223 stream->seeking = FALSE;
1224 stream->eos = 0;
1225 stream->eos_message_id = 0;
1226 stream->stream_id = stream_id;
1227 stream->presentation = presentation;
1228 stream->stopEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
1229
1230 if (!stream->stopEvent)
1231 goto error_stopEvent;
1232
1233 stream->ready = CreateEvent(nullptr, TRUE, TRUE, nullptr);
1234
1235 if (!stream->ready)
1236 goto error_ready;
1237
1238 stream->sample_list = Queue_New(TRUE, -1, -1);
1239
1240 if (!stream->sample_list)
1241 goto error_sample_list;
1242
1243 obj = Queue_Object(stream->sample_list);
1244 if (!obj)
1245 goto error_sample_ack_list;
1246 obj->fnObjectFree = tsmf_sample_free;
1247
1248 stream->sample_ack_list = Queue_New(TRUE, -1, -1);
1249
1250 if (!stream->sample_ack_list)
1251 goto error_sample_ack_list;
1252
1253 obj = Queue_Object(stream->sample_ack_list);
1254 if (!obj)
1255 goto error_play_thread;
1256 obj->fnObjectFree = tsmf_sample_free;
1257
1258 stream->play_thread =
1259 CreateThread(nullptr, 0, tsmf_stream_playback_func, stream, CREATE_SUSPENDED, nullptr);
1260
1261 if (!stream->play_thread)
1262 goto error_play_thread;
1263
1264 stream->ack_thread =
1265 CreateThread(nullptr, 0, tsmf_stream_ack_func, stream, CREATE_SUSPENDED, nullptr);
1266
1267 if (!stream->ack_thread)
1268 goto error_ack_thread;
1269
1270 if (!ArrayList_Append(presentation->stream_list, stream))
1271 goto error_add;
1272
1273 stream->rdpcontext = rdpcontext;
1274 return stream;
1275error_add:
1276 (void)SetEvent(stream->stopEvent);
1277
1278 if (WaitForSingleObject(stream->ack_thread, INFINITE) == WAIT_FAILED)
1279 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", GetLastError());
1280
1281error_ack_thread:
1282 (void)SetEvent(stream->stopEvent);
1283
1284 if (WaitForSingleObject(stream->play_thread, INFINITE) == WAIT_FAILED)
1285 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", GetLastError());
1286
1287error_play_thread:
1288 Queue_Free(stream->sample_ack_list);
1289error_sample_ack_list:
1290 Queue_Free(stream->sample_list);
1291error_sample_list:
1292 (void)CloseHandle(stream->ready);
1293error_ready:
1294 (void)CloseHandle(stream->stopEvent);
1295error_stopEvent:
1296 free(stream);
1297 return nullptr;
1298}
1299
1300void tsmf_stream_start_threads(TSMF_STREAM* stream)
1301{
1302 ResumeThread(stream->play_thread);
1303 ResumeThread(stream->ack_thread);
1304}
1305
1306TSMF_STREAM* tsmf_stream_find_by_id(TSMF_PRESENTATION* presentation, UINT32 stream_id)
1307{
1308 BOOL found = FALSE;
1309 TSMF_STREAM* stream = nullptr;
1310 ArrayList_Lock(presentation->stream_list);
1311 size_t count = ArrayList_Count(presentation->stream_list);
1312
1313 for (size_t index = 0; index < count; index++)
1314 {
1315 stream = (TSMF_STREAM*)ArrayList_GetItem(presentation->stream_list, index);
1316
1317 if (stream->stream_id == stream_id)
1318 {
1319 found = TRUE;
1320 break;
1321 }
1322 }
1323
1324 ArrayList_Unlock(presentation->stream_list);
1325 return (found) ? stream : nullptr;
1326}
1327
1328static void tsmf_stream_resync(void* arg)
1329{
1330 TSMF_STREAM* stream = arg;
1331 (void)ResetEvent(stream->ready);
1332}
1333
1334BOOL tsmf_stream_set_format(TSMF_STREAM* stream, const char* name, wStream* s)
1335{
1336 TS_AM_MEDIA_TYPE mediatype;
1337 BOOL ret = TRUE;
1338
1339 if (stream->decoder)
1340 {
1341 WLog_ERR(TAG, "duplicated call");
1342 return FALSE;
1343 }
1344
1345 if (!tsmf_codec_parse_media_type(&mediatype, s))
1346 {
1347 WLog_ERR(TAG, "unable to parse media type");
1348 return FALSE;
1349 }
1350
1351 if (mediatype.MajorType == TSMF_MAJOR_TYPE_VIDEO)
1352 {
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;
1361 }
1362 else if (mediatype.MajorType == TSMF_MAJOR_TYPE_AUDIO)
1363 {
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;
1371
1372 if (stream->bits_per_sample == 0)
1373 stream->bits_per_sample = 16;
1374
1375 stream->minBufferLevel = AUDIO_MIN_BUFFER_LEVEL;
1376 stream->maxBufferLevel = AUDIO_MAX_BUFFER_LEVEL;
1377 }
1378
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);
1385
1386 if (!stream->decoder)
1387 return FALSE;
1388
1389 if (stream->decoder->SetAckFunc)
1390 ret &= stream->decoder->SetAckFunc(stream->decoder, tsmf_stream_process_ack, stream);
1391
1392 if (stream->decoder->SetSyncFunc)
1393 ret &= stream->decoder->SetSyncFunc(stream->decoder, tsmf_stream_resync, stream);
1394
1395 return ret;
1396}
1397
1398void tsmf_stream_end(TSMF_STREAM* stream, UINT32 message_id)
1399{
1400 if (!stream)
1401 return;
1402
1403 stream->eos = 1;
1404 stream->eos_message_id = message_id;
1405}
1406
1407void s_tsmf_stream_free(void* obj)
1408{
1409 TSMF_STREAM* stream = (TSMF_STREAM*)obj;
1410
1411 if (!stream)
1412 return;
1413
1414 tsmf_stream_stop(stream);
1415 (void)SetEvent(stream->stopEvent);
1416
1417 if (stream->play_thread)
1418 {
1419 if (WaitForSingleObject(stream->play_thread, INFINITE) == WAIT_FAILED)
1420 {
1421 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", GetLastError());
1422 return;
1423 }
1424
1425 (void)CloseHandle(stream->play_thread);
1426 stream->play_thread = nullptr;
1427 }
1428
1429 if (stream->ack_thread)
1430 {
1431 if (WaitForSingleObject(stream->ack_thread, INFINITE) == WAIT_FAILED)
1432 {
1433 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", GetLastError());
1434 return;
1435 }
1436
1437 (void)CloseHandle(stream->ack_thread);
1438 stream->ack_thread = nullptr;
1439 }
1440
1441 Queue_Free(stream->sample_list);
1442 Queue_Free(stream->sample_ack_list);
1443
1444 if (stream->decoder && stream->decoder->Free)
1445 {
1446 stream->decoder->Free(stream->decoder);
1447 stream->decoder = nullptr;
1448 }
1449
1450 (void)CloseHandle(stream->stopEvent);
1451 (void)CloseHandle(stream->ready);
1452 ZeroMemory(stream, sizeof(TSMF_STREAM));
1453 free(stream);
1454}
1455
1456void tsmf_stream_free(TSMF_STREAM* stream)
1457{
1458 TSMF_PRESENTATION* presentation = stream->presentation;
1459 ArrayList_Remove(presentation->stream_list, stream);
1460}
1461
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,
1464 BYTE* data)
1465{
1466 WINPR_ASSERT(stream);
1467
1468 (void)SetEvent(stream->ready);
1469
1470 if (TERMINATING)
1471 return TRUE;
1472
1473 TSMF_SAMPLE* sample = (TSMF_SAMPLE*)calloc(1, sizeof(TSMF_SAMPLE));
1474
1475 if (!sample)
1476 {
1477 WLog_ERR(TAG, "calloc sample failed!");
1478 return FALSE;
1479 }
1480
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;
1486
1487 if ((sample->extensions & 0x00000080) || (sample->extensions & 0x00000040))
1488 sample->invalidTimestamps = TRUE;
1489 else
1490 sample->invalidTimestamps = FALSE;
1491
1492 sample->data_size = data_size;
1493 sample->data = calloc(1, data_size + TSMF_BUFFER_PADDING_SIZE);
1494
1495 if (!sample->data)
1496 goto fail;
1497
1498 CopyMemory(sample->data, data, data_size);
1499 if (!Queue_Enqueue(stream->sample_list, sample))
1500 goto fail;
1501
1502 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): Queue_Enqueue takes ownership of sample
1503 return TRUE;
1504
1505fail:
1506 if (sample)
1507 free(sample->data);
1508 free(sample);
1509 return FALSE;
1510}
1511
1512#ifndef _WIN32
1513
1514static void tsmf_signal_handler(int s)
1515{
1516 TERMINATING = 1;
1517 ArrayList_Free(presentation_list);
1518
1519 if (s == SIGINT)
1520 {
1521 (void)signal(s, SIG_DFL);
1522 kill(getpid(), s);
1523 }
1524 else if (s == SIGUSR1)
1525 {
1526 (void)signal(s, SIG_DFL);
1527 }
1528}
1529
1530#endif
1531
1532BOOL tsmf_media_init(void)
1533{
1534 wObject* obj = nullptr;
1535#ifndef _WIN32
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);
1542#endif
1543
1544 if (!presentation_list)
1545 {
1546 presentation_list = ArrayList_New(TRUE);
1547
1548 if (!presentation_list)
1549 return FALSE;
1550
1551 obj = ArrayList_Object(presentation_list);
1552 if (!obj)
1553 return FALSE;
1554 obj->fnObjectFree = s_tsmf_presentation_free;
1555 }
1556
1557 return TRUE;
1558}
This struct contains function pointer to initialize/free objects.
Definition collections.h:52