Skip to content

Commit

Permalink
webrtc: switch to recvonly transceivers (#4059)
Browse files Browse the repository at this point in the history
This fixes compatibility with devices that support decoding AV1 but
doesn't support encoding it.

This was previously impossible to achieve due to a bug that prevented
video from being displayed when recvonly transceivers were in use and
audio was not present.
  • Loading branch information
aler9 committed Jan 4, 2025
1 parent 21b5031 commit 3c9e191
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
36 changes: 20 additions & 16 deletions internal/protocols/webrtc/peer_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,31 @@ func (co *PeerConnection) Start() error {
if co.Publish {
videoSetupped := false
audioSetupped := false
for _, tr := range co.OutgoingTracks {
if tr.isVideo() {
videoSetupped = true
} else {
audioSetupped = true
}
}

// When audio is not used, a track has to be present anyway,
// otherwise video is not displayed on Firefox and Chrome.
if !audioSetupped {
co.OutgoingTracks = append(co.OutgoingTracks, &OutgoingTrack{
Caps: webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypePCMU,
ClockRate: 8000,
},
})
}

for _, tr := range co.OutgoingTracks {
var codecType webrtc.RTPCodecType
if tr.isVideo() {
codecType = webrtc.RTPCodecTypeVideo
videoSetupped = true
} else {
codecType = webrtc.RTPCodecTypeAudio
audioSetupped = true
}

err := mediaEngine.RegisterCodec(webrtc.RTPCodecParameters{
Expand All @@ -151,8 +167,8 @@ func (co *PeerConnection) Start() error {
}
}

// always register at least a video and a audio codec
// otherwise handshake will fail or audio will be muted on some clients (like Firefox)
// When video is not used, a track must not be added but a codec has to present.
// Otherwise audio is muted on Firefox and Chrome.
if !videoSetupped {
err := mediaEngine.RegisterCodec(webrtc.RTPCodecParameters{
RTPCodecCapability: webrtc.RTPCodecCapability{
Expand All @@ -165,18 +181,6 @@ func (co *PeerConnection) Start() error {
return err
}
}
if !audioSetupped {
err := mediaEngine.RegisterCodec(webrtc.RTPCodecParameters{
RTPCodecCapability: webrtc.RTPCodecCapability{
MimeType: webrtc.MimeTypePCMU,
ClockRate: 8000,
},
PayloadType: 0,
}, webrtc.RTPCodecTypeAudio)
if err != nil {
return err
}
}
} else {
for _, codec := range incomingVideoCodecs {
err := mediaEngine.RegisterCodec(codec, webrtc.RTPCodecTypeVideo)
Expand Down
2 changes: 1 addition & 1 deletion internal/servers/webrtc/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@
sdpSemantics: 'unified-plan',
});

const direction = 'sendrecv';
const direction = 'recvonly';
this.pc.addTransceiver('video', { direction });
this.pc.addTransceiver('audio', { direction });

Expand Down

0 comments on commit 3c9e191

Please sign in to comment.