From 1ede42da96838b463ec6cb9588ef1b4a57585a3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Kwa=C5=9Bny?= Date: Fri, 15 Apr 2011 02:37:15 +0800 Subject: [PATCH 1/9] add "-d" option for daemon mode --- shairport.pl | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/shairport.pl b/shairport.pl index a797140c..92c9f291 100755 --- a/shairport.pl +++ b/shairport.pl @@ -47,11 +47,13 @@ my $apname = "ShairPort $$ on " . `hostname`; # password - required to connect # for no password, set: my $password = ''; +my $daemon; # suppose hairtunes is under same directory my $hairtunes_cli = $FindBin::Bin . '/hairtunes'; GetOptions("apname=s" => \$apname, - "password=s" => \$password); + "password=s" => \$password, + "d" => \$daemon); # use --apname MyName --password secret as arguments @@ -152,6 +154,24 @@ my $sel = new IO::Select($listen); print "listening...\n"; + +if ($daemon) { + use POSIX; + POSIX::setsid or die "setsid: $!"; + my $pid = fork(); + if ($pid < 0) { + die "fork: $!"; + } elsif ($pid) { + exit 0; + } + chdir "/"; + umask 0; + open (STDIN, "/dev/null"); + open (STDERR, ">&STDOUT"); +}; + + while (1) { my @waiting = $sel->can_read; foreach $fh (@waiting) { From 8d95eec3a92ebe56ec478dec081790734f056591 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Kwa=C5=9Bny?= Date: Thu, 14 Apr 2011 21:14:40 +0200 Subject: [PATCH 2/9] - add install target to Makefile - add sample init script (shairport.init.sample) --- Makefile | 6 ++++ shairport.init.sample | 67 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100755 shairport.init.sample diff --git a/Makefile b/Makefile index c1ae84a5..546bd900 100644 --- a/Makefile +++ b/Makefile @@ -13,5 +13,11 @@ clean: .PTHONY: all clean +prefix=/usr/local +install: hairtunes + install -m 0755 hairtunes $(prefix)/bin + install -m 0755 shairport.pl $(prefix)/bin +.PHONY: install + .SILENT: clean diff --git a/shairport.init.sample b/shairport.init.sample new file mode 100755 index 00000000..4cd73826 --- /dev/null +++ b/shairport.init.sample @@ -0,0 +1,67 @@ +#!/bin/bash +# +# This starts and stops shairport +# +### BEGIN INIT INFO +# Provides: shairport +# Required-Start: $network +# Required-Stop: +# Short-Description: shairport - Airtunes emulator! +# Description: Airtunes emulator! +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +### END INIT INFO + + +# Source function library. +. /lib/lsb/init-functions + +NAME=shairport +DAEMON="/usr/local/bin/shairport.pl" +DAEMON_ARGS="" +PIDFILE=/var/run/$NAME.pid + +[ -x $binary ] || exit 0 + +RETVAL=0 + +start() { + echo -n "Starting shairport: " + start-stop-daemon --start --quiet --pidfile "$PIDFILE" \ + --exec "$DAEMON" -b --oknodo -- $DAEMON_ARGS + log_end_msg $? +} + +stop() { + echo -n "Shutting down shairport: " + start-stop-daemon --stop --quiet --pidfile "$PIDFILE" \ + --retry 1 --oknodo + log_end_msg $? +} + +restart() { + stop + sleep 1 + start +} + +case "$1" in + start) + start + ;; + stop) + stop + ;; + status) + status shairport + ;; + restart) + restart + ;; + *) + echo "Usage: $0 {start|stop|status|restart}" + ;; +esac + +exit 0 + From 05fd47a65c83c583667dadd401630395a2af7644 Mon Sep 17 00:00:00 2001 From: Martin Spasov Date: Fri, 15 Apr 2011 12:42:17 +0300 Subject: [PATCH 3/9] Added LICENSE file. --- LICENSE | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..6bb1c607 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or +sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file From e5a1efe44060df46288a9e418750661235fd1fc9 Mon Sep 17 00:00:00 2001 From: okertanov Date: Fri, 15 Apr 2011 14:12:32 +0300 Subject: [PATCH 4/9] fix O2 optimization distorsions --- Makefile | 2 +- hairtunes.c | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index c1ae84a5..27e5be0f 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ PKGFLAGS:=$(shell pkg-config --cflags --libs openssl ao) -CFLAGS:=-O0 +CFLAGS:=-O2 LDFLAGS:=-lm -lpthread PAUFLAGS:=-lportaudio diff --git a/hairtunes.c b/hairtunes.c index d637d653..60817f95 100644 --- a/hairtunes.c +++ b/hairtunes.c @@ -628,6 +628,8 @@ int stuff_buffer(double playback_rate, short *inptr, short *outptr) { *outptr++ = dithered_vol(*inptr++); } } + + return stuffsamp; } void *audio_thread_func(void *arg) { From 0e652b21d49bcac7671180f50b6699569e3bb1da Mon Sep 17 00:00:00 2001 From: jhl Date: Fri, 15 Apr 2011 22:35:07 +1000 Subject: [PATCH 5/9] Fix compile bug on some IPv4 platforms. --- hairtunes.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hairtunes.c b/hairtunes.c index 60817f95..737df046 100644 --- a/hairtunes.c +++ b/hairtunes.c @@ -424,7 +424,9 @@ int init_rtp(void) { si.sin6_flowinfo = 0; #else si.sin_family = AF_INET; - si.sin_len = sizeof(si); + #ifdef SIN_LEN + si.sin_len = sizeof(si); + #endif si.sin_addr.s_addr = htonl(INADDR_ANY); #endif From f4addf902de216648c11a1b8b8569b20df9d13d4 Mon Sep 17 00:00:00 2001 From: jhl Date: Fri, 15 Apr 2011 22:47:03 +1000 Subject: [PATCH 6/9] fix missing return value fix - replacing right bug, wrong fix. --- hairtunes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hairtunes.c b/hairtunes.c index 737df046..e785baac 100644 --- a/hairtunes.c +++ b/hairtunes.c @@ -631,7 +631,7 @@ int stuff_buffer(double playback_rate, short *inptr, short *outptr) { } } - return stuffsamp; + return frame_size + stuff; } void *audio_thread_func(void *arg) { From 2fbfd2782b9bcda2e1362545aa25be784284425e Mon Sep 17 00:00:00 2001 From: okertanov Date: Fri, 15 Apr 2011 16:01:10 +0300 Subject: [PATCH 7/9] Compilation fixes: -Wall warnings. unistd.h included, forward function ptototypes, loop/assignment parentheses, proper return pathes, buffers signed/unsigned __attribute__((unused)) --- Makefile | 2 +- hairtunes.c | 34 +++++++++++++++++++++++----------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 1b119446..80278687 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ PKGFLAGS:=$(shell pkg-config --cflags --libs openssl ao) -CFLAGS:=-O2 +CFLAGS:=-O2 -Wall LDFLAGS:=-lm -lpthread PAUFLAGS:=-lportaudio diff --git a/hairtunes.c b/hairtunes.c index 60817f95..73efb336 100644 --- a/hairtunes.c +++ b/hairtunes.c @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -84,8 +85,11 @@ int fancy_resampling = 1; SRC_STATE *src; #endif -void rtp_request_resend(seq_t first, seq_t last); +int init_rtp(void); void init_buffer(void); +int init_output(void); +int uninit_output(void); +void rtp_request_resend(seq_t first, seq_t last); void ab_resync(void); // interthread variables @@ -160,7 +164,7 @@ int main(int argc, char **argv) { char *arg; int i; assert(RAND_MAX >= 0x10000); // XXX move this to compile time - while (arg = *++argv) { + while ( (arg = *++argv) ) { if (!strcasecmp(arg, "iv")) { hexaesiv = *++argv; argc--; @@ -203,7 +207,7 @@ int main(int argc, char **argv) { memset(fmtp, 0, sizeof(fmtp)); i = 0; - while (arg = strsep(&fmtpstr, " \t")) + while ( (arg = strsep(&fmtpstr, " \t")) ) fmtp[i++] = atoi(arg); init_decoder(); @@ -247,6 +251,8 @@ int main(int argc, char **argv) { fflush(stderr); uninit_output(); + + return EXIT_SUCCESS; } void init_buffer(void) { @@ -271,14 +277,14 @@ static inline int seq_order(seq_t a, seq_t b) { } void alac_decode(short *dest, char *buf, int len) { - char packet[MAX_PACKET]; + unsigned char packet[MAX_PACKET]; assert(len<=MAX_PACKET); - char iv[16]; + unsigned char iv[16]; int i; memcpy(iv, aesiv, sizeof(iv)); for (i=0; i+16<=len; i += 16) - AES_cbc_encrypt(buf+i, packet+i, 0x10, &aes, iv, AES_DECRYPT); + AES_cbc_encrypt((unsigned char*)buf+i, packet+i, 0x10, &aes, iv, AES_DECRYPT); if (len & 0xf) memcpy(packet+i, buf+i, len & 0xf); @@ -378,6 +384,8 @@ void *rtp_thread_func(void *arg) { buffer_put_packet(seqno, pktp+12, plen-12); } } + + return 0; } void rtp_request_resend(seq_t first, seq_t last) { @@ -406,7 +414,7 @@ int init_rtp(void) { #ifdef AF_INET6 struct sockaddr_in6 si; int type = AF_INET6; - short *sin_port = &si.sin6_port; + unsigned short *sin_port = &si.sin6_port; #else struct sockaddr_in si; int type = AF_INET; @@ -506,9 +514,11 @@ static void biquad_lpf(biquad_t *bq, double freq, double Q) { static double biquad_filt(biquad_t *bq, double in) { double w = in - bq->a[0]*bq->hist[0] - bq->a[1]*bq->hist[1]; - double out = bq->b[1]*bq->hist[0] + bq->b[2]*bq->hist[1] + bq->b[0]*w; + double out __attribute__((unused)) = bq->b[1]*bq->hist[0] + bq->b[2]*bq->hist[1] + bq->b[0]*w; bq->hist[1] = bq->hist[0]; bq->hist[0] = w; + + return w; } double bf_playback_rate = 1.0; @@ -638,9 +648,9 @@ void *audio_thread_func(void *arg) { #else PaStream* stream = arg; #endif - int i, play_samples; + int i __attribute__((unused)), play_samples; - signed short buf_fill; + signed short buf_fill __attribute__((unused)); signed short *inbuf, *outbuf; outbuf = malloc(OUTFRAME_BYTES); @@ -695,7 +705,7 @@ void *audio_thread_func(void *arg) { #define NUM_CHANNELS 2 int init_output(void) { - int err; + int err __attribute__((unused)); #if HAVE_AO ao_initialize(); int driver = ao_default_driver_id(); @@ -758,6 +768,8 @@ int init_output(void) { pthread_t audio_thread; pthread_create(&audio_thread, NULL, audio_thread_func, arg); + + return 0; } int uninit_output(void) { From 2ac93d7fcb59b8ab912f255e58e15d595dd8ee24 Mon Sep 17 00:00:00 2001 From: Albert Zeyer Date: Fri, 15 Apr 2011 15:29:06 +0200 Subject: [PATCH 8/9] fixed missing return --- hairtunes.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hairtunes.c b/hairtunes.c index 03342f0d..70cd6e31 100644 --- a/hairtunes.c +++ b/hairtunes.c @@ -702,6 +702,8 @@ void *audio_thread_func(void *arg) { } #endif } + + return 0; } #define NUM_CHANNELS 2 From 19d330efd90dd687f383c5ce66da96e6567babff Mon Sep 17 00:00:00 2001 From: Albert Zeyer Date: Fri, 15 Apr 2011 15:29:33 +0200 Subject: [PATCH 9/9] small fix for Makefile This fails if for example only hairtunes.c was changed but not alac.c. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 80278687..4cd42fd1 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ PAUFLAGS:=-lportaudio all: hairtunes hairtunes: hairtunes.c alac.c - $(CC) $(CFLAGS) $(PKGFLAGS) $(LDFLAGS) $? -o $@ + $(CC) $(CFLAGS) $(PKGFLAGS) $(LDFLAGS) hairtunes.c alac.c -o $@ clean: -@rm -rf hairtunes