Skip to content

Commit

Permalink
Add probe stream API #998
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed May 15, 2024
1 parent c4949eb commit 4823e60
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
24 changes: 23 additions & 1 deletion internal/streams/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/AlexxIT/go2rtc/internal/api"
"github.com/AlexxIT/go2rtc/internal/app"
"github.com/AlexxIT/go2rtc/pkg/probe"
"github.com/AlexxIT/go2rtc/pkg/tcp"
"github.com/rs/zerolog"
)

Expand Down Expand Up @@ -148,7 +150,27 @@ func streamsHandler(w http.ResponseWriter, r *http.Request) {
// Not sure about all this API. Should be rewrited...
switch r.Method {
case "GET":
api.ResponsePrettyJSON(w, streams[src])
stream := Get(src)
if stream == nil {
http.Error(w, "", http.StatusNotFound)
return
}

cons := probe.NewProbe(query)
if len(cons.Medias) != 0 {
cons.RemoteAddr = tcp.RemoteAddr(r)
cons.UserAgent = r.UserAgent()
if err := stream.AddConsumer(cons); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

api.ResponsePrettyJSON(w, stream)

stream.RemoveConsumer(cons)
} else {
api.ResponsePrettyJSON(w, streams[src])
}

case "PUT":
name := query.Get("name")
Expand Down
70 changes: 70 additions & 0 deletions pkg/probe/probe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package probe

import (
"net/url"
"strings"

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

type Probe struct {
Type string `json:"type,omitempty"`
RemoteAddr string `json:"remote_addr,omitempty"`
UserAgent string `json:"user_agent,omitempty"`
Medias []*core.Media `json:"medias,omitempty"`
Receivers []*core.Receiver `json:"receivers,omitempty"`
Senders []*core.Sender `json:"senders,omitempty"`
}

func NewProbe(query url.Values) *Probe {
c := &Probe{Type: "probe"}
c.Medias = core.ParseQuery(query)

for _, value := range query["microphone"] {
media := &core.Media{Kind: core.KindAudio, Direction: core.DirectionRecvonly}

for _, name := range strings.Split(value, ",") {
name = strings.ToUpper(name)
switch name {
case "", "COPY":
name = core.CodecAny
}
media.Codecs = append(media.Codecs, &core.Codec{Name: name})
}

c.Medias = append(c.Medias, media)
}

return c
}

func (p *Probe) GetMedias() []*core.Media {
return p.Medias
}

func (p *Probe) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error {
sender := core.NewSender(media, codec)
sender.Bind(track)
p.Senders = append(p.Senders, sender)
return nil
}

func (p *Probe) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) {
receiver := core.NewReceiver(media, codec)
p.Receivers = append(p.Receivers, receiver)
return receiver, nil
}

func (p *Probe) Start() error {
return nil
}

func (p *Probe) Stop() error {
for _, receiver := range p.Receivers {
receiver.Close()
}
for _, sender := range p.Senders {
sender.Close()
}
return nil
}

0 comments on commit 4823e60

Please sign in to comment.