Skip to content

Commit e3891a3

Browse files
Bring in platform-audio stuff and rebase off main
1 parent 35d954a commit e3891a3

8 files changed

Lines changed: 114 additions & 14 deletions

File tree

‎.github/workflows/builds.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jobs:
5959

6060
steps:
6161
- name: Checkout (with submodules)
62-
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
62+
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
6363
with:
6464
submodules: recursive
6565
fetch-depth: 1

‎.github/workflows/docker-images.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757

5858
steps:
5959
- name: Checkout (with submodules)
60-
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
60+
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
6161
with:
6262
submodules: recursive
6363
fetch-depth: 0
@@ -245,7 +245,7 @@ jobs:
245245

246246
steps:
247247
- name: Checkout
248-
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
248+
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
249249
with:
250250
fetch-depth: 0
251251

‎.github/workflows/nightly.yml‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
integration_repeat: 10
3131
integration_rust_log: info,livekit_ffi::server=debug
3232
platform_audio_repeat: 100
33-
platform_audio_rust_log: info,livekit_ffi::server=debug,livekit_ffi::server::platform_audio=debug,livekit::platform_audio=debug
33+
platform_audio_rust_log: info,livekit_ffi::server=debug
3434
run_stress_tests: true
3535
stress_repeat: 1
3636
unit_timeout_minutes: 60
@@ -73,7 +73,7 @@ jobs:
7373

7474
steps:
7575
- name: Checkout (with submodules)
76-
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
76+
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
7777
with:
7878
submodules: recursive
7979
fetch-depth: 1
@@ -111,9 +111,11 @@ jobs:
111111
- name: Set build environment
112112
run: |
113113
LLVM_VERSION=$(llvm-config --version | cut -d. -f1)
114-
echo "LIBCLANG_PATH=/usr/lib/llvm-${LLVM_VERSION}/lib" >> "$GITHUB_ENV"
115-
echo "CXXFLAGS=-Wno-deprecated-declarations -fno-omit-frame-pointer" >> "$GITHUB_ENV"
116-
echo "CFLAGS=-Wno-deprecated-declarations -fno-omit-frame-pointer" >> "$GITHUB_ENV"
114+
{
115+
echo "LIBCLANG_PATH=/usr/lib/llvm-${LLVM_VERSION}/lib"
116+
echo "CXXFLAGS=-Wno-deprecated-declarations -fno-omit-frame-pointer"
117+
echo "CFLAGS=-Wno-deprecated-declarations -fno-omit-frame-pointer"
118+
} >> "$GITHUB_ENV"
117119
118120
- name: Configure sanitizer build
119121
run: |
@@ -164,7 +166,7 @@ jobs:
164166
EOF
165167
"$RUNNER_TEMP/livekit-server" --config "$RUNNER_TEMP/livekit.yaml" --dev > "$RUNNER_TEMP/livekit.jsonl" 2>&1 &
166168
echo "log-path=$RUNNER_TEMP/livekit.jsonl" >> "$GITHUB_OUTPUT"
167-
for i in $(seq 1 30); do
169+
for _ in $(seq 1 30); do
168170
if [[ "$(curl -fsS http://localhost:7880/ || true)" == "OK" ]]; then
169171
exit 0
170172
fi
@@ -298,7 +300,7 @@ jobs:
298300
EOF
299301
livekit-server --config "$RUNNER_TEMP/livekit.yaml" --dev > "$RUNNER_TEMP/livekit.jsonl" 2>&1 &
300302
echo "log-path=$RUNNER_TEMP/livekit.jsonl" >> "$GITHUB_OUTPUT"
301-
for i in $(seq 1 30); do
303+
for _ in $(seq 1 30); do
302304
if [[ "$(curl -fsS http://localhost:7880/ || true)" == "OK" ]]; then
303305
exit 0
304306
fi
@@ -314,7 +316,7 @@ jobs:
314316
timeout-minutes: 30
315317
shell: bash
316318
env:
317-
RUST_LOG: info,livekit_ffi::server=debug,livekit_ffi::server::platform_audio=debug,livekit::platform_audio=debug
319+
RUST_LOG: info,livekit_ffi::server=debug
318320
RUST_BACKTRACE: full
319321
run: |
320322
set -euo pipefail

‎client-sdk-rust‎

‎include/livekit/local_participant.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,14 @@ class LIVEKIT_API LocalParticipant : public Participant {
246246
/// prune expired @c weak_ptr entries.
247247
mutable TrackMap published_tracks_by_sid_;
248248

249+
/// Guards @ref published_tracks_by_sid_. The map is written from the
250+
/// application thread (@ref publishTrack / @ref unpublishTrack) and both read
251+
/// and pruned from the FFI callback thread (@ref trackPublications /
252+
/// @ref findTrackPublication, reached via Room::onEvent). Without this lock
253+
/// those concurrent accesses race and free map nodes out from under each
254+
/// other (heap-use-after-free). Leaf lock: no other lock is taken while held.
255+
mutable std::mutex published_tracks_mutex_;
256+
249257
std::unordered_map<std::string, RpcHandler> rpc_handlers_;
250258

251259
// Shared state for RPC invocation tracking. Using shared_ptr so the state

‎include/livekit/platform_audio.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ class LIVEKIT_API PlatformAudioSource {
114114

115115
PlatformAudioSource(FfiHandle handle, std::shared_ptr<PlatformAudioState> platform_audio) noexcept;
116116

117-
FfiHandle handle_;
118117
std::shared_ptr<PlatformAudioState> platform_audio_;
118+
FfiHandle handle_;
119119
};
120120

121121
/// Platform audio device manager backed by WebRTC's Audio Device Module.

‎src/local_participant.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ void LocalParticipant::publishTrack(const std::shared_ptr<Track>& track, const T
197197
auto publication = std::make_shared<LocalTrackPublication>(owned_pub);
198198

199199
const std::string sid = publication->sid();
200-
published_tracks_by_sid_[sid] = std::weak_ptr<Track>(track);
200+
{
201+
const std::scoped_lock<std::mutex> lock(published_tracks_mutex_);
202+
published_tracks_by_sid_[sid] = std::weak_ptr<Track>(track);
203+
}
201204

202205
track->setPublication(publication);
203206
}
@@ -237,6 +240,7 @@ void LocalParticipant::unpublishTrack(const std::string& track_sid) {
237240

238241
fut.get();
239242

243+
const std::scoped_lock<std::mutex> lock(published_tracks_mutex_);
240244
if (auto it = published_tracks_by_sid_.find(track_sid); it != published_tracks_by_sid_.end()) {
241245
if (auto t = it->second.lock()) {
242246
t->setPublication(nullptr);
@@ -247,6 +251,7 @@ void LocalParticipant::unpublishTrack(const std::string& track_sid) {
247251

248252
LocalParticipant::PublicationMap LocalParticipant::trackPublications() const {
249253
PublicationMap out;
254+
const std::scoped_lock<std::mutex> lock(published_tracks_mutex_);
250255
for (auto it = published_tracks_by_sid_.begin(); it != published_tracks_by_sid_.end();) {
251256
auto t = it->second.lock();
252257
if (!t) {
@@ -443,6 +448,7 @@ void LocalParticipant::handleRpcMethodInvocation(uint64_t invocation_id, const s
443448
}
444449

445450
std::shared_ptr<TrackPublication> LocalParticipant::findTrackPublication(const std::string& sid) const {
451+
const std::scoped_lock<std::mutex> lock(published_tracks_mutex_);
446452
auto it = published_tracks_by_sid_.find(sid);
447453
if (it == published_tracks_by_sid_.end()) {
448454
return nullptr;

‎src/tests/integration/test_platform_audio.cpp‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,4 +319,88 @@ TEST_F(PlatformAudioIntegrationTest, PlatformAudioFramesReachRemote) {
319319
receiver_room->clearOnAudioFrameCallback(sender_identity, track_name);
320320
}
321321

322+
namespace {
323+
324+
/// Run one publish/subscribe/unpublish cycle against a fresh pair of rooms,
325+
/// reusing a caller-owned PlatformAudio so the underlying Rust LkRuntime (and
326+
/// therefore the platform Audio Device Module) is never torn down between
327+
/// cycles. Returns true if the receiver observed the published track.
328+
bool runPlatformAudioCycle(PlatformAudio& platform_audio, const TestConfig& config, const std::string& track_name) {
329+
RoomOptions options;
330+
options.auto_subscribe = true;
331+
332+
PlatformTrackState receiver_state;
333+
PlatformTrackCollectorDelegate receiver_delegate(receiver_state);
334+
335+
auto receiver_room = std::make_unique<Room>();
336+
receiver_room->setDelegate(&receiver_delegate);
337+
if (!receiver_room->connect(config.url, config.token_b, options)) {
338+
return false;
339+
}
340+
341+
auto sender_room = std::make_unique<Room>();
342+
if (!sender_room->connect(config.url, config.token_a, options)) {
343+
return false;
344+
}
345+
346+
const auto source = platform_audio.createAudioSource();
347+
if (source == nullptr) {
348+
return false;
349+
}
350+
351+
const auto track = LocalAudioTrack::createLocalAudioTrack(track_name, source);
352+
if (track == nullptr) {
353+
return false;
354+
}
355+
356+
TrackPublishOptions publish_options;
357+
publish_options.source = TrackSource::SOURCE_MICROPHONE;
358+
lockLocalParticipant(*sender_room)->publishTrack(track, publish_options);
359+
360+
std::unique_lock<std::mutex> lock(receiver_state.mutex);
361+
return receiver_state.cv.wait_for(lock, kSubscriptionTimeout,
362+
[&]() { return receiver_state.subscribed_audio_names.count(track_name) > 0; });
363+
}
364+
365+
} // namespace
366+
367+
// Control arm for the macOS PlatformAudio instability investigation.
368+
//
369+
// The standard PlatformAudioIntegrationTest cases each call livekit::shutdown()
370+
// in TearDown(), which disposes the FFI server, drops the last Arc<LkRuntime>,
371+
// and runs AdmProxy::~AdmProxy() -> platform_adm_->Terminate(). Under
372+
// --gtest_repeat that means the native CoreAudio ADM is fully terminated and
373+
// recreated on *every* iteration -- the suspected crash path.
374+
//
375+
// This test instead holds a single PlatformAudio alive for the whole test, so
376+
// the runtime and ADM are created once and never terminated between cycles. It
377+
// loops the same connect/publish/subscribe cycle PLATFORM_AUDIO_PIN_ITERATIONS
378+
// times (default 20). If the repeat arm crashes on macOS but this pinned arm
379+
// stays green, the instability is in ADM teardown/recreation, not the steady
380+
// media path.
381+
TEST_F(PlatformAudioIntegrationTest, PinnedRuntimeRepeatedPublishStress) {
382+
EXPECT_TRUE(config_.available) << "Missing integration configuration";
383+
384+
std::unique_ptr<PlatformAudio> platform_audio;
385+
try {
386+
platform_audio = std::make_unique<PlatformAudio>();
387+
} catch (const PlatformAudioError& error) {
388+
GTEST_SKIP() << "PlatformAudio unavailable: " << error.what();
389+
}
390+
391+
int iterations = 20;
392+
if (const char* env = std::getenv("PLATFORM_AUDIO_PIN_ITERATIONS")) {
393+
const int parsed = std::atoi(env);
394+
if (parsed > 0) {
395+
iterations = parsed;
396+
}
397+
}
398+
399+
for (int i = 0; i < iterations; ++i) {
400+
const std::string track_name = "platform-mic-pinned-" + std::to_string(i);
401+
const bool subscribed = runPlatformAudioCycle(*platform_audio, config_, track_name);
402+
ASSERT_TRUE(subscribed) << "Receiver never subscribed on pinned iteration " << i;
403+
}
404+
}
405+
322406
} // namespace livekit::test

0 commit comments

Comments
 (0)