|
|
|
@ -6,7 +6,6 @@ import (
|
|
|
|
|
"math/rand" |
|
|
|
|
"net/http" |
|
|
|
|
"sync" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
rice "github.com/GeertJohan/go.rice" |
|
|
|
|
"github.com/gorilla/mux" |
|
|
|
@ -56,7 +55,6 @@ func NewWebInterface(address string, path string) *WebInterface {
|
|
|
|
|
r.PathPrefix(path).Handler(http.StripPrefix(path, http.FileServer(rice.MustFindBox("public").HTTPBox()))) |
|
|
|
|
|
|
|
|
|
go w.handleIncomingClients() |
|
|
|
|
go w.handleTerminatedClients() |
|
|
|
|
|
|
|
|
|
go func() { |
|
|
|
|
if err := http.ListenAndServe(address, r); err != nil { |
|
|
|
@ -84,27 +82,6 @@ func (w *WebInterface) handleIncomingClients() {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (w *WebInterface) handleTerminatedClients() { |
|
|
|
|
for { |
|
|
|
|
time.Sleep(15 * time.Second) |
|
|
|
|
|
|
|
|
|
w.ClientsLock.Lock() |
|
|
|
|
for id := range w.Clients { |
|
|
|
|
if w.Clients[id].Status != -1 { |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
name := w.Clients[id].Name |
|
|
|
|
delete(w.Clients, id) |
|
|
|
|
|
|
|
|
|
for _, wc := range w.Clients { |
|
|
|
|
wc.Out <- &Message{T: MessageDisconnect, M: []byte(name)} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
w.ClientsLock.Unlock() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (w *WebInterface) handleRead(c *Client) { |
|
|
|
|
for msg := range c.In { |
|
|
|
|
if msg == nil { |
|
|
|
@ -146,8 +123,9 @@ func (w *WebInterface) handleRead(c *Client) {
|
|
|
|
|
c.AudioTrack = nil |
|
|
|
|
|
|
|
|
|
if c.PeerConn != nil { |
|
|
|
|
c.PeerConn.Close() |
|
|
|
|
c.PeerConn = nil |
|
|
|
|
c.ClosePeerConn() |
|
|
|
|
|
|
|
|
|
log.Printf("closing peerconn, peer sent %s %d", msg.T, c.ID) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if msg.T == MessageDisconnect { |
|
|
|
@ -195,6 +173,21 @@ func (w *WebInterface) webSocketHandler(wr http.ResponseWriter, r *http.Request)
|
|
|
|
|
incomingClients <- c |
|
|
|
|
|
|
|
|
|
<-c.Terminated |
|
|
|
|
|
|
|
|
|
w.ClientsLock.Lock() |
|
|
|
|
for id := range w.Clients { |
|
|
|
|
if w.Clients[id].Status != -1 { |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
name := w.Clients[id].Name |
|
|
|
|
delete(w.Clients, id) |
|
|
|
|
|
|
|
|
|
for _, wc := range w.Clients { |
|
|
|
|
wc.Out <- &Message{T: MessageDisconnect, M: []byte(name)} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
w.ClientsLock.Unlock() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (w *WebInterface) answerRTC(c *Client, sdp []byte) ([]byte, error) { |
|
|
|
@ -238,7 +231,9 @@ func (w *WebInterface) answerRTC(c *Client, sdp []byte) ([]byte, error) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if payloadType == 0 { |
|
|
|
|
panic("no payloadType") |
|
|
|
|
c.ClosePeerConn() |
|
|
|
|
|
|
|
|
|
return nil, errors.New("no payloadType") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
t, err := pc.NewTrack(payloadType, rand.Uint32(), "audio", "harmony") |
|
|
|
@ -248,7 +243,7 @@ func (w *WebInterface) answerRTC(c *Client, sdp []byte) ([]byte, error) {
|
|
|
|
|
|
|
|
|
|
c.AudioTrack = t |
|
|
|
|
|
|
|
|
|
_, err = pc.AddTrack(t) |
|
|
|
|
_, err = pc.AddTrack(c.AudioTrack) |
|
|
|
|
if err != nil { |
|
|
|
|
panic(err) |
|
|
|
|
} |
|
|
|
@ -261,8 +256,8 @@ func (w *WebInterface) answerRTC(c *Client, sdp []byte) ([]byte, error) {
|
|
|
|
|
for { |
|
|
|
|
p, err = remoteTrack.ReadRTP() |
|
|
|
|
if err != nil { |
|
|
|
|
c.AudioTrack = nil |
|
|
|
|
c.PeerConn.Close() |
|
|
|
|
c.ClosePeerConn() |
|
|
|
|
log.Printf("failed to read RTP from %d", c.ID) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -274,10 +269,13 @@ func (w *WebInterface) answerRTC(c *Client, sdp []byte) ([]byte, error) {
|
|
|
|
|
|
|
|
|
|
p.SSRC = wc.AudioTrack.SSRC() |
|
|
|
|
p.PayloadType = wc.AudioTrack.PayloadType() |
|
|
|
|
p.SequenceNumber = wc.SequenceNumber |
|
|
|
|
|
|
|
|
|
if err = wc.AudioTrack.WriteRTP(p); err != nil && err != io.ErrClosedPipe { |
|
|
|
|
panic(err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
wc.SequenceNumber++ |
|
|
|
|
} |
|
|
|
|
w.ClientsLock.Unlock() |
|
|
|
|
} |
|
|
|
@ -290,10 +288,9 @@ func (w *WebInterface) answerRTC(c *Client, sdp []byte) ([]byte, error) {
|
|
|
|
|
w.ClientsLock.Lock() |
|
|
|
|
|
|
|
|
|
if connectionState == webrtc.PeerConnectionStateDisconnected { |
|
|
|
|
c.AudioTrack = nil |
|
|
|
|
c.ClosePeerConn() |
|
|
|
|
|
|
|
|
|
c.PeerConn.Close() |
|
|
|
|
c.PeerConn = nil |
|
|
|
|
log.Printf("closing peerconn, peer disconnected %d", c.ID) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, wc := range w.Clients { |
|
|
|
|