Skip to content
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

Add guide on offering to receive media tracks #165

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions guides/advanced/mastering_transceivers.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,69 @@ receive do {:ex_webrtc, ^pc1, {:track, _track}} = msg -> IO.inspect(msg) end

<!-- tabs-close -->

## Offer to receive data

Offering to receive media tracks is a bit tricky as we can't force the other side to send something.
Therefore, when we send an offer with the mline's direction set to `recvonly`, the other side will,
by default, set such a track to inactive.
To make things work, we have to manually set the direction to `sendonly`.

<!-- tabs-open -->

### JavaScript

[![JS FIDDLE](https://img.shields.io/badge/-JS%20FIDDLE-blueviolet)](https://jsfiddle.net/mickel8/bhv8ds03/)

```js
pc1 = new RTCPeerConnection();
pc2 = new RTCPeerConnection();

tr = pc1.addTransceiver("audio", { direction: "recvonly" });

offer = await pc1.createOffer();

await pc1.setLocalDescription(offer);
await pc2.setRemoteDescription(offer);

// change direction from default "recvonly" to "sendonly"
// in other case, when negotiation finishes,
// currentDirection of this transceiver will be inactive
pc2.getTransceivers()[0].direction = "sendonly";

answer = await pc2.createAnswer();
await pc2.setLocalDescription(answer);
await pc1.setRemoteDescription(answer);

console.log(pc2.getTransceivers()[0].direction);
console.log(pc2.getTransceivers()[0].currentDirection);
```

### Elixir WebRTC

```elixir
{:ok, pc1} = PeerConnection.start_link()
{:ok, pc2} = PeerConnection.start_link()

{:ok, _tr} = PeerConnection.add_transceiver(pc1, :audio, direction: :recvonly)

{:ok, offer} = PeerConnection.create_offer(pc1)
:ok = PeerConnection.set_local_description(pc1, offer)
:ok = PeerConnection.set_remote_description(pc2, offer)

[pc2_tr] = PeerConnection.get_transceivers(pc2)
:ok = PeerConnection.set_transceiver_direction(pc2, pc2_tr.id, :sendonly)

{:ok, answer} = PeerConnection.create_answer(pc2)
:ok = PeerConnection.set_local_description(pc2, answer)
:ok = PeerConnection.set_remote_description(pc1, answer)

[pc2_tr] = PeerConnection.get_transceivers(pc2)
IO.inspect(pc2_tr.direction)
IO.inspect(pc2_tr.current_direction)
```

<!-- tabs-close -->

## Rejecting Incoming Track

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