Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions docs/guide/client-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,11 @@ Whep.Session viewer = Whep.from(URI.create("https://example.com/live/whep"))
viewer.peer().onTrack(track -> System.out.println("Receiving " + track.kind()));
```

The offer goes to the endpoint with the local candidates, as WHIP and WHEP servers expect, and the future completes once the peer is connected. If the endpoint refuses the offer, or the peer does not connect within the timeout (15 seconds by default), the future fails, and the session is deleted on the server again. An endpoint that is busy, with status 503, or a WHEP endpoint whose stream has yet to start, with status 409, gets the offer again after the wait its `Retry-After` names, at least a second, as long as that fits within the timeout; without `Retry-After` the session fails at once. A WHEP endpoint may also reject the offer and counter it with one of its own, with status 406: `Whep` then takes its offer back, answers the endpoint's, receiving only, and sends the answer by `PATCH` to the session the endpoint named, before the counter-offer's `valid-until`, 30 seconds by default. The answer rejects the audio or video of the counter-offer that `receive(...)` leaves out, and the session fails if the counter-offer holds none of what it asks for. Data channels of a counter-offer are accepted. Cancelling the future, say once `get` gave up waiting, ends the session the same way. `onTrack` hands over tracks that already arrived, so a handler added once the future completed misses none.
The offer goes to the endpoint at once, before any candidate is gathered, and the future completes once the peer is connected. If the endpoint refuses the offer, or the peer does not connect within the timeout (15 seconds by default), the future fails, and the session is deleted on the server again. An endpoint that is busy, with status 503, or a WHEP endpoint whose stream has yet to start, with status 409, gets the offer again after the wait its `Retry-After` names, at least a second, as long as that fits within the timeout; without `Retry-After` the session fails at once. A WHEP endpoint may also reject the offer and counter it with one of its own, with status 406: `Whep` then takes its offer back, answers the endpoint's, receiving only, and sends the answer by `PATCH` to the session the endpoint named, before the counter-offer's `valid-until`, 30 seconds by default. The answer rejects the audio or video of the counter-offer that `receive(...)` leaves out, and the session fails if the counter-offer holds none of what it asks for. Data channels of a counter-offer are accepted. Cancelling the future, say once `get` gave up waiting, ends the session the same way. `onTrack` hands over tracks that already arrived, so a handler added once the future completed misses none.

Only once the endpoint answered does the peer gather its candidates, with the STUN and TURN servers the endpoint names in `Link` headers with `rel="ice-server"`, unless `iceServer(...)` names servers of its own, which take precedence. The candidates follow the offer by `PATCH` to the session, as `application/trickle-ice-sdpfrag`, a few at a time and with the session's `ETag` as `If-Match`, as RFC 9725 describes. An endpoint that takes no such `PATCH`, with status 405, 415, 422 or 501, gets no more; it finds the peer all the same, from the checks the peer sends to its candidates.

When the connection fails, or stays disconnected for two seconds, the session restarts ICE: the peer gathers new candidates, and only the new ICE username fragment and password go to the session, by `PATCH` with `If-Match: *`, while the media go on as negotiated. The session's answer, with status 200, brings its own ICE information and a new `ETag`. Three restarts in a row that do not bring the connection back, or an endpoint that takes no restarts, end the attempts, which the error handler hears of. `session.restartIce()` restarts ICE at once, say after the network changed; a restart that fails leaves the ICE session before as it was. A session that answered a WHEP counter-offer takes no restarts.

The timeout bounds the requests as a whole, not each read, so a server that sends its answer a byte at a time cannot stretch it. A request still out when the session times out or is cancelled is cut off at once; one still waiting for the head of its answer only from Java 16 on, and before that at its timeout. Once the server has named the session, in the head of its answer, it is deleted all the same; a session the server created for an answer that had not come yet is left for the server to time out.

Expand All @@ -613,7 +617,7 @@ Requests go through Java's `HttpClient`, with the default `SSLContext` and the H
| Builder method | What it sets |
|---|---|
| `bearerToken(String)` | The token sent as `Authorization: Bearer`; for many services, the stream key. |
| `iceServer(String...)` | A STUN or TURN server; endpoints usually name their own in the answer. |
| `iceServer(String...)` | A STUN or TURN server, which replaces those the endpoint names in its answer. |
| `track(LocalTrack)` | A track to publish, WHIP only. The tracks share one stream. |
| `receive(MediaKind...)` | What to receive, WHEP only; video and audio by default. |
| `timeout(Duration)` | How long the session may take to be connected, and its deletion to be answered. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
* <li>Ending the session, which deletes it on the server</li>
* </ul>
* <p>
* {@link Whep} sends an offer with all local candidates, applies the answer and waits until
* the peer is connected. The Client API guide shows the same steps by hand, through the
* {@link Whep} sends an offer, trickles the local candidates after it, applies the answer and
* waits until the peer is connected. The Client API guide shows the same steps by hand, through the
* peer's negotiator, for servers that speak a protocol of their own.
* <p>
* Run it against a WHEP endpoint, such as one of MediaMTX, for a number of seconds:
Expand Down
16 changes: 16 additions & 0 deletions webrtc/src/main/java/dev/onvoid/webrtc/client/HttpCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -108,6 +109,9 @@ final class HttpCall {
/** Statuses other than 2xx whose body is read as that of a 2xx one. */
private Set<Integer> answerStatuses = Collections.emptySet();

/** Header fields the request carries beyond those it sets itself. */
private final Map<String, String> headers = new LinkedHashMap<>();

/** The exchange under way, which an abort cancels. */
private CompletableFuture<?> exchange;

Expand Down Expand Up @@ -189,6 +193,16 @@ HttpCall retry(ScheduledExecutorService timer, Set<Integer> statuses) {
return this;
}

/**
* Adds a header field to the request, such as If-Match.
*
* @return this request.
*/
HttpCall header(String name, String value) {
headers.put(name, value);
return this;
}

/**
* Reads the body of a response with one of the given statuses as that of
* a 2xx one, up to the size {@link #send} is given, rather than only as
Expand Down Expand Up @@ -290,6 +304,8 @@ private void exchange(URI target, String contentType, byte[] content, int maxBod
if (bearerToken != null) {
builder.header("Authorization", "Bearer " + bearerToken);
}

headers.forEach(builder::header);
if (content != null) {
builder.header("Content-Type", contentType)
.header("Accept", contentType)
Expand Down
Loading
Loading