Where are Gstreamer bus log messages? - python

I am trying to stream a .mp4 to a RTSP server using Gstreamer in python
import sys
import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')
gi.require_version('GstRtsp', '1.0')
from gi.repository import Gst, GstRtspServer, GObject, GLib, GstRtsp
loop = GLib.MainLoop()
Gst.init(None)
file_path = "test.mp4"
class TestRtspMediaFactory(GstRtspServer.RTSPMediaFactory):
def __init__(self):
GstRtspServer.RTSPMediaFactory.__init__(self)
def do_create_element(self, url):
src_demux = f"filesrc location={file_path} ! qtdemux name=demux"
h264_transcode = "demux.video_0"
pipeline = "{0} {1} ! queue ! rtph264pay name=pay0 config-interval=1 pt=96".format(src_demux, h264_transcode)
print ("Element created: " + pipeline)
self._pipeline = Gst.parse_launch(pipeline)
def bus_handler(bus, message):
print(message)
self.bus = self._pipeline.get_bus()
self.bus.connect('message', bus_handler)
self.bus.add_signal_watch_full(1)
return self._pipeline
class GstreamerRtspServer():
def __init__(self):
self.rtspServer = GstRtspServer.RTSPServer()
factory = TestRtspMediaFactory()
factory.set_shared(True)
mountPoints = self.rtspServer.get_mount_points()
self.address = '127.0.0.1' #my RPi's local IP
self.port = '8553'
self.rtspServer.set_address(self.address)
self.rtspServer.set_service(self.port)
urlstr = "/user=&password=.sdp"
url = GstRtsp.RTSPUrl.parse(urlstr)
mountPoints.add_factory(urlstr, factory)
self.rtspServer.attach(None)
if __name__ == '__main__':
s = GstreamerRtspServer()
loop.run()
However I am trying to understand how to use Gstreamer bus to log messages like eos or errors and warnings but I don't see any, even when I send eos events and the streaming effectively stops
s.rtspServer._pipeline._end_stream_event.set()
s.rtspServer._pipeline.send_event(Gst.Event.new_eos())
Am I using it properly? If not, what can I fix to properly log bus messages?

Following solution is based on this accepted but somehow incomplete answer.
I found out the way that does not require "manual" creation of pipeline elements but instead it keeps (in this scenario) convenient Gst.parse_launch(pipelineCmd) method and extends Gst.Bin to enable message debugging.
Here is full example source code (check out commented lines for some explanations):
#!/usr/bin/env python
import sys
import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')
from gi.repository import Gst, GstRtspServer, GObject, GLib
Gst.init(None)
loop = GLib.MainLoop()
# extended Gst.Bin that overrides do_handle_message and adds debugging
class ExtendedBin(Gst.Bin):
def do_handle_message(self,message):
if message.type == Gst.MessageType.ERROR:
error, debug = message.parse_error()
print("ERROR:", message.src.get_name(), ":", error.message)
if debug:
print ("Debug info: " + debug)
elif message.type == Gst.MessageType.EOS:
print ("End of stream")
elif message.type == Gst.MessageType.STATE_CHANGED:
oldState, newState, pendingState = message.parse_state_changed()
print ("State changed -> old:{}, new:{}, pending:{}".format(oldState,newState,pendingState))
else :
print("Some other message type: " + str(message.type))
#call base handler to enable message propagation
Gst.Bin.do_handle_message(self,message)
class TestRtspMediaFactory(GstRtspServer.RTSPMediaFactory):
def __init__(self):
GstRtspServer.RTSPMediaFactory.__init__(self)
def do_create_element(self, url):
#set mp4 file path to filesrc's location property
src_demux = "filesrc location=/path/to/dir/test.mp4 ! qtdemux name=demux"
h264_transcode = "demux.video_0"
#uncomment following line if video transcoding is necessary
#h264_transcode = "demux.video_0 ! decodebin ! queue ! x264enc"
pipelineCmd = "{0} {1} ! queue ! rtph264pay name=pay0 config-interval=1 pt=96".format(src_demux, h264_transcode)
self.pipeline = Gst.parse_launch(pipelineCmd)
print ("Pipeline created: " + pipelineCmd)
# creates extended Gst.Bin with message debugging enabled
extendedBin = ExtendedBin("extendedBin")
# Gst.pipeline inherits Gst.Bin and Gst.Element so following is possible
extendedBin.add(self.pipeline)
# creates new Pipeline and adds extended Bin to it
self.extendedPipeline = Gst.Pipeline.new("extendedPipeline")
self.extendedPipeline.add(extendedBin)
return self.extendedPipeline
class GstreamerRtspServer(GstRtspServer.RTSPServer):
def __init__(self):
self.rtspServer = GstRtspServer.RTSPServer()
self.factory = TestRtspMediaFactory()
self.factory.set_shared(True)
mountPoints = self.rtspServer.get_mount_points()
mountPoints.add_factory("/stream", self.factory)
self.rtspServer.attach(None)
print ("RTSP server is ready")
if __name__ == '__main__':
s = GstreamerRtspServer()
loop.run()
Please note that Gst.Pipeline actually inherists/extends Gst.Bin (and Gst.Element) so it is possible (no matter how strange it sounds) to add pipeline to bin.
This little "trick" saves time for us "lazy" programmers to keep using parsing of command line syntax to create pipeline elements.
In some more complex scenarios, where parsing of command line syntax is not applicable, solution would be following:
create ExtendedBin,
"manually" create elements with Gst.ElementFactory.make method (and set necessary properties)
add created elements to ExtendedBean
link elements
create new pipeline and add bin to it
use pipeline where it is needed.

Related

GStreamer, Tee in RTSP server

New to Gstreamer, trying to create an RTSP server that consumes a source once per output stream. Code follows. In the factory do_create_element(), if I use the source_pipeline as a return value, I am able to connect to the server and consume the stream. However, as expected, if I connect multiple clients, the server crashes with an error saying the src pad is already connected. My idea was to use the tee element, add a queue after it, and wrap the queue in a pipeline (since I need a GstBin element to give back from the factory). However, this does not work. When I connect a first client to server, nothing happens server-side (as-in, no crashes. The function is called and exits successfully). However, client side, no stream is read, with a 'SDP contains no stream error' (see the console output after the code).
I tried connecting the tee manually to the queue (line left in comment) instead of using the pipeline to do it, but nothing changes. I suspect the queue needs something to consume it, but I'm not sure how/what to use (or if that's the actual issue).
Here is a minimal example:
import gi
gi.require_version("Gst", "1.0")
gi.require_version("GstRtspServer", "1.0")
# noinspection PyUnresolvedReferences
from gi.repository import Gst, GstRtspServer, GObject
def create_source():
s_src = "videotestsrc ! video/x-raw,rate=30,width=320,height=240,format=I420"
s_h264 = "x264enc tune=zerolatency"
pipeline_str = "( {s_src} ! queue max-size-buffers=1000 name=q_enc ! {s_h264} ! rtph264pay name=pay0 pt=96 )".format(
**locals()
)
return Gst.parse_launch(pipeline_str)
def _main():
GObject.threads_init()
Gst.init(None)
source = create_source()
tee = Gst.ElementFactory.make("tee", "tee")
source_pipeline = Gst.Pipeline()
source_pipeline.add(source)
source_pipeline.add(tee)
source_pipeline.set_state(Gst.State.PLAYING)
class Factory(GstRtspServer.RTSPMediaFactoryURI):
def __init__(self):
super(Factory, self).__init__()
self._n = 0
def do_create_element(self, url): # -> GstBin (Pipeline)
print("in [Factory] do_create_element")
source_pipeline.set_state(Gst.State.PAUSED)
stream_pipeline = Gst.Pipeline()
# Create a new queue to buffer the tee's output
q = Gst.ElementFactory.make("queue")
stream_pipeline.add(q)
source_pipeline.add(tee)
source_pipeline.add(stream_pipeline)
# tee.link(q)
stream_pipeline.set_state(Gst.State.PLAYING)
source_pipeline.set_state(Gst.State.PLAYING)
return stream_pipeline
factory = Factory()
factory.set_shared(True)
server = GstRtspServer.RTSPServer()
server.get_mount_points().add_factory("/endpoint", factory)
server.attach(None)
loop = GObject.MainLoop()
loop.run()
if __name__ == "__main__":
_main()
Here is the shell output when connecting:
> gst-launch-1.0 rtspsrc location=rtsp://127.0.0.1:8554/endpoint ! rtph264depay ! h264parse ! mp4mux ! filesink location=file.mp4
Setting pipeline to PAUSED ...
Pipeline is live and does not need PREROLL ...
Progress: (open) Opening Stream
Pipeline is PREROLLED ...
Prerolled, waiting for progress to finish...
Progress: (connect) Connecting to rtsp://127.0.0.1:8554/endpoint
Progress: (open) Retrieving server options
Progress: (open) Retrieving media info
ERROR: from element /GstPipeline:pipeline0/GstRTSPSrc:rtspsrc0: Could not get/set settings from/on resource.
Additional debug info:
../gst/rtsp/gstrtspsrc.c(7637): gst_rtspsrc_setup_streams_start (): /GstPipeline:pipeline0/GstRTSPSrc:rtspsrc0:
SDP contains no streams
ERROR: pipeline doesn't want to preroll.
Setting pipeline to NULL ...
Freeing pipeline ...
Thanks ahead!

How to use gst along with pyqt to stream video on pyqt widget

Am using gst along with my pyqt. I want to display the video stream in my widget. While doing so my application starts streaming the video and then crashes. What am I doing wrong ?
Camera Code
from PyQt4 import QtCore
import gst
class camThread(QtCore.QThread):
updateImage = QtCore.pyqtSignal(str)
flag = None
def __init__(self,windowId):
QtCore.QThread.__init__(self)
self.windowId =windowId
self.player = gst.parse_launch("udpsrc port=5000 ! application/x-rtp, encoding-name=H264, payload=96 ! rtph264depay ! h264parse ! ffdec_h264 ! autovideosink")
bus = self.player.get_bus()
bus.add_signal_watch()
bus.enable_sync_message_emission()
bus.connect("sync-message::element", self.on_sync_message)
self.bus = bus
def on_sync_message(self, bus, message):
print "akash 123"
if message.structure is None:
return
message_name = message.structure.get_name()
if message_name == "prepare-xwindow-id":
win_id = self.windowId
assert win_id
imagesink = message.src
imagesink.set_property("force-aspect-ratio", True)
imagesink.set_xwindow_id(win_id)
def run(self):
print "akash"
self.player.set_state(gst.STATE_PLAYING)
msg = self.bus.timed_pop_filtered(gst.CLOCK_TIME_NONE,
gst.MESSAGE_ERROR | gst.MESSAGE_EOS)
self.flag = True
while(True):
if(self.flag==False):
break
def quit(self):
self.flag = false
self.player.set_state(gst.STATE_NULL)
#self.cap.release()
Calling code
def stopCam(self):
if(self.cam!=None):
self.cam.quit()
self.cam = None
def startCam(self):
if(self.cam==None):
self.cam = camThread(self.pic.winId())
self.cam.start()
elif(self.cam.isRunning()):
pass
What am I doing wrong ? Here is the entire code on paste bin
PasteBin file 1
PasteBin file 2
Edit:
I opened the python in debugger. The application becomes unresponsive/fails, when I start the gst playing i.e. it fails after gst bus timed pop.
One of the possible reason I could see was that a thread relating to the video streaming stops or exits after it is started in the application. After which the application goes black/unresponsive/crashes.
I think you forgot to initialise the threading system.
import gobject
gobject.threads_init()
import gst
Your run() function also does not need the while loop and the flag self.flag. According to the documentation here, the call to timed_pop_filtered(gst.CLOCK_TIME_NONE, ...) will block until it receives the specified message. When you click on the "Stop Cam" button, you terminate the playback. The flag is never used.
I also had to change self.cam = camThread(self.pic.winId()) to self.cam = camThread(int(self.pic.winId())) and comment out imagesink.set_property("force-aspect-ratio", True)
-- I am using Mac OS X.
Hope this helps.

Use dbus to just send a message in Python

I have 2 Python programs. I just want to send a message (a long string) from the one to the other, and I want use dbus.
Now, is there an easy way to do this?
For example, if the message is very small, I have partially solved the problem putting the message in the path. But then I had to use the external program dbus-send:
Server (python):
import dbus,gtk
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
def msg_handler(*args,**keywords):
try:
msg=str(keywords['path'][8:])
#...do smthg with msg
print msg
except:
pass
bus.add_signal_receiver(handler_function=msg_handler, dbus_interface='my.app', path_keyword='path')
gtk.main()
Client (bash:( ):
dbus-send --session /my/app/this_is_the_message my.app.App
Is there a way to write the client in Python? or also, is there a better way to achieve the same result?
Here is an example that uses interface method calls:
Server:
#!/usr/bin/python3
#Python DBUS Test Server
#runs until the Quit() method is called via DBUS
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
class MyDBUSService(dbus.service.Object):
def __init__(self):
bus_name = dbus.service.BusName('org.my.test', bus=dbus.SessionBus())
dbus.service.Object.__init__(self, bus_name, '/org/my/test')
#dbus.service.method('org.my.test')
def hello(self):
"""returns the string 'Hello, World!'"""
return "Hello, World!"
#dbus.service.method('org.my.test')
def string_echo(self, s):
"""returns whatever is passed to it"""
return s
#dbus.service.method('org.my.test')
def Quit(self):
"""removes this object from the DBUS connection and exits"""
self.remove_from_connection()
Gtk.main_quit()
return
DBusGMainLoop(set_as_default=True)
myservice = MyDBUSService()
Gtk.main()
Client:
#!/usr/bin/python3
#Python script to call the methods of the DBUS Test Server
import dbus
#get the session bus
bus = dbus.SessionBus()
#get the object
the_object = bus.get_object("org.my.test", "/org/my/test")
#get the interface
the_interface = dbus.Interface(the_object, "org.my.test")
#call the methods and print the results
reply = the_interface.hello()
print(reply)
reply = the_interface.string_echo("test 123")
print(reply)
the_interface.Quit()
Output:
$ ./dbus-test-server.py &
[1] 26241
$ ./dbus-server-tester.py
Hello, World!
test 123
Hope that helps.

python gstreamer play multiple video streams

I'm participating in an art project which includes remotely playing videos. I've implemented a simple python application with a HTTP server and a gstreamer video player. I'm able to catch a http request and change the video which is currently playing, but I'd like to just add the new video in the same window and continue playing two videos simultaniously.
I've used playbin2 to play videos, but I think it can only play one uri at the time. I've tried to find other solutions which could play several videos at the same time, but no use...
Could anyone please post a simple example of playing multiple streams at the same time, or give me some pointers to the documentation or other resources??
Thanks in advance!!
PS. Here's the code I wrote: the VideoPlayer class initializes the stream and the the playCurrent function switches the currently played video - I'd like that function just to add the new video to the stream.
#!/usr/bin/python
import threading
import time
import BaseHTTPServer
from BaseHTTPServer import HTTPServer
from urlparse import urlparse, parse_qs
from os import path
import gst
import gtk
HOST_NAME = 'localhost' # !!!REMEMBER TO CHANGE THIS!!!
PORT_NUMBER = 9000 # Maybe set this to 9000.
#################################################################
# VIDEO DICTIONARY
# Manages the video database
#################################################################
# VideoDictionary class
#################################################################
# This class allows to access the video database
# used by the video player - for best performance, it's a native
# python dictionary
class VideoDictionary():
# declaring filenames
filename = path.join(path.dirname(path.abspath(__file__)), 'large.mp4')
filename_02 = path.join(path.dirname(path.abspath(__file__)), '01.avi')
# declaring uris
uri = 'file://' + filename
uri_02 = 'file://' + filename_02
# combining it all into a dictionary
videoDict = {}
videoDict["01"] = uri
videoDict["02"] = uri_02
# setting the current video
currentVideo = "01"
#################################################################
# VIDEO DICTIONARY END
#################################################################
#################################################################
# VIDEO PLAYER
# Manages all the video playing
#################################################################
# VideoPlayer class
#################################################################
# This class initializes the GST pipe context and it
# handles different events related to video stream playing
class VideoPlayer(object, VideoDictionary):
VideoDictionary = ""
def __init__(self, VideoDictionary):
self.VideoDictionary = VideoDictionary
self.window = gtk.Window()
self.window.connect('destroy', self.quit)
self.window.set_default_size(1024, 768)
self.drawingarea = gtk.DrawingArea()
self.window.add(self.drawingarea)
# Create GStreamer pipeline
self.pipeline = gst.Pipeline()
# Create bus to get events from GStreamer pipeline
self.bus = self.pipeline.get_bus()
# This is needed to make the video output in our DrawingArea:
self.bus.enable_sync_message_emission()
self.bus.connect('sync-message::element', self.on_sync_message)
# Create GStreamer elements
self.playbin = gst.element_factory_make('playbin2')
# Add playbin2 to the pipeline
self.pipeline.add(self.playbin)
self.window.show_all()
self.xid = self.drawingarea.window.xid
print('DEBUG INFO: player initialization finished')
def playCurrent(self):
print('DEBUG INFO: getting running video ')
print(self.VideoDictionary.currentVideo)
self.pipeline.set_state(gst.STATE_READY)
self.playbin.set_property('uri', self.VideoDictionary.videoDict[self.VideoDictionary.currentVideo])
self.pipeline.set_state(gst.STATE_PLAYING)
def quit(self, window):
print('DEBUG INFO: quitting player')
self.pipeline.set_state(gst.STATE_NULL)
gtk.main_quit()
def on_sync_message(self, bus, msg):
if msg.structure.get_name() == 'prepare-xwindow-id':
msg.src.set_property('force-aspect-ratio', True)
msg.src.set_xwindow_id(self.xid)
def on_eos(self, bus, msg):
print('DEBUG INFO: EOS detected')
print('on_eos(): seeking to start of video')
self.pipeline.seek_simple(
gst.FORMAT_TIME,
gst.SEEK_FLAG_FLUSH | gst.SEEK_FLAG_KEY_UNIT,
0L
)
def on_error(self, bus, msg):
print('DEBUG INFO: error detected')
print('on_error():', msg.parse_error())
#################################################################
# VIDEO PLAYER END
#################################################################
#################################################################
# HTTP SERVER
# implements the http listener in a separate thread
# the listener plays the videos depending on the
# received parameters in the GET request
#################################################################
# HttpHandler class
#################################################################
# uses global variables to operate videos
class HttpHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
# initialize the currently played video
global VideoDictionary
print('DEBUG INFO: GET running playCurrent')
if VideoDictionary.currentVideo == "01":
VideoDictionary.currentVideo = "02"
else:
VideoDictionary.currentVideo = "01"
# play the video we have just set
global player
player.playCurrent()
# HttpThread class
#################################################################
# initializes the http listener in a separate thread
class HttpThread (threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
gtk.gdk.threads_enter()
server_class = BaseHTTPServer.HTTPServer
httpd = server_class((HOST_NAME, PORT_NUMBER), HttpHandler)
print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)
gtk.gdk.threads_leave()
return
#################################################################
# HTTP SERVER END
#################################################################
if __name__ == '__main__':
VideoDictionary = VideoDictionary()
player = VideoPlayer(VideoDictionary)
gtk.gdk.threads_init()
thread2 = HttpThread()
thread2.run()
gtk.gdk.threads_enter()
gtk.main()
gtk.gdk.threads_leave()
Here is a simple example of code that plays multiple video streams at the same time.
It works with Python 2 and 3 and it uses the standard Python GUI (Tk) and Gstreamer 1.0. It should therefore be portable, but I only tested it under Ubuntu 16.04.
(The ffmpeg fork libav created problems under Ubuntu 14.04, which seem to be solved under 16.04. Note that you need the package gstreamer1.0-libav in addition to gstreamer1.0-plugins-*.)
The code is configured to create eight frames in a column, and to associate a Gstreamer player with each of them. You are required to give a list of (up to eight) valid local video file names as arguments to the file into which you saved it (say multivid.py), like this:
$ python3 multivid.py video1.webm video2.mp4
The sound channels are simply mixed together. You probably want to change this.
My solution does not address remote playing, but you have already solved that part.
I previously posted the same code in an answer to another question on video files in tkinter, where the question did not ask for simultaneous streams. It is therefore more appropriate here.
import sys
import os
if sys.version_info[0] < 3:
import Tkinter as tkinter
else:
import tkinter
import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst, GObject
# Needed for set_window_handle():
gi.require_version('GstVideo', '1.0')
from gi.repository import GstVideo
def set_frame_handle(bus, message, frame_id):
if not message.get_structure() is None:
if message.get_structure().get_name() == 'prepare-window-handle':
display_frame = message.src
display_frame.set_property('force-aspect-ratio', True)
display_frame.set_window_handle(frame_id)
NUMBER_OF_FRAMES = 8 # with more frames than arguments, videos are repeated
relative_height = 1 / float(NUMBER_OF_FRAMES)
# Only argument number checked, not validity.
number_of_file_names_given = len(sys.argv) - 1
if number_of_file_names_given < 1:
print('Give at least one video file name.')
sys.exit()
if number_of_file_names_given < NUMBER_OF_FRAMES:
print('Up to', NUMBER_OF_FRAMES, 'video file names can be given.')
file_names = list()
for index in range(number_of_file_names_given):
file_names.append(sys.argv[index + 1])
window = tkinter.Tk()
window.title("Multiple videos in a column using Tk and GStreamer 1.0")
window.geometry('480x960')
Gst.init(None)
GObject.threads_init()
for number in range(NUMBER_OF_FRAMES):
display_frame = tkinter.Frame(window, bg='')
relative_y = number * relative_height
display_frame.place(relx = 0, rely = relative_y,
anchor = tkinter.NW, relwidth = 1, relheight = relative_height)
frame_id = display_frame.winfo_id()
player = Gst.ElementFactory.make('playbin', None)
fullname = os.path.abspath(file_names[number % len(file_names)])
player.set_property('uri', 'file://%s' % fullname)
player.set_state(Gst.State.PLAYING)
bus = player.get_bus()
bus.enable_sync_message_emission()
bus.connect('sync-message::element', set_frame_handle, frame_id)
window.mainloop()
If you save the handles to the players (say in player_list), you can later change the uri that it is playing in one of them like this:
player_list[index].set_state(Gst.State.NULL)
player_list[index].set_property('uri', 'file://%s' % fileName)
player_list[index].set_state(Gst.State.PLAYING)

python: simple dbus example- os.fork() in service routine?

I am trying to write dbus server where I want to run some external shell program (grep here) to do the job.
when I do:
prompt$ server.py
then:
prompt$ client.py # works fine, ie. runs grep command in child process.
prompt$ client.py # ..., but second invocation produces following error message:
DBusException: org.freedesktop.DBus.Error.ServiceUnknown: The name org.example.ExampleService was not provided by any .service files
I am stuck. Are You able to help me?
here is server.py (client.py thereafter):
import gtk, glib
import os
import dbus
import dbus.service
import dbus.mainloop.glib
import subprocess
messages_queue=list()
grep_pid=0
def queue_msg(message):
global messages_queue
messages_queue.append(message)
return
def dequeue_msg():
global messages_queue,grep_pid
if grep_pid != 0:
try:
pid=os.waitpid(grep_pid,os.P_NOWAIT)
except:
return True
if pid[0] == 0:
return True
grep_pid=0
if len(messages_queue) == 0:
return True
else:
tekst=messages_queue.pop(0)
cmd="grep 'pp'"
print cmd
#works fine, when I do return here
#return True
grep_pid=os.fork()
if grep_pid != 0:
return True
os.setpgid(0,0)
pop=subprocess.Popen(cmd,shell=True,stdin=subprocess.PIPE)
pop.stdin.write(tekst)
pop.stdin.close()
pop.wait()
exit(0)
class DemoException(dbus.DBusException):
_dbus_error_name = 'org.example.Exception'
class MyServer(dbus.service.Object):
#dbus.service.method("org.example.ExampleInterface",
in_signature='', out_signature='')
def QueueMsg(self):
queue_msg("ppppp")
#dbus.service.method("org.example.ExampleInterface",
in_signature='', out_signature='')
def Exit(self):
mainloop.quit()
from dbus.mainloop.glib import threads_init
if __name__ == '__main__':
glib.threads_init()
threads_init()
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
session_bus = dbus.SessionBus()
name = dbus.service.BusName("org.example.ExampleService", session_bus)
object = MyServer(session_bus, '/My')
glib.timeout_add_seconds(1, dequeue_msg)
mainloop = glib.MainLoop()
print "Running example service."
mainloop.run()
now client.py:
import sys
from traceback import print_exc
import dbus
def main():
bus = dbus.SessionBus()
try:
remote_object = bus.get_object("org.example.ExampleService",
"/My")
except dbus.DBusException:
print_exc()
sys.exit(1)
iface = dbus.Interface(remote_object, "org.example.ExampleInterface")
iface.QueueMsg()
if sys.argv[1:] == ['--exit-service']:
iface.Exit()
if __name__ == '__main__':
main()
You usually get this error message when you try to access a service that is no longer available. Check if your server is still running.
You can use d-feet to debug your dbus connections.
The error message about the missing .service file means that you need to create a service file in dbus-1/services.
For example:
# /usr/local/share/dbus-1/services/org.example.ExampleService.service
[D-BUS Service]
Name=org.example.ExampleService
Exec=/home/user1401567/service.py
A lot of tutorials don't include this detail (maybe .service files didn't use to be required?) But, at least on Ubuntu 12.04, dbus services can't be connected to without it.

Categories