Fixed several microphone things

canary
WolverinDEV 2018-08-12 22:31:40 +02:00
parent 7a9e4b604b
commit 66bb1350cb
4 changed files with 35 additions and 32 deletions

View File

@ -120,15 +120,11 @@ namespace Modals {
let select_error = tag.find(".voice_microphone_select_error");
navigator.mediaDevices.enumerateDevices().then(devices => {
let currentStream = globalClient.voiceConnection.voiceRecorder.getMediaStream();
let currentDeviceId;
if(currentStream) {
let audio = currentStream.getAudioTracks()[0];
currentDeviceId = audio.getSettings().deviceId;
}
let recoder = globalClient.voiceConnection.voiceRecorder;
console.log("Got " + devices.length + " devices:");
for(let device of devices) {
console.log(device);
console.log(" - Type: %s Name %s ID: %s Group: %s", device.kind, device.label, device.deviceId, device.groupId);
if(device.kind == "audioinput") {
let dtag = $.spawn("option");
dtag.attr("device-id", device.deviceId);
@ -136,7 +132,7 @@ namespace Modals {
dtag.text(device.label);
select_microphone.append(dtag);
dtag.prop("selected", currentDeviceId && device.deviceId == currentDeviceId);
if(recoder) dtag.prop("selected", device.deviceId == recoder.device_id());
}
}
}).catch(error => {
@ -150,7 +146,7 @@ namespace Modals {
let deviceId = deviceSelected.attr("device-id");
let groupId = deviceSelected.attr("device-group");
console.log("Selected microphone device: id: %o group: %o", deviceId, groupId);
globalClient.voiceConnection.voiceRecorder.changeDevice(deviceId, groupId);
globalClient.voiceConnection.voiceRecorder.change_device(deviceId, groupId);
});
//Initialise speakers

View File

@ -9,7 +9,7 @@ enum PlayerState {
class AudioController {
private static getUserMediaFunction() {
if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia)
return (settings, success, fail) => navigator.mediaDevices.getUserMedia(settings).then(success).catch(fail);
return (settings, success, fail) => { navigator.mediaDevices.getUserMedia(settings).then(success).catch(fail); };
return navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
}
public static userMedia = AudioController.getUserMediaFunction();

View File

@ -184,7 +184,7 @@ class VoiceConnection {
config.iceServers = [];
config.iceServers.push({ urls: 'stun:stun.l.google.com:19302' });
this.rtcPeerConnection = new RTCPeerConnection(config);
const dataChannelConfig = { ordered: false, maxRetransmits: 0 };
const dataChannelConfig = { ordered: true, maxRetransmits: 0 };
this.dataChannel = this.rtcPeerConnection.createDataChannel('main', dataChannelConfig);
this.dataChannel.onmessage = this.onDataChannelMessage.bind(this);

View File

@ -65,19 +65,16 @@ class VoiceRecorder {
this._chunkCount = 0
}
});
this.processor.connect(this.audioContext.destination);
//Not needed but make sure we have data for the preprocessor
this.mute = this.audioContext.createGain();
this.mute.gain.setValueAtTime(0, 0);
this.mute.connect(this.audioContext.destination);
this.processor.connect(this.audioContext.destination);
if(this.vadHandler) {
if(this.vadHandler)
this.vadHandler.initialise();
if(this.microphoneStream)
this.vadHandler.initialiseNewStream(undefined, this.microphoneStream);
}
this.on_microphone(this.mediaStream);
});
this.setVADHandler(new PassThroughVAD());
@ -127,6 +124,7 @@ class VoiceRecorder {
this.vadHandler.changeHandle(null, true);
this.vadHandler.finalize();
}
this.vadHandler = handler;
this.vadHandler.changeHandle(this, false);
if(this.audioContext) {
@ -146,13 +144,16 @@ class VoiceRecorder {
else this.stop();
}
changeDevice(device: string, group: string) {
device_group_id() : string { return this._deviceGroup; }
device_id() : string { return this._deviceId; }
change_device(device: string, group: string) {
if(this._deviceId == device && this._deviceGroup == group) return;
this._deviceId = device;
this._deviceGroup = group;
settings.changeGlobal("microphone_device_id", device);
settings.changeServer("microphone_device_group", group);
settings.changeGlobal("microphone_device_group", group);
if(this._recording) {
this.stop();
this.start(device, group);
@ -161,10 +162,12 @@ class VoiceRecorder {
start(device: string, groupId: string){
this._deviceId = device;
console.log("Attempt recording! (Device: %o | Group: %o)", device, groupId);
this._deviceGroup = groupId;
console.log("[VoiceRecorder] Start recording! (Device: %o | Group: %o)", device, groupId);
this._recording = true;
console.log("Function: %o", AudioController.userMedia);
let result = AudioController.userMedia({
AudioController.userMedia({
audio: {
deviceId: device,
groupId: groupId
@ -174,39 +177,43 @@ class VoiceRecorder {
console.error("Could not get microphone!");
console.error(error);
});
console.log(result);
}
stop(){
stop(stop_media_stream: boolean = true){
console.log("Stop recording!");
this._recording = false;
if(this.microphoneStream) this.microphoneStream.disconnect();
this.microphoneStream = undefined;
if(this.mediaStream) {
if(stop_media_stream && this.mediaStream) {
if(this.mediaStream.stop)
this.mediaStream.stop();
else
this.mediaStream.getTracks().forEach(value => {
value.stop();
});
this.mediaStream = undefined;
}
this.mediaStream = undefined;
}
private on_microphone(stream: MediaStream) {
const oldStream = this.microphoneStream;
if(oldStream)
this.stop(); //Disconnect old stream
console.log("Start recording!");
const old_microphone_stream = this.microphoneStream;
if(old_microphone_stream)
this.stop(this.mediaStream != stream); //Disconnect old stream
this.mediaStream = stream;
if(!this.audioContext) return;
if(!this.mediaStream) return;
if(!this.audioContext) {
console.log("[VoiceRecorder] Got microphone stream, but havn't a audio context. Waiting until its initialized");
return;
}
this.microphoneStream = this.audioContext.createMediaStreamSource(stream);
this.microphoneStream.connect(this.processor);
this.vadHandler.initialiseNewStream(oldStream, this.microphoneStream);
if(this.vadHandler)
this.vadHandler.initialiseNewStream(old_microphone_stream, this.microphoneStream);
}
}
class MuteVAD extends VoiceActivityDetector {