Skip to content

Commit 9468573

Browse files
authored
Add guide on offering to receive media tracks (#165)
1 parent 738771f commit 9468573

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

guides/advanced/mastering_transceivers.md

+63
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,69 @@ receive do {:ex_webrtc, ^pc1, {:track, _track}} = msg -> IO.inspect(msg) end
164164

165165
<!-- tabs-close -->
166166

167+
## Offer to receive data
168+
169+
Offering to receive media tracks is a bit tricky as we can't force the other side to send something.
170+
Therefore, when we send an offer with the mline's direction set to `recvonly`, the other side will,
171+
by default, set such a track to inactive.
172+
To make things work, we have to manually set the direction to `sendonly`.
173+
174+
<!-- tabs-open -->
175+
176+
### JavaScript
177+
178+
[![JS FIDDLE](https://img.shields.io/badge/-JS%20FIDDLE-blueviolet)](https://jsfiddle.net/mickel8/bhv8ds03/)
179+
180+
```js
181+
pc1 = new RTCPeerConnection();
182+
pc2 = new RTCPeerConnection();
183+
184+
tr = pc1.addTransceiver("audio", { direction: "recvonly" });
185+
186+
offer = await pc1.createOffer();
187+
188+
await pc1.setLocalDescription(offer);
189+
await pc2.setRemoteDescription(offer);
190+
191+
// change direction from default "recvonly" to "sendonly"
192+
// in other case, when negotiation finishes,
193+
// currentDirection of this transceiver will be inactive
194+
pc2.getTransceivers()[0].direction = "sendonly";
195+
196+
answer = await pc2.createAnswer();
197+
await pc2.setLocalDescription(answer);
198+
await pc1.setRemoteDescription(answer);
199+
200+
console.log(pc2.getTransceivers()[0].direction);
201+
console.log(pc2.getTransceivers()[0].currentDirection);
202+
```
203+
204+
### Elixir WebRTC
205+
206+
```elixir
207+
{:ok, pc1} = PeerConnection.start_link()
208+
{:ok, pc2} = PeerConnection.start_link()
209+
210+
{:ok, _tr} = PeerConnection.add_transceiver(pc1, :audio, direction: :recvonly)
211+
212+
{:ok, offer} = PeerConnection.create_offer(pc1)
213+
:ok = PeerConnection.set_local_description(pc1, offer)
214+
:ok = PeerConnection.set_remote_description(pc2, offer)
215+
216+
[pc2_tr] = PeerConnection.get_transceivers(pc2)
217+
:ok = PeerConnection.set_transceiver_direction(pc2, pc2_tr.id, :sendonly)
218+
219+
{:ok, answer} = PeerConnection.create_answer(pc2)
220+
:ok = PeerConnection.set_local_description(pc2, answer)
221+
:ok = PeerConnection.set_remote_description(pc1, answer)
222+
223+
[pc2_tr] = PeerConnection.get_transceivers(pc2)
224+
IO.inspect(pc2_tr.direction)
225+
IO.inspect(pc2_tr.current_direction)
226+
```
227+
228+
<!-- tabs-close -->
229+
167230
## Rejecting Incoming Track
168231

169232
To reject incoming track, we simply change the transceiver's direction to "inactive".

0 commit comments

Comments
 (0)