1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/mediastream/webaudio_media_stream_source.h"
#include <utility>
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "media/base/audio_glitch_info.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
namespace blink {
WebAudioMediaStreamSource::WebAudioMediaStreamSource(
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
: MediaStreamAudioSource(std::move(task_runner), false /* is_remote */),
fifo_(ConvertToBaseRepeatingCallback(CrossThreadBindRepeating(
&WebAudioMediaStreamSource::DeliverRebufferedAudio,
WTF::CrossThreadUnretained(this)))) {
DVLOG(1) << "WebAudioMediaStreamSource::WebAudioMediaStreamSource()";
}
WebAudioMediaStreamSource::~WebAudioMediaStreamSource() {
DVLOG(1) << "WebAudioMediaStreamSource::~WebAudioMediaStreamSource()";
}
void WebAudioMediaStreamSource::SetFormat(int number_of_channels,
float sample_rate) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
VLOG(1) << "WebAudio media stream source changed format to: channels="
<< number_of_channels << ", sample_rate=" << sample_rate;
// If the channel count is greater than 8, use discrete layout. However,
// anything beyond 8 is ignored by some audio tracks/sinks.
media::ChannelLayout channel_layout =
number_of_channels > 8 ? media::CHANNEL_LAYOUT_DISCRETE
: media::GuessChannelLayout(number_of_channels);
// Set the format used by this WebAudioMediaStreamSource. We are using 10ms
// data as a buffer size since that is the native buffer size of WebRtc packet
// running on.
fifo_.Reset(sample_rate / 100);
media::AudioParameters params(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
{channel_layout, number_of_channels},
sample_rate, fifo_.frames_per_buffer());
MediaStreamAudioSource::SetFormat(params);
if (!wrapper_bus_ || wrapper_bus_->channels() != params.channels())
wrapper_bus_ = media::AudioBus::CreateWrapper(params.channels());
}
void WebAudioMediaStreamSource::ConsumeAudio(
const Vector<const float*>& audio_data,
int number_of_frames) {
TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("mediastream"),
"WebAudioMediaStreamSource::ConsumeAudio", "frames",
number_of_frames);
// TODO(https://crbug.com/1302080): this should use the actual audio
// playout stamp instead of Now().
current_reference_time_ = base::TimeTicks::Now();
wrapper_bus_->set_frames(number_of_frames);
DCHECK_EQ(wrapper_bus_->channels(), static_cast<int>(audio_data.size()));
for (wtf_size_t i = 0; i < audio_data.size(); ++i) {
// TODO(crbug.com/375449662): Spanify `audio_data`.
wrapper_bus_->SetChannelData(
static_cast<int>(i),
UNSAFE_TODO(base::span(const_cast<float*>(audio_data[i]),
base::checked_cast<size_t>(number_of_frames))));
}
// The following will result in zero, one, or multiple synchronous calls to
// DeliverRebufferedAudio().
fifo_.Push(*wrapper_bus_);
}
void WebAudioMediaStreamSource::DeliverRebufferedAudio(
const media::AudioBus& audio_bus,
int frame_delay) {
TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("mediastream"),
"WebAudioMediaStreamSource::DeliverRebufferedAudio", "frames",
audio_bus.frames());
const base::TimeTicks reference_time =
current_reference_time_ +
base::Microseconds(
frame_delay * base::Time::kMicrosecondsPerSecond /
MediaStreamAudioSource::GetAudioParameters().sample_rate());
MediaStreamAudioSource::DeliverDataToTracks(audio_bus, reference_time, {});
}
} // namespace blink
|