1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-04 10:19:24 +02:00
This commit is contained in:
Daniel Neto 2023-11-10 09:36:43 -03:00
parent 0e3fa24329
commit 4e95a0e94c
369 changed files with 16946 additions and 89882 deletions

View file

@ -688,18 +688,32 @@ Cea708Stream.prototype.handleText = function(i, service, options) {
var char;
var charCodeArray;
// Converts an array of bytes to a unicode hex string.
function toHexString(byteArray) {
return byteArray.map((byte) => {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('');
};
if (isMultiByte) {
charCodeArray = [currentByte, nextByte];
i++;
} else {
charCodeArray = [currentByte];
}
// Use the TextDecoder if one was created for this service
if (service.textDecoder_ && !isExtended) {
if (isMultiByte) {
charCodeArray = [currentByte, nextByte];
i++;
} else {
charCodeArray = [currentByte];
}
char = service.textDecoder_.decode(new Uint8Array(charCodeArray));
} else {
char = get708CharFromCode(extended | currentByte);
// We assume any multi-byte char without a decoder is unicode.
if (isMultiByte) {
const unicode = toHexString(charCodeArray);
// Takes a unicode hex string and creates a single character.
char = String.fromCharCode(parseInt(unicode, 16));
} else {
char = get708CharFromCode(extended | currentByte);
}
}
if (win.pendingNewLine && !win.isEmpty()) {
@ -1231,10 +1245,12 @@ var ROWS = [0x1100, 0x1120, 0x1200, 0x1220, 0x1500, 0x1520, 0x1600, 0x1620,
// CEA-608 captions are rendered onto a 34x15 matrix of character
// cells. The "bottom" row is the last element in the outer array.
// We keep track of positioning information as we go by storing the
// number of indentations and the tab offset in this buffer.
var createDisplayBuffer = function() {
var result = [], i = BOTTOM_ROW + 1;
while (i--) {
result.push('');
result.push({ text: '', indent: 0, offset: 0 });
}
return result;
};
@ -1312,9 +1328,9 @@ var Cea608Stream = function(field, dataChannel) {
} else if (data === this.BACKSPACE_) {
if (this.mode_ === 'popOn') {
this.nonDisplayed_[this.row_] = this.nonDisplayed_[this.row_].slice(0, -1);
this.nonDisplayed_[this.row_].text = this.nonDisplayed_[this.row_].text.slice(0, -1);
} else {
this.displayed_[this.row_] = this.displayed_[this.row_].slice(0, -1);
this.displayed_[this.row_].text = this.displayed_[this.row_].text.slice(0, -1);
}
} else if (data === this.ERASE_DISPLAYED_MEMORY_) {
this.flushDisplayed(packet.pts);
@ -1352,9 +1368,9 @@ var Cea608Stream = function(field, dataChannel) {
// Delete the previous character
if (this.mode_ === 'popOn') {
this.nonDisplayed_[this.row_] = this.nonDisplayed_[this.row_].slice(0, -1);
this.nonDisplayed_[this.row_].text = this.nonDisplayed_[this.row_].text.slice(0, -1);
} else {
this.displayed_[this.row_] = this.displayed_[this.row_].slice(0, -1);
this.displayed_[this.row_].text = this.displayed_[this.row_].text.slice(0, -1);
}
// Bitmask char0 so that we can apply character transformations
@ -1390,7 +1406,13 @@ var Cea608Stream = function(field, dataChannel) {
// increments, with an additional offset code of 1-3 to reach any
// of the 32 columns specified by CEA-608. So all we need to do
// here is increment the column cursor by the given offset.
this.column_ += (char1 & 0x03);
const offset = (char1 & 0x03);
// For an offest value 1-3, set the offset for that caption
// in the non-displayed array.
this.nonDisplayed_[this.row_].offset = offset;
this.column_ += offset;
// Detect PACs (Preamble Address Codes)
} else if (this.isPAC(char0, char1)) {
@ -1427,7 +1449,11 @@ var Cea608Stream = function(field, dataChannel) {
// increments the column cursor by 4, so we can get the desired
// column position by bit-shifting to the right (to get n/2)
// and multiplying by 4.
this.column_ = ((data & 0xe) >> 1) * 4;
const indentations = ((data & 0xe) >> 1);
this.column_ = indentations * 4;
// add to the number of indentations for positioning
this.nonDisplayed_[this.row_].indent += indentations;
}
if (this.isColorPAC(char1)) {
@ -1458,32 +1484,51 @@ Cea608Stream.prototype = new Stream();
// Trigger a cue point that captures the current state of the
// display buffer
Cea608Stream.prototype.flushDisplayed = function(pts) {
var content = this.displayed_
// remove spaces from the start and end of the string
.map(function(row, index) {
const logWarning = (index) => {
this.trigger('log', {
level: 'warn',
message: 'Skipping a malformed 608 caption at index ' + index + '.'
});
};
const content = [];
this.displayed_.forEach((row, i) => {
if (row && row.text && row.text.length) {
try {
return row.trim();
// remove spaces from the start and end of the string
row.text = row.text.trim();
} catch (e) {
// Ordinarily, this shouldn't happen. However, caption
// parsing errors should not throw exceptions and
// break playback.
this.trigger('log', {
level: 'warn',
message: 'Skipping a malformed 608 caption at index ' + index + '.'
});
return '';
logWarning(i);
}
}, this)
// combine all text rows to display in one cue
.join('\n')
// and remove blank rows from the start and end, but not the middle
.replace(/^\n+|\n+$/g, '');
// See the below link for more details on the following fields:
// https://dvcs.w3.org/hg/text-tracks/raw-file/default/608toVTT/608toVTT.html#positioning-in-cea-608
if (row.text.length) {
content.push({
// The text to be displayed in the caption from this specific row, with whitespace removed.
text: row.text,
// Value between 1 and 15 representing the PAC row used to calculate line height.
line: i + 1,
// A number representing the indent position by percentage (CEA-608 PAC indent code).
// The value will be a number between 10 and 80. Offset is used to add an aditional
// value to the position if necessary.
position: 10 + Math.min(70, row.indent * 10) + (row.offset * 2.5),
});
}
}
else if (row === undefined || row === null) {
logWarning(i);
}
});
if (content.length) {
this.trigger('data', {
startPts: this.startPts_,
endPts: pts,
text: content,
content,
stream: this.name_
});
}
@ -1686,7 +1731,7 @@ Cea608Stream.prototype.setRollUp = function(pts, newBaseRow) {
// move currently displayed captions (up or down) to the new base row
for (var i = 0; i < this.rollUpRows_; i++) {
this.displayed_[newBaseRow - i] = this.displayed_[this.row_ - i];
this.displayed_[this.row_ - i] = '';
this.displayed_[this.row_ - i] = { text: '', indent: 0, offset: 0 };
}
}
@ -1722,18 +1767,18 @@ Cea608Stream.prototype.clearFormatting = function(pts) {
// Mode Implementations
Cea608Stream.prototype.popOn = function(pts, text) {
var baseRow = this.nonDisplayed_[this.row_];
var baseRow = this.nonDisplayed_[this.row_].text;
// buffer characters
baseRow += text;
this.nonDisplayed_[this.row_] = baseRow;
this.nonDisplayed_[this.row_].text = baseRow;
};
Cea608Stream.prototype.rollUp = function(pts, text) {
var baseRow = this.displayed_[this.row_];
var baseRow = this.displayed_[this.row_].text;
baseRow += text;
this.displayed_[this.row_] = baseRow;
this.displayed_[this.row_].text = baseRow;
};
@ -1741,24 +1786,24 @@ Cea608Stream.prototype.shiftRowsUp_ = function() {
var i;
// clear out inactive rows
for (i = 0; i < this.topRow_; i++) {
this.displayed_[i] = '';
this.displayed_[i] = { text: '', indent: 0, offset: 0 };
}
for (i = this.row_ + 1; i < BOTTOM_ROW + 1; i++) {
this.displayed_[i] = '';
this.displayed_[i] = { text: '', indent: 0, offset: 0 };
}
// shift displayed rows up
for (i = this.topRow_; i < this.row_; i++) {
this.displayed_[i] = this.displayed_[i + 1];
}
// clear out the bottom row
this.displayed_[this.row_] = '';
this.displayed_[this.row_] = { text: '', indent: 0, offset: 0 };
};
Cea608Stream.prototype.paintOn = function(pts, text) {
var baseRow = this.displayed_[this.row_];
var baseRow = this.displayed_[this.row_].text;
baseRow += text;
this.displayed_[this.row_] = baseRow;
this.displayed_[this.row_].text = baseRow;
};
// exports