Skip to content

Commit

Permalink
Code refactoring after #859
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 30, 2024
1 parent 7a0646f commit 5283222
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 96 deletions.
5 changes: 5 additions & 0 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/AlexxIT/go2rtc/pkg/magic"
pkg "github.com/AlexxIT/go2rtc/pkg/rtsp"
"github.com/AlexxIT/go2rtc/pkg/shell"
"github.com/AlexxIT/go2rtc/pkg/stdin"
"github.com/rs/zerolog"
)

Expand Down Expand Up @@ -79,6 +80,10 @@ func execHandle(rawURL string) (core.Producer, error) {
}

func handlePipe(_ string, cmd *exec.Cmd, query url.Values) (core.Producer, error) {
if query.Get("backchannel") == "1" {
return stdin.NewClient(cmd)
}

r, err := PipeCloser(cmd, query)
if err != nil {
return nil, err
Expand Down
21 changes: 0 additions & 21 deletions internal/execbc/init.go

This file was deleted.

2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/AlexxIT/go2rtc/internal/dvrip"
"github.com/AlexxIT/go2rtc/internal/echo"
"github.com/AlexxIT/go2rtc/internal/exec"
"github.com/AlexxIT/go2rtc/internal/execbc"
"github.com/AlexxIT/go2rtc/internal/expr"
"github.com/AlexxIT/go2rtc/internal/ffmpeg"
"github.com/AlexxIT/go2rtc/internal/gopro"
Expand Down Expand Up @@ -81,7 +80,6 @@ func main() {
bubble.Init() // bubble source
expr.Init() // expr source
gopro.Init() // gopro source
execbc.Init() // Local Backchannel

// 6. Helper modules

Expand Down
53 changes: 0 additions & 53 deletions pkg/execbc/client.go

This file was deleted.

41 changes: 41 additions & 0 deletions pkg/stdin/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package stdin

import (
"io"
"os/exec"

"github.com/AlexxIT/go2rtc/pkg/core"
)

type Client struct {
cmd *exec.Cmd
pipe io.WriteCloser

medias []*core.Media
sender *core.Sender
send int
}

func NewClient(cmd *exec.Cmd) (*Client, error) {
pipe, err := PipeCloser(cmd)
if err != nil {
return nil, err
}

c := &Client{
pipe: pipe,
cmd: cmd,
medias: []*core.Media{
{
Kind: core.KindAudio,
Direction: core.DirectionSendonly,
Codecs: []*core.Codec{
{Name: core.CodecPCMA, ClockRate: 8000},
{Name: core.CodecPCM},
},
},
},
}

return c, nil
}
20 changes: 5 additions & 15 deletions pkg/execbc/consumer.go → pkg/stdin/consumer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package execbc
package stdin

import (
"encoding/json"
Expand All @@ -19,8 +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.pipeCloser.Write(packet.Payload)

_, _ = c.pipe.Write(packet.Payload)
c.send += len(packet.Payload)
}
}
Expand All @@ -30,28 +29,19 @@ func (c *Client) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver
}

func (c *Client) Start() (err error) {
if err = c.Open(); err != nil {
return
}
return
return c.cmd.Run()
}

func (c *Client) Stop() (err error) {
if c.sender != nil {
c.sender.Close()
}

if c.conn != nil {
_ = c.Close()
return c.conn.Close()
}

return nil
return c.pipe.Close()
}

func (c *Client) MarshalJSON() ([]byte, error) {
info := &core.Info{
Type: "Command Backchannel PCMA",
Type: "Exec active consumer",
Medias: c.medias,
Send: c.send,
}
Expand Down
9 changes: 4 additions & 5 deletions pkg/execbc/pipe.go → pkg/stdin/pipe.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package execbc
package stdin

import (
"errors"
"io"
"os/exec"

"github.com/AlexxIT/go2rtc/pkg/core"
)

type pipeCloser struct {
Expand All @@ -15,13 +14,13 @@ type pipeCloser struct {

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 core.Any(p.Closer.Close(), p.cmd.Process.Kill(), p.cmd.Wait())
return errors.Join(p.Closer.Close(), p.cmd.Process.Kill(), p.cmd.Wait())
}

0 comments on commit 5283222

Please sign in to comment.