Skip to content

Commit 373f883

Browse files
Urhengulaslu-zero
authored andcommittedJun 11, 2020
Add rav1e_js as subcrate
Changes: * add also example website * make alert() catchable * let caller define # of frames to encode * move alert to wrapper function * add license headers * add tests
0 parents  commit 373f883

14 files changed

+360
-0
lines changed
 

‎.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/target
2+
**/*.rs.bk
3+
Cargo.lock
4+
bin/
5+
pkg/
6+
wasm-pack.log

‎Cargo.toml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "rav1e_js"
3+
version = "0.1.0"
4+
authors = ["Urhengulas <johann.hemmann@code.berlin>"]
5+
edition = "2018"
6+
description = "JavaScript bindings for rav1e"
7+
repository = "https://github.com/xiph/rav1e/"
8+
license= "BSD-2-Clause"
9+
10+
[lib]
11+
crate-type = ["cdylib", "rlib"]
12+
13+
[features]
14+
default = ["console_error_panic_hook"]
15+
16+
[dependencies]
17+
rav1e = { path = ".." }
18+
wasm-bindgen = "0.2.63"
19+
web-sys = {version = "0.3", features = ["console"]}
20+
21+
# The `console_error_panic_hook` crate provides better debugging of panics by
22+
# logging them with `console.error`. This is great for development, but requires
23+
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
24+
# code size when deploying.
25+
console_error_panic_hook = { version = "0.1.6", optional = true }
26+
27+
[dev-dependencies]
28+
wasm-bindgen-test = "0.3.13"

‎README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# rav1e_js
2+
3+
## Install `wasm-pack`
4+
5+
```bash
6+
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
7+
```
8+
9+
## Usage
10+
11+
```bash
12+
# Build (emits wasm-, js/ts-files to pkg/)
13+
wasm-pack build
14+
15+
# Test in Headless Browser
16+
wasm-pack test --headless --firefox
17+
```
18+
19+
## Example Website
20+
```bash
21+
# make sure you have the latest npm version
22+
npm install -g npm@latest
23+
24+
cd rav1e_js
25+
wasm-pack build
26+
27+
cd www/
28+
npm install
29+
npm start
30+
# website served at localhost:8080
31+
# check the developer-console
32+
```

‎src/lib.rs

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}

‎src/utils.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
pub fn set_panic_hook() {
11+
// When the `console_error_panic_hook` feature is enabled, we can call the
12+
// `set_panic_hook` function at least once during initialization, and then
13+
// we will get better error messages if our code ever panics.
14+
//
15+
// For more details see
16+
// https://github.com/rustwasm/console_error_panic_hook#readme
17+
#[cfg(feature = "console_error_panic_hook")]
18+
console_error_panic_hook::set_once();
19+
}
20+
21+
/// A macro to provide `println!(..)`-style syntax for `console.log` logging.
22+
#[macro_export]
23+
macro_rules! log {
24+
( $( $t:tt )* ) => {
25+
web_sys::console::log_1(&format!( $( $t )* ).into());
26+
}
27+
}

‎tests/node.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
//! Test suite for node.
11+
12+
#![cfg(target_arch = "wasm32")]
13+
14+
use wasm_bindgen_test::*;
15+
16+
#[wasm_bindgen_test]
17+
fn simple_encoding() {
18+
rav1e_js::simple_encoding_without_alert(30);
19+
}

‎tests/web.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
//! Test suite for the Web and headless browsers.
11+
12+
#![cfg(target_arch = "wasm32")]
13+
14+
use wasm_bindgen_test::*;
15+
16+
wasm_bindgen_test_configure!(run_in_browser);
17+
18+
#[wasm_bindgen_test]
19+
fn simple_encoding() {
20+
rav1e_js::simple_encoding_without_alert(30);
21+
}

‎www/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
dist
3+
package-lock.json

‎www/bootstrap.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
// A dependency graph that contains any wasm must all be imported
11+
// asynchronously. This `bootstrap.js` file does the single async import, so
12+
// that no one else needs to worry about it again.
13+
import("./index.ts")
14+
.then("Loaded wasm-module")
15+
.catch(e => console.error("Error importing `index.ts`:", e));

‎www/index.html

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!--
2+
Copyright (c) 2020, The rav1e contributors. All rights reserved
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+
11+
<!DOCTYPE html>
12+
<html>
13+
14+
<head>
15+
<meta charset="utf-8">
16+
<title>Hello rav1e_js!</title>
17+
</head>
18+
19+
<body>
20+
<script src="./bootstrap.js"></script>
21+
</body>
22+
23+
</html>

‎www/index.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
import { simple_encoding } from "rav1e";
11+
12+
simple_encoding(30);

‎www/package.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "rav1e_js-www",
3+
"version": "0.1.0",
4+
"description": "",
5+
"main": "index.ts",
6+
"scripts": {
7+
"build": "webpack --config webpack.config.js",
8+
"start": "webpack-dev-server"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/xiph/rav1e.git"
13+
},
14+
"keywords": [
15+
"webassembly",
16+
"wasm",
17+
"rust",
18+
"webpack"
19+
],
20+
"author": "Urhengulas <johann.hemmann@code.berlin>",
21+
"license": "BSD-2-Clause",
22+
"bugs": {
23+
"url": "https://github.com/xiph/rav1e/issues"
24+
},
25+
"homepage": "https://github.com/xiph/rav1e/tree/master/rav1e_js#readme",
26+
"dependencies": {
27+
"rav1e": "file:../pkg",
28+
"typescript": "^3.9.5"
29+
},
30+
"devDependencies": {
31+
"copy-webpack-plugin": "^5.0.0",
32+
"ts-loader": "^7.0.5",
33+
"webpack": "^4.29.3",
34+
"webpack-cli": "^3.1.0",
35+
"webpack-dev-server": "^3.1.5"
36+
}
37+
}

‎www/tsconfig.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"esModuleInterop": true,
5+
"importHelpers": true,
6+
"module": "commonjs",
7+
"noImplicitAny": true,
8+
"outDir": "./dist",
9+
"sourceMap": true,
10+
"strict": true,
11+
"target": "ES2017",
12+
"typeRoots": [
13+
"./node_modules/@types",
14+
"./node_modules"
15+
],
16+
"types": []
17+
},
18+
"include": [
19+
"**/*.ts"
20+
],
21+
"exclude": [
22+
"node_modules"
23+
]
24+
}

‎www/webpack.config.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const CopyWebpackPlugin = require("copy-webpack-plugin");
2+
const path = require('path');
3+
4+
module.exports = {
5+
entry: "./bootstrap.js",
6+
output: {
7+
path: path.resolve(__dirname, "dist"),
8+
filename: "bootstrap.js",
9+
},
10+
mode: "development",
11+
plugins: [
12+
new CopyWebpackPlugin(['index.html'])
13+
],
14+
module: {
15+
rules: [
16+
{ test: /\.ts$/, use: 'ts-loader' }
17+
]
18+
}
19+
};

0 commit comments

Comments
 (0)
Please sign in to comment.