Commit b50a0c22 authored by Rafaël Carré's avatar Rafaël Carré

* #6588: install a lock manager in FFmpeg instead of protecting calls ourselves

parent eb4f9434
......@@ -1196,11 +1196,6 @@ class ManagerImpl
*/
void audioLayerMutexUnlock(void) { _audiolayerMutex.leaveMutex(); }
/**
* Enter the mutex for libavcodec
*/
ost::Mutex& avcodecMutex() { return avcodecMutex_; }
private:
/* Transform digest to string.
* output must be at least PJSIP_MD5STRLEN+1 bytes.
......@@ -1266,9 +1261,6 @@ class ManagerImpl
/** Protected current call access */
ost::Mutex _currentCallMutex;
/** Protected libavcodec access */
ost::Mutex avcodecMutex_;
/** Audio layer */
AudioLayer* _audiodriver;
......
......@@ -34,8 +34,9 @@
#include <string>
#include <iostream>
#include <cc++/thread.h>
extern "C" {
#define __STDC_CONSTANT_MACROS
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
......@@ -100,4 +101,37 @@ std::list<std::string> installedCodecs()
}
return codecs;
}
static ost::Mutex avcodec_lock;
static int avcodecManageMutex(void **mutex, enum AVLockOp op)
{
(void)mutex; // no need to store our mutex in ffmpeg
switch(op) {
case AV_LOCK_CREATE:
break; // our mutex is already created
case AV_LOCK_DESTROY:
break; // our mutex doesn't need to be destroyed
case AV_LOCK_OBTAIN:
avcodec_lock.enter();
break;
case AV_LOCK_RELEASE:
avcodec_lock.leave();
break;
}
return 0;
}
void sfl_avcodec_init_locking()
{
static int done = 0;
ost::MutexLock lock(avcodec_lock);
if (done)
return;
done = 1;
av_lockmgr_register(avcodecManageMutex);
}
} // end namespace libav_utils
......@@ -44,6 +44,8 @@ namespace libav_utils {
* has been anticipated and it has been tested.
*/
bool isSupportedCodec(const char *codec);
void sfl_avcodec_init_locking();
}
#endif // __LIBAV_UTILS_H__
......@@ -29,9 +29,8 @@
* as that of the covered work.
*/
#define __STDC_CONSTANT_MACROS
#include "video_receive_thread.h"
#include "libav_utils.h"
// libav includes
extern "C" {
......@@ -223,11 +222,9 @@ void VideoReceiveThread::loadSDP()
void VideoReceiveThread::setup()
{
{
ost::MutexLock lock(Manager::instance().avcodecMutex());
av_register_all();
avdevice_register_all();
}
libav_utils::sfl_avcodec_init_locking();
av_register_all();
avdevice_register_all();
dstWidth_ = atoi(args_["width"].c_str());
dstHeight_ = atoi(args_["height"].c_str());
......@@ -286,11 +283,7 @@ void VideoReceiveThread::setup()
int ret;
// retrieve stream information
{
ost::MutexLock lock(Manager::instance().avcodecMutex());
ret = av_find_stream_info(inputCtx_);
}
ret = av_find_stream_info(inputCtx_);
if (ret < 0)
{
_error("%s:Could not find stream info!", __PRETTY_FUNCTION__);
......@@ -325,10 +318,7 @@ void VideoReceiveThread::setup()
}
// open codec
{
ost::MutexLock lock(Manager::instance().avcodecMutex());
ret = avcodec_open(decoderCtx_, inputDecoder);
}
ret = avcodec_open(decoderCtx_, inputDecoder);
if (ret < 0)
{
_error("%s:Could not open codec!", __PRETTY_FUNCTION__);
......@@ -415,7 +405,6 @@ void VideoReceiveThread::cleanup()
// doesn't need to be freed, we didn't use avcodec_alloc_context
if (decoderCtx_)
{
ost::MutexLock lock(Manager::instance().avcodecMutex());
avcodec_close(decoderCtx_);
decoderCtx_ = 0;
}
......
......@@ -28,9 +28,8 @@
* as that of the covered work.
*/
#define __STDC_CONSTANT_MACROS
#include "video_send_thread.h"
#include "libav_utils.h"
// libav includes
extern "C" {
......@@ -124,11 +123,9 @@ void VideoSendThread::prepareEncoderContext()
void VideoSendThread::setup()
{
int ret;
{
ost::MutexLock lock(Manager::instance().avcodecMutex());
av_register_all();
avdevice_register_all();
}
libav_utils::sfl_avcodec_init_locking();
av_register_all();
avdevice_register_all();
if (!test_source_)
{
......@@ -160,12 +157,8 @@ void VideoSendThread::setup()
exit();
}
{
ost::MutexLock lock(Manager::instance().avcodecMutex());
// retrieve stream information
ret = av_find_stream_info(inputCtx_);
}
// retrieve stream information
ret = av_find_stream_info(inputCtx_);
if (ret < 0)
{
_error("Could not find stream info!");
......@@ -200,10 +193,7 @@ void VideoSendThread::setup()
}
// open codec
{
ost::MutexLock lock(Manager::instance().avcodecMutex());
ret = avcodec_open(inputDecoderCtx_, inputDecoder);
}
ret = avcodec_open(inputDecoderCtx_, inputDecoder);
if (ret < 0)
{
_error("%s:Could not open codec!", __PRETTY_FUNCTION__);
......@@ -243,10 +233,7 @@ void VideoSendThread::setup()
scaledPicture_ = avcodec_alloc_frame();
// open encoder
{
ost::MutexLock lock(Manager::instance().avcodecMutex());
ret = avcodec_open(encoderCtx_, encoder);
}
ret = avcodec_open(encoderCtx_, encoder);
if (ret < 0)
{
_error("%s:Could not open encoder!", __PRETTY_FUNCTION__);
......@@ -347,21 +334,18 @@ void VideoSendThread::cleanup()
}
// close the codecs
if (encoderCtx_)
{
ost::MutexLock lock(Manager::instance().avcodecMutex());
if (encoderCtx_)
{
avcodec_close(encoderCtx_);
av_freep(&encoderCtx_);
encoderCtx_ = 0;
}
avcodec_close(encoderCtx_);
av_freep(&encoderCtx_);
encoderCtx_ = 0;
}
// doesn't need to be freed, we didn't use avcodec_alloc_context
if (inputDecoderCtx_)
{
avcodec_close(inputDecoderCtx_);
inputDecoderCtx_ = 0;
}
// doesn't need to be freed, we didn't use avcodec_alloc_context
if (inputDecoderCtx_)
{
avcodec_close(inputDecoderCtx_);
inputDecoderCtx_ = 0;
}
// close the video file
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment