summaryrefslogtreecommitdiff
path: root/bin/capture+encode+serve
blob: e3fce448d420e63211cbb2d3a68e3b6db122725d (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 strictures 2;
  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 %PIPELINE = (
  48. AMR => {
  49. AENC => [ 'amrnbenc', $QUEUE, 'rtpamrpay' ],
  50. },
  51. H264 => {
  52. # * let x264 use low-latency sliced-threads (i.e. don't disable treads)
  53. VENC => [
  54. "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\"",
  55. 'video/x-h264,profile=baseline',
  56. $QUEUE,
  57. 'rtph264pay',
  58. ],
  59. },
  60. OPUS => {
  61. AENC => [ 'opusenc', $QUEUE, 'rtpopuspay' ],
  62. },
  63. VP8 => {
  64. VENC => [
  65. "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",
  66. 'video/x-vp8',
  67. $QUEUE,
  68. 'rtpvp8pay',
  69. ],
  70. },
  71. RAW => {
  72. AENC => ['rtpL16pay'],
  73. VENC => ['rtpvrawpay'],
  74. },
  75. );
  76. our $nextpayload = 0;
  77. sub cam
  78. {
  79. my ( $device, $payload ) = @_;
  80. my $factory = Gst::RTSPMediaFactory->new();
  81. my $pipeline = join(
  82. ' ! ',
  83. ( "v4l2src device=$device",
  84. $QUEUE,
  85. 'videoconvert',
  86. $VCAPS,
  87. $QUEUE,
  88. @{ $PIPELINE{$VFORMAT}{'VENC'} },
  89. )
  90. );
  91. return "( $pipeline name=pay$payload )";
  92. }
  93. sub mic
  94. {
  95. my ( $device, $payload ) = @_;
  96. my $factory = Gst::RTSPMediaFactory->new();
  97. my $pipeline = join(
  98. ' ! ',
  99. ( "alsasrc device=$device buffer-time=$ABUFFERS",
  100. $QUEUE,
  101. 'audioconvert',
  102. $QUEUE,
  103. @{ $PIPELINE{$AFORMAT}{'AENC'} },
  104. )
  105. );
  106. return "( $pipeline name=pay$payload )";
  107. }
  108. sub factory
  109. {
  110. my @pipeline = @_;
  111. my $factory = Gst::RTSPMediaFactory->new();
  112. $factory->set_launch( join( ' ', @pipeline ) );
  113. $factory->set_shared(TRUE);
  114. #say "media ($device): " . $factory->get_launch();
  115. # $factory->set_latency(5);
  116. #say "latency ($device): " . $factory->get_latency();
  117. return $factory;
  118. }
  119. Gst::init( [ $0, @ARGV ] );
  120. my $loop = Glib::MainLoop->new( undef, FALSE );
  121. # create a server instance
  122. my $server = Gst::RTSPServer->new();
  123. $server->set_address($ADDRESS);
  124. $server->set_service($PORT);
  125. # get the mount points for this server, every server has a default
  126. # object that be used to map uri mount points to media factories
  127. my $mounts = $server->get_mount_points();
  128. # attach media to URIs
  129. my @mounts;
  130. for my $i ( 0 .. $#VDEVICES ) {
  131. my $mount = "/cam$i";
  132. $mounts->add_factory(
  133. $mount,
  134. factory( cam( $VDEVICES[$i], $nextpayload++ ) )
  135. );
  136. push @mounts, $mount;
  137. }
  138. for my $i ( 0 .. $#ADEVICES ) {
  139. my $mount = "/mic$i";
  140. $mounts->add_factory(
  141. $mount,
  142. factory( mic( $ADEVICES[$i], $nextpayload++ ) )
  143. );
  144. push @mounts, $mount;
  145. }
  146. if ( @ADEVICES and @VDEVICES ) {
  147. my $mount = "/main";
  148. $mounts->add_factory(
  149. $mount,
  150. factory(
  151. mic( $ADEVICES[0], $#VDEVICES + 1 ),
  152. cam( $VDEVICES[0], 0 )
  153. )
  154. );
  155. push @mounts, $mount;
  156. }
  157. # don't need the ref to the mapper anymore
  158. undef $mounts;
  159. # attach the server to the default maincontext
  160. my $retval = $server->attach(undef);
  161. # start serving
  162. say "streams ready at the following URLs:";
  163. for (@mounts) {
  164. say "rtsp://$ADDRESS:$PORT$_";
  165. }
  166. $loop->run;