Fix running backchannel exec without start #1080

This commit is contained in:
Alex X
2024-05-03 15:57:18 +03:00
parent 2ea66deb08
commit a03db503c3
3 changed files with 13 additions and 38 deletions
+2 -10
View File
@@ -1,15 +1,13 @@
package stdin
import (
"io"
"os/exec"
"github.com/AlexxIT/go2rtc/pkg/core"
)
type Client struct {
cmd *exec.Cmd
pipe io.WriteCloser
cmd *exec.Cmd
medias []*core.Media
sender *core.Sender
@@ -17,14 +15,8 @@ type Client struct {
}
func NewClient(cmd *exec.Cmd) (*Client, error) {
pipe, err := PipeCloser(cmd)
if err != nil {
return nil, err
}
c := &Client{
pipe: pipe,
cmd: cmd,
cmd: cmd,
medias: []*core.Media{
{
Kind: core.KindAudio,
+11 -2
View File
@@ -2,6 +2,7 @@ package stdin
import (
"encoding/json"
"errors"
"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/pion/rtp"
@@ -17,9 +18,14 @@ func (c *Client) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver,
func (c *Client) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver) error {
if c.sender == nil {
stdin, err := c.cmd.StdinPipe()
if err != nil {
return err
}
c.sender = core.NewSender(media, track.Codec)
c.sender.Handler = func(packet *rtp.Packet) {
_, _ = c.pipe.Write(packet.Payload)
_, _ = stdin.Write(packet.Payload)
c.send += len(packet.Payload)
}
}
@@ -36,7 +42,10 @@ func (c *Client) Stop() (err error) {
if c.sender != nil {
c.sender.Close()
}
return c.pipe.Close()
if c.cmd.Process == nil {
return nil
}
return errors.Join(c.cmd.Process.Kill(), c.cmd.Wait())
}
func (c *Client) MarshalJSON() ([]byte, error) {
-26
View File
@@ -1,26 +0,0 @@
package stdin
import (
"errors"
"io"
"os/exec"
)
type pipeCloser struct {
io.Writer
io.Closer
cmd *exec.Cmd
}
func PipeCloser(cmd *exec.Cmd) (io.WriteCloser, error) {
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
return pipeCloser{stdin, stdin, cmd}, nil
}
func (p pipeCloser) Close() (err error) {
return errors.Join(p.Closer.Close(), p.cmd.Process.Kill(), p.cmd.Wait())
}