rpms/audio-entropyd/devel audio-entropyd-1.0.5-alsa.patch, NONE, 1.1 audio-entropyd.spec, 1.14, 1.15

Tom Callaway spot at fedoraproject.org
Mon Apr 13 21:44:54 UTC 2009


Author: spot

Update of /cvs/pkgs/rpms/audio-entropyd/devel
In directory cvs1.fedora.phx.redhat.com:/tmp/cvs-serv12910

Modified Files:
	audio-entropyd.spec 
Added Files:
	audio-entropyd-1.0.5-alsa.patch 
Log Message:
port from OSS to ALSA

audio-entropyd-1.0.5-alsa.patch:

--- NEW FILE audio-entropyd-1.0.5-alsa.patch ---
diff -up audio-entropyd-1.0.5/audio-entropyd.c.alsa audio-entropyd-1.0.5/audio-entropyd.c
--- audio-entropyd-1.0.5/audio-entropyd.c.alsa	2008-09-28 10:34:06.000000000 -0400
+++ audio-entropyd-1.0.5/audio-entropyd.c	2009-04-13 16:52:50.000000000 -0400
@@ -62,6 +62,10 @@
  **
  ** 02Apr2007  Folkert van Heusden <folkert at vanheusden.com>
  **            Added entropy-tester
+ **
+ ** 13Apr2009  Tom "spot" Callaway <tcallawa at redhat.com>
+ **            Migrated code to use ALSA (libasound)
+ **            Many thanks to Lennart Poettering
  *
  * $Log: audio-entropyd.c,v $
  * Revision 0.5  2003/01/27 17:39:31  folkert
@@ -86,6 +90,7 @@
 #include <sys/mman.h>
 #include <sys/time.h>
 
+#include <alsa/asoundlib.h>
 #include <linux/soundcard.h>
 #include <asm/types.h>
 #include <linux/random.h>
@@ -97,7 +102,6 @@
 #include "RNGTEST.h"
 #include "error.h"
 
-#define DEFAULT_DSP_DEV				"/dev/dsp"
 #define RANDOM_DEVICE				"/dev/random"
 #define URANDOM_DEVICE				"/dev/urandom"
 #define DEFAULT_INPUT_BUFFER_SIZE		16384
@@ -112,21 +116,25 @@ int treshold = 0;
 char skip_test = 0;
 int error_state = 0;
 
+static char *cdevice = "hw:0";				/* capture device */
+static snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;	/* sample format */
+static unsigned int channels = 2;			/* 2 channels = stereo */
+const char *id = "capture";
+int err;
+
 #define max(x, y)	((x)>(y)?(x):(y))
 
 /* Prototypes */
-void main_loop(const char *dsp_device, int read_size, int sample_rate);
-int setup_dsp_device(const char *dsp_device, int sample_rate);
+void main_loop(const char *cdevice, int read_size, int sample_rate);
+int setparams(snd_pcm_t *chandle, int sample_rate);
 void usage(void);
 void credit_krng(int random_fd, struct rand_pool_info *entropy);
 void daemonise(void);
 void gracefully_exit(int signum);
 void logging_handler(int signum);
-void get_random_data(char *input_buffer, int read_size, int sample_rate, int *n_output_bytes, char *output_buffer);
+void get_random_data(snd_pcm_t *chandle, char *input_buffer, int read_size, int sample_rate, int *n_output_bytes, char *output_buffer);
 int add_to_kernel_entropyspool(int handle, char *buffer, int nbytes);
 
-char *dsp_device = DEFAULT_DSP_DEV;
-
 /* Functions */
 
 int main(int argc, char **argv)
@@ -137,7 +145,7 @@ int main(int argc, char **argv)
 	static struct option long_options[] =
 	{
 		{"read-size",	1, NULL, 'r' },
-		{"dsp-device",	1, NULL, 'd' },
+		{"device",	1, NULL, 'd' },
 		{"sample-rate",	1, NULL, 'N' },
 		{"skip-test",	0, NULL, 's' },
 		{"verbose",	0, NULL, 'v' },
@@ -167,7 +175,7 @@ int main(int argc, char **argv)
 				break;
 
 			case 'd':
-				dsp_device = strdup(optarg);
+				cdevice = strdup(optarg);
 				break;
 
 			case 'N':
@@ -211,37 +219,52 @@ int main(int argc, char **argv)
 
 	daemonise();
 
-	main_loop(dsp_device, read_size, sample_rate);
+	main_loop(cdevice, read_size, sample_rate);
 
 	exit(0);
 }
 
-int setup_dsp_device(const char *dsp_device, int sample_rate)
+int setparams(snd_pcm_t *chandle, int sample_rate)
 {
-	int dsp_fd, setting;
+	snd_pcm_hw_params_t *ct_params;		/* templates with rate, format and channels */
+	snd_pcm_hw_params_alloca(&ct_params);
 
-	/* Open sound device */
-	dsp_fd = open(dsp_device, O_RDONLY);
-	if (dsp_fd == -1)
-		error_exit("Could not open %s for reading: %m", dsp_device);
-
-	/* Set sample rate */
-	if (set_soundcard_sample_rate(dsp_fd, sample_rate) == -1)
-		error_exit("Could not set %i sample rate", sample_rate);
+	err = snd_pcm_hw_params_any(chandle, ct_params);
+	if (err < 0)
+		error_exit("Broken configuration for %s PCM: no configurations available: %s", id, snd_strerror(err));
+
+	/* Disable rate resampling */
+	err = snd_pcm_hw_params_set_rate_resample(chandle, ct_params, 0);
+	if (err < 0)
+		error_exit("Could not disable rate resampling: %s", snd_strerror(err));
+
+	/* Set access to SND_PCM_ACCESS_RW_INTERLEAVED */
+	err = snd_pcm_hw_params_set_access(chandle, ct_params, SND_PCM_ACCESS_RW_INTERLEAVED);
+	if (err < 0)
+		error_exit("Could not set access to SND_PCM_ACCESS_RW_INTERLEAVED: %s", snd_strerror(err));
+
+	/* Restrict a configuration space to have rate nearest to our target rate */
+	err = snd_pcm_hw_params_set_rate_near(chandle, ct_params, &sample_rate, 0);
+	if (err < 0)
+		error_exit("Rate %iHz not available for %s: %s", sample_rate, id, snd_strerror(err));
 
 	/* Set sample format */
-	setting = AFMT_S16_LE; /* 16 bit little endian */
-	if (ioctl(dsp_fd, SNDCTL_DSP_SETFMT, &setting) == -1)
-		error_exit("Could not set 16 bit sample mode: %m");
+	err = snd_pcm_hw_params_set_format(chandle, ct_params, format);
+	if (err < 0)
+		error_exit("Sample format not available for %s: %s", id, snd_strerror(err));
 
 	/* Set stereo */
-	if (set_soundcard_number_of_channels(dsp_fd, 2) == -1)
-		error_exit("Could not set stereo sample mode: %m");
-
-	return(dsp_fd);
+	err = snd_pcm_hw_params_set_channels(chandle, ct_params, channels);
+	if (err < 0)
+		error_exit("Channels count (%i) not available for %s: %s", channels, id, snd_strerror(err));
+
+	/* Apply settings to sound device */
+	err = snd_pcm_hw_params(chandle, ct_params);
+	if (err < 0)
+		error_exit("Could not apply settings to sound device!");
 }
 
-void main_loop(const char *dsp_device, int read_size, int sample_rate)
+void main_loop(const char *cdevice, int read_size, int sample_rate)
 {
 	unsigned char output_buffer[read_size];
 	char input_buffer[max(read_size, DEFAULT_CLICK_READ)];
@@ -249,6 +272,11 @@ void main_loop(const char *dsp_device, i
 	int random_fd = -1, max_bits;
 	FILE *poolsize_fh;
 
+	snd_pcm_t *chandle;
+
+	if ((err = snd_pcm_open(&chandle, cdevice, SND_PCM_STREAM_CAPTURE, 0)) < 0)
+		error_exit("Record open error: %s", snd_strerror(err));
+
 	/* lock buffers in core */
 	if (mlock(output_buffer, sizeof(output_buffer)) == -1 || mlock(input_buffer, sizeof(input_buffer)) == -1)
 		error_exit("Could not lock buffers in core");
@@ -268,7 +296,7 @@ void main_loop(const char *dsp_device, i
 	/* first get some data so that we can immediately submit something when the
 	 * kernel entropy-buffer gets below some limit
 	 */
-	get_random_data(input_buffer, read_size, sample_rate, &n_output_bytes, output_buffer);
+	get_random_data(chandle, input_buffer, read_size, sample_rate, &n_output_bytes, output_buffer);
 
 	/* Main read loop */
 	for(;;)
@@ -308,7 +336,7 @@ void main_loop(const char *dsp_device, i
 			if (ioctl(random_fd, RNDGETENTCNT, &after) == -1)
 				error_exit("Coundn't query entropy-level from kernel: %m");
 
-			get_random_data(input_buffer, read_size, sample_rate, &n_output_bytes, output_buffer);
+			get_random_data(chandle, input_buffer, read_size, sample_rate, &n_output_bytes, output_buffer);
 		}
 
 		if (loggingstate == 1)
@@ -348,9 +376,8 @@ int add_to_kernel_entropyspool(int handl
 
 #define order(a, b)     (((a) == (b)) ? -1 : (((a) > (b)) ? 1 : 0))
 
-void get_random_data(char *input_buffer, int read_size, int sample_rate, int *n_output_bytes, char *output_buffer)
+void get_random_data(snd_pcm_t *chandle, char *input_buffer, int read_size, int sample_rate, int *n_output_bytes, char *output_buffer)
 {
-	int dsp_fd;
 	int n_to_do, bits_out=0, loop;
 	char *dummy;
 	static short psl=0, psr=0; /* previous samples */
@@ -359,29 +386,42 @@ void get_random_data(char *input_buffer,
 
 	*n_output_bytes=0;
 
-	/* Open and set up DSP for reading */
-	dsp_fd = setup_dsp_device(dsp_device, sample_rate);	
+	/* Open and set up ALSA device for reading */
+	setparams(chandle, sample_rate);
 
 	/* Discard the first data read */
 	/* it often contains weird looking data - probably a click from */
 	/* driver loading / card initialisation */
-	read(dsp_fd, input_buffer, DEFAULT_CLICK_READ);
+	int initial_len = snd_pcm_bytes_to_frames(chandle, DEFAULT_CLICK_READ);
+	snd_pcm_sframes_t garbage_frames_read = snd_pcm_readi(chandle, input_buffer, initial_len);
+	/* Make sure we aren't hitting a disconnect/suspend case */
+	if (garbage_frames_read < 0)
+		snd_pcm_recover(chandle, garbage_frames_read, 0);
+	/* Nope, something else is wrong. Bail. */
+	if (garbage_frames_read < 0)
+		error_exit("Read error: %m");
 
 	/* Read a buffer of audio */
-	n_to_do = read_size;
+	n_to_do = snd_pcm_bytes_to_frames(chandle, read_size);
 	dummy = input_buffer;
 	while (n_to_do)
 	{
-		int bytes_read = read(dsp_fd, dummy, n_to_do);
-		if (bytes_read == -1) 
+		snd_pcm_sframes_t frames_read = snd_pcm_readi(chandle, dummy, n_to_do);
+		/* Make	sure we	aren't hitting a disconnect/suspend case */
+		if (frames_read < 0)
+			frames_read = snd_pcm_recover(chandle, frames_read, 0);
+		/* Nope, something else is wrong. Bail.	*/
+		if (frames_read < 0)
+			error_exit("Read error: %m");
+		if (frames_read == -1) 
 		{
 			if (errno != EINTR)
 				error_exit("Read error: %m");
 		}
 		else
 		{
-			n_to_do -= bytes_read;
-			dummy += bytes_read;	
+			n_to_do -= frames_read;
+			dummy += frames_read;	
 		}
 	}
 
@@ -458,8 +498,6 @@ void get_random_data(char *input_buffer,
 			}
 		}
 	}
-
-	close(dsp_fd);
 }
 
 void usage(void)
@@ -469,7 +507,7 @@ void usage(void)
 	fprintf(stderr, "\n");
 	fprintf(stderr, "Options:\n");
 	fprintf(stderr, "--read-size,    -r []  Number of bytes to read at a time. (Default %i)\n", DEFAULT_INPUT_BUFFER_SIZE);
-	fprintf(stderr, "--dsp-device,   -d []  Specify sound device to use. (Default %s)\n", DEFAULT_DSP_DEV);
+	fprintf(stderr, "--device,       -d []  Specify sound device to use. (Default %s)\n", cdevice);
 	fprintf(stderr, "--sample-rate,  -N []  Audio sampling rate. (default %i)\n", DEFAULT_SAMPLE_RATE);
 	fprintf(stderr, "--skip-test,    -s     Do not check if data is random enough.\n");
 	fprintf(stderr, "--verbose,      -v     Be verbose (to syslog).\n");
diff -up audio-entropyd-1.0.5/Makefile.alsa audio-entropyd-1.0.5/Makefile
--- audio-entropyd-1.0.5/Makefile.alsa	2008-09-28 10:34:06.000000000 -0400
+++ audio-entropyd-1.0.5/Makefile	2009-04-13 17:26:10.000000000 -0400
@@ -5,13 +5,13 @@ DEBUGFLAGS= #-DDEBUG
 INCLUDES=
 DEFINES+=# -DDEBUG
 CFLAGS+= $(DEFINES) $(WARNFLAGS) $(DEBUGFLAGS) $(INCLUDES) $(OPT_FLAGS) -DVERSION=\"$(VERSION)\"
-LFLAGS=-lm
+LFLAGS=-lm -lasound
 
 TARGETS=audio-entropyd
 
 all: $(TARGETS) 
 
-audio-entropyd: snd_dev.o audio-entropyd.o error.o proc.o val.o RNGTEST.o error.o
+audio-entropyd: audio-entropyd.o error.o proc.o val.o RNGTEST.o error.o
 	$(CC) -o $@ $^ $(LFLAGS) 
 
 install: audio-entropyd
diff -up audio-entropyd-1.0.5/snd_dev.c.alsa audio-entropyd-1.0.5/snd_dev.c
--- audio-entropyd-1.0.5/snd_dev.c.alsa	2008-09-28 10:34:06.000000000 -0400
+++ audio-entropyd-1.0.5/snd_dev.c	2009-04-13 17:26:30.000000000 -0400
@@ -1,46 +0,0 @@
-#include <sys/ioctl.h>
-#include <linux/soundcard.h>
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include "error.h"
-
-int set_soundcard_sample_format(int fd)
-{
-	int format = AFMT_S16_NE;	/* machine-endian 16 bit signed */
-
-	if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) == -1)
-		error_exit("set_soundcard_sample_format::ioctl: failed\n");
-
-	if (format != AFMT_S16_NE)
-		error_exit("set_soundcard_sample_format: sampleformat unexpected: %d\n", format);
-
-	return 0;
-}
-
-int set_soundcard_number_of_channels(int fd, int n_channels)
-{
-	int dummy = n_channels;
-
-	if (ioctl(fd, SNDCTL_DSP_CHANNELS, &dummy) == -1)
-		error_exit("set_soundcard_number_of_channels::ioctl: failed");
-
-	if (dummy != n_channels)
-		error_exit("set_soundcard_number_of_channels::ioctl: number of channels unexpected: %d", dummy);
-
-	return 0;
-}
-
-int set_soundcard_sample_rate(int fd, int hz)
-{
-	int dummy = hz;
-
-	if (ioctl(fd, SNDCTL_DSP_SPEED, &dummy) == -1)
-		error_exit("set_soundcard_sample_rate::ioctl: failed: %d");
-
-	if (dummy != hz)
-		error_exit("set_soundcard_sample_rate::ioctl: samplerate unexpected: %d", dummy);
-
-	return 0;
-}
diff -up audio-entropyd-1.0.5/snd_dev.h.alsa audio-entropyd-1.0.5/snd_dev.h
--- audio-entropyd-1.0.5/snd_dev.h.alsa	2008-09-28 10:34:06.000000000 -0400
+++ audio-entropyd-1.0.5/snd_dev.h	2009-04-13 17:26:30.000000000 -0400
@@ -1,3 +0,0 @@
-int set_soundcard_sample_format(int fd);
-int set_soundcard_number_of_channels(int fd, int n_channels);
-int set_soundcard_sample_rate(int fd, int hz);


Index: audio-entropyd.spec
===================================================================
RCS file: /cvs/pkgs/rpms/audio-entropyd/devel/audio-entropyd.spec,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- audio-entropyd.spec	24 Feb 2009 03:09:30 -0000	1.14
+++ audio-entropyd.spec	13 Apr 2009 21:44:23 -0000	1.15
@@ -1,6 +1,6 @@
 Name:           audio-entropyd
 Version:        1.0.5
-Release:        3%{?dist}
+Release:        4%{?dist}
 License:        GPLv2
 Group:          System Environment/Daemons
 Summary:        Generate entropy from audio output
@@ -8,7 +8,9 @@
 Source0:        http://www.vanheusden.com/aed/audio-entropyd-%{version}.tgz
 Source1:        audio-entropyd
 Source2:	audio-entropyd.conf
+Patch0:		audio-entropyd-1.0.5-alsa.patch
 BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
+BuildRequires:	alsa-lib-devel
 Requires(post):   chkconfig
 Requires(preun):  chkconfig, initscripts
 Requires(postun): initscripts
@@ -18,6 +20,7 @@
 
 %prep
 %setup -q
+%patch0 -p1 -b .alsa
 
 %build
 make OPT_FLAGS="$RPM_OPT_FLAGS" %{?_smp_mflags}
@@ -56,6 +59,9 @@
 %config(noreplace) %{_sysconfdir}/sysconfig/audio-entropyd
 
 %changelog
+* Wed Mar 25 2009 Tom "spot" Callaway <tcallawa at redhat.com> - 1.0.5-4
+- port from OSS to ALSA
+
 * Mon Feb 23 2009 Fedora Release Engineering <rel-eng at lists.fedoraproject.org> - 1.0.5-3
 - Rebuilt for https://fedoraproject.org/wiki/Fedora_11_Mass_Rebuild
 




More information about the scm-commits mailing list