123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- 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;
- })();
|