diff options
author | Aaron Levinson <alevinsn@aracnet.com> | 2017-04-15 08:41:46 +0200 |
---|---|---|
committer | Marton Balint <cus@passwd.hu> | 2017-04-15 12:26:21 +0200 |
commit | 9e86a61870c1652cdcc6a34646aee40d973ba23b (patch) | |
tree | 219d266c41b2f2a1f13d99742cea1618ff5c0a16 /libavdevice/decklink_enc.cpp | |
parent | e8547647292ee827db593a4d5b60f041a6af9769 (diff) | |
download | ffmpeg-9e86a61870c1652cdcc6a34646aee40d973ba23b.tar.gz |
avdevice/decklink: remove pthread dependency
Purpose: avdevice/decklink: Removed pthread dependency by replacing
semaphore used in code appropriately. Doing so makes it easier to
build ffmpeg using Visual C++ on Windows. This is a contination of
Kyle Schwarz's "avdevice/decklink: Remove pthread dependency" patch
that is available at https://patchwork.ffmpeg.org/patch/2654/ . This
patch wasn't accepted, and as far as I can tell, there was no
follow-up after it was rejected.
Notes: Used Visual Studio 2015 (with update 3) for this.
Comments:
-- configure: Eliminated pthreads dependency for decklink_indev_deps
and decklink_outdev_deps and replaced with threads dependency
-- libavdevice/decklink_common.cpp / .h:
a) Eliminated semaphore and replaced with a combination of a mutex,
condition variable, and a counter (frames_buffer_available_spots).
b) Removed include of pthread.h and semaphore.h and now using
libavutil/thread.h instead.
-- libavdevice/decklink_dec.cpp: Eliminated include of pthread.h and
semaphore.h.
-- libavdevice/decklink_enc.cpp:
a) Eliminated include of pthread.h and semaphore.h.
b) Replaced use of semaphore with the equivalent using a combination
of a mutex, condition variable, and a counter
(frames_buffer_available_spots). In theory, libavutil/thread.h and
the associated code could have been modified instead to add
cross-platform implementations of the sem_ functions, but an
inspection of the ffmpeg source base indicates that there are only
two cases in which semaphores are used (including this one that was
replaced), so it was deemed to not be worth the effort.
Signed-off-by: Marton Balint <cus@passwd.hu>
Diffstat (limited to 'libavdevice/decklink_enc.cpp')
-rw-r--r-- | libavdevice/decklink_enc.cpp | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/libavdevice/decklink_enc.cpp b/libavdevice/decklink_enc.cpp index 18ef9058e2..5105967101 100644 --- a/libavdevice/decklink_enc.cpp +++ b/libavdevice/decklink_enc.cpp @@ -24,9 +24,6 @@ using std::atomic; #include <DeckLinkAPI.h> -#include <pthread.h> -#include <semaphore.h> - extern "C" { #include "libavformat/avformat.h" #include "libavformat/internal.h" @@ -91,7 +88,10 @@ public: av_frame_unref(avframe); - sem_post(&ctx->semaphore); + pthread_mutex_lock(&ctx->mutex); + ctx->frames_buffer_available_spots++; + pthread_cond_broadcast(&ctx->cond); + pthread_mutex_unlock(&ctx->mutex); return S_OK; } @@ -133,7 +133,6 @@ static int decklink_setup_video(AVFormatContext *avctx, AVStream *st) ctx->output_callback = new decklink_output_callback(); ctx->dlo->SetScheduledFrameCompletionCallback(ctx->output_callback); - /* Start video semaphore. */ ctx->frames_preroll = st->time_base.den * ctx->preroll; if (st->time_base.den > 1000) ctx->frames_preroll /= 1000; @@ -141,7 +140,9 @@ static int decklink_setup_video(AVFormatContext *avctx, AVStream *st) /* Buffer twice as many frames as the preroll. */ ctx->frames_buffer = ctx->frames_preroll * 2; ctx->frames_buffer = FFMIN(ctx->frames_buffer, 60); - sem_init(&ctx->semaphore, 0, ctx->frames_buffer); + pthread_mutex_init(&ctx->mutex, NULL); + pthread_cond_init(&ctx->cond, NULL); + ctx->frames_buffer_available_spots = ctx->frames_buffer; /* The device expects the framerate to be fixed. */ avpriv_set_pts_info(st, 64, st->time_base.num, st->time_base.den); @@ -211,7 +212,8 @@ av_cold int ff_decklink_write_trailer(AVFormatContext *avctx) if (ctx->output_callback) delete ctx->output_callback; - sem_destroy(&ctx->semaphore); + pthread_mutex_destroy(&ctx->mutex); + pthread_cond_destroy(&ctx->cond); av_freep(&cctx->ctx); @@ -247,7 +249,12 @@ static int decklink_write_video_packet(AVFormatContext *avctx, AVPacket *pkt) } /* Always keep at most one second of frames buffered. */ - sem_wait(&ctx->semaphore); + pthread_mutex_lock(&ctx->mutex); + while (ctx->frames_buffer_available_spots == 0) { + pthread_cond_wait(&ctx->cond, &ctx->mutex); + } + ctx->frames_buffer_available_spots--; + pthread_mutex_unlock(&ctx->mutex); /* Schedule frame for playback. */ hr = ctx->dlo->ScheduleVideoFrame((struct IDeckLinkVideoFrame *) frame, |