Akash Deshpande
Akash Deshpande

Reputation: 2645

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.

Upvotes: 0

Views: 1852

Answers (1)

Roman Kutlak
Roman Kutlak

Reputation: 2784

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.

Upvotes: 1

Related Questions