2015-03-04 12:24:26 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2015-11-12 21:02:42 -08:00
|
|
|
#ifndef WEBRTC_CALL_BITRATE_ALLOCATOR_H_
|
|
|
|
|
#define WEBRTC_CALL_BITRATE_ALLOCATOR_H_
|
2015-03-04 12:24:26 +00:00
|
|
|
|
2016-03-12 06:10:44 -08:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
2015-03-04 12:24:26 +00:00
|
|
|
#include <list>
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
2016-01-20 02:32:54 -08:00
|
|
|
#include "webrtc/base/criticalsection.h"
|
2015-03-04 12:24:26 +00:00
|
|
|
#include "webrtc/base/thread_annotations.h"
|
|
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2016-03-11 15:44:32 +01:00
|
|
|
// Used by all send streams with adaptive bitrate, to get the currently
|
|
|
|
|
// allocated bitrate for the send stream. The current network properties are
|
|
|
|
|
// given at the same time, to let the send stream decide about possible loss
|
|
|
|
|
// protection.
|
|
|
|
|
class BitrateAllocatorObserver {
|
|
|
|
|
public:
|
|
|
|
|
virtual void OnBitrateUpdated(uint32_t bitrate_bps,
|
|
|
|
|
uint8_t fraction_loss,
|
|
|
|
|
int64_t rtt) = 0;
|
|
|
|
|
virtual ~BitrateAllocatorObserver() {}
|
|
|
|
|
};
|
2015-03-04 12:24:26 +00:00
|
|
|
|
2016-03-11 15:44:32 +01:00
|
|
|
// Usage: this class will register multiple RtcpBitrateObserver's one at each
|
|
|
|
|
// RTCP module. It will aggregate the results and run one bandwidth estimation
|
|
|
|
|
// and push the result to the encoders via BitrateAllocatorObserver(s).
|
2015-03-04 12:24:26 +00:00
|
|
|
class BitrateAllocator {
|
|
|
|
|
public:
|
|
|
|
|
BitrateAllocator();
|
|
|
|
|
|
2016-03-11 15:44:32 +01:00
|
|
|
// Allocate target_bitrate across the registered BitrateAllocatorObservers.
|
2015-11-05 04:25:49 -08:00
|
|
|
// Returns actual bitrate allocated (might be higher than target_bitrate if
|
|
|
|
|
// for instance EnforceMinBitrate() is enabled.
|
|
|
|
|
uint32_t OnNetworkChanged(uint32_t target_bitrate,
|
|
|
|
|
uint8_t fraction_loss,
|
|
|
|
|
int64_t rtt);
|
2015-03-04 12:24:26 +00:00
|
|
|
|
|
|
|
|
// Set the start and max send bitrate used by the bandwidth management.
|
|
|
|
|
//
|
2015-09-15 15:08:03 +02:00
|
|
|
// |observer| updates bitrates if already in use.
|
|
|
|
|
// |min_bitrate_bps| = 0 equals no min bitrate.
|
|
|
|
|
// |max_bitrate_bps| = 0 equals no max bitrate.
|
2016-05-13 01:43:51 -07:00
|
|
|
// |enforce_min_bitrate| = 'true' will allocate at least |min_bitrate_bps| for
|
|
|
|
|
// this observer, even if the BWE is too low, 'false' will allocate 0 to
|
|
|
|
|
// the observer if BWE doesn't allow |min_bitrate_bps|.
|
|
|
|
|
// Returns bitrate allocated for |observer|.
|
2016-03-11 15:44:32 +01:00
|
|
|
int AddObserver(BitrateAllocatorObserver* observer,
|
|
|
|
|
uint32_t min_bitrate_bps,
|
2016-05-13 01:43:51 -07:00
|
|
|
uint32_t max_bitrate_bps,
|
|
|
|
|
bool enforce_min_bitrate);
|
2015-03-04 12:24:26 +00:00
|
|
|
|
2016-03-11 15:44:32 +01:00
|
|
|
void RemoveObserver(BitrateAllocatorObserver* observer);
|
2015-03-04 12:24:26 +00:00
|
|
|
|
2016-05-13 01:43:51 -07:00
|
|
|
private:
|
|
|
|
|
struct ObserverConfig {
|
|
|
|
|
ObserverConfig(BitrateAllocatorObserver* observer,
|
|
|
|
|
uint32_t min_bitrate_bps,
|
|
|
|
|
uint32_t max_bitrate_bps,
|
|
|
|
|
bool enforce_min_bitrate)
|
|
|
|
|
: observer(observer),
|
|
|
|
|
min_bitrate_bps(min_bitrate_bps),
|
|
|
|
|
max_bitrate_bps(max_bitrate_bps),
|
|
|
|
|
enforce_min_bitrate(enforce_min_bitrate) {}
|
|
|
|
|
BitrateAllocatorObserver* const observer;
|
|
|
|
|
uint32_t min_bitrate_bps;
|
|
|
|
|
uint32_t max_bitrate_bps;
|
|
|
|
|
bool enforce_min_bitrate;
|
|
|
|
|
};
|
|
|
|
|
|
2015-03-04 12:24:26 +00:00
|
|
|
// This method controls the behavior when the available bitrate is lower than
|
|
|
|
|
// the minimum bitrate, or the sum of minimum bitrates.
|
|
|
|
|
// When true, the bitrate will never be set lower than the minimum bitrate(s).
|
|
|
|
|
// When false, the bitrate observers will be allocated rates up to their
|
|
|
|
|
// respective minimum bitrate, satisfying one observer after the other.
|
2016-05-13 01:43:51 -07:00
|
|
|
void EnforceMinBitrate(bool enforce_min_bitrate)
|
|
|
|
|
EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
|
2015-03-04 12:24:26 +00:00
|
|
|
|
2016-05-13 01:43:51 -07:00
|
|
|
typedef std::list<ObserverConfig> ObserverConfigList;
|
|
|
|
|
ObserverConfigList::iterator FindObserverConfig(
|
2016-03-11 15:44:32 +01:00
|
|
|
const BitrateAllocatorObserver* observer)
|
|
|
|
|
EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
|
2016-05-13 01:43:51 -07:00
|
|
|
|
|
|
|
|
typedef std::multimap<uint32_t, const ObserverConfig*> ObserverSortingMap;
|
|
|
|
|
typedef std::map<BitrateAllocatorObserver*, int> ObserverAllocation;
|
|
|
|
|
|
|
|
|
|
ObserverAllocation AllocateBitrates() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
|
|
|
|
|
ObserverAllocation NormalRateAllocation(uint32_t bitrate,
|
2015-03-26 11:11:06 +01:00
|
|
|
uint32_t sum_min_bitrates)
|
2015-03-04 12:24:26 +00:00
|
|
|
EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
|
|
|
|
|
|
2016-05-13 01:43:51 -07:00
|
|
|
ObserverAllocation ZeroRateAllocation() EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
|
|
|
|
|
ObserverAllocation LowRateAllocation(uint32_t bitrate)
|
2015-03-04 12:24:26 +00:00
|
|
|
EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
|
|
|
|
|
|
2016-01-25 03:52:44 -08:00
|
|
|
rtc::CriticalSection crit_sect_;
|
2015-03-26 11:11:06 +01:00
|
|
|
// Stored in a list to keep track of the insertion order.
|
2016-05-13 01:43:51 -07:00
|
|
|
ObserverConfigList bitrate_observer_configs_;
|
2015-03-04 12:24:26 +00:00
|
|
|
bool enforce_min_bitrate_ GUARDED_BY(crit_sect_);
|
2015-03-26 11:11:06 +01:00
|
|
|
uint32_t last_bitrate_bps_ GUARDED_BY(crit_sect_);
|
|
|
|
|
uint8_t last_fraction_loss_ GUARDED_BY(crit_sect_);
|
|
|
|
|
int64_t last_rtt_ GUARDED_BY(crit_sect_);
|
2015-03-04 12:24:26 +00:00
|
|
|
};
|
|
|
|
|
} // namespace webrtc
|
2015-11-12 21:02:42 -08:00
|
|
|
#endif // WEBRTC_CALL_BITRATE_ALLOCATOR_H_
|