From 0aa593f6720a43bc52200a474a6807a91352bf71 Mon Sep 17 00:00:00 2001 From: Luc Sinet Date: Fri, 5 Jun 2020 17:45:18 +0200 Subject: [PATCH] replace-omxplayer #comment Add subtitle selection capability to the player wrapper. - Set the subtitle with the prefered language when available. --- OpenCast/app/service/player.py | 6 ++++++ OpenCast/infra/media/player_wrapper.py | 26 +++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/OpenCast/app/service/player.py b/OpenCast/app/service/player.py index 83f0d7a7..5ed66062 100644 --- a/OpenCast/app/service/player.py +++ b/OpenCast/app/service/player.py @@ -1,6 +1,7 @@ import structlog from OpenCast.app.command import player as player_cmds from OpenCast.app.error import CommandFailure +from OpenCast.config import config from OpenCast.domain.model.player_state import PlayerState from .service import Service @@ -104,6 +105,11 @@ def play_video(model): model.play(video) self._player.play(video.id, str(video.path)) + player = self._player_model() + if player.subtitle_state is True: + sub_stream = video.stream("subtitle", config["subtitle.language"]) + if sub_stream is not None: + self._player.select_subtitle_stream(sub_stream.index) self._update(cmd_id, play_video) def _queue_video_impl(self, cmd_id, video): diff --git a/OpenCast/infra/media/player_wrapper.py b/OpenCast/infra/media/player_wrapper.py index 28377325..93297872 100644 --- a/OpenCast/infra/media/player_wrapper.py +++ b/OpenCast/infra/media/player_wrapper.py @@ -1,9 +1,11 @@ -from threading import Lock +from threading import Condition from uuid import UUID import OpenCast.infra.event.player as e import structlog -import vlc +from vlc import EventType + +from .error import PlayerError class PlayerWrapper: @@ -16,9 +18,6 @@ def __init__(self, vlc_instance, evt_dispatcher): self._player = self._instance.media_player_new() self._id_to_media = {} - self._lock = Lock() - self._stop_operation_id = None - player_events = self._player.event_manager() player_events.event_attach(EventType.MediaPlayerEndReached, self._on_media_end) @@ -39,6 +38,23 @@ def pause(self): def unpause(self): self._player.pause() + def select_subtitle_stream(self, index: int): + media = self._player.get_media() + if media is None: + raise PlayerError("the player is not started") + + def is_playing(_, cv): + with cv: + cv.notify() + + cv = Condition() + self._player.event_manager().event_attach( + EventType.MediaPlayerPlaying, is_playing, cv + ) + with cv: + cv.wait_for(self._player.is_playing) + self._player.video_set_spu(index) + def toggle_subtitle(self): self._player.toggle_teletext()