Skip to content

Commit

Permalink
import Moq from @kixelated/moq/video (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
kixelated authored Dec 13, 2024
1 parent c980343 commit 3453eb2
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,31 @@ For example:

```html
<script type="module">
import '@kixelated/moq'
import '@kixelated/moq/video'
</script>

<moq-video src="https://relay.quic.video/demo/bbb"></moq-video>
```

Because it uses WASM, it's not part of the main workspace and requires some extra steps to build.
The package is a gross frankenstein of Rust+Typescript.
To make changes, you'll need to install (Bun)[https://bun.sh/] and then run:

```sh
cd moq-web
bun i
bun dev
```

This will start a development server on `http://localhost:3000`.
There's two separate compilation steps, the first building with `wasm-pack` and the second bundling with `rspack`.
You can also test the package locally by linking.
Replace `bun` with your favorite package manager; it might work.

```sh
bun pack
bun link

# In your other package
bun link @kixelated/moq
```

See the [moq-web README](moq-web/README.md) for more information.

## moq-karp
Expand Down
2 changes: 1 addition & 1 deletion moq-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "moq-web"
authors = ["Luke Curley <[email protected]>"]
edition = "2021"
version = "0.3.7"
version = "0.3.8"
license = "MIT OR Apache-2.0"
repository = "https://github.com/kixelated/moq-web"
description = "Web implementation for MoQ utilizing WebAssembly+Typescript"
Expand Down
20 changes: 20 additions & 0 deletions moq-web/src/bridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as Comlink from "comlink";
import type { Api } from "./worker";

// Create a new worker instance
// We wait until the worker is fully initialized before we return the proxy.
function init(): Promise<Comlink.Remote<Api>> {
return new Promise((resolve) => {
const worker = new Worker(new URL("./worker", import.meta.url), { type: "module" });
worker.addEventListener(
"message",
(event) => {
const proxy: Comlink.Remote<Api> = Comlink.wrap(worker);
resolve(proxy);
},
{ once: true },
);
});
}

export { init };
2 changes: 1 addition & 1 deletion moq-web/src/demo/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { MoqVideoElement } from "../element/video";
export { MoqVideoElement } from "..";
3 changes: 2 additions & 1 deletion moq-web/src/element/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { MoqVideoElement } from "./video";
import MoqVideoElement from "./video";
export { MoqVideoElement };
2 changes: 1 addition & 1 deletion moq-web/src/element/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as Moq from "..";
// Supports a subset of the <video> element API.
// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video
// Also: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement
export class MoqVideoElement extends HTMLElement implements HTMLVideoElement {
export default class MoqVideoElement extends HTMLElement implements HTMLVideoElement {
#watch?: Moq.Watch;
#canvas?: OffscreenCanvas;

Expand Down
7 changes: 5 additions & 2 deletions moq-web/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
export { Watch } from "./watch";

// NOTE: You can also use the custom elements
// They are in a separate folder since they require side-effects to function fully.
// import "@kixelated/moq/element/video"
// They are in a separate path since they require side-effects to function fully.
// import "@kixelated/moq/video"

// We still export them here for convenience
export { MoqVideoElement } from "./element";
16 changes: 0 additions & 16 deletions moq-web/src/main.ts

This file was deleted.

14 changes: 7 additions & 7 deletions moq-web/src/watch.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import * as Comlink from "comlink";
import { init } from "./main";
import { init } from "./bridge";
import type * as Api from "./worker";

export class Watch {
#inner: Promise<Comlink.Remote<Api.Watch>>;
#bridge: Promise<Comlink.Remote<Api.Watch>>;

constructor(src: string) {
this.#inner = init.then((api) => api.watch(src));
this.#bridge = init().then((api) => api.watch(src));
}

async pause(value: boolean) {
await (await this.#inner).pause(value);
await (await this.#bridge).pause(value);
}

async volume(value: number) {
await (await this.#inner).volume(value);
await (await this.#bridge).volume(value);
}

async render(value: HTMLCanvasElement | OffscreenCanvas) {
const canvas = value instanceof HTMLCanvasElement ? value.transferControlToOffscreen() : value;

await (await this.#inner).render(Comlink.transfer(canvas, [canvas]));
await (await this.#bridge).render(Comlink.transfer(canvas, [canvas]));
}

async close() {
await (await this.#inner).close();
await (await this.#bridge).close();
}
}
12 changes: 2 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,12 @@
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
},
"./element": {
"types": "./dist/src/element/index.d.ts",
"import": "./dist/src/element/index.js"
},
"./element/video": {
"./video": {
"types": "./dist/src/element/video.d.ts",
"import": "./dist/src/element/video.js"
},
"./element/karp": {
"types": "./dist/src/element/karp.d.ts",
"import": "./dist/src/element/karp.js"
}
},
"sideEffects": ["./dist/src/element/index.js", "./dist/src/element/video.js"],
"sideEffects": ["./dist/src/element/video.js"],
"files": ["dist", "pkg", "README.md", "LICENSE*"],
"scripts": {
"pack": "bun run pack:wasm && rm pkg/.gitignore && tsc",
Expand Down

0 comments on commit 3453eb2

Please sign in to comment.