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.

71 lines
2.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2017 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 "audio/null_audio_poller.h"
  11. #include <stddef.h>
  12. #include "rtc_base/checks.h"
  13. #include "rtc_base/location.h"
  14. #include "rtc_base/thread.h"
  15. #include "rtc_base/time_utils.h"
  16. namespace webrtc {
  17. namespace internal {
  18. namespace {
  19. constexpr int64_t kPollDelayMs = 10; // WebRTC uses 10ms by default
  20. constexpr size_t kNumChannels = 1;
  21. constexpr uint32_t kSamplesPerSecond = 48000; // 48kHz
  22. constexpr size_t kNumSamples = kSamplesPerSecond / 100; // 10ms of samples
  23. } // namespace
  24. NullAudioPoller::NullAudioPoller(AudioTransport* audio_transport)
  25. : audio_transport_(audio_transport),
  26. reschedule_at_(rtc::TimeMillis() + kPollDelayMs) {
  27. RTC_DCHECK(audio_transport);
  28. OnMessage(nullptr); // Start the poll loop.
  29. }
  30. NullAudioPoller::~NullAudioPoller() {
  31. RTC_DCHECK(thread_checker_.IsCurrent());
  32. rtc::Thread::Current()->Clear(this);
  33. }
  34. void NullAudioPoller::OnMessage(rtc::Message* msg) {
  35. RTC_DCHECK(thread_checker_.IsCurrent());
  36. // Buffer to hold the audio samples.
  37. int16_t buffer[kNumSamples * kNumChannels];
  38. // Output variables from |NeedMorePlayData|.
  39. size_t n_samples;
  40. int64_t elapsed_time_ms;
  41. int64_t ntp_time_ms;
  42. audio_transport_->NeedMorePlayData(kNumSamples, sizeof(int16_t), kNumChannels,
  43. kSamplesPerSecond, buffer, n_samples,
  44. &elapsed_time_ms, &ntp_time_ms);
  45. // Reschedule the next poll iteration. If, for some reason, the given
  46. // reschedule time has already passed, reschedule as soon as possible.
  47. int64_t now = rtc::TimeMillis();
  48. if (reschedule_at_ < now) {
  49. reschedule_at_ = now;
  50. }
  51. rtc::Thread::Current()->PostAt(RTC_FROM_HERE, reschedule_at_, this, 0);
  52. // Loop after next will be kPollDelayMs later.
  53. reschedule_at_ += kPollDelayMs;
  54. }
  55. } // namespace internal
  56. } // namespace webrtc