Created
September 9, 2016 20:43
-
-
Save JuniorPolegato/62d9d1a29a9438bc13a441cd54cb67a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import gi | |
gi.require_version('Gst', '1.0') | |
gi.require_version('GstVideo', '1.0') | |
gi.require_version('Gtk', '3.0') | |
from gi.repository import GObject, Gst, Gtk | |
# Needed for window.get_xid(), xvimagesink.set_window_handle(), respectively: | |
from gi.repository import GdkX11, GstVideo | |
GObject.threads_init() | |
Gst.init(None) | |
uri = ('playbin' | |
' uri=rtsp://admin:admin@192.168.1.%i:554/cam/realmonitor?channel=%i') | |
class CamMon(object): | |
def __init__(self): | |
self.window = Gtk.Window() | |
self.window.set_default_size(704/2*3, 480) | |
self.window.connect('destroy', self.quit) | |
self.table = Gtk.Table() | |
self.window.add(self.table) | |
self.draw = [] | |
self.pipeline = [] | |
self.bus = [] | |
ip = 226 | |
for line in range(4): | |
for column in range(6): | |
d = Gtk.DrawingArea() | |
self.draw.append(d) | |
self.table.attach(d, column, column + 1, line, line + 1) | |
channel = (line * 6 + column + 1) | |
if channel > 12: | |
channel -= 12 | |
ip = 227 | |
pipeline = Gst.parse_launch(uri % (ip, channel)) | |
bus = pipeline.get_bus() | |
bus.add_signal_watch() | |
bus.connect('message::error', self.on_error) | |
bus.enable_sync_message_emission() | |
bus.connect('sync-message::element', self.on_sync_message) | |
self.pipeline.append(pipeline) | |
self.bus.append(bus) | |
def run(self): | |
self.window.show_all() | |
# You need to get the XID after window.show_all(). You shouldn't get it | |
# in the on_sync_message() handler because threading issues will cause | |
# segfaults there. | |
self.xid = [] | |
for i in range(24): | |
self.xid.append(self.draw[i].get_property('window').get_xid()) | |
self.pipeline[0].set_state(Gst.State.PLAYING) | |
Gtk.main() | |
def quit(self, window): | |
for i in range(24): | |
self.pipeline[i].set_state(Gst.State.NULL) | |
Gtk.main_quit() | |
def on_sync_message(self, bus, msg): | |
i = self.bus.index(bus) | |
if msg.get_structure().get_name() == 'prepare-window-handle': | |
print('prepare-window-handle %i' % i) | |
msg.src.set_property('force-aspect-ratio', True) | |
msg.src.set_window_handle(self.xid[i]) | |
if i < 24: | |
self.pipeline[i+1].set_state(Gst.State.PLAYING) | |
def on_error(self, bus, msg): | |
i = self.bus.index(bus) | |
print('on_error %i:' % i, msg.parse_error()) | |
monitor = CamMon() | |
monitor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment