Some changes

canary
WolverinDEV 2018-02-28 20:49:56 +01:00
parent ecb8b7a686
commit 819836d706
9 changed files with 107139 additions and 36280 deletions

View File

@ -3,7 +3,7 @@ project(TeaWeb-Native)
set(CMAKE_CXX_COMPILER "emcc")
set(CMAKE_C_COMPILER "emcc")
set(CMAKE_C_LINK_EXECUTABLE "emcc")
set(CMAKE_CXX_FLAGS "-O2") #-s WASM=1
set(CMAKE_CXX_FLAGS "-s ASSERTIONS=2") #-s WASM=1
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_EXE_LINKER_FLAGS "-s EXTRA_EXPORTED_RUNTIME_METHODS='[\"ccall\", \"cwrap\"]'") #

File diff suppressed because one or more lines are too long

View File

@ -5,7 +5,6 @@
using namespace std;
extern "C" {
struct OpusHandle {
string name;
OpusEncoder* encoder = nullptr;
OpusDecoder* decoder = nullptr;
@ -14,10 +13,10 @@ extern "C" {
EMSCRIPTEN_KEEPALIVE
OpusHandle* codec_opus_createNativeHandle(size_t channelCount) {
printf("inizalisized (%d)!\n", channelCount);
auto codec = new OpusHandle;
codec->name = "Opus codec!";
codec->decoder = opus_decoder_create(48000, channelCount, nullptr);
codec->encoder = opus_encoder_create(48000, channelCount, OPUS_APPLICATION_VOIP, nullptr);
codec->encoder = opus_encoder_create(48000, channelCount, OPUS_APPLICATION_AUDIO, nullptr);
return codec;
}
@ -36,6 +35,10 @@ extern "C" {
EMSCRIPTEN_KEEPALIVE
int codec_opus_encode(OpusHandle* handle, uint8_t* buffer, size_t length, size_t maxLength) {
printf("codec_opus_encode(%p,%p,%d, %d)\n", handle->encoder, (void*) buffer, length, maxLength);
float bbuffer[960];
uint8_t rbuffer[120];
return opus_encode_float(handle->encoder, bbuffer, 960, rbuffer, 120);
auto result = opus_encode_float(handle->encoder, (float*) buffer, length / handle->channelCount, buffer, maxLength);
if(result < 0) return result;
return result;

View File

@ -10,10 +10,10 @@ class OpusCodec extends Codec {
return "Opus";
}
initialise() {
this.fn_newHandle = Module.cwrap("codec_opus_createNativeHandle", "pointer", []);
this.fn_decode = Module.cwrap("codec_opus_encode", "number", ["pointer", "pointer", "length"]);
this.fn_newHandle = Module.cwrap("codec_opus_createNativeHandle", "pointer", ["number"]);
this.fn_decode = Module.cwrap("codec_opus_decode", "number", ["pointer", "pointer", "number", "number"]); /* codec_opus_decode(handle, buffer, length, maxlength) */
this.nativeHandle = this.fn_newHandle();
this.fn_encode = Module.cwrap("codec_opus_encode", "number", ["pointer", "pointer", "number", "number"]);
this.nativeHandle = this.fn_newHandle(1);
}
deinitialise() {
}
@ -25,22 +25,27 @@ class OpusCodec extends Codec {
let result = this.fn_decode(this.nativeHandle, heapBytes.byteOffset, data.byteLength, maxBytes);
if (result < 0) {
Module._free(buffer);
return "invalid result (" + result + ")";
return "invalid result on decode (" + result + ")";
}
let buf = Module.HEAPF32.slice(heapBytes.byteOffset / 4, (heapBytes.byteOffset / 4) + (result * 4 * this.channelCount));
Module._free(buffer);
return buf;
}
convertBlock(incomingData, length) {
var i, l = length;
var outputData = new Float32Array(length);
for (i = 0; i < l; i++) {
outputData[i] = (incomingData[i] - 128) / 128.0;
}
return outputData;
}
encode(data) {
return undefined;
let maxBytes = 4096 * 1 + 4;
let buffer = Module._malloc(maxBytes);
console.log("X");
let heapBytes = new Uint8Array(Module.HEAPU8.buffer, buffer, maxBytes);
//heapBytes.set(data);
let result = this.fn_encode(this.nativeHandle, heapBytes.byteOffset, 960, maxBytes);
if (result < 0) {
Module._free(buffer);
return "invalid result on encode (" + result + ")";
}
console.log("Bytes: " + result);
let buf = Module.HEAP8.slice(heapBytes.byteOffset, heapBytes.byteOffset + result);
Module._free(buffer);
return Uint8Array.from(buf);
}
_arrayToHeap(typedArray) {
let numBytes = typedArray.length * typedArray.BYTES_PER_ELEMENT;

View File

@ -1 +1 @@
{"version":3,"file":"Codec.js","sourceRoot":"","sources":["Codec.ts"],"names":[],"mappings":"AAAA;IACI,gBAAc,CAAC;CASlB;AAED,eAAgB,SAAQ,KAAK;IAOzB;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KAAK,CAAC,MAAM,CAAC,CAAC;QACrB,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IAEO,YAAY,CAAC,UAAe;QAChC,IAAI,QAAQ,GAAG,UAAU,CAAC,MAAM,GAAG,UAAU,CAAC,iBAAiB,CAAC;QAChE,IAAI,GAAG,GAAG,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QACnC,IAAI,SAAS,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,QAAQ,CAAC,CAAC;QACpE,SAAS,CAAC,GAAG,CAAC,IAAI,UAAU,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;QACjD,MAAM,CAAC,SAAS,CAAC;IACrB,CAAC;CACJ"}

View File

@ -7,7 +7,7 @@ abstract class Codec {
abstract decode(data: Uint8Array) : Float32Array | string;
abstract encode(data: Float32Array) : Uint8Array;
abstract encode(data: Float32Array) : Uint8Array | string;
}
class OpusCodec extends Codec {
@ -16,6 +16,7 @@ class OpusCodec extends Codec {
private fn_newHandle: any;
private fn_decode: any;
private fn_encode: any;
constructor() {
super();
@ -26,11 +27,11 @@ class OpusCodec extends Codec {
}
initialise() {
this.fn_newHandle = Module.cwrap("codec_opus_createNativeHandle", "pointer", []);
this.fn_decode = Module.cwrap("codec_opus_encode", "number", ["pointer", "pointer", "length"]);
this.fn_newHandle = Module.cwrap("codec_opus_createNativeHandle", "pointer", ["number"]);
this.fn_decode = Module.cwrap("codec_opus_decode", "number", ["pointer", "pointer", "number", "number"]); /* codec_opus_decode(handle, buffer, length, maxlength) */
this.fn_encode = Module.cwrap("codec_opus_encode", "number", ["pointer", "pointer", "number", "number"]);
this.nativeHandle = this.fn_newHandle();
this.nativeHandle = this.fn_newHandle(1);
}
deinitialise() {
@ -45,24 +46,28 @@ class OpusCodec extends Codec {
let result = this.fn_decode(this.nativeHandle, heapBytes.byteOffset, data.byteLength, maxBytes);
if(result < 0) {
Module._free(buffer);
return "invalid result (" + result + ")";
return "invalid result on decode (" + result + ")";
}
let buf = Module.HEAPF32.slice(heapBytes.byteOffset / 4, (heapBytes.byteOffset / 4) + (result * 4 * this.channelCount));
Module._free(buffer);
return buf;
}
convertBlock(incomingData, length: number) { // incoming data is a UInt8Array
var i, l = length;
var outputData = new Float32Array(length);
for (i = 0; i < l; i++) {
outputData[i] = (incomingData[i] - 128) / 128.0;
encode(data: Float32Array): Uint8Array | string {
let maxBytes = 4096 * 1 + 4;
let buffer = Module._malloc(maxBytes);
console.log("X");
let heapBytes = new Uint8Array(Module.HEAPU8.buffer, buffer, maxBytes);
//heapBytes.set(data);
let result = this.fn_encode(this.nativeHandle, heapBytes.byteOffset, 960, maxBytes);
if(result < 0) {
Module._free(buffer);
return "invalid result on encode (" + result + ")";
}
return outputData;
}
encode(data: Float32Array): Uint8Array {
return undefined;
console.log("Bytes: " + result);
let buf = Module.HEAP8.slice(heapBytes.byteOffset, heapBytes.byteOffset + result);
Module._free(buffer);
return Uint8Array.from(buf);
}
private _arrayToHeap(typedArray: any){

View File

@ -59,18 +59,6 @@ class VoiceConnection {
console.log("Got new data channel!");
}
onDataChannelMessage(message) {
/*
let bin = new Uint8Array(message.data);
let clientId = bin[0] << 8 | bin[1];
console.log("Client id " + clientId);
let client = this.client.channelTree.findClient(clientId);
if(!client) {
console.error("Having voice from unknown client? (ClientID: " + clientId + ")");
return;
}
var pcm = new Float32Array(message.data, 4);
client.getAudioController().play(pcm);
*/
let bin = new Uint8Array(message.data);
let clientId = bin[0] << 8 | bin[1];
console.log("Client id " + clientId);
@ -89,8 +77,15 @@ class VoiceConnection {
sendPCMData(data) {
console.log("SEND DATA!");
//console.log(data);
if (this.dataChannel)
this.dataChannel.send(data);
//FIXME just for debug
if (this.dataChannel) {
console.log("XXX");
let enbcoded = this.codec.encode(data);
if (enbcoded instanceof Uint8Array)
this.dataChannel.send(enbcoded);
else
console.log("Invalid decode " + enbcoded);
}
}
}
class VoiceRecorder {

File diff suppressed because one or more lines are too long

View File

@ -80,18 +80,6 @@ class VoiceConnection {
}
onDataChannelMessage(message) {
/*
let bin = new Uint8Array(message.data);
let clientId = bin[0] << 8 | bin[1];
console.log("Client id " + clientId);
let client = this.client.channelTree.findClient(clientId);
if(!client) {
console.error("Having voice from unknown client? (ClientID: " + clientId + ")");
return;
}
var pcm = new Float32Array(message.data, 4);
client.getAudioController().play(pcm);
*/
let bin = new Uint8Array(message.data);
let clientId = bin[0] << 8 | bin[1];
console.log("Client id " + clientId);
@ -110,8 +98,15 @@ class VoiceConnection {
private sendPCMData(data: any) {
console.log("SEND DATA!");
//console.log(data);
if(this.dataChannel) //FIXME just for debug
this.dataChannel.send(data);
//FIXME just for debug
if(this.dataChannel) {
console.log("XXX");
let enbcoded = this.codec.encode(data);
if(enbcoded instanceof Uint8Array)
this.dataChannel.send(enbcoded);
else console.log("Invalid decode " + enbcoded);
}
}
}