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