You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cast/scripts/cast

395 lines
14 KiB

#!/usr/bin/env python3
import threading
import time
from typing import Callable
import pychromecast
import wx
import wx.lib.scrolledpanel as scrolled
import wx.media
from vlc import Instance
from Channel import SVT
from ChannelProvider import ChannelProvider
from Utils import MYPATH, make_bitmap_from_file, make_sized_button
WIDTH = int(720 / 2)
HEIGHT = int(1440 / 2)
BTN_HEIGHT = 40
SPACER_HEIGHT = 10
SCROLL_RATE = 5
BM_BTN_STYLE = wx.BOTTOM | wx.EXPAND | wx.LEFT | wx.TOP
BTN_STYLE = wx.BORDER_NONE | wx.BU_AUTODRAW | wx.BU_EXACTFIT | wx.BU_NOTEXT
FLAGS = wx.SizerFlags()
FLAGS.Center()
class Cast(wx.Frame):
def __init__(self, *args, **kw):
"""__init__.
:param args:
:param kw:
"""
super().__init__(*args, **kw)
self.m_selected_chromecast = None
self.SetSizeHints(WIDTH, HEIGHT, maxW=WIDTH)
self.m_style = self.GetWindowStyle()
self.m_chromecast_thr = threading.Thread(target=self.get_chromecasts,
args=(),
kwargs={})
self.m_vlc = Instance()
self.m_vlc_medialist = self.m_vlc.media_list_new()
self.m_vlc_listplayer = self.m_vlc.media_list_player_new()
self.m_chromecast_thr.start()
self.m_sizer: wx.Sizer = wx.BoxSizer(wx.VERTICAL)
self.m_panel: wx.lib.scrolledpanel.ScrolledPanel = scrolled.ScrolledPanel( # type: ignore
self, -1, style=wx.VSCROLL)
self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True)
self.m_panel.SetSizer(self.m_sizer)
self.m_providers: list[ChannelProvider] = self.get_providers()
self.show_channel_list(None, 0)
def add_back_button(self, callback: Callable) -> None:
backbtn = wx.Button(self.m_panel,
-1,
label="Go back",
size=(WIDTH, BTN_HEIGHT))
backbtn.Bind(wx.EVT_BUTTON, callback)
self.m_sizer.Add(backbtn)
def get_chromecasts(self) -> None:
"""
[TODO:description]
:rtype None: [TODO:description]
"""
self.m_chromecasts, self.m_browser = pychromecast.get_chromecasts()
def get_player_controls(self, channel_index: int, uri: str) -> wx.BoxSizer:
inner_sizer = wx.BoxSizer(wx.HORIZONTAL)
play_button = wx.Button(self.m_panel,
-1,
"Play",
size=(WIDTH / 4, BTN_HEIGHT))
pause_button = wx.Button(self.m_panel,
-1,
"Pause",
size=(WIDTH / 4, BTN_HEIGHT))
back_button = wx.Button(self.m_panel,
-1,
"Back",
size=(WIDTH / 4, BTN_HEIGHT))
back_button.Bind(
wx.EVT_BUTTON,
lambda event, cindex=channel_index: self.show_video_list(
event, cindex),
)
stop_button = wx.Button(self.m_panel,
-1,
"Stop",
size=(WIDTH / 4, BTN_HEIGHT))
inner_sizer.Add(play_button, FLAGS)
inner_sizer.Add(pause_button, FLAGS)
inner_sizer.Add(back_button, FLAGS)
inner_sizer.Add(stop_button, FLAGS)
if not self.m_chromecast_thr.is_alive(
) and not self.m_selected_chromecast:
btm = make_bitmap_from_file('{}/assets/Cast.png'.format(MYPATH), wx.Size(24,24))
cast_button = wx.BitmapButton(self.m_panel,
-1,
bitmap=btm,
size=(WIDTH / 4, BTN_HEIGHT))
cast_button.Bind(
wx.EVT_BUTTON,
lambda event, muri=uri, cindex=channel_index: self.
select_chromecast(event, muri, cindex),
)
inner_sizer.Add(cast_button, FLAGS)
if self.m_selected_chromecast:
self.cast(wx.media.EVT_MEDIA_LOADED, uri),
play_button.Bind(wx.EVT_BUTTON, self.play_cast)
pause_button.Bind(wx.EVT_BUTTON, self.pause_cast)
stop_button.Bind(
wx.EVT_BUTTON,
lambda event, muri=uri, cindex=channel_index: self.
stop_callback(event, muri, cindex),
)
else:
self.Bind(wx.media.EVT_MEDIA_LOADED,
lambda event: wx.Frame.SetFocus(self))
play_button.Bind(wx.EVT_BUTTON, self.play)
pause_button.Bind(wx.EVT_BUTTON, self.pause)
stop_button.Bind(wx.EVT_BUTTON, self.stop)
inner_sizer.Fit(self)
inner_sizer.Layout()
return inner_sizer
def get_providers(self) -> list[ChannelProvider]:
providers = list()
svt = ChannelProvider(
"SVT",
channels=[
SVT.SVT("feed"),
SVT.SVT("ch-svt1"),
SVT.SVT("ch-svt2"),
SVT.SVT("ch-svt24"),
SVT.SVT("ch-barnkanalen"),
SVT.SVT("ch-kunskapskanalen"),
],
)
providers.append(svt)
return providers
def show_channel_list(self, _, provider_index) -> None:
self.m_selected_provider_index = provider_index
self.m_selected_provider = self.m_providers[provider_index]
self.m_sizer.Clear(delete_windows=True)
self.m_sizer = wx.BoxSizer(wx.VERTICAL)
closebtn = wx.Button(self.m_panel,
-1,
label="Close",
size=(WIDTH, BTN_HEIGHT))
closebtn.Bind(wx.EVT_BUTTON, lambda event: self.Destroy())
self.m_sizer.Add(closebtn, 0, wx.ALL, 1)
channel_index = 0
for channel in self.m_selected_provider.get_channels():
bitmap = channel.get_logo_as_bitmap()
callback = lambda event, cindex=channel_index: self.show_video_list(
event, cindex)
btn_sizer: wx.BoxSizer = make_sized_button(self.m_panel, bitmap,
channel.get_name(),
callback)
self.m_sizer.Add(btn_sizer)
channel_index += 1
self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True)
self.m_panel.SetSizer(self.m_sizer)
self.m_sizer.Fit(self)
self.m_sizer.Layout()
def show_video_list(self, _, channel_index) -> None:
self.SetWindowStyle(self.m_style)
self.m_selected_channel = self.m_selected_provider.get_channel_by_index(
channel_index)
self.m_sizer.Clear(delete_windows=True)
self.m_sizer = wx.BoxSizer(wx.VERTICAL)
back_callback = lambda event: self.show_channel_list(
event, self.m_selected_provider_index)
self.add_back_button(back_callback)
if self.m_selected_provider.get_name() == 'SVT' and self.m_selected_channel.get_id() == 'feed':
def refresh_callback(event):
self.m_selected_channel.refresh()
self.show_video_list(event, channel_index)
refreshbtn = wx.Button(self.m_panel,
-1,
label="Refresh",
size=(WIDTH, BTN_HEIGHT))
refreshbtn.Bind(wx.EVT_BUTTON, refresh_callback)
self.m_sizer.Add(refreshbtn)
if self.m_selected_channel.wait():
with wx.BusyInfo("Please wait, working..."):
while self.m_selected_channel.wait():
time.sleep(1)
wx.GetApp().Yield()
btnindex = 0
for item in self.m_selected_channel.get_items(): # type: ignore
inner_sizer = wx.BoxSizer(wx.VERTICAL)
pane_sizer = wx.BoxSizer(wx.VERTICAL)
title = wx.StaticText(self.m_panel, -1)
title.SetLabelMarkup("<span weight='bold' >{}</span>".format(
item["title"]))
bitmap = item["thumbnail"]
btn = wx.BitmapButton(self.m_panel,
id=btnindex,
bitmap=bitmap,
style=BM_BTN_STYLE,
size=bitmap.GetSize())
btn.Bind(
wx.EVT_BUTTON,
lambda event, link=item["link"], cindex=channel_index: self.
show_player(event, link, cindex),
)
inner_sizer.Add(title)
inner_sizer.Add(btn)
collapsable_pane = wx.CollapsiblePane(self.m_panel,
wx.ID_ANY,
"Details:",
size=(WIDTH, 20))
inner_sizer.Add(collapsable_pane, 0, wx.GROW | wx.ALL, 5)
pane_win = collapsable_pane.GetPane()
description = wx.StaticText(pane_win, -1, item["description"])
description.Wrap(WIDTH - 2)
pane_sizer.Add(description, wx.GROW | wx.ALL, 2)
pane_win.SetSizer(pane_sizer)
pane_sizer.SetSizeHints(pane_win)
def fit_and_layout(_):
pane_sizer.Layout()
pane_sizer.Fit(pane_win)
inner_sizer.Layout()
inner_sizer.Fit(self)
self.m_sizer.Fit(self)
self.m_sizer.Layout()
collapsable_pane.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED,
lambda event: fit_and_layout(event))
#inner_sizer.Add(description)
self.m_sizer.Add(inner_sizer)
self.m_sizer.AddSpacer(SPACER_HEIGHT)
btnindex = btnindex + 1
self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True)
self.m_panel.SetSizer(self.m_sizer)
self.m_sizer.Fit(self)
self.m_sizer.Layout()
def show_player(self, _, uri: str, channel_index: int):
"""
Show the video player
:param _ event: unused
:param uri str: the link to the video stream
"""
media = self.m_vlc.media_new(uri)
self.m_vlc_medialist.add_media(media)
self.m_vlc_listplayer.set_media_list(self.m_vlc_medialist)
self.m_sizer.Clear(delete_windows=True)
self.m_sizer = wx.BoxSizer(wx.VERTICAL)
self.m_sizer.Add(self.get_player_controls(channel_index, uri), FLAGS)
self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True)
self.m_panel.SetSizer(self.m_sizer)
self.m_sizer.Fit(self)
self.m_sizer.Layout()
def select_chromecast(self, _, uri, channel_index):
self.m_sizer.Clear(delete_windows=True)
self.m_sizer = wx.BoxSizer(wx.VERTICAL)
cancel_btn = wx.Button(self.m_panel,
-1,
"Cancel",
size=(WIDTH, BTN_HEIGHT))
cancel_btn.Bind(
wx.EVT_BUTTON,
lambda event, cindex=channel_index: self.show_video_list(
event, cindex),
)
self.m_sizer.Add(cancel_btn)
for cast in self.m_chromecasts:
friendly_name = cast.device.friendly_name
btn = wx.Button(self.m_panel,
id=-1,
label=friendly_name,
size=(WIDTH, BTN_HEIGHT))
btn.Bind(
wx.EVT_BUTTON,
lambda event, chromecast=cast, muri=uri, cindex=channel_index:
self.set_chromecast(event, chromecast, muri, cindex),
)
self.m_sizer.Add(btn)
self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True)
self.m_panel.SetSizer(self.m_sizer)
self.m_sizer.Fit(self)
self.m_sizer.Layout()
def set_chromecast(self, event, chromecast, uri, channel_index):
self.m_selected_chromecast = chromecast
self.show_player(event, uri, channel_index)
def cast(self, _, uri):
mimetype = "video/mp4"
cast = self.m_selected_chromecast
cast.wait()
cast.media_controller.play_media(uri, mimetype)
# Wait for player_state PLAYING
player_state = None
has_played = False
while True:
if player_state != cast.media_controller.status.player_state:
player_state = cast.media_controller.status.player_state
if player_state == "PLAYING":
has_played = True
break
if (cast.socket_client.is_connected and has_played
and player_state != "PLAYING"):
has_played = False
cast.media_controller.play_media(uri, mimetype)
time.sleep(0.1)
self.m_browser.stop_discovery()
def pause_cast(self, event):
cast = self.m_selected_chromecast
cast.wait()
cast.media_controller.pause()
def play_cast(self, event):
cast = self.m_selected_chromecast
cast.wait()
cast.media_controller.play()
def stop_cast(self, event):
cast = self.m_selected_chromecast
cast.wait()
cast.media_controller.pause()
time.sleep(2)
cast.media_controller.stop()
self.m_selected_chromecast = None
def stop_callback(self, event, uri: str, channel_index: int):
self.stop_cast(event)
self.m_sizer.Clear(delete_windows=True)
self.m_sizer = wx.BoxSizer(wx.VERTICAL)
self.m_sizer.Add(self.get_player_controls(channel_index, uri))
self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True)
self.m_panel.SetSizer(self.m_sizer)
self.m_sizer.Fit(self)
self.m_sizer.Layout()
def play(self, _):
self.m_vlc_listplayer.play()
def pause(self, _):
self.m_vlc_listplayer.pause()
def stop(self, _):
self.m_vlc_listplayer.stop()
def quit(self, _):
self.Destroy()
if __name__ == "__main__":
# When this module is run (not imported) then create the app, the
# frame, show it, and start the event loop.
app: wx.App = wx.App()
frm: Cast = Cast(None, title="Cast")
frm.Show()
app.MainLoop()