willprice / python-omxplayer-wrapper

:tv: Control OMXPlayer, the Raspberry Pi media player, from Python
http://python-omxplayer-wrapper.readthedocs.io
GNU Lesser General Public License v3.0
253 stars 72 forks source link

"DBus cannot connect to the OMXPlayer process" when OMXPlayer instantiated in Flask route handler #183

Closed superlou closed 5 years ago

superlou commented 5 years ago

Issue Report

Note that I've heavily edited this issue. My initial magical diagnosis of a Python code execution halt without an exception was way off base.

Description

I just updated from Stretch to Buster, and I'm receiving SystemError: DBus cannot connect to the OMXPlayer process when calling OMXPlayer(...). This only seems to happen when trying to create the player in a Flask route handler, but this was working on Stretch and I haven't found any documentation indicating this behaviour would change. I've also tried reverting to older Flask versions without any difference in symptoms.

Problem reproduction

I have created a helper class to manage my communication with OMXPlayer in a Flask webserver.

class Omx:
   ...

    def play(self, filename):
        if self.player:
            self.expects_loading_exit = True
            self.player.load(filename)
        else:
            print('here1')
            self.player = OMXPlayer(filename, args=['-b', '--no-osd', '-o', 'both']) #, '--loop'])
            print('here2')
            self.player.stopEvent += self.on_player_stop
            self.player.exitEvent += self.on_player_exit

The full project is on github.

If I instantiate this class and run in a Python 3 shell, everything works: the video plays and "here1" and "here2" are printed. If the web server calls play during configuration, it also works with the same behavior:

def main():
    app.config['MEDIA_FOLDER'] = 'media'
    create_folder(app.config['MEDIA_FOLDER'])
    app.config['omx'] = Omx(app.config['MEDIA_FOLDER'])
    app.config['omx'].play('media/example.mp4')
    app.config['DEBUG'] = True

    socketio.run(app, host='0.0.0.0', port=8910, debug=True)

However, if I call play from inside a route handler, "here1" is printed and the video plays, but "here2" is not printed and I receive the SystemError.

@app.route('/api/player', methods=['GET', 'POST'])
def player_status():
    if request.method == 'GET':
        omx = app.config['omx']
        omx.play('media/example.mp4')

Is there anything in python-omxplayer-wrapper that might explain this change in behavior? Is there anything on Buster that could cause this? This approach worked under Stretch, though admittedly it's a fresh install, so it's possible that this is a symptom of something else.

Environment details

Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 10 (buster)
Release:        10
Codename:       buster
Software Version
python-omxplayer-wrapper 0.3.2
python-dbus (dpkg -s python-dbus) 1.2.8-3
python3-dbus (dpkg -s python3-dbus) 1.2.8-3
python (python --version) 3.7.3
omxplayer (omxplayer --version) f06235c
superlou commented 5 years ago

Using PDB, and stepping over the OMXPlayer(...) call, I believe the error is a failure to connect to the OMXPlayer process.

> /usr/local/lib/python3.7/dist-packages/omxplayer/player.py(154)__init__()->None
-> self.load(source, pause=pause)
(Pdb) l
149             #: Event called on setting position ``callback(player, absolute_position)``
150             self.positionEvent = Event()
151     
152             self._process = None
153             self._connection = None
154  ->         self.load(source, pause=pause)
155     
156         def _load_source(self, source):
157             if self._process:
158                 self.quit()
159     
(Pdb) s
SystemError: DBus cannot connect to the OMXPlayer process
> /home/pi/workspace/pideck2/omx.py(18)play()
-> self.player = OMXPlayer(filename, args=['-b', '--no-osd', '-o', 'both']) #, '--loop'])
superlou commented 5 years ago

It definitely has something to do with executing in gevent. I switched to eventlet and it's working. I'm going to close this since I think it's a problem with my understanding of gevent, and not something in OMX player wrapper's DBUS access itself.

jonra1993 commented 4 years ago

Hi @superlou can you explain how did you solve it, please ??

superlou commented 4 years ago

@jonra1993, unfortunately, I was never able to figure out how to fix it in gevent. You can see the app that runs on eventlet here: https://github.com/superlou/pideck. The connection happens at https://github.com/superlou/pideck/blob/master/omx.py#L25.

jonra1993 commented 4 years ago

Thanks for your time @superlou. I am not sure what happened with my python script but I cloned omxplayer examples and they worked so I decided just edit examples.