annotate libervia/web/pages/_browser/alt_media_player.py @ 1593:c6976c5b85a1

browser (chat/select): add bookmarked entities on empty search
author Goffi <goffi@goffi.org>
date Mon, 11 Dec 2023 00:43:06 +0100
parents eb00d593801d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
3 """This module implement an alternative media player
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
4
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
5 If browser can't play natively some libre video/audio formats, ogv.js will be used,
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
6 otherwise the native player will be used.
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
7
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
8 This player uses its own controls, this allow better tuning/event handling notably with
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
9 slideshow.
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 """
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
12 from browser import document, timer, html
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
13
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
14
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
15 NO_PAGINATION = "NO_PAGINATION"
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
16 NO_SCROLLBAR = "NO_SCROLLBAR"
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
17
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
19 class MediaPlayer:
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 TIMER_MODES = ("timer", "remaining")
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
21 # will be set to False if browser can't play natively webm or ogv
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
22 native = True
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
23 # will be set to True when template and modules will be imported
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 imports_done = False
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
25
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
26 def __init__(
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
27 self,
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
28 sources,
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
29 to_rpl_vid_elt=None,
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
30 poster=None,
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
31 reduce_click_area=False
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
32 ):
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
33 """
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
34 @param sources: list of paths to media
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
35 only the first one is used at the moment
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
36 @param to_rpl_vid_elt: video element to replace
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
37 if None, nothing is replaced and element must be inserted manually
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
38 @param reduce_click_area: when True, only center of the element will react to
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
39 click. Useful when used in slideshow, as click on border is used to
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
40 show/hide slide controls
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
41 """
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
42 self.do_imports()
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
43
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
44 self.reduce_click_area = reduce_click_area
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
45
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
46 self.media_player_elt = media_player_elt = media_player_tpl.get_elt()
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
47 self.player = player = self._create_player(sources, poster)
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
48 if to_rpl_vid_elt is not None:
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
49 to_rpl_vid_elt.parentNode.replaceChild(media_player_elt, to_rpl_vid_elt)
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
50 overlay_play_elt = self.media_player_elt.select_one(".media_overlay_play")
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
51 overlay_play_elt.bind("click", self.on_play_click)
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
52 self.progress_elt = media_player_elt.select_one("progress")
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 self.progress_elt.bind("click", self.on_progress_click)
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
54 self.timer_elt = media_player_elt.select_one(".timer")
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 self.timer_mode = "timer"
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
56
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
57 self.controls_elt = media_player_elt.select_one(".media_controls")
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
58 # we devnull 2 following events to avoid accidental side effect
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
59 # this is notably useful in slideshow to avoid changing the slide when
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
60 # the user misses slightly a button
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
61 self.controls_elt.bind("mousedown", self._devnull)
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
62 self.controls_elt.bind("click", self._devnull)
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
63
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
64 player_wrapper_elt = media_player_elt.select_one(".media_elt")
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
65 player.preload = "none"
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 player.src = sources[0]
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 player_wrapper_elt <= player
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 self.hide_controls_timer = None
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
69
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
70 # we capture mousedown to avoid side effect on slideshow
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
71 player_wrapper_elt.addEventListener("mousedown", self._devnull)
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
72 player_wrapper_elt.addEventListener("click", self.on_player_click)
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
73
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 # buttons
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 for handler in ("play", "change_timer_mode", "change_volume", "fullscreen"):
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
76 for elt in media_player_elt.select(f".click_to_{handler}"):
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 elt.bind("click", getattr(self, f"on_{handler}_click"))
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 # events
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 # FIXME: progress is not implemented in OGV.js, update when available
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 for event in ("play", "pause", "timeupdate", "ended", "volumechange"):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 player.bind(event, getattr(self, f"on_{event}"))
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
82
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
83 @property
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
84 def elt(self):
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
85 return self.media_player_elt
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
86
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
87 def _create_player(self, sources, poster):
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
88 """Create player element, using native one when possible"""
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
89 player = None
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
90 if not self.native:
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
91 source = sources[0]
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
92 ext = self.get_source_ext(source)
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
93 if ext is None:
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
94 print(
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
95 f"no extension found for {source}, using native player"
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
96 )
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
97 elif ext in self.cant_play_ext_list:
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
98 print(f"OGV player user for {source}")
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
99 player = self.ogv.OGVPlayer.new()
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
100 # OGCPlayer has non standard "poster" property
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
101 player.poster = poster
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
102 if player is None:
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
103 player = html.VIDEO(poster=poster)
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
104 return player
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
105
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
106 def reset(self):
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
107 """Put back media player in intial state
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
108
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
109 media will be stopped, time will be set to beginning, overlay will be put back
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
110 """
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
111 print("resetting media player")
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
112 self.player.pause()
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
113 self.player.currentTime = 0
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
114 self.media_player_elt.classList.remove("in_use")
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
115
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
116 def _devnull(self, evt):
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
117 # stop an event
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
118 evt.preventDefault()
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 evt.stopPropagation()
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
120
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
121 def on_player_click(self, evt):
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
122 if self.reduce_click_area:
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
123 bounding_rect = self.media_player_elt.getBoundingClientRect()
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
124 margin_x = margin_y = 200
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
125 if ((evt.clientX - bounding_rect.left < margin_x
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
126 or bounding_rect.right - evt.clientX < margin_x
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
127 or evt.clientY - bounding_rect.top < margin_y
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
128 or bounding_rect.bottom - evt.clientY < margin_y
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
129 )):
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
130 # click is not in the center, we don't handle it and let the event
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
131 # propagate
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
132 return
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
133 self.on_play_click(evt)
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
134
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 def on_play_click(self, evt):
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
136 evt.preventDefault()
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
137 evt.stopPropagation()
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
138 self.media_player_elt.classList.add("in_use")
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 if self.player.paused:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 print("playing")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 self.player.play()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 else:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 self.player.pause()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 print("paused")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
145
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 def on_change_timer_mode_click(self, evt):
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
147 evt.preventDefault()
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
148 evt.stopPropagation()
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 self.timer_mode = self.TIMER_MODES[
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 (self.TIMER_MODES.index(self.timer_mode) + 1) % len(self.TIMER_MODES)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 ]
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
152
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 def on_change_volume_click(self, evt):
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
154 evt.stopPropagation()
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 self.player.muted = not self.player.muted
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
156
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 def on_fullscreen_click(self, evt):
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
158 evt.stopPropagation()
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 try:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 fullscreen_elt = document.fullscreenElement
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
161 request_fullscreen = self.media_player_elt.requestFullscreen
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 except AttributeError:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 print("fullscreen is not available on this browser")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 else:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 if fullscreen_elt == None:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 print("requesting fullscreen")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 request_fullscreen()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 else:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 print(f"leaving fullscreen: {fullscreen_elt}")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 try:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 document.exitFullscreen()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 except AttributeError:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 print("exitFullscreen not available on this browser")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
174
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 def on_progress_click(self, evt):
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
176 evt.stopPropagation()
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 position = evt.offsetX / evt.target.width
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 new_time = self.player.duration * position
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 self.player.currentTime = new_time
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
180
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 def on_play(self, evt):
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
182 self.media_player_elt.classList.add("playing")
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 self.show_controls()
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
184 self.media_player_elt.bind("mousemove", self.on_mouse_move)
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
185
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 def on_pause(self, evt):
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
187 self.media_player_elt.classList.remove("playing")
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 self.show_controls()
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
189 self.media_player_elt.unbind("mousemove")
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
190
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 def on_timeupdate(self, evt):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 self.update_progress()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
193
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 def on_ended(self, evt):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 self.update_progress()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
196
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 def on_volumechange(self, evt):
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
198 evt.stopPropagation()
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 if self.player.muted:
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
200 self.media_player_elt.classList.add("muted")
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 else:
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
202 self.media_player_elt.classList.remove("muted")
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
203
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 def on_mouse_move(self, evt):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
205 self.show_controls()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
206
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 def update_progress(self):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 duration = self.player.duration
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 current_time = duration if self.player.ended else self.player.currentTime
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 self.progress_elt.max = duration
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 self.progress_elt.value = current_time
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 self.progress_elt.text = f"{current_time/duration*100:.02f}"
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 current_time, duration = int(current_time), int(duration)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 if self.timer_mode == "timer":
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 cur_min, cur_sec = divmod(current_time, 60)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 tot_min, tot_sec = divmod(duration, 60)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 self.timer_elt.text = f"{cur_min}:{cur_sec:02d}/{tot_min}:{tot_sec:02d}"
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 elif self.timer_mode == "remaining":
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 rem_min, rem_sec = divmod(duration - current_time, 60)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 self.timer_elt.text = f"{rem_min}:{rem_sec:02d}"
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 else:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 print(f"ERROR: unknown timer mode: {self.timer_mode}")
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
223
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 def hide_controls(self):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 self.controls_elt.classList.add("hidden")
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
226 self.media_player_elt.style.cursor = "none"
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 if self.hide_controls_timer is not None:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 timer.clear_timeout(self.hide_controls_timer)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 self.hide_controls_timer = None
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
230
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 def show_controls(self):
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 self.controls_elt.classList.remove("hidden")
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
233 self.media_player_elt.style.cursor = ""
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 if self.hide_controls_timer is not None:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 timer.clear_timeout(self.hide_controls_timer)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 if self.player.paused:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 self.hide_controls_timer = None
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 else:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 self.hide_controls_timer = timer.set_timeout(self.hide_controls, 3000)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
240
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 @classmethod
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 def do_imports(cls):
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
243 # we do imports (notably for ogv.js) only if they are necessary
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 if cls.imports_done:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 return
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
246 if not cls.native:
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
247 from js_modules import ogv
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
248 cls.ogv = ogv
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
249 if not ogv.OGVCompat.supported('OGVPlayer'):
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
250 print("Can't use OGVPlayer with this browser")
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
251 raise NotImplementedError
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 import template
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
253 global media_player_tpl
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
254 media_player_tpl = template.Template("components/media_player.html")
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 cls.imports_done = True
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
256
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
257 @staticmethod
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
258 def get_source_ext(source):
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
259 try:
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
260 ext = f".{source.rsplit('.', 1)[1].strip()}"
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
261 except IndexError:
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
262 return None
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
263 return ext or None
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
264
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
265 @classmethod
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
266 def install(cls, cant_play):
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
267 cls.native = False
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 ext_list = set()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 for data in cant_play.values():
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 ext_list.update(data['ext'])
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
271 cls.cant_play_ext_list = ext_list
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
272 for to_rpl_vid_elt in document.body.select('video'):
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
273 sources = []
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
274 src = (to_rpl_vid_elt.src or '').strip()
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
275 if src:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 sources.append(src)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
277
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
278 for source_elt in to_rpl_vid_elt.select('source'):
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
279 src = (source_elt.src or '').strip()
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
280 if src:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
281 sources.append(src)
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
282
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
283 # FIXME: we only use first found source
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 try:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 source = sources[0]
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 except IndexError:
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
287 print(f"Can't find any source for following elt:\n{to_rpl_vid_elt.html}")
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
288 continue
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
289
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
290 ext = cls.get_source_ext(source)
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
291
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
292 ext = f".{source.rsplit('.', 1)[1]}"
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
293 if ext is None:
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
294 print(
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
295 "No extension found for source of following elt:\n"
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
296 f"{to_rpl_vid_elt.html}"
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
297 )
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
298 continue
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
299 if ext in ext_list:
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
300 print(f"alternative player will be used for {source!r}")
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
301 cls(sources, to_rpl_vid_elt)
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
302
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
303
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
304 def install_if_needed():
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
305 CONTENT_TYPES = {
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
306 "ogg_theora": {"type": 'video/ogg; codecs="theora"', "ext": [".ogv", ".ogg"]},
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
307 "webm_vp8": {"type": 'video/webm; codecs="vp8, vorbis"', "ext": [".webm"]},
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
308 "webm_vp9": {"type": 'video/webm; codecs="vp9"', "ext": [".webm"]},
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
309 # FIXME: handle audio
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
310 # "ogg_vorbis": {"type": 'audio/ogg; codecs="vorbis"', "ext": ".ogg"},
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
311 }
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
312 test_media_elt = html.VIDEO()
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
313 cant_play = {k:d for k,d in CONTENT_TYPES.items()
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
314 if test_media_elt.canPlayType(d['type']) != "probably"}
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
315
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
316 if cant_play:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
317 cant_play_list = '\n'.join(f"- {k} ({d['type']})" for k, d in cant_play.items())
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
318 print(
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
319 "This browser is incompatible with following content types, using "
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
320 f"alternative:\n{cant_play_list}"
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
321 )
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
322 try:
1344
472267dcd4d8 browser (alt_media_player): native player support + poster + flags + restricted area:
Goffi <goffi@goffi.org>
parents: 1337
diff changeset
323 MediaPlayer.install(cant_play)
1337
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
324 except NotImplementedError:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
325 pass
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
326 else:
f0648005cd11 browser: alternative media player:
Goffi <goffi@goffi.org>
parents:
diff changeset
327 print("This browser can play natively all requested open video/audio formats")