Unverified 提交 eeaebc82 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo 提交者: GitHub

createWorker(): add libwebrtcFieldTrials option (#960)

- Bonus track: use snprintf and vsnprintf instead of sprintf and vsprintf because the latter are deprecated.
上级 d99cbc01
...@@ -64,6 +64,16 @@ export type WorkerSettings = ...@@ -64,6 +64,16 @@ export type WorkerSettings =
*/ */
dtlsPrivateKeyFile?: string; dtlsPrivateKeyFile?: string;
/**
* Field trials for libwebrtc.
* @private
*
* NOTE: For advanced users only. An invalid value will make the worker crash.
* Default value is
* "WebRTC-Bwe-AlrLimitedBackoff/Enabled/".
*/
libwebrtcFieldTrials?: string;
/** /**
* Custom application data. * Custom application data.
*/ */
...@@ -235,6 +245,7 @@ export class Worker extends EnhancedEventEmitter<WorkerEvents> ...@@ -235,6 +245,7 @@ export class Worker extends EnhancedEventEmitter<WorkerEvents>
rtcMaxPort, rtcMaxPort,
dtlsCertificateFile, dtlsCertificateFile,
dtlsPrivateKeyFile, dtlsPrivateKeyFile,
libwebrtcFieldTrials,
appData appData
}: WorkerSettings) }: WorkerSettings)
{ {
...@@ -278,6 +289,9 @@ export class Worker extends EnhancedEventEmitter<WorkerEvents> ...@@ -278,6 +289,9 @@ export class Worker extends EnhancedEventEmitter<WorkerEvents>
if (typeof dtlsPrivateKeyFile === 'string' && dtlsPrivateKeyFile) if (typeof dtlsPrivateKeyFile === 'string' && dtlsPrivateKeyFile)
spawnArgs.push(`--dtlsPrivateKeyFile=${dtlsPrivateKeyFile}`); spawnArgs.push(`--dtlsPrivateKeyFile=${dtlsPrivateKeyFile}`);
if (typeof libwebrtcFieldTrials === 'string' && libwebrtcFieldTrials)
spawnArgs.push(`--libwebrtcFieldTrials=${libwebrtcFieldTrials}`);
logger.debug( logger.debug(
'spawning worker process: %s %s', spawnBin, spawnArgs.join(' ')); 'spawning worker process: %s %s', spawnBin, spawnArgs.join(' '));
......
...@@ -46,6 +46,7 @@ export async function createWorker( ...@@ -46,6 +46,7 @@ export async function createWorker(
rtcMaxPort = 59999, rtcMaxPort = 59999,
dtlsCertificateFile, dtlsCertificateFile,
dtlsPrivateKeyFile, dtlsPrivateKeyFile,
libwebrtcFieldTrials,
appData appData
}: WorkerSettings = {} }: WorkerSettings = {}
): Promise<Worker> ): Promise<Worker>
...@@ -63,6 +64,7 @@ export async function createWorker( ...@@ -63,6 +64,7 @@ export async function createWorker(
rtcMaxPort, rtcMaxPort,
dtlsCertificateFile, dtlsCertificateFile,
dtlsPrivateKeyFile, dtlsPrivateKeyFile,
libwebrtcFieldTrials,
appData appData
}); });
......
...@@ -34,13 +34,14 @@ test('createWorker() succeeds', async () => ...@@ -34,13 +34,14 @@ test('createWorker() succeeds', async () =>
// eslint-disable-next-line require-atomic-updates // eslint-disable-next-line require-atomic-updates
worker = await createWorker( worker = await createWorker(
{ {
logLevel : 'debug', logLevel : 'debug',
logTags : [ 'info' ], logTags : [ 'info' ],
rtcMinPort : 0, rtcMinPort : 0,
rtcMaxPort : 9999, rtcMaxPort : 9999,
dtlsCertificateFile : path.join(__dirname, 'data', 'dtls-cert.pem'), dtlsCertificateFile : path.join(__dirname, 'data', 'dtls-cert.pem'),
dtlsPrivateKeyFile : path.join(__dirname, 'data', 'dtls-key.pem'), dtlsPrivateKeyFile : path.join(__dirname, 'data', 'dtls-key.pem'),
appData : { bar: 456 } libwebrtcFieldTrials : 'WebRTC-Bwe-AlrLimitedBackoff/Disabled/',
appData : { bar: 456 }
}); });
expect(worker.constructor.name).toBe('Worker'); expect(worker.constructor.name).toBe('Worker');
expect(typeof worker.pid).toBe('number'); expect(typeof worker.pid).toBe('number');
......
...@@ -182,6 +182,13 @@ pub struct WorkerSettings { ...@@ -182,6 +182,13 @@ pub struct WorkerSettings {
/// ///
/// If `None`, a certificate is dynamically created. /// If `None`, a certificate is dynamically created.
pub dtls_files: Option<WorkerDtlsFiles>, pub dtls_files: Option<WorkerDtlsFiles>,
/// Field trials for libwebrtc.
///
/// NOTE: For advanced users only. An invalid value will make the worker crash.
/// Default value is
/// "WebRTC-Bwe-AlrLimitedBackoff/Enabled/".
#[doc(hidden)]
pub libwebrtc_field_trials: Option<String>,
/// Function that will be called under worker thread before worker starts, can be used for /// Function that will be called under worker thread before worker starts, can be used for
/// pinning worker threads to CPU cores. /// pinning worker threads to CPU cores.
pub thread_initializer: Option<Arc<dyn Fn() + Send + Sync>>, pub thread_initializer: Option<Arc<dyn Fn() + Send + Sync>>,
...@@ -210,6 +217,7 @@ impl Default for WorkerSettings { ...@@ -210,6 +217,7 @@ impl Default for WorkerSettings {
], ],
rtc_ports_range: 10000..=59999, rtc_ports_range: 10000..=59999,
dtls_files: None, dtls_files: None,
libwebrtc_field_trials: None,
thread_initializer: None, thread_initializer: None,
app_data: AppData::default(), app_data: AppData::default(),
} }
...@@ -223,6 +231,7 @@ impl fmt::Debug for WorkerSettings { ...@@ -223,6 +231,7 @@ impl fmt::Debug for WorkerSettings {
log_tags, log_tags,
rtc_ports_range, rtc_ports_range,
dtls_files, dtls_files,
libwebrtc_field_trials,
thread_initializer, thread_initializer,
app_data, app_data,
} = self; } = self;
...@@ -232,6 +241,7 @@ impl fmt::Debug for WorkerSettings { ...@@ -232,6 +241,7 @@ impl fmt::Debug for WorkerSettings {
.field("log_tags", &log_tags) .field("log_tags", &log_tags)
.field("rtc_ports_range", &rtc_ports_range) .field("rtc_ports_range", &rtc_ports_range)
.field("dtls_files", &dtls_files) .field("dtls_files", &dtls_files)
.field("libwebrtc_field_trials", &libwebrtc_field_trials)
.field( .field(
"thread_initializer", "thread_initializer",
&thread_initializer.as_ref().map(|_| "ThreadInitializer"), &thread_initializer.as_ref().map(|_| "ThreadInitializer"),
...@@ -335,6 +345,7 @@ impl Inner { ...@@ -335,6 +345,7 @@ impl Inner {
log_tags, log_tags,
rtc_ports_range, rtc_ports_range,
dtls_files, dtls_files,
libwebrtc_field_trials,
thread_initializer, thread_initializer,
app_data, app_data,
}: WorkerSettings, }: WorkerSettings,
...@@ -376,6 +387,13 @@ impl Inner { ...@@ -376,6 +387,13 @@ impl Inner {
)); ));
} }
if let Some(libwebrtc_field_trials) = libwebrtc_field_trials {
spawn_args.push(format!(
"--libwebrtcFieldTrials={}",
libwebrtc_field_trials.as_str()
));
}
let id = WorkerId::new(); let id = WorkerId::new();
debug!( debug!(
"spawning worker with arguments [id:{}]: {}", "spawning worker with arguments [id:{}]: {}",
......
...@@ -47,6 +47,8 @@ fn create_worker_succeeds() { ...@@ -47,6 +47,8 @@ fn create_worker_succeeds() {
certificate: "tests/integration/data/dtls-cert.pem".into(), certificate: "tests/integration/data/dtls-cert.pem".into(),
private_key: "tests/integration/data/dtls-key.pem".into(), private_key: "tests/integration/data/dtls-key.pem".into(),
}); });
settings.libwebrtc_field_trials =
Some("WebRTC-Bwe-AlrLimitedBackoff/Disabled/".to_string());
settings.app_data = AppData::new(CustomAppData { bar: 456 }); settings.app_data = AppData::new(CustomAppData { bar: 456 });
settings settings
......
...@@ -38,6 +38,7 @@ public: ...@@ -38,6 +38,7 @@ public:
uint16_t rtcMaxPort{ 59999u }; uint16_t rtcMaxPort{ 59999u };
std::string dtlsCertificateFile; std::string dtlsCertificateFile;
std::string dtlsPrivateKeyFile; std::string dtlsPrivateKeyFile;
std::string libwebrtcFieldTrials{ "WebRTC-Bwe-AlrLimitedBackoff/Enabled/" };
}; };
public: public:
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#include "Logger.hpp" #include "Logger.hpp"
#include "MediaSoupErrors.hpp" #include "MediaSoupErrors.hpp"
#include <cmath> // std::ceil() #include <cmath> // std::ceil()
#include <cstdio> // sprintf()
#include <cstring> // std::memcpy(), std::memmove() #include <cstring> // std::memcpy(), std::memmove()
namespace Channel namespace Channel
......
...@@ -3,13 +3,13 @@ ...@@ -3,13 +3,13 @@
#include "DepLibWebRTC.hpp" #include "DepLibWebRTC.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include "Settings.hpp"
#include "system_wrappers/source/field_trial.h" // webrtc::field_trial #include "system_wrappers/source/field_trial.h" // webrtc::field_trial
#include <mutex> #include <mutex>
/* Static. */ /* Static. */
static std::once_flag globalInitOnce; static std::once_flag globalInitOnce;
static constexpr char FieldTrials[] = "WebRTC-Bwe-AlrLimitedBackoff/Enabled/";
/* Static methods. */ /* Static methods. */
...@@ -17,7 +17,16 @@ void DepLibWebRTC::ClassInit() ...@@ -17,7 +17,16 @@ void DepLibWebRTC::ClassInit()
{ {
MS_TRACE(); MS_TRACE();
std::call_once(globalInitOnce, [] { webrtc::field_trial::InitFieldTrialsFromString(FieldTrials); }); MS_DEBUG_TAG(
info, "libwebrtc field trials: \"%s\"", Settings::configuration.libwebrtcFieldTrials.c_str());
std::call_once(
globalInitOnce,
[]
{
webrtc::field_trial::InitFieldTrialsFromString(
Settings::configuration.libwebrtcFieldTrials.c_str());
});
} }
void DepLibWebRTC::ClassDestroy() void DepLibWebRTC::ClassDestroy()
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "DepLibUV.hpp" #include "DepLibUV.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include <usrsctp.h> #include <usrsctp.h>
#include <cstdio> // std::vsnprintf()
#include <mutex> #include <mutex>
/* Static. */ /* Static. */
...@@ -40,7 +41,7 @@ inline static void sctpDebug(const char* format, ...) ...@@ -40,7 +41,7 @@ inline static void sctpDebug(const char* format, ...)
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
vsprintf(buffer, format, ap); vsnprintf(buffer, sizeof(buffer), format, ap);
// Remove the artificial carriage return set by usrsctp. // Remove the artificial carriage return set by usrsctp.
buffer[std::strlen(buffer) - 1] = '\0'; buffer[std::strlen(buffer) - 1] = '\0';
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include "MediaSoupErrors.hpp" #include "MediaSoupErrors.hpp"
#include "PayloadChannel/PayloadChannelRequest.hpp" #include "PayloadChannel/PayloadChannelRequest.hpp"
#include <cmath> // std::ceil() #include <cmath> // std::ceil()
#include <cstdio> // sprintf()
#include <cstring> // std::memcpy(), std::memmove() #include <cstring> // std::memcpy(), std::memmove()
namespace PayloadChannel namespace PayloadChannel
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include <openssl/err.h> #include <openssl/err.h>
#include <openssl/evp.h> #include <openssl/evp.h>
#include <uv.h> #include <uv.h>
#include <cstdio> // std::sprintf(), std::fopen() #include <cstdio> // std::snprintf(), std::fopen()
#include <cstring> // std::memcpy(), std::strcmp() #include <cstring> // std::memcpy(), std::strcmp()
#define LOG_OPENSSL_ERROR(desc) \ #define LOG_OPENSSL_ERROR(desc) \
...@@ -479,7 +479,7 @@ namespace RTC ...@@ -479,7 +479,7 @@ namespace RTC
// Convert to hexadecimal format in uppercase with colons. // Convert to hexadecimal format in uppercase with colons.
for (unsigned int i{ 0 }; i < size; ++i) for (unsigned int i{ 0 }; i < size; ++i)
{ {
std::sprintf(hexFingerprint + (i * 3), "%.2X:", binaryFingerprint[i]); std::snprintf(hexFingerprint + (i * 3), 4, "%.2X:", binaryFingerprint[i]);
} }
hexFingerprint[(size * 3) - 1] = '\0'; hexFingerprint[(size * 3) - 1] = '\0';
...@@ -1134,7 +1134,7 @@ namespace RTC ...@@ -1134,7 +1134,7 @@ namespace RTC
// Convert to hexadecimal format in uppercase with colons. // Convert to hexadecimal format in uppercase with colons.
for (unsigned int i{ 0 }; i < size; ++i) for (unsigned int i{ 0 }; i < size; ++i)
{ {
std::sprintf(hexFingerprint + (i * 3), "%.2X:", binaryFingerprint[i]); std::snprintf(hexFingerprint + (i * 3), 4, "%.2X:", binaryFingerprint[i]);
} }
hexFingerprint[(size * 3) - 1] = '\0'; hexFingerprint[(size * 3) - 1] = '\0';
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "DepUsrSCTP.hpp" #include "DepUsrSCTP.hpp"
#include "Logger.hpp" #include "Logger.hpp"
#include "MediaSoupErrors.hpp" #include "MediaSoupErrors.hpp"
#include <cstdio> // std::snprintf()
#include <cstdlib> // std::malloc(), std::free() #include <cstdlib> // std::malloc(), std::free()
#include <cstring> // std::memset(), std::memcpy() #include <cstring> // std::memset(), std::memcpy()
#include <string> #include <string>
......
...@@ -53,12 +53,13 @@ void Settings::SetConfiguration(int argc, char* argv[]) ...@@ -53,12 +53,13 @@ void Settings::SetConfiguration(int argc, char* argv[])
// clang-format off // clang-format off
struct option options[] = struct option options[] =
{ {
{ "logLevel", optional_argument, nullptr, 'l' }, { "logLevel", optional_argument, nullptr, 'l' },
{ "logTags", optional_argument, nullptr, 't' }, { "logTags", optional_argument, nullptr, 't' },
{ "rtcMinPort", optional_argument, nullptr, 'm' }, { "rtcMinPort", optional_argument, nullptr, 'm' },
{ "rtcMaxPort", optional_argument, nullptr, 'M' }, { "rtcMaxPort", optional_argument, nullptr, 'M' },
{ "dtlsCertificateFile", optional_argument, nullptr, 'c' }, { "dtlsCertificateFile", optional_argument, nullptr, 'c' },
{ "dtlsPrivateKeyFile", optional_argument, nullptr, 'p' }, { "dtlsPrivateKeyFile", optional_argument, nullptr, 'p' },
{ "libwebrtcFieldTrials", optional_argument, nullptr, 'W' },
{ nullptr, 0, nullptr, 0 } { nullptr, 0, nullptr, 0 }
}; };
// clang-format on // clang-format on
...@@ -139,6 +140,22 @@ void Settings::SetConfiguration(int argc, char* argv[]) ...@@ -139,6 +140,22 @@ void Settings::SetConfiguration(int argc, char* argv[])
break; break;
} }
case 'W':
{
stringValue = std::string(optarg);
if (stringValue != Settings::configuration.libwebrtcFieldTrials)
{
MS_WARN_TAG(
info,
"overriding default value of libwebrtcFieldTrials may generate crashes in mediasoup-worker");
Settings::configuration.libwebrtcFieldTrials = stringValue;
}
break;
}
// Invalid option. // Invalid option.
case '?': case '?':
{ {
...@@ -221,17 +238,22 @@ void Settings::PrintConfiguration() ...@@ -221,17 +238,22 @@ void Settings::PrintConfiguration()
MS_DEBUG_TAG( MS_DEBUG_TAG(
info, info,
" logLevel : %s", " logLevel : %s",
Settings::logLevel2String[Settings::configuration.logLevel].c_str()); Settings::logLevel2String[Settings::configuration.logLevel].c_str());
MS_DEBUG_TAG(info, " logTags : %s", logTagsStream.str().c_str()); MS_DEBUG_TAG(info, " logTags : %s", logTagsStream.str().c_str());
MS_DEBUG_TAG(info, " rtcMinPort : %" PRIu16, Settings::configuration.rtcMinPort); MS_DEBUG_TAG(info, " rtcMinPort : %" PRIu16, Settings::configuration.rtcMinPort);
MS_DEBUG_TAG(info, " rtcMaxPort : %" PRIu16, Settings::configuration.rtcMaxPort); MS_DEBUG_TAG(info, " rtcMaxPort : %" PRIu16, Settings::configuration.rtcMaxPort);
if (!Settings::configuration.dtlsCertificateFile.empty()) if (!Settings::configuration.dtlsCertificateFile.empty())
{ {
MS_DEBUG_TAG( MS_DEBUG_TAG(
info, " dtlsCertificateFile : %s", Settings::configuration.dtlsCertificateFile.c_str()); info, " dtlsCertificateFile : %s", Settings::configuration.dtlsCertificateFile.c_str());
MS_DEBUG_TAG(
info, " dtlsPrivateKeyFile : %s", Settings::configuration.dtlsPrivateKeyFile.c_str());
}
if (!Settings::configuration.libwebrtcFieldTrials.empty())
{
MS_DEBUG_TAG( MS_DEBUG_TAG(
info, " dtlsPrivateKeyFile : %s", Settings::configuration.dtlsPrivateKeyFile.c_str()); info, " libwebrtcFieldTrials : %s", Settings::configuration.libwebrtcFieldTrials.c_str());
} }
MS_DEBUG_TAG(info, "</configuration>"); MS_DEBUG_TAG(info, "</configuration>");
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论