|
| 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.assertTrue; |
| 20 | + |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | + |
| 24 | +import dev.onvoid.webrtc.media.MediaType; |
| 25 | +import dev.onvoid.webrtc.media.audio.AudioDeviceModule; |
| 26 | +import dev.onvoid.webrtc.media.audio.AudioLayer; |
| 27 | +import dev.onvoid.webrtc.media.audio.AudioOptions; |
| 28 | +import dev.onvoid.webrtc.media.audio.AudioTrack; |
| 29 | +import dev.onvoid.webrtc.media.audio.AudioTrackSource; |
| 30 | + |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.junit.jupiter.api.condition.EnabledIfSystemProperty; |
| 33 | +import org.junit.jupiter.api.condition.EnabledOnOs; |
| 34 | +import org.junit.jupiter.api.condition.OS; |
| 35 | +import org.junit.jupiter.api.parallel.Execution; |
| 36 | +import org.junit.jupiter.api.parallel.ExecutionMode; |
| 37 | + |
| 38 | +/** See PulseAudioIntegrationTest for the optional PulseAudio server setup. */ |
| 39 | +@EnabledOnOs(OS.LINUX) |
| 40 | +@EnabledIfSystemProperty(named = "webrtc.test.pulseaudio", matches = "true") |
| 41 | +@Execution(ExecutionMode.SAME_THREAD) |
| 42 | +class PulseAudioSignalingIntegrationTest extends TestBase { |
| 43 | + |
| 44 | + @Test |
| 45 | + void remoteAnswerRestartsPlayout() throws Exception { |
| 46 | + AudioDeviceModule adm = new AudioDeviceModule(AudioLayer.kLinuxPulseAudio); |
| 47 | + try { |
| 48 | + PeerConnectionFactory receiverFactory = new PeerConnectionFactory(adm); |
| 49 | + try { |
| 50 | + AudioTrackSource source = factory.createAudioSource(new AudioOptions()); |
| 51 | + try { |
| 52 | + AudioTrack track = factory.createAudioTrack("audio", source); |
| 53 | + try { |
| 54 | + for (int cycle = 1; cycle <= 5; cycle++) { |
| 55 | + negotiate(receiverFactory, track, cycle); |
| 56 | + } |
| 57 | + } |
| 58 | + finally { |
| 59 | + track.dispose(); |
| 60 | + } |
| 61 | + } |
| 62 | + finally { |
| 63 | + source.dispose(); |
| 64 | + } |
| 65 | + } |
| 66 | + finally { |
| 67 | + receiverFactory.dispose(); |
| 68 | + } |
| 69 | + } |
| 70 | + finally { |
| 71 | + adm.dispose(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private void negotiate(PeerConnectionFactory receiverFactory, AudioTrack track, |
| 76 | + int cycle) throws Exception { |
| 77 | + PeerConnectionObserver observer = new PeerConnectionObserver() { |
| 78 | + @Override |
| 79 | + public void onIceCandidate(RTCIceCandidate candidate) { } |
| 80 | + }; |
| 81 | + RTCPeerConnection receiver = receiverFactory.createPeerConnection( |
| 82 | + new RTCConfiguration(), observer); |
| 83 | + try { |
| 84 | + RTCPeerConnection sender = factory.createPeerConnection(new RTCConfiguration(), observer); |
| 85 | + try { |
| 86 | + RTCRtpTransceiverInit init = new RTCRtpTransceiverInit(); |
| 87 | + init.direction = RTCRtpTransceiverDirection.RECV_ONLY; |
| 88 | + receiver.addTransceiver(MediaType.AUDIO, init).dispose(); |
| 89 | + sender.addTrack(track, Collections.singletonList("stream")).dispose(); |
| 90 | + |
| 91 | + TestCreateDescObserver offerObserver = new TestCreateDescObserver(); |
| 92 | + receiver.createOffer(new RTCOfferOptions(), offerObserver); |
| 93 | + RTCSessionDescription offer = offerObserver.get(15, TimeUnit.SECONDS); |
| 94 | + TestSetDescObserver localOffer = new TestSetDescObserver(); |
| 95 | + receiver.setLocalDescription(offer, localOffer); |
| 96 | + localOffer.get(15, TimeUnit.SECONDS); |
| 97 | + TestSetDescObserver remoteOffer = new TestSetDescObserver(); |
| 98 | + sender.setRemoteDescription(offer, remoteOffer); |
| 99 | + remoteOffer.get(15, TimeUnit.SECONDS); |
| 100 | + |
| 101 | + TestCreateDescObserver answerObserver = new TestCreateDescObserver(); |
| 102 | + sender.createAnswer(new RTCAnswerOptions(), answerObserver); |
| 103 | + RTCSessionDescription answer = answerObserver.get(15, TimeUnit.SECONDS); |
| 104 | + TestSetDescObserver localAnswer = new TestSetDescObserver(); |
| 105 | + sender.setLocalDescription(answer, localAnswer); |
| 106 | + localAnswer.get(15, TimeUnit.SECONDS); |
| 107 | + |
| 108 | + // Applying the answer starts PulseAudio even without ICE or RTP. |
| 109 | + // A dead processing thread causes a synchronous ten-second wait. |
| 110 | + TestSetDescObserver remoteAnswer = new TestSetDescObserver(); |
| 111 | + long started = System.nanoTime(); |
| 112 | + receiver.setRemoteDescription(answer, remoteAnswer); |
| 113 | + remoteAnswer.get(15, TimeUnit.SECONDS); |
| 114 | + long elapsed = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started); |
| 115 | + System.out.printf("PulseAudio remote answer cycle %d: %d ms%n", cycle, elapsed); |
| 116 | + assertTrue(elapsed < 5000, |
| 117 | + "Audio startup timed out in cycle " + cycle + ": " + elapsed + " ms"); |
| 118 | + } |
| 119 | + finally { |
| 120 | + sender.close(); |
| 121 | + } |
| 122 | + } |
| 123 | + finally { |
| 124 | + receiver.close(); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments