forked from illuspas/Node-Media-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_core_bufferpool.js
59 lines (49 loc) · 1010 Bytes
/
node_core_bufferpool.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
//
// Created by Mingliang Chen on 17/8/1.
// illuspas[a]gmail.com
// Copyright (c) 2017 Nodemedia. All rights reserved.
//
const Readable = require('stream').Readable;
class BufferPool extends Readable {
constructor(gfun, options) {
super(options);
this.gFun = gfun;
}
_read(size) {
}
init(gFun) {
this.readBytes = 0;
this.poolBytes = 0;
this.needBytes = 0;
this.gFun.next(false);
}
stop() {
try {
this.gFun.next(true);
} catch (e) {
// console.log(e);
}
}
push(buf) {
super.push(buf);
this.poolBytes += buf.length;
this.readBytes += buf.length;
if (this.needBytes > 0 && this.needBytes <= this.poolBytes) {
this.gFun.next(false);
}
}
read(size) {
this.poolBytes -= size;
return super.read(size);
}
need(size) {
let ret = this.poolBytes < size;
if (ret) {
this.needBytes = size;
} else {
this.needBytes = 0;
}
return ret;
}
}
module.exports = BufferPool