• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "rtc_base/checks.h"
14 #include "rtc_base/location.h"
15 #include "rtc_base/logging.h"
16 #include "rtc_base/numerics/safe_conversions.h"
17 #include "rtc_base/numerics/safe_minmax.h"
18 #include "rtc_base/thread.h"
19 #include "rtc_base/thread_checker.h"
20 
21 namespace {
22 constexpr int kDefaultDelay = 0;
23 constexpr int kMaximumDelayMs = 10000;
24 }  // namespace
25 
26 namespace webrtc {
27 
JitterBufferDelay(rtc::Thread * worker_thread)28 JitterBufferDelay::JitterBufferDelay(rtc::Thread* worker_thread)
29     : signaling_thread_(rtc::Thread::Current()), worker_thread_(worker_thread) {
30   RTC_DCHECK(worker_thread_);
31 }
32 
OnStart(cricket::Delayable * media_channel,uint32_t ssrc)33 void JitterBufferDelay::OnStart(cricket::Delayable* media_channel,
34                                 uint32_t ssrc) {
35   RTC_DCHECK_RUN_ON(signaling_thread_);
36 
37   media_channel_ = media_channel;
38   ssrc_ = ssrc;
39 
40   // Trying to apply cached delay for the audio stream.
41   if (cached_delay_seconds_) {
42     Set(cached_delay_seconds_.value());
43   }
44 }
45 
OnStop()46 void JitterBufferDelay::OnStop() {
47   RTC_DCHECK_RUN_ON(signaling_thread_);
48   // Assume that audio stream is no longer present.
49   media_channel_ = nullptr;
50   ssrc_ = absl::nullopt;
51 }
52 
Set(absl::optional<double> delay_seconds)53 void JitterBufferDelay::Set(absl::optional<double> delay_seconds) {
54   RTC_DCHECK_RUN_ON(worker_thread_);
55 
56   // TODO(kuddai) propagate absl::optional deeper down as default preference.
57   int delay_ms =
58       rtc::saturated_cast<int>(delay_seconds.value_or(kDefaultDelay) * 1000);
59   delay_ms = rtc::SafeClamp(delay_ms, 0, kMaximumDelayMs);
60 
61   cached_delay_seconds_ = delay_seconds;
62   if (media_channel_ && ssrc_) {
63     media_channel_->SetBaseMinimumPlayoutDelayMs(ssrc_.value(), delay_ms);
64   }
65 }
66 
67 }  // namespace webrtc
68