Code Cleanup, rename outputbc to execbc, using buffered Writer
This commit is contained in:
BIN
Binary file not shown.
@@ -1,19 +1,19 @@
|
|||||||
package outputbc
|
package execbc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/AlexxIT/go2rtc/internal/streams"
|
"github.com/AlexxIT/go2rtc/internal/streams"
|
||||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||||
"github.com/AlexxIT/go2rtc/pkg/outputbc"
|
"github.com/AlexxIT/go2rtc/pkg/execbc"
|
||||||
"github.com/AlexxIT/go2rtc/pkg/shell"
|
"github.com/AlexxIT/go2rtc/pkg/shell"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
streams.HandleFunc("outputbc", handle)
|
streams.HandleFunc("execbc", handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handle(url string) (core.Producer, error) {
|
func handle(url string) (core.Producer, error) {
|
||||||
args := shell.QuoteSplit(url[9:])
|
args := shell.QuoteSplit(url[7:])
|
||||||
con, err := outputbc.NewClient(args)
|
con, err := execbc.NewClient(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/AlexxIT/go2rtc/internal/outputbc"
|
|
||||||
|
|
||||||
"github.com/AlexxIT/go2rtc/internal/api"
|
"github.com/AlexxIT/go2rtc/internal/api"
|
||||||
"github.com/AlexxIT/go2rtc/internal/api/ws"
|
"github.com/AlexxIT/go2rtc/internal/api/ws"
|
||||||
"github.com/AlexxIT/go2rtc/internal/app"
|
"github.com/AlexxIT/go2rtc/internal/app"
|
||||||
@@ -11,6 +9,7 @@ import (
|
|||||||
"github.com/AlexxIT/go2rtc/internal/dvrip"
|
"github.com/AlexxIT/go2rtc/internal/dvrip"
|
||||||
"github.com/AlexxIT/go2rtc/internal/echo"
|
"github.com/AlexxIT/go2rtc/internal/echo"
|
||||||
"github.com/AlexxIT/go2rtc/internal/exec"
|
"github.com/AlexxIT/go2rtc/internal/exec"
|
||||||
|
"github.com/AlexxIT/go2rtc/internal/execbc"
|
||||||
"github.com/AlexxIT/go2rtc/internal/expr"
|
"github.com/AlexxIT/go2rtc/internal/expr"
|
||||||
"github.com/AlexxIT/go2rtc/internal/ffmpeg"
|
"github.com/AlexxIT/go2rtc/internal/ffmpeg"
|
||||||
"github.com/AlexxIT/go2rtc/internal/gopro"
|
"github.com/AlexxIT/go2rtc/internal/gopro"
|
||||||
@@ -82,7 +81,7 @@ func main() {
|
|||||||
bubble.Init() // bubble source
|
bubble.Init() // bubble source
|
||||||
expr.Init() // expr source
|
expr.Init() // expr source
|
||||||
gopro.Init() // gopro source
|
gopro.Init() // gopro source
|
||||||
outputbc.Init()
|
execbc.Init() // Local Backchannel
|
||||||
|
|
||||||
// 6. Helper modules
|
// 6. Helper modules
|
||||||
|
|
||||||
|
|||||||
@@ -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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -19,7 +19,7 @@ func (c *Client) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver
|
|||||||
if c.sender == nil {
|
if c.sender == nil {
|
||||||
c.sender = core.NewSender(media, track.Codec)
|
c.sender = core.NewSender(media, track.Codec)
|
||||||
c.sender.Handler = func(packet *rtp.Packet) {
|
c.sender.Handler = func(packet *rtp.Packet) {
|
||||||
c.pipe.Write(packet.Payload)
|
c.pipeCloser.Write(packet.Payload)
|
||||||
|
|
||||||
c.send += len(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