This source file includes following definitions.
- stream_direction_
- Open
- Close
- Start
- Stop
- SamplesReady
- StreamError
- ReadAudio
- NotifyStreamError
- GetMaxVolume
- SetVolume
- GetVolume
- GetVolumeRatioFromDecibels
- GetDecibelsFromVolumeRatio
#include "media/audio/cras/cras_input.h"
#include <math.h>
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/time/time.h"
#include "media/audio/audio_manager.h"
#include "media/audio/cras/audio_manager_cras.h"
namespace media {
CrasInputStream::CrasInputStream(const AudioParameters& params,
AudioManagerCras* manager,
const std::string& device_id)
: audio_manager_(manager),
bytes_per_frame_(0),
callback_(NULL),
client_(NULL),
params_(params),
started_(false),
stream_id_(0),
stream_direction_(device_id == AudioManagerBase::kLoopbackInputDeviceId ?
CRAS_STREAM_POST_MIX_PRE_DSP : CRAS_STREAM_INPUT) {
DCHECK(audio_manager_);
}
CrasInputStream::~CrasInputStream() {
DCHECK(!client_);
}
bool CrasInputStream::Open() {
if (client_) {
NOTREACHED() << "CrasInputStream already open";
return false;
}
if (params_.sample_rate() <= 0) {
DLOG(WARNING) << "Unsupported audio frequency.";
return false;
}
if (AudioParameters::AUDIO_PCM_LINEAR != params_.format() &&
AudioParameters::AUDIO_PCM_LOW_LATENCY != params_.format()) {
DLOG(WARNING) << "Unsupported audio format.";
return false;
}
snd_pcm_format_t pcm_format =
AudioManagerCras::BitsToFormat(params_.bits_per_sample());
if (pcm_format == SND_PCM_FORMAT_UNKNOWN) {
DLOG(WARNING) << "Unsupported bits/sample: " << params_.bits_per_sample();
return false;
}
if (cras_client_create(&client_) < 0) {
DLOG(WARNING) << "Couldn't create CRAS client.\n";
client_ = NULL;
return false;
}
if (cras_client_connect(client_)) {
DLOG(WARNING) << "Couldn't connect CRAS client.\n";
cras_client_destroy(client_);
client_ = NULL;
return false;
}
if (cras_client_run_thread(client_)) {
DLOG(WARNING) << "Couldn't run CRAS client.\n";
cras_client_destroy(client_);
client_ = NULL;
return false;
}
return true;
}
void CrasInputStream::Close() {
Stop();
if (client_) {
cras_client_stop(client_);
cras_client_destroy(client_);
client_ = NULL;
}
audio_manager_->ReleaseInputStream(this);
}
void CrasInputStream::Start(AudioInputCallback* callback) {
DCHECK(client_);
DCHECK(callback);
if (started_)
return;
StartAgc();
callback_ = callback;
cras_audio_format* audio_format = cras_audio_format_create(
AudioManagerCras::BitsToFormat(params_.bits_per_sample()),
params_.sample_rate(),
params_.channels());
if (!audio_format) {
DLOG(WARNING) << "Error setting up audio parameters.";
callback_->OnError(this);
callback_ = NULL;
return;
}
unsigned int frames_per_packet = params_.frames_per_buffer();
cras_stream_params* stream_params = cras_client_stream_params_create(
stream_direction_,
frames_per_packet,
frames_per_packet,
frames_per_packet,
CRAS_STREAM_TYPE_DEFAULT,
0,
this,
CrasInputStream::SamplesReady,
CrasInputStream::StreamError,
audio_format);
if (!stream_params) {
DLOG(WARNING) << "Error setting up stream parameters.";
callback_->OnError(this);
callback_ = NULL;
cras_audio_format_destroy(audio_format);
return;
}
bytes_per_frame_ = cras_client_format_bytes_per_frame(audio_format);
if (cras_client_add_stream(client_, &stream_id_, stream_params)) {
DLOG(WARNING) << "Failed to add the stream.";
callback_->OnError(this);
callback_ = NULL;
}
cras_audio_format_destroy(audio_format);
cras_client_stream_params_destroy(stream_params);
started_ = true;
}
void CrasInputStream::Stop() {
DCHECK(client_);
if (!callback_ || !started_)
return;
StopAgc();
cras_client_rm_stream(client_, stream_id_);
started_ = false;
callback_ = NULL;
}
int CrasInputStream::SamplesReady(cras_client* client,
cras_stream_id_t stream_id,
uint8* samples,
size_t frames,
const timespec* sample_ts,
void* arg) {
CrasInputStream* me = static_cast<CrasInputStream*>(arg);
me->ReadAudio(frames, samples, sample_ts);
return frames;
}
int CrasInputStream::StreamError(cras_client* client,
cras_stream_id_t stream_id,
int err,
void* arg) {
CrasInputStream* me = static_cast<CrasInputStream*>(arg);
me->NotifyStreamError(err);
return 0;
}
void CrasInputStream::ReadAudio(size_t frames,
uint8* buffer,
const timespec* sample_ts) {
DCHECK(callback_);
timespec latency_ts = {0, 0};
cras_client_calc_capture_latency(sample_ts, &latency_ts);
double latency_usec =
latency_ts.tv_sec * base::Time::kMicrosecondsPerSecond +
latency_ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond;
double frames_latency =
latency_usec * params_.sample_rate() / base::Time::kMicrosecondsPerSecond;
unsigned int bytes_latency =
static_cast<unsigned int>(frames_latency * bytes_per_frame_);
double normalized_volume = 0.0;
GetAgcVolume(&normalized_volume);
callback_->OnData(this,
buffer,
frames * bytes_per_frame_,
bytes_latency,
normalized_volume);
}
void CrasInputStream::NotifyStreamError(int err) {
if (callback_)
callback_->OnError(this);
}
double CrasInputStream::GetMaxVolume() {
DCHECK(client_);
double dB = cras_client_get_system_max_capture_gain(client_) / 100.0;
return GetVolumeRatioFromDecibels(dB);
}
void CrasInputStream::SetVolume(double volume) {
DCHECK(client_);
double dB = GetDecibelsFromVolumeRatio(volume);
cras_client_set_system_capture_gain(client_, static_cast<long>(dB * 100.0));
UpdateAgcVolume();
}
double CrasInputStream::GetVolume() {
if (!client_)
return 0.0;
long dB = cras_client_get_system_capture_gain(client_) / 100.0;
return GetVolumeRatioFromDecibels(dB);
}
double CrasInputStream::GetVolumeRatioFromDecibels(double dB) const {
return pow(10, dB / 20.0);
}
double CrasInputStream::GetDecibelsFromVolumeRatio(double volume_ratio) const {
return 20 * log10(volume_ratio);
}
}