henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 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 | |
| 11 | #include "audio/null_audio_poller.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 12 | |
| 13 | #include <stddef.h> |
| 14 | |
| 15 | #include "rtc_base/checks.h" |
| 16 | #include "rtc_base/location.h" |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 17 | #include "rtc_base/thread.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "rtc_base/time_utils.h" |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | namespace internal { |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | constexpr int64_t kPollDelayMs = 10; // WebRTC uses 10ms by default |
| 26 | |
| 27 | constexpr size_t kNumChannels = 1; |
| 28 | constexpr uint32_t kSamplesPerSecond = 48000; // 48kHz |
| 29 | constexpr size_t kNumSamples = kSamplesPerSecond / 100; // 10ms of samples |
| 30 | |
| 31 | } // namespace |
| 32 | |
| 33 | NullAudioPoller::NullAudioPoller(AudioTransport* audio_transport) |
| 34 | : audio_transport_(audio_transport), |
| 35 | reschedule_at_(rtc::TimeMillis() + kPollDelayMs) { |
| 36 | RTC_DCHECK(audio_transport); |
| 37 | OnMessage(nullptr); // Start the poll loop. |
| 38 | } |
| 39 | |
| 40 | NullAudioPoller::~NullAudioPoller() { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 41 | RTC_DCHECK(thread_checker_.IsCurrent()); |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 42 | rtc::Thread::Current()->Clear(this); |
| 43 | } |
| 44 | |
| 45 | void NullAudioPoller::OnMessage(rtc::Message* msg) { |
Sebastian Jansson | c01367d | 2019-04-08 15:20:44 +0200 | [diff] [blame] | 46 | RTC_DCHECK(thread_checker_.IsCurrent()); |
henrika | 5f6bf24 | 2017-11-01 11:06:56 +0100 | [diff] [blame] | 47 | |
| 48 | // Buffer to hold the audio samples. |
| 49 | int16_t buffer[kNumSamples * kNumChannels]; |
| 50 | // Output variables from |NeedMorePlayData|. |
| 51 | size_t n_samples; |
| 52 | int64_t elapsed_time_ms; |
| 53 | int64_t ntp_time_ms; |
| 54 | audio_transport_->NeedMorePlayData(kNumSamples, sizeof(int16_t), kNumChannels, |
| 55 | kSamplesPerSecond, buffer, n_samples, |
| 56 | &elapsed_time_ms, &ntp_time_ms); |
| 57 | |
| 58 | // Reschedule the next poll iteration. If, for some reason, the given |
| 59 | // reschedule time has already passed, reschedule as soon as possible. |
| 60 | int64_t now = rtc::TimeMillis(); |
| 61 | if (reschedule_at_ < now) { |
| 62 | reschedule_at_ = now; |
| 63 | } |
| 64 | rtc::Thread::Current()->PostAt(RTC_FROM_HERE, reschedule_at_, this, 0); |
| 65 | |
| 66 | // Loop after next will be kPollDelayMs later. |
| 67 | reschedule_at_ += kPollDelayMs; |
| 68 | } |
| 69 | |
| 70 | } // namespace internal |
| 71 | } // namespace webrtc |