Skip to content

Commit

Permalink
Merge pull request #108 from shiguredo/feature/add-forwarding-filters
Browse files Browse the repository at this point in the history
マルチ転送フィルターの追加
  • Loading branch information
torikizi authored Dec 25, 2024
2 parents 6bc7219 + 5fc1286 commit f45226a
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 60 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
- @torikizi
- [CHANGE] CMakefile の依存から libva と libdrm を削除する
- @torikizi
- [CHANGE] ForwardingFilter は非推奨であるため Obsolete を追加
- 今後は ForwardingFilters を利用するように促すメッセージを追加
- @torikizi
- [UPDATE] Sora C++ SDK を `2024.8.0` に上げる
- WEBRTC_BUILD_VERSION を `m131.6778.4.0` にアップデート
- BOOST_VERSION を 1.87.0 にアップデート
Expand All @@ -28,6 +31,10 @@
- @torikizi
- [ADD] `Sora.Config``ClientCert`, `ClientKey`, `CACert` を追加
- @melpon
- [ADD] ForwardingFilter に name と priority を追加
- @torikizi
- [ADD] ForwardingFilters 機能を使えるようにする
- @torikizi

### misc

Expand Down
88 changes: 56 additions & 32 deletions Sora/Sora.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ public class DataChannel
public const string FieldKind = "kind";
public const string OperatorIsIn = "is_in";
public const string OperatorIsNotIn = "is_not_in";

public class ForwardingFilter
{
public string? Action;
public string? Name;
public int? Priority;
public class Rule
{
public string Field;
Expand All @@ -85,7 +86,6 @@ public class Rule
public string? Version;
public string? Metadata;
}

/// <summary>
/// カメラの設定
/// </summary>
Expand Down Expand Up @@ -234,8 +234,9 @@ public class Config
public string ProxyPassword = "";
// Proxy サーバーに接続するときの User-Agent。未設定ならデフォルト値が使われる
public string ProxyAgent = "";

[System.Obsolete("forwardingFilter は非推奨です。代わりに forwardingFilters を使用してください。")]
public ForwardingFilter ForwardingFilter;
public List<ForwardingFilter> ForwardingFilters;

// ハードウェアエンコーダー/デコーダーを利用するかどうか。null の場合は実装依存となる
public bool? UseHardwareEncoder;
Expand Down Expand Up @@ -461,36 +462,17 @@ public void Connect(Config config)
cc.proxy_agent = config.ProxyAgent;
if (config.ForwardingFilter != null)
{
var ff = new SoraConf.Internal.ForwardingFilter();
if (config.ForwardingFilter.Action != null)
{
ff.SetAction(config.ForwardingFilter.Action);
}
foreach (var rs in config.ForwardingFilter.Rules)
{
var ccrs = new SoraConf.Internal.ForwardingFilter.Rules();
foreach (var r in rs)
{
var ccr = new SoraConf.Internal.ForwardingFilter.Rule();
ccr.field = r.Field;
ccr.op = r.Operator;
foreach (var v in r.Values)
{
ccr.values.Add(v);
}
ccrs.rules.Add(ccr);
}
ff.rules.Add(ccrs);
}
if (config.ForwardingFilter.Version != null)
{
ff.SetVersion(config.ForwardingFilter.Version);
}
if (config.ForwardingFilter.Metadata != null)
cc.SetForwardingFilter(ConvertToInternalForwardingFilter(config.ForwardingFilter));
}

if (config.ForwardingFilters != null)
{
var ffs = new SoraConf.Internal.ForwardingFilters();
foreach (var filter in config.ForwardingFilters)
{
ff.SetMetadata(config.ForwardingFilter.Metadata);
ffs.filters.Add(ConvertToInternalForwardingFilter(filter));
}
cc.SetForwardingFilter(ff);
cc.SetForwardingFilters(ffs);
}
if (config.UseHardwareEncoder.HasValue)
{
Expand Down Expand Up @@ -522,6 +504,48 @@ public void Disconnect()
sora_disconnect(p);
}

static SoraConf.Internal.ForwardingFilter ConvertToInternalForwardingFilter(ForwardingFilter filter)
{
var ff = new SoraConf.Internal.ForwardingFilter();
if (filter.Action != null)
{
ff.SetAction(filter.Action);
}
if (filter.Name != null)
{
ff.SetName(filter.Name);
}
if (filter.Priority.HasValue)
{
ff.SetPriority(filter.Priority.Value);
}
foreach (var rs in filter.Rules)
{
var ccrs = new SoraConf.Internal.ForwardingFilter.Rules();
foreach (var r in rs)
{
var ccr = new SoraConf.Internal.ForwardingFilter.Rule();
ccr.field = r.Field;
ccr.op = r.Operator;
foreach (var v in r.Values)
{
ccr.values.Add(v);
}
ccrs.rules.Add(ccr);
}
ff.rules.Add(ccrs);
}
if (filter.Version != null)
{
ff.SetVersion(filter.Version);
}
if (filter.Metadata != null)
{
ff.SetMetadata(filter.Metadata);
}
return ff;
}

/// <summary>
/// カメラを切り替えます。
/// </summary>
Expand Down Expand Up @@ -1248,4 +1272,4 @@ public static IAudioOutputHelper Create(Action onChangeRoute)
#endif
}
}
}
}
7 changes: 7 additions & 0 deletions proto/sora_conf_internal.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ message DataChannel {

message ForwardingFilter {
optional string action = 2;
optional string name = 4;
optional int32 priority = 6;
message Rule {
string field = 1;
string op = 2;
Expand All @@ -27,6 +29,10 @@ message ForwardingFilter {
optional string metadata = 7;
}

message ForwardingFilters {
repeated ForwardingFilter filters = 1;
}

message CameraConfig {
int32 capturer_type = 17;
int64 unity_camera_texture = 18;
Expand Down Expand Up @@ -80,6 +86,7 @@ message ConnectConfig {
string audio_streaming_language_code = 48;
string signaling_notify_metadata = 49;
optional ForwardingFilter forwarding_filter = 51;
optional ForwardingFilters forwarding_filters = 52;
optional bool use_hardware_encoder = 53;
optional string client_cert = 54;
optional string client_key = 55;
Expand Down
78 changes: 50 additions & 28 deletions src/sora.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,35 +447,15 @@ void Sora::DoConnect(const sora_conf::internal::ConnectConfig& cc,
config.proxy_agent = cc.proxy_agent;
config.audio_streaming_language_code = cc.audio_streaming_language_code;
if (cc.has_forwarding_filter()) {
sora::SoraSignalingConfig::ForwardingFilter ff;
if (cc.forwarding_filter.has_action()) {
ff.action = cc.forwarding_filter.action;
}
for (const auto& rs : cc.forwarding_filter.rules) {
std::vector<sora::SoraSignalingConfig::ForwardingFilter::Rule> ffrs;
for (const auto& r : rs.rules) {
sora::SoraSignalingConfig::ForwardingFilter::Rule ffr;
ffr.field = r.field;
ffr.op = r.op;
ffr.values = r.values;
ffrs.push_back(ffr);
}
ff.rules.push_back(ffrs);
}
if (cc.forwarding_filter.has_version()) {
ff.version = cc.forwarding_filter.version;
}
if (cc.forwarding_filter.has_metadata()) {
boost::system::error_code ec;
auto ffmd = boost::json::parse(cc.forwarding_filter.metadata, ec);
if (ec) {
RTC_LOG(LS_WARNING) << "Invalid JSON: forwarding_filter metadata="
<< cc.forwarding_filter.metadata;
} else {
ff.metadata = ffmd;
}
config.forwarding_filter =
ConvertToForwardingFilter(cc.forwarding_filter);
}
if (cc.has_forwarding_filters()) {
std::vector<sora::SoraSignalingConfig::ForwardingFilter> filters;
for (const auto& filter : cc.forwarding_filters.filters) {
filters.push_back(ConvertToForwardingFilter(filter));
}
config.forwarding_filter = ff;
config.forwarding_filters = filters;
}
if (cc.has_client_cert()) {
config.client_cert = cc.client_cert;
Expand Down Expand Up @@ -761,6 +741,48 @@ bool Sora::InitADM(rtc::scoped_refptr<webrtc::AudioDeviceModule> adm,
return true;
}

sora::SoraSignalingConfig::ForwardingFilter Sora::ConvertToForwardingFilter(
const sora_conf::internal::ForwardingFilter& filter) {
sora::SoraSignalingConfig::ForwardingFilter ff;

if (filter.has_action()) {
ff.action = filter.action;
}
if (filter.has_name()) {
ff.name = filter.name;
}
if (filter.has_priority()) {
ff.priority = filter.priority;
}
for (const auto& rs : filter.rules) {
std::vector<sora::SoraSignalingConfig::ForwardingFilter::Rule> ffrs;
for (const auto& r : rs.rules) {
sora::SoraSignalingConfig::ForwardingFilter::Rule ffr;
ffr.field = r.field;
ffr.op = r.op;
ffr.values = r.values;
ffrs.push_back(ffr);
}
ff.rules.push_back(ffrs);
}

if (filter.has_version()) {
ff.version = filter.version;
}
if (filter.has_metadata()) {
boost::system::error_code ec;
auto ffmd = boost::json::parse(filter.metadata, ec);
if (ec) {
RTC_LOG(LS_WARNING) << "Invalid JSON: forwarding_filter metadata="
<< filter.metadata;
} else {
ff.metadata = ffmd;
}
}

return ff;
}

rtc::scoped_refptr<webrtc::VideoTrackSourceInterface> Sora::CreateVideoCapturer(
int capturer_type,
void* unity_camera_texture,
Expand Down
3 changes: 3 additions & 0 deletions src/sora.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class Sora : public std::enable_shared_from_this<Sora>,
void* GetAndroidApplicationContext(void* env);
static sora_conf::ErrorCode ToErrorCode(sora::SoraSignalingErrorCode ec);

static sora::SoraSignalingConfig::ForwardingFilter ConvertToForwardingFilter(
const sora_conf::internal::ForwardingFilter& filter);

// SoraSignalingObserver の実装
void OnSetOffer(std::string offer) override;
void OnDisconnect(sora::SoraSignalingErrorCode ec,
Expand Down

0 comments on commit f45226a

Please sign in to comment.