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