1
Fork 0

Rename client to webClient.

This commit is contained in:
Juliusz Chroboczek 2020-05-27 11:44:49 +02:00
parent 50982fddc6
commit d8b984979b
3 changed files with 55 additions and 55 deletions

View File

@ -281,7 +281,7 @@ type downConnection interface {
type rtpDownConnection struct { type rtpDownConnection struct {
id string id string
client *client client *webClient
pc *webrtc.PeerConnection pc *webrtc.PeerConnection
remote *upConnection remote *upConnection
tracks []*rtpDownTrack tracks []*rtpDownTrack

View File

@ -21,22 +21,6 @@ import (
"github.com/pion/webrtc/v2" "github.com/pion/webrtc/v2"
) )
type client struct {
group *group
id string
username string
permissions userPermission
requested map[string]uint32
done chan struct{}
writeCh chan interface{}
writerDone chan struct{}
actionCh chan interface{}
mu sync.Mutex
down map[string]*rtpDownConnection
up map[string]*upConnection
}
type chatHistoryEntry struct { type chatHistoryEntry struct {
id string id string
user string user string
@ -57,7 +41,7 @@ type group struct {
locked uint32 locked uint32
mu sync.Mutex mu sync.Mutex
clients map[string]*client clients map[string]*webClient
history []chatHistoryEntry history []chatHistoryEntry
} }
@ -80,7 +64,7 @@ type getUpAction struct {
} }
type pushConnsAction struct { type pushConnsAction struct {
c *client c *webClient
} }
type connectionFailedAction struct { type connectionFailedAction struct {
@ -138,7 +122,7 @@ func addGroup(name string, desc *groupDescription) (*group, error) {
g = &group{ g = &group{
name: name, name: name,
description: desc, description: desc,
clients: make(map[string]*client), clients: make(map[string]*webClient),
} }
groups.groups[name] = g groups.groups[name] = g
} else if desc != nil { } else if desc != nil {
@ -211,7 +195,7 @@ type userid struct {
username string username string
} }
func addClient(name string, client *client, user, pass string) (*group, []userid, error) { func addClient(name string, client *webClient, user, pass string) (*group, []userid, error) {
g, err := addGroup(name, nil) g, err := addGroup(name, nil)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -247,7 +231,7 @@ func addClient(name string, client *client, user, pass string) (*group, []userid
return g, users, nil return g, users, nil
} }
func delClient(c *client) { func delClient(c *webClient) {
c.group.mu.Lock() c.group.mu.Lock()
defer c.group.mu.Unlock() defer c.group.mu.Unlock()
g := c.group g := c.group
@ -259,10 +243,10 @@ func delClient(c *client) {
delete(g.clients, c.id) delete(g.clients, c.id)
} }
func (g *group) getClients(except *client) []*client { func (g *group) getClients(except *webClient) []*webClient {
g.mu.Lock() g.mu.Lock()
defer g.mu.Unlock() defer g.mu.Unlock()
clients := make([]*client, 0, len(g.clients)) clients := make([]*webClient, 0, len(g.clients))
for _, c := range g.clients { for _, c := range g.clients {
if c != except { if c != except {
clients = append(clients, c) clients = append(clients, c)
@ -271,7 +255,7 @@ func (g *group) getClients(except *client) []*client {
return clients return clients
} }
func (g *group) getClientUnlocked(id string) *client { func (g *group) getClientUnlocked(id string) *webClient {
for _, c := range g.clients { for _, c := range g.clients {
if c.id == id { if c.id == id {
return c return c
@ -280,7 +264,7 @@ func (g *group) getClientUnlocked(id string) *client {
return nil return nil
} }
func (g *group) Range(f func(c *client) bool) { func (g *group) Range(f func(c *webClient) bool) {
g.mu.Lock() g.mu.Lock()
defer g.mu.Unlock() defer g.mu.Unlock()
for _, c := range g.clients { for _, c := range g.clients {
@ -327,7 +311,7 @@ func (err writerDeadError) Error() string {
return "client writer died" return "client writer died"
} }
func (c *client) write(m clientMessage) error { func (c *webClient) write(m clientMessage) error {
select { select {
case c.writeCh <- m: case c.writeCh <- m:
return nil return nil
@ -336,7 +320,7 @@ func (c *client) write(m clientMessage) error {
} }
} }
func (c *client) error(err error) error { func (c *webClient) error(err error) error {
switch e := err.(type) { switch e := err.(type) {
case userError: case userError:
return c.write(clientMessage{ return c.write(clientMessage{
@ -354,7 +338,7 @@ func (err clientDeadError) Error() string {
return "client dead" return "client dead"
} }
func (c *client) action(m interface{}) error { func (c *webClient) action(m interface{}) error {
select { select {
case c.actionCh <- m: case c.actionCh <- m:
return nil return nil
@ -611,7 +595,7 @@ func getGroupStats() []groupStats {
return gs return gs
} }
func getClientStats(c *client) clientStats { func getClientStats(c *webClient) clientStats {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()

View File

@ -91,6 +91,22 @@ func isWSNormalError(err error) bool {
websocket.CloseGoingAway) websocket.CloseGoingAway)
} }
type webClient struct {
group *group
id string
username string
permissions userPermission
requested map[string]uint32
done chan struct{}
writeCh chan interface{}
writerDone chan struct{}
actionCh chan interface{}
mu sync.Mutex
down map[string]*rtpDownConnection
up map[string]*upConnection
}
type rateMap map[string]uint32 type rateMap map[string]uint32
func (v *rateMap) UnmarshalJSON(b []byte) error { func (v *rateMap) UnmarshalJSON(b []byte) error {
@ -183,7 +199,7 @@ func startClient(conn *websocket.Conn) (err error) {
return return
} }
c := &client{ c := &webClient{
id: m.Id, id: m.Id,
username: m.Username, username: m.Username,
actionCh: make(chan interface{}, 10), actionCh: make(chan interface{}, 10),
@ -262,7 +278,7 @@ func startClient(conn *websocket.Conn) (err error) {
return clientLoop(c, conn) return clientLoop(c, conn)
} }
func getUpConn(c *client, id string) *upConnection { func getUpConn(c *webClient, id string) *upConnection {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
@ -276,7 +292,7 @@ func getUpConn(c *client, id string) *upConnection {
return conn return conn
} }
func getUpConns(c *client) []string { func getUpConns(c *webClient) []string {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
up := make([]string, 0, len(c.up)) up := make([]string, 0, len(c.up))
@ -286,7 +302,7 @@ func getUpConns(c *client) []string {
return up return up
} }
func addUpConn(c *client, id string) (*upConnection, error) { func addUpConn(c *webClient, id string) (*upConnection, error) {
pc, err := groups.api.NewPeerConnection(iceConfiguration()) pc, err := groups.api.NewPeerConnection(iceConfiguration())
if err != nil { if err != nil {
return nil, err return nil, err
@ -563,7 +579,7 @@ func rtcpUpListener(conn *upConnection, track *upTrack, r *webrtc.RTPReceiver) {
} }
} }
func sendRR(c *client, conn *upConnection) error { func sendRR(c *webClient, conn *upConnection) error {
c.mu.Lock() c.mu.Lock()
if len(conn.tracks) == 0 { if len(conn.tracks) == 0 {
c.mu.Unlock() c.mu.Unlock()
@ -604,7 +620,7 @@ func sendRR(c *client, conn *upConnection) error {
}) })
} }
func rtcpUpSender(c *client, conn *upConnection) { func rtcpUpSender(c *webClient, conn *upConnection) {
for { for {
time.Sleep(time.Second) time.Sleep(time.Second)
err := sendRR(c, conn) err := sendRR(c, conn)
@ -617,7 +633,7 @@ func rtcpUpSender(c *client, conn *upConnection) {
} }
} }
func delUpConn(c *client, id string) bool { func delUpConn(c *webClient, id string) bool {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
@ -653,7 +669,7 @@ func delUpConn(c *client, id string) bool {
return true return true
} }
func getDownConn(c *client, id string) *rtpDownConnection { func getDownConn(c *webClient, id string) *rtpDownConnection {
if c.down == nil { if c.down == nil {
return nil return nil
} }
@ -667,7 +683,7 @@ func getDownConn(c *client, id string) *rtpDownConnection {
return conn return conn
} }
func getConn(c *client, id string) iceConnection { func getConn(c *webClient, id string) iceConnection {
up := getUpConn(c, id) up := getUpConn(c, id)
if up != nil { if up != nil {
return up return up
@ -679,7 +695,7 @@ func getConn(c *client, id string) iceConnection {
return nil return nil
} }
func addDownConn(c *client, id string, remote *upConnection) (*rtpDownConnection, error) { func addDownConn(c *webClient, id string, remote *upConnection) (*rtpDownConnection, error) {
pc, err := groups.api.NewPeerConnection(iceConfiguration()) pc, err := groups.api.NewPeerConnection(iceConfiguration())
if err != nil { if err != nil {
return nil, err return nil, err
@ -716,7 +732,7 @@ func addDownConn(c *client, id string, remote *upConnection) (*rtpDownConnection
return conn, nil return conn, nil
} }
func delDownConn(c *client, id string) bool { func delDownConn(c *webClient, id string) bool {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
@ -740,7 +756,7 @@ func delDownConn(c *client, id string) bool {
return true return true
} }
func addDownTrack(c *client, conn *rtpDownConnection, remoteTrack *upTrack, remoteConn *upConnection) (*webrtc.RTPSender, error) { func addDownTrack(c *webClient, conn *rtpDownConnection, remoteTrack *upTrack, remoteConn *upConnection) (*webrtc.RTPSender, error) {
local, err := conn.pc.NewTrack( local, err := conn.pc.NewTrack(
remoteTrack.track.PayloadType(), remoteTrack.track.PayloadType(),
remoteTrack.track.SSRC(), remoteTrack.track.SSRC(),
@ -1058,7 +1074,7 @@ func sendRecovery(p *rtcp.TransportLayerNack, track *rtpDownTrack) {
} }
} }
func negotiate(c *client, down *rtpDownConnection) error { func negotiate(c *webClient, down *rtpDownConnection) error {
offer, err := down.pc.CreateOffer(nil) offer, err := down.pc.CreateOffer(nil)
if err != nil { if err != nil {
return err return err
@ -1094,7 +1110,7 @@ func negotiate(c *client, down *rtpDownConnection) error {
}) })
} }
func sendICE(c *client, id string, candidate *webrtc.ICECandidate) error { func sendICE(c *webClient, id string, candidate *webrtc.ICECandidate) error {
if candidate == nil { if candidate == nil {
return nil return nil
} }
@ -1106,7 +1122,7 @@ func sendICE(c *client, id string, candidate *webrtc.ICECandidate) error {
}) })
} }
func gotOffer(c *client, id string, offer webrtc.SessionDescription, labels map[string]string) error { func gotOffer(c *webClient, id string, offer webrtc.SessionDescription, labels map[string]string) error {
var err error var err error
up, ok := c.up[id] up, ok := c.up[id]
if !ok { if !ok {
@ -1147,7 +1163,7 @@ func gotOffer(c *client, id string, offer webrtc.SessionDescription, labels map[
}) })
} }
func gotAnswer(c *client, id string, answer webrtc.SessionDescription) error { func gotAnswer(c *webClient, id string, answer webrtc.SessionDescription) error {
down := getDownConn(c, id) down := getDownConn(c, id)
if down == nil { if down == nil {
return protocolError("unknown id in answer") return protocolError("unknown id in answer")
@ -1168,7 +1184,7 @@ func gotAnswer(c *client, id string, answer webrtc.SessionDescription) error {
return nil return nil
} }
func gotICE(c *client, candidate *webrtc.ICECandidateInit, id string) error { func gotICE(c *webClient, candidate *webrtc.ICECandidateInit, id string) error {
conn := getConn(c, id) conn := getConn(c, id)
if conn == nil { if conn == nil {
return errors.New("unknown id in ICE") return errors.New("unknown id in ICE")
@ -1176,7 +1192,7 @@ func gotICE(c *client, candidate *webrtc.ICECandidateInit, id string) error {
return conn.addICECandidate(candidate) return conn.addICECandidate(candidate)
} }
func (c *client) setRequested(requested map[string]uint32) error { func (c *webClient) setRequested(requested map[string]uint32) error {
if c.down != nil { if c.down != nil {
for id := range c.down { for id := range c.down {
c.write(clientMessage{ c.write(clientMessage{
@ -1199,11 +1215,11 @@ func (c *client) setRequested(requested map[string]uint32) error {
return nil return nil
} }
func (c *client) isRequested(label string) bool { func (c *webClient) isRequested(label string) bool {
return c.requested[label] != 0 return c.requested[label] != 0
} }
func addDownConnTracks(c *client, remote *upConnection, tracks []*upTrack) (*rtpDownConnection, error) { func addDownConnTracks(c *webClient, remote *upConnection, tracks []*upTrack) (*rtpDownConnection, error) {
requested := false requested := false
for _, t := range tracks { for _, t := range tracks {
if c.isRequested(t.label) { if c.isRequested(t.label) {
@ -1234,14 +1250,14 @@ func addDownConnTracks(c *client, remote *upConnection, tracks []*upTrack) (*rtp
return down, nil return down, nil
} }
func pushConn(c *client, conn *upConnection, tracks []*upTrack, label string) { func pushConn(c *webClient, conn *upConnection, tracks []*upTrack, label string) {
c.action(addConnAction{conn, tracks}) c.action(addConnAction{conn, tracks})
if label != "" { if label != "" {
c.action(addLabelAction{conn.id, conn.label}) c.action(addLabelAction{conn.id, conn.label})
} }
} }
func clientLoop(c *client, conn *websocket.Conn) error { func clientLoop(c *webClient, conn *websocket.Conn) error {
read := make(chan interface{}, 1) read := make(chan interface{}, 1)
go clientReader(conn, read, c.done) go clientReader(conn, read, c.done)
@ -1393,7 +1409,7 @@ func clientLoop(c *client, conn *websocket.Conn) error {
} }
} }
func failConnection(c *client, id string, message string) error { func failConnection(c *webClient, id string, message string) error {
if id != "" { if id != "" {
err := c.write(clientMessage{ err := c.write(clientMessage{
Type: "abort", Type: "abort",
@ -1412,7 +1428,7 @@ func failConnection(c *client, id string, message string) error {
return nil return nil
} }
func handleClientMessage(c *client, m clientMessage) error { func handleClientMessage(c *webClient, m clientMessage) error {
switch m.Type { switch m.Type {
case "request": case "request":
err := c.setRequested(m.Request) err := c.setRequested(m.Request)
@ -1507,7 +1523,7 @@ func handleClientMessage(c *client, m clientMessage) error {
return nil return nil
} }
func sendRateUpdate(c *client) { func sendRateUpdate(c *webClient) {
type remb struct { type remb struct {
pc *webrtc.PeerConnection pc *webrtc.PeerConnection
ssrc uint32 ssrc uint32