Skip to content

Commit 007701a

Browse files
Paulo Morgadopaulomorgado
authored andcommitted
Implement turns
NEW FEATURES: - turns implementation PERFORMANCE OPTIMIZATIONS: Memory Management: - Add PooledSegmentedBuffer<T> for efficient buffer pooling with ArrayPool<T> integration - Implement MemoryOperations helper for optimized memory operations - Add ValueStringBuilder for stack-allocated string building to reduce heap allocations - Introduce SearchValues for optimized span-based searching - Replace BufferUtils with more efficient memory operation primitives - Use NetEscapades.EnumGenerators to optimize enum usage. Socket and Network Layer: - Refactor NetServices with connection pooling and reduced allocations - Add SocketConnection abstraction with concrete implementations (SocketTcpConnection, SocketUdpConnection, SocketTlsConnection) for better resource management - Optimize IPSocket with span-based parsing and reduced string allocations - Enhance PortRange with improved allocation patterns System Utilities: - Add BinaryOperations for bit manipulation optimizations - Implement EncodingExtensions for efficient encoding/decoding operations - Add HashExtensions for streamlined hash computations Crypto and Security: - Refactor Crypto class with reduced allocations - Add SslClientAuthenticationOptions and SslStreamExtensions for down-level targets - Optimize CRC32 implementation Data Serialization: - Add SipSorceryJsonSerializerContext for AOT-friendly JSON serialization - Remove legacy JSONWriter and JsonParser in favor of System.Text.Json Protocol Implementations: - Optimize RTP/RTCP packet handling with span-based operations - Refactor SCTP chunks for reduced allocations - Improve SDP parsing with ValueStringBuilder - Optimize STUN attribute handling Codec Optimizations: - Extensive refactoring of codes to otpimize memory usage - Update AudioEncoder with more efficient buffer handling HIGH-PERFORMANCE LOGGING: Structured Logging Infrastructure: - Introduce LoggerMessage source generator pattern across all modules - Add domain-specific logging extensions for compile-time log message generation Benefits: - Zero-cost logging when log level is disabled - Compile-time string formatting - Reduced memory allocations in hot paths - Type-safe structured logging - Improved debuggability with consistent log patterns MODERNIZATION: - Standardize on .NET 10.0 and C# 14.0 - Used Meziantou.Polyfill for down-level targets - Added other polyfills to keep the source code clean of conditional compilation symbols. - Introduce BouncyCastleExtensions for down-level targets SUPPORTING CHANGES: Type System: - Refactor TypeExtensions - Add IByteSerializable interface for consistent serialization - Add ProtocolTypeExtensions for protocol type handling Testing: - Update existing tests to use new optimized APIs - Replaced Serilog with MartinCostello.Logging.XUnit
1 parent 1331f5a commit 007701a

File tree

1,700 files changed

+59548
-270321
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,700 files changed

+59548
-270321
lines changed

.editorconfig

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,28 @@ csharp_preserve_single_line_statements = true
2424
csharp_preserve_single_line_blocks = true
2525

2626
# Don't allow single line if/else blocks without braces.
27-
csharp_prefer_braces = true:error
27+
csharp_prefer_braces = true:error
28+
29+
# IDE0007: Use implicit type
30+
dotnet_diagnostic.IDE0007.severity = suggestion
31+
32+
# IDE0019: Use pattern matching
33+
dotnet_diagnostic.IDE0019.severity = suggestion
34+
35+
# IDE0030: Use coalesce expression
36+
dotnet_diagnostic.IDE0030.severity = suggestion
37+
38+
# IDE0031: Use null propagation
39+
dotnet_diagnostic.IDE0031.severity = suggestion
40+
41+
# IDE0078: Use pattern matching
42+
dotnet_diagnostic.IDE0078.severity = suggestion
43+
44+
# IDE0083: Use pattern matching
45+
dotnet_diagnostic.IDE0083.severity = suggestion
46+
47+
# Default severity for analyzer diagnostics with category 'Performance'
48+
dotnet_analyzer_diagnostic.category-Style.severity = suggestion
49+
50+
# Default severity for analyzer diagnostics with category 'Style'
51+
dotnet_analyzer_diagnostic.category-Performance.severity = suggestion
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# License
2+
3+
## 1. BSD 3-Clause "New" or "Revised" License
4+
5+
Copyright (c) 2020–2025 Aaron Clauson
6+
All rights reserved.
7+
8+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
9+
10+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
11+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
12+
3. Neither the name “SIP Sorcery,” nor “Aaron Clauson,” nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13+
14+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15+
16+
---
17+
18+
## 2. Additional Restriction: Boycott, Divestment, Sanctions (BDS) – Attribution-NonCommercial-ShareAlike
19+
20+
**Boycott Divestment Sanctions – Attribution-NonCommercial-ShareAlike (BDS BY-NC-SA)**
21+
22+
This software **must not be used** to further the Apartheid policies of Israel. Use, modification, or distribution **inside** Israel and the Occupied Territories is strictly forbidden until the demands of the Boycott, Divestment and Sanctions movement have been met:
23+
24+
* Israel has ended the occupation and colonization of all Arab lands occupied in 1967 and dismantled the Wall;
25+
* Arab-Palestinian citizens of Israel have been granted full equality; and
26+
* Palestinian refugees have obtained the right to return to their homes and properties as stipulated in UN Resolution 194.
27+
28+
For all other geographic regions **outside** of Israel and the Occupied Territories, use, modification, and distribution are permitted under the terms of the **BSD 3-Clause "New" or "Revised" License** above (Section 1), provided that any future use, modification, or distribution carries the above BDS restriction and abides by the ShareAlike and NonCommercial principles.
29+
30+
This restriction is **not** intended to limit the rights of Israelis or any other people residing outside of Israel and the Occupied Territories.
31+
32+
In any instance where the BSD 3-Clause License conflicts with the above restriction, the above restriction shall be interpreted as superior, and all other non-conflicting provisions of the BSD 3-Clause license shall remain in effect.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SIPSorceryMedia.Abstractions
2+
3+
This project provides the logic for the interfaces required by the [SIPSorcery](https://github.com/sipsorcery-org/sipsorcery) real-time communications library and the components that provide functions such as:
4+
5+
- Access to audio or video devices (example [SIPSorceryMedia.Windows](https://github.com/sipsorcery-org/SIPSorceryMedia.Windows)).
6+
- Access to codecs from native libraries (examples [SIPSorceryMedia.Encoders](https://github.com/sipsorcery-org/SIPSorceryMedia.Encoders) and [SIPSorceryMedia.FFmpeg](https://github.com/sipsorcery-org/SIPSorceryMedia.FFmpeg)).
7+
8+
# Important Interfaces
9+
10+
The most important interfacs contained in this library are:
11+
12+
- IAudioEncoder: Needs to be implemented by classes that provide audio decoding and/or encoding. An example is the [AudioEncoder](https://github.com/sipsorcery-org/sipsorcery/blob/master/src/app/Media/Codecs/AudioEncoder.cs) class.
13+
14+
- IVideoEncoder: Needs to be implemented by classes that provide video decoding and/or encoding. An example is the [VpxVideoEncoder](https://github.com/sipsorcery-org/SIPSorceryMedia.Encoders/blob/master/src/VpxVideoEncoder.cs#L25) class.
15+
16+
- IAudioSource: Needs to be implemented by classes that act as a source of raw audio samples. Typically a microphone. An example is the [WindowsAudioEndPoint](https://github.com/sipsorcery-org/SIPSorceryMedia.Windows/blob/master/src/WindowsAudioEndPoint.cs#L32) class.
17+
18+
- IAudioSink: Needs to be implemented by classes that act as a sink for raw audio samples. Typically an audio speaker. An example is the [WindowsAudioEndPoint](https://github.com/sipsorcery-org/SIPSorceryMedia.Windows/blob/master/src/WindowsAudioEndPoint.cs#L32) class.
19+
20+
- IVideoSource: Needs to be implemented by classes that act as a source of raw video frames. Typically a webcam. An examples is the [WindowsVideoEndPoint](https://github.com/sipsorcery-org/SIPSorceryMedia.Windows/blob/master/src/WindowsVideoEndPoint.cs#L48).
21+
22+
- IVideoSink: Needs to be implemented by classes that act as a sink for raw video frames. The video sink is usually a bitmap or some kind of graphics surface. An examples is the [WindowsVideoEndPoint](https://github.com/sipsorcery-org/SIPSorceryMedia.Windows/blob/master/src/WindowsVideoEndPoint.cs#L48).
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//-----------------------------------------------------------------------------
2+
// Filename: IAudioEncoder.cs
3+
//
4+
// Description: Common interface for an audio codec.
5+
//
6+
// Author(s):
7+
// Aaron Clauson (aaron@sipsorcery.com)
8+
//
9+
// History:
10+
// 20 May 2025 Aaron Clauson Refactored from MediaEndPoints.
11+
//
12+
// License:
13+
// BSD 3-Clause "New" or "Revised" License and the additional
14+
// BDS BY-NC-SA restriction, see included LICENSE.md file.
15+
//-----------------------------------------------------------------------------
16+
17+
using System;
18+
using System.Buffers;
19+
using System.Collections.Generic;
20+
21+
namespace SIPSorceryMedia.Abstractions;
22+
23+
public interface IAudioEncoder
24+
{
25+
/// <summary>
26+
/// Needs to be set with the list of audio formats that the encoder supports.
27+
/// </summary>
28+
List<AudioFormat> SupportedFormats { get; }
29+
30+
/// <summary>
31+
/// Encodes 16bit signed PCM samples.
32+
/// </summary>
33+
/// <param name="pcm">An array of 16 bit signed audio samples.</param>
34+
/// <param name="format">The audio format to encode the PCM sample to.</param>
35+
/// <param name="destination">A <see cref="IBufferWriter{T}"/> of <see langword="byte"/> to receieve the encoded sample.</param>
36+
void EncodeAudio(ReadOnlySpan<short> pcm, AudioFormat format, IBufferWriter<byte> destination);
37+
38+
/// <summary>
39+
/// Decodes to 16bit signed PCM samples.
40+
/// </summary>
41+
/// <param name="encodedSample">The span containing the encoded sample.</param>
42+
/// <param name="format">The audio format of the encoded sample.</param>
43+
/// <param name="destination">A <see cref="IBufferWriter{T}"/> of <see langword="short"/> to receive the decoded PCM samples.</param>
44+
void DecodeAudio(ReadOnlySpan<byte> encodedSample, AudioFormat format, IBufferWriter<short> destination);
45+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//-----------------------------------------------------------------------------
2+
// Filename: ITextEncoder.cs
3+
//
4+
// Description: Common interface for a text codec.
5+
//
6+
// Author(s):
7+
// Aaron Clauson (aaron@sipsorcery.com)
8+
//
9+
// History:
10+
// 20 May 2025 Aaron Clauson Refactored from MediaEndPoints.
11+
//
12+
// License:
13+
// BSD 3-Clause "New" or "Revised" License and the additional
14+
// BDS BY-NC-SA restriction, see included LICENSE.md file.
15+
//-----------------------------------------------------------------------------
16+
17+
namespace SIPSorceryMedia.Abstractions;
18+
19+
public interface ITextEncoder
20+
{
21+
/// <summary>
22+
/// Encode a text into a byte array.
23+
/// </summary>
24+
/// <param name="text">A symbol or text to be transmitted</param>
25+
/// <param name="format">The text format of the sample.</param>
26+
/// <returns>A byte array containing the encoded text sample</returns>
27+
byte[] EncodeText(char[] text, TextFormat format);
28+
29+
/// <summary>
30+
/// Decode a byte array into a string type text.
31+
/// </summary>
32+
/// <param name="encodedSample">A symbol or text that was received</param>
33+
/// <param name="format">The text format of the sample.</param>
34+
/// <returns></returns>
35+
char[] DecodeText(byte[] encodedSample, TextFormat format);
36+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//-----------------------------------------------------------------------------
2+
// Filename: IVideoEncoder.cs
3+
//
4+
// Description: Common interface for a video codec.
5+
//
6+
// Author(s):
7+
// Aaron Clauson (aaron@sipsorcery.com)
8+
//
9+
// History:
10+
// 20 May 2025 Aaron Clauson Refactored from MediaEndPoints.
11+
//
12+
// License:
13+
// BSD 3-Clause "New" or "Revised" License and the additional
14+
// BDS BY-NC-SA restriction, see included LICENSE.md file.
15+
//-----------------------------------------------------------------------------
16+
17+
using System;
18+
using System.Collections.Generic;
19+
20+
namespace SIPSorceryMedia.Abstractions;
21+
22+
public interface IVideoEncoder : IDisposable
23+
{
24+
/// <summary>
25+
/// Needs to be set with the list of video formats that the encoder supports.
26+
/// </summary>
27+
List<VideoFormat> SupportedFormats { get; }
28+
29+
byte[] EncodeVideo(int width, int height, byte[] sample, VideoPixelFormatsEnum pixelFormat, VideoCodecsEnum codec);
30+
31+
byte[] EncodeVideoFaster(RawImage rawImage, VideoCodecsEnum codec); // Avoid to use byte[] to improve performance
32+
33+
void ForceKeyFrame();
34+
35+
IEnumerable<VideoSample> DecodeVideo(byte[] encodedSample, VideoPixelFormatsEnum pixelFormat, VideoCodecsEnum codec);
36+
37+
IEnumerable<RawImage> DecodeVideoFaster(byte[] encodedSample, VideoPixelFormatsEnum pixelFormat, VideoCodecsEnum codec); // Avoid to use byte[] to improve performance
38+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//-----------------------------------------------------------------------------
2+
// Filename: RawImage.cs
3+
//
4+
// Description: A raw image for use with a video codec.
5+
//
6+
// Author(s):
7+
// Aaron Clauson (aaron@sipsorcery.com)
8+
//
9+
// History:
10+
// 20 May 2025 Aaron Clauson Refactored from MediaEndPoints.
11+
//
12+
// License:
13+
// BSD 3-Clause "New" or "Revised" License and the additional
14+
// BDS BY-NC-SA restriction, see included LICENSE.md file.
15+
//-----------------------------------------------------------------------------
16+
17+
using System;
18+
using System.Runtime.InteropServices;
19+
20+
namespace SIPSorceryMedia.Abstractions;
21+
22+
public class RawImage
23+
{
24+
/// <summary>
25+
/// The width, in pixels, of the image
26+
/// </summary>
27+
public int Width { get; set; }
28+
29+
/// <summary>
30+
/// The height, in pixels, of the image
31+
/// </summary>
32+
public int Height { get; set; }
33+
34+
/// <summary>
35+
/// Integer that specifies the byte offset between the beginning of one scan line and the next.
36+
/// </summary>
37+
public int Stride { get; set; }
38+
39+
/// <summary>
40+
/// Pointer to an array of bytes that contains the pixel data.
41+
/// </summary>
42+
public nint Sample { get; set; }
43+
44+
/// <summary>
45+
/// The pixel format of the image
46+
/// </summary>
47+
public VideoPixelFormatsEnum PixelFormat { get; set; }
48+
49+
/// <summary>
50+
/// Get bytes array of the image.
51+
///
52+
/// For performance reasons it's better to use directly Sample
53+
/// </summary>
54+
/// <returns></returns>
55+
public byte[]? GetBuffer()
56+
{
57+
byte[]? result = null;
58+
59+
if ((Height > 0) && (Stride > 0))
60+
{
61+
var bufferSize = Height * Stride;
62+
63+
result = new byte[bufferSize];
64+
Marshal.Copy(Sample, result, 0, bufferSize);
65+
}
66+
return result;
67+
}
68+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//-----------------------------------------------------------------------------
2+
// Filename: VideoSample.cs
3+
//
4+
// Description: Representation of an unencoded video sample.
5+
//
6+
// Author(s):
7+
// Aaron Clauson (aaron@sipsorcery.com)
8+
//
9+
// History:
10+
// 20 May 2025 Aaron Clauson Refactored from MediaEndPoints.
11+
//
12+
// License:
13+
// BSD 3-Clause "New" or "Revised" License and the additional
14+
// BDS BY-NC-SA restriction, see included LICENSE.md file.
15+
//-----------------------------------------------------------------------------
16+
17+
namespace SIPSorceryMedia.Abstractions;
18+
19+
public struct VideoSample
20+
{
21+
public uint Width;
22+
public uint Height;
23+
public byte[] Sample;
24+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//-----------------------------------------------------------------------------
2+
// Filename: AudioCodecsEnum.cs
3+
//
4+
// Description: Enum for common audio codecs.
5+
//
6+
// Author(s):
7+
// Aaron Clauson (aaron@sipsorcery.com)
8+
//
9+
// History:
10+
// 20 May 2025 Aaron Clauson Refactored from MediaEndPoints.
11+
//
12+
// License:
13+
// BSD 3-Clause "New" or "Revised" License and the additional
14+
// BDS BY-NC-SA restriction, see included LICENSE.md file.
15+
//-----------------------------------------------------------------------------
16+
17+
namespace SIPSorceryMedia.Abstractions;
18+
19+
public enum AudioCodecsEnum
20+
{
21+
Unknown,
22+
23+
PCMU,
24+
GSM,
25+
G723,
26+
DVI4,
27+
LPC,
28+
PCMA,
29+
G722,
30+
L16,
31+
QCELP,
32+
CN,
33+
MPA,
34+
G728,
35+
G729,
36+
OPUS,
37+
38+
PCM_S16LE, // PCM signed 16-bit little-endian (equivalent to FFmpeg s16le). For use with Azure, not likely to be supported in VoIP/WebRTC.
39+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//-----------------------------------------------------------------------------
2+
// Filename: AudioSamplingRatesEnum.cs
3+
//
4+
// Description: Enum for common audio sampling rates.
5+
//
6+
// Author(s):
7+
// Aaron Clauson (aaron@sipsorcery.com)
8+
//
9+
// History:
10+
// 20 May 2025 Aaron Clauson Refactored from MediaEndPoints.
11+
//
12+
// License:
13+
// BSD 3-Clause "New" or "Revised" License and the additional
14+
// BDS BY-NC-SA restriction, see included LICENSE.md file.
15+
//-----------------------------------------------------------------------------
16+
17+
namespace SIPSorceryMedia.Abstractions;
18+
19+
public enum AudioSamplingRatesEnum
20+
{
21+
Rate8KHz = 8000,
22+
Rate16KHz = 16000,
23+
Rate24kHz = 24000,
24+
Rate44_1kHz = 44100,
25+
Rate48kHz = 48000
26+
}

0 commit comments

Comments
 (0)