Skip to content

Commit

Permalink
Fix ghost exec/ffmpeg process
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Jun 5, 2024
1 parent 756be98 commit 9bb36eb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
8 changes: 6 additions & 2 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func handleRTSP(url string, cmd *exec.Cmd, path string) (core.Producer, error) {
cmd.Stdout = os.Stdout
}

waiter := make(chan core.Producer)
waiter := make(chan *pkg.Conn, 1)

waitersMu.Lock()
waiters[path] = waiter
Expand Down Expand Up @@ -149,6 +149,10 @@ func handleRTSP(url string, cmd *exec.Cmd, path string) (core.Producer, error) {
return nil, fmt.Errorf("exec/rtsp\n%s", cmd.Stderr)
case prod := <-waiter:
log.Debug().Stringer("launch", time.Since(ts)).Msg("[exec] run rtsp")
prod.OnClose = func() error {
log.Debug().Msgf("[exec] kill rtsp")
return errors.Join(cmd.Process.Kill(), cmd.Wait())
}
return prod, nil
}
}
Expand All @@ -157,7 +161,7 @@ func handleRTSP(url string, cmd *exec.Cmd, path string) (core.Producer, error) {

var (
log zerolog.Logger
waiters = map[string]chan core.Producer{}
waiters = make(map[string]chan *pkg.Conn)
waitersMu sync.Mutex
)

Expand Down
6 changes: 5 additions & 1 deletion internal/streams/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (p *Producer) reconnect(workerID, retry int) {
for _, media := range conn.GetMedias() {
switch media.Direction {
case core.DirectionRecvonly:
for _, receiver := range p.receivers {
for i, receiver := range p.receivers {
codec := media.MatchCodec(receiver.Codec)
if codec == nil {
continue
Expand All @@ -219,6 +219,7 @@ func (p *Producer) reconnect(workerID, retry int) {
}

receiver.Replace(track)
p.receivers[i] = track
break
}

Expand All @@ -234,6 +235,9 @@ func (p *Producer) reconnect(workerID, retry int) {
}
}

// stop previous connection after moving tracks (fix ghost exec/ffmpeg)
_ = p.conn.Stop()
// swap connections
p.conn = conn

go p.worker(conn, workerID)
Expand Down
3 changes: 3 additions & 0 deletions pkg/rtsp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,5 +304,8 @@ func (c *Conn) Close() error {
if c.mode == core.ModeActiveProducer {
_ = c.Teardown()
}
if c.OnClose != nil {
_ = c.OnClose()
}
return c.conn.Close()
}
1 change: 1 addition & 0 deletions pkg/rtsp/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Conn struct {

Backchannel bool
Media string
OnClose func() error
PacketSize uint16
SessionName string
Timeout int
Expand Down

0 comments on commit 9bb36eb

Please sign in to comment.