Commit 9575b808 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Use mids instead of track ids for indexing labels.

It turns out that track ids are not necessarily the same on the local and
remote sides.  Thanks to Ines Klimann for noticing the issue.
parent cbff5067
...@@ -346,9 +346,28 @@ func addUpConn(c *client, id string) (*upConnection, error) { ...@@ -346,9 +346,28 @@ func addUpConn(c *client, id string) (*upConnection, error) {
c.mu.Unlock() c.mu.Unlock()
return return
} }
mid := getUpMid(pc, remote)
if mid == "" {
log.Printf("Couldn't get track's mid")
c.mu.Unlock()
return
}
label, ok := u.labels[mid]
if !ok {
log.Printf("Couldn't get track's label")
isvideo := remote.Kind() == webrtc.RTPCodecTypeVideo
if isvideo {
label = "video"
} else {
label = "audio"
}
}
track := &upTrack{ track := &upTrack{
track: remote, track: remote,
label: u.labels[remote.ID()], label: label,
cache: packetcache.New(96), cache: packetcache.New(96),
rate: estimator.New(time.Second), rate: estimator.New(time.Second),
jitter: jitter.New(remote.Codec().ClockRate), jitter: jitter.New(remote.Codec().ClockRate),
...@@ -957,8 +976,20 @@ func negotiate(c *client, down *downConnection) error { ...@@ -957,8 +976,20 @@ func negotiate(c *client, down *downConnection) error {
} }
labels := make(map[string]string) labels := make(map[string]string)
for _, t := range down.tracks { for _, t := range down.pc.GetTransceivers() {
labels[t.track.ID()] = t.remote.label var track *webrtc.Track
if t.Sender() != nil {
track = t.Sender().Track()
}
if track == nil {
continue
}
for _, tr := range down.tracks {
if tr.track == track {
labels[t.Mid()] = tr.remote.label
}
}
} }
return c.write(clientMessage{ return c.write(clientMessage{
......
...@@ -96,11 +96,21 @@ type upConnection struct { ...@@ -96,11 +96,21 @@ type upConnection struct {
labels map[string]string labels map[string]string
} }
func getUpMid(pc *webrtc.PeerConnection, track *webrtc.Track) string {
for _, t := range pc.GetTransceivers() {
if t.Receiver() != nil && t.Receiver().Track() == track {
return t.Mid()
}
}
return ""
}
func (up *upConnection) complete() bool { func (up *upConnection) complete() bool {
for id, _ := range up.labels { for mid, _ := range up.labels {
found := false found := false
for _, t := range up.tracks { for _, t := range up.tracks {
if t.track.ID() == id { m := getUpMid(up.pc, t.track)
if m == mid {
found = true found = true
break break
} }
......
...@@ -41,6 +41,7 @@ function Connection(id, pc) { ...@@ -41,6 +41,7 @@ function Connection(id, pc) {
this.pc = pc; this.pc = pc;
this.stream = null; this.stream = null;
this.labels = {}; this.labels = {};
this.labelsByMid = {};
this.iceCandidates = []; this.iceCandidates = [];
this.timers = []; this.timers = [];
this.audioStats = {}; this.audioStats = {};
...@@ -647,12 +648,18 @@ async function gotOffer(id, labels, offer) { ...@@ -647,12 +648,18 @@ async function gotOffer(id, labels, offer) {
}; };
c.pc.ontrack = function(e) { c.pc.ontrack = function(e) {
let label = e.transceiver && c.labelsByMid[e.transceiver.mid];
if(label) {
c.labels[e.track.id] = label;
} else {
console.error("Couldn't find label for track");
}
c.stream = e.streams[0]; c.stream = e.streams[0];
setMedia(id); setMedia(id);
}; };
} }
c.labels = labels; c.labelsByMid = labels;
await c.pc.setRemoteDescription(offer); await c.pc.setRemoteDescription(offer);
await addIceCandidates(c); await addIceCandidates(c);
...@@ -1066,10 +1073,22 @@ async function negotiate(id) { ...@@ -1066,10 +1073,22 @@ async function negotiate(id) {
if(!offer) if(!offer)
throw(new Error("Didn't create offer")); throw(new Error("Didn't create offer"));
await c.pc.setLocalDescription(offer); await c.pc.setLocalDescription(offer);
// mids are not known until this point
c.pc.getTransceivers().forEach(t => {
if(t.sender && t.sender.track) {
let label = c.labels[t.sender.track.id];
if(label)
c.labelsByMid[t.mid] = label;
else
console.error("Couldn't find label for track");
}
});
send({ send({
type: 'offer', type: 'offer',
id: id, id: id,
labels: c.labels, labels: c.labelsByMid,
offer: offer, offer: offer,
}); });
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment