From a8b867f1665983f77bb76d470a4d5cffed47601e Mon Sep 17 00:00:00 2001
From: Alexandre Savard <alexandre.savard@savoirfairelinux.net>
Date: Tue, 27 Oct 2009 18:17:16 -0400
Subject: [PATCH] [#2352] Makes getMainBuffer() everywhere

---
 sflphone-common/src/audio/alsa/alsalayer.cpp     | 16 ++++++++--------
 sflphone-common/src/audio/audiolayer.h           |  5 +++++
 .../src/audio/pulseaudio/pulselayer.cpp          | 15 ++++++---------
 sflphone-common/src/managerimpl.cpp              | 10 +++++++---
 4 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/sflphone-common/src/audio/alsa/alsalayer.cpp b/sflphone-common/src/audio/alsa/alsalayer.cpp
index a08d5da9f8..2938219c8a 100644
--- a/sflphone-common/src/audio/alsa/alsalayer.cpp
+++ b/sflphone-common/src/audio/alsa/alsalayer.cpp
@@ -161,8 +161,8 @@ AlsaLayer::startStream (void)
     startPlaybackStream ();
 
     _urgentRingBuffer.flush();
-    _mainBuffer.flushAllBuffers();
-    _mainBuffer.flushDefault();
+    getMainBuffer()->flushAllBuffers();
+    getMainBuffer()->flushDefault();
 
     if(_audioThread == NULL) {
 	try {
@@ -875,7 +875,7 @@ void AlsaLayer::audioCallback (void)
         out=0;
 
         // Consume the regular one as well (same amount of bytes)
-        _mainBuffer.discard (toGet);
+        getMainBuffer()->discard (toGet);
 
     } else {
 
@@ -928,14 +928,14 @@ void AlsaLayer::audioCallback (void)
 
 	    maxNbBytesToGet = maxNbSamplesToGet * sizeof(SFLDataFormat);
             
-            normalAvailBytes = _mainBuffer.availForGet();
+            normalAvailBytes = getMainBuffer()->availForGet();
             toGet = (normalAvailBytes < (int)maxNbBytesToGet) ? normalAvailBytes : maxNbBytesToGet;
 
             out = (SFLDataFormat*) malloc (maxNbBytesToGet);
 
             if (normalAvailBytes) {
 
-                _mainBuffer.getData(out, toGet, spkrVolume);
+                getMainBuffer()->getData(out, toGet, spkrVolume);
 
 		if (_mainBufferSampleRate && ((int)_audioSampleRate != _mainBufferSampleRate)) {
 
@@ -998,7 +998,7 @@ void AlsaLayer::audioCallback (void)
 	// _debug("micAvailBytes %i\n", micAvailBytes);
 	if(micAvailBytes > 0) 
 	{
-            micAvailPut = _mainBuffer.availForPut();
+            micAvailPut = getMainBuffer()->availForPut();
             toPut = (micAvailBytes <= framesPerBufferAlsa) ? micAvailBytes : framesPerBufferAlsa;
             in = (SFLDataFormat*)malloc(toPut * sizeof(SFLDataFormat));
             toPut = read (in, toPut* sizeof(SFLDataFormat));
@@ -1021,14 +1021,14 @@ void AlsaLayer::audioCallback (void)
 
 		    dcblocker->filter_signal(rsmpl_out, nbSample);
 
-		    _mainBuffer.putData(rsmpl_out, nbSample * sizeof (SFLDataFormat), 100);
+		    getMainBuffer()->putData(rsmpl_out, nbSample * sizeof (SFLDataFormat), 100);
 
 		    free(rsmpl_out);
 		    rsmpl_out = 0;
 		
 		} else {
 
-		    _mainBuffer.putData(in, toPut, 100);
+		    getMainBuffer()->putData(in, toPut, 100);
 		}
 	    }
             free(in); in=0;
diff --git a/sflphone-common/src/audio/audiolayer.h b/sflphone-common/src/audio/audiolayer.h
index 75921da755..b995a87af9 100644
--- a/sflphone-common/src/audio/audiolayer.h
+++ b/sflphone-common/src/audio/audiolayer.h
@@ -210,6 +210,11 @@ class AudioLayer {
          */
 	MainBuffer* getMainBuffer( void ) { return &_mainBuffer; }
 
+	/**
+	 * Set the mainbuffer once the audiolayer is created
+	 */
+	// void setMainBuffer( MainBuffer* mainbuffer ) { _mainBuffer = mainbuffer; }
+
         /**
          * Default volume for incoming RTP and Urgent sounds.
          */
diff --git a/sflphone-common/src/audio/pulseaudio/pulselayer.cpp b/sflphone-common/src/audio/pulseaudio/pulselayer.cpp
index cd89dc9fa7..47eebfe2d9 100644
--- a/sflphone-common/src/audio/pulseaudio/pulselayer.cpp
+++ b/sflphone-common/src/audio/pulseaudio/pulselayer.cpp
@@ -358,14 +358,11 @@ void PulseLayer::startStream (void)
 	// Create Streams
 	connectPulseAudioServer();
 
-	// _urgentRingBuffer.flushAll();
-	// _mainBuffer.flushAllBuffers();
-
 	is_started = true;
     }
 
     _urgentRingBuffer.flushAll();
-    _mainBuffer.flushAllBuffers();
+    getMainBuffer()->flushAllBuffers();
 
 }
 
@@ -507,7 +504,7 @@ void PulseLayer::writeToSpeaker (void)
         pa_stream_write (playback->pulseStream(), out, writeableSize, pa_xfree, 0, PA_SEEK_RELATIVE);
 
         // Consume the regular one as well (same amount of bytes)
-        _mainBuffer.discard (writeableSize);
+        getMainBuffer()->discard (writeableSize);
 
 
     } else {
@@ -566,7 +563,7 @@ void PulseLayer::writeToSpeaker (void)
 	    }
 
             out = (SFLDataFormat*) pa_xmalloc (maxNbBytesToGet);
-            normalAvailBytes = _mainBuffer.availForGet();
+            normalAvailBytes = getMainBuffer()->availForGet();
 	    
             byteToGet = (normalAvailBytes < (int)(maxNbBytesToGet)) ? normalAvailBytes : maxNbBytesToGet;
 
@@ -577,7 +574,7 @@ void PulseLayer::writeToSpeaker (void)
 		if( (byteToGet%2) != 0 )
 		    byteToGet = byteToGet-1;
 
-                _mainBuffer.getData (out, byteToGet, 100);
+                getMainBuffer()->getData (out, byteToGet, 100);
 
 		// test if resampling is required
 		if (_mainBufferSampleRate && ((int)_audioSampleRate != _mainBufferSampleRate)) {
@@ -649,14 +646,14 @@ void PulseLayer::readFromMic (void)
 	    // remove dc offset
 	    dcblocker->filter_signal( rsmpl_out, nbSample );
 
-	    _mainBuffer.putData ( (void*) rsmpl_out, nbSample*sizeof(SFLDataFormat), 100);
+	    getMainBuffer()->putData ( (void*) rsmpl_out, nbSample*sizeof(SFLDataFormat), 100);
 
 	    pa_xfree (rsmpl_out);
 
         } else {
 
             // no resampling required
-            _mainBuffer.putData ( (void*) data, r, 100);
+            getMainBuffer()->putData ( (void*) data, r, 100);
         }
 
 
diff --git a/sflphone-common/src/managerimpl.cpp b/sflphone-common/src/managerimpl.cpp
index 18aca9f696..51599a04d9 100644
--- a/sflphone-common/src/managerimpl.cpp
+++ b/sflphone-common/src/managerimpl.cpp
@@ -3209,12 +3209,16 @@ void ManagerImpl::switchAudioManager (void)
 
     _debug ("has current call: %i \n", hasCurrentCall());
 
+    if(hasCurrentCall())
+	_audiodriver->startStream();
+	
+
     // need to stop audio streams if there is currently no call
-    if ( (type != PULSEAUDIO) && (!hasCurrentCall())) {
+    // if ( (type != PULSEAUDIO) && (!hasCurrentCall())) {
         // _debug("There is currently a call!!\n");
-        _audiodriver->stopStream();
+        // _audiodriver->stopStream();
 
-    }
+	// }
 }
 
 /**
-- 
GitLab