From d0cc8f27978ace4afe972f5a2e467afee1548ab8 Mon Sep 17 00:00:00 2001
From: Alexandre Savard <alexandre.savard@savoirfairelinux.net>
Date: Tue, 27 Oct 2009 16:52:49 -0400
Subject: [PATCH] [#2352] Use 50 sec latency on pulseaudio stream creation

This parameter may vary from 20 to 200 ms, the longer it is,
the more noticable the latency is.

Long latency however, reduce interups, which reduce CPU
---
 .../src/audio/pulseaudio/audiostream.cpp      | 19 ++++--------
 .../src/audio/pulseaudio/pulselayer.cpp       | 29 +------------------
 2 files changed, 7 insertions(+), 41 deletions(-)

diff --git a/sflphone-common/src/audio/pulseaudio/audiostream.cpp b/sflphone-common/src/audio/pulseaudio/audiostream.cpp
index 614d2e5c27..565ef8b783 100644
--- a/sflphone-common/src/audio/pulseaudio/audiostream.cpp
+++ b/sflphone-common/src/audio/pulseaudio/audiostream.cpp
@@ -192,30 +192,23 @@ AudioStream::createStream (pa_context* c)
     // parameters are defined as number of bytes
     // 2048 bytes (1024 int16) is 20 ms at 44100 Hz 
     if (_streamType == PLAYBACK_STREAM) {
-	
+
+	// 20 ms framesize TODO: take framesize value from config
         attributes->maxlength = (uint32_t) -1;
-        attributes->tlength = pa_usec_to_bytes(20 * PA_USEC_PER_MSEC, &sample_spec);
+        attributes->tlength = pa_usec_to_bytes(50 * PA_USEC_PER_MSEC, &sample_spec);
         attributes->prebuf = (uint32_t) -1;      
         attributes->minreq = (uint32_t) -1;
         attributes->fragsize = (uint32_t) -1;
-
-	_debug("tlength: %i\n", pa_usec_to_bytes(20 * PA_USEC_PER_MSEC, &sample_spec));
-	/*
-	attributes->maxlength = 88200;
-        attributes->tlength = 4096;
-        attributes->prebuf = 4096;      
-        attributes->minreq = 2048;
-        attributes->fragsize = 4096;
-	*/
 	
 	pa_stream_connect_playback( s , NULL , attributes, PA_STREAM_ADJUST_LATENCY, &_volume, NULL);
     } else if (_streamType == CAPTURE_STREAM) {
-	
+
+	// 20 ms framesize TODO: take framesize value from config
         attributes->maxlength = (uint32_t) -1;
         attributes->tlength = (uint32_t) -1;
         attributes->prebuf = (uint32_t) -1;
         attributes->minreq = (uint32_t) -1;
-        attributes->fragsize = 4096;
+        attributes->fragsize = pa_usec_to_bytes(50 * PA_USEC_PER_MSEC, &sample_spec);
 	
 	
 
diff --git a/sflphone-common/src/audio/pulseaudio/pulselayer.cpp b/sflphone-common/src/audio/pulseaudio/pulselayer.cpp
index df3823fe78..cd89dc9fa7 100644
--- a/sflphone-common/src/audio/pulseaudio/pulselayer.cpp
+++ b/sflphone-common/src/audio/pulseaudio/pulselayer.cpp
@@ -546,22 +546,16 @@ void PulseLayer::writeToSpeaker (void)
         } else {
 
 	    int _mainBufferSampleRate = getMainBuffer()->getInternalSamplingRate();
-	    // int maxNbSamplesToGet = 0;
+
 	    int maxNbBytesToGet = 0;
 
-	    _debug("--------------- Playback -----------\n");
-	    _debug("writeableSize: %i\n", writeableSize);
 
 	    // test if audio resampling is needed
 	    if (_mainBufferSampleRate && ((int)_audioSampleRate != _mainBufferSampleRate)) {
  
 		// upsamplefactor is used to compute the number of bytes to get in the ring buffer 
 		double upsampleFactor = (double) _mainBufferSampleRate / _audioSampleRate;
-		_debug("upsampleFactor: %f\n", upsampleFactor);
 
-		// maxNbSamplesToGet is the number of sample to get in the ring buffer which, 
-                // once resampled, will not be over the writeableSize
-		// maxNbSamplesToGet = (int) ((double) framesPerBuffer * upsampleFactor);
 		maxNbBytesToGet = ((double) writeableSize * upsampleFactor);
 
 	    } else {
@@ -571,15 +565,10 @@ void PulseLayer::writeToSpeaker (void)
 
 	    }
 
-	    //  maxNbBytesToGet = maxNbSamplesToGet * sizeof(SFLDataFormat);
-	    _debug("maxNbBytesToGet: %i\n", maxNbBytesToGet);
-
             out = (SFLDataFormat*) pa_xmalloc (maxNbBytesToGet);
             normalAvailBytes = _mainBuffer.availForGet();
-	    _debug("normalAvailBytes: %i\n", normalAvailBytes);
 	    
             byteToGet = (normalAvailBytes < (int)(maxNbBytesToGet)) ? normalAvailBytes : maxNbBytesToGet;
-	    _debug("byteToGet: %i\n", byteToGet);
 
             if (byteToGet) {
 
@@ -597,12 +586,8 @@ void PulseLayer::writeToSpeaker (void)
 		    
 		    // Do sample rate conversion
 		    int nb_sample_down = byteToGet / sizeof(SFLDataFormat);
-		    _debug("nbSampleDown: %i\n", nb_sample_down);
-
 
 		    int nbSample = _converter->upsampleData((SFLDataFormat*)out, rsmpl_out, _mainBufferSampleRate, _audioSampleRate, nb_sample_down);
-		    _debug("nbSample converted: %i\n", nbSample);
-		    _debug("bytes to be written: %i\n", nbSample*sizeof(SFLDataFormat));
 		    
 		    pa_stream_write (playback->pulseStream(), rsmpl_out, nbSample*sizeof(SFLDataFormat), NULL, 0, PA_SEEK_RELATIVE);
 
@@ -618,24 +603,18 @@ void PulseLayer::writeToSpeaker (void)
             } else {
 
 		if((tone == 0) && (file_tone == 0)) {
-
-		    // _debug("maxNbBytesToGet: %i\n", maxNbBytesToGet);
 		    
 		    SFLDataFormat* zeros = (SFLDataFormat*)pa_xmalloc (writeableSize);
   
 		    bzero (zeros, writeableSize);
 		    pa_stream_write(playback->pulseStream(), zeros, writeableSize, pa_xfree, 0, PA_SEEK_RELATIVE);
 
-		    // pa_xfree (zeros);
-		    
-
 		}
             }
 
 	    
 	    _urgentRingBuffer.Discard(byteToGet);
 
-            // pa_xfree (out);
         }
 
     }
@@ -647,8 +626,6 @@ void PulseLayer::readFromMic (void)
     const char* data = NULL;
     size_t r;
 
-    // _debug("--------------- Capture -----------\n");
-
     if (pa_stream_peek (record->pulseStream() , (const void**) &data , &r) < 0 || !data) {
         _debug("pa_stream_peek() failed: %s\n" , pa_strerror( pa_context_errno( context) ));
     }
@@ -666,16 +643,12 @@ void PulseLayer::readFromMic (void)
 	    int nbSample = r / sizeof(SFLDataFormat);
             int nb_sample_up = nbSample;
 
-	    // _debug("nbSample from mic: %i\n", nbSample);
-
             
             nbSample = _converter->downsampleData ((SFLDataFormat*)data, rsmpl_out, _mainBufferSampleRate, _audioSampleRate, nb_sample_up);
 
 	    // remove dc offset
 	    dcblocker->filter_signal( rsmpl_out, nbSample );
 
-	    // _debug("nbSample copied: %i\n", nbSample);
-
 	    _mainBuffer.putData ( (void*) rsmpl_out, nbSample*sizeof(SFLDataFormat), 100);
 
 	    pa_xfree (rsmpl_out);
-- 
GitLab