MCPcopy
hub / github.com/livekit/livekit / AddTrack

Method AddTrack

test/client/client.go:888–960  ·  view source on GitHub ↗
(track *webrtc.TrackLocalStaticSample, path string, opts ...AddTrackOption)

Source from the content-addressed store, hash-verified

886}
887
888func (c *RTCClient) AddTrack(track *webrtc.TrackLocalStaticSample, path string, opts ...AddTrackOption) (writer TrackWriter, err error) {
889 select {
890 case <-c.transportReady:
891 case <-c.ctx.Done():
892 return nil, c.ctx.Err()
893 }
894 var params AddTrackParams
895 for _, opt := range opts {
896 opt(&params)
897 }
898 trackType := livekit.TrackType_AUDIO
899 if track.Kind() == webrtc.RTPCodecTypeVideo {
900 trackType = livekit.TrackType_VIDEO
901 }
902
903 sender, _, err := c.publisher.AddTrack(track, types.AddTrackParams{}, nil, rtc.RTCPFeedbackConfig{})
904 if err != nil {
905 logger.Errorw(
906 "add track failed", err,
907 "participant", c.localParticipant.Identity,
908 "participantID", c.localParticipant.Sid,
909 "trackID", track.ID(),
910 )
911 return
912 }
913
914 if err = c.SendAddTrack(track.ID(), track.Codec().MimeType, track.StreamID(), trackType); err != nil {
915 return
916 }
917
918 // wait till track published message is received
919 timeout := time.After(5 * time.Second)
920 var ti *livekit.TrackInfo
921 for {
922 select {
923 case <-timeout:
924 return nil, errors.New("could not publish track after timeout")
925 default:
926 c.lock.Lock()
927 ti = c.pendingPublishedTracks[track.ID()]
928 if ti != nil {
929 delete(c.pendingPublishedTracks, track.ID())
930 c.lock.Unlock()
931 break
932 }
933 c.lock.Unlock()
934 time.Sleep(50 * time.Millisecond)
935 }
936 if ti != nil {
937 break
938 }
939 }
940
941 c.lock.Lock()
942 defer c.lock.Unlock()
943
944 c.localTracks[ti.Sid] = track
945 c.trackSenders[ti.Sid] = sender

Callers 2

AddFileTrackMethod · 0.95

Calls 11

SendAddTrackMethod · 0.95
NewTrackWriterFunction · 0.85
DoneMethod · 0.80
KindMethod · 0.65
AddTrackMethod · 0.65
IDMethod · 0.65
CodecMethod · 0.65
StreamIDMethod · 0.65
NegotiateMethod · 0.65
StartMethod · 0.65

Tested by

no test coverage detected