-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- define config throttler.maxDataRate as max limit on transcoder usage. * measured in times x normal fps. e.g. if maxDataRate = 2 we expect no more than twice the original throughput. * By default maxDataRate is set to Infinity and no data rate limit is performed.
- Loading branch information
1 parent
cd20f1c
commit f42792a
Showing
8 changed files
with
130 additions
and
5 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// throttler.c | ||
// live_transcoder | ||
// | ||
// | ||
|
||
|
||
#include "../transcode/transcode_session.h" | ||
#include "throttler.h" | ||
#include "json_parser.h" | ||
|
||
static void doThrottle(float maxDataRate, | ||
int coldSeconds, | ||
int minThrottleWaitMs, | ||
samples_stats_t *stats, | ||
AVRational targetFramerate); | ||
|
||
int | ||
throttler_init(samples_stats_t *stats,throttler_t *throttler) { | ||
json_value_t *config = GetConfig(); | ||
json_get_double(config,"throttler.maxDataRate",INFINITY,(double*)&throttler->maxDataRate); | ||
*(bool*)&throttler->enabled = throttler->maxDataRate < INFINITY; | ||
if(throttler->enabled){ | ||
json_get_int(config,"throttler.coldSeconds",0,(int*)&throttler->coldSeconds); | ||
json_get_int(config,"throttler.minThrottleWaitMs",1,(int*)&throttler->minThrottleWaitMs); | ||
throttler->stats = stats; | ||
} | ||
return 0; | ||
} | ||
|
||
void | ||
throttler_process(throttler_t *throttler,transcode_session_t *transcode_session) { | ||
if(throttler && throttler->maxDataRate < INFINITY) { | ||
const transcode_mediaInfo_t *mediaInfo = transcode_session ? transcode_session->currentMediaInfo : NULL; | ||
if(mediaInfo && mediaInfo->codecParams){ | ||
const bool isVideo = mediaInfo->codecParams->codec_type == AVMEDIA_TYPE_VIDEO; | ||
const AVRational frameRate = isVideo ? mediaInfo->frameRate : | ||
(AVRational){ .num = mediaInfo->codecParams->sample_rate , | ||
.den = 1024 }; | ||
|
||
doThrottle(throttler->maxDataRate, | ||
throttler->coldSeconds, | ||
throttler->minThrottleWaitMs, | ||
throttler->stats, | ||
frameRate); | ||
} | ||
} | ||
} | ||
|
||
static | ||
void | ||
doThrottle(float maxDataRate, | ||
int coldSeconds, | ||
int minThrottleWaitMs, | ||
samples_stats_t *stats, | ||
AVRational targetFramerate) | ||
{ | ||
|
||
if(targetFramerate.den > 0 && targetFramerate.num > 0) { | ||
float currentDataRate; | ||
|
||
samples_stats_log(CATEGORY_RECEIVER,AV_LOG_DEBUG,stats,"throttleThread-Stats"); | ||
|
||
if(stats->totalFrames < coldSeconds * targetFramerate.num / targetFramerate.den) { | ||
return; | ||
} | ||
|
||
currentDataRate = stats->currentFrameRate * targetFramerate.den / (float)targetFramerate.num; | ||
|
||
LOGGER(CATEGORY_THROTTLER, | ||
AV_LOG_DEBUG,"throttleThread. data rate current: %.3f max: %.3f", | ||
currentDataRate, | ||
maxDataRate); | ||
|
||
if(currentDataRate > maxDataRate) { | ||
// going to sleep for a period of time gained due to race | ||
int throttleWaitUSec = (currentDataRate - maxDataRate) * 1000 * 1000; //av_rescale(1000 * 1000,targetFramerate.den,targetFramerate.num); | ||
const int minThrottleWaitMs = 1; | ||
if(throttleWaitUSec > minThrottleWaitMs * 1000) { | ||
LOGGER(CATEGORY_THROTTLER,AV_LOG_INFO,"throttleThread. throttling %.3f ms",throttleWaitUSec / 1000.f); | ||
stats->throttleWait += av_rescale_q( throttleWaitUSec, clockScale, standard_timebase); | ||
av_usleep(throttleWaitUSec); | ||
} | ||
} | ||
} | ||
} |
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,20 @@ | ||
// live_transcoder | ||
// | ||
|
||
#ifndef Throttler_h | ||
#define Throttler_h | ||
|
||
typedef struct { | ||
const bool enabled; | ||
const double maxDataRate; | ||
const int coldSeconds; | ||
const int minThrottleWaitMs; | ||
samples_stats_t *stats; | ||
} throttler_t; | ||
|
||
int throttler_init(samples_stats_t *stats,throttler_t *throttler); | ||
|
||
void | ||
throttler_process(throttler_t *throttler,transcode_session_t *session); | ||
|
||
#endif |