#include "media/audio/audio_output_resampler.h"
#include <stdint.h>
#include <algorithm>
#include <memory>
#include <string>
#include <utility>
#include "base/compiler_specific.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/not_fatal_until.h"
#include "base/numerics/safe_conversions.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "media/audio/audio_manager.h"
#include "media/audio/audio_output_dispatcher_impl.h"
#include "media/audio/audio_output_proxy.h"
#if BUILDFLAG(IS_WIN)
#include "media/audio/win/core_audio_util_win.h"
#endif
#include "media/base/audio_converter.h"
#include "media/base/audio_timestamp_helper.h"
#include "media/base/limits.h"
#include "media/base/sample_rates.h"
namespace media {
class OnMoreDataConverter
: public AudioOutputStream::AudioSourceCallback,
public AudioConverter::InputCallback { … };
namespace {
static void RecordStats(const AudioParameters& output_params) { … }
#if BUILDFLAG(IS_WIN)
AudioParameters GetFallbackHighLatencyOutputParams(
const AudioParameters& original_output_params) {
DCHECK_EQ(original_output_params.format(),
AudioParameters::AUDIO_PCM_LOW_LATENCY);
static const int kMinLowLatencyFrameSize = 2048;
const int frames_per_buffer = std::max(
original_output_params.frames_per_buffer(), kMinLowLatencyFrameSize);
AudioParameters fallback_params(original_output_params);
fallback_params.set_format(AudioParameters::AUDIO_PCM_LINEAR);
fallback_params.set_frames_per_buffer(frames_per_buffer);
return fallback_params;
}
AudioParameters GetFallbackLowLatencyOutputParams(
const std::string& device_id,
const AudioParameters& original_output_params) {
AudioParameters output_params;
HRESULT hr = CoreAudioUtil::GetPreferredAudioParameters(
device_id,
true, &output_params, false);
if (SUCCEEDED(hr)) {
int effects = output_params.effects();
effects |= original_output_params.effects();
output_params.set_effects(effects);
output_params.SetChannelLayoutConfig(
original_output_params.channel_layout(),
original_output_params.channels());
return output_params;
}
return AudioParameters();
}
#endif
enum class OpenStreamResult { … };
OpenStreamResult GetSubsequentStreamCreationResultBucket(
const AudioParameters& current_params,
bool success) { … }
}
AudioOutputResampler::AudioOutputResampler(
AudioManager* audio_manager,
const AudioParameters& input_params,
const AudioParameters& output_params,
const std::string& output_device_id,
base::TimeDelta close_delay,
const RegisterDebugRecordingSourceCallback&
register_debug_recording_source_callback)
: … { … }
AudioOutputResampler::~AudioOutputResampler() { … }
void AudioOutputResampler::Reinitialize() { … }
std::unique_ptr<AudioOutputDispatcherImpl> AudioOutputResampler::MakeDispatcher(
const std::string& output_device_id,
const AudioParameters& params) { … }
AudioOutputProxy* AudioOutputResampler::CreateStreamProxy() { … }
bool AudioOutputResampler::OpenStream() { … }
bool AudioOutputResampler::StartStream(
AudioOutputStream::AudioSourceCallback* callback,
AudioOutputProxy* stream_proxy) { … }
void AudioOutputResampler::StreamVolumeSet(AudioOutputProxy* stream_proxy,
double volume) { … }
void AudioOutputResampler::StopStream(AudioOutputProxy* stream_proxy) { … }
void AudioOutputResampler::CloseStream(AudioOutputProxy* stream_proxy) { … }
void AudioOutputResampler::FlushStream(AudioOutputProxy* stream_proxy) { … }
void AudioOutputResampler::StopStreamInternal(
const CallbackMap::value_type& item) { … }
OnMoreDataConverter::OnMoreDataConverter(
const AudioParameters& input_params,
const AudioParameters& output_params,
std::unique_ptr<AudioDebugRecorder> debug_recorder)
: … { … }
OnMoreDataConverter::~OnMoreDataConverter() { … }
void OnMoreDataConverter::Start(
AudioOutputStream::AudioSourceCallback* callback) { … }
void OnMoreDataConverter::Stop() { … }
int OnMoreDataConverter::OnMoreData(base::TimeDelta delay,
base::TimeTicks delay_timestamp,
const AudioGlitchInfo& glitch_info,
AudioBus* dest) { … }
double OnMoreDataConverter::ProvideInput(AudioBus* dest,
uint32_t frames_delayed,
const AudioGlitchInfo& glitch_info) { … }
void OnMoreDataConverter::OnError(ErrorType type) { … }
}