Skip to content

Commit

Permalink
use thread safe atomic bool
Browse files Browse the repository at this point in the history
  • Loading branch information
EmrysMyrddin authored and edaniels committed Sep 20, 2023
1 parent 694b4ab commit 2882fd4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
7 changes: 4 additions & 3 deletions pkg/codec/vaapi/vp8.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import (
"image"
"io"
"sync"
"sync/atomic"
"unsafe"

"github.com/pion/mediadevices/pkg/codec"
Expand Down Expand Up @@ -100,7 +101,7 @@ type encoderVP8 struct {

rate *framerateDetector

forceKeyFrame bool
forceKeyFrame atomic.Bool

mu sync.Mutex
closed bool
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions pkg/codec/vaapi/vp9.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"image"
"io"
"sync"
"sync/atomic"
"unsafe"

"github.com/pion/mediadevices/pkg/codec"
Expand Down Expand Up @@ -92,7 +93,7 @@ type encoderVP9 struct {

rate *framerateDetector

forceKeyFrame bool
forceKeyFrame atomic.Bool

mu sync.Mutex
closed bool
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 2882fd4

Please sign in to comment.