jaseg / python-mpv

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

Player not working alongside LED Matrix library #197

Closed creating-worlds closed 1 year ago

creating-worlds commented 2 years ago

Hi all,

first of all, thank you for this great library. It already helped me a lot.

I am having a screen and an LED Matrix (with Adafruit bonnet) connected to my Pi 4 which runs Bullseye in a headless mode.

After a long time of troubleshooting my code I finally found the line that would cause the MPV player not to run.

Here is my code (at least the important part)

import mpv
import time
import threading
import socketio
import json
from rgbmatrix import RGBMatrix, RGBMatrixOptions, graphics

class SIO_Wrapper():
    current_channel = 0
    sio = socketio.Client()

   .......
    [MY SOCKETIO FUNCTIONS]
   .......

class VideoPlayer():

    def start_video_in_thread(self, video_name):
        thread = threading.Thread(target=self.play_video(video_name))
        thread.start()

    def play_video(self, video_name):
        print("start video")
        player = mpv.MPV(vo="gpu", gpu_context="drm", hwdec="rkmpp")  
        player.play("/path/to/vid/{}.mp4".format(video_name))
        player.wait_for_playback()
        print("stop video")

class MatrixManager():

    options = RGBMatrixOptions()
    options.rows = 32
    options.cols = 64
    options.chain_length = 1
    options.hardware_mapping = 'adafruit-hat-pwm'
    options.gpio_slowdown = 3
    options.brightness = 100
    options.pwm_lsb_nanoseconds=100
    options.pwm_bits = 7
    options.pwm_dither_bits = 0

    def __init__(self, *args, **kwargs):
        super(MatrixManager, self).__init__(*args, **kwargs)

    def initialize(self):

        matrix = RGBMatrix(options = self.options)
        return matrix

if __name__ == "__main__":
    video_player=VideoPlayer()
    socket = SIO_Wrapper()
    socket.run()
    led_matrix = MatrixManager()
    matrix = led_matrix.initialize()

    while True:
        channel = socket.get_current_channel()
        if channel == 1:
            video_player.start_video_in_thread("TESTVIDEO")
            socket.set_current_channel(0)
            print("channel 0")   

I first thought there was a threading issue with the socketio client but the problem seems to be related to the LED Matrix library.

If I comment out led_matrix.initialize() it works just fine and the video plays.

When the matrix was initialized before I try to play the video, it prints "start video" and without any output it prints "stop video" immediately after.

Any idea how I could solve this or whats the cause of this issue?

jaseg commented 2 years ago

Try initializing MPV like this to get debug output: MPV(..., log_handler=print, loglevel='debug')

I can imagine the LED matrix lib maybe taking up some GPU resources preventing libmpv from accessing the GPU?

jaseg commented 1 year ago

Feel free to re-open if you have further issues.