mirror of
https://github.com/darksaid98/mediaplex.git
synced 2026-12-01 14:38:45 +00:00
feat(src/opus): updated bindings
This commit is contained in:
2 files changed
+637
-1045
No files matched your search
+343
-751
File diff suppressed because it is too large.
Load diff
+294
-294
@@ -1,294 +1,294 @@
|
|||||||
use core::slice;
|
use core::slice;
|
||||||
|
|
||||||
use napi::bindgen_prelude::*;
|
use napi::bindgen_prelude::*;
|
||||||
use napi::Error;
|
use napi::Error;
|
||||||
use napi::Result;
|
use napi::Result;
|
||||||
|
|
||||||
use self::lib::*;
|
use self::lib::*;
|
||||||
|
|
||||||
mod lib;
|
mod lib;
|
||||||
|
|
||||||
const FRAME_SIZE: usize = 960;
|
const FRAME_SIZE: usize = 960;
|
||||||
const MAX_FRAME_SIZE: usize = 6 * FRAME_SIZE;
|
const MAX_FRAME_SIZE: usize = 6 * FRAME_SIZE;
|
||||||
const MAX_PACKET_SIZE: usize = 3 * 1276;
|
const MAX_PACKET_SIZE: usize = 3 * 1276;
|
||||||
|
|
||||||
fn get_decode_error(decoded_samples: i32) -> &'static str {
|
fn get_decode_error(decoded_samples: i32) -> &'static str {
|
||||||
match decoded_samples {
|
match decoded_samples {
|
||||||
OPUS_BAD_ARG => "One or more invalid/out of range arguments",
|
OPUS_BAD_ARG => "One or more invalid/out of range arguments",
|
||||||
OPUS_BUFFER_TOO_SMALL => "The mode struct passed is invalid",
|
OPUS_BUFFER_TOO_SMALL => "The mode struct passed is invalid",
|
||||||
OPUS_INTERNAL_ERROR => "An internal error was detected",
|
OPUS_INTERNAL_ERROR => "An internal error was detected",
|
||||||
OPUS_INVALID_PACKET => "The compressed data passed is corrupted",
|
OPUS_INVALID_PACKET => "The compressed data passed is corrupted",
|
||||||
OPUS_UNIMPLEMENTED => "Invalid/unsupported request number",
|
OPUS_UNIMPLEMENTED => "Invalid/unsupported request number",
|
||||||
OPUS_INVALID_STATE => "An encoder or decoder structure is invalid or already freed.",
|
OPUS_INVALID_STATE => "An encoder or decoder structure is invalid or already freed.",
|
||||||
OPUS_ALLOC_FAIL => "Memory allocation has failed",
|
OPUS_ALLOC_FAIL => "Memory allocation has failed",
|
||||||
_ => "Unknown OPUS error",
|
_ => "Unknown OPUS error",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[napi]
|
#[napi]
|
||||||
pub fn get_opus_version() -> String {
|
pub fn get_opus_version() -> String {
|
||||||
let version_string = unsafe { opus_get_version_string() };
|
let version_string = unsafe { opus_get_version_string() };
|
||||||
let version_cstr = unsafe { core::ffi::CStr::from_ptr(version_string) };
|
let version_cstr = unsafe { core::ffi::CStr::from_ptr(version_string) };
|
||||||
|
|
||||||
// fallback to commit hash
|
// fallback to commit hash
|
||||||
let version_string = version_cstr.to_str().unwrap_or("82ac57d").to_owned();
|
let version_string = version_cstr.to_str().unwrap_or("82ac57d").to_owned();
|
||||||
|
|
||||||
version_string
|
version_string
|
||||||
}
|
}
|
||||||
|
|
||||||
#[napi(js_name = "OpusEncoder")]
|
#[napi(js_name = "OpusEncoder")]
|
||||||
pub struct JsOpusEncoder {
|
pub struct JsOpusEncoder {
|
||||||
encoder: *mut OpusEncoder,
|
encoder: *mut OpusEncoder,
|
||||||
decoder: *mut OpusDecoder,
|
decoder: *mut OpusDecoder,
|
||||||
sample_rate: i32,
|
sample_rate: i32,
|
||||||
channels: i32,
|
channels: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[napi(js_name = "OpusEncoder")]
|
#[napi()]
|
||||||
impl JsOpusEncoder {
|
impl JsOpusEncoder {
|
||||||
#[napi(constructor)]
|
#[napi(constructor)]
|
||||||
pub fn new(sample_rate: i32, channels: i32) -> Result<Self> {
|
pub fn new(sample_rate: i32, channels: i32) -> Result<Self> {
|
||||||
Ok(JsOpusEncoder {
|
Ok(JsOpusEncoder {
|
||||||
encoder: std::ptr::null_mut(),
|
encoder: std::ptr::null_mut(),
|
||||||
decoder: std::ptr::null_mut(),
|
decoder: std::ptr::null_mut(),
|
||||||
sample_rate,
|
sample_rate,
|
||||||
channels,
|
channels,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ensure_encoder(&mut self) -> i32 {
|
fn ensure_encoder(&mut self) -> i32 {
|
||||||
if self.encoder.is_null() {
|
if self.encoder.is_null() {
|
||||||
let mut opus_code = 0;
|
let mut opus_code = 0;
|
||||||
|
|
||||||
let pointer = unsafe {
|
let pointer = unsafe {
|
||||||
opus_encoder_create(
|
opus_encoder_create(
|
||||||
self.sample_rate,
|
self.sample_rate,
|
||||||
self.channels,
|
self.channels,
|
||||||
OPUS_APPLICATION_AUDIO as i32,
|
OPUS_APPLICATION_AUDIO as i32,
|
||||||
&mut opus_code,
|
&mut opus_code,
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
if opus_code == OPUS_OK as i32 || !pointer.is_null() {
|
if opus_code == OPUS_OK as i32 || !pointer.is_null() {
|
||||||
self.encoder = pointer;
|
self.encoder = pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
return opus_code;
|
return opus_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
OPUS_OK as i32
|
OPUS_OK as i32
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ensure_decoder(&mut self) -> i32 {
|
fn ensure_decoder(&mut self) -> i32 {
|
||||||
if self.decoder.is_null() {
|
if self.decoder.is_null() {
|
||||||
let mut opus_code = 0;
|
let mut opus_code = 0;
|
||||||
|
|
||||||
let pointer = unsafe { opus_decoder_create(self.sample_rate, self.channels, &mut opus_code) };
|
let pointer = unsafe { opus_decoder_create(self.sample_rate, self.channels, &mut opus_code) };
|
||||||
|
|
||||||
if opus_code == OPUS_OK as i32 || !pointer.is_null() {
|
if opus_code == OPUS_OK as i32 || !pointer.is_null() {
|
||||||
self.decoder = pointer;
|
self.decoder = pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
return opus_code;
|
return opus_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
OPUS_OK as i32
|
OPUS_OK as i32
|
||||||
}
|
}
|
||||||
|
|
||||||
#[napi]
|
#[napi]
|
||||||
pub fn has_encoder(&self) -> bool {
|
pub fn has_encoder(&self) -> bool {
|
||||||
!self.encoder.is_null()
|
!self.encoder.is_null()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[napi]
|
#[napi]
|
||||||
pub fn has_decoder(&self) -> bool {
|
pub fn has_decoder(&self) -> bool {
|
||||||
!self.decoder.is_null()
|
!self.decoder.is_null()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[napi(catch_unwind)]
|
#[napi(catch_unwind)]
|
||||||
pub fn encode(&mut self, data: Buffer) -> Result<Buffer> {
|
pub fn encode(&mut self, data: Buffer) -> Result<Buffer> {
|
||||||
let status = self.ensure_encoder();
|
let status = self.ensure_encoder();
|
||||||
|
|
||||||
if status != OPUS_OK as i32 {
|
if status != OPUS_OK as i32 {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
Status::GenericFailure,
|
Status::GenericFailure,
|
||||||
format!("Failed to create encoder: {}", get_decode_error(status)),
|
format!("Failed to create encoder: {}", get_decode_error(status)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut out_buffer = vec![0u8; MAX_PACKET_SIZE];
|
let mut out_buffer = vec![0u8; MAX_PACKET_SIZE];
|
||||||
|
|
||||||
let pcm = unsafe { std::mem::transmute::<&[u8], &[i16]>(&data) };
|
let pcm = unsafe { std::mem::transmute::<&[u8], &[i16]>(&data) };
|
||||||
|
|
||||||
let frame_size = data.len() / 2 / (self.channels as usize);
|
let frame_size = data.len() / 2 / (self.channels as usize);
|
||||||
|
|
||||||
let compressed_len = unsafe {
|
let compressed_len = unsafe {
|
||||||
opus_encode(
|
opus_encode(
|
||||||
self.encoder,
|
self.encoder,
|
||||||
pcm.as_ptr(),
|
pcm.as_ptr(),
|
||||||
frame_size as i32,
|
frame_size as i32,
|
||||||
out_buffer.as_mut_ptr(),
|
out_buffer.as_mut_ptr(),
|
||||||
MAX_PACKET_SIZE as i32,
|
MAX_PACKET_SIZE as i32,
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
out_buffer.truncate(compressed_len as usize);
|
out_buffer.truncate(compressed_len as usize);
|
||||||
Ok(out_buffer.into())
|
Ok(out_buffer.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[napi(catch_unwind)]
|
#[napi(catch_unwind)]
|
||||||
pub fn decode(&mut self, data: Buffer) -> Result<Buffer> {
|
pub fn decode(&mut self, data: Buffer) -> Result<Buffer> {
|
||||||
let status = self.ensure_decoder();
|
let status = self.ensure_decoder();
|
||||||
|
|
||||||
if status != OPUS_OK as i32 {
|
if status != OPUS_OK as i32 {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
Status::GenericFailure,
|
Status::GenericFailure,
|
||||||
format!("Failed to create decoder: {}", get_decode_error(status)),
|
format!("Failed to create decoder: {}", get_decode_error(status)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut out = vec![0i16; MAX_FRAME_SIZE * self.channels as usize];
|
let mut out = vec![0i16; MAX_FRAME_SIZE * self.channels as usize];
|
||||||
|
|
||||||
let decoded_samples = unsafe {
|
let decoded_samples = unsafe {
|
||||||
opus_decode(
|
opus_decode(
|
||||||
self.decoder,
|
self.decoder,
|
||||||
data.as_ptr(),
|
data.as_ptr(),
|
||||||
data.len() as i32,
|
data.len() as i32,
|
||||||
out.as_mut_ptr(),
|
out.as_mut_ptr(),
|
||||||
MAX_FRAME_SIZE as i32,
|
MAX_FRAME_SIZE as i32,
|
||||||
0,
|
0,
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
if decoded_samples < 0 {
|
if decoded_samples < 0 {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
Status::GenericFailure,
|
Status::GenericFailure,
|
||||||
format!("Failed to decode: {}", get_decode_error(decoded_samples)),
|
format!("Failed to decode: {}", get_decode_error(decoded_samples)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let out = unsafe {
|
let out = unsafe {
|
||||||
let ptr = out.as_ptr() as *const u8;
|
let ptr = out.as_ptr() as *const u8;
|
||||||
std::slice::from_raw_parts(ptr, decoded_samples as usize * 4)
|
std::slice::from_raw_parts(ptr, decoded_samples as usize * 4)
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(out.to_vec().into())
|
Ok(out.to_vec().into())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[napi(catch_unwind)]
|
#[napi(catch_unwind)]
|
||||||
pub fn set_bitrate(&mut self, bitrate: i32) -> Result<()> {
|
pub fn set_bitrate(&mut self, bitrate: i32) -> Result<()> {
|
||||||
let status = self.ensure_encoder();
|
let status = self.ensure_encoder();
|
||||||
|
|
||||||
if status != OPUS_OK as i32 {
|
if status != OPUS_OK as i32 {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
Status::GenericFailure,
|
Status::GenericFailure,
|
||||||
format!("Failed to create encoder: {}", get_decode_error(status)),
|
format!("Failed to create encoder: {}", get_decode_error(status)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let status =
|
let status =
|
||||||
unsafe { opus_encoder_ctl(self.encoder, OPUS_SET_BITRATE_REQUEST as i32, bitrate) };
|
unsafe { opus_encoder_ctl(self.encoder, OPUS_SET_BITRATE_REQUEST as i32, bitrate) };
|
||||||
|
|
||||||
if status != OPUS_OK as i32 {
|
if status != OPUS_OK as i32 {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
Status::GenericFailure,
|
Status::GenericFailure,
|
||||||
format!("Failed to apply encoder ctl: {}", get_decode_error(status)),
|
format!("Failed to apply encoder ctl: {}", get_decode_error(status)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[napi(catch_unwind)]
|
#[napi(catch_unwind)]
|
||||||
pub fn get_bitrate(&mut self) -> Result<i32> {
|
pub fn get_bitrate(&mut self) -> Result<i32> {
|
||||||
let status = self.ensure_encoder();
|
let status = self.ensure_encoder();
|
||||||
|
|
||||||
if status != OPUS_OK as i32 {
|
if status != OPUS_OK as i32 {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
Status::GenericFailure,
|
Status::GenericFailure,
|
||||||
format!("Failed to create encoder: {}", get_decode_error(status)),
|
format!("Failed to create encoder: {}", get_decode_error(status)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut value = 0;
|
let mut value = 0;
|
||||||
|
|
||||||
let status =
|
let status =
|
||||||
unsafe { opus_encoder_ctl(self.encoder, OPUS_GET_BITRATE_REQUEST as i32, &mut value) };
|
unsafe { opus_encoder_ctl(self.encoder, OPUS_GET_BITRATE_REQUEST as i32, &mut value) };
|
||||||
|
|
||||||
if status != OPUS_OK as i32 {
|
if status != OPUS_OK as i32 {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
Status::GenericFailure,
|
Status::GenericFailure,
|
||||||
format!("Failed to apply encoder ctl: {}", get_decode_error(status)),
|
format!("Failed to apply encoder ctl: {}", get_decode_error(status)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(value)
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[napi(catch_unwind)]
|
#[napi(catch_unwind)]
|
||||||
pub fn apply_encoder_ctl(&mut self, request: i32, value: i32) -> Result<()> {
|
pub fn apply_encoder_ctl(&mut self, request: i32, value: i32) -> Result<()> {
|
||||||
let status = self.ensure_encoder();
|
let status = self.ensure_encoder();
|
||||||
|
|
||||||
if status != OPUS_OK as i32 {
|
if status != OPUS_OK as i32 {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
Status::GenericFailure,
|
Status::GenericFailure,
|
||||||
format!("Failed to create encoder: {}", get_decode_error(status)),
|
format!("Failed to create encoder: {}", get_decode_error(status)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let status = unsafe { opus_encoder_ctl(self.encoder, request, value) };
|
let status = unsafe { opus_encoder_ctl(self.encoder, request, value) };
|
||||||
|
|
||||||
if status != OPUS_OK as i32 {
|
if status != OPUS_OK as i32 {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
Status::GenericFailure,
|
Status::GenericFailure,
|
||||||
format!("Failed to apply encoder ctl: {}", get_decode_error(status)),
|
format!("Failed to apply encoder ctl: {}", get_decode_error(status)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[napi]
|
#[napi]
|
||||||
pub fn apply_decoder_ctl(&mut self, request: i32, value: i32) -> Result<()> {
|
pub fn apply_decoder_ctl(&mut self, request: i32, value: i32) -> Result<()> {
|
||||||
let status = self.ensure_decoder();
|
let status = self.ensure_decoder();
|
||||||
|
|
||||||
if status != OPUS_OK as i32 {
|
if status != OPUS_OK as i32 {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
Status::GenericFailure,
|
Status::GenericFailure,
|
||||||
format!("Failed to create decoder: {}", get_decode_error(status)),
|
format!("Failed to create decoder: {}", get_decode_error(status)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let status = unsafe { opus_decoder_ctl(self.decoder, request, value) };
|
let status = unsafe { opus_decoder_ctl(self.decoder, request, value) };
|
||||||
|
|
||||||
if status != OPUS_OK as i32 {
|
if status != OPUS_OK as i32 {
|
||||||
return Err(Error::new(
|
return Err(Error::new(
|
||||||
Status::GenericFailure,
|
Status::GenericFailure,
|
||||||
format!("Failed to apply decoder ctl: {}", get_decode_error(status)),
|
format!("Failed to apply decoder ctl: {}", get_decode_error(status)),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[napi(getter)]
|
#[napi(getter)]
|
||||||
pub fn get_version(&self) -> String {
|
pub fn get_version(&self) -> String {
|
||||||
get_opus_version()
|
get_opus_version()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for JsOpusEncoder {
|
impl Drop for JsOpusEncoder {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if !self.encoder.is_null() {
|
if !self.encoder.is_null() {
|
||||||
unsafe {
|
unsafe {
|
||||||
opus_encoder_destroy(self.encoder);
|
opus_encoder_destroy(self.encoder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self.decoder.is_null() {
|
if !self.decoder.is_null() {
|
||||||
unsafe {
|
unsafe {
|
||||||
opus_decoder_destroy(self.decoder);
|
opus_decoder_destroy(self.decoder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user