|
Line 0
Link Here
|
|
|
1 |
// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style license that can be |
| 3 |
// found in the LICENSE file. |
| 4 |
|
| 5 |
#include "base/metrics/histogram_macros.h" |
| 6 |
|
| 7 |
#include "media/audio/openbsd/audio_manager_openbsd.h" |
| 8 |
|
| 9 |
#include "media/audio/audio_device_description.h" |
| 10 |
#include "media/audio/audio_output_dispatcher.h" |
| 11 |
#if defined(USE_PULSEAUDIO) |
| 12 |
#include "media/audio/pulse/audio_manager_pulse.h" |
| 13 |
#endif |
| 14 |
#if defined(USE_SNDIO) |
| 15 |
#include "media/audio/sndio/sndio_input.h" |
| 16 |
#include "media/audio/sndio/sndio_output.h" |
| 17 |
#else |
| 18 |
#include "media/audio/fake_audio_manager.h" |
| 19 |
#endif |
| 20 |
#include "media/base/limits.h" |
| 21 |
#include "media/base/media_switches.h" |
| 22 |
|
| 23 |
namespace media { |
| 24 |
|
| 25 |
enum OpenBSDAudioIO { |
| 26 |
kPulse, |
| 27 |
kSndio, |
| 28 |
kAudioIOMax = kSndio |
| 29 |
}; |
| 30 |
|
| 31 |
#if defined(USE_SNDIO) |
| 32 |
// Maximum number of output streams that can be open simultaneously. |
| 33 |
static const int kMaxOutputStreams = 4; |
| 34 |
|
| 35 |
// Default sample rate for input and output streams. |
| 36 |
static const int kDefaultSampleRate = 48000; |
| 37 |
|
| 38 |
void AddDefaultDevice(AudioDeviceNames* device_names) { |
| 39 |
DCHECK(device_names->empty()); |
| 40 |
device_names->push_front(AudioDeviceName::CreateDefault()); |
| 41 |
} |
| 42 |
|
| 43 |
bool AudioManagerOpenBSD::HasAudioOutputDevices() { |
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
bool AudioManagerOpenBSD::HasAudioInputDevices() { |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
void AudioManagerOpenBSD::ShowAudioInputSettings() { |
| 52 |
NOTIMPLEMENTED(); |
| 53 |
} |
| 54 |
|
| 55 |
void AudioManagerOpenBSD::GetAudioInputDeviceNames( |
| 56 |
AudioDeviceNames* device_names) { |
| 57 |
DCHECK(device_names->empty()); |
| 58 |
AddDefaultDevice(device_names); |
| 59 |
} |
| 60 |
|
| 61 |
void AudioManagerOpenBSD::GetAudioOutputDeviceNames( |
| 62 |
AudioDeviceNames* device_names) { |
| 63 |
AddDefaultDevice(device_names); |
| 64 |
} |
| 65 |
|
| 66 |
#if defined(USE_SNDIO) |
| 67 |
const char* AudioManagerOpenBSD::GetName() { |
| 68 |
return "SNDIO"; |
| 69 |
} |
| 70 |
#endif |
| 71 |
|
| 72 |
AudioParameters AudioManagerOpenBSD::GetInputStreamParameters( |
| 73 |
const std::string& device_id) { |
| 74 |
static const int kDefaultInputBufferSize = 1024; |
| 75 |
|
| 76 |
int user_buffer_size = GetUserBufferSize(); |
| 77 |
int buffer_size = user_buffer_size ? |
| 78 |
user_buffer_size : kDefaultInputBufferSize; |
| 79 |
|
| 80 |
return AudioParameters( |
| 81 |
AudioParameters::AUDIO_PCM_LOW_LATENCY, CHANNEL_LAYOUT_STEREO, |
| 82 |
kDefaultSampleRate, 16, buffer_size); |
| 83 |
} |
| 84 |
|
| 85 |
AudioManagerOpenBSD::AudioManagerOpenBSD( |
| 86 |
scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 87 |
scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, |
| 88 |
AudioLogFactory* audio_log_factory) |
| 89 |
: AudioManagerBase(std::move(task_runner), |
| 90 |
std::move(worker_task_runner), |
| 91 |
audio_log_factory) { |
| 92 |
DLOG(WARNING) << "AudioManagerOpenBSD"; |
| 93 |
SetMaxOutputStreamsAllowed(kMaxOutputStreams); |
| 94 |
} |
| 95 |
|
| 96 |
AudioManagerOpenBSD::~AudioManagerOpenBSD() { |
| 97 |
Shutdown(); |
| 98 |
} |
| 99 |
|
| 100 |
AudioOutputStream* AudioManagerOpenBSD::MakeLinearOutputStream( |
| 101 |
const AudioParameters& params, |
| 102 |
const LogCallback& log_callback) { |
| 103 |
DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); |
| 104 |
return MakeOutputStream(params); |
| 105 |
} |
| 106 |
|
| 107 |
AudioOutputStream* AudioManagerOpenBSD::MakeLowLatencyOutputStream( |
| 108 |
const AudioParameters& params, |
| 109 |
const std::string& device_id, |
| 110 |
const LogCallback& log_callback) { |
| 111 |
DLOG_IF(ERROR, !device_id.empty()) << "Not implemented!"; |
| 112 |
DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); |
| 113 |
return MakeOutputStream(params); |
| 114 |
} |
| 115 |
|
| 116 |
AudioInputStream* AudioManagerOpenBSD::MakeLinearInputStream( |
| 117 |
const AudioParameters& params, |
| 118 |
const std::string& device_id, |
| 119 |
const LogCallback& log_callback) { |
| 120 |
DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); |
| 121 |
return MakeInputStream(params); |
| 122 |
} |
| 123 |
|
| 124 |
AudioInputStream* AudioManagerOpenBSD::MakeLowLatencyInputStream( |
| 125 |
const AudioParameters& params, |
| 126 |
const std::string& device_id, |
| 127 |
const LogCallback& log_callback) { |
| 128 |
DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format()); |
| 129 |
return MakeInputStream(params); |
| 130 |
} |
| 131 |
|
| 132 |
AudioParameters AudioManagerOpenBSD::GetPreferredOutputStreamParameters( |
| 133 |
const std::string& output_device_id, |
| 134 |
const AudioParameters& input_params) { |
| 135 |
// TODO(tommi): Support |output_device_id|. |
| 136 |
DLOG_IF(ERROR, !output_device_id.empty()) << "Not implemented!"; |
| 137 |
static const int kDefaultOutputBufferSize = 2048; |
| 138 |
|
| 139 |
ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; |
| 140 |
int sample_rate = kDefaultSampleRate; |
| 141 |
int buffer_size = kDefaultOutputBufferSize; |
| 142 |
int bits_per_sample = 16; |
| 143 |
if (input_params.IsValid()) { |
| 144 |
sample_rate = input_params.sample_rate(); |
| 145 |
bits_per_sample = input_params.bits_per_sample(); |
| 146 |
channel_layout = input_params.channel_layout(); |
| 147 |
buffer_size = std::min(buffer_size, input_params.frames_per_buffer()); |
| 148 |
} |
| 149 |
|
| 150 |
int user_buffer_size = GetUserBufferSize(); |
| 151 |
if (user_buffer_size) |
| 152 |
buffer_size = user_buffer_size; |
| 153 |
|
| 154 |
return AudioParameters( |
| 155 |
AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, |
| 156 |
sample_rate, bits_per_sample, buffer_size); |
| 157 |
} |
| 158 |
|
| 159 |
AudioInputStream* AudioManagerOpenBSD::MakeInputStream( |
| 160 |
const AudioParameters& params) { |
| 161 |
DLOG(WARNING) << "MakeInputStream"; |
| 162 |
return new SndioAudioInputStream(this, |
| 163 |
AudioDeviceDescription::kDefaultDeviceId, params); |
| 164 |
} |
| 165 |
|
| 166 |
AudioOutputStream* AudioManagerOpenBSD::MakeOutputStream( |
| 167 |
const AudioParameters& params) { |
| 168 |
DLOG(WARNING) << "MakeOutputStream"; |
| 169 |
return new SndioAudioOutputStream(params, this); |
| 170 |
} |
| 171 |
#endif |
| 172 |
|
| 173 |
ScopedAudioManagerPtr CreateAudioManager( |
| 174 |
scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 175 |
scoped_refptr<base::SingleThreadTaskRunner> worker_task_runner, |
| 176 |
AudioLogFactory* audio_log_factory) { |
| 177 |
DLOG(WARNING) << "CreateAudioManager"; |
| 178 |
#if defined(USE_PULSEAUDIO) |
| 179 |
// Do not move task runners when creating AudioManagerPulse. |
| 180 |
// If the creation fails, we need to use the task runners to create other |
| 181 |
// AudioManager implementations. |
| 182 |
std::unique_ptr<AudioManagerPulse, AudioManagerDeleter> manager( |
| 183 |
new AudioManagerPulse(task_runner, worker_task_runner, |
| 184 |
audio_log_factory)); |
| 185 |
if (manager->Init()) { |
| 186 |
UMA_HISTOGRAM_ENUMERATION("Media.OpenBSDAudioIO", kPulse, kAudioIOMax + 1); |
| 187 |
return std::move(manager); |
| 188 |
} |
| 189 |
DVLOG(1) << "PulseAudio is not available on the OS"; |
| 190 |
#endif |
| 191 |
|
| 192 |
#if defined(USE_SNDIO) |
| 193 |
UMA_HISTOGRAM_ENUMERATION("Media.OpenBSDAudioIO", kSndio, kAudioIOMax + 1); |
| 194 |
return ScopedAudioManagerPtr( |
| 195 |
new AudioManagerOpenBSD(std::move(task_runner), |
| 196 |
std::move(worker_task_runner),audio_log_factory)); |
| 197 |
#else |
| 198 |
return ScopedAudioManagerPtr( |
| 199 |
new FakeAudioManager(std::move(task_runner), |
| 200 |
std::move(worker_task_runner), audio_log_factory)); |
| 201 |
#endif |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
} // namespace media |