File: gstvideo.lua

package info (click to toggle)
lua-lgi 0.9.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,388 kB
  • sloc: ansic: 5,082; makefile: 169; sh: 31
file content (120 lines) | stat: -rwxr-xr-x 3,219 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#! /usr/bin/env lua

--
-- Sample GStreamer application, based on public Vala GStreamer Video
-- Example (http://live.gnome.org/Vala/GStreamerSample)
--

local lgi  = require 'lgi'
local GLib = lgi.GLib
local Gtk  = lgi.Gtk
local GdkX11 = lgi.GdkX11
local Gst  = lgi.Gst
if tonumber(Gst._version) >= 1.0 then
   local GstVideo = lgi.GstVideo
end

local app = Gtk.Application { application_id = 'org.lgi.samples.gstvideo' }

local window = Gtk.Window {
   title = "LGI Based Video Player",
   Gtk.Box {
      orientation = 'VERTICAL',
      Gtk.DrawingArea {
	 id = 'video',
	 expand = true,
	 width = 300,
	 height = 150,
      },
      Gtk.ButtonBox {
	 orientation = 'HORIZONTAL',
	 Gtk.Button {
	    id = 'play',
	    use_stock = true,
	    label = Gtk.STOCK_MEDIA_PLAY,
	 },
	 Gtk.Button {
	    id = 'stop',
	    use_stock = true,
	    sensitive = false,
	    label = Gtk.STOCK_MEDIA_STOP,
	 },
	 Gtk.Button {
	    id = 'quit',
	    use_stock = true,
	    label = Gtk.STOCK_QUIT,
	 },
      },
   }
}

function window.child.quit:on_clicked()
   window:destroy()
end

local pipeline = Gst.Pipeline.new('mypipeline')
local src = Gst.ElementFactory.make('autovideosrc', 'videosrc')
local colorspace = Gst.ElementFactory.make('videoconvert', 'colorspace')
                or Gst.ElementFactory.make('ffmpegcolorspace', 'colorspace')
local scale = Gst.ElementFactory.make('videoscale', 'scale')
local rate = Gst.ElementFactory.make('videorate', 'rate')
local sink = Gst.ElementFactory.make('xvimagesink', 'sink')

pipeline:add_many(src, colorspace, scale, rate, sink)
src:link_many(colorspace, scale, rate, sink)

function window.child.play:on_clicked()
   pipeline.state = 'PLAYING'
end

function window.child.stop:on_clicked()
   pipeline.state = 'PAUSED'
end

local function bus_callback(bus, message)
   if message.type.ERROR then
      print('Error:', message:parse_error().message)
      Gtk.main_quit()
   end
   if message.type.EOS then
      print 'end of stream'
   end
   if message.type.STATE_CHANGED then
      local old, new, pending = message:parse_state_changed()
      print(string.format('state changed: %s->%s:%s', old, new, pending))

      -- Set up sensitive state on buttons according to current state.
      -- Note that this is forwarded to mainloop, because bus callback
      -- can be called in some side thread and Gtk might not like to
      -- be controlled from other than main thread on some platforms.
      GLib.idle_add(GLib.PRIORITY_DEFAULT, function()
	 window.child.play.sensitive = (new ~= 'PLAYING')
	 window.child.stop.sensitive = (new == 'PLAYING')
	 return GLib.SOURCE_REMOVE
      end)
   end
   if message.type.TAG then
      message:parse_tag():foreach(
	 function(list, tag)
	    print(('tag: %s = %s'):format(tag, tostring(list:get(tag))))
	 end)
   end
   return true
end

function window.child.video:on_realize()
   -- Retarget video output to the drawingarea.
   sink:set_window_handle(self.window:get_xid())
end


function app:on_activate()
   window.application = app
   pipeline.bus:add_watch(GLib.PRIORITY_DEFAULT, bus_callback)
   window:show_all()
end

app:run { arg[0], ... }

-- Must always set the pipeline to NULL before disposing it
pipeline.state = 'NULL'