jaseg / python-mpv

Python interface to the awesome mpv media player
https://git.jaseg.de/python-mpv.git
Other
543 stars 68 forks source link

Qt Embedding Termination Issue #145

Closed CalculusAce closed 3 years ago

CalculusAce commented 3 years ago

I am currently running python-mpv on Windows and using the Qt embedding for a Qt application I am developing. I have a class defined for mpv player that I am displaying as a a non-modal window that the user can interact with. I am able to open the mpv player, interact with it, and close it successfully the first time. However, if I then open it again after doing some work in the main portion of the application, the entire application freezes. I suspect this may have to do with cleanup issues from when the original instance of MPV was closed.

I wanted to see if I could get some insight on how I may be able to address this, so the user can seamlessly utilize the MPV aspect of the application and other aspects of the application. This issue does not seem to be reproducible on Mac (with the same code), so I think it is exclusively a Windows issue.

Here's some simplified code for context:

Simple MPV_Player Class

import sys, mpv
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget

class MPV_Player(QMainWindow):
    def __init__(self,parent=None):
        super(MPV_Player,self).__init__(parent)

        self.title = 'MPV Player'
        self.left = 350
        self.top = 50
        self.width = 1250
        self.height = 950

        self.setWindowTitle(self.title)
        self.setGeometry(self.left,self.top,self.width,self.height)

        self.container = QWidget(self)
        self.setCentralWidget(self.container)
        self.container.setAttribute(Qt.WA_DontCreateNativeAncestors)
        self.container.setAttribute(Qt.WA_NativeWindow)
        self.player = mpv.MPV(wid=str(int(self.container.winId())),player_operation_mode='pseudo-gui')

        self.player.osd_font_size = 20
        self.player.osd_align_x = 'right'
        self.player.osd_align_y = 'bottom'

    def closeEvent(self,event):
        self.player.stop()
        self.player.quit()

app = QApplication(sys.argv)
import locale
locale.setlocale(locale.LC_NUMERIC,'C')
win = MPV_Player()
win.show()
sys.exit(app.exec_())

In the main application that the MPV_Player class is accessed from

def openMPV(self):
    self.mpv_player = MPV_Player()
    self.mpv_player.show()

Any input on this would be very much appreciated. I have spent quite a few hours trying to figure out what is causing the freeze.

Thanks!

jaseg commented 3 years ago

I've just tested your code on linux, and it all seems to work. I think the quickest way to identify the issue would be if you could launch your app in a debugger like gdb, then provoke the freeze and then check the backtraces of all threads for where they're blocking. If your app completely freezes, Qt's main thread is likely blocking somewhere.

CalculusAce commented 3 years ago

@jaseg Thanks for taking a look at this. I was looking through other issues that have been reported and saw an issue that is similar to this one. I ended up solving this by implementing a 2nd window that houses the mpv instance and brings it to the foreground when the application needs it. I wanted to report that here in case others run into this using the Qt implementation as I think that should be the standard / recommended way of using it within Qt.