-
Notifications
You must be signed in to change notification settings - Fork 113
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
RSDK-7403 - add remote rtp_passthrough support #3957
Changes from all commits
a2a389a
25b3720
0da90a1
6be640f
192e7c7
96981d9
7e6e5fa
677392d
5529876
eeda863
ad73e2d
785a5c0
d338773
3024f21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ package grpc | |
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net" | ||
"sync" | ||
"time" | ||
|
@@ -15,10 +14,10 @@ import ( | |
"go.uber.org/zap" | ||
"go.viam.com/utils" | ||
"go.viam.com/utils/rpc" | ||
"golang.org/x/exp/maps" | ||
googlegrpc "google.golang.org/grpc" | ||
|
||
"go.viam.com/rdk/logging" | ||
"go.viam.com/rdk/resource" | ||
rutils "go.viam.com/rdk/utils" | ||
) | ||
|
||
|
@@ -80,8 +79,8 @@ type SharedConn struct { | |
// set to nil before this channel is closed. | ||
peerConnFailed chan struct{} | ||
|
||
resOnTrackMu sync.Mutex | ||
resOnTrackCBs map[resource.Name]OnTrackCB | ||
onTrackCBByTrackNameMu sync.Mutex | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pull out the name changes into a separate PR |
||
onTrackCBByTrackName map[string]OnTrackCB | ||
|
||
logger logging.Logger | ||
} | ||
|
@@ -106,18 +105,18 @@ func (sc *SharedConn) NewStream( | |
return sc.grpcConn.NewStream(ctx, desc, method, opts...) | ||
} | ||
|
||
// AddOnTrackSub adds an OnTrack subscription for the resource. | ||
func (sc *SharedConn) AddOnTrackSub(name resource.Name, onTrackCB OnTrackCB) { | ||
sc.resOnTrackMu.Lock() | ||
defer sc.resOnTrackMu.Unlock() | ||
sc.resOnTrackCBs[name] = onTrackCB | ||
// AddOnTrackSub adds an OnTrack subscription for the track. | ||
func (sc *SharedConn) AddOnTrackSub(trackName string, onTrackCB OnTrackCB) { | ||
sc.onTrackCBByTrackNameMu.Lock() | ||
defer sc.onTrackCBByTrackNameMu.Unlock() | ||
sc.onTrackCBByTrackName[trackName] = onTrackCB | ||
} | ||
|
||
// RemoveOnTrackSub removes an OnTrack subscription for the resource. | ||
func (sc *SharedConn) RemoveOnTrackSub(name resource.Name) { | ||
sc.resOnTrackMu.Lock() | ||
defer sc.resOnTrackMu.Unlock() | ||
delete(sc.resOnTrackCBs, name) | ||
// RemoveOnTrackSub removes an OnTrack subscription for the track. | ||
func (sc *SharedConn) RemoveOnTrackSub(trackName string) { | ||
sc.onTrackCBByTrackNameMu.Lock() | ||
defer sc.onTrackCBByTrackNameMu.Unlock() | ||
delete(sc.onTrackCBByTrackName, trackName) | ||
} | ||
|
||
// GrpcConn returns a gRPC capable client connection. | ||
|
@@ -159,9 +158,11 @@ func (sc *SharedConn) ResetConn(conn rpc.ClientConn, moduleLogger logging.Logger | |
sc.logger = moduleLogger.Sublogger("networking.conn") | ||
} | ||
|
||
if sc.resOnTrackCBs == nil { | ||
// It is safe to access this without a mutex as it is only ever nil once at the beginning of the | ||
// SharedConn's lifetime | ||
if sc.onTrackCBByTrackName == nil { | ||
// Same initilization argument as above with the logger. | ||
sc.resOnTrackCBs = make(map[resource.Name]OnTrackCB) | ||
sc.onTrackCBByTrackName = make(map[string]OnTrackCB) | ||
} | ||
|
||
sc.peerConnMu.Lock() | ||
|
@@ -199,16 +200,12 @@ func (sc *SharedConn) ResetConn(conn rpc.ClientConn, moduleLogger logging.Logger | |
} | ||
|
||
sc.peerConn.OnTrack(func(trackRemote *webrtc.TrackRemote, rtpReceiver *webrtc.RTPReceiver) { | ||
name, err := resource.NewFromString(trackRemote.StreamID()) | ||
if err != nil { | ||
sc.logger.Errorw("StreamID did not parse as a ResourceName", "sharedConn", fmt.Sprintf("%p", sc), "streamID", trackRemote.StreamID()) | ||
return | ||
} | ||
sc.resOnTrackMu.Lock() | ||
onTrackCB, ok := sc.resOnTrackCBs[name] | ||
sc.resOnTrackMu.Unlock() | ||
sc.onTrackCBByTrackNameMu.Lock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed the requirement that the streamID (aka track name) parse as a resource name as we are now just storing strings in the callback lookup map There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth it to have the onTrackByTrackName as a separate struct with a separate mutex within the client struct? I always worry with multiple mutexes on the same struct that we're making the code harder to handle. Totally okay if the answer is no - too much redesign/I'm actually making it more complex. |
||
onTrackCB, ok := sc.onTrackCBByTrackName[trackRemote.StreamID()] | ||
sc.onTrackCBByTrackNameMu.Unlock() | ||
if !ok { | ||
sc.logger.Errorw("Callback not found for StreamID", "sharedConn", fmt.Sprintf("%p", sc), "streamID", trackRemote.StreamID()) | ||
msg := "Callback not found for StreamID: %s, keys(resOnTrackCBs): %#v" | ||
sc.logger.Errorf(msg, trackRemote.StreamID(), maps.Keys(sc.onTrackCBByTrackName)) | ||
return | ||
} | ||
onTrackCB(trackRemote, rtpReceiver) | ||
|
@@ -339,6 +336,7 @@ func NewLocalPeerConnection(logger logging.Logger) (*webrtc.PeerConnection, erro | |
|
||
return false | ||
}) | ||
settingEngine.DisableSRTPReplayProtection(true) | ||
|
||
options := []func(a *webrtc.API){webrtc.WithMediaEngine(&m), webrtc.WithInterceptorRegistry(&i)} | ||
if utils.Debug { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package grpc | ||
|
||
// Tracker allows callback functions to a WebRTC peer connection's OnTrack callback | ||
// function by track name. | ||
// Both grpc.SharedConn and grpc.ReconfigurableClientConn implement tracker. | ||
type Tracker interface { | ||
AddOnTrackSub(trackName string, onTrackCB OnTrackCB) | ||
RemoveOnTrackSub(trackName string) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package grpc | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"go.viam.com/test" | ||
) | ||
|
||
func TestTrackerImplementations(t *testing.T) { | ||
tracker := reflect.TypeOf((*Tracker)(nil)).Elem() | ||
|
||
t.Run("*ReconfigurableClientConn should implement Tracker", func(t *testing.T) { | ||
test.That(t, reflect.TypeOf(&ReconfigurableClientConn{}).Implements(tracker), test.ShouldBeTrue) | ||
}) | ||
|
||
t.Run("*SharedConn should implement Tracker", func(t *testing.T) { | ||
test.That(t, reflect.TypeOf(&SharedConn{}).Implements(tracker), test.ShouldBeTrue) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renameed variables as we are no longer storing resource names, but rather strings of the resource name