From ed47881197cb778a80550b61fd86c3881de47384 Mon Sep 17 00:00:00 2001 From: Alex Andres Date: Wed, 11 Nov 2026 23:40:14 +0100 Subject: [PATCH] fix: report why macOS system audio capture stopped A stopped ScreenCaptureKit stream always failed with a hint to check the recording permission, even when the user ended the capture from the menu bar or the display went away. The failure now names the actual cause: stopped by the user, permission declined, or the system's error code and description. Capture timestamps are translated from the stream's synchronization clock, as ScreenCaptureKit documents, falling back to the host clock when the stream reports none. --- .../media/audio/macos/SystemAudioCapture.cpp | 47 +++++++++++++++++-- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/webrtc-jni/src/main/cpp/src/media/audio/macos/SystemAudioCapture.cpp b/webrtc-jni/src/main/cpp/src/media/audio/macos/SystemAudioCapture.cpp index d49bd25df..8f7d05529 100644 --- a/webrtc-jni/src/main/cpp/src/media/audio/macos/SystemAudioCapture.cpp +++ b/webrtc-jni/src/main/cpp/src/media/audio/macos/SystemAudioCapture.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include // Receives the audio of an SCStream. A receiver may outlive a timed-out start @@ -47,8 +48,36 @@ API_AVAILABLE(macos(13.0)) - (void)stream:(SCStream *)stream didStopWithError:(NSError *)error { @synchronized(self) { - if (owner) { - owner->Fail("System audio: ScreenCaptureKit stopped; check recording permission and restart"); + if (!owner) { + return; + } + + // Name the actual cause: the user ending the capture from the menu bar + // or a lost display needs no permission change. + try { + std::string message = "System audio: ScreenCaptureKit stopped"; + + if ([error.domain isEqualToString:SCStreamErrorDomain] && error.code == SCStreamErrorUserStopped) { + message += " by the user"; + } + else if ([error.domain isEqualToString:SCStreamErrorDomain] && error.code == SCStreamErrorUserDeclined) { + message += "; grant screen/audio recording permission and restart"; + } + else if (error) { + const char * reason = error.localizedDescription.UTF8String; + + message += " (error " + std::to_string(error.code) + ")"; + + if (reason) { + message += ": "; + message += reason; + } + } + + owner->Fail(message.c_str()); + } + catch (...) { + owner->Fail("System audio: ScreenCaptureKit stopped"); } } } @@ -124,10 +153,18 @@ API_AVAILABLE(macos(13.0)) } } - // The presentation time is on the host clock; translate it to the - // clock of webrtc::TimeMicros(). + // The presentation time is on the stream's synchronization clock; + // translate it to the clock of webrtc::TimeMicros(). That clock is + // the host clock in practice, which is the fallback when the stream + // reports none. + CMClockRef clock = stream.synchronizationClock; + + if (!clock) { + clock = CMClockGetHostTimeClock(); + } + const double pts = CMTimeGetSeconds(CMSampleBufferGetPresentationTimeStamp(sample)); - const double now = CMTimeGetSeconds(CMClockGetTime(CMClockGetHostTimeClock())); + const double now = CMTimeGetSeconds(CMClockGetTime(clock)); if (!std::isfinite(pts) || !std::isfinite(now)) { owner->Fail("System audio: Invalid ScreenCaptureKit timestamp");