Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

マルチ転送フィルターを設定可能にする #62

Merged
merged 18 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

## develop

- [ADD] forwardingfilters を設定可能にする
- マルチ転送フィルターを設定できるように項目を追加
- @torikizi
- [ADD] forwardingfilter に name と priority を追加
- 既存の forwardingfilter に name と priority を追加
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここ [ADD] のところと同じ内容だから削って良さそう

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かに同じことが書いてあるだけでした。消します。

- @torikizi

## sora-unity-sdk-2024.4.0

- [UPDATE] Sora Unity SDK 2024.4.0 にあげる
Expand Down
136 changes: 95 additions & 41 deletions SoraUnitySdkSamples/Assets/SoraSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,92 @@ public class RuleList
{
public Rule[] data;
}
[System.Serializable]
public class ForwardingFilter
{
public bool enableAction = false;
public string action;
public bool enableName = false;
public string name;
public bool enablePriority = false;
public int priority;
public RuleList[] rules;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RuleList の配列だから ruleLists の方が名前的に良さそう。rules だと Rule の配列だと思ってしまいそうなので。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ruleLists に名前を変えます。

public bool enableVersion = false;
public string version = "";
public bool enableMetadata = false;
public string metadata = "";
}
public static class ForwardingFilterHelper
{
[Serializable]
private class ForwardingFilterMetadata
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そもそもこれって ForwardingFilter に渡すメタデータ用のクラスなので、string metadata の代わりに ForwardingFilterMetadata metadata にした方が良いです。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

        private class ForwardingFilterMetadata
        {
            public ForwardingFilterMetadata metadata;
        }

というようにした方が良いというご指摘だと理解したのですが、ここは JSON 用のプロパティのため、string にしておかないとうまくいかないようです

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

いや、ForwardingFilter クラスに定義してある string metadata を、string じゃなく ForwardingFilterMetadata にした方が良いよねって話です

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

失礼しました。勘違いしておりました。
以下のように修正します。

public ForwardingFilterMetadata metadata = new ForwardingFilterMetadata();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

インスペクタで設定してもらう値だから初期値はいらないはず

{
public string metadata;
}
private static void ConfigureFilter(Sora.ForwardingFilter filter, ForwardingFilter settings)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

settings という名前が現状と合ってなさそう

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forwardingFilter に修正します

{
if (settings.enableAction) filter.Action = settings.action;
if (settings.enableName) filter.Name = settings.name;
if (settings.enablePriority) filter.Priority = settings.priority;

if (settings.rules != null)
{
foreach (var ruleSet in settings.rules)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ruleSet の Set は集合だと勘違いしやすいので、別の名前を付けたほうが良いです

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ruleListData にします

{
if (ruleSet?.data == null) continue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ruleSetruleSet.data が null になることってあります?インスペクタで設定している以上無さそうな気がしてます。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null になることはない想定です。こちらも修正します


var ruleList = new List<Sora.ForwardingFilter.Rule>();
foreach (var rule in ruleSet.data)
{
var newRule = new Sora.ForwardingFilter.Rule
{
Field = rule.field,
Operator = rule.op
};
newRule.Values.AddRange(rule.values);
ruleList.Add(newRule);
}

if (ruleList.Any())
{
filter.Rules.Add(ruleList);
}
}
}

if (settings.enableVersion) filter.Version = settings.version;
if (settings.enableMetadata)
{
filter.Metadata = ForwardingFilterMetadataHelper.ConvertMetadataToJson(settings.metadata);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここでしか ConvertMetadataToJson 関数を使ってないので、この関数に処理を分ける必要は無いです

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (forwardingFilter.enableMetadata) の中にまとめます

}
}
private static class ForwardingFilterMetadataHelper
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToJson 1個で済むようになったので、他の if 文と同じ書き方にしておいて下さい

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご指摘のとおりです。他とあわせて一行になるようにします。

public static string ConvertMetadataToJson(string metadata)
{
var ffMetadata = new ForwardingFilterMetadata()
{
metadata = metadata
};
return JsonUtility.ToJson(ffMetadata);
}
}
public static Sora.ForwardingFilter CreateFilter(ForwardingFilter settings)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create より Convert の方が機能として近そうなのと、この機能は Forwarding Filter なので略して Filter と書いてはダメです。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

結果的に ForwardingFilterHelper はこの関数1個しか使わないので、ForwardingFilterHelper というクラスにまとめなくても、単に SoraSample に static 関数1個生やすだけでいいと思います。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご指摘のとおりです。ForwardingFilterHelper にまとめるのをやめます。

{
if (settings == null) return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create 関数で入力が null は前提条件として破綻してると思うので、この関数を呼び出す側がチェックした方が良いです。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forwardingfilter と forwardingfilters に enable フラグを追加してチェックするように修正します。


var filter = new Sora.ForwardingFilter();
ConfigureFilter(filter, settings);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これ1箇所でしか使ってないので関数にしなくても良さそう。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConfigureFilter を削除して ConvertForwardingFilter にまとめます。

return filter;
}
}

[Header("ForwardingFilter の設定")]
public bool enableForwardingFilterAction = false;
public string forwardingFilterAction;
public RuleList[] forwardingFilter;
public bool enableForwardingFilterVersion = false;
public string forwardingFilterVersion = "";
public bool enableForwardingFilterMetadata = false;
public string forwardingFilterMetadataMessage = "";
[System.Obsolete("forwardingFilter は非推奨です。代わりに forwardingFilters を使用してください。")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これはもう1行下で設定するのが正しそう?もしかしたら両方の変数に必要かも

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

対象としては両方あった方が良さそうですね。両方設定します。
Image from Gyazo

public ForwardingFilter forwardingFilter;

[Header("ForwardingFilters の設定")]
public ForwardingFilter[] forwardingFilters;

[Header("DataChannel シグナリングの設定")]
public bool dataChannelSignaling = false;
Expand Down Expand Up @@ -597,12 +674,6 @@ class VideoH264Params
public string profile_level_id;
}

[Serializable]
class ForwardingFilterMetadata
{
public string forwarding_filter_metadata;
}

public void OnClickStart()
{

Expand Down Expand Up @@ -677,16 +748,6 @@ public void OnClickStart()
};
videoH264ParamsJson = JsonUtility.ToJson(h264Params);
}
// enableForwardingFilterMetadata が true の場合はメタデータを設定する
string forwardingFilterMetadataJson = "";
if (enableForwardingFilterMetadata)
{
var ffm = new ForwardingFilterMetadata()
{
forwarding_filter_metadata = forwardingFilterMetadataMessage
};
forwardingFilterMetadataJson = JsonUtility.ToJson(ffm);
}

InitSora();

Expand Down Expand Up @@ -777,30 +838,23 @@ public void OnClickStart()
}
fixedDataChannelLabels = config.DataChannels.Select(x => x.Label).ToArray();
}
if (forwardingFilter.Length != 0)
if (forwardingFilter != null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

インスペクタで設定している以上、通常 forwardingFilter は null にはならないので、bool enableForwardingFilter を用意するか、以前と同じ条件にするなら forwardingFilter.rules.Length != 0 にする必要があります。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enableForwardingFilter を用意します。

{
config.ForwardingFilter = new Sora.ForwardingFilter();
if (enableForwardingFilterAction) config.ForwardingFilter.Action = forwardingFilterAction;
foreach (var rs in forwardingFilter)
config.ForwardingFilter = ForwardingFilterHelper.CreateFilter(forwardingFilter);
}
if (forwardingFilters != null && forwardingFilters.Length > 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

インスペクタで設定している以上、通常 forwardingFilters は null にはならないはずです。
あと、サンプルの仕様として長さ0の ForwardingFilters を作ることを許容するかどうか次第ですけど、こちらも bool enableForwardingFilters フラグを用意するかどうか考える必要がありそう。

Copy link
Contributor Author

@torikizi torikizi Dec 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

何も条件がないと forwardingfilters の項目を設定してしまうのでフラグを用意します。

{
config.ForwardingFilters = new List<Sora.ForwardingFilter>();

foreach (var filter in forwardingFilters)
{
var ccrs = new List<Sora.ForwardingFilter.Rule>();
foreach (var r in rs.data)
var newFilter = ForwardingFilterHelper.CreateFilter(filter);
if (newFilter != null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここが null になることは無いはず

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご指摘のとおりです。チェックを外します。

{
var ccr = new Sora.ForwardingFilter.Rule();
ccr.Field = r.field;
ccr.Operator = r.op;
foreach (var v in r.values)
{
ccr.Values.Add(v);
}
ccrs.Add(ccr);
config.ForwardingFilters.Add(newFilter);
}
config.ForwardingFilter.Rules.Add(ccrs);
}
if (enableForwardingFilterVersion) config.ForwardingFilter.Version = forwardingFilterVersion;
if (enableForwardingFilterMetadata) config.ForwardingFilter.Metadata = forwardingFilterMetadataJson;
}

sora.Connect(config);
SetState(State.Started);
Debug.LogFormat("Sora is Created: signalingUrl={0}, channelId={1}", signalingUrl, channelId);
Expand Down