Skip to content

Commit

Permalink
fix bitrate controller cause panic
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohan Totting committed Jan 11, 2024
1 parent d713276 commit fadf2e7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
36 changes: 14 additions & 22 deletions bitratecontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func (bc *bitrateController) Claims() map[string]*bitrateClaim {
}

func (bc *bitrateController) Exist(id string) bool {
bc.mu.Lock()
defer bc.mu.Unlock()
bc.mu.RLock()
defer bc.mu.RUnlock()

if _, ok := bc.claims[id]; ok {
return true
Expand All @@ -114,8 +114,8 @@ func (bc *bitrateController) Exist(id string) bool {
}

func (bc *bitrateController) GetClaim(id string) *bitrateClaim {
bc.mu.Lock()
defer bc.mu.Unlock()
bc.mu.RLock()
defer bc.mu.RUnlock()

if claim, ok := bc.claims[id]; ok && claim != nil {
return claim
Expand All @@ -126,15 +126,12 @@ func (bc *bitrateController) GetClaim(id string) *bitrateClaim {

// if all is false, only active claims will be counted
func (bc *bitrateController) TotalBitrates() uint32 {
bc.mu.Lock()
defer bc.mu.Unlock()

return bc.totalBitrates()
}

func (bc *bitrateController) totalBitrates() uint32 {
total := uint32(0)
for _, claim := range bc.claims {
for _, claim := range bc.Claims() {
total += claim.bitrate
}

Expand All @@ -161,8 +158,8 @@ func (bc *bitrateController) setQuality(clientTrackID string, quality QualityLev
}

func (bc *bitrateController) setSimulcastClaim(clientTrackID string, simulcast bool) {
bc.mu.RLock()
defer bc.mu.RUnlock()
bc.mu.Lock()
defer bc.mu.Unlock()

if claim, ok := bc.claims[clientTrackID]; ok {
claim.simulcast = simulcast
Expand Down Expand Up @@ -214,9 +211,6 @@ func (bc *bitrateController) checkAllTrackActive(claim *bitrateClaim) (bool, Qua
}

func (bc *bitrateController) addAudioClaims(clientTracks []iClientTrack) (leftTracks []iClientTrack, err error) {
bc.mu.Lock()
defer bc.mu.Unlock()

errors := make([]error, 0)

leftTracks = make([]iClientTrack, 0)
Expand Down Expand Up @@ -275,19 +269,19 @@ func (bc *bitrateController) addClaims(clientTracks []iClientTrack) error {
return err
}

bc.mu.Lock()
defer bc.mu.Unlock()

errors := make([]error, 0)
claimed := 0

for _, clientTrack := range leftTracks {
if clientTrack.Kind() == webrtc.RTPCodecTypeVideo {
trackQuality := bc.getDistributedQuality(len(leftTracks) - claimed)
bc.mu.RLock()
if _, ok := bc.claims[clientTrack.ID()]; ok {
errors = append(errors, ErrAlreadyClaimed)
bc.mu.RUnlock()
continue
}
bc.mu.RUnlock()

if !clientTrack.IsSimulcast() && !clientTrack.IsScaleable() {
trackQuality = QualityHigh
Expand Down Expand Up @@ -318,10 +312,8 @@ func (bc *bitrateController) addClaims(clientTracks []iClientTrack) error {
func (bc *bitrateController) addClaim(clientTrack iClientTrack, quality QualityLevel, locked bool) (*bitrateClaim, error) {
bitrate := bc.client.sfu.QualityLevelToBitrate(quality)

if !locked {
bc.mu.RLock()
defer bc.mu.RUnlock()
}
bc.mu.Lock()
defer bc.mu.Unlock()

bc.claims[clientTrack.ID()] = &bitrateClaim{
mu: sync.Mutex{},
Expand Down Expand Up @@ -358,8 +350,8 @@ func (bc *bitrateController) removeClaim(id string) {
}

func (bc *bitrateController) exists(id string) bool {
bc.mu.Lock()
defer bc.mu.Unlock()
bc.mu.RLock()
defer bc.mu.RUnlock()

if _, ok := bc.claims[id]; ok {
return true
Expand Down
2 changes: 1 addition & 1 deletion examples/http-websocket/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@
sendEncodings: [
{
maxBitrate: 1200*1000,
scalabilityMode: 'L3T3'
scalabilityMode: 'L3T1'
},

]
Expand Down

0 comments on commit fadf2e7

Please sign in to comment.