Skip to content

Commit

Permalink
sora_client と environment に対応
Browse files Browse the repository at this point in the history
  • Loading branch information
melpon committed Nov 17, 2023
1 parent f4c1696 commit 50271b6
Show file tree
Hide file tree
Showing 11 changed files with 457 additions and 54 deletions.
6 changes: 4 additions & 2 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
"${workspaceFolder}/_source/libdatachannel/deps/plog/include",
"${workspaceFolder}/_source/opus/include",

"${workspaceFolder}/_build/ubuntu-20.04_x86_64/release/sorac",
"${workspaceFolder}/_build/ubuntu-20.04_x86_64/release/sorac/proto/sorac",
"${workspaceFolder}/_install/ubuntu-20.04_x86_64/release/mbedtls/include",
"${workspaceFolder}/_install/ubuntu-20.04_x86_64/release/openh264/include",
"${workspaceFolder}/_build/ubuntu-20.04_x86_64/release/sorac/proto/sorac",
"${workspaceFolder}/_install/ubuntu-20.04_x86_64/release/libjpeg-turbo/include",
"${workspaceFolder}/_install/ubuntu-20.04_x86_64/release/libyuv/include",

"${workspaceFolder}/_build/macos_arm64/release/sorac",
"${workspaceFolder}/_build/macos_arm64/release/sorac/proto/sorac",
"${workspaceFolder}/_install/macos_arm64/release/mbedtls/include",
"${workspaceFolder}/_install/macos_arm64/release/openh264/include",
"${workspaceFolder}/_build/macos_arm64/release/sorac/proto/sorac",
"${workspaceFolder}/_install/macos_arm64/release/libjpeg-turbo/include",
"${workspaceFolder}/_install/macos_arm64/release/libyuv/include"
],
Expand Down
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ endfunction()

add_library(sorac STATIC)

configure_file(src/version.gen.h.template ${CMAKE_CURRENT_BINARY_DIR}/version.gen.h)
target_include_directories(sorac PRIVATE ${CMAKE_CURRENT_BINARY_DIR})

file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/proto")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/proto/sorac")
if (WIN32)
Expand Down Expand Up @@ -92,8 +95,10 @@ target_sources(sorac
src/open_h264_video_encoder.cpp
src/opus_audio_encoder.cpp
src/signaling.cpp
src/types.cpp
src/sorac.cpp
src/types.cpp
src/util.cpp
src/version.cpp
PUBLIC
FILE_SET HEADERS
BASE_DIRS
Expand Down Expand Up @@ -146,9 +151,12 @@ target_link_libraries(sorac
)

if (SORAC_TARGET STREQUAL "macos_arm64")
enable_language(OBJCXX)
target_link_options(sumomo PRIVATE -ObjC)
target_sources(sorac
PRIVATE
src/vt_h26x_video_encoder.cpp
src/mac_version.mm
PUBLIC
FILE_SET HEADERS
BASE_DIRS
Expand Down
16 changes: 16 additions & 0 deletions include/sorac/version.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef SORAC_VERSION_HPP_
#define SORAC_VERSION_HPP_

#include <string>

namespace sorac {

class Version {
public:
static std::string GetClientName();
static std::string GetEnvironment();
};

} // namespace sorac

#endif
70 changes: 62 additions & 8 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,64 @@ def install_deps(target_platform: str, build_platform: str, source_dir, shared_s
install_libyuv(**install_libyuv_args)


class LibVersion(object):
sora_c_sdk: str
sora_c_sdk_commit: str
libdatachannel: str
opus: str
mbedtls: str
nlohmann_json: str
libjuice: str
libsrtp: str
plog: str
usrsctp: str

def to_cmake(self):
return [f'-DSORA_C_SDK_VERSION={self.sora_c_sdk}',
f'-DSORA_C_SDK_COMMIT={self.sora_c_sdk_commit}',
f'-DLIBDATACHANNEL_VERSION={self.libdatachannel}',
f'-DOPUS_VERSION={self.opus}',
f'-DMBEDTLS_VERSION={self.mbedtls}',
f'-DNLOHMANN_JSON_VERSION={self.nlohmann_json}',
f'-DLIBJUICE_VERSION={self.libjuice}',
f'-DLIBSRTP_VERSION={self.libsrtp}',
f'-DPLOG_VERSION={self.plog}',
f'-DUSRSCTP_VERSION={self.usrsctp}']

@staticmethod
def create(version, base_dir, libdatachannel_dir):
libv = LibVersion()
with cd(base_dir):
libv.sora_c_sdk_commit = cmdcap(['git', 'rev-parse', 'HEAD'])
with cd(libdatachannel_dir):
# 以下のような出力が得られるので、ここから必要な部分を取り出す
# bc889afb4c5bf1c0d8ee29ef35eaaf4c8bef8a5d deps/json (bc889afb)
# 5f753cad49059cea4eb492eb5c11a3bbb4dd6324 deps/libjuice (v1.3.3)
# a566a9cfcd619e8327784aa7cff4a1276dc1e895 deps/libsrtp (a566a9c)
# e21baecd4753f14da64ede979c5a19302618b752 deps/plog (e21baec)
# 5ca29ac7d8055802c7657191325c06386640ac24 deps/usrsctp (5ca29ac)
r = cmdcap(['git', 'submodule', 'status'])
lines = r.split('\n')
for line in lines:
name, commit = line.strip().split(' ')[1:3]
commit = commit.strip('()')
if '/json' in name:
libv.nlohmann_json = commit
elif '/libjuice' in name:
libv.libjuice = commit
elif '/libsrtp' in name:
libv.libsrtp = commit
elif '/plog' in name:
libv.plog = commit
elif '/usrsctp' in name:
libv.usrsctp = commit
libv.sora_c_sdk = version['SORA_C_SDK_VERSION']
libv.libdatachannel = version['LIBDATACHANNEL_VERSION']
libv.opus = version['OPUS_VERSION']
libv.mbedtls = version['MBEDTLS_VERSION']
return libv


AVAILABLE_TARGETS = ['windows_x86_64', 'macos_x86_64', 'macos_arm64', 'ubuntu-20.04_x86_64',
'ubuntu-22.04_x86_64', 'ios', 'android']

Expand Down Expand Up @@ -685,16 +743,12 @@ def main():
cmake_args = []
cmake_args.append(f'-DCMAKE_BUILD_TYPE={configuration}')
cmake_args.append(f"-DCMAKE_INSTALL_PREFIX={cmake_path(os.path.join(install_dir, 'sorac'))}")
cmake_args.append(f"-DSORAC_TARGET={target_platform}")
libver = LibVersion.create(read_version_file(os.path.join(BASE_DIR, 'VERSION')),
BASE_DIR, os.path.join(shared_source_dir, 'libdatachannel'))
cmake_args += libver.to_cmake()
cmake_args.append(f"-DPROTOBUF_DIR={cmake_path(os.path.join(install_dir, 'protobuf'))}")
cmake_args.append(f"-DPROTOC_GEN_JSONIF_DIR={cmake_path(os.path.join(install_dir, 'protoc-gen-jsonif'))}")
with cd(BASE_DIR):
version = read_version_file('VERSION')
sora_c_sdk_version = version['SORA_C_SDK_VERSION']
sora_c_sdk_commit = cmdcap(['git', 'rev-parse', 'HEAD'])
# android_native_api_level = version['ANDROID_NATIVE_API_LEVEL']
cmake_args.append(f"-DSORAC_VERSION={sora_c_sdk_version}")
cmake_args.append(f"-DSORAC_COMMIT={sora_c_sdk_commit}")
cmake_args.append(f"-DSORAC_TARGET={target_platform}")
if target_platform in ('macos_x86_64', 'macos_arm64'):
sysroot = cmdcap(['xcrun', '--sdk', 'macosx', '--show-sdk-path'])
target = 'x86_64-apple-darwin' if target_platform in ('macos_x86_64',) else 'aarch64-apple-darwin'
Expand Down
16 changes: 16 additions & 0 deletions src/mac_version.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef SORAC_MAC_VERSION_HPP_
#define SORAC_MAC_VERSION_HPP_

#include <string>

namespace sorac {

class MacVersion {
public:
static std::string GetOSName();
static std::string GetOSVersion();
};

} // namespace sorac

#endif
95 changes: 95 additions & 0 deletions src/mac_version.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "mac_version.h"

#import <Foundation/Foundation.h>
#include <TargetConditionals.h>

// TARGET_OS_* から OS 名を調べる。
// アーキテクチャもマクロから分かるけど、それは実行時に uname を使って調べるので不要

// 以下の情報は /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/TargetConditionals.h からのコピペ

/****************************************************************************************************
TARGET_CPU_*
These conditionals specify which microprocessor instruction set is being
generated. At most one of these is true, the rest are false.
TARGET_CPU_PPC - Compiler is generating PowerPC instructions for 32-bit mode
TARGET_CPU_PPC64 - Compiler is generating PowerPC instructions for 64-bit mode
TARGET_CPU_68K - Compiler is generating 680x0 instructions
TARGET_CPU_X86 - Compiler is generating x86 instructions for 32-bit mode
TARGET_CPU_X86_64 - Compiler is generating x86 instructions for 64-bit mode
TARGET_CPU_ARM - Compiler is generating ARM instructions for 32-bit mode
TARGET_CPU_ARM64 - Compiler is generating ARM instructions for 64-bit mode
TARGET_CPU_MIPS - Compiler is generating MIPS instructions
TARGET_CPU_SPARC - Compiler is generating Sparc instructions
TARGET_CPU_ALPHA - Compiler is generating Dec Alpha instructions
TARGET_OS_*
These conditionals specify in which Operating System the generated code will
run. Indention is used to show which conditionals are evolutionary subclasses.
The MAC/WIN32/UNIX conditionals are mutually exclusive.
The IOS/TV/WATCH conditionals are mutually exclusive.
TARGET_OS_WIN32 - Generated code will run under 32-bit Windows
TARGET_OS_UNIX - Generated code will run under some Unix (not OSX)
TARGET_OS_MAC - Generated code will run under Mac OS X variant
TARGET_OS_OSX - Generated code will run under OS X devices
TARGET_OS_IPHONE - Generated code for firmware, devices, or simulator
TARGET_OS_IOS - Generated code will run under iOS
TARGET_OS_TV - Generated code will run under Apple TV OS
TARGET_OS_WATCH - Generated code will run under Apple Watch OS
TARGET_OS_BRIDGE - Generated code will run under Bridge devices
TARGET_OS_MACCATALYST - Generated code will run under macOS
TARGET_OS_SIMULATOR - Generated code will run under a simulator
TARGET_OS_EMBEDDED - DEPRECATED: Use TARGET_OS_IPHONE and/or TARGET_OS_SIMULATOR instead
TARGET_IPHONE_SIMULATOR - DEPRECATED: Same as TARGET_OS_SIMULATOR
TARGET_OS_NANO - DEPRECATED: Same as TARGET_OS_WATCH
+----------------------------------------------------------------+
| TARGET_OS_MAC |
| +---+ +-----------------------------------------------------+ |
| | | | TARGET_OS_IPHONE | |
| |OSX| | +-----+ +----+ +-------+ +--------+ +-------------+ | |
| | | | | IOS | | TV | | WATCH | | BRIDGE | | MACCATALYST | | |
| | | | +-----+ +----+ +-------+ +--------+ +-------------+ | |
| +---+ +-----------------------------------------------------+ |
+----------------------------------------------------------------+
TARGET_RT_*
These conditionals specify in which runtime the generated code will
run. This is needed when the OS and CPU support more than one runtime
(e.g. Mac OS X supports CFM and mach-o).
TARGET_RT_LITTLE_ENDIAN - Generated code uses little endian format for integers
TARGET_RT_BIG_ENDIAN - Generated code uses big endian format for integers
TARGET_RT_64_BIT - Generated code uses 64-bit pointers
TARGET_RT_MAC_CFM - TARGET_OS_MAC is true and CFM68K or PowerPC CFM (TVectors) are used
TARGET_RT_MAC_MACHO - TARGET_OS_MAC is true and Mach-O/dlyd runtime is used
****************************************************************************************************/

namespace sorac {

std::string MacVersion::GetOSName() {
#if TARGET_OS_MAC
return "macOS";
#elif TARGET_OS_IPHONE
return "iPhone";
#else
return "Unknown OS";
#endif
}

std::string MacVersion::GetOSVersion() {
// "Version 10.8.2 (Build 12C60)" みたいな文字列を取得できる
NSString* str = NSProcessInfo.processInfo.operatingSystemVersionString;
return [str UTF8String];
}

}
48 changes: 5 additions & 43 deletions src/signaling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
#include "sorac/current_time.hpp"
#include "sorac/open_h264_video_encoder.hpp"
#include "sorac/opus_audio_encoder.hpp"
#include "sorac/version.hpp"

#if defined(__APPLE__)
#include "sorac/vt_h26x_video_encoder.hpp"
#endif

#include "util.hpp"

// https://github.com/paullouisageneau/libdatachannel/issues/990
namespace rtc {
using ::operator<<;
Expand Down Expand Up @@ -48,49 +51,6 @@ struct Client {
std::map<std::string, std::shared_ptr<rtc::DataChannel>> dcs;
};

static std::string generate_random_string(int length, std::string pattern) {
if (pattern.size() == 0) {
return "";
}

std::random_device random;
// % を計算する時にマイナス値があると危険なので unsigned 型であることを保証する
typedef std::make_unsigned<std::random_device::result_type>::type
unsigned_type;
std::string r;
for (int i = 0; i < length; i++) {
r += pattern[(unsigned_type)random() % pattern.size()];
}
return r;
}

static std::string generate_random_string(int length) {
return generate_random_string(
length, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
}

static std::vector<std::string> split_with(const std::string& str,
const std::string& token) {
int sp = 0;
std::vector<std::string> lines;
while (true) {
auto ep = str.find(token, sp);
if (ep == std::string::npos) {
if (str.size() - sp > 0) {
lines.push_back(str.substr(sp));
}
break;
}
lines.push_back(str.substr(sp, ep - sp));
sp = ep + token.size();
}
return lines;
}

static bool starts_with(const std::string& str, const std::string& s) {
return str.substr(0, s.size()) == s;
}

class SignalingImpl : public Signaling {
public:
SignalingImpl(const soracp::SignalingConfig& config) : config_(config) {}
Expand Down Expand Up @@ -486,6 +446,8 @@ class SignalingImpl : public Signaling {
{"type", "connect"},
{"role", sc.role},
{"channel_id", sc.channel_id},
{"sora_client", Version::GetClientName()},
{"environment", Version::GetEnvironment()},
};
auto set_if = [](nlohmann::json& js, const std::string& key, auto value,
bool cond) {
Expand Down
Loading

0 comments on commit 50271b6

Please sign in to comment.