|
| 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; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.*; |
| 20 | + |
| 21 | +import dev.onvoid.webrtc.media.MediaStream; |
| 22 | +import dev.onvoid.webrtc.media.MediaStreamTrack; |
| 23 | +import dev.onvoid.webrtc.media.audio.AudioOptions; |
| 24 | +import dev.onvoid.webrtc.media.audio.AudioTrack; |
| 25 | +import dev.onvoid.webrtc.media.audio.AudioTrackSource; |
| 26 | +import dev.onvoid.webrtc.media.video.CustomVideoSource; |
| 27 | +import dev.onvoid.webrtc.media.video.VideoTrack; |
| 28 | + |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +import org.junit.jupiter.api.AfterEach; |
| 34 | +import org.junit.jupiter.api.BeforeEach; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | +import org.junit.jupiter.api.parallel.Execution; |
| 37 | +import org.junit.jupiter.api.parallel.ExecutionMode; |
| 38 | + |
| 39 | +/** |
| 40 | + * MediaStream.getAudioTracks() and getVideoTracks() return the arrays native |
| 41 | + * code made: an array whose local reference was deleted on the way out reached |
| 42 | + * Java as null, whether the stream had tracks of that kind or none. |
| 43 | + */ |
| 44 | +@Execution(ExecutionMode.SAME_THREAD) |
| 45 | +class MediaStreamTracksTests extends TestBase { |
| 46 | + |
| 47 | + private final List<String> audioTrackIds = Collections.synchronizedList(new ArrayList<>()); |
| 48 | + |
| 49 | + private final List<String> videoTrackIds = Collections.synchronizedList(new ArrayList<>()); |
| 50 | + |
| 51 | + private final List<Throwable> errors = Collections.synchronizedList(new ArrayList<>()); |
| 52 | + |
| 53 | + private RTCPeerConnection caller; |
| 54 | + |
| 55 | + private RTCPeerConnection callee; |
| 56 | + |
| 57 | + private RTCRtpSender sender; |
| 58 | + |
| 59 | + private MediaStreamTrack track; |
| 60 | + |
| 61 | + private AutoCloseable source; |
| 62 | + |
| 63 | + |
| 64 | + @BeforeEach |
| 65 | + void init() { |
| 66 | + // TestBase keeps one instance for all tests of the class. |
| 67 | + sender = null; |
| 68 | + track = null; |
| 69 | + source = null; |
| 70 | + audioTrackIds.clear(); |
| 71 | + videoTrackIds.clear(); |
| 72 | + errors.clear(); |
| 73 | + |
| 74 | + RTCConfiguration config = new RTCConfiguration(); |
| 75 | + |
| 76 | + caller = factory.createPeerConnection(config, candidate -> { }); |
| 77 | + callee = factory.createPeerConnection(config, new PeerConnectionObserver() { |
| 78 | + |
| 79 | + @Override |
| 80 | + public void onIceCandidate(RTCIceCandidate candidate) { |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void onAddTrack(RTCRtpReceiver receiver, MediaStream[] mediaStreams) { |
| 85 | + // The remote stream is alive here: the peer connection holds it. |
| 86 | + try { |
| 87 | + for (MediaStream stream : mediaStreams) { |
| 88 | + collect(stream.getAudioTracks(), audioTrackIds); |
| 89 | + collect(stream.getVideoTracks(), videoTrackIds); |
| 90 | + |
| 91 | + stream.dispose(); |
| 92 | + } |
| 93 | + |
| 94 | + receiver.dispose(); |
| 95 | + } |
| 96 | + catch (Throwable e) { |
| 97 | + errors.add(e); |
| 98 | + } |
| 99 | + } |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + @AfterEach |
| 104 | + void dispose() throws Exception { |
| 105 | + if (sender != null) { |
| 106 | + sender.dispose(); |
| 107 | + } |
| 108 | + |
| 109 | + caller.close(); |
| 110 | + callee.close(); |
| 111 | + |
| 112 | + if (track != null) { |
| 113 | + track.dispose(); |
| 114 | + } |
| 115 | + if (source != null) { |
| 116 | + source.close(); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void remoteAudioStreamTracks() throws Exception { |
| 122 | + AudioTrackSource audioSource = factory.createAudioSource(new AudioOptions()); |
| 123 | + source = audioSource::dispose; |
| 124 | + |
| 125 | + AudioTrack audioTrack = factory.createAudioTrack("audioTrack", audioSource); |
| 126 | + track = audioTrack; |
| 127 | + |
| 128 | + sender = caller.addTrack(audioTrack, Collections.singletonList("stream")); |
| 129 | + |
| 130 | + negotiate(); |
| 131 | + |
| 132 | + assertTrue(errors.isEmpty(), () -> "Errors: " + errors); |
| 133 | + assertEquals(Collections.singletonList("audioTrack"), audioTrackIds); |
| 134 | + assertEquals(Collections.emptyList(), videoTrackIds); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void remoteVideoStreamTracks() throws Exception { |
| 139 | + CustomVideoSource videoSource = new CustomVideoSource(); |
| 140 | + source = videoSource::dispose; |
| 141 | + |
| 142 | + VideoTrack videoTrack = factory.createVideoTrack("videoTrack", videoSource); |
| 143 | + track = videoTrack; |
| 144 | + |
| 145 | + sender = caller.addTrack(videoTrack, Collections.singletonList("stream")); |
| 146 | + |
| 147 | + negotiate(); |
| 148 | + |
| 149 | + assertTrue(errors.isEmpty(), () -> "Errors: " + errors); |
| 150 | + assertEquals(Collections.emptyList(), audioTrackIds); |
| 151 | + assertEquals(Collections.singletonList("videoTrack"), videoTrackIds); |
| 152 | + } |
| 153 | + |
| 154 | + private static void collect(MediaStreamTrack[] tracks, List<String> ids) { |
| 155 | + // An empty array, not null, for a stream without tracks of the kind. |
| 156 | + assertNotNull(tracks); |
| 157 | + |
| 158 | + for (MediaStreamTrack track : tracks) { |
| 159 | + ids.add(track.getId()); |
| 160 | + |
| 161 | + // The tracks are views of the stream's tracks. |
| 162 | + track.dispose(); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + private void negotiate() throws Exception { |
| 167 | + RTCSessionDescription offer = createDescription(caller, true); |
| 168 | + setRemoteDescription(callee, offer); |
| 169 | + |
| 170 | + RTCSessionDescription answer = createDescription(callee, false); |
| 171 | + setRemoteDescription(caller, answer); |
| 172 | + } |
| 173 | + |
| 174 | + private static RTCSessionDescription createDescription(RTCPeerConnection connection, boolean offer) throws Exception { |
| 175 | + TestCreateDescObserver createObserver = new TestCreateDescObserver(); |
| 176 | + TestSetDescObserver setObserver = new TestSetDescObserver(); |
| 177 | + |
| 178 | + if (offer) { |
| 179 | + connection.createOffer(new RTCOfferOptions(), createObserver); |
| 180 | + } |
| 181 | + else { |
| 182 | + connection.createAnswer(new RTCAnswerOptions(), createObserver); |
| 183 | + } |
| 184 | + |
| 185 | + RTCSessionDescription description = createObserver.get(); |
| 186 | + |
| 187 | + connection.setLocalDescription(description, setObserver); |
| 188 | + setObserver.get(); |
| 189 | + |
| 190 | + return description; |
| 191 | + } |
| 192 | + |
| 193 | + private static void setRemoteDescription(RTCPeerConnection connection, RTCSessionDescription description) throws Exception { |
| 194 | + TestSetDescObserver setObserver = new TestSetDescObserver(); |
| 195 | + |
| 196 | + connection.setRemoteDescription(description, setObserver); |
| 197 | + setObserver.get(); |
| 198 | + } |
| 199 | +} |
0 commit comments