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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/audio/sync_reader.h"
#include <limits>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include "base/check.h"
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "base/sync_socket.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "media/base/audio_bus.h"
#include "media/base/audio_parameters.h"
#include "services/audio/output_glitch_counter.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::NiceMock;
using ::testing::Test;
using ::testing::TestWithParam;
using media::AudioBus;
using media::AudioOutputBuffer;
using media::AudioOutputBufferParameters;
using media::AudioParameters;
namespace audio {
namespace {
void NoLog(const std::string&) {}
static_assert(
std::is_unsigned<
decltype(AudioOutputBufferParameters::bitstream_data_size)>::value,
"If |bitstream_data_size| is ever made signed, add tests for negative "
"buffer sizes.");
enum OverflowTestCase {
kZero,
kNoOverflow,
kOverflowByOne,
kOverflowByOneThousand,
kOverflowByMax
};
static const OverflowTestCase overflow_test_case_values[]{
kZero, kNoOverflow, kOverflowByOne, kOverflowByOneThousand, kOverflowByMax};
class SyncReaderBitstreamTest : public TestWithParam<OverflowTestCase> {
public:
SyncReaderBitstreamTest() {}
~SyncReaderBitstreamTest() override {}
private:
base::test::TaskEnvironment env_;
};
TEST_P(SyncReaderBitstreamTest, BitstreamBufferOverflow_DoesNotWriteOOB) {
const int kSampleRate = 44100;
const int kFramesPerBuffer = 1;
AudioParameters params(AudioParameters::AUDIO_BITSTREAM_AC3,
media::ChannelLayoutConfig::Stereo(), kSampleRate,
kFramesPerBuffer);
auto socket = std::make_unique<base::CancelableSyncSocket>();
SyncReader reader(base::BindRepeating(&NoLog), params, socket.get());
ASSERT_TRUE(reader.IsValid());
base::WritableSharedMemoryMapping shmem =
reader.TakeSharedMemoryRegion().Map();
ASSERT_TRUE(shmem.IsValid());
auto* const buffer = shmem.GetMemoryAs<media::AudioOutputBuffer>();
ASSERT_TRUE(buffer);
reader.RequestMoreData(base::TimeDelta(), base::TimeTicks(), {});
uint32_t signal;
EXPECT_EQ(socket->Receive(base::byte_span_from_ref(signal)), sizeof(signal));
// So far, this is an ordinary stream.
// Now |reader| expects data to be written to the shared memory. The renderer
// says how much data was written.
switch (GetParam()) {
case kZero:
buffer->params.bitstream_data_size = 0;
break;
case kNoOverflow:
buffer->params.bitstream_data_size =
shmem.mapped_size() - sizeof(AudioOutputBufferParameters);
break;
case kOverflowByOne:
buffer->params.bitstream_data_size =
shmem.mapped_size() - sizeof(AudioOutputBufferParameters) + 1;
break;
case kOverflowByOneThousand:
buffer->params.bitstream_data_size =
shmem.mapped_size() - sizeof(AudioOutputBufferParameters) + 1000;
break;
case kOverflowByMax:
buffer->params.bitstream_data_size = std::numeric_limits<
decltype(buffer->params.bitstream_data_size)>::max();
break;
}
++signal;
EXPECT_EQ(socket->Send(base::byte_span_from_ref(signal)), sizeof(signal));
// The purpose of the test is to ensure this call doesn't result in undefined
// behavior, which should be verified by sanitizers.
std::unique_ptr<AudioBus> output_bus = AudioBus::Create(params);
reader.Read(output_bus.get(), false);
}
INSTANTIATE_TEST_SUITE_P(All,
SyncReaderBitstreamTest,
::testing::ValuesIn(overflow_test_case_values));
class MockOutputGlitchCounter : public OutputGlitchCounter {
public:
MockOutputGlitchCounter()
: OutputGlitchCounter(media::AudioLatency::Type::kRtc) {}
MockOutputGlitchCounter(const MockOutputGlitchCounter&) = delete;
MockOutputGlitchCounter& operator=(const MockOutputGlitchCounter&) = delete;
MOCK_METHOD2(ReportMissedCallback, void(bool, bool));
};
class SyncReaderTest : public ::testing::Test {
public:
SyncReaderTest()
: params_(AudioParameters(AudioParameters::AUDIO_BITSTREAM_AC3,
media::ChannelLayoutConfig::Stereo(),
kSampleRate,
kFramesPerBuffer)) {}
SyncReaderTest(const SyncReaderTest&) = delete;
SyncReaderTest& operator=(const SyncReaderTest&) = delete;
protected:
void SetUp() override {
socket_ = std::make_unique<base::CancelableSyncSocket>();
mock_audio_glitch_counter_ptr_ =
std::make_unique<NiceMock<MockOutputGlitchCounter>>();
mock_output_glitch_counter_ = mock_audio_glitch_counter_ptr_.get();
reader_ = std::make_unique<SyncReader>(
base::BindRepeating(&NoLog), params_, socket_.get(),
std::move(mock_audio_glitch_counter_ptr_));
CHECK(reader_->IsValid());
reader_->set_max_wait_timeout_for_test(base::Milliseconds(999));
shmem_ = reader_->TakeSharedMemoryRegion().Map();
CHECK(shmem_.IsValid());
buffer_ = shmem_.GetMemoryAs<media::AudioOutputBuffer>();
CHECK(buffer_);
}
void TearDown() override {
mock_output_glitch_counter_ = nullptr;
reader_.reset();
mock_audio_glitch_counter_ptr_.reset();
socket_.reset();
auto shmem = std::move(shmem_);
}
const int kSampleRate = 44100;
const int kFramesPerBuffer = 1;
const AudioParameters params_;
std::unique_ptr<base::CancelableSyncSocket> socket_;
private:
std::unique_ptr<MockOutputGlitchCounter> mock_audio_glitch_counter_ptr_;
protected:
raw_ptr<MockOutputGlitchCounter> mock_output_glitch_counter_ = nullptr;
std::unique_ptr<SyncReader> reader_;
base::WritableSharedMemoryMapping shmem_;
raw_ptr<media::AudioOutputBuffer> buffer_ = nullptr;
};
TEST_F(SyncReaderTest, CallsGlitchCounter) {
// Provoke all four combinations of arguments for
// OutputGlitchCounter::ReportMissedCallback.
uint32_t buffer_index = 0;
{
reader_->RequestMoreData(base::TimeDelta(), base::TimeTicks(), {});
uint32_t signal;
EXPECT_EQ(socket_->Receive(base::byte_span_from_ref(signal)),
sizeof(signal));
buffer_->params.bitstream_data_size =
shmem_.mapped_size() - sizeof(AudioOutputBufferParameters);
std::unique_ptr<AudioBus> output_bus = AudioBus::Create(params_);
++buffer_index;
EXPECT_EQ(socket_->Send(base::byte_span_from_ref(buffer_index)),
sizeof(buffer_index));
EXPECT_CALL(*mock_output_glitch_counter_,
ReportMissedCallback(/*missed_callback = */ false,
/*is_mixing = */ false));
reader_->Read(output_bus.get(), false);
}
{
reader_->RequestMoreData(base::TimeDelta(), base::TimeTicks(), {});
uint32_t signal;
EXPECT_EQ(socket_->Receive(base::byte_span_from_ref(signal)),
sizeof(signal));
buffer_->params.bitstream_data_size =
shmem_.mapped_size() - sizeof(AudioOutputBufferParameters);
std::unique_ptr<AudioBus> output_bus = AudioBus::Create(params_);
++buffer_index;
EXPECT_EQ(socket_->Send(base::byte_span_from_ref(buffer_index)),
sizeof(buffer_index));
EXPECT_CALL(*mock_output_glitch_counter_,
ReportMissedCallback(/*missed_callback = */ false,
/*is_mixing = */ true));
reader_->Read(output_bus.get(), true);
}
{
reader_->RequestMoreData(base::TimeDelta(), base::TimeTicks(), {});
uint32_t signal;
EXPECT_EQ(socket_->Receive(base::byte_span_from_ref(signal)),
sizeof(signal));
buffer_->params.bitstream_data_size =
shmem_.mapped_size() - sizeof(AudioOutputBufferParameters);
std::unique_ptr<AudioBus> output_bus = AudioBus::Create(params_);
// Send an incorrect buffer index, which will count as a missed callback.
buffer_index = 123;
EXPECT_EQ(socket_->Send(base::byte_span_from_ref(buffer_index)),
sizeof(buffer_index));
EXPECT_CALL(*mock_output_glitch_counter_,
ReportMissedCallback(/*missed_callback = */ true,
/*is_mixing = */ false));
reader_->Read(output_bus.get(), false);
}
{
reader_->RequestMoreData(base::TimeDelta(), base::TimeTicks(), {});
uint32_t signal;
EXPECT_EQ(socket_->Receive(base::byte_span_from_ref(signal)),
sizeof(signal));
buffer_->params.bitstream_data_size =
shmem_.mapped_size() - sizeof(AudioOutputBufferParameters);
std::unique_ptr<AudioBus> output_bus = AudioBus::Create(params_);
// Send an incorrect buffer index, which will count as a missed callback.
buffer_index = 123;
EXPECT_EQ(socket_->Send(base::byte_span_from_ref(buffer_index)),
sizeof(buffer_index));
EXPECT_CALL(*mock_output_glitch_counter_,
ReportMissedCallback(/*missed_callback = */ true,
/*is_mixing = */ true));
reader_->Read(output_bus.get(), true);
}
}
TEST_F(SyncReaderTest, PropagatesDelay) {
base::TimeDelta delay = base::Milliseconds(123);
base::TimeTicks delay_timestamp = base::TimeTicks() + base::Days(5);
reader_->RequestMoreData(delay, delay_timestamp, {});
uint32_t signal;
EXPECT_EQ(socket_->Receive(base::byte_span_from_ref(signal)), sizeof(signal));
buffer_->params.bitstream_data_size =
shmem_.mapped_size() - sizeof(AudioOutputBufferParameters);
std::unique_ptr<AudioBus> output_bus = AudioBus::Create(params_);
EXPECT_EQ(buffer_->params.delay_us, delay.InMicroseconds());
EXPECT_EQ(buffer_->params.delay_timestamp_us,
(delay_timestamp - base::TimeTicks()).InMicroseconds());
}
TEST_F(SyncReaderTest, PropagatesGlitchInfo) {
{
media::AudioGlitchInfo glitch_info{.duration = base::Seconds(1),
.count = 123};
reader_->RequestMoreData(base::TimeDelta(), base::TimeTicks(), glitch_info);
uint32_t signal;
EXPECT_EQ(socket_->Receive(base::byte_span_from_ref(signal)),
sizeof(signal));
buffer_->params.bitstream_data_size =
shmem_.mapped_size() - sizeof(AudioOutputBufferParameters);
std::unique_ptr<AudioBus> output_bus = AudioBus::Create(params_);
EXPECT_EQ(buffer_->params.glitch_duration_us,
base::Seconds(1).InMicroseconds());
EXPECT_EQ(buffer_->params.glitch_count, 123u);
// Set a clearly incorrect buffer index. This means that the reader will
// assume it's got the wrong data back from the Renderer process, and will
// proceed to drop it. This causes a glitch.
uint32_t buffer_index = 321;
EXPECT_EQ(socket_->Send(base::byte_span_from_ref(buffer_index)),
sizeof(buffer_index));
reader_->Read(output_bus.get(), false);
}
{
media::AudioGlitchInfo glitch_info{.duration = base::Seconds(2),
.count = 246};
reader_->RequestMoreData(base::TimeDelta(), base::TimeTicks(), glitch_info);
uint32_t signal;
EXPECT_EQ(socket_->Receive(base::byte_span_from_ref(signal)),
sizeof(signal));
buffer_->params.bitstream_data_size =
shmem_.mapped_size() - sizeof(AudioOutputBufferParameters);
std::unique_ptr<AudioBus> output_bus = AudioBus::Create(params_);
// Since there was a glitch on the last read, this time there will be an
// extra glitch reflected in the propagated info.
EXPECT_EQ(
buffer_->params.glitch_duration_us,
(base::Seconds(2) + params_.GetBufferDuration()).InMicroseconds());
EXPECT_EQ(buffer_->params.glitch_count, 246u + 1u);
// This time, send the correct buffer index.
uint32_t buffer_index = 2;
EXPECT_EQ(socket_->Send(base::byte_span_from_ref(buffer_index)),
sizeof(buffer_index));
reader_->Read(output_bus.get(), false);
}
{
media::AudioGlitchInfo glitch_info{.duration = base::Seconds(3),
.count = 321};
reader_->RequestMoreData(base::TimeDelta(), base::TimeTicks(), glitch_info);
uint32_t signal;
EXPECT_EQ(socket_->Receive(base::byte_span_from_ref(signal)),
sizeof(signal));
buffer_->params.bitstream_data_size =
shmem_.mapped_size() - sizeof(AudioOutputBufferParameters);
std::unique_ptr<AudioBus> output_bus = AudioBus::Create(params_);
// This time there should be no added glitches.
EXPECT_EQ(buffer_->params.glitch_duration_us,
base::Seconds(3).InMicroseconds());
EXPECT_EQ(buffer_->params.glitch_count, 321u);
}
}
} // namespace
} // namespace audio
|