Code Cleanup, rename outputbc to execbc, using buffered Writer
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package execbc
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"os/exec"
|
||||
"sync"
|
||||
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
medias []*core.Media
|
||||
sender *core.Sender
|
||||
conn net.Conn
|
||||
send int
|
||||
pipeCloser io.WriteCloser
|
||||
commandArgs []string
|
||||
cmd *exec.Cmd
|
||||
}
|
||||
|
||||
var lock = &sync.Mutex{}
|
||||
var singleInstance *Client
|
||||
|
||||
func NewClient(commandArgs []string) (*Client, error) {
|
||||
return &Client{commandArgs: commandArgs}, nil
|
||||
}
|
||||
|
||||
func (c *Client) Dial() error {
|
||||
media := &core.Media{
|
||||
Kind: core.KindAudio,
|
||||
Direction: core.DirectionSendonly,
|
||||
Codecs: []*core.Codec{
|
||||
{Name: core.CodecPCMA, ClockRate: 8000},
|
||||
},
|
||||
}
|
||||
|
||||
c.medias = append(c.medias, media)
|
||||
|
||||
cmdName := c.commandArgs[0]
|
||||
args := c.commandArgs[1:]
|
||||
cmd := *exec.Command(cmdName, args...)
|
||||
|
||||
pipeCloser, error := PipeCloser(&cmd)
|
||||
if error != nil {
|
||||
return error
|
||||
}
|
||||
c.pipeCloser = pipeCloser
|
||||
c.cmd = &cmd
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Client) Open() (err error) {
|
||||
c.cmd.Run()
|
||||
return
|
||||
}
|
||||
|
||||
func (c Client) Close() (err error) {
|
||||
return c.pipeCloser.Close()
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package outputbc
|
||||
package execbc
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -19,7 +19,7 @@ func (c *Client) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver
|
||||
if c.sender == nil {
|
||||
c.sender = core.NewSender(media, track.Codec)
|
||||
c.sender.Handler = func(packet *rtp.Packet) {
|
||||
c.pipe.Write(packet.Payload)
|
||||
c.pipeCloser.Write(packet.Payload)
|
||||
|
||||
c.send += len(packet.Payload)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package execbc
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"os/exec"
|
||||
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
)
|
||||
|
||||
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{bufio.NewWriterSize(stdin, 640), stdin, cmd}, nil
|
||||
}
|
||||
|
||||
func (p pipeCloser) Close() (err error) {
|
||||
return core.Any(p.Closer.Close(), p.cmd.Process.Kill(), p.cmd.Wait())
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package outputbc
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net"
|
||||
"os/exec"
|
||||
"sync"
|
||||
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
medias []*core.Media
|
||||
sender *core.Sender
|
||||
conn net.Conn
|
||||
send int
|
||||
cmd exec.Cmd
|
||||
pipe io.WriteCloser
|
||||
command []string
|
||||
}
|
||||
|
||||
var lock = &sync.Mutex{}
|
||||
var singleInstance *Client
|
||||
|
||||
func NewClient(command []string) (*Client, error) {
|
||||
return &Client{command: command}, nil
|
||||
}
|
||||
|
||||
func (c *Client) Dial() {
|
||||
media := &core.Media{
|
||||
Kind: core.KindAudio,
|
||||
Direction: core.DirectionSendonly,
|
||||
Codecs: []*core.Codec{
|
||||
{Name: core.CodecPCMA, ClockRate: 8000},
|
||||
},
|
||||
}
|
||||
|
||||
c.medias = append(c.medias, media)
|
||||
if c.pipe == nil {
|
||||
cmdName := c.command[0]
|
||||
args := c.command[1:]
|
||||
c.cmd = *exec.Command(cmdName, args...)
|
||||
c.pipe, _ = c.cmd.StdinPipe()
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Open() (err error) {
|
||||
c.cmd.Run()
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) Close() (err error) {
|
||||
c.pipe.Close()
|
||||
c.cmd.Process.Kill()
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user