rotrevrep

Play custom input stream with Gtk 4

Mar 13th, 2021 (edited)
955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 1.79 KB | None | 0 0
  1. // compile with : valac test-gtk4.vala --pkg gio-unix-2.0 --pkg gtk4
  2.  
  3. public class Pipe : IOStream {
  4.     int fds[2];
  5.     OutputStream ostream;
  6.     InputStream istream;
  7.    
  8.     construct {
  9.         Unix.open_pipe (fds, 0);
  10.         input_fd = fds[0]; output_fd = fds[1];
  11.         istream = new UnixInputStream (fds[0], false);
  12.         ostream = new UnixOutputStream (fds[1], false);
  13.     }
  14.    
  15.     public int input_fd { get; private set; }
  16.    
  17.     public int output_fd { get; private set; }
  18.    
  19.     public override InputStream input_stream {
  20.         get {
  21.             return istream;
  22.         }
  23.     }
  24.    
  25.     public override OutputStream output_stream {
  26.         get {
  27.             return ostream;
  28.         }
  29.     }
  30. }
  31.  
  32. public class VideoWriter : Pipe {
  33.     Gtk.Video video;
  34.    
  35.     public VideoWriter (Gtk.Video video) {
  36.         this.video = video;
  37.     }
  38.    
  39.     async void write_to_pipe (InputStream stream) {
  40.         new Thread<void*>("write", () => {
  41.             output_stream.splice (stream, OutputStreamSpliceFlags.NONE);
  42.             Idle.add (write_to_pipe.callback);
  43.             return null;
  44.         });
  45.         yield;
  46.     }
  47.    
  48.     public void set_input_stream (InputStream stream) {
  49.         write_to_pipe.begin (stream, (obj, res) => {
  50.            
  51.         });
  52.         video.set_file (File.new_for_path ("/dev/fd/" + input_fd.to_string()));
  53.     }
  54. }
  55.  
  56. public static void main (string[] args) {
  57.     Gtk.init();
  58.    
  59.     var proc = new Subprocess (SubprocessFlags.STDOUT_PIPE | SubprocessFlags.STDIN_PIPE | SubprocessFlags.STDERR_PIPE, "streamlink", "-O", "https://www.france.tv/france-2/direct.html", "best");
  60.     var loop = new MainLoop();
  61.     var win = new Gtk.Window();
  62.     var vid = new Gtk.Video();
  63.     var writer = new VideoWriter (vid);
  64.    
  65.     vid.set_autoplay (true);
  66.     vid.realize.connect (() => {
  67.         writer.set_input_stream (proc.get_stdout_pipe());
  68.     });
  69.    
  70.     win.child = vid;
  71.     (win as Gtk.Widget).unrealize.connect (loop.quit);
  72.     win.set_size_request (400, 300);
  73.     win.show();
  74.    
  75.     loop.run();
  76. }
  77.  
Add Comment
Please, Sign In to add comment