File: FlvStreaming.pm

package info (click to toggle)
perlbal 1.80-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 1,264 kB
  • sloc: perl: 11,215; sh: 98; makefile: 5
file content (107 lines) | stat: -rw-r--r-- 2,746 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
=head1 NAME

Perlbal::Plugin::FlvStreaming - Enable FLV streaming with reverse proxy

=head1 DESCRIPTION

This plugin enable FLV streaming by modifying headers and prepending
FLV header to the body.

=head1 SYNOPSIS

    LOAD FlvStreaming
    
    CREATE SERVICE flv_proxy
      SET role    = reverse_proxy
      SET plugins = FlvStreaming
    ENABLE flv_proxy


=head1 LICENSE

This plugin is part of the Perlbal distribution, and as such can be
distributed under the same licence terms as the rest of Perlbal.

=cut

package Perlbal::Plugin::FlvStreaming;

use strict;
use warnings;
use URI;
use URI::QueryParam;

my  $FLV_HEADER = 'FLV' . pack('CCNN', 1, 1, 9, 9);

sub load { }

sub register {
    my ($class, $svc) = @_;
    unless ($svc && $svc->{role} eq "reverse_proxy") {
        die "You can't load the flvstreaming plugin on a service not of role reverse_proxy.\n";
    }

    $svc->register_hook('FlvStreaming', 'start_proxy_request',     \&start_proxy_request);
    $svc->register_hook('FlvStreaming', 'modify_response_headers', \&modify_response_headers);
    $svc->register_hook('FlvStreaming', 'prepend_body',            \&prepend_body);

    return 1;
}

sub start_proxy_request {
    my Perlbal::ClientProxy $client = shift;

    my $uri  = URI->new( $client->{req_headers}->{uri} );
    my $path = $uri->path;
    return 0 if $path !~ /\.flv$/;

    my $start = $uri->query_param('start');
    return 0 unless $start;

    $client->{req_headers}->header('range', "bytes=$start-");
    Perlbal::log('debug', "FlvStreaming: $uri") if Perlbal::DEBUG;
    Perlbal::log('debug', "FlvStreaming: Add request header 'Range: bytes=$start-'") if Perlbal::DEBUG;
    return 0;
}

sub modify_response_headers {
    my Perlbal::BackendHTTP $be     = shift;
    my Perlbal::ClientProxy $client = shift;

    my $headers = $client->{res_headers};

    my $uri  = URI->new( $client->{req_headers}->{uri} );
    my $path = $uri->path;
    return 0 if $path !~ /\.flv$/;

    my $start = $uri->query_param('start');
    return 0 unless $start;

    $headers->{responseLine}   = 'HTTP/1.0 200 OK';
    $headers->{code}           = '200';

    delete $headers->{headers}->{'accept-ranges'};
    delete $headers->{headers}->{'content-range'};

    $headers->{headers}->{'content-type'}    = 'video/x-flv';
    $headers->{headers}->{'content-length'} += length $FLV_HEADER;

    return 0;
}

sub prepend_body {
    my Perlbal::BackendHTTP $be     = shift;
    my Perlbal::ClientProxy $client = shift;

    my $uri  = URI->new( $client->{req_headers}->{uri} );
    my $path = $uri->path;
    return 0 if $path !~ /\.flv$/;

    my $start = $uri->query_param('start');
    return 0 unless $start;

    $client->write($FLV_HEADER);
    return 0;
}

1;