From f4148de9cfc7243a007817e301b7c3809ef34d2b Mon Sep 17 00:00:00 2001 From: evdokimovs Date: Fri, 19 Jan 2024 14:18:34 +0100 Subject: [PATCH 1/7] Add if flags --- crates/libwebrtc-sys/build.rs | 3 +- .../libwebrtc-sys/src/cpp/device_info_mac.mm | 31 +++++++++++++------ crates/libwebrtc-sys/src/cpp/mac_capturer.mm | 28 ++++++++++------- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/crates/libwebrtc-sys/build.rs b/crates/libwebrtc-sys/build.rs index b4b43a2c5b..17c69a8101 100644 --- a/crates/libwebrtc-sys/build.rs +++ b/crates/libwebrtc-sys/build.rs @@ -30,6 +30,8 @@ static OPENAL_URL: &str = "https://github.com/kcat/openal-soft/archive/refs/tags/1.23.1"; fn main() -> anyhow::Result<()> { + #[cfg(target_os = "macos")] + println!("cargo:rustc-env=MACOSX_DEPLOYMENT_TARGET=10.11"); let lib_dir = libpath()?; if lib_dir.exists() { fs::create_dir_all(&lib_dir)?; @@ -72,7 +74,6 @@ fn main() -> anyhow::Result<()> { } #[cfg(target_os = "macos")] { - println!("cargo:rustc-env=MACOSX_DEPLOYMENT_TARGET=10.11"); build .include(libpath.join("include/sdk/objc/base")) .include(libpath.join("include/sdk/objc")); diff --git a/crates/libwebrtc-sys/src/cpp/device_info_mac.mm b/crates/libwebrtc-sys/src/cpp/device_info_mac.mm index 31c0ef9d56..1ce95b745f 100644 --- a/crates/libwebrtc-sys/src/cpp/device_info_mac.mm +++ b/crates/libwebrtc-sys/src/cpp/device_info_mac.mm @@ -14,6 +14,8 @@ DeviceInfoMac::~DeviceInfoMac() {} uint32_t DeviceInfoMac::NumberOfDevices() { + NSArray* devices; + if (@available(macOS 10.15, *)) { AVCaptureDeviceDiscoverySession* discoverySession = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[ @@ -23,7 +25,11 @@ mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionUnspecified]; - NSArray* devices = discoverySession.devices; + devices = discoverySession.devices; + } else { + devices = + [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; + } return [devices count]; } @@ -40,16 +46,21 @@ uint32_t deviceUniqueIdUTF8Length, char* /*productUniqueIdUTF8*/, uint32_t /*productUniqueIdUTF8Length*/) { - AVCaptureDeviceDiscoverySession* discoverySession = - [AVCaptureDeviceDiscoverySession - discoverySessionWithDeviceTypes:@[ - AVCaptureDeviceTypeBuiltInWideAngleCamera, - AVCaptureDeviceTypeExternalUnknown - ] - mediaType:AVMediaTypeVideo - position:AVCaptureDevicePositionUnspecified]; + NSArray* devices; + if (@available(macOS 10.15, *)) { + AVCaptureDeviceDiscoverySession* discoverySession = + [AVCaptureDeviceDiscoverySession + discoverySessionWithDeviceTypes:@[ + AVCaptureDeviceTypeBuiltInWideAngleCamera, + AVCaptureDeviceTypeExternalUnknown + ] + mediaType:AVMediaTypeVideo + position:AVCaptureDevicePositionUnspecified]; + devices = discoverySession.devices; + } else { + devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; + } - NSArray* devices = discoverySession.devices; AVCaptureDevice* device = devices[deviceNumber]; deviceNameLength = [device.localizedName length]; memset(deviceNameUTF8, 0, deviceNameLength); diff --git a/crates/libwebrtc-sys/src/cpp/mac_capturer.mm b/crates/libwebrtc-sys/src/cpp/mac_capturer.mm index b00244675a..41ff881a8c 100644 --- a/crates/libwebrtc-sys/src/cpp/mac_capturer.mm +++ b/crates/libwebrtc-sys/src/cpp/mac_capturer.mm @@ -38,8 +38,8 @@ - (void)capturer:(RTCVideoCapturer*)capturer didCaptureVideoFrame:(RTCVideoFrame int currentDiff = INT_MAX; for (AVCaptureDeviceFormat* format in formats) { CMVideoDimensions dimension = CMVideoFormatDescriptionGetDimensions(format.formatDescription); - int diff = std::abs((int64_t)width - dimension.width) + - std::abs((int64_t)height - dimension.height); + int diff = + std::abs((int64_t)width - dimension.width) + std::abs((int64_t)height - dimension.height); if (diff < currentDiff) { selectedFormat = format; currentDiff = diff; @@ -76,16 +76,20 @@ - (void)capturer:(RTCVideoCapturer*)capturer didCaptureVideoFrame:(RTCVideoFrame size_t height, size_t target_fps, uint32_t capture_device_index) { - AVCaptureDeviceDiscoverySession* discoverySession = - [AVCaptureDeviceDiscoverySession - discoverySessionWithDeviceTypes:@[ - AVCaptureDeviceTypeBuiltInWideAngleCamera, - AVCaptureDeviceTypeExternalUnknown - ] - mediaType:AVMediaTypeVideo - position:AVCaptureDevicePositionUnspecified]; - - NSArray* devices = discoverySession.devices; + NSArray* devices; + if (@available(macOS 10.15, *)) { + AVCaptureDeviceDiscoverySession* discoverySession = [AVCaptureDeviceDiscoverySession + discoverySessionWithDeviceTypes:@[ + AVCaptureDeviceTypeBuiltInWideAngleCamera, AVCaptureDeviceTypeExternalUnknown + ] + mediaType:AVMediaTypeVideo + position:AVCaptureDevicePositionUnspecified]; + devices = discoverySession.devices; + } + else { + devices = [RTCCameraVideoCapturer captureDevices]; + } + AVCaptureDevice* device = [devices objectAtIndex:capture_device_index]; if (!device) { RTC_LOG(LS_ERROR) << "Failed to create MacCapture"; From 54ade4a04dc479105cbf7c1910ae21b801ff4167 Mon Sep 17 00:00:00 2001 From: alexlapa Date: Fri, 19 Jan 2024 17:51:46 +0200 Subject: [PATCH 2/7] corrections --- crates/libwebrtc-sys/build.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/libwebrtc-sys/build.rs b/crates/libwebrtc-sys/build.rs index 17c69a8101..46ced4b320 100644 --- a/crates/libwebrtc-sys/build.rs +++ b/crates/libwebrtc-sys/build.rs @@ -30,13 +30,15 @@ static OPENAL_URL: &str = "https://github.com/kcat/openal-soft/archive/refs/tags/1.23.1"; fn main() -> anyhow::Result<()> { - #[cfg(target_os = "macos")] - println!("cargo:rustc-env=MACOSX_DEPLOYMENT_TARGET=10.11"); let lib_dir = libpath()?; if lib_dir.exists() { fs::create_dir_all(&lib_dir)?; } download_libwebrtc()?; + + #[cfg(target_os = "macos")] + std::env::set_var("MACOSX_DEPLOYMENT_TARGET", 10.11); + compile_openal()?; let path = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?); From 634e37b69e61950731cdbec7bfdac3e971af5ea2 Mon Sep 17 00:00:00 2001 From: alexlapa Date: Wed, 24 Jan 2024 14:04:32 +0200 Subject: [PATCH 3/7] corrections --- README.md | 2 +- crates/libwebrtc-sys/build.rs | 5 ++++- crates/native/build.rs | 3 ++- example/macos/Podfile.lock | 2 +- example/macos/Runner.xcodeproj/project.pbxproj | 5 +++-- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index edd861140e..c76e73e4e3 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Initially, represented a fork of the [Flutter-WebRTC] plugin, but at the moment, ## Supported platforms -- [macOS] 10.11+ +- [macOS] 10.13+ - [Linux] (with [PulseAudio] and [X11] for screen sharing) - [Windows] 7+ - [Android] 24+ diff --git a/crates/libwebrtc-sys/build.rs b/crates/libwebrtc-sys/build.rs index 46ced4b320..3fb555b063 100644 --- a/crates/libwebrtc-sys/build.rs +++ b/crates/libwebrtc-sys/build.rs @@ -37,7 +37,10 @@ fn main() -> anyhow::Result<()> { download_libwebrtc()?; #[cfg(target_os = "macos")] - std::env::set_var("MACOSX_DEPLOYMENT_TARGET", 10.11); + { + println!("cargo:rustc-env=MACOSX_DEPLOYMENT_TARGET=10.13"); + env::set_var("MACOSX_DEPLOYMENT_TARGET", "10.13"); + } compile_openal()?; diff --git a/crates/native/build.rs b/crates/native/build.rs index b5fed8adf1..76a9bec249 100644 --- a/crates/native/build.rs +++ b/crates/native/build.rs @@ -7,7 +7,8 @@ use std::{env, path::PathBuf, process}; fn main() -> anyhow::Result<()> { #[cfg(target_os = "macos")] { - println!("cargo:rustc-env=MACOSX_DEPLOYMENT_TARGET=10.11"); + println!("cargo:rustc-env=MACOSX_DEPLOYMENT_TARGET=10.13"); + env::set_var("MACOSX_DEPLOYMENT_TARGET", "10.13"); println!("cargo:rustc-link-arg=-Wl,-undefined,dynamic_lookup"); println!( "cargo:rustc-link-arg=-Wl,-install_name,\ diff --git a/example/macos/Podfile.lock b/example/macos/Podfile.lock index 3f9e82858e..55c18d2936 100644 --- a/example/macos/Podfile.lock +++ b/example/macos/Podfile.lock @@ -19,4 +19,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 353c8bcc5d5b0994e508d035b5431cfe18c1dea7 -COCOAPODS: 1.14.3 +COCOAPODS: 1.14.2 diff --git a/example/macos/Runner.xcodeproj/project.pbxproj b/example/macos/Runner.xcodeproj/project.pbxproj index 109459f43f..097f5b8a23 100644 --- a/example/macos/Runner.xcodeproj/project.pbxproj +++ b/example/macos/Runner.xcodeproj/project.pbxproj @@ -411,7 +411,7 @@ "$(inherited)", "@executable_path/../Frameworks", ); - ONLY_ACTIVE_ARCH = YES; + ONLY_ACTIVE_ARCH = NO; PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_VERSION = 5.0; }; @@ -539,6 +539,7 @@ "$(inherited)", "@executable_path/../Frameworks", ); + ONLY_ACTIVE_ARCH = NO; PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_VERSION = 5.0; @@ -560,7 +561,7 @@ "$(inherited)", "@executable_path/../Frameworks", ); - ONLY_ACTIVE_ARCH = YES; + ONLY_ACTIVE_ARCH = NO; PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_VERSION = 5.0; }; From a9206466970b5bef83e52d01fcfbfae80eec314d Mon Sep 17 00:00:00 2001 From: alexlapa Date: Wed, 24 Jan 2024 14:53:17 +0200 Subject: [PATCH 4/7] corrections --- example/macos/Runner.xcodeproj/project.pbxproj | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/example/macos/Runner.xcodeproj/project.pbxproj b/example/macos/Runner.xcodeproj/project.pbxproj index 02588a82e9..a7ec9cb151 100644 --- a/example/macos/Runner.xcodeproj/project.pbxproj +++ b/example/macos/Runner.xcodeproj/project.pbxproj @@ -429,7 +429,7 @@ "$(inherited)", "@executable_path/../Frameworks", ); - ONLY_ACTIVE_ARCH = NO; + ONLY_ACTIVE_ARCH = YES; PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_VERSION = 5.0; }; @@ -557,7 +557,6 @@ "$(inherited)", "@executable_path/../Frameworks", ); - ONLY_ACTIVE_ARCH = NO; PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; SWIFT_VERSION = 5.0; @@ -579,7 +578,7 @@ "$(inherited)", "@executable_path/../Frameworks", ); - ONLY_ACTIVE_ARCH = NO; + ONLY_ACTIVE_ARCH = YES; PROVISIONING_PROFILE_SPECIFIER = ""; SWIFT_VERSION = 5.0; }; From 8142ff0e123a688367312a4a5d30bdc917698709 Mon Sep 17 00:00:00 2001 From: evdokimovs Date: Thu, 25 Jan 2024 15:34:28 +0100 Subject: [PATCH 5/7] Weakly link AVFoundation --- crates/libwebrtc-sys/build.rs | 7 +++++++ crates/native/build.rs | 8 +++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/libwebrtc-sys/build.rs b/crates/libwebrtc-sys/build.rs index 17c69a8101..14de4e050c 100644 --- a/crates/libwebrtc-sys/build.rs +++ b/crates/libwebrtc-sys/build.rs @@ -481,6 +481,13 @@ fn link_libs() -> anyhow::Result<()> { ] { println!("cargo:rustc-link-lib=framework={framework}"); } + // AVFoundation framework needs to be weakly linked due to the usage of + // new APIs in libwebrtc-sys that are not available on older systems. If + // AVFoundation is linked strongly, dyld on application start may crash + // the application because it cannot link the new API symbols on the + // older MacOS system. + println!("cargo:rustc-link-arg=-weak_framework"); + println!("cargo:rustc-link-arg=AVFoundation"); if let Some(path) = macos_link_search_path() { println!("cargo:rustc-link-lib=clang_rt.osx"); println!("cargo:rustc-link-search={path}"); diff --git a/crates/native/build.rs b/crates/native/build.rs index b5fed8adf1..e145f57145 100644 --- a/crates/native/build.rs +++ b/crates/native/build.rs @@ -39,7 +39,13 @@ fn main() -> anyhow::Result<()> { #[cfg(target_os = "macos")] /// Emits all the required `rustc-link-lib` instructions. fn link_libs() { - println!("cargo:rustc-link-lib=framework=AVFoundation"); + // AVFoundation framework needs to be weakly linked due to the usage of + // new APIs in libwebrtc-sys that are not available on older systems. If + // AVFoundation is linked strongly, dyld on application start may crash + // the application because it cannot link the new API symbols on the + // older MacOS system. + println!("cargo:rustc-link-arg=-weak_framework"); + println!("cargo:rustc-link-arg=AVFoundation"); if let Some(path) = macos_link_search_path() { println!("cargo:rustc-link-lib=clang_rt.osx"); println!("cargo:rustc-link-search={path}"); From 363259f1471e277dd05da6ac0a9f8678f8380d17 Mon Sep 17 00:00:00 2001 From: evdokimovs Date: Thu, 25 Jan 2024 17:04:05 +0100 Subject: [PATCH 6/7] Fix for older MacOS --- crates/libwebrtc-sys/src/cpp/video_sink.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/libwebrtc-sys/src/cpp/video_sink.cc b/crates/libwebrtc-sys/src/cpp/video_sink.cc index 6c1e63c210..91ad04d3da 100644 --- a/crates/libwebrtc-sys/src/cpp/video_sink.cc +++ b/crates/libwebrtc-sys/src/cpp/video_sink.cc @@ -10,7 +10,7 @@ ForwardingVideoSink::ForwardingVideoSink( // Propagates the received `VideoFrame` to the Rust side. void ForwardingVideoSink::OnFrame(const webrtc::VideoFrame& video_frame) { - bridge::on_frame(*cb_.value(), + bridge::on_frame(**cb_, std::make_unique(video_frame)); } From 2a79eab0681cde179dd899d759b840e476c098e7 Mon Sep 17 00:00:00 2001 From: evdokimovs Date: Fri, 26 Jan 2024 10:04:07 +0100 Subject: [PATCH 7/7] Fix std::optional --- crates/libwebrtc-sys/src/cpp/bridge.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/libwebrtc-sys/src/cpp/bridge.cc b/crates/libwebrtc-sys/src/cpp/bridge.cc index 1956d426a1..aa810f7c98 100644 --- a/crates/libwebrtc-sys/src/cpp/bridge.cc +++ b/crates/libwebrtc-sys/src/cpp/bridge.cc @@ -34,7 +34,7 @@ TrackEventObserver::TrackEventObserver( // is attached to, has its state changed. void TrackEventObserver::OnChanged() { if (track_) { - if (track_.value()->state() == + if ((*track_)->state() == webrtc::MediaStreamTrackInterface::TrackState::kEnded) { bridge::on_ended(*cb_); }