Guest User

Untitled

a guest
May 9th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 3.20 KB | None | 0 0
  1. public void record (string outputpath){
  2.     pipeline                   = new Pipeline ("screencast-pipe");
  3.     muxer                      = ElementFactory.make ("matroskamux", "mux");
  4.     dynamic Element sink       = ElementFactory.make ("filesink", "sink");
  5.    
  6.     //video bin
  7.     var videobin               = new Gst.Bin ("videobin");
  8.     dynamic Element vsrc       = ElementFactory.make ("ximagesrc", "vsrc");
  9.     dynamic Element colorspace = ElementFactory.make ("ffmpegcolorspace", "color");
  10.     dynamic Element vencode    = ElementFactory.make ("diracenc", "vencode");
  11.     videobin.add_many  (vsrc, colorspace, vencode);
  12.     videobin.link_many (vsrc, colorspace, vencode);
  13.    
  14.     //audio bin
  15.     var audiobin               = new Gst.Bin ("audiobin");
  16.     dynamic Element asrc       = ElementFactory.make ("autoaudiosrc", "asrc");
  17.     dynamic Element aconv      = ElementFactory.make ("audioconvert", "aconv");
  18.     dynamic Element aresample  = ElementFactory.make ("audioresample", "aresample");
  19.     dynamic Element audiorate  = ElementFactory.make ("audiorate", "arate");
  20.     dynamic Element aencode    = ElementFactory.make ("vorbisenc", "aencode");
  21.     audiobin.add_many  (asrc, aconv, aresample, audiorate, aencode);
  22.     audiobin.link_many (asrc, aconv, aresample, audiorate, aencode);
  23.    
  24.     //configure
  25.     sink.set ("location", outputpath);
  26.    
  27.     if (pipeline==null||muxer==null||sink==null||videobin==null||vsrc==null||colorspace==null||
  28.         vencode==null||audiobin==null||asrc==null||aconv==null||aresample==null||audiorate==null||
  29.         aencode==null){
  30.         stderr.printf ("Error: Elements weren't made correctly!\n");
  31.     }
  32.    
  33.     pipeline.add_many (videobin, muxer, sink);//audiobin
  34.     //videobin.get_static_pad ("src_%d").link(mux.get_request_pad ("video_%d"));
  35.     videobin.add_pad (new GhostPad ("src", muxer.get_request_pad ("video_%d")));
  36.     audiobin.add_pad (new GhostPad ("src", muxer.get_request_pad ("audio_%d")));
  37.     muxer.link (sink);
  38.    
  39.     pipeline.get_bus ().add_watch (bus_message_cb);
  40.     pipeline.set_state (State.PLAYING);
  41.     Timeout.add (500, (GLib.SourceFunc) update_slide);
  42. }
  43.  
  44. private bool bus_message_cb (Gst.Bus bus, Message msg){
  45.     switch (msg.type){
  46.         case Gst.MessageType.ERROR:
  47.             GLib.Error err; string debug;
  48.             msg.parse_error (out err, out debug);
  49.             print ("Error: %s\n", err.message);
  50.             pipeline.set_state (State.NULL);
  51.             break;
  52.         case Gst.MessageType.EOS:
  53.             print ("Finished, why??\n");
  54.             pipeline.set_state (State.NULL);
  55.             break;
  56.         case Gst.MessageType.STATE_CHANGED:
  57.             Gst.State oldstate; Gst.State newstate; Gst.State pending;
  58.             msg.parse_state_changed (out oldstate, out newstate, out pending);
  59.             print ("state changed: %s->%s:%s\n", oldstate.to_string (), newstate.to_string (), pending.to_string ());
  60.             break;
  61.         default:
  62.             break;
  63.     }
  64.     return true;
  65. }
  66.  
  67. private void pad_added_cb (Pad pad){
  68.     var sinkpad = muxer.get_request_pad ("video_%d");
  69.     if (sinkpad == null){
  70.         Caps caps = pad.get_caps_reffed ();
  71.         Signal.emit_by_name (muxer, "request-pad", caps, out sinkpad);
  72.     }
  73.     if (sinkpad == null){
  74.         stderr.printf ("Couldn't get an encoding channel for pad ...\n");
  75.     }
  76.     var pad_link_ok = (pad.link (sinkpad) == PadLinkReturn.OK);
  77.     if (!pad_link_ok) {
  78.         warning ("Failed to link pad '%s' to '%s'", pad.name, sinkpad.name);
  79.     }
  80.     return;
  81. }
Add Comment
Please, Sign In to add comment