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

View File

@ -9,7 +9,7 @@ enum PlayerState {
class AudioController { class AudioController {
private static getUserMediaFunction() { private static getUserMediaFunction() {
if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) 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; return navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
} }
public static userMedia = AudioController.getUserMediaFunction(); public static userMedia = AudioController.getUserMediaFunction();

View File

@ -184,7 +184,7 @@ class VoiceConnection {
config.iceServers = []; config.iceServers = [];
config.iceServers.push({ urls: 'stun:stun.l.google.com:19302' }); config.iceServers.push({ urls: 'stun:stun.l.google.com:19302' });
this.rtcPeerConnection = new RTCPeerConnection(config); 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 = this.rtcPeerConnection.createDataChannel('main', dataChannelConfig);
this.dataChannel.onmessage = this.onDataChannelMessage.bind(this); this.dataChannel.onmessage = this.onDataChannelMessage.bind(this);

View File

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