blob: 3fdf823d24f26e1a7feedf2c208de78d6138f5a8 [file] [log] [blame]
Ruslan Burakov428dcb22019-04-18 17:49:49 +02001/*
2 * Copyright 2019 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "pc/jitter_buffer_delay.h"
12
Artem Titovd15a5752021-02-10 14:31:24 +010013#include "api/sequence_checker.h"
Ruslan Burakov428dcb22019-04-18 17:49:49 +020014#include "rtc_base/checks.h"
Ruslan Burakov428dcb22019-04-18 17:49:49 +020015#include "rtc_base/numerics/safe_conversions.h"
16#include "rtc_base/numerics/safe_minmax.h"
17#include "rtc_base/thread.h"
Ruslan Burakov428dcb22019-04-18 17:49:49 +020018
19namespace {
20constexpr int kDefaultDelay = 0;
21constexpr int kMaximumDelayMs = 10000;
22} // namespace
23
24namespace webrtc {
25
26JitterBufferDelay::JitterBufferDelay(rtc::Thread* worker_thread)
27 : signaling_thread_(rtc::Thread::Current()), worker_thread_(worker_thread) {
28 RTC_DCHECK(worker_thread_);
29}
30
31void JitterBufferDelay::OnStart(cricket::Delayable* media_channel,
32 uint32_t ssrc) {
33 RTC_DCHECK_RUN_ON(signaling_thread_);
34
35 media_channel_ = media_channel;
36 ssrc_ = ssrc;
37
38 // Trying to apply cached delay for the audio stream.
39 if (cached_delay_seconds_) {
40 Set(cached_delay_seconds_.value());
41 }
42}
43
44void JitterBufferDelay::OnStop() {
45 RTC_DCHECK_RUN_ON(signaling_thread_);
46 // Assume that audio stream is no longer present.
47 media_channel_ = nullptr;
48 ssrc_ = absl::nullopt;
49}
50
51void JitterBufferDelay::Set(absl::optional<double> delay_seconds) {
52 RTC_DCHECK_RUN_ON(worker_thread_);
53
54 // TODO(kuddai) propagate absl::optional deeper down as default preference.
55 int delay_ms =
56 rtc::saturated_cast<int>(delay_seconds.value_or(kDefaultDelay) * 1000);
57 delay_ms = rtc::SafeClamp(delay_ms, 0, kMaximumDelayMs);
58
59 cached_delay_seconds_ = delay_seconds;
60 if (media_channel_ && ssrc_) {
61 media_channel_->SetBaseMinimumPlayoutDelayMs(ssrc_.value(), delay_ms);
62 }
63}
64
65} // namespace webrtc