/
audio-track-controller.ts
426 lines (373 loc) · 12 KB
/
audio-track-controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import AudioTrackController from '../../../src/controller/audio-track-controller';
import Hls, { MediaPlaylist } from '../../../src/hls';
import { AttrList } from '../../../src/utils/attr-list';
import { LevelDetails } from '../../../src/loader/level-details';
import { Events } from '../../../src/events';
import * as sinon from 'sinon';
import * as chai from 'chai';
import * as sinonChai from 'sinon-chai';
import { PlaylistContextType } from '../../../src/types/loader';
chai.use(sinonChai);
const expect = chai.expect;
describe('AudioTrackController', function () {
const tracks: MediaPlaylist[] = [
{
attrs: new AttrList({}),
bitrate: 0,
autoselect: false,
default: true,
forced: false,
groupId: '1',
id: 0,
name: 'A',
type: 'AUDIO',
url: '',
},
{
attrs: new AttrList({}),
bitrate: 0,
autoselect: false,
default: false,
forced: false,
groupId: '1',
id: 1,
name: 'B',
type: 'AUDIO',
url: '',
},
{
attrs: new AttrList({}),
bitrate: 0,
autoselect: false,
default: false,
forced: false,
groupId: '1',
id: 2,
name: 'C',
type: 'AUDIO',
url: '',
},
{
attrs: new AttrList({}),
bitrate: 0,
autoselect: false,
default: true,
forced: false,
groupId: '2',
id: 0,
name: 'A',
type: 'AUDIO',
url: '',
},
{
attrs: new AttrList({}),
bitrate: 0,
autoselect: false,
default: false,
forced: false,
groupId: '2',
id: 1,
name: 'B',
type: 'AUDIO',
url: '',
},
{
attrs: new AttrList({}),
bitrate: 0,
autoselect: false,
default: false,
forced: false,
groupId: '2',
id: 2,
name: 'C',
type: 'AUDIO',
url: '',
},
];
let hls; //: Hls;
let audioTrackController; //: AudioTrackController;
const levels = [
{
urlId: 1,
audioGroupIds: ['1', '2'],
},
];
beforeEach(function () {
hls = new Hls();
audioTrackController = new AudioTrackController(hls);
hls.levelController = {
levels,
};
});
afterEach(function () {
hls.destroy();
});
describe('onManifestLoading', function () {
it('should reset the tracks list and current trackId', function () {
audioTrackController.tracks = tracks;
audioTrackController.onManifestLoading();
expect(audioTrackController.tracks).to.be.empty;
});
});
describe('onLevelLoading', function () {
it('should set the audioTracks contained in the event data and trigger AUDIO_TRACKS_UPDATED', function () {
const audioTracksUpdatedCallback = sinon.spy();
hls.on(Hls.Events.AUDIO_TRACKS_UPDATED, audioTracksUpdatedCallback);
audioTrackController.onManifestParsed(Events.MANIFEST_PARSED, {
audioTracks: tracks,
});
audioTrackController.onLevelLoading(Events.LEVEL_LOADING, {
level: 0,
});
expect(audioTrackController.tracks).to.equal(tracks);
expect(audioTracksUpdatedCallback).to.be.calledOnce;
expect(audioTracksUpdatedCallback).to.be.calledWith(
Events.AUDIO_TRACKS_UPDATED,
{
audioTracks: tracks.slice(3, 6),
}
);
});
});
it('should select audioGroupId and trigger AUDIO_TRACK_SWITCHING', function (done) {
hls.on(Hls.Events.AUDIO_TRACK_SWITCHING, (event, data) => {
done();
});
const newLevelInfo = levels[0];
const newGroupId = newLevelInfo.audioGroupIds[newLevelInfo.urlId];
audioTrackController.tracks = tracks;
// Update the level to set audioGroupId
audioTrackController.onLevelLoading(Events.LEVEL_LOADING, {
level: 0,
});
audioTrackController.audioTrack = 2;
// current track name
const audioTrackName = tracks[audioTrackController.audioTrack].name;
audioTrackController.onManifestParsed(Events.MANIFEST_PARSED, {
audioTracks: tracks,
});
// group has switched
expect(audioTrackController.groupId).to.equal(newGroupId);
// name is still the same
expect(tracks[audioTrackController.audioTrack].name).to.equal(
audioTrackName
);
});
it('should always switch tracks when audioTrack is set to a valid index', function () {
const audioTracksUpdatedCallback = sinon.spy();
const audioTrackSwitchingCallback = sinon.spy();
hls.on(Hls.Events.AUDIO_TRACKS_UPDATED, audioTracksUpdatedCallback);
hls.on(Hls.Events.AUDIO_TRACK_SWITCHING, audioTrackSwitchingCallback);
audioTrackController.onManifestParsed(Events.MANIFEST_PARSED, {
audioTracks: tracks,
});
audioTrackController.onLevelLoading(Events.LEVEL_LOADING, {
level: 0,
});
expect(audioTracksUpdatedCallback, 'AUDIO_TRACKS_UPDATED').to.have.been
.calledOnce;
expect(
audioTrackSwitchingCallback,
'AUDIO_TRACK_SWITCHING to initial track 0'
).to.have.been.calledOnce;
audioTrackController.onAudioTrackLoaded(Events.AUDIO_TRACK_LOADED, {
details: new LevelDetails(''),
id: 0,
groupId: '1',
networkDetails: null,
stats: { loading: {} },
deliveryDirectives: null,
});
expect(audioTrackController.tracksInGroup[0], 'tracksInGroup[0]')
.to.have.property('details')
.which.is.an('object');
audioTrackController.audioTrack = 1;
expect(audioTrackSwitchingCallback, 'AUDIO_TRACK_SWITCHING to track 1').to
.have.been.calledTwice;
audioTrackController.onAudioTrackLoaded(Events.AUDIO_TRACK_LOADED, {
details: new LevelDetails(''),
id: 1,
groupId: '1',
networkDetails: null,
stats: { loading: {} },
deliveryDirectives: null,
});
expect(audioTrackController.tracksInGroup[1], 'tracksInGroup[1]')
.to.have.property('details')
.which.is.an('object');
audioTrackController.audioTrack = 0;
expect(audioTrackSwitchingCallback, 'AUDIO_TRACK_SWITCHING back to track 0')
.to.have.been.calledThrice;
});
describe('shouldLoadTrack', function () {
it('should not need loading because the audioTrack is embedded in the main playlist', function () {
audioTrackController.canLoad = true;
expect(audioTrackController.shouldLoadTrack({ details: { live: true } }))
.to.be.false;
expect(audioTrackController.shouldLoadTrack({ details: undefined })).to.be
.false;
});
it('should need loading because the track has not been loaded yet', function () {
audioTrackController.canLoad = true;
expect(
audioTrackController.shouldLoadTrack({
details: { live: true },
url: 'http://example.com/manifest.m3u8',
}),
'track 1'
).to.be.true;
expect(
audioTrackController.shouldLoadTrack({
details: null,
url: 'http://example.com/manifest.m3u8',
}),
'track 2'
).to.be.true;
});
});
describe('onLevelLoading', function () {
it('should reselect the current track and trigger AUDIO_TRACK_SWITCHING eventually', function (done) {
hls.on(Hls.Events.AUDIO_TRACK_SWITCHING, (event, data) => {
done();
});
const levelLoadedEvent = {
level: 0,
};
const newLevelInfo = levels[levelLoadedEvent.level];
const newGroupId = newLevelInfo.audioGroupIds[newLevelInfo.urlId];
audioTrackController.tracks = tracks;
audioTrackController.onLevelLoading(Events.LEVEL_LOADING, {
level: 0,
});
audioTrackController.audioTrack = 2;
// current track name
const audioTrackName = tracks[audioTrackController.audioTrack].name;
audioTrackController.onLevelLoading(
Events.LEVEL_LOADING,
levelLoadedEvent
);
// group has switched
expect(audioTrackController.groupId).to.equal(newGroupId);
// name is still the same
expect(tracks[audioTrackController.audioTrack].name).to.equal(
audioTrackName
);
});
it('should load audio tracks with a url', function () {
const shouldLoadTrack = sinon.spy(
audioTrackController,
'shouldLoadTrack'
);
const audioTrackLoadingCallback = sinon.spy();
const trackWithUrl = {
groupId: '1',
id: 0,
name: 'A',
default: true,
url: './trackA.m3u8',
};
hls.on(Hls.Events.AUDIO_TRACK_LOADING, audioTrackLoadingCallback);
hls.levelController = {
levels: [
{
urlId: 0,
audioGroupIds: ['1'],
},
],
};
audioTrackController.tracks = [trackWithUrl];
audioTrackController.onLevelLoading(Events.LEVEL_LOADING, {
level: 0,
});
audioTrackController.startLoad();
expect(shouldLoadTrack).to.have.been.calledTwice;
expect(shouldLoadTrack).to.have.been.calledWith(trackWithUrl);
expect(
shouldLoadTrack.firstCall.returnValue,
'expected shouldLoadTrack to return false before startLoad() is called'
).to.be.false;
expect(
shouldLoadTrack.secondCall.returnValue,
'expected shouldLoadTrack to return true after startLoad() is called'
).to.be.true;
expect(audioTrackLoadingCallback).to.have.been.calledOnce;
});
it('should not attempt to load audio tracks without a url', function () {
const shouldLoadTrack = sinon.spy(
audioTrackController,
'shouldLoadTrack'
);
const audioTrackLoadingCallback = sinon.spy();
const trackWithOutUrl = tracks[0];
hls.on(Hls.Events.AUDIO_TRACK_LOADING, audioTrackLoadingCallback);
hls.levelController = {
levels: [
{
urlId: 0,
audioGroupIds: ['1'],
},
],
};
audioTrackController.tracks = tracks;
audioTrackController.onLevelLoading(Events.LEVEL_LOADING, {
level: 0,
});
audioTrackController.startLoad(0);
expect(shouldLoadTrack).to.have.been.calledTwice;
expect(shouldLoadTrack).to.have.been.calledWith(trackWithOutUrl);
expect(shouldLoadTrack.firstCall.returnValue).to.be.false;
expect(shouldLoadTrack.secondCall.returnValue).to.be.false;
expect(audioTrackLoadingCallback).to.not.have.been.called;
});
});
describe('onError', function () {
it('should clear interval (only) on fatal network errors', function () {
audioTrackController.timer = 1000;
audioTrackController.onError(Events.ERROR, {
type: Hls.ErrorTypes.MEDIA_ERROR,
});
expect(audioTrackController.timer).to.equal(1000);
audioTrackController.onError(Events.ERROR, {
type: Hls.ErrorTypes.MEDIA_ERROR,
fatal: true,
});
expect(audioTrackController.timer).to.equal(1000);
audioTrackController.onError(Events.ERROR, {
type: Hls.ErrorTypes.NETWORK_ERROR,
fatal: false,
});
expect(audioTrackController.timer).to.equal(1000);
audioTrackController.onError(Events.ERROR, {
type: Hls.ErrorTypes.NETWORK_ERROR,
fatal: true,
});
expect(audioTrackController.timer).to.equal(-1);
});
it('should retry track loading if track has not changed', function () {
const retryLoadingOrFail = sinon.spy(
audioTrackController,
'retryLoadingOrFail'
);
const currentTrackId = 4;
const currentGroupId = 'aac';
audioTrackController.trackId = currentTrackId;
audioTrackController.groupId = currentGroupId;
audioTrackController.tracks = tracks;
audioTrackController.onError(Events.ERROR, {
type: Hls.ErrorTypes.NETWORK_ERROR,
details: Hls.ErrorDetails.AUDIO_TRACK_LOAD_ERROR,
fatal: false,
context: {
type: PlaylistContextType.AUDIO_TRACK,
id: currentTrackId,
groupId: currentGroupId,
},
});
expect(
audioTrackController.audioTrack,
'track index/id is not changed as there is no redundant track to choose from'
).to.equal(4);
expect(retryLoadingOrFail).to.have.been.calledOnce;
});
});
});