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 {
|
type Camera struct {
|
||||||
name string // Camera device name.
|
name string // Camera device name.
|
||||||
width int // Camera frame width.
|
width int // Camera frame width.
|
||||||
height int // Camera frame height.
|
height int // Camera frame height.
|
||||||
depth int // Camera frame depth.
|
depth int // Camera frame depth.
|
||||||
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.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Camera device name.
|
// Camera device name.
|
||||||
|
@ -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()
|
||||||
|
|
|
@ -12,19 +12,19 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type VideoWriter struct {
|
type VideoWriter struct {
|
||||||
filename string // Output filename.
|
filename string // Output filename.
|
||||||
streamfile string // Extra stream data filename.
|
streamfile string // Extra stream data filename.
|
||||||
width int // Frame width.
|
width int // Frame width.
|
||||||
height int // Frame height.
|
height int // Frame height.
|
||||||
bitrate int // Output video bitrate.
|
bitrate int // Output video bitrate.
|
||||||
loop int // Number of times for GIF to loop.
|
loop int // Number of times for GIF to loop.
|
||||||
delay int // Delay of final frame of GIF. Default -1 (same delay as previous frame).
|
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.
|
macro int // Macroblock size for determining how to resize frames for codecs.
|
||||||
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.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optional parameters for VideoWriter.
|
// Optional parameters for VideoWriter.
|
||||||
|
@ -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