decoder.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. JSMpeg.Decoder.Base = (function(){ "use strict";
  2. var BaseDecoder = function(options) {
  3. this.destination = null;
  4. this.canPlay = false;
  5. this.collectTimestamps = !options.streaming;
  6. this.bytesWritten = 0;
  7. this.timestamps = [];
  8. this.timestampIndex = 0;
  9. this.startTime = 0;
  10. this.decodedTime = 0;
  11. Object.defineProperty(this, 'currentTime', {get: this.getCurrentTime});
  12. };
  13. BaseDecoder.prototype.destroy = function() {};
  14. BaseDecoder.prototype.connect = function(destination) {
  15. this.destination = destination;
  16. };
  17. BaseDecoder.prototype.bufferGetIndex = function() {
  18. return this.bits.index;
  19. };
  20. BaseDecoder.prototype.bufferSetIndex = function(index) {
  21. this.bits.index = index;
  22. };
  23. BaseDecoder.prototype.bufferWrite = function(buffers) {
  24. return this.bits.write(buffers);
  25. };
  26. BaseDecoder.prototype.write = function(pts, buffers) {
  27. if (this.collectTimestamps) {
  28. if (this.timestamps.length === 0) {
  29. this.startTime = pts;
  30. this.decodedTime = pts;
  31. }
  32. this.timestamps.push({index: this.bytesWritten << 3, time: pts});
  33. }
  34. this.bytesWritten += this.bufferWrite(buffers);
  35. this.canPlay = true;
  36. };
  37. BaseDecoder.prototype.seek = function(time) {
  38. if (!this.collectTimestamps) {
  39. return;
  40. }
  41. this.timestampIndex = 0;
  42. for (var i = 0; i < this.timestamps.length; i++) {
  43. if (this.timestamps[i].time > time) {
  44. break;
  45. }
  46. this.timestampIndex = i;
  47. }
  48. var ts = this.timestamps[this.timestampIndex];
  49. if (ts) {
  50. this.bufferSetIndex(ts.index);
  51. this.decodedTime = ts.time;
  52. }
  53. else {
  54. this.bufferSetIndex(0);
  55. this.decodedTime = this.startTime;
  56. }
  57. };
  58. BaseDecoder.prototype.decode = function() {
  59. this.advanceDecodedTime(0);
  60. };
  61. BaseDecoder.prototype.advanceDecodedTime = function(seconds) {
  62. if (this.collectTimestamps) {
  63. var newTimestampIndex = -1;
  64. var currentIndex = this.bufferGetIndex();
  65. for (var i = this.timestampIndex; i < this.timestamps.length; i++) {
  66. if (this.timestamps[i].index > currentIndex) {
  67. break;
  68. }
  69. newTimestampIndex = i;
  70. }
  71. // Did we find a new PTS, different from the last? If so, we don't have
  72. // to advance the decoded time manually and can instead sync it exactly
  73. // to the PTS.
  74. if (
  75. newTimestampIndex !== -1 &&
  76. newTimestampIndex !== this.timestampIndex
  77. ) {
  78. this.timestampIndex = newTimestampIndex;
  79. this.decodedTime = this.timestamps[this.timestampIndex].time;
  80. return;
  81. }
  82. }
  83. this.decodedTime += seconds;
  84. };
  85. BaseDecoder.prototype.getCurrentTime = function() {
  86. return this.decodedTime;
  87. };
  88. return BaseDecoder;
  89. })();