diff --git a/internal/engine/jitsi/jitsi.go b/internal/engine/jitsi/jitsi.go index 9ea4b9f..2283ce2 100644 --- a/internal/engine/jitsi/jitsi.go +++ b/internal/engine/jitsi/jitsi.go @@ -316,6 +316,13 @@ func (s *Session) joinAndOpenBridge(ctx context.Context) (*j.Session, error) { } func (s *Session) shouldNegotiatePC() bool { + if s.onData != nil { + return true + } + return s.shouldRequestVideo() +} + +func (s *Session) shouldRequestVideo() bool { s.videoTrackMu.RLock() defer s.videoTrackMu.RUnlock() return len(s.videoTracks) > 0 || s.onVideoTrack != nil @@ -472,9 +479,11 @@ func (s *Session) negotiatePC(ctx context.Context, jSess *j.Session) error { } } - // Tell JVB to forward video streams to this endpoint. - if err := jSess.RequestVideo(ctx, 720); err != nil { - logger.Debugf("jitsi: request video: %v", err) + if s.shouldRequestVideo() { + // Tell JVB to forward video streams to this endpoint. + if err := jSess.RequestVideo(ctx, 720); err != nil { + logger.Debugf("jitsi: request video: %v", err) + } } s.pcMu.Lock() diff --git a/internal/engine/jitsi/jitsi_test.go b/internal/engine/jitsi/jitsi_test.go index b473df6..b25e61f 100644 --- a/internal/engine/jitsi/jitsi_test.go +++ b/internal/engine/jitsi/jitsi_test.go @@ -93,6 +93,57 @@ func TestNewSucceeds(t *testing.T) { } } +func TestByteStreamNegotiatesPeerConnectionWithoutRequestingVideo(t *testing.T) { + sess, err := New(context.Background(), engine.Config{ + URL: testHost, + Extra: map[string]string{credentialKeyRoom: testRoom}, + OnData: func([]byte) {}, + }) + if err != nil { + t.Fatalf("New: %v", err) + } + defer func() { _ = sess.Close() }() + + js, ok := sess.(*Session) + if !ok { + t.Fatal("sess is not *Session") + } + if !js.shouldNegotiatePC() { + t.Fatal("shouldNegotiatePC() = false for bytestream session") + } + if js.shouldRequestVideo() { + t.Fatal("shouldRequestVideo() = true for bytestream-only session") + } +} + +func TestVideoSessionNegotiatesPeerConnectionAndRequestsVideo(t *testing.T) { + sess, err := New(context.Background(), engine.Config{ + URL: testHost, + Extra: map[string]string{credentialKeyRoom: testRoom}, + }) + if err != nil { + t.Fatalf("New: %v", err) + } + defer func() { _ = sess.Close() }() + + js, ok := sess.(*Session) + if !ok { + t.Fatal("sess is not *Session") + } + if js.shouldNegotiatePC() { + t.Fatal("shouldNegotiatePC() = true before bytestream/video is configured") + } + if err := js.AddVideoTrack(nil); err != nil { + t.Fatalf("AddVideoTrack(nil): %v", err) + } + if !js.shouldNegotiatePC() { + t.Fatal("shouldNegotiatePC() = false for video session") + } + if !js.shouldRequestVideo() { + t.Fatal("shouldRequestVideo() = false for video session") + } +} + func TestSendBeforeConnect(t *testing.T) { sess, err := New(context.Background(), engine.Config{ URL: testHost,