|
| 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