Close channel to avoid routine leak (#188)

This commit is contained in:
giongto35 2020-05-31 03:28:36 +08:00 committed by GitHub
parent ee9088a065
commit d84503fa75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 0 deletions

View file

@ -202,6 +202,11 @@ func (w *WebRTC) StartClient(isMobile bool, iceCB OnIceCallback) (string, error)
log.Println("Received Voice from Client")
for {
if w.RoomID == "" {
// skip sending voice when game is not running
continue
}
i, err := remoteTrack.Read(rtpBuf)
// TODO: can receive track but the voice doesn't work
if err == nil {
@ -290,6 +295,8 @@ func (w *WebRTC) StopClient() {
// NOTE: ImageChannel is waiting for input. Close in writer is not correct for this
close(w.ImageChannel)
close(w.AudioChannel)
close(w.VoiceInChannel)
close(w.VoiceOutChannel)
}
// IsConnected comment

View file

@ -49,6 +49,13 @@ func (r *Room) startVoice() {
// broadcast voice
go func() {
for sample := range r.voiceInChannel {
r.voiceOutChannel <- sample
}
}()
// fanout voice
go func() {
for sample := range r.voiceOutChannel {
for _, webRTC := range r.rtcSessions {
if webRTC.IsConnected() {
// NOTE: can block here
@ -56,6 +63,9 @@ func (r *Room) startVoice() {
}
}
}
for _, webRTC := range r.rtcSessions {
close(webRTC.VoiceOutChannel)
}
}()
}

View file

@ -222,6 +222,7 @@ func (r *Room) startWebRTCSession(peerconnection *webrtc.WebRTC) {
log.Println("Start WebRTC session")
go func() {
// set up voice input and output. A room has multiple voice input and only one combined voice output.
for voiceInput := range peerconnection.VoiceInChannel {
// NOTE: when room is no longer running. InputChannel needs to have extra event to go inside the loop
@ -262,6 +263,7 @@ func (r *Room) RemoveSession(w *webrtc.WebRTC) {
log.Println("found session: ", w.ID)
if s.ID == w.ID {
r.rtcSessions = append(r.rtcSessions[:i], r.rtcSessions[i+1:]...)
s.RoomID = ""
log.Println("Removed session ", s.ID, " from room: ", r.ID)
break
}
@ -309,6 +311,8 @@ func (r *Room) Close() {
}
log.Println("Closing input of room ", r.ID)
close(r.inputChannel)
close(r.voiceOutChannel)
close(r.voiceInChannel)
close(r.Done)
// Close here is a bit wrong because this read channel
// Just dont close it, let it be gc