Skip to content

Commit

Permalink
Linux demo and native code fixes (versatica#62)
Browse files Browse the repository at this point in the history
* Fix-up signalling code

* Make `SignallingChannel` trait partially async
* Use `tokio-tungstenite` once again
* Add peer ID to `sig-ack` message as it is now required

* Remove some `todo!()` and fix `mediasoupclient-sys` code.

* Invoke on_open when WS is connected

* Make native callbacks settable at any point

* Undo sig-ack changes

* Use message-passing architecture to untangle asynchronous mutating callbacks

* Closer to equivalence between WASM and Native

* Dirty fixing of the event emission for JS

* Implement Transport::consume

* Fill up some more native stubs

* WebRTC MediaStream bindings

* Minor fixes
- Temporarily disable event emission on native platforms
- Fix native "on connect" transport callbacks

* Convert Linux demo to a GTK+ app to not have a silly infinite loop (and maybe present the cameras later!)

* Align Native and WASM daily-core implementations further

- Make WebSocket callbacks registerable in the same order on
  all targets (e.g. enable registering the callbacks for browser
  WebSockets before we attempt to open them)
- Flesh out more native implementation details in WebRTC

* Small tweaks

* Update fake_tracks feature

* Re-enable participant event emission (still requires action on them)

* Prototype C event API

* Refactor for code organization

* Fix android build

* Nuke fake tracks from orbit

* Slight cleanup

* Partial success at cleaning up the code

* Cleanup

* Fix clippy warning

* Unify stub enumerate_devices

* Clippy fix

* Fix initial review comments

* Clearer names in native WebSocket signaling
  • Loading branch information
jpgneves authored Jun 4, 2021
1 parent db49067 commit 192e1d8
Show file tree
Hide file tree
Showing 249 changed files with 3,148 additions and 30,297 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["daily-core", "mediasoupclient", "mediasoupclient-sys", "webrtc-sys"]
members = ["daily-core", "mediasoupclient", "mediasoupclient-sys", "webrtc-daily", "webrtc-sys"]
[profile.release]
lto = true # Link-time optimization to enable more places where code can be inlined
#opt-level = 's' # Optimize for size
5 changes: 3 additions & 2 deletions daily-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ crate-type = ["lib", "cdylib", "staticlib"]

[dependencies]
mediasoupclient = { path = "../mediasoupclient" }
webrtc-daily = { path = "../webrtc-daily" }
anyhow = { version = "1.0", default_features = false }
async-trait = "0.1"
chrono = "0.4"
futures = { version = "0.3", default_features = false, features = ["std"] }
futures = { version = "0.3", default_features = false, features = ["std", "executor"] }
isocountry = "0.3"
log = { version = "0.4", default_features = false }
querystring = { version = "1.1", default_features = false }
Expand Down Expand Up @@ -63,7 +64,7 @@ wasm-bindgen-test = "0.3"
libc = "0.2"
pretty_env_logger = "0.4"
tokio = { version = "1.2", default_features = false, features = ["net", "rt", "rt-multi-thread", "time"] }
tungstenite = { version = "0.13", default_features = false, features = ["rustls-tls"] }
tokio-tungstenite = { version = "0.14", default_features = false, features = [ "connect", "rustls-tls"] }
ureq = { version = "2.0", default_features = false, features = ["tls", "json"] }

[target.'cfg(target_os="android")'.dependencies]
Expand Down
20 changes: 13 additions & 7 deletions daily-core/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
//! for `daily-core`.
use std::sync::{Arc, Mutex};

#[allow(unused_imports)]
#[cfg(target_arch = "wasm32")]
use anyhow::anyhow;
use mediasoupclient::stream::{MediaStream, MediaStreamConstraints, MediaStreamTrack};
use serde::{Deserialize, Serialize};
use webrtc_daily::media_stream::{MediaStream, MediaStreamConstraints, MediaStreamTrack};

#[cfg(target_arch = "wasm32")]
use std::convert::TryFrom;
Expand All @@ -16,7 +16,7 @@ use wasm_bindgen::JsCast;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::{prelude::*, JsValue};

use crate::soup::sfu::SoupSfuClient;
use crate::soup::sfu::SoupSfuClientHandle;

#[cfg(feature = "api-v0")]
mod v0;
Expand Down Expand Up @@ -89,7 +89,7 @@ impl TryFrom<JsValue> for MediaSpec {
if v.is_object() {
#[allow(clippy::redundant_clone)]
let v = v.clone();
if v.is_instance_of::<MediaStreamTrack>() {
if v.is_instance_of::<web_sys::MediaStreamTrack>() {
MediaStreamTrack::try_from(v)
.map(|track| MediaSpec::Track { track })
.map_err(|e| anyhow!("Failed to convert from MediaStreamTrack: {:?}", e))
Expand Down Expand Up @@ -181,7 +181,7 @@ pub enum MeetingStatus {
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
#[derive(AsRefStr, EnumString, Clone, PartialEq, Eq, Hash)]
#[derive(AsRefStr, EnumString, Debug, Clone, PartialEq, Eq, Hash)]
/// The possible set of events one can listen on.
pub enum DailyEvent {
// Sadly these need to be done manually
Expand Down Expand Up @@ -227,19 +227,25 @@ impl Participant {
self.id.to_string()
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen(method, structural, getter))]
#[cfg(not(target_arch = "wasm32"))]
/// The stream being produced
pub fn stream(&self) -> MediaStream {
self.stream.clone()
}

#[cfg(target_arch = "wasm32")]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(method, structural, getter))]
pub fn stream(&self) -> web_sys::MediaStream {
self.stream.0.clone()
}
}

#[allow(dead_code)]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
/// A Daily call object. This is the main entry point to `daily-core`.
pub struct CallObject {
pub(crate) meeting_state: Arc<Mutex<MeetingState>>,
pub(crate) sfu_client: Arc<Mutex<Box<SoupSfuClient>>>,
pub(crate) sfu_client: Arc<Mutex<SoupSfuClientHandle>>,
#[cfg(feature = "api-v1")]
pub(crate) send_settings: Option<SendSettings>,
#[cfg(feature = "api-v1")]
Expand Down
2 changes: 1 addition & 1 deletion daily-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ use crate::DailyEvent;
pub(crate) trait EventEmitter<F, T> {
fn on(&mut self, event: DailyEvent, callback: F) -> T;

fn emit(&self, event: DailyEvent, data: T) -> bool;
fn emit(&mut self, event: DailyEvent, data: T) -> bool;
}
6 changes: 3 additions & 3 deletions daily-core/src/native/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use jni::objects::JClass;
use jni::sys::jlong;
use jni::JNIEnv;

use crate::{CallObjectImpl, DailyCoreContext};
use crate::ffi::DailyCoreContext;

/// Initializes Daily Core, creating a core execution context
#[no_mangle]
pub unsafe extern "C" fn Java_co_daily_core_initialize(_env: JNIEnv, _: JClass) -> jlong {
crate::daily_core_initialize() as jlong
crate::ffi::daily_core_initialize() as jlong
}

/// Creates a new [CallObject]
Expand All @@ -17,5 +17,5 @@ pub unsafe extern "C" fn Java_co_daily_core_CallObject_new(
_: JClass,
ctx: jlong,
) -> jlong {
crate::call_object::daily_core_call_object_new(ctx as *mut DailyCoreContext) as jlong
crate::ffi::daily_core_call_object_new(ctx as *mut DailyCoreContext) as jlong
}
44 changes: 11 additions & 33 deletions daily-core/src/native/call_object.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
use crate::{
api::*,
event::EventEmitter,
signalling::Signalling,
soup::sfu::{SoupSfu, SoupSfuClient},
CallConfig,
};

use super::{signalling::WsSignalChannel, DailyCoreContext};
use super::signalling::WsSignalChannel;
use async_trait::async_trait;
#[cfg(feature = "api-v1")]
#[allow(unused_imports)]
use core::future;
use libc::c_int;
#[cfg(feature = "api-v1")]
use std::collections::HashMap;
use std::ffi::CStr;
use std::sync::{Arc, Mutex};

impl CallObject {
pub fn new() -> Self {
let peer_id = PeerId::new();
let channel = WsSignalChannel::new();
let signalling = Signalling::new(peer_id, channel);
let sfu_client = SoupSfuClient::new_boxed(peer_id, signalling);
let sfu_client = SoupSfuClient::create(peer_id, signalling);
Self {
meeting_state: Arc::new(Mutex::new(MeetingState::new())),
sfu_client: Arc::new(Mutex::new(sfu_client)),
Expand All @@ -33,6 +32,14 @@ impl CallObject {
recv_settings: HashMap::new(),
}
}

pub fn on(
&mut self,
event: DailyEvent,
callback: Box<dyn FnMut(Box<dyn std::any::Any + Send>) + Send>,
) {
self.sfu_client.lock().unwrap().on(event, callback);
}
}

impl Default for CallObject {
Expand All @@ -51,38 +58,9 @@ impl CallObjectImpl for CallObject {

let mut sfu = self.sfu_client.lock().unwrap();
sfu.init(&config.url).await?;

sfu.allow_this_implementation_to_accept_calls(true).await?;
self.meeting_state.lock().unwrap().status = MeetingStatus::Joined;

Ok(())
}
}

#[no_mangle]
pub extern "C" fn daily_core_call_object_new(_ctx: *mut DailyCoreContext) -> *mut CallObject {
Box::into_raw(Box::new(CallObject::new()))
}

#[no_mangle]
pub extern "C" fn daily_core_call_object_join(
ctx: *mut DailyCoreContext,
call: *mut CallObject,
url: *const libc::c_char,
) -> c_int {
let ctx = unsafe { &*ctx };
let call = unsafe { &mut *call };
let config = CallConfig {
url: unsafe { CStr::from_ptr(url).to_string_lossy().into_owned() },
video: None,
audio: None,
};

match ctx.runtime.block_on(call.join(&config)) {
Ok(_) => 0,
Err(e) => {
println!("{:?}", e);
1
}
}
}
13 changes: 9 additions & 4 deletions daily-core/src/native/device.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use mediasoupclient::stream::{MediaStream, MediaStreamConstraints};
use webrtc_daily::media_stream::{MediaStream, MediaStreamConstraints};

use crate::device::{MediaDeviceInfo, MediaDeviceKind};

Expand All @@ -10,12 +10,17 @@ pub async fn get_user_media(
}

pub async fn enumerate_devices() -> Result<Vec<MediaDeviceInfo>, anyhow::Error> {
todo!()
Ok(Vec::new())
}

pub fn get_default_device(
_available_devices: Vec<MediaDeviceInfo>,
_kind: MediaDeviceKind,
kind: MediaDeviceKind,
) -> MediaDeviceInfo {
todo!()
MediaDeviceInfo {
device_id: None,
group_id: None,
kind: Some(kind),
label: None,
}
}
29 changes: 29 additions & 0 deletions daily-core/src/native/event_emitter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};

use crate::DailyEvent;

type Callback = Box<dyn FnMut(Box<dyn std::any::Any + Send>) + Send>;

#[derive(Default, Clone)]
pub struct EventEmitter {
callbacks: Arc<Mutex<HashMap<DailyEvent, Callback>>>,
}

impl EventEmitter {
pub fn on(&mut self, event: DailyEvent, callback: Callback) {
self.callbacks.lock().unwrap().insert(event, callback);
}

pub fn emit(&mut self, event: DailyEvent, data: Box<dyn std::any::Any + Send>) -> bool {
match self.callbacks.lock().unwrap().get_mut(&event) {
Some(cb) => {
cb(data);
true
}
None => false,
}
}
}
Loading

0 comments on commit 192e1d8

Please sign in to comment.