diff --git a/cmd/lk/join.go b/cmd/lk/join.go index cfc761b9..921698d3 100644 --- a/cmd/lk/join.go +++ b/cmd/lk/join.go @@ -30,10 +30,11 @@ import ( "github.com/pion/webrtc/v3" "github.com/urfave/cli/v3" - provider2 "github.com/livekit/livekit-cli/pkg/provider" "github.com/livekit/protocol/livekit" "github.com/livekit/protocol/logger" lksdk "github.com/livekit/server-sdk-go/v2" + + provider2 "github.com/livekit/livekit-cli/pkg/provider" ) var ( diff --git a/cmd/lk/room.go b/cmd/lk/room.go index 99c476d2..9d1e82c2 100644 --- a/cmd/lk/room.go +++ b/cmd/lk/room.go @@ -152,6 +152,14 @@ var ( "Can be used multiple times to publish multiple files. " + "Can publish from Unix or TCP socket using the format '://' or '://' respectively. Valid codecs are \"h264\", \"vp8\", \"opus\"", }, + &cli.StringFlag{ + Name: "publish-data", + Usage: "Publish user data to the room.", + }, + &cli.StringFlag{ + Name: "publish-dtmf", + Usage: "Publish DTMF digits to the room. Character 'w' adds 0.5 sec delay.", + }, &cli.FloatFlag{ Name: "fps", Usage: "If video files are published, indicates `FPS` of video", @@ -737,9 +745,16 @@ func joinRoom(ctx context.Context, cmd *cli.Command) error { done := make(chan os.Signal, 1) roomCB := &lksdk.RoomCallback{ ParticipantCallback: lksdk.ParticipantCallback{ - OnDataReceived: func(data []byte, params lksdk.DataReceiveParams) { + OnDataPacket: func(p lksdk.DataPacket, params lksdk.DataReceiveParams) { identity := params.SenderIdentity - logger.Infow("received data", "data", data, "participant", identity) + switch p := p.(type) { + case *lksdk.UserDataPacket: + logger.Infow("received data", "data", p.Payload, "participant", identity) + case *livekit.SipDTMF: + logger.Infow("received dtmf", "digits", p.Digit, "participant", identity) + default: + logger.Infow("received unsupported data", "data", p, "participant", identity) + } }, OnConnectionQualityChanged: func(update *livekit.ConnectionQualityInfo, p lksdk.Participant) { logger.Debugw("connection quality changed", "participant", p.Identity(), "quality", update.Quality) @@ -820,11 +835,12 @@ func joinRoom(ctx context.Context, cmd *cli.Command) error { } } - if cmd.StringSlice("publish") != nil { + exitAfterPublish := cmd.Bool("exit-after-publish") + if publish := cmd.StringSlice("publish"); publish != nil { fps := cmd.Float("fps") - for _, pub := range cmd.StringSlice("publish") { + for _, pub := range publish { onPublishComplete := func(pub *lksdk.LocalTrackPublication) { - if cmd.Bool("exit-after-publish") { + if exitAfterPublish { close(done) return } @@ -839,6 +855,26 @@ func joinRoom(ctx context.Context, cmd *cli.Command) error { } } + publishPacket := func(p lksdk.DataPacket) error { + if err = room.LocalParticipant.PublishDataPacket(p, lksdk.WithDataPublishReliable(true)); err != nil { + return err + } + if exitAfterPublish { + close(done) + } + return nil + } + if data := cmd.String("publish-data"); data != "" { + if err = publishPacket(&lksdk.UserDataPacket{Payload: []byte(data)}); err != nil { + return err + } + } + if dtmf := cmd.String("publish-dtmf"); dtmf != "" { + if err = publishPacket(&livekit.SipDTMF{Digit: dtmf}); err != nil { + return err + } + } + <-done return nil }