From fc1ff9209e9fdb483443e954a3f4e2f55d0070b2 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Fri, 13 Mar 2015 10:41:47 -0500 Subject: [PATCH] FS-7508: timing update --- src/mod/codecs/mod_vpx/mod_vpx.c | 20 +++++++++++++++----- src/switch_time.c | 2 +- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/mod/codecs/mod_vpx/mod_vpx.c b/src/mod/codecs/mod_vpx/mod_vpx.c index bd1c420f5d..4d90341c28 100644 --- a/src/mod/codecs/mod_vpx/mod_vpx.c +++ b/src/mod/codecs/mod_vpx/mod_vpx.c @@ -72,6 +72,7 @@ struct vpx_context { const vpx_codec_cx_pkt_t *pkt; vpx_codec_iter_t iter; uint32_t last_ts; + switch_time_t last_ms; vpx_codec_ctx_t decoder; uint8_t decoder_init; switch_buffer_t *vpx_packet_buffer; @@ -531,6 +532,8 @@ static void reset_codec_encoder(switch_codec_t *codec) if (context->encoder_init) { vpx_codec_destroy(&context->encoder); } + context->last_ts = 0; + context->last_ms = 0; context->framecount = 0; context->encoder_init = 0; context->pkt = NULL; @@ -542,7 +545,10 @@ static switch_status_t switch_vpx_encode(switch_codec_t *codec, switch_frame_t * vpx_context_t *context = (vpx_context_t *)codec->private_info; int width = 0; int height = 0; + uint32_t dur; + int64_t pts; vpx_enc_frame_flags_t vpx_flags = 0; + switch_time_t now; if (frame->flags & SFF_SAME_IMAGE) { return consume_partition(context, frame); @@ -580,9 +586,10 @@ static switch_status_t switch_vpx_encode(switch_codec_t *codec, switch_frame_t * init_encoder(codec); } + now = switch_time_now(); + if (context->need_key_frame != 0) { // force generate a key frame - switch_time_t now = switch_micro_time_now(); if (!context->last_key_frame || (now - context->last_key_frame) > KEY_FRAME_MIN_FREQ) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "VPX KEYFRAME GENERATED\n"); @@ -594,11 +601,14 @@ static switch_status_t switch_vpx_encode(switch_codec_t *codec, switch_frame_t * context->framecount++; - + pts = (now - context->start_time) / 1000; + + dur = context->last_ms ? (now - context->last_ms) / 1000 : pts; + if (vpx_codec_encode(&context->encoder, (vpx_image_t *) frame->img, - switch_micro_time_now() - context->start_time, - 1, + pts, + dur, vpx_flags, VPX_DL_REALTIME) != VPX_CODEC_OK) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "VPX encode error %d:%s\n", @@ -610,7 +620,7 @@ static switch_status_t switch_vpx_encode(switch_codec_t *codec, switch_frame_t * context->iter = NULL; context->last_ts = frame->timestamp; - + context->last_ms = now; return consume_partition(context, frame); } diff --git a/src/switch_time.c b/src/switch_time.c index be3debf560..7642be10e2 100644 --- a/src/switch_time.c +++ b/src/switch_time.c @@ -380,7 +380,7 @@ SWITCH_DECLARE(void) switch_time_set_cond_yield(switch_bool_t enable) static switch_status_t timer_generic_sync(switch_timer_t *timer) { - switch_time_t now = switch_micro_time_now(); + switch_time_t now = switch_time_now(); int64_t elapsed = (now - timer->start); timer->tick = (elapsed / timer->interval) / 1000;