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

feat: mediadevices #96

Open
wants to merge 1 commit 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
4 changes: 3 additions & 1 deletion webrtc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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++")
Expand Down
26 changes: 26 additions & 0 deletions webrtc-sys/include/livekit/macos/media_devices.h
Original file line number Diff line number Diff line change
@@ -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 <string>

#include "livekit/media_devices.h"

namespace livekit {

class MacMediaDevices : public MediaDevices {
std::vector<DeviceInfo> ListDevices() const override;
};
} // namespace livekit
35 changes: 35 additions & 0 deletions webrtc-sys/include/livekit/media_devices.h
Original file line number Diff line number Diff line change
@@ -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 <memory>
#include <vector>

#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<DeviceInfo> ListDevices() const = 0;
};

} // namespace livekit
1 change: 1 addition & 0 deletions webrtc-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions webrtc-sys/src/macos/media_devices.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#import <AVFoundation/AVFoundation.h>

#include <vector>
#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<DeviceInfo> MacMediaDevices::ListDevices() const {
std::vector<DeviceInfo> 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
Empty file.
33 changes: 33 additions & 0 deletions webrtc-sys/src/media_devices.rs
Original file line number Diff line number Diff line change
@@ -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");

}
}
1 change: 0 additions & 1 deletion webrtc-sys/src/objc_video_factory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
namespace livekit {

std::unique_ptr<webrtc::VideoEncoderFactory> CreateObjCVideoEncoderFactory() {
// TODO(theomonnom): Simulcast?
return webrtc::ObjCToNativeVideoEncoderFactory([[RTCVideoEncoderFactoryH264 alloc] init]);
}

Expand Down