| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Perlbal::Plugin::FlvStreaming - Enable FLV streaming with reverse proxy | ||
| 4 | |||
| 5 | =head1 DESCRIPTION | ||
| 6 | |||
| 7 | This plugin enable FLV streaming by modifying headers and prepending | ||
| 8 | FLV header to the body. | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | LOAD FlvStreaming | ||
| 13 | |||
| 14 | CREATE SERVICE flv_proxy | ||
| 15 | SET role = reverse_proxy | ||
| 16 | SET plugins = FlvStreaming | ||
| 17 | ENABLE flv_proxy | ||
| 18 | |||
| 19 | |||
| 20 | =head1 LICENSE | ||
| 21 | |||
| 22 | This plugin is part of the Perlbal distribution, and as such can be | ||
| 23 | distributed under the same licence terms as the rest of Perlbal. | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | package Perlbal::Plugin::FlvStreaming; | ||
| 28 | |||
| 29 | use strict; | ||
| 30 | use warnings; | ||
| 31 | use URI; | ||
| 32 | use URI::QueryParam; | ||
| 33 | |||
| 34 | my $FLV_HEADER = 'FLV' . pack('CCNN', 1, 1, 9, 9); | ||
| 35 | |||
| 36 | sub load { } | ||
| 37 | |||
| 38 | sub register { | ||
| 39 | my ($class, $svc) = @_; | ||
| 40 | unless ($svc && $svc->{role} eq "reverse_proxy") { | ||
| 41 | die "You can't load the flvstreaming plugin on a service not of role reverse_proxy.\n"; | ||
| 42 | } | ||
| 43 | |||
| 44 | $svc->register_hook('FlvStreaming', 'start_proxy_request', \&start_proxy_request); | ||
| 45 | $svc->register_hook('FlvStreaming', 'modify_response_headers', \&modify_response_headers); | ||
| 46 | $svc->register_hook('FlvStreaming', 'prepend_body', \&prepend_body); | ||
| 47 | |||
| 48 | return 1; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub start_proxy_request { | ||
| 52 | my Perlbal::ClientProxy $client = shift; | ||
| 53 | |||
| 54 | my $uri = URI->new( $client->{req_headers}->{uri} ); | ||
| 55 | my $path = $uri->path; | ||
| 56 | return 0 if $path !~ /\.flv$/; | ||
| 57 | |||
| 58 | my $start = $uri->query_param('start'); | ||
| 59 | return 0 unless $start; | ||
| 60 | |||
| 61 | $client->{req_headers}->header('range', "bytes=$start-"); | ||
| 62 | Perlbal::log('debug', "FlvStreaming: $uri") if Perlbal::DEBUG; | ||
| 63 | Perlbal::log('debug', "FlvStreaming: Add request header 'Range: bytes=$start-'") if Perlbal::DEBUG; | ||
| 64 | return 0; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub modify_response_headers { | ||
| 68 | my Perlbal::BackendHTTP $be = shift; | ||
| 69 | my Perlbal::ClientProxy $client = shift; | ||
| 70 | |||
| 71 | my $headers = $client->{res_headers}; | ||
| 72 | |||
| 73 | my $uri = URI->new( $client->{req_headers}->{uri} ); | ||
| 74 | my $path = $uri->path; | ||
| 75 | return 0 if $path !~ /\.flv$/; | ||
| 76 | |||
| 77 | my $start = $uri->query_param('start'); | ||
| 78 | return 0 unless $start; | ||
| 79 | |||
| 80 | $headers->{responseLine} = 'HTTP/1.0 200 OK'; | ||
| 81 | $headers->{code} = '200'; | ||
| 82 | |||
| 83 | delete $headers->{headers}->{'accept-ranges'}; | ||
| 84 | delete $headers->{headers}->{'content-range'}; | ||
| 85 | |||
| 86 | $headers->{headers}->{'content-type'} = 'video/x-flv'; | ||
| 87 | $headers->{headers}->{'content-length'} += length $FLV_HEADER; | ||
| 88 | |||
| 89 | return 0; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub prepend_body { | ||
| 93 | my Perlbal::BackendHTTP $be = shift; | ||
| 94 | my Perlbal::ClientProxy $client = shift; | ||
| 95 | |||
| 96 | my $uri = URI->new( $client->{req_headers}->{uri} ); | ||
| 97 | my $path = $uri->path; | ||
| 98 | return 0 if $path !~ /\.flv$/; | ||
| 99 | |||
| 100 | my $start = $uri->query_param('start'); | ||
| 101 | return 0 unless $start; | ||
| 102 | |||
| 103 | $client->write($FLV_HEADER); | ||
| 104 | return 0; | ||
| 105 | } | ||
| 106 | |||
| 107 | 1; |