Skip to content

Commit 6a96e7f

Browse files
authored
fix: Fixed RTCDataChannel constructor (#285)
* fix CI yaml file * fix datachanenl constructor * fixed bug * refactor
1 parent 4268ddb commit 6a96e7f

19 files changed

+294
-112
lines changed

Plugin~/WebRTCPlugin/Context.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -376,17 +376,9 @@ namespace webrtc
376376
m_listStatsReport.erase(found);
377377
}
378378

379-
DataChannelObject* Context::CreateDataChannel(PeerConnectionObject* obj, const char* label, const RTCDataChannelInit& options)
379+
DataChannelObject* Context::CreateDataChannel(PeerConnectionObject* obj, const char* label, const DataChannelInit& options)
380380
{
381-
webrtc::DataChannelInit config;
382-
config.reliable = options.reliable;
383-
config.ordered = options.ordered;
384-
config.maxRetransmitTime = options.maxRetransmitTime;
385-
config.maxRetransmits = options.maxRetransmits;
386-
config.protocol = options.protocol == nullptr ? "" : options.protocol;
387-
config.negotiated = options.negotiated;
388-
389-
auto channel = obj->connection->CreateDataChannel(label, &config);
381+
auto channel = obj->connection->CreateDataChannel(label, &options);
390382
if (channel == nullptr)
391383
return nullptr;
392384
auto dataChannelObj = std::make_unique<DataChannelObject>(channel, *obj);

Plugin~/WebRTCPlugin/Context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ namespace webrtc
8989
void DeleteStatsReport(const webrtc::RTCStatsReport* report);
9090

9191
// DataChannel
92-
DataChannelObject* CreateDataChannel(PeerConnectionObject* obj, const char* label, const RTCDataChannelInit& options);
92+
DataChannelObject* CreateDataChannel(PeerConnectionObject* obj, const char* label, const DataChannelInit& options);
9393
void AddDataChannel(std::unique_ptr<DataChannelObject>& channel);
9494
void DeleteDataChannel(DataChannelObject* obj);
9595

Plugin~/WebRTCPlugin/DataChannelObject.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ namespace webrtc
1919
DataChannelObject(rtc::scoped_refptr<webrtc::DataChannelInterface> channel, PeerConnectionObject& pc);
2020
~DataChannelObject();
2121

22-
std::string GetLabel() const
23-
{
24-
return dataChannel->label();
25-
}
26-
int GetID() const
27-
{
28-
return dataChannel->id();
29-
}
3022
void Close()
3123
{
3224
dataChannel->Close();
@@ -36,11 +28,6 @@ namespace webrtc
3628
dataChannel->Send(webrtc::DataBuffer(std::string(data)));
3729
}
3830

39-
webrtc::DataChannelInterface::DataState GetReadyState() const
40-
{
41-
return dataChannel->state();
42-
}
43-
4431
void Send(const byte* data, int len)
4532
{
4633
rtc::CopyOnWriteBuffer buf(data, len);
@@ -67,7 +54,6 @@ namespace webrtc
6754
DelegateOnMessage onMessage = nullptr;
6855
DelegateOnOpen onOpen = nullptr;
6956
DelegateOnClose onClose = nullptr;
70-
private:
7157
rtc::scoped_refptr<webrtc::DataChannelInterface> dataChannel;
7258
};
7359

Plugin~/WebRTCPlugin/WebRTCPlugin.cpp

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,19 @@ namespace webrtc
102102
}
103103
return *this;
104104
}
105+
106+
explicit operator const absl::optional<T>&() const
107+
{
108+
absl::optional<T> dst = absl::nullopt;
109+
if (hasValue)
110+
dst = value;
111+
return dst;
112+
}
113+
114+
const T& value_or(const T& v) const
115+
{
116+
return hasValue ? value : v;
117+
}
105118
};
106119

107120
template<typename T>
@@ -739,9 +752,27 @@ extern "C"
739752
obj->CreateAnswer(*options);
740753
}
741754

755+
struct RTCDataChannelInit
756+
{
757+
Optional<bool> ordered;
758+
Optional<int32_t> maxRetransmitTime;
759+
Optional<int32_t> maxRetransmits;
760+
char* protocol;
761+
Optional<bool> negotiated;
762+
Optional<int32_t> id;
763+
};
764+
742765
UNITY_INTERFACE_EXPORT DataChannelObject* ContextCreateDataChannel(Context* ctx, PeerConnectionObject* obj, const char* label, const RTCDataChannelInit* options)
743766
{
744-
return ctx->CreateDataChannel(obj, label, *options);
767+
DataChannelInit _options;
768+
_options.ordered = options->ordered.value_or(true);
769+
_options.maxRetransmitTime = static_cast<absl::optional<int32_t>>(options->maxRetransmitTime);
770+
_options.maxRetransmits = static_cast<absl::optional<int32_t>>(options->maxRetransmits);
771+
_options.protocol = options->protocol == nullptr ? "" : options->protocol;
772+
_options.negotiated = options->negotiated.value_or(false);
773+
_options.id = options->id.value_or(-1);
774+
775+
return ctx->CreateDataChannel(obj, label, _options);
745776
}
746777

747778
UNITY_INTERFACE_EXPORT void ContextDeleteDataChannel(Context* ctx, DataChannelObject* channel)
@@ -1077,18 +1108,48 @@ extern "C"
10771108

10781109
UNITY_INTERFACE_EXPORT int DataChannelGetID(DataChannelObject* dataChannelObj)
10791110
{
1080-
return dataChannelObj->GetID();
1111+
return dataChannelObj->dataChannel->id();
10811112
}
10821113

10831114
UNITY_INTERFACE_EXPORT char* DataChannelGetLabel(DataChannelObject* dataChannelObj)
10841115
{
1085-
return ConvertString(dataChannelObj->GetLabel());
1116+
return ConvertString(dataChannelObj->dataChannel->label());
1117+
}
1118+
1119+
UNITY_INTERFACE_EXPORT char* DataChannelGetProtocol(DataChannelObject* dataChannelObj)
1120+
{
1121+
return ConvertString(dataChannelObj->dataChannel->protocol());
1122+
}
1123+
1124+
UNITY_INTERFACE_EXPORT uint16_t DataChannelGetMaxRetransmits(DataChannelObject* dataChannelObj)
1125+
{
1126+
return dataChannelObj->dataChannel->maxRetransmits();
1127+
}
1128+
1129+
UNITY_INTERFACE_EXPORT uint16_t DataChannelGetMaxRetransmitTime(DataChannelObject* dataChannelObj)
1130+
{
1131+
return dataChannelObj->dataChannel->maxRetransmitTime();
1132+
}
1133+
1134+
UNITY_INTERFACE_EXPORT bool DataChannelGetOrdered(DataChannelObject* dataChannelObj)
1135+
{
1136+
return dataChannelObj->dataChannel->ordered();
1137+
}
1138+
1139+
UNITY_INTERFACE_EXPORT uint64_t DataChannelGetBufferedAmount(DataChannelObject* dataChannelObj)
1140+
{
1141+
return dataChannelObj->dataChannel->buffered_amount();
1142+
}
1143+
1144+
UNITY_INTERFACE_EXPORT bool DataChannelGetNegotiated(DataChannelObject* dataChannelObj)
1145+
{
1146+
return dataChannelObj->dataChannel->negotiated();
10861147
}
10871148

10881149
UNITY_INTERFACE_EXPORT DataChannelInterface::DataState DataChannelGetReadyState(
10891150
DataChannelObject* dataChannelObj)
10901151
{
1091-
return dataChannelObj->GetReadyState();
1152+
return dataChannelObj->dataChannel->state();
10921153
}
10931154

10941155
UNITY_INTERFACE_EXPORT void DataChannelSend(DataChannelObject* dataChannelObj, const char* msg)

Plugin~/WebRTCPlugin/WebRTCPlugin.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,6 @@ namespace webrtc
145145
bool offerToReceiveVideo;
146146
};
147147

148-
struct RTCDataChannelInit
149-
{
150-
bool reliable = false;
151-
bool ordered = true;
152-
int maxRetransmitTime = -1;
153-
int maxRetransmits = -1;
154-
char* protocol;
155-
bool negotiated = false;
156-
int id = -1;
157-
};
158-
159148
struct RTCAnswerOptions
160149
{
161150
bool iceRestart;

Runtime/Scripts/Context.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void PeerConnectionRegisterOnSetSessionDescFailure(IntPtr ptr, DelegateNa
117117
NativeMethods.PeerConnectionRegisterOnSetSessionDescFailure(self, ptr, callback);
118118
}
119119

120-
public IntPtr CreateDataChannel(IntPtr ptr, string label, ref RTCDataChannelInit options)
120+
public IntPtr CreateDataChannel(IntPtr ptr, string label, ref RTCDataChannelInitInternal options)
121121
{
122122
return NativeMethods.ContextCreateDataChannel(self, ptr, label, ref options);
123123
}

Runtime/Scripts/Internal/Marshalling.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,24 @@ public static implicit operator OptionalDouble(double? a)
106106
}
107107
}
108108

109+
[StructLayout(LayoutKind.Sequential)]
110+
internal struct OptionalBool
111+
{
112+
[MarshalAs(UnmanagedType.U1)]
113+
public bool hasValue;
114+
[MarshalAs(UnmanagedType.U1)]
115+
public bool value;
116+
117+
public static implicit operator bool?(OptionalBool a)
118+
{
119+
return a.hasValue ? a.value : (bool?)null;
120+
}
121+
public static implicit operator OptionalBool(bool? a)
122+
{
123+
return new OptionalBool { hasValue = a.HasValue, value = a.GetValueOrDefault() };
124+
}
125+
}
126+
109127
[StructLayout(LayoutKind.Sequential)]
110128
internal struct MarshallingArray<T> where T : struct
111129
{

Runtime/Scripts/RTCDataChannel.cs

Lines changed: 104 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,71 @@
33

44
namespace Unity.WebRTC
55
{
6+
/// <summary>
7+
///
8+
/// </summary>
9+
public class RTCDataChannelInit
10+
{
11+
/// <summary>
12+
///
13+
/// </summary>
14+
public bool? ordered;
15+
/// <summary>
16+
///
17+
/// </summary>
18+
/// <remarks>
19+
/// <see cref="RTCDataChannelInit.maxRetransmits"/> Cannot be set along with `maxRetransmits` />
20+
/// </remarks>
21+
/// <seealso cref="RTCDataChannelInit.maxRetransmits"/>
22+
public int? maxPacketLifeTime;
23+
/// <summary>
24+
///
25+
/// </summary>
26+
/// <remarks>
27+
/// <see cref="RTCDataChannelInit.maxPacketLifeTime"/> Cannot be set along with `maxPacketLifeTime` />
28+
/// </remarks>
29+
/// <seealso cref="RTCDataChannelInit.maxPacketLifeTime"/>
30+
public int? maxRetransmits;
31+
/// <summary>
32+
///
33+
/// </summary>
34+
public string protocol;
35+
/// <summary>
36+
///
37+
/// </summary>
38+
public bool? negotiated;
39+
/// <summary>
40+
///
41+
/// </summary>
42+
public int? id;
43+
}
44+
45+
[StructLayout(LayoutKind.Sequential)]
46+
internal struct RTCDataChannelInitInternal
47+
{
48+
public OptionalBool ordered;
49+
public OptionalInt maxRetransmitTime;
50+
public OptionalInt maxRetransmits;
51+
[MarshalAs(UnmanagedType.LPStr)]
52+
public string protocol;
53+
public OptionalBool negotiated;
54+
public OptionalInt id;
55+
56+
public static explicit operator RTCDataChannelInitInternal (RTCDataChannelInit origin)
57+
{
58+
RTCDataChannelInitInternal dst = new RTCDataChannelInitInternal
59+
{
60+
ordered = origin.ordered,
61+
maxRetransmitTime = origin.maxPacketLifeTime,
62+
maxRetransmits = origin.maxRetransmits,
63+
protocol = origin.protocol,
64+
negotiated = origin.negotiated,
65+
id = origin.id
66+
};
67+
return dst;
68+
}
69+
}
70+
671
public delegate void DelegateOnOpen();
772
public delegate void DelegateOnClose();
873
public delegate void DelegateOnMessage(byte[] bytes);
@@ -45,12 +110,45 @@ public DelegateOnClose OnClose
45110
}
46111
}
47112

48-
public int Id
49-
{
50-
get => NativeMethods.DataChannelGetID(self);
51-
}
113+
/// <summary>
114+
///
115+
/// </summary>
116+
public int Id => NativeMethods.DataChannelGetID(self);
117+
118+
/// <summary>
119+
///
120+
/// </summary>
121+
public string Label => NativeMethods.DataChannelGetLabel(self).AsAnsiStringWithFreeMem();
122+
123+
/// <summary>
124+
///
125+
/// </summary>
126+
public string Protocol => NativeMethods.DataChannelGetProtocol(self).AsAnsiStringWithFreeMem();
127+
128+
/// <summary>
129+
///
130+
/// </summary>
131+
public ushort MaxRetransmits => NativeMethods.DataChannelGetMaxRetransmits(self);
132+
133+
/// <summary>
134+
///
135+
/// </summary>
136+
public ushort MaxRetransmitTime => NativeMethods.DataChannelGetMaxRetransmitTime(self);
52137

53-
public string Label { get; private set; }
138+
/// <summary>
139+
///
140+
/// </summary>
141+
public bool Ordered => NativeMethods.DataChannelGetOrdered(self);
142+
143+
/// <summary>
144+
///
145+
/// </summary>
146+
public ulong BufferedAmount => NativeMethods.DataChannelGetBufferedAmount(self);
147+
148+
/// <summary>
149+
///
150+
/// </summary>
151+
public bool Negotiated => NativeMethods.DataChannelGetNegotiated(self);
54152

55153
/// <summary>
56154
/// The property returns an enum of the <c>RTCDataChannelState</c> which shows
@@ -60,10 +158,7 @@ public int Id
60158
/// <see cref="Send(string)"/> method must be called when the state is <b>Open</b>.
61159
/// </remarks>
62160
/// <seealso cref="RTCDataChannelState"/>
63-
public RTCDataChannelState ReadyState
64-
{
65-
get => NativeMethods.DataChannelGetReadyState(self);
66-
}
161+
public RTCDataChannelState ReadyState => NativeMethods.DataChannelGetReadyState(self);
67162

68163
[AOT.MonoPInvokeCallback(typeof(DelegateNativeOnMessage))]
69164
static void DataChannelNativeOnMessage(IntPtr ptr, byte[] msg, int len)
@@ -105,8 +200,6 @@ internal RTCDataChannel(IntPtr ptr, RTCPeerConnection peerConnection)
105200
{
106201
self = ptr;
107202
WebRTC.Table.Add(self, this);
108-
Label = NativeMethods.DataChannelGetLabel(self).AsAnsiStringWithFreeMem();
109-
110203
NativeMethods.DataChannelRegisterOnMessage(self, DataChannelNativeOnMessage);
111204
NativeMethods.DataChannelRegisterOnOpen(self, DataChannelNativeOnOpen);
112205
NativeMethods.DataChannelRegisterOnClose(self, DataChannelNativeOnClose);

Runtime/Scripts/RTCPeerConnection.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,12 @@ public RTCSessionDescriptionAsyncOperation CreateAnswer(ref RTCAnswerOptions opt
536536
/// This string may be checked by <see cref="RTCDataChannel.Label"/>. </param>
537537
/// <param name="options"> A struct provides configuration options for the data channel. </param>
538538
/// <returns> A new data channel. </returns>
539-
public RTCDataChannel CreateDataChannel(string label, ref RTCDataChannelInit options)
539+
public RTCDataChannel CreateDataChannel(string label, RTCDataChannelInit options = null)
540540
{
541-
IntPtr ptr = WebRTC.Context.CreateDataChannel(self, label, ref options);
541+
RTCDataChannelInitInternal _options =
542+
options == null ? new RTCDataChannelInitInternal() : (RTCDataChannelInitInternal)options;
543+
544+
IntPtr ptr = WebRTC.Context.CreateDataChannel(self, label, ref _options);
542545
if (ptr == IntPtr.Zero)
543546
throw new ArgumentException("RTCDataChannelInit object is incorrect.");
544547
return new RTCDataChannel(ptr, this);

0 commit comments

Comments
 (0)