mirror of
https://github.com/jech/galene.git
synced 2024-11-10 02:35:58 +01:00
Mute clients when bitrate is too low.
This commit is contained in:
parent
1de42bd80e
commit
87f70c4345
2 changed files with 41 additions and 15 deletions
34
client.go
34
client.go
|
@ -305,6 +305,9 @@ func addUpConn(c *client, id string) (*upConnection, error) {
|
||||||
|
|
||||||
local := track.getLocal()
|
local := track.getLocal()
|
||||||
for _, l := range local {
|
for _, l := range local {
|
||||||
|
if l.muted() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
err := l.track.WriteRTP(&packet)
|
err := l.track.WriteRTP(&packet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("%v", err)
|
log.Printf("%v", err)
|
||||||
|
@ -463,8 +466,11 @@ func addDownTrack(c *client, id string, remoteTrack *upTrack, remoteConn *upConn
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
track := &downTrack{local, remoteTrack, new(timeStampedBitrate)}
|
track := &downTrack{
|
||||||
|
track: local,
|
||||||
|
remote: remoteTrack,
|
||||||
|
maxBitrate: new(timeStampedBitrate),
|
||||||
|
}
|
||||||
conn.tracks = append(conn.tracks, track)
|
conn.tracks = append(conn.tracks, track)
|
||||||
remoteTrack.addLocal(track)
|
remoteTrack.addLocal(track)
|
||||||
|
|
||||||
|
@ -479,8 +485,7 @@ func msSinceEpoch() uint64 {
|
||||||
return uint64(time.Since(epoch) / time.Millisecond)
|
return uint64(time.Since(epoch) / time.Millisecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
func rtcpListener(g *group, c *downConnection, s *webrtc.RTPSender,
|
func rtcpListener(g *group, c *downConnection, s *webrtc.RTPSender, bitrate *timeStampedBitrate) {
|
||||||
bitrate *timeStampedBitrate) {
|
|
||||||
for {
|
for {
|
||||||
ps, err := s.ReadRTCP()
|
ps, err := s.ReadRTCP()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -539,20 +544,25 @@ func trackKinds(down *downConnection) (audio bool, video bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateUpBitrate(up *upConnection) {
|
func updateUpBitrate(up *upConnection) {
|
||||||
for _, t := range up.tracks {
|
|
||||||
t.maxBitrate = ^uint64(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
now := msSinceEpoch()
|
now := msSinceEpoch()
|
||||||
|
|
||||||
for _, track := range up.tracks {
|
for _, track := range up.tracks {
|
||||||
|
track.maxBitrate = ^uint64(0)
|
||||||
local := track.getLocal()
|
local := track.getLocal()
|
||||||
for _, l := range local {
|
for _, l := range local {
|
||||||
ms := atomic.LoadUint64(&l.maxBitrate.timestamp)
|
ms := atomic.LoadUint64(&l.maxBitrate.timestamp)
|
||||||
bitrate := atomic.LoadUint64(&l.maxBitrate.bitrate)
|
bitrate := atomic.LoadUint64(&l.maxBitrate.bitrate)
|
||||||
if now-ms > 5000 || bitrate == 0 {
|
if now < ms || now > ms + 5000 || bitrate == 0 {
|
||||||
|
l.setMuted(false)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if bitrate < 9600 ||
|
||||||
|
(l.track.Kind() == webrtc.RTPCodecTypeVideo &&
|
||||||
|
bitrate < 128000) {
|
||||||
|
l.setMuted(true)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
l.setMuted(false)
|
||||||
if track.maxBitrate > bitrate {
|
if track.maxBitrate > bitrate {
|
||||||
track.maxBitrate = bitrate
|
track.maxBitrate = bitrate
|
||||||
}
|
}
|
||||||
|
@ -917,15 +927,13 @@ func sendRateUpdate(c *client) {
|
||||||
updateUpBitrate(u)
|
updateUpBitrate(u)
|
||||||
for _, t := range u.tracks {
|
for _, t := range u.tracks {
|
||||||
bitrate := t.maxBitrate
|
bitrate := t.maxBitrate
|
||||||
if bitrate != ^uint64(0) {
|
if bitrate == ^uint64(0) {
|
||||||
if bitrate < 6000 {
|
continue
|
||||||
bitrate = 6000
|
|
||||||
}
|
}
|
||||||
rembs = append(rembs,
|
rembs = append(rembs,
|
||||||
remb{u.pc, t.track.SSRC(), bitrate})
|
remb{u.pc, t.track.SSRC(), bitrate})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
|
|
||||||
for _, r := range rembs {
|
for _, r := range rembs {
|
||||||
|
|
18
group.go
18
group.go
|
@ -12,6 +12,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pion/webrtc/v2"
|
"github.com/pion/webrtc/v2"
|
||||||
|
@ -63,12 +64,29 @@ type timeStampedBitrate struct {
|
||||||
bitrate uint64
|
bitrate uint64
|
||||||
timestamp uint64
|
timestamp uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
type downTrack struct {
|
type downTrack struct {
|
||||||
track *webrtc.Track
|
track *webrtc.Track
|
||||||
remote *upTrack
|
remote *upTrack
|
||||||
|
isMuted uint32
|
||||||
maxBitrate *timeStampedBitrate
|
maxBitrate *timeStampedBitrate
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *downTrack) muted() bool {
|
||||||
|
return atomic.LoadUint32(&t.isMuted) != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *downTrack) setMuted(muted bool) {
|
||||||
|
if t.muted() == muted {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m := uint32(0)
|
||||||
|
if muted {
|
||||||
|
m = 1
|
||||||
|
}
|
||||||
|
atomic.StoreUint32(&t.isMuted, m)
|
||||||
|
}
|
||||||
|
|
||||||
type downConnection struct {
|
type downConnection struct {
|
||||||
id string
|
id string
|
||||||
pc *webrtc.PeerConnection
|
pc *webrtc.PeerConnection
|
||||||
|
|
Loading…
Reference in a new issue