1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-22 16:45:58 +01:00

Push complete connections rather than individual tracks.

This commit is contained in:
Juliusz Chroboczek 2020-05-17 23:51:17 +02:00
parent b26a8cad78
commit b265aacd32
2 changed files with 63 additions and 50 deletions

104
client.go
View file

@ -308,15 +308,21 @@ func addUpConn(c *client, id string) (*upConnection, error) {
maxBitrate: ^uint64(0), maxBitrate: ^uint64(0),
} }
u.tracks = append(u.tracks, track) u.tracks = append(u.tracks, track)
done := u.complete() var tracks []*upTrack
if u.complete() {
tracks = make([]*upTrack, len(u.tracks))
copy(tracks, u.tracks)
}
if remote.Kind() == webrtc.RTPCodecTypeVideo { if remote.Kind() == webrtc.RTPCodecTypeVideo {
atomic.AddUint32(&c.group.videoCount, 1) atomic.AddUint32(&c.group.videoCount, 1)
} }
c.mu.Unlock() c.mu.Unlock()
clients := c.group.getClients(c) if tracks != nil {
for _, cc := range clients { clients := c.group.getClients(c)
pushTracks(cc, u, []*upTrack{track}, done, u.label) for _, cc := range clients {
pushConn(cc, u, tracks, u.label)
}
} }
go upLoop(conn, track) go upLoop(conn, track)
@ -578,16 +584,7 @@ func delDownConn(c *client, id string) bool {
return true return true
} }
func addDownTrack(c *client, id string, remoteTrack *upTrack, remoteConn *upConnection) (*downConnection, *webrtc.RTPSender, error) { func addDownTrack(c *client, conn *downConnection, remoteTrack *upTrack, remoteConn *upConnection) (*webrtc.RTPSender, error) {
conn := getDownConn(c, id)
if conn == nil {
var err error
conn, err = addDownConn(c, id, remoteConn)
if err != nil {
return nil, nil, err
}
}
local, err := conn.pc.NewTrack( local, err := conn.pc.NewTrack(
remoteTrack.track.PayloadType(), remoteTrack.track.PayloadType(),
remoteTrack.track.SSRC(), remoteTrack.track.SSRC(),
@ -595,12 +592,12 @@ func addDownTrack(c *client, id string, remoteTrack *upTrack, remoteConn *upConn
remoteTrack.track.Label(), remoteTrack.track.Label(),
) )
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
s, err := conn.pc.AddTrack(local) s, err := conn.pc.AddTrack(local)
if err != nil { if err != nil {
return nil, nil, err return nil, err
} }
track := &downTrack{ track := &downTrack{
@ -616,7 +613,7 @@ func addDownTrack(c *client, id string, remoteTrack *upTrack, remoteConn *upConn
go rtcpDownListener(conn, track, s) go rtcpDownListener(conn, track, s)
return conn, s, nil return s, nil
} }
const ( const (
@ -831,7 +828,7 @@ func sendRecovery(p *rtcp.TransportLayerNack, track *downTrack) {
} }
} }
func negotiate(c *client, id string, down *downConnection) error { func negotiate(c *client, down *downConnection) error {
offer, err := down.pc.CreateOffer(nil) offer, err := down.pc.CreateOffer(nil)
if err != nil { if err != nil {
return err return err
@ -849,7 +846,7 @@ func negotiate(c *client, id string, down *downConnection) error {
return c.write(clientMessage{ return c.write(clientMessage{
Type: "offer", Type: "offer",
Id: id, Id: down.id,
Offer: &offer, Offer: &offer,
Labels: labels, Labels: labels,
}) })
@ -946,7 +943,7 @@ func (c *client) setRequested(requested []string) error {
go func() { go func() {
clients := c.group.getClients(c) clients := c.group.getClients(c)
for _, cc := range clients { for _, cc := range clients {
cc.action(pushTracksAction{c}) cc.action(pushConnsAction{c})
} }
}() }()
@ -962,14 +959,41 @@ func (c *client) isRequested(label string) bool {
return false return false
} }
func pushTracks(c *client, conn *upConnection, tracks []*upTrack, done bool, label string) { func addDownConnTracks(c *client, remote *upConnection, tracks []*upTrack) (*downConnection, error) {
for i, t := range tracks { requested := false
c.action(addTrackAction{t, conn, done && i == len(tracks)-1}) for _, t := range tracks {
if c.isRequested(t.label) {
requested = true
break
}
}
if !requested {
return nil, nil
} }
if done && label != "" { down, err := addDownConn(c, remote.id, remote)
c.action(addLabelAction{conn.id, conn.label}) if err != nil {
return nil, err
}
for _, t := range tracks {
if !c.isRequested(t.label) {
continue
}
_, err = addDownTrack(c, down, t, remote)
if err != nil {
delDownConn(c, down.id)
return nil, err
}
}
return down, nil
}
func pushConn(c *client, conn *upConnection, tracks []*upTrack, label string) {
c.action(addConnAction{conn, tracks})
if label != "" {
c.action(addLabelAction{conn.id, conn.label})
} }
} }
@ -1030,21 +1054,15 @@ func clientLoop(c *client, conn *websocket.Conn) error {
} }
case a := <-c.actionCh: case a := <-c.actionCh:
switch a := a.(type) { switch a := a.(type) {
case addTrackAction: case addConnAction:
var down *downConnection down, err := addDownConnTracks(
var err error c, a.conn, a.tracks,
if c.isRequested(a.track.label) { )
down, _, err = addDownTrack( if err != nil {
c, a.remote.id, a.track, return err
a.remote)
if err != nil {
return err
}
} else {
down = getDownConn(c, a.remote.id)
} }
if a.done && down != nil { if down != nil {
err = negotiate(c, a.remote.id, down) err = negotiate(c, down)
if err != nil { if err != nil {
return err return err
} }
@ -1063,15 +1081,11 @@ func clientLoop(c *client, conn *websocket.Conn) error {
Id: a.id, Id: a.id,
Value: a.label, Value: a.label,
}) })
case pushTracksAction: case pushConnsAction:
for _, u := range c.up { for _, u := range c.up {
tracks := make([]*upTrack, len(u.tracks)) tracks := make([]*upTrack, len(u.tracks))
copy(tracks, u.tracks) copy(tracks, u.tracks)
go pushTracks( go pushConn(a.c, u, tracks, u.label)
a.c, u, tracks,
u.complete(),
u.label,
)
} }
case connectionFailedAction: case connectionFailedAction:
found := delUpConn(c, a.id) found := delUpConn(c, a.id)

View file

@ -209,10 +209,9 @@ type delConnAction struct {
id string id string
} }
type addTrackAction struct { type addConnAction struct {
track *upTrack conn *upConnection
remote *upConnection tracks []*upTrack
done bool
} }
type addLabelAction struct { type addLabelAction struct {
@ -224,7 +223,7 @@ type getUpAction struct {
ch chan<- string ch chan<- string
} }
type pushTracksAction struct { type pushConnsAction struct {
c *client c *client
} }