UNPKG

3.21 kBJavaScriptView Raw
1import { isArrayBufferView } from '@videojs/vhs-utils/es/byte-helpers';
2
3/**
4 * @file bin-utils.js
5 */
6
7/**
8 * convert a TimeRange to text
9 *
10 * @param {TimeRange} range the timerange to use for conversion
11 * @param {number} i the iterator on the range to convert
12 * @return {string} the range in string format
13 */
14const textRange = function(range, i) {
15 return range.start(i) + '-' + range.end(i);
16};
17
18/**
19 * format a number as hex string
20 *
21 * @param {number} e The number
22 * @param {number} i the iterator
23 * @return {string} the hex formatted number as a string
24 */
25const formatHexString = function(e, i) {
26 const value = e.toString(16);
27
28 return '00'.substring(0, 2 - value.length) + value + (i % 2 ? ' ' : '');
29};
30const formatAsciiString = function(e) {
31 if (e >= 0x20 && e < 0x7e) {
32 return String.fromCharCode(e);
33 }
34 return '.';
35};
36
37/**
38 * Creates an object for sending to a web worker modifying properties that are TypedArrays
39 * into a new object with seperated properties for the buffer, byteOffset, and byteLength.
40 *
41 * @param {Object} message
42 * Object of properties and values to send to the web worker
43 * @return {Object}
44 * Modified message with TypedArray values expanded
45 * @function createTransferableMessage
46 */
47export const createTransferableMessage = function(message) {
48 const transferable = {};
49
50 Object.keys(message).forEach((key) => {
51 const value = message[key];
52
53 if (isArrayBufferView(value)) {
54 transferable[key] = {
55 bytes: value.buffer,
56 byteOffset: value.byteOffset,
57 byteLength: value.byteLength
58 };
59 } else {
60 transferable[key] = value;
61 }
62 });
63
64 return transferable;
65};
66
67/**
68 * Returns a unique string identifier for a media initialization
69 * segment.
70 *
71 * @param {Object} initSegment
72 * the init segment object.
73 *
74 * @return {string} the generated init segment id
75 */
76export const initSegmentId = function(initSegment) {
77 const byterange = initSegment.byterange || {
78 length: Infinity,
79 offset: 0
80 };
81
82 return [
83 byterange.length, byterange.offset, initSegment.resolvedUri
84 ].join(',');
85};
86
87/**
88 * Returns a unique string identifier for a media segment key.
89 *
90 * @param {Object} key the encryption key
91 * @return {string} the unique id for the media segment key.
92 */
93export const segmentKeyId = function(key) {
94 return key.resolvedUri;
95};
96
97/**
98 * utils to help dump binary data to the console
99 *
100 * @param {Array|TypedArray} data
101 * data to dump to a string
102 *
103 * @return {string} the data as a hex string.
104 */
105export const hexDump = (data) => {
106 const bytes = Array.prototype.slice.call(data);
107 const step = 16;
108 let result = '';
109 let hex;
110 let ascii;
111
112 for (let j = 0; j < bytes.length / step; j++) {
113 hex = bytes.slice(j * step, j * step + step).map(formatHexString).join('');
114 ascii = bytes.slice(j * step, j * step + step).map(formatAsciiString).join('');
115 result += hex + ' ' + ascii + '\n';
116 }
117
118 return result;
119};
120
121export const tagDump = ({ bytes }) => hexDump(bytes);
122
123export const textRanges = (ranges) => {
124 let result = '';
125 let i;
126
127 for (i = 0; i < ranges.length; i++) {
128 result += textRange(ranges, i) + ' ';
129 }
130 return result;
131};