Skip to content

Commit 4f8229c

Browse files
authored
feat: use new webrtc builds & linux support (#54)
1 parent ee83cb8 commit 4f8229c

File tree

3 files changed

+85
-53
lines changed

3 files changed

+85
-53
lines changed

.github/workflows/builds.yml

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ jobs:
2323
target: x86_64-apple-darwin
2424
- os: macos-latest
2525
target: aarch64-apple-darwin
26+
- os: ubuntu-latest
27+
target: x86_64-unknown-linux-gnu
28+
#- os: ubuntu-latest
29+
# target: aarch64-unknown-linux-gnu
2630

2731
name: Build (${{ matrix.target }})
2832
runs-on: ${{ matrix.os }}
@@ -37,6 +41,10 @@ jobs:
3741
rustup update --no-self-update stable
3842
rustup target add ${{ matrix.target }}
3943
44+
- name: Install linux dependencies
45+
if: ${{ matrix.os == 'ubuntu-latest' }}
46+
run: sudo apt install -y libssl-dev libx11-dev libgl1-mesa-dev libxext-dev
47+
4048
- uses: actions/checkout@v3
4149
with:
4250
submodules: true

.github/workflows/tests.yml

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ jobs:
1919
target: x86_64-pc-windows-msvc
2020
- os: macos-latest
2121
target: x86_64-apple-darwin
22+
- os: ubuntu-latest
23+
target: x86_64-unknown-linux-gnu
2224

2325
name: Test (${{ matrix.target }})
2426
runs-on: ${{ matrix.os }}
@@ -33,6 +35,10 @@ jobs:
3335
rustup update --no-self-update stable
3436
rustup target add ${{ matrix.target }}
3537
38+
- name: Install linux dependencies
39+
if: ${{ matrix.os == 'ubuntu-latest' }}
40+
run: sudo apt install -y libssl-dev libx11-dev libgl1-mesa-dev libxext-dev
41+
3642
- uses: actions/checkout@v3
3743
with:
3844
submodules: true

webrtc-sys/build.rs

+71-53
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
use flate2::read::GzDecoder;
21
use regex::Regex;
32
use std::env;
43
use std::fs;
54
use std::io::{self, Write};
65
use std::path;
76
use std::process::Command;
8-
use tar::Archive;
97

10-
const WEBRTC_TAG: &str = "m104.5112.09";
8+
const WEBRTC_TAG: &str = "webrtc-beb0471";
119

12-
fn download_prebuilt(
13-
target_os: &str,
14-
target_arch: &str,
10+
fn download_prebuilt_webrtc(
1511
out_path: path::PathBuf,
1612
) -> Result<path::PathBuf, Box<dyn std::error::Error>> {
17-
let target_arch = match target_arch {
13+
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
14+
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
15+
let use_debug = {
16+
let var = env::var("LK_DEBUG_WEBRTC");
17+
var.is_ok() && var.unwrap() == "true"
18+
};
19+
20+
let target_arch = match target_arch.as_str() {
1821
"aarch64" => "arm64",
19-
_ => target_arch,
22+
"x86_64" => "x64",
23+
_ => &target_arch,
2024
};
2125

22-
let file_ext = if target_os == "windows" {
23-
"zip"
24-
} else {
25-
"tar.gz"
26+
let target_os = match target_os.as_str() {
27+
"windows" => "win",
28+
_ => &target_os,
2629
};
2730

28-
let file_name = format!("webrtc.{}_{}.{}", target_os, target_arch, file_ext);
31+
let profile = if use_debug { "debug" } else { "release" };
32+
let file_name = format!("webrtc-{}-{}-{}.zip", target_os, target_arch, profile);
2933
let file_url = format!(
30-
"https://github.com/webrtc-sdk/webrtc-build/releases/download/{}/{}",
34+
"https://github.com/livekit/client-sdk-rust/releases/download/{}/{}",
3135
WEBRTC_TAG, file_name
3236
);
3337
let file_path = out_path.join(&file_name);
@@ -41,52 +45,52 @@ fn download_prebuilt(
4145
let file = fs::File::create(&file_path)?;
4246
{
4347
// Download WebRTC-SDK
44-
let mut writer = io::BufWriter::new(file);
45-
let mut res = reqwest::blocking::get(&file_url)?;
46-
io::copy(&mut res, &mut writer)?;
48+
let res = reqwest::blocking::get(&file_url);
49+
if let Ok(mut res) = res {
50+
let mut writer = io::BufWriter::new(file);
51+
io::copy(&mut res, &mut writer)?;
52+
} else {
53+
fs::remove_file(&file_path)?;
54+
res?;
55+
}
4756
}
4857

4958
// Extract the archive
5059
let file = fs::File::open(&file_path)?;
51-
if file_ext == "zip" {
52-
let mut archive = zip::ZipArchive::new(file)?;
53-
for i in 0..archive.len() {
54-
let mut inner_file = archive.by_index(i)?;
55-
let relative_path = inner_file.mangled_name();
56-
57-
if relative_path.to_string_lossy().is_empty() {
58-
continue; // Ignore root
59-
}
60+
let res = zip::ZipArchive::new(file);
61+
if res.is_err() {
62+
fs::remove_file(&file_path)?;
63+
}
64+
let mut archive = res?;
65+
for i in 0..archive.len() {
66+
let mut inner_file = archive.by_index(i)?;
67+
let relative_path = inner_file.mangled_name();
68+
69+
if relative_path.to_string_lossy().is_empty() {
70+
continue; // Ignore root
71+
}
6072

61-
let extracted_file = out_path.join(relative_path);
62-
if inner_file.name().ends_with('/') {
63-
// Directory
64-
fs::create_dir_all(&extracted_file)?;
65-
} else {
66-
// File
67-
if let Some(p) = extracted_file.parent() {
68-
if !p.exists() {
69-
fs::create_dir_all(&p)?;
70-
}
73+
let extracted_file = out_path.join(relative_path);
74+
if inner_file.name().ends_with('/') {
75+
// Directory
76+
fs::create_dir_all(&extracted_file)?;
77+
} else {
78+
// File
79+
if let Some(p) = extracted_file.parent() {
80+
if !p.exists() {
81+
fs::create_dir_all(&p)?;
7182
}
72-
let mut outfile = fs::File::create(&extracted_file)?;
73-
io::copy(&mut inner_file, &mut outfile)?;
7483
}
84+
let mut outfile = fs::File::create(&extracted_file)?;
85+
io::copy(&mut inner_file, &mut outfile)?;
7586
}
76-
} else if file_ext == "tar.gz" {
77-
let unzipped = GzDecoder::new(file);
78-
let mut a = Archive::new(unzipped);
79-
a.unpack(&out_path)?;
8087
}
8188
}
8289

83-
Ok(out_path.join("webrtc"))
90+
Ok(out_path.join(file_name.replace(".zip", "")))
8491
}
8592

8693
fn main() {
87-
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
88-
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
89-
9094
let use_custom_webrtc = {
9195
let var = env::var("LK_CUSTOM_WEBRTC");
9296
var.is_ok() && var.unwrap() == "true"
@@ -99,8 +103,7 @@ fn main() {
99103
} else {
100104
// Download a prebuilt version of WebRTC
101105
let download_dir = env::var("OUT_DIR").unwrap() + "/webrtc-sdk";
102-
let webrtc_dir =
103-
download_prebuilt(&target_os, &target_arch, path::PathBuf::from(download_dir)).unwrap();
106+
let webrtc_dir = download_prebuilt_webrtc(path::PathBuf::from(download_dir)).unwrap();
104107

105108
(webrtc_dir.join("include"), webrtc_dir.join("lib"))
106109
};
@@ -164,7 +167,8 @@ fn main() {
164167
webrtc_lib.canonicalize().unwrap().to_str().unwrap()
165168
);
166169

167-
match &target_os as &str {
170+
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
171+
match target_os.as_str() {
168172
"windows" => {
169173
println!("cargo:rustc-link-lib=dylib=msdmo");
170174
println!("cargo:rustc-link-lib=dylib=wmcodecdspuuid");
@@ -189,7 +193,21 @@ fn main() {
189193
//.define("WEBRTC_ENABLE_SYMBOL_EXPORT", None) Not necessary when using WebRTC as a static library
190194
.define("NOMINMAX", None);
191195
}
192-
"linux" => {}
196+
"linux" => {
197+
println!("cargo:rustc-link-lib=dylib=Xext");
198+
println!("cargo:rustc-link-lib=dylib=X11");
199+
println!("cargo:rustc-link-lib=dylib=GL");
200+
println!("cargo:rustc-link-lib=dylib=rt");
201+
println!("cargo:rustc-link-lib=dylib=dl");
202+
println!("cargo:rustc-link-lib=dylib=pthread");
203+
println!("cargo:rustc-link-lib=dylib=m");
204+
println!("cargo:rustc-link-lib=static=webrtc");
205+
206+
builder
207+
.flag("-std=c++17")
208+
.define("WEBRTC_POSIX", None)
209+
.define("WEBRTC_LINUX", None);
210+
}
193211
"macos" => {
194212
println!("cargo:rustc-link-lib=framework=Foundation");
195213
println!("cargo:rustc-link-lib=framework=AVFoundation");
@@ -256,11 +274,11 @@ fn main() {
256274
);
257275
let android_ndk = path::PathBuf::from(ndk_env);
258276

259-
let host_os = if cfg!(target_os = "linux") {
277+
let host_os = if cfg!(linux) {
260278
"linux-x86_64"
261-
} else if cfg!(target_os = "macos") {
279+
} else if cfg!(macos) {
262280
"darwin-x86_64"
263-
} else if cfg!(target_os = "windows") {
281+
} else if cfg!(windows) {
264282
"windows-x86_64"
265283
} else {
266284
panic!("Unsupported host OS");

0 commit comments

Comments
 (0)