xref: /aosp_15_r20/external/webrtc/pc/jitter_buffer_delay.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
13 #include "api/sequence_checker.h"
14 #include "rtc_base/checks.h"
15 #include "rtc_base/numerics/safe_conversions.h"
16 #include "rtc_base/numerics/safe_minmax.h"
17 
18 namespace {
19 constexpr int kDefaultDelay = 0;
20 constexpr int kMaximumDelayMs = 10000;
21 }  // namespace
22 
23 namespace webrtc {
24 
JitterBufferDelay()25 JitterBufferDelay::JitterBufferDelay() {
26   worker_thread_checker_.Detach();
27 }
28 
Set(absl::optional<double> delay_seconds)29 void JitterBufferDelay::Set(absl::optional<double> delay_seconds) {
30   RTC_DCHECK_RUN_ON(&worker_thread_checker_);
31   cached_delay_seconds_ = delay_seconds;
32 }
33 
GetMs() const34 int JitterBufferDelay::GetMs() const {
35   RTC_DCHECK_RUN_ON(&worker_thread_checker_);
36   return rtc::SafeClamp(
37       rtc::saturated_cast<int>(cached_delay_seconds_.value_or(kDefaultDelay) *
38                                1000),
39       0, kMaximumDelayMs);
40 }
41 
42 }  // namespace webrtc
43