mediasoup 3.9.2 → 3.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,6 +19,20 @@ namespace Channel
19
19
  ChannelNotifier::channel = channel;
20
20
  }
21
21
 
22
+ void ChannelNotifier::Emit(uint64_t targetId, const char* event)
23
+ {
24
+ MS_TRACE();
25
+
26
+ MS_ASSERT(ChannelNotifier::channel, "channel unset");
27
+
28
+ json jsonNotification = json::object();
29
+
30
+ jsonNotification["targetId"] = targetId;
31
+ jsonNotification["event"] = event;
32
+
33
+ ChannelNotifier::channel->Send(jsonNotification);
34
+ }
35
+
22
36
  void ChannelNotifier::Emit(const std::string& targetId, const char* event)
23
37
  {
24
38
  MS_TRACE();
@@ -2,6 +2,7 @@
2
2
  // #define MS_LOG_DEV_LEVEL 3
3
3
 
4
4
  #include "Channel/ChannelSocket.hpp"
5
+ #include "DepLibUV.hpp"
5
6
  #include "Logger.hpp"
6
7
  #include "MediaSoupErrors.hpp"
7
8
  #include <cmath> // std::ceil()
@@ -10,7 +11,19 @@
10
11
 
11
12
  namespace Channel
12
13
  {
13
- /* Static. */
14
+ /* Static methods for UV callbacks. */
15
+ inline static void onAsync(uv_handle_t* handle)
16
+ {
17
+ while (static_cast<ChannelSocket*>(handle->data)->CallbackRead())
18
+ {
19
+ // Read while there are new messages.
20
+ }
21
+ }
22
+
23
+ inline static void onClose(uv_handle_t* handle)
24
+ {
25
+ delete handle;
26
+ }
14
27
 
15
28
  // Binary length for a 4194304 bytes payload.
16
29
  static constexpr size_t MessageMaxLen{ 4194308 };
@@ -18,11 +31,48 @@ namespace Channel
18
31
 
19
32
  /* Instance methods. */
20
33
  ChannelSocket::ChannelSocket(int consumerFd, int producerFd)
21
- : consumerSocket(consumerFd, MessageMaxLen, this), producerSocket(producerFd, MessageMaxLen)
34
+ : consumerSocket(new ConsumerSocket(consumerFd, MessageMaxLen, this)),
35
+ producerSocket(new ProducerSocket(producerFd, MessageMaxLen)),
36
+ writeBuffer(static_cast<uint8_t*>(std::malloc(MessageMaxLen)))
22
37
  {
23
38
  MS_TRACE_STD();
39
+ }
40
+
41
+ ChannelSocket::ChannelSocket(
42
+ ChannelReadFn channelReadFn,
43
+ ChannelReadCtx channelReadCtx,
44
+ ChannelWriteFn channelWriteFn,
45
+ ChannelWriteCtx channelWriteCtx)
46
+ : channelReadFn(channelReadFn), channelReadCtx(channelReadCtx), channelWriteFn(channelWriteFn),
47
+ channelWriteCtx(channelWriteCtx)
48
+ {
49
+ MS_TRACE_STD();
50
+
51
+ int err;
52
+
53
+ this->uvReadHandle = new uv_async_t;
54
+ this->uvReadHandle->data = static_cast<void*>(this);
55
+
56
+ err =
57
+ uv_async_init(DepLibUV::GetLoop(), this->uvReadHandle, reinterpret_cast<uv_async_cb>(onAsync));
24
58
 
25
- this->writeBuffer = static_cast<uint8_t*>(std::malloc(MessageMaxLen));
59
+ if (err != 0)
60
+ {
61
+ delete this->uvReadHandle;
62
+ this->uvReadHandle = nullptr;
63
+
64
+ MS_THROW_ERROR_STD("uv_async_init() failed: %s", uv_strerror(err));
65
+ }
66
+
67
+ err = uv_async_send(this->uvReadHandle);
68
+
69
+ if (err != 0)
70
+ {
71
+ delete this->uvReadHandle;
72
+ this->uvReadHandle = nullptr;
73
+
74
+ MS_THROW_ERROR_STD("uv_async_send() failed: %s", uv_strerror(err));
75
+ }
26
76
  }
27
77
 
28
78
  ChannelSocket::~ChannelSocket()
@@ -33,6 +83,9 @@ namespace Channel
33
83
 
34
84
  if (!this->closed)
35
85
  Close();
86
+
87
+ delete this->consumerSocket;
88
+ delete this->producerSocket;
36
89
  }
37
90
 
38
91
  void ChannelSocket::Close()
@@ -44,8 +97,20 @@ namespace Channel
44
97
 
45
98
  this->closed = true;
46
99
 
47
- this->consumerSocket.Close();
48
- this->producerSocket.Close();
100
+ if (this->uvReadHandle)
101
+ {
102
+ uv_close(reinterpret_cast<uv_handle_t*>(this->uvReadHandle), static_cast<uv_close_cb>(onClose));
103
+ }
104
+
105
+ if (this->consumerSocket)
106
+ {
107
+ this->consumerSocket->Close();
108
+ }
109
+
110
+ if (this->producerSocket)
111
+ {
112
+ this->producerSocket->Close();
113
+ }
49
114
  }
50
115
 
51
116
  void ChannelSocket::SetListener(Listener* listener)
@@ -71,10 +136,11 @@ namespace Channel
71
136
  return;
72
137
  }
73
138
 
74
- SendImpl(message.c_str(), static_cast<uint32_t>(message.length()));
139
+ SendImpl(
140
+ reinterpret_cast<const uint8_t*>(message.c_str()), static_cast<uint32_t>(message.length()));
75
141
  }
76
142
 
77
- void ChannelSocket::SendLog(char* message, uint32_t messageLen)
143
+ void ChannelSocket::SendLog(const char* message, uint32_t messageLen)
78
144
  {
79
145
  MS_TRACE_STD();
80
146
 
@@ -88,23 +154,84 @@ namespace Channel
88
154
  return;
89
155
  }
90
156
 
91
- SendImpl(message, messageLen);
157
+ SendImpl(reinterpret_cast<const uint8_t*>(message), messageLen);
92
158
  }
93
159
 
94
- inline void ChannelSocket::SendImpl(const void* payload, uint32_t payloadLen)
160
+ bool ChannelSocket::CallbackRead()
95
161
  {
96
162
  MS_TRACE_STD();
97
163
 
98
- std::memcpy(this->writeBuffer, &payloadLen, sizeof(uint32_t));
164
+ if (this->closed)
165
+ return false;
166
+
167
+ uint8_t* message{ nullptr };
168
+ uint32_t messageLen;
169
+ size_t messageCtx;
170
+
171
+ auto free = this->channelReadFn(
172
+ &message, &messageLen, &messageCtx, this->uvReadHandle, this->channelReadCtx);
173
+
174
+ if (free)
175
+ {
176
+ try
177
+ {
178
+ json jsonMessage = json::parse(message, message + static_cast<size_t>(messageLen));
179
+ auto* request = new Channel::ChannelRequest(this, jsonMessage);
180
+
181
+ // Notify the listener.
182
+ try
183
+ {
184
+ this->listener->OnChannelRequest(this, request);
185
+ }
186
+ catch (const MediaSoupTypeError& error)
187
+ {
188
+ request->TypeError(error.what());
189
+ }
190
+ catch (const MediaSoupError& error)
191
+ {
192
+ request->Error(error.what());
193
+ }
194
+
195
+ // Delete the Request.
196
+ delete request;
197
+ }
198
+ catch (const json::parse_error& error)
199
+ {
200
+ MS_ERROR_STD("JSON parsing error: %s", error.what());
201
+ }
202
+ catch (const MediaSoupError& error)
203
+ {
204
+ MS_ERROR_STD("discarding wrong Channel request");
205
+ }
206
+
207
+ free(message, messageLen, messageCtx);
208
+ }
209
+
210
+ return free != nullptr;
211
+ }
212
+
213
+ inline void ChannelSocket::SendImpl(const uint8_t* payload, uint32_t payloadLen)
214
+ {
215
+ MS_TRACE_STD();
99
216
 
100
- if (payloadLen != 0)
217
+ // Write using function call if provided.
218
+ if (this->channelWriteFn)
101
219
  {
102
- std::memcpy(this->writeBuffer + sizeof(uint32_t), payload, payloadLen);
220
+ this->channelWriteFn(payload, payloadLen, this->channelWriteCtx);
103
221
  }
222
+ else
223
+ {
224
+ std::memcpy(this->writeBuffer, &payloadLen, sizeof(uint32_t));
104
225
 
105
- size_t len = sizeof(uint32_t) + payloadLen;
226
+ if (payloadLen != 0)
227
+ {
228
+ std::memcpy(this->writeBuffer + sizeof(uint32_t), payload, payloadLen);
229
+ }
230
+
231
+ size_t len = sizeof(uint32_t) + payloadLen;
106
232
 
107
- this->producerSocket.Write(this->writeBuffer, len);
233
+ this->producerSocket->Write(this->writeBuffer, len);
234
+ }
108
235
  }
109
236
 
110
237
  void ChannelSocket::OnConsumerSocketMessage(ConsumerSocket* /*consumerSocket*/, char* msg, size_t msgLen)
@@ -17,10 +17,13 @@ void DepOpenSSL::ClassInit()
17
17
  {
18
18
  MS_TRACE();
19
19
 
20
- std::call_once(globalInitOnce, [] {
21
- MS_DEBUG_TAG(info, "openssl version: \"%s\"", OpenSSL_version(OPENSSL_VERSION));
20
+ std::call_once(
21
+ globalInitOnce,
22
+ []
23
+ {
24
+ MS_DEBUG_TAG(info, "openssl version: \"%s\"", OpenSSL_version(OPENSSL_VERSION));
22
25
 
23
- // Initialize some crypto stuff.
24
- RAND_poll();
25
- });
26
+ // Initialize some crypto stuff.
27
+ RAND_poll();
28
+ });
26
29
  }
@@ -6,7 +6,7 @@
6
6
 
7
7
  /* Class variables. */
8
8
 
9
- const int64_t Logger::pid{ static_cast<int64_t>(uv_os_getpid()) };
9
+ const uint64_t Logger::pid{ static_cast<uint64_t>(uv_os_getpid()) };
10
10
  thread_local Channel::ChannelSocket* Logger::channel{ nullptr };
11
11
  thread_local char Logger::buffer[Logger::bufferSize];
12
12
 
@@ -2,6 +2,7 @@
2
2
  // #define MS_LOG_DEV_LEVEL 3
3
3
 
4
4
  #include "PayloadChannel/PayloadChannelSocket.hpp"
5
+ #include "DepLibUV.hpp"
5
6
  #include "Logger.hpp"
6
7
  #include "MediaSoupErrors.hpp"
7
8
  #include "PayloadChannel/PayloadChannelRequest.hpp"
@@ -12,6 +13,18 @@
12
13
  namespace PayloadChannel
13
14
  {
14
15
  /* Static. */
16
+ inline static void onAsync(uv_handle_t* handle)
17
+ {
18
+ while (static_cast<PayloadChannelSocket*>(handle->data)->CallbackRead())
19
+ {
20
+ // Read while there are new messages.
21
+ }
22
+ }
23
+
24
+ inline static void onClose(uv_handle_t* handle)
25
+ {
26
+ delete handle;
27
+ }
15
28
 
16
29
  // Binary length for a 4194304 bytes payload.
17
30
  static constexpr size_t MessageMaxLen{ 4194308 };
@@ -19,11 +32,48 @@ namespace PayloadChannel
19
32
 
20
33
  /* Instance methods. */
21
34
  PayloadChannelSocket::PayloadChannelSocket(int consumerFd, int producerFd)
22
- : consumerSocket(consumerFd, MessageMaxLen, this), producerSocket(producerFd, MessageMaxLen)
35
+ : consumerSocket(new ConsumerSocket(consumerFd, MessageMaxLen, this)),
36
+ producerSocket(new ProducerSocket(producerFd, MessageMaxLen)),
37
+ writeBuffer(static_cast<uint8_t*>(std::malloc(MessageMaxLen)))
38
+ {
39
+ MS_TRACE();
40
+ }
41
+
42
+ PayloadChannelSocket::PayloadChannelSocket(
43
+ PayloadChannelReadFn payloadChannelReadFn,
44
+ PayloadChannelReadCtx payloadChannelReadCtx,
45
+ PayloadChannelWriteFn payloadChannelWriteFn,
46
+ PayloadChannelWriteCtx payloadChannelWriteCtx)
47
+ : payloadChannelReadFn(payloadChannelReadFn), payloadChannelReadCtx(payloadChannelReadCtx),
48
+ payloadChannelWriteFn(payloadChannelWriteFn), payloadChannelWriteCtx(payloadChannelWriteCtx)
23
49
  {
24
50
  MS_TRACE();
25
51
 
26
- this->writeBuffer = (uint8_t*)std::malloc(MessageMaxLen);
52
+ int err;
53
+
54
+ this->uvReadHandle = new uv_async_t;
55
+ this->uvReadHandle->data = static_cast<void*>(this);
56
+
57
+ err =
58
+ uv_async_init(DepLibUV::GetLoop(), this->uvReadHandle, reinterpret_cast<uv_async_cb>(onAsync));
59
+
60
+ if (err != 0)
61
+ {
62
+ delete this->uvReadHandle;
63
+ this->uvReadHandle = nullptr;
64
+
65
+ MS_THROW_ERROR("uv_async_init() failed: %s", uv_strerror(err));
66
+ }
67
+
68
+ err = uv_async_send(this->uvReadHandle);
69
+
70
+ if (err != 0)
71
+ {
72
+ delete this->uvReadHandle;
73
+ this->uvReadHandle = nullptr;
74
+
75
+ MS_THROW_ERROR("uv_async_send() failed: %s", uv_strerror(err));
76
+ }
27
77
  }
28
78
 
29
79
  PayloadChannelSocket::~PayloadChannelSocket()
@@ -35,6 +85,9 @@ namespace PayloadChannel
35
85
 
36
86
  if (!this->closed)
37
87
  Close();
88
+
89
+ delete this->consumerSocket;
90
+ delete this->producerSocket;
38
91
  }
39
92
 
40
93
  void PayloadChannelSocket::Close()
@@ -46,8 +99,20 @@ namespace PayloadChannel
46
99
 
47
100
  this->closed = true;
48
101
 
49
- this->consumerSocket.Close();
50
- this->producerSocket.Close();
102
+ if (this->uvReadHandle)
103
+ {
104
+ uv_close(reinterpret_cast<uv_handle_t*>(this->uvReadHandle), static_cast<uv_close_cb>(onClose));
105
+ }
106
+
107
+ if (this->consumerSocket)
108
+ {
109
+ this->consumerSocket->Close();
110
+ }
111
+
112
+ if (this->producerSocket)
113
+ {
114
+ this->producerSocket->Close();
115
+ }
51
116
  }
52
117
 
53
118
  void PayloadChannelSocket::SetListener(Listener* listener)
@@ -79,8 +144,11 @@ namespace PayloadChannel
79
144
  return;
80
145
  }
81
146
 
82
- SendImpl(message.c_str(), static_cast<uint32_t>(message.length()));
83
- SendImpl(payload, static_cast<uint32_t>(payloadLen));
147
+ SendImpl(
148
+ reinterpret_cast<const uint8_t*>(message.c_str()),
149
+ static_cast<uint32_t>(message.length()),
150
+ payload,
151
+ static_cast<uint32_t>(payloadLen));
84
152
  }
85
153
 
86
154
  void PayloadChannelSocket::Send(json& jsonMessage)
@@ -99,23 +167,166 @@ namespace PayloadChannel
99
167
  return;
100
168
  }
101
169
 
102
- SendImpl(message.c_str(), static_cast<uint32_t>(message.length()));
170
+ SendImpl(
171
+ reinterpret_cast<const uint8_t*>(message.c_str()), static_cast<uint32_t>(message.length()));
103
172
  }
104
173
 
105
- inline void PayloadChannelSocket::SendImpl(const void* payload, uint32_t payloadLen)
174
+ bool PayloadChannelSocket::CallbackRead()
106
175
  {
107
176
  MS_TRACE();
108
177
 
109
- std::memcpy(this->writeBuffer, &payloadLen, sizeof(uint32_t));
178
+ if (this->closed)
179
+ return false;
180
+
181
+ uint8_t* message{ nullptr };
182
+ uint32_t messageLen;
183
+ size_t messageCtx;
184
+
185
+ uint8_t* payload{ nullptr };
186
+ uint32_t payloadLen;
187
+ size_t payloadCapacity;
188
+
189
+ auto free = this->payloadChannelReadFn(
190
+ &message,
191
+ &messageLen,
192
+ &messageCtx,
193
+ &payload,
194
+ &payloadLen,
195
+ &payloadCapacity,
196
+ this->uvReadHandle,
197
+ this->payloadChannelReadCtx);
198
+
199
+ if (free)
200
+ {
201
+ try
202
+ {
203
+ json jsonData = json::parse(message, message + static_cast<size_t>(messageLen));
110
204
 
111
- if (payloadLen != 0)
205
+ if (PayloadChannelRequest::IsRequest(jsonData))
206
+ {
207
+ try
208
+ {
209
+ auto* request = new PayloadChannel::PayloadChannelRequest(this, jsonData);
210
+ request->SetPayload(payload, payloadLen);
211
+
212
+ // Notify the listener.
213
+ try
214
+ {
215
+ this->listener->OnPayloadChannelRequest(this, request);
216
+ }
217
+ catch (const MediaSoupTypeError& error)
218
+ {
219
+ request->TypeError(error.what());
220
+ }
221
+ catch (const MediaSoupError& error)
222
+ {
223
+ request->Error(error.what());
224
+ }
225
+
226
+ // Delete the Request.
227
+ delete request;
228
+ }
229
+ catch (const json::parse_error& error)
230
+ {
231
+ MS_ERROR_STD("JSON parsing error: %s", error.what());
232
+ }
233
+ catch (const MediaSoupError& error)
234
+ {
235
+ MS_ERROR("discarding wrong Payload Channel notification");
236
+ }
237
+ }
238
+
239
+ else if (Notification::IsNotification(jsonData))
240
+ {
241
+ try
242
+ {
243
+ auto* notification = new PayloadChannel::Notification(jsonData);
244
+ notification->SetPayload(payload, payloadLen);
245
+
246
+ // Notify the listener.
247
+ try
248
+ {
249
+ this->listener->OnPayloadChannelNotification(this, notification);
250
+ }
251
+ catch (const MediaSoupError& error)
252
+ {
253
+ MS_ERROR("notification failed: %s", error.what());
254
+ }
255
+
256
+ // Delete the Notification.
257
+ delete notification;
258
+ }
259
+ catch (const json::parse_error& error)
260
+ {
261
+ MS_ERROR_STD("JSON parsing error: %s", error.what());
262
+ }
263
+ catch (const MediaSoupError& error)
264
+ {
265
+ MS_ERROR("discarding wrong Payload Channel notification");
266
+ }
267
+ }
268
+
269
+ else
270
+ {
271
+ MS_ERROR("discarding wrong Payload Channel data");
272
+ }
273
+ }
274
+ catch (const json::parse_error& error)
275
+ {
276
+ MS_ERROR("JSON parsing error: %s", error.what());
277
+ }
278
+ catch (const MediaSoupError& error)
279
+ {
280
+ MS_ERROR("discarding wrong Channel request");
281
+ }
282
+
283
+ free(message, messageLen, messageCtx);
284
+ free(payload, payloadLen, payloadCapacity);
285
+ }
286
+
287
+ return free != nullptr;
288
+ }
289
+
290
+ inline void PayloadChannelSocket::SendImpl(const uint8_t* message, uint32_t messageLen)
291
+ {
292
+ MS_TRACE();
293
+
294
+ // Write using function call if provided.
295
+ if (this->payloadChannelWriteFn)
112
296
  {
113
- std::memcpy(this->writeBuffer + sizeof(uint32_t), payload, payloadLen);
297
+ this->payloadChannelWriteFn(message, messageLen, nullptr, 0, this->payloadChannelWriteCtx);
114
298
  }
299
+ else
300
+ {
301
+ std::memcpy(this->writeBuffer, &messageLen, sizeof(uint32_t));
115
302
 
116
- size_t len = sizeof(uint32_t) + payloadLen;
303
+ if (messageLen != 0)
304
+ {
305
+ std::memcpy(this->writeBuffer + sizeof(uint32_t), message, messageLen);
306
+ }
307
+
308
+ size_t len = sizeof(uint32_t) + messageLen;
117
309
 
118
- this->producerSocket.Write(this->writeBuffer, len);
310
+ this->producerSocket->Write(this->writeBuffer, len);
311
+ }
312
+ }
313
+
314
+ inline void PayloadChannelSocket::SendImpl(
315
+ const uint8_t* message, uint32_t messageLen, const uint8_t* payload, uint32_t payloadLen)
316
+ {
317
+ MS_TRACE();
318
+
319
+ // Write using function call if provided.
320
+ if (this->payloadChannelWriteFn)
321
+ {
322
+ this->payloadChannelWriteFn(
323
+ message, messageLen, payload, payloadLen, this->payloadChannelWriteCtx);
324
+ }
325
+ else
326
+ {
327
+ SendImpl(message, messageLen);
328
+ SendImpl(payload, payloadLen);
329
+ }
119
330
  }
120
331
 
121
332
  void PayloadChannelSocket::OnConsumerSocketMessage(
@@ -130,8 +341,7 @@ namespace PayloadChannel
130
341
  {
131
342
  try
132
343
  {
133
- json jsonMessage = json::parse(msg, msg + msgLen);
134
- this->ongoingRequest = new PayloadChannel::PayloadChannelRequest(this, jsonMessage);
344
+ this->ongoingRequest = new PayloadChannel::PayloadChannelRequest(this, jsonData);
135
345
  }
136
346
  catch (const json::parse_error& error)
137
347
  {
@@ -147,8 +357,7 @@ namespace PayloadChannel
147
357
  {
148
358
  try
149
359
  {
150
- json jsonMessage = json::parse(msg, msg + msgLen);
151
- this->ongoingNotification = new PayloadChannel::Notification(jsonMessage);
360
+ this->ongoingNotification = new PayloadChannel::Notification(jsonData);
152
361
  }
153
362
  catch (const json::parse_error& error)
154
363
  {
@@ -463,7 +463,7 @@ namespace RTC
463
463
  size_t levelIndex = this->nextLevelIndex >= (i + 1)
464
464
  ? this->nextLevelIndex - i - 1
465
465
  : this->nextLevelIndex + LevelsBuffLen - i - 1;
466
- uint8_t level = this->levels[levelIndex];
466
+ uint8_t level = this->levels[levelIndex];
467
467
 
468
468
  if (level < minLevel)
469
469
  {
@@ -222,12 +222,15 @@ namespace RTC
222
222
  return;
223
223
  }
224
224
 
225
- const auto* cb = new onQueuedCallback([&request](bool queued, bool sctpSendBufferFull) {
226
- if (queued)
227
- request->Accept();
228
- else
229
- request->Error(sctpSendBufferFull == true ? "sctpsendbufferfull" : "message send failed");
230
- });
225
+ const auto* cb = new onQueuedCallback(
226
+ [&request](bool queued, bool sctpSendBufferFull)
227
+ {
228
+ if (queued)
229
+ request->Accept();
230
+ else
231
+ request->Error(
232
+ sctpSendBufferFull == true ? "sctpsendbufferfull" : "message send failed");
233
+ });
231
234
 
232
235
  SendMessage(ppid, msg, len, cb);
233
236