@gradio/video 0.10.3 → 0.11.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,8 @@
5
5
 
6
6
  import { resolve_wasm_src } from "@gradio/wasm/svelte";
7
7
 
8
+ import Hls from "hls.js";
9
+
8
10
  export let src: HTMLVideoAttributes["src"] = undefined;
9
11
 
10
12
  export let muted: HTMLVideoAttributes["muted"] = undefined;
@@ -19,10 +21,12 @@
19
21
 
20
22
  export let node: HTMLVideoElement | undefined = undefined;
21
23
  export let loop: boolean;
24
+ export let is_stream;
22
25
 
23
26
  export let processingVideo = false;
24
27
 
25
28
  let resolved_src: typeof src;
29
+ let stream_active = false;
26
30
 
27
31
  // The `src` prop can be updated before the Promise from `resolve_wasm_src` is resolved.
28
32
  // In such a case, the resolved value for the old `src` has to be discarded,
@@ -46,6 +50,53 @@
46
50
  }
47
51
 
48
52
  const dispatch = createEventDispatcher();
53
+
54
+ function load_stream(
55
+ src: string | null | undefined,
56
+ is_stream: boolean,
57
+ node: HTMLVideoElement | undefined
58
+ ): void {
59
+ if (!src || !is_stream) return;
60
+ if (!node) return;
61
+ if (Hls.isSupported() && !stream_active) {
62
+ const hls = new Hls({
63
+ maxBufferLength: 1, // 0.5 seconds (500 ms)
64
+ maxMaxBufferLength: 1, // Maximum max buffer length in seconds
65
+ lowLatencyMode: true // Enable low latency mode
66
+ });
67
+ hls.loadSource(src);
68
+ hls.attachMedia(node);
69
+ hls.on(Hls.Events.MANIFEST_PARSED, function () {
70
+ (node as HTMLVideoElement).play();
71
+ });
72
+ hls.on(Hls.Events.ERROR, function (event, data) {
73
+ console.error("HLS error:", event, data);
74
+ if (data.fatal) {
75
+ switch (data.type) {
76
+ case Hls.ErrorTypes.NETWORK_ERROR:
77
+ console.error(
78
+ "Fatal network error encountered, trying to recover"
79
+ );
80
+ hls.startLoad();
81
+ break;
82
+ case Hls.ErrorTypes.MEDIA_ERROR:
83
+ console.error("Fatal media error encountered, trying to recover");
84
+ hls.recoverMediaError();
85
+ break;
86
+ default:
87
+ console.error("Fatal error, cannot recover");
88
+ hls.destroy();
89
+ break;
90
+ }
91
+ }
92
+ });
93
+ stream_active = true;
94
+ }
95
+ }
96
+
97
+ $: src, (stream_active = false);
98
+
99
+ $: load_stream(src, is_stream, node);
49
100
  </script>
50
101
 
51
102
  <!--
@@ -51,13 +51,14 @@
51
51
  </script>
52
52
 
53
53
  <BlockLabel {show_label} Icon={Video} label={label || "Video"} />
54
- {#if value === null || value.url === undefined}
54
+ {#if !value || value.url === undefined}
55
55
  <Empty unpadded_box={true} size="large"><Video /></Empty>
56
56
  {:else}
57
57
  {#key value.url}
58
58
  <Player
59
59
  src={value.url}
60
60
  subtitle={subtitle?.url}
61
+ is_stream={value.is_stream}
61
62
  {autoplay}
62
63
  on:play
63
64
  on:pause
@@ -73,7 +74,12 @@
73
74
  {/key}
74
75
  <div class="icon-buttons" data-testid="download-div">
75
76
  {#if show_download_button}
76
- <DownloadLink href={value.url} download={value.orig_name || value.path}>
77
+ <DownloadLink
78
+ href={value.is_stream
79
+ ? value.url?.replace("playlist.m3u8", "playlist-file")
80
+ : value.url}
81
+ download={value.orig_name || value.path}
82
+ >
77
83
  <IconButton Icon={Download} label="Download" />
78
84
  </DownloadLink>
79
85
  {/if}