Perl script as a router between icecast2 and VLC client

69 Views Asked by At

I wrote a Perl script to act as router between an icecast2 server and a VLC client. I start the script via plackup --listen :8000 testStreamingServer.pl --debug

The problem is, I always get to the print "Starting server\n"; line but neither VLC, Firefox or curl can connect to the audio stream. There is no further info on the server side. No debug message, nothing.

Here is the code:

#!/usr/bin/env perl
use strict;
use warnings;

use Data::Dumper;
use LWP::UserAgent;
use Plack::Runner;
use File::Temp qw(tempfile);

my $source_url = 'http://server1.example.com/stream.mp3';
my $redirect_url = 'http://server2.example.com/stream.mp3';
my $ua = LWP::UserAgent->new();

print "Starting server\n";

my $app = sub {
    print "Request received\n";
    my $response = $ua->get($source_url);

    if ($response->is_success) {
        print "Source server is online, stream is playable\n";
        my ($fh, $filename) = tempfile();
        my $content_response = $ua->get($source_url, ':content_file' => $filename);
        my $headers = $content_response->headers();
        print Dumper $headers;
        my $size = -s $filename;
        open(my $file, "<", $filename);
        my $file_content = do { local $/; <$file> };
        close $file;
        return [
            200,
            [ 'Content-Type' => 'audio/mpeg',
            'Content-Length' => $size,
            'icy-metaint' => 8192,
            'icy-name' => 'My Stream',
            'icy-description' => 'My stream description',
            'icy-genre' => 'variety',
            'icy-pub' => 1,
            'icy-br' => 128,
            'Connection' => 'close' ],
            [ $file_content ],
        ];

    } else {
        print "Source server is offline, redirecting to backup stream\n";
        return [
            302,
            [ 'Location' => $redirect_url, 'Connection' => 'close' ],
            [],
        ];
    }
};

my $runner = Plack::Runner->new;
$runner->parse_options(qw(--listen :8000 --server HTTP::Server::PSGI));
$runner->run($app);
print "Server started\n";

I tried print debugging, line by line. I am clueless right now.

0

There are 0 best solutions below