Skip to content

Commit

Permalink
ffmpeg 添加timeout参数,防止拉取一个错误的rtsp流时一直阻塞
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghaoxi3000 committed Apr 16, 2020
1 parent e784b42 commit 9b27b4e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
6 changes: 0 additions & 6 deletions serializer/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ import (
"github.com/gin-gonic/gin"
)

// 自定义错误
const (
ErrorFFMPEGStart = 5001 // FFMPEG 启动失败
)


// Response 基础序列化器
type Response struct {
Code int `json:"code"`
Expand Down
9 changes: 6 additions & 3 deletions service/rtsptrans.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (service *RTSPTransSrv) Service() *serializer.Response {
} else {
reflush := make(chan int)
if cmd, stdin, err := runFFMPEG(service.URL, processCh); err != nil {
return serializer.Err(serializer.ErrorFFMPEGStart, err.Error(), err)
return serializer.Err(400, err.Error(), err)
} else {
go keepFFMPEG(cmd, stdin, &reflush, processCh)
}
Expand All @@ -69,7 +69,7 @@ func keepFFMPEG(cmd *exec.Cmd, stdin io.WriteCloser, ch *chan int, playCh string
_, _ = stdin.Write([]byte("q"))
err := cmd.Wait()
if err != nil {
util.Log().Error("Run ffmpeg err", err.Error())
util.Log().Error("Run ffmpeg err %v", err.Error())
}
return
}
Expand All @@ -81,6 +81,8 @@ func runFFMPEG(rtsp string, playCh string) (*exec.Cmd, io.WriteCloser, error) {
"-rtsp_transport",
"tcp",
"-re",
"-timeout",
"5",
"-i",
rtsp,
"-q",
Expand All @@ -97,12 +99,13 @@ func runFFMPEG(rtsp string, playCh string) (*exec.Cmd, io.WriteCloser, error) {
fmt.Sprintf("http://127.0.0.1:3000/stream/upload/%s", playCh),
}

util.Log().Debug("FFmpeg cmd: ffmpeg %v", strings.Join(params, " "))
cmd := exec.Command("ffmpeg", params...)
cmd.Stdout = nil
cmd.Stderr = nil
stdin, err := cmd.StdinPipe()
if err != nil {
util.Log().Error("Get ffmpeg stdin err:%v", err)
util.Log().Error("Get ffmpeg stdin err:%v", err.Error())
return nil, nil, errors.New("拉流进程启动失败")
}

Expand Down

0 comments on commit 9b27b4e

Please sign in to comment.