Git Product home page Git Product logo

sipsorcerymedia.sdl2's Introduction

SIPSorceryMedia.SDL2

This project is an example of developing a C# library that can use features from SDL2 native libraries and that integrates with the SIPSorcery real-time communications library.

The classes in this project provide Audio End Point and Audio Source features.

So used in correlation with SIPSorceryMedia.FFMpeg you have both Audio and Video End Point and Audio and Video Source using multi-platform component.

Using both you have these features:

  • Video Input:

    • using local file or remote using URI [With SIPSorceryMedia.FFMpeg]
    • using camera [With SIPSorceryMedia.FFMpeg]
    • using screen [With SIPSorceryMedia.FFMpeg]
  • Audio Input:

    • using local file or remote using URI [With SIPSorceryMedia.FFMpeg]
    • using microphone [With SIPSorceryMedia.FFMpeg or SIPSorceryMedia.SDL2]
  • Audio Ouput:

    • using a speaker [With SIPSorceryMedia.SDL2]

Audio Codecs supported by this library: (using AudioEncoder from SIPSorcery )

  • PCMU
  • PCMA
  • G722
  • G729 (not tested in SIP Context but should work)
  • Opus (see CheckCodec example which implements Opus Codec using Concentus (C# implementation)

Installing SDL2

For Windows

No additional steps are required for an x64 build. The nuget package includes the SDL v2.0.20 x64 binaries.

For Mac

Install the DMG file available here

For Linux

Install the SDL binaries using the package manager for the distribution.

sudo apt-get install libsdl2

Testing

Several projects permits to understand how the library can be used:

  • PlayAudioFile - Multiplatform application:

    • Let user select an Audio Playback device
    • Play Audio file
  • PlayVideoFile - Multiplatform application:

    • Let user select an Audio Playback device
    • Play Video file (Display it in ASCII in a Console/Terminal Window)
  • CheckCodec - Multiplatform application:

    • Let user select an Audio Playback device
    • Let user select an Audio Recording device OR a Video File
    • Let user select one of the Audio Codecs supported
    • Play back the sound after Encoding then Decoding the sample using the specified codec

sipsorcerymedia.sdl2's People

Contributors

christophei avatar sipsorcery avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

sipsorcerymedia.sdl2's Issues

Exception "Cannot get the audio sending format, missing either local or remote audio track." in _audioSource.OnAudioSourceEncodedSample Event

My setup code is

_audioEndPoint = new SDL2AudioEndPoint(AudioOutputName, _audioEncoder);
_audioEndPoint.SetAudioSinkFormat(_audioFormat.Value);

int frameSize = _audioEncoder.GetFrameSize();
_audioSource = new SDL2AudioSource(AudioInputName, _audioEncoder, (uint)frameSize);
_audioSource.SetAudioSourceFormat(_audioFormat.Value);

MediaStreamTrack audioInputTrack = new MediaStreamTrack(_audioSource.GetAudioSourceFormats(),
MediaStreamStatusEnum.SendRecv);
_peerConnection.addTrack(audioInputTrack);

_peerConnection.OnAudioFormatsNegotiated += (audioFormats) =>
{
_audioSource.SetAudioSourceFormat(audioFormats.First());
_audioEndPoint.SetAudioSinkFormat(audioFormats.First());
};

_audioSource.OnAudioSourceEncodedSample += _audioSource_OnAudioSourceEncodedSample;
_peerConnection.OnRtpPacketReceived += _peerConnection_OnRtpPacketReceived;

private void _audioSource_OnAudioSourceEncodedSample (uint durationRtpUnits, byte[] sample)
{
_peerConnection.SendAudio(durationRtpUnits, sample);
}

Sometime I am getting error from _peerConnection.SendAudio(durationRtpUnits, sample) and Error is "Cannot get the audio sending format, missing either local or remote audio track.". But not always.

Would you please help me in this regard.

SDL2 is no longer included with the Nuget package on Windows

I had to manually download SDL2.dll and copy it to the same directory as my application in order for the library to be properly found by the SipSorceryMedia functions.

Otherwise I received the following error:
Unhandled exception: System.DllNotFoundException: Unable to load DLL 'SDL2' or one of its dependencies: The specified module could not be found. (0x8007007E)

Am I in a right track to setup audio input and Output with Sipsorcery WebRTC lib

_audioSource = new SDL2AudioSource(AudioInputName, new AudioEncoder());
_audioSink = new SDL2AudioEndPoint(AudioOutputName, new AudioEncoder());

        MediaStreamTrack audioInputTrack = new MediaStreamTrack(_audioSource.GetAudioSourceFormats(),
            MediaStreamStatusEnum.SendOnly);
        _peerConnection.addTrack(audioInputTrack);

        _audioSource.OnAudioSourceEncodedSample += _peerConnection.SendAudio;
        _peerConnection.OnRtpPacketReceived += (IPEndPoint rep, SDPMediaTypesEnum media, RTPPacket rtpPkt) =>
        {
            if (media == SDPMediaTypesEnum.audio)
            {
                _audioSink.GotAudioRtp(rep, rtpPkt.Header.SyncSource,
                    rtpPkt.Header.SequenceNumber, rtpPkt.Header.Timestamp,
                    rtpPkt.Header.PayloadType, rtpPkt.Header.MarkerBit == 1,
                    rtpPkt.Payload);
            }
        };

        _peerConnection.OnAudioFormatsNegotiated += (audioFormats) =>
        {
            _audioSource.SetAudioSourceFormat(audioFormats.First());
            _audioSink.SetAudioSinkFormat(audioFormats.First());
        };

Getting exception when creating SDL2AudioEndPoint instance when SIPSorcery package is installed.

I was trying to use SIPSorceryMedia.SDL2 library in a simple console application:

using SIPSorcery.Media;
using SIPSorceryMedia.SDL2;

SDL2Helper.InitSDL();
SDL2AudioEndPoint endpoint = new("Speakers (Realtek High Definition Audio)", new AudioEncoder());

but on the fifth line I get this exception:

System.MissingMethodException
HResult=0x80131513
Message=Method not found: 'Microsoft.Extensions.Logging.ILogger SIPSorcery.LogFactory.CreateLogger()'.
Source=SIPSorceryMedia.SDL2
StackTrace:
at SIPSorceryMedia.SDL2.SDL2AudioEndPoint..ctor(String audioOutDeviceName, IAudioEncoder audioEncoder)
at Program.Main(String[] args) in C:\Users<UserName>\source\repos\ConsoleApp4\ConsoleApp4\Program.cs:line 5

I am using .NET 8, and I tried both SDL2-2.0.20-win32-x64 (as mentioned in the README) and SDL-2.30.5-win32-x64 (lates version)
I have also tried .NET 9.0-Preview and I have tried to install Microsoft.Extensions.Logging, Microsoft.Extensions.Logging.Console and I got the same exception on the same line.

Edit:

The Exception is only thrown when SIPSorcery package is installed. when I remove it no exception is thrown.

sending opus encoded audio with webrtc

Hi
I am trying to capture windows desktop sound using WasapiLoopbackCapture
waveIn = new WasapiLoopbackCapture();
waveIn.WaveFormat = new WaveFormat(48000 , 16 , 1);
waveIn.DataAvailable += LocalAudioSampleAvailable;
and I want to encode the received buffers in LocalAudioSampleAvailable with opus audio encoder , so I made some changes in WindowsAudioEndPoint
https://github.com/sipsorcery-org/SIPSorceryMedia.Windows/blob/master/src/WindowsAudioEndPoint.cs
WindowsAudioEndPoint audioSource = new WindowsAudioEndPoint(new OpusAudioEncoder());
audioSource.RestrictFormats(format => format.Codec == AudioCodecsEnum.OPUS);
but when I run my app , I get error in "chrome and Edge"
"Uncaught (in promise) DOMException: Failed to execute 'setRemoteDescription' on 'RTCPeerConnection': Failed to set remote offer sdp: Failed to set remote audio description send parameters for m-section with mid='0'."
I fixed that by changing this line inside "OpusAudioEncoder" class
https://github.com/sipsorcery-org/SIPSorceryMedia.SDL2/blob/main/test/CheckCodec/OpusAudioEncoder.cs
public static readonly AudioFormat MEDIA_FORMAT_OPUS = new AudioFormat(111, "opus", SAMPLE_RATE, SAMPLE_RATE, 2, "a=fmtp:111 minptime=10;useinbandfec=1");
with this line
public static readonly AudioFormat MEDIA_FORMAT_OPUS = new AudioFormat(AudioCodecsEnum.OPUS, 111, SAMPLE_RATE, SAMPLE_RATE, 2, "a=fmtp:111 minptime=10;useinbandfec=1");
the problem is , I cant get the sound at the end correctly and I don't know why , the following code shows how I am encoding the buffers when I get them from WasapiLoopbackCapture inside LocalAudioSampleAvailable

private void LocalAudioSampleAvailable(object sender, WaveInEventArgs args)
        {
            // Note NAudio.Wave.WaveBuffer.ShortBuffer does not take into account little endian.
            // https://github.com/naudio/NAudio/blob/master/NAudio/Wave/WaveOutputs/WaveBuffer.cs
            //WaveBuffer wavBuffer = new WaveBuffer(args.Buffer.Take(args.BytesRecorded).ToArray());
            //byte[] encodedSample = _audioEncoder.EncodeAudio(wavBuffer.ShortBuffer, _audioFormatManager.SelectedFormat);
            try
            {
                byte[] buffer = args.Buffer.Take(args.BytesRecorded).ToArray();
                short[] pcm = buffer.Where((x, i) => i % 2 == 0).Select((y, i) => BitConverter.ToInt16(buffer, i * 2)).ToArray();
                byte[] encodedSample = _audioEncoder.EncodeAudio(pcm, _audioFormatManager.SelectedFormat);

                //OnAudioSourceEncodedSample?.Invoke((uint)_audioFormatManager.SelectedFormat.RtpClockRate / 1000 * AUDIO_SAMPLE_PERIOD_MILLISECONDS, encodedSample);
                OnAudioSourceEncodedSample?.Invoke((uint)encodedSample.Length, encodedSample);
                //OnAudioSourceEncodedSample?.Invoke((uint)(pcm.Length * _audioFormatManager.SelectedFormat.RtpClockRate / _audioFormatManager.SelectedFormat.ClockRate), encodedSample);

            }
            catch (Exception ex)
            {

            }
        }

I am getting sound but it contains too much noise and it is not clear at all and the quality is very low , can you help me with this ?
many thanks

SDL2 in Ubuntu 20.04 (WSL)

sudo apt-get install libsdl2 gives following error:

E: Unable to locate package libsdl2.

WSL:
Windows 10 Linux Subsystem
RELEASE: Ubuntu 20.04.4 LTS
KERNEL: Linux 5.10.102.1-microsoft-standard-WSL2

the application works in debug mode but crashes in non-debugging mode

Application: ssss.exe
CoreCLR Version: 7.0.923.32018
.NET Version: 7.0.9
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Stack:
at SDL2.SDL.SDL_CloseAudioDevice(UInt32)
at SDL2.SDL.SDL_CloseAudioDevice(UInt32)
at SIPSorceryMedia.SDL2.SDL2Helper.CloseAudioDevice(UInt32)
at SIPSorceryMedia.SDL2.SDL2Helper.CloseAudioPlaybackDevice(UInt32)
at SIPSorceryMedia.SDL2.SDL2AudioEndPoint.CloseAudioSink()
at SIPSorceryMedia.SDL2.SDL2AudioEndPoint.InitPlaybackDevice()
at SIPSorceryMedia.SDL2.SDL2AudioEndPoint.SetAudioSinkFormat(SIPSorceryMedia.Abstractions.AudioFormat)
at SIPSorcery.Media.VoIPMediaSession.AudioFormatsNegotiated(System.Collections.Generic.List1<SIPSorceryMedia.Abstractions.AudioFormat>) at SIPSorcery.Net.RTPSession.RaisedOnAudioFormatsNegotiated(Int32, System.Collections.Generic.List1<SIPSorceryMedia.Abstractions.AudioFormat>)
at SIPSorcery.net.RTP.AudioStream.CheckAudioFormatsNegotiation()
at SIPSorcery.Net.RTPSession.SetRemoteDescription(SIPSorcery.SIP.App.SdpType, SIPSorcery.Net.SDP)
at SIPSorcery.SIP.App.SIPUserAgent+d__135.MoveNext()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[SIPSorcery.SIP.App.SIPUserAgent+d__135, SIPSorcery, Version=6.0.12.0, Culture=neutral, PublicKeyToken=null]](d__135 ByRef)
at System.Runtime.CompilerServices.AsyncVoidMethodBuilder.Start[[SIPSorcery.SIP.App.SIPUserAgent+d__135, SIPSorcery, Version=6.0.12.0, Culture=neutral, PublicKeyToken=null]](d__135 ByRef)
at SIPSorcery.SIP.App.SIPUserAgent.ClientCallAnsweredHandler(SIPSorcery.SIP.App.ISIPClientUserAgent, SIPSorcery.SIP.SIPResponse)
at SIPSorcery.SIP.App.SIPClientUserAgent.ServerFinalResponseReceived(SIPSorcery.SIP.SIPEndPoint, SIPSorcery.SIP.SIPEndPoint, SIPSorcery.SIP.SIPTransaction, SIPSorcery.SIP.SIPResponse)
at SIPSorcery.SIP.UACInviteTransaction+<UACInviteTransaction_TransactionFinalResponseReceived>d__19.MoveNext()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[[SIPSorcery.SIP.UACInviteTransaction+<UACInviteTransaction_TransactionFinalResponseReceived>d__19, SIPSorcery, Version=6.0.12.0, Culture=neutral, PublicKeyToken=null]](<UACInviteTransaction_TransactionFinalResponseReceived>d__19 ByRef)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1[[System.Net.Sockets.SocketError, System.Net.Primitives, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]].Start[[SIPSorcery.SIP.UACInviteTransaction+<UACInviteTransaction_TransactionFinalResponseReceived>d__19, SIPSorcery, Version=6.0.12.0, Culture=neutral, PublicKeyToken=null]](<UACInviteTransaction_TransactionFinalResponseReceived>d__19 ByRef)
at SIPSorcery.SIP.UACInviteTransaction.UACInviteTransaction_TransactionFinalResponseReceived(SIPSorcery.SIP.SIPEndPoint, SIPSorcery.SIP.SIPEndPoint, SIPSorcery.SIP.SIPTransaction, SIPSorcery.SIP.SIPResponse)
at SIPSorcery.SIP.SIPTransaction.GotResponse(SIPSorcery.SIP.SIPEndPoint, SIPSorcery.SIP.SIPEndPoint, SIPSorcery.SIP.SIPResponse)
at SIPSorcery.SIP.SIPTransport.SIPMessageReceived(SIPSorcery.SIP.SIPChannel, SIPSorcery.SIP.SIPEndPoint, SIPSorcery.SIP.SIPEndPoint, Byte[])
at SIPSorcery.SIP.SIPTransport.ProcessReceiveQueue()
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef, System.Threading.Thread)

In Linux (WSL-Ubuntu) "sudo apt-get install libsdl2" says "Unable to locate package libsdl2"

In Linux (WSL-Ubuntu) "sudo apt-get install libsdl2" says "Unable to locate package libsdl2"

But "sudo apt install libsdl2-dev libsdl2-2.0-0 -y" is working. After installation "sudo apt install libsdl2-dev libsdl2-2.0-0 -y", SDL2 does not providing any choice to select audio input or output device. It only shows "RDP Source" as Audio Input and "RDP Sink" as audio output.

My built in mic is not working. Using external USB Mic. But SDL2 cannot recognize it.

Please help me solve this issue.

Failed to open audio recording device [name]. SDL Error: CoreAudio error (AudioQueueStart): -66681 [MacOS]

When using our app, if an audio input or output device is unplugged, I can switch to another available device on all platforms (Windows, MacOS, Linux). However, after plugging the device back in, the app can only switch back to it on Windows.

On MacOS, I encounter the following error: "Failed to open audio recording device [name]. SDL Error: CoreAudio error (AudioQueueStart): -66681".

This indicates that the unplugged device is still in use or blocked by SDL2 or MacOS. I have tried the following code in the "InitRecordingDevice" method after calling "CloseAudio()", but it still does not work:

SDL_QuitSubSystem(SDL_INIT_AUDIO);
SDL_InitSubSystem(SDL_INIT_AUDIO);

I have also added "SDL_ClearQueuedAudio(dev)" in the "CloseAudio" method before calling "CloseAudioRecordingDevice", but this has not resolved the issue either.

Are there any solutions for this problem?

Failed to Send Audio

I can hear sound but it is not sending audio. Code is as follow:

OpusAudioEncoder audioEncoder = new OpusAudioEncoder();
AudioFormat? audioFormat = OpusAudioEncoder.MEDIA_FORMAT_OPUS;

        _audioEndPoint = new SDL2AudioEndPoint(AudioOutputName, audioEncoder);
        _audioEndPoint.SetAudioSinkFormat(audioFormat.Value);

        int frameSize = audioEncoder.GetFrameSize();
        _audioSource = new SDL2AudioSource(AudioInputName, audioEncoder, (uint)frameSize);
        _audioSource.SetAudioSourceFormat(audioFormat.Value);

        MediaStreamTrack audioInputTrack = new MediaStreamTrack(_audioSource.GetAudioSourceFormats(),
            MediaStreamStatusEnum.SendRecv);
        _peerConnection.addTrack(audioInputTrack);

        _audioSource.OnAudioSourceEncodedSample += _peerConnection.SendAudio;

        _peerConnection.OnRtpPacketReceived += (IPEndPoint rep, SDPMediaTypesEnum media, RTPPacket rtpPkt) =>
        {
            if (media == SDPMediaTypesEnum.audio)
            {
                _audioEndPoint.GotAudioRtp(rep, rtpPkt.Header.SyncSource,
                    rtpPkt.Header.SequenceNumber, rtpPkt.Header.Timestamp,
                    rtpPkt.Header.PayloadType, rtpPkt.Header.MarkerBit == 1,
                    rtpPkt.Payload);
            }
        };

        _peerConnection.OnAudioFormatsNegotiated += (audioFormats) =>
        {
            _audioSource.SetAudioSourceFormat(audioFormats.First());
            _audioEndPoint.SetAudioSinkFormat(audioFormats.First());
        };

Echo in sound

When using the laptop's built-in speaker and microphone, there is a tendency for echo to occur. However, this issue does not arise when headphones are used.

Is there any echo/noise cancellation feature in SIPSorceryMedia.SDL2?

Code:
_audioEncoder = new OpusAudioEncoder();
_audioFormat = OpusAudioEncoder.MEDIA_FORMAT_OPUS;

_frameSize = _audioEncoder.GetFrameSize();
_audioSource = new SDL2AudioSource(AudioInputName, _audioEncoder, (uint)_frameSize);
_audioEndPoint = new SDL2AudioEndPoint(AudioOutputName, _audioEncoder);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.