Created
April 26, 2018 17:56
-
-
Save p120ph37/c64ccf98a61e64aa8334e35cadb6b5f5 to your computer and use it in GitHub Desktop.
Send an RTP stream of G.711-silence to an arbitrary destination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use IO::Socket; | |
# use Time::HiRes qw/time sleep/; | |
# Since Time::HiRes is not always available, use the syscalls directly: | |
require 'syscall.ph'; | |
use subs qw/time sleep/; | |
sub time { | |
my $tv = pack('q2', 0, 0); | |
syscall(SYS_gettimeofday(), $tv, undef); | |
my($s, $u) = unpack('q2', $tv); | |
return $s + ($u / 1e6); | |
} | |
sub sleep { | |
my($u) = @_; | |
my $ts = pack('q2', $u, ($u - int $u) * 1e9); | |
return syscall(SYS_nanosleep(), $ts, undef); | |
} | |
# End missing Time::HiRes workaround | |
my($interval, $addr, $peer) = @ARGV; | |
$interval ||= 20; | |
$addr ||= '0.0.0.0'; | |
$addr =~ /:\d+$/ or $addr .= ':9000'; | |
$peer ||= '127.0.0.1'; | |
$peer =~ /:\d+$/ or $peer .= ':9000'; | |
print "Interval: $interval\n"; | |
print "Addr: $addr\n"; | |
print "Peer: $peer\n"; | |
my $id = int(rand(2**32)); | |
my $timestamp = int(time() * 1000 / 20); | |
my $t = time; | |
while(1) { | |
rtpping($addr, $peer, $interval); | |
$t += $interval / 1000; | |
my $n = time; | |
if($t < $n) { | |
$t = $n; | |
} else { | |
sleep($t - $n); | |
} | |
} | |
sub xlog { | |
my $t = time; | |
sprintf('%.6f', $t - int $t) =~ /\.(\d+)/; | |
my $f = $1; | |
my $y = (localtime($t))[5] + 1900; | |
my $m = (localtime($t))[4] + 1; | |
printf '%04d-%02d-%02d %02d:%02d:%02d.%s: %s', $y, $m, (localtime($t))[3, 2, 1, 0], $f, "@_"; | |
} | |
my $sock; | |
sub rtpping { | |
my($addr, $peer, $ms) = @_; | |
if(not defined $sock) { | |
my $newsock = IO::Socket::INET->new( | |
LocalAddr => $addr, | |
PeerAddr => $peer, | |
Proto => 'udp', | |
) or die "Could not create socket: $!\n"; | |
$sock = $newsock; | |
} | |
my $localport = $sock->sockport(); | |
my $localhost = $sock->sockhost(); | |
my $peerport = $sock->peerport(); | |
my $peerhost = $sock->peerhost(); | |
# Simulated RTP packet of G.711 silence | |
my $seq = $timestamp % 0x10000; | |
my $msg = pack('CCnNN', | |
0x80, # RFC-1889 v2 | |
0x00, # G.711 | |
$seq, $timestamp, $id); | |
$msg .= pack('C', 255) x (8 * $ms); | |
$timestamp ++; | |
$sock->send($msg) or $! == 111 or die "Send error: $!\n"; | |
xlog("Sent ${ms}ms G.711 silence $localhost:$localport -> $peerhost:$peerport #$seq\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Syntax:
perl rtpping.pl {packet_ms} {src_ip}:{src_port} {dst_ip}:{dst_port}
Example:
perl rtpping.pl 20 10.0.0.2:9000 192.168.1.2:9000