Commit fa186c4d authored by Rafaël Carré's avatar Rafaël Carré
Browse files

* #6629 : simplify pulse callbacks

parent 84ec8623
......@@ -130,5 +130,7 @@ AudioStream::stream_state_callback (pa_stream* s, void* user_data)
bool AudioStream::isReady (void)
{
if (!_audiostream)
return false;
return pa_stream_get_state (_audiostream) == PA_STREAM_READY;
}
......@@ -42,21 +42,21 @@ void playback_callback (pa_stream* s, size_t bytes, void* userdata)
{
assert (s && bytes);
assert (bytes > 0);
static_cast<PulseLayer*> (userdata)->processPlaybackData();
static_cast<PulseLayer*> (userdata)->writeToSpeaker();
}
void capture_callback (pa_stream* s, size_t bytes, void* userdata)
{
assert (s && bytes);
assert (bytes > 0);
static_cast<PulseLayer*> (userdata)->processCaptureData();
static_cast<PulseLayer*> (userdata)->readFromMic();
}
void ringtone_callback (pa_stream* s, size_t bytes, void* userdata)
{
assert (s && bytes);
assert (bytes > 0);
static_cast<PulseLayer*> (userdata)->processRingtoneData();
static_cast<PulseLayer*> (userdata)->ringtoneToSpeaker();
}
void stream_moved_callback (pa_stream *s, void *userdata UNUSED)
......@@ -405,72 +405,20 @@ PulseLayer::stopStream (void)
disconnectAudioStream();
}
void PulseLayer::processPlaybackData (void)
{
// Handle the data for the speakers
if (playback_ and playback_->pulseStream() and (pa_stream_get_state (playback_->pulseStream()) == PA_STREAM_READY)) {
// If the playback buffer is full, we don't overflow it; wait for it to have free space
if (pa_stream_writable_size (playback_->pulseStream()) == 0)
return;
writeToSpeaker();
}
}
void PulseLayer::processCaptureData (void)
{
// Handle the mic
// We check if the stream is ready
if (record_ and record_->pulseStream() and pa_stream_get_state (record_->pulseStream()) == PA_STREAM_READY)
readFromMic();
}
void PulseLayer::processRingtoneData (void)
{
// handle ringtone playback
if (ringtone_ and ringtone_->pulseStream() and (pa_stream_get_state (ringtone_->pulseStream()) == PA_STREAM_READY)) {
// If the playback buffer is full, we don't overflow it; wait for it to have free space
if (pa_stream_writable_size (ringtone_->pulseStream()) == 0)
return;
ringtoneToSpeaker();
}
}
void PulseLayer::processData (void)
{
// Handle the data for the speakers
if (playback_ and playback_->pulseStream() and (pa_stream_get_state (playback_->pulseStream()) == PA_STREAM_READY)) {
// If the playback buffer is full, we don't overflow it; wait for it to have free space
if (pa_stream_writable_size (playback_->pulseStream()) == 0)
return;
writeToSpeaker();
}
// Handle the mic
// We check if the stream is ready
if (record_ and record_->pulseStream() and (pa_stream_get_state (record_->pulseStream()) == PA_STREAM_READY))
readFromMic();
}
void PulseLayer::writeToSpeaker (void)
{
notifyincomingCall();
if (!playback_ or !playback_->isReady())
return;
// available bytes to be written in pulseaudio internal buffer
int writeableSizeBytes = pa_stream_writable_size (playback_->pulseStream());
if (writeableSizeBytes < 0) {
if (writeableSizeBytes < 0)
_error("Pulse: playback error : %s", pa_strerror (writeableSizeBytes));
if (writeableSizeBytes <= 0)
return;
}
notifyincomingCall();
int urgentBytes = urgentRingBuffer_.AvailForGet();
if (urgentBytes > writeableSizeBytes)
......@@ -530,7 +478,6 @@ void PulseLayer::writeToSpeaker (void)
SFLDataFormat *out = (SFLDataFormat*) pa_xmalloc (inBytes);
getMainBuffer()->getData (out, inBytes);
// test if resampling is required
if (resample) {
SFLDataFormat* rsmpl_out = (SFLDataFormat*) pa_xmalloc (outBytes);
converter_->resample (out, rsmpl_out, mainBufferSampleRate, audioSampleRate_, inSamples);
......@@ -544,6 +491,9 @@ void PulseLayer::writeToSpeaker (void)
void PulseLayer::readFromMic (void)
{
if (!record_ or !record_->isReady())
return;
const char *data = NULL;
size_t r;
SFLDataFormat *out;
......@@ -581,12 +531,14 @@ end:
void PulseLayer::ringtoneToSpeaker (void)
{
int writableSize = pa_stream_writable_size (ringtone_->pulseStream());
if (!ringtone_ or !ringtone_->isReady())
return;
if (!ringtone_->isReady()) {
_error("PulseAudio: Error: Ringtone stream not in state ready");
int writableSize = pa_stream_writable_size (ringtone_->pulseStream());
if (writableSize < 0)
_error("Pulse: ringtone error : %s", pa_strerror (writableSize));
if (writableSize <= 0)
return;
}
SFLDataFormat *out = (SFLDataFormat *) pa_xmalloc (writableSize);
......
......@@ -65,6 +65,14 @@ class PulseLayer : public AudioLayer
return &sourceList_;
}
/**
* Write data from the ring buffer to the harware and read data from the hardware
*/
void readFromMic (void);
void writeToSpeaker (void);
void ringtoneToSpeaker (void);
void updateSinkList (void);
void updateSourceList (void);
......@@ -79,67 +87,6 @@ class PulseLayer : public AudioLayer
static void context_state_callback (pa_context* c, void* user_data);
void setPlaybackVolume (int volume);
void setCaptureVolume (int volume);
/**
* Accessor
* @return AudioStream* The pointer on the playback AudioStream object
*/
AudioStream* getPlaybackStream() const {
return playback_;
}
/**
* Accessor
* @return AudioStream* The pointer on the record AudioStream object
*/
AudioStream* getRecordStream() const {
return record_;
}
/**
* Accessor
* @return AudioStream* The pointer on the ringtone AudioStream object
*/
AudioStream* getRingtoneStream() const {
return ringtone_;
}
int getSpkrVolume (void) const {
return spkrVolume_;
}
void setSpkrVolume (int value) {
spkrVolume_ = value;
}
int getMicVolume (void) const {
return micVolume_;
}
void setMicVolume (int value) {
micVolume_ = value;
}
/**
* Handle used to write voice data to speaker
*/
void processPlaybackData (void);
/**
* Handle used to write voice data to microphone
*/
void processCaptureData (void);
/**
* Handle used to write audio data to speaker
*/
void processRingtoneData (void);
/**
* Process speaker and microphone audio data
*/
void processData (void);
private:
// Copy Constructor
PulseLayer (const PulseLayer& rh);
......@@ -147,13 +94,6 @@ class PulseLayer : public AudioLayer
// Assignment Operator
PulseLayer& operator= (const PulseLayer& rh);
/**
* Write data from the ring buffer to the harware and read data from the hardware
*/
void readFromMic (void);
void writeToSpeaker (void);
void ringtoneToSpeaker (void);
/**
* Create the audio streams into the given context
* @param c The pulseaudio context
......@@ -184,9 +124,6 @@ class PulseLayer : public AudioLayer
*/
AudioStream* ringtone_;
int spkrVolume_;
int micVolume_;
DeviceList sinkList_;
DeviceList sourceList_;
......
......@@ -34,7 +34,6 @@
SamplerateConverter::SamplerateConverter (int freq) : _maxFreq(freq)
{
printf("FREQ %d\n", freq);
int err;
_src_state = src_new (SRC_LINEAR, 1, &err);
......
......@@ -100,22 +100,4 @@ void AudioLayerTest::testPulseConnect()
_pulselayer = (PulseLayer*) Manager::instance().getAudioDriver();
CPPUNIT_ASSERT (_pulselayer->getLayerType() == PULSEAUDIO);
int sampleRate;
sampleRate = manager->audioPreference.getSmplrate();
CPPUNIT_ASSERT (_pulselayer->getPlaybackStream() == NULL);
CPPUNIT_ASSERT (_pulselayer->getRecordStream() == NULL);
sleep (1);
CPPUNIT_ASSERT (_pulselayer->getPlaybackStream() == NULL);
CPPUNIT_ASSERT (_pulselayer->getRecordStream() == NULL);
_pulselayer->startStream();
CPPUNIT_ASSERT (_pulselayer->getPlaybackStream()->pulseStream() != NULL);
CPPUNIT_ASSERT (_pulselayer->getRecordStream()->pulseStream() != NULL);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment