From 128d4776f84a8d84592f9dc4992d0c542f603bd3 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Wed, 6 Mar 2019 23:17:47 +0000 Subject: [PATCH] [core] some more hardening work --- src/mod/applications/mod_av/avformat.c | 13 ++++++++--- .../mod_local_stream/mod_local_stream.c | 23 +++++++++++-------- src/switch_core_media.c | 13 +++++++---- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/mod/applications/mod_av/avformat.c b/src/mod/applications/mod_av/avformat.c index 5ffd12d923..6036580d64 100644 --- a/src/mod/applications/mod_av/avformat.c +++ b/src/mod/applications/mod_av/avformat.c @@ -822,7 +822,12 @@ static void *SWITCH_THREAD_FUNC video_thread_run(switch_thread_t *thread, void * top: switch_assert(context->eh.video_queue); - + while(switch_queue_size(context->eh.video_queue) > 1) { + switch_image_t *tmp_img; + switch_queue_pop(context->eh.video_queue, &pop); + tmp_img = (switch_image_t *) pop; + switch_img_free(&tmp_img); + } if (switch_queue_pop(context->eh.video_queue, &pop) == SWITCH_STATUS_SUCCESS) { switch_img_free(&img); @@ -910,7 +915,7 @@ static void *SWITCH_THREAD_FUNC video_thread_run(switch_thread_t *thread, void * } else { uint64_t delta_tmp; - switch_core_timer_sync(context->eh.video_timer); + switch_core_timer_next(context->eh.video_timer); delta_tmp = (context->eh.video_timer->samplecount * 90) - context->eh.last_ts; if (delta_tmp != 0) { @@ -1757,6 +1762,7 @@ static switch_status_t av_file_open(switch_file_handle_t *handle, const char *pa } if (context->has_video) { + switch_fps_t fps_data = { 0 }; switch_queue_create(&context->eh.video_queue, context->read_fps, handle->memory_pool); context->no_video_decode = handle->params && switch_true(switch_event_get_header(handle->params, "no_video_decode")); if (context->no_video_decode) { @@ -1764,7 +1770,8 @@ static switch_status_t av_file_open(switch_file_handle_t *handle, const char *pa switch_queue_create(&context->video_pkt_queue, 120 * 5, handle->memory_pool); } switch_mutex_init(&context->eh.mutex, SWITCH_MUTEX_NESTED, handle->memory_pool); - switch_core_timer_init(&context->video_timer, "soft", (int)(1000.0f / context->read_fps), 1, context->pool); + switch_calc_video_fps(&fps_data, context->read_fps); + switch_core_timer_init(&context->video_timer, "soft", fps_data.ms, fps_data.samples, context->pool); } { diff --git a/src/mod/formats/mod_local_stream/mod_local_stream.c b/src/mod/formats/mod_local_stream/mod_local_stream.c index c70d77410c..a38411e954 100644 --- a/src/mod/formats/mod_local_stream/mod_local_stream.c +++ b/src/mod/formats/mod_local_stream/mod_local_stream.c @@ -131,6 +131,7 @@ struct local_stream_source { uint8_t logo_opacity; uint8_t text_opacity; switch_mm_t mm; + int sync; }; typedef struct local_stream_source local_stream_source_t; @@ -362,6 +363,7 @@ static void *SWITCH_THREAD_FUNC read_stream_thread(switch_thread_t *thread, void continue; } + source->sync = 0; switch_buffer_zero(audio_buffer); if (switch_core_file_has_video(&fh, SWITCH_FALSE)) { @@ -907,7 +909,7 @@ static switch_status_t local_stream_file_open(switch_file_handle_t *handle, cons handle->private_info = context; handle->interval = source->interval; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Opening Stream [%s] %dhz\n", path, handle->samplerate); - + handle->mm.source_fps = source->mm.source_fps; switch_mutex_init(&context->audio_mutex, SWITCH_MUTEX_NESTED, context->pool); if (switch_buffer_create_dynamic(&context->audio_buffer, 512, 1024, 0) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error!\n"); @@ -999,14 +1001,19 @@ static switch_status_t local_stream_file_read_video(switch_file_handle_t *handle local_stream_context_t *context = handle->private_info; switch_status_t status; switch_time_t now; - unsigned int fps = (unsigned int)ceil(handle->mm.fps); - unsigned int min_qsize = fps / 2; - unsigned int buf_qsize = 5; + unsigned int min_qsize = 1; + unsigned int buf_qsize = 1; + + handle->mm.source_fps = context->source->mm.source_fps; if (!(context->ready && context->source->ready)) { return SWITCH_STATUS_FALSE; } + if (!context->source->sync) { + return SWITCH_STATUS_BREAK; + } + if (!context->source->has_video) { if (frame) { switch_image_t *src_img = context->source->cover_art; @@ -1040,11 +1047,6 @@ static switch_status_t local_stream_file_read_video(switch_file_handle_t *handle return SWITCH_STATUS_BREAK; } - if (handle->mm.fps >= context->source->mm.source_fps) { - min_qsize = 1; - buf_qsize = 1; - } - while(context->ready && context->source->ready && switch_queue_size(context->video_q) > min_qsize) { if (switch_queue_trypop(context->video_q, &pop) == SWITCH_STATUS_SUCCESS) { switch_image_t *img = (switch_image_t *) pop; @@ -1170,9 +1172,12 @@ static switch_status_t local_stream_file_read(switch_file_handle_t *handle, void if ((bytes = switch_buffer_read(context->audio_buffer, data, need))) { *len = bytes / 2 / context->source->channels; + context->source->sync = 1; } else { size_t blank; + context->source->sync = 0; + switch_assert(handle->samplerate <= 48000); switch_assert(handle->real_channels <= 2); diff --git a/src/switch_core_media.c b/src/switch_core_media.c index ec0b5978a1..5c4e19ad49 100644 --- a/src/switch_core_media.c +++ b/src/switch_core_media.c @@ -6843,7 +6843,7 @@ static void *SWITCH_THREAD_FUNC video_write_thread(switch_thread_t *thread, void switch_video_read_flag_t read_flags = SVR_FLUSH; switch_core_session_t *b_session = NULL; switch_fps_t fps_data = { 0 }; - float fps; + float fps = 15.0f; switch_image_t *last_frame = NULL; int last_w = 0, last_h = 0, kps = 0; @@ -6878,12 +6878,14 @@ static void *SWITCH_THREAD_FUNC video_write_thread(switch_thread_t *thread, void switch_core_media_gen_key_frame(session); - if (smh->video_write_fh && smh->video_write_fh->mm.source_fps) { - fps = smh->video_write_fh->mm.source_fps; + if (smh->video_write_fh) { + if (smh->video_write_fh->mm.fps) { + fps = smh->video_write_fh->mm.fps; + } else if (smh->video_write_fh->mm.source_fps) { + fps = smh->video_write_fh->mm.source_fps; + } } else if (video_globals.fps) { fps = video_globals.fps; - } else { - fps = 15; } switch_calc_video_fps(&fps_data, fps); @@ -6929,6 +6931,7 @@ static void *SWITCH_THREAD_FUNC video_write_thread(switch_thread_t *thread, void if (smh->video_write_fh && !switch_test_flag(smh->video_write_fh, SWITCH_FILE_FLAG_VIDEO_EOF)) { wstatus = switch_core_file_read_video(smh->video_write_fh, &fr, read_flags); + if (wstatus == SWITCH_STATUS_BREAK) { switch_core_timer_sync(&timer); }