Skip to content

Commit d812bb0

Browse files
committed
feat: check camera, microphone and network before a call
WebRtc.checkCall measures whether a video track delivers frames that are not all black, whether an audio track delivers audio that is neither silent nor clipped, and whether a connection to a real test peer comes up, and names the problems it finds.
1 parent 3a39e66 commit d812bb0

9 files changed

Lines changed: 1803 additions & 0 deletions

File tree

‎docs/guide/client-api-design.md‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ public final class WebRtc implements AutoCloseable {
219219
public List<Peer> peers(); // snapshot of the open peers
220220
public AudioMode audioMode();
221221
public LoadStats loadStats(); // how much work waits and runs, cheap enough to read every second
222+
public CompletableFuture<CallCheckResult> checkCall(CallCheck check); // camera, microphone and network before a call
222223

223224
public CompletableFuture<Void> closeAsync();
224225
@Override
@@ -858,6 +859,75 @@ public enum QualityCondition {
858859
- `diagnosticReport()` can go into a support ticket as it is. It leaves out raw stats, ids,
859860
addresses, ports, URLs, credentials, SDP, certificates and codec parameters.
860861

862+
### `CallCheck`, before a call
863+
864+
A self-test for "am I ready for the call?": the camera delivers frames, the microphone audio that is
865+
neither silent nor clipped, and a connection to a test peer comes up.
866+
867+
```java
868+
public final class CallCheck {
869+
870+
public static Builder builder();
871+
872+
public static final class Builder {
873+
public Builder video(LocalVideo track); // frames arrive, not all black
874+
public Builder audio(LocalAudio track); // audio arrives, not silent, not clipped
875+
public Builder network(PeerOptions options); // a connection to a test peer comes up
876+
public Builder duration(Duration duration); // how long to measure, default 3 s
877+
public Builder timeout(Duration timeout); // how long to wait for the first sample, default 10 s
878+
public CallCheck build();
879+
}
880+
}
881+
882+
public final class CallCheckResult {
883+
884+
public enum Problem { NO_VIDEO, DARK_VIDEO, NO_AUDIO, SILENT_AUDIO, CLIPPED_AUDIO, NO_CONNECTION;
885+
public String reason();
886+
}
887+
888+
public boolean isReady(); // no problem found, not a guarantee
889+
public Set<Problem> problems();
890+
public Video video(); // null for a part not checked
891+
public Audio audio();
892+
public Network network();
893+
894+
public static final class Video {
895+
public int frames();
896+
public double framesPerSecond();
897+
public int width();
898+
public int height();
899+
public double brightness(); // average luma of the brightest frame, 0 to 1
900+
}
901+
902+
public static final class Audio {
903+
public Duration received();
904+
public double peakLevel(); // linear, 0 to 1
905+
public double rmsLevel();
906+
public double clipping(); // share of samples at full scale
907+
}
908+
909+
public static final class Network {
910+
public boolean isConnected();
911+
public Duration connectTime();
912+
public Duration roundTripTime();
913+
public String candidateType();
914+
public boolean isRelayed();
915+
public Throwable failure(); // TimeoutException, or why the peer failed
916+
}
917+
}
918+
```
919+
920+
- The parts run at the same time, so a check takes as long as its slowest part. Each waits on the
921+
runtime's timer for its first sample, then measures for the duration, which tests fire by hand
922+
with a `ManualTimer`.
923+
- Problems with reasons, as for `PeerDiagnostics`, rather than a score.
924+
- Reachability is told by a connection to a real test peer, never derived from the configuration.
925+
`relayOnly(true)` makes a connection prove the TURN servers.
926+
- WebRTC records a microphone only while a send stream runs, and the client API does not start the
927+
recording itself. So the check sends a microphone to a pair of peers of its own, whose sender drops
928+
every encoded frame, so that the user does not hear themselves. The audio reaches the check
929+
through a sink on the track, after processing.
930+
861931
### `Whip` and `Whep`
862932

863933
Sessions with a media server, over a peer with `Negotiation.MANUAL`. Their requests follow RFC 9725

‎docs/guide/client-api.md‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,69 @@ ICE credentials, SDP, certificates and codec parameters. Missing measurements
10191019
become JSON null. The raw `report()` and its string representation are not
10201020
sanitized diagnostic exports.
10211021

1022+
## Checking Before a Call
1023+
1024+
Diagnostics watch a call that is under way. `checkCall` answers the question before it: am I ready
1025+
for the call? It checks that the camera delivers frames, that the microphone delivers audio that is
1026+
neither silent nor clipped, and that a connection to a test peer comes up, all at the same time:
1027+
1028+
```java
1029+
LocalCamera camera = rtc.media().camera();
1030+
LocalAudio microphone = rtc.media().microphone();
1031+
1032+
CallCheck check = CallCheck.builder()
1033+
.video(camera)
1034+
.audio(microphone) // ask the user to say something meanwhile
1035+
.network(PeerOptions.builder()
1036+
.iceServer("turn:turn.example.com", "user", "secret")
1037+
.relayOnly(true) // only a relayed connection counts
1038+
.signaling(testPeerSignaling)
1039+
.build())
1040+
.build();
1041+
1042+
rtc.checkCall(check).thenAccept(result -> {
1043+
if (result.isReady()) {
1044+
System.out.println("Ready: " + result);
1045+
}
1046+
result.problems().forEach(problem -> System.out.println(problem.reason()));
1047+
});
1048+
```
1049+
1050+
Every part is optional, but a check needs at least one. Each waits up to the check's `timeout`
1051+
(10 seconds by default) for the first frame, the first audio or the connection, then measures for
1052+
its `duration` (3 seconds by default). The future completes on an event thread of the runtime's
1053+
local media, so the rule against `join()` holds there. The tracks stay the application's: the check
1054+
neither closes nor mutes them, and the same tracks can go into the call once it is done.
1055+
1056+
| Problem | Found when |
1057+
|---|---|
1058+
| `NO_VIDEO` | The video track delivered no frame within the timeout. |
1059+
| `DARK_VIDEO` | No frame had an average luma above 8% of white: a covered lens, a closed privacy shutter or no light. The brightest frame counts, since a camera's first frames are often dark while its exposure settles. A muted track sends black frames, so it is dark too. |
1060+
| `NO_AUDIO` | The audio track delivered no audio within the timeout. |
1061+
| `SILENT_AUDIO` | The audio never rose above -50 dBFS: a muted microphone, or nobody spoke. |
1062+
| `CLIPPED_AUDIO` | More than 0.1% of the samples were at full scale (99% of it or above): the input is too loud and distorts. |
1063+
| `NO_CONNECTION` | No connection to the test peer came up within the timeout. `network().failure()` tells why: a `TimeoutException`, or the failure of the peer. |
1064+
1065+
Besides the problems, the result has the measurements behind them: frames, frame rate, resolution and
1066+
brightness of the video; how much audio arrived, with its peak and RMS level and the share of
1067+
clipped samples; and for the network the time it took to connect, the round trip time, the local
1068+
candidate type and whether the connection is relayed. `isReady()` means that no part found a
1069+
problem, not that the call is sure to work.
1070+
1071+
Audio is measured as the track sends it, after audio processing. WebRTC records a microphone only
1072+
while it sends it, so for a microphone the check sends the track to a peer of its own, in the same
1073+
runtime, which drops every encoded frame before it goes out: nothing is played back, and the user
1074+
does not hear themselves. That peer, and the one of the network check, show among `rtc.peers()`
1075+
while the check runs and are closed before the result goes out.
1076+
1077+
Whether the network lets a call through cannot be told from the local configuration, only from a
1078+
connection that is made. The network part therefore connects to a real test peer: one the
1079+
application runs for this, such as an echo bot behind its own signaling, or any other peer that
1080+
answers the offer. The check creates a peer with the given options, opens a data channel so that it
1081+
offers, reads the connection's statistics once it is up and closes the peer. With `relayOnly(true)`
1082+
the connection can only go through the TURN servers of the options, so a connection proves that they
1083+
work; without it, a direct connection may come up instead.
1084+
10221085
## Closing
10231086

10241087
You close what you created, and closing an owner closes what it owns:
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/*
2+
* Copyright 2026 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dev.onvoid.webrtc.client;
18+
19+
import static java.util.Objects.requireNonNull;
20+
21+
import java.time.Duration;
22+
23+
import dev.onvoid.webrtc.internal.Timeouts;
24+
25+
/**
26+
* What {@link WebRtc#checkCall(CallCheck)} checks before a call: whether a
27+
* video track delivers frames, whether an audio track delivers audio that is
28+
* neither silent nor clipped, and whether a connection to a test peer comes
29+
* up. Any of the three may be left out, but not all of them.
30+
* <pre>{@code
31+
* LocalCamera camera = rtc.media().camera();
32+
* LocalAudio microphone = rtc.media().microphone();
33+
*
34+
* CallCheck check = CallCheck.builder()
35+
* .video(camera)
36+
* .audio(microphone)
37+
* .network(PeerOptions.builder()
38+
* .iceServer("turn:turn.example.com", "user", "secret")
39+
* .relayOnly(true)
40+
* .signaling(testPeerSignaling)
41+
* .build())
42+
* .build();
43+
*
44+
* rtc.checkCall(check).thenAccept(result -> {
45+
* result.problems().forEach(problem -> System.out.println(problem.reason()));
46+
* });
47+
* }</pre>
48+
* The tracks stay the application's: the check neither closes nor mutes them,
49+
* so the same tracks can go into the call afterwards.
50+
*/
51+
public final class CallCheck {
52+
53+
final LocalVideo video;
54+
55+
final LocalAudio audio;
56+
57+
final PeerOptions network;
58+
59+
final Duration duration;
60+
61+
final Duration timeout;
62+
63+
64+
private CallCheck(Builder builder) {
65+
this.video = builder.video;
66+
this.audio = builder.audio;
67+
this.network = builder.network;
68+
this.duration = builder.duration;
69+
this.timeout = builder.timeout;
70+
}
71+
72+
/**
73+
* @return a builder of a check.
74+
*/
75+
public static Builder builder() {
76+
return new Builder();
77+
}
78+
79+
80+
81+
/** Builds a {@link CallCheck}. */
82+
public static final class Builder {
83+
84+
private LocalVideo video;
85+
86+
private LocalAudio audio;
87+
88+
private PeerOptions network;
89+
90+
private Duration duration = Duration.ofSeconds(3);
91+
92+
private Duration timeout = Duration.ofSeconds(10);
93+
94+
95+
private Builder() {
96+
}
97+
98+
/**
99+
* Checks that the track delivers frames that are not all black, as
100+
* from a covered lens or a closed privacy shutter. The frames are
101+
* those a local preview of the track gets.
102+
*
103+
* @param track a camera, a screen or any other local video track.
104+
*
105+
* @return this builder.
106+
*/
107+
public Builder video(LocalVideo track) {
108+
this.video = requireNonNull(track, "track");
109+
return this;
110+
}
111+
112+
/**
113+
* Checks that the track delivers audio that is neither silent nor
114+
* clipped, so the user should speak during the check. The audio is
115+
* measured as the track sends it, after audio processing.
116+
* <p>
117+
* WebRTC records a microphone only while it sends it, so for a
118+
* {@link LocalMedia#microphone() microphone} the check sends the track
119+
* to a peer of its own, in this runtime, which it closes again. That
120+
* peer drops every encoded frame before it goes out, so nothing is
121+
* played back and the user does not hear themselves.
122+
*
123+
* @param track a microphone or any other local audio track.
124+
*
125+
* @return this builder.
126+
*/
127+
public Builder audio(LocalAudio track) {
128+
this.audio = requireNonNull(track, "track");
129+
return this;
130+
}
131+
132+
/**
133+
* Checks that a connection to a test peer comes up: a peer the
134+
* application runs for this, or any other that answers over the
135+
* signaling of the options, such as an echo bot. Only a connection
136+
* that is made tells that the network lets one through; the local
137+
* configuration alone cannot. With {@link
138+
* PeerOptions.Builder#relayOnly(boolean) relayOnly} set, a connection
139+
* proves that the TURN servers of the options work.
140+
* <p>
141+
* The check creates a peer with the options, opens a data channel, so
142+
* that the peer offers, reads the statistics of the connection and
143+
* closes the peer again.
144+
*
145+
* @param options the ICE servers and the signaling to the test peer.
146+
*
147+
* @return this builder.
148+
*/
149+
public Builder network(PeerOptions options) {
150+
this.network = requireNonNull(options, "options");
151+
return this;
152+
}
153+
154+
/**
155+
* @param duration how long frames and audio are measured, from the
156+
* first frame or the first audio on; 3 seconds by
157+
* default.
158+
*
159+
* @return this builder.
160+
*/
161+
public Builder duration(Duration duration) {
162+
this.duration = Timeouts.requirePositive(duration, "duration");
163+
return this;
164+
}
165+
166+
/**
167+
* @param timeout how long the check waits for the first frame, the
168+
* first audio and the connection before it reports
169+
* that there is none; 10 seconds by default.
170+
*
171+
* @return this builder.
172+
*/
173+
public Builder timeout(Duration timeout) {
174+
this.timeout = Timeouts.requirePositive(timeout, "timeout");
175+
return this;
176+
}
177+
178+
/**
179+
* @return the check.
180+
*
181+
* @throws IllegalStateException if nothing was given to check.
182+
*/
183+
public CallCheck build() {
184+
if (video == null && audio == null && network == null) {
185+
throw new IllegalStateException("A call check needs video, audio or a network to check");
186+
}
187+
188+
return new CallCheck(this);
189+
}
190+
}
191+
}

0 commit comments

Comments
 (0)