forked from little-core-labs/little-media-box
-
Notifications
You must be signed in to change notification settings - Fork 1
/
delivery.js
209 lines (180 loc) · 4.67 KB
/
delivery.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
const { Asset } = require('./asset')
const { EventEmitter } = require('events')
const { Source } = require('./source')
const { demux } = require('./demux')
const { Pool } = require('nanoresource-pool')
const assert = require('assert')
const Batch = require('batch')
const uuid = require('uuid/v4')
const os = require('os')
/**
* Default concurrency of source probing (`ffprobe`).
* @private
*/
const DEFAULT_PROBE_CONCURRENCY = 2 * os.cpus().length
/**
* Default concurrency of source probing (`ffmpeg`).
* @private
*/
const DEFAULT_DEMUX_CONCURRENCY = os.cpus().length
/**
* The `Delivery` class represents a container of multiple
* sources.
* @public
* @class
* @extends nanoresource-pool
*/
class Delivery extends Pool {
/**
* `Delivery` class constructor.
* @param {(Object)} opts
* @param {(String)} opts.id
*/
constructor(opts) {
super(Source, opts)
if (!opts || 'object' !== typeof opts) {
opts = {}
}
this.id = opts.id || uuid()
this.date = Date.now()
this.dateIso = new Date(this.date).toISOString()
}
/**
* All assets in the delivery pool, including
* child delivery pools.
* @accessor
* @type {Array<Source>}
*/
get assets() {
return this.sources.filter(s => s instanceof Asset)
}
/**
* All sources in the delivery pool, including
* child delivery pools.
* @accessor
* @type {Array<Source>}
*/
get sources() {
return this.query()
}
/**
* Creates and adds a new Asset from a URI. Associates it with the Delivery.
* @param {String} uri
* @param {Object} opts
* @return {Asset}
*/
associate(uri, opts={}) {
// Do not allow a new resource to be created if one already exists in the
// pool with the given `uri`
if (this.sources.some(s => s.uri.includes(uri))) {
return this.sources.filter(s => s.uri.includes(uri))[0]
}
const asset = new Asset(uri, { associations: opts.associations || [this.id]})
return this.add(asset)
}
/**
* Creates and adds a new source from a URI.
* @param {String} uri
* @param {Object} opts
* @return {Source}
*/
source(uri, opts) {
// Do not allow a new resource to be created if one already exists in the
// pool with the given `uri`
if (this.sources.some(s => s.uri.includes(uri))) {
return this.sources.filter(s => s.uri.includes(uri))[0]
}
return this.resource(uri, opts)
}
/**
* Probes all sources in delivery pool.
* @param {Object} opts
* @param {Function} callback
*/
probe(opts, callback) {
if ('function' === typeof opts) {
callback = opts
}
if (!opts || 'object' !== typeof opts) {
opts = {}
}
assert('function' === typeof callback, 'callback is not a function')
const { concurrency = DEFAULT_PROBE_CONCURRENCY } = opts
const { sources } = this
const probes = {}
const batch = new Batch().concurrency(concurrency)
for (const source of sources) {
batch.push((next) => {
source.probe((err, info) => {
if (err) { return next(err) }
probes[source.uri] = info
next(null)
})
})
}
batch.end((err) => {
if (err) { return callback(err) }
callback(null, probes)
})
}
/**
* An alias for `probe()`.
* @param {Function} callback
*/
stat(callback) {
this.probe(callback)
}
/**
* Demux sources into output streams. Outputs
* @param {(Object)} opts
* @param {Function} callback
* @return {EventEmitter}
*/
demux(opts, callback) {
if ('function' === typeof opts) {
callback = opts
}
if (!opts || 'object' !== typeof opts) {
opts = {}
}
assert('function' === typeof callback, 'callback is not a function')
const { concurrency = DEFAULT_PROBE_CONCURRENCY } = opts
const { sources } = this
const demuxes = {}
const emitter = new EventEmitter()
const batch = new Batch()
batch.concurrency(concurrency)
emitter.setMaxListeners(0)
for (const source of sources) {
batch.push((next) => {
const demuxer = demux(source, opts, (err, outputs) => {
if (err) { return next(err) }
demuxes[source.uri] = outputs
next(null)
})
const proxy = (event) => {
demuxer.on(event, (...args) => {
emitter.emit(event, ...args.concat(source))
})
}
proxy('codecData')
proxy('end')
proxy('error')
proxy('progress')
proxy('start')
proxy('stderr')
})
}
batch.end((err) => {
if (err) { return callback(err) }
callback(null, demuxes)
})
return emitter
}
}
/**
* Module exports.
*/
module.exports = {
Delivery
}