- Notifications
You must be signed in to change notification settings - Fork464
A WebRTC, SIP and VoIP library for C# and .NET. Designed for real-time communications apps.
License
sipsorcery-org/sipsorcery
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This fully C# library can be used to add Real-time Communications, typically audio and video calls, to .NET applications.
The diagram below is a high level overview of a Real-time audio and video call between Alice and Bob. It illustrates where theSIPSorcery
and associated libraries can help.
Supports both VoIP (get started) and WebRTC (get started).
Some of the protocols supported:
- Session Initiation Protocol(SIP),
- Real-time Transport Protocol(RTP),
- Web Real-time Communications(WebRTC),as of 26 Jan 2021 now an official IETF and W3C specification,
- Interactive Connectivity Establishment(ICE),
- SCTP, SDP, STUN and more.
Media End Points - Audio/Video Sinks and Sources:
The main
SIPSorcery
library does not provide access to audio and video devices or native codecs. Providing cross platform access to to these features on top of .NET is a large undertaking. A number of separate demonstration libraries show some different approaches to accessing audio/video devices and wrapping codecs with .NET.- SIPSorceryMedia.Windows: An example of a Windows specific library that provides audio capture and playback.
- SIPSorceryMedia.Encoders: An example of a Windows specific wrapper for theVP8 video codec.
- SIPSorceryMedia.FFmpeg: An example of a cross platform library that features audio and video codecs using PInvoke andFFmpeg.
- SIPSorceryMedia.SDL2: An example of integrating the cross-platformSDL2 Simple Direct Media Layer library.
This library provides only a small number of audio and video codecs (G711, G722 and G729). OPUS is available viaConcentus. Additional codecs, particularly video ones, require C or C++ libraries. An effort is underway to port theVP8 video codec to C# seeVP8.Net.
The library is should work with .NET Framework >= 4.6.1 and all .NET Core and .NET versions. The demo applications initially targetted .NET Core 3.1 and are updated to later .NET versions as time and interest permit. The library is available via NuGet.
dotnet add package SIPSorcery
With Visual Studio Package Manager Console (or search forSIPSorcery on NuGet):
Install-Package SIPSorcery
Class reference documentation and articles explaining common usage are available athttps://sipsorcery-org.github.io/sipsorcery/.
The simplest possible example to place an audio-only SIP call is shown below. This example relies on the Windows specificSIPSorceryMedia.Windows
library to play the received audio and only works on Windows (due to lack of .NET audio device support on non-Windows platforms).
dotnet new console --name SIPGetStarted --framework net8.0 --target-framework-override net8.0-windows10.0.17763.0cd SIPGetStarteddotnet add package SIPSorcerydotnet add package SIPSorceryMedia.Windows# Paste the code below into Program.cs.dotnet run# If successful you will hear a "Hello World" announcement.
stringDESTINATION="music@iptel.org";Console.WriteLine("SIP Get Started");varuserAgent=newSIPSorcery.SIP.App.SIPUserAgent();varwinAudio=newSIPSorceryMedia.Windows.WindowsAudioEndPoint(newSIPSorcery.Media.AudioEncoder());varvoipMediaSession=newSIPSorcery.Media.VoIPMediaSession(winAudio.ToMediaEndPoints());// Place the call and wait for the result.boolcallResult=awaituserAgent.Call(DESTINATION,null,null,voipMediaSession);Console.WriteLine($"Call result{(callResult?"success":"failure")}.");Console.WriteLine("Press any key to hangup and exit.");Console.ReadLine();
TheGetStarted example contains the full source and project file for the example above.
The three key classes in the above example are described in dedicated articles:
Theexamples folder contains sample code to demonstrate other common SIP/VoIP cases.
The WebRTC specifications do not include directions about how signaling should be done (for VoIP the signaling protocol is SIP; WebRTC has no equivalent). The example below uses a simple JSON message exchange over web sockets for signaling. Part of the reason theGetting Started WebRTC
is longer than theGetting Started VoIP
example is the need for custom signaling.
The example requires two steps:
- Run the
dotnet
console application, - Open an HTML page in a browser on the same machine.
The full project file and code are available atWebRTC Get Started.
The example relies on the Windows specificSIPSorceryMedia.Encoders
package, which is mainly a wrapper aroundlibvpx. Hopefully in the future there will be equivalent packages for other platforms.
Step 1:
dotnet new console --name WebRTCGetStartedcd WebRTCGetStarteddotnet add package SIPSorcerydotnet add package SIPSorceryMedia.Encoders# Paste the code below into Program.cs.dotnet run
usingSystem;usingSystem.Linq;usingSystem.Net;usingSystem.Threading.Tasks;usingSIPSorcery.Media;usingSIPSorcery.Net;usingSIPSorceryMedia.Encoders;usingWebSocketSharp.Server;namespacedemo{classProgram{privateconstintWEBSOCKET_PORT=8081;staticvoidMain(){Console.WriteLine("WebRTC Get Started");// Start web socket.Console.WriteLine("Starting web socket server...");varwebSocketServer=newWebSocketServer(IPAddress.Any,WEBSOCKET_PORT);webSocketServer.AddWebSocketService<WebRTCWebSocketPeer>("/",(peer)=>peer.CreatePeerConnection=()=>CreatePeerConnection());webSocketServer.Start();Console.WriteLine($"Waiting for web socket connections on{webSocketServer.Address}:{webSocketServer.Port}...");Console.WriteLine("Press any key exit.");Console.ReadLine();}privatestaticTask<RTCPeerConnection>CreatePeerConnection(){varpc=newRTCPeerConnection(null);vartestPatternSource=newVideoTestPatternSource(newVpxVideoEncoder());MediaStreamTrackvideoTrack=newMediaStreamTrack(testPatternSource.GetVideoSourceFormats(),MediaStreamStatusEnum.SendOnly);pc.addTrack(videoTrack);testPatternSource.OnVideoSourceEncodedSample+=pc.SendVideo;pc.OnVideoFormatsNegotiated+=(formats)=>testPatternSource.SetVideoSourceFormat(formats.First());pc.onconnectionstatechange+=async(state)=>{Console.WriteLine($"Peer connection state change to{state}.");switch(state){caseRTCPeerConnectionState.connected:awaittestPatternSource.StartVideo();break;caseRTCPeerConnectionState.failed:pc.Close("ice disconnection");break;caseRTCPeerConnectionState.closed:awaittestPatternSource.CloseVideo();testPatternSource.Dispose();break;}};returnTask.FromResult(pc);}}}
Step 2:
Create an HTML file, paste the contents below into it, open it in a browser that supports WebRTC and finally press thestart
button.
<!DOCTYPE html><head><scripttype="text/javascript">constWEBSOCKET_URL="ws://127.0.0.1:8081/"varpc,ws;asyncfunctionstart(){pc=newRTCPeerConnection();pc.ontrack=evt=>document.querySelector('#videoCtl').srcObject=evt.streams[0];pc.onicecandidate=evt=>evt.candidate&&ws.send(JSON.stringify(evt.candidate));ws=newWebSocket(document.querySelector('#websockurl').value,[]);ws.onmessage=asyncfunction(evt){varobj=JSON.parse(evt.data);if(obj?.candidate){pc.addIceCandidate(obj);}elseif(obj?.sdp){awaitpc.setRemoteDescription(newRTCSessionDescription(obj));pc.createAnswer().then((answer)=>pc.setLocalDescription(answer)).then(()=>ws.send(JSON.stringify(pc.localDescription)));}};};asyncfunctionclosePeer(){awaitpc?.close();awaitws?.close();};</script></head><body><videocontrolsautoplay="autoplay"id="videoCtl"width="640"height="480"></video><div><inputtype="text"id="websockurl"size="40"/><buttontype="button"class="btn btn-success"onclick="start();">Start</button><buttontype="button"class="btn btn-success"onclick="closePeer();">Close</button></div></body><script>document.querySelector('#websockurl').value=WEBSOCKET_URL;</script>
Result:
If successful the browser should display a test pattern image.
Theexamples folder contains sample code to demonstrate other common WebRTC cases.
About
A WebRTC, SIP and VoIP library for C# and .NET. Designed for real-time communications apps.