Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crashes on old MacOS versions #147

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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+
Expand Down
15 changes: 14 additions & 1 deletion crates/libwebrtc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?);
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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}");
Expand Down
2 changes: 1 addition & 1 deletion crates/libwebrtc-sys/src/cpp/bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
}
Expand Down
31 changes: 21 additions & 10 deletions crates/libwebrtc-sys/src/cpp/device_info_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
DeviceInfoMac::~DeviceInfoMac() {}

uint32_t DeviceInfoMac::NumberOfDevices() {
NSArray<AVCaptureDevice*>* devices;
if (@available(macOS 10.15, *)) {
AVCaptureDeviceDiscoverySession* discoverySession =
[AVCaptureDeviceDiscoverySession
discoverySessionWithDeviceTypes:@[
Expand All @@ -23,7 +25,11 @@
mediaType:AVMediaTypeVideo
position:AVCaptureDevicePositionUnspecified];

NSArray<AVCaptureDevice*>* devices = discoverySession.devices;
devices = discoverySession.devices;
} else {
devices =
[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
}

return [devices count];
}
Expand All @@ -40,16 +46,21 @@
uint32_t deviceUniqueIdUTF8Length,
char* /*productUniqueIdUTF8*/,
uint32_t /*productUniqueIdUTF8Length*/) {
AVCaptureDeviceDiscoverySession* discoverySession =
[AVCaptureDeviceDiscoverySession
discoverySessionWithDeviceTypes:@[
AVCaptureDeviceTypeBuiltInWideAngleCamera,
AVCaptureDeviceTypeExternalUnknown
]
mediaType:AVMediaTypeVideo
position:AVCaptureDevicePositionUnspecified];
NSArray<AVCaptureDevice*>* 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<AVCaptureDevice*>* devices = discoverySession.devices;
AVCaptureDevice* device = devices[deviceNumber];
deviceNameLength = [device.localizedName length];
memset(deviceNameUTF8, 0, deviceNameLength);
Expand Down
28 changes: 16 additions & 12 deletions crates/libwebrtc-sys/src/cpp/mac_capturer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<AVCaptureDevice*>* devices = discoverySession.devices;
NSArray<AVCaptureDevice*>* 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";
Expand Down
2 changes: 1 addition & 1 deletion crates/libwebrtc-sys/src/cpp/video_sink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<webrtc::VideoFrame>(video_frame));
}

Expand Down
11 changes: 9 additions & 2 deletions crates/native/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,\
Expand Down Expand Up @@ -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}");
Expand Down
2 changes: 1 addition & 1 deletion example/macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 353c8bcc5d5b0994e508d035b5431cfe18c1dea7

COCOAPODS: 1.14.3
COCOAPODS: 1.14.2
Loading