Skip to content

Commit

Permalink
Fix data race in drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
at-wat authored and lherman-cs committed Feb 20, 2020
1 parent 2bac44a commit 33b2964
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
4 changes: 3 additions & 1 deletion pkg/driver/audiotest/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ func (d *dummy) AudioRecord(p prop.Media) (audio.Reader, error) {
nextReadTime := time.Now()
var phase int

closed := d.closed

reader := audio.ReaderFunc(func(samples [][2]float32) (int, error) {
select {
case <-d.closed:
case <-closed:
return 0, io.EOF
default:
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/driver/camera/camera_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ func (c *camera) VideoRecord(p prop.Media) (video.Reader, error) {
return nil, err
}

cam := c.cam

ctx, cancel := context.WithCancel(context.Background())
c.cancel = cancel
var buf []byte
Expand All @@ -138,7 +140,7 @@ func (c *camera) VideoRecord(p prop.Media) (video.Reader, error) {
return nil, io.EOF
}

err := c.cam.WaitForFrame(5) // 5 seconds
err := cam.WaitForFrame(5) // 5 seconds
switch err.(type) {
case nil:
case *webcam.Timeout:
Expand All @@ -148,7 +150,7 @@ func (c *camera) VideoRecord(p prop.Media) (video.Reader, error) {
return nil, err
}

b, err := c.cam.ReadFrame()
b, err := cam.ReadFrame()
if err != nil {
// Camera has been stopped.
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion pkg/driver/screen/x11_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ func (s *screen) VideoRecord(p prop.Media) (video.Reader, error) {
s.tick = time.NewTicker(time.Duration(float32(time.Second) / p.FrameRate))

var dst image.RGBA
reader := s.reader

r := video.ReaderFunc(func() (image.Image, error) {
<-s.tick.C
return s.reader.Read().ToRGBA(&dst), nil
return reader.Read().ToRGBA(&dst), nil
})
return r, nil
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/driver/videotest/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,18 @@ func (d *dummy) VideoRecord(p prop.Media) (video.Reader, error) {
}
random := rand.New(rand.NewSource(0))

d.tick = time.NewTicker(time.Duration(float32(time.Second) / p.FrameRate))
tick := time.NewTicker(time.Duration(float32(time.Second) / p.FrameRate))
d.tick = tick
closed := d.closed

r := video.ReaderFunc(func() (image.Image, error) {
select {
case <-d.closed:
case <-closed:
return nil, io.EOF
default:
}

<-d.tick.C
<-tick.C

copy(yy, yyBase)
copy(cb, cbBase)
Expand Down

0 comments on commit 33b2964

Please sign in to comment.