-
Notifications
You must be signed in to change notification settings - Fork 522
Expand file tree
/
Copy pathProgram.cs
More file actions
113 lines (98 loc) · 4.33 KB
/
Program.cs
File metadata and controls
113 lines (98 loc) · 4.33 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
//-----------------------------------------------------------------------------
// Filename: Program.cs
//
// Description: Sample program of how to receive and incoming call and record it.
//
// Author(s):
// Aaron Clauson (aaron@sipsorcery.com)
//
// History:
// 26 Mar 2020 Aaron Clauson Created, Dublin, Ireland.
//
// License:
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
//-----------------------------------------------------------------------------
using System;
using System.Net;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Serilog;
using Serilog.Extensions.Logging;
using SIPSorcery.Media;
using SIPSorcery.Net;
using SIPSorcery.SIP;
using SIPSorcery.SIP.App;
using NAudio.Wave;
using SIPSorceryMedia.Abstractions;
using SIPSorceryMedia.Windows;
namespace demo
{
class Program
{
private static int SIP_LISTEN_PORT = 5060;
private static Microsoft.Extensions.Logging.ILogger Log = NullLogger.Instance;
private static readonly WaveFormat _waveFormat = new WaveFormat(8000, 16, 1);
private static WaveFileWriter _waveFile;
private static SIPTransport _sipTransport;
static void Main()
{
Console.WriteLine("SIPSorcery Getting Started Demo");
Log = AddConsoleLogger();
_waveFile = new WaveFileWriter("output.mp3", _waveFormat);
_sipTransport = new SIPTransport();
_sipTransport.AddSIPChannel(new SIPUDPChannel(new IPEndPoint(IPAddress.Any, SIP_LISTEN_PORT)));
var userAgent = new SIPUserAgent(_sipTransport, null, true);
userAgent.ServerCallCancelled += (uas, cancelReq) => Log.LogDebug("Incoming call cancelled by remote party.");
userAgent.OnCallHungup += (dialog) => _waveFile?.Close();
userAgent.OnIncomingCall += async (ua, req) =>
{
WindowsAudioEndPoint winAudioEP = new WindowsAudioEndPoint(new AudioEncoder());
VoIPMediaSession voipMediaSession = new VoIPMediaSession(winAudioEP.ToMediaEndPoints());
voipMediaSession.AcceptRtpFromAny = true;
voipMediaSession.OnRtpPacketReceived += OnRtpPacketReceived;
var uas = userAgent.AcceptCall(req);
await userAgent.Answer(uas, voipMediaSession);
};
Console.WriteLine("press any key to exit...");
Console.Read();
// Clean up.
_sipTransport.Shutdown();
}
private static void OnRtpPacketReceived(IPEndPoint remoteEndPoint, SDPMediaTypesEnum mediaType, RTPPacket rtpPacket)
{
if (mediaType == SDPMediaTypesEnum.audio)
{
var sample = rtpPacket.Payload;
for (int index = 0; index < sample.Length; index++)
{
if (rtpPacket.Header.PayloadType == (int)SDPWellKnownMediaFormatsEnum.PCMA)
{
short pcm = NAudio.Codecs.ALawDecoder.ALawToLinearSample(sample.Span[index]);
byte[] pcmSample = new byte[] { (byte)(pcm & 0xFF), (byte)(pcm >> 8) };
_waveFile.Write(pcmSample, 0, 2);
}
else
{
short pcm = NAudio.Codecs.MuLawDecoder.MuLawToLinearSample(sample.Span[index]);
byte[] pcmSample = new byte[] { (byte)(pcm & 0xFF), (byte)(pcm >> 8) };
_waveFile.Write(pcmSample, 0, 2);
}
}
}
}
/// <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>();
}
}
}