From c546154d6756b6ad318e1472f847d8485e63bb48 Mon Sep 17 00:00:00 2001 From: Sergey Khripchenko Date: Fri, 5 Oct 2018 08:22:46 -0700 Subject: [PATCH] FS-11436 more granularly calculate 'samples' and modify requested 'fsp' accordingly + review all places where it used and reflect that 'fps' is (float) --- src/include/switch_utils.h | 10 +++++++--- .../mod_conference/conference_video.c | 14 +++++++------- src/switch_core_media.c | 15 ++++++--------- src/switch_core_media_bug.c | 12 +++++++----- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/include/switch_utils.h b/src/include/switch_utils.h index 54536361db..2dbee93ef0 100644 --- a/src/include/switch_utils.h +++ b/src/include/switch_utils.h @@ -1074,9 +1074,13 @@ static inline int32_t switch_calc_bitrate(int w, int h, int quality, double fps) static inline void switch_calc_fps(switch_fps_t *fpsP, float fps, int samplerate) { - fpsP->fps = fps; - fpsP->ms = (int)(1000 / fps); - fpsP->samples = (int)(samplerate / fps); + /* + implicit/truncf() - leave us with equal-or-smaller ms and thus equal-or-bigger fps, which is better for quality (than roundf()). + also equal-or-bigger fps is better for things like (int)fps + */ + fpsP->ms = (int)(1000.0f / fps); + fpsP->fps = 1000.0f / fpsP->ms; + fpsP->samples = (int)(samplerate / 1000 * fpsP->ms); // samplerate 99.99% is a factor of 1000, so we safe here with integer div by 1000 return; } #define switch_calc_video_fps(fpsP, fps) switch_calc_fps(fpsP, fps, 90000) diff --git a/src/mod/applications/mod_conference/conference_video.c b/src/mod/applications/mod_conference/conference_video.c index 4ff4220fd1..4c0f43e0ab 100644 --- a/src/mod/applications/mod_conference/conference_video.c +++ b/src/mod/applications/mod_conference/conference_video.c @@ -2797,12 +2797,12 @@ void conference_video_pop_next_image(conference_member_t *member, switch_image_t member->blanks++; - if (member->blanks == member->conference->video_fps.fps || (member->blanks % (int)(member->conference->video_fps.fps * 10)) == 0) { + if (member->blanks == (int)member->conference->video_fps.fps || (member->blanks % (int)(member->conference->video_fps.fps * 10)) == 0) { switch_core_session_request_video_refresh(member->session); member->good_img = 0; } - if (member->blanks == member->conference->video_fps.fps * 5) { + if (member->blanks == (int)(member->conference->video_fps.fps * 5)) { member->blackouts++; conference_video_check_avatar(member, SWITCH_TRUE); conference_video_clear_managed_kps(member); @@ -2973,12 +2973,12 @@ void conference_video_check_auto_bitrate(conference_member_t *member, mcu_layer_ } if ((kps_in = switch_calc_bitrate(vid_params.width, vid_params.height, - member->conference->video_quality, (int)(member->conference->video_fps.fps))) < 512) { + member->conference->video_quality, member->conference->video_fps.fps)) < 512) { kps_in = 512; } if (layer) { - kps = switch_calc_bitrate(screen_w, screen_h, member->conference->video_quality, (int)(member->conference->video_fps.fps)); + kps = switch_calc_bitrate(screen_w, screen_h, member->conference->video_quality, member->conference->video_fps.fps); } else { kps = kps_in; } @@ -3701,7 +3701,7 @@ void *SWITCH_THREAD_FUNC conference_video_muxing_thread_run(switch_thread_t *thr if (imember->channel && !switch_channel_test_flag(imember->channel, CF_VIDEO_BITRATE_UNMANAGABLE) && conference_utils_test_flag(conference, CFLAG_MANAGE_INBOUND_VIDEO_BITRATE)) { switch_core_media_get_vid_params(imember->session, &vid_params); - kps = switch_calc_bitrate(vid_params.width, vid_params.height, conference->video_quality, (int)(imember->conference->video_fps.fps)); + kps = switch_calc_bitrate(vid_params.width, vid_params.height, conference->video_quality, imember->conference->video_fps.fps); conference_video_set_incoming_bitrate(imember, kps, SWITCH_TRUE); } } @@ -4273,7 +4273,7 @@ void pop_conference_video_next_canvas_image(mcu_canvas_t *canvas, switch_image_t break; } size = switch_queue_size(canvas->video_queue); - } while(size > canvas->conference->video_fps.fps / 2); + } while(size > (int)(canvas->conference->video_fps.fps / 2)); *imgP = img; } @@ -4993,7 +4993,7 @@ switch_status_t conference_video_thread_callback(switch_core_session_t *session, if (frame->img && (((member->video_layer_id > -1) && canvas_id > -1) || member->canvas) && conference_utils_member_test_flag(member, MFLAG_CAN_BE_SEEN) && !conference_utils_member_test_flag(member, MFLAG_HOLD) && - switch_queue_size(member->video_queue) < member->conference->video_fps.fps && + switch_queue_size(member->video_queue) < (int)member->conference->video_fps.fps && !member->conference->canvases[canvas_id]->playing_video_file) { if (conference_utils_member_test_flag(member, MFLAG_FLIP_VIDEO) || diff --git a/src/switch_core_media.c b/src/switch_core_media.c index c33acaff4d..9eb25b2b6b 100644 --- a/src/switch_core_media.c +++ b/src/switch_core_media.c @@ -6670,10 +6670,10 @@ static void *SWITCH_THREAD_FUNC video_write_thread(switch_thread_t *thread, void switch_rtp_engine_t *v_engine; int buflen = SWITCH_RTP_MAX_BUF_LEN; switch_timer_t timer = { 0 }; - int fps; switch_video_read_flag_t read_flags = SVR_BLOCK; switch_core_session_t *b_session = NULL; switch_fps_t fps_data = { 0 }; + float fps; switch_image_t *last_frame = NULL; if (switch_core_session_read_lock(session) != SWITCH_STATUS_SUCCESS) { @@ -6707,17 +6707,14 @@ static void *SWITCH_THREAD_FUNC video_write_thread(switch_thread_t *thread, void if (smh->video_write_fh && smh->video_write_fh->mm.source_fps) { - fps = (int) smh->video_write_fh->mm.source_fps; - } else { + fps = smh->video_write_fh->mm.source_fps; + } else if (video_globals.fps) { fps = video_globals.fps; - } - - if (!fps) { + } else { fps = 15; } - - switch_calc_video_fps(&fps_data, fps); + switch_core_timer_init(&timer, "soft", fps_data.ms, fps_data.samples, switch_core_session_get_pool(session)); while (smh->video_write_thread_running > 0 && @@ -6764,7 +6761,7 @@ static void *SWITCH_THREAD_FUNC video_write_thread(switch_thread_t *thread, void switch_img_fill(last_frame, 0, 0, last_frame->d_w, last_frame->d_h, &bgcolor); fr.img = last_frame; - for (x = 0; x < fps_data.fps / 2; x++) { + for (x = 0; x < (int)(fps_data.fps / 2); x++) { switch_core_timer_next(&timer); fr.timestamp = timer.samplecount; fr.flags = SFF_USE_VIDEO_TIMESTAMP|SFF_RAW_RTP|SFF_RAW_RTP_PARSE_FRAME; diff --git a/src/switch_core_media_bug.c b/src/switch_core_media_bug.c index 29f7837de4..9f10bf5955 100644 --- a/src/switch_core_media_bug.c +++ b/src/switch_core_media_bug.c @@ -599,11 +599,11 @@ static void *SWITCH_THREAD_FUNC video_bug_thread(switch_thread_t *thread, void * switch_frame_t frame = { 0 }; switch_timer_t timer = { 0 }; switch_mm_t mm = { 0 }; - int fps = 15; int vw = 1280; int vh = 720; int last_w = 0, last_h = 0, other_last_w = 0, other_last_h = 0; switch_fps_t fps_data = { 0 }; + float fps; switch_rgb_color_t color = { 0 }; switch_color_set_rgb(&color, "#000000"); @@ -628,14 +628,16 @@ static void *SWITCH_THREAD_FUNC video_bug_thread(switch_thread_t *thread, void * switch_core_media_bug_get_media_params(bug, &mm); - if (mm.fps) { - fps = (int) mm.fps; - } - if (mm.vw) vw = mm.vw; if (mm.vh) vh = mm.vh; + if (mm.fps) { + fps = mm.fps; + } else { + fps = 15; + } switch_calc_video_fps(&fps_data, fps); + switch_core_timer_init(&timer, "soft", fps_data.ms, fps_data.samples, NULL); while (bug->ready) {