Skip to content

Commit

Permalink
[wasm] MP4 example (#753)
Browse files Browse the repository at this point in the history
  • Loading branch information
noituri authored Sep 17, 2024
1 parent cab0e11 commit facbb86
Show file tree
Hide file tree
Showing 27 changed files with 3,017 additions and 799 deletions.
3 changes: 0 additions & 3 deletions compositor_web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ version = "0.1.0"
edition = "2021"
license = "BUSL-1.1"

[build]
target = "wasm32-unknown-unknown"

[lib]
crate-type = ["cdylib", "rlib"]

Expand Down
2 changes: 1 addition & 1 deletion compositor_web/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod wgpu;
pub fn start() -> Result<(), JsValue> {
console_error_panic_hook::set_once();
tracing_wasm::set_as_global_default();
wasm_log::init(wasm_log::Config::new(log::Level::Trace));
wasm_log::init(wasm_log::Config::new(log::Level::Info));

Ok(())
}
Expand Down
1 change: 0 additions & 1 deletion ts/@live-compositor/browser-render/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
dist
example
src/generated
91 changes: 0 additions & 91 deletions ts/@live-compositor/browser-render/example/src/App.tsx

This file was deleted.

10 changes: 0 additions & 10 deletions ts/@live-compositor/browser-render/example/src/main.tsx

This file was deleted.

21 changes: 0 additions & 21 deletions ts/@live-compositor/browser-render/example/vite.config.ts

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'
import js from '@eslint/js';
import globals from 'globals';
import reactHooks from 'eslint-plugin-react-hooks';
import reactRefresh from 'eslint-plugin-react-refresh';
import tseslint from 'typescript-eslint';

export default tseslint.config(
{ ignores: ['dist'] },
Expand All @@ -19,10 +19,7 @@ export default tseslint.config(
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
'react-refresh/only-export-components': ['warn', { allowConstantExport: true }],
},
},
)
}
);
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint .",
"typecheck": "tsc --noEmit",
"preview": "vite preview"
},
"dependencies": {
"@live-compositor/browser-render": "..",
"@live-compositor/browser-render": "0.1.0-rc.0",
"mp4box": "^0.5.2",
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@
.card {
padding: 2em;
}

.examples-tabs {
display: flexbox;
flex-direction: row;
}

.examples-tabs button {
margin: 5px;
}
29 changes: 29 additions & 0 deletions ts/examples/vite-browser-render/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useState } from 'react';
import './App.css';
import Counter from './examples/Counter';
import MP4Player from './examples/MP4Player';

const EXAMPLES = {
'counter': <Counter />,
'mp4': <MP4Player />
};

function App() {
const [currentExample, setCurrentExample] = useState<keyof typeof EXAMPLES>('counter');

return (
<>
<h1>Browser Renderer Examples</h1>
<div className="examples-tabs">
<button onClick={() => setCurrentExample('counter')}>Counter</button>
<button onClick={() => setCurrentExample('mp4')}>MP4</button>
</div>
<div className="card">
{EXAMPLES[currentExample]}
</div>
</>
);
}


export default App;
73 changes: 73 additions & 0 deletions ts/examples/vite-browser-render/src/examples/Counter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

import { useEffect, useRef, useState } from 'react';
import { useRenderer } from './utils';

function Counter() {
const canvasRef = useRef<HTMLCanvasElement | null>(null);
const [count, setCount] = useState(0);
const renderer = useRenderer();

useEffect(() => {
if (renderer == null) {
return;
}

renderer.updateScene(
'output',
{ width: 256, height: 256 },
{
type: 'view',
children: [
{
type: 'view',
direction: 'column',
top: 0,
left: 50,
children: [
{
type: 'image',
image_id: 'img',
},
{
type: 'text',
font_size: 30,
font_family: 'Noto Sans',
text: `Count is ${count}`,
align: 'center',
},
],
},
],
}
);

let pts = 0;
const renderInterval = setInterval(() => {
const input = {
ptsMs: pts,
frames: {},
};
const outputs = renderer.render(input);
const frame = outputs.frames['output'];
const resolution = frame.resolution;
const canvas = canvasRef.current;
const context = canvas!.getContext('2d');
context?.putImageData(new ImageData(frame!.data, resolution.width, resolution.height), 0, 0);

pts += 30;
}, 30);

return () => clearInterval(renderInterval);
}, [renderer, count]);

return (
<>
<div className="card">
<canvas ref={canvasRef} width={300} height={300}></canvas>
<button onClick={() => setCount(count => count + 1)}>Count +1</button>
</div>
</>
);
}

export default Counter;
Loading

0 comments on commit facbb86

Please sign in to comment.