-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adaptive bitrate infrastructure (#3544)
* Push burst bitrate to 100mbps, and stop clamping fps to 45. * Fix FPS logging and increase maximum instantaneous framerate to 90 * Change burst bitrate to 100mbps * fallback to old settings if nacking a lot * change to good starting bitrate * change to good starting bitrate * change more thing sto good bitrate * more renaming of bitrate * everything in bits not megabits * good bitrate * bitrate.c file with some bitrate code * rename bitrate function * remove extraneous typo * video ring buffer * bad rebase bitrate * clang-formta * divide by int * move the bad bitrate to bitrate.c * documentation * actually, don't switch back to 16/100 * clang-format? * more infra * changed return value * typos * sorry variables now extern * initialize throughput to -1, I guess * remove burst bitrate refs * remove unused variable Co-authored-by: Nicholas Pipitone <npip99@gmail.com> Co-authored-by: Yoel Hawa <yoelhawa@gmail.com>
- Loading branch information
1 parent
10ed548
commit 6aede74
Showing
8 changed files
with
169 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* Copyright Fractal Computers, Inc. 2021 | ||
* @file bitrate.h | ||
* @brief This file contains the client's adaptive bitrate code. Any algorithms we are using for | ||
predicting bitrate should be stored here. | ||
============================ | ||
Usage | ||
============================ | ||
Place to put any predictive/adaptive bitrate algorithms. In the current setup, each algorithm is a | ||
function that takes in inputs through a BitrateStatistics struct. The function is responsible for | ||
maintaining any internal state necessary for the algorithm (possibly through the use of custom types | ||
or helpers), and should update max_bitrate when necessary. To change the algorithm the client uses, | ||
set calculate_new_bitrate to the desired algorithm. | ||
*/ | ||
|
||
/* | ||
============================ | ||
Includes | ||
============================ | ||
*/ | ||
#include "bitrate.h" | ||
|
||
volatile int max_bitrate = STARTING_BITRATE; | ||
volatile int max_burst_bitrate = STARTING_BURST_BITRATE; | ||
#define BAD_BITRATE 10400000 | ||
#define BAD_BURST_BITRATE 31800000 | ||
|
||
/* | ||
============================ | ||
Private Functions | ||
============================ | ||
*/ | ||
Bitrates fallback_bitrate(BitrateStatistics stats); | ||
Bitrates ewma_bitrate(BitrateStatistics stats); | ||
|
||
/* | ||
============================ | ||
Private Function Implementations | ||
============================ | ||
*/ | ||
Bitrates fallback_bitrate(BitrateStatistics stats) { | ||
/* | ||
Switches between two sets of bitrate/burst bitrate: the default of 16mbps/100mbps and a | ||
fallback of 10mbps/30mbps. We fall back if we've nacked a lot in the last second. | ||
Arguments: | ||
stats (BitrateStatistics): struct containing the average number of nacks per second | ||
since the last time this function was called | ||
*/ | ||
static Bitrates bitrates; | ||
if (stats.num_nacks_per_second > 6 && max_bitrate != BAD_BITRATE) { | ||
bitrates.bitrate = BAD_BITRATE; | ||
bitrates.burst_bitrate = BAD_BURST_BITRATE; | ||
} else { | ||
bitrates.bitrate = max_bitrate; | ||
bitrates.burst_bitrate = max_burst_bitrate; | ||
} | ||
return bitrates; | ||
} | ||
|
||
Bitrates ewma_bitrate(BitrateStatistics stats) { | ||
/* | ||
Keeps an exponentially weighted moving average of the throughput per second the client is | ||
getting, and uses that to predict a good bitrate to ask the server for. | ||
Arguments: | ||
stats (BitrateStatistics): struct containing the throughput per second of the client | ||
*/ | ||
static const double alpha = 0.8; | ||
// because the max bitrate of the encoder is usually larger than the actual amount of data we | ||
// get from the server | ||
static const double bitrate_throughput_ratio = 1.25; | ||
static int throughput = -1; | ||
static Bitrates bitrates; | ||
if (throughput == -1) { | ||
throughput = (int)(STARTING_BITRATE / bitrate_throughput_ratio); | ||
} | ||
// sanity check to make sure we're not sending it negative bitrate | ||
if (stats.throughput_per_second >= 0) { | ||
throughput = (int)(alpha * throughput + (1 - alpha) * stats.throughput_per_second); | ||
bitrates.bitrate = (int)(bitrate_throughput_ratio * throughput); | ||
} | ||
bitrates.burst_bitrate = max_burst_bitrate; | ||
return bitrates; | ||
} | ||
|
||
/* | ||
============================ | ||
Public Function Implementations | ||
============================ | ||
*/ | ||
|
||
BitrateCalculator calculate_new_bitrate = fallback_bitrate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#ifndef CLIENT_BITRATE_H | ||
#define CLIENT_BITRATE_H | ||
/** | ||
* Copyright Fractal Computers, Inc. 2021 | ||
* @file bitrate.h | ||
* @brief This file contains the client's adaptive bitrate code. Any algorithms we are using for | ||
predicting bitrate should be stored here. | ||
============================ | ||
Usage | ||
============================ | ||
The client should periodically call calculate_new_bitrate with any data rrequested, such as | ||
throughput or nack data. This will update max_bitrate and max_burst_bitrate if needed. | ||
*/ | ||
|
||
/* | ||
============================ | ||
Includes | ||
============================ | ||
*/ | ||
|
||
#include <fractal/core/fractal.h> | ||
|
||
/* | ||
============================ | ||
Custom Types | ||
============================ | ||
*/ | ||
|
||
typedef struct BitrateStatistics { | ||
int num_nacks_per_second; | ||
int throughput_per_second; | ||
} BitrateStatistics; | ||
|
||
typedef struct Bitrates { | ||
int bitrate; | ||
int burst_bitrate; | ||
} Bitrates; | ||
|
||
typedef Bitrates (*BitrateCalculator)(BitrateStatistics); | ||
|
||
/* | ||
============================ | ||
Public Functions | ||
============================ | ||
*/ | ||
|
||
/** | ||
* @brief Update max_bitrate and max_burst_bitrate with the latest client data. This function | ||
* does not trigger any client bitrate updates. | ||
* | ||
* @param stats A struct containing any information we might need to update bitrate. | ||
*/ | ||
extern BitrateCalculator calculate_new_bitrate; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters