-
Notifications
You must be signed in to change notification settings - Fork 77
/
m3u.js
195 lines (163 loc) · 5.31 KB
/
m3u.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
var M3U = module.exports = function M3U() {
this.items = {
PlaylistItem: [],
StreamItem: [],
IframeStreamItem: [],
MediaItem: []
};
this.properties = {};
};
M3U.PlaylistItem = require('./m3u/PlaylistItem');
M3U.MediaItem = require('./m3u/MediaItem');
M3U.StreamItem = require('./m3u/StreamItem');
M3U.IframeStreamItem = require('./m3u/IframeStreamItem');
var Item = require('./m3u/Item');
M3U.create = function createM3U() {
return new M3U;
};
M3U.prototype.get = function getProperty(key) {
return this.properties[key];
};
M3U.prototype.set = function setProperty(key, value) {
var tagKey = propertyMap.findByTag(key);
if (tagKey) key = tagKey.key;
this.properties[key] = coerce[dataTypes[key] || 'unknown'](value);
return this;
};
M3U.prototype.addItem = function addItem(item) {
this.items[item.constructor.name].push(item);
return this;
};
M3U.prototype.addPlaylistItem = function addPlaylistItem(data) {
this.items.PlaylistItem.push(M3U.PlaylistItem.create(data));
};
M3U.prototype.removePlaylistItem = function removePlaylistItem(index) {
if (index < this.items.PlaylistItem.length && index >= 0){
this.items.PlaylistItem.splice(index, 1);
} else {
throw new RangeError('M3U PlaylistItem out of range');
}
};
M3U.prototype.addMediaItem = function addMediaItem(data) {
this.items.MediaItem.push(M3U.MediaItem.create(data));
};
M3U.prototype.addStreamItem = function addStreamItem(data) {
this.items.StreamItem.push(M3U.StreamItem.create(data));
};
M3U.prototype.addIframeStreamItem = function addIframeStreamItem(data) {
this.items.IframeStreamItem.push(M3U.IframeStreamItem.create(data));
};
M3U.prototype.domainDurations = function domainDurations() {
var index = 0;
return this.items.PlaylistItem.reduce(function(duration, item) {
if (item.get('discontinuity')) {
index = duration.push(0) - 1;
}
duration[index] += item.get('duration');
return duration;
}, [0]);
};
M3U.prototype.totalDuration = function totalDuration() {
return this.items.PlaylistItem.reduce(function(duration, item) {
return duration + item.get('duration');
}, 0);
};
M3U.prototype.merge = function merge(m3u) {
if (m3u.get('targetDuration') > this.get('targetDuration')) {
this.set('targetDuration', m3u.get('targetDuration'));
}
m3u.items.PlaylistItem[0].set('discontinuity', true);
this.items.PlaylistItem = this.items.PlaylistItem.concat(m3u.items.PlaylistItem);
return this;
};
M3U.prototype.toString = function toString() {
var self = this;
var output = ['#EXTM3U'];
Object.keys(this.properties).forEach(function(key) {
var tagKey = propertyMap.findByKey(key);
var tag = tagKey ? tagKey.tag : key;
if (dataTypes[key] == 'boolean') {
output.push('#' + tag);
} else {
output.push('#' + tag + ':' + self.get(key));
}
});
if (this.items.PlaylistItem.length) {
output.push(this.items.PlaylistItem.map(itemToString).join('\n'));
if (this.get('playlistType') === 'VOD') {
output.push('#EXT-X-ENDLIST');
}
} else {
if (this.items.StreamItem.length) {
output.push(this.items.StreamItem.map(itemToString).join('\n') + '\n');
}
if (this.items.IframeStreamItem.length) {
output.push(this.items.IframeStreamItem.map(itemToString).join('\n') + '\n');
}
if (this.items.MediaItem.length) {
output.push(this.items.MediaItem.map(itemToString).join('\n') + '\n');
}
}
return output.join('\n') + '\n';
};
M3U.prototype.serialize = function serialize() {
var object = { properties: this.properties, items: {} };
var self = this;
Object.keys(this.items).forEach(function(constructor) {
object.items[constructor] = self.items[constructor].map(serializeItem);
});
return object;
};
M3U.unserialize = function unserialize(object) {
var m3u = new M3U;
m3u.properties = object.properties;
Object.keys(object.items).forEach(function(constructor) {
m3u.items[constructor] = object.items[constructor].map(
Item.unserialize.bind(null, M3U[constructor])
);
});
return m3u;
};
function itemToString(item) {
return item.toString();
}
function serializeItem(item) {
return item.serialize();
}
var coerce = {
boolean: function coerceBoolean(value) {
return true;
},
integer: function coerceInteger(value) {
return parseInt(value, 10);
},
unknown: function coerceUnknown(value) {
return value;
}
};
var dataTypes = {
iframesOnly : 'boolean',
independentSegments : 'boolean',
targetDuration : 'integer',
mediaSequence : 'integer',
version : 'integer'
};
var propertyMap = [
{ tag: 'EXT-X-ALLOW-CACHE', key: 'allowCache' },
{ tag: 'EXT-X-I-FRAMES-ONLY', key: 'iframesOnly' },
{ tag: 'EXT-X-INDEPENDENT-SEGMENTS', key: 'independentSegments' },
{ tag: 'EXT-X-MEDIA-SEQUENCE', key: 'mediaSequence' },
{ tag: 'EXT-X-PLAYLIST-TYPE', key: 'playlistType' },
{ tag: 'EXT-X-TARGETDURATION', key: 'targetDuration' },
{ tag: 'EXT-X-VERSION', key: 'version' }
];
propertyMap.findByTag = function findByTag(tag) {
return propertyMap[propertyMap.map(function(tagKey) {
return tagKey.tag;
}).indexOf(tag)];
};
propertyMap.findByKey = function findByKey(key) {
return propertyMap[propertyMap.map(function(tagKey) {
return tagKey.key;
}).indexOf(key)];
};