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
13 #include <stddef.h>
14
15 #include "rtc_base/checks.h"
16 #include "rtc_base/location.h"
17 #include "rtc_base/thread.h"
18 #include "rtc_base/time_utils.h"
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
NullAudioPoller(AudioTransport * audio_transport)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
~NullAudioPoller()40 NullAudioPoller::~NullAudioPoller() {
41 RTC_DCHECK(thread_checker_.IsCurrent());
42 rtc::Thread::Current()->Clear(this);
43 }
44
OnMessage(rtc::Message * msg)45 void NullAudioPoller::OnMessage(rtc::Message* msg) {
46 RTC_DCHECK(thread_checker_.IsCurrent());
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
72