Ticket #519: 0001-pulse-add-resume-when-pulseaudio-is-killed.patch

File 0001-pulse-add-resume-when-pulseaudio-is-killed.patch, 6.3 KB (added by elmarco, 3 years ago)

no more assert(pcm->stream)!!!

  • pulse/pcm_pulse.c

    From 525cf902d9f1bb02d3e6f97183d40cfe6c0a7ea2 Mon Sep 17 00:00:00 2001
    From: =?utf-8?q?Marc-Andr=C3=A9=20Lureau?= <marc-andre.lureau@nokia.com>
    Date: Fri, 3 Apr 2009 02:06:37 +0300
    Subject: [PATCH] pulse: add resume when pulseaudio is killed
    
    ---
     pulse/pcm_pulse.c |   45 ++++++++++++++++++++++++++++++++----
     pulse/pulse.c     |   66 +++++++++++++++++++++++++++++++++++++++++-----------
     pulse/pulse.h     |    2 +
     3 files changed, 94 insertions(+), 19 deletions(-)
    
    diff --git a/pulse/pcm_pulse.c b/pulse/pcm_pulse.c
    index db8d1e1..2abfec3 100644
    a b  
    269269 
    270270    pa_threaded_mainloop_lock(pcm->p->mainloop); 
    271271 
    272     assert(pcm->stream); 
     272    if (!pcm->stream) { 
     273        ret = -ESTRPIPE; 
     274        goto finish; 
     275    } 
    273276 
    274277    ret = pulse_check_connection(pcm->p); 
    275278    if (ret < 0) 
     
    309312 
    310313    pa_threaded_mainloop_lock(pcm->p->mainloop); 
    311314 
    312     assert(pcm->stream); 
     315    if (!pcm->stream) { 
     316        err = -ESTRPIPE; 
     317        goto finish; 
     318    } 
    313319 
    314320    for (;;) { 
    315321        err = pulse_check_connection(pcm->p); 
     
    358364 
    359365    pa_threaded_mainloop_lock(pcm->p->mainloop); 
    360366 
    361     assert(pcm->stream); 
     367    if (!pcm->stream) { 
     368        ret = -ESTRPIPE; 
     369        goto finish; 
     370    } 
    362371 
    363372    ret = pulse_check_connection(pcm->p); 
    364373    if (ret < 0) 
     
    413422 
    414423    pa_threaded_mainloop_lock(pcm->p->mainloop); 
    415424 
    416     assert(pcm->stream); 
     425    if (!pcm->stream) { 
     426        ret = -ESTRPIPE; 
     427        goto finish; 
     428    } 
    417429 
    418430    ret = pulse_check_connection(pcm->p); 
    419431    if (ret < 0) 
     
    553565        pcm->stream = NULL; 
    554566    } 
    555567 
    556     err = pulse_check_connection(pcm->p); 
     568    err = pulse_check_connection_reconnect(pcm->p); 
    557569    if (err < 0) 
    558570        goto finish; 
    559571 
     
    638650    return err; 
    639651} 
    640652 
     653static int pulse_resume(snd_pcm_ioplug_t * io) 
     654{ 
     655    int ret = 0; 
     656    snd_pcm_pulse_t *pcm = io->private_data; 
     657 
     658    assert(pcm); 
     659    assert(pcm->p); 
     660 
     661    pa_threaded_mainloop_lock(pcm->p->mainloop); 
     662 
     663    if (!pcm->stream) { 
     664        ret = pulse_check_connection_reconnect(pcm->p); 
     665        goto finish; 
     666    } 
     667 
     668finish: 
     669    pa_threaded_mainloop_unlock(pcm->p->mainloop); 
     670 
     671    return ret; 
     672} 
     673 
    641674static int pulse_hw_params(snd_pcm_ioplug_t * io, 
    642675               snd_pcm_hw_params_t * params) 
    643676{ 
     
    748781    .delay = pulse_delay, 
    749782    .poll_revents = pulse_pcm_poll_revents, 
    750783    .prepare = pulse_prepare, 
     784    .resume = pulse_resume, 
    751785    .hw_params = pulse_hw_params, 
    752786    .close = pulse_close, 
    753787}; 
     
    761795    .delay = pulse_delay, 
    762796    .poll_revents = pulse_pcm_poll_revents, 
    763797    .prepare = pulse_prepare, 
     798    .resume = pulse_resume, 
    764799    .hw_params = pulse_hw_params, 
    765800    .close = pulse_close, 
    766801}; 
  • pulse/pulse.c

    diff --git a/pulse/pulse.c b/pulse/pulse.c
    index 3940238..ca246ab 100644
    a b  
    2727 
    2828#include "pulse.h" 
    2929 
     30static void context_state_cb(pa_context * c, void *userdata); 
     31 
     32static void make_new_context(snd_pulse_t *p) 
     33{ 
     34    char proc[PATH_MAX], buf[PATH_MAX + 20]; 
     35 
     36    assert(p); 
     37    assert(!p->context); 
     38 
     39    if (pa_get_binary_name(proc, sizeof(proc))) 
     40        snprintf(buf, sizeof(buf), "ALSA plug-in [%s]", 
     41             pa_path_get_filename(proc)); 
     42    else 
     43        snprintf(buf, sizeof(buf), "ALSA plug-in"); 
     44    buf[sizeof(buf)-1] = 0; 
     45 
     46    p->context = 
     47        pa_context_new(pa_threaded_mainloop_get_api(p->mainloop), buf); 
     48    assert(p->context); 
     49 
     50    pa_context_set_state_callback(p->context, context_state_cb, p); 
     51} 
     52 
    3053int pulse_check_connection(snd_pulse_t * p) 
    3154{ 
    3255    pa_context_state_t state; 
     
    4366    return 0; 
    4467} 
    4568 
     69int pulse_check_connection_reconnect(snd_pulse_t * p) 
     70{ 
     71    assert(p); 
     72    assert(p->context); 
     73    assert(p->mainloop); 
     74 
     75    if (pulse_check_connection(p) < 0) { 
     76        pa_context_disconnect(p->context); 
     77        pa_context_unref(p->context); 
     78        p->context = NULL; 
     79        p->state = PULSE_STATE_INIT; 
     80        pa_threaded_mainloop_unlock(p->mainloop); /* I don't think it is a good idea from here... */ 
     81        make_new_context(p); 
     82        pulse_connect(p, p->server); 
     83        pa_threaded_mainloop_lock(p->mainloop); 
     84    } 
     85 
     86    return 0; 
     87} 
     88 
    4689void pulse_stream_state_cb(pa_stream * s, void *userdata) 
    4790{ 
    4891    snd_pulse_t *p = userdata; 
     
    164207{ 
    165208    snd_pulse_t *p; 
    166209    int fd[2] = { -1, -1 }; 
    167     char proc[PATH_MAX], buf[PATH_MAX + 20]; 
    168210 
    169211    p = calloc(1, sizeof(snd_pulse_t)); 
    170212 
     
    188230    if (!p->mainloop) 
    189231        goto fail; 
    190232 
    191     if (pa_get_binary_name(proc, sizeof(proc))) 
    192         snprintf(buf, sizeof(buf), "ALSA plug-in [%s]", 
    193              pa_path_get_filename(proc)); 
    194     else 
    195         snprintf(buf, sizeof(buf), "ALSA plug-in"); 
    196     buf[sizeof(buf)-1] = 0; 
    197  
    198     p->context = 
    199         pa_context_new(pa_threaded_mainloop_get_api(p->mainloop), buf); 
    200     assert(p->context); 
    201  
    202     pa_context_set_state_callback(p->context, context_state_cb, p); 
     233    make_new_context(p); 
    203234 
    204235    if (pa_threaded_mainloop_start(p->mainloop) < 0) 
    205236        goto fail; 
     
    238269    close(p->thread_fd); 
    239270    close(p->main_fd); 
    240271 
     272    if (p->server) 
     273        free(p->server); 
     274 
    241275    free(p); 
    242276} 
    243277 
     
    250284    assert(p->mainloop); 
    251285    assert(p->state == PULSE_STATE_INIT); 
    252286 
     287    if (!p->server) 
     288        p->server = server ? strdup(server) : NULL; 
     289 
    253290    pa_threaded_mainloop_lock(p->mainloop); 
    254291 
    255     err = pa_context_connect(p->context, server, 0, NULL); 
     292    usleep(300000); /* FIXME: there is a race when re-connecting immediately and autospawn */ 
     293    err = pa_context_connect(p->context, server, PA_CONTEXT_NOFAIL, NULL); 
    256294    if (err < 0) 
    257295        goto error; 
    258296 
  • pulse/pulse.h

    diff --git a/pulse/pulse.h b/pulse/pulse.h
    index 7bf1a5b..a91fda2 100644
    a b  
    2929typedef struct snd_pulse { 
    3030    pa_threaded_mainloop *mainloop; 
    3131    pa_context *context; 
     32    char *server; 
    3233 
    3334    int thread_fd, main_fd; 
    3435 
     
    4041} snd_pulse_t; 
    4142 
    4243int pulse_check_connection(snd_pulse_t * p); 
     44int pulse_check_connection_reconnect(snd_pulse_t * p); 
    4345 
    4446void pulse_stream_state_cb(pa_stream * s, void *userdata); 
    4547void pulse_stream_success_cb(pa_stream * s, int success, void *userdata);