History

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.

Aaron Levinson committed 9 years ago

Commit 9e86a611 parent e8547645 files · +21 −17Browse files

Unified · Split

5 files
  1. configure +2 −2
  2. libavdevice/decklink_common.cpp +0 −3
  3. libavdevice/decklink_common.h +4 −1
  4. libavdevice/decklink_dec.cpp +0 −3
  5. libavdevice/decklink_enc.cpp +15 −8
configure+2−2
@@ -2995,9 +2995,9 @@ avfoundation_indev_deps="pthreads" · 2994 unmodified lines
29952995 avfoundation_indev_extralibs="-framework Foundation -framework AVFoundation -framework CoreVideo -framework CoreMedia"
29962996 bktr_indev_deps_any="dev_bktr_ioctl_bt848_h machine_ioctl_bt848_h dev_video_bktr_ioctl_bt848_h dev_ic_bt8xx_h"
29972997 caca_outdev_deps="libcaca"
2998−decklink_indev_deps="decklink pthreads"
2998+decklink_indev_deps="decklink threads"
29992999 decklink_indev_extralibs="-lstdc++"
3000−decklink_outdev_deps="decklink pthreads"
3000+decklink_outdev_deps="decklink threads"
30013001 decklink_outdev_extralibs="-lstdc++"
30023002 dshow_indev_deps="IBaseFilter"
30033003 dshow_indev_extralibs="-lpsapi -lole32 -lstrmiids -luuid -loleaut32 -lshlwapi"
libavdevice/decklink_common.cpp+0−3
@@ -26,9 +26,6 @@ · 25 unmodified lines
2626 #include <DeckLinkAPIDispatch.cpp>
2727 #endif
2828
29−#include <pthread.h>
30−#include <semaphore.h>
31−
3229 extern "C" {
3330 #include "libavformat/avformat.h"
3431 #include "libavformat/internal.h"
libavdevice/decklink_common.h+4−1
@@ -24,6 +24,7 @@ · 23 unmodified lines
2424
2525 #include <DeckLinkAPIVersion.h>
2626
27+#include "libavutil/thread.h"
2728 #include "decklink_common_c.h"
2829
2930 class decklink_output_callback;
@@ -89,7 +90,9 @@ struct decklink_ctx { · 59 unmodified lines
8990 int frames_preroll;
9091 int frames_buffer;
9192
92− sem_t semaphore;
93+ pthread_mutex_t mutex;
94+ pthread_cond_t cond;
95+ int frames_buffer_available_spots;
9396
9497 int channels;
9598 };
libavdevice/decklink_dec.cpp+0−3
@@ -21,9 +21,6 @@ · 20 unmodified lines
2121
2222 #include <DeckLinkAPI.h>
2323
24−#include <pthread.h>
25−#include <semaphore.h>
26−
2724 extern "C" {
2825 #include "config.h"
2926 #include "libavformat/avformat.h"
libavdevice/decklink_enc.cpp+15−8
@@ -24,9 +24,6 @@ using std::atomic; · 23 unmodified lines
2424
2525 #include <DeckLinkAPI.h>
2626
27−#include <pthread.h>
28−#include <semaphore.h>
29−
3027 extern "C" {
3128 #include "libavformat/avformat.h"
3229 #include "libavformat/internal.h"
@@ -91,7 +88,10 @@ public: · 58 unmodified lines
9188
9289 av_frame_unref(avframe);
9390
94− sem_post(&ctx->semaphore);
91+ pthread_mutex_lock(&ctx->mutex);
92+ ctx->frames_buffer_available_spots++;
93+ pthread_cond_broadcast(&ctx->cond);
94+ pthread_mutex_unlock(&ctx->mutex);
9595
9696 return S_OK;
9797 }
@@ -133,7 +133,6 @@ static int decklink_setup_video(AVFormatContext *avctx, AVStream *st) · 35 unmodified lines
133133 ctx->output_callback = new decklink_output_callback();
134134 ctx->dlo->SetScheduledFrameCompletionCallback(ctx->output_callback);
135135
136− /* Start video semaphore. */
137136 ctx->frames_preroll = st->time_base.den * ctx->preroll;
138137 if (st->time_base.den > 1000)
139138 ctx->frames_preroll /= 1000;
@@ -141,7 +140,9 @@ static int decklink_setup_video(AVFormatContext *avctx, AVStream *st) · 1 unmodified line
141140 /* Buffer twice as many frames as the preroll. */
142141 ctx->frames_buffer = ctx->frames_preroll * 2;
143142 ctx->frames_buffer = FFMIN(ctx->frames_buffer, 60);
144− sem_init(&ctx->semaphore, 0, ctx->frames_buffer);
143+ pthread_mutex_init(&ctx->mutex, NULL);
144+ pthread_cond_init(&ctx->cond, NULL);
145+ ctx->frames_buffer_available_spots = ctx->frames_buffer;
145146
146147 /* The device expects the framerate to be fixed. */
147148 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) · 63 unmodified lines
211212 if (ctx->output_callback)
212213 delete ctx->output_callback;
213214
214− sem_destroy(&ctx->semaphore);
215+ pthread_mutex_destroy(&ctx->mutex);
216+ pthread_cond_destroy(&ctx->cond);
215217
216218 av_freep(&cctx->ctx);
217219
@@ -247,7 +249,12 @@ static int decklink_write_video_packet(AVFormatContext *avctx, AVPacket *pkt) · 29 unmodified lines
247249 }
248250
249251 /* Always keep at most one second of frames buffered. */
250− sem_wait(&ctx->semaphore);
252+ pthread_mutex_lock(&ctx->mutex);
253+ while (ctx->frames_buffer_available_spots == 0) {
254+ pthread_cond_wait(&ctx->cond, &ctx->mutex);
255+ }
256+ ctx->frames_buffer_available_spots--;
257+ pthread_mutex_unlock(&ctx->mutex);
251258
252259 /* Schedule frame for playback. */
253260 hr = ctx->dlo->ScheduleVideoFrame((struct IDeckLinkVideoFrame *) frame,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t