blob: 063a367d1cb5242ab75a11b9ae2baba0dedcd6ff [file] [log] [blame]
henrika5f6bf242017-11-01 11:06:56 +01001/*
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 Gerey988cc082018-10-23 12:03:01 +020012
13#include <stddef.h>
14
15#include "rtc_base/checks.h"
16#include "rtc_base/location.h"
henrika5f6bf242017-11-01 11:06:56 +010017#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/time_utils.h"
henrika5f6bf242017-11-01 11:06:56 +010019
20namespace webrtc {
21namespace internal {
22
23namespace {
24
25constexpr int64_t kPollDelayMs = 10; // WebRTC uses 10ms by default
26
27constexpr size_t kNumChannels = 1;
28constexpr uint32_t kSamplesPerSecond = 48000; // 48kHz
29constexpr size_t kNumSamples = kSamplesPerSecond / 100; // 10ms of samples
30
31} // namespace
32
33NullAudioPoller::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
40NullAudioPoller::~NullAudioPoller() {
41 RTC_DCHECK(thread_checker_.CalledOnValidThread());
42 rtc::Thread::Current()->Clear(this);
43}
44
45void NullAudioPoller::OnMessage(rtc::Message* msg) {
46 RTC_DCHECK(thread_checker_.CalledOnValidThread());
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