summaryrefslogtreecommitdiff
path: root/bin/capture+encode+serve
blob: e87310f6dec9e8e26995b5f26e637c968be2af42 (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 $SPEED_VPX = 15;
  40. my $SPEED_X264 = 'ultrafast';
  41. my $VBITRATE;
  42. # inspired by Apple HLS recommendations
  43. if ( $HEIGHT le 234 ) { $VBITRATE = 145000 }
  44. elsif ( $HEIGHT le 270 ) { $VBITRATE = 365000 }
  45. elsif ( $HEIGHT le 360 ) { $VBITRATE = 730000 }
  46. elsif ( $HEIGHT le 432 ) { $VBITRATE = 1100000 }
  47. elsif ( $HEIGHT le 540 ) { $VBITRATE = 2000000 }
  48. elsif ( $HEIGHT le 720 ) { $VBITRATE = 3000000 }
  49. my $VCAPS = "video/x-raw,height=$HEIGHT";
  50. my $ACAPS = "audio/x-raw,rate=$AUDIORATE,channels=2,depth=16";
  51. # * http://stackoverflow.com/a/42237307
  52. my $ABUFFERS = 20000;
  53. # * force threads using queues - see http://stackoverflow.com/a/30738533
  54. # * generous queue sizes inspired by https://wiki.xiph.org/GST_cookbook
  55. my $QUEUE = "queue max-size-bytes=100000000 max-size-time=0";
  56. my %PIPELINE = (
  57. AMR => {
  58. AENC => [ 'amrnbenc', $QUEUE, 'rtpamrpay' ],
  59. },
  60. H264 => {
  61. # * let x264 use low-latency sliced-threads (i.e. don't disable treads)
  62. VENC => [
  63. "x264enc speed-preset=$SPEED_H264 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\"",
  64. 'video/x-h264,profile=baseline',
  65. $QUEUE,
  66. 'rtph264pay',
  67. ],
  68. },
  69. OPUS => {
  70. AENC => [ 'opusenc', $QUEUE, 'rtpopuspay' ],
  71. },
  72. VP8 => {
  73. VENC => [
  74. "vp8enc threads=4 cpu-used=$SPEED_VPX deadline=1000000 end-usage=1 target-bitrate=$VBITRATE undershoot=95 keyframe-max-dist=999999 max-quantizer=56 deadline=5000 static-threshold=500",
  75. 'video/x-vp8',
  76. $QUEUE,
  77. 'rtpvp8pay',
  78. ],
  79. },
  80. RAW => {
  81. AENC => ['rtpL16pay'],
  82. VENC => ['rtpvrawpay'],
  83. },
  84. );
  85. our $nextpayload = 0;
  86. sub cam
  87. {
  88. my ( $device, $payload ) = @_;
  89. my $factory = Gst::RTSPMediaFactory->new();
  90. my $pipeline = join(
  91. ' ! ',
  92. ( "v4l2src device=$device",
  93. $QUEUE,
  94. 'videoconvert',
  95. $VCAPS,
  96. $QUEUE,
  97. @{ $PIPELINE{$VFORMAT}{'VENC'} },
  98. )
  99. );
  100. return "( $pipeline name=pay$payload )";
  101. }
  102. sub mic
  103. {
  104. my ( $device, $payload ) = @_;
  105. my $factory = Gst::RTSPMediaFactory->new();
  106. my $pipeline = join(
  107. ' ! ',
  108. ( "alsasrc device=$device buffer-time=$ABUFFERS",
  109. $QUEUE,
  110. 'audioconvert',
  111. $QUEUE,
  112. @{ $PIPELINE{$AFORMAT}{'AENC'} },
  113. )
  114. );
  115. return "( $pipeline name=pay$payload )";
  116. }
  117. sub factory
  118. {
  119. my @pipeline = @_;
  120. my $factory = Gst::RTSPMediaFactory->new();
  121. $factory->set_launch( join( ' ', @pipeline ) );
  122. $factory->set_shared(TRUE);
  123. #say "media ($device): " . $factory->get_launch();
  124. # $factory->set_latency(5);
  125. #say "latency ($device): " . $factory->get_latency();
  126. return $factory;
  127. }
  128. Gst::init( [ $0, @ARGV ] );
  129. my $loop = Glib::MainLoop->new( undef, FALSE );
  130. # create a server instance
  131. my $server = Gst::RTSPServer->new();
  132. $server->set_address($ADDRESS);
  133. $server->set_service($PORT);
  134. # get the mount points for this server, every server has a default
  135. # object that be used to map uri mount points to media factories
  136. my $mounts = $server->get_mount_points();
  137. # attach media to URIs
  138. my @mounts;
  139. for my $i ( 0 .. $#VDEVICES ) {
  140. my $mount = "/cam$i";
  141. $mounts->add_factory(
  142. $mount,
  143. factory( cam( $VDEVICES[$i], $nextpayload++ ) )
  144. );
  145. push @mounts, $mount;
  146. }
  147. for my $i ( 0 .. $#ADEVICES ) {
  148. my $mount = "/mic$i";
  149. $mounts->add_factory(
  150. $mount,
  151. factory( mic( $ADEVICES[$i], $nextpayload++ ) )
  152. );
  153. push @mounts, $mount;
  154. }
  155. if ( @ADEVICES and @VDEVICES ) {
  156. my $mount = "/main";
  157. $mounts->add_factory(
  158. $mount,
  159. factory(
  160. mic( $ADEVICES[0], $#VDEVICES + 1 ),
  161. cam( $VDEVICES[0], 0 )
  162. )
  163. );
  164. push @mounts, $mount;
  165. }
  166. # don't need the ref to the mapper anymore
  167. undef $mounts;
  168. # attach the server to the default maincontext
  169. my $retval = $server->attach(undef);
  170. # start serving
  171. say "streams ready at the following URLs:";
  172. for (@mounts) {
  173. say "rtsp://$ADDRESS:$PORT$_";
  174. }
  175. $loop->run;