Use io.ReadFull in Read()
This commit is contained in:
parent
05cff486bc
commit
b1d35e0f81
2 changed files with 32 additions and 33 deletions
15
camera.go
15
camera.go
|
@ -21,7 +21,7 @@ type Camera struct {
|
||||||
fps float64 // Camera frame rate.
|
fps float64 // Camera frame rate.
|
||||||
codec string // Camera codec.
|
codec string // Camera codec.
|
||||||
framebuffer []byte // Raw frame data.
|
framebuffer []byte // Raw frame data.
|
||||||
pipe *io.ReadCloser // Stdout pipe for ffmpeg process streaming webcam.
|
pipe io.ReadCloser // Stdout pipe for ffmpeg process streaming webcam.
|
||||||
cmd *exec.Cmd // ffmpeg command.
|
cmd *exec.Cmd // ffmpeg command.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,7 +219,7 @@ func (camera *Camera) init() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
camera.pipe = &pipe
|
camera.pipe = pipe
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -240,10 +240,9 @@ func (camera *Camera) Read() bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
total := 0
|
if _, err := io.ReadFull(camera.pipe, camera.framebuffer); err != nil {
|
||||||
for total < camera.width*camera.height*camera.depth {
|
camera.Close()
|
||||||
n, _ := (*camera.pipe).Read(camera.framebuffer[total:])
|
return false
|
||||||
total += n
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
@ -252,7 +251,7 @@ func (camera *Camera) Read() bool {
|
||||||
// Closes the pipe and stops the ffmpeg process.
|
// Closes the pipe and stops the ffmpeg process.
|
||||||
func (camera *Camera) Close() {
|
func (camera *Camera) Close() {
|
||||||
if camera.pipe != nil {
|
if camera.pipe != nil {
|
||||||
(*camera.pipe).Close()
|
camera.pipe.Close()
|
||||||
}
|
}
|
||||||
if camera.cmd != nil {
|
if camera.cmd != nil {
|
||||||
camera.cmd.Process.Kill()
|
camera.cmd.Process.Kill()
|
||||||
|
@ -267,7 +266,7 @@ func (camera *Camera) cleanup() {
|
||||||
go func() {
|
go func() {
|
||||||
<-c
|
<-c
|
||||||
if camera.pipe != nil {
|
if camera.pipe != nil {
|
||||||
(*camera.pipe).Close()
|
camera.pipe.Close()
|
||||||
}
|
}
|
||||||
if camera.cmd != nil {
|
if camera.cmd != nil {
|
||||||
camera.cmd.Process.Kill()
|
camera.cmd.Process.Kill()
|
||||||
|
|
|
@ -23,7 +23,7 @@ type VideoWriter struct {
|
||||||
fps float64 // Frames per second for output video. Default 25.
|
fps float64 // Frames per second for output video. Default 25.
|
||||||
quality float64 // Used if bitrate not given. Default 0.5.
|
quality float64 // Used if bitrate not given. Default 0.5.
|
||||||
codec string // Codec to encode video with. Default libx264.
|
codec string // Codec to encode video with. Default libx264.
|
||||||
pipe *io.WriteCloser // Stdout pipe of ffmpeg process.
|
pipe io.WriteCloser // Stdout pipe of ffmpeg process.
|
||||||
cmd *exec.Cmd // ffmpeg command.
|
cmd *exec.Cmd // ffmpeg command.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,7 +257,7 @@ func (writer *VideoWriter) init() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
writer.pipe = &pipe
|
writer.pipe = pipe
|
||||||
|
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -277,7 +277,7 @@ func (writer *VideoWriter) Write(frame []byte) error {
|
||||||
|
|
||||||
total := 0
|
total := 0
|
||||||
for total < len(frame) {
|
for total < len(frame) {
|
||||||
n, err := (*writer.pipe).Write(frame[total:])
|
n, err := writer.pipe.Write(frame[total:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -290,7 +290,7 @@ func (writer *VideoWriter) Write(frame []byte) error {
|
||||||
// Closes the pipe and stops the ffmpeg process.
|
// Closes the pipe and stops the ffmpeg process.
|
||||||
func (writer *VideoWriter) Close() {
|
func (writer *VideoWriter) Close() {
|
||||||
if writer.pipe != nil {
|
if writer.pipe != nil {
|
||||||
(*writer.pipe).Close()
|
writer.pipe.Close()
|
||||||
}
|
}
|
||||||
if writer.cmd != nil {
|
if writer.cmd != nil {
|
||||||
writer.cmd.Wait()
|
writer.cmd.Wait()
|
||||||
|
@ -305,7 +305,7 @@ func (writer *VideoWriter) cleanup() {
|
||||||
go func() {
|
go func() {
|
||||||
<-c
|
<-c
|
||||||
if writer.pipe != nil {
|
if writer.pipe != nil {
|
||||||
(*writer.pipe).Close()
|
writer.pipe.Close()
|
||||||
}
|
}
|
||||||
if writer.cmd != nil {
|
if writer.cmd != nil {
|
||||||
writer.cmd.Process.Kill()
|
writer.cmd.Process.Kill()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue