Hey guys, We're using OSS livekit for a new featur...
# server-api
s
Hey guys, We're using OSS livekit for a new feature in our company. In this. we have to relay a audio and video track of the host from one room to another. The experience is a bit laggy. To improve it I added the following 2 things using the https://github.com/livekit/server-sdk-go repo: 1. added a pacer: https://github.com/livekit/server-sdk-go?tab=readme-ov-file#using-a-pacer 2. I'm requesting I-frames using PLI every 3 seconds: https://github.com/livekit/server-sdk-go/blob/main/examples/filesaver/main.go#L105 This helped a lot but still there is some improvement that can be done. Can you please suggest other best practices that we can use or things we can check or monitor? e.g
WithRetransmitBufferSize
to retransmit buffer size to reponse to nack request Thanks
This is how the code looks currently:
Copy code
func (a *Activities) relayTrack(ctx context.Context, track *webrtc.TrackRemote, kind string, destRoom *lksdk.Room, rp *lksdk.RemoteParticipant) error {
	localTrack, err := lksdk.NewLocalTrack(track.Codec().RTPCodecCapability)
	if err != nil {
		return err
	}

	// Publish the track from source room to destination room
	_, err = destRoom.LocalParticipant.PublishTrack(localTrack, &lksdk.TrackPublicationOptions{})
	if err != nil {
		return err
	}

	go a.startRelay(ctx, track, localTrack)
	if kind == constants.VIDEO {
		go a.requestKeyFrames(track, rp.WritePLI)
	}
	return nil
}

// start writing RTP packets
func (a *Activities) startRelay(ctx context.Context, track *webrtc.TrackRemote, localTrack *lksdk.LocalTrack) {
	for {
		pkt, _, err := track.ReadRTP()
		if err != nil {
			a.logger.Error(ctx, "failed to read RTP packet", map[string]interface{}{"error": err.Error()})
			break
		}
		localTrack.WriteRTP(pkt, nil)
	}
}

// Send PLI packets to request keyframes of remote participant. This helps to avoid video freeze
func (a *Activities) requestKeyFrames(track *webrtc.TrackRemote, pliWriter lksdk.PLIWriter) {
	ticker := time.NewTicker(constants.KEYFRAME_REFRESH_TICKER)
	defer ticker.Stop()
	for range ticker.C {
		pliWriter(track.SSRC())
	}
}
1. We are first publishing
localTrack
to
destRoom
(destination room) 2. Reading RTP packets from
track
(source track in source room) and writing them to
localTrack
3. We are requesting I-frames every 3 sec.
This is how we connect to the room using Pacer:
Copy code
func (a *Activities) connectBotToRoom(token string) (*lksdk.Room, error) {
	// Control total output bitrate to 10Mbps with 1s max latency
	pf := pacer.NewPacerFactory(
		pacer.LeakyBucketPacer,
		pacer.WithBitrate(constants.PACER_BITRATE),
		pacer.WithMaxLatency(constants.PACER_MAX_LATENCY),
	)
	// Create custom websocket dialer that skips certificate verification
	websocket.DefaultDialer.TLSClientConfig = &tls.Config{
		InsecureSkipVerify: true,
	}
	return lksdk.ConnectToRoomWithToken(a.config.LivekitHost, token, nil, lksdk.WithPacer(pf), lksdk.WithAutoSubscribe(false))
}
e
how is the latency and packet loss rate between your servers?
Another problem of this ways is the forwarded track in the dest room will lose simulcast layers.
s
Another problem of this ways is the forwarded track in the dest room will lose simulcast layers.
We haven't enabled simulcast so we can ignore this.
how is the latency and packet loss rate between your servers?
We are still working on enabling this as OSS version doesn't have this out of the box.
@early-gigabyte-44411 let me know if any more questions. Also, please possible ideas to make this even better.
e
You don’t need the pacer and additional pli in the forwarding go sdk, just let all rtp packets pass through and forwards the pli request from destination room to the source room if there is no bandwidth bottle neck. This will minimize the forwarding latency
s
without the PLI the video was freezing. So I added it.
Also, do you think using samplebuilder is better than my approach? samplebuilder example: https://github.com/livekit/server-sdk-go/blob/main/examples/filesaver/main.go#L103-L106 my approach:
Copy code
func (a *Activities) startRelay(ctx context.Context, track *webrtc.TrackRemote, localTrack *lksdk.LocalTrack) {
	for {
		pkt, _, err := track.ReadRTP()
		if err != nil {
			a.logger.Error(ctx, "failed to read RTP packet", map[string]interface{}{"error": err.Error()})
			break
		}
		localTrack.WriteRTP(pkt, nil)
	}
}
e
No, samplebuilder is not required here
✅ 1