Skip to content

Commit

Permalink
Released the source on github 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
YouKnow-sys committed Mar 16, 2024
0 parents commit 88ab830
Show file tree
Hide file tree
Showing 19 changed files with 1,513 additions and 0 deletions.
55 changes: 55 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Rust CI

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

env:
CARGO_TERM_COLOR: always

jobs:
build:
name: Build on ${{ matrix.os }}.
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- os: ubuntu-latest
cibw_archs: "native"
cibw_os_build: "manylinux_"
- os: arm64-server
cibw_archs: "aarch64"
cibw_os_build: "manylinux_"
- os: windows-latest
cibw_archs: "native"
- os: macos-14
cibw_archs: "arm64"
cibw_os_build: "macosx_arm64"

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Update Linux dependencies
run: sudo apt update
if: contains(matrix.os, 'ubuntu')

- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable

- name: Install cargo-audit
run: cargo install cargo-audit

- name: Build
run: cargo build --verbose --all-features

- name: Test
run: cargo test --verbose --all-features

- name: Clippy
run: cargo clippy --verbose --all-features -- -D warnings

- name: Audit
run: cargo audit --deny warnings
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"[rust]": {
"editor.formatOnSave": true,
"editor.formatOnPaste": true,
"editor.defaultFormatter": "rust-lang.rust-analyzer",
},
}
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[workspace]
resolver = "2"
members = [ "libntgcalls-sys", "ntgcalls" ]

[workspace.package]
authors = ["Gameside <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"
keywords = ["telegram", "bot"]
repository = "https://github.com/YouKnow-sys/ntgcalls-rs"
readme = "README.md"


[profile.release]
codegen-units = 1
lto = "fat"
opt-level = "s"
strip = "symbols"
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# NTgCall-rs
A simple safe binding for NTgCalls C api.

## Todo
- [ ] Add support for linking NTgCalls lib statically when NTgCalls added it.
- [x] Add support for downloading precompiled NTgCalls lib if a feature flag is enabled instead of putting the lib directly in the project. (kinda done, but im just hardcoding a version at this point and also including a lib file)
- [ ] Clean up the code.
- [x] Copy the shared lib to output folder after build for each platform. (can get buggy if build for multiplatform)
14 changes: 14 additions & 0 deletions libntgcalls-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "libntgcalls-sys"
version = "0.1.0"
license.workspace = true
repository.workspace = true
authors.workspace = true
edition.workspace = true
readme = "README.md"

[dependencies]

[features]
default = ["bundled"]
bundled = []
5 changes: 5 additions & 0 deletions libntgcalls-sys/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# libNTgCalls-sys
build command:
```cmd
bindgen libntgcall-sys\lib\include\ntgcalls.h -o libntgcall-sys\src\bindings.rs --allowlist-var '^NTG_.*' --allowlist-function '^ntg_.*'
```
119 changes: 119 additions & 0 deletions libntgcalls-sys/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
use std::{
env,
path::{Path, PathBuf},
};

#[cfg(not(feature = "bundled"))]
compile_error!(
"For now we only support the NTgCalls bundled, so the `bundled` feature need to be enabled"
);

fn main() {
println!("cargo:rerun-if-changed=build.rs");

let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
// this can fail when building with `--target` flag...
let target_dir = PathBuf::from(format!(
"{}/target/{}",
PathBuf::from(&manifest_dir).parent().unwrap().display(),
env::var("PROFILE").unwrap()
));

println!(
"cargo:rustc-link-search={}",
Path::new(&manifest_dir).join("lib").display()
);

#[cfg(feature = "bundled")]
bundled(&out_dir, &target_dir);
}

#[cfg(feature = "bundled")]
fn bundled(out_dir: &std::path::Path, target_dir: &std::path::Path) {
use std::process::Command;

if let Ok(ntgcalls_path) = env::var("NTGCAllS_BUNDLE_DIR") {
println!("cargo:rustc-link-search={ntgcalls_path}");
} else {
assert!(
["windows", "linux", "macos"].contains(&std::env::consts::OS),
"Unsupported os (`{}`) for bundled lib.",
std::env::consts::OS
);

assert!(
["x86_64", "arm64", "aarch64"].contains(&std::env::consts::ARCH),
"Unsupported CPU architecture (`{}`) for bundled lib.",
std::env::consts::ARCH
);

let url = if let Ok(ntgcalls_url) = env::var("NTGCAllS_BUNDLE_URL") {
PathBuf::from(ntgcalls_url)
} else {
PathBuf::from(format!(
"{}/ntgcalls.{}-{}-{}.zip",
env::var("NTGCAllS_BUNDLE_URL_PREFIX").unwrap_or_else(|_| String::from(
"https://github.com/pytgcalls/ntgcalls/releases/download/v1.1.3" // we are currently hardcoding this version, this should change in future
)),
std::env::consts::OS,
if std::env::consts::ARCH == "aarch64" {
"arm64"
} else {
std::env::consts::ARCH
},
if cfg!(debug_assertions) {
"debug-shared_libs"
} else {
"shared_libs"
}
))
};

let curl_status = Command::new("curl")
.args(["-LOkf", url.to_str().unwrap()])
.current_dir(out_dir)
.status()
.expect("Curl is needed to download the bundled libraries!");

if !curl_status.success() {
panic!("Download bundled libraries from {:?} failed", url)
}

#[cfg(any(target_os = "linux", target_os = "macos"))]
let ext_status = Command::new("unzip")
.arg(url.file_name().unwrap().to_str().unwrap())
.current_dir(out_dir)
.status()
.expect("Tar is needed to upack the bundled libraries!");

#[cfg(target_os = "windows")]
let ext_status = Command::new("tar")
.args(["-xzvf", url.file_name().unwrap().to_str().unwrap()])
.current_dir(out_dir)
.status()
.expect("tar is needed to upack the bundled libraries!");

if !ext_status.success() {
panic!("Unpack bundled libraries failed")
}

println!("cargo:rustc-link-search={}", out_dir.display());
}

let file_name = if cfg!(target_os = "windows") {
"ntgcalls.dll"
} else if cfg!(target_os = "linux") {
"libntgcalls.so"
} else {
"libntgcalls.dylib"
};

std::fs::copy(
out_dir.join(file_name),
target_dir.parent().unwrap().join(file_name),
)
.expect("failed to copy lib file to target dir.");

println!("cargo:rustc-link-lib=ntgcalls");
}
139 changes: 139 additions & 0 deletions libntgcalls-sys/lib/include/ntgcalls.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
//
// Created by Laky64 on 29/08/2023.
//

#pragma once

#if defined _WIN32 || defined __CYGWIN__
#ifdef NTG_EXPORTS
#define NTG_C_EXPORT __declspec(dllexport) // building the library
#else
#define NTG_C_EXPORT __declspec(dllimport) // using the library
#endif
#else // not WIN32
#define NTG_C_EXPORT
#endif

// EXCEPTIONS CODES

// NTgCalls
#define NTG_CONNECTION_ALREADY_EXISTS (-100)
#define NTG_CONNECTION_NOT_FOUND (-101)

// STREAM
#define NTG_FILE_NOT_FOUND (-200)
#define NTG_ENCODER_NOT_FOUND (-201)
#define NTG_FFMPEG_NOT_FOUND (-202)
#define NTG_SHELL_ERROR (-203)

// WebRTC
#define NTG_RTMP_NEEDED (-300)
#define NTG_INVALID_TRANSPORT (-301)
#define NTG_CONNECTION_FAILED (-302)

// Others
#define NTG_UNKNOWN_EXCEPTION (-1)
#define NTG_INVALID_UID (-2)
#define NTG_ERR_TOO_SMALL (-3)

#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>

typedef enum {
NTG_FILE = 1 << 0,
NTG_SHELL = 1 << 1,
NTG_FFMPEG = 1 << 2,
NTG_NO_LATENCY = 1 << 3,
} ntg_input_mode_enum;

typedef enum {
NTG_STREAM_AUDIO,
NTG_STREAM_VIDEO
} ntg_stream_type_enum;

typedef enum {
NTG_PLAYING,
NTG_PAUSED,
NTG_IDLING
} ntg_stream_status_enum;

typedef struct {
ntg_input_mode_enum inputMode;
char* input;
uint32_t sampleRate;
uint8_t bitsPerSample, channelCount;
} ntg_audio_description_struct;

typedef struct {
ntg_input_mode_enum inputMode;
char* input;
uint16_t width, height;
uint8_t fps;
} ntg_video_description_struct;

typedef struct {
ntg_audio_description_struct* audio;
ntg_video_description_struct* video;
} ntg_media_description_struct;

typedef struct {
int64_t chatId;
ntg_stream_status_enum status;
} ntg_group_call_struct;

typedef struct {
bool muted;
bool videoPaused;
bool videoStopped;
} ntg_media_state_struct;

typedef void (*ntg_stream_callback)(uint32_t, int64_t, ntg_stream_type_enum);

typedef void (*ntg_upgrade_callback)(uint32_t, int64_t, ntg_media_state_struct);

typedef void (*ntg_disconnect_callback)(uint32_t, int64_t);

NTG_C_EXPORT uint32_t ntg_init();

NTG_C_EXPORT int ntg_destroy(uint32_t uid);

NTG_C_EXPORT int ntg_get_params(uint32_t uid, int64_t chatID, ntg_media_description_struct desc, char* buffer, int size);

NTG_C_EXPORT int ntg_connect(uint32_t uid, int64_t chatID, char* params);

NTG_C_EXPORT int ntg_change_stream(uint32_t uid, int64_t chatID, ntg_media_description_struct desc);

NTG_C_EXPORT int ntg_pause(uint32_t uid, int64_t chatID);

NTG_C_EXPORT int ntg_resume(uint32_t uid, int64_t chatID);

NTG_C_EXPORT int ntg_mute(uint32_t uid, int64_t chatID);

NTG_C_EXPORT int ntg_unmute(uint32_t uid, int64_t chatID);

NTG_C_EXPORT int ntg_stop(uint32_t uid, int64_t chatID);

NTG_C_EXPORT int64_t ntg_time(uint32_t uid, int64_t chatID);

NTG_C_EXPORT int ntg_get_state(uint32_t uid, int64_t chatID, ntg_media_state_struct *mediaState);

NTG_C_EXPORT int ntg_calls(uint32_t uid, ntg_group_call_struct *buffer, int size);

NTG_C_EXPORT int ntg_calls_count(uint32_t uid);

NTG_C_EXPORT int ntg_on_stream_end(uint32_t uid, ntg_stream_callback callback);

NTG_C_EXPORT int ntg_on_upgrade(uint32_t uid, ntg_upgrade_callback callback);

NTG_C_EXPORT int ntg_on_disconnect(uint32_t uid, ntg_disconnect_callback callback);

NTG_C_EXPORT int ntg_get_version(char* buffer, int size);

NTG_C_EXPORT int ntg_cpu_usage(uint32_t uid, double *buffer);

#ifdef __cplusplus
}
#endif
Binary file added libntgcalls-sys/lib/ntgcalls.lib
Binary file not shown.
Loading

0 comments on commit 88ab830

Please sign in to comment.