# livekit-server-sdk-go **Repository Path**: scpro/livekit-server-sdk-go ## Basic Information - **Project Name**: livekit-server-sdk-go - **Description**: https://github.com/livekit/server-sdk-go.git - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: batchiotest - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-09-04 - **Last Updated**: 2023-09-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README The LiveKit icon, the name of the repository and some sample code in the background. # LiveKit Go SDK Use this SDK to manage LiveKit rooms and create access tokens from your Go backend. ## Installation ```shell go get github.com/livekit/server-sdk-go ``` Note: since v1.0 release, this package requires Go 1.18+ in order to build. ## Token creation ```go import ( "time" lksdk "github.com/livekit/server-sdk-go" "github.com/livekit/protocol/auth" ) func getJoinToken(apiKey, apiSecret, room, identity string) (string, error) { at := auth.NewAccessToken(apiKey, apiSecret) grant := &auth.VideoGrant{ RoomJoin: true, Room: room, } at.AddGrant(grant). SetIdentity(identity). SetValidFor(time.Hour) return at.ToJWT() } ``` ## RoomService API RoomService使您能够完全控制房间和房间内的参与者。它包括选择性跟踪订阅以及审核功能。 ```go import ( lksdk "github.com/livekit/server-sdk-go" livekit "github.com/livekit/protocol/livekit" ) func main() { host := "host" apiKey := "key" apiSecret := "secret" roomName := "myroom" identity := "participantIdentity" roomClient := lksdk.NewRoomServiceClient(host, apiKey, apiSecret) // create a new room room, _ := roomClient.CreateRoom(context.Background(), &livekit.CreateRoomRequest{ Name: roomName, }) // list rooms res, _ := roomClient.ListRooms(context.Background(), &livekit.ListRoomsRequest{}) // 终止房间并让参与者离开 roomClient.DeleteRoom(context.Background(), &livekit.DeleteRoomRequest{ Room: roomId, }) // 列出房间中的参与者 res, _ := roomClient.ListParticipants(context.Background(), &livekit.ListParticipantsRequest{ Room: roomName, }) // 断开参与者与会议室的连接 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, }) } ``` ## 作为参与者进行互动 Participant SDK为您提供了作为客户端的访问程序访问权限,使您能够向会议室发布和录制音频/视频/数据。 ```go import ( lksdk "github.com/livekit/server-sdk-go" ) func main() { host := "" apiKey := "api-key" apiSecret := "api-secret" roomName := "myroom" identity := "botuser" roomCB := &lksdk.RoomCallback{ ParticipantCallback: lksdk.ParticipantCallback{ OnTrackSubscribed: trackSubscribed }, } room, err := lksdk.ConnectToRoom(host, lksdk.ConnectInfo{ APIKey: apiKey, APISecret: apiSecret, RoomName: roomName, ParticipantIdentity: identity, }, roomCB) if err != nil { panic(err) } ... room.Disconnect() } func trackSubscribed(track *webrtc.TrackRemote, publication *lksdk.RemoteTrackPublication, rp *lksdk.RemoteParticipant) { } ``` ## Publishing tracks to Room With the Go SDK, you can publish existing files encoded in H.264, VP8, and Opus to the room. First, you will need to encode media into the right format. ### VP8 / Opus ```bash ffmpeg -i \ -c:v libvpx -keyint_min 120 -qmax 50 -maxrate 2M -b:v 1M \ -c:a libopus -page_duration 20000 -vn ``` The above encodes VP8 at average 1Mbps / max 2Mbps with a minimum keyframe interval of 120. ### H.264 / Opus ```bash ffmpeg -i \ -c:v libx264 -bsf:v h264_mp4toannexb -b:v 2M -profile baseline -pix_fmt yuv420p \ -x264-params keyint=120 -max_delay 0 -bf 0 \ -c:a libopus -page_duration 20000 -vn ``` The above encodes H264 with CBS of 2Mbps with a minimum keyframe interval of 120. ### Publish from file ```go file := "video.ivf" videoWidth := 1920 videoHeight := 1080 track, err := lksdk.NewLocalFileTrack(file, // control FPS to ensure synchronization 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{ Name: file, Width: videoWidth, Height: videoHeight, }); err != nil { return err } ``` ### Publish from io.ReadCloser implementation ```go // - `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](https://github.com/livekit/livekit-cli/blob/main/cmd/livekit-cli/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. ## Receiving webhooks The Go SDK helps you to verify and decode webhook callbacks to ensure their authenticity. See [webhooks guide](https://docs.livekit.io/guides/webhooks) for configuration. ```go import ( "github.com/livekit/protocol/auth" "github.com/livekit/protocol/livekit" "github.com/livekit/protocol/webhook" ) var authProvider = auth.NewSimpleKeyProvider( apiKey, apiSecret, ) func ServeHTTP(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 error return } // consume WebhookEvent } ```
LiveKit Ecosystem
Client SDKsComponents · JavaScript · iOS/macOS · Android · Flutter · React Native · Rust · Python · Unity (web) · Unity (beta)
Server SDKsNode.js · Golang · Ruby · Java/Kotlin · PHP (community) · Python (community)
ServicesLivekit server · Egress · Ingress
ResourcesDocs · Example apps · Cloud · Self-hosting · CLI