Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tune HLS Video Parameters #36

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ pub mod wasp {
extern "C" {
type WaspHlsPlayer;

fn buildPlayer(videoElement: JsValue, config: JsValue) -> WaspHlsPlayer;
fn buildPlayer(
videoElement: JsValue,
initialBandwidth: f64,
config: JsValue,
) -> WaspHlsPlayer;

#[wasm_bindgen(method)]
fn load(this: &WaspHlsPlayer, url: &str);
Expand Down Expand Up @@ -42,10 +46,24 @@ pub mod wasp {
pub struct WaspHlsPlayerW(WaspHlsPlayer);

impl WaspHlsPlayerW {
pub fn new(video_element: &HtmlElement<Video>, config: Option<WaspHlsConfig>) -> Self {
pub fn new_recommended(video_element: &HtmlElement<Video>) -> Self {
let config = WaspHlsConfig {
buffer_goal: Some(15.),
..Default::default()
};
Self::new(video_element, Some(config), None)
}

pub fn new(
video_element: &HtmlElement<Video>,
config: Option<WaspHlsConfig>,
initial_bandwidth: Option<usize>,
) -> Self {
let video_raw: &JsValue = video_element.deref();
let conf = serde_wasm_bindgen::to_value(&config).unwrap();
let wasp = buildPlayer(video_raw.clone(), conf);
// Default estimate for 720p
let initial_bandwidth = initial_bandwidth.unwrap_or(6500000);
let wasp = buildPlayer(video_raw.clone(), initial_bandwidth as f64, conf);
Self(wasp)
}

Expand Down
5 changes: 3 additions & 2 deletions src/js/wasp-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import WaspHlsPlayer from "https://esm.sh/[email protected]";

export function buildPlayer(videoElement, config) {
export function buildPlayer(videoElement, initialBandwidth, config) {
const wasp = new WaspHlsPlayer(videoElement, config);
wasp.initialize({
workerUrl: "/wasp/worker.js",
wasmUrl: "/wasp/wasp_hls_bg.wasm",
initialBandwidth,
});

return wasp;
}
}
2 changes: 1 addition & 1 deletion src/page/post_view/video_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn HlsVideo(video_ref: NodeRef<Video>, allow_show: RwSignal<bool>) -> impl I
let video = video_ref.get()?;
let video = video.classes("object-contain h-full");
log::debug!("initializing wasp player");
let wasp_p = WaspHlsPlayerW::new(&video, None);
let wasp_p = WaspHlsPlayerW::new_recommended(&video);
video.set_muted(true);
video.set_loop(true);
video.set_autoplay(true);
Expand Down