-
Notifications
You must be signed in to change notification settings - Fork 109
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
base: main
Are you sure you want to change the base?
Changes from all commits
9bfcd12
65449b9
ee13a15
4fe7de2
90e0593
0d0a0b5
12cc02c
efa8682
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 |
---|---|---|
|
@@ -5,15 +5,20 @@ import ( | |
"errors" | ||
"sync" | ||
|
||
"github.com/edaniels/golog" | ||
"github.com/pion/webrtc/v3" | ||
"go.viam.com/utils/rpc" | ||
"golang.org/x/exp/maps" | ||
googlegrpc "google.golang.org/grpc" | ||
) | ||
|
||
// ReconfigurableClientConn allows for the underlying client connections to be swapped under the hood. | ||
type ReconfigurableClientConn struct { | ||
connMu sync.RWMutex | ||
conn rpc.ClientConn | ||
|
||
onTrackCBByTrackNameMu sync.Mutex | ||
onTrackCBByTrackName map[string]OnTrackCB | ||
} | ||
|
||
// Return this constant such that backoff error logging can compare consecutive errors and reliably | ||
|
@@ -57,8 +62,31 @@ func (c *ReconfigurableClientConn) NewStream( | |
// ReplaceConn replaces the underlying client connection with the connection passed in. This does not close the | ||
// old connection, the caller is expected to close it if needed. | ||
func (c *ReconfigurableClientConn) ReplaceConn(conn rpc.ClientConn) { | ||
golog.Global().Info("ReplaceConn START") | ||
defer golog.Global().Info("ReplaceConn END") | ||
c.connMu.Lock() | ||
c.conn = conn | ||
// It is safe to access this without a mutex as it is only ever nil once at the beginning of the | ||
// ReconfigurableClientConn's lifetime | ||
if c.onTrackCBByTrackName == nil { | ||
c.onTrackCBByTrackName = make(map[string]OnTrackCB) | ||
} | ||
|
||
if pc := conn.PeerConn(); pc != nil { | ||
pc.OnTrack(func(trackRemote *webrtc.TrackRemote, rtpReceiver *webrtc.RTPReceiver) { | ||
golog.Global().Warnf("OnTrack START %s pc: %p", trackRemote.StreamID(), pc) | ||
defer golog.Global().Warnf("OnTrack END %s pc: %p", trackRemote.StreamID(), pc) | ||
Comment on lines
+77
to
+78
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. remove |
||
c.onTrackCBByTrackNameMu.Lock() | ||
onTrackCB, ok := c.onTrackCBByTrackName[trackRemote.StreamID()] | ||
c.onTrackCBByTrackNameMu.Unlock() | ||
if !ok { | ||
msg := "Callback not found for StreamID (trackName): %s, keys(resOnTrackCBs): %#v" | ||
golog.Global().Errorf(msg, trackRemote.StreamID(), maps.Keys(c.onTrackCBByTrackName)) | ||
return | ||
} | ||
onTrackCB(trackRemote, rtpReceiver) | ||
}) | ||
} | ||
c.connMu.Unlock() | ||
} | ||
|
||
|
@@ -84,3 +112,17 @@ func (c *ReconfigurableClientConn) Close() error { | |
c.conn = nil | ||
return conn.Close() | ||
} | ||
|
||
// AddOnTrackSub adds an OnTrack subscription for the track. | ||
func (c *ReconfigurableClientConn) AddOnTrackSub(trackName string, onTrackCB OnTrackCB) { | ||
c.onTrackCBByTrackNameMu.Lock() | ||
defer c.onTrackCBByTrackNameMu.Unlock() | ||
c.onTrackCBByTrackName[trackName] = onTrackCB | ||
} | ||
|
||
// RemoveOnTrackSub removes an OnTrack subscription for the track. | ||
func (c *ReconfigurableClientConn) RemoveOnTrackSub(trackName string) { | ||
c.onTrackCBByTrackNameMu.Lock() | ||
defer c.onTrackCBByTrackNameMu.Unlock() | ||
delete(c.onTrackCBByTrackName, trackName) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ package grpc | |
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net" | ||
"sync" | ||
"time" | ||
|
@@ -16,10 +15,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" | ||
) | ||
|
||
|
@@ -81,8 +80,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. renameed variables as we are no longer storing resource names, but rather strings of the resource name 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 | ||
} | ||
|
@@ -107,18 +106,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. | ||
|
@@ -160,9 +159,11 @@ func (sc *SharedConn) ResetConn(conn rpc.ClientConn, moduleLogger logging.Logger | |
sc.logger = moduleLogger.Sublogger("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() | ||
|
@@ -200,16 +201,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) | ||
|
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) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -356,11 +356,15 @@ func (rc *RobotClient) connectWithLock(ctx context.Context) error { | |
rc.conn.ReplaceConn(conn) | ||
rc.client = client | ||
rc.refClient = refClient | ||
rc.logger.Warn("connected") | ||
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. remove |
||
rc.connected.Store(true) | ||
if len(rc.resourceClients) != 0 { | ||
rc.logger.Warn("updateResources START") | ||
if err := rc.updateResources(ctx); err != nil { | ||
rc.logger.Errorf("updateResources END, err: %s", err.Error()) | ||
return err | ||
} | ||
rc.logger.Warn("updateResources END") | ||
} | ||
|
||
if rc.changeChan != nil { | ||
|
@@ -450,6 +454,7 @@ func (rc *RobotClient) checkConnection(ctx context.Context, checkEvery, reconnec | |
"reconnect_interval", reconnectEvery.Seconds(), | ||
) | ||
rc.mu.Lock() | ||
rc.logger.Warn("NOT connected") | ||
rc.connected.Store(false) | ||
if rc.changeChan != nil { | ||
rc.changeChan <- true | ||
|
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.
remove