|
| 1 | +// Copyright (c) 2020, The rav1e contributors. All rights reserved |
| 2 | +// |
| 3 | +// This source code is subject to the terms of the BSD 2 Clause License and |
| 4 | +// the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 5 | +// was not distributed with this source code in the LICENSE file, you can |
| 6 | +// obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 7 | +// Media Patent License 1.0 was not distributed with this source code in the |
| 8 | +// PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
| 9 | + |
| 10 | +mod utils; |
| 11 | + |
| 12 | +use rav1e::config::SpeedSettings; |
| 13 | +use rav1e::prelude::*; |
| 14 | + |
| 15 | +use wasm_bindgen::prelude::*; |
| 16 | + |
| 17 | +#[wasm_bindgen] |
| 18 | +extern { |
| 19 | + #[wasm_bindgen(catch)] |
| 20 | + fn alert(s: &str) -> Result<(), JsValue>; |
| 21 | +} |
| 22 | + |
| 23 | +/// Encode the same tiny blank frame 30 times |
| 24 | +#[wasm_bindgen] |
| 25 | +pub fn simple_encoding(repeats: u32) { |
| 26 | + let msg = simple_encoding_without_alert(repeats); |
| 27 | + |
| 28 | + match alert(&*msg) { |
| 29 | + Ok(_) => {} |
| 30 | + Err(_) => log!("Error calling alert()"), |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +pub fn simple_encoding_without_alert(repeats: u32) -> String { |
| 35 | + // Enable logging for panics |
| 36 | + utils::set_panic_hook(); |
| 37 | + |
| 38 | + let mut enc = EncoderConfig::default(); |
| 39 | + enc.width = 64; |
| 40 | + enc.height = 96; |
| 41 | + enc.speed_settings = SpeedSettings::from_preset(9); |
| 42 | + |
| 43 | + let cfg = Config::new().with_encoder_config(enc); |
| 44 | + let mut ctx: Context<u16> = cfg.new_context().unwrap(); |
| 45 | + log!("Instantiated new encoder (config: {:?})", cfg); |
| 46 | + |
| 47 | + let f = ctx.new_frame(); |
| 48 | + log!("Generated new frame: {:?}", f); |
| 49 | + |
| 50 | + for i in 0..repeats { |
| 51 | + log!("Sending frame {}", i); |
| 52 | + match ctx.send_frame(f.clone()) { |
| 53 | + Ok(_) => {} |
| 54 | + Err(e) => match e { |
| 55 | + EncoderStatus::EnoughData => { |
| 56 | + log!("Warn: Unable to append frame {} to the internal queue", i); |
| 57 | + } |
| 58 | + _ => { |
| 59 | + panic!("Unable to send frame {}", i); |
| 60 | + } |
| 61 | + }, |
| 62 | + } |
| 63 | + } |
| 64 | + log!("Successfully send {} frames to the encoder", repeats); |
| 65 | + |
| 66 | + ctx.flush(); |
| 67 | + log!("Flush encoder"); |
| 68 | + |
| 69 | + loop { |
| 70 | + match ctx.receive_packet() { |
| 71 | + Ok(packet) => { |
| 72 | + // Mux the packet. |
| 73 | + log!("Received packet: {}", packet) |
| 74 | + } |
| 75 | + Err(EncoderStatus::Encoded) => { |
| 76 | + // A frame was encoded without emitting a packet. This is normal, |
| 77 | + // just proceed as usual. |
| 78 | + } |
| 79 | + Err(EncoderStatus::LimitReached) => { |
| 80 | + // All frames have been encoded. Time to break out of the loop. |
| 81 | + break; |
| 82 | + } |
| 83 | + Err(EncoderStatus::NeedMoreData) => { |
| 84 | + // The encoder has requested additional frames. Push the next frame |
| 85 | + // in, or flush the encoder if there are no frames left (on None). |
| 86 | + ctx.flush(); |
| 87 | + } |
| 88 | + Err(e) => { |
| 89 | + panic!(e); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + format!("Done encoding the same tiny blank frame {} times.", repeats) |
| 94 | +} |
0 commit comments