Home | History | Annotate | Download | only in source
      1 /*
      2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include "webrtc/modules/video_coding/main/source/timing.h"
     12 
     13 #include "webrtc/modules/video_coding/main/source/internal_defines.h"
     14 #include "webrtc/modules/video_coding/main/source/jitter_buffer_common.h"
     15 #include "webrtc/system_wrappers/interface/clock.h"
     16 #include "webrtc/system_wrappers/interface/timestamp_extrapolator.h"
     17 
     18 
     19 namespace webrtc {
     20 
     21 VCMTiming::VCMTiming(Clock* clock,
     22                      VCMTiming* master_timing)
     23     : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
     24       clock_(clock),
     25       master_(false),
     26       ts_extrapolator_(),
     27       codec_timer_(),
     28       render_delay_ms_(kDefaultRenderDelayMs),
     29       min_playout_delay_ms_(0),
     30       jitter_delay_ms_(0),
     31       current_delay_ms_(0),
     32       last_decode_ms_(0),
     33       prev_frame_timestamp_(0) {
     34   if (master_timing == NULL) {
     35     master_ = true;
     36     ts_extrapolator_ = new TimestampExtrapolator(clock_->TimeInMilliseconds());
     37   } else {
     38     ts_extrapolator_ = master_timing->ts_extrapolator_;
     39   }
     40 }
     41 
     42 VCMTiming::~VCMTiming() {
     43   if (master_) {
     44     delete ts_extrapolator_;
     45   }
     46   delete crit_sect_;
     47 }
     48 
     49 void VCMTiming::Reset() {
     50   CriticalSectionScoped cs(crit_sect_);
     51   ts_extrapolator_->Reset(clock_->TimeInMilliseconds());
     52   codec_timer_.Reset();
     53   render_delay_ms_ = kDefaultRenderDelayMs;
     54   min_playout_delay_ms_ = 0;
     55   jitter_delay_ms_ = 0;
     56   current_delay_ms_ = 0;
     57   prev_frame_timestamp_ = 0;
     58 }
     59 
     60 void VCMTiming::ResetDecodeTime() {
     61   codec_timer_.Reset();
     62 }
     63 
     64 void VCMTiming::set_render_delay(uint32_t render_delay_ms) {
     65   CriticalSectionScoped cs(crit_sect_);
     66   render_delay_ms_ = render_delay_ms;
     67 }
     68 
     69 void VCMTiming::set_min_playout_delay(uint32_t min_playout_delay_ms) {
     70   CriticalSectionScoped cs(crit_sect_);
     71   min_playout_delay_ms_ = min_playout_delay_ms;
     72 }
     73 
     74 void VCMTiming::SetJitterDelay(uint32_t jitter_delay_ms) {
     75   CriticalSectionScoped cs(crit_sect_);
     76   if (jitter_delay_ms != jitter_delay_ms_) {
     77     jitter_delay_ms_ = jitter_delay_ms;
     78     // When in initial state, set current delay to minimum delay.
     79     if (current_delay_ms_ == 0) {
     80       current_delay_ms_ = jitter_delay_ms_;
     81     }
     82   }
     83 }
     84 
     85 void VCMTiming::UpdateCurrentDelay(uint32_t frame_timestamp) {
     86   CriticalSectionScoped cs(crit_sect_);
     87   uint32_t target_delay_ms = TargetDelayInternal();
     88 
     89   if (current_delay_ms_ == 0) {
     90     // Not initialized, set current delay to target.
     91     current_delay_ms_ = target_delay_ms;
     92   } else if (target_delay_ms != current_delay_ms_) {
     93     int64_t delay_diff_ms = static_cast<int64_t>(target_delay_ms) -
     94         current_delay_ms_;
     95     // Never change the delay with more than 100 ms every second. If we're
     96     // changing the delay in too large steps we will get noticeable freezes. By
     97     // limiting the change we can increase the delay in smaller steps, which
     98     // will be experienced as the video is played in slow motion. When lowering
     99     // the delay the video will be played at a faster pace.
    100     int64_t max_change_ms = 0;
    101     if (frame_timestamp < 0x0000ffff && prev_frame_timestamp_ > 0xffff0000) {
    102       // wrap
    103       max_change_ms = kDelayMaxChangeMsPerS * (frame_timestamp +
    104           (static_cast<int64_t>(1) << 32) - prev_frame_timestamp_) / 90000;
    105     } else {
    106       max_change_ms = kDelayMaxChangeMsPerS *
    107           (frame_timestamp - prev_frame_timestamp_) / 90000;
    108     }
    109     if (max_change_ms <= 0) {
    110       // Any changes less than 1 ms are truncated and
    111       // will be postponed. Negative change will be due
    112       // to reordering and should be ignored.
    113       return;
    114     }
    115     delay_diff_ms = std::max(delay_diff_ms, -max_change_ms);
    116     delay_diff_ms = std::min(delay_diff_ms, max_change_ms);
    117 
    118     current_delay_ms_ = current_delay_ms_ + static_cast<int32_t>(delay_diff_ms);
    119   }
    120   prev_frame_timestamp_ = frame_timestamp;
    121 }
    122 
    123 void VCMTiming::UpdateCurrentDelay(int64_t render_time_ms,
    124                                    int64_t actual_decode_time_ms) {
    125   CriticalSectionScoped cs(crit_sect_);
    126   uint32_t target_delay_ms = TargetDelayInternal();
    127   int64_t delayed_ms = actual_decode_time_ms -
    128       (render_time_ms - MaxDecodeTimeMs() - render_delay_ms_);
    129   if (delayed_ms < 0) {
    130     return;
    131   }
    132   if (current_delay_ms_ + delayed_ms <= target_delay_ms) {
    133     current_delay_ms_ += static_cast<uint32_t>(delayed_ms);
    134   } else {
    135     current_delay_ms_ = target_delay_ms;
    136   }
    137 }
    138 
    139 int32_t VCMTiming::StopDecodeTimer(uint32_t time_stamp,
    140                                    int64_t start_time_ms,
    141                                    int64_t now_ms) {
    142   CriticalSectionScoped cs(crit_sect_);
    143   int32_t time_diff_ms = codec_timer_.StopTimer(start_time_ms, now_ms);
    144   assert(time_diff_ms >= 0);
    145   last_decode_ms_ = time_diff_ms;
    146   return 0;
    147 }
    148 
    149 void VCMTiming::IncomingTimestamp(uint32_t time_stamp, int64_t now_ms) {
    150   CriticalSectionScoped cs(crit_sect_);
    151   ts_extrapolator_->Update(now_ms, time_stamp);
    152 }
    153 
    154 int64_t VCMTiming::RenderTimeMs(uint32_t frame_timestamp, int64_t now_ms)
    155     const {
    156   CriticalSectionScoped cs(crit_sect_);
    157   const int64_t render_time_ms = RenderTimeMsInternal(frame_timestamp, now_ms);
    158   return render_time_ms;
    159 }
    160 
    161 int64_t VCMTiming::RenderTimeMsInternal(uint32_t frame_timestamp,
    162                                         int64_t now_ms) const {
    163   int64_t estimated_complete_time_ms =
    164     ts_extrapolator_->ExtrapolateLocalTime(frame_timestamp);
    165   if (estimated_complete_time_ms == -1) {
    166     estimated_complete_time_ms = now_ms;
    167   }
    168 
    169   // Make sure that we have at least the playout delay.
    170   uint32_t actual_delay = std::max(current_delay_ms_, min_playout_delay_ms_);
    171   return estimated_complete_time_ms + actual_delay;
    172 }
    173 
    174 // Must be called from inside a critical section.
    175 int32_t VCMTiming::MaxDecodeTimeMs(FrameType frame_type /*= kVideoFrameDelta*/)
    176     const {
    177   const int32_t decode_time_ms = codec_timer_.RequiredDecodeTimeMs(frame_type);
    178   assert(decode_time_ms >= 0);
    179   return decode_time_ms;
    180 }
    181 
    182 uint32_t VCMTiming::MaxWaitingTime(int64_t render_time_ms, int64_t now_ms)
    183     const {
    184   CriticalSectionScoped cs(crit_sect_);
    185 
    186   const int64_t max_wait_time_ms = render_time_ms - now_ms -
    187       MaxDecodeTimeMs() - render_delay_ms_;
    188 
    189   if (max_wait_time_ms < 0) {
    190     return 0;
    191   }
    192   return static_cast<uint32_t>(max_wait_time_ms);
    193 }
    194 
    195 bool VCMTiming::EnoughTimeToDecode(uint32_t available_processing_time_ms)
    196     const {
    197   CriticalSectionScoped cs(crit_sect_);
    198   int32_t max_decode_time_ms = MaxDecodeTimeMs();
    199   if (max_decode_time_ms < 0) {
    200     // Haven't decoded any frames yet, try decoding one to get an estimate
    201     // of the decode time.
    202     return true;
    203   } else if (max_decode_time_ms == 0) {
    204     // Decode time is less than 1, set to 1 for now since
    205     // we don't have any better precision. Count ticks later?
    206     max_decode_time_ms = 1;
    207   }
    208   return static_cast<int32_t>(available_processing_time_ms) -
    209       max_decode_time_ms > 0;
    210 }
    211 
    212 uint32_t VCMTiming::TargetVideoDelay() const {
    213   CriticalSectionScoped cs(crit_sect_);
    214   return TargetDelayInternal();
    215 }
    216 
    217 uint32_t VCMTiming::TargetDelayInternal() const {
    218   return std::max(min_playout_delay_ms_,
    219       jitter_delay_ms_ + MaxDecodeTimeMs() + render_delay_ms_);
    220 }
    221 
    222 void VCMTiming::GetTimings(int* decode_ms,
    223                            int* max_decode_ms,
    224                            int* current_delay_ms,
    225                            int* target_delay_ms,
    226                            int* jitter_buffer_ms,
    227                            int* min_playout_delay_ms,
    228                            int* render_delay_ms) const {
    229   CriticalSectionScoped cs(crit_sect_);
    230   *decode_ms = last_decode_ms_;
    231   *max_decode_ms = MaxDecodeTimeMs();
    232   *current_delay_ms = current_delay_ms_;
    233   *target_delay_ms = TargetDelayInternal();
    234   *jitter_buffer_ms = jitter_delay_ms_;
    235   *min_playout_delay_ms = min_playout_delay_ms_;
    236   *render_delay_ms = render_delay_ms_;
    237 }
    238 
    239 }  // namespace webrtc
    240