blob: c22b3d879115811ce6d0d1d274db1bfd3718c27d [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"
12#include "rtc_base/logging.h"
13#include "rtc_base/thread.h"
14
15namespace webrtc {
16namespace internal {
17
18namespace {
19
20constexpr int64_t kPollDelayMs = 10; // WebRTC uses 10ms by default
21
22constexpr size_t kNumChannels = 1;
23constexpr uint32_t kSamplesPerSecond = 48000; // 48kHz
24constexpr size_t kNumSamples = kSamplesPerSecond / 100; // 10ms of samples
25
26} // namespace
27
28NullAudioPoller::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
35NullAudioPoller::~NullAudioPoller() {
36 RTC_DCHECK(thread_checker_.CalledOnValidThread());
37 rtc::Thread::Current()->Clear(this);
38}
39
40void 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