lksdk

package module
v0.5.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2021 License: Apache-2.0 Imports: 25 Imported by: 22

README

LiveKit Go SDK

This is the official Golang SDK to LiveKit. You would integrate this on your app's backend in order to

  • Create access tokens
  • Access LiveKit server-side APIs, giving you moderation capabilities
  • Client SDK to interact as participant, publish & record room streams

Token creation

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 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/server-sdk-go/proto"
)

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{})

    // 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.

import (
  lksdk "github.com/livekit/server-sdk-go"
)

func main() {
  host := "<host>"
  apiKey := "api-key"
  apiSecret := "api-secret"
  roomName := "myroom"
  identity := "botuser"
	room, err := lksdk.ConnectToRoom(host, lksdk.ConnectInfo{
		APIKey:              apiKey,
		APISecret:           apiSecret,
		RoomName:            roomName,
		ParticipantIdentity: identity,
	})
	if err != nil {
		panic(err)
	}

  room.Callback.OnTrackSubscribed = trackSubscribed
  ...
  room.Disconnect()
}

func trackSubscribed(track *webrtc.TrackRemote, publication lksdk.TrackPublication, rp *lksdk.RemoteParticipant) {

}

Documentation

Index

Constants

View Source
const PROTOCOL = 2

Variables

View Source
var (
	ErrConnectionTimeout   = errors.New("could not connect after timeout")
	ErrTrackPublishTimeout = errors.New("timed out publishing track")
)

Functions

func FromProtoIceServers

func FromProtoIceServers(iceservers []*livekit.ICEServer) []webrtc.ICEServer

func FromProtoSessionDescription

func FromProtoSessionDescription(sd *livekit.SessionDescription) webrtc.SessionDescription

func FromProtoTrickle

func FromProtoTrickle(trickle *livekit.TrickleRequest) webrtc.ICECandidateInit

func SetLogger

func SetLogger(l logr.Logger)

SetLogger overrides logger with a logr implementation https://github.com/go-logr/logr

func ToProtoSessionDescription

func ToProtoSessionDescription(sd webrtc.SessionDescription) *livekit.SessionDescription

func ToProtoTrickle

func ToProtoTrickle(candidateInit webrtc.ICECandidateInit, target livekit.SignalTarget) *livekit.TrickleRequest

Types

type ConnectInfo

type ConnectInfo struct {
	APIKey              string
	APISecret           string
	RoomName            string
	ParticipantIdentity string
	ParticipantMetadata string
}

type LoadTestProvider

type LoadTestProvider struct {
	BytesPerSample uint32
	SampleDuration time.Duration
}

func NewLoadTestProvider

func NewLoadTestProvider(bitrate uint32) (*LoadTestProvider, error)

func (*LoadTestProvider) NextSample

func (p *LoadTestProvider) NextSample() (media.Sample, error)

type LocalParticipant

type LocalParticipant struct {
	// contains filtered or unexported fields
}

func (*LocalParticipant) AudioLevel

func (p *LocalParticipant) AudioLevel() float32

func (*LocalParticipant) Identity

func (p *LocalParticipant) Identity() string

func (*LocalParticipant) IsSpeaking

func (p *LocalParticipant) IsSpeaking() bool

func (*LocalParticipant) Metadata added in v0.5.13

func (p *LocalParticipant) Metadata() string

func (*LocalParticipant) PublishData added in v0.5.12

func (p *LocalParticipant) PublishData(data []byte, kind livekit.DataPacket_Kind, destinationSids []string) error

func (*LocalParticipant) PublishTrack

func (p *LocalParticipant) PublishTrack(track webrtc.TrackLocal, name string) (*LocalTrackPublication, error)

func (*LocalParticipant) SID

func (p *LocalParticipant) SID() string

func (*LocalParticipant) Tracks

func (p *LocalParticipant) Tracks() []TrackPublication

type LocalSampleTrack

type LocalSampleTrack struct {
	webrtc.TrackLocalStaticSample
	// contains filtered or unexported fields
}

LocalSampleTrack is a local track that simplifies writing samples. It handles timing and publishing of things, so as long as a SampleProvider is provided, the class takes care of publishing tracks at the right frequency

func NewLocalSampleTrack

func NewLocalSampleTrack(c webrtc.RTPCodecCapability, sampleProvider SampleProvider) (*LocalSampleTrack, error)

func (*LocalSampleTrack) Bind

func (s *LocalSampleTrack) Bind(t webrtc.TrackLocalContext) (webrtc.RTPCodecParameters, error)

func (*LocalSampleTrack) Unbind

func (s *LocalSampleTrack) Unbind(t webrtc.TrackLocalContext) error

type LocalTrackPublication

type LocalTrackPublication struct {
	// contains filtered or unexported fields
}

func (*LocalTrackPublication) IsMuted

func (p *LocalTrackPublication) IsMuted() bool

func (*LocalTrackPublication) IsSubscribed

func (p *LocalTrackPublication) IsSubscribed() bool

func (*LocalTrackPublication) Kind

func (p *LocalTrackPublication) Kind() TrackKind

func (*LocalTrackPublication) Name

func (p *LocalTrackPublication) Name() string

func (*LocalTrackPublication) SID

func (p *LocalTrackPublication) SID() string

func (*LocalTrackPublication) SetMuted

func (p *LocalTrackPublication) SetMuted(muted bool)

func (*LocalTrackPublication) Track

func (p *LocalTrackPublication) Track() Track

func (*LocalTrackPublication) TrackLocal

func (p *LocalTrackPublication) TrackLocal() webrtc.TrackLocal

type NullSampleProvider

type NullSampleProvider struct {
	BytesPerSample uint32
	SampleDuration time.Duration
}

NullSampleProvider is a media provider that provides null packets, it could meet a certain bitrate, if desired

func NewNullSampleProvider

func NewNullSampleProvider(bitrate uint32) *NullSampleProvider

func (*NullSampleProvider) NextSample

func (p *NullSampleProvider) NextSample() (media.Sample, error)

type PCTransport

type PCTransport struct {
	// contains filtered or unexported fields
}

PCTransport is a wrapper around PeerConnection, with some helper methods

func NewPCTransport

func NewPCTransport(iceServers []webrtc.ICEServer) (*PCTransport, error)

func (*PCTransport) AddICECandidate

func (t *PCTransport) AddICECandidate(candidate webrtc.ICECandidateInit) error

func (*PCTransport) Close

func (t *PCTransport) Close() error

func (*PCTransport) IsConnected

func (t *PCTransport) IsConnected() bool

func (*PCTransport) OnNegotiationNeeded

func (t *PCTransport) OnNegotiationNeeded(f func())

func (*PCTransport) PeerConnection

func (t *PCTransport) PeerConnection() *webrtc.PeerConnection

func (*PCTransport) SetRemoteDescription

func (t *PCTransport) SetRemoteDescription(sd webrtc.SessionDescription) error

type Participant

type Participant interface {
	SID() string
	Identity() string
	IsSpeaking() bool
	AudioLevel() float32

	Tracks() []TrackPublication
	Metadata() string
	// contains filtered or unexported methods
}

type ParticipantCallback

type ParticipantCallback struct {
	// for all participants
	OnTrackMuted        func(pub TrackPublication, p Participant)
	OnTrackUnmuted      func(pub TrackPublication, p Participant)
	OnMetadataChanged   func(oldMetadata string, p Participant)
	OnIsSpeakingChanged func(p Participant)

	// for remote participants
	OnTrackSubscribed         func(track *webrtc.TrackRemote, publication TrackPublication, rp *RemoteParticipant)
	OnTrackUnsubscribed       func(track *webrtc.TrackRemote, publication TrackPublication, rp *RemoteParticipant)
	OnTrackSubscriptionFailed func(sid string, rp *RemoteParticipant)
	OnTrackPublished          func(publication TrackPublication, rp *RemoteParticipant)
	OnTrackUnpublished        func(publication TrackPublication, rp *RemoteParticipant)
	OnDataReceived            func(data []byte, rp *RemoteParticipant)
}

func NewParticipantCallback

func NewParticipantCallback() *ParticipantCallback

type PubCallback

type PubCallback func(pub TrackPublication, participant *RemoteParticipant)

type RTCEngine

type RTCEngine struct {
	JoinTimeout time.Duration

	// callbacks
	OnDisconnected          func()
	OnMediaTrack            func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver)
	OnParticipantUpdate     func([]*livekit.ParticipantInfo)
	OnActiveSpeakersChanged func([]*livekit.SpeakerInfo)
	OnDataReceived          func(userPacket *livekit.UserPacket)
	// contains filtered or unexported fields
}

func NewRTCEngine

func NewRTCEngine() *RTCEngine

func (*RTCEngine) Close

func (e *RTCEngine) Close()

func (*RTCEngine) IsConnected

func (e *RTCEngine) IsConnected() bool

func (*RTCEngine) Join

func (e *RTCEngine) Join(url string, token string) (*livekit.JoinResponse, error)

func (*RTCEngine) TrackPublishedChan

func (e *RTCEngine) TrackPublishedChan() <-chan *livekit.TrackPublishedResponse

type RemoteParticipant

type RemoteParticipant struct {
	// contains filtered or unexported fields
}

func (*RemoteParticipant) AudioLevel

func (p *RemoteParticipant) AudioLevel() float32

func (*RemoteParticipant) Identity

func (p *RemoteParticipant) Identity() string

func (*RemoteParticipant) IsSpeaking

func (p *RemoteParticipant) IsSpeaking() bool

func (*RemoteParticipant) Metadata added in v0.5.13

func (p *RemoteParticipant) Metadata() string

func (*RemoteParticipant) SID

func (p *RemoteParticipant) SID() string

func (*RemoteParticipant) Tracks

func (p *RemoteParticipant) Tracks() []TrackPublication

type RemoteTrackPublication

type RemoteTrackPublication struct {
	// contains filtered or unexported fields
}

func (*RemoteTrackPublication) IsMuted

func (p *RemoteTrackPublication) IsMuted() bool

func (*RemoteTrackPublication) IsSubscribed

func (p *RemoteTrackPublication) IsSubscribed() bool

func (*RemoteTrackPublication) Kind

func (p *RemoteTrackPublication) Kind() TrackKind

func (*RemoteTrackPublication) Name

func (p *RemoteTrackPublication) Name() string

func (*RemoteTrackPublication) Receiver

func (p *RemoteTrackPublication) Receiver() *webrtc.RTPReceiver

func (*RemoteTrackPublication) SID

func (p *RemoteTrackPublication) SID() string

func (*RemoteTrackPublication) Track

func (p *RemoteTrackPublication) Track() Track

func (*RemoteTrackPublication) TrackRemote

func (p *RemoteTrackPublication) TrackRemote() *webrtc.TrackRemote

type Room

type Room struct {
	SID              string
	Name             string
	LocalParticipant *LocalParticipant
	Participants     map[string]*RemoteParticipant
	ActiveSpeakers   []Participant
	Callback         *RoomCallback
	// contains filtered or unexported fields
}

func ConnectToRoom

func ConnectToRoom(url string, info ConnectInfo) (*Room, error)

func ConnectToRoomWithToken

func ConnectToRoomWithToken(url, token string) (*Room, error)

func (*Room) Disconnect

func (r *Room) Disconnect()

func (*Room) GetParticipant

func (r *Room) GetParticipant(sid string) *RemoteParticipant

func (*Room) GetParticipants

func (r *Room) GetParticipants() []*RemoteParticipant

type RoomCallback

type RoomCallback struct {
	OnDisconnected            func()
	OnParticipantConnected    func(*RemoteParticipant)
	OnParticipantDisconnected func(*RemoteParticipant)
	OnActiveSpeakersChanged   func([]Participant)

	ParticipantCallback
}

func NewRoomCallback

func NewRoomCallback() *RoomCallback

type RoomServiceClient

type RoomServiceClient struct {
	livekit.RoomService
	// contains filtered or unexported fields
}

func NewRoomServiceClient

func NewRoomServiceClient(url string, apiKey string, secretKey string) *RoomServiceClient

func (*RoomServiceClient) CreateRoom

func (*RoomServiceClient) CreateToken

func (c *RoomServiceClient) CreateToken() *auth.AccessToken

func (*RoomServiceClient) DeleteRoom

func (*RoomServiceClient) GetParticipant

func (*RoomServiceClient) ListParticipants

func (*RoomServiceClient) ListRooms

func (*RoomServiceClient) MutePublishedTrack

func (*RoomServiceClient) RemoveParticipant

func (*RoomServiceClient) UpdateParticipant

type SampleProvider

type SampleProvider interface {
	NextSample() (media.Sample, error)
}

type SignalClient

type SignalClient struct {
	OnClose                 func()
	OnAnswer                func(sd webrtc.SessionDescription)
	OnOffer                 func(sd webrtc.SessionDescription)
	OnTrickle               func(init webrtc.ICECandidateInit, target livekit.SignalTarget)
	OnParticipantUpdate     func([]*livekit.ParticipantInfo)
	OnLocalTrackPublished   func(response *livekit.TrackPublishedResponse)
	OnActiveSpeakersChanged func([]*livekit.SpeakerInfo)
	OnLeave                 func()
	// contains filtered or unexported fields
}

func NewSignalClient

func NewSignalClient() *SignalClient

func (*SignalClient) Close

func (c *SignalClient) Close()

func (*SignalClient) IsConnected

func (c *SignalClient) IsConnected() bool

func (*SignalClient) Join

func (c *SignalClient) Join(urlPrefix string, token string) (*livekit.JoinResponse, error)

func (*SignalClient) ReadResponse

func (c *SignalClient) ReadResponse() (*livekit.SignalResponse, error)

func (*SignalClient) SendAnswer

func (c *SignalClient) SendAnswer(sd webrtc.SessionDescription) error

func (*SignalClient) SendICECandidate

func (c *SignalClient) SendICECandidate(candidate webrtc.ICECandidateInit, target livekit.SignalTarget) error

func (*SignalClient) SendLeave

func (c *SignalClient) SendLeave() error

func (*SignalClient) SendMuteTrack

func (c *SignalClient) SendMuteTrack(sid string, muted bool) error

func (*SignalClient) SendOffer

func (c *SignalClient) SendOffer(sd webrtc.SessionDescription) error

func (*SignalClient) SendRequest

func (c *SignalClient) SendRequest(req *livekit.SignalRequest) error

type Track

type Track interface {
	ID() string
}

type TrackKind

type TrackKind string
const (
	TrackKindVideo TrackKind = "video"
	TrackKindAudio TrackKind = "audio"
)

func KindFromRTPType

func KindFromRTPType(rt webrtc.RTPCodecType) TrackKind

func (TrackKind) ProtoType

func (k TrackKind) ProtoType() livekit.TrackType

func (TrackKind) RTPType

func (k TrackKind) RTPType() webrtc.RTPCodecType

func (TrackKind) String

func (k TrackKind) String() string

type TrackPubCallback

type TrackPubCallback func(track Track, pub TrackPublication, participant *RemoteParticipant)

type TrackPublication

type TrackPublication interface {
	Name() string
	SID() string
	Kind() TrackKind
	IsMuted() bool
	IsSubscribed() bool
	Track() Track
	// contains filtered or unexported methods
}

type TrackPublicationOptions

type TrackPublicationOptions struct {
	Name string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL