forked from illuspas/Node-Media-Server
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbuffer-pool.js
More file actions
39 lines (32 loc) · 738 Bytes
/
buffer-pool.js
File metadata and controls
39 lines (32 loc) · 738 Bytes
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
const stream = require('stream');
const Readable = stream.Readable;
class BufferPool extends Readable {
constructor(options) {
super(options);
}
init(gFun) {
this.totalBufferLength = 0;
this.needBufferLength = 0;
this.gFun = gFun;
this.gFun.next();
}
push(buf) {
super.push(buf);
this.totalBufferLength += buf.length;
if (this.needBufferLength > 0 && this.needBufferLength <= this.totalBufferLength) {
this.gFun.next();
}
}
read(size) {
this.totalBufferLength -= size;
return super.read(size);
}
need(size) {
const ret = this.totalBufferLength < size;
if (ret) {
this.needBufferLength = size;
}
return ret;
}
}
module.exports = BufferPool;