Commit 37b19940 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Fail addLocal if connection is closed.

parent 7972edfc
...@@ -50,17 +50,19 @@ func (up *upTrack) notifyLocal(add bool, track downTrack) { ...@@ -50,17 +50,19 @@ func (up *upTrack) notifyLocal(add bool, track downTrack) {
} }
} }
func (up *upTrack) addLocal(local downTrack) { func (up *upTrack) addLocal(local downTrack) error {
up.mu.Lock() up.mu.Lock()
for _, t := range up.local { for _, t := range up.local {
if t == local { if t == local {
up.mu.Unlock() up.mu.Unlock()
return return nil
} }
} }
up.local = append(up.local, local) up.local = append(up.local, local)
up.mu.Unlock() up.mu.Unlock()
up.notifyLocal(true, local) up.notifyLocal(true, local)
return nil
} }
func (up *upTrack) delLocal(local downTrack) bool { func (up *upTrack) delLocal(local downTrack) bool {
...@@ -108,19 +110,25 @@ type upConnection struct { ...@@ -108,19 +110,25 @@ type upConnection struct {
iceCandidates []*webrtc.ICECandidateInit iceCandidates []*webrtc.ICECandidateInit
mu sync.Mutex mu sync.Mutex
closed bool
local []downConnection local []downConnection
} }
func (up *upConnection) addLocal(local downConnection) { var ErrConnectionClosed = errors.New("connection is closed")
func (up *upConnection) addLocal(local downConnection) error {
up.mu.Lock() up.mu.Lock()
defer up.mu.Unlock() defer up.mu.Unlock()
if up.closed {
return ErrConnectionClosed
}
for _, t := range up.local { for _, t := range up.local {
if t == local { if t == local {
up.mu.Unlock() return nil
return
} }
} }
up.local = append(up.local, local) up.local = append(up.local, local)
return nil
} }
func (up *upConnection) delLocal(local downConnection) bool { func (up *upConnection) delLocal(local downConnection) bool {
...@@ -143,6 +151,21 @@ func (up *upConnection) getLocal() []downConnection { ...@@ -143,6 +151,21 @@ func (up *upConnection) getLocal() []downConnection {
return local return local
} }
func (up *upConnection) Close() error {
up.mu.Lock()
defer up.mu.Unlock()
go func(local []downConnection) {
for _, l := range local {
l.Close()
}
}(up.local)
up.local = nil
up.closed = true
return up.pc.Close()
}
func (up *upConnection) addICECandidate(candidate *webrtc.ICECandidateInit) error { func (up *upConnection) addICECandidate(candidate *webrtc.ICECandidateInit) error {
if up.pc.RemoteDescription() != nil { if up.pc.RemoteDescription() != nil {
return up.pc.AddICECandidate(*candidate) return up.pc.AddICECandidate(*candidate)
......
...@@ -194,7 +194,10 @@ func newDiskConn(directory, label string, up *upConnection, remoteTracks []*upTr ...@@ -194,7 +194,10 @@ func newDiskConn(directory, label string, up *upConnection, remoteTracks []*upTr
} }
} }
up.addLocal(&conn) err := up.addLocal(&conn)
if err != nil {
return nil, err
}
return &conn, nil return &conn, nil
} }
......
...@@ -675,14 +675,7 @@ func delUpConn(c *webClient, id string) bool { ...@@ -675,14 +675,7 @@ func delUpConn(c *webClient, id string) bool {
} }
} }
local := conn.getLocal() conn.Close()
go func() {
for _, l := range local {
l.Close()
}
}()
conn.pc.Close()
delete(c.up, id) delete(c.up, id)
return true return true
} }
...@@ -744,9 +737,13 @@ func addDownConn(c *webClient, id string, remote *upConnection) (*rtpDownConnect ...@@ -744,9 +737,13 @@ func addDownConn(c *webClient, id string, remote *upConnection) (*rtpDownConnect
conn.pc.Close() conn.pc.Close()
return nil, errors.New("Adding duplicate connection") return nil, errors.New("Adding duplicate connection")
} }
c.down[id] = conn err = remote.addLocal(conn)
if err != nil {
conn.pc.Close()
return nil, err
}
remote.addLocal(conn) c.down[id] = conn
return conn, nil return conn, nil
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment