Skip to content

Commit f175af9

Browse files
authored
fix: restart PulseAudio processing threads after reinitialization (#501)
1 parent f80d27b commit f175af9

5 files changed

Lines changed: 250 additions & 1 deletion

File tree

‎webrtc-jni/src/main/cpp/dependencies/webrtc/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ if (PATCHES)
450450

451451
if (RESULT EQUAL 0)
452452
message(STATUS "Patch applied: ${PATCH}")
453-
elseif(PATCH MATCHES "/patches/common/")
453+
elseif(PATCH MATCHES "/patches/common/" OR PATCH MATCHES "/pulseaudio_reinitialization[.]patch$")
454454
message(FATAL_ERROR "Required WebRTC patch could not be applied: ${PATCH}")
455455
else()
456456
message(STATUS "Warning: Patch ${PATCH} not applied ....")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.patch text eol=lf
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--- a/modules/audio_device/linux/audio_device_pulse_linux.cc
2+
+++ b/modules/audio_device/linux/audio_device_pulse_linux.cc
3+
@@ -178,3 +178,11 @@ AudioDeviceGeneric::InitStatus AudioDeviceLinuxPulse::Init() {
4+
+ // Terminate() stops both processing threads with this flag. The ADM is
5+
+ // reused when a factory creates another peer after closing its last one.
6+
+ // Reset it before spawning either thread so audio can start again.
7+
+ {
8+
+ MutexLock lock(&mutex_);
9+
+ quit_ = false;
10+
+ }
11+
+
12+
// RECORDING
13+
const auto attributes =
14+
ThreadAttributes().SetPriority(ThreadPriority::kRealtime);
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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.media.audio;
18+
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
20+
21+
import java.util.concurrent.CountDownLatch;
22+
import java.util.concurrent.TimeUnit;
23+
24+
import dev.onvoid.webrtc.PeerConnectionFactory;
25+
import dev.onvoid.webrtc.PeerConnectionObserver;
26+
import dev.onvoid.webrtc.RTCConfiguration;
27+
import dev.onvoid.webrtc.RTCIceCandidate;
28+
import dev.onvoid.webrtc.RTCPeerConnection;
29+
30+
import org.junit.jupiter.api.Test;
31+
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
32+
import org.junit.jupiter.api.condition.EnabledOnOs;
33+
import org.junit.jupiter.api.condition.OS;
34+
import org.junit.jupiter.api.parallel.Execution;
35+
import org.junit.jupiter.api.parallel.ExecutionMode;
36+
37+
/**
38+
* Requires a running PulseAudio-compatible server. Run explicitly with
39+
* -Dwebrtc.test.pulseaudio=true; PULSE_SINK and PULSE_SOURCE can select a
40+
* virtual sink and its monitor so no speakers or microphone are used.
41+
*/
42+
@EnabledOnOs(OS.LINUX)
43+
@EnabledIfSystemProperty(named = "webrtc.test.pulseaudio", matches = "true")
44+
@Execution(ExecutionMode.SAME_THREAD)
45+
class PulseAudioIntegrationTest {
46+
47+
@Test
48+
void playoutRestarts() throws Exception {
49+
restartAudio(false);
50+
}
51+
52+
@Test
53+
void recordingRestarts() throws Exception {
54+
restartAudio(true);
55+
}
56+
57+
private void restartAudio(boolean recording) throws Exception {
58+
AudioDeviceModule adm = new AudioDeviceModule(AudioLayer.kLinuxPulseAudio);
59+
try {
60+
PeerConnectionFactory factory = new PeerConnectionFactory(adm);
61+
try {
62+
// Closing the last peer terminates the ADM. Each subsequent
63+
// peer must reinitialize both PulseAudio processing threads.
64+
for (int cycle = 1; cycle <= 5; cycle++) {
65+
RTCPeerConnection peer = factory.createPeerConnection(
66+
new RTCConfiguration(), new PeerConnectionObserver() {
67+
@Override
68+
public void onIceCandidate(RTCIceCandidate candidate) { }
69+
});
70+
try {
71+
System.out.printf("PulseAudio %s cycle %d%n",
72+
recording ? "recording" : "playout", cycle);
73+
CountDownLatch frames = new CountDownLatch(3);
74+
if (recording) {
75+
adm.setAudioSink((data, samples, bytes, channels, rate, delay, drift) -> {
76+
if (samples > 0) {
77+
frames.countDown();
78+
}
79+
});
80+
adm.initRecording();
81+
adm.startRecording();
82+
}
83+
else {
84+
adm.setAudioSource((data, samples, bytes, channels, rate) -> {
85+
frames.countDown();
86+
return samples;
87+
});
88+
adm.initPlayout();
89+
adm.startPlayout();
90+
}
91+
assertTrue(frames.await(5, TimeUnit.SECONDS),
92+
"No audio callbacks in cycle " + cycle);
93+
}
94+
finally {
95+
peer.close();
96+
}
97+
}
98+
}
99+
finally {
100+
factory.dispose();
101+
}
102+
}
103+
finally {
104+
adm.dispose();
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)