From 6094c0d6f5e6d04b9b5d5cd9f97b8503d7e1c000 Mon Sep 17 00:00:00 2001
From: llea <llea>
Date: Tue, 5 Apr 2005 19:43:39 +0000
Subject: [PATCH] Fix ALSA

---
 CHANGES                      |   4 +-
 src/Makefile                 |   4 +-
 src/audiobuffer.cpp          |   1 +
 src/audiocodec.cpp           |  63 --------------------
 src/audiocodec.h             |   1 -
 src/audiodriversalsa.cpp     | 109 +++++++++++------------------------
 src/audiodriversalsa.h       |   6 +-
 src/audiortp.cpp             |   8 +--
 src/configurationpanelui.cpp |   2 +-
 src/configurationpanelui.h   |   2 +-
 src/manager.cpp              |   1 +
 src/phonebookui.cpp          |   2 +-
 src/phonebookui.h            |   2 +-
 src/qtGUImainwindow.cpp      |   2 +
 src/sip.cpp                  |   3 +-
 src/tonegenerator.cpp        |   2 +
 src/url_inputui.cpp          |   2 +-
 src/url_inputui.h            |   2 +-
 18 files changed, 55 insertions(+), 161 deletions(-)

diff --git a/CHANGES b/CHANGES
index ca20a29e07..ef612b9d0e 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,5 +1,5 @@
-SFLphone (0.3) / 2005-02-10
-	* ALSA driver support added (not yet operationnal)
+SFLphone (0.3) / 2005-04-05
+	* ALSA driver support added 
 	* GSM audio codec support added
 	* Rings support (just ulaw format) added
 	* Notification incoming call added
diff --git a/src/Makefile b/src/Makefile
index f503ba35fa..05ab754cf0 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -66,8 +66,8 @@ OBJS = \
 	volumecontrol.o volumecontrol.moc.o \
 	stun.o udp.o  
 	
-start:	check prereq all
-#start:	check  all
+#start:	check prereq all
+start:	check  all
 
 check:
 ifeq ($(CONFIGURE_CONF),../configure.conf)
diff --git a/src/audiobuffer.cpp b/src/audiobuffer.cpp
index 821cbb0558..c85a0d59da 100644
--- a/src/audiobuffer.cpp
+++ b/src/audiobuffer.cpp
@@ -52,6 +52,7 @@ void AudioBuffer::resize (size_t newsize)
 void
 AudioBuffer::setData (short *buf, int vol) {
 	short *databuf = (short*)data;
+
 	for (int i = 0; i < (int)size; i++) {
 		databuf[i] = buf[i]*vol/100;
 	}
diff --git a/src/audiocodec.cpp b/src/audiocodec.cpp
index c7bcdcb09b..ce8682c952 100644
--- a/src/audiocodec.cpp
+++ b/src/audiocodec.cpp
@@ -26,12 +26,6 @@
 
 #include <string>
 
-#define swab16(x) \
-	((unsigned short)( \
-	(((unsigned short)(x) & (unsigned short)0x00ffU) << 8) | \
-	(((unsigned short)(x) & (unsigned short)0xff00U) >> 8) ))
-
-
 
 using namespace std;
 
@@ -138,40 +132,7 @@ AudioCodec::codecEncode (int pt, unsigned char *dst, short *src, unsigned int si
 		break;
 
 	case PAYLOAD_CODEC_GSM:
-#if 0		
-	{
-		gsm_frame gsmdata; // dst
-		int iii;
-		
-		bzero (gsmdata, sizeof(gsm_frame));
-
-		printf ("Before gsm_encode: ");
-		for (iii = 0; iii < 33; iii++) {
-			unsigned char *ptr = gsmdata;
-			printf ("%02X ", ptr[iii]);
-		}
-		gsm_signal sample[160];
-		for (iii = 0; iii < 160; iii++) {
-			unsigned short dat;
-			dat = (unsigned short) src[iii];
-			//dat = (unsigned short) sample[iii];
-			//sample[iii] = (short) swab16(dat);
-			sample[iii] = src[iii];
-		}
-		gsm_encode(encode_gsmhandle, sample, gsmdata);
-		printf ("\nAfter gsm_encode: ");
-		for (iii = 0; iii < 33; iii++) {
-			unsigned char *ptr = gsmdata;
-			printf ("%02X ", ptr[iii]);
-			dst[iii] = ptr[iii];
-		}
-		printf ("\n------\n");
-	}
-#endif
-#if 1
 		gsm_encode(encode_gsmhandle, (gsm_signal*)src, (gsm_byte*)dst);
-	
-#endif
 		return 33;
 		break;
 
@@ -208,28 +169,4 @@ AudioCodec::gsmDestroy (void) {
 	gsm_destroy(encode_gsmhandle);
 }
 
-int
-AudioCodec::getSizeByPayload (int pt){
-	switch (pt) {
-	case PAYLOAD_CODEC_ULAW:
-	case PAYLOAD_CODEC_ALAW:
-		return 320;
-		break;
-
-	case PAYLOAD_CODEC_GSM:
-		return 320;
-		break;
-
-	case PAYLOAD_CODEC_ILBC:
-		// TODO
-		break;
 
-	case PAYLOAD_CODEC_SPEEX:
-		// TODO
-		break;
-
-	default:
-		break;
-	}
-	return 0;
-}
diff --git a/src/audiocodec.h b/src/audiocodec.h
index 8630be6062..9d927752d1 100644
--- a/src/audiocodec.h
+++ b/src/audiocodec.h
@@ -56,7 +56,6 @@ public:
 	static int	codecEncode 	(int, unsigned char *, short *, unsigned int);
 	static void	gsmCreate		(void);
 	static void	gsmDestroy		(void);
-	static int  getSizeByPayload(int);
 };
 
 #endif // __CODEC_AUDIO_H__
diff --git a/src/audiodriversalsa.cpp b/src/audiodriversalsa.cpp
index b3d20ffb0b..799641a8f4 100644
--- a/src/audiodriversalsa.cpp
+++ b/src/audiodriversalsa.cpp
@@ -43,9 +43,16 @@ AudioDriversALSA::AudioDriversALSA(DeviceMode mode) : AudioDrivers () {
 
 AudioDriversALSA::~AudioDriversALSA (void) {
 	/* Close the audio handle */
-	if (audio_hdl != NULL) snd_pcm_close (audio_hdl);
+	this->closeDevice();
 }
 
+void
+AudioDriversALSA::closeDevice (void) {
+	if (audio_hdl != NULL) {
+		snd_pcm_close (audio_hdl);
+		audio_hdl = (snd_pcm_t *) NULL;
+	}
+}
 
 int
 AudioDriversALSA::initDevice (DeviceMode mode) {
@@ -57,16 +64,17 @@ AudioDriversALSA::initDevice (DeviceMode mode) {
 	}
 	
 	// Open the audio device
-	// Flags : blocking (else have to OR omode with SND_PCM_NONBLOCK).
 	switch (mode) {
 	case ReadOnly:
 		/* Only read sound from the device */
-		err = snd_pcm_open (&audio_hdl, ALSA_DEVICE,SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
+		err = snd_pcm_open (&audio_hdl, ALSA_DEVICE,SND_PCM_STREAM_CAPTURE, 
+				SND_PCM_NONBLOCK);
 		break;
 
 	case WriteOnly:
 		/* Only write sound to the device */
-		err = snd_pcm_open (&audio_hdl, ALSA_DEVICE,SND_PCM_STREAM_PLAYBACK,SND_PCM_NONBLOCK);	
+		err = snd_pcm_open (&audio_hdl, ALSA_DEVICE,SND_PCM_STREAM_PLAYBACK,
+				SND_PCM_NONBLOCK);	
 		break;
 	default:
 		break;
@@ -132,7 +140,7 @@ AudioDriversALSA::initDevice (DeviceMode mode) {
 		printf ("Cannot set channel count (%s)\n", snd_strerror (err));
 		return -1;
 	}
-	
+
 	// Apply previously setup parameters
 	err = snd_pcm_hw_params (audio_hdl, hw_params);
 	if (err < 0) {
@@ -142,6 +150,7 @@ AudioDriversALSA::initDevice (DeviceMode mode) {
 		
 	// Free temp variable used for configuration.
 	snd_pcm_hw_params_free (hw_params);
+
 	////////////////////////////////////////////////////////////////////////////
 	// END DEVICE SETUP
 	////////////////////////////////////////////////////////////////////////////
@@ -157,75 +166,24 @@ AudioDriversALSA::writeBuffer (void) {
 		printf ("ALSA: writeBuffer(): Device Not Open\n");
 		return -1;
 	}
-
-#if 1
+	
 	int rc;
 	size_t count = audio_buf.getSize()/2;
 	short* buf = (short *)audio_buf.getData();
 	while (count > 0) {
 		rc = snd_pcm_writei(audio_hdl, buf, count);
+		snd_pcm_wait(audio_hdl, 1);
 		if (rc == -EPIPE) {
 			snd_pcm_prepare(audio_hdl);
 		} else if (rc == -EAGAIN) {
 			continue;
 		} else if (rc < 0) {
-			printf ("ALSA: write(): %s\n", strerror(errno));
 			break; 
 		}
-		printf("rc = %d\n",rc);
 		buf += rc;
 		count -= rc;
 	}	
 	return rc;
-#endif
-}
-
-unsigned int
-AudioDriversALSA::readableBytes (void) {
-	audio_buf_info info;
-#if 0
-	struct timeval timeout;
-	fd_set read_fds;
-
-	if (devstate != DeviceOpened) {
-		return 0;
-	}
-	timeout.tv_sec = 0;
-	timeout.tv_usec = 0;
-	FD_ZERO (&read_fds);
-	FD_SET (audio_fd, &read_fds);
-	if (select (audio_fd + 1, &read_fds, NULL, NULL, &timeout) == -1) {
-		return 0;
-	}
-	if (!FD_ISSET ( audio_fd, &read_fds)) {
-		return 0;
-	}
-	if (ioctl (audio_fd, SNDCTL_DSP_GETISPACE, &info) == -1) {
-		printf ("ERROR: readableBytes %s\n", strerror(errno));		
-		return 0;
-	}
-#endif
-	return info.bytes;
-}
-
-
-int
-AudioDriversALSA::readBuffer (int bytes) {
-/*	if (devstate != DeviceOpened) {
-		printf ("Device Not Open\n");
-		return false;
-	}
-
-	audio_buf.resize (bytes);
-	size_t count = bytes;
-
-	void *buf;
-	buf = audio_buf.getData ();
-	size_t rc = read (audio_fd, buf, count);
-	if (rc != count) {
-		printf ("warning: asked microphone for %d got %d\n", count, rc);
-	}*/
-	return true;
 }
 
 int
@@ -234,34 +192,37 @@ AudioDriversALSA::readBuffer (void *ptr, int bytes) {
 		printf ("ALSA: readBuffer(): Device Not Open\n");
 		return -1;
 	}
-
-#if 1	
-	ssize_t count = bytes/2;
+		 
+	ssize_t count = bytes;
 	ssize_t rc;
-	
 	do {
 		rc = snd_pcm_readi(audio_hdl, (short*)ptr, count);
 	} while (rc == -EAGAIN);
-	if (rc == -EBADFD) printf ("Read: PCM is not in the right state\n");
-	if (rc == -ESTRPIPE) printf ("Read: a suspend event occurred\n");
 	if (rc == -EPIPE) {
-			printf ("Read: -EPIPE %d\n", rc);
 			snd_pcm_prepare(audio_hdl);
+			bzero(ptr, bytes);
+			rc = 320;
 	}
-	if (rc > 0 && rc != count) {
-		printf("Read: warning: asked microphone for %d frames but got %d\n",
-			count, rc);
-	}
-	
+	if (rc != 320)
+		rc = rc * 2;
 	return rc;
-#endif
 }
 
 int
 AudioDriversALSA::resetDevice (void) {
-/*	printf ("ALSA: Resetting device.\n");
-	snd_pcm_drop(audio_hdl);
-	snd_pcm_drain(audio_hdl);*/
+	int err;
+
+	printf("Resetting...\n");
+	if ((err = snd_pcm_drop(audio_hdl)) < 0) {
+		printf ("ALSA: drop() error: %s\n", snd_strerror (err));
+		return -1;
+	}
+
+	if ((err = snd_pcm_prepare(audio_hdl)) < 0) {
+		printf ("ALSA: prepare() error: %s\n", snd_strerror (err));
+		return -1;
+	}
+
 	return 0;
 }
 
diff --git a/src/audiodriversalsa.h b/src/audiodriversalsa.h
index fe515a4501..8e31369a8e 100644
--- a/src/audiodriversalsa.h
+++ b/src/audiodriversalsa.h
@@ -45,15 +45,11 @@ public:
 	int 	initDevice	(DeviceMode);
 	int		resetDevice	(void);	
 	int 	writeBuffer	(void);
-	int 	readBuffer	(int);
 	int		readBuffer 	(void *, int);
-	unsigned int readableBytes(void);
 	
-	int 	audio_fd;
-	
-
 private:
 	snd_pcm_t *audio_hdl;
+	void closeDevice (void);
 };
 
 #endif  // _AUDIO_DRIVERS_ALSA_H_
diff --git a/src/audiortp.cpp b/src/audiortp.cpp
index 78843cbc4b..c5742779a6 100644
--- a/src/audiortp.cpp
+++ b/src/audiortp.cpp
@@ -93,9 +93,7 @@ AudioRtp::closeRtpSession (SipCall *ca) {
 	ca->enable_audio = -1;
 
 	if (RTXThread != NULL) {
-		// Wait for them...and delete.
-		//RTXThread->join();
-		qDebug("DELETED");
+		qDebug("RTXThread DELETED");
 		delete RTXThread;
 		RTXThread = NULL;
 	}
@@ -262,7 +260,6 @@ AudioRtpRTX::run (void) {
 		////////////////////////////
 		// Send session
 		////////////////////////////
-		//int size = AudioCodec::getSizeByPayload(ca->payload);
 		int size = 320;
 		if (!manager->mute) {
 #ifdef ALSA
@@ -283,9 +280,9 @@ AudioRtpRTX::run (void) {
 			if (!manager->useAlsa)
 				i = audioDevice->readBuffer (data_mute, size);
 		}
-	//qDebug("read i = %d", i); 
 		// TODO : return an error because no sound
 		if (i < 0) {
+			qDebug("audiortp.cpp: No sound");
 			break;
 		}
 		for (int j = 0; j < i; j++)
@@ -303,7 +300,6 @@ AudioRtpRTX::run (void) {
 		} else {
 			session->putData(timestamp, data_to_send, compSize);
 		}
-		//timestamp += compSize;
 		timestamp += 160;
 
 		////////////////////////////
diff --git a/src/configurationpanelui.cpp b/src/configurationpanelui.cpp
index 36b2275e28..d2a26a5e00 100644
--- a/src/configurationpanelui.cpp
+++ b/src/configurationpanelui.cpp
@@ -1,7 +1,7 @@
 /****************************************************************************
 ** Form implementation generated from reading ui file 'configurationpanel.ui'
 **
-** Created: Thu Mar 31 11:43:54 2005
+** Created: Thu Mar 31 12:04:22 2005
 **      by: The User Interface Compiler ($Id$)
 **
 ** WARNING! All changes made in this file will be lost!
diff --git a/src/configurationpanelui.h b/src/configurationpanelui.h
index ac84e4e6e4..62b1fdd94f 100644
--- a/src/configurationpanelui.h
+++ b/src/configurationpanelui.h
@@ -1,7 +1,7 @@
 /****************************************************************************
 ** Form interface generated from reading ui file 'configurationpanel.ui'
 **
-** Created: Thu Mar 31 11:43:54 2005
+** Created: Thu Mar 31 12:04:22 2005
 **      by: The User Interface Compiler ($Id$)
 **
 ** WARNING! All changes made in this file will be lost!
diff --git a/src/manager.cpp b/src/manager.cpp
index 5bf59e7002..4dd80c15c4 100644
--- a/src/manager.cpp
+++ b/src/manager.cpp
@@ -199,6 +199,7 @@ Manager::ring (bool var) {
 	if (this->b_ringing != var) {
 		this->b_ringing = var;
 	}
+
 	tonezone = ringing();
 
 	if (sip->getNumberPendingCalls() == 1) 
diff --git a/src/phonebookui.cpp b/src/phonebookui.cpp
index d422f74191..db2ed159d2 100644
--- a/src/phonebookui.cpp
+++ b/src/phonebookui.cpp
@@ -1,7 +1,7 @@
 /****************************************************************************
 ** Form implementation generated from reading ui file 'phonebook.ui'
 **
-** Created: Thu Mar 31 11:43:53 2005
+** Created: Thu Mar 31 12:04:22 2005
 **      by: The User Interface Compiler ($Id$)
 **
 ** WARNING! All changes made in this file will be lost!
diff --git a/src/phonebookui.h b/src/phonebookui.h
index fb46428726..dd24d47aa9 100644
--- a/src/phonebookui.h
+++ b/src/phonebookui.h
@@ -1,7 +1,7 @@
 /****************************************************************************
 ** Form interface generated from reading ui file 'phonebook.ui'
 **
-** Created: Thu Mar 31 11:43:53 2005
+** Created: Thu Mar 31 12:04:22 2005
 **      by: The User Interface Compiler ($Id$)
 **
 ** WARNING! All changes made in this file will be lost!
diff --git a/src/qtGUImainwindow.cpp b/src/qtGUImainwindow.cpp
index 05e0e6d1f4..8fdbf856ad 100644
--- a/src/qtGUImainwindow.cpp
+++ b/src/qtGUImainwindow.cpp
@@ -1132,8 +1132,10 @@ QtGUIMainWindow::pressedKeySlot (int id) {
 	callmanager->audiodriver->audio_buf.resize(SAMPLING_RATE);
 	callmanager->audiodriver->audio_buf.setData(
 										buf, callmanager->getSpkrVolume());
+
 	pulselen = Config::get("Signalisations", "DTMF.pulseLength", 250);
 	callmanager->audiodriver->audio_buf.resize(pulselen * (OCTETS/1000));
+//	callmanager->audiodriver->resetDevice();
 	a = callmanager->audiodriver->writeBuffer();
 	if (a == 1) {
 		pressedKeySlot(id);
diff --git a/src/sip.cpp b/src/sip.cpp
index f889e0894b..8be9fe0ebe 100644
--- a/src/sip.cpp
+++ b/src/sip.cpp
@@ -908,8 +908,7 @@ SIP::getEvent (void) {
 
 		// The remote peer closed the phone call(we received BYE).
 		case EXOSIP_CALL_CLOSED:
-			qDebug("<- (%i %i) BYE from: %s", event->cid, event->did, 
-				event->remote_uri);
+			qDebug("<- (%i %i) BYE", event->cid, event->did);
 
 			call[theline]->usehold = false;
 			theline = findLineNumber(event);
diff --git a/src/tonegenerator.cpp b/src/tonegenerator.cpp
index 7940d6989e..694a02ec65 100644
--- a/src/tonegenerator.cpp
+++ b/src/tonegenerator.cpp
@@ -241,6 +241,7 @@ ToneGenerator::toneHandle (int idr) {
 		}
 
 		if (!manager->tonezone) {
+			manager->audiodriver->resetDevice();
 			if (tonethread != NULL) {	
 				delete tonethread;
 				tonethread = NULL;
@@ -292,6 +293,7 @@ ToneGenerator::playRing (const char *fileName) {
 		tonethread->start();
 	}
 	if (!manager->tonezone) {
+		manager->audiodriver->resetDevice();
 		if (tonethread != NULL) {	
 			delete tonethread;
 			tonethread = NULL;
diff --git a/src/url_inputui.cpp b/src/url_inputui.cpp
index 3429fdc619..3209c47159 100644
--- a/src/url_inputui.cpp
+++ b/src/url_inputui.cpp
@@ -1,7 +1,7 @@
 /****************************************************************************
 ** Form implementation generated from reading ui file 'url_input.ui'
 **
-** Created: Thu Mar 31 11:43:53 2005
+** Created: Thu Mar 31 12:04:22 2005
 **      by: The User Interface Compiler ($Id$)
 **
 ** WARNING! All changes made in this file will be lost!
diff --git a/src/url_inputui.h b/src/url_inputui.h
index 0edaff1214..ec7a3698f6 100644
--- a/src/url_inputui.h
+++ b/src/url_inputui.h
@@ -1,7 +1,7 @@
 /****************************************************************************
 ** Form interface generated from reading ui file 'url_input.ui'
 **
-** Created: Thu Mar 31 11:43:53 2005
+** Created: Thu Mar 31 12:04:22 2005
 **      by: The User Interface Compiler ($Id$)
 **
 ** WARNING! All changes made in this file will be lost!
-- 
GitLab