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 (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 new Uint16Array view on the same buffer. This clamps the values
  106. * to be within the same view (i.e. you can't use this to move past the end
  107. * of the view, even if the underlying buffer is larger). However, you can
  108. * pass a negative offset to access the data before the view.
  109. *
  110. * @param {BufferSource} data
  111. * @param {number=} offset The offset from the beginning of this data's view
  112. * to start the new view at.
  113. * @param {number=} length The byte length of the new view.
  114. * @return {!Uint16Array}
  115. * @export
  116. */
  117. static toUint16(data, offset = 0, length = Infinity) {
  118. return shaka.util.BufferUtils.view_(data, offset, length, Uint16Array);
  119. }
  120. /**
  121. * Creates a DataView over the given buffer.
  122. *
  123. * @see toUint8
  124. * @param {BufferSource} buffer
  125. * @param {number=} offset
  126. * @param {number=} length
  127. * @return {!DataView}
  128. * @export
  129. */
  130. static toDataView(buffer, offset = 0, length = Infinity) {
  131. return shaka.util.BufferUtils.view_(buffer, offset, length, DataView);
  132. }
  133. /**
  134. * @param {BufferSource} data
  135. * @param {number} offset
  136. * @param {number} length
  137. * @param {function(new:T, ArrayBuffer, number, number)} Type
  138. * @return {!T}
  139. * @template T
  140. * @private
  141. */
  142. static view_(data, offset, length, Type) {
  143. const buffer = shaka.util.BufferUtils.unsafeGetArrayBuffer_(data);
  144. let bytesPerElement = 1;
  145. if ('BYTES_PER_ELEMENT' in Type) {
  146. bytesPerElement = Type.BYTES_PER_ELEMENT;
  147. }
  148. // Absolute end of the |data| view within |buffer|.
  149. /** @suppress {strictMissingProperties} */
  150. const dataEnd = ((data.byteOffset || 0) + data.byteLength) /
  151. bytesPerElement;
  152. // Absolute start of the result within |buffer|.
  153. /** @suppress {strictMissingProperties} */
  154. const rawStart = ((data.byteOffset || 0) + offset) / bytesPerElement;
  155. const start = Math.floor(Math.max(0, Math.min(rawStart, dataEnd)));
  156. // Absolute end of the result within |buffer|.
  157. const end = Math.floor(Math.min(start + Math.max(length, 0), dataEnd));
  158. return new Type(buffer, start, end - start);
  159. }
  160. };