From b1d35e0f81c66160f7f9d4397651ce99dabc3b28 Mon Sep 17 00:00:00 2001 From: Alex Eidt Date: Fri, 17 Mar 2023 10:49:43 -0700 Subject: [PATCH] Use io.ReadFull in Read() --- camera.go | 31 +++++++++++++++---------------- videowriter.go | 34 +++++++++++++++++----------------- 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/camera.go b/camera.go index a5c18fd..c562532 100644 --- a/camera.go +++ b/camera.go @@ -14,15 +14,15 @@ import ( ) type Camera struct { - name string // Camera device name. - width int // Camera frame width. - height int // Camera frame height. - depth int // Camera frame depth. - fps float64 // Camera frame rate. - codec string // Camera codec. - framebuffer []byte // Raw frame data. - pipe *io.ReadCloser // Stdout pipe for ffmpeg process streaming webcam. - cmd *exec.Cmd // ffmpeg command. + name string // Camera device name. + width int // Camera frame width. + height int // Camera frame height. + depth int // Camera frame depth. + fps float64 // Camera frame rate. + codec string // Camera codec. + framebuffer []byte // Raw frame data. + pipe io.ReadCloser // Stdout pipe for ffmpeg process streaming webcam. + cmd *exec.Cmd // ffmpeg command. } // Camera device name. @@ -219,7 +219,7 @@ func (camera *Camera) init() error { return err } - camera.pipe = &pipe + camera.pipe = pipe if err := cmd.Start(); err != nil { return err } @@ -240,10 +240,9 @@ func (camera *Camera) Read() bool { } } - total := 0 - for total < camera.width*camera.height*camera.depth { - n, _ := (*camera.pipe).Read(camera.framebuffer[total:]) - total += n + if _, err := io.ReadFull(camera.pipe, camera.framebuffer); err != nil { + camera.Close() + return false } return true @@ -252,7 +251,7 @@ func (camera *Camera) Read() bool { // Closes the pipe and stops the ffmpeg process. func (camera *Camera) Close() { if camera.pipe != nil { - (*camera.pipe).Close() + camera.pipe.Close() } if camera.cmd != nil { camera.cmd.Process.Kill() @@ -267,7 +266,7 @@ func (camera *Camera) cleanup() { go func() { <-c if camera.pipe != nil { - (*camera.pipe).Close() + camera.pipe.Close() } if camera.cmd != nil { camera.cmd.Process.Kill() diff --git a/videowriter.go b/videowriter.go index 084a0ea..359e5de 100644 --- a/videowriter.go +++ b/videowriter.go @@ -12,19 +12,19 @@ import ( ) type VideoWriter struct { - filename string // Output filename. - streamfile string // Extra stream data filename. - width int // Frame width. - height int // Frame height. - bitrate int // Output video bitrate. - loop int // Number of times for GIF to loop. - delay int // Delay of final frame of GIF. Default -1 (same delay as previous frame). - macro int // Macroblock size for determining how to resize frames for codecs. - fps float64 // Frames per second for output video. Default 25. - quality float64 // Used if bitrate not given. Default 0.5. - codec string // Codec to encode video with. Default libx264. - pipe *io.WriteCloser // Stdout pipe of ffmpeg process. - cmd *exec.Cmd // ffmpeg command. + filename string // Output filename. + streamfile string // Extra stream data filename. + width int // Frame width. + height int // Frame height. + bitrate int // Output video bitrate. + loop int // Number of times for GIF to loop. + delay int // Delay of final frame of GIF. Default -1 (same delay as previous frame). + macro int // Macroblock size for determining how to resize frames for codecs. + fps float64 // Frames per second for output video. Default 25. + quality float64 // Used if bitrate not given. Default 0.5. + codec string // Codec to encode video with. Default libx264. + pipe io.WriteCloser // Stdout pipe of ffmpeg process. + cmd *exec.Cmd // ffmpeg command. } // Optional parameters for VideoWriter. @@ -257,7 +257,7 @@ func (writer *VideoWriter) init() error { if err != nil { return err } - writer.pipe = &pipe + writer.pipe = pipe if err := cmd.Start(); err != nil { return err @@ -277,7 +277,7 @@ func (writer *VideoWriter) Write(frame []byte) error { total := 0 for total < len(frame) { - n, err := (*writer.pipe).Write(frame[total:]) + n, err := writer.pipe.Write(frame[total:]) if err != nil { return err } @@ -290,7 +290,7 @@ func (writer *VideoWriter) Write(frame []byte) error { // Closes the pipe and stops the ffmpeg process. func (writer *VideoWriter) Close() { if writer.pipe != nil { - (*writer.pipe).Close() + writer.pipe.Close() } if writer.cmd != nil { writer.cmd.Wait() @@ -305,7 +305,7 @@ func (writer *VideoWriter) cleanup() { go func() { <-c if writer.pipe != nil { - (*writer.pipe).Close() + writer.pipe.Close() } if writer.cmd != nil { writer.cmd.Process.Kill()