forked from video-dev/hls.js
/
audio-track-controller.js
302 lines (248 loc) · 8.94 KB
/
audio-track-controller.js
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
import AudioTrackController from '../../../src/controller/audio-track-controller';
import Hls from '../../../src/hls';
import { Events } from '../../../src/events';
const sinon = require('sinon');
describe('AudioTrackController', function () {
const tracks = [{
groupId: '1',
id: 0,
default: true,
name: 'A'
}, {
groupId: '1',
id: 1,
default: false,
name: 'B'
}, {
groupId: '1',
id: 2,
name: 'C'
}, {
groupId: '2',
id: 0,
default: true,
name: 'A'
}, {
groupId: '2',
id: 1,
default: false,
name: 'B'
}, {
groupId: '3',
id: 2,
name: 'C'
}];
let hls;
let audioTrackController;
beforeEach(function () {
hls = new Hls();
audioTrackController = new AudioTrackController(hls);
});
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('onManifestParsed', function () {
it('should set the audioTracks contained in the event data and trigger AUDIO_TRACKS_UPDATED', function (done) {
hls.on(Hls.Events.AUDIO_TRACKS_UPDATED, (event, data) => {
expect(data.audioTracks).to.equal(tracks);
expect(audioTrackController.tracks).to.equal(tracks);
done();
});
audioTrackController.onManifestParsed(Events.MANIFEST_PARSED, {
audioTracks: tracks
});
});
it('should set the audioTracks contained in the event data (nullable) to an empty array and trigger AUDIO_TRACKS_UPDATED', function (done) {
hls.on(Hls.Events.AUDIO_TRACKS_UPDATED, (event, data) => {
expect(data.audioTracks).to.be.empty;
expect(audioTrackController.tracks).to.be.empty;
done();
});
audioTrackController.onManifestParsed(Events.MANIFEST_PARSED, {
audioTracks: null
});
});
});
it('should select audioGroupId and trigger AUDIO_TRACK_SWITCHING', function (done) {
hls.on(Hls.Events.AUDIO_TRACK_SWITCHING, (event, data) => {
done();
});
const levels = [
{
urlId: 1,
audioGroupIds: ['1', '2']
}
];
hls.levelController = {
levels
};
const newLevelInfo = levels[0];
const newGroupId = newLevelInfo.audioGroupIds[newLevelInfo.urlId];
audioTrackController.audioGroupId = '1';
audioTrackController.tracks = tracks;
audioTrackController.audioTrack = 2;
// current track name
const audioTrackName = tracks[audioTrackController.audioTrack].name;
audioTrackController.onManifestParsed({
audioTracks: tracks
});
// group has switched
expect(audioTrackController.audioGroupId).to.equal(newGroupId);
// name is still the same
expect(tracks[audioTrackController.audioTrack].name).to.equal(audioTrackName);
});
describe('_needsTrackLoading', function () {
it('should not need loading because the audioTrack is embedded in the main playlist', function () {
expect(audioTrackController._needsTrackLoading({ details: { live: true } })).to.be.false;
expect(audioTrackController._needsTrackLoading({ details: null })).to.be.false;
});
it('should need loading because the track has not been loaded yet', function () {
expect(audioTrackController._needsTrackLoading({ details: { live: true }, url: 'http://example.com/manifest.m3u8' })).to.be.true;
expect(audioTrackController._needsTrackLoading({ details: null, url: 'http://example.com/manifest.m3u8' })).to.be.true;
});
});
describe('onAudioTrackSwitched', function () {
it('should update the current audioGroupId', function () {
audioTrackController.tracks = tracks;
audioTrackController.audioGroupId = '2';
audioTrackController.onAudioTrackSwitched(Events.AUDIO_TRACK_SWITCHED, {
id: 1
});
expect(audioTrackController.audioGroupId).to.equal('1');
});
});
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 levels = [
{
urlId: 1,
audioGroupIds: ['1', '2']
}
];
hls.levelController = {
levels
};
const levelLoadedEvent = {
level: 0
};
const newLevelInfo = levels[levelLoadedEvent.level];
const newGroupId = newLevelInfo.audioGroupIds[newLevelInfo.urlId];
audioTrackController.audioGroupId = '1'; // previous group ID
audioTrackController.tracks = tracks;
audioTrackController.audioTrack = 2;
// current track name
const audioTrackName = tracks[audioTrackController.audioTrack].name;
audioTrackController.onLevelLoading(Events.LEVEL_LOADING, levelLoadedEvent);
// group has switched
expect(audioTrackController.audioGroupId).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 needsTrackLoading = sinon.spy(audioTrackController, '_needsTrackLoading');
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
});
expect(needsTrackLoading).to.have.been.calledOnce;
expect(needsTrackLoading).to.have.been.calledWith(trackWithUrl);
expect(needsTrackLoading.firstCall.returnValue, true, 'expected _needsTrackLoading to return true');
expect(audioTrackLoadingCallback).to.have.been.calledOnce;
});
it('should not attempt to load audio tracks without a url', function () {
const needsTrackLoading = sinon.spy(audioTrackController, '_needsTrackLoading');
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
});
expect(needsTrackLoading).to.have.been.calledOnce;
expect(needsTrackLoading).to.have.been.calledWith(trackWithOutUrl);
expect(needsTrackLoading.firstCall.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
});
// fatal network error clears interval
expect(audioTrackController.timer).to.equal(null);
});
it('should blacklist current track on fatal network error, and find a backup track (fallback mechanism)', function () {
const currentTrackId = 4;
audioTrackController._trackId = currentTrackId;
audioTrackController.tracks = tracks;
audioTrackController.onError(Events.ERROR, {
type: Hls.ErrorTypes.MEDIA_ERROR,
fatal: true
});
expect(!!audioTrackController.trackIdBlacklist[currentTrackId]).to.be.false;
audioTrackController.onError(Events.ERROR, {
type: Hls.ErrorTypes.NETWORK_ERROR,
fatal: true
});
expect(!!audioTrackController.trackIdBlacklist[currentTrackId]).to.be.false;
audioTrackController.onError(Events.ERROR, {
type: Hls.ErrorTypes.NETWORK_ERROR,
details: Hls.ErrorDetails.AUDIO_TRACK_LOAD_ERROR,
fatal: true,
context: {
id: 'foobarLoaderContextId'
}
});
expect(!!audioTrackController.trackIdBlacklist[currentTrackId]).to.be.true;
expect(audioTrackController.audioTrack).to.equal(1);
});
});
});