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