-
Notifications
You must be signed in to change notification settings - Fork 3
/
BeamSender.cs
686 lines (623 loc) · 32.2 KB
/
BeamSender.cs
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
// SPDX-FileCopyrightText: © 2023 YorVeX, https://github.com/YorVeX
// SPDX-License-Identifier: MIT
using System.Buffers;
using System.Collections.Concurrent;
using System.IO.Pipes;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using K4os.Compression.LZ4;
using LibJpegTurbo;
using QoirLib;
using DensityApi;
using ObsInterop;
namespace xObsBeam;
public class BeamSender
{
public const int MaxFrameQueueSize = 5;
public const int DefaultPort = 13629;
readonly ConcurrentDictionary<string, BeamSenderClient> _clients = new();
readonly ConcurrentDictionary<string, NamedPipeServerStream> _pipeServers = new();
TcpListener _listener = new(IPAddress.Loopback, DefaultPort);
string _pipeName = "";
CancellationTokenSource _listenCancellationSource = new();
ArrayPool<byte>? _videoDataPool;
int _videoDataPoolMaxSize;
private int _videoFramesProcessed;
private int _videoFramesCompressed;
readonly Beam.SenderTypes _senderType;
Beam.VideoHeader _videoHeader;
Beam.AudioHeader _audioHeader;
Beam.VideoPlaneInfo _videoPlaneInfo;
Beam.VideoPlaneInfo _i420PlaneInfo;
int _audioPlanes;
int _audioBlockSize;
// cached compression settings
int _jpegCompressionQuality = 90;
TJSAMP _jpegSubsampling = TJSAMP.TJSAMP_444;
TJPF _jpegPixelFormat = TJPF.TJPF_RGB;
TJCS _jpegColorspace = TJCS.TJCS_RGB;
bool _jpegYuv;
bool _libJpegTurboV3;
private double _compressionThreshold = 1;
DENSITY_ALGORITHM _densityAlgorithm;
bool _compressionThreadingSync = true;
unsafe qoir_encode_options_struct* _qoirEncodeOptions = null;
readonly PeerDiscovery _discoveryServer = new();
public BeamSender(Beam.SenderTypes senderType)
{
_senderType = senderType;
}
public unsafe bool SetVideoParameters(BeamSenderProperties properties, video_format format, video_format conversionVideoFormat, uint width, uint height, uint fps_num, uint fps_den, byte full_range, float* color_matrix, float* color_range_min, float* color_range_max, uint* linesize, video_data._data_e__FixedBuffer data)
{
// reset some variables
_videoFramesProcessed = 0;
_videoFramesCompressed = 0;
_compressionThreshold = 1;
// allow potential previous compression buffers to be garbage collected
_videoDataPool = null;
// free previous options objects if they exist
if (_qoirEncodeOptions != null)
{
EncoderSupport.FreePooledPinned(_qoirEncodeOptions);
_qoirEncodeOptions = null;
}
if (conversionVideoFormat != video_format.VIDEO_FORMAT_NONE)
format = conversionVideoFormat;
// get the plane information for the current frame format and size
_videoPlaneInfo = Beam.GetVideoPlaneInfo(format, width, height);
if (_videoPlaneInfo.Count == 0) // unsupported format, will also be logged by the GetPlaneInfo() function
return false;
for (int planeIndex = 0; planeIndex < _videoPlaneInfo.Count; planeIndex++)
{
Module.Log($"SetVideoParameters(): {format} line size[{planeIndex}] = {linesize[planeIndex]}, plane size[{planeIndex}] = {_videoPlaneInfo.PlaneSizes[planeIndex]}, offset[{planeIndex}] = {_videoPlaneInfo.Offsets[planeIndex]}", ObsLogLevel.Debug);
// validate actual video data plane pointers against the GetPlaneSizes() information
var pointerOffset = (IntPtr)data.e0 + _videoPlaneInfo.Offsets[planeIndex];
if (pointerOffset != (IntPtr)data[planeIndex])
{
// either GetPlaneInfo() returned wrong information or the video data plane pointers are not contiguous in memory (which we currently rely on)
Module.Log($"Video data plane pointer for {format} plane {planeIndex} with resolution {width}x{height} has a difference of {pointerOffset - (IntPtr)data[planeIndex]} ({(IntPtr)data[planeIndex] - (IntPtr)data.e0} instead of {pointerOffset - (IntPtr)data.e0}).", ObsLogLevel.Warning);
}
}
// create the video header with current frame base info as a template for every frame - in most cases only the timestamp changes so that this instance can be reused without copies of it being created
var videoHeader = new Beam.VideoHeader()
{
Type = (_senderType == Beam.SenderTypes.FilterVideo ? Beam.Type.VideoOnly : Beam.Type.Video),
DataSize = (int)_videoPlaneInfo.DataSize,
Width = width,
Height = height,
Fps = fps_num,
FpsDenominator = fps_den,
Format = format,
FullRange = full_range,
Compression = Beam.CompressionTypes.None,
};
new ReadOnlySpan<float>(color_matrix, 16).CopyTo(new Span<float>(videoHeader.ColorMatrix, 16));
new ReadOnlySpan<float>(color_range_min, 3).CopyTo(new Span<float>(videoHeader.ColorRangeMin, 3));
new ReadOnlySpan<float>(color_range_max, 3).CopyTo(new Span<float>(videoHeader.ColorRangeMax, 3));
_videoHeader = videoHeader;
// cache compression settings
if (properties.QoirCompression && EncoderSupport.QoirLib)
{
_videoHeader.Compression = Beam.CompressionTypes.Qoir;
_videoDataPoolMaxSize = (int)((width * height * 4)); // this buffer will only be used if compression actually reduced the frame size
if (_videoDataPoolMaxSize > 2147483591) // maximum byte array size
_videoDataPoolMaxSize = 2147483591;
_videoDataPool = ArrayPool<byte>.Create(_videoDataPoolMaxSize, MaxFrameQueueSize);
_qoirEncodeOptions = EncoderSupport.MAllocPooledPinned<qoir_encode_options_struct>();
new Span<byte>(_qoirEncodeOptions, sizeof(qoir_encode_options_struct)).Clear();
// 0 = lossless - 1 loses 1 bit and results in 7 bit colors, 2 results in 6 bit colors, etc., even setting this to 7 is working, but for lossy both CPU load and compression ratio are worse than JPEG, so it's useless
_qoirEncodeOptions->lossiness = 0;
_qoirEncodeOptions->contextual_malloc_func = &EncoderSupport.QoirMAlloc; // important to use our own memory allocator, so that we can also free the memory later, also it's pooled memory
_qoirEncodeOptions->contextual_free_func = &EncoderSupport.QoirFree;
_compressionThreshold = properties.QoirCompressionLevel / 10.0;
}
else if (properties.JpegCompression && EncoderSupport.LibJpegTurbo)
{
_videoHeader.Compression = Beam.CompressionTypes.Jpeg;
_jpegCompressionQuality = properties.JpegCompressionQuality;
_videoDataPoolMaxSize = (int)_videoPlaneInfo.DataSize;
if (_videoDataPoolMaxSize > 2147483591) // maximum byte array size
_videoDataPoolMaxSize = 2147483591;
_videoDataPool = ArrayPool<byte>.Create(_videoDataPoolMaxSize, MaxFrameQueueSize);
_libJpegTurboV3 = EncoderSupport.LibJpegTurboV3;
_jpegPixelFormat = EncoderSupport.ObsToJpegPixelFormat(format);
_jpegSubsampling = EncoderSupport.ObsToJpegSubsampling(format);
_jpegColorspace = EncoderSupport.ObsToJpegColorSpace(format);
_jpegYuv = EncoderSupport.FormatIsYuv(format);
if (format == video_format.VIDEO_FORMAT_NV12) // NV12 is a special case, because it's already in YUV format, but the planes are interleaved
_i420PlaneInfo = Beam.GetVideoPlaneInfo(video_format.VIDEO_FORMAT_I420, width, height);
_compressionThreshold = properties.JpegCompressionLevel / 10.0;
}
else if (properties.DensityCompression && EncoderSupport.DensityApi)
{
_videoHeader.Compression = Beam.CompressionTypes.Density;
_densityAlgorithm = (DENSITY_ALGORITHM)properties.DensityCompressionStrength;
_videoDataPoolMaxSize = (int)Density.density_compress_safe_size((ulong)videoHeader.DataSize);
if (_videoDataPoolMaxSize > 2147483591) // maximum byte array size
_videoDataPoolMaxSize = 2147483591;
_videoDataPool = ArrayPool<byte>.Create(_videoDataPoolMaxSize, MaxFrameQueueSize);
_compressionThreshold = properties.DensityCompressionLevel / 10.0;
}
else if (properties.QoiCompression)
{
_videoHeader.Compression = Beam.CompressionTypes.Qoi;
_videoDataPoolMaxSize = (int)((width * height * 5)); // QOI's theoretical max size for BGRA is 5x the size of the original image
if (_videoDataPoolMaxSize > 2147483591) // maximum byte array size
_videoDataPoolMaxSize = 2147483591;
_videoDataPool = ArrayPool<byte>.Create(_videoDataPoolMaxSize, MaxFrameQueueSize);
_compressionThreshold = properties.QoiCompressionLevel / 10.0;
}
else if (properties.QoyCompression)
{
Qoy.Initialize();
_videoHeader.Compression = Beam.CompressionTypes.Qoy;
_videoDataPoolMaxSize = Qoy.GetMaxSize((int)width, (int)height);
if (_videoDataPoolMaxSize > 2147483591) // maximum byte array size
_videoDataPoolMaxSize = 2147483591;
_videoDataPool = ArrayPool<byte>.Create(_videoDataPoolMaxSize, MaxFrameQueueSize);
_compressionThreshold = properties.QoyCompressionLevel / 10.0;
}
else if (properties.Lz4Compression)
{
_videoHeader.Compression = Beam.CompressionTypes.Lz4;
_videoDataPoolMaxSize = LZ4Codec.MaximumOutputSize((int)_videoPlaneInfo.DataSize);
if (_videoDataPoolMaxSize > 2147483591) // maximum byte array size
_videoDataPoolMaxSize = 2147483591;
_videoDataPool = ArrayPool<byte>.Create(_videoDataPoolMaxSize, MaxFrameQueueSize);
_compressionThreshold = properties.Lz4CompressionLevel / 10.0;
}
_compressionThreadingSync = properties.CompressionMainThread;
var videoBandwidthMbps = (((Beam.VideoHeader.VideoHeaderDataSize + _videoPlaneInfo.DataSize) * (fps_num / fps_den)) / 1024 / 1024) * 8;
if (_videoHeader.Compression == Beam.CompressionTypes.None)
Module.Log($"Video output feed initialized, theoretical uncompressed net bandwidth demand is {videoBandwidthMbps} Mpbs.", ObsLogLevel.Info);
else
{
Module.Log($"Video output feed initialized with {_videoHeader.Compression} compression. Sync to render thread: {_compressionThreadingSync}. Theoretical uncompressed net bandwidth demand would be {videoBandwidthMbps} Mpbs.", ObsLogLevel.Info);
}
return true;
}
public unsafe bool VideoParametersChanged(video_format format, uint width, uint height, uint fps_num, uint fps_den, byte full_range, float* color_matrix, float* color_range_min, float* color_range_max)
{
var videoHeader = _videoHeader;
return ((_videoHeader.Width != width) ||
(_videoHeader.Height != height) ||
(_videoHeader.Fps != fps_num) ||
(_videoHeader.FpsDenominator != fps_den) ||
(_videoHeader.Format != format) ||
(_videoHeader.FullRange != full_range) ||
new ReadOnlySpan<float>(color_matrix, 16).SequenceEqual(new ReadOnlySpan<float>(videoHeader.ColorMatrix, 16)) == false ||
new ReadOnlySpan<float>(color_range_min, 3).SequenceEqual(new ReadOnlySpan<float>(videoHeader.ColorRangeMin, 3)) == false ||
new ReadOnlySpan<float>(color_range_max, 3).SequenceEqual(new ReadOnlySpan<float>(videoHeader.ColorRangeMax, 3)) == false
);
}
public unsafe void SetAudioParameters(audio_format format, speaker_layout speakers, uint samples_per_sec, uint frames)
{
Beam.GetAudioPlaneInfo(format, speakers, out _audioPlanes, out _audioBlockSize);
int audioDataSize = Beam.GetTotalAudioSize(format, speakers, frames);
_audioHeader = new Beam.AudioHeader()
{
Type = (_senderType == Beam.SenderTypes.FilterAudio ? Beam.Type.AudioOnly : Beam.Type.Audio),
DataSize = (audioDataSize * 2), // for filters there is some variance in the number of frames and therefore in the total data size, so we double it to be on the safe side (this field is used to determine the pool size)
Format = format,
SampleRate = samples_per_sec,
Speakers = speakers,
Frames = frames,
};
Module.Log($"{_audioHeader.Type} output feed initialized with {_audioPlanes} audio planes and a block size of {_audioBlockSize} bytes.", ObsLogLevel.Debug);
}
public unsafe bool AudioParametersChanged(audio_format format, speaker_layout speakers, uint samples_per_sec)
{
return ((_audioHeader.Format != format) ||
(_audioHeader.Speakers != speakers) ||
(_audioHeader.SampleRate != samples_per_sec)
);
}
public bool CanStart
{
get
{
if (_senderType is Beam.SenderTypes.Output or Beam.SenderTypes.FilterAudioVideo)
return ((_videoPlaneInfo.DataSize > 0) && (_audioBlockSize > 0));
if (_senderType == Beam.SenderTypes.FilterVideo)
return (_videoPlaneInfo.DataSize > 0);
if (_senderType == Beam.SenderTypes.FilterAudio)
return (_audioBlockSize > 0);
return false;
}
}
public async void Start(string identifier, IPAddress localAddr, int port, bool automaticPort)
{
if ((_senderType is Beam.SenderTypes.Output or Beam.SenderTypes.FilterAudioVideo or Beam.SenderTypes.FilterVideo) && (_videoPlaneInfo.DataSize == 0))
throw new InvalidOperationException("Video data size is unknown. Call SetVideoParameters() before calling Start().");
if ((_senderType is Beam.SenderTypes.Output or Beam.SenderTypes.FilterAudioVideo or Beam.SenderTypes.FilterAudio) && (_audioBlockSize == 0))
throw new InvalidOperationException("Audio data size is unknown. Call SetAudioParameters() before calling Start().");
int failCount = 0;
while (failCount < 10)
{
try
{
_listener = new TcpListener(localAddr, port);
_listener.Start();
}
catch (SocketException)
{
if (automaticPort)
{
failCount++;
Module.Log($"Failed to start TCP listener for {identifier} on {localAddr}:{port}, attempt {failCount} of 10.", ObsLogLevel.Debug);
continue;
}
else
{
Module.Log($"Failed to start TCP listener for {identifier} on {localAddr}:{port}, try configuring a different port or use a different interface.", ObsLogLevel.Error);
return;
}
}
try
{
_discoveryServer.StartServer(localAddr, port, _senderType, identifier);
break; // if we got here without exception the port is good
}
catch (SocketException)
{
try { _listener.Stop(); } catch { } // listening on the TCP port worked if we got here, so try to stop it again to try the next port
_discoveryServer.StopServer();
if (automaticPort)
{
failCount++;
Module.Log($"Failed to start UDP listener for {identifier} on {localAddr}:{port}, attempt {failCount} of 10.", ObsLogLevel.Debug);
continue;
}
else
{
Module.Log($"Failed to start UDP listener for {identifier} on {localAddr}:{port}, try configuring a different port or use a different interface.", ObsLogLevel.Error);
return;
}
}
}
if (failCount >= 10)
{
Module.Log($"Failed to start listener for {identifier} on {localAddr}:{port}, try configuring a static port or use a different interface.", ObsLogLevel.Error);
return;
}
Module.Log($"Listening on {localAddr}:{port}.", ObsLogLevel.Info);
if (!_listenCancellationSource.TryReset())
{
_listenCancellationSource.Dispose();
_listenCancellationSource = new CancellationTokenSource();
}
while (!_listenCancellationSource.Token.IsCancellationRequested)
{
try
{
Module.Log($"Waiting for new connections on {localAddr}:{port}.", ObsLogLevel.Debug);
var clientSocket = await _listener.AcceptSocketAsync(_listenCancellationSource.Token);
if (_listenCancellationSource.Token.IsCancellationRequested)
break;
if ((clientSocket != null) && (clientSocket.RemoteEndPoint != null))
{
string clientId = clientSocket.RemoteEndPoint.ToString()!;
Module.Log($"Socket client {clientId} accepted. Pending: {_listener.Pending()}, Data available: {clientSocket.Available}, Blocking: {clientSocket.Blocking}, Connected: {clientSocket.Connected}, DontFragment: {clientSocket.DontFragment}, Dual: {clientSocket.DualMode}, Broadcast: {clientSocket.EnableBroadcast}, Excl: {clientSocket.ExclusiveAddressUse}, Bound: {clientSocket.IsBound}, Linger: {clientSocket.LingerState!}, NoDelay: {clientSocket.NoDelay}, RecvBuff: {clientSocket.ReceiveBufferSize}, SendBuff: {clientSocket.SendBufferSize}, TTL: {clientSocket.Ttl}", ObsLogLevel.Debug);
var client = new BeamSenderClient(clientId, clientSocket, _videoHeader, _audioHeader);
client.Disconnected += ClientDisconnectedEventHandler;
client.Start();
_clients.AddOrUpdate(clientId, client, (key, oldClient) => client);
}
else
Module.Log($"Listener accepted socket but it was null.", ObsLogLevel.Warning);
}
catch (OperationCanceledException ex)
{
Module.Log($"Listener loop cancelled through {ex.GetType().Name}.", ObsLogLevel.Debug);
break;
}
catch (Exception ex)
{
Module.Log($"{ex.GetType().Name} in BeamSender.Start: {ex.Message}\n{ex.StackTrace}", ObsLogLevel.Error);
throw;
}
}
_listener.Stop();
Module.Log($"Listener stopped.", ObsLogLevel.Info);
}
public async void Start(string identifier, string pipeName)
{
if ((_senderType is Beam.SenderTypes.Output or Beam.SenderTypes.FilterAudioVideo or Beam.SenderTypes.FilterVideo) && (_videoPlaneInfo.DataSize == 0))
throw new InvalidOperationException("Video data size is unknown. Call SetVideoParameters() before calling Start().");
if ((_senderType is Beam.SenderTypes.Output or Beam.SenderTypes.FilterAudioVideo or Beam.SenderTypes.FilterAudio) && (_audioBlockSize == 0))
throw new InvalidOperationException("Audio data size is unknown. Call SetAudioParameters() before calling Start().");
_pipeName = pipeName;
var pipeStream = new NamedPipeServerStream(_pipeName, PipeDirection.InOut, 10, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
_discoveryServer.StartServer(_senderType, identifier);
Module.Log($"Listening on {_pipeName}.", ObsLogLevel.Info);
if (!_listenCancellationSource.TryReset())
{
_listenCancellationSource.Dispose();
_listenCancellationSource = new CancellationTokenSource();
}
while (!_listenCancellationSource.Token.IsCancellationRequested)
{
try
{
Module.Log($"Waiting for new connections on {_pipeName}.", ObsLogLevel.Debug);
await pipeStream.WaitForConnectionAsync(_listenCancellationSource.Token);
if (_listenCancellationSource.Token.IsCancellationRequested)
break;
// generate GUID for the client
string clientId = Guid.NewGuid().ToString();
Module.Log($"Pipe client {clientId} accepted.", ObsLogLevel.Debug);
// create a new BeamSenderClient
var client = new BeamSenderClient(clientId, pipeStream, _videoHeader, _audioHeader);
client.Disconnected += ClientDisconnectedEventHandler;
client.Start();
_clients.AddOrUpdate(clientId, client, (key, oldClient) => client);
// create a new pipe for the next client
pipeStream = new NamedPipeServerStream(_pipeName, PipeDirection.InOut, 10, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
}
catch (OperationCanceledException ex)
{
Module.Log($"Listener loop cancelled through {ex.GetType().Name}.", ObsLogLevel.Debug);
break;
}
catch (Exception ex)
{
Module.Log($"{ex.GetType().Name} in BeamSender.Start: {ex.Message}\n{ex.StackTrace}", ObsLogLevel.Error);
throw;
}
}
pipeStream.Close();
pipeStream.Dispose();
Module.Log($"Listener stopped.", ObsLogLevel.Info);
}
public void Stop()
{
try
{
Module.Log($"Stopping BeamSender...", ObsLogLevel.Debug);
_listenCancellationSource.Cancel();
foreach (var client in _clients.Values)
client.Disconnect(); // this will block for up to 1000 ms per client to try and get a clean disconnect
_audioPlanes = 0;
_audioBlockSize = 0;
_discoveryServer.StopServer();
Module.Log($"Stopped BeamSender.", ObsLogLevel.Debug);
}
catch (Exception ex)
{
Module.Log($"{ex.GetType().Name} in BeamReceiver.Stop: {ex.Message}\n{ex.StackTrace}", ObsLogLevel.Error);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void SendCompressed(ulong timestamp, Beam.VideoHeader videoHeader, byte* rawData, byte[]? encodedData)
{
try
{
int encodedDataLength = 0;
if (videoHeader.Compression is Beam.CompressionTypes.Jpeg) // apply JPEG compression if enabled
{
fixed (byte* jpegBuf = encodedData)
{
// nuint jpegDataLength = (nuint)(videoHeader.Width * TurboJpeg.tjPixelSize[(int)TJPF.TJPF_BGRA] * videoHeader.Height);
nuint jpegDataLength = (nuint)videoHeader.DataSize;
int compressResult;
void* turboJpegCompress; // needs to be recreated for every compression to be sure it's thread-safe, as stated here: https://github.com/libjpeg-turbo/libjpeg-turbo/issues/584
if (EncoderSupport.LibJpegTurboV3)
{
turboJpegCompress = TurboJpeg.tj3Init((int)TJINIT.TJINIT_COMPRESS);
_ = TurboJpeg.tj3Set(turboJpegCompress, (int)TJPARAM.TJPARAM_NOREALLOC, 1);
_ = TurboJpeg.tj3Set(turboJpegCompress, (int)TJPARAM.TJPARAM_COLORSPACE, (int)_jpegColorspace);
_ = TurboJpeg.tj3Set(turboJpegCompress, (int)TJPARAM.TJPARAM_SUBSAMP, (int)_jpegSubsampling);
_ = TurboJpeg.tj3Set(turboJpegCompress, (int)TJPARAM.TJPARAM_QUALITY, _jpegCompressionQuality);
}
else
turboJpegCompress = TurboJpeg.tjInitCompress();
if (_jpegYuv)
{
var planeInfo = _videoPlaneInfo;
if (videoHeader.Format == video_format.VIDEO_FORMAT_NV12)
planeInfo = _i420PlaneInfo; // packed format, was converted to I420 so that libjpeg-turbo can handle it
// set the pointers to the start of each plane
var planes = stackalloc byte*[planeInfo.Count];
for (int planeIndex = 0; planeIndex < planeInfo.Count; planeIndex++)
planes[planeIndex] = rawData + planeInfo.Offsets[planeIndex];
if (_libJpegTurboV3)
{
compressResult = TurboJpeg.tj3CompressFromYUVPlanes8(turboJpegCompress, planes, (int)videoHeader.Width, null, (int)videoHeader.Height, &jpegBuf, &jpegDataLength);
TurboJpeg.tj3Destroy(turboJpegCompress);
}
else
{
compressResult = TurboJpeg.tjCompressFromYUVPlanes(turboJpegCompress, planes, (int)videoHeader.Width, null, (int)videoHeader.Height, (int)_jpegSubsampling, &jpegBuf, &jpegDataLength, _jpegCompressionQuality, TurboJpeg.TJFLAG_NOREALLOC);
_ = TurboJpeg.tjDestroy(turboJpegCompress);
}
}
else
{
if (_libJpegTurboV3)
{
compressResult = TurboJpeg.tj3Compress8(turboJpegCompress, rawData, (int)videoHeader.Width, 0, (int)videoHeader.Height, (int)_jpegPixelFormat, &jpegBuf, &jpegDataLength);
TurboJpeg.tj3Destroy(turboJpegCompress);
}
else
{
compressResult = TurboJpeg.tjCompress2(turboJpegCompress, rawData, (int)videoHeader.Width, 0, (int)videoHeader.Height, (int)_jpegPixelFormat, &jpegBuf, &jpegDataLength, (int)_jpegSubsampling, _jpegCompressionQuality, TurboJpeg.TJFLAG_NOREALLOC);
_ = TurboJpeg.tjDestroy(turboJpegCompress);
}
}
encodedDataLength = (int)jpegDataLength;
if (compressResult != 0)
{
if (_libJpegTurboV3)
Module.Log("JPEG compression failed with error " + TurboJpeg.tj3GetErrorCode(turboJpegCompress) + ": " + Marshal.PtrToStringUTF8((IntPtr)TurboJpeg.tj3GetErrorStr(turboJpegCompress)), ObsLogLevel.Error);
else
Module.Log("JPEG compression failed with error " + TurboJpeg.tjGetErrorCode(turboJpegCompress) + ": " + Marshal.PtrToStringUTF8((IntPtr)TurboJpeg.tjGetErrorStr2(turboJpegCompress)), ObsLogLevel.Error);
return;
}
}
}
else if (videoHeader.Compression is Beam.CompressionTypes.Density) // apply Density compression if enabled
{
fixed (byte* densityBuf = encodedData)
{
var densityResult = Density.density_compress(rawData, (ulong)videoHeader.DataSize, densityBuf, (ulong)_videoDataPoolMaxSize, _densityAlgorithm);
if (densityResult.state != DENSITY_STATE.DENSITY_STATE_OK)
{
Module.Log("Density compression failed with error " + densityResult.state, ObsLogLevel.Error);
return;
}
encodedDataLength = (int)densityResult.bytesWritten;
}
}
else if (videoHeader.Compression is Beam.CompressionTypes.Qoir) // apply QOIR compression if enabled
{
var qoirPixelBuffer = EncoderSupport.MAllocPooledPinned<qoir_pixel_buffer_struct>();
qoirPixelBuffer->pixcfg.width_in_pixels = videoHeader.Width;
qoirPixelBuffer->pixcfg.height_in_pixels = videoHeader.Height;
qoirPixelBuffer->pixcfg.pixfmt = Qoir.QOIR_PIXEL_FORMAT__BGRA_NONPREMUL;
qoirPixelBuffer->data = rawData;
qoirPixelBuffer->stride_in_bytes = _videoPlaneInfo.Linesize[0];
var qoirEncodeResult = Qoir.qoir_encode(qoirPixelBuffer, _qoirEncodeOptions);
EncoderSupport.FreePooledPinned(qoirPixelBuffer);
if (qoirEncodeResult.status_message != null)
{
Module.Log("QOIR compression failed with error: " + Marshal.PtrToStringUTF8((IntPtr)qoirEncodeResult.status_message), ObsLogLevel.Error);
return;
}
if (qoirEncodeResult.dst_len == 0)
{
Module.Log("QOIR compression failed.", ObsLogLevel.Error);
return;
}
encodedDataLength = (int)qoirEncodeResult.dst_len;
new ReadOnlySpan<byte>(qoirEncodeResult.dst_ptr, encodedDataLength).CopyTo(encodedData);
EncoderSupport.FreePooledPinned(qoirEncodeResult.dst_ptr);
}
else if (videoHeader.Compression is Beam.CompressionTypes.Qoi) // apply QOI compression if enabled
encodedDataLength = Qoi.Encode(rawData, 0, videoHeader.DataSize, 4, encodedData!); // encode the frame with QOI
else if (videoHeader.Compression is Beam.CompressionTypes.Qoy) // apply QOY compression if enabled
encodedDataLength = Qoy.Encode(rawData, videoHeader.Width, videoHeader.Height, encodedData!); // encode the frame with QOY
else if (videoHeader.Compression is Beam.CompressionTypes.Lz4)
{
fixed (byte* targetData = encodedData)
encodedDataLength = LZ4Codec.Encode(rawData, videoHeader.DataSize, targetData, _videoDataPoolMaxSize, LZ4Level.L00_FAST);
}
if (encodedDataLength < videoHeader.DataSize) // did compression decrease the size of the data?
videoHeader.DataSize = encodedDataLength;
else
{
Module.Log(videoHeader.Compression + " compression did not decrease the size of the data, skipping frame " + timestamp, ObsLogLevel.Debug);
videoHeader.Compression = Beam.CompressionTypes.None; // send raw instead
}
if (videoHeader.Compression != Beam.CompressionTypes.None)
{
foreach (var client in _clients.Values)
client.EnqueueVideoFrame(timestamp, videoHeader, encodedData!);
}
else
{
foreach (var client in _clients.Values)
client.EnqueueVideoFrame(timestamp, videoHeader, rawData);
}
}
catch (Exception ex)
{
Module.Log($"{ex.GetType().Name} in sendCompressed(): {ex.Message}\n{ex.StackTrace}", ObsLogLevel.Error);
throw;
}
finally
{
// return the rented encoding data buffers to the pool, each client has created a copy of that data for its own use
if (encodedData != null)
_videoDataPool?.Return(encodedData);
}
}
public unsafe void SendVideo(ulong timestamp, byte* data)
{
if (_clients.IsEmpty)
return;
// make sure clients know the order of the frames, needs to be done here in a sync context
foreach (var client in _clients.Values)
client.EnqueueVideoTimestamp(timestamp);
// no compression of any kind, we also stay sync so just enqueue the raw data right away from the original unmanaged memory
if ((_videoHeader.Compression == Beam.CompressionTypes.None))
{
foreach (var client in _clients.Values)
client.EnqueueVideoFrame(timestamp, _videoHeader, data);
return;
}
bool compressThisFrame = ((_compressionThreshold == 1) || (((double)_videoFramesCompressed / _videoFramesProcessed) < _compressionThreshold));
// frame skipping logic
if (_compressionThreshold < 1) // is frame skipping enabled?
{
_videoFramesProcessed++;
if (compressThisFrame)
_videoFramesCompressed++;
else if (_videoFramesProcessed >= 10)
{
_videoFramesProcessed = 0;
_videoFramesCompressed = 0;
}
}
if (!compressThisFrame)
{
var videoHeader = _videoHeader;
videoHeader.Compression = Beam.CompressionTypes.None;
foreach (var client in _clients.Values)
client.EnqueueVideoFrame(timestamp, videoHeader, data);
return;
}
// --- continue here if compression is enabled ---
// prepare compression buffer
byte[]? encodedData = _videoDataPool!.Rent(_videoDataPoolMaxSize);
if (_compressionThreadingSync)
{
if ((_videoHeader.Compression is Beam.CompressionTypes.Jpeg) && (_videoHeader.Format == video_format.VIDEO_FORMAT_NV12)) //TODO: support deinterleaving for more packed formats: VIDEO_FORMAT_YVYU, VIDEO_FORMAT_YUY2, VIDEO_FORMAT_UYVY, VIDEO_FORMAT_AYUV, VIDEO_FORMAT_V210
{
byte[]? managedDataCopy = _videoDataPool!.Rent(_videoDataPoolMaxSize);
EncoderSupport.Nv12ToI420(data, managedDataCopy, _videoPlaneInfo, _i420PlaneInfo);
fixed (byte* videoData = managedDataCopy)
SendCompressed(timestamp, _videoHeader, videoData, encodedData);
_videoDataPool!.Return(managedDataCopy);
}
else
SendCompressed(timestamp, _videoHeader, data, encodedData); // in sync with this OBS render thread, hence the unmanaged data array and header instance stays valid and can directly be used
}
else
{
byte[]? managedDataCopy = _videoDataPool!.Rent(_videoDataPoolMaxSize); // need a managed memory copy for async handover
// to save an additional frame copy operation the JPEG deinterleaving is done in the sync part of the code with an implicit copy operation to managed memory that is needed anyway for the async handover
if ((_videoHeader.Compression is Beam.CompressionTypes.Jpeg) && (_videoHeader.Format == video_format.VIDEO_FORMAT_NV12)) //TODO: support deinterleaving for more packed formats: VIDEO_FORMAT_YVYU, VIDEO_FORMAT_YUY2, VIDEO_FORMAT_UYVY, VIDEO_FORMAT_AYUV, VIDEO_FORMAT_V210
EncoderSupport.Nv12ToI420(data, managedDataCopy, _videoPlaneInfo, _i420PlaneInfo);
else
new ReadOnlySpan<byte>(data, (int)_videoPlaneInfo.DataSize).CopyTo(managedDataCopy); // just copy to managed as-is for formats that are not packed
var beamVideoData = new Beam.BeamVideoData(_videoHeader, managedDataCopy, timestamp); // create a copy of the video header and data, so that the data can be used in the thread
Task.Factory.StartNew(state =>
{
var capturedBeamVideoData = (Beam.BeamVideoData)state!; // capture into thread-local context
fixed (byte* videoData = capturedBeamVideoData.Data)
SendCompressed(capturedBeamVideoData.Timestamp, capturedBeamVideoData.Header, videoData, encodedData);
_videoDataPool!.Return(capturedBeamVideoData.Data);
}, beamVideoData);
}
}
public unsafe void SendAudio(ulong timestamp, uint frames, byte* data)
{
// calulate the data size based on the frame count (stable for outputs, but for filters it can vary for every call)
int audioDataSize = _audioBlockSize * (int)frames;
// send the audio data to all currently connected clients
foreach (var client in _clients.Values)
client.EnqueueAudio(timestamp, frames, data, audioDataSize);
}
#region Event handlers
private unsafe void ClientDisconnectedEventHandler(object? sender, EventArgs e)
{
string clientId = ((BeamSenderClient)sender!).ClientId;
if (!_clients.TryRemove(clientId, out _))
Module.Log($"BeamSender.ClientDisconnectedEventHandler: Could not remove client from dictionary.", ObsLogLevel.Error);
if (_pipeServers.TryRemove(clientId, out NamedPipeServerStream? pipeServer))
pipeServer.Dispose();
}
#endregion Event handlers
}