congestion_controller.cc revision 44caf01c34d4fddec039f917c83fed7e0ce977b2
19ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org/*
29ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
39ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org *
49ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org *  Use of this source code is governed by a BSD-style license
59ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org *  that can be found in the LICENSE file in the root of the source
69ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org *  tree. An additional intellectual property rights grant can be found
79ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org *  in the file PATENTS.  All contributing project authors may
89ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
99ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org */
109ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org
1129b2219914a059fe5164c312e7cc6d1bf0b4e610andresp@webrtc.org#include "webrtc/video_engine/vie_channel_group.h"
129ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org
1329b2219914a059fe5164c312e7cc6d1bf0b4e610andresp@webrtc.org#include "webrtc/common.h"
1441907748cbc10cbd9a3531a1314735f5bca8eeb7henrik.lundin@webrtc.org#include "webrtc/experiments.h"
1529b2219914a059fe5164c312e7cc6d1bf0b4e610andresp@webrtc.org#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
1629b2219914a059fe5164c312e7cc6d1bf0b4e610andresp@webrtc.org#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
1729b2219914a059fe5164c312e7cc6d1bf0b4e610andresp@webrtc.org#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
1829b2219914a059fe5164c312e7cc6d1bf0b4e610andresp@webrtc.org#include "webrtc/modules/utility/interface/process_thread.h"
19a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
20a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org#include "webrtc/system_wrappers/interface/thread_annotations.h"
215ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org#include "webrtc/system_wrappers/interface/trace.h"
2229b2219914a059fe5164c312e7cc6d1bf0b4e610andresp@webrtc.org#include "webrtc/video_engine/call_stats.h"
2329b2219914a059fe5164c312e7cc6d1bf0b4e610andresp@webrtc.org#include "webrtc/video_engine/encoder_state_feedback.h"
2429b2219914a059fe5164c312e7cc6d1bf0b4e610andresp@webrtc.org#include "webrtc/video_engine/vie_channel.h"
2529b2219914a059fe5164c312e7cc6d1bf0b4e610andresp@webrtc.org#include "webrtc/video_engine/vie_encoder.h"
2629b2219914a059fe5164c312e7cc6d1bf0b4e610andresp@webrtc.org#include "webrtc/video_engine/vie_remb.h"
279ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org
289ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.orgnamespace webrtc {
29a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.orgnamespace {
30a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org
315ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.orgstatic const uint32_t kTimeOffsetSwitchThreshold = 30;
325ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org
33a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.orgclass WrappingBitrateEstimator : public RemoteBitrateEstimator {
34a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org public:
355ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org  WrappingBitrateEstimator(int engine_id, RemoteBitrateObserver* observer,
3641907748cbc10cbd9a3531a1314735f5bca8eeb7henrik.lundin@webrtc.org                           Clock* clock, ProcessThread* process_thread,
3741907748cbc10cbd9a3531a1314735f5bca8eeb7henrik.lundin@webrtc.org                           const Config& config)
38a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org      : observer_(observer),
39a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org        clock_(clock),
40a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org        process_thread_(process_thread),
41a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org        crit_sect_(CriticalSectionWrapper::CreateCriticalSection()),
425ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org        engine_id_(engine_id),
4341907748cbc10cbd9a3531a1314735f5bca8eeb7henrik.lundin@webrtc.org        min_bitrate_bps_(config.Get<RemoteBitrateEstimatorMinRate>().min_rate),
44a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org        rate_control_type_(kMimdControl),
45a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org        rbe_(RemoteBitrateEstimatorFactory().Create(observer_,
46a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org                                                    clock_,
47a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org                                                    rate_control_type_,
48e9abd591d73218e11a8bd3e7c72d4d7af9a3cea8henrik.lundin@webrtc.org                                                    min_bitrate_bps_)),
495ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org        using_absolute_send_time_(false),
505ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org        packets_since_absolute_send_time_(0) {
51a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org    assert(process_thread_ != NULL);
52a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org    process_thread_->RegisterModule(rbe_.get());
53a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  }
54a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  virtual ~WrappingBitrateEstimator() {
55a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org    process_thread_->DeRegisterModule(rbe_.get());
56a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  }
57a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org
58a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  virtual void IncomingPacket(int64_t arrival_time_ms,
59a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org                              int payload_size,
60de98478965979e3de7578caae192c5110bc578efstefan@webrtc.org                              const RTPHeader& header) {
61a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org    CriticalSectionScoped cs(crit_sect_.get());
62a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org    PickEstimatorFromHeader(header);
63a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org    rbe_->IncomingPacket(arrival_time_ms, payload_size, header);
64a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  }
65a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org
66a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  virtual int32_t Process() {
67a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org    assert(false && "Not supposed to register the WrappingBitrateEstimator!");
68a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org    return 0;
69a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  }
70a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org
71a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  virtual int32_t TimeUntilNextProcess() {
72a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org    assert(false && "Not supposed to register the WrappingBitrateEstimator!");
73a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org    return 0;
74a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  }
75a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org
76a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  virtual void OnRttUpdate(uint32_t rtt) {
77a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org    CriticalSectionScoped cs(crit_sect_.get());
78a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org    rbe_->OnRttUpdate(rtt);
79a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  }
80a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org
81a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  virtual void RemoveStream(unsigned int ssrc) {
82a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org    CriticalSectionScoped cs(crit_sect_.get());
83a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org    rbe_->RemoveStream(ssrc);
84a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  }
85a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org
86a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  virtual bool LatestEstimate(std::vector<unsigned int>* ssrcs,
87a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org                              unsigned int* bitrate_bps) const {
88a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org    CriticalSectionScoped cs(crit_sect_.get());
89a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org    return rbe_->LatestEstimate(ssrcs, bitrate_bps);
90a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  }
91a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org
921f64f067840212b3e5b67a0d6a50bcd805b5bc1ajiayl@webrtc.org  virtual bool GetStats(ReceiveBandwidthEstimatorStats* output) const {
931f64f067840212b3e5b67a0d6a50bcd805b5bc1ajiayl@webrtc.org    CriticalSectionScoped cs(crit_sect_.get());
941f64f067840212b3e5b67a0d6a50bcd805b5bc1ajiayl@webrtc.org    return rbe_->GetStats(output);
951f64f067840212b3e5b67a0d6a50bcd805b5bc1ajiayl@webrtc.org  }
961f64f067840212b3e5b67a0d6a50bcd805b5bc1ajiayl@webrtc.org
97a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org  void SetConfig(const webrtc::Config& config) {
98a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org    CriticalSectionScoped cs(crit_sect_.get());
99a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org    RateControlType new_control_type =
100a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org        config.Get<AimdRemoteRateControl>().enabled ? kAimdControl :
101a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org                                                      kMimdControl;
102a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org    if (new_control_type != rate_control_type_) {
103a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org      rate_control_type_ = new_control_type;
104a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org      PickEstimator();
105a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org    }
106a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org  }
107a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org
108a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org private:
109a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org  void PickEstimatorFromHeader(const RTPHeader& header)
110a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org      EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get()) {
1115ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org    if (header.extension.hasAbsoluteSendTime) {
1125ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org      // If we see AST in header, switch RBE strategy immediately.
1135ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org      if (!using_absolute_send_time_) {
1145ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org        WEBRTC_TRACE(kTraceStateInfo, kTraceVideo, ViEId(engine_id_),
1155ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org            "WrappingBitrateEstimator: Switching to absolute send time RBE.");
1165ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org        using_absolute_send_time_ = true;
117a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org        PickEstimator();
1185ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org      }
1195ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org      packets_since_absolute_send_time_ = 0;
1205ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org    } else {
1215ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org      // When we don't see AST, wait for a few packets before going back to TOF.
1225ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org      if (using_absolute_send_time_) {
1235ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org        ++packets_since_absolute_send_time_;
1245ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org        if (packets_since_absolute_send_time_ >= kTimeOffsetSwitchThreshold) {
1255ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org          WEBRTC_TRACE(kTraceStateInfo, kTraceVideo, ViEId(engine_id_),
1265ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org              "WrappingBitrateEstimator: Switching to transmission time offset "
1275ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org              "RBE.");
1285ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org          using_absolute_send_time_ = false;
129a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org          PickEstimator();
1305ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org        }
1315ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org      }
1325ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org    }
1335ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org  }
1345ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org
135a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org  // Instantiate RBE for Time Offset or Absolute Send Time extensions.
136a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org  void PickEstimator() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_.get()) {
137a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org    process_thread_->DeRegisterModule(rbe_.get());
138a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org    if (using_absolute_send_time_) {
139a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org      rbe_.reset(AbsoluteSendTimeRemoteBitrateEstimatorFactory().Create(
140a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org          observer_, clock_, rate_control_type_, min_bitrate_bps_));
141a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org    } else {
142a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org      rbe_.reset(RemoteBitrateEstimatorFactory().Create(
143a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org          observer_, clock_, rate_control_type_, min_bitrate_bps_));
144a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org    }
145a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org    process_thread_->RegisterModule(rbe_.get());
146a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org  }
147a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org
148a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  RemoteBitrateObserver* observer_;
149a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  Clock* clock_;
150a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  ProcessThread* process_thread_;
151a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  scoped_ptr<CriticalSectionWrapper> crit_sect_;
1525ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org  const int engine_id_;
153e9abd591d73218e11a8bd3e7c72d4d7af9a3cea8henrik.lundin@webrtc.org  const uint32_t min_bitrate_bps_;
154a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org  RateControlType rate_control_type_;
155a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  scoped_ptr<RemoteBitrateEstimator> rbe_;
1565ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org  bool using_absolute_send_time_;
1575ab756703ea32f2c2ff9878d6eae628c7380bc14pbos@webrtc.org  uint32_t packets_since_absolute_send_time_;
158a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org
159a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org  DISALLOW_IMPLICIT_CONSTRUCTORS(WrappingBitrateEstimator);
160a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org};
161a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org}  // namespace
1629ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org
16344caf01c34d4fddec039f917c83fed7e0ce977b2andresp@webrtc.orgChannelGroup::ChannelGroup(int engine_id,
16444caf01c34d4fddec039f917c83fed7e0ce977b2andresp@webrtc.org                           ProcessThread* process_thread,
16541907748cbc10cbd9a3531a1314735f5bca8eeb7henrik.lundin@webrtc.org                           const Config* config)
166b5865079868c4dec49571e7aef0aa52124b50c64stefan@webrtc.org    : remb_(new VieRemb()),
16744caf01c34d4fddec039f917c83fed7e0ce977b2andresp@webrtc.org      bitrate_controller_(
16844caf01c34d4fddec039f917c83fed7e0ce977b2andresp@webrtc.org          BitrateController::CreateBitrateController(Clock::GetRealTimeClock(),
16944caf01c34d4fddec039f917c83fed7e0ce977b2andresp@webrtc.org                                                     true)),
1707c894b7cc718773f32d21985ff33a64f9e13946emflodman@webrtc.org      call_stats_(new CallStats()),
1717c894b7cc718773f32d21985ff33a64f9e13946emflodman@webrtc.org      encoder_state_feedback_(new EncoderStateFeedback()),
17241907748cbc10cbd9a3531a1314735f5bca8eeb7henrik.lundin@webrtc.org      config_(config),
17341907748cbc10cbd9a3531a1314735f5bca8eeb7henrik.lundin@webrtc.org      own_config_(),
1747c894b7cc718773f32d21985ff33a64f9e13946emflodman@webrtc.org      process_thread_(process_thread) {
17541907748cbc10cbd9a3531a1314735f5bca8eeb7henrik.lundin@webrtc.org  if (!config) {
17641907748cbc10cbd9a3531a1314735f5bca8eeb7henrik.lundin@webrtc.org    own_config_.reset(new Config);
17741907748cbc10cbd9a3531a1314735f5bca8eeb7henrik.lundin@webrtc.org    config_ = own_config_.get();
17841907748cbc10cbd9a3531a1314735f5bca8eeb7henrik.lundin@webrtc.org  }
17941907748cbc10cbd9a3531a1314735f5bca8eeb7henrik.lundin@webrtc.org  assert(config_);  // Must have a valid config pointer here.
18041907748cbc10cbd9a3531a1314735f5bca8eeb7henrik.lundin@webrtc.org  remote_bitrate_estimator_.reset(
18144caf01c34d4fddec039f917c83fed7e0ce977b2andresp@webrtc.org      new WrappingBitrateEstimator(engine_id,
18244caf01c34d4fddec039f917c83fed7e0ce977b2andresp@webrtc.org                                   remb_.get(),
18344caf01c34d4fddec039f917c83fed7e0ce977b2andresp@webrtc.org                                   Clock::GetRealTimeClock(),
18444caf01c34d4fddec039f917c83fed7e0ce977b2andresp@webrtc.org                                   process_thread,
18541907748cbc10cbd9a3531a1314735f5bca8eeb7henrik.lundin@webrtc.org                                   *config_)),
18644caf01c34d4fddec039f917c83fed7e0ce977b2andresp@webrtc.org      call_stats_->RegisterStatsObserver(remote_bitrate_estimator_.get());
18744caf01c34d4fddec039f917c83fed7e0ce977b2andresp@webrtc.org
1887c894b7cc718773f32d21985ff33a64f9e13946emflodman@webrtc.org  process_thread->RegisterModule(call_stats_.get());
18944caf01c34d4fddec039f917c83fed7e0ce977b2andresp@webrtc.org  process_thread->RegisterModule(bitrate_controller_.get());
19049888ce42858e308c7b0575697f06e2483a34daepwestin@webrtc.org}
1919ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org
1929ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.orgChannelGroup::~ChannelGroup() {
19344caf01c34d4fddec039f917c83fed7e0ce977b2andresp@webrtc.org  process_thread_->DeRegisterModule(bitrate_controller_.get());
1946cd201cf31dc8e50bf815139b0c9fdc83d3ba2bfandrew@webrtc.org  process_thread_->DeRegisterModule(call_stats_.get());
19544caf01c34d4fddec039f917c83fed7e0ce977b2andresp@webrtc.org  call_stats_->DeregisterStatsObserver(remote_bitrate_estimator_.get());
1969ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org  assert(channels_.empty());
1979ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org  assert(!remb_->InUse());
1989ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org}
199a6db54d4c9a2bfc703bc208eb5cbc19505e9cef3solenberg@webrtc.org
2009ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.orgvoid ChannelGroup::AddChannel(int channel_id) {
2019ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org  channels_.insert(channel_id);
2029ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org}
2039ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org
2049354cc965c04a0c79ea36622043751596a6fd015stefan@webrtc.orgvoid ChannelGroup::RemoveChannel(int channel_id, unsigned int ssrc) {
2059ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org  channels_.erase(channel_id);
2069354cc965c04a0c79ea36622043751596a6fd015stefan@webrtc.org  remote_bitrate_estimator_->RemoveStream(ssrc);
2079ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org}
2089ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org
2099ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.orgbool ChannelGroup::HasChannel(int channel_id) {
2109ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org  return channels_.find(channel_id) != channels_.end();
2119ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org}
2129ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org
2139ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.orgbool ChannelGroup::Empty() {
2149ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org  return channels_.empty();
2159ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org}
2169ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org
217cb89c6f91491e683e47e5505536c154c905d5194bjornv@webrtc.orgBitrateController* ChannelGroup::GetBitrateController() {
218cb89c6f91491e683e47e5505536c154c905d5194bjornv@webrtc.org  return bitrate_controller_.get();
219f72881406fba86adc2d649bb1caee0d5604f9339stefan@webrtc.org}
220f72881406fba86adc2d649bb1caee0d5604f9339stefan@webrtc.org
2219354cc965c04a0c79ea36622043751596a6fd015stefan@webrtc.orgRemoteBitrateEstimator* ChannelGroup::GetRemoteBitrateEstimator() {
2229354cc965c04a0c79ea36622043751596a6fd015stefan@webrtc.org  return remote_bitrate_estimator_.get();
2239354cc965c04a0c79ea36622043751596a6fd015stefan@webrtc.org}
2249354cc965c04a0c79ea36622043751596a6fd015stefan@webrtc.org
2257c894b7cc718773f32d21985ff33a64f9e13946emflodman@webrtc.orgCallStats* ChannelGroup::GetCallStats() {
2267c894b7cc718773f32d21985ff33a64f9e13946emflodman@webrtc.org  return call_stats_.get();
2277c894b7cc718773f32d21985ff33a64f9e13946emflodman@webrtc.org}
2287c894b7cc718773f32d21985ff33a64f9e13946emflodman@webrtc.org
229aca26292aeb35c91e70dd22d5923bf74ce68fa30mflodman@webrtc.orgEncoderStateFeedback* ChannelGroup::GetEncoderStateFeedback() {
230aca26292aeb35c91e70dd22d5923bf74ce68fa30mflodman@webrtc.org  return encoder_state_feedback_.get();
231aca26292aeb35c91e70dd22d5923bf74ce68fa30mflodman@webrtc.org}
232aca26292aeb35c91e70dd22d5923bf74ce68fa30mflodman@webrtc.org
233aeff4f30036ed477dd96445d8060c3fa177ffe22mflodman@webrtc.orgbool ChannelGroup::SetChannelRembStatus(int channel_id, bool sender,
234aeff4f30036ed477dd96445d8060c3fa177ffe22mflodman@webrtc.org                                        bool receiver, ViEChannel* channel) {
2359ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org  // Update the channel state.
2369ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org  if (sender || receiver) {
2379ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org    if (!channel->EnableRemb(true)) {
2389ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org      return false;
2399ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org    }
240aeff4f30036ed477dd96445d8060c3fa177ffe22mflodman@webrtc.org  } else {
2419ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org    channel->EnableRemb(false);
2429ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org  }
2439354cc965c04a0c79ea36622043751596a6fd015stefan@webrtc.org  // Update the REMB instance with necessary RTP modules.
2449ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org  RtpRtcp* rtp_module = channel->rtp_rtcp();
2459ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org  if (sender) {
2469ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org    remb_->AddRembSender(rtp_module);
2479ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org  } else {
2489ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org    remb_->RemoveRembSender(rtp_module);
2499ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org  }
2509ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org  if (receiver) {
2519ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org    remb_->AddReceiveChannel(rtp_module);
2529ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org  } else {
2539ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org    remb_->RemoveReceiveChannel(rtp_module);
2549ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org  }
2559ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org  return true;
2569ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org}
257a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org
258a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.orgvoid ChannelGroup::SetBandwidthEstimationConfig(const webrtc::Config& config) {
259a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org  WrappingBitrateEstimator* estimator =
260a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org      static_cast<WrappingBitrateEstimator*>(remote_bitrate_estimator_.get());
261a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org  estimator->SetConfig(config);
262a16147c03726e54b2ecb1e48c7171a33d83a5261stefan@webrtc.org}
2639ec883e8bd334acfc246741f7a2cca3e55d7f5b2mflodman@webrtc.org}  // namespace webrtc
264