blob: be78439a902a8ea4dbd9b3660d98cbf7a9fabee1 [file] [log] [blame]
/*
* Copyright 2019 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.
*/
#include "pc/playout_latency.h"
#include "iostream"
#include "rtc_base/checks.h"
#include "rtc_base/location.h"
#include "rtc_base/logging.h"
#include "rtc_base/numerics/safe_conversions.h"
#include "rtc_base/thread.h"
#include "rtc_base/thread_checker.h"
namespace {
constexpr int kDefaultLatency = 0;
constexpr int kRoundToZeroThresholdMs = 10;
} // namespace
namespace webrtc {
PlayoutLatency::PlayoutLatency(rtc::Thread* worker_thread)
: signaling_thread_(rtc::Thread::Current()), worker_thread_(worker_thread) {
RTC_DCHECK(worker_thread_);
}
void PlayoutLatency::OnStart(cricket::Delayable* media_channel, uint32_t ssrc) {
RTC_DCHECK_RUN_ON(signaling_thread_);
media_channel_ = media_channel;
ssrc_ = ssrc;
// Trying to apply cached latency for the audio stream.
if (cached_latency_) {
SetLatency(cached_latency_.value());
}
}
void PlayoutLatency::OnStop() {
RTC_DCHECK_RUN_ON(signaling_thread_);
// Assume that audio stream is no longer present for latency calls.
media_channel_ = nullptr;
ssrc_ = absl::nullopt;
}
void PlayoutLatency::SetLatency(double latency) {
RTC_DCHECK_RUN_ON(worker_thread_);
RTC_DCHECK_GE(latency, 0);
RTC_DCHECK_LE(latency, 10);
int delay_ms = rtc::dchecked_cast<int>(latency * 1000);
// In JitterBuffer 0 delay has special meaning of being unconstrained value
// that is why we round delay to 0 if it is small enough during conversion
// from latency.
if (delay_ms <= kRoundToZeroThresholdMs) {
delay_ms = 0;
}
cached_latency_ = latency;
if (media_channel_ && ssrc_) {
media_channel_->SetBaseMinimumPlayoutDelayMs(ssrc_.value(), delay_ms);
}
}
double PlayoutLatency::GetLatency() const {
RTC_DCHECK_RUN_ON(worker_thread_);
absl::optional<int> delay_ms;
if (media_channel_ && ssrc_) {
delay_ms = media_channel_->GetBaseMinimumPlayoutDelayMs(ssrc_.value());
}
if (delay_ms) {
return delay_ms.value() / 1000.0;
} else {
return cached_latency_.value_or(kDefaultLatency);
}
}
} // namespace webrtc