Skip to content

Commit

Permalink
fix(post_view): tune HLS video parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
rupansh committed Jan 15, 2024
1 parent cbb3e84 commit 74954ca
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
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

0 comments on commit 74954ca

Please sign in to comment.