1#!/usr/local/bin/perl 2# Copyright (C) 2005-2006 Quentin Sculo <squentin@free.fr> 3# 4# This program is free software; you can redistribute it and/or modify 5# it under the terms of the GNU General Public License version 3, as 6# published by the Free Software Foundation 7 8use strict; 9use warnings; 10use Socket; 11use constant { EOL => "\015\012" }; 12my ($file,$sec,$title); 13my $port=8000; 14while (my $arg=shift) 15{ if ($arg eq '-p') {$port=shift||8000} 16 elsif ($arg eq '-b') {shift} 17 elsif ($arg eq '-K') {$sec='-k '.shift} 18 elsif ($arg eq '-title') {$title=shift} 19 elsif (-f $arg) {$file=$arg} 20} 21 22$title||=$file; 23my $mime='audio/x-unknown'; #FIXME 24if ($file=~m/mp3$/i) { $mime='audio/mpeg'; } 25elsif ($file=~m/ogg$|oga$/i) { $mime='application/ogg'; } 26elsif ($file=~m/flac$/i) { $mime='audio/x-flac'; } 27elsif ($file=~m/mpc$/i) { $mime='audio/x-musepack'; } 28 29sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" } 30 31my $proto = getprotobyname('tcp'); 32($port) = $port =~ /^(\d+)$/ or die "invalid port"; 33socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; 34setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, 35 pack('l', 1)) || die "setsockopt: $!"; 36bind(Server, sockaddr_in($port, INADDR_ANY)) || die "bind: $!"; 37listen(Server,SOMAXCONN) || die "listen: $!"; 38logmsg "server started on port $port"; 39 40my $paddr = accept(Client,Server); 41my($port2,$iaddr) = sockaddr_in($paddr); 42#my $name = gethostbyaddr($iaddr,AF_INET); 43logmsg 'connection from ',inet_ntoa($iaddr), " at port $port2"; 44 45#shoutcast and icecast protocol : 46# http://sander.vanzoest.com/talks/2002/audio_and_apache/ 47while (<Client>) 48{ #warn $_; 49 last if $_ eq EOL; 50} 51my $answer= 52 'HTTP/1.0 200 OK'.EOL. 53 'Server: iceserver/0.2'.EOL. 54 "Content-Type: $mime".EOL. 55 "x-audiocast-name: $title".EOL. 56 'x-audiocast-public: 0'.EOL; 57send Client,$answer.EOL,0; 58#warn $answer; 59open FILE,$file; 60while (read FILE,$_,16384) 61{ send Client,$_,0; 62} 63close Client; 64exit; 65 66 67