Skip to content
Snippets Groups Projects
Commit cce5909c authored by Tristan Matthews's avatar Tristan Matthews
Browse files

* #7097: more cleanup in audio backend

parent 16e6a3c1
No related branches found
No related tags found
No related merge requests found
...@@ -31,8 +31,8 @@ ...@@ -31,8 +31,8 @@
* as that of the covered work. * as that of the covered work.
*/ */
#ifndef _AUDIO_LAYER_H #ifndef __AUDIO_LAYER_H__
#define _AUDIO_LAYER_H #define __AUDIO_LAYER_H__
#include <cc++/thread.h> // for ost::Mutex #include <cc++/thread.h> // for ost::Mutex
#include <sys/time.h> #include <sys/time.h>
...@@ -59,7 +59,7 @@ class AudioLayer { ...@@ -59,7 +59,7 @@ class AudioLayer {
AudioLayer(const AudioLayer& rh); AudioLayer(const AudioLayer& rh);
// assignment operator // assignment operator
AudioLayer& operator= (const AudioLayer& rh); const AudioLayer& operator= (const AudioLayer& rh);
public: public:
/** /**
...@@ -70,23 +70,23 @@ class AudioLayer { ...@@ -70,23 +70,23 @@ class AudioLayer {
/** /**
* Destructor * Destructor
*/ */
virtual ~AudioLayer(void); virtual ~AudioLayer();
/** /**
* Start the capture stream and prepare the playback stream. * Start the capture stream and prepare the playback stream.
* The playback starts accordingly to its threshold * The playback starts accordingly to its threshold
* ALSA Library API * ALSA Library API
*/ */
virtual void startStream(void) = 0; virtual void startStream() = 0;
/** /**
* Stop the playback and capture streams. * Stop the playback and capture streams.
* Drops the pending frames and put the capture and playback handles to PREPARED state * Drops the pending frames and put the capture and playback handles to PREPARED state
* ALSA Library API * ALSA Library API
*/ */
virtual void stopStream(void) = 0; virtual void stopStream() = 0;
bool isStarted(void) const { bool isStarted() const {
return isStarted_; return isStarted_;
} }
...@@ -98,9 +98,9 @@ class AudioLayer { ...@@ -98,9 +98,9 @@ class AudioLayer {
*/ */
void putUrgent(void* buffer, int toCopy); void putUrgent(void* buffer, int toCopy);
void flushMain(void); void flushMain();
void flushUrgent(void); void flushUrgent();
/** /**
...@@ -115,11 +115,11 @@ class AudioLayer { ...@@ -115,11 +115,11 @@ class AudioLayer {
/** /**
* Get the mutex lock for the entire audio layer * Get the mutex lock for the entire audio layer
*/ */
ost::Mutex* getMutexLock(void) { ost::Mutex* getMutexLock() {
return &mutex_; return &mutex_;
} }
void notifyincomingCall(void); void notifyincomingCall();
protected: protected:
......
...@@ -90,7 +90,7 @@ class AudioLoop { ...@@ -90,7 +90,7 @@ class AudioLoop {
AudioLoop(const AudioLoop& rh); AudioLoop(const AudioLoop& rh);
// Assignment Operator // Assignment Operator
AudioLoop& operator= (const AudioLoop& rh); const AudioLoop& operator= (const AudioLoop& rh);
}; };
#endif // __AUDIOLOOP_H__ #endif // __AUDIOLOOP_H__
......
...@@ -51,7 +51,7 @@ struct wavhdr { ...@@ -51,7 +51,7 @@ struct wavhdr {
}; };
AudioRecord::AudioRecord() : fp(NULL) AudioRecord::AudioRecord() : fileHandle_(NULL)
, channels_(1) , channels_(1)
, byteCounter_(0) , byteCounter_(0)
, sndSmplRate_(8000) , sndSmplRate_(8000)
...@@ -59,15 +59,10 @@ AudioRecord::AudioRecord() : fp(NULL) ...@@ -59,15 +59,10 @@ AudioRecord::AudioRecord() : fp(NULL)
, nbSamplesSpk_(0) , nbSamplesSpk_(0)
, nbSamplesMax_(3000) , nbSamplesMax_(3000)
, recordingEnabled_(false) , recordingEnabled_(false)
, mixBuffer_(NULL) , mixBuffer_(new SFLDataFormat[nbSamplesMax_])
, micBuffer_(NULL) , micBuffer_(new SFLDataFormat[nbSamplesMax_])
, spkBuffer_(NULL) , spkBuffer_(new SFLDataFormat[nbSamplesMax_])
{ {
mixBuffer_ = new SFLDataFormat[nbSamplesMax_];
micBuffer_ = new SFLDataFormat[nbSamplesMax_];
spkBuffer_ = new SFLDataFormat[nbSamplesMax_];
createFilename(); createFilename();
} }
...@@ -100,10 +95,7 @@ void AudioRecord::setRecordingOption(FILE_TYPE type, int sndSmplRate, const std: ...@@ -100,10 +95,7 @@ void AudioRecord::setRecordingOption(FILE_TYPE type, int sndSmplRate, const std:
void AudioRecord::initFileName(std::string peerNumber) void AudioRecord::initFileName(std::string peerNumber)
{ {
std::string fName = fileName_;
std::string fName;
fName = fileName_;
fName.append("-" + peerNumber); fName.append("-" + peerNumber);
if (fileType_ == FILE_RAW) { if (fileType_ == FILE_RAW) {
...@@ -128,29 +120,25 @@ std::string AudioRecord::getFileName() ...@@ -128,29 +120,25 @@ std::string AudioRecord::getFileName()
bool AudioRecord::openFile() bool AudioRecord::openFile()
{ {
bool result = false; bool result = false;
_debug("AudioRecord: Open file()"); _debug("AudioRecord: Open file()");
if (isFileExist()) { if (not fileExists()) {
_debug("AudioRecord: Filename does not exist, creating one"); _debug("AudioRecord: Filename does not exist, creating one");
byteCounter_ = 0; byteCounter_ = 0;
if (fileType_ == FILE_RAW) { if (fileType_ == FILE_RAW)
result = setRawFile(); result = setRawFile();
} else if (fileType_ == FILE_WAV) { else if (fileType_ == FILE_WAV)
result = setWavFile(); result = setWavFile();
}
} else { } else {
_debug("AudioRecord: Filename already exist opening it"); _debug("AudioRecord: Filename already exists, opening it");
if (fileType_ == FILE_RAW)
if (fileType_ == FILE_RAW) {
result = openExistingRawFile(); result = openExistingRawFile();
} else if (fileType_ == FILE_WAV) { else if (fileType_ == FILE_WAV)
result = openExistingWavFile(); result = openExistingWavFile();
} }
}
return result; return result;
} }
...@@ -158,65 +146,45 @@ bool AudioRecord::openFile() ...@@ -158,65 +146,45 @@ bool AudioRecord::openFile()
void AudioRecord::closeFile() void AudioRecord::closeFile()
{ {
if (fileHandle_ == 0) return;
if (fp == 0) return;
if (fileType_ == FILE_RAW) if (fileType_ == FILE_RAW)
fclose(fp); fclose(fileHandle_);
else if (fileType_ == FILE_WAV) else if (fileType_ == FILE_WAV)
this->closeWavFile(); this->closeWavFile();
} }
bool AudioRecord::isOpenFile() bool AudioRecord::isOpenFile()
{ {
return fileHandle_ != 0;
if (fp) {
return true;
} else {
return false;
}
} }
bool AudioRecord::isFileExist() bool AudioRecord::fileExists()
{ {
_info("AudioRecord: Try to open name : %s ", fileName_); _info("AudioRecord: Trying to open %s ", fileName_);
return fopen(fileName_,"rb") != 0;
if (fopen(fileName_,"rb") ==0) {
return true;
}
return false;
} }
bool AudioRecord::isRecording() bool AudioRecord::isRecording() const
{ {
return recordingEnabled_;
if (recordingEnabled_)
return true;
else
return false;
} }
bool AudioRecord::setRecording() bool AudioRecord::setRecording()
{ {
if (isOpenFile()) { if (isOpenFile()) {
if (!recordingEnabled_) { if (!recordingEnabled_) {
_info("AudioRecording: Start recording"); _info("AudioRecording: Start recording");
recordingEnabled_ = true; recordingEnabled_ = true;
} else { } else {
recordingEnabled_ = false;
_info("AudioRecording: Stop recording"); _info("AudioRecording: Stop recording");
recordingEnabled_ = false;
} }
} else { } else {
openFile(); openFile();
recordingEnabled_ = true; // once opend file, start recording recordingEnabled_ = true; // once opend file, start recording
} }
...@@ -228,15 +196,12 @@ bool AudioRecord::setRecording() ...@@ -228,15 +196,12 @@ bool AudioRecord::setRecording()
void AudioRecord::stopRecording() void AudioRecord::stopRecording()
{ {
_info("AudioRecording: Stop recording"); _info("AudioRecording: Stop recording");
if (recordingEnabled_)
recordingEnabled_ = false; recordingEnabled_ = false;
} }
void AudioRecord::createFilename() void AudioRecord::createFilename()
{ {
time_t rawtime; time_t rawtime;
struct tm * timeinfo; struct tm * timeinfo;
...@@ -289,10 +254,9 @@ void AudioRecord::createFilename() ...@@ -289,10 +254,9 @@ void AudioRecord::createFilename()
bool AudioRecord::setRawFile() bool AudioRecord::setRawFile()
{ {
fileHandle_ = fopen(savePath_.c_str(), "wb");
fp = fopen(savePath_.c_str(), "wb"); if (!fileHandle_) {
if (!fp) {
_warn("AudioRecord: Could not create RAW file!"); _warn("AudioRecord: Could not create RAW file!");
return false; return false;
} }
...@@ -307,9 +271,9 @@ bool AudioRecord::setWavFile() ...@@ -307,9 +271,9 @@ bool AudioRecord::setWavFile()
{ {
_debug("AudioRecord: Create new wave file %s, sampling rate: %d", savePath_.c_str(), sndSmplRate_); _debug("AudioRecord: Create new wave file %s, sampling rate: %d", savePath_.c_str(), sndSmplRate_);
fp = fopen(savePath_.c_str(), "wb"); fileHandle_ = fopen(savePath_.c_str(), "wb");
if (!fp) { if (!fileHandle_) {
_warn("AudioRecord: Error: could not create WAV file."); _warn("AudioRecord: Error: could not create WAV file.");
return false; return false;
} }
...@@ -335,7 +299,7 @@ bool AudioRecord::setWavFile() ...@@ -335,7 +299,7 @@ bool AudioRecord::setWavFile()
hdr.bytes_per_sec = (SINT32)(hdr.sample_rate * hdr.bytes_per_samp); hdr.bytes_per_sec = (SINT32)(hdr.sample_rate * hdr.bytes_per_samp);
if (fwrite(&hdr, 4, 11, fp) != 11) { if (fwrite(&hdr, 4, 11, fileHandle_) != 11) {
_warn("AudioRecord: Error: could not write WAV header for file. "); _warn("AudioRecord: Error: could not write WAV header for file. ");
return false; return false;
} }
...@@ -348,9 +312,9 @@ bool AudioRecord::setWavFile() ...@@ -348,9 +312,9 @@ bool AudioRecord::setWavFile()
bool AudioRecord::openExistingRawFile() bool AudioRecord::openExistingRawFile()
{ {
fp = fopen(fileName_, "ab+"); fileHandle_ = fopen(fileName_, "ab+");
if (!fp) { if (!fileHandle_) {
_warn("AudioRecord: could not create RAW file!"); _warn("AudioRecord: could not create RAW file!");
return false; return false;
} }
...@@ -363,146 +327,123 @@ bool AudioRecord::openExistingWavFile() ...@@ -363,146 +327,123 @@ bool AudioRecord::openExistingWavFile()
{ {
_info("%s(%s)\n", __PRETTY_FUNCTION__, fileName_); _info("%s(%s)\n", __PRETTY_FUNCTION__, fileName_);
fp = fopen(fileName_, "rb+"); fileHandle_ = fopen(fileName_, "rb+");
if (!fp) { if (!fileHandle_) {
_warn("AudioRecord: Error: could not open WAV file!"); _warn("AudioRecord: Error: could not open WAV file!");
return false; return false;
} }
if (fseek(fp, 40, SEEK_SET) != 0) // jump to data length if (fseek(fileHandle_, 40, SEEK_SET) != 0) // jump to data length
_warn("AudioRecord: Error: Couldn't seek offset 40 in the file "); _warn("AudioRecord: Error: Couldn't seek offset 40 in the file ");
if (fread(&byteCounter_, 4, 1, fp)) if (fread(&byteCounter_, 4, 1, fileHandle_))
_warn("AudioRecord: Error: bytecounter Read successfully "); _warn("AudioRecord: Error: bytecounter Read successfully ");
if (fseek(fp, 0 , SEEK_END) != 0) if (fseek(fileHandle_, 0 , SEEK_END) != 0)
_warn("AudioRecord: Error: Couldn't seek at the en of the file "); _warn("AudioRecord: Error: Couldn't seek at the en of the file ");
if (fclose(fp) != 0) if (fclose(fileHandle_) != 0)
_warn("AudioRecord: Error: Can't close file r+ "); _warn("AudioRecord: Error: Can't close file r+ ");
fileHandle_ = fopen(fileName_, "ab+");
if (!fileHandle_) {
fp = fopen(fileName_, "ab+");
if (!fp) {
_warn("AudioRecord: Error: Could not createopen WAV file ab+!"); _warn("AudioRecord: Error: Could not createopen WAV file ab+!");
return false; return false;
} }
if (fseek(fp, 4 , SEEK_END) != 0) if (fseek(fileHandle_, 4 , SEEK_END) != 0)
_warn("AudioRecord: Error: Couldn't seek at the en of the file "); _warn("AudioRecord: Error: Couldn't seek at the en of the file ");
return true; return true;
} }
void AudioRecord::closeWavFile() void AudioRecord::closeWavFile()
{ {
if (fp == 0) { if (fileHandle_ == 0) {
_debug("AudioRecord: Can't closeWavFile, a file has not yet been opened!"); _debug("AudioRecord: Can't closeWavFile, a file has not yet been opened!");
return; return;
} }
_debug("AudioRecord: Close wave file"); _debug("AudioRecord: Close wave file");
SINT32 bytes = byteCounter_ * channels_; SINT32 bytes = byteCounter_ * channels_;
fseek(fp, 40, SEEK_SET); // jump to data length fseek(fileHandle_, 40, SEEK_SET); // jump to data length
if (ferror(fp)) if (ferror(fileHandle_))
_warn("AudioRecord: Error: can't reach offset 40 while closing"); _warn("AudioRecord: Error: can't reach offset 40 while closing");
fwrite(&bytes, sizeof(SINT32), 1, fp); fwrite(&bytes, sizeof(SINT32), 1, fileHandle_);
if (ferror(fp)) if (ferror(fileHandle_))
_warn("AudioRecord: Error: can't write bytes for data length "); _warn("AudioRecord: Error: can't write bytes for data length ");
bytes = byteCounter_ * channels_ + 44; // + 44 for the wave header bytes = byteCounter_ * channels_ + 44; // + 44 for the wave header
fseek(fp, 4, SEEK_SET); // jump to file size fseek(fileHandle_, 4, SEEK_SET); // jump to file size
if (ferror(fp)) if (ferror(fileHandle_))
_warn("AudioRecord: Error: can't reach offset 4"); _warn("AudioRecord: Error: can't reach offset 4");
fwrite(&bytes, 4, 1, fp); fwrite(&bytes, 4, 1, fileHandle_);
if (ferror(fp)) if (ferror(fileHandle_))
_warn("AudioRecord: Error: can't reach offset 4"); _warn("AudioRecord: Error: can't reach offset 4");
if (fclose(fileHandle_) != 0)
if (fclose(fp) != 0)
_warn("AudioRecord: Error: can't close file"); _warn("AudioRecord: Error: can't close file");
} }
void AudioRecord::recSpkrData(SFLDataFormat* buffer, int nSamples) void AudioRecord::recSpkrData(SFLDataFormat* buffer, int nSamples)
{ {
if (recordingEnabled_) { if (recordingEnabled_) {
nbSamplesMic_ = nSamples; nbSamplesMic_ = nSamples;
for (int i = 0; i < nbSamplesMic_; i++) for (int i = 0; i < nbSamplesMic_; i++)
micBuffer_[i] = buffer[i]; micBuffer_[i] = buffer[i];
} }
return;
} }
void AudioRecord::recMicData(SFLDataFormat* buffer, int nSamples) void AudioRecord::recMicData(SFLDataFormat* buffer, int nSamples)
{ {
if (recordingEnabled_) { if (recordingEnabled_) {
nbSamplesSpk_ = nSamples; nbSamplesSpk_ = nSamples;
for (int i = 0; i < nbSamplesSpk_; i++) for (int i = 0; i < nbSamplesSpk_; i++)
spkBuffer_[i] = buffer[i]; spkBuffer_[i] = buffer[i];
} }
return;
} }
void AudioRecord::recData(SFLDataFormat* buffer, int nSamples) void AudioRecord::recData(SFLDataFormat* buffer, int nSamples)
{ {
if (recordingEnabled_) { if (recordingEnabled_) {
if (fileHandle_ == 0) {
if (fp == 0) {
_debug("AudioRecord: Can't record data, a file has not yet been opened!"); _debug("AudioRecord: Can't record data, a file has not yet been opened!");
return; return;
} }
if (fwrite(buffer, sizeof(SFLDataFormat), nSamples, fp) != (unsigned int) nSamples) if (fwrite(buffer, sizeof(SFLDataFormat), nSamples, fileHandle_) != (unsigned int) nSamples)
_warn("AudioRecord: Could not record data! "); _warn("AudioRecord: Could not record data! ");
else { else {
fflush(fp); fflush(fileHandle_);
byteCounter_ += (unsigned long)(nSamples*sizeof(SFLDataFormat)); byteCounter_ += (unsigned long)(nSamples*sizeof(SFLDataFormat));
} }
} }
return;
} }
void AudioRecord::recData(SFLDataFormat* buffer_1, SFLDataFormat* buffer_2, int nSamples_1, int nSamples_2 UNUSED) void AudioRecord::recData(SFLDataFormat* buffer_1, SFLDataFormat* buffer_2,
int nSamples_1, int /*nSamples_2*/)
{ {
if (recordingEnabled_) { if (recordingEnabled_) {
if (fileHandle_ == 0) {
_debug("Recording enabled");
if (fp == 0) {
_debug("AudioRecord: Can't record data, a file has not yet been opened!"); _debug("AudioRecord: Can't record data, a file has not yet been opened!");
return; return;
} }
...@@ -510,15 +451,13 @@ void AudioRecord::recData(SFLDataFormat* buffer_1, SFLDataFormat* buffer_2, int ...@@ -510,15 +451,13 @@ void AudioRecord::recData(SFLDataFormat* buffer_1, SFLDataFormat* buffer_2, int
for (int k = 0; k < nSamples_1; k++) { for (int k = 0; k < nSamples_1; k++) {
mixBuffer_[k] = (buffer_1[k]+buffer_2[k]); mixBuffer_[k] = (buffer_1[k]+buffer_2[k]);
if (fwrite(&mixBuffer_[k], 2, 1, fp) != 1) if (fwrite(&mixBuffer_[k], 2, 1, fileHandle_) != 1)
_warn("AudioRecord: Could not record data!"); _warn("AudioRecord: Could not record data!");
else else
fflush(fp); fflush(fileHandle_);
} }
byteCounter_ += (unsigned long)(nSamples_1 * sizeof(SFLDataFormat)); byteCounter_ += (unsigned long)(nSamples_1 * sizeof(SFLDataFormat));
} }
return;
} }
...@@ -49,7 +49,7 @@ class AudioRecord { ...@@ -49,7 +49,7 @@ class AudioRecord {
/** /**
* Get the recrding sampling rate * Get the recrding sampling rate
*/ */
int getSndSamplingRate(void) const; int getSndSamplingRate() const;
void setRecordingOption(FILE_TYPE type, int sndSmplRate, const std::string &path); void setRecordingOption(FILE_TYPE type, int sndSmplRate, const std::string &path);
...@@ -61,7 +61,7 @@ class AudioRecord { ...@@ -61,7 +61,7 @@ class AudioRecord {
/** /**
* Return the filepath of the recording * Return the filepath of the recording
*/ */
std::string getFileName(void); std::string getFileName();
/** /**
* Check if no otehr file is opened, then create a new one * Check if no otehr file is opened, then create a new one
...@@ -83,14 +83,14 @@ class AudioRecord { ...@@ -83,14 +83,14 @@ class AudioRecord {
bool isOpenFile(); bool isOpenFile();
/** /**
* Check if a file already exist * Check if a file already exists
*/ */
bool isFileExist(); bool fileExists();
/** /**
* Check recording state * Check recording state
*/ */
bool isRecording(); bool isRecording() const;
/** /**
* Set recording flag * Set recording flag
...@@ -170,7 +170,7 @@ class AudioRecord { ...@@ -170,7 +170,7 @@ class AudioRecord {
/** /**
* Pointer to the recorded file * Pointer to the recorded file
*/ */
FILE *fp; //file pointer FILE *fileHandle_;
/** /**
* File format (RAW / WAVE) * File format (RAW / WAVE)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment