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.
395 lines
14 KiB
395 lines
14 KiB
3 years ago
|
#!/usr/bin/env python3
|
||
3 years ago
|
import threading
|
||
3 years ago
|
import time
|
||
3 years ago
|
from typing import Callable
|
||
3 years ago
|
|
||
3 years ago
|
import pychromecast
|
||
3 years ago
|
import wx
|
||
3 years ago
|
import wx.lib.scrolledpanel as scrolled
|
||
3 years ago
|
import wx.media
|
||
3 years ago
|
from vlc import Instance
|
||
3 years ago
|
|
||
3 years ago
|
from Channel import SVT
|
||
3 years ago
|
from ChannelProvider import ChannelProvider
|
||
3 years ago
|
from Utils import MYPATH, make_bitmap_from_file, make_sized_button
|
||
3 years ago
|
|
||
3 years ago
|
WIDTH = int(720 / 2)
|
||
|
HEIGHT = int(1440 / 2)
|
||
3 years ago
|
BTN_HEIGHT = 40
|
||
|
SPACER_HEIGHT = 10
|
||
3 years ago
|
SCROLL_RATE = 5
|
||
3 years ago
|
|
||
3 years ago
|
BM_BTN_STYLE = wx.BOTTOM | wx.EXPAND | wx.LEFT | wx.TOP
|
||
3 years ago
|
BTN_STYLE = wx.BORDER_NONE | wx.BU_AUTODRAW | wx.BU_EXACTFIT | wx.BU_NOTEXT
|
||
3 years ago
|
|
||
3 years ago
|
FLAGS = wx.SizerFlags()
|
||
|
FLAGS.Center()
|
||
|
|
||
3 years ago
|
|
||
3 years ago
|
class Cast(wx.Frame):
|
||
|
def __init__(self, *args, **kw):
|
||
|
"""__init__.
|
||
|
:param args:
|
||
|
:param kw:
|
||
|
"""
|
||
|
super().__init__(*args, **kw)
|
||
3 years ago
|
self.m_selected_chromecast = None
|
||
3 years ago
|
self.SetSizeHints(WIDTH, HEIGHT, maxW=WIDTH)
|
||
3 years ago
|
self.m_style = self.GetWindowStyle()
|
||
3 years ago
|
self.m_chromecast_thr = threading.Thread(target=self.get_chromecasts,
|
||
|
args=(),
|
||
|
kwargs={})
|
||
3 years ago
|
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()
|
||
3 years ago
|
self.m_chromecast_thr.start()
|
||
3 years ago
|
self.m_sizer: wx.Sizer = wx.BoxSizer(wx.VERTICAL)
|
||
3 years ago
|
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)
|
||
3 years ago
|
self.m_panel.SetSizer(self.m_sizer)
|
||
3 years ago
|
self.m_providers: list[ChannelProvider] = self.get_providers()
|
||
3 years ago
|
self.show_channel_list(None, 0)
|
||
3 years ago
|
|
||
3 years ago
|
def add_back_button(self, callback: Callable) -> None:
|
||
3 years ago
|
backbtn = wx.Button(self.m_panel,
|
||
|
-1,
|
||
|
label="Go back",
|
||
|
size=(WIDTH, BTN_HEIGHT))
|
||
|
backbtn.Bind(wx.EVT_BUTTON, callback)
|
||
3 years ago
|
self.m_sizer.Add(backbtn)
|
||
|
|
||
3 years ago
|
def get_chromecasts(self) -> None:
|
||
|
"""
|
||
|
[TODO:description]
|
||
3 years ago
|
|
||
3 years ago
|
:rtype None: [TODO:description]
|
||
|
"""
|
||
3 years ago
|
self.m_chromecasts, self.m_browser = pychromecast.get_chromecasts()
|
||
|
|
||
3 years ago
|
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),
|
||
|
)
|
||
|
|
||
3 years ago
|
stop_button = wx.Button(self.m_panel,
|
||
|
-1,
|
||
|
"Stop",
|
||
|
size=(WIDTH / 4, BTN_HEIGHT))
|
||
3 years ago
|
inner_sizer.Add(play_button, FLAGS)
|
||
|
inner_sizer.Add(pause_button, FLAGS)
|
||
|
inner_sizer.Add(back_button, FLAGS)
|
||
3 years ago
|
inner_sizer.Add(stop_button, FLAGS)
|
||
3 years ago
|
|
||
|
if not self.m_chromecast_thr.is_alive(
|
||
|
) and not self.m_selected_chromecast:
|
||
3 years ago
|
btm = make_bitmap_from_file('{}/assets/Cast.png'.format(MYPATH), wx.Size(24,24))
|
||
|
cast_button = wx.BitmapButton(self.m_panel,
|
||
3 years ago
|
-1,
|
||
3 years ago
|
bitmap=btm,
|
||
3 years ago
|
size=(WIDTH / 4, BTN_HEIGHT))
|
||
3 years ago
|
cast_button.Bind(
|
||
3 years ago
|
wx.EVT_BUTTON,
|
||
|
lambda event, muri=uri, cindex=channel_index: self.
|
||
|
select_chromecast(event, muri, cindex),
|
||
|
)
|
||
3 years ago
|
inner_sizer.Add(cast_button, FLAGS)
|
||
3 years ago
|
|
||
|
|
||
|
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)
|
||
3 years ago
|
stop_button.Bind(
|
||
|
wx.EVT_BUTTON,
|
||
|
lambda event, muri=uri, cindex=channel_index: self.
|
||
|
stop_callback(event, muri, cindex),
|
||
|
)
|
||
3 years ago
|
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)
|
||
3 years ago
|
stop_button.Bind(wx.EVT_BUTTON, self.stop)
|
||
3 years ago
|
inner_sizer.Fit(self)
|
||
|
inner_sizer.Layout()
|
||
|
|
||
|
return inner_sizer
|
||
|
|
||
3 years ago
|
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
|
||
|
|
||
3 years ago
|
|
||
|
def show_channel_list(self, _, provider_index) -> None:
|
||
3 years ago
|
self.m_selected_provider_index = provider_index
|
||
|
self.m_selected_provider = self.m_providers[provider_index]
|
||
3 years ago
|
self.m_sizer.Clear(delete_windows=True)
|
||
|
self.m_sizer = wx.BoxSizer(wx.VERTICAL)
|
||
3 years ago
|
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)
|
||
3 years ago
|
|
||
3 years ago
|
channel_index = 0
|
||
3 years ago
|
|
||
3 years ago
|
for channel in self.m_selected_provider.get_channels():
|
||
|
bitmap = channel.get_logo_as_bitmap()
|
||
3 years ago
|
callback = lambda event, cindex=channel_index: self.show_video_list(
|
||
|
event, cindex)
|
||
3 years ago
|
btn_sizer: wx.BoxSizer = make_sized_button(self.m_panel, bitmap,
|
||
|
channel.get_name(),
|
||
|
callback)
|
||
3 years ago
|
self.m_sizer.Add(btn_sizer)
|
||
3 years ago
|
channel_index += 1
|
||
|
|
||
3 years ago
|
self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True)
|
||
3 years ago
|
self.m_panel.SetSizer(self.m_sizer)
|
||
3 years ago
|
self.m_sizer.Fit(self)
|
||
|
self.m_sizer.Layout()
|
||
3 years ago
|
|
||
3 years ago
|
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)
|
||
3 years ago
|
self.m_sizer.Clear(delete_windows=True)
|
||
|
self.m_sizer = wx.BoxSizer(wx.VERTICAL)
|
||
3 years ago
|
back_callback = lambda event: self.show_channel_list(
|
||
|
event, self.m_selected_provider_index)
|
||
|
self.add_back_button(back_callback)
|
||
3 years ago
|
if self.m_selected_provider.get_name() == 'SVT' and self.m_selected_channel.get_id() == 'feed':
|
||
3 years ago
|
|
||
3 years ago
|
def refresh_callback(event):
|
||
3 years ago
|
self.m_selected_channel.refresh()
|
||
3 years ago
|
self.show_video_list(event, channel_index)
|
||
3 years ago
|
|
||
|
refreshbtn = wx.Button(self.m_panel,
|
||
3 years ago
|
-1,
|
||
|
label="Refresh",
|
||
|
size=(WIDTH, BTN_HEIGHT))
|
||
3 years ago
|
refreshbtn.Bind(wx.EVT_BUTTON, refresh_callback)
|
||
|
self.m_sizer.Add(refreshbtn)
|
||
|
|
||
|
if self.m_selected_channel.wait():
|
||
3 years ago
|
with wx.BusyInfo("Please wait, working..."):
|
||
3 years ago
|
while self.m_selected_channel.wait():
|
||
3 years ago
|
time.sleep(1)
|
||
|
wx.GetApp().Yield()
|
||
|
|
||
3 years ago
|
btnindex = 0
|
||
|
for item in self.m_selected_channel.get_items(): # type: ignore
|
||
3 years ago
|
inner_sizer = wx.BoxSizer(wx.VERTICAL)
|
||
3 years ago
|
pane_sizer = wx.BoxSizer(wx.VERTICAL)
|
||
3 years ago
|
title = wx.StaticText(self.m_panel, -1)
|
||
3 years ago
|
title.SetLabelMarkup("<span weight='bold' >{}</span>".format(
|
||
|
item["title"]))
|
||
3 years ago
|
|
||
3 years ago
|
bitmap = item["thumbnail"]
|
||
3 years ago
|
btn = wx.BitmapButton(self.m_panel,
|
||
3 years ago
|
id=btnindex,
|
||
3 years ago
|
bitmap=bitmap,
|
||
3 years ago
|
style=BM_BTN_STYLE,
|
||
|
size=bitmap.GetSize())
|
||
3 years ago
|
btn.Bind(
|
||
|
wx.EVT_BUTTON,
|
||
3 years ago
|
lambda event, link=item["link"], cindex=channel_index: self.
|
||
|
show_player(event, link, cindex),
|
||
3 years ago
|
)
|
||
3 years ago
|
inner_sizer.Add(title)
|
||
|
inner_sizer.Add(btn)
|
||
3 years ago
|
collapsable_pane = wx.CollapsiblePane(self.m_panel,
|
||
|
wx.ID_ANY,
|
||
|
"Details:",
|
||
|
size=(WIDTH, 20))
|
||
3 years ago
|
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)
|
||
3 years ago
|
|
||
3 years ago
|
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()
|
||
|
|
||
3 years ago
|
collapsable_pane.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED,
|
||
|
lambda event: fit_and_layout(event))
|
||
3 years ago
|
#inner_sizer.Add(description)
|
||
3 years ago
|
self.m_sizer.Add(inner_sizer)
|
||
3 years ago
|
self.m_sizer.AddSpacer(SPACER_HEIGHT)
|
||
3 years ago
|
btnindex = btnindex + 1
|
||
3 years ago
|
self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True)
|
||
3 years ago
|
self.m_panel.SetSizer(self.m_sizer)
|
||
3 years ago
|
self.m_sizer.Fit(self)
|
||
|
self.m_sizer.Layout()
|
||
3 years ago
|
|
||
3 years ago
|
def show_player(self, _, uri: str, channel_index: int):
|
||
3 years ago
|
"""
|
||
|
Show the video player
|
||
|
|
||
|
:param _ event: unused
|
||
|
:param uri str: the link to the video stream
|
||
|
"""
|
||
3 years ago
|
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)
|
||
|
|
||
3 years ago
|
self.m_sizer.Clear(delete_windows=True)
|
||
3 years ago
|
self.m_sizer = wx.BoxSizer(wx.VERTICAL)
|
||
3 years ago
|
self.m_sizer.Add(self.get_player_controls(channel_index, uri), FLAGS)
|
||
|
self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True)
|
||
3 years ago
|
self.m_panel.SetSizer(self.m_sizer)
|
||
3 years ago
|
self.m_sizer.Fit(self)
|
||
|
self.m_sizer.Layout()
|
||
3 years ago
|
|
||
3 years ago
|
def select_chromecast(self, _, uri, channel_index):
|
||
3 years ago
|
self.m_sizer.Clear(delete_windows=True)
|
||
|
self.m_sizer = wx.BoxSizer(wx.VERTICAL)
|
||
|
|
||
3 years ago
|
cancel_btn = wx.Button(self.m_panel,
|
||
|
-1,
|
||
|
"Cancel",
|
||
3 years ago
|
size=(WIDTH, BTN_HEIGHT))
|
||
3 years ago
|
cancel_btn.Bind(
|
||
|
wx.EVT_BUTTON,
|
||
3 years ago
|
lambda event, cindex=channel_index: self.show_video_list(
|
||
|
event, cindex),
|
||
3 years ago
|
)
|
||
3 years ago
|
self.m_sizer.Add(cancel_btn)
|
||
3 years ago
|
|
||
3 years ago
|
for cast in self.m_chromecasts:
|
||
3 years ago
|
friendly_name = cast.device.friendly_name
|
||
3 years ago
|
btn = wx.Button(self.m_panel,
|
||
|
id=-1,
|
||
|
label=friendly_name,
|
||
|
size=(WIDTH, BTN_HEIGHT))
|
||
3 years ago
|
btn.Bind(
|
||
|
wx.EVT_BUTTON,
|
||
3 years ago
|
lambda event, chromecast=cast, muri=uri, cindex=channel_index:
|
||
|
self.set_chromecast(event, chromecast, muri, cindex),
|
||
3 years ago
|
)
|
||
3 years ago
|
self.m_sizer.Add(btn)
|
||
3 years ago
|
self.m_panel.SetupScrolling(rate_y=SCROLL_RATE, scrollToTop=True)
|
||
3 years ago
|
self.m_panel.SetSizer(self.m_sizer)
|
||
3 years ago
|
self.m_sizer.Fit(self)
|
||
|
self.m_sizer.Layout()
|
||
3 years ago
|
|
||
3 years ago
|
def set_chromecast(self, event, chromecast, uri, channel_index):
|
||
3 years ago
|
self.m_selected_chromecast = chromecast
|
||
3 years ago
|
self.show_player(event, uri, channel_index)
|
||
3 years ago
|
|
||
3 years ago
|
def cast(self, _, uri):
|
||
3 years ago
|
mimetype = "video/mp4"
|
||
3 years ago
|
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
|
||
3 years ago
|
|
||
3 years ago
|
break
|
||
|
|
||
3 years ago
|
if (cast.socket_client.is_connected and has_played
|
||
|
and player_state != "PLAYING"):
|
||
3 years ago
|
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
|
||
3 years ago
|
cast.wait()
|
||
3 years ago
|
cast.media_controller.pause()
|
||
|
|
||
|
def play_cast(self, event):
|
||
|
cast = self.m_selected_chromecast
|
||
3 years ago
|
cast.wait()
|
||
3 years ago
|
cast.media_controller.play()
|
||
|
|
||
3 years ago
|
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()
|
||
|
|
||
3 years ago
|
def play(self, _):
|
||
3 years ago
|
self.m_vlc_listplayer.play()
|
||
3 years ago
|
|
||
3 years ago
|
def pause(self, _):
|
||
3 years ago
|
self.m_vlc_listplayer.pause()
|
||
|
|
||
|
def stop(self, _):
|
||
|
self.m_vlc_listplayer.stop()
|
||
3 years ago
|
|
||
3 years ago
|
def quit(self, _):
|
||
3 years ago
|
self.Destroy()
|
||
|
|
||
|
|
||
3 years ago
|
if __name__ == "__main__":
|
||
3 years ago
|
# 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()
|
||
3 years ago
|
frm: Cast = Cast(None, title="Cast")
|
||
3 years ago
|
frm.Show()
|
||
|
app.MainLoop()
|