Ignore:
Timestamp:
Mar 12, 2017, 2:30:48 PM (7 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
sampler
Children:
8b07fa9
Parents:
91ad284
Message:

src/io/source_avcodec.c: make thread safe using a global lock

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/io/source_avcodec.c

    r91ad284 r618ebd5  
    5353
    5454#define AUBIO_AVCODEC_MAX_BUFFER_SIZE FF_MIN_BUFFER_SIZE
     55
     56#ifdef HAVE_PTHREAD_H
     57// Global mutex to make sure avcodec_open2 is not called simultaneously in a
     58// multithreaded environment. Comment the following define if no lock required.
     59#define HAVE_AUBIO_AVCODEC_MUTEX
     60#include <pthread.h>
     61pthread_mutex_t aubio_avcodec_mutex = PTHREAD_MUTEX_INITIALIZER;
     62#endif /* HAVE_PTHREAD_H */
    5563
    5664struct _aubio_source_avcodec_t {
     
    122130  strncpy(s->path, path, strnlen(path, PATH_MAX) + 1);
    123131
     132#ifdef HAVE_AUBIO_AVCODEC_MUTEX
     133  pthread_mutex_lock(&aubio_avcodec_mutex);
     134#endif /* HAVE_AUBIO_AVCODEC_MUTEX */
     135
    124136  // register all formats and codecs
    125137  av_register_all();
     
    231243  }
    232244
     245#ifdef HAVE_AUBIO_AVCODEC_MUTEX
     246  pthread_mutex_unlock(&aubio_avcodec_mutex);
     247#endif /* HAVE_AUBIO_AVCODEC_MUTEX */
     248
    233249  /* get input specs */
    234250  s->input_samplerate = avCodecCtx->sample_rate;
     
    280296  //AUBIO_ERR("can not read %s at samplerate %dHz with a hop_size of %d\n",
    281297  //    s->path, s->samplerate, s->hop_size);
     298#ifdef HAVE_AUBIO_AVCODEC_MUTEX
     299  pthread_mutex_unlock(&aubio_avcodec_mutex);
     300#endif /* HAVE_AUBIO_AVCODEC_MUTEX */
    282301  del_aubio_source_avcodec(s);
    283302  return NULL;
Note: See TracChangeset for help on using the changeset viewer.