forked from phoboslab/jsmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.js
112 lines (89 loc) · 2.45 KB
/
decoder.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
JSMpeg.Decoder.Base = (function(){ "use strict";
var BaseDecoder = function(options) {
this.destination = null;
this.canPlay = false;
this.collectTimestamps = !options.streaming;
this.bytesWritten = 0;
this.timestamps = [];
this.timestampIndex = 0;
this.startTime = 0;
this.decodedTime = 0;
Object.defineProperty(this, 'currentTime', {get: this.getCurrentTime});
};
BaseDecoder.prototype.destroy = function() {};
BaseDecoder.prototype.connect = function(destination) {
this.destination = destination;
};
BaseDecoder.prototype.bufferGetIndex = function() {
return this.bits.index;
};
BaseDecoder.prototype.bufferSetIndex = function(index) {
this.bits.index = index;
};
BaseDecoder.prototype.bufferWrite = function(buffers) {
return this.bits.write(buffers);
};
BaseDecoder.prototype.write = function(pts, buffers) {
if (this.collectTimestamps) {
if (this.timestamps.length === 0) {
this.startTime = pts;
this.decodedTime = pts;
}
this.timestamps.push({index: this.bytesWritten << 3, time: pts});
}
this.bytesWritten += this.bufferWrite(buffers);
this.canPlay = true;
};
BaseDecoder.prototype.seek = function(time) {
if (!this.collectTimestamps) {
return;
}
this.timestampIndex = 0;
for (var i = 0; i < this.timestamps.length; i++) {
if (this.timestamps[i].time > time) {
break;
}
this.timestampIndex = i;
}
var ts = this.timestamps[this.timestampIndex];
if (ts) {
this.bufferSetIndex(ts.index);
this.decodedTime = ts.time;
}
else {
this.bufferSetIndex(0);
this.decodedTime = this.startTime;
}
};
BaseDecoder.prototype.decode = function() {
this.advanceDecodedTime(0);
};
BaseDecoder.prototype.advanceDecodedTime = function(seconds) {
if (this.collectTimestamps) {
var newTimestampIndex = -1;
var currentIndex = this.bufferGetIndex();
for (var i = this.timestampIndex; i < this.timestamps.length; i++) {
if (this.timestamps[i].index > currentIndex) {
break;
}
newTimestampIndex = i;
}
// Did we find a new PTS, different from the last? If so, we don't have
// to advance the decoded time manually and can instead sync it exactly
// to the PTS.
if (
newTimestampIndex !== -1 &&
newTimestampIndex !== this.timestampIndex
) {
this.timestampIndex = newTimestampIndex;
this.decodedTime = this.timestamps[this.timestampIndex].time;
return;
}
}
this.decodedTime += seconds;
};
BaseDecoder.prototype.getCurrentTime = function() {
return this.decodedTime;
};
return BaseDecoder;
})();