From d9c858315ff74003343dbaf7ab3f864f82c0687c Mon Sep 17 00:00:00 2001 From: WolverinDEV Date: Wed, 26 Sep 2018 15:30:22 +0200 Subject: [PATCH] Fixed voice initializing --- js/codec/BasicCodec.ts | 3 +-- js/log.ts | 6 ++++-- js/main.ts | 4 ++-- js/proto.ts | 10 ++++++++++ js/voice/AudioController.ts | 4 +--- js/voice/AudioResampler.ts | 2 +- js/voice/VoiceHandler.ts | 12 ++++++++---- 7 files changed, 27 insertions(+), 14 deletions(-) diff --git a/js/codec/BasicCodec.ts b/js/codec/BasicCodec.ts index 9865e126..cb00e45b 100644 --- a/js/codec/BasicCodec.ts +++ b/js/codec/BasicCodec.ts @@ -18,7 +18,6 @@ class AVGCalculator { } } -declare class webkitOfflineAudioContext extends OfflineAudioContext {} abstract class BasicCodec implements Codec { protected _audioContext: OfflineAudioContext; protected _decodeResampler: AudioResampler; @@ -33,7 +32,7 @@ abstract class BasicCodec implements Codec { constructor(codecSampleRate: number) { this.channelCount = 1; this.samplesPerUnit = 960; - this._audioContext = new (webkitOfflineAudioContext || OfflineAudioContext)(AudioController.globalContext.destination.channelCount, 1024,AudioController.globalContext.sampleRate ); + this._audioContext = new (window.webkitOfflineAudioContext || window.OfflineAudioContext)(AudioController.globalContext.destination.channelCount, 1024,AudioController.globalContext.sampleRate ); this._codecSampleRate = codecSampleRate; this._decodeResampler = new AudioResampler(AudioController.globalContext.sampleRate); this._encodeResampler = new AudioResampler(codecSampleRate); diff --git a/js/log.ts b/js/log.ts index 144291fd..31e6a486 100644 --- a/js/log.ts +++ b/js/log.ts @@ -4,7 +4,8 @@ enum LogCategory { SERVER, PERMISSIONS, GENERAL, - NETWORKING + NETWORKING, + VOICE } namespace log { @@ -22,7 +23,8 @@ namespace log { [LogCategory.SERVER, "Server "], [LogCategory.PERMISSIONS, "Permission "], [LogCategory.GENERAL, "General "], - [LogCategory.NETWORKING, "Network "] + [LogCategory.NETWORKING, "Network "], + [LogCategory.VOICE, "Voice "] ]); function logDirect(type: LogType, message: string, ...optionalParams: any[]) { diff --git a/js/main.ts b/js/main.ts index bba92870..388f80cb 100644 --- a/js/main.ts +++ b/js/main.ts @@ -92,8 +92,8 @@ function main() { app.loadedListener.push(() => { try { main(); - if(!AudioController.initialized) { - console.log("Initialize audio controller later!"); + if(!AudioController.initialized()) { + log.info(LogCategory.VOICE, "Initialize audio controller later!"); $(document).one('click', event => AudioController.initializeFromGesture()); } } catch (ex) { diff --git a/js/proto.ts b/js/proto.ts index 5adccfdf..d1dba46f 100644 --- a/js/proto.ts +++ b/js/proto.ts @@ -172,4 +172,14 @@ function calculate_width(text: string) : number { let size = element.width(); element.detach(); return size; +} + +declare class webkitAudioContext extends AudioContext {} +declare class webkitOfflineAudioContext extends OfflineAudioContext {} +interface Window { + readonly webkitAudioContext: typeof webkitAudioContext; + readonly AudioContext: typeof webkitAudioContext; + readonly OfflineAudioContext: typeof OfflineAudioContext; + readonly webkitOfflineAudioContext: typeof webkitOfflineAudioContext; + readonly RTCPeerConnection: typeof RTCPeerConnection; } \ No newline at end of file diff --git a/js/voice/AudioController.ts b/js/voice/AudioController.ts index 44ffecbd..d8aece29 100644 --- a/js/voice/AudioController.ts +++ b/js/voice/AudioController.ts @@ -11,8 +11,6 @@ interface Navigator { webkitGetUserMedia(constraints: MediaStreamConstraints, successCallback: NavigatorUserMediaSuccessCallback, errorCallback: NavigatorUserMediaErrorCallback): void; } -declare class webkitAudioContext extends AudioContext {} - class AudioController { private static getUserMediaFunction() { if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) @@ -32,7 +30,7 @@ class AudioController { if(this._globalContext && this._globalContext.state != "suspended") return this._globalContext; if(!this._globalContext) - this._globalContext = new (webkitAudioContext || AudioContext)(); + this._globalContext = new (window.webkitAudioContext || window.AudioContext)(); if(this._globalContext.state == "suspended") { if(!this._globalContextPromise) { (this._globalContextPromise = this._globalContext.resume()).then(() => { diff --git a/js/voice/AudioResampler.ts b/js/voice/AudioResampler.ts index ed5fe2c4..09f45598 100644 --- a/js/voice/AudioResampler.ts +++ b/js/voice/AudioResampler.ts @@ -17,7 +17,7 @@ class AudioResampler { return new Promise(resolve => resolve(buffer)); let context; - context = new (webkitOfflineAudioContext || OfflineAudioContext)(buffer.numberOfChannels, Math.ceil(buffer.length * this.targetSampleRate / buffer.sampleRate), this.targetSampleRate); + context = new (window.webkitOfflineAudioContext || window.OfflineAudioContext)(buffer.numberOfChannels, Math.ceil(buffer.length * this.targetSampleRate / buffer.sampleRate), this.targetSampleRate); let source = context.createBufferSource(); source.buffer = buffer; diff --git a/js/voice/VoiceHandler.ts b/js/voice/VoiceHandler.ts index cdd96da2..7ee538ef 100644 --- a/js/voice/VoiceHandler.ts +++ b/js/voice/VoiceHandler.ts @@ -117,7 +117,6 @@ interface RTCPeerConnection { createOffer(successCallback?: RTCSessionDescriptionCallback, failureCallback?: RTCPeerConnectionErrorCallback, options?: RTCOfferOptions): Promise; } - class VoiceConnection { client: TSClient; rtcPeerConnection: RTCPeerConnection; @@ -150,6 +149,7 @@ class VoiceConnection { this.voiceRecorder.reinitialiseVAD(); AudioController.on_initialized(() => { + log.info(LogCategory.VOICE, "Initializing voice handler after AudioController has been initialized!"); this.codec_pool[4].initialize(2); this.codec_pool[5].initialize(2); @@ -163,12 +163,12 @@ class VoiceConnection { } native_encoding_supported() : boolean { - if(!(webkitAudioContext || AudioContext).prototype.createMediaStreamDestination) return false; //Required, but not available within edge + if(!(window.webkitAudioContext || window.AudioContext || {prototype: {}} as typeof AudioContext).prototype.createMediaStreamDestination) return false; //Required, but not available within edge return true; } javascript_encoding_supported() : boolean { - if(!RTCPeerConnection.prototype.createDataChannel) return false; + if(!(window.RTCPeerConnection || {prototype: {}} as typeof RTCPeerConnection).prototype.createDataChannel) return false; return true; } @@ -183,7 +183,11 @@ class VoiceConnection { } private setup_native() { - if(!this.native_encoding_supported()) return; + log.info(LogCategory.VOICE, "Setting up native voice stream!"); + if(!this.native_encoding_supported()) { + log.warn(LogCategory.VOICE, "Native codec isnt supported!"); + return; + } this.voiceRecorder.on_data = undefined;