-
Notifications
You must be signed in to change notification settings - Fork 522
Expand file tree
/
Copy pathProgram.cs
More file actions
206 lines (174 loc) · 7.27 KB
/
Program.cs
File metadata and controls
206 lines (174 loc) · 7.27 KB
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
//-----------------------------------------------------------------------------
// Filename: Program.cs
//
// Description: An example of how to use a custom audio codec.
//
// Author(s):
// Aaron Clauson (aaron@sipsorcery.com)
//
// History:
// 27 Dec 2020 Aaron Clauson Created, Dublin, Ireland.
//
// License:
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
//-----------------------------------------------------------------------------
using System;
using System.Buffers;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using CommunityToolkit.HighPerformance;
using CommunityToolkit.HighPerformance.Buffers;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Extensions.Logging;
using SIPSorcery.Media;
using SIPSorcery.SIP;
using SIPSorcery.SIP.App;
using SIPSorceryMedia.Abstractions;
using SIPSorceryMedia.Windows;
namespace demo
{
class G729Codec : IAudioEncoder
{
private G729Decoder _g729Decoder;
private G729Encoder _g729Encoder;
private List<AudioFormat> _supportedFormats = new List<AudioFormat>
{
new AudioFormat(SDPWellKnownMediaFormatsEnum.G729)
};
public List<AudioFormat> SupportedFormats
{
get => _supportedFormats;
}
public G729Codec()
{
_g729Encoder = new G729Encoder();
_g729Decoder = new G729Decoder();
}
public void DecodeAudio(ReadOnlySpan<byte> encodedSample, AudioFormat format, IBufferWriter<short> destination)
{
using var buffer = new ArrayPoolBufferWriter<byte>(8192);
_g729Decoder.Process(encodedSample, buffer);
var pcm = buffer.WrittenSpan;
for (int i = 0; i < pcm.Length / 2; i++)
{
destination.Write(BinaryPrimitives.ReadInt16LittleEndian(pcm.Slice(i * 2, 2)));
}
}
public void EncodeAudio(ReadOnlySpan<short> pcm, AudioFormat format, IBufferWriter<byte> destination)
{
_g729Encoder.Process(MemoryMarshal.AsBytes(pcm), destination);
}
}
class Program
{
private static string DESTINATION = "time@sipsorcery.com";
static async Task Main()
{
Console.WriteLine("SIPSorcery Custom Audio Codec Demo");
AddConsoleLogger();
CancellationTokenSource exitCts = new CancellationTokenSource();
var sipTransport = new SIPTransport();
EnableTraceLogs(sipTransport);
var userAgent = new SIPUserAgent(sipTransport, null);
userAgent.ClientCallFailed += (uac, error, sipResponse) => Console.WriteLine($"Call failed {error}.");
userAgent.OnCallHungup += (dialog) => exitCts.Cancel();
var windowsAudio = new WindowsAudioEndPoint(new G729Codec());
windowsAudio.RestrictFormats(x => x.Codec == AudioCodecsEnum.G729);
var voipMediaSession = new VoIPMediaSession(windowsAudio.ToMediaEndPoints());
voipMediaSession.AcceptRtpFromAny = true;
// Place the call and wait for the result.
var callTask = userAgent.Call(DESTINATION, null, null, voipMediaSession);
Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e)
{
e.Cancel = true;
if (userAgent != null)
{
if (userAgent.IsCalling || userAgent.IsRinging)
{
Console.WriteLine("Cancelling in progress call.");
userAgent.Cancel();
}
else if (userAgent.IsCallActive)
{
Console.WriteLine("Hanging up established call.");
userAgent.Hangup();
}
}
;
exitCts.Cancel();
};
Console.WriteLine("press ctrl-c to exit...");
bool callResult = await callTask;
if (callResult)
{
Console.WriteLine($"Call to {DESTINATION} succeeded.");
exitCts.Token.WaitHandle.WaitOne();
}
else
{
Console.WriteLine($"Call to {DESTINATION} failed.");
}
Console.WriteLine("Exiting...");
if (userAgent?.IsHangingUp == true)
{
Console.WriteLine("Waiting 1s for the call hangup or cancel to complete...");
await Task.Delay(1000);
}
// Clean up.
sipTransport.Shutdown();
}
/// <summary>
/// Enable detailed SIP log messages.
/// </summary>
private static void EnableTraceLogs(SIPTransport sipTransport)
{
sipTransport.SIPRequestInTraceEvent += (localEP, remoteEP, req) =>
{
Console.WriteLine($"Request received: {localEP}<-{remoteEP}");
Console.WriteLine(req.ToString());
};
sipTransport.SIPRequestOutTraceEvent += (localEP, remoteEP, req) =>
{
Console.WriteLine($"Request sent: {localEP}->{remoteEP}");
Console.WriteLine(req.ToString());
};
sipTransport.SIPResponseInTraceEvent += (localEP, remoteEP, resp) =>
{
Console.WriteLine($"Response received: {localEP}<-{remoteEP}");
Console.WriteLine(resp.ToString());
};
sipTransport.SIPResponseOutTraceEvent += (localEP, remoteEP, resp) =>
{
Console.WriteLine($"Response sent: {localEP}->{remoteEP}");
Console.WriteLine(resp.ToString());
};
sipTransport.SIPRequestRetransmitTraceEvent += (tx, req, count) =>
{
Console.WriteLine($"Request retransmit {count} for request {req.StatusLine}, initial transmit {DateTime.Now.Subtract(tx.InitialTransmit).TotalSeconds.ToString("0.###")}s ago.");
};
sipTransport.SIPResponseRetransmitTraceEvent += (tx, resp, count) =>
{
Console.WriteLine($"Response retransmit {count} for response {resp.ShortDescription}, initial transmit {DateTime.Now.Subtract(tx.InitialTransmit).TotalSeconds.ToString("0.###")}s ago.");
};
}
/// <summary>
/// Adds a console logger. Can be omitted if internal SIPSorcery debug and warning messages are not required.
/// </summary>
private static Microsoft.Extensions.Logging.ILogger AddConsoleLogger()
{
var serilogLogger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Is(Serilog.Events.LogEventLevel.Debug)
.WriteTo.Console()
.CreateLogger();
var factory = new SerilogLoggerFactory(serilogLogger);
SIPSorcery.LogFactory.Set(factory);
return factory.CreateLogger<Program>();
}
}
}