mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-15 16:06:41 +03:00
562a2d567b
This enables the eslint rule requiring jsdocs on all class
declarations, function declarations, and methods.
Unfortunately, there are two problems with this:
1. We don't use class _declarations_, we use class _expressions_,
which are not covered by this rule. So it does not enforce jsdoc at
the class level.
2. We tend to document a class at the class-level, rather than at the
constructor. But a constructor counts as a method for eslint, so it
requires docs on the constructor. There is no way to configure it to
make an exception for trivial constructors.
So for all trivial (no-argument) constructors, we add empty jsdocs:
/** */
constructor() {
This was quicker and easier than setting up some alternative plugin in
eslint to make an exception for us.
The good news is that this rule caught several undocumented parameters
and places where the jsdoc comment was malformed. So fixing those
also improves the compiler's ability to enforce types.
Change-Id: Icbc46ed690c94e53d354648a883119524f8fca45
112 lines
2.3 KiB
JavaScript
112 lines
2.3 KiB
JavaScript
/*! @license
|
|
* Shaka Player
|
|
* Copyright 2016 Google LLC
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
goog.provide('shaka.text.SrtTextParser');
|
|
|
|
goog.require('goog.asserts');
|
|
goog.require('shaka.text.TextEngine');
|
|
goog.require('shaka.text.VttTextParser');
|
|
goog.require('shaka.util.BufferUtils');
|
|
goog.require('shaka.util.StringUtils');
|
|
|
|
|
|
/**
|
|
* @implements {shaka.extern.TextParser}
|
|
* @export
|
|
*/
|
|
shaka.text.SrtTextParser = class {
|
|
/** */
|
|
constructor() {
|
|
/**
|
|
* @type {!shaka.extern.TextParser}
|
|
* @private
|
|
*/
|
|
this.parser_ = new shaka.text.VttTextParser();
|
|
}
|
|
|
|
/**
|
|
* @override
|
|
* @export
|
|
*/
|
|
parseInit(data) {
|
|
goog.asserts.assert(false, 'SRT does not have init segments');
|
|
}
|
|
|
|
/**
|
|
* @override
|
|
* @export
|
|
*/
|
|
parseMedia(data, time) {
|
|
const SrtTextParser = shaka.text.SrtTextParser;
|
|
const BufferUtils = shaka.util.BufferUtils;
|
|
const StringUtils = shaka.util.StringUtils;
|
|
|
|
// Get the input as a string.
|
|
const str = StringUtils.fromUTF8(data);
|
|
|
|
const vvtText = SrtTextParser.srt2webvtt(str);
|
|
|
|
const newData = BufferUtils.toUint8(StringUtils.toUTF8(vvtText));
|
|
|
|
return this.parser_.parseMedia(newData, time);
|
|
}
|
|
|
|
/**
|
|
* Convert a SRT format to WebVTT
|
|
*
|
|
* @param {!string} data
|
|
* @return {!string}
|
|
* @export
|
|
*/
|
|
static srt2webvtt(data) {
|
|
const SrtTextParser = shaka.text.SrtTextParser;
|
|
let result = 'WEBVTT\n\n';
|
|
|
|
// Supports no cues
|
|
if (data == '') {
|
|
return result;
|
|
}
|
|
|
|
// remove dos newlines
|
|
let srt = data.replace(/\r+/g, '');
|
|
// trim white space start and end
|
|
srt = srt.trim();
|
|
|
|
// get cues
|
|
const cuelist = srt.split('\n\n');
|
|
for (const cue of cuelist) {
|
|
result += SrtTextParser.convertSrtCue_(cue);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Convert a SRT cue into WebVTT cue
|
|
*
|
|
* @param {!string} caption
|
|
* @return {!string}
|
|
* @private
|
|
*/
|
|
static convertSrtCue_(caption) {
|
|
const lines = caption.split(/\n/);
|
|
|
|
// detect and skip numeric identifier
|
|
if (lines[0].match(/\d+/)) {
|
|
lines.shift();
|
|
}
|
|
|
|
// convert time codes
|
|
lines[0] = lines[0].replace(/,/g, '.');
|
|
|
|
return lines.join('\n') + '\n\n';
|
|
}
|
|
};
|
|
|
|
|
|
shaka.text.TextEngine.registerParser(
|
|
'text/srt', () => new shaka.text.SrtTextParser());
|