Commit 4d471ba0 authored by Tristan Matthews's avatar Tristan Matthews
Browse files

* #26839: audio: fix formatting with astyle

parent 3209eb47
......@@ -74,6 +74,7 @@ bool AlsaThread::isRunning() const
AlsaThread::~AlsaThread()
{
running_ = false;
if (thread_)
pthread_join(thread_, NULL);
}
......@@ -198,7 +199,7 @@ bool AlsaLayer::openDevice(snd_pcm_t **pcm, const std::string &dev, snd_pcm_stre
if (err < 0) {
ERROR("Alsa: couldn't open device %s : %s", dev.c_str(),
snd_strerror(err));
snd_strerror(err));
return false;
}
......@@ -388,11 +389,12 @@ bool AlsaLayer::alsa_set_params(snd_pcm_t *pcm_handle)
ERROR("buffer to small, could not use");
return false;
}
#undef HW
DEBUG("%s using sampling rate %dHz",
(snd_pcm_stream(pcm_handle) == SND_PCM_STREAM_PLAYBACK) ? "playback" : "capture",
sampleRate_);
(snd_pcm_stream(pcm_handle) == SND_PCM_STREAM_PLAYBACK) ? "playback" : "capture",
sampleRate_);
snd_pcm_sw_params_t *swparams = NULL;
snd_pcm_sw_params_alloca(&swparams);
......@@ -414,7 +416,7 @@ AlsaLayer::write(void* buffer, int length, snd_pcm_t * handle)
{
//Do not waste CPU cycle to handle void
if (!length)
return;
return;
snd_pcm_uframes_t frames = snd_pcm_bytes_to_frames(handle, length);
watchdogTotalCount_++;
......@@ -454,12 +456,14 @@ AlsaLayer::write(void* buffer, int length, snd_pcm_t * handle)
ERROR("Writing in state SND_PCM_STATE_SETUP, should be "
"SND_PCM_STATE_PREPARED or SND_PCM_STATE_RUNNING");
int error = snd_pcm_prepare(handle);
if (error < 0) {
ERROR("Failed to prepare handle: %s", snd_strerror(error));
stopPlaybackStream();
}
}
}
break;
}
......@@ -539,13 +543,16 @@ namespace {
bool safeUpdate(snd_pcm_t *handle, int &samples)
{
samples = snd_pcm_avail_update(handle);
if (samples < 0) {
samples = snd_pcm_recover(handle, samples, 0);
if (samples < 0) {
ERROR("Got unrecoverable error from snd_pcm_avail_update: %s", snd_strerror(samples));
return false;
}
}
return true;
}
......@@ -553,9 +560,11 @@ std::vector<std::string>
getValues(const std::vector<HwIDPair> &deviceMap)
{
std::vector<std::string> audioDeviceList;
for (std::vector<HwIDPair>::const_iterator iter = deviceMap.begin();
iter != deviceMap.end(); ++iter)
audioDeviceList.push_back(iter->second);
return audioDeviceList;
}
}
......@@ -600,12 +609,11 @@ AlsaLayer::getAudioDeviceIndexMap(bool getCapture) const
if (snd_ctl_pcm_info(handle ,pcminfo) < 0) {
DEBUG(" Cannot get info");
}
else {
} else {
DEBUG("card %i : %s [%s]",
numCard,
snd_ctl_card_info_get_id(info),
snd_ctl_card_info_get_name(info));
numCard,
snd_ctl_card_info_get_id(info),
snd_ctl_card_info_get_name(info));
std::string description = snd_ctl_card_info_get_name(info);
description.append(" - ");
description.append(snd_pcm_info_get_name(pcminfo));
......@@ -635,6 +643,7 @@ AlsaLayer::soundCardIndexExists(int card, PCMType stream)
name.append(ss.str());
snd_ctl_t* handle;
if (snd_ctl_open(&handle, name.c_str(), 0) != 0)
return false;
......@@ -672,8 +681,10 @@ AlsaLayer::getAudioDeviceName(int index, PCMType type) const
case SFL_PCM_PLAYBACK:
case SFL_PCM_RINGTONE:
return getPlaybackDeviceList().at(index);
case SFL_PCM_CAPTURE:
return getCaptureDeviceList().at(index);
default:
ERROR("Unexpected type %d", type);
return "";
......@@ -700,6 +711,7 @@ void AlsaLayer::capture()
SFLDataFormat * const in_ptr = &(*in.begin());
const int toGetBytes = in.size() * sizeof(in[0]);
if (read(in_ptr, toGetBytes) != toGetBytes) {
ERROR("ALSA MIC : Couldn't read!");
return;
......@@ -712,15 +724,15 @@ void AlsaLayer::capture()
std::vector<SFLDataFormat> rsmpl_out(outSamples);
SFLDataFormat * const rsmpl_out_ptr = &(*rsmpl_out.begin());
converter_.resample(in_ptr, rsmpl_out_ptr,
rsmpl_out.size(), mainBufferSampleRate, sampleRate_,
toGetSamples);
rsmpl_out.size(), mainBufferSampleRate, sampleRate_,
toGetSamples);
dcblocker_.process(rsmpl_out_ptr, rsmpl_out_ptr, outSamples);
Manager::instance().getMainBuffer().putData(rsmpl_out_ptr,
rsmpl_out.size() * sizeof(rsmpl_out[0]), MainBuffer::DEFAULT_ID);
} else {
dcblocker_.process(in_ptr, in_ptr, toGetSamples);
Manager::instance().getMainBuffer().putData(in_ptr, toGetBytes,
MainBuffer::DEFAULT_ID);
MainBuffer::DEFAULT_ID);
}
}
......@@ -729,6 +741,7 @@ void AlsaLayer::playback(int maxSamples)
size_t bytesToGet = Manager::instance().getMainBuffer().availableForGet(MainBuffer::DEFAULT_ID);
const size_t bytesToPut = maxSamples * sizeof(SFLDataFormat);
// no audio available, play tone or silence
if (bytesToGet <= 0) {
// FIXME: not thread safe! we only lock the mutex when we get the
......@@ -738,6 +751,7 @@ void AlsaLayer::playback(int maxSamples)
std::vector<SFLDataFormat> out(maxSamples, 0);
SFLDataFormat * const out_ptr = &(*out.begin());
if (tone)
tone->getNext(out_ptr, out.size(), getPlaybackGain());
else if (file_tone && !ringtoneHandle_)
......@@ -752,6 +766,7 @@ void AlsaLayer::playback(int maxSamples)
double resampleFactor = 1.0;
size_t maxNbBytesToGet = bytesToPut;
if (resample) {
resampleFactor = static_cast<double>(sampleRate_) / mainBufferSampleRate;
maxNbBytesToGet = bytesToGet / resampleFactor;
......@@ -771,7 +786,7 @@ void AlsaLayer::playback(int maxSamples)
std::vector<SFLDataFormat> rsmpl_out(outSamples);
SFLDataFormat * const rsmpl_out_ptr = &(*rsmpl_out.begin());
converter_.resample(out_ptr, rsmpl_out_ptr, rsmpl_out.size(),
mainBufferSampleRate, sampleRate_, samplesToGet);
mainBufferSampleRate, sampleRate_, samplesToGet);
write(rsmpl_out_ptr, outBytes, playbackHandle_);
} else {
write(out_ptr, bytesToGet, playbackHandle_);
......@@ -789,8 +804,10 @@ void AlsaLayer::audioCallback()
snd_pcm_wait(playbackHandle_, 20);
int playbackAvailSmpl = 0;
if (not safeUpdate(playbackHandle_, playbackAvailSmpl))
return;
const size_t playbackAvailBytes = playbackAvailSmpl * sizeof(SFLDataFormat);
size_t bytesToGet = urgentRingBuffer_.availableForGet(MainBuffer::DEFAULT_ID);
......@@ -815,8 +832,10 @@ void AlsaLayer::audioCallback()
if (ringtoneHandle_) {
AudioLoop *file_tone = Manager::instance().getTelephoneFile();
int ringtoneAvailSmpl = 0;
if (not safeUpdate(ringtoneHandle_, ringtoneAvailSmpl))
return;
int ringtoneAvailBytes = ringtoneAvailSmpl * sizeof(SFLDataFormat);
std::vector<SFLDataFormat> out(ringtoneAvailSmpl, 0);
......@@ -842,12 +861,15 @@ void AlsaLayer::updatePreference(AudioPreference &preference, int index, PCMType
case SFL_PCM_PLAYBACK:
preference.setAlsaCardout(index);
break;
case AudioLayer::SFL_PCM_CAPTURE:
preference.setAlsaCardin(index);
break;
case AudioLayer::SFL_PCM_RINGTONE:
preference.setAlsaCardring(index);
break;
default:
break;
}
......
......@@ -99,16 +99,18 @@ AudioLoop::getNext(SFLDataFormat* output, size_t total_samples, short volume)
// We want to send values in milisecond
int divisor = sampleRate_ / 1000;
if(divisor == 0) {
if (divisor == 0) {
ERROR("Error cannot update playback slider, sampling rate is 0");
return;
}
if(isRecording_) {
if((updatePlaybackScale % 5) == 0) {
if (isRecording_) {
if ((updatePlaybackScale % 5) == 0) {
CallManager *cm = Manager::instance().getClient()->getCallManager();
cm->updatePlaybackScale(pos_ / divisor, size_ / divisor);
}
updatePlaybackScale++;
}
}
......
......@@ -149,7 +149,7 @@ namespace {
bool
nonFilenameCharacter(char c)
{
return not (std::isalnum(c) or c == '_' or c == '.');
return not(std::isalnum(c) or c == '_' or c == '.');
}
// Replace any character that is inappropriate for a filename with '_'
......@@ -200,6 +200,7 @@ bool AudioRecord::openFile()
result = setWavFile();
} else {
DEBUG("Filename already exists, opening it");
if (fileType_ == FILE_RAW)
result = openExistingRawFile();
else if (fileType_ == FILE_WAV)
......@@ -242,6 +243,7 @@ bool AudioRecord::toggleRecording()
openFile();
recordingEnabled_ = true;
}
return recordingEnabled_;
}
......@@ -266,24 +268,24 @@ bool AudioRecord::setRawFile()
}
namespace {
std::string header_to_string(const wavhdr &hdr)
{
std::stringstream ss;
ss << hdr.riff << "\0 "
<< hdr.file_size << " "
<< hdr.wave << "\0 "
<< hdr.fmt << "\0 "
<< hdr.chunk_size << " "
<< hdr.format_tag << " "
<< hdr.num_chans << " "
<< hdr.sample_rate << " "
<< hdr.bytes_per_sec << " "
<< hdr.bytes_per_samp << " "
<< hdr.bits_per_samp << " "
<< hdr.data << "\0 "
<< hdr.data_length;
return ss.str();
}
std::string header_to_string(const wavhdr &hdr)
{
std::stringstream ss;
ss << hdr.riff << "\0 "
<< hdr.file_size << " "
<< hdr.wave << "\0 "
<< hdr.fmt << "\0 "
<< hdr.chunk_size << " "
<< hdr.format_tag << " "
<< hdr.num_chans << " "
<< hdr.sample_rate << " "
<< hdr.bytes_per_sec << " "
<< hdr.bytes_per_samp << " "
<< hdr.bits_per_samp << " "
<< hdr.data << "\0 "
<< hdr.data_length;
return ss.str();
}
}
bool AudioRecord::setWavFile()
......@@ -301,18 +303,19 @@ bool AudioRecord::setWavFile()
* write them as arrays since strings enclosed in quotes include a
* null character */
wavhdr hdr = {{'R', 'I', 'F', 'F'},
44,
{'W', 'A', 'V', 'E'},
{'f','m', 't', ' '},
16,
1,
channels_,
sndSmplRate_,
-1, /* initialized below */
-1, /* initialized below */
16,
{'d', 'a', 't', 'a'},
0};
44,
{'W', 'A', 'V', 'E'},
{'f','m', 't', ' '},
16,
1,
channels_,
sndSmplRate_,
-1, /* initialized below */
-1, /* initialized below */
16,
{'d', 'a', 't', 'a'},
0
};
hdr.bytes_per_samp = channels_ * hdr.bits_per_samp / 8;
hdr.bytes_per_sec = hdr.sample_rate * hdr.bytes_per_samp;
......
......@@ -56,8 +56,10 @@ AudioRecorder::AudioRecorder(AudioRecord *arec, MainBuffer *mb) :
recorderId_ = id.append(s);
}
AudioRecorder::~AudioRecorder() {
AudioRecorder::~AudioRecorder()
{
running_ = false;
if (thread_)
pthread_join(thread_, NULL);
}
......
......@@ -68,6 +68,7 @@ AudioRtpFactory::~AudioRtpFactory()
void AudioRtpFactory::initConfig()
{
DEBUG("AudioRtpFactory: init config");
if (rtpSession_ != NULL)
stop();
......@@ -78,18 +79,22 @@ void AudioRtpFactory::initConfig()
if (account) {
srtpEnabled_ = account->getSrtpEnabled();
std::string key(account->getSrtpKeyExchange());
if (srtpEnabled_) {
#if HAVE_ZRTP
if (key == "sdes")
keyExchangeProtocol_ = SDES;
else if (key == "zrtp")
keyExchangeProtocol_ = ZRTP;
#else
keyExchangeProtocol_ = SDES;
keyExchangeProtocol_ = SDES;
#endif
} else {
keyExchangeProtocol_ = NONE;
}
helloHashEnabled_ = account->getZrtpHelloHash();
} else {
srtpEnabled_ = false;
......@@ -107,14 +112,18 @@ void AudioRtpFactory::initSession()
switch (keyExchangeProtocol_) {
#if HAVE_ZRTP
case ZRTP:
rtpSession_ = new AudioZrtpSession(*ca_, zidFilename);
// TODO: be careful with that. The hello hash is computed asynchronously. Maybe it's
// not even available at that point.
if (helloHashEnabled_)
ca_->getLocalSDP()->setZrtpHash(static_cast<AudioZrtpSession *>(rtpSession_)->getHelloHash());
break;
#endif
case SDES:
rtpSession_ = new AudioSrtpSession(*ca_);
break;
......@@ -192,6 +201,7 @@ AudioZrtpSession * AudioRtpFactory::getAudioZrtpSession()
void AudioRtpFactory::initLocalCryptoInfo()
{
DEBUG("AudioRtpFactory: Init local crypto info");
if (rtpSession_ && keyExchangeProtocol_ == SDES) {
AudioSrtpSession *srtp = static_cast<AudioSrtpSession*>(rtpSession_);
// the context is invalidated and deleted by the call to initLocalCryptoInfo
......@@ -203,6 +213,7 @@ void AudioRtpFactory::initLocalCryptoInfo()
void AudioRtpFactory::initLocalCryptoInfoOnOffHold()
{
DEBUG("AudioRtpFactory: Init local crypto info");
if (rtpSession_ && keyExchangeProtocol_ == SDES) {
AudioSrtpSession *srtp = static_cast<AudioSrtpSession*>(rtpSession_);
// the context is invalidated and deleted by the call to initLocalCryptoInfo
......@@ -214,7 +225,7 @@ void AudioRtpFactory::initLocalCryptoInfoOnOffHold()
void AudioRtpFactory::setRemoteCryptoInfo(SdesNegotiator& nego)
{
if (rtpSession_ ) {
if (rtpSession_) {
if (keyExchangeProtocol_ == SDES) {
AudioSrtpSession *srtp = static_cast<AudioSrtpSession *>(rtpSession_);
srtp->setRemoteCryptoInfo(nego);
......
......@@ -43,37 +43,41 @@
namespace sfl {
#ifdef RECTODISK
std::ofstream rtpResampled ("testRtpOutputResampled.raw", std::ifstream::binary);
std::ofstream rtpResampled("testRtpOutputResampled.raw", std::ifstream::binary);
std::ofstream rtpNotResampled("testRtpOutput.raw", std::ifstream::binary);
#endif
DTMFEvent::DTMFEvent(char digit) : payload(), newevent(true), length(1000)
{
/*
From RFC2833:
Event encoding (decimal)
_________________________
0--9 0--9
* 10
# 11
A--D 12--15
Flash 16
*/
/*
From RFC2833:
Event encoding (decimal)
_________________________
0--9 0--9
* 10
# 11
A--D 12--15
Flash 16
*/
switch (digit) {
case '*':
digit = 10;
break;
case '#':
digit = 11;
break;
case 'A' ... 'D':
digit = digit - 'A' + 12;
break;
case '0' ... '9':
digit = digit - '0';
break;
default:
ERROR("Unexpected DTMF %c", digit);
}
......@@ -85,7 +89,7 @@ DTMFEvent::DTMFEvent(char digit) : payload(), newevent(true), length(1000)
}
AudioRtpRecord::AudioRtpRecord() :
callId_("")
callId_("")
, codecSampleRate_(0)
, dtmfQueue_()
, audioCodecs_()
......@@ -133,6 +137,7 @@ AudioRtpRecord::getCurrentCodec() const
ERROR("No codec found");
return 0;
}
return audioCodecs_[currentCodecIndex_];
}
......@@ -141,6 +146,7 @@ AudioRtpRecord::deleteCodecs()
{
for (std::vector<AudioCodec *>::iterator i = audioCodecs_.begin(); i != audioCodecs_.end(); ++i)
delete *i;
audioCodecs_.clear();
}
......@@ -211,10 +217,12 @@ AudioRtpRecordHandler::getCurrentAudioCodecNames()
ScopedLock lock(audioRtpRecord_.audioCodecMutex_);
{
std::string sep = "";
for (std::vector<AudioCodec*>::const_iterator i = audioRtpRecord_.audioCodecs_.begin();
i != audioRtpRecord_.audioCodecs_.end(); ++i) {
if (*i)
result += sep + (*i)->getMimeSubtype();
sep = " ";
}
}
......@@ -229,6 +237,7 @@ void AudioRtpRecordHandler::setRtpMedia(const std::vector<AudioCodec*> &audioCod
audioRtpRecord_.deleteCodecs();
// Set various codec info to reduce indirection
audioRtpRecord_.audioCodecs_ = audioCodecs;
if (audioCodecs.empty()) {
ERROR("Audio codecs empty");
return;
......@@ -318,18 +327,20 @@ int AudioRtpRecordHandler::processDataEncode()
samplesToGet);
#ifdef RECTODISK
rtpResampled.write((const char *)audioRtpRecord_.resampledData_.data(), samplesToGet*sizeof(SFLDataFormat)/2 );
rtpResampled.write((const char *)audioRtpRecord_.resampledData_.data(), samplesToGet*sizeof(SFLDataFormat)/2);
#endif
out = audioRtpRecord_.resampledData_.data();
}
#if HAVE_SPEEXDSP
if (Manager::instance().audioPreference.getNoiseReduce()) {
ScopedLock lock(audioRtpRecord_.audioProcessMutex_);
RETURN_IF_NULL(audioRtpRecord_.noiseSuppressEncode_, 0, "Noise suppressor already destroyed");
audioRtpRecord_.noiseSuppressEncode_->process(micData, getCodecFrameSize());
}
#endif
{
......@@ -347,13 +358,16 @@ void AudioRtpRecordHandler::processDataDecode(unsigned char *spkrData, size_t si
{
if (audioRtpRecord_.isDead())
return;
if (audioRtpRecord_.decoderPayloadType_ != payloadType) {
const bool switched = audioRtpRecord_.tryToSwitchPayloadTypes(payloadType);
if (not switched) {
if (!warningInterval_) {
warningInterval_ = 250;
WARN("Invalid payload type %d, expected %d", payloadType, audioRtpRecord_.decoderPayloadType_);
}
warningInterval_--;
return;
}
......@@ -370,11 +384,13 @@ void AudioRtpRecordHandler::processDataDecode(unsigned char *spkrData, size_t si
}
#if HAVE_SPEEXDSP
if (Manager::instance().audioPreference.getNoiseReduce()) {
ScopedLock lock(audioRtpRecord_.audioProcessMutex_);
RETURN_IF_NULL(audioRtpRecord_.noiseSuppressDecode_, "Noise suppressor already destroyed");
audioRtpRecord_.noiseSuppressDecode_->process(spkrDataDecoded, getCodecFrameSize());
}
#endif
audioRtpRecord_.fadeInDecodedData(inSamples);
......@@ -421,17 +437,22 @@ bool
AudioRtpRecordHandler::codecsDiffer(const std::vector<AudioCodec*> &codecs) const
{
const std::vector<AudioCodec*> &current = audioRtpRecord_.audioCodecs_;
if (codecs.size() != current.size())
return true;
for (std::vector<AudioCodec*>::const_iterator i = codecs.begin(); i != codecs.end(); ++i) {
if (*i) {
bool matched = false;
for (std::vector<AudioCodec*>::const_iterator j = current.begin(); !matched and j != current.end(); ++j)
matched = (*i)->getPayloadType() == (*j)->getPayloadType();
if (not matched)
return true;
}
}
return false;
}
......
......@@ -71,11 +71,13 @@ void AudioRtpSession::updateSessionMedia(const std::vector<AudioCodec*> &audioCo
Manager::instance().audioSamplingRateChanged(audioRtpRecord_.codecSampleRate_);
#if HAVE_SPEEXDSP
if (lastSamplingRate != audioRtpRecord_.codecSampleRate_) {
DEBUG("Update noise suppressor with sampling rate %d and frame size %d",
getCodecSampleRate(), getCodecFrameSize());
initNoiseSuppress();
}
#endif
}
......@@ -85,6 +87,7 @@ void AudioRtpSession::setSessionMedia(const std::vector<AudioCodec*> &audioCodec
// G722 requires timestamp to be incremented at 8kHz
const ost::PayloadType payloadType = getEncoderPayloadType();
if (payloadType == ost::sptG722) {
const int G722_RTP_TIME_INCREMENT = 160;
timestampIncrement_ = G722_RTP_TIME_INCREMENT;
......@@ -93,7 +96,7 @@ void AudioRtpSession::setSessionMedia(const std::vector<AudioCodec*> &audioCodec
if (payloadType == ost::sptG722) {
const int G722_RTP_CLOCK_RATE = 8000;