You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.0 KiB

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