trackwriter, progress with tester client
This commit is contained in:
+128
-2
@@ -1,9 +1,18 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/manifoldco/promptui"
|
||||
"github.com/pion/webrtc/v3"
|
||||
"github.com/urfave/cli/v2"
|
||||
|
||||
"github.com/livekit/livekit-server/cmd/cli/client"
|
||||
@@ -42,7 +51,9 @@ func joinRoom(c *cli.Context) error {
|
||||
v.Set("peer_id", c.String("peer-id"))
|
||||
u.RawQuery = v.Encode()
|
||||
|
||||
logger.GetLogger().Infow("connecting to Websocket signal", "url", u.String())
|
||||
log := logger.GetLogger()
|
||||
|
||||
log.Infow("connecting to Websocket signal", "url", u.String())
|
||||
conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -54,7 +65,122 @@ func joinRoom(c *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: input loop to detect user commands
|
||||
// signal to stop client
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
||||
|
||||
go func() {
|
||||
sig := <-sigChan
|
||||
log.Infow("exit requested, shutting down", "signal", sig)
|
||||
rc.Stop()
|
||||
}()
|
||||
|
||||
// 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 {
|
||||
log.Errorw("could not handle command", "err", err)
|
||||
}
|
||||
|
||||
rc.ResumeLogs()
|
||||
}
|
||||
}()
|
||||
|
||||
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.AddTrack(mediaPath, codecType, filepath.Base(mediaPath), "livekit")
|
||||
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.AddTrack(audioPath, webrtc.RTPCodecTypeAudio, filepath.Base(audioPath), "livekit")
|
||||
if err != nil {
|
||||
fmt.Printf("added audio track: %s\n", audioPath)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@ package commands
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
@@ -26,3 +29,12 @@ func PrintJSON(obj interface{}) {
|
||||
txt, _ := json.Marshal(obj)
|
||||
fmt.Println(string(txt))
|
||||
}
|
||||
|
||||
func ExpandUser(p string) string {
|
||||
if strings.HasPrefix(p, "~") {
|
||||
home, _ := os.UserHomeDir()
|
||||
return filepath.Join(home, p[1:])
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user