diff --git a/webrtc-sys/build.rs b/webrtc-sys/build.rs index 468fff10..bf98dd8f 100644 --- a/webrtc-sys/build.rs +++ b/webrtc-sys/build.rs @@ -136,6 +136,7 @@ fn main() { "src/peer_connection.rs", "src/peer_connection_factory.rs", "src/media_stream.rs", + "src/media_devices.rs", "src/media_stream_track.rs", "src/audio_track.rs", "src/video_track.rs", @@ -159,6 +160,7 @@ fn main() { "src/peer_connection.cpp", "src/peer_connection_factory.cpp", "src/media_stream.cpp", + "src/media_devices.cpp", "src/media_stream_track.cpp", "src/audio_track.cpp", "src/video_track.cpp", @@ -276,7 +278,7 @@ fn main() { } } - builder.file("src/objc_video_factory.mm"); + builder.files(&["src/objc_video_factory.mm", "src/macos/media_devices.mm"]); builder .flag("-stdlib=libc++") diff --git a/webrtc-sys/include/livekit/macos/media_devices.h b/webrtc-sys/include/livekit/macos/media_devices.h new file mode 100644 index 00000000..27e933db --- /dev/null +++ b/webrtc-sys/include/livekit/macos/media_devices.h @@ -0,0 +1,26 @@ +/* + * Copyright 2023 LiveKit + * + * Licensed under the Apache License, Version 2.0 (the “License”); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "livekit/media_devices.h" + +namespace livekit { + +class MacMediaDevices : public MediaDevices { + std::vector ListDevices() const override; +}; +} // namespace livekit diff --git a/webrtc-sys/include/livekit/media_devices.h b/webrtc-sys/include/livekit/media_devices.h new file mode 100644 index 00000000..eae77178 --- /dev/null +++ b/webrtc-sys/include/livekit/media_devices.h @@ -0,0 +1,35 @@ +/* + * Copyright 2023 LiveKit + * + * Licensed under the Apache License, Version 2.0 (the “License”); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an “AS IS” BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +#include "rust/cxx.h" +#include "webrtc-sys/src/media_devices.rs.h" + +namespace livekit { + +class MediaDevices { + public: + MediaDevices() = default; + virtual ~MediaDevices() = default; + + virtual std::vector ListDevices() const = 0; +}; + +} // namespace livekit diff --git a/webrtc-sys/src/lib.rs b/webrtc-sys/src/lib.rs index d24faea0..3b703225 100644 --- a/webrtc-sys/src/lib.rs +++ b/webrtc-sys/src/lib.rs @@ -4,6 +4,7 @@ pub mod candidate; pub mod data_channel; pub mod helper; pub mod jsep; +pub mod media_devices; pub mod media_stream; pub mod media_stream_track; pub mod peer_connection; diff --git a/webrtc-sys/src/macos/media_devices.mm b/webrtc-sys/src/macos/media_devices.mm new file mode 100644 index 00000000..b3fc5c3b --- /dev/null +++ b/webrtc-sys/src/macos/media_devices.mm @@ -0,0 +1,38 @@ +#import + +#include +#include "livekit/macos/media_devices.h" + +namespace livekit { + +DeviceFacing to_rust_facing(AVCaptureDevicePosition position) { + switch (position) { + case AVCaptureDevicePositionBack: + return DeviceFacing::Environment; + case AVCaptureDevicePositionFront: + return DeviceFacing::User; + case AVCaptureDevicePositionUnspecified: + return DeviceFacing::Unknown; + } +} + +std::vector MacMediaDevices::ListDevices() const { + std::vector devices; + + // video devices + NSArray* videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; + for (AVCaptureDevice* device in videoDevices) { + DeviceInfo info{}; + info.facing = to_rust_facing(device.position); + info.id = [device.uniqueID UTF8String]; + info.name = [device.localizedName UTF8String]; + info.kind = DeviceKind::VideoInput; + devices.push_back(info); + } + + // audio devices + + return devices; +} + +} // namespace livekit \ No newline at end of file diff --git a/webrtc-sys/src/media_devices.cpp b/webrtc-sys/src/media_devices.cpp new file mode 100644 index 00000000..e69de29b diff --git a/webrtc-sys/src/media_devices.rs b/webrtc-sys/src/media_devices.rs new file mode 100644 index 00000000..e60dfec0 --- /dev/null +++ b/webrtc-sys/src/media_devices.rs @@ -0,0 +1,33 @@ +use crate::impl_thread_safety; +use std::error::Error; +use std::fmt::{Display, Formatter}; + +#[cxx::bridge(namespace = "livekit")] +pub mod ffi { + + #[repr(i32)] + pub enum DeviceFacing { + Unknown, + User, + Environment, + } + + #[repr(i32)] + pub enum DeviceKind { + AudioInput, + AudioOutput, + VideoInput, + } + + pub struct DeviceInfo { + pub id: String, + pub name: String, + pub kind: DeviceKind, + pub facing: DeviceFacing, // If video input + } + + unsafe extern "C++" { + include!("livekit/media_devices.h"); + + } +} diff --git a/webrtc-sys/src/objc_video_factory.mm b/webrtc-sys/src/objc_video_factory.mm index 1ad79a0e..de12d977 100644 --- a/webrtc-sys/src/objc_video_factory.mm +++ b/webrtc-sys/src/objc_video_factory.mm @@ -24,7 +24,6 @@ namespace livekit { std::unique_ptr CreateObjCVideoEncoderFactory() { - // TODO(theomonnom): Simulcast? return webrtc::ObjCToNativeVideoEncoderFactory([[RTCVideoEncoderFactoryH264 alloc] init]); }