forked from sipsorcery-org/sipsorcery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
executable file
·233 lines (208 loc) · 9.38 KB
/
Program.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
//-----------------------------------------------------------------------------
// Filename: Program.cs
//
// Description: An abbreviated example program of how to use the SIPSorcery
// core library to place a SIP call. The example program depends on one audio
// input and one audio output being available.
//
// Author(s):
// Aaron Clauson ([email protected])
//
// History:
// 26 Oct 2019 Aaron Clauson Created, Dublin, Ireland.
// 26 Feb 2020 Aaron Clauson Switched RTP to use RtpAVSession.
//
// License:
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
//-----------------------------------------------------------------------------
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Serilog;
using Serilog.Extensions.Logging;
using Serilog.Events;
using SIPSorcery.Media;
using SIPSorcery.Net;
using SIPSorcery.SIP;
using SIPSorcery.SIP.App;
using SIPSorceryMedia.Abstractions;
using SIPSorceryMedia.Windows;
namespace demo
{
class Program
{
//private static readonly string DEFAULT_DESTINATION_SIP_URI = "sips:[email protected]";
private static readonly string DEFAULT_DESTINATION_SIP_URI = "sip:[email protected]";
private static Microsoft.Extensions.Logging.ILogger Log = NullLogger.Instance;
static void Main(string[] args)
{
Console.WriteLine("SIPSorcery client user agent example.");
Console.WriteLine("Press ctrl-c to exit.");
// Plumbing code to facilitate a graceful exit.
ManualResetEvent exitMre = new ManualResetEvent(false);
bool preferIPv6 = false;
bool isCallHungup = false;
bool hasCallFailed = false;
Log = AddConsoleLogger(LogEventLevel.Debug);
SIPURI callUri = SIPURI.ParseSIPURI(DEFAULT_DESTINATION_SIP_URI);
if (args?.Length > 0)
{
if (!SIPURI.TryParse(args[0], out callUri))
{
Log.LogWarning($"Command line argument could not be parsed as a SIP URI {args[0]}");
}
}
if(args?.Length > 1 && args[1] == "ipv6")
{
preferIPv6 = true;
}
if (preferIPv6)
{
Log.LogInformation($"Call destination {callUri}, preferencing IPv6.");
}
else
{
Log.LogInformation($"Call destination {callUri}.");
}
// Set up a default SIP transport.
var sipTransport = new SIPTransport();
sipTransport.PreferIPv6NameResolution = preferIPv6;
sipTransport.EnableTraceLogs();
var audioSession = new WindowsAudioEndPoint(new AudioEncoder());
//audioSession.RestrictFormats(x => x.Codec == AudioCodecsEnum.PCMA || x.Codec == AudioCodecsEnum.PCMU);
//audioSession.RestrictFormats(x => x.Codec == AudioCodecsEnum.PCMA);
var rtpSession = new VoIPMediaSession(audioSession.ToMediaEndPoints());
var offerSDP = rtpSession.CreateOffer(preferIPv6 ? IPAddress.IPv6Any : IPAddress.Any);
// Create a client user agent to place a call to a remote SIP server along with event handlers for the different stages of the call.
var uac = new SIPClientUserAgent(sipTransport);
uac.CallTrying += (uac, resp) => Log.LogInformation($"{uac.CallDescriptor.To} Trying: {resp.StatusCode} {resp.ReasonPhrase}.");
uac.CallRinging += async (uac, resp) =>
{
Log.LogInformation($"{uac.CallDescriptor.To} Ringing: {resp.StatusCode} {resp.ReasonPhrase}.");
if (resp.Status == SIPResponseStatusCodesEnum.SessionProgress)
{
if (resp.Body != null)
{
var result = rtpSession.SetRemoteDescription(SdpType.answer, SDP.ParseSDPDescription(resp.Body));
if (result == SetDescriptionResultEnum.OK)
{
await rtpSession.Start();
Log.LogInformation($"Remote SDP set from in progress response. RTP session started.");
}
}
}
};
uac.CallFailed += (uac, err, resp) =>
{
Log.LogWarning($"Call attempt to {uac.CallDescriptor.To} Failed: {err}");
hasCallFailed = true;
};
uac.CallAnswered += async (iuac, resp) =>
{
if (resp.Status == SIPResponseStatusCodesEnum.Ok)
{
Log.LogInformation($"{uac.CallDescriptor.To} Answered: {resp.StatusCode} {resp.ReasonPhrase}.");
if (resp.Body != null)
{
var result = rtpSession.SetRemoteDescription(SdpType.answer, SDP.ParseSDPDescription(resp.Body));
if (result == SetDescriptionResultEnum.OK)
{
await rtpSession.Start();
}
else
{
Log.LogWarning($"Failed to set remote description {result}.");
uac.Hangup();
}
}
else if(!rtpSession.IsStarted)
{
Log.LogWarning($"Failed to set get remote description in session progress or final response.");
uac.Hangup();
}
}
else
{
Log.LogWarning($"{uac.CallDescriptor.To} Answered: {resp.StatusCode} {resp.ReasonPhrase}.");
}
};
// The only incoming request that needs to be explicitly handled for this example is if the remote end hangs up the call.
sipTransport.SIPTransportRequestReceived += async (SIPEndPoint localSIPEndPoint, SIPEndPoint remoteEndPoint, SIPRequest sipRequest) =>
{
if (sipRequest.Method == SIPMethodsEnum.BYE)
{
SIPResponse okResponse = SIPResponse.GetResponse(sipRequest, SIPResponseStatusCodesEnum.Ok, null);
await sipTransport.SendResponseAsync(okResponse);
if (uac.IsUACAnswered)
{
Log.LogInformation("Call was hungup by remote server.");
isCallHungup = true;
exitMre.Set();
}
}
};
// Start the thread that places the call.
SIPCallDescriptor callDescriptor = new SIPCallDescriptor(
SIPConstants.SIP_DEFAULT_USERNAME,
null,
callUri.ToString(),
SIPConstants.SIP_DEFAULT_FROMURI,
callUri.CanonicalAddress,
null, null, null,
SIPCallDirection.Out,
SDP.SDP_MIME_CONTENTTYPE,
offerSDP.ToString(),
null);
uac.Call(callDescriptor, null);
// Ctrl-c will gracefully exit the call at any point.
Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e)
{
e.Cancel = true;
exitMre.Set();
};
// Wait for a signal saying the call failed, was cancelled with ctrl-c or completed.
exitMre.WaitOne();
Log.LogInformation("Exiting...");
rtpSession.Close(null);
if (!isCallHungup && uac != null)
{
if (uac.IsUACAnswered)
{
Log.LogInformation($"Hanging up call to {uac.CallDescriptor.To}.");
uac.Hangup();
}
else if (!hasCallFailed)
{
Log.LogInformation($"Cancelling call to {uac.CallDescriptor.To}.");
uac.Cancel();
}
// Give the BYE or CANCEL request time to be transmitted.
Log.LogInformation("Waiting 1s for call to clean up...");
Task.Delay(1000).Wait();
}
if (sipTransport != null)
{
Log.LogInformation("Shutting down SIP transport...");
sipTransport.Shutdown();
}
}
/// <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(
LogEventLevel logLevel = LogEventLevel.Debug)
{
var serilogLogger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Is(logLevel)
.WriteTo.Console()
.CreateLogger();
var factory = new SerilogLoggerFactory(serilogLogger);
SIPSorcery.LogFactory.Set(factory);
return factory.CreateLogger<Program>();
}
}
}