luthfiampas / Bufdio

A cross platform audio playback library for .NET with PortAudio and FFmpeg
MIT License
7 stars 3 forks source link

Unable to load shared library 'libdl' or one of its dependencies #5

Open KirMozor opened 2 years ago

KirMozor commented 2 years ago

Hello, I tried the following code:

        private static string url = "https://www.learningcontainer.com/wp-content/uploads/2020/02/Kalimba.mp3";
        static void Main()
        {
            BufdioLib.InitializePortAudio();
            BufdioLib.InitializeFFmpeg();
            using IAudioPlayer player = new AudioPlayer();
            player.LoadAsync(url);
            player.Play();
            //player.Pause();
            //player.Stop();
            //player.Seek(TimeSpan.FromSeconds(2));
        }

But your package turns out to have a bug, it crashes when it cannot find the libdl library

/home/kirill/RiderProjects/TestEvents/bin/Debug/net6.0/TestEvents
Unhandled exception. System.DllNotFoundException: Unable to load shared library 'libdl' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibdl: cannot open shared object file: No such file or directory
   at Bufdio.Interop.Libdl.dlopen(String fileName, Int32 flags)
   at Bufdio.Utilities.LibraryLoader..ctor(String libraryName)
   at Bufdio.BufdioLib.InitializePortAudio(String portAudioPath)
   at Program.ProgramNew.Main() in /home/kirill/RiderProjects/TestEvents/Program.cs:line 14

My distribution is ArchLinux and I decided to check if I have ffmpeg and portaudio installed, as it turns out I have

➜  ~ sudo pacman -Qs ffmpeg
[sudo] пароль для kirill: 
local/ffmpeg 2:5.0-7
    Complete solution to record, convert and stream audio and video
local/ffmpeg4.4 4.4.1-5
    Complete solution to record, convert and stream audio and video
local/gst-libav 1.20.2-2
    Multimedia graph framework - libav plugin
➜  ~ sudo pacman -Qs portaudio
local/portaudio 1:19.7.0-1
    A free, cross-platform, open source, audio I/O library.
➜  ~ 

Then I went to Google and searched where libdl comes from, as it turns out it belongs to the package glibc, but I have this package installed

➜  ~ sudo pacman -Qs ffmpeg
[sudo] пароль для kirill: 
local/ffmpeg 2:5.0-7
    Complete solution to record, convert and stream audio and video
local/ffmpeg4.4 4.4.1-5
    Complete solution to record, convert and stream audio and video
local/gst-libav 1.20.2-2
    Multimedia graph framework - libav plugin
➜  ~ sudo pacman -Qs portaudio
local/portaudio 1:19.7.0-1
    A free, cross-platform, open source, audio I/O library.
➜  ~ sudo pacman -Qs glibc    
local/glibc 2.35-6
    GNU C Library
local/lib32-glibc 2.35-6
    GNU C Library (32-bit)
➜  ~ 

A search of the /usr/lib folder showed me that there are libraries in the system, but they have different names

➜  ~ find /usr/lib/libdl*
/usr/lib/libdl.a
/usr/lib/libdl.so.2

Of course, you could make a reference to /usr/lib/libdl.so.2 named /usr/lib/libdl.so, but this is not the solution to the problem, what should I do (or you?)

KirMozor commented 2 years ago

I decided to try to make a symbolic link to the file sudo ln -s /usr/lib/libdl.so.2 /usr/lib/libdl.so However (as expected) PortAudio didn't like it :(

ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
ALSA lib pcm_oss.c:397:(_snd_pcm_oss_open) Cannot open device /dev/dsp
ALSA lib pcm_oss.c:397:(_snd_pcm_oss_open) Cannot open device /dev/dsp
ALSA lib pcm_a52.c:1001:(_snd_pcm_a52_open) a52 is only for playback
ALSA lib confmisc.c:160:(snd_config_get_card) Invalid field card
ALSA lib pcm_usb_stream.c:482:(_snd_pcm_usb_stream_open) Invalid card 'card'
ALSA lib confmisc.c:160:(snd_config_get_card) Invalid field card
ALSA lib pcm_usb_stream.c:482:(_snd_pcm_usb_stream_open) Invalid card 'card'
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
Unhandled exception. Bufdio.Exceptions.BufdioException: No loaded audio for playback.
   at Bufdio.Utilities.Ensure.That[TException](Boolean condition, String message)
   at Bufdio.Players.AudioPlayer.Play()
   at Program.ProgramNew.Main() in /home/kirill/RiderProjects/TestEvents/Program.cs:line 18

Process finished with exit code 137.
luthfiampas commented 2 years ago

Hi @KirMozor

Unfortunately I don't own arch, could you please rename this line to libdl.so.2 and check if it works? https://github.com/luthfiampas/Bufdio/blob/46a4c67bb5a15a15bfbe834d7c99ad0702cc10e5/src/Bufdio/Interop/Libdl.cs#L8

Also I'm not sure why PortAudio uses jack as the default output device when the jack server doesn't even start.

...
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
...
luthfiampas commented 2 years ago

@KirMozor Could you please try this branch out (fix/linux-native-methods)? This will use libdl.so.2 in Linux. Tested in Fedora and Debian.

KirMozor commented 2 years ago

Hello, I will try to rename the line in the source code that you said About the arch. There's a great thing called Distrobox, it lets you put any distribution through a docker container About jack, I use Pipewire and maybe it started using jack instead of pulseaudio (the system has a package pipewire-alsa, pipewire-pulse)

KirMozor commented 2 years ago

@KirMozor Could you please try this branch out (fix/linux-native-methods)? This will use libdl.so.2 in Linux. Tested in Fedora and Debian.

Result:

/home/kirill/RiderProjects/TestEvents/bin/Debug/net6.0/TestEvents
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
ALSA lib pcm_oss.c:397:(_snd_pcm_oss_open) Cannot open device /dev/dsp
ALSA lib pcm_oss.c:397:(_snd_pcm_oss_open) Cannot open device /dev/dsp
ALSA lib pcm_a52.c:1001:(_snd_pcm_a52_open) a52 is only for playback
ALSA lib confmisc.c:160:(snd_config_get_card) Invalid field card
ALSA lib pcm_usb_stream.c:482:(_snd_pcm_usb_stream_open) Invalid card 'card'
ALSA lib confmisc.c:160:(snd_config_get_card) Invalid field card
ALSA lib pcm_usb_stream.c:482:(_snd_pcm_usb_stream_open) Invalid card 'card'
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
Unhandled exception. Bufdio.Exceptions.BufdioException: No loaded audio for playback.
   at Bufdio.Utilities.Ensure.That[TException](Boolean condition, String message)
   at Bufdio.Players.AudioPlayer.Play()
   at Program.ProgramNew.Main() in /home/kirill/RiderProjects/TestEvents/Program.cs:line 18
KirMozor commented 2 years ago

My libdl.so.2 libdl.so.2.zip

KirMozor commented 2 years ago

WHAT???? Your example on Avalonia works...

KirMozor commented 2 years ago

Yeah, but the url doesn't play :( How to fix it? изображение I used this URL: https://www.youtube.com/watch?v=7wtfhZwyrcc Log:

/home/kirill/Tools/Bufdio/examples/BufdioAvalonia/bin/Debug/net6.0/BufdioAvalonia
ALSA lib pcm_dmix.c:999:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm_route.c:877:(find_matching_chmap) Found no matching channel map
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
ALSA lib pcm_oss.c:397:(_snd_pcm_oss_open) Cannot open device /dev/dsp
ALSA lib pcm_oss.c:397:(_snd_pcm_oss_open) Cannot open device /dev/dsp
ALSA lib pcm_a52.c:1001:(_snd_pcm_a52_open) a52 is only for playback
ALSA lib confmisc.c:160:(snd_config_get_card) Invalid field card
ALSA lib pcm_usb_stream.c:482:(_snd_pcm_usb_stream_open) Invalid card 'card'
ALSA lib confmisc.c:160:(snd_config_get_card) Invalid field card
ALSA lib pcm_usb_stream.c:482:(_snd_pcm_usb_stream_open) Invalid card 'card'
ALSA lib pcm_dmix.c:999:(snd_pcm_dmix_open) unable to open slave
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
luthfiampas commented 2 years ago

Sorry, I think this issue is related to ffmpeg, you can check if it works by playing local audio file first.

For example, we can't load this file (https://www.learningcontainer.com/wp-content/uploads/2020/02/Kalimba.mp3) because the transfer encoding is being chunked that makes duration of the audio file remains unknown until we get complete response.

Yeah, but the url doesn't play :( How to fix it?

It happens a lot with ffmpeg in the first load, you can retry and check if it works. Will take a deeper look, maybe tomorrow.

KirMozor commented 2 years ago

Sorry, I think this issue is related to ffmpeg, you can check if it works by playing local audio file first.

For example, we can't load this file (https://www.learningcontainer.com/wp-content/uploads/2020/02/Kalimba.mp3) because the transfer encoding is being chunked that makes duration of the audio file remains unknown until we get complete response.

I checked if the track by url and the local file through ffmpeg, and it plays https://user-images.githubusercontent.com/68265324/173661425-cb1942f6-d094-4ed2-a7e6-6200ffb476d2.mp4

KirMozor commented 2 years ago

What progress has been made in this case?

maximilien-noal commented 2 years ago

I had the same problem on the latest Fedora Linux Workstation.

I have a local fork where I use the NativeLibrary class from the .NET Core 3.1 (and later) standard library instead:

using System;
using System.Runtime.InteropServices;

namespace Bufdio.Utilities
 {
  internal sealed class LibraryLoader : IDisposable
  {
      private readonly IntPtr _handle;
      private bool _disposed;

      public LibraryLoader(string libraryName)
      {
          Ensure.NotNull(libraryName, nameof(libraryName));

          if (!NativeLibrary.TryLoad(libraryName, out _handle)) {
              throw new NotSupportedException("Platform is not supported.");

          }

          Ensure.That<Exception>(_handle != IntPtr.Zero, $"Could not load native libary: {libraryName}.");
      }

      public TDelegate LoadFunc<TDelegate>(string name)
      {
          IntPtr ptr = NativeLibrary.GetExport(_handle, name);
          Ensure.That<Exception>(ptr != IntPtr.Zero, $"Could not load function name: {name}.");

          return Marshal.GetDelegateForFunctionPointer<TDelegate>(ptr);
      }

      public void Dispose()
      {
          if (_disposed)
          {
              return;
          }
          NativeLibrary.Free(_handle);
          _disposed = true;
      }
  }
}

Source: https://github.com/dotnet/corert/blob/master/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/NativeLibrary.cs

It fixes the issue. Maybe this class could be backported if targeting netstandard2.1 is still required ?

KirMozor commented 2 years ago

I could not find this fork on your GitHub page. Can you provide me with the source code? I'd like to try and test a fix.

maximilien-noal commented 2 years ago

https://github.com/OpenRakis/Spice86/blob/fix/pcm/src/Bufdio/Utilities/LibraryLoader.cs

maximilien-noal commented 1 year ago

Sorry about the delay, here is the fix:

But it depends on .NET >= 6, not .NET Standard.

https://github.com/OpenRakis/Spice86/blob/f86f730c9b9728bd1dea5a30a1056a58fde46f5d/src/Bufdio.Spice86/Utilities/LibraryLoader.cs#L14