1
- use flate2:: read:: GzDecoder ;
2
1
use regex:: Regex ;
3
2
use std:: env;
4
3
use std:: fs;
5
4
use std:: io:: { self , Write } ;
6
5
use std:: path;
7
6
use std:: process:: Command ;
8
- use tar:: Archive ;
9
7
10
- const WEBRTC_TAG : & str = "m104.5112.09 " ;
8
+ const WEBRTC_TAG : & str = "webrtc-beb0471 " ;
11
9
12
- fn download_prebuilt (
13
- target_os : & str ,
14
- target_arch : & str ,
10
+ fn download_prebuilt_webrtc (
15
11
out_path : path:: PathBuf ,
16
12
) -> 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 ( ) {
18
21
"aarch64" => "arm64" ,
19
- _ => target_arch,
22
+ "x86_64" => "x64" ,
23
+ _ => & target_arch,
20
24
} ;
21
25
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,
26
29
} ;
27
30
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) ;
29
33
let file_url = format ! (
30
- "https://github.com/webrtc -sdk/webrtc-build /releases/download/{}/{}" ,
34
+ "https://github.com/livekit/client -sdk-rust /releases/download/{}/{}" ,
31
35
WEBRTC_TAG , file_name
32
36
) ;
33
37
let file_path = out_path. join ( & file_name) ;
@@ -41,52 +45,52 @@ fn download_prebuilt(
41
45
let file = fs:: File :: create ( & file_path) ?;
42
46
{
43
47
// 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
+ }
47
56
}
48
57
49
58
// Extract the archive
50
59
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
+ }
60
72
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) ?;
71
82
}
72
- let mut outfile = fs:: File :: create ( & extracted_file) ?;
73
- io:: copy ( & mut inner_file, & mut outfile) ?;
74
83
}
84
+ let mut outfile = fs:: File :: create ( & extracted_file) ?;
85
+ io:: copy ( & mut inner_file, & mut outfile) ?;
75
86
}
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) ?;
80
87
}
81
88
}
82
89
83
- Ok ( out_path. join ( "webrtc" ) )
90
+ Ok ( out_path. join ( file_name . replace ( ".zip" , "" ) ) )
84
91
}
85
92
86
93
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
-
90
94
let use_custom_webrtc = {
91
95
let var = env:: var ( "LK_CUSTOM_WEBRTC" ) ;
92
96
var. is_ok ( ) && var. unwrap ( ) == "true"
@@ -99,8 +103,7 @@ fn main() {
99
103
} else {
100
104
// Download a prebuilt version of WebRTC
101
105
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 ( ) ;
104
107
105
108
( webrtc_dir. join ( "include" ) , webrtc_dir. join ( "lib" ) )
106
109
} ;
@@ -164,7 +167,8 @@ fn main() {
164
167
webrtc_lib. canonicalize( ) . unwrap( ) . to_str( ) . unwrap( )
165
168
) ;
166
169
167
- match & target_os as & str {
170
+ let target_os = env:: var ( "CARGO_CFG_TARGET_OS" ) . unwrap ( ) ;
171
+ match target_os. as_str ( ) {
168
172
"windows" => {
169
173
println ! ( "cargo:rustc-link-lib=dylib=msdmo" ) ;
170
174
println ! ( "cargo:rustc-link-lib=dylib=wmcodecdspuuid" ) ;
@@ -189,7 +193,21 @@ fn main() {
189
193
//.define("WEBRTC_ENABLE_SYMBOL_EXPORT", None) Not necessary when using WebRTC as a static library
190
194
. define ( "NOMINMAX" , None ) ;
191
195
}
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
+ }
193
211
"macos" => {
194
212
println ! ( "cargo:rustc-link-lib=framework=Foundation" ) ;
195
213
println ! ( "cargo:rustc-link-lib=framework=AVFoundation" ) ;
@@ -256,11 +274,11 @@ fn main() {
256
274
) ;
257
275
let android_ndk = path:: PathBuf :: from ( ndk_env) ;
258
276
259
- let host_os = if cfg ! ( target_os = " linux" ) {
277
+ let host_os = if cfg ! ( linux) {
260
278
"linux-x86_64"
261
- } else if cfg ! ( target_os = " macos" ) {
279
+ } else if cfg ! ( macos) {
262
280
"darwin-x86_64"
263
- } else if cfg ! ( target_os = " windows" ) {
281
+ } else if cfg ! ( windows) {
264
282
"windows-x86_64"
265
283
} else {
266
284
panic ! ( "Unsupported host OS" ) ;
0 commit comments