Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add force mute/unmute of client #6

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ type Client struct {
mu sync.RWMutex
peerConnection *PeerConnection
quicClient quic.Connection
isMuted *atomic.Bool
// pending received tracks are the remote tracks from other clients that waiting to add when the client is connected
pendingReceivedTracks []SubscribeTrackRequest
// pending published tracks are the remote tracks that still state as unknown source, and can't be published until the client state the source media or screen
Expand Down Expand Up @@ -329,6 +330,7 @@ func NewClient(s *SFU, id string, name string, peerConnectionConfig webrtc.Confi
mu: sync.RWMutex{},
negotiationNeeded: &atomic.Bool{},
peerConnection: newPeerConnection(peerConnection),
isMuted: &atomic.Bool{},
state: &stateNew,
tracks: newTrackList(opts.Log),
options: opts,
Expand Down Expand Up @@ -585,6 +587,24 @@ func NewClient(s *SFU, id string, name string, peerConnectionConfig webrtc.Confi
return client
}

func (c *Client) IsMuted() bool {
return c.isMuted.Load()
}

func (c *Client) Mute() {
c.isMuted.CompareAndSwap(false, true)
for _, track := range c.tracks.GetTracks() {
track.Mute()
}
}

func (c *Client) Unmute() {
c.isMuted.CompareAndSwap(true, false)
for _, track := range c.tracks.GetTracks() {
track.Unmute()
}
}

/*
Starts an indivisual client recording session.
Creates a new quic client solely for this client.
Expand Down
1 change: 1 addition & 0 deletions examples/http-websocket/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ func main() {
roomsOpts.Bitrates.InitialBandwidth = 1_000_000
// roomsOpts.PLIInterval = 3 * time.Second
defaultRoom, _ := roomManager.NewRoom(roomID, roomName, sfu.RoomTypeLocal, roomsOpts)

// turnServer := sfu.StartTurnServer(ctx, localIp.String())
// defer turnServer.Close()

Expand Down
21 changes: 21 additions & 0 deletions track.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ type ITrack interface {
StopRecording()
PauseRecording()
ContinueRecording()
Mute()
Unmute()
}

type Track struct {
Expand All @@ -82,6 +84,7 @@ type Track struct {
trackRecorder recorder.TrackRecorder
isRecording atomic.Bool
isPaused atomic.Bool
isMuted atomic.Bool
}

func newTrack(ctx context.Context, client *Client, trackRemote IRemoteTrack, minWait, maxWait, pliInterval time.Duration, onPLI func(), stats stats.Getter, onStatsUpdated func(*stats.Stats)) (ITrack, error) {
Expand All @@ -106,10 +109,16 @@ func newTrack(ctx context.Context, client *Client, trackRemote IRemoteTrack, min
onEndedCallbacks: make([]func(), 0),
isRecording: atomic.Bool{},
isPaused: atomic.Bool{},
isMuted: atomic.Bool{},
}

onRead := func(p *rtp.Packet) {
tracks := t.base.clientTracks.GetTracks()
if t.MimeType() == webrtc.MimeTypeOpus {
if t.isMuted.Load() {
p = t.getSilencePacket(p)
}
}

for _, track := range tracks {
//nolint:ineffassign,staticcheck // packet is from the pool
Expand Down Expand Up @@ -171,6 +180,14 @@ func newTrack(ctx context.Context, client *Client, trackRemote IRemoteTrack, min
return t, nil
}

func (t *Track) Mute() {
t.isMuted.CompareAndSwap(false, true)
}

func (t *Track) Unmute() {
t.isMuted.CompareAndSwap(true, false)
}

func (t *Track) StartRecording(stream quic.SendStream) error {
t.mu.Lock()
defer t.mu.Unlock()
Expand Down Expand Up @@ -499,6 +516,9 @@ func newSimulcastTrack(client *Client, track IRemoteTrack, minWait, maxWait, pli
return t
}

func (t *SimulcastTrack) Mute() {}
func (t *SimulcastTrack) Unmute() {}

func (t *SimulcastTrack) StartRecording(stream quic.SendStream) error {
return nil
}
Expand Down Expand Up @@ -621,6 +641,7 @@ func (t *SimulcastTrack) AddRemoteTrack(track IRemoteTrack, minWait, maxWait tim
}

tracks := t.base.clientTracks.GetTracks()

for _, track := range tracks {
//nolint:ineffassign,staticcheck // packet is from the pool
packet := t.base.pool.NewPacket(&p.Header, p.Payload)
Expand Down
Loading