From d8be7fc5a5aeee1826514cd8210585e89f6ef2da Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Mon, 15 Feb 2021 14:02:39 +0100 Subject: [PATCH] DShow: fix init race condition + debug --- src/video_capture/DirectShowGrabber.cpp | 26 +++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/video_capture/DirectShowGrabber.cpp b/src/video_capture/DirectShowGrabber.cpp index c72a6d1d6..a6346a597 100644 --- a/src/video_capture/DirectShowGrabber.cpp +++ b/src/video_capture/DirectShowGrabber.cpp @@ -80,7 +80,6 @@ struct vidcap_dshow_state { bool convert_YUYV_RGB; ///< @todo check - currently newer set struct video_frame *frame; - long frameLength; long grabBufferLen; long returnBufferLen; bool haveNewReturnBuffer; @@ -183,7 +182,10 @@ public: s->haveNewReturnBuffer = true; } LeaveCriticalSection(&s->returnBufferCS); - if (grabMightWait) WakeConditionVariable(&s->grabWaitCV); + if (grabMightWait) { + LOG(LOG_LEVEL_DEBUG) << MOD_NAME << "WakeConditionVariable s->grabWaitCV\n"; + WakeConditionVariable(&s->grabWaitCV); + } return S_OK; } @@ -268,6 +270,13 @@ static bool common_init(struct vidcap_dshow_state *s) { return false; } + // Create non-inheritable mutex without a name, owned by this thread + InitializeCriticalSectionAndSpinCount(&s->returnBufferCS, 0x40); + s->haveNewReturnBuffer = false; + + s->frames = 0; + gettimeofday(&s->t0, NULL); + // create device enumerator res = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&s->devEnumerator)); if (res != S_OK) { @@ -1234,15 +1243,7 @@ static int vidcap_dshow_init(struct vidcap_params *params, void **state) { goto error; } - // Create non-inheritable mutex without a name, owned by this thread - InitializeCriticalSectionAndSpinCount(&s->returnBufferCS, 0x40); - s->haveNewReturnBuffer = false; - s->frame = vf_alloc_desc(s->desc); - s->frameLength = 0; - - s->frames = 0; - gettimeofday(&s->t0, NULL); *state = s; return VIDCAP_INIT_OK; @@ -1289,15 +1290,16 @@ static struct video_frame * vidcap_dshow_grab(void *state, struct audio_frame ** struct vidcap_dshow_state *s = (struct vidcap_dshow_state *) state; *audio = NULL; - //fprintf(stderr, "[dshow] GRAB: enter: %d\n", s->deviceNumber); + LOG(LOG_LEVEL_DEBUG) << MOD_NAME << "GRAB: enter: " << s->deviceNumber << "\n"; EnterCriticalSection(&s->returnBufferCS); //fprintf(stderr, "[dshow] s: %p\n", s); while (!s->haveNewReturnBuffer) { - //fprintf(stderr, "[dshow] s: %p\n", s); + LOG(LOG_LEVEL_DEBUG) << MOD_NAME << "Wait CV\n"; SleepConditionVariableCS(&s->grabWaitCV, &s->returnBufferCS, INFINITE); //fprintf(stderr, "[dshow] s: %p\n", s); } + LOG(LOG_LEVEL_DEBUG) << MOD_NAME << "Swap buffers\n"; //fprintf(stderr, "[dshow] s: %p\n", s); // switch the buffers BYTE *tmp = s->returnBuffer;