File: Server.pm

package info (click to toggle)
librtsp-server-perl 0.06-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 184 kB
  • sloc: perl: 1,072; sh: 28; makefile: 14
file content (268 lines) | stat: -rw-r--r-- 5,565 bytes parent folder | download | duplicates (3)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package RTSP::Server;

use Moose;
    with 'MooseX::Getopt';

use namespace::autoclean;

use RTSP::Server::Logger;
use RTSP::Server::Source;
use RTSP::Server::Client;

our $VERSION = '0.06';
our $RTP_START_PORT = 20_000;

## configuration attributes

has 'client_listen_port' => (
    is => 'rw',
    isa => 'Int',
    default => '554',
    cmd_flag => 'clientport',
    cmd_aliases => 'c',
    metaclass => 'MooseX::Getopt::Meta::Attribute',
);

has 'source_listen_port' => (
    is => 'rw',
    isa => 'Int',
    default => '5545',
    cmd_flag => 'serverport',
    cmd_aliases => 's',
    metaclass => 'MooseX::Getopt::Meta::Attribute',
);

has 'client_listen_address' => (
    is => 'rw',
    isa => 'Str',
    default => '0.0.0.0',
);

has 'source_listen_address' => (
    is => 'rw',
    isa => 'Str',
    default => '0.0.0.0',
);

has 'log_level' => (
    is => 'rw',
    isa => 'Int',
    default => 2,
    cmd_flag => 'loglevel',
    cmd_aliases => 'l',
    metaclass => 'MooseX::Getopt::Meta::Attribute',
);

has 'max_clients' => (
    is => 'rw',
    isa => 'Int',
    default => 100,
);

## internal attributes

has 'rtp_start_port' => (
    is => 'rw',
    isa => 'Int',
    default => $RTP_START_PORT,
);

has 'source_server' => (
    is => 'rw',
    clearer => 'close_source_server',
    traits => [ 'NoGetopt' ],
);

has 'client_server' => (
    is => 'rw',
    clearer => 'close_client_server',
    traits => [ 'NoGetopt' ],
);

has 'logger' => (
    is => 'rw',
    isa => 'RTSP::Server::Logger',
    handles => [qw/ trace debug info warn error /],
    lazy => 1,
    builder => 'build_logger',
    traits => [ 'NoGetopt' ],
);

# map of uri => Mount
has 'mounts' => (
    is => 'rw',
    isa => 'HashRef',
    default => sub { {} },
    lazy => 1,
    traits => [ 'NoGetopt' ],
);

sub client_count {
    my ($self) = @_;

    return $self->client_server->connection_count;
}

sub client_stopped {
    my ($self, $client) = @_;
    $self->client_count($self->client_count + 1);
}

sub next_rtp_start_port {
    my ($self) = @_;

    my $port = $self->rtp_start_port;
    $self->rtp_start_port($port + 2);

    return $port;
}

# call from time to time to keep things tidy
sub housekeeping {
    my ($self) = @_;

    # if we have no more mount points, it's safe to reset the rtp
    # start ports
    unless (keys %{ $self->mounts }) {
        $self->rtp_start_port($RTP_START_PORT);
    }
}

# call this to start the server
sub listen {
    my ($self) = @_;

    print "Starting RTSP server, log level = " . $self->log_level . "\n";

    my $source_server = $self->start_source_server;
    my $client_server = $self->start_client_server;
}

sub start_client_server {
    my ($self) = @_;

    $self->close_client_server;

    my $bind_ip = $self->client_listen_address;
    my $bind_port = $self->client_listen_port;

    my $server = RTSP::Server::Client->new(
        listen_address => $bind_ip,
        listen_port => $bind_port,
        server => $self,
    );

    $server->listen;

    $self->client_server($server);
    $self->info("Client server started");
    
    return $server;
}

sub start_source_server {
    my ($self) = @_;

    $self->close_source_server;

    my $bind_ip = $self->source_listen_address;
    my $bind_port = $self->source_listen_port;

    my $server = RTSP::Server::Source->new(
        listen_address => $bind_ip,
        listen_port => $bind_port,
        server => $self,
    );

    $server->listen;

    $self->source_server($server);
    $self->info("Source server started");
    
    return $server;
}

sub build_logger {
    my ($self) = @_;

    return RTSP::Server::Logger->new(
        log_level => $self->log_level
    );
}

__PACKAGE__->meta->make_immutable;

__END__

=head1 NAME

RTSP::Server - Lightweight RTSP/RTP server. Like icecast, for
audio/video streams.

=head1 SYNOPSIS

  use AnyEvent;
  use RTSP::Server;

  # defaults:
  my $srv = new RTSP::Server(
      log_level             => 2,   # 0 = no output, 5 = most verbose
      max_clients           => 100,
      client_listen_port    => 554,
      source_listen_port    => 5545,
      rtp_start_port        => 20000,
      client_listen_address => '0.0.0.0',
      source_listen_address => '0.0.0.0',
  );

  # listen and accept incoming connections asynchronously
  # (returns immediately)
  $srv->listen;

  # main loop
  my $cv = AnyEvent->condvar;
  # ...
  $cv->recv;

  undef $srv;  # when the server goes out of scope, all sockets will
               # be cleaned up

=head1 DESCRIPTION

This server is designed to enable to rebroadcasting of RTP media
streams to clients, controlled by RTSP. Please see README for more
information.

=head1 USAGE

After starting the server, stream sources may send an ANNOUNCE for a
desired mountpoint, followed by a RECORD request to begin streaming.
Clients can then connect on the client port at the same mountpoint and
send a PLAY request to receive the RTP data streamed from the source.

=head1 BUNDLED APPLICATIONS

Includes rtsp-server.pl, which basically contains the synopsis.

=head2 COMING SOON

Priv dropping, authentication, client encoder, stats, tests

=head1 SEE ALSO

L<RTSP::Proxy>, L<RTSP::Client>, L<AnyEvent::Socket>

=head1 AUTHOR

Mischa Spiegelmock, E<lt>revmischa@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2010 by Mischa Spiegelmock

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.0 or,
at your option, any later version of Perl 5 you may have available.


=cut