I'm trying to write a GTK4 app using python with an embedded media player to use for streaming.
Here's what I have now:
def async_run(func, *args):
thread = Thread(target=func, args=args)
thread.start()
class VideoStream:
def __init__(self):
self.bytes_len =0
self.memory_stream = Gio.MemoryInputStream()
self.media_file = Gtk.MediaFile.new_for_input_stream(self.memory_stream)
def write(self, input_bytes:bytes):
self.bytes_len += len(input_bytes)
gbytes = GLib.Bytes(input_bytes)
self.memory_stream.add_bytes(gbytes)
def get_gtk_memory_stream(self):
return self.media_file
def start() -> Gtk.Video:
async_run(_impl_start_stream)
video_stream.get_gtk_memory_stream().play()
return Gtk.Video.new_for_media_stream(video_stream.get_gtk_memory_stream())
_impl_start_stream loads the video data & calls VideoStream.write several times. Thanks to async_run, it does this from another thread because I want to be able to load the video bit by bit without blocking UI updates.
However, when I call start() and put the resulting widget on the UI, it shows a player with an X across the screen and the play button does nothing.
Since it's not obvious from the documentation what's going wrong, I'll ask this:
- Is it the file type or codec? My expectation is it could only play some file types, but I'm not sure which ones it supports. I'm trying to load .mp4, but it's not clear which formats the media stream accepts. All the documentation talks about binary input, but I don't see where it talks about format
- Is the asynchronous loading a problem? If necessary, I could load the entire stream into a file then play that file, but that adds an a lot of lag time to the start of the video while it loads.
- Do I have to do something to the player as the video loads? Currently there's nothing to forcefully reload the UI or set any properties of the player from the _impl_start_stream thread, but I imagine something like that could be necessary.
My version is 4.6.6 according to pkg-config --modversion gtk4
[edit] After reinstalling my entire computer, it now crashes with this error instead
ERROR:../modules/media/gtkgstmediafile.c:299:gtk_gst_media_file_open: code should not be reached Bail out! ERROR:../modules/media/gtkgstmediafile.c:299:gtk_gst_media_file_open: code should not be reached
Loading from a file instead does not error, but it gives a playback time of 0.