mediasoup-sys 0.12.2

FFI bindings to C++ libmediasoup-worker
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#define MS_CLASS "RTC::RTP::Codecs::VP8"
// #define MS_LOG_DEV_LEVEL 3

#include "RTC/RTP/Codecs/VP8.hpp"
#include "Logger.hpp"
#include <cstring> // std::memcpy()

namespace RTC
{
	namespace RTP
	{
		namespace Codecs
		{
			/* Class methods. */

			VP8::PayloadDescriptor* VP8::Parse(const uint8_t* data, size_t len)
			{
				MS_TRACE();

				if (len < 1)
				{
					MS_WARN_DEV("ignoring empty payload");

					return nullptr;
				}

				std::unique_ptr<PayloadDescriptor> payloadDescriptor(new PayloadDescriptor());

				size_t offset{ 0 };
				uint8_t byte = data[offset];

				payloadDescriptor->extended       = (byte >> 7) & 0x01;
				payloadDescriptor->nonReference   = (byte >> 5) & 0x01;
				payloadDescriptor->start          = (byte >> 4) & 0x01;
				payloadDescriptor->partitionIndex = byte & 0x07;

				if (payloadDescriptor->extended)
				{
					if (len < ++offset + 1)
					{
						MS_WARN_DEV("ignoring invalid payload (2)");

						return nullptr;
					}

					byte = data[offset];

					payloadDescriptor->i = (byte >> 7) & 0x01;
					payloadDescriptor->l = (byte >> 6) & 0x01;
					payloadDescriptor->t = (byte >> 5) & 0x01;
					payloadDescriptor->k = (byte >> 4) & 0x01;
				}

				if (payloadDescriptor->i)
				{
					if (len < ++offset + 1)
					{
						MS_WARN_DEV("ignoring invalid payload (3)");

						return nullptr;
					}

					byte = data[offset];

					if ((byte >> 7) & 0x01)
					{
						if (len < ++offset + 1)
						{
							MS_WARN_DEV("ignoring invalid payload (4)");

							return nullptr;
						}

						payloadDescriptor->hasTwoBytesPictureId = true;
						payloadDescriptor->pictureId            = (byte & 0x7F) << 8;
						payloadDescriptor->pictureId += data[offset];
					}
					else
					{
						payloadDescriptor->hasOneBytePictureId = true;
						payloadDescriptor->pictureId           = byte & 0x7F;
					}

					payloadDescriptor->hasPictureId = true;
				}

				if (payloadDescriptor->l)
				{
					if (len < ++offset + 1)
					{
						MS_WARN_DEV("ignoring invalid payload (5)");

						return nullptr;
					}

					payloadDescriptor->hasTl0PictureIndex = true;
					payloadDescriptor->tl0PictureIndex    = data[offset];
				}

				if (payloadDescriptor->t || payloadDescriptor->k)
				{
					if (len < ++offset + 1)
					{
						MS_WARN_DEV("ignoring invalid payload (6)");

						return nullptr;
					}

					byte = data[offset];

					payloadDescriptor->hasTlIndex = true;
					payloadDescriptor->tlIndex    = (byte >> 6) & 0x03;
					payloadDescriptor->y          = (byte >> 5) & 0x01;
					payloadDescriptor->keyIndex   = byte & 0x1F;
				}

				if (
				  // NOLINTNEXTLINE(bugprone-inc-dec-in-conditions)
				  (len >= ++offset + 1) && payloadDescriptor->start &&
				  payloadDescriptor->partitionIndex == 0 && (!(data[offset] & 0x01)) // Inverse Keyframe bit.
				)
				{
					payloadDescriptor->isKeyFrame = true;
				}

				return payloadDescriptor.release();
			}

			void VP8::ProcessRtpPacket(RTP::Packet* packet)
			{
				MS_TRACE();

				auto* data = packet->GetPayload();
				auto len   = packet->GetPayloadLength();

				PayloadDescriptor* payloadDescriptor = VP8::Parse(data, len);

				if (!payloadDescriptor)
				{
					return;
				}

				auto* payloadDescriptorHandler = new PayloadDescriptorHandler(payloadDescriptor);

				packet->SetPayloadDescriptorHandler(payloadDescriptorHandler);

				// Modify the RtpPacket payload in order to always have two byte pictureId.
				if (payloadDescriptor->hasOneBytePictureId)
				{
					// Shift the RTP payload one byte from the begining of the pictureId field.
					packet->ShiftPayload(2, 1);

					// Set the two byte pictureId marker bit.
					data[2] = 0x80;

					// Update the payloadDescriptor.
					payloadDescriptor->hasOneBytePictureId  = false;
					payloadDescriptor->hasTwoBytesPictureId = true;
				}
			}

			/* Instance methods. */

			void VP8::PayloadDescriptor::Dump(int indentation) const
			{
				MS_TRACE();

				MS_DUMP_CLEAN(indentation, "<VP8::PayloadDescriptor>");
				MS_DUMP_CLEAN(
				  indentation,
				  "  i:%" PRIu8 "|l:%" PRIu8 "|t:%" PRIu8 "|k:%" PRIu8,
				  this->i,
				  this->l,
				  this->t,
				  this->k);
				MS_DUMP_CLEAN(indentation, "  extended: %" PRIu8, this->extended);
				MS_DUMP_CLEAN(indentation, "  nonReference: %" PRIu8, this->nonReference);
				MS_DUMP_CLEAN(indentation, "  start: %" PRIu8, this->start);
				MS_DUMP_CLEAN(indentation, "  partitionIndex: %" PRIu8, this->partitionIndex);
				MS_DUMP_CLEAN(indentation, "  pictureId: %" PRIu16, this->pictureId);
				MS_DUMP_CLEAN(indentation, "  tl0PictureIndex: %" PRIu8, this->tl0PictureIndex);
				MS_DUMP_CLEAN(indentation, "  tlIndex: %" PRIu8, this->tlIndex);
				MS_DUMP_CLEAN(indentation, "  y: %" PRIu8, this->y);
				MS_DUMP_CLEAN(indentation, "  keyIndex: %" PRIu8, this->keyIndex);
				MS_DUMP_CLEAN(indentation, "  isKeyFrame: %s", this->isKeyFrame ? "true" : "false");
				MS_DUMP_CLEAN(indentation, "  hasPictureId: %s", this->hasPictureId ? "true" : "false");
				MS_DUMP_CLEAN(
				  indentation, "  hasOneBytePictureId: %s", this->hasOneBytePictureId ? "true" : "false");
				MS_DUMP_CLEAN(
				  indentation, "  hasTwoBytesPictureId: %s", this->hasTwoBytesPictureId ? "true" : "false");
				MS_DUMP_CLEAN(
				  indentation, "  hasTl0PictureIndex: %s", this->hasTl0PictureIndex ? "true" : "false");
				MS_DUMP_CLEAN(indentation, "  hasTlIndex: %s", this->hasTlIndex ? "true" : "false");
				MS_DUMP_CLEAN(indentation, "</VP8::PayloadDescriptor>");
			}

			void VP8::PayloadDescriptor::Encode(uint8_t* data, uint16_t pictureId, uint8_t tl0PictureIndex) const
			{
				MS_TRACE();

				if (data == nullptr)
				{
					MS_ABORT("data is nullptr");
				}

				// Nothing to do.
				if (!this->extended)
				{
					return;
				}

				data += 2;

				if (this->i)
				{
					if (this->hasTwoBytesPictureId)
					{
						uint16_t netPictureId = htons(pictureId);

						std::memcpy(data, &netPictureId, 2);
						data[0] |= 0x80;
						data += 2;
					}
					else if (this->hasOneBytePictureId)
					{
						*data = pictureId;
						data++;

						if (pictureId > 127)
						{
							MS_DEBUG_TAG(rtp, "casting pictureId value to one byte");
						}
					}
				}

				if (this->l)
				{
					*data = tl0PictureIndex;
				}
			}

			void VP8::PayloadDescriptor::Encode(uint8_t* data) const
			{
				MS_TRACE();

				if (this->encoder == std::nullopt)
				{
					return;
				}

				this->encoder->Encode(data, this);
			}

			void VP8::PayloadDescriptor::Restore(uint8_t* data) const
			{
				MS_TRACE();

				if (this->hasPictureId && this->hasTl0PictureIndex)
				{
					Encode(data, this->pictureId, this->tl0PictureIndex);
				}
			}

			void VP8::PayloadDescriptor::Encoder::Encode(
			  uint8_t* data, const PayloadDescriptor* payloadDescriptor) const
			{
				payloadDescriptor->Encode(
				  data, this->encodingData.pictureId, this->encodingData.tl0PictureIndex);
			}

			VP8::PayloadDescriptorHandler::PayloadDescriptorHandler(VP8::PayloadDescriptor* payloadDescriptor)
			{
				MS_TRACE();

				this->payloadDescriptor.reset(payloadDescriptor);
			}

			bool VP8::PayloadDescriptorHandler::Process(
			  Codecs::EncodingContext* encodingContext, RTP::Packet* packet, bool& /*marker*/)
			{
				MS_TRACE();

				auto* context = static_cast<Codecs::VP8::EncodingContext*>(encodingContext);

				MS_ASSERT(context->GetTargetTemporalLayer() >= 0, "target temporal layer cannot be -1");

				// Check if the payload should contain temporal layer info.
				if (context->GetTemporalLayers() > 1 && !this->payloadDescriptor->hasTlIndex)
				{
					MS_WARN_DEV("stream is supposed to have >1 temporal layers but does not have TlIndex field");
				}

				// Check whether pictureId and tl0PictureIndex sync is required.
				if (
				  context->syncRequired && this->payloadDescriptor->hasPictureId &&
				  this->payloadDescriptor->hasTl0PictureIndex)
				{
					context->pictureIdManager.Sync(this->payloadDescriptor->pictureId - 1);
					context->tl0PictureIndexManager.Sync(this->payloadDescriptor->tl0PictureIndex - 1);

					context->syncRequired = false;
				}

				// Incremental pictureId. Check the temporal layer.
				if (
				  this->payloadDescriptor->hasPictureId && this->payloadDescriptor->hasTlIndex &&
				  this->payloadDescriptor->hasTl0PictureIndex &&
				  !RTC::SeqManager<uint16_t, 15>::IsSeqLowerThan(
				    this->payloadDescriptor->pictureId, context->pictureIdManager.GetMaxInput()))
				{
					// Drop if:
					// - Temporal layer is higher than target.
					// - Temporal layer is higher than current and sync flag is not set.
					if (
					  this->payloadDescriptor->tlIndex > context->GetTargetTemporalLayer() ||
					  (this->payloadDescriptor->tlIndex > context->GetCurrentTemporalLayer() &&
					   !this->payloadDescriptor->y))
					{
						context->pictureIdManager.Drop(this->payloadDescriptor->pictureId);

						if (this->payloadDescriptor->tlIndex == 0)
						{
							context->tl0PictureIndexManager.Drop(this->payloadDescriptor->tl0PictureIndex);
						}

						return false;
					}
				}

				// Update pictureId and tl0PictureIndex values.
				uint16_t pictureId;
				uint8_t tl0PictureIndex;

				// Do not send a dropped pictureId.
				if (
				  this->payloadDescriptor->hasPictureId &&
				  !context->pictureIdManager.Input(this->payloadDescriptor->pictureId, pictureId))
				{
					return false;
				}

				// Do not send a dropped tl0PictureIndex.
				if (
				  this->payloadDescriptor->hasTl0PictureIndex &&
				  !context->tl0PictureIndexManager.Input(
				    this->payloadDescriptor->tl0PictureIndex, tl0PictureIndex))
				{
					return false;
				}

				// Update/fix current temporal layer.
				if (this->payloadDescriptor->hasTlIndex && this->payloadDescriptor->tlIndex == context->GetTargetTemporalLayer())
				{
					context->SetCurrentTemporalLayer(this->payloadDescriptor->tlIndex);
				}
				else if (!this->payloadDescriptor->hasTlIndex)
				{
					context->SetCurrentTemporalLayer(0);
				}

				if (context->GetCurrentTemporalLayer() > context->GetTargetTemporalLayer())
				{
					context->SetCurrentTemporalLayer(context->GetTargetTemporalLayer());
				}

				// Do not send tlIndex higher than current one.
				if (this->payloadDescriptor->tlIndex > context->GetCurrentTemporalLayer())
				{
					return false;
				}

				if (this->payloadDescriptor->hasPictureId && this->payloadDescriptor->hasTl0PictureIndex)
				{
					// Store the encoding data for retransmissions.
					this->payloadDescriptor->CreateEncoder({ pictureId, tl0PictureIndex });
					this->payloadDescriptor->Encode(packet->GetPayload());
				}

				return true;
			};

			void VP8::PayloadDescriptorHandler::Encode(
			  RTP::Packet* packet, Codecs::PayloadDescriptor::Encoder* encoder)
			{
				MS_TRACE();

				auto* vp8Encoder = static_cast<VP8::PayloadDescriptor::Encoder*>(encoder);

				vp8Encoder->Encode(packet->GetPayload(), this->payloadDescriptor.get());
			}

			void VP8::PayloadDescriptorHandler::Restore(RTP::Packet* packet)
			{
				MS_TRACE();

				if (this->payloadDescriptor->hasPictureId && this->payloadDescriptor->hasTl0PictureIndex)
				{
					this->payloadDescriptor->Restore(packet->GetPayload());
				}
			}
		} // namespace Codecs
	} // namespace RTP
} // namespace RTC