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