Use io.ReadFull in Read()
This commit is contained in:
parent
05cff486bc
commit
b1d35e0f81
2 changed files with 32 additions and 33 deletions
31
camera.go
31
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()
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue