chromium/third_party/openscreen/src/cast/streaming/impl/bandwidth_estimator.h

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CAST_STREAMING_IMPL_BANDWIDTH_ESTIMATOR_H_
#define CAST_STREAMING_IMPL_BANDWIDTH_ESTIMATOR_H_

#include <stdint.h>

#include <limits>

#include "platform/api/time.h"

namespace openscreen::cast {

// Tracks send attempts and successful receives, and then computes a total
// network bandwith estimate.
//
// Two metrics are tracked by the BandwidthEstimator, over a "recent history"
// time window:
//
//   1. The number of packets sent during bursts (see SenderPacketRouter for
//      explanation of what a "burst" is). These track when the network was
//      actually in-use for transmission and the magnitude of each burst. When
//      computing bandwidth, the estimator assumes the timeslices where the
//      network was not in-use could have been used to send even more bytes at
//      the same rate.
//
//   2. Successful receipt of payload bytes over time, or a lack thereof.
//      Packets that include acknowledgements from the Receivers are providing
//      proof of the successful receipt of payload bytes. All other packets
//      provide proof of network connectivity over time, and are used to
//      identify periods of time where nothing was received.
//
// The BandwidthEstimator assumes a simplified model for streaming over the
// network. The model does not include any detailed knowledge about things like
// protocol overhead, packet re-transmits, parasitic bufferring, network
// reliability, etc. Instead, it automatically accounts for all such things by
// looking at what's actually leaving the Senders and what's actually making it
// to the Receivers.
//
// This simplified model does produce some known inaccuracies in the resulting
// estimations. If no data has recently been transmitted (or been received),
// estimations cannot be provided. If the transmission rate is near (or
// exceeding) the network's capacity, the estimations will be very accurate. In
// between those two extremes, the logic will tend to under-estimate the
// network's capacity. However, those under-estimates will still be far larger
// than the current transmission rate.
//
// Thus, these estimates can be used effectively as a control signal for
// congestion control in upstream code modules. The logic computing the media's
// encoding target bitrate should be adjusted in realtime using a TCP-like
// congestion control algorithm:
//
//   1. When the estimated bitrate is less than the current encoding target
//      bitrate, aggressively and immediately decrease the encoding bitrate.
//
//   2. When the estimated bitrate is more than the current encoding target
//      bitrate, gradually increase the encoding bitrate (up to the maximum
//      that is reasonable for the application).
class BandwidthEstimator {};

}  // namespace openscreen::cast

#endif  // CAST_STREAMING_IMPL_BANDWIDTH_ESTIMATOR_H_