From 410e85617f241710e18f8b73832afa31b9e272ee Mon Sep 17 00:00:00 2001 From: Valentin Cocaud Date: Wed, 20 Sep 2023 21:30:25 +0200 Subject: [PATCH] use thread safe atomic bool --- pkg/codec/vaapi/vp8.go | 7 ++++--- pkg/codec/vaapi/vp9.go | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/pkg/codec/vaapi/vp8.go b/pkg/codec/vaapi/vp8.go index 69c7c101..571b9bba 100644 --- a/pkg/codec/vaapi/vp8.go +++ b/pkg/codec/vaapi/vp8.go @@ -62,6 +62,7 @@ import ( "image" "io" "sync" + "sync/atomic" "unsafe" "github.com/pion/mediadevices/pkg/codec" @@ -100,7 +101,7 @@ type encoderVP8 struct { rate *framerateDetector - forceKeyFrame bool + forceKeyFrame atomic.Bool mu sync.Mutex closed bool @@ -318,7 +319,7 @@ func (e *encoderVP8) Read() ([]byte, func(), error) { e.frParam.data.framerate = C.uint(e.rate.Calc()) - if kf || e.forceKeyFrame { + if kf || e.forceKeyFrame.CompareAndSwap(true, false) { // Key frame C.setForceKFFlagVP8(&e.picParam, 1) C.setFrameTypeFlagVP8(&e.picParam, 0) @@ -549,7 +550,7 @@ func (e *encoderVP8) Controller() codec.EncoderController { } func (e *encoderVP8) ForceKeyFrame() { - e.forceKeyFrame = true + e.forceKeyFrame.Store(true) } func (e *encoderVP8) Close() error { diff --git a/pkg/codec/vaapi/vp9.go b/pkg/codec/vaapi/vp9.go index 1ee3a1b0..d530fee7 100644 --- a/pkg/codec/vaapi/vp9.go +++ b/pkg/codec/vaapi/vp9.go @@ -45,6 +45,7 @@ import ( "image" "io" "sync" + "sync/atomic" "unsafe" "github.com/pion/mediadevices/pkg/codec" @@ -92,7 +93,7 @@ type encoderVP9 struct { rate *framerateDetector - forceKeyFrame bool + forceKeyFrame atomic.Bool mu sync.Mutex closed bool @@ -307,7 +308,7 @@ func (e *encoderVP9) Read() ([]byte, func(), error) { e.frParam.data.framerate = C.uint(e.rate.Calc()) - if kf || e.forceKeyFrame { + if kf || e.forceKeyFrame.CompareAndSwap(true, false) { C.setForceKFFlag9(&e.picParam, 1) C.setFrameTypeFlagVP9(&e.picParam, 0) e.picParam.refresh_frame_flags = 0 @@ -484,7 +485,7 @@ func (e *encoderVP9) Controller() codec.EncoderController { } func (e *encoderVP9) ForceKeyFrame() { - e.forceKeyFrame = true + e.forceKeyFrame.Store(true) } func (e *encoderVP9) Close() error {