forked from video-dev/hls.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio-track-controller.js
99 lines (87 loc) · 2.93 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
/*
* audio track controller
*/
import Event from '../events';
import EventHandler from '../event-handler';
import {logger} from '../utils/logger';
class AudioTrackController extends EventHandler {
constructor(hls) {
super(hls, Event.MANIFEST_LOADING,
Event.MANIFEST_LOADED,
Event.AUDIO_TRACK_LOADED);
this.tracks = [];
this.trackId = 0;
}
destroy() {
EventHandler.prototype.destroy.call(this);
}
onManifestLoading() {
// reset audio tracks on manifest loading
this.tracks = [];
this.trackId = 0;
}
onManifestLoaded(data) {
let tracks = data.audioTracks || [];
this.tracks = tracks;
this.hls.trigger(Event.AUDIO_TRACKS_UPDATED, {audioTracks : tracks});
// loop through available audio tracks and autoselect default if needed
tracks.forEach(track => {
if(track.default) {
this.audioTrack = track.id;
return;
}
});
}
onAudioTrackLoaded(data) {
if (data.id < this.tracks.length) {
logger.log(`audioTrack ${data.id} loaded`);
this.tracks[data.id].details = data.details;
// check if current playlist is a live playlist
if (data.details.live && !this.timer) {
// if live playlist we will have to reload it periodically
// set reload period to playlist target duration
this.timer = setInterval(this.ontick, 1000 * data.details.targetduration);
}
if (!data.details.live && this.timer) {
// playlist is not live and timer is armed : stopping it
clearInterval(this.timer);
this.timer = null;
}
}
}
/** get alternate audio tracks list from playlist **/
get audioTracks() {
return this.tracks;
}
/** get index of the selected audio track (index in audio track lists) **/
get audioTrack() {
return this.trackId;
}
/** select an audio track, based on its index in audio track lists**/
set audioTrack(audioTrackId) {
if (this.trackId !== audioTrackId || this.tracks[audioTrackId].details === undefined) {
this.setAudioTrackInternal(audioTrackId);
}
}
setAudioTrackInternal(newId) {
// check if level idx is valid
if (newId >= 0 && newId < this.tracks.length) {
// stopping live reloading timer if any
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
this.trackId = newId;
logger.log(`switching to audioTrack ${newId}`);
this.hls.trigger(Event.AUDIO_TRACK_SWITCH, {id: newId});
var audioTrack = this.tracks[newId];
// check if we need to load playlist for this audio Track
if (audioTrack.details === undefined || audioTrack.details.live === true) {
// track not retrieved yet, or live playlist we need to (re)load it
logger.log(`(re)loading playlist for audioTrack ${newId}`);
this.hls.trigger(Event.AUDIO_TRACK_LOADING, {url: audioTrack.url, id: newId});
}
}
}
}
export default AudioTrackController;