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. /**
  8. * @summary A set of BufferSource utility functions.
  9. * @export
  10. */
  11. shaka.util.BufferUtils = class {
  12. /**
  13. * Compare two buffers for equality. For buffers of different types, this
  14. * compares the underlying buffers as binary data.
  15. *
  16. * @param {?BufferSource} arr1
  17. * @param {?BufferSource} arr2
  18. * @return {boolean}
  19. * @export
  20. * @suppress {strictMissingProperties}
  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 (let i = 0; i < arr1.byteLength; i++) {
  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 (!ArrayBuffer.isView(view)) {
  60. return /** @type {!ArrayBuffer} */(view);
  61. } else {
  62. return /** @type {!ArrayBufferView} */(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 (!ArrayBuffer.isView(view)) {
  76. return /** @type {!ArrayBuffer} */(view);
  77. } else {
  78. const aView = /** @type {!ArrayBufferView} */(view);
  79. if (aView.byteOffset == 0 &&
  80. aView.byteLength == aView.buffer.byteLength) {
  81. // This is a TypedArray over the whole buffer.
  82. return aView.buffer;
  83. }
  84. // This is a "view" on the buffer. Create a new buffer that only contains
  85. // the data. Note that since this isn't an ArrayBuffer, the "new" call
  86. // will allocate a new buffer to hold the copy.
  87. return new Uint8Array(aView).buffer;
  88. }
  89. }
  90. /**
  91. * Creates a new Uint8Array view on the same buffer. This clamps the values
  92. * to be within the same view (i.e. you can't use this to move past the end
  93. * of the view, even if the underlying buffer is larger). However, you can
  94. * pass a negative offset to access the data before the view.
  95. *
  96. * @param {BufferSource} data
  97. * @param {number=} offset The offset from the beginning of this data's view
  98. * to start the new view at.
  99. * @param {number=} length The byte length of the new view.
  100. * @return {!Uint8Array}
  101. * @export
  102. */
  103. static toUint8(data, offset = 0, length = Infinity) {
  104. return shaka.util.BufferUtils.view_(data, offset, length, Uint8Array);
  105. }
  106. /**
  107. * Creates a new Uint16Array view on the same buffer. This clamps the values
  108. * to be within the same view (i.e. you can't use this to move past the end
  109. * of the view, even if the underlying buffer is larger). However, you can
  110. * pass a negative offset to access the data before the view.
  111. *
  112. * @param {BufferSource} data
  113. * @param {number=} offset The offset from the beginning of this data's view
  114. * to start the new view at.
  115. * @param {number=} length The byte length of the new view.
  116. * @return {!Uint16Array}
  117. * @export
  118. */
  119. static toUint16(data, offset = 0, length = Infinity) {
  120. return shaka.util.BufferUtils.view_(data, offset, length, Uint16Array);
  121. }
  122. /**
  123. * Creates a DataView over the given buffer.
  124. *
  125. * @see toUint8
  126. * @param {BufferSource} buffer
  127. * @param {number=} offset
  128. * @param {number=} length
  129. * @return {!DataView}
  130. * @export
  131. */
  132. static toDataView(buffer, offset = 0, length = Infinity) {
  133. return shaka.util.BufferUtils.view_(buffer, offset, length, DataView);
  134. }
  135. /**
  136. * @param {BufferSource} data
  137. * @param {number} offset
  138. * @param {number} length
  139. * @param {function(new:T, ArrayBuffer, number, number)} Type
  140. * @return {!T}
  141. * @template T
  142. * @private
  143. */
  144. static view_(data, offset, length, Type) {
  145. const buffer = shaka.util.BufferUtils.unsafeGetArrayBuffer_(data);
  146. let bytesPerElement = 1;
  147. if ('BYTES_PER_ELEMENT' in Type) {
  148. bytesPerElement = Type.BYTES_PER_ELEMENT;
  149. }
  150. // Absolute end of the |data| view within |buffer|.
  151. /** @suppress {strictMissingProperties} */
  152. const dataEnd = ((data.byteOffset || 0) + data.byteLength) /
  153. bytesPerElement;
  154. // Absolute start of the result within |buffer|.
  155. /** @suppress {strictMissingProperties} */
  156. const rawStart = ((data.byteOffset || 0) + offset) / bytesPerElement;
  157. const start = Math.floor(Math.max(0, Math.min(rawStart, dataEnd)));
  158. // Absolute end of the result within |buffer|.
  159. const end = Math.floor(Math.min(start + Math.max(length, 0), dataEnd));
  160. return new Type(buffer, start, end - start);
  161. }
  162. };