Skip to content

Commit

Permalink
Ensure http server is started prior to creating MPD for playback anxd…
Browse files Browse the repository at this point in the history
  • Loading branch information
MoojMidge committed Nov 8, 2024
1 parent 8d3f367 commit 858e263
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
35 changes: 26 additions & 9 deletions resources/lib/youtube_plugin/kodion/context/xbmc/xbmc_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ def wakeup(self, target, timeout=None, payload=None):
data.update(payload)
self.send_notification(WAKEUP, data)
if not timeout:
return
return None

pop_property = self.get_ui().pop_property
no_timeout = timeout < 0
Expand All @@ -834,17 +834,34 @@ def wakeup(self, target, timeout=None, payload=None):
wait_period = wait_period_ms / 1000

while no_timeout or remaining > 0:
awake = pop_property(WAKEUP)
if awake:
if awake == target:
self.log_debug('Wakeup |{0}| in {1}ms'
.format(awake, timeout - remaining))
else:
self.log_error('Wakeup |{0}| in {1}ms - expected |{2}|'
.format(awake, timeout - remaining, target))
data = pop_property(WAKEUP)
if data:
data = json.loads(data)

if data:
response = data.get('response')
response_target = data.get('target') or 'Unknown'

if target == response_target:
if response:
self.log_debug('Wakeup |{0}| in {1}ms'
.format(response_target,
timeout - remaining))
else:
self.log_error('Wakeup |{0}| in {1}ms - failed'
.format(response_target,
timeout - remaining))
return response

self.log_error('Wakeup |{0}| in {1}ms - expected |{2}|'
.format(response_target,
timeout - remaining,
target))
break

wait(wait_period)
remaining -= wait_period_ms
else:
self.log_error('Wakeup |{0}| timed out in {1}ms'
.format(target, timeout))
return False
19 changes: 15 additions & 4 deletions resources/lib/youtube_plugin/kodion/monitors/service_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ def onNotification(self, sender, method, data):
player = xbmc.Player()
try:
playing_file = urlsplit(player.getPlayingFile())
if playing_file.path in {PATHS.MPD, PATHS.REDIRECT}:
if playing_file.path in {PATHS.MPD,
PATHS.PLAY,
PATHS.REDIRECT}:
self.onWake()
except RuntimeError:
pass
Expand All @@ -129,10 +131,13 @@ def onNotification(self, sender, method, data):

if target == PLUGIN_WAKEUP:
self.interrupt = True
response = True

elif target == SERVER_WAKEUP:
if not self.httpd and self.httpd_required():
self.start_httpd()
response = self.start_httpd()
else:
response = True
if self.httpd_sleep_allowed:
self.httpd_sleep_allowed = None

Expand All @@ -142,9 +147,14 @@ def onNotification(self, sender, method, data):
self._settings_collect = True
elif state == 'process':
self.onSettingsChanged(force=True)
response = True

else:
return

if data.get('response_required'):
self.set_property(WAKEUP, target)
data['response'] = response
self.set_property(WAKEUP, json.dumps(data, ensure_ascii=False))

elif event == REFRESH_CONTAINER:
self.refresh_container()
Expand Down Expand Up @@ -245,7 +255,7 @@ def start_httpd(self):
port=self._httpd_port,
context=context)
if not self.httpd:
return
return False

self.httpd_thread = threading.Thread(target=self.httpd.serve_forever)
self.httpd_thread.start()
Expand All @@ -254,6 +264,7 @@ def start_httpd(self):
context.log_debug('HTTPServer: Listening on |{ip}:{port}|'
.format(ip=address[0],
port=address[1]))
return True

def shutdown_httpd(self):
if self.httpd:
Expand Down
6 changes: 5 additions & 1 deletion resources/lib/youtube_plugin/youtube/helper/yt_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
PLAY_PROMPT_SUBTITLES,
PLAY_TIMESHIFT,
PLAY_WITH,
SERVER_WAKEUP,
)
from ...kodion.items import AudioItem, UriItem, VideoItem
from ...kodion.network import get_connect_address
Expand Down Expand Up @@ -68,14 +69,17 @@ def _play_stream(provider, context):
audio_only = settings.audio_only()
use_adaptive_formats = (not is_external
or settings.alternative_player_adaptive())
use_mpd = (use_adaptive_formats
and settings.use_mpd_videos()
and context.wakeup(SERVER_WAKEUP, timeout=5))

try:
streams = client.get_streams(
context,
video_id=video_id,
ask_for_quality=ask_for_quality,
audio_only=audio_only,
use_mpd=use_adaptive_formats and settings.use_mpd_videos(),
use_mpd=use_mpd,
)
except YouTubeException as exc:
msg = ('yt_play.play_video - Error'
Expand Down

0 comments on commit 858e263

Please sign in to comment.