oh-sipclient: Fix microphone access stays active when foreground is left on iOS (#1575)

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
pull/1585/head
Florian Hotze 2022-12-12 09:27:12 +01:00 committed by GitHub
parent b4aff86c84
commit 5052597b0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 6 deletions

View File

@ -67,7 +67,8 @@ export default {
remoteParty: '', remoteParty: '',
phonebook: new Map(), phonebook: new Map(),
loggerPrefix: 'oh-sipclient', loggerPrefix: 'oh-sipclient',
showLocalVideo: false showLocalVideo: false,
stream: null
} }
}, },
mixins: [mixin, foregroundService, actionsMixin], mixins: [mixin, foregroundService, actionsMixin],
@ -85,7 +86,7 @@ export default {
} }
} }
if (this.context.editmode) return // do not connect SIP while editing if (this.context.editmode) return // Do not connect SIP while editing
// Make sure we have Mic/Camera permissions // Make sure we have Mic/Camera permissions
if (!navigator.mediaDevices) { if (!navigator.mediaDevices) {
@ -93,23 +94,31 @@ export default {
} else { } else {
navigator.mediaDevices.getUserMedia({ audio: true, video: this.config.enableVideo }) navigator.mediaDevices.getUserMedia({ audio: true, video: this.config.enableVideo })
.then((stream) => { .then((stream) => {
// Store MediaDevices access here to stop it when foreground is left
// Do NOT stop MediaDevices access here (keep Mic/Camera access) to improve call startup time
this.stream = stream
// Start SIP connection // Start SIP connection
this.sipStart() this.sipStart()
}) })
.catch((err) => { .catch((err) => {
console.log('could not access microphone/camera', err) console.log('Could not access microphone/camera', err)
this.$f7.dialog.alert('To use the SIP widget you must allow microphone/camera access in your browser and reload this page.') this.$f7.dialog.alert('To use the SIP widget you must allow microphone/camera access in your browser and reload this page.')
}) })
} }
}, },
stopForegroundActivity () { stopForegroundActivity () {
// Stop MediaDevices access here, otherwise Mic/Camera access will stay active on iOS
this.stream.getTracks().forEach((track) => track.stop())
if (this.phone) this.phone.stop() if (this.phone) this.phone.stop()
}, },
/**
* Starts the JsSIP UserAgent and connects to the SIP server.
*/
sipStart () { sipStart () {
if (this.phone) this.phone.stop() // reconnect to reload config if (this.phone) this.phone.stop() // Reconnect to reload config
this.context.component.config = { ...this.config, ...this.localConfig } // merge local device configuration this.context.component.config = { ...this.config, ...this.localConfig } // Merge local device configuration
import(/* webpackChunkName: "jssip" */ 'jssip').then((JsSIP) => { // lazy load jssip import(/* webpackChunkName: "jssip" */ 'jssip').then((JsSIP) => { // Lazy load jssip
this.config.enableSIPDebug ? JsSIP.debug.enable('JsSIP:*') : JsSIP.debug.disable() this.config.enableSIPDebug ? JsSIP.debug.enable('JsSIP:*') : JsSIP.debug.disable()
// SIP user agent setup // SIP user agent setup
this.remoteAudio = new window.Audio() this.remoteAudio = new window.Audio()
@ -171,6 +180,10 @@ export default {
this.phone.start() this.phone.start()
}) })
}, },
/**
* Plays a given tone. Might not properly work on all browsers and devices.
* @param {*} audio file to be played
*/
playTone (file) { playTone (file) {
if (this.config.enableTones === true) { if (this.config.enableTones === true) {
console.info(this.loggerPrefix + ': Starting to play tone') console.info(this.loggerPrefix + ': Starting to play tone')
@ -183,12 +196,18 @@ export default {
}) })
} }
}, },
/**
* Stops all played tones.
*/
stopTones () { stopTones () {
if (this.config.enableTones === true) { if (this.config.enableTones === true) {
console.info(this.loggerPrefix + ': Stop playing tone') console.info(this.loggerPrefix + ': Stop playing tone')
this.audio.pause() this.audio.pause()
} }
}, },
/**
* Attaches MediaStreams (remote audio, remote & eventually local video) for the SIP call.
*/
attachMedia () { attachMedia () {
this.session.connection.addEventListener('track', (track) => { this.session.connection.addEventListener('track', (track) => {
if (this.config.enableVideo) { if (this.config.enableVideo) {
@ -206,9 +225,14 @@ export default {
} }
}) })
}, },
/**
* Stops all MediaStreams (remote audio, remote & eventually local video) of the SIP call.
*/
stopMedia () { stopMedia () {
if (this.config.enableVideo) this.$refs.remoteVideo.srcObject = null if (this.config.enableVideo) this.$refs.remoteVideo.srcObject = null
if (this.config.enableLocalVideo) { if (this.config.enableLocalVideo) {
// Make sure all tracks are stopped
this.$refs.localVideo.srcObject.getTracks().forEach((track) => track.stop())
this.$refs.localVideo.srcObject = null this.$refs.localVideo.srcObject = null
this.showLocalVideo = false this.showLocalVideo = false
} }