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