RoomService gives you complete control over rooms and participants within them. It includes selective track subscriptions as well as moderation capabilities.
import (
lksdk "github.com/livekit/server-sdk-go"
livekit "github.com/livekit/protocol/livekit"
)
funcmain() {
hostURL := "host-url"// ex: https://project-123456.livekit.cloud
apiKey := "api-key"
apiSecret := "api-secret"
roomName := "myroom"
identity := "participantIdentity"
roomClient := lksdk.NewRoomServiceClient(hostURL, apiKey, apiSecret)
// create a new room
room, _ := roomClient.CreateRoom(context.Background(), &livekit.CreateRoomRequest{
Name: roomName,
})
// list rooms
res, _ := roomClient.ListRooms(context.Background(), &livekit.ListRoomsRequest{})
// terminate a room and cause participants to leave
roomClient.DeleteRoom(context.Background(), &livekit.DeleteRoomRequest{
Room: roomId,
})
// list participants in a room
res, _ := roomClient.ListParticipants(context.Background(), &livekit.ListParticipantsRequest{
Room: roomName,
})
// disconnect a participant from room
roomClient.RemoveParticipant(context.Background(), &livekit.RoomParticipantIdentity{
Room: roomName,
Identity: identity,
})
// mute/unmute participant's tracks
roomClient.MutePublishedTrack(context.Background(), &livekit.MuteRoomTrackRequest{
Room: roomName,
Identity: identity,
TrackSid: "track_sid",
Muted: true,
})
}
Interacting as a participant
The Participant SDK gives you access programmatic access as a client enabling you to publish and record audio/video/data to the room.
// - `in` implements io.ReadCloser, such as buffer or file// - `mime` has to be one of webrtc.MimeType...
track, err := lksdk.NewLocalReaderTrack(in, mime,
lksdk.ReaderTrackWithFrameDuration(33 * time.Millisecond),
lksdk.ReaderTrackWithOnWriteComplete(func() { fmt.Println("track finished") }),
)
if err != nil {
return err
}
if _, err = room.LocalParticipant.PublishTrack(track, &lksdk.TrackPublicationOptions{}); err != nil {
return err
}
For a full working example, refer to join.go in livekit-cli.
Publish from other sources
In order to publish from non-file sources, you will have to implement your own SampleProvider, that could provide frames of data with a NextSample method.
The SDK takes care of sending the samples to the room.
Using a pacer
With video publishing, keyframes can be an order of magnitude larger than delta frames.
This size difference can cause a significant increase in bitrate when a keyframe is transmitted, leading to a surge in packet flow.
Such spikes might result in packet loss at the forwarding layer. To maintain a consistent packet flow,
you can enable the use of a pacer.
import"github.com/livekit/mediatransportutil/pkg/pacer"// Control total output bitrate to 10Mbps with 1s max latency
pf := pacer.NewPacerFactory(
pacer.LeakyBucketPacer,
pacer.WithBitrate(10000000),
pacer.WithMaxLatency(time.Second),
)
room, err := lksdk.ConnectToRoom(hostURL, lksdk.ConnectInfo{
APIKey: apiKey,
APISecret: apiSecret,
RoomName: roomName,
ParticipantIdentity: identity,
}, &lksdk.RoomCallback{
ParticipantCallback: lksdk.ParticipantCallback{
OnTrackSubscribed: onTrackSubscribed,
},
}, lksdk.WithPacer(pf))
Receiving webhooks
The Go SDK helps you to verify and decode webhook callbacks to ensure their authenticity.
See webhooks guide for configuration.
import (
"github.com/livekit/protocol/auth""github.com/livekit/protocol/livekit""github.com/livekit/protocol/webhook"
)
var authProvider = auth.NewSimpleKeyProvider(
apiKey, apiSecret,
)
funcServeHTTP(w http.ResponseWriter, r *http.Request) {
// event is a livekit.WebhookEvent{} object
event, err := webhook.ReceiveWebhookEvent(r, authProvider)
if err != nil {
// could not validate, handle errorreturn
}
// consume WebhookEvent
}
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.