fix client publishing protocol
This commit is contained in:
+66
-87
@@ -1,7 +1,6 @@
|
|||||||
package client
|
package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"container/ring"
|
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -24,29 +23,29 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type RTCClient struct {
|
type RTCClient struct {
|
||||||
id string
|
id string
|
||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
PeerConn *webrtc.PeerConnection
|
PeerConn *webrtc.PeerConnection
|
||||||
localTracks []webrtc.TrackLocal
|
// sid => track
|
||||||
|
localTracks map[string]webrtc.TrackLocal
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
|
wsLock sync.Mutex
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
connected bool
|
connected bool
|
||||||
iceConnected bool
|
iceConnected bool
|
||||||
paused bool
|
|
||||||
me *webrtc.MediaEngine // optional, populated only when receiving tracks
|
me *webrtc.MediaEngine // optional, populated only when receiving tracks
|
||||||
subscribedTracks map[string][]*webrtc.TrackRemote
|
subscribedTracks map[string][]*webrtc.TrackRemote
|
||||||
localParticipant *livekit.ParticipantInfo
|
localParticipant *livekit.ParticipantInfo
|
||||||
remoteParticipants map[string]*livekit.ParticipantInfo
|
remoteParticipants map[string]*livekit.ParticipantInfo
|
||||||
|
|
||||||
|
// tracks waiting to be acked, cid => trackInfo
|
||||||
|
pendingPublishedTracks map[string]*livekit.TrackInfo
|
||||||
|
|
||||||
// pending actions to start after connected to peer
|
// pending actions to start after connected to peer
|
||||||
pendingCandidates []*webrtc.ICECandidate
|
pendingCandidates []*webrtc.ICECandidate
|
||||||
pendingTrackWriters []*TrackWriter
|
pendingTrackWriters []*TrackWriter
|
||||||
OnConnected func()
|
OnConnected func()
|
||||||
|
|
||||||
// navigate log ring buffer. saving the last N entries
|
|
||||||
writer *ring.Ring
|
|
||||||
reader *ring.Ring
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -58,7 +57,6 @@ var (
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
maxLogs = 256
|
|
||||||
extMimeMapping = map[string]string{
|
extMimeMapping = map[string]string{
|
||||||
".ivf": webrtc.MimeTypeVP8,
|
".ivf": webrtc.MimeTypeVP8,
|
||||||
".h264": webrtc.MimeTypeH264,
|
".h264": webrtc.MimeTypeH264,
|
||||||
@@ -88,18 +86,15 @@ func NewRTCClient(conn *websocket.Conn) (*RTCClient, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
logRing := ring.New(maxLogs)
|
|
||||||
c := &RTCClient{
|
c := &RTCClient{
|
||||||
conn: conn,
|
conn: conn,
|
||||||
lock: sync.Mutex{},
|
pendingCandidates: make([]*webrtc.ICECandidate, 0),
|
||||||
pendingCandidates: make([]*webrtc.ICECandidate, 0),
|
localTracks: make(map[string]webrtc.TrackLocal),
|
||||||
localTracks: make([]webrtc.TrackLocal, 0),
|
pendingPublishedTracks: make(map[string]*livekit.TrackInfo),
|
||||||
subscribedTracks: make(map[string][]*webrtc.TrackRemote),
|
subscribedTracks: make(map[string][]*webrtc.TrackRemote),
|
||||||
remoteParticipants: make(map[string]*livekit.ParticipantInfo),
|
remoteParticipants: make(map[string]*livekit.ParticipantInfo),
|
||||||
reader: logRing,
|
PeerConn: peerConn,
|
||||||
writer: logRing,
|
me: &webrtc.MediaEngine{},
|
||||||
PeerConn: peerConn,
|
|
||||||
me: &webrtc.MediaEngine{},
|
|
||||||
}
|
}
|
||||||
c.ctx, c.cancel = context.WithCancel(context.Background())
|
c.ctx, c.cancel = context.WithCancel(context.Background())
|
||||||
c.me.RegisterDefaultCodecs()
|
c.me.RegisterDefaultCodecs()
|
||||||
@@ -118,12 +113,12 @@ func NewRTCClient(conn *websocket.Conn) (*RTCClient, error) {
|
|||||||
|
|
||||||
// send it through
|
// send it through
|
||||||
if err := c.SendIceCandidate(ic); err != nil {
|
if err := c.SendIceCandidate(ic); err != nil {
|
||||||
c.AppendLog("failed to send ice candidate", "err", err)
|
logger.Debugw("failed to send ice candidate", "err", err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
peerConn.OnTrack(func(track *webrtc.TrackRemote, rtpReceiver *webrtc.RTPReceiver) {
|
peerConn.OnTrack(func(track *webrtc.TrackRemote, rtpReceiver *webrtc.RTPReceiver) {
|
||||||
c.AppendLog("track received", "label", track.StreamID(), "id", track.ID())
|
logger.Debugw("track received", "label", track.StreamID(), "id", track.ID())
|
||||||
go c.processTrack(track)
|
go c.processTrack(track)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -138,14 +133,15 @@ func NewRTCClient(conn *websocket.Conn) (*RTCClient, error) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
peerConn.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
|
peerConn.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
|
||||||
c.AppendLog("ICE state has changed", "state", connectionState.String())
|
logger.Debugw("ICE state has changed", "state", connectionState.String(),
|
||||||
|
"participant", c.localParticipant.Sid)
|
||||||
if connectionState == webrtc.ICEConnectionStateConnected {
|
if connectionState == webrtc.ICEConnectionStateConnected {
|
||||||
// flush peers
|
// flush peers
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
for _, tw := range c.pendingTrackWriters {
|
for _, tw := range c.pendingTrackWriters {
|
||||||
if err := tw.Start(); err != nil {
|
if err := tw.Start(); err != nil {
|
||||||
c.AppendLog("track writer error", "err", err)
|
logger.Debugw("track writer error", "err", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,8 +164,6 @@ func (c *RTCClient) ID() string {
|
|||||||
|
|
||||||
// create an offer for the server
|
// create an offer for the server
|
||||||
func (c *RTCClient) Run() error {
|
func (c *RTCClient) Run() error {
|
||||||
go c.logLoop()
|
|
||||||
|
|
||||||
c.conn.SetCloseHandler(func(code int, text string) error {
|
c.conn.SetCloseHandler(func(code int, text string) error {
|
||||||
// when closed, stop connection
|
// when closed, stop connection
|
||||||
logger.Infow("connection closed", "code", code, "text", text)
|
logger.Infow("connection closed", "code", code, "text", text)
|
||||||
@@ -183,7 +177,7 @@ func (c *RTCClient) Run() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
dc.OnOpen(func() {
|
dc.OnOpen(func() {
|
||||||
c.AppendLog("data channel open")
|
logger.Debugw("data channel open")
|
||||||
})
|
})
|
||||||
|
|
||||||
// run the session
|
// run the session
|
||||||
@@ -202,9 +196,9 @@ func (c *RTCClient) Run() error {
|
|||||||
}
|
}
|
||||||
c.lock.Unlock()
|
c.lock.Unlock()
|
||||||
|
|
||||||
c.AppendLog("join accepted, sending offer..", "participant", msg.Join.Participant.Sid)
|
logger.Debugw("join accepted, sending offer..", "participant", msg.Join.Participant.Sid)
|
||||||
c.localParticipant = msg.Join.Participant
|
c.localParticipant = msg.Join.Participant
|
||||||
c.AppendLog("other participants", "count", len(msg.Join.OtherParticipants))
|
logger.Debugw("other participants", "count", len(msg.Join.OtherParticipants))
|
||||||
|
|
||||||
// Create an offer to send to the other process
|
// Create an offer to send to the other process
|
||||||
offer, err := c.PeerConn.CreateOffer(nil)
|
offer, err := c.PeerConn.CreateOffer(nil)
|
||||||
@@ -212,7 +206,7 @@ func (c *RTCClient) Run() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.AppendLog("created offer", "offer", offer.SDP)
|
logger.Debugw("created offer", "offer", offer.SDP)
|
||||||
|
|
||||||
// Sets the LocalDescription, and starts our UDP listeners
|
// Sets the LocalDescription, and starts our UDP listeners
|
||||||
// Note: this will start the gathering of ICE candidates
|
// Note: this will start the gathering of ICE candidates
|
||||||
@@ -226,7 +220,7 @@ func (c *RTCClient) Run() error {
|
|||||||
Offer: rtc.ToProtoSessionDescription(offer),
|
Offer: rtc.ToProtoSessionDescription(offer),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
c.AppendLog("connecting to remote...")
|
logger.Debugw("connecting to remote...")
|
||||||
if err = c.SendRequest(req); err != nil {
|
if err = c.SendRequest(req); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -235,7 +229,7 @@ func (c *RTCClient) Run() error {
|
|||||||
case *livekit.SignalResponse_Answer:
|
case *livekit.SignalResponse_Answer:
|
||||||
c.handleAnswer(rtc.FromProtoSessionDescription(msg.Answer))
|
c.handleAnswer(rtc.FromProtoSessionDescription(msg.Answer))
|
||||||
case *livekit.SignalResponse_Offer:
|
case *livekit.SignalResponse_Offer:
|
||||||
c.AppendLog("received server offer",
|
logger.Debugw("received server offer",
|
||||||
"type", msg.Offer.Type)
|
"type", msg.Offer.Type)
|
||||||
desc := rtc.FromProtoSessionDescription(msg.Offer)
|
desc := rtc.FromProtoSessionDescription(msg.Offer)
|
||||||
if err := c.handleOffer(desc); err != nil {
|
if err := c.handleOffer(desc); err != nil {
|
||||||
@@ -245,7 +239,6 @@ func (c *RTCClient) Run() error {
|
|||||||
c.negotiate()
|
c.negotiate()
|
||||||
case *livekit.SignalResponse_Trickle:
|
case *livekit.SignalResponse_Trickle:
|
||||||
candidateInit := rtc.FromProtoTrickle(msg.Trickle)
|
candidateInit := rtc.FromProtoTrickle(msg.Trickle)
|
||||||
c.AppendLog("adding remote candidate", "candidate", candidateInit.Candidate)
|
|
||||||
if err := c.PeerConn.AddICECandidate(candidateInit); err != nil {
|
if err := c.PeerConn.AddICECandidate(candidateInit); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -253,9 +246,15 @@ func (c *RTCClient) Run() error {
|
|||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
for _, p := range msg.Update.Participants {
|
for _, p := range msg.Update.Participants {
|
||||||
c.remoteParticipants[p.Sid] = p
|
c.remoteParticipants[p.Sid] = p
|
||||||
c.AppendLog("participant update", "id", p.Sid, "state", p.State.String())
|
logger.Debugw("participant update", "id", p.Sid, "state", p.State.String())
|
||||||
}
|
}
|
||||||
c.lock.Unlock()
|
c.lock.Unlock()
|
||||||
|
case *livekit.SignalResponse_TrackPublished:
|
||||||
|
logger.Debugw("track published", "track", msg.TrackPublished.Track.Name, "participant", c.localParticipant.Sid,
|
||||||
|
"cid", msg.TrackPublished.Cid, "trackSid", msg.TrackPublished.Track.Sid)
|
||||||
|
c.lock.Lock()
|
||||||
|
c.pendingPublishedTracks[msg.TrackPublished.Cid] = msg.TrackPublished.Track
|
||||||
|
c.lock.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -326,28 +325,19 @@ func (c *RTCClient) Stop() {
|
|||||||
c.cancel()
|
c.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RTCClient) PauseLogs() {
|
|
||||||
c.paused = true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *RTCClient) ResumeLogs() {
|
|
||||||
c.paused = false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *RTCClient) SendRequest(msg *livekit.SignalRequest) error {
|
func (c *RTCClient) SendRequest(msg *livekit.SignalRequest) error {
|
||||||
c.lock.Lock()
|
|
||||||
defer c.lock.Unlock()
|
|
||||||
payload, err := protojson.Marshal(msg)
|
payload, err := protojson.Marshal(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.wsLock.Lock()
|
||||||
|
defer c.wsLock.Unlock()
|
||||||
return c.conn.WriteMessage(websocket.TextMessage, payload)
|
return c.conn.WriteMessage(websocket.TextMessage, payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RTCClient) SendIceCandidate(ic *webrtc.ICECandidate) error {
|
func (c *RTCClient) SendIceCandidate(ic *webrtc.ICECandidate) error {
|
||||||
candInit := ic.ToJSON()
|
candInit := ic.ToJSON()
|
||||||
c.AppendLog("sending trickle candidate", "candidate", candInit.Candidate)
|
|
||||||
return c.SendRequest(&livekit.SignalRequest{
|
return c.SendRequest(&livekit.SignalRequest{
|
||||||
Message: &livekit.SignalRequest_Trickle{
|
Message: &livekit.SignalRequest_Trickle{
|
||||||
Trickle: rtc.ToProtoTrickle(candInit),
|
Trickle: rtc.ToProtoTrickle(candInit),
|
||||||
@@ -365,9 +355,30 @@ func (c *RTCClient) AddTrack(track *webrtc.TrackLocalStaticSample, path string)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// wait till track published message is received
|
||||||
|
timeout := time.After(5 * time.Second)
|
||||||
|
var ti *livekit.TrackInfo
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-timeout:
|
||||||
|
return nil, errors.New("could not publish track after timeout")
|
||||||
|
default:
|
||||||
|
c.lock.Lock()
|
||||||
|
ti = c.pendingPublishedTracks[track.ID()]
|
||||||
|
c.lock.Unlock()
|
||||||
|
if ti != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
}
|
||||||
|
if ti != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
c.localTracks = append(c.localTracks, track)
|
c.localTracks[ti.Sid] = track
|
||||||
|
|
||||||
if _, err = c.PeerConn.AddTrack(track); err != nil {
|
if _, err = c.PeerConn.AddTrack(track); err != nil {
|
||||||
return
|
return
|
||||||
@@ -401,7 +412,7 @@ func (c *RTCClient) AddFileTrack(path string, id string, label string) (writer *
|
|||||||
return nil, fmt.Errorf("%s has an unsupported extension", filepath.Base(path))
|
return nil, fmt.Errorf("%s has an unsupported extension", filepath.Base(path))
|
||||||
}
|
}
|
||||||
|
|
||||||
c.AppendLog("adding track",
|
logger.Debugw("adding track",
|
||||||
"mime", mime,
|
"mime", mime,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -447,7 +458,7 @@ func (c *RTCClient) handleOffer(desc webrtc.SessionDescription) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// send remote an answer
|
// send remote an answer
|
||||||
c.AppendLog("sending answer")
|
logger.Debugw("sending answer")
|
||||||
return c.SendRequest(&livekit.SignalRequest{
|
return c.SendRequest(&livekit.SignalRequest{
|
||||||
Message: &livekit.SignalRequest_Answer{
|
Message: &livekit.SignalRequest_Answer{
|
||||||
Answer: rtc.ToProtoSessionDescription(answer),
|
Answer: rtc.ToProtoSessionDescription(answer),
|
||||||
@@ -456,7 +467,7 @@ func (c *RTCClient) handleOffer(desc webrtc.SessionDescription) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *RTCClient) handleAnswer(desc webrtc.SessionDescription) error {
|
func (c *RTCClient) handleAnswer(desc webrtc.SessionDescription) error {
|
||||||
c.AppendLog("handling server answer")
|
logger.Debugw("handling server answer")
|
||||||
// remote answered the offer, establish connection
|
// remote answered the offer, establish connection
|
||||||
err := c.PeerConn.SetRemoteDescription(desc)
|
err := c.PeerConn.SetRemoteDescription(desc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -478,7 +489,7 @@ func (c *RTCClient) handleAnswer(desc webrtc.SessionDescription) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *RTCClient) requestNegotiation() error {
|
func (c *RTCClient) requestNegotiation() error {
|
||||||
c.AppendLog("requesting negotiation")
|
logger.Debugw("requesting negotiation")
|
||||||
return c.SendRequest(&livekit.SignalRequest{
|
return c.SendRequest(&livekit.SignalRequest{
|
||||||
Message: &livekit.SignalRequest_Negotiate{
|
Message: &livekit.SignalRequest_Negotiate{
|
||||||
Negotiate: &livekit.NegotiationRequest{},
|
Negotiate: &livekit.NegotiationRequest{},
|
||||||
@@ -487,7 +498,7 @@ func (c *RTCClient) requestNegotiation() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *RTCClient) negotiate() error {
|
func (c *RTCClient) negotiate() error {
|
||||||
c.AppendLog("starting negotiation")
|
logger.Debugw("starting negotiation")
|
||||||
offer, err := c.PeerConn.CreateOffer(nil)
|
offer, err := c.PeerConn.CreateOffer(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -509,38 +520,6 @@ type logEntry struct {
|
|||||||
args []interface{}
|
args []interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RTCClient) AppendLog(msg string, args ...interface{}) {
|
|
||||||
entry := &logEntry{
|
|
||||||
msg: msg,
|
|
||||||
args: args,
|
|
||||||
}
|
|
||||||
|
|
||||||
// not locking this, log loss ok in multithreaded env
|
|
||||||
writer := c.writer
|
|
||||||
writer.Value = entry
|
|
||||||
c.writer = writer.Next()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *RTCClient) logLoop() {
|
|
||||||
for {
|
|
||||||
for !c.paused && c.reader != c.writer {
|
|
||||||
val, _ := c.reader.Value.(*logEntry)
|
|
||||||
if val != nil {
|
|
||||||
logger.Infow(val.msg, val.args...)
|
|
||||||
}
|
|
||||||
// advance reader until writer
|
|
||||||
c.reader = c.reader.Next()
|
|
||||||
}
|
|
||||||
|
|
||||||
// sleep or abort
|
|
||||||
select {
|
|
||||||
case <-c.ctx.Done():
|
|
||||||
return
|
|
||||||
case <-time.After(100 * time.Millisecond):
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *RTCClient) processTrack(track *webrtc.TrackRemote) {
|
func (c *RTCClient) processTrack(track *webrtc.TrackRemote) {
|
||||||
lastUpdate := time.Time{}
|
lastUpdate := time.Time{}
|
||||||
pId, trackId := rtc.UnpackTrackId(track.ID())
|
pId, trackId := rtc.UnpackTrackId(track.ID())
|
||||||
@@ -564,12 +543,12 @@ func (c *RTCClient) processTrack(track *webrtc.TrackRemote) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AppendLog("error reading RTP", "err", err)
|
logger.Debugw("error reading RTP", "err", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
numBytes += pkt.MarshalSize()
|
numBytes += pkt.MarshalSize()
|
||||||
if time.Now().Sub(lastUpdate) > 30*time.Second {
|
if time.Now().Sub(lastUpdate) > 30*time.Second {
|
||||||
c.AppendLog("consumed from participant",
|
logger.Debugw("consumed from participant",
|
||||||
"track", trackId, "participant", pId,
|
"track", trackId, "participant", pId,
|
||||||
"size", numBytes)
|
"size", numBytes)
|
||||||
lastUpdate = time.Now()
|
lastUpdate = time.Now()
|
||||||
|
|||||||
@@ -1,16 +1,12 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/manifoldco/promptui"
|
|
||||||
"github.com/pion/webrtc/v3"
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/cmd/cli/client"
|
"github.com/livekit/livekit-server/cmd/cli/client"
|
||||||
@@ -102,116 +98,9 @@ func joinRoom(c *cli.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// start loop to detect input
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
r := bufio.NewReader(os.Stdin)
|
|
||||||
_, _, err := r.ReadLine()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// pause client output and wait
|
|
||||||
rc.PauseLogs()
|
|
||||||
|
|
||||||
err = handleCommand(rc)
|
|
||||||
if err != nil {
|
|
||||||
logger.Errorw("could not handle command", "err", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
rc.ResumeLogs()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
return rc.Run()
|
return rc.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleCommand(rc *client.RTCClient) error {
|
|
||||||
cmdPrompt := promptui.Select{
|
|
||||||
Label: "select command",
|
|
||||||
Items: []string{
|
|
||||||
"add video",
|
|
||||||
"add audio",
|
|
||||||
//"add data",
|
|
||||||
//"send data",
|
|
||||||
"remove track",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
idx, _, err := cmdPrompt.Run()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
switch idx {
|
|
||||||
case 0:
|
|
||||||
return handleAddMedia(rc, false)
|
|
||||||
case 1:
|
|
||||||
return handleAddMedia(rc, true)
|
|
||||||
default:
|
|
||||||
return errors.New("unimplemented command")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleAddMedia(rc *client.RTCClient, isAudio bool) error {
|
|
||||||
fileType := "vp8, h264"
|
|
||||||
codecType := webrtc.RTPCodecTypeVideo
|
|
||||||
if isAudio {
|
|
||||||
fileType = "opus"
|
|
||||||
codecType = webrtc.RTPCodecTypeAudio
|
|
||||||
}
|
|
||||||
// get media location
|
|
||||||
p := promptui.Prompt{
|
|
||||||
Label: fmt.Sprintf("media path (%s)", fileType),
|
|
||||||
Validate: func(s string) error {
|
|
||||||
s = ExpandUser(s)
|
|
||||||
// ensure it exists
|
|
||||||
st, err := os.Stat(s)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if st.IsDir() {
|
|
||||||
return errors.New("cannot be a directory")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
mediaPath, err := p.Run()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
mediaPath = ExpandUser(mediaPath)
|
|
||||||
|
|
||||||
// TODO: see what the ID should be
|
|
||||||
_, err = rc.AddFileTrack(mediaPath, codecType.String(), filepath.Base(mediaPath))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
fmt.Printf("added %s track: %s\n", codecType.String(), mediaPath)
|
|
||||||
|
|
||||||
if isAudio {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// for video files, also look for the .ogg at the same path
|
|
||||||
videoExt := filepath.Ext(mediaPath)
|
|
||||||
if len(videoExt) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
audioPath := mediaPath[0:len(mediaPath)-len(videoExt)] + ".ogg"
|
|
||||||
if _, err = os.Stat(audioPath); err == nil {
|
|
||||||
_, err = rc.AddFileTrack(audioPath, codecType.String(), filepath.Base(audioPath))
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("added audio track: %s\n", audioPath)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleSignals(rc *client.RTCClient) {
|
func handleSignals(rc *client.RTCClient) {
|
||||||
// signal to stop client
|
// signal to stop client
|
||||||
sigChan := make(chan os.Signal, 1)
|
sigChan := make(chan os.Signal, 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user