-
Notifications
You must be signed in to change notification settings - Fork 1.8k
How to Properly Set Media Direction (sendonly) in WebRTC Answer? #3395
Description
Your environment.
- Version: webrtc pion version
github.com/pion/webrtc/v4 v4.2.9 - Browser: Chrome Version 145.0.7632.162 (Official Build) (64-bit)
- Other Information: N/A
What did you do?
The system I’m working on involves communication between browsers and non-WebRTC devices. This requires translating SDP between WebRTC and another signaling/media system.
My goal here is to generate a WebRTC answer with video direction set to sendonly.
The steps I followed to generate a WebRTC answer:
1- Call AddTransceiverFromTrack method to be able to configure the video direction as sendonly.
webrtcLocalTrack, err := webrtc.NewTrackLocalStaticRTP(.....)
transceiverInit := webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionSendonly}
transceiver, err := c.peerConnection.AddTransceiverFromTrack(webrtcLocalTrack, transceiverInit)
2- SetRemoteDescription(offerType, webrtcOfferGeneratedFromCh) // the offer has video direction a=sendrecv.
3- webrtcAnswer := CreateAnswer(nil)
4- SetLocalDescription(webrtcAnswer)
What did you expect?
In step 3, I expected the webrtcAnswer to have the video direction a=sendonly.
What happened?
The generated webrtcAnswer in step 3 has the video direction a=sendrecv.
While debugging, I found that the transceiver direction is changed to sendrecv when calling setRemoteDescription method:

I am not fully aware of the side effects, but if the original transceiver direction that I set in step 1 is preserved, CreateAnswer generates an SDP with video direction sendonly, which is the expected behaviour.
Alternatively, I followed the flow described in this diagram, but I wasn’t able to achieve the desired result.
I think setting the media direction is not straightforward. Is there a general approach/steps/recipe for handling it?
Currently, my use case is sendonly, but later I’ll need to support all direction types. For example, when I call AddTransceiverFromTrack with recvonly, I get this error: "AddTransceiverFromTrack currently only supports sendonly and sendrecv"
Maybe making the setDirection method public could potentially simplify things?
Thank you in advance.