Source: lib/util/buffer_utils.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.util.BufferUtils');
  7. goog.require('shaka.util.Iterables');
  8. /**
  9. * @summary A set of BufferSource utility functions.
  10. * @export
  11. */
  12. shaka.util.BufferUtils = class {
  13. /**
  14. * Compare two buffers for equality. For buffers of different types, this
  15. * compares the underlying buffers as binary data.
  16. *
  17. * @param {?BufferSource} arr1
  18. * @param {?BufferSource} arr2
  19. * @return {boolean}
  20. * @export
  21. */
  22. static equal(arr1, arr2) {
  23. const BufferUtils = shaka.util.BufferUtils;
  24. if (!arr1 && !arr2) {
  25. return true;
  26. }
  27. if (!arr1 || !arr2) {
  28. return false;
  29. }
  30. if (arr1.byteLength != arr2.byteLength) {
  31. return false;
  32. }
  33. // Quickly check if these are views of the same buffer. An ArrayBuffer can
  34. // be passed but doesn't have a byteOffset field, so default to 0.
  35. if (BufferUtils.unsafeGetArrayBuffer_(arr1) ==
  36. BufferUtils.unsafeGetArrayBuffer_(arr2) &&
  37. (arr1.byteOffset || 0) == (arr2.byteOffset || 0)) {
  38. return true;
  39. }
  40. const uint8A = shaka.util.BufferUtils.toUint8(arr1);
  41. const uint8B = shaka.util.BufferUtils.toUint8(arr2);
  42. for (const i of shaka.util.Iterables.range(arr1.byteLength)) {
  43. if (uint8A[i] != uint8B[i]) {
  44. return false;
  45. }
  46. }
  47. return true;
  48. }
  49. /**
  50. * Gets the underlying ArrayBuffer of the given view. The caller needs to
  51. * ensure it uses the "byteOffset" and "byteLength" fields of the view to
  52. * only use the same "view" of the data.
  53. *
  54. * @param {BufferSource} view
  55. * @return {!ArrayBuffer}
  56. * @private
  57. */
  58. static unsafeGetArrayBuffer_(view) {
  59. if (view instanceof ArrayBuffer) {
  60. return view;
  61. } else {
  62. return view.buffer;
  63. }
  64. }
  65. /**
  66. * Gets an ArrayBuffer that contains the data from the given TypedArray. Note
  67. * this will allocate a new ArrayBuffer if the object is a partial view of
  68. * the data.
  69. *
  70. * @param {!BufferSource} view
  71. * @return {!ArrayBuffer}
  72. * @export
  73. */
  74. static toArrayBuffer(view) {
  75. if (view instanceof ArrayBuffer) {
  76. return view;
  77. } else {
  78. if (view.byteOffset == 0 && view.byteLength == view.buffer.byteLength) {
  79. // This is a TypedArray over the whole buffer.
  80. return view.buffer;
  81. }
  82. // This is a "view" on the buffer. Create a new buffer that only contains
  83. // the data. Note that since this isn't an ArrayBuffer, the "new" call
  84. // will allocate a new buffer to hold the copy.
  85. return new Uint8Array(view).buffer;
  86. }
  87. }
  88. /**
  89. * Creates a new Uint8Array view on the same buffer. This clamps the values
  90. * to be within the same view (i.e. you can't use this to move past the end
  91. * of the view, even if the underlying buffer is larger). However, you can
  92. * pass a negative offset to access the data before the view.
  93. *
  94. * @param {BufferSource} data
  95. * @param {number=} offset The offset from the beginning of this data's view
  96. * to start the new view at.
  97. * @param {number=} length The byte length of the new view.
  98. * @return {!Uint8Array}
  99. * @export
  100. */
  101. static toUint8(data, offset = 0, length = Infinity) {
  102. return shaka.util.BufferUtils.view_(data, offset, length, Uint8Array);
  103. }
  104. /**
  105. * Creates a DataView over the given buffer.
  106. *
  107. * @see toUint8
  108. * @param {BufferSource} buffer
  109. * @param {number=} offset
  110. * @param {number=} length
  111. * @return {!DataView}
  112. * @export
  113. */
  114. static toDataView(buffer, offset = 0, length = Infinity) {
  115. return shaka.util.BufferUtils.view_(buffer, offset, length, DataView);
  116. }
  117. /**
  118. * @param {BufferSource} data
  119. * @param {number} offset
  120. * @param {number} length
  121. * @param {function(new:T, ArrayBuffer, number, number)} Type
  122. * @return {!T}
  123. * @template T
  124. * @private
  125. */
  126. static view_(data, offset, length, Type) {
  127. const buffer = shaka.util.BufferUtils.unsafeGetArrayBuffer_(data);
  128. // Absolute end of the |data| view within |buffer|.
  129. const dataEnd = (data.byteOffset || 0) + data.byteLength;
  130. // Absolute start of the result within |buffer|.
  131. const rawStart = (data.byteOffset || 0) + offset;
  132. const start = Math.max(0, Math.min(rawStart, dataEnd));
  133. // Absolute end of the result within |buffer|.
  134. const end = Math.min(start + Math.max(length, 0), dataEnd);
  135. return new Type(buffer, start, end - start);
  136. }
  137. };