Browse Source

Play sounds when someone is joining or leaving a call

Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/5410/head
Joas Schilling 6 years ago
parent
commit
09341afb03
No known key found for this signature in database GPG Key ID: 7076EA9751AACDDA
  1. BIN
      img/LibremEmailNotification.ogg
  2. BIN
      img/LibremTextMessage.ogg
  3. 83
      src/utils/sounds.js
  4. 32
      src/utils/webrtc/webrtc.js

BIN
img/LibremEmailNotification.ogg

BIN
img/LibremTextMessage.ogg

83
src/utils/sounds.js

@ -0,0 +1,83 @@
/**
* @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import { generateFilePath } from '@nextcloud/router'
export const Sounds = {
isInCall: false,
lastPlayedJoin: 0,
lastPlayedLeave: 0,
_playFile(soundFile) {
const file = generateFilePath('spreed', 'img', soundFile)
const audio = new Audio(file)
audio.play()
},
playJoin(force) {
if (force) {
this.isInCall = true
} else if (!this.isInCall) {
return
}
const currentTime = (new Date()).getTime()
if (!force && this.lastPlayedJoin >= (currentTime - 7000)) {
if (this.lastPlayedJoin >= (currentTime - 7000)) {
console.debug('Skipping join sound because it was played %.2f seconds ago', currentTime - this.lastPlayedJoin)
}
return
}
if (force) {
// Don't play sounds for 8 more seconds when you just joined.
this.lastPlayedJoin = currentTime + 8000
this.lastPlayedLeave = currentTime + 8000
console.debug('Playing join sound because of self joining')
} else {
this.lastPlayedJoin = currentTime
console.debug('Playing join sound')
}
this._playFile('LibremEmailNotification.ogg')
},
playLeave(force) {
if (!this.isInCall) {
return
}
const currentTime = (new Date()).getTime()
if (!force && this.lastPlayedLeave >= (currentTime - 7000)) {
if (this.lastPlayedLeave >= (currentTime - 7000)) {
console.debug('Skipping leave sound because it was played %f.2 seconds ago', currentTime - this.lastPlayedLeave)
}
return
}
if (force) {
console.debug('Playing leave sound because of self leaving')
this.isInCall = false
} else {
console.debug('Playing leave sound')
}
this.lastPlayedLeave = currentTime
this._playFile('LibremTextMessage.ogg')
},
}

32
src/utils/webrtc/webrtc.js

@ -34,6 +34,7 @@ import {
TOAST_PERMANENT_TIMEOUT,
TOAST_DEFAULT_TIMEOUT,
} from '@nextcloud/dialogs'
import { Sounds } from '../sounds.js'
let webrtc
const spreedPeerConnectionTable = []
@ -211,6 +212,9 @@ function usersChanged(signaling, newUsers, disconnectedSessionIds) {
checkStartPublishOwnPeer(signaling)
}
let playJoinSound = false
let playLeaveSound = false
newUsers.forEach(function(user) {
if (!user.inCall) {
return
@ -219,6 +223,9 @@ function usersChanged(signaling, newUsers, disconnectedSessionIds) {
// TODO(fancycode): Adjust property name of internal PHP backend to be all lowercase.
const sessionId = user.sessionId || user.sessionid
if (!sessionId || sessionId === currentSessionId || previousUsersInRoom.indexOf(sessionId) !== -1) {
if (sessionId === currentSessionId && previousUsersInRoom.indexOf(sessionId) !== -1) {
Sounds.playJoin(true)
}
return
}
@ -270,6 +277,8 @@ function usersChanged(signaling, newUsers, disconnectedSessionIds) {
}
}
playJoinSound = true
const createPeer = function() {
const peer = webrtc.webrtc.createPeer({
id: sessionId,
@ -339,12 +348,24 @@ function usersChanged(signaling, newUsers, disconnectedSessionIds) {
clearInterval(delayedConnectionToPeer[sessionId])
delete delayedConnectionToPeer[sessionId]
}
playLeaveSound = true
})
if (selfInCall !== PARTICIPANT.CALL_FLAG.DISCONNECTED) {
if (playJoinSound) {
Sounds.playJoin()
} else if (playLeaveSound) {
Sounds.playLeave()
}
}
previousUsersInRoom = arrayDiff(previousUsersInRoom, disconnectedSessionIds)
}
function usersInCallChanged(signaling, users) {
const previousSelfInCall = selfInCall
// The passed list are the users that are currently in the room,
// i.e. that are in the call and should call each other.
const currentSessionId = signaling.getSessionId()
@ -370,7 +391,15 @@ function usersInCallChanged(signaling, users) {
userMapping[sessionId] = user
}
if (!selfInCall) {
if (previousSelfInCall === PARTICIPANT.CALL_FLAG.DISCONNECTED
&& selfInCall !== PARTICIPANT.CALL_FLAG.DISCONNECTED) {
Sounds.playJoin(true)
} else if (previousSelfInCall !== PARTICIPANT.CALL_FLAG.DISCONNECTED
&& selfInCall === PARTICIPANT.CALL_FLAG.DISCONNECTED) {
Sounds.playLeave(true)
}
if (selfInCall === PARTICIPANT.CALL_FLAG.DISCONNECTED) {
// Own session is no longer in the call, disconnect from all others.
usersChanged(signaling, [], previousUsersInRoom)
return
@ -440,6 +469,7 @@ export default function initWebRTC(signaling, _callParticipantCollection, _local
}
clearErrorNotification()
Sounds.playLeave(true)
webrtc.leaveCall()
})

Loading…
Cancel
Save