mirror of
https://github.com/darksaid98/mediaplex.git
synced 2026-12-01 14:38:45 +00:00
feat: implement async probe
This commit is contained in:
1 parent
3f96a50859
commit
ed309d9870
6 files changed
+57
-70
No files matched your search
@@ -12,6 +12,7 @@ napi = { git = "https://github.com/napi-rs/napi-rs", default-features = false, f
|
||||
"napi4",
|
||||
] }
|
||||
napi-derive = { git = "https://github.com/napi-rs/napi-rs" }
|
||||
samplerate = "0.2.4"
|
||||
symphonia = { version = "0.5.3", features = ["all"] }
|
||||
|
||||
[build-dependencies]
|
||||
|
||||
Vendored
+12
-1
@@ -17,9 +17,20 @@ export interface Metadata {
|
||||
bpm: number | null;
|
||||
}
|
||||
|
||||
export interface ProbeStreamOptions {
|
||||
/**
|
||||
* If true, mediaplex will probe the stream synchronously. Defaults to false.
|
||||
*/
|
||||
sync?: boolean;
|
||||
/**
|
||||
* The maximum number of bytes to read from the stream. Defaults to 2MB.
|
||||
*/
|
||||
probeSize?: number;
|
||||
}
|
||||
|
||||
export * from './js-binding';
|
||||
export function probeStream(
|
||||
stream: Readable,
|
||||
probeSize?: number
|
||||
options?: ProbeStreamOptions
|
||||
): Promise<StreamProbeResult>;
|
||||
export function readMetadata(result: ProbeResult): Metadata;
|
||||
@@ -1,6 +1,5 @@
|
||||
const binding = require('./js-binding');
|
||||
const { Readable } = require('stream');
|
||||
const { Worker } = require('worker_threads');
|
||||
|
||||
/**
|
||||
* @typedef StreamProbeResult
|
||||
@@ -11,15 +10,20 @@ const { Worker } = require('worker_threads');
|
||||
/**
|
||||
* Attempt to probe a Readable stream
|
||||
* @param {Readable} stream The readable stream to probe
|
||||
* @param {number} [probeSize] The number of bytes to read. Defaults to 10 MB
|
||||
* @param {Object} options The options
|
||||
* @param {number} [options.probeSize=2 * 1024 * 1024] The amount of bytes to read from the stream. Defaults to 2 MB.
|
||||
* @param {boolean} [options.sync=false] Whether to probe synchronously. Defaults to false.
|
||||
* @returns {Promise<StreamProbeResult>}
|
||||
*/
|
||||
async function probeStream(
|
||||
stream,
|
||||
probeSize = 1024 * 1024 * 10
|
||||
options = {
|
||||
probeSize: 2 * 1024 * 1024,
|
||||
sync: false
|
||||
}
|
||||
) {
|
||||
const { probeSize = 2 * 1024 * 1024, sync = false } = options;
|
||||
return new Promise((resolve, reject) => {
|
||||
// preconditions
|
||||
if (stream.readableObjectMode) {
|
||||
reject(new Error('Cannot probe a readable stream in object mode'));
|
||||
return;
|
||||
@@ -64,7 +68,8 @@ async function probeStream(
|
||||
|
||||
const onClose = async () => {
|
||||
try {
|
||||
const result = await toThread(readBuffer);
|
||||
const probeInner = sync ? probeSync : probe;
|
||||
const result = await probeInner(readBuffer);
|
||||
if (result != null) resolved = result;
|
||||
} catch { }
|
||||
|
||||
@@ -88,65 +93,6 @@ async function probeStream(
|
||||
});
|
||||
}
|
||||
|
||||
async function toThread(buffer) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const workerScript = ';('.concat((function MEDIAPLEX_PROBE_METADATA() {
|
||||
const { probe } = require('./js-binding');
|
||||
const { parentPort, workerData } = require('worker_threads');
|
||||
|
||||
if (!parentPort || !workerData) {
|
||||
throw new Error('Could not probe on worker thread');
|
||||
}
|
||||
|
||||
try {
|
||||
const result = probe(workerData);
|
||||
parentPort.postMessage({
|
||||
error: false,
|
||||
value: result
|
||||
});
|
||||
} catch (err) {
|
||||
parentPort.postMessage({
|
||||
error: true,
|
||||
value: err
|
||||
});
|
||||
} finally {
|
||||
process.exit(0);
|
||||
}
|
||||
}).toString()).concat(')();');
|
||||
|
||||
const worker = new Worker(workerScript, {
|
||||
eval: true,
|
||||
workerData: buffer
|
||||
});
|
||||
|
||||
worker.unref();
|
||||
|
||||
const terminator = setTimeout(() => {
|
||||
if (markAsResolved) return;
|
||||
worker.terminate().catch(() => null);
|
||||
}, 5000).unref();
|
||||
|
||||
const markAsResolved = () => clearTimeout(terminator);
|
||||
|
||||
worker.once('message', (result) => {
|
||||
markAsResolved();
|
||||
if (result.error) {
|
||||
reject(result.value);
|
||||
} else {
|
||||
resolve(result.value);
|
||||
}
|
||||
|
||||
worker.terminate().catch(() => null);
|
||||
});
|
||||
|
||||
worker.once('error', (e) => {
|
||||
markAsResolved();
|
||||
reject(e);
|
||||
worker.terminate().catch(() => null);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads metadata tags
|
||||
* @param {import('./js-binding').ProbeResult} result The probe result to read metadata from
|
||||
@@ -190,12 +136,13 @@ function readMetadata(result) {
|
||||
return res;
|
||||
}
|
||||
|
||||
const { CodecType, probe, OpusEncoder, getOpusVersion } = binding;
|
||||
const { CodecType, probe, probeSync, OpusEncoder, getOpusVersion } = binding;
|
||||
const { version } = require('./package.json');
|
||||
|
||||
module.exports = {
|
||||
CodecType,
|
||||
probe,
|
||||
probeSync,
|
||||
probeStream,
|
||||
readMetadata,
|
||||
OpusEncoder,
|
||||
|
||||
Vendored
+2
-1
@@ -92,7 +92,8 @@ export interface MetadataField {
|
||||
name: string
|
||||
value: string
|
||||
}
|
||||
export function probe(data: Buffer): ProbeResult
|
||||
export function probe(data: Buffer): Promise<ProbeResult>
|
||||
export function probeSync(data: Buffer): ProbeResult
|
||||
export type JsOpusEncoder = OpusEncoder
|
||||
export class OpusEncoder {
|
||||
constructor(sampleRate: number, channels: number)
|
||||
|
||||
+2
-1
@@ -252,9 +252,10 @@ if (!nativeBinding) {
|
||||
throw new Error(`Failed to load native binding`)
|
||||
}
|
||||
|
||||
const { getOpusVersion, OpusEncoder, CodecType, probe } = nativeBinding
|
||||
const { getOpusVersion, OpusEncoder, CodecType, probe, probeSync } = nativeBinding
|
||||
|
||||
module.exports.getOpusVersion = getOpusVersion
|
||||
module.exports.OpusEncoder = OpusEncoder
|
||||
module.exports.CodecType = CodecType
|
||||
module.exports.probe = probe
|
||||
module.exports.probeSync = probeSync
|
||||
+28
-2
@@ -1,5 +1,5 @@
|
||||
use napi::bindgen_prelude::*;
|
||||
use napi::Result;
|
||||
use napi::{Result, Task};
|
||||
use symphonia::core::meta::MetadataRevision;
|
||||
use symphonia::core::{
|
||||
audio::Channels, codecs::*, io::MediaSourceStream, probe::Hint, units::TimeBase,
|
||||
@@ -124,8 +124,34 @@ pub struct MetadataField {
|
||||
pub value: String,
|
||||
}
|
||||
|
||||
#[napi(catch_unwind, ts_return_type = "Promise<ProbeResult>")]
|
||||
pub fn probe(data: Buffer) -> AsyncTask<ProbeTask> {
|
||||
return AsyncTask::new(ProbeTask { input: data });
|
||||
}
|
||||
|
||||
#[napi(catch_unwind)]
|
||||
pub fn probe(data: Buffer) -> Result<JsProbeResult> {
|
||||
pub fn probe_sync(data: Buffer) -> Result<JsProbeResult> {
|
||||
return probe_inner(data.to_vec());
|
||||
}
|
||||
|
||||
pub struct ProbeTask {
|
||||
input: Buffer,
|
||||
}
|
||||
|
||||
impl Task for ProbeTask {
|
||||
type Output = JsProbeResult;
|
||||
type JsValue = JsProbeResult;
|
||||
|
||||
fn compute(&mut self) -> Result<Self::Output> {
|
||||
probe_inner(self.input.to_vec())
|
||||
}
|
||||
|
||||
fn resolve(&mut self, env: Env, output: Self::Output) -> Result<Self::JsValue> {
|
||||
Ok(output)
|
||||
}
|
||||
}
|
||||
|
||||
fn probe_inner(data: Vec<u8>) -> Result<JsProbeResult> {
|
||||
// Create a Cursor over the data.
|
||||
let cursor = std::io::Cursor::new(data.to_vec());
|
||||
|
||||
|
||||
Reference in new issue
Block a user