Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / video_coding / main / source / media_optimization.cc
index 4dc7225..0d9a4bd 100644 (file)
@@ -75,7 +75,8 @@ struct MediaOptimization::EncodedFrameSample {
 };
 
 MediaOptimization::MediaOptimization(Clock* clock)
-    : clock_(clock),
+    : crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
+      clock_(clock),
       max_bit_rate_(0),
       send_codec_type_(kVideoCodecUnknown),
       codec_width_(0),
@@ -113,7 +114,9 @@ MediaOptimization::~MediaOptimization(void) {
 }
 
 void MediaOptimization::Reset() {
-  SetEncodingData(kVideoCodecUnknown, 0, 0, 0, 0, 0, 0, max_payload_size_);
+  CriticalSectionScoped lock(crit_sect_.get());
+  SetEncodingDataInternal(
+      kVideoCodecUnknown, 0, 0, 0, 0, 0, 0, max_payload_size_);
   memset(incoming_frame_times_, -1, sizeof(incoming_frame_times_));
   incoming_frame_rate_ = 0.0;
   frame_dropper_->Reset();
@@ -145,6 +148,25 @@ void MediaOptimization::SetEncodingData(VideoCodecType send_codec_type,
                                         uint16_t height,
                                         int num_layers,
                                         int32_t mtu) {
+  CriticalSectionScoped lock(crit_sect_.get());
+  SetEncodingDataInternal(send_codec_type,
+                          max_bit_rate,
+                          frame_rate,
+                          target_bitrate,
+                          width,
+                          height,
+                          num_layers,
+                          mtu);
+}
+
+void MediaOptimization::SetEncodingDataInternal(VideoCodecType send_codec_type,
+                                                int32_t max_bit_rate,
+                                                uint32_t frame_rate,
+                                                uint32_t target_bitrate,
+                                                uint16_t width,
+                                                uint16_t height,
+                                                int num_layers,
+                                                int32_t mtu) {
   // Everything codec specific should be reset here since this means the codec
   // has changed. If native dimension values have changed, then either user
   // initiated change, or QM initiated change. Will be able to determine only
@@ -181,6 +203,7 @@ uint32_t MediaOptimization::SetTargetRates(
     uint32_t round_trip_time_ms,
     VCMProtectionCallback* protection_callback,
     VCMQMSettingsCallback* qmsettings_callback) {
+  CriticalSectionScoped lock(crit_sect_.get());
   // TODO(holmer): Consider putting this threshold only on the video bitrate,
   // and not on protection.
   if (max_bit_rate_ > 0 &&
@@ -194,7 +217,7 @@ uint32_t MediaOptimization::SetTargetRates(
   loss_prot_logic_->UpdateResidualPacketLoss(static_cast<float>(fraction_lost));
 
   // Get frame rate for encoder: this is the actual/sent frame rate.
-  float actual_frame_rate = SentFrameRate();
+  float actual_frame_rate = SentFrameRateInternal();
 
   // Sanity check.
   if (actual_frame_rate < 1.0) {
@@ -297,6 +320,7 @@ uint32_t MediaOptimization::SetTargetRates(
 
 void MediaOptimization::EnableProtectionMethod(bool enable,
                                                VCMProtectionMethodEnum method) {
+  CriticalSectionScoped lock(crit_sect_.get());
   bool updated = false;
   if (enable) {
     updated = loss_prot_logic_->SetMethod(method);
@@ -309,17 +333,28 @@ void MediaOptimization::EnableProtectionMethod(bool enable,
 }
 
 uint32_t MediaOptimization::InputFrameRate() {
+  CriticalSectionScoped lock(crit_sect_.get());
+  return InputFrameRateInternal();
+}
+
+uint32_t MediaOptimization::InputFrameRateInternal() {
   ProcessIncomingFrameRate(clock_->TimeInMilliseconds());
   return uint32_t(incoming_frame_rate_ + 0.5f);
 }
 
 uint32_t MediaOptimization::SentFrameRate() {
+  CriticalSectionScoped lock(crit_sect_.get());
+  return SentFrameRateInternal();
+}
+
+uint32_t MediaOptimization::SentFrameRateInternal() {
   PurgeOldFrameSamples(clock_->TimeInMilliseconds());
   UpdateSentFramerate();
   return avg_sent_framerate_;
 }
 
 uint32_t MediaOptimization::SentBitRate() {
+  CriticalSectionScoped lock(crit_sect_.get());
   const int64_t now_ms = clock_->TimeInMilliseconds();
   PurgeOldFrameSamples(now_ms);
   UpdateSentBitrate(now_ms);
@@ -327,6 +362,7 @@ uint32_t MediaOptimization::SentBitRate() {
 }
 
 VCMFrameCount MediaOptimization::SentFrameCount() {
+  CriticalSectionScoped lock(crit_sect_.get());
   VCMFrameCount count;
   count.numDeltaFrames = delta_frame_cnt_;
   count.numKeyFrames = key_frame_cnt_;
@@ -336,6 +372,7 @@ VCMFrameCount MediaOptimization::SentFrameCount() {
 int32_t MediaOptimization::UpdateWithEncodedData(int encoded_length,
                                                  uint32_t timestamp,
                                                  FrameType encoded_frame_type) {
+  CriticalSectionScoped lock(crit_sect_.get());
   const int64_t now_ms = clock_->TimeInMilliseconds();
   PurgeOldFrameSamples(now_ms);
   if (encoded_frame_samples_.size() > 0 &&
@@ -386,22 +423,55 @@ int32_t MediaOptimization::UpdateWithEncodedData(int encoded_length,
   return VCM_OK;
 }
 
-void MediaOptimization::EnableQM(bool enable) { enable_qm_ = enable; }
+void MediaOptimization::EnableQM(bool enable) {
+  CriticalSectionScoped lock(crit_sect_.get());
+  enable_qm_ = enable;
+}
 
 void MediaOptimization::EnableFrameDropper(bool enable) {
+  CriticalSectionScoped lock(crit_sect_.get());
   frame_dropper_->Enable(enable);
 }
 
+void MediaOptimization::SuspendBelowMinBitrate(int threshold_bps,
+                                               int window_bps) {
+  CriticalSectionScoped lock(crit_sect_.get());
+  assert(threshold_bps > 0 && window_bps >= 0);
+  suspension_threshold_bps_ = threshold_bps;
+  suspension_window_bps_ = window_bps;
+  suspension_enabled_ = true;
+  video_suspended_ = false;
+}
+
+bool MediaOptimization::IsVideoSuspended() const {
+  CriticalSectionScoped lock(crit_sect_.get());
+  return video_suspended_;
+}
+
 bool MediaOptimization::DropFrame() {
+  CriticalSectionScoped lock(crit_sect_.get());
   UpdateIncomingFrameRate();
   // Leak appropriate number of bytes.
-  frame_dropper_->Leak((uint32_t)(InputFrameRate() + 0.5f));
+  frame_dropper_->Leak((uint32_t)(InputFrameRateInternal() + 0.5f));
   if (video_suspended_) {
     return true;  // Drop all frames when muted.
   }
   return frame_dropper_->DropFrame();
 }
 
+void MediaOptimization::UpdateContentData(
+    const VideoContentMetrics* content_metrics) {
+  CriticalSectionScoped lock(crit_sect_.get());
+  // Updating content metrics.
+  if (content_metrics == NULL) {
+    // Disable QM if metrics are NULL.
+    enable_qm_ = false;
+    qm_resolution_->Reset();
+  } else {
+    content_->UpdateContentData(content_metrics);
+  }
+}
+
 void MediaOptimization::UpdateIncomingFrameRate() {
   int64_t now = clock_->TimeInMilliseconds();
   if (incoming_frame_times_[0] == 0) {
@@ -416,18 +486,6 @@ void MediaOptimization::UpdateIncomingFrameRate() {
   ProcessIncomingFrameRate(now);
 }
 
-void MediaOptimization::UpdateContentData(
-    const VideoContentMetrics* content_metrics) {
-  // Updating content metrics.
-  if (content_metrics == NULL) {
-    // Disable QM if metrics are NULL.
-    enable_qm_ = false;
-    qm_resolution_->Reset();
-  } else {
-    content_->UpdateContentData(content_metrics);
-  }
-}
-
 int32_t MediaOptimization::SelectQuality(
     VCMQMSettingsCallback* video_qmsettings_callback) {
   // Reset quantities for QM select.
@@ -458,17 +516,6 @@ int32_t MediaOptimization::SelectQuality(
   return VCM_OK;
 }
 
-void MediaOptimization::SuspendBelowMinBitrate(int threshold_bps,
-                                               int window_bps) {
-  assert(threshold_bps > 0 && window_bps >= 0);
-  suspension_threshold_bps_ = threshold_bps;
-  suspension_window_bps_ = window_bps;
-  suspension_enabled_ = true;
-  video_suspended_ = false;
-}
-
-bool MediaOptimization::IsVideoSuspended() const { return video_suspended_; }
-
 void MediaOptimization::PurgeOldFrameSamples(int64_t now_ms) {
   while (!encoded_frame_samples_.empty()) {
     if (now_ms - encoded_frame_samples_.front().time_complete_ms >