mirror of
https://github.com/pion/mediadevices.git
synced 2025-10-05 08:36:55 +08:00
Use Mutex instead of WaitGroup
This commit is contained in:

committed by
Lukas Herman

parent
29d0299c5c
commit
993c23ee90
@@ -53,13 +53,6 @@ func BenchmarkRead(b *testing.B) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
b.Skipf("Failed to capture image: %v", err)
|
b.Skipf("Failed to capture image: %v", err)
|
||||||
}
|
}
|
||||||
defer func() {
|
|
||||||
if r != nil {
|
|
||||||
go func() {
|
|
||||||
_, _ = r.Read() // Workaround for deadlock
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
|
@@ -35,7 +35,7 @@ type camera struct {
|
|||||||
formats map[webcam.PixelFormat]frame.Format
|
formats map[webcam.PixelFormat]frame.Format
|
||||||
reversedFormats map[frame.Format]webcam.PixelFormat
|
reversedFormats map[frame.Format]webcam.PixelFormat
|
||||||
started bool
|
started bool
|
||||||
wg *sync.WaitGroup
|
mutex sync.Mutex
|
||||||
cancel func()
|
cancel func()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,8 +90,9 @@ func (c *camera) Close() error {
|
|||||||
if c.cancel != nil {
|
if c.cancel != nil {
|
||||||
// Let the reader knows that the caller has closed the camera
|
// Let the reader knows that the caller has closed the camera
|
||||||
c.cancel()
|
c.cancel()
|
||||||
// Wait until the reader receives the cancelation
|
// Wait until the reader unref the buffer
|
||||||
c.wg.Wait()
|
c.mutex.Lock()
|
||||||
|
defer c.mutex.Unlock()
|
||||||
|
|
||||||
// Note: StopStreaming frees frame buffers even if they are still used in Go code.
|
// Note: StopStreaming frees frame buffers even if they are still used in Go code.
|
||||||
// There is currently no convenient way to do this safely.
|
// There is currently no convenient way to do this safely.
|
||||||
@@ -121,14 +122,15 @@ func (c *camera) VideoRecord(p prop.Media) (video.Reader, error) {
|
|||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
c.cancel = cancel
|
c.cancel = cancel
|
||||||
c.wg = &sync.WaitGroup{}
|
|
||||||
c.wg.Add(1)
|
|
||||||
var buf []byte
|
var buf []byte
|
||||||
r := video.ReaderFunc(func() (img image.Image, err error) {
|
r := video.ReaderFunc(func() (img image.Image, err error) {
|
||||||
|
// Lock to avoid accessing the buffer after StopStreaming()
|
||||||
|
c.mutex.Lock()
|
||||||
|
defer c.mutex.Unlock()
|
||||||
|
|
||||||
// Wait until a frame is ready
|
// Wait until a frame is ready
|
||||||
for i := 0; i < maxEmptyFrameCount; i++ {
|
for i := 0; i < maxEmptyFrameCount; i++ {
|
||||||
if ctx.Err() != nil {
|
if ctx.Err() != nil {
|
||||||
c.wg.Done()
|
|
||||||
// Return EOF if the camera is already closed.
|
// Return EOF if the camera is already closed.
|
||||||
return nil, io.EOF
|
return nil, io.EOF
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user