summaryrefslogtreecommitdiff
path: root/bin/send-gst-rtsp-v-a
blob: 6f88b8c3933dbd6a6de83250b6a91d9138edeff7 (plain)
  1. #!/usr/bin/perl
  2. # Send live video/audio media as RTP streams, published via RTSP
  3. use v5.12;
  4. use warnings;
  5. use Glib qw( TRUE FALSE );
  6. use Glib::Object::Introspection;
  7. use IPC::System::Simple qw(capturex);
  8. BEGIN {
  9. Glib::Object::Introspection->setup(
  10. basename => 'Gst',
  11. version => '1.0',
  12. package => 'Gst',
  13. );
  14. Glib::Object::Introspection->setup(
  15. basename => 'GstRtspServer',
  16. version => '1.0',
  17. package => 'Gst',
  18. );
  19. }
  20. my $ADDRESS = shift || $ENV{'ADDRESS'} || '127.0.0.1';
  21. my $PORT = shift || $ENV{'PORT'} || '8554';
  22. my $VDEVICES = shift || $ENV{'VDEVICES'} || '';
  23. my $ADEVICES = shift || $ENV{'ADEVICES'} || '';
  24. my $VFORMAT = shift || $ENV{'VFORMAT'} || 'RAW'; # H264 VP8 RAW - default: RAW
  25. my $AFORMAT = shift || $ENV{'AFORMAT'} || 'RAW'; # AMR OPUS RAW - default: RAW
  26. my @VDEVICES = $VDEVICES ? split ' ', $VDEVICES : sort split ' ', capturex('find', qw(/dev -maxdepth 1 -type c -name video*));
  27. # FIXME: Detect/blacklist and skip faulty devices
  28. #my @ADEVICES = grep { /^hw:/ } capturex( 'arecord', qw(-L) );
  29. my @ADEVICES = split ' ', $ADEVICES;
  30. chomp @ADEVICES;
  31. #use Data::Dump; die dd @ADEVICES;
  32. my $HEIGHT = 240;
  33. my $FRAMERATE = 25;
  34. my $AUDIORATE = 48000;
  35. my $VCAPS = "video/x-raw,height=$HEIGHT";
  36. my $ACAPS = "audio/x-raw,rate=$AUDIORATE,channels=2,depth=16";
  37. # * http://stackoverflow.com/a/42237307
  38. my $ABUFFERS = 20000;
  39. # * force threads using queues - see http://stackoverflow.com/a/30738533
  40. # * generous queue sizes inspired by https://wiki.xiph.org/GST_cookbook
  41. my $QUEUE = "queue max-size-bytes=100000000 max-size-time=0";
  42. my %VFORMAT = (
  43. H264 => {
  44. # * let x264 use low-latency sliced-threads (i.e. don't disable treads)
  45. VENC => "x264enc speed-preset=ultrafast tune=zerolatency bitrate=800 byte-stream=true key-int-max=15 intra-refresh=true option-string=\"slice-max-size=8192:vbv-maxrate=80:vbv-bufsize=10\" ! video/x-h264,profile=baseline ! $QUEUE ! rtph264pay",
  46. },
  47. VP8 => {
  48. VENC => "vp8enc cpu-used=10 threads=2 deadline=10000 ! video/x-vp8 ! $QUEUE ! rtpvp8pay",
  49. },
  50. RAW => {
  51. VENC => "rtpvrawpay",
  52. },
  53. );
  54. my %AFORMAT = (
  55. AMR => {
  56. AENC => "amrnbenc ! $QUEUE ! rtpamrpay",
  57. },
  58. OPUS => {
  59. AENC => "opusenc ! $QUEUE ! rtpopuspay",
  60. },
  61. RAW => {
  62. AENC => "rtpL16pay",
  63. },
  64. );
  65. our $nextpayload = 0;
  66. sub cam {
  67. my $device = shift;
  68. my $payload = "pay" . $nextpayload++;
  69. my $factory = Gst::RTSPMediaFactory->new();
  70. $factory->set_launch("( v4l2src device=$device ! $QUEUE ! videoconvert ! $VCAPS ! $QUEUE ! $VFORMAT{$VFORMAT}{'VENC'} name=$payload )");
  71. $factory->set_shared(TRUE);
  72. say "media ($device): " . $factory->get_launch();
  73. # $factory->set_latency(5);
  74. #say "latency ($device): " . $factory->get_latency();
  75. return $factory;
  76. }
  77. sub mic {
  78. my $device = shift;
  79. my $payload = "pay" . $nextpayload++;
  80. my $factory = Gst::RTSPMediaFactory->new();
  81. $factory->set_launch("( alsasrc device=$device buffer-time=$ABUFFERS ! $QUEUE ! audioconvert ! $QUEUE ! $AFORMAT{$AFORMAT}{'AENC'} name=$payload )");
  82. $factory->set_shared(TRUE);
  83. #say "media ($device): " . $factory->get_launch();
  84. # $factory->set_latency(5);
  85. #say "latency ($device): " . $factory->get_latency();
  86. return $factory;
  87. }
  88. Gst::init([ $0, @ARGV ]);
  89. my $loop = Glib::MainLoop->new( undef, FALSE );
  90. # create a server instance
  91. my $server = Gst::RTSPServer->new();
  92. $server->set_address($ADDRESS);
  93. $server->set_service($PORT);
  94. # get the mount points for this server, every server has a default
  95. # object that be used to map uri mount points to media factories
  96. my $mounts = $server->get_mount_points();
  97. # attach media to URIs
  98. my @mounts;
  99. for my $i ( 0 .. $#VDEVICES ) {
  100. my $mount = "/cam$i";
  101. $mounts->add_factory($mount, cam($VDEVICES[$i]));
  102. push @mounts, $mount;
  103. };
  104. for my $i ( 0 .. $#ADEVICES ) {
  105. my $mount = "/mic$i";
  106. $mounts->add_factory($mount, mic($ADEVICES[$i]));
  107. push @mounts, $mount;
  108. };
  109. # don't need the ref to the mapper anymore
  110. undef $mounts;
  111. # attach the server to the default maincontext
  112. my $retval = $server->attach(undef);
  113. # start serving
  114. say "streams ready at the following URLs:";
  115. for (@mounts) {
  116. say "rtsp://$ADDRESS:$PORT$_";
  117. }
  118. $loop->run;