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 b4b43a2c5b..f8b0682db8 100644 --- a/crates/libwebrtc-sys/build.rs +++ b/crates/libwebrtc-sys/build.rs @@ -35,6 +35,13 @@ fn main() -> anyhow::Result<()> { fs::create_dir_all(&lib_dir)?; } download_libwebrtc()?; + + #[cfg(target_os = "macos")] + { + println!("cargo:rustc-env=MACOSX_DEPLOYMENT_TARGET=10.13"); + env::set_var("MACOSX_DEPLOYMENT_TARGET", "10.13"); + } + compile_openal()?; let path = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?); @@ -72,7 +79,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")); @@ -480,6 +486,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/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_); } 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"; 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)); } diff --git a/crates/native/build.rs b/crates/native/build.rs index b5fed8adf1..a5781cff95 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,\ @@ -39,7 +40,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}"); 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