Commit 19265f91 authored by Ken MacKay's avatar Ken MacKay Committed by Commit Bot

[Chromecast] Fix rendering delay tests

Fix some issues in MixerInputConnection which caused incorrect rendering
delay or failure to start the stream.

Bug: internal b/143135800
Test: cast_media_unittests
Change-Id: I2c1a39da00aa36d36f72741cb374a1b87e5b46bb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1918180Reviewed-by: default avatarYuchen Liu <yucliu@chromium.org>
Commit-Queue: Kenneth MacKay <kmackay@chromium.org>
Cr-Commit-Position: refs/heads/master@{#715517}
parent 390eca7a
...@@ -46,13 +46,10 @@ namespace media { ...@@ -46,13 +46,10 @@ namespace media {
namespace { namespace {
// Make fill size big enough that most buffers will fit into it. This is more const int kInitialFillSizeFrames = 512;
// efficient in general since we don't need to allocate more buffers from the
// heap (can use the buffer pool instead).
const int kFillSizeFrames = 1536;
const double kPlaybackRateEpsilon = 0.001; const double kPlaybackRateEpsilon = 0.001;
const int64_t kDefaultInputQueueMs = 90; const int64_t kDefaultInputQueueMs = 200;
constexpr base::TimeDelta kFadeTime = base::TimeDelta::FromMilliseconds(5); constexpr base::TimeDelta kFadeTime = base::TimeDelta::FromMilliseconds(5);
const int kDefaultStartThresholdMs = 70; const int kDefaultStartThresholdMs = 70;
...@@ -108,6 +105,7 @@ AudioDecoderForMixer::AudioDecoderForMixer( ...@@ -108,6 +105,7 @@ AudioDecoderForMixer::AudioDecoderForMixer(
MediaPipelineBackendForMixer* backend) MediaPipelineBackendForMixer* backend)
: backend_(backend), : backend_(backend),
task_runner_(backend->GetTaskRunner()), task_runner_(backend->GetTaskRunner()),
buffer_pool_frames_(kInitialFillSizeFrames),
pending_output_frames_(kNoPendingOutput), pending_output_frames_(kNoPendingOutput),
pool_(new ::media::AudioBufferMemoryPool()), pool_(new ::media::AudioBufferMemoryPool()),
weak_factory_(this) { weak_factory_(this) {
...@@ -161,13 +159,21 @@ bool AudioDecoderForMixer::Start(int64_t playback_start_pts, ...@@ -161,13 +159,21 @@ bool AudioDecoderForMixer::Start(int64_t playback_start_pts,
return true; return true;
} }
void AudioDecoderForMixer::CreateMixerInput(const AudioConfig& config, void AudioDecoderForMixer::CreateBufferPool(const AudioConfig& config,
bool start_playback_asap) { int frame_count) {
DCHECK_GT(frame_count, 0);
buffer_pool_frames_ = frame_count;
buffer_pool_ = base::MakeRefCounted<IOBufferPool>( buffer_pool_ = base::MakeRefCounted<IOBufferPool>(
kFillSizeFrames * sizeof(float) * config.channel_number + frame_count * sizeof(float) * config.channel_number +
kAudioMessageHeaderSize, kAudioMessageHeaderSize,
std::numeric_limits<size_t>::max(), true /* threadsafe */); std::numeric_limits<size_t>::max(), true /* threadsafe */);
buffer_pool_->Preallocate(1); buffer_pool_->Preallocate(1);
}
void AudioDecoderForMixer::CreateMixerInput(const AudioConfig& config,
bool start_playback_asap) {
CreateBufferPool(config, buffer_pool_frames_);
DCHECK_GT(buffer_pool_frames_, 0);
audio_resampler_ = std::make_unique<AudioResampler>(config.channel_number); audio_resampler_ = std::make_unique<AudioResampler>(config.channel_number);
audio_resampler_->SetMediaClockRate(av_sync_clock_rate_); audio_resampler_->SetMediaClockRate(av_sync_clock_rate_);
...@@ -184,7 +190,7 @@ void AudioDecoderForMixer::CreateMixerInput(const AudioConfig& config, ...@@ -184,7 +190,7 @@ void AudioDecoderForMixer::CreateMixerInput(const AudioConfig& config,
params.set_sample_rate(config.samples_per_second); params.set_sample_rate(config.samples_per_second);
params.set_num_channels(config.channel_number); params.set_num_channels(config.channel_number);
params.set_channel_selection(ToPlayoutChannel(backend_->AudioChannel())); params.set_channel_selection(ToPlayoutChannel(backend_->AudioChannel()));
params.set_fill_size_frames(kFillSizeFrames); params.set_fill_size_frames(buffer_pool_frames_);
params.set_start_threshold_frames(StartThreshold(config.samples_per_second)); params.set_start_threshold_frames(StartThreshold(config.samples_per_second));
params.set_max_buffered_frames(MaxQueuedFrames(config.samples_per_second)); params.set_max_buffered_frames(MaxQueuedFrames(config.samples_per_second));
params.set_use_fader(true); params.set_use_fader(true);
...@@ -542,14 +548,11 @@ void AudioDecoderForMixer::WritePcm(scoped_refptr<DecoderBufferBase> buffer) { ...@@ -542,14 +548,11 @@ void AudioDecoderForMixer::WritePcm(scoped_refptr<DecoderBufferBase> buffer) {
// subtracts at most 1 frame. // subtracts at most 1 frame.
DCHECK_GT(frame_count, 0); DCHECK_GT(frame_count, 0);
scoped_refptr<::net::IOBuffer> io_buffer; if (frame_count > buffer_pool_frames_) {
if (resampled->data_size() > CreateBufferPool(config_, frame_count * 2);
buffer_pool_->buffer_size() - kAudioMessageHeaderSize) {
io_buffer = base::MakeRefCounted<::net::IOBuffer>(resampled->data_size() +
kAudioMessageHeaderSize);
} else {
io_buffer = buffer_pool_->GetBuffer();
} }
auto io_buffer = buffer_pool_->GetBuffer();
memcpy(io_buffer->data() + kAudioMessageHeaderSize, resampled->data(), memcpy(io_buffer->data() + kAudioMessageHeaderSize, resampled->data(),
resampled->data_size()); resampled->data_size());
......
...@@ -77,6 +77,7 @@ class AudioDecoderForMixer ...@@ -77,6 +77,7 @@ class AudioDecoderForMixer
void OnEosPlayed() override; void OnEosPlayed() override;
void OnMixerError() override; void OnMixerError() override;
void CreateBufferPool(const AudioConfig& config, int frame_count);
void CreateMixerInput(const AudioConfig& config, bool start_playback_asap); void CreateMixerInput(const AudioConfig& config, bool start_playback_asap);
void CleanUpPcm(); void CleanUpPcm();
void ResetMixerInputForNewConfig(const AudioConfig& config); void ResetMixerInputForNewConfig(const AudioConfig& config);
...@@ -99,6 +100,7 @@ class AudioDecoderForMixer ...@@ -99,6 +100,7 @@ class AudioDecoderForMixer
Statistics stats_; Statistics stats_;
int buffer_pool_frames_ = 0;
bool pending_buffer_complete_ = false; bool pending_buffer_complete_ = false;
bool mixer_error_ = false; bool mixer_error_ = false;
bool paused_ = false; bool paused_ = false;
......
...@@ -85,6 +85,7 @@ class MixerInputConnection : public mixer_service::MixerSocket::Delegate, ...@@ -85,6 +85,7 @@ class MixerInputConnection : public mixer_service::MixerSocket::Delegate,
int64_t timestamp) override; int64_t timestamp) override;
void OnConnectionError() override; void OnConnectionError() override;
void CreateBufferPool(int frame_count);
void OnInactivityTimeout(); void OnInactivityTimeout();
void RestartPlaybackAt(int64_t timestamp, int64_t pts); void RestartPlaybackAt(int64_t timestamp, int64_t pts);
void SetMediaPlaybackRate(double rate); void SetMediaPlaybackRate(double rate);
...@@ -118,7 +119,7 @@ class MixerInputConnection : public mixer_service::MixerSocket::Delegate, ...@@ -118,7 +119,7 @@ class MixerInputConnection : public mixer_service::MixerSocket::Delegate,
EXCLUSIVE_LOCKS_REQUIRED(lock_); EXCLUSIVE_LOCKS_REQUIRED(lock_);
void WritePcm(scoped_refptr<net::IOBuffer> data); void WritePcm(scoped_refptr<net::IOBuffer> data);
RenderingDelay QueueData(scoped_refptr<net::IOBuffer> data) int64_t QueueData(scoped_refptr<net::IOBuffer> data)
EXCLUSIVE_LOCKS_REQUIRED(lock_); EXCLUSIVE_LOCKS_REQUIRED(lock_);
void PostPcmCompletion(); void PostPcmCompletion();
...@@ -135,6 +136,7 @@ class MixerInputConnection : public mixer_service::MixerSocket::Delegate, ...@@ -135,6 +136,7 @@ class MixerInputConnection : public mixer_service::MixerSocket::Delegate,
const bool ignore_for_stream_count_; const bool ignore_for_stream_count_;
const int fill_size_; const int fill_size_;
const int algorithm_fill_size_;
const int num_channels_; const int num_channels_;
const int input_samples_per_second_; const int input_samples_per_second_;
const mixer_service::SampleFormat sample_format_; const mixer_service::SampleFormat sample_format_;
...@@ -144,7 +146,7 @@ class MixerInputConnection : public mixer_service::MixerSocket::Delegate, ...@@ -144,7 +146,7 @@ class MixerInputConnection : public mixer_service::MixerSocket::Delegate,
const int playout_channel_; const int playout_channel_;
const scoped_refptr<base::SequencedTaskRunner> io_task_runner_; const scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
const int max_queued_frames_; int max_queued_frames_;
// Minimum number of frames buffered before starting to fill data. // Minimum number of frames buffered before starting to fill data.
int start_threshold_frames_; int start_threshold_frames_;
...@@ -154,6 +156,7 @@ class MixerInputConnection : public mixer_service::MixerSocket::Delegate, ...@@ -154,6 +156,7 @@ class MixerInputConnection : public mixer_service::MixerSocket::Delegate,
bool audio_ready_for_playback_fired_ = false; bool audio_ready_for_playback_fired_ = false;
base::OneShotTimer inactivity_timer_; base::OneShotTimer inactivity_timer_;
bool connection_error_ = false; bool connection_error_ = false;
int buffer_pool_frames_ = 0;
base::Lock lock_; base::Lock lock_;
State state_ GUARDED_BY(lock_) = State::kUninitialized; State state_ GUARDED_BY(lock_) = State::kUninitialized;
...@@ -163,7 +166,8 @@ class MixerInputConnection : public mixer_service::MixerSocket::Delegate, ...@@ -163,7 +166,8 @@ class MixerInputConnection : public mixer_service::MixerSocket::Delegate,
base::circular_deque<scoped_refptr<net::IOBuffer>> queue_ GUARDED_BY(lock_); base::circular_deque<scoped_refptr<net::IOBuffer>> queue_ GUARDED_BY(lock_);
int queued_frames_ GUARDED_BY(lock_) = 0; int queued_frames_ GUARDED_BY(lock_) = 0;
RenderingDelay mixer_rendering_delay_ GUARDED_BY(lock_); RenderingDelay mixer_rendering_delay_ GUARDED_BY(lock_);
RenderingDelay last_buffer_delay_ GUARDED_BY(lock_); int64_t next_playback_timestamp_ GUARDED_BY(lock_) = INT64_MIN;
int mixer_read_size_ GUARDED_BY(lock_) = 0;
int extra_delay_frames_ GUARDED_BY(lock_) = 0; int extra_delay_frames_ GUARDED_BY(lock_) = 0;
int current_buffer_offset_ GUARDED_BY(lock_) = 0; int current_buffer_offset_ GUARDED_BY(lock_) = 0;
AudioFader fader_ GUARDED_BY(lock_); AudioFader fader_ GUARDED_BY(lock_);
......
...@@ -368,10 +368,7 @@ void MultizoneBackendTest::OnEndOfStream() { ...@@ -368,10 +368,7 @@ void MultizoneBackendTest::OnEndOfStream() {
kMaxRenderingDelayErrorUs); kMaxRenderingDelayErrorUs);
} }
// TODO(b/143135800): The new mixer backend didn't calculate the rendering delay TEST_P(MultizoneBackendTest, RenderingDelay) {
// correctly when playback rate changes. Temporary disable the test to unblock
// PoG.
TEST_P(MultizoneBackendTest, DISABLED_RenderingDelay) {
const TestParams& params = GetParam(); const TestParams& params = GetParam();
int sample_rate = testing::get<0>(params); int sample_rate = testing::get<0>(params);
float playback_rate = testing::get<1>(params); float playback_rate = testing::get<1>(params);
...@@ -383,7 +380,7 @@ TEST_P(MultizoneBackendTest, DISABLED_RenderingDelay) { ...@@ -383,7 +380,7 @@ TEST_P(MultizoneBackendTest, DISABLED_RenderingDelay) {
Start(playback_rate); Start(playback_rate);
} }
TEST_F(MultizoneBackendTest, DISABLED_RenderingDelayWithMultipleRateChanges) { TEST_F(MultizoneBackendTest, RenderingDelayWithMultipleRateChanges) {
Initialize(48000 /* sample_rate */, 10 /* playback_rate_change_count */); Initialize(48000 /* sample_rate */, 10 /* playback_rate_change_count */);
AddEffectsStreams(); AddEffectsStreams();
Start(1.0f /* playback_rate */); Start(1.0f /* playback_rate */);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment