Skip to content
This repository was archived by the owner on Jan 16, 2023. It is now read-only.

Commit 58ef6a2

Browse files
dsilhavybbert
authored andcommitted
Add missing reason to SwitchRequest in ABRRulesCollection.js (Dash-Industry-Forum#3487)
* Add missing reason to SwitchRequest in ABRRulesCollection.js * Add additional unit test
1 parent e20ea56 commit 58ef6a2

File tree

2 files changed

+154
-15
lines changed

2 files changed

+154
-15
lines changed

src/streaming/rules/abr/ABRRulesCollection.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -135,46 +135,57 @@ function ABRRulesCollection(config) {
135135
return srArray.filter(sr => sr.quality > SwitchRequest.NO_CHANGE);
136136
}
137137

138+
/**
139+
*
140+
* @param {array} srArray
141+
* @return {object} SwitchRequest
142+
*/
138143
function getMinSwitchRequest(srArray) {
139144
const values = {};
145+
let newSwitchReq = null;
140146
let i,
141147
len,
142148
req,
143-
newQuality,
144-
quality;
149+
quality,
150+
reason;
145151

146152
if (srArray.length === 0) {
147153
return;
148154
}
149155

150-
values[SwitchRequest.PRIORITY.STRONG] = SwitchRequest.NO_CHANGE;
151-
values[SwitchRequest.PRIORITY.WEAK] = SwitchRequest.NO_CHANGE;
152-
values[SwitchRequest.PRIORITY.DEFAULT] = SwitchRequest.NO_CHANGE;
156+
values[SwitchRequest.PRIORITY.STRONG] = { quality: SwitchRequest.NO_CHANGE, reason: null };
157+
values[SwitchRequest.PRIORITY.WEAK] = { quality: SwitchRequest.NO_CHANGE, reason: null };
158+
values[SwitchRequest.PRIORITY.DEFAULT] = { quality: SwitchRequest.NO_CHANGE, reason: null };
153159

154160
for (i = 0, len = srArray.length; i < len; i += 1) {
155161
req = srArray[i];
156162
if (req.quality !== SwitchRequest.NO_CHANGE) {
157-
values[req.priority] = values[req.priority] > SwitchRequest.NO_CHANGE ? Math.min(values[req.priority], req.quality) : req.quality;
163+
// We only use the new quality in case it is lower than the already saved one or if no new quality has been selected for the respective priority
164+
if (values[req.priority].quality === SwitchRequest.NO_CHANGE || values[req.priority].quality > req.quality) {
165+
values[req.priority].quality = req.quality;
166+
values[req.priority].reason = req.reason || null;
167+
}
158168
}
159169
}
160170

161-
if (values[SwitchRequest.PRIORITY.WEAK] !== SwitchRequest.NO_CHANGE) {
162-
newQuality = values[SwitchRequest.PRIORITY.WEAK];
171+
if (values[SwitchRequest.PRIORITY.WEAK].quality !== SwitchRequest.NO_CHANGE) {
172+
newSwitchReq = values[SwitchRequest.PRIORITY.WEAK];
163173
}
164174

165-
if (values[SwitchRequest.PRIORITY.DEFAULT] !== SwitchRequest.NO_CHANGE) {
166-
newQuality = values[SwitchRequest.PRIORITY.DEFAULT];
175+
if (values[SwitchRequest.PRIORITY.DEFAULT].quality !== SwitchRequest.NO_CHANGE) {
176+
newSwitchReq = values[SwitchRequest.PRIORITY.DEFAULT];
167177
}
168178

169-
if (values[SwitchRequest.PRIORITY.STRONG] !== SwitchRequest.NO_CHANGE) {
170-
newQuality = values[SwitchRequest.PRIORITY.STRONG];
179+
if (values[SwitchRequest.PRIORITY.STRONG].quality !== SwitchRequest.NO_CHANGE) {
180+
newSwitchReq = values[SwitchRequest.PRIORITY.STRONG];
171181
}
172182

173-
if (newQuality !== SwitchRequest.NO_CHANGE) {
174-
quality = newQuality;
183+
if (newSwitchReq) {
184+
quality = newSwitchReq.quality;
185+
reason = newSwitchReq.reason;
175186
}
176187

177-
return SwitchRequest(context).create(quality);
188+
return SwitchRequest(context).create(quality, reason);
178189
}
179190

180191
function getMaxQuality(rulesContext) {
@@ -211,6 +222,7 @@ function ABRRulesCollection(config) {
211222
initialize,
212223
reset,
213224
getMaxQuality,
225+
getMinSwitchRequest,
214226
shouldAbandonFragment,
215227
getQualitySwitchRules
216228
};

test/unit/streaming.rules.abr.ABRRulesCollection.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,132 @@ describe('ABRRulesCollection', function () {
8181
expect(shouldAbandonFragment.quality).to.be.equal(SwitchRequest.NO_CHANGE); // jshint ignore:line
8282
});
8383

84+
it('should return correct switch request in getMinSwitchRequest for a single item', () => {
85+
const srArray = [{
86+
quality: 5,
87+
priority: SwitchRequest.PRIORITY.WEAK,
88+
reason: {
89+
throughput: 1000
90+
}
91+
}];
92+
93+
const sr = abrRulesCollection.getMinSwitchRequest(srArray);
94+
95+
expect(sr.quality).to.be.equal(5);
96+
expect(sr.reason.throughput).to.be.equal(1000);
97+
});
98+
99+
it('should return correct switch request in getMinSwitchRequest for multiple items with similar priorities', () => {
100+
const srArray = [
101+
{
102+
quality: 6,
103+
priority: SwitchRequest.PRIORITY.WEAK,
104+
reason: {
105+
throughput: 60
106+
}
107+
},
108+
{
109+
quality: 4,
110+
priority: SwitchRequest.PRIORITY.WEAK,
111+
reason: {
112+
throughput: 40
113+
}
114+
},
115+
{
116+
quality: 5,
117+
priority: SwitchRequest.PRIORITY.WEAK,
118+
reason: {
119+
throughput: 50
120+
}
121+
}
122+
];
123+
124+
const sr = abrRulesCollection.getMinSwitchRequest(srArray);
125+
126+
expect(sr.quality).to.be.equal(4);
127+
expect(sr.reason.throughput).to.be.equal(40);
128+
});
129+
130+
it('should return correct switch request in getMinSwitchRequest for multiple items with different priorities', () => {
131+
const srArray = [
132+
{
133+
quality: 6,
134+
priority: SwitchRequest.PRIORITY.DEFAULT,
135+
reason: {
136+
throughput: 60
137+
}
138+
},
139+
{
140+
quality: 5,
141+
priority: SwitchRequest.PRIORITY.STRONG,
142+
reason: {
143+
throughput: 50
144+
}
145+
},
146+
{
147+
quality: 7,
148+
priority: SwitchRequest.PRIORITY.WEAK,
149+
reason: {
150+
throughput: 70
151+
}
152+
}
153+
];
154+
155+
const sr = abrRulesCollection.getMinSwitchRequest(srArray);
156+
157+
expect(sr.quality).to.be.equal(5);
158+
expect(sr.reason.throughput).to.be.equal(50);
159+
});
160+
161+
it('should return correct switch request in getMinSwitchRequest for multiple items with different and similar priorities', () => {
162+
const srArray = [
163+
{
164+
quality: 6,
165+
priority: SwitchRequest.PRIORITY.DEFAULT,
166+
reason: {
167+
throughput: 60
168+
}
169+
},
170+
{
171+
quality: 5,
172+
priority: SwitchRequest.PRIORITY.STRONG,
173+
reason: {
174+
throughput: 50
175+
}
176+
},
177+
{
178+
quality: 4,
179+
priority: SwitchRequest.PRIORITY.STRONG,
180+
reason: {
181+
throughput: 40
182+
}
183+
},
184+
{
185+
quality: 7,
186+
priority: SwitchRequest.PRIORITY.WEAK,
187+
reason: {
188+
throughput: 70
189+
}
190+
}
191+
];
192+
193+
const sr = abrRulesCollection.getMinSwitchRequest(srArray);
194+
195+
expect(sr.quality).to.be.equal(4);
196+
expect(sr.reason.throughput).to.be.equal(40);
197+
});
198+
199+
it('should return correct switch request in getMinSwitchRequest for a single item without reason', () => {
200+
const srArray = [{
201+
quality: 5,
202+
priority: SwitchRequest.PRIORITY.WEAK
203+
}];
204+
205+
const sr = abrRulesCollection.getMinSwitchRequest(srArray);
206+
207+
expect(sr.quality).to.be.equal(5);
208+
expect(sr.reason).to.be.null; // jshint ignore:line
209+
});
210+
84211
});
85212
});

0 commit comments

Comments
 (0)