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