Skip to content

Commit

Permalink
Use Deno FFI (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy authored Feb 23, 2022
1 parent 555d0db commit 458a5cb
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 121 deletions.
24 changes: 12 additions & 12 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Release CI
name: CI

on: [push, pull_request]

Expand Down Expand Up @@ -26,6 +26,13 @@ jobs:
toolchain: stable
override: true

- uses: denoland/setup-deno@v1
with:
deno-version: canary

- name: Install deno_bindgen
run: deno install -Afq -n deno_bindgen https://raw.githubusercontent.com/littledivy/deno_bindgen/main/cli.ts

- name: Log versions
run: |
rustc --version
Expand All @@ -46,18 +53,11 @@ jobs:
path: target
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}

- name: Remove Some Cache
if: matrix.os == 'windows-latest'
run: |
rm target/release/gn_root -Recurse -ErrorAction Ignore
rm target/debug/gn_root -Recurse -ErrorAction Ignore
- name: Install webkit2gtk
- name: Install libasound2-dev
if: matrix.os == 'ubuntu-latest'
run: |
sudo apt-get update
sudo apt-get install libasound2-dev
- name: Run cargo build
uses: actions-rs/cargo@v1
with:
command: build
args: --release
- name: Build
run: deno_bindgen --release
5 changes: 0 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ jobs:
path: target
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}

- name: Remove Some Cache
if: matrix.os == 'windows-latest'
run: |
rm target/release/gn_root -Recurse -ErrorAction Ignore
rm target/debug/gn_root -Recurse -ErrorAction Ignore
- name: Install webkit2gtk
if: matrix.os == 'ubuntu-latest'
run: |
Expand Down
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,4 @@ crate-type = ["cdylib"]

[dependencies]
rodio = "0.12.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
futures = "0.3.4"
deno_core = "0.61.0"
deno_bindgen = "0.5.0"
33 changes: 33 additions & 0 deletions bindings/bindings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Auto-generated with deno_bindgen
import { CachePolicy, prepare } from "https://deno.land/x/[email protected]/plug.ts"
function encode(v: string | Uint8Array): Uint8Array {
if (typeof v !== "string") return v
return new TextEncoder().encode(v)
}
function decode(v: Uint8Array): string {
return new TextDecoder().decode(v)
}
function readPointer(v: any): Uint8Array {
const ptr = new Deno.UnsafePointerView(v as Deno.UnsafePointer)
const lengthBe = new Uint8Array(4)
const view = new DataView(lengthBe.buffer)
ptr.copyInto(lengthBe, 0)
const buf = new Uint8Array(view.getUint32(0))
ptr.copyInto(buf, 4)
return buf
}
const opts = {
name: "deno_audio",
url: (new URL("../target/release", import.meta.url)).toString(),
policy: undefined,
}
const _lib = await prepare(opts, {
play: { parameters: ["pointer", "usize"], result: "void", nonblocking: true },
})

export function play(a0: string) {
const a0_buf = encode(a0)
let rawResult = _lib.symbols.play(a0_buf, a0_buf.byteLength)
const result = rawResult
return result
}
10 changes: 0 additions & 10 deletions core_type.ts

This file was deleted.

14 changes: 0 additions & 14 deletions detect.ts

This file was deleted.

40 changes: 9 additions & 31 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,15 @@
//! ```
use std::io::BufReader;
use deno_bindgen::deno_bindgen;

use deno_core::plugin_api::Interface;
use deno_core::plugin_api::Op;
use deno_core::plugin_api::ZeroCopyBuf;
use futures::future::FutureExt;
#[deno_bindgen(non_blocking)]
pub fn play(filename: &str) {
let (_stream, handle) = rodio::OutputStream::try_default().unwrap();
let sink = rodio::Sink::try_new(&handle).unwrap();

#[no_mangle]
pub fn deno_plugin_init(interface: &mut dyn Interface) {
interface.register_op("play", op_play);
}

fn op_play(_interface: &mut dyn Interface, zero_copy: &mut [ZeroCopyBuf]) -> Op {
let data = &zero_copy[0][..];
let data_str = std::str::from_utf8(&data[..]).unwrap().to_string();
let fut = async move {
let (tx, rx) = futures::channel::oneshot::channel::<Result<(), ()>>();
std::thread::spawn(move || {
let (_stream, handle) = rodio::OutputStream::try_default().unwrap();
let sink = rodio::Sink::try_new(&handle).unwrap();

let file = std::fs::File::open(&data_str).unwrap();
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());

sink.sleep_until_end();
tx.send(Ok(()));
});
let result_box = serde_json::to_vec(&rx.await.unwrap())
.unwrap()
.into_boxed_slice();
result_box
};

Op::Async(fut.boxed())
let file = std::fs::File::open(&filename).unwrap();
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());
// `play` runs from a different thread.
sink.sleep_until_end();
}
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { play } from "./op.ts";
export { play } from "./bindings/bindings.ts";
20 changes: 0 additions & 20 deletions op.ts

This file was deleted.

24 changes: 0 additions & 24 deletions plugin.ts

This file was deleted.

0 comments on commit 458a5cb

Please sign in to comment.