Skip to content

Commit

Permalink
flip
Browse files Browse the repository at this point in the history
  • Loading branch information
disa6302 committed Apr 4, 2024
1 parent 547bf78 commit b7dedfd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,9 @@ In order to enable TWCC usage in the SDK, 2 things need to be set up:
1. Set the `disableSenderSideBandwidthEstimation` to FALSE. In our samples, the value is set using `disableTwcc` flag in `pSampleConfiguration`
```c
pSampleConfiguration->disableTwcc = TRUE; // to disable TWCC
pSampleConfiguration->disableTwcc = FALSE; // to enable TWCC
configuration.kvsRtcConfiguration.disableSenderSideBandwidthEstimation = pSampleConfiguration->disableTwcc;
pSampleConfiguration->enableTwcc = TRUE; // to enable TWCC
pSampleConfiguration->enableTwcc = FALSE; // to disable TWCC
configuration.kvsRtcConfiguration.disableSenderSideBandwidthEstimation = ~pSampleConfiguration->enableTwcc;
```
2. Set the callback that will have the business logic to modify the bitrate based on packet loss information. The callback can be set using `peerConnectionOnSenderBandwidthEstimation()`.
Expand Down
23 changes: 10 additions & 13 deletions samples/Common.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ STATUS initializePeerConnection(PSampleConfiguration pSampleConfiguration, PRtcP
configuration.kvsRtcConfiguration.iceSetInterfaceFilterFunc = NULL;

// disable TWCC
configuration.kvsRtcConfiguration.disableSenderSideBandwidthEstimation = pSampleConfiguration->disableTwcc;
configuration.kvsRtcConfiguration.disableSenderSideBandwidthEstimation = !(pSampleConfiguration->enableTwcc);
DLOGI("Disable: %s", configuration.kvsRtcConfiguration.disableSenderSideBandwidthEstimation ? "Disabled" : "Enabled");

// Set the ICE mode explicitly
configuration.iceTransportPolicy = ICE_TRANSPORT_POLICY_ALL;
Expand Down Expand Up @@ -552,9 +553,9 @@ STATUS createSampleStreamingSession(PSampleConfiguration pSampleConfiguration, P
pSampleStreamingSession->peerConnectionMetrics.peerConnectionStats.peerConnectionStartTime = GETTIME() / HUNDREDS_OF_NANOS_IN_A_MILLISECOND;
// Flag to enable SDK to calculate selected ice server, local, remote and candidate pair stats.
pSampleConfiguration->enableIceStats = FALSE;
pSampleConfiguration->disableTwcc = FALSE;
pSampleConfiguration->enableTwcc = TRUE;

if (!pSampleConfiguration->disableTwcc) {
if (pSampleConfiguration->enableTwcc) {
pSampleStreamingSession->twccMetadata.updateLock = MUTEX_CREATE(TRUE);
}

Expand Down Expand Up @@ -604,7 +605,7 @@ STATUS createSampleStreamingSession(PSampleConfiguration pSampleConfiguration, P
CHK_STATUS(transceiverOnBandwidthEstimation(pSampleStreamingSession->pAudioRtcRtpTransceiver, (UINT64) pSampleStreamingSession,
sampleBandwidthEstimationHandler));
// twcc bandwidth estimation
if (!pSampleConfiguration->disableTwcc) {
if (pSampleConfiguration->enableTwcc) {
CHK_STATUS(peerConnectionOnSenderBandwidthEstimation(pSampleStreamingSession->pPeerConnection, (UINT64) pSampleStreamingSession,
sampleSenderBandwidthEstimationHandler));
}
Expand Down Expand Up @@ -658,7 +659,7 @@ STATUS freeSampleStreamingSession(PSampleStreamingSession* ppSampleStreamingSess
}
MUTEX_UNLOCK(pSampleConfiguration->sampleConfigurationObjLock);

if (!pSampleConfiguration->disableTwcc) {
if (pSampleConfiguration->enableTwcc) {
if (IS_VALID_MUTEX_VALUE(pSampleStreamingSession->twccMetadata.updateLock)) {
MUTEX_FREE(pSampleStreamingSession->twccMetadata.updateLock);
}
Expand Down Expand Up @@ -738,7 +739,7 @@ VOID sampleSenderBandwidthEstimationHandler(UINT64 customData, UINT32 txBytes, U

currentTimeMs = GETTIME();
timeDiff = currentTimeMs - pSampleStreamingSession->twccMetadata.lastAdjustmentTimeMs;
if (timeDiff < ADJUSTMENT_INTERVAL_SECONDS) {
if (timeDiff < TWCC_BITRATE_ADJUSTMENT_INTERVAL_SECONDS) {
// Too soon for another adjustment
return;
}
Expand All @@ -750,15 +751,11 @@ VOID sampleSenderBandwidthEstimationHandler(UINT64 customData, UINT32 txBytes, U
if (pSampleStreamingSession->twccMetadata.averagePacketLoss <= 5) {
// increase encoder bitrate by 5 percent with a cap at MAX_BITRATE
videoBitrate = (UINT64) MIN(videoBitrate * 1.05, MAX_VIDEO_BITRATE_KBPS);
} else {
// decrease encoder bitrate by average packet loss percent, with a cap at MIN_BITRATE
videoBitrate = (UINT64) MAX(videoBitrate * (1.0 - pSampleStreamingSession->twccMetadata.averagePacketLoss / 100.0), MIN_VIDEO_BITRATE_KBPS);
}

if (pSampleStreamingSession->twccMetadata.averagePacketLoss <= 5) {
// increase encoder bitrate by 5 percent with a cap at MAX_BITRATE
audioBitrate = (UINT64) MIN(audioBitrate * 1.05, MAX_AUDIO_BITRATE_BPS);
} else {
// decrease encoder bitrate by average packet loss percent, with a cap at MIN_BITRATE
videoBitrate = (UINT64) MAX(videoBitrate * (1.0 - pSampleStreamingSession->twccMetadata.averagePacketLoss / 100.0), MIN_VIDEO_BITRATE_KBPS);
// decrease encoder bitrate by average packet loss percent, with a cap at MIN_BITRATE
audioBitrate = (UINT64) MAX(audioBitrate * (1.0 - pSampleStreamingSession->twccMetadata.averagePacketLoss / 100.0), MIN_AUDIO_BITRATE_BPS);
}
Expand All @@ -771,7 +768,7 @@ VOID sampleSenderBandwidthEstimationHandler(UINT64 customData, UINT32 txBytes, U
pSampleStreamingSession->twccMetadata.lastAdjustmentTimeMs = currentTimeMs;

DLOGI("Adjustment made: average packet loss = %.2f%%, timediff: %llu ms", pSampleStreamingSession->twccMetadata.averagePacketLoss,
ADJUSTMENT_INTERVAL_SECONDS, timeDiff);
TWCC_BITRATE_ADJUSTMENT_INTERVAL_SECONDS, timeDiff);
DLOGI("Suggested video bitrate %u kbps, suggested audio bitrate: %u bps, sent: %u bytes %u packets received: %u bytes %u packets in %lu msec",
videoBitrate, audioBitrate, txBytes, txPacketsCnt, rxBytes, rxPacketsCnt, duration / 10000ULL);
}
Expand Down
4 changes: 2 additions & 2 deletions samples/Samples.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ extern "C" {
#define MAX_SIGNALING_CLIENT_METRICS_MESSAGE_SIZE 736 // strlen(SIGNALING_CLIENT_METRICS_JSON_TEMPLATE) + 20 * 10
#define MAX_ICE_AGENT_METRICS_MESSAGE_SIZE 113 // strlen(ICE_AGENT_METRICS_JSON_TEMPLATE) + 20 * 2

#define ADJUSTMENT_INTERVAL_SECONDS 1 * HUNDREDS_OF_NANOS_IN_A_SECOND
#define TWCC_BITRATE_ADJUSTMENT_INTERVAL_SECONDS 1 * HUNDREDS_OF_NANOS_IN_A_SECOND
#define MIN_VIDEO_BITRATE_KBPS 512 // Unit kilobits/sec. Value could change based on codec.
#define MAX_VIDEO_BITRATE_KBPS 2048000 // Unit kilobits/sec. Value could change based on codec.
#define MIN_AUDIO_BITRATE_BPS 4000 // Unit bits/sec. Value could change based on codec.
Expand Down Expand Up @@ -155,7 +155,7 @@ typedef struct {
PCHAR rtspUri;
UINT32 logLevel;
BOOL enableIceStats;
BOOL disableTwcc;
BOOL enableTwcc;
} SampleConfiguration, *PSampleConfiguration;

typedef struct {
Expand Down
4 changes: 2 additions & 2 deletions samples/kvsWebRTCClientMasterGstSample.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ GstFlowReturn on_new_sample(GstElement* sink, gpointer data, UINT64 trackid)
frame.index = (UINT32) ATOMIC_INCREMENT(&pSampleStreamingSession->frameIndex);

if (trackid == DEFAULT_AUDIO_TRACK_ID) {
if (!pSampleStreamingSession->pSampleConfiguration->disableTwcc && senderPipeline != NULL) {
if (pSampleStreamingSession->pSampleConfiguration->enableTwcc && senderPipeline != NULL) {
GstElement* encoder = gst_bin_get_by_name(GST_BIN(senderPipeline), "sampleAudioEncoder");
if (encoder != NULL) {
g_object_get(G_OBJECT(encoder), "bitrate", &bitrate, NULL);
Expand All @@ -86,7 +86,7 @@ GstFlowReturn on_new_sample(GstElement* sink, gpointer data, UINT64 trackid)
pSampleStreamingSession->audioTimestamp +=
SAMPLE_AUDIO_FRAME_DURATION; // assume audio frame size is 20ms, which is default in opusenc
} else {
if (!pSampleStreamingSession->pSampleConfiguration->disableTwcc && senderPipeline != NULL) {
if (pSampleStreamingSession->pSampleConfiguration->enableTwcc && senderPipeline != NULL) {
GstElement* encoder = gst_bin_get_by_name(GST_BIN(senderPipeline), "sampleVideoEncoder");
if (encoder != NULL) {
g_object_get(G_OBJECT(encoder), "bitrate", &bitrate, NULL);
Expand Down

0 comments on commit b7dedfd

Please sign in to comment.