From patchwork Mon Jun 4 20:21:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 9249 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:11c:0:0:0:0:0 with SMTP id c28-v6csp68280jad; Mon, 4 Jun 2018 13:21:56 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKdkxqo/godEndEbdvSOzd0yrn/jL5DlYxydIEou3txC25F06W9NkA/Qwut4uyC3DIONYFX X-Received: by 2002:adf:f546:: with SMTP id j6-v6mr16250119wrp.241.1528143716176; Mon, 04 Jun 2018 13:21:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528143716; cv=none; d=google.com; s=arc-20160816; b=C5qsXZ/6GuTXWAUd9ktjzmRr/6vpWKdI0TCujkEswRIXeW36NaD7+SNm6R2oqFTV/Q wb3gmVkUlU0QSJgWkVQscPcvBAG9msJgXGkzjIlXVEtKQfGETcaKVQN9yi/vGWG/ebIc bS+HKGHKlstOfXtsA+CNUTSjpS9ueeUWIGl8DxJp+Qt5XwjXCCD8pJBgIRwPR1CncsZg KS3DVcaH271jfKWDsKXG0xw9xS20wV/z6r3DIDobzrfueiegTvoERxOMpkIeqd4ZfF7B /+DYN4ygRHE892bC7C5iLUbvKp+5AV2h94Tvfs1HLX8Dy9qmROGCeWryNbvV9B25caUI wRhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:delivered-to:arc-authentication-results; bh=zB4aAeOUc1bItmd6ZDKzmjtPNS7HGT6U44DTxosJwyk=; b=gxsMpWKTENZOVnTu2lsH39boZX+RULd43pgysqVzDjqL/dHqvCUX76zIOEEd/40cpD 7GRSm1d4U4dJX7WRqnZBNeHF1ks4rmglHl7f7Xz18kEgNbyvDw3RuaVpmEh4RG9LKvTy hlQ110hR+HnC4PMdnLgArktTnhaSA070TRCplzqPLBzDxUM0Oo9VmW+k2OUdQUXzUS5T vvzCUf/YLYmcEmiFFye/7DO75ssejdcczaE7b5UhNOx6llJcjprP2NBBweVbwsKf7Yw2 j2RvvW9nwrA03mNwjG0LlDHvNzm3AKExq15bG2dXaUUlXZCeNALw1Ovw7i5XmBGdI36L l1Xg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id t14-v6si711327wmd.203.2018.06.04.13.21.55; Mon, 04 Jun 2018 13:21:56 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E54F668A050; Mon, 4 Jun 2018 23:21:00 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 31513689B7C for ; Mon, 4 Jun 2018 23:20:55 +0300 (EEST) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 12C1BE10BD; Mon, 4 Jun 2018 22:21:42 +0200 (CEST) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id dgz7EVKbHQIt; Mon, 4 Jun 2018 22:21:40 +0200 (CEST) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id A7445E0E32; Mon, 4 Jun 2018 22:21:40 +0200 (CEST) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Jun 2018 22:21:31 +0200 Message-Id: <20180604202131.6529-2-cus@passwd.hu> X-Mailer: git-send-email 2.16.3 In-Reply-To: <20180604202131.6529-1-cus@passwd.hu> References: <20180604202131.6529-1-cus@passwd.hu> Subject: [FFmpeg-devel] [PATCH 2/2] avdevice/decklink_dec: use std::atomic for decklink_input_callback refcounting X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Also remove the callback from the context, and add proper error handling. Signed-off-by: Marton Balint --- libavdevice/decklink_common.h | 1 - libavdevice/decklink_dec.cpp | 35 ++++++++++++++--------------------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/libavdevice/decklink_common.h b/libavdevice/decklink_common.h index 57ee7d1d68..f416134b8a 100644 --- a/libavdevice/decklink_common.h +++ b/libavdevice/decklink_common.h @@ -56,7 +56,6 @@ struct decklink_ctx { IDeckLinkConfiguration *cfg; IDeckLinkAttributes *attr; decklink_output_callback *output_callback; - decklink_input_callback *input_callback; /* DeckLink mode information */ BMDTimeValue bmd_tb_den; diff --git a/libavdevice/decklink_dec.cpp b/libavdevice/decklink_dec.cpp index 897fca1003..974ee1d94c 100644 --- a/libavdevice/decklink_dec.cpp +++ b/libavdevice/decklink_dec.cpp @@ -596,8 +596,7 @@ public: virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame*, IDeckLinkAudioInputPacket*); private: - ULONG m_refCount; - pthread_mutex_t m_mutex; + std::atomic _refs; AVFormatContext *avctx; decklink_ctx *ctx; int no_video; @@ -605,42 +604,30 @@ private: int64_t initial_audio_pts; }; -decklink_input_callback::decklink_input_callback(AVFormatContext *_avctx) : m_refCount(0) +decklink_input_callback::decklink_input_callback(AVFormatContext *_avctx) : _refs(1) { avctx = _avctx; decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data; ctx = (struct decklink_ctx *)cctx->ctx; no_video = 0; initial_audio_pts = initial_video_pts = AV_NOPTS_VALUE; - pthread_mutex_init(&m_mutex, NULL); } decklink_input_callback::~decklink_input_callback() { - pthread_mutex_destroy(&m_mutex); } ULONG decklink_input_callback::AddRef(void) { - pthread_mutex_lock(&m_mutex); - m_refCount++; - pthread_mutex_unlock(&m_mutex); - - return (ULONG)m_refCount; + return ++_refs; } ULONG decklink_input_callback::Release(void) { - pthread_mutex_lock(&m_mutex); - m_refCount--; - pthread_mutex_unlock(&m_mutex); - - if (m_refCount == 0) { + int ret = --_refs; + if (!ret) delete this; - return 0; - } - - return (ULONG)m_refCount; + return ret; } static int64_t get_pkt_pts(IDeckLinkVideoInputFrame *videoFrame, @@ -966,6 +953,7 @@ av_cold int ff_decklink_read_header(AVFormatContext *avctx) struct decklink_cctx *cctx = (struct decklink_cctx *)avctx->priv_data; struct decklink_ctx *ctx; class decklink_allocator *allocator; + class decklink_input_callback *input_callback; AVStream *st; HRESULT result; char fname[1024]; @@ -1056,8 +1044,13 @@ av_cold int ff_decklink_read_header(AVFormatContext *avctx) goto error; } - ctx->input_callback = new decklink_input_callback(avctx); - ctx->dli->SetCallback(ctx->input_callback); + input_callback = new decklink_input_callback(avctx); + ret = (ctx->dli->SetCallback(input_callback) == S_OK ? 0 : AVERROR_EXTERNAL); + input_callback->Release(); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Cannot set input callback\n"); + goto error; + } allocator = new decklink_allocator(); ret = (ctx->dli->SetVideoInputFrameMemoryAllocator(allocator) == S_OK ? 0 : AVERROR_EXTERNAL);