NAME

RTMP::Client - A Simple RTMP client

SYNOPSIS

use RTMP::Client qw(rtmp_connect rtmp_play rtmp_call);

print "connect success\n" if rtmp_connect('192.168.1.1', '1935', 'live/23');
rtmp_play('MainB_1', '-1.0', '-1.0');
rtmp_call('YourFunc', 'YourARGV');

DESCRIPTION

This is a simple RTMP client without video or audio decode. It implemented in pure PERL including packing Adobe amf packages.

METHODS

rtmp_connect($rtmp_server_address, $server_port, $application_name)

Just like the 'NetConnection.connect()' function in ActionScript, with args are set in different way.

rtmp_play($stream_or_file_name, $play_type, $length, $interval_call_hook_function, $hook_function)

Just like the 'NetStream.play()' function in ActionScript, with args are set in different way. You can use the last two args or not.

rtmp_call($stream_or_file_name, $play_type, $length, $file_path_to_store_the_data_received)

Just like the 'NetStream.call()' function in ActionScript, with args are set in different way.

EXAMPLES

use RTMP::Client qw(rtmp_connect rtmp_play rtmp_call);

Speed Detection

report download speed every 5 secs.

print "connect success\n" if rtmp_connect('192.168.1.1', '1935', 'live/23');
my $report_time = 5;
rtmp_play('MainB_1', '-1.0', '-1.0', $report_time, \&speed_detector);
sub speed_detector
{
    my $rev_length = shift;
    my $speed = $rev_length / 1024 / $report_time;
    if ($speed > 3)
    {
        my $cur_time = strftime("%F_%T", localtime);
        print $cur_time, "\t", $speed, "\tKbytes/s\n";
    }
    else
    {
        print "too slow !\n";
    }
}

Save to File

do things like "rtmpdump".http://rtmpdump.mplayerhq.hu/

print "connect success\n" if rtmp_connect('192.168.1.1', '1935', 'live/23');
my $loop_time = 10;
rtmp_play('MainB_1', '-1.0', '-1.0', $report_time, \&save_to_file);
sub save_to_file
{
    my $rev_length = shift;
    my $rev_binary = shift;
    open my $fh,">>","/root/rtmp_dump.bin";
    binmode $fh;
    print $fh $rev_binary;
    close $fh;
}

SOME INTERNAL METHODS

rtmp_handshake()

No args need. Called in function rtmp_connect().

pack_amf_body_to_chunks($string, $object_id, $stream_id, $type)

Output a available binary amf packet. Works on amf message body, just like add a right amf header before the message body.

pack_amf_object_start()

pack_amf_object_end()

pack_amf_attribute_name($string)

It packs a attribute_name which less than 65536 bytes or return null.

pack_amf_number($double)

Return 9 bytes binary data.

pack_amf_boolean($boolean)

Return 2 bytes binary data.

pack_amf_string($string)

it can pack a string which less than 65536 bytes or it return null. There will be a long string packer in future.

pack_amf_boolean($boolean)

Return 1 byte binary data.

my_recv_a_chunk()

Recieve a rtmp chunk.

my_recv_a_msg()

Recieve a rtmp message.

my_recv_nostop(\&sub)

Wait until recieved bytes, then return it.

my_recv($int_wanted_length, $int_time_out)

Wait $int_time_out Seconds, or Recieve $int_wanted_length bytes, then return it.

my_send_bin($binary_data)

Send binary data to server.

my_send_hex(@array_with_hex)

Convert hex array to binary ,then send them to server.

Print binary data in a readable format.

rtmp_timer($int_chunk_id)

Return rtmp timestamp of the chunk stream id in string format. From Adobe RTMP Spec: 1.Timestamps in RTMP Chunk Stream are given as an integer number of milliseconds. 2.each Chunk Stream will start with a timestamp of 0, but this is not required. 3.Timestamps MUST be monotonically increasing, and SHOULD be linear in time.

reset_rtmp_timer($chunk_id)

reset_rtmp_timer($chunk_id, $chunk_message_length)

get_rtmp_chunk_msg_length($chunk_id)

set_rtmp_chunk_msg_type_id($chunk_id, $chunk_message_type_id)

get_rtmp_chunk_msg_type_id($chunk_id)

set_rtmp_chunk_msg_stream_id($chunk_id, $chunk_message_stream_id)

get_rtmp_chunk_msg_stream_id($chunk_id)

set_rtmp_client_chunk_size($new_chunk_size)

get_rtmp_client_chunk_size

reset_rtmp_msg($chunk_id)

put_rtmp_msg($chunk_id, $string)

get_rtmp_msg

set_rtmp_window($window_size, $limit_type)

get_rtmp_window()

set_rtmp_peer_window($window_size)

get_rtmp_peer_window()

dec($binary_data)

Dump the data to dec

dec($string, $indent, $front_color, $back_color)

print colorful strings

analysis_rtmp_msg($msg_type, $msg)

output the rtmp stream information to STDOUT

AUTHOR

Written by ChenGang, yikuyiku.com@gmail.com http://blog.yikuyiku.com/

COPYRIGHT

Copyright (c) 2011 ChenGang.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Kamaitachi