WonderInventions / node-webrtc

node-webrtc is a Node.js Native Addon that provides bindings to WebRTC M98
Other
107 stars 8 forks source link

[Feature Request] Add Support For 8-Bit Audio #5

Open alipsitz-sf opened 9 months ago

alipsitz-sf commented 9 months ago

https://github.com/WonderInventions/node-webrtc/blob/3ecf646fc9c65cbe203408e8b0ccd0ed87abf28a/src/dictionaries/node_webrtc/rtc_on_data_event_dict.cc#L19C7-L19C7

Hello, would it be possible to remove this restriction that the bits per sample is exactly 16? More specially I'm hoping to use this library with a system that requires a value of 8 bits per sample.

duvallj commented 9 months ago

If I'm remembering correctly, this turned out to be an issue with the libwebrtc API this function eventually ends up calling only supporting 16 bit samples. So, I ended up just leaving this check in because it's pretty tricky in C to properly handle the cases correctly.

If you have 8-bit samples, it's likely not too hard to transcode into 16-bit samples in Javascript:

const x = new Int8Array([1, 2, 3, 4, 5, 127, -128, 0]);
const y = new Int16Array(x).map((s) => s << 8);
console.log(y);
// Int16Array(8) [
//      256,   512,
//      768,  1024,
//     1280, 32512,
//   -32768,     0
// ]
alipsitz-sf commented 9 months ago

Hi @duvallj, thank you so much for the information. I'll work to investigate whether that will work and update.

The issue we are trying to solve is to use the non-standard APIs in the library to automatically accept a call from a peer and send a PCM payload that can be heard on the far-end. Unfortunately the only offered codec from the peer is PCMU/8000 (which by spec has to be 8-bit). There's a lot of layers, so I'm not quite sure if the far end will be able to decode the audio properly.