Advertisement
rotrevrep

camerabin

Sep 15th, 2013
1,103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 3.02 KB | None | 0 0
  1. // -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
  2. /***
  3.   BEGIN LICENSE
  4.  
  5.   Copyright (C) 2011-2013 Mario Guerriero <mario@elementaryos.org>
  6.   This program is free software: you can redistribute it and/or modify it
  7.   under the terms of the GNU Lesser General Public License version 3, as
  8.   published    by the Free Software Foundation.
  9.  
  10.   This program is distributed in the hope that it will be useful, but
  11.   WITHOUT ANY WARRANTY; without even the implied warranties of
  12.   MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
  13.   PURPOSE.  See the GNU General Public License for more details.
  14.  
  15.   You should have received a copy of the GNU General Public License along
  16.   with this program.  If not, see <http://www.gnu.org/licenses>
  17.  
  18.   END LICENSE
  19. ***/
  20.  
  21. namespace Snap.Widgets {
  22.  
  23.     public class Camera : Gtk.DrawingArea {
  24.        
  25.         private uint* xid;
  26.         private Gst.Element? camerabin = null;
  27.        
  28.         public class Camera () {
  29.             this.realize.connect (() => {
  30.                 this.get_window ().ensure_native ();
  31.                 xid = (uint*)Gdk.X11Window.get_xid (this.get_window ());
  32.              });
  33.            
  34.             this.camerabin = Gst.ElementFactory.make ("camerabin", "camera");
  35.            
  36.             var bus = this.camerabin.get_bus ();
  37.             bus.add_watch (Priority.DEFAULT, on_bus_watch);
  38.             bus.set_sync_handler (on_bus_callback);
  39.  
  40.             var wrapper_src = Gst.ElementFactory.make ("wrappercamerabinsrc", null);
  41.             camerabin.set ("camera-source", wrapper_src);
  42.            
  43.             var v4l2_src = Gst.ElementFactory.make ("v4l2src", null);
  44.             wrapper_src.set ("video-source", v4l2_src);
  45.            
  46.             var gudev = new GUdev.Client ({ "video4linux", null });
  47.             var devices = gudev.query_by_subsystem ("video4linux");
  48.             var device_file = devices.data.get_device_file ();
  49.             v4l2_src.set ("device", device_file);
  50.            
  51.             this.play ();
  52.             this.show_all ();
  53.         }
  54.        
  55.         private bool on_bus_watch (Gst.Bus bus, Gst.Message message) {
  56.     if(Gst.Video.is_video_overlay_prepare_window_handle_message(message))
  57.                 (message.src as Gst.Video.Overlay).set_window_handle(xid);
  58.             return true;
  59.         }
  60.        
  61.         private Gst.BusSyncReply on_bus_callback (Gst.Bus bus, Gst.Message message) {
  62.             if (message.type != Gst.MessageType.ELEMENT)
  63.                 return Gst.BusSyncReply.PASS;
  64.                
  65.             if (!message.has_name ("prepare-window-handle"))
  66.                 return Gst.BusSyncReply.PASS;
  67.  
  68.             var overlay = message.src as Gst.Video.Overlay;
  69.             overlay.set_window_handle (Camera.xid);
  70.  
  71.             return Gst.BusSyncReply.DROP;
  72.         }
  73.        
  74.         public void play () {
  75.             this.camerabin.set_state (Gst.State.PLAYING);  
  76.         }
  77.        
  78.         public void stop () {
  79.             this.camerabin.set_state (Gst.State.NULL);  
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement