Support for video dimensions in publications (#36)

* send back video dimensions

* Store video dimensions in MediaTrack and forward in TrackInfo

* use committed protocol
This commit is contained in:
David Zhao
2021-06-18 21:05:56 -07:00
committed by GitHub
parent 515b1f0f7e
commit 38c81e5645
14 changed files with 157 additions and 49 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ require (
github.com/google/wire v0.5.0
github.com/gorilla/websocket v1.4.2
github.com/jxskiss/base62 v0.0.0-20191017122030-4f11678b909b
github.com/livekit/protocol v0.5.2
github.com/livekit/protocol v0.5.3
github.com/magefile/mage v1.11.0
github.com/maxbrunsfeld/counterfeiter/v6 v6.3.0
github.com/mitchellh/go-homedir v1.1.0
+2
View File
@@ -234,6 +234,8 @@ github.com/livekit/pion-webrtc/v3 v3.0.30 h1:2RomVfztLegWIePXcyPFJDiF5pbT64aIX4R
github.com/livekit/pion-webrtc/v3 v3.0.30/go.mod h1:XFQeLYBf++bWWA0sJqh6zF1ouWluosxwTOMOoTZGaD0=
github.com/livekit/protocol v0.5.2 h1:jYj+50gKBrtJ1q6iEVdHDruHVllA1xtOhiM1/YKqgMk=
github.com/livekit/protocol v0.5.2/go.mod h1:wo3CGfYB7XMF8GoVJAfTARrYSP/ombi+sbLl6AYdKP0=
github.com/livekit/protocol v0.5.3 h1:u4J6poLLX3zRFsJVGAz5X8KFv9+6dwffksMW/4EI+14=
github.com/livekit/protocol v0.5.3/go.mod h1:wo3CGfYB7XMF8GoVJAfTARrYSP/ombi+sbLl6AYdKP0=
github.com/lucsky/cuid v1.0.2 h1:z4XlExeoderxoPj2/dxKOyPxe9RCOu7yNq9/XWxIUMQ=
github.com/lucsky/cuid v1.0.2/go.mod h1:QaaJqckboimOmhRSJXSx/+IT+VTfxfPGSo/6mfgUfmE=
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
+1 -1
View File
@@ -38,7 +38,7 @@ func newMockParticipant(identity string, protocol types.ProtocolVersion) *typesf
p.SetTrackMutedStub = func(sid string, muted bool) {
updateTrack()
}
p.AddTrackStub = func(s string, s2 string, trackType livekit.TrackType) {
p.AddTrackStub = func(req *livekit.AddTrackRequest) {
updateTrack()
}
+24 -7
View File
@@ -29,13 +29,14 @@ var (
// MediaTrack represents a WebRTC track that needs to be forwarded
// Implements the PublishedTrack interface
type MediaTrack struct {
params MediaTrackParams
ssrc webrtc.SSRC
name string
streamID string
kind livekit.TrackType
codec webrtc.RTPCodecParameters
muted utils.AtomicFlag
params MediaTrackParams
ssrc webrtc.SSRC
name string
streamID string
kind livekit.TrackType
codec webrtc.RTPCodecParameters
muted utils.AtomicFlag
simulcasted bool
// channel to send RTCP packets to the source
lock sync.RWMutex
@@ -57,6 +58,8 @@ type MediaTrackParams struct {
ReceiverConfig ReceiverConfig
AudioConfig config.AudioConfig
Stats *RoomStatsReporter
Width uint32
Height uint32
}
func NewMediaTrack(track *webrtc.TrackRemote, params MediaTrackParams) *MediaTrack {
@@ -283,6 +286,8 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.Tra
t.params.Stats.AddPublishedTrack(t.kind.String())
}
t.receiver.AddUpTrack(track, buff, true)
// when RID is set, track is simulcasted
t.simulcasted = track.RID() != ""
buff.Bind(receiver.GetParameters(), buffer.Options{
MaxBitRate: t.params.ReceiverConfig.maxBitrate,
@@ -310,6 +315,18 @@ func (t *MediaTrack) RemoveAllSubscribers() {
t.subscribedTracks = make(map[string]*SubscribedTrack)
}
func (t *MediaTrack) ToProto() *livekit.TrackInfo {
return &livekit.TrackInfo{
Sid: t.ID(),
Type: t.Kind(),
Name: t.Name(),
Muted: t.IsMuted(),
Width: t.params.Width,
Height: t.params.Height,
Simulcast: t.simulcasted,
}
}
// TODO: send for all downtracks from the source participant
// https://tools.ietf.org/html/rfc7941
func (t *MediaTrack) sendDownTrackBindingReports(sub types.Participant) {
+12 -8
View File
@@ -188,7 +188,7 @@ func (p *ParticipantImpl) ToProto() *livekit.ParticipantInfo {
p.lock.RLock()
for _, t := range p.publishedTracks {
info.Tracks = append(info.Tracks, ToProtoTrack(t))
info.Tracks = append(info.Tracks, t.ToProto())
}
p.lock.RUnlock()
return info
@@ -275,26 +275,28 @@ func (p *ParticipantImpl) HandleOffer(sdp webrtc.SessionDescription) (answer web
// AddTrack is called when client intends to publish track.
// records track details and lets client know it's ok to proceed
func (p *ParticipantImpl) AddTrack(clientId, name string, trackType livekit.TrackType) {
func (p *ParticipantImpl) AddTrack(req *livekit.AddTrackRequest) {
p.lock.Lock()
defer p.lock.Unlock()
// if track is already published, reject
if p.pendingTracks[clientId] != nil {
if p.pendingTracks[req.Cid] != nil {
return
}
ti := &livekit.TrackInfo{
Type: trackType,
Name: name,
Sid: utils.NewGuid(utils.TrackPrefix),
Type: req.Type,
Name: req.Name,
Sid: utils.NewGuid(utils.TrackPrefix),
Width: req.Width,
Height: req.Height,
}
p.pendingTracks[clientId] = ti
p.pendingTracks[req.Cid] = ti
_ = p.writeMessage(&livekit.SignalResponse{
Message: &livekit.SignalResponse_TrackPublished{
TrackPublished: &livekit.TrackPublishedResponse{
Cid: clientId,
Cid: req.Cid,
Track: ti,
},
},
@@ -710,6 +712,8 @@ func (p *ParticipantImpl) onMediaTrack(track *webrtc.TrackRemote, rtpReceiver *w
ReceiverConfig: p.params.Config.Receiver,
AudioConfig: p.params.AudioConfig,
Stats: p.params.Stats,
Width: ti.Width,
Height: ti.Height,
})
mt.name = ti.Name
newTrack = true
+19 -3
View File
@@ -95,7 +95,13 @@ func TestTrackPublishing(t *testing.T) {
//track := &typesfakes.FakePublishedTrack{}
//track.IDReturns("id")
sink := p.params.Sink.(*routingfakes.FakeMessageSink)
p.AddTrack("cid", "webcam", livekit.TrackType_VIDEO)
p.AddTrack(&livekit.AddTrackRequest{
Cid: "cid",
Name: "webcam",
Type: livekit.TrackType_VIDEO,
Width: 1024,
Height: 768,
})
require.Equal(t, 1, sink.WriteMessageCallCount())
res := sink.WriteMessageArgsForCall(0).(*livekit.SignalResponse)
require.IsType(t, &livekit.SignalResponse_TrackPublished{}, res.Message)
@@ -103,6 +109,8 @@ func TestTrackPublishing(t *testing.T) {
require.Equal(t, "cid", published.Cid)
require.Equal(t, "webcam", published.Track.Name)
require.Equal(t, livekit.TrackType_VIDEO, published.Track.Type)
require.Equal(t, uint32(1024), published.Track.Width)
require.Equal(t, uint32(768), published.Track.Height)
})
t.Run("should not allow adding of duplicate tracks", func(t *testing.T) {
@@ -110,8 +118,16 @@ func TestTrackPublishing(t *testing.T) {
//track := &typesfakes.FakePublishedTrack{}
//track.IDReturns("id")
sink := p.params.Sink.(*routingfakes.FakeMessageSink)
p.AddTrack("cid", "webcam", livekit.TrackType_VIDEO)
p.AddTrack("cid", "duplicate", livekit.TrackType_AUDIO)
p.AddTrack(&livekit.AddTrackRequest{
Cid: "cid",
Name: "webcam",
Type: livekit.TrackType_VIDEO,
})
p.AddTrack(&livekit.AddTrackRequest{
Cid: "cid",
Name: "duplicate",
Type: livekit.TrackType_AUDIO,
})
require.Equal(t, 1, sink.WriteMessageCallCount())
})
+2 -2
View File
@@ -157,7 +157,7 @@ func (r *Room) Join(participant types.Participant, opts *ParticipantOptions) err
r.statsReporter.AddParticipant()
// it's important to set this before connection, we don't want to miss out on any publishedTracks
participant.OnTrackPublished(r.onTrackAdded)
participant.OnTrackPublished(r.onTrackPublished)
participant.OnStateChange(func(p types.Participant, oldState livekit.ParticipantInfo_State) {
logger.Debugw("participant state changed", "state", p.State(), "participant", p.Identity(),
"oldState", oldState)
@@ -344,7 +344,7 @@ func (r *Room) autoSubscribe(participant types.Participant) bool {
}
// a ParticipantImpl in the room added a new remoteTrack, subscribe other participants to it
func (r *Room) onTrackAdded(participant types.Participant, track types.PublishedTrack) {
func (r *Room) onTrackPublished(participant types.Participant, track types.PublishedTrack) {
// publish participant update, since track state is changed
r.broadcastParticipantState(participant, true)
+3 -1
View File
@@ -158,7 +158,9 @@ func TestParticipantUpdate(t *testing.T) {
"track publishes are sent to existing participants",
true,
func(p types.Participant) {
p.AddTrack("", "", livekit.TrackType_VIDEO)
p.AddTrack(&livekit.AddTrackRequest{
Type: livekit.TrackType_VIDEO,
})
},
},
}
+12 -1
View File
@@ -46,7 +46,7 @@ func (t *SubscribedTrack) SetPublisherMuted(muted bool) {
func (t *SubscribedTrack) SetVideoQuality(quality livekit.VideoQuality) {
if t.dt.Kind() == webrtc.RTPCodecTypeVideo {
t.dt.SwitchSpatialLayer(int64(quality), true)
t.dt.SwitchSpatialLayer(spatialLayerForQuality(quality), true)
}
}
@@ -54,3 +54,14 @@ func (t *SubscribedTrack) updateDownTrackMute() {
muted := t.subMuted.Get() || t.pubMuted.Get()
t.dt.Mute(muted)
}
func spatialLayerForQuality(quality livekit.VideoQuality) int64 {
switch quality {
case livekit.VideoQuality_LOW:
return 0
case livekit.VideoQuality_MEDIUM:
return 1
default:
return 2
}
}
+2 -1
View File
@@ -38,7 +38,7 @@ type Participant interface {
Negotiate()
ICERestart() error
AddTrack(clientId, name string, trackType livekit.TrackType)
AddTrack(req *livekit.AddTrackRequest)
GetPublishedTracks() []PublishedTrack
GetSubscribedTracks() []SubscribedTrack
HandleOffer(sdp webrtc.SessionDescription) (answer webrtc.SessionDescription, err error)
@@ -93,6 +93,7 @@ type PublishedTrack interface {
RemoveSubscriber(participantId string)
IsSubscriber(subId string) bool
RemoveAllSubscribers()
ToProto() *livekit.TrackInfo
// callbacks
OnClose(func())
+10 -14
View File
@@ -44,12 +44,10 @@ type FakeParticipant struct {
result1 int
result2 error
}
AddTrackStub func(string, string, livekit.TrackType)
AddTrackStub func(*livekit.AddTrackRequest)
addTrackMutex sync.RWMutex
addTrackArgsForCall []struct {
arg1 string
arg2 string
arg3 livekit.TrackType
arg1 *livekit.AddTrackRequest
}
CanPublishStub func() bool
canPublishMutex sync.RWMutex
@@ -536,18 +534,16 @@ func (fake *FakeParticipant) AddSubscriberReturnsOnCall(i int, result1 int, resu
}{result1, result2}
}
func (fake *FakeParticipant) AddTrack(arg1 string, arg2 string, arg3 livekit.TrackType) {
func (fake *FakeParticipant) AddTrack(arg1 *livekit.AddTrackRequest) {
fake.addTrackMutex.Lock()
fake.addTrackArgsForCall = append(fake.addTrackArgsForCall, struct {
arg1 string
arg2 string
arg3 livekit.TrackType
}{arg1, arg2, arg3})
arg1 *livekit.AddTrackRequest
}{arg1})
stub := fake.AddTrackStub
fake.recordInvocation("AddTrack", []interface{}{arg1, arg2, arg3})
fake.recordInvocation("AddTrack", []interface{}{arg1})
fake.addTrackMutex.Unlock()
if stub != nil {
fake.AddTrackStub(arg1, arg2, arg3)
fake.AddTrackStub(arg1)
}
}
@@ -557,17 +553,17 @@ func (fake *FakeParticipant) AddTrackCallCount() int {
return len(fake.addTrackArgsForCall)
}
func (fake *FakeParticipant) AddTrackCalls(stub func(string, string, livekit.TrackType)) {
func (fake *FakeParticipant) AddTrackCalls(stub func(*livekit.AddTrackRequest)) {
fake.addTrackMutex.Lock()
defer fake.addTrackMutex.Unlock()
fake.AddTrackStub = stub
}
func (fake *FakeParticipant) AddTrackArgsForCall(i int) (string, string, livekit.TrackType) {
func (fake *FakeParticipant) AddTrackArgsForCall(i int) *livekit.AddTrackRequest {
fake.addTrackMutex.RLock()
defer fake.addTrackMutex.RUnlock()
argsForCall := fake.addTrackArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3
return argsForCall.arg1
}
func (fake *FakeParticipant) CanPublish() bool {
@@ -94,6 +94,16 @@ type FakePublishedTrack struct {
startMutex sync.RWMutex
startArgsForCall []struct {
}
ToProtoStub func() *livekit.TrackInfo
toProtoMutex sync.RWMutex
toProtoArgsForCall []struct {
}
toProtoReturns struct {
result1 *livekit.TrackInfo
}
toProtoReturnsOnCall map[int]struct {
result1 *livekit.TrackInfo
}
invocations map[string][][]interface{}
invocationsMutex sync.RWMutex
}
@@ -576,6 +586,59 @@ func (fake *FakePublishedTrack) StartCalls(stub func()) {
fake.StartStub = stub
}
func (fake *FakePublishedTrack) ToProto() *livekit.TrackInfo {
fake.toProtoMutex.Lock()
ret, specificReturn := fake.toProtoReturnsOnCall[len(fake.toProtoArgsForCall)]
fake.toProtoArgsForCall = append(fake.toProtoArgsForCall, struct {
}{})
stub := fake.ToProtoStub
fakeReturns := fake.toProtoReturns
fake.recordInvocation("ToProto", []interface{}{})
fake.toProtoMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakePublishedTrack) ToProtoCallCount() int {
fake.toProtoMutex.RLock()
defer fake.toProtoMutex.RUnlock()
return len(fake.toProtoArgsForCall)
}
func (fake *FakePublishedTrack) ToProtoCalls(stub func() *livekit.TrackInfo) {
fake.toProtoMutex.Lock()
defer fake.toProtoMutex.Unlock()
fake.ToProtoStub = stub
}
func (fake *FakePublishedTrack) ToProtoReturns(result1 *livekit.TrackInfo) {
fake.toProtoMutex.Lock()
defer fake.toProtoMutex.Unlock()
fake.ToProtoStub = nil
fake.toProtoReturns = struct {
result1 *livekit.TrackInfo
}{result1}
}
func (fake *FakePublishedTrack) ToProtoReturnsOnCall(i int, result1 *livekit.TrackInfo) {
fake.toProtoMutex.Lock()
defer fake.toProtoMutex.Unlock()
fake.ToProtoStub = nil
if fake.toProtoReturnsOnCall == nil {
fake.toProtoReturnsOnCall = make(map[int]struct {
result1 *livekit.TrackInfo
})
}
fake.toProtoReturnsOnCall[i] = struct {
result1 *livekit.TrackInfo
}{result1}
}
func (fake *FakePublishedTrack) Invocations() map[string][][]interface{} {
fake.invocationsMutex.RLock()
defer fake.invocationsMutex.RUnlock()
@@ -601,6 +664,8 @@ func (fake *FakePublishedTrack) Invocations() map[string][][]interface{} {
defer fake.setMutedMutex.RUnlock()
fake.startMutex.RLock()
defer fake.startMutex.RUnlock()
fake.toProtoMutex.RLock()
defer fake.toProtoMutex.RUnlock()
copiedInvocations := map[string][][]interface{}{}
for key, value := range fake.invocations {
copiedInvocations[key] = value
-9
View File
@@ -93,15 +93,6 @@ func FromProtoTrickle(trickle *livekit.TrickleRequest) (webrtc.ICECandidateInit,
return ci, nil
}
func ToProtoTrack(t types.PublishedTrack) *livekit.TrackInfo {
return &livekit.TrackInfo{
Sid: t.ID(),
Type: t.Kind(),
Name: t.Name(),
Muted: t.IsMuted(),
}
}
func ToProtoTrackKind(kind webrtc.RTPCodecType) livekit.TrackType {
switch kind {
case webrtc.RTPCodecTypeVideo:
+4 -1
View File
@@ -368,7 +368,7 @@ func (r *RoomManager) rtcSessionWorker(room *rtc.Room, participant types.Partici
case *livekit.SignalRequest_AddTrack:
logger.Debugw("add track request", "participant", participant.Identity(),
"track", msg.AddTrack.Cid)
participant.AddTrack(msg.AddTrack.Cid, msg.AddTrack.Name, msg.AddTrack.Type)
participant.AddTrack(msg.AddTrack)
case *livekit.SignalRequest_Answer:
if participant.State() == livekit.ParticipantInfo_JOINING {
logger.Errorw("cannot negotiate before peer offer", nil, "participant", participant.Identity())
@@ -410,6 +410,9 @@ func (r *RoomManager) rtcSessionWorker(room *rtc.Room, participant types.Partici
if subTrack.ID() != sid {
continue
}
logger.Debugw("updating track settings",
"participant", participant.Identity(),
"settings", msg.TrackSetting)
subTrack.SetMuted(msg.TrackSetting.Disabled)
subTrack.SetVideoQuality(msg.TrackSetting.Quality)
}