Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alsa module blocks when joining thread on destruction. #62

Closed
piwinux opened this issue Aug 10, 2015 · 9 comments
Closed

Alsa module blocks when joining thread on destruction. #62

piwinux opened this issue Aug 10, 2015 · 9 comments

Comments

@piwinux
Copy link

piwinux commented Aug 10, 2015

Hello,

I am trying Baresip on Linux Debian Jessie. I am trying to figure out how to have working sound when dialing SIP URI. Logs when dialing say:

call: connecting to ...
call: SIP Progress: 100 Trying (/)
call: SIP Progress: 100 Trying (/)
audio: Set audio decoder: PCMU 8000Hz 1ch
audio: Set audio encoder: PCMU 8000Hz 1ch
audio tx pipeline:        alsa ---> vumeter ---> PCMU
audio rx pipeline:        alsa <--- vumeter <--- PCMU
...: Call established: ...
[0:00:03] audio=0/0 (bit/s)

The audio does not work in my case, so I quit (I type q); when doing that, the application hangs after dumping these lines:

Quit
Stopping 1 useragent.. 
...: Call with ... terminated (duration: 3 secs)

CPU takes 100% and I have to kill the process using kill -9. When running the application in gdb, the following threads are running:

(gdb) info threads
  Id   Target Id         Frame 
  5    Thread 0x7fffea338700 (LWP 980) "baresip" (running)
  4    Thread 0x7fffeab89700 (LWP 979) "threaded-ml" (running)
  3    Thread 0x7fffeb3da700 (LWP 978) "baresip" (running)
  2    Thread 0x7fffebc2b700 (LWP 977) "threaded-ml" (running)
* 1    Thread 0x7ffff7fbe700 (LWP 973) "baresip" 0x00007ffff6e664db in pthread_join (threadid=140737122633472, thread_return=0x0) at pthread_join.c:92

And the backtrace of the current thread is:

(gdb) bt
#0  0x00007ffff6e664db in pthread_join (threadid=140737122633472, thread_return=0x0) at pthread_join.c:92
#1  0x00007ffff53504b6 in ausrc_destructor (arg=0x68fa00) at modules/alsa/alsa_src.c:40
#2  0x00007ffff7bbd584 in mem_deref (data=0x68fa00) at src/mem/mem.c:318
#3  0x000000000040a594 in stop_tx (tx=0x65fab8, a=0x65fab0) at src/audio.c:179
#4  0x000000000040c59c in audio_stop (a=0x65fab0) at src/audio.c:1125
#5  0x000000000040d929 in call_stream_stop (call=0x6606b0) at src/call.c:184
#6  0x000000000040dee4 in call_destructor (arg=0x6606b0) at src/call.c:345
#7  0x00007ffff7bbd584 in mem_deref (data=0x6606b0) at src/mem/mem.c:318
#8  0x00007ffff7bb59ab in list_flush (list=0x65e8f8) at src/list/list.c:51
#9  0x0000000000419cbd in ua_destructor (arg=0x65e8b0) at src/ua.c:450
#10 0x00007ffff7bbd584 in mem_deref (data=0x65e8b0) at src/mem/mem.c:318
#11 0x00007ffff7bb59ab in list_flush (list=0x6296c8 <uag+8>) at src/list/list.c:51
#12 0x000000000041b4d3 in ua_stop_all (forced=false) at src/ua.c:1300
#13 0x000000000041b149 in cmd_quit (pf=0x7ffff5b57550 <pf_stderr>, unused=0x7fffffffdcc0) at src/ua.c:1166
#14 0x00000000004113c2 in cmd_process (ctxp=0x6299f0 <uictx>, key=113 'q', pf=0x7ffff5b57550 <pf_stderr>) at src/cmd.c:302
#15 0x000000000041bd43 in ui_handler (key=113 'q', pf=0x7ffff5b57550 <pf_stderr>) at src/ui.c:17
#16 0x000000000041be40 in ui_input_key (key=113 'q', pf=0x7ffff5b57550 <pf_stderr>) at src/ui.c:83
#17 0x00007ffff5956d0f in report_key (ui=0x655ae0, key=113 'q') at modules/stdio/stdio.c:66
#18 0x00007ffff5956d9f in ui_fd_handler (flags=1, arg=0x655ae0) at modules/stdio/stdio.c:90
#19 0x00007ffff7bbb53f in fd_handler (re=0x7ffff7ddbcc0 <global_re>, fd=0, flags=1) at src/main/main.c:246
#20 0x00007ffff7bbc4f1 in fd_poll (re=0x7ffff7ddbcc0 <global_re>) at src/main/main.c:849
#21 0x00007ffff7bbc7ee in re_main (signalh=0x41f29b <signal_handler>) at src/main/main.c:992
#22 0x000000000041f54b in main (argc=1, argv=0x7fffffffe0e8) at src/main.c:169

This may be related to the fact that I have no sound (maybe a problem regarding NAT traversal?), but in any case joining the thread blocks the application.

@sreimers
Copy link
Member

Hello,
can you tell us your baresip version and paste your ~/.baresip/config please.

Thank you!

@piwinux
Copy link
Author

piwinux commented Aug 10, 2015

I have this problem with version 0.4.14 and with the GitHub version. For both builds I use re-0.4.13 and rem-0.4.6.

Here is my ~/.baresip/config:

#
# baresip configuration
#

#------------------------------------------------------------------------------

# Core
poll_method     epoll       # poll, select, epoll ..

# SIP
sip_trans_bsize     128
#sip_listen     0.0.0.0:5060
#sip_certificate    cert.pem

# Audio
audio_player        alsa,default
audio_source        alsa,default
audio_alert     alsa,default
audio_srate     8000-48000
audio_channels      1-2
#ausrc_srate        48000
#auplay_srate       48000
#ausrc_channels     0
#auplay_channels        0

# Video
#video_source       v4l2,/dev/video0
#video_display      x11,nil
video_size      352x288
video_bitrate       500000
video_fps       25

# AVT - Audio/Video Transport
rtp_tos         184
#rtp_ports      10000-20000
#rtp_bandwidth      512-1024 # [kbit/s]
rtcp_enable     no
rtcp_mux        no
jitter_buffer_delay 5-10        # frames
rtp_stats       no

# Network
#dns_server     10.0.0.1:53
#net_interface      wlan0

# BFCP
#bfcp_proto     udp

#------------------------------------------------------------------------------
# Modules

#module_path        /usr/local/lib/baresip/modules

# UI Modules
module          stdio.so
#module         cons.so
#module         evdev.so
#module         httpd.so

# Audio codec Modules (in order)
#module         opus.so
#module         silk.so
#module         amr.so
#module         g7221.so
#module         g722.so
#module         g726.so
module          g711.so
#module         gsm.so
#module         l16.so
#module         speex.so
#module         bv32.so

# Audio filter Modules (in encoding order)
module          vumeter.so
#module         sndfile.so
#module         speex_aec.so
#module         speex_pp.so
#module         plc.so

# Audio driver Modules
module          alsa.so
#module         portaudio.so

# Video codec Modules (in order)
#module         avcodec.so
#module         vpx.so

# Video filter Modules (in encoding order)
#module         selfview.so

# Video source modules
#module         v4l.so
#module         v4l2.so
#module         x11grab.so
#module         cairo.so

# Video display modules
#module         x11.so
#module         sdl2.so

# Audio/Video source modules
#module         rst.so
#module         gst.so

# Media NAT modules
module          stun.so
module          turn.so
module          ice.so
module          natpmp.so

# Media encryption modules
#module         srtp.so
#module         dtls_srtp.so


#------------------------------------------------------------------------------
# Temporary Modules (loaded then unloaded)

module_tmp      uuid.so
module_tmp      account.so


#------------------------------------------------------------------------------
# Application Modules

module_app      auloop.so
module_app      contact.so
module_app      menu.so
#module_app     mwi.so
#module_app     natbd.so
#module_app     presence.so
#module_app     syslog.so
module_app      vidloop.so


#------------------------------------------------------------------------------
# Module parameters


cons_listen     0.0.0.0:5555

http_listen     0.0.0.0:8000

evdev_device        /dev/input/event0

# Speex codec parameters
speex_quality       7 # 0-10
speex_complexity    7 # 0-10
speex_enhancement   0 # 0-1
speex_mode_nb       3 # 1-6
speex_mode_wb       6 # 1-6
speex_vbr       0 # Variable Bit Rate 0-1
speex_vad       0 # Voice Activity Detection 0-1
speex_agc_level     8000

# Opus codec parameters
opus_bitrate        28000 # 6000-510000

# NAT Behavior Discovery
natbd_server        creytiv.com
natbd_interval      600     # in seconds

# Selfview
video_selfview      window # {window,pip}
#selfview_size      64x64

# ICE
ice_turn        no
ice_debug       no
ice_nomination      regular # {regular,aggressive}
ice_mode        full    # {full,lite}

@sreimers
Copy link
Member

You can also test with audio-loop (command "a"). Do you use a clean alsa or with pulseaudio?

@piwinux
Copy link
Author

piwinux commented Aug 12, 2015

I tried the audio-loop command as you suggested, but I have no sound. It seems that pulseaudio is used when I use baresip; I don't know much regarding audio configuration, but based on this answer on Ask Ubuntu, I did grep the ouput of pactl list:

$ pactl list | grep baresip
                application.name = "ALSA plug-in [baresip]"
                application.process.binary = "baresip"
                module-stream-restore.id = "sink-input-by-application-name:ALSA plug-in [baresip]"
                application.name = "ALSA plug-in [baresip]"
                application.process.binary = "baresip"
                module-stream-restore.id = "source-output-by-application-name:ALSA plug-in [baresip]"
                application.name = "ALSA plug-in [baresip]"
                application.process.binary = "baresip"
                application.name = "ALSA plug-in [baresip]"
                application.process.binary = "baresip"

I can try without PulseAudio but I have to find out how to disable it for Baresip first... The output of baresip when invoking audio-loop gives:

Audio-loop: 8000Hz, 1ch
8000Hz 1ch  n_read=238 n_write=244 rw_ratio=0.97audio-loop stopped

However, joining the thread does not block when quitting the application!

@piwinux
Copy link
Author

piwinux commented Aug 12, 2015

I just tried audio-loop when running baresip without pulseaudio (using pasuspender as mentioned here). I have no sound, and the application hangs when exiting; the backtrace is slightly different but leads to the same code:

(gdb) bt
#0  0x00007ffff6e664db in pthread_join (threadid=140737122633472, thread_return=0x0) at pthread_join.c:92
#1  0x00007ffff53504b6 in ausrc_destructor (arg=0x671790) at modules/alsa/alsa_src.c:40
#2  0x00007ffff7bbd584 in mem_deref (data=0x671790) at src/mem/mem.c:318
#3  0x00007ffff4441e21 in auloop_destructor (arg=0x660430) at modules/auloop/auloop.c:75
#4  0x00007ffff7bbd584 in mem_deref (data=0x660430) at src/mem/mem.c:318
#5  0x00007ffff4442851 in auloop_stop (pf=0x7ffff5b57550 <pf_stderr>, arg=0x7fffffffdd90) at modules/auloop/auloop.c:349
#6  0x00000000004113c2 in cmd_process (ctxp=0x6299f0 <uictx>, key=65 'A', pf=0x7ffff5b57550 <pf_stderr>) at src/cmd.c:302
#7  0x000000000041bd43 in ui_handler (key=65 'A', pf=0x7ffff5b57550 <pf_stderr>) at src/ui.c:17
#8  0x000000000041be40 in ui_input_key (key=65 'A', pf=0x7ffff5b57550 <pf_stderr>) at src/ui.c:83
#9  0x00007ffff5956d0f in report_key (ui=0x655ae0, key=65 'A') at modules/stdio/stdio.c:66
#10 0x00007ffff5956d9f in ui_fd_handler (flags=1, arg=0x655ae0) at modules/stdio/stdio.c:90
#11 0x00007ffff7bbb53f in fd_handler (re=0x7ffff7ddbcc0 <global_re>, fd=0, flags=1) at src/main/main.c:246
#12 0x00007ffff7bbc4f1 in fd_poll (re=0x7ffff7ddbcc0 <global_re>) at src/main/main.c:849
#13 0x00007ffff7bbc7ee in re_main (signalh=0x41f29b <signal_handler>) at src/main/main.c:992
#14 0x000000000041f54b in main (argc=1, argv=0x7fffffffe1b8) at src/main.c:169

@sreimers
Copy link
Member

With pulseaudio 6.0 on archlinux there is no such problem. You can check with "pavucontrol" which audio interface is mapped to baresip. Maybe I can check this with debian jessie and pulseaudio next week too.

@alfredh
Copy link
Collaborator

alfredh commented Sep 20, 2015

most likely the read thread is hanging here:

    while (st->run) {
        err = snd_pcm_readi(st->read, st->sampv, num_frames);

in blocking mode this function call will not return until there are samples in the buffer.
so it you have problems with your Audio device then we might have to wait forever :)

could you try a couple of things:

  1. use NONBLOCK mode

in alsa_src.c:

-       err = snd_pcm_open(&st->read, st->device, SND_PCM_STREAM_CAPTURE, 0);
+       err = snd_pcm_open(&st->read, st->device, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
  1. close the audio-device BEFORE calling pthread_join, in destructor:
static void ausrc_destructor(void *arg)
{
    struct ausrc_st *st = arg;

    if (st->read)
        snd_pcm_close(st->read);

    /* Wait for termination of other thread */
    if (st->run) {
        st->run = false;
        (void)pthread_join(st->thread, NULL);
    }

    mem_deref(st->sampv);
    mem_deref(st->device);
}

I have seen similar bugs before, so most likely it is a ALSA API-usage error (i.e. baresip).

NOTE: using non-blocking mode is not really an option as it consumes 100% cpu

@alfredh
Copy link
Collaborator

alfredh commented Sep 20, 2015

@alfredh
Copy link
Collaborator

alfredh commented Oct 10, 2015

is there any more updates on this one?

if not feel free to close the Issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants