*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class SPSParser {
static _ebsp2rbsp(uint8array) {
let src = uint8array;
let src_length = src.byteLength;
let dst = new Uint8Array(src_length);
let dst_idx = 0;
for (let i = 0; i < src_length; i++) {
if (i >= 2) {
// Unescape: Skip 0x03 after 00 00
if (src[i] === 0x03 && src[i - 1] === 0x00 && src[i - 2] === 0x00) {
continue;
}
}
dst[dst_idx] = src[i];
dst_idx++;
}
return new Uint8Array(dst.buffer, 0, dst_idx);
}
// 解析 SPS
// https://zhuanlan.zhihu.com/p/27896239
static parseSPS(uint8array) {
let rbsp = SPSParser._ebsp2rbsp(uint8array);
let gb = new ExpGolomb$1(rbsp);
gb.readByte();
// 标识当前H.264码流的profile。
// 我们知道,H.264中定义了三种常用的档次profile: 基准档次:baseline profile;主要档次:main profile; 扩展档次:extended profile;
let profile_idc = gb.readByte(); // profile_idc
gb.readByte(); // constraint_set_flags[5] + reserved_zero[3]
// 标识当前码流的Level。编码的Level定义了某种条件下的最大视频分辨率、最大视频帧率等参数,码流所遵从的level由level_idc指定。
let level_idc = gb.readByte(); // level_idc
// 表示当前的序列参数集的id。通过该id值,图像参数集pps可以引用其代表的sps中的参数。
gb.readUEG(); // seq_parameter_set_id
let profile_string = SPSParser.getProfileString(profile_idc);
let level_string = SPSParser.getLevelString(level_idc);
let chroma_format_idc = 1;
let chroma_format = 420;
let chroma_format_table = [0, 420, 422, 444];
let bit_depth = 8;
//
if (profile_idc === 100 || profile_idc === 110 || profile_idc === 122 || profile_idc === 244 || profile_idc === 44 || profile_idc === 83 || profile_idc === 86 || profile_idc === 118 || profile_idc === 128 || profile_idc === 138 || profile_idc === 144) {
//
chroma_format_idc = gb.readUEG();
if (chroma_format_idc === 3) {
gb.readBits(1); // separate_colour_plane_flag
}
if (chroma_format_idc <= 3) {
chroma_format = chroma_format_table[chroma_format_idc];
}
bit_depth = gb.readUEG() + 8; // bit_depth_luma_minus8
gb.readUEG(); // bit_depth_chroma_minus8
gb.readBits(1); // qpprime_y_zero_transform_bypass_flag
if (gb.readBool()) {
// seq_scaling_matrix_present_flag
let scaling_list_count = chroma_format_idc !== 3 ? 8 : 12;
for (let i = 0; i < scaling_list_count; i++) {
if (gb.readBool()) {
// seq_scaling_list_present_flag
if (i < 6) {
SPSParser._skipScalingList(gb, 16);
} else {
SPSParser._skipScalingList(gb, 64);
}
}
}
}
}
// 用于计算MaxFrameNum的值。计算公式为MaxFrameNum = 2^(log2_max_frame_num_minus4 +
gb.readUEG(); // log2_max_frame_num_minus4
// 表示解码picture order count(POC)的方法。POC是另一种计量图像序号的方式,与frame_num有着不同的计算方法。该语法元素的取值为0、1或2。
let pic_order_cnt_type = gb.readUEG();
if (pic_order_cnt_type === 0) {
gb.readUEG(); // log2_max_pic_order_cnt_lsb_minus_4
} else if (pic_order_cnt_type === 1) {
gb.readBits(1); // delta_pic_order_always_zero_flag
gb.readSEG(); // offset_for_non_ref_pic
gb.readSEG(); // offset_for_top_to_bottom_field
let num_ref_frames_in_pic_order_cnt_cycle = gb.readUEG();
for (let i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++) {
gb.readSEG(); // offset_for_ref_frame
}
}
// 用于表示参考帧的最大数目。
let ref_frames = gb.readUEG(); // max_num_ref_frames
// 标识位,说明frame_num中是否允许不连续的值。
gb.readBits(1); // gaps_in_frame_num_value_allowed_flag
// 用于计算图像的宽度。单位为宏块个数,因此图像的实际宽度为:
let pic_width_in_mbs_minus1 = gb.readUEG();
// 使用PicHeightInMapUnits来度量视频中一帧图像的高度。
// PicHeightInMapUnits并非图像明确的以像素或宏块为单位的高度,而需要考虑该宏块是帧编码或场编码。PicHeightInMapUnits的计算方式为:
let pic_height_in_map_units_minus1 = gb.readUEG();
// 标识位,说明宏块的编码方式。当该标识位为0时,宏块可能为帧编码或场编码;
// 该标识位为1时,所有宏块都采用帧编码。根据该标识位取值不同,PicHeightInMapUnits的含义也不同,
// 为0时表示一场数据按宏块计算的高度,为1时表示一帧数据按宏块计算的高度。
let frame_mbs_only_flag = gb.readBits(1);
if (frame_mbs_only_flag === 0) {
// 标识位,说明是否采用了宏块级的帧场自适应编码。当该标识位为0时,不存在帧编码和场编码之间的切换;当标识位为1时,宏块可能在帧编码和场编码模式之间进行选择。
gb.readBits(1); // mb_adaptive_frame_field_flag
}
// 标识位,用于B_Skip、B_Direct模式运动矢量的推导计算。
gb.readBits(1); // direct_8x8_inference_flag
let frame_crop_left_offset = 0;
let frame_crop_right_offset = 0;
let frame_crop_top_offset = 0;
let frame_crop_bottom_offset = 0;
let frame_cropping_flag = gb.readBool();
if (frame_cropping_flag) {
frame_crop_left_offset = gb.readUEG();
frame_crop_right_offset = gb.readUEG();
frame_crop_top_offset = gb.readUEG();
frame_crop_bottom_offset = gb.readUEG();
}
let sar_width = 1,
sar_height = 1;
let fps = 0,
fps_fixed = true,
fps_num = 0,
fps_den = 0;
// 标识位,说明SPS中是否存在VUI信息。
let vui_parameters_present_flag = gb.readBool();
if (vui_parameters_present_flag) {
if (gb.readBool()) {
// aspect_ratio_info_present_flag
let aspect_ratio_idc = gb.readByte();
let sar_w_table = [1, 12, 10, 16, 40, 24, 20, 32, 80, 18, 15, 64, 160, 4, 3, 2];
let sar_h_table = [1, 11, 11, 11, 33, 11, 11, 11, 33, 11, 11, 33, 99, 3, 2, 1];
if (aspect_ratio_idc > 0 && aspect_ratio_idc < 16) {
sar_width = sar_w_table[aspect_ratio_idc - 1];
sar_height = sar_h_table[aspect_ratio_idc - 1];
} else if (aspect_ratio_idc === 255) {
sar_width = gb.readByte() << 8 | gb.readByte();
sar_height = gb.readByte() << 8 | gb.readByte();
}
}
if (gb.readBool()) {
// overscan_info_present_flag
gb.readBool(); // overscan_appropriate_flag
}
if (gb.readBool()) {
// video_signal_type_present_flag
gb.readBits(4); // video_format & video_full_range_flag
if (gb.readBool()) {
// colour_description_present_flag
gb.readBits(24); // colour_primaries & transfer_characteristics & matrix_coefficients
}
}
if (gb.readBool()) {
// chroma_loc_info_present_flag
gb.readUEG(); // chroma_sample_loc_type_top_field
gb.readUEG(); // chroma_sample_loc_type_bottom_field
}
if (gb.readBool()) {
// timing_info_present_flag
let num_units_in_tick = gb.readBits(32);
let time_scale = gb.readBits(32);
fps_fixed = gb.readBool(); // fixed_frame_rate_flag
fps_num = time_scale;
fps_den = num_units_in_tick * 2;
fps = fps_num / fps_den;
}
}
let sarScale = 1;
if (sar_width !== 1 || sar_height !== 1) {
sarScale = sar_width / sar_height;
}
let crop_unit_x = 0,
crop_unit_y = 0;
if (chroma_format_idc === 0) {
crop_unit_x = 1;
crop_unit_y = 2 - frame_mbs_only_flag;
} else {
let sub_wc = chroma_format_idc === 3 ? 1 : 2;
let sub_hc = chroma_format_idc === 1 ? 2 : 1;
crop_unit_x = sub_wc;
crop_unit_y = sub_hc * (2 - frame_mbs_only_flag);
}
let codec_width = (pic_width_in_mbs_minus1 + 1) * 16;
let codec_height = (2 - frame_mbs_only_flag) * ((pic_height_in_map_units_minus1 + 1) * 16);
codec_width -= (frame_crop_left_offset + frame_crop_right_offset) * crop_unit_x;
codec_height -= (frame_crop_top_offset + frame_crop_bottom_offset) * crop_unit_y;
let present_width = Math.ceil(codec_width * sarScale);
gb.destroy();
gb = null;
// 解析出来的SPS 内容。
return {
profile_string: profile_string,
// baseline, high, high10, ...
level_string: level_string,
// 3, 3.1, 4, 4.1, 5, 5.1, ...
bit_depth: bit_depth,
// 8bit, 10bit, ...
ref_frames: ref_frames,
chroma_format: chroma_format,
// 4:2:0, 4:2:2, ...
chroma_format_string: SPSParser.getChromaFormatString(chroma_format),
frame_rate: {
fixed: fps_fixed,
fps: fps,
fps_den: fps_den,
fps_num: fps_num
},
sar_ratio: {
width: sar_width,
height: sar_height
},
codec_size: {
width: codec_width,
height: codec_height
},
present_size: {
width: present_width,
height: codec_height
}
};
}
static parseSPS$2(uint8array) {
let codec_array = uint8array.subarray(1, 4);
let codec_mimetype = 'avc1.';
for (let j = 0; j < 3; j++) {
let h = codec_array[j].toString(16);
if (h.length < 2) {
h = '0' + h;
}
codec_mimetype += h;
}
let rbsp = SPSParser._ebsp2rbsp(uint8array);
let gb = new ExpGolomb$1(rbsp);
gb.readByte();
let profile_idc = gb.readByte(); // profile_idc
gb.readByte(); // constraint_set_flags[5] + reserved_zero[3]
let level_idc = gb.readByte(); // level_idc
gb.readUEG(); // seq_parameter_set_id
let profile_string = SPSParser.getProfileString(profile_idc);
let level_string = SPSParser.getLevelString(level_idc);
let chroma_format_idc = 1;
let chroma_format = 420;
let chroma_format_table = [0, 420, 422, 444];
let bit_depth_luma = 8;
let bit_depth_chroma = 8;
if (profile_idc === 100 || profile_idc === 110 || profile_idc === 122 || profile_idc === 244 || profile_idc === 44 || profile_idc === 83 || profile_idc === 86 || profile_idc === 118 || profile_idc === 128 || profile_idc === 138 || profile_idc === 144) {
chroma_format_idc = gb.readUEG();
if (chroma_format_idc === 3) {
gb.readBits(1); // separate_colour_plane_flag
}
if (chroma_format_idc <= 3) {
chroma_format = chroma_format_table[chroma_format_idc];
}
bit_depth_luma = gb.readUEG() + 8; // bit_depth_luma_minus8
bit_depth_chroma = gb.readUEG() + 8; // bit_depth_chroma_minus8
gb.readBits(1); // qpprime_y_zero_transform_bypass_flag
if (gb.readBool()) {
// seq_scaling_matrix_present_flag
let scaling_list_count = chroma_format_idc !== 3 ? 8 : 12;
for (let i = 0; i < scaling_list_count; i++) {
if (gb.readBool()) {
// seq_scaling_list_present_flag
if (i < 6) {
SPSParser._skipScalingList(gb, 16);
} else {
SPSParser._skipScalingList(gb, 64);
}
}
}
}
}
gb.readUEG(); // log2_max_frame_num_minus4
let pic_order_cnt_type = gb.readUEG();
if (pic_order_cnt_type === 0) {
gb.readUEG(); // log2_max_pic_order_cnt_lsb_minus_4
} else if (pic_order_cnt_type === 1) {
gb.readBits(1); // delta_pic_order_always_zero_flag
gb.readSEG(); // offset_for_non_ref_pic
gb.readSEG(); // offset_for_top_to_bottom_field
let num_ref_frames_in_pic_order_cnt_cycle = gb.readUEG();
for (let i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++) {
gb.readSEG(); // offset_for_ref_frame
}
}
let ref_frames = gb.readUEG(); // max_num_ref_frames
gb.readBits(1); // gaps_in_frame_num_value_allowed_flag
let pic_width_in_mbs_minus1 = gb.readUEG();
let pic_height_in_map_units_minus1 = gb.readUEG();
let frame_mbs_only_flag = gb.readBits(1);
if (frame_mbs_only_flag === 0) {
gb.readBits(1); // mb_adaptive_frame_field_flag
}
gb.readBits(1); // direct_8x8_inference_flag
let frame_crop_left_offset = 0;
let frame_crop_right_offset = 0;
let frame_crop_top_offset = 0;
let frame_crop_bottom_offset = 0;
let frame_cropping_flag = gb.readBool();
if (frame_cropping_flag) {
frame_crop_left_offset = gb.readUEG();
frame_crop_right_offset = gb.readUEG();
frame_crop_top_offset = gb.readUEG();
frame_crop_bottom_offset = gb.readUEG();
}
let sar_width = 1,
sar_height = 1;
let fps = 0,
fps_fixed = true,
fps_num = 0,
fps_den = 0;
let vui_parameters_present_flag = gb.readBool();
if (vui_parameters_present_flag) {
if (gb.readBool()) {
// aspect_ratio_info_present_flag
let aspect_ratio_idc = gb.readByte();
let sar_w_table = [1, 12, 10, 16, 40, 24, 20, 32, 80, 18, 15, 64, 160, 4, 3, 2];
let sar_h_table = [1, 11, 11, 11, 33, 11, 11, 11, 33, 11, 11, 33, 99, 3, 2, 1];
if (aspect_ratio_idc > 0 && aspect_ratio_idc < 16) {
sar_width = sar_w_table[aspect_ratio_idc - 1];
sar_height = sar_h_table[aspect_ratio_idc - 1];
} else if (aspect_ratio_idc === 255) {
sar_width = gb.readByte() << 8 | gb.readByte();
sar_height = gb.readByte() << 8 | gb.readByte();
}
}
if (gb.readBool()) {
// overscan_info_present_flag
gb.readBool(); // overscan_appropriate_flag
}
if (gb.readBool()) {
// video_signal_type_present_flag
gb.readBits(4); // video_format & video_full_range_flag
if (gb.readBool()) {
// colour_description_present_flag
gb.readBits(24); // colour_primaries & transfer_characteristics & matrix_coefficients
}
}
if (gb.readBool()) {
// chroma_loc_info_present_flag
gb.readUEG(); // chroma_sample_loc_type_top_field
gb.readUEG(); // chroma_sample_loc_type_bottom_field
}
if (gb.readBool()) {
// timing_info_present_flag
let num_units_in_tick = gb.readBits(32);
let time_scale = gb.readBits(32);
fps_fixed = gb.readBool(); // fixed_frame_rate_flag
fps_num = time_scale;
fps_den = num_units_in_tick * 2;
fps = fps_num / fps_den;
}
}
let sarScale = 1;
if (sar_width !== 1 || sar_height !== 1) {
sarScale = sar_width / sar_height;
}
let crop_unit_x = 0,
crop_unit_y = 0;
if (chroma_format_idc === 0) {
crop_unit_x = 1;
crop_unit_y = 2 - frame_mbs_only_flag;
} else {
let sub_wc = chroma_format_idc === 3 ? 1 : 2;
let sub_hc = chroma_format_idc === 1 ? 2 : 1;
crop_unit_x = sub_wc;
crop_unit_y = sub_hc * (2 - frame_mbs_only_flag);
}
let codec_width = (pic_width_in_mbs_minus1 + 1) * 16;
let codec_height = (2 - frame_mbs_only_flag) * ((pic_height_in_map_units_minus1 + 1) * 16);
codec_width -= (frame_crop_left_offset + frame_crop_right_offset) * crop_unit_x;
codec_height -= (frame_crop_top_offset + frame_crop_bottom_offset) * crop_unit_y;
let present_width = Math.ceil(codec_width * sarScale);
gb.destroy();
gb = null;
return {
codec_mimetype,
profile_idc,
level_idc,
profile_string,
// baseline, high, high10, ...
level_string,
// 3, 3.1, 4, 4.1, 5, 5.1, ...
chroma_format_idc,
bit_depth: bit_depth_luma,
// 8bit, 10bit, ...
bit_depth_luma,
bit_depth_chroma,
ref_frames,
chroma_format,
// 4:2:0, 4:2:2, ...
chroma_format_string: SPSParser.getChromaFormatString(chroma_format),
frame_rate: {
fixed: fps_fixed,
fps: fps,
fps_den: fps_den,
fps_num: fps_num
},
sar_ratio: {
width: sar_width,
height: sar_height
},
codec_size: {
width: codec_width,
height: codec_height
},
present_size: {
width: present_width,
height: codec_height
}
};
}
static _skipScalingList(gb, count) {
let last_scale = 8,
next_scale = 8;
let delta_scale = 0;
for (let i = 0; i < count; i++) {
if (next_scale !== 0) {
delta_scale = gb.readSEG();
next_scale = (last_scale + delta_scale + 256) % 256;
}
last_scale = next_scale === 0 ? last_scale : next_scale;
}
}
// profile_idc = 66 → baseline profile;
// profile_idc = 77 → main profile;
// profile_idc = 88 → extended profile;
// 在新版的标准中,还包括了High、High 10、High 4:2:2、High 4:4:4、High 10 Intra、High
// 4:2:2 Intra、High 4:4:4 Intra、CAVLC 4:4:4 Intra
static getProfileString(profile_idc) {
switch (profile_idc) {
case 66:
return 'Baseline';
case 77:
return 'Main';
case 88:
return 'Extended';
case 100:
return 'High';
case 110:
return 'High10';
case 122:
return 'High422';
case 244:
return 'High444';
default:
return 'Unknown';
}
}
static getLevelString(level_idc) {
return (level_idc / 10).toFixed(1);
}
static getChromaFormatString(chroma) {
switch (chroma) {
case 420:
return '4:2:0';
case 422:
return '4:2:2';
case 444:
return '4:4:4';
default:
return 'Unknown';
}
}
}
class Bitop {
constructor(buffer) {
this.buffer = buffer;
this.buflen = buffer.length;
this.bufpos = 0;
this.bufoff = 0;
this.iserro = false;
}
read(n) {
let v = 0;
let d = 0;
while (n) {
if (n < 0 || this.bufpos >= this.buflen) {
this.iserro = true;
return 0;
}
this.iserro = false;
d = this.bufoff + n > 8 ? 8 - this.bufoff : n;
v <<= d;
v += this.buffer[this.bufpos] >> 8 - this.bufoff - d & 0xff >> 8 - d;
this.bufoff += d;
n -= d;
if (this.bufoff == 8) {
this.bufpos++;
this.bufoff = 0;
}
}
return v;
}
look(n) {
let p = this.bufpos;
let o = this.bufoff;
let v = this.read(n);
this.bufpos = p;
this.bufoff = o;
return v;
}
read_golomb() {
let n;
for (n = 0; this.read(1) === 0 && !this.iserro; n++);
return (1 << n) + this.read(n) - 1;
}
}
//
function parseAVCDecoderConfigurationRecord(arrayBuffer) {
const meta = {};
let le = function () {
let buf = new ArrayBuffer(2);
new DataView(buf).setInt16(0, 256, true); // little-endian write
return new Int16Array(buf)[0] === 256; // platform-spec read, if equal then LE
}();
const v = new DataView(arrayBuffer.buffer);
let version = v.getUint8(0); // configurationVersion
let avcProfile = v.getUint8(1); // avcProfileIndication
v.getUint8(2); // profile_compatibil
v.getUint8(3); // AVCLevelIndication
if (version !== 1 || avcProfile === 0) {
// this._onError(DemuxErrors.FORMAT_ERROR, 'Flv: Invalid AVCDecoderConfigurationRecord');
return {};
}
const _naluLengthSize = (v.getUint8(4) & 3) + 1; // lengthSizeMinusOne
if (_naluLengthSize !== 3 && _naluLengthSize !== 4) {
// holy shit!!!
// this._onError(DemuxErrors.FORMAT_ERROR, `Flv: Strange NaluLengthSizeMinusOne: ${_naluLengthSize - 1}`);
return {};
}
let spsCount = v.getUint8(5) & 31; // numOfSequenceParameterSets
if (spsCount === 0) {
// this._onError(DemuxErrors.FORMAT_ERROR, 'Flv: Invalid AVCDecoderConfigurationRecord: No SPS');
return {};
}
let offset = 6;
for (let i = 0; i < spsCount; i++) {
let len = v.getUint16(offset, !le); // sequenceParameterSetLength
offset += 2;
if (len === 0) {
continue;
}
// Notice: Nalu without startcode header (00 00 00 01)
let sps = new Uint8Array(arrayBuffer.buffer, offset, len);
offset += len;
// flv.js作者选择了自己来解析这个数据结构,也是迫不得已,因为JS环境下没有ffmpeg,解析这个结构主要是为了提取 sps和pps。虽然理论上sps允许有多个,但其实一般就一个。
// packetTtype 为 1 表示 NALU,NALU= network abstract layer unit,这是H.264的概念,网络抽象层数据单元,其实简单理解就是一帧视频数据。
// pps的信息没什么用,所以作者只实现了sps的分析器,说明作者下了很大功夫去学习264的标准,其中的Golomb解码还是挺复杂的,能解对不容易,我在PC和手机平台都是用ffmpeg去解析的。
// SPS里面包括了视频分辨率,帧率,profile level等视频重要信息。
let config = SPSParser.parseSPS(sps);
// console.log('h264 sps config',config)
if (i !== 0) {
// ignore other sps's config
continue;
}
meta.sps = sps;
meta.timescale = 1000;
meta.codecWidth = config.codec_size.width;
meta.codecHeight = config.codec_size.height;
meta.presentWidth = config.present_size.width;
meta.presentHeight = config.present_size.height;
meta.profile = config.profile_string;
meta.level = config.level_string;
meta.bitDepth = config.bit_depth;
meta.chromaFormat = config.chroma_format;
meta.sarRatio = config.sar_ratio;
meta.frameRate = config.frame_rate;
if (config.frame_rate.fixed === false || config.frame_rate.fps_num === 0 || config.frame_rate.fps_den === 0) {
meta.frameRate = {
fixed: true,
// fps: 23.976,
// fps_num: 23976,
fps: 25,
fps_num: 25000,
fps_den: 1000
};
}
let fps_den = meta.frameRate.fps_den;
let fps_num = meta.frameRate.fps_num;
meta.refSampleDuration = meta.timescale * (fps_den / fps_num);
let codecArray = sps.subarray(1, 4);
let codecString = 'avc1.';
for (let j = 0; j < 3; j++) {
let h = codecArray[j].toString(16);
if (h.length < 2) {
h = '0' + h;
}
codecString += h;
}
// codec
meta.codec = codecString;
}
let ppsCount = v.getUint8(offset); // numOfPictureParameterSets
if (ppsCount === 0) {
// this._onError(DemuxErrors.FORMAT_ERROR, 'Flv: Invalid AVCDecoderConfigurationRecord: No PPS');
return {};
}
offset++;
for (let i = 0; i < ppsCount; i++) {
let len = v.getUint16(offset, !le); // pictureParameterSetLength
offset += 2;
if (len === 0) {
continue;
}
let pps = new Uint8Array(arrayBuffer.buffer, offset, len);
// pps is useless for extracting video information
offset += len;
meta.pps = pps;
}
meta.videoType = VIDEO_ENCODE_TYPE.h264;
if (meta.sps) {
const spsLength = meta.sps.byteLength;
const spsFlag = new Uint8Array([spsLength >>> 24 & 0xFF, spsLength >>> 16 & 0xFF, spsLength >>> 8 & 0xFF, spsLength & 0xFF]);
const sps = new Uint8Array(spsLength + 4);
sps.set(spsFlag, 0);
sps.set(meta.sps, 4);
meta.sps = sps;
}
if (meta.pps) {
const ppsLength = meta.pps.byteLength;
const ppsFlag = new Uint8Array([ppsLength >>> 24 & 0xFF, ppsLength >>> 16 & 0xFF, ppsLength >>> 8 & 0xFF, ppsLength & 0xFF]);
const pps = new Uint8Array(ppsLength + 4);
pps.set(ppsFlag, 0);
pps.set(meta.pps, 4);
meta.pps = pps;
}
// meta.avcc = arrayBuffer;
return meta;
}
function avcEncoderConfigurationRecord$2(_ref2) {
let {
sps,
pps
} = _ref2;
// require Nalu without 4 byte length-header
let length = 6 + 2 + sps.byteLength + 1 + 2 + pps.byteLength;
let need_extra_fields = false;
const sps_details = SPSParser.parseSPS$2(sps);
if (sps[3] !== 66 && sps[3] !== 77 && sps[3] !== 88) {
need_extra_fields = true;
length += 4;
}
let data = new Uint8Array(length);
data[0] = 0x01; // configurationVersion
data[1] = sps[1]; // AVCProfileIndication
data[2] = sps[2]; // profile_compatibility
data[3] = sps[3]; // AVCLevelIndication
data[4] = 0xFF; // 111111 + lengthSizeMinusOne(3)
data[5] = 0xE0 | 0x01; // 111 + numOfSequenceParameterSets
let sps_length = sps.byteLength;
data[6] = sps_length >>> 8; // sequenceParameterSetLength
data[7] = sps_length & 0xFF;
let offset = 8;
data.set(sps, 8);
offset += sps_length;
data[offset] = 1; // numOfPictureParameterSets
let pps_length = pps.byteLength;
data[offset + 1] = pps_length >>> 8; // pictureParameterSetLength
data[offset + 2] = pps_length & 0xFF;
data.set(pps, offset + 3);
offset += 3 + pps_length;
if (need_extra_fields) {
data[offset] = 0xFC | sps_details.chroma_format_idc;
data[offset + 1] = 0xF8 | sps_details.bit_depth_luma - 8;
data[offset + 2] = 0xF8 | sps_details.bit_depth_chroma - 8;
data[offset + 3] = 0x00; // number of sps ext
offset += 4;
}
const prevData = [0x17, 0x00, 0x00, 0x00, 0x00];
const newData = new Uint8Array(prevData.length + data.byteLength);
newData.set(prevData, 0);
newData.set(data, prevData.length);
return newData;
}
/**
*
* @param oneNALBuffer
* @param isIframe
* @returns {Uint8Array}
*/
function avcEncoderNalePacket(oneNALBuffer, isIframe) {
// 正常发送nal
const idrBit = 0x10 | 7;
const nIdrBit = 0x20 | 7;
let tmp = [];
if (isIframe) {
tmp[0] = idrBit;
} else {
tmp[0] = nIdrBit;
}
// compositionTime
tmp[1] = 1;
tmp[2] = 0;
tmp[3] = 0;
tmp[4] = 0;
//
tmp[5] = oneNALBuffer.byteLength >> 24 & 0xff;
tmp[6] = oneNALBuffer.byteLength >> 16 & 0xff;
tmp[7] = oneNALBuffer.byteLength >> 8 & 0xff;
tmp[8] = oneNALBuffer.byteLength & 0xff;
const arrayBuffer = new Uint8Array(tmp.length + oneNALBuffer.byteLength);
arrayBuffer.set(tmp, 0);
arrayBuffer.set(oneNALBuffer, tmp.length);
return arrayBuffer;
}
function avcEncoderNalePacketNotLength(oneNALBuffer, isIframe) {
// 正常发送nal
const idrBit = 0x10 | 7;
const nIdrBit = 0x20 | 7;
let tmp = [];
if (isIframe) {
tmp[0] = idrBit;
} else {
tmp[0] = nIdrBit;
}
// compositionTime
tmp[1] = 1;
tmp[2] = 0;
tmp[3] = 0;
tmp[4] = 0;
const arrayBuffer = new Uint8Array(tmp.length + oneNALBuffer.byteLength);
arrayBuffer.set(tmp, 0);
arrayBuffer.set(oneNALBuffer, tmp.length);
return arrayBuffer;
}
/**
* (NALU类型 & 0001 1111)
* @param nalu
* @returns {number}
*/
function getAvcSeqHeadType(nalu) {
return nalu[0] & 0b0001_1111;
}
function isAvcSeqHead(type) {
return type === H264_NAL_TYPE.sps || type === H264_NAL_TYPE.pps;
}
function isHvcSEIType(type) {
return type === H264_NAL_TYPE.kSliceSEI;
}
function isNotAvcSeqHead(type) {
return !isAvcSeqHead(type) && !isHvcSEIType(type);
}
function isAvcNaluIFrame(type) {
return type === H264_NAL_TYPE.iFrame;
}
function isSameAvcNaluType(naluList) {
if (naluList.length === 0) {
return false;
}
const type = getAvcSeqHeadType(naluList[0]);
for (let i = 1; i < naluList.length; i++) {
if (type !== getAvcSeqHeadType(naluList[i])) {
return false;
}
}
return true;
}
class H264AnnexBParser {
constructor(data) {
this.data = data;
this.eofFlag = false;
this.currentStartcodeOffset = this.findNextStartCodeOffset(0);
if (this.eofFlag) {
console.error('Could not find H264 startcode until payload end!');
}
}
findNextStartCodeOffset(start_offset) {
let i = start_offset;
let data = this.data;
while (true) {
if (i + 3 >= data.byteLength) {
this.eofFlag = true;
return data.byteLength;
}
// search 00 00 00 01 or 00 00 01
let uint32 = data[i + 0] << 24 | data[i + 1] << 16 | data[i + 2] << 8 | data[i + 3];
let uint24 = data[i + 0] << 16 | data[i + 1] << 8 | data[i + 2];
if (uint32 === 0x00000001 || uint24 === 0x000001) {
return i;
} else {
i++;
}
}
}
readNextNaluPayload() {
let data = this.data;
let nalu_payload = null;
while (nalu_payload == null) {
if (this.eofFlag) {
break;
}
// offset pointed to start code
let startcode_offset = this.currentStartcodeOffset;
// nalu payload start offset
let offset = startcode_offset;
let u32 = data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3];
if (u32 === 0x00000001) {
offset += 4;
} else {
offset += 3;
}
let nalu_type = data[offset] & 0x1F;
let forbidden_bit = (data[offset] & 0x80) >>> 7;
let next_startcode_offset = this.findNextStartCodeOffset(offset);
this.currentStartcodeOffset = next_startcode_offset;
if (nalu_type >= H264_NAL_TYPE.kReserved0) {
continue;
}
if (forbidden_bit !== 0) {
// Log.e(this.TAG, `forbidden_bit near offset ${offset} should be 0 but has value ${forbidden_bit}`);
continue;
}
let payload_data = data.subarray(offset, next_startcode_offset);
nalu_payload = {
type: nalu_type,
data: payload_data
};
}
return nalu_payload;
}
}
class H264NaluAVC1 {
constructor(nalu) {
let nalu_size = nalu.data.byteLength;
this.type = nalu.type;
this.data = new Uint8Array(4 + nalu_size); // 4 byte length-header + nalu payload
let v = new DataView(this.data.buffer);
// Fill 4 byte length-header
v.setUint32(0, nalu_size);
// Copy payload
this.data.set(nalu.data, 4);
}
}
const _ebsp2rbsp = uint8array => {
let src = uint8array;
let src_length = src.byteLength;
let dst = new Uint8Array(src_length);
let dst_idx = 0;
for (let i = 0; i < src_length; i++) {
if (i >= 2) {
// Unescape: Skip 0x03 after 00 00
if (src[i] === 0x03 && src[i - 1] === 0x00 && src[i - 2] === 0x00) {
continue;
}
}
dst[dst_idx] = src[i];
dst_idx++;
}
return new Uint8Array(dst.buffer, 0, dst_idx);
};
const getLevelString = level_idc => {
return (level_idc / 30).toFixed(1);
};
const getChromaFormatString = chroma_format_idc => {
switch (chroma_format_idc) {
case 0:
return '4:0:0';
case 1:
return '4:2:0';
case 2:
return '4:2:2';
case 3:
return '4:4:4';
default:
return 'Unknown';
}
};
const parseHevcSPS = uint8array => {
let rbsp = _ebsp2rbsp(uint8array);
let gb = new ExpGolomb$1(rbsp);
/* remove NALu Header */
gb.readByte();
gb.readByte();
let left_offset = 0,
right_offset = 0,
top_offset = 0,
bottom_offset = 0;
// SPS
gb.readBits(4);
let max_sub_layers_minus1 = gb.readBits(3);
gb.readBool();
// profile_tier_level begin
let general_profile_space = gb.readBits(2);
let general_tier_flag = gb.readBool();
let general_profile_idc = gb.readBits(5);
let general_profile_compatibility_flags_1 = gb.readByte();
let general_profile_compatibility_flags_2 = gb.readByte();
let general_profile_compatibility_flags_3 = gb.readByte();
let general_profile_compatibility_flags_4 = gb.readByte();
let general_constraint_indicator_flags_1 = gb.readByte();
let general_constraint_indicator_flags_2 = gb.readByte();
let general_constraint_indicator_flags_3 = gb.readByte();
let general_constraint_indicator_flags_4 = gb.readByte();
let general_constraint_indicator_flags_5 = gb.readByte();
let general_constraint_indicator_flags_6 = gb.readByte();
let general_level_idc = gb.readByte();
let sub_layer_profile_present_flag = [];
let sub_layer_level_present_flag = [];
for (let i = 0; i < max_sub_layers_minus1; i++) {
sub_layer_profile_present_flag.push(gb.readBool());
sub_layer_level_present_flag.push(gb.readBool());
}
if (max_sub_layers_minus1 > 0) {
for (let i = max_sub_layers_minus1; i < 8; i++) {
gb.readBits(2);
}
}
for (let i = 0; i < max_sub_layers_minus1; i++) {
if (sub_layer_profile_present_flag[i]) {
gb.readByte(); // sub_layer_profile_space, sub_layer_tier_flag, sub_layer_profile_idc
gb.readByte();
gb.readByte();
gb.readByte();
gb.readByte(); // sub_layer_profile_compatibility_flag
gb.readByte();
gb.readByte();
gb.readByte();
gb.readByte();
gb.readByte();
gb.readByte();
}
if (sub_layer_profile_present_flag[i]) {
gb.readByte();
}
}
// profile_tier_level end
gb.readUEG();
let chroma_format_idc = gb.readUEG();
if (chroma_format_idc == 3) {
gb.readBits(1); // separate_colour_plane_flag
}
let pic_width_in_luma_samples = gb.readUEG();
let pic_height_in_luma_samples = gb.readUEG();
let conformance_window_flag = gb.readBool();
if (conformance_window_flag) {
left_offset += gb.readUEG();
right_offset += gb.readUEG();
top_offset += gb.readUEG();
bottom_offset += gb.readUEG();
}
let bit_depth_luma_minus8 = gb.readUEG();
let bit_depth_chroma_minus8 = gb.readUEG();
let log2_max_pic_order_cnt_lsb_minus4 = gb.readUEG();
let sub_layer_ordering_info_present_flag = gb.readBool();
for (let i = sub_layer_ordering_info_present_flag ? 0 : max_sub_layers_minus1; i <= max_sub_layers_minus1; i++) {
gb.readUEG(); // max_dec_pic_buffering_minus1[i]
gb.readUEG(); // max_num_reorder_pics[i]
gb.readUEG(); // max_latency_increase_plus1[i]
}
gb.readUEG();
gb.readUEG();
gb.readUEG();
gb.readUEG();
gb.readUEG();
gb.readUEG();
let scaling_list_enabled_flag = gb.readBool();
if (scaling_list_enabled_flag) {
let sps_scaling_list_data_present_flag = gb.readBool();
if (sps_scaling_list_data_present_flag) {
for (let sizeId = 0; sizeId < 4; sizeId++) {
for (let matrixId = 0; matrixId < (sizeId === 3 ? 2 : 6); matrixId++) {
let scaling_list_pred_mode_flag = gb.readBool();
if (!scaling_list_pred_mode_flag) {
gb.readUEG(); // scaling_list_pred_matrix_id_delta
} else {
let coefNum = Math.min(64, 1 << 4 + (sizeId << 1));
if (sizeId > 1) {
gb.readSEG();
}
for (let i = 0; i < coefNum; i++) {
gb.readSEG();
}
}
}
}
}
}
gb.readBool();
gb.readBool();
let pcm_enabled_flag = gb.readBool();
if (pcm_enabled_flag) {
gb.readByte();
gb.readUEG();
gb.readUEG();
gb.readBool();
}
let num_short_term_ref_pic_sets = gb.readUEG();
let num_delta_pocs = 0;
for (let i = 0; i < num_short_term_ref_pic_sets; i++) {
let inter_ref_pic_set_prediction_flag = false;
if (i !== 0) {
inter_ref_pic_set_prediction_flag = gb.readBool();
}
if (inter_ref_pic_set_prediction_flag) {
if (i === num_short_term_ref_pic_sets) {
gb.readUEG();
}
gb.readBool();
gb.readUEG();
let next_num_delta_pocs = 0;
for (let j = 0; j <= num_delta_pocs; j++) {
let used_by_curr_pic_flag = gb.readBool();
let use_delta_flag = false;
if (!used_by_curr_pic_flag) {
use_delta_flag = gb.readBool();
}
if (used_by_curr_pic_flag || use_delta_flag) {
next_num_delta_pocs++;
}
}
num_delta_pocs = next_num_delta_pocs;
} else {
let num_negative_pics = gb.readUEG();
let num_positive_pics = gb.readUEG();
num_delta_pocs = num_negative_pics + num_positive_pics;
for (let j = 0; j < num_negative_pics; j++) {
gb.readUEG();
gb.readBool();
}
for (let j = 0; j < num_positive_pics; j++) {
gb.readUEG();
gb.readBool();
}
}
}
let long_term_ref_pics_present_flag = gb.readBool();
if (long_term_ref_pics_present_flag) {
let num_long_term_ref_pics_sps = gb.readUEG();
for (let i = 0; i < num_long_term_ref_pics_sps; i++) {
for (let j = 0; j < log2_max_pic_order_cnt_lsb_minus4 + 4; j++) {
gb.readBits(1);
}
gb.readBits(1);
}
}
//*
let default_display_window_flag = false; // for calc offset
let min_spatial_segmentation_idc = 0; // for hvcC
let sar_width = 1,
sar_height = 1;
let fps_fixed = false,
fps_den = 1,
fps_num = 1;
//*/
gb.readBool();
gb.readBool();
let vui_parameters_present_flag = gb.readBool();
if (vui_parameters_present_flag) {
let aspect_ratio_info_present_flag = gb.readBool();
if (aspect_ratio_info_present_flag) {
let aspect_ratio_idc = gb.readByte();
let sar_w_table = [1, 12, 10, 16, 40, 24, 20, 32, 80, 18, 15, 64, 160, 4, 3, 2];
let sar_h_table = [1, 11, 11, 11, 33, 11, 11, 11, 33, 11, 11, 33, 99, 3, 2, 1];
if (aspect_ratio_idc > 0 && aspect_ratio_idc < 16) {
sar_width = sar_w_table[aspect_ratio_idc - 1];
sar_height = sar_h_table[aspect_ratio_idc - 1];
} else if (aspect_ratio_idc === 255) {
sar_width = gb.readBits(16);
sar_height = gb.readBits(16);
}
}
let overscan_info_present_flag = gb.readBool();
if (overscan_info_present_flag) {
gb.readBool();
}
let video_signal_type_present_flag = gb.readBool();
if (video_signal_type_present_flag) {
gb.readBits(3);
gb.readBool();
let colour_description_present_flag = gb.readBool();
if (colour_description_present_flag) {
gb.readByte();
gb.readByte();
gb.readByte();
}
}
let chroma_loc_info_present_flag = gb.readBool();
if (chroma_loc_info_present_flag) {
gb.readUEG();
gb.readUEG();
}
gb.readBool();
gb.readBool();
gb.readBool();
default_display_window_flag = gb.readBool();
if (default_display_window_flag) {
left_offset += gb.readUEG();
right_offset += gb.readUEG();
top_offset += gb.readUEG();
bottom_offset += gb.readUEG();
}
let vui_timing_info_present_flag = gb.readBool();
if (vui_timing_info_present_flag) {
fps_den = gb.readBits(32);
fps_num = gb.readBits(32);
let vui_poc_proportional_to_timing_flag = gb.readBool();
if (vui_poc_proportional_to_timing_flag) {
gb.readUEG();
let vui_hrd_parameters_present_flag = gb.readBool();
if (vui_hrd_parameters_present_flag) {
let nal_hrd_parameters_present_flag = false;
let vcl_hrd_parameters_present_flag = false;
let sub_pic_hrd_params_present_flag = false;
{
nal_hrd_parameters_present_flag = gb.readBool();
vcl_hrd_parameters_present_flag = gb.readBool();
if (nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag) {
sub_pic_hrd_params_present_flag = gb.readBool();
if (sub_pic_hrd_params_present_flag) {
gb.readByte();
gb.readBits(5);
gb.readBool();
gb.readBits(5);
}
gb.readBits(4);
gb.readBits(4);
if (sub_pic_hrd_params_present_flag) {
gb.readBits(4);
}
gb.readBits(5);
gb.readBits(5);
gb.readBits(5);
}
}
for (let i = 0; i <= max_sub_layers_minus1; i++) {
let fixed_pic_rate_general_flag = gb.readBool();
fps_fixed = fixed_pic_rate_general_flag;
let fixed_pic_rate_within_cvs_flag = false;
let cpbCnt = 1;
if (!fixed_pic_rate_general_flag) {
fixed_pic_rate_within_cvs_flag = gb.readBool();
}
let low_delay_hrd_flag = false;
if (fixed_pic_rate_within_cvs_flag) {
gb.readSEG();
} else {
low_delay_hrd_flag = gb.readBool();
}
if (!low_delay_hrd_flag) {
cpbcnt = gb.readUEG() + 1;
}
if (nal_hrd_parameters_present_flag) {
for (let j = 0; j < cpbCnt; j++) {
gb.readUEG();
gb.readUEG();
if (sub_pic_hrd_params_present_flag) {
gb.readUEG();
gb.readUEG();
}
}
}
if (vcl_hrd_parameters_present_flag) {
for (let j = 0; j < cpbCnt; j++) {
gb.readUEG();
gb.readUEG();
if (sub_pic_hrd_params_present_flag) {
gb.readUEG();
gb.readUEG();
}
}
}
}
}
}
}
let bitstream_restriction_flag = gb.readBool();
if (bitstream_restriction_flag) {
gb.readBool();
gb.readBool();
gb.readBool();
min_spatial_segmentation_idc = gb.readUEG();
gb.readUEG();
gb.readUEG();
gb.readUEG();
gb.readUEG();
}
}
gb.readBool(); // ignore...
// for meta data
let codec_mimetype = `hvc1.${general_profile_idc}.1.L${general_level_idc}.B0`;
let codec_width = pic_width_in_luma_samples;
let codec_height = pic_height_in_luma_samples;
let sar_scale = 1;
if (sar_width !== 1 && sar_height !== 1) {
sar_scale = sar_width / sar_height;
}
gb.destroy();
gb = null;
return {
codec_mimetype,
level_string: getLevelString(general_level_idc),
profile_idc: general_profile_idc,
bit_depth: bit_depth_luma_minus8 + 8,
ref_frames: 1,
// FIXME!!!
chroma_format: chroma_format_idc,
chroma_format_string: getChromaFormatString(chroma_format_idc),
general_level_idc,
general_profile_space,
general_tier_flag,
general_profile_idc,
general_profile_compatibility_flags_1,
general_profile_compatibility_flags_2,
general_profile_compatibility_flags_3,
general_profile_compatibility_flags_4,
general_constraint_indicator_flags_1,
general_constraint_indicator_flags_2,
general_constraint_indicator_flags_3,
general_constraint_indicator_flags_4,
general_constraint_indicator_flags_5,
general_constraint_indicator_flags_6,
min_spatial_segmentation_idc,
constant_frame_rate: 0 /* FIXME!! fps_fixed ? 1 : 0? */,
chroma_format_idc,
bit_depth_luma_minus8,
bit_depth_chroma_minus8,
frame_rate: {
fixed: fps_fixed,
fps: fps_num / fps_den,
fps_den: fps_den,
fps_num: fps_num
},
sar_ratio: {
width: sar_width,
height: sar_height
},
codec_size: {
width: codec_width,
height: codec_height
},
present_size: {
width: codec_width * sar_scale,
height: codec_height
}
};
};
const parseHevcVPS = uint8array => {
let rbsp = _ebsp2rbsp(uint8array);
let gb = new ExpGolomb$1(rbsp);
/* remove NALu Header */
gb.readByte();
gb.readByte();
// VPS
gb.readBits(4);
gb.readBits(2);
gb.readBits(6);
let max_sub_layers_minus1 = gb.readBits(3);
let temporal_id_nesting_flag = gb.readBool();
// and more ...
return {
num_temporal_layers: max_sub_layers_minus1 + 1,
temporal_id_nested: temporal_id_nesting_flag
};
};
const parseHevcPPS = uint8array => {
let rbsp = _ebsp2rbsp(uint8array);
let gb = new ExpGolomb$1(rbsp);
/* remove NALu Header */
gb.readByte();
gb.readByte();
gb.readUEG();
gb.readUEG();
gb.readBool();
gb.readBool();
gb.readBits(3);
gb.readBool();
gb.readBool();
gb.readUEG();
gb.readUEG();
gb.readSEG();
gb.readBool();
gb.readBool();
let cu_qp_delta_enabled_flag = gb.readBool();
if (cu_qp_delta_enabled_flag) {
gb.readUEG();
}
gb.readSEG();
gb.readSEG();
gb.readBool();
gb.readBool();
gb.readBool();
gb.readBool();
let tiles_enabled_flag = gb.readBool();
let entropy_coding_sync_enabled_flag = gb.readBool();
// and more ...
// needs hvcC
let parallelismType = 1; // slice-based parallel decoding
if (entropy_coding_sync_enabled_flag && tiles_enabled_flag) {
parallelismType = 0; // mixed-type parallel decoding
} else if (entropy_coding_sync_enabled_flag) {
parallelismType = 3; // wavefront-based parallel decoding
} else if (tiles_enabled_flag) {
parallelismType = 2; // tile-based parallel decoding
}
return {
parallelismType
};
};
class H265NaluParser {
static _ebsp2rbsp(uint8array) {
let src = uint8array;
let src_length = src.byteLength;
let dst = new Uint8Array(src_length);
let dst_idx = 0;
for (let i = 0; i < src_length; i++) {
if (i >= 2) {
// Unescape: Skip 0x03 after 00 00
if (src[i] === 0x03 && src[i - 1] === 0x00 && src[i - 2] === 0x00) {
continue;
}
}
dst[dst_idx] = src[i];
dst_idx++;
}
return new Uint8Array(dst.buffer, 0, dst_idx);
}
static parseVPS(uint8array) {
let rbsp = H265NaluParser._ebsp2rbsp(uint8array);
let gb = new ExpGolomb$1(rbsp);
/* remove NALu Header */
gb.readByte();
gb.readByte();
// VPS
gb.readBits(4);
gb.readBits(2);
gb.readBits(6);
let max_sub_layers_minus1 = gb.readBits(3);
let temporal_id_nesting_flag = gb.readBool();
// and more ...
return {
num_temporal_layers: max_sub_layers_minus1 + 1,
temporal_id_nested: temporal_id_nesting_flag
};
}
static parseSPS(uint8array) {
let rbsp = H265NaluParser._ebsp2rbsp(uint8array);
let gb = new ExpGolomb$1(rbsp);
/* remove NALu Header */
gb.readByte();
gb.readByte();
let left_offset = 0,
right_offset = 0,
top_offset = 0,
bottom_offset = 0;
// SPS
gb.readBits(4);
let max_sub_layers_minus1 = gb.readBits(3);
gb.readBool();
// profile_tier_level begin
let general_profile_space = gb.readBits(2);
let general_tier_flag = gb.readBool();
let general_profile_idc = gb.readBits(5);
let general_profile_compatibility_flags_1 = gb.readByte();
let general_profile_compatibility_flags_2 = gb.readByte();
let general_profile_compatibility_flags_3 = gb.readByte();
let general_profile_compatibility_flags_4 = gb.readByte();
let general_constraint_indicator_flags_1 = gb.readByte();
let general_constraint_indicator_flags_2 = gb.readByte();
let general_constraint_indicator_flags_3 = gb.readByte();
let general_constraint_indicator_flags_4 = gb.readByte();
let general_constraint_indicator_flags_5 = gb.readByte();
let general_constraint_indicator_flags_6 = gb.readByte();
let general_level_idc = gb.readByte();
let sub_layer_profile_present_flag = [];
let sub_layer_level_present_flag = [];
for (let i = 0; i < max_sub_layers_minus1; i++) {
sub_layer_profile_present_flag.push(gb.readBool());
sub_layer_level_present_flag.push(gb.readBool());
}
if (max_sub_layers_minus1 > 0) {
for (let i = max_sub_layers_minus1; i < 8; i++) {
gb.readBits(2);
}
}
for (let i = 0; i < max_sub_layers_minus1; i++) {
if (sub_layer_profile_present_flag[i]) {
gb.readByte(); // sub_layer_profile_space, sub_layer_tier_flag, sub_layer_profile_idc
gb.readByte();
gb.readByte();
gb.readByte();
gb.readByte(); // sub_layer_profile_compatibility_flag
gb.readByte();
gb.readByte();
gb.readByte();
gb.readByte();
gb.readByte();
gb.readByte();
}
if (sub_layer_level_present_flag[i]) {
gb.readByte();
}
}
// profile_tier_level end
gb.readUEG();
let chroma_format_idc = gb.readUEG();
if (chroma_format_idc == 3) {
gb.readBits(1); // separate_colour_plane_flag
}
let pic_width_in_luma_samples = gb.readUEG();
let pic_height_in_luma_samples = gb.readUEG();
let conformance_window_flag = gb.readBool();
if (conformance_window_flag) {
left_offset += gb.readUEG();
right_offset += gb.readUEG();
top_offset += gb.readUEG();
bottom_offset += gb.readUEG();
}
let bit_depth_luma_minus8 = gb.readUEG();
let bit_depth_chroma_minus8 = gb.readUEG();
let log2_max_pic_order_cnt_lsb_minus4 = gb.readUEG();
let sub_layer_ordering_info_present_flag = gb.readBool();
for (let i = sub_layer_ordering_info_present_flag ? 0 : max_sub_layers_minus1; i <= max_sub_layers_minus1; i++) {
gb.readUEG(); // max_dec_pic_buffering_minus1[i]
gb.readUEG(); // max_num_reorder_pics[i]
gb.readUEG(); // max_latency_increase_plus1[i]
}
gb.readUEG();
gb.readUEG();
gb.readUEG();
gb.readUEG();
gb.readUEG();
gb.readUEG();
let scaling_list_enabled_flag = gb.readBool();
if (scaling_list_enabled_flag) {
let sps_scaling_list_data_present_flag = gb.readBool();
if (sps_scaling_list_data_present_flag) {
for (let sizeId = 0; sizeId < 4; sizeId++) {
for (let matrixId = 0; matrixId < (sizeId === 3 ? 2 : 6); matrixId++) {
let scaling_list_pred_mode_flag = gb.readBool();
if (!scaling_list_pred_mode_flag) {
gb.readUEG(); // scaling_list_pred_matrix_id_delta
} else {
let coefNum = Math.min(64, 1 << 4 + (sizeId << 1));
if (sizeId > 1) {
gb.readSEG();
}
for (let i = 0; i < coefNum; i++) {
gb.readSEG();
}
}
}
}
}
}
gb.readBool();
gb.readBool();
let pcm_enabled_flag = gb.readBool();
if (pcm_enabled_flag) {
gb.readByte();
gb.readUEG();
gb.readUEG();
gb.readBool();
}
let num_short_term_ref_pic_sets = gb.readUEG();
let num_delta_pocs = 0;
for (let i = 0; i < num_short_term_ref_pic_sets; i++) {
let inter_ref_pic_set_prediction_flag = false;
if (i !== 0) {
inter_ref_pic_set_prediction_flag = gb.readBool();
}
if (inter_ref_pic_set_prediction_flag) {
if (i === num_short_term_ref_pic_sets) {
gb.readUEG();
}
gb.readBool();
gb.readUEG();
let next_num_delta_pocs = 0;
for (let j = 0; j <= num_delta_pocs; j++) {
let used_by_curr_pic_flag = gb.readBool();
let use_delta_flag = false;
if (!used_by_curr_pic_flag) {
use_delta_flag = gb.readBool();
}
if (used_by_curr_pic_flag || use_delta_flag) {
next_num_delta_pocs++;
}
}
num_delta_pocs = next_num_delta_pocs;
} else {
let num_negative_pics = gb.readUEG();
let num_positive_pics = gb.readUEG();
num_delta_pocs = num_negative_pics + num_positive_pics;
for (let j = 0; j < num_negative_pics; j++) {
gb.readUEG();
gb.readBool();
}
for (let j = 0; j < num_positive_pics; j++) {
gb.readUEG();
gb.readBool();
}
}
}
let long_term_ref_pics_present_flag = gb.readBool();
if (long_term_ref_pics_present_flag) {
let num_long_term_ref_pics_sps = gb.readUEG();
for (let i = 0; i < num_long_term_ref_pics_sps; i++) {
for (let j = 0; j < log2_max_pic_order_cnt_lsb_minus4 + 4; j++) {
gb.readBits(1);
}
gb.readBits(1);
}
}
//*
let default_display_window_flag = false; // for calc offset
let min_spatial_segmentation_idc = 0; // for hvcC
let sar_width = 1,
sar_height = 1;
let fps_fixed = false,
fps_den = 1,
fps_num = 1;
//*/
gb.readBool();
gb.readBool();
let vui_parameters_present_flag = gb.readBool();
if (vui_parameters_present_flag) {
let aspect_ratio_info_present_flag = gb.readBool();
if (aspect_ratio_info_present_flag) {
let aspect_ratio_idc = gb.readByte();
let sar_w_table = [1, 12, 10, 16, 40, 24, 20, 32, 80, 18, 15, 64, 160, 4, 3, 2];
let sar_h_table = [1, 11, 11, 11, 33, 11, 11, 11, 33, 11, 11, 33, 99, 3, 2, 1];
if (aspect_ratio_idc > 0 && aspect_ratio_idc <= 16) {
sar_width = sar_w_table[aspect_ratio_idc - 1];
sar_height = sar_h_table[aspect_ratio_idc - 1];
} else if (aspect_ratio_idc === 255) {
sar_width = gb.readBits(16);
sar_height = gb.readBits(16);
}
}
let overscan_info_present_flag = gb.readBool();
if (overscan_info_present_flag) {
gb.readBool();
}
let video_signal_type_present_flag = gb.readBool();
if (video_signal_type_present_flag) {
gb.readBits(3);
gb.readBool();
let colour_description_present_flag = gb.readBool();
if (colour_description_present_flag) {
gb.readByte();
gb.readByte();
gb.readByte();
}
}
let chroma_loc_info_present_flag = gb.readBool();
if (chroma_loc_info_present_flag) {
gb.readUEG();
gb.readUEG();
}
gb.readBool();
gb.readBool();
gb.readBool();
default_display_window_flag = gb.readBool();
if (default_display_window_flag) {
gb.readUEG();
gb.readUEG();
gb.readUEG();
gb.readUEG();
}
let vui_timing_info_present_flag = gb.readBool();
if (vui_timing_info_present_flag) {
fps_den = gb.readBits(32);
fps_num = gb.readBits(32);
let vui_poc_proportional_to_timing_flag = gb.readBool();
if (vui_poc_proportional_to_timing_flag) {
gb.readUEG();
}
let vui_hrd_parameters_present_flag = gb.readBool();
if (vui_hrd_parameters_present_flag) {
let nal_hrd_parameters_present_flag = false;
let vcl_hrd_parameters_present_flag = false;
let sub_pic_hrd_params_present_flag = false;
{
nal_hrd_parameters_present_flag = gb.readBool();
vcl_hrd_parameters_present_flag = gb.readBool();
if (nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag) {
sub_pic_hrd_params_present_flag = gb.readBool();
if (sub_pic_hrd_params_present_flag) {
gb.readByte();
gb.readBits(5);
gb.readBool();
gb.readBits(5);
}
gb.readBits(4);
gb.readBits(4);
if (sub_pic_hrd_params_present_flag) {
gb.readBits(4);
}
gb.readBits(5);
gb.readBits(5);
gb.readBits(5);
}
}
for (let i = 0; i <= max_sub_layers_minus1; i++) {
let fixed_pic_rate_general_flag = gb.readBool();
fps_fixed = fixed_pic_rate_general_flag;
let fixed_pic_rate_within_cvs_flag = true;
let cpbCnt = 1;
if (!fixed_pic_rate_general_flag) {
fixed_pic_rate_within_cvs_flag = gb.readBool();
}
let low_delay_hrd_flag = false;
if (fixed_pic_rate_within_cvs_flag) {
gb.readUEG();
} else {
low_delay_hrd_flag = gb.readBool();
}
if (!low_delay_hrd_flag) {
cpbCnt = gb.readUEG() + 1;
}
if (nal_hrd_parameters_present_flag) {
for (let j = 0; j < cpbCnt; j++) {
gb.readUEG();
gb.readUEG();
if (sub_pic_hrd_params_present_flag) {
gb.readUEG();
gb.readUEG();
}
}
gb.readBool();
}
if (vcl_hrd_parameters_present_flag) {
for (let j = 0; j < cpbCnt; j++) {
gb.readUEG();
gb.readUEG();
if (sub_pic_hrd_params_present_flag) {
gb.readUEG();
gb.readUEG();
}
}
gb.readBool();
}
}
}
}
let bitstream_restriction_flag = gb.readBool();
if (bitstream_restriction_flag) {
gb.readBool();
gb.readBool();
gb.readBool();
min_spatial_segmentation_idc = gb.readUEG();
gb.readUEG();
gb.readUEG();
gb.readUEG();
gb.readUEG();
}
}
gb.readBool(); // ignore...
// for meta data
let codec_mimetype = `hvc1.${general_profile_idc}.1.L${general_level_idc}.B0`;
let sub_wc = chroma_format_idc === 1 || chroma_format_idc === 2 ? 2 : 1;
let sub_hc = chroma_format_idc === 1 ? 2 : 1;
let codec_width = pic_width_in_luma_samples - (left_offset + right_offset) * sub_wc;
let codec_height = pic_height_in_luma_samples - (top_offset + bottom_offset) * sub_hc;
let sar_scale = 1;
if (sar_width !== 1 && sar_height !== 1) {
sar_scale = sar_width / sar_height;
}
gb.destroy();
gb = null;
return {
codec_mimetype,
profile_string: H265NaluParser.getProfileString(general_profile_idc),
level_string: H265NaluParser.getLevelString(general_level_idc),
profile_idc: general_profile_idc,
bit_depth: bit_depth_luma_minus8 + 8,
ref_frames: 1,
// FIXME!!!
chroma_format: chroma_format_idc,
chroma_format_string: H265NaluParser.getChromaFormatString(chroma_format_idc),
general_level_idc,
general_profile_space,
general_tier_flag,
general_profile_idc,
general_profile_compatibility_flags_1,
general_profile_compatibility_flags_2,
general_profile_compatibility_flags_3,
general_profile_compatibility_flags_4,
general_constraint_indicator_flags_1,
general_constraint_indicator_flags_2,
general_constraint_indicator_flags_3,
general_constraint_indicator_flags_4,
general_constraint_indicator_flags_5,
general_constraint_indicator_flags_6,
min_spatial_segmentation_idc,
constant_frame_rate: 0 /* FIXME!! fps_fixed ? 1 : 0? */,
chroma_format_idc,
bit_depth_luma_minus8,
bit_depth_chroma_minus8,
frame_rate: {
fixed: fps_fixed,
fps: fps_num / fps_den,
fps_den: fps_den,
fps_num: fps_num
},
sar_ratio: {
width: sar_width,
height: sar_height
},
codec_size: {
width: codec_width,
height: codec_height
},
present_size: {
width: codec_width * sar_scale,
height: codec_height
}
};
}
static parsePPS(uint8array) {
let rbsp = H265NaluParser._ebsp2rbsp(uint8array);
let gb = new ExpGolomb$1(rbsp);
/* remove NALu Header */
gb.readByte();
gb.readByte();
gb.readUEG();
gb.readUEG();
gb.readBool();
gb.readBool();
gb.readBits(3);
gb.readBool();
gb.readBool();
gb.readUEG();
gb.readUEG();
gb.readSEG();
gb.readBool();
gb.readBool();
let cu_qp_delta_enabled_flag = gb.readBool();
if (cu_qp_delta_enabled_flag) {
gb.readUEG();
}
gb.readSEG();
gb.readSEG();
gb.readBool();
gb.readBool();
gb.readBool();
gb.readBool();
let tiles_enabled_flag = gb.readBool();
let entropy_coding_sync_enabled_flag = gb.readBool();
// and more ...
// needs hvcC
let parallelismType = 1; // slice-based parallel decoding
if (entropy_coding_sync_enabled_flag && tiles_enabled_flag) {
parallelismType = 0; // mixed-type parallel decoding
} else if (entropy_coding_sync_enabled_flag) {
parallelismType = 3; // wavefront-based parallel decoding
} else if (tiles_enabled_flag) {
parallelismType = 2; // tile-based parallel decoding
}
return {
parallelismType
};
}
static getChromaFormatString(chroma_idc) {
switch (chroma_idc) {
case 0:
return '4:0:0';
case 1:
return '4:2:0';
case 2:
return '4:2:2';
case 3:
return '4:4:4';
default:
return 'Unknown';
}
}
static getProfileString(profile_idc) {
switch (profile_idc) {
case 1:
return 'Main';
case 2:
return 'Main10';
case 3:
return 'MainSP';
case 4:
return 'Rext';
case 9:
return 'SCC';
default:
return 'Unknown';
}
}
static getLevelString(level_idc) {
return (level_idc / 30).toFixed(1);
}
}
/**
*
* @param arrayBuffer
*/
function parseHEVCDecoderConfigurationRecord$2(arrayBuffer) {
let info = {
codecWidth: 0,
codecHeight: 0,
videoType: VIDEO_ENCODE_TYPE.h265
};
info.width = 0;
info.height = 0;
info.profile = 0;
info.level = 0;
// remove 5 bytes
arrayBuffer = arrayBuffer.slice(5);
do {
let hevc = {};
if (arrayBuffer.length < 23) {
console.warn('parseHEVCDecoderConfigurationRecord$2', `arrayBuffer.length ${arrayBuffer.length} < 23`);
break;
}
hevc.configurationVersion = arrayBuffer[0];
if (hevc.configurationVersion != 1) {
break;
}
hevc.general_profile_space = arrayBuffer[1] >> 6 & 0x03;
hevc.general_tier_flag = arrayBuffer[1] >> 5 & 0x01;
hevc.general_profile_idc = arrayBuffer[1] & 0x1F;
hevc.general_profile_compatibility_flags = arrayBuffer[2] << 24 | arrayBuffer[3] << 16 | arrayBuffer[4] << 8 | arrayBuffer[5];
hevc.general_constraint_indicator_flags = arrayBuffer[6] << 24 | arrayBuffer[7] << 16 | arrayBuffer[8] << 8 | arrayBuffer[9];
hevc.general_constraint_indicator_flags = hevc.general_constraint_indicator_flags << 16 | arrayBuffer[10] << 8 | arrayBuffer[11];
hevc.general_level_idc = arrayBuffer[12];
hevc.min_spatial_segmentation_idc = (arrayBuffer[13] & 0x0F) << 8 | arrayBuffer[14];
hevc.parallelismType = arrayBuffer[15] & 0x03;
hevc.chromaFormat = arrayBuffer[16] & 0x03;
hevc.bitDepthLumaMinus8 = arrayBuffer[17] & 0x07;
hevc.bitDepthChromaMinus8 = arrayBuffer[18] & 0x07;
hevc.avgFrameRate = arrayBuffer[19] << 8 | arrayBuffer[20];
hevc.constantFrameRate = arrayBuffer[21] >> 6 & 0x03;
hevc.numTemporalLayers = arrayBuffer[21] >> 3 & 0x07;
hevc.temporalIdNested = arrayBuffer[21] >> 2 & 0x01;
hevc.lengthSizeMinusOne = arrayBuffer[21] & 0x03;
let numOfArrays = arrayBuffer[22];
let p = arrayBuffer.slice(23);
for (let i = 0; i < numOfArrays; i++) {
if (p.length < 3) {
break;
}
let nalutype = p[0] & 0x3F;
let n = p[1] << 8 | p[2];
// console.log('nalutype', nalutype,n)
p = p.slice(3);
for (let j = 0; j < n; j++) {
if (p.length < 2) {
break;
}
let k = p[0] << 8 | p[1];
// console.log('k', k)
if (p.length < 2 + k) {
break;
}
p = p.slice(2);
if (nalutype == 33) {
//SPS
let sps = new Uint8Array(k);
sps.set(p.slice(0, k), 0);
hevc.psps = HEVCParseSPS(sps, hevc);
info.profile = hevc.general_profile_idc;
info.level = hevc.general_level_idc / 30.0;
info.width = hevc.psps.pic_width_in_luma_samples - (hevc.psps.conf_win_left_offset + hevc.psps.conf_win_right_offset);
info.height = hevc.psps.pic_height_in_luma_samples - (hevc.psps.conf_win_top_offset + hevc.psps.conf_win_bottom_offset);
}
p = p.slice(k);
}
}
} while (0);
info.codecWidth = info.width || 1920;
info.codecHeight = info.height || 1080;
info.presentHeight = info.codecHeight;
info.presentWidth = info.codecWidth;
info.timescale = 1000;
info.refSampleDuration = 1000 * (1000 / 23976);
return info;
}
/**
*
* @param arrayBuffer
* @returns {{}}
* @desc v4 版本
*/
function parseHEVCDecoderConfigurationRecord$4(arrayBuffer) {
const hevcSequenceHeader = arrayBuffer;
if (hevcSequenceHeader.length < 22) {
console.error(`Invalid HEVCDecoderConfigurationRecord, lack of data!`);
return;
}
let meta = {
codecWidth: 0,
codecHeight: 0,
videoType: VIDEO_ENCODE_TYPE.h265
};
let le = function () {
let buf = new ArrayBuffer(2);
new DataView(buf).setInt16(0, 256, true); // little-endian write
return new Int16Array(buf)[0] === 256; // platform-spec read, if equal then LE
}();
let v = new DataView(hevcSequenceHeader.buffer);
let version = v.getUint8(0); // configurationVersion
let hevcProfile = v.getUint8(1) & 0x1F; // hevcProfileIndication
if (version !== 1 || hevcProfile === 0) {
console.error(`Flv: Invalid HEVCDecoderConfigurationRecord`);
return;
}
let naluLengthSize = (v.getUint8(21) & 3) + 1; // lengthSizeMinusOne
if (naluLengthSize !== 3 && naluLengthSize !== 4) {
// holy shit!!!
console.error(`Strange NaluLengthSizeMinusOne: ${naluLengthSize - 1}`);
return;
}
let numOfArrays = v.getUint8(22);
for (let i = 0, offset = 23; i < numOfArrays; i++) {
let nalUnitType = v.getUint8(offset + 0) & 0x3F;
let numNalus = v.getUint16(offset + 1, !le);
offset += 3;
for (let j = 0; j < numNalus; j++) {
let len = v.getUint16(offset + 0, !le);
if (j !== 0) {
offset += 2 + len;
continue;
}
if (nalUnitType === 33) {
offset += 2;
let sps = new Uint8Array(hevcSequenceHeader.buffer, offset, len);
let config = H265NaluParser.parseSPS(sps);
meta.codecWidth = config.codec_size.width;
meta.codecHeight = config.codec_size.height;
meta.presentWidth = config.present_size.width;
meta.presentHeight = config.present_size.height;
meta.profile = config.profile_string;
meta.level = config.level_string;
meta.bitDepth = config.bit_depth;
meta.chromaFormat = config.chroma_format;
meta.sarRatio = config.sar_ratio;
meta.frameRate = config.frame_rate;
if (config.frame_rate.fixed === false || config.frame_rate.fps_num === 0 || config.frame_rate.fps_den === 0) {
meta.frameRate = {
fixed: true,
fps: 23.976,
fps_num: 23976,
fps_den: 1000
};
}
meta.frameRate.fps_den;
meta.frameRate.fps_num;
// meta.refSampleDuration = meta.timescale * (fps_den / fps_num);
meta.codec = config.codec_mimetype;
offset += len;
} else {
offset += 2 + len;
}
}
}
meta.hvcc = new Uint8Array(hevcSequenceHeader);
return meta;
}
function parseHEVCDecoderVPSAndSPSAndPPS(arrayBuffer) {
//
let offset = 28 - 5; // 23
//
const vpsTag = arrayBuffer[offset];
if ((vpsTag & 0x3F) !== H265_NAL_TYPE.vps) {
console.warn(`parseHEVCDecoderVPSAndSPSAndPPS and vpsTag is ${vpsTag}`);
return {};
}
offset += 2;
offset += 1;
const vpsLength = arrayBuffer[offset + 1] | arrayBuffer[offset] << 8;
offset += 2;
const vpsData = arrayBuffer.slice(offset, offset + vpsLength);
// console.log('vpsData:', Uint8Array.from(vpsData));
offset += vpsLength;
const spsTag = arrayBuffer[offset];
if ((spsTag & 0x3F) !== H265_NAL_TYPE.sps) {
console.warn(`parseHEVCDecoderVPSAndSPSAndPPS and sps tag is ${spsTag}`);
return {};
}
offset += 2;
offset += 1;
const spsLength = arrayBuffer[offset + 1] | arrayBuffer[offset] << 8;
offset += 2;
const spsData = arrayBuffer.slice(offset, offset + spsLength);
// console.log('spsData:', Uint8Array.from(spsData));
offset += spsLength;
const ppsTag = arrayBuffer[offset];
if ((ppsTag & 0x3F) !== H265_NAL_TYPE.pps) {
console.warn(`parseHEVCDecoderVPSAndSPSAndPPS and pps tag is ${ppsTag}`);
return {};
}
offset += 2;
offset += 1;
const ppsLength = arrayBuffer[offset + 1] | arrayBuffer[offset] << 8;
offset += 2;
const ppsData = arrayBuffer.slice(offset, offset + ppsLength);
// console.log('ppsData:', Uint8Array.from(ppsData));
const spsFlag = new Uint8Array([spsLength >>> 24 & 0xFF, spsLength >>> 16 & 0xFF, spsLength >>> 8 & 0xFF, spsLength & 0xFF]);
const ppsFlag = new Uint8Array([ppsLength >>> 24 & 0xFF, ppsLength >>> 16 & 0xFF, ppsLength >>> 8 & 0xFF, ppsLength & 0xFF]);
const vpsFlag = new Uint8Array([vpsLength >>> 24 & 0xFF, vpsLength >>> 16 & 0xFF, vpsLength >>> 8 & 0xFF, vpsLength & 0xFF]);
const sps = new Uint8Array(spsLength + 4);
sps.set(spsFlag, 0);
sps.set(spsData, 4);
const pps = new Uint8Array(ppsLength + 4);
pps.set(ppsFlag, 0);
pps.set(ppsData, 4);
const vps = new Uint8Array(vpsLength + 4);
vps.set(vpsFlag, 0);
vps.set(vpsData, 4);
return {
sps,
pps,
vps
};
}
function HEVCParsePtl(bitop, hevc, max_sub_layers_minus1) {
let general_ptl = {};
general_ptl.profile_space = bitop.read(2);
general_ptl.tier_flag = bitop.read(1);
general_ptl.profile_idc = bitop.read(5);
general_ptl.profile_compatibility_flags = bitop.read(32);
general_ptl.general_progressive_source_flag = bitop.read(1);
general_ptl.general_interlaced_source_flag = bitop.read(1);
general_ptl.general_non_packed_constraint_flag = bitop.read(1);
general_ptl.general_frame_only_constraint_flag = bitop.read(1);
bitop.read(32);
bitop.read(12);
general_ptl.level_idc = bitop.read(8);
general_ptl.sub_layer_profile_present_flag = [];
general_ptl.sub_layer_level_present_flag = [];
for (let i = 0; i < max_sub_layers_minus1; i++) {
general_ptl.sub_layer_profile_present_flag[i] = bitop.read(1);
general_ptl.sub_layer_level_present_flag[i] = bitop.read(1);
}
if (max_sub_layers_minus1 > 0) {
for (let i = max_sub_layers_minus1; i < 8; i++) {
bitop.read(2);
}
}
general_ptl.sub_layer_profile_space = [];
general_ptl.sub_layer_tier_flag = [];
general_ptl.sub_layer_profile_idc = [];
general_ptl.sub_layer_profile_compatibility_flag = [];
general_ptl.sub_layer_progressive_source_flag = [];
general_ptl.sub_layer_interlaced_source_flag = [];
general_ptl.sub_layer_non_packed_constraint_flag = [];
general_ptl.sub_layer_frame_only_constraint_flag = [];
general_ptl.sub_layer_level_idc = [];
for (let i = 0; i < max_sub_layers_minus1; i++) {
if (general_ptl.sub_layer_profile_present_flag[i]) {
general_ptl.sub_layer_profile_space[i] = bitop.read(2);
general_ptl.sub_layer_tier_flag[i] = bitop.read(1);
general_ptl.sub_layer_profile_idc[i] = bitop.read(5);
general_ptl.sub_layer_profile_compatibility_flag[i] = bitop.read(32);
general_ptl.sub_layer_progressive_source_flag[i] = bitop.read(1);
general_ptl.sub_layer_interlaced_source_flag[i] = bitop.read(1);
general_ptl.sub_layer_non_packed_constraint_flag[i] = bitop.read(1);
general_ptl.sub_layer_frame_only_constraint_flag[i] = bitop.read(1);
bitop.read(32);
bitop.read(12);
}
if (general_ptl.sub_layer_level_present_flag[i]) {
general_ptl.sub_layer_level_idc[i] = bitop.read(8);
} else {
general_ptl.sub_layer_level_idc[i] = 1;
}
}
return general_ptl;
}
function HEVCParseSPS(SPS, hevc) {
let psps = {};
let NumBytesInNALunit = SPS.length;
let rbsp_array = [];
let bitop = new Bitop(SPS);
bitop.read(1); //forbidden_zero_bit
bitop.read(6); //nal_unit_type
bitop.read(6); //nuh_reserved_zero_6bits
bitop.read(3); //nuh_temporal_id_plus1
for (let i = 2; i < NumBytesInNALunit; i++) {
if (i + 2 < NumBytesInNALunit && bitop.look(24) == 0x000003) {
rbsp_array.push(bitop.read(8));
rbsp_array.push(bitop.read(8));
i += 2;
bitop.read(8); /* equal to 0x03 */
} else {
rbsp_array.push(bitop.read(8));
}
}
let rbsp = new Uint8Array(rbsp_array);
let rbspBitop = new Bitop(rbsp);
psps.sps_video_parameter_set_id = rbspBitop.read(4);
psps.sps_max_sub_layers_minus1 = rbspBitop.read(3);
psps.sps_temporal_id_nesting_flag = rbspBitop.read(1);
psps.profile_tier_level = HEVCParsePtl(rbspBitop, hevc, psps.sps_max_sub_layers_minus1);
psps.sps_seq_parameter_set_id = rbspBitop.read_golomb();
psps.chroma_format_idc = rbspBitop.read_golomb();
if (psps.chroma_format_idc == 3) {
psps.separate_colour_plane_flag = rbspBitop.read(1);
} else {
psps.separate_colour_plane_flag = 0;
}
psps.pic_width_in_luma_samples = rbspBitop.read_golomb();
psps.pic_height_in_luma_samples = rbspBitop.read_golomb();
psps.conformance_window_flag = rbspBitop.read(1);
if (psps.conformance_window_flag) {
let vert_mult = 1 + (psps.chroma_format_idc < 2);
let horiz_mult = 1 + (psps.chroma_format_idc < 3);
psps.conf_win_left_offset = rbspBitop.read_golomb() * horiz_mult;
psps.conf_win_right_offset = rbspBitop.read_golomb() * horiz_mult;
psps.conf_win_top_offset = rbspBitop.read_golomb() * vert_mult;
psps.conf_win_bottom_offset = rbspBitop.read_golomb() * vert_mult;
} else {
psps.conf_win_left_offset = 0;
psps.conf_win_right_offset = 0;
psps.conf_win_top_offset = 0;
psps.conf_win_bottom_offset = 0;
}
// Logger.debug(psps);
return psps;
}
function hevcEncoderConfigurationRecord$2(_ref2) {
let {
vps,
pps,
sps
} = _ref2;
let detail = {
configurationVersion: 1
};
const vpsDetail = parseHevcVPS(vps);
const spsDetail = parseHevcSPS(sps);
const ppsDetail = parseHevcPPS(pps);
detail = Object.assign(detail, vpsDetail, spsDetail, ppsDetail);
let length = 23 + (3 + 2 + vps.byteLength) + (3 + 2 + sps.byteLength) + (3 + 2 + pps.byteLength);
let data = new Uint8Array(length);
data[0] = 0x01; // configurationVersion
data[1] = (detail.general_profile_space & 0x03) << 6 | (detail.general_tier_flag ? 1 : 0) << 5 | detail.general_profile_idc & 0x1F;
data[2] = detail.general_profile_compatibility_flags_1 || 0;
data[3] = detail.general_profile_compatibility_flags_2 || 0;
data[4] = detail.general_profile_compatibility_flags_3 || 0;
data[5] = detail.general_profile_compatibility_flags_4 || 0;
data[6] = detail.general_constraint_indicator_flags_1 || 0;
data[7] = detail.general_constraint_indicator_flags_2 || 0;
data[8] = detail.general_constraint_indicator_flags_3 || 0;
data[9] = detail.general_constraint_indicator_flags_4 || 0;
data[10] = detail.general_constraint_indicator_flags_5 || 0;
data[11] = detail.general_constraint_indicator_flags_6 || 0;
data[12] = 0x3C;
data[13] = 0xF0 | (detail.min_spatial_segmentation_idc & 0x0F00) >> 8;
data[14] = detail.min_spatial_segmentation_idc & 0xFF;
data[15] = 0xFC | detail.parallelismType & 0x03;
data[16] = 0xFC | detail.chroma_format_idc & 0x03;
data[17] = 0xF8 | detail.bit_depth_luma_minus8 & 0x07;
data[18] = 0xF8 | detail.bit_depth_chroma_minus8 & 0x07;
data[19] = 0;
data[20] = 0;
data[21] = (detail.constant_frame_rate & 0x03) << 6 | (detail.num_temporal_layers & 0x07) << 3 | (detail.temporal_id_nested ? 1 : 0) << 2 | 3;
data[22] = 3;
data[23 + 0 + 0] = 0x80 | H265_NAL_TYPE.vps;
data[23 + 0 + 1] = 0;
data[23 + 0 + 2] = 1;
data[23 + 0 + 3] = (vps.byteLength & 0xFF00) >> 8;
data[23 + 0 + 4] = (vps.byteLength & 0x00FF) >> 0;
data.set(vps, 23 + 0 + 5);
data[23 + (5 + vps.byteLength) + 0] = 0x80 | H265_NAL_TYPE.sps;
data[23 + (5 + vps.byteLength) + 1] = 0;
data[23 + (5 + vps.byteLength) + 2] = 1;
data[23 + (5 + vps.byteLength) + 3] = (sps.byteLength & 0xFF00) >> 8;
data[23 + (5 + vps.byteLength) + 4] = (sps.byteLength & 0x00FF) >> 0;
data.set(sps, 23 + (5 + vps.byteLength) + 5);
data[23 + (5 + vps.byteLength + 5 + sps.byteLength) + 0] = 0x80 | H265_NAL_TYPE.pps;
data[23 + (5 + vps.byteLength + 5 + sps.byteLength) + 1] = 0;
data[23 + (5 + vps.byteLength + 5 + sps.byteLength) + 2] = 1;
data[23 + (5 + vps.byteLength + 5 + sps.byteLength) + 3] = (pps.byteLength & 0xFF00) >> 8;
data[23 + (5 + vps.byteLength + 5 + sps.byteLength) + 4] = (pps.byteLength & 0x00FF) >> 0;
data.set(pps, 23 + (5 + vps.byteLength + 5 + sps.byteLength) + 5);
const prevData = [0x1c, 0, 0, 0, 0];
const newData = new Uint8Array(prevData.length + data.byteLength);
newData.set(prevData, 0);
newData.set(data, prevData.length);
return newData;
}
/**
*
* @param oneNALBuffer
* @param isIframe
* @returns {Uint8Array}
*/
function hevcEncoderNalePacket(oneNALBuffer, isIframe) {
// 正常发送nal
// 这边增加 是否i帧, 然后前面封装了1 + 8 个字节的数据。
const idrBit = 0x10 | 12;
const nIdrBit = 0x20 | 12;
let tmp = [];
if (isIframe) {
tmp[0] = idrBit;
} else {
tmp[0] = nIdrBit;
}
tmp[1] = 1;
//
tmp[2] = 0;
tmp[3] = 0;
tmp[4] = 0;
// 真正开始的地方。。。
tmp[5] = oneNALBuffer.byteLength >> 24 & 0xff;
tmp[6] = oneNALBuffer.byteLength >> 16 & 0xff;
tmp[7] = oneNALBuffer.byteLength >> 8 & 0xff;
tmp[8] = oneNALBuffer.byteLength & 0xff;
const arrayBuffer = new Uint8Array(tmp.length + oneNALBuffer.byteLength);
arrayBuffer.set(tmp, 0);
arrayBuffer.set(oneNALBuffer, tmp.length);
return arrayBuffer;
}
function hevcEncoderNalePacketNotLength(oneNALBuffer, isIframe) {
// 正常发送nal
// 这边增加 是否i帧, 然后前面封装了1 + 8 个字节的数据。
const idrBit = 0x10 | 12;
const nIdrBit = 0x20 | 12;
let tmp = [];
if (isIframe) {
tmp[0] = idrBit;
} else {
tmp[0] = nIdrBit;
}
tmp[1] = 1;
//
tmp[2] = 0;
tmp[3] = 0;
tmp[4] = 0;
const arrayBuffer = new Uint8Array(tmp.length + oneNALBuffer.byteLength);
arrayBuffer.set(tmp, 0);
arrayBuffer.set(oneNALBuffer, tmp.length);
return arrayBuffer;
}
function getHevcSeqHeadType(nalu) {
return (nalu[0] & 0x7E) >> 1;
}
function isHevcSEIType(type) {
return type === H265_NAL_TYPE.sei;
}
// 32-40是VPS SPS PPS SUFFIX_SEI_NUT等
function isHevcSeqHead(type) {
return type >= 32 && type <= 40;
}
function isNotHevcSeqHead(type) {
return !isHevcSeqHead(type);
}
// 16-21是关键(I)帧
function isHevcNaluIFrame(type) {
return type >= 16 && type <= 21;
}
function isSameHevcNaluType(naluList) {
if (naluList.length === 0) {
return false;
}
const type = getHevcSeqHeadType(naluList[0]);
for (let i = 1; i < naluList.length; i++) {
if (type !== getHevcSeqHeadType(naluList[i])) {
return false;
}
}
return true;
}
class H265AnnexBParser {
constructor(data) {
this.data = data;
this.eofFlag = false;
this.currentStartcodeOffset = this.findNextStartCodeOffset(0);
if (this.eofFlag) {
console.error('Could not find H265 startcode until payload end!');
}
}
findNextStartCodeOffset(start_offset) {
let i = start_offset;
let data = this.data;
while (true) {
if (i + 3 >= data.byteLength) {
this.eofFlag = true;
return data.byteLength;
}
// search 00 00 00 01 or 00 00 01
let uint32 = data[i + 0] << 24 | data[i + 1] << 16 | data[i + 2] << 8 | data[i + 3];
let uint24 = data[i + 0] << 16 | data[i + 1] << 8 | data[i + 2];
if (uint32 === 0x00000001 || uint24 === 0x000001) {
return i;
} else {
i++;
}
}
}
readNextNaluPayload() {
let data = this.data;
let nalu_payload = null;
while (nalu_payload == null) {
if (this.eofFlag) {
break;
}
// offset pointed to start code
let startcode_offset = this.currentStartcodeOffset;
// nalu payload start offset
let offset = startcode_offset;
let u32 = data[offset] << 24 | data[offset + 1] << 16 | data[offset + 2] << 8 | data[offset + 3];
if (u32 === 0x00000001) {
offset += 4;
} else {
offset += 3;
}
let nalu_type = data[offset] >> 1 & 0x3F;
let forbidden_bit = (data[offset] & 0x80) >>> 7;
let next_startcode_offset = this.findNextStartCodeOffset(offset);
this.currentStartcodeOffset = next_startcode_offset;
if (forbidden_bit !== 0) {
// Log.e(this.TAG, `forbidden_bit near offset ${offset} should be 0 but has value ${forbidden_bit}`);
continue;
}
let payload_data = data.subarray(offset, next_startcode_offset);
nalu_payload = {
type: nalu_type,
data: payload_data
};
}
return nalu_payload;
}
}
class H265NaluHVC1 {
constructor(nalu) {
let nalu_size = nalu.data.byteLength;
this.type = nalu.type;
this.data = new Uint8Array(4 + nalu_size); // 4 byte length-header + nalu payload
let v = new DataView(this.data.buffer);
// Fill 4 byte length-header
v.setUint32(0, nalu_size);
// Copy payload
this.data.set(nalu.data, 4);
}
}
class CommonLoader$1 extends Emitter {
constructor(player) {
super();
this.TAG_NAME = 'recorderCommon';
this.player = player;
this.fileName = '';
this._isRecording = false;
this._recordingTimestamp = 0;
this.recordingInterval = null;
//
this.sps = null;
this.pps = null;
this.vps = null;
this.codecId = null;
this.audioCodeId = null;
this.metaInfo = {
codecWidth: 0,
codecHeight: 0,
presentWidth: 0,
presentHeight: 0,
refSampleDuration: 0,
timescale: 1000,
avcc: null,
videoType: ''
};
this.audioMetaInfo = {
timescale: 1000,
sampleRate: 0,
refSampleDuration: 0,
channelCount: 0,
codec: '',
originalCodec: '',
audioType: '',
extraData: new Uint8Array(0)
};
}
destroy() {
this._reset();
this.sps = null;
this.pps = null;
this.vps = null;
this.codecId = null;
this.audioCodeId = null;
this.metaInfo = null;
this.audioMetaInfo = null;
}
get isH264() {
return this.codecId === VIDEO_ENC_CODE.h264;
}
get isH265() {
return this.codecId === VIDEO_ENC_CODE.h265;
}
setFileName(fileName) {
this.fileName = fileName;
}
get isRecording() {
return this._isRecording;
}
get recording() {
return this._isRecording;
}
get recordTime() {
return this._recordingTimestamp;
}
startRecord() {
// 子类实现
}
// just for mp4
handleAddNaluTrack(payload, isIframe, dts, cts) {
// 子类实现
}
// just for mp4
handleAddAudioTrack(payload, dts) {
// 子类实现
}
handleAddTrack(arrayBuffer) {
// 子类实现
}
stopRecordAndSave() {
// 子类实现
}
startRecordingInterval() {
// 子类实现
}
isWasmMp4() {
return false;
}
stopRecordingInterval() {
if (this.recordingInterval) {
clearInterval(this.recordingInterval);
}
this.recordingInterval = null;
}
getToTalByteLength() {
// 子类实现
return 0;
}
_reset() {
this.fileName = '';
this._isRecording = false;
this._recordingTimestamp = 0;
this.stopRecordingInterval();
}
initMetaData(arrayBuffer, codecId) {
let metaData;
const extraData = arrayBuffer.slice(5);
this.codecId = codecId;
this.metaInfo.avcc = extraData;
//
if (codecId === VIDEO_ENC_CODE.h264) {
metaData = parseAVCDecoderConfigurationRecord(extraData);
} else if (codecId === VIDEO_ENC_CODE.h265) {
metaData = parseHEVCDecoderVPSAndSPSAndPPS(extraData);
const metaData2 = parseHEVCDecoderConfigurationRecord$2(arrayBuffer);
metaData = Object.assign(metaData, metaData2);
}
//console.log('initMetaData-metaData', metaData);
if (metaData) {
if (metaData.vps) {
this.vps = metaData.vps;
}
if (metaData.pps) {
this.pps = metaData.pps;
}
if (metaData.sps) {
this.sps = metaData.sps;
}
if (metaData.presentWidth) {
this.metaInfo.presentWidth = metaData.presentWidth;
}
if (metaData.presentHeight) {
this.metaInfo.presentHeight = metaData.presentHeight;
}
if (metaData.codecWidth) {
this.metaInfo.codecWidth = metaData.codecWidth;
}
if (metaData.codecHeight) {
this.metaInfo.codecHeight = metaData.codecHeight;
}
if (metaData.timescale) {
this.metaInfo.timescale = metaData.timescale;
}
if (metaData.refSampleDuration) {
this.metaInfo.refSampleDuration = metaData.refSampleDuration;
}
if (metaData.videoType) {
this.metaInfo.videoType = metaData.videoType;
}
}
// console.log('initMetaData-metaInfo', this.metaInfo);
}
initAudioMetaData(arrayBuffer, codecId) {
this.audioCodeId = codecId;
const size = arrayBuffer[0] >> 1 & 0x01;
let metaData = null;
if (codecId === AUDIO_ENC_CODE.AAC) {
metaData = readAACSpecificConfig(arrayBuffer);
if (metaData) {
if (metaData.channelCount) {
this.audioMetaInfo.channelCount = metaData.channelCount;
}
if (metaData.codec) {
this.audioMetaInfo.codec = metaData.codec;
}
if (metaData.originalCodec) {
this.audioMetaInfo.originalCodec = metaData.originalCodec;
}
if (metaData.config) {
this.audioMetaInfo.config = metaData.config;
}
if (metaData.sampleRate) {
this.audioMetaInfo.sampleRate = metaData.sampleRate;
}
if (this.audioMetaInfo.sampleRate && this.audioMetaInfo.timescale) {
// The decode result of an aac sample is 1024 PCM samples
this.audioMetaInfo.refSampleDuration = 1024 / this.audioMetaInfo.sampleRate * this.audioMetaInfo.timescale;
}
}
this.audioMetaInfo.depth = size ? 16 : 8;
this.audioMetaInfo.extraData = arrayBuffer.slice(2);
} else {
this.audioMetaInfo.depth = size === 0 ? 8 : 16;
}
}
initAudioAacExtraData(arrayBuffer) {
this.audioMetaInfo.extraData = new Uint8Array(arrayBuffer);
}
}
class RecordRTCLoader extends CommonLoader$1 {
constructor(player) {
super(player);
this.totalByteLength = 0;
this._startRecordingTimestamp = null;
player.debug.log('RecorderRTC', 'init');
}
_reset() {
super._reset();
this.totalByteLength = 0;
this._startRecordingTimestamp = null;
if (this.recorder) {
this.recorder.destroy();
this.recorder = null;
}
}
destroy() {
super.destroy();
this._reset();
this.player.debug.log('RecorderRTC', 'destroy');
}
getSeekableBlob(inputBlob) {
const reader = new EBML.Reader();
const decoder = new EBML.Decoder();
const tool = EBML.tools;
const fileReader = new FileReader();
const renderPromise = new Promise((res, rej) => {
fileReader.onload = function (e) {
const ebmlElms = decoder.decode(this.result);
ebmlElms.forEach(function (element) {
reader.read(element);
});
reader.stop();
const refinedMetadataBuf = tool.makeMetadataSeekable(reader.metadatas, reader.duration, reader.cues);
const body = this.result.slice(reader.metadataSize);
const newBlob = new Blob([refinedMetadataBuf, body], {
type: "video/webm"
});
res(newBlob);
};
fileReader.readAsArrayBuffer(inputBlob);
});
return renderPromise;
}
startRecord() {
const debug = this.player.debug;
const options = {
type: 'video',
mimeType: 'video/webm;codecs=h264',
timeSlice: 1000,
onTimeStamp: timestamp => {
debug.log('RecorderRTC', 'record timestamp :' + timestamp);
if (this._startRecordingTimestamp === null) {
this._startRecordingTimestamp = timestamp;
}
this._recordingTimestamp = (timestamp - this._startRecordingTimestamp) / 1000;
},
ondataavailable: blob => {
this.totalByteLength += blob.size;
debug.log('RecorderRTC', 'ondataavailable', blob.size);
},
disableLogs: !this.player._opt.debug
};
try {
// video
let stream = null;
// safari 浏览器不支持 captureStream() 方法。
// canvas
if (this.player.getRenderType() === RENDER_TYPE.canvas) {
stream = this.player.video.$videoElement.captureStream(25);
} else {
// video
// wcs or wasm decode
if (this.player.video.mediaStream) {
stream = this.player.video.mediaStream;
} else {
if (this.player.isOldHls()) {
// hls
stream = this.player.video.$videoElement.captureStream(25);
} else if (this.player._opt.useMSE) {
// mse decode
stream = this.player.video.$videoElement.captureStream(25);
} else if (this.player._opt.useWCS) {
// wcs
stream = this.player.video.$videoElement.captureStream(25);
} else if (this.player.isWebrtcH264()) {
// webrtc
stream = this.player.webrtc.videoStream;
} else if (this.player.isAliyunRtc()) {
// aliyun rtc
stream = this.player.video.$videoElement.captureStream(25);
}
}
}
if (stream) {
// audio
if (this.player.audio && this.player.audio.mediaStreamAudioDestinationNode && this.player.audio.mediaStreamAudioDestinationNode.stream && !this.player.audio.isStateSuspended() && this.player.audio.hasAudio && this.player._opt.hasAudio) {
const audioStream = this.player.audio.mediaStreamAudioDestinationNode.stream;
if (audioStream.getAudioTracks().length > 0) {
const audioTrack = audioStream.getAudioTracks()[0];
if (audioTrack && audioTrack.enabled) {
stream.addTrack(audioTrack);
}
}
}
this.recorder = RecordRTC_1(stream, options);
} else {
debug.error('RecorderRTC', 'startRecord error and can not create stream');
this.player.emitError(EVENTS.recordCreateError, 'can not create stream');
return;
}
} catch (e) {
debug.error('RecorderRTC', 'startRecord error', e);
this.player.emitError(EVENTS.recordCreateError, e);
return;
}
if (this.recorder) {
this._isRecording = true;
this.player.emit(EVENTS.recording, true);
this.recorder.startRecording();
debug.log('RecorderRTC', 'start recording');
this.player.emit(EVENTS.recordStart);
this.startRecordingInterval();
}
}
startRecordingInterval() {
this.stopRecordingInterval();
this.recordingInterval = window.setInterval(() => {
this.player.emit(EVENTS.recordingTimestamp, this._recordingTimestamp);
}, 1000);
}
stopRecordAndSave() {
let type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : RECORDING_TYPE.download;
let fileName = arguments.length > 1 ? arguments[1] : undefined;
return new Promise((resolve, reject) => {
if (!this.recorder || !this._isRecording) {
reject('recorder is not ready');
}
if (fileName) {
this.setFileName(fileName);
}
this.recorder.stopRecording(() => {
this.player.debug.log('RecorderRTC', 'stop recording');
const fileName = (this.fileName || now$2()) + '.' + FILE_SUFFIX.webm;
this.recorder.getBlob();
// this.getSeekableBlob(blob).then((seekableBlob) => {
// if (type === RECORDING_TYPE.blob) {
// resolve(seekableBlob);
// this.player.emit(EVENTS.recordBlob, seekableBlob);
// } else {
// resolve();
// RecordRTC.invokeSaveAsDialog(seekableBlob, fileName);
// }
// })
if (type === RECORDING_TYPE.blob) {
const blob = this.recorder.getBlob();
resolve(blob);
this.player.emit(EVENTS.recordBlob, blob);
} else {
resolve();
// saveAs(blob, fileName)
this.recorder.save(fileName);
}
this.player.emit(EVENTS.recordEnd);
this._reset();
this.player.emit(EVENTS.recording, false);
});
});
}
getToTalByteLength() {
return this.totalByteLength;
}
getTotalDuration() {
return this.recordTime;
}
getType() {
return FILE_SUFFIX.webm;
}
// 占个坑位
initMetaData() {}
}
class MP4$3 {
static init() {
MP4$3.types = {
avc1: [],
avcC: [],
hvc1: [],
hvcC: [],
btrt: [],
dinf: [],
dref: [],
esds: [],
ftyp: [],
// 视频类型
hdlr: [],
mdat: [],
// 视频数据
mdhd: [],
mdia: [],
mfhd: [],
minf: [],
moof: [],
//
moov: [],
// 视频信息(视频参数)
mp4a: [],
mvex: [],
mvhd: [],
sdtp: [],
stbl: [],
stco: [],
stsc: [],
stsd: [],
stsz: [],
stts: [],
tfdt: [],
tfhd: [],
traf: [],
// 视频参数(moov)中主要的子box 为track,每个track都是一个随时间变化的媒体序列,
// 时间单位为一个sample,可以是一帧数据,或者音频(注意,一帧音频可以分解成多个音频sample,所以音频一般用sample作为单位,而不用帧)
trak: [],
trun: [],
trex: [],
tkhd: [],
vmhd: [],
smhd: [],
'.mp3': [],
free: [],
edts: [],
elst: [],
stss: []
};
for (let name in MP4$3.types) {
if (MP4$3.types.hasOwnProperty(name)) {
MP4$3.types[name] = [name.charCodeAt(0), name.charCodeAt(1), name.charCodeAt(2), name.charCodeAt(3)];
}
}
let constants = MP4$3.constants = {};
// File Type Box,描述文件遵从的MP4规范与版本
constants.FTYP = new Uint8Array([0x69, 0x73, 0x6F, 0x6D,
// major_brand: isom
0x0, 0x0, 0x02, 0x0,
// minor_version: 0x20
0x69, 0x73, 0x6F, 0x6D,
// isom
0x69, 0x73, 0x6F, 0x32,
// iso
0x61, 0x76, 0x63, 0x31,
// avc1
0x6D, 0x70, 0x34, 0x31,
// MP4
0x00, 0x00, 0x00, 0x00]);
constants.STSD_PREFIX = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0x00, 0x00, 0x00, 0x01 // entry_count
]);
constants.STTS = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0x00, 0x00, 0x00, 0x00 // entry_count
]);
constants.STSC = constants.STCO = constants.STTS;
constants.STSZ = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0x00, 0x00, 0x00, 0x00,
// sample_size
0x00, 0x00, 0x00, 0x00 // sample_count
]);
constants.HDLR_VIDEO = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0x00, 0x00, 0x00, 0x00,
// pre_defined
0x76, 0x69, 0x64, 0x65,
// handler_type: 'vide'
0x00, 0x00, 0x00, 0x00,
// reserved: 3 * 4 bytes
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x56, 0x69, 0x64, 0x65, 0x6F, 0x48, 0x61, 0x6E, 0x64, 0x6C, 0x65, 0x72, 0x00 // name: VideoHandler
]);
constants.HDLR_AUDIO = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0x00, 0x00, 0x00, 0x00,
// pre_defined
0x73, 0x6F, 0x75, 0x6E,
// handler_type: 'soun'
0x00, 0x00, 0x00, 0x00,
// reserved: 3 * 4 bytes
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x6F, 0x75, 0x6E, 0x64, 0x48, 0x61, 0x6E, 0x64, 0x6C, 0x65, 0x72, 0x00 // name: SoundHandler
]);
constants.DREF = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0x00, 0x00, 0x00, 0x01,
// entry_count
0x00, 0x00, 0x00, 0x0C,
// entry_size
0x75, 0x72, 0x6C, 0x20,
// type 'url '
0x00, 0x00, 0x00, 0x01 // version(0) + flags
]);
// Sound media header
constants.SMHD = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0x00, 0x00, 0x00, 0x00 // balance(2) + reserved(2)
]);
// video media header
constants.VMHD = new Uint8Array([0x00, 0x00, 0x00, 0x01,
// version(0) + flags
0x00, 0x00,
// graphicsmode: 2 bytes
0x00, 0x00, 0x00, 0x00,
// opcolor: 3 * 2 bytes
0x00, 0x00]);
}
// Generate a box
// Type是box的类型
// box的参数除了第一个为类型,其他参数都需要是二进制的arraybuffer类型。
static box(type) {
// box前8位为预留位,这8位中前4位为数据size,
// 当size值为0时,表示该box为文件的最后一个box(仅存在于mdat box中),
// 当size值为1时,表示该box的size为large size(8位)
// 真正的box size要在largesize中得到(同样仅存在于mdat box中)。
// 后4位为前面box type的Unicode编码。当type是uuid时,代表Box中的数据是用户自定义扩展类型。
let size = 8;
let result = null;
// 方法中的第三行表示获取参数中除去第一个参数的其他参数
let datas = Array.prototype.slice.call(arguments, 1);
let arrayCount = datas.length;
for (let i = 0; i < arrayCount; i++) {
size += datas[i].byteLength;
}
// Box由header和body组成
result = new Uint8Array(size);
// 以32位的4字节整数存储方式存储到内存,
// 开头4个字节(32位)为box size。
result[0] = size >>> 24 & 0xFF; // size
result[1] = size >>> 16 & 0xFF;
result[2] = size >>> 8 & 0xFF;
result[3] = size & 0xFF;
// 后面紧跟的4位为box的类型
result.set(type, 4); // type
let offset = 8;
// Box body可以由数据组成,也可以由子box组成。
for (let i = 0; i < arrayCount; i++) {
// data body
result.set(datas[i], offset);
offset += datas[i].byteLength;
}
return result;
}
// emit ftyp & moov
static generateInitSegment(meta, trakList, mdatBytes) {
// Ftypbox 是一个由四个字符组成的码字,用来表示编码类型、兼容协议或者媒体文件的用途。
// 在普通MP4文件中,ftyp box有且仅有一个,在文件的开始位置。
let ftyp = MP4$3.box(MP4$3.types.ftyp, MP4$3.constants.FTYP);
//
let free = MP4$3.box(MP4$3.types.free);
// allocate mdatbox init fps = 25
let offset = 8;
let mdatbox = new Uint8Array();
// 所有的长度。。。。。
// Mdat box中,可能会使用到box的large size,当数据足够大,无法用4个字节来描述时,便会使用到large size。
// 在读取MP4文件时,当mdat box的size位为1时,真正的box size在large size中,
// 同样在写mp4文件时,若需要large size,需要将box size位配置为1。
if (mdatBytes + offset >= Math.pow(2, 32) - 1) {
//large size
offset = 16;
mdatbox = new Uint8Array(mdatBytes + offset);
mdatbox.set(new Uint8Array([0x00, 0x00, 0x00, 0x01]), 0);
// 视频数据(mdat)
mdatbox.set(MP4$3.types.mdat, 4);
mdatbox.set(new Uint8Array([mdatBytes + 8 >>> 56 & 0xFF, mdatBytes + 8 >>> 48 & 0xFF, mdatBytes + 8 >>> 40 & 0xFF, mdatBytes + 8 >>> 32 & 0xFF, mdatBytes + 8 >>> 24 & 0xFF, mdatBytes + 8 >>> 16 & 0xFF, mdatBytes + 8 >>> 8 & 0xFF, mdatBytes + 8 & 0xFF]), 8);
} else {
mdatbox = new Uint8Array(mdatBytes + offset);
mdatbox[0] = mdatBytes + 8 >>> 24 & 0xFF;
mdatbox[1] = mdatBytes + 8 >>> 16 & 0xFF;
mdatbox[2] = mdatBytes + 8 >>> 8 & 0xFF;
mdatbox[3] = mdatBytes + 8 & 0xFF;
//视频数据(mdat)
mdatbox.set(MP4$3.types.mdat, 4);
}
// Write samples into mdatbox
for (let i = 0; i < trakList.length; i++) {
let trak = trakList[i];
// duration
trak.duration = trak.refSampleDuration * trak.sequenceNumber;
for (let j = 0; j < trak.sequenceNumber; j++) {
// 遍历 samples
let sample = trak.samples[j];
// sample
sample.chunkOffset = ftyp.byteLength + free.byteLength + offset;
// 合并 data 数据。。。。。。
let data = sample.data;
mdatbox.set(data, offset);
offset += data.byteLength;
}
}
// Moov box中存放着媒体信息,上面提到的stbl里存放帧信息,属于媒体信息,也在moov box里。
// Moov box 用来描述媒体数据。
// Moov box 主要包含 mvhd、trak、mvex三种子box。
let moov = MP4$3.moov(meta, trakList);
// 视频类型(ftyp)、视频数据(mdat)、视频信息(moov)
let result = new Uint8Array(ftyp.byteLength + moov.byteLength + mdatbox.byteLength + free.byteLength);
// ftyp 视频类型
result.set(ftyp, 0);
// free
result.set(free, ftyp.byteLength);
// mdat 视频数据
result.set(mdatbox, ftyp.byteLength + free.byteLength);
// moov 视频信息
result.set(moov, ftyp.byteLength + mdatbox.byteLength + free.byteLength);
return result;
}
// Movie metadata box
// 媒体的metadata信息,有且仅有一个,位于moov box中。
// Moov box 主要包含 mvhd、trak、mvex三种子box。
// 视频参数(moov)中主要的子box 为track,
// 每个track都是一个随时间变化的媒体序列,时间单位为一个sample,可以是一帧数据,
// 或者音频(注意,一帧音频可以分解成多个音频sample,所以音频一般用sample作为单位,而不用帧)。
// Sample按照事件顺序排列。track里面的每个sample通过引用关联到一个sample description。
// 这个sample descriptios定义了怎样解码这个sample,例如使用的压缩算法。(注:在目前的使用中,该值为1)
static moov(meta, trakList) {
let timescale = meta.timescale;
let duration = meta.duration;
let trakLen = trakList.length;
// Mvhd box定义了整个文件的特性
let mvhd = MP4$3.mvhd(timescale, duration);
let trakArrayBuffer = new Uint8Array();
for (let i = 0; i < trakLen; i++) {
let trak = MP4$3.trak(trakList[i]);
let arrayBuffer = new Uint8Array(trak.byteLength + trakArrayBuffer.byteLength);
arrayBuffer.set(trakArrayBuffer, 0);
arrayBuffer.set(trak, trakArrayBuffer.byteLength);
trakArrayBuffer = new Uint8Array(arrayBuffer.byteLength);
trakArrayBuffer.set(arrayBuffer, 0);
}
return MP4$3.box(MP4$3.types.moov, mvhd, trakArrayBuffer);
}
// Movie header box
// 这里写mp4时需要传入的参数为Time scale 和 Duration,其他的使用默认值即可。
// MP4文件的整体信息,跟具体的视频流、音频流无关,比如创建时间、文件时长等。
// mvhd针对整个影片
// 这里写mp4时需要传入的参数为Time scale 和 Duration,其他的使用默认值即可。
static mvhd(timescale, duration) {
//
return MP4$3.box(MP4$3.types.mvhd, new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0xCE, 0xBA, 0xFD, 0xA8,
// creation_time 文件创建时间;
0xCE, 0xBA, 0xFD, 0xA8,
// modification_time 文件修改时间;
timescale >>> 24 & 0xFF,
// timescale: 4 bytes 一秒包含的时间单位(整数)。举个例子,如果timescale等于1000,那么,一秒包含1000个时间单位(后面track等的时间,都要用这个来换算,比如track的duration为10,000,那么,track的实际时长为10,000/1000=10s);
timescale >>> 16 & 0xFF, timescale >>> 8 & 0xFF, timescale & 0xFF, duration >>> 24 & 0xFF,
// duration: 4 bytes 影片时长(整数),根据文件中的track的信息推导出来,等于时间最长的track的duration
duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF, 0x00, 0x01, 0x00, 0x00,
// Preferred rate: 1.0 推荐的播放速率,32位整数,高16位、低16位分别代表整数部分、小数部分([16.16]),举例 0x0001 0000 代表1.0,正常播放速度;
0x01, 0x00, 0x00, 0x00,
// Preferred Volume(1.0, 2bytes) + reserved(2bytes) 播放音量,16位整数,高8位、低8位分别代表整数部分、小数部分([8.8]),举例 0x01 00 表示 1.0,即最大音量;
0x00, 0x00, 0x00, 0x00,
// reserved: 4 + 4 bytes
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
// ----begin composition matrix----
0x00, 0x00, 0x00, 0x00,
// 视频的转换矩阵,一般可以忽略不计;
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
// ----end composition matrix----
0x00, 0x00, 0x00, 0x00,
// ----begin pre_defined 6 * 4 bytes----
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// ----end pre_defined 6 * 4 bytes----
0x00, 0x00, 0x00, 0x03 // next_track_ID: 4 bytes 3 32位整数,非0,一般可以忽略不计。当要添加一个新的track到这个影片时,可以使用的track id,必须比当前已经使用的track id要大。也就是说,添加新的track时,需要遍历所有track,确认可用的track id;
]));
}
// Track box
// 一个Track box定义了movie中的一个track。一部movie可以包含一个或多个tracks,它们之间相互独立,各自有各自的时间和空间信息。每个track box 都有与之关联的mdat box。
// 包含媒体数据引用和描述
// 包含modifier track
// 流媒体协议的打包信息(hint trak),引用或者复用对应的媒体sample data。
// Hint tracks和modifier tracks必须保证完整性,同时和至少一个media track一起存在。
// 换句话说,即使hint tracks复制了对应的媒体sample data,media tracks 也不能从一部hinted movie中删除。
// 写mp4时仅用到第一个目的,所以这里只介绍媒体数据的引用和描述。
// 一个trak box一般主要包含了tkhd box、 edts box 、mdia box
static trak(meta) {
return MP4$3.box(MP4$3.types.trak, MP4$3.tkhd(meta), MP4$3.mdia(meta));
}
// Track header box
// 用来描述trak box的header 信息,定义了一个trak的时间、空间、音量信息。
static tkhd(meta) {
let trackId = meta.id,
duration = meta.duration;
let width = meta.presentWidth,
height = meta.presentHeight;
return MP4$3.box(MP4$3.types.tkhd, new Uint8Array([0x00, 0x00, 0x00, 0x0F,
// version(0) + flags tkhd box的版本;
0xCE, 0xBA, 0xFD, 0xA8,
// creation_time 当前track的创建时间;
0xCE, 0xBA, 0xFD, 0xA8,
// modification_time 当前track的最近修改时间;
trackId >>> 24 & 0xFF,
// track_ID: 4 bytes 当前track的唯一标识,不能为0,不能重复;
trackId >>> 16 & 0xFF, trackId >>> 8 & 0xFF, trackId & 0xFF, 0x00, 0x00, 0x00, 0x00,
// reserved: 4 bytes
duration >>> 24 & 0xFF,
// duration: 4 bytes 当前track的完整时长(需要除以timescale得到具体秒数);
duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF, 0x00, 0x00, 0x00, 0x00,
// reserved: 2 * 4 bytes
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// layer(2bytes) + alternate_group(2bytes) layer 视频轨道的叠加顺序,数字越小越靠近观看者,比如1比2靠上,0比1靠上; alternate_group 当前track的分组ID,alternate_group值相同的track在同一个分组里面。同个分组里的track,同一时间只能有一个track处于播放状态。当alternate_group为0时,表示当前track没有跟其他track处于同个分组。一个分组里面,也可以只有一个track;
0x00, 0x00, 0x00, 0x00,
// volume(2bytes) + reserved(2bytes) audio track的音量,介于0.0~1.0之间;
0x00, 0x01, 0x00, 0x00,
// ----begin composition matrix----
0x00, 0x00, 0x00, 0x00,
// 视频的变换矩阵; 36
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
// ----end composition matrix----
width >>> 8 & 0xFF,
// width and height 视频的宽高; 4
width & 0xFF, 0x00, 0x00, height >>> 8 & 0xFF,
// width and height 视频的宽高; 4
height & 0xFF, 0x00, 0x00]));
}
static edts(meta, i) {
return MP4$3.box(MP4$3.types.edts, MP4$3.elst(meta, i));
}
// 该box为edst box的唯一子box,不是所有的MP4文件都有edst box,这个box是使其对应的trak box的时间戳产生偏移。
// 暂时未发现需要该偏移量的地方,编码时也未对该box进行编码。
static elst(meta, i) {
let videoDelayDuration = 0;
for (let j = 0; j < i; j++) {
if (meta[j].type === 'video') {
videoDelayDuration += meta[j].duration;
}
}
let duration = meta[i].duration;
if (videoDelayDuration === 0) {
videoDelayDuration = meta[i].refSampleDuration;
}
return MP4$3.box(MP4$3.types.elst, new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, videoDelayDuration >>> 24 & 0xFF,
// SampleDuration: 4 bytes
videoDelayDuration >>> 16 & 0xFF, videoDelayDuration >>> 8 & 0xFF, videoDelayDuration & 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
//media_time
0x00, 0x01, 0x00, 0x00,
// media_rate(2byte) + Media rate fraction(3byte)
duration >>> 24 & 0xFF,
// Duration: 4 bytes
duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00]));
}
// Media Box
// 该box定义了trak box的类型和sample的信息。
static mdia(meta) {
return MP4$3.box(MP4$3.types.mdia, MP4$3.mdhd(meta), MP4$3.hdlr(meta), MP4$3.minf(meta));
}
// Media header box
// mdhd box 定义了该box的timescale
// 和duration(注:这里的这两个参数与前面说的mvhd有区别,这里的这两个参数都是以一个sample为时间单位的,
// 例:在只有一个视频trak的情况下,mvhd的timescale为1000,一个sample的duration为40
// ,那么这里的timescale为1000/40,同理这里的duration算法与之一样理解。)
static mdhd(meta) {
let timescale = meta.timescale / meta.refSampleDuration;
let duration = timescale * meta.duration / meta.timescale;
return MP4$3.box(MP4$3.types.mdhd, new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0xCE, 0xBA, 0xFD, 0xA8,
// creation_time
0xCE, 0xBA, 0xFD, 0xA8,
// modification_time
timescale >>> 24 & 0xFF,
// timescale: 4 bytes
timescale >>> 16 & 0xFF, timescale >>> 8 & 0xFF, timescale & 0xFF, duration >>> 24 & 0xFF,
// duration: 4 bytes
duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF, 0x55, 0xC4,
// language: und (undetermined)
0x00, 0x00 // pre_defined = 0
]));
}
// Media handler reference box
// 声明当前track的类型,以及对应的处理器(handler)。
/**
* vide(0x76 69 64 65),video track;
soun(0x73 6f 75 6e),audio track;
hint(0x68 69 6e 74),hint track;
//Hdlr box 定义了这段trak的媒体处理组件,以下图会更清晰的解释这个box
* @param meta
* @returns {null}
*/
static hdlr(meta) {
let data = null;
data = MP4$3.constants.HDLR_VIDEO;
return MP4$3.box(MP4$3.types.hdlr, data);
}
// Media infomation box
// 该box也是上面的mdia box的子box,其主要用来描述该trak的具体的媒体处理组件内容的。
static minf(meta) {
let xmhd = null;
xmhd = MP4$3.box(MP4$3.types.vmhd, MP4$3.constants.VMHD);
return MP4$3.box(MP4$3.types.minf, xmhd, MP4$3.dinf(), MP4$3.stbl(meta));
}
// Data infomation box
// dinf box 定义了该trak的数据信息,包括了数据的引用方式,数据的存储方式等。
// dinf box 用来定义媒体处理组件如何获取媒体数据的
static dinf() {
return MP4$3.box(MP4$3.types.dinf, MP4$3.box(MP4$3.types.dref, MP4$3.constants.DREF));
}
// Sample table box
// Sample Table Box(stbl)是上面minf的子box之一,用来定义存放时间/偏移的映射关系,数据信息都在以下子box中
// 在普通mp4中,在获取数据之前,需要解析每个帧数据所在位置,每个帧数据都存放在mdat中,而这些帧的信息全部存放在stbl box 中,
// 所以,若要mp4文件能够正常播放,需要在写mp4文件时,将所有的帧数据信息写入 stbl box中。
// MP4文件的媒体数据部分在mdat box里,而stbl则包含了这些媒体数据的索引以及时间信息,了解stbl对解码、渲染MP4文件很关键。
// 在MP4文件中,媒体数据被分成多个chunk,每个chunk可包含多个sample,而sample则由帧组成(通常1个sample对应1个帧),关系如下:
static stbl(meta) {
let sampleList = meta.samples;
let sampleToChunk = [{
No: 1,
num: 0,
sampleDelte: 1,
chunkNo: 1,
duration: sampleList[0].duration
}];
let durationList = [sampleList[0].duration];
let len = sampleList.length;
for (let i = 0; i < len; i++) {
for (let j = 0; j < sampleToChunk.length; j++) {
if (sampleList[i].duration === sampleToChunk[j].duration) {
sampleToChunk[j].num++;
} else {
if (durationList.indexOf(sampleList[i].duration) < 0) {
durationList.push(sampleList[i].duration);
sampleToChunk.push({
No: 2,
num: 0,
sampleDelte: 1,
chunkNo: i + 1,
duration: sampleList[i].duration
});
}
}
}
}
return MP4$3.box(MP4$3.types.stbl,
// type: stbl
MP4$3.stsd(meta),
// Sample Description Table Sample Description Box用来描述数据的格式,比如视频格式为avc,比如音频格式为aac
MP4$3.stts(sampleToChunk),
// Time-To-Sample 时间戳和Sample序号映射表
MP4$3.stss(sampleList),
// 关键帧序号,该box存在于video trak,因为audio trak 中以sample为单位,但多个sample才组成一帧音频,所以在audio trak中无需该box。
MP4$3.stsc(sampleToChunk),
//Sample-To-Chunk Sample to chunk 的映射表。这个算法比较巧妙,在多个chunk时,该算法较为复杂。在本次使用中未考虑多个chunk的状态,仅考虑整个文件单个chunk的情况。
MP4$3.stsz(sampleList),
// Sample size Sample Size Boxes 每个Sample大小的表。Stz2是另一种sample size的存储算法,更节省空间,使用时使用其中一种即可,这里使用stsz。原因简单,因为算法容易。
MP4$3.stco(sampleToChunk, sampleList) // Chunk offset co64: 每个Chunk位置偏移表,sample的偏移可根据其他box推算出来,co64是指64位的chunk偏移,暂时只使用到32位的,因此这里使用stco即可。
);
}
// 每个sample的时长;
// stts包含了DTS到sample number的映射表,主要用来推导每个帧的时长。
static stts(sampleToChunk) {
let sampleToChunkLen = sampleToChunk.length;
let stts = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
sampleToChunkLen >>> 24 & 0xFF,
// entry_count: 4 bytes tts 中包含的entry条目数;
sampleToChunkLen >>> 16 & 0xFF, sampleToChunkLen >>> 8 & 0xFF, sampleToChunkLen & 0xFF]);
let offset = stts.byteLength,
sttsInfo = new Uint8Array(offset + sampleToChunkLen * 8);
sttsInfo.set(stts, 0);
for (let index = 0; index < sampleToChunkLen; index++) {
sttsInfo.set(new Uint8Array([sampleToChunk[index].num >>> 24 & 0xFF,
// samplesPerChunk: 4 bytes
sampleToChunk[index].num >>> 16 & 0xFF, sampleToChunk[index].num >>> 8 & 0xFF, sampleToChunk[index].num & 0xFF, sampleToChunk[index].sampleDelte >>> 24 & 0xFF,
// samplesDescription index: 4 bytes
sampleToChunk[index].sampleDelte >>> 16 & 0xFF, sampleToChunk[index].sampleDelte >>> 8 & 0xFF, sampleToChunk[index].sampleDelte & 0xFF]), offset);
offset += 8;
}
return MP4$3.box(MP4$3.types.stts, sttsInfo);
}
// 哪些sample是关键帧;
// mp4文件中,关键帧所在的sample序号。如果没有stss的话,所有的sample中都是关键帧。
static stss(mdatDataList) {
let keyFrameMap = [],
len = mdatDataList.length;
for (let i = 0; i < len; i++) {
if (mdatDataList[i].isKeyframe === true) {
keyFrameMap.push(i + 1);
}
}
let keyFrameLen = keyFrameMap.length;
let stss = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
keyFrameLen >>> 24 & 0xFF,
// entry_count: 4 bytes entry的条目数,可以认为是关键帧的数目;
keyFrameLen >>> 16 & 0xFF, keyFrameLen >>> 8 & 0xFF, keyFrameLen & 0xFF]);
let offset = stss.byteLength,
stssInfo = new Uint8Array(offset + keyFrameLen * 4);
stssInfo.set(stss, 0);
for (let index = 0; index < keyFrameLen; index++) {
stssInfo.set(new Uint8Array([keyFrameMap[index] >>> 24 & 0xFF,
// entry_count: 4 bytes
keyFrameMap[index] >>> 16 & 0xFF, keyFrameMap[index] >>> 8 & 0xFF, keyFrameMap[index] & 0xFF]), offset);
offset += 4;
}
return MP4$3.box(MP4$3.types.stss, stssInfo);
}
// 每个thunk中包含几个sample;
// sample 以 chunk 为单位分成多个组。chunk的size可以是不同的,chunk里面的sample的size也可以是不同的。
static stsc(sampleToChunk) {
let sampleToChunkLen = sampleToChunk.length;
let stsc = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
sampleToChunkLen >>> 24 & 0xFF,
// entry_count: 4 bytes 有多少个表项(每个表项,包含first_chunk、samples_per_chunk、sample_description_index信息);
sampleToChunkLen >>> 16 & 0xFF, sampleToChunkLen >>> 8 & 0xFF, sampleToChunkLen & 0xFF]);
let offset = stsc.byteLength,
stscInfo = new Uint8Array(offset + sampleToChunkLen * 12);
stscInfo.set(stsc, 0);
for (let index = 0; index < sampleToChunkLen; index++) {
let firstChunk = sampleToChunk[index].chunkNo,
samplesPerChunk = sampleToChunk[index].num,
sampleDelte = sampleToChunk[index].sampleDelte;
stscInfo.set(new Uint8Array([firstChunk >>> 24 & 0xFF,
// firstChunk: 4 bytes 当前表项中,对应的第一个chunk的序号;
firstChunk >>> 16 & 0xFF, firstChunk >>> 8 & 0xFF, firstChunk & 0xFF, samplesPerChunk >>> 24 & 0xFF,
// samplesPerChunk: 4 bytes 每个chunk包含的sample数;
samplesPerChunk >>> 16 & 0xFF, samplesPerChunk >>> 8 & 0xFF, samplesPerChunk & 0xFF, sampleDelte >>> 24 & 0xFF,
// samplesDescription index: 4 bytes 指向 stsd 中 sample description 的索引值(参考stsd小节);
sampleDelte >>> 16 & 0xFF, sampleDelte >>> 8 & 0xFF, sampleDelte & 0xFF]), offset);
offset += 12;
}
return MP4$3.box(MP4$3.types.stsc, stscInfo);
}
// 每个sample的size(单位是字节);
// 每个sample的大小(字节),根据 sample_size 字段,可以知道当前track包含了多少个sample(或帧)。
static stsz(mdatDataList) {
let len = mdatDataList.length;
let stsz = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0x00, 0x00, 0x00, 0x00,
// sample size 当所有sample的size都一样时,该值为sample的size ,否则为0
len >>> 24 & 0xFF,
// sample count: 4 bytes 当前track里面的sample数目。如果 sample_size==0,那么,sample_count 等于下面entry的条目;
len >>> 16 & 0xFF, len >>> 8 & 0xFF, len & 0xFF]);
let offset = stsz.byteLength,
stszInfo = new Uint8Array(offset + len * 4);
stszInfo.set(stsz, 0);
for (let i = 0; i < len; i++) {
let data = mdatDataList[i].data;
let dataLen = data.byteLength;
stszInfo.set(new Uint8Array([dataLen >>> 24 & 0xFF,
//per sample size: 4 bytes
dataLen >>> 16 & 0xFF, dataLen >>> 8 & 0xFF, dataLen & 0xFF]), offset);
offset += 4;
}
return MP4$3.box(MP4$3.types.stsz, stszInfo);
}
// thunk在文件中的偏移;
// 存储了该 track 中每个 chunk 在文件中的偏移。
static stco(sampleToChunk, mdatDataList) {
let offset = mdatDataList[0].chunkOffset;
return MP4$3.box(MP4$3.types.stco, new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0x00, 0x00, 0x00, 0x01,
// entry_count: 4 bytes // 默认只有视频时,只有一段chunk
offset >>> 24 & 0xFF,
// samplesPerChunk: 4 bytes
offset >>> 16 & 0xFF, offset >>> 8 & 0xFF, offset & 0xFF]));
}
// Sample description box
// 给出视频、音频的编码、宽高、音量等信息,以及每个sample中包含多少个frame;
static stsd(meta) {
if (meta.type === 'audio') {
//
if (meta.codec === 'mp3') {
return MP4$3.box(MP4$3.types.stsd, MP4$3.constants.STSD_PREFIX, MP4$3.mp3(meta));
}
// else: aac -> mp4a
return MP4$3.box(MP4$3.types.stsd, MP4$3.constants.STSD_PREFIX, MP4$3.mp4a(meta));
} else {
if (meta.videoType === 'avc') {
return MP4$3.box(MP4$3.types.stsd, MP4$3.constants.STSD_PREFIX, MP4$3.avc1(meta));
} else {
return MP4$3.box(MP4$3.types.stsd, MP4$3.constants.STSD_PREFIX, MP4$3.hvc1(meta));
}
//
}
}
static mp3(meta) {
let channelCount = meta.channelCount;
let sampleRate = meta.sampleRate;
let data = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// reserved(4)
0x00, 0x00, 0x00, 0x01,
// reserved(2) + data_reference_index(2)
0x00, 0x00, 0x00, 0x00,
// reserved: 2 * 4 bytes
0x00, 0x00, 0x00, 0x00, 0x00, channelCount,
// channelCount(2)
0x00, 0x10,
// sampleSize(2)
0x00, 0x00, 0x00, 0x00,
// reserved(4)
sampleRate >>> 8 & 0xFF,
// Audio sample rate
sampleRate & 0xFF, 0x00, 0x00]);
return MP4$3.box(MP4$3.types['.mp3'], data);
}
static mp4a(meta) {
let channelCount = meta.channelCount;
let sampleRate = meta.sampleRate;
let data = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// reserved(4)
0x00, 0x00, 0x00, 0x01,
// reserved(2) + data_reference_index(2)
0x00, 0x00, 0x00, 0x00,
// reserved: 2 * 4 bytes
0x00, 0x00, 0x00, 0x00, 0x00, channelCount,
// channelCount(2)
0x00, 0x10,
// sampleSize(2)
0x00, 0x00, 0x00, 0x00,
// reserved(4)
sampleRate >>> 8 & 0xFF,
// Audio sample rate
sampleRate & 0xFF, 0x00, 0x00]);
return MP4$3.box(MP4$3.types.mp4a, data, MP4$3.esds(meta));
}
static esds(meta) {
let config = meta.config || [];
let configSize = config.length;
let data = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version 0 + flags
0x03,
// descriptor_type
0x17 + configSize,
// length3
0x00, 0x01,
// es_id
0x00,
// stream_priority
0x04,
// descriptor_type
0x0F + configSize,
// length
0x40,
// codec: mpeg4_audio
0x15,
// stream_type: Audio
0x00, 0x00, 0x00,
// buffer_size
0x00, 0x00, 0x00, 0x00,
// maxBitrate
0x00, 0x00, 0x00, 0x00,
// avgBitrate
0x05 // descriptor_type
].concat([configSize]).concat(config).concat([0x06, 0x01, 0x02 // GASpecificConfig
]));
return MP4$3.box(MP4$3.types.esds, data);
}
static avc1(meta) {
let avcc = meta.avcc;
let width = meta.codecWidth,
height = meta.codecHeight;
let data = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// reserved(4)
0x00, 0x00, 0x00, 0x01,
// reserved(2) + data_reference_index(2)
0x00, 0x00, 0x00, 0x00,
// pre_defined(2) + reserved(2)
0x00, 0x00, 0x00, 0x00,
// pre_defined: 3 * 4 bytes
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, width >>> 8 & 0xFF,
// width: 2 bytes
width & 0xFF, height >>> 8 & 0xFF,
// height: 2 bytes
height & 0xFF, 0x00, 0x48, 0x00, 0x00,
// horizresolution: 4 bytes
0x00, 0x48, 0x00, 0x00,
// vertresolution: 4 bytes
0x00, 0x00, 0x00, 0x00,
// reserved: 4 bytes
0x00, 0x01,
// frame_count
0x0D,
// strlen 10bytes
0x6a, 0x65, 0x73, 0x73,
// compressorname: 32 bytes
0x69, 0x62, 0x75, 0x63, 0x61, 0x2d, 0x70, 0x72, 0x6f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18,
// depth
0xFF, 0xFF // pre_defined = -1
]);
return MP4$3.box(MP4$3.types.avc1, data, MP4$3.box(MP4$3.types.avcC, avcc));
}
// hvc
static hvc1(meta) {
let avcc = meta.avcc;
const width = meta.codecWidth;
const height = meta.codecHeight;
let data = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, width >>> 8 & 255, width & 255, height >>> 8 & 255, height & 255, 0, 72, 0, 0, 0, 72, 0, 0, 0, 0, 0, 0, 0, 1, 13, 106, 101, 115, 115, 105, 98, 117, 99, 97, 45, 112, 114, 111, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 255, 255]);
return MP4$3.box(MP4$3.types.hvc1, data, MP4$3.box(MP4$3.types.hvcC, avcc));
}
// Movie Extends box
static mvex(meta) {
return MP4$3.box(MP4$3.types.mvex, MP4$3.trex(meta));
}
// Track Extends box
// 用来给 fMP4 的 sample 设置各种默认值,比如时长、大小等。
static trex(meta) {
let trackId = meta.id;
let data = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
trackId >>> 24 & 0xFF,
// track_ID 对应的 track 的 ID,比如video track、audio track 的ID;
trackId >>> 16 & 0xFF, trackId >>> 8 & 0xFF, trackId & 0xFF, 0x00, 0x00, 0x00, 0x01,
// default_sample_description_index sample description 的默认 index(指向stsd);
0x00, 0x00, 0x00, 0x00,
// default_sample_duration sample 默认时长,一般为0;
0x00, 0x00, 0x00, 0x00,
// default_sample_size sample 默认大小,一般为0;
0x00, 0x01, 0x00, 0x01 // default_sample_flags sample 的默认flag,一般为0;
]);
return MP4$3.box(MP4$3.types.trex, data);
}
// Movie fragment box
// moof是个container box,相关 metadata 在内嵌box里,比如 mfhd、 tfhd、trun 等。
static moof(meta, baseMediaDecodeTime) {
return MP4$3.box(MP4$3.types.moof, MP4$3.mfhd(meta.sequenceNumber), MP4$3.traf(meta, baseMediaDecodeTime));
}
// 结构比较简单,sequence_number 为 movie fragment 的序列号。根据 movie fragment 产生的顺序,从1开始递增。
static mfhd(sequenceNumber) {
let data = new Uint8Array([0x00, 0x00, 0x00, 0x00, sequenceNumber >>> 24 & 0xFF,
// sequence_number: int32
sequenceNumber >>> 16 & 0xFF, sequenceNumber >>> 8 & 0xFF, sequenceNumber & 0xFF]);
return MP4$3.box(MP4$3.types.mfhd, data);
}
// Track fragment box
// 对 fmp4 来说,数据被氛围多个 movie fragment。一个 movie fragment 可包含多个track fragment(每个 track 包含0或多个 track fragment)。每个 track fragment 中,可以包含多个该 track 的 sample。
// 每个 track fragment 中,包含多个 track run,每个 track run 代表一组连续的 sample。
static traf(meta, baseMediaDecodeTime) {
let trackId = meta.id;
// Track fragment header box
// tfhd 用来设置 track fragment 中 的 sample 的 metadata 的默认值。
let tfhd = MP4$3.box(MP4$3.types.tfhd, new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) & flags
trackId >>> 24 & 0xFF,
// track_ID
trackId >>> 16 & 0xFF, trackId >>> 8 & 0xFF, trackId & 0xFF]));
// Track Fragment Decode Time
let tfdt = MP4$3.box(MP4$3.types.tfdt, new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) & flags
baseMediaDecodeTime >>> 24 & 0xFF,
// baseMediaDecodeTime: int32
baseMediaDecodeTime >>> 16 & 0xFF, baseMediaDecodeTime >>> 8 & 0xFF, baseMediaDecodeTime & 0xFF]));
let sdtp = MP4$3.sdtp(meta);
let trun = MP4$3.trun(meta, sdtp.byteLength + 16 + 16 + 8 + 16 + 8 + 8);
return MP4$3.box(MP4$3.types.traf, tfhd, tfdt, trun, sdtp);
}
// Sample Dependency Type box
static sdtp(meta) {
let samples = meta.samples || [];
let sampleCount = samples.length;
let data = new Uint8Array(4 + sampleCount);
// 0~4 bytes: version(0) & flags
for (let i = 0; i < sampleCount; i++) {
let flags = samples[i].flags;
data[i + 4] = flags.isLeading << 6 // is_leading: 2 (bit)
| flags.dependsOn << 4 // sample_depends_on
| flags.isDependedOn << 2 // sample_is_depended_on
| flags.hasRedundancy; // sample_has_redundancy
}
return MP4$3.box(MP4$3.types.sdtp, data);
}
// Track fragment run box
static trun(meta, offset) {
let samples = meta.samples || [];
let sampleCount = samples.length;
let dataSize = 12 + 16 * sampleCount;
let data = new Uint8Array(dataSize);
offset += 8 + dataSize;
data.set([0x00, 0x00, 0x0F, 0x01,
// version(0) & flags
sampleCount >>> 24 & 0xFF,
// sample_count
sampleCount >>> 16 & 0xFF, sampleCount >>> 8 & 0xFF, sampleCount & 0xFF, offset >>> 24 & 0xFF,
// data_offset
offset >>> 16 & 0xFF, offset >>> 8 & 0xFF, offset & 0xFF], 0);
for (let i = 0; i < sampleCount; i++) {
let duration = samples[i].duration;
let size = samples[i].size;
let flags = samples[i].flags;
let cts = samples[i].cts;
data.set([duration >>> 24 & 0xFF,
// sample_duration
duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF, size >>> 24 & 0xFF,
// sample_size
size >>> 16 & 0xFF, size >>> 8 & 0xFF, size & 0xFF, flags.isLeading << 2 | flags.dependsOn,
// sample_flags
flags.isDependedOn << 6 | flags.hasRedundancy << 4 | flags.isNonSync, 0x00, 0x00,
// sample_degradation_priority
cts >>> 24 & 0xFF,
// sample_composition_time_offset
cts >>> 16 & 0xFF, cts >>> 8 & 0xFF, cts & 0xFF], 12 + 16 * i);
}
return MP4$3.box(MP4$3.types.trun, data);
}
// Media Data Box,存放实际的媒体数据,一般有多个
// Mdat box 中包含了MP4文件的媒体数据,在文件中的位置可以在moov的前面,也可以在moov的后面,
// 因我们这里用到MP4文件格式用来写mp4文件,需要计算每一帧媒体数据在文件中的偏移量,为了方便计算,mdat放置moov前面。
// Mdat box数据格式单一,无子box。主要分为box header 和box body,box header中存放box size 和box type(mdat),box body中存放所有媒体数据,媒体数据以sample为数据单元。
// 这里使用时,视频数据中,每一个sample是一个视频帧,存放sample时,需要根据帧数据类型进行拼帧处理后存放。
static mdat(data) {
return MP4$3.box(MP4$3.types.mdat, data);
}
}
MP4$3.init();
class MP4RecorderLoader extends CommonLoader$1 {
constructor(player) {
super(player);
this.TAG_NAME = 'recorderMP4';
this._reset();
player.debug.log(this.TAG_NAME, 'init');
}
destroy() {
super.destroy();
this._reset();
this.player.debug.log(this.TAG_NAME, 'destroy');
}
_reset() {
super._reset();
this.totalDuration = 0;
this.totalAudioDuration = 0;
this.totalByteLength = 0;
this.totalAudioByteLength = 0;
this.bufferList = [];
this.audioBufferList = [];
this.cacheTrack = {};
this.audioCacheTrack = {};
this.sequenceNumber = 0;
this.audioSequenceNumber = 0;
}
startRecord() {
const debug = this.player.debug;
this._isRecording = true;
this.player.emit(EVENTS.recording, true);
debug.log(this.TAG_NAME, 'start recording');
this.player.emit(EVENTS.recordStart);
this.startRecordingInterval();
}
// override
startRecordingInterval() {
this.stopRecordingInterval();
this.recordingInterval = window.setInterval(() => {
this.player.emit(EVENTS.recordingTimestamp, this.getTotalDuration());
}, 1000);
}
formatFmp4Track(payload, isIframe, dts, cts) {
const track = {
id: 1,
sequenceNumber: ++this.sequenceNumber,
size: payload.byteLength,
dts: dts,
cts: cts,
isKeyframe: isIframe,
data: payload,
duration: 0,
flags: {
isLeading: 0,
dependsOn: isIframe ? 2 : 1,
isDependedOn: isIframe ? 1 : 0,
hasRedundancy: 0,
isNonSync: isIframe ? 0 : 1
}
};
return track;
}
formatAudioFmp4Track(payload, dts) {
const audioTrack = {
id: 2,
sequenceNumber: ++this.audioSequenceNumber,
size: payload.byteLength,
dts: dts,
pts: dts,
cts: 0,
data: new Uint8Array(payload),
duration: 0,
originalDts: dts,
flags: {
isLeading: 0,
dependsOn: 1,
isDependedOn: 0,
hasRedundancy: 0
}
};
return audioTrack;
}
handleAddNaluTrack(payload, isIframe, dts, cts) {
if (this.cacheTrack.id && dts >= this.cacheTrack.dts) {
// 主要就是为了这个duration 字段
this.cacheTrack.duration = dts - this.cacheTrack.dts;
this.handleAddFmp4Track(this.cacheTrack);
} else {
this.cacheTrack = {};
}
this.cacheTrack = this.formatFmp4Track(payload, isIframe, dts, cts);
}
handleAddAudioTrack(payload, dts) {
// if (this.audioCacheTrack.id && dts >= this.audioCacheTrack.dts) {
// this.audioCacheTrack.duration = dts - this.audioCacheTrack.dts;
// this.handleAddFmp4AudioTrack(this.audioCacheTrack);
// } else {
// this.audioCacheTrack = {};
// }
// this.audioCacheTrack = this.formatAudioFmp4Track(payload, dts);
}
//
handleAddFmp4Track(track) {
if (!this.isRecording) {
this.player.debug.error(this.TAG_NAME, 'handleAddFmp4Track, isRecording is false ');
return;
}
if (!(this.sps !== null && this.pps !== null) && this.isH264) {
this.player.debug.error(this.TAG_NAME, 'handleAddFmp4Track, is h264 and this.sps or this.pps is null ');
return;
}
if (!(this.sps !== null && this.pps !== null && this.vps !== null) && this.isH265) {
this.player.debug.error(this.TAG_NAME, 'handleAddFmp4Track, is h265 and this.sps or this.pps or this.vps is null ');
return;
}
const trackItem = Object.assign({}, track);
trackItem.pts = trackItem.dts + trackItem.cts;
const oldData = trackItem.data;
if (trackItem.isKeyframe) {
if (this.isH264) {
const drFlag = new Uint8Array(this.sps.byteLength + this.pps.byteLength);
drFlag.set(this.sps, 0);
drFlag.set(this.pps, this.sps.byteLength);
const newData = new Uint8Array(drFlag.byteLength + oldData.byteLength);
newData.set(drFlag, 0);
newData.set(oldData, drFlag.byteLength);
trackItem.data = newData;
} else if (this.isH265) {
const drFlag = new Uint8Array(this.sps.byteLength + this.pps.byteLength + this.vps.byteLength);
drFlag.set(this.vps, 0);
drFlag.set(this.sps, this.vps.byteLength);
drFlag.set(this.pps, this.vps.byteLength + this.sps.byteLength);
const newData = new Uint8Array(drFlag.byteLength + oldData.byteLength);
newData.set(drFlag, 0);
newData.set(oldData, drFlag.byteLength);
trackItem.data = newData;
}
}
trackItem.size = trackItem.data.byteLength;
this.totalDuration += trackItem.duration;
this.totalByteLength += trackItem.data.byteLength;
trackItem.duration = 0;
trackItem.originalDts = trackItem.dts;
delete trackItem.id;
delete trackItem.sequenceNumber;
this.bufferList.push(trackItem);
}
handleAddFmp4AudioTrack(track) {
const trackItem = Object.assign({}, track);
trackItem.pts = trackItem.dts + trackItem.cts;
trackItem.size = trackItem.data.byteLength;
this.totalAudioDuration += trackItem.duration;
this.totalAudioByteLength += trackItem.data.byteLength;
trackItem.duration = 0;
trackItem.originalDts = trackItem.dts;
delete trackItem.id;
delete trackItem.sequenceNumber;
this.audioBufferList.push(trackItem);
}
getTotalDuration() {
return this.totalDuration / 1000;
}
getType() {
return FILE_SUFFIX.mp4;
}
//
getToTalByteLength() {
return this.totalByteLength + this.totalAudioByteLength;
}
stopRecordAndSave() {
let type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : RECORDING_TYPE.download;
let fileName = arguments.length > 1 ? arguments[1] : undefined;
return new Promise((resolve, reject) => {
if (!this.isRecording) {
this.player.debug.error(this.TAG_NAME, 'stop recording fail, isRecording is false ');
return reject('stop recording fail, isRecording is false ');
}
if (this.bufferList.length === 0) {
this.player.debug.error(this.TAG_NAME, 'stop recording fail, this.bufferList.length is 0 ');
return reject('stop recording fail, this.bufferList.length is 0 ');
}
if (fileName) {
this.setFileName(fileName);
}
const trakItemVideo = {
id: 1,
type: 'video',
sps: this.sps,
pps: this.pps,
samples: this.bufferList,
sequenceNumber: this.bufferList.length,
length: 0,
addSampleNum: 1,
duration: 0,
...this.metaInfo
};
const trakItemAudio = {
id: 2,
type: 'audio',
sequenceNumber: this.audioBufferList.length,
samples: this.audioBufferList,
...this.audioMetaInfo
};
const trackList = [trakItemVideo];
if (trakItemAudio.samples.length > 0) {
trackList.push(trakItemAudio);
}
this.player.debug.log(this.TAG_NAME, `trackList length is ${trackList.length}`);
// console.error('trackList', trackList, this.totalByteLength, this.totalAudioByteLength, this.totalDuration, this.totalAudioDuration);
const metaBox = MP4$3.generateInitSegment({
timescale: 1000,
// flv 默认为1000
duration: this.totalDuration
}, trackList, this.totalByteLength + this.totalAudioByteLength);
this.player.debug.log(this.TAG_NAME, 'stop recording');
const blob = new Blob([metaBox], {
'type': 'application/octet-stream'
});
if (type === RECORDING_TYPE.blob) {
resolve(blob);
this.player.emit(EVENTS.recordBlob, blob);
} else {
resolve();
const fileName = (this.fileName || now$2()) + '.' + FILE_SUFFIX.mp4;
saveBlobToFile(fileName, blob);
}
this._reset();
this.player.emit(EVENTS.recording, false);
});
}
}
function mergeArrayBuffer$2(bufferList) {
const Cons = bufferList[0].constructor;
return bufferList.reduce((pre, val) => {
const merge = new Cons((pre.byteLength | 0) + (val.byteLength | 0));
merge.set(pre, 0);
merge.set(val, pre.byteLength | 0);
return merge;
}, new Cons());
}
class FlvRecorderLoader extends CommonLoader$1 {
constructor(player) {
super(player);
this.TAG_NAME = 'FlvRecorderLoader';
this.player = player;
this._init();
this.player.debug.log(this.TAG_NAME, 'init');
}
destroy() {
super.destroy();
this._init();
this.player.debug.log(this.TAG_NAME, 'destroy');
}
_init() {
this.hasAudio = false;
this.hasVideo = false;
this.startTime = null;
this.currentTime = 0;
this.prevTimestamp = 0;
this.totalByteLength = 0;
this.totalDuration = 0;
this.flvMetaData = null;
this.aacSequenceHeader = null;
this.videoSequenceHeader = null;
this.bufferList = [];
}
_reset() {
super._reset();
this._init();
}
startRecord() {
const debug = this.player.debug;
this._isRecording = true;
this.player.emit(EVENTS.recording, true);
debug.log(this.TAG_NAME, 'start recording');
this.player.emit(EVENTS.recordStart);
this.startRecordingInterval();
}
// override
startRecordingInterval() {
this.stopRecordingInterval();
this.recordingInterval = window.setInterval(() => {
this.player.emit(EVENTS.recordingTimestamp, this.getTotalDuration());
}, 1000);
}
addMetaData(arrayBuffer) {
this.flvMetaData = arrayBuffer;
}
addAACSequenceHeader(arrayBuffer) {
this.aacSequenceHeader = arrayBuffer;
}
addVideoSequenceHeader(arrayBuffer) {
this.videoSequenceHeader = arrayBuffer;
}
addVideo(arrayBuffer, ts) {
this._setStartTime(ts);
const newTs = this._getBufferTs(ts);
this.hasVideo = true;
this._createBufferItem(arrayBuffer, FLV_MEDIA_TYPE.video, newTs);
}
addAudio(arrayBuffer, ts) {
this._setStartTime(ts);
const newTs = this._getBufferTs(ts);
this.hasAudio = true;
this._createBufferItem(arrayBuffer, FLV_MEDIA_TYPE.audio, newTs);
}
_setStartTime(ts) {
if (this.startTime === null && this._isRecording) {
this.startTime = ts;
this.player.debug.log(this.TAG_NAME, `_setStartTime is ${ts}`);
}
}
_getBufferTs(ts) {
if (ts > this.currentTime) {
this.currentTime = ts;
}
let result = 0;
if (this.startTime && ts >= this.startTime) {
result = ts - this.startTime;
}
if (result > this.prevTimestamp) {
this.prevTimestamp = result;
} else {
result = this.prevTimestamp;
}
return result;
}
_createBufferItem(uint8Array, type, ts) {
const packet = this._createFlvPacket(uint8Array, type, ts);
const tag = this._createFlvTag(packet);
this.totalByteLength += tag.byteLength;
this.bufferList.push(tag);
}
_createFlvTag(packet) {
let PreviousTagSize = 11 + packet.header.length;
let tagBuffer = new Uint8Array(PreviousTagSize + 4);
tagBuffer[0] = packet.header.type;
let dv = new DataView(tagBuffer.buffer);
tagBuffer[1] = packet.header.length >> 16 & 0xff;
tagBuffer[2] = packet.header.length >> 8 & 0xff;
tagBuffer[3] = packet.header.length & 0xff;
tagBuffer[4] = packet.header.timestamp >> 16 & 0xff;
tagBuffer[5] = packet.header.timestamp >> 8 & 0xff;
tagBuffer[6] = packet.header.timestamp & 0xff;
tagBuffer[7] = packet.header.timestamp >> 24 & 0xff;
tagBuffer[8] = 0;
tagBuffer[9] = 0;
tagBuffer[10] = 0;
dv.setUint32(PreviousTagSize, PreviousTagSize);
tagBuffer.set(packet.payload.subarray(0, packet.header.length), 11);
return tagBuffer;
}
_createFlvPacket() {
let payload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
let type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
let time = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
return {
header: {
length: payload ? payload.length : 0,
timestamp: time,
type: type
},
payload: payload
};
}
stopRecordAndSave() {
let type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : RECORDING_TYPE.download;
let fileName = arguments.length > 1 ? arguments[1] : undefined;
return new Promise((resolve, reject) => {
if (!this.isRecording) {
this.player.debug.error(this.TAG_NAME, 'stop recording fail, isRecording is false ');
return reject('stop recording fail, isRecording is false ');
}
if (this.bufferList.length === 0) {
this.player.debug.error(this.TAG_NAME, 'stop recording fail, this.bufferList.length is 0 ');
return reject('stop recording fail, this.bufferList.length is 0 ');
}
if (fileName) {
this.setFileName(fileName);
}
const flvHeader = new Uint8Array([0x46, 0x4c, 0x56,
// flv
0x01, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00]);
if (this.hasVideo) {
flvHeader[4] |= 0b00000001;
}
if (this.hasAudio) {
flvHeader[4] |= 0b00000100;
}
let tempBufferList = [flvHeader];
// script tag
if (this.flvMetaData) {
const packet = this._createFlvPacket(this.flvMetaData, FLV_MEDIA_TYPE.scriptData);
const tag = this._createFlvTag(packet);
tempBufferList.push(tag);
}
// video sequence header
if (this.videoSequenceHeader) {
const packet = this._createFlvPacket(this.videoSequenceHeader, FLV_MEDIA_TYPE.video);
const tag = this._createFlvTag(packet);
tempBufferList.push(tag);
}
// aac sequence header
if (this.aacSequenceHeader) {
const packet = this._createFlvPacket(this.aacSequenceHeader, FLV_MEDIA_TYPE.audio);
const tag = this._createFlvTag(packet);
tempBufferList.push(tag);
}
let bufferList = tempBufferList.concat(this.bufferList);
const mergeBuffer = mergeArrayBuffer$2(bufferList);
this.player.debug.log(this.TAG_NAME, 'stop recording');
const blob = new Blob([mergeBuffer], {
'type': 'application/octet-stream'
});
if (type === RECORDING_TYPE.blob) {
resolve(blob);
this.player.emit(EVENTS.recordBlob, blob);
} else {
resolve();
const fileName = (this.fileName || now$2()) + '.' + FILE_SUFFIX.flv;
saveBlobToFile(fileName, blob);
}
this._reset();
this.player.emit(EVENTS.recording, false);
});
}
getTotalDuration() {
let result = 0;
if (this.startTime !== null && this.currentTime !== null) {
result = this.currentTime - this.startTime;
}
// 四舍五入
return Math.round(result / 1000);
}
getType() {
return FILE_SUFFIX.flv;
}
//
getToTalByteLength() {
return this.totalByteLength;
}
}
const mp3FrameParseStats = {
init: 0,
findFirstStartCode: 1,
findSecondStartCode: 2
};
class Mp3FrameParseLoader extends Emitter {
constructor(player) {
super();
this.player = player;
this.isDestroyed = false;
this.reset();
}
destroy() {
this.isDestroyed = false;
this.off();
this.reset();
}
reset() {
this.stats = mp3FrameParseStats.init;
this.tempBuffer = new Uint8Array(0);
this.parsedOffset = 0;
this.versionLayer = 0;
}
dispatch(data, ts) {
let newBuffer = new Uint8Array(this.tempBuffer.length + data.length);
newBuffer.set(this.tempBuffer, 0);
newBuffer.set(data, this.tempBuffer.length);
this.tempBuffer = newBuffer;
while (1) {
if (this.isDestroyed) {
break;
}
if (this.state == mp3FrameParseStats.Init) {
let bf = false;
while (this.tempBuffer.length - this.parsedOffset >= 2) {
if (this.isDestroyed) {
break;
}
// mp3帧头是连续11bit等于1
if (this.tempBuffer[this.parsedOffset] != 0xFF) {
this.parsedOffset++;
continue;
}
if (this.tempBuffer[this.parsedOffset + 1] & 0xE0 != 0xE0) {
this.parsedOffset++;
continue;
}
this.versionLayer = this.tempBuffer[this.parsedOffset + 1];
this.state = mp3FrameParseStats.findFirstStartCode;
this.fisrtStartCodeOffset = this.parsedOffset;
this.parsedOffset += 2;
bf = true;
break;
}
if (bf) {
continue;
} else {
break;
}
} else if (this.state == mp3FrameParseStats.findFirstStartCode) {
let bf = false;
while (this.tempBuffer.length - this.parsedOffset >= 2) {
if (this.isDestroyed) {
break;
}
if (this.tempBuffer[this.parsedOffset] != 0xFF) {
this.parsedOffset++;
continue;
}
if (this.tempBuffer[this.parsedOffset + 1] != this.versionLayer) {
this.parsedOffset++;
continue;
}
this.state = mp3FrameParseStats.findSecondStartCode;
this.secondStartCodeOffset = this.parsedOffset;
this.parsedOffset += 2;
bf = true;
break;
}
if (bf) {
continue;
} else {
break;
}
} else if (this.state == mp3FrameParseStats.findSecondStartCode) {
let lastFrame = this.tempBuffer.slice(this.fisrtStartCodeOffset, this.secondStartCodeOffset);
this.emit('data', lastFrame, ts);
this.tempBuffer = this.tempBuffer.slice(this.secondStartCodeOffset);
this.fisrtStartCodeOffset = 0;
this.parsedOffset = 2;
this.state = mp3FrameParseStats.findFirstStartCode;
}
}
}
}
class WasmMp4Loader extends CommonLoader$1 {
constructor(player) {
super(player);
this.TAG_NAME = 'recorderWasmMP4';
this._reset();
// config wasmMp4Recorder
this.wasmMp4Recorder = new window.JessibucaProMp4Recorder({
debug: player._opt.debug,
debugLevel: player._opt.debugLevel,
debugUuid: player._opt.debugUuid,
decoder: player._opt.wasmMp4RecorderDecoder
});
this.mp3Demuxer = null;
this.isG711 = false;
player.debug.log(this.TAG_NAME, 'init');
}
destroy() {
super.destroy();
if (this.mp3Demuxer) {
this.mp3Demuxer.destroy();
this.mp3Demuxer = null;
}
this.isG711 = false;
this._reset();
this.player.debug.log(this.TAG_NAME, 'destroy');
}
_reset() {
super._reset();
this.cacheTrack = {};
this.audioCacheTrack = {};
this.totalDuration = 0;
this.totalAudioDuration = 0;
this.totalByteLength = 0;
this.totalAudioByteLength = 0;
this.hasAudio = false;
this.hasVideo = false;
}
getType() {
return FILE_SUFFIX.mp4;
}
isWasmMp4() {
return true;
}
getTotalDuration() {
return this.totalDuration / 1000;
}
//
getToTalByteLength() {
return this.totalByteLength + this.totalAudioByteLength;
}
startRecord() {
const debug = this.player.debug;
const audioInfo = this.player.getAudioInfo();
const videoInfo = this.player.getVideoInfo();
const params = {};
if (this.codecId) {
const video = {
type: this.codecId,
width: videoInfo.width,
height: videoInfo.height,
extraData: this.metaInfo.avcc
};
params.video = video;
this.hasVideo = true;
}
if (audioInfo.encTypeCode) {
this.isG711 = audioInfo.encTypeCode === AUDIO_ENC_CODE.ALAW || audioInfo.encTypeCode === AUDIO_ENC_CODE.MULAW;
const audio = {
type: audioInfo.encTypeCode,
sampleRate: audioInfo.sampleRate,
channels: audioInfo.channels,
extraData: this.audioMetaInfo.extraData,
depth: audioInfo.depth
};
this.audioCodeId = audioInfo.encTypeCode;
params.audio = audio;
this.hasAudio = true;
}
this.wasmMp4Recorder.startRecord(params).then(() => {
this._isRecording = true;
this.player.emit(EVENTS.recording, true);
debug.log(this.TAG_NAME, 'start recording');
this.player.emit(EVENTS.recordStart);
this.startRecordingInterval();
}).catch(e => {
debug.error(this.TAG_NAME, 'startRecord error', e);
this.player.emitError(EVENTS.recordCreateError, e);
});
}
startRecordingInterval() {
this.stopRecordingInterval();
this.recordingInterval = window.setInterval(() => {
this.player.emit(EVENTS.recordingTimestamp, this.getTotalDuration());
}, 1000);
}
stopRecordAndSave() {
let type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : RECORDING_TYPE.download;
let fileName = arguments.length > 1 ? arguments[1] : undefined;
return new Promise((resolve, reject) => {
if (!this.isRecording) {
this.player.debug.error(this.TAG_NAME, 'stop recording fail, isRecording is false ');
return reject('stop recording fail, isRecording is false ');
}
if (this.totalDuration === 0) {
this.player.debug.error(this.TAG_NAME, 'stop recording fail, totalDuration is 0 ');
return reject('stop recording fail, totalDuration is 0 ');
}
if (fileName) {
this.setFileName(fileName);
}
this.wasmMp4Recorder.stopRecord().then(blob => {
if (type === RECORDING_TYPE.blob) {
resolve(blob);
this.player.emit(EVENTS.recordBlob, blob);
} else {
resolve();
const suffix = this.isG711 ? FILE_SUFFIX.mov : FILE_SUFFIX.mp4;
const fileName = (this.fileName || now$2()) + '.' + suffix;
saveBlobToFile(fileName, blob);
}
}).catch(e => {
this.player.debug.error(this.TAG_NAME, 'stopRecord error', e);
reject(e);
}).finally(() => {
this._reset();
this.player.emit(EVENTS.recording, false);
});
});
}
handleAddAudioTrack(payload, dts) {
if (this.audioCodeId === AUDIO_ENC_CODE.MP3) {
if (!this.mp3Demuxer) {
this.mp3Demuxer = new Mp3FrameParseLoader(this.player);
this.mp3Demuxer.on('data', (audioBuffer, audioTs) => {
this._handleAddAudioTrack(audioBuffer, audioTs);
});
}
this.mp3Demuxer.dispatch(payload, dts);
} else {
this._handleAddAudioTrack(payload, dts);
}
}
_handleAddAudioTrack(payload, dts) {
if (isFalse(this.hasAudio)) {
return;
}
if (this.audioCacheTrack.id && dts >= this.audioCacheTrack.dts) {
this.audioCacheTrack.duration = dts - this.audioCacheTrack.dts;
this.totalAudioDuration += this.audioCacheTrack.duration;
this.totalAudioByteLength += this.audioCacheTrack.payload.byteLength;
this.wasmMp4Recorder.sendAudioFrame(this.audioCacheTrack.payload, this.audioCacheTrack.dts);
} else {
this.audioCacheTrack = {};
}
this.audioCacheTrack = {
id: 2,
payload: payload,
dts: dts
};
}
handleAddNaluTrack(payload, isIFrame, dts, cts) {
if (isFalse(this.hasVideo)) {
return;
}
if (this.cacheTrack.id && dts >= this.cacheTrack.dts) {
this.cacheTrack.duration = dts - this.cacheTrack.dts;
this.totalDuration += this.cacheTrack.duration;
this.totalByteLength += this.cacheTrack.payload.byteLength;
this.wasmMp4Recorder.sendVideoFrame(this.cacheTrack.payload, this.cacheTrack.isIFrame, this.cacheTrack.dts, this.cacheTrack.cts);
} else {
this.cacheTrack = {};
}
this.cacheTrack = {
id: 1,
payload: payload,
isIFrame: isIFrame,
dts: dts,
cts: cts
};
}
}
class Recorder {
constructor(player) {
const Loader = Recorder.getLoaderFactory(player._opt);
return new Loader(player);
}
static getLoaderFactory(opt) {
if (opt.recordType === FILE_SUFFIX.mp4) {
if (opt.useWasm || opt.useMSE || opt.useWCS) {
if (window.JessibucaProMp4Recorder && opt.mp4RecordUseWasm) {
return WasmMp4Loader;
}
return MP4RecorderLoader;
}
} else if (opt.recordType === FILE_SUFFIX.flv) {
return FlvRecorderLoader;
}
// default use RecordRTC
return RecordRTCLoader;
}
}
function checkContinuation(uint8array, start, checkLength) {
let array = uint8array;
if (start + checkLength < array.length) {
while (checkLength--) {
if ((array[++start] & 0xC0) !== 0x80) return false;
}
return true;
} else {
return false;
}
}
function decodeUTF8(uint8array) {
let out = [];
let input = uint8array;
let i = 0;
let length = uint8array.length;
while (i < length) {
if (input[i] < 0x80) {
out.push(String.fromCharCode(input[i]));
++i;
continue;
} else if (input[i] < 0xC0) ; else if (input[i] < 0xE0) {
if (checkContinuation(input, i, 1)) {
let ucs4 = (input[i] & 0x1F) << 6 | input[i + 1] & 0x3F;
if (ucs4 >= 0x80) {
out.push(String.fromCharCode(ucs4 & 0xFFFF));
i += 2;
continue;
}
}
} else if (input[i] < 0xF0) {
if (checkContinuation(input, i, 2)) {
let ucs4 = (input[i] & 0xF) << 12 | (input[i + 1] & 0x3F) << 6 | input[i + 2] & 0x3F;
if (ucs4 >= 0x800 && (ucs4 & 0xF800) !== 0xD800) {
out.push(String.fromCharCode(ucs4 & 0xFFFF));
i += 3;
continue;
}
}
} else if (input[i] < 0xF8) {
if (checkContinuation(input, i, 3)) {
let ucs4 = (input[i] & 0x7) << 18 | (input[i + 1] & 0x3F) << 12 | (input[i + 2] & 0x3F) << 6 | input[i + 3] & 0x3F;
if (ucs4 > 0x10000 && ucs4 < 0x110000) {
ucs4 -= 0x10000;
out.push(String.fromCharCode(ucs4 >>> 10 | 0xD800));
out.push(String.fromCharCode(ucs4 & 0x3FF | 0xDC00));
i += 4;
continue;
}
}
}
out.push(String.fromCharCode(0xFFFD));
++i;
}
return out.join('');
}
let le = function () {
let buf = new ArrayBuffer(2);
new DataView(buf).setInt16(0, 256, true); // little-endian write
return new Int16Array(buf)[0] === 256; // platform-spec read, if equal then LE
}();
class AMF {
static parseScriptData(arrayBuffer, dataOffset, dataSize) {
let data = {};
try {
let name = AMF.parseValue(arrayBuffer, dataOffset, dataSize);
let value = AMF.parseValue(arrayBuffer, dataOffset + name.size, dataSize - name.size);
data[name.data] = value.data;
} catch (e) {
console.error('AMF', e.toString());
}
return data;
}
static parseObject(arrayBuffer, dataOffset, dataSize) {
let name = AMF.parseString(arrayBuffer, dataOffset, dataSize);
let value = AMF.parseValue(arrayBuffer, dataOffset + name.size, dataSize - name.size);
let isObjectEnd = value.objectEnd;
return {
data: {
name: name.data,
value: value.data
},
size: name.size + value.size,
objectEnd: isObjectEnd
};
}
static parseVariable(arrayBuffer, dataOffset, dataSize) {
return AMF.parseObject(arrayBuffer, dataOffset, dataSize);
}
static parseString(arrayBuffer, dataOffset, dataSize) {
let v = new DataView(arrayBuffer, dataOffset, dataSize);
let length = v.getUint16(0, !le);
let str;
if (length > 0) {
str = decodeUTF8(new Uint8Array(arrayBuffer, dataOffset + 2, length));
} else {
str = '';
}
return {
data: str,
size: 2 + length
};
}
static parseLongString(arrayBuffer, dataOffset, dataSize) {
let v = new DataView(arrayBuffer, dataOffset, dataSize);
let length = v.getUint32(0, !le);
let str;
if (length > 0) {
str = decodeUTF8(new Uint8Array(arrayBuffer, dataOffset + 4, length));
} else {
str = '';
}
return {
data: str,
size: 4 + length
};
}
static parseDate(arrayBuffer, dataOffset, dataSize) {
let v = new DataView(arrayBuffer, dataOffset, dataSize);
let timestamp = v.getFloat64(0, !le);
let localTimeOffset = v.getInt16(8, !le);
timestamp += localTimeOffset * 60 * 1000; // get UTC time
return {
data: new Date(timestamp),
size: 8 + 2
};
}
static parseValue(arrayBuffer, dataOffset, dataSize) {
let v = new DataView(arrayBuffer, dataOffset, dataSize);
let offset = 1;
let type = v.getUint8(0);
let value;
let objectEnd = false;
try {
switch (type) {
case 0:
// Number(Double) type
value = v.getFloat64(1, !le);
offset += 8;
break;
case 1:
{
// Boolean type
let b = v.getUint8(1);
value = b ? true : false;
offset += 1;
break;
}
case 2:
{
// String type
let amfstr = AMF.parseString(arrayBuffer, dataOffset + 1, dataSize - 1);
value = amfstr.data;
offset += amfstr.size;
break;
}
case 3:
{
// Object(s) type
value = {};
let terminal = 0; // workaround for malformed Objects which has missing ScriptDataObjectEnd
if ((v.getUint32(dataSize - 4, !le) & 0x00FFFFFF) === 9) {
terminal = 3;
}
while (offset < dataSize - 4) {
// 4 === type(UI8) + ScriptDataObjectEnd(UI24)
let amfobj = AMF.parseObject(arrayBuffer, dataOffset + offset, dataSize - offset - terminal);
if (amfobj.objectEnd) break;
value[amfobj.data.name] = amfobj.data.value;
offset += amfobj.size;
}
if (offset <= dataSize - 3) {
let marker = v.getUint32(offset - 1, !le) & 0x00FFFFFF;
if (marker === 9) {
offset += 3;
}
}
break;
}
case 8:
{
// ECMA array type (Mixed array)
value = {};
offset += 4; // ECMAArrayLength(UI32)
let terminal = 0; // workaround for malformed MixedArrays which has missing ScriptDataObjectEnd
if ((v.getUint32(dataSize - 4, !le) & 0x00FFFFFF) === 9) {
terminal = 3;
}
while (offset < dataSize - 8) {
// 8 === type(UI8) + ECMAArrayLength(UI32) + ScriptDataVariableEnd(UI24)
let amfvar = AMF.parseVariable(arrayBuffer, dataOffset + offset, dataSize - offset - terminal);
if (amfvar.objectEnd) break;
value[amfvar.data.name] = amfvar.data.value;
offset += amfvar.size;
}
if (offset <= dataSize - 3) {
let marker = v.getUint32(offset - 1, !le) & 0x00FFFFFF;
if (marker === 9) {
offset += 3;
}
}
break;
}
case 9:
// ScriptDataObjectEnd
value = undefined;
offset = 1;
objectEnd = true;
break;
case 10:
{
// Strict array type
// ScriptDataValue[n]. NOTE: according to video_file_format_spec_v10_1.pdf
value = [];
let strictArrayLength = v.getUint32(1, !le);
offset += 4;
for (let i = 0; i < strictArrayLength; i++) {
let val = AMF.parseValue(arrayBuffer, dataOffset + offset, dataSize - offset);
value.push(val.data);
offset += val.size;
}
break;
}
case 11:
{
// Date type
let date = AMF.parseDate(arrayBuffer, dataOffset + 1, dataSize - 1);
value = date.data;
offset += date.size;
break;
}
case 12:
{
// Long string type
let amfLongStr = AMF.parseString(arrayBuffer, dataOffset + 1, dataSize - 1);
value = amfLongStr.data;
offset += amfLongStr.size;
break;
}
default:
// ignore and skip
offset = dataSize;
console.warn('AMF', 'Unsupported AMF value type ' + type);
}
} catch (e) {
console.error('AMF', e.toString());
}
return {
data: value,
size: offset,
objectEnd: objectEnd
};
}
}
function parseFlvScriptData(arrayBuffer) {
return AMF.parseScriptData(arrayBuffer.buffer, arrayBuffer.byteOffset, arrayBuffer.byteLength);
}
class DecoderWorker {
constructor(player) {
this.player = player;
this.destroyResolve = null;
this.workerClearTimeout = null;
this.workerUrl = null;
let decoder = player._opt.decoder;
this.decoderWorkerCloseTimeout = null;
// wcs or mse
if (isFalse(this.player._opt.useWasm)) {
if (this.player._opt.demuxUseWorker) {
// mse decoder audio + not audio
if (this.player._opt.mseDecodeAudio || isFalse(this.player._opt.hasAudio)) {
decoder = player._opt.decoderHardNotWasm;
} else {
decoder = player._opt.decoderHard;
}
} else {
decoder = player._opt.decoderAudio;
}
}
if (decoder.indexOf('http') === 0 && this.player._opt.isDecoderUseCDN) {
const blob = new Blob([`importScripts("${decoder}")`], {
"type": 'application/javascript'
});
decoder = window.URL.createObjectURL(blob);
this.workerUrl = decoder;
// 必须要释放,不然每次调用内存都明显泄露内存
//chrome 83 file协议下如果直接释放,将会使WebWorker无法启动
this.workerClearTimeout = setTimeout(() => {
window.URL.revokeObjectURL(this.workerUrl);
this.workerUrl = null;
this.workerClearTimeout = null;
}, URL_OBJECT_CLEAR_TIME);
}
this.decoderWorker = new Worker(decoder);
this._initDecoderWorker();
player.debug.log('decoderWorker', `init and decoder url is ${decoder}`);
player.on(EVENTS.visibilityChange, () => {
this.updateWorkConfig({
key: 'visibility',
value: player.visibility
});
});
}
destroy() {
return new Promise((resolve, reject) => {
if (this.player.loaded) {
this.player.debug.log('decoderWorker', 'has loaded and post message to destroy');
if (this.decoderWorker) {
this.decoderWorker.postMessage({
cmd: WORKER_SEND_TYPE.close
});
this.destroyResolve = resolve;
// 需要添加个防御如果worker 线程没有返回,则直接触发超时逻辑。
this.decoderWorkerCloseTimeout = setTimeout(() => {
this.player.debug.warn('decoderWorker', 'send close but not response and destroy directly');
if (this.decoderWorkerCloseTimeout) {
clearTimeout(this.decoderWorkerCloseTimeout);
this.decoderWorkerCloseTimeout = null;
}
this._destroy();
setTimeout(() => {
resolve();
}, 0);
}, 2 * 1000);
} else {
this.player.debug.warn('decoderWorker', 'has loaded but decoderWorker is null and destroy directly');
this._destroy();
setTimeout(() => {
resolve();
}, 0);
}
} else {
this.player.debug.log('decoderWorker', 'has not loaded and destroy directly');
this._destroy();
setTimeout(() => {
resolve();
}, 0);
}
});
}
_destroy() {
if (this.decoderWorkerCloseTimeout) {
clearTimeout(this.decoderWorkerCloseTimeout);
this.decoderWorkerCloseTimeout = null;
}
if (this.workerUrl) {
window.URL.revokeObjectURL(this.workerUrl);
this.workerUrl = null;
}
if (this.workerClearTimeout) {
clearTimeout(this.workerClearTimeout);
this.workerClearTimeout = null;
}
if (this.decoderWorker) {
this.decoderWorker.terminate();
this.decoderWorker.onerror = null;
this.decoderWorker.onmessageerror = null;
this.decoderWorker.onmessage = null;
this.decoderWorker = null;
}
this.player.debug.log(`decoderWorker`, 'destroy');
if (this.destroyResolve) {
this.destroyResolve();
this.destroyResolve = null;
}
}
_initDecoderWorker() {
const {
debug,
events: {
proxy
}
} = this.player;
//
this.decoderWorker.onerror = e => {
this.player.debug.error(`decoderWorker`, 'onerror', e);
this.player.emitError(EVENTS_ERROR.decoderWorkerInitError, e);
};
this.decoderWorker.onmessageerror = e => {
this.player.debug.error(`decoderWorker`, 'onmessageerror', e);
};
this.decoderWorker.onmessage = event => {
const msg = event.data;
switch (msg.cmd) {
case WORKER_CMD_TYPE.init:
debug.log(`decoderWorker`, 'onmessage:', WORKER_CMD_TYPE.init);
//
if (this.decoderWorker) {
this._initWork();
}
if (!this.player.loaded) {
this.player.emit(EVENTS.load);
}
this.player.emit(EVENTS.decoderWorkerInit);
break;
case WORKER_CMD_TYPE.videoCode:
debug.log(`decoderWorker`, 'onmessage:', WORKER_CMD_TYPE.videoCode, msg.code);
if (!this.player._times.decodeStart) {
this.player._times.decodeStart = now$2();
}
this.player.video.updateVideoInfo({
encTypeCode: msg.code
});
break;
case WORKER_CMD_TYPE.videoCodec:
debug.log(`decoderWorker`, 'onmessage:', WORKER_CMD_TYPE.videoCodec, msg.codecId);
if (this.player.recorder) {
this.player.recorder.initMetaData(msg.buffer, msg.codecId);
}
this.player.video.updateVideoInfo({
encTypeCode: msg.codecId
});
break;
case WORKER_CMD_TYPE.audioCode:
debug.log(`decoderWorker`, 'onmessage:', WORKER_CMD_TYPE.audioCode, msg.code);
this.player.audio && this.player.audio.updateAudioInfo({
encTypeCode: msg.code
});
break;
case WORKER_CMD_TYPE.audioAACSequenceHeader:
debug.log(`decoderWorker`, 'onmessage:', WORKER_CMD_TYPE.audioAACSequenceHeader);
if (this.player.recorder) {
this.player.recorder.initAudioAacExtraData(msg.buffer);
}
break;
case WORKER_CMD_TYPE.initVideo:
debug.log(`decoderWorker`, 'onmessage:', WORKER_CMD_TYPE.initVideo, `width:${msg.w},height:${msg.h}`);
if (isEmpty(msg.w) || isEmpty(msg.h)) {
this.player.emitError(EVENTS_ERROR.videoInfoError, `video width ${msg.w} or height ${msg.h} is empty`);
return;
}
this.player.video.updateVideoInfo({
width: msg.w,
height: msg.h
});
// just for canvas render
if (!this.player._opt.openWebglAlignment && !isWebglRenderSupport(msg.w) && this.player.getRenderType() === RENDER_TYPE.canvas) {
this.player.emitError(EVENTS_ERROR.webglAlignmentError);
return;
}
this.player.video.initCanvasViewSize();
if (this.player._opt.playType === PLAY_TYPE.playbackTF) {
this.player.video.initFps();
this.player.video.initVideoDelay();
}
break;
case WORKER_CMD_TYPE.initAudio:
debug.log(`decoderWorker`, 'onmessage:', WORKER_CMD_TYPE.initAudio, `channels:${msg.channels},sampleRate:${msg.sampleRate}`);
if (msg.channels > AUDIO_CHANNEL_MAX) {
this.player.emitError(EVENTS_ERROR.audioChannelError, `audio channel is ${msg.channels}, max is ${AUDIO_CHANNEL_MAX}`);
return;
}
if (this.player.audio) {
this.player.audio.updateAudioInfo(msg);
if (this.player._opt.playType === PLAY_TYPE.player) {
this.player.audio.initScriptNode();
} else if (this.player._opt.playType === PLAY_TYPE.playbackTF) {
this.player.audio.initScriptNodeDelay();
}
}
break;
case WORKER_CMD_TYPE.render:
if (!this.player.video) {
debug.warn('decoderWorker', 'onmessage render but video is null');
return;
}
// debug.log(`decoderWorker`, 'onmessage:', WORKER_CMD_TYPE.render, `msg ts:${msg.ts}`);
if (this.player.isPlayer()) {
// un caught typeError: failed to construct 'VideoFrame': codedWidth muse be nonzero;
if (isFalse(this.player.video.getHasInit())) {
debug.warn('decoderWorker', 'onmessage render but video has not init');
return;
}
this.player.video.render(msg);
this.player.handleRender();
this.player.emit(EVENTS.timeUpdate, msg.ts);
this.player.updateStats({
dfps: true,
buf: msg.delay
});
if (!this.player._times.videoStart) {
this.player._times.videoStart = now$2();
this.player.handlePlayToRenderTimes();
}
} else if (this.player.isPlayback()) {
this.player.updateStats({
dfps: true
});
if (isFalse(this.player.playbackPause)) {
if (this.player.playback.isUseLocalCalculateTime) {
this.player.playback.increaseLocalTimestamp();
}
if (this.player.playback.isUseFpsRender) {
this.player.video.pushData(msg);
} else {
this.player.video.render$2(msg);
}
} else {
// 暂停的时候,如果不需要清除缓存
if (!this.player.playback.isPlaybackPauseClearCache && this.player.playback.isCacheBeforeDecodeForFpsRender) {
if (this.player.playback.isUseFpsRender) {
this.player.video.pushData(msg);
}
}
}
}
break;
case WORKER_CMD_TYPE.videoNalu:
if (this.player.recorder && this.player.recorder.isRecording && this.player._opt.recordType === FILE_SUFFIX.mp4) {
this.player.recorder.handleAddNaluTrack(msg.buffer, msg.isIFrame, msg.ts, msg.cts);
}
break;
case WORKER_CMD_TYPE.audioNalu:
if (this.player.recorder && this.player.recorder.isRecording && this.player._opt.recordType === FILE_SUFFIX.mp4 && this.player.recorder.isWasmMp4()) {
this.player.recorder.handleAddAudioTrack(msg.buffer, msg.ts);
}
break;
case WORKER_CMD_TYPE.videoPayload:
const {
webcodecsDecoder,
mseDecoder
} = this.player;
this.player.updateStats({
buf: msg.delay
});
const uint8Array = new Uint8Array(msg.payload);
if (this.player._opt.useWCS && !this.player._opt.useOffscreen) {
webcodecsDecoder.decodeVideo(uint8Array, msg.ts, msg.isIFrame, msg.cts);
} else if (this.player._opt.useMSE) {
mseDecoder.decodeVideo(uint8Array, msg.ts, msg.isIFrame, msg.cts);
}
break;
case WORKER_CMD_TYPE.audioPayload:
if (this.player._opt.useMSE) {
const uint8Array = new Uint8Array(msg.payload);
this.player.mseDecoder.decodeAudio(uint8Array, msg.ts, msg.cts);
}
break;
case WORKER_CMD_TYPE.playAudio:
// debug.log(`decoderWorker`, 'onmessage:', WORKER_CMD_TYPE.playAudio, `msg ts:${msg.ts}`);
// 只有在 playing 的时候。
// 或者 设置hasVideo 为false的情况
if (!this.player.audio) {
debug.warn('decoderWorker', 'onmessage playAudio but audio is null');
return;
}
if (this.player.playing && this.player.audio || !this.player.video) {
// 如果不解码video
if (!this.player._opt.hasVideo) {
this.player.handleRender();
}
if (this.player._opt.playType === PLAY_TYPE.player) {
this.player.audio.play(msg.buffer, msg.ts);
} else if (this.player._opt.playType === PLAY_TYPE.playbackTF) {
if (isFalse(this.player.playbackPause)) {
this.player.audio.play(msg.buffer, msg.ts);
} else {
if (!this.player.playback.isPlaybackPauseClearCache && this.player.playback.isCacheBeforeDecodeForFpsRender) {
if (this.player.playback.isUseFpsRender) {
this.player.audio.play(msg.buffer, msg.ts);
}
}
}
}
}
break;
case WORKER_CMD_TYPE.workerFetch:
// debug.log(`decoderWorker`, `workerFetch: ${msg.type},${msg.value}`);
if (msg.type === EVENTS.streamSuccess) {
if (this.player.stream) {
this.player.stream.emit(EVENTS.streamSuccess);
} else {
debug.warn('decoderWorker', `onmessage and workerFetch response stream success but stream is null`);
}
} else if (msg.type === EVENTS.streamRate) {
this.player.emit(EVENTS.kBps, (msg.value / 1024).toFixed(2));
} else if (msg.type === EVENTS.streamEnd) {
if (this.player) {
if (msg.value === PLAYER_STREAM_TYPE.websocket) {
this.player.emit(EVENTS.websocketClose, msg.msg);
}
if (this.player.stream) {
this.player.stream.emit(EVENTS.streamEnd, msg.msg);
} else {
debug && debug.warn('decoderWorker', `onmessage and workerFetch response stream end but player.stream is null`);
}
} else {
debug && debug.warn('decoderWorker', `onmessage and workerFetch response stream end but player is null`);
}
} else if (msg.type === EVENTS_ERROR.websocketError) {
if (this.player && this.player.stream) {
this.player.stream.emit(EVENTS_ERROR.websocketError, msg.value);
} else {
debug && debug.warn('decoderWorker', `onmessage and workerFetch response websocket error but stream is null`);
}
} else if (msg.type === EVENTS_ERROR.fetchError) {
if (this.player && this.player.stream) {
this.player.stream.emit(EVENTS_ERROR.fetchError, msg.value);
} else {
debug && debug.warn('decoderWorker', `onmessage and workerFetch response fetch error but stream is null`);
}
} else if (msg.type === EVENTS.streamAbps) {
this.player.updateStats({
abps: msg.value
});
} else if (msg.type === EVENTS.streamVbps) {
if (!this.player._times.demuxStart) {
this.player._times.demuxStart = now$2();
}
this.player.updateStats({
vbps: msg.value
});
} else if (msg.type === EVENTS.streamDts) {
this.player.updateStats({
dts: msg.value
});
} else if (msg.type === EVENTS.netBuf) {
this.player.updateStats({
netBuf: msg.value
});
} else if (msg.type === EVENTS.networkDelayTimeout) {
this.player.emit(EVENTS.networkDelayTimeout, msg.value);
} else if (msg.type === EVENTS.streamStats) {
const obj = JSON.parse(msg.value);
this.player.updateStats({
workerStats: obj
});
} else if (msg.type === EVENTS.websocketOpen) {
this.player.emit(EVENTS.websocketOpen);
}
break;
case WORKER_CMD_TYPE.iframeIntervalTs:
if (this.player) {
this.player.videoIframeIntervalTs = msg.value;
}
break;
case WORKER_CMD_TYPE.isDropping:
if (this.player) {
this.player.updateStats({
isDropping: true
});
}
break;
case WORKER_CMD_TYPE.checkFirstIFrame:
this.player.decoderCheckFirstIFrame();
break;
case WORKER_CMD_TYPE.playbackStreamVideoFps:
if (this.player && this.player.video) {
this.player.video.setStreamFps(msg.value);
}
break;
case WORKER_CMD_TYPE.wasmError:
if (msg.message) {
if (msg.message.indexOf(WASM_ERROR.invalidNalUnitSize) !== -1) {
this.player.emitError(EVENTS_ERROR.wasmDecodeError, '');
}
}
break;
case WORKER_CMD_TYPE.wasmDecodeVideoNoResponseError:
this.player.emitError(EVENTS_ERROR.wasmDecodeVideoNoResponseError);
break;
case WORKER_CMD_TYPE.simdH264DecodeVideoWidthIsTooLarge:
this.player.emitError(EVENTS_ERROR.simdH264DecodeVideoWidthIsTooLarge);
break;
case WORKER_CMD_TYPE.wasmWidthOrHeightChange:
this.player.emitError(EVENTS_ERROR.wasmWidthOrHeightChange);
break;
case WORKER_CMD_TYPE.simdDecodeError:
this.player.emitError(EVENTS_ERROR.simdDecodeError);
break;
case WORKER_CMD_TYPE.workerEnd:
debug.log(`decoderWorker`, 'onmessage:', WORKER_CMD_TYPE.workerEnd);
break;
case WORKER_CMD_TYPE.closeEnd:
debug.log(`decoderWorker`, 'onmessage:', WORKER_CMD_TYPE.closeEnd);
this._destroy();
break;
case WORKER_CMD_TYPE.tempStream:
if (this.player) {
this.player.pushTempStream(msg.buffer);
}
break;
case WORKER_CMD_TYPE.videoSEI:
if (this.player) {
this.player.emit(EVENTS.videoSEI, {
ts: msg.ts,
data: new Uint8Array(msg.buffer)
});
}
break;
case WORKER_CMD_TYPE.flvScriptData:
if (this.player) {
if (this.player.isRecordTypeFlv()) {
const payloadCopy = new Uint8Array(msg.buffer);
this.player.recorder.addMetaData(payloadCopy);
}
const payload = new Uint8Array(msg.buffer);
const scriptObj = parseFlvScriptData(payload);
if (scriptObj && scriptObj.onMetaData) {
this.player.updateMetaData(scriptObj.onMetaData);
}
}
break;
case WORKER_CMD_TYPE.aacSequenceHeader:
if (this.player && this.player.isRecordTypeFlv()) {
const payloadCopy = new Uint8Array(msg.buffer);
this.player.recorder.addAACSequenceHeader(payloadCopy, msg.ts);
}
break;
case WORKER_CMD_TYPE.videoSequenceHeader:
if (this.player && this.player.isRecordTypeFlv()) {
const payloadCopy = new Uint8Array(msg.buffer);
this.player.recorder.addVideoSequenceHeader(payloadCopy, msg.ts);
}
break;
case WORKER_CMD_TYPE.flvBufferData:
if (this.player && this.player.isRecordTypeFlv() && this.player.recording) {
const payloadCopy = new Uint8Array(msg.buffer);
if (msg.type === MEDIA_TYPE.video) {
this.player.recorder.addVideo(payloadCopy, msg.ts);
} else if (msg.type === MEDIA_TYPE.audio) {
this.player.recorder.addAudio(payloadCopy, msg.ts);
}
}
break;
case WORKER_CMD_TYPE.mseHandle:
if (this.player) {
this.player.debug.log(`decoderWorker`, 'onmessage:', WORKER_CMD_TYPE.mseHandle);
this.player.video.$videoElement.srcObject = msg.mseHandle;
}
break;
case WORKER_CMD_TYPE.mseFirstRenderTime:
if (this.player) {
this.player.debug.log(`decoderWorker`, 'onmessage:', WORKER_CMD_TYPE.mseFirstRenderTime, msg.value);
this.player._mseWorkerData.firstRenderTime = Number(msg.value);
}
break;
case WORKER_CMD_TYPE.mseError:
if (this.player) {
this.player.debug.log(`decoderWorker`, 'onmessage:', WORKER_CMD_TYPE.mseError, msg.value, msg.msg);
this.player.emitError(msg.value, msg.msg);
}
break;
default:
this.player[msg.cmd] && this.player[msg.cmd](msg);
break;
}
};
}
_initWork() {
const opt = {
debug: this.player._opt.debug,
debugLevel: this.player._opt.debugLevel,
debugUuid: this.player._opt.debugUuid,
useOffscreen: this.player._opt.useOffscreen,
useWCS: this.player._opt.useWCS,
useMSE: this.player._opt.useMSE,
videoBuffer: this.player._opt.videoBuffer,
videoBufferDelay: this.player._opt.videoBufferDelay,
openWebglAlignment: this.player._opt.openWebglAlignment,
playType: this.player._opt.playType,
hasAudio: this.player._opt.hasAudio,
hasVideo: this.player._opt.hasVideo,
playbackRate: 1,
playbackForwardMaxRateDecodeIFrame: this.player._opt.playbackForwardMaxRateDecodeIFrame,
playbackIsCacheBeforeDecodeForFpsRender: this.player._opt.playbackConfig.isCacheBeforeDecodeForFpsRender,
sampleRate: this.player.audio && this.player.audio.audioContext && this.player.audio.audioContext.sampleRate || 0,
audioBufferSize: this.player.audio && this.player.audio.getAudioBufferSize() || 1024,
networkDelay: this.player._opt.networkDelay,
visibility: this.player.visibility,
useSIMD: this.player._opt.useSIMD,
recordType: this.player._opt.recordType,
checkFirstIFrame: this.player._opt.checkFirstIFrame,
isM7sCrypto: this.player._opt.isM7sCrypto,
isXorCrypto: this.player._opt.isXorCrypto,
isSm4Crypto: this.player._opt.isSm4Crypto,
sm4CryptoKey: this.player._opt.sm4CryptoKey,
m7sCryptoAudio: this.player._opt.m7sCryptoAudio,
isFlv: this.player._opt.isFlv,
isFmp4: this.player._opt.isFmp4,
isMpeg4: this.player._opt.isMpeg4,
isTs: this.player._opt.isTs,
isNakedFlow: this.player._opt.isNakedFlow,
isHls265: this.player.isUseHls265(),
isFmp4Private: this.player._opt.isFmp4Private,
isEmitSEI: this.player._opt.isEmitSEI,
isRecordTypeFlv: this.player.isRecordTypeFlv(),
isWasmMp4: this.player.recorder && this.player.recorder.isWasmMp4() || false,
isChrome: isChrome(),
isDropSameTimestampGop: this.player._opt.isDropSameTimestampGop,
mseDecodeAudio: this.player._opt.mseDecodeAudio,
nakedFlowH265DemuxUseNew: this.player._opt.nakedFlowH265DemuxUseNew,
mseDecoderUseWorker: this.player._opt.mseDecoderUseWorker,
mseAutoCleanupMinBackwardDuration: this.player._opt.mseAutoCleanupMinBackwardDuration,
mseAutoCleanupMaxBackwardDuration: this.player._opt.mseAutoCleanupMaxBackwardDuration,
mseCorrectTimeDuration: this.player._opt.mseCorrectTimeDuration,
mseCorrectAudioTimeDuration: this.player._opt.mseCorrectAudioTimeDuration
};
this.decoderWorker.postMessage({
cmd: WORKER_SEND_TYPE.init,
opt: JSON.stringify(opt)
});
if (this.player._opt.isM7sCrypto) {
this.updateWorkConfig({
key: 'cryptoKey',
value: this.player._opt.cryptoKey
});
this.updateWorkConfig({
key: 'cryptoIV',
value: this.player._opt.cryptoIV
});
}
}
decodeVideo(arrayBuffer, ts, isIFrame) {
if (this.player._opt.playType === PLAY_TYPE.player) {
if (this.player.isUseHls265()) {
this._decodeVideoNoDelay(arrayBuffer, ts, isIFrame);
} else {
this._decodeVideo(arrayBuffer, ts, isIFrame);
}
} else if (this.player._opt.playType === PLAY_TYPE.playbackTF) {
// if rate
if (this.player.video.rate >= this.player._opt.playbackForwardMaxRateDecodeIFrame) {
if (isIFrame) {
this.player.debug.log(`decoderWorker`, `current rate is ${this.player.video.rate},only decode i frame`);
this._decodeVideoNoDelay(arrayBuffer, ts, isIFrame);
}
} else {
if (this.player.video.rate === 1) {
this._decodeVideo(arrayBuffer, ts, isIFrame);
} else {
this._decodeVideoNoDelay(arrayBuffer, ts, isIFrame);
}
}
}
}
_decodeVideo(arrayBuffer, ts, isIFrame) {
const options = {
type: MEDIA_TYPE.video,
ts: Math.max(ts, 0),
isIFrame
};
// this.player.debug.log('decoderWorker', 'decodeVideo', options);
this.decoderWorker.postMessage({
cmd: WORKER_SEND_TYPE.decode,
buffer: arrayBuffer,
options
}, [arrayBuffer.buffer]);
}
/**
*
* @param arrayBuffer
* @param ts
* @private
*/
_decodeVideoNoDelay(arrayBuffer, ts, isIFrame) {
this.decoderWorker.postMessage({
cmd: WORKER_SEND_TYPE.videoDecode,
buffer: arrayBuffer,
ts: Math.max(ts, 0),
isIFrame
}, [arrayBuffer.buffer]);
}
decodeAudio(arrayBuffer, ts) {
if (this.player._opt.playType === PLAY_TYPE.player) {
if (this.player._opt.useWCS) {
this._decodeAudioNoDelay(arrayBuffer, ts);
} else if (this.player._opt.useMSE) {
this._decodeAudioNoDelay(arrayBuffer, ts);
} else if (this.player.isUseHls265()) {
this._decodeAudioNoDelay(arrayBuffer, ts);
} else {
this._decodeAudio(arrayBuffer, ts);
}
} else if (this.player._opt.playType === PLAY_TYPE.playbackTF) {
// this._decodeAudioNoDelay(arrayBuffer, ts);
if (this.player.video.rate === 1) {
this._decodeAudio(arrayBuffer, ts);
} else {
this._decodeAudioNoDelay(arrayBuffer, ts);
}
}
}
//
_decodeAudio(arrayBuffer, ts) {
const options = {
type: MEDIA_TYPE.audio,
ts: Math.max(ts, 0)
};
// this.player.debug.log('decoderWorker', 'decodeAudio',options);
this.decoderWorker.postMessage({
cmd: WORKER_SEND_TYPE.decode,
buffer: arrayBuffer,
options
}, [arrayBuffer.buffer]);
}
_decodeAudioNoDelay(arrayBuffer, ts) {
// console.log('_decodeAudioNoDelay', ts);
this.decoderWorker.postMessage({
cmd: WORKER_SEND_TYPE.audioDecode,
buffer: arrayBuffer,
ts: Math.max(ts, 0)
}, [arrayBuffer.buffer]);
}
updateWorkConfig(config) {
this.decoderWorker && this.decoderWorker.postMessage({
cmd: WORKER_SEND_TYPE.updateConfig,
key: config.key,
value: config.value
});
}
workerFetchStream(url) {
const {
_opt
} = this.player;
const opt = {
protocol: _opt.protocol,
isFlv: _opt.isFlv,
isFmp4: _opt.isFmp4,
isMpeg4: _opt.isMpeg4,
isNakedFlow: _opt.isNakedFlow,
isTs: _opt.isTs
};
this.decoderWorker.postMessage({
cmd: WORKER_SEND_TYPE.fetchStream,
url,
opt: JSON.stringify(opt)
});
}
clearWorkBuffer() {
let needClear = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
this.decoderWorker.postMessage({
cmd: WORKER_SEND_TYPE.clearBuffer,
needClear
});
}
workerSendMessage(message) {
this.decoderWorker.postMessage({
cmd: WORKER_SEND_TYPE.sendWsMessage,
message
});
}
//
updateVideoTimestamp(message) {
this.decoderWorker.postMessage({
cmd: WORKER_SEND_TYPE.mseUpdateVideoTimestamp,
message
});
}
}
// a simple ajax
var jsonType = 'application/json, text/javascript';
var htmlType = 'text/html';
var xmlTypeRE = /^(?:text|application)\/xml/i;
var rheaders = /^(.*?):[ \t]*([^\r\n]*)\r?$/mg;
var rurl = /^([\w.+-]+:)(?:\/\/(?:[^\/?#]*@|)([^\/?#:]*)(?::(\d+)|)|)/;
var blankRE = /^\s*$/; // \s
var lastModified = {};
var etag = {};
var responseHeadersString = '';
var responseHeaders;
/*
* default setting
* */
var _settings = {
type: "GET",
beforeSend: noop$1,
success: noop$1,
error: noop$1,
complete: noop$1,
context: null,
xhr: function () {
return new window.XMLHttpRequest();
},
accepts: {
json: jsonType,
xml: 'application/xml, text/xml',
html: htmlType,
text: 'text/plain',
'*': "*/".concat("*")
},
crossDomain: false,
timeout: 0,
username: null,
password: null,
processData: true,
promise: noop$1,
contentType: 'application/x-www-form-urlencoded; charset=UTF-8'
};
function noop$1() {}
var ajax = function (url, options) {
if (typeof url === 'object') {
options = url;
url = undefined;
}
options = options || {};
//
var settings = extend({}, options);
//
for (var key in _settings) {
if (settings[key] === undefined) {
settings[key] = _settings[key];
}
}
//
try {
var q = {};
var promise = new Promise(function (resolve, reject) {
q.resolve = resolve;
q.reject = reject;
});
promise.resolve = q.resolve;
promise.reject = q.reject;
settings.promise = promise;
} catch (e) {
//
settings.promise = {
resolve: noop$1,
reject: noop$1
};
}
// url
var ajaxLocParts = rurl.exec(window.location.href.toLowerCase()) || [];
settings.url = ((url || settings.url || window.location.href) + '').replace(/#.*$/, '').replace(/^\/\//, ajaxLocParts[1] + "//");
var cacheURL = settings.url;
// cross domain
if (!settings.crossDomain) {
settings.crossDomain = /^([\w-]+:)?\/\/([^\/]+)/.test(settings.url) && RegExp.$2 !== window.location.href;
}
//
var dataType = settings.dataType;
// jsonp
if (dataType === 'jsonp') {
//
var hasPlaceholder = /=\?/.test(settings.url);
if (!hasPlaceholder) {
var jsonpCallback = (settings.jsonp || 'callback') + '=?';
settings.url = appendQuery(settings.url, jsonpCallback);
}
return JSONP(settings);
}
//
serializeData(settings);
var mime = settings.accepts[dataType] || settings.accepts['*']; // mime
var baseHeader = {}; // header
/^([\w-]+:)\/\//.test(settings.url) ? RegExp.$1 : window.location.protocol; // protocol
var xhr = _settings.xhr();
var abortTimeout;
// X-Requested-With header
// For cross-domain requests, seeing as conditions for a preflight are
// akin to a jigsaw puzzle, we simply never set it to be sure.
// (it can always be set on a per-request basis or even using ajaxSetup)
// For same-domain requests, won't change header if already provided.
if (!settings.crossDomain && !baseHeader['X-Requested-With']) {
baseHeader['X-Requested-With'] = 'XMLHttpRequest';
}
// Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
if (settings.ifModified) {
if (lastModified[cacheURL]) {
baseHeader['If-Modified-Since'] = lastModified[cacheURL];
}
if (etag[cacheURL]) {
baseHeader['If-None-Match'] = etag[cacheURL];
}
}
// mime
if (mime) {
//
baseHeader['Accept'] = mime;
if (mime.indexOf(',') > -1) {
mime = mime.split(',', 2)[0];
}
//
xhr.overrideMimeType && xhr.overrideMimeType(mime);
}
// not get and not head
var hasContent = !/^(?:GET|HEAD)$/.test(settings.type.toUpperCase());
//
if (settings.data && hasContent && settings.contentType !== false || options.contentType) {
baseHeader['Content-Type'] = settings.contentType;
}
// cache: default true
if (settings.cache === false && !hasContent) {
var rts = /([?&])_=[^&]*/;
settings.url = rts.test(cacheURL) ? cacheURL.replace(rts, "$1_=" + now$1()) : cacheURL + (/\?/.test(cacheURL) ? "&" : "?") + "_=" + now$1();
}
// headers
settings.headers = extend(baseHeader, settings.headers || {});
// on ready state change
xhr.onreadystatechange = function () {
// readystate
if (xhr.readyState === 4) {
clearTimeout(abortTimeout);
var result;
var error = false;
var isSuccess = xhr.status >= 200 && xhr.status < 300 || xhr.status === 304;
//
if (isSuccess) {
responseHeadersString = xhr.getAllResponseHeaders();
if (settings.ifModified) {
var modified = getResponseHeader('Last-Modified');
if (modified) {
lastModified[cacheURL] = modified;
}
modified = getResponseHeader('etag');
if (modified) {
etag[cacheURL] = modified;
}
}
dataType = dataType || mimeToDataType(xhr.getResponseHeader('content-type'));
result = xhr.responseText;
try {
// xml
if (dataType === 'xml') {
result = xhr.responseXML;
}
// json
else if (dataType === 'json') {
result = blankRE.test(result) ? null : JSON.parse(result);
}
} catch (e) {
error = e;
}
if (error) {
ajaxError(error, 'parseerror', xhr, settings);
} else {
ajaxSuccess(result, xhr, settings);
}
} else {
ajaxError(null, 'error', xhr, settings);
}
}
};
// async
var async = 'async' in settings ? settings.async : true;
// open
xhr.open(settings.type, settings.url, async, settings.username, settings.password);
// xhrFields
if (settings.xhrFields) {
for (var name in settings.xhrFields) {
xhr[name] = settings.xhrFields[name];
}
}
// Override mime type if needed
if (settings.mimeType && xhr.overrideMimeType) {
xhr.overrideMimeType(settings.mimeType);
}
// set request header
for (var name in settings.headers) {
// Support: IE<9
// IE's ActiveXObject throws a 'Type Mismatch' exception when setting
// request header to a null-value.
//
// To keep consistent with other XHR implementations, cast the value
// to string and ignore `undefined`.
if (settings.headers[name] !== undefined) {
xhr.setRequestHeader(name, settings.headers[name] + "");
}
}
// before send
if (ajaxBeforeSend(xhr, settings) === false) {
xhr.abort();
return false;
}
// timeout
if (settings.timeout > 0) {
abortTimeout = window.setTimeout(function () {
xhr.onreadystatechange = noop$1;
xhr.abort();
ajaxError(null, 'timeout', xhr, settings);
}, settings.timeout);
}
// send
xhr.send(settings.data ? settings.data : null);
// abort method
settings.promise.abort = function () {
xhr.abort();
};
//
return settings.promise;
};
/*
* method get
* */
ajax.get = function (url, data, success, dataType) {
if (isFunction(data)) {
dataType = dataType || success;
success = data;
data = undefined;
}
return ajax({
url: url,
data: data,
success: success,
dataType: dataType
});
};
/*
* method post
*
* dataType:
* */
ajax.post = function (url, data, success, dataType) {
if (isFunction(data)) {
dataType = dataType || success;
success = data;
data = undefined;
}
return ajax({
type: 'POST',
url: url,
data: data,
success: success,
dataType: dataType
});
};
/*
* method getJSON
* */
ajax.getJSON = function (url, data, success) {
if (isFunction(data)) {
success = data;
data = undefined;
}
return ajax({
url: url,
data: data,
success: success,
dataType: 'json'
});
};
/*
* method ajaxSetup
* */
ajax.ajaxSetup = function (target, settings) {
return settings ? extend(extend(target, _settings), settings) : extend(_settings, target);
};
/*
* utils
*
* */
// triggers and extra global event ajaxBeforeSend that's like ajaxSend but cancelable
function ajaxBeforeSend(xhr, settings) {
var context = settings.context;
//
if (settings.beforeSend.call(context, xhr, settings) === false) {
return false;
}
}
// ajax success
function ajaxSuccess(data, xhr, settings) {
var context = settings.context;
var status = 'success';
settings.success.call(context, data, status, xhr);
settings.promise.resolve(data, status, xhr);
ajaxComplete(status, xhr, settings);
}
// status: "success", "notmodified", "error", "timeout", "abort", "parsererror"
function ajaxComplete(status, xhr, settings) {
var context = settings.context;
settings.complete.call(context, xhr, status);
}
// type: "timeout", "error", "abort", "parsererror"
function ajaxError(error, type, xhr, settings) {
var context = settings.context;
settings.error.call(context, xhr, type, error);
settings.promise.reject(xhr, type, error);
ajaxComplete(type, xhr, settings);
}
function getResponseHeader(key) {
var match;
if (!responseHeaders) {
responseHeaders = {};
while (match = rheaders.exec(responseHeadersString)) {
responseHeaders[match[1].toLowerCase()] = match[2];
}
match = responseHeaders[key.toLowerCase()];
}
return match === null ? null : match;
}
// jsonp
/*
* tks: https://www.cnblogs.com/rubylouvre/archive/2011/02/13/1953087.html
* */
function JSONP(options) {
//
var callbackName = options.jsonpCallback || 'jsonp' + now$1();
var script = window.document.createElement('script');
var abort = function () {
// 设置 window.xxx = noop
if (callbackName in window) {
window[callbackName] = noop$1;
}
};
var xhr = {
abort: abort
};
var abortTimeout;
var head = window.document.getElementsByTagName('head')[0] || window.document.documentElement;
// ie8+
script.onerror = function (error) {
_error(error);
};
function _error(error) {
window.clearTimeout(abortTimeout);
xhr.abort();
ajaxError(error.type, xhr, error.type, options);
_removeScript();
}
window[callbackName] = function (data) {
window.clearTimeout(abortTimeout);
ajaxSuccess(data, xhr, options);
_removeScript();
};
//
serializeData(options);
script.src = options.url.replace(/=\?/, '=' + callbackName);
//
script.src = appendQuery(script.src, '_=' + new Date().getTime());
//
script.async = true;
// script charset
if (options.scriptCharset) {
script.charset = options.scriptCharset;
}
//
head.insertBefore(script, head.firstChild);
//
if (options.timeout > 0) {
abortTimeout = window.setTimeout(function () {
xhr.abort();
ajaxError('timeout', xhr, 'timeout', options);
_removeScript();
}, options.timeout);
}
// remove script
function _removeScript() {
if (script.clearAttributes) {
script.clearAttributes();
} else {
script.onload = script.onreadystatechange = script.onerror = null;
}
if (script.parentNode) {
script.parentNode.removeChild(script);
}
//
script = null;
delete window[callbackName];
}
// add abort
options.promise.abort = function () {
xhr.abort();
};
// add xhr
options.promise.xhr = xhr;
return options.promise;
}
// mime to data type
function mimeToDataType(mime) {
return mime && (mime === htmlType ? 'html' : mime === jsonType ? 'json' : xmlTypeRE.test(mime) && 'xml') || 'text';
}
// append query
function appendQuery(url, query) {
return (url + '&' + query).replace(/[&?]{1,2}/, '?');
}
// serialize data
function serializeData(options) {
// formData
if (isObject$1(options) && !isFormData(options.data) && options.processData) {
options.data = param(options.data);
}
if (options.data && (!options.type || options.type.toUpperCase() === 'GET')) {
options.url = appendQuery(options.url, options.data);
}
}
// serialize
function serialize(params, obj, traditional, scope) {
var _isArray = isArray(obj);
for (var key in obj) {
var value = obj[key];
if (scope) {
key = traditional ? scope : scope + '[' + (_isArray ? '' : key) + ']';
}
// handle data in serializeArray format
if (!scope && _isArray) {
params.add(value.name, value.value);
} else if (traditional ? _isArray(value) : isObject$1(value)) {
serialize(params, value, traditional, key);
} else {
params.add(key, value);
}
}
}
// param
function param(obj, traditional) {
var params = [];
//
params.add = function (k, v) {
this.push(encodeURIComponent(k) + '=' + encodeURIComponent(v));
};
serialize(params, obj, traditional);
return params.join('&').replace('%20', '+');
}
// extend
function extend(target) {
var slice = Array.prototype.slice;
var args = slice.call(arguments, 1);
//
for (var i = 0, length = args.length; i < length; i++) {
var source = args[i] || {};
for (var key in source) {
if (source.hasOwnProperty(key) && source[key] !== undefined) {
target[key] = source[key];
}
}
}
return target;
}
// is object
function isObject$1(obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
}
// is formData
function isFormData(obj) {
return obj instanceof FormData;
}
// is array
function isArray(value) {
return Object.prototype.toString.call(value) === "[object Array]";
}
// is function
function isFunction(value) {
return typeof value === "function";
}
function now$1() {
return new Date().getTime();
}
function checkInt(value) {
return parseInt(value) === value;
}
function checkInts(arrayish) {
if (!checkInt(arrayish.length)) {
return false;
}
for (var i = 0; i < arrayish.length; i++) {
if (!checkInt(arrayish[i]) || arrayish[i] < 0 || arrayish[i] > 255) {
return false;
}
}
return true;
}
function coerceArray(arg, copy) {
// ArrayBuffer view
if (arg.buffer && arg.name === 'Uint8Array') {
if (copy) {
if (arg.slice) {
arg = arg.slice();
} else {
arg = Array.prototype.slice.call(arg);
}
}
return arg;
}
// It's an array; check it is a valid representation of a byte
if (Array.isArray(arg)) {
if (!checkInts(arg)) {
throw new Error('Array contains invalid value: ' + arg);
}
return new Uint8Array(arg);
}
// Something else, but behaves like an array (maybe a Buffer? Arguments?)
if (checkInt(arg.length) && checkInts(arg)) {
return new Uint8Array(arg);
}
throw new Error('unsupported array-like object');
}
function createArray(length) {
return new Uint8Array(length);
}
function copyArray(sourceArray, targetArray, targetStart, sourceStart, sourceEnd) {
if (sourceStart != null || sourceEnd != null) {
if (sourceArray.slice) {
sourceArray = sourceArray.slice(sourceStart, sourceEnd);
} else {
sourceArray = Array.prototype.slice.call(sourceArray, sourceStart, sourceEnd);
}
}
targetArray.set(sourceArray, targetStart);
}
var convertUtf8 = function () {
function toBytes(text) {
var result = [],
i = 0;
text = encodeURI(text);
while (i < text.length) {
var c = text.charCodeAt(i++);
// if it is a % sign, encode the following 2 bytes as a hex value
if (c === 37) {
result.push(parseInt(text.substr(i, 2), 16));
i += 2;
// otherwise, just the actual byte
} else {
result.push(c);
}
}
return coerceArray(result);
}
function fromBytes(bytes) {
var result = [],
i = 0;
while (i < bytes.length) {
var c = bytes[i];
if (c < 128) {
result.push(String.fromCharCode(c));
i++;
} else if (c > 191 && c < 224) {
result.push(String.fromCharCode((c & 0x1f) << 6 | bytes[i + 1] & 0x3f));
i += 2;
} else {
result.push(String.fromCharCode((c & 0x0f) << 12 | (bytes[i + 1] & 0x3f) << 6 | bytes[i + 2] & 0x3f));
i += 3;
}
}
return result.join('');
}
return {
toBytes: toBytes,
fromBytes: fromBytes
};
}();
var convertHex = function () {
function toBytes(text) {
var result = [];
for (var i = 0; i < text.length; i += 2) {
result.push(parseInt(text.substr(i, 2), 16));
}
return result;
}
// http://ixti.net/development/javascript/2011/11/11/base64-encodedecode-of-utf8-in-browser-with-js.html
var Hex = '0123456789abcdef';
function fromBytes(bytes) {
var result = [];
for (var i = 0; i < bytes.length; i++) {
var v = bytes[i];
result.push(Hex[(v & 0xf0) >> 4] + Hex[v & 0x0f]);
}
return result.join('');
}
return {
toBytes: toBytes,
fromBytes: fromBytes
};
}();
// Number of rounds by keysize
var numberOfRounds = {
16: 10,
24: 12,
32: 14
};
// Round constant words
var rcon = [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91];
// S-box and Inverse S-box (S is for Substitution)
var S = [0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, 0xd0, 0xef, 0xaa, 0x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x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d];
// Transformations for encryption
var T1 = [0xc66363a5, 0xf87c7c84, 0xee777799, 0xf67b7b8d, 0xfff2f20d, 0xd66b6bbd, 0xde6f6fb1, 0x91c5c554, 0x60303050, 0x02010103, 0xce6767a9, 0x562b2b7d, 0xe7fefe19, 0xb5d7d762, 0x4dababe6, 0xec76769a, 0x8fcaca45, 0x1f82829d, 0x89c9c940, 0xfa7d7d87, 0xeffafa15, 0xb25959eb, 0x8e4747c9, 0xfbf0f00b, 0x41adadec, 0xb3d4d467, 0x5fa2a2fd, 0x45afafea, 0x239c9cbf, 0x53a4a4f7, 0xe4727296, 0x9bc0c05b, 0x75b7b7c2, 0xe1fdfd1c, 0x3d9393ae, 0x4c26266a, 0x6c36365a, 0x7e3f3f41, 0xf5f7f702, 0x83cccc4f, 0x6834345c, 0x51a5a5f4, 0xd1e5e534, 0xf9f1f108, 0xe2717193, 0xabd8d873, 0x62313153, 0x2a15153f, 0x0804040c, 0x95c7c752, 0x46232365, 0x9dc3c35e, 0x30181828, 0x379696a1, 0x0a05050f, 0x2f9a9ab5, 0x0e070709, 0x24121236, 0x1b80809b, 0xdfe2e23d, 0xcdebeb26, 0x4e272769, 0x7fb2b2cd, 0xea75759f, 0x1209091b, 0x1d83839e, 0x582c2c74, 0x341a1a2e, 0x361b1b2d, 0xdc6e6eb2, 0xb45a5aee, 0x5ba0a0fb, 0xa45252f6, 0x763b3b4d, 0xb7d6d661, 0x7db3b3ce, 0x5229297b, 0xdde3e33e, 0x5e2f2f71, 0x13848497, 0xa65353f5, 0xb9d1d168, 0x00000000, 0xc1eded2c, 0x40202060, 0xe3fcfc1f, 0x79b1b1c8, 0xb65b5bed, 0xd46a6abe, 0x8dcbcb46, 0x67bebed9, 0x7239394b, 0x944a4ade, 0x984c4cd4, 0xb05858e8, 0x85cfcf4a, 0xbbd0d06b, 0xc5efef2a, 0x4faaaae5, 0xedfbfb16, 0x864343c5, 0x9a4d4dd7, 0x66333355, 0x11858594, 0x8a4545cf, 0xe9f9f910, 0x04020206, 0xfe7f7f81, 0xa05050f0, 0x783c3c44, 0x259f9fba, 0x4ba8a8e3, 0xa25151f3, 0x5da3a3fe, 0x804040c0, 0x058f8f8a, 0x3f9292ad, 0x219d9dbc, 0x70383848, 0xf1f5f504, 0x63bcbcdf, 0x77b6b6c1, 0xafdada75, 0x42212163, 0x20101030, 0xe5ffff1a, 0xfdf3f30e, 0xbfd2d26d, 0x81cdcd4c, 0x180c0c14, 0x26131335, 0xc3ecec2f, 0xbe5f5fe1, 0x359797a2, 0x884444cc, 0x2e171739, 0x93c4c457, 0x55a7a7f2, 0xfc7e7e82, 0x7a3d3d47, 0xc86464ac, 0xba5d5de7, 0x3219192b, 0xe6737395, 0xc06060a0, 0x19818198, 0x9e4f4fd1, 0xa3dcdc7f, 0x44222266, 0x542a2a7e, 0x3b9090ab, 0x0b888883, 0x8c4646ca, 0xc7eeee29, 0x6bb8b8d3, 0x2814143c, 0xa7dede79, 0xbc5e5ee2, 0x160b0b1d, 0xaddbdb76, 0xdbe0e03b, 0x64323256, 0x743a3a4e, 0x140a0a1e, 0x924949db, 0x0c06060a, 0x4824246c, 0xb85c5ce4, 0x9fc2c25d, 0xbdd3d36e, 0x43acacef, 0xc46262a6, 0x399191a8, 0x319595a4, 0xd3e4e437, 0xf279798b, 0xd5e7e732, 0x8bc8c843, 0x6e373759, 0xda6d6db7, 0x018d8d8c, 0xb1d5d564, 0x9c4e4ed2, 0x49a9a9e0, 0xd86c6cb4, 0xac5656fa, 0xf3f4f407, 0xcfeaea25, 0xca6565af, 0xf47a7a8e, 0x47aeaee9, 0x10080818, 0x6fbabad5, 0xf0787888, 0x4a25256f, 0x5c2e2e72, 0x381c1c24, 0x57a6a6f1, 0x73b4b4c7, 0x97c6c651, 0xcbe8e823, 0xa1dddd7c, 0xe874749c, 0x3e1f1f21, 0x964b4bdd, 0x61bdbddc, 0x0d8b8b86, 0x0f8a8a85, 0xe0707090, 0x7c3e3e42, 0x71b5b5c4, 0xcc6666aa, 0x904848d8, 0x06030305, 0xf7f6f601, 0x1c0e0e12, 0xc26161a3, 0x6a35355f, 0xae5757f9, 0x69b9b9d0, 0x17868691, 0x99c1c158, 0x3a1d1d27, 0x279e9eb9, 0xd9e1e138, 0xebf8f813, 0x2b9898b3, 0x22111133, 0xd26969bb, 0xa9d9d970, 0x078e8e89, 0x339494a7, 0x2d9b9bb6, 0x3c1e1e22, 0x15878792, 0xc9e9e920, 0x87cece49, 0xaa5555ff, 0x50282878, 0xa5dfdf7a, 0x038c8c8f, 0x59a1a1f8, 0x09898980, 0x1a0d0d17, 0x65bfbfda, 0xd7e6e631, 0x844242c6, 0xd06868b8, 0x824141c3, 0x299999b0, 0x5a2d2d77, 0x1e0f0f11, 0x7bb0b0cb, 0xa85454fc, 0x6dbbbbd6, 0x2c16163a];
var T2 = [0xa5c66363, 0x84f87c7c, 0x99ee7777, 0x8df67b7b, 0x0dfff2f2, 0xbdd66b6b, 0xb1de6f6f, 0x5491c5c5, 0x50603030, 0x03020101, 0xa9ce6767, 0x7d562b2b, 0x19e7fefe, 0x62b5d7d7, 0xe64dabab, 0x9aec7676, 0x458fcaca, 0x9d1f8282, 0x4089c9c9, 0x87fa7d7d, 0x15effafa, 0xebb25959, 0xc98e4747, 0x0bfbf0f0, 0xec41adad, 0x67b3d4d4, 0xfd5fa2a2, 0xea45afaf, 0xbf239c9c, 0xf753a4a4, 0x96e47272, 0x5b9bc0c0, 0xc275b7b7, 0x1ce1fdfd, 0xae3d9393, 0x6a4c2626, 0x5a6c3636, 0x417e3f3f, 0x02f5f7f7, 0x4f83cccc, 0x5c683434, 0xf451a5a5, 0x34d1e5e5, 0x08f9f1f1, 0x93e27171, 0x73abd8d8, 0x53623131, 0x3f2a1515, 0x0c080404, 0x5295c7c7, 0x65462323, 0x5e9dc3c3, 0x28301818, 0xa1379696, 0x0f0a0505, 0xb52f9a9a, 0x090e0707, 0x36241212, 0x9b1b8080, 0x3ddfe2e2, 0x26cdebeb, 0x694e2727, 0xcd7fb2b2, 0x9fea7575, 0x1b120909, 0x9e1d8383, 0x74582c2c, 0x2e341a1a, 0x2d361b1b, 0xb2dc6e6e, 0xeeb45a5a, 0xfb5ba0a0, 0xf6a45252, 0x4d763b3b, 0x61b7d6d6, 0xce7db3b3, 0x7b522929, 0x3edde3e3, 0x715e2f2f, 0x97138484, 0xf5a65353, 0x68b9d1d1, 0x00000000, 0x2cc1eded, 0x60402020, 0x1fe3fcfc, 0xc879b1b1, 0xedb65b5b, 0xbed46a6a, 0x468dcbcb, 0xd967bebe, 0x4b723939, 0xde944a4a, 0xd4984c4c, 0xe8b05858, 0x4a85cfcf, 0x6bbbd0d0, 0x2ac5efef, 0xe54faaaa, 0x16edfbfb, 0xc5864343, 0xd79a4d4d, 0x55663333, 0x94118585, 0xcf8a4545, 0x10e9f9f9, 0x06040202, 0x81fe7f7f, 0xf0a05050, 0x44783c3c, 0xba259f9f, 0xe34ba8a8, 0xf3a25151, 0xfe5da3a3, 0xc0804040, 0x8a058f8f, 0xad3f9292, 0xbc219d9d, 0x48703838, 0x04f1f5f5, 0xdf63bcbc, 0xc177b6b6, 0x75afdada, 0x63422121, 0x30201010, 0x1ae5ffff, 0x0efdf3f3, 0x6dbfd2d2, 0x4c81cdcd, 0x14180c0c, 0x35261313, 0x2fc3ecec, 0xe1be5f5f, 0xa2359797, 0xcc884444, 0x392e1717, 0x5793c4c4, 0xf255a7a7, 0x82fc7e7e, 0x477a3d3d, 0xacc86464, 0xe7ba5d5d, 0x2b321919, 0x95e67373, 0xa0c06060, 0x98198181, 0xd19e4f4f, 0x7fa3dcdc, 0x66442222, 0x7e542a2a, 0xab3b9090, 0x830b8888, 0xca8c4646, 0x29c7eeee, 0xd36bb8b8, 0x3c281414, 0x79a7dede, 0xe2bc5e5e, 0x1d160b0b, 0x76addbdb, 0x3bdbe0e0, 0x56643232, 0x4e743a3a, 0x1e140a0a, 0xdb924949, 0x0a0c0606, 0x6c482424, 0xe4b85c5c, 0x5d9fc2c2, 0x6ebdd3d3, 0xef43acac, 0xa6c46262, 0xa8399191, 0xa4319595, 0x37d3e4e4, 0x8bf27979, 0x32d5e7e7, 0x438bc8c8, 0x596e3737, 0xb7da6d6d, 0x8c018d8d, 0x64b1d5d5, 0xd29c4e4e, 0xe049a9a9, 0xb4d86c6c, 0xfaac5656, 0x07f3f4f4, 0x25cfeaea, 0xafca6565, 0x8ef47a7a, 0xe947aeae, 0x18100808, 0xd56fbaba, 0x88f07878, 0x6f4a2525, 0x725c2e2e, 0x24381c1c, 0xf157a6a6, 0xc773b4b4, 0x5197c6c6, 0x23cbe8e8, 0x7ca1dddd, 0x9ce87474, 0x213e1f1f, 0xdd964b4b, 0xdc61bdbd, 0x860d8b8b, 0x850f8a8a, 0x90e07070, 0x427c3e3e, 0xc471b5b5, 0xaacc6666, 0xd8904848, 0x05060303, 0x01f7f6f6, 0x121c0e0e, 0xa3c26161, 0x5f6a3535, 0xf9ae5757, 0xd069b9b9, 0x91178686, 0x5899c1c1, 0x273a1d1d, 0xb9279e9e, 0x38d9e1e1, 0x13ebf8f8, 0xb32b9898, 0x33221111, 0xbbd26969, 0x70a9d9d9, 0x89078e8e, 0xa7339494, 0xb62d9b9b, 0x223c1e1e, 0x92158787, 0x20c9e9e9, 0x4987cece, 0xffaa5555, 0x78502828, 0x7aa5dfdf, 0x8f038c8c, 0xf859a1a1, 0x80098989, 0x171a0d0d, 0xda65bfbf, 0x31d7e6e6, 0xc6844242, 0xb8d06868, 0xc3824141, 0xb0299999, 0x775a2d2d, 0x111e0f0f, 0xcb7bb0b0, 0xfca85454, 0xd66dbbbb, 0x3a2c1616];
var T3 = [0x63a5c663, 0x7c84f87c, 0x7799ee77, 0x7b8df67b, 0xf20dfff2, 0x6bbdd66b, 0x6fb1de6f, 0xc55491c5, 0x30506030, 0x01030201, 0x67a9ce67, 0x2b7d562b, 0xfe19e7fe, 0xd762b5d7, 0xabe64dab, 0x769aec76, 0xca458fca, 0x829d1f82, 0xc94089c9, 0x7d87fa7d, 0xfa15effa, 0x59ebb259, 0x47c98e47, 0xf00bfbf0, 0xadec41ad, 0xd467b3d4, 0xa2fd5fa2, 0xafea45af, 0x9cbf239c, 0xa4f753a4, 0x7296e472, 0xc05b9bc0, 0xb7c275b7, 0xfd1ce1fd, 0x93ae3d93, 0x266a4c26, 0x365a6c36, 0x3f417e3f, 0xf702f5f7, 0xcc4f83cc, 0x345c6834, 0xa5f451a5, 0xe534d1e5, 0xf108f9f1, 0x7193e271, 0xd873abd8, 0x31536231, 0x153f2a15, 0x040c0804, 0xc75295c7, 0x23654623, 0xc35e9dc3, 0x18283018, 0x96a13796, 0x050f0a05, 0x9ab52f9a, 0x07090e07, 0x12362412, 0x809b1b80, 0xe23ddfe2, 0xeb26cdeb, 0x27694e27, 0xb2cd7fb2, 0x759fea75, 0x091b1209, 0x839e1d83, 0x2c74582c, 0x1a2e341a, 0x1b2d361b, 0x6eb2dc6e, 0x5aeeb45a, 0xa0fb5ba0, 0x52f6a452, 0x3b4d763b, 0xd661b7d6, 0xb3ce7db3, 0x297b5229, 0xe33edde3, 0x2f715e2f, 0x84971384, 0x53f5a653, 0xd168b9d1, 0x00000000, 0xed2cc1ed, 0x20604020, 0xfc1fe3fc, 0xb1c879b1, 0x5bedb65b, 0x6abed46a, 0xcb468dcb, 0xbed967be, 0x394b7239, 0x4ade944a, 0x4cd4984c, 0x58e8b058, 0xcf4a85cf, 0xd06bbbd0, 0xef2ac5ef, 0xaae54faa, 0xfb16edfb, 0x43c58643, 0x4dd79a4d, 0x33556633, 0x85941185, 0x45cf8a45, 0xf910e9f9, 0x02060402, 0x7f81fe7f, 0x50f0a050, 0x3c44783c, 0x9fba259f, 0xa8e34ba8, 0x51f3a251, 0xa3fe5da3, 0x40c08040, 0x8f8a058f, 0x92ad3f92, 0x9dbc219d, 0x38487038, 0xf504f1f5, 0xbcdf63bc, 0xb6c177b6, 0xda75afda, 0x21634221, 0x10302010, 0xff1ae5ff, 0xf30efdf3, 0xd26dbfd2, 0xcd4c81cd, 0x0c14180c, 0x13352613, 0xec2fc3ec, 0x5fe1be5f, 0x97a23597, 0x44cc8844, 0x17392e17, 0xc45793c4, 0xa7f255a7, 0x7e82fc7e, 0x3d477a3d, 0x64acc864, 0x5de7ba5d, 0x192b3219, 0x7395e673, 0x60a0c060, 0x81981981, 0x4fd19e4f, 0xdc7fa3dc, 0x22664422, 0x2a7e542a, 0x90ab3b90, 0x88830b88, 0x46ca8c46, 0xee29c7ee, 0xb8d36bb8, 0x143c2814, 0xde79a7de, 0x5ee2bc5e, 0x0b1d160b, 0xdb76addb, 0xe03bdbe0, 0x32566432, 0x3a4e743a, 0x0a1e140a, 0x49db9249, 0x060a0c06, 0x246c4824, 0x5ce4b85c, 0xc25d9fc2, 0xd36ebdd3, 0xacef43ac, 0x62a6c462, 0x91a83991, 0x95a43195, 0xe437d3e4, 0x798bf279, 0xe732d5e7, 0xc8438bc8, 0x37596e37, 0x6db7da6d, 0x8d8c018d, 0xd564b1d5, 0x4ed29c4e, 0xa9e049a9, 0x6cb4d86c, 0x56faac56, 0xf407f3f4, 0xea25cfea, 0x65afca65, 0x7a8ef47a, 0xaee947ae, 0x08181008, 0xbad56fba, 0x7888f078, 0x256f4a25, 0x2e725c2e, 0x1c24381c, 0xa6f157a6, 0xb4c773b4, 0xc65197c6, 0xe823cbe8, 0xdd7ca1dd, 0x749ce874, 0x1f213e1f, 0x4bdd964b, 0xbddc61bd, 0x8b860d8b, 0x8a850f8a, 0x7090e070, 0x3e427c3e, 0xb5c471b5, 0x66aacc66, 0x48d89048, 0x03050603, 0xf601f7f6, 0x0e121c0e, 0x61a3c261, 0x355f6a35, 0x57f9ae57, 0xb9d069b9, 0x86911786, 0xc15899c1, 0x1d273a1d, 0x9eb9279e, 0xe138d9e1, 0xf813ebf8, 0x98b32b98, 0x11332211, 0x69bbd269, 0xd970a9d9, 0x8e89078e, 0x94a73394, 0x9bb62d9b, 0x1e223c1e, 0x87921587, 0xe920c9e9, 0xce4987ce, 0x55ffaa55, 0x28785028, 0xdf7aa5df, 0x8c8f038c, 0xa1f859a1, 0x89800989, 0x0d171a0d, 0xbfda65bf, 0xe631d7e6, 0x42c68442, 0x68b8d068, 0x41c38241, 0x99b02999, 0x2d775a2d, 0x0f111e0f, 0xb0cb7bb0, 0x54fca854, 0xbbd66dbb, 0x163a2c16];
var T4 = [0x6363a5c6, 0x7c7c84f8, 0x777799ee, 0x7b7b8df6, 0xf2f20dff, 0x6b6bbdd6, 0x6f6fb1de, 0xc5c55491, 0x30305060, 0x01010302, 0x6767a9ce, 0x2b2b7d56, 0xfefe19e7, 0xd7d762b5, 0xababe64d, 0x76769aec, 0xcaca458f, 0x82829d1f, 0xc9c94089, 0x7d7d87fa, 0xfafa15ef, 0x5959ebb2, 0x4747c98e, 0xf0f00bfb, 0xadadec41, 0xd4d467b3, 0xa2a2fd5f, 0xafafea45, 0x9c9cbf23, 0xa4a4f753, 0x727296e4, 0xc0c05b9b, 0xb7b7c275, 0xfdfd1ce1, 0x9393ae3d, 0x26266a4c, 0x36365a6c, 0x3f3f417e, 0xf7f702f5, 0xcccc4f83, 0x34345c68, 0xa5a5f451, 0xe5e534d1, 0xf1f108f9, 0x717193e2, 0xd8d873ab, 0x31315362, 0x15153f2a, 0x04040c08, 0xc7c75295, 0x23236546, 0xc3c35e9d, 0x18182830, 0x9696a137, 0x05050f0a, 0x9a9ab52f, 0x0707090e, 0x12123624, 0x80809b1b, 0xe2e23ddf, 0xebeb26cd, 0x2727694e, 0xb2b2cd7f, 0x75759fea, 0x09091b12, 0x83839e1d, 0x2c2c7458, 0x1a1a2e34, 0x1b1b2d36, 0x6e6eb2dc, 0x5a5aeeb4, 0xa0a0fb5b, 0x5252f6a4, 0x3b3b4d76, 0xd6d661b7, 0xb3b3ce7d, 0x29297b52, 0xe3e33edd, 0x2f2f715e, 0x84849713, 0x5353f5a6, 0xd1d168b9, 0x00000000, 0xeded2cc1, 0x20206040, 0xfcfc1fe3, 0xb1b1c879, 0x5b5bedb6, 0x6a6abed4, 0xcbcb468d, 0xbebed967, 0x39394b72, 0x4a4ade94, 0x4c4cd498, 0x5858e8b0, 0xcfcf4a85, 0xd0d06bbb, 0xefef2ac5, 0xaaaae54f, 0xfbfb16ed, 0x4343c586, 0x4d4dd79a, 0x33335566, 0x85859411, 0x4545cf8a, 0xf9f910e9, 0x02020604, 0x7f7f81fe, 0x5050f0a0, 0x3c3c4478, 0x9f9fba25, 0xa8a8e34b, 0x5151f3a2, 0xa3a3fe5d, 0x4040c080, 0x8f8f8a05, 0x9292ad3f, 0x9d9dbc21, 0x38384870, 0xf5f504f1, 0xbcbcdf63, 0xb6b6c177, 0xdada75af, 0x21216342, 0x10103020, 0xffff1ae5, 0xf3f30efd, 0xd2d26dbf, 0xcdcd4c81, 0x0c0c1418, 0x13133526, 0xecec2fc3, 0x5f5fe1be, 0x9797a235, 0x4444cc88, 0x1717392e, 0xc4c45793, 0xa7a7f255, 0x7e7e82fc, 0x3d3d477a, 0x6464acc8, 0x5d5de7ba, 0x19192b32, 0x737395e6, 0x6060a0c0, 0x81819819, 0x4f4fd19e, 0xdcdc7fa3, 0x22226644, 0x2a2a7e54, 0x9090ab3b, 0x8888830b, 0x4646ca8c, 0xeeee29c7, 0xb8b8d36b, 0x14143c28, 0xdede79a7, 0x5e5ee2bc, 0x0b0b1d16, 0xdbdb76ad, 0xe0e03bdb, 0x32325664, 0x3a3a4e74, 0x0a0a1e14, 0x4949db92, 0x06060a0c, 0x24246c48, 0x5c5ce4b8, 0xc2c25d9f, 0xd3d36ebd, 0xacacef43, 0x6262a6c4, 0x9191a839, 0x9595a431, 0xe4e437d3, 0x79798bf2, 0xe7e732d5, 0xc8c8438b, 0x3737596e, 0x6d6db7da, 0x8d8d8c01, 0xd5d564b1, 0x4e4ed29c, 0xa9a9e049, 0x6c6cb4d8, 0x5656faac, 0xf4f407f3, 0xeaea25cf, 0x6565afca, 0x7a7a8ef4, 0xaeaee947, 0x08081810, 0xbabad56f, 0x787888f0, 0x25256f4a, 0x2e2e725c, 0x1c1c2438, 0xa6a6f157, 0xb4b4c773, 0xc6c65197, 0xe8e823cb, 0xdddd7ca1, 0x74749ce8, 0x1f1f213e, 0x4b4bdd96, 0xbdbddc61, 0x8b8b860d, 0x8a8a850f, 0x707090e0, 0x3e3e427c, 0xb5b5c471, 0x6666aacc, 0x4848d890, 0x03030506, 0xf6f601f7, 0x0e0e121c, 0x6161a3c2, 0x35355f6a, 0x5757f9ae, 0xb9b9d069, 0x86869117, 0xc1c15899, 0x1d1d273a, 0x9e9eb927, 0xe1e138d9, 0xf8f813eb, 0x9898b32b, 0x11113322, 0x6969bbd2, 0xd9d970a9, 0x8e8e8907, 0x9494a733, 0x9b9bb62d, 0x1e1e223c, 0x87879215, 0xe9e920c9, 0xcece4987, 0x5555ffaa, 0x28287850, 0xdfdf7aa5, 0x8c8c8f03, 0xa1a1f859, 0x89898009, 0x0d0d171a, 0xbfbfda65, 0xe6e631d7, 0x4242c684, 0x6868b8d0, 0x4141c382, 0x9999b029, 0x2d2d775a, 0x0f0f111e, 0xb0b0cb7b, 0x5454fca8, 0xbbbbd66d, 0x16163a2c];
// Transformations for decryption
var T5 = [0x51f4a750, 0x7e416553, 0x1a17a4c3, 0x3a275e96, 0x3bab6bcb, 0x1f9d45f1, 0xacfa58ab, 0x4be30393, 0x2030fa55, 0xad766df6, 0x88cc7691, 0xf5024c25, 0x4fe5d7fc, 0xc52acbd7, 0x26354480, 0xb562a38f, 0xdeb15a49, 0x25ba1b67, 0x45ea0e98, 0x5dfec0e1, 0xc32f7502, 0x814cf012, 0x8d4697a3, 0x6bd3f9c6, 0x038f5fe7, 0x15929c95, 0xbf6d7aeb, 0x955259da, 0xd4be832d, 0x587421d3, 0x49e06929, 0x8ec9c844, 0x75c2896a, 0xf48e7978, 0x99583e6b, 0x27b971dd, 0xbee14fb6, 0xf088ad17, 0xc920ac66, 0x7dce3ab4, 0x63df4a18, 0xe51a3182, 0x97513360, 0x62537f45, 0xb16477e0, 0xbb6bae84, 0xfe81a01c, 0xf9082b94, 0x70486858, 0x8f45fd19, 0x94de6c87, 0x527bf8b7, 0xab73d323, 0x724b02e2, 0xe31f8f57, 0x6655ab2a, 0xb2eb2807, 0x2fb5c203, 0x86c57b9a, 0xd33708a5, 0x302887f2, 0x23bfa5b2, 0x02036aba, 0xed16825c, 0x8acf1c2b, 0xa779b492, 0xf307f2f0, 0x4e69e2a1, 0x65daf4cd, 0x0605bed5, 0xd134621f, 0xc4a6fe8a, 0x342e539d, 0xa2f355a0, 0x058ae132, 0xa4f6eb75, 0x0b83ec39, 0x4060efaa, 0x5e719f06, 0xbd6e1051, 0x3e218af9, 0x96dd063d, 0xdd3e05ae, 0x4de6bd46, 0x91548db5, 0x71c45d05, 0x0406d46f, 0x605015ff, 0x1998fb24, 0xd6bde997, 0x894043cc, 0x67d99e77, 0xb0e842bd, 0x07898b88, 0xe7195b38, 0x79c8eedb, 0xa17c0a47, 0x7c420fe9, 0xf8841ec9, 0x00000000, 0x09808683, 0x322bed48, 0x1e1170ac, 0x6c5a724e, 0xfd0efffb, 0x0f853856, 0x3daed51e, 0x362d3927, 0x0a0fd964, 0x685ca621, 0x9b5b54d1, 0x24362e3a, 0x0c0a67b1, 0x9357e70f, 0xb4ee96d2, 0x1b9b919e, 0x80c0c54f, 0x61dc20a2, 0x5a774b69, 0x1c121a16, 0xe293ba0a, 0xc0a02ae5, 0x3c22e043, 0x121b171d, 0x0e090d0b, 0xf28bc7ad, 0x2db6a8b9, 0x141ea9c8, 0x57f11985, 0xaf75074c, 0xee99ddbb, 0xa37f60fd, 0xf701269f, 0x5c72f5bc, 0x44663bc5, 0x5bfb7e34, 0x8b432976, 0xcb23c6dc, 0xb6edfc68, 0xb8e4f163, 0xd731dcca, 0x42638510, 0x13972240, 0x84c61120, 0x854a247d, 0xd2bb3df8, 0xaef93211, 0xc729a16d, 0x1d9e2f4b, 0xdcb230f3, 0x0d8652ec, 0x77c1e3d0, 0x2bb3166c, 0xa970b999, 0x119448fa, 0x47e96422, 0xa8fc8cc4, 0xa0f03f1a, 0x567d2cd8, 0x223390ef, 0x87494ec7, 0xd938d1c1, 0x8ccaa2fe, 0x98d40b36, 0xa6f581cf, 0xa57ade28, 0xdab78e26, 0x3fadbfa4, 0x2c3a9de4, 0x5078920d, 0x6a5fcc9b, 0x547e4662, 0xf68d13c2, 0x90d8b8e8, 0x2e39f75e, 0x82c3aff5, 0x9f5d80be, 0x69d0937c, 0x6fd52da9, 0xcf2512b3, 0xc8ac993b, 0x10187da7, 0xe89c636e, 0xdb3bbb7b, 0xcd267809, 0x6e5918f4, 0xec9ab701, 0x834f9aa8, 0xe6956e65, 0xaaffe67e, 0x21bccf08, 0xef15e8e6, 0xbae79bd9, 0x4a6f36ce, 0xea9f09d4, 0x29b07cd6, 0x31a4b2af, 0x2a3f2331, 0xc6a59430, 0x35a266c0, 0x744ebc37, 0xfc82caa6, 0xe090d0b0, 0x33a7d815, 0xf104984a, 0x41ecdaf7, 0x7fcd500e, 0x1791f62f, 0x764dd68d, 0x43efb04d, 0xccaa4d54, 0xe49604df, 0x9ed1b5e3, 0x4c6a881b, 0xc12c1fb8, 0x4665517f, 0x9d5eea04, 0x018c355d, 0xfa877473, 0xfb0b412e, 0xb3671d5a, 0x92dbd252, 0xe9105633, 0x6dd64713, 0x9ad7618c, 0x37a10c7a, 0x59f8148e, 0xeb133c89, 0xcea927ee, 0xb761c935, 0xe11ce5ed, 0x7a47b13c, 0x9cd2df59, 0x55f2733f, 0x1814ce79, 0x73c737bf, 0x53f7cdea, 0x5ffdaa5b, 0xdf3d6f14, 0x7844db86, 0xcaaff381, 0xb968c43e, 0x3824342c, 0xc2a3405f, 0x161dc372, 0xbce2250c, 0x283c498b, 0xff0d9541, 0x39a80171, 0x080cb3de, 0xd8b4e49c, 0x6456c190, 0x7bcb8461, 0xd532b670, 0x486c5c74, 0xd0b85742];
var T6 = [0x5051f4a7, 0x537e4165, 0xc31a17a4, 0x963a275e, 0xcb3bab6b, 0xf11f9d45, 0xabacfa58, 0x934be303, 0x552030fa, 0xf6ad766d, 0x9188cc76, 0x25f5024c, 0xfc4fe5d7, 0xd7c52acb, 0x80263544, 0x8fb562a3, 0x49deb15a, 0x6725ba1b, 0x9845ea0e, 0xe15dfec0, 0x02c32f75, 0x12814cf0, 0xa38d4697, 0xc66bd3f9, 0xe7038f5f, 0x9515929c, 0xebbf6d7a, 0xda955259, 0x2dd4be83, 0xd3587421, 0x2949e069, 0x448ec9c8, 0x6a75c289, 0x78f48e79, 0x6b99583e, 0xdd27b971, 0xb6bee14f, 0x17f088ad, 0x66c920ac, 0xb47dce3a, 0x1863df4a, 0x82e51a31, 0x60975133, 0x4562537f, 0xe0b16477, 0x84bb6bae, 0x1cfe81a0, 0x94f9082b, 0x58704868, 0x198f45fd, 0x8794de6c, 0xb7527bf8, 0x23ab73d3, 0xe2724b02, 0x57e31f8f, 0x2a6655ab, 0x07b2eb28, 0x032fb5c2, 0x9a86c57b, 0xa5d33708, 0xf2302887, 0xb223bfa5, 0xba02036a, 0x5ced1682, 0x2b8acf1c, 0x92a779b4, 0xf0f307f2, 0xa14e69e2, 0xcd65daf4, 0xd50605be, 0x1fd13462, 0x8ac4a6fe, 0x9d342e53, 0xa0a2f355, 0x32058ae1, 0x75a4f6eb, 0x390b83ec, 0xaa4060ef, 0x065e719f, 0x51bd6e10, 0xf93e218a, 0x3d96dd06, 0xaedd3e05, 0x464de6bd, 0xb591548d, 0x0571c45d, 0x6f0406d4, 0xff605015, 0x241998fb, 0x97d6bde9, 0xcc894043, 0x7767d99e, 0xbdb0e842, 0x8807898b, 0x38e7195b, 0xdb79c8ee, 0x47a17c0a, 0xe97c420f, 0xc9f8841e, 0x00000000, 0x83098086, 0x48322bed, 0xac1e1170, 0x4e6c5a72, 0xfbfd0eff, 0x560f8538, 0x1e3daed5, 0x27362d39, 0x640a0fd9, 0x21685ca6, 0xd19b5b54, 0x3a24362e, 0xb10c0a67, 0x0f9357e7, 0xd2b4ee96, 0x9e1b9b91, 0x4f80c0c5, 0xa261dc20, 0x695a774b, 0x161c121a, 0x0ae293ba, 0xe5c0a02a, 0x433c22e0, 0x1d121b17, 0x0b0e090d, 0xadf28bc7, 0xb92db6a8, 0xc8141ea9, 0x8557f119, 0x4caf7507, 0xbbee99dd, 0xfda37f60, 0x9ff70126, 0xbc5c72f5, 0xc544663b, 0x345bfb7e, 0x768b4329, 0xdccb23c6, 0x68b6edfc, 0x63b8e4f1, 0xcad731dc, 0x10426385, 0x40139722, 0x2084c611, 0x7d854a24, 0xf8d2bb3d, 0x11aef932, 0x6dc729a1, 0x4b1d9e2f, 0xf3dcb230, 0xec0d8652, 0xd077c1e3, 0x6c2bb316, 0x99a970b9, 0xfa119448, 0x2247e964, 0xc4a8fc8c, 0x1aa0f03f, 0xd8567d2c, 0xef223390, 0xc787494e, 0xc1d938d1, 0xfe8ccaa2, 0x3698d40b, 0xcfa6f581, 0x28a57ade, 0x26dab78e, 0xa43fadbf, 0xe42c3a9d, 0x0d507892, 0x9b6a5fcc, 0x62547e46, 0xc2f68d13, 0xe890d8b8, 0x5e2e39f7, 0xf582c3af, 0xbe9f5d80, 0x7c69d093, 0xa96fd52d, 0xb3cf2512, 0x3bc8ac99, 0xa710187d, 0x6ee89c63, 0x7bdb3bbb, 0x09cd2678, 0xf46e5918, 0x01ec9ab7, 0xa8834f9a, 0x65e6956e, 0x7eaaffe6, 0x0821bccf, 0xe6ef15e8, 0xd9bae79b, 0xce4a6f36, 0xd4ea9f09, 0xd629b07c, 0xaf31a4b2, 0x312a3f23, 0x30c6a594, 0xc035a266, 0x37744ebc, 0xa6fc82ca, 0xb0e090d0, 0x1533a7d8, 0x4af10498, 0xf741ecda, 0x0e7fcd50, 0x2f1791f6, 0x8d764dd6, 0x4d43efb0, 0x54ccaa4d, 0xdfe49604, 0xe39ed1b5, 0x1b4c6a88, 0xb8c12c1f, 0x7f466551, 0x049d5eea, 0x5d018c35, 0x73fa8774, 0x2efb0b41, 0x5ab3671d, 0x5292dbd2, 0x33e91056, 0x136dd647, 0x8c9ad761, 0x7a37a10c, 0x8e59f814, 0x89eb133c, 0xeecea927, 0x35b761c9, 0xede11ce5, 0x3c7a47b1, 0x599cd2df, 0x3f55f273, 0x791814ce, 0xbf73c737, 0xea53f7cd, 0x5b5ffdaa, 0x14df3d6f, 0x867844db, 0x81caaff3, 0x3eb968c4, 0x2c382434, 0x5fc2a340, 0x72161dc3, 0x0cbce225, 0x8b283c49, 0x41ff0d95, 0x7139a801, 0xde080cb3, 0x9cd8b4e4, 0x906456c1, 0x617bcb84, 0x70d532b6, 0x74486c5c, 0x42d0b857];
var T7 = [0xa75051f4, 0x65537e41, 0xa4c31a17, 0x5e963a27, 0x6bcb3bab, 0x45f11f9d, 0x58abacfa, 0x03934be3, 0xfa552030, 0x6df6ad76, 0x769188cc, 0x4c25f502, 0xd7fc4fe5, 0xcbd7c52a, 0x44802635, 0xa38fb562, 0x5a49deb1, 0x1b6725ba, 0x0e9845ea, 0xc0e15dfe, 0x7502c32f, 0xf012814c, 0x97a38d46, 0xf9c66bd3, 0x5fe7038f, 0x9c951592, 0x7aebbf6d, 0x59da9552, 0x832dd4be, 0x21d35874, 0x692949e0, 0xc8448ec9, 0x896a75c2, 0x7978f48e, 0x3e6b9958, 0x71dd27b9, 0x4fb6bee1, 0xad17f088, 0xac66c920, 0x3ab47dce, 0x4a1863df, 0x3182e51a, 0x33609751, 0x7f456253, 0x77e0b164, 0xae84bb6b, 0xa01cfe81, 0x2b94f908, 0x68587048, 0xfd198f45, 0x6c8794de, 0xf8b7527b, 0xd323ab73, 0x02e2724b, 0x8f57e31f, 0xab2a6655, 0x2807b2eb, 0xc2032fb5, 0x7b9a86c5, 0x08a5d337, 0x87f23028, 0xa5b223bf, 0x6aba0203, 0x825ced16, 0x1c2b8acf, 0xb492a779, 0xf2f0f307, 0xe2a14e69, 0xf4cd65da, 0xbed50605, 0x621fd134, 0xfe8ac4a6, 0x539d342e, 0x55a0a2f3, 0xe132058a, 0xeb75a4f6, 0xec390b83, 0xefaa4060, 0x9f065e71, 0x1051bd6e, 0x8af93e21, 0x063d96dd, 0x05aedd3e, 0xbd464de6, 0x8db59154, 0x5d0571c4, 0xd46f0406, 0x15ff6050, 0xfb241998, 0xe997d6bd, 0x43cc8940, 0x9e7767d9, 0x42bdb0e8, 0x8b880789, 0x5b38e719, 0xeedb79c8, 0x0a47a17c, 0x0fe97c42, 0x1ec9f884, 0x00000000, 0x86830980, 0xed48322b, 0x70ac1e11, 0x724e6c5a, 0xfffbfd0e, 0x38560f85, 0xd51e3dae, 0x3927362d, 0xd9640a0f, 0xa621685c, 0x54d19b5b, 0x2e3a2436, 0x67b10c0a, 0xe70f9357, 0x96d2b4ee, 0x919e1b9b, 0xc54f80c0, 0x20a261dc, 0x4b695a77, 0x1a161c12, 0xba0ae293, 0x2ae5c0a0, 0xe0433c22, 0x171d121b, 0x0d0b0e09, 0xc7adf28b, 0xa8b92db6, 0xa9c8141e, 0x198557f1, 0x074caf75, 0xddbbee99, 0x60fda37f, 0x269ff701, 0xf5bc5c72, 0x3bc54466, 0x7e345bfb, 0x29768b43, 0xc6dccb23, 0xfc68b6ed, 0xf163b8e4, 0xdccad731, 0x85104263, 0x22401397, 0x112084c6, 0x247d854a, 0x3df8d2bb, 0x3211aef9, 0xa16dc729, 0x2f4b1d9e, 0x30f3dcb2, 0x52ec0d86, 0xe3d077c1, 0x166c2bb3, 0xb999a970, 0x48fa1194, 0x642247e9, 0x8cc4a8fc, 0x3f1aa0f0, 0x2cd8567d, 0x90ef2233, 0x4ec78749, 0xd1c1d938, 0xa2fe8cca, 0x0b3698d4, 0x81cfa6f5, 0xde28a57a, 0x8e26dab7, 0xbfa43fad, 0x9de42c3a, 0x920d5078, 0xcc9b6a5f, 0x4662547e, 0x13c2f68d, 0xb8e890d8, 0xf75e2e39, 0xaff582c3, 0x80be9f5d, 0x937c69d0, 0x2da96fd5, 0x12b3cf25, 0x993bc8ac, 0x7da71018, 0x636ee89c, 0xbb7bdb3b, 0x7809cd26, 0x18f46e59, 0xb701ec9a, 0x9aa8834f, 0x6e65e695, 0xe67eaaff, 0xcf0821bc, 0xe8e6ef15, 0x9bd9bae7, 0x36ce4a6f, 0x09d4ea9f, 0x7cd629b0, 0xb2af31a4, 0x23312a3f, 0x9430c6a5, 0x66c035a2, 0xbc37744e, 0xcaa6fc82, 0xd0b0e090, 0xd81533a7, 0x984af104, 0xdaf741ec, 0x500e7fcd, 0xf62f1791, 0xd68d764d, 0xb04d43ef, 0x4d54ccaa, 0x04dfe496, 0xb5e39ed1, 0x881b4c6a, 0x1fb8c12c, 0x517f4665, 0xea049d5e, 0x355d018c, 0x7473fa87, 0x412efb0b, 0x1d5ab367, 0xd25292db, 0x5633e910, 0x47136dd6, 0x618c9ad7, 0x0c7a37a1, 0x148e59f8, 0x3c89eb13, 0x27eecea9, 0xc935b761, 0xe5ede11c, 0xb13c7a47, 0xdf599cd2, 0x733f55f2, 0xce791814, 0x37bf73c7, 0xcdea53f7, 0xaa5b5ffd, 0x6f14df3d, 0xdb867844, 0xf381caaf, 0xc43eb968, 0x342c3824, 0x405fc2a3, 0xc372161d, 0x250cbce2, 0x498b283c, 0x9541ff0d, 0x017139a8, 0xb3de080c, 0xe49cd8b4, 0xc1906456, 0x84617bcb, 0xb670d532, 0x5c74486c, 0x5742d0b8];
var T8 = [0xf4a75051, 0x4165537e, 0x17a4c31a, 0x275e963a, 0xab6bcb3b, 0x9d45f11f, 0xfa58abac, 0xe303934b, 0x30fa5520, 0x766df6ad, 0xcc769188, 0x024c25f5, 0xe5d7fc4f, 0x2acbd7c5, 0x35448026, 0x62a38fb5, 0xb15a49de, 0xba1b6725, 0xea0e9845, 0xfec0e15d, 0x2f7502c3, 0x4cf01281, 0x4697a38d, 0xd3f9c66b, 0x8f5fe703, 0x929c9515, 0x6d7aebbf, 0x5259da95, 0xbe832dd4, 0x7421d358, 0xe0692949, 0xc9c8448e, 0xc2896a75, 0x8e7978f4, 0x583e6b99, 0xb971dd27, 0xe14fb6be, 0x88ad17f0, 0x20ac66c9, 0xce3ab47d, 0xdf4a1863, 0x1a3182e5, 0x51336097, 0x537f4562, 0x6477e0b1, 0x6bae84bb, 0x81a01cfe, 0x082b94f9, 0x48685870, 0x45fd198f, 0xde6c8794, 0x7bf8b752, 0x73d323ab, 0x4b02e272, 0x1f8f57e3, 0x55ab2a66, 0xeb2807b2, 0xb5c2032f, 0xc57b9a86, 0x3708a5d3, 0x2887f230, 0xbfa5b223, 0x036aba02, 0x16825ced, 0xcf1c2b8a, 0x79b492a7, 0x07f2f0f3, 0x69e2a14e, 0xdaf4cd65, 0x05bed506, 0x34621fd1, 0xa6fe8ac4, 0x2e539d34, 0xf355a0a2, 0x8ae13205, 0xf6eb75a4, 0x83ec390b, 0x60efaa40, 0x719f065e, 0x6e1051bd, 0x218af93e, 0xdd063d96, 0x3e05aedd, 0xe6bd464d, 0x548db591, 0xc45d0571, 0x06d46f04, 0x5015ff60, 0x98fb2419, 0xbde997d6, 0x4043cc89, 0xd99e7767, 0xe842bdb0, 0x898b8807, 0x195b38e7, 0xc8eedb79, 0x7c0a47a1, 0x420fe97c, 0x841ec9f8, 0x00000000, 0x80868309, 0x2bed4832, 0x1170ac1e, 0x5a724e6c, 0x0efffbfd, 0x8538560f, 0xaed51e3d, 0x2d392736, 0x0fd9640a, 0x5ca62168, 0x5b54d19b, 0x362e3a24, 0x0a67b10c, 0x57e70f93, 0xee96d2b4, 0x9b919e1b, 0xc0c54f80, 0xdc20a261, 0x774b695a, 0x121a161c, 0x93ba0ae2, 0xa02ae5c0, 0x22e0433c, 0x1b171d12, 0x090d0b0e, 0x8bc7adf2, 0xb6a8b92d, 0x1ea9c814, 0xf1198557, 0x75074caf, 0x99ddbbee, 0x7f60fda3, 0x01269ff7, 0x72f5bc5c, 0x663bc544, 0xfb7e345b, 0x4329768b, 0x23c6dccb, 0xedfc68b6, 0xe4f163b8, 0x31dccad7, 0x63851042, 0x97224013, 0xc6112084, 0x4a247d85, 0xbb3df8d2, 0xf93211ae, 0x29a16dc7, 0x9e2f4b1d, 0xb230f3dc, 0x8652ec0d, 0xc1e3d077, 0xb3166c2b, 0x70b999a9, 0x9448fa11, 0xe9642247, 0xfc8cc4a8, 0xf03f1aa0, 0x7d2cd856, 0x3390ef22, 0x494ec787, 0x38d1c1d9, 0xcaa2fe8c, 0xd40b3698, 0xf581cfa6, 0x7ade28a5, 0xb78e26da, 0xadbfa43f, 0x3a9de42c, 0x78920d50, 0x5fcc9b6a, 0x7e466254, 0x8d13c2f6, 0xd8b8e890, 0x39f75e2e, 0xc3aff582, 0x5d80be9f, 0xd0937c69, 0xd52da96f, 0x2512b3cf, 0xac993bc8, 0x187da710, 0x9c636ee8, 0x3bbb7bdb, 0x267809cd, 0x5918f46e, 0x9ab701ec, 0x4f9aa883, 0x956e65e6, 0xffe67eaa, 0xbccf0821, 0x15e8e6ef, 0xe79bd9ba, 0x6f36ce4a, 0x9f09d4ea, 0xb07cd629, 0xa4b2af31, 0x3f23312a, 0xa59430c6, 0xa266c035, 0x4ebc3774, 0x82caa6fc, 0x90d0b0e0, 0xa7d81533, 0x04984af1, 0xecdaf741, 0xcd500e7f, 0x91f62f17, 0x4dd68d76, 0xefb04d43, 0xaa4d54cc, 0x9604dfe4, 0xd1b5e39e, 0x6a881b4c, 0x2c1fb8c1, 0x65517f46, 0x5eea049d, 0x8c355d01, 0x877473fa, 0x0b412efb, 0x671d5ab3, 0xdbd25292, 0x105633e9, 0xd647136d, 0xd7618c9a, 0xa10c7a37, 0xf8148e59, 0x133c89eb, 0xa927eece, 0x61c935b7, 0x1ce5ede1, 0x47b13c7a, 0xd2df599c, 0xf2733f55, 0x14ce7918, 0xc737bf73, 0xf7cdea53, 0xfdaa5b5f, 0x3d6f14df, 0x44db8678, 0xaff381ca, 0x68c43eb9, 0x24342c38, 0xa3405fc2, 0x1dc37216, 0xe2250cbc, 0x3c498b28, 0x0d9541ff, 0xa8017139, 0x0cb3de08, 0xb4e49cd8, 0x56c19064, 0xcb84617b, 0x32b670d5, 0x6c5c7448, 0xb85742d0];
// Transformations for decryption key expansion
var U1 = [0x00000000, 0x0e090d0b, 0x1c121a16, 0x121b171d, 0x3824342c, 0x362d3927, 0x24362e3a, 0x2a3f2331, 0x70486858, 0x7e416553, 0x6c5a724e, 0x62537f45, 0x486c5c74, 0x4665517f, 0x547e4662, 0x5a774b69, 0xe090d0b0, 0xee99ddbb, 0xfc82caa6, 0xf28bc7ad, 0xd8b4e49c, 0xd6bde997, 0xc4a6fe8a, 0xcaaff381, 0x90d8b8e8, 0x9ed1b5e3, 0x8ccaa2fe, 0x82c3aff5, 0xa8fc8cc4, 0xa6f581cf, 0xb4ee96d2, 0xbae79bd9, 0xdb3bbb7b, 0xd532b670, 0xc729a16d, 0xc920ac66, 0xe31f8f57, 0xed16825c, 0xff0d9541, 0xf104984a, 0xab73d323, 0xa57ade28, 0xb761c935, 0xb968c43e, 0x9357e70f, 0x9d5eea04, 0x8f45fd19, 0x814cf012, 0x3bab6bcb, 0x35a266c0, 0x27b971dd, 0x29b07cd6, 0x038f5fe7, 0x0d8652ec, 0x1f9d45f1, 0x119448fa, 0x4be30393, 0x45ea0e98, 0x57f11985, 0x59f8148e, 0x73c737bf, 0x7dce3ab4, 0x6fd52da9, 0x61dc20a2, 0xad766df6, 0xa37f60fd, 0xb16477e0, 0xbf6d7aeb, 0x955259da, 0x9b5b54d1, 0x894043cc, 0x87494ec7, 0xdd3e05ae, 0xd33708a5, 0xc12c1fb8, 0xcf2512b3, 0xe51a3182, 0xeb133c89, 0xf9082b94, 0xf701269f, 0x4de6bd46, 0x43efb04d, 0x51f4a750, 0x5ffdaa5b, 0x75c2896a, 0x7bcb8461, 0x69d0937c, 0x67d99e77, 0x3daed51e, 0x33a7d815, 0x21bccf08, 0x2fb5c203, 0x058ae132, 0x0b83ec39, 0x1998fb24, 0x1791f62f, 0x764dd68d, 0x7844db86, 0x6a5fcc9b, 0x6456c190, 0x4e69e2a1, 0x4060efaa, 0x527bf8b7, 0x5c72f5bc, 0x0605bed5, 0x080cb3de, 0x1a17a4c3, 0x141ea9c8, 0x3e218af9, 0x302887f2, 0x223390ef, 0x2c3a9de4, 0x96dd063d, 0x98d40b36, 0x8acf1c2b, 0x84c61120, 0xaef93211, 0xa0f03f1a, 0xb2eb2807, 0xbce2250c, 0xe6956e65, 0xe89c636e, 0xfa877473, 0xf48e7978, 0xdeb15a49, 0xd0b85742, 0xc2a3405f, 0xccaa4d54, 0x41ecdaf7, 0x4fe5d7fc, 0x5dfec0e1, 0x53f7cdea, 0x79c8eedb, 0x77c1e3d0, 0x65daf4cd, 0x6bd3f9c6, 0x31a4b2af, 0x3fadbfa4, 0x2db6a8b9, 0x23bfa5b2, 0x09808683, 0x07898b88, 0x15929c95, 0x1b9b919e, 0xa17c0a47, 0xaf75074c, 0xbd6e1051, 0xb3671d5a, 0x99583e6b, 0x97513360, 0x854a247d, 0x8b432976, 0xd134621f, 0xdf3d6f14, 0xcd267809, 0xc32f7502, 0xe9105633, 0xe7195b38, 0xf5024c25, 0xfb0b412e, 0x9ad7618c, 0x94de6c87, 0x86c57b9a, 0x88cc7691, 0xa2f355a0, 0xacfa58ab, 0xbee14fb6, 0xb0e842bd, 0xea9f09d4, 0xe49604df, 0xf68d13c2, 0xf8841ec9, 0xd2bb3df8, 0xdcb230f3, 0xcea927ee, 0xc0a02ae5, 0x7a47b13c, 0x744ebc37, 0x6655ab2a, 0x685ca621, 0x42638510, 0x4c6a881b, 0x5e719f06, 0x5078920d, 0x0a0fd964, 0x0406d46f, 0x161dc372, 0x1814ce79, 0x322bed48, 0x3c22e043, 0x2e39f75e, 0x2030fa55, 0xec9ab701, 0xe293ba0a, 0xf088ad17, 0xfe81a01c, 0xd4be832d, 0xdab78e26, 0xc8ac993b, 0xc6a59430, 0x9cd2df59, 0x92dbd252, 0x80c0c54f, 0x8ec9c844, 0xa4f6eb75, 0xaaffe67e, 0xb8e4f163, 0xb6edfc68, 0x0c0a67b1, 0x02036aba, 0x10187da7, 0x1e1170ac, 0x342e539d, 0x3a275e96, 0x283c498b, 0x26354480, 0x7c420fe9, 0x724b02e2, 0x605015ff, 0x6e5918f4, 0x44663bc5, 0x4a6f36ce, 0x587421d3, 0x567d2cd8, 0x37a10c7a, 0x39a80171, 0x2bb3166c, 0x25ba1b67, 0x0f853856, 0x018c355d, 0x13972240, 0x1d9e2f4b, 0x47e96422, 0x49e06929, 0x5bfb7e34, 0x55f2733f, 0x7fcd500e, 0x71c45d05, 0x63df4a18, 0x6dd64713, 0xd731dcca, 0xd938d1c1, 0xcb23c6dc, 0xc52acbd7, 0xef15e8e6, 0xe11ce5ed, 0xf307f2f0, 0xfd0efffb, 0xa779b492, 0xa970b999, 0xbb6bae84, 0xb562a38f, 0x9f5d80be, 0x91548db5, 0x834f9aa8, 0x8d4697a3];
var U2 = [0x00000000, 0x0b0e090d, 0x161c121a, 0x1d121b17, 0x2c382434, 0x27362d39, 0x3a24362e, 0x312a3f23, 0x58704868, 0x537e4165, 0x4e6c5a72, 0x4562537f, 0x74486c5c, 0x7f466551, 0x62547e46, 0x695a774b, 0xb0e090d0, 0xbbee99dd, 0xa6fc82ca, 0xadf28bc7, 0x9cd8b4e4, 0x97d6bde9, 0x8ac4a6fe, 0x81caaff3, 0xe890d8b8, 0xe39ed1b5, 0xfe8ccaa2, 0xf582c3af, 0xc4a8fc8c, 0xcfa6f581, 0xd2b4ee96, 0xd9bae79b, 0x7bdb3bbb, 0x70d532b6, 0x6dc729a1, 0x66c920ac, 0x57e31f8f, 0x5ced1682, 0x41ff0d95, 0x4af10498, 0x23ab73d3, 0x28a57ade, 0x35b761c9, 0x3eb968c4, 0x0f9357e7, 0x049d5eea, 0x198f45fd, 0x12814cf0, 0xcb3bab6b, 0xc035a266, 0xdd27b971, 0xd629b07c, 0xe7038f5f, 0xec0d8652, 0xf11f9d45, 0xfa119448, 0x934be303, 0x9845ea0e, 0x8557f119, 0x8e59f814, 0xbf73c737, 0xb47dce3a, 0xa96fd52d, 0xa261dc20, 0xf6ad766d, 0xfda37f60, 0xe0b16477, 0xebbf6d7a, 0xda955259, 0xd19b5b54, 0xcc894043, 0xc787494e, 0xaedd3e05, 0xa5d33708, 0xb8c12c1f, 0xb3cf2512, 0x82e51a31, 0x89eb133c, 0x94f9082b, 0x9ff70126, 0x464de6bd, 0x4d43efb0, 0x5051f4a7, 0x5b5ffdaa, 0x6a75c289, 0x617bcb84, 0x7c69d093, 0x7767d99e, 0x1e3daed5, 0x1533a7d8, 0x0821bccf, 0x032fb5c2, 0x32058ae1, 0x390b83ec, 0x241998fb, 0x2f1791f6, 0x8d764dd6, 0x867844db, 0x9b6a5fcc, 0x906456c1, 0xa14e69e2, 0xaa4060ef, 0xb7527bf8, 0xbc5c72f5, 0xd50605be, 0xde080cb3, 0xc31a17a4, 0xc8141ea9, 0xf93e218a, 0xf2302887, 0xef223390, 0xe42c3a9d, 0x3d96dd06, 0x3698d40b, 0x2b8acf1c, 0x2084c611, 0x11aef932, 0x1aa0f03f, 0x07b2eb28, 0x0cbce225, 0x65e6956e, 0x6ee89c63, 0x73fa8774, 0x78f48e79, 0x49deb15a, 0x42d0b857, 0x5fc2a340, 0x54ccaa4d, 0xf741ecda, 0xfc4fe5d7, 0xe15dfec0, 0xea53f7cd, 0xdb79c8ee, 0xd077c1e3, 0xcd65daf4, 0xc66bd3f9, 0xaf31a4b2, 0xa43fadbf, 0xb92db6a8, 0xb223bfa5, 0x83098086, 0x8807898b, 0x9515929c, 0x9e1b9b91, 0x47a17c0a, 0x4caf7507, 0x51bd6e10, 0x5ab3671d, 0x6b99583e, 0x60975133, 0x7d854a24, 0x768b4329, 0x1fd13462, 0x14df3d6f, 0x09cd2678, 0x02c32f75, 0x33e91056, 0x38e7195b, 0x25f5024c, 0x2efb0b41, 0x8c9ad761, 0x8794de6c, 0x9a86c57b, 0x9188cc76, 0xa0a2f355, 0xabacfa58, 0xb6bee14f, 0xbdb0e842, 0xd4ea9f09, 0xdfe49604, 0xc2f68d13, 0xc9f8841e, 0xf8d2bb3d, 0xf3dcb230, 0xeecea927, 0xe5c0a02a, 0x3c7a47b1, 0x37744ebc, 0x2a6655ab, 0x21685ca6, 0x10426385, 0x1b4c6a88, 0x065e719f, 0x0d507892, 0x640a0fd9, 0x6f0406d4, 0x72161dc3, 0x791814ce, 0x48322bed, 0x433c22e0, 0x5e2e39f7, 0x552030fa, 0x01ec9ab7, 0x0ae293ba, 0x17f088ad, 0x1cfe81a0, 0x2dd4be83, 0x26dab78e, 0x3bc8ac99, 0x30c6a594, 0x599cd2df, 0x5292dbd2, 0x4f80c0c5, 0x448ec9c8, 0x75a4f6eb, 0x7eaaffe6, 0x63b8e4f1, 0x68b6edfc, 0xb10c0a67, 0xba02036a, 0xa710187d, 0xac1e1170, 0x9d342e53, 0x963a275e, 0x8b283c49, 0x80263544, 0xe97c420f, 0xe2724b02, 0xff605015, 0xf46e5918, 0xc544663b, 0xce4a6f36, 0xd3587421, 0xd8567d2c, 0x7a37a10c, 0x7139a801, 0x6c2bb316, 0x6725ba1b, 0x560f8538, 0x5d018c35, 0x40139722, 0x4b1d9e2f, 0x2247e964, 0x2949e069, 0x345bfb7e, 0x3f55f273, 0x0e7fcd50, 0x0571c45d, 0x1863df4a, 0x136dd647, 0xcad731dc, 0xc1d938d1, 0xdccb23c6, 0xd7c52acb, 0xe6ef15e8, 0xede11ce5, 0xf0f307f2, 0xfbfd0eff, 0x92a779b4, 0x99a970b9, 0x84bb6bae, 0x8fb562a3, 0xbe9f5d80, 0xb591548d, 0xa8834f9a, 0xa38d4697];
var U3 = [0x00000000, 0x0d0b0e09, 0x1a161c12, 0x171d121b, 0x342c3824, 0x3927362d, 0x2e3a2436, 0x23312a3f, 0x68587048, 0x65537e41, 0x724e6c5a, 0x7f456253, 0x5c74486c, 0x517f4665, 0x4662547e, 0x4b695a77, 0xd0b0e090, 0xddbbee99, 0xcaa6fc82, 0xc7adf28b, 0xe49cd8b4, 0xe997d6bd, 0xfe8ac4a6, 0xf381caaf, 0xb8e890d8, 0xb5e39ed1, 0xa2fe8cca, 0xaff582c3, 0x8cc4a8fc, 0x81cfa6f5, 0x96d2b4ee, 0x9bd9bae7, 0xbb7bdb3b, 0xb670d532, 0xa16dc729, 0xac66c920, 0x8f57e31f, 0x825ced16, 0x9541ff0d, 0x984af104, 0xd323ab73, 0xde28a57a, 0xc935b761, 0xc43eb968, 0xe70f9357, 0xea049d5e, 0xfd198f45, 0xf012814c, 0x6bcb3bab, 0x66c035a2, 0x71dd27b9, 0x7cd629b0, 0x5fe7038f, 0x52ec0d86, 0x45f11f9d, 0x48fa1194, 0x03934be3, 0x0e9845ea, 0x198557f1, 0x148e59f8, 0x37bf73c7, 0x3ab47dce, 0x2da96fd5, 0x20a261dc, 0x6df6ad76, 0x60fda37f, 0x77e0b164, 0x7aebbf6d, 0x59da9552, 0x54d19b5b, 0x43cc8940, 0x4ec78749, 0x05aedd3e, 0x08a5d337, 0x1fb8c12c, 0x12b3cf25, 0x3182e51a, 0x3c89eb13, 0x2b94f908, 0x269ff701, 0xbd464de6, 0xb04d43ef, 0xa75051f4, 0xaa5b5ffd, 0x896a75c2, 0x84617bcb, 0x937c69d0, 0x9e7767d9, 0xd51e3dae, 0xd81533a7, 0xcf0821bc, 0xc2032fb5, 0xe132058a, 0xec390b83, 0xfb241998, 0xf62f1791, 0xd68d764d, 0xdb867844, 0xcc9b6a5f, 0xc1906456, 0xe2a14e69, 0xefaa4060, 0xf8b7527b, 0xf5bc5c72, 0xbed50605, 0xb3de080c, 0xa4c31a17, 0xa9c8141e, 0x8af93e21, 0x87f23028, 0x90ef2233, 0x9de42c3a, 0x063d96dd, 0x0b3698d4, 0x1c2b8acf, 0x112084c6, 0x3211aef9, 0x3f1aa0f0, 0x2807b2eb, 0x250cbce2, 0x6e65e695, 0x636ee89c, 0x7473fa87, 0x7978f48e, 0x5a49deb1, 0x5742d0b8, 0x405fc2a3, 0x4d54ccaa, 0xdaf741ec, 0xd7fc4fe5, 0xc0e15dfe, 0xcdea53f7, 0xeedb79c8, 0xe3d077c1, 0xf4cd65da, 0xf9c66bd3, 0xb2af31a4, 0xbfa43fad, 0xa8b92db6, 0xa5b223bf, 0x86830980, 0x8b880789, 0x9c951592, 0x919e1b9b, 0x0a47a17c, 0x074caf75, 0x1051bd6e, 0x1d5ab367, 0x3e6b9958, 0x33609751, 0x247d854a, 0x29768b43, 0x621fd134, 0x6f14df3d, 0x7809cd26, 0x7502c32f, 0x5633e910, 0x5b38e719, 0x4c25f502, 0x412efb0b, 0x618c9ad7, 0x6c8794de, 0x7b9a86c5, 0x769188cc, 0x55a0a2f3, 0x58abacfa, 0x4fb6bee1, 0x42bdb0e8, 0x09d4ea9f, 0x04dfe496, 0x13c2f68d, 0x1ec9f884, 0x3df8d2bb, 0x30f3dcb2, 0x27eecea9, 0x2ae5c0a0, 0xb13c7a47, 0xbc37744e, 0xab2a6655, 0xa621685c, 0x85104263, 0x881b4c6a, 0x9f065e71, 0x920d5078, 0xd9640a0f, 0xd46f0406, 0xc372161d, 0xce791814, 0xed48322b, 0xe0433c22, 0xf75e2e39, 0xfa552030, 0xb701ec9a, 0xba0ae293, 0xad17f088, 0xa01cfe81, 0x832dd4be, 0x8e26dab7, 0x993bc8ac, 0x9430c6a5, 0xdf599cd2, 0xd25292db, 0xc54f80c0, 0xc8448ec9, 0xeb75a4f6, 0xe67eaaff, 0xf163b8e4, 0xfc68b6ed, 0x67b10c0a, 0x6aba0203, 0x7da71018, 0x70ac1e11, 0x539d342e, 0x5e963a27, 0x498b283c, 0x44802635, 0x0fe97c42, 0x02e2724b, 0x15ff6050, 0x18f46e59, 0x3bc54466, 0x36ce4a6f, 0x21d35874, 0x2cd8567d, 0x0c7a37a1, 0x017139a8, 0x166c2bb3, 0x1b6725ba, 0x38560f85, 0x355d018c, 0x22401397, 0x2f4b1d9e, 0x642247e9, 0x692949e0, 0x7e345bfb, 0x733f55f2, 0x500e7fcd, 0x5d0571c4, 0x4a1863df, 0x47136dd6, 0xdccad731, 0xd1c1d938, 0xc6dccb23, 0xcbd7c52a, 0xe8e6ef15, 0xe5ede11c, 0xf2f0f307, 0xfffbfd0e, 0xb492a779, 0xb999a970, 0xae84bb6b, 0xa38fb562, 0x80be9f5d, 0x8db59154, 0x9aa8834f, 0x97a38d46];
var U4 = [0x00000000, 0x090d0b0e, 0x121a161c, 0x1b171d12, 0x24342c38, 0x2d392736, 0x362e3a24, 0x3f23312a, 0x48685870, 0x4165537e, 0x5a724e6c, 0x537f4562, 0x6c5c7448, 0x65517f46, 0x7e466254, 0x774b695a, 0x90d0b0e0, 0x99ddbbee, 0x82caa6fc, 0x8bc7adf2, 0xb4e49cd8, 0xbde997d6, 0xa6fe8ac4, 0xaff381ca, 0xd8b8e890, 0xd1b5e39e, 0xcaa2fe8c, 0xc3aff582, 0xfc8cc4a8, 0xf581cfa6, 0xee96d2b4, 0xe79bd9ba, 0x3bbb7bdb, 0x32b670d5, 0x29a16dc7, 0x20ac66c9, 0x1f8f57e3, 0x16825ced, 0x0d9541ff, 0x04984af1, 0x73d323ab, 0x7ade28a5, 0x61c935b7, 0x68c43eb9, 0x57e70f93, 0x5eea049d, 0x45fd198f, 0x4cf01281, 0xab6bcb3b, 0xa266c035, 0xb971dd27, 0xb07cd629, 0x8f5fe703, 0x8652ec0d, 0x9d45f11f, 0x9448fa11, 0xe303934b, 0xea0e9845, 0xf1198557, 0xf8148e59, 0xc737bf73, 0xce3ab47d, 0xd52da96f, 0xdc20a261, 0x766df6ad, 0x7f60fda3, 0x6477e0b1, 0x6d7aebbf, 0x5259da95, 0x5b54d19b, 0x4043cc89, 0x494ec787, 0x3e05aedd, 0x3708a5d3, 0x2c1fb8c1, 0x2512b3cf, 0x1a3182e5, 0x133c89eb, 0x082b94f9, 0x01269ff7, 0xe6bd464d, 0xefb04d43, 0xf4a75051, 0xfdaa5b5f, 0xc2896a75, 0xcb84617b, 0xd0937c69, 0xd99e7767, 0xaed51e3d, 0xa7d81533, 0xbccf0821, 0xb5c2032f, 0x8ae13205, 0x83ec390b, 0x98fb2419, 0x91f62f17, 0x4dd68d76, 0x44db8678, 0x5fcc9b6a, 0x56c19064, 0x69e2a14e, 0x60efaa40, 0x7bf8b752, 0x72f5bc5c, 0x05bed506, 0x0cb3de08, 0x17a4c31a, 0x1ea9c814, 0x218af93e, 0x2887f230, 0x3390ef22, 0x3a9de42c, 0xdd063d96, 0xd40b3698, 0xcf1c2b8a, 0xc6112084, 0xf93211ae, 0xf03f1aa0, 0xeb2807b2, 0xe2250cbc, 0x956e65e6, 0x9c636ee8, 0x877473fa, 0x8e7978f4, 0xb15a49de, 0xb85742d0, 0xa3405fc2, 0xaa4d54cc, 0xecdaf741, 0xe5d7fc4f, 0xfec0e15d, 0xf7cdea53, 0xc8eedb79, 0xc1e3d077, 0xdaf4cd65, 0xd3f9c66b, 0xa4b2af31, 0xadbfa43f, 0xb6a8b92d, 0xbfa5b223, 0x80868309, 0x898b8807, 0x929c9515, 0x9b919e1b, 0x7c0a47a1, 0x75074caf, 0x6e1051bd, 0x671d5ab3, 0x583e6b99, 0x51336097, 0x4a247d85, 0x4329768b, 0x34621fd1, 0x3d6f14df, 0x267809cd, 0x2f7502c3, 0x105633e9, 0x195b38e7, 0x024c25f5, 0x0b412efb, 0xd7618c9a, 0xde6c8794, 0xc57b9a86, 0xcc769188, 0xf355a0a2, 0xfa58abac, 0xe14fb6be, 0xe842bdb0, 0x9f09d4ea, 0x9604dfe4, 0x8d13c2f6, 0x841ec9f8, 0xbb3df8d2, 0xb230f3dc, 0xa927eece, 0xa02ae5c0, 0x47b13c7a, 0x4ebc3774, 0x55ab2a66, 0x5ca62168, 0x63851042, 0x6a881b4c, 0x719f065e, 0x78920d50, 0x0fd9640a, 0x06d46f04, 0x1dc37216, 0x14ce7918, 0x2bed4832, 0x22e0433c, 0x39f75e2e, 0x30fa5520, 0x9ab701ec, 0x93ba0ae2, 0x88ad17f0, 0x81a01cfe, 0xbe832dd4, 0xb78e26da, 0xac993bc8, 0xa59430c6, 0xd2df599c, 0xdbd25292, 0xc0c54f80, 0xc9c8448e, 0xf6eb75a4, 0xffe67eaa, 0xe4f163b8, 0xedfc68b6, 0x0a67b10c, 0x036aba02, 0x187da710, 0x1170ac1e, 0x2e539d34, 0x275e963a, 0x3c498b28, 0x35448026, 0x420fe97c, 0x4b02e272, 0x5015ff60, 0x5918f46e, 0x663bc544, 0x6f36ce4a, 0x7421d358, 0x7d2cd856, 0xa10c7a37, 0xa8017139, 0xb3166c2b, 0xba1b6725, 0x8538560f, 0x8c355d01, 0x97224013, 0x9e2f4b1d, 0xe9642247, 0xe0692949, 0xfb7e345b, 0xf2733f55, 0xcd500e7f, 0xc45d0571, 0xdf4a1863, 0xd647136d, 0x31dccad7, 0x38d1c1d9, 0x23c6dccb, 0x2acbd7c5, 0x15e8e6ef, 0x1ce5ede1, 0x07f2f0f3, 0x0efffbfd, 0x79b492a7, 0x70b999a9, 0x6bae84bb, 0x62a38fb5, 0x5d80be9f, 0x548db591, 0x4f9aa883, 0x4697a38d];
function convertToInt32(bytes) {
var result = [];
for (var i = 0; i < bytes.length; i += 4) {
result.push(bytes[i] << 24 | bytes[i + 1] << 16 | bytes[i + 2] << 8 | bytes[i + 3]);
}
return result;
}
var AesJs = function (key) {
if (!(this instanceof AesJs)) {
throw Error('AES must be instanitated with `new`');
}
Object.defineProperty(this, 'key', {
value: coerceArray(key, true)
});
this._prepare();
};
AesJs.prototype._prepare = function () {
var rounds = numberOfRounds[this.key.length];
if (rounds == null) {
throw new Error('invalid key size (must be 16, 24 or 32 bytes)');
}
// encryption round keys
this._Ke = [];
// decryption round keys
this._Kd = [];
for (var i = 0; i <= rounds; i++) {
this._Ke.push([0, 0, 0, 0]);
this._Kd.push([0, 0, 0, 0]);
}
var roundKeyCount = (rounds + 1) * 4;
var KC = this.key.length / 4;
// convert the key into ints
var tk = convertToInt32(this.key);
// copy values into round key arrays
var index;
for (var i = 0; i < KC; i++) {
index = i >> 2;
this._Ke[index][i % 4] = tk[i];
this._Kd[rounds - index][i % 4] = tk[i];
}
// key expansion (fips-197 section 5.2)
var rconpointer = 0;
var t = KC,
tt;
while (t < roundKeyCount) {
tt = tk[KC - 1];
tk[0] ^= S[tt >> 16 & 0xFF] << 24 ^ S[tt >> 8 & 0xFF] << 16 ^ S[tt & 0xFF] << 8 ^ S[tt >> 24 & 0xFF] ^ rcon[rconpointer] << 24;
rconpointer += 1;
// key expansion (for non-256 bit)
if (KC != 8) {
for (var i = 1; i < KC; i++) {
tk[i] ^= tk[i - 1];
}
// key expansion for 256-bit keys is "slightly different" (fips-197)
} else {
for (var i = 1; i < KC / 2; i++) {
tk[i] ^= tk[i - 1];
}
tt = tk[KC / 2 - 1];
tk[KC / 2] ^= S[tt & 0xFF] ^ S[tt >> 8 & 0xFF] << 8 ^ S[tt >> 16 & 0xFF] << 16 ^ S[tt >> 24 & 0xFF] << 24;
for (var i = KC / 2 + 1; i < KC; i++) {
tk[i] ^= tk[i - 1];
}
}
// copy values into round key arrays
var i = 0,
r,
c;
while (i < KC && t < roundKeyCount) {
r = t >> 2;
c = t % 4;
this._Ke[r][c] = tk[i];
this._Kd[rounds - r][c] = tk[i++];
t++;
}
}
// inverse-cipher-ify the decryption round key (fips-197 section 5.3)
for (var r = 1; r < rounds; r++) {
for (var c = 0; c < 4; c++) {
tt = this._Kd[r][c];
this._Kd[r][c] = U1[tt >> 24 & 0xFF] ^ U2[tt >> 16 & 0xFF] ^ U3[tt >> 8 & 0xFF] ^ U4[tt & 0xFF];
}
}
};
AesJs.prototype.encrypt = function (plaintext) {
if (plaintext.length != 16) {
throw new Error('invalid plaintext size (must be 16 bytes)');
}
var rounds = this._Ke.length - 1;
var a = [0, 0, 0, 0];
// convert plaintext to (ints ^ key)
var t = convertToInt32(plaintext);
for (var i = 0; i < 4; i++) {
t[i] ^= this._Ke[0][i];
}
// apply round transforms
for (var r = 1; r < rounds; r++) {
for (var i = 0; i < 4; i++) {
a[i] = T1[t[i] >> 24 & 0xff] ^ T2[t[(i + 1) % 4] >> 16 & 0xff] ^ T3[t[(i + 2) % 4] >> 8 & 0xff] ^ T4[t[(i + 3) % 4] & 0xff] ^ this._Ke[r][i];
}
t = a.slice();
}
// the last round is special
var result = createArray(16),
tt;
for (var i = 0; i < 4; i++) {
tt = this._Ke[rounds][i];
result[4 * i] = (S[t[i] >> 24 & 0xff] ^ tt >> 24) & 0xff;
result[4 * i + 1] = (S[t[(i + 1) % 4] >> 16 & 0xff] ^ tt >> 16) & 0xff;
result[4 * i + 2] = (S[t[(i + 2) % 4] >> 8 & 0xff] ^ tt >> 8) & 0xff;
result[4 * i + 3] = (S[t[(i + 3) % 4] & 0xff] ^ tt) & 0xff;
}
return result;
};
AesJs.prototype.decrypt = function (ciphertext) {
if (ciphertext.length != 16) {
throw new Error('invalid ciphertext size (must be 16 bytes)');
}
var rounds = this._Kd.length - 1;
var a = [0, 0, 0, 0];
// convert plaintext to (ints ^ key)
var t = convertToInt32(ciphertext);
for (var i = 0; i < 4; i++) {
t[i] ^= this._Kd[0][i];
}
// apply round transforms
for (var r = 1; r < rounds; r++) {
for (var i = 0; i < 4; i++) {
a[i] = T5[t[i] >> 24 & 0xff] ^ T6[t[(i + 3) % 4] >> 16 & 0xff] ^ T7[t[(i + 2) % 4] >> 8 & 0xff] ^ T8[t[(i + 1) % 4] & 0xff] ^ this._Kd[r][i];
}
t = a.slice();
}
// the last round is special
var result = createArray(16),
tt;
for (var i = 0; i < 4; i++) {
tt = this._Kd[rounds][i];
result[4 * i] = (Si[t[i] >> 24 & 0xff] ^ tt >> 24) & 0xff;
result[4 * i + 1] = (Si[t[(i + 3) % 4] >> 16 & 0xff] ^ tt >> 16) & 0xff;
result[4 * i + 2] = (Si[t[(i + 2) % 4] >> 8 & 0xff] ^ tt >> 8) & 0xff;
result[4 * i + 3] = (Si[t[(i + 1) % 4] & 0xff] ^ tt) & 0xff;
}
return result;
};
/**
* Mode Of Operation - Electonic Codebook (ECB)
*/
var ModeOfOperationECB = function (key) {
if (!(this instanceof ModeOfOperationECB)) {
throw Error('AES must be instanitated with `new`');
}
this.description = "Electronic Code Block";
this.name = "ecb";
this._aes = new AesJs(key);
};
ModeOfOperationECB.prototype.encrypt = function (plaintext) {
plaintext = coerceArray(plaintext);
if (plaintext.length % 16 !== 0) {
throw new Error('invalid plaintext size (must be multiple of 16 bytes)');
}
var ciphertext = createArray(plaintext.length);
var block = createArray(16);
for (var i = 0; i < plaintext.length; i += 16) {
copyArray(plaintext, block, 0, i, i + 16);
block = this._aes.encrypt(block);
copyArray(block, ciphertext, i);
}
return ciphertext;
};
ModeOfOperationECB.prototype.decrypt = function (ciphertext) {
ciphertext = coerceArray(ciphertext);
if (ciphertext.length % 16 !== 0) {
throw new Error('invalid ciphertext size (must be multiple of 16 bytes)');
}
var plaintext = createArray(ciphertext.length);
var block = createArray(16);
for (var i = 0; i < ciphertext.length; i += 16) {
copyArray(ciphertext, block, 0, i, i + 16);
block = this._aes.decrypt(block);
copyArray(block, plaintext, i);
}
return plaintext;
};
/**
* Mode Of Operation - Cipher Block Chaining (CBC)
*/
var ModeOfOperationCBC = function (key, iv) {
if (!(this instanceof ModeOfOperationCBC)) {
throw Error('AES must be instanitated with `new`');
}
this.description = "Cipher Block Chaining";
this.name = "cbc";
if (!iv) {
iv = createArray(16);
} else if (iv.length != 16) {
throw new Error('invalid initialation vector size (must be 16 bytes)');
}
this._lastCipherblock = coerceArray(iv, true);
this._aes = new AesJs(key);
};
ModeOfOperationCBC.prototype.encrypt = function (plaintext) {
plaintext = coerceArray(plaintext);
if (plaintext.length % 16 !== 0) {
throw new Error('invalid plaintext size (must be multiple of 16 bytes)');
}
var ciphertext = createArray(plaintext.length);
var block = createArray(16);
for (var i = 0; i < plaintext.length; i += 16) {
copyArray(plaintext, block, 0, i, i + 16);
for (var j = 0; j < 16; j++) {
block[j] ^= this._lastCipherblock[j];
}
this._lastCipherblock = this._aes.encrypt(block);
copyArray(this._lastCipherblock, ciphertext, i);
}
return ciphertext;
};
ModeOfOperationCBC.prototype.decrypt = function (ciphertext) {
ciphertext = coerceArray(ciphertext);
if (ciphertext.length % 16 !== 0) {
throw new Error('invalid ciphertext size (must be multiple of 16 bytes)');
}
var plaintext = createArray(ciphertext.length);
var block = createArray(16);
for (var i = 0; i < ciphertext.length; i += 16) {
copyArray(ciphertext, block, 0, i, i + 16);
block = this._aes.decrypt(block);
for (var j = 0; j < 16; j++) {
plaintext[i + j] = block[j] ^ this._lastCipherblock[j];
}
copyArray(ciphertext, this._lastCipherblock, 0, i, i + 16);
}
return plaintext;
};
/**
* Mode Of Operation - Cipher Feedback (CFB)
*/
var ModeOfOperationCFB = function (key, iv, segmentSize) {
if (!(this instanceof ModeOfOperationCFB)) {
throw Error('AES must be instanitated with `new`');
}
this.description = "Cipher Feedback";
this.name = "cfb";
if (!iv) {
iv = createArray(16);
} else if (iv.length != 16) {
throw new Error('invalid initialation vector size (must be 16 size)');
}
if (!segmentSize) {
segmentSize = 1;
}
this.segmentSize = segmentSize;
this._shiftRegister = coerceArray(iv, true);
this._aes = new AesJs(key);
};
ModeOfOperationCFB.prototype.encrypt = function (plaintext) {
if (plaintext.length % this.segmentSize != 0) {
throw new Error('invalid plaintext size (must be segmentSize bytes)');
}
var encrypted = coerceArray(plaintext, true);
var xorSegment;
for (var i = 0; i < encrypted.length; i += this.segmentSize) {
xorSegment = this._aes.encrypt(this._shiftRegister);
for (var j = 0; j < this.segmentSize; j++) {
encrypted[i + j] ^= xorSegment[j];
}
// Shift the register
copyArray(this._shiftRegister, this._shiftRegister, 0, this.segmentSize);
copyArray(encrypted, this._shiftRegister, 16 - this.segmentSize, i, i + this.segmentSize);
}
return encrypted;
};
ModeOfOperationCFB.prototype.decrypt = function (ciphertext) {
if (ciphertext.length % this.segmentSize != 0) {
throw new Error('invalid ciphertext size (must be segmentSize bytes)');
}
var plaintext = coerceArray(ciphertext, true);
var xorSegment;
for (var i = 0; i < plaintext.length; i += this.segmentSize) {
xorSegment = this._aes.encrypt(this._shiftRegister);
for (var j = 0; j < this.segmentSize; j++) {
plaintext[i + j] ^= xorSegment[j];
}
// Shift the register
copyArray(this._shiftRegister, this._shiftRegister, 0, this.segmentSize);
copyArray(ciphertext, this._shiftRegister, 16 - this.segmentSize, i, i + this.segmentSize);
}
return plaintext;
};
/**
* Mode Of Operation - Output Feedback (OFB)
*/
var ModeOfOperationOFB = function (key, iv) {
if (!(this instanceof ModeOfOperationOFB)) {
throw Error('AES must be instanitated with `new`');
}
this.description = "Output Feedback";
this.name = "ofb";
if (!iv) {
iv = createArray(16);
} else if (iv.length != 16) {
throw new Error('invalid initialation vector size (must be 16 bytes)');
}
this._lastPrecipher = coerceArray(iv, true);
this._lastPrecipherIndex = 16;
this._aes = new AesJs(key);
};
ModeOfOperationOFB.prototype.encrypt = function (plaintext) {
var encrypted = coerceArray(plaintext, true);
for (var i = 0; i < encrypted.length; i++) {
if (this._lastPrecipherIndex === 16) {
this._lastPrecipher = this._aes.encrypt(this._lastPrecipher);
this._lastPrecipherIndex = 0;
}
encrypted[i] ^= this._lastPrecipher[this._lastPrecipherIndex++];
}
return encrypted;
};
// Decryption is symetric
ModeOfOperationOFB.prototype.decrypt = ModeOfOperationOFB.prototype.encrypt;
/**
* Counter object for CTR common mode of operation
*/
var Counter = function (initialValue) {
if (!(this instanceof Counter)) {
throw Error('Counter must be instanitated with `new`');
}
// We allow 0, but anything false-ish uses the default 1
if (initialValue !== 0 && !initialValue) {
initialValue = 1;
}
if (typeof initialValue === 'number') {
this._counter = createArray(16);
this.setValue(initialValue);
} else {
this.setBytes(initialValue);
}
};
Counter.prototype.setValue = function (value) {
if (typeof value !== 'number' || parseInt(value) != value) {
throw new Error('invalid counter value (must be an integer)');
}
// We cannot safely handle numbers beyond the safe range for integers
if (value > Number.MAX_SAFE_INTEGER) {
throw new Error('integer value out of safe range');
}
for (var index = 15; index >= 0; --index) {
this._counter[index] = value % 256;
value = parseInt(value / 256);
}
};
Counter.prototype.setBytes = function (bytes) {
bytes = coerceArray(bytes, true);
if (bytes.length != 16) {
throw new Error('invalid counter bytes size (must be 16 bytes)');
}
this._counter = bytes;
};
Counter.prototype.increment = function () {
for (var i = 15; i >= 0; i--) {
if (this._counter[i] === 255) {
this._counter[i] = 0;
} else {
this._counter[i]++;
break;
}
}
};
/**
* Mode Of Operation - Counter (CTR)
*/
var ModeOfOperationCTR = function (key, counter) {
if (!(this instanceof ModeOfOperationCTR)) {
throw Error('AES must be instanitated with `new`');
}
this.description = "Counter";
this.name = "ctr";
if (!(counter instanceof Counter)) {
counter = new Counter(counter);
}
this._counter = counter;
this._remainingCounter = null;
this._remainingCounterIndex = 16;
this._aes = new AesJs(key);
};
ModeOfOperationCTR.prototype.encrypt = function (plaintext) {
var encrypted = coerceArray(plaintext, true);
for (var i = 0; i < encrypted.length; i++) {
if (this._remainingCounterIndex === 16) {
this._remainingCounter = this._aes.encrypt(this._counter._counter);
this._remainingCounterIndex = 0;
this._counter.increment();
}
encrypted[i] ^= this._remainingCounter[this._remainingCounterIndex++];
}
return encrypted;
};
// Decryption is symetric
ModeOfOperationCTR.prototype.decrypt = ModeOfOperationCTR.prototype.encrypt;
///////////////////////
// Padding
// See:https://tools.ietf.org/html/rfc2315
function pkcs7pad(data) {
data = coerceArray(data, true);
var padder = 16 - data.length % 16;
var result = createArray(data.length + padder);
copyArray(data, result);
for (var i = data.length; i < result.length; i++) {
result[i] = padder;
}
return result;
}
function pkcs7strip(data) {
data = coerceArray(data, true);
if (data.length < 16) {
throw new Error('PKCS#7 invalid length');
}
var padder = data[data.length - 1];
if (padder > 16) {
throw new Error('PKCS#7 padding byte out of range');
}
var length = data.length - padder;
for (var i = 0; i < padder; i++) {
if (data[length + i] !== padder) {
throw new Error('PKCS#7 invalid padding byte');
}
}
var result = createArray(length);
copyArray(data, result, 0, 0, length);
return result;
}
///////////////////////
// Exporting
// The block cipher
const aesjs = {
AES: AesJs,
Counter: Counter,
ModeOfOperation: {
ecb: ModeOfOperationECB,
cbc: ModeOfOperationCBC,
cfb: ModeOfOperationCFB,
ofb: ModeOfOperationOFB,
ctr: ModeOfOperationCTR
},
utils: {
hex: convertHex,
utf8: convertUtf8
},
padding: {
pkcs7: {
pad: pkcs7pad,
strip: pkcs7strip
}
},
_arrayTest: {
coerceArray: coerceArray,
createArray: createArray,
copyArray: copyArray
}
};
function getNaluLength$2(data) {
let length = data[3] | data[2] << 8 | data[1] << 16 | data[0] << 24;
return length;
}
// aes-256-ctr 解密
function aes256ctrDecrypt(arrayBuffer, key, iv) {
let isHevc = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
key = new Uint8Array(key);
iv = new Uint8Array(iv);
const totalLength = arrayBuffer.byteLength;
// 17(23)/27(39)[是否i帧] ,0/1(), 0,0,0, 0,0,0,0,[NALU],0,0,0,0,[NALU].... 只需要解密NALU部分数据。
// 其中NALU部分需要跳过两个(nalu头 + 再加一个字节数据)
let startIndex = 5;
while (startIndex < totalLength) {
let tempNaluLength = getNaluLength$2(arrayBuffer.slice(startIndex, startIndex + 4));
if (tempNaluLength > totalLength) {
break;
}
let naluType = arrayBuffer[startIndex + 4];
// 这边只是判断了h264的加密规则,265的目前没有做处理
let needDecrypt = false;
if (isHevc) {
naluType = naluType >>> 1 & 0x3f;
needDecrypt = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21].includes(naluType);
} else {
naluType = naluType & 0x1f;
needDecrypt = naluType === 1 || naluType === 5;
}
//
if (needDecrypt) {
const tempNalu = arrayBuffer.slice(startIndex + 4 + 2, startIndex + 4 + tempNaluLength);
let aesCtr = new aesjs.ModeOfOperation.ctr(key, new aesjs.Counter(iv));
const decryptMsg = aesCtr.decrypt(tempNalu);
// release memory
aesCtr = null;
arrayBuffer.set(decryptMsg, startIndex + 4 + 2);
}
startIndex = startIndex + 4 + tempNaluLength;
}
return arrayBuffer;
}
function aes256ctrDecryptAacAudio(arrayBuffer, key, iv) {
//
const totalLength = arrayBuffer.byteLength;
if (totalLength <= 30) {
return arrayBuffer;
}
//
const step = 30 + 2;
const tempNalu = arrayBuffer.slice(step);
let aesCtr = new aesjs.ModeOfOperation.ctr(key, new aesjs.Counter(iv));
const decryptMsg = aesCtr.decrypt(tempNalu);
// release memory
aesCtr = null;
arrayBuffer.set(decryptMsg, step);
return arrayBuffer;
}
function getM7SCryptoStreamKey(path) {
return new Promise((resolve, reject) => {
ajax.get(path).then(res => {
resolve(res);
}).catch(e => {
reject(e);
});
});
}
/* eslint-disable no-bitwise, no-mixed-operators, complexity */
const DECRYPT = 0;
const ROUND = 32;
const BLOCK = 16;
const Sbox = [0xd6, 0x90, 0xe9, 0xfe, 0xcc, 0xe1, 0x3d, 0xb7, 0x16, 0xb6, 0x14, 0xc2, 0x28, 0xfb, 0x2c, 0x05, 0x2b, 0x67, 0x9a, 0x76, 0x2a, 0xbe, 0x04, 0xc3, 0xaa, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99, 0x9c, 0x42, 0x50, 0xf4, 0x91, 0xef, 0x98, 0x7a, 0x33, 0x54, 0x0b, 0x43, 0xed, 0xcf, 0xac, 0x62, 0xe4, 0xb3, 0x1c, 0xa9, 0xc9, 0x08, 0xe8, 0x95, 0x80, 0xdf, 0x94, 0xfa, 0x75, 0x8f, 0x3f, 0xa6, 0x47, 0x07, 0xa7, 0xfc, 0xf3, 0x73, 0x17, 0xba, 0x83, 0x59, 0x3c, 0x19, 0xe6, 0x85, 0x4f, 0xa8, 0x68, 0x6b, 0x81, 0xb2, 0x71, 0x64, 0xda, 0x8b, 0xf8, 0xeb, 0x0f, 0x4b, 0x70, 0x56, 0x9d, 0x35, 0x1e, 0x24, 0x0e, 0x5e, 0x63, 0x58, 0xd1, 0xa2, 0x25, 0x22, 0x7c, 0x3b, 0x01, 0x21, 0x78, 0x87, 0xd4, 0x00, 0x46, 0x57, 0x9f, 0xd3, 0x27, 0x52, 0x4c, 0x36, 0x02, 0xe7, 0xa0, 0xc4, 0xc8, 0x9e, 0xea, 0xbf, 0x8a, 0xd2, 0x40, 0xc7, 0x38, 0xb5, 0xa3, 0xf7, 0xf2, 0xce, 0xf9, 0x61, 0x15, 0xa1, 0xe0, 0xae, 0x5d, 0xa4, 0x9b, 0x34, 0x1a, 0x55, 0xad, 0x93, 0x32, 0x30, 0xf5, 0x8c, 0xb1, 0xe3, 0x1d, 0xf6, 0xe2, 0x2e, 0x82, 0x66, 0xca, 0x60, 0xc0, 0x29, 0x23, 0xab, 0x0d, 0x53, 0x4e, 0x6f, 0xd5, 0xdb, 0x37, 0x45, 0xde, 0xfd, 0x8e, 0x2f, 0x03, 0xff, 0x6a, 0x72, 0x6d, 0x6c, 0x5b, 0x51, 0x8d, 0x1b, 0xaf, 0x92, 0xbb, 0xdd, 0xbc, 0x7f, 0x11, 0xd9, 0x5c, 0x41, 0x1f, 0x10, 0x5a, 0xd8, 0x0a, 0xc1, 0x31, 0x88, 0xa5, 0xcd, 0x7b, 0xbd, 0x2d, 0x74, 0xd0, 0x12, 0xb8, 0xe5, 0xb4, 0xb0, 0x89, 0x69, 0x97, 0x4a, 0x0c, 0x96, 0x77, 0x7e, 0x65, 0xb9, 0xf1, 0x09, 0xc5, 0x6e, 0xc6, 0x84, 0x18, 0xf0, 0x7d, 0xec, 0x3a, 0xdc, 0x4d, 0x20, 0x79, 0xee, 0x5f, 0x3e, 0xd7, 0xcb, 0x39, 0x48];
const CK = [0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269, 0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9, 0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249, 0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9, 0xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229, 0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299, 0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209, 0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279];
/**
* 16 进制串转字节数组
*/
function hexToArray(str) {
const arr = [];
for (let i = 0, len = str.length; i < len; i += 2) {
arr.push(parseInt(str.substr(i, 2), 16));
}
return arr;
}
/**
* 字节数组转 16 进制串
*/
function ArrayToHex(arr) {
return arr.map(item => {
item = item.toString(16);
return item.length === 1 ? '0' + item : item;
}).join('');
}
/**
* utf8 串转字节数组
*/
function utf8ToArray(str) {
const arr = [];
for (let i = 0, len = str.length; i < len; i++) {
const point = str.codePointAt(i);
if (point <= 0x007f) {
// 单字节,标量值:00000000 00000000 0zzzzzzz
arr.push(point);
} else if (point <= 0x07ff) {
// 双字节,标量值:00000000 00000yyy yyzzzzzz
arr.push(0xc0 | point >>> 6); // 110yyyyy(0xc0-0xdf)
arr.push(0x80 | point & 0x3f); // 10zzzzzz(0x80-0xbf)
} else if (point <= 0xD7FF || point >= 0xE000 && point <= 0xFFFF) {
// 三字节:标量值:00000000 xxxxyyyy yyzzzzzz
arr.push(0xe0 | point >>> 12); // 1110xxxx(0xe0-0xef)
arr.push(0x80 | point >>> 6 & 0x3f); // 10yyyyyy(0x80-0xbf)
arr.push(0x80 | point & 0x3f); // 10zzzzzz(0x80-0xbf)
} else if (point >= 0x010000 && point <= 0x10FFFF) {
// 四字节:标量值:000wwwxx xxxxyyyy yyzzzzzz
i++;
arr.push(0xf0 | point >>> 18 & 0x1c); // 11110www(0xf0-0xf7)
arr.push(0x80 | point >>> 12 & 0x3f); // 10xxxxxx(0x80-0xbf)
arr.push(0x80 | point >>> 6 & 0x3f); // 10yyyyyy(0x80-0xbf)
arr.push(0x80 | point & 0x3f); // 10zzzzzz(0x80-0xbf)
} else {
// 五、六字节,暂时不支持
arr.push(point);
throw new Error('input is not supported');
}
}
return arr;
}
/**
* 字节数组转 utf8 串
*/
function arrayToUtf8(arr) {
const str = [];
for (let i = 0, len = arr.length; i < len; i++) {
if (arr[i] >= 0xf0 && arr[i] <= 0xf7) {
// 四字节
str.push(String.fromCodePoint(((arr[i] & 0x07) << 18) + ((arr[i + 1] & 0x3f) << 12) + ((arr[i + 2] & 0x3f) << 6) + (arr[i + 3] & 0x3f)));
i += 3;
} else if (arr[i] >= 0xe0 && arr[i] <= 0xef) {
// 三字节
str.push(String.fromCodePoint(((arr[i] & 0x0f) << 12) + ((arr[i + 1] & 0x3f) << 6) + (arr[i + 2] & 0x3f)));
i += 2;
} else if (arr[i] >= 0xc0 && arr[i] <= 0xdf) {
// 双字节
str.push(String.fromCodePoint(((arr[i] & 0x1f) << 6) + (arr[i + 1] & 0x3f)));
i++;
} else {
// 单字节
str.push(String.fromCodePoint(arr[i]));
}
}
return str.join('');
}
/**
* 32 比特循环左移
*/
function rotl(x, n) {
const s = n & 31;
return x << s | x >>> 32 - s;
}
/**
* 非线性变换
*/
function byteSub(a) {
return (Sbox[a >>> 24 & 0xFF] & 0xFF) << 24 | (Sbox[a >>> 16 & 0xFF] & 0xFF) << 16 | (Sbox[a >>> 8 & 0xFF] & 0xFF) << 8 | Sbox[a & 0xFF] & 0xFF;
}
/**
* 线性变换,加密/解密用
*/
function l1(b) {
return b ^ rotl(b, 2) ^ rotl(b, 10) ^ rotl(b, 18) ^ rotl(b, 24);
}
/**
* 线性变换,生成轮密钥用
*/
function l2(b) {
return b ^ rotl(b, 13) ^ rotl(b, 23);
}
/**
* 以一组 128 比特进行加密/解密操作
*/
function sms4Crypt(input, output, roundKey) {
const x = new Array(4);
// 字节数组转成字数组(此处 1 字 = 32 比特)
const tmp = new Array(4);
for (let i = 0; i < 4; i++) {
tmp[0] = input[4 * i] & 0xff;
tmp[1] = input[4 * i + 1] & 0xff;
tmp[2] = input[4 * i + 2] & 0xff;
tmp[3] = input[4 * i + 3] & 0xff;
x[i] = tmp[0] << 24 | tmp[1] << 16 | tmp[2] << 8 | tmp[3];
}
// x[i + 4] = x[i] ^ l1(byteSub(x[i + 1] ^ x[i + 2] ^ x[i + 3] ^ roundKey[i]))
for (let r = 0, mid; r < 32; r += 4) {
mid = x[1] ^ x[2] ^ x[3] ^ roundKey[r + 0];
x[0] ^= l1(byteSub(mid)); // x[4]
mid = x[2] ^ x[3] ^ x[0] ^ roundKey[r + 1];
x[1] ^= l1(byteSub(mid)); // x[5]
mid = x[3] ^ x[0] ^ x[1] ^ roundKey[r + 2];
x[2] ^= l1(byteSub(mid)); // x[6]
mid = x[0] ^ x[1] ^ x[2] ^ roundKey[r + 3];
x[3] ^= l1(byteSub(mid)); // x[7]
}
// 反序变换
for (let j = 0; j < 16; j += 4) {
output[j] = x[3 - j / 4] >>> 24 & 0xff;
output[j + 1] = x[3 - j / 4] >>> 16 & 0xff;
output[j + 2] = x[3 - j / 4] >>> 8 & 0xff;
output[j + 3] = x[3 - j / 4] & 0xff;
}
}
/**
* 密钥扩展算法
*/
function sms4KeyExt(key, roundKey, cryptFlag) {
const x = new Array(4);
// 字节数组转成字数组(此处 1 字 = 32 比特)
const tmp = new Array(4);
for (let i = 0; i < 4; i++) {
tmp[0] = key[0 + 4 * i] & 0xff;
tmp[1] = key[1 + 4 * i] & 0xff;
tmp[2] = key[2 + 4 * i] & 0xff;
tmp[3] = key[3 + 4 * i] & 0xff;
x[i] = tmp[0] << 24 | tmp[1] << 16 | tmp[2] << 8 | tmp[3];
}
// 与系统参数做异或
x[0] ^= 0xa3b1bac6;
x[1] ^= 0x56aa3350;
x[2] ^= 0x677d9197;
x[3] ^= 0xb27022dc;
// roundKey[i] = x[i + 4] = x[i] ^ l2(byteSub(x[i + 1] ^ x[i + 2] ^ x[i + 3] ^ CK[i]))
for (let r = 0, mid; r < 32; r += 4) {
mid = x[1] ^ x[2] ^ x[3] ^ CK[r + 0];
roundKey[r + 0] = x[0] ^= l2(byteSub(mid)); // x[4]
mid = x[2] ^ x[3] ^ x[0] ^ CK[r + 1];
roundKey[r + 1] = x[1] ^= l2(byteSub(mid)); // x[5]
mid = x[3] ^ x[0] ^ x[1] ^ CK[r + 2];
roundKey[r + 2] = x[2] ^= l2(byteSub(mid)); // x[6]
mid = x[0] ^ x[1] ^ x[2] ^ CK[r + 3];
roundKey[r + 3] = x[3] ^= l2(byteSub(mid)); // x[7]
}
// 解密时使用反序的轮密钥
if (cryptFlag === DECRYPT) {
for (let r = 0, mid; r < 16; r++) {
mid = roundKey[r];
roundKey[r] = roundKey[31 - r];
roundKey[31 - r] = mid;
}
}
}
function sm4(inArray, key, cryptFlag) {
let {
padding = 'pkcs#7',
mode,
iv = [],
output = 'string'
} = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
// 默认走 ECB 模式
if (mode === 'cbc') {
// CBC 模式,
if (typeof iv === 'string') iv = hexToArray(iv);
if (iv.length !== 128 / 8) {
// iv 不是 128 比特
throw new Error('iv is invalid');
}
}
// 检查 key
if (typeof key === 'string') key = hexToArray(key);
if (key.length !== 128 / 8) {
// key 不是 128 比特
throw new Error('key is invalid');
}
// 检查输入
if (typeof inArray === 'string') {
if (cryptFlag !== DECRYPT) {
// 加密,输入为 utf8 串
inArray = utf8ToArray(inArray);
} else {
// 解密,输入为 16 进制串
inArray = hexToArray(inArray);
}
} else {
inArray = [...inArray];
}
// 新增填充,sm4 是 16 个字节一个分组,所以统一走到 pkcs#7
if ((padding === 'pkcs#5' || padding === 'pkcs#7') && cryptFlag !== DECRYPT) {
const paddingCount = BLOCK - inArray.length % BLOCK;
for (let i = 0; i < paddingCount; i++) inArray.push(paddingCount);
}
// 生成轮密钥
const roundKey = new Array(ROUND);
sms4KeyExt(key, roundKey, cryptFlag);
const outArray = [];
let lastVector = iv;
let restLen = inArray.length;
let point = 0;
while (restLen >= BLOCK) {
const input = inArray.slice(point, point + 16);
const output = new Array(16);
if (mode === 'cbc') {
for (let i = 0; i < BLOCK; i++) {
if (cryptFlag !== DECRYPT) {
// 加密过程在组加密前进行异或
input[i] ^= lastVector[i];
}
}
}
sms4Crypt(input, output, roundKey);
for (let i = 0; i < BLOCK; i++) {
if (mode === 'cbc') {
if (cryptFlag === DECRYPT) {
// 解密过程在组解密后进行异或
output[i] ^= lastVector[i];
}
}
outArray[point + i] = output[i];
}
if (mode === 'cbc') {
if (cryptFlag !== DECRYPT) {
// 使用上一次输出作为加密向量
lastVector = output;
} else {
// 使用上一次输入作为解密向量
lastVector = input;
}
}
restLen -= BLOCK;
point += BLOCK;
}
// 去除填充,sm4 是 16 个字节一个分组,所以统一走到 pkcs#7
if ((padding === 'pkcs#5' || padding === 'pkcs#7') && cryptFlag === DECRYPT) {
const len = outArray.length;
const paddingCount = outArray[len - 1];
for (let i = 1; i <= paddingCount; i++) {
if (outArray[len - i] !== paddingCount) throw new Error('padding is invalid');
}
outArray.splice(len - paddingCount, paddingCount);
}
// 调整输出
if (output !== 'array') {
if (cryptFlag !== DECRYPT) {
// 加密,输出转 16 进制串
return ArrayToHex(outArray);
} else {
// 解密,输出转 utf8 串
return arrayToUtf8(outArray);
}
} else {
return outArray;
}
}
// module.exports = {
// encrypt(inArray, key, options) {
// return sm4(inArray, key, 1, options)
// },
// // sm4.decrypt(frame, key, { mode: 'cbc', iv: key, output: 'array' })
// decrypt(inArray, key, options) {
// return sm4(inArray, key, 0, options)
// }
// }
function getNaluLength$1(data) {
let length = data[3] | data[2] << 8 | data[1] << 16 | data[0] << 24;
return length;
}
function sm4Decrypt(arrayBuffer, key) {
let isHevc = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
const totalLength = arrayBuffer.byteLength;
// 17(23)/27(39)[是否i帧] ,0/1(), 0,0,0, 0,0,0,0,[NALU],0,0,0,0,[NALU].... 只需要解密NALU部分数据。
// 其中NALU部分需要跳过两个(nalu头 + 再加一个字节数据)
let startIndex = 5;
while (startIndex < totalLength) {
let tempNaluLength = getNaluLength$1(arrayBuffer.slice(startIndex, startIndex + 4));
if (tempNaluLength > totalLength) {
break;
}
//
let naluType = arrayBuffer[startIndex + 4];
let needDecrypt = false;
if (isHevc) {
naluType = naluType >>> 1 & 0x3f;
needDecrypt = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21].includes(naluType);
} else {
naluType = naluType & 0x1f;
needDecrypt = naluType === 1 || naluType === 5;
}
//
if (needDecrypt) {
const tempNalu = arrayBuffer.slice(startIndex + 4 + 2, startIndex + 4 + tempNaluLength);
const decryptMsg = sm4(tempNalu, key, 0, {
padding: 'none',
output: 'array'
});
arrayBuffer.set(decryptMsg, startIndex + 4 + 2);
}
startIndex = startIndex + 4 + tempNaluLength;
}
return arrayBuffer;
}
function decrypt(arrayBuffer, key, iv) {
// start with 2 index,
for (let i = 2; i < arrayBuffer.length; ++i) {
const ii = i - 2;
const a = key[ii % key.length];
const b = iv[ii % iv.length];
arrayBuffer[i] = arrayBuffer[i] ^ (a ^ b);
}
return arrayBuffer;
}
function getNaluLength(data) {
let length = data[3] | data[2] << 8 | data[1] << 16 | data[0] << 24;
return length;
}
function xorDecrypt(arrayBuffer, key, iv) {
let isHevc = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
const totalLength = arrayBuffer.byteLength;
// 17(23)/27(39)[是否i帧] ,0/1(), 0,0,0, 0,0,0,0,[NALU],0,0,0,0,[NALU].... 只需要解密NALU部分数据。
// 其中NALU部分需要跳过两个(nalu头 + 再加一个字节数据)
let startIndex = 5;
while (startIndex < totalLength) {
let tempNaluLength = getNaluLength(arrayBuffer.slice(startIndex, startIndex + 4));
if (tempNaluLength > totalLength) {
break;
}
//
let naluType = arrayBuffer[startIndex + 4];
let needDecrypt = false;
if (isHevc) {
naluType = naluType >>> 1 & 0x3f;
needDecrypt = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21].includes(naluType);
} else {
naluType = naluType & 0x1f;
needDecrypt = naluType === 1 || naluType === 5;
}
//
if (needDecrypt) {
const tempNalu = arrayBuffer.slice(startIndex + 4, startIndex + 4 + tempNaluLength);
// 跳过2个字段
const decryptMsg = decrypt(tempNalu, key, iv);
arrayBuffer.set(decryptMsg, startIndex + 4);
}
startIndex = startIndex + 4 + tempNaluLength;
}
return arrayBuffer;
}
class CommonLoader extends Emitter {
constructor(player) {
super();
this.TAG_NAME = 'CommonDemux';
this.player = player;
this.stopId = null;
this.firstTimestamp = null;
this.startTimestamp = null;
this.preDelayTimestamp = null;
this.preLoopTimestamp = null;
this.bufferStartDts = null;
this.bufferStartLocalTs = null;
this.preIframeTs = null;
this.preFrameTs = null;
this.preTimestamp = null;
this.preTimestampDuration = 0;
this.prevPayloadBufferSize = 0;
this.isStreamTsMoreThanLocal = false;
this.delay = -1;
this.pushLatestDelay = -1;
this.bufferList = [];
this.historyIntervalDiffTimeList = [];
this.playbackStreamFps = null;
this.playbackStreamAudioFps = null;
this.playbackStreamVideoFps = null;
this.dropping = false;
this.isPushDropping = false;
this.nalUnitSize = null;
this.initInterval();
this.player.debug.log('CommonDemux', 'init');
}
destroy() {
this.bufferList = [];
this.historyIntervalDiffTimeList = [];
this.playbackStreamFps = null;
this.playbackStreamAudioFps = null;
this.playbackStreamVideoFps = null;
this.clearStopInterval();
this.firstTimestamp = null;
this.startTimestamp = null;
this.bufferStartDts = null;
this.bufferStartLocalTs = null;
this.preDelayTimestamp = null;
this.preLoopTimestamp = null;
this.preIframeTs = null;
this.preTimestamp = null;
this.preTimestampDuration = 0;
this.prevPayloadBufferSize = 0;
this.isStreamTsMoreThanLocal = false;
this.delay = -1;
this.pushLatestDelay = -1;
this.dropping = false;
this.isPushDropping = false;
this.nalUnitSize = null;
this.off();
this.player.debug.log('CommonDemux', 'destroy');
}
isDropping() {
return this.dropping || this.isPushDropping;
}
getDelay(timestamp, type) {
if (!timestamp || !this.player.isDemuxDecodeFirstIIframeInit()) {
return -1;
}
if (type === MEDIA_TYPE.audio) {
return this.delay;
}
//
if (this.preDelayTimestamp && this.preDelayTimestamp > timestamp) {
if (this.preDelayTimestamp - timestamp > 1000) {
this.player.debug.warn('CommonDemux', `getDelay() and preDelayTimestamp is ${this.preDelayTimestamp} > timestamp is ${timestamp} more than ${this.preDelayTimestamp - timestamp}ms and return ${this.delay}`);
}
this.preDelayTimestamp = timestamp;
return this.delay;
}
if (!this.firstTimestamp) {
this.firstTimestamp = timestamp;
this.startTimestamp = Date.now();
this.delay = -1;
} else {
if (timestamp) {
const localTimestamp = Date.now() - this.startTimestamp;
const timeTimestamp = timestamp - this.firstTimestamp;
// console.error(`localTimestamp is ${localTimestamp}, timeTimestamp is ${timeTimestamp}`)
if (localTimestamp >= timeTimestamp) {
this.isStreamTsMoreThanLocal = false;
this.delay = localTimestamp - timeTimestamp;
} else {
// maybe some stream ts more large than local ts
this.isStreamTsMoreThanLocal = true;
this.delay = timeTimestamp - localTimestamp;
}
}
}
this.preDelayTimestamp = timestamp;
return this.delay;
}
getDelayNotUpdateDelay(timestamp, type) {
if (!timestamp || !this.player.isDemuxDecodeFirstIIframeInit()) {
return -1;
}
if (type === MEDIA_TYPE.audio) {
return this.pushLatestDelay;
}
if (this.preDelayTimestamp && this.preDelayTimestamp - timestamp > 1000) {
this.player.debug.warn('CommonDemux', `getDelayNotUpdateDelay() and preDelayTimestamp is ${this.preDelayTimestamp} > timestamp is ${timestamp} more than ${this.preDelayTimestamp - timestamp}ms and return -1`);
return -1;
}
if (!this.firstTimestamp) {
return -1;
} else {
let delay = -1;
if (timestamp) {
const localTimestamp = Date.now() - this.startTimestamp;
const timeTimestamp = timestamp - this.firstTimestamp;
if (localTimestamp >= timeTimestamp) {
delay = localTimestamp - timeTimestamp;
} else {
// maybe some stream ts more large than local ts
delay = timeTimestamp - localTimestamp;
}
}
return delay;
}
}
resetDelay() {
this.firstTimestamp = null;
this.startTimestamp = null;
this.delay = -1;
this.dropping = false;
}
resetAllDelay() {
this.resetDelay();
this.preDelayTimestamp = null;
}
//
initInterval() {
if (this.player.isUseHls265()) {
this.player.debug.log('CommonDemux', `initInterval() and is hls and support hls265 so return`);
return;
}
if (this.player.getStreamType().indexOf(PLAYER_STREAM_TYPE.worker) !== -1) {
this.player.debug.log('CommonDemux', `initInterval() and is worker stream so return`);
return;
}
if (this.player.isPlaybackCacheBeforeDecodeForFpsRender()) {
this.player.debug.log('CommonDemux', `initInterval() and playback and playbackIsCacheBeforeDecodeForFpsRender is true so return`);
return;
}
this.player.debug.log('CommonDemux', `setInterval()`);
this._loop();
this.stopId = setInterval(() => {
let nowTime = new Date().getTime();
if (!this.preLoopTimestamp) {
this.preLoopTimestamp = nowTime;
}
const diffTime = nowTime - this.preLoopTimestamp;
this.updateHistoryIntervalDiffTimeList(diffTime);
if (diffTime > 100) {
this.player.debug.warn('CommonDemux', `loop demux diff time is ${diffTime}`);
}
this._loop();
this.preLoopTimestamp = new Date().getTime();
}, DEMUX_LOOP_INTERVAL_TIMES);
}
clearStopInterval() {
if (this.stopId) {
clearInterval(this.stopId);
this.stopId = null;
}
}
updateHistoryIntervalDiffTimeList(diffTime) {
if (this.historyIntervalDiffTimeList.length > 5) {
this.historyIntervalDiffTimeList.shift();
}
this.historyIntervalDiffTimeList.push(diffTime);
}
isHistoryIntervalDiffTimeAllLarge() {
if (this.historyIntervalDiffTimeList.length < 5) {
return false;
}
for (let i = 0; i < this.historyIntervalDiffTimeList.length; i++) {
// less than 900ms
if (this.historyIntervalDiffTimeList[i] < 900) {
return false;
}
}
return true;
}
//
initPlaybackCacheLoop() {
this.clearStopInterval();
const loop = () => {
let data = null;
if (this.bufferList.length) {
data = this.bufferList.shift();
this._doDecoderDecode(data);
}
};
loop();
const fragDuration = Math.ceil(1000 / (this.playbackStreamFps * this.player.getPlaybackRate()));
this.player.debug.log('CommonDemux', `initPlaybackCacheLoop() and fragDuration is ${fragDuration}, playbackStreamFps is ${this.playbackStreamFps}, playbackRate is ${this.player.getPlaybackRate()}`);
this.stopId = setInterval(loop, fragDuration);
}
_loop() {
let data;
const videoBuffer = this.player._opt.videoBuffer;
const videoBufferDelay = this.player._opt.videoBufferDelay;
const isPlayer = this.player._opt.playType === PLAY_TYPE.player;
if (this.bufferList.length) {
if (this.isPushDropping) {
this.player.debug.warn('CommonDemux', `_loop isPushDropping is true and bufferList length is ${this.bufferList.length}`);
return;
}
if (this.dropping) {
data = this.bufferList.shift();
this.player.debug.warn('CommonDemux', `_loop is dropping and data.ts is ${data.ts}, data.type is ${data.type}, data.isIFrame is ${data.isIFrame}, delay is ${this.delay} ,buffer list is ${this.bufferList.length}`);
while (!data.isIFrame && this.bufferList.length) {
data = this.bufferList.shift();
}
const tempDelay = this.getDelayNotUpdateDelay(data.ts, data.type);
// i frame
// min delay
if (data.isIFrame && tempDelay <= this.getNotDroppingDelayTs()) {
this.player.debug.log('CommonDemux', `_loop data isIFrame is true and delay is ${this.delay}`);
this.dropping = false;
this._doDecoderDecode(data);
this._decodeNext(data);
}
} else {
if (this.player.isPlayback() || this.player.isPlayUseMSE() || videoBuffer === 0) {
while (this.bufferList.length) {
data = this.bufferList.shift();
this._doDecoderDecode(data);
}
} else {
data = this.bufferList[0];
if (this.getDelay(data.ts, data.type) === -1) {
this.player.debug.log('CommonDemux', `delay is -1 and data.ts is ${data.ts} data.type is ${data.type}`);
this.bufferList.shift();
this._doDecoderDecode(data);
this._decodeNext(data);
} else if (this.delay > videoBufferDelay + videoBuffer && isPlayer) {
if (this.hasIframeInBufferList()) {
this.player.debug.warn('CommonDemux', `_loop delay is ${this.delay}, set dropping is true`);
this.resetAllDelay();
this.dropping = true;
this.player.updateStats({
isDropping: true
});
} else {
this.bufferList.shift();
this._doDecoderDecode(data);
this._decodeNext(data);
}
} else {
while (this.bufferList.length) {
data = this.bufferList[0];
if (this.getDelay(data.ts, data.type) > videoBuffer) {
this.bufferList.shift();
this._doDecoderDecode(data);
} else {
if (this.delay < 0) {
this.player.debug.warn('CommonDemux', `_loop delay is ${this.delay} bufferList is ${this.bufferList}`);
}
break;
}
}
}
}
}
} else {
if (this.delay !== -1) {
this.player.debug.log('CommonDemux', `loop() bufferList is empty and reset delay`);
}
this.resetAllDelay();
}
}
//
_doDecode(payload, type, ts, isIFrame) {
let cts = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
const player = this.player;
let options = {
ts: ts,
cts: cts,
type: type,
isIFrame: false
};
if (this.player.isPlayer()) {
if (type === MEDIA_TYPE.video) {
if (player._opt.playType === PLAY_TYPE.player) {
this.calcNetworkDelay(ts);
}
}
// use offscreen
if (player._opt.useWCS && !player._opt.useOffscreen) {
if (type === MEDIA_TYPE.video) {
options.isIFrame = isIFrame;
}
this.pushBuffer(payload, options);
} else if (player._opt.useMSE) {
// use mse
if (type === MEDIA_TYPE.video) {
options.isIFrame = isIFrame;
}
this.pushBuffer(payload, options);
} else {
// wasm
if (type === MEDIA_TYPE.video) {
player.decoderWorker && player.decoderWorker.decodeVideo(payload, ts, isIFrame);
} else if (type === MEDIA_TYPE.audio) {
if (player._opt.hasAudio) {
player.decoderWorker && player.decoderWorker.decodeAudio(payload, ts);
}
}
}
} else if (this.player.isPlayback()) {
if (type === MEDIA_TYPE.video) {
options.isIFrame = isIFrame;
}
if (this.player.isPlaybackOnlyDecodeIFrame()) {
// only decode video not decode audio
if (type === MEDIA_TYPE.video && isIFrame) {
this.pushBuffer(payload, options);
}
} else {
// playback
if (this.player.isPlaybackCacheBeforeDecodeForFpsRender()) {
this.pushBuffer(payload, options);
} else {
if (this.player.getPlaybackRate() === 1) {
this.pushBuffer(payload, options);
} else {
this.pushBuffer(payload, options, false);
}
}
}
}
}
// for hls
_doDecodeByHls(payload, type, ts, isIFrame) {
let cts = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
let _isAudioAacCodecPacket = false;
// decode aac header
if (type === MEDIA_TYPE.audio && isAacCodecPacket(payload)) {
this.player.debug.log('CommonDemux', `hls pushBuffer audio ts is ${ts}, isAacCodecPacket is true`);
_isAudioAacCodecPacket = true;
// flv recorder
if (this.player.isRecordTypeFlv()) {
const payloadCopy = new Uint8Array(payload);
this.player.recorder.addAACSequenceHeader(payloadCopy, ts);
}
}
let _isVideoSequenceHeader = false;
// init video sequence header
if (type === MEDIA_TYPE.video && isIFrame && isVideoSequenceHeader(payload)) {
this.player.debug.log('CommonDemux', `hls pushBuffer video ts is ${ts}, isVideoSequenceHeader is true`);
_isVideoSequenceHeader = true;
// flv recorder
if (this.player.isRecordTypeFlv()) {
const payloadCopy = new Uint8Array(payload);
this.player.recorder.addVideoSequenceHeader(payloadCopy, ts);
}
}
// recording
if (this.player.recording && isFalse(_isVideoSequenceHeader) && isFalse(_isAudioAacCodecPacket)) {
this.handleRecording(payload, type, ts, isIFrame, cts);
}
if (type === MEDIA_TYPE.video) {
this._doDecoderDecode({
ts: ts,
cts: cts,
payload: payload,
type: MEDIA_TYPE.video,
isIFrame: isIFrame
});
} else if (type === MEDIA_TYPE.audio) {
this._doDecoderDecode({
ts: ts,
payload: payload,
type: MEDIA_TYPE.audio
});
}
}
// for fmp4
_doDecodeByFmp4(payload, type, ts, isIFrame) {
let cts = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
this._doDecode(payload, type, ts, isIFrame, cts);
}
_doDecodeByTs(payload, type, ts, isIFrame) {
let cts = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
this._doDecode(payload, type, ts, isIFrame, cts);
}
_decodeNext(data) {
const ts = data.ts;
if (this.bufferList.length === 0) {
return;
}
let nextData = this.bufferList[0];
const diff = nextData.ts - ts;
const isVideoAndNextAudio = data.type === MEDIA_TYPE.video && nextData.type === MEDIA_TYPE.audio;
const isSqeHeader = data.type === MEDIA_TYPE.video && isVideoSequenceHeader(data.payload);
// 如果
if (diff <= DEMUX_LOOP_INTERVAL_TIMES || isVideoAndNextAudio || isSqeHeader) {
this.player.debug.log('CommonDemux', `decode data type is ${data.type} and
ts is ${ts} next data type is ${nextData.type} ts is ${nextData.ts}
diff is ${diff} and isVideoAndNextAudio is ${isVideoAndNextAudio} and isVideoSqeHeader is ${isSqeHeader}`);
this.bufferList.shift();
this._doDecoderDecode(nextData);
}
}
_doDecoderDecode(data) {
const player = this.player;
const {
webcodecsDecoder,
mseDecoder
} = player;
// player.debug.log('CommonDemux', `_doDecoderDecode data.type is ${data.type} data.ts is ${data.ts}`);
if (this.player.isPlayer()) {
this.player.updateStats({
buf: this.delay
});
}
if (data.type === MEDIA_TYPE.audio) {
if (player._opt.hasAudio) {
if (player._opt.useMSE && player._opt.mseDecodeAudio) {
mseDecoder.decodeAudio(data.payload, data.ts);
} else {
player.decoderWorker && player.decoderWorker.decodeAudio(data.payload, data.ts);
}
}
} else if (data.type === MEDIA_TYPE.video) {
if (player._opt.isEmitSEI) {
this.findSei(data.payload, data.ts);
}
if (player._opt.useWCS && !player._opt.useOffscreen) {
webcodecsDecoder.decodeVideo(data.payload, data.ts, data.isIFrame, data.cts);
} else if (player._opt.useMSE) {
mseDecoder.decodeVideo(data.payload, data.ts, data.isIFrame, data.cts);
} else {
player.decoderWorker && player.decoderWorker.decodeVideo(data.payload, data.ts, data.isIFrame);
}
}
}
pushBuffer(payload, options) {
let isPushBuffer = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
// this.player.debug.error('CommonDemux', `
// payload ts is ${options.ts} and
// type is ${options.type} and isIFrame is ${options.isIFrame}`);
const videoBuffer = this.player._opt.videoBuffer;
const videoBufferDelay = this.player._opt.videoBufferDelay;
const isPlayer = this.player.isPlayer();
// decode aac header
if (options.type === MEDIA_TYPE.audio && isAacCodecPacket(payload)) {
this.player.debug.log('CommonDemux', `pushBuffer() audio ts is ${options.ts}, isAacCodecPacket is true`);
// flv recorder
if (this.player.isRecordTypeFlv()) {
const payloadCopy = new Uint8Array(payload);
this.player.recorder.addAACSequenceHeader(payloadCopy, options.ts);
}
this._doDecoderDecode({
ts: options.ts,
payload: payload,
type: MEDIA_TYPE.audio
});
return;
}
// init video sequence header
if (options.type === MEDIA_TYPE.video && options.isIFrame && isVideoSequenceHeader(payload)) {
this.player.debug.log('CommonDemux', `pushBuffer() video ts is ${options.ts}, isVideoSequenceHeader is true`);
// flv recorder
if (this.player.isRecordTypeFlv()) {
const payloadCopy = new Uint8Array(payload);
this.player.recorder.addVideoSequenceHeader(payloadCopy, options.ts);
}
this._doDecoderDecode({
ts: options.ts,
payload: payload,
type: MEDIA_TYPE.video,
isIFrame: options.isIFrame,
cts: options.cts
});
return;
}
// recording
if (this.player.recording) {
this.handleRecording(payload, options.type, options.ts, options.isIFrame, options.cts);
}
if (isPlayer) {
// for player
if (this.preTimestampDuration > 0 && this.preTimestamp > 0 && options.type === MEDIA_TYPE.video) {
const diff = options.ts - this.preTimestamp;
const maxDiff = this.preTimestampDuration + this.preTimestampDuration / 2;
if (diff >= maxDiff) {
this.player.debug.log('CommonDemux', `pushBuffer() video
ts is ${options.ts}, preTimestamp is ${this.preTimestamp},
diff is ${diff} and preTimestampDuration is ${this.preTimestampDuration} and maxDiff is ${maxDiff}
maybe trigger black screen or flower screen`);
}
}
// for player
if (this.preTimestamp > 0 && options.ts < this.preTimestamp && options.type === MEDIA_TYPE.video && this.preTimestamp - options.ts > FRAME_TS_MAX_DIFF) {
this.player.debug.warn('CommonDemux', `pushBuffer() video
ts is ${options.ts}, preTimestamp is ${this.preTimestamp},
diff is ${this.preTimestamp - options.ts} more than ${FRAME_TS_MAX_DIFF}
and resetAllDelay()`);
this.resetAllDelay();
}
// for player
if (options.ts <= this.preTimestamp && this.preTimestamp > 0 && options.type === MEDIA_TYPE.video) {
this.player.debug.warn('CommonDemux', `pushBuffer() video and isIFrame is ${options.isIFrame} and
ts is ${options.ts} less than (or equal) preTimestamp is ${this.preTimestamp} and
payloadBufferSize is ${payload.byteLength} and prevPayloadBufferSize is ${this.prevPayloadBufferSize}`);
if (this.player._opt.isDropSameTimestampGop && isFalse(options.isIFrame) &&
// 非i帧数据
this.player.isDemuxDecodeFirstIIframeInit()) {
const hasIframe = this.hasIframeInBufferList();
const isNotPushDropping = isFalse(this.isPushDropping);
this.player.debug.log('CommonDemux', `pushBuffer(), isDropSameTimestampGop is true and
hasIframe is ${hasIframe} and isNotPushDropping is ${isNotPushDropping} and next drop buffer`);
if (hasIframe && isNotPushDropping) {
this.dropBuffer$2();
} else {
this.clearBuffer(true);
}
return;
}
}
// for player
if (this.player.isDemuxDecodeFirstIIframeInit()) {
let pushLatestDelay = this.getDelayNotUpdateDelay(options.ts, options.type);
this.pushLatestDelay = pushLatestDelay;
const maxDelay = videoBufferDelay + videoBuffer;
if (this.player._opt.useMSE) {
if (pushLatestDelay > maxDelay && this.delay < maxDelay && this.delay > 0) {
if (this.hasIframeInBufferList() && this.isPushDropping === false) {
this.player.debug.warn('CommonDemux', `useMSE, pushLatestDelay is ${pushLatestDelay} > ${videoBufferDelay + videoBuffer}, bufferList is ${this.bufferList.length}, delay is ${this.delay} and dropBuffer$2()`);
this.dropBuffer$2();
}
}
} else {
if (pushLatestDelay > maxDelay && this.delay < maxDelay && this.delay > 0) {
if (this.hasIframeInBufferList() && this.isPushDropping === false) {
this.player.debug.warn('CommonDemux', `useWCS, pushLatestDelay is ${pushLatestDelay} > ${videoBufferDelay + videoBuffer},bufferList is ${this.bufferList.length}, delay is ${this.delay} and dropBuffer$2()`);
this.dropBuffer$2();
}
}
}
// for player
if (this.isHistoryIntervalDiffTimeAllLarge() && isFalse(this.player.visibility)) {
if (this.player._opt.useMSE) {
if (this.hasIframeInBufferList() && this.isPushDropping === false) {
this.player.debug.warn('CommonDemux', `useMSE, page visibility is false and
history interval diff is ${this.historyIntervalDiffTimeList.join(',')} and
bufferList is ${this.bufferList.length},
delay is ${this.delay} and dropBuffer$2()`);
this.dropBuffer$2();
}
} else {
if (this.hasIframeInBufferList() && this.isPushDropping === false) {
this.player.debug.warn('CommonDemux', `useWCS, page visibility is false and
history interval diff is ${this.historyIntervalDiffTimeList.join(',')} and
bufferList is ${this.bufferList.length},
delay is ${this.delay} and dropBuffer$2()`);
this.dropBuffer$2();
}
}
}
}
if (options.type === MEDIA_TYPE.video) {
if (this.preTimestamp > 0) {
this.preTimestampDuration = options.ts - this.preTimestamp;
}
this.prevPayloadBufferSize = payload.byteLength;
this.preTimestamp = options.ts;
}
}
if (isPushBuffer) {
// 音频
if (options.type === MEDIA_TYPE.audio) {
// this.player.debug.warn('CommonDemux', `pushBuffer audio ts is ${options.ts}`);
this.bufferList.push({
ts: options.ts,
payload: payload,
type: MEDIA_TYPE.audio
});
} else if (options.type === MEDIA_TYPE.video) {
// this.player.debug.error('CommonDemux', `pushBuffer video ts is ${options.ts}`);
this.bufferList.push({
ts: options.ts,
cts: options.cts,
payload: payload,
type: MEDIA_TYPE.video,
isIFrame: options.isIFrame
});
}
} else {
if (options.type === MEDIA_TYPE.video) {
this._doDecoderDecode({
ts: options.ts,
cts: options.cts,
payload: payload,
type: MEDIA_TYPE.video,
isIFrame: options.isIFrame
});
} else if (options.type === MEDIA_TYPE.audio) {
this._doDecoderDecode({
ts: options.ts,
payload: payload,
type: MEDIA_TYPE.audio
});
}
}
// for playback
if (this.player.isPlaybackCacheBeforeDecodeForFpsRender()) {
if (isEmpty(this.playbackStreamVideoFps) || isEmpty(this.playbackStreamAudioFps)) {
let playbackStreamVideoFps = this.playbackStreamVideoFps;
let playbackStreamAudioFps = this.playbackStreamAudioFps;
if (isEmpty(this.playbackStreamVideoFps)) {
playbackStreamVideoFps = calcStreamFpsByBufferList(this.bufferList, MEDIA_TYPE.video);
if (playbackStreamVideoFps > 0) {
this.playbackStreamVideoFps = playbackStreamVideoFps;
if (this.player.video) {
this.player.video.setStreamFps(this.playbackStreamVideoFps);
}
if (playbackStreamAudioFps) {
this.playbackStreamFps = playbackStreamVideoFps + playbackStreamAudioFps;
} else {
this.playbackStreamFps = playbackStreamVideoFps;
}
if (isFalse(this.player._opt.hasAudio)) {
this.player.debug.log(this.TAG_NAME, 'playbackCacheBeforeDecodeForFpsRender,_opt.hasAudio is false and set streamAudioFps is 0');
this.playbackStreamAudioFps = 0;
}
this.initPlaybackCacheLoop();
}
}
if (isEmpty(this.playbackStreamAudioFps)) {
playbackStreamAudioFps = calcStreamFpsByBufferList(this.bufferList, MEDIA_TYPE.audio);
if (playbackStreamAudioFps > 0) {
this.playbackStreamAudioFps = playbackStreamAudioFps;
if (playbackStreamVideoFps) {
this.playbackStreamFps = playbackStreamVideoFps + playbackStreamAudioFps;
} else {
this.playbackStreamFps = playbackStreamAudioFps;
}
this.initPlaybackCacheLoop();
}
}
if (isEmpty(this.playbackStreamVideoFps) && isEmpty(this.playbackStreamAudioFps)) {
const tsList = this.bufferList.map(item => {
return {
type: item.type,
ts: item.ts
};
});
this.player.debug.log('CommonDemux', `playbackCacheBeforeDecodeForFpsRender, calc streamAudioFps is ${playbackStreamAudioFps}, streamVideoFps is ${playbackStreamVideoFps}, bufferListLength is ${this.bufferList.length} and ts list is ${JSON.stringify(tsList)}`);
}
const hasAudio = this.getAudioBufferLength() > 0;
const maxBufferLength = hasAudio ? 60 : 40;
if (this.bufferList.length >= maxBufferLength) {
this.debug.warn('CommonDemux', `playbackCacheBeforeDecodeForFpsRender, bufferListLength is ${this.bufferList.length} more than ${maxBufferLength}, and hasAudio is ${hasAudio} an set streamFps is 25`);
this.playbackStreamVideoFps = playbackStreamVideoFps;
if (this.player.video) {
this.player.video.setStreamFps(this.playbackStreamVideoFps);
}
if (hasAudio) {
this.playbackStreamAudioFps = 25;
this.playbackStreamFps = this.playbackStreamVideoFps + this.playbackStreamAudioFps;
} else {
this.playbackStreamFps = this.playbackStreamVideoFps;
}
this.initPlaybackCacheLoop();
}
}
}
}
dropBuffer$2() {
if (this.bufferList.length > 0) {
let iFrameIndex = this.bufferList.findIndex(bufferItem => {
return isTrue(bufferItem.isIFrame) && bufferItem.type === MEDIA_TYPE.video;
});
if (this.isAllIframeInBufferList()) {
for (let i = 0; i < this.bufferList.length; i++) {
const bufferItem = this.bufferList[i];
const tempDelay = this.getDelayNotUpdateDelay(bufferItem.ts, bufferItem.type);
if (tempDelay >= this.getNotDroppingDelayTs()) {
this.player.debug.log('CommonDemux', `dropBuffer$2() isAllIframeInBufferList() is true, and index is ${i} and tempDelay is ${tempDelay} and notDroppingDelayTs is ${this.getNotDroppingDelayTs()}`);
iFrameIndex = i;
break;
}
}
}
if (iFrameIndex >= 0) {
this.isPushDropping = true;
this.player.updateStats({
isDropping: true
});
// old bufferList length
const bufferListLength = this.bufferList.length;
this.bufferList = this.bufferList.slice(iFrameIndex);
const iFrameItem = this.bufferList.shift();
this.resetAllDelay();
this.getDelay(iFrameItem.ts, iFrameItem.type);
this._doDecoderDecode(iFrameItem);
this.isPushDropping = false;
this.player.debug.log('CommonDemux', `dropBuffer$2() iFrameIndex is ${iFrameIndex},and old bufferList length is ${bufferListLength} ,and new bufferList length is ${this.bufferList.length} and new delay is ${this.delay} `);
} else {
this.isPushDropping = false;
}
}
if (this.bufferList.length === 0) {
this.isPushDropping = false;
}
}
clearBuffer() {
let needClear = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
this.player.debug.log('CommonDemux', `clearBuffer,buffer length is ${this.bufferList.length}, need clear is ${needClear} and _opt.checkFirstIFrame is ${this.player._opt.checkFirstIFrame}`);
if (needClear) {
this.bufferList = [];
}
if (this.player.isPlayer()) {
this.resetAllDelay();
if (isTrue(this.player._opt.checkFirstIFrame)) {
this.dropping = true;
this.player.updateStats({
isDropping: true
});
}
}
// waiting for i frame
this.player.decoderCheckFirstIFrame();
}
calcNetworkDelay(dts) {
//
if (!(this.player.isDemuxDecodeFirstIIframeInit() && dts > 0)) {
return;
}
if (this.bufferStartDts === null) {
this.bufferStartDts = dts;
this.bufferStartLocalTs = now$2();
} else {
//
if (dts < this.bufferStartDts) {
this.player.debug.warn('CommonDemux', `calcNetworkDelay dts is ${dts} and bufferStartDts is ${this.bufferStartDts}`);
this.bufferStartDts = dts;
this.bufferStartLocalTs = now$2();
}
}
let diff1 = dts - this.bufferStartDts;
let localDiff = now$2() - this.bufferStartLocalTs;
let delay = localDiff > diff1 ? localDiff - diff1 : 0;
// just for
if (delay > this.player._opt.networkDelay && this.player._opt.playType === PLAY_TYPE.player) {
this.player.debug.warn('CommonDemux', `delay is more than networkDelay and now dts:${dts},start dts is ${this.bufferStartDts}, vs start is ${diff1},local diff is ${localDiff} ,delay is ${delay}, _opt.networkDelay is ${this.player._opt.networkDelay}`);
this.player.emit(EVENTS.networkDelayTimeout, delay);
}
this.player.updateStats({
netBuf: delay
});
}
calcIframeIntervalTimestamp(ts) {
if (this.preIframeTs === null) {
this.preIframeTs = ts;
} else {
if (this.preIframeTs < ts) {
const intervalTimestamp = ts - this.preIframeTs;
if (this.player) {
this.player.videoIframeIntervalTs = intervalTimestamp;
}
// post 到主线程里面去。
this.preIframeTs = ts;
}
}
}
// 计算流的fps
calcBufferFps(ts) {}
getNotDroppingDelayTs() {
return this.player._opt.videoBuffer + this.player._opt.videoBufferDelay / 2;
}
getMaxDelayTs() {
return this.player._opt.videoBuffer + this.player._opt.videoBufferDelay;
}
getPushLatestDelay() {
return this.pushLatestDelay;
}
getVideoBufferLength() {
let result = 0;
this.bufferList.forEach(item => {
if (item.type === MEDIA_TYPE.video) {
result += 1;
}
});
return result;
}
getAudioBufferLength() {
let result = 0;
this.bufferList.forEach(item => {
if (item.type === MEDIA_TYPE.audio) {
result += 1;
}
});
return result;
}
hasIframeInBufferList() {
return this.bufferList.some(item => {
return item.type === MEDIA_TYPE.video && item.isIFrame;
});
}
isAllIframeInBufferList() {
const videoBufferLength = this.getVideoBufferLength();
let iFrameLength = 0;
this.bufferList.forEach(item => {
if (item.type === MEDIA_TYPE.video && item.isIFrame) {
iFrameLength += 1;
}
});
return videoBufferLength === iFrameLength;
}
getInputByteLength() {
return 0;
}
getIsStreamTsMoreThanLocal() {
return this.isStreamTsMoreThanLocal;
}
close() {}
reset() {}
findSei(payload, ts) {
let isHevc = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
let size = 4;
if (isNotEmpty(this.nalUnitSize)) {
size = this.nalUnitSize;
}
const units = parseAvcC(payload.slice(5), size);
if (isFalse(isHevc)) {
const videoInfo = this.player.getVideoInfo();
if (videoInfo && videoInfo.encType) {
isHevc = videoInfo.encType === VIDEO_ENC_TYPE_SHOW.h265;
}
}
units.forEach(unit => {
const type = isHevc ? unit[0] >>> 1 & 0x3f : unit[0] & 0x1f;
if (isHevc && (type === H265_NAL_TYPE.suffixSei || type === H265_NAL_TYPE.prefixSei) || isFalse(isHevc) && type === H264_NAL_TYPE.kSliceSEI) {
this.player.emit(EVENTS.videoSEI, {
ts,
data: unit
});
}
});
}
handleRecording(payload, type, ts, isIFrame, cts) {
if (this.player.isRecordTypeFlv()) {
const payloadCopy = new Uint8Array(payload);
if (type === MEDIA_TYPE.video) {
this.player.recorder.addVideo(payloadCopy, ts);
} else if (type === MEDIA_TYPE.audio) {
this.player.recorder.addAudio(payloadCopy, ts);
}
} else if (this.player.isRecordTypeMp4()) {
const payloadCopy = new Uint8Array(payload);
if (this.player.recorder.isWasmMp4()) {
// wasm mp4 录制
if (type === MEDIA_TYPE.video) {
// remove video tag header(1) + CompositionTime(4)
this.player.recorder.handleAddNaluTrack(payloadCopy.slice(5), isIFrame, ts, cts);
} else if (type === MEDIA_TYPE.audio) {
const payloadCopy = new Uint8Array(payload);
// aac need remove 2
// other need remove 1
this.player.recorder.handleAddAudioTrack(isAAC(payloadCopy) ? payloadCopy.slice(2) : payloadCopy.slice(1), ts);
}
} else {
// 普通的mp4录制。
if (type === MEDIA_TYPE.video) {
this.player.recorder.handleAddNaluTrack(payloadCopy.slice(5), isIFrame, ts, cts);
}
}
}
}
updateNalUnitSize(payload) {
const videoCodec = payload[0] & 0x0F;
this.player.video.updateVideoInfo({
encTypeCode: videoCodec
});
const isHevc = videoCodec === VIDEO_ENC_CODE.h265;
this.nalUnitSize = getUnitSizeFromVideoSequenceHeader(payload, isHevc);
this.player.debug.log(this.TAG_NAME, `demux() isVideoSequenceHeader is true and isHevc is ${isHevc} and nalUnitSize is ${this.nalUnitSize}`);
}
cryptoPayload(payload, isIFrame) {
let payloadBuffer = payload;
let player = this.player;
if (player._opt.isM7sCrypto) {
if (player._opt.cryptoKey && player._opt.cryptoKey.byteLength > 0 && player._opt.cryptoIV && player._opt.cryptoIV.byteLength > 0) {
const videoInfo = this.player.video.getVideoInfo();
if (videoInfo.encTypeCode) {
payloadBuffer = aes256ctrDecrypt(payload, player._opt.cryptoKey, player._opt.cryptoIV, videoInfo.encTypeCode === VIDEO_ENC_CODE.h265);
} else {
player.debug.warn(this.TAG_NAME, `videoInfo.encTypeCode is ${videoInfo.encTypeCode}`);
}
} else {
player.debug.error(this.TAG_NAME, `isM7sCrypto cryptoKey.length is ${player._opt.cryptoKey && player._opt.cryptoKey.byteLength} or cryptoIV.length is ${player._opt.cryptoIV && player._opt.cryptoIV.byteLength} null`);
}
} else if (player._opt.isSm4Crypto) {
if (player._opt.sm4CryptoKey && isIFrame) {
payloadBuffer = sm4Decrypt(payload, player._opt.sm4CryptoKey);
} else {
if (!player._opt.sm4CryptoKey) {
player.debug.error(this.TAG_NAME, `isSm4Crypto opt.sm4CryptoKey is null`);
}
}
} else if (player._opt.isXorCrypto) {
if (player._opt.cryptoKey && player._opt.cryptoKey.byteLength > 0 && player._opt.cryptoIV && player._opt.cryptoIV.byteLength > 0) {
const videoInfo = this.player.video.getVideoInfo();
payloadBuffer = xorDecrypt(payload, player._opt.cryptoKey, player._opt.cryptoIV, videoInfo.encTypeCode === VIDEO_ENC_CODE.h265);
} else {
player.debug.error(this.TAG_NAME, `isXorCrypto opt.xorCryptoKey is null`);
}
}
return payloadBuffer;
}
cryptoPayloadAudio(payload) {
let payloadBuffer = payload;
let player = this.player;
if (player._opt.isM7sCrypto) {
if (player._opt.cryptoKey && player._opt.cryptoKey.byteLength > 0 && player._opt.cryptoIV && player._opt.cryptoIV.byteLength > 0) {
const codecId = payload[0] >> 4;
if (codecId === AUDIO_ENC_CODE.AAC) {
payloadBuffer = aes256ctrDecryptAacAudio(payload, player._opt.cryptoKey, player._opt.cryptoIV);
}
} else {
player.debug.error(this.TAG_NAME, `isM7sCrypto cryptoKey.length is ${player._opt.cryptoKey && player._opt.cryptoKey.byteLength} or cryptoIV.length is ${player._opt.cryptoIV && player._opt.cryptoIV.byteLength} null`);
}
}
return payloadBuffer;
}
_decodeEnhancedH265Video(payload, dts) {
const flags = payload[0];
const frameTypeEx = flags & 0x30;
const packetEx = flags & 0x0F;
const codecId = payload.slice(1, 5);
const tmp = new ArrayBuffer(4);
const tmp32 = new Uint32Array(tmp);
// 'av01' 'hvc1'
const isAV1 = String.fromCharCode(codecId[0]) == 'a';
if (packetEx === PACKET_TYPE_EX.PACKET_TYPE_SEQ_START) {
if (frameTypeEx === FRAME_TYPE_EX.FT_KEY) {
// header video info
const extraData = payload.slice(5);
if (isAV1) ; else {
const payloadBuffer = new Uint8Array(5 + extraData.length);
payloadBuffer.set([0x1c, 0x00, 0x00, 0x00, 0x00], 0);
payloadBuffer.set(extraData, 5);
this.updateNalUnitSize(payloadBuffer);
this.player.debug.log(this.TAG_NAME, `demux() isVideoSequenceHeader(enhancedH265) is true and nalUnitSize is ${this.nalUnitSize}`);
this._doDecode(payloadBuffer, MEDIA_TYPE.video, 0, true, 0);
}
}
} else if (packetEx === PACKET_TYPE_EX.PACKET_TYPE_FRAMES) {
let payloadBuffer = payload;
let cts = 0;
const isIFrame = frameTypeEx === FRAME_TYPE_EX.FT_KEY;
if (isIFrame) {
this.calcIframeIntervalTimestamp(dts);
}
if (isAV1) ; else {
// h265
tmp32[0] = payload[4];
tmp32[1] = payload[3];
tmp32[2] = payload[2];
tmp32[3] = 0;
cts = tmp32[0];
const data = payload.slice(8);
payloadBuffer = hevcEncoderNalePacketNotLength(data, isIFrame);
payloadBuffer = this.cryptoPayload(payloadBuffer, isIFrame);
this._doDecode(payloadBuffer, MEDIA_TYPE.video, dts, isIFrame, cts);
}
} else if (packetEx === PACKET_TYPE_EX.PACKET_TYPE_FRAMESX) {
const isIFrame = frameTypeEx === FRAME_TYPE_EX.FT_KEY;
const data = payload.slice(5);
if (isIFrame) {
this.calcIframeIntervalTimestamp(dts);
}
let payloadBuffer = hevcEncoderNalePacketNotLength(data, isIFrame);
payloadBuffer = this.cryptoPayload(payloadBuffer, isIFrame);
this._doDecode(payloadBuffer, MEDIA_TYPE.video, dts, isIFrame, 0);
}
}
_isEnhancedH265Header(flags) {
return (flags & FRAME_HEADER_EX) === FRAME_HEADER_EX;
}
}
var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const U32 = Symbol(32);
const U16 = Symbol(16);
const U8 = Symbol(8);
class OPut {
constructor(g) {
this.g = g;
this.consumed = 0;
if (g)
this.need = g.next().value;
}
setG(g) {
this.g = g;
this.demand(g.next().value, true);
}
consume() {
if (this.buffer && this.consumed) {
this.buffer.copyWithin(0, this.consumed);
this.buffer = this.buffer.subarray(0, this.buffer.length - this.consumed);
this.consumed = 0;
}
}
demand(n, consume) {
if (consume)
this.consume();
this.need = n;
return this.flush();
}
read(need) {
return __awaiter(this, void 0, void 0, function* () {
if (this.lastReadPromise) {
yield this.lastReadPromise;
}
return this.lastReadPromise = new Promise((resolve, reject) => {
var _a;
this.reject = reject;
this.resolve = (data) => {
delete this.lastReadPromise;
delete this.resolve;
delete this.need;
resolve(data);
};
const result = this.demand(need, true);
if (!result)
(_a = this.pull) === null || _a === void 0 ? void 0 : _a.call(this, need); //已饥饿,等待数据
});
});
}
readU32() {
return this.read(U32);
}
readU16() {
return this.read(U16);
}
readU8() {
return this.read(U8);
}
close() {
var _a;
if (this.g)
this.g.return();
if (this.buffer)
this.buffer.subarray(0, 0);
(_a = this.reject) === null || _a === void 0 ? void 0 : _a.call(this, new Error('EOF'));
delete this.lastReadPromise;
}
flush() {
if (!this.buffer || !this.need)
return;
let returnValue = null;
const unread = this.buffer.subarray(this.consumed);
let n = 0;
const notEnough = (x) => unread.length < (n = x);
if (typeof this.need === 'number') {
if (notEnough(this.need))
return;
returnValue = unread.subarray(0, n);
}
else if (this.need === U32) {
if (notEnough(4))
return;
returnValue = (unread[0] << 24) | (unread[1] << 16) | (unread[2] << 8) | unread[3];
}
else if (this.need === U16) {
if (notEnough(2))
return;
returnValue = (unread[0] << 8) | unread[1];
}
else if (this.need === U8) {
if (notEnough(1))
return;
returnValue = unread[0];
}
else if (!('buffer' in this.need)) {
if (notEnough(this.need.byteLength))
return;
new Uint8Array(this.need).set(unread.subarray(0, n));
returnValue = this.need;
}
else if ('byteOffset' in this.need) {
if (notEnough(this.need.byteLength - this.need.byteOffset))
return;
new Uint8Array(this.need.buffer, this.need.byteOffset).set(unread.subarray(0, n));
returnValue = this.need;
}
else if (this.g) {
this.g.throw(new Error('Unsupported type'));
return;
}
this.consumed += n;
if (this.g)
this.demand(this.g.next(returnValue).value, true);
else if (this.resolve)
this.resolve(returnValue);
return returnValue;
}
write(value) {
if (value instanceof Uint8Array) {
this.malloc(value.length).set(value);
}
else if ('buffer' in value) {
this.malloc(value.byteLength).set(new Uint8Array(value.buffer, value.byteOffset, value.byteLength));
}
else {
this.malloc(value.byteLength).set(new Uint8Array(value));
}
if (this.g || this.resolve)
this.flush();
//富余,需要等到饥饿
else
return new Promise((resolve) => this.pull = resolve);
}
writeU32(value) {
this.malloc(4).set([(value >> 24) & 0xff, (value >> 16) & 0xff, (value >> 8) & 0xff, value & 0xff]);
this.flush();
}
writeU16(value) {
this.malloc(2).set([(value >> 8) & 0xff, value & 0xff]);
this.flush();
}
writeU8(value) {
this.malloc(1)[0] = value;
this.flush();
}
malloc(size) {
if (this.buffer) {
const l = this.buffer.length;
const nl = l + size;
if (nl <= this.buffer.buffer.byteLength - this.buffer.byteOffset) {
this.buffer = new Uint8Array(this.buffer.buffer, this.buffer.byteOffset, nl);
}
else {
const n = new Uint8Array(nl);
n.set(this.buffer);
this.buffer = n;
}
return this.buffer.subarray(l, nl);
}
else {
this.buffer = new Uint8Array(size);
return this.buffer;
}
}
}
OPut.U32 = U32;
OPut.U16 = U16;
OPut.U8 = U8;
class FlvLoader extends CommonLoader {
constructor(player) {
super(player);
this.TAG_NAME = 'FlvDemux';
this.input = new OPut(this.demux());
player.debug.log(this.TAG_NAME, 'init');
}
destroy() {
super.destroy();
this.input = null;
this.player.debug.log(this.TAG_NAME, 'destroy');
}
dispatch(data) {
if (this.input) {
this.input.write(data);
} else {
this.player && this.player.debug.warn(this.TAG_NAME, 'dispatch() this.input is null');
}
// this.player.debug.log(this.TAG_NAME, `this.input.buffer.length is ${this.input.buffer.length},byteLength is ${this.input.buffer.byteLength}`);
}
*demux() {
yield 9;
const tmp = new ArrayBuffer(4);
const tmp8 = new Uint8Array(tmp);
const tmp32 = new Uint32Array(tmp);
const player = this.player;
while (true) {
if (!this.input) {
return;
}
tmp8[3] = 0;
const t = yield 15;
const type = t[4];
tmp8[0] = t[7];
tmp8[1] = t[6];
tmp8[2] = t[5];
const length = tmp32[0];
tmp8[0] = t[10];
tmp8[1] = t[9];
tmp8[2] = t[8];
tmp8[3] = t[11];
let ts = tmp32[0];
const payload = (yield length).slice();
if (!player) {
return;
}
switch (type) {
case FLV_MEDIA_TYPE.audio:
if (player._opt.hasAudio) {
player.updateStats({
abps: payload.byteLength
});
if (payload.byteLength > 0) {
let payloadBuffer = payload;
if (isTrue(this.player._opt.m7sCryptoAudio)) {
payloadBuffer = this.cryptoPayloadAudio(payload);
}
this._doDecode(payloadBuffer, MEDIA_TYPE.audio, ts);
}
}
break;
case FLV_MEDIA_TYPE.video:
// console.log('flv loader demux', payload);
if (player._opt.hasVideo && payload.length >= 6) {
let dts = ts;
player.updateStats({
vbps: payload.byteLength,
dts: dts
});
if (!player._times.demuxStart) {
player._times.demuxStart = now$2();
}
const flags = payload[0];
if (this._isEnhancedH265Header(flags)) {
this._decodeEnhancedH265Video(payload, dts);
} else {
const codecId = flags & 0x0F;
const frameType = flags >> 4 & 0x0F;
let isIFrame = frameType === VIDEO_FRAME_TYPE.keyFrame;
const isH265 = codecId === VIDEO_ENC_CODE.h265;
const isH264 = codecId === VIDEO_ENC_CODE.h264;
if (isFalse(isH265 || isH264)) {
this.player.debug.warn(this.TAG_NAME, `demux() codecId is ${codecId} and ignore`);
return;
}
if (isIFrame) {
this.calcIframeIntervalTimestamp(ts);
if (this.nalUnitSize === null && isVideoSequenceHeader(payload)) {
this.updateNalUnitSize(payload);
}
}
// cts
tmp32[0] = payload[4];
tmp32[1] = payload[3];
tmp32[2] = payload[2];
tmp32[3] = 0;
let cts = tmp32[0];
let payloadBuffer = this.cryptoPayload(payload, isIFrame);
this._doDecode(payloadBuffer, MEDIA_TYPE.video, ts, isIFrame, cts);
}
} else {
if (payload.length < 6) {
player.debug.warn(this.TAG_NAME, `payload.length is ${payload.length} less than 6 and ignore`);
}
}
break;
case FLV_MEDIA_TYPE.scriptData:
if (this.player.isRecordTypeFlv()) {
const payloadCopy = new Uint8Array(payload);
this.player.recorder.addMetaData(payloadCopy);
}
const scriptObj = parseFlvScriptData(payload);
if (scriptObj && scriptObj.onMetaData) {
player.updateMetaData(scriptObj.onMetaData);
}
break;
default:
player.debug.log(this.TAG_NAME, `demux() type is ${type}`);
break;
}
}
}
close() {
this.input = null;
}
getInputByteLength() {
let result = 0;
if (this.input && this.input.buffer) {
result = this.input.buffer.byteLength;
}
return result;
}
}
class M7sLoader extends CommonLoader {
constructor(player) {
super(player);
this.TAG_NAME = 'M7sDemux';
player.debug.log(this.TAG_NAME, 'init');
}
destroy() {
super.destroy();
this.player.debug.log(this.TAG_NAME, 'destroy');
}
dispatch(data) {
const player = this.player;
const dv = new DataView(data);
const type = dv.getUint8(0);
const ts = dv.getUint32(1, false);
const tmp = new ArrayBuffer(4);
const tmp32 = new Uint32Array(tmp);
switch (type) {
case MEDIA_TYPE.audio:
if (player._opt.hasAudio) {
const payload = new Uint8Array(data, 5);
player.updateStats({
abps: payload.byteLength
});
if (payload.byteLength > 0) {
this._doDecode(payload, type, ts);
}
}
break;
case MEDIA_TYPE.video:
if (player._opt.hasVideo) {
if (!player._times.demuxStart) {
player._times.demuxStart = now$2();
}
if (dv.byteLength >= 5 + 6) {
const payload = new Uint8Array(data, 5);
let dts = ts;
player.updateStats({
vbps: payload.byteLength,
dts: dts
});
const flag = payload[0];
if (this._isEnhancedH265Header(flag)) {
this._decodeEnhancedH265Video(payload, ts);
} else {
const isIFrame = dv.getUint8(5) >> 4 === 1;
if (isIFrame) {
this.calcIframeIntervalTimestamp(ts);
}
tmp32[0] = payload[4];
tmp32[1] = payload[3];
tmp32[2] = payload[2];
tmp32[3] = 0;
let cts = tmp32[0];
let payloadBuffer = this.cryptoPayload(payload, isIFrame);
this._doDecode(payloadBuffer, type, ts, isIFrame, cts);
}
} else {
this.player.debug.warn(this.TAG_NAME, 'dispatch', 'dv byteLength is', dv.byteLength, 'and return');
}
}
break;
}
}
}
class WebTransportLoader extends FlvLoader {
constructor(player) {
super(player);
player.debug.log('WebTransportDemux', 'init');
}
destroy() {
this.player.debug.log('WebTransportDemux', 'destroy');
super.destroy();
}
}
var defineProperty = createCommonjsModule(function (module) {
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
module.exports = _defineProperty, module.exports.__esModule = true, module.exports["default"] = module.exports;
});
var _defineProperty$1 = unwrapExports(defineProperty);
class NakedFlowLoader extends CommonLoader {
constructor(player) {
super(player);
_defineProperty$1(this, "TAG_NAME", 'NakedFlowDemux');
this.lastBuf = null;
this.vps = null;
this.sps = null;
this.pps = null;
this.streamVideoType = null;
this.streamAudioType = null;
this.tempNaluBufferList = new Uint8Array(0);
this.localDts = 0;
this.isSendSeqHeader = false;
this.isSendAACSeqHeader = false;
player.debug.log(this.TAG_NAME, 'init');
}
destroy() {
super.destroy();
this.lastBuf = null;
this.vps = null;
this.sps = null;
this.pps = null;
this.streamVideoType = null;
this.streamAudioType = null;
this.tempNaluBufferList = new Uint8Array(0);
this.localDts = 0;
this.localAudioDts = 0;
this.isSendSeqHeader = false;
this.isSendAACSeqHeader = false;
this.player.debug.log(this.TAG_NAME, 'destroy');
}
//
dispatch(data) {
this.player;
const uint8Array = new Uint8Array(data);
// player.debug.log(this.TAG_NAME, 'dispatch', uint8Array.byteLength);
// const naluArray = this.extractNALu(uint8Array);
// console.log('naluArray', naluArray);
this.extractNALu$2(uint8Array);
// this.addNaluToBuffer(uint8Array);
// this.handleNALu(uint8Array);
}
addNaluToBuffer(nalu) {
const len = nalu.byteLength + this.tempNaluBufferList.byteLength;
const newBuffer = new Uint8Array(len);
newBuffer.set(this.tempNaluBufferList, 0);
newBuffer.set(nalu, this.tempNaluBufferList.byteLength);
this.tempNaluBufferList = newBuffer;
//console.log('addNaluToBuffer byteLength is', this.tempNaluBufferList.byteLength);
}
downloadNakedFlowFile() {
const blob = new Blob([this.tempNaluBufferList]);
try {
const oa = document.createElement('a');
oa.href = window.URL.createObjectURL(blob);
oa.download = Date.now() + '.h264';
oa.click();
window.URL.revokeObjectURL(oa.href);
} catch (e) {
console.error('downloadTempNalu', e);
}
}
//
getNaluDts() {
const nakedFlowFps = this.player._opt.nakedFlowFps;
// console.log(`nakedFlowFps is ${nakedFlowFps} and dts is ${(1000 / nakedFlowFps)}`);
this.localDts = this.localDts + parseInt(1000 / nakedFlowFps);
return this.localDts;
}
getNaluAudioDts() {
const audioContextSampleRate = this.player.audio.audioContext.sampleRate;
const audioBufferSize = this.player.audio.audioBufferSize;
// dts
return this.localDts + parseInt(audioBufferSize / audioContextSampleRate * 1000);
}
//
extractNALu(buffer) {
let i = 0,
length = buffer.byteLength,
value,
state = 0,
result = [],
lastIndex;
while (i < length) {
value = buffer[i++];
// Annex-B格式使用start code进行分割,start code为0x000001或0x00000001,
// SPS/PPS作为一般NALU单元以start code作为分隔符的方式放在文件或者直播流的头部。
// finding 3 or 4-byte start codes (00 00 01 OR 00 00 00 01)
switch (state) {
case 0:
if (value === 0) {
state = 1;
}
break;
case 1:
if (value === 0) {
state = 2;
} else {
state = 0;
}
break;
case 2:
case 3:
if (value === 0) {
state = 3;
} else if (value === 1 && i < length) {
buffer[i] & 0x1f;
if (lastIndex) {
result.push(buffer.subarray(lastIndex, i - state - 1));
// console.error('lastUnitType', lastUnitType);
}
lastIndex = i;
state = 0;
} else {
state = 0;
}
break;
}
}
if (lastIndex) {
result.push(buffer.subarray(lastIndex, length));
// console.error('lastUnitType', lastUnitType);
}
return result;
}
extractNALu$2(buffer) {
let typedArray = null;
if (!buffer || buffer.byteLength < 1) return;
if (this.lastBuf) {
typedArray = new Uint8Array(buffer.byteLength + this.lastBuf.length);
typedArray.set(this.lastBuf);
typedArray.set(new Uint8Array(buffer), this.lastBuf.length);
} else {
typedArray = new Uint8Array(buffer);
}
let lastNalEndPos = 0;
let b1 = -1; // byte before one
let b2 = -2; // byte before two
const nalStartPos = new Array();
for (let i = 0; i < typedArray.length; i += 2) {
const b_0 = typedArray[i];
const b_1 = typedArray[i + 1];
if (b1 == 0 && b_0 == 0 && b_1 == 0) {
nalStartPos.push(i - 1);
} else if (b_1 == 1 && b_0 == 0 && b1 == 0 && b2 == 0) {
nalStartPos.push(i - 2);
}
b2 = b_0;
b1 = b_1;
}
if (nalStartPos.length > 1) {
for (let i = 0; i < nalStartPos.length - 1; ++i) {
const naluItem = typedArray.subarray(nalStartPos[i], nalStartPos[i + 1] + 1);
this.handleNALu(naluItem);
//console.log('nakedFlowDemuxer.lastBuf nalType', this.lastBuf.byteLength);
lastNalEndPos = nalStartPos[i + 1];
}
} else {
lastNalEndPos = nalStartPos[0];
}
if (lastNalEndPos != 0 && lastNalEndPos < typedArray.length) {
this.lastBuf = typedArray.subarray(lastNalEndPos);
} else {
if (!!!this.lastBuf) {
this.lastBuf = typedArray;
}
const _newBuf = new Uint8Array(this.lastBuf.length + buffer.byteLength);
_newBuf.set(this.lastBuf);
_newBuf.set(new Uint8Array(buffer), this.lastBuf.length);
this.lastBuf = _newBuf;
}
}
handleNALu(nalu) {
if (nalu.byteLength < 4) {
this.player.debug.warn(this.TAG_NAME, `handleNALu nalu byteLength is ${nalu.byteLength} <= 4`);
return;
}
// 0001 去掉前4个字节(start code)
nalu = nalu.slice(4);
this.handleVideoNalu(nalu);
}
handleVideoNalu(nalu) {
const uint8Array = new Uint8Array(nalu);
// todo:这边首帧必须是sps/pps/vps 不能是P帧或者I帧,这个方法会检测失败的。
if (!this.streamVideoType) {
this.streamVideoType = checkNaluType(uint8Array);
}
if (this.streamVideoType === VIDEO_ENC_TYPE_SHOW.h264) {
const tempNalu = this.handleAddNaluStartCode(uint8Array);
const naluList = this.extractNALu(tempNalu);
if (naluList.length === 0) {
this.player.debug.warn(this.TAG_NAME, 'handleVideoNalu', 'naluList.length === 0');
return;
}
const newNaluList = [];
naluList.forEach(naluItem => {
const nalType = getAvcSeqHeadType(naluItem);
if (nalType === H264_NAL_TYPE.pps || nalType === H264_NAL_TYPE.sps) {
this.handleVideoH264Nalu(naluItem);
} else {
if (isNotAvcSeqHead(nalType)) {
newNaluList.push(naluItem);
}
}
});
if (newNaluList.length === 1) {
this.handleVideoH264Nalu(newNaluList[0]);
} else {
const isSameNaluType = isSameAvcNaluType(newNaluList);
if (isSameNaluType) {
const naluType = getAvcSeqHeadType(newNaluList[0]);
const isIFrame = isAvcNaluIFrame(naluType);
this.handleVideoH264NaluList(newNaluList, isIFrame, naluType);
} else {
newNaluList.forEach(naluItem => {
this.handleVideoH264Nalu(naluItem);
});
}
}
} else if (this.streamVideoType === VIDEO_ENC_TYPE_SHOW.h265) {
if (this.player._opt.nakedFlowH265DemuxUseNew) {
const tempNalu = this.handleAddNaluStartCode(uint8Array);
const naluList = this.extractNALu(tempNalu);
if (naluList.length === 0) {
this.player.debug.warn(this.TAG_NAME, 'handleVideoNalu', 'h265 naluList.length === 0');
return;
}
const newNaluList = [];
naluList.forEach(naluItem => {
const nalType = getHevcSeqHeadType(naluItem);
if (nalType === H265_NAL_TYPE.pps || nalType === H265_NAL_TYPE.sps || nalType === H265_NAL_TYPE.vps) {
this.handleVideoH265Nalu(naluItem);
} else {
if (isNotHevcSeqHead(nalType)) {
newNaluList.push(naluItem);
}
}
});
if (newNaluList.length === 1) {
this.handleVideoH265Nalu(newNaluList[0]);
} else {
const isSameNaluType = isSameHevcNaluType(newNaluList);
if (isSameNaluType) {
const naluType = getHevcSeqHeadType(newNaluList[0]);
const isIFrame = isHevcNaluIFrame(naluType);
this.handleVideoH265NaluList(newNaluList, isIFrame, naluType);
} else {
newNaluList.forEach(naluItem => {
this.handleVideoH265Nalu(naluItem);
});
}
}
} else {
const naluType = getHevcSeqHeadType(uint8Array);
if (naluType === H265_NAL_TYPE.pps) {
this.extractH265PPS(uint8Array);
} else {
this.handleVideoH265Nalu(uint8Array);
}
}
} else {
this.player.debug.error(this.TAG_NAME, ` this.streamVideoType is null`);
}
}
extractH264PPS(nalu) {
const tempNalu = this.handleAddNaluStartCode(nalu);
const naluList = this.extractNALu(tempNalu);
naluList.forEach(naluItem => {
const nalType = getAvcSeqHeadType(naluItem);
if (isHvcSEIType(nalType)) {
this.extractH264SEI(naluItem);
} else {
this.handleVideoH264Nalu(naluItem);
}
});
}
extractH265PPS(nalu) {
const tempNalu = this.handleAddNaluStartCode(nalu);
const naluList = this.extractNALu(tempNalu);
naluList.forEach(naluItem => {
const nalType = getHevcSeqHeadType(naluItem);
if (isHevcSEIType(nalType)) {
this.extractH265SEI(naluItem);
} else {
this.handleVideoH265Nalu(naluItem);
}
});
}
extractH264SEI(nalu) {
const tempNalu = this.handleAddNaluStartCode(nalu);
const naluList = this.extractNALu(tempNalu);
naluList.forEach(naluItem => {
this.handleVideoH264Nalu(naluItem);
});
}
extractH265SEI(nalu) {
const tempNalu = this.handleAddNaluStartCode(nalu);
const naluList = this.extractNALu(tempNalu);
//console.log('extractH265SEI', naluList);
naluList.forEach(naluItem => {
this.handleVideoH265Nalu(naluItem);
});
}
handleAddNaluStartCode(nalu) {
const prefix = [0, 0, 0, 1];
const newNalu = new Uint8Array(nalu.length + prefix.length);
newNalu.set(prefix);
newNalu.set(nalu, prefix.length);
return newNalu;
}
handleAudioAACNalu(nalu) {
if (!nalu || nalu.byteLength < 1) return;
if (!this.streamAudioType) {
this.streamAudioType = AUDIO_ENC_CODE_SHOW.AAC;
}
let uint8Array = new Uint8Array(nalu);
// 需要取出7个字节的ADTS头。
const accADTSHeader = uint8Array.slice(0, 7);
// 移除掉 ADTS头
uint8Array = uint8Array.slice(7);
if (!this.isSendAACSeqHeader) {
// 七个字节的adts头;
// aac 格式
// 需要先发序列帧。然后再发后续的
// 序列帧:asc 序列
// 后续的帧,0 变成 1 就行了。
// 0: Main profile
// 1: Low Complexity profile(LC)
// 2: Scalable Sampling Rate profile(SSR)
const profile = (accADTSHeader[2] & 0xc0) >> 6;
// const profile = 0;
// const sampleRate = (+audioInfo.sampleRate);
const sampleRate = (accADTSHeader[2] & 0x3C) >> 2;
// const channel = (+audioInfo.channel);
const channel = (accADTSHeader[2] & 0x1) << 2 | (accADTSHeader[3] & 0xc0) >> 6;
const config1 = profile << 3 | (sampleRate & 0xe) >> 1;
const config2 = (sampleRate & 0x1) << 7 | channel << 3;
// 0xAF >> 4 === 10
const temp = [0xAF, 0x00, config1, config2];
const arrayBuffer = new Uint8Array(temp);
this.isSendAACSeqHeader = true;
this._doDecode(arrayBuffer, MEDIA_TYPE.audio, 0, false, 0);
}
const dts = this.getNaluAudioDts();
const arrayBuffer = new Uint8Array(uint8Array.length + 2);
arrayBuffer.set([0xAF, 0x01], 0);
arrayBuffer.set(uint8Array, 2);
this._doDecode(arrayBuffer, MEDIA_TYPE.audio, dts, false, 0);
}
handleAudioG711ANalu(nalu) {
if (!nalu || nalu.byteLength < 1) return;
if (!this.streamAudioType) {
this.streamAudioType = AUDIO_ENC_CODE_SHOW.ALAW;
}
let uint8Array = new Uint8Array(nalu);
const dts = this.getNaluAudioDts();
const arrayBuffer = new Uint8Array(uint8Array.length + 1);
arrayBuffer.set([7 << 4 | 1 << 1], 0);
arrayBuffer.set(uint8Array, 1);
this._doDecode(arrayBuffer, MEDIA_TYPE.audio, dts, false, 0);
}
handleAudioG711UNalu(nalu) {
if (!nalu || nalu.byteLength < 1) return;
if (!this.streamAudioType) {
this.streamAudioType = AUDIO_ENC_CODE_SHOW.MULAW;
}
let uint8Array = new Uint8Array(nalu);
const dts = this.getNaluAudioDts();
const arrayBuffer = new Uint8Array(uint8Array.length + 1);
arrayBuffer.set([8 << 4 | 1 << 1], 0);
arrayBuffer.set(uint8Array, 1);
this._doDecode(arrayBuffer, MEDIA_TYPE.audio, dts, false, 0);
}
handleVideoH264Nalu(nalu) {
const nalType = getAvcSeqHeadType(nalu);
// this.player.debug.log(this.TAG_NAME, `handleVideoH264Nalu anlType is ${nalType}, nalu[0] is ${nalu[0]}`);
switch (nalType) {
case H264_NAL_TYPE.sps:
this.sps = nalu;
break;
case H264_NAL_TYPE.pps:
this.pps = nalu;
break;
}
if (!this.isSendSeqHeader) {
if (this.sps && this.pps) {
this.isSendSeqHeader = true;
const seqHeader = avcEncoderConfigurationRecord$2({
sps: this.sps,
pps: this.pps
});
this._doDecode(seqHeader, MEDIA_TYPE.video, 0, true, 0);
this.sps = null;
this.pps = null;
}
} else {
if (this.sps && this.pps) {
const seqHeader = avcEncoderConfigurationRecord$2({
sps: this.sps,
pps: this.pps
});
const dts = this.getNaluDts();
this._doDecode(seqHeader, MEDIA_TYPE.video, dts, true, 0);
this.sps = null;
this.pps = null;
}
if (isNotAvcSeqHead(nalType)) {
if (!this.player._times.demuxStart) {
this.player._times.demuxStart = now$2();
}
const isIFrame = isAvcNaluIFrame(nalType);
const dts = this.getNaluDts();
const packet = avcEncoderNalePacket(nalu, isIFrame);
this._preDoDecode(packet, MEDIA_TYPE.video, dts, isIFrame, 0);
} else {
this.player.debug.warn(this.TAG_NAME, `handleVideoH264Nalu is avc seq head nalType is ${nalType}`);
}
}
}
handleVideoH264NaluList(naluList, isIFrame, naluType) {
if (this.isSendSeqHeader) {
if (!this.player._times.demuxStart) {
this.player._times.demuxStart = now$2();
}
const dts = this.getNaluDts();
const newNalu = naluList.reduce((pre, cur) => {
const nalu2 = addNaleHeaderLength(pre);
const nalu3 = addNaleHeaderLength(cur);
const nalu4 = new Uint8Array(nalu2.byteLength + nalu3.byteLength);
nalu4.set(nalu2, 0);
nalu4.set(nalu3, nalu2.byteLength);
return nalu4;
});
const packet = avcEncoderNalePacketNotLength(newNalu, isIFrame);
this._preDoDecode(packet, MEDIA_TYPE.video, dts, isIFrame, 0);
} else {
this.player.debug.warn(this.TAG_NAME, `handleVideoH264NaluList isSendSeqHeader is false`);
}
}
handleVideoH265Nalu(nalu) {
const nalType = getHevcSeqHeadType(nalu);
switch (nalType) {
case H265_NAL_TYPE.vps:
this.vps = nalu;
break;
case H265_NAL_TYPE.sps:
this.sps = nalu;
break;
case H265_NAL_TYPE.pps:
this.pps = nalu;
break;
}
if (!this.isSendSeqHeader) {
if (this.vps && this.sps && this.pps) {
this.isSendSeqHeader = true;
const seqHeader = hevcEncoderConfigurationRecord$2({
vps: this.vps,
sps: this.sps,
pps: this.pps
});
this._doDecode(seqHeader, MEDIA_TYPE.video, 0, true, 0);
this.vps = null;
this.sps = null;
this.pps = null;
}
} else {
if (this.vps && this.sps && this.pps) {
const seqHeader = hevcEncoderConfigurationRecord$2({
vps: this.vps,
sps: this.sps,
pps: this.pps
});
const dts = this.getNaluDts();
this._doDecode(seqHeader, MEDIA_TYPE.video, dts, true, 0);
this.vps = null;
this.sps = null;
this.pps = null;
}
if (isNotHevcSeqHead(nalType)) {
if (!this.player._times.demuxStart) {
this.player._times.demuxStart = now$2();
}
const isIFrame = isHevcNaluIFrame(nalType);
const dts = this.getNaluDts();
const packet = hevcEncoderNalePacket(nalu, isIFrame);
this._preDoDecode(packet, MEDIA_TYPE.video, dts, isIFrame, 0);
}
}
}
handleVideoH265NaluList(naluList, isIFrame, naluType) {
if (this.isSendSeqHeader) {
if (!this.player._times.demuxStart) {
this.player._times.demuxStart = now$2();
}
const dts = this.getNaluDts();
const newNalu = naluList.reduce((pre, cur) => {
const nalu2 = addNaleHeaderLength(pre);
const nalu3 = addNaleHeaderLength(cur);
const nalu4 = new Uint8Array(nalu2.byteLength + nalu3.byteLength);
nalu4.set(nalu2, 0);
nalu4.set(nalu3, nalu2.byteLength);
return nalu4;
});
const packet = hevcEncoderNalePacketNotLength(newNalu, isIFrame);
this._preDoDecode(packet, MEDIA_TYPE.video, dts, isIFrame, 0);
} else {
this.player.debug.warn(this.TAG_NAME, `handleVideoH265NaluList isSendSeqHeader is false`);
}
}
_preDoDecode(packet, type, dts, isIFrame, cts) {
this.player.updateStats({
vbps: packet.byteLength,
dts: dts
});
if (isIFrame) {
this.calcIframeIntervalTimestamp(dts);
}
this._doDecode(packet, MEDIA_TYPE.video, dts, isIFrame, cts);
}
getInputByteLength() {
let result = 0;
if (this.lastBuf) {
result = this.lastBuf.byteLength;
}
return result;
}
}
class EmptyLoader extends CommonLoader {
constructor(player) {
super(player);
this.player = player;
player.debug.log('EmptyDemux', 'init');
}
destroy() {
super.destroy();
this.player.debug.log('EmptyDemux', 'destroy');
}
}
var mp4box = createCommonjsModule(function (module, exports) {
// file:src/log.js
/*
* Copyright (c) 2012-2013. Telecom ParisTech/TSI/MM/GPAC Cyril Concolato
* License: BSD-3-Clause (see LICENSE file)
*/
var Log = function () {
var start = new Date();
var LOG_LEVEL_ERROR = 4;
var LOG_LEVEL_WARNING = 3;
var LOG_LEVEL_INFO = 2;
var LOG_LEVEL_DEBUG = 1;
var log_level = LOG_LEVEL_ERROR;
var logObject = {
setLogLevel: function (level) {
if (level == this.debug) log_level = LOG_LEVEL_DEBUG;else if (level == this.info) log_level = LOG_LEVEL_INFO;else if (level == this.warn) log_level = LOG_LEVEL_WARNING;else if (level == this.error) log_level = LOG_LEVEL_ERROR;else log_level = LOG_LEVEL_ERROR;
},
debug: function (module, msg) {
if (console.debug === undefined) {
console.debug = console.log;
}
if (LOG_LEVEL_DEBUG >= log_level) {
console.debug("[" + Log.getDurationString(new Date() - start, 1000) + "]", "[" + module + "]", msg);
}
},
log: function (module, msg) {
this.debug(module.msg);
},
info: function (module, msg) {
if (LOG_LEVEL_INFO >= log_level) {
console.info("[" + Log.getDurationString(new Date() - start, 1000) + "]", "[" + module + "]", msg);
}
},
warn: function (module, msg) {
if (LOG_LEVEL_WARNING >= log_level) {
console.warn("[" + Log.getDurationString(new Date() - start, 1000) + "]", "[" + module + "]", msg);
}
},
error: function (module, msg) {
if (LOG_LEVEL_ERROR >= log_level) {
console.error("[" + Log.getDurationString(new Date() - start, 1000) + "]", "[" + module + "]", msg);
}
}
};
return logObject;
}();
/* Helper function to print a duration value in the form H:MM:SS.MS */
Log.getDurationString = function (duration, _timescale) {
var neg;
/* Helper function to print a number on a fixed number of digits */
function pad(number, length) {
var str = '' + number;
var a = str.split('.');
while (a[0].length < length) {
a[0] = '0' + a[0];
}
return a.join('.');
}
if (duration < 0) {
neg = true;
duration = -duration;
} else {
neg = false;
}
var timescale = _timescale || 1;
var duration_sec = duration / timescale;
var hours = Math.floor(duration_sec / 3600);
duration_sec -= hours * 3600;
var minutes = Math.floor(duration_sec / 60);
duration_sec -= minutes * 60;
var msec = duration_sec * 1000;
duration_sec = Math.floor(duration_sec);
msec -= duration_sec * 1000;
msec = Math.floor(msec);
return (neg ? "-" : "") + hours + ":" + pad(minutes, 2) + ":" + pad(duration_sec, 2) + "." + pad(msec, 3);
};
/* Helper function to stringify HTML5 TimeRanges objects */
Log.printRanges = function (ranges) {
var length = ranges.length;
if (length > 0) {
var str = "";
for (var i = 0; i < length; i++) {
if (i > 0) str += ",";
str += "[" + Log.getDurationString(ranges.start(i)) + "," + Log.getDurationString(ranges.end(i)) + "]";
}
return str;
} else {
return "(empty)";
}
};
{
exports.Log = Log;
}
// file:src/stream.js
var MP4BoxStream = function (arrayBuffer) {
if (arrayBuffer instanceof ArrayBuffer) {
this.buffer = arrayBuffer;
this.dataview = new DataView(arrayBuffer);
} else {
throw "Needs an array buffer";
}
this.position = 0;
};
/*************************************************************************
Common API between MultiBufferStream and SimpleStream
*************************************************************************/
MP4BoxStream.prototype.getPosition = function () {
return this.position;
};
MP4BoxStream.prototype.getEndPosition = function () {
return this.buffer.byteLength;
};
MP4BoxStream.prototype.getLength = function () {
return this.buffer.byteLength;
};
MP4BoxStream.prototype.seek = function (pos) {
var npos = Math.max(0, Math.min(this.buffer.byteLength, pos));
this.position = isNaN(npos) || !isFinite(npos) ? 0 : npos;
return true;
};
MP4BoxStream.prototype.isEos = function () {
return this.getPosition() >= this.getEndPosition();
};
/*************************************************************************
Read methods, simimar to DataStream but simpler
*************************************************************************/
MP4BoxStream.prototype.readAnyInt = function (size, signed) {
var res = 0;
if (this.position + size <= this.buffer.byteLength) {
switch (size) {
case 1:
if (signed) {
res = this.dataview.getInt8(this.position);
} else {
res = this.dataview.getUint8(this.position);
}
break;
case 2:
if (signed) {
res = this.dataview.getInt16(this.position);
} else {
res = this.dataview.getUint16(this.position);
}
break;
case 3:
if (signed) {
throw "No method for reading signed 24 bits values";
} else {
res = this.dataview.getUint8(this.position) << 16;
res |= this.dataview.getUint8(this.position + 1) << 8;
res |= this.dataview.getUint8(this.position + 2);
}
break;
case 4:
if (signed) {
res = this.dataview.getInt32(this.position);
} else {
res = this.dataview.getUint32(this.position);
}
break;
case 8:
if (signed) {
throw "No method for reading signed 64 bits values";
} else {
res = this.dataview.getUint32(this.position) << 32;
res |= this.dataview.getUint32(this.position + 4);
}
break;
default:
throw "readInt method not implemented for size: " + size;
}
this.position += size;
return res;
} else {
throw "Not enough bytes in buffer";
}
};
MP4BoxStream.prototype.readUint8 = function () {
return this.readAnyInt(1, false);
};
MP4BoxStream.prototype.readUint16 = function () {
return this.readAnyInt(2, false);
};
MP4BoxStream.prototype.readUint24 = function () {
return this.readAnyInt(3, false);
};
MP4BoxStream.prototype.readUint32 = function () {
return this.readAnyInt(4, false);
};
MP4BoxStream.prototype.readUint64 = function () {
return this.readAnyInt(8, false);
};
MP4BoxStream.prototype.readString = function (length) {
if (this.position + length <= this.buffer.byteLength) {
var s = "";
for (var i = 0; i < length; i++) {
s += String.fromCharCode(this.readUint8());
}
return s;
} else {
throw "Not enough bytes in buffer";
}
};
MP4BoxStream.prototype.readCString = function () {
var arr = [];
while (true) {
var b = this.readUint8();
if (b !== 0) {
arr.push(b);
} else {
break;
}
}
return String.fromCharCode.apply(null, arr);
};
MP4BoxStream.prototype.readInt8 = function () {
return this.readAnyInt(1, true);
};
MP4BoxStream.prototype.readInt16 = function () {
return this.readAnyInt(2, true);
};
MP4BoxStream.prototype.readInt32 = function () {
return this.readAnyInt(4, true);
};
MP4BoxStream.prototype.readInt64 = function () {
return this.readAnyInt(8, false);
};
MP4BoxStream.prototype.readUint8Array = function (length) {
var arr = new Uint8Array(length);
for (var i = 0; i < length; i++) {
arr[i] = this.readUint8();
}
return arr;
};
MP4BoxStream.prototype.readInt16Array = function (length) {
var arr = new Int16Array(length);
for (var i = 0; i < length; i++) {
arr[i] = this.readInt16();
}
return arr;
};
MP4BoxStream.prototype.readUint16Array = function (length) {
var arr = new Int16Array(length);
for (var i = 0; i < length; i++) {
arr[i] = this.readUint16();
}
return arr;
};
MP4BoxStream.prototype.readUint32Array = function (length) {
var arr = new Uint32Array(length);
for (var i = 0; i < length; i++) {
arr[i] = this.readUint32();
}
return arr;
};
MP4BoxStream.prototype.readInt32Array = function (length) {
var arr = new Int32Array(length);
for (var i = 0; i < length; i++) {
arr[i] = this.readInt32();
}
return arr;
};
{
exports.MP4BoxStream = MP4BoxStream;
} // file:src/DataStream.js
/**
DataStream reads scalars, arrays and structs of data from an ArrayBuffer.
It's like a file-like DataView on steroids.
@param {ArrayBuffer} arrayBuffer ArrayBuffer to read from.
@param {?Number} byteOffset Offset from arrayBuffer beginning for the DataStream.
@param {?Boolean} endianness DataStream.BIG_ENDIAN or DataStream.LITTLE_ENDIAN (the default).
*/
var DataStream = function (arrayBuffer, byteOffset, endianness) {
this._byteOffset = byteOffset || 0;
if (arrayBuffer instanceof ArrayBuffer) {
this.buffer = arrayBuffer;
} else if (typeof arrayBuffer == "object") {
this.dataView = arrayBuffer;
if (byteOffset) {
this._byteOffset += byteOffset;
}
} else {
this.buffer = new ArrayBuffer(arrayBuffer || 0);
}
this.position = 0;
this.endianness = endianness == null ? DataStream.LITTLE_ENDIAN : endianness;
};
DataStream.prototype = {};
DataStream.prototype.getPosition = function () {
return this.position;
};
/**
Internal function to resize the DataStream buffer when required.
@param {number} extra Number of bytes to add to the buffer allocation.
@return {null}
*/
DataStream.prototype._realloc = function (extra) {
if (!this._dynamicSize) {
return;
}
var req = this._byteOffset + this.position + extra;
var blen = this._buffer.byteLength;
if (req <= blen) {
if (req > this._byteLength) {
this._byteLength = req;
}
return;
}
if (blen < 1) {
blen = 1;
}
while (req > blen) {
blen *= 2;
}
var buf = new ArrayBuffer(blen);
var src = new Uint8Array(this._buffer);
var dst = new Uint8Array(buf, 0, src.length);
dst.set(src);
this.buffer = buf;
this._byteLength = req;
};
/**
Internal function to trim the DataStream buffer when required.
Used for stripping out the extra bytes from the backing buffer when
the virtual byteLength is smaller than the buffer byteLength (happens after
growing the buffer with writes and not filling the extra space completely).
@return {null}
*/
DataStream.prototype._trimAlloc = function () {
if (this._byteLength == this._buffer.byteLength) {
return;
}
var buf = new ArrayBuffer(this._byteLength);
var dst = new Uint8Array(buf);
var src = new Uint8Array(this._buffer, 0, dst.length);
dst.set(src);
this.buffer = buf;
};
/**
Big-endian const to use as default endianness.
@type {boolean}
*/
DataStream.BIG_ENDIAN = false;
/**
Little-endian const to use as default endianness.
@type {boolean}
*/
DataStream.LITTLE_ENDIAN = true;
/**
Virtual byte length of the DataStream backing buffer.
Updated to be max of original buffer size and last written size.
If dynamicSize is false is set to buffer size.
@type {number}
*/
DataStream.prototype._byteLength = 0;
/**
Returns the byte length of the DataStream object.
@type {number}
*/
Object.defineProperty(DataStream.prototype, 'byteLength', {
get: function () {
return this._byteLength - this._byteOffset;
}
});
/**
Set/get the backing ArrayBuffer of the DataStream object.
The setter updates the DataView to point to the new buffer.
@type {Object}
*/
Object.defineProperty(DataStream.prototype, 'buffer', {
get: function () {
this._trimAlloc();
return this._buffer;
},
set: function (v) {
this._buffer = v;
this._dataView = new DataView(this._buffer, this._byteOffset);
this._byteLength = this._buffer.byteLength;
}
});
/**
Set/get the byteOffset of the DataStream object.
The setter updates the DataView to point to the new byteOffset.
@type {number}
*/
Object.defineProperty(DataStream.prototype, 'byteOffset', {
get: function () {
return this._byteOffset;
},
set: function (v) {
this._byteOffset = v;
this._dataView = new DataView(this._buffer, this._byteOffset);
this._byteLength = this._buffer.byteLength;
}
});
/**
Set/get the backing DataView of the DataStream object.
The setter updates the buffer and byteOffset to point to the DataView values.
@type {Object}
*/
Object.defineProperty(DataStream.prototype, 'dataView', {
get: function () {
return this._dataView;
},
set: function (v) {
this._byteOffset = v.byteOffset;
this._buffer = v.buffer;
this._dataView = new DataView(this._buffer, this._byteOffset);
this._byteLength = this._byteOffset + v.byteLength;
}
});
/**
Sets the DataStream read/write position to given position.
Clamps between 0 and DataStream length.
@param {number} pos Position to seek to.
@return {null}
*/
DataStream.prototype.seek = function (pos) {
var npos = Math.max(0, Math.min(this.byteLength, pos));
this.position = isNaN(npos) || !isFinite(npos) ? 0 : npos;
};
/**
Returns true if the DataStream seek pointer is at the end of buffer and
there's no more data to read.
@return {boolean} True if the seek pointer is at the end of the buffer.
*/
DataStream.prototype.isEof = function () {
return this.position >= this._byteLength;
};
/**
Maps a Uint8Array into the DataStream buffer.
Nice for quickly reading in data.
@param {number} length Number of elements to map.
@param {?boolean} e Endianness of the data to read.
@return {Object} Uint8Array to the DataStream backing buffer.
*/
DataStream.prototype.mapUint8Array = function (length) {
this._realloc(length * 1);
var arr = new Uint8Array(this._buffer, this.byteOffset + this.position, length);
this.position += length * 1;
return arr;
};
/**
Reads an Int32Array of desired length and endianness from the DataStream.
@param {number} length Number of elements to map.
@param {?boolean} e Endianness of the data to read.
@return {Object} The read Int32Array.
*/
DataStream.prototype.readInt32Array = function (length, e) {
length = length == null ? this.byteLength - this.position / 4 : length;
var arr = new Int32Array(length);
DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset + this.position, length * arr.BYTES_PER_ELEMENT);
DataStream.arrayToNative(arr, e == null ? this.endianness : e);
this.position += arr.byteLength;
return arr;
};
/**
Reads an Int16Array of desired length and endianness from the DataStream.
@param {number} length Number of elements to map.
@param {?boolean} e Endianness of the data to read.
@return {Object} The read Int16Array.
*/
DataStream.prototype.readInt16Array = function (length, e) {
length = length == null ? this.byteLength - this.position / 2 : length;
var arr = new Int16Array(length);
DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset + this.position, length * arr.BYTES_PER_ELEMENT);
DataStream.arrayToNative(arr, e == null ? this.endianness : e);
this.position += arr.byteLength;
return arr;
};
/**
Reads an Int8Array of desired length from the DataStream.
@param {number} length Number of elements to map.
@param {?boolean} e Endianness of the data to read.
@return {Object} The read Int8Array.
*/
DataStream.prototype.readInt8Array = function (length) {
length = length == null ? this.byteLength - this.position : length;
var arr = new Int8Array(length);
DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset + this.position, length * arr.BYTES_PER_ELEMENT);
this.position += arr.byteLength;
return arr;
};
/**
Reads a Uint32Array of desired length and endianness from the DataStream.
@param {number} length Number of elements to map.
@param {?boolean} e Endianness of the data to read.
@return {Object} The read Uint32Array.
*/
DataStream.prototype.readUint32Array = function (length, e) {
length = length == null ? this.byteLength - this.position / 4 : length;
var arr = new Uint32Array(length);
DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset + this.position, length * arr.BYTES_PER_ELEMENT);
DataStream.arrayToNative(arr, e == null ? this.endianness : e);
this.position += arr.byteLength;
return arr;
};
/**
Reads a Uint16Array of desired length and endianness from the DataStream.
@param {number} length Number of elements to map.
@param {?boolean} e Endianness of the data to read.
@return {Object} The read Uint16Array.
*/
DataStream.prototype.readUint16Array = function (length, e) {
length = length == null ? this.byteLength - this.position / 2 : length;
var arr = new Uint16Array(length);
DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset + this.position, length * arr.BYTES_PER_ELEMENT);
DataStream.arrayToNative(arr, e == null ? this.endianness : e);
this.position += arr.byteLength;
return arr;
};
/**
Reads a Uint8Array of desired length from the DataStream.
@param {number} length Number of elements to map.
@param {?boolean} e Endianness of the data to read.
@return {Object} The read Uint8Array.
*/
DataStream.prototype.readUint8Array = function (length) {
length = length == null ? this.byteLength - this.position : length;
var arr = new Uint8Array(length);
DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset + this.position, length * arr.BYTES_PER_ELEMENT);
this.position += arr.byteLength;
return arr;
};
/**
Reads a Float64Array of desired length and endianness from the DataStream.
@param {number} length Number of elements to map.
@param {?boolean} e Endianness of the data to read.
@return {Object} The read Float64Array.
*/
DataStream.prototype.readFloat64Array = function (length, e) {
length = length == null ? this.byteLength - this.position / 8 : length;
var arr = new Float64Array(length);
DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset + this.position, length * arr.BYTES_PER_ELEMENT);
DataStream.arrayToNative(arr, e == null ? this.endianness : e);
this.position += arr.byteLength;
return arr;
};
/**
Reads a Float32Array of desired length and endianness from the DataStream.
@param {number} length Number of elements to map.
@param {?boolean} e Endianness of the data to read.
@return {Object} The read Float32Array.
*/
DataStream.prototype.readFloat32Array = function (length, e) {
length = length == null ? this.byteLength - this.position / 4 : length;
var arr = new Float32Array(length);
DataStream.memcpy(arr.buffer, 0, this.buffer, this.byteOffset + this.position, length * arr.BYTES_PER_ELEMENT);
DataStream.arrayToNative(arr, e == null ? this.endianness : e);
this.position += arr.byteLength;
return arr;
};
/**
Reads a 32-bit int from the DataStream with the desired endianness.
@param {?boolean} e Endianness of the number.
@return {number} The read number.
*/
DataStream.prototype.readInt32 = function (e) {
var v = this._dataView.getInt32(this.position, e == null ? this.endianness : e);
this.position += 4;
return v;
};
/**
Reads a 16-bit int from the DataStream with the desired endianness.
@param {?boolean} e Endianness of the number.
@return {number} The read number.
*/
DataStream.prototype.readInt16 = function (e) {
var v = this._dataView.getInt16(this.position, e == null ? this.endianness : e);
this.position += 2;
return v;
};
/**
Reads an 8-bit int from the DataStream.
@return {number} The read number.
*/
DataStream.prototype.readInt8 = function () {
var v = this._dataView.getInt8(this.position);
this.position += 1;
return v;
};
/**
Reads a 32-bit unsigned int from the DataStream with the desired endianness.
@param {?boolean} e Endianness of the number.
@return {number} The read number.
*/
DataStream.prototype.readUint32 = function (e) {
var v = this._dataView.getUint32(this.position, e == null ? this.endianness : e);
this.position += 4;
return v;
};
/**
Reads a 16-bit unsigned int from the DataStream with the desired endianness.
@param {?boolean} e Endianness of the number.
@return {number} The read number.
*/
DataStream.prototype.readUint16 = function (e) {
var v = this._dataView.getUint16(this.position, e == null ? this.endianness : e);
this.position += 2;
return v;
};
/**
Reads an 8-bit unsigned int from the DataStream.
@return {number} The read number.
*/
DataStream.prototype.readUint8 = function () {
var v = this._dataView.getUint8(this.position);
this.position += 1;
return v;
};
/**
Reads a 32-bit float from the DataStream with the desired endianness.
@param {?boolean} e Endianness of the number.
@return {number} The read number.
*/
DataStream.prototype.readFloat32 = function (e) {
var v = this._dataView.getFloat32(this.position, e == null ? this.endianness : e);
this.position += 4;
return v;
};
/**
Reads a 64-bit float from the DataStream with the desired endianness.
@param {?boolean} e Endianness of the number.
@return {number} The read number.
*/
DataStream.prototype.readFloat64 = function (e) {
var v = this._dataView.getFloat64(this.position, e == null ? this.endianness : e);
this.position += 8;
return v;
};
/**
Native endianness. Either DataStream.BIG_ENDIAN or DataStream.LITTLE_ENDIAN
depending on the platform endianness.
@type {boolean}
*/
DataStream.endianness = new Int8Array(new Int16Array([1]).buffer)[0] > 0;
/**
Copies byteLength bytes from the src buffer at srcOffset to the
dst buffer at dstOffset.
@param {Object} dst Destination ArrayBuffer to write to.
@param {number} dstOffset Offset to the destination ArrayBuffer.
@param {Object} src Source ArrayBuffer to read from.
@param {number} srcOffset Offset to the source ArrayBuffer.
@param {number} byteLength Number of bytes to copy.
*/
DataStream.memcpy = function (dst, dstOffset, src, srcOffset, byteLength) {
var dstU8 = new Uint8Array(dst, dstOffset, byteLength);
var srcU8 = new Uint8Array(src, srcOffset, byteLength);
dstU8.set(srcU8);
};
/**
Converts array to native endianness in-place.
@param {Object} array Typed array to convert.
@param {boolean} arrayIsLittleEndian True if the data in the array is
little-endian. Set false for big-endian.
@return {Object} The converted typed array.
*/
DataStream.arrayToNative = function (array, arrayIsLittleEndian) {
if (arrayIsLittleEndian == this.endianness) {
return array;
} else {
return this.flipArrayEndianness(array);
}
};
/**
Converts native endianness array to desired endianness in-place.
@param {Object} array Typed array to convert.
@param {boolean} littleEndian True if the converted array should be
little-endian. Set false for big-endian.
@return {Object} The converted typed array.
*/
DataStream.nativeToEndian = function (array, littleEndian) {
if (this.endianness == littleEndian) {
return array;
} else {
return this.flipArrayEndianness(array);
}
};
/**
Flips typed array endianness in-place.
@param {Object} array Typed array to flip.
@return {Object} The converted typed array.
*/
DataStream.flipArrayEndianness = function (array) {
var u8 = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
for (var i = 0; i < array.byteLength; i += array.BYTES_PER_ELEMENT) {
for (var j = i + array.BYTES_PER_ELEMENT - 1, k = i; j > k; j--, k++) {
var tmp = u8[k];
u8[k] = u8[j];
u8[j] = tmp;
}
}
return array;
};
/**
Seek position where DataStream#readStruct ran into a problem.
Useful for debugging struct parsing.
@type {number}
*/
DataStream.prototype.failurePosition = 0;
String.fromCharCodeUint8 = function (uint8arr) {
var arr = [];
for (var i = 0; i < uint8arr.length; i++) {
arr[i] = uint8arr[i];
}
return String.fromCharCode.apply(null, arr);
};
/**
Read a string of desired length and encoding from the DataStream.
@param {number} length The length of the string to read in bytes.
@param {?string} encoding The encoding of the string data in the DataStream.
Defaults to ASCII.
@return {string} The read string.
*/
DataStream.prototype.readString = function (length, encoding) {
if (encoding == null || encoding == "ASCII") {
return String.fromCharCodeUint8.apply(null, [this.mapUint8Array(length == null ? this.byteLength - this.position : length)]);
} else {
return new TextDecoder(encoding).decode(this.mapUint8Array(length));
}
};
/**
Read null-terminated string of desired length from the DataStream. Truncates
the returned string so that the null byte is not a part of it.
@param {?number} length The length of the string to read.
@return {string} The read string.
*/
DataStream.prototype.readCString = function (length) {
var blen = this.byteLength - this.position;
var u8 = new Uint8Array(this._buffer, this._byteOffset + this.position);
var len = blen;
if (length != null) {
len = Math.min(length, blen);
}
for (var i = 0; i < len && u8[i] !== 0; i++); // find first zero byte
var s = String.fromCharCodeUint8.apply(null, [this.mapUint8Array(i)]);
if (length != null) {
this.position += len - i;
} else if (i != blen) {
this.position += 1; // trailing zero if not at end of buffer
}
return s;
};
/*
TODO: fix endianness for 24/64-bit fields
TODO: check range/support for 64-bits numbers in JavaScript
*/
var MAX_SIZE = Math.pow(2, 32);
DataStream.prototype.readInt64 = function () {
return this.readInt32() * MAX_SIZE + this.readUint32();
};
DataStream.prototype.readUint64 = function () {
return this.readUint32() * MAX_SIZE + this.readUint32();
};
DataStream.prototype.readInt64 = function () {
return this.readUint32() * MAX_SIZE + this.readUint32();
};
DataStream.prototype.readUint24 = function () {
return (this.readUint8() << 16) + (this.readUint8() << 8) + this.readUint8();
};
{
exports.DataStream = DataStream;
}
// file:src/DataStream-write.js
/**
Saves the DataStream contents to the given filename.
Uses Chrome's anchor download property to initiate download.
@param {string} filename Filename to save as.
@return {null}
*/
DataStream.prototype.save = function (filename) {
var blob = new Blob([this.buffer]);
if (window.URL && URL.createObjectURL) {
var url = window.URL.createObjectURL(blob);
var a = document.createElement('a');
// Required in Firefox:
document.body.appendChild(a);
a.setAttribute('href', url);
a.setAttribute('download', filename);
// Required in Firefox:
a.setAttribute('target', '_self');
a.click();
window.URL.revokeObjectURL(url);
} else {
throw "DataStream.save: Can't create object URL.";
}
};
/**
Whether to extend DataStream buffer when trying to write beyond its size.
If set, the buffer is reallocated to twice its current size until the
requested write fits the buffer.
@type {boolean}
*/
DataStream.prototype._dynamicSize = true;
Object.defineProperty(DataStream.prototype, 'dynamicSize', {
get: function () {
return this._dynamicSize;
},
set: function (v) {
if (!v) {
this._trimAlloc();
}
this._dynamicSize = v;
}
});
/**
Internal function to trim the DataStream buffer when required.
Used for stripping out the first bytes when not needed anymore.
@return {null}
*/
DataStream.prototype.shift = function (offset) {
var buf = new ArrayBuffer(this._byteLength - offset);
var dst = new Uint8Array(buf);
var src = new Uint8Array(this._buffer, offset, dst.length);
dst.set(src);
this.buffer = buf;
this.position -= offset;
};
/**
Writes an Int32Array of specified endianness to the DataStream.
@param {Object} arr The array to write.
@param {?boolean} e Endianness of the data to write.
*/
DataStream.prototype.writeInt32Array = function (arr, e) {
this._realloc(arr.length * 4);
if (arr instanceof Int32Array && this.byteOffset + this.position % arr.BYTES_PER_ELEMENT === 0) {
DataStream.memcpy(this._buffer, this.byteOffset + this.position, arr.buffer, 0, arr.byteLength);
this.mapInt32Array(arr.length, e);
} else {
for (var i = 0; i < arr.length; i++) {
this.writeInt32(arr[i], e);
}
}
};
/**
Writes an Int16Array of specified endianness to the DataStream.
@param {Object} arr The array to write.
@param {?boolean} e Endianness of the data to write.
*/
DataStream.prototype.writeInt16Array = function (arr, e) {
this._realloc(arr.length * 2);
if (arr instanceof Int16Array && this.byteOffset + this.position % arr.BYTES_PER_ELEMENT === 0) {
DataStream.memcpy(this._buffer, this.byteOffset + this.position, arr.buffer, 0, arr.byteLength);
this.mapInt16Array(arr.length, e);
} else {
for (var i = 0; i < arr.length; i++) {
this.writeInt16(arr[i], e);
}
}
};
/**
Writes an Int8Array to the DataStream.
@param {Object} arr The array to write.
*/
DataStream.prototype.writeInt8Array = function (arr) {
this._realloc(arr.length * 1);
if (arr instanceof Int8Array && this.byteOffset + this.position % arr.BYTES_PER_ELEMENT === 0) {
DataStream.memcpy(this._buffer, this.byteOffset + this.position, arr.buffer, 0, arr.byteLength);
this.mapInt8Array(arr.length);
} else {
for (var i = 0; i < arr.length; i++) {
this.writeInt8(arr[i]);
}
}
};
/**
Writes a Uint32Array of specified endianness to the DataStream.
@param {Object} arr The array to write.
@param {?boolean} e Endianness of the data to write.
*/
DataStream.prototype.writeUint32Array = function (arr, e) {
this._realloc(arr.length * 4);
if (arr instanceof Uint32Array && this.byteOffset + this.position % arr.BYTES_PER_ELEMENT === 0) {
DataStream.memcpy(this._buffer, this.byteOffset + this.position, arr.buffer, 0, arr.byteLength);
this.mapUint32Array(arr.length, e);
} else {
for (var i = 0; i < arr.length; i++) {
this.writeUint32(arr[i], e);
}
}
};
/**
Writes a Uint16Array of specified endianness to the DataStream.
@param {Object} arr The array to write.
@param {?boolean} e Endianness of the data to write.
*/
DataStream.prototype.writeUint16Array = function (arr, e) {
this._realloc(arr.length * 2);
if (arr instanceof Uint16Array && this.byteOffset + this.position % arr.BYTES_PER_ELEMENT === 0) {
DataStream.memcpy(this._buffer, this.byteOffset + this.position, arr.buffer, 0, arr.byteLength);
this.mapUint16Array(arr.length, e);
} else {
for (var i = 0; i < arr.length; i++) {
this.writeUint16(arr[i], e);
}
}
};
/**
Writes a Uint8Array to the DataStream.
@param {Object} arr The array to write.
*/
DataStream.prototype.writeUint8Array = function (arr) {
this._realloc(arr.length * 1);
if (arr instanceof Uint8Array && this.byteOffset + this.position % arr.BYTES_PER_ELEMENT === 0) {
DataStream.memcpy(this._buffer, this.byteOffset + this.position, arr.buffer, 0, arr.byteLength);
this.mapUint8Array(arr.length);
} else {
for (var i = 0; i < arr.length; i++) {
this.writeUint8(arr[i]);
}
}
};
/**
Writes a Float64Array of specified endianness to the DataStream.
@param {Object} arr The array to write.
@param {?boolean} e Endianness of the data to write.
*/
DataStream.prototype.writeFloat64Array = function (arr, e) {
this._realloc(arr.length * 8);
if (arr instanceof Float64Array && this.byteOffset + this.position % arr.BYTES_PER_ELEMENT === 0) {
DataStream.memcpy(this._buffer, this.byteOffset + this.position, arr.buffer, 0, arr.byteLength);
this.mapFloat64Array(arr.length, e);
} else {
for (var i = 0; i < arr.length; i++) {
this.writeFloat64(arr[i], e);
}
}
};
/**
Writes a Float32Array of specified endianness to the DataStream.
@param {Object} arr The array to write.
@param {?boolean} e Endianness of the data to write.
*/
DataStream.prototype.writeFloat32Array = function (arr, e) {
this._realloc(arr.length * 4);
if (arr instanceof Float32Array && this.byteOffset + this.position % arr.BYTES_PER_ELEMENT === 0) {
DataStream.memcpy(this._buffer, this.byteOffset + this.position, arr.buffer, 0, arr.byteLength);
this.mapFloat32Array(arr.length, e);
} else {
for (var i = 0; i < arr.length; i++) {
this.writeFloat32(arr[i], e);
}
}
};
/**
Writes a 32-bit int to the DataStream with the desired endianness.
@param {number} v Number to write.
@param {?boolean} e Endianness of the number.
*/
DataStream.prototype.writeInt32 = function (v, e) {
this._realloc(4);
this._dataView.setInt32(this.position, v, e == null ? this.endianness : e);
this.position += 4;
};
/**
Writes a 16-bit int to the DataStream with the desired endianness.
@param {number} v Number to write.
@param {?boolean} e Endianness of the number.
*/
DataStream.prototype.writeInt16 = function (v, e) {
this._realloc(2);
this._dataView.setInt16(this.position, v, e == null ? this.endianness : e);
this.position += 2;
};
/**
Writes an 8-bit int to the DataStream.
@param {number} v Number to write.
*/
DataStream.prototype.writeInt8 = function (v) {
this._realloc(1);
this._dataView.setInt8(this.position, v);
this.position += 1;
};
/**
Writes a 32-bit unsigned int to the DataStream with the desired endianness.
@param {number} v Number to write.
@param {?boolean} e Endianness of the number.
*/
DataStream.prototype.writeUint32 = function (v, e) {
this._realloc(4);
this._dataView.setUint32(this.position, v, e == null ? this.endianness : e);
this.position += 4;
};
/**
Writes a 16-bit unsigned int to the DataStream with the desired endianness.
@param {number} v Number to write.
@param {?boolean} e Endianness of the number.
*/
DataStream.prototype.writeUint16 = function (v, e) {
this._realloc(2);
this._dataView.setUint16(this.position, v, e == null ? this.endianness : e);
this.position += 2;
};
/**
Writes an 8-bit unsigned int to the DataStream.
@param {number} v Number to write.
*/
DataStream.prototype.writeUint8 = function (v) {
this._realloc(1);
this._dataView.setUint8(this.position, v);
this.position += 1;
};
/**
Writes a 32-bit float to the DataStream with the desired endianness.
@param {number} v Number to write.
@param {?boolean} e Endianness of the number.
*/
DataStream.prototype.writeFloat32 = function (v, e) {
this._realloc(4);
this._dataView.setFloat32(this.position, v, e == null ? this.endianness : e);
this.position += 4;
};
/**
Writes a 64-bit float to the DataStream with the desired endianness.
@param {number} v Number to write.
@param {?boolean} e Endianness of the number.
*/
DataStream.prototype.writeFloat64 = function (v, e) {
this._realloc(8);
this._dataView.setFloat64(this.position, v, e == null ? this.endianness : e);
this.position += 8;
};
/**
Write a UCS-2 string of desired endianness to the DataStream. The
lengthOverride argument lets you define the number of characters to write.
If the string is shorter than lengthOverride, the extra space is padded with
zeroes.
@param {string} str The string to write.
@param {?boolean} endianness The endianness to use for the written string data.
@param {?number} lengthOverride The number of characters to write.
*/
DataStream.prototype.writeUCS2String = function (str, endianness, lengthOverride) {
if (lengthOverride == null) {
lengthOverride = str.length;
}
for (var i = 0; i < str.length && i < lengthOverride; i++) {
this.writeUint16(str.charCodeAt(i), endianness);
}
for (; i < lengthOverride; i++) {
this.writeUint16(0);
}
};
/**
Writes a string of desired length and encoding to the DataStream.
@param {string} s The string to write.
@param {?string} encoding The encoding for the written string data.
Defaults to ASCII.
@param {?number} length The number of characters to write.
*/
DataStream.prototype.writeString = function (s, encoding, length) {
var i = 0;
if (encoding == null || encoding == "ASCII") {
if (length != null) {
var len = Math.min(s.length, length);
for (i = 0; i < len; i++) {
this.writeUint8(s.charCodeAt(i));
}
for (; i < length; i++) {
this.writeUint8(0);
}
} else {
for (i = 0; i < s.length; i++) {
this.writeUint8(s.charCodeAt(i));
}
}
} else {
this.writeUint8Array(new TextEncoder(encoding).encode(s.substring(0, length)));
}
};
/**
Writes a null-terminated string to DataStream and zero-pads it to length
bytes. If length is not given, writes the string followed by a zero.
If string is longer than length, the written part of the string does not have
a trailing zero.
@param {string} s The string to write.
@param {?number} length The number of characters to write.
*/
DataStream.prototype.writeCString = function (s, length) {
var i = 0;
if (length != null) {
var len = Math.min(s.length, length);
for (i = 0; i < len; i++) {
this.writeUint8(s.charCodeAt(i));
}
for (; i < length; i++) {
this.writeUint8(0);
}
} else {
for (i = 0; i < s.length; i++) {
this.writeUint8(s.charCodeAt(i));
}
this.writeUint8(0);
}
};
/**
Writes a struct to the DataStream. Takes a structDefinition that gives the
types and a struct object that gives the values. Refer to readStruct for the
structure of structDefinition.
@param {Object} structDefinition Type definition of the struct.
@param {Object} struct The struct data object.
*/
DataStream.prototype.writeStruct = function (structDefinition, struct) {
for (var i = 0; i < structDefinition.length; i += 2) {
var t = structDefinition[i + 1];
this.writeType(t, struct[structDefinition[i]], struct);
}
};
/**
Writes object v of type t to the DataStream.
@param {Object} t Type of data to write.
@param {Object} v Value of data to write.
@param {Object} struct Struct to pass to write callback functions.
*/
DataStream.prototype.writeType = function (t, v, struct) {
var tp;
if (typeof t == "function") {
return t(this, v);
} else if (typeof t == "object" && !(t instanceof Array)) {
return t.set(this, v, struct);
}
var lengthOverride = null;
var charset = "ASCII";
var pos = this.position;
if (typeof t == 'string' && /:/.test(t)) {
tp = t.split(":");
t = tp[0];
lengthOverride = parseInt(tp[1]);
}
if (typeof t == 'string' && /,/.test(t)) {
tp = t.split(",");
t = tp[0];
charset = parseInt(tp[1]);
}
switch (t) {
case 'uint8':
this.writeUint8(v);
break;
case 'int8':
this.writeInt8(v);
break;
case 'uint16':
this.writeUint16(v, this.endianness);
break;
case 'int16':
this.writeInt16(v, this.endianness);
break;
case 'uint32':
this.writeUint32(v, this.endianness);
break;
case 'int32':
this.writeInt32(v, this.endianness);
break;
case 'float32':
this.writeFloat32(v, this.endianness);
break;
case 'float64':
this.writeFloat64(v, this.endianness);
break;
case 'uint16be':
this.writeUint16(v, DataStream.BIG_ENDIAN);
break;
case 'int16be':
this.writeInt16(v, DataStream.BIG_ENDIAN);
break;
case 'uint32be':
this.writeUint32(v, DataStream.BIG_ENDIAN);
break;
case 'int32be':
this.writeInt32(v, DataStream.BIG_ENDIAN);
break;
case 'float32be':
this.writeFloat32(v, DataStream.BIG_ENDIAN);
break;
case 'float64be':
this.writeFloat64(v, DataStream.BIG_ENDIAN);
break;
case 'uint16le':
this.writeUint16(v, DataStream.LITTLE_ENDIAN);
break;
case 'int16le':
this.writeInt16(v, DataStream.LITTLE_ENDIAN);
break;
case 'uint32le':
this.writeUint32(v, DataStream.LITTLE_ENDIAN);
break;
case 'int32le':
this.writeInt32(v, DataStream.LITTLE_ENDIAN);
break;
case 'float32le':
this.writeFloat32(v, DataStream.LITTLE_ENDIAN);
break;
case 'float64le':
this.writeFloat64(v, DataStream.LITTLE_ENDIAN);
break;
case 'cstring':
this.writeCString(v, lengthOverride);
break;
case 'string':
this.writeString(v, charset, lengthOverride);
break;
case 'u16string':
this.writeUCS2String(v, this.endianness, lengthOverride);
break;
case 'u16stringle':
this.writeUCS2String(v, DataStream.LITTLE_ENDIAN, lengthOverride);
break;
case 'u16stringbe':
this.writeUCS2String(v, DataStream.BIG_ENDIAN, lengthOverride);
break;
default:
if (t.length == 3) {
var ta = t[1];
for (var i = 0; i < v.length; i++) {
this.writeType(ta, v[i]);
}
break;
} else {
this.writeStruct(t, v);
break;
}
}
if (lengthOverride != null) {
this.position = pos;
this._realloc(lengthOverride);
this.position = pos + lengthOverride;
}
};
DataStream.prototype.writeUint64 = function (v) {
var h = Math.floor(v / MAX_SIZE);
this.writeUint32(h);
this.writeUint32(v & 0xFFFFFFFF);
};
DataStream.prototype.writeUint24 = function (v) {
this.writeUint8((v & 0x00FF0000) >> 16);
this.writeUint8((v & 0x0000FF00) >> 8);
this.writeUint8(v & 0x000000FF);
};
DataStream.prototype.adjustUint32 = function (position, value) {
var pos = this.position;
this.seek(position);
this.writeUint32(value);
this.seek(pos);
};
// file:src/DataStream-map.js
/**
Maps an Int32Array into the DataStream buffer, swizzling it to native
endianness in-place. The current offset from the start of the buffer needs to
be a multiple of element size, just like with typed array views.
Nice for quickly reading in data. Warning: potentially modifies the buffer
contents.
@param {number} length Number of elements to map.
@param {?boolean} e Endianness of the data to read.
@return {Object} Int32Array to the DataStream backing buffer.
*/
DataStream.prototype.mapInt32Array = function (length, e) {
this._realloc(length * 4);
var arr = new Int32Array(this._buffer, this.byteOffset + this.position, length);
DataStream.arrayToNative(arr, e == null ? this.endianness : e);
this.position += length * 4;
return arr;
};
/**
Maps an Int16Array into the DataStream buffer, swizzling it to native
endianness in-place. The current offset from the start of the buffer needs to
be a multiple of element size, just like with typed array views.
Nice for quickly reading in data. Warning: potentially modifies the buffer
contents.
@param {number} length Number of elements to map.
@param {?boolean} e Endianness of the data to read.
@return {Object} Int16Array to the DataStream backing buffer.
*/
DataStream.prototype.mapInt16Array = function (length, e) {
this._realloc(length * 2);
var arr = new Int16Array(this._buffer, this.byteOffset + this.position, length);
DataStream.arrayToNative(arr, e == null ? this.endianness : e);
this.position += length * 2;
return arr;
};
/**
Maps an Int8Array into the DataStream buffer.
Nice for quickly reading in data.
@param {number} length Number of elements to map.
@param {?boolean} e Endianness of the data to read.
@return {Object} Int8Array to the DataStream backing buffer.
*/
DataStream.prototype.mapInt8Array = function (length) {
this._realloc(length * 1);
var arr = new Int8Array(this._buffer, this.byteOffset + this.position, length);
this.position += length * 1;
return arr;
};
/**
Maps a Uint32Array into the DataStream buffer, swizzling it to native
endianness in-place. The current offset from the start of the buffer needs to
be a multiple of element size, just like with typed array views.
Nice for quickly reading in data. Warning: potentially modifies the buffer
contents.
@param {number} length Number of elements to map.
@param {?boolean} e Endianness of the data to read.
@return {Object} Uint32Array to the DataStream backing buffer.
*/
DataStream.prototype.mapUint32Array = function (length, e) {
this._realloc(length * 4);
var arr = new Uint32Array(this._buffer, this.byteOffset + this.position, length);
DataStream.arrayToNative(arr, e == null ? this.endianness : e);
this.position += length * 4;
return arr;
};
/**
Maps a Uint16Array into the DataStream buffer, swizzling it to native
endianness in-place. The current offset from the start of the buffer needs to
be a multiple of element size, just like with typed array views.
Nice for quickly reading in data. Warning: potentially modifies the buffer
contents.
@param {number} length Number of elements to map.
@param {?boolean} e Endianness of the data to read.
@return {Object} Uint16Array to the DataStream backing buffer.
*/
DataStream.prototype.mapUint16Array = function (length, e) {
this._realloc(length * 2);
var arr = new Uint16Array(this._buffer, this.byteOffset + this.position, length);
DataStream.arrayToNative(arr, e == null ? this.endianness : e);
this.position += length * 2;
return arr;
};
/**
Maps a Float64Array into the DataStream buffer, swizzling it to native
endianness in-place. The current offset from the start of the buffer needs to
be a multiple of element size, just like with typed array views.
Nice for quickly reading in data. Warning: potentially modifies the buffer
contents.
@param {number} length Number of elements to map.
@param {?boolean} e Endianness of the data to read.
@return {Object} Float64Array to the DataStream backing buffer.
*/
DataStream.prototype.mapFloat64Array = function (length, e) {
this._realloc(length * 8);
var arr = new Float64Array(this._buffer, this.byteOffset + this.position, length);
DataStream.arrayToNative(arr, e == null ? this.endianness : e);
this.position += length * 8;
return arr;
};
/**
Maps a Float32Array into the DataStream buffer, swizzling it to native
endianness in-place. The current offset from the start of the buffer needs to
be a multiple of element size, just like with typed array views.
Nice for quickly reading in data. Warning: potentially modifies the buffer
contents.
@param {number} length Number of elements to map.
@param {?boolean} e Endianness of the data to read.
@return {Object} Float32Array to the DataStream backing buffer.
*/
DataStream.prototype.mapFloat32Array = function (length, e) {
this._realloc(length * 4);
var arr = new Float32Array(this._buffer, this.byteOffset + this.position, length);
DataStream.arrayToNative(arr, e == null ? this.endianness : e);
this.position += length * 4;
return arr;
};
// file:src/buffer.js
/**
* MultiBufferStream is a class that acts as a SimpleStream for parsing
* It holds several, possibly non-contiguous ArrayBuffer objects, each with a fileStart property
* containing the offset for the buffer data in an original/virtual file
*
* It inherits also from DataStream for all read/write/alloc operations
*/
/**
* Constructor
*/
var MultiBufferStream = function (buffer) {
/* List of ArrayBuffers, with a fileStart property, sorted in fileStart order and non overlapping */
this.buffers = [];
this.bufferIndex = -1;
if (buffer) {
this.insertBuffer(buffer);
this.bufferIndex = 0;
}
};
MultiBufferStream.prototype = new DataStream(new ArrayBuffer(), 0, DataStream.BIG_ENDIAN);
/************************************************************************************
Methods for the managnement of the buffers (insertion, removal, concatenation, ...)
***********************************************************************************/
MultiBufferStream.prototype.initialized = function () {
var firstBuffer;
if (this.bufferIndex > -1) {
return true;
} else if (this.buffers.length > 0) {
firstBuffer = this.buffers[0];
if (firstBuffer.fileStart === 0) {
this.buffer = firstBuffer;
this.bufferIndex = 0;
Log.debug("MultiBufferStream", "Stream ready for parsing");
return true;
} else {
Log.warn("MultiBufferStream", "The first buffer should have a fileStart of 0");
this.logBufferLevel();
return false;
}
} else {
Log.warn("MultiBufferStream", "No buffer to start parsing from");
this.logBufferLevel();
return false;
}
};
/**
* helper functions to concatenate two ArrayBuffer objects
* @param {ArrayBuffer} buffer1
* @param {ArrayBuffer} buffer2
* @return {ArrayBuffer} the concatenation of buffer1 and buffer2 in that order
*/
ArrayBuffer.concat = function (buffer1, buffer2) {
Log.debug("ArrayBuffer", "Trying to create a new buffer of size: " + (buffer1.byteLength + buffer2.byteLength));
var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
tmp.set(new Uint8Array(buffer1), 0);
tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
return tmp.buffer;
};
/**
* Reduces the size of a given buffer, but taking the part between offset and offset+newlength
* @param {ArrayBuffer} buffer
* @param {Number} offset the start of new buffer
* @param {Number} newLength the length of the new buffer
* @return {ArrayBuffer} the new buffer
*/
MultiBufferStream.prototype.reduceBuffer = function (buffer, offset, newLength) {
var smallB;
smallB = new Uint8Array(newLength);
smallB.set(new Uint8Array(buffer, offset, newLength));
smallB.buffer.fileStart = buffer.fileStart + offset;
smallB.buffer.usedBytes = 0;
return smallB.buffer;
};
/**
* Inserts the new buffer in the sorted list of buffers,
* making sure, it is not overlapping with existing ones (possibly reducing its size).
* if the new buffer overrides/replaces the 0-th buffer (for instance because it is bigger),
* updates the DataStream buffer for parsing
*/
MultiBufferStream.prototype.insertBuffer = function (ab) {
var to_add = true;
/* TODO: improve insertion if many buffers */
for (var i = 0; i < this.buffers.length; i++) {
var b = this.buffers[i];
if (ab.fileStart <= b.fileStart) {
/* the insertion position is found */
if (ab.fileStart === b.fileStart) {
/* The new buffer overlaps with an existing buffer */
if (ab.byteLength > b.byteLength) {
/* the new buffer is bigger than the existing one
remove the existing buffer and try again to insert
the new buffer to check overlap with the next ones */
this.buffers.splice(i, 1);
i--;
continue;
} else {
/* the new buffer is smaller than the existing one, just drop it */
Log.warn("MultiBufferStream", "Buffer (fileStart: " + ab.fileStart + " - Length: " + ab.byteLength + ") already appended, ignoring");
}
} else {
/* The beginning of the new buffer is not overlapping with an existing buffer
let's check the end of it */
if (ab.fileStart + ab.byteLength <= b.fileStart) ; else {
/* There is some overlap, cut the new buffer short, and add it*/
ab = this.reduceBuffer(ab, 0, b.fileStart - ab.fileStart);
}
Log.debug("MultiBufferStream", "Appending new buffer (fileStart: " + ab.fileStart + " - Length: " + ab.byteLength + ")");
this.buffers.splice(i, 0, ab);
/* if this new buffer is inserted in the first place in the list of the buffer,
and the DataStream is initialized, make it the buffer used for parsing */
if (i === 0) {
this.buffer = ab;
}
}
to_add = false;
break;
} else if (ab.fileStart < b.fileStart + b.byteLength) {
/* the new buffer overlaps its beginning with the end of the current buffer */
var offset = b.fileStart + b.byteLength - ab.fileStart;
var newLength = ab.byteLength - offset;
if (newLength > 0) {
/* the new buffer is bigger than the current overlap, drop the overlapping part and try again inserting the remaining buffer */
ab = this.reduceBuffer(ab, offset, newLength);
} else {
/* the content of the new buffer is entirely contained in the existing buffer, drop it entirely */
to_add = false;
break;
}
}
}
/* if the buffer has not been added, we can add it at the end */
if (to_add) {
Log.debug("MultiBufferStream", "Appending new buffer (fileStart: " + ab.fileStart + " - Length: " + ab.byteLength + ")");
this.buffers.push(ab);
/* if this new buffer is inserted in the first place in the list of the buffer,
and the DataStream is initialized, make it the buffer used for parsing */
if (i === 0) {
this.buffer = ab;
}
}
};
/**
* Displays the status of the buffers (number and used bytes)
* @param {Object} info callback method for display
*/
MultiBufferStream.prototype.logBufferLevel = function (info) {
var i;
var buffer;
var used, total;
var ranges = [];
var range;
var bufferedString = "";
used = 0;
total = 0;
for (i = 0; i < this.buffers.length; i++) {
buffer = this.buffers[i];
if (i === 0) {
range = {};
ranges.push(range);
range.start = buffer.fileStart;
range.end = buffer.fileStart + buffer.byteLength;
bufferedString += "[" + range.start + "-";
} else if (range.end === buffer.fileStart) {
range.end = buffer.fileStart + buffer.byteLength;
} else {
range = {};
range.start = buffer.fileStart;
bufferedString += ranges[ranges.length - 1].end - 1 + "], [" + range.start + "-";
range.end = buffer.fileStart + buffer.byteLength;
ranges.push(range);
}
used += buffer.usedBytes;
total += buffer.byteLength;
}
if (ranges.length > 0) {
bufferedString += range.end - 1 + "]";
}
var log = info ? Log.info : Log.debug;
if (this.buffers.length === 0) {
log("MultiBufferStream", "No more buffer in memory");
} else {
log("MultiBufferStream", "" + this.buffers.length + " stored buffer(s) (" + used + "/" + total + " bytes), continuous ranges: " + bufferedString);
}
};
MultiBufferStream.prototype.cleanBuffers = function () {
var i;
var buffer;
for (i = 0; i < this.buffers.length; i++) {
buffer = this.buffers[i];
if (buffer.usedBytes === buffer.byteLength) {
Log.debug("MultiBufferStream", "Removing buffer #" + i);
this.buffers.splice(i, 1);
i--;
}
}
};
MultiBufferStream.prototype.mergeNextBuffer = function () {
var next_buffer;
if (this.bufferIndex + 1 < this.buffers.length) {
next_buffer = this.buffers[this.bufferIndex + 1];
if (next_buffer.fileStart === this.buffer.fileStart + this.buffer.byteLength) {
var oldLength = this.buffer.byteLength;
var oldUsedBytes = this.buffer.usedBytes;
var oldFileStart = this.buffer.fileStart;
this.buffers[this.bufferIndex] = ArrayBuffer.concat(this.buffer, next_buffer);
this.buffer = this.buffers[this.bufferIndex];
this.buffers.splice(this.bufferIndex + 1, 1);
this.buffer.usedBytes = oldUsedBytes; /* TODO: should it be += ? */
this.buffer.fileStart = oldFileStart;
Log.debug("ISOFile", "Concatenating buffer for box parsing (length: " + oldLength + "->" + this.buffer.byteLength + ")");
return true;
} else {
return false;
}
} else {
return false;
}
};
/*************************************************************************
Seek-related functions
*************************************************************************/
/**
* Finds the buffer that holds the given file position
* @param {Boolean} fromStart indicates if the search should start from the current buffer (false)
* or from the first buffer (true)
* @param {Number} filePosition position in the file to seek to
* @param {Boolean} markAsUsed indicates if the bytes in between the current position and the seek position
* should be marked as used for garbage collection
* @return {Number} the index of the buffer holding the seeked file position, -1 if not found.
*/
MultiBufferStream.prototype.findPosition = function (fromStart, filePosition, markAsUsed) {
var i;
var abuffer = null;
var index = -1;
/* find the buffer with the largest position smaller than the given position */
if (fromStart === true) {
/* the reposition can be in the past, we need to check from the beginning of the list of buffers */
i = 0;
} else {
i = this.bufferIndex;
}
while (i < this.buffers.length) {
abuffer = this.buffers[i];
if (abuffer.fileStart <= filePosition) {
index = i;
if (markAsUsed) {
if (abuffer.fileStart + abuffer.byteLength <= filePosition) {
abuffer.usedBytes = abuffer.byteLength;
} else {
abuffer.usedBytes = filePosition - abuffer.fileStart;
}
this.logBufferLevel();
}
} else {
break;
}
i++;
}
if (index !== -1) {
abuffer = this.buffers[index];
if (abuffer.fileStart + abuffer.byteLength >= filePosition) {
Log.debug("MultiBufferStream", "Found position in existing buffer #" + index);
return index;
} else {
return -1;
}
} else {
return -1;
}
};
/**
* Finds the largest file position contained in a buffer or in the next buffers if they are contiguous (no gap)
* starting from the given buffer index or from the current buffer if the index is not given
*
* @param {Number} inputindex Index of the buffer to start from
* @return {Number} The largest file position found in the buffers
*/
MultiBufferStream.prototype.findEndContiguousBuf = function (inputindex) {
var i;
var currentBuf;
var nextBuf;
var index = inputindex !== undefined ? inputindex : this.bufferIndex;
currentBuf = this.buffers[index];
/* find the end of the contiguous range of data */
if (this.buffers.length > index + 1) {
for (i = index + 1; i < this.buffers.length; i++) {
nextBuf = this.buffers[i];
if (nextBuf.fileStart === currentBuf.fileStart + currentBuf.byteLength) {
currentBuf = nextBuf;
} else {
break;
}
}
}
/* return the position of last byte in the file that we have */
return currentBuf.fileStart + currentBuf.byteLength;
};
/**
* Returns the largest file position contained in the buffers, larger than the given position
* @param {Number} pos the file position to start from
* @return {Number} the largest position in the current buffer or in the buffer and the next contiguous
* buffer that holds the given position
*/
MultiBufferStream.prototype.getEndFilePositionAfter = function (pos) {
var index = this.findPosition(true, pos, false);
if (index !== -1) {
return this.findEndContiguousBuf(index);
} else {
return pos;
}
};
/*************************************************************************
Garbage collection related functions
*************************************************************************/
/**
* Marks a given number of bytes as used in the current buffer for garbage collection
* @param {Number} nbBytes
*/
MultiBufferStream.prototype.addUsedBytes = function (nbBytes) {
this.buffer.usedBytes += nbBytes;
this.logBufferLevel();
};
/**
* Marks the entire current buffer as used, ready for garbage collection
*/
MultiBufferStream.prototype.setAllUsedBytes = function () {
this.buffer.usedBytes = this.buffer.byteLength;
this.logBufferLevel();
};
/*************************************************************************
Common API between MultiBufferStream and SimpleStream
*************************************************************************/
/**
* Tries to seek to a given file position
* if possible, repositions the parsing from there and returns true
* if not possible, does not change anything and returns false
* @param {Number} filePosition position in the file to seek to
* @param {Boolean} fromStart indicates if the search should start from the current buffer (false)
* or from the first buffer (true)
* @param {Boolean} markAsUsed indicates if the bytes in between the current position and the seek position
* should be marked as used for garbage collection
* @return {Boolean} true if the seek succeeded, false otherwise
*/
MultiBufferStream.prototype.seek = function (filePosition, fromStart, markAsUsed) {
var index;
index = this.findPosition(fromStart, filePosition, markAsUsed);
if (index !== -1) {
this.buffer = this.buffers[index];
this.bufferIndex = index;
this.position = filePosition - this.buffer.fileStart;
Log.debug("MultiBufferStream", "Repositioning parser at buffer position: " + this.position);
return true;
} else {
Log.debug("MultiBufferStream", "Position " + filePosition + " not found in buffered data");
return false;
}
};
/**
* Returns the current position in the file
* @return {Number} the position in the file
*/
MultiBufferStream.prototype.getPosition = function () {
if (this.bufferIndex === -1 || this.buffers[this.bufferIndex] === null) {
throw "Error accessing position in the MultiBufferStream";
}
return this.buffers[this.bufferIndex].fileStart + this.position;
};
/**
* Returns the length of the current buffer
* @return {Number} the length of the current buffer
*/
MultiBufferStream.prototype.getLength = function () {
return this.byteLength;
};
MultiBufferStream.prototype.getEndPosition = function () {
if (this.bufferIndex === -1 || this.buffers[this.bufferIndex] === null) {
throw "Error accessing position in the MultiBufferStream";
}
return this.buffers[this.bufferIndex].fileStart + this.byteLength;
};
{
exports.MultiBufferStream = MultiBufferStream;
} // file:src/descriptor.js
/*
* Copyright (c) 2012-2013. Telecom ParisTech/TSI/MM/GPAC Cyril Concolato
* License: BSD-3-Clause (see LICENSE file)
*/
var MPEG4DescriptorParser = function () {
var ES_DescrTag = 0x03;
var DecoderConfigDescrTag = 0x04;
var DecSpecificInfoTag = 0x05;
var SLConfigDescrTag = 0x06;
var descTagToName = [];
descTagToName[ES_DescrTag] = "ES_Descriptor";
descTagToName[DecoderConfigDescrTag] = "DecoderConfigDescriptor";
descTagToName[DecSpecificInfoTag] = "DecoderSpecificInfo";
descTagToName[SLConfigDescrTag] = "SLConfigDescriptor";
this.getDescriptorName = function (tag) {
return descTagToName[tag];
};
var that = this;
var classes = {};
this.parseOneDescriptor = function (stream) {
var size = 0;
var tag;
var desc;
var byteRead;
tag = stream.readUint8();
byteRead = stream.readUint8();
while (byteRead & 0x80) {
size = (byteRead & 0x7F) << 7;
byteRead = stream.readUint8();
}
size += byteRead & 0x7F;
Log.debug("MPEG4DescriptorParser", "Found " + (descTagToName[tag] || "Descriptor " + tag) + ", size " + size + " at position " + stream.getPosition());
if (descTagToName[tag]) {
desc = new classes[descTagToName[tag]](size);
} else {
desc = new classes.Descriptor(size);
}
desc.parse(stream);
return desc;
};
classes.Descriptor = function (_tag, _size) {
this.tag = _tag;
this.size = _size;
this.descs = [];
};
classes.Descriptor.prototype.parse = function (stream) {
this.data = stream.readUint8Array(this.size);
};
classes.Descriptor.prototype.findDescriptor = function (tag) {
for (var i = 0; i < this.descs.length; i++) {
if (this.descs[i].tag == tag) {
return this.descs[i];
}
}
return null;
};
classes.Descriptor.prototype.parseRemainingDescriptors = function (stream) {
var start = stream.position;
while (stream.position < start + this.size) {
var desc = that.parseOneDescriptor(stream);
this.descs.push(desc);
}
};
classes.ES_Descriptor = function (size) {
classes.Descriptor.call(this, ES_DescrTag, size);
};
classes.ES_Descriptor.prototype = new classes.Descriptor();
classes.ES_Descriptor.prototype.parse = function (stream) {
this.ES_ID = stream.readUint16();
this.flags = stream.readUint8();
this.size -= 3;
if (this.flags & 0x80) {
this.dependsOn_ES_ID = stream.readUint16();
this.size -= 2;
} else {
this.dependsOn_ES_ID = 0;
}
if (this.flags & 0x40) {
var l = stream.readUint8();
this.URL = stream.readString(l);
this.size -= l + 1;
} else {
this.URL = "";
}
if (this.flags & 0x20) {
this.OCR_ES_ID = stream.readUint16();
this.size -= 2;
} else {
this.OCR_ES_ID = 0;
}
this.parseRemainingDescriptors(stream);
};
classes.ES_Descriptor.prototype.getOTI = function (stream) {
var dcd = this.findDescriptor(DecoderConfigDescrTag);
if (dcd) {
return dcd.oti;
} else {
return 0;
}
};
classes.ES_Descriptor.prototype.getAudioConfig = function (stream) {
var dcd = this.findDescriptor(DecoderConfigDescrTag);
if (!dcd) return null;
var dsi = dcd.findDescriptor(DecSpecificInfoTag);
if (dsi && dsi.data) {
var audioObjectType = (dsi.data[0] & 0xF8) >> 3;
if (audioObjectType === 31 && dsi.data.length >= 2) {
audioObjectType = 32 + ((dsi.data[0] & 0x7) << 3) + ((dsi.data[1] & 0xE0) >> 5);
}
return audioObjectType;
} else {
return null;
}
};
classes.DecoderConfigDescriptor = function (size) {
classes.Descriptor.call(this, DecoderConfigDescrTag, size);
};
classes.DecoderConfigDescriptor.prototype = new classes.Descriptor();
classes.DecoderConfigDescriptor.prototype.parse = function (stream) {
this.oti = stream.readUint8();
this.streamType = stream.readUint8();
this.upStream = (this.streamType >> 1 & 1) !== 0;
this.streamType = this.streamType >>> 2;
this.bufferSize = stream.readUint24();
this.maxBitrate = stream.readUint32();
this.avgBitrate = stream.readUint32();
this.size -= 13;
this.parseRemainingDescriptors(stream);
};
classes.DecoderSpecificInfo = function (size) {
classes.Descriptor.call(this, DecSpecificInfoTag, size);
};
classes.DecoderSpecificInfo.prototype = new classes.Descriptor();
classes.SLConfigDescriptor = function (size) {
classes.Descriptor.call(this, SLConfigDescrTag, size);
};
classes.SLConfigDescriptor.prototype = new classes.Descriptor();
return this;
};
{
exports.MPEG4DescriptorParser = MPEG4DescriptorParser;
}
// file:src/box.js
/*
* Copyright (c) 2012-2013. Telecom ParisTech/TSI/MM/GPAC Cyril Concolato
* License: BSD-3-Clause (see LICENSE file)
*/
var BoxParser = {
ERR_INVALID_DATA: -1,
ERR_NOT_ENOUGH_DATA: 0,
OK: 1,
// Boxes to be created with default parsing
BASIC_BOXES: ["mdat", "idat", "free", "skip", "meco", "strk"],
FULL_BOXES: ["hmhd", "nmhd", "iods", "xml ", "bxml", "ipro", "mere"],
CONTAINER_BOXES: [["moov", ["trak", "pssh"]], ["trak"], ["edts"], ["mdia"], ["minf"], ["dinf"], ["stbl", ["sgpd", "sbgp"]], ["mvex", ["trex"]], ["moof", ["traf"]], ["traf", ["trun", "sgpd", "sbgp"]], ["vttc"], ["tref"], ["iref"], ["mfra", ["tfra"]], ["meco"], ["hnti"], ["hinf"], ["strk"], ["strd"], ["sinf"], ["rinf"], ["schi"], ["trgr"], ["udta", ["kind"]], ["iprp", ["ipma"]], ["ipco"], ["grpl"], ["j2kH"], ["etyp", ["tyco"]]],
// Boxes effectively created
boxCodes: [],
fullBoxCodes: [],
containerBoxCodes: [],
sampleEntryCodes: {},
sampleGroupEntryCodes: [],
trackGroupTypes: [],
UUIDBoxes: {},
UUIDs: [],
initialize: function () {
BoxParser.FullBox.prototype = new BoxParser.Box();
BoxParser.ContainerBox.prototype = new BoxParser.Box();
BoxParser.SampleEntry.prototype = new BoxParser.Box();
BoxParser.TrackGroupTypeBox.prototype = new BoxParser.FullBox();
/* creating constructors for simple boxes */
BoxParser.BASIC_BOXES.forEach(function (type) {
BoxParser.createBoxCtor(type);
});
BoxParser.FULL_BOXES.forEach(function (type) {
BoxParser.createFullBoxCtor(type);
});
BoxParser.CONTAINER_BOXES.forEach(function (types) {
BoxParser.createContainerBoxCtor(types[0], null, types[1]);
});
},
Box: function (_type, _size, _uuid) {
this.type = _type;
this.size = _size;
this.uuid = _uuid;
},
FullBox: function (type, size, uuid) {
BoxParser.Box.call(this, type, size, uuid);
this.flags = 0;
this.version = 0;
},
ContainerBox: function (type, size, uuid) {
BoxParser.Box.call(this, type, size, uuid);
this.boxes = [];
},
SampleEntry: function (type, size, hdr_size, start) {
BoxParser.ContainerBox.call(this, type, size);
this.hdr_size = hdr_size;
this.start = start;
},
SampleGroupEntry: function (type) {
this.grouping_type = type;
},
TrackGroupTypeBox: function (type, size) {
BoxParser.FullBox.call(this, type, size);
},
createBoxCtor: function (type, parseMethod) {
BoxParser.boxCodes.push(type);
BoxParser[type + "Box"] = function (size) {
BoxParser.Box.call(this, type, size);
};
BoxParser[type + "Box"].prototype = new BoxParser.Box();
if (parseMethod) BoxParser[type + "Box"].prototype.parse = parseMethod;
},
createFullBoxCtor: function (type, parseMethod) {
//BoxParser.fullBoxCodes.push(type);
BoxParser[type + "Box"] = function (size) {
BoxParser.FullBox.call(this, type, size);
};
BoxParser[type + "Box"].prototype = new BoxParser.FullBox();
BoxParser[type + "Box"].prototype.parse = function (stream) {
this.parseFullHeader(stream);
if (parseMethod) {
parseMethod.call(this, stream);
}
};
},
addSubBoxArrays: function (subBoxNames) {
if (subBoxNames) {
this.subBoxNames = subBoxNames;
var nbSubBoxes = subBoxNames.length;
for (var k = 0; k < nbSubBoxes; k++) {
this[subBoxNames[k] + "s"] = [];
}
}
},
createContainerBoxCtor: function (type, parseMethod, subBoxNames) {
//BoxParser.containerBoxCodes.push(type);
BoxParser[type + "Box"] = function (size) {
BoxParser.ContainerBox.call(this, type, size);
BoxParser.addSubBoxArrays.call(this, subBoxNames);
};
BoxParser[type + "Box"].prototype = new BoxParser.ContainerBox();
if (parseMethod) BoxParser[type + "Box"].prototype.parse = parseMethod;
},
createMediaSampleEntryCtor: function (mediaType, parseMethod, subBoxNames) {
BoxParser.sampleEntryCodes[mediaType] = [];
BoxParser[mediaType + "SampleEntry"] = function (type, size) {
BoxParser.SampleEntry.call(this, type, size);
BoxParser.addSubBoxArrays.call(this, subBoxNames);
};
BoxParser[mediaType + "SampleEntry"].prototype = new BoxParser.SampleEntry();
if (parseMethod) BoxParser[mediaType + "SampleEntry"].prototype.parse = parseMethod;
},
createSampleEntryCtor: function (mediaType, type, parseMethod, subBoxNames) {
BoxParser.sampleEntryCodes[mediaType].push(type);
BoxParser[type + "SampleEntry"] = function (size) {
BoxParser[mediaType + "SampleEntry"].call(this, type, size);
BoxParser.addSubBoxArrays.call(this, subBoxNames);
};
BoxParser[type + "SampleEntry"].prototype = new BoxParser[mediaType + "SampleEntry"]();
if (parseMethod) BoxParser[type + "SampleEntry"].prototype.parse = parseMethod;
},
createEncryptedSampleEntryCtor: function (mediaType, type, parseMethod) {
BoxParser.createSampleEntryCtor.call(this, mediaType, type, parseMethod, ["sinf"]);
},
createSampleGroupCtor: function (type, parseMethod) {
//BoxParser.sampleGroupEntryCodes.push(type);
BoxParser[type + "SampleGroupEntry"] = function (size) {
BoxParser.SampleGroupEntry.call(this, type, size);
};
BoxParser[type + "SampleGroupEntry"].prototype = new BoxParser.SampleGroupEntry();
if (parseMethod) BoxParser[type + "SampleGroupEntry"].prototype.parse = parseMethod;
},
createTrackGroupCtor: function (type, parseMethod) {
//BoxParser.trackGroupTypes.push(type);
BoxParser[type + "TrackGroupTypeBox"] = function (size) {
BoxParser.TrackGroupTypeBox.call(this, type, size);
};
BoxParser[type + "TrackGroupTypeBox"].prototype = new BoxParser.TrackGroupTypeBox();
if (parseMethod) BoxParser[type + "TrackGroupTypeBox"].prototype.parse = parseMethod;
},
createUUIDBox: function (uuid, isFullBox, isContainerBox, parseMethod) {
BoxParser.UUIDs.push(uuid);
BoxParser.UUIDBoxes[uuid] = function (size) {
if (isFullBox) {
BoxParser.FullBox.call(this, "uuid", size, uuid);
} else {
if (isContainerBox) {
BoxParser.ContainerBox.call(this, "uuid", size, uuid);
} else {
BoxParser.Box.call(this, "uuid", size, uuid);
}
}
};
BoxParser.UUIDBoxes[uuid].prototype = isFullBox ? new BoxParser.FullBox() : isContainerBox ? new BoxParser.ContainerBox() : new BoxParser.Box();
if (parseMethod) {
if (isFullBox) {
BoxParser.UUIDBoxes[uuid].prototype.parse = function (stream) {
this.parseFullHeader(stream);
if (parseMethod) {
parseMethod.call(this, stream);
}
};
} else {
BoxParser.UUIDBoxes[uuid].prototype.parse = parseMethod;
}
}
}
};
BoxParser.initialize();
BoxParser.TKHD_FLAG_ENABLED = 0x000001;
BoxParser.TKHD_FLAG_IN_MOVIE = 0x000002;
BoxParser.TKHD_FLAG_IN_PREVIEW = 0x000004;
BoxParser.TFHD_FLAG_BASE_DATA_OFFSET = 0x01;
BoxParser.TFHD_FLAG_SAMPLE_DESC = 0x02;
BoxParser.TFHD_FLAG_SAMPLE_DUR = 0x08;
BoxParser.TFHD_FLAG_SAMPLE_SIZE = 0x10;
BoxParser.TFHD_FLAG_SAMPLE_FLAGS = 0x20;
BoxParser.TFHD_FLAG_DUR_EMPTY = 0x10000;
BoxParser.TFHD_FLAG_DEFAULT_BASE_IS_MOOF = 0x20000;
BoxParser.TRUN_FLAGS_DATA_OFFSET = 0x01;
BoxParser.TRUN_FLAGS_FIRST_FLAG = 0x04;
BoxParser.TRUN_FLAGS_DURATION = 0x100;
BoxParser.TRUN_FLAGS_SIZE = 0x200;
BoxParser.TRUN_FLAGS_FLAGS = 0x400;
BoxParser.TRUN_FLAGS_CTS_OFFSET = 0x800;
BoxParser.Box.prototype.add = function (name) {
return this.addBox(new BoxParser[name + "Box"]());
};
BoxParser.Box.prototype.addBox = function (box) {
this.boxes.push(box);
if (this[box.type + "s"]) {
this[box.type + "s"].push(box);
} else {
this[box.type] = box;
}
return box;
};
BoxParser.Box.prototype.set = function (prop, value) {
this[prop] = value;
return this;
};
BoxParser.Box.prototype.addEntry = function (value, _prop) {
var prop = _prop || "entries";
if (!this[prop]) {
this[prop] = [];
}
this[prop].push(value);
return this;
};
{
exports.BoxParser = BoxParser;
}
// file:src/box-parse.js
/*
* Copyright (c) Telecom ParisTech/TSI/MM/GPAC Cyril Concolato
* License: BSD-3-Clause (see LICENSE file)
*/
BoxParser.parseUUID = function (stream) {
return BoxParser.parseHex16(stream);
};
BoxParser.parseHex16 = function (stream) {
var hex16 = "";
for (var i = 0; i < 16; i++) {
var hex = stream.readUint8().toString(16);
hex16 += hex.length === 1 ? "0" + hex : hex;
}
return hex16;
};
BoxParser.parseOneBox = function (stream, headerOnly, parentSize) {
var box;
var start = stream.getPosition();
var hdr_size = 0;
var diff;
var uuid;
if (stream.getEndPosition() - start < 8) {
Log.debug("BoxParser", "Not enough data in stream to parse the type and size of the box");
return {
code: BoxParser.ERR_NOT_ENOUGH_DATA
};
}
if (parentSize && parentSize < 8) {
Log.debug("BoxParser", "Not enough bytes left in the parent box to parse a new box");
return {
code: BoxParser.ERR_NOT_ENOUGH_DATA
};
}
var size = stream.readUint32();
var type = stream.readString(4);
var box_type = type;
Log.debug("BoxParser", "Found box of type '" + type + "' and size " + size + " at position " + start);
hdr_size = 8;
if (type == "uuid") {
if (stream.getEndPosition() - stream.getPosition() < 16 || parentSize - hdr_size < 16) {
stream.seek(start);
Log.debug("BoxParser", "Not enough bytes left in the parent box to parse a UUID box");
return {
code: BoxParser.ERR_NOT_ENOUGH_DATA
};
}
uuid = BoxParser.parseUUID(stream);
hdr_size += 16;
box_type = uuid;
}
if (size == 1) {
if (stream.getEndPosition() - stream.getPosition() < 8 || parentSize && parentSize - hdr_size < 8) {
stream.seek(start);
Log.warn("BoxParser", "Not enough data in stream to parse the extended size of the \"" + type + "\" box");
return {
code: BoxParser.ERR_NOT_ENOUGH_DATA
};
}
size = stream.readUint64();
hdr_size += 8;
} else if (size === 0) {
/* box extends till the end of file or invalid file */
if (parentSize) {
size = parentSize;
} else {
/* box extends till the end of file */
if (type !== "mdat") {
Log.error("BoxParser", "Unlimited box size not supported for type: '" + type + "'");
box = new BoxParser.Box(type, size);
return {
code: BoxParser.OK,
box: box,
size: box.size
};
}
}
}
if (size !== 0 && size < hdr_size) {
Log.error("BoxParser", "Box of type " + type + " has an invalid size " + size + " (too small to be a box)");
return {
code: BoxParser.ERR_NOT_ENOUGH_DATA,
type: type,
size: size,
hdr_size: hdr_size,
start: start
};
}
if (size !== 0 && parentSize && size > parentSize) {
Log.error("BoxParser", "Box of type '" + type + "' has a size " + size + " greater than its container size " + parentSize);
return {
code: BoxParser.ERR_NOT_ENOUGH_DATA,
type: type,
size: size,
hdr_size: hdr_size,
start: start
};
}
if (size !== 0 && start + size > stream.getEndPosition()) {
stream.seek(start);
Log.info("BoxParser", "Not enough data in stream to parse the entire '" + type + "' box");
return {
code: BoxParser.ERR_NOT_ENOUGH_DATA,
type: type,
size: size,
hdr_size: hdr_size,
start: start
};
}
if (headerOnly) {
return {
code: BoxParser.OK,
type: type,
size: size,
hdr_size: hdr_size,
start: start
};
} else {
if (BoxParser[type + "Box"]) {
box = new BoxParser[type + "Box"](size);
} else {
if (type !== "uuid") {
Log.warn("BoxParser", "Unknown box type: '" + type + "'");
box = new BoxParser.Box(type, size);
box.has_unparsed_data = true;
} else {
if (BoxParser.UUIDBoxes[uuid]) {
box = new BoxParser.UUIDBoxes[uuid](size);
} else {
Log.warn("BoxParser", "Unknown uuid type: '" + uuid + "'");
box = new BoxParser.Box(type, size);
box.uuid = uuid;
box.has_unparsed_data = true;
}
}
}
}
box.hdr_size = hdr_size;
/* recording the position of the box in the input stream */
box.start = start;
if (box.write === BoxParser.Box.prototype.write && box.type !== "mdat") {
Log.info("BoxParser", "'" + box_type + "' box writing not yet implemented, keeping unparsed data in memory for later write");
box.parseDataAndRewind(stream);
}
box.parse(stream);
diff = stream.getPosition() - (box.start + box.size);
if (diff < 0) {
Log.warn("BoxParser", "Parsing of box '" + box_type + "' did not read the entire indicated box data size (missing " + -diff + " bytes), seeking forward");
stream.seek(box.start + box.size);
} else if (diff > 0) {
Log.error("BoxParser", "Parsing of box '" + box_type + "' read " + diff + " more bytes than the indicated box data size, seeking backwards");
if (box.size !== 0) stream.seek(box.start + box.size);
}
return {
code: BoxParser.OK,
box: box,
size: box.size
};
};
BoxParser.Box.prototype.parse = function (stream) {
if (this.type != "mdat") {
this.data = stream.readUint8Array(this.size - this.hdr_size);
} else {
if (this.size === 0) {
stream.seek(stream.getEndPosition());
} else {
stream.seek(this.start + this.size);
}
}
};
/* Used to parse a box without consuming its data, to allow detailled parsing
Useful for boxes for which a write method is not yet implemented */
BoxParser.Box.prototype.parseDataAndRewind = function (stream) {
this.data = stream.readUint8Array(this.size - this.hdr_size);
// rewinding
stream.position -= this.size - this.hdr_size;
};
BoxParser.FullBox.prototype.parseDataAndRewind = function (stream) {
this.parseFullHeader(stream);
this.data = stream.readUint8Array(this.size - this.hdr_size);
// restore the header size as if the full header had not been parsed
this.hdr_size -= 4;
// rewinding
stream.position -= this.size - this.hdr_size;
};
BoxParser.FullBox.prototype.parseFullHeader = function (stream) {
this.version = stream.readUint8();
this.flags = stream.readUint24();
this.hdr_size += 4;
};
BoxParser.FullBox.prototype.parse = function (stream) {
this.parseFullHeader(stream);
this.data = stream.readUint8Array(this.size - this.hdr_size);
};
BoxParser.ContainerBox.prototype.parse = function (stream) {
var ret;
var box;
while (stream.getPosition() < this.start + this.size) {
ret = BoxParser.parseOneBox(stream, false, this.size - (stream.getPosition() - this.start));
if (ret.code === BoxParser.OK) {
box = ret.box;
/* store the box in the 'boxes' array to preserve box order (for offset) but also store box in a property for more direct access */
this.boxes.push(box);
if (this.subBoxNames && this.subBoxNames.indexOf(box.type) != -1) {
this[this.subBoxNames[this.subBoxNames.indexOf(box.type)] + "s"].push(box);
} else {
var box_type = box.type !== "uuid" ? box.type : box.uuid;
if (this[box_type]) {
Log.warn("Box of type " + box_type + " already stored in field of this type");
} else {
this[box_type] = box;
}
}
} else {
return;
}
}
};
BoxParser.Box.prototype.parseLanguage = function (stream) {
this.language = stream.readUint16();
var chars = [];
chars[0] = this.language >> 10 & 0x1F;
chars[1] = this.language >> 5 & 0x1F;
chars[2] = this.language & 0x1F;
this.languageString = String.fromCharCode(chars[0] + 0x60, chars[1] + 0x60, chars[2] + 0x60);
};
// file:src/parsing/sampleentries/sampleentry.js
BoxParser.SAMPLE_ENTRY_TYPE_VISUAL = "Visual";
BoxParser.SAMPLE_ENTRY_TYPE_AUDIO = "Audio";
BoxParser.SAMPLE_ENTRY_TYPE_HINT = "Hint";
BoxParser.SAMPLE_ENTRY_TYPE_METADATA = "Metadata";
BoxParser.SAMPLE_ENTRY_TYPE_SUBTITLE = "Subtitle";
BoxParser.SAMPLE_ENTRY_TYPE_SYSTEM = "System";
BoxParser.SAMPLE_ENTRY_TYPE_TEXT = "Text";
BoxParser.SampleEntry.prototype.parseHeader = function (stream) {
stream.readUint8Array(6);
this.data_reference_index = stream.readUint16();
this.hdr_size += 8;
};
BoxParser.SampleEntry.prototype.parse = function (stream) {
this.parseHeader(stream);
this.data = stream.readUint8Array(this.size - this.hdr_size);
};
BoxParser.SampleEntry.prototype.parseDataAndRewind = function (stream) {
this.parseHeader(stream);
this.data = stream.readUint8Array(this.size - this.hdr_size);
// restore the header size as if the sample entry header had not been parsed
this.hdr_size -= 8;
// rewinding
stream.position -= this.size - this.hdr_size;
};
BoxParser.SampleEntry.prototype.parseFooter = function (stream) {
BoxParser.ContainerBox.prototype.parse.call(this, stream);
};
// Base SampleEntry types with default parsing
BoxParser.createMediaSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_HINT);
BoxParser.createMediaSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_METADATA);
BoxParser.createMediaSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_SUBTITLE);
BoxParser.createMediaSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_SYSTEM);
BoxParser.createMediaSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_TEXT);
//Base SampleEntry types for Audio and Video with specific parsing
BoxParser.createMediaSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, function (stream) {
var compressorname_length;
this.parseHeader(stream);
stream.readUint16();
stream.readUint16();
stream.readUint32Array(3);
this.width = stream.readUint16();
this.height = stream.readUint16();
this.horizresolution = stream.readUint32();
this.vertresolution = stream.readUint32();
stream.readUint32();
this.frame_count = stream.readUint16();
compressorname_length = Math.min(31, stream.readUint8());
this.compressorname = stream.readString(compressorname_length);
if (compressorname_length < 31) {
stream.readString(31 - compressorname_length);
}
this.depth = stream.readUint16();
stream.readUint16();
this.parseFooter(stream);
});
BoxParser.createMediaSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_AUDIO, function (stream) {
this.parseHeader(stream);
stream.readUint32Array(2);
this.channel_count = stream.readUint16();
this.samplesize = stream.readUint16();
stream.readUint16();
stream.readUint16();
this.samplerate = stream.readUint32() / (1 << 16);
this.parseFooter(stream);
});
// Sample entries inheriting from Audio and Video
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "avc1");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "avc2");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "avc3");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "avc4");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "av01");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "dav1");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "hvc1");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "hev1");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "hvt1");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "lhe1");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "dvh1");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "dvhe");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "vvc1");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "vvi1");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "vvs1");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "vvcN");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "vp08");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "vp09");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "avs3");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "j2ki");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "mjp2");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "mjpg");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "uncv");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_AUDIO, "mp4a");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_AUDIO, "ac-3");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_AUDIO, "ac-4");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_AUDIO, "ec-3");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_AUDIO, "Opus");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_AUDIO, "mha1");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_AUDIO, "mha2");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_AUDIO, "mhm1");
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_AUDIO, "mhm2");
// Encrypted sample entries
BoxParser.createEncryptedSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_VISUAL, "encv");
BoxParser.createEncryptedSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_AUDIO, "enca");
BoxParser.createEncryptedSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_SUBTITLE, "encu");
BoxParser.createEncryptedSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_SYSTEM, "encs");
BoxParser.createEncryptedSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_TEXT, "enct");
BoxParser.createEncryptedSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_METADATA, "encm");
// file:src/parsing/a1lx.js
BoxParser.createBoxCtor("a1lx", function (stream) {
var large_size = stream.readUint8() & 1;
var FieldLength = ((large_size & 1) + 1) * 16;
this.layer_size = [];
for (var i = 0; i < 3; i++) {
if (FieldLength == 16) {
this.layer_size[i] = stream.readUint16();
} else {
this.layer_size[i] = stream.readUint32();
}
}
}); // file:src/parsing/a1op.js
BoxParser.createBoxCtor("a1op", function (stream) {
this.op_index = stream.readUint8();
}); // file:src/parsing/auxC.js
BoxParser.createFullBoxCtor("auxC", function (stream) {
this.aux_type = stream.readCString();
var aux_subtype_length = this.size - this.hdr_size - (this.aux_type.length + 1);
this.aux_subtype = stream.readUint8Array(aux_subtype_length);
}); // file:src/parsing/av1C.js
BoxParser.createBoxCtor("av1C", function (stream) {
var tmp = stream.readUint8();
if (tmp >> 7 & 0x1 !== 1) {
Log.error("av1C marker problem");
return;
}
this.version = tmp & 0x7F;
if (this.version !== 1) {
Log.error("av1C version " + this.version + " not supported");
return;
}
tmp = stream.readUint8();
this.seq_profile = tmp >> 5 & 0x7;
this.seq_level_idx_0 = tmp & 0x1F;
tmp = stream.readUint8();
this.seq_tier_0 = tmp >> 7 & 0x1;
this.high_bitdepth = tmp >> 6 & 0x1;
this.twelve_bit = tmp >> 5 & 0x1;
this.monochrome = tmp >> 4 & 0x1;
this.chroma_subsampling_x = tmp >> 3 & 0x1;
this.chroma_subsampling_y = tmp >> 2 & 0x1;
this.chroma_sample_position = tmp & 0x3;
tmp = stream.readUint8();
this.reserved_1 = tmp >> 5 & 0x7;
if (this.reserved_1 !== 0) {
Log.error("av1C reserved_1 parsing problem");
return;
}
this.initial_presentation_delay_present = tmp >> 4 & 0x1;
if (this.initial_presentation_delay_present === 1) {
this.initial_presentation_delay_minus_one = tmp & 0xF;
} else {
this.reserved_2 = tmp & 0xF;
if (this.reserved_2 !== 0) {
Log.error("av1C reserved_2 parsing problem");
return;
}
}
var configOBUs_length = this.size - this.hdr_size - 4;
this.configOBUs = stream.readUint8Array(configOBUs_length);
});
// file:src/parsing/avcC.js
BoxParser.createBoxCtor("avcC", function (stream) {
var i;
var toparse;
this.configurationVersion = stream.readUint8();
this.AVCProfileIndication = stream.readUint8();
this.profile_compatibility = stream.readUint8();
this.AVCLevelIndication = stream.readUint8();
this.lengthSizeMinusOne = stream.readUint8() & 0x3;
this.nb_SPS_nalus = stream.readUint8() & 0x1F;
toparse = this.size - this.hdr_size - 6;
this.SPS = [];
for (i = 0; i < this.nb_SPS_nalus; i++) {
this.SPS[i] = {};
this.SPS[i].length = stream.readUint16();
this.SPS[i].nalu = stream.readUint8Array(this.SPS[i].length);
toparse -= 2 + this.SPS[i].length;
}
this.nb_PPS_nalus = stream.readUint8();
toparse--;
this.PPS = [];
for (i = 0; i < this.nb_PPS_nalus; i++) {
this.PPS[i] = {};
this.PPS[i].length = stream.readUint16();
this.PPS[i].nalu = stream.readUint8Array(this.PPS[i].length);
toparse -= 2 + this.PPS[i].length;
}
if (toparse > 0) {
this.ext = stream.readUint8Array(toparse);
}
});
// file:src/parsing/btrt.js
BoxParser.createBoxCtor("btrt", function (stream) {
this.bufferSizeDB = stream.readUint32();
this.maxBitrate = stream.readUint32();
this.avgBitrate = stream.readUint32();
});
// file:src/parsing/ccst.js
BoxParser.createFullBoxCtor("ccst", function (stream) {
var flags = stream.readUint8();
this.all_ref_pics_intra = (flags & 0x80) == 0x80;
this.intra_pred_used = (flags & 0x40) == 0x40;
this.max_ref_per_pic = (flags & 0x3f) >> 2;
stream.readUint24();
});
// file:src/parsing/cdef.js
BoxParser.createBoxCtor("cdef", function (stream) {
var i;
this.channel_count = stream.readUint16();
this.channel_indexes = [];
this.channel_types = [];
this.channel_associations = [];
for (i = 0; i < this.channel_count; i++) {
this.channel_indexes.push(stream.readUint16());
this.channel_types.push(stream.readUint16());
this.channel_associations.push(stream.readUint16());
}
});
// file:src/parsing/clap.js
BoxParser.createBoxCtor("clap", function (stream) {
this.cleanApertureWidthN = stream.readUint32();
this.cleanApertureWidthD = stream.readUint32();
this.cleanApertureHeightN = stream.readUint32();
this.cleanApertureHeightD = stream.readUint32();
this.horizOffN = stream.readUint32();
this.horizOffD = stream.readUint32();
this.vertOffN = stream.readUint32();
this.vertOffD = stream.readUint32();
}); // file:src/parsing/clli.js
BoxParser.createBoxCtor("clli", function (stream) {
this.max_content_light_level = stream.readUint16();
this.max_pic_average_light_level = stream.readUint16();
});
// file:src/parsing/cmex.js
BoxParser.createFullBoxCtor("cmex", function (stream) {
if (this.flags & 0x1) {
this.pos_x = stream.readInt32();
}
if (this.flags & 0x2) {
this.pos_y = stream.readInt32();
}
if (this.flags & 0x4) {
this.pos_z = stream.readInt32();
}
if (this.flags & 0x8) {
if (this.version == 0) {
if (this.flags & 0x10) {
this.quat_x = stream.readInt32();
this.quat_y = stream.readInt32();
this.quat_z = stream.readInt32();
} else {
this.quat_x = stream.readInt16();
this.quat_y = stream.readInt16();
this.quat_z = stream.readInt16();
}
} else if (this.version == 1) ;
}
if (this.flags & 0x20) {
this.id = stream.readUint32();
}
});
// file:src/parsing/cmin.js
BoxParser.createFullBoxCtor("cmin", function (stream) {
this.focal_length_x = stream.readInt32();
this.principal_point_x = stream.readInt32();
this.principal_point_y = stream.readInt32();
if (this.flags & 0x1) {
this.focal_length_y = stream.readInt32();
this.skew_factor = stream.readInt32();
}
}); // file:src/parsing/cmpd.js
BoxParser.createBoxCtor("cmpd", function (stream) {
this.component_count = stream.readUint32();
this.component_types = [];
this.component_type_urls = [];
for (i = 0; i < this.component_count; i++) {
var component_type = stream.readUint16();
this.component_types.push(component_type);
if (component_type >= 0x8000) {
this.component_type_urls.push(stream.readCString());
}
}
}); // file:src/parsing/co64.js
BoxParser.createFullBoxCtor("co64", function (stream) {
var entry_count;
var i;
entry_count = stream.readUint32();
this.chunk_offsets = [];
if (this.version === 0) {
for (i = 0; i < entry_count; i++) {
this.chunk_offsets.push(stream.readUint64());
}
}
});
// file:src/parsing/CoLL.js
BoxParser.createFullBoxCtor("CoLL", function (stream) {
this.maxCLL = stream.readUint16();
this.maxFALL = stream.readUint16();
});
// file:src/parsing/colr.js
BoxParser.createBoxCtor("colr", function (stream) {
this.colour_type = stream.readString(4);
if (this.colour_type === 'nclx') {
this.colour_primaries = stream.readUint16();
this.transfer_characteristics = stream.readUint16();
this.matrix_coefficients = stream.readUint16();
var tmp = stream.readUint8();
this.full_range_flag = tmp >> 7;
} else if (this.colour_type === 'rICC') {
this.ICC_profile = stream.readUint8Array(this.size - 4);
} else if (this.colour_type === 'prof') {
this.ICC_profile = stream.readUint8Array(this.size - 4);
}
}); // file:src/parsing/cprt.js
BoxParser.createFullBoxCtor("cprt", function (stream) {
this.parseLanguage(stream);
this.notice = stream.readCString();
});
// file:src/parsing/cslg.js
BoxParser.createFullBoxCtor("cslg", function (stream) {
if (this.version === 0) {
this.compositionToDTSShift = stream.readInt32(); /* signed */
this.leastDecodeToDisplayDelta = stream.readInt32(); /* signed */
this.greatestDecodeToDisplayDelta = stream.readInt32(); /* signed */
this.compositionStartTime = stream.readInt32(); /* signed */
this.compositionEndTime = stream.readInt32(); /* signed */
}
});
// file:src/parsing/ctts.js
BoxParser.createFullBoxCtor("ctts", function (stream) {
var entry_count;
var i;
entry_count = stream.readUint32();
this.sample_counts = [];
this.sample_offsets = [];
if (this.version === 0) {
for (i = 0; i < entry_count; i++) {
this.sample_counts.push(stream.readUint32());
/* some files are buggy and declare version=0 while using signed offsets.
The likelyhood of using the most significant bit in a 32-bits time offset is very low,
so using signed value here as well */
var value = stream.readInt32();
if (value < 0) {
Log.warn("BoxParser", "ctts box uses negative values without using version 1");
}
this.sample_offsets.push(value);
}
} else if (this.version == 1) {
for (i = 0; i < entry_count; i++) {
this.sample_counts.push(stream.readUint32());
this.sample_offsets.push(stream.readInt32()); /* signed */
}
}
});
// file:src/parsing/dac3.js
BoxParser.createBoxCtor("dac3", function (stream) {
var tmp_byte1 = stream.readUint8();
var tmp_byte2 = stream.readUint8();
var tmp_byte3 = stream.readUint8();
this.fscod = tmp_byte1 >> 6;
this.bsid = tmp_byte1 >> 1 & 0x1F;
this.bsmod = (tmp_byte1 & 0x1) << 2 | tmp_byte2 >> 6 & 0x3;
this.acmod = tmp_byte2 >> 3 & 0x7;
this.lfeon = tmp_byte2 >> 2 & 0x1;
this.bit_rate_code = tmp_byte2 & 0x3 | tmp_byte3 >> 5 & 0x7;
});
// file:src/parsing/dec3.js
BoxParser.createBoxCtor("dec3", function (stream) {
var tmp_16 = stream.readUint16();
this.data_rate = tmp_16 >> 3;
this.num_ind_sub = tmp_16 & 0x7;
this.ind_subs = [];
for (var i = 0; i < this.num_ind_sub + 1; i++) {
var ind_sub = {};
this.ind_subs.push(ind_sub);
var tmp_byte1 = stream.readUint8();
var tmp_byte2 = stream.readUint8();
var tmp_byte3 = stream.readUint8();
ind_sub.fscod = tmp_byte1 >> 6;
ind_sub.bsid = tmp_byte1 >> 1 & 0x1F;
ind_sub.bsmod = (tmp_byte1 & 0x1) << 4 | tmp_byte2 >> 4 & 0xF;
ind_sub.acmod = tmp_byte2 >> 1 & 0x7;
ind_sub.lfeon = tmp_byte2 & 0x1;
ind_sub.num_dep_sub = tmp_byte3 >> 1 & 0xF;
if (ind_sub.num_dep_sub > 0) {
ind_sub.chan_loc = (tmp_byte3 & 0x1) << 8 | stream.readUint8();
}
}
});
// file:src/parsing/dfLa.js
BoxParser.createFullBoxCtor("dfLa", function (stream) {
var BLOCKTYPE_MASK = 0x7F;
var LASTMETADATABLOCKFLAG_MASK = 0x80;
var boxesFound = [];
var knownBlockTypes = ["STREAMINFO", "PADDING", "APPLICATION", "SEEKTABLE", "VORBIS_COMMENT", "CUESHEET", "PICTURE", "RESERVED"];
// dfLa is a FullBox
this.parseFullHeader(stream);
// for (i=0; ; i++) { // to end of box
do {
var flagAndType = stream.readUint8();
var type = Math.min(flagAndType & BLOCKTYPE_MASK, knownBlockTypes.length - 1);
// if this is a STREAMINFO block, read the true samplerate since this
// can be different to the AudioSampleEntry samplerate.
if (!type) {
// read past all the other stuff
stream.readUint8Array(13);
// extract samplerate
this.samplerate = stream.readUint32() >> 12;
// read to end of STREAMINFO
stream.readUint8Array(20);
} else {
// not interested in other block types so just discard length bytes
stream.readUint8Array(stream.readUint24());
}
boxesFound.push(knownBlockTypes[type]);
if (!!(flagAndType & LASTMETADATABLOCKFLAG_MASK)) {
break;
}
} while (true);
this.numMetadataBlocks = boxesFound.length + " (" + boxesFound.join(", ") + ")";
});
// file:src/parsing/dimm.js
BoxParser.createBoxCtor("dimm", function (stream) {
this.bytessent = stream.readUint64();
});
// file:src/parsing/dmax.js
BoxParser.createBoxCtor("dmax", function (stream) {
this.time = stream.readUint32();
});
// file:src/parsing/dmed.js
BoxParser.createBoxCtor("dmed", function (stream) {
this.bytessent = stream.readUint64();
});
// file:src/parsing/dOps.js
BoxParser.createBoxCtor("dOps", function (stream) {
this.Version = stream.readUint8();
this.OutputChannelCount = stream.readUint8();
this.PreSkip = stream.readUint16();
this.InputSampleRate = stream.readUint32();
this.OutputGain = stream.readInt16();
this.ChannelMappingFamily = stream.readUint8();
if (this.ChannelMappingFamily !== 0) {
this.StreamCount = stream.readUint8();
this.CoupledCount = stream.readUint8();
this.ChannelMapping = [];
for (var i = 0; i < this.OutputChannelCount; i++) {
this.ChannelMapping[i] = stream.readUint8();
}
}
});
// file:src/parsing/dref.js
BoxParser.createFullBoxCtor("dref", function (stream) {
var ret;
var box;
this.entries = [];
var entry_count = stream.readUint32();
for (var i = 0; i < entry_count; i++) {
ret = BoxParser.parseOneBox(stream, false, this.size - (stream.getPosition() - this.start));
if (ret.code === BoxParser.OK) {
box = ret.box;
this.entries.push(box);
} else {
return;
}
}
});
// file:src/parsing/drep.js
BoxParser.createBoxCtor("drep", function (stream) {
this.bytessent = stream.readUint64();
});
// file:src/parsing/elng.js
BoxParser.createFullBoxCtor("elng", function (stream) {
this.extended_language = stream.readString(this.size - this.hdr_size);
});
// file:src/parsing/elst.js
BoxParser.createFullBoxCtor("elst", function (stream) {
this.entries = [];
var entry_count = stream.readUint32();
for (var i = 0; i < entry_count; i++) {
var entry = {};
this.entries.push(entry);
if (this.version === 1) {
entry.segment_duration = stream.readUint64();
entry.media_time = stream.readInt64();
} else {
entry.segment_duration = stream.readUint32();
entry.media_time = stream.readInt32();
}
entry.media_rate_integer = stream.readInt16();
entry.media_rate_fraction = stream.readInt16();
}
});
// file:src/parsing/emsg.js
BoxParser.createFullBoxCtor("emsg", function (stream) {
if (this.version == 1) {
this.timescale = stream.readUint32();
this.presentation_time = stream.readUint64();
this.event_duration = stream.readUint32();
this.id = stream.readUint32();
this.scheme_id_uri = stream.readCString();
this.value = stream.readCString();
} else {
this.scheme_id_uri = stream.readCString();
this.value = stream.readCString();
this.timescale = stream.readUint32();
this.presentation_time_delta = stream.readUint32();
this.event_duration = stream.readUint32();
this.id = stream.readUint32();
}
var message_size = this.size - this.hdr_size - (4 * 4 + (this.scheme_id_uri.length + 1) + (this.value.length + 1));
if (this.version == 1) {
message_size -= 4;
}
this.message_data = stream.readUint8Array(message_size);
});
// file:src/parsing/EntityToGroup.js
// ISO/IEC 14496-12:2022 Section 8.18.3 Entity to group box
BoxParser.createEntityToGroupCtor = function (type, parseMethod) {
BoxParser[type + "Box"] = function (size) {
BoxParser.FullBox.call(this, type, size);
};
BoxParser[type + "Box"].prototype = new BoxParser.FullBox();
BoxParser[type + "Box"].prototype.parse = function (stream) {
this.parseFullHeader(stream);
if (parseMethod) {
parseMethod.call(this, stream);
} else {
this.group_id = stream.readUint32();
this.num_entities_in_group = stream.readUint32();
this.entity_ids = [];
for (i = 0; i < this.num_entities_in_group; i++) {
var entity_id = stream.readUint32();
this.entity_ids.push(entity_id);
}
}
};
};
// Auto exposure bracketing (ISO/IEC 23008-12:2022 Section 6.8.6.2.1)
BoxParser.createEntityToGroupCtor("aebr");
// Flash exposure bracketing (ISO/IEC 23008-12:2022 Section 6.8.6.5.1)
BoxParser.createEntityToGroupCtor("afbr");
// Album collection (ISO/IEC 23008-12:2022 Section 6.8.7.1)
BoxParser.createEntityToGroupCtor("albc");
// Alternative entity (ISO/IEC 14496-12:2022 Section 8.18.3.1)
BoxParser.createEntityToGroupCtor("altr");
// Burst image entity group (ISO/IEC 23008-12:2022 Section 6.8.2.2)
BoxParser.createEntityToGroupCtor("brst");
// Depth of field bracketing (ISO/IEC 23008-12:2022 Section 6.8.6.6.1)
BoxParser.createEntityToGroupCtor("dobr");
// Equivalent entity (ISO/IEC 23008-12:2022 Section 6.8.1.1)
BoxParser.createEntityToGroupCtor("eqiv");
// Favourites collection (ISO/IEC 23008-12:2022 Section 6.8.7.2)
BoxParser.createEntityToGroupCtor("favc");
// Focus bracketing (ISO/IEC 23008-12:2022 Section 6.8.6.4.1)
BoxParser.createEntityToGroupCtor("fobr");
// Audio to image entity group (ISO/IEC 23008-12:2022 Section 6.8.4)
BoxParser.createEntityToGroupCtor("iaug");
// Panorama (ISO/IEC 23008-12:2022 Section 6.8.8.1)
BoxParser.createEntityToGroupCtor("pano");
// Slideshow (ISO/IEC 23008-12:2022 Section 6.8.9.1)
BoxParser.createEntityToGroupCtor("slid");
// Stereo pair (ISO/IEC 23008-12:2022 Section 6.8.5)
BoxParser.createEntityToGroupCtor("ster");
// Time-synchronised capture entity group (ISO/IEC 23008-12:2022 Section 6.8.3)
BoxParser.createEntityToGroupCtor("tsyn");
// White balance bracketing (ISO/IEC 23008-12:2022 Section 6.8.6.3.1)
BoxParser.createEntityToGroupCtor("wbbr");
// Alternative entity (ISO/IEC 23008-12:2022 AMD1 Section 6.8.10)
BoxParser.createEntityToGroupCtor("prgr");
// file:src/parsing/esds.js
BoxParser.createFullBoxCtor("esds", function (stream) {
var esd_data = stream.readUint8Array(this.size - this.hdr_size);
if (typeof MPEG4DescriptorParser !== "undefined") {
var esd_parser = new MPEG4DescriptorParser();
this.esd = esd_parser.parseOneDescriptor(new DataStream(esd_data.buffer, 0, DataStream.BIG_ENDIAN));
}
});
// file:src/parsing/fiel.js
BoxParser.createBoxCtor("fiel", function (stream) {
this.fieldCount = stream.readUint8();
this.fieldOrdering = stream.readUint8();
});
// file:src/parsing/frma.js
BoxParser.createBoxCtor("frma", function (stream) {
this.data_format = stream.readString(4);
});
// file:src/parsing/ftyp.js
BoxParser.createBoxCtor("ftyp", function (stream) {
var toparse = this.size - this.hdr_size;
this.major_brand = stream.readString(4);
this.minor_version = stream.readUint32();
toparse -= 8;
this.compatible_brands = [];
var i = 0;
while (toparse >= 4) {
this.compatible_brands[i] = stream.readString(4);
toparse -= 4;
i++;
}
});
// file:src/parsing/hdlr.js
BoxParser.createFullBoxCtor("hdlr", function (stream) {
if (this.version === 0) {
stream.readUint32();
this.handler = stream.readString(4);
stream.readUint32Array(3);
this.name = stream.readString(this.size - this.hdr_size - 20);
if (this.name[this.name.length - 1] === '\0') {
this.name = this.name.slice(0, -1);
}
}
});
// file:src/parsing/hvcC.js
BoxParser.createBoxCtor("hvcC", function (stream) {
var i, j;
var length;
var tmp_byte;
this.configurationVersion = stream.readUint8();
tmp_byte = stream.readUint8();
this.general_profile_space = tmp_byte >> 6;
this.general_tier_flag = (tmp_byte & 0x20) >> 5;
this.general_profile_idc = tmp_byte & 0x1F;
this.general_profile_compatibility = stream.readUint32();
this.general_constraint_indicator = stream.readUint8Array(6);
this.general_level_idc = stream.readUint8();
this.min_spatial_segmentation_idc = stream.readUint16() & 0xFFF;
this.parallelismType = stream.readUint8() & 0x3;
this.chroma_format_idc = stream.readUint8() & 0x3;
this.bit_depth_luma_minus8 = stream.readUint8() & 0x7;
this.bit_depth_chroma_minus8 = stream.readUint8() & 0x7;
this.avgFrameRate = stream.readUint16();
tmp_byte = stream.readUint8();
this.constantFrameRate = tmp_byte >> 6;
this.numTemporalLayers = (tmp_byte & 0XD) >> 3;
this.temporalIdNested = (tmp_byte & 0X4) >> 2;
this.lengthSizeMinusOne = tmp_byte & 0X3;
this.nalu_arrays = [];
var numOfArrays = stream.readUint8();
for (i = 0; i < numOfArrays; i++) {
var nalu_array = [];
this.nalu_arrays.push(nalu_array);
tmp_byte = stream.readUint8();
nalu_array.completeness = (tmp_byte & 0x80) >> 7;
nalu_array.nalu_type = tmp_byte & 0x3F;
var numNalus = stream.readUint16();
for (j = 0; j < numNalus; j++) {
var nalu = {};
nalu_array.push(nalu);
length = stream.readUint16();
nalu.data = stream.readUint8Array(length);
}
}
});
// file:src/parsing/iinf.js
BoxParser.createFullBoxCtor("iinf", function (stream) {
var ret;
if (this.version === 0) {
this.entry_count = stream.readUint16();
} else {
this.entry_count = stream.readUint32();
}
this.item_infos = [];
for (var i = 0; i < this.entry_count; i++) {
ret = BoxParser.parseOneBox(stream, false, this.size - (stream.getPosition() - this.start));
if (ret.code === BoxParser.OK) {
if (ret.box.type !== "infe") {
Log.error("BoxParser", "Expected 'infe' box, got " + ret.box.type);
}
this.item_infos[i] = ret.box;
} else {
return;
}
}
});
// file:src/parsing/iloc.js
BoxParser.createFullBoxCtor("iloc", function (stream) {
var byte;
byte = stream.readUint8();
this.offset_size = byte >> 4 & 0xF;
this.length_size = byte & 0xF;
byte = stream.readUint8();
this.base_offset_size = byte >> 4 & 0xF;
if (this.version === 1 || this.version === 2) {
this.index_size = byte & 0xF;
} else {
this.index_size = 0;
// reserved = byte & 0xF;
}
this.items = [];
var item_count = 0;
if (this.version < 2) {
item_count = stream.readUint16();
} else if (this.version === 2) {
item_count = stream.readUint32();
} else {
throw "version of iloc box not supported";
}
for (var i = 0; i < item_count; i++) {
var item = {};
this.items.push(item);
if (this.version < 2) {
item.item_ID = stream.readUint16();
} else if (this.version === 2) {
item.item_ID = stream.readUint32();
} else {
throw "version of iloc box not supported";
}
if (this.version === 1 || this.version === 2) {
item.construction_method = stream.readUint16() & 0xF;
} else {
item.construction_method = 0;
}
item.data_reference_index = stream.readUint16();
switch (this.base_offset_size) {
case 0:
item.base_offset = 0;
break;
case 4:
item.base_offset = stream.readUint32();
break;
case 8:
item.base_offset = stream.readUint64();
break;
default:
throw "Error reading base offset size";
}
var extent_count = stream.readUint16();
item.extents = [];
for (var j = 0; j < extent_count; j++) {
var extent = {};
item.extents.push(extent);
if (this.version === 1 || this.version === 2) {
switch (this.index_size) {
case 0:
extent.extent_index = 0;
break;
case 4:
extent.extent_index = stream.readUint32();
break;
case 8:
extent.extent_index = stream.readUint64();
break;
default:
throw "Error reading extent index";
}
}
switch (this.offset_size) {
case 0:
extent.extent_offset = 0;
break;
case 4:
extent.extent_offset = stream.readUint32();
break;
case 8:
extent.extent_offset = stream.readUint64();
break;
default:
throw "Error reading extent index";
}
switch (this.length_size) {
case 0:
extent.extent_length = 0;
break;
case 4:
extent.extent_length = stream.readUint32();
break;
case 8:
extent.extent_length = stream.readUint64();
break;
default:
throw "Error reading extent index";
}
}
}
});
// file:src/parsing/imir.js
BoxParser.createBoxCtor("imir", function (stream) {
var tmp = stream.readUint8();
this.reserved = tmp >> 7;
this.axis = tmp & 1;
}); // file:src/parsing/infe.js
BoxParser.createFullBoxCtor("infe", function (stream) {
if (this.version === 0 || this.version === 1) {
this.item_ID = stream.readUint16();
this.item_protection_index = stream.readUint16();
this.item_name = stream.readCString();
this.content_type = stream.readCString();
this.content_encoding = stream.readCString();
}
if (this.version === 1) {
this.extension_type = stream.readString(4);
Log.warn("BoxParser", "Cannot parse extension type");
stream.seek(this.start + this.size);
return;
}
if (this.version >= 2) {
if (this.version === 2) {
this.item_ID = stream.readUint16();
} else if (this.version === 3) {
this.item_ID = stream.readUint32();
}
this.item_protection_index = stream.readUint16();
this.item_type = stream.readString(4);
this.item_name = stream.readCString();
if (this.item_type === "mime") {
this.content_type = stream.readCString();
this.content_encoding = stream.readCString();
} else if (this.item_type === "uri ") {
this.item_uri_type = stream.readCString();
}
}
});
// file:src/parsing/ipma.js
BoxParser.createFullBoxCtor("ipma", function (stream) {
var i, j;
entry_count = stream.readUint32();
this.associations = [];
for (i = 0; i < entry_count; i++) {
var item_assoc = {};
this.associations.push(item_assoc);
if (this.version < 1) {
item_assoc.id = stream.readUint16();
} else {
item_assoc.id = stream.readUint32();
}
var association_count = stream.readUint8();
item_assoc.props = [];
for (j = 0; j < association_count; j++) {
var tmp = stream.readUint8();
var p = {};
item_assoc.props.push(p);
p.essential = (tmp & 0x80) >> 7 === 1;
if (this.flags & 0x1) {
p.property_index = (tmp & 0x7F) << 8 | stream.readUint8();
} else {
p.property_index = tmp & 0x7F;
}
}
}
});
// file:src/parsing/iref.js
BoxParser.createFullBoxCtor("iref", function (stream) {
var ret;
var box;
this.references = [];
while (stream.getPosition() < this.start + this.size) {
ret = BoxParser.parseOneBox(stream, true, this.size - (stream.getPosition() - this.start));
if (ret.code === BoxParser.OK) {
if (this.version === 0) {
box = new BoxParser.SingleItemTypeReferenceBox(ret.type, ret.size, ret.hdr_size, ret.start);
} else {
box = new BoxParser.SingleItemTypeReferenceBoxLarge(ret.type, ret.size, ret.hdr_size, ret.start);
}
if (box.write === BoxParser.Box.prototype.write && box.type !== "mdat") {
Log.warn("BoxParser", box.type + " box writing not yet implemented, keeping unparsed data in memory for later write");
box.parseDataAndRewind(stream);
}
box.parse(stream);
this.references.push(box);
} else {
return;
}
}
});
// file:src/parsing/irot.js
BoxParser.createBoxCtor("irot", function (stream) {
this.angle = stream.readUint8() & 0x3;
});
// file:src/parsing/ispe.js
BoxParser.createFullBoxCtor("ispe", function (stream) {
this.image_width = stream.readUint32();
this.image_height = stream.readUint32();
}); // file:src/parsing/kind.js
BoxParser.createFullBoxCtor("kind", function (stream) {
this.schemeURI = stream.readCString();
this.value = stream.readCString();
});
// file:src/parsing/leva.js
BoxParser.createFullBoxCtor("leva", function (stream) {
var count = stream.readUint8();
this.levels = [];
for (var i = 0; i < count; i++) {
var level = {};
this.levels[i] = level;
level.track_ID = stream.readUint32();
var tmp_byte = stream.readUint8();
level.padding_flag = tmp_byte >> 7;
level.assignment_type = tmp_byte & 0x7F;
switch (level.assignment_type) {
case 0:
level.grouping_type = stream.readString(4);
break;
case 1:
level.grouping_type = stream.readString(4);
level.grouping_type_parameter = stream.readUint32();
break;
case 2:
break;
case 3:
break;
case 4:
level.sub_track_id = stream.readUint32();
break;
default:
Log.warn("BoxParser", "Unknown leva assignement type");
}
}
});
// file:src/parsing/lsel.js
BoxParser.createBoxCtor("lsel", function (stream) {
this.layer_id = stream.readUint16();
}); // file:src/parsing/maxr.js
BoxParser.createBoxCtor("maxr", function (stream) {
this.period = stream.readUint32();
this.bytes = stream.readUint32();
});
// file:src/parsing/mdcv.js
function ColorPoint(x, y) {
this.x = x;
this.y = y;
}
ColorPoint.prototype.toString = function () {
return "(" + this.x + "," + this.y + ")";
};
BoxParser.createBoxCtor("mdcv", function (stream) {
this.display_primaries = [];
this.display_primaries[0] = new ColorPoint(stream.readUint16(), stream.readUint16());
this.display_primaries[1] = new ColorPoint(stream.readUint16(), stream.readUint16());
this.display_primaries[2] = new ColorPoint(stream.readUint16(), stream.readUint16());
this.white_point = new ColorPoint(stream.readUint16(), stream.readUint16());
this.max_display_mastering_luminance = stream.readUint32();
this.min_display_mastering_luminance = stream.readUint32();
});
// file:src/parsing/mdhd.js
BoxParser.createFullBoxCtor("mdhd", function (stream) {
if (this.version == 1) {
this.creation_time = stream.readUint64();
this.modification_time = stream.readUint64();
this.timescale = stream.readUint32();
this.duration = stream.readUint64();
} else {
this.creation_time = stream.readUint32();
this.modification_time = stream.readUint32();
this.timescale = stream.readUint32();
this.duration = stream.readUint32();
}
this.parseLanguage(stream);
stream.readUint16();
});
// file:src/parsing/mehd.js
BoxParser.createFullBoxCtor("mehd", function (stream) {
if (this.flags & 0x1) {
Log.warn("BoxParser", "mehd box incorrectly uses flags set to 1, converting version to 1");
this.version = 1;
}
if (this.version == 1) {
this.fragment_duration = stream.readUint64();
} else {
this.fragment_duration = stream.readUint32();
}
});
// file:src/parsing/meta.js
BoxParser.createFullBoxCtor("meta", function (stream) {
this.boxes = [];
BoxParser.ContainerBox.prototype.parse.call(this, stream);
});
// file:src/parsing/mfhd.js
BoxParser.createFullBoxCtor("mfhd", function (stream) {
this.sequence_number = stream.readUint32();
});
// file:src/parsing/mfro.js
BoxParser.createFullBoxCtor("mfro", function (stream) {
this._size = stream.readUint32();
});
// file:src/parsing/mskC.js
BoxParser.createFullBoxCtor("mskC", function (stream) {
this.bits_per_pixel = stream.readUint8();
});
// file:src/parsing/mvhd.js
BoxParser.createFullBoxCtor("mvhd", function (stream) {
if (this.version == 1) {
this.creation_time = stream.readUint64();
this.modification_time = stream.readUint64();
this.timescale = stream.readUint32();
this.duration = stream.readUint64();
} else {
this.creation_time = stream.readUint32();
this.modification_time = stream.readUint32();
this.timescale = stream.readUint32();
this.duration = stream.readUint32();
}
this.rate = stream.readUint32();
this.volume = stream.readUint16() >> 8;
stream.readUint16();
stream.readUint32Array(2);
this.matrix = stream.readUint32Array(9);
stream.readUint32Array(6);
this.next_track_id = stream.readUint32();
});
// file:src/parsing/npck.js
BoxParser.createBoxCtor("npck", function (stream) {
this.packetssent = stream.readUint32();
});
// file:src/parsing/nump.js
BoxParser.createBoxCtor("nump", function (stream) {
this.packetssent = stream.readUint64();
});
// file:src/parsing/padb.js
BoxParser.createFullBoxCtor("padb", function (stream) {
var sample_count = stream.readUint32();
this.padbits = [];
for (var i = 0; i < Math.floor((sample_count + 1) / 2); i++) {
this.padbits = stream.readUint8();
}
});
// file:src/parsing/pasp.js
BoxParser.createBoxCtor("pasp", function (stream) {
this.hSpacing = stream.readUint32();
this.vSpacing = stream.readUint32();
}); // file:src/parsing/payl.js
BoxParser.createBoxCtor("payl", function (stream) {
this.text = stream.readString(this.size - this.hdr_size);
});
// file:src/parsing/payt.js
BoxParser.createBoxCtor("payt", function (stream) {
this.payloadID = stream.readUint32();
var count = stream.readUint8();
this.rtpmap_string = stream.readString(count);
});
// file:src/parsing/pdin.js
BoxParser.createFullBoxCtor("pdin", function (stream) {
var count = (this.size - this.hdr_size) / 8;
this.rate = [];
this.initial_delay = [];
for (var i = 0; i < count; i++) {
this.rate[i] = stream.readUint32();
this.initial_delay[i] = stream.readUint32();
}
});
// file:src/parsing/pitm.js
BoxParser.createFullBoxCtor("pitm", function (stream) {
if (this.version === 0) {
this.item_id = stream.readUint16();
} else {
this.item_id = stream.readUint32();
}
});
// file:src/parsing/pixi.js
BoxParser.createFullBoxCtor("pixi", function (stream) {
var i;
this.num_channels = stream.readUint8();
this.bits_per_channels = [];
for (i = 0; i < this.num_channels; i++) {
this.bits_per_channels[i] = stream.readUint8();
}
});
// file:src/parsing/pmax.js
BoxParser.createBoxCtor("pmax", function (stream) {
this.bytes = stream.readUint32();
});
// file:src/parsing/prdi.js
BoxParser.createFullBoxCtor("prdi", function (stream) {
this.step_count = stream.readUint16();
this.item_count = [];
if (this.flags & 0x2) {
for (var i = 0; i < this.step_count; i++) {
this.item_count[i] = stream.readUint16();
}
}
}); // file:src/parsing/prft.js
BoxParser.createFullBoxCtor("prft", function (stream) {
this.ref_track_id = stream.readUint32();
this.ntp_timestamp = stream.readUint64();
if (this.version === 0) {
this.media_time = stream.readUint32();
} else {
this.media_time = stream.readUint64();
}
});
// file:src/parsing/pssh.js
BoxParser.createFullBoxCtor("pssh", function (stream) {
this.system_id = BoxParser.parseHex16(stream);
if (this.version > 0) {
var count = stream.readUint32();
this.kid = [];
for (var i = 0; i < count; i++) {
this.kid[i] = BoxParser.parseHex16(stream);
}
}
var datasize = stream.readUint32();
if (datasize > 0) {
this.data = stream.readUint8Array(datasize);
}
});
// file:src/parsing/qt/clef.js
BoxParser.createFullBoxCtor("clef", function (stream) {
this.width = stream.readUint32();
this.height = stream.readUint32();
}); // file:src/parsing/qt/enof.js
BoxParser.createFullBoxCtor("enof", function (stream) {
this.width = stream.readUint32();
this.height = stream.readUint32();
}); // file:src/parsing/qt/prof.js
BoxParser.createFullBoxCtor("prof", function (stream) {
this.width = stream.readUint32();
this.height = stream.readUint32();
}); // file:src/parsing/qt/tapt.js
BoxParser.createContainerBoxCtor("tapt", null, ["clef", "prof", "enof"]); // file:src/parsing/rtp.js
BoxParser.createBoxCtor("rtp ", function (stream) {
this.descriptionformat = stream.readString(4);
this.sdptext = stream.readString(this.size - this.hdr_size - 4);
});
// file:src/parsing/saio.js
BoxParser.createFullBoxCtor("saio", function (stream) {
if (this.flags & 0x1) {
this.aux_info_type = stream.readUint32();
this.aux_info_type_parameter = stream.readUint32();
}
var count = stream.readUint32();
this.offset = [];
for (var i = 0; i < count; i++) {
if (this.version === 0) {
this.offset[i] = stream.readUint32();
} else {
this.offset[i] = stream.readUint64();
}
}
});
// file:src/parsing/saiz.js
BoxParser.createFullBoxCtor("saiz", function (stream) {
if (this.flags & 0x1) {
this.aux_info_type = stream.readUint32();
this.aux_info_type_parameter = stream.readUint32();
}
this.default_sample_info_size = stream.readUint8();
var count = stream.readUint32();
this.sample_info_size = [];
if (this.default_sample_info_size === 0) {
for (var i = 0; i < count; i++) {
this.sample_info_size[i] = stream.readUint8();
}
}
});
// file:src/parsing/sampleentries/mett.js
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_METADATA, "mett", function (stream) {
this.parseHeader(stream);
this.content_encoding = stream.readCString();
this.mime_format = stream.readCString();
this.parseFooter(stream);
});
// file:src/parsing/sampleentries/metx.js
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_METADATA, "metx", function (stream) {
this.parseHeader(stream);
this.content_encoding = stream.readCString();
this.namespace = stream.readCString();
this.schema_location = stream.readCString();
this.parseFooter(stream);
});
// file:src/parsing/sampleentries/sbtt.js
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_SUBTITLE, "sbtt", function (stream) {
this.parseHeader(stream);
this.content_encoding = stream.readCString();
this.mime_format = stream.readCString();
this.parseFooter(stream);
});
// file:src/parsing/sampleentries/stpp.js
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_SUBTITLE, "stpp", function (stream) {
this.parseHeader(stream);
this.namespace = stream.readCString();
this.schema_location = stream.readCString();
this.auxiliary_mime_types = stream.readCString();
this.parseFooter(stream);
});
// file:src/parsing/sampleentries/stxt.js
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_SUBTITLE, "stxt", function (stream) {
this.parseHeader(stream);
this.content_encoding = stream.readCString();
this.mime_format = stream.readCString();
this.parseFooter(stream);
});
// file:src/parsing/sampleentries/tx3g.js
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_SUBTITLE, "tx3g", function (stream) {
this.parseHeader(stream);
this.displayFlags = stream.readUint32();
this.horizontal_justification = stream.readInt8();
this.vertical_justification = stream.readInt8();
this.bg_color_rgba = stream.readUint8Array(4);
this.box_record = stream.readInt16Array(4);
this.style_record = stream.readUint8Array(12);
this.parseFooter(stream);
});
// file:src/parsing/sampleentries/wvtt.js
BoxParser.createSampleEntryCtor(BoxParser.SAMPLE_ENTRY_TYPE_METADATA, "wvtt", function (stream) {
this.parseHeader(stream);
this.parseFooter(stream);
});
// file:src/parsing/samplegroups/alst.js
BoxParser.createSampleGroupCtor("alst", function (stream) {
var i;
var roll_count = stream.readUint16();
this.first_output_sample = stream.readUint16();
this.sample_offset = [];
for (i = 0; i < roll_count; i++) {
this.sample_offset[i] = stream.readUint32();
}
var remaining = this.description_length - 4 - 4 * roll_count;
this.num_output_samples = [];
this.num_total_samples = [];
for (i = 0; i < remaining / 4; i++) {
this.num_output_samples[i] = stream.readUint16();
this.num_total_samples[i] = stream.readUint16();
}
});
// file:src/parsing/samplegroups/avll.js
BoxParser.createSampleGroupCtor("avll", function (stream) {
this.layerNumber = stream.readUint8();
this.accurateStatisticsFlag = stream.readUint8();
this.avgBitRate = stream.readUint16();
this.avgFrameRate = stream.readUint16();
});
// file:src/parsing/samplegroups/avss.js
BoxParser.createSampleGroupCtor("avss", function (stream) {
this.subSequenceIdentifier = stream.readUint16();
this.layerNumber = stream.readUint8();
var tmp_byte = stream.readUint8();
this.durationFlag = tmp_byte >> 7;
this.avgRateFlag = tmp_byte >> 6 & 0x1;
if (this.durationFlag) {
this.duration = stream.readUint32();
}
if (this.avgRateFlag) {
this.accurateStatisticsFlag = stream.readUint8();
this.avgBitRate = stream.readUint16();
this.avgFrameRate = stream.readUint16();
}
this.dependency = [];
var numReferences = stream.readUint8();
for (var i = 0; i < numReferences; i++) {
var dependencyInfo = {};
this.dependency.push(dependencyInfo);
dependencyInfo.subSeqDirectionFlag = stream.readUint8();
dependencyInfo.layerNumber = stream.readUint8();
dependencyInfo.subSequenceIdentifier = stream.readUint16();
}
});
// file:src/parsing/samplegroups/dtrt.js
BoxParser.createSampleGroupCtor("dtrt", function (stream) {
Log.warn("BoxParser", "Sample Group type: " + this.grouping_type + " not fully parsed");
});
// file:src/parsing/samplegroups/mvif.js
BoxParser.createSampleGroupCtor("mvif", function (stream) {
Log.warn("BoxParser", "Sample Group type: " + this.grouping_type + " not fully parsed");
});
// file:src/parsing/samplegroups/prol.js
BoxParser.createSampleGroupCtor("prol", function (stream) {
this.roll_distance = stream.readInt16();
});
// file:src/parsing/samplegroups/rap.js
BoxParser.createSampleGroupCtor("rap ", function (stream) {
var tmp_byte = stream.readUint8();
this.num_leading_samples_known = tmp_byte >> 7;
this.num_leading_samples = tmp_byte & 0x7F;
});
// file:src/parsing/samplegroups/rash.js
BoxParser.createSampleGroupCtor("rash", function (stream) {
this.operation_point_count = stream.readUint16();
if (this.description_length !== 2 + (this.operation_point_count === 1 ? 2 : this.operation_point_count * 6) + 9) {
Log.warn("BoxParser", "Mismatch in " + this.grouping_type + " sample group length");
this.data = stream.readUint8Array(this.description_length - 2);
} else {
if (this.operation_point_count === 1) {
this.target_rate_share = stream.readUint16();
} else {
this.target_rate_share = [];
this.available_bitrate = [];
for (var i = 0; i < this.operation_point_count; i++) {
this.available_bitrate[i] = stream.readUint32();
this.target_rate_share[i] = stream.readUint16();
}
}
this.maximum_bitrate = stream.readUint32();
this.minimum_bitrate = stream.readUint32();
this.discard_priority = stream.readUint8();
}
});
// file:src/parsing/samplegroups/roll.js
BoxParser.createSampleGroupCtor("roll", function (stream) {
this.roll_distance = stream.readInt16();
});
// file:src/parsing/samplegroups/samplegroup.js
BoxParser.SampleGroupEntry.prototype.parse = function (stream) {
Log.warn("BoxParser", "Unknown Sample Group type: " + this.grouping_type);
this.data = stream.readUint8Array(this.description_length);
};
// file:src/parsing/samplegroups/scif.js
BoxParser.createSampleGroupCtor("scif", function (stream) {
Log.warn("BoxParser", "Sample Group type: " + this.grouping_type + " not fully parsed");
});
// file:src/parsing/samplegroups/scnm.js
BoxParser.createSampleGroupCtor("scnm", function (stream) {
Log.warn("BoxParser", "Sample Group type: " + this.grouping_type + " not fully parsed");
});
// file:src/parsing/samplegroups/seig.js
BoxParser.createSampleGroupCtor("seig", function (stream) {
this.reserved = stream.readUint8();
var tmp = stream.readUint8();
this.crypt_byte_block = tmp >> 4;
this.skip_byte_block = tmp & 0xF;
this.isProtected = stream.readUint8();
this.Per_Sample_IV_Size = stream.readUint8();
this.KID = BoxParser.parseHex16(stream);
this.constant_IV_size = 0;
this.constant_IV = 0;
if (this.isProtected === 1 && this.Per_Sample_IV_Size === 0) {
this.constant_IV_size = stream.readUint8();
this.constant_IV = stream.readUint8Array(this.constant_IV_size);
}
});
// file:src/parsing/samplegroups/stsa.js
BoxParser.createSampleGroupCtor("stsa", function (stream) {
Log.warn("BoxParser", "Sample Group type: " + this.grouping_type + " not fully parsed");
});
// file:src/parsing/samplegroups/sync.js
BoxParser.createSampleGroupCtor("sync", function (stream) {
var tmp_byte = stream.readUint8();
this.NAL_unit_type = tmp_byte & 0x3F;
});
// file:src/parsing/samplegroups/tele.js
BoxParser.createSampleGroupCtor("tele", function (stream) {
var tmp_byte = stream.readUint8();
this.level_independently_decodable = tmp_byte >> 7;
});
// file:src/parsing/samplegroups/tsas.js
BoxParser.createSampleGroupCtor("tsas", function (stream) {
Log.warn("BoxParser", "Sample Group type: " + this.grouping_type + " not fully parsed");
});
// file:src/parsing/samplegroups/tscl.js
BoxParser.createSampleGroupCtor("tscl", function (stream) {
Log.warn("BoxParser", "Sample Group type: " + this.grouping_type + " not fully parsed");
});
// file:src/parsing/samplegroups/vipr.js
BoxParser.createSampleGroupCtor("vipr", function (stream) {
Log.warn("BoxParser", "Sample Group type: " + this.grouping_type + " not fully parsed");
});
// file:src/parsing/sbgp.js
BoxParser.createFullBoxCtor("sbgp", function (stream) {
this.grouping_type = stream.readString(4);
if (this.version === 1) {
this.grouping_type_parameter = stream.readUint32();
} else {
this.grouping_type_parameter = 0;
}
this.entries = [];
var entry_count = stream.readUint32();
for (var i = 0; i < entry_count; i++) {
var entry = {};
this.entries.push(entry);
entry.sample_count = stream.readInt32();
entry.group_description_index = stream.readInt32();
}
});
// file:src/parsing/sbpm.js
function Pixel(row, col) {
this.bad_pixel_row = row;
this.bad_pixel_column = col;
}
Pixel.prototype.toString = function pixelToString() {
return "[row: " + this.bad_pixel_row + ", column: " + this.bad_pixel_column + "]";
};
BoxParser.createFullBoxCtor("sbpm", function (stream) {
var i;
this.component_count = stream.readUint16();
this.component_index = [];
for (i = 0; i < this.component_count; i++) {
this.component_index.push(stream.readUint16());
}
var flags = stream.readUint8();
this.correction_applied = 0x80 == (flags & 0x80);
this.num_bad_rows = stream.readUint32();
this.num_bad_cols = stream.readUint32();
this.num_bad_pixels = stream.readUint32();
this.bad_rows = [];
this.bad_columns = [];
this.bad_pixels = [];
for (i = 0; i < this.num_bad_rows; i++) {
this.bad_rows.push(stream.readUint32());
}
for (i = 0; i < this.num_bad_cols; i++) {
this.bad_columns.push(stream.readUint32());
}
for (i = 0; i < this.num_bad_pixels; i++) {
var row = stream.readUint32();
var col = stream.readUint32();
this.bad_pixels.push(new Pixel(row, col));
}
});
// file:src/parsing/schm.js
BoxParser.createFullBoxCtor("schm", function (stream) {
this.scheme_type = stream.readString(4);
this.scheme_version = stream.readUint32();
if (this.flags & 0x000001) {
this.scheme_uri = stream.readString(this.size - this.hdr_size - 8);
}
});
// file:src/parsing/sdp.js
BoxParser.createBoxCtor("sdp ", function (stream) {
this.sdptext = stream.readString(this.size - this.hdr_size);
});
// file:src/parsing/sdtp.js
BoxParser.createFullBoxCtor("sdtp", function (stream) {
var tmp_byte;
var count = this.size - this.hdr_size;
this.is_leading = [];
this.sample_depends_on = [];
this.sample_is_depended_on = [];
this.sample_has_redundancy = [];
for (var i = 0; i < count; i++) {
tmp_byte = stream.readUint8();
this.is_leading[i] = tmp_byte >> 6;
this.sample_depends_on[i] = tmp_byte >> 4 & 0x3;
this.sample_is_depended_on[i] = tmp_byte >> 2 & 0x3;
this.sample_has_redundancy[i] = tmp_byte & 0x3;
}
});
// file:src/parsing/senc.js
// Cannot be fully parsed because Per_Sample_IV_Size needs to be known
BoxParser.createFullBoxCtor("senc" /*, function(stream) {
this.parseFullHeader(stream);
var sample_count = stream.readUint32();
this.samples = [];
for (var i = 0; i < sample_count; i++) {
var sample = {};
// tenc.default_Per_Sample_IV_Size or seig.Per_Sample_IV_Size
sample.InitializationVector = this.readUint8Array(Per_Sample_IV_Size*8);
if (this.flags & 0x2) {
sample.subsamples = [];
subsample_count = stream.readUint16();
for (var j = 0; j < subsample_count; j++) {
var subsample = {};
subsample.BytesOfClearData = stream.readUint16();
subsample.BytesOfProtectedData = stream.readUint32();
sample.subsamples.push(subsample);
}
}
// TODO
this.samples.push(sample);
}
}*/);
// file:src/parsing/sgpd.js
BoxParser.createFullBoxCtor("sgpd", function (stream) {
this.grouping_type = stream.readString(4);
Log.debug("BoxParser", "Found Sample Groups of type " + this.grouping_type);
if (this.version === 1) {
this.default_length = stream.readUint32();
} else {
this.default_length = 0;
}
if (this.version >= 2) {
this.default_group_description_index = stream.readUint32();
}
this.entries = [];
var entry_count = stream.readUint32();
for (var i = 0; i < entry_count; i++) {
var entry;
if (BoxParser[this.grouping_type + "SampleGroupEntry"]) {
entry = new BoxParser[this.grouping_type + "SampleGroupEntry"](this.grouping_type);
} else {
entry = new BoxParser.SampleGroupEntry(this.grouping_type);
}
this.entries.push(entry);
if (this.version === 1) {
if (this.default_length === 0) {
entry.description_length = stream.readUint32();
} else {
entry.description_length = this.default_length;
}
} else {
entry.description_length = this.default_length;
}
if (entry.write === BoxParser.SampleGroupEntry.prototype.write) {
Log.info("BoxParser", "SampleGroup for type " + this.grouping_type + " writing not yet implemented, keeping unparsed data in memory for later write");
// storing data
entry.data = stream.readUint8Array(entry.description_length);
// rewinding
stream.position -= entry.description_length;
}
entry.parse(stream);
}
});
// file:src/parsing/sidx.js
BoxParser.createFullBoxCtor("sidx", function (stream) {
this.reference_ID = stream.readUint32();
this.timescale = stream.readUint32();
if (this.version === 0) {
this.earliest_presentation_time = stream.readUint32();
this.first_offset = stream.readUint32();
} else {
this.earliest_presentation_time = stream.readUint64();
this.first_offset = stream.readUint64();
}
stream.readUint16();
this.references = [];
var count = stream.readUint16();
for (var i = 0; i < count; i++) {
var ref = {};
this.references.push(ref);
var tmp_32 = stream.readUint32();
ref.reference_type = tmp_32 >> 31 & 0x1;
ref.referenced_size = tmp_32 & 0x7FFFFFFF;
ref.subsegment_duration = stream.readUint32();
tmp_32 = stream.readUint32();
ref.starts_with_SAP = tmp_32 >> 31 & 0x1;
ref.SAP_type = tmp_32 >> 28 & 0x7;
ref.SAP_delta_time = tmp_32 & 0xFFFFFFF;
}
});
// file:src/parsing/singleitemtypereference.js
BoxParser.SingleItemTypeReferenceBox = function (type, size, hdr_size, start) {
BoxParser.Box.call(this, type, size);
this.hdr_size = hdr_size;
this.start = start;
};
BoxParser.SingleItemTypeReferenceBox.prototype = new BoxParser.Box();
BoxParser.SingleItemTypeReferenceBox.prototype.parse = function (stream) {
this.from_item_ID = stream.readUint16();
var count = stream.readUint16();
this.references = [];
for (var i = 0; i < count; i++) {
this.references[i] = {};
this.references[i].to_item_ID = stream.readUint16();
}
};
// file:src/parsing/singleitemtypereferencelarge.js
BoxParser.SingleItemTypeReferenceBoxLarge = function (type, size, hdr_size, start) {
BoxParser.Box.call(this, type, size);
this.hdr_size = hdr_size;
this.start = start;
};
BoxParser.SingleItemTypeReferenceBoxLarge.prototype = new BoxParser.Box();
BoxParser.SingleItemTypeReferenceBoxLarge.prototype.parse = function (stream) {
this.from_item_ID = stream.readUint32();
var count = stream.readUint16();
this.references = [];
for (var i = 0; i < count; i++) {
this.references[i] = {};
this.references[i].to_item_ID = stream.readUint32();
}
};
// file:src/parsing/SmDm.js
BoxParser.createFullBoxCtor("SmDm", function (stream) {
this.primaryRChromaticity_x = stream.readUint16();
this.primaryRChromaticity_y = stream.readUint16();
this.primaryGChromaticity_x = stream.readUint16();
this.primaryGChromaticity_y = stream.readUint16();
this.primaryBChromaticity_x = stream.readUint16();
this.primaryBChromaticity_y = stream.readUint16();
this.whitePointChromaticity_x = stream.readUint16();
this.whitePointChromaticity_y = stream.readUint16();
this.luminanceMax = stream.readUint32();
this.luminanceMin = stream.readUint32();
});
// file:src/parsing/smhd.js
BoxParser.createFullBoxCtor("smhd", function (stream) {
this.balance = stream.readUint16();
stream.readUint16();
});
// file:src/parsing/ssix.js
BoxParser.createFullBoxCtor("ssix", function (stream) {
this.subsegments = [];
var subsegment_count = stream.readUint32();
for (var i = 0; i < subsegment_count; i++) {
var subsegment = {};
this.subsegments.push(subsegment);
subsegment.ranges = [];
var range_count = stream.readUint32();
for (var j = 0; j < range_count; j++) {
var range = {};
subsegment.ranges.push(range);
range.level = stream.readUint8();
range.range_size = stream.readUint24();
}
}
});
// file:src/parsing/stco.js
BoxParser.createFullBoxCtor("stco", function (stream) {
var entry_count;
entry_count = stream.readUint32();
this.chunk_offsets = [];
if (this.version === 0) {
for (var i = 0; i < entry_count; i++) {
this.chunk_offsets.push(stream.readUint32());
}
}
});
// file:src/parsing/stdp.js
BoxParser.createFullBoxCtor("stdp", function (stream) {
var count = (this.size - this.hdr_size) / 2;
this.priority = [];
for (var i = 0; i < count; i++) {
this.priority[i] = stream.readUint16();
}
});
// file:src/parsing/sthd.js
BoxParser.createFullBoxCtor("sthd");
// file:src/parsing/stri.js
BoxParser.createFullBoxCtor("stri", function (stream) {
this.switch_group = stream.readUint16();
this.alternate_group = stream.readUint16();
this.sub_track_id = stream.readUint32();
var count = (this.size - this.hdr_size - 8) / 4;
this.attribute_list = [];
for (var i = 0; i < count; i++) {
this.attribute_list[i] = stream.readUint32();
}
});
// file:src/parsing/stsc.js
BoxParser.createFullBoxCtor("stsc", function (stream) {
var entry_count;
var i;
entry_count = stream.readUint32();
this.first_chunk = [];
this.samples_per_chunk = [];
this.sample_description_index = [];
if (this.version === 0) {
for (i = 0; i < entry_count; i++) {
this.first_chunk.push(stream.readUint32());
this.samples_per_chunk.push(stream.readUint32());
this.sample_description_index.push(stream.readUint32());
}
}
});
// file:src/parsing/stsd.js
BoxParser.createFullBoxCtor("stsd", function (stream) {
var i;
var ret;
var entryCount;
var box;
this.entries = [];
entryCount = stream.readUint32();
for (i = 1; i <= entryCount; i++) {
ret = BoxParser.parseOneBox(stream, true, this.size - (stream.getPosition() - this.start));
if (ret.code === BoxParser.OK) {
if (BoxParser[ret.type + "SampleEntry"]) {
box = new BoxParser[ret.type + "SampleEntry"](ret.size);
box.hdr_size = ret.hdr_size;
box.start = ret.start;
} else {
Log.warn("BoxParser", "Unknown sample entry type: " + ret.type);
box = new BoxParser.SampleEntry(ret.type, ret.size, ret.hdr_size, ret.start);
}
if (box.write === BoxParser.SampleEntry.prototype.write) {
Log.info("BoxParser", "SampleEntry " + box.type + " box writing not yet implemented, keeping unparsed data in memory for later write");
box.parseDataAndRewind(stream);
}
box.parse(stream);
this.entries.push(box);
} else {
return;
}
}
});
// file:src/parsing/stsg.js
BoxParser.createFullBoxCtor("stsg", function (stream) {
this.grouping_type = stream.readUint32();
var count = stream.readUint16();
this.group_description_index = [];
for (var i = 0; i < count; i++) {
this.group_description_index[i] = stream.readUint32();
}
});
// file:src/parsing/stsh.js
BoxParser.createFullBoxCtor("stsh", function (stream) {
var entry_count;
var i;
entry_count = stream.readUint32();
this.shadowed_sample_numbers = [];
this.sync_sample_numbers = [];
if (this.version === 0) {
for (i = 0; i < entry_count; i++) {
this.shadowed_sample_numbers.push(stream.readUint32());
this.sync_sample_numbers.push(stream.readUint32());
}
}
});
// file:src/parsing/stss.js
BoxParser.createFullBoxCtor("stss", function (stream) {
var i;
var entry_count;
entry_count = stream.readUint32();
if (this.version === 0) {
this.sample_numbers = [];
for (i = 0; i < entry_count; i++) {
this.sample_numbers.push(stream.readUint32());
}
}
});
// file:src/parsing/stsz.js
BoxParser.createFullBoxCtor("stsz", function (stream) {
var i;
this.sample_sizes = [];
if (this.version === 0) {
this.sample_size = stream.readUint32();
this.sample_count = stream.readUint32();
for (i = 0; i < this.sample_count; i++) {
if (this.sample_size === 0) {
this.sample_sizes.push(stream.readUint32());
} else {
this.sample_sizes[i] = this.sample_size;
}
}
}
});
// file:src/parsing/stts.js
BoxParser.createFullBoxCtor("stts", function (stream) {
var entry_count;
var i;
var delta;
entry_count = stream.readUint32();
this.sample_counts = [];
this.sample_deltas = [];
if (this.version === 0) {
for (i = 0; i < entry_count; i++) {
this.sample_counts.push(stream.readUint32());
delta = stream.readInt32();
if (delta < 0) {
Log.warn("BoxParser", "File uses negative stts sample delta, using value 1 instead, sync may be lost!");
delta = 1;
}
this.sample_deltas.push(delta);
}
}
});
// file:src/parsing/stvi.js
BoxParser.createFullBoxCtor("stvi", function (stream) {
var tmp32 = stream.readUint32();
this.single_view_allowed = tmp32 & 0x3;
this.stereo_scheme = stream.readUint32();
var length = stream.readUint32();
this.stereo_indication_type = stream.readString(length);
var ret;
var box;
this.boxes = [];
while (stream.getPosition() < this.start + this.size) {
ret = BoxParser.parseOneBox(stream, false, this.size - (stream.getPosition() - this.start));
if (ret.code === BoxParser.OK) {
box = ret.box;
this.boxes.push(box);
this[box.type] = box;
} else {
return;
}
}
});
// file:src/parsing/styp.js
BoxParser.createBoxCtor("styp", function (stream) {
BoxParser.ftypBox.prototype.parse.call(this, stream);
});
// file:src/parsing/stz2.js
BoxParser.createFullBoxCtor("stz2", function (stream) {
var i;
var sample_count;
this.sample_sizes = [];
if (this.version === 0) {
this.reserved = stream.readUint24();
this.field_size = stream.readUint8();
sample_count = stream.readUint32();
if (this.field_size === 4) {
for (i = 0; i < sample_count; i += 2) {
var tmp = stream.readUint8();
this.sample_sizes[i] = tmp >> 4 & 0xF;
this.sample_sizes[i + 1] = tmp & 0xF;
}
} else if (this.field_size === 8) {
for (i = 0; i < sample_count; i++) {
this.sample_sizes[i] = stream.readUint8();
}
} else if (this.field_size === 16) {
for (i = 0; i < sample_count; i++) {
this.sample_sizes[i] = stream.readUint16();
}
} else {
Log.error("BoxParser", "Error in length field in stz2 box");
}
}
});
// file:src/parsing/subs.js
BoxParser.createFullBoxCtor("subs", function (stream) {
var i, j;
var entry_count;
var subsample_count;
entry_count = stream.readUint32();
this.entries = [];
for (i = 0; i < entry_count; i++) {
var sampleInfo = {};
this.entries[i] = sampleInfo;
sampleInfo.sample_delta = stream.readUint32();
sampleInfo.subsamples = [];
subsample_count = stream.readUint16();
if (subsample_count > 0) {
for (j = 0; j < subsample_count; j++) {
var subsample = {};
sampleInfo.subsamples.push(subsample);
if (this.version == 1) {
subsample.size = stream.readUint32();
} else {
subsample.size = stream.readUint16();
}
subsample.priority = stream.readUint8();
subsample.discardable = stream.readUint8();
subsample.codec_specific_parameters = stream.readUint32();
}
}
}
});
// file:src/parsing/tenc.js
BoxParser.createFullBoxCtor("tenc", function (stream) {
stream.readUint8(); // reserved
if (this.version === 0) {
stream.readUint8();
} else {
var tmp = stream.readUint8();
this.default_crypt_byte_block = tmp >> 4 & 0xF;
this.default_skip_byte_block = tmp & 0xF;
}
this.default_isProtected = stream.readUint8();
this.default_Per_Sample_IV_Size = stream.readUint8();
this.default_KID = BoxParser.parseHex16(stream);
if (this.default_isProtected === 1 && this.default_Per_Sample_IV_Size === 0) {
this.default_constant_IV_size = stream.readUint8();
this.default_constant_IV = stream.readUint8Array(this.default_constant_IV_size);
}
}); // file:src/parsing/tfdt.js
BoxParser.createFullBoxCtor("tfdt", function (stream) {
if (this.version == 1) {
this.baseMediaDecodeTime = stream.readUint64();
} else {
this.baseMediaDecodeTime = stream.readUint32();
}
});
// file:src/parsing/tfhd.js
BoxParser.createFullBoxCtor("tfhd", function (stream) {
var readBytes = 0;
this.track_id = stream.readUint32();
if (this.size - this.hdr_size > readBytes && this.flags & BoxParser.TFHD_FLAG_BASE_DATA_OFFSET) {
this.base_data_offset = stream.readUint64();
readBytes += 8;
} else {
this.base_data_offset = 0;
}
if (this.size - this.hdr_size > readBytes && this.flags & BoxParser.TFHD_FLAG_SAMPLE_DESC) {
this.default_sample_description_index = stream.readUint32();
readBytes += 4;
} else {
this.default_sample_description_index = 0;
}
if (this.size - this.hdr_size > readBytes && this.flags & BoxParser.TFHD_FLAG_SAMPLE_DUR) {
this.default_sample_duration = stream.readUint32();
readBytes += 4;
} else {
this.default_sample_duration = 0;
}
if (this.size - this.hdr_size > readBytes && this.flags & BoxParser.TFHD_FLAG_SAMPLE_SIZE) {
this.default_sample_size = stream.readUint32();
readBytes += 4;
} else {
this.default_sample_size = 0;
}
if (this.size - this.hdr_size > readBytes && this.flags & BoxParser.TFHD_FLAG_SAMPLE_FLAGS) {
this.default_sample_flags = stream.readUint32();
readBytes += 4;
} else {
this.default_sample_flags = 0;
}
});
// file:src/parsing/tfra.js
BoxParser.createFullBoxCtor("tfra", function (stream) {
this.track_ID = stream.readUint32();
stream.readUint24();
var tmp_byte = stream.readUint8();
this.length_size_of_traf_num = tmp_byte >> 4 & 0x3;
this.length_size_of_trun_num = tmp_byte >> 2 & 0x3;
this.length_size_of_sample_num = tmp_byte & 0x3;
this.entries = [];
var number_of_entries = stream.readUint32();
for (var i = 0; i < number_of_entries; i++) {
if (this.version === 1) {
this.time = stream.readUint64();
this.moof_offset = stream.readUint64();
} else {
this.time = stream.readUint32();
this.moof_offset = stream.readUint32();
}
this.traf_number = stream["readUint" + 8 * (this.length_size_of_traf_num + 1)]();
this.trun_number = stream["readUint" + 8 * (this.length_size_of_trun_num + 1)]();
this.sample_number = stream["readUint" + 8 * (this.length_size_of_sample_num + 1)]();
}
});
// file:src/parsing/tkhd.js
BoxParser.createFullBoxCtor("tkhd", function (stream) {
if (this.version == 1) {
this.creation_time = stream.readUint64();
this.modification_time = stream.readUint64();
this.track_id = stream.readUint32();
stream.readUint32();
this.duration = stream.readUint64();
} else {
this.creation_time = stream.readUint32();
this.modification_time = stream.readUint32();
this.track_id = stream.readUint32();
stream.readUint32();
this.duration = stream.readUint32();
}
stream.readUint32Array(2);
this.layer = stream.readInt16();
this.alternate_group = stream.readInt16();
this.volume = stream.readInt16() >> 8;
stream.readUint16();
this.matrix = stream.readInt32Array(9);
this.width = stream.readUint32();
this.height = stream.readUint32();
});
// file:src/parsing/tmax.js
BoxParser.createBoxCtor("tmax", function (stream) {
this.time = stream.readUint32();
});
// file:src/parsing/tmin.js
BoxParser.createBoxCtor("tmin", function (stream) {
this.time = stream.readUint32();
});
// file:src/parsing/totl.js
BoxParser.createBoxCtor("totl", function (stream) {
this.bytessent = stream.readUint32();
});
// file:src/parsing/tpay.js
BoxParser.createBoxCtor("tpay", function (stream) {
this.bytessent = stream.readUint32();
});
// file:src/parsing/tpyl.js
BoxParser.createBoxCtor("tpyl", function (stream) {
this.bytessent = stream.readUint64();
});
// file:src/parsing/TrackGroup.js
BoxParser.TrackGroupTypeBox.prototype.parse = function (stream) {
this.parseFullHeader(stream);
this.track_group_id = stream.readUint32();
};
// file:src/parsing/trackgroups/msrc.js
BoxParser.createTrackGroupCtor("msrc"); // file:src/parsing/TrakReference.js
BoxParser.TrackReferenceTypeBox = function (type, size, hdr_size, start) {
BoxParser.Box.call(this, type, size);
this.hdr_size = hdr_size;
this.start = start;
};
BoxParser.TrackReferenceTypeBox.prototype = new BoxParser.Box();
BoxParser.TrackReferenceTypeBox.prototype.parse = function (stream) {
this.track_ids = stream.readUint32Array((this.size - this.hdr_size) / 4);
};
// file:src/parsing/tref.js
BoxParser.trefBox.prototype.parse = function (stream) {
var ret;
var box;
while (stream.getPosition() < this.start + this.size) {
ret = BoxParser.parseOneBox(stream, true, this.size - (stream.getPosition() - this.start));
if (ret.code === BoxParser.OK) {
box = new BoxParser.TrackReferenceTypeBox(ret.type, ret.size, ret.hdr_size, ret.start);
if (box.write === BoxParser.Box.prototype.write && box.type !== "mdat") {
Log.info("BoxParser", "TrackReference " + box.type + " box writing not yet implemented, keeping unparsed data in memory for later write");
box.parseDataAndRewind(stream);
}
box.parse(stream);
this.boxes.push(box);
} else {
return;
}
}
};
// file:src/parsing/trep.js
BoxParser.createFullBoxCtor("trep", function (stream) {
this.track_ID = stream.readUint32();
this.boxes = [];
while (stream.getPosition() < this.start + this.size) {
ret = BoxParser.parseOneBox(stream, false, this.size - (stream.getPosition() - this.start));
if (ret.code === BoxParser.OK) {
box = ret.box;
this.boxes.push(box);
} else {
return;
}
}
});
// file:src/parsing/trex.js
BoxParser.createFullBoxCtor("trex", function (stream) {
this.track_id = stream.readUint32();
this.default_sample_description_index = stream.readUint32();
this.default_sample_duration = stream.readUint32();
this.default_sample_size = stream.readUint32();
this.default_sample_flags = stream.readUint32();
});
// file:src/parsing/trpy.js
BoxParser.createBoxCtor("trpy", function (stream) {
this.bytessent = stream.readUint64();
});
// file:src/parsing/trun.js
BoxParser.createFullBoxCtor("trun", function (stream) {
var readBytes = 0;
this.sample_count = stream.readUint32();
readBytes += 4;
if (this.size - this.hdr_size > readBytes && this.flags & BoxParser.TRUN_FLAGS_DATA_OFFSET) {
this.data_offset = stream.readInt32(); //signed
readBytes += 4;
} else {
this.data_offset = 0;
}
if (this.size - this.hdr_size > readBytes && this.flags & BoxParser.TRUN_FLAGS_FIRST_FLAG) {
this.first_sample_flags = stream.readUint32();
readBytes += 4;
} else {
this.first_sample_flags = 0;
}
this.sample_duration = [];
this.sample_size = [];
this.sample_flags = [];
this.sample_composition_time_offset = [];
if (this.size - this.hdr_size > readBytes) {
for (var i = 0; i < this.sample_count; i++) {
if (this.flags & BoxParser.TRUN_FLAGS_DURATION) {
this.sample_duration[i] = stream.readUint32();
}
if (this.flags & BoxParser.TRUN_FLAGS_SIZE) {
this.sample_size[i] = stream.readUint32();
}
if (this.flags & BoxParser.TRUN_FLAGS_FLAGS) {
this.sample_flags[i] = stream.readUint32();
}
if (this.flags & BoxParser.TRUN_FLAGS_CTS_OFFSET) {
if (this.version === 0) {
this.sample_composition_time_offset[i] = stream.readUint32();
} else {
this.sample_composition_time_offset[i] = stream.readInt32(); //signed
}
}
}
}
});
// file:src/parsing/tsel.js
BoxParser.createFullBoxCtor("tsel", function (stream) {
this.switch_group = stream.readUint32();
var count = (this.size - this.hdr_size - 4) / 4;
this.attribute_list = [];
for (var i = 0; i < count; i++) {
this.attribute_list[i] = stream.readUint32();
}
});
// file:src/parsing/txtC.js
BoxParser.createFullBoxCtor("txtC", function (stream) {
this.config = stream.readCString();
});
// file:src/parsing/tyco.js
BoxParser.createBoxCtor("tyco", function (stream) {
var count = (this.size - this.hdr_size) / 4;
this.compatible_brands = [];
for (var i = 0; i < count; i++) {
this.compatible_brands[i] = stream.readString(4);
}
});
// file:src/parsing/udes.js
BoxParser.createFullBoxCtor("udes", function (stream) {
this.lang = stream.readCString();
this.name = stream.readCString();
this.description = stream.readCString();
this.tags = stream.readCString();
});
// file:src/parsing/uncC.js
BoxParser.createFullBoxCtor("uncC", function (stream) {
var i;
this.profile = stream.readUint32();
if (this.version == 1) ; else if (this.version == 0) {
this.component_count = stream.readUint32();
this.component_index = [];
this.component_bit_depth_minus_one = [];
this.component_format = [];
this.component_align_size = [];
for (i = 0; i < this.component_count; i++) {
this.component_index.push(stream.readUint16());
this.component_bit_depth_minus_one.push(stream.readUint8());
this.component_format.push(stream.readUint8());
this.component_align_size.push(stream.readUint8());
}
this.sampling_type = stream.readUint8();
this.interleave_type = stream.readUint8();
this.block_size = stream.readUint8();
var flags = stream.readUint8();
this.component_little_endian = flags >> 7 & 0x1;
this.block_pad_lsb = flags >> 6 & 0x1;
this.block_little_endian = flags >> 5 & 0x1;
this.block_reversed = flags >> 4 & 0x1;
this.pad_unknown = flags >> 3 & 0x1;
this.pixel_size = stream.readUint32();
this.row_align_size = stream.readUint32();
this.tile_align_size = stream.readUint32();
this.num_tile_cols_minus_one = stream.readUint32();
this.num_tile_rows_minus_one = stream.readUint32();
}
});
// file:src/parsing/url.js
BoxParser.createFullBoxCtor("url ", function (stream) {
if (this.flags !== 0x000001) {
this.location = stream.readCString();
}
});
// file:src/parsing/urn.js
BoxParser.createFullBoxCtor("urn ", function (stream) {
this.name = stream.readCString();
if (this.size - this.hdr_size - this.name.length - 1 > 0) {
this.location = stream.readCString();
}
});
// file:src/parsing/uuid/piff/piffLsm.js
BoxParser.createUUIDBox("a5d40b30e81411ddba2f0800200c9a66", true, false, function (stream) {
this.LiveServerManifest = stream.readString(this.size - this.hdr_size).replace(/&/g, "&").replace(//g, ">").replace(/"/g, """).replace(/'/g, "'");
}); // file:src/parsing/uuid/piff/piffPssh.js
BoxParser.createUUIDBox("d08a4f1810f34a82b6c832d8aba183d3", true, false, function (stream) {
this.system_id = BoxParser.parseHex16(stream);
var datasize = stream.readUint32();
if (datasize > 0) {
this.data = stream.readUint8Array(datasize);
}
});
// file:src/parsing/uuid/piff/piffSenc.js
BoxParser.createUUIDBox("a2394f525a9b4f14a2446c427c648df4", true, false /*, function(stream) {
if (this.flags & 0x1) {
this.AlgorithmID = stream.readUint24();
this.IV_size = stream.readUint8();
this.KID = BoxParser.parseHex16(stream);
}
var sample_count = stream.readUint32();
this.samples = [];
for (var i = 0; i < sample_count; i++) {
var sample = {};
sample.InitializationVector = this.readUint8Array(this.IV_size*8);
if (this.flags & 0x2) {
sample.subsamples = [];
sample.NumberOfEntries = stream.readUint16();
for (var j = 0; j < sample.NumberOfEntries; j++) {
var subsample = {};
subsample.BytesOfClearData = stream.readUint16();
subsample.BytesOfProtectedData = stream.readUint32();
sample.subsamples.push(subsample);
}
}
this.samples.push(sample);
}
}*/);
// file:src/parsing/uuid/piff/piffTenc.js
BoxParser.createUUIDBox("8974dbce7be74c5184f97148f9882554", true, false, function (stream) {
this.default_AlgorithmID = stream.readUint24();
this.default_IV_size = stream.readUint8();
this.default_KID = BoxParser.parseHex16(stream);
}); // file:src/parsing/uuid/piff/piffTfrf.js
BoxParser.createUUIDBox("d4807ef2ca3946958e5426cb9e46a79f", true, false, function (stream) {
this.fragment_count = stream.readUint8();
this.entries = [];
for (var i = 0; i < this.fragment_count; i++) {
var entry = {};
var absolute_time = 0;
var absolute_duration = 0;
if (this.version === 1) {
absolute_time = stream.readUint64();
absolute_duration = stream.readUint64();
} else {
absolute_time = stream.readUint32();
absolute_duration = stream.readUint32();
}
entry.absolute_time = absolute_time;
entry.absolute_duration = absolute_duration;
this.entries.push(entry);
}
}); // file:src/parsing/uuid/piff/piffTfxd.js
BoxParser.createUUIDBox("6d1d9b0542d544e680e2141daff757b2", true, false, function (stream) {
if (this.version === 1) {
this.absolute_time = stream.readUint64();
this.duration = stream.readUint64();
} else {
this.absolute_time = stream.readUint32();
this.duration = stream.readUint32();
}
}); // file:src/parsing/vmhd.js
BoxParser.createFullBoxCtor("vmhd", function (stream) {
this.graphicsmode = stream.readUint16();
this.opcolor = stream.readUint16Array(3);
});
// file:src/parsing/vpcC.js
BoxParser.createFullBoxCtor("vpcC", function (stream) {
var tmp;
if (this.version === 1) {
this.profile = stream.readUint8();
this.level = stream.readUint8();
tmp = stream.readUint8();
this.bitDepth = tmp >> 4;
this.chromaSubsampling = tmp >> 1 & 0x7;
this.videoFullRangeFlag = tmp & 0x1;
this.colourPrimaries = stream.readUint8();
this.transferCharacteristics = stream.readUint8();
this.matrixCoefficients = stream.readUint8();
this.codecIntializationDataSize = stream.readUint16();
this.codecIntializationData = stream.readUint8Array(this.codecIntializationDataSize);
} else {
this.profile = stream.readUint8();
this.level = stream.readUint8();
tmp = stream.readUint8();
this.bitDepth = tmp >> 4 & 0xF;
this.colorSpace = tmp & 0xF;
tmp = stream.readUint8();
this.chromaSubsampling = tmp >> 4 & 0xF;
this.transferFunction = tmp >> 1 & 0x7;
this.videoFullRangeFlag = tmp & 0x1;
this.codecIntializationDataSize = stream.readUint16();
this.codecIntializationData = stream.readUint8Array(this.codecIntializationDataSize);
}
}); // file:src/parsing/vttC.js
BoxParser.createBoxCtor("vttC", function (stream) {
this.text = stream.readString(this.size - this.hdr_size);
});
// file:src/parsing/vvcC.js
BoxParser.createFullBoxCtor("vvcC", function (stream) {
var i, j;
// helper object to simplify extracting individual bits
var bitReader = {
held_bits: undefined,
num_held_bits: 0,
stream_read_1_bytes: function (strm) {
this.held_bits = strm.readUint8();
this.num_held_bits = 1 * 8;
},
stream_read_2_bytes: function (strm) {
this.held_bits = strm.readUint16();
this.num_held_bits = 2 * 8;
},
extract_bits: function (num_bits) {
var ret = this.held_bits >> this.num_held_bits - num_bits & (1 << num_bits) - 1;
this.num_held_bits -= num_bits;
return ret;
}
};
// VvcDecoderConfigurationRecord
bitReader.stream_read_1_bytes(stream);
bitReader.extract_bits(5); // reserved
this.lengthSizeMinusOne = bitReader.extract_bits(2);
this.ptl_present_flag = bitReader.extract_bits(1);
if (this.ptl_present_flag) {
bitReader.stream_read_2_bytes(stream);
this.ols_idx = bitReader.extract_bits(9);
this.num_sublayers = bitReader.extract_bits(3);
this.constant_frame_rate = bitReader.extract_bits(2);
this.chroma_format_idc = bitReader.extract_bits(2);
bitReader.stream_read_1_bytes(stream);
this.bit_depth_minus8 = bitReader.extract_bits(3);
bitReader.extract_bits(5); // reserved
// VvcPTLRecord
{
bitReader.stream_read_2_bytes(stream);
bitReader.extract_bits(2); // reserved
this.num_bytes_constraint_info = bitReader.extract_bits(6);
this.general_profile_idc = bitReader.extract_bits(7);
this.general_tier_flag = bitReader.extract_bits(1);
this.general_level_idc = stream.readUint8();
bitReader.stream_read_1_bytes(stream);
this.ptl_frame_only_constraint_flag = bitReader.extract_bits(1);
this.ptl_multilayer_enabled_flag = bitReader.extract_bits(1);
this.general_constraint_info = new Uint8Array(this.num_bytes_constraint_info);
if (this.num_bytes_constraint_info) {
for (i = 0; i < this.num_bytes_constraint_info - 1; i++) {
var cnstr1 = bitReader.extract_bits(6);
bitReader.stream_read_1_bytes(stream);
var cnstr2 = bitReader.extract_bits(2);
this.general_constraint_info[i] = cnstr1 << 2 | cnstr2;
}
this.general_constraint_info[this.num_bytes_constraint_info - 1] = bitReader.extract_bits(6);
} else {
//forbidden in spec!
bitReader.extract_bits(6);
}
if (this.num_sublayers > 1) {
bitReader.stream_read_1_bytes(stream);
this.ptl_sublayer_present_mask = 0;
for (j = this.num_sublayers - 2; j >= 0; --j) {
var val = bitReader.extract_bits(1);
this.ptl_sublayer_present_mask |= val << j;
}
for (j = this.num_sublayers; j <= 8 && this.num_sublayers > 1; ++j) {
bitReader.extract_bits(1); // ptl_reserved_zero_bit
}
this.sublayer_level_idc = [];
for (j = this.num_sublayers - 2; j >= 0; --j) {
if (this.ptl_sublayer_present_mask & 1 << j) {
this.sublayer_level_idc[j] = stream.readUint8();
}
}
}
this.ptl_num_sub_profiles = stream.readUint8();
this.general_sub_profile_idc = [];
if (this.ptl_num_sub_profiles) {
for (i = 0; i < this.ptl_num_sub_profiles; i++) {
this.general_sub_profile_idc.push(stream.readUint32());
}
}
} // end VvcPTLRecord
this.max_picture_width = stream.readUint16();
this.max_picture_height = stream.readUint16();
this.avg_frame_rate = stream.readUint16();
}
var VVC_NALU_OPI = 12;
var VVC_NALU_DEC_PARAM = 13;
this.nalu_arrays = [];
var num_of_arrays = stream.readUint8();
for (i = 0; i < num_of_arrays; i++) {
var nalu_array = [];
this.nalu_arrays.push(nalu_array);
bitReader.stream_read_1_bytes(stream);
nalu_array.completeness = bitReader.extract_bits(1);
bitReader.extract_bits(2); // reserved
nalu_array.nalu_type = bitReader.extract_bits(5);
var numNalus = 1;
if (nalu_array.nalu_type != VVC_NALU_DEC_PARAM && nalu_array.nalu_type != VVC_NALU_OPI) {
numNalus = stream.readUint16();
}
for (j = 0; j < numNalus; j++) {
var len = stream.readUint16();
nalu_array.push({
data: stream.readUint8Array(len),
length: len
});
}
}
});
// file:src/parsing/vvnC.js
BoxParser.createFullBoxCtor("vvnC", function (stream) {
// VvcNALUConfigBox
var tmp = strm.readUint8();
this.lengthSizeMinusOne = tmp & 0x3;
});
// file:src/box-codecs.js
BoxParser.SampleEntry.prototype.isVideo = function () {
return false;
};
BoxParser.SampleEntry.prototype.isAudio = function () {
return false;
};
BoxParser.SampleEntry.prototype.isSubtitle = function () {
return false;
};
BoxParser.SampleEntry.prototype.isMetadata = function () {
return false;
};
BoxParser.SampleEntry.prototype.isHint = function () {
return false;
};
BoxParser.SampleEntry.prototype.getCodec = function () {
return this.type.replace('.', '');
};
BoxParser.SampleEntry.prototype.getWidth = function () {
return "";
};
BoxParser.SampleEntry.prototype.getHeight = function () {
return "";
};
BoxParser.SampleEntry.prototype.getChannelCount = function () {
return "";
};
BoxParser.SampleEntry.prototype.getSampleRate = function () {
return "";
};
BoxParser.SampleEntry.prototype.getSampleSize = function () {
return "";
};
BoxParser.VisualSampleEntry.prototype.isVideo = function () {
return true;
};
BoxParser.VisualSampleEntry.prototype.getWidth = function () {
return this.width;
};
BoxParser.VisualSampleEntry.prototype.getHeight = function () {
return this.height;
};
BoxParser.AudioSampleEntry.prototype.isAudio = function () {
return true;
};
BoxParser.AudioSampleEntry.prototype.getChannelCount = function () {
return this.channel_count;
};
BoxParser.AudioSampleEntry.prototype.getSampleRate = function () {
return this.samplerate;
};
BoxParser.AudioSampleEntry.prototype.getSampleSize = function () {
return this.samplesize;
};
BoxParser.SubtitleSampleEntry.prototype.isSubtitle = function () {
return true;
};
BoxParser.MetadataSampleEntry.prototype.isMetadata = function () {
return true;
};
BoxParser.decimalToHex = function (d, padding) {
var hex = Number(d).toString(16);
padding = typeof padding === "undefined" || padding === null ? padding = 2 : padding;
while (hex.length < padding) {
hex = "0" + hex;
}
return hex;
};
BoxParser.avc1SampleEntry.prototype.getCodec = BoxParser.avc2SampleEntry.prototype.getCodec = BoxParser.avc3SampleEntry.prototype.getCodec = BoxParser.avc4SampleEntry.prototype.getCodec = function () {
var baseCodec = BoxParser.SampleEntry.prototype.getCodec.call(this);
if (this.avcC) {
return baseCodec + "." + BoxParser.decimalToHex(this.avcC.AVCProfileIndication) + "" + BoxParser.decimalToHex(this.avcC.profile_compatibility) + "" + BoxParser.decimalToHex(this.avcC.AVCLevelIndication);
} else {
return baseCodec;
}
};
BoxParser.hev1SampleEntry.prototype.getCodec = BoxParser.hvc1SampleEntry.prototype.getCodec = function () {
var i;
var baseCodec = BoxParser.SampleEntry.prototype.getCodec.call(this);
if (this.hvcC) {
baseCodec += '.';
switch (this.hvcC.general_profile_space) {
case 0:
baseCodec += '';
break;
case 1:
baseCodec += 'A';
break;
case 2:
baseCodec += 'B';
break;
case 3:
baseCodec += 'C';
break;
}
baseCodec += this.hvcC.general_profile_idc;
baseCodec += '.';
var val = this.hvcC.general_profile_compatibility;
var reversed = 0;
for (i = 0; i < 32; i++) {
reversed |= val & 1;
if (i == 31) break;
reversed <<= 1;
val >>= 1;
}
baseCodec += BoxParser.decimalToHex(reversed, 0);
baseCodec += '.';
if (this.hvcC.general_tier_flag === 0) {
baseCodec += 'L';
} else {
baseCodec += 'H';
}
baseCodec += this.hvcC.general_level_idc;
var hasByte = false;
var constraint_string = "";
for (i = 5; i >= 0; i--) {
if (this.hvcC.general_constraint_indicator[i] || hasByte) {
constraint_string = "." + BoxParser.decimalToHex(this.hvcC.general_constraint_indicator[i], 0) + constraint_string;
hasByte = true;
}
}
baseCodec += constraint_string;
}
return baseCodec;
};
BoxParser.vvc1SampleEntry.prototype.getCodec = BoxParser.vvi1SampleEntry.prototype.getCodec = function () {
var i;
var baseCodec = BoxParser.SampleEntry.prototype.getCodec.call(this);
if (this.vvcC) {
baseCodec += '.' + this.vvcC.general_profile_idc;
if (this.vvcC.general_tier_flag) {
baseCodec += '.H';
} else {
baseCodec += '.L';
}
baseCodec += this.vvcC.general_level_idc;
var constraint_string = "";
if (this.vvcC.general_constraint_info) {
var bytes = [];
var byte = 0;
byte |= this.vvcC.ptl_frame_only_constraint << 7;
byte |= this.vvcC.ptl_multilayer_enabled << 6;
var last_nonzero;
for (i = 0; i < this.vvcC.general_constraint_info.length; ++i) {
byte |= this.vvcC.general_constraint_info[i] >> 2 & 0x3f;
bytes.push(byte);
if (byte) {
last_nonzero = i;
}
byte = this.vvcC.general_constraint_info[i] >> 2 & 0x03;
}
if (last_nonzero === undefined) {
constraint_string = ".CA";
} else {
constraint_string = ".C";
var base32_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
var held_bits = 0;
var num_held_bits = 0;
for (i = 0; i <= last_nonzero; ++i) {
held_bits = held_bits << 8 | bytes[i];
num_held_bits += 8;
while (num_held_bits >= 5) {
var val = held_bits >> num_held_bits - 5 & 0x1f;
constraint_string += base32_chars[val];
num_held_bits -= 5;
held_bits &= (1 << num_held_bits) - 1;
}
}
if (num_held_bits) {
held_bits <<= 5 - num_held_bits; // right-pad with zeros to 5 bits (is this correct?)
constraint_string += base32_chars[held_bits & 0x1f];
}
}
}
baseCodec += constraint_string;
}
return baseCodec;
};
BoxParser.mp4aSampleEntry.prototype.getCodec = function () {
var baseCodec = BoxParser.SampleEntry.prototype.getCodec.call(this);
if (this.esds && this.esds.esd) {
var oti = this.esds.esd.getOTI();
var dsi = this.esds.esd.getAudioConfig();
return baseCodec + "." + BoxParser.decimalToHex(oti) + (dsi ? "." + dsi : "");
} else {
return baseCodec;
}
};
BoxParser.stxtSampleEntry.prototype.getCodec = function () {
var baseCodec = BoxParser.SampleEntry.prototype.getCodec.call(this);
if (this.mime_format) {
return baseCodec + "." + this.mime_format;
} else {
return baseCodec;
}
};
BoxParser.vp08SampleEntry.prototype.getCodec = BoxParser.vp09SampleEntry.prototype.getCodec = function () {
var baseCodec = BoxParser.SampleEntry.prototype.getCodec.call(this);
var level = this.vpcC.level;
if (level == 0) {
level = "00";
}
var bitDepth = this.vpcC.bitDepth;
if (bitDepth == 8) {
bitDepth = "08";
}
return baseCodec + ".0" + this.vpcC.profile + "." + level + "." + bitDepth;
};
BoxParser.av01SampleEntry.prototype.getCodec = function () {
var baseCodec = BoxParser.SampleEntry.prototype.getCodec.call(this);
var level = this.av1C.seq_level_idx_0;
if (level < 10) {
level = "0" + level;
}
var bitdepth;
if (this.av1C.seq_profile === 2 && this.av1C.high_bitdepth === 1) {
bitdepth = this.av1C.twelve_bit === 1 ? "12" : "10";
} else if (this.av1C.seq_profile <= 2) {
bitdepth = this.av1C.high_bitdepth === 1 ? "10" : "08";
}
// TODO need to parse the SH to find color config
return baseCodec + "." + this.av1C.seq_profile + "." + level + (this.av1C.seq_tier_0 ? "H" : "M") + "." + bitdepth; //+"."+this.av1C.monochrome+"."+this.av1C.chroma_subsampling_x+""+this.av1C.chroma_subsampling_y+""+this.av1C.chroma_sample_position;
};
// file:src/box-write.js
/*
* Copyright (c) Telecom ParisTech/TSI/MM/GPAC Cyril Concolato
* License: BSD-3-Clause (see LICENSE file)
*/
BoxParser.Box.prototype.writeHeader = function (stream, msg) {
this.size += 8;
if (this.size > MAX_SIZE) {
this.size += 8;
}
if (this.type === "uuid") {
this.size += 16;
}
Log.debug("BoxWriter", "Writing box " + this.type + " of size: " + this.size + " at position " + stream.getPosition() + (msg || ""));
if (this.size > MAX_SIZE) {
stream.writeUint32(1);
} else {
this.sizePosition = stream.getPosition();
stream.writeUint32(this.size);
}
stream.writeString(this.type, null, 4);
if (this.type === "uuid") {
stream.writeUint8Array(this.uuid);
}
if (this.size > MAX_SIZE) {
stream.writeUint64(this.size);
}
};
BoxParser.FullBox.prototype.writeHeader = function (stream) {
this.size += 4;
BoxParser.Box.prototype.writeHeader.call(this, stream, " v=" + this.version + " f=" + this.flags);
stream.writeUint8(this.version);
stream.writeUint24(this.flags);
};
BoxParser.Box.prototype.write = function (stream) {
if (this.type === "mdat") {
/* TODO: fix this */
if (this.data) {
this.size = this.data.length;
this.writeHeader(stream);
stream.writeUint8Array(this.data);
}
} else {
this.size = this.data ? this.data.length : 0;
this.writeHeader(stream);
if (this.data) {
stream.writeUint8Array(this.data);
}
}
};
BoxParser.ContainerBox.prototype.write = function (stream) {
this.size = 0;
this.writeHeader(stream);
for (var i = 0; i < this.boxes.length; i++) {
if (this.boxes[i]) {
this.boxes[i].write(stream);
this.size += this.boxes[i].size;
}
}
/* adjusting the size, now that all sub-boxes are known */
Log.debug("BoxWriter", "Adjusting box " + this.type + " with new size " + this.size);
stream.adjustUint32(this.sizePosition, this.size);
};
BoxParser.TrackReferenceTypeBox.prototype.write = function (stream) {
this.size = this.track_ids.length * 4;
this.writeHeader(stream);
stream.writeUint32Array(this.track_ids);
};
// file:src/writing/avcC.js
BoxParser.avcCBox.prototype.write = function (stream) {
var i;
this.size = 7;
for (i = 0; i < this.SPS.length; i++) {
this.size += 2 + this.SPS[i].length;
}
for (i = 0; i < this.PPS.length; i++) {
this.size += 2 + this.PPS[i].length;
}
if (this.ext) {
this.size += this.ext.length;
}
this.writeHeader(stream);
stream.writeUint8(this.configurationVersion);
stream.writeUint8(this.AVCProfileIndication);
stream.writeUint8(this.profile_compatibility);
stream.writeUint8(this.AVCLevelIndication);
stream.writeUint8(this.lengthSizeMinusOne + (63 << 2));
stream.writeUint8(this.SPS.length + (7 << 5));
for (i = 0; i < this.SPS.length; i++) {
stream.writeUint16(this.SPS[i].length);
stream.writeUint8Array(this.SPS[i].nalu);
}
stream.writeUint8(this.PPS.length);
for (i = 0; i < this.PPS.length; i++) {
stream.writeUint16(this.PPS[i].length);
stream.writeUint8Array(this.PPS[i].nalu);
}
if (this.ext) {
stream.writeUint8Array(this.ext);
}
};
// file:src/writing/co64.js
BoxParser.co64Box.prototype.write = function (stream) {
var i;
this.version = 0;
this.flags = 0;
this.size = 4 + 8 * this.chunk_offsets.length;
this.writeHeader(stream);
stream.writeUint32(this.chunk_offsets.length);
for (i = 0; i < this.chunk_offsets.length; i++) {
stream.writeUint64(this.chunk_offsets[i]);
}
};
// file:src/writing/cslg.js
BoxParser.cslgBox.prototype.write = function (stream) {
this.version = 0;
this.flags = 0;
this.size = 4 * 5;
this.writeHeader(stream);
stream.writeInt32(this.compositionToDTSShift);
stream.writeInt32(this.leastDecodeToDisplayDelta);
stream.writeInt32(this.greatestDecodeToDisplayDelta);
stream.writeInt32(this.compositionStartTime);
stream.writeInt32(this.compositionEndTime);
};
// file:src/writing/ctts.js
BoxParser.cttsBox.prototype.write = function (stream) {
var i;
this.version = 0;
this.flags = 0;
this.size = 4 + 8 * this.sample_counts.length;
this.writeHeader(stream);
stream.writeUint32(this.sample_counts.length);
for (i = 0; i < this.sample_counts.length; i++) {
stream.writeUint32(this.sample_counts[i]);
if (this.version === 1) {
stream.writeInt32(this.sample_offsets[i]); /* signed */
} else {
stream.writeUint32(this.sample_offsets[i]); /* unsigned */
}
}
};
// file:src/writing/dref.js
BoxParser.drefBox.prototype.write = function (stream) {
this.version = 0;
this.flags = 0;
this.size = 4; //
this.writeHeader(stream);
stream.writeUint32(this.entries.length);
for (var i = 0; i < this.entries.length; i++) {
this.entries[i].write(stream);
this.size += this.entries[i].size;
}
/* adjusting the size, now that all sub-boxes are known */
Log.debug("BoxWriter", "Adjusting box " + this.type + " with new size " + this.size);
stream.adjustUint32(this.sizePosition, this.size);
};
// file:src/writing/elng.js
BoxParser.elngBox.prototype.write = function (stream) {
this.version = 0;
this.flags = 0;
this.size = this.extended_language.length;
this.writeHeader(stream);
stream.writeString(this.extended_language);
};
// file:src/writing/elst.js
BoxParser.elstBox.prototype.write = function (stream) {
this.version = 0;
this.flags = 0;
this.size = 4 + 12 * this.entries.length;
this.writeHeader(stream);
stream.writeUint32(this.entries.length);
for (var i = 0; i < this.entries.length; i++) {
var entry = this.entries[i];
stream.writeUint32(entry.segment_duration);
stream.writeInt32(entry.media_time);
stream.writeInt16(entry.media_rate_integer);
stream.writeInt16(entry.media_rate_fraction);
}
};
// file:src/writing/emsg.js
BoxParser.emsgBox.prototype.write = function (stream) {
this.version = 0;
this.flags = 0;
this.size = 4 * 4 + this.message_data.length + (this.scheme_id_uri.length + 1) + (this.value.length + 1);
this.writeHeader(stream);
stream.writeCString(this.scheme_id_uri);
stream.writeCString(this.value);
stream.writeUint32(this.timescale);
stream.writeUint32(this.presentation_time_delta);
stream.writeUint32(this.event_duration);
stream.writeUint32(this.id);
stream.writeUint8Array(this.message_data);
};
// file:src/writing/ftyp.js
BoxParser.ftypBox.prototype.write = function (stream) {
this.size = 8 + 4 * this.compatible_brands.length;
this.writeHeader(stream);
stream.writeString(this.major_brand, null, 4);
stream.writeUint32(this.minor_version);
for (var i = 0; i < this.compatible_brands.length; i++) {
stream.writeString(this.compatible_brands[i], null, 4);
}
};
// file:src/writing/hdlr.js
BoxParser.hdlrBox.prototype.write = function (stream) {
this.size = 5 * 4 + this.name.length + 1;
this.version = 0;
this.flags = 0;
this.writeHeader(stream);
stream.writeUint32(0);
stream.writeString(this.handler, null, 4);
stream.writeUint32(0);
stream.writeUint32(0);
stream.writeUint32(0);
stream.writeCString(this.name);
};
// file:src/writing/hvcC.js
BoxParser.hvcCBox.prototype.write = function (stream) {
var i, j;
this.size = 23;
for (i = 0; i < this.nalu_arrays.length; i++) {
this.size += 3;
for (j = 0; j < this.nalu_arrays[i].length; j++) {
this.size += 2 + this.nalu_arrays[i][j].data.length;
}
}
this.writeHeader(stream);
stream.writeUint8(this.configurationVersion);
stream.writeUint8(this.general_profile_space << 6 + this.general_tier_flag << 5 + this.general_profile_idc);
stream.writeUint32(this.general_profile_compatibility);
stream.writeUint8Array(this.general_constraint_indicator);
stream.writeUint8(this.general_level_idc);
stream.writeUint16(this.min_spatial_segmentation_idc + (15 << 24));
stream.writeUint8(this.parallelismType + (63 << 2));
stream.writeUint8(this.chroma_format_idc + (63 << 2));
stream.writeUint8(this.bit_depth_luma_minus8 + (31 << 3));
stream.writeUint8(this.bit_depth_chroma_minus8 + (31 << 3));
stream.writeUint16(this.avgFrameRate);
stream.writeUint8((this.constantFrameRate << 6) + (this.numTemporalLayers << 3) + (this.temporalIdNested << 2) + this.lengthSizeMinusOne);
stream.writeUint8(this.nalu_arrays.length);
for (i = 0; i < this.nalu_arrays.length; i++) {
// bit(1) array_completeness + bit(1) reserved = 0 + bit(6) nal_unit_type
stream.writeUint8((this.nalu_arrays[i].completeness << 7) + this.nalu_arrays[i].nalu_type);
stream.writeUint16(this.nalu_arrays[i].length);
for (j = 0; j < this.nalu_arrays[i].length; j++) {
stream.writeUint16(this.nalu_arrays[i][j].data.length);
stream.writeUint8Array(this.nalu_arrays[i][j].data);
}
}
};
// file:src/writing/kind.js
BoxParser.kindBox.prototype.write = function (stream) {
this.version = 0;
this.flags = 0;
this.size = this.schemeURI.length + 1 + (this.value.length + 1);
this.writeHeader(stream);
stream.writeCString(this.schemeURI);
stream.writeCString(this.value);
};
// file:src/writing/mdhd.js
BoxParser.mdhdBox.prototype.write = function (stream) {
this.size = 4 * 4 + 2 * 2;
this.flags = 0;
this.version = 0;
this.writeHeader(stream);
stream.writeUint32(this.creation_time);
stream.writeUint32(this.modification_time);
stream.writeUint32(this.timescale);
stream.writeUint32(this.duration);
stream.writeUint16(this.language);
stream.writeUint16(0);
};
// file:src/writing/mehd.js
BoxParser.mehdBox.prototype.write = function (stream) {
this.version = 0;
this.flags = 0;
this.size = 4;
this.writeHeader(stream);
stream.writeUint32(this.fragment_duration);
};
// file:src/writing/mfhd.js
BoxParser.mfhdBox.prototype.write = function (stream) {
this.version = 0;
this.flags = 0;
this.size = 4;
this.writeHeader(stream);
stream.writeUint32(this.sequence_number);
};
// file:src/writing/mvhd.js
BoxParser.mvhdBox.prototype.write = function (stream) {
this.version = 0;
this.flags = 0;
this.size = 23 * 4 + 2 * 2;
this.writeHeader(stream);
stream.writeUint32(this.creation_time);
stream.writeUint32(this.modification_time);
stream.writeUint32(this.timescale);
stream.writeUint32(this.duration);
stream.writeUint32(this.rate);
stream.writeUint16(this.volume << 8);
stream.writeUint16(0);
stream.writeUint32(0);
stream.writeUint32(0);
stream.writeUint32Array(this.matrix);
stream.writeUint32(0);
stream.writeUint32(0);
stream.writeUint32(0);
stream.writeUint32(0);
stream.writeUint32(0);
stream.writeUint32(0);
stream.writeUint32(this.next_track_id);
};
// file:src/writing/sampleentry.js
BoxParser.SampleEntry.prototype.writeHeader = function (stream) {
this.size = 8;
BoxParser.Box.prototype.writeHeader.call(this, stream);
stream.writeUint8(0);
stream.writeUint8(0);
stream.writeUint8(0);
stream.writeUint8(0);
stream.writeUint8(0);
stream.writeUint8(0);
stream.writeUint16(this.data_reference_index);
};
BoxParser.SampleEntry.prototype.writeFooter = function (stream) {
for (var i = 0; i < this.boxes.length; i++) {
this.boxes[i].write(stream);
this.size += this.boxes[i].size;
}
Log.debug("BoxWriter", "Adjusting box " + this.type + " with new size " + this.size);
stream.adjustUint32(this.sizePosition, this.size);
};
BoxParser.SampleEntry.prototype.write = function (stream) {
this.writeHeader(stream);
stream.writeUint8Array(this.data);
this.size += this.data.length;
Log.debug("BoxWriter", "Adjusting box " + this.type + " with new size " + this.size);
stream.adjustUint32(this.sizePosition, this.size);
};
BoxParser.VisualSampleEntry.prototype.write = function (stream) {
this.writeHeader(stream);
this.size += 2 * 7 + 6 * 4 + 32;
stream.writeUint16(0);
stream.writeUint16(0);
stream.writeUint32(0);
stream.writeUint32(0);
stream.writeUint32(0);
stream.writeUint16(this.width);
stream.writeUint16(this.height);
stream.writeUint32(this.horizresolution);
stream.writeUint32(this.vertresolution);
stream.writeUint32(0);
stream.writeUint16(this.frame_count);
stream.writeUint8(Math.min(31, this.compressorname.length));
stream.writeString(this.compressorname, null, 31);
stream.writeUint16(this.depth);
stream.writeInt16(-1);
this.writeFooter(stream);
};
BoxParser.AudioSampleEntry.prototype.write = function (stream) {
this.writeHeader(stream);
this.size += 2 * 4 + 3 * 4;
stream.writeUint32(0);
stream.writeUint32(0);
stream.writeUint16(this.channel_count);
stream.writeUint16(this.samplesize);
stream.writeUint16(0);
stream.writeUint16(0);
stream.writeUint32(this.samplerate << 16);
this.writeFooter(stream);
};
BoxParser.stppSampleEntry.prototype.write = function (stream) {
this.writeHeader(stream);
this.size += this.namespace.length + 1 + this.schema_location.length + 1 + this.auxiliary_mime_types.length + 1;
stream.writeCString(this.namespace);
stream.writeCString(this.schema_location);
stream.writeCString(this.auxiliary_mime_types);
this.writeFooter(stream);
};
// file:src/writing/samplegroups/samplegroup.js
BoxParser.SampleGroupEntry.prototype.write = function (stream) {
stream.writeUint8Array(this.data);
};
// file:src/writing/sbgp.js
BoxParser.sbgpBox.prototype.write = function (stream) {
this.version = 1;
this.flags = 0;
this.size = 12 + 8 * this.entries.length;
this.writeHeader(stream);
stream.writeString(this.grouping_type, null, 4);
stream.writeUint32(this.grouping_type_parameter);
stream.writeUint32(this.entries.length);
for (var i = 0; i < this.entries.length; i++) {
var entry = this.entries[i];
stream.writeInt32(entry.sample_count);
stream.writeInt32(entry.group_description_index);
}
};
// file:src/writing/sgpd.js
BoxParser.sgpdBox.prototype.write = function (stream) {
var i;
var entry;
// leave version as read
// this.version;
this.flags = 0;
this.size = 12;
for (i = 0; i < this.entries.length; i++) {
entry = this.entries[i];
if (this.version === 1) {
if (this.default_length === 0) {
this.size += 4;
}
this.size += entry.data.length;
}
}
this.writeHeader(stream);
stream.writeString(this.grouping_type, null, 4);
if (this.version === 1) {
stream.writeUint32(this.default_length);
}
if (this.version >= 2) {
stream.writeUint32(this.default_sample_description_index);
}
stream.writeUint32(this.entries.length);
for (i = 0; i < this.entries.length; i++) {
entry = this.entries[i];
if (this.version === 1) {
if (this.default_length === 0) {
stream.writeUint32(entry.description_length);
}
}
entry.write(stream);
}
};
// file:src/writing/sidx.js
BoxParser.sidxBox.prototype.write = function (stream) {
this.version = 0;
this.flags = 0;
this.size = 4 * 4 + 2 + 2 + 12 * this.references.length;
this.writeHeader(stream);
stream.writeUint32(this.reference_ID);
stream.writeUint32(this.timescale);
stream.writeUint32(this.earliest_presentation_time);
stream.writeUint32(this.first_offset);
stream.writeUint16(0);
stream.writeUint16(this.references.length);
for (var i = 0; i < this.references.length; i++) {
var ref = this.references[i];
stream.writeUint32(ref.reference_type << 31 | ref.referenced_size);
stream.writeUint32(ref.subsegment_duration);
stream.writeUint32(ref.starts_with_SAP << 31 | ref.SAP_type << 28 | ref.SAP_delta_time);
}
};
// file:src/writing/smhd.js
BoxParser.smhdBox.prototype.write = function (stream) {
this.version = 0;
this.flags = 1;
this.size = 4;
this.writeHeader(stream);
stream.writeUint16(this.balance);
stream.writeUint16(0);
};
// file:src/writing/stco.js
BoxParser.stcoBox.prototype.write = function (stream) {
this.version = 0;
this.flags = 0;
this.size = 4 + 4 * this.chunk_offsets.length;
this.writeHeader(stream);
stream.writeUint32(this.chunk_offsets.length);
stream.writeUint32Array(this.chunk_offsets);
};
// file:src/writing/stsc.js
BoxParser.stscBox.prototype.write = function (stream) {
var i;
this.version = 0;
this.flags = 0;
this.size = 4 + 12 * this.first_chunk.length;
this.writeHeader(stream);
stream.writeUint32(this.first_chunk.length);
for (i = 0; i < this.first_chunk.length; i++) {
stream.writeUint32(this.first_chunk[i]);
stream.writeUint32(this.samples_per_chunk[i]);
stream.writeUint32(this.sample_description_index[i]);
}
};
// file:src/writing/stsd.js
BoxParser.stsdBox.prototype.write = function (stream) {
var i;
this.version = 0;
this.flags = 0;
this.size = 0;
this.writeHeader(stream);
stream.writeUint32(this.entries.length);
this.size += 4;
for (i = 0; i < this.entries.length; i++) {
this.entries[i].write(stream);
this.size += this.entries[i].size;
}
/* adjusting the size, now that all sub-boxes are known */
Log.debug("BoxWriter", "Adjusting box " + this.type + " with new size " + this.size);
stream.adjustUint32(this.sizePosition, this.size);
};
// file:src/writing/stsh.js
BoxParser.stshBox.prototype.write = function (stream) {
var i;
this.version = 0;
this.flags = 0;
this.size = 4 + 8 * this.shadowed_sample_numbers.length;
this.writeHeader(stream);
stream.writeUint32(this.shadowed_sample_numbers.length);
for (i = 0; i < this.shadowed_sample_numbers.length; i++) {
stream.writeUint32(this.shadowed_sample_numbers[i]);
stream.writeUint32(this.sync_sample_numbers[i]);
}
};
// file:src/writing/stss.js
BoxParser.stssBox.prototype.write = function (stream) {
this.version = 0;
this.flags = 0;
this.size = 4 + 4 * this.sample_numbers.length;
this.writeHeader(stream);
stream.writeUint32(this.sample_numbers.length);
stream.writeUint32Array(this.sample_numbers);
};
// file:src/writing/stsz.js
BoxParser.stszBox.prototype.write = function (stream) {
var i;
var constant = true;
this.version = 0;
this.flags = 0;
if (this.sample_sizes.length > 0) {
i = 0;
while (i + 1 < this.sample_sizes.length) {
if (this.sample_sizes[i + 1] !== this.sample_sizes[0]) {
constant = false;
break;
} else {
i++;
}
}
} else {
constant = false;
}
this.size = 8;
if (!constant) {
this.size += 4 * this.sample_sizes.length;
}
this.writeHeader(stream);
if (!constant) {
stream.writeUint32(0);
} else {
stream.writeUint32(this.sample_sizes[0]);
}
stream.writeUint32(this.sample_sizes.length);
if (!constant) {
stream.writeUint32Array(this.sample_sizes);
}
};
// file:src/writing/stts.js
BoxParser.sttsBox.prototype.write = function (stream) {
var i;
this.version = 0;
this.flags = 0;
this.size = 4 + 8 * this.sample_counts.length;
this.writeHeader(stream);
stream.writeUint32(this.sample_counts.length);
for (i = 0; i < this.sample_counts.length; i++) {
stream.writeUint32(this.sample_counts[i]);
stream.writeUint32(this.sample_deltas[i]);
}
};
// file:src/writing/tfdt.js
BoxParser.tfdtBox.prototype.write = function (stream) {
var UINT32_MAX = Math.pow(2, 32) - 1;
// use version 1 if baseMediaDecodeTime does not fit 32 bits
this.version = this.baseMediaDecodeTime > UINT32_MAX ? 1 : 0;
this.flags = 0;
this.size = 4;
if (this.version === 1) {
this.size += 4;
}
this.writeHeader(stream);
if (this.version === 1) {
stream.writeUint64(this.baseMediaDecodeTime);
} else {
stream.writeUint32(this.baseMediaDecodeTime);
}
};
// file:src/writing/tfhd.js
BoxParser.tfhdBox.prototype.write = function (stream) {
this.version = 0;
this.size = 4;
if (this.flags & BoxParser.TFHD_FLAG_BASE_DATA_OFFSET) {
this.size += 8;
}
if (this.flags & BoxParser.TFHD_FLAG_SAMPLE_DESC) {
this.size += 4;
}
if (this.flags & BoxParser.TFHD_FLAG_SAMPLE_DUR) {
this.size += 4;
}
if (this.flags & BoxParser.TFHD_FLAG_SAMPLE_SIZE) {
this.size += 4;
}
if (this.flags & BoxParser.TFHD_FLAG_SAMPLE_FLAGS) {
this.size += 4;
}
this.writeHeader(stream);
stream.writeUint32(this.track_id);
if (this.flags & BoxParser.TFHD_FLAG_BASE_DATA_OFFSET) {
stream.writeUint64(this.base_data_offset);
}
if (this.flags & BoxParser.TFHD_FLAG_SAMPLE_DESC) {
stream.writeUint32(this.default_sample_description_index);
}
if (this.flags & BoxParser.TFHD_FLAG_SAMPLE_DUR) {
stream.writeUint32(this.default_sample_duration);
}
if (this.flags & BoxParser.TFHD_FLAG_SAMPLE_SIZE) {
stream.writeUint32(this.default_sample_size);
}
if (this.flags & BoxParser.TFHD_FLAG_SAMPLE_FLAGS) {
stream.writeUint32(this.default_sample_flags);
}
};
// file:src/writing/tkhd.js
BoxParser.tkhdBox.prototype.write = function (stream) {
this.version = 0;
//this.flags = 0;
this.size = 4 * 18 + 2 * 4;
this.writeHeader(stream);
stream.writeUint32(this.creation_time);
stream.writeUint32(this.modification_time);
stream.writeUint32(this.track_id);
stream.writeUint32(0);
stream.writeUint32(this.duration);
stream.writeUint32(0);
stream.writeUint32(0);
stream.writeInt16(this.layer);
stream.writeInt16(this.alternate_group);
stream.writeInt16(this.volume << 8);
stream.writeUint16(0);
stream.writeInt32Array(this.matrix);
stream.writeUint32(this.width);
stream.writeUint32(this.height);
};
// file:src/writing/trex.js
BoxParser.trexBox.prototype.write = function (stream) {
this.version = 0;
this.flags = 0;
this.size = 4 * 5;
this.writeHeader(stream);
stream.writeUint32(this.track_id);
stream.writeUint32(this.default_sample_description_index);
stream.writeUint32(this.default_sample_duration);
stream.writeUint32(this.default_sample_size);
stream.writeUint32(this.default_sample_flags);
};
// file:src/writing/trun.js
BoxParser.trunBox.prototype.write = function (stream) {
this.version = 0;
this.size = 4;
if (this.flags & BoxParser.TRUN_FLAGS_DATA_OFFSET) {
this.size += 4;
}
if (this.flags & BoxParser.TRUN_FLAGS_FIRST_FLAG) {
this.size += 4;
}
if (this.flags & BoxParser.TRUN_FLAGS_DURATION) {
this.size += 4 * this.sample_duration.length;
}
if (this.flags & BoxParser.TRUN_FLAGS_SIZE) {
this.size += 4 * this.sample_size.length;
}
if (this.flags & BoxParser.TRUN_FLAGS_FLAGS) {
this.size += 4 * this.sample_flags.length;
}
if (this.flags & BoxParser.TRUN_FLAGS_CTS_OFFSET) {
this.size += 4 * this.sample_composition_time_offset.length;
}
this.writeHeader(stream);
stream.writeUint32(this.sample_count);
if (this.flags & BoxParser.TRUN_FLAGS_DATA_OFFSET) {
this.data_offset_position = stream.getPosition();
stream.writeInt32(this.data_offset); //signed
}
if (this.flags & BoxParser.TRUN_FLAGS_FIRST_FLAG) {
stream.writeUint32(this.first_sample_flags);
}
for (var i = 0; i < this.sample_count; i++) {
if (this.flags & BoxParser.TRUN_FLAGS_DURATION) {
stream.writeUint32(this.sample_duration[i]);
}
if (this.flags & BoxParser.TRUN_FLAGS_SIZE) {
stream.writeUint32(this.sample_size[i]);
}
if (this.flags & BoxParser.TRUN_FLAGS_FLAGS) {
stream.writeUint32(this.sample_flags[i]);
}
if (this.flags & BoxParser.TRUN_FLAGS_CTS_OFFSET) {
if (this.version === 0) {
stream.writeUint32(this.sample_composition_time_offset[i]);
} else {
stream.writeInt32(this.sample_composition_time_offset[i]); //signed
}
}
}
};
// file:src/writing/url.js
BoxParser["url Box"].prototype.write = function (stream) {
this.version = 0;
if (this.location) {
this.flags = 0;
this.size = this.location.length + 1;
} else {
this.flags = 0x000001;
this.size = 0;
}
this.writeHeader(stream);
if (this.location) {
stream.writeCString(this.location);
}
};
// file:src/writing/urn.js
BoxParser["urn Box"].prototype.write = function (stream) {
this.version = 0;
this.flags = 0;
this.size = this.name.length + 1 + (this.location ? this.location.length + 1 : 0);
this.writeHeader(stream);
stream.writeCString(this.name);
if (this.location) {
stream.writeCString(this.location);
}
};
// file:src/writing/vmhd.js
BoxParser.vmhdBox.prototype.write = function (stream) {
this.version = 0;
this.flags = 1;
this.size = 8;
this.writeHeader(stream);
stream.writeUint16(this.graphicsmode);
stream.writeUint16Array(this.opcolor);
};
// file:src/box-unpack.js
/*
* Copyright (c) Telecom ParisTech/TSI/MM/GPAC Cyril Concolato
* License: BSD-3-Clause (see LICENSE file)
*/
BoxParser.cttsBox.prototype.unpack = function (samples) {
var i, j, k;
k = 0;
for (i = 0; i < this.sample_counts.length; i++) {
for (j = 0; j < this.sample_counts[i]; j++) {
samples[k].pts = samples[k].dts + this.sample_offsets[i];
k++;
}
}
};
BoxParser.sttsBox.prototype.unpack = function (samples) {
var i, j, k;
k = 0;
for (i = 0; i < this.sample_counts.length; i++) {
for (j = 0; j < this.sample_counts[i]; j++) {
if (k === 0) {
samples[k].dts = 0;
} else {
samples[k].dts = samples[k - 1].dts + this.sample_deltas[i];
}
k++;
}
}
};
BoxParser.stcoBox.prototype.unpack = function (samples) {
var i;
for (i = 0; i < this.chunk_offsets.length; i++) {
samples[i].offset = this.chunk_offsets[i];
}
};
BoxParser.stscBox.prototype.unpack = function (samples) {
var i, j, k, l, m;
l = 0;
m = 0;
for (i = 0; i < this.first_chunk.length; i++) {
for (j = 0; j < (i + 1 < this.first_chunk.length ? this.first_chunk[i + 1] : Infinity); j++) {
m++;
for (k = 0; k < this.samples_per_chunk[i]; k++) {
if (samples[l]) {
samples[l].description_index = this.sample_description_index[i];
samples[l].chunk_index = m;
} else {
return;
}
l++;
}
}
}
};
BoxParser.stszBox.prototype.unpack = function (samples) {
var i;
for (i = 0; i < this.sample_sizes.length; i++) {
samples[i].size = this.sample_sizes[i];
}
};
// file:src/box-diff.js
BoxParser.DIFF_BOXES_PROP_NAMES = ["boxes", "entries", "references", "subsamples", "items", "item_infos", "extents", "associations", "subsegments", "ranges", "seekLists", "seekPoints", "esd", "levels"];
BoxParser.DIFF_PRIMITIVE_ARRAY_PROP_NAMES = ["compatible_brands", "matrix", "opcolor", "sample_counts", "sample_counts", "sample_deltas", "first_chunk", "samples_per_chunk", "sample_sizes", "chunk_offsets", "sample_offsets", "sample_description_index", "sample_duration"];
BoxParser.boxEqualFields = function (box_a, box_b) {
if (box_a && !box_b) return false;
var prop;
for (prop in box_a) {
if (BoxParser.DIFF_BOXES_PROP_NAMES.indexOf(prop) > -1) {
continue;
// } else if (excluded_fields && excluded_fields.indexOf(prop) > -1) {
// continue;
} else if (box_a[prop] instanceof BoxParser.Box || box_b[prop] instanceof BoxParser.Box) {
continue;
} else if (typeof box_a[prop] === "undefined" || typeof box_b[prop] === "undefined") {
continue;
} else if (typeof box_a[prop] === "function" || typeof box_b[prop] === "function") {
continue;
} else if (box_a.subBoxNames && box_a.subBoxNames.indexOf(prop.slice(0, 4)) > -1 || box_b.subBoxNames && box_b.subBoxNames.indexOf(prop.slice(0, 4)) > -1) {
continue;
} else {
if (prop === "data" || prop === "start" || prop === "size" || prop === "creation_time" || prop === "modification_time") {
continue;
} else if (BoxParser.DIFF_PRIMITIVE_ARRAY_PROP_NAMES.indexOf(prop) > -1) {
continue;
} else {
if (box_a[prop] !== box_b[prop]) {
return false;
}
}
}
}
return true;
};
BoxParser.boxEqual = function (box_a, box_b) {
if (!BoxParser.boxEqualFields(box_a, box_b)) {
return false;
}
for (var j = 0; j < BoxParser.DIFF_BOXES_PROP_NAMES.length; j++) {
var name = BoxParser.DIFF_BOXES_PROP_NAMES[j];
if (box_a[name] && box_b[name]) {
if (!BoxParser.boxEqual(box_a[name], box_b[name])) {
return false;
}
}
}
return true;
}; // file:src/text-mp4.js
var XMLSubtitlein4Parser = function () {};
XMLSubtitlein4Parser.prototype.parseSample = function (sample) {
var res = {};
var i;
res.resources = [];
var stream = new MP4BoxStream(sample.data.buffer);
if (!sample.subsamples || sample.subsamples.length === 0) {
res.documentString = stream.readString(sample.data.length);
} else {
res.documentString = stream.readString(sample.subsamples[0].size);
if (sample.subsamples.length > 1) {
for (i = 1; i < sample.subsamples.length; i++) {
res.resources[i] = stream.readUint8Array(sample.subsamples[i].size);
}
}
}
if (typeof DOMParser !== "undefined") {
res.document = new DOMParser().parseFromString(res.documentString, "application/xml");
}
return res;
};
var Textin4Parser = function () {};
Textin4Parser.prototype.parseSample = function (sample) {
var textString;
var stream = new MP4BoxStream(sample.data.buffer);
textString = stream.readString(sample.data.length);
return textString;
};
Textin4Parser.prototype.parseConfig = function (data) {
var textString;
var stream = new MP4BoxStream(data.buffer);
stream.readUint32(); // version & flags
textString = stream.readCString();
return textString;
};
{
exports.XMLSubtitlein4Parser = XMLSubtitlein4Parser;
exports.Textin4Parser = Textin4Parser;
}
// file:src/isofile.js
/*
* Copyright (c) 2012-2013. Telecom ParisTech/TSI/MM/GPAC Cyril Concolato
* License: BSD-3-Clause (see LICENSE file)
*/
var ISOFile = function (stream) {
/* MutiBufferStream object used to parse boxes */
this.stream = stream || new MultiBufferStream();
/* Array of all boxes (in order) found in the file */
this.boxes = [];
/* Array of all mdats */
this.mdats = [];
/* Array of all moofs */
this.moofs = [];
/* Boolean indicating if the file is compatible with progressive parsing (moov first) */
this.isProgressive = false;
/* Boolean used to fire moov start event only once */
this.moovStartFound = false;
/* Callback called when the moov parsing starts */
this.onMoovStart = null;
/* Boolean keeping track of the call to onMoovStart, to avoid double calls */
this.moovStartSent = false;
/* Callback called when the moov is entirely parsed */
this.onReady = null;
/* Boolean keeping track of the call to onReady, to avoid double calls */
this.readySent = false;
/* Callback to call when segments are ready */
this.onSegment = null;
/* Callback to call when samples are ready */
this.onSamples = null;
/* Callback to call when there is an error in the parsing or processing of samples */
this.onError = null;
/* Boolean indicating if the moov box run-length encoded tables of sample information have been processed */
this.sampleListBuilt = false;
/* Array of Track objects for which fragmentation of samples is requested */
this.fragmentedTracks = [];
/* Array of Track objects for which extraction of samples is requested */
this.extractedTracks = [];
/* Boolean indicating that fragmention is ready */
this.isFragmentationInitialized = false;
/* Boolean indicating that fragmented has started */
this.sampleProcessingStarted = false;
/* Number of the next 'moof' to generate when fragmenting */
this.nextMoofNumber = 0;
/* Boolean indicating if the initial list of items has been produced */
this.itemListBuilt = false;
/* Callback called when the sidx box is entirely parsed */
this.onSidx = null;
/* Boolean keeping track of the call to onSidx, to avoid double calls */
this.sidxSent = false;
};
ISOFile.prototype.setSegmentOptions = function (id, user, options) {
var trak = this.getTrackById(id);
if (trak) {
var fragTrack = {};
this.fragmentedTracks.push(fragTrack);
fragTrack.id = id;
fragTrack.user = user;
fragTrack.trak = trak;
trak.nextSample = 0;
fragTrack.segmentStream = null;
fragTrack.nb_samples = 1000;
fragTrack.rapAlignement = true;
if (options) {
if (options.nbSamples) fragTrack.nb_samples = options.nbSamples;
if (options.rapAlignement) fragTrack.rapAlignement = options.rapAlignement;
}
}
};
ISOFile.prototype.unsetSegmentOptions = function (id) {
var index = -1;
for (var i = 0; i < this.fragmentedTracks.length; i++) {
var fragTrack = this.fragmentedTracks[i];
if (fragTrack.id == id) {
index = i;
}
}
if (index > -1) {
this.fragmentedTracks.splice(index, 1);
}
};
ISOFile.prototype.setExtractionOptions = function (id, user, options) {
var trak = this.getTrackById(id);
if (trak) {
var extractTrack = {};
this.extractedTracks.push(extractTrack);
extractTrack.id = id;
extractTrack.user = user;
extractTrack.trak = trak;
trak.nextSample = 0;
extractTrack.nb_samples = 1000;
extractTrack.samples = [];
if (options) {
if (options.nbSamples) extractTrack.nb_samples = options.nbSamples;
}
}
};
ISOFile.prototype.unsetExtractionOptions = function (id) {
var index = -1;
for (var i = 0; i < this.extractedTracks.length; i++) {
var extractTrack = this.extractedTracks[i];
if (extractTrack.id == id) {
index = i;
}
}
if (index > -1) {
this.extractedTracks.splice(index, 1);
}
};
ISOFile.prototype.parse = function () {
var ret;
var box;
var parseBoxHeadersOnly = false;
if (this.restoreParsePosition) {
if (!this.restoreParsePosition()) {
return;
}
}
while (true) {
if (this.hasIncompleteMdat && this.hasIncompleteMdat()) {
if (this.processIncompleteMdat()) {
continue;
} else {
return;
}
} else {
if (this.saveParsePosition) {
this.saveParsePosition();
}
ret = BoxParser.parseOneBox(this.stream, parseBoxHeadersOnly);
if (ret.code === BoxParser.ERR_NOT_ENOUGH_DATA) {
if (this.processIncompleteBox) {
if (this.processIncompleteBox(ret)) {
continue;
} else {
return;
}
} else {
return;
}
} else {
var box_type;
/* the box is entirely parsed */
box = ret.box;
box_type = box.type !== "uuid" ? box.type : box.uuid;
/* store the box in the 'boxes' array to preserve box order (for file rewrite if needed) */
this.boxes.push(box);
/* but also store box in a property for more direct access */
switch (box_type) {
case "mdat":
this.mdats.push(box);
break;
case "moof":
this.moofs.push(box);
break;
case "moov":
this.moovStartFound = true;
if (this.mdats.length === 0) {
this.isProgressive = true;
}
/* no break */
/* falls through */
default:
if (this[box_type] !== undefined) {
Log.warn("ISOFile", "Duplicate Box of type: " + box_type + ", overriding previous occurrence");
}
this[box_type] = box;
break;
}
if (this.updateUsedBytes) {
this.updateUsedBytes(box, ret);
}
}
}
}
};
ISOFile.prototype.checkBuffer = function (ab) {
if (ab === null || ab === undefined) {
throw "Buffer must be defined and non empty";
}
if (ab.fileStart === undefined) {
throw "Buffer must have a fileStart property";
}
if (ab.byteLength === 0) {
Log.warn("ISOFile", "Ignoring empty buffer (fileStart: " + ab.fileStart + ")");
this.stream.logBufferLevel();
return false;
}
Log.info("ISOFile", "Processing buffer (fileStart: " + ab.fileStart + ")");
/* mark the bytes in the buffer as not being used yet */
ab.usedBytes = 0;
this.stream.insertBuffer(ab);
this.stream.logBufferLevel();
if (!this.stream.initialized()) {
Log.warn("ISOFile", "Not ready to start parsing");
return false;
}
return true;
};
/* Processes a new ArrayBuffer (with a fileStart property)
Returns the next expected file position, or undefined if not ready to parse */
ISOFile.prototype.appendBuffer = function (ab, last) {
var nextFileStart;
if (!this.checkBuffer(ab)) {
return;
}
/* Parse whatever is in the existing buffers */
this.parse();
/* Check if the moovStart callback needs to be called */
if (this.moovStartFound && !this.moovStartSent) {
this.moovStartSent = true;
if (this.onMoovStart) this.onMoovStart();
}
if (this.moov) {
/* A moov box has been entirely parsed */
/* if this is the first call after the moov is found we initialize the list of samples (may be empty in fragmented files) */
if (!this.sampleListBuilt) {
this.buildSampleLists();
this.sampleListBuilt = true;
}
/* We update the sample information if there are any new moof boxes */
this.updateSampleLists();
/* If the application needs to be informed that the 'moov' has been found,
we create the information object and callback the application */
if (this.onReady && !this.readySent) {
this.readySent = true;
this.onReady(this.getInfo());
}
/* See if any sample extraction or segment creation needs to be done with the available samples */
this.processSamples(last);
/* Inform about the best range to fetch next */
if (this.nextSeekPosition) {
nextFileStart = this.nextSeekPosition;
this.nextSeekPosition = undefined;
} else {
nextFileStart = this.nextParsePosition;
}
if (this.stream.getEndFilePositionAfter) {
nextFileStart = this.stream.getEndFilePositionAfter(nextFileStart);
}
} else {
if (this.nextParsePosition) {
/* moov has not been parsed but the first buffer was received,
the next fetch should probably be the next box start */
nextFileStart = this.nextParsePosition;
} else {
/* No valid buffer has been parsed yet, we cannot know what to parse next */
nextFileStart = 0;
}
}
if (this.sidx) {
if (this.onSidx && !this.sidxSent) {
this.onSidx(this.sidx);
this.sidxSent = true;
}
}
if (this.meta) {
if (this.flattenItemInfo && !this.itemListBuilt) {
this.flattenItemInfo();
this.itemListBuilt = true;
}
if (this.processItems) {
this.processItems(this.onItem);
}
}
if (this.stream.cleanBuffers) {
Log.info("ISOFile", "Done processing buffer (fileStart: " + ab.fileStart + ") - next buffer to fetch should have a fileStart position of " + nextFileStart);
this.stream.logBufferLevel();
this.stream.cleanBuffers();
this.stream.logBufferLevel(true);
Log.info("ISOFile", "Sample data size in memory: " + this.getAllocatedSampleDataSize());
}
return nextFileStart;
};
ISOFile.prototype.getInfo = function () {
var i, j;
var movie = {};
var trak;
var track;
var ref;
var sample_desc;
var _1904 = new Date('1904-01-01T00:00:00Z').getTime();
if (this.moov) {
movie.hasMoov = true;
movie.duration = this.moov.mvhd.duration;
movie.timescale = this.moov.mvhd.timescale;
movie.isFragmented = this.moov.mvex != null;
if (movie.isFragmented && this.moov.mvex.mehd) {
movie.fragment_duration = this.moov.mvex.mehd.fragment_duration;
}
movie.isProgressive = this.isProgressive;
movie.hasIOD = this.moov.iods != null;
movie.brands = [];
movie.brands.push(this.ftyp.major_brand);
movie.brands = movie.brands.concat(this.ftyp.compatible_brands);
movie.created = new Date(_1904 + this.moov.mvhd.creation_time * 1000);
movie.modified = new Date(_1904 + this.moov.mvhd.modification_time * 1000);
movie.tracks = [];
movie.audioTracks = [];
movie.videoTracks = [];
movie.subtitleTracks = [];
movie.metadataTracks = [];
movie.hintTracks = [];
movie.otherTracks = [];
for (i = 0; i < this.moov.traks.length; i++) {
trak = this.moov.traks[i];
sample_desc = trak.mdia.minf.stbl.stsd.entries[0];
track = {};
movie.tracks.push(track);
track.id = trak.tkhd.track_id;
track.name = trak.mdia.hdlr.name;
track.references = [];
if (trak.tref) {
for (j = 0; j < trak.tref.boxes.length; j++) {
ref = {};
track.references.push(ref);
ref.type = trak.tref.boxes[j].type;
ref.track_ids = trak.tref.boxes[j].track_ids;
}
}
if (trak.edts) {
track.edits = trak.edts.elst.entries;
}
track.created = new Date(_1904 + trak.tkhd.creation_time * 1000);
track.modified = new Date(_1904 + trak.tkhd.modification_time * 1000);
track.movie_duration = trak.tkhd.duration;
track.movie_timescale = movie.timescale;
track.layer = trak.tkhd.layer;
track.alternate_group = trak.tkhd.alternate_group;
track.volume = trak.tkhd.volume;
track.matrix = trak.tkhd.matrix;
track.track_width = trak.tkhd.width / (1 << 16);
track.track_height = trak.tkhd.height / (1 << 16);
track.timescale = trak.mdia.mdhd.timescale;
track.cts_shift = trak.mdia.minf.stbl.cslg;
track.duration = trak.mdia.mdhd.duration;
track.samples_duration = trak.samples_duration;
track.codec = sample_desc.getCodec();
track.kind = trak.udta && trak.udta.kinds.length ? trak.udta.kinds[0] : {
schemeURI: "",
value: ""
};
track.language = trak.mdia.elng ? trak.mdia.elng.extended_language : trak.mdia.mdhd.languageString;
track.nb_samples = trak.samples.length;
track.size = trak.samples_size;
track.bitrate = track.size * 8 * track.timescale / track.samples_duration;
if (sample_desc.isAudio()) {
track.type = "audio";
movie.audioTracks.push(track);
track.audio = {};
track.audio.sample_rate = sample_desc.getSampleRate();
track.audio.channel_count = sample_desc.getChannelCount();
track.audio.sample_size = sample_desc.getSampleSize();
} else if (sample_desc.isVideo()) {
track.type = "video";
movie.videoTracks.push(track);
track.video = {};
track.video.width = sample_desc.getWidth();
track.video.height = sample_desc.getHeight();
} else if (sample_desc.isSubtitle()) {
track.type = "subtitles";
movie.subtitleTracks.push(track);
} else if (sample_desc.isHint()) {
track.type = "metadata";
movie.hintTracks.push(track);
} else if (sample_desc.isMetadata()) {
track.type = "metadata";
movie.metadataTracks.push(track);
} else {
track.type = "metadata";
movie.otherTracks.push(track);
}
}
} else {
movie.hasMoov = false;
}
movie.mime = "";
if (movie.hasMoov && movie.tracks) {
if (movie.videoTracks && movie.videoTracks.length > 0) {
movie.mime += 'video/mp4; codecs=\"';
} else if (movie.audioTracks && movie.audioTracks.length > 0) {
movie.mime += 'audio/mp4; codecs=\"';
} else {
movie.mime += 'application/mp4; codecs=\"';
}
for (i = 0; i < movie.tracks.length; i++) {
if (i !== 0) movie.mime += ',';
movie.mime += movie.tracks[i].codec;
}
movie.mime += '\"; profiles=\"';
movie.mime += this.ftyp.compatible_brands.join();
movie.mime += '\"';
}
return movie;
};
ISOFile.prototype.setNextSeekPositionFromSample = function (sample) {
if (!sample) {
return;
}
if (this.nextSeekPosition) {
this.nextSeekPosition = Math.min(sample.offset + sample.alreadyRead, this.nextSeekPosition);
} else {
this.nextSeekPosition = sample.offset + sample.alreadyRead;
}
};
ISOFile.prototype.processSamples = function (last) {
var i;
var trak;
if (!this.sampleProcessingStarted) return;
/* For each track marked for fragmentation,
check if the next sample is there (i.e. if the sample information is known (i.e. moof has arrived) and if it has been downloaded)
and create a fragment with it */
if (this.isFragmentationInitialized && this.onSegment !== null) {
for (i = 0; i < this.fragmentedTracks.length; i++) {
var fragTrak = this.fragmentedTracks[i];
trak = fragTrak.trak;
while (trak.nextSample < trak.samples.length && this.sampleProcessingStarted) {
/* The sample information is there (either because the file is not fragmented and this is not the last sample,
or because the file is fragmented and the moof for that sample has been received */
Log.debug("ISOFile", "Creating media fragment on track #" + fragTrak.id + " for sample " + trak.nextSample);
var result = this.createFragment(fragTrak.id, trak.nextSample, fragTrak.segmentStream);
if (result) {
fragTrak.segmentStream = result;
trak.nextSample++;
} else {
/* The fragment could not be created because the media data is not there (not downloaded), wait for it */
break;
}
/* A fragment is created by sample, but the segment is the accumulation in the buffer of these fragments.
It is flushed only as requested by the application (nb_samples) to avoid too many callbacks */
if (trak.nextSample % fragTrak.nb_samples === 0 || last || trak.nextSample >= trak.samples.length) {
Log.info("ISOFile", "Sending fragmented data on track #" + fragTrak.id + " for samples [" + Math.max(0, trak.nextSample - fragTrak.nb_samples) + "," + (trak.nextSample - 1) + "]");
Log.info("ISOFile", "Sample data size in memory: " + this.getAllocatedSampleDataSize());
if (this.onSegment) {
this.onSegment(fragTrak.id, fragTrak.user, fragTrak.segmentStream.buffer, trak.nextSample, last || trak.nextSample >= trak.samples.length);
}
/* force the creation of a new buffer */
fragTrak.segmentStream = null;
if (fragTrak !== this.fragmentedTracks[i]) {
/* make sure we can stop fragmentation if needed */
break;
}
}
}
}
}
if (this.onSamples !== null) {
/* For each track marked for data export,
check if the next sample is there (i.e. has been downloaded) and send it */
for (i = 0; i < this.extractedTracks.length; i++) {
var extractTrak = this.extractedTracks[i];
trak = extractTrak.trak;
while (trak.nextSample < trak.samples.length && this.sampleProcessingStarted) {
Log.debug("ISOFile", "Exporting on track #" + extractTrak.id + " sample #" + trak.nextSample);
var sample = this.getSample(trak, trak.nextSample);
if (sample) {
trak.nextSample++;
extractTrak.samples.push(sample);
} else {
this.setNextSeekPositionFromSample(trak.samples[trak.nextSample]);
break;
}
if (trak.nextSample % extractTrak.nb_samples === 0 || trak.nextSample >= trak.samples.length) {
Log.debug("ISOFile", "Sending samples on track #" + extractTrak.id + " for sample " + trak.nextSample);
if (this.onSamples) {
this.onSamples(extractTrak.id, extractTrak.user, extractTrak.samples);
}
extractTrak.samples = [];
if (extractTrak !== this.extractedTracks[i]) {
/* check if the extraction needs to be stopped */
break;
}
}
}
}
}
};
/* Find and return specific boxes using recursion and early return */
ISOFile.prototype.getBox = function (type) {
var result = this.getBoxes(type, true);
return result.length ? result[0] : null;
};
ISOFile.prototype.getBoxes = function (type, returnEarly) {
var result = [];
ISOFile._sweep.call(this, type, result, returnEarly);
return result;
};
ISOFile._sweep = function (type, result, returnEarly) {
if (this.type && this.type == type) result.push(this);
for (var box in this.boxes) {
if (result.length && returnEarly) return;
ISOFile._sweep.call(this.boxes[box], type, result, returnEarly);
}
};
ISOFile.prototype.getTrackSamplesInfo = function (track_id) {
var track = this.getTrackById(track_id);
if (track) {
return track.samples;
} else {
return;
}
};
ISOFile.prototype.getTrackSample = function (track_id, number) {
var track = this.getTrackById(track_id);
var sample = this.getSample(track, number);
return sample;
};
/* Called by the application to release the resources associated to samples already forwarded to the application */
ISOFile.prototype.releaseUsedSamples = function (id, sampleNum) {
var size = 0;
var trak = this.getTrackById(id);
if (!trak.lastValidSample) trak.lastValidSample = 0;
for (var i = trak.lastValidSample; i < sampleNum; i++) {
size += this.releaseSample(trak, i);
}
Log.info("ISOFile", "Track #" + id + " released samples up to " + sampleNum + " (released size: " + size + ", remaining: " + this.samplesDataSize + ")");
trak.lastValidSample = sampleNum;
};
ISOFile.prototype.start = function () {
this.sampleProcessingStarted = true;
this.processSamples(false);
};
ISOFile.prototype.stop = function () {
this.sampleProcessingStarted = false;
};
/* Called by the application to flush the remaining samples (e.g. once the download is finished or when no more samples will be added) */
ISOFile.prototype.flush = function () {
Log.info("ISOFile", "Flushing remaining samples");
this.updateSampleLists();
this.processSamples(true);
this.stream.cleanBuffers();
this.stream.logBufferLevel(true);
};
/* Finds the byte offset for a given time on a given track
also returns the time of the previous rap */
ISOFile.prototype.seekTrack = function (time, useRap, trak) {
var j;
var sample;
var seek_offset = Infinity;
var rap_seek_sample_num = 0;
var seek_sample_num = 0;
var timescale;
if (trak.samples.length === 0) {
Log.info("ISOFile", "No sample in track, cannot seek! Using time " + Log.getDurationString(0, 1) + " and offset: " + 0);
return {
offset: 0,
time: 0
};
}
for (j = 0; j < trak.samples.length; j++) {
sample = trak.samples[j];
if (j === 0) {
seek_sample_num = 0;
timescale = sample.timescale;
} else if (sample.cts > time * sample.timescale) {
seek_sample_num = j - 1;
break;
}
if (useRap && sample.is_sync) {
rap_seek_sample_num = j;
}
}
if (useRap) {
seek_sample_num = rap_seek_sample_num;
}
time = trak.samples[seek_sample_num].cts;
trak.nextSample = seek_sample_num;
while (trak.samples[seek_sample_num].alreadyRead === trak.samples[seek_sample_num].size) {
// No remaining samples to look for, all are downloaded.
if (!trak.samples[seek_sample_num + 1]) {
break;
}
seek_sample_num++;
}
seek_offset = trak.samples[seek_sample_num].offset + trak.samples[seek_sample_num].alreadyRead;
Log.info("ISOFile", "Seeking to " + (useRap ? "RAP" : "") + " sample #" + trak.nextSample + " on track " + trak.tkhd.track_id + ", time " + Log.getDurationString(time, timescale) + " and offset: " + seek_offset);
return {
offset: seek_offset,
time: time / timescale
};
};
ISOFile.prototype.getTrackDuration = function (trak) {
var sample;
if (!trak.samples) {
return Infinity;
}
sample = trak.samples[trak.samples.length - 1];
return (sample.cts + sample.duration) / sample.timescale;
};
/* Finds the byte offset in the file corresponding to the given time or to the time of the previous RAP */
ISOFile.prototype.seek = function (time, useRap) {
var moov = this.moov;
var trak;
var trak_seek_info;
var i;
var seek_info = {
offset: Infinity,
time: Infinity
};
if (!this.moov) {
throw "Cannot seek: moov not received!";
} else {
for (i = 0; i < moov.traks.length; i++) {
trak = moov.traks[i];
if (time > this.getTrackDuration(trak)) {
// skip tracks that already ended
continue;
}
trak_seek_info = this.seekTrack(time, useRap, trak);
if (trak_seek_info.offset < seek_info.offset) {
seek_info.offset = trak_seek_info.offset;
}
if (trak_seek_info.time < seek_info.time) {
seek_info.time = trak_seek_info.time;
}
}
Log.info("ISOFile", "Seeking at time " + Log.getDurationString(seek_info.time, 1) + " needs a buffer with a fileStart position of " + seek_info.offset);
if (seek_info.offset === Infinity) {
/* No sample info, in all tracks, cannot seek */
seek_info = {
offset: this.nextParsePosition,
time: 0
};
} else {
/* check if the seek position is already in some buffer and
in that case return the end of that buffer (or of the last contiguous buffer) */
/* TODO: Should wait until append operations are done */
seek_info.offset = this.stream.getEndFilePositionAfter(seek_info.offset);
}
Log.info("ISOFile", "Adjusted seek position (after checking data already in buffer): " + seek_info.offset);
return seek_info;
}
};
ISOFile.prototype.equal = function (b) {
var box_index = 0;
while (box_index < this.boxes.length && box_index < b.boxes.length) {
var a_box = this.boxes[box_index];
var b_box = b.boxes[box_index];
if (!BoxParser.boxEqual(a_box, b_box)) {
return false;
}
box_index++;
}
return true;
};
{
exports.ISOFile = ISOFile;
}
// file:src/isofile-advanced-parsing.js
/* position in the current buffer of the beginning of the last box parsed */
ISOFile.prototype.lastBoxStartPosition = 0;
/* indicator if the parsing is stuck in the middle of an mdat box */
ISOFile.prototype.parsingMdat = null;
/* next file position that the parser needs:
- 0 until the first buffer (i.e. fileStart ===0) has been received
- otherwise, the next box start until the moov box has been parsed
- otherwise, the position of the next sample to fetch
*/
ISOFile.prototype.nextParsePosition = 0;
/* keep mdat data */
ISOFile.prototype.discardMdatData = false;
ISOFile.prototype.processIncompleteBox = function (ret) {
var box;
var merged;
var found;
/* we did not have enough bytes in the current buffer to parse the entire box */
if (ret.type === "mdat") {
/* we had enough bytes to get its type and size and it's an 'mdat' */
/* special handling for mdat boxes, since we don't actually need to parse it linearly
we create the box */
box = new BoxParser[ret.type + "Box"](ret.size);
this.parsingMdat = box;
this.boxes.push(box);
this.mdats.push(box);
box.start = ret.start;
box.hdr_size = ret.hdr_size;
this.stream.addUsedBytes(box.hdr_size);
/* indicate that the parsing should start from the end of the box */
this.lastBoxStartPosition = box.start + box.size;
/* let's see if we have the end of the box in the other buffers */
found = this.stream.seek(box.start + box.size, false, this.discardMdatData);
if (found) {
/* found the end of the box */
this.parsingMdat = null;
/* let's see if we can parse more in this buffer */
return true;
} else {
/* 'mdat' end not found in the existing buffers */
/* determine the next position in the file to start parsing from */
if (!this.moovStartFound) {
/* moov not find yet,
the file probably has 'mdat' at the beginning, and 'moov' at the end,
indicate that the downloader should not try to download those bytes now */
this.nextParsePosition = box.start + box.size;
} else {
/* we have the start of the moov box,
the next bytes should try to complete the current 'mdat' */
this.nextParsePosition = this.stream.findEndContiguousBuf();
}
/* not much we can do, wait for more buffers to arrive */
return false;
}
} else {
/* box is incomplete, we may not even know its type */
if (ret.type === "moov") {
/* the incomplete box is a 'moov' box */
this.moovStartFound = true;
if (this.mdats.length === 0) {
this.isProgressive = true;
}
}
/* either it's not an mdat box (and we need to parse it, we cannot skip it)
(TODO: we could skip 'free' boxes ...)
or we did not have enough data to parse the type and size of the box,
we try to concatenate the current buffer with the next buffer to restart parsing */
merged = this.stream.mergeNextBuffer ? this.stream.mergeNextBuffer() : false;
if (merged) {
/* The next buffer was contiguous, the merging succeeded,
we can now continue parsing,
the next best position to parse is at the end of this new buffer */
this.nextParsePosition = this.stream.getEndPosition();
return true;
} else {
/* we cannot concatenate existing buffers because they are not contiguous or because there is no additional buffer */
/* The next best position to parse is still at the end of this old buffer */
if (!ret.type) {
/* There were not enough bytes in the buffer to parse the box type and length,
the next fetch should retrieve those missing bytes, i.e. the next bytes after this buffer */
this.nextParsePosition = this.stream.getEndPosition();
} else {
/* we had enough bytes to parse size and type of the incomplete box
if we haven't found yet the moov box, skip this one and try the next one
if we have found the moov box, let's continue linear parsing */
if (this.moovStartFound) {
this.nextParsePosition = this.stream.getEndPosition();
} else {
this.nextParsePosition = this.stream.getPosition() + ret.size;
}
}
return false;
}
}
};
ISOFile.prototype.hasIncompleteMdat = function () {
return this.parsingMdat !== null;
};
ISOFile.prototype.processIncompleteMdat = function () {
var box;
var found;
/* we are in the parsing of an incomplete mdat box */
box = this.parsingMdat;
found = this.stream.seek(box.start + box.size, false, this.discardMdatData);
if (found) {
Log.debug("ISOFile", "Found 'mdat' end in buffered data");
/* the end of the mdat has been found */
this.parsingMdat = null;
/* we can parse more in this buffer */
return true;
} else {
/* we don't have the end of this mdat yet,
indicate that the next byte to fetch is the end of the buffers we have so far,
return and wait for more buffer to come */
this.nextParsePosition = this.stream.findEndContiguousBuf();
return false;
}
};
ISOFile.prototype.restoreParsePosition = function () {
/* Reposition at the start position of the previous box not entirely parsed */
return this.stream.seek(this.lastBoxStartPosition, true, this.discardMdatData);
};
ISOFile.prototype.saveParsePosition = function () {
/* remember the position of the box start in case we need to roll back (if the box is incomplete) */
this.lastBoxStartPosition = this.stream.getPosition();
};
ISOFile.prototype.updateUsedBytes = function (box, ret) {
if (this.stream.addUsedBytes) {
if (box.type === "mdat") {
/* for an mdat box, only its header is considered used, other bytes will be used when sample data is requested */
this.stream.addUsedBytes(box.hdr_size);
if (this.discardMdatData) {
this.stream.addUsedBytes(box.size - box.hdr_size);
}
} else {
/* for all other boxes, the entire box data is considered used */
this.stream.addUsedBytes(box.size);
}
}
};
// file:src/isofile-advanced-creation.js
ISOFile.prototype.add = BoxParser.Box.prototype.add;
ISOFile.prototype.addBox = BoxParser.Box.prototype.addBox;
ISOFile.prototype.init = function (_options) {
var options = _options || {};
this.add("ftyp").set("major_brand", options.brands && options.brands[0] || "iso4").set("minor_version", 0).set("compatible_brands", options.brands || ["iso4"]);
var moov = this.add("moov");
moov.add("mvhd").set("timescale", options.timescale || 600).set("rate", options.rate || 1 << 16).set("creation_time", 0).set("modification_time", 0).set("duration", options.duration || 0).set("volume", options.width ? 0 : 0x0100).set("matrix", [1 << 16, 0, 0, 0, 1 << 16, 0, 0, 0, 0x40000000]).set("next_track_id", 1);
moov.add("mvex");
return this;
};
ISOFile.prototype.addTrack = function (_options) {
if (!this.moov) {
this.init(_options);
}
var options = _options || {};
options.width = options.width || 320;
options.height = options.height || 320;
options.id = options.id || this.moov.mvhd.next_track_id;
options.type = options.type || "avc1";
var trak = this.moov.add("trak");
this.moov.mvhd.next_track_id = options.id + 1;
trak.add("tkhd").set("flags", BoxParser.TKHD_FLAG_ENABLED | BoxParser.TKHD_FLAG_IN_MOVIE | BoxParser.TKHD_FLAG_IN_PREVIEW).set("creation_time", 0).set("modification_time", 0).set("track_id", options.id).set("duration", options.duration || 0).set("layer", options.layer || 0).set("alternate_group", 0).set("volume", 1).set("matrix", [0, 0, 0, 0, 0, 0, 0, 0, 0]).set("width", options.width << 16).set("height", options.height << 16);
var mdia = trak.add("mdia");
mdia.add("mdhd").set("creation_time", 0).set("modification_time", 0).set("timescale", options.timescale || 1).set("duration", options.media_duration || 0).set("language", options.language || "und");
mdia.add("hdlr").set("handler", options.hdlr || "vide").set("name", options.name || "Track created with MP4Box.js");
mdia.add("elng").set("extended_language", options.language || "fr-FR");
var minf = mdia.add("minf");
if (BoxParser[options.type + "SampleEntry"] === undefined) return;
var sample_description_entry = new BoxParser[options.type + "SampleEntry"]();
sample_description_entry.data_reference_index = 1;
var media_type = "";
for (var mediaType in BoxParser.sampleEntryCodes) {
var codes = BoxParser.sampleEntryCodes[mediaType];
for (var i = 0; i < codes.length; i++) {
if (codes.indexOf(options.type) > -1) {
media_type = mediaType;
break;
}
}
}
switch (media_type) {
case "Visual":
minf.add("vmhd").set("graphicsmode", 0).set("opcolor", [0, 0, 0]);
sample_description_entry.set("width", options.width).set("height", options.height).set("horizresolution", 0x48 << 16).set("vertresolution", 0x48 << 16).set("frame_count", 1).set("compressorname", options.type + " Compressor").set("depth", 0x18);
if (options.avcDecoderConfigRecord) {
var avcC = new BoxParser.avcCBox();
avcC.parse(new MP4BoxStream(options.avcDecoderConfigRecord));
sample_description_entry.addBox(avcC);
} else if (options.hevcDecoderConfigRecord) {
var hvcC = new BoxParser.hvcCBox();
hvcC.parse(new MP4BoxStream(options.hevcDecoderConfigRecord));
sample_description_entry.addBox(hvcC);
}
break;
case "Audio":
minf.add("smhd").set("balance", options.balance || 0);
sample_description_entry.set("channel_count", options.channel_count || 2).set("samplesize", options.samplesize || 16).set("samplerate", options.samplerate || 1 << 16);
break;
case "Hint":
minf.add("hmhd"); // TODO: add properties
break;
case "Subtitle":
minf.add("sthd");
switch (options.type) {
case "stpp":
sample_description_entry.set("namespace", options.namespace || "nonamespace").set("schema_location", options.schema_location || "").set("auxiliary_mime_types", options.auxiliary_mime_types || "");
break;
}
break;
case "Metadata":
minf.add("nmhd");
break;
case "System":
minf.add("nmhd");
break;
default:
minf.add("nmhd");
break;
}
if (options.description) {
sample_description_entry.addBox(options.description);
}
if (options.description_boxes) {
options.description_boxes.forEach(function (b) {
sample_description_entry.addBox(b);
});
}
minf.add("dinf").add("dref").addEntry(new BoxParser["url Box"]().set("flags", 0x1));
var stbl = minf.add("stbl");
stbl.add("stsd").addEntry(sample_description_entry);
stbl.add("stts").set("sample_counts", []).set("sample_deltas", []);
stbl.add("stsc").set("first_chunk", []).set("samples_per_chunk", []).set("sample_description_index", []);
stbl.add("stco").set("chunk_offsets", []);
stbl.add("stsz").set("sample_sizes", []);
this.moov.mvex.add("trex").set("track_id", options.id).set("default_sample_description_index", options.default_sample_description_index || 1).set("default_sample_duration", options.default_sample_duration || 0).set("default_sample_size", options.default_sample_size || 0).set("default_sample_flags", options.default_sample_flags || 0);
this.buildTrakSampleLists(trak);
return options.id;
};
BoxParser.Box.prototype.computeSize = function (stream_) {
var stream = stream_ || new DataStream();
stream.endianness = DataStream.BIG_ENDIAN;
this.write(stream);
};
ISOFile.prototype.addSample = function (track_id, data, _options) {
var options = _options || {};
var sample = {};
var trak = this.getTrackById(track_id);
if (trak === null) return;
sample.number = trak.samples.length;
sample.track_id = trak.tkhd.track_id;
sample.timescale = trak.mdia.mdhd.timescale;
sample.description_index = options.sample_description_index ? options.sample_description_index - 1 : 0;
sample.description = trak.mdia.minf.stbl.stsd.entries[sample.description_index];
sample.data = data;
sample.size = data.byteLength;
sample.alreadyRead = sample.size;
sample.duration = options.duration || 1;
sample.cts = options.cts || 0;
sample.dts = options.dts || 0;
sample.is_sync = options.is_sync || false;
sample.is_leading = options.is_leading || 0;
sample.depends_on = options.depends_on || 0;
sample.is_depended_on = options.is_depended_on || 0;
sample.has_redundancy = options.has_redundancy || 0;
sample.degradation_priority = options.degradation_priority || 0;
sample.offset = 0;
sample.subsamples = options.subsamples;
trak.samples.push(sample);
trak.samples_size += sample.size;
trak.samples_duration += sample.duration;
if (trak.first_dts === undefined) {
trak.first_dts = options.dts;
}
this.processSamples();
var moof = this.createSingleSampleMoof(sample);
this.addBox(moof);
moof.computeSize();
/* adjusting the data_offset now that the moof size is known*/
moof.trafs[0].truns[0].data_offset = moof.size + 8; //8 is mdat header
this.add("mdat").data = new Uint8Array(data);
return sample;
};
ISOFile.prototype.createSingleSampleMoof = function (sample) {
var sample_flags = 0;
if (sample.is_sync) sample_flags = 1 << 25; // sample_depends_on_none (I picture)
else sample_flags = 1 << 16; // non-sync
var moof = new BoxParser.moofBox();
moof.add("mfhd").set("sequence_number", this.nextMoofNumber);
this.nextMoofNumber++;
var traf = moof.add("traf");
var trak = this.getTrackById(sample.track_id);
traf.add("tfhd").set("track_id", sample.track_id).set("flags", BoxParser.TFHD_FLAG_DEFAULT_BASE_IS_MOOF);
traf.add("tfdt").set("baseMediaDecodeTime", sample.dts - (trak.first_dts || 0));
traf.add("trun").set("flags", BoxParser.TRUN_FLAGS_DATA_OFFSET | BoxParser.TRUN_FLAGS_DURATION | BoxParser.TRUN_FLAGS_SIZE | BoxParser.TRUN_FLAGS_FLAGS | BoxParser.TRUN_FLAGS_CTS_OFFSET).set("data_offset", 0).set("first_sample_flags", 0).set("sample_count", 1).set("sample_duration", [sample.duration]).set("sample_size", [sample.size]).set("sample_flags", [sample_flags]).set("sample_composition_time_offset", [sample.cts - sample.dts]);
return moof;
};
// file:src/isofile-sample-processing.js
/* Index of the last moof box received */
ISOFile.prototype.lastMoofIndex = 0;
/* size of the buffers allocated for samples */
ISOFile.prototype.samplesDataSize = 0;
/* Resets all sample tables */
ISOFile.prototype.resetTables = function () {
var i;
var trak, stco, stsc, stsz, stts, ctts, stss;
this.initial_duration = this.moov.mvhd.duration;
this.moov.mvhd.duration = 0;
for (i = 0; i < this.moov.traks.length; i++) {
trak = this.moov.traks[i];
trak.tkhd.duration = 0;
trak.mdia.mdhd.duration = 0;
stco = trak.mdia.minf.stbl.stco || trak.mdia.minf.stbl.co64;
stco.chunk_offsets = [];
stsc = trak.mdia.minf.stbl.stsc;
stsc.first_chunk = [];
stsc.samples_per_chunk = [];
stsc.sample_description_index = [];
stsz = trak.mdia.minf.stbl.stsz || trak.mdia.minf.stbl.stz2;
stsz.sample_sizes = [];
stts = trak.mdia.minf.stbl.stts;
stts.sample_counts = [];
stts.sample_deltas = [];
ctts = trak.mdia.minf.stbl.ctts;
if (ctts) {
ctts.sample_counts = [];
ctts.sample_offsets = [];
}
stss = trak.mdia.minf.stbl.stss;
var k = trak.mdia.minf.stbl.boxes.indexOf(stss);
if (k != -1) trak.mdia.minf.stbl.boxes[k] = null;
}
};
ISOFile.initSampleGroups = function (trak, traf, sbgps, trak_sgpds, traf_sgpds) {
var l;
var k;
var sample_group_info;
var sample_group_key;
function SampleGroupInfo(_type, _parameter, _sbgp) {
this.grouping_type = _type;
this.grouping_type_parameter = _parameter;
this.sbgp = _sbgp;
this.last_sample_in_run = -1;
this.entry_index = -1;
}
if (traf) {
traf.sample_groups_info = [];
}
if (!trak.sample_groups_info) {
trak.sample_groups_info = [];
}
for (k = 0; k < sbgps.length; k++) {
sample_group_key = sbgps[k].grouping_type + "/" + sbgps[k].grouping_type_parameter;
sample_group_info = new SampleGroupInfo(sbgps[k].grouping_type, sbgps[k].grouping_type_parameter, sbgps[k]);
if (traf) {
traf.sample_groups_info[sample_group_key] = sample_group_info;
}
if (!trak.sample_groups_info[sample_group_key]) {
trak.sample_groups_info[sample_group_key] = sample_group_info;
}
for (l = 0; l < trak_sgpds.length; l++) {
if (trak_sgpds[l].grouping_type === sbgps[k].grouping_type) {
sample_group_info.description = trak_sgpds[l];
sample_group_info.description.used = true;
}
}
if (traf_sgpds) {
for (l = 0; l < traf_sgpds.length; l++) {
if (traf_sgpds[l].grouping_type === sbgps[k].grouping_type) {
sample_group_info.fragment_description = traf_sgpds[l];
sample_group_info.fragment_description.used = true;
sample_group_info.is_fragment = true;
}
}
}
}
if (!traf) {
for (k = 0; k < trak_sgpds.length; k++) {
if (!trak_sgpds[k].used && trak_sgpds[k].version >= 2) {
sample_group_key = trak_sgpds[k].grouping_type + "/0";
sample_group_info = new SampleGroupInfo(trak_sgpds[k].grouping_type, 0);
if (!trak.sample_groups_info[sample_group_key]) {
trak.sample_groups_info[sample_group_key] = sample_group_info;
}
}
}
} else {
if (traf_sgpds) {
for (k = 0; k < traf_sgpds.length; k++) {
if (!traf_sgpds[k].used && traf_sgpds[k].version >= 2) {
sample_group_key = traf_sgpds[k].grouping_type + "/0";
sample_group_info = new SampleGroupInfo(traf_sgpds[k].grouping_type, 0);
sample_group_info.is_fragment = true;
if (!traf.sample_groups_info[sample_group_key]) {
traf.sample_groups_info[sample_group_key] = sample_group_info;
}
}
}
}
}
};
ISOFile.setSampleGroupProperties = function (trak, sample, sample_number, sample_groups_info) {
var k;
var index;
sample.sample_groups = [];
for (k in sample_groups_info) {
sample.sample_groups[k] = {};
sample.sample_groups[k].grouping_type = sample_groups_info[k].grouping_type;
sample.sample_groups[k].grouping_type_parameter = sample_groups_info[k].grouping_type_parameter;
if (sample_number >= sample_groups_info[k].last_sample_in_run) {
if (sample_groups_info[k].last_sample_in_run < 0) {
sample_groups_info[k].last_sample_in_run = 0;
}
sample_groups_info[k].entry_index++;
if (sample_groups_info[k].entry_index <= sample_groups_info[k].sbgp.entries.length - 1) {
sample_groups_info[k].last_sample_in_run += sample_groups_info[k].sbgp.entries[sample_groups_info[k].entry_index].sample_count;
}
}
if (sample_groups_info[k].entry_index <= sample_groups_info[k].sbgp.entries.length - 1) {
sample.sample_groups[k].group_description_index = sample_groups_info[k].sbgp.entries[sample_groups_info[k].entry_index].group_description_index;
} else {
sample.sample_groups[k].group_description_index = -1; // special value for not defined
}
if (sample.sample_groups[k].group_description_index !== 0) {
var description;
if (sample_groups_info[k].fragment_description) {
description = sample_groups_info[k].fragment_description;
} else {
description = sample_groups_info[k].description;
}
if (sample.sample_groups[k].group_description_index > 0) {
if (sample.sample_groups[k].group_description_index > 65535) {
index = (sample.sample_groups[k].group_description_index >> 16) - 1;
} else {
index = sample.sample_groups[k].group_description_index - 1;
}
if (description && index >= 0) {
sample.sample_groups[k].description = description.entries[index];
}
} else {
if (description && description.version >= 2) {
if (description.default_group_description_index > 0) {
sample.sample_groups[k].description = description.entries[description.default_group_description_index - 1];
}
}
}
}
}
};
ISOFile.process_sdtp = function (sdtp, sample, number) {
if (!sample) {
return;
}
if (sdtp) {
sample.is_leading = sdtp.is_leading[number];
sample.depends_on = sdtp.sample_depends_on[number];
sample.is_depended_on = sdtp.sample_is_depended_on[number];
sample.has_redundancy = sdtp.sample_has_redundancy[number];
} else {
sample.is_leading = 0;
sample.depends_on = 0;
sample.is_depended_on = 0;
sample.has_redundancy = 0;
}
};
/* Build initial sample list from sample tables */
ISOFile.prototype.buildSampleLists = function () {
var i;
var trak;
for (i = 0; i < this.moov.traks.length; i++) {
trak = this.moov.traks[i];
this.buildTrakSampleLists(trak);
}
};
ISOFile.prototype.buildTrakSampleLists = function (trak) {
var j;
var stco, stsc, stsz, stts, ctts, stss, stsd, subs, sbgps, sgpds, stdp;
var chunk_run_index, chunk_index, last_chunk_in_run, offset_in_chunk, last_sample_in_chunk;
var last_sample_in_stts_run, stts_run_index, last_sample_in_ctts_run, ctts_run_index, last_stss_index, subs_entry_index, last_subs_sample_index;
trak.samples = [];
trak.samples_duration = 0;
trak.samples_size = 0;
stco = trak.mdia.minf.stbl.stco || trak.mdia.minf.stbl.co64;
stsc = trak.mdia.minf.stbl.stsc;
stsz = trak.mdia.minf.stbl.stsz || trak.mdia.minf.stbl.stz2;
stts = trak.mdia.minf.stbl.stts;
ctts = trak.mdia.minf.stbl.ctts;
stss = trak.mdia.minf.stbl.stss;
stsd = trak.mdia.minf.stbl.stsd;
subs = trak.mdia.minf.stbl.subs;
stdp = trak.mdia.minf.stbl.stdp;
sbgps = trak.mdia.minf.stbl.sbgps;
sgpds = trak.mdia.minf.stbl.sgpds;
last_sample_in_stts_run = -1;
stts_run_index = -1;
last_sample_in_ctts_run = -1;
ctts_run_index = -1;
last_stss_index = 0;
subs_entry_index = 0;
last_subs_sample_index = 0;
ISOFile.initSampleGroups(trak, null, sbgps, sgpds);
if (typeof stsz === "undefined") {
return;
}
/* we build the samples one by one and compute their properties */
for (j = 0; j < stsz.sample_sizes.length; j++) {
var sample = {};
sample.number = j;
sample.track_id = trak.tkhd.track_id;
sample.timescale = trak.mdia.mdhd.timescale;
sample.alreadyRead = 0;
trak.samples[j] = sample;
/* size can be known directly */
sample.size = stsz.sample_sizes[j];
trak.samples_size += sample.size;
/* computing chunk-based properties (offset, sample description index)*/
if (j === 0) {
chunk_index = 1; /* the first sample is in the first chunk (chunk indexes are 1-based) */
chunk_run_index = 0; /* the first chunk is the first entry in the first_chunk table */
sample.chunk_index = chunk_index;
sample.chunk_run_index = chunk_run_index;
last_sample_in_chunk = stsc.samples_per_chunk[chunk_run_index];
offset_in_chunk = 0;
/* Is there another entry in the first_chunk table ? */
if (chunk_run_index + 1 < stsc.first_chunk.length) {
/* The last chunk in the run is the chunk before the next first chunk */
last_chunk_in_run = stsc.first_chunk[chunk_run_index + 1] - 1;
} else {
/* There is only one entry in the table, it is valid for all future chunks*/
last_chunk_in_run = Infinity;
}
} else {
if (j < last_sample_in_chunk) {
/* the sample is still in the current chunk */
sample.chunk_index = chunk_index;
sample.chunk_run_index = chunk_run_index;
} else {
/* the sample is in the next chunk */
chunk_index++;
sample.chunk_index = chunk_index;
/* reset the accumulated offset in the chunk */
offset_in_chunk = 0;
if (chunk_index <= last_chunk_in_run) ; else {
chunk_run_index++;
/* Is there another entry in the first_chunk table ? */
if (chunk_run_index + 1 < stsc.first_chunk.length) {
/* The last chunk in the run is the chunk before the next first chunk */
last_chunk_in_run = stsc.first_chunk[chunk_run_index + 1] - 1;
} else {
/* There is only one entry in the table, it is valid for all future chunks*/
last_chunk_in_run = Infinity;
}
}
sample.chunk_run_index = chunk_run_index;
last_sample_in_chunk += stsc.samples_per_chunk[chunk_run_index];
}
}
sample.description_index = stsc.sample_description_index[sample.chunk_run_index] - 1;
sample.description = stsd.entries[sample.description_index];
sample.offset = stco.chunk_offsets[sample.chunk_index - 1] + offset_in_chunk; /* chunk indexes are 1-based */
offset_in_chunk += sample.size;
/* setting dts, cts, duration and rap flags */
if (j > last_sample_in_stts_run) {
stts_run_index++;
if (last_sample_in_stts_run < 0) {
last_sample_in_stts_run = 0;
}
last_sample_in_stts_run += stts.sample_counts[stts_run_index];
}
if (j > 0) {
trak.samples[j - 1].duration = stts.sample_deltas[stts_run_index];
trak.samples_duration += trak.samples[j - 1].duration;
sample.dts = trak.samples[j - 1].dts + trak.samples[j - 1].duration;
} else {
sample.dts = 0;
}
if (ctts) {
if (j >= last_sample_in_ctts_run) {
ctts_run_index++;
if (last_sample_in_ctts_run < 0) {
last_sample_in_ctts_run = 0;
}
last_sample_in_ctts_run += ctts.sample_counts[ctts_run_index];
}
sample.cts = trak.samples[j].dts + ctts.sample_offsets[ctts_run_index];
} else {
sample.cts = sample.dts;
}
if (stss) {
if (j == stss.sample_numbers[last_stss_index] - 1) {
// sample numbers are 1-based
sample.is_sync = true;
last_stss_index++;
} else {
sample.is_sync = false;
sample.degradation_priority = 0;
}
if (subs) {
if (subs.entries[subs_entry_index].sample_delta + last_subs_sample_index == j + 1) {
sample.subsamples = subs.entries[subs_entry_index].subsamples;
last_subs_sample_index += subs.entries[subs_entry_index].sample_delta;
subs_entry_index++;
}
}
} else {
sample.is_sync = true;
}
ISOFile.process_sdtp(trak.mdia.minf.stbl.sdtp, sample, sample.number);
if (stdp) {
sample.degradation_priority = stdp.priority[j];
} else {
sample.degradation_priority = 0;
}
if (subs) {
if (subs.entries[subs_entry_index].sample_delta + last_subs_sample_index == j) {
sample.subsamples = subs.entries[subs_entry_index].subsamples;
last_subs_sample_index += subs.entries[subs_entry_index].sample_delta;
}
}
if (sbgps.length > 0 || sgpds.length > 0) {
ISOFile.setSampleGroupProperties(trak, sample, j, trak.sample_groups_info);
}
}
if (j > 0) {
trak.samples[j - 1].duration = Math.max(trak.mdia.mdhd.duration - trak.samples[j - 1].dts, 0);
trak.samples_duration += trak.samples[j - 1].duration;
}
};
/* Update sample list when new 'moof' boxes are received */
ISOFile.prototype.updateSampleLists = function () {
var i, j, k;
var default_sample_description_index, default_sample_duration, default_sample_size, default_sample_flags;
var last_run_position;
var box, moof, traf, trak, trex;
var sample;
var sample_flags;
if (this.moov === undefined) {
return;
}
/* if the input file is fragmented and fetched in multiple downloads, we need to update the list of samples */
while (this.lastMoofIndex < this.moofs.length) {
box = this.moofs[this.lastMoofIndex];
this.lastMoofIndex++;
if (box.type == "moof") {
moof = box;
for (i = 0; i < moof.trafs.length; i++) {
traf = moof.trafs[i];
trak = this.getTrackById(traf.tfhd.track_id);
trex = this.getTrexById(traf.tfhd.track_id);
if (traf.tfhd.flags & BoxParser.TFHD_FLAG_SAMPLE_DESC) {
default_sample_description_index = traf.tfhd.default_sample_description_index;
} else {
default_sample_description_index = trex ? trex.default_sample_description_index : 1;
}
if (traf.tfhd.flags & BoxParser.TFHD_FLAG_SAMPLE_DUR) {
default_sample_duration = traf.tfhd.default_sample_duration;
} else {
default_sample_duration = trex ? trex.default_sample_duration : 0;
}
if (traf.tfhd.flags & BoxParser.TFHD_FLAG_SAMPLE_SIZE) {
default_sample_size = traf.tfhd.default_sample_size;
} else {
default_sample_size = trex ? trex.default_sample_size : 0;
}
if (traf.tfhd.flags & BoxParser.TFHD_FLAG_SAMPLE_FLAGS) {
default_sample_flags = traf.tfhd.default_sample_flags;
} else {
default_sample_flags = trex ? trex.default_sample_flags : 0;
}
traf.sample_number = 0;
/* process sample groups */
if (traf.sbgps.length > 0) {
ISOFile.initSampleGroups(trak, traf, traf.sbgps, trak.mdia.minf.stbl.sgpds, traf.sgpds);
}
for (j = 0; j < traf.truns.length; j++) {
var trun = traf.truns[j];
for (k = 0; k < trun.sample_count; k++) {
sample = {};
sample.moof_number = this.lastMoofIndex;
sample.number_in_traf = traf.sample_number;
traf.sample_number++;
sample.number = trak.samples.length;
traf.first_sample_index = trak.samples.length;
trak.samples.push(sample);
sample.track_id = trak.tkhd.track_id;
sample.timescale = trak.mdia.mdhd.timescale;
sample.description_index = default_sample_description_index - 1;
sample.description = trak.mdia.minf.stbl.stsd.entries[sample.description_index];
sample.size = default_sample_size;
if (trun.flags & BoxParser.TRUN_FLAGS_SIZE) {
sample.size = trun.sample_size[k];
}
trak.samples_size += sample.size;
sample.duration = default_sample_duration;
if (trun.flags & BoxParser.TRUN_FLAGS_DURATION) {
sample.duration = trun.sample_duration[k];
}
trak.samples_duration += sample.duration;
if (trak.first_traf_merged || k > 0) {
sample.dts = trak.samples[trak.samples.length - 2].dts + trak.samples[trak.samples.length - 2].duration;
} else {
if (traf.tfdt) {
sample.dts = traf.tfdt.baseMediaDecodeTime;
} else {
sample.dts = 0;
}
trak.first_traf_merged = true;
}
sample.cts = sample.dts;
if (trun.flags & BoxParser.TRUN_FLAGS_CTS_OFFSET) {
sample.cts = sample.dts + trun.sample_composition_time_offset[k];
}
sample_flags = default_sample_flags;
if (trun.flags & BoxParser.TRUN_FLAGS_FLAGS) {
sample_flags = trun.sample_flags[k];
} else if (k === 0 && trun.flags & BoxParser.TRUN_FLAGS_FIRST_FLAG) {
sample_flags = trun.first_sample_flags;
}
sample.is_sync = sample_flags >> 16 & 0x1 ? false : true;
sample.is_leading = sample_flags >> 26 & 0x3;
sample.depends_on = sample_flags >> 24 & 0x3;
sample.is_depended_on = sample_flags >> 22 & 0x3;
sample.has_redundancy = sample_flags >> 20 & 0x3;
sample.degradation_priority = sample_flags & 0xFFFF;
//ISOFile.process_sdtp(traf.sdtp, sample, sample.number_in_traf);
var bdop = traf.tfhd.flags & BoxParser.TFHD_FLAG_BASE_DATA_OFFSET ? true : false;
var dbim = traf.tfhd.flags & BoxParser.TFHD_FLAG_DEFAULT_BASE_IS_MOOF ? true : false;
var dop = trun.flags & BoxParser.TRUN_FLAGS_DATA_OFFSET ? true : false;
var bdo = 0;
if (!bdop) {
if (!dbim) {
if (j === 0) {
// the first track in the movie fragment
bdo = moof.start; // the position of the first byte of the enclosing Movie Fragment Box
} else {
bdo = last_run_position; // end of the data defined by the preceding *track* (irrespective of the track id) fragment in the moof
}
} else {
bdo = moof.start;
}
} else {
bdo = traf.tfhd.base_data_offset;
}
if (j === 0 && k === 0) {
if (dop) {
sample.offset = bdo + trun.data_offset; // If the data-offset is present, it is relative to the base-data-offset established in the track fragment header
} else {
sample.offset = bdo; // the data for this run starts the base-data-offset defined by the track fragment header
}
} else {
sample.offset = last_run_position; // this run starts immediately after the data of the previous run
}
last_run_position = sample.offset + sample.size;
if (traf.sbgps.length > 0 || traf.sgpds.length > 0 || trak.mdia.minf.stbl.sbgps.length > 0 || trak.mdia.minf.stbl.sgpds.length > 0) {
ISOFile.setSampleGroupProperties(trak, sample, sample.number_in_traf, traf.sample_groups_info);
}
}
}
if (traf.subs) {
trak.has_fragment_subsamples = true;
var sample_index = traf.first_sample_index;
for (j = 0; j < traf.subs.entries.length; j++) {
sample_index += traf.subs.entries[j].sample_delta;
sample = trak.samples[sample_index - 1];
sample.subsamples = traf.subs.entries[j].subsamples;
}
}
}
}
}
};
/* Try to get sample data for a given sample:
returns null if not found
returns the same sample if already requested
*/
ISOFile.prototype.getSample = function (trak, sampleNum) {
var buffer;
var sample = trak.samples[sampleNum];
if (!this.moov) {
return null;
}
if (!sample.data) {
/* Not yet fetched */
sample.data = new Uint8Array(sample.size);
sample.alreadyRead = 0;
this.samplesDataSize += sample.size;
Log.debug("ISOFile", "Allocating sample #" + sampleNum + " on track #" + trak.tkhd.track_id + " of size " + sample.size + " (total: " + this.samplesDataSize + ")");
} else if (sample.alreadyRead == sample.size) {
/* Already fetched entirely */
return sample;
}
/* The sample has only been partially fetched, we need to check in all buffers */
while (true) {
var index = this.stream.findPosition(true, sample.offset + sample.alreadyRead, false);
if (index > -1) {
buffer = this.stream.buffers[index];
var lengthAfterStart = buffer.byteLength - (sample.offset + sample.alreadyRead - buffer.fileStart);
if (sample.size - sample.alreadyRead <= lengthAfterStart) {
/* the (rest of the) sample is entirely contained in this buffer */
Log.debug("ISOFile", "Getting sample #" + sampleNum + " data (alreadyRead: " + sample.alreadyRead + " offset: " + (sample.offset + sample.alreadyRead - buffer.fileStart) + " read size: " + (sample.size - sample.alreadyRead) + " full size: " + sample.size + ")");
DataStream.memcpy(sample.data.buffer, sample.alreadyRead, buffer, sample.offset + sample.alreadyRead - buffer.fileStart, sample.size - sample.alreadyRead);
/* update the number of bytes used in this buffer and check if it needs to be removed */
buffer.usedBytes += sample.size - sample.alreadyRead;
this.stream.logBufferLevel();
sample.alreadyRead = sample.size;
return sample;
} else {
/* the sample does not end in this buffer */
if (lengthAfterStart === 0) return null;
Log.debug("ISOFile", "Getting sample #" + sampleNum + " partial data (alreadyRead: " + sample.alreadyRead + " offset: " + (sample.offset + sample.alreadyRead - buffer.fileStart) + " read size: " + lengthAfterStart + " full size: " + sample.size + ")");
DataStream.memcpy(sample.data.buffer, sample.alreadyRead, buffer, sample.offset + sample.alreadyRead - buffer.fileStart, lengthAfterStart);
sample.alreadyRead += lengthAfterStart;
/* update the number of bytes used in this buffer and check if it needs to be removed */
buffer.usedBytes += lengthAfterStart;
this.stream.logBufferLevel();
/* keep looking in the next buffer */
}
} else {
return null;
}
}
};
/* Release the memory used to store the data of the sample */
ISOFile.prototype.releaseSample = function (trak, sampleNum) {
var sample = trak.samples[sampleNum];
if (sample.data) {
this.samplesDataSize -= sample.size;
sample.data = null;
sample.alreadyRead = 0;
return sample.size;
} else {
return 0;
}
};
ISOFile.prototype.getAllocatedSampleDataSize = function () {
return this.samplesDataSize;
};
/* Builds the MIME Type 'codecs' sub-parameters for the whole file */
ISOFile.prototype.getCodecs = function () {
var i;
var codecs = "";
for (i = 0; i < this.moov.traks.length; i++) {
var trak = this.moov.traks[i];
if (i > 0) {
codecs += ",";
}
codecs += trak.mdia.minf.stbl.stsd.entries[0].getCodec();
}
return codecs;
};
/* Helper function */
ISOFile.prototype.getTrexById = function (id) {
var i;
if (!this.moov || !this.moov.mvex) return null;
for (i = 0; i < this.moov.mvex.trexs.length; i++) {
var trex = this.moov.mvex.trexs[i];
if (trex.track_id == id) return trex;
}
return null;
};
/* Helper function */
ISOFile.prototype.getTrackById = function (id) {
if (this.moov === undefined) {
return null;
}
for (var j = 0; j < this.moov.traks.length; j++) {
var trak = this.moov.traks[j];
if (trak.tkhd.track_id == id) return trak;
}
return null;
};
// file:src/isofile-item-processing.js
ISOFile.prototype.items = [];
ISOFile.prototype.entity_groups = [];
/* size of the buffers allocated for samples */
ISOFile.prototype.itemsDataSize = 0;
ISOFile.prototype.flattenItemInfo = function () {
var items = this.items;
var entity_groups = this.entity_groups;
var i, j;
var item;
var meta = this.meta;
if (meta === null || meta === undefined) return;
if (meta.hdlr === undefined) return;
if (meta.iinf === undefined) return;
for (i = 0; i < meta.iinf.item_infos.length; i++) {
item = {};
item.id = meta.iinf.item_infos[i].item_ID;
items[item.id] = item;
item.ref_to = [];
item.name = meta.iinf.item_infos[i].item_name;
if (meta.iinf.item_infos[i].protection_index > 0) {
item.protection = meta.ipro.protections[meta.iinf.item_infos[i].protection_index - 1];
}
if (meta.iinf.item_infos[i].item_type) {
item.type = meta.iinf.item_infos[i].item_type;
} else {
item.type = "mime";
}
item.content_type = meta.iinf.item_infos[i].content_type;
item.content_encoding = meta.iinf.item_infos[i].content_encoding;
}
if (meta.grpl) {
for (i = 0; i < meta.grpl.boxes.length; i++) {
entity_group = {};
entity_group.id = meta.grpl.boxes[i].group_id;
entity_group.entity_ids = meta.grpl.boxes[i].entity_ids;
entity_group.type = meta.grpl.boxes[i].type;
entity_groups[entity_group.id] = entity_group;
}
}
if (meta.iloc) {
for (i = 0; i < meta.iloc.items.length; i++) {
var itemloc = meta.iloc.items[i];
item = items[itemloc.item_ID];
if (itemloc.data_reference_index !== 0) {
Log.warn("Item storage with reference to other files: not supported");
item.source = meta.dinf.boxes[itemloc.data_reference_index - 1];
}
switch (itemloc.construction_method) {
case 0:
// offset into the file referenced by the data reference index
break;
case 1:
// offset into the idat box of this meta box
Log.warn("Item storage with construction_method : not supported");
break;
case 2:
// offset into another item
Log.warn("Item storage with construction_method : not supported");
break;
}
item.extents = [];
item.size = 0;
for (j = 0; j < itemloc.extents.length; j++) {
item.extents[j] = {};
item.extents[j].offset = itemloc.extents[j].extent_offset + itemloc.base_offset;
item.extents[j].length = itemloc.extents[j].extent_length;
item.extents[j].alreadyRead = 0;
item.size += item.extents[j].length;
}
}
}
if (meta.pitm) {
items[meta.pitm.item_id].primary = true;
}
if (meta.iref) {
for (i = 0; i < meta.iref.references.length; i++) {
var ref = meta.iref.references[i];
for (j = 0; j < ref.references.length; j++) {
items[ref.from_item_ID].ref_to.push({
type: ref.type,
id: ref.references[j]
});
}
}
}
if (meta.iprp) {
for (var k = 0; k < meta.iprp.ipmas.length; k++) {
var ipma = meta.iprp.ipmas[k];
for (i = 0; i < ipma.associations.length; i++) {
var association = ipma.associations[i];
item = items[association.id];
if (!item) {
item = entity_groups[association.id];
}
if (item) {
if (item.properties === undefined) {
item.properties = {};
item.properties.boxes = [];
}
for (j = 0; j < association.props.length; j++) {
var propEntry = association.props[j];
if (propEntry.property_index > 0 && propEntry.property_index - 1 < meta.iprp.ipco.boxes.length) {
var propbox = meta.iprp.ipco.boxes[propEntry.property_index - 1];
item.properties[propbox.type] = propbox;
item.properties.boxes.push(propbox);
}
}
}
}
}
}
};
ISOFile.prototype.getItem = function (item_id) {
var buffer;
var item;
if (!this.meta) {
return null;
}
item = this.items[item_id];
if (!item.data && item.size) {
/* Not yet fetched */
item.data = new Uint8Array(item.size);
item.alreadyRead = 0;
this.itemsDataSize += item.size;
Log.debug("ISOFile", "Allocating item #" + item_id + " of size " + item.size + " (total: " + this.itemsDataSize + ")");
} else if (item.alreadyRead === item.size) {
/* Already fetched entirely */
return item;
}
/* The item has only been partially fetched, we need to check in all buffers to find the remaining extents*/
for (var i = 0; i < item.extents.length; i++) {
var extent = item.extents[i];
if (extent.alreadyRead === extent.length) {
continue;
} else {
var index = this.stream.findPosition(true, extent.offset + extent.alreadyRead, false);
if (index > -1) {
buffer = this.stream.buffers[index];
var lengthAfterStart = buffer.byteLength - (extent.offset + extent.alreadyRead - buffer.fileStart);
if (extent.length - extent.alreadyRead <= lengthAfterStart) {
/* the (rest of the) extent is entirely contained in this buffer */
Log.debug("ISOFile", "Getting item #" + item_id + " extent #" + i + " data (alreadyRead: " + extent.alreadyRead + " offset: " + (extent.offset + extent.alreadyRead - buffer.fileStart) + " read size: " + (extent.length - extent.alreadyRead) + " full extent size: " + extent.length + " full item size: " + item.size + ")");
DataStream.memcpy(item.data.buffer, item.alreadyRead, buffer, extent.offset + extent.alreadyRead - buffer.fileStart, extent.length - extent.alreadyRead);
/* update the number of bytes used in this buffer and check if it needs to be removed */
buffer.usedBytes += extent.length - extent.alreadyRead;
this.stream.logBufferLevel();
item.alreadyRead += extent.length - extent.alreadyRead;
extent.alreadyRead = extent.length;
} else {
/* the sample does not end in this buffer */
Log.debug("ISOFile", "Getting item #" + item_id + " extent #" + i + " partial data (alreadyRead: " + extent.alreadyRead + " offset: " + (extent.offset + extent.alreadyRead - buffer.fileStart) + " read size: " + lengthAfterStart + " full extent size: " + extent.length + " full item size: " + item.size + ")");
DataStream.memcpy(item.data.buffer, item.alreadyRead, buffer, extent.offset + extent.alreadyRead - buffer.fileStart, lengthAfterStart);
extent.alreadyRead += lengthAfterStart;
item.alreadyRead += lengthAfterStart;
/* update the number of bytes used in this buffer and check if it needs to be removed */
buffer.usedBytes += lengthAfterStart;
this.stream.logBufferLevel();
return null;
}
} else {
return null;
}
}
}
if (item.alreadyRead === item.size) {
/* fetched entirely */
return item;
} else {
return null;
}
};
/* Release the memory used to store the data of the item */
ISOFile.prototype.releaseItem = function (item_id) {
var item = this.items[item_id];
if (item.data) {
this.itemsDataSize -= item.size;
item.data = null;
item.alreadyRead = 0;
for (var i = 0; i < item.extents.length; i++) {
var extent = item.extents[i];
extent.alreadyRead = 0;
}
return item.size;
} else {
return 0;
}
};
ISOFile.prototype.processItems = function (callback) {
for (var i in this.items) {
var item = this.items[i];
this.getItem(item.id);
if (callback && !item.sent) {
callback(item);
item.sent = true;
item.data = null;
}
}
};
ISOFile.prototype.hasItem = function (name) {
for (var i in this.items) {
var item = this.items[i];
if (item.name === name) {
return item.id;
}
}
return -1;
};
ISOFile.prototype.getMetaHandler = function () {
if (!this.meta) {
return null;
} else {
return this.meta.hdlr.handler;
}
};
ISOFile.prototype.getPrimaryItem = function () {
if (!this.meta || !this.meta.pitm) {
return null;
} else {
return this.getItem(this.meta.pitm.item_id);
}
};
ISOFile.prototype.itemToFragmentedTrackFile = function (_options) {
var options = _options || {};
var item = null;
if (options.itemId) {
item = this.getItem(options.itemId);
} else {
item = this.getPrimaryItem();
}
if (item == null) return null;
var file = new ISOFile();
file.discardMdatData = false;
// assuming the track type is the same as the item type
var trackOptions = {
type: item.type,
description_boxes: item.properties.boxes
};
if (item.properties.ispe) {
trackOptions.width = item.properties.ispe.image_width;
trackOptions.height = item.properties.ispe.image_height;
}
var trackId = file.addTrack(trackOptions);
if (trackId) {
file.addSample(trackId, item.data);
return file;
} else {
return null;
}
};
// file:src/isofile-write.js
/* Rewrite the entire file */
ISOFile.prototype.write = function (outstream) {
for (var i = 0; i < this.boxes.length; i++) {
this.boxes[i].write(outstream);
}
};
ISOFile.prototype.createFragment = function (track_id, sampleNumber, stream_) {
var trak = this.getTrackById(track_id);
var sample = this.getSample(trak, sampleNumber);
if (sample == null) {
this.setNextSeekPositionFromSample(trak.samples[sampleNumber]);
return null;
}
var stream = stream_ || new DataStream();
stream.endianness = DataStream.BIG_ENDIAN;
var moof = this.createSingleSampleMoof(sample);
moof.write(stream);
/* adjusting the data_offset now that the moof size is known*/
moof.trafs[0].truns[0].data_offset = moof.size + 8; //8 is mdat header
Log.debug("MP4Box", "Adjusting data_offset with new value " + moof.trafs[0].truns[0].data_offset);
stream.adjustUint32(moof.trafs[0].truns[0].data_offset_position, moof.trafs[0].truns[0].data_offset);
var mdat = new BoxParser.mdatBox();
mdat.data = sample.data;
mdat.write(stream);
return stream;
};
/* Modify the file and create the initialization segment */
ISOFile.writeInitializationSegment = function (ftyp, moov, total_duration, sample_duration) {
var i;
Log.debug("ISOFile", "Generating initialization segment");
var stream = new DataStream();
stream.endianness = DataStream.BIG_ENDIAN;
ftyp.write(stream);
/* we can now create the new mvex box */
var mvex = moov.add("mvex");
if (total_duration) {
mvex.add("mehd").set("fragment_duration", total_duration);
}
for (i = 0; i < moov.traks.length; i++) {
mvex.add("trex").set("track_id", moov.traks[i].tkhd.track_id).set("default_sample_description_index", 1).set("default_sample_duration", sample_duration).set("default_sample_size", 0).set("default_sample_flags", 1 << 16);
}
moov.write(stream);
return stream.buffer;
};
ISOFile.prototype.save = function (name) {
var stream = new DataStream();
stream.endianness = DataStream.BIG_ENDIAN;
this.write(stream);
stream.save(name);
};
ISOFile.prototype.getBuffer = function () {
var stream = new DataStream();
stream.endianness = DataStream.BIG_ENDIAN;
this.write(stream);
return stream.buffer;
};
ISOFile.prototype.initializeSegmentation = function () {
var i;
var initSegs;
var trak;
var seg;
if (this.onSegment === null) {
Log.warn("MP4Box", "No segmentation callback set!");
}
if (!this.isFragmentationInitialized) {
this.isFragmentationInitialized = true;
this.nextMoofNumber = 0;
this.resetTables();
}
initSegs = [];
for (i = 0; i < this.fragmentedTracks.length; i++) {
var moov = new BoxParser.moovBox();
moov.mvhd = this.moov.mvhd;
moov.boxes.push(moov.mvhd);
trak = this.getTrackById(this.fragmentedTracks[i].id);
moov.boxes.push(trak);
moov.traks.push(trak);
seg = {};
seg.id = trak.tkhd.track_id;
seg.user = this.fragmentedTracks[i].user;
seg.buffer = ISOFile.writeInitializationSegment(this.ftyp, moov, this.moov.mvex && this.moov.mvex.mehd ? this.moov.mvex.mehd.fragment_duration : undefined, this.moov.traks[i].samples.length > 0 ? this.moov.traks[i].samples[0].duration : 0);
initSegs.push(seg);
}
return initSegs;
};
// file:src/box-print.js
/*
* Copyright (c) Telecom ParisTech/TSI/MM/GPAC Cyril Concolato
* License: BSD-3-Clause (see LICENSE file)
*/
BoxParser.Box.prototype.printHeader = function (output) {
this.size += 8;
if (this.size > MAX_SIZE) {
this.size += 8;
}
if (this.type === "uuid") {
this.size += 16;
}
output.log(output.indent + "size:" + this.size);
output.log(output.indent + "type:" + this.type);
};
BoxParser.FullBox.prototype.printHeader = function (output) {
this.size += 4;
BoxParser.Box.prototype.printHeader.call(this, output);
output.log(output.indent + "version:" + this.version);
output.log(output.indent + "flags:" + this.flags);
};
BoxParser.Box.prototype.print = function (output) {
this.printHeader(output);
};
BoxParser.ContainerBox.prototype.print = function (output) {
this.printHeader(output);
for (var i = 0; i < this.boxes.length; i++) {
if (this.boxes[i]) {
var prev_indent = output.indent;
output.indent += " ";
this.boxes[i].print(output);
output.indent = prev_indent;
}
}
};
ISOFile.prototype.print = function (output) {
output.indent = "";
for (var i = 0; i < this.boxes.length; i++) {
if (this.boxes[i]) {
this.boxes[i].print(output);
}
}
};
BoxParser.mvhdBox.prototype.print = function (output) {
BoxParser.FullBox.prototype.printHeader.call(this, output);
output.log(output.indent + "creation_time: " + this.creation_time);
output.log(output.indent + "modification_time: " + this.modification_time);
output.log(output.indent + "timescale: " + this.timescale);
output.log(output.indent + "duration: " + this.duration);
output.log(output.indent + "rate: " + this.rate);
output.log(output.indent + "volume: " + (this.volume >> 8));
output.log(output.indent + "matrix: " + this.matrix.join(", "));
output.log(output.indent + "next_track_id: " + this.next_track_id);
};
BoxParser.tkhdBox.prototype.print = function (output) {
BoxParser.FullBox.prototype.printHeader.call(this, output);
output.log(output.indent + "creation_time: " + this.creation_time);
output.log(output.indent + "modification_time: " + this.modification_time);
output.log(output.indent + "track_id: " + this.track_id);
output.log(output.indent + "duration: " + this.duration);
output.log(output.indent + "volume: " + (this.volume >> 8));
output.log(output.indent + "matrix: " + this.matrix.join(", "));
output.log(output.indent + "layer: " + this.layer);
output.log(output.indent + "alternate_group: " + this.alternate_group);
output.log(output.indent + "width: " + this.width);
output.log(output.indent + "height: " + this.height);
}; // file:src/mp4box.js
/*
* Copyright (c) 2012-2013. Telecom ParisTech/TSI/MM/GPAC Cyril Concolato
* License: BSD-3-Clause (see LICENSE file)
*/
var MP4Box = {};
MP4Box.createFile = function (_keepMdatData, _stream) {
/* Boolean indicating if bytes containing media data should be kept in memory */
var keepMdatData = _keepMdatData !== undefined ? _keepMdatData : true;
var file = new ISOFile(_stream);
file.discardMdatData = keepMdatData ? false : true;
return file;
};
{
exports.createFile = MP4Box.createFile;
}
});
mp4box.Log;
mp4box.MP4BoxStream;
mp4box.DataStream;
mp4box.MultiBufferStream;
mp4box.MPEG4DescriptorParser;
mp4box.BoxParser;
mp4box.XMLSubtitlein4Parser;
mp4box.Textin4Parser;
mp4box.ISOFile;
mp4box.createFile;
function reduce(array) {
return array.reduce((e, t) => 256 * e + t);
}
function range(array) {
const s = [101, 103, 119, 99];
const a = 28;
const t = array.length - a,
r = array.slice(t, t + s.length);
return s.every((e, t) => e === r[t]);
}
class TransportDescrambler {
constructor() {
this.s = null;
this.a = null;
this.l = 0;
this.c = 0;
this.u = 1 / 0;
this.A = false;
this.d = false;
this.r = 4194304;
this.n = new Uint8Array([30, 158, 90, 33, 244, 57, 83, 165, 2, 70, 35, 87, 215, 231, 226, 108]);
this.t = this.n.slice().reverse();
}
destroy() {
this.s = null;
this.a = null;
this.l = 0;
this.c = 0;
this.u = 1 / 0;
this.A = false;
this.d = false;
this.r = 4194304;
this.n = null;
this.t = null;
}
transport(buffer) {
if (!this.s && this.l > 50) {
return buffer;
}
this.l++;
if (this.d) {
return buffer;
}
const h = new Uint8Array(buffer);
if (this.A) {
if (!(this.c < this.u)) {
if (this.a && this.s) {
this.a.set(h, this.r);
this.s.parse(null, this.r, h.byteLength);
return this.a.slice(this.r, this.r + h.byteLength);
} else {
console.error("video_error_2");
this.d = true;
return buffer;
}
}
if (range(h)) {
this.c++;
}
} else {
const r = function (e, t) {
const r = function (e, t) {
for (let r = 0; r < e.byteLength - t.length; r++) for (let n = 0; n < t.length && e[r + n] === t[n]; n++) if (n === t.length - 1) return r;
return null;
}(e, t);
if (r) {
const t = reduce(e.slice(r + 16, r + 16 + 8));
return [t, reduce(e.slice(r + 24, r + 24 + 8)), function (e) {
return e.map(e => ~e);
}(e.slice(r + 32, r + 32 + t))];
}
return null;
}(h, this.t);
if (!r) {
return buffer;
}
const l = function (e) {
try {
if ("object" != typeof WebAssembly || "function" != typeof WebAssembly.instantiate) {
throw null;
}
{
const e = new WebAssembly.Module(Uint8Array.of(0, 97, 115, 109, 1, 0, 0, 0));
if (!(e instanceof WebAssembly.Module && new WebAssembly.Instance(e) instanceof WebAssembly.Instance)) throw null;
}
} catch (e) {
return new Error("video_error_4");
}
let t;
try {
t = {
env: {
__handle_stack_overflow: () => e(new Error("video_error_1")),
memory: new WebAssembly.Memory({
initial: 256,
maximum: 256
})
}
};
} catch (e) {
return new Error("video_error_5");
}
return t;
}(buffer);
if (l instanceof Error) {
console.error(l.message);
this.d = true;
return buffer;
}
this.A = true;
this.u = r[1];
if (range(h)) {
this.c++;
}
WebAssembly.instantiate(r[2], l).then(t => {
if (!function (e) {
return "function" == typeof e.parse && "object" == typeof e.memory;
}(t.instance.exports)) {
this.d = true;
console.error('video_error_3');
return;
}
this.s = t.instance.exports;
this.a = new Uint8Array(this.s.memory.buffer);
}).catch(t => {
this.d = true;
console.error('video_error_6');
});
}
return buffer;
}
}
class Fmp4Loader extends CommonLoader {
constructor(player) {
super(player);
this.TAG_NAME = 'Fmp4Loader';
this.player = player;
this.mp4Box = mp4box.createFile();
this.tempFmp4List = [];
this.offset = 0;
this.videoTrackId = null;
this.audioTrackId = null;
this.isHevc = false;
this.transportDescarmber = null;
if (this.player._opt.isFmp4Private) {
this.transportDescarmber = new TransportDescrambler();
}
this._listenMp4Box();
player.debug.log(this.TAG_NAME, 'init');
}
destroy() {
if (this.mp4Box) {
this.mp4Box.flush();
this.mp4Box = null;
}
if (this.transportDescarmber) {
this.transportDescarmber.destroy();
this.transportDescarmber = null;
}
this.tempFmp4List = [];
this.offset = 0;
this.videoTrackId = null;
this.audioTrackId = null;
this.isHevc = false;
this.player.debug.log(this.TAG_NAME, 'destroy');
}
_listenMp4Box() {
this.mp4Box.onReady = this.onReady.bind(this);
this.mp4Box.onError = this.onError.bind(this);
this.mp4Box.onSamples = this.onSamples.bind(this);
}
onReady(info) {
this.player.debug.log(this.TAG_NAME, 'onReady', info);
const videoTrack = info.videoTracks[0];
const audioTrack = info.audioTracks[0];
if (videoTrack) {
//
this.videoTrackId = videoTrack.id;
const seqHeader = this.getSeqHeader(videoTrack);
if (seqHeader) {
this.player.debug.log(this.TAG_NAME, 'seqHeader', seqHeader);
this._doDecodeByFmp4(seqHeader, MEDIA_TYPE.video, 0, true, 0);
}
this.mp4Box.setExtractionOptions(videoTrack.id);
}
if (audioTrack && this.player._opt.hasAudio) {
this.audioTrackId = audioTrack.id;
const audioInfo = audioTrack.audio || {};
const sampleRateIndex = AAC_FREQ_LIST.indexOf(audioInfo.sample_rate);
const profile = audioTrack.codec.replace('mp4a.40.', '');
this.mp4Box.setExtractionOptions(audioTrack.id);
const config = {
profile: parseInt(profile, 10),
sampleRate: sampleRateIndex,
channel: audioInfo.channel_count
};
const aacADTSHeader = aacEncoderConfigurationRecordV2(config);
this.player.debug.log(this.TAG_NAME, 'aacADTSHeader', aacADTSHeader, 'config', config);
this._doDecodeByFmp4(aacADTSHeader, MEDIA_TYPE.audio, 0, false, 0);
}
this.mp4Box.start();
}
onError(error) {
this.player.debug.error(this.TAG_NAME, 'mp4Box onError', error);
}
onSamples(trackId, ref, samples) {
// this.player.debug.log(this.TAG_NAME, 'onSamples', trackId, ref, samples);
if (trackId === this.videoTrackId) {
for (const sample of samples) {
const data = sample.data;
const isIFrame = sample.is_sync;
const timestamp = 1000 * sample.cts / sample.timescale;
1000 * sample.duration / sample.timescale;
this.player.updateStats({
vbps: data.byteLength,
dts: timestamp
});
if (isIFrame) {
this.calcIframeIntervalTimestamp(timestamp);
}
let packet = null;
if (this.isHevc) {
packet = hevcEncoderNalePacketNotLength(data, isIFrame);
} else {
packet = avcEncoderNalePacketNotLength(data, isIFrame);
}
this._doDecodeByFmp4(packet, MEDIA_TYPE.video, timestamp, isIFrame, 0);
}
} else if (trackId === this.audioTrackId) {
if (this.player._opt.hasAudio) {
for (const sample of samples) {
const data = sample.data;
this.player.updateStats({
abps: data.byteLength
});
const timestamp = 1000 * sample.cts / sample.timescale;
1000 * sample.duration / sample.timescale;
const packet = new Uint8Array(data.byteLength + 2);
packet.set([0xAF, 0x01], 0);
packet.set(data, 2);
// this.player.debug.log(this.TAG_NAME, 'onSamples: audio', 'timestamp', timestamp, 'duration', duration)
this._doDecodeByFmp4(packet, MEDIA_TYPE.audio, timestamp, false, 0);
}
}
} else {
this.player.debug.warn(this.TAG_NAME, 'onSamples() trackId error', trackId);
}
}
getSeqHeader(track) {
const trak = this.mp4Box.getTrackById(track.id);
for (const entry of trak.mdia.minf.stbl.stsd.entries) {
if (entry.avcC || entry.hvcC) {
const stream = new mp4box.DataStream(undefined, 0, mp4box.DataStream.BIG_ENDIAN);
let prevData = [];
if (entry.avcC) {
entry.avcC.write(stream);
prevData = [0x17, 0x00, 0x00, 0x00, 0x00];
} else {
this.isHevc = true;
entry.hvcC.write(stream);
prevData = [0x1c, 0x00, 0x00, 0x00, 0x00];
}
const seqHeader = new Uint8Array(stream.buffer, 8); // Remove the box header.
const newData = new Uint8Array(prevData.length + seqHeader.length);
newData.set(prevData, 0);
newData.set(seqHeader, prevData.length);
return newData;
}
}
return null;
}
dispatch(data) {
let buffer = new Uint8Array(data);
if (typeof data === 'string') {
this.player.debug.warn(this.TAG_NAME, 'dispatch()', 'data is string', data);
return;
}
if (typeof data === 'object') {
if (this.transportDescarmber) {
buffer = this.transportDescarmber.transport(buffer);
}
// this.tempFmp4List.push(buffer);
buffer.buffer.fileStart = this.offset;
this.offset += buffer.byteLength;
this.mp4Box.appendBuffer(buffer.buffer);
} else {
this.player.debug.warn(this.TAG_NAME, 'dispatch()', 'data is not object', data);
}
}
downloadFmp4File() {
const blob = new Blob(this.tempFmp4List, {
type: 'video/mp4; codecs="avc1.640028,mp4a.40.2"'
});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = now$2() + '.fmp4';
a.click();
URL.revokeObjectURL(url);
}
}
class Mpeg4Loader extends CommonLoader {
constructor(player) {
super(player);
_defineProperty$1(this, "LOG_NAME", 'Mpeg4Loader');
this.player = player;
this.player.debug.log(this.LOG_NAME, 'init');
}
destroy() {
super.destroy();
this.player.debug.log(this.LOG_NAME, 'destroy');
}
}
function concatUint8Array() {
for (var _len = arguments.length, arr = new Array(_len), _key = 0; _key < _len; _key++) {
arr[_key] = arguments[_key];
}
arr = arr.filter(Boolean);
if (arr.length < 2) return arr[0];
const data = new Uint8Array(arr.reduce((p, c) => p + c.byteLength, 0));
let prevLen = 0;
arr.forEach(d => {
data.set(d, prevLen);
prevLen += d.byteLength;
});
return data;
}
class HlsTsLoader extends CommonLoader {
constructor(player) {
super(player);
this.player = player;
this._pmtId = -1;
this._remainingPacketData = null;
this._videoPesData = [];
this._audioPesData = [];
this._gopId = 0;
this._videoPid = -1;
this._audioPid = -1;
this._codecType = VIDEO_ENC_CODE.h264;
this._audioCodecType = AUDIO_ENC_CODE.AAC;
this._vps = null;
this._sps = null;
this._pps = null;
this.TAG_NAME = 'HlsTsLoader';
this._isForHls = true;
this.videoTrack = HlsTsLoader.initVideoTrack();
this.audioTrack = HlsTsLoader.initAudioTrack();
this._baseDts = -1;
this._baseDtsInited = false;
this._basefps = 25; // audio + video
this._baseFpsInterval = null;
this._tempSampleTsList = [];
this._hasAudio = false;
this._hasVideo = false;
this._audioNextPts = undefined;
this._videoNextDts = undefined;
this._audioTimestampBreak = false;
this._videoTimestampBreak = false;
this._lastAudioExceptionGapDot = 0;
this._lastAudioExceptionOverlapDot = 0;
this._lastAudioExceptionLargeGapDot = 0;
this._isSendAACSeqHeader = false;
this.workerClearTimeout = null;
this.workerUrl = null;
this.loopWorker = null;
this.tempSampleListInfo = {};
if (this._isUseWorker()) {
this._initLoopWorker();
}
this.player.debug.log(this.TAG_NAME, 'init');
}
destroy() {
super.destroy();
if (this.workerUrl) {
URL.revokeObjectURL(this.workerUrl);
this.workerUrl = null;
}
if (this.workerClearTimeout) {
clearTimeout(this.workerClearTimeout);
this.workerClearTimeout = null;
}
if (this.loopWorker) {
this.loopWorker.postMessage({
cmd: 'destroy'
});
this.loopWorker.terminate();
this.loopWorker = null;
}
this._stopDecodeLoopInterval();
this.videoTrack = null;
this.audioTrack = null;
this.tempSampleListInfo = {};
this._baseDts = -1;
this._baseDtsInited = false;
this._basefps = 25;
this._hasCalcFps = false;
this._audioNextPts = undefined;
this._videoNextDts = undefined;
this._audioTimestampBreak = false;
this._videoTimestampBreak = false;
this._lastAudioExceptionGapDot = 0;
this._lastAudioExceptionOverlapDot = 0;
this._lastAudioExceptionLargeGapDot = 0;
this._isForHls = true;
this._isSendAACSeqHeader = false;
this.player.debug.log(this.TAG_NAME, 'destroy');
}
static initVideoTrack() {
return {
samples: []
};
}
static initAudioTrack() {
return {
samples: []
};
}
static probe(data) {
if (!data.length) return false;
return data[0] === 0x47 && data[188] === 0x47 && data[376] === 0x47;
}
_parsePES(data) {
const headerDataLen = data[8];
if (headerDataLen === null || headerDataLen === undefined || data.length < headerDataLen + 9) {
return;
}
const startPrefix = data[0] << 16 | data[1] << 8 | data[2];
if (startPrefix !== 1) {
this.player.debug.warn(this.TAG_NAME, `_parsePES startPrefix should be 1 but with value ${startPrefix}`);
return {
error: true,
code: 'startPrefixError'
};
}
const pesLen = (data[4] << 8) + data[5];
if (pesLen && pesLen > data.length - 6) {
return;
}
let pts;
let dts;
const ptsDtsFlags = data[7];
if (ptsDtsFlags & 0xc0) {
pts = (data[9] & 0x0e) * 536870912 + (data[10] & 0xff) * 4194304 + (data[11] & 0xfe) * 16384 + (data[12] & 0xff) * 128 + (data[13] & 0xfe) / 2;
if (ptsDtsFlags & 0x40) {
dts = (data[14] & 0x0e) * 536870912 + (data[15] & 0xff) * 4194304 + (data[16] & 0xfe) * 16384 + (data[17] & 0xff) * 128 + (data[18] & 0xfe) / 2;
if (pts - dts > 60 * 90000) pts = dts;
} else {
dts = pts;
}
}
return {
data: data.subarray(9 + headerDataLen),
pts: pts,
dts: dts,
originalPts: pts,
originalDts: dts
};
}
_demux(data) {
let discontinuity = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
let contiguous = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
if (discontinuity) {
this._pmtId = -1;
this.videoTrack = HlsTsLoader.initVideoTrack();
this.audioTrack = HlsTsLoader.initAudioTrack();
}
// contiguous is false or discontinuity is true
if (!contiguous || discontinuity) {
this._remainingPacketData = null;
this._videoPesData = [];
this._audioPesData = [];
} else {
// 清空上次的pes数据
this.videoTrack.samples = [];
this.audioTrack.samples = [];
if (this._remainingPacketData) {
data = concatUint8Array(this._remainingPacketData, data);
this._remainingPacketData = null;
}
}
let dataLen = data.length;
// remaining length
const remainingLength = dataLen % 188;
//
if (remainingLength) {
this._remainingPacketData = data.subarray(dataLen - remainingLength);
dataLen -= remainingLength;
}
for (let start = 0; start < dataLen; start += 188) {
// check start with 0x47
if (data[start] !== 0x47) {
throw new Error('TS packet did not start with 0x47');
}
const payloadUnitStartIndicator = !!(data[start + 1] & 0x40);
const pid = ((data[start + 1] & 0x1f) << 8) + data[start + 2];
const adaptationFiledControl = (data[start + 3] & 0x30) >> 4;
let offset;
if (adaptationFiledControl > 1) {
// [start + 4] adaptation_field_length
offset = start + 5 + data[start + 4];
if (offset === start + 188) continue;
} else {
offset = start + 4;
}
switch (pid) {
case 0:
// PAT
if (payloadUnitStartIndicator) {
offset += data[offset] + 1;
}
this._pmtId = (data[offset + 10] & 0x1f) << 8 | data[offset + 11];
break;
case this._pmtId:
{
if (payloadUnitStartIndicator) {
offset += data[offset] + 1;
}
const tableEnd = offset + 3 + ((data[offset + 1] & 0x0f) << 8 | data[offset + 2]) - 4;
const programInfoLength = (data[offset + 10] & 0x0f) << 8 | data[offset + 11];
offset += 12 + programInfoLength;
while (offset < tableEnd) {
const esPid = (data[offset + 1] & 0x1f) << 8 | data[offset + 2];
switch (data[offset]) {
case 0x0f:
// AAC ADTS
//console.log('AAC ADTS pid is', esPid);
this._audioPid = esPid;
this._audioCodecType = AUDIO_ENC_CODE.AAC;
break;
case 0x1b:
// AVC
// console.log('AVC pid is', esPid);
if (this._videoPid !== -1) {
break;
}
this._videoPid = esPid;
this._codecType = VIDEO_ENC_CODE.h264;
break;
case 0x24:
// HEVC
// console.log('HEVC pid is', esPid);
if (this._videoPid !== -1) {
break;
}
this._videoPid = esPid;
this._codecType = VIDEO_ENC_CODE.h265;
break;
default:
// console.warn(`Unsupported stream. type: ${data[offset]}, pid: ${esPid}`)
this.player.debug.log(this.TAG_NAME, `Unsupported stream. type: ${data[offset]}, pid: ${esPid}`);
break;
}
offset += ((data[offset + 3] & 0x0f) << 8 | data[offset + 4]) + 5;
}
}
break;
case this._videoPid:
if (payloadUnitStartIndicator && this._videoPesData.length) {
this._parseVideoData();
}
this._videoPesData.push(data.subarray(offset, start + 188));
break;
case this._audioPid:
if (payloadUnitStartIndicator && this._audioPesData.length) {
this._parseAudioData();
}
this._audioPesData.push(data.subarray(offset, start + 188));
break;
case 17:
case 0x1fff:
break;
default:
// console.warn(`Unknown pid: ${pid}`);
this.player.debug.log(this.TAG_NAME, `Unknown pid: ${pid}`);
break;
}
}
this._parseVideoData();
this._parseAudioData();
this.audioTrack.formatTimescale = this.videoTrack.formatTimescale = this.videoTrack.timescale = 90000;
this.audioTrack.timescale = this.audioTrack.sampleRate || 0;
}
/**
*
* @param data
* @param discontinuity 是否是分片的第一个ts
* @param contiguous 是否是连续的ts
* @param startTime ts的开始时间
*/
demuxAndFix(data, discontinuity, contiguous, startTime) {
//
if (!this.player._times.demuxStart) {
this.player._times.demuxStart = now$2();
}
this._demux(data, discontinuity, contiguous);
this._fix(startTime, discontinuity, contiguous);
}
_parseVideoData() {
if (!this._videoPesData.length) {
// console.log('_parseVideoData', 'no video pes data');
this.player.debug.log(this.TAG_NAME, '_parseVideoData', 'no video pes data');
return;
}
//
const pes = this._parsePES(concatUint8Array(...this._videoPesData));
if (!pes || pes.error) {
if (pes && pes.code === 'startPrefixError') {
this._videoPesData = [];
}
return;
}
const units = parseAnnexB(pes.data);
if (units) {
this._createVideoSample(units, pes.pts, pes.dts);
} else {
this.player.debug.warn(this.TAG_NAME, 'Cannot parse avc units', pes);
}
this._videoPesData = [];
}
_createVideoSample(units, pts, dts) {
if (!units.length) {
return;
}
const isHevc = this._codecType === VIDEO_ENC_CODE.h265;
const videoSample = {
isIFrame: false,
type: MEDIA_TYPE.video,
isHevc,
vps: null,
sps: null,
pps: null,
pts,
dts,
payload: null
};
units.forEach(unit => {
const type = isHevc ? unit[0] >>> 1 & 0x3f : unit[0] & 0x1f;
switch (type) {
case 5: // IDR
case 16: // HEVC BLA_W_LP
case 17: // HEVC BLA_W_RADL
case 18: // HEVC BLA_N_LP
case 19: // HEVC IDR_W_RADL
case 20: // HEVC IDR_N_LP
case 21: // HEVC CRA_NUT
case 22: // HEVC RSV_IRAP_VCL22
case 23:
// HEVC RSV_IRAP_VCL23
if (!isHevc && type !== 5 || isHevc && type === 5) {
break;
}
videoSample.isIFrame = true;
this._gopId++;
break;
case 6: // SEI
case 39: // HEVC PREFIX_SEI
case 40:
// HEVC SUFFIX_SEI
if (!isHevc && type !== 6 || isHevc && type === 6) {
break;
}
// todo sei信息
parseSEI(removeEPB(unit), isHevc);
// if (this.player._opt.isEmitSEI) {
// this.player.emit(EVENTS.videoSEI, {
// ts: dts / 90,
// data: unit
// })
// }
// fix 分割nal之前只要sei信息被当做单独一个sample
return;
case 32:
// HEVC VPS
if (!isHevc) {
break;
}
if (!videoSample.vps) {
videoSample.vps = unit;
}
// todo vps信息
break;
case 7: // SPS
case 33:
// HEVC SPS
if (!isHevc && type !== 7 || isHevc && type === 7) {
break;
}
if (!videoSample.sps) {
videoSample.sps = unit;
}
// todo sps信息
//console.log('sps', unit);
break;
case 8: // PPS
case 34:
// HEVC PPS
if (!isHevc && type !== 8 || isHevc && type === 8) {
break;
}
if (!videoSample.pps) {
videoSample.pps = unit;
}
// todo pps信息
break;
}
// 264/265 merge unit
if (isHevc && isNotHevcSeqHead(type) || !isHevc && isNotAvcSeqHead(type)) {
// 拼接
const newUnit = addNaleHeaderLength(unit);
if (!videoSample.payload) {
videoSample.payload = newUnit;
} else {
// merge
// this.player.debug.log(this.TAG_NAME, 'append nal unit to payload');
const newBuffer = new Uint8Array(videoSample.payload.byteLength + newUnit.byteLength);
newBuffer.set(videoSample.payload, 0);
newBuffer.set(newUnit, videoSample.payload.byteLength);
videoSample.payload = newBuffer;
}
}
});
let seqHeader = null;
if (isHevc) {
if (videoSample.sps && videoSample.vps && videoSample.pps) {
seqHeader = hevcEncoderConfigurationRecord$2({
vps: videoSample.vps,
sps: videoSample.sps,
pps: videoSample.pps
});
}
} else {
if (videoSample.sps && videoSample.pps) {
seqHeader = avcEncoderConfigurationRecord$2({
sps: videoSample.sps,
pps: videoSample.pps
});
}
}
if (seqHeader) {
this.player.debug.log(this.TAG_NAME, '_createVideoSample', 'seqHeader');
this._doDecodeByHls(seqHeader, MEDIA_TYPE.video, Math.round(videoSample.pts / 90), true, 0);
}
if (videoSample.isIFrame) {
this.calcIframeIntervalTimestamp(Math.round(videoSample.dts / 90));
}
this.videoTrack.samples = this.videoTrack.samples.concat(videoSample);
}
_parseAudioData() {
if (!this._audioPesData.length) return;
if (!this.player._opt.hasAudio) {
return;
}
const pes = this._parsePES(concatUint8Array(...this._audioPesData));
if (!pes || pes.error) {
if (pes && pes.code === 'startPrefixError') {
this._audioPesData = [];
}
return;
}
// console.log('_parseAudioData', pes.dts, pes.pts);
if (this._audioCodecType === AUDIO_ENC_CODE.AAC) {
const ret = parseADTS(pes.data, pes.originalPts);
if (ret) {
this.audioTrack.codec = ret.codec;
this.audioTrack.sampleRate = ret.sampleRate;
this.audioTrack.channelCount = ret.channelCount;
if (!this._isSendAACSeqHeader) {
// 先要取 accADTSHeader = [];
const accADTSHeader = aacEncoderConfigurationRecordV2({
profile: ret.objectType,
sampleRate: ret.samplingFrequencyIndex,
channel: ret.channelCount
});
this._isSendAACSeqHeader = true;
this.player.debug.log(this.TAG_NAME, 'aac seq header', `profile: ${ret.objectType}, sampleRate:${ret.sampleRate},sampleRateIndex: ${ret.samplingFrequencyIndex}, channel: ${ret.channelCount}`);
this._doDecodeByHls(accADTSHeader, MEDIA_TYPE.audio, 0, false, 0);
}
if (this._isSendAACSeqHeader) {
const audioSampleList = [];
ret.frames.forEach(s => {
const pts = s.pts;
const arrayBuffer = new Uint8Array(s.data.length + 2);
arrayBuffer.set([0xAF, 0x01], 0);
arrayBuffer.set(s.data, 2);
const audioSample = {
type: MEDIA_TYPE.audio,
pts,
dts: pts,
payload: arrayBuffer
};
audioSampleList.push(audioSample);
});
this.audioTrack.samples = this.audioTrack.samples.concat(audioSampleList);
} else {
this.player.debug.warn(this.TAG_NAME, `aac seq header not send`);
}
} else {
this.player.debug.warn(this.TAG_NAME, `aac parseADTS error`);
}
}
this._audioPesData = [];
}
_fix() {
let startTime = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
let discontinuity = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
let contiguous = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
startTime = Math.round(startTime * 90000);
const videoTrack = this.videoTrack;
const audioTrack = this.audioTrack;
const vSamples = videoTrack.samples;
const aSamples = audioTrack.samples;
if (!vSamples.length && !aSamples.length) {
return;
}
const firstVideoSample = vSamples[0];
const firstAudioSample = aSamples[0];
// consider av delta
let vaDelta = 0;
if (vSamples.length && aSamples.length) {
vaDelta = firstVideoSample.dts - firstAudioSample.pts;
}
if (!this._baseDtsInited) {
this._calculateBaseDts();
}
// recalc baseDts
if (discontinuity) {
this._calculateBaseDts();
this._baseDts -= startTime;
}
if (!contiguous) {
/**
* segment.start = min(a, v)
* segment.start
* |
* a
* -- vaDelta --
* v
*/
this._videoNextDts = vaDelta > 0 ? startTime + vaDelta : startTime;
this._audioNextPts = vaDelta > 0 ? startTime : startTime - vaDelta;
const vDeltaToNextDts = firstVideoSample ? firstVideoSample.dts - this._baseDts - this._videoNextDts : 0;
const aDeltaToNextDts = firstAudioSample ? firstAudioSample.pts - this._baseDts - this._audioNextPts : 0;
// 90000 = 1s
if (Math.abs(vDeltaToNextDts || aDeltaToNextDts) > TS_MAX_VIDEO_FRAME_DURATION) {
this._calculateBaseDts(this.audioTrack, this.videoTrack);
this._baseDts -= startTime;
}
}
this._resetBaseDtsWhenStreamBreaked();
// fix audio first
this._fixAudio(audioTrack);
this._fixVideo(videoTrack);
let allSampleList = videoTrack.samples.concat(audioTrack.samples);
// sort
allSampleList = allSampleList.map(sample => {
sample.dts = Math.round(sample.dts / 90);
sample.pts = Math.round(sample.pts / 90);
sample.cts = sample.pts - sample.dts;
return sample;
}).sort((a, b) => {
return a.dts - b.dts;
});
allSampleList.forEach(sample => {
const arrayBuffer = new Uint8Array(sample.payload);
delete sample.payload;
if (this._isUseWorker()) {
this.loopWorker.postMessage({
...sample,
payload: arrayBuffer,
cmd: 'sample'
}, [arrayBuffer.buffer]);
} else {
if (sample.type === MEDIA_TYPE.video) {
// 直接解码
this._doDecodeVideo({
...sample,
payload: arrayBuffer
});
} else if (sample.type === MEDIA_TYPE.audio) {
this._doDecodeAudio({
...sample,
payload: arrayBuffer
});
}
}
});
if (isFalse(this._hasCalcFps) && this._isUseWorker()) {
this._hasCalcFps = true;
this._calcDecodeFps(allSampleList);
}
// todo 如果缓存数据超过一定量,需要清理,防止延迟过高
}
_isUseWorker() {
return !this.player.isUseMSE() && this.isForHls();
}
_calculateBaseDts() {
const audioTrack = this.audioTrack;
const videoTrack = this.videoTrack;
const audioSamps = audioTrack.samples;
const videoSamps = videoTrack.samples;
if (!audioSamps.length && !videoSamps.length) {
return false;
}
let audioBasePts = Infinity;
let videoBaseDts = Infinity;
if (audioSamps.length) {
audioTrack.baseDts = audioBasePts = audioSamps[0].pts;
}
if (videoSamps.length) {
videoTrack.baseDts = videoBaseDts = videoSamps[0].dts;
}
this._baseDts = Math.min(audioBasePts, videoBaseDts);
const delta = videoBaseDts - audioBasePts;
if (Number.isFinite(delta) && Math.abs(delta) > TS_LARGE_AV_FIRST_FRAME_GAP) {
// warn: LARGE_AV_SHIFT
this.player.debug.warn(this.TAG_NAME, `large av first frame gap,
video pts: ${videoBaseDts},
audio pts: ${audioBasePts},
base dts: ${this._baseDts},
detect is: ${delta}`);
}
this._baseDtsInited = true;
return true;
}
_resetBaseDtsWhenStreamBreaked() {
if (this._baseDtsInited && this._videoTimestampBreak && this._audioTimestampBreak) {
/**
* timestamp breaked
* _audioNextDts
* ---------------------|
* (_baseDts) _videoNextDts
* ----------------------|
* <----------------
* nextVideo.dts
* ----------------------------------------|
* nextAudio.dts
* ---------------------------------------|
*/
// calc baseDts base on new samples
const calc = this._calculateBaseDts(this.audioTrack, this.videoTrack);
if (!calc) return;
// consider the expect dts for next frame
this._baseDts -= Math.min(this._audioNextPts, this._videoNextDts);
this._audioLastSample = null;
this._videoLastSample = null;
this._videoTimestampBreak = false;
this._audioTimestampBreak = false;
}
}
_fixAudio(audioTrack) {
const samples = audioTrack.samples;
if (!samples.length) return;
samples.forEach(x => {
x.pts -= this._baseDts;
x.dts = x.pts;
});
this._doFixAudioInternal(audioTrack, samples, 90000);
}
_fixVideo(videoTrack) {
const samples = videoTrack.samples;
if (!samples.length) return;
samples.forEach(x => {
x.dts -= this._baseDts;
x.pts -= this._baseDts;
});
if (this._videoNextDts === undefined) {
const samp0 = samples[0];
this._videoNextDts = samp0.dts;
}
const len = samples.length;
let sampleDuration = 0;
const firstSample = samples[0];
const nextSample = samples[1];
const vDelta = this._videoNextDts - firstSample.dts;
// 90000 / 2 // 500ms
if (Math.abs(vDelta) > TS_MAX_DTS_DELTA_WITH_NEXT_CHUNK) {
// resolve first frame first
firstSample.dts += vDelta;
firstSample.pts += vDelta;
this.player.debug.warn(this.TAG_NAME, `large video gap between chunk,
next dts is ${this._videoNextDts},
first dts is ${firstSample.dts},
next dts is ${nextSample.dts},
duration is ${vDelta}`);
// check to ajust the whole segment
// // 1s
if (nextSample && Math.abs(nextSample.dts - firstSample.dts) > TS_MAX_VIDEO_FRAME_DURATION) {
this._videoTimestampBreak = true;
samples.forEach((x, i) => {
if (i === 0) return;
x.dts += vDelta;
x.pts += vDelta;
});
}
}
let refSampleDurationInt;
const first = videoTrack.samples[0];
const last = videoTrack.samples[len - 1];
// 100ms default
refSampleDurationInt = len === 1 ? 9000 : Math.floor((last.dts - first.dts) / (len - 1));
for (let i = 0; i < len; i++) {
const dts = samples[i].dts;
const nextSample = samples[i + 1];
if (i < len - 1) {
sampleDuration = nextSample.dts - dts;
} else if (samples[i - 1]) {
sampleDuration = Math.min(dts - samples[i - 1].dts, refSampleDurationInt);
} else {
sampleDuration = refSampleDurationInt;
}
if (sampleDuration > TS_MAX_VIDEO_FRAME_DURATION || sampleDuration < 0) {
// dts exception of adjacent frame
this._videoTimestampBreak = true;
// check if only video breaked!
sampleDuration = this._audioTimestampBreak ? refSampleDurationInt : Math.max(sampleDuration, 30 * 90); // 30ms
// check if sample breaked within current fragment
const expectFragEnd = this._audioNextPts || 0;
if (nextSample && nextSample.dts > expectFragEnd) {
sampleDuration = refSampleDurationInt;
}
// todo: LARGE_VIDEO_GAP
this.player.debug.warn(this.TAG_NAME, `large video gap between frames,
time is ${dts / videoTrack.timescale},
dts is ${dts},
origin dts is ${samples[i].originalDts},
next dts is ${this._videoNextDts},
sample Duration is ${sampleDuration} ,
ref Sample DurationInt is ${refSampleDurationInt}`);
}
samples[i].duration = sampleDuration;
this._videoNextDts += sampleDuration;
}
}
_doFixAudioInternal(audioTrack, samples, timescale) {
if (!audioTrack.sampleDuration) {
audioTrack.sampleDuration = getFrameDuration$1(audioTrack.timescale, timescale);
}
const refSampleDuration = audioTrack.sampleDuration;
if (this._audioNextPts === undefined) {
const samp0 = samples[0];
this._audioNextPts = samp0.pts;
}
for (let i = 0; i < samples.length; i++) {
const nextPts = this._audioNextPts;
const sample = samples[i];
const delta = sample.pts - nextPts;
// fill frames
// delta >= 3 * refSampleDurationInt
// delta <= 500s
if (!this._audioTimestampBreak && delta >= TS_AUDIO_GAP_OVERLAP_THRESHOLD_COUNT * refSampleDuration && delta <= TS_MAX_SILENT_FRAME_DURATION && !isSafari()) {
getSilentFrame(audioTrack.codec, audioTrack.channelCount) || samples[0].data.subarray();
const count = Math.floor(delta / refSampleDuration);
if (Math.abs(sample.pts - this._lastAudioExceptionGapDot) > TS_AUDIO_EXCETION_LOG_EMIT_DURATION) {
this._lastAudioExceptionGapDot = sample.pts;
}
// todo:WarningType.AUDIO_FILLED
this.player.debug.warn(this.TAG_NAME, `audio gap detected,
pts is ${samples.pts},
originPts is ${samples.originalPts},
count is ${count},
nextPts is ${nextPts},
ref sample duration is ${refSampleDuration}`);
for (let j = 0; j < count; j++) {
// const silentSample = new AudioSample(Math.floor(nextPts), silentFrame)
// silentSample.originPts = Math.floor(this._baseDts + nextPts)
// samples.splice(i, 0, silentSample)
this._audioNextPts += refSampleDuration;
i++;
}
i--;
// delta <= -3 * refSampleDurationInt
// delta >= -500ms
} else if (delta <= -TS_AUDIO_GAP_OVERLAP_THRESHOLD_COUNT * refSampleDuration && delta >= -1 * TS_MAX_SILENT_FRAME_DURATION) {
// need discard frames
if (Math.abs(sample.pts - this._lastAudioExceptionOverlapDot) > TS_AUDIO_EXCETION_LOG_EMIT_DURATION) {
this._lastAudioExceptionOverlapDot = sample.pts;
// todo:AUDIO_DROPPED
this.player.debug.warn(this.TAG_NAME, `audio overlap detected,
pts is ${sample.pts},
originPts is ${sample.originalPts},
nextPts is ${nextPts},
ref sample duration is ${refSampleDuration}`);
}
samples.splice(i, 1);
i--;
} else {
if (Math.abs(delta) >= TS_MAX_SILENT_FRAME_DURATION) {
this._audioTimestampBreak = true;
if (Math.abs(sample.pts - this._lastAudioExceptionLargeGapDot) > TS_AUDIO_EXCETION_LOG_EMIT_DURATION) {
this._lastAudioExceptionLargeGapDot = sample.pts;
// todo: LARGE_AUDIO_GAP
this.player.debug.warn(this.TAG_NAME, `large audio gap detected,
time is ${sample.pts / 1000}
pts is ${sample.pts},
originPts is ${sample.originalPts},
nextPts is ${nextPts},
sample duration is ${delta}
ref sample duration is ${refSampleDuration}`);
}
}
sample.dts = sample.pts = nextPts;
this._audioNextPts += refSampleDuration;
}
}
}
_calcDecodeFps(sampleList) {
const _tempSampleTsList = sampleList.map(sample => {
return {
ts: sample.dts || sample.pts,
type: sample.type
};
});
const streamVideoFps = calcStreamFpsByBufferList(_tempSampleTsList, MEDIA_TYPE.video);
if (streamVideoFps) {
this.player.debug.log(this.TAG_NAME, `_calcDecodeFps() video fps is ${streamVideoFps}, update base fps is ${this._basefps}`);
this._basefps = streamVideoFps;
}
this._postMessageToLoopWorker('updateBaseFps', {
baseFps: this._basefps
});
}
_initLoopWorker() {
this.player.debug.log(this.TAG_NAME, '_initLoopWorker()');
// worker fun
// worker 里面跑interval
function LoopWorkerFun() {
const MEDIA_TYPE = {
audio: 1,
video: 2
};
class LoopWorker {
constructor() {
this.baseFps = 0;
this.fpsInterval = null;
this.preLoopTimestamp = null;
this.startBpsTime = null;
this.allSampleList = [];
}
destroy() {
this._clearInterval();
this.baseFps = 0;
this.allSampleList = [];
this.preLoopTimestamp = null;
this.startBpsTime = null;
}
updateBaseFps(baseFps) {
this.baseFps = baseFps;
this._clearInterval();
this._startInterval();
}
pushSample(sample) {
delete sample.cmd;
this.allSampleList.push(sample);
}
_startInterval() {
const fragDuration = Math.ceil(1000 / this.baseFps);
this.fpsInterval = setInterval(() => {
let nowTime = new Date().getTime();
if (!this.preLoopTimestamp) {
this.preLoopTimestamp = nowTime;
}
if (!this.startBpsTime) {
this.startBpsTime = nowTime;
}
const diffTime = nowTime - this.preLoopTimestamp;
if (diffTime > fragDuration * 2) {
console.warn(`JbPro:[TsLoader LoopWorker] loop interval is ${diffTime}ms, more than ${fragDuration} * 2ms`);
}
this._loop();
this.preLoopTimestamp = new Date().getTime();
if (this.startBpsTime) {
const timestamp = nowTime - this.startBpsTime;
if (timestamp >= 1000) {
this._calcSampleList();
this.startBpsTime = nowTime;
}
}
}, fragDuration);
}
_clearInterval() {
if (this.fpsInterval) {
clearInterval(this.fpsInterval);
this.fpsInterval = null;
}
}
_calcSampleList() {
const tempObj = {
buferredDuration: 0,
allListLength: this.allSampleList.length,
audioListLength: 0,
videoListLength: 0
};
this.allSampleList.forEach(sample => {
if (sample.type === MEDIA_TYPE.video) {
tempObj.videoListLength++;
if (sample.duration) {
tempObj.buferredDuration += Math.round(sample.duration / 90);
}
} else if (sample.type === MEDIA_TYPE.audio) {
tempObj.audioListLength++;
}
});
postMessage({
cmd: 'sampleListInfo',
...tempObj
});
}
_loop() {
let sample = null;
if (this.allSampleList.length) {
sample = this.allSampleList.shift();
if (sample.type === MEDIA_TYPE.video) {
postMessage({
cmd: 'decodeVideo',
...sample
}, [sample.payload.buffer]);
// check next is audio
let tempSample = this.allSampleList[0];
// 这边是检查了所有的audio
while (tempSample && tempSample.type === MEDIA_TYPE.audio) {
sample = this.allSampleList.shift();
postMessage({
cmd: 'decodeAudio',
...sample
}, [sample.payload.buffer]);
tempSample = this.allSampleList[0];
}
} else if (sample.type === MEDIA_TYPE.audio) {
postMessage({
cmd: 'decodeAudio',
...sample
}, [sample.payload.buffer]);
// check next is video
// todo:这个就检查了一个。
if (this.allSampleList.length && this.allSampleList[0].type === MEDIA_TYPE.video) {
sample = this.allSampleList.shift();
postMessage({
cmd: 'decodeVideo',
...sample
}, [sample.payload.buffer]);
}
}
}
}
}
let loopWorker = new LoopWorker();
self.onmessage = e => {
const msg = e.data;
switch (msg.cmd) {
case 'updateBaseFps':
loopWorker.updateBaseFps(msg.baseFps);
break;
case 'sample':
loopWorker.pushSample(msg);
break;
case 'destroy':
loopWorker.destroy();
loopWorker = null;
break;
}
};
}
const loopWorkerString = function2String(LoopWorkerFun.toString());
const blob = new Blob([loopWorkerString], {
type: "text/javascript"
});
const workerUrl = URL.createObjectURL(blob);
let loopWorker = new Worker(workerUrl);
this.workerUrl = workerUrl;
// 必须要释放,不然每次调用内存都明显泄露内存
// chrome 83 file协议下如果直接释放,将会使WebWorker无法启动
this.workerClearTimeout = setTimeout(() => {
window.URL.revokeObjectURL(this.workerUrl);
this.workerUrl = null;
this.workerClearTimeout = null;
}, URL_OBJECT_CLEAR_TIME);
loopWorker.onmessage = event => {
const msg = event.data;
switch (msg.cmd) {
case 'decodeVideo':
this._doDecodeVideo(msg);
break;
case 'decodeAudio':
this._doDecodeAudio(msg);
break;
case 'sampleListInfo':
this.tempSampleListInfo = msg;
break;
}
};
this.loopWorker = loopWorker;
}
_postMessageToLoopWorker(cmd, options) {
if (!this._isUseWorker()) {
return;
}
if (this.loopWorker) {
this.loopWorker.postMessage({
cmd,
...options
});
} else {
this.player.debug.warn(this.TAG_NAME, `loop worker is not init, can not post message`);
}
}
_doDecodeAudio(sample) {
const uint8Array = new Uint8Array(sample.payload);
this.player.updateStats({
abps: uint8Array.byteLength
});
let payloadBuffer = uint8Array;
if (isTrue(this.player._opt.m7sCryptoAudio)) {
payloadBuffer = this.cryptoPayloadAudio(uint8Array);
}
if (this.isForHls()) {
this._doDecodeByHls(payloadBuffer, MEDIA_TYPE.audio, sample.dts, false, 0);
} else {
this._doDecodeByTs(payloadBuffer, MEDIA_TYPE.audio, sample.dts, false, 0);
}
}
_doDecodeVideo(sample) {
const uint8Array = new Uint8Array(sample.payload);
let packet = null;
if (sample.isHevc) {
// add 5 header
packet = hevcEncoderNalePacketNotLength(uint8Array, sample.isIFrame);
} else {
packet = avcEncoderNalePacketNotLength(uint8Array, sample.isIFrame);
}
this.player.updateStats({
dts: sample.dts,
vbps: packet.byteLength
});
const cts = sample.pts - sample.dts;
let payloadBuffer = this.cryptoPayload(packet, sample.isIFrame);
if (this.isForHls()) {
this._doDecodeByHls(payloadBuffer, MEDIA_TYPE.video, sample.dts, sample.isIFrame, cts);
} else {
this._doDecodeByTs(payloadBuffer, MEDIA_TYPE.video, sample.dts, sample.isIFrame, cts);
}
}
_stopDecodeLoopInterval() {
if (this._baseFpsInterval) {
clearInterval(this._baseFpsInterval);
this._baseFpsInterval = null;
}
}
getBuferredDuration() {
return this.tempSampleListInfo.buferredDuration || 0;
}
getSampleListLength() {
return this.tempSampleListInfo.allListLength || 0;
}
getSampleAudioListLength() {
return this.tempSampleListInfo.audioListLength || 0;
}
getSampleVideoListLength() {
return this.tempSampleListInfo.videoListLength || 0;
}
isForHls() {
return this._isForHls;
}
}
const TS_STREAM_TYPE = {
kMPEG1Audio: 0x03,
kMPEG2Audio: 0x04,
kPESPrivateData: 0x06,
kADTSAAC: 0x0F,
kLOASAAC: 0x11,
kAC3: 0x81,
kEAC3: 0x87,
kMetadata: 0x15,
kSCTE35: 0x86,
kH264: 0x1b,
kH265: 0x24
};
class SliceQueue {
constructor() {
this.slices = [];
this.total_length = 0;
this.expected_length = 0;
this.random_access_indicator = 0;
}
}
class PESData {
constructor() {
this.pid = null;
this.data = null;
this.stream_type = null;
this.random_access_indicator = null;
}
}
class PESPrivateData {
constructor() {
this.pid = null;
this.stream_id = null;
this.len = null;
this.data = null;
this.pts = null;
this.nearest_pts = null;
this.dts = null;
}
}
const MPEG4SamplingFrequencies = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
class MediaInfo {
constructor() {
this.mimeType = null;
this.duration = null;
this.hasAudio = null;
this.hasVideo = null;
this.audioCodec = null;
this.videoCodec = null;
this.audioDataRate = null;
this.videoDataRate = null;
this.audioSampleRate = null;
this.audioChannelCount = null;
this.width = null;
this.height = null;
this.fps = null;
this.profile = null;
this.level = null;
this.refFrames = null;
this.chromaFormat = null;
this.sarNum = null;
this.sarDen = null;
this.metadata = null;
this.segments = null; // MediaInfo[]
this.segmentCount = null;
this.hasKeyframesIndex = null;
this.keyframesIndex = null;
}
isComplete() {
let audioInfoComplete = this.hasAudio === false || this.hasAudio === true && this.audioCodec != null && this.audioSampleRate != null && this.audioChannelCount != null;
let videoInfoComplete = this.hasVideo === false || this.hasVideo === true && this.videoCodec != null && this.width != null && this.height != null && this.fps != null && this.profile != null && this.level != null && this.refFrames != null && this.chromaFormat != null && this.sarNum != null && this.sarDen != null;
// keyframesIndex may not be present
return this.mimeType != null && audioInfoComplete && videoInfoComplete;
}
isSeekable() {
return this.hasKeyframesIndex === true;
}
getNearestKeyframe(milliseconds) {
if (this.keyframesIndex == null) {
return null;
}
let table = this.keyframesIndex;
let keyframeIdx = this._search(table.times, milliseconds);
return {
index: keyframeIdx,
milliseconds: table.times[keyframeIdx],
fileposition: table.filepositions[keyframeIdx]
};
}
_search(list, value) {
let idx = 0;
let last = list.length - 1;
let mid = 0;
let lbound = 0;
let ubound = last;
if (value < list[0]) {
idx = 0;
lbound = ubound + 1; // skip search
}
while (lbound <= ubound) {
mid = lbound + Math.floor((ubound - lbound) / 2);
if (mid === last || value >= list[mid] && value < list[mid + 1]) {
idx = mid;
break;
} else if (list[mid] < value) {
lbound = mid + 1;
} else {
ubound = mid - 1;
}
}
return idx;
}
}
class AudioSpecificConfig {
constructor(frame) {
let config = null;
let original_audio_object_type = frame.audio_object_type;
let audio_object_type = frame.audio_object_type;
let sampling_index = frame.sampling_freq_index;
let channel_config = frame.channel_config;
let extension_sampling_index = 0;
let userAgent = navigator.userAgent.toLowerCase();
if (userAgent.indexOf('firefox') !== -1) {
// firefox: use SBR (HE-AAC) if freq less than 24kHz
if (sampling_index >= 6) {
audio_object_type = 5;
config = new Array(4);
extension_sampling_index = sampling_index - 3;
} else {
// use LC-AAC
audio_object_type = 2;
config = new Array(2);
extension_sampling_index = sampling_index;
}
} else if (userAgent.indexOf('android') !== -1) {
// android: always use LC-AAC
audio_object_type = 2;
config = new Array(2);
extension_sampling_index = sampling_index;
} else {
// for other browsers, e.g. chrome...
// Always use HE-AAC to make it easier to switch aac codec profile
audio_object_type = 5;
extension_sampling_index = sampling_index;
config = new Array(4);
if (sampling_index >= 6) {
extension_sampling_index = sampling_index - 3;
} else if (channel_config === 1) {
// Mono channel
audio_object_type = 2;
config = new Array(2);
extension_sampling_index = sampling_index;
}
}
config[0] = audio_object_type << 3;
config[0] |= (sampling_index & 0x0F) >>> 1;
config[1] = (sampling_index & 0x0F) << 7;
config[1] |= (channel_config & 0x0F) << 3;
if (audio_object_type === 5) {
config[1] |= (extension_sampling_index & 0x0F) >>> 1;
config[2] = (extension_sampling_index & 0x01) << 7;
// extended audio object type: force to 2 (LC-AAC)
config[2] |= 2 << 2;
config[3] = 0;
}
this.config = config;
this.sampling_rate = MPEG4SamplingFrequencies[sampling_index];
this.sampling_index = sampling_index;
this.channel_count = channel_config;
this.object_type = audio_object_type;
this.original_object_type = original_audio_object_type;
this.codec_mimetype = 'mp4a.40.' + audio_object_type;
this.original_codec_mimetype = 'mp4a.40.' + original_audio_object_type;
}
}
class TsLoaderV2 extends CommonLoader {
constructor(player) {
super(player);
this.player = player;
this.TAG_NAME = 'TsLoaderV2';
this.first_parse_ = true;
this.tsPacketSize = 0;
this.syncOffset = 0;
this.pmt_ = null;
this.config_ = null;
this.media_info_ = new MediaInfo();
this.timescale_ = 90;
this.duration_ = 0;
this.pat_ = {
version_number: 0,
network_pid: 0,
program_map_pid: {}
};
this.current_program_ = null;
this.current_pmt_pid_ = -1;
this.program_pmt_map_ = {};
this.pes_slice_queues_ = {};
this.section_slice_queues_ = {};
this.video_metadata_ = {
vps: null,
sps: null,
pps: null,
details: null
};
this.audio_metadata_ = {
codec: null,
audio_object_type: null,
sampling_freq_index: null,
sampling_frequency: null,
channel_config: null
};
this.last_pcr_ = null;
this.audio_last_sample_pts_ = undefined;
this.aac_last_incomplete_data_ = null;
this.has_video_ = false;
this.has_audio_ = false;
this.video_init_segment_dispatched_ = false;
this.audio_init_segment_dispatched_ = false;
this.video_metadata_changed_ = false;
this.audio_metadata_changed_ = false;
this.loas_previous_frame = null;
this.video_track_ = {
type: 'video',
id: 1,
sequenceNumber: 0,
samples: [],
length: 0
};
this.audio_track_ = {
type: 'audio',
id: 2,
sequenceNumber: 0,
samples: [],
length: 0
};
this._remainingPacketData = null;
this.player.debug.log(this.TAG_NAME, 'init');
}
destroy() {
this.media_info_ = null;
this.pes_slice_queues_ = null;
this.section_slice_queues_ = null;
this.video_metadata_ = null;
this.audio_metadata_ = null;
this.aac_last_incomplete_data_ = null;
this.video_track_ = null;
this.audio_track_ = null;
this._remainingPacketData = null;
super.destroy();
}
probe(buffer) {
let data = new Uint8Array(buffer);
let sync_offset = -1;
let ts_packet_size = 188;
if (data.byteLength <= 3 * ts_packet_size) {
return {
needMoreData: true
};
}
while (sync_offset === -1) {
let scan_window = Math.min(1000, data.byteLength - 3 * ts_packet_size);
for (let i = 0; i < scan_window;) {
// sync_byte should all be 0x47
if (data[i] === 0x47 && data[i + ts_packet_size] === 0x47 && data[i + 2 * ts_packet_size] === 0x47) {
sync_offset = i;
break;
} else {
i++;
}
}
// find sync_offset failed in previous ts_packet_size
if (sync_offset === -1) {
if (ts_packet_size === 188) {
// try 192 packet size (BDAV, etc.)
ts_packet_size = 192;
} else if (ts_packet_size === 192) {
// try 204 packet size (European DVB, etc.)
ts_packet_size = 204;
} else {
// 192, 204 also failed, exit
break;
}
}
}
if (sync_offset === -1) {
// both 188, 192, 204 failed, Non MPEG-TS
return {
match: false
};
}
if (ts_packet_size === 192 && sync_offset >= 4) {
// Log.v('TSDemuxer', `ts_packet_size = 192, m2ts mode`);
sync_offset -= 4;
}
return {
match: true,
consumed: 0,
ts_packet_size,
sync_offset
};
}
_initPmt() {
return {
program_number: 0,
version_number: 0,
pcr_pid: 0,
// pid -> stream_type
pid_stream_type: {},
common_pids: {
h264: undefined,
h265: undefined,
adts_aac: undefined,
loas_aac: undefined,
opus: undefined,
ac3: undefined,
eac3: undefined,
mp3: undefined
},
pes_private_data_pids: {},
timed_id3_pids: {},
synchronous_klv_pids: {},
asynchronous_klv_pids: {},
scte_35_pids: {},
smpte2038_pids: {}
};
}
dispatch(data) {
// 传入的是Uint8Array 但是这里需要ArrayBuffer
let arrayBuffer = new Uint8Array(data);
if (this._remainingPacketData) {
arrayBuffer = concatUint8Array(this._remainingPacketData, arrayBuffer);
this._remainingPacketData = null;
}
let chunk = arrayBuffer.buffer;
const offset = this.parseChunks(chunk);
// unconsumed data remain
if (offset) {
this._remainingPacketData = arrayBuffer.subarray(offset);
} else {
if (arrayBuffer.length < this.tsPacketSize) {
this._remainingPacketData = arrayBuffer;
}
}
}
parseChunks(chunk) {
// 偏移量
let offset = 0;
if (this.first_parse_) {
this.first_parse_ = false;
const probeData = this.probe(chunk);
if (probeData.match) {
this.tsPacketSize = probeData.ts_packet_size;
this.syncOffset = probeData.sync_offset;
}
offset = this.syncOffset;
this.player.debug.log(this.TAG_NAME, `isFirstDispatch and tsPacketSize = ${this.tsPacketSize}, syncOffset = ${this.syncOffset}`);
}
while (offset + this.tsPacketSize <= chunk.byteLength) {
if (this.tsPacketSize === 192) {
offset += 4;
}
const data = new Uint8Array(chunk, offset, 188);
let sync_byte = data[0];
if (sync_byte !== 0x47) {
this.player.debug.warn(this.TAG_NAME, `parseChunks() sync_byte = ${sync_byte}, not start with 0x47, so break`);
break;
}
let payload_unit_start_indicator = (data[1] & 0x40) >>> 6;
(data[1] & 0x20) >>> 5;
let pid = (data[1] & 0x1F) << 8 | data[2];
let adaptation_field_control = (data[3] & 0x30) >>> 4;
let continuity_conunter = data[3] & 0x0F;
let is_pcr_pid = this.pmt_ && this.pmt_.pcr_pid === pid ? true : false;
let adaptation_field_info = {};
let ts_payload_start_index = 4;
if (adaptation_field_control == 0x02 || adaptation_field_control == 0x03) {
let adaptation_field_length = data[4];
if (adaptation_field_length > 0 && (is_pcr_pid || adaptation_field_control == 0x03)) {
// Parse adaptation field
adaptation_field_info.discontinuity_indicator = (data[5] & 0x80) >>> 7;
adaptation_field_info.random_access_indicator = (data[5] & 0x40) >>> 6;
adaptation_field_info.elementary_stream_priority_indicator = (data[5] & 0x20) >>> 5;
let PCR_flag = (data[5] & 0x10) >>> 4;
if (PCR_flag) {
let pcr_base = data[6] << 25 | data[7] << 17 | data[8] << 9 | data[9] << 1 | data[10] >>> 7;
let pcr_extension = (data[10] & 0x01) << 8 | data[11];
let pcr = pcr_base * 300 + pcr_extension;
this.last_pcr_ = pcr;
}
}
if (adaptation_field_control == 0x02 || 5 + adaptation_field_length === 188) {
// TS packet only has adaption field, jump to next
offset += 188;
if (this.tsPacketSize === 204) {
// skip parity word (16 bytes) for RS encoded TS
offset += 16;
}
continue;
} else {
// Point ts_payload_start_index to the start of payload
ts_payload_start_index = 4 + 1 + adaptation_field_length;
}
}
if (adaptation_field_control == 0x01 || adaptation_field_control == 0x03) {
if (pid === 0 ||
// PAT (pid === 0)
pid === this.current_pmt_pid_ ||
// PMT
this.pmt_ != undefined && this.pmt_.pid_stream_type[pid] === TS_STREAM_TYPE.kSCTE35) {
// SCTE35
let ts_payload_length = 188 - ts_payload_start_index;
this.handleSectionSlice(chunk, offset + ts_payload_start_index, ts_payload_length, {
pid,
payload_unit_start_indicator,
continuity_conunter,
random_access_indicator: adaptation_field_info.random_access_indicator
});
} else if (this.pmt_ != undefined && this.pmt_.pid_stream_type[pid] != undefined) {
// PES
let ts_payload_length = 188 - ts_payload_start_index;
let stream_type = this.pmt_.pid_stream_type[pid];
// process PES only for known common_pids
if (pid === this.pmt_.common_pids.h264 || pid === this.pmt_.common_pids.h265 || pid === this.pmt_.common_pids.adts_aac || pid === this.pmt_.common_pids.loas_aac || pid === this.pmt_.common_pids.ac3 || pid === this.pmt_.common_pids.eac3 || pid === this.pmt_.common_pids.opus || pid === this.pmt_.common_pids.mp3 || this.pmt_.pes_private_data_pids[pid] === true || this.pmt_.timed_id3_pids[pid] === true || this.pmt_.synchronous_klv_pids[pid] === true || this.pmt_.asynchronous_klv_pids[pid] === true) {
this.handlePESSlice(chunk, offset + ts_payload_start_index, ts_payload_length, {
pid,
stream_type,
payload_unit_start_indicator,
continuity_conunter,
random_access_indicator: adaptation_field_info.random_access_indicator
});
}
}
}
offset += 188;
if (this.tsPacketSize === 204) {
// skip parity word (16 bytes) for RS encoded TS
offset += 16;
}
}
this.dispatchAudioVideoMediaSegment();
return offset;
}
handleSectionSlice(buffer, offset, length, misc) {
let data = new Uint8Array(buffer, offset, length);
let slice_queue = this.section_slice_queues_[misc.pid];
if (misc.payload_unit_start_indicator) {
let pointer_field = data[0];
if (slice_queue != undefined && slice_queue.total_length !== 0) {
let remain_section = new Uint8Array(buffer, offset + 1, Math.min(length, pointer_field));
slice_queue.slices.push(remain_section);
slice_queue.total_length += remain_section.byteLength;
if (slice_queue.total_length === slice_queue.expected_length) {
this.emitSectionSlices(slice_queue, misc);
} else {
this.clearSlices(slice_queue, misc);
}
}
for (let i = 1 + pointer_field; i < data.byteLength;) {
let table_id = data[i + 0];
if (table_id === 0xFF) {
// this.player.debug.log(this.TAG_NAME, `handleSectionSlice(): table_id is 0xFF, skip`);
break;
}
let section_length = (data[i + 1] & 0x0F) << 8 | data[i + 2];
this.section_slice_queues_[misc.pid] = new SliceQueue();
slice_queue = this.section_slice_queues_[misc.pid];
slice_queue.expected_length = section_length + 3;
slice_queue.random_access_indicator = misc.random_access_indicator;
let remain_section = new Uint8Array(buffer, offset + i, Math.min(length - i, slice_queue.expected_length - slice_queue.total_length));
slice_queue.slices.push(remain_section);
slice_queue.total_length += remain_section.byteLength;
if (slice_queue.total_length === slice_queue.expected_length) {
this.emitSectionSlices(slice_queue, misc);
} else if (slice_queue.total_length >= slice_queue.expected_length) {
this.clearSlices(slice_queue, misc);
}
i += remain_section.byteLength;
}
} else if (slice_queue != undefined && slice_queue.total_length !== 0) {
let remain_section = new Uint8Array(buffer, offset, Math.min(length, slice_queue.expected_length - slice_queue.total_length));
slice_queue.slices.push(remain_section);
slice_queue.total_length += remain_section.byteLength;
if (slice_queue.total_length === slice_queue.expected_length) {
this.emitSectionSlices(slice_queue, misc);
} else if (slice_queue.total_length >= slice_queue.expected_length) {
this.clearSlices(slice_queue, misc);
}
}
}
handlePESSlice(buffer, offset, length, misc) {
let data = new Uint8Array(buffer, offset, length);
let packet_start_code_prefix = data[0] << 16 | data[1] << 8 | data[2];
data[3];
let PES_packet_length = data[4] << 8 | data[5];
// start 开始阶段
if (misc.payload_unit_start_indicator) {
if (packet_start_code_prefix !== 1) {
this.player.debug.warn(this.TAG_NAME, `handlePESSlice: packet_start_code_prefix should be 1 but with value ${packet_start_code_prefix}`);
return;
}
// handle queued PES slices:
// Merge into a big Uint8Array then call parsePES()
let slice_queue = this.pes_slice_queues_[misc.pid];
if (slice_queue) {
if (slice_queue.expected_length === 0 || slice_queue.expected_length === slice_queue.total_length) {
this.emitPESSlices(slice_queue, misc);
} else {
this.clearSlices(slice_queue, misc);
}
}
// Make a new PES queue for new PES slices
this.pes_slice_queues_[misc.pid] = new SliceQueue();
this.pes_slice_queues_[misc.pid].random_access_indicator = misc.random_access_indicator;
}
if (this.pes_slice_queues_[misc.pid] == undefined) {
// ignore PES slices without [PES slice that has payload_unit_start_indicator]
return;
}
// push subsequent PES slices into pes_queue
let slice_queue = this.pes_slice_queues_[misc.pid];
slice_queue.slices.push(data);
if (misc.payload_unit_start_indicator) {
slice_queue.expected_length = PES_packet_length === 0 ? 0 : PES_packet_length + 6;
}
slice_queue.total_length += data.byteLength;
if (slice_queue.expected_length > 0 && slice_queue.expected_length === slice_queue.total_length) {
this.emitPESSlices(slice_queue, misc);
} else if (slice_queue.expected_length > 0 && slice_queue.expected_length < slice_queue.total_length) {
this.clearSlices(slice_queue, misc);
}
}
emitSectionSlices(slice_queue, misc) {
let data = new Uint8Array(slice_queue.total_length);
for (let i = 0, offset = 0; i < slice_queue.slices.length; i++) {
let slice = slice_queue.slices[i];
data.set(slice, offset);
offset += slice.byteLength;
}
slice_queue.slices = [];
slice_queue.expected_length = -1;
slice_queue.total_length = 0;
let section_data = {};
section_data.pid = misc.pid;
section_data.data = data;
section_data.file_position = slice_queue.file_position;
section_data.random_access_indicator = slice_queue.random_access_indicator;
this.parseSection(section_data);
}
emitPESSlices(slice_queue, misc) {
let data = new Uint8Array(slice_queue.total_length);
for (let i = 0, offset = 0; i < slice_queue.slices.length; i++) {
let slice = slice_queue.slices[i];
data.set(slice, offset);
offset += slice.byteLength;
}
slice_queue.slices = [];
slice_queue.expected_length = -1;
slice_queue.total_length = 0;
let pes_data = new PESData();
pes_data.pid = misc.pid;
pes_data.data = data;
pes_data.stream_type = misc.stream_type;
pes_data.random_access_indicator = slice_queue.random_access_indicator;
this.parsePES(pes_data);
}
clearSlices(slice_queue) {
slice_queue.slices = [];
slice_queue.expected_length = -1;
slice_queue.total_length = 0;
}
parseSection(section_data) {
let data = section_data.data;
let pid = section_data.pid;
if (pid === 0x00) {
this.parsePAT(data);
} else if (pid === this.current_pmt_pid_) {
this.parsePMT(data);
} else if (this.pmt_ != undefined && this.pmt_.scte_35_pids[pid]) {
this.parseSCTE35(data);
}
}
parsePES(pes_data) {
let data = pes_data.data;
let packet_start_code_prefix = data[0] << 16 | data[1] << 8 | data[2];
let stream_id = data[3];
let PES_packet_length = data[4] << 8 | data[5];
if (packet_start_code_prefix !== 1) {
this.player.debug.error(this.TAG_NAME, `parsePES: packet_start_code_prefix should be 1 but with value ${packet_start_code_prefix}`);
return;
}
if (stream_id !== 0xBC // program_stream_map
&& stream_id !== 0xBE // padding_stream
&& stream_id !== 0xBF // private_stream_2
&& stream_id !== 0xF0 // ECM
&& stream_id !== 0xF1 // EMM
&& stream_id !== 0xFF // program_stream_directory
&& stream_id !== 0xF2 // DSMCC
&& stream_id !== 0xF8) {
(data[6] & 0x30) >>> 4;
let PTS_DTS_flags = (data[7] & 0xC0) >>> 6;
let PES_header_data_length = data[8];
let pts;
let dts;
if (PTS_DTS_flags === 0x02 || PTS_DTS_flags === 0x03) {
pts = (data[9] & 0x0E) * 536870912 +
// 1 << 29
(data[10] & 0xFF) * 4194304 +
// 1 << 22
(data[11] & 0xFE) * 16384 +
// 1 << 14
(data[12] & 0xFF) * 128 +
// 1 << 7
(data[13] & 0xFE) / 2;
if (PTS_DTS_flags === 0x03) {
dts = (data[14] & 0x0E) * 536870912 +
// 1 << 29
(data[15] & 0xFF) * 4194304 +
// 1 << 22
(data[16] & 0xFE) * 16384 +
// 1 << 14
(data[17] & 0xFF) * 128 +
// 1 << 7
(data[18] & 0xFE) / 2;
} else {
dts = pts;
}
}
let payload_start_index = 6 + 3 + PES_header_data_length;
let payload_length;
if (PES_packet_length !== 0) {
if (PES_packet_length < 3 + PES_header_data_length) {
this.player.debug.warn(this.TAG_NAME, `Malformed PES: PES_packet_length < 3 + PES_header_data_length`);
return;
}
payload_length = PES_packet_length - 3 - PES_header_data_length;
} else {
// PES_packet_length === 0
payload_length = data.byteLength - payload_start_index;
}
let payload = data.subarray(payload_start_index, payload_start_index + payload_length);
switch (pes_data.stream_type) {
case TS_STREAM_TYPE.kMPEG1Audio:
case TS_STREAM_TYPE.kMPEG2Audio:
this.parseMP3Payload(payload, pts);
break;
case TS_STREAM_TYPE.kPESPrivateData:
if (this.pmt_.common_pids.opus === pes_data.pid) {
this.parseOpusPayload(payload, pts);
} else if (this.pmt_.common_pids.ac3 === pes_data.pid) {
this.parseAC3Payload(payload, pts);
} else if (this.pmt_.common_pids.eac3 === pes_data.pid) {
this.parseEAC3Payload(payload, pts);
} else if (this.pmt_.asynchronous_klv_pids[pes_data.pid]) {
this.parseAsynchronousKLVMetadataPayload(payload, pes_data.pid, stream_id);
} else if (this.pmt_.smpte2038_pids[pes_data.pid]) {
this.parseSMPTE2038MetadataPayload(payload, pts, dts, pes_data.pid, stream_id);
} else {
this.parsePESPrivateDataPayload(payload, pts, dts, pes_data.pid, stream_id);
}
break;
case TS_STREAM_TYPE.kADTSAAC:
this.parseADTSAACPayload(payload, pts);
break;
case TS_STREAM_TYPE.kLOASAAC:
this.parseLOASAACPayload(payload, pts);
break;
case TS_STREAM_TYPE.kAC3:
this.parseAC3Payload(payload, pts);
break;
case TS_STREAM_TYPE.kEAC3:
this.parseEAC3Payload(payload, pts);
break;
case TS_STREAM_TYPE.kMetadata:
if (this.pmt_.timed_id3_pids[pes_data.pid]) {
this.parseTimedID3MetadataPayload(payload, pts, dts, pes_data.pid, stream_id);
} else if (this.pmt_.synchronous_klv_pids[pes_data.pid]) {
this.parseSynchronousKLVMetadataPayload(payload, pts, dts, pes_data.pid, stream_id);
}
break;
case TS_STREAM_TYPE.kH264:
this.parseH264Payload(payload, pts, dts, pes_data.random_access_indicator);
break;
case TS_STREAM_TYPE.kH265:
this.parseH265Payload(payload, pts, dts, pes_data.random_access_indicator);
break;
}
} else if (stream_id === 0xBC // program_stream_map
|| stream_id === 0xBF // private_stream_2
|| stream_id === 0xF0 // ECM
|| stream_id === 0xF1 // EMM
|| stream_id === 0xFF // program_stream_directory
|| stream_id === 0xF2 // DSMCC_stream
|| stream_id === 0xF8) {
// ITU-T Rec. H.222.1 type E stream
if (pes_data.stream_type === TS_STREAM_TYPE.kPESPrivateData) {
let payload_start_index = 6;
let payload_length;
if (PES_packet_length !== 0) {
payload_length = PES_packet_length;
} else {
// PES_packet_length === 0
payload_length = data.byteLength - payload_start_index;
}
let payload = data.subarray(payload_start_index, payload_start_index + payload_length);
this.parsePESPrivateDataPayload(payload, undefined, undefined, pes_data.pid, stream_id);
}
}
}
parsePAT(data) {
let table_id = data[0];
if (table_id !== 0x00) {
Log.e(this.TAG, `parsePAT: table_id ${table_id} is not corresponded to PAT!`);
return;
}
let section_length = (data[1] & 0x0F) << 8 | data[2];
data[3] << 8 | data[4];
let version_number = (data[5] & 0x3E) >>> 1;
let current_next_indicator = data[5] & 0x01;
let section_number = data[6];
data[7];
let pat = null;
if (current_next_indicator === 1 && section_number === 0) {
pat = {
version_number: 0,
network_pid: 0,
program_pmt_pid: {}
};
pat.version_number = version_number;
} else {
pat = this.pat_;
if (pat == undefined) {
return;
}
}
let program_start_index = 8;
let program_bytes = section_length - 5 - 4; // section_length - (headers + crc)
let first_program_number = -1;
let first_pmt_pid = -1;
for (let i = program_start_index; i < program_start_index + program_bytes; i += 4) {
let program_number = data[i] << 8 | data[i + 1];
let pid = (data[i + 2] & 0x1F) << 8 | data[i + 3];
if (program_number === 0) {
// network_PID
pat.network_pid = pid;
} else {
// program_map_PID
pat.program_pmt_pid[program_number] = pid;
if (first_program_number === -1) {
first_program_number = program_number;
}
if (first_pmt_pid === -1) {
first_pmt_pid = pid;
}
}
}
// Currently we only deal with first appeared PMT pid
if (current_next_indicator === 1 && section_number === 0) {
if (this.pat_ == undefined) {
this.player.debug.log(this.TAG_NAME, `Parsed first PAT: ${JSON.stringify(pat)}`);
}
this.pat_ = pat;
this.current_program_ = first_program_number;
this.current_pmt_pid_ = first_pmt_pid;
}
}
parsePMT(data) {
let table_id = data[0];
if (table_id !== 0x02) {
this.player.debug.error(this.TAG_NAME, `parsePMT: table_id ${table_id} is not corresponded to PMT!`);
return;
}
let section_length = (data[1] & 0x0F) << 8 | data[2];
let program_number = data[3] << 8 | data[4];
let version_number = (data[5] & 0x3E) >>> 1;
let current_next_indicator = data[5] & 0x01;
let section_number = data[6];
data[7];
let pmt;
if (current_next_indicator === 1 && section_number === 0) {
pmt = this._initPmt();
pmt.program_number = program_number;
pmt.version_number = version_number;
this.program_pmt_map_[program_number] = pmt;
} else {
pmt = this.program_pmt_map_[program_number];
if (pmt == undefined) {
return;
}
}
pmt.pcr_pid = (data[8] & 0x1F) << 8 | data[9];
let program_info_length = (data[10] & 0x0F) << 8 | data[11];
let info_start_index = 12 + program_info_length;
let info_bytes = section_length - 9 - program_info_length - 4;
for (let i = info_start_index; i < info_start_index + info_bytes;) {
let stream_type = data[i];
let elementary_PID = (data[i + 1] & 0x1F) << 8 | data[i + 2];
let ES_info_length = (data[i + 3] & 0x0F) << 8 | data[i + 4];
pmt.pid_stream_type[elementary_PID] = stream_type;
let already_has_video = pmt.common_pids.h264 || pmt.common_pids.h265;
let already_has_audio = pmt.common_pids.adts_aac || pmt.common_pids.loas_aac || pmt.common_pids.ac3 || pmt.common_pids.eac3 || pmt.common_pids.opus || pmt.common_pids.mp3;
if (stream_type === TS_STREAM_TYPE.kH264 && !already_has_video) {
pmt.common_pids.h264 = elementary_PID;
} else if (stream_type === TS_STREAM_TYPE.kH265 && !already_has_video) {
pmt.common_pids.h265 = elementary_PID;
} else if (stream_type === TS_STREAM_TYPE.kADTSAAC && !already_has_audio) {
pmt.common_pids.adts_aac = elementary_PID;
} else if (stream_type === TS_STREAM_TYPE.kLOASAAC && !already_has_audio) {
pmt.common_pids.loas_aac = elementary_PID;
} else if (stream_type === TS_STREAM_TYPE.kAC3 && !already_has_audio) {
pmt.common_pids.ac3 = elementary_PID; // ATSC AC-3
} else if (stream_type === TS_STREAM_TYPE.kEAC3 && !already_has_audio) {
pmt.common_pids.eac3 = elementary_PID; // ATSC EAC-3
} else if ((stream_type === TS_STREAM_TYPE.kMPEG1Audio || stream_type === TS_STREAM_TYPE.kMPEG2Audio) && !already_has_audio) {
pmt.common_pids.mp3 = elementary_PID;
} else if (stream_type === TS_STREAM_TYPE.kPESPrivateData) {
pmt.pes_private_data_pids[elementary_PID] = true;
if (ES_info_length > 0) {
// parse descriptor for PES private data
for (let offset = i + 5; offset < i + 5 + ES_info_length;) {
let tag = data[offset + 0];
let length = data[offset + 1];
if (tag === 0x05) {
// Registration Descriptor
let registration = String.fromCharCode(...Array.from(data.subarray(offset + 2, offset + 2 + length)));
if (registration === 'VANC') {
pmt.smpte2038_pids[elementary_PID] = true;
} /* else if (registration === 'AC-3' && !already_has_audio) {
pmt.common_pids.ac3 = elementary_PID; // DVB AC-3 (FIXME: NEED VERIFY)
} */ /* else if (registration === 'EC-3' && !alrady_has_audio) {
pmt.common_pids.eac3 = elementary_PID; // DVB EAC-3 (FIXME: NEED VERIFY)
} */else if (registration === 'Opus') {
pmt.common_pids.opus = elementary_PID;
} else if (registration === 'KLVA') {
pmt.asynchronous_klv_pids[elementary_PID] = true;
}
} else if (tag === 0x7F) {
// DVB extension descriptor
if (elementary_PID === pmt.common_pids.opus) {
let ext_desc_tag = data[offset + 2];
let channel_config_code = null;
if (ext_desc_tag === 0x80) {
// User defined (provisional Opus)
channel_config_code = data[offset + 3];
}
if (channel_config_code == null) {
Log.e(this.TAG, `Not Supported Opus channel count.`);
continue;
}
const meta = {
codec: 'opus',
channel_count: (channel_config_code & 0x0F) === 0 ? 2 : channel_config_code & 0x0F,
channel_config_code,
sample_rate: 48000
};
const sample = {
codec: 'opus',
meta
};
if (this.audio_init_segment_dispatched_ == false) {
this.audio_metadata_ = meta;
this.dispatchAudioInitSegment(sample);
} else if (this.detectAudioMetadataChange(sample)) {
// flush stashed frames before notify new AudioSpecificConfig
this.dispatchAudioMediaSegment();
// notify new AAC AudioSpecificConfig
this.dispatchAudioInitSegment(sample);
}
}
}
offset += 2 + length;
}
// provide descriptor for PES private data via callback
let descriptors = data.subarray(i + 5, i + 5 + ES_info_length);
this.dispatchPESPrivateDataDescriptor(elementary_PID, stream_type, descriptors);
}
} else if (stream_type === TS_STREAM_TYPE.kMetadata) {
if (ES_info_length > 0) {
// parse descriptor for PES private data
for (let offset = i + 5; offset < i + 5 + ES_info_length;) {
let tag = data[offset + 0];
let length = data[offset + 1];
if (tag === 0x26) {
let metadata_application_format = data[offset + 2] << 8 | data[offset + 3] << 0;
let metadata_application_format_identifier = null;
if (metadata_application_format === 0xFFFF) {
metadata_application_format_identifier = String.fromCharCode(...Array.from(data.subarray(offset + 4, offset + 4 + 4)));
}
let metadata_format = data[offset + 4 + (metadata_application_format === 0xFFFF ? 4 : 0)];
let metadata_format_identifier = null;
if (metadata_format === 0xFF) {
let pad = 4 + (metadata_application_format === 0xFFFF ? 4 : 0) + 1;
metadata_format_identifier = String.fromCharCode(...Array.from(data.subarray(offset + pad, offset + pad + 4)));
}
if (metadata_application_format_identifier === 'ID3 ' && metadata_format_identifier === 'ID3 ') {
pmt.timed_id3_pids[elementary_PID] = true;
} else if (metadata_format_identifier === 'KLVA') {
pmt.synchronous_klv_pids[elementary_PID] = true;
}
}
offset += 2 + length;
}
}
} else if (stream_type === TS_STREAM_TYPE.kSCTE35) {
pmt.scte_35_pids[elementary_PID] = true;
}
i += 5 + ES_info_length;
}
if (program_number === this.current_program_) {
if (this.pmt_ == undefined) {
this.player.debug.log(this.TAG_NAME, `Parsed first PMT: ${JSON.stringify(pmt)}`);
}
this.pmt_ = pmt;
if (pmt.common_pids.h264 || pmt.common_pids.h265) {
this.has_video_ = true;
}
if (pmt.common_pids.adts_aac || pmt.common_pids.loas_aac || pmt.common_pids.ac3 || pmt.common_pids.opus || pmt.common_pids.mp3) {
this.has_audio_ = true;
}
}
}
parseSCTE35(data) {
// todo: 待解决
}
parseH264Payload(data, pts, dts, random_access_indicator) {
let annexb_parser = new H264AnnexBParser(data);
let nalu_payload = null;
let payload = null;
let units = [];
let length = 0;
let keyframe = false;
while ((nalu_payload = annexb_parser.readNextNaluPayload()) != null) {
let nalu_avc1 = new H264NaluAVC1(nalu_payload);
if (nalu_avc1.type === H264_NAL_TYPE.kSliceSPS) {
// Notice: parseSPS requires Nalu without startcode or length-header
let details = SPSParser.parseSPS$2(nalu_payload.data);
if (!this.video_init_segment_dispatched_) {
this.video_metadata_.sps = nalu_avc1;
this.video_metadata_.details = details;
} else if (this.detectVideoMetadataChange(nalu_avc1, details) === true) {
this.player.debug.log(this.TAG_NAME, `H264: Critical h264 metadata has been changed, attempt to re-generate InitSegment`);
this.video_metadata_changed_ = true;
this.video_metadata_ = {
vps: undefined,
sps: nalu_avc1,
pps: undefined,
details: details
};
}
} else if (nalu_avc1.type === H264_NAL_TYPE.kSlicePPS) {
if (!this.video_init_segment_dispatched_ || this.video_metadata_changed_) {
this.video_metadata_.pps = nalu_avc1;
if (this.video_metadata_.sps && this.video_metadata_.pps) {
if (this.video_metadata_changed_) {
// flush stashed frames before changing codec metadata
this.dispatchVideoMediaSegment();
}
// notify new codec metadata (maybe changed)
this.dispatchVideoInitSegment();
}
}
} else if (nalu_avc1.type === H264_NAL_TYPE.kSliceIDR) {
keyframe = true;
} else if (nalu_avc1.type === H264_NAL_TYPE.kSliceNonIDR && random_access_indicator === 1) {
// For open-gop stream, use random_access_indicator to identify keyframe
keyframe = true;
}
// Push samples to remuxer only if initialization metadata has been dispatched
if (this.video_init_segment_dispatched_) {
units.push(nalu_avc1);
length += nalu_avc1.data.byteLength;
}
}
let pts_ms = Math.floor(pts / this.timescale_);
let dts_ms = Math.floor(dts / this.timescale_);
if (units.length) {
let track = this.video_track_;
// units 合并成一个 Uint8Array 赋值给 payload
for (let i = 0; i < units.length; i++) {
let unit = units[i];
if (payload == null) {
payload = unit.data;
} else {
let temp = new Uint8Array(payload.byteLength + unit.data.byteLength);
temp.set(payload, 0);
temp.set(unit.data, payload.byteLength);
payload = temp;
}
}
let avc_sample = {
// units,
length,
isIFrame: keyframe,
dts: dts_ms,
pts: pts_ms,
cts: pts_ms - dts_ms,
payload,
type: MEDIA_TYPE.video,
isHevc: false
};
track.samples.push(avc_sample);
// track.length += length;
track.length = payload.byteLength;
}
}
parseH265Payload(data, pts, dts, random_access_indicator) {
let annexb_parser = new H265AnnexBParser(data);
let nalu_payload = null;
let payload = null;
let units = [];
let length = 0;
let keyframe = false;
while ((nalu_payload = annexb_parser.readNextNaluPayload()) != null) {
let nalu_hvc1 = new H265NaluHVC1(nalu_payload);
if (nalu_hvc1.type === H265_NAL_TYPE.kSliceVPS) {
if (!this.video_init_segment_dispatched_) {
let details = H265NaluParser.parseVPS(nalu_payload.data);
this.video_metadata_.vps = nalu_hvc1;
this.video_metadata_.details = {
...this.video_metadata_.details,
...details
};
}
} else if (nalu_hvc1.type === H265_NAL_TYPE.kSliceSPS) {
let details = H265NaluParser.parseSPS(nalu_payload.data);
if (!this.video_init_segment_dispatched_) {
this.video_metadata_.sps = nalu_hvc1;
this.video_metadata_.details = {
...this.video_metadata_.details,
...details
};
} else if (this.detectVideoMetadataChange(nalu_hvc1, details) === true) {
this.player.debug.log(this.TAG_NAME, `H265: Critical h265 metadata has been changed, attempt to re-generate InitSegment`);
this.video_metadata_changed_ = true;
this.video_metadata_ = {
vps: undefined,
sps: nalu_hvc1,
pps: undefined,
details: details
};
}
} else if (nalu_hvc1.type === H265_NAL_TYPE.kSlicePPS) {
if (!this.video_init_segment_dispatched_ || this.video_metadata_changed_) {
let details = H265NaluParser.parsePPS(nalu_payload.data);
this.video_metadata_.pps = nalu_hvc1;
this.video_metadata_.details = {
...this.video_metadata_.details,
...details
};
if (this.video_metadata_.vps && this.video_metadata_.sps && this.video_metadata_.pps) {
if (this.video_metadata_changed_) {
// flush stashed frames before changing codec metadata
this.dispatchVideoMediaSegment();
}
// notify new codec metadata (maybe changed)
this.dispatchVideoInitSegment();
}
}
} else if (nalu_hvc1.type === H265_NAL_TYPE.kSliceIDR_W_RADL || nalu_hvc1.type === H265_NAL_TYPE.kSliceIDR_N_LP || nalu_hvc1.type === H265_NAL_TYPE.kSliceCRA_NUT) {
keyframe = true;
}
// Push samples to remuxer only if initialization metadata has been dispatched
if (this.video_init_segment_dispatched_) {
units.push(nalu_hvc1);
length += nalu_hvc1.data.byteLength;
}
}
let pts_ms = Math.floor(pts / this.timescale_);
let dts_ms = Math.floor(dts / this.timescale_);
if (units.length) {
let track = this.video_track_;
// units 合并成一个 Uint8Array 赋值给 payload
for (let i = 0; i < units.length; i++) {
let unit = units[i];
if (payload == null) {
payload = unit.data;
} else {
let temp = new Uint8Array(payload.byteLength + unit.data.byteLength);
temp.set(payload, 0);
temp.set(unit.data, payload.byteLength);
payload = temp;
}
}
let hvc_sample = {
type: MEDIA_TYPE.video,
// units,
length,
isIFrame: keyframe,
dts: dts_ms,
pts: pts_ms,
cts: pts_ms - dts_ms,
payload,
isHevc: true
};
track.samples.push(hvc_sample);
// track.length += length;
track.length = payload.byteLength;
}
}
detectVideoMetadataChange(new_sps, new_details) {
if (new_details.codec_mimetype !== this.video_metadata_.details.codec_mimetype) {
this.player.debug.log(this.TAG_NAME, `Video: Codec mimeType changed from ` + `${this.video_metadata_.details.codec_mimetype} to ${new_details.codec_mimetype}`);
return true;
}
if (new_details.codec_size.width !== this.video_metadata_.details.codec_size.width || new_details.codec_size.height !== this.video_metadata_.details.codec_size.height) {
let old_size = this.video_metadata_.details.codec_size;
let new_size = new_details.codec_size;
this.player.debug.log(this.TAG_NAME, `Video: Coded Resolution changed from ` + `${old_size.width}x${old_size.height} to ${new_size.width}x${new_size.height}`);
return true;
}
if (new_details.present_size.width !== this.video_metadata_.details.present_size.width) {
this.player.debug.log(this.TAG_NAME, `Video: Present resolution width changed from ` + `${this.video_metadata_.details.present_size.width} to ${new_details.present_size.width}`);
return true;
}
return false;
}
isInitSegmentDispatched() {
if (this.has_video_ && this.has_audio_) {
// both video & audio
return this.video_init_segment_dispatched_ && this.audio_init_segment_dispatched_;
}
if (this.has_video_ && !this.has_audio_) {
// video only
return this.video_init_segment_dispatched_;
}
if (!this.has_video_ && this.has_audio_) {
// audio only
return this.audio_init_segment_dispatched_;
}
return false;
}
dispatchVideoInitSegment() {
let details = this.video_metadata_.details;
let meta = {};
meta.type = 'video';
meta.id = this.video_track_.id;
meta.timescale = 1000;
meta.duration = this.duration_;
meta.codecWidth = details.codec_size.width;
meta.codecHeight = details.codec_size.height;
meta.presentWidth = details.present_size.width;
meta.presentHeight = details.present_size.height;
meta.profile = details.profile_string;
meta.level = details.level_string;
meta.bitDepth = details.bit_depth;
meta.chromaFormat = details.chroma_format;
meta.sarRatio = details.sar_ratio;
meta.frameRate = details.frame_rate;
let fps_den = meta.frameRate.fps_den;
let fps_num = meta.frameRate.fps_num;
meta.refSampleDuration = 1000 * (fps_den / fps_num);
meta.codec = details.codec_mimetype;
let isHevc = false;
// hevc
if (this.video_metadata_.vps) {
isHevc = true;
let vps_without_header = this.video_metadata_.vps.data.subarray(4);
let sps_without_header = this.video_metadata_.sps.data.subarray(4);
let pps_without_header = this.video_metadata_.pps.data.subarray(4);
meta.hvcc = hevcEncoderConfigurationRecord$2({
vps: vps_without_header,
sps: sps_without_header,
pps: pps_without_header
});
if (this.video_init_segment_dispatched_ == false) {
this.player.debug.log(this.TAG_NAME, `Generated first HEVCDecoderConfigurationRecord for mimeType: ${meta.codec}`);
}
if (meta.hvcc) {
this._doDecodeByTs(meta.hvcc, MEDIA_TYPE.video, 0, true, 0);
}
} else {
// avc
let sps_without_header = this.video_metadata_.sps.data.subarray(4);
let pps_without_header = this.video_metadata_.pps.data.subarray(4);
meta.avcc = avcEncoderConfigurationRecord$2({
sps: sps_without_header,
pps: pps_without_header
});
if (this.video_init_segment_dispatched_ == false) {
this.player.debug.log(this.TAG_NAME, `Generated first AVCDecoderConfigurationRecord for mimeType: ${meta.codec}`);
}
if (meta.avcc) {
this._doDecodeByTs(meta.avcc, MEDIA_TYPE.video, 0, true, 0);
}
}
// this.onTrackMetadata('video', meta);
this.video_init_segment_dispatched_ = true;
this.video_metadata_changed_ = false;
// notify new MediaInfo
let mi = this.media_info_;
mi.hasVideo = true;
mi.width = meta.codecWidth;
mi.height = meta.codecHeight;
mi.fps = meta.frameRate.fps;
mi.profile = meta.profile;
mi.level = meta.level;
mi.refFrames = details.ref_frames;
mi.chromaFormat = details.chroma_format_string;
mi.sarNum = meta.sarRatio.width;
mi.sarDen = meta.sarRatio.height;
mi.videoCodec = meta.codec;
if (mi.hasAudio && mi.audioCodec) {
mi.mimeType = `video/mp2t; codecs="${mi.videoCodec},${mi.audioCodec}"`;
} else {
mi.mimeType = `video/mp2t; codecs="${mi.videoCodec}"`;
}
this.player.video.updateVideoInfo({
width: meta.codecWidth,
height: meta.codecHeight,
encTypeCode: isHevc ? VIDEO_ENC_CODE.h265 : VIDEO_ENC_CODE.h264
});
// if (mi.isComplete()) {
// this.onMediaInfo(mi);
// }
// console.error('dispatchVideoInitSegment', mi, mi.isComplete());
}
dispatchVideoMediaSegment() {
if (this.isInitSegmentDispatched()) {
if (this.video_track_.length) {
this._preDoDecode();
}
}
}
dispatchAudioMediaSegment() {
if (this.isInitSegmentDispatched()) {
if (this.audio_track_.length) {
// this.onDataAvailable(this.audio_track_, null);
this._preDoDecode();
}
}
}
dispatchAudioVideoMediaSegment() {
if (this.isInitSegmentDispatched()) {
if (this.audio_track_.length || this.video_track_.length) {
this._preDoDecode();
}
}
}
parseADTSAACPayload(data, pts) {
if (this.has_video_ && !this.video_init_segment_dispatched_) {
// If first video IDR frame hasn't been detected,
// Wait for first IDR frame and video init segment being dispatched
return;
}
if (this.aac_last_incomplete_data_) {
let buf = new Uint8Array(data.byteLength + this.aac_last_incomplete_data_.byteLength);
buf.set(this.aac_last_incomplete_data_, 0);
buf.set(data, this.aac_last_incomplete_data_.byteLength);
data = buf;
}
let ref_sample_duration;
let base_pts_ms;
if (pts != undefined) {
base_pts_ms = pts / this.timescale_;
}
if (this.audio_metadata_.codec === 'aac') {
if (pts == undefined && this.audio_last_sample_pts_ != undefined) {
ref_sample_duration = 1024 / this.audio_metadata_.sampling_frequency * 1000;
base_pts_ms = this.audio_last_sample_pts_ + ref_sample_duration;
} else if (pts == undefined) {
this.player.debug.warn(this.TAG_NAME, `AAC: Unknown pts`);
return;
}
if (this.aac_last_incomplete_data_ && this.audio_last_sample_pts_) {
ref_sample_duration = 1024 / this.audio_metadata_.sampling_frequency * 1000;
let new_pts_ms = this.audio_last_sample_pts_ + ref_sample_duration;
if (Math.abs(new_pts_ms - base_pts_ms) > 1) {
this.player.debug.warn(this.TAG_NAME, `AAC: Detected pts overlapped, ` + `expected: ${new_pts_ms}ms, PES pts: ${base_pts_ms}ms`);
base_pts_ms = new_pts_ms;
}
}
}
let adts_parser = new AACADTSParser(data);
let aac_frame = null;
let sample_pts_ms = base_pts_ms;
let last_sample_pts_ms;
while ((aac_frame = adts_parser.readNextAACFrame()) != null) {
ref_sample_duration = 1024 / aac_frame.sampling_frequency * 1000;
const audio_sample = {
codec: 'aac',
data: aac_frame
};
if (this.audio_init_segment_dispatched_ == false) {
this.audio_metadata_ = {
codec: 'aac',
audio_object_type: aac_frame.audio_object_type,
sampling_freq_index: aac_frame.sampling_freq_index,
sampling_frequency: aac_frame.sampling_frequency,
channel_config: aac_frame.channel_config
};
this.dispatchAudioInitSegment(audio_sample);
} else if (this.detectAudioMetadataChange(audio_sample)) {
// flush stashed frames before notify new AudioSpecificConfig
this.dispatchAudioMediaSegment();
// notify new AAC AudioSpecificConfig
this.dispatchAudioInitSegment(audio_sample);
}
last_sample_pts_ms = sample_pts_ms;
let sample_pts_ms_int = Math.floor(sample_pts_ms);
const arrayBuffer = new Uint8Array(aac_frame.data.length + 2);
arrayBuffer.set([0xAF, 0x01], 0);
arrayBuffer.set(aac_frame.data, 2);
let aac_sample = {
// unit: aac_frame.data,
payload: arrayBuffer,
length: arrayBuffer.byteLength,
pts: sample_pts_ms_int,
dts: sample_pts_ms_int,
type: MEDIA_TYPE.audio
};
this.audio_track_.samples.push(aac_sample);
this.audio_track_.length += arrayBuffer.byteLength;
sample_pts_ms += ref_sample_duration;
}
if (adts_parser.hasIncompleteData()) {
this.aac_last_incomplete_data_ = adts_parser.getIncompleteData();
}
if (last_sample_pts_ms) {
this.audio_last_sample_pts_ = last_sample_pts_ms;
}
}
parseLOASAACPayload(data, pts) {
if (this.has_video_ && !this.video_init_segment_dispatched_) {
// If first video IDR frame hasn't been detected,
// Wait for first IDR frame and video init segment being dispatched
return;
}
if (this.aac_last_incomplete_data_) {
let buf = new Uint8Array(data.byteLength + this.aac_last_incomplete_data_.byteLength);
buf.set(this.aac_last_incomplete_data_, 0);
buf.set(data, this.aac_last_incomplete_data_.byteLength);
data = buf;
}
let ref_sample_duration;
let base_pts_ms;
if (pts != undefined) {
base_pts_ms = pts / this.timescale_;
}
if (this.audio_metadata_.codec === 'aac') {
if (pts == undefined && this.audio_last_sample_pts_ != undefined) {
ref_sample_duration = 1024 / this.audio_metadata_.sampling_frequency * 1000;
base_pts_ms = this.audio_last_sample_pts_ + ref_sample_duration;
} else if (pts == undefined) {
this.player.debug.warn(this.TAG_NAME, `AAC: Unknown pts`);
return;
}
if (this.aac_last_incomplete_data_ && this.audio_last_sample_pts_) {
ref_sample_duration = 1024 / this.audio_metadata_.sampling_frequency * 1000;
let new_pts_ms = this.audio_last_sample_pts_ + ref_sample_duration;
if (Math.abs(new_pts_ms - base_pts_ms) > 1) {
this.player.debug.warn(this.TAG, `AAC: Detected pts overlapped, ` + `expected: ${new_pts_ms}ms, PES pts: ${base_pts_ms}ms`);
base_pts_ms = new_pts_ms;
}
}
}
let loas_parser = new AACLOASParser(data);
let aac_frame = null;
let sample_pts_ms = base_pts_ms;
let last_sample_pts_ms;
while ((aac_frame = loas_parser.readNextAACFrame(this.loas_previous_frame ?? undefined)) != null) {
this.loas_previous_frame = aac_frame;
ref_sample_duration = 1024 / aac_frame.sampling_frequency * 1000;
const audio_sample = {
codec: 'aac',
data: aac_frame
};
if (this.audio_init_segment_dispatched_ == false) {
this.audio_metadata_ = {
codec: 'aac',
audio_object_type: aac_frame.audio_object_type,
sampling_freq_index: aac_frame.sampling_freq_index,
sampling_frequency: aac_frame.sampling_frequency,
channel_config: aac_frame.channel_config
};
this.dispatchAudioInitSegment(audio_sample);
} else if (this.detectAudioMetadataChange(audio_sample)) {
// flush stashed frames before notify new AudioSpecificConfig
this.dispatchAudioMediaSegment();
// notify new AAC AudioSpecificConfig
this.dispatchAudioInitSegment(audio_sample);
}
last_sample_pts_ms = sample_pts_ms;
let sample_pts_ms_int = Math.floor(sample_pts_ms);
const arrayBuffer = new Uint8Array(aac_frame.data.length + 2);
arrayBuffer.set([0xAF, 0x01], 0);
arrayBuffer.set(aac_frame.data, 2);
let aac_sample = {
// unit: aac_frame.data,
payload: arrayBuffer,
length: arrayBuffer.byteLength,
pts: sample_pts_ms_int,
dts: sample_pts_ms_int,
type: MEDIA_TYPE.audio
};
this.audio_track_.samples.push(aac_sample);
this.audio_track_.length += arrayBuffer.byteLength;
sample_pts_ms += ref_sample_duration;
}
if (loas_parser.hasIncompleteData()) {
this.aac_last_incomplete_data_ = loas_parser.getIncompleteData();
}
if (last_sample_pts_ms) {
this.audio_last_sample_pts_ = last_sample_pts_ms;
}
}
parseAC3Payload(data, pts) {
// todo: 待开发
}
parseEAC3Payload(data, pts) {
// todo: 待开发
}
parseOpusPayload(data, pts) {
// todo: 待开发
}
parseMP3Payload(data, pts) {
if (this.has_video_ && !this.video_init_segment_dispatched_) {
// If first video IDR frame hasn't been detected,
// Wait for first IDR frame and video init segment being dispatched
return;
}
let _mpegAudioV10SampleRateTable = [44100, 48000, 32000, 0];
let _mpegAudioV20SampleRateTable = [22050, 24000, 16000, 0];
let _mpegAudioV25SampleRateTable = [11025, 12000, 8000, 0];
let ver = data[1] >>> 3 & 0x03;
let layer = (data[1] & 0x06) >> 1;
(data[2] & 0xF0) >>> 4;
let sampling_freq_index = (data[2] & 0x0C) >>> 2;
let channel_mode = data[3] >>> 6 & 0x03;
let channel_count = channel_mode !== 3 ? 2 : 1;
let sample_rate = 0;
let object_type = 34; // Layer-3, listed in MPEG-4 Audio Object Types
switch (ver) {
case 0:
// MPEG 2.5
sample_rate = _mpegAudioV25SampleRateTable[sampling_freq_index];
break;
case 2:
// MPEG 2
sample_rate = _mpegAudioV20SampleRateTable[sampling_freq_index];
break;
case 3:
// MPEG 1
sample_rate = _mpegAudioV10SampleRateTable[sampling_freq_index];
break;
}
switch (layer) {
case 1:
// Layer 3
object_type = 34;
break;
case 2:
// Layer 2
object_type = 33;
break;
case 3:
// Layer 1
object_type = 32;
break;
}
const sample = {};
sample.object_type = object_type;
sample.sample_rate = sample_rate;
sample.channel_count = channel_count;
sample.data = data;
const audio_sample = {
codec: 'mp3',
data: sample
};
if (this.audio_init_segment_dispatched_ == false) {
this.audio_metadata_ = {
codec: 'mp3',
object_type,
sample_rate,
channel_count
};
this.dispatchAudioInitSegment(audio_sample);
} else if (this.detectAudioMetadataChange(audio_sample)) {
// flush stashed frames before notify new AudioSpecificConfig
this.dispatchAudioMediaSegment();
// notify new AAC AudioSpecificConfig
this.dispatchAudioInitSegment(audio_sample);
}
let mp3_sample = {
// unit: data,
payload: data,
length: data.byteLength,
pts: pts / this.timescale_,
dts: pts / this.timescale_,
type: MEDIA_TYPE.audio
};
this.audio_track_.samples.push(mp3_sample);
this.audio_track_.length += data.byteLength;
}
detectAudioMetadataChange(sample) {
if (sample.codec !== this.audio_metadata_.codec) {
this.player.debug.log(this.TAG_NAME, `Audio: Audio Codecs changed from ` + `${this.audio_metadata_.codec} to ${sample.codec}`);
return true;
}
if (sample.codec === 'aac' && this.audio_metadata_.codec === 'aac') {
const frame = sample.data;
if (frame.audio_object_type !== this.audio_metadata_.audio_object_type) {
this.player.debug.log(this.TAG_NAME, `AAC: AudioObjectType changed from ` + `${this.audio_metadata_.audio_object_type} to ${frame.audio_object_type}`);
return true;
}
if (frame.sampling_freq_index !== this.audio_metadata_.sampling_freq_index) {
this.player.debug.log(this.TAG_NAME, `AAC: SamplingFrequencyIndex changed from ` + `${this.audio_metadata_.sampling_freq_index} to ${frame.sampling_freq_index}`);
return true;
}
if (frame.channel_config !== this.audio_metadata_.channel_config) {
this.player.debug.log(this.TAG_NAME, `AAC: Channel configuration changed from ` + `${this.audio_metadata_.channel_config} to ${frame.channel_config}`);
return true;
}
} else if (sample.codec === 'ac-3' && this.audio_metadata_.codec === 'ac-3') {
const frame = sample.data;
if (frame.sampling_frequency !== this.audio_metadata_.sampling_frequency) {
this.player.debug.log(this.TAG_NAME, `AC3: Sampling Frequency changed from ` + `${this.audio_metadata_.sampling_frequency} to ${frame.sampling_frequency}`);
return true;
}
if (frame.bit_stream_identification !== this.audio_metadata_.bit_stream_identification) {
this.player.debug.log(this.TAG_NAME, `AC3: Bit Stream Identification changed from ` + `${this.audio_metadata_.bit_stream_identification} to ${frame.bit_stream_identification}`);
return true;
}
if (frame.bit_stream_mode !== this.audio_metadata_.bit_stream_mode) {
this.player.debug.log(this.TAG_NAME, `AC3: BitStream Mode changed from ` + `${this.audio_metadata_.bit_stream_mode} to ${frame.bit_stream_mode}`);
return true;
}
if (frame.channel_mode !== this.audio_metadata_.channel_mode) {
this.player.debug.log(this.TAG_NAME, `AC3: Channel Mode changed from ` + `${this.audio_metadata_.channel_mode} to ${frame.channel_mode}`);
return true;
}
if (frame.low_frequency_effects_channel_on !== this.audio_metadata_.low_frequency_effects_channel_on) {
this.player.debug.log(this.TAG_NAME, `AC3: Low Frequency Effects Channel On changed from ` + `${this.audio_metadata_.low_frequency_effects_channel_on} to ${frame.low_frequency_effects_channel_on}`);
return true;
}
} else if (sample.codec === 'opus' && this.audio_metadata_.codec === 'opus') {
const data = sample.meta;
if (data.sample_rate !== this.audio_metadata_.sample_rate) {
this.player.debug.log(this.TAG_NAME, `Opus: SamplingFrequencyIndex changed from ` + `${this.audio_metadata_.sample_rate} to ${data.sample_rate}`);
return true;
}
if (data.channel_count !== this.audio_metadata_.channel_count) {
this.player.debug.log(this.TAG_NAME, `Opus: Channel count changed from ` + `${this.audio_metadata_.channel_count} to ${data.channel_count}`);
return true;
}
} else if (sample.codec === 'mp3' && this.audio_metadata_.codec === 'mp3') {
const data = sample.data;
if (data.object_type !== this.audio_metadata_.object_type) {
this.player.debug.log(this.TAG_NAME, `MP3: AudioObjectType changed from ` + `${this.audio_metadata_.object_type} to ${data.object_type}`);
return true;
}
if (data.sample_rate !== this.audio_metadata_.sample_rate) {
this.player.debug.log(this.TAG_NAME, `MP3: SamplingFrequencyIndex changed from ` + `${this.audio_metadata_.sample_rate} to ${data.sample_rate}`);
return true;
}
if (data.channel_count !== this.audio_metadata_.channel_count) {
this.player.debug.log(this.TAG_NAME, `MP3: Channel count changed from ` + `${this.audio_metadata_.channel_count} to ${data.channel_count}`);
return true;
}
}
return false;
}
dispatchAudioInitSegment(sample) {
let meta = {};
meta.type = 'audio';
meta.id = this.audio_track_.id;
meta.timescale = 1000;
meta.duration = this.duration_;
let encTypeCode = '';
if (this.audio_metadata_.codec === 'aac') {
let aac_frame = sample.codec === 'aac' ? sample.data : null;
let audio_specific_config = new AudioSpecificConfig(aac_frame);
meta.audioSampleRate = audio_specific_config.sampling_rate;
meta.audioSampleRateIndex = audio_specific_config.sampling_index;
meta.channelCount = audio_specific_config.channel_count;
meta.codec = audio_specific_config.codec_mimetype;
meta.originalCodec = audio_specific_config.original_codec_mimetype;
meta.config = audio_specific_config.config;
meta.refSampleDuration = 1024 / meta.audioSampleRate * meta.timescale;
encTypeCode = AUDIO_ENC_CODE.AAC;
const aacADTSHeader = aacEncoderConfigurationRecordV2({
profile: this.player._opt.mseDecodeAudio ? audio_specific_config.object_type : audio_specific_config.original_object_type,
sampleRate: meta.audioSampleRateIndex,
channel: meta.channelCount
});
console.error('aacADTSHeader', `profile: ${audio_specific_config.object_type}, sampleRate: ${meta.audioSampleRateIndex}, channel: ${meta.channelCount}`);
this._doDecodeByTs(aacADTSHeader, MEDIA_TYPE.audio, 0, false, 0);
} else if (this.audio_metadata_.codec === 'ac-3') ; else if (this.audio_metadata_.codec === 'ec-3') ; else if (this.audio_metadata_.codec === 'opus') ; else if (this.audio_metadata_.codec === 'mp3') {
meta.audioSampleRate = this.audio_metadata_.sample_rate;
meta.channelCount = this.audio_metadata_.channel_count;
meta.codec = 'mp3';
meta.originalCodec = 'mp3';
meta.config = undefined;
encTypeCode = AUDIO_ENC_CODE.MP3;
}
if (this.audio_init_segment_dispatched_ == false) {
this.player.debug.log(this.TAG_NAME, `Generated first AudioSpecificConfig for mimeType: ${meta.codec}`);
}
// todo:待开发
// this.onTrackMetadata('audio', meta);
// console.error('audio', meta);
this.audio_init_segment_dispatched_ = true;
this.video_metadata_changed_ = false;
// notify new MediaInfo
let mi = this.media_info_;
mi.hasAudio = true;
mi.audioCodec = meta.originalCodec;
mi.audioSampleRate = meta.audioSampleRate;
mi.audioChannelCount = meta.channelCount;
if (mi.hasVideo && mi.videoCodec) {
mi.mimeType = `video/mp2t; codecs="${mi.videoCodec},${mi.audioCodec}"`;
} else {
mi.mimeType = `video/mp2t; codecs="${mi.audioCodec}"`;
}
if (encTypeCode) {
this.player.audio.updateAudioInfo({
encTypeCode,
channels: meta.channelCount,
sampleRate: meta.audioSampleRate
});
}
// todo 待完成
// console.error('todo:dispatchAudioInitSegment', mi, mi.isComplete());
}
dispatchPESPrivateDataDescriptor(pid, stream_type, descriptor) {
// todo: 待解决
}
parsePESPrivateDataPayload(data, pts, dts, pid, stream_id) {
let private_data = new PESPrivateData();
private_data.pid = pid;
private_data.stream_id = stream_id;
private_data.len = data.byteLength;
private_data.data = data;
if (pts != undefined) {
let pts_ms = Math.floor(pts / this.timescale_);
private_data.pts = pts_ms;
} else {
private_data.nearest_pts = this.getNearestTimestampMilliseconds();
}
if (dts != undefined) {
let dts_ms = Math.floor(dts / this.timescale_);
private_data.dts = dts_ms;
}
// todo:待解决
// console.error('todo :parsePESPrivateDataPayload', private_data);
}
parseTimedID3MetadataPayload(data, pts, dts, pid, stream_id) {
// todo 待开发
this.player.debug.log(this.TAG_NAME, `Timed ID3 Metadata: pid=${pid}, pts=${pts}, dts=${dts}, stream_id=${stream_id}`);
}
parseSynchronousKLVMetadataPayload(data, pts, dts, pid, stream_id) {
this.player.debug.log(this.TAG_NAME, `Synchronous KLV Metadata: pid=${pid}, pts=${pts}, dts=${dts}, stream_id=${stream_id}`);
}
parseAsynchronousKLVMetadataPayload(data, pid, stream_id) {
// todo: 待开发
this.player.debug.log(this.TAG_NAME, `Asynchronous KLV Metadata: pid=${pid}, stream_id=${stream_id}`);
}
parseSMPTE2038MetadataPayload(data, pts, dts, pid, stream_id) {
// todo: 待开发
this.player.debug.log(this.TAG_NAME, `SMPTE 2038 Metadata: pid=${pid}, pts=${pts}, dts=${dts}, stream_id=${stream_id}`);
}
getNearestTimestampMilliseconds() {
// Prefer using last audio sample pts if audio track exists
if (this.audio_last_sample_pts_ != undefined) {
return Math.floor(this.audio_last_sample_pts_);
} else if (this.last_pcr_ != undefined) {
// Fallback to PCR time if audio track doesn't exist
const pcr_time_ms = Math.floor(this.last_pcr_ / 300 / this.timescale_);
return pcr_time_ms;
}
return undefined;
}
//
_preDoDecode() {
const videoTrack = this.video_track_;
const audioTrack = this.audio_track_;
let allSampleList = videoTrack.samples;
if (audioTrack.samples.length > 0) {
allSampleList = videoTrack.samples.concat(audioTrack.samples);
allSampleList = allSampleList.sort((a, b) => {
return a.dts - b.dts;
});
}
allSampleList.forEach(sample => {
const arrayBuffer = new Uint8Array(sample.payload);
delete sample.payload;
if (sample.type === MEDIA_TYPE.video) {
this._doDecodeVideo({
...sample,
payload: arrayBuffer
});
} else if (sample.type === MEDIA_TYPE.audio) {
this._doDecodeAudio({
...sample,
payload: arrayBuffer
});
}
});
// clear
videoTrack.samples = [];
videoTrack.length = 0;
audioTrack.samples = [];
audioTrack.length = 0;
}
_doDecodeVideo(sample) {
const uint8Array = new Uint8Array(sample.payload);
let packet = null;
if (sample.isHevc) {
// add 5 header
packet = hevcEncoderNalePacketNotLength(uint8Array, sample.isIFrame);
} else {
packet = avcEncoderNalePacketNotLength(uint8Array, sample.isIFrame);
}
this.player.updateStats({
dts: sample.dts,
vbps: packet.byteLength
});
if (sample.isIFrame) {
this.calcIframeIntervalTimestamp(sample.dts);
}
let payloadBuffer = this.cryptoPayload(packet, sample.isIFrame);
this._doDecodeByTs(payloadBuffer, MEDIA_TYPE.video, sample.dts, sample.isIFrame, sample.cts);
}
_doDecodeAudio(sample) {
const uint8Array = new Uint8Array(sample.payload);
this.player.updateStats({
abps: uint8Array.byteLength
});
let payloadBuffer = uint8Array;
if (isTrue(this.player._opt.m7sCryptoAudio)) {
payloadBuffer = this.cryptoPayloadAudio(uint8Array);
}
this._doDecodeByTs(payloadBuffer, MEDIA_TYPE.audio, sample.dts, false, 0);
}
}
class Demux {
constructor(player) {
const Loader = Demux.getLoaderFactory(player);
return new Loader(player);
}
static getLoaderFactory(player) {
const type = player._opt.demuxType;
if (type === DEMUX_TYPE.m7s) {
return M7sLoader;
} else if (type === DEMUX_TYPE.flv || player.isWebrtcH265()) {
return FlvLoader;
} else if (type === DEMUX_TYPE.webTransport) {
return WebTransportLoader;
} else if (type === DEMUX_TYPE.nakedFlow) {
return NakedFlowLoader;
} else if (type === DEMUX_TYPE.fmp4) {
return Fmp4Loader;
} else if (type === DEMUX_TYPE.mpeg4) {
return Mpeg4Loader;
} else if (type === DEMUX_TYPE.ts) {
return TsLoaderV2;
// return TsLoader;
} else {
return EmptyLoader;
}
}
}
class WebcodecsDecoder extends Emitter {
constructor(player) {
super();
this.player = player;
this.TAG_NAME = 'Webcodecs';
this.hasInit = false;
this.isDecodeFirstIIframe = isFalse(player._opt.checkFirstIFrame) ? true : false;
this.isInitInfo = false;
this.prevTimestamp = null;
this.decodeDiffTimestamp = null;
this.prevDts = null;
this.decoder = null;
this.isWidthOrHeightChanged = false;
this.initDecoder();
player.debug.log(this.TAG_NAME, 'init');
}
destroy() {
if (this.decoder) {
if (isFalse(this.isDecodeStateClosed())) {
this.decoder.close();
}
this.decoder = null;
}
this.prevTimestamp = null;
this.decodeDiffTimestamp = null;
this.prevDts = null;
this.hasInit = false;
this.isInitInfo = false;
this.isDecodeFirstIIframe = false;
this.isWidthOrHeightChanged = false;
this.off();
this.player.debug.log(this.TAG_NAME, 'destroy');
}
initDecoder() {
const _this = this;
this.decoder = new VideoDecoder({
output(videoFrame) {
_this.handleDecode(videoFrame);
},
error(error) {
_this.handleError(error);
}
});
}
handleDecode(videoFrame) {
if (!this.isInitInfo) {
this.player.video.updateVideoInfo({
width: videoFrame.codedWidth,
height: videoFrame.codedHeight
});
this.player.video.initCanvasViewSize();
this.isInitInfo = true;
}
// console.log(this.TAG_NAME, 'handleDecode duration and timestamp', videoFrame.duration, videoFrame.timestamp)
// player
if (this.player.isPlayer()) {
this.player.updateStats({
dfps: true
});
if (!this.player._times.videoStart) {
this.player._times.videoStart = now$2();
this.player.handlePlayToRenderTimes();
}
this.player.video.render({
videoFrame,
ts: videoFrame.timestamp
});
this.player.handleRender();
} else if (this.player.isPlayback()) {
// playback
this.player.updateStats({
dfps: true
});
if (isFalse(this.player.playbackPause)) {
// is not pause
if (this.player.playback.isUseLocalCalculateTime) {
this.player.playback.increaseLocalTimestamp();
}
if (this.player.playback.isUseFpsRender) {
this.player.video.pushData({
videoFrame,
ts: videoFrame.timestamp
});
} else {
this.player.video.render$2({
videoFrame,
ts: videoFrame.timestamp
});
}
} else {
// 暂停的时候,如果不需要清除缓存
if (isFalse(this.player.playback.isPlaybackPauseClearCache) && this.player.playback.isCacheBeforeDecodeForFpsRender) {
if (this.player.playback.isUseFpsRender) {
this.player.video.pushData({
videoFrame,
ts: videoFrame.timestamp
});
}
}
}
}
}
handleError(error) {
this.player.debug.error(this.TAG_NAME, 'VideoDecoder handleError:', error.code, error);
const errorString = error.toString();
if (errorString.indexOf(WCS_ERROR.unsupportedConfiguration) !== -1) {
this.player.emitError(EVENTS_ERROR.webcodecsUnsupportedConfigurationError, errorString);
} else if (errorString.indexOf(WCS_ERROR.decoderFailure) !== -1) {
this.player.emitError(EVENTS_ERROR.webcodecsDecodeError, errorString);
} else if (errorString.indexOf(WCS_ERROR.decodingError) !== -1) {
this.player.emitError(EVENTS_ERROR.webcodecsDecodeError, errorString);
} else if (errorString.indexOf(WCS_ERROR.decoderError) !== -1) {
this.player.emitError(EVENTS_ERROR.webcodecsDecodeError, errorString);
} else if (errorString.indexOf(WCS_ERROR.hevcDecodingIsNotSupported) !== -1) {
this.player.emitError(EVENTS_ERROR.webcodecsH265NotSupport);
}
}
decodeVideo(payload, ts, isIframe, cts) {
// this.player.debug.log('Webcodecs decoder', 'decodeVideo', ts, isIframe);
const player = this.player;
if (!player) {
return;
}
if (this.player.isDestroyedOrClosed()) {
this.player.debug.warn(this.TAG_NAME, 'decodeVideo() player is destroyed');
return;
}
if (!this.hasInit) {
if (isIframe && payload[1] === AVC_PACKET_TYPE.sequenceHeader) {
const videoCodec = payload[0] & 0x0F;
this.player.video.updateVideoInfo({
encTypeCode: videoCodec
});
// 如果解码出来的是
if (videoCodec === VIDEO_ENC_CODE.h265 && !supportWCSDecodeHevc()) {
const browserInfo = getBrowser();
this.player.debug.warn(this.TAG_NAME, 'WebcodecsDecoder not support hevc decode', browserInfo.type, browserInfo.version);
this.player.emitError(EVENTS_ERROR.webcodecsH265NotSupport);
return;
}
if (!this.player._times.decodeStart) {
this.player._times.decodeStart = now$2();
}
let config = null;
let videInfo = null;
const extraData = payload.slice(5);
if (videoCodec === VIDEO_ENC_CODE.h264) {
// config = formatAvcVideoDecoderConfigure(extraData);
videInfo = parseAVCDecoderConfigurationRecord(extraData);
config = {
codec: videInfo.codec,
description: extraData
};
} else if (videoCodec === VIDEO_ENC_CODE.h265) {
// config = formatHevcVideoDecoderConfigure(extraData);
videInfo = parseHEVCDecoderConfigurationRecord$4(extraData);
config = {
codec: videInfo.codec,
description: extraData
};
}
if (videInfo && videInfo.codecWidth && videInfo.codecHeight) {
config.codedHeight = videInfo.codecHeight;
config.codedWidth = videInfo.codecWidth;
}
if (this.player.recorder && this.player._opt.recordType === FILE_SUFFIX.mp4) {
this.player.recorder.initMetaData(payload, videoCodec);
}
if (!config || config && !config.codec) {
this.player.debug.warn(this.TAG_NAME, 'decodeVideo and webcodecs configure is', JSON.stringify(config));
this.player.emitError(EVENTS_ERROR.webcodecsDecodeConfigureError);
return;
}
// console.log('webcodecs decodeVideo config is ', config);
this.player.debug.log(this.TAG_NAME, `decoder.configure() and codec is ${config.codec}`);
try {
this.decoder.configure(config);
this.hasInit = true;
} catch (e) {
this.player.debug.log(this.TAG_NAME, 'configure error', e.code, e);
const errorString = e.toString();
if (errorString.indexOf(WCS_ERROR.hevcDecodingIsNotSupported) !== -1) {
this.player.emitError(EVENTS_ERROR.webcodecsH265NotSupport);
} else {
this.player.emitError(EVENTS_ERROR.webcodecsDecodeConfigureError);
}
}
}
} else {
// fix : Uncaught DOMException: Failed to execute 'decode' on 'VideoDecoder': A key frame is required after configure() or flush().
if (!this.isDecodeFirstIIframe && isIframe) {
this.isDecodeFirstIIframe = true;
}
if (this.isDecodeFirstIIframe) {
if (this.isDecodeStateClosed()) {
this.player.debug.warn(this.TAG_NAME, 'VideoDecoder isDecodeStateClosed true');
return;
}
if (isIframe && payload[1] === 0) {
const videoCodec = payload[0] & 0x0F;
let config = {};
if (videoCodec === VIDEO_ENC_CODE.h264) {
let data = payload.slice(5);
config = parseAVCDecoderConfigurationRecord(data);
} else if (videoCodec === VIDEO_ENC_CODE.h265) {
config = parseHEVCDecoderConfigurationRecord$2(payload);
}
const videoInfo = this.player.video.videoInfo;
if (videoInfo && videoInfo.width && videoInfo.height && config && config.codecWidth && config.codecHeight && (config.codecWidth !== videoInfo.width || config.codecHeight !== videoInfo.height)) {
this.player.debug.warn(this.TAG_NAME, `decodeVideo: video width or height is changed,
old width is ${videoInfo.width}, old height is ${videoInfo.height},
new width is ${config.codecWidth}, new height is ${config.codecHeight},
and emit change event`);
this.isWidthOrHeightChanged = true;
this.player.emitError(EVENTS_ERROR.wcsWidthOrHeightChange);
}
}
if (this.isWidthOrHeightChanged) {
this.player.debug.warn(this.TAG_NAME, `decodeVideo: video width or height is changed, and return`);
return;
}
if (isVideoSequenceHeader(payload)) {
this.player.debug.warn(this.TAG_NAME, 'decodeVideo and payload is video sequence header so drop this frame');
return;
}
if (payload.byteLength < VIDEO_PAYLOAD_MIN_SIZE) {
this.player.debug.warn(this.TAG_NAME, `decodeVideo and payload is too small , payload length is ${payload.byteLength}`);
return;
}
let isFirst = false;
let nowTime = new Date().getTime();
if (!this.prevTimestamp) {
this.prevTimestamp = nowTime;
isFirst = true;
}
const diffTime = nowTime - this.prevTimestamp;
this.decodeDiffTimestamp = diffTime;
if (diffTime > 500 && !isFirst && this.player.isPlayer()) {
this.player.debug.warn(this.TAG_NAME, 'decodeVideo diff time is ', diffTime);
}
const buffer = payload.slice(5);
const chunk = new EncodedVideoChunk({
data: buffer,
timestamp: ts,
type: isIframe ? ENCODED_VIDEO_TYPE.key : ENCODED_VIDEO_TYPE.delta
});
this.player.emit(EVENTS.timeUpdate, ts);
// if (this.player.recorder &&
// this.player.recorder.isRecording &&
// this.player._opt.recordType === FILE_SUFFIX.mp4) {
// this.player.recorder.handleAddNaluTrack(buffer, isIframe, ts, cts);
// }
try {
this.decoder.decode(chunk);
} catch (e) {
this.player.debug.error(this.TAG_NAME, 'VideoDecoder', e);
const errorString = e.toString();
if (errorString.indexOf(WCS_ERROR.keyframeIsRequiredError) !== -1) {
this.player.emitError(EVENTS_ERROR.webcodecsDecodeError);
} else if (errorString.indexOf(WCS_ERROR.canNotDecodeClosedCodec) !== -1) {
this.player.emitError(EVENTS_ERROR.webcodecsDecodeError);
}
}
this.prevTimestamp = new Date().getTime();
} else {
this.player.debug.log(this.TAG_NAME, 'VideoDecoder first frame is not iFrame');
}
}
}
getDecodeDiffTimes() {
return this.decodeDiffTimestamp;
}
isDecodeStateClosed() {
return this.decoder.state === 'closed';
}
isDecodeStateConfigured() {
return this.decoder.state === 'configured';
}
isDecodeStateUnConfigured() {
return this.decoder.state === 'unconfigured';
}
}
const iconsMap = {
play: '播放',
pause: '暂停',
audio: '',
mute: '',
screenshot: '截图',
loading: '',
fullscreen: '全屏',
fullscreenExit: '退出全屏',
record: '录制',
recordStop: '停止录制',
narrow: '缩小',
expand: '放大',
ptz: '操作盘',
ptzActive: '操作盘激活',
zoom: '电子放大',
zoomStop: '关闭电子放大',
close: '关闭',
performance: '性能面板',
performanceActive: '性能面板激活',
face: '人脸识别',
faceActive: '人脸识别激活',
object: '物品识别',
objectActive: '物品识别激活',
occlusion: '遮挡物检查',
occlusionActive: '遮挡物检查激活',
logSave: '保存日志'
};
var icons = Object.keys(iconsMap).reduce((icons, key) => {
icons[key] = `
${iconsMap[key] ? `${iconsMap[key]}` : ''}
`;
return icons;
}, {});
var template = ((player, control) => {
if (player._opt.hasControl && player._opt.controlAutoHide) {
player.$container.classList.add('jb-pro-controls-show-auto-hide');
} else {
player.$container.classList.add('jb-pro-controls-show');
}
const options = player._opt;
const operateBtns = options.operateBtns;
const playbackOperateDom = `
${options.playbackConfig.showPrecisionBtn ? `
${icons.narrow}
${icons.expand}
` : ''}
`;
const playbackOperateSimpleDom = `
`;
player.$container.insertAdjacentHTML('beforeend', `
${options.background ? `` : ''}
${options.loadingIcon ? `
${icons.loading}
${options.loadingText}
` : ''}
${options.hasControl && operateBtns.play ? `` : ''}
${options.hasControl && operateBtns.ptz ? `
${options.ptzMoreArrowShow ? `
` : ''}
${options.ptzZoomShow ? `
` : ''}
${options.ptzApertureShow ? `
` : ''}
${options.ptzFocusShow ? `
` : ''}
${options.ptzCruiseShow ? `
` : ''}
${options.ptzFogShow ? `
` : ''}
${options.ptzWiperShow ? `
` : ''}
` : ''}
${options.hasVideo ? `
${icons.narrow}
电子放大
${icons.expand}
${icons.zoomStop}
00:00:00
${icons.recordStop}
` : ''}
${options.hasControl ? `
${options.showBandwidth ? `
` : ''}
${options.controlHtml}
${options.playType === PLAY_TYPE.playbackTF && options.playbackConfig.showControl && options.playbackConfig.controlType === PLAYBACK_CONTROL_TYPE.normal ? playbackOperateDom : ''}
${options.playType === PLAY_TYPE.playbackTF && options.playbackConfig.showControl && options.playbackConfig.controlType === PLAYBACK_CONTROL_TYPE.simple ? playbackOperateSimpleDom : ''}
${options.playType === PLAY_TYPE.playbackTF && options.playbackConfig.showRateBtn ? `
` : ''}
${operateBtns.close ? `
` : ''}
${operateBtns.logSave ? `
` : ''}
${operateBtns.performance ? `
${icons.performance}
${icons.performanceActive}
` : ''}
${operateBtns.aiFace ? `
${icons.face}
${icons.faceActive}
` : ''}
${operateBtns.aiObject ? `
${icons.object}
${icons.objectActive}
` : ''}
${operateBtns.aiOcclusion ? `
${icons.occlusion}
${icons.occlusionActive}
` : ''}
${operateBtns.quality ? `
` : ''}
${operateBtns.scale ? `
` : ''}
${operateBtns.audio ? `
${icons.audio}
${icons.mute}
` : ''}
${operateBtns.play ? `
${icons.play}
${icons.pause}
` : ''}
${operateBtns.screenshot ? `
` : ''}
${operateBtns.record ? `
${icons.record}
${icons.recordStop}
` : ''}
${operateBtns.ptz ? `
${icons.ptz}
${icons.ptzActive}
` : ''}
${operateBtns.zoom ? `
${icons.zoom}
${icons.zoomStop}
` : ''}
${operateBtns.fullscreen ? `
${icons.fullscreen}
${icons.fullscreenExit}
` : ''}
` : ''}
`);
Object.defineProperty(control, '$poster', {
value: player.$container.querySelector('.jb-pro-poster')
});
Object.defineProperty(control, '$loadingBg', {
value: player.$container.querySelector('.jb-pro-loading-bg')
});
Object.defineProperty(control, '$loadingBgImage', {
value: player.$container.querySelector('.jb-pro-loading-bg-image')
});
Object.defineProperty(control, '$loading', {
value: player.$container.querySelector('.jb-pro-loading')
});
Object.defineProperty(control, '$loadingText', {
value: player.$container.querySelector('.jb-pro-loading-text')
});
Object.defineProperty(control, '$play', {
value: player.$container.querySelector('.jb-pro-play')
});
Object.defineProperty(control, '$playBig', {
value: player.$container.querySelector('.jb-pro-play-big')
});
Object.defineProperty(control, '$recording', {
value: player.$container.querySelector('.jb-pro-recording')
});
Object.defineProperty(control, '$recordingTime', {
value: player.$container.querySelector('.jb-pro-recording-time')
});
Object.defineProperty(control, '$recordingStop', {
value: player.$container.querySelector('.jb-pro-recording-stop')
});
Object.defineProperty(control, '$pause', {
value: player.$container.querySelector('.jb-pro-pause')
});
Object.defineProperty(control, '$controls', {
value: player.$container.querySelector('.jb-pro-controls')
});
Object.defineProperty(control, '$controlsInner', {
value: player.$container.querySelector('.jb-pro-controls-bottom')
});
Object.defineProperty(control, '$controlsLeft', {
value: player.$container.querySelector('.jb-pro-controls-left')
});
Object.defineProperty(control, '$controlsRight', {
value: player.$container.querySelector('.jb-pro-controls-right')
});
Object.defineProperty(control, '$volume', {
value: player.$container.querySelector('.jb-pro-volume')
});
Object.defineProperty(control, '$volumePanelWrap', {
value: player.$container.querySelector('.jb-pro-volume-panel-wrap')
});
Object.defineProperty(control, '$volumePanelText', {
value: player.$container.querySelector('.jb-pro-volume-panel-text')
});
Object.defineProperty(control, '$volumePanel', {
value: player.$container.querySelector('.jb-pro-volume-panel')
});
Object.defineProperty(control, '$volumeHandle', {
value: player.$container.querySelector('.jb-pro-volume-panel-handle')
});
Object.defineProperty(control, '$volumeOn', {
value: player.$container.querySelector('.jb-pro-icon-audio')
});
Object.defineProperty(control, '$volumeOff', {
value: player.$container.querySelector('.jb-pro-icon-mute')
});
Object.defineProperty(control, '$fullscreen', {
value: player.$container.querySelector('.jb-pro-fullscreen')
});
Object.defineProperty(control, '$fullscreenExit', {
value: player.$container.querySelector('.jb-pro-fullscreen-exit')
});
Object.defineProperty(control, '$record', {
value: player.$container.querySelector('.jb-pro-record')
});
Object.defineProperty(control, '$recordStop', {
value: player.$container.querySelector('.jb-pro-record-stop')
});
Object.defineProperty(control, '$screenshot', {
value: player.$container.querySelector('.jb-pro-screenshot')
});
Object.defineProperty(control, '$speed', {
value: player.$container.querySelector('.jb-pro-speed')
});
Object.defineProperty(control, '$controlHtml', {
value: player.$container.querySelector('.jb-pro-controls-item-html')
});
Object.defineProperty(control, '$playbackTime', {
value: player.$container.querySelector('.jb-pro-controls-playback-time')
});
Object.defineProperty(control, '$playbackTimeInner', {
value: player.$container.querySelector('.jb-pro-controls-playback-time-inner')
});
Object.defineProperty(control, '$playbackTimeScroll', {
value: player.$container.querySelector('.jb-pro-controls-playback-time-scroll')
});
Object.defineProperty(control, '$playbackTimeList', {
value: player.$container.querySelector('.jb-pro-controls-playback-time-list')
});
Object.defineProperty(control, '$playbackTimeListOne', {
value: player.$container.querySelector('.jb-pro-playback-time-one-wrap')
});
Object.defineProperty(control, '$playbackTimeListSecond', {
value: player.$container.querySelector('.jb-pro-playback-time-second-wrap')
});
Object.defineProperty(control, '$playbackCurrentTime', {
value: player.$container.querySelector('.jb-pro-controls-playback-current-time')
});
Object.defineProperty(control, '$playbackCurrentTimeText', {
value: player.$container.querySelector('.jb-pro-controls-playback-current-time-text')
});
Object.defineProperty(control, '$controlsPlaybackBtns', {
value: player.$container.querySelector('.jb-pro-controls-playback-btns')
});
Object.defineProperty(control, '$playbackNarrow', {
value: player.$container.querySelector('.jb-pro-playback-narrow')
});
//
Object.defineProperty(control, '$playbackExpand', {
value: player.$container.querySelector('.jb-pro-playback-expand')
});
Object.defineProperty(control, '$ptz', {
value: player.$container.querySelector('.jb-pro-ptz')
});
Object.defineProperty(control, '$ptzActive', {
value: player.$container.querySelector('.jb-pro-ptz-active')
});
Object.defineProperty(control, '$ptzControl', {
value: player.$container.querySelector('.jb-pro-ptz-controls')
});
Object.defineProperty(control, '$ptzBgActive', {
value: player.$container.querySelector('.jb-pro-ptz-bg-active')
});
Object.defineProperty(control, '$ptzControlCircular', {
value: player.$container.querySelector('.jb-pro-ptz-control')
});
Object.defineProperty(control, '$ptzArrows', {
value: player.$container.querySelectorAll('.jb-pro-ptz-arrow')
});
Object.defineProperty(control, '$ptzExpand', {
value: player.$container.querySelector('.jb-pro-ptz-expand')
});
Object.defineProperty(control, '$ptzNarrow', {
value: player.$container.querySelector('.jb-pro-ptz-narrow')
});
Object.defineProperty(control, '$ptzApertureFar', {
value: player.$container.querySelector('.jb-pro-ptz-aperture-far')
});
Object.defineProperty(control, '$ptzApertureNear', {
value: player.$container.querySelector('.jb-pro-ptz-aperture-near')
});
Object.defineProperty(control, '$ptzFocusFar', {
value: player.$container.querySelector('.jb-pro-ptz-focus-far')
});
Object.defineProperty(control, '$ptzFocusNear', {
value: player.$container.querySelector('.jb-pro-ptz-focus-near')
});
Object.defineProperty(control, '$ptzCruisePlay', {
value: player.$container.querySelector('.jb-pro-ptz-cruise-play')
});
Object.defineProperty(control, '$ptzCruisePause', {
value: player.$container.querySelector('.jb-pro-ptz-cruise-pause')
});
Object.defineProperty(control, '$ptzFogOpen', {
value: player.$container.querySelector('.jb-pro-ptz-fog-open')
});
Object.defineProperty(control, '$ptzFogClose', {
value: player.$container.querySelector('.jb-pro-ptz-fog-close')
});
Object.defineProperty(control, '$ptzWiperOpen', {
value: player.$container.querySelector('.jb-pro-ptz-wiper-open')
});
Object.defineProperty(control, '$ptzWiperClose', {
value: player.$container.querySelector('.jb-pro-ptz-wiper-close')
});
Object.defineProperty(control, '$qualityText', {
value: player.$container.querySelector('.jb-pro-quality-icon-text')
});
Object.defineProperty(control, '$qualityMenu', {
value: player.$container.querySelector('.jb-pro-quality-menu')
});
Object.defineProperty(control, '$qualityMenuList', {
value: player.$container.querySelector('.jb-pro-quality-menu-list')
});
Object.defineProperty(control, '$scaleText', {
value: player.$container.querySelector('.jb-pro-scale-icon-text')
});
Object.defineProperty(control, '$scaleMenu', {
value: player.$container.querySelector('.jb-pro-scale-menu')
});
Object.defineProperty(control, '$scaleMenuList', {
value: player.$container.querySelector('.jb-pro-scale-menu-list')
});
Object.defineProperty(control, '$zoom', {
value: player.$container.querySelector('.jb-pro-zoom')
});
Object.defineProperty(control, '$zoomStop', {
value: player.$container.querySelector('.jb-pro-zoom-stop')
});
Object.defineProperty(control, '$zoomNarrow', {
value: player.$container.querySelector('.jb-pro-zoom-narrow')
});
Object.defineProperty(control, '$zoomExpand', {
value: player.$container.querySelector('.jb-pro-zoom-expand')
});
Object.defineProperty(control, '$zoomStop2', {
value: player.$container.querySelector('.jb-pro-zoom-stop2')
});
Object.defineProperty(control, '$close', {
value: player.$container.querySelector('.jb-pro-close')
});
Object.defineProperty(control, '$zoomControls', {
value: player.$container.querySelector('.jb-pro-zoom-controls')
});
Object.defineProperty(control, '$performancePanel', {
value: player.$container.querySelector('.jb-pro-performance-panel')
});
Object.defineProperty(control, '$performance', {
value: player.$container.querySelector('.jb-pro-performance')
});
Object.defineProperty(control, '$performanceActive', {
value: player.$container.querySelector('.jb-pro-performance-active')
});
Object.defineProperty(control, '$faceDetect', {
value: player.$container.querySelector('.jb-pro-face')
});
Object.defineProperty(control, '$faceDetectActive', {
value: player.$container.querySelector('.jb-pro-face-active')
});
Object.defineProperty(control, '$objectDetect', {
value: player.$container.querySelector('.jb-pro-object')
});
Object.defineProperty(control, '$objectDetectActive', {
value: player.$container.querySelector('.jb-pro-object-active')
});
Object.defineProperty(control, '$occlusionDetect', {
value: player.$container.querySelector('.jb-pro-occlusion')
});
Object.defineProperty(control, '$occlusionDetectActive', {
value: player.$container.querySelector('.jb-pro-occlusion-active')
});
Object.defineProperty(control, '$contextmenus', {
value: player.$container.querySelector('.jb-pro-contextmenus')
});
Object.defineProperty(control, '$speedText', {
value: player.$container.querySelector('.jb-pro-speed-icon-text')
});
Object.defineProperty(control, '$speedMenu', {
value: player.$container.querySelector('.jb-pro-speed-menu')
});
Object.defineProperty(control, '$speedMenuList', {
value: player.$container.querySelector('.jb-pro-speed-menu-list')
});
Object.defineProperty(control, '$logSave', {
value: player.$container.querySelector('.jb-pro-logSave')
});
Object.defineProperty(control, '$playbackProgress', {
value: player.$container.querySelector('.jb-pro-control-progress-simple')
});
Object.defineProperty(control, '$playbackProgressTip', {
value: player.$container.querySelector('.jb-pro-progress-tip')
});
Object.defineProperty(control, '$playbackProgressHover', {
value: player.$container.querySelector('.jb-pro-progress-hover')
});
Object.defineProperty(control, '$playbackProgressPlayed', {
value: player.$container.querySelector('.jb-pro-progress-played')
});
Object.defineProperty(control, '$playbackProgressIndicator', {
value: player.$container.querySelector('.jb-pro-progress-indicator')
});
Object.defineProperty(control, '$playbackProgressTime', {
value: player.$container.querySelector('.jb-pro-playback-control-time')
});
Object.defineProperty(control, '$tipsMessage', {
value: player.$container.querySelector('.jb-pro-tips-message')
});
Object.defineProperty(control, '$tipsMessageClose', {
value: player.$container.querySelector('.jb-pro-tips-message-close')
});
Object.defineProperty(control, '$tipsMessageContent', {
value: player.$container.querySelector('.jb-pro-tips-message-content')
});
});
// 24小时,以一个小时为单位的。
function _isInPlayList(list, time) {
let result = false;
list.forEach(item => {
if (!result) {
if (item.startTimestamp <= time && item.endTimestamp > time) {
result = true;
}
}
});
return result;
}
// 一个小时 60个格子
function initHourMinList1() {
let playbackList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
let result = [];
const playbackItem = playbackList[0] || {};
const oneDay = playbackItem.startTimestamp;
for (let i = 0; i < 24 * 60; i++) {
const isStart = i % 60 === 0;
let hasRecord = false;
if (oneDay) {
const tempMin = formatMinuteTimestamp(oneDay, i);
hasRecord = _isInPlayList(playbackList, tempMin);
}
result.push({
title: formatMinTimeTips(i),
timestamp: i,
dataType: 'min',
hasRecord,
isStart
});
}
return result;
}
// 半个小时 60个格式
function initHalfHourMinSecondList1() {
let playbackList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
let result = [];
const playbackItem = playbackList[0] || {};
const oneDay = playbackItem.startTimestamp;
for (let i = 0; i < 24 * 60; i++) {
let second = i * 60;
let isStart = second % (30 * 60) === 0;
let hasRecord = false;
if (oneDay) {
const tempSecond = formatSecondTimestamp(oneDay, second);
hasRecord = _isInPlayList(playbackList, tempSecond);
}
result.push({
title: formatSecondTimeTips(second),
timestamp: second,
dataType: 'second',
hasRecord,
isStart
});
let second2 = i * 60 + 30;
isStart = second2 % (30 * 60) === 0;
if (oneDay) {
const tempSecond2 = formatSecondTimestamp(oneDay, second2);
hasRecord = _isInPlayList(playbackList, tempSecond2);
}
result.push({
title: formatSecondTimeTips(second2),
timestamp: second2,
dataType: 'second',
hasRecord,
isStart
});
}
return result;
}
// 十分钟 60个格子
function initTenMinSecondList1() {
let playbackList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
//
let result = [];
const playbackItem = playbackList[0] || {};
const oneDay = playbackItem.startTimestamp;
// 时
for (let i = 0; i < 24 * 6; i++) {
// 内层
for (let j = 0; j < 60; j++) {
let step = 10 * j + i * 600;
let isStart = step % (10 * 60) === 0;
let hasRecord = false;
if (oneDay) {
const tempSecond = formatSecondTimestamp(oneDay, step);
hasRecord = _isInPlayList(playbackList, tempSecond);
}
result.push({
title: formatSecondTimeTips(step),
timestamp: step,
dataType: 'second',
isStart,
hasRecord
});
}
}
return result;
}
// 五分钟 60个格子
function initFiveMinSecondList1() {
let playbackList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
// 外层 23个 小时
let result = [];
const playbackItem = playbackList[0] || {};
const oneDay = playbackItem.startTimestamp;
for (let i = 0; i < 24 * 12; i++) {
// 内层 60个格子
for (let j = 0; j < 60; j++) {
let step = 5 * j + i * 300;
let isStart = step % (5 * 60) === 0;
let hasRecord = false;
if (oneDay) {
const tempSecond = formatSecondTimestamp(oneDay, step);
hasRecord = _isInPlayList(playbackList, tempSecond);
}
result.push({
title: formatSecondTimeTips(step),
timestamp: step,
dataType: 'second',
isStart,
hasRecord
});
}
}
return result;
}
// 24小时 以一个小时间隔
function initHourList2() {
let result = [];
for (let i = 0; i < 24; i++) {
let title = i + ":00";
if (i < 10) {
title = "0" + title;
}
result.push({
title: title,
hour: i,
min: 0,
second: 0
});
}
return result;
}
// 24小时 以半个小时间隔
function initHalfHourList2() {
let result = [];
for (let i = 0; i < 24; i++) {
let title = i + ":00";
let titleHalf = i + ':30';
if (i < 10) {
title = "0" + title;
titleHalf = '0' + titleHalf;
}
result.push({
title: title,
hour: i,
min: 0,
second: 0
});
result.push({
title: titleHalf,
hour: i,
min: 30,
second: 0
});
}
return result;
}
// 24小时,10分钟为间隔
function initTenMinList2() {
let result = [];
for (let i = 0; i < 24; i++) {
let title = i + ":00";
if (i < 10) {
title = "0" + title;
}
result.push({
title: title,
hour: i,
min: 0,
second: 0
});
for (let j = 1; j < 6; j++) {
let tenMin = '' + j + '0';
result.push({
title: title.replace(':00', ':' + tenMin),
hour: i,
min: j * 10,
second: 0
});
}
}
return result;
}
// 24小时,5分钟为间隔
function initFileMinList2() {
let result = [];
for (let i = 0; i < 24; i++) {
let title = i + ":00";
if (i < 10) {
title = "0" + title;
}
result.push({
title: title,
hour: i,
min: 0,
second: 0
});
result.push({
title: title.replace(':00', ':05'),
hour: i,
min: 5,
second: 0
});
for (let j = 1; j < 6; j++) {
let tenMin = '' + j + '0';
let fileMin = j + '5';
result.push({
title: title.replace(':00', ':' + tenMin),
hour: i,
min: j * 10,
second: 0
});
result.push({
title: title.replace(':00', ':' + fileMin),
hour: i,
min: j * 10 + 5,
second: 0
});
}
}
return result;
}
function renderTimeDay() {
let oneList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
let secondList = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
let control = arguments.length > 2 ? arguments[2] : undefined;
const oneListLength = oneList.length;
const secondListLength = secondList.length;
const total = Math.max(oneListLength, secondListLength);
const once = PLAYBACK_RENDER_ONCE_LENGTH;
const loopCount = Math.ceil(total / once);
let countRender = 0;
let currentIndex = 0;
function render() {
let fragment = '';
let fragment2 = '';
for (let i = 0; i < once; i++) {
const time = oneList[currentIndex];
if (time) {
fragment += `
${time.title}
`;
}
const hour = secondList[currentIndex];
if (hour) {
fragment2 += `
${hour.title}
`;
}
currentIndex += 1;
}
if (fragment) {
control.$playbackTimeListOne.insertAdjacentHTML('beforeend', fragment);
}
if (fragment2) {
control.$playbackTimeListSecond.insertAdjacentHTML('beforeend', fragment2);
}
countRender += 1;
if (countRender < loopCount) {
control.rafId = window.requestAnimationFrame(render);
}
}
render();
}
// 一个小时一个间隔
function renderHourTimeDay(playbackList, control) {
const minuteList = initHourMinList1(playbackList);
const hourList = initHourList2();
renderTimeDay(minuteList, hourList, control);
}
// 半个小时一个间隔
function renderHalfHourTimeDay(playbackList, control) {
const minuteList = initHalfHourMinSecondList1(playbackList);
const hourList = initHalfHourList2();
renderTimeDay(minuteList, hourList, control);
}
// 10分钟一个间隔
function renderTenMinTimeDay(playbackList, control) {
const minuteList = initTenMinSecondList1(playbackList);
const hourList = initTenMinList2();
renderTimeDay(minuteList, hourList, control);
}
// 5分钟一个间隔
function renderFiveMinTimeDay(playbackList, control) {
const minuteList = initFiveMinSecondList1(playbackList);
const hourList = initFileMinList2();
renderTimeDay(minuteList, hourList, control);
}
function secondToTime(second) {
const add0 = num => num < 10 ? `0${num}` : String(num);
const hour = Math.floor(second / 3600);
const min = Math.floor((second - hour * 3600) / 60);
const sec = Math.floor(second - hour * 3600 - min * 60);
return (hour > 0 ? [hour, min, sec] : [min, sec]).map(add0).join(':');
}
function getPosFromEvent(control, duration, event) {
const $playbackProgress = control.$playbackProgress;
const {
left
} = $playbackProgress.getBoundingClientRect();
const eventLeft = isMobile() ? event.touches[0].clientX : event.pageX;
const width = clamp(eventLeft - left, 0, $playbackProgress.clientWidth);
const second = parseInt(width / $playbackProgress.clientWidth * duration, 10);
const time = secondToTime(second);
const percentage = clamp(width / $playbackProgress.clientWidth, 0, 1);
return {
second,
time,
width,
percentage
};
}
function getPercentage(second, duration) {
return clamp(second / duration, 0, 1);
}
function addClass(target, className) {
return target.classList.add(className);
}
function removeClass(target, className) {
return target.classList.remove(className);
}
function hasClass(target, className) {
return target.classList.contains(className);
}
// append
function append(parent, child) {
if (child instanceof Element) {
parent.appendChild(child);
} else {
parent.insertAdjacentHTML('beforeend', String(child));
}
return parent.lastElementChild || parent.lastChild;
}
function setStyle(element, key, value) {
if (element && element.style && isNotEmpty(key)) {
element.style[key] = value;
}
return element;
}
// include from event
function includeFromEvent(event, target) {
// js 捕获事件冒泡路径。
return event.composedPath && event.composedPath().indexOf(target) > -1;
}
function removeElement(element) {
let result = false;
if (element) {
if (element.parentNode) {
element.parentNode.removeChild(element);
result = true;
}
}
return result;
}
var observer$1 = ((player, control) => {
const {
events: {
proxy
}
} = player;
const object = document.createElement('object');
object.setAttribute('aria-hidden', 'true');
object.setAttribute('tabindex', -1);
object.type = 'text/html';
object.data = 'about:blank';
setStyle$1(object, {
display: 'block',
position: 'absolute',
top: '0',
left: '0',
height: '100%',
width: '100%',
overflow: 'hidden',
pointerEvents: 'none',
zIndex: '-1'
});
let playerWidth = player.width;
let playerHeight = player.height;
const resizeFn = throttle(() => {
if (player.width !== playerWidth || player.height !== playerHeight) {
playerWidth = player.width;
playerHeight = player.height;
player.emit(EVENTS.resize);
screenfullH5Control();
}
}, PLAYER_RESIZE_TIME);
proxy(object, 'load', () => {
proxy(object.contentDocument.defaultView, 'resize', () => {
resizeFn();
});
});
player.$container.appendChild(object);
player.on(EVENTS.destroy, () => {
player.$container.removeChild(object);
});
function setVolumeHandle(percentage) {
if (percentage === 0) {
setStyle$1(control.$volumeOn, 'display', 'none');
setStyle$1(control.$volumeOff, 'display', 'flex');
setStyle$1(control.$volumeHandle, 'top', `${48}px`);
} else {
if (control.$volumeHandle && control.$volumePanel) {
const panelHeight = getStyle(control.$volumePanel, 'height') || 60;
const handleHeight = getStyle(control.$volumeHandle, 'height');
const top = panelHeight - (panelHeight - handleHeight) * percentage - handleHeight;
setStyle$1(control.$volumeHandle, 'top', `${top}px`);
setStyle$1(control.$volumeOn, 'display', 'flex');
setStyle$1(control.$volumeOff, 'display', 'none');
}
}
control.$volumePanelText && (control.$volumePanelText.innerHTML = parseInt(percentage * 100));
}
player.on(EVENTS.volumechange, () => {
setVolumeHandle(player.volume);
});
player.on(EVENTS.loading, flag => {
setStyle$1(control.$loading, 'display', flag ? 'flex' : 'none');
if (isFalse(player._opt.backgroundLoadingShow) && isTrue(flag) || isFalse(flag)) {
// hidden
setStyle$1(control.$poster, 'display', 'none');
}
if (flag) {
setStyle$1(control.$playBig, 'display', 'none');
setStyle$1(control.$tipsMessage, 'display', 'none');
}
if (!flag) {
if (player._opt.extendDomConfig.showAfterLoading && control.$extendDom) {
setStyle$1(control.$extendDom, 'display', 'block');
}
}
// playing
if (!flag) {
// 除了mse解码外。
if (player.getRenderType() === RENDER_TYPE.canvas && isFalse(player._opt.useMSE)) {
handleRemoveLoadingBgImage();
}
}
});
// 主要是给 mse+ video 服务的。
player.on(EVENTS.removeLoadingBgImage, () => {
// need delay to hidden,because video play not immediately show view.
handleRemoveLoadingBgImage();
});
const handleRemoveLoadingBgImage = () => {
if (control.$loadingBgImage && control.$loadingBg && control.$loadingBgImage.src) {
player.debug && player.debug.log('Control', 'remove loading bg image');
control.$loadingBgImage.width = 0;
control.$loadingBgImage.height = 0;
control.$loadingBgImage.src = '';
setStyle$1(control.$loadingBg, 'display', 'none');
}
};
const handleScreenfullChange = e => {
// 全屏
if (player.fullscreen) {
if (getTarget(e) === player.$container) {
screenfullChange();
}
} else {
// 取消全屏
screenfullChange();
}
};
const screenfullChange = fullscreen => {
let isFullScreen = isBoolean(fullscreen) ? fullscreen : player.fullscreen;
setStyle$1(control.$fullscreenExit, 'display', isFullScreen ? 'flex' : 'none');
setStyle$1(control.$fullscreen, 'display', isFullScreen ? 'none' : 'flex');
// control.autoSize();
};
const showPlaybackTFDom = () => {
return player._opt.playType === PLAY_TYPE.playbackTF && player._opt.playbackConfig.showControl;
};
const screenfullH5Control = fn => {
if (isMobile() && control.$controls && player._opt.useWebFullScreen) {
setTimeout(() => {
if (player.fullscreen) {
const controlHeight = showPlaybackTFDom() ? CONTROL_PLAYBACK_HEIGHT : CONTROL_HEIGHT;
let translateX = player.height / 2 - player.width + controlHeight / 2;
let translateY = player.height / 2 - controlHeight / 2;
control.$controls.style.transform = `translateX(${-translateX}px) translateY(-${translateY}px) rotate(-90deg)`;
if (control.$zoomControls) {
const controlsWidth = 156;
const controlsHeight = 30;
const controlsTranslateX = player.width / 2 + controlsWidth / 2 - controlsHeight / 2;
control.$zoomControls.style.transform = `translateX(${-controlsTranslateX}px) translateY(${player.height / 2}px) rotate(-90deg)`;
}
if (control.$recording) {
const recordingWidth = 101;
const recordingHeight = 20;
const recordingTranslateX = player.width / 2 + recordingWidth / 2 - recordingHeight / 2;
control.$recording.style.transform = `translateX(${-recordingTranslateX}px) translateY(${player.height / 2}px) rotate(-90deg)`;
}
} else {
control.$controls.style.transform = `translateX(0) translateY(0) rotate(0)`;
if (control.$zoomControls) {
control.$zoomControls.style.transform = `translateX(-50%) translateY(0) rotate(0)`;
}
if (control.$recording) {
control.$recording.style.transform = `translateX(-50%) translateY(0) rotate(0)`;
}
}
fn && fn();
}, 10);
}
};
try {
screenfull.on('change', handleScreenfullChange);
player.events.destroys.push(() => {
screenfull.off('change', handleScreenfullChange);
});
} catch (error) {
//
}
//
player.on(EVENTS.webFullscreen, value => {
if (isMobile()) {
screenfullChange(value);
screenfullH5Control(() => {
_resizePlaybackTime();
});
}
});
player.on(EVENTS.recording, () => {
if (player.playing) {
setStyle$1(control.$record, 'display', player.recording ? 'none' : 'flex');
setStyle$1(control.$recordStop, 'display', player.recording ? 'flex' : 'none');
if (player._opt.hasControl || player._opt.isShowRecordingUI) {
setStyle$1(control.$recording, 'display', player.recording ? 'flex' : 'none');
if (isFalse(player.recording) && control.$recordingTime) {
control.$recordingTime.innerHTML = formatTimeTips(0);
}
}
}
});
//
player.on(EVENTS.recordingTimestamp, timestamp => {
// console.log(timestamp);
control.$recordingTime && (control.$recordingTime.innerHTML = formatTimeTips(timestamp));
});
player.on(EVENTS.zooming, () => {
if (player.playing) {
setStyle$1(control.$zoom, 'display', player.zooming ? 'none' : 'flex');
setStyle$1(control.$zoomStop, 'display', player.zooming ? 'flex' : 'none');
if (player._opt.hasControl || player._opt.isShowZoomingUI) {
setStyle$1(control.$zoomControls, 'display', player.zooming ? 'flex' : 'none');
}
}
});
player.on(EVENTS.playing, flag => {
handlePlaying(flag);
});
const handlePlaying = flag => {
// playing
if (flag) {
setStyle$1(control.$play, 'display', 'none');
setStyle$1(control.$playBig, 'display', 'none');
} else {
// not playing
if (player.isPlayFailedAndPaused && isFalse(player._opt.playFailedAndPausedShowPlayBtn)) {
setStyle$1(control.$play, 'display', 'none');
setStyle$1(control.$playBig, 'display', 'none');
} else {
setStyle$1(control.$play, 'display', 'flex');
setStyle$1(control.$playBig, 'display', 'block');
}
}
// setStyle(control.$play, 'display', flag ? 'none' : 'flex');
// setStyle(control.$playBig, 'display', flag ? 'none' : 'block');
setStyle$1(control.$pause, 'display', flag ? 'flex' : 'none');
setStyle$1(control.$screenshot, 'display', flag ? 'flex' : 'none');
setStyle$1(control.$record, 'display', flag ? 'flex' : 'none');
setStyle$1(control.$qualityMenu, 'display', flag ? 'flex' : 'none');
setStyle$1(control.$volume, 'display', flag ? 'flex' : 'none');
setStyle$1(control.$ptz, 'display', flag ? 'flex' : 'none');
setStyle$1(control.$zoom, 'display', flag ? 'flex' : 'none');
setStyle$1(control.$scaleMenu, 'display', flag ? 'flex' : 'none');
setStyle$1(control.$faceDetect, 'display', flag ? 'flex' : 'none');
setStyle$1(control.$objectDetect, 'display', flag ? 'flex' : 'none');
setStyle$1(control.$occlusionDetect, 'display', flag ? 'flex' : 'none');
setStyle$1(control.$controlHtml, 'display', flag ? 'flex' : 'none');
if (player.isPlayback()) {
setStyle$1(control.$speedMenu, 'display', flag ? 'flex' : 'none');
}
screenfullChange();
control.extendBtnList.forEach(item => {
if (item.$iconWrap) {
setStyle$1(item.$iconWrap, 'display', flag ? 'flex' : 'none');
}
// 默认是隐藏的
if (item.$activeIconWrap) {
setStyle$1(item.$activeIconWrap, 'display', 'none');
}
});
if (player._opt.showPerformance) {
setStyle$1(control.$performanceActive, 'display', flag ? 'flex' : 'none');
} else {
setStyle$1(control.$performance, 'display', flag ? 'flex' : 'none');
setStyle$1(control.$performanceActive, 'display', 'none');
}
// hidden
setStyle$1(control.$poster, 'display', 'none');
setStyle$1(control.$ptzActive, 'display', 'none');
setStyle$1(control.$recordStop, 'display', 'none');
setStyle$1(control.$zoomStop, 'display', 'none');
setStyle$1(control.$faceDetectActive, 'display', 'none');
setStyle$1(control.$objectDetectActive, 'display', 'none');
// 不在播放
if (!flag) {
control.$speed && (control.$speed.innerHTML = bpsSize(''));
// zoom
setStyle$1(control.$zoomControls, 'display', 'none');
// record
setStyle$1(control.$recording, 'display', 'none');
// ptz
if (control.$ptzControl) {
control.$ptzControl.classList.remove('jb-pro-ptz-controls-show');
}
}
_resizePlaybackTime();
if (flag) {
_playbackTimeOffset();
}
};
//
player.on(EVENTS.playbackPause, flag => {
handlePlaying(!flag);
});
player.on(EVENTS.kBps, rate => {
const bps = bpsSize$2(rate);
control.kbpsShow = bps;
if (player._opt.showBandwidth) {
control.$speed && (control.$speed.innerHTML = bps);
_resizePlaybackTime();
}
});
const _resizePlaybackTime = () => {
if (showPlaybackTFDom()) {
if (player._opt.playbackConfig.controlType === PLAYBACK_CONTROL_TYPE.normal) {
let width = control.controlsInnerRect.width - control.controlsLeftRect.width - control.controlsRightRect.width - control.controlsPlaybackBtnsRect.width;
if (isMobile() && player.webFullscreen) {
width = control.controlsInnerRect.height - control.controlsLeftRect.height - control.controlsRightRect.height - control.controlsPlaybackBtnsRect.height;
}
control.$playbackTimeInner.style.width = width + 'px';
}
}
};
// playback time offset
const _playbackTimeOffset = () => {
if (!showPlaybackTFDom() || player._opt.playbackConfig.controlType !== PLAYBACK_CONTROL_TYPE.normal) {
return;
}
const leftPx = control.$playbackCurrentTime.style.left;
let left = parseInt(leftPx, 10);
const innerWidth = control.controlsPlaybackTimeInner.width;
if (left - innerWidth / 2 > 0) {
left = parseInt(left - innerWidth / 2, 10);
} else {
left = 0;
}
control.$playbackTimeInner.scrollLeft = left;
};
// show playback operate
if (showPlaybackTFDom()) {
// 计算偏移量
const _calcCurrentTimeTextOffset = () => {
if (showPlaybackTFDom()) {
let left = 0;
const playingTimestamp = player.playback && player.playback.playingTimestamp;
if (playingTimestamp) {
const nowDate = new Date(playingTimestamp);
const hour = nowDate.getHours();
const min = nowDate.getMinutes();
const second = nowDate.getSeconds();
if (player.playback.is60Min) {
left = hour * 60 + min;
} else if (player.playback.is30Min) {
left = (hour * 60 + min) * 2 + parseInt(second / 30, 10);
} else if (player.playback.is10Min) {
left = (hour * 60 + min) * 6 + parseInt(second / 10, 10);
} else if (player.playback.is5Min) {
left = (hour * 60 + min) * 12 + parseInt(second / 5, 10);
} else if (player.playback.is1Min) {
left = (hour * 60 + min) * 60 + parseInt(second, 10);
}
control.$playbackCurrentTime.style.left = left + 'px';
// console.log('playingTimestamp', playingTimestamp, 'hour is ', hour, 'min is', min, 'second is', second, 'left is ', left)
}
}
};
// toggle playback button
const _togglePlaybackButton = precision => {
control.$playbackNarrow.classList.remove('disabled');
control.$playbackExpand.classList.remove('disabled');
if (precision === PLAYBACK_CONTROL_TIME_PRECISION.oneHour) {
control.$playbackNarrow.classList.add('disabled');
}
if (precision === PLAYBACK_CONTROL_TIME_PRECISION.fiveMin) {
control.$playbackExpand.classList.add('disabled');
}
};
//
// if (player._opt.showBandwidth) {
// control.$controlsLeft.style.width = '90px'
// }
// 播放时间
player.on(EVENTS.playbackTime, time => {
if (player._opt.playbackConfig.controlType === PLAYBACK_CONTROL_TYPE.normal) {
control.$playbackCurrentTimeText && (control.$playbackCurrentTimeText.innerText = parseTime(time, "{h}:{i}:{s}"));
_calcCurrentTimeTextOffset();
} else if (player._opt.playbackConfig.controlType === PLAYBACK_CONTROL_TYPE.simple) {
// console.log('playbackTime', time);
const percentage = getPercentage(time, player.playback.totalDuration);
control.$playbackProgressPlayed.style.width = `${percentage * 100}%`;
control.$playbackProgressIndicator.style.left = `calc(${percentage * 100}% - ${14 / 2}px)`;
control.$playbackProgressTime.innerText = `${secondToTime(time)} / ${secondToTime(player.playback.totalDuration)}`;
}
});
// 缩放类型
player.on(EVENTS.playbackPrecision, (precision, playbackList) => {
if (!showPlaybackTFDom() || player._opt.playbackConfig.controlType !== PLAYBACK_CONTROL_TYPE.normal) {
return;
}
// update class
control.$playbackTimeScroll.classList.remove(PLAYBACK_CONTROL_TIME_PRECISION_CLASS.oneHour, PLAYBACK_CONTROL_TIME_PRECISION_CLASS.halfHour, PLAYBACK_CONTROL_TIME_PRECISION_CLASS.fiveMin, PLAYBACK_CONTROL_TIME_PRECISION_CLASS.tenMin);
control.$playbackTimeScroll.classList.add(PLAYBACK_CONTROL_TIME_PRECISION_CLASS[precision]);
if (control.rafId) {
window.cancelAnimationFrame(control.rafId);
control.rafId = null;
}
if (control.changePercisitionInterval) {
clearTimeout(control.changePercisitionInterval);
control.changePercisitionInterval = null;
}
// clear inner html
control.$playbackTimeListOne.innerHTML = '';
control.$playbackTimeListSecond.innerHTML = '';
control.changePercisitionInterval = setTimeout(() => {
control.$playbackTimeListOne.innerHTML = '';
control.$playbackTimeListSecond.innerHTML = '';
switch (precision) {
case PLAYBACK_CONTROL_TIME_PRECISION.oneHour:
renderHourTimeDay(playbackList, control);
break;
case PLAYBACK_CONTROL_TIME_PRECISION.halfHour:
renderHalfHourTimeDay(playbackList, control);
break;
case PLAYBACK_CONTROL_TIME_PRECISION.tenMin:
renderTenMinTimeDay(playbackList, control);
break;
case PLAYBACK_CONTROL_TIME_PRECISION.fiveMin:
renderFiveMinTimeDay(playbackList, control);
break;
}
_calcCurrentTimeTextOffset();
if (player._opt.playbackConfig.showPrecisionBtn) {
_togglePlaybackButton(precision);
}
_playbackTimeOffset();
}, 16);
// if (renderDom) {
// control.$playbackTimeList.insertAdjacentHTML(
// 'beforeend',
// renderDom
// )
//
// }
});
// resize
player.on(EVENTS.resize, () => {
_resizePlaybackTime();
});
// scroll
player.on(EVENTS.playbackTimeScroll, () => {
_playbackTimeOffset();
});
//
_resizePlaybackTime();
}
// quality
if (player._opt.operateBtns.quality && player._opt.qualityConfig.length > 0) {
player.on(EVENTS.streamQualityChange, value => {
_changeStreamQuality(value);
});
const _changeStreamQuality = value => {
control.$qualityText.innerText = value;
control.$qualityMenuItems.forEach($qualityMenuItem => {
const quality = $qualityMenuItem.dataset.quality;
$qualityMenuItem.classList.remove('jb-pro-quality-menu-item-active');
if (quality === value) {
$qualityMenuItem.classList.add('jb-pro-quality-menu-item-active');
}
});
};
const _renderQuality = () => {
const qualityList = player._opt.qualityConfig || [];
let qualityDom = '';
qualityList.forEach(item => {
qualityDom += `
`;
});
if (qualityDom) {
control.$qualityMenuList.insertAdjacentHTML('beforeend', qualityDom);
//
Object.defineProperty(control, '$qualityMenuItems', {
value: player.$container.querySelectorAll('.jb-pro-quality-menu-item')
});
// init default quality
}
};
_renderQuality();
if (player.streamQuality) {
_changeStreamQuality(player.streamQuality);
}
}
// scale
if (player._opt.operateBtns.scale && player._opt.scaleConfig.length > 0) {
player.on(EVENTS.viewResizeChange, value => {
_changeViewResize(value);
});
const _changeViewResize = value => {
const scaleTypeText = player._opt.scaleConfig[value];
control.$scaleText.innerText = scaleTypeText;
control.$scaleMenuItems.forEach($scaleMenuItem => {
const scale = $scaleMenuItem.dataset.scale;
$scaleMenuItem.classList.remove('jb-pro-scale-menu-item-active');
if (toNumber(scale) === toNumber(value)) {
$scaleMenuItem.classList.add('jb-pro-scale-menu-item-active');
}
});
};
const _renderScale = () => {
const scaleList = player._opt.scaleConfig || [];
let scaleDom = '';
scaleList.forEach((item, index) => {
scaleDom += `
`;
});
if (scaleDom) {
control.$scaleMenuList.insertAdjacentHTML('beforeend', scaleDom);
//
Object.defineProperty(control, '$scaleMenuItems', {
value: player.$container.querySelectorAll('.jb-pro-scale-menu-item')
});
// init default quality
}
};
_renderScale();
_changeViewResize(player.scaleType);
}
// playback rate ui
if (player.isPlayback()) {
if (player._opt.playbackConfig.showRateBtn && player._opt.playbackConfig.rateConfig.length > 0) {
player.on(EVENTS.playbackRateChange, value => {
_changePlaybackSpeed(value);
});
const _changePlaybackSpeed = value => {
const speedConfig = player._opt.playbackConfig.rateConfig;
const speedItem = speedConfig.find(item => {
return toNumber(item.value) === toNumber(value);
});
if (speedItem) {
control.$speedText.innerText = speedItem.label;
control.$speedMenuItems.forEach($speedMenuItem => {
const speed = $speedMenuItem.dataset.speed;
$speedMenuItem.classList.remove('jb-pro-speed-menu-item-active');
if (toNumber(speed) === toNumber(value)) {
$speedMenuItem.classList.add('jb-pro-speed-menu-item-active');
}
});
}
};
const _renderPlaybackSpeed = () => {
const speedList = player._opt.playbackConfig.rateConfig;
let scaleDom = '';
speedList.forEach((item, index) => {
scaleDom += `
`;
});
if (scaleDom) {
control.$speedMenuList.insertAdjacentHTML('beforeend', scaleDom);
//
Object.defineProperty(control, '$speedMenuItems', {
value: player.$container.querySelectorAll('.jb-pro-speed-menu-item')
});
// init default quality
}
};
_renderPlaybackSpeed();
const defaultRate = player.playback ? player.playback.playbackRate : 1;
_changePlaybackSpeed(defaultRate);
}
}
//
player.on(EVENTS.stats, function () {
let stats = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
if (player._opt.showPerformance) {
setStyle$1(control.$performancePanel, 'display', 'block');
control.$performancePanel.innerHTML = '';
const versionTime = proVersionTime;
const performanceMemory = getPerformanceMemory();
const cpuLevel = player.getCpuLevel();
const cpuLevelTitle = isNotEmpty(cpuLevel) && cpuLevel !== -1 ? `${CPU_LEVEL[cpuLevel]}` : '';
const videoInfo = player.video && player.video.videoInfo || {};
const audioInfo = player.audio && player.audio.audioInfo || {};
const times = player._times || {};
const renderType = player.getRenderType();
const canvasRenderTpe = player.getCanvasRenderType();
const decoderType = player.getDecodeType();
const demuxType = player.getDemuxType();
const streamType = player.getStreamType();
const audioEngine = player.getAudioEngineType();
let recordingDuration = player.getRecordingDuration();
let recordingTotalSize = player.getRecordingByteLength();
const isAudioPlaybackRateSpeed = player.isAudioPlaybackRateSpeed();
const videoIframeIntervalTs = player.videoIframeIntervalTs;
recordingDuration = formatTimeTips(recordingDuration);
recordingTotalSize = formatFileSize(recordingTotalSize);
const playType = player.isPlayback() ? '录播' : '直播';
let isDropping = stats.isDropping;
// let isWebrtcH264 = player.isWebrtcH264();
const isMSEDecodeAudio = player._opt.useMSE && player._opt.mseDecodeAudio;
const kbpsShow = player.control ? player.control.kbpsShow : '0 KB/s';
const videoPlaybackQuality = player.getVideoPlaybackQuality();
const renderDom = `
版本 ${versionTime}
${player._opt.isMulti ? `
UUid ${player._opt.debugUuid}
` : ''}
${player.isInMulti() ? `
窗口下标 ${player._opt.multiIndex}
` : ''}
${performanceMemory ? `
内存大小限制 ${formatFileSize(performanceMemory.jsHeapSizeLimit)}
可使用的内存 ${formatFileSize(performanceMemory.totalJSHeapSize)}
已使用的内存 ${formatFileSize(performanceMemory.usedJSHeapSize)}
` : ''}
${cpuLevelTitle ? `
CPU压力情况 ${cpuLevelTitle}
` : ''}
${performanceMemory && performanceMemory.usedJSHeapSize > performanceMemory.totalJSHeapSize ? `
可能内存泄漏 是}
` : ''}
播放模式 ${playType}
${player.isPlayback() ? `
播放倍率 ${player.playback.rate}倍
播放模式 ${player.playback.isUseFpsRender ? '固定FPS' : '动态FPS'}
${player.playback.isUseFpsRender ? `
固定FPS ${player.video.getStreamFps()}
` : ''}
` : ""}
解封装模式 ${DEMUX_TYPE_SHOW[demuxType]}
解码模式 ${decoderType}
渲染组件 ${renderType}
${renderType === RENDER_TYPE.canvas ? `
渲染引擎 ${canvasRenderTpe}
` : ''}
网络请求组件 ${streamType}
视频格式 ${videoInfo.encType || '-'}
视频(宽x高) ${videoInfo.width || '-'}x${videoInfo.height || '-'}
${player.isPlayer() ? `
视频GOP(ms) ${videoIframeIntervalTs || '-'}
` : ''}
音频格式 ${AUDIO_ENC_CODE_SHOW[audioInfo.encType] || '-'}
音频引擎 ${audioEngine || '-'}
音频通道 ${audioInfo.channels || '-'}
音频采样率 ${audioInfo.sampleRate || '-'}
${player.isPlayer() ? `
播放器初始化(ms) ${times.playTimestamp}
开始请求地址(ms) ${times.streamTimestamp}
请求响应(ms) ${times.streamResponseTimestamp}
解封装(ms) ${times.demuxTimestamp}
解码(ms) ${times.decodeTimestamp}
页面开始渲染(ms) ${times.videoTimestamp}
初始化到页面渲染(ms) ${times.allTimestamp}
${player.recording ? `
视频录制时间 ${recordingDuration}
视频录制大小 ${recordingTotalSize}
` : ''}
` : ''}
音频码率(bit) ${stats.abps}
视频码率(bit) ${stats.vbps}
视频帧率(fps) ${stats.fps}
视频峰值帧率(fps) ${stats.maxFps}
解码帧率(fps) ${stats.dfps}
音频缓冲帧 ${stats.audioBuffer}
音频缓冲时长(ms) ${stats.audioBufferDelayTs}
${player.isPlayer() ? `
视频待解码帧 ${stats.demuxBuffer}
` : `
缓存时长(ms) ${stats.playbackCacheDataDuration}
视频待渲染帧 ${stats.playbackVideoBuffer}
视频待解码帧 ${stats.demuxBuffer}
音频待解码帧 ${stats.audioDemuxBuffer}
`}
待解封装数据(byte) ${stats.flvBuffer}
${player._opt.useMSE ? `
MSE缓冲时长(ms) ${stats.mseDelay}
MSE待解码帧 ${stats.msePendingBuffer}
MSE缓存时长(s) ${stats.mseStore}
MSE解码间隔(ms) ${stats.mseDecodeDiffTimes}
MSE解码时间(ms) ${stats.mseTs}
MSE播放模式 ${stats.mseDecodePlaybackRate > 1 ? '加速' : '正常'}
` : ''}
${player._opt.useWCS ? `
WCS解码间隔(ms) ${stats.wcsDecodeDiffTimes}
` : ''}
${player.isOldHls() ? `
HLS缓冲时长(ms) ${stats.hlsDelay}
` : ''}
${player.isUseHls265() ? `
HLS缓冲时长(ms) ${stats.hlsDelay}
HLS待解码帧 ${stats.hlsDemuxLength}
HLS待解码视频帧 ${stats.hlsDemuxVideoLength}
HLS待解码音频帧 ${stats.hlsDemuxAudioLength}
` : ''}
${player.isPlayer() && videoPlaybackQuality ? `
Video已渲染帧 ${videoPlaybackQuality.renderedVideoFrames}
Video已丢弃帧 ${videoPlaybackQuality.droppedVideoFrames}
` : ''}
${player.isPlayer() ? `
网络延迟(ms) ${stats.netBuf}
缓冲时长(ms) ${stats.buf}
最新缓冲时长(ms) ${stats.pushLatestDelay}
` : ''}
${player._opt.useMSE || player.isWebrtcH264() || player.isAliyunRtc() ? `
video显示时间(s) ${stats.videoCurrentTime}
video间隔时间(s) ${stats.videoCurrentTimeDiff}
videoBuffer缓存时间(ms) ${stats.mseVideoBufferDelayTime}
` : ''}
视频显示时间(ms) ${stats.currentPts || stats.ts}
${player._opt.hasAudio && player.isAudioNotMute() && isFalse(isMSEDecodeAudio) ? `
音频显示时间(ms) ${stats.audioTs}
${player._opt.hasVideo ? `
音视频同步时间戳(ms) ${stats.audioSyncVideo}
` : ''}
音频播放模式 ${isAudioPlaybackRateSpeed ? '加速' : '正常'}
` : ''}
视频解码时间(ms) ${stats.dts}
${player.isPlayer() ? `
解码前-解码后延迟(ms) ${stats.delayTs}
总延迟(网络+解码)(ms) ${stats.totalDelayTs}
` : ''}
${player.isPlayer() && stats.isStreamTsMoreThanLocal ? `
是否超过一倍率推流 是
` : ''}
${player.isPlayer() ? `
是否播放流畅 ${stats.videoSmooth}
` : ''}
${player.isPlayer() ? `
是否在丢帧 ${isDropping}
` : ''}
网速 ${kbpsShow}
播放时长(s) ${formatTimeTips(stats.pTs)}
`;
control.$performancePanel.insertAdjacentHTML('beforeend', renderDom);
} else {
control.$performancePanel.innerHTML = '';
setStyle$1(control.$performancePanel, 'display', 'none');
}
});
player.on(EVENTS.togglePerformancePanel, flag => {
setStyle$1(control.$performance, 'display', flag ? 'none' : 'flex');
setStyle$1(control.$performanceActive, 'display', flag ? 'flex' : 'none');
});
player.on(EVENTS.faceDetectActive, flag => {
setStyle$1(control.$faceDetect, 'display', flag ? 'none' : 'flex');
setStyle$1(control.$faceDetectActive, 'display', flag ? 'flex' : 'none');
});
player.on(EVENTS.objectDetectActive, flag => {
setStyle$1(control.$objectDetect, 'display', flag ? 'none' : 'flex');
setStyle$1(control.$objectDetectActive, 'display', flag ? 'flex' : 'none');
});
player.on(EVENTS.occlusionDetectActive, flag => {
setStyle$1(control.$occlusionDetect, 'display', flag ? 'none' : 'flex');
setStyle$1(control.$occlusionDetectActive, 'display', flag ? 'flex' : 'none');
});
});
var property = ((player, control) => {
Object.defineProperty(control, 'controlsRect', {
get: () => {
return control.$controls.getBoundingClientRect();
}
});
Object.defineProperty(control, 'controlsInnerRect', {
get: () => {
return control.$controlsInner.getBoundingClientRect();
}
});
Object.defineProperty(control, 'controlsLeftRect', {
get: () => {
return control.$controlsLeft.getBoundingClientRect();
}
});
Object.defineProperty(control, 'controlsRightRect', {
get: () => {
return control.$controlsRight.getBoundingClientRect();
}
});
Object.defineProperty(control, 'controlsPlaybackTimeInner', {
get: () => {
return control.$playbackTimeInner && control.$playbackTimeInner.getBoundingClientRect() || {};
}
});
Object.defineProperty(control, 'controlsPlaybackBtnsRect', {
get: () => {
return control.$controlsPlaybackBtns && control.$controlsPlaybackBtns.getBoundingClientRect() || {
width: 0
};
}
});
});
var events = ((player, control) => {
const {
events: {
proxy
},
debug
} = player;
//
const options = player._opt;
const operateBtns = options.operateBtns;
function volumeChangeFromEvent(event) {
const {
bottom: panelBottom,
height: panelHeight
} = control.$volumePanel.getBoundingClientRect();
const {
height: handleHeight
} = control.$volumeHandle.getBoundingClientRect();
let moveLen = event.y;
// if (isMobile() && player.fullscreen) {
// moveLen = event.x;
// }
const percentage = clamp(panelBottom - moveLen - handleHeight / 2, 0, panelHeight - handleHeight / 2) / (panelHeight - handleHeight);
return percentage;
}
// 右键菜单 for pc
if (isPc()) {
//
proxy(window, ['click', 'contextmenu'], event => {
if (event.composedPath().indexOf(player.$container) > -1) {
control.isFocus = true;
} else {
control.isFocus = false;
}
});
}
proxy(control.$controls, 'click', e => {
e.stopPropagation();
});
if (operateBtns.play) {
proxy(control.$pause, 'click', e => {
if (options.playType === PLAY_TYPE.playbackTF && options.playbackConfig.uiUsePlaybackPause) {
player.playbackPause = true;
// player.emit(EVENTS.playbackPauseOrResume, true);
} else {
if (isFunction$1(operateBtns.pauseFn)) {
operateBtns.pauseFn();
} else {
player.pauseForControl();
}
}
});
// 监听 play 方法
proxy(control.$play, 'click', e => {
if (options.playType === PLAY_TYPE.playbackTF && player.playbackPause) {
player.playbackPause = false;
// player.emit(EVENTS.playbackPauseOrResume, false);
} else {
if (isFunction$1(operateBtns.playFn)) {
operateBtns.playFn();
} else {
player.playForControl().then(() => {
player.resumeAudioAfterPause();
});
}
}
});
}
// 监听 play 方法
proxy(control.$playBig, 'click', e => {
if (options.playType === PLAY_TYPE.playbackTF && player.playbackPause) {
player.playbackPause = false;
// player.emit(EVENTS.playbackPauseOrResume, false);
} else {
if (isFunction$1(operateBtns.playFn)) {
operateBtns.playFn();
} else {
player.playForControl().then(() => {
player.resumeAudioAfterPause();
});
}
}
});
if (operateBtns.screenshot) {
proxy(control.$screenshot, 'click', e => {
e.stopPropagation();
if (isFunction$1(operateBtns.screenshotFn)) {
operateBtns.screenshotFn();
} else {
player.video.screenshot();
}
});
}
if (operateBtns.audio) {
// audio panel
if (isPc()) {
proxy(control.$volume, 'mouseover', () => {
control.$volumePanelWrap.classList.add('jb-pro-volume-panel-wrap-show');
});
proxy(control.$volume, 'mouseout', () => {
control.$volumePanelWrap.classList.remove('jb-pro-volume-panel-wrap-show');
});
proxy(control.$volumePanel, 'click', event => {
event.stopPropagation();
player.volume = volumeChangeFromEvent(event);
});
proxy(control.$volumeHandle, 'mousedown', event => {
event.stopPropagation();
control.isVolumeDroging = true;
});
proxy(control.$volumeHandle, 'mousemove', event => {
if (control.isVolumeDroging) {
player.volume = volumeChangeFromEvent(event);
}
});
proxy(document, 'mouseup', () => {
if (control.isVolumeDroging) {
control.isVolumeDroging = false;
}
});
}
proxy(control.$volumeOn, 'click', e => {
e.stopPropagation();
setStyle$1(control.$volumeOn, 'display', 'none');
setStyle$1(control.$volumeOff, 'display', 'block');
const lastVolume = player.volume;
player.volume = 0;
// inner
player._lastVolume = isPc() ? lastVolume : 1;
});
proxy(control.$volumeOff, 'click', e => {
e.stopPropagation();
setStyle$1(control.$volumeOn, 'display', 'block');
setStyle$1(control.$volumeOff, 'display', 'none');
player.volume = isPc() ? player.lastVolume || 0.5 : 1;
});
}
if (operateBtns.record) {
proxy(control.$record, 'click', e => {
e.stopPropagation();
if (isFunction$1(operateBtns.recordFn)) {
operateBtns.recordFn();
} else {
player.recording = true;
}
});
proxy(control.$recordStop, 'click', e => {
e.stopPropagation();
if (isFunction$1(operateBtns.recordStopFn)) {
operateBtns.recordStopFn();
} else {
player.recording = false;
}
});
}
proxy(control.$recordingStop, 'click', e => {
e.stopPropagation();
if (isFunction$1(operateBtns.recordStopFn)) {
operateBtns.recordStopFn();
} else {
player.recording = false;
}
});
if (operateBtns.fullscreen) {
proxy(control.$fullscreen, 'click', e => {
e.stopPropagation();
if (isFunction$1(operateBtns.fullscreenFn)) {
operateBtns.fullscreenFn();
} else {
player.fullscreen = true;
}
});
proxy(control.$fullscreenExit, 'click', e => {
e.stopPropagation();
if (isFunction$1(operateBtns.fullscreenExitFn)) {
operateBtns.fullscreenExitFn();
} else {
player.fullscreen = false;
}
});
}
if (operateBtns.ptz) {
proxy(control.$ptz, 'click', e => {
e.stopPropagation();
setStyle$1(control.$ptzActive, 'display', 'flex');
setStyle$1(control.$ptz, 'display', 'none');
control.$ptzControl.classList.add('jb-pro-ptz-controls-show');
});
proxy(control.$ptzActive, 'click', e => {
e.stopPropagation();
setStyle$1(control.$ptz, 'display', 'flex');
setStyle$1(control.$ptzActive, 'display', 'none');
control.$ptzControl.classList.remove('jb-pro-ptz-controls-show');
});
// 绑定在 ptz 按钮上面 才会出现的。
control.$ptzArrows.forEach($ptzArrow => {
if (options.ptzClickType === PTZ_ACTIVE_EVENT_TYPE.click) {
proxy($ptzArrow, 'click', e => {
e.stopPropagation();
const target = e.currentTarget;
const dataset = target.dataset;
const arrow = dataset.arrow;
control.$ptzBgActive.classList.add('jb-pro-ptz-bg-active-show');
control.$ptzBgActive.classList.add(`jb-pro-ptz-bg-active-${arrow}`);
control.$ptzControlCircular.classList.add(`jb-pro-ptz-control-${arrow}`);
player.emit(EVENTS.ptz, convertToCamelCase(arrow));
setTimeout(() => {
control.$ptzBgActive.classList.remove('jb-pro-ptz-bg-active-show');
PTZ_ARROW.forEach(arrow => {
control.$ptzBgActive.classList.remove(`jb-pro-ptz-bg-active-${arrow}`);
control.$ptzControlCircular.classList.remove(`jb-pro-ptz-control-${arrow}`);
});
player.emit(EVENTS.ptz, PTZ_OBJ.stop);
}, options.ptzStopEmitDelay * 1000);
});
} else if (options.ptzClickType === PTZ_ACTIVE_EVENT_TYPE.mouseDownAndUp) {
let isPtzMouseDown = false;
proxy($ptzArrow, 'mousedown', e => {
e.stopPropagation();
isPtzMouseDown = true;
const target = e.currentTarget;
const dataset = target.dataset;
const arrow = dataset.arrow;
control.$ptzBgActive.classList.add('jb-pro-ptz-bg-active-show');
control.$ptzBgActive.classList.add(`jb-pro-ptz-bg-active-${arrow}`);
control.$ptzControlCircular.classList.add(`jb-pro-ptz-control-${arrow}`);
player.emit(EVENTS.ptz, convertToCamelCase(arrow));
});
const ptzStop = () => {
isPtzMouseDown = false;
control.$ptzBgActive.classList.remove('jb-pro-ptz-bg-active-show');
PTZ_ARROW.forEach(arrow => {
control.$ptzBgActive.classList.remove(`jb-pro-ptz-bg-active-${arrow}`);
control.$ptzControlCircular.classList.remove(`jb-pro-ptz-control-${arrow}`);
});
player.emit(EVENTS.ptz, PTZ_OBJ.stop);
};
proxy($ptzArrow, 'mouseup', e => {
e.stopPropagation();
if (isPtzMouseDown) {
ptzStop();
}
});
proxy(window, 'mouseup', e => {
e.stopPropagation();
if (isPtzMouseDown) {
ptzStop();
}
});
}
});
if (options.ptzZoomShow) {
if (options.ptzClickType === PTZ_ACTIVE_EVENT_TYPE.click) {
proxy(control.$ptzExpand, 'click', e => {
e.stopPropagation();
player.emit(EVENTS.ptz, PTZ_OBJ.zoomExpand);
setTimeout(() => {
player.emit(EVENTS.ptz, PTZ_OBJ.stop);
}, options.ptzStopEmitDelay * 1000);
});
proxy(control.$ptzNarrow, 'click', e => {
e.stopPropagation();
player.emit(EVENTS.ptz, PTZ_OBJ.zoomNarrow);
setTimeout(() => {
player.emit(EVENTS.ptz, PTZ_OBJ.stop);
}, options.ptzStopEmitDelay * 1000);
});
} else if (options.ptzClickType === PTZ_ACTIVE_EVENT_TYPE.mouseDownAndUp) {
let isPtzExpandMouseDown = false;
let isPtzNarrowMouseDown = false;
proxy(control.$ptzExpand, 'mousedown', e => {
e.stopPropagation();
isPtzExpandMouseDown = true;
player.emit(EVENTS.ptz, PTZ_OBJ.zoomExpand);
});
proxy(control.$ptzNarrow, 'mousedown', e => {
e.stopPropagation();
isPtzNarrowMouseDown = true;
player.emit(EVENTS.ptz, PTZ_OBJ.zoomNarrow);
});
const ptzStop = () => {
isPtzExpandMouseDown = false;
isPtzNarrowMouseDown = false;
player.emit(EVENTS.ptz, PTZ_OBJ.stop);
};
proxy(control.$ptzExpand, 'mouseup', e => {
e.stopPropagation();
if (isPtzExpandMouseDown) {
ptzStop();
}
});
proxy(control.$ptzNarrow, 'mouseup', e => {
e.stopPropagation();
if (isPtzNarrowMouseDown) {
ptzStop();
}
});
proxy(window, 'mouseup', e => {
e.stopPropagation();
if (isPtzExpandMouseDown || isPtzNarrowMouseDown) {
ptzStop();
}
});
}
}
if (options.ptzApertureShow) {
if (options.ptzClickType === PTZ_ACTIVE_EVENT_TYPE.click) {
proxy(control.$ptzApertureFar, 'click', e => {
e.stopPropagation();
player.emit(EVENTS.ptz, PTZ_OBJ.apertureFar);
setTimeout(() => {
player.emit(EVENTS.ptz, PTZ_OBJ.fiStop);
}, options.ptzStopEmitDelay * 1000);
});
proxy(control.$ptzApertureNear, 'click', e => {
e.stopPropagation();
player.emit(EVENTS.ptz, PTZ_OBJ.apertureNear);
setTimeout(() => {
player.emit(EVENTS.ptz, PTZ_OBJ.fiStop);
}, options.ptzStopEmitDelay * 1000);
});
} else if (options.ptzClickType === PTZ_ACTIVE_EVENT_TYPE.mouseDownAndUp) {
let isPtzApertureFarMouseDown = false;
let isPtzApertureNearMouseDown = false;
proxy(control.$ptzApertureFar, 'mousedown', e => {
e.stopPropagation();
isPtzApertureFarMouseDown = true;
player.emit(EVENTS.ptz, PTZ_OBJ.apertureFar);
});
proxy(control.$ptzApertureNear, 'mousedown', e => {
e.stopPropagation();
isPtzApertureNearMouseDown = true;
player.emit(EVENTS.ptz, PTZ_OBJ.apertureNear);
});
const ptzStop = () => {
isPtzApertureFarMouseDown = false;
isPtzApertureNearMouseDown = false;
player.emit(EVENTS.ptz, PTZ_OBJ.fiStop);
};
proxy(control.$ptzApertureFar, 'mouseup', e => {
e.stopPropagation();
if (isPtzApertureFarMouseDown) {
ptzStop();
}
});
proxy(control.$ptzApertureNear, 'mouseup', e => {
e.stopPropagation();
if (isPtzApertureNearMouseDown) {
ptzStop();
}
});
proxy(window, 'mouseup', e => {
e.stopPropagation();
if (isPtzApertureFarMouseDown || isPtzApertureNearMouseDown) {
ptzStop();
}
});
}
}
if (options.ptzFocusShow) {
if (options.ptzClickType === PTZ_ACTIVE_EVENT_TYPE.click) {
proxy(control.$ptzFocusFar, 'click', e => {
e.stopPropagation();
player.emit(EVENTS.ptz, PTZ_OBJ.focusFar);
setTimeout(() => {
player.emit(EVENTS.ptz, PTZ_OBJ.fiStop);
}, options.ptzStopEmitDelay * 1000);
});
proxy(control.$ptzFocusNear, 'click', e => {
e.stopPropagation();
player.emit(EVENTS.ptz, PTZ_OBJ.focusNear);
setTimeout(() => {
player.emit(EVENTS.ptz, PTZ_OBJ.fiStop);
}, options.ptzStopEmitDelay * 1000);
});
} else if (options.ptzClickType === PTZ_ACTIVE_EVENT_TYPE.mouseDownAndUp) {
let isPtzFocusFarMouseDown = false;
let isPtzFocusNearMouseDown = false;
proxy(control.$ptzFocusFar, 'mousedown', e => {
e.stopPropagation();
isPtzFocusFarMouseDown = true;
player.emit(EVENTS.ptz, PTZ_OBJ.focusFar);
});
proxy(control.$ptzFocusNear, 'mousedown', e => {
e.stopPropagation();
isPtzFocusNearMouseDown = true;
player.emit(EVENTS.ptz, PTZ_OBJ.focusNear);
});
const ptzStop = () => {
isPtzFocusFarMouseDown = false;
isPtzFocusNearMouseDown = false;
player.emit(EVENTS.ptz, PTZ_OBJ.fiStop);
};
proxy(control.$ptzFocusFar, 'mouseup', e => {
e.stopPropagation();
if (isPtzFocusFarMouseDown) {
ptzStop();
}
});
proxy(control.$ptzFocusNear, 'mouseup', e => {
e.stopPropagation();
if (isPtzFocusNearMouseDown) {
ptzStop();
}
});
proxy(window, 'mouseup', e => {
e.stopPropagation();
if (isPtzFocusFarMouseDown || isPtzFocusNearMouseDown) {
ptzStop();
}
});
}
}
if (options.ptzCruiseShow) {
proxy(control.$ptzCruisePlay, 'click', e => {
e.stopPropagation();
player.emit(EVENTS.ptz, PTZ_OBJ.cruiseStart);
});
proxy(control.$ptzCruisePause, 'click', e => {
e.stopPropagation();
player.emit(EVENTS.ptz, PTZ_OBJ.cruiseStop);
});
}
if (options.ptzFogShow) {
proxy(control.$ptzFogOpen, 'click', e => {
e.stopPropagation();
player.emit(EVENTS.ptz, PTZ_OBJ.fogOpen);
});
proxy(control.$ptzFogClose, 'click', e => {
e.stopPropagation();
player.emit(EVENTS.ptz, PTZ_OBJ.fogClose);
});
}
if (options.ptzWiperShow) {
proxy(control.$ptzWiperOpen, 'click', e => {
e.stopPropagation();
player.emit(EVENTS.ptz, PTZ_OBJ.wiperOpen);
});
proxy(control.$ptzWiperClose, 'click', e => {
e.stopPropagation();
player.emit(EVENTS.ptz, PTZ_OBJ.wiperClose);
});
}
// 支持拖拽
if (options.ptzSupportDraggable) {
control.isPtzControlDroging = false;
control.tempPtzPosition = {
x: 0,
y: 0
};
proxy(control.$ptzControl, isMobile() ? 'touchstart' : 'mousedown', event => {
event.stopPropagation();
control.isPtzControlDroging = true;
control.$ptzControl.style.cursor = 'grabbing';
const {
posX,
posY
} = getMousePosition(event);
control.tempPtzPosition = {
x: posX,
y: posY
};
});
proxy(control.$ptzControl, isMobile() ? 'touchmove' : 'mousemove', event => {
if (control.isPtzControlDroging) {
event.stopPropagation();
const {
posX,
posY
} = getMousePosition(event);
const x = control.tempPtzPosition.x - posX;
const y = control.tempPtzPosition.y - posY;
// offsetLeft取到的值是数值型的,只能取值,不能赋值。
control.$ptzControl.style.left = `${control.$ptzControl.offsetLeft - x}px`;
control.$ptzControl.style.top = `${control.$ptzControl.offsetTop - y}px`;
control.tempPtzPosition = {
x: posX,
y: posY
};
}
});
function handleMouseUp() {
if (control.isPtzControlDroging) {
control.isPtzControlDroging = false;
control.$ptzControl.style.cursor = 'grab';
control.tempPtzPosition = {
x: 0,
y: 0
};
}
}
proxy(control.$ptzControl, isMobile() ? 'touchend' : 'mouseup', event => {
event.stopPropagation();
handleMouseUp();
});
proxy(window, isMobile() ? 'touchend' : 'mouseup', event => {
event.stopPropagation();
handleMouseUp();
});
}
}
if (operateBtns.performance) {
proxy(control.$performance, 'click', e => {
e.stopPropagation();
player.togglePerformancePanel(true);
});
proxy(control.$performanceActive, 'click', e => {
e.stopPropagation();
player.togglePerformancePanel(false);
});
}
if (operateBtns.logSave) {
proxy(control.$logSave, 'click', e => {
e.stopPropagation();
player.downloadMemoryLog();
});
}
if (operateBtns.aiFace) {
proxy(control.$faceDetect, 'click', e => {
e.stopPropagation();
player.faceDetect(true);
});
proxy(control.$faceDetectActive, 'click', e => {
e.stopPropagation();
player.faceDetect(false);
});
}
if (operateBtns.aiObject) {
proxy(control.$objectDetect, 'click', e => {
e.stopPropagation();
player.objectDetect(true);
});
proxy(control.$objectDetectActive, 'click', e => {
e.stopPropagation();
player.objectDetect(false);
});
}
if (operateBtns.aiOcclusion) {
proxy(control.$occlusionDetect, 'click', e => {
e.stopPropagation();
player.occlusionDetect(true);
});
proxy(control.$occlusionDetectActive, 'click', e => {
e.stopPropagation();
player.occlusionDetect(false);
});
}
if (player._opt.hasControl && player._opt.controlAutoHide) {
//
proxy(player.$container, 'mouseover', () => {
if (!player.fullscreen) {
setStyle$1(control.$controls, 'display', 'block');
startDelayControlHidden();
}
});
proxy(player.$container, 'mousemove', () => {
if (player.$container && control.$controls) {
if (!player.fullscreen) {
if (control.$controls.style.display === 'none') {
setStyle$1(control.$controls, 'display', 'block');
startDelayControlHidden();
}
} else {
if (control.$controls.style.display === 'none') {
setStyle$1(control.$controls, 'display', 'block');
startDelayControlHidden();
}
}
}
});
proxy(player.$container, 'mouseout', () => {
stopDelayControlHidden();
setStyle$1(control.$controls, 'display', 'none');
});
let delayHiddenTimeout = null;
const startDelayControlHidden = () => {
stopDelayControlHidden();
delayHiddenTimeout = setTimeout(() => {
setStyle$1(control.$controls, 'display', 'none');
}, 5 * 1000);
};
const stopDelayControlHidden = () => {
if (delayHiddenTimeout) {
clearTimeout(delayHiddenTimeout);
delayHiddenTimeout = null;
}
};
}
// show playback operate
// 录像流监听方法
if (player._opt.playType === PLAY_TYPE.playbackTF) {
let playbackControlType = player._opt.playbackConfig.controlType;
if (player._opt.playbackConfig.showRateBtn) {
proxy(control.$speedMenu, 'mouseover', () => {
control.$speedMenuList.classList.add('jb-pro-speed-menu-shown');
});
proxy(control.$speedMenu, 'mouseout', () => {
control.$speedMenuList.classList.remove('jb-pro-speed-menu-shown');
});
proxy(control.$speedMenuList, 'click', e => {
const target = getTarget(e);
if (target.matches('div.jb-pro-speed-menu-item')) {
const dataset = target.dataset;
player.emit(EVENTS.playbackPreRateChange, dataset.speed);
}
});
}
if (playbackControlType === PLAYBACK_CONTROL_TYPE.normal) {
//
proxy(control.$playbackNarrow, 'click', e => {
e.stopPropagation();
if (player.playback) {
player.playback.narrowPrecision();
}
});
proxy(control.$playbackExpand, 'click', e => {
e.stopPropagation();
if (player.playback) {
player.playback.expandPrecision();
}
});
//
proxy(control.$playbackTimeList, 'click', e => {
const target = getTarget(e);
if (target.matches('div.jb-pro-playback-time-minute-one')) {
if (player.playback) {
player.playback.seek(target.dataset);
}
}
});
// 支持滚动轴
if (player._opt.playbackConfig.supportWheel) {
//
proxy(control.$playbackTimeInner, 'wheel', event => {
event.preventDefault();
const delta = event.wheelDelta ? event.wheelDelta / 120 : -(event.detail || 0) / 3;
// 向下
if (delta > 0) {
if (player.playback) {
player.playback.expandPrecision();
}
}
// 向上
else {
if (player.playback) {
player.playback.narrowPrecision();
}
}
});
}
} else if (playbackControlType === PLAYBACK_CONTROL_TYPE.simple) {
control.isDroging = false;
//
proxy(control.$playbackProgress, 'click', event => {
if (event.target !== control.$playbackProgressIndicator) {
let percentage = 0;
let second = 0;
// 看下是否被旋转了。
if (player.isInWebFullscreen()) {
percentage = event.touches[0].clientY / player.height;
second = percentage * player.playback.totalDuration;
} else {
const position = getPosFromEvent(control, player.playback.totalDuration, event);
percentage = position.percentage;
second = position.second;
}
if (player.playback) {
player.playback.seek({
time: second
});
}
}
});
proxy(control.$playbackProgress, 'mousemove', event => {
setStyle$1(control.$playbackProgressTip, 'display', 'block');
const {
width,
time
} = getPosFromEvent(control, player.playback.totalDuration, event);
control.$playbackProgressTip.innerHTML = time;
const tipWidth = control.$playbackProgressTip.clientWidth;
if (width <= tipWidth / 2) {
setStyle$1(control.$playbackProgressTip, 'left', 0);
} else if (width > control.$playbackProgress.clientWidth - tipWidth / 2) {
setStyle$1(control.$playbackProgressTip, 'left', `${control.$playbackProgress - tipWidth}px`);
} else {
setStyle$1(control.$playbackProgressTip, 'left', `${width - tipWidth / 2}px`);
}
});
proxy(control.$playbackProgress, 'mouseout', () => {
setStyle$1(control.$playbackProgressTip, 'display', 'none');
});
proxy(control.$playbackProgressIndicator, 'mousedown', event => {
control.isDroging = true;
});
proxy(control.$playbackProgress, 'mousemove', event => {
if (control.isDroging) {
const {
second,
percentage
} = getPosFromEvent(control, player.playback.totalDuration, event);
if (player.playback) {
player.playback.seek({
time: second
});
}
}
});
proxy(control.$playbackProgress, 'mouseup', event => {
if (control.isDroging) {
control.isDroging = false;
}
});
}
}
//
if (operateBtns.quality) {
proxy(control.$qualityMenu, 'mouseover', () => {
control.$qualityMenuList.classList.add('jb-pro-quality-menu-shown');
});
proxy(control.$qualityMenu, 'mouseout', () => {
control.$qualityMenuList.classList.remove('jb-pro-quality-menu-shown');
});
proxy(control.$qualityMenuList, 'click', e => {
const target = getTarget(e);
if (target.matches('div.jb-pro-quality-menu-item')) {
const dataset = target.dataset;
player.streamQuality = dataset.quality;
}
});
}
if (operateBtns.scale) {
proxy(control.$scaleMenu, 'mouseover', () => {
control.$scaleMenuList.classList.add('jb-pro-scale-menu-shown');
});
proxy(control.$scaleMenu, 'mouseout', () => {
control.$scaleMenuList.classList.remove('jb-pro-scale-menu-shown');
});
proxy(control.$scaleMenuList, 'click', e => {
const target = getTarget(e);
if (target.matches('div.jb-pro-scale-menu-item')) {
const dataset = target.dataset;
player.setScaleMode(dataset.scale);
}
});
}
if (operateBtns.zoom) {
proxy(control.$zoom, 'click', e => {
e.stopPropagation();
player.zooming = true;
});
proxy(control.$zoomStop, 'click', e => {
e.stopPropagation();
player.zooming = false;
});
}
proxy(control.$zoomExpand, 'click', e => {
e.stopPropagation();
if (player.zoom) {
player.zoom.expandPrecision();
}
});
proxy(control.$zoomNarrow, 'click', e => {
e.stopPropagation();
if (player.zoom) {
player.zoom.narrowPrecision();
}
});
proxy(control.$zoomStop2, 'click', e => {
e.stopPropagation();
player.zooming = false;
});
if (operateBtns.close) {
proxy(control.$close, 'click', e => {
e.stopPropagation();
player.doDestroy();
});
}
proxy(control.$tipsMessageClose, 'click', e => {
e.stopPropagation();
// clear and hidden
control.$tipsMessageContent.innerHTML = '';
setStyle$1(control.$tipsMessage, 'display', 'none');
});
});
function styleInject(css, ref) {
if ( ref === void 0 ) ref = {};
var insertAt = ref.insertAt;
if (!css || typeof document === 'undefined') { return; }
var head = document.head || document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.type = 'text/css';
if (insertAt === 'top') {
if (head.firstChild) {
head.insertBefore(style, head.firstChild);
} else {
head.appendChild(style);
}
} else {
head.appendChild(style);
}
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
}
var css_248z$1 = "@keyframes rotation{0%{-webkit-transform:rotate(0deg)}to{-webkit-transform:rotate(1turn)}}@keyframes magentaPulse{0%{background-color:#630030;-webkit-box-shadow:0 0 9px #333}50%{background-color:#a9014b;-webkit-box-shadow:0 0 18px #a9014b}to{background-color:#630030;-webkit-box-shadow:0 0 9px #333}}.jb-pro-container video::-webkit-media-controls{display:none!important}.jb-pro-container .jb-pro-icon{cursor:pointer;width:16px;height:16px;display:inline-block}.jb-pro-container .jb-pro-ptz-controls{position:absolute;width:156px;height:156px;visibility:hidden;opacity:0;border-radius:78px;background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAATgAAAE4BAMAAAA9UfJZAAAAMFBMVEUAAABHcEy0tLRZWVmysrKoqKi1tbWvr6+2traBgYG1tbWWlpa1tbW1tbVUVFS1tbVGCHqkAAAAD3RSTlMzAO9U3LSWySp3aZcVRDUDw823AAAJYUlEQVR42u3d32sbVxYH8EPHxgg/lBsa7SBkukmpSbwLI2KbEPpgZ5MQtwmM0wRMmgdhP6RgEuwlSVnYlmGMYaEvMU1KKX4QNq0pocVmm7CYfRBaQguFpbgPKRSC/4V2LGliO+bulWKrkvVrftyZ+WbxeTRG+nDnnnNmRjP3EpMR6tMH18du/0Xj1tGz5+9cf/DUlPKx5PsTkr8s3eZ1cX7ym1zkuI/f1wTFunNt9fP+FIno7/98/tFY+Y8ffBUlLrmkl2Cr96guTv27BMxP5iLCqUvi68+tpqhJKPNXBH3SjACnfimm/7Wmsl3fI/FP75lh457oPH+1Da3M+1T8481QcT0T7UetevR618LDPdH4hTlyHLGH3LoZEk6d4PlvyVW8pfNeMwzcDwa/kCKXoTzk9tfB455o1mXyEIOa+0PrFvcFt+fIU8QM/k6guOQifzNFHkN5l/flgsOJVHibfMR9l2nhBqem+VXyFZ/xghkMTp3il8lnDPKiGQROhs2lzjEuKcVW1uWk4ybk2Eq63pxk3CK/RZLiJO+Ti/vZXw3ZX1E+kon7jv+JJMY/+Q15uIRWIKmRthZk4VTDTsnFKYZtSsItWiskObq1Pjm4f8gqIrUF5W8ycAl+nAKIT/iCf1zSKFAgkW4/7drifrLmgsHF2k87alvhblFAcbJttWuDU/VtCiyyedMXbjGfCg6n6H1+cHE+TQFGFx/3jksa2xRoZO2cZ9xsUJn6e8aO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wvx4LDMfYFtz2efMYM/o7LL3OLY080byVlULNusqBx7AeDX3B9aJWH3P6aBY8rpUX+W3e2t3SXqeAZVzq0/JyLmRe7wt0fUs849t8Jzv/u8Ngq/+K8d42FhxODp/P8VQc85VPxjzc9folXHFO/1Lh1rc3BjT0S//SeycLGCd6Sxvm51abDp8xf4dyaNL1/gw+caBhLuvj6O6v36mWn5scEPe+H5hMn4uP3hUEAr63e6y+PYX//qflHY+U/fvCVzw/3ixPD98vSbV4X5ye/yfn+aP+4MvDpg+tjZ4+K8bKOnr1z/cFTU8rH/g92biFxn2S73AAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%;transition:visibility .3s,opacity .3s;right:43px;bottom:135px}.jb-pro-container .jb-pro-ptz-controls.show-vertical{right:43px}.jb-pro-container .jb-pro-ptz-controls.show-vertical .jb-pro-ptz-btns{left:0;top:156px}.jb-pro-container .jb-pro-ptz-controls.show-level{right:163px}.jb-pro-container .jb-pro-ptz-controls.show-level .jb-pro-ptz-btns{min-height:156px;left:144px;top:0;display:flex;flex-direction:column;justify-content:center}.jb-pro-container .jb-pro-ptz-controls.jb-pro-ptz-controls-show{visibility:visible;opacity:1}.jb-pro-container .jb-pro-ptz-bg-active{visibility:hidden;opacity:0;width:156px;height:156px;background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAATgAAAE4CAMAAAD4oR9YAAAAM1BMVEX///////////////////////////9HcEz///////////////////////////////////85yRS0AAAAEXRSTlO5DCgVgZBxAK2fQDRkBR5XTPLKM/gAABnnSURBVHja7F0Jkqs6DGQPO9z/tD8Jq6WWbCCvIjKfzAGmulrW3ooedr6ui+M4TdP++SXPr1l/SdL3aRrHhv7ZyA5qb9xe0L3Am+DrkzeCL/BeX908MezTuPsfOArdgl3KsZuhq99fk/Tx3waum+ByAHua5QbYilkzY1aP728YhrH5InrfBa57OLAtVjpRbYaumex04dq4APeC7vnVSfo/45bXLe33jGscMx3f0A1vyg3t69e2dRL/NeA6wrgdcCvj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pro-ptz-bg-active.jb-pro-ptz-bg-active-up{transform:rotate(-90deg)}.jb-pro-container .jb-pro-ptz-bg-active.jb-pro-ptz-bg-active-left{transform:rotate(180deg)}.jb-pro-container .jb-pro-ptz-bg-active.jb-pro-ptz-bg-active-down{transform:rotate(90deg)}.jb-pro-container .jb-pro-ptz-bg-active.jb-pro-ptz-bg-active-left-up{transform:rotate(-135deg)}.jb-pro-container .jb-pro-ptz-bg-active.jb-pro-ptz-bg-active-right-up{transform:rotate(-45deg)}.jb-pro-container .jb-pro-ptz-bg-active.jb-pro-ptz-bg-active-left-down{transform:rotate(135deg)}.jb-pro-container .jb-pro-ptz-bg-active.jb-pro-ptz-bg-active-right-down{transform:rotate(45deg)}.jb-pro-container .jb-pro-ptz-bg-active.jb-pro-ptz-bg-active-show{visibility:visible;opacity:1}.jb-pro-container .jb-pro-ptz-control{position:absolute;left:53px;top:53px;width:50px;height:50px;background:#fff;border-radius:50%;transition:left .3s,top .3s}.jb-pro-container .jb-pro-ptz-control.jb-pro-ptz-control-left{left:33px}.jb-pro-container .jb-pro-ptz-control.jb-pro-ptz-control-up{top:33px}.jb-pro-container .jb-pro-ptz-control.jb-pro-ptz-control-right{left:73px}.jb-pro-container .jb-pro-ptz-control.jb-pro-ptz-control-down{top:73px}.jb-pro-container .jb-pro-ptz-control.jb-pro-ptz-control-left-up{top:39px;left:39px}.jb-pro-container .jb-pro-ptz-control.jb-pro-ptz-control-left-down{left:39px;top:67px}.jb-pro-container .jb-pro-ptz-control.jb-pro-ptz-control-right-up{top:39px;left:67px}.jb-pro-container .jb-pro-ptz-control.jb-pro-ptz-control-right-down{top:67px;left:67px}.jb-pro-container .jb-pro-ptz-icon{position:relative}.jb-pro-container .jb-pro-ptz-icon:hover .icon-title-tips{visibility:visible;opacity:1}.jb-pro-container .jb-pro-ptz-btns{display:block;position:absolute;left:0;top:156px;width:156px;box-sizing:border-box;padding:0 30px}.jb-pro-container .jb-pro-ptz-btns .jb-pro-ptz-btn{display:flex;justify-content:space-between}.jb-pro-container .jb-pro-ptz-expand .jb-pro-ptz-expand-icon{display:inline-block;width:28px;height:28px;cursor:pointer;background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAAANlBMVEVfX19fX19fX19fX19fX19fX19fX19HcExfX19fX19fX1/////Pz8+oqKjCwsKhoaHn5+eWlpaOqTaDAAAAC3RSTlP/3CaKxwJiAELtp4ri/s4AAACuSURBVCjPfZPREoUgCERXBdPRyv7/Z6/Z1aQp9oWJMyYLiKUrOIpAJBdGCldgbzBkPM/QEoTI3jBEPBRDhwEvChe08Q1Ge0ImvIq4Qj8ljrLdH77CyQPWlCdHC0Q1e9rmmuC+oQN9Q4LwcQg40L6eyqm0uEpXSUqe3fKpkkqL+Y/o+07SrahNEO0T0LBsvOitf4xsLqiNTB32wtqaVKosGLO2mhUrS93+PZ4D99wPqzMJVcbEyA8AAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-ptz-expand:hover .jb-pro-ptz-expand-icon{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAAAM1BMVEVHcEyZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZn////i4uLZ2dnIyMjExMS8vLy+iXNeAAAACnRSTlMAYomLxwEm9+NCLo6DKwAAALNJREFUKM99k9kWgyAMRIMmEMLm/39tKaVKFJkXl3sYJ4sAXeQ3ZOcYd0+gRYblFBuFLYoS2ot5lpvYn8zJQ65TO2GVNmdCmQq/qczw4gjpejD14BgmhziEIvCjVRlPioftHW6A7xBB1a8CCUMvsuSqEkPM7eZX6h8GrQ67bYpNIbRL6rb4/k2EfVXKsgmqfQrW9qnGq96a28jGQG1ky2HXpVysyYyeDIhWq7le6ua9P36HD6+2GRi8iBZBAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-ptz-narrow .jb-pro-ptz-narrow-icon{display:inline-block;width:28px;height:28px;background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAAAM1BMVEVHcExfX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX1/9/f2/v7/y8vLUObqxAAAADXRSTlMA3IrE6SZi9wI+y0gNXAn3CgAAAI5JREFUKM+Fk1kOwyAMBQ04bJHT3P+0JVUMNMWv8zvSk1cipfjAKXHwhR7k6KTjYp7dVuWLug1XWB5wz96T/JD2O3Phmv0k5ypL6lVVFIPYpLOka5WKSSFvS0/BloHYlkza5HkMzrvVLo8ZlRr7mtFYWBBsBQ4BjC//GTxcGVw2PpOVHQ6fJj7qS4936OoN2K4e5yE6N1UAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%;cursor:pointer}.jb-pro-container .jb-pro-ptz-narrow:hover .jb-pro-ptz-narrow-icon{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcBAMAAACAI8KnAAAAJ1BMVEVHcEyZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZn+/v7X19ckk9ihAAAACnRSTlMA9+NCAsuKJsRiPv/2GwAAAJlJREFUGNNjYAAC5gxFoTYDBijw1FoFBIumQHjsUavAYGkBmGu0CgqUwRqlYNyFIO2Fq+BAnIGBJQrBXerAwLkKCUxgYELmKjBYIXMXM2Qhc5cxdCFzVzBoIXMXMYAcsRsMdgEdgs4FKT4DBqdAitGMQrMIzRkojlRB9wKaB9G8z+CMGjgshjCuMCjoWNxRAxYt2KGRYgJiAQAnZcjElaB/xwAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-ptz-aperture-far .jb-pro-ptz-aperture-icon{display:inline-block;width:28px;height:28px;background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAAASFBMVEVHcExfX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX1////9fX1+kpKRzc3ODg4PFxcW1tbXW1tbk5OT29vaVlZVmZmZ8vCMFAAAADHRSTlMAxGJ5Mssm9+NCiYtiH91SAAABAklEQVQoz41T7Q6DIAyEJQooUL55/zddhVazzZjdHyqXXo8DhWCYTWqltNyN+MZLuxP69UGti/vAsl6c0e4L+tQ2yv1AEbvecMhO5cXdYhk+6aO3WGrNAMwentlMz/ZAKIlNoRsqY2wtFWu9t8wasc0iYVN0LkQfrG1zbxNyrIBcntOQrH1Ukkb60QcxYF1xMA2dh8zWj6ZDsLCsIrL4Ds5Hm9FMbCEROWUB0COaLXEIZJKV7CKybGO7UuxjxY2C/TkMbxboKBQCxgMN6MCJQ6Ch/QjOZg/B13LGx8FDTe3IFvl+Bc9XBi3UWoex68qeL/vxmdyxyvz3NJ8f9dDef36HN7koIK2LjxB0AAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%;cursor:pointer}.jb-pro-container .jb-pro-ptz-aperture-far:hover .jb-pro-ptz-aperture-icon{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAAAOVBMVEVHcEyZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZn+/v6cnJzr6+u/v7+xsbGlpaXNzc3b29vqh7uRAAAAC3RSTlMAyRjKA59J/3PzPhe1wxwAAAD2SURBVCjPjZPrssMgCIR1mkYtisD7P+zBCyZpM5mzv5hs0M8VnZvaok9BlXzc3FXbO5z0vtifFC5Kn8OL4UfxwVvuHm61d5Z0b6ZGZZwZpQAUosWsjVZntVS1sH3ZFo1IRVYfGXgx+VGwNkkIVbhq9/jm3cAhaNv1Uk3IA8mNn7D3kbQeWK3TLH2jCthrDFcTMwUWaKiClc9mJtJWhS3SF5BpJqMQW1b3xwnkDahMoHYomkeJRgSENA/MFsKML7fgoCBVbGvM+Cx4JcKWbWHKK/h1ZYS1Jy/nK3u8bB3KhzG5deMxtfv3aO7/Heq+9ms8h9fxHP4AHzAWU9zlWNgAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-ptz-aperture-near .jb-pro-ptz-aperture-icon{display:inline-block;width:28px;height:28px;background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAAAQlBMVEVHcExfX19fX19fX19fX19fX19fX19fX19fX19fX19fX1////9fX1+FhYWbm5vz8/Nzc3OwsLDi4uLDw8PW1tZmZmYgm6a+AAAAC3RSTlMAYmOLx4kn9+NCIVJiPGAAAAD+SURBVCjPjVPttoMgDMOJAqOU8uH7v+qKFN2c597lF5LTJg1VqQG3aGuM1bNTV0wWDtjpg3pq+IB+npyzcIE9ejsDXzDCrjccs+tOariF3n2OLyw5xko0vh9MDjNb9Q0hp2GK3cixlIApe4/JD9appR8SFxWAUFLg6n63iB1irnY1Jv0mlrok7nUdcZRa1YeshxBA9iijChlxI6iZEaBgSEL2tkRcymPGGJpqlbZ6uDg0WR/F0DwuMpxDkYwiIXA8hO2uMJdGCCK6teB8RQoY8xGfevQjxYQt25qoRwDT25MRBjZ7GtP/P/afa3LHmrflXa+ruf661Hvv+et3eAF6Fh3v+sSUGgAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%;cursor:pointer}.jb-pro-container .jb-pro-ptz-aperture-near:hover .jb-pro-ptz-aperture-icon{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAAAM1BMVEWZmZmZmZmZmZmZmZmZmZmZmZmZmZlHcEyZmZmZmZn///+qqqq9vb3z8/PMzMzo6Oja2tpXGg+mAAAACnRSTlP/JomLxwJiAONCr+rW2wAAAOtJREFUKM99U9sWhCAInEpLBS///7WLEWy7p9O8qEzCMBIOQ15DAlLYsoegS9yFMKQ93skl4Adh+ZI54Q8pG5nxgKzkgkcsk4zhmQxRyN1OPHqtncjOu5AuppcJ6s1EHTA1YzC3Wgq3YmzGqpsmlwZAo7F8oLEVKoeE6+TbSxK0JJ/3FLOwFnUxzXuoltYDDMLoAlmYXLAWIrkqbdZKs+q4KBfkNV1uwGaBim9TdLWS3R7iGRvCNTPB7JvGlc5EXK8cKbrxooint73RzXh7Msl6Oj/uT/b62O9j8sj6gMXX0Xwf6jP3Zr9DtNAHTYMMXrXSK0YAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-ptz-focus-far .jb-pro-ptz-focus-icon{display:inline-block;width:28px;height:28px;background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAAAPFBMVEVHcExfX19fX19fX19fX19fX19fX19fX19fX19fX19fX1////92dnbs7OyFhYWjo6Pe3t7Ly8uxsbG8vLyG+Q0EAAAAC3RSTlMAiWJjx9wm/0Lti7mfpe0AAADaSURBVCjPfVMJDoQgDERFC/bg8P9/XUSO6CqTqA0TptNDpSrMpC2A1btRT8wrNKzzjdo03KC3zhkLD9imbeAFhd3sG2kvZQ2v0NknfGBJZKkhBM9MxOxDKBV1N4iHi0TRHYjN01Qi7/kK2PtyNDU7DAEJgDAAN0u1jsQEFEkcVVmrqjeXrkWRmC67eqbgG7bJyvkQSQkvUvec7szpek6t9ubWJSK/uJVSm+APzHKCh++DWWuH4plQKNYOpfappcjy2VvJn9744cjGwx6uyXjBxqs5Xuqsvf/9Dj8rLhRg+bQ5VAAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%;cursor:pointer}.jb-pro-container .jb-pro-ptz-focus-far:hover .jb-pro-ptz-focus-icon{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAAAOVBMVEWZmZmZmZmZmZmZmZmZmZmZmZmZmZlHcEyZmZmZmZmZmZn///+xsbGoqKjt7e309PTExMTQ0NDe3t774OlGAAAAC3RSTlP/itxixwImAELtp8B2gZgAAADmSURBVCjPjZMLjsUgCEVpq60G8Lf/xQ62gvNeOmZuUiWeKHC1cKnC5iJAdFuwJXgmf+xg2g//G54OPuTOCUOEL8WgMMCLwgPP+Abj2aF38CrnBR7whw6Bo4fWUk7MMrQ2OrpAq0GspTLLgKg1wTailNITZA0EaTkZGjIAY5NwlATah5CGRMJYj50tFtlWiapsLvAPRdtL/WOmET7QzZyl5ywzp7NWsjBJ1odsragJqeJ9HGFNZoLaJw71hMTm0O7NeDE1Z6YsU5rGL69sedmXXz0ToW8PzA/oV09T8OJR32fb7+B17Qe3WwtC9PVbHAAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-ptz-focus-near .jb-pro-ptz-focus-icon{display:inline-block;width:28px;height:28px;background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAAARVBMVEVHcExfX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX1/////t7e2dnZ3W1tbGxsa3t7eDg4Oqqqri4uKTk5NImu/5AAAADXRSTlMAYieJ3MvE/0Lti4oh87zNagAAAOtJREFUKM+NU1sOwyAMY30FtoWS8Lj/UZe2gWpVh2aJH1wcO0mNqbDj4gDc8rLmiscEDdPji3rP8IX5fXLWwQWuaVu4gbKDuyPdsJMz3GLefcIPbJ6PDCEAFDlUAJiORM3NigQFAXAFlqOeRhWJyFFIHxNGvRrN0mp470U++3axGM2RAmXcXqKnkDSN0a9WIk5Sa01MpDXBQAdVtrA8lBhFnnKpsmoo5VBrhszV0KuJ5N2tP92O50iQjpzcctravoihdoi0Q1NrfN56m0VWzFBoje+OrD/s7pr0F0yUr6s5/LvUu/bz+B2ep+IHdMIV2SUZfCsAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%;cursor:pointer}.jb-pro-container .jb-pro-ptz-focus-near:hover .jb-pro-ptz-focus-icon{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAAAQlBMVEVHcEyZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZn////c3Nz09PTp6enR0dHFxcW7u7uwsLAUKT0cAAAADXRSTlMA3IrE6WIm9wI+y0gNQZpqdwAAAOdJREFUKM99U9GShCAMQ8BF3Cmlpfz/r15dAe88l8zwQiQkoRrTEa3zIXhno7lhWxcYWNbtN/fa4Q/218VFDzf4of0O8A/h3TQfOGU/ytsOj9gPVyt8warkmYEQQAgABYDxTKROz88koS6AVIB1fRCNbSI1cVUy15Jq27LGjTtyzipPeWw40/IXQkrHyZSRmqw3LaQgctFNKYzYyGACfEXossLMojFEj7J0WfdwJ3dD9uY2X25tL0Hj45mTR87Y66u9IQFsDS1bL57o7JbUDNIofvpk08eej8kTe3Hz0ZwP9UFfv8OgfgBUByCEUZhYtAAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-ptz-cruise-play .jb-pro-ptz-focus-icon{display:inline-block;width:28px;height:28px;background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB0AAAAcCAYAAACdz7SqAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAJWSURBVHgBtZe/b9pAFMefbSYWMjCjdOtmYEYqXVC3umMlBFRiT7qxkexITcdKSDUSYna2iqX+E/DG6LCQgcETUiR+5Pv8I3Kc+McF5ysZ3x2++9x79/zuLFEG9Xq9s/1+r8my/AnVJq4z/2LZfB0Oh1tFUQxd1+208aQ0GAa7kCTpMgRJk4kJXk+nU5NEoe12W8PM/wrAotIx2Z+w3MkE7XQ6Q3S4otNlY5zPUZdL7wiMBUsRIK/fDeWvhQ92XS0HrQia83cCsqoIyMug8gQ9Ho/DpF7FYpEajQa9VTBoyIZxWeEfv6IndSqVSjQYDKhcLtNqtaLtdkuigmGSZVn/XKiqqr9wqyZ1YEtbrRZVKhX3zvX1ei0Eh7Ufa7Xan8C9VRIUg9lyQZfzO19VOOugkBpAgaXRtnq97oLZ5ZvNJm0YQkBZ8m63E7YyKl5ntrrf77vlJCF/qzLlKLZ4NBqRpmmuF2LBlLOyBFYBchDKlIeWyyWNx+PUtS2Qtx+eJA6i2WzmQtMEA+8KnA+73a6N+jkJil1pGAbN5/PMfRBIZsGn3+LFvSABMYiBgpnJgZEeFHQD4EzQrOsWI4N/nrY2uPg/eeefV8WvAKfALOsWJ3jzA++rcqjhB25OXAd244nA62AjV4LGxWLhIBk/oPiF8pc9mUy+BZVnyQEzueEZUb5yjyvhBiX6BCw2YTGvdZNyAkYPZsprT/rgO/K2vDcdQQH7jes7gPcv/kvqyCcKbEVX6PxVAG76QWPGPZAIDcEZqGECTQyokpe9wp8VfNqzyA2L9M+KRzm19l1i6ZQBAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%;cursor:pointer}.jb-pro-container .jb-pro-ptz-cruise-play:hover .jb-pro-ptz-focus-icon{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB0AAAAdCAYAAABWk2cPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAJ5SURBVHgBtZe7buJAFIYPjsVFAok3wBUlYcUDLNvQISUSDVVCA0IUYZ8gmzcgBeIiJLwdFau8QToaQKygovI20CJBARKX/Y/jICPAF3B+yRpjZvzNOWfOzLGLLKrZbAaXy+WdKIq32+1WcrlcQe0vZb1e/8XvQT6ff7fyLpdZh0qlEr+5uXne7XZxMpciCIKMSfzGBBSyCwVMAqxpEXYEhzdeAJYtQ2u12h2aJq4gXadSLpf7aQptNBoPm81GJofEsc5ms9/0zwT9j3K5HHUSyEJ4ovV6vXkSyjHEyvxDXyCAHxGy4hEUq+4JjWQ02O/30xV65rTbQ9lKNEWjEYFAgFKpFMViMbpQnOfFPRSKWxnldrtVaDqdpnA4THalefMDinx8sDOYrY7H45RIJOy6PMibjch3F24AJEmSeo3HY+p2u7RYLEzHwNqogOUcpSvFrk4mk5Zcjry9FZCX1+46qj5dzvEOhUKGfQVyWAyPRCLqojsnEYtohpiSE5rP59TpdEhRFMN+Itw7Q3DpGq1WKxqNRurF90YC75+gnXszulBsVbvdpl6vZwpkwasDNWVw9r3BWlu5Op1OVdBkMrEzTD11RO1exmUJytb0+30aDodkV7DyjT2rBpNrG8zg3WwQW9ZqtS4CatASt/sVhAC/GA3glWk1bqcEo+TPwm0P5QeYySt9jRS9UQe54vF4fqEZkMNCdXivrw4PoJlMZoad5IeDYE7F+0KhcPC+syVotVotIQ5PdLm4CD8CGkJZfAIhzlw3SWRdM+T9q9frLbHnTnUwrfBZfPCiecQG8v3MBHj/HvAm4/P55HMwW1C9tG8a/RmsGH1CnNJ/17UakVMOx7kAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-ptz-cruise-pause .jb-pro-ptz-focus-icon{display:inline-block;width:28px;height:28px;background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB0AAAAcCAYAAACdz7SqAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAHySURBVHgBxZc7bsJAEIbHdprQJAU1ckln3FM4J4jTIkSMRB9uQDhBlBPECIk2yQ1MSeeIxg0KZQoKKpSCR/4xtsXTNmA7v2ThHe/ut7M7O7sIFEOGYdzO5/OSJEn3KGp4ZDy33ucxP6vVqo86H71ez47qT4iCLZfLJ0EQmhuQKNlo89rtdk06FVqtVjV49kZrr86RicG2TdMcx4LWarUWGjzT5Rqjn7tdsJAi8Ch4C4op1TGl75S8GKwCPOWC6FsRNDKAL5SOZER30HcAhbFFEUFTLBapXC5v2fL5/J7tiOCXofHLlVfikRhRrSqVChUKBXIchyaTiWtrNBruYLjM9jB5jlmup4vFQqMYyuVydKE03vsuVBTFR8pIcFD317REGQlRXBJ5PSl+iktCskgZC57eZA5l/Q/Uy4lTykjYq1++p5EHb1LC9rRdKA7dT8pO64yERG/SmVM8m81OqW7yckr8Ztv2r6Io1whnLawF59fhcLiVY0ejkWsfDAYUJfRfB+snOE85J2KRvym9RGF2Op06vwRbhg9YHgmlIz7E235B2vwC1x1VVdl7jZLT3nVF2q0BsJUg+ODFTDpU0wP3PfC5a2wB+BD7CuqLgws/TQQYn7cyxYfxfdc6ViEUujMAHUlER4cKHfhbATvPjBUG8/UH1xXJDxHoYGQAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%;cursor:pointer}.jb-pro-container .jb-pro-ptz-cruise-pause:hover .jb-pro-ptz-focus-icon{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB0AAAAcCAYAAACdz7SqAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAH/SURBVHgBxVY7bsJAEJ01CAkJKUhUULmiDSnpoKYINyBuQIgi3AA4QZQKIQq4QaChhhuQCxAXSERUSDSIb944axSS+CPA5kn27o7X+2Z2dz6CXKDT6URXq1UqGAw+Hg6HDB4V4qj8rAsh9N1uN9rv971KpTJ2Wk84kW2322csVv1BYr+gEGMo8Foul7uWc6w+NJvNjKIoHXRVOgMg74K8AXLdFWm73a7BujpdDh3rZH8TCw8JLYlPSFutVh7NG10feigUetA0bcEDxZTiDFU0L+QN1M1mc1z7SBoIBGrkcGkSiQQlk8kTWSQS+SP7D3CzJ76c3A/yi61kodOP6XSaYrEYzWYzWi6XhiybzVI8HjfGLLeDNGxoWpohF8C50CXgwMK+r0gNCuQTENnyitQgRT4BQSOlyFvrKsRdCapC/uPuFqR0G1IZExfkH94NSzkHkk+Ap4wNUiTqPvmH74gUDoe7dOYWr9dr13M5sfNxBnjQ7/dXuVwuDGHG7ieOr9Pp9CTGzudzQz6ZTMgJ2FFtMBh8HvMpx0Ro/UEeBQq2slgsatw/uoxMsBp5A64WG+bgxE9LpVIPpUWDrgv7csUE4nEdlWCNPCC0JJXEF5Wg8MchHs11CWpCVvZVkBfckksyrneHVnMEuQRXitiqPBS4lwpEJYmORkc7Qju0IzPxBZ2t+3mW/JtqAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-ptz-fog-open .jb-pro-ptz-focus-icon{display:inline-block;width:28px;height:28px;background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB0AAAAdCAYAAABWk2cPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAKgSURBVHgBtZe/jtpAEMYH28WJioIa+URDF8MTkAalO6cH4YIiFbk8QXS8QO6oUlCAkGjDtTRHJBoa4nQ0KBZdJApoEAV/Mp9jkAF7bXPcJ5ndtdf725nZWdYxiijDMBKr1SqhKIqKNpdms9mcRxkjFqZTsVjMy7J8x1WdL9WjC6D97Xb73G63m0HjxULAvnI1T+FlMfxBBPeEwoW73Q6we7pcViwWe8+utwKhDFQZ+IOrGr1eAH9ksOkLdSz8Rd5xu1RzBmfdFkvupwz8dmUgBENeYNAZtFQqGVwY9DZSnTVi6+Decrn8h0JYWSgUKJfLHdrL5ZKm0ykNBgOazWbCd9nNt3CzgoZjpUohlEqlKJPJHN3DJHRdp16vR51Ox/ddTiVkw70NlSTp7rRDMpmkWq1G4/HYvmANSpHghXg8To1Gw/M5W1oGVEZD07TvXNy4O8Btw+GQFouFbV06nabRaGRbhbaf8AzvTiYTr8c32Wz2p8KrSuMgJ7x6IEa4AIuivau9tNlsNGm9XqsBY9gxhOvCCi728wa7OCHhRzQAZl2pVGyXRZEoBAoFCDGs1+v2QooiUfpIHE+LBEL+VatVezVfDcp/whYJhAWBVHFvCEFCfz8op6cpORuxJRokajz98tSRae+97OJnUS/RanQLkwNQ4FrzsA2yyU0Gf/br2e12A90LlwYAkS5Pdrm/wRs+/rh1ChAshuV7wTqAQoTAarVat6gorll8YWvzXBXmbdTUcY3/sK/L+4ppmnPeF/9SCGsvAXIsH8+gDthkMFyepyuJgS0GHh3w5NNOzO0zeMHVD/R64BMDP53d93vBORW+0GVnpvmpS0NBXXDDSacwR9K5kxaPok+NUJ8VDlzlQucJvKNj63G2/U3/E78fZqx/rk0w4ggu8jUAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%;cursor:pointer}.jb-pro-container .jb-pro-ptz-fog-open:hover .jb-pro-ptz-focus-icon{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB0AAAAdCAYAAABWk2cPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAK2SURBVHgBtVa9jtpAEB58/EqArqCBBqehorgTVDQxPVJOiD5QAEJUeYLjniBcgRACKeQJQsQDBBoqkIigookraC1BwT+ZcQBBsNd7Ovgk2LV3vd/M7PyZgBPlclnc7XZPFovFv91uRZPJdL9fktfr9W987udyuRbPWSajDUgm3d3dPSOhBMaQUaAXu93eSKVSCryVtFQqPaBWXznJNMlR8zo3aa1W+7zZbIo4vYd3QBCEQjqdfjEkrVarzyhlAa4EvOt6JpNJ6ZJWKpUnHH7A9VHMZrNfLkjJO9Ecv3Aqwg2A1osevFs4vCQP5SF0Op3g9XrPflar1egzut9vCNVHzPS3j8EkcCAcDkMgELh4PxqNoNvtwmw20/tUXC6XSRyLqqZ7LS80CgaDqiY8IEESiQSIoqi7B53qE40qqVYsolTgdrshEokAeh9IkgRGIDPTPhJYC8SD4eg3k52RQNQi7XQ66txms+kepEUcjUah2Wxqrq9Wq6h5Pp8/4CUzD1osFuqPFwfnIsE1IBKbyDqANIzFYhCPx+EtYPmCGQxAppJlGQaDAVwLaFlBgRtAx7T/SNGNZWCg1WqpoeP3+4EX0+kUJpOJNiEqKWA49HGusA6gwPd4PMCLXq+nu4bpsK+6LVV9YMDlcqnkPKD7JyF1oFD+PSSHV9ZB4/EYQqEQGGE4HEK73dZdR+Ua6nh4gWXtD3AkfCL3+XzHZ4pfyrekIQnHApr2A2oqn4YM1TvDWkr3xbozPVAxJ0KaH1MRFllSvQG3gYztz7FtOct/mLqorZDhulD2BVzWJKW2kTZckVjBnviM8IKUQBtQ40ck/w7vAIUhnvGYz+cvwpHZbGNHkcQMwtXGnICs9YrCF/Q2GHb4e3IJBxLgo44ACsY6afbT4XDUWd09N+kpqOhTDT55Jf9/Z0b4C/UJLQCcLGi1AAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-ptz-fog-close .jb-pro-ptz-focus-icon{display:inline-block;width:28px;height:28px;background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB0AAAAcCAYAAACdz7SqAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAIjSURBVHgBtZe9UsJAEMc3l1Q0WvAA2NEp1MyInVbGnoE8AVrSqW+gT2CY4QGwpTEFNcaOEqksKKiYcYYP/xsSJoRwx8f5n7nRu9u73+7lbu8waAc5jnM6m81sIcQlqhcoOZTTsHvAZT6fv5um2XZdd6Caz1DBMNm9YRgPMYhKHhx8brVaHu0LrVQqNjx/2wOWlAtnn9MiT4VWq9VHDHii4zXAPFdJsPGPwK1gIwHk7/dC+sXgAsBjroioFZsmpznCuHKLxeIxqqwirdVqvGmcbaOy2SyVSqW1tuFwGJTRaES7CEGd8TJbXOEo4YkjG8BQ27ZT+7rdLrXbbSUcx4+P3oOIVQ4Wr0Cj0aBMJiO1Q6Q1PvsirFzSkeKVqNfrKjM+8xeCybRMbUcrn88HRSasqs2R5kijisWitB+reiKm0+mhaS5VvMwqCdIs1WYKoJZljUmj+NwqobS8D7Wp1+tJ+5EPvkWYD33SIE4S/X5faoOHgCdC+jsdKc5GnJUUGiNIzwrpbjwh76tOpxMAJ5OJyjTwKp7wP/CnvM06LeFzdPwNd4AFihL+Chom/U86/HmiAr4CGOR4M2r0fX9cKBR+8e816deg2WzeRJW15ABPXvgxRXoVPFfiDWbSAhF7iJiXvUyagMmHmZlmGYK/aXn7HPSNAWui3AH4s9EnG8ibiy94DL6l3W8jL3zvetsMpNCEAw4cKGPCc9r8WeGj/YuBMlikP+yn3EGZYjlWAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%;cursor:pointer}.jb-pro-container .jb-pro-ptz-fog-close:hover .jb-pro-ptz-focus-icon{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB0AAAAcCAYAAACdz7SqAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAI5SURBVHgBxZZLjtpAEIbLjUFih8QFnBUrJCIOEHOCROIACStALBhOMMwJMlkgBCyYnCDMCUJWrJCQYB+zBwkJFiBe+QvZyEz86IEezSc1dj/w76pyV5dGEnS73cR6vf4SiUQ+HY9HE0MJu5GmaSNcFrvd7hn3vXK5bIU9TwsTw8Oqh8PhzhGRoIf1tSBxX9F2u12FVfVXiL3ksVgs1khWtNVqfcfljm7HgtW5l1Zrbyh4Fo7FYh8LhcLCGRDu2U6nc69YkDG22+0v98DZ0mazaQgh/tLbUUOMHy9E8eH8treDJ8lkkgzDuBibz+c0m81otVqRBAu4+QO7WecerDSDBB3RbDbrOTcej2kwGFAIvNc5dPVTTLHpv9INpNNpyufzBEsC1yF81dOVf8KslCHIEy4SjUYjIxDLDDoGKYAtDrNW13VT7Pf7azOOJ6lUKnAeySLB7jVIIezmMAS9A/igxIIUwns3VBQ+HpFCLMsKnGcjhX0CKLF2MpnQcrkMXIPzuX+KKax9phthtw6Hw7BlVqVSGel25wntqqy02WxOFnLj+xB6/HNTwmdXTqdTGTHmfKA7lhKSxAOCbPr9g90n82X6AcGfTgVxUTngtKlD+J4UAw/2S6VSzulfJAe8SZ3fiNRiQbTgHvgvI0H4Gxb9IAVwTexVmHmmQbiCD1suHy26En7xaDSa86p/A4ttrpuIT3ohpLcTxw/tAWJ9vzUaSWCLm3DXZzS+z7hELFzYjX/i8fiTu9T04x9LgQk+PbvDKQAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-ptz-wiper-open .jb-pro-ptz-focus-icon{display:inline-block;width:28px;height:28px;background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAALxSURBVHgBtVYrrBpBFJ0FFAqBqCIrUXRBk5QaUleewBBIlwSDoW1CUlzz0CSlaJJHA8FgqMBg4CWIKkpRuEfQCDAYAvSeYYfsLgss6fYkk5ndmXvP/c1HYjaQTqdjbrf7PQ0VarLWgBW1Cdput3tutVrdW7qka5OZTEZ1uVwfaBhj9jDf7/ePzWazwe4hVFVVPhwOT3cQnRFLkvS20WjMzRMuCzKFyH7/AxkAg18oQp/ME24LsgENfcwBkJfvQqHQejqd/jr905HJGpnMHIYW3iHGp5AS2df/QabpfiKHfCdCVCN1qh1hv9/PIpEIi0ajLBgMskAgcDZfKBTMYjJVL8+nh7MeS/8ioFiQbDYbtlgseA/lXq+Xt9lsxrrdLkulUmw8Hp/poLB+JC+rkpa7FysiKEokEpyo3+/zBiIrr2EU1kKmWCxariMvsy46IRJWZFBSLpf5GApgvZUSYLlcstFoxMfwHnKQN4Mi+cYdDoe/IGpmslKpxJV0Oh223W7ZLcTjcb6uVqtxLxFayJtkfSga2Sycy+X4YnhlBzAQhGI9euTRqnhAaNjkKA5Unl0yALlD0SC0Au12m/fIvx5nRxsEYV2lUrHMgxmiYMwGwmiE1qzDrSiKSv0r8QMxF2Wdz+fZer3mhXAJCP9gMOCGAiBJJpM8h6jqXq9nWO+hLfGH9ohiVoTFIEbxwFqECMoQInwLy4V3mEMe0SB7YWtMJJzoVK7fLnkgKg65BVBM8Bj5gjHwDORoGNfrdUMu9SDnfl7d+HqAEArFCYNQYoxtAODfJSIBimTWpV2Swxt8PLwiRAgjGrwXuEVGmBNXw6MxP5KXMWYTCKs4U+2COH6g59tCu6uG7A7AYxteCcypVU+EmgVZbcJxkO4HcmplIEQuMcGOTz8nyT6T7on4Npw0mMBzgDnkqUZWNfyzWujQM/FB79lVQh2xeudbZ0VE36mvipzdRagjjtFtnSBlr9nxuS9uGCidk1HPdFp1xcvsGv4CcbeEIeSIw9MAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%;cursor:pointer}.jb-pro-container .jb-pro-ptz-wiper-open:hover .jb-pro-ptz-focus-icon{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAM0SURBVHgBtVa/SyNREJ7dBLVQELU4bdwrtFJOUCxsXEG0Ue4OK6tLKhEb/QsO/4LzChVtvGvE0sPOxtiYJuFyJBBICtOlSSAQCEnIj5tvyC676ybZYPLB4y373ptv5puZt6uQB1xeXup+v/9zs9lc4qHxK621VFAUJYbRaDSeDw4O7rvZUroQBXw+3zcm0ckbMkx8enh4+It6IWQijYlueiByI95g4oxzQXW+OD8/X1JV9e87yACNbbxeXV0dOxcUJxnn6okfx6l/OOHcnr0hhIzsFcg06jNa8obwbErKOfs+CDIhUdUbxrhJiGrknAW8HB4bGyNN02h+fp6mp6dpcnLStj46OkpbW1vOY1q5XJZ8iqTX19dPnYpkZmZGSObm5qharVI+n5cZ5ENDQzKy2SxFIhFaW1ujTCZDqVTKaabA+z4qrdy9uhHB0PLyskSTSCRkVCoV16gRLfYODw/T7e2tOOQE5zKocmRf3Mggzd7eHvEtQnd3dxSNRl3JgGKxaEaE6HEO553gOllXuQ3W3ch2d3fFyMvLS1siKxYXF0XWh4cHOYfzUMgKpE1t3Y026LouhxCVF8DBhYUFcz9m5HF7e9u5VUOV2pp8dnaWpqamPJMBKysrEh2kNRAOh2VG/q14c7XhILzb3993zYMT2IOCcTo4MTEhkqKgrPCx1gGePxgv6vW6EKJYNjc3qVQqSSG0A+RPJpPiKACS1dVVaQ9UdSwWs+33c6n+47ZYchqKx+NCvLOzI80NiWAMEkFyeM75lx5FdFhD4SCXIEJluxRbTLm4uDjmcv3RLgIYgrfILZBOpymXy0m+UImIDLLCATyHQiFbLq1gB/90bHwrcNPAILwGAaREcz8+Pso6CNsRGZDGx0eS8xXqRgh5DYkgIwZuFQPdyGACfwJ+PHGhnHKUOnkEetS4U72Co/uNWdoC3yovUVqBiD1EZW4fGRk5MwkBjjKIBRoAarXa12AwWLARIpdY4McC9RcnR0dHZjPabhosMOkG9S9S2/8MMLDfRKhljawjoYU4wNXby79OgavxJwrEyFlPhBZinasYH+pPPOMaNL4wMJphJZ553Bt/Zp3wHwTYnvHjbDCuAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-ptz-wiper-close .jb-pro-ptz-focus-icon{display:inline-block;width:28px;height:28px;background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB0AAAAcCAYAAACdz7SqAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAANOSURBVHgBtVc9TCJBFB52r7jYyCXWnhcbO1aru8RErjGXWBxX2BijGG1svNhJo56VhQU2ViQsgdDQYEFDc5rQ0BDoaMgRGhoKaMhdCHDve+6QZVhgMfglm9mdn/e9v3kz6xEusL+/79c0bcvj8fjpc8V6gCY9VXqK3W73MZFIpN3I80wj03X9il79wh2q/X7fJAXvTdNszkQaDAZXaHF0BrIRcvLKERE/OQ3qYwh/06shXg8vRPl8PlEqlZ4nktoIV8QcgBxwItbfitBOvL6+XioWi2XZp8kXIryaN6FNdpSM8spvthRWUmPaJ25vb4vV1VVRqVSGBCwtLYm1tTUeW1hY4KfVak2hFe+J+B+5+QkfnL2Hh4fI1KAq/OLiQlxeXorl5WWxsbEhNjc3RbvdFrVajVvMkcTlclmk02nRaDTGETfJ1Z+wlTxWLP84zQoEAkwEodlslh+QqZDWY36hUGByp3m9Xu88Ho+HNaokfidCCAIh2kgkMlYQAOtyuZy4vb1lBW9ubnidCioa37klkw0nQrgWgpLJJMfXDUAOBbEO66GAAkOS+tSRk5MTXgjr4FIAMXULrIObz87O1CHv8fHxR03thXAkDhbahezt7TlpzkA8sc7uUngIQIhUgNRr70AWQsu7u7uBEPTBdaqboQTcCIUgXL4DUBzjTrHVDcP4BmVlR6fTYVLg9PSU9yC2CIghEG7HHIC2GluJOGYyGR6DYiDCGEKD/iErNS2sU238QnH9rGqDopDP5zm+EIJ3aL6zs8NEUAKCQSiVwxp4BFaHQqGRwkJoRqPRkEZ7tCjGAAJQHED28PDAVkirsX1kUcAc7FEXWc5c7+iQTltnpyMgHNYgUSSZBL5BhqTBHkVMAZnxKsijMbQ6Vf+/FFe/mFLs6/X6IJZ20t3dXXb34uIihwFuTqVSI3Mt0nPia3LtpVLot461V0FWLiiBZwzMWCx2xOSy5+DgIEya/BRvA1xfvlKxr+JjUBwola/Fy81u7iDCX5JwiJSPHNJm3sQWoTnUp06a57XFIrxW+0dug8guutM8EvEHpxPIJRDDH6qFA2UmrSSrg0SO5HJLjhDdUxue+bLtQG7QqR8ggVvC4beCFHumRMSxVJxEJvEfnFm91YrgD/sAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%;cursor:pointer}.jb-pro-container .jb-pro-ptz-wiper-close:hover .jb-pro-ptz-focus-icon{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB0AAAAcCAYAAACdz7SqAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAANwSURBVHgBtZa/SyNBFMffboJaKAS10SZ7hXqFegGbCyjmUPTAH3eHlZWmULG6u/Kqi/9BLEQRIdoKigf2l1Q2ChGtBLmtFAQloPgL1HvfRyZuJhuzBvOFzezOj/eZee/NZAzyoMXFxYjf7+99enqK8GNxlZVtyhiGYXOZ5vo/MzMzW17sGaVgPp/vN2DkTbZpmqs8wfloNJqh10AZZjEs8QpYAfzx8TE6Ozub9AQFkGf7l55dWLbYTmxqamruRehbAl8CG5UEOvTNmWSmekHCVAgIJVgB9eHHD1bJSTPp7NXe3k68Hejg4CBvdF1dHTU0NFBVVRVdXl7S/f09nZ+fl2BS4Pb29geXMXyIe5eXlxM6FMaHh4dpY2ODGhsbybIsamlpyUFQog/geE5PT2l3d5eurq6KgTPc7x22kpGN5T+3Xl1dXdTa2ipGDw8P5bm7uyvoB3hTU5P0t22b9vb2ZFIu+smxjSOmEbfW2tpaAcJgMpkUQ25ACG4+Ojqi7e1tqq6uprGxMRmvi8P1BaXJLyE34MjIiBja2dmhzs5O8iLAMUGMw3h4yCkOobDYs+YHfXAkEpGBWJ1KJMTUqzAObh4cHNSbAisrK0FTrw0Gg5I4GKiEBAmHwwUzV2pubpZJOV0KD0EIkS6TlxxwViALMcvx8fGcEdQhKzs6OvIGYxLIcEwIxuFSvEP19fXSjpzQ5RsdHf3M5XtV8fDwIFDs0f7+frq+vpYtAjAMwu3oA/X09MgqEcf9/X1pQ/wBQhuyPZ1O56/SNOO+oaGhMAM+6rM5Ozuj4+NjiS+M4B2ZGQqFBHRyckLd3d2USqWor6+Pbm5uZAySqa2tjdbX1+VbU2Z6evoX3JumIoIBHA6ATUxMyCl1cXEhQOxD1ON9c3NT9qjufl2K5a+pqdliA4liHWEc7kOiwMXOvYpvwJA02KOILwS3FoGuoTTlWDKMJJUQ4qwfDmoyAwMDkkiYIBIOsS2iJH7k7MW1JPu3VpbUyYWVw91u4oWtcjyjOSi0tLQU54bvVBnh+vKJry82PnKHAydFDI1UATFwTgHzoIgtZvPW4Cxw1VlX0YtZFhjT6wvOXrgBK+ZnjcqXimHMrbHUZXuSV43kCpE3IUTzvPfjr75s61pYWAgx/Ctndy9/WlxaCsKPzZs+xc8Ww9IvwZT+A8hTw5fcMmXrAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-ptz-arrow{cursor:pointer;position:absolute;width:0;height:0}.jb-pro-container .jb-pro-ptz-arrow-up{left:71px;top:15px;border:7px solid transparent;border-bottom:10px solid #fff}.jb-pro-container .jb-pro-ptz-arrow-right{top:71px;right:15px;border:7px solid transparent;border-left:10px solid #fff}.jb-pro-container .jb-pro-ptz-arrow-left{left:15px;top:71px;border:7px solid transparent;border-right:10px solid #fff}.jb-pro-container .jb-pro-ptz-arrow-down{left:71px;bottom:15px;border:7px solid transparent;border-top:10px solid #fff}.jb-pro-container .jb-pro-ptz-arrow-left-up{transform:rotate(45deg);left:32px;top:33px;border:7px solid transparent;border-right:10px solid #fff}.jb-pro-container .jb-pro-ptz-arrow-right-up{transform:rotate(-45deg);right:32px;top:33px;border:7px solid transparent;border-left:10px solid #fff}.jb-pro-container .jb-pro-ptz-arrow-left-down{transform:rotate(45deg);left:32px;bottom:33px;border:7px solid transparent;border-top:10px solid #fff}.jb-pro-container .jb-pro-ptz-arrow-right-down{transform:rotate(-45deg);right:32px;bottom:33px;border:7px solid transparent;border-top:10px solid #fff}.jb-pro-container .jb-pro-loading-bg{display:none}.jb-pro-container .jb-pro-loading-bg,.jb-pro-container .jb-pro-poster{position:absolute;z-index:10;left:0;top:0;right:0;bottom:0;height:100%;width:100%;background-position:50%;background-repeat:no-repeat;background-size:contain;pointer-events:none}.jb-pro-container .jb-pro-play-big{position:absolute;display:none;height:100%;width:100%;z-index:1;background:rgba(0,0,0,.4)}.jb-pro-container .jb-pro-play-big:after{cursor:pointer;content:\"\";position:absolute;left:50%;top:50%;transform:translate(-50%,-50%);display:block;width:48px;height:48px;background-image:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAIVBMVEVHcEzMzMzMzMzNzc3MzMzPz8/Nzc3MzMzMzMzMzMzMzMzLVn6fAAAACnRSTlMA+duduRUwSGSD8toSsAAAAI9JREFUOMvV07ENgzAQhWG3lLSp6MwCViYIRSpG8AiM4FWMaPymDBKESMF/cQ0S136F353vnLuo3gp1kOYEoCXW4LFKIZAnqAXYICeASoAdzgG+cApwgF4EfwF+oDkCqIwA6gnyAKA8AaizQhsBAjzuqUHofInGIQbjRxXjMrTJuHDestR4Bng4eGrN0929PqNfzC6h06weAAAAAElFTkSuQmCC\");background-repeat:no-repeat;background-position:50%}.jb-pro-container .jb-pro-play-big:hover:after{background-image:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAJFBMVEVHcEz///////////////////////////////////////////8Uel1nAAAAC3RSTlMA+rbVFUecgC7rYhEEz+4AAACbSURBVDjL1dMhDsJAEIXhdeiGpKYWQVKDWc8ReojFc4ReAlPFFQazad7lIGkb0jK/WEPSsZ+YN5mZEP5UrZIP0vgE0Kv3oPlIJlAk8AJM4ASYwAkww2+ABfQg0ImgugPYsfNBVl99kA0AsjOArAPQpRTGHiBB8whxM0y+3skXNltsvMYriIWrpWPA80mFJ5qL3gAfB1/tcAt7rzdiV+HEgs1oBgAAAABJRU5ErkJggg==\")}.jb-pro-container .jb-pro-recording{display:none;position:absolute;box-sizing:border-box;left:50%;top:0;padding:0 3px;transform:translateX(-50%);justify-content:space-around;align-items:center;width:101px;height:20px;background:#000;opacity:1;border-radius:0 0 8px 8px;z-index:1}.jb-pro-container .jb-pro-recording .jb-pro-recording-red-point{width:8px;height:8px;background:#ff1f1f;border-radius:50%;animation:magentaPulse 1s linear infinite}.jb-pro-container .jb-pro-recording .jb-pro-recording-time{font-size:14px;font-weight:500;color:#ddd}.jb-pro-container .jb-pro-recording .jb-pro-recording-stop{height:100%}.jb-pro-container .jb-pro-recording .jb-pro-icon-recordStop{width:16px;height:16px;cursor:pointer}.jb-pro-container .jb-pro-zoom-controls{display:none;position:absolute;box-sizing:border-box;left:50%;top:0;padding:0 3px;transform:translateX(-50%);justify-content:space-around;align-items:center;width:156px;height:30px;background:#000;opacity:1;border-radius:0 0 8px 8px;z-index:1}.jb-pro-container .jb-pro-zoom-controls .jb-pro-icon{vertical-align:top}.jb-pro-container .jb-pro-zoom-controls .jb-pro-zoom-narrow{width:16px;height:16px;cursor:pointer}.jb-pro-container .jb-pro-zoom-controls .jb-pro-zoom-tips{font-size:14px;font-weight:500;color:#ddd}.jb-pro-container .jb-pro-zoom-controls .jb-pro-zoom-expand,.jb-pro-container .jb-pro-zoom-controls .jb-pro-zoom-stop2{width:16px;height:16px;cursor:pointer}.jb-pro-container .jb-pro-loading{display:none;flex-direction:column;justify-content:center;align-items:center;position:absolute;z-index:20;left:0;top:0;right:0;bottom:0;width:100%;height:100%;pointer-events:none}.jb-pro-container .jb-pro-loading-text{line-height:20px;font-size:13px;color:#fff;margin-top:10px}.jb-pro-container .jb-pro-controls{background-color:#161616;box-sizing:border-box;display:flex;flex-direction:column;justify-content:flex-end;position:absolute;z-index:40;left:0;right:0;bottom:0;height:38px;width:100%;padding-left:13px;padding-right:13px;font-size:14px;color:#fff;opacity:0;visibility:hidden;transition:all .2s ease-in-out;-webkit-user-select:none;-moz-user-select:none;user-select:none;transition:width .5s ease-in}.jb-pro-container .jb-pro-controls .jb-pro-controls-item{position:relative;display:flex;justify-content:center;padding:0 8px}.jb-pro-container .jb-pro-controls .jb-pro-controls-item:hover .icon-title-tips{visibility:visible;opacity:1}.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-face,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-face-active,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-fullscreen,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-fullscreen-exit,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-icon-audio,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-microphone-close,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-object,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-object-active,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-occlusion,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-occlusion-active,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-pause,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-performance,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-performance-active,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-play,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-ptz,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-ptz-active,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-quality-menu,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-record,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-record-stop,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-scale-menu,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-screenshot,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-speed-menu,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-template-menu,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-volume,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-zoom,.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-zoom-stop{display:none}.jb-pro-container .jb-pro-controls .jb-pro-controls-item.jb-pro-speed{padding:0}.jb-pro-container .jb-pro-controls .jb-pro-controls-item-html{position:relative;display:none;justify-content:center}.jb-pro-container .jb-pro-controls .jb-pro-playback-control-time{position:relative;justify-content:center;padding:0 8px}.jb-pro-container .jb-pro-controls .jb-pro-icon-audio,.jb-pro-container .jb-pro-controls .jb-pro-icon-mute{z-index:1}.jb-pro-container .jb-pro-controls .jb-pro-controls-bottom{display:flex;justify-content:space-between;height:100%}.jb-pro-container .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-left,.jb-pro-container .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-right{display:flex;align-items:center}.jb-pro-container.jb-pro-controls-show .jb-pro-controls{opacity:1;visibility:visible}.jb-pro-container.jb-pro-controls-show-auto-hide .jb-pro-controls{opacity:.8;visibility:visible;display:none}.jb-pro-container.jb-pro-hide-cursor *{cursor:none!important}.jb-pro-container .jb-pro-icon-loading{width:50px;height:50px;background-image:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADwAAAA8BAMAAADI0sRBAAAAIVBMVEVHcEx4eHh4eHh4eHh4eHh3d3d4eHh4eHh4eHh4eHh4eHiqaCaRAAAACnRSTlMAikwX3CxpwZ7zIGi5xgAAAZ9JREFUOMt9lb9Lw0AUx2Njqm4iGEqmEnBxslKUdhJctFMVcW5wKZ0CLpJJQRw6iVqQbg3FtPdX+l5+XHJ3796bQj557773fe8ujtOI+6jz5p84dHjTkYD4+RhT9CIRZWRPJr1bChnbY532GhT4oUpbI6HEuqvgIH9544dh9J4/rpr0Ms8oV3zMNT7X1MXSmaznzfHjM4n72moe5n8ryYrW9rKRvgf0S93JA7yKa9lbzUg3keJb8OVCtwkrFmoO4MnsAuj5rGqnZg+GZXUXkl9NjEui9n3YA9XgpMgakLXz6ujMTIgrCkPVv0Jil8KgKQN/wRN69hLcb1vrbR2nQkxwiZTGQ5Teb7TO8PUaS8Q03sE+zkjP8qbjzgJtEhRbV4gnlkOFeM7hDYNdxPbiYFvKSHN6L2NmY5WzMYPtplZdTxncRvn2sI+DHIoug22jWMaA12Y7BrXzrG8BX32XPMDKWVzw1bdMOnH1KNqNi8toqn7JGumZnStXLi0e4tcP6R3I635Nc/mzsMxl9aux9b78UVmn2pve8u6eR50j9c0/ywzyVl5+z84AAAAASUVORK5CYII=\");background-position:50%;background-repeat:no-repeat;background-size:100% 100%;animation-name:rotation;animation-duration:1s;animation-timing-function:linear;animation-iteration-count:infinite}.jb-pro-container .jb-pro-icon-screenshot{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAJFBMVEVHcEzMzMzMzMzMzMzMzMzNzc3MzMzMzMzNzc3Nzc3MzMzMzMwv5ahDAAAAC3RSTlMAW+8S2UGVwyyZnOTRhEMAAAEfSURBVDjLzZKxbsJADIYdBQpsSCyVMlXAdEuQYGKpWFk6dWHJgsQ7sMDSpUv3PgITAQT0f7ly57ucQ45KXRAZot+/89k+X4ju9KzL4XOhlm3pR0enYrWViSRPXQIQSASkRSkIJEFRimYCuQAHSW89IOv6SH5TCsuAj68Ab1wDzqkAzqoC7AUAPtgsABgkBBgkCJiNHehGok//KRVsHqd+3Dj1/vukt3AH/Jj05s5/AmyZhFVWXDls44iVvfQWkCvgxU6g9ZdJfCLvjJbYaT3GvjOY4mQSG3SJGjhr/Y1Xohp+TGKqqzexZ/1GVGdNCitt6R8zVvb9d+JmKdl8o5sPWbtxT6zFuJcDQtk92MNmYiXHquYlZlVt1j4P6cd7fgHFW7Nhqu29TwAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-screenshot:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAALVBMVEVHcEz////////////////////////////////////////////////////////NXt0CAAAADnRSTlMAWe+X2TINXUYZvctoYyuS2NgAAAEVSURBVDjLzZKhDsJADIZLCAsEg8WgEGCWGSwCgZxB4MgEAonB8wQEXoBH4BEWXgPJgEBG0mdgR3u77raRYAgnlq+9+/t3TQF+dKbZcJXSYSnzlViT457lRScKmBqILSFA3GoO8S4E+Ex5JiSJ4CbVdyOQdZNomX9D4dl+ko3NC8vFFmhPy8FIsi0ZlwLBW/LY5BxYYreUSgoFAEmhB5Rc9OCbUoXmTmDadQKTn4y6A/XTaoSKdb6KyGU6RJ7eHgpb3ABinAoil303xB6vQnRahNhXvMdre+fzOgxVrokX4jHAnBh8PALU8Eq8BqgTg/vePF8tpuPy9/NFaalSc273RizarYqfkswjifNMQ/TyTGMv4v87L+ks5gqDbc9OAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-play{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAIVBMVEVHcEzMzMzMzMzNzc3MzMzPz8/Nzc3MzMzMzMzMzMzMzMzLVn6fAAAACnRSTlMA+duduRUwSGSD8toSsAAAAI9JREFUOMvV07ENgzAQhWG3lLSp6MwCViYIRSpG8AiM4FWMaPymDBKESMF/cQ0S136F353vnLuo3gp1kOYEoCXW4LFKIZAnqAXYICeASoAdzgG+cApwgF4EfwF+oDkCqIwA6gnyAKA8AaizQhsBAjzuqUHofInGIQbjRxXjMrTJuHDestR4Bng4eGrN0929PqNfzC6h06weAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-play:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAJFBMVEVHcEz///////////////////////////////////////////8Uel1nAAAAC3RSTlMA+rbVFUecgC7rYhEEz+4AAACbSURBVDjL1dMhDsJAEIXhdeiGpKYWQVKDWc8ReojFc4ReAlPFFQazad7lIGkb0jK/WEPSsZ+YN5mZEP5UrZIP0vgE0Kv3oPlIJlAk8AJM4ASYwAkww2+ABfQg0ImgugPYsfNBVl99kA0AsjOArAPQpRTGHiBB8whxM0y+3skXNltsvMYriIWrpWPA80mFJ5qL3gAfB1/tcAt7rzdiV+HEgs1oBgAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-pause{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAFVBMVEVHcEzMzMzHx8fMzMzMzMzOzs7MzMz4IlKVAAAABnRSTlMA6xIU1hVqIuOVAAAAMUlEQVQ4y2NgGFYgLS3RAEQziQFZoxKjEqMSaBJpEAkgIw1ZQlBQRAEs4QhkDeIMDgAWx1gMHyIL4wAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-pause:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAFVBMVEVHcEz///////////////////////+3ygHaAAAABnRSTlMA7OgSFBXMmAA4AAAAM0lEQVQ4y2NgGFYgLS3JAUQzi6WlJY5KjEqMSqBJpEEkgIw0ZAklJSUDsISikpLQIM7gAJjhWp6XcaOxAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-record{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAHlBMVEVHcEzGxsbMzMzMzMzLy8vMzMzMzMzNzc3MzMzMzMxEZ/aQAAAACXRSTlMADPKKKeBxlV6neZ4mAAAArUlEQVQ4y2NgGDZgJhpowCURAJeYpIQENJEkCgWRgCeShAGyjfgl2EqwS7BFToZJiLg4ICTEImdOh0pwRM6cDJcIjpw5E6aDFeh8B5gECBCUYAkKCoMbNXNSOlyCgUEQ4apJJmxIEkjOVWFgxi4RgEsikGQJnEYp4pLA6VxUDyJLIAUJcRLIwY7qXKSIQvOHWCQODzKIleBPPjgTHM4kijNR48oGkajiYUMykwMAAfmZhUjBISQAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-record:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAJ1BMVEVHcEz///////////////////////////////////////////////8NopmKAAAADHRSTlMA8gyMLeRw1x2DBpWGN2QDAAAAt0lEQVQ4y2NgGDbgDBpIwCVRAJc4KIgEZM4EwCU2KSEBHySJCcg29iBJGCBLgHUs24FdYlnNYZiESksDXKJALebMaagES8yZw3CJypgzZ2A6WIHOd4BJgABMgh2XBEtpaBjcqDMHs+ESDFyLEK46aMGGkEB2rgQDJ3aJAFwSUSRL4DQKp+VHcTkXxYMoEkhBQpwEcrCjSCBHFJo/1GIO408MOJMPzgSHM4niTNS4skENqnjYkMzkAEgzyFpeX6L3AAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-recordStop{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAAOVBMVEVHcEz///////////////////////////////////////////////////////////////////////99PJZNAAAAEnRSTlMAMPndsnZj1CSYPcmL6wsdoU80pOJLAAABu0lEQVRIx5VV26KEIAhMA++p9f8fezatFDPr8LSrjMxwiWlqzRuMYKW0ENH46c3WuGyVLXEduuO83UyK59fl1jX5EEUXD9DOaSh43XEP5zUIdZ4pAecTofVnWSt3ip4rx7N61vjnY1D30CpH4QQR8vsP+RA5Rs3KpjfMU/pMim/LgbvH7DF2F8sU0owqapKLqgnhuGWwImUagn2zhUX6WQQoYkXG9WxSAJd700/ygsCpAoliaDsPiG48GM1X5Ft/06sfp8DrDE+3DpekWjxM6366fgEcnklC+AIIWYQmPEeAaUmjFOnhCLDfxZRH+w1gU5b/DYjfNcyJ0p7dxX8B+FwxQVtvAGB5ig0d5gFA5KbzS91hI8CenvlHflfN/XvzJQnxbBEko1gbvVnPii+FadSVRUEaYylQfJtpLB+aRG4LY/80yKdUbCraM0lozGR4ewZ0Wtnj1iC7hjWKNnjYmR62W15cLlL3+2pyMR09jccyuyUrHKsvthc5xsY1iWJ0Xk3t+2XP7AnWwrAQmBH6asXubmL1Z5Lz6o992jWiu9lnMSiQsK27FS9NxhCumZgB2fTBPFsFolhZr5B/D3o9sJAI6skAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-recordStop:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAMFBMVEVHcEwimP0imPwimP8imPwimP0imPsimP0imfsimPwimP0imPwimf8imPwimPwimPxLmg1rAAAAD3RSTlMA15sisGUvdz3szYwMT8H+hDJEAAABx0lEQVQ4y3WUO08CQRCADxBQchBiMDE24KswFlw0Wtig/oHzUWglJBZWio3GDjUxlhhrEzT+ABMrO2iptLLVRluDggI+xtmdXW73LkxxNzvfzuzszNwZhhDzdOxqaPGgYrgkOgFczh90ezAJQmpL2v6OHYnqk2aWxOTtAnu/O/Y5XNZXmTZjo3ot7aESwLdFeqAM0MoJkMf9ltwVQJ9PcQN0UFIJogvdJgLQVFMsAlxyBaPmVBDCE8W7qd+2SDsx0q4OwhSrCG134W54jDfKLjDNxaL8/9AAMM/solptRnoALBbwEPWYrOEzLnrZsTGoMW+fBHG2SiLPUNI6KMOH8QS/XsCMBYQekIEv7NGZF/Rht2yqmA4i3UG9O0iTqgMfhirDhRdU8XJZqqEO8tDAqje8IIt1r+I5HmBjfD9AxQ1MgJQRpc6GJRALHOAS1WRlhMs4VaSFzwIWzCUF3op71kdNsNs/FDCuA58YqCQl7IhN3WbDnlLtfjnuON515WM17c7w41QPOuBIzDT5wqi0T4ESGV3gjtTjkuPATwHoX9+cPRlmmvJ57YAir2qKy459QL/UhrS/uAu3xf8KiX3DI+b22t6jc9F/qfaum9E1pJ4AAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-fullscreen{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAALVBMVEVHcEzMzMzOzs7Pz8/MzMzMzMzNzc3MzMzMzMzMzMzMzMzMzMzNzc3MzMzMzMxdHBitAAAADnRSTlMA8AwGYt0aUcg+til5kgmZywUAAAGWSURBVDjL1VM9SwNBEB12IcGkMagxNktQSZFCYimIJAqKpZ3Vco3YBYs0NkKQgBg4ECtLFSFVCGgQrERBsLayPE0Ip7n5De73BdKLTrE3ezPvze6+GYDfsAQKy/Rz0i/OZJjY9XREuohTKulS+ZFGiADfyZXU5uHktI44VH5apDx554aXwJqloiIwiEsStAjCAsRNF1jCmGqa4Vc+BvS4JkjwzyPE6xiwzsxxeTbZxqjkANSWZFkoIj5bQBl4aBDfkPDNpeRRialB+SRAFz8UU1sAaEUjSCDoJ7iukZJ1V+c01bFczM1pWaa+a0Rp7MHn4V8Z1R9vLLCv9WjKdVFfk77JP+bZdz35YAfKXx6KhKp93abUYVbrj49g9aAYSuFCLbPUwzdCsYEWTloXgw1oGwQbENeuKwxzXhxwAADRMFd+zzRc6AAASY6RH8VjUHaXTrlOpDgCUP3gelc01e2d+f16cWbnQ46BGCRNVsWAWQJVw2xGfUXVv2k1OsLfazXqblzS99u1FwKFvBJioXBY2+r82U75Ab7O0ypVV0wKAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-fullscreen:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAANlBMVEVHcEz///////////////////////////////////////////////////////////////////+GUsxbAAAAEXRSTlMA3clDHfdfDQbsVG00u4Cqkr04dRsAAAGaSURBVEjH7VXLdsQgCPUBAROdjP//s1UTDUxsz3TRTc+wC94bHsLVmP9hkE8LIdvgupt2tFhcpy2CMZyVxNePEoqDp4hwEGz5PTqWoZNzLrmD9riltBEYzzpZQ8bweXy56Qy6Tivsp5JQYtawuMH3eJlSxIJtdSSa4xer4lMB89pK23mKrwjZVpsRzLq35vEE3+r26h6w5EKppcp3fP2lIpTPWJvoXoobeNI1sM3haHvx56tu2sdElJ54GbnPQ7RZ1sCpl8qPEMXBNQ8vN82jNbzLCPzGSDOZj/2Bqd19R1rELIEbDFrUJfKYdlALbDuDMko/hz7t8DqtCfr8h1Vt4rn7eh/6Ph37ch20aW8McsfGCOXzcr+GOlQG1rJ2HSHUDO/4Mu01qVAqTCpCJfgZ3phTS2pKm5aZOMUbs7Q6nk6L8UzRh6W78jH+gD/VRxHokPuNgUGTaPPR+zDR1mrlvcGgwkAVacSbeoN4Z0rb5/6XLrW/2GTLk2NhXHRKzrqAt9cD4rr4ddvae0NAYgOICdZyvPj4UYRf2BdfbB8iWvnTUwAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-fullscreenExit{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAG1BMVEVHcEzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMxi4XDRAAAACHRSTlMAf3WqJmy+7azWNnMAAADNSURBVDjLxZQ9E4IwDIaLfMzCOTDSOz07OjLir5CR0dmJFVHIzzZtnMzbOzcyZHnuaXpJWmO2ji4GhpAzzZeQzw8FKCj1pMHss9OgpwsGOS0YGOcVUMPsfBVkmJoVCBJW0FFB6SFIaMKAlRGDnEiDkuM00lP3SmL6H5QSh+iIr9ZWVWntUV9Z4qWbHAWrhcUYNLC4Wwm3xb1r2mOQYoVn2EKFAVb81KHiQQq6L3vSUoMBUmSzgCKbVeiL3eTp3Odf8H1sxRAZZNZt/Vt8AHcPQbiQQVF+AAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-fullscreenExit:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAJ1BMVEVHcEz///////////////////////////////////////////////8NopmKAAAADHRSTlMAe4Fzh7MZbKPtN8gpX19xAAAA3klEQVQ4y8XUIQ7CQBAF0CWhKQHDGtQKQFcURUgqEARBED0Aoqp3IKnoATAEW8MNOACmhQYKcyi6uyjmr+aLqXj56bSbVoh/J3HBxcw0ZfA2c3FiQLGeQcWh1lOVDDJTAeCbCgAR6QqCDj0xCEU5Bl2BIALKMwhdKjGILRUYfCIOEynlvABANk64M5jabJxHfJY2I76yzYu/ZCc0s1WbNYAQ3jxqwMoGHoqWGHpUQajEgcYYcKWFtjLE4FGJQewoxODRDQOqKPPcHl9sfzSXa/0L349tEDsOsp/8+2/xAY+BZBY9KhM5AAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-audio{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAIVBMVEVHcEzKysrMzMzMzMzMzMzMzMzMzMzMzMzMzMzNzc3MzMz8gU00AAAACnRSTlMAL0jMZKt96hGJsSKv1QAAAJ9JREFUOMvN078KQFAYBfCbJEw2q6SUyWxTnsJT2JTJ6D38Gb6n5PpT6BwbuaNfTnz3fEp9fZwAP7czD0MsGCzBYJcEYsGwBEHQQRB0EII1CMAWdAOzyJUvJ4jyDVyZVHKGpj9guEI2IuhaDFadYnCkIm9I+kPgn8t+kI7kOsT72HcwQnJR9Gofy8DrwwtHK8pLzdeALg5fNb6cdJ1fOjOGYrl5CLFcggAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-audio:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAJ1BMVEVHcEz///////////////////////////////////////////////8NopmKAAAADHRSTlMAL0TQf2frEaZYt8E+idL4AAAAnUlEQVQ4y2NgoDdgUcAuzr4Hh4TaGQHsBp3BLsG+BoeE2hnsEkCDsEqADMIqATIImwTYICwSEIPQJFhnBjKInUGSUA2CSPCcOcIwB1ki5xRM4iiDDrKEzQlsEuUF2CVY0jbgkDhjMLQkEkj0IM4gQQ1EHMHOOXMiakRpTiQQtXgTA+7kgzvB4UyiuBM17myAM+Pgzmq4MyfO7EwjAAAEf+BAxqI/agAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-mute{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAMFBMVEVHcEzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMztXryaAAAAD3RSTlMAHd4P7ztyiy1OnKhdx7uY2gyAAAAB8ElEQVQ4y72Uu0vDQBzHE45C3SJFaSGjUEiRpJRCBifXokkpSv0DfOBrEKxUUSoFH9ClFAUfU7c4OARRCs5WjThIsJuDlLo4ldvPe8SYxGyCt1zgw+dev+8vHPcvYybvfCR2OgUv2CoLdI62bUn3gq5UJxOomZofXPUNrIChJRklfcDSiKK2bYQ6PiCaREk0EQoYPFYa3F73F+DGsBJLmAjBAMBKakG15KAxqIhd2KuK5reRccBDNbqGJhfSlnNc/kBh4OY+vaoR5QrRC0afKgxMrKdXNKI8yxRENpyd+i0VA6wMm5IOBmORCwfAFjGwolofevbuPrvpGhRgpVaqiJflrLuUQQFRToS9l894EMDe7q6yL7/G5wMATc5mQCiAvQo3HAbwLtyYHQZgTxhohgGUa/CP12EA16W2/Ou4ZEj1dNG9oPMkZGiwJWQ8j+huPoJy9Z/XHTfUFSbAURveCtE5px5vs7hQDKS25FSDn2KR5Up1/lRmIHne7BvKd82LMRJbSjr5M40lmaYZ5/PRpiCpizjJyk+CwOGOzACLpSdz02QxkiuRhd8dNOwkV7zl2YX1mcySKJrQiPkXY9klSt6rkJPRwIlmzrcWSGyzHuTb11V/N0y/S6SdwdCR4m/47OIN7XOQCfwJwHGp8IcfyRdBLEZK4Uxp6wAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-mute:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAAP1BMVEVHcEz///////////////////////////////////////////////////////////////////////////////9KjZoYAAAAFHRSTlMAbk2o8o0P5TsGnVojFi/XHcV9urLaq0oAAAIQSURBVEjH3ZZZkqswDEXxLM8T+1/rkw0BuwM0f131+IvDyZVk6SrL8p88QCHp8UAnWmoWoG8AwQRN4/vOiqwUuwWqiob646MMhEW+KnMLMIXffgiZoODBusb7kCzjByF7OOszkOyp4amJ7fWVPwCN6BpyWRypvANPCvi7u4aTXe13YI+KkYA5bMRTSMlLnSyGwisSH40B0FJOrKVB6iAw2U7sGgPgQ/DTPQjitCOZT4Q6gWCtGwGFbSM34ENUNSpAKTACmC31rnRgJwjDdkFAJpfkAsZcAJvCSVQB2FaWWKdB/ATMAHwIUYL21lRBZRDiC5AnsBHBUqxEyaraFMoz0AnvHNa6xDWS34FGgNf6PYAEZoCt+BroUWFVbeb5HbCqSvB2cToyca+Adjlycdawt0C/HeyScl1W/w3gsU3aAd7JF8AuAN6isr7NAogXAFeq59Ec7ifAm49hF8wCMWfVNXAeCAkjEGuBr6R5ZGJzBrl4gGniBME+w8aMMyCsNZtfaemnmYaAQ6I3Rz0fnDhHJxc93Vrr7sHWjP3XRtTba2L3EkdFVaPC6YmXxKI9DGF1YPLdiwfD2r37cL5nDVw+uKzU5K27hsXCXPtyC2uyyl0D0p2TY7VaIvFYWU0Dm8TdOXmrVp52HG4lU4K/N38PpS/Kw70dbmb/sC0WCcQwNNzzjrzUj7veUyIIyL/4m/EP8V829O8zh5EAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-ptz{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAAM1BMVEVHcEzMzMzMzMzMzMzMzMzMzMzNzc3MzMzNzc3MzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMxDm1AzAAAAEXRSTlMAHqw+E8It/gjxc03jhGDTlgAjE2kAAAI6SURBVEjHpVbZkoQgDBwg3Of/f+12gtfMbK1Yy4NampCju4Ov1y8ruNyyo9fyCibVlNW6vUu29GpWQwTVdPTernqwffHex0WP4Nro3k+PrMKCvew/PcZ95aoNsY9yua+cGxTZsnf2K7ch4CAdGnWwYx/5LgKZCgxGdrhH3BeqNmnwviFXPZbQJmcM25HJ5n7/Pczr2QqksMJfO7BJOHLKLWFdMiL1QXYRwFagMklb4GB1zS7s8Le3fqH7Q28tVLkKdIzeaOJB2OKtY8KfTQCUa4++dNsLbrqpTR7xguHGt8hfg8P+seuaEkNdBjahzOD7g1cHP21SL2LtMNBKMdTRJjffTX1wVgrsEb1ITJU6QknwYJAcQky2cE02NbzPp70KyKggtpu9xM7Ik+CBIqY+8FrYyRUkrs+MEm1TG6tG9LZxNDNFGDue63TYGgIHqUUcuNhvhyZtn5LnlDq3Juxa4noCuX0scErOpNMDCrVcHPCS5rFGuQR9KZpxnB6CQ0ZO+AIBcTPk3UdbTx1L7jLEEHsMW7ZYm8NlIFyxn8+xdL7MURYEhzd5k2N2TabxIAORwC2r00QQfNSgrnqTJfh7cDmDu9ggtV0EZFr7kGu4KAxtNDln406TLwF9iZCIwn9EfjPuncRHYm5x3NfK3cFAS5kWxljSnTFlPHBAUFg5HsCCVOd9cdwXq+027un2QJlMjqISJutCDceRtTTuT4+1Q3HWHR8cu0fh9sGvADTYH9iLah/9nPz5+/MDJnQfoIVoAnQAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-ptz:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAAQlBMVEVHcEz////////////////////////////////////////////////////////////////////////////////////1bZCPAAAAFXRSTlMAeVoYDS1pBiPv4/g6lkzO2qSxvoixscijAAACi0lEQVRIx51W2ZLcIAwcLnPf+P9/NS2wp5xkq0ziF3ZRC3R0i/l8fvi04oxx89n+RBi1sqB38TKw7lw7lNzDa3U0e57bHhJ4d567HlqGC08eLAj9Gv8XD4/OwtsdhlfCW2s9LfWtuNrMhG1uLWP17Xh14LF4m8dx1OJ9jkm85YCYch5JzDVy81on3MFYkpTNYGmHHloYI+aqlNhlh5ZSvmH1AyBUwPcMX+u/clW3XYvA2YgRGXyZIZX6PXv0l3GlV9Jp9FKcK7mytJIg8xEeBdaBtV4P8tAixexnp73tY7ZBkrk9WEL8t7ZUDqtIA3jXa8sODSeUVqw5aztT+g4QeNAgM0PGTA0+EmfVeUvU0CEW4hX+lt/zseE7jJKDSmi0EUKBhr7EAMKPvNjOA9Fz4Yk/CFgcGRSdkYOHCLRxQfzKRN5SB/b5Ot8SzzQiKqcb6ipFdCfi1JI84OBdxzarhJ8RkpxHOQszl7bpn0GpmnXq6R4OfTkwwlyUWzeQg7rSLNhNUzEUEqJF3DMHeeXgqRIaYwHimLKipK8hMZOYVfJ5oKtIFSgXk5z4KcNx3GOIPGYfPigh9YGHBNQKVKbZhxMnqq+OHfWBHARZfWm1dnfSXfJuXInplse8wy39zkisdQ5kuLY+Bpc5hwGiH8Mx1rE24DFaLqXkfrNVkjk+8JPTPF0bUw+R9MCVuA8ks/xDyF/9orMqpBQeiob5RbIbmv7/TxvoX1Oqa914TsYg7QUWx8Y7R89JQY8mY6Dat1G5GARuMOgYHY9cbL1vNvc++fk67olLk4nrQSl7457OPqdeKn+vEwm4LPzW87DyXvqIyei9znHS+cZz9SxV+Qf8nNbjn37NkDx4+PmCXzHOLUMtjgmUAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-ptzActive{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAANlBMVEVHcEwgmv8imPwimPwhmP8imP8imPwimPwmmf8imPwimPwhmP0imPwimPwimPwimPsimP0imPoLqfILAAAAEnRSTlMADprCJBnU/gXwh3LiUKxAXjJ/ELXKAAACbklEQVRIx5VW2bbjIAwLa1jM9v8/O7Iht+09MwPlpS2VwLZkJ9f1lxVG1joPdZ0u1ZIxurhv8DH6dMpQI5t439GAEc7xYPgTRnAtV7plReRhd5kDP8+fd+wzt70yPhIJzWT7/6DCJCAYLReZPMIuJC4pClSktHqbNpJO1aTmmAme3cvtWs9NSfq52CPhnJNjlbVHwgmWSWGHeQHcKFjj7fygwidf2fa4mbVOuladcnsoDn9/CKKAWf6HEtpANiJvUp9Fgr0SvocPP/sqjOCK9ktpyCAo4Kt/d4n4/6baseNKAt5XrauPN1A4ZLAd30RUI4kNfELVx+yd3lrXYIg1Bp/B/dGs5N8mPsJoVyiwkofQSlm+K9aiwmoQMDoI4wefoK/rhkESrSuV+BR2i6HZH9juLzxCtBmn6rFmB66gNJQwBOSxncT/N3FAlxAoLQfJDy2+6rMLI3aznl9fhOcGSdZzoNhex2K3TXJEVe3M4Zb6QhHuJc5BWakYIqqietcPw6FK+EHofUHRqtLEQ5X8dKVENXXgEpLOpcjwkFI30YFVHE8fyx2ShZJ/ydTKlZwZ4BCOv/af9uMi0DILe4kieynScgPbhai+DRDckTT6N6zJpI3HMghM3BMg+q9pHmwrbR0Q0D5wc0JHPy0E5Ur7Nf3Rk68LnR0NHfO28/T5P4cNr+uLFR7eEdiNxvMRbd5Onlqsis4iIVdme4di3akWOz+3T0aUnFWHa8QZrO51NO69MeLPafrNuBenxel/3d21j8k/jyxf+75OQabFxOtuT94G3Lpjzb0zBn2Dl0ep/waPidQ/p/XB60Dp7e8F/QO7WSJg4zEzdwAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-ptzActive:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAANlBMVEVHcExru/9ru/9qu/9ru/9ru/9ru/9ru/9svP9ruv9ru/9ru/9ru/9ru/9ru/9ru/9ru/9ru/+3uxEqAAAAEnRSTlMA5q0cR/PC/ggSKtN5OIhaa5izIOiAAAACV0lEQVRIx5VW2ZLjMAiMToNu/f/PboNsJ5PaKWv8kKRkWkDTQF6v/zyUfO+JX9sP1dKKd/v2acQcm992kbo1R4il7iHIif0BRNtCUOozH4cikAdt2YfjRLTnzN15/8I85wFCI2xDzlnyMLO7R0aFoTzbFGB+jIkYLkyePvmW8d2fs2ZfZuv8It9s6zvV5uSr2LmKb9oVh37QvpjYOcf8fcO3Cb1jGqOU0SvTTd6X2KUBLgKdLzaiDtE2n+isTh8/+AX7E6/Vh/NNSyfVm10RXIe1nxXRapmsDQB7SMnkmE0IxgqvUn0jteEf1T2CHXgr0gjZllGk1HoJL7lD7fRpD20CgJcwQ6EdSh3lLK0zVfuqz2WvPlPJh7HqnCQ4MysxoAYQE0vHub/toRuqzRy5pEUvbg62MzlJQn1MHBe1x7WSAddpNDSN1c9wRMhKfi2jrICwAIsQvHkD7Al41bOrBNC1Y7QdmSg15LDokGETJB/imxYJKfmFMDqI3FjUkDS3NSEDjBaxknRYSb+4nj4kC43W2OKrkqGB8jV2Fq03YiWbhHQTpUV1mMHXWYf8HlNX7ZUQRC6iwBAIK0r0ILr1UxqKsPattAkhwSIiMl76bT/Ft/TbTy2jHQoM7Cz9isHVMb6GILl0H4DGivbw7xNc8rAtiJn3O/qvD6JT/wgs7Y370ZSdWmbZWEKiG1lXImldW7Q1jGMbZW9tUVKZmGjjGvf+EXAqzWiX5OfFSHVp/zjbfGPc3whdcbTF67V2N1e79qYMru2/AnzVYFsdWFt/+nOypv8vQvoHou4gOtSrG5EAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-performance{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAnUExURUdwTM7OzszMzM3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3NzW8hQDoAAAAMdFJOUwC/gO8ra6tUQNmVEKtAA1gAAAE4SURBVDjLzZMhT8RAEIWnpS3tXcWdQ6wgOJIKfFdUICv6AyruB5w4EuSJCwRXgUBWkGArLsEi6JVuaZkfxawguSWz/p7azLdtdt97C3ASagp+PpeXPDjHVx7McMWDCBc8qPCZB4m44YFIeOBh+ckCf9z+sCCYqi8WvA/5Nwu26cfA+JRBs4h7WmX/tr9AXcQHAPdgzEPZh5idTQBOZ3zyiModISKQGxZ7dd85E0SKfMGjM7vXeI/5AE5LvrwpcK9IOrQYB5BlCsFIvjzgrkaSjsBZbmC9XsGsAxfn8gKXpL9sSlnQFBwFTTuaKWXgI5Ar+sfHTrW6DUDO+2jEEtCNQ9wL6gNujMvvdFS3eugxGT51fBXxTvF1wLLngbT0BGphKalAy0MQ3Z4Ha2V7UoMFJKkFVAWcvH4B0OJfd9YsTl0AAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-performance:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAzUExURUdwTP///////////////////////////////////////////////////////////////8/RimEAAAAQdFJOUwD2qdfrViyAwW2XORBGtSCsD6SyAAABN0lEQVRIx+2VzQ7DIAyDYfxToH7/p10CbQ+T2s33Rb1MijM3/qDG/EurolD9zSNRggBU0hFelMABmenfADhGkJDhGUFGgiX6I1AAQlDgZK/td8ELtQORiTmIq42I2bYdCD8NjzPmYQxO+rbn2S5qzEUF6YjkgcJude56X3s0SoL7LRAeylyaVPiFq0Jyx230mpeXmXVOHssRbhackhCq25Sny++xcBW1n6Snqw6BTgrqRP6kzfDcgmRLGonDWSf2OecwB491dOrEVZpNsxiyiHzWJ0Z+BbBwzaoWVb49HrqVfSkxHen7WNznLhzl8xpQlT3E9oHUtYaufB+7NY9g9ctbjFfj/h1DwXUrzDltMn1Ql9M814EQWPLmEBg4R7JSzpFQxTmSBMkLvJKOhFHfKEEnv3L/+qw3DuMPzAFH9pIAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-performanceActive{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAtUExURUdwTBGW2xKV2hKW3BKW2xGW3BKW2xGW3BGW2xKW2xKV3RGW2xGW2xGW3BKW2+P2IvgAAAAOdFJOUwC/gCvzq1RAleURc9VmrA9rXAAAAT9JREFUOMvNkzFLw1AUhU+SxtTahnZyC+IsIeAenDuE/oJQcS/FTYTi6hKcHYpDZ/EXBME95BeURpM0Me39Db4Mgk/u23umcL8kvHfOucBBKEr5eW95xoMjeuBBlyY86NOQBwt654HrPPHAca/YuUHrDQsG9eybBWa1+GLBqvS3LJiNX0vGJw/R0N6LJ+/f6x9I3uwG0D+luRU0FnmdCtBy6ZM7KvQdjgXwJYuNZVNrFfqF8IX+nFm/oBvyS2iZ8OWxgH4u1IZmU4kgfoa5E77c0jwhoTYCbTRFGE7QrXFCveCURkK/2cRBCp2gFYiyWk7Jw4AgXOmQZMAqa9uAeCP+JsViihtb9OKIPtBUuvy8jeqyHRpMhvc5X0W6Lvg60HrPg0DREySOoqQJKRbByRWbExaqlSoVwB0rwCLFwesHquttxhcsa64AAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-performanceActive:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAnUExURUdwTH3G63zE6n3G7H3F633G7H3F633G633F7H3F7HvG7H3G7H3F6xOzy+EAAAAMdFJOUwC/hKbvK0BT2XURZct0z00AAAE3SURBVDjLzZMhU8NAEIX3LiSUNqI1qBMMChFRgYxAd07E4CI6HQwzEUXgKjC4CH5ABAKJyA+oaHqBJO3+KPaYYSZh9nyfyuyXu7l77x3ASSgu+flYX/HgHOc8kC4wwikPMnzhQaTueKAiHvi43bFg0m4OLPBM9sWCjyb9ZsHm8NkwPiUQT8MjfSX/fn+F/C3syJf9YB7oLsDkjIaiHix5QCNbGBmAFPve+3lXi/0viLB3ZnmDz5g2ICry5daAvCbZhSE2oIsFeC35ssJljiQbgZitIY7nIGu4wLG+xBnpL5tClxQhCANR1Q5TSmCCQK7YjftOVbYNUOxot0EsHl0uwHdFfcD14PJLG9W9HfpMho81X0V8MnwdcHvkgS4cBcqVo6QKHQ9B1a6XY1zAcSiIFg6QlXDy+gEd714RcAqEowAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-face{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAeUExURUdwTL+/v76+vsDAwL+/v7+/v7+/v8DAwL+/v7+/v96T4QgAAAAJdFJOUwBAgSCbs9hZap+hQJoAAADvSURBVDjLY2AYRkAJCAygbEZBQUEBKJtpJhDAOJpA9kSYxESgKphuIaBuhASqyYzESjC2hDRilQBZ64BFgm2qMLPYNCwSkQpAwlMBWYIRzJsKYrKCxZSRncKSDFYzBSNY2MD2MqRhSFgqgClPAwwJBxwSbAk4jGKZDCKZp2JIsEOcOx0ztjQDYL5ENwsYJKrTsMSvGSgepzVg2pE5qSikPRPT8sopBuDod0ATZ54JsbZyBrJgAVAp1Ax2oPXMykjxYQaLA2CMoERUZQDMrgBUCc8CKIdzAvHJhyQJpLQLSu0TCaV2lPwBSu0KQy+LAwBuJj5UbruNggAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-face:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAnUExURUdwTP///////////////////////////////////////////////w2imYoAAAAMdFJOUwBAgBvfrFmcw4wrbtGliFoAAAD2SURBVDjLY2AYRkAJCAygbEZBQUEBKJvpDBDAODpA9kGYxEGgKphuIaBuhASqyYzESjBu79yIVWIP0NoCLBIsR4WZJY5gkYiZACRqFJAlGMG8EyAmK1hMGdkp7M4gkus4RrCwBIApFwwJmwlgqsYAQ6IAIrEAwygHHEaxngJbfhRDgg0sxHoMM7Z0AmC+RAPsJ4S5NE5gid8VoHg8sgHTDh/PzM60HkzLc44bgKMf3RKuMwoQ+dNIgswJDAycUDPYjhgwMCsjxYcJLA6AMYISUTkBMLsaUCVqEqAcngPEJx+SJJDSLii1HySU2lHyByi1Kwy9LA4AqflRBKNSA88AAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-faceActive{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAhUExURUdwTBCT2xKV2hGY2hKW2xGW2xGW2xGW2xKX3BGX3BKW23povfoAAAAKdFJOUwBAgRybs9hZLG6hrV9tAAAA80lEQVQ4y2NgGEZACQgMoGxGQUFBASibaRUQwDhaQPZCmMRCoCqYbiGgboQEqsmMxEowTgmZiFViFtBaBywSbEuFmcWWYZGIUgASXgrIEoxg3lIQkxUspozsFJZkEMmxBCNY2MD2MqRhSFgpgCkvAwwJBxwSbAk4jGJZAbZ8KYYEO8S5yzFjSysA5kt0s5YKc6guwxK/baB4XDYB045Vi4pCyrMwLa9aYgCOfgc0cY5VEGurViIJMhcAlULNYF9mwMCsjBQfZrA4AMYISkRVBcDsCkCV8CqAcrgWEJ98SJJASrug1L6QUGpHyR+g1K4w9LI4ALk0RHtSETFcAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-faceActive:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAhUExURUdwTHzD63zE6n7H7H3F7H3F633G63zF7H3G7H3F7H3F68TinBIAAAAKdFJOUwBAgR+Z31ipwW4yAjb9AAAA+UlEQVQ4y2NgGEZACQgMoGxGQUFBASibaRUQwDhaQPZCmMRCoCqYbiGgboQEqsmMxEowTmmZiFViFtBaBywSrMuFmSWWYpGoUgASXgrIEowgHtsKEJMDLKaM7BSWYLCaJRjBwloAploxJKwUwJSXAYYE2KUMVRgSrAFgKhTDKPbFIJJ5OYYEG1iIfRlmbGkVwHyJBlhWCDNrrMASvxageFw6AdOOqKCklrQuTMuzlhiAo98BTZx5FcTarJXIgglApVBr2ZYaMDArI8WHKSwOgDGCElFZBTC7GlAlvBKgHK4FxCcfkiSQ0i4otS8klNpR8gcotSsMvSwOAIs+RIlIrewIAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-object{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAA51JREFUaEPtmT1oFEEUx9/bnRALGxE70btkZjcR7QMiRisRbCzEQvxCLGyEhCDRBC/RoNaWFgYLFUSwUBAUDKKFFqIQ3OzNJpfCyspS8HaejNyG87J7t7N3l03gttydefP/vbcz8+YNwhZ/cIvrhx5A3hFMjEAQBDwMwzOmAi3LWhBCLDTr5/t+ycSubdtLnPOncX1iAfQAlmXdNBmkru2RVgBSSspg+5kQ4lRjv1iAIAgqRFTIMIju0i2AWNvrAKSUowDwLqP4tACm9rWmbABCiFxXKimlhu0BtPFHtdc19wj4vn/Wtu3XnPOfWVByA5BSngCAuwCwryb8Hud8EhGNltANB/B9/5BlWXcA4GCMx/8g4jjn/H7aaGwYgOd5BxhjcwCgPd/0QcRVAJhM2l3rO3cdQEq5GxFvEdH5VsLX7aKIH8MwLLmu+zapb9cAFhcXt/f3988AwJip8Jj2L2zbLg0MDHxr/NY1gCAIpohIJ2V2BwC0iUd9fX2lQqFQ6cQvtJbICSGOxHhlVOdKjuPMl8vlqzrxI6IdWUAQ8Um1Wp0ZGhryiYghYrUtgDQionxJT0al1IwGkVJeBAANvieNDQB4joglzvmilHIXAIwrpX67rmuUamfKcxoTvnoQ3/dP27Z9g4j2J4C81KBCiC9LS0suY2yMiC7rttoZuQBEQhsiopfT6wAwUvv+hjE2XSwWP0kpRxBxgohO1kPmDhAHEgTBUaVU1XGc98vLy8eVUhoqbpPLPwIx6/wqEV2ovX8AALzZ/Ng0EWgQGa1iLQ8xPYCUyx8YHjt7EWjm2I7sAy0it7ERqNWEDkeiklIJg8pFWwC1ZC6SM9NYc1oXgcaiVlxVYmVlZW8YhlMAcCnFvOkEQPqqRBqASLTneTsZY3pjapZSpwF4Y1nW7ODg4Ie202kTgGiwSqWyrVqtXgOA6Zj0OhGAiF4R0azrup87dqDJAlA/eLlcnkDESQCI0us4gOdEdNtxnK+tfkHjA027AJEgKeUVANBROVd7p3fixwAwJ4T43kp4nR2zylynAOoE/JuAiPiDcx6kFb5pAEwF5zKJ2xXZrH9uc6BTUD0AAGh639Xo6bjUI+G/NglS+p3Y87wCY+y/2ozJSGkuRDLekQFjbEexWPxVryfpjuxhllKhNtwtAESc55xHx9M1hsR0WlcOlFLHTLyv26Ypi5hesyql5oeHh3VBeN2T6TxgCtXN9j2Abno3je0tH4G/KbtRT7VUKs8AAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-object:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAAwdJREFUaEPtmTuIFTEUhv8fLSxsFrET3UIQRHtBRNdKBBsLsRBfiIWNoIisD1xfqLWlhYuFCiJYKAgKLqKFFqIgoiBoYWVlKbjwy4HMJXs3M5Nk7tzZCzfdvZPk/F9OHicnxIgXjrh+jAG69mCpByStB3AgQ+AcybmqdpJmEvv9SvJhqE0QwBm4lGikqD4VAaCMvh+R3NffrgzgB4DJDCPWpC2AYN+LACTtAPAqU3wsQGr/pikPgGSnO5Ukgx0DNJhRzZp27gFJBwE8J/k7B6UzAEl7ANwEsNEJvwVgmmTSFjp0AEnbANwAsDUw4v8AnCZ5O9YbQwOQtBnAdQA28nXlp/NG8HT1G7cOIGkNgKsADtepDnx/C2CG5Muytq0BSFoJ4DKAUxnC+5s8cSCf+j+0CXDBjAJYNgAA6+Keg7AwpldyAXqBHMmpwKjYyThJclbSSQBWfyIT5IF5kuQ3SctJzjcCiBHhxUu2GM24gRx1IGtj+gDw2I34Z0mrbXcC8JdkUqidFecEAj4fZD+A8wA2lYA8NVCSHyRtcGvouKtrg9EJQKHVB7Ht9ByALe7jCwAXSb6TZP+dAbC3D7JzgBDITgDzJF9L2u2gQoectV0yAD7IEffjDgC7plaVJQdgYotdLOYSMwaI2gETr51jD1SN6qDOgSobw/WAywltLxRVhBIxi7LxInaxUCHHFvmCpFkorWInoR8LheqsA2DB3LGIRdPIAznBXC1AIVrSKncwVYXUMQB2Sl8h+SYQOCanVaIBPJAVAM5aqBAIr6sAnjnh78s82aoHQkYlWYwz7YXXIQCLRK+R/Fg3BYcO4HnlhPPKIfefTYX7dm8m+aVOuNdP+1OoSow79KzKL5LfY4UvGYBUwZ0s4qYiazzY7RRqCtfZIm4qfGBrAEDle1e/0FDoUTKvUxiT3gfsaWlBbibFUsyDiKSkBK9nf4LkH19P2RvZ3cxUIVoEmCVZXE97DFXPrJY52JUy+lY3Ji2S8cxq4i3jsahk3QdSodqsPwZoc3Rj+h55D/wH5CHfQHNA9EUAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-objectActive{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAA9JJREFUaEPtmU+IE1ccx7+/N7O0BwUn24UeRKOd7LalvS+I6HoSwYsH8SCtLf1jEqWoyLJVMfEP6nlNIgi6eFChCB4UChaU0h7aQ1GQrnFSdw9e/JNs9LTFzPvJZI27m8xk5k02m13I3sK+33vfz/u9N+/7fo+wzP9ometHF6DTGfTMQF+mYNqwd6sKlFLcK++P3WsWF8nkUyr9stAeTcXN624xrgDVAUgcVxmk1lbaGPIFyFocou9fSonYzvo4d4BsYQLgaIhB0EYA174bAFaNWpuFhrthxDsxQQCMrKXUPwGbvfr2BSglYh39UjmwXYCwy2kh4jqegd5M/itdaL8+i5vPwwB1DMDIWNuJcBbA545wAs4V4+YIiJQ+oYsOYGTzGwniDIANLjP+hkCHiglzNGg2Fg3AGB3/Ugj9NBO2+4ujSSaMeJ2uc+PbDhC5aK1GhU6CeY+/8IZz9E+CnSomBn7zim0bQF/m4QqbPkgDOKgufH4EAzdZaKny3vUP6vtqG0BvtnCUwY4p01oFcOIZuCJ7elKvvo9OtLyESMN7IzeViA3VC3TshiY4Wkz2j0Uyj3+aMX5shAIhuia4kn6Z+DSPu6xjiCotAQQRMeuXaJJYpqsgOetbcBV8TZA+ANxgotRU3Hz48SWr7800DjHL6VJyQMlqh/I5jYZvFsTI5XcRa0cA/sLDv98iieMv98X++Sj7aEBCPwjwD9W2LNMdAqhJnQMyc5D9DGBwRhzukK4fK/647q9I1hoE6DDAO+ZBdh6gEaQ3V9hi27JS3tf/u3Hhv20kpQPldsgthQw0fOcnpc3fVK2EhosEmE33x9LJwKxM54Lj/Ap0SeoCBPz+qVw7uxnwmdQFOge8R1n0DDg1ISaxqSbJy0oE2pTvqhStbOK5FQy2ka6vOTVkoL6o5VaVWHX+yVoh7KMAvvPbNq1mQNmNBgGoiV55fry3R+jOweRpqQMBMO4wxImp5Cd/tGynVQBqg0UvT3z4eroyDMaxenvdHIBvE/GJYnzgb69MtjUDboMa2ceHCTQCoGqvPQBuSOZT5WT/fb8luOgANUFGxkoQYVja+Lq2iQm4ygKnS3tj//oJf9+PamUuzBJqJsY59Jz/9wh6+iJpFoIKXzIAqoI7solbFdksvmN7YKGgugAMNH3vqp9pN+vhtq5VMqT2PpAbjwrW59VmVAYL8iASCfdGBvm/bpQPrCvP1eP+RpYrXA5XKgTaBkA0Voqb1eupL4DToFo5YLlVZfadtkHKIqrPrFLIsXL8s0k3LaHuA6pQ7WzfBWjn7Abpe9ln4C11Qo9Pmb2aMgAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-objectActive:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAA8hJREFUaEPtmU1oU1kUx//nvthx4cJqX3RgGLsQBNG1tq8Z7axkwM0sxIX4hbhwIyg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NaJUQqJ3Hi3ut+KSNLajMqfQV5EAnzRuZoEF8ivfZw74dqPbXeyP4MVSoE0DoAmkgafeXrqS+A08CpHBDTPpXRd9oGKYuoPrNqETlhD/w446Ul1H1AFaqV7bsArRzdIL5XfQS+AaeCtE+rbksUAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-occlusion{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAACCVJREFUaEO9Wn2MXFUVP+e96X5oa9UoYpUszrzzZlqLKDYhgAFaaKU2+M2W9Q8TNUQSQEsiRUjFVaHGNmoBMTUmaPwD2iIhqKR8WEBFGkLFUjbtzDtvpjYrVhpCXAtm1867x5zxvnXm9c28mR13b7LpdO65557fveeez0GYp1Gr1d4VRdF7lL3rui/n8/lX5mMr/H8wDcPwIgC4QkQuAwAVWv9eA4BJy/8sAHg7ALysf4i4DwAe9Tzvj/3uP2cA5XL5CsdxNiLi5QBwUkQeQcRHrNB/JaKZZuGYeRAA3gsAZ4nIBkTcAABLROS3xpjdpVLp0bmA6RlApVI5FxE3OY4zGkXRdkTc6/v+c3PZPAiC80Vkveu6Nxlj9ojIjmKx+GIvvLoGcODAgTctXbp0CwBsEpGHEXEbEf25l83a0TLzh0RkMyJ+AgB2TE1N3b5q1ap/dcO7KwC6AQDsRMQhAPiu53m7umHeK00YhlcDwC0iMg0A13ZzQJkAlKmI3A8ADyDi9Z7nnehVsF7owzA8Q0R+BABXIeJY1mF1BMDM3wSAcRHZ5Pv+nb0I0i9tEARfRcQdxphvFYvF8Xb82gJg5gsA4FkiyrylfoQVEScIgttyudxLhULhwSQvZhYAuJCI9qftkyrckSNHzs7lckdFZJ3v+0/0I2CntRMTEwNDQ0P3i8inAeCUPmLP8/Y2rwmCYC0iPl6v19+3fPnyvyT5nQZAmQ4ODv5aRF70fX/zfAk/OTk5PD09rcbg4/EeiPgPEfkUET2dALENEc+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HBG1WNU8NIObDReY+RYA0EqDhhJqbQ4SkRYI+hotj9gCaDGj1Wp1lTHm+ZRdvk5EDdVh5h9qiaWZBhHXe57XCLKYWVXoygSPk0SksX1fw3r4FjPa4shsVqanP5DcyRhzebFY3Hf06NG31ut1fWQNT4uIWzzPu0M/d3gjTxPR6r6k/+/htDqyMAy/Y4zRPOCmmLn1zprYJ8eriLja87yJSqXyScdxHtKSi4YCVvgxRLyvjZCpFqhXQEEQbHccZ9rzvG/EsdBFIqKmVK1AYzQJl8Z/NH4PzHxV/NkCuAsRb0hb1C4x7xUAMx9GxGu0ONycD0yKyGcTuegLAJD26K4joh+nbczMe7QolZwTkZt939/Wq7BJeq2nIuIviUgr3v9LaDSdFJFjKR7vtMcYOxXVRWPMpY7jCBE1PGiz84k3F5G7fd//Sr/CW80YR8SROK2cvYFOKSUzr0PED2hGZYw5ISLHrafUx9QI2uICmFY1RORsx3FmEPE5EXne8zz1mn2Pjiml1d+ekvrmqHO+K3hWvvZJvb2ensoqCwmgq7KK1eGttnuyNqsSvVAAtGINAE9oF6hjYUsB2EaG6vWx2La3U96FAmAt28jU1NTqZOMjtbhrGxovZJXVFwJAXGYHgPPS6lVtS+dxY6PT41wIAFpe79To6Fj7j9PCdmX2TvF6vzYzLqtrgyX2MWk8M5sXcaNDRLQjeft8Vaxj4bQSLSJbEFHjsraNjZg+E4AS2obHTzRb0kxsvirXVm1vBoAT9Xr9y2kNjeQtdAVAF9kQW5PseW+zzszMjCcbGe1UsmsAMYOURveTc62nap1TRNYsSKM7eQIaO7mu+zkAWAsArwPA45oXdPNTAxutaillsTqoKIruW7CfGqRdpRaHReSj2lvo8scezyDiY1qc7dda9axC3W64UD+3+Q/fZENVhTDr2gAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-occlusion:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAABphJREFU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bX6mJT41eE5TM52/6M6uQNDxlzvhAdga91gusat81cDVquurPQA4QS3s2lcN8rYyyOGzfLdQ8cseLsiWu7NrN1o17UJVJ+zW123+BfogD+TkdLQFAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-occlusionActive{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAACItJREFUaEO9Wn+MVNUV/s59+wOqyM7bFaxAsDBvFqnWak0MYhBQUEqUVssi/aNJawg7bxaKScFqqKyt0gip4sK8XUKqjX/IL43RlqBSxLZWYqwWdWNh3oIlFClkebMUaXfZefc0983MMjv7Zt7MLrvvn93MPec753v33HPuPfcRhukZt+3YeCndCQpeCO3kmWVTTg+HKbocoKHWjpnEuBfMd4GgnJ7ADAcCJzx8iUlE0AGcBOMkiPYz4c1kNPzXodofNAE9fvheQCyBoLvBOA/wHunSHq7CiXM9+BdWGj39nGuxq8dWYyJdxCSh8UKAFoIwBpL/CMidTmzam4MhUzaBmpYjNwmNVoFEA9jdSEx7zzZFPhiM8dotiduYeAFIWw2Wu6TLm7pW1n9SDlbJBK7d+rev/c8du5aAVQC/Lok2dEWNv5djrJBsTat9s2BeA9AiBjaN1s499eXyW/9bCnZJBNIG0AbQKCb8OhkN7ygFvFyZUGvHQ8R4DOBuSWgs5QUFEkiD8nYAuyuJmk5Hw2fKdawc+fGtHeN6mbcAWMxES4NeVlECequ9DoxmMK9yYpHny3FkqLJ6PPFTEG0CyyedWH1zIbyCBPQ2ewYk3ndMI3CWhuTsOhb6uMQTEBWfOdGpr+Zj6ZbNELjdaTQO+tnxda6m9R/XCa74AoLnO42RfUNysJjyuvYqffyo7QA/AKCXQYuSZnhvrorelpgHSW9LSn2jK3r9P/PhBhLY1V6ld1b/HuBPHDOyZricn/jsidEXRnXvIOD+SzaoS7r8/a4Vxrv9SFiJDQDd5NT13IeGGy7mjg0gELLs9QQsQAp3Oiu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3HcD5NYlgzNcAgqI22/CsY9JTbOmro2ZbQALkujpYWs8IRVSa8vdUa53OLEvJb9sZ5Pp3F9tsettAyKJPWm7vwDKbNHCG+bIW3BqAFcVsj6BkzkuNEMa+td4woUQG0ezgYCCCyI69MPRdL93yYWLyvKJRnWOpy1n7ZXC8aLXkGWy0C1AjJS9hYE6LzZ1mTkK7EiZ6V4ztS1G11jbnCegRB3uPshms6sdL87+8nKPE2gNV565S7mNQOw7L9DyAcVOXzxOG3ZRyBz3y2+ixqW/QEAj01HD5t606+8HEdS9u4CKVUyLB819ebNtQZbKq/4VIjA70xdm63GigBkXmDJhy+reB6b0b3u5fO0nA8hOKlrTgUtLj7Dmxu8LakHfzLa4J0Zdo4oNMe9VlZ1pYyoUk/i62DSAc6wlMfzpf7ioc0lwAqsxnUEugDB7yCLd8127f0rEXzFK2U+e9R2qfcCcCUCLWej4qXeAVAjrTKeAKqiVRQItQQgaLng3BI/Jnq8ACjGWlLgTUjeW5HYUgCcRsZgfY9UG7qQ28tN6XgBUJlNCJoj6gcXlDY+PMndwmb5wI9WHw8ALs0O4tu8+KoK9Hq+sVGJXh8PAHl6vXyjo3KDo3AtLEezVzqvjzYbubQ6A3G3xpQ5nlR25TY6iLGF6kRyrBhrNwrFRHNWRpiwvlJjw5WvqvuSb3gEfs2gIAFPjhVznU+VeJTAGSFyP/ZqaJQ+7qoAKCXV+OBTdnw82qw0TYuXNjLKrZOqAbgGShvdLOitkfKpiuckyQvHpdFd+gQUHQ9B3yPGEhDOAfwGS9pT1asGgldCNTgYk5igCtSL4/aqgddUKnIY4G8xc7ialz2IKAXQ64qcHW22qnkJVetwvF63+T9nAHfjRfzL0gAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-logSave{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAA1RJREFUaEPtmT1oFEEUx9/bFLFKJzG2Wikn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LoYbr07oIIBvDNRFnPuwBlzXwybsd74C95KWhPrxIhsgAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-logSave:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAAthJREFUaEPtmTuMTUEYx3//TrWdeLRUJCrReISGUBCvLVAqNGiEiASh8IoClUKyBQqPBAWhsREqnYRE9NgoV0T3yci5m9mzc2bmnHvvOXuTO8ktbs7MN//ffPP45hsx4kUjrp8xQNcezPaAmW0EDgFrgLXA0j7F/wG+AlckPW5qKwvAzE4At5p2ktHusqTzGfUWVEkCmJk1MdygzQ1Jp+u2iwKY2X3gcF2jfdS/I8l5O7tUApjZTuBlwNIk8E7STHYvQMCTzs6jgI27ko7l2o4B3AaO+4YkJadcVcdlAGfLzA5WQGR7IgbwGtjuCdoj6UXuyJTrhQBcnQjETUmnUv3FAH4CyzwDKyX9SBnM9QAwIWk2AXFV0tlYnzGAebtPP9OnEPkNWO2J2STpQ+9/xBOXJF2ogmgT4Amw3xNyT9JRX1gEYq+kZyGIoQGY2UXgAPyPt5x4V8qH1TRwDvgk6XdkOr2XtLltgLfA1qLTj5I2mNl3YEWDdTQraaJtgAVryMz2AU8bAFC1Boc5hYKbgJmtB6aKgDCbZdEAeLuOWyPrit+qFMmiA0gJrjr4yu1an0Ip4Z6Hss6hMUBu6FD3JB9PodwRGGkPeOHCX+CafzmPDUAR85wBlrgwQ5LbUueV3AHsaxGbmR8uOAGTPYga8f+0pG2LBWAOosYNrFOAqith6L5bdQee81optG7nHIjE8DlnVlB8EVa3AxCJ4VMAleJbB2gAERXfCUANiKT4zgAyILLEdwoQgcgW3zlAIWA30EvWXq+bFBvESTzQxFZqSyqdAe7i7xIAvTIjaXnIRmupxZoAznvPvTZvJO2oCzDQ5G5NgPKbRGWyt7X0egrAzFwedktFtnqXpFe1PFAsxLYfOEIaH0g6UjUAyXx/i09MQY2pq2gSoPDEsB/5QuJPSnLrMFqyAAqIQT+zloX9Aj4DX4CHfuo9RpANkBqJrr6PAboa+V6/I++Bf0in3kCazcMZAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-zoom{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAkUExURUdwTMDAwL+/v7+/v7+/v76+vr+/v7+/v7+/v7+/v7+/v7+/vxtcv/AAAAALdFJOUwBVdZCpGdZE7C3B0wnY6AAAAV9JREFUOMt1lDtPwzAQx20ghGwVsHVJhQRDlsywWKqEULsw8FhRJ2iWiglGGJvFCw8xIVVCSEwkKU25L4ftpOHs2Cclsu7v370smxCLecRh+xOH8Ja5kGEs/6dtYf1XONcWZurbwwRCQjYZ9l7uHAPAOBHpI1QC/QSY9g84ldmnAn7Z3htwsfZnvSe54eJd5hlcC7gccxTwSETZEr5+714vLRdfkFraVNtetXLpR7ha+mkHCV1Y/q8ZOW8AgDkecsIaAEIcGbIGyLUq4KcBYl0o7QAB4FZACBMrQKpaJOCZQqyAIhgayYEpgD1DZghXCvBTNJFKmCvgzmhcCEsBwMjSRx6BslYftRX0JLYKrAsLqzDC54EFMwvy58QhMM8hFNq0UpwET+sLC9/4BmEBTytyRCIbDoAEiR1Qp1dbRxfoyv9g3ogaKVuXhVZZbtqX6Ez6H23PxG4CM259QDwt/h8ABfK8nDqSAAAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-zoom:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAzUExURUdwTP///////////////////////////////////////////////////////////////8/RimEAAAAQdFJOUwCCyreU7KZVRCDaZnETLwj6WaYVAAABUUlEQVRIx5WV2Q6EIAxFoVBWxf7/184AOnGUsvTBqDmXrqRCzNou1kwGtyZIQNuiD0P6evWTURFUMtIoPO+iVoGIZPkMqkdKA0jZgoJ6suQc2EoiGBldOoQ9HSDk5+GTs1Fqo9IlcEpvNv3qv0MBhSalIFSv3+MCJCY8fUaCxasycrMu9dp5VTUhubXZ6CqsAvn66ZAsx+e8fON37oT3bZ4aiRU0kDlavGSrQI95HPBZACt8FuCL16IruBVkzBeBW+CLYHvyfiDQd96IpHpOMqP++fAq9VMQ7rwrM5+6gjwG/3y/D/m8ylPYpvpAeqO7DfvwMPO9D6jnBabG56YFZx77fEjD+9AyLdYEhp8nxoNh5wmJtfY8AS+wzIZZrJNcC4h3wV8IH1p87Iyfnc6XVaAd7ICHAsebw97zgDSxZ/yvViiPuYUfTe650knM2+7Ywz9yOCklzohLOwAAAABJRU5ErkJggg==\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-zoomStop{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAMAAABg3Am1AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAzUExURUdwTP///////////////////////////////////////////////////////////////8/RimEAAAAQdFJOUwDjmTC6Y0KBVakX8s8iC3JYR1wyAAABVUlEQVRIx62V2baFIAiGcwJRS97/aU/Zbtht0VzrcJOuvl/AAabpXy1RF0F/m2h2PX5mzucEuO9gMon1MY7KlG8w4LImC1XFQmxDGVkm1DYpXi0qS1gXTEGzKoqNS1ajAxPqJDikFLcFS+aukYJffRfXnAj9kYUXFVi86wzzPjuCFRXBwS3KaC/Pt02Ww7uf1jz3BVYtX/OsyDQFDn7yswPXb+Ovs39eJJWwxrOXV4thgC+//QBf/uMAXwAa4AuRBvgdWR58CB2B+eaRG5e8QO7Jn2sIAvzhbdsDnXzev8q0BengGXv8EciH3y1CY5++yI/BRNx82g/La7lc7b0Am/tUC6n3HoZ4SbDyy5BgzSNivV6wbPXeIPOpHlOU+CjkTQKvQOpXdRdJvn+5xttWQa4EZUOzluonT6+q72X6Zb0+T/lNhQd7hg9vm8LWUhW5gT4iN7c/0ZUo8Q3AttYAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-zoomStop:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAqUExURUdwTBKV2hGW2xKW2xKW2xKW2xGV3BGW2xGW3BGX2xKW3BKW2xCX2xKW2yDN61kAAAANdFJOUwCAVKxm3T/DmhrxKw97YQBGAAABaklEQVQ4y2NgIA6wGqCLMCqAqV4HdIm9F0Ek22UMM9hiG4AkVzQDA+cmx+YyJJnDlw8wMNQaNpfevbu0QgxJgrN3JQPD3VvhjU57kESnOZqvvXuJgcEXxVFMEkDt1w11gZYwoVgue3d5SzaQSgCydZG1TMkBU8vBuq9guBcqNBNDgj0GQrMEb0CVYEmA2ViANRQZ795tQPZduADciXcVUNSthGu4hGrAJawagBIXsWoAClzGqgEocRWrBqDIrQNQDTPRXZ8A0cB41wBNQgGiQfbubTQJAagGDB0GIA0OsnevbkCTCADiu4J374ahW34JqAEIbiXMRJOAgkvMKJYgJBx6717AKiF4F4eOu+jxgQCXGHBIKPDgkBC8JYCURJDtQHYWN4oEcvpZiyR+C9lZtkgSKKG1CyGOGrxItleixjrcLFBWRAYcMIkG9NQMiY67N7AlcxAQwMwA6SBxM2xZQ3jtVRPsRQ83sg8ACMIUxzzE8wsAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-close{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAeUExURUdwTM3Nzc7Ozs7Ozs3Nzc3NzcrKys3Nzc3Nzc3NzePZJxkAAAAJdFJOUwCtKdPBGAmNTt3jdDcAAADfSURBVDjL1dOxDoIwEADQgwR07GTCZtI/IGHgAzBhM9EPkMTB0Y3V0ZXIcn/rtRR6pefgSKeSu3ellyvA9lZ/5F9p/3K7PZY8oPG5BD6MpPUSgIITzdIStifAshjRQV1PCFT8TxaicTzzwEwINOEdHVmDmcTAkRhMhMAp7iQRjcMtDhCp8SA1v0ARGIIK/gnkv0p1OBTS4QRUIpE7DiYYXTBrzcld3JIrAarXrps4AVNwRSZgExoJmIyAaAdsShUMn/JF2fh4YEkpAcgvnuwYCIb6EbbbP4PsDfLD2dD6Av1qTvAQlzUTAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-close:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAeUExURUdwTP///////////////////////////////////8kBMKEAAAAJdFJOUwCt0igUwJdJePGbgLgAAADcSURBVDjL1ZMxEoIwFESTCaOWFDapqeicnAALTiANnaWlnVewDTMW/7b+aCAb8jkAVIHN2/lsNkrt73lf8M08nnF1pAYFR/dFmAAx7SIoi4iDbRrWDMAuQFzmmxAGbjjJgjj6dCjMCAND/o8RWQMzUgIRKYE/wsC5TJIRR74rBUZaqqXwLZEXT0WTDGwLW1aavJWQir9qadw++NgykWoMNtcykh8Q5EECgr5C+jjpGjHjPGhPU5eVzyfPJitfnUyhPg6ywMKZ7BygcYcsPCj1Kc8uXYPqpeSLs6PnC4w8S+8OJ9MLAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-narrow{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAABm1JREFUaEPVWWuIVVUUXmvfYRosHzUZUUqKjkSXSe5Z+5qJhfawLELCR5ohPhKjjB4EWgkpkopRSWEPS1Mqy7Gkd0iPG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lqrZ8EgGKTE0nAGPM0M9+Wj8BhKWV9HLjrukuFEPMBoMf+HKdrm3oimhcnFyy1IwkopRYj4soCYF1dXf3JPAMKfiilNiFisaSOJOC67nQhxCu+h3i0lLI1boWqPW+M6WDmoXk7+4io8L3rr+IuZIwZycztPocWEdHqajtYCl8pdSEifuOT2UhEc/w63fZUrfWvADCwixlii+M4408mAa31AgB4xufDHCLaGElAKbUZEf09cAMR/XCySGit7TXO3IJ9IcTQYI8cLKfvQsQ1BYVq9sBxi9La2jqipqbmMwA4Oy8bWtoHCTiIaBua0/JKB5hZSikPxhms9LxSah0i2i26MHoUct0eYt+2tRIRF5/MKGit7YFqD9auYU9tRBwfls49emJjzEBm3gEAxVKCmadJKbdWepWj8LTWHwLAlb75hUS0Nkw+6lbidkTspoCI6aibgUoSU0otR8QlBUxE3Oo4TleNlpiAFQwWUfa/XC7XZ8yYMccq6bAfS2v9OADcHUidqUT0RdkE8iS+BIBsQHkkEe2qNAml1HOIeGsA914isqQiR5LL3aMAcHoAYTIR2av0Ex7t7e3ne563mplvDoIh4le2xC+VurEE8pGwh1m3/sBec3ie96iUcndvWLS3t4/o7OycK4SYw8znRKZIDIlEBPIk7IrfGDB01JIAgJZsNtuShIjruplUKmWdtidsMLK2lFmFiPP8fUapSCQmYJ2L6cDsNfh7iLiXme1VjG3GOwFguOd5DYg4HAAa7O/Q3QTRXiavchxnhzHmIgCw3V6xWYoiURYBa9h13atTqdTssJxNEoEImQeJaIV/LimJsgkUjCilLrGXToho33mFrmoMod2I+LLneW9KKb8Nk01CotcE/AaVUmOFENcz8zgAGAIA54Y4dAgADiCifejtjcQnSSIWRgIAniIi+5q3cm/q/c40NzfX9e3bd4gQ4kxEPFhbW3sgnU7/lcThJJHwV8kViUBvHStHz0aCme2qe7W1tasbGxu7rkFPGQKR50Q5q/B/lP0HjgOoT/ydvaYAAAAASUVORK5CYII=\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-narrow:hover{background:url(\"data:image/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YXhQVWva7CkCkyg/nAIyRtKJuhXo9bjuSyOFJzypJ2ff/XCqHdBTwXs6gmZLm9NrAKvm29wY+zc1ZKGlqnqeYzH0PbJ8mLJM0dpABTAfuyNkwVdLCKgDFGniEpC8HC4TtaONEOyej4cUaubgDFwLzcgw9q4HrFsV2pAxvANuluaWpfRHAAUAUNJsmpmhrjJa0tk5ht8dtRx0cITqjAYlcv5c49zJHKI2QmtEG3wXbcaDGwZrRamBsmTuX1cTxEi8H8qnEJElLur3KreTZfgk4Mjc+Q9L8svmtuhLnAUWGka06A90EZjt6UdGTymiJpCxHG6CqqrHVL4lKnMMkRaXWE7J9M3BRwXUmSnqrlcK61uLbwIEF5lGSou3XVbJ9F3BOQeglkgJUS2rS3P0F2LwgYYKkaKWvN9neGYgT//QSYR9Hil/lurUAQqjtOMyK9UEkWXMlfdgJihTn45CK1GCHChmVIBoBSCBixU8qKIrdmRuXG5LigqOWbO+fjA7jizsbqcxsINLsfJ3REkRjAAlEVQUWbfDncjc0UYz/BewJjCh8lgGNZvJsSctt75MabLUg2gKQQBydSrwyn63dgRYTrpQUdwR91BRE2wAyDbYPSk2nuPOKVW6X4t15EHhS0mdlzE1AdAygsFpxwX0CEJcUuwM7lhgULhW5VRgeHYnXmiBuAeI2SXHN272b+gKgzRKQrYFIBNdI+qOJwQ13oi8/68oOdGpYO3xpJ2LV/45zI/t3hSEDoBXYIQ/gH99H3EBePlczAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-expand{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAABvJJREFUaEPVWWmIXEUQrnqzZseYxGM9QBNF4hrJEDfzqieuAY/gFRXPyMYbzUHwQBQUzx+KeBAJKhrRqEhEQWMU75AYXREPlunu3bjxIDGraIwiuJpFkL26pMK84eXtm3lvZieGFAzsTtf1VVd3V9Ug7OOE+7j/0BAAnZ2d2UmTJ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SsMwoCEVf4vr+re2zIDqSJ5Hh5BAQzS9R/IiJpf3fRPgOg4jsx3sjsbfn/AH37LF5g3/BiAAAAAElFTkSuQmCC\") no-repeat 50%;background-size:100% 100%}.jb-pro-container .jb-pro-icon-expand:hover{background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAABYZJREFUaEPVmXmoVVUUxn8f0WRzVkJlIWVGUmIJWWADTVaUldFclCnSQBQYNv1RSCWGVFRSlkRh0GBS2YBmJWGJUBHNNJiYWQSZfwTRxBfrce5jv/3Ovefc++4z3oKLD/fea61v7bXXdMQQJw1x/ekKANs7AOcA44Ddkt/2wJbk9z2wTNIP3TLcgADYvhw4q/gFiLq0ClgBLJf0Ud1DZfs6AmD7UuA64JiBCAf+BR4GHpH0TSe82gJgO6x9PXBqJ8JanPktQABzJP3VDu/aAGzfADzYhPnPQLjFUuA7YHPx+xPYHxhZ/Bt/nw4c14TP+8CFkjbWBVELgO2ngfD3nJYBCyW9Wldg7LN9cigKXATsnJ39HTi+7tuoBGDb3VI852P7QOBO4MoSGaMlfVtlmJYAbL8HHJsxmSdpdhXjdtZt3wPcWnJmhKRfWvFqCsD2PODm7PBdksJiXSfbJwDvZIzfAM6VFG+plEoBFNHmlezEc5LCZweNbEcSjMSX0iJJ09sFsDwLlaslTRo0zRPGts8AXstkTZYUOvWjfjdQJKnFyc7PgCmS1m0NACHD9t3AbYm8yNiT6wKIWJxm2JmSFm4t5QsA2wBvZ/liuqRFuR59bsD2JcAzyaYPJU1oV3nbF8fjAw4DlnTy8Es8YYWk06oAPJnF5I6sb/tF4LxC2CZJ+3VghGFAVK17JmdHSVqf8uq9AdtR+kZNsmOx4Y9I/5KiLGiLbEc4jLDYQ5IqE2aZANvxFqNwbNBNkh5oBiAsFpZr0FJJU9vSvNjcRQBRPKbhfJWkE5sBuBe4JVmcIemJ/xNAyLb9a+JGGyRF+dFLqQstAK5J1g6XFCG0berWD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}.jb-pro-container .jb-pro-menu-icon-text,.jb-pro-container .jb-pro-quality-icon-text,.jb-pro-container .jb-pro-scale-icon-text,.jb-pro-container .jb-pro-speed-icon-text{font-size:14px;min-width:30px;height:20px;line-height:20px;cursor:pointer;text-align:center}.jb-pro-container .jb-pro-speed{box-sizing:border-box;text-align:center;font-size:14px;color:#fff;width:90px}.jb-pro-container .jb-pro-menu-list,.jb-pro-container .jb-pro-quality-menu-list,.jb-pro-container .jb-pro-scale-menu-list,.jb-pro-container .jb-pro-speed-menu-list{position:absolute;left:50%;bottom:100%;visibility:hidden;opacity:0;transform:translateX(-50%);transition:visibility .3s,opacity .3s;background-color:rgba(0,0,0,.5);border-radius:4px;overflow:hidden;width:-moz-max-content;width:max-content}.jb-pro-container .jb-pro-menu-list.jb-pro-menu-shown,.jb-pro-container .jb-pro-menu-list.jb-pro-quality-menu-shown,.jb-pro-container .jb-pro-menu-list.jb-pro-scale-menu-shown,.jb-pro-container .jb-pro-menu-list.jb-pro-speed-menu-shown,.jb-pro-container .jb-pro-quality-menu-list.jb-pro-menu-shown,.jb-pro-container .jb-pro-quality-menu-list.jb-pro-quality-menu-shown,.jb-pro-container .jb-pro-quality-menu-list.jb-pro-scale-menu-shown,.jb-pro-container .jb-pro-quality-menu-list.jb-pro-speed-menu-shown,.jb-pro-container .jb-pro-scale-menu-list.jb-pro-menu-shown,.jb-pro-container .jb-pro-scale-menu-list.jb-pro-quality-menu-shown,.jb-pro-container .jb-pro-scale-menu-list.jb-pro-scale-menu-shown,.jb-pro-container .jb-pro-scale-menu-list.jb-pro-speed-menu-shown,.jb-pro-container .jb-pro-speed-menu-list.jb-pro-menu-shown,.jb-pro-container .jb-pro-speed-menu-list.jb-pro-quality-menu-shown,.jb-pro-container .jb-pro-speed-menu-list.jb-pro-scale-menu-shown,.jb-pro-container .jb-pro-speed-menu-list.jb-pro-speed-menu-shown{visibility:visible;opacity:1}.jb-pro-container .icon-title-tips{pointer-events:none;position:absolute;left:50%;bottom:100%;visibility:hidden;opacity:0;transform:translateX(-50%);transition:visibility .3s ease 0s,opacity .3s ease 0s;background-color:rgba(0,0,0,.5);border-radius:4px}.jb-pro-container .icon-title{display:inline-block;padding:5px 10px;font-size:12px;white-space:nowrap;color:#fff}.jb-pro-container .jb-pro-quality-menu{padding:8px 0}.jb-pro-container .jb-pro-menu-item,.jb-pro-container .jb-pro-quality-menu-item,.jb-pro-container .jb-pro-scale-menu-item,.jb-pro-container .jb-pro-speed-menu-item{display:block;height:25px;line-height:25px;margin:0;padding:0 10px;cursor:pointer;font-size:14px;text-align:center;width:50px;color:hsla(0,0%,100%,.5);transition:color .3s,background-color .3s}.jb-pro-container .jb-pro-menu-item:hover,.jb-pro-container .jb-pro-quality-menu-item:hover,.jb-pro-container .jb-pro-scale-menu-item:hover,.jb-pro-container .jb-pro-speed-menu-item:hover{background-color:hsla(0,0%,100%,.2)}.jb-pro-container .jb-pro-menu-item:focus,.jb-pro-container .jb-pro-quality-menu-item:focus,.jb-pro-container .jb-pro-scale-menu-item:focus,.jb-pro-container .jb-pro-speed-menu-item:focus{outline:none}.jb-pro-container .jb-pro-menu-item.jb-pro-menu-item-active,.jb-pro-container .jb-pro-menu-item.jb-pro-quality-menu-item-active,.jb-pro-container .jb-pro-menu-item.jb-pro-scale-menu-item-active,.jb-pro-container .jb-pro-menu-item.jb-pro-speed-menu-item-active,.jb-pro-container .jb-pro-quality-menu-item.jb-pro-menu-item-active,.jb-pro-container .jb-pro-quality-menu-item.jb-pro-quality-menu-item-active,.jb-pro-container .jb-pro-quality-menu-item.jb-pro-scale-menu-item-active,.jb-pro-container .jb-pro-quality-menu-item.jb-pro-speed-menu-item-active,.jb-pro-container .jb-pro-scale-menu-item.jb-pro-menu-item-active,.jb-pro-container .jb-pro-scale-menu-item.jb-pro-quality-menu-item-active,.jb-pro-container .jb-pro-scale-menu-item.jb-pro-scale-menu-item-active,.jb-pro-container .jb-pro-scale-menu-item.jb-pro-speed-menu-item-active,.jb-pro-container .jb-pro-speed-menu-item.jb-pro-menu-item-active,.jb-pro-container .jb-pro-speed-menu-item.jb-pro-quality-menu-item-active,.jb-pro-container .jb-pro-speed-menu-item.jb-pro-scale-menu-item-active,.jb-pro-container .jb-pro-speed-menu-item.jb-pro-speed-menu-item-active{color:#2298fc}.jb-pro-container .jb-pro-volume-panel-wrap{position:absolute;left:50%;bottom:100%;visibility:hidden;opacity:0;transform:translateX(-50%) translateY(22%);transition:visibility .3s,opacity .3s;background-color:rgba(0,0,0,.5);border-radius:4px;height:120px;width:50px;overflow:hidden}.jb-pro-container .jb-pro-volume-panel-wrap.jb-pro-volume-panel-wrap-show{visibility:visible;opacity:1}.jb-pro-container .jb-pro-volume-panel{cursor:pointer;position:absolute;top:21px;height:60px;width:50px;overflow:hidden}.jb-pro-container .jb-pro-volume-panel-text{position:absolute;left:0;top:0;width:50px;height:20px;line-height:20px;text-align:center;color:#fff;font-size:12px}.jb-pro-container .jb-pro-volume-panel-handle{position:absolute;top:48px;left:50%;width:12px;height:12px;border-radius:12px;margin-left:-6px;background:#fff}.jb-pro-container .jb-pro-volume-panel-handle:before{bottom:-54px;background:#fff}.jb-pro-container .jb-pro-volume-panel-handle:after{bottom:6px;background:hsla(0,0%,100%,.2)}.jb-pro-container .jb-pro-volume-panel-handle:after,.jb-pro-container .jb-pro-volume-panel-handle:before{content:\"\";position:absolute;display:block;left:50%;width:3px;margin-left:-1px;height:60px}.jb-pro-container.jb-pro-fullscreen-web .jb-pro-controls{width:100vh}.jb-pro-container.jb-pro-fullscreen-web .jb-pro-play-big:after{transform:translate(-50%,-50%) rotate(270deg)}.jb-pro-container.jb-pro-fullscreen-web .jb-pro-loading{flex-direction:row}.jb-pro-container.jb-pro-fullscreen-web .jb-pro-loading-text{transform:rotate(270deg)}.jb-pro-container .jb-pro-contextmenus{display:none;flex-direction:column;position:absolute;z-index:120;left:10px;top:10px;min-width:200px;padding:5px 0;background-color:rgba(0,0,0,.9);border-radius:3px}.jb-pro-container .jb-pro-contextmenus .jb-pro-contextmenu{cursor:pointer;font-size:12px;display:block;color:#fff;padding:10px 15px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;text-shadow:0 0 2px rgba(0,0,0,.5);border-bottom:1px solid hsla(0,0%,100%,.1)}.jb-pro-container .jb-pro-contextmenus .jb-pro-contextmenu a{color:#fff;text-decoration:none}.jb-pro-container .jb-pro-contextmenus .jb-pro-contextmenu span{display:inline-block;padding:0 7px}.jb-pro-container .jb-pro-contextmenus .jb-pro-contextmenu span.art-current,.jb-pro-container .jb-pro-contextmenus .jb-pro-contextmenu span:hover{color:var(--theme)}.jb-pro-container .jb-pro-contextmenus .jb-pro-contextmenu:hover{background-color:hsla(0,0%,100%,.1)}.jb-pro-container .jb-pro-contextmenus .jb-pro-contextmenu:last-child{border-bottom:none}.jb-pro-container.jb-pro-contextmenus-show .jb-pro-contextmenus{display:flex}.jb-pro-container .jb-pro-extend-dom{display:block;position:relative;width:100%;height:100%;display:none}.jb-pro-container-playback .jb-pro-controls{height:48px}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center{flex:1;display:flex;box-sizing:border-box;justify-content:space-between;font-size:12px}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-controls-playback-time{box-sizing:border-box;flex:1;position:relative;height:100%}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-controls-playback-time-inner{width:300px;height:100%;overflow-y:hidden;overflow-x:auto}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-controls-playback-current-time{position:absolute;left:0;top:0;height:15px;width:1px;background-color:red;text-align:center;z-index:1}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-controls-playback-current-time-text{position:absolute;box-sizing:border-box;padding:0 5px;width:60px;left:-25px;top:15px;border:1px solid red;height:15px;line-height:15px;cursor:move;background-color:#fff;color:#000;-webkit-user-select:none;-moz-user-select:none;user-select:none}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-controls-playback-time-scroll{position:relative;width:1440px;margin:0 auto}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-controls-playback-time-scroll.one-hour{width:1440px}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-controls-playback-time-scroll.half-hour{width:2880px}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-controls-playback-time-scroll.ten-min{width:8640px}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-controls-playback-time-scroll.five-min{width:17280px}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-controls-playback-time-scroll.one-min{width:86400px}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-controls-playback-time-list{position:relative;background-color:#ccc;height:48px}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-day{height:100%;overflow:hidden}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-one-wrap{height:8px;z-index:1}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-second-wrap{height:25px}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-controls-playback-btns{display:flex;align-items:center}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-minute-one,.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-second-one{float:left;width:1px;height:8px;margin:0;cursor:default;position:relative;z-index:1}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-minute-one.active,.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-second-one.active{background-color:orange;cursor:pointer}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-minute-one.start,.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-second-one.start{background-color:#999}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-minute-one:hover .jb-pro-playback-time-title-tips,.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-second-one:hover .jb-pro-playback-time-title-tips{visibility:visible;opacity:1}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-title-tips{pointer-events:none;position:absolute;left:0;top:100%;visibility:hidden;opacity:0;transform:translateX(13%);transition:visibility .3s ease 0s,opacity .3s ease 0s;background-color:#000;border-radius:4px;z-index:1}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-title-tips.jb-pro-playback-time-title-tips-left{transform:translateX(-100%)}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-title-tips .jb-pro-playback-time-title{display:inline-block;padding:2px 5px;font-size:12px;white-space:nowrap;color:#fff}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-hour,.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-minute{float:left;position:relative;width:60px;box-sizing:border-box;border-top:1px solid #999;-webkit-user-select:none;-moz-user-select:none;user-select:none;text-align:left;height:25px;line-height:25px}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-hour:first-child,.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-minute:first-child{border-left:0}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-hour:first-child .jb-pro-playback-time-hour-text,.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-minute:first-child .jb-pro-playback-time-hour-text{left:0}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-hour:after,.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-minute:after{content:\"\";position:absolute;left:0;top:-8px;width:1px;height:14px;background-color:#999}.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-hour-text,.jb-pro-container-playback .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-center .jb-pro-playback-time-minute-text{position:absolute;left:-13px}.jb-pro-container-playback .jb-pro-playback-expand.disabled .jb-pro-icon-expand,.jb-pro-container-playback .jb-pro-playback-narrow.disabled .jb-pro-icon-narrow{cursor:no-drop}.jb-pro-container-playback .jb-pro-control-progress-simple{position:absolute;box-sizing:border-box;left:0;top:-2px;width:100%;display:flex;flex-direction:row;align-items:center;height:8px;cursor:pointer}.jb-pro-container-playback .jb-pro-control-progress-simple:hover{top:0;align-items:flex-start}.jb-pro-container-playback .jb-pro-control-progress-simple:hover .jb-pro-control-progress-inner{height:100%}.jb-pro-container-playback .jb-pro-control-progress-simple:hover .jb-pro-control-progress-inner .jb-pro-progress-indicator{transform:scale(1);visibility:visible}.jb-pro-container-playback .jb-pro-control-progress-inner{display:flex;align-items:center;position:relative;height:50%;width:100%;transition:all .2s ease;background:hsla(0,0%,100%,.5)}.jb-pro-container-playback .jb-pro-progress-hover{display:none;width:0}.jb-pro-container-playback .jb-pro-progress-played{position:absolute;left:0;top:0;right:0;bottom:0;height:100%;width:0;background-color:orange}.jb-pro-container-playback .jb-pro-progress-indicator{visibility:hidden;align-items:center;justify-content:center;position:absolute;z-index:40;border-radius:50%;transform:scale(.1);transition:transform .1s ease-in-out}.jb-pro-container-playback .jb-pro-progress-indicator .jb-pro-icon{width:100%;height:100%;pointer-events:none;-webkit-user-select:none;-moz-user-select:none;user-select:none}.jb-pro-container-playback .jb-pro-progress-indicator:hover{transform:scale(1.2)!important}.jb-pro-container-playback .jb-pro-progress-tip{display:none;position:absolute;z-index:50;top:-25px;left:0;height:20px;padding:0 5px;line-height:20px;color:#fff;font-size:12px;text-align:center;background:rgba(0,0,0,.7);border-radius:3px;font-weight:700;white-space:nowrap}.jb-pro-container-playback.jb-pro-fullscreen-web .jb-pro-controls .jb-pro-controls-bottom .jb-pro-controls-playback-time-inner{overflow-y:auto}.jb-pro-zoom-control{cursor:grab}.jb-pro-performance-panel{position:absolute;box-sizing:border-box;z-index:10000;left:0;top:0;padding:5px;font-size:10px;background:rgba(0,0,0,.2);color:#fff;max-height:100%;overflow-y:auto;display:none}.jb-pro-performance-panel .jb-pro-performance-item{display:flex;align-items:center;margin-top:3px;color:#fff}.jb-pro-performance-panel .jb-pro-performance-item-block{height:10px}.jb-pro-tips-message{position:absolute;top:0;left:0;width:100%;height:100%;background:linear-gradient(180deg,rgba(0,0,0,.8),hsla(0,0%,100%,0));overflow:auto;box-sizing:content-box;display:none}.jb-pro-tips-message:before{color:hsla(0,0%,100%,.3);content:\"X\";font-family:Arial,Helvetica,sans-serif;font-size:40px;left:0;line-height:1;margin-top:-20px;position:absolute;text-shadow:2em 2em 4em #000;text-align:center;top:50%;vertical-align:middle;width:100%}.jb-pro-tips-message .jb-pro-tips-message-close{position:absolute;z-index:99999;right:0;top:0;width:40px;height:40px;display:flex;align-items:center;justify-content:center}.jb-pro-tips-message .jb-pro-tips-message-close .jb-pro-tips-message-close-icon{width:20px;height:20px;border-radius:10px;cursor:pointer;background:url(\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAeUExURUdwTM3Nzc7Ozs7Ozs3Nzc3NzcrKys3Nzc3Nzc3NzePZJxkAAAAJdFJOUwCtKdPBGAmNTt3jdDcAAADfSURBVDjL1dOxDoIwEADQgwR07GTCZtI/IGHgAzBhM9EPkMTB0Y3V0ZXIcn/rtRR6pefgSKeSu3ellyvA9lZ/5F9p/3K7PZY8oPG5BD6MpPUSgIITzdIStifAshjRQV1PCFT8TxaicTzzwEwINOEdHVmDmcTAkRhMhMAp7iQRjcMtDhCp8SA1v0ARGIIK/gnkv0p1OBTS4QRUIpE7DiYYXTBrzcld3JIrAarXrps4AVNwRSZgExoJmIyAaAdsShUMn/JF2fh4YEkpAcgvnuwYCIb6EbbbP4PsDfLD2dD6Av1qTvAQlzUTAAAAAElFTkSuQmCC\") no-repeat 50%;background-color:#fff;background-size:100% 100%}.jb-pro-tips-message .jb-pro-tips-message-content{overflow:auto;padding:35px;box-sizing:border-box;width:100%;height:100%}.jb-pro-tips-message .jb-pro-tips-message-content .jb-pro-tips-message-content-item{font-size:14px;color:#fff;text-align:center;line-height:1.5}\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0eWxlLnNjc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsb0JBQ0UsR0FDRSw4QkFBaUMsQ0FDbkMsR0FDRSwrQkFBbUMsQ0FBRSxDQUV6Qyx3QkFDRSxHQUNFLHdCQUF5QixDQUN6QiwrQkFBa0MsQ0FDcEMsSUFDRSx3QkFBeUIsQ0FDekIsbUNBQXNDLENBQ3hDLEdBQ0Usd0JBQXlCLENBQ3pCLCtCQUFrQyxDQUFFLENBRXhDLGdEQUNFLHNCQUEwQixDQUU1QiwrQkFDRSxjQUFlLENBQ2YsVUFBVyxDQUNYLFdBQVksQ0FDWixvQkFBdUIsQ0FFek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AgICBkaXNwbGF5OiBub25lOyB9XG4gICAgLmpiLXByby1jb250YWluZXIgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWl0ZW0uamItcHJvLWljb24tYXVkaW8ge1xuICAgICAgZGlzcGxheTogbm9uZTsgfVxuICAgIC5qYi1wcm8tY29udGFpbmVyIC5qYi1wcm8tY29udHJvbHMgLmpiLXByby1jb250cm9scy1pdGVtLmpiLXByby1wbGF5IHtcbiAgICAgIGRpc3BsYXk6IG5vbmU7IH1cbiAgICAuamItcHJvLWNvbnRhaW5lciAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtaXRlbS5qYi1wcm8tcGF1c2Uge1xuICAgICAgZGlzcGxheTogbm9uZTsgfVxuICAgIC5qYi1wcm8tY29udGFpbmVyIC5qYi1wcm8tY29udHJvbHMgLmpiLXByby1jb250cm9scy1pdGVtLmpiLXByby1mdWxsc2NyZWVuLWV4aXQge1xuICAgICAgZGlzcGxheTogbm9uZTsgfVxuICAgIC5qYi1wcm8tY29udGFpbmVyIC5qYi1wcm8tY29udHJvbHMgLmpiLXByby1jb250cm9scy1pdGVtLmpiLXByby1zY3JlZW5zaG90IHtcbiAgICAgIGRpc3BsYXk6IG5vbmU7IH1cbiAgICAuamItcHJvLWNvbnRhaW5lciAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtaXRlbS5qYi1wcm8tcHR6IHtcbiAgICAgIGRpc3BsYXk6IG5vbmU7IH1cbiAgICAuamItcHJvLWNvbnRhaW5lciAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtaXRlbS5qYi1wcm8tcGVyZm9ybWFuY2Uge1xuICAgICAgZGlzcGxheTogbm9uZTsgfVxuICAgIC5qYi1wcm8tY29udGFpbmVyIC5qYi1wcm8tY29udHJvbHMgLmpiLXByby1jb250cm9scy1pdGVtLmpiLXByby1mYWNlIHtcbiAgICAgIGRpc3BsYXk6IG5vbmU7IH1cbiA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yB9XG4gICAgLmpiLXByby1jb250YWluZXIgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWl0ZW0uamItcHJvLXBlcmZvcm1hbmNlLWFjdGl2ZSB7XG4gICAgICBkaXNwbGF5OiBub25lOyB9XG4gICAgLmpiLXByby1jb250YWluZXIgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWl0ZW0uamItcHJvLWZhY2UtYWN0aXZlIHtcbiAgICAgIGRpc3BsYXk6IG5vbmU7IH1cbiAgICAuamItcHJvLWNvbnRhaW5lciAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtaXRlbS5qYi1wcm8tb2JqZWN0LWFjdGl2ZSB7XG4gICAgICBkaXNwbGF5OiBub25lOyB9XG4gICAgLmpiLXByby1jb250YWluZXIgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWl0ZW0uamItcHJvLW9jY2x1c2lvbi1hY3RpdmUge1xuICAgICAgZGlzcGxheTogbm9uZTsgfVxuICAgIC5qYi1wcm8tY29udGFpbmVyIC5qYi1wcm8tY29udHJvbHMgLmpiLXByby1jb250cm9scy1pdGVtLmpiLXByby1yZWNvcmQge1xuICAgICAgZGlzcGxheTogbm9uZTsgfVxuICAgIC5qYi1wcm8tY29udGFpbmVyIC5qYi1wcm8tY29udHJvbHMgLmpiLXByby1jb250cm9scy1pdGVtLmpiLXByby1mdWxsc2NyZWVuIHtcbiAgICAgIGRpc3BsYXk6IG5vbmU7IH1cbiAgICAuamItcHJvLWNvbnRhaW5lciAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtaXRlbS5qYi1wcm8tcmVjb3JkLXN0b3Age1xuICAgICAgZGlzcGxheTogbm9uZTsgfVxuICAgIC5qYi1wcm8tY29udGFpbmVyIC5qYi1wcm8tY29udHJvbHMgLmpiLXByby1jb250cm9scy1pdGVtLmpiLXByby16b29tIHtcbiAgICAgIGRpc3BsYXk6IG5vbmU7IH1cbiAgICAuamItcHJvLWNvbnRhaW5lciAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtaXRlbS5qYi1wcm8tem9vbS1zdG9wIHtcbiAgICAgIGRpc3BsYXk6IG5vbmU7IH1cbiAgICAuamItcHJvLWNvbnRhaW5lciAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtaXRlbS5qYi1wcm8tc3BlZWQge1xuICAgICAgcGFkZGluZzogMDsgfVxuICAuamItcHJvLWNvbnRhaW5lciAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtaXRlbS1odG1sIHtcbiAgICBwb3NpdGlvbjogcmVsYXRpdmU7XG4gICAgZGlzcGxheTogbm9uZTtcbiAgICBqdXN0aWZ5LWNvbnRlbnQ6IGNlbnRlcjsgfVxuICAuamItcHJvLWNvbnRhaW5lciAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tcGxheWJhY2stY29udHJvbC10aW1lIHtcbiAgICBwb3NpdGlvbjogcmVsYXRpdmU7XG4gICAganVzdGlmeS1jb250ZW50OiBjZW50ZXI7XG4gICAgcGFkZGluZzogMCA4cHg7IH1cbiAgLmpiLXByby1jb250YWluZXIgLmpiLXByby1jb250cm9scyAuamItcHJvLWljb24tYXVkaW8sIC5qYi1wcm8tY29udGFpbmVyIC5qYi1wcm8tY29udHJvbHMgLmpiLXByby1pY29uLW11dGUge1xuICAgIHotaW5kZXg6IDE7IH1cbiAgLmpiLXByby1jb250YWluZXIgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWJvdHRvbSB7XG4gICAgZGlzcGxheTogZmxleDtcbiAgICBqdXN0aWZ5LWNvbnRlbnQ6IHNwYWNlLWJldHdlZW47XG4gICAgaGVpZ2h0OiAxMDAlOyB9XG4gICAgLmpiLXByby1jb250YWluZXIgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWJvdHRvbSAuamItcHJvLWNvbnRyb2xzLWx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c3QsIC5qYi1wcm8tY29udGFpbmVyIC5qYi1wcm8tc3BlZWQtbWVudS1saXN0IHtcbiAgcG9zaXRpb246IGFic29sdXRlO1xuICBsZWZ0OiA1MCU7XG4gIGJvdHRvbTogMTAwJTtcbiAgdmlzaWJpbGl0eTogaGlkZGVuO1xuICBvcGFjaXR5OiAwO1xuICB0cmFuc2Zvcm06IHRyYW5zbGF0ZVgoLTUwJSk7XG4gIHRyYW5zaXRpb246IHZpc2liaWxpdHkgMzAwbXMsIG9wYWNpdHkgMzAwbXM7XG4gIGJhY2tncm91bmQtY29sb3I6IHJnYmEoMCwgMCwgMCwgMC41KTtcbiAgYm9yZGVyLXJhZGl1czogNHB4O1xuICBvdmVyZmxvdzogaGlkZGVuO1xuICB3aWR0aDogbWF4LWNvbnRlbnQ7IH1cbiAgLmpiLXByby1jb250YWluZXIgLmpiLXByby1xdWFsaXR5LW1lbnUtbGlzdC5qYi1wcm8tcXVhbGl0eS1tZW51LXNob3duLCAuamItcHJvLWNvbnRhaW5lciAuamItcHJvLXF1YWxpdHktbWVudS1saXN0LmpiLXByby1zY2FsZS1tZW51LXNob3duLCAuamItcHJvLWNvbnRhaW5lciAuamItcHJvLXF1YWxpdHktbWVudS1saXN0LmpiLXByby1tZW51LXNob3duLCAuamItcHJvLWNvbnRhaW5lciAuamItcHJvLXF1YWxpdHktbWVudS1saXN0LmpiLXByby1zcGVlZC1tZW51LXNob3duLCAuamItcHJvLWNvbnRhaW5lciAuamItcHJvLXNjYWxlLW1lbnUtbGlzdC5qYi1wcm8tcXVhbGl0eS1tZW51LXNob3duLCAuamItcHJvLWNvbnRhaW5lciAuamItcHJvLXNjYWxlLW1lbnUtbGlzdC5qYi1wcm8tc2NhbGUtbWVudS1zaG93biwgLmpiLXByby1jb250YWluZXIgLmpiLXByby1zY2FsZS1tZW51LWxpc3QuamItcHJvLW1lbnUtc2hvd24sIC5qYi1wcm8tY29udGFpbmVyIC5qYi1wcm8tc2NhbGUtbWVudS1saXN0Lmp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piLXByby1jb250cm9scyB7XG4gIGhlaWdodDogNDhweDsgfVxuICAuamItcHJvLWNvbnRhaW5lci1wbGF5YmFjayAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtYm90dG9tIC5qYi1wcm8tY29udHJvbHMtY2VudGVyIHtcbiAgICBmbGV4OiAxO1xuICAgIGRpc3BsYXk6IGZsZXg7XG4gICAgYm94LXNpemluZzogYm9yZGVyLWJveDtcbiAgICBqdXN0aWZ5LWNvbnRlbnQ6IHNwYWNlLWJldHdlZW47XG4gICAgZm9udC1zaXplOiAxMnB4OyB9XG4gICAgLmpiLXByby1jb250YWluZXItcGxheWJhY2sgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWJvdHRvbSAuamItcHJvLWNvbnRyb2xzLWNlbnRlciAuamItcHJvLWNvbnRyb2xzLXBsYXliYWNrLXRpbWUge1xuICAgICAgYm94LXNpemluZzogYm9yZGVyLWJveDtcbiAgICAgIGZsZXg6IDE7XG4gICAgICBwb3NpdGlvbjogcmVsYXRpdmU7XG4gICAgICBoZWlnaHQ6IDEwMCU7IH1cbiAgICAuamItcHJvLWNvbnRhaW5lci1wbGF5YmFjayAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtYm90dG9tIC5qYi1wcm8tY29udHJvbHMtY2VudGVyIC5qYi1wcm8tY29udHJvbHMtcGxheWJhY2stdGltZS1pbm5lciB7XG4gICAgICB3aWR0aDogMzAwcHg7XG4gICAgICBoZWlnaHQ6IDEwMCU7XG4gICAgICBvdmVyZmxvdy15OiBoaWRkZW47XG4gICAgICBvdmVyZmxvdy14OiBhdXRvOyB9XG4gICAgLmpiLXByby1jb250YWluZXItcGxheWJhY2sgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWJvdHRvbSAuamItcHJvLWNvbnRyb2xzLWNlbnRlciAuamItcHJvLWNvbnRyb2xzLXBsYXliYWNrLWN1cnJlbnQtdGltZSB7XG4gICAgICBwb3NpdGlvbjogYWJzb2x1dGU7XG4gICAgICBsZWZ0OiAwcHg7XG4gICAgICB0b3A6IDA7XG4gICAgICBoZWlnaHQ6IDE1cHg7XG4gICAgICB3aWR0aDogMXB4O1xuICAgICAgYmFja2dyb3VuZC1jb2xvcjogcmVkO1xuICAgICAgdGV4dC1hbGlnbjogY2VudGVyO1xuICAgICAgei1pbmRleDogMTsgfVxuICAgIC5qYi1wcm8tY29udGFpbmVyLXBsYXliYWNrIC5qYi1wcm8tY29udHJvbHMgLmpiLXByby1jb250cm9scy1ib3R0b20gLmpiLXByby1jb250cm9scy1jZW50ZXIgLmpiLXByby1jb250cm9scy1wbGF5YmFjay1jdXJyZW50LXRpbWUtdGV4dCB7XG4gICAgICBwb3NpdGlvbjogYWJzb2x1dGU7XG4gICAgICBib3gtc2l6aW5nOiBib3JkZXItYm94O1xuICAgICAgcGFkZGluZzogMCA1cHg7XG4gICAgICB3aWR0aDogNjBweDtcbiAgICAgIGxlZnQ6IC0yNXB4O1xuICAgICAgdG9wOiAxNXB4O1xuICAgICAgYm9yZGVyOiAxcHggc29saWQgcmVkO1xuICAgICAgaGVpZ2h0OiAxNXB4O1xuICAgICAgbGluZS1oZWlnaHQ6IDE1cHg7XG4gICAgICBjdXJzb3I6IG1vdmU7XG4gICAgICBiYWNrZ3JvdW5kLWNvbG9yOiAjZmZmO1xuICAgICAgY29sb3I6ICMwMDA7XG4gICAgICAtbXMtdXNlci1zZWxlY3Q6IG5vbmU7XG4gICAgICAtd2Via2l0LXVzZXItc2VsZWN0OiBub25lO1xuICAgICAgLW1vei11c2VyLXNlbGVjdDogbm9uZTtcbiAgICAgIHVzZXItc2VsZWN0OiBub25lOyB9XG4gICAgLmpiLXByby1jb250YWluZXItcGxheWJhY2sgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWJvdHRvbSAuamItcHJvLWNvbnRyb2xzLWNlbnRlciAuamItcHJvLWNvbnRyb2xzLXBsYXliYWNrLXRpbWUtc2Nyb2xsIHtcbiAgICAgIHBvc2l0aW9uOiByZWxhdGl2ZTtcbiAgICAgIHdpZHRoOiAxNDQwcHg7XG4gICAgICBtYXJnaW46IDAgYXV0bzsgfVxuICAgICAgLmpiLXByby1jb250YWluZXItcGxheWJhY2sgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWJvdHRvbSAuamItcHJvLWNvbnRyb2xzLWNlbnRlciAuamItcHJvLWNvbnRyb2xzLXBsYXliYWNrLXRpbWUtc2Nyb2xsLm9uZS1ob3VyIHtcbiAgICAgICAgd2lkdGg6IDE0NDBweDsgfVxuICAgICAgLmpiLXByby1jb250YWluZXItcGxheWJhY2sgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWJvdHRvbSAuamItcHJvLWNvbnRyb2xzLWNlbnRlciAuamItcHJvLWNvbnRyb2xzLXBsYXliYWNrLXRpbWUtc2Nyb2xsLmhhbGYtaG91ciB7XG4gICAgICAgIHdpZHRoOiAyODgwcHg7IH1cbiAgICAgIC5qYi1wcm8tY29udGFpbmVyLXBsYXliYWNrIC5qYi1wcm8tY29udHJvbHMgLmpiLXByby1jb250cm9scy1ib3R0b20gLmpiLXByby1jb250cm9scy1jZW50ZXIgLmpiLXByby1jb250cm9scy1wbGF5YmFjay10aW1lLXNjcm9sbC50ZW4tbWluIHtcbiAgICAgICAgd2lkdGg6IDg2NDBweDsgfVxuICAgICAgLmpiLXByby1jb250YWluZXItcGxheWJhY2sgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWJvdHRvbSAuamItcHJvLWNvbnRyb2xzLWNlbnRlciAuamItcHJvLWNvbnRyb2xzLXBsYXliYWNrLXRpbWUtc2Nyb2xsLmZpdmUtbWluIHtcbiAgICAgICAgd2lkdGg6IDE3MjgwcHg7IH1cbiAgICAgIC5qYi1wcm8tY29udGFpbmVyLXBsYXliYWNrIC5qYi1wcm8tY29udHJvbHMgLmpiLXByby1jb250cm9scy1ib3R0b20gLmpiLXByby1jb250cm9scy1jZW50ZXIgLmpiLXByby1jb250cm9scy1wbGF5YmFjay10aW1lLXNjcm9sbC5vbmUtbWluIHtcbiAgICAgICAgd2lkdGg6IDg2NDAwcHg7IH1cbiAgICAuamItcHJvLWNvbnRhaW5lci1wbGF5YmFjayAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtYm90dG9tIC5qYi1wcm8tY29udHJvbHMtY2VudGVyIC5qYi1wcm8tY29udHJvbHMtcGxheWJhY2stdGltZS1saXN0IHtcbiAgICAgIHBvc2l0aW9uOiByZWxhdGl2ZTtcbiAgICAgIGJhY2tncm91bmQtY29sb3I6ICNjY2M7XG4gICAgICBoZWlnaHQ6IDQ4cHg7IH1cbiAgICAuamItcHJvLWNvbnRhaW5lci1wbGF5YmFjayAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtYm90dG9tIC5qYi1wcm8tY29udHJvbHMtY2VudGVyIC5qYi1wcm8tcGxheWJhY2stdGltZS1kYXkge1xuICAgICAgaGVpZ2h0OiAxMDAlO1xuICAgICAgb3ZlcmZsb3c6IGhpZGRlbjsgfVxuICAgIC5qYi1wcm8tY29udGFpbmVyLXBsYXliYWNrIC5qYi1wcm8tY29udHJvbHMgLmpiLXByby1jb250cm9scy1ib3R0b20gLmpiLXByby1jb250cm9scy1jZW50ZXIgLmpiLXByby1wbGF5YmFjay10aW1lLW9uZS13cmFwIHtcbiAgICAgIGhlaWdodDogOHB4O1xuICAgICAgei1pbmRleDogMTsgfVxuICAgIC5qYi1wcm8tY29udGFpbmVyLXBsYXliYWNrIC5qYi1wcm8tY29udHJvbHMgLmpiLXByby1jb250cm9scy1ib3R0b20gLmpiLXByby1jb250cm9scy1jZW50ZXIgLmpiLXByby1wbGF5YmFjay10aW1lLXNlY29uZC13cmFwIHtcbiAgICAgIGhlaWdodDogMjVweDsgfVxuICAgIC5qYi1wcm8tY29udGFpbmVyLXBsYXliYWNrIC5qYi1wcm8tY29udHJvbHMgLmpiLXByby1jb250cm9scy1ib3R0b20gLmpiLXByby1jb250cm9scy1jZW50ZXIgLmpiLXByby1jb250cm9scy1wbGF5YmFjay1idG5zIHtcbiAgICAgIGRpc3BsYXk6IGZsZXg7XG4gICAgICBhbGlnbi1pdGVtczogY2VudGVyOyB9XG4gICAgLmpiLXByby1jb250YWluZXItcGxheWJhY2sgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWJvdHRvbSAuamItcHJvLWNvbnRyb2xzLWNlbnRlciAuamItcHJvLXBsYXliYWNrLXRpbWUtbWludXRlLW9uZSwgLmpiLXByby1jb250YWluZXItcGxheWJhY2sgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWJvdHRvbSAuamItcHJvLWNvbnRyb2xzLWNlbnRlciAuamItcHJvLXBsYXliYWNrLXRpbWUtc2Vjb25kLW9uZSB7XG4gICAgICBmbG9hdDogbGVmdDtcbiAgICAgIHdpZHRoOiAxcHg7XG4gICAgICBoZWlnaHQ6IDhweDtcbiAgICAgIG1hcmdpbjogMDtcbiAgICAgIGN1cnNvcjogZGVmYXVsdDtcbiAgICAgIHBvc2l0aW9uOiByZWxhdGl2ZTtcbiAgICAgIHotaW5kZXg6IDE7IH1cbiAgICAgIC5qYi1wcm8tY29udGFpbmVyLXBsYXliYWNrIC5qYi1wcm8tY29udHJvbHMgLmpiLXByby1jb250cm9scy1ib3R0b20gLmpiLXByby1jb250cm9scy1jZW50ZXIgLmpiLXByby1wbGF5YmFjay10aW1lLW1pbnV0ZS1vbmUuYWN0aXZlLCAuamItcHJvLWNvbnRhaW5lci1wbGF5YmFjayAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtYm90dG9tIC5qYi1wcm8tY29udHJvbHMtY2VudGVyIC5qYi1wcm8tcGxheWJhY2stdGltZS1zZWNvbmQtb25lLmFjdGl2ZSB7XG4gICAgICAgIGJhY2tncm91bmQtY29sb3I6IG9yYW5nZTtcbiAgICAgICAgY3Vyc29yOiBwb2ludGVyOyB9XG4gICAgICAuamItcHJvLWNvbnRhaW5lci1wbGF5YmFjayAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtYm90dG9tIC5qYi1wcm8tY29udHJvbHMtY2VudGVyIC5qYi1wcm8tcGxheWJhY2stdGltZS1taW51dGUtb25lLnN0YXJ0LCAuamItcHJvLWNvbnRhaW5lci1wbGF5YmFjayAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtYm90dG9tIC5qYi1wcm8tY29udHJvbHMtY2VudGVyIC5qYi1wcm8tcGxheWJhY2stdGltZS1zZWNvbmQtb25lLnN0YXJ0IHtcbiAgICAgICAgYmFja2dyb3VuZC1jb2xvcjogIzk5OTsgfVxuICAgICAgLmpiLXByby1jb250YWluZXItcGxheWJhY2sgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWJvdHRvbSAuamItcHJvLWNvbnRyb2xzLWNlbnRlciAuamItcHJvLXBsYXliYWNrLXRpbWUtbWludXRlLW9uZTpob3ZlciAuamItcHJvLXBsYXliYWNrLXRpbWUtdGl0bGUtdGlwcywgLmpiLXByby1jb250YWluZXItcGxheWJhY2sgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWJvdHRvbSAuamItcHJvLWNvbnRyb2xzLWNlbnRlciAuamItcHJvLXBsYXliYWNrLXRpbWUtc2Vjb25kLW9uZTpob3ZlciAuamItcHJvLXBsYXliYWNrLXRpbWUtdGl0bGUtdGlwcyB7XG4gICAgICAgIHZpc2liaWxpdHk6IHZpc2libGU7XG4gICAgICAgIG9wYWNpdHk6IDE7IH1cbiAgICAuamItcHJvLWNvbnRhaW5lci1wbGF5YmFjayAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtYm90dG9tIC5qYi1wcm8tY29udHJvbHMtY2VudGVyIC5qYi1wcm8tcGxheWJhY2stdGltZS10aXRsZS10aXBzIHtcbiAgICAgIHBvaW50ZXItZXZlbnRzOiBub25lO1xuICAgICAgcG9zaXRpb246IGFic29sdXRlO1xuICAgICAgbGVmdDogMDtcbiAgICAgIHRvcDogMTAwJTtcbiAgICAgIHZpc2liaWxpdHk6IGhpZGRlbjtcbiAgICAgIG9wYWNpdHk6IDA7XG4gICAgICB0cmFuc2Zvcm06IHRyYW5zbGF0ZVgoMTMlKTtcbiAgICAgIHRyYW5zaXRpb246IHZpc2liaWxpdHkgMzAwbXMgZWFzZSAwcywgb3BhY2l0eSAzMDBtcyBlYXNlIDBzO1xuICAgICAgYmFja2dyb3VuZC1jb2xvcjogYmxhY2s7XG4gICAgICBib3JkZXItcmFkaXVzOiA0cHg7XG4gICAgICB6LWluZGV4OiAxOyB9XG4gICAgICAuamItcHJvLWNvbnRhaW5lci1wbGF5YmFjayAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtYm90dG9tIC5qYi1wcm8tY29udHJvbHMtY2VudGVyIC5qYi1wcm8tcGxheWJhY2stdGltZS10aXRsZS10aXBzLmpiLXByby1wbGF5YmFjay10aW1lLXRpdGxlLXRpcHMtbGVmdCB7XG4gICAgICAgIHRyYW5zZm9ybTogdHJhbnNsYXRlWCgtMTAwJSk7IH1cbiAgICAgIC5qYi1wcm8tY29udGFpbmVyLXBsYXliYWNrIC5qYi1wcm8tY29udHJvbHMgLmpiLXByby1jb250cm9scy1ib3R0b20gLmpiLXByby1jb250cm9scy1jZW50ZXIgLmpiLXByby1wbGF5YmFjay10aW1lLXRpdGxlLXRpcHMgLmpiLXByby1wbGF5YmFjay10aW1lLXRpdGxlIHtcbiAgICAgICAgZGlzcGxheTogaW5saW5lLWJsb2NrO1xuICAgICAgICBwYWRkaW5nOiAycHggNXB4O1xuICAgICAgICBmb250LXNpemU6IDEycHg7XG4gICAgICAgIHdoaXRlLXNwYWNlOiBub3dyYXA7XG4gICAgICAgIGNvbG9yOiB3aGl0ZTsgfVxuICAgIC5qYi1wcm8tY29udGFpbmVyLXBsYXliYWNrIC5qYi1wcm8tY29udHJvbHMgLmpiLXByby1jb250cm9scy1ib3R0b20gLmpiLXByby1jb250cm9scy1jZW50ZXIgLmpiLXByby1wbGF5YmFjay10aW1lLWhvdXIsIC5qYi1wcm8tY29udGFpbmVyLXBsYXliYWNrIC5qYi1wcm8tY29udHJvbHMgLmpiLXByby1jb250cm9scy1ib3R0b20gLmpiLXByby1jb250cm9scy1jZW50ZXIgLmpiLXByby1wbGF5YmFjay10aW1lLW1pbnV0ZSB7XG4gICAgICBmbG9hdDogbGVmdDtcbiAgICAgIHBvc2l0aW9uOiByZWxhdGl2ZTtcbiAgICAgIHdpZHRoOiA2MHB4O1xuICAgICAgYm94LXNpemluZzogYm9yZGVyLWJveDtcbiAgICAgIGJvcmRlci10b3A6IDFweCBzb2xpZCAjOTk5O1xuICAgICAgLW1zLXVzZXItc2VsZWN0OiBub25lO1xuICAgICAgLXdlYmtpdC11c2VyLXNlbGVjdDogbm9uZTtcbiAgICAgIC1tb3otdXNlci1zZWxlY3Q6IG5vbmU7XG4gICAgICB1c2VyLXNlbGVjdDogbm9uZTtcbiAgICAgIHRleHQtYWxpZ246IGxlZnQ7XG4gICAgICBoZWlnaHQ6IDI1cHg7XG4gICAgICBsaW5lLWhlaWdodDogMjVweDsgfVxuICAgICAgLmpiLXByby1jb250YWluZXItcGxheWJhY2sgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWJvdHRvbSAuamItcHJvLWNvbnRyb2xzLWNlbnRlciAuamItcHJvLXBsYXliYWNrLXRpbWUtaG91cjpmaXJzdC1jaGlsZCwgLmpiLXByby1jb250YWluZXItcGxheWJhY2sgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWJvdHRvbSAuamItcHJvLWNvbnRyb2xzLWNlbnRlciAuamItcHJvLXBsYXliYWNrLXRpbWUtbWludXRlOmZpcnN0LWNoaWxkIHtcbiAgICAgICAgYm9yZGVyLWxlZnQ6IDA7IH1cbiAgICAgICAgLmpiLXByby1jb250YWluZXItcGxheWJhY2sgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWJvdHRvbSAuamItcHJvLWNvbnRyb2xzLWNlbnRlciAuamItcHJvLXBsYXliYWNrLXRpbWUtaG91cjpmaXJzdC1jaGlsZCAuamItcHJvLXBsYXliYWNrLXRpbWUtaG91ci10ZXh0LCAuamItcHJvLWNvbnRhaW5lci1wbGF5YmFjayAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtYm90dG9tIC5qYi1wcm8tY29udHJvbHMtY2VudGVyIC5qYi1wcm8tcGxheWJhY2stdGltZS1taW51dGU6Zmlyc3QtY2hpbGQgLmpiLXByby1wbGF5YmFjay10aW1lLWhvdXItdGV4dCB7XG4gICAgICAgICAgbGVmdDogMDsgfVxuICAgICAgLmpiLXByby1jb250YWluZXItcGxheWJhY2sgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWJvdHRvbSAuamItcHJvLWNvbnRyb2xzLWNlbnRlciAuamItcHJvLXBsYXliYWNrLXRpbWUtaG91cjphZnRlciwgLmpiLXByby1jb250YWluZXItcGxheWJhY2sgLmpiLXByby1jb250cm9scyAuamItcHJvLWNvbnRyb2xzLWJvdHRvbSAuamItcHJvLWNvbnRyb2xzLWNlbnRlciAuamItcHJvLXBsYXliYWNrLXRpbWUtbWludXRlOmFmdGVyIHtcbiAgICAgICAgY29udGVudDogJyc7XG4gICAgICAgIHBvc2l0aW9uOiBhYnNvbHV0ZTtcbiAgICAgICAgbGVmdDogMDtcbiAgICAgICAgdG9wOiAtOHB4O1xuICAgICAgICB3aWR0aDogMXB4O1xuICAgICAgICBoZWlnaHQ6IDE0cHg7XG4gICAgICAgIGJhY2tncm91bmQtY29sb3I6ICM5OTk7IH1cbiAgICAuamItcHJvLWNvbnRhaW5lci1wbGF5YmFjayAuamItcHJvLWNvbnRyb2xzIC5qYi1wcm8tY29udHJvbHMtYm90dG9tIC5qYi1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Gp1c3RpZnktY29udGVudDogY2VudGVyOyB9XG4gICAgLmpiLXByby10aXBzLW1lc3NhZ2UgLmpiLXByby10aXBzLW1lc3NhZ2UtY2xvc2UgLmpiLXByby10aXBzLW1lc3NhZ2UtY2xvc2UtaWNvbiB7XG4gICAgICB3aWR0aDogMjBweDtcbiAgICAgIGhlaWdodDogMjBweDtcbiAgICAgIGJvcmRlci1yYWRpdXM6IDEwcHg7XG4gICAgICBjdXJzb3I6IHBvaW50ZXI7XG4gICAgICBiYWNrZ3JvdW5kOiB1cmwoXCIuLi9hc3NldHMvY2xvc2UucG5nXCIpIG5vLXJlcGVhdCBjZW50ZXI7XG4gICAgICBiYWNrZ3JvdW5kLWNvbG9yOiAjZmZmO1xuICAgICAgYmFja2dyb3VuZC1zaXplOiAxMDAlIDEwMCU7IH1cbiAgLmpiLXByby10aXBzLW1lc3NhZ2UgLmpiLXByby10aXBzLW1lc3NhZ2UtY29udGVudCB7XG4gICAgb3ZlcmZsb3c6IGF1dG87XG4gICAgcGFkZGluZzogMzVweDtcbiAgICBib3gtc2l6aW5nOiBib3JkZXItYm94O1xuICAgIHdpZHRoOiAxMDAlO1xuICAgIGhlaWdodDogMTAwJTsgfVxuICAgIC5qYi1wcm8tdGlwcy1tZXNzYWdlIC5qYi1wcm8tdGlwcy1tZXNzYWdlLWNvbnRlbnQgLmpiLXByby10aXBzLW1lc3NhZ2UtY29udGVudC1pdGVtIHtcbiAgICAgIGZvbnQtc2l6ZTogMTRweDtcbiAgICAgIGNvbG9yOiAjZmZmO1xuICAgICAgdGV4dC1hbGlnbjogY2VudGVyO1xuICAgICAgbGluZS1oZWlnaHQ6IDEuNTsgfVxuIl19 */";
styleInject(css_248z$1);
// todo: 待定
var hotkey = ((player, control) => {
const {
events: {
proxy
}
} = player;
const keys = {};
function addHotkey(key, event) {
if (keys[key]) {
keys[key].push(event);
} else {
keys[key] = [event];
}
}
//
addHotkey(HOT_KEY.esc, () => {
if (player.fullscreen) {
player.fullscreen = false;
}
});
//
addHotkey(HOT_KEY.arrowUp, () => {
player.volume += 0.05;
});
//
addHotkey(HOT_KEY.arrowDown, () => {
player.volume -= 0.05;
});
proxy(window, 'keydown', event => {
if (control.isFocus) {
const tag = document.activeElement.tagName.toUpperCase();
const editable = document.activeElement.getAttribute('contenteditable');
if (tag !== 'INPUT' && tag !== 'TEXTAREA' && editable !== '' && editable !== 'true') {
const events = keys[event.keyCode];
if (events) {
event.preventDefault();
events.forEach(fn => fn());
}
}
}
});
});
class Control {
constructor(player) {
this.player = player;
this.TAG_NAME = 'Control';
this.extendBtnList = [];
template(player, this);
if (player._opt.extendOperateBtns.length > 0) {
player._opt.extendOperateBtns.forEach(btn => {
this.addExtendBtn(btn);
});
}
if (player._opt.extendDomConfig && player._opt.extendDomConfig.html) {
this.addExtendDom(player._opt.extendDomConfig);
}
property(player, this);
observer$1(player, this);
events(player, this);
if (player._opt.hotKey) {
hotkey(player, this);
}
this.btnIndex = 0;
// 当触发异常机制,要保留最后一帧画面
this.initLoadingBackground();
// update loading style
if (isNotEmptyObject(player._opt.loadingIconStyle)) {
this.initLoadingIconStyle(player._opt.loadingIconStyle);
}
if (isNotEmptyObject(player._opt.ptzPositionConfig)) {
this.updatePtzPosition(player._opt.ptzPositionConfig);
}
this.kbpsShow = '0 KB/s';
this.player.debug.log('Control', 'init');
}
destroy() {
// performance
if (this.$performancePanel) {
this.$performancePanel.innerHTML = '';
const result = removeElement(this.$performancePanel);
if (!result) {
const $performancePanel = this.player.$container.querySelector('.jb-pro-performance-panel');
if ($performancePanel && this.player.$container) {
this.player.$container.removeChild($performancePanel);
}
}
}
//
if (this.$poster) {
const result = removeElement(this.$poster);
if (!result) {
const $poster = this.player.$container.querySelector('.jb-pro-poster');
if ($poster && this.player.$container) {
this.player.$container.removeChild($poster);
}
}
}
if (this.$loading) {
const result = removeElement(this.$loading);
if (!result) {
const $loading = this.player.$container.querySelector('.jb-pro-loading');
if ($loading && this.player.$container) {
this.player.$container.removeChild($loading);
}
}
}
if (this.$loadingBg) {
const result = removeElement(this.$loadingBg);
if (!result) {
const $loadingBg = this.player.$container.querySelector('.jb-pro-loading-bg');
if ($loadingBg && this.player.$container) {
this.player.$container.removeChild($loadingBg);
}
}
}
if (this.$controls) {
const result = removeElement(this.$controls);
if (!result) {
const $controls = this.player.$container.querySelector('.jb-pro-controls');
if ($controls && this.player.$container) {
this.player.$container.removeChild($controls);
}
}
}
if (this.$playBig) {
const result = removeElement(this.$playBig);
if (!result) {
const $playBig = this.player.$container.querySelector('.jb-pro-play-big');
if ($playBig && this.player.$container) {
this.player.$container.removeChild($playBig);
}
}
}
if (this.$recording) {
const result = removeElement(this.$recording);
if (!result) {
const $recording = this.player.$container.querySelector('.jb-pro-recording');
if ($recording && this.player.$container) {
this.player.$container.removeChild($recording);
}
}
}
if (this.$ptzControl) {
const result = removeElement(this.$ptzControl);
if (!result) {
const $ptzControl = this.player.$container.querySelector('.jb-pro-ptz-controls');
if ($ptzControl && this.player.$container) {
this.player.$container.removeChild($ptzControl);
}
}
}
if (this.$zoomControls) {
const result = removeElement(this.$zoomControls);
if (!result) {
const $zoomControls = this.player.$container.querySelector('.jb-pro-zoom-controls');
if ($zoomControls && this.player.$container) {
this.player.$container.removeChild($zoomControls);
}
}
}
if (this.$contextmenus) {
this.$contextmenus.innerHTML = '';
const result = removeElement(this.$contextmenus);
if (!result) {
const $contextmenus = this.player.$container.querySelector('.jb-pro-contextmenus');
if ($contextmenus && this.player.$container) {
this.player.$container.removeChild($contextmenus);
}
}
}
if (this.$tipsMessage) {
const result = removeElement(this.$tipsMessage);
if (!result) {
const $tipsMessage = this.player.$container.querySelector('.jb-pro-tips-message');
if ($tipsMessage && this.player.$container) {
this.player.$container.removeChild($tipsMessage);
}
}
}
if (this.$extendDom) {
const result = removeElement(this.$extendDom);
if (!result) {
const $extendDom = this.player.$container.querySelector('.jb-pro-extend-dom');
if ($extendDom && this.player.$container) {
this.player.$container.removeChild($extendDom);
}
}
}
this.btnIndex = 0;
this.extendBtnList = [];
this.kbpsShow = '0 KB/s';
if (this.player.$container) {
this.player.$container.classList.remove('jb-pro-controls-show-auto-hide');
this.player.$container.classList.remove('jb-pro-controls-show');
}
this.player.debug.log('Control', 'destroy');
}
getBtnIndex() {
return this.btnIndex++;
}
autoSize() {
const player = this.player;
player.$container.style.padding = '0 0';
const playerWidth = player.width;
const playerHeight = player.height;
const playerRatio = playerWidth / playerHeight;
const canvasWidth = player.video.$videoElement.width;
const canvasHeight = player.video.$videoElement.height;
const canvasRatio = canvasWidth / canvasHeight;
if (playerRatio > canvasRatio) {
const padding = (playerWidth - playerHeight * canvasRatio) / 2;
player.$container.style.padding = `0 ${padding}px`;
} else {
const padding = (playerHeight - playerWidth / canvasRatio) / 2;
player.$container.style.padding = `${padding}px 0`;
}
}
initLoadingBackground() {
const player = this.player;
if (player._opt.loadingBackground && player._opt.loadingBackgroundWidth && player._opt.loadingBackgroundHeight) {
player.debug.log('Control', 'initLoadingBackground()');
if (this.player._opt.aspectRatio === 'default' || isMobile()) {
if (player.getRenderType() === RENDER_TYPE.canvas) {
this._initLoadingBackground();
} else if (player.getRenderType() === RENDER_TYPE.video) {
this._initLoadingBackground();
}
} else {
this._initLoadingBackgroundForRatio();
}
setStyle(this.$loadingBg, 'display', 'block');
// 用完就要销毁
player._opt.loadingBackground = '';
player._opt.loadingBackgroundWidth = 0;
player._opt.loadingBackgroundHeight = 0;
}
}
initLoadingIconStyle(style) {
const $loadingIcon = this.player.$container.querySelector('.jb-pro-icon-loading');
if ($loadingIcon) {
if (style.width) {
setStyle($loadingIcon, 'width', `${style.width}px`);
}
if (style.height) {
setStyle($loadingIcon, 'height', `${style.height}px`);
}
if (style.background) {
setStyle($loadingIcon, 'backgroundImage', `url("${style.background}")`);
}
if (style.hasAnimation === false) {
setStyle($loadingIcon, 'animationName', "none");
setStyle($loadingIcon, 'animationDuration', 0);
setStyle($loadingIcon, 'animationTimingFunction', "ease");
setStyle($loadingIcon, 'animationIterationCount', 1);
}
}
}
_initLoadingBackgroundForRatio() {
const ratioArray = this.player._opt.aspectRatio.split(':').map(Number);
let width = this.player.width;
let height = this.player.height;
const option = this.player._opt;
let controlHeight = 0;
if (option.hasControl && !option.controlAutoHide) {
controlHeight = option.playType === PLAY_TYPE.playbackTF ? CONTROL_PLAYBACK_HEIGHT : CONTROL_HEIGHT;
height -= controlHeight;
}
const videoInfo = {
width: this.player._opt.loadingBackgroundWidth,
height: this.player._opt.loadingBackgroundHeight
};
const videoRatio = videoInfo.width / videoInfo.height;
const setupRatio = ratioArray[0] / ratioArray[1];
this.$loadingBgImage.src = this.player._opt.loadingBackground;
if (videoRatio > setupRatio) {
const percentage = setupRatio * videoInfo.height / videoInfo.width;
this.$loadingBgImage.style.width = `${percentage * 100}%`;
this.$loadingBgImage.style.height = `calc(100% - ${controlHeight}px)`;
this.$loadingBgImage.style.padding = `0 ${(width - width * percentage) / 2}px`;
} else {
const percentage = videoInfo.width / setupRatio / videoInfo.height;
this.$loadingBgImage.style.width = '100%';
this.$loadingBgImage.style.height = `calc(${percentage * 100}% - ${controlHeight}px)`;
this.$loadingBgImage.style.padding = `${(height - height * percentage) / 2}px 0`;
}
}
_initLoadingBackground() {
const player = this.player;
let height = player.height;
const option = player._opt;
if (option.hasControl && !option.controlAutoHide) {
const controlHeight = option.playType === PLAY_TYPE.playbackTF ? CONTROL_PLAYBACK_HEIGHT : CONTROL_HEIGHT;
height -= controlHeight;
}
let resizeWidth = player.width;
let resizeHeight = height;
const rotate = option.rotate;
if (rotate === 270 || rotate === 90) {
resizeWidth = height;
resizeHeight = player.width;
}
this.$loadingBgImage.width = resizeWidth;
this.$loadingBgImage.height = resizeHeight;
this.$loadingBgImage.src = player._opt.loadingBackground;
let left = (player.width - resizeWidth) / 2;
let top = (height - resizeHeight) / 2;
let objectFill = 'contain';
if (!option.isResize) {
objectFill = 'fill';
}
if (option.isFullResize) {
objectFill = 'none';
}
let transform = '';
if (option.mirrorRotate === 'none') {
if (rotate) {
transform += ' rotate(' + rotate + 'deg)';
}
}
if (option.mirrorRotate === 'level') {
transform += ' rotateY(180deg)'; // 水平镜像翻转
} else if (option.mirrorRotate === 'vertical') {
transform += ' rotateX(180deg)'; // 垂直镜像翻转
}
if (this.player._opt.videoRenderSupportScale) {
this.$loadingBgImage.style.objectFit = objectFill;
}
this.$loadingBgImage.style.transform = transform;
this.$loadingBgImage.style.padding = "0";
this.$loadingBgImage.style.left = left + "px";
this.$loadingBgImage.style.top = top + "px";
}
_validateExtendBtn(options) {
let result = true;
if (!options.name) {
this.player.debug.warn('Control', 'extend button name is required');
result = false;
}
if (result) {
const findIndex = this.extendBtnList.findIndex(item => item.name === options.name);
if (findIndex !== -1) {
this.player.debug.warn('Control', `extend button name: ${options.name} is already exist`);
result = false;
}
}
if (result) {
if (!options.icon) {
this.player.debug.warn('Control', 'extend button icon is required');
result = false;
}
}
return result;
}
addExtendBtn() {
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
const defaultOptions = getDefaultButtonOptions();
options = Object.assign({}, defaultOptions, options);
if (!this._validateExtendBtn(options)) {
return;
}
const name = options.name || '';
const $controlsRight = this.$controlsRight;
const hasActive = options.activeIcon && options.activeClick;
const $btn = `
${options.icon ? `
${options.iconTitle ? `
${options.iconTitle}
` : ``}
` : ''}
${options.activeIcon ? `
${options.activeIconTitle ? `
${options.activeIconTitle}
` : ``}
` : ''}
`;
const $childList = Array.from($controlsRight.children);
const nextChild = $childList[options.index];
if (nextChild) {
// insert before
nextChild.insertAdjacentHTML('beforebegin', $btn);
} else {
append($controlsRight, $btn);
}
const $iconContainer = $controlsRight.querySelector(`.jb-pro-controls-item-wrap-${name}`);
const $icon = options.icon ? $controlsRight.querySelector(`.jb-pro-icon-extend-${name}`) : null;
const $iconWrap = options.icon ? $controlsRight.querySelector(`.jb-pro-control-extend-${name}`) : null;
const $activeIcon = options.activeIcon ? $controlsRight.querySelector(`.jb-pro-icon-extend-${name}-active`) : null;
const $activeIconWrap = options.activeIcon ? $controlsRight.querySelector(`.jb-pro-control-extend-${name}-active`) : null;
const {
events: {
proxy
},
debug
} = this.player;
if (options.icon) {
setStyle($icon, 'background', `url(${options.icon}) no-repeat center`);
setStyle($icon, 'background-size', '100% 100%');
setStyle($iconWrap, 'display', 'none');
if (options.iconHover) {
proxy($icon, 'mouseover', () => {
setStyle($icon, 'background', `url(${options.iconHover}) no-repeat center`);
setStyle($icon, 'background-size', '100% 100%');
});
proxy($icon, 'mouseout', () => {
setStyle($icon, 'background', `url(${options.icon}) no-repeat center`);
setStyle($icon, 'background-size', '100% 100%');
});
}
}
if (options.activeIcon) {
setStyle($activeIcon, 'background', `url(${options.activeIcon}) no-repeat center`);
setStyle($activeIcon, 'background-size', '100% 100%');
setStyle($activeIconWrap, 'display', 'none');
if (options.activeIconHover) {
proxy($activeIcon, 'mouseover', () => {
setStyle($activeIcon, 'background', `url(${options.activeIconHover}) no-repeat center`);
setStyle($activeIcon, 'background-size', '100% 100%');
});
proxy($activeIcon, 'mouseout', () => {
setStyle($activeIcon, 'background', `url(${options.activeIcon}) no-repeat center`);
setStyle($activeIcon, 'background-size', '100% 100%');
});
}
}
if (options.click && $icon) {
proxy($icon, 'click', event => {
event.preventDefault();
if (hasActive) {
setStyle($iconWrap, 'display', 'none');
setStyle($activeIconWrap, 'display', 'flex');
}
if (this.player.isInMulti()) {
options.click.call(this.player, event, this.player._opt.multiIndex);
} else {
options.click.call(this.player, event);
}
});
}
if (options.activeClick && $activeIcon) {
proxy($activeIcon, 'click', event => {
event.preventDefault();
setStyle($iconWrap, 'display', 'flex');
setStyle($activeIconWrap, 'display', 'none');
if (this.player.isInMulti()) {
options.activeClick.call(this.player, event, this.player._opt.multiIndex);
} else {
options.activeClick.call(this.player, event);
}
});
}
this.extendBtnList.push({
name,
$iconContainer,
$iconWrap,
$activeIconWrap
});
}
addExtendDom(extendDomConfig) {
this.player.debug.log(this.TAG_NAME, 'addExtendDom');
if (extendDomConfig.html) {
const extendDomWrap = `
${extendDomConfig.html}
`;
this.player.$container.insertAdjacentHTML('beforeend', extendDomWrap);
Object.defineProperty(this, '$extendDom', {
value: this.player.$container.querySelector('.jb-pro-extend-dom')
});
// show
if (extendDomConfig.showBeforePlay) {
setStyle(this.$extendDom, 'display', 'block');
}
}
}
toggleExtendDom(flag) {
if (this.$extendDom) {
if (!isBoolean(flag)) {
flag = this.$extendDom.style.display === 'none';
}
if (flag) {
setStyle(this.$extendDom, 'display', 'block');
} else {
setStyle(this.$extendDom, 'display', 'none');
}
}
}
updateExtendDom(extendDom) {
this.player.debug.log(this.TAG_NAME, 'updateExtendDom');
if (this.$extendDom) {
this.$extendDom.innerHTML = extendDom;
}
}
removeExtendDom() {
this.player.debug.log(this.TAG_NAME, 'removeExtendDom');
if (this.$extendDom) {
this.$extendDom.innerHTML = '';
}
}
updateLoadingText(text) {
if (this.$loadingText) {
this.$loadingText.innerText = text;
}
}
getExtendBtnList() {
return this.extendBtnList;
}
showTipsMessage(content, type) {
const $tipsMessage = this.$tipsMessage;
const $tipsMessageContent = this.$tipsMessageContent;
if ($tipsMessage) {
const htmlContent = `
${content}
${type ? `Error Type:${type}
` : ''}
`;
$tipsMessageContent.innerHTML = htmlContent;
setStyle($tipsMessage, 'display', 'block');
}
}
hideTipsMessage() {
const $tipsMessage = this.$tipsMessage;
if ($tipsMessage) {
$tipsMessageContent.innerHTML = '';
setStyle($tipsMessage, 'display', 'none');
}
}
updatePtzPosition(position) {
const $ptzControl = this.$ptzControl;
if (isNotEmptyObject(position) && $ptzControl) {
let left = 'auto';
if (position.left) {
left = Number(position.left) === position.left ? position.left + 'px' : position.left;
}
setStyle($ptzControl, 'left', left);
let top = 'auto';
if (position.top) {
top = Number(position.top) === position.top ? position.top + 'px' : position.top;
}
setStyle($ptzControl, 'top', top);
let bottom = 'auto';
if (position.bottom) {
bottom = Number(position.bottom) === position.bottom ? position.bottom + 'px' : position.bottom;
}
setStyle($ptzControl, 'bottom', bottom);
let right = 'auto';
if (position.right) {
right = Number(position.right) === position.right ? position.right + 'px' : position.right;
}
setStyle($ptzControl, 'right', right);
}
}
}
var css_248z = ".jb-pro-container{position:relative;width:100%;height:100%;overflow:hidden}.jb-pro-container.jb-pro-fullscreen-web{position:fixed;z-index:9999;left:0;top:0;right:0;bottom:0;width:100vw!important;height:100vh!important;background:#000}\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0eWxlLnNjc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsa0JBQ0UsaUJBQWtCLENBQ2xCLFVBQVcsQ0FDWCxXQUFZLENBQ1osZUFBa0IsQ0FDbEIsd0NBQ0UsY0FBZSxDQUNmLFlBQWEsQ0FDYixNQUFPLENBQ1AsS0FBTSxDQUNOLE9BQVEsQ0FDUixRQUFTLENBQ1QscUJBQXVCLENBQ3ZCLHNCQUF3QixDQUN4QixlQUFrQiIsImZpbGUiOiJzdHlsZS5zY3NzIiwic291cmNlc0NvbnRlbnQiOlsiLmpiLXByby1jb250YWluZXIge1xuICBwb3NpdGlvbjogcmVsYXRpdmU7XG4gIHdpZHRoOiAxMDAlO1xuICBoZWlnaHQ6IDEwMCU7XG4gIG92ZXJmbG93OiBoaWRkZW47IH1cbiAgLmpiLXByby1jb250YWluZXIuamItcHJvLWZ1bGxzY3JlZW4td2ViIHtcbiAgICBwb3NpdGlvbjogZml4ZWQ7XG4gICAgei1pbmRleDogOTk5OTtcbiAgICBsZWZ0OiAwO1xuICAgIHRvcDogMDtcbiAgICByaWdodDogMDtcbiAgICBib3R0b206IDA7XG4gICAgd2lkdGg6IDEwMHZ3ICFpbXBvcnRhbnQ7XG4gICAgaGVpZ2h0OiAxMDB2aCAhaW1wb3J0YW50O1xuICAgIGJhY2tncm91bmQ6ICMwMDA7IH1cbiJdfQ== */";
styleInject(css_248z);
var observer = (player => {
const {
_opt,
debug,
events: {
proxy
}
} = player;
if (_opt.supportDblclickFullscreen) {
proxy(player.$container, 'dblclick', e => {
const target = getTarget(e);
const nodeName = target.nodeName.toLowerCase();
if (nodeName === 'canvas' || nodeName === 'video') {
player.fullscreen = !player.fullscreen;
}
});
}
//
proxy(document, 'visibilitychange', () => {
player.visibility = "visible" === document.visibilityState;
debug.log('visibilitychange', document.visibilityState);
if (_opt.hiddenAutoPause) {
debug.log('visibilitychange', 'hiddenAutoPause is true ', document.visibilityState, player._isPlayingBeforePageHidden);
if ("visible" === document.visibilityState) {
if (player._isPlayingBeforePageHidden) {
player.play();
}
} else {
player._isPlayingBeforePageHidden = player.playing;
// hidden
if (player.playing) {
player.pause();
}
}
}
});
if (isPc()) {
proxy(document, ['click', 'contextmenu'], e => {
if (includeFromEvent(e, player.$container)) {
// is contextmenu
if (isTrue(player._opt.disableContextmenu) && e.type === 'contextmenu') {
e.preventDefault();
}
player.isInput = e.target.tagName === 'INPUT';
player.isFocus = true;
player.emit(EVENTS.focus);
} else {
player.isInput = false;
player.isFocus = false;
player.emit(EVENTS.blur);
}
});
}
if (_opt.autoResize) {
const resizeFn = throttle(() => {
player.resize();
}, PLAYER_RESIZE_TIME);
proxy(window, ['resize', 'orientationchange'], () => {
resizeFn();
});
// check screen orientation change
if (screen && screen.orientation && screen.orientation.onchange) {
proxy(screen.orientation, 'change', () => {
resizeFn();
});
}
}
});
// MP4 boxes generator for ISO BMFF (ISO Base Media File Format, defined in ISO/IEC 14496-12)
class MP4$2 {
static init() {
MP4$2.types = {
avc1: [],
avcC: [],
hvc1: [],
hvcC: [],
av01: [],
av1C: [],
btrt: [],
dinf: [],
dref: [],
esds: [],
ftyp: [],
hdlr: [],
mdat: [],
mdhd: [],
mdia: [],
mfhd: [],
minf: [],
moof: [],
moov: [],
mp4a: [],
mvex: [],
mvhd: [],
sdtp: [],
stbl: [],
stco: [],
stsc: [],
stsd: [],
stsz: [],
stts: [],
tfdt: [],
tfhd: [],
traf: [],
trak: [],
trun: [],
trex: [],
tkhd: [],
vmhd: [],
smhd: [],
'.mp3': [],
Opus: [],
dOps: [],
'ac-3': [],
dac3: [],
'ec-3': [],
dec3: []
};
for (let name in MP4$2.types) {
if (MP4$2.types.hasOwnProperty(name)) {
MP4$2.types[name] = [name.charCodeAt(0), name.charCodeAt(1), name.charCodeAt(2), name.charCodeAt(3)];
}
}
let constants = MP4$2.constants = {};
constants.FTYP = new Uint8Array([0x69, 0x73, 0x6F, 0x6D,
// major_brand: isom
0x0, 0x0, 0x0, 0x1,
// minor_version: 0x01
0x69, 0x73, 0x6F, 0x6D,
// isom
0x61, 0x76, 0x63, 0x31 // avc1
]);
constants.STSD_PREFIX = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0x00, 0x00, 0x00, 0x01 // entry_count
]);
constants.STTS = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0x00, 0x00, 0x00, 0x00 // entry_count
]);
constants.STSC = constants.STCO = constants.STTS;
constants.STSZ = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0x00, 0x00, 0x00, 0x00,
// sample_size
0x00, 0x00, 0x00, 0x00 // sample_count
]);
constants.HDLR_VIDEO = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0x00, 0x00, 0x00, 0x00,
// pre_defined
0x76, 0x69, 0x64, 0x65,
// handler_type: 'vide'
0x00, 0x00, 0x00, 0x00,
// reserved: 3 * 4 bytes
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x56, 0x69, 0x64, 0x65, 0x6F, 0x48, 0x61, 0x6E, 0x64, 0x6C, 0x65, 0x72, 0x00 // name: VideoHandler
]);
constants.HDLR_AUDIO = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0x00, 0x00, 0x00, 0x00,
// pre_defined
0x73, 0x6F, 0x75, 0x6E,
// handler_type: 'soun'
0x00, 0x00, 0x00, 0x00,
// reserved: 3 * 4 bytes
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x6F, 0x75, 0x6E, 0x64, 0x48, 0x61, 0x6E, 0x64, 0x6C, 0x65, 0x72, 0x00 // name: SoundHandler
]);
constants.DREF = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0x00, 0x00, 0x00, 0x01,
// entry_count
0x00, 0x00, 0x00, 0x0C,
// entry_size
0x75, 0x72, 0x6C, 0x20,
// type 'url '
0x00, 0x00, 0x00, 0x01 // version(0) + flags
]);
// Sound media header
constants.SMHD = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0x00, 0x00, 0x00, 0x00 // balance(2) + reserved(2)
]);
// video media header
constants.VMHD = new Uint8Array([0x00, 0x00, 0x00, 0x01,
// version(0) + flags
0x00, 0x00,
// graphicsmode: 2 bytes
0x00, 0x00, 0x00, 0x00,
// opcolor: 3 * 2 bytes
0x00, 0x00]);
}
// Generate a box
static box(type) {
let size = 8;
let result = null;
let datas = Array.prototype.slice.call(arguments, 1);
let arrayCount = datas.length;
for (let i = 0; i < arrayCount; i++) {
size += datas[i].byteLength;
}
result = new Uint8Array(size);
result[0] = size >>> 24 & 0xFF; // size
result[1] = size >>> 16 & 0xFF;
result[2] = size >>> 8 & 0xFF;
result[3] = size & 0xFF;
result.set(type, 4); // type
let offset = 8;
for (let i = 0; i < arrayCount; i++) {
// data body
result.set(datas[i], offset);
offset += datas[i].byteLength;
}
return result;
}
// emit ftyp & moov
/**
* ftyp box与Moov box绑定,描述数据的类型、兼容协议以及视频参数
* @param meta
* @returns {Uint8Array}
*/
static generateInitSegment(meta) {
let ftyp = MP4$2.box(MP4$2.types.ftyp, MP4$2.constants.FTYP);
let moov = MP4$2.moov(meta);
let result = new Uint8Array(ftyp.byteLength + moov.byteLength);
result.set(ftyp, 0);
result.set(moov, ftyp.byteLength);
return result;
}
// Movie metadata box
static moov(meta) {
let mvhd = MP4$2.mvhd(meta.timescale, meta.duration);
let trak = MP4$2.trak(meta);
let mvex = MP4$2.mvex(meta);
return MP4$2.box(MP4$2.types.moov, mvhd, trak, mvex);
}
// Movie header box
static mvhd(timescale, duration) {
return MP4$2.box(MP4$2.types.mvhd, new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
0x00, 0x00, 0x00, 0x00,
// creation_time
0x00, 0x00, 0x00, 0x00,
// modification_time
timescale >>> 24 & 0xFF,
// timescale: 4 bytes
timescale >>> 16 & 0xFF, timescale >>> 8 & 0xFF, timescale & 0xFF, duration >>> 24 & 0xFF,
// duration: 4 bytes
duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF, 0x00, 0x01, 0x00, 0x00,
// Preferred rate: 1.0
0x01, 0x00, 0x00, 0x00,
// PreferredVolume(1.0, 2bytes) + reserved(2bytes)
0x00, 0x00, 0x00, 0x00,
// reserved: 4 + 4 bytes
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
// ----begin composition matrix----
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
// ----end composition matrix----
0x00, 0x00, 0x00, 0x00,
// ----begin pre_defined 6 * 4 bytes----
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// ----end pre_defined 6 * 4 bytes----
0xFF, 0xFF, 0xFF, 0xFF // next_track_ID
]));
}
// Track box
static trak(meta) {
return MP4$2.box(MP4$2.types.trak, MP4$2.tkhd(meta), MP4$2.mdia(meta));
}
// Track header box
static tkhd(meta) {
let trackId = meta.id,
duration = meta.duration;
let width = meta.presentWidth,
height = meta.presentHeight;
return MP4$2.box(MP4$2.types.tkhd, new Uint8Array([0x00, 0x00, 0x00, 0x07,
// version(0) + flags
0x00, 0x00, 0x00, 0x00,
// creation_time
0x00, 0x00, 0x00, 0x00,
// modification_time
trackId >>> 24 & 0xFF,
// track_ID: 4 bytes
trackId >>> 16 & 0xFF, trackId >>> 8 & 0xFF, trackId & 0xFF, 0x00, 0x00, 0x00, 0x00,
// reserved: 4 bytes
duration >>> 24 & 0xFF,
// duration: 4 bytes
duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF, 0x00, 0x00, 0x00, 0x00,
// reserved: 2 * 4 bytes
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// layer(2bytes) + alternate_group(2bytes)
0x00, 0x00, 0x00, 0x00,
// volume(2bytes) + reserved(2bytes)
0x00, 0x01, 0x00, 0x00,
// ----begin composition matrix----
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
// ----end composition matrix----
width >>> 8 & 0xFF,
// width and height
width & 0xFF, 0x00, 0x00,
// width
height >>> 8 & 0xFF, height & 0xFF, 0x00, 0x00 // height
]));
}
// Media Box
static mdia(meta) {
return MP4$2.box(MP4$2.types.mdia, MP4$2.mdhd(meta), MP4$2.hdlr(meta), MP4$2.minf(meta));
}
// Media header box
static mdhd(meta) {
let timescale = meta.timescale;
let duration = meta.duration;
return MP4$2.box(MP4$2.types.mdhd, new Uint8Array([0x00,
// version(0)
0x00, 0x00, 0x00,
// flags
0x00, 0x00, 0x00, 0x00,
// creation_time
0x00, 0x00, 0x00, 0x00,
// modification_time
timescale >>> 24 & 0xFF,
// timescale: 4 bytes
timescale >>> 16 & 0xFF, timescale >>> 8 & 0xFF, timescale & 0xFF,
// timescale
duration >>> 24 & 0xFF,
// duration: 4 bytes
duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF,
// duration
0x55, 0xC4,
// language: und (undetermined)
0x00, 0x00 // pre_defined = 0
]));
}
// Media handler reference box
static hdlr(meta) {
let data = null;
if (meta.type === 'audio') {
data = MP4$2.constants.HDLR_AUDIO;
} else {
data = MP4$2.constants.HDLR_VIDEO;
}
return MP4$2.box(MP4$2.types.hdlr, data);
}
// Media infomation box
static minf(meta) {
let xmhd = null;
if (meta.type === 'audio') {
xmhd = MP4$2.box(MP4$2.types.smhd, MP4$2.constants.SMHD);
} else {
xmhd = MP4$2.box(MP4$2.types.vmhd, MP4$2.constants.VMHD);
}
return MP4$2.box(MP4$2.types.minf, xmhd, MP4$2.dinf(), MP4$2.stbl(meta));
}
// Data infomation box
static dinf() {
let result = MP4$2.box(MP4$2.types.dinf, MP4$2.box(MP4$2.types.dref, MP4$2.constants.DREF));
return result;
}
// Sample table box
static stbl(meta) {
let result = MP4$2.box(MP4$2.types.stbl,
// type: stbl
MP4$2.stsd(meta),
// Sample Description Table
MP4$2.box(MP4$2.types.stts, MP4$2.constants.STTS),
// Time-To-Sample
MP4$2.box(MP4$2.types.stsc, MP4$2.constants.STSC),
// Sample-To-Chunk
MP4$2.box(MP4$2.types.stsz, MP4$2.constants.STSZ),
// Sample size
MP4$2.box(MP4$2.types.stco, MP4$2.constants.STCO) // Chunk offset
);
return result;
}
// Sample description box
static stsd(meta) {
if (meta.type === 'audio') {
if (meta.audioType === 'mp3') {
return MP4$2.box(MP4$2.types.stsd, MP4$2.constants.STSD_PREFIX, MP4$2.mp3(meta));
}
// else: aac -> mp4a
return MP4$2.box(MP4$2.types.stsd, MP4$2.constants.STSD_PREFIX, MP4$2.mp4a(meta));
} else {
if (meta.videoType === 'avc') {
// avc
return MP4$2.box(MP4$2.types.stsd, MP4$2.constants.STSD_PREFIX, MP4$2.avc1(meta));
} else {
// hevc
return MP4$2.box(MP4$2.types.stsd, MP4$2.constants.STSD_PREFIX, MP4$2.hvc1(meta));
}
}
}
static mp3(meta) {
let channelCount = meta.channelCount;
let sampleRate = meta.audioSampleRate;
let data = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// reserved(4)
0x00, 0x00, 0x00, 0x01,
// reserved(2) + data_reference_index(2)
0x00, 0x00, 0x00, 0x00,
// reserved: 2 * 4 bytes
0x00, 0x00, 0x00, 0x00, 0x00, channelCount,
// channelCount(2)
0x00, 0x10,
// sampleSize(2)
0x00, 0x00, 0x00, 0x00,
// reserved(4)
sampleRate >>> 8 & 0xFF,
// Audio sample rate
sampleRate & 0xFF, 0x00, 0x00]);
return MP4$2.box(MP4$2.types['.mp3'], data);
}
static mp4a(meta) {
let channelCount = meta.channelCount;
let sampleRate = meta.audioSampleRate;
let data = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// reserved(4)
0x00, 0x00, 0x00, 0x01,
// reserved(2) + data_reference_index(2)
0x00, 0x00, 0x00, 0x00,
// reserved: 2 * 4 bytes
0x00, 0x00, 0x00, 0x00, 0x00, channelCount,
// channelCount(2)
0x00, 0x10,
// sampleSize(2)
0x00, 0x00, 0x00, 0x00,
// reserved(4)
sampleRate >>> 8 & 0xFF,
// Audio sample rate
sampleRate & 0xFF, 0x00, 0x00]);
return MP4$2.box(MP4$2.types.mp4a, data, MP4$2.esds(meta));
}
static esds(meta) {
let config = meta.config || [];
let configSize = config.length;
let data = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version 0 + flags
0x03,
// descriptor_type
0x17 + configSize,
// length3
0x00, 0x01,
// es_id
0x00,
// stream_priority
0x04,
// descriptor_type
0x0F + configSize,
// length
0x40,
// codec: mpeg4_audio
0x15,
// stream_type: Audio
0x00, 0x00, 0x00,
// buffer_size
0x00, 0x00, 0x00, 0x00,
// maxBitrate
0x00, 0x00, 0x00, 0x00,
// avgBitrate
0x05 // descriptor_type
].concat([configSize]).concat(config).concat([0x06, 0x01, 0x02 // GASpecificConfig
]));
return MP4$2.box(MP4$2.types.esds, data);
}
// avc
static avc1(meta) {
let avcc = meta.avcc;
const width = meta.codecWidth;
const height = meta.codecHeight;
let data = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, width >>> 8 & 255, width & 255, height >>> 8 & 255, height & 255, 0, 72, 0, 0, 0, 72, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 255, 255]);
return MP4$2.box(MP4$2.types.avc1, data, MP4$2.box(MP4$2.types.avcC, avcc));
}
// hvc
static hvc1(meta) {
let avcc = meta.avcc;
const width = meta.codecWidth;
const height = meta.codecHeight;
let data = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, width >>> 8 & 255, width & 255, height >>> 8 & 255, height & 255, 0, 72, 0, 0, 0, 72, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 255, 255]);
return MP4$2.box(MP4$2.types.hvc1, data, MP4$2.box(MP4$2.types.hvcC, avcc));
}
// Movie Extends box
static mvex(meta) {
return MP4$2.box(MP4$2.types.mvex, MP4$2.trex(meta));
}
// Track Extends box
static trex(meta) {
let trackId = meta.id;
let data = new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) + flags
trackId >>> 24 & 0xFF,
// track_ID
trackId >>> 16 & 0xFF, trackId >>> 8 & 0xFF, trackId & 0xFF, 0x00, 0x00, 0x00, 0x01,
// default_sample_description_index
0x00, 0x00, 0x00, 0x00,
// default_sample_duration
0x00, 0x00, 0x00, 0x00,
// default_sample_size
0x00, 0x01, 0x00, 0x01 // default_sample_flags
]);
return MP4$2.box(MP4$2.types.trex, data);
}
// Movie fragment box
static moof(meta, baseMediaDecodeTime) {
return MP4$2.box(MP4$2.types.moof, MP4$2.mfhd(meta.sequenceNumber), MP4$2.traf(meta, baseMediaDecodeTime));
}
//
static mfhd(sequenceNumber) {
let data = new Uint8Array([0x00, 0x00, 0x00, 0x00, sequenceNumber >>> 24 & 0xFF,
// sequence_number: int32
sequenceNumber >>> 16 & 0xFF, sequenceNumber >>> 8 & 0xFF, sequenceNumber & 0xFF]);
return MP4$2.box(MP4$2.types.mfhd, data);
}
// Track fragment box
static traf(meta, baseMediaDecodeTime) {
let trackId = meta.id;
// Track fragment header box
let tfhd = MP4$2.box(MP4$2.types.tfhd, new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) & flags
trackId >>> 24 & 0xFF,
// track_ID
trackId >>> 16 & 0xFF, trackId >>> 8 & 0xFF, trackId & 0xFF]));
// Track Fragment Decode Time
let tfdt = MP4$2.box(MP4$2.types.tfdt, new Uint8Array([0x00, 0x00, 0x00, 0x00,
// version(0) & flags
baseMediaDecodeTime >>> 24 & 0xFF,
// baseMediaDecodeTime: int32
baseMediaDecodeTime >>> 16 & 0xFF, baseMediaDecodeTime >>> 8 & 0xFF, baseMediaDecodeTime & 0xFF]));
let sdtp = MP4$2.sdtp(meta);
let trun = MP4$2.trun(meta, sdtp.byteLength + 16 + 16 + 8 + 16 + 8 + 8);
return MP4$2.box(MP4$2.types.traf, tfhd, tfdt, trun, sdtp);
}
// Sample Dependency Type box
static sdtp(meta) {
let data = new Uint8Array(4 + 1);
let flags = meta.flags;
data[4] = flags.isLeading << 6 | flags.dependsOn << 4 | flags.isDependedOn << 2 | flags.hasRedundancy;
return MP4$2.box(MP4$2.types.sdtp, data);
}
// trun
static trun(meta, offset) {
let dataSize = 12 + 16;
let data = new Uint8Array(dataSize);
offset += 8 + dataSize;
data.set([0x00, 0x00, 0x0F, 0x01,
// version(0) & flags
0x00, 0x00, 0x00, 0x01,
// sample_count
offset >>> 24 & 0xFF,
// data_offset
offset >>> 16 & 0xFF, offset >>> 8 & 0xFF, offset & 0xFF], 0);
let duration = meta.duration;
let size = meta.size;
let flags = meta.flags;
let cts = meta.cts;
data.set([duration >>> 24 & 0xFF,
// sample_duration
duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF, size >>> 24 & 0xFF,
// sample_size
size >>> 16 & 0xFF, size >>> 8 & 0xFF, size & 0xFF, flags.isLeading << 2 | flags.dependsOn,
// sample_flags
flags.isDependedOn << 6 | flags.hasRedundancy << 4 | flags.isNonSync, 0x00, 0x00,
// sample_degradation_priority
cts >>> 24 & 0xFF,
// sample_composition_time_offset
cts >>> 16 & 0xFF, cts >>> 8 & 0xFF, cts & 0xFF], 12);
return MP4$2.box(MP4$2.types.trun, data);
}
// mdat
static mdat(data) {
return MP4$2.box(MP4$2.types.mdat, data);
}
}
MP4$2.init();
const mpegAudioV10SampleRateTable = [44100, 48000, 32000, 0];
const mpegAudioV20SampleRateTable = [22050, 24000, 16000, 0];
const mpegAudioV25SampleRateTable = [11025, 12000, 8000, 0];
const mpegAudioL1BitRateTable = [0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, -1];
const mpegAudioL2BitRateTable = [0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, -1];
const mpegAudioL3BitRateTable = [0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, -1];
function parseMp3AudioSpecificConfig(mp3frame) {
if (mp3frame.length < 4) {
console.error(`Invalid MP3 packet, header missing!`);
return;
}
let array = new Uint8Array(mp3frame.buffer);
let result = null;
if (array[0] !== 0xFF) {
console.error(`Invalid MP3 packet, first byte != 0xFF `);
return;
}
let ver = array[1] >>> 3 & 0x03;
let layer = (array[1] & 0x06) >> 1;
let bitrate_index = (array[2] & 0xF0) >>> 4;
let sampling_freq_index = (array[2] & 0x0C) >>> 2;
let channel_mode = array[3] >>> 6 & 0x03;
let channel_count = channel_mode !== 3 ? 2 : 1;
let sample_rate = 0;
let bit_rate = 0;
let codec = 'mp3';
switch (ver) {
case 0:
// MPEG 2.5
sample_rate = mpegAudioV25SampleRateTable[sampling_freq_index];
break;
case 2:
// MPEG 2
sample_rate = mpegAudioV20SampleRateTable[sampling_freq_index];
break;
case 3:
// MPEG 1
sample_rate = mpegAudioV10SampleRateTable[sampling_freq_index];
break;
}
switch (layer) {
case 1:
if (bitrate_index < mpegAudioL3BitRateTable.length) {
bit_rate = mpegAudioL3BitRateTable[bitrate_index];
}
break;
case 2:
if (bitrate_index < mpegAudioL2BitRateTable.length) {
bit_rate = mpegAudioL2BitRateTable[bitrate_index];
}
break;
case 3:
if (bitrate_index < mpegAudioL1BitRateTable.length) {
bit_rate = mpegAudioL1BitRateTable[bitrate_index];
}
break;
}
result = {
bitRate: bit_rate,
samplingRate: sample_rate,
channelCount: channel_count,
codec: codec,
originalCodec: codec,
audioType: 'mp3'
};
return result;
}
class MediaSource$3 extends Emitter {
constructor(player) {
super();
this.TAG_NAME = 'MediaSource';
this.player = player;
this._resetInIt();
// Use ManagedMediaSource only if w3c MediaSource is not available (e.g. iOS Safari)
this._useManagedMediaSource = 'ManagedMediaSource' in self && !('MediaSource' in self);
this.mediaSource = this._useManagedMediaSource ? new self.ManagedMediaSource() : new self.MediaSource();
// if not check first frame is iframe, will set isVideoFirstIFrame = true
this.isDecodeFirstIIframe = isFalse(player._opt.checkFirstIFrame) ? true : false;
//
this.mediaSourceObjectURL = null;
if (!this._useManagedMediaSource) {
this.mediaSourceObjectURL = window.URL.createObjectURL(this.mediaSource);
}
this.isSupportVideoFrameCallback = supportVideoFrameCallback();
this.canvasRenderInterval = null;
if (player._opt.mseUseCanvasRender) {
this.$videoElement = document.createElement('video');
if (this._useManagedMediaSource) {
// Apple ManagedMediaSource
this.$videoElement['disableRemotePlayback'] = true;
this.$videoElement.srcObject = this.mediaSource;
} else {
this.$videoElement.src = this.mediaSourceObjectURL;
}
this.initVideoEvents();
} else {
if (this._useManagedMediaSource) {
// Apple ManagedMediaSource
this.player.video.$videoElement['disableRemotePlayback'] = true;
this.player.video.$videoElement.srcObject = this.mediaSource;
} else {
this.player.video.$videoElement.src = this.mediaSourceObjectURL;
}
this.$videoElement = this.player.video.$videoElement;
}
this._bindMediaSourceEvents();
this.audioSourceBufferCheckTimeout = null;
this.audioSourceNoDataCheckTimeout = null;
if (this.player.isPlayback()) {
this.player.on(EVENTS.playbackPause, flag => {
// play
if (isFalse(flag)) {
if (isTrue(player._opt.checkFirstIFrame)) {
this.player.debug.log(this.TAG_NAME, `playbackPause is false and _opt.checkFirstIFrame is true so set isDecodeFirstIIframe = false`);
this.isDecodeFirstIIframe = false;
}
this.clearUpAllSourceBuffer();
// this.$videoElement.currentTime = 0;
this.$videoElement.play();
} else {
// pause
this.$videoElement.pause();
this.cacheTrack = {};
}
});
}
// listen visiblity change
// 这个考虑移动到video 里面就行了。
this.player.on(EVENTS.visibilityChange, flag => {
if (flag) {
//
setTimeout(() => {
if (this.player.isPlaying() && this.$videoElement) {
const lastTime = this.getVideoBufferLastTime();
if (lastTime > this.$videoElement.currentTime) {
this.player.debug.log(this.TAG_NAME, `visibilityChange is true and lastTime is ${lastTime} and currentTime is ${this.$videoElement.currentTime} so set currentTime to lastTime`);
this.$videoElement.currentTime = lastTime;
}
}
}, 300);
}
});
if (this._useManagedMediaSource) {
this.player.debug.log(this.TAG_NAME, 'init and using ManagedMediaSource');
} else {
this.player.debug.log(this.TAG_NAME, 'init');
}
}
destroy() {
this.stop();
this._clearAudioSourceBufferCheckTimeout();
this._clearAudioNoDataCheckTimeout();
this._stopCanvasRender();
if (this.eventListenList.length) {
this.eventListenList.forEach(item => item());
this.eventListenList = [];
}
if (this.supportVideoFrameCallbackHandle && this.$videoElement) {
this.$videoElement.cancelVideoFrameCallback(this.supportVideoFrameCallbackHandle);
this.supportVideoFrameCallbackHandle = null;
}
if (this.$videoElement) {
if (this.player._opt.mseUseCanvasRender) {
if (this.$videoElement.src) {
this.$videoElement.src = '';
this.$videoElement.removeAttribute('src');
}
}
this.$videoElement = null;
}
if (this.mediaSourceObjectURL) {
window.URL.revokeObjectURL(this.mediaSourceObjectURL);
this.mediaSourceObjectURL = null;
}
this._resetInIt();
this.off();
this.player.debug.log(this.TAG_NAME, 'destroy');
}
needInitAudio() {
return this.player._opt.hasAudio && this.player._opt.mseDecodeAudio;
}
_resetInIt() {
this.isAvc = null; //
this.isAAC = null; //
this.videoMeta = {};
this.audioMeta = {};
this.sourceBuffer = null;
this.audioSourceBuffer = null;
this.hasInit = false;
this.hasAudioInit = false;
this.isInitInfo = false;
this.isAudioInitInfo = false;
this.audioMimeType = '';
this.videoMimeType = '';
this.cacheTrack = {};
this.cacheAudioTrack = {};
this.timeInit = false;
this.sequenceNumber = 0;
this.audioSequenceNumber = 0;
this.firstRenderTime = null;
this.firstAudioTime = null; // 暂时不用
this.$videoElement = null;
this.mediaSourceAppendBufferFull = false;
this.mediaSourceAppendBufferError = false;
this.mediaSourceAddSourceBufferError = false;
this.mediaSourceBufferError = false;
this.mediaSourceError = false;
this.prevTimestamp = null;
this.decodeDiffTimestamp = null;
this.prevDts = null;
this.prevAudioDts = null;
this.prevPayloadBufferSize = 0;
this.isWidthOrHeightChanged = false;
this.prevTs = null;
this.prevAudioTs = null;
this.eventListenList = [];
this.pendingRemoveRanges = [];
this.pendingSegments = [];
this.pendingAudioRemoveRanges = [];
this.pendingAudioSegments = [];
this.supportVideoFrameCallbackHandle = null;
}
get state() {
return this.mediaSource && this.mediaSource.readyState;
}
// source 打开,并且准备接受通过 sourceBuffer.appendBuffer 添加的数据。
get isStateOpen() {
return this.state === MEDIA_SOURCE_STATE.open;
}
// 当前 MS 没有和 media element(比如:video.src) 相关联。创建时,MS 就是该状态
get isStateClosed() {
return this.state === MEDIA_SOURCE_STATE.closed;
}
// 当 endOfStream() 执行完成,会变为该状态,此时,source 依然和 media element 连接。
get isStateEnded() {
return this.state === MEDIA_SOURCE_STATE.ended;
}
// 获得当前媒体播放的时间,既可以设置(get),也可以获取(set)。单位为 s(秒)
get duration() {
return this.mediaSource && this.mediaSource.duration || -1;
}
set duration(duration) {
this.mediaSource.duration = duration;
}
_bindMediaSourceEvents() {
const {
debug,
events: {
proxy
}
} = this.player;
// 当 "closed" to "open" 或者 "ended" to "open" 时触发。
const sourceOpenProxyDestroy = proxy(this.mediaSource, MEDIA_SOURCE_EVENTS.sourceOpen, () => {
this.player.debug.log(this.TAG_NAME, 'sourceOpen');
this._onMediaSourceSourceOpen();
this.player.emit(EVENTS.mseSourceOpen);
});
//当 "open" to "closed" 或者 "ended" to "closed" 时触发。
const sourceCloseProxyDestroy = proxy(this.mediaSource, MEDIA_SOURCE_EVENTS.sourceClose, () => {
this.player.debug.log(this.TAG_NAME, 'sourceClose');
this.player.emit(EVENTS.mseSourceClose);
});
// 当 "open" to "ended" 时触发
const sourceendedProxyDestroy = proxy(this.mediaSource, MEDIA_SOURCE_EVENTS.sourceended, () => {
this.player.debug.log(this.TAG_NAME, 'sourceended');
this.player.emit(EVENTS.mseSourceended);
});
this.eventListenList.push(sourceOpenProxyDestroy, sourceCloseProxyDestroy, sourceendedProxyDestroy);
// 监听 change
// tips:在窗口不是最小化的情况下,会一直触发,但是一旦最小化,就不会触发了。
const timeUpdateProxyDestroy = proxy(this.$videoElement, VIDEO_ELEMENT_EVENTS.timeUpdate, event => {
// for 降级处理用的。
if (isFalse(this.isSupportVideoFrameCallback)) {
if (this.player.checkIsInRender()) {
this.player.handleRender();
} else {
const ts = parseInt(event.timeStamp, 10);
this.player.debug.log(this.TAG_NAME, `mseUseCanvasRender is ${this.player._opt.mseUseCanvasRender} and
$videoElement ts is ${ts}, but not in render and vbps is ${this.player._stats.vbps} and fps is ${this.player._stats.fps}`);
}
}
});
const ratechangeProxyDestroy = proxy(this.$videoElement, VIDEO_ELEMENT_EVENTS.ratechange, () => {
this.player.debug.log(this.TAG_NAME, 'video playback Rate change', this.$videoElement && this.$videoElement.playbackRate);
if (this.$videoElement && this.$videoElement.paused) {
this.player.debug.log(this.TAG_NAME, 'video is paused and next try to replay');
this.$videoElement.play();
}
});
this.eventListenList.push(timeUpdateProxyDestroy, ratechangeProxyDestroy);
if (this._useManagedMediaSource) {
const startstreamingDestroy = proxy(this.mediaSource, MEDIA_SOURCE_EVENTS.startstreaming, () => {
this.player.debug.log(this.TAG_NAME, 'ManagedMediaSource startstreaming');
this.player.emit(EVENTS.mseSourceStartStreaming);
});
const endstreamingDestroy = proxy(this.mediaSource, MEDIA_SOURCE_EVENTS.endstreaming, () => {
this.player.debug.log(this.TAG_NAME, 'ManagedMediaSource endstreaming');
this.player.emit(EVENTS.mseSourceEndStreaming);
});
const qualitychangeDestroy = proxy(this.mediaSource, MEDIA_SOURCE_EVENTS.qualitychange, () => {
this.player.debug.log(this.TAG_NAME, 'ManagedMediaSource qualitychange');
});
this.eventListenList.push(startstreamingDestroy, endstreamingDestroy, qualitychangeDestroy);
}
}
_onMediaSourceSourceOpen() {
// 1. remove evnet listener
// 2. check _pendingSourceBufferInit list
// 3. do Append Segments
if (!this.sourceBuffer) {
this.player.debug.log('MediaSource', 'onMediaSourceSourceOpen() sourceBuffer is null and next init');
this._initSourceBuffer();
}
if (!this.audioSourceBuffer) {
this.player.debug.log('MediaSource', 'onMediaSourceSourceOpen() audioSourceBuffer is null and next init');
this._initAudioSourceBuffer();
}
// append immediately only if init segment in subsequence
if (this._hasPendingSegments()) {
this._doAppendSegments();
}
}
initVideoEvents() {
const {
proxy
} = this.player.events;
const canplayProxyDestroy = proxy(this.$videoElement, VIDEO_ELEMENT_EVENTS.canplay, () => {
this.player.debug.log(this.TAG_NAME, 'video canplay');
this.$videoElement.play().then(() => {
this.player.emit(EVENTS.removeLoadingBgImage);
if (supportVideoFrameCallback()) {
this.supportVideoFrameCallbackHandle = this.$videoElement.requestVideoFrameCallback(this.videoFrameCallback.bind(this));
} else {
// hls265 for canvas render
if (this.player.isUseHls265()) {
this._stopCanvasRender();
this.canvasRenderInterval = setInterval(() => {
this.player.video.render({
$video: this.$videoElement,
ts: parseInt(this.$videoElement.currentTime * 1000, 10) || 0
});
}, 1000 / 25);
}
}
this.player.debug.log(this.TAG_NAME, 'video play');
}).catch(e => {
// DOMException: play() failed because the user didn't interact with the document first. https://goo.gl/xX8pDD
this.player.debug.error(this.TAG_NAME, 'video play error ', e);
this.player.emitError(EVENTS_ERROR.mediaSourceUseCanvasRenderPlayFailed, e);
});
});
const waitingProxyDestroy = proxy(this.$videoElement, VIDEO_ELEMENT_EVENTS.waiting, () => {
this.player.debug.log(this.TAG_NAME, 'video waiting');
});
//
const timeUpdateProxyDestroy = proxy(this.$videoElement, VIDEO_ELEMENT_EVENTS.timeUpdate, event => {
parseInt(event.timeStamp, 10);
// this.player.emit(EVENTS.videoTimeUpdate, timeStamp);
// check video is playing
if (this.$videoElement.paused) {
this.player.debug.warn(this.TAG_NAME, 'video is paused and next try to replay');
this.$videoElement.play().then(() => {
this.player.debug.log(this.TAG_NAME, 'video is paused and replay success');
}).catch(e => {
this.player.debug.warn(this.TAG_NAME, 'video is paused and replay error ', e);
});
}
});
this.eventListenList.push(canplayProxyDestroy, waitingProxyDestroy, timeUpdateProxyDestroy);
}
videoFrameCallback(now) {
let metaData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (this.player.isDestroyedOrClosed()) {
this.player.debug.log(this.TAG_NAME, 'videoFrameCallback() player is destroyed');
return;
}
const ts = parseInt(Math.max(metaData.mediaTime, this.$videoElement.currentTime) * 1000, 10) || 0;
this.player.handleRender();
this.player.video.render({
$video: this.$videoElement,
ts
});
if (this.player.isUseHls265()) {
this.player.updateStats({
fps: true,
ts: ts
});
}
this.supportVideoFrameCallbackHandle = this.$videoElement.requestVideoFrameCallback(this.videoFrameCallback.bind(this));
}
decodeVideo(payload, ts, isIframe, cts) {
const player = this.player;
if (!player) {
return;
}
if (this.player.isDestroyedOrClosed()) {
this.player.debug.warn(this.TAG_NAME, 'decodeVideo() player is destroyed');
return;
}
if (!this.hasInit) {
// this.player.debug.warn(this.TAG_NAME, `decodeVideo has not init , isIframe is ${isIframe} , payload is ${payload[1]}`)
if (isIframe && payload[1] === AVC_PACKET_TYPE.sequenceHeader) {
const videoCodec = payload[0] & 0x0F;
player.video.updateVideoInfo({
encTypeCode: videoCodec
});
// windows 下面的 360 浏览器是支持 mse 解码播放的。
// edge 也是支持 mse 解码播放的。
if (videoCodec === VIDEO_ENC_CODE.h265 && isFalse(supportMSEDecodeHevc())) {
// this.emit(EVENTS_ERROR.mediaSourceH265NotSupport)
this.player.emitError(EVENTS_ERROR.mediaSourceH265NotSupport);
return;
}
if (!player._times.decodeStart) {
player._times.decodeStart = now$2();
}
this.hasInit = this._decodeConfigurationRecord(payload, ts, isIframe, videoCodec);
} else {
this.player.debug.warn(this.TAG_NAME, `decodeVideo has not init , isIframe is ${isIframe} , payload is ${payload[1]}`);
}
} else {
if (!this.isDecodeFirstIIframe && isIframe) {
this.isDecodeFirstIIframe = true;
}
if (this.isDecodeFirstIIframe) {
// check video width or height is changed
if (isIframe && payload[1] === 0) {
const videoCodec = payload[0] & 0x0F;
let config = {};
if (videoCodec === VIDEO_ENC_CODE.h264) {
let data = payload.slice(5);
config = parseAVCDecoderConfigurationRecord(data);
} else if (videoCodec === VIDEO_ENC_CODE.h265) {
config = parseHEVCDecoderConfigurationRecord$2(payload);
}
const videoInfo = this.player.video.videoInfo;
if (videoInfo && videoInfo.width && videoInfo.height && config && config.codecWidth && config.codecHeight && (config.codecWidth !== videoInfo.width || config.codecHeight !== videoInfo.height)) {
this.player.debug.warn(this.TAG_NAME, `
decodeVideo: video width or height is changed,
old width is ${videoInfo.width}, old height is ${videoInfo.height},
new width is ${config.codecWidth}, new height is ${config.codecHeight},
and emit change event`);
this.isWidthOrHeightChanged = true;
this.player.emitError(EVENTS_ERROR.mseWidthOrHeightChange);
}
}
if (this.isWidthOrHeightChanged) {
this.player.debug.warn(this.TAG_NAME, `decodeVideo: video width or height is changed, and return`);
return;
}
if (isVideoSequenceHeader(payload)) {
this.player.debug.warn(this.TAG_NAME, 'decodeVideo and payload is video sequence header so drop this frame');
return;
}
if (payload.byteLength < VIDEO_PAYLOAD_MIN_SIZE) {
this.player.debug.warn(this.TAG_NAME, `decodeVideo and payload is too small , payload length is ${payload.byteLength}`);
return;
}
let dts = ts;
// just for player
if (this.player.isPlayer()) {
if (this.firstRenderTime === null) {
this.firstRenderTime = ts;
}
dts = ts - this.firstRenderTime;
if (dts < 0) {
this.player.debug.warn(this.TAG_NAME, `decodeVideo
local dts is < 0 , ts is ${ts} and prevTs is ${this.prevTs},
firstRenderTime is ${this.firstRenderTime} and mseCorrectTimeDuration is ${this.player._opt.mseCorrectTimeDuration}`);
if (this.prevDts === null) {
dts = 0;
} else {
dts = this.prevDts + this.player._opt.mseCorrectTimeDuration;
}
//
if (this._checkTsIsMaxDiff(ts)) {
this.player.debug.warn(this.TAG_NAME, `decodeVideo is max diff , ts is ${ts} and prevTs is ${this.prevTs}, diff is ${this.prevTs - ts} and emit replay`);
this.player.emitError(EVENTS_ERROR.mediaSourceTsIsMaxDiff, `decodeVideo is max diff, prevTs is ${this.prevTs} and ts is ${ts}`);
return;
}
}
if (this.prevDts !== null && dts <= this.prevDts) {
this.player.debug.warn(this.TAG_NAME, `
decodeVideo dts is less than(or equal) prev dts ,
dts is ${dts} and prev dts is ${this.prevDts} ,
and now ts is ${ts} and prev ts is ${this.prevTs} ,
and diff is ${ts - this.prevTs} and firstRenderTime is ${this.firstRenderTime} and isIframe is ${isIframe},
and mseCorrectTimeDuration is ${this.player._opt.mseCorrectTimeDuration},
and prevPayloadBufferSize is ${this.prevPayloadBufferSize} and payload size is ${payload.byteLength}`);
if (dts === this.prevDts) {
if (this.prevPayloadBufferSize === payload.byteLength) {
this.player.debug.warn(this.TAG_NAME, 'decodeVideo dts is equal to prev dts and payload size is equal to prev payload size so drop this frame');
return;
}
}
dts = this.prevDts + this.player._opt.mseCorrectTimeDuration;
if (this._checkTsIsMaxDiff(ts)) {
this.player.debug.warn(this.TAG_NAME, `decodeVideo is max diff , ts is ${ts} and prevTs is ${this.prevTs}, diff is ${this.prevTs - ts} and emit replay`);
this.emit(EVENTS_ERROR.mediaSourceTsIsMaxDiff, `decodeVideo is max diff, prevTs is ${this.prevTs} and ts is ${ts}`);
return;
}
}
}
if (this.player.isPlayer()) {
this._decodeVideo(payload, dts, isIframe, cts, ts);
} else if (this.player.isPlayback()) {
if (isFalse(this.player.playbackPause)) {
// is not pause
if (this.player.playback.isUseLocalCalculateTime) {
this.player.playback.increaseLocalTimestamp();
}
this._decodeVideo(payload, dts, isIframe, cts, ts);
}
}
this.prevDts = dts;
this.prevPayloadBufferSize = payload.byteLength;
this.prevTs = ts;
// 如果是video
// if (this.player.getRenderType() === RENDER_TYPE.video && this.player.video) {
// this.player.video.doAddContentToWatermark();
// }
} else {
this.player.debug.log(this.TAG_NAME, 'decodeVideo first frame is not iFrame');
}
}
}
decodeAudio(payload, ts) {
const player = this.player;
if (!player) {
return;
}
if (this.player.isDestroyedOrClosed()) {
this.player.debug.warn(this.TAG_NAME, 'decodeAudio() player is destroyed');
return;
}
if (isFalse(this.hasAudioInit)) {
this.hasAudioInit = this._decodeAudioConfigurationRecord(payload, ts);
} else {
let dts = ts;
// body check
if (isAacCodecPacket(payload)) {
this.player.debug.log(this.TAG_NAME, 'decodeAudio and has already initialized and payload is aac codec packet so drop this frame');
return;
}
this._clearAudioNoDataCheckTimeout();
if (this.player.isPlayer()) {
if (this.firstAudioTime === null) {
this.firstAudioTime = ts;
if (this.firstRenderTime !== null && this.prevTs !== null) {
// If the video has been played for more than 300ms, this is the audio data,
// it is necessary to reduce the start time of the video difference (prevTs - firstRenderTime).
const diff = Math.abs(this.firstRenderTime - this.prevTs);
if (diff > 300) {
this.firstAudioTime -= diff;
this.player.debug.warn(this.TAG_NAME, `video
firstAudioTime is ${this.firstRenderTime} and current time is ${this.prevTs}
play time is ${diff} and firstAudioTime ${ts} - ${diff} = ${this.firstAudioTime}`);
}
}
}
dts = ts - this.firstAudioTime;
if (dts < 0) {
this.player.debug.warn(this.TAG_NAME, `decodeAudio
local dts is < 0 , ts is ${ts} and prevTs is ${this.prevAudioTs},
firstAudioTime is ${this.firstAudioTime}`);
if (this.prevAudioDts === null) {
dts = 0;
} else {
dts = this.prevAudioDts + this.player._opt.mseCorrectAudioTimeDuration;
}
if (this._checkAudioTsIsMaxDiff(ts)) {
this.player.debug.warn(this.TAG_NAME, `decodeAudio is max diff , ts is ${ts} and prevTs is ${this.prevAudioDts}, diff is ${this.prevAudioDts - ts} and emit replay`);
this.player.emitError(EVENTS_ERROR.mediaSourceTsIsMaxDiff, `decodeAudio is max diff, prevTs is ${this.prevAudioDts} and ts is ${ts}`);
return;
}
}
if (this.prevAudioTs !== null && dts <= this.prevAudioDts) {
this.player.debug.warn(this.TAG_NAME, `
decodeAudio dts is less than(or equal) prev dts ,
dts is ${dts} and prev dts is ${this.prevAudioDts} ,
and now ts is ${ts} and prev ts is ${this.prevAudioTs} ,
and diff is ${ts - this.prevAudioTs}`);
dts = this.prevAudioDts + this.player._opt.mseCorrectAudioTimeDuration;
}
}
if (this.player.isPlayer()) {
this._decodeAudio(payload, dts, ts);
} else if (this.player.isPlayback()) {
if (isFalse(this.player.playbackPause)) {
this._decodeAudio(payload, dts, ts);
}
}
this.prevAudioTs = ts;
this.prevAudioDts = dts;
}
}
_checkTsIsMaxDiff(ts) {
return this.prevTs > 0 && ts < this.prevTs && this.prevTs - ts > FRAME_TS_MAX_DIFF;
}
_checkAudioTsIsMaxDiff(ts) {
return this.prevAudioTs > 0 && ts < this.prevAudioTs && this.prevAudioTs - ts > FRAME_TS_MAX_DIFF;
}
_decodeConfigurationRecord(payload, ts, isIframe, videoCodec) {
let data = payload.slice(5);
let config = {};
// this.player.debug.log(this.TAG_NAME, `_decodeConfigurationRecord videoCodec is ${videoCodec}`)
if (videoCodec === VIDEO_ENC_CODE.h264) {
config = parseAVCDecoderConfigurationRecord(data);
// config = parseAVCDecoderConfigurationRecord$2(payload)
} else if (videoCodec === VIDEO_ENC_CODE.h265) {
// config = parseHEVCDecoderConfigurationRecord(data);
// config = parseHEVCDecoderConfigurationRecord$2(payload);
config = parseHEVCDecoderConfigurationRecord$4(data);
}
// todo:just for recorder
if (this.player.recorder && this.player._opt.recordType === FILE_SUFFIX.mp4) {
this.player.recorder.initMetaData(payload, videoCodec);
}
if (config.codecWidth === 0 && config.codecHeight === 0) {
this.player.debug.warn(this.TAG_NAME, '_decodeConfigurationRecord', config);
this.player.emitError(EVENTS_ERROR.mediaSourceDecoderConfigurationError);
return false;
}
const metaData = {
id: AV_TRACK_ID.video,
// video tag data
type: 'video',
timescale: 1000,
duration: 0,
avcc: data,
codecWidth: config.codecWidth,
codecHeight: config.codecHeight,
videoType: config.videoType
};
// ftyp
const metaBox = MP4$2.generateInitSegment(metaData);
this.isAvc = videoCodec === VIDEO_ENC_CODE.h264;
let codec = config.codec;
if (codec) {
this.videoMimeType = `video/mp4; codecs="${config.codec}"`;
} else {
this.videoMimeType = this.isAvc ? MP4_CODECS.avc : MP4_CODECS.hev;
}
this._initSourceBuffer();
this.appendBuffer(metaBox.buffer);
this.sequenceNumber = 0;
this.cacheTrack = {};
this.timeInit = false;
return true;
}
_decodeAudioConfigurationRecord(payload, ts) {
const codecId = payload[0] >> 4;
const isMp3 = codecId === AUDIO_ENC_CODE.MP3;
const isAAC = codecId === AUDIO_ENC_CODE.AAC;
if (isFalse(isAAC || isMp3)) {
// inner error, not emit error event。
// inner change audio engine
this.player.debug.warn(this.TAG_NAME, `_decodeAudioConfigurationRecord audio codec is not support , codecId is ${codecId} ant auto wasm decode`);
this.player.emit(EVENTS_ERROR.mediaSourceAudioG711NotSupport);
return false;
}
const metaData = {
id: AV_TRACK_ID.audio,
type: 'audio',
timescale: 1000
};
let metaInfo = {};
if (isAacCodecPacket(payload)) {
const extraData = payload.slice(2);
metaInfo = parseAACAudioSpecificConfig(extraData);
if (metaInfo) {
metaData.audioSampleRate = metaInfo.sampleRate;
metaData.channelCount = metaInfo.channelCount;
metaData.config = metaInfo.config;
metaData.refSampleDuration = 1024 / metaData.audioSampleRate * metaData.timescale;
} else {
return false;
}
} else if (isMp3) {
// mp3
// mp3 没有extradata信息,这里的extradata 就是第一帧mp3
metaInfo = parseMp3AudioSpecificConfig(payload);
if (metaInfo) {
metaData.audioSampleRate = metaInfo.samplingRate;
metaData.channelCount = metaInfo.channelCount;
metaData.refSampleDuration = 1152 / metaData.audioSampleRate * metaData.timescale;
} else {
return false;
}
} else {
return false;
}
metaData.codec = metaInfo.codec;
metaData.duration = 0;
let container = 'mp4';
let codec = metaInfo.codec;
let metaBox = null;
if (isMp3 && isFalse(isFirefox())) {
// 'audio/mpeg' for MP3 audio track
container = 'mpeg';
codec = '';
metaBox = new Uint8Array();
} else {
// 'audio/mp4, codecs="codec"'
metaBox = MP4$2.generateInitSegment(metaData);
}
// console.error('metaData', metaData);
// console.error('metaBox', metaBox);
let mimeType = `${metaData.type}/${container}`;
if (codec && codec.length > 0) {
mimeType += `;codecs=${codec}`;
}
if (isFalse(this.isAudioInitInfo)) {
this.player.audio.updateAudioInfo({
encTypeCode: codecId,
channels: metaData.channelCount,
sampleRate: metaData.audioSampleRate
});
this.isAudioInitInfo = true;
}
this.audioMimeType = mimeType;
this.isAAC = isAAC;
// this.player.debug.log(this.TAG_NAME, `_decodeAudioConfigurationRecord mimeType is ${mimeType} and isAAC is ${isAAC}`);
this._initAudioSourceBuffer();
this.appendAudioBuffer(metaBox.buffer);
return true;
}
_initSourceBuffer() {
const {
debug,
events: {
proxy
}
} = this.player;
if (this.sourceBuffer === null && this.mediaSource !== null && this.isStateOpen && this.videoMimeType) {
try {
this.sourceBuffer = this.mediaSource.addSourceBuffer(this.videoMimeType);
debug.log(this.TAG_NAME, '_initSourceBuffer() this.mediaSource.addSourceBuffer()', this.videoMimeType);
} catch (e) {
debug.error(this.TAG_NAME, 'appendBuffer() this.mediaSource.addSourceBuffer()', e.code, e);
// need throw error and change to use wasm play
this.player.emitError(EVENTS_ERROR.mseAddSourceBufferError, e);
this.mediaSourceAddSourceBufferError = true;
return;
}
if (this.sourceBuffer) {
const sourceBufferErrorProxyDestroy = proxy(this.sourceBuffer, 'error', error => {
this.mediaSourceBufferError = true;
debug.error(this.TAG_NAME, 'mseSourceBufferError this.sourceBuffer', error);
this.player.emitError(EVENTS_ERROR.mseSourceBufferError, error);
});
const updateendProxyDestroy = proxy(this.sourceBuffer, 'updateend', () => {
if (this._hasPendingRemoveRanges()) {
// debug.log(this.TAG_NAME, 'this.sourceBuffer updateend and has pending remove ranges');
this._doRemoveRanges();
} else if (this._hasPendingSegments()) {
// debug.log(this.TAG_NAME, 'this.sourceBuffer updateend and has pending segments');
this._doAppendSegments();
}
});
this.eventListenList.push(sourceBufferErrorProxyDestroy, updateendProxyDestroy);
}
} else {
debug.log(this.TAG_NAME, `_initSourceBuffer and this.isStateOpen is ${this.isStateOpen} and this.isAvc === null is ${this.isAvc === null}`);
}
}
_initAudioSourceBuffer() {
const {
debug,
events: {
proxy
}
} = this.player;
if (this.audioSourceBuffer === null && this.mediaSource !== null && this.isStateOpen && this.audioMimeType) {
try {
this.audioSourceBuffer = this.mediaSource.addSourceBuffer(this.audioMimeType);
this._clearAudioSourceBufferCheckTimeout();
debug.log(this.TAG_NAME, '_initAudioSourceBuffer() this.mediaSource.addSourceBuffer()', this.audioMimeType);
} catch (e) {
debug.error(this.TAG_NAME, 'appendAudioBuffer() this.mediaSource.addSourceBuffer()', e.code, e);
// need throw error and change to use wasm play
this.player.emitError(EVENTS_ERROR.mseAddSourceBufferError, e);
this.mediaSourceAddSourceBufferError = true;
return;
}
if (this.audioSourceBuffer) {
const sourceBufferErrorProxyDestroy = proxy(this.audioSourceBuffer, 'error', error => {
this.mediaSourceBufferError = true;
debug.error(this.TAG_NAME, 'mseSourceBufferError this.audioSourceBuffer', error);
this.player.emitError(EVENTS_ERROR.mseSourceBufferError, error);
});
const updateendProxyDestroy = proxy(this.audioSourceBuffer, 'updateend', () => {
if (this._hasPendingRemoveRanges()) {
// debug.log(this.TAG_NAME, 'this.audioSourceBuffer updateend and has pending remove ranges');
this._doRemoveRanges();
} else if (this._hasPendingSegments()) {
// debug.log(this.TAG_NAME, 'this.audioSourceBuffer updateend and has pending segments');
this._doAppendSegments();
}
});
this.eventListenList.push(sourceBufferErrorProxyDestroy, updateendProxyDestroy);
if (this.audioSourceNoDataCheckTimeout === null) {
this.audioSourceNoDataCheckTimeout = setTimeout(() => {
// this.player.debug.warn(this.TAG_NAME, '_doAppendSegments() init audio timeout and set _opt.hasAudio is false');
// this.player._opt.hasAudio = false;
// this.audioSourceBufferCheckTimeout = null;
this._clearAudioNoDataCheckTimeout();
this.player.emit(EVENTS_ERROR.mediaSourceAudioNoDataTimeout);
}, 1000);
}
}
} else {
debug.log(this.TAG_NAME, `_initAudioSourceBuffer and this.isStateOpen is ${this.isStateOpen} and this.audioMimeType is ${this.audioMimeType}`);
}
}
//
_decodeVideo(payload, dts, isIframe, cts, ts) {
const player = this.player;
let arrayBuffer = payload.slice(5);
let bytes = arrayBuffer.byteLength;
if (bytes === 0) {
player.debug.warn(this.TAG_NAME, '_decodeVideo payload bytes is 0 and return');
return;
}
let nowTime = new Date().getTime();
let isFirst = false;
if (!this.prevTimestamp) {
this.prevTimestamp = nowTime;
isFirst = true;
}
const diffTime = nowTime - this.prevTimestamp;
this.decodeDiffTimestamp = diffTime;
if (diffTime > 500 && !isFirst && this.player.isPlayer()) {
player.debug.warn(this.TAG_NAME, `_decodeVideo now time is ${nowTime} and prev time is ${this.prevTimestamp}, diff time is ${diffTime} ms`);
}
const $video = this.$videoElement;
// const maxDelay = player._opt.videoBufferDelay + player._opt.videoBuffer;
// if ($video.buffered.length > 1) {
// this.removeBuffer($video.buffered.start(0), $video.buffered.end(0));
// this.timeInit = false;
// }
if (this.cacheTrack.id && dts >= this.cacheTrack.dts) {
// 需要额外加8个size
let mdatBytes = 8 + this.cacheTrack.size;
let mdatbox = new Uint8Array(mdatBytes);
mdatbox[0] = mdatBytes >>> 24 & 255;
mdatbox[1] = mdatBytes >>> 16 & 255;
mdatbox[2] = mdatBytes >>> 8 & 255;
mdatbox[3] = mdatBytes & 255;
// mdat box用来存储视频碎片数据,
mdatbox.set(MP4$2.types.mdat, 4);
mdatbox.set(this.cacheTrack.data, 8);
this.cacheTrack.duration = dts - this.cacheTrack.dts;
// moof
// moof box仅在流式MP4中使用,用于将多个sample组合成一个fragment。
// moof 用来描述mdat
// console.error('cacheTrack', this.cacheTrack, this.cacheTrack.dts);
let moofbox = MP4$2.moof(this.cacheTrack, this.cacheTrack.dts);
// 用完清空数据。
this.cacheTrack = {};
let result = new Uint8Array(moofbox.byteLength + mdatbox.byteLength);
result.set(moofbox, 0);
result.set(mdatbox, moofbox.byteLength);
// appendBuffer
this.appendBuffer(result.buffer);
// player.debug.log(this.TAG_NAME, 'decode ts is', dts,ts);
player.emit(EVENTS.timeUpdate, ts);
//
if (player.isPlayer()) {
if (player.isUseHls265()) {
player.updateStats({
dfps: true,
mseTs: dts
});
} else {
player.updateStats({
fps: true,
dfps: true,
ts: ts,
mseTs: dts
});
}
} else if (player.isPlayback()) {
player.playback.updateStats({
ts: ts
});
}
if (!player._times.videoStart) {
player._times.videoStart = now$2();
player.handlePlayToRenderTimes();
}
} else {
player.debug.log(this.TAG_NAME, `timeInit set false , cacheTrack = {} now dts is ${dts}, and ts is ${ts} cacheTrack dts is ${this.cacheTrack && this.cacheTrack.dts}`);
this.timeInit = false;
this.cacheTrack = {};
}
if (!this.cacheTrack) {
this.cacheTrack = {};
}
this.cacheTrack.id = AV_TRACK_ID.video;
this.cacheTrack.sequenceNumber = ++this.sequenceNumber;
this.cacheTrack.size = bytes;
this.cacheTrack.dts = dts;
this.cacheTrack.cts = cts;
this.cacheTrack.isKeyframe = isIframe;
this.cacheTrack.data = arrayBuffer;
//
this.cacheTrack.flags = {
isLeading: 0,
dependsOn: isIframe ? 2 : 1,
isDependedOn: isIframe ? 1 : 0,
hasRedundancy: 0,
isNonSync: isIframe ? 0 : 1
};
//
// if (!this.timeInit && $video.buffered.length === 1) {
// player.debug.log(this.TAG_NAME, 'timeInit set true');
// this.timeInit = true;
// $video.currentTime = $video.buffered.end(0);
// }
if (!this.isInitInfo && $video.videoWidth > 0 && $video.videoHeight > 0) {
player.debug.log(this.TAG_NAME, `updateVideoInfo: ${$video.videoWidth},${$video.videoHeight}`);
player.video.updateVideoInfo({
width: $video.videoWidth,
height: $video.videoHeight
});
player.video.initCanvasViewSize();
this.isInitInfo = true;
}
// use canvas render
if (player._opt.mseUseCanvasRender && isFalse(this.isSupportVideoFrameCallback) && isFalse(player.isUseHls265())) {
player.video.render({
$video,
ts: dts
});
}
this.prevTimestamp = new Date().getTime();
}
_stopCanvasRender() {
if (this.canvasRenderInterval) {
clearInterval(this.canvasRenderInterval);
this.canvasRenderInterval = null;
}
}
_decodeAudio(payload, dts, ts) {
this.player;
let arrayBuffer = this.isAAC ? payload.slice(2) : payload.slice(1);
let bytes = arrayBuffer.byteLength;
if (this.cacheAudioTrack.id && dts >= this.cacheAudioTrack.dts) {
// 需要额外加8个size
let mdatBytes = 8 + this.cacheAudioTrack.size;
let mdatbox = new Uint8Array(mdatBytes);
mdatbox[0] = mdatBytes >>> 24 & 255;
mdatbox[1] = mdatBytes >>> 16 & 255;
mdatbox[2] = mdatBytes >>> 8 & 255;
mdatbox[3] = mdatBytes & 255;
// mdat box用来存储视频碎片数据,
mdatbox.set(MP4$2.types.mdat, 4);
mdatbox.set(this.cacheAudioTrack.data, 8);
this.cacheAudioTrack.duration = dts - this.cacheAudioTrack.dts;
// moof
// moof box仅在流式MP4中使用,用于将多个sample组合成一个fragment。
// moof 用来描述mdat
// console.error('cacheAudioTrack', this.cacheAudioTrack, this.cacheAudioTrack.dts);
let moofbox = MP4$2.moof(this.cacheAudioTrack, this.cacheAudioTrack.dts);
// 用完清空数据。
this.cacheAudioTrack = {};
let result = new Uint8Array(moofbox.byteLength + mdatbox.byteLength);
result.set(moofbox, 0);
result.set(mdatbox, moofbox.byteLength);
this.appendAudioBuffer(result.buffer);
} else {
this.cacheAudioTrack = {};
}
if (!this.cacheAudioTrack) {
this.cacheAudioTrack = {};
}
this.cacheAudioTrack.id = AV_TRACK_ID.audio;
this.cacheAudioTrack.sequenceNumber = ++this.audioSequenceNumber;
this.cacheAudioTrack.size = bytes;
this.cacheAudioTrack.dts = dts;
this.cacheAudioTrack.cts = 0;
this.cacheAudioTrack.data = arrayBuffer;
this.cacheAudioTrack.flags = {
isLeading: 0,
dependsOn: 1,
isDependedOn: 0,
hasRedundancy: 0
};
}
appendBuffer(buffer) {
if (this.player.isDestroyedOrClosed()) {
this.player.debug.warn(this.TAG_NAME, 'appendBuffer() player is destroyed');
return;
}
const {
debug,
events: {
proxy
}
} = this.player;
if (this.mediaSourceAddSourceBufferError) {
debug.warn(this.TAG_NAME, `appendBuffer() this.mediaSourceAddSourceBufferError is true`);
return;
}
if (this.mediaSourceAppendBufferFull) {
debug.warn(this.TAG_NAME, `appendBuffer() this.mediaSourceAppendBufferFull is true`);
return;
}
if (this.mediaSourceAppendBufferError) {
debug.warn(this.TAG_NAME, `appendBuffer() this.mediaSourceAppendBufferError is true`);
return;
}
if (this.mediaSourceBufferError) {
debug.warn(this.TAG_NAME, `appendBuffer() this.mediaSourceBufferError is true`);
return;
}
this.pendingSegments.push(buffer);
if (this.sourceBuffer) {
// just for player
if (this.player.isPlayer()) {
this._handleUpdatePlaybackRate();
}
if (this.player.isPlayback()) {
this._handleUpdateBufferDelayTime();
}
if (this.player._opt.mseAutoCleanupSourceBuffer && this._needCleanupSourceBuffer()) {
this._doCleanUpSourceBuffer();
}
if (isFalse(this.getSourceBufferUpdating()) && this.isStateOpen && isFalse(this._hasPendingRemoveRanges())) {
this._doAppendSegments();
return;
}
}
if (this.isStateClosed) {
this.mediaSourceBufferError = true;
this.player.emitError(EVENTS_ERROR.mseSourceBufferError, 'mediaSource is not attached to video or mediaSource is closed');
} else if (this.isStateEnded) {
this.mediaSourceBufferError = true;
this.player.emitError(EVENTS_ERROR.mseSourceBufferError, 'mediaSource is end');
} else {
if (this._hasPendingRemoveRanges()) {
debug.log(this.TAG_NAME, `video has pending remove ranges and video length is ${this.pendingRemoveRanges.length}, audio length is ${this.pendingAudioRemoveRanges.length}`);
}
}
}
appendAudioBuffer(buffer) {
if (this.player.isDestroyedOrClosed()) {
this.player.debug.warn(this.TAG_NAME, 'appendAudioBuffer() player is destroyed');
return;
}
const {
debug,
events: {
proxy
}
} = this.player;
if (this.mediaSourceAddSourceBufferError) {
debug.warn(this.TAG_NAME, `appendAudioBuffer() this.mediaSourceAddSourceBufferError is true`);
return;
}
if (this.mediaSourceAppendBufferFull) {
debug.warn(this.TAG_NAME, `appendAudioBuffer() this.mediaSourceAppendBufferFull is true`);
return;
}
if (this.mediaSourceAppendBufferError) {
debug.warn(this.TAG_NAME, `appendAudioBuffer() this.mediaSourceAppendBufferError is true`);
return;
}
if (this.mediaSourceBufferError) {
debug.warn(this.TAG_NAME, `appendAudioBuffer() this.mediaSourceBufferError is true`);
return;
}
this.pendingAudioSegments.push(buffer);
if (this.audioSourceBuffer) {
// just for player
if (this.player.isPlayer()) {
this._handleUpdatePlaybackRate();
}
if (this.player.isPlayback()) {
this._handleUpdateBufferDelayTime();
}
if (this.player._opt.mseAutoCleanupSourceBuffer && this._needCleanupSourceBuffer()) {
this._doCleanUpSourceBuffer();
}
if (isFalse(this.getAudioSourceBufferUpdating()) && this.isStateOpen && isFalse(this._hasPendingRemoveRanges())) {
this._doAppendSegments();
return;
}
}
if (this.isStateClosed) {
this.mediaSourceBufferError = true;
this.player.emitError(EVENTS_ERROR.mseSourceBufferError, 'mediaSource is not attached to video or mediaSource is closed');
} else if (this.isStateEnded) {
this.mediaSourceBufferError = true;
this.player.emitError(EVENTS_ERROR.mseSourceBufferError, 'mediaSource is end');
} else {
if (this._hasPendingRemoveRanges()) {
debug.log(this.TAG_NAME, `audio has pending remove ranges and video length is ${this.pendingRemoveRanges.length}, audio length is ${this.pendingAudioRemoveRanges.length}`);
}
}
}
getSourceBufferUpdating() {
return this.sourceBuffer && this.sourceBuffer.updating;
}
getAudioSourceBufferUpdating() {
return this.audioSourceBuffer && this.audioSourceBuffer.updating;
}
stop() {
// remove source buffer
this.removeSourceBuffer();
// end of stream
this.endOfStream();
// abort source buffer
this.abortSourceBuffer();
}
checkSourceBufferDelay() {
const $video = this.$videoElement;
let result = 0;
let buffered = 0;
if ($video.buffered.length > 0) {
buffered = $video.buffered.end($video.buffered.length - 1);
result = buffered - $video.currentTime;
}
if (result < 0) {
this.player.debug.warn(this.TAG_NAME, `checkMSESourceBufferDelay ${result} < 0, and buffered is ${buffered} ,currentTime is ${$video.currentTime} , try to seek ${$video.currentTime} to ${buffered}`);
$video.currentTime = buffered;
result = 0;
}
return result;
}
checkSourceBufferStore() {
const $video = this.$videoElement;
let result = 0;
if ($video.buffered.length > 0) {
result = $video.currentTime - $video.buffered.start(0);
}
return result;
}
getDecodeDiffTimes() {
return this.decodeDiffTimestamp;
}
removeBuffer(start, end) {
const _isMacOsFirefox = isMacOsFirefox();
this.player.debug.log(this.TAG_NAME, `removeBuffer() start is ${start} and end is ${end} and _isMacOsFirefox is ${_isMacOsFirefox}`);
if (this.isStateOpen && isFalse(_isMacOsFirefox)) {
if (isFalse(this.getSourceBufferUpdating())) {
try {
this.sourceBuffer.remove(start, end);
} catch (e) {
this.player.debug.warn(this.TAG_NAME, 'removeBuffer() sourceBuffer error', e);
}
}
if (isFalse(this.getAudioSourceBufferUpdating())) {
try {
this.audioSourceBuffer.remove(start, end);
} catch (e) {
this.player.debug.warn(this.TAG_NAME, 'removeBuffer() audioSourceBuffer error', e);
}
}
}
}
// clear up all source buffer
clearUpAllSourceBuffer() {
// const $video = this.$videoElement;
// const buffered = $video.buffered;
if (this.sourceBuffer) {
const buffered = this.sourceBuffer.buffered;
for (let i = 0; i < buffered.length; i++) {
let start = buffered.start(i);
let end = buffered.end(i);
this.pendingRemoveRanges.push({
start: start,
end: end
});
}
if (isFalse(this.getSourceBufferUpdating())) {
this._doRemoveRanges();
}
}
if (this.audioSourceBuffer) {
const buffered = this.audioSourceBuffer.buffered;
for (let i = 0; i < buffered.length; i++) {
let start = buffered.start(i);
let end = buffered.end(i);
this.pendingAudioRemoveRanges.push({
start: start,
end: end
});
}
if (isFalse(this.getAudioSourceBufferUpdating())) {
this._doRemoveRanges();
}
}
}
endOfStream() {
// fix: MediaSource endOfStream before demuxer initialization completes (before HAVE_METADATA) is treated as an error
if (this.isStateOpen && this.$videoElement && this.$videoElement.readyState >= 1) {
try {
this.player.debug.log(this.TAG_NAME, 'endOfStream()');
this.mediaSource.endOfStream();
} catch (e) {
this.player.debug.warn(this.TAG_NAME, 'endOfStream() error', e);
}
}
}
abortSourceBuffer() {
if (this.isStateOpen) {
if (this.sourceBuffer) {
try {
this.player.debug.log(this.TAG_NAME, 'abortSourceBuffer() abort sourceBuffer');
this.sourceBuffer.abort();
} catch (e) {}
}
if (this.audioSourceBuffer) {
try {
this.player.debug.log(this.TAG_NAME, 'abortSourceBuffer() abort audioSourceBuffer');
this.audioSourceBuffer.abort();
} catch (e) {}
}
}
this.sourceBuffer = null;
this.audioSourceBuffer = null;
}
removeSourceBuffer() {
if (!this.isStateClosed && this.mediaSource) {
// video
if (this.sourceBuffer) {
try {
this.player.debug.log(this.TAG_NAME, 'removeSourceBuffer() sourceBuffer');
this.mediaSource.removeSourceBuffer(this.sourceBuffer);
} catch (e) {
this.player.debug.warn(this.TAG_NAME, 'removeSourceBuffer() sourceBuffer error', e);
}
}
// audio
if (this.audioSourceBuffer) {
try {
this.player.debug.log(this.TAG_NAME, 'removeSourceBuffer() audioSourceBuffer');
this.mediaSource.removeSourceBuffer(this.audioSourceBuffer);
} catch (e) {
this.player.debug.warn(this.TAG_NAME, 'removeSourceBuffer() audioSourceBuffer error', e);
}
}
}
}
_hasPendingSegments() {
return this.pendingSegments.length > 0 || this.pendingAudioSegments.length > 0;
}
// pending
getPendingSegmentsLength() {
return this.pendingSegments.length;
}
_handleUpdatePlaybackRate() {
if (!this.$videoElement) {
return;
}
const $video = this.$videoElement;
const videoBuffer = this.player._opt.videoBuffer;
const videoBufferDelay = this.player._opt.videoBufferDelay;
let maxDelay = (videoBuffer + videoBufferDelay) / 1000;
const ranges = $video.buffered; // 已缓冲的时间范围
ranges.length ? ranges.start(0) : 0;
const buffered = ranges.length ? ranges.end(ranges.length - 1) : 0;
let time = $video.currentTime;
const buffer = buffered - time;
// not less than MSE_MAX_DELAY_TIME
const maxDelayTime = Math.max(MSE_MAX_DELAY_TIME, maxDelay + MSE_DELAY_INCREASE_TIME);
this.player.updateStats({
mseVideoBufferDelayTime: buffer
});
if (buffer > maxDelayTime) {
this.player.debug.log(this.TAG_NAME, `handleUpdatePlaybackRate and buffered is ${buffered} and current is ${time} , delay buffer is more than ${maxDelayTime} is ${buffer} and new time is ${buffered}`);
$video.currentTime = buffered;
time = $video.currentTime;
} else if (buffer < 0) {
this.player.debug.warn(this.TAG_NAME, `handleUpdatePlaybackRate and delay buffer is ${buffered} - current is ${time} = ${buffer} < 0 and check video is paused : ${$video.paused} `);
// buffer
if (buffered === 0) {
this.player.emit(EVENTS_ERROR.mediaSourceBufferedIsZeroError, 'video.buffered is empty');
return;
}
// check if the video is paused
if ($video.paused) {
// if paused, play it
$video.play();
}
}
const rate = this._getPlaybackRate(buffered - time);
if ($video.playbackRate !== rate) {
this.player.debug.log(this.TAG_NAME, `handleUpdatePlaybackRate and buffered is ${buffered} and current time is ${time} and delay is ${buffered - time} set playbackRate is ${rate} `);
$video.playbackRate = rate;
}
}
_handleUpdateBufferDelayTime() {
const bufferDelayTime = this.getVideoBufferDelayTime();
this.player.updateStats({
mseVideoBufferDelayTime: bufferDelayTime
});
}
_doAppendSegments() {
if (this.isStateClosed || this.isStateEnded) {
return;
}
if (this.needInitAudio() && this.audioSourceBuffer === null) {
this.player.debug.log(this.TAG_NAME, '_doAppendSegments() audioSourceBuffer is null and need init audio source buffer');
if (this.audioSourceBufferCheckTimeout === null) {
this.audioSourceBufferCheckTimeout = setTimeout(() => {
this._clearAudioSourceBufferCheckTimeout();
this.player.emit(EVENTS_ERROR.mediaSourceAudioInitTimeout);
}, 1000);
}
return;
}
if (isFalse(this.getSourceBufferUpdating())) {
// video
if (this.pendingSegments.length > 0) {
const segment = this.pendingSegments.shift();
try {
this.sourceBuffer.appendBuffer(segment);
} catch (e) {
this.player.debug.error(this.TAG_NAME, 'this.sourceBuffer.appendBuffer()', e.code, e);
if (e.code === 22) {
// QuotaExceededError
// The SourceBuffer is full, and cannot free space to append additional buffers
this.stop();
this.mediaSourceAppendBufferFull = true;
this.player.emitError(EVENTS_ERROR.mediaSourceFull);
} else if (e.code === 11) {
// Failed to execute 'appendBuffer' on 'SourceBuffer': The HTMLMediaElement.error attribute is not null.
this.stop();
this.mediaSourceAppendBufferError = true;
this.player.emitError(EVENTS_ERROR.mediaSourceAppendBufferError);
} else {
this.stop();
this.mediaSourceBufferError = true;
this.player.emitError(EVENTS.mseSourceBufferError, e);
}
}
}
}
if (isFalse(this.getAudioSourceBufferUpdating())) {
// audio
if (this.pendingAudioSegments.length > 0) {
const segment = this.pendingAudioSegments.shift();
try {
this.audioSourceBuffer.appendBuffer(segment);
} catch (e) {
this.player.debug.error(this.TAG_NAME, 'this.audioSourceBuffer.appendBuffer()', e.code, e);
if (e.code === 22) {
// QuotaExceededError
// The SourceBuffer is full, and cannot free space to append additional buffers
this.stop();
this.mediaSourceAppendBufferFull = true;
this.player.emitError(EVENTS_ERROR.mediaSourceFull);
} else if (e.code === 11) {
// Failed to execute 'appendBuffer' on 'SourceBuffer': The HTMLMediaElement.error attribute is not null.
this.stop();
this.mediaSourceAppendBufferError = true;
this.player.emitError(EVENTS_ERROR.mediaSourceAppendBufferError);
} else {
this.stop();
this.mediaSourceBufferError = true;
this.player.emitError(EVENTS.mseSourceBufferError, e);
}
}
}
}
}
_doCleanUpSourceBuffer() {
if (!this.$videoElement) {
return;
}
const $video = this.$videoElement;
const currentTime = $video.currentTime;
// video
if (this.sourceBuffer) {
const buffered = this.sourceBuffer.buffered;
let doRemove = false;
for (let i = 0; i < buffered.length; i++) {
let start = buffered.start(i);
let end = buffered.end(i);
if (start <= currentTime && currentTime < end + 3) {
// padding 3 seconds
if (currentTime - start >= this.player._opt.mseAutoCleanupMaxBackwardDuration) {
doRemove = true;
let removeEnd = currentTime - this.player._opt.mseAutoCleanupMinBackwardDuration;
this.pendingRemoveRanges.push({
start: start,
end: removeEnd
});
}
} else if (end < currentTime) {
doRemove = true;
this.pendingRemoveRanges.push({
start: start,
end: end
});
}
}
if (doRemove && isFalse(this.getSourceBufferUpdating())) {
this._doRemoveRanges();
}
}
// audio
if (this.audioSourceBuffer) {
const buffered = this.audioSourceBuffer.buffered;
let doRemove = false;
for (let i = 0; i < buffered.length; i++) {
let start = buffered.start(i);
let end = buffered.end(i);
if (start <= currentTime && currentTime < end + 3) {
// padding 3 seconds
if (currentTime - start >= this.player._opt.mseAutoCleanupMaxBackwardDuration) {
doRemove = true;
let removeEnd = currentTime - this.player._opt.mseAutoCleanupMinBackwardDuration;
this.pendingAudioRemoveRanges.push({
start: start,
end: removeEnd
});
}
} else if (end < currentTime) {
doRemove = true;
this.pendingAudioRemoveRanges.push({
start: start,
end: end
});
}
}
if (doRemove && isFalse(this.getAudioSourceBufferUpdating())) {
this._doRemoveRanges();
}
}
}
_hasPendingRemoveRanges() {
return this.pendingRemoveRanges.length > 0 || this.pendingAudioRemoveRanges.length > 0;
}
_doRemoveRanges() {
if (this.sourceBuffer && isFalse(this.getSourceBufferUpdating())) {
let ranges = this.pendingRemoveRanges;
while (ranges.length && isFalse(this.getSourceBufferUpdating())) {
let range = ranges.shift();
try {
this.sourceBuffer.remove(range.start, range.end);
} catch (e) {
this.player.debug.warn(this.TAG_NAME, '_doRemoveRanges() sourceBuffer error', e);
}
}
}
if (this.audioSourceBuffer && isFalse(this.getAudioSourceBufferUpdating())) {
let ranges = this.pendingAudioRemoveRanges;
while (ranges.length && isFalse(this.getAudioSourceBufferUpdating())) {
let range = ranges.shift();
try {
this.audioSourceBuffer.remove(range.start, range.end);
} catch (e) {
this.player.debug.warn(this.TAG_NAME, '_doRemoveRanges() audioSourceBuffer error', e);
}
}
}
}
getDecodePlaybackRate() {
let result = 0;
const $video = this.$videoElement;
if ($video) {
result = $video.playbackRate;
}
return result;
}
_getPlaybackRate(buffer) {
const $video = this.$videoElement;
let videoBufferDelay = this.player._opt.videoBufferDelay + this.player._opt.videoBuffer;
// not less than 1000ms
const maxDelay = Math.max(videoBufferDelay, 1000);
const minDelay = maxDelay / 2;
// s -> ms
buffer = buffer * 1000;
switch ($video.playbackRate) {
case 1:
if (buffer > maxDelay) {
return 1.2;
}
return 1;
default:
if (buffer <= minDelay) {
return 1;
}
return $video.playbackRate;
}
}
_needCleanupSourceBuffer() {
if (isFalse(this.player._opt.mseAutoCleanupSourceBuffer) || !this.$videoElement) {
return false;
}
const $video = this.$videoElement;
const buffered = $video.buffered;
const currentTime = $video.currentTime;
if (buffered.length >= 1) {
if (currentTime - buffered.start(0) >= this.player._opt.mseAutoCleanupMaxBackwardDuration) {
return true;
}
}
return false;
}
getVideoCurrentTime() {
let result = 0;
if (this.$videoElement) {
result = this.$videoElement.currentTime;
}
return result;
}
getVideoBufferLastTime() {
const $video = this.$videoElement;
let result = 0;
if ($video) {
const ranges = $video.buffered; // 已缓冲的时间范围
ranges.length ? ranges.start(0) : 0;
const buffered = ranges.length ? ranges.end(ranges.length - 1) : 0;
result = buffered;
}
return result;
}
getVideoBufferDelayTime() {
const $video = this.$videoElement;
const buffered = this.getVideoBufferLastTime();
let time = $video.currentTime;
const buffer = buffered - time;
return buffer > 0 ? buffer : 0;
}
_clearAudioSourceBufferCheckTimeout() {
if (this.audioSourceBufferCheckTimeout) {
clearTimeout(this.audioSourceBufferCheckTimeout);
this.audioSourceBufferCheckTimeout = null;
}
}
// check has audio data
_clearAudioNoDataCheckTimeout() {
if (this.audioSourceNoDataCheckTimeout) {
clearTimeout(this.audioSourceNoDataCheckTimeout);
this.audioSourceNoDataCheckTimeout = null;
}
}
}
// tks: https://github.com/richtr/NoSleep.js
const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ggKVgPj//qOHggKpgPj//qOHggK9gPj//qOegQLDANECAAUQEBRgAGFgv9AAIgAQzX61yT5xzAAAo4eCAtGA+P/+o4eCAuWA+P/+o4eCAvmA+P/+o4eCAw2A+P/+o4eCAyGA+P/+o56BAycA0QIABRCsABgAGFgv9AAIgAQzX61yT5xzAACjh4IDNYD4//6jh4IDSYD4//6jh4IDXYD4//6jh4IDcYD4//6jh4IDhYD4//6jnoEDiwDRAgAFEKwAGAAYWC/0AAiABDNfrXJPnHMAAKOHggOZgPj//qOHggOtgPj//qOHggPBgPj//qOHggPVgPj//qOHggPpgPj//qOegQPvANECAAUQrAAYABhYL/QACIAEM1+tck+ccwAAo4eCA/2A+P/+o4eCBBGA+P/+o4eCBCWA+P/+o4eCBDmA+P/+o4eCBE2A+P/+o56BBFMA0QIABRCsABgAGFgv9AAIgAQzX61yT5xzAACjh4IEiID4//6jh4IEnID4//6jh4IEsID4//6jnoEEtwDRAgAFEKwAGAAYWC/0AAiABDNfrXJPnHMAAKOHggTEgPj//qOHggTYgPj//qOHggTsgPj//qOHggUAgPj//qOHggUUgPj//qOegQUbANECAAUQrAAYABhYL/QACIAEM1+tck+ccwAAo4eCBSiA+P/+o4eCBTyA+P/+o4eCBVCA+P/+o4eCBWSA+P/+o4eCBXiA+P/+o56BBX8A0QIABRCsABgAGFgv9AAIgAQzX61yT5xzAACjh4IFjID4//6jh4IFoID4//6jh4IFtID4//6jh4IFyID4//6jh4IF3ID4//6jnoEF4wDRAgAFEKwAGAAYWC/0AAiABDNfrXJPnHMAAKOHggXwgPj//qOHggYEgPj//qOHggYYgPj//qOHggYsgPj//qOHggZAgPj//qOegQZHANECAAUQrAAYABhYL/QACIAEM1+tck+ccwAAo4eCBlSA+P/+o4eCBmiA+P/+o4eCBnyA+P/+o4eCBpCA+P/+o4eCBqSA+P/+o56BBqsA0QIABRCsABgAGFgv9AAIgAQzX61yT5xzAACjh4IGuID4//6jh4IGzID4//6jh4IG4ID4//6jh4IG9ID4//6jh4IHCID4//6jnoEHDwDRAgAFEBAUYABhYL/QACIAEM1+tck+ccwAAKOHggccgPj//qOHggcwgPj//qOHggdEgPj//qOHggdYgPj//qOHggdsgPj//qOegQdzANECAAUQrAAYABhYL/QACIAEM1+tck+ccwAAo4eCB4CA+P/+o4eCB5SA+P/+o4eCB6iA+P/+o4eCB7yA+P/+o4eCB9CA+P/+o56BB9cA0QIABRCsABgAGFgv9AAIgAQzX61yT5xzAACjh4IH5ID4//6jh4IH+ID4//6jh4IIDID4//6jh4IIIID4//6jh4IINID4//6jnoEIOwDRAgAFEKwAGAAYWC/0AAiABDNfrXJPnHMAAKOHgghIgPj//qOHgghcgPj//qOHgghwgPj//qOHggiEgPj//qOegQifANECAAUQrAAYABhYL/QACIAEM1+tck+ccwAAo4eCCMCA+P/+o4eCCNSA+P/+o4eCCOiA+P/+o4eCCPyA+P/+o56BCQMA0QIABRCsABgAGFgv9AAIgAQzX61yT5xzAACjh4IJEID4//6jh4IJJID4//6jh4IJOID4//6jh4IJTID4//6jh4IJYID4//6jnoEJZwDRAgAFEKwAGAAYWC/0AAiABDNfrXJPnHMAAKOHggl0gPj//qOHggmIgPj//qOHggmcgPj//qOHggmwgPj//qOHggnEgPj//qOegQnLANECAAUQrAAYABhYL/QACIAEM1+tck+ccwAAo4eCCdiA+P/+o4eCCeyA+P/+o4eCCgCA+P/+o4eCChSA+P/+o4eCCiiA+P/+o56BCi8A0QIABRCsABgAGFgv9AAIgAQzX61yT5xzAACjh4IKPID4//6jh4IKUID4//6jh4IKZID4//6jh4IKeID4//6jh4IKjID4//6jnoEKkwDRAgAFEKwAGAAYWC/0AAiABDNfrXJPnHMAAKOHggqggPj//qOHggq0gPj//qOHggrIgPj//qOHggrcgPj//qOHggrwgPj//qOegQr3ANECAAUQrAAYABhYL/QACIAEM1+tck+ccwAAo4eCCwSA+P/+o4eCCxiA+P/+o4eCCyyA+P/+o4eCC0CA+P/+o4eCC1SA+P/+o56BC1sA0QIABRAQFGAAYWC/0AAiABDNfrXJPnHMAACjh4ILaID4//6jh4ILfID4//6jh4ILkID4//6jh4ILpID4//6jh4ILuID4//6jnoELvwDRAgAFEKwAGAAYWC/0AAiABDNfrXJPnHMAAKOHggvMgPj//qOHggvggPj//qOHggv0gPj//qOHggwIgPj//qOHggwcgPj//qOegQwjANECAAUQrAAYABhYL/QACIAEM1+tck+ccwAAo4eCDDCA+P/+o4eCDESA+P/+o4eCDFiA+P/+o4eCDGyA+P/+o4eCDICA+P/+o56BDIcA0QIABRCsABgAGFgv9AAIgAQzX61yT5xzAACjh4IMlID4//6jh4IMqID4//6jh4IMvID4//6jh4IM0ID4//6jnoEM6wDRAgAFEKwAGAAYWC/0AAiABDNfrXJPnHMAAKOHgg0MgPj//qOHgg0ggPj//qOHgg00gPj//qOHgg1IgPj//qOegQ1PANECAAUQrAAYABhYL/QACIAEM1+tck+ccwAAo4eCDVyA+P/+o4eCDXCA+P/+o4eCDYSA+P/+o4eCDZiA+P/+o4eCDayA+P/+o56BDbMA0QIABRCsABgAGFgv9AAIgAQzX61yT5xzAACjh4INwID4//6jh4IN1ID4//6jh4IN6ID4//6jh4IN/ID4//6jh4IOEID4//6jnoEOFwDRAgAFEKwAGAAYWC/0AAiABDNfrXJPnHMAAKOHgg4kgPj//qOHgg44gPj//qOHgg5MgPj//qOHgg5ggPj//qOHgg50gPj//qOegQ57ANECAAUQrAAYABhYL/QACIAEM1+tck+ccwAAo4eCDoiA+P/+o4eCDpyA+P/+o4eCDrCA+P/+o4eCDsSA+P/+o4eCDtiA+P/+o56BDt8A0QIABRCsABgAGFgv9AAIgAQzX61yT5xzAACjh4IO7ID4//6jh4IPAID4//6jh4IPFID4//6jh4IPKID4//6jh4IPPID4//6jnoEPQwDRAgAFEKwAGAAYWC/0AAiABDNfrXJPnHMAAKOHgg9QgPj//qOHgg9kgPj//qOHgg94gPj//qOHgg+MgPj//qOHgg+ggPj//qOegQ+nANECAAUQEBRgAGFgv9AAIgAQzX61yT5xzAAAo4eCD7SA+P/+o4eCD8iA+P/+o4eCD9yA+P/+o4eCD/CA+P/+o4eCEASA+P/+o56BEAsA0QIABRCsABgAGFgv9AAIgAQzX61yT5xzAACjh4IQGID4//6jh4IQLID4//6jh4IQQID4//6jh4IQVID4//6jh4IQaID4//6jnoEQbwDRAgAFEKwAGAAYWC/0AAiABDNfrXJPnHMAAKOHghB8gPj//qOHghCQgPj//qOHghCkgPj//qOHghC4gPj//qOHghDMgPj//qOegRDTANECAAUQrAAYABhYL/QACIAEM1+tck+ccwAAo4eCEOCA+P/+o4eCEPSA+P/+o4eCEQiA+P/+o56BETcA0QIABRCsABgAGFgv9AAIgAQzX61yT5xzAACjh4IRQ4D4//6jh4IRV4D4//6jh4IRa4D4//6jh4IRf4D4//6jh4IRk4D4//6jnoERmwDRAgAFEKwAGAAYWC/0AAiABDNfrXJPnHMAAKOHghGngPj//qOHghG7gPj//qOHghHPgPj//qOHghHjgPj//qOHghH3gPj//qOegRH/ANECAAUQrAAYABhYL/QACIAEM1+tck+ccwAAo4eCEguA+P/+o4eCEh+A+P/+o4eCEjOA+P/+o4eCEkeA+P/+o4eCEluA+P/+o56BEmMA0QIABRCsABgAGFgv9AAIgAQzX61yT5xzAACjh4ISb4D4//6jh4ISg4D4//6jh4ISl4D4//6jh4ISq4D4//6jh4ISv4D4//6jnoESxwDRAgAFEKwAGAAYWC/0AAiABDNfrXJPnHMAAKOHghLTgPj//qOHghLngPj//qOHghL7gPj//qOHghMPgPj//qOHghMjgPj//qOegRMrANECAAUQrAAYABhYL/QACIAEM1+tck+ccwAAo4eCEzeA+P/+o4eCE0uA+P/+o4eCE1+A+P/+o4eCE3OA+P/+oAEAAAAAAAAPoYeCE4cA+P/+daKDB/KBHFO7a5G7j7OBB7eK94EB8YIDX/CBDA==";
const MP4$1 = "data:video/mp4;base64,AAAAIGZ0eXBpc29tAAACAGlzb21pc28yYXZjMW1wNDEAAAAIZnJlZQAACORtZGF03gIATGF2YzU4LjM1LjEwMAACMEAOAAACcQYF//9t3EXpvebZSLeWLNgg2SPu73gyNjQgLSBjb3JlIDE2MSByMzAyNyA0MTIxMjc3IC0gSC4yNjQvTVBFRy00IEFWQyBjb2RlYyAtIENvcHlsZWZ0IDIwMDMtMjAyMCAtIGh0dHA6Ly93d3cudmlkZW9sYW4ub3JnL3gyNjQuaHRtbCAtIG9wdGlvbnM6IGNhYmFjPTAgcmVmPTMgZGVibG9jaz0xOjA6MCBhbmFseXNlPTB4MToweDExMSBtZT1oZXggc3VibWU9NyBwc3k9MSBwc3lfcmQ9MS4wMDowLjAwIG1peGVkX3JlZj0xIG1lX3JhbmdlPTE2IGNocm9tYV9tZT0xIHRyZWxsaXM9MSA4eDhkY3Q9MCBjcW09MCBkZWFkem9uZT0yMSwxMSBmYXN0X3Bza2lwPTEgY2hyb21hX3FwX29mZnNldD0tMiB0aHJlYWRzPTcgbG9va2FoZWFkX3RocmVhZHM9MSBzbGljZWRfdGhyZWFkcz0wIG5yPTAgZGVjaW1hdGU9MSBpbnRlcmxhY2VkPTAgYmx1cmF5X2NvbXBhdD0wIGNvbnN0cmFpbmVkX2ludHJhPTAgYmZyYW1lcz0wIHdlaWdodHA9MCBrZXlpbnQ9MjUwIGtleWludF9taW49MTAgc2NlbmVjdXQ9NDAgaW50cmFfcmVmcmVzaD0wIHJjX2xvb2thaGVhZD00MCByYz1jcmYgbWJ0cmVlPTEgY3JmPTIzLjAgcWNvbXA9MC42MCBxcG1pbj0wIHFwbWF4PTY5IHFwc3RlcD00IGlwX3JhdGlvPTEuNDAgYXE9MToxLjAwAIAAAADvZYiED/JigADD7JycnJycnJycnJycnJycnJycnJ11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111114BGCAHARggBwEYIAcBGCAHARggBwAAAAdBmjgf4BLYARggBwEYIAcBGCAHARggBwAAAAdBmlQH+AS2ARggBwEYIAcBGCAHARggBwAAAAdBmmA/wCWwARggBw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EAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAA3HN0Y28AAAAAAAAAMwAAADAAAAOtAAADzAAAA+cAAAQCAAAEIQAABDwAAARXAAAEdgAABJEAAASsAAAEywAABOIAAAT5AAAFGAAABTMAAAVOAAAFbQAABYgAAAWjAAAFwgAABd0AAAX4AAAGDwAABioAAAZFAAAGZAAABn8AAAaaAAAGuQAABtQAAAbvAAAHDgAABykAAAc8AAAHWwAAB3YAAAeRAAAHsAAAB8sAAAfmAAAIBQAACCAAAAg7AAAIWgAACG0AAAiIAAAIpwAACMIAAAjdAAAI/AAAABpzZ3BkAQAAAHJvbGwAAAACAAAAAf//AAAAHHNiZ3AAAAAAcm9sbAAAAAEAAADSAAAAAQAAAGJ1ZHRhAAAAWm1ldGEAAAAAAAAAIWhkbHIAAAAAAAAAAG1kaXJhcHBsAAAAAAAAAAAAAAAALWlsc3QAAAAlqXRvbwAAAB1kYXRhAAAAAQAAAABMYXZmNTguMjAuMTAw";
// Detect native Wake Lock API support
const nativeWakeLock = () => "wakeLock" in navigator && window.navigator.userAgent.indexOf("Samsung") === -1 && isFalse(isIOS());
class NoSleep {
constructor(player) {
this.player = player;
this.enabled = false;
if (nativeWakeLock()) {
this.player.debug.log('NoSleep', 'Native Wake Lock API supported.');
this._wakeLock = null;
this.handleVisibilityChange = () => {
if (this._wakeLock !== null && document.visibilityState === "visible") {
this.enable();
}
};
document.addEventListener("visibilitychange", this.handleVisibilityChange);
document.addEventListener("fullscreenchange", this.handleVisibilityChange);
} else {
this.player.debug.log('NoSleep', 'Native Wake Lock API not supported. so use video element.');
// Set up no sleep video element
this.noSleepVideo = document.createElement("video");
this.noSleepVideo.setAttribute("title", "No Sleep");
this.noSleepVideo.setAttribute("playsinline", "");
this._addSourceToVideo(this.noSleepVideo, "webm", WEBM);
this._addSourceToVideo(this.noSleepVideo, "mp4", MP4$1);
Object.assign(this.noSleepVideo.style, {
position: "absolute",
left: "-100%",
top: "-100%"
});
document.querySelector('body').append(this.noSleepVideo);
this.handleNoSleepVideoTimeUpdate = () => {
if (this.noSleepVideo && this.noSleepVideo.currentTime > 4) {
this.noSleepVideo.currentTime = 1;
}
};
this.noSleepVideo.addEventListener('timeupdate', this.handleNoSleepVideoTimeUpdate);
}
}
destroy() {
if (this._wakeLock) {
this._wakeLock.release();
this._wakeLock = null;
}
if (this.noSleepVideo) {
if (this.handleNoSleepVideoTimeUpdate) {
this.noSleepVideo.removeEventListener('timeupdate', this.handleNoSleepVideoTimeUpdate);
}
try {
if (this.noSleepVideo.parentNode) {
this.noSleepVideo.parentNode.removeChild(this.noSleepVideo);
}
} catch (e) {
this.player.debug.warn('NoSleep', 'Failed to remove noSleepVideo element.');
}
this.noSleepVideo = null;
}
if (this.handleVisibilityChange) {
document.removeEventListener("visibilitychange", this.handleVisibilityChange);
document.removeEventListener("fullscreenchange", this.handleVisibilityChange);
}
}
_addSourceToVideo(element, type, dataURI) {
var source = document.createElement("source");
source.src = dataURI;
source.type = `video/${type}`;
element.appendChild(source);
}
get isEnabled() {
return this.enabled;
}
enable() {
const debug = this.player.debug;
if (nativeWakeLock()) {
return navigator.wakeLock.request("screen").then(wakeLock => {
this._wakeLock = wakeLock;
this.enabled = true;
debug.log('wakeLock', 'Wake Lock active.');
this._wakeLock.addEventListener("release", () => {
// ToDo: Potentially emit an event for the page to observe since
// Wake Lock releases happen when page visibility changes.
// (https://web.dev/wakelock/#wake-lock-lifecycle)
debug.log('wakeLock', 'Wake Lock released.');
});
}).catch(err => {
this.enabled = false;
debug.warn('wakeLock', `${err.name}, ${err.message}`);
throw err;
});
} else {
let playPromise = this.noSleepVideo.play();
return playPromise.then(res => {
debug.log('wakeLock', 'noSleepVideo Wake Lock active.');
this.enabled = true;
return res;
}).catch(err => {
debug.warn('wakeLock', `noSleepVideo ${err.name}, ${err.message}`);
this.enabled = false;
throw err;
});
}
}
disable() {
if (nativeWakeLock()) {
if (this._wakeLock) {
this._wakeLock.release();
}
this._wakeLock = null;
} else {
if (this.noSleepVideo) {
this.noSleepVideo.pause();
}
}
this.enabled = false;
this.player.debug.log('wakeLock', 'Disabling wake lock.');
}
}
function getDefaultExportFromCjs (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
var urlToolkit = {exports: {}};
(function (module, exports) {
// see https://tools.ietf.org/html/rfc1808
(function (root) {
var URL_REGEX =
/^(?=((?:[a-zA-Z0-9+\-.]+:)?))\1(?=((?:\/\/[^\/?#]*)?))\2(?=((?:(?:[^?#\/]*\/)*[^;?#\/]*)?))\3((?:;[^?#]*)?)(\?[^#]*)?(#[^]*)?$/;
var FIRST_SEGMENT_REGEX = /^(?=([^\/?#]*))\1([^]*)$/;
var SLASH_DOT_REGEX = /(?:\/|^)\.(?=\/)/g;
var SLASH_DOT_DOT_REGEX = /(?:\/|^)\.\.\/(?!\.\.\/)[^\/]*(?=\/)/g;
var URLToolkit = {
// If opts.alwaysNormalize is true then the path will always be normalized even when it starts with / or //
// E.g
// With opts.alwaysNormalize = false (default, spec compliant)
// http://a.com/b/cd + /e/f/../g => http://a.com/e/f/../g
// With opts.alwaysNormalize = true (not spec compliant)
// http://a.com/b/cd + /e/f/../g => http://a.com/e/g
buildAbsoluteURL: function (baseURL, relativeURL, opts) {
opts = opts || {};
// remove any remaining space and CRLF
baseURL = baseURL.trim();
relativeURL = relativeURL.trim();
if (!relativeURL) {
// 2a) If the embedded URL is entirely empty, it inherits the
// entire base URL (i.e., is set equal to the base URL)
// and we are done.
if (!opts.alwaysNormalize) {
return baseURL;
}
var basePartsForNormalise = URLToolkit.parseURL(baseURL);
if (!basePartsForNormalise) {
throw new Error('Error trying to parse base URL.');
}
basePartsForNormalise.path = URLToolkit.normalizePath(
basePartsForNormalise.path
);
return URLToolkit.buildURLFromParts(basePartsForNormalise);
}
var relativeParts = URLToolkit.parseURL(relativeURL);
if (!relativeParts) {
throw new Error('Error trying to parse relative URL.');
}
if (relativeParts.scheme) {
// 2b) If the embedded URL starts with a scheme name, it is
// interpreted as an absolute URL and we are done.
if (!opts.alwaysNormalize) {
return relativeURL;
}
relativeParts.path = URLToolkit.normalizePath(relativeParts.path);
return URLToolkit.buildURLFromParts(relativeParts);
}
var baseParts = URLToolkit.parseURL(baseURL);
if (!baseParts) {
throw new Error('Error trying to parse base URL.');
}
if (!baseParts.netLoc && baseParts.path && baseParts.path[0] !== '/') {
// If netLoc missing and path doesn't start with '/', assume everthing before the first '/' is the netLoc
// This causes 'example.com/a' to be handled as '//example.com/a' instead of '/example.com/a'
var pathParts = FIRST_SEGMENT_REGEX.exec(baseParts.path);
baseParts.netLoc = pathParts[1];
baseParts.path = pathParts[2];
}
if (baseParts.netLoc && !baseParts.path) {
baseParts.path = '/';
}
var builtParts = {
// 2c) Otherwise, the embedded URL inherits the scheme of
// the base URL.
scheme: baseParts.scheme,
netLoc: relativeParts.netLoc,
path: null,
params: relativeParts.params,
query: relativeParts.query,
fragment: relativeParts.fragment,
};
if (!relativeParts.netLoc) {
// 3) If the embedded URL's is non-empty, we skip to
// Step 7. Otherwise, the embedded URL inherits the
// (if any) of the base URL.
builtParts.netLoc = baseParts.netLoc;
// 4) If the embedded URL path is preceded by a slash "/", the
// path is not relative and we skip to Step 7.
if (relativeParts.path[0] !== '/') {
if (!relativeParts.path) {
// 5) If the embedded URL path is empty (and not preceded by a
// slash), then the embedded URL inherits the base URL path
builtParts.path = baseParts.path;
// 5a) if the embedded URL's is non-empty, we skip to
// step 7; otherwise, it inherits the of the base
// URL (if any) and
if (!relativeParts.params) {
builtParts.params = baseParts.params;
// 5b) if the embedded URL's is non-empty, we skip to
// step 7; otherwise, it inherits the of the base
// URL (if any) and we skip to step 7.
if (!relativeParts.query) {
builtParts.query = baseParts.query;
}
}
} else {
// 6) The last segment of the base URL's path (anything
// following the rightmost slash "/", or the entire path if no
// slash is present) is removed and the embedded URL's path is
// appended in its place.
var baseURLPath = baseParts.path;
var newPath =
baseURLPath.substring(0, baseURLPath.lastIndexOf('/') + 1) +
relativeParts.path;
builtParts.path = URLToolkit.normalizePath(newPath);
}
}
}
if (builtParts.path === null) {
builtParts.path = opts.alwaysNormalize
? URLToolkit.normalizePath(relativeParts.path)
: relativeParts.path;
}
return URLToolkit.buildURLFromParts(builtParts);
},
parseURL: function (url) {
var parts = URL_REGEX.exec(url);
if (!parts) {
return null;
}
return {
scheme: parts[1] || '',
netLoc: parts[2] || '',
path: parts[3] || '',
params: parts[4] || '',
query: parts[5] || '',
fragment: parts[6] || '',
};
},
normalizePath: function (path) {
// The following operations are
// then applied, in order, to the new path:
// 6a) All occurrences of "./", where "." is a complete path
// segment, are removed.
// 6b) If the path ends with "." as a complete path segment,
// that "." is removed.
path = path.split('').reverse().join('').replace(SLASH_DOT_REGEX, '');
// 6c) All occurrences of "/../", where is a
// complete path segment not equal to "..", are removed.
// Removal of these path segments is performed iteratively,
// removing the leftmost matching pattern on each iteration,
// until no matching pattern remains.
// 6d) If the path ends with "/..", where is a
// complete path segment not equal to "..", that
// "/.." is removed.
while (
path.length !== (path = path.replace(SLASH_DOT_DOT_REGEX, '')).length
) {}
return path.split('').reverse().join('');
},
buildURLFromParts: function (parts) {
return (
parts.scheme +
parts.netLoc +
parts.path +
parts.params +
parts.query +
parts.fragment
);
},
};
module.exports = URLToolkit;
})();
} (urlToolkit));
var urlToolkitExports = urlToolkit.exports;
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
enumerableOnly && (symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
})), keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = null != arguments[i] ? arguments[i] : {};
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
_defineProperty(target, key, source[key]);
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
return target;
}
function _defineProperty(obj, key, value) {
key = _toPropertyKey(key);
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _extends() {
_extends = Object.assign ? Object.assign.bind() : function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}
function _toPrimitive(input, hint) {
if (typeof input !== "object" || input === null) return input;
var prim = input[Symbol.toPrimitive];
if (prim !== undefined) {
var res = prim.call(input, hint || "default");
if (typeof res !== "object") return res;
throw new TypeError("@@toPrimitive must return a primitive value.");
}
return (hint === "string" ? String : Number)(input);
}
function _toPropertyKey(arg) {
var key = _toPrimitive(arg, "string");
return typeof key === "symbol" ? key : String(key);
}
const isFiniteNumber = Number.isFinite || function (value) {
return typeof value === 'number' && isFinite(value);
};
let Events = /*#__PURE__*/function (Events) {
Events["MEDIA_ATTACHING"] = "hlsMediaAttaching";
Events["MEDIA_ATTACHED"] = "hlsMediaAttached";
Events["MEDIA_DETACHING"] = "hlsMediaDetaching";
Events["MEDIA_DETACHED"] = "hlsMediaDetached";
Events["BUFFER_RESET"] = "hlsBufferReset";
Events["BUFFER_CODECS"] = "hlsBufferCodecs";
Events["BUFFER_CREATED"] = "hlsBufferCreated";
Events["BUFFER_APPENDING"] = "hlsBufferAppending";
Events["BUFFER_APPENDED"] = "hlsBufferAppended";
Events["BUFFER_EOS"] = "hlsBufferEos";
Events["BUFFER_FLUSHING"] = "hlsBufferFlushing";
Events["BUFFER_FLUSHED"] = "hlsBufferFlushed";
Events["MANIFEST_LOADING"] = "hlsManifestLoading";
Events["MANIFEST_LOADED"] = "hlsManifestLoaded";
Events["MANIFEST_PARSED"] = "hlsManifestParsed";
Events["LEVEL_SWITCHING"] = "hlsLevelSwitching";
Events["LEVEL_SWITCHED"] = "hlsLevelSwitched";
Events["LEVEL_LOADING"] = "hlsLevelLoading";
Events["LEVEL_LOADED"] = "hlsLevelLoaded";
Events["LEVEL_UPDATED"] = "hlsLevelUpdated";
Events["LEVEL_PTS_UPDATED"] = "hlsLevelPtsUpdated";
Events["LEVELS_UPDATED"] = "hlsLevelsUpdated";
Events["AUDIO_TRACKS_UPDATED"] = "hlsAudioTracksUpdated";
Events["AUDIO_TRACK_SWITCHING"] = "hlsAudioTrackSwitching";
Events["AUDIO_TRACK_SWITCHED"] = "hlsAudioTrackSwitched";
Events["AUDIO_TRACK_LOADING"] = "hlsAudioTrackLoading";
Events["AUDIO_TRACK_LOADED"] = "hlsAudioTrackLoaded";
Events["SUBTITLE_TRACKS_UPDATED"] = "hlsSubtitleTracksUpdated";
Events["SUBTITLE_TRACKS_CLEARED"] = "hlsSubtitleTracksCleared";
Events["SUBTITLE_TRACK_SWITCH"] = "hlsSubtitleTrackSwitch";
Events["SUBTITLE_TRACK_LOADING"] = "hlsSubtitleTrackLoading";
Events["SUBTITLE_TRACK_LOADED"] = "hlsSubtitleTrackLoaded";
Events["SUBTITLE_FRAG_PROCESSED"] = "hlsSubtitleFragProcessed";
Events["CUES_PARSED"] = "hlsCuesParsed";
Events["NON_NATIVE_TEXT_TRACKS_FOUND"] = "hlsNonNativeTextTracksFound";
Events["INIT_PTS_FOUND"] = "hlsInitPtsFound";
Events["FRAG_LOADING"] = "hlsFragLoading";
Events["FRAG_LOAD_EMERGENCY_ABORTED"] = "hlsFragLoadEmergencyAborted";
Events["FRAG_LOADED"] = "hlsFragLoaded";
Events["FRAG_DECRYPTED"] = "hlsFragDecrypted";
Events["FRAG_PARSING_INIT_SEGMENT"] = "hlsFragParsingInitSegment";
Events["FRAG_PARSING_USERDATA"] = "hlsFragParsingUserdata";
Events["FRAG_PARSING_METADATA"] = "hlsFragParsingMetadata";
Events["FRAG_PARSED"] = "hlsFragParsed";
Events["FRAG_BUFFERED"] = "hlsFragBuffered";
Events["FRAG_CHANGED"] = "hlsFragChanged";
Events["FPS_DROP"] = "hlsFpsDrop";
Events["FPS_DROP_LEVEL_CAPPING"] = "hlsFpsDropLevelCapping";
Events["ERROR"] = "hlsError";
Events["DESTROYING"] = "hlsDestroying";
Events["KEY_LOADING"] = "hlsKeyLoading";
Events["KEY_LOADED"] = "hlsKeyLoaded";
Events["LIVE_BACK_BUFFER_REACHED"] = "hlsLiveBackBufferReached";
Events["BACK_BUFFER_REACHED"] = "hlsBackBufferReached";
return Events;
}({});
/**
* Defines each Event type and payload by Event name. Used in {@link hls.js#HlsEventEmitter} to strongly type the event listener API.
*/
let ErrorTypes = /*#__PURE__*/function (ErrorTypes) {
ErrorTypes["NETWORK_ERROR"] = "networkError";
ErrorTypes["MEDIA_ERROR"] = "mediaError";
ErrorTypes["KEY_SYSTEM_ERROR"] = "keySystemError";
ErrorTypes["MUX_ERROR"] = "muxError";
ErrorTypes["OTHER_ERROR"] = "otherError";
return ErrorTypes;
}({});
let ErrorDetails = /*#__PURE__*/function (ErrorDetails) {
ErrorDetails["KEY_SYSTEM_NO_KEYS"] = "keySystemNoKeys";
ErrorDetails["KEY_SYSTEM_NO_ACCESS"] = "keySystemNoAccess";
ErrorDetails["KEY_SYSTEM_NO_SESSION"] = "keySystemNoSession";
ErrorDetails["KEY_SYSTEM_NO_CONFIGURED_LICENSE"] = "keySystemNoConfiguredLicense";
ErrorDetails["KEY_SYSTEM_LICENSE_REQUEST_FAILED"] = "keySystemLicenseRequestFailed";
ErrorDetails["KEY_SYSTEM_SERVER_CERTIFICATE_REQUEST_FAILED"] = "keySystemServerCertificateRequestFailed";
ErrorDetails["KEY_SYSTEM_SERVER_CERTIFICATE_UPDATE_FAILED"] = "keySystemServerCertificateUpdateFailed";
ErrorDetails["KEY_SYSTEM_SESSION_UPDATE_FAILED"] = "keySystemSessionUpdateFailed";
ErrorDetails["KEY_SYSTEM_STATUS_OUTPUT_RESTRICTED"] = "keySystemStatusOutputRestricted";
ErrorDetails["KEY_SYSTEM_STATUS_INTERNAL_ERROR"] = "keySystemStatusInternalError";
ErrorDetails["MANIFEST_LOAD_ERROR"] = "manifestLoadError";
ErrorDetails["MANIFEST_LOAD_TIMEOUT"] = "manifestLoadTimeOut";
ErrorDetails["MANIFEST_PARSING_ERROR"] = "manifestParsingError";
ErrorDetails["MANIFEST_INCOMPATIBLE_CODECS_ERROR"] = "manifestIncompatibleCodecsError";
ErrorDetails["LEVEL_EMPTY_ERROR"] = "levelEmptyError";
ErrorDetails["LEVEL_LOAD_ERROR"] = "levelLoadError";
ErrorDetails["LEVEL_LOAD_TIMEOUT"] = "levelLoadTimeOut";
ErrorDetails["LEVEL_PARSING_ERROR"] = "levelParsingError";
ErrorDetails["LEVEL_SWITCH_ERROR"] = "levelSwitchError";
ErrorDetails["AUDIO_TRACK_LOAD_ERROR"] = "audioTrackLoadError";
ErrorDetails["AUDIO_TRACK_LOAD_TIMEOUT"] = "audioTrackLoadTimeOut";
ErrorDetails["SUBTITLE_LOAD_ERROR"] = "subtitleTrackLoadError";
ErrorDetails["SUBTITLE_TRACK_LOAD_TIMEOUT"] = "subtitleTrackLoadTimeOut";
ErrorDetails["FRAG_LOAD_ERROR"] = "fragLoadError";
ErrorDetails["FRAG_LOAD_TIMEOUT"] = "fragLoadTimeOut";
ErrorDetails["FRAG_DECRYPT_ERROR"] = "fragDecryptError";
ErrorDetails["FRAG_PARSING_ERROR"] = "fragParsingError";
ErrorDetails["FRAG_GAP"] = "fragGap";
ErrorDetails["REMUX_ALLOC_ERROR"] = "remuxAllocError";
ErrorDetails["KEY_LOAD_ERROR"] = "keyLoadError";
ErrorDetails["KEY_LOAD_TIMEOUT"] = "keyLoadTimeOut";
ErrorDetails["BUFFER_ADD_CODEC_ERROR"] = "bufferAddCodecError";
ErrorDetails["BUFFER_INCOMPATIBLE_CODECS_ERROR"] = "bufferIncompatibleCodecsError";
ErrorDetails["BUFFER_APPEND_ERROR"] = "bufferAppendError";
ErrorDetails["BUFFER_APPENDING_ERROR"] = "bufferAppendingError";
ErrorDetails["BUFFER_STALLED_ERROR"] = "bufferStalledError";
ErrorDetails["BUFFER_FULL_ERROR"] = "bufferFullError";
ErrorDetails["BUFFER_SEEK_OVER_HOLE"] = "bufferSeekOverHole";
ErrorDetails["BUFFER_NUDGE_ON_STALL"] = "bufferNudgeOnStall";
ErrorDetails["INTERNAL_EXCEPTION"] = "internalException";
ErrorDetails["INTERNAL_ABORTED"] = "aborted";
ErrorDetails["UNKNOWN"] = "unknown";
return ErrorDetails;
}({});
const noop = function noop() {};
const fakeLogger = {
trace: noop,
debug: noop,
log: noop,
warn: noop,
info: noop,
error: noop
};
let exportedLogger = fakeLogger;
// let lastCallTime;
// function formatMsgWithTimeInfo(type, msg) {
// const now = Date.now();
// const diff = lastCallTime ? '+' + (now - lastCallTime) : '0';
// lastCallTime = now;
// msg = (new Date(now)).toISOString() + ' | [' + type + '] > ' + msg + ' ( ' + diff + ' ms )';
// return msg;
// }
function consolePrintFn(type) {
const func = self.console[type];
if (func) {
return func.bind(self.console, `[${type}] >`);
}
return noop;
}
function exportLoggerFunctions(debugConfig, ...functions) {
functions.forEach(function (type) {
exportedLogger[type] = debugConfig[type] ? debugConfig[type].bind(debugConfig) : consolePrintFn(type);
});
}
function enableLogs(debugConfig, id) {
// check that console is available
if (self.console && debugConfig === true || typeof debugConfig === 'object') {
exportLoggerFunctions(debugConfig,
// Remove out from list here to hard-disable a log-level
// 'trace',
'debug', 'log', 'info', 'warn', 'error');
// Some browsers don't allow to use bind on console object anyway
// fallback to default if needed
try {
exportedLogger.log(`Debug logs enabled for "${id}" in hls.js version ${"1.4.12"}`);
} catch (e) {
exportedLogger = fakeLogger;
}
} else {
exportedLogger = fakeLogger;
}
}
const logger = exportedLogger;
const DECIMAL_RESOLUTION_REGEX = /^(\d+)x(\d+)$/;
const ATTR_LIST_REGEX = /(.+?)=(".*?"|.*?)(?:,|$)/g;
// adapted from https://github.com/kanongil/node-m3u8parse/blob/master/attrlist.js
class AttrList {
constructor(attrs) {
if (typeof attrs === 'string') {
attrs = AttrList.parseAttrList(attrs);
}
for (const attr in attrs) {
if (attrs.hasOwnProperty(attr)) {
if (attr.substring(0, 2) === 'X-') {
this.clientAttrs = this.clientAttrs || [];
this.clientAttrs.push(attr);
}
this[attr] = attrs[attr];
}
}
}
decimalInteger(attrName) {
const intValue = parseInt(this[attrName], 10);
if (intValue > Number.MAX_SAFE_INTEGER) {
return Infinity;
}
return intValue;
}
hexadecimalInteger(attrName) {
if (this[attrName]) {
let stringValue = (this[attrName] || '0x').slice(2);
stringValue = (stringValue.length & 1 ? '0' : '') + stringValue;
const value = new Uint8Array(stringValue.length / 2);
for (let i = 0; i < stringValue.length / 2; i++) {
value[i] = parseInt(stringValue.slice(i * 2, i * 2 + 2), 16);
}
return value;
} else {
return null;
}
}
hexadecimalIntegerAsNumber(attrName) {
const intValue = parseInt(this[attrName], 16);
if (intValue > Number.MAX_SAFE_INTEGER) {
return Infinity;
}
return intValue;
}
decimalFloatingPoint(attrName) {
return parseFloat(this[attrName]);
}
optionalFloat(attrName, defaultValue) {
const value = this[attrName];
return value ? parseFloat(value) : defaultValue;
}
enumeratedString(attrName) {
return this[attrName];
}
bool(attrName) {
return this[attrName] === 'YES';
}
decimalResolution(attrName) {
const res = DECIMAL_RESOLUTION_REGEX.exec(this[attrName]);
if (res === null) {
return undefined;
}
return {
width: parseInt(res[1], 10),
height: parseInt(res[2], 10)
};
}
static parseAttrList(input) {
let match;
const attrs = {};
const quote = '"';
ATTR_LIST_REGEX.lastIndex = 0;
while ((match = ATTR_LIST_REGEX.exec(input)) !== null) {
let value = match[2];
if (value.indexOf(quote) === 0 && value.lastIndexOf(quote) === value.length - 1) {
value = value.slice(1, -1);
}
const name = match[1].trim();
attrs[name] = value;
}
return attrs;
}
}
// Avoid exporting const enum so that these values can be inlined
function isDateRangeCueAttribute(attrName) {
return attrName !== "ID" && attrName !== "CLASS" && attrName !== "START-DATE" && attrName !== "DURATION" && attrName !== "END-DATE" && attrName !== "END-ON-NEXT";
}
function isSCTE35Attribute(attrName) {
return attrName === "SCTE35-OUT" || attrName === "SCTE35-IN";
}
class DateRange {
constructor(dateRangeAttr, dateRangeWithSameId) {
this.attr = void 0;
this._startDate = void 0;
this._endDate = void 0;
this._badValueForSameId = void 0;
if (dateRangeWithSameId) {
const previousAttr = dateRangeWithSameId.attr;
for (const key in previousAttr) {
if (Object.prototype.hasOwnProperty.call(dateRangeAttr, key) && dateRangeAttr[key] !== previousAttr[key]) {
logger.warn(`DATERANGE tag attribute: "${key}" does not match for tags with ID: "${dateRangeAttr.ID}"`);
this._badValueForSameId = key;
break;
}
}
// Merge DateRange tags with the same ID
dateRangeAttr = _extends(new AttrList({}), previousAttr, dateRangeAttr);
}
this.attr = dateRangeAttr;
this._startDate = new Date(dateRangeAttr["START-DATE"]);
if ("END-DATE" in this.attr) {
const endDate = new Date(this.attr["END-DATE"]);
if (isFiniteNumber(endDate.getTime())) {
this._endDate = endDate;
}
}
}
get id() {
return this.attr.ID;
}
get class() {
return this.attr.CLASS;
}
get startDate() {
return this._startDate;
}
get endDate() {
if (this._endDate) {
return this._endDate;
}
const duration = this.duration;
if (duration !== null) {
return new Date(this._startDate.getTime() + duration * 1000);
}
return null;
}
get duration() {
if ("DURATION" in this.attr) {
const duration = this.attr.decimalFloatingPoint("DURATION");
if (isFiniteNumber(duration)) {
return duration;
}
} else if (this._endDate) {
return (this._endDate.getTime() - this._startDate.getTime()) / 1000;
}
return null;
}
get plannedDuration() {
if ("PLANNED-DURATION" in this.attr) {
return this.attr.decimalFloatingPoint("PLANNED-DURATION");
}
return null;
}
get endOnNext() {
return this.attr.bool("END-ON-NEXT");
}
get isValid() {
return !!this.id && !this._badValueForSameId && isFiniteNumber(this.startDate.getTime()) && (this.duration === null || this.duration >= 0) && (!this.endOnNext || !!this.class);
}
}
class LoadStats {
constructor() {
this.aborted = false;
this.loaded = 0;
this.retry = 0;
this.total = 0;
this.chunkCount = 0;
this.bwEstimate = 0;
this.loading = {
start: 0,
first: 0,
end: 0
};
this.parsing = {
start: 0,
end: 0
};
this.buffering = {
start: 0,
first: 0,
end: 0
};
}
}
var ElementaryStreamTypes = {
AUDIO: "audio",
VIDEO: "video",
AUDIOVIDEO: "audiovideo"
};
class BaseSegment {
// baseurl is the URL to the playlist
// relurl is the portion of the URL that comes from inside the playlist.
// Holds the types of data this fragment supports
constructor(baseurl) {
this._byteRange = null;
this._url = null;
this.baseurl = void 0;
this.relurl = void 0;
this.elementaryStreams = {
[ElementaryStreamTypes.AUDIO]: null,
[ElementaryStreamTypes.VIDEO]: null,
[ElementaryStreamTypes.AUDIOVIDEO]: null
};
this.baseurl = baseurl;
}
// setByteRange converts a EXT-X-BYTERANGE attribute into a two element array
setByteRange(value, previous) {
const params = value.split('@', 2);
const byteRange = [];
if (params.length === 1) {
byteRange[0] = previous ? previous.byteRangeEndOffset : 0;
} else {
byteRange[0] = parseInt(params[1]);
}
byteRange[1] = parseInt(params[0]) + byteRange[0];
this._byteRange = byteRange;
}
get byteRange() {
if (!this._byteRange) {
return [];
}
return this._byteRange;
}
get byteRangeStartOffset() {
return this.byteRange[0];
}
get byteRangeEndOffset() {
return this.byteRange[1];
}
get url() {
if (!this._url && this.baseurl && this.relurl) {
this._url = urlToolkitExports.buildAbsoluteURL(this.baseurl, this.relurl, {
alwaysNormalize: true
});
}
return this._url || '';
}
set url(value) {
this._url = value;
}
}
/**
* Object representing parsed data from an HLS Segment. Found in {@link hls.js#LevelDetails.fragments}.
*/
class Fragment extends BaseSegment {
// EXTINF has to be present for a m3u8 to be considered valid
// sn notates the sequence number for a segment, and if set to a string can be 'initSegment'
// levelkeys are the EXT-X-KEY tags that apply to this segment for decryption
// core difference from the private field _decryptdata is the lack of the initialized IV
// _decryptdata will set the IV for this segment based on the segment number in the fragment
// A string representing the fragment type
// A reference to the loader. Set while the fragment is loading, and removed afterwards. Used to abort fragment loading
// A reference to the key loader. Set while the key is loading, and removed afterwards. Used to abort key loading
// The level/track index to which the fragment belongs
// The continuity counter of the fragment
// The starting Presentation Time Stamp (PTS) of the fragment. Set after transmux complete.
// The ending Presentation Time Stamp (PTS) of the fragment. Set after transmux complete.
// The starting Decode Time Stamp (DTS) of the fragment. Set after transmux complete.
// The ending Decode Time Stamp (DTS) of the fragment. Set after transmux complete.
// The start time of the fragment, as listed in the manifest. Updated after transmux complete.
// Set by `updateFragPTSDTS` in level-helper
// The maximum starting Presentation Time Stamp (audio/video PTS) of the fragment. Set after transmux complete.
// The minimum ending Presentation Time Stamp (audio/video PTS) of the fragment. Set after transmux complete.
// Load/parse timing information
// A flag indicating whether the segment was downloaded in order to test bitrate, and was not buffered
// #EXTINF segment title
// The Media Initialization Section for this segment
// Fragment is the last fragment in the media playlist
// Fragment is marked by an EXT-X-GAP tag indicating that it does not contain media data and should not be loaded
constructor(type, baseurl) {
super(baseurl);
this._decryptdata = null;
this.rawProgramDateTime = null;
this.programDateTime = null;
this.tagList = [];
this.duration = 0;
this.sn = 0;
this.levelkeys = void 0;
this.type = void 0;
this.loader = null;
this.keyLoader = null;
this.level = -1;
this.cc = 0;
this.startPTS = void 0;
this.endPTS = void 0;
this.startDTS = void 0;
this.endDTS = void 0;
this.start = 0;
this.deltaPTS = void 0;
this.maxStartPTS = void 0;
this.minEndPTS = void 0;
this.stats = new LoadStats();
this.urlId = 0;
this.data = void 0;
this.bitrateTest = false;
this.title = null;
this.initSegment = null;
this.endList = void 0;
this.gap = void 0;
this.type = type;
}
get decryptdata() {
const {
levelkeys
} = this;
if (!levelkeys && !this._decryptdata) {
return null;
}
if (!this._decryptdata && this.levelkeys && !this.levelkeys.NONE) {
const key = this.levelkeys.identity;
if (key) {
this._decryptdata = key.getDecryptData(this.sn);
} else {
const keyFormats = Object.keys(this.levelkeys);
if (keyFormats.length === 1) {
return this._decryptdata = this.levelkeys[keyFormats[0]].getDecryptData(this.sn);
}
}
}
return this._decryptdata;
}
get end() {
return this.start + this.duration;
}
get endProgramDateTime() {
if (this.programDateTime === null) {
return null;
}
if (!isFiniteNumber(this.programDateTime)) {
return null;
}
const duration = !isFiniteNumber(this.duration) ? 0 : this.duration;
return this.programDateTime + duration * 1000;
}
get encrypted() {
var _this$_decryptdata;
// At the m3u8-parser level we need to add support for manifest signalled keyformats
// when we want the fragment to start reporting that it is encrypted.
// Currently, keyFormat will only be set for identity keys
if ((_this$_decryptdata = this._decryptdata) != null && _this$_decryptdata.encrypted) {
return true;
} else if (this.levelkeys) {
const keyFormats = Object.keys(this.levelkeys);
const len = keyFormats.length;
if (len > 1 || len === 1 && this.levelkeys[keyFormats[0]].encrypted) {
return true;
}
}
return false;
}
setKeyFormat(keyFormat) {
if (this.levelkeys) {
const key = this.levelkeys[keyFormat];
if (key && !this._decryptdata) {
this._decryptdata = key.getDecryptData(this.sn);
}
}
}
abortRequests() {
var _this$loader, _this$keyLoader;
(_this$loader = this.loader) == null ? void 0 : _this$loader.abort();
(_this$keyLoader = this.keyLoader) == null ? void 0 : _this$keyLoader.abort();
}
setElementaryStreamInfo(type, startPTS, endPTS, startDTS, endDTS, partial = false) {
const {
elementaryStreams
} = this;
const info = elementaryStreams[type];
if (!info) {
elementaryStreams[type] = {
startPTS,
endPTS,
startDTS,
endDTS,
partial
};
return;
}
info.startPTS = Math.min(info.startPTS, startPTS);
info.endPTS = Math.max(info.endPTS, endPTS);
info.startDTS = Math.min(info.startDTS, startDTS);
info.endDTS = Math.max(info.endDTS, endDTS);
}
clearElementaryStreamInfo() {
const {
elementaryStreams
} = this;
elementaryStreams[ElementaryStreamTypes.AUDIO] = null;
elementaryStreams[ElementaryStreamTypes.VIDEO] = null;
elementaryStreams[ElementaryStreamTypes.AUDIOVIDEO] = null;
}
}
/**
* Object representing parsed data from an HLS Partial Segment. Found in {@link hls.js#LevelDetails.partList}.
*/
class Part extends BaseSegment {
constructor(partAttrs, frag, baseurl, index, previous) {
super(baseurl);
this.fragOffset = 0;
this.duration = 0;
this.gap = false;
this.independent = false;
this.relurl = void 0;
this.fragment = void 0;
this.index = void 0;
this.stats = new LoadStats();
this.duration = partAttrs.decimalFloatingPoint('DURATION');
this.gap = partAttrs.bool('GAP');
this.independent = partAttrs.bool('INDEPENDENT');
this.relurl = partAttrs.enumeratedString('URI');
this.fragment = frag;
this.index = index;
const byteRange = partAttrs.enumeratedString('BYTERANGE');
if (byteRange) {
this.setByteRange(byteRange, previous);
}
if (previous) {
this.fragOffset = previous.fragOffset + previous.duration;
}
}
get start() {
return this.fragment.start + this.fragOffset;
}
get end() {
return this.start + this.duration;
}
get loaded() {
const {
elementaryStreams
} = this;
return !!(elementaryStreams.audio || elementaryStreams.video || elementaryStreams.audiovideo);
}
}
const DEFAULT_TARGET_DURATION = 10;
/**
* Object representing parsed data from an HLS Media Playlist. Found in {@link hls.js#Level.details}.
*/
class LevelDetails {
// Manifest reload synchronization
constructor(baseUrl) {
this.PTSKnown = false;
this.alignedSliding = false;
this.averagetargetduration = void 0;
this.endCC = 0;
this.endSN = 0;
this.fragments = void 0;
this.fragmentHint = void 0;
this.partList = null;
this.dateRanges = void 0;
this.live = true;
this.ageHeader = 0;
this.advancedDateTime = void 0;
this.updated = true;
this.advanced = true;
this.availabilityDelay = void 0;
this.misses = 0;
this.startCC = 0;
this.startSN = 0;
this.startTimeOffset = null;
this.targetduration = 0;
this.totalduration = 0;
this.type = null;
this.url = void 0;
this.m3u8 = '';
this.version = null;
this.canBlockReload = false;
this.canSkipUntil = 0;
this.canSkipDateRanges = false;
this.skippedSegments = 0;
this.recentlyRemovedDateranges = void 0;
this.partHoldBack = 0;
this.holdBack = 0;
this.partTarget = 0;
this.preloadHint = void 0;
this.renditionReports = void 0;
this.tuneInGoal = 0;
this.deltaUpdateFailed = void 0;
this.driftStartTime = 0;
this.driftEndTime = 0;
this.driftStart = 0;
this.driftEnd = 0;
this.encryptedFragments = void 0;
this.playlistParsingError = null;
this.variableList = null;
this.hasVariableRefs = false;
this.fragments = [];
this.encryptedFragments = [];
this.dateRanges = {};
this.url = baseUrl;
}
reloaded(previous) {
if (!previous) {
this.advanced = true;
this.updated = true;
return;
}
const partSnDiff = this.lastPartSn - previous.lastPartSn;
const partIndexDiff = this.lastPartIndex - previous.lastPartIndex;
this.updated = this.endSN !== previous.endSN || !!partIndexDiff || !!partSnDiff || !this.live;
this.advanced = this.endSN > previous.endSN || partSnDiff > 0 || partSnDiff === 0 && partIndexDiff > 0;
if (this.updated || this.advanced) {
this.misses = Math.floor(previous.misses * 0.6);
} else {
this.misses = previous.misses + 1;
}
this.availabilityDelay = previous.availabilityDelay;
}
get hasProgramDateTime() {
if (this.fragments.length) {
return isFiniteNumber(this.fragments[this.fragments.length - 1].programDateTime);
}
return false;
}
get levelTargetDuration() {
return this.averagetargetduration || this.targetduration || DEFAULT_TARGET_DURATION;
}
get drift() {
const runTime = this.driftEndTime - this.driftStartTime;
if (runTime > 0) {
const runDuration = this.driftEnd - this.driftStart;
return runDuration * 1000 / runTime;
}
return 1;
}
get edge() {
return this.partEnd || this.fragmentEnd;
}
get partEnd() {
var _this$partList;
if ((_this$partList = this.partList) != null && _this$partList.length) {
return this.partList[this.partList.length - 1].end;
}
return this.fragmentEnd;
}
get fragmentEnd() {
var _this$fragments;
if ((_this$fragments = this.fragments) != null && _this$fragments.length) {
return this.fragments[this.fragments.length - 1].end;
}
return 0;
}
get age() {
if (this.advancedDateTime) {
return Math.max(Date.now() - this.advancedDateTime, 0) / 1000;
}
return 0;
}
get lastPartIndex() {
var _this$partList2;
if ((_this$partList2 = this.partList) != null && _this$partList2.length) {
return this.partList[this.partList.length - 1].index;
}
return -1;
}
get lastPartSn() {
var _this$partList3;
if ((_this$partList3 = this.partList) != null && _this$partList3.length) {
return this.partList[this.partList.length - 1].fragment.sn;
}
return this.endSN;
}
}
function base64Decode(base64encodedStr) {
return Uint8Array.from(atob(base64encodedStr), c => c.charCodeAt(0));
}
function getKeyIdBytes(str) {
const keyIdbytes = strToUtf8array(str).subarray(0, 16);
const paddedkeyIdbytes = new Uint8Array(16);
paddedkeyIdbytes.set(keyIdbytes, 16 - keyIdbytes.length);
return paddedkeyIdbytes;
}
function changeEndianness(keyId) {
const swap = function swap(array, from, to) {
const cur = array[from];
array[from] = array[to];
array[to] = cur;
};
swap(keyId, 0, 3);
swap(keyId, 1, 2);
swap(keyId, 4, 5);
swap(keyId, 6, 7);
}
function convertDataUriToArrayBytes(uri) {
// data:[
const colonsplit = uri.split(':');
let keydata = null;
if (colonsplit[0] === 'data' && colonsplit.length === 2) {
const semicolonsplit = colonsplit[1].split(';');
const commasplit = semicolonsplit[semicolonsplit.length - 1].split(',');
if (commasplit.length === 2) {
const isbase64 = commasplit[0] === 'base64';
const data = commasplit[1];
if (isbase64) {
semicolonsplit.splice(-1, 1); // remove from processing
keydata = base64Decode(data);
} else {
keydata = getKeyIdBytes(data);
}
}
}
return keydata;
}
function strToUtf8array(str) {
return Uint8Array.from(unescape(encodeURIComponent(str)), c => c.charCodeAt(0));
}
/**
* @see https://developer.mozilla.org/en-US/docs/Web/API/Navigator/requestMediaKeySystemAccess
*/
var KeySystems = {
CLEARKEY: "org.w3.clearkey",
FAIRPLAY: "com.apple.fps",
PLAYREADY: "com.microsoft.playready",
WIDEVINE: "com.widevine.alpha"
};
// Playlist #EXT-X-KEY KEYFORMAT values
var KeySystemFormats = {
CLEARKEY: "org.w3.clearkey",
FAIRPLAY: "com.apple.streamingkeydelivery",
PLAYREADY: "com.microsoft.playready",
WIDEVINE: "urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed"
};
function keySystemFormatToKeySystemDomain(format) {
switch (format) {
case KeySystemFormats.FAIRPLAY:
return KeySystems.FAIRPLAY;
case KeySystemFormats.PLAYREADY:
return KeySystems.PLAYREADY;
case KeySystemFormats.WIDEVINE:
return KeySystems.WIDEVINE;
case KeySystemFormats.CLEARKEY:
return KeySystems.CLEARKEY;
}
}
// System IDs for which we can extract a key ID from "encrypted" event PSSH
var KeySystemIds = {
WIDEVINE: "edef8ba979d64acea3c827dcd51d21ed"
};
function keySystemIdToKeySystemDomain(systemId) {
if (systemId === KeySystemIds.WIDEVINE) {
return KeySystems.WIDEVINE;
// } else if (systemId === KeySystemIds.PLAYREADY) {
// return KeySystems.PLAYREADY;
// } else if (systemId === KeySystemIds.CENC || systemId === KeySystemIds.CLEARKEY) {
// return KeySystems.CLEARKEY;
}
}
function keySystemDomainToKeySystemFormat(keySystem) {
switch (keySystem) {
case KeySystems.FAIRPLAY:
return KeySystemFormats.FAIRPLAY;
case KeySystems.PLAYREADY:
return KeySystemFormats.PLAYREADY;
case KeySystems.WIDEVINE:
return KeySystemFormats.WIDEVINE;
case KeySystems.CLEARKEY:
return KeySystemFormats.CLEARKEY;
}
}
function getKeySystemsForConfig(config) {
const {
drmSystems,
widevineLicenseUrl
} = config;
const keySystemsToAttempt = drmSystems ? [KeySystems.FAIRPLAY, KeySystems.WIDEVINE, KeySystems.PLAYREADY, KeySystems.CLEARKEY].filter(keySystem => !!drmSystems[keySystem]) : [];
if (!keySystemsToAttempt[KeySystems.WIDEVINE] && widevineLicenseUrl) {
keySystemsToAttempt.push(KeySystems.WIDEVINE);
}
return keySystemsToAttempt;
}
const requestMediaKeySystemAccess = function () {
if (typeof self !== 'undefined' && self.navigator && self.navigator.requestMediaKeySystemAccess) {
return self.navigator.requestMediaKeySystemAccess.bind(self.navigator);
} else {
return null;
}
}();
/**
* @see https://developer.mozilla.org/en-US/docs/Web/API/MediaKeySystemConfiguration
*/
function getSupportedMediaKeySystemConfigurations(keySystem, audioCodecs, videoCodecs, drmSystemOptions) {
let initDataTypes;
switch (keySystem) {
case KeySystems.FAIRPLAY:
initDataTypes = ['cenc', 'sinf'];
break;
case KeySystems.WIDEVINE:
case KeySystems.PLAYREADY:
initDataTypes = ['cenc'];
break;
case KeySystems.CLEARKEY:
initDataTypes = ['cenc', 'keyids'];
break;
default:
throw new Error(`Unknown key-system: ${keySystem}`);
}
return createMediaKeySystemConfigurations(initDataTypes, audioCodecs, videoCodecs, drmSystemOptions);
}
function createMediaKeySystemConfigurations(initDataTypes, audioCodecs, videoCodecs, drmSystemOptions) {
const baseConfig = {
initDataTypes: initDataTypes,
persistentState: drmSystemOptions.persistentState || 'not-allowed',
distinctiveIdentifier: drmSystemOptions.distinctiveIdentifier || 'not-allowed',
sessionTypes: drmSystemOptions.sessionTypes || [drmSystemOptions.sessionType || 'temporary'],
audioCapabilities: audioCodecs.map(codec => ({
contentType: `audio/mp4; codecs="${codec}"`,
robustness: drmSystemOptions.audioRobustness || '',
encryptionScheme: drmSystemOptions.audioEncryptionScheme || null
})),
videoCapabilities: videoCodecs.map(codec => ({
contentType: `video/mp4; codecs="${codec}"`,
robustness: drmSystemOptions.videoRobustness || '',
encryptionScheme: drmSystemOptions.videoEncryptionScheme || null
}))
};
return [baseConfig];
}
function sliceUint8(array, start, end) {
// @ts-expect-error This polyfills IE11 usage of Uint8Array slice.
// It always exists in the TypeScript definition so fails, but it fails at runtime on IE11.
return Uint8Array.prototype.slice ? array.slice(start, end) : new Uint8Array(Array.prototype.slice.call(array, start, end));
}
// breaking up those two types in order to clarify what is happening in the decoding path.
/**
* Returns true if an ID3 header can be found at offset in data
* @param data - The data to search
* @param offset - The offset at which to start searching
*/
const isHeader$2 = (data, offset) => {
/*
* http://id3.org/id3v2.3.0
* [0] = 'I'
* [1] = 'D'
* [2] = '3'
* [3,4] = {Version}
* [5] = {Flags}
* [6-9] = {ID3 Size}
*
* An ID3v2 tag can be detected with the following pattern:
* $49 44 33 yy yy xx zz zz zz zz
* Where yy is less than $FF, xx is the 'flags' byte and zz is less than $80
*/
if (offset + 10 <= data.length) {
// look for 'ID3' identifier
if (data[offset] === 0x49 && data[offset + 1] === 0x44 && data[offset + 2] === 0x33) {
// check version is within range
if (data[offset + 3] < 0xff && data[offset + 4] < 0xff) {
// check size is within range
if (data[offset + 6] < 0x80 && data[offset + 7] < 0x80 && data[offset + 8] < 0x80 && data[offset + 9] < 0x80) {
return true;
}
}
}
}
return false;
};
/**
* Returns true if an ID3 footer can be found at offset in data
* @param data - The data to search
* @param offset - The offset at which to start searching
*/
const isFooter = (data, offset) => {
/*
* The footer is a copy of the header, but with a different identifier
*/
if (offset + 10 <= data.length) {
// look for '3DI' identifier
if (data[offset] === 0x33 && data[offset + 1] === 0x44 && data[offset + 2] === 0x49) {
// check version is within range
if (data[offset + 3] < 0xff && data[offset + 4] < 0xff) {
// check size is within range
if (data[offset + 6] < 0x80 && data[offset + 7] < 0x80 && data[offset + 8] < 0x80 && data[offset + 9] < 0x80) {
return true;
}
}
}
}
return false;
};
/**
* Returns any adjacent ID3 tags found in data starting at offset, as one block of data
* @param data - The data to search in
* @param offset - The offset at which to start searching
* @returns the block of data containing any ID3 tags found
* or *undefined* if no header is found at the starting offset
*/
const getID3Data = (data, offset) => {
const front = offset;
let length = 0;
while (isHeader$2(data, offset)) {
// ID3 header is 10 bytes
length += 10;
const size = readSize(data, offset + 6);
length += size;
if (isFooter(data, offset + 10)) {
// ID3 footer is 10 bytes
length += 10;
}
offset += length;
}
if (length > 0) {
return data.subarray(front, front + length);
}
return undefined;
};
const readSize = (data, offset) => {
let size = 0;
size = (data[offset] & 0x7f) << 21;
size |= (data[offset + 1] & 0x7f) << 14;
size |= (data[offset + 2] & 0x7f) << 7;
size |= data[offset + 3] & 0x7f;
return size;
};
const canParse$2 = (data, offset) => {
return isHeader$2(data, offset) && readSize(data, offset + 6) + 10 <= data.length - offset;
};
/**
* Searches for the Elementary Stream timestamp found in the ID3 data chunk
* @param data - Block of data containing one or more ID3 tags
*/
const getTimeStamp = data => {
const frames = getID3Frames(data);
for (let i = 0; i < frames.length; i++) {
const frame = frames[i];
if (isTimeStampFrame(frame)) {
return readTimeStamp(frame);
}
}
return undefined;
};
/**
* Returns true if the ID3 frame is an Elementary Stream timestamp frame
*/
const isTimeStampFrame = frame => {
return frame && frame.key === 'PRIV' && frame.info === 'com.apple.streaming.transportStreamTimestamp';
};
const getFrameData = data => {
/*
Frame ID $xx xx xx xx (four characters)
Size $xx xx xx xx
Flags $xx xx
*/
const type = String.fromCharCode(data[0], data[1], data[2], data[3]);
const size = readSize(data, 4);
// skip frame id, size, and flags
const offset = 10;
return {
type,
size,
data: data.subarray(offset, offset + size)
};
};
/**
* Returns an array of ID3 frames found in all the ID3 tags in the id3Data
* @param id3Data - The ID3 data containing one or more ID3 tags
*/
const getID3Frames = id3Data => {
let offset = 0;
const frames = [];
while (isHeader$2(id3Data, offset)) {
const size = readSize(id3Data, offset + 6);
// skip past ID3 header
offset += 10;
const end = offset + size;
// loop through frames in the ID3 tag
while (offset + 8 < end) {
const frameData = getFrameData(id3Data.subarray(offset));
const frame = decodeFrame(frameData);
if (frame) {
frames.push(frame);
}
// skip frame header and frame data
offset += frameData.size + 10;
}
if (isFooter(id3Data, offset)) {
offset += 10;
}
}
return frames;
};
const decodeFrame = frame => {
if (frame.type === 'PRIV') {
return decodePrivFrame(frame);
} else if (frame.type[0] === 'W') {
return decodeURLFrame(frame);
}
return decodeTextFrame(frame);
};
const decodePrivFrame = frame => {
/*
Format: \0
*/
if (frame.size < 2) {
return undefined;
}
const owner = utf8ArrayToStr(frame.data, true);
const privateData = new Uint8Array(frame.data.subarray(owner.length + 1));
return {
key: frame.type,
info: owner,
data: privateData.buffer
};
};
const decodeTextFrame = frame => {
if (frame.size < 2) {
return undefined;
}
if (frame.type === 'TXXX') {
/*
Format:
[0] = {Text Encoding}
[1-?] = {Description}\0{Value}
*/
let index = 1;
const description = utf8ArrayToStr(frame.data.subarray(index), true);
index += description.length + 1;
const value = utf8ArrayToStr(frame.data.subarray(index));
return {
key: frame.type,
info: description,
data: value
};
}
/*
Format:
[0] = {Text Encoding}
[1-?] = {Value}
*/
const text = utf8ArrayToStr(frame.data.subarray(1));
return {
key: frame.type,
data: text
};
};
const decodeURLFrame = frame => {
if (frame.type === 'WXXX') {
/*
Format:
[0] = {Text Encoding}
[1-?] = {Description}\0{URL}
*/
if (frame.size < 2) {
return undefined;
}
let index = 1;
const description = utf8ArrayToStr(frame.data.subarray(index), true);
index += description.length + 1;
const value = utf8ArrayToStr(frame.data.subarray(index));
return {
key: frame.type,
info: description,
data: value
};
}
/*
Format:
[0-?] = {URL}
*/
const url = utf8ArrayToStr(frame.data);
return {
key: frame.type,
data: url
};
};
const readTimeStamp = timeStampFrame => {
if (timeStampFrame.data.byteLength === 8) {
const data = new Uint8Array(timeStampFrame.data);
// timestamp is 33 bit expressed as a big-endian eight-octet number,
// with the upper 31 bits set to zero.
const pts33Bit = data[3] & 0x1;
let timestamp = (data[4] << 23) + (data[5] << 15) + (data[6] << 7) + data[7];
timestamp /= 45;
if (pts33Bit) {
timestamp += 47721858.84;
} // 2^32 / 90
return Math.round(timestamp);
}
return undefined;
};
// http://stackoverflow.com/questions/8936984/uint8array-to-string-in-javascript/22373197
// http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt
/* utf.js - UTF-8 <=> UTF-16 convertion
*
* Copyright (C) 1999 Masanao Izumo
* Version: 1.0
* LastModified: Dec 25 1999
* This library is free. You can redistribute it and/or modify it.
*/
const utf8ArrayToStr = (array, exitOnNull = false) => {
const decoder = getTextDecoder();
if (decoder) {
const decoded = decoder.decode(array);
if (exitOnNull) {
// grab up to the first null
const idx = decoded.indexOf('\0');
return idx !== -1 ? decoded.substring(0, idx) : decoded;
}
// remove any null characters
return decoded.replace(/\0/g, '');
}
const len = array.length;
let c;
let char2;
let char3;
let out = '';
let i = 0;
while (i < len) {
c = array[i++];
if (c === 0x00 && exitOnNull) {
return out;
} else if (c === 0x00 || c === 0x03) {
// If the character is 3 (END_OF_TEXT) or 0 (NULL) then skip it
continue;
}
switch (c >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12:
case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode((c & 0x1f) << 6 | char2 & 0x3f);
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode((c & 0x0f) << 12 | (char2 & 0x3f) << 6 | (char3 & 0x3f) << 0);
break;
}
}
return out;
};
let decoder;
function getTextDecoder() {
if (!decoder && typeof self.TextDecoder !== 'undefined') {
decoder = new self.TextDecoder('utf-8');
}
return decoder;
}
/**
* hex dump helper class
*/
const Hex = {
hexDump: function (array) {
let str = '';
for (let i = 0; i < array.length; i++) {
let h = array[i].toString(16);
if (h.length < 2) {
h = '0' + h;
}
str += h;
}
return str;
}
};
const UINT32_MAX$1 = Math.pow(2, 32) - 1;
const push = [].push;
// We are using fixed track IDs for driving the MP4 remuxer
// instead of following the TS PIDs.
// There is no reason not to do this and some browsers/SourceBuffer-demuxers
// may not like if there are TrackID "switches"
// See https://github.com/video-dev/hls.js/issues/1331
// Here we are mapping our internal track types to constant MP4 track IDs
// With MSE currently one can only have one track of each, and we are muxing
// whatever video/audio rendition in them.
const RemuxerTrackIdConfig = {
video: 1,
audio: 2,
id3: 3,
text: 4
};
function bin2str(data) {
return String.fromCharCode.apply(null, data);
}
function readUint16(buffer, offset) {
const val = buffer[offset] << 8 | buffer[offset + 1];
return val < 0 ? 65536 + val : val;
}
function readUint32(buffer, offset) {
const val = readSint32(buffer, offset);
return val < 0 ? 4294967296 + val : val;
}
function readSint32(buffer, offset) {
return buffer[offset] << 24 | buffer[offset + 1] << 16 | buffer[offset + 2] << 8 | buffer[offset + 3];
}
function writeUint32(buffer, offset, value) {
buffer[offset] = value >> 24;
buffer[offset + 1] = value >> 16 & 0xff;
buffer[offset + 2] = value >> 8 & 0xff;
buffer[offset + 3] = value & 0xff;
}
// Find the data for a box specified by its path
function findBox(data, path) {
const results = [];
if (!path.length) {
// short-circuit the search for empty paths
return results;
}
const end = data.byteLength;
for (let i = 0; i < end;) {
const size = readUint32(data, i);
const type = bin2str(data.subarray(i + 4, i + 8));
const endbox = size > 1 ? i + size : end;
if (type === path[0]) {
if (path.length === 1) {
// this is the end of the path and we've found the box we were
// looking for
results.push(data.subarray(i + 8, endbox));
} else {
// recursively search for the next box along the path
const subresults = findBox(data.subarray(i + 8, endbox), path.slice(1));
if (subresults.length) {
push.apply(results, subresults);
}
}
}
i = endbox;
}
// we've finished searching all of data
return results;
}
function parseSegmentIndex(sidx) {
const references = [];
const version = sidx[0];
// set initial offset, we skip the reference ID (not needed)
let index = 8;
const timescale = readUint32(sidx, index);
index += 4;
// TODO: parse earliestPresentationTime and firstOffset
// usually zero in our case
const earliestPresentationTime = 0;
const firstOffset = 0;
if (version === 0) {
index += 8;
} else {
index += 16;
}
// skip reserved
index += 2;
let startByte = sidx.length + firstOffset;
const referencesCount = readUint16(sidx, index);
index += 2;
for (let i = 0; i < referencesCount; i++) {
let referenceIndex = index;
const referenceInfo = readUint32(sidx, referenceIndex);
referenceIndex += 4;
const referenceSize = referenceInfo & 0x7fffffff;
const referenceType = (referenceInfo & 0x80000000) >>> 31;
if (referenceType === 1) {
logger.warn('SIDX has hierarchical references (not supported)');
return null;
}
const subsegmentDuration = readUint32(sidx, referenceIndex);
referenceIndex += 4;
references.push({
referenceSize,
subsegmentDuration,
// unscaled
info: {
duration: subsegmentDuration / timescale,
start: startByte,
end: startByte + referenceSize - 1
}
});
startByte += referenceSize;
// Skipping 1 bit for |startsWithSap|, 3 bits for |sapType|, and 28 bits
// for |sapDelta|.
referenceIndex += 4;
// skip to next ref
index = referenceIndex;
}
return {
earliestPresentationTime,
timescale,
version,
referencesCount,
references
};
}
/**
* Parses an MP4 initialization segment and extracts stream type and
* timescale values for any declared tracks. Timescale values indicate the
* number of clock ticks per second to assume for time-based values
* elsewhere in the MP4.
*
* To determine the start time of an MP4, you need two pieces of
* information: the timescale unit and the earliest base media decode
* time. Multiple timescales can be specified within an MP4 but the
* base media decode time is always expressed in the timescale from
* the media header box for the track:
* ```
* moov > trak > mdia > mdhd.timescale
* moov > trak > mdia > hdlr
* ```
* @param initSegment the bytes of the init segment
* @returns a hash of track type to timescale values or null if
* the init segment is malformed.
*/
function parseInitSegment(initSegment) {
const result = [];
const traks = findBox(initSegment, ['moov', 'trak']);
for (let i = 0; i < traks.length; i++) {
const trak = traks[i];
const tkhd = findBox(trak, ['tkhd'])[0];
if (tkhd) {
let version = tkhd[0];
let index = version === 0 ? 12 : 20;
const trackId = readUint32(tkhd, index);
const mdhd = findBox(trak, ['mdia', 'mdhd'])[0];
if (mdhd) {
version = mdhd[0];
index = version === 0 ? 12 : 20;
const timescale = readUint32(mdhd, index);
const hdlr = findBox(trak, ['mdia', 'hdlr'])[0];
if (hdlr) {
const hdlrType = bin2str(hdlr.subarray(8, 12));
const type = {
soun: ElementaryStreamTypes.AUDIO,
vide: ElementaryStreamTypes.VIDEO
}[hdlrType];
if (type) {
// Parse codec details
const stsd = findBox(trak, ['mdia', 'minf', 'stbl', 'stsd'])[0];
let codec;
if (stsd) {
codec = bin2str(stsd.subarray(12, 16));
// TODO: Parse codec details to be able to build MIME type.
// stsd.start += 8;
// const codecBox = findBox(stsd, [codec])[0];
// if (codecBox) {
// TODO: Codec parsing support for avc1, mp4a, hevc, av01...
// }
}
result[trackId] = {
timescale,
type
};
result[type] = {
timescale,
id: trackId,
codec
};
}
}
}
}
}
const trex = findBox(initSegment, ['moov', 'mvex', 'trex']);
trex.forEach(trex => {
const trackId = readUint32(trex, 4);
const track = result[trackId];
if (track) {
track.default = {
duration: readUint32(trex, 12),
flags: readUint32(trex, 20)
};
}
});
return result;
}
function patchEncyptionData(initSegment, decryptdata) {
if (!initSegment || !decryptdata) {
return initSegment;
}
const keyId = decryptdata.keyId;
if (keyId && decryptdata.isCommonEncryption) {
const traks = findBox(initSegment, ['moov', 'trak']);
traks.forEach(trak => {
const stsd = findBox(trak, ['mdia', 'minf', 'stbl', 'stsd'])[0];
// skip the sample entry count
const sampleEntries = stsd.subarray(8);
let encBoxes = findBox(sampleEntries, ['enca']);
const isAudio = encBoxes.length > 0;
if (!isAudio) {
encBoxes = findBox(sampleEntries, ['encv']);
}
encBoxes.forEach(enc => {
const encBoxChildren = isAudio ? enc.subarray(28) : enc.subarray(78);
const sinfBoxes = findBox(encBoxChildren, ['sinf']);
sinfBoxes.forEach(sinf => {
const tenc = parseSinf(sinf);
if (tenc) {
// Look for default key id (keyID offset is always 8 within the tenc box):
const tencKeyId = tenc.subarray(8, 24);
if (!tencKeyId.some(b => b !== 0)) {
logger.log(`[eme] Patching keyId in 'enc${isAudio ? 'a' : 'v'}>sinf>>tenc' box: ${Hex.hexDump(tencKeyId)} -> ${Hex.hexDump(keyId)}`);
tenc.set(keyId, 8);
}
}
});
});
});
}
return initSegment;
}
function parseSinf(sinf) {
const schm = findBox(sinf, ['schm'])[0];
if (schm) {
const scheme = bin2str(schm.subarray(4, 8));
if (scheme === 'cbcs' || scheme === 'cenc') {
return findBox(sinf, ['schi', 'tenc'])[0];
}
}
logger.error(`[eme] missing 'schm' box`);
return null;
}
/**
* Determine the base media decode start time, in seconds, for an MP4
* fragment. If multiple fragments are specified, the earliest time is
* returned.
*
* The base media decode time can be parsed from track fragment
* metadata:
* ```
* moof > traf > tfdt.baseMediaDecodeTime
* ```
* It requires the timescale value from the mdhd to interpret.
*
* @param initData - a hash of track type to timescale values
* @param fmp4 - the bytes of the mp4 fragment
* @returns the earliest base media decode start time for the
* fragment, in seconds
*/
function getStartDTS(initData, fmp4) {
// we need info from two children of each track fragment box
return findBox(fmp4, ['moof', 'traf']).reduce((result, traf) => {
const tfdt = findBox(traf, ['tfdt'])[0];
const version = tfdt[0];
const start = findBox(traf, ['tfhd']).reduce((result, tfhd) => {
// get the track id from the tfhd
const id = readUint32(tfhd, 4);
const track = initData[id];
if (track) {
let baseTime = readUint32(tfdt, 4);
if (version === 1) {
// If value is too large, assume signed 64-bit. Negative track fragment decode times are invalid, but they exist in the wild.
// This prevents large values from being used for initPTS, which can cause playlist sync issues.
// https://github.com/video-dev/hls.js/issues/5303
if (baseTime === UINT32_MAX$1) {
logger.warn(`[mp4-demuxer]: Ignoring assumed invalid signed 64-bit track fragment decode time`);
return result;
}
baseTime *= UINT32_MAX$1 + 1;
baseTime += readUint32(tfdt, 8);
}
// assume a 90kHz clock if no timescale was specified
const scale = track.timescale || 90e3;
// convert base time to seconds
const startTime = baseTime / scale;
if (isFinite(startTime) && (result === null || startTime < result)) {
return startTime;
}
}
return result;
}, null);
if (start !== null && isFinite(start) && (result === null || start < result)) {
return start;
}
return result;
}, null);
}
/*
For Reference:
aligned(8) class TrackFragmentHeaderBox
extends FullBox(‘tfhd’, 0, tf_flags){
unsigned int(32) track_ID;
// all the following are optional fields
unsigned int(64) base_data_offset;
unsigned int(32) sample_description_index;
unsigned int(32) default_sample_duration;
unsigned int(32) default_sample_size;
unsigned int(32) default_sample_flags
}
*/
function getDuration(data, initData) {
let rawDuration = 0;
let videoDuration = 0;
let audioDuration = 0;
const trafs = findBox(data, ['moof', 'traf']);
for (let i = 0; i < trafs.length; i++) {
const traf = trafs[i];
// There is only one tfhd & trun per traf
// This is true for CMAF style content, and we should perhaps check the ftyp
// and only look for a single trun then, but for ISOBMFF we should check
// for multiple track runs.
const tfhd = findBox(traf, ['tfhd'])[0];
// get the track id from the tfhd
const id = readUint32(tfhd, 4);
const track = initData[id];
if (!track) {
continue;
}
const trackDefault = track.default;
const tfhdFlags = readUint32(tfhd, 0) | (trackDefault == null ? void 0 : trackDefault.flags);
let sampleDuration = trackDefault == null ? void 0 : trackDefault.duration;
if (tfhdFlags & 0x000008) {
// 0x000008 indicates the presence of the default_sample_duration field
if (tfhdFlags & 0x000002) {
// 0x000002 indicates the presence of the sample_description_index field, which precedes default_sample_duration
// If present, the default_sample_duration exists at byte offset 12
sampleDuration = readUint32(tfhd, 12);
} else {
// Otherwise, the duration is at byte offset 8
sampleDuration = readUint32(tfhd, 8);
}
}
// assume a 90kHz clock if no timescale was specified
const timescale = track.timescale || 90e3;
const truns = findBox(traf, ['trun']);
for (let j = 0; j < truns.length; j++) {
rawDuration = computeRawDurationFromSamples(truns[j]);
if (!rawDuration && sampleDuration) {
const sampleCount = readUint32(truns[j], 4);
rawDuration = sampleDuration * sampleCount;
}
if (track.type === ElementaryStreamTypes.VIDEO) {
videoDuration += rawDuration / timescale;
} else if (track.type === ElementaryStreamTypes.AUDIO) {
audioDuration += rawDuration / timescale;
}
}
}
if (videoDuration === 0 && audioDuration === 0) {
// If duration samples are not available in the traf use sidx subsegment_duration
let sidxDuration = 0;
const sidxs = findBox(data, ['sidx']);
for (let i = 0; i < sidxs.length; i++) {
const sidx = parseSegmentIndex(sidxs[i]);
if (sidx != null && sidx.references) {
sidxDuration += sidx.references.reduce((dur, ref) => dur + ref.info.duration || 0, 0);
}
}
return sidxDuration;
}
if (videoDuration) {
return videoDuration;
}
return audioDuration;
}
/*
For Reference:
aligned(8) class TrackRunBox
extends FullBox(‘trun’, version, tr_flags) {
unsigned int(32) sample_count;
// the following are optional fields
signed int(32) data_offset;
unsigned int(32) first_sample_flags;
// all fields in the following array are optional
{
unsigned int(32) sample_duration;
unsigned int(32) sample_size;
unsigned int(32) sample_flags
if (version == 0)
{ unsigned int(32)
else
{ signed int(32)
}[ sample_count ]
}
*/
function computeRawDurationFromSamples(trun) {
const flags = readUint32(trun, 0);
// Flags are at offset 0, non-optional sample_count is at offset 4. Therefore we start 8 bytes in.
// Each field is an int32, which is 4 bytes
let offset = 8;
// data-offset-present flag
if (flags & 0x000001) {
offset += 4;
}
// first-sample-flags-present flag
if (flags & 0x000004) {
offset += 4;
}
let duration = 0;
const sampleCount = readUint32(trun, 4);
for (let i = 0; i < sampleCount; i++) {
// sample-duration-present flag
if (flags & 0x000100) {
const sampleDuration = readUint32(trun, offset);
duration += sampleDuration;
offset += 4;
}
// sample-size-present flag
if (flags & 0x000200) {
offset += 4;
}
// sample-flags-present flag
if (flags & 0x000400) {
offset += 4;
}
// sample-composition-time-offsets-present flag
if (flags & 0x000800) {
offset += 4;
}
}
return duration;
}
function offsetStartDTS(initData, fmp4, timeOffset) {
findBox(fmp4, ['moof', 'traf']).forEach(traf => {
findBox(traf, ['tfhd']).forEach(tfhd => {
// get the track id from the tfhd
const id = readUint32(tfhd, 4);
const track = initData[id];
if (!track) {
return;
}
// assume a 90kHz clock if no timescale was specified
const timescale = track.timescale || 90e3;
// get the base media decode time from the tfdt
findBox(traf, ['tfdt']).forEach(tfdt => {
const version = tfdt[0];
let baseMediaDecodeTime = readUint32(tfdt, 4);
if (version === 0) {
baseMediaDecodeTime -= timeOffset * timescale;
baseMediaDecodeTime = Math.max(baseMediaDecodeTime, 0);
writeUint32(tfdt, 4, baseMediaDecodeTime);
} else {
baseMediaDecodeTime *= Math.pow(2, 32);
baseMediaDecodeTime += readUint32(tfdt, 8);
baseMediaDecodeTime -= timeOffset * timescale;
baseMediaDecodeTime = Math.max(baseMediaDecodeTime, 0);
const upper = Math.floor(baseMediaDecodeTime / (UINT32_MAX$1 + 1));
const lower = Math.floor(baseMediaDecodeTime % (UINT32_MAX$1 + 1));
writeUint32(tfdt, 4, upper);
writeUint32(tfdt, 8, lower);
}
});
});
});
}
// TODO: Check if the last moof+mdat pair is part of the valid range
function segmentValidRange(data) {
const segmentedRange = {
valid: null,
remainder: null
};
const moofs = findBox(data, ['moof']);
if (!moofs) {
return segmentedRange;
} else if (moofs.length < 2) {
segmentedRange.remainder = data;
return segmentedRange;
}
const last = moofs[moofs.length - 1];
// Offset by 8 bytes; findBox offsets the start by as much
segmentedRange.valid = sliceUint8(data, 0, last.byteOffset - 8);
segmentedRange.remainder = sliceUint8(data, last.byteOffset - 8);
return segmentedRange;
}
function appendUint8Array(data1, data2) {
const temp = new Uint8Array(data1.length + data2.length);
temp.set(data1);
temp.set(data2, data1.length);
return temp;
}
function parseSamples(timeOffset, track) {
const seiSamples = [];
const videoData = track.samples;
const timescale = track.timescale;
const trackId = track.id;
let isHEVCFlavor = false;
const moofs = findBox(videoData, ['moof']);
moofs.map(moof => {
const moofOffset = moof.byteOffset - 8;
const trafs = findBox(moof, ['traf']);
trafs.map(traf => {
// get the base media decode time from the tfdt
const baseTime = findBox(traf, ['tfdt']).map(tfdt => {
const version = tfdt[0];
let result = readUint32(tfdt, 4);
if (version === 1) {
result *= Math.pow(2, 32);
result += readUint32(tfdt, 8);
}
return result / timescale;
})[0];
if (baseTime !== undefined) {
timeOffset = baseTime;
}
return findBox(traf, ['tfhd']).map(tfhd => {
const id = readUint32(tfhd, 4);
const tfhdFlags = readUint32(tfhd, 0) & 0xffffff;
const baseDataOffsetPresent = (tfhdFlags & 0x000001) !== 0;
const sampleDescriptionIndexPresent = (tfhdFlags & 0x000002) !== 0;
const defaultSampleDurationPresent = (tfhdFlags & 0x000008) !== 0;
let defaultSampleDuration = 0;
const defaultSampleSizePresent = (tfhdFlags & 0x000010) !== 0;
let defaultSampleSize = 0;
const defaultSampleFlagsPresent = (tfhdFlags & 0x000020) !== 0;
let tfhdOffset = 8;
if (id === trackId) {
if (baseDataOffsetPresent) {
tfhdOffset += 8;
}
if (sampleDescriptionIndexPresent) {
tfhdOffset += 4;
}
if (defaultSampleDurationPresent) {
defaultSampleDuration = readUint32(tfhd, tfhdOffset);
tfhdOffset += 4;
}
if (defaultSampleSizePresent) {
defaultSampleSize = readUint32(tfhd, tfhdOffset);
tfhdOffset += 4;
}
if (defaultSampleFlagsPresent) {
tfhdOffset += 4;
}
if (track.type === 'video') {
isHEVCFlavor = isHEVC(track.codec);
}
findBox(traf, ['trun']).map(trun => {
const version = trun[0];
const flags = readUint32(trun, 0) & 0xffffff;
const dataOffsetPresent = (flags & 0x000001) !== 0;
let dataOffset = 0;
const firstSampleFlagsPresent = (flags & 0x000004) !== 0;
const sampleDurationPresent = (flags & 0x000100) !== 0;
let sampleDuration = 0;
const sampleSizePresent = (flags & 0x000200) !== 0;
let sampleSize = 0;
const sampleFlagsPresent = (flags & 0x000400) !== 0;
const sampleCompositionOffsetsPresent = (flags & 0x000800) !== 0;
let compositionOffset = 0;
const sampleCount = readUint32(trun, 4);
let trunOffset = 8; // past version, flags, and sample count
if (dataOffsetPresent) {
dataOffset = readUint32(trun, trunOffset);
trunOffset += 4;
}
if (firstSampleFlagsPresent) {
trunOffset += 4;
}
let sampleOffset = dataOffset + moofOffset;
for (let ix = 0; ix < sampleCount; ix++) {
if (sampleDurationPresent) {
sampleDuration = readUint32(trun, trunOffset);
trunOffset += 4;
} else {
sampleDuration = defaultSampleDuration;
}
if (sampleSizePresent) {
sampleSize = readUint32(trun, trunOffset);
trunOffset += 4;
} else {
sampleSize = defaultSampleSize;
}
if (sampleFlagsPresent) {
trunOffset += 4;
}
if (sampleCompositionOffsetsPresent) {
if (version === 0) {
compositionOffset = readUint32(trun, trunOffset);
} else {
compositionOffset = readSint32(trun, trunOffset);
}
trunOffset += 4;
}
if (track.type === ElementaryStreamTypes.VIDEO) {
let naluTotalSize = 0;
while (naluTotalSize < sampleSize) {
const naluSize = readUint32(videoData, sampleOffset);
sampleOffset += 4;
if (isSEIMessage(isHEVCFlavor, videoData[sampleOffset])) {
const data = videoData.subarray(sampleOffset, sampleOffset + naluSize);
parseSEIMessageFromNALu(data, isHEVCFlavor ? 2 : 1, timeOffset + compositionOffset / timescale, seiSamples);
}
sampleOffset += naluSize;
naluTotalSize += naluSize + 4;
}
}
timeOffset += sampleDuration / timescale;
}
});
}
});
});
});
return seiSamples;
}
function isHEVC(codec) {
if (!codec) {
return false;
}
const delimit = codec.indexOf('.');
const baseCodec = delimit < 0 ? codec : codec.substring(0, delimit);
return baseCodec === 'hvc1' || baseCodec === 'hev1' ||
// Dolby Vision
baseCodec === 'dvh1' || baseCodec === 'dvhe';
}
function isSEIMessage(isHEVCFlavor, naluHeader) {
if (isHEVCFlavor) {
const naluType = naluHeader >> 1 & 0x3f;
return naluType === 39 || naluType === 40;
} else {
const naluType = naluHeader & 0x1f;
return naluType === 6;
}
}
function parseSEIMessageFromNALu(unescapedData, headerSize, pts, samples) {
const data = discardEPB(unescapedData);
let seiPtr = 0;
// skip nal header
seiPtr += headerSize;
let payloadType = 0;
let payloadSize = 0;
let endOfCaptions = false;
let b = 0;
while (seiPtr < data.length) {
payloadType = 0;
do {
if (seiPtr >= data.length) {
break;
}
b = data[seiPtr++];
payloadType += b;
} while (b === 0xff);
// Parse payload size.
payloadSize = 0;
do {
if (seiPtr >= data.length) {
break;
}
b = data[seiPtr++];
payloadSize += b;
} while (b === 0xff);
const leftOver = data.length - seiPtr;
if (!endOfCaptions && payloadType === 4 && seiPtr < data.length) {
endOfCaptions = true;
const countryCode = data[seiPtr++];
if (countryCode === 181) {
const providerCode = readUint16(data, seiPtr);
seiPtr += 2;
if (providerCode === 49) {
const userStructure = readUint32(data, seiPtr);
seiPtr += 4;
if (userStructure === 0x47413934) {
const userDataType = data[seiPtr++];
// Raw CEA-608 bytes wrapped in CEA-708 packet
if (userDataType === 3) {
const firstByte = data[seiPtr++];
const totalCCs = 0x1f & firstByte;
const enabled = 0x40 & firstByte;
const totalBytes = enabled ? 2 + totalCCs * 3 : 0;
const byteArray = new Uint8Array(totalBytes);
if (enabled) {
byteArray[0] = firstByte;
for (let i = 1; i < totalBytes; i++) {
byteArray[i] = data[seiPtr++];
}
}
samples.push({
type: userDataType,
payloadType,
pts,
bytes: byteArray
});
}
}
}
}
} else if (payloadType === 5 && payloadSize < leftOver) {
endOfCaptions = true;
if (payloadSize > 16) {
const uuidStrArray = [];
for (let i = 0; i < 16; i++) {
const _b = data[seiPtr++].toString(16);
uuidStrArray.push(_b.length == 1 ? '0' + _b : _b);
if (i === 3 || i === 5 || i === 7 || i === 9) {
uuidStrArray.push('-');
}
}
const length = payloadSize - 16;
const userDataBytes = new Uint8Array(length);
for (let i = 0; i < length; i++) {
userDataBytes[i] = data[seiPtr++];
}
samples.push({
payloadType,
pts,
uuid: uuidStrArray.join(''),
userData: utf8ArrayToStr(userDataBytes),
userDataBytes
});
}
} else if (payloadSize < leftOver) {
seiPtr += payloadSize;
} else if (payloadSize > leftOver) {
break;
}
}
}
/**
* remove Emulation Prevention bytes from a RBSP
*/
function discardEPB(data) {
const length = data.byteLength;
const EPBPositions = [];
let i = 1;
// Find all `Emulation Prevention Bytes`
while (i < length - 2) {
if (data[i] === 0 && data[i + 1] === 0 && data[i + 2] === 0x03) {
EPBPositions.push(i + 2);
i += 2;
} else {
i++;
}
}
// If no Emulation Prevention Bytes were found just return the original
// array
if (EPBPositions.length === 0) {
return data;
}
// Create a new array to hold the NAL unit data
const newLength = length - EPBPositions.length;
const newData = new Uint8Array(newLength);
let sourceIndex = 0;
for (i = 0; i < newLength; sourceIndex++, i++) {
if (sourceIndex === EPBPositions[0]) {
// Skip this byte
sourceIndex++;
// Remove this position index
EPBPositions.shift();
}
newData[i] = data[sourceIndex];
}
return newData;
}
function parseEmsg(data) {
const version = data[0];
let schemeIdUri = '';
let value = '';
let timeScale = 0;
let presentationTimeDelta = 0;
let presentationTime = 0;
let eventDuration = 0;
let id = 0;
let offset = 0;
if (version === 0) {
while (bin2str(data.subarray(offset, offset + 1)) !== '\0') {
schemeIdUri += bin2str(data.subarray(offset, offset + 1));
offset += 1;
}
schemeIdUri += bin2str(data.subarray(offset, offset + 1));
offset += 1;
while (bin2str(data.subarray(offset, offset + 1)) !== '\0') {
value += bin2str(data.subarray(offset, offset + 1));
offset += 1;
}
value += bin2str(data.subarray(offset, offset + 1));
offset += 1;
timeScale = readUint32(data, 12);
presentationTimeDelta = readUint32(data, 16);
eventDuration = readUint32(data, 20);
id = readUint32(data, 24);
offset = 28;
} else if (version === 1) {
offset += 4;
timeScale = readUint32(data, offset);
offset += 4;
const leftPresentationTime = readUint32(data, offset);
offset += 4;
const rightPresentationTime = readUint32(data, offset);
offset += 4;
presentationTime = 2 ** 32 * leftPresentationTime + rightPresentationTime;
if (!Number.isSafeInteger(presentationTime)) {
presentationTime = Number.MAX_SAFE_INTEGER;
logger.warn('Presentation time exceeds safe integer limit and wrapped to max safe integer in parsing emsg box');
}
eventDuration = readUint32(data, offset);
offset += 4;
id = readUint32(data, offset);
offset += 4;
while (bin2str(data.subarray(offset, offset + 1)) !== '\0') {
schemeIdUri += bin2str(data.subarray(offset, offset + 1));
offset += 1;
}
schemeIdUri += bin2str(data.subarray(offset, offset + 1));
offset += 1;
while (bin2str(data.subarray(offset, offset + 1)) !== '\0') {
value += bin2str(data.subarray(offset, offset + 1));
offset += 1;
}
value += bin2str(data.subarray(offset, offset + 1));
offset += 1;
}
const payload = data.subarray(offset, data.byteLength);
return {
schemeIdUri,
value,
timeScale,
presentationTime,
presentationTimeDelta,
eventDuration,
id,
payload
};
}
function mp4Box(type, ...payload) {
const len = payload.length;
let size = 8;
let i = len;
while (i--) {
size += payload[i].byteLength;
}
const result = new Uint8Array(size);
result[0] = size >> 24 & 0xff;
result[1] = size >> 16 & 0xff;
result[2] = size >> 8 & 0xff;
result[3] = size & 0xff;
result.set(type, 4);
for (i = 0, size = 8; i < len; i++) {
result.set(payload[i], size);
size += payload[i].byteLength;
}
return result;
}
function mp4pssh(systemId, keyids, data) {
if (systemId.byteLength !== 16) {
throw new RangeError('Invalid system id');
}
let version;
let kids;
if (keyids) {
version = 1;
kids = new Uint8Array(keyids.length * 16);
for (let ix = 0; ix < keyids.length; ix++) {
const k = keyids[ix]; // uint8array
if (k.byteLength !== 16) {
throw new RangeError('Invalid key');
}
kids.set(k, ix * 16);
}
} else {
version = 0;
kids = new Uint8Array();
}
let kidCount;
if (version > 0) {
kidCount = new Uint8Array(4);
if (keyids.length > 0) {
new DataView(kidCount.buffer).setUint32(0, keyids.length, false);
}
} else {
kidCount = new Uint8Array();
}
const dataSize = new Uint8Array(4);
if (data && data.byteLength > 0) {
new DataView(dataSize.buffer).setUint32(0, data.byteLength, false);
}
return mp4Box([112, 115, 115, 104], new Uint8Array([version, 0x00, 0x00, 0x00 // Flags
]), systemId,
// 16 bytes
kidCount, kids, dataSize, data || new Uint8Array());
}
function parsePssh(initData) {
if (!(initData instanceof ArrayBuffer) || initData.byteLength < 32) {
return null;
}
const result = {
version: 0,
systemId: '',
kids: null,
data: null
};
const view = new DataView(initData);
const boxSize = view.getUint32(0);
if (initData.byteLength !== boxSize && boxSize > 44) {
return null;
}
const type = view.getUint32(4);
if (type !== 0x70737368) {
return null;
}
result.version = view.getUint32(8) >>> 24;
if (result.version > 1) {
return null;
}
result.systemId = Hex.hexDump(new Uint8Array(initData, 12, 16));
const dataSizeOrKidCount = view.getUint32(28);
if (result.version === 0) {
if (boxSize - 32 < dataSizeOrKidCount) {
return null;
}
result.data = new Uint8Array(initData, 32, dataSizeOrKidCount);
} else if (result.version === 1) {
result.kids = [];
for (let i = 0; i < dataSizeOrKidCount; i++) {
result.kids.push(new Uint8Array(initData, 32 + i * 16, 16));
}
}
return result;
}
let keyUriToKeyIdMap = {};
class LevelKey {
static clearKeyUriToKeyIdMap() {
keyUriToKeyIdMap = {};
}
constructor(method, uri, format, formatversions = [1], iv = null) {
this.uri = void 0;
this.method = void 0;
this.keyFormat = void 0;
this.keyFormatVersions = void 0;
this.encrypted = void 0;
this.isCommonEncryption = void 0;
this.iv = null;
this.key = null;
this.keyId = null;
this.pssh = null;
this.method = method;
this.uri = uri;
this.keyFormat = format;
this.keyFormatVersions = formatversions;
this.iv = iv;
this.encrypted = method ? method !== 'NONE' : false;
this.isCommonEncryption = this.encrypted && method !== 'AES-128';
}
isSupported() {
// If it's Segment encryption or No encryption, just select that key system
if (this.method) {
if (this.method === 'AES-128' || this.method === 'NONE') {
return true;
}
if (this.keyFormat === 'identity') {
// Maintain support for clear SAMPLE-AES with MPEG-3 TS
return this.method === 'SAMPLE-AES';
} else {
switch (this.keyFormat) {
case KeySystemFormats.FAIRPLAY:
case KeySystemFormats.WIDEVINE:
case KeySystemFormats.PLAYREADY:
case KeySystemFormats.CLEARKEY:
return ['ISO-23001-7', 'SAMPLE-AES', 'SAMPLE-AES-CENC', 'SAMPLE-AES-CTR'].indexOf(this.method) !== -1;
}
}
}
return false;
}
getDecryptData(sn) {
if (!this.encrypted || !this.uri) {
return null;
}
if (this.method === 'AES-128' && this.uri && !this.iv) {
if (typeof sn !== 'number') {
// We are fetching decryption data for a initialization segment
// If the segment was encrypted with AES-128
// It must have an IV defined. We cannot substitute the Segment Number in.
if (this.method === 'AES-128' && !this.iv) {
logger.warn(`missing IV for initialization segment with method="${this.method}" - compliance issue`);
}
// Explicitly set sn to resulting value from implicit conversions 'initSegment' values for IV generation.
sn = 0;
}
const iv = createInitializationVector(sn);
const decryptdata = new LevelKey(this.method, this.uri, 'identity', this.keyFormatVersions, iv);
return decryptdata;
}
// Initialize keyId if possible
const keyBytes = convertDataUriToArrayBytes(this.uri);
if (keyBytes) {
switch (this.keyFormat) {
case KeySystemFormats.WIDEVINE:
this.pssh = keyBytes;
// In case of widevine keyID is embedded in PSSH box. Read Key ID.
if (keyBytes.length >= 22) {
this.keyId = keyBytes.subarray(keyBytes.length - 22, keyBytes.length - 6);
}
break;
case KeySystemFormats.PLAYREADY:
{
const PlayReadyKeySystemUUID = new Uint8Array([0x9a, 0x04, 0xf0, 0x79, 0x98, 0x40, 0x42, 0x86, 0xab, 0x92, 0xe6, 0x5b, 0xe0, 0x88, 0x5f, 0x95]);
this.pssh = mp4pssh(PlayReadyKeySystemUUID, null, keyBytes);
const keyBytesUtf16 = new Uint16Array(keyBytes.buffer, keyBytes.byteOffset, keyBytes.byteLength / 2);
const keyByteStr = String.fromCharCode.apply(null, Array.from(keyBytesUtf16));
// Parse Playready WRMHeader XML
const xmlKeyBytes = keyByteStr.substring(keyByteStr.indexOf('<'), keyByteStr.length);
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlKeyBytes, 'text/xml');
const keyData = xmlDoc.getElementsByTagName('KID')[0];
if (keyData) {
const keyId = keyData.childNodes[0] ? keyData.childNodes[0].nodeValue : keyData.getAttribute('VALUE');
if (keyId) {
const keyIdArray = base64Decode(keyId).subarray(0, 16);
// KID value in PRO is a base64-encoded little endian GUID interpretation of UUID
// KID value in ‘tenc’ is a big endian UUID GUID interpretation of UUID
changeEndianness(keyIdArray);
this.keyId = keyIdArray;
}
}
break;
}
default:
{
let keydata = keyBytes.subarray(0, 16);
if (keydata.length !== 16) {
const padded = new Uint8Array(16);
padded.set(keydata, 16 - keydata.length);
keydata = padded;
}
this.keyId = keydata;
break;
}
}
}
// Default behavior: assign a new keyId for each uri
if (!this.keyId || this.keyId.byteLength !== 16) {
let keyId = keyUriToKeyIdMap[this.uri];
if (!keyId) {
const val = Object.keys(keyUriToKeyIdMap).length % Number.MAX_SAFE_INTEGER;
keyId = new Uint8Array(16);
const dv = new DataView(keyId.buffer, 12, 4); // Just set the last 4 bytes
dv.setUint32(0, val);
keyUriToKeyIdMap[this.uri] = keyId;
}
this.keyId = keyId;
}
return this;
}
}
function createInitializationVector(segmentNumber) {
const uint8View = new Uint8Array(16);
for (let i = 12; i < 16; i++) {
uint8View[i] = segmentNumber >> 8 * (15 - i) & 0xff;
}
return uint8View;
}
const VARIABLE_REPLACEMENT_REGEX = /\{\$([a-zA-Z0-9-_]+)\}/g;
function hasVariableReferences(str) {
return VARIABLE_REPLACEMENT_REGEX.test(str);
}
function substituteVariablesInAttributes(parsed, attr, attributeNames) {
if (parsed.variableList !== null || parsed.hasVariableRefs) {
for (let i = attributeNames.length; i--;) {
const name = attributeNames[i];
const value = attr[name];
if (value) {
attr[name] = substituteVariables(parsed, value);
}
}
}
}
function substituteVariables(parsed, value) {
if (parsed.variableList !== null || parsed.hasVariableRefs) {
const variableList = parsed.variableList;
return value.replace(VARIABLE_REPLACEMENT_REGEX, variableReference => {
const variableName = variableReference.substring(2, variableReference.length - 1);
const variableValue = variableList == null ? void 0 : variableList[variableName];
if (variableValue === undefined) {
parsed.playlistParsingError || (parsed.playlistParsingError = new Error(`Missing preceding EXT-X-DEFINE tag for Variable Reference: "${variableName}"`));
return variableReference;
}
return variableValue;
});
}
return value;
}
function addVariableDefinition(parsed, attr, parentUrl) {
let variableList = parsed.variableList;
if (!variableList) {
parsed.variableList = variableList = {};
}
let NAME;
let VALUE;
if ('QUERYPARAM' in attr) {
NAME = attr.QUERYPARAM;
try {
const searchParams = new self.URL(parentUrl).searchParams;
if (searchParams.has(NAME)) {
VALUE = searchParams.get(NAME);
} else {
throw new Error(`"${NAME}" does not match any query parameter in URI: "${parentUrl}"`);
}
} catch (error) {
parsed.playlistParsingError || (parsed.playlistParsingError = new Error(`EXT-X-DEFINE QUERYPARAM: ${error.message}`));
}
} else {
NAME = attr.NAME;
VALUE = attr.VALUE;
}
if (NAME in variableList) {
parsed.playlistParsingError || (parsed.playlistParsingError = new Error(`EXT-X-DEFINE duplicate Variable Name declarations: "${NAME}"`));
} else {
variableList[NAME] = VALUE || '';
}
}
function importVariableDefinition(parsed, attr, sourceVariableList) {
const IMPORT = attr.IMPORT;
if (sourceVariableList && IMPORT in sourceVariableList) {
let variableList = parsed.variableList;
if (!variableList) {
parsed.variableList = variableList = {};
}
variableList[IMPORT] = sourceVariableList[IMPORT];
} else {
parsed.playlistParsingError || (parsed.playlistParsingError = new Error(`EXT-X-DEFINE IMPORT attribute not found in Multivariant Playlist: "${IMPORT}"`));
}
}
/**
* MediaSource helper
*/
function getMediaSource() {
if (typeof self === 'undefined') return undefined;
return self.MediaSource || self.WebKitMediaSource;
}
// from http://mp4ra.org/codecs.html
const sampleEntryCodesISO = {
audio: {
a3ds: true,
'ac-3': true,
'ac-4': true,
alac: true,
alaw: true,
dra1: true,
'dts+': true,
'dts-': true,
dtsc: true,
dtse: true,
dtsh: true,
'ec-3': true,
enca: true,
g719: true,
g726: true,
m4ae: true,
mha1: true,
mha2: true,
mhm1: true,
mhm2: true,
mlpa: true,
mp4a: true,
'raw ': true,
Opus: true,
opus: true,
// browsers expect this to be lowercase despite MP4RA says 'Opus'
samr: true,
sawb: true,
sawp: true,
sevc: true,
sqcp: true,
ssmv: true,
twos: true,
ulaw: true
},
video: {
avc1: true,
avc2: true,
avc3: true,
avc4: true,
avcp: true,
av01: true,
drac: true,
dva1: true,
dvav: true,
dvh1: true,
dvhe: true,
encv: true,
hev1: true,
hvc1: true,
mjp2: true,
mp4v: true,
mvc1: true,
mvc2: true,
mvc3: true,
mvc4: true,
resv: true,
rv60: true,
s263: true,
svc1: true,
svc2: true,
'vc-1': true,
vp08: true,
vp09: true
},
text: {
stpp: true,
wvtt: true
}
};
const MediaSource$2 = getMediaSource();
function isCodecType(codec, type) {
const typeCodes = sampleEntryCodesISO[type];
return !!typeCodes && typeCodes[codec.slice(0, 4)] === true;
}
function isCodecSupportedInMp4(codec, type) {
var _MediaSource$isTypeSu;
return (_MediaSource$isTypeSu = MediaSource$2 == null ? void 0 : MediaSource$2.isTypeSupported(`${type || 'video'}/mp4;codecs="${codec}"`)) != null ? _MediaSource$isTypeSu : false;
}
const MASTER_PLAYLIST_REGEX = /#EXT-X-STREAM-INF:([^\r\n]*)(?:[\r\n](?:#[^\r\n]*)?)*([^\r\n]+)|#EXT-X-(SESSION-DATA|SESSION-KEY|DEFINE|CONTENT-STEERING|START):([^\r\n]*)[\r\n]+/g;
const MASTER_PLAYLIST_MEDIA_REGEX = /#EXT-X-MEDIA:(.*)/g;
const IS_MEDIA_PLAYLIST = /^#EXT(?:INF|-X-TARGETDURATION):/m; // Handle empty Media Playlist (first EXTINF not signaled, but TARGETDURATION present)
const LEVEL_PLAYLIST_REGEX_FAST = new RegExp([/#EXTINF:\s*(\d*(?:\.\d+)?)(?:,(.*)\s+)?/.source,
// duration (#EXTINF:,), group 1 => duration, group 2 => title
/(?!#) *(\S[\S ]*)/.source,
// segment URI, group 3 => the URI (note newline is not eaten)
/#EXT-X-BYTERANGE:*(.+)/.source,
// next segment's byterange, group 4 => range spec (x@y)
/#EXT-X-PROGRAM-DATE-TIME:(.+)/.source,
// next segment's program date/time group 5 => the datetime spec
/#.*/.source // All other non-segment oriented tags will match with all groups empty
].join('|'), 'g');
const LEVEL_PLAYLIST_REGEX_SLOW = new RegExp([/#(EXTM3U)/.source, /#EXT-X-(DATERANGE|DEFINE|KEY|MAP|PART|PART-INF|PLAYLIST-TYPE|PRELOAD-HINT|RENDITION-REPORT|SERVER-CONTROL|SKIP|START):(.+)/.source, /#EXT-X-(BITRATE|DISCONTINUITY-SEQUENCE|MEDIA-SEQUENCE|TARGETDURATION|VERSION): *(\d+)/.source, /#EXT-X-(DISCONTINUITY|ENDLIST|GAP)/.source, /(#)([^:]*):(.*)/.source, /(#)(.*)(?:.*)\r?\n?/.source].join('|'));
class M3U8Parser$1 {
static findGroup(groups, mediaGroupId) {
for (let i = 0; i < groups.length; i++) {
const group = groups[i];
if (group.id === mediaGroupId) {
return group;
}
}
}
static convertAVC1ToAVCOTI(codec) {
// Convert avc1 codec string from RFC-4281 to RFC-6381 for MediaSource.isTypeSupported
const avcdata = codec.split('.');
if (avcdata.length > 2) {
let result = avcdata.shift() + '.';
result += parseInt(avcdata.shift()).toString(16);
result += ('000' + parseInt(avcdata.shift()).toString(16)).slice(-4);
return result;
}
return codec;
}
static resolve(url, baseUrl) {
return urlToolkitExports.buildAbsoluteURL(baseUrl, url, {
alwaysNormalize: true
});
}
static isMediaPlaylist(str) {
return IS_MEDIA_PLAYLIST.test(str);
}
static parseMasterPlaylist(string, baseurl) {
const hasVariableRefs = hasVariableReferences(string) ;
const parsed = {
contentSteering: null,
levels: [],
playlistParsingError: null,
sessionData: null,
sessionKeys: null,
startTimeOffset: null,
variableList: null,
hasVariableRefs
};
const levelsWithKnownCodecs = [];
MASTER_PLAYLIST_REGEX.lastIndex = 0;
let result;
while ((result = MASTER_PLAYLIST_REGEX.exec(string)) != null) {
if (result[1]) {
var _level$unknownCodecs;
// '#EXT-X-STREAM-INF' is found, parse level tag in group 1
const attrs = new AttrList(result[1]);
{
substituteVariablesInAttributes(parsed, attrs, ['CODECS', 'SUPPLEMENTAL-CODECS', 'ALLOWED-CPC', 'PATHWAY-ID', 'STABLE-VARIANT-ID', 'AUDIO', 'VIDEO', 'SUBTITLES', 'CLOSED-CAPTIONS', 'NAME']);
}
const uri = substituteVariables(parsed, result[2]) ;
const level = {
attrs,
bitrate: attrs.decimalInteger('AVERAGE-BANDWIDTH') || attrs.decimalInteger('BANDWIDTH'),
name: attrs.NAME,
url: M3U8Parser$1.resolve(uri, baseurl)
};
const resolution = attrs.decimalResolution('RESOLUTION');
if (resolution) {
level.width = resolution.width;
level.height = resolution.height;
}
setCodecs((attrs.CODECS || '').split(/[ ,]+/).filter(c => c), level);
if (level.videoCodec && level.videoCodec.indexOf('avc1') !== -1) {
level.videoCodec = M3U8Parser$1.convertAVC1ToAVCOTI(level.videoCodec);
}
if (!((_level$unknownCodecs = level.unknownCodecs) != null && _level$unknownCodecs.length)) {
levelsWithKnownCodecs.push(level);
}
parsed.levels.push(level);
} else if (result[3]) {
const tag = result[3];
const attributes = result[4];
switch (tag) {
case 'SESSION-DATA':
{
// #EXT-X-SESSION-DATA
const sessionAttrs = new AttrList(attributes);
{
substituteVariablesInAttributes(parsed, sessionAttrs, ['DATA-ID', 'LANGUAGE', 'VALUE', 'URI']);
}
const dataId = sessionAttrs['DATA-ID'];
if (dataId) {
if (parsed.sessionData === null) {
parsed.sessionData = {};
}
parsed.sessionData[dataId] = sessionAttrs;
}
break;
}
case 'SESSION-KEY':
{
// #EXT-X-SESSION-KEY
const sessionKey = parseKey(attributes, baseurl, parsed);
if (sessionKey.encrypted && sessionKey.isSupported()) {
if (parsed.sessionKeys === null) {
parsed.sessionKeys = [];
}
parsed.sessionKeys.push(sessionKey);
} else {
logger.warn(`[Keys] Ignoring invalid EXT-X-SESSION-KEY tag: "${attributes}"`);
}
break;
}
case 'DEFINE':
{
// #EXT-X-DEFINE
{
const variableAttributes = new AttrList(attributes);
substituteVariablesInAttributes(parsed, variableAttributes, ['NAME', 'VALUE', 'QUERYPARAM']);
addVariableDefinition(parsed, variableAttributes, baseurl);
}
break;
}
case 'CONTENT-STEERING':
{
// #EXT-X-CONTENT-STEERING
const contentSteeringAttributes = new AttrList(attributes);
{
substituteVariablesInAttributes(parsed, contentSteeringAttributes, ['SERVER-URI', 'PATHWAY-ID']);
}
parsed.contentSteering = {
uri: M3U8Parser$1.resolve(contentSteeringAttributes['SERVER-URI'], baseurl),
pathwayId: contentSteeringAttributes['PATHWAY-ID'] || '.'
};
break;
}
case 'START':
{
// #EXT-X-START
parsed.startTimeOffset = parseStartTimeOffset(attributes);
break;
}
}
}
}
// Filter out levels with unknown codecs if it does not remove all levels
const stripUnknownCodecLevels = levelsWithKnownCodecs.length > 0 && levelsWithKnownCodecs.length < parsed.levels.length;
parsed.levels = stripUnknownCodecLevels ? levelsWithKnownCodecs : parsed.levels;
if (parsed.levels.length === 0) {
parsed.playlistParsingError = new Error('no levels found in manifest');
}
return parsed;
}
static parseMasterPlaylistMedia(string, baseurl, parsed) {
let result;
const results = {};
const levels = parsed.levels;
const groupsByType = {
AUDIO: levels.map(level => ({
id: level.attrs.AUDIO,
audioCodec: level.audioCodec
})),
SUBTITLES: levels.map(level => ({
id: level.attrs.SUBTITLES,
textCodec: level.textCodec
})),
'CLOSED-CAPTIONS': []
};
let id = 0;
MASTER_PLAYLIST_MEDIA_REGEX.lastIndex = 0;
while ((result = MASTER_PLAYLIST_MEDIA_REGEX.exec(string)) !== null) {
const attrs = new AttrList(result[1]);
const type = attrs.TYPE;
if (type) {
const groups = groupsByType[type];
const medias = results[type] || [];
results[type] = medias;
{
substituteVariablesInAttributes(parsed, attrs, ['URI', 'GROUP-ID', 'LANGUAGE', 'ASSOC-LANGUAGE', 'STABLE-RENDITION-ID', 'NAME', 'INSTREAM-ID', 'CHARACTERISTICS', 'CHANNELS']);
}
const media = {
attrs,
bitrate: 0,
id: id++,
groupId: attrs['GROUP-ID'] || '',
instreamId: attrs['INSTREAM-ID'],
name: attrs.NAME || attrs.LANGUAGE || '',
type,
default: attrs.bool('DEFAULT'),
autoselect: attrs.bool('AUTOSELECT'),
forced: attrs.bool('FORCED'),
lang: attrs.LANGUAGE,
url: attrs.URI ? M3U8Parser$1.resolve(attrs.URI, baseurl) : ''
};
if (groups != null && groups.length) {
// If there are audio or text groups signalled in the manifest, let's look for a matching codec string for this track
// If we don't find the track signalled, lets use the first audio groups codec we have
// Acting as a best guess
const groupCodec = M3U8Parser$1.findGroup(groups, media.groupId) || groups[0];
assignCodec(media, groupCodec, 'audioCodec');
assignCodec(media, groupCodec, 'textCodec');
}
medias.push(media);
}
}
return results;
}
static parseLevelPlaylist(string, baseurl, id, type, levelUrlId, multivariantVariableList) {
const level = new LevelDetails(baseurl);
const fragments = level.fragments;
// The most recent init segment seen (applies to all subsequent segments)
let currentInitSegment = null;
let currentSN = 0;
let currentPart = 0;
let totalduration = 0;
let discontinuityCounter = 0;
let prevFrag = null;
let frag = new Fragment(type, baseurl);
let result;
let i;
let levelkeys;
let firstPdtIndex = -1;
let createNextFrag = false;
LEVEL_PLAYLIST_REGEX_FAST.lastIndex = 0;
level.m3u8 = string;
level.hasVariableRefs = hasVariableReferences(string) ;
while ((result = LEVEL_PLAYLIST_REGEX_FAST.exec(string)) !== null) {
if (createNextFrag) {
createNextFrag = false;
frag = new Fragment(type, baseurl);
// setup the next fragment for part loading
frag.start = totalduration;
frag.sn = currentSN;
frag.cc = discontinuityCounter;
frag.level = id;
if (currentInitSegment) {
frag.initSegment = currentInitSegment;
frag.rawProgramDateTime = currentInitSegment.rawProgramDateTime;
currentInitSegment.rawProgramDateTime = null;
}
}
const duration = result[1];
if (duration) {
// INF
frag.duration = parseFloat(duration);
// avoid sliced strings https://github.com/video-dev/hls.js/issues/939
const title = (' ' + result[2]).slice(1);
frag.title = title || null;
frag.tagList.push(title ? ['INF', duration, title] : ['INF', duration]);
} else if (result[3]) {
// url
if (isFiniteNumber(frag.duration)) {
frag.start = totalduration;
if (levelkeys) {
setFragLevelKeys(frag, levelkeys, level);
}
frag.sn = currentSN;
frag.level = id;
frag.cc = discontinuityCounter;
frag.urlId = levelUrlId;
fragments.push(frag);
// avoid sliced strings https://github.com/video-dev/hls.js/issues/939
const uri = (' ' + result[3]).slice(1);
frag.relurl = substituteVariables(level, uri) ;
assignProgramDateTime(frag, prevFrag);
prevFrag = frag;
totalduration += frag.duration;
currentSN++;
currentPart = 0;
createNextFrag = true;
}
} else if (result[4]) {
// X-BYTERANGE
const data = (' ' + result[4]).slice(1);
if (prevFrag) {
frag.setByteRange(data, prevFrag);
} else {
frag.setByteRange(data);
}
} else if (result[5]) {
// PROGRAM-DATE-TIME
// avoid sliced strings https://github.com/video-dev/hls.js/issues/939
frag.rawProgramDateTime = (' ' + result[5]).slice(1);
frag.tagList.push(['PROGRAM-DATE-TIME', frag.rawProgramDateTime]);
if (firstPdtIndex === -1) {
firstPdtIndex = fragments.length;
}
} else {
result = result[0].match(LEVEL_PLAYLIST_REGEX_SLOW);
if (!result) {
logger.warn('No matches on slow regex match for level playlist!');
continue;
}
for (i = 1; i < result.length; i++) {
if (typeof result[i] !== 'undefined') {
break;
}
}
// avoid sliced strings https://github.com/video-dev/hls.js/issues/939
const tag = (' ' + result[i]).slice(1);
const value1 = (' ' + result[i + 1]).slice(1);
const value2 = result[i + 2] ? (' ' + result[i + 2]).slice(1) : '';
switch (tag) {
case 'PLAYLIST-TYPE':
level.type = value1.toUpperCase();
break;
case 'MEDIA-SEQUENCE':
currentSN = level.startSN = parseInt(value1);
break;
case 'SKIP':
{
const skipAttrs = new AttrList(value1);
{
substituteVariablesInAttributes(level, skipAttrs, ['RECENTLY-REMOVED-DATERANGES']);
}
const skippedSegments = skipAttrs.decimalInteger('SKIPPED-SEGMENTS');
if (isFiniteNumber(skippedSegments)) {
level.skippedSegments = skippedSegments;
// This will result in fragments[] containing undefined values, which we will fill in with `mergeDetails`
for (let _i = skippedSegments; _i--;) {
fragments.unshift(null);
}
currentSN += skippedSegments;
}
const recentlyRemovedDateranges = skipAttrs.enumeratedString('RECENTLY-REMOVED-DATERANGES');
if (recentlyRemovedDateranges) {
level.recentlyRemovedDateranges = recentlyRemovedDateranges.split('\t');
}
break;
}
case 'TARGETDURATION':
level.targetduration = Math.max(parseInt(value1), 1);
break;
case 'VERSION':
level.version = parseInt(value1);
break;
case 'EXTM3U':
break;
case 'ENDLIST':
level.live = false;
break;
case '#':
if (value1 || value2) {
frag.tagList.push(value2 ? [value1, value2] : [value1]);
}
break;
case 'DISCONTINUITY':
discontinuityCounter++;
frag.tagList.push(['DIS']);
break;
case 'GAP':
frag.gap = true;
frag.tagList.push([tag]);
break;
case 'BITRATE':
frag.tagList.push([tag, value1]);
break;
case 'DATERANGE':
{
const dateRangeAttr = new AttrList(value1);
{
substituteVariablesInAttributes(level, dateRangeAttr, ['ID', 'CLASS', 'START-DATE', 'END-DATE', 'SCTE35-CMD', 'SCTE35-OUT', 'SCTE35-IN']);
substituteVariablesInAttributes(level, dateRangeAttr, dateRangeAttr.clientAttrs);
}
const dateRange = new DateRange(dateRangeAttr, level.dateRanges[dateRangeAttr.ID]);
if (dateRange.isValid || level.skippedSegments) {
level.dateRanges[dateRange.id] = dateRange;
} else {
logger.warn(`Ignoring invalid DATERANGE tag: "${value1}"`);
}
// Add to fragment tag list for backwards compatibility (< v1.2.0)
frag.tagList.push(['EXT-X-DATERANGE', value1]);
break;
}
case 'DEFINE':
{
{
const variableAttributes = new AttrList(value1);
substituteVariablesInAttributes(level, variableAttributes, ['NAME', 'VALUE', 'IMPORT', 'QUERYPARAM']);
if ('IMPORT' in variableAttributes) {
importVariableDefinition(level, variableAttributes, multivariantVariableList);
} else {
addVariableDefinition(level, variableAttributes, baseurl);
}
}
break;
}
case 'DISCONTINUITY-SEQUENCE':
discontinuityCounter = parseInt(value1);
break;
case 'KEY':
{
const levelKey = parseKey(value1, baseurl, level);
if (levelKey.isSupported()) {
if (levelKey.method === 'NONE') {
levelkeys = undefined;
break;
}
if (!levelkeys) {
levelkeys = {};
}
if (levelkeys[levelKey.keyFormat]) {
levelkeys = _extends({}, levelkeys);
}
levelkeys[levelKey.keyFormat] = levelKey;
} else {
logger.warn(`[Keys] Ignoring invalid EXT-X-KEY tag: "${value1}"`);
}
break;
}
case 'START':
level.startTimeOffset = parseStartTimeOffset(value1);
break;
case 'MAP':
{
const mapAttrs = new AttrList(value1);
{
substituteVariablesInAttributes(level, mapAttrs, ['BYTERANGE', 'URI']);
}
if (frag.duration) {
// Initial segment tag is after segment duration tag.
// #EXTINF: 6.0
// #EXT-X-MAP:URI="init.mp4
const init = new Fragment(type, baseurl);
setInitSegment(init, mapAttrs, id, levelkeys);
currentInitSegment = init;
frag.initSegment = currentInitSegment;
if (currentInitSegment.rawProgramDateTime && !frag.rawProgramDateTime) {
frag.rawProgramDateTime = currentInitSegment.rawProgramDateTime;
}
} else {
// Initial segment tag is before segment duration tag
setInitSegment(frag, mapAttrs, id, levelkeys);
currentInitSegment = frag;
createNextFrag = true;
}
break;
}
case 'SERVER-CONTROL':
{
const serverControlAttrs = new AttrList(value1);
level.canBlockReload = serverControlAttrs.bool('CAN-BLOCK-RELOAD');
level.canSkipUntil = serverControlAttrs.optionalFloat('CAN-SKIP-UNTIL', 0);
level.canSkipDateRanges = level.canSkipUntil > 0 && serverControlAttrs.bool('CAN-SKIP-DATERANGES');
level.partHoldBack = serverControlAttrs.optionalFloat('PART-HOLD-BACK', 0);
level.holdBack = serverControlAttrs.optionalFloat('HOLD-BACK', 0);
break;
}
case 'PART-INF':
{
const partInfAttrs = new AttrList(value1);
level.partTarget = partInfAttrs.decimalFloatingPoint('PART-TARGET');
break;
}
case 'PART':
{
let partList = level.partList;
if (!partList) {
partList = level.partList = [];
}
const previousFragmentPart = currentPart > 0 ? partList[partList.length - 1] : undefined;
const index = currentPart++;
const partAttrs = new AttrList(value1);
{
substituteVariablesInAttributes(level, partAttrs, ['BYTERANGE', 'URI']);
}
const part = new Part(partAttrs, frag, baseurl, index, previousFragmentPart);
partList.push(part);
frag.duration += part.duration;
break;
}
case 'PRELOAD-HINT':
{
const preloadHintAttrs = new AttrList(value1);
{
substituteVariablesInAttributes(level, preloadHintAttrs, ['URI']);
}
level.preloadHint = preloadHintAttrs;
break;
}
case 'RENDITION-REPORT':
{
const renditionReportAttrs = new AttrList(value1);
{
substituteVariablesInAttributes(level, renditionReportAttrs, ['URI']);
}
level.renditionReports = level.renditionReports || [];
level.renditionReports.push(renditionReportAttrs);
break;
}
default:
logger.warn(`line parsed but not handled: ${result}`);
break;
}
}
}
if (prevFrag && !prevFrag.relurl) {
fragments.pop();
totalduration -= prevFrag.duration;
if (level.partList) {
level.fragmentHint = prevFrag;
}
} else if (level.partList) {
assignProgramDateTime(frag, prevFrag);
frag.cc = discontinuityCounter;
level.fragmentHint = frag;
if (levelkeys) {
setFragLevelKeys(frag, levelkeys, level);
}
}
const fragmentLength = fragments.length;
const firstFragment = fragments[0];
const lastFragment = fragments[fragmentLength - 1];
totalduration += level.skippedSegments * level.targetduration;
if (totalduration > 0 && fragmentLength && lastFragment) {
level.averagetargetduration = totalduration / fragmentLength;
const lastSn = lastFragment.sn;
level.endSN = lastSn !== 'initSegment' ? lastSn : 0;
if (!level.live) {
lastFragment.endList = true;
}
if (firstFragment) {
level.startCC = firstFragment.cc;
}
} else {
level.endSN = 0;
level.startCC = 0;
}
if (level.fragmentHint) {
totalduration += level.fragmentHint.duration;
}
level.totalduration = totalduration;
level.endCC = discontinuityCounter;
/**
* Backfill any missing PDT values
* "If the first EXT-X-PROGRAM-DATE-TIME tag in a Playlist appears after
* one or more Media Segment URIs, the client SHOULD extrapolate
* backward from that tag (using EXTINF durations and/or media
* timestamps) to associate dates with those segments."
* We have already extrapolated forward, but all fragments up to the first instance of PDT do not have their PDTs
* computed.
*/
if (firstPdtIndex > 0) {
backfillProgramDateTimes(fragments, firstPdtIndex);
}
return level;
}
}
function parseKey(keyTagAttributes, baseurl, parsed) {
var _keyAttrs$METHOD, _keyAttrs$KEYFORMAT;
// https://tools.ietf.org/html/rfc8216#section-4.3.2.4
const keyAttrs = new AttrList(keyTagAttributes);
{
substituteVariablesInAttributes(parsed, keyAttrs, ['KEYFORMAT', 'KEYFORMATVERSIONS', 'URI', 'IV', 'URI']);
}
const decryptmethod = (_keyAttrs$METHOD = keyAttrs.METHOD) != null ? _keyAttrs$METHOD : '';
const decrypturi = keyAttrs.URI;
const decryptiv = keyAttrs.hexadecimalInteger('IV');
const decryptkeyformatversions = keyAttrs.KEYFORMATVERSIONS;
// From RFC: This attribute is OPTIONAL; its absence indicates an implicit value of "identity".
const decryptkeyformat = (_keyAttrs$KEYFORMAT = keyAttrs.KEYFORMAT) != null ? _keyAttrs$KEYFORMAT : 'identity';
if (decrypturi && keyAttrs.IV && !decryptiv) {
logger.error(`Invalid IV: ${keyAttrs.IV}`);
}
// If decrypturi is a URI with a scheme, then baseurl will be ignored
// No uri is allowed when METHOD is NONE
const resolvedUri = decrypturi ? M3U8Parser$1.resolve(decrypturi, baseurl) : '';
const keyFormatVersions = (decryptkeyformatversions ? decryptkeyformatversions : '1').split('/').map(Number).filter(Number.isFinite);
return new LevelKey(decryptmethod, resolvedUri, decryptkeyformat, keyFormatVersions, decryptiv);
}
function parseStartTimeOffset(startAttributes) {
const startAttrs = new AttrList(startAttributes);
const startTimeOffset = startAttrs.decimalFloatingPoint('TIME-OFFSET');
if (isFiniteNumber(startTimeOffset)) {
return startTimeOffset;
}
return null;
}
function setCodecs(codecs, level) {
['video', 'audio', 'text'].forEach(type => {
const filtered = codecs.filter(codec => isCodecType(codec, type));
if (filtered.length) {
const preferred = filtered.filter(codec => {
return codec.lastIndexOf('avc1', 0) === 0 || codec.lastIndexOf('mp4a', 0) === 0;
});
level[`${type}Codec`] = preferred.length > 0 ? preferred[0] : filtered[0];
// remove from list
codecs = codecs.filter(codec => filtered.indexOf(codec) === -1);
}
});
level.unknownCodecs = codecs;
}
function assignCodec(media, groupItem, codecProperty) {
const codecValue = groupItem[codecProperty];
if (codecValue) {
media[codecProperty] = codecValue;
}
}
function backfillProgramDateTimes(fragments, firstPdtIndex) {
let fragPrev = fragments[firstPdtIndex];
for (let i = firstPdtIndex; i--;) {
const frag = fragments[i];
// Exit on delta-playlist skipped segments
if (!frag) {
return;
}
frag.programDateTime = fragPrev.programDateTime - frag.duration * 1000;
fragPrev = frag;
}
}
function assignProgramDateTime(frag, prevFrag) {
if (frag.rawProgramDateTime) {
frag.programDateTime = Date.parse(frag.rawProgramDateTime);
} else if (prevFrag != null && prevFrag.programDateTime) {
frag.programDateTime = prevFrag.endProgramDateTime;
}
if (!isFiniteNumber(frag.programDateTime)) {
frag.programDateTime = null;
frag.rawProgramDateTime = null;
}
}
function setInitSegment(frag, mapAttrs, id, levelkeys) {
frag.relurl = mapAttrs.URI;
if (mapAttrs.BYTERANGE) {
frag.setByteRange(mapAttrs.BYTERANGE);
}
frag.level = id;
frag.sn = 'initSegment';
if (levelkeys) {
frag.levelkeys = levelkeys;
}
frag.initSegment = null;
}
function setFragLevelKeys(frag, levelkeys, level) {
frag.levelkeys = levelkeys;
const {
encryptedFragments
} = level;
if ((!encryptedFragments.length || encryptedFragments[encryptedFragments.length - 1].levelkeys !== levelkeys) && Object.keys(levelkeys).some(format => levelkeys[format].isCommonEncryption)) {
encryptedFragments.push(frag);
}
}
var PlaylistContextType = {
MANIFEST: "manifest",
LEVEL: "level",
AUDIO_TRACK: "audioTrack",
SUBTITLE_TRACK: "subtitleTrack"
};
var PlaylistLevelType = {
MAIN: "main",
AUDIO: "audio",
SUBTITLE: "subtitle"
};
function mapContextToLevelType(context) {
const {
type
} = context;
switch (type) {
case PlaylistContextType.AUDIO_TRACK:
return PlaylistLevelType.AUDIO;
case PlaylistContextType.SUBTITLE_TRACK:
return PlaylistLevelType.SUBTITLE;
default:
return PlaylistLevelType.MAIN;
}
}
function getResponseUrl(response, context) {
let url = response.url;
// responseURL not supported on some browsers (it is used to detect URL redirection)
// data-uri mode also not supported (but no need to detect redirection)
if (url === undefined || url.indexOf('data:') === 0) {
// fallback to initial URL
url = context.url;
}
return url;
}
class PlaylistLoader {
constructor(hls) {
this.hls = void 0;
this.loaders = Object.create(null);
this.variableList = null;
this.hls = hls;
this.registerListeners();
}
startLoad(startPosition) {}
stopLoad() {
this.destroyInternalLoaders();
}
registerListeners() {
const {
hls
} = this;
hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.on(Events.LEVEL_LOADING, this.onLevelLoading, this);
hls.on(Events.AUDIO_TRACK_LOADING, this.onAudioTrackLoading, this);
hls.on(Events.SUBTITLE_TRACK_LOADING, this.onSubtitleTrackLoading, this);
}
unregisterListeners() {
const {
hls
} = this;
hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.off(Events.LEVEL_LOADING, this.onLevelLoading, this);
hls.off(Events.AUDIO_TRACK_LOADING, this.onAudioTrackLoading, this);
hls.off(Events.SUBTITLE_TRACK_LOADING, this.onSubtitleTrackLoading, this);
}
/**
* Returns defaults or configured loader-type overloads (pLoader and loader config params)
*/
createInternalLoader(context) {
const config = this.hls.config;
const PLoader = config.pLoader;
const Loader = config.loader;
const InternalLoader = PLoader || Loader;
const loader = new InternalLoader(config);
this.loaders[context.type] = loader;
return loader;
}
getInternalLoader(context) {
return this.loaders[context.type];
}
resetInternalLoader(contextType) {
if (this.loaders[contextType]) {
delete this.loaders[contextType];
}
}
/**
* Call `destroy` on all internal loader instances mapped (one per context type)
*/
destroyInternalLoaders() {
for (const contextType in this.loaders) {
const loader = this.loaders[contextType];
if (loader) {
loader.destroy();
}
this.resetInternalLoader(contextType);
}
}
destroy() {
this.variableList = null;
this.unregisterListeners();
this.destroyInternalLoaders();
}
onManifestLoading(event, data) {
const {
url
} = data;
this.variableList = null;
this.load({
id: null,
level: 0,
responseType: 'text',
type: PlaylistContextType.MANIFEST,
url,
deliveryDirectives: null
});
}
onLevelLoading(event, data) {
const {
id,
level,
url,
deliveryDirectives
} = data;
this.load({
id,
level,
responseType: 'text',
type: PlaylistContextType.LEVEL,
url,
deliveryDirectives
});
}
onAudioTrackLoading(event, data) {
const {
id,
groupId,
url,
deliveryDirectives
} = data;
this.load({
id,
groupId,
level: null,
responseType: 'text',
type: PlaylistContextType.AUDIO_TRACK,
url,
deliveryDirectives
});
}
onSubtitleTrackLoading(event, data) {
const {
id,
groupId,
url,
deliveryDirectives
} = data;
this.load({
id,
groupId,
level: null,
responseType: 'text',
type: PlaylistContextType.SUBTITLE_TRACK,
url,
deliveryDirectives
});
}
load(context) {
var _context$deliveryDire;
const config = this.hls.config;
// logger.debug(`[playlist-loader]: Loading playlist of type ${context.type}, level: ${context.level}, id: ${context.id}`);
// Check if a loader for this context already exists
let loader = this.getInternalLoader(context);
if (loader) {
const loaderContext = loader.context;
if (loaderContext && loaderContext.url === context.url) {
// same URL can't overlap
logger.trace('[playlist-loader]: playlist request ongoing');
return;
}
logger.log(`[playlist-loader]: aborting previous loader for type: ${context.type}`);
loader.abort();
}
// apply different configs for retries depending on
// context (manifest, level, audio/subs playlist)
let loadPolicy;
if (context.type === PlaylistContextType.MANIFEST) {
loadPolicy = config.manifestLoadPolicy.default;
} else {
loadPolicy = _extends({}, config.playlistLoadPolicy.default, {
timeoutRetry: null,
errorRetry: null
});
}
loader = this.createInternalLoader(context);
// Override level/track timeout for LL-HLS requests
// (the default of 10000ms is counter productive to blocking playlist reload requests)
if ((_context$deliveryDire = context.deliveryDirectives) != null && _context$deliveryDire.part) {
let levelDetails;
if (context.type === PlaylistContextType.LEVEL && context.level !== null) {
levelDetails = this.hls.levels[context.level].details;
} else if (context.type === PlaylistContextType.AUDIO_TRACK && context.id !== null) {
levelDetails = this.hls.audioTracks[context.id].details;
} else if (context.type === PlaylistContextType.SUBTITLE_TRACK && context.id !== null) {
levelDetails = this.hls.subtitleTracks[context.id].details;
}
if (levelDetails) {
const partTarget = levelDetails.partTarget;
const targetDuration = levelDetails.targetduration;
if (partTarget && targetDuration) {
const maxLowLatencyPlaylistRefresh = Math.max(partTarget * 3, targetDuration * 0.8) * 1000;
loadPolicy = _extends({}, loadPolicy, {
maxTimeToFirstByteMs: Math.min(maxLowLatencyPlaylistRefresh, loadPolicy.maxTimeToFirstByteMs),
maxLoadTimeMs: Math.min(maxLowLatencyPlaylistRefresh, loadPolicy.maxTimeToFirstByteMs)
});
}
}
}
const legacyRetryCompatibility = loadPolicy.errorRetry || loadPolicy.timeoutRetry || {};
const loaderConfig = {
loadPolicy,
timeout: loadPolicy.maxLoadTimeMs,
maxRetry: legacyRetryCompatibility.maxNumRetry || 0,
retryDelay: legacyRetryCompatibility.retryDelayMs || 0,
maxRetryDelay: legacyRetryCompatibility.maxRetryDelayMs || 0
};
const loaderCallbacks = {
onSuccess: (response, stats, context, networkDetails) => {
const loader = this.getInternalLoader(context);
this.resetInternalLoader(context.type);
const string = response.data;
// Validate if it is an M3U8 at all
if (string.indexOf('#EXTM3U') !== 0) {
this.handleManifestParsingError(response, context, new Error('no EXTM3U delimiter'), networkDetails || null, stats);
return;
}
stats.parsing.start = performance.now();
if (M3U8Parser$1.isMediaPlaylist(string)) {
this.handleTrackOrLevelPlaylist(response, stats, context, networkDetails || null, loader);
} else {
this.handleMasterPlaylist(response, stats, context, networkDetails);
}
},
onError: (response, context, networkDetails, stats) => {
this.handleNetworkError(context, networkDetails, false, response, stats);
},
onTimeout: (stats, context, networkDetails) => {
this.handleNetworkError(context, networkDetails, true, undefined, stats);
}
};
// logger.debug(`[playlist-loader]: Calling internal loader delegate for URL: ${context.url}`);
loader.load(context, loaderConfig, loaderCallbacks);
}
handleMasterPlaylist(response, stats, context, networkDetails) {
const hls = this.hls;
const string = response.data;
const url = getResponseUrl(response, context);
const parsedResult = M3U8Parser$1.parseMasterPlaylist(string, url);
if (parsedResult.playlistParsingError) {
this.handleManifestParsingError(response, context, parsedResult.playlistParsingError, networkDetails, stats);
return;
}
const {
contentSteering,
levels,
sessionData,
sessionKeys,
startTimeOffset,
variableList
} = parsedResult;
this.variableList = variableList;
const {
AUDIO: audioTracks = [],
SUBTITLES: subtitles,
'CLOSED-CAPTIONS': captions
} = M3U8Parser$1.parseMasterPlaylistMedia(string, url, parsedResult);
if (audioTracks.length) {
// check if we have found an audio track embedded in main playlist (audio track without URI attribute)
const embeddedAudioFound = audioTracks.some(audioTrack => !audioTrack.url);
// if no embedded audio track defined, but audio codec signaled in quality level,
// we need to signal this main audio track this could happen with playlists with
// alt audio rendition in which quality levels (main)
// contains both audio+video. but with mixed audio track not signaled
if (!embeddedAudioFound && levels[0].audioCodec && !levels[0].attrs.AUDIO) {
logger.log('[playlist-loader]: audio codec signaled in quality level, but no embedded audio track signaled, create one');
audioTracks.unshift({
type: 'main',
name: 'main',
groupId: 'main',
default: false,
autoselect: false,
forced: false,
id: -1,
attrs: new AttrList({}),
bitrate: 0,
url: ''
});
}
}
hls.trigger(Events.MANIFEST_LOADED, {
levels,
audioTracks,
subtitles,
captions,
contentSteering,
url,
stats,
networkDetails,
sessionData,
sessionKeys,
startTimeOffset,
variableList
});
}
handleTrackOrLevelPlaylist(response, stats, context, networkDetails, loader) {
const hls = this.hls;
const {
id,
level,
type
} = context;
const url = getResponseUrl(response, context);
const levelUrlId = isFiniteNumber(id) ? id : 0;
const levelId = isFiniteNumber(level) ? level : levelUrlId;
const levelType = mapContextToLevelType(context);
const levelDetails = M3U8Parser$1.parseLevelPlaylist(response.data, url, levelId, levelType, levelUrlId, this.variableList);
// We have done our first request (Manifest-type) and receive
// not a master playlist but a chunk-list (track/level)
// We fire the manifest-loaded event anyway with the parsed level-details
// by creating a single-level structure for it.
if (type === PlaylistContextType.MANIFEST) {
const singleLevel = {
attrs: new AttrList({}),
bitrate: 0,
details: levelDetails,
name: '',
url
};
hls.trigger(Events.MANIFEST_LOADED, {
levels: [singleLevel],
audioTracks: [],
url,
stats,
networkDetails,
sessionData: null,
sessionKeys: null,
contentSteering: null,
startTimeOffset: null,
variableList: null
});
}
// save parsing time
stats.parsing.end = performance.now();
// extend the context with the new levelDetails property
context.levelDetails = levelDetails;
this.handlePlaylistLoaded(levelDetails, response, stats, context, networkDetails, loader);
}
handleManifestParsingError(response, context, error, networkDetails, stats) {
this.hls.trigger(Events.ERROR, {
type: ErrorTypes.NETWORK_ERROR,
details: ErrorDetails.MANIFEST_PARSING_ERROR,
fatal: context.type === PlaylistContextType.MANIFEST,
url: response.url,
err: error,
error,
reason: error.message,
response,
context,
networkDetails,
stats
});
}
handleNetworkError(context, networkDetails, timeout = false, response, stats) {
let message = `A network ${timeout ? 'timeout' : 'error' + (response ? ' (status ' + response.code + ')' : '')} occurred while loading ${context.type}`;
if (context.type === PlaylistContextType.LEVEL) {
message += `: ${context.level} id: ${context.id}`;
} else if (context.type === PlaylistContextType.AUDIO_TRACK || context.type === PlaylistContextType.SUBTITLE_TRACK) {
message += ` id: ${context.id} group-id: "${context.groupId}"`;
}
const error = new Error(message);
logger.warn(`[playlist-loader]: ${message}`);
let details = ErrorDetails.UNKNOWN;
let fatal = false;
const loader = this.getInternalLoader(context);
switch (context.type) {
case PlaylistContextType.MANIFEST:
details = timeout ? ErrorDetails.MANIFEST_LOAD_TIMEOUT : ErrorDetails.MANIFEST_LOAD_ERROR;
fatal = true;
break;
case PlaylistContextType.LEVEL:
details = timeout ? ErrorDetails.LEVEL_LOAD_TIMEOUT : ErrorDetails.LEVEL_LOAD_ERROR;
fatal = false;
break;
case PlaylistContextType.AUDIO_TRACK:
details = timeout ? ErrorDetails.AUDIO_TRACK_LOAD_TIMEOUT : ErrorDetails.AUDIO_TRACK_LOAD_ERROR;
fatal = false;
break;
case PlaylistContextType.SUBTITLE_TRACK:
details = timeout ? ErrorDetails.SUBTITLE_TRACK_LOAD_TIMEOUT : ErrorDetails.SUBTITLE_LOAD_ERROR;
fatal = false;
break;
}
if (loader) {
this.resetInternalLoader(context.type);
}
const errorData = {
type: ErrorTypes.NETWORK_ERROR,
details,
fatal,
url: context.url,
loader,
context,
error,
networkDetails,
stats
};
if (response) {
const url = (networkDetails == null ? void 0 : networkDetails.url) || context.url;
errorData.response = _objectSpread2({
url,
data: undefined
}, response);
}
this.hls.trigger(Events.ERROR, errorData);
}
handlePlaylistLoaded(levelDetails, response, stats, context, networkDetails, loader) {
const hls = this.hls;
const {
type,
level,
id,
groupId,
deliveryDirectives
} = context;
const url = getResponseUrl(response, context);
const parent = mapContextToLevelType(context);
const levelIndex = typeof context.level === 'number' && parent === PlaylistLevelType.MAIN ? level : undefined;
if (!levelDetails.fragments.length) {
const _error = new Error('No Segments found in Playlist');
hls.trigger(Events.ERROR, {
type: ErrorTypes.NETWORK_ERROR,
details: ErrorDetails.LEVEL_EMPTY_ERROR,
fatal: false,
url,
error: _error,
reason: _error.message,
response,
context,
level: levelIndex,
parent,
networkDetails,
stats
});
return;
}
if (!levelDetails.targetduration) {
levelDetails.playlistParsingError = new Error('Missing Target Duration');
}
const error = levelDetails.playlistParsingError;
if (error) {
hls.trigger(Events.ERROR, {
type: ErrorTypes.NETWORK_ERROR,
details: ErrorDetails.LEVEL_PARSING_ERROR,
fatal: false,
url,
error,
reason: error.message,
response,
context,
level: levelIndex,
parent,
networkDetails,
stats
});
return;
}
if (levelDetails.live && loader) {
if (loader.getCacheAge) {
levelDetails.ageHeader = loader.getCacheAge() || 0;
}
if (!loader.getCacheAge || isNaN(levelDetails.ageHeader)) {
levelDetails.ageHeader = 0;
}
}
switch (type) {
case PlaylistContextType.MANIFEST:
case PlaylistContextType.LEVEL:
hls.trigger(Events.LEVEL_LOADED, {
details: levelDetails,
level: levelIndex || 0,
id: id || 0,
stats,
networkDetails,
deliveryDirectives
});
break;
case PlaylistContextType.AUDIO_TRACK:
hls.trigger(Events.AUDIO_TRACK_LOADED, {
details: levelDetails,
id: id || 0,
groupId: groupId || '',
stats,
networkDetails,
deliveryDirectives
});
break;
case PlaylistContextType.SUBTITLE_TRACK:
hls.trigger(Events.SUBTITLE_TRACK_LOADED, {
details: levelDetails,
id: id || 0,
groupId: groupId || '',
stats,
networkDetails,
deliveryDirectives
});
break;
}
}
}
function sendAddTrackEvent(track, videoEl) {
let event;
try {
event = new Event('addtrack');
} catch (err) {
// for IE11
event = document.createEvent('Event');
event.initEvent('addtrack', false, false);
}
event.track = track;
videoEl.dispatchEvent(event);
}
function addCueToTrack(track, cue) {
// Sometimes there are cue overlaps on segmented vtts so the same
// cue can appear more than once in different vtt files.
// This avoid showing duplicated cues with same timecode and text.
const mode = track.mode;
if (mode === 'disabled') {
track.mode = 'hidden';
}
if (track.cues && !track.cues.getCueById(cue.id)) {
try {
track.addCue(cue);
if (!track.cues.getCueById(cue.id)) {
throw new Error(`addCue is failed for: ${cue}`);
}
} catch (err) {
logger.debug(`[texttrack-utils]: ${err}`);
try {
const textTrackCue = new self.TextTrackCue(cue.startTime, cue.endTime, cue.text);
textTrackCue.id = cue.id;
track.addCue(textTrackCue);
} catch (err2) {
logger.debug(`[texttrack-utils]: Legacy TextTrackCue fallback failed: ${err2}`);
}
}
}
if (mode === 'disabled') {
track.mode = mode;
}
}
function clearCurrentCues(track) {
// When track.mode is disabled, track.cues will be null.
// To guarantee the removal of cues, we need to temporarily
// change the mode to hidden
const mode = track.mode;
if (mode === 'disabled') {
track.mode = 'hidden';
}
if (track.cues) {
for (let i = track.cues.length; i--;) {
track.removeCue(track.cues[i]);
}
}
if (mode === 'disabled') {
track.mode = mode;
}
}
function removeCuesInRange(track, start, end, predicate) {
const mode = track.mode;
if (mode === 'disabled') {
track.mode = 'hidden';
}
if (track.cues && track.cues.length > 0) {
const cues = getCuesInRange(track.cues, start, end);
for (let i = 0; i < cues.length; i++) {
if (!predicate || predicate(cues[i])) {
track.removeCue(cues[i]);
}
}
}
if (mode === 'disabled') {
track.mode = mode;
}
}
// Find first cue starting after given time.
// Modified version of binary search O(log(n)).
function getFirstCueIndexAfterTime(cues, time) {
// If first cue starts after time, start there
if (time < cues[0].startTime) {
return 0;
}
// If the last cue ends before time there is no overlap
const len = cues.length - 1;
if (time > cues[len].endTime) {
return -1;
}
let left = 0;
let right = len;
while (left <= right) {
const mid = Math.floor((right + left) / 2);
if (time < cues[mid].startTime) {
right = mid - 1;
} else if (time > cues[mid].startTime && left < len) {
left = mid + 1;
} else {
// If it's not lower or higher, it must be equal.
return mid;
}
}
// At this point, left and right have swapped.
// No direct match was found, left or right element must be the closest. Check which one has the smallest diff.
return cues[left].startTime - time < time - cues[right].startTime ? left : right;
}
function getCuesInRange(cues, start, end) {
const cuesFound = [];
const firstCueInRange = getFirstCueIndexAfterTime(cues, start);
if (firstCueInRange > -1) {
for (let i = firstCueInRange, len = cues.length; i < len; i++) {
const cue = cues[i];
if (cue.startTime >= start && cue.endTime <= end) {
cuesFound.push(cue);
} else if (cue.startTime > end) {
return cuesFound;
}
}
}
return cuesFound;
}
var MetadataSchema = {
audioId3: "org.id3",
dateRange: "com.apple.quicktime.HLS",
emsg: "https://aomedia.org/emsg/ID3"
};
const MIN_CUE_DURATION = 0.25;
function getCueClass() {
if (typeof self === 'undefined') return undefined;
// Attempt to recreate Safari functionality by creating
// WebKitDataCue objects when available and store the decoded
// ID3 data in the value property of the cue
return self.WebKitDataCue || self.VTTCue || self.TextTrackCue;
}
// VTTCue latest draft allows an infinite duration, fallback
// to MAX_VALUE if necessary
const MAX_CUE_ENDTIME = (() => {
const Cue = getCueClass();
try {
new Cue(0, Number.POSITIVE_INFINITY, '');
} catch (e) {
return Number.MAX_VALUE;
}
return Number.POSITIVE_INFINITY;
})();
function dateRangeDateToTimelineSeconds(date, offset) {
return date.getTime() / 1000 - offset;
}
function hexToArrayBuffer(str) {
return Uint8Array.from(str.replace(/^0x/, '').replace(/([\da-fA-F]{2}) ?/g, '0x$1 ').replace(/ +$/, '').split(' ')).buffer;
}
class ID3TrackController {
constructor(hls) {
this.hls = void 0;
this.id3Track = null;
this.media = null;
this.dateRangeCuesAppended = {};
this.hls = hls;
this._registerListeners();
}
destroy() {
this._unregisterListeners();
this.id3Track = null;
this.media = null;
this.dateRangeCuesAppended = {};
// @ts-ignore
this.hls = null;
}
_registerListeners() {
const {
hls
} = this;
hls.on(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
hls.on(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.on(Events.FRAG_PARSING_METADATA, this.onFragParsingMetadata, this);
hls.on(Events.BUFFER_FLUSHING, this.onBufferFlushing, this);
hls.on(Events.LEVEL_UPDATED, this.onLevelUpdated, this);
}
_unregisterListeners() {
const {
hls
} = this;
hls.off(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
hls.off(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.off(Events.FRAG_PARSING_METADATA, this.onFragParsingMetadata, this);
hls.off(Events.BUFFER_FLUSHING, this.onBufferFlushing, this);
hls.off(Events.LEVEL_UPDATED, this.onLevelUpdated, this);
}
// Add ID3 metatadata text track.
onMediaAttached(event, data) {
this.media = data.media;
}
onMediaDetaching() {
if (!this.id3Track) {
return;
}
clearCurrentCues(this.id3Track);
this.id3Track = null;
this.media = null;
this.dateRangeCuesAppended = {};
}
onManifestLoading() {
this.dateRangeCuesAppended = {};
}
createTrack(media) {
const track = this.getID3Track(media.textTracks);
track.mode = 'hidden';
return track;
}
getID3Track(textTracks) {
if (!this.media) {
return;
}
for (let i = 0; i < textTracks.length; i++) {
const textTrack = textTracks[i];
if (textTrack.kind === 'metadata' && textTrack.label === 'id3') {
// send 'addtrack' when reusing the textTrack for metadata,
// same as what we do for captions
sendAddTrackEvent(textTrack, this.media);
return textTrack;
}
}
return this.media.addTextTrack('metadata', 'id3');
}
onFragParsingMetadata(event, data) {
if (!this.media) {
return;
}
const {
hls: {
config: {
enableEmsgMetadataCues,
enableID3MetadataCues
}
}
} = this;
if (!enableEmsgMetadataCues && !enableID3MetadataCues) {
return;
}
const {
samples
} = data;
// create track dynamically
if (!this.id3Track) {
this.id3Track = this.createTrack(this.media);
}
const Cue = getCueClass();
for (let i = 0; i < samples.length; i++) {
const type = samples[i].type;
if (type === MetadataSchema.emsg && !enableEmsgMetadataCues || !enableID3MetadataCues) {
continue;
}
const frames = getID3Frames(samples[i].data);
if (frames) {
const startTime = samples[i].pts;
let endTime = startTime + samples[i].duration;
if (endTime > MAX_CUE_ENDTIME) {
endTime = MAX_CUE_ENDTIME;
}
const timeDiff = endTime - startTime;
if (timeDiff <= 0) {
endTime = startTime + MIN_CUE_DURATION;
}
for (let j = 0; j < frames.length; j++) {
const frame = frames[j];
// Safari doesn't put the timestamp frame in the TextTrack
if (!isTimeStampFrame(frame)) {
// add a bounds to any unbounded cues
this.updateId3CueEnds(startTime, type);
const cue = new Cue(startTime, endTime, '');
cue.value = frame;
if (type) {
cue.type = type;
}
this.id3Track.addCue(cue);
}
}
}
}
}
updateId3CueEnds(startTime, type) {
var _this$id3Track;
const cues = (_this$id3Track = this.id3Track) == null ? void 0 : _this$id3Track.cues;
if (cues) {
for (let i = cues.length; i--;) {
const cue = cues[i];
if (cue.type === type && cue.startTime < startTime && cue.endTime === MAX_CUE_ENDTIME) {
cue.endTime = startTime;
}
}
}
}
onBufferFlushing(event, {
startOffset,
endOffset,
type
}) {
const {
id3Track,
hls
} = this;
if (!hls) {
return;
}
const {
config: {
enableEmsgMetadataCues,
enableID3MetadataCues
}
} = hls;
if (id3Track && (enableEmsgMetadataCues || enableID3MetadataCues)) {
let predicate;
if (type === 'audio') {
predicate = cue => cue.type === MetadataSchema.audioId3 && enableID3MetadataCues;
} else if (type === 'video') {
predicate = cue => cue.type === MetadataSchema.emsg && enableEmsgMetadataCues;
} else {
predicate = cue => cue.type === MetadataSchema.audioId3 && enableID3MetadataCues || cue.type === MetadataSchema.emsg && enableEmsgMetadataCues;
}
removeCuesInRange(id3Track, startOffset, endOffset, predicate);
}
}
onLevelUpdated(event, {
details
}) {
if (!this.media || !details.hasProgramDateTime || !this.hls.config.enableDateRangeMetadataCues) {
return;
}
const {
dateRangeCuesAppended,
id3Track
} = this;
const {
dateRanges
} = details;
const ids = Object.keys(dateRanges);
// Remove cues from track not found in details.dateRanges
if (id3Track) {
const idsToRemove = Object.keys(dateRangeCuesAppended).filter(id => !ids.includes(id));
for (let i = idsToRemove.length; i--;) {
const id = idsToRemove[i];
Object.keys(dateRangeCuesAppended[id].cues).forEach(key => {
id3Track.removeCue(dateRangeCuesAppended[id].cues[key]);
});
delete dateRangeCuesAppended[id];
}
}
// Exit if the playlist does not have Date Ranges or does not have Program Date Time
const lastFragment = details.fragments[details.fragments.length - 1];
if (ids.length === 0 || !isFiniteNumber(lastFragment == null ? void 0 : lastFragment.programDateTime)) {
return;
}
if (!this.id3Track) {
this.id3Track = this.createTrack(this.media);
}
const dateTimeOffset = lastFragment.programDateTime / 1000 - lastFragment.start;
const Cue = getCueClass();
for (let i = 0; i < ids.length; i++) {
const id = ids[i];
const dateRange = dateRanges[id];
const appendedDateRangeCues = dateRangeCuesAppended[id];
const cues = (appendedDateRangeCues == null ? void 0 : appendedDateRangeCues.cues) || {};
let durationKnown = (appendedDateRangeCues == null ? void 0 : appendedDateRangeCues.durationKnown) || false;
const startTime = dateRangeDateToTimelineSeconds(dateRange.startDate, dateTimeOffset);
let endTime = MAX_CUE_ENDTIME;
const endDate = dateRange.endDate;
if (endDate) {
endTime = dateRangeDateToTimelineSeconds(endDate, dateTimeOffset);
durationKnown = true;
} else if (dateRange.endOnNext && !durationKnown) {
const nextDateRangeWithSameClass = ids.reduce((filterMapArray, id) => {
const candidate = dateRanges[id];
if (candidate.class === dateRange.class && candidate.id !== id && candidate.startDate > dateRange.startDate) {
filterMapArray.push(candidate);
}
return filterMapArray;
}, []).sort((a, b) => a.startDate.getTime() - b.startDate.getTime())[0];
if (nextDateRangeWithSameClass) {
endTime = dateRangeDateToTimelineSeconds(nextDateRangeWithSameClass.startDate, dateTimeOffset);
durationKnown = true;
}
}
const attributes = Object.keys(dateRange.attr);
for (let j = 0; j < attributes.length; j++) {
const key = attributes[j];
if (!isDateRangeCueAttribute(key)) {
continue;
}
let cue = cues[key];
if (cue) {
if (durationKnown && !appendedDateRangeCues.durationKnown) {
cue.endTime = endTime;
}
} else {
let data = dateRange.attr[key];
cue = new Cue(startTime, endTime, '');
if (isSCTE35Attribute(key)) {
data = hexToArrayBuffer(data);
}
cue.value = {
key,
data
};
cue.type = MetadataSchema.dateRange;
cue.id = id;
this.id3Track.addCue(cue);
cues[key] = cue;
}
}
dateRangeCuesAppended[id] = {
cues,
dateRange,
durationKnown
};
}
}
}
class LatencyController {
constructor(hls) {
this.hls = void 0;
this.config = void 0;
this.media = null;
this.levelDetails = null;
this.currentTime = 0;
this.stallCount = 0;
this._latency = null;
this.timeupdateHandler = () => this.timeupdate();
this.hls = hls;
this.config = hls.config;
this.registerListeners();
}
get latency() {
return this._latency || 0;
}
get maxLatency() {
const {
config,
levelDetails
} = this;
if (config.liveMaxLatencyDuration !== undefined) {
return config.liveMaxLatencyDuration;
}
return levelDetails ? config.liveMaxLatencyDurationCount * levelDetails.targetduration : 0;
}
get targetLatency() {
const {
levelDetails
} = this;
if (levelDetails === null) {
return null;
}
const {
holdBack,
partHoldBack,
targetduration
} = levelDetails;
const {
liveSyncDuration,
liveSyncDurationCount,
lowLatencyMode
} = this.config;
const userConfig = this.hls.userConfig;
let targetLatency = lowLatencyMode ? partHoldBack || holdBack : holdBack;
if (userConfig.liveSyncDuration || userConfig.liveSyncDurationCount || targetLatency === 0) {
targetLatency = liveSyncDuration !== undefined ? liveSyncDuration : liveSyncDurationCount * targetduration;
}
const maxLiveSyncOnStallIncrease = targetduration;
const liveSyncOnStallIncrease = 1.0;
return targetLatency + Math.min(this.stallCount * liveSyncOnStallIncrease, maxLiveSyncOnStallIncrease);
}
get liveSyncPosition() {
const liveEdge = this.estimateLiveEdge();
const targetLatency = this.targetLatency;
const levelDetails = this.levelDetails;
if (liveEdge === null || targetLatency === null || levelDetails === null) {
return null;
}
const edge = levelDetails.edge;
const syncPosition = liveEdge - targetLatency - this.edgeStalled;
const min = edge - levelDetails.totalduration;
const max = edge - (this.config.lowLatencyMode && levelDetails.partTarget || levelDetails.targetduration);
return Math.min(Math.max(min, syncPosition), max);
}
get drift() {
const {
levelDetails
} = this;
if (levelDetails === null) {
return 1;
}
return levelDetails.drift;
}
get edgeStalled() {
const {
levelDetails
} = this;
if (levelDetails === null) {
return 0;
}
const maxLevelUpdateAge = (this.config.lowLatencyMode && levelDetails.partTarget || levelDetails.targetduration) * 3;
return Math.max(levelDetails.age - maxLevelUpdateAge, 0);
}
get forwardBufferLength() {
const {
media,
levelDetails
} = this;
if (!media || !levelDetails) {
return 0;
}
const bufferedRanges = media.buffered.length;
return (bufferedRanges ? media.buffered.end(bufferedRanges - 1) : levelDetails.edge) - this.currentTime;
}
destroy() {
this.unregisterListeners();
this.onMediaDetaching();
this.levelDetails = null;
// @ts-ignore
this.hls = this.timeupdateHandler = null;
}
registerListeners() {
this.hls.on(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
this.hls.on(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
this.hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
this.hls.on(Events.LEVEL_UPDATED, this.onLevelUpdated, this);
this.hls.on(Events.ERROR, this.onError, this);
}
unregisterListeners() {
this.hls.off(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
this.hls.off(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
this.hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
this.hls.off(Events.LEVEL_UPDATED, this.onLevelUpdated, this);
this.hls.off(Events.ERROR, this.onError, this);
}
onMediaAttached(event, data) {
this.media = data.media;
this.media.addEventListener('timeupdate', this.timeupdateHandler);
}
onMediaDetaching() {
if (this.media) {
this.media.removeEventListener('timeupdate', this.timeupdateHandler);
this.media = null;
}
}
onManifestLoading() {
this.levelDetails = null;
this._latency = null;
this.stallCount = 0;
}
onLevelUpdated(event, {
details
}) {
this.levelDetails = details;
if (details.advanced) {
this.timeupdate();
}
if (!details.live && this.media) {
this.media.removeEventListener('timeupdate', this.timeupdateHandler);
}
}
onError(event, data) {
var _this$levelDetails;
if (data.details !== ErrorDetails.BUFFER_STALLED_ERROR) {
return;
}
this.stallCount++;
if ((_this$levelDetails = this.levelDetails) != null && _this$levelDetails.live) {
logger.warn('[playback-rate-controller]: Stall detected, adjusting target latency');
}
}
timeupdate() {
const {
media,
levelDetails
} = this;
if (!media || !levelDetails) {
return;
}
this.currentTime = media.currentTime;
const latency = this.computeLatency();
if (latency === null) {
return;
}
this._latency = latency;
// Adapt playbackRate to meet target latency in low-latency mode
const {
lowLatencyMode,
maxLiveSyncPlaybackRate
} = this.config;
if (!lowLatencyMode || maxLiveSyncPlaybackRate === 1) {
return;
}
const targetLatency = this.targetLatency;
if (targetLatency === null) {
return;
}
const distanceFromTarget = latency - targetLatency;
// Only adjust playbackRate when within one target duration of targetLatency
// and more than one second from under-buffering.
// Playback further than one target duration from target can be considered DVR playback.
const liveMinLatencyDuration = Math.min(this.maxLatency, targetLatency + levelDetails.targetduration);
const inLiveRange = distanceFromTarget < liveMinLatencyDuration;
if (levelDetails.live && inLiveRange && distanceFromTarget > 0.05 && this.forwardBufferLength > 1) {
const max = Math.min(2, Math.max(1.0, maxLiveSyncPlaybackRate));
const rate = Math.round(2 / (1 + Math.exp(-0.75 * distanceFromTarget - this.edgeStalled)) * 20) / 20;
media.playbackRate = Math.min(max, Math.max(1, rate));
} else if (media.playbackRate !== 1 && media.playbackRate !== 0) {
media.playbackRate = 1;
}
}
estimateLiveEdge() {
const {
levelDetails
} = this;
if (levelDetails === null) {
return null;
}
return levelDetails.edge + levelDetails.age;
}
computeLatency() {
const liveEdge = this.estimateLiveEdge();
if (liveEdge === null) {
return null;
}
return liveEdge - this.currentTime;
}
}
const HdcpLevels = ['NONE', 'TYPE-0', 'TYPE-1', null];
var HlsSkip = {
No: "",
Yes: "YES",
v2: "v2"
};
function getSkipValue(details, msn) {
const {
canSkipUntil,
canSkipDateRanges,
endSN
} = details;
const snChangeGoal = msn !== undefined ? msn - endSN : 0;
if (canSkipUntil && snChangeGoal < canSkipUntil) {
if (canSkipDateRanges) {
return HlsSkip.v2;
}
return HlsSkip.Yes;
}
return HlsSkip.No;
}
class HlsUrlParameters {
constructor(msn, part, skip) {
this.msn = void 0;
this.part = void 0;
this.skip = void 0;
this.msn = msn;
this.part = part;
this.skip = skip;
}
addDirectives(uri) {
const url = new self.URL(uri);
if (this.msn !== undefined) {
url.searchParams.set('_HLS_msn', this.msn.toString());
}
if (this.part !== undefined) {
url.searchParams.set('_HLS_part', this.part.toString());
}
if (this.skip) {
url.searchParams.set('_HLS_skip', this.skip);
}
return url.href;
}
}
class Level {
constructor(data) {
this._attrs = void 0;
this.audioCodec = void 0;
this.bitrate = void 0;
this.codecSet = void 0;
this.height = void 0;
this.id = void 0;
this.name = void 0;
this.videoCodec = void 0;
this.width = void 0;
this.unknownCodecs = void 0;
this.audioGroupIds = void 0;
this.details = void 0;
this.fragmentError = 0;
this.loadError = 0;
this.loaded = void 0;
this.realBitrate = 0;
this.textGroupIds = void 0;
this.url = void 0;
this._urlId = 0;
this.url = [data.url];
this._attrs = [data.attrs];
this.bitrate = data.bitrate;
if (data.details) {
this.details = data.details;
}
this.id = data.id || 0;
this.name = data.name;
this.width = data.width || 0;
this.height = data.height || 0;
this.audioCodec = data.audioCodec;
this.videoCodec = data.videoCodec;
this.unknownCodecs = data.unknownCodecs;
this.codecSet = [data.videoCodec, data.audioCodec].filter(c => c).join(',').replace(/\.[^.,]+/g, '');
}
get maxBitrate() {
return Math.max(this.realBitrate, this.bitrate);
}
get attrs() {
return this._attrs[this._urlId];
}
get pathwayId() {
return this.attrs['PATHWAY-ID'] || '.';
}
get uri() {
return this.url[this._urlId] || '';
}
get urlId() {
return this._urlId;
}
set urlId(value) {
const newValue = value % this.url.length;
if (this._urlId !== newValue) {
this.fragmentError = 0;
this.loadError = 0;
this.details = undefined;
this._urlId = newValue;
}
}
get audioGroupId() {
var _this$audioGroupIds;
return (_this$audioGroupIds = this.audioGroupIds) == null ? void 0 : _this$audioGroupIds[this.urlId];
}
get textGroupId() {
var _this$textGroupIds;
return (_this$textGroupIds = this.textGroupIds) == null ? void 0 : _this$textGroupIds[this.urlId];
}
addFallback(data) {
this.url.push(data.url);
this._attrs.push(data.attrs);
}
}
function updateFromToPTS(fragFrom, fragTo) {
const fragToPTS = fragTo.startPTS;
// if we know startPTS[toIdx]
if (isFiniteNumber(fragToPTS)) {
// update fragment duration.
// it helps to fix drifts between playlist reported duration and fragment real duration
let duration = 0;
let frag;
if (fragTo.sn > fragFrom.sn) {
duration = fragToPTS - fragFrom.start;
frag = fragFrom;
} else {
duration = fragFrom.start - fragToPTS;
frag = fragTo;
}
if (frag.duration !== duration) {
frag.duration = duration;
}
// we dont know startPTS[toIdx]
} else if (fragTo.sn > fragFrom.sn) {
const contiguous = fragFrom.cc === fragTo.cc;
// TODO: With part-loading end/durations we need to confirm the whole fragment is loaded before using (or setting) minEndPTS
if (contiguous && fragFrom.minEndPTS) {
fragTo.start = fragFrom.start + (fragFrom.minEndPTS - fragFrom.start);
} else {
fragTo.start = fragFrom.start + fragFrom.duration;
}
} else {
fragTo.start = Math.max(fragFrom.start - fragTo.duration, 0);
}
}
function updateFragPTSDTS(details, frag, startPTS, endPTS, startDTS, endDTS) {
const parsedMediaDuration = endPTS - startPTS;
if (parsedMediaDuration <= 0) {
logger.warn('Fragment should have a positive duration', frag);
endPTS = startPTS + frag.duration;
endDTS = startDTS + frag.duration;
}
let maxStartPTS = startPTS;
let minEndPTS = endPTS;
const fragStartPts = frag.startPTS;
const fragEndPts = frag.endPTS;
if (isFiniteNumber(fragStartPts)) {
// delta PTS between audio and video
const deltaPTS = Math.abs(fragStartPts - startPTS);
if (!isFiniteNumber(frag.deltaPTS)) {
frag.deltaPTS = deltaPTS;
} else {
frag.deltaPTS = Math.max(deltaPTS, frag.deltaPTS);
}
maxStartPTS = Math.max(startPTS, fragStartPts);
startPTS = Math.min(startPTS, fragStartPts);
startDTS = Math.min(startDTS, frag.startDTS);
minEndPTS = Math.min(endPTS, fragEndPts);
endPTS = Math.max(endPTS, fragEndPts);
endDTS = Math.max(endDTS, frag.endDTS);
}
const drift = startPTS - frag.start;
if (frag.start !== 0) {
frag.start = startPTS;
}
frag.duration = endPTS - frag.start;
frag.startPTS = startPTS;
frag.maxStartPTS = maxStartPTS;
frag.startDTS = startDTS;
frag.endPTS = endPTS;
frag.minEndPTS = minEndPTS;
frag.endDTS = endDTS;
const sn = frag.sn; // 'initSegment'
// exit if sn out of range
if (!details || sn < details.startSN || sn > details.endSN) {
return 0;
}
let i;
const fragIdx = sn - details.startSN;
const fragments = details.fragments;
// update frag reference in fragments array
// rationale is that fragments array might not contain this frag object.
// this will happen if playlist has been refreshed between frag loading and call to updateFragPTSDTS()
// if we don't update frag, we won't be able to propagate PTS info on the playlist
// resulting in invalid sliding computation
fragments[fragIdx] = frag;
// adjust fragment PTS/duration from seqnum-1 to frag 0
for (i = fragIdx; i > 0; i--) {
updateFromToPTS(fragments[i], fragments[i - 1]);
}
// adjust fragment PTS/duration from seqnum to last frag
for (i = fragIdx; i < fragments.length - 1; i++) {
updateFromToPTS(fragments[i], fragments[i + 1]);
}
if (details.fragmentHint) {
updateFromToPTS(fragments[fragments.length - 1], details.fragmentHint);
}
details.PTSKnown = details.alignedSliding = true;
return drift;
}
function mergeDetails(oldDetails, newDetails) {
// Track the last initSegment processed. Initialize it to the last one on the timeline.
let currentInitSegment = null;
const oldFragments = oldDetails.fragments;
for (let i = oldFragments.length - 1; i >= 0; i--) {
const oldInit = oldFragments[i].initSegment;
if (oldInit) {
currentInitSegment = oldInit;
break;
}
}
if (oldDetails.fragmentHint) {
// prevent PTS and duration from being adjusted on the next hint
delete oldDetails.fragmentHint.endPTS;
}
// check if old/new playlists have fragments in common
// loop through overlapping SN and update startPTS , cc, and duration if any found
let ccOffset = 0;
let PTSFrag;
mapFragmentIntersection(oldDetails, newDetails, (oldFrag, newFrag) => {
if (oldFrag.relurl) {
// Do not compare CC if the old fragment has no url. This is a level.fragmentHint used by LL-HLS parts.
// It maybe be off by 1 if it was created before any parts or discontinuity tags were appended to the end
// of the playlist.
ccOffset = oldFrag.cc - newFrag.cc;
}
if (isFiniteNumber(oldFrag.startPTS) && isFiniteNumber(oldFrag.endPTS)) {
newFrag.start = newFrag.startPTS = oldFrag.startPTS;
newFrag.startDTS = oldFrag.startDTS;
newFrag.maxStartPTS = oldFrag.maxStartPTS;
newFrag.endPTS = oldFrag.endPTS;
newFrag.endDTS = oldFrag.endDTS;
newFrag.minEndPTS = oldFrag.minEndPTS;
newFrag.duration = oldFrag.endPTS - oldFrag.startPTS;
if (newFrag.duration) {
PTSFrag = newFrag;
}
// PTS is known when any segment has startPTS and endPTS
newDetails.PTSKnown = newDetails.alignedSliding = true;
}
newFrag.elementaryStreams = oldFrag.elementaryStreams;
newFrag.loader = oldFrag.loader;
newFrag.stats = oldFrag.stats;
newFrag.urlId = oldFrag.urlId;
if (oldFrag.initSegment) {
newFrag.initSegment = oldFrag.initSegment;
currentInitSegment = oldFrag.initSegment;
}
});
if (currentInitSegment) {
const fragmentsToCheck = newDetails.fragmentHint ? newDetails.fragments.concat(newDetails.fragmentHint) : newDetails.fragments;
fragmentsToCheck.forEach(frag => {
var _currentInitSegment;
if (!frag.initSegment || frag.initSegment.relurl === ((_currentInitSegment = currentInitSegment) == null ? void 0 : _currentInitSegment.relurl)) {
frag.initSegment = currentInitSegment;
}
});
}
if (newDetails.skippedSegments) {
newDetails.deltaUpdateFailed = newDetails.fragments.some(frag => !frag);
if (newDetails.deltaUpdateFailed) {
logger.warn('[level-helper] Previous playlist missing segments skipped in delta playlist');
for (let i = newDetails.skippedSegments; i--;) {
newDetails.fragments.shift();
}
newDetails.startSN = newDetails.fragments[0].sn;
newDetails.startCC = newDetails.fragments[0].cc;
} else if (newDetails.canSkipDateRanges) {
newDetails.dateRanges = mergeDateRanges(oldDetails.dateRanges, newDetails.dateRanges, newDetails.recentlyRemovedDateranges);
}
}
const newFragments = newDetails.fragments;
if (ccOffset) {
logger.warn('discontinuity sliding from playlist, take drift into account');
for (let i = 0; i < newFragments.length; i++) {
newFragments[i].cc += ccOffset;
}
}
if (newDetails.skippedSegments) {
newDetails.startCC = newDetails.fragments[0].cc;
}
// Merge parts
mapPartIntersection(oldDetails.partList, newDetails.partList, (oldPart, newPart) => {
newPart.elementaryStreams = oldPart.elementaryStreams;
newPart.stats = oldPart.stats;
});
// if at least one fragment contains PTS info, recompute PTS information for all fragments
if (PTSFrag) {
updateFragPTSDTS(newDetails, PTSFrag, PTSFrag.startPTS, PTSFrag.endPTS, PTSFrag.startDTS, PTSFrag.endDTS);
} else {
// ensure that delta is within oldFragments range
// also adjust sliding in case delta is 0 (we could have old=[50-60] and new=old=[50-61])
// in that case we also need to adjust start offset of all fragments
adjustSliding(oldDetails, newDetails);
}
if (newFragments.length) {
newDetails.totalduration = newDetails.edge - newFragments[0].start;
}
newDetails.driftStartTime = oldDetails.driftStartTime;
newDetails.driftStart = oldDetails.driftStart;
const advancedDateTime = newDetails.advancedDateTime;
if (newDetails.advanced && advancedDateTime) {
const edge = newDetails.edge;
if (!newDetails.driftStart) {
newDetails.driftStartTime = advancedDateTime;
newDetails.driftStart = edge;
}
newDetails.driftEndTime = advancedDateTime;
newDetails.driftEnd = edge;
} else {
newDetails.driftEndTime = oldDetails.driftEndTime;
newDetails.driftEnd = oldDetails.driftEnd;
newDetails.advancedDateTime = oldDetails.advancedDateTime;
}
}
function mergeDateRanges(oldDateRanges, deltaDateRanges, recentlyRemovedDateranges) {
const dateRanges = _extends({}, oldDateRanges);
if (recentlyRemovedDateranges) {
recentlyRemovedDateranges.forEach(id => {
delete dateRanges[id];
});
}
Object.keys(deltaDateRanges).forEach(id => {
const dateRange = new DateRange(deltaDateRanges[id].attr, dateRanges[id]);
if (dateRange.isValid) {
dateRanges[id] = dateRange;
} else {
logger.warn(`Ignoring invalid Playlist Delta Update DATERANGE tag: "${JSON.stringify(deltaDateRanges[id].attr)}"`);
}
});
return dateRanges;
}
function mapPartIntersection(oldParts, newParts, intersectionFn) {
if (oldParts && newParts) {
let delta = 0;
for (let i = 0, len = oldParts.length; i <= len; i++) {
const oldPart = oldParts[i];
const newPart = newParts[i + delta];
if (oldPart && newPart && oldPart.index === newPart.index && oldPart.fragment.sn === newPart.fragment.sn) {
intersectionFn(oldPart, newPart);
} else {
delta--;
}
}
}
}
function mapFragmentIntersection(oldDetails, newDetails, intersectionFn) {
const skippedSegments = newDetails.skippedSegments;
const start = Math.max(oldDetails.startSN, newDetails.startSN) - newDetails.startSN;
const end = (oldDetails.fragmentHint ? 1 : 0) + (skippedSegments ? newDetails.endSN : Math.min(oldDetails.endSN, newDetails.endSN)) - newDetails.startSN;
const delta = newDetails.startSN - oldDetails.startSN;
const newFrags = newDetails.fragmentHint ? newDetails.fragments.concat(newDetails.fragmentHint) : newDetails.fragments;
const oldFrags = oldDetails.fragmentHint ? oldDetails.fragments.concat(oldDetails.fragmentHint) : oldDetails.fragments;
for (let i = start; i <= end; i++) {
const oldFrag = oldFrags[delta + i];
let newFrag = newFrags[i];
if (skippedSegments && !newFrag && i < skippedSegments) {
// Fill in skipped segments in delta playlist
newFrag = newDetails.fragments[i] = oldFrag;
}
if (oldFrag && newFrag) {
intersectionFn(oldFrag, newFrag);
}
}
}
function adjustSliding(oldDetails, newDetails) {
const delta = newDetails.startSN + newDetails.skippedSegments - oldDetails.startSN;
const oldFragments = oldDetails.fragments;
if (delta < 0 || delta >= oldFragments.length) {
return;
}
addSliding(newDetails, oldFragments[delta].start);
}
function addSliding(details, start) {
if (start) {
const fragments = details.fragments;
for (let i = details.skippedSegments; i < fragments.length; i++) {
fragments[i].start += start;
}
if (details.fragmentHint) {
details.fragmentHint.start += start;
}
}
}
function computeReloadInterval(newDetails, distanceToLiveEdgeMs = Infinity) {
let reloadInterval = 1000 * newDetails.targetduration;
if (newDetails.updated) {
// Use last segment duration when shorter than target duration and near live edge
const fragments = newDetails.fragments;
const liveEdgeMaxTargetDurations = 4;
if (fragments.length && reloadInterval * liveEdgeMaxTargetDurations > distanceToLiveEdgeMs) {
const lastSegmentDuration = fragments[fragments.length - 1].duration * 1000;
if (lastSegmentDuration < reloadInterval) {
reloadInterval = lastSegmentDuration;
}
}
} else {
// estimate = 'miss half average';
// follow HLS Spec, If the client reloads a Playlist file and finds that it has not
// changed then it MUST wait for a period of one-half the target
// duration before retrying.
reloadInterval /= 2;
}
return Math.round(reloadInterval);
}
function getFragmentWithSN(level, sn, fragCurrent) {
if (!(level != null && level.details)) {
return null;
}
const levelDetails = level.details;
let fragment = levelDetails.fragments[sn - levelDetails.startSN];
if (fragment) {
return fragment;
}
fragment = levelDetails.fragmentHint;
if (fragment && fragment.sn === sn) {
return fragment;
}
if (sn < levelDetails.startSN && fragCurrent && fragCurrent.sn === sn) {
return fragCurrent;
}
return null;
}
function getPartWith(level, sn, partIndex) {
var _level$details;
if (!(level != null && level.details)) {
return null;
}
return findPart((_level$details = level.details) == null ? void 0 : _level$details.partList, sn, partIndex);
}
function findPart(partList, sn, partIndex) {
if (partList) {
for (let i = partList.length; i--;) {
const part = partList[i];
if (part.index === partIndex && part.fragment.sn === sn) {
return part;
}
}
}
return null;
}
function isTimeoutError(error) {
switch (error.details) {
case ErrorDetails.FRAG_LOAD_TIMEOUT:
case ErrorDetails.KEY_LOAD_TIMEOUT:
case ErrorDetails.LEVEL_LOAD_TIMEOUT:
case ErrorDetails.MANIFEST_LOAD_TIMEOUT:
return true;
}
return false;
}
function getRetryConfig(loadPolicy, error) {
const isTimeout = isTimeoutError(error);
return loadPolicy.default[`${isTimeout ? 'timeout' : 'error'}Retry`];
}
function getRetryDelay(retryConfig, retryCount) {
// exponential backoff capped to max retry delay
const backoffFactor = retryConfig.backoff === 'linear' ? 1 : Math.pow(2, retryCount);
return Math.min(backoffFactor * retryConfig.retryDelayMs, retryConfig.maxRetryDelayMs);
}
function getLoaderConfigWithoutReties(loderConfig) {
return _objectSpread2(_objectSpread2({}, loderConfig), {
errorRetry: null,
timeoutRetry: null
});
}
function shouldRetry(retryConfig, retryCount, isTimeout, httpStatus) {
return !!retryConfig && retryCount < retryConfig.maxNumRetry && (retryForHttpStatus(httpStatus) || !!isTimeout);
}
function retryForHttpStatus(httpStatus) {
// Do not retry on status 4xx, status 0 (CORS error), or undefined (decrypt/gap/parse error)
return httpStatus === 0 && navigator.onLine === false || !!httpStatus && (httpStatus < 400 || httpStatus > 499);
}
const BinarySearch = {
/**
* Searches for an item in an array which matches a certain condition.
* This requires the condition to only match one item in the array,
* and for the array to be ordered.
*
* @param list The array to search.
* @param comparisonFn
* Called and provided a candidate item as the first argument.
* Should return:
* > -1 if the item should be located at a lower index than the provided item.
* > 1 if the item should be located at a higher index than the provided item.
* > 0 if the item is the item you're looking for.
*
* @returns the object if found, otherwise returns null
*/
search: function (list, comparisonFn) {
let minIndex = 0;
let maxIndex = list.length - 1;
let currentIndex = null;
let currentElement = null;
while (minIndex <= maxIndex) {
currentIndex = (minIndex + maxIndex) / 2 | 0;
currentElement = list[currentIndex];
const comparisonResult = comparisonFn(currentElement);
if (comparisonResult > 0) {
minIndex = currentIndex + 1;
} else if (comparisonResult < 0) {
maxIndex = currentIndex - 1;
} else {
return currentElement;
}
}
return null;
}
};
/**
* Returns first fragment whose endPdt value exceeds the given PDT, or null.
* @param fragments - The array of candidate fragments
* @param PDTValue - The PDT value which must be exceeded
* @param maxFragLookUpTolerance - The amount of time that a fragment's start/end can be within in order to be considered contiguous
*/
function findFragmentByPDT(fragments, PDTValue, maxFragLookUpTolerance) {
if (PDTValue === null || !Array.isArray(fragments) || !fragments.length || !isFiniteNumber(PDTValue)) {
return null;
}
// if less than start
const startPDT = fragments[0].programDateTime;
if (PDTValue < (startPDT || 0)) {
return null;
}
const endPDT = fragments[fragments.length - 1].endProgramDateTime;
if (PDTValue >= (endPDT || 0)) {
return null;
}
maxFragLookUpTolerance = maxFragLookUpTolerance || 0;
for (let seg = 0; seg < fragments.length; ++seg) {
const frag = fragments[seg];
if (pdtWithinToleranceTest(PDTValue, maxFragLookUpTolerance, frag)) {
return frag;
}
}
return null;
}
/**
* Finds a fragment based on the SN of the previous fragment; or based on the needs of the current buffer.
* This method compensates for small buffer gaps by applying a tolerance to the start of any candidate fragment, thus
* breaking any traps which would cause the same fragment to be continuously selected within a small range.
* @param fragPrevious - The last frag successfully appended
* @param fragments - The array of candidate fragments
* @param bufferEnd - The end of the contiguous buffered range the playhead is currently within
* @param maxFragLookUpTolerance - The amount of time that a fragment's start/end can be within in order to be considered contiguous
* @returns a matching fragment or null
*/
function findFragmentByPTS(fragPrevious, fragments, bufferEnd = 0, maxFragLookUpTolerance = 0) {
let fragNext = null;
if (fragPrevious) {
fragNext = fragments[fragPrevious.sn - fragments[0].sn + 1] || null;
} else if (bufferEnd === 0 && fragments[0].start === 0) {
fragNext = fragments[0];
}
// Prefer the next fragment if it's within tolerance
if (fragNext && fragmentWithinToleranceTest(bufferEnd, maxFragLookUpTolerance, fragNext) === 0) {
return fragNext;
}
// We might be seeking past the tolerance so find the best match
const foundFragment = BinarySearch.search(fragments, fragmentWithinToleranceTest.bind(null, bufferEnd, maxFragLookUpTolerance));
if (foundFragment && (foundFragment !== fragPrevious || !fragNext)) {
return foundFragment;
}
// If no match was found return the next fragment after fragPrevious, or null
return fragNext;
}
/**
* The test function used by the findFragmentBySn's BinarySearch to look for the best match to the current buffer conditions.
* @param candidate - The fragment to test
* @param bufferEnd - The end of the current buffered range the playhead is currently within
* @param maxFragLookUpTolerance - The amount of time that a fragment's start can be within in order to be considered contiguous
* @returns 0 if it matches, 1 if too low, -1 if too high
*/
function fragmentWithinToleranceTest(bufferEnd = 0, maxFragLookUpTolerance = 0, candidate) {
// eagerly accept an accurate match (no tolerance)
if (candidate.start <= bufferEnd && candidate.start + candidate.duration > bufferEnd) {
return 0;
}
// offset should be within fragment boundary - config.maxFragLookUpTolerance
// this is to cope with situations like
// bufferEnd = 9.991
// frag[Ø] : [0,10]
// frag[1] : [10,20]
// bufferEnd is within frag[0] range ... although what we are expecting is to return frag[1] here
// frag start frag start+duration
// |-----------------------------|
// <---> <--->
// ...--------><-----------------------------><---------....
// previous frag matching fragment next frag
// return -1 return 0 return 1
// logger.log(`level/sn/start/end/bufEnd:${level}/${candidate.sn}/${candidate.start}/${(candidate.start+candidate.duration)}/${bufferEnd}`);
// Set the lookup tolerance to be small enough to detect the current segment - ensures we don't skip over very small segments
const candidateLookupTolerance = Math.min(maxFragLookUpTolerance, candidate.duration + (candidate.deltaPTS ? candidate.deltaPTS : 0));
if (candidate.start + candidate.duration - candidateLookupTolerance <= bufferEnd) {
return 1;
} else if (candidate.start - candidateLookupTolerance > bufferEnd && candidate.start) {
// if maxFragLookUpTolerance will have negative value then don't return -1 for first element
return -1;
}
return 0;
}
/**
* The test function used by the findFragmentByPdt's BinarySearch to look for the best match to the current buffer conditions.
* This function tests the candidate's program date time values, as represented in Unix time
* @param candidate - The fragment to test
* @param pdtBufferEnd - The Unix time representing the end of the current buffered range
* @param maxFragLookUpTolerance - The amount of time that a fragment's start can be within in order to be considered contiguous
* @returns true if contiguous, false otherwise
*/
function pdtWithinToleranceTest(pdtBufferEnd, maxFragLookUpTolerance, candidate) {
const candidateLookupTolerance = Math.min(maxFragLookUpTolerance, candidate.duration + (candidate.deltaPTS ? candidate.deltaPTS : 0)) * 1000;
// endProgramDateTime can be null, default to zero
const endProgramDateTime = candidate.endProgramDateTime || 0;
return endProgramDateTime - candidateLookupTolerance > pdtBufferEnd;
}
function findFragWithCC(fragments, cc) {
return BinarySearch.search(fragments, candidate => {
if (candidate.cc < cc) {
return 1;
} else if (candidate.cc > cc) {
return -1;
} else {
return 0;
}
});
}
const RENDITION_PENALTY_DURATION_MS = 300000;
var NetworkErrorAction = {
DoNothing: 0,
SendEndCallback: 1,
SendAlternateToPenaltyBox: 2,
RemoveAlternatePermanently: 3,
InsertDiscontinuity: 4,
RetryRequest: 5
};
var ErrorActionFlags = {
None: 0,
MoveAllAlternatesMatchingHost: 1,
MoveAllAlternatesMatchingHDCP: 2,
SwitchToSDR: 4
}; // Reserved for future use
class ErrorController {
constructor(hls) {
this.hls = void 0;
this.playlistError = 0;
this.penalizedRenditions = {};
this.log = void 0;
this.warn = void 0;
this.error = void 0;
this.hls = hls;
this.log = logger.log.bind(logger, `[info]:`);
this.warn = logger.warn.bind(logger, `[warning]:`);
this.error = logger.error.bind(logger, `[error]:`);
this.registerListeners();
}
registerListeners() {
const hls = this.hls;
hls.on(Events.ERROR, this.onError, this);
hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.on(Events.LEVEL_UPDATED, this.onLevelUpdated, this);
}
unregisterListeners() {
const hls = this.hls;
if (!hls) {
return;
}
hls.off(Events.ERROR, this.onError, this);
hls.off(Events.ERROR, this.onErrorOut, this);
hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.off(Events.LEVEL_UPDATED, this.onLevelUpdated, this);
}
destroy() {
this.unregisterListeners();
// @ts-ignore
this.hls = null;
this.penalizedRenditions = {};
}
startLoad(startPosition) {
this.playlistError = 0;
}
stopLoad() {}
getVariantLevelIndex(frag) {
return (frag == null ? void 0 : frag.type) === PlaylistLevelType.MAIN ? frag.level : this.hls.loadLevel;
}
onManifestLoading() {
this.playlistError = 0;
this.penalizedRenditions = {};
}
onLevelUpdated() {
this.playlistError = 0;
}
onError(event, data) {
var _data$frag, _data$level;
if (data.fatal) {
return;
}
const hls = this.hls;
const context = data.context;
switch (data.details) {
case ErrorDetails.FRAG_LOAD_ERROR:
case ErrorDetails.FRAG_LOAD_TIMEOUT:
case ErrorDetails.KEY_LOAD_ERROR:
case ErrorDetails.KEY_LOAD_TIMEOUT:
data.errorAction = this.getFragRetryOrSwitchAction(data);
return;
case ErrorDetails.FRAG_PARSING_ERROR:
// ignore empty segment errors marked as gap
if ((_data$frag = data.frag) != null && _data$frag.gap) {
data.errorAction = {
action: NetworkErrorAction.DoNothing,
flags: ErrorActionFlags.None
};
return;
}
// falls through
case ErrorDetails.FRAG_GAP:
case ErrorDetails.FRAG_DECRYPT_ERROR:
{
// Switch level if possible, otherwise allow retry count to reach max error retries
data.errorAction = this.getFragRetryOrSwitchAction(data);
data.errorAction.action = NetworkErrorAction.SendAlternateToPenaltyBox;
return;
}
case ErrorDetails.LEVEL_EMPTY_ERROR:
case ErrorDetails.LEVEL_PARSING_ERROR:
{
var _data$context, _data$context$levelDe;
// Only retry when empty and live
const levelIndex = data.parent === PlaylistLevelType.MAIN ? data.level : hls.loadLevel;
if (data.details === ErrorDetails.LEVEL_EMPTY_ERROR && !!((_data$context = data.context) != null && (_data$context$levelDe = _data$context.levelDetails) != null && _data$context$levelDe.live)) {
data.errorAction = this.getPlaylistRetryOrSwitchAction(data, levelIndex);
} else {
// Escalate to fatal if not retrying or switching
data.levelRetry = false;
data.errorAction = this.getLevelSwitchAction(data, levelIndex);
}
}
return;
case ErrorDetails.LEVEL_LOAD_ERROR:
case ErrorDetails.LEVEL_LOAD_TIMEOUT:
if (typeof (context == null ? void 0 : context.level) === 'number') {
data.errorAction = this.getPlaylistRetryOrSwitchAction(data, context.level);
}
return;
case ErrorDetails.AUDIO_TRACK_LOAD_ERROR:
case ErrorDetails.AUDIO_TRACK_LOAD_TIMEOUT:
case ErrorDetails.SUBTITLE_LOAD_ERROR:
case ErrorDetails.SUBTITLE_TRACK_LOAD_TIMEOUT:
if (context) {
const level = hls.levels[hls.loadLevel];
if (level && (context.type === PlaylistContextType.AUDIO_TRACK && context.groupId === level.audioGroupId || context.type === PlaylistContextType.SUBTITLE_TRACK && context.groupId === level.textGroupId)) {
// Perform Pathway switch or Redundant failover if possible for fastest recovery
// otherwise allow playlist retry count to reach max error retries
data.errorAction = this.getPlaylistRetryOrSwitchAction(data, hls.loadLevel);
data.errorAction.action = NetworkErrorAction.SendAlternateToPenaltyBox;
data.errorAction.flags = ErrorActionFlags.MoveAllAlternatesMatchingHost;
return;
}
}
return;
case ErrorDetails.KEY_SYSTEM_STATUS_OUTPUT_RESTRICTED:
{
const level = hls.levels[hls.loadLevel];
const restrictedHdcpLevel = level == null ? void 0 : level.attrs['HDCP-LEVEL'];
if (restrictedHdcpLevel) {
data.errorAction = {
action: NetworkErrorAction.SendAlternateToPenaltyBox,
flags: ErrorActionFlags.MoveAllAlternatesMatchingHDCP,
hdcpLevel: restrictedHdcpLevel
};
}
}
return;
case ErrorDetails.BUFFER_ADD_CODEC_ERROR:
case ErrorDetails.REMUX_ALLOC_ERROR:
data.errorAction = this.getLevelSwitchAction(data, (_data$level = data.level) != null ? _data$level : hls.loadLevel);
return;
case ErrorDetails.INTERNAL_EXCEPTION:
case ErrorDetails.BUFFER_APPENDING_ERROR:
case ErrorDetails.BUFFER_APPEND_ERROR:
case ErrorDetails.BUFFER_FULL_ERROR:
case ErrorDetails.LEVEL_SWITCH_ERROR:
case ErrorDetails.BUFFER_STALLED_ERROR:
case ErrorDetails.BUFFER_SEEK_OVER_HOLE:
case ErrorDetails.BUFFER_NUDGE_ON_STALL:
data.errorAction = {
action: NetworkErrorAction.DoNothing,
flags: ErrorActionFlags.None
};
return;
}
if (data.type === ErrorTypes.KEY_SYSTEM_ERROR) {
const levelIndex = this.getVariantLevelIndex(data.frag);
// Do not retry level. Escalate to fatal if switching levels fails.
data.levelRetry = false;
data.errorAction = this.getLevelSwitchAction(data, levelIndex);
return;
}
}
getPlaylistRetryOrSwitchAction(data, levelIndex) {
var _data$response;
const hls = this.hls;
const retryConfig = getRetryConfig(hls.config.playlistLoadPolicy, data);
const retryCount = this.playlistError++;
const httpStatus = (_data$response = data.response) == null ? void 0 : _data$response.code;
const retry = shouldRetry(retryConfig, retryCount, isTimeoutError(data), httpStatus);
if (retry) {
return {
action: NetworkErrorAction.RetryRequest,
flags: ErrorActionFlags.None,
retryConfig,
retryCount
};
}
const errorAction = this.getLevelSwitchAction(data, levelIndex);
if (retryConfig) {
errorAction.retryConfig = retryConfig;
errorAction.retryCount = retryCount;
}
return errorAction;
}
getFragRetryOrSwitchAction(data) {
const hls = this.hls;
// Share fragment error count accross media options (main, audio, subs)
// This allows for level based rendition switching when media option assets fail
const variantLevelIndex = this.getVariantLevelIndex(data.frag);
const level = hls.levels[variantLevelIndex];
const {
fragLoadPolicy,
keyLoadPolicy
} = hls.config;
const retryConfig = getRetryConfig(data.details.startsWith('key') ? keyLoadPolicy : fragLoadPolicy, data);
const fragmentErrors = hls.levels.reduce((acc, level) => acc + level.fragmentError, 0);
// Switch levels when out of retried or level index out of bounds
if (level) {
var _data$response2;
if (data.details !== ErrorDetails.FRAG_GAP) {
level.fragmentError++;
}
const httpStatus = (_data$response2 = data.response) == null ? void 0 : _data$response2.code;
const retry = shouldRetry(retryConfig, fragmentErrors, isTimeoutError(data), httpStatus);
if (retry) {
return {
action: NetworkErrorAction.RetryRequest,
flags: ErrorActionFlags.None,
retryConfig,
retryCount: fragmentErrors
};
}
}
// Reach max retry count, or Missing level reference
// Switch to valid index
const errorAction = this.getLevelSwitchAction(data, variantLevelIndex);
// Add retry details to allow skipping of FRAG_PARSING_ERROR
if (retryConfig) {
errorAction.retryConfig = retryConfig;
errorAction.retryCount = fragmentErrors;
}
return errorAction;
}
getLevelSwitchAction(data, levelIndex) {
const hls = this.hls;
if (levelIndex === null || levelIndex === undefined) {
levelIndex = hls.loadLevel;
}
const level = this.hls.levels[levelIndex];
if (level) {
level.loadError++;
if (hls.autoLevelEnabled) {
var _data$frag2, _data$context2;
// Search for next level to retry
let nextLevel = -1;
const {
levels,
loadLevel,
minAutoLevel,
maxAutoLevel
} = hls;
const fragErrorType = (_data$frag2 = data.frag) == null ? void 0 : _data$frag2.type;
const {
type: playlistErrorType,
groupId: playlistErrorGroupId
} = (_data$context2 = data.context) != null ? _data$context2 : {};
for (let i = levels.length; i--;) {
const candidate = (i + loadLevel) % levels.length;
if (candidate !== loadLevel && candidate >= minAutoLevel && candidate <= maxAutoLevel && levels[candidate].loadError === 0) {
const levelCandidate = levels[candidate];
// Skip level switch if GAP tag is found in next level at same position
if (data.details === ErrorDetails.FRAG_GAP && data.frag) {
const levelDetails = levels[candidate].details;
if (levelDetails) {
const fragCandidate = findFragmentByPTS(data.frag, levelDetails.fragments, data.frag.start);
if (fragCandidate != null && fragCandidate.gap) {
continue;
}
}
} else if (playlistErrorType === PlaylistContextType.AUDIO_TRACK && playlistErrorGroupId === levelCandidate.audioGroupId || playlistErrorType === PlaylistContextType.SUBTITLE_TRACK && playlistErrorGroupId === levelCandidate.textGroupId) {
// For audio/subs playlist errors find another group ID or fallthrough to redundant fail-over
continue;
} else if (fragErrorType === PlaylistLevelType.AUDIO && level.audioGroupId === levelCandidate.audioGroupId || fragErrorType === PlaylistLevelType.SUBTITLE && level.textGroupId === levelCandidate.textGroupId) {
// For audio/subs frag errors find another group ID or fallthrough to redundant fail-over
continue;
}
nextLevel = candidate;
break;
}
}
if (nextLevel > -1 && hls.loadLevel !== nextLevel) {
data.levelRetry = true;
this.playlistError = 0;
return {
action: NetworkErrorAction.SendAlternateToPenaltyBox,
flags: ErrorActionFlags.None,
nextAutoLevel: nextLevel
};
}
}
}
// No levels to switch / Manual level selection / Level not found
// Resolve with Pathway switch, Redundant fail-over, or stay on lowest Level
return {
action: NetworkErrorAction.SendAlternateToPenaltyBox,
flags: ErrorActionFlags.MoveAllAlternatesMatchingHost
};
}
onErrorOut(event, data) {
var _data$errorAction;
switch ((_data$errorAction = data.errorAction) == null ? void 0 : _data$errorAction.action) {
case NetworkErrorAction.DoNothing:
break;
case NetworkErrorAction.SendAlternateToPenaltyBox:
this.sendAlternateToPenaltyBox(data);
if (!data.errorAction.resolved && data.details !== ErrorDetails.FRAG_GAP) {
data.fatal = true;
}
break;
}
if (data.fatal) {
this.hls.stopLoad();
return;
}
}
sendAlternateToPenaltyBox(data) {
const hls = this.hls;
const errorAction = data.errorAction;
if (!errorAction) {
return;
}
const {
flags,
hdcpLevel,
nextAutoLevel
} = errorAction;
switch (flags) {
case ErrorActionFlags.None:
this.switchLevel(data, nextAutoLevel);
break;
case ErrorActionFlags.MoveAllAlternatesMatchingHost:
{
// Handle Redundant Levels here. Pathway switching is handled by content-steering-controller
if (!errorAction.resolved) {
errorAction.resolved = this.redundantFailover(data);
}
}
break;
case ErrorActionFlags.MoveAllAlternatesMatchingHDCP:
if (hdcpLevel) {
hls.maxHdcpLevel = HdcpLevels[HdcpLevels.indexOf(hdcpLevel) - 1];
errorAction.resolved = true;
}
this.warn(`Restricting playback to HDCP-LEVEL of "${hls.maxHdcpLevel}" or lower`);
break;
}
// If not resolved by previous actions try to switch to next level
if (!errorAction.resolved) {
this.switchLevel(data, nextAutoLevel);
}
}
switchLevel(data, levelIndex) {
if (levelIndex !== undefined && data.errorAction) {
this.warn(`switching to level ${levelIndex} after ${data.details}`);
this.hls.nextAutoLevel = levelIndex;
data.errorAction.resolved = true;
// Stream controller is responsible for this but won't switch on false start
this.hls.nextLoadLevel = this.hls.nextAutoLevel;
}
}
redundantFailover(data) {
const {
hls,
penalizedRenditions
} = this;
const levelIndex = data.parent === PlaylistLevelType.MAIN ? data.level : hls.loadLevel;
const level = hls.levels[levelIndex];
const redundantLevels = level.url.length;
const errorUrlId = data.frag ? data.frag.urlId : level.urlId;
if (level.urlId === errorUrlId && (!data.frag || level.details)) {
this.penalizeRendition(level, data);
}
for (let i = 1; i < redundantLevels; i++) {
const newUrlId = (errorUrlId + i) % redundantLevels;
const penalizedRendition = penalizedRenditions[newUrlId];
// Check if rendition is penalized and skip if it is a bad fit for failover
if (!penalizedRendition || checkExpired(penalizedRendition, data, penalizedRenditions[errorUrlId])) {
// delete penalizedRenditions[newUrlId];
// Update the url id of all levels so that we stay on the same set of variants when level switching
this.warn(`Switching to Redundant Stream ${newUrlId + 1}/${redundantLevels}: "${level.url[newUrlId]}" after ${data.details}`);
this.playlistError = 0;
hls.levels.forEach(lv => {
lv.urlId = newUrlId;
});
hls.nextLoadLevel = levelIndex;
return true;
}
}
return false;
}
penalizeRendition(level, data) {
const {
penalizedRenditions
} = this;
const penalizedRendition = penalizedRenditions[level.urlId] || {
lastErrorPerfMs: 0,
errors: [],
details: undefined
};
penalizedRendition.lastErrorPerfMs = performance.now();
penalizedRendition.errors.push(data);
penalizedRendition.details = level.details;
penalizedRenditions[level.urlId] = penalizedRendition;
}
}
function checkExpired(penalizedRendition, data, currentPenaltyState) {
// Expire penalty for switching back to rendition after RENDITION_PENALTY_DURATION_MS
if (performance.now() - penalizedRendition.lastErrorPerfMs > RENDITION_PENALTY_DURATION_MS) {
return true;
}
// Expire penalty on GAP tag error if rendition has no GAP at position (does not cover media tracks)
const lastErrorDetails = penalizedRendition.details;
if (data.details === ErrorDetails.FRAG_GAP && lastErrorDetails && data.frag) {
const position = data.frag.start;
const candidateFrag = findFragmentByPTS(null, lastErrorDetails.fragments, position);
if (candidateFrag && !candidateFrag.gap) {
return true;
}
}
// Expire penalty if there are more errors in currentLevel than in penalizedRendition
if (currentPenaltyState && penalizedRendition.errors.length < currentPenaltyState.errors.length) {
const lastCandidateError = penalizedRendition.errors[penalizedRendition.errors.length - 1];
if (lastErrorDetails && lastCandidateError.frag && data.frag && Math.abs(lastCandidateError.frag.start - data.frag.start) > lastErrorDetails.targetduration * 3) {
return true;
}
}
return false;
}
class BasePlaylistController {
constructor(hls, logPrefix) {
this.hls = void 0;
this.timer = -1;
this.requestScheduled = -1;
this.canLoad = false;
this.log = void 0;
this.warn = void 0;
this.log = logger.log.bind(logger, `${logPrefix}:`);
this.warn = logger.warn.bind(logger, `${logPrefix}:`);
this.hls = hls;
}
destroy() {
this.clearTimer();
// @ts-ignore
this.hls = this.log = this.warn = null;
}
clearTimer() {
clearTimeout(this.timer);
this.timer = -1;
}
startLoad() {
this.canLoad = true;
this.requestScheduled = -1;
this.loadPlaylist();
}
stopLoad() {
this.canLoad = false;
this.clearTimer();
}
switchParams(playlistUri, previous) {
const renditionReports = previous == null ? void 0 : previous.renditionReports;
if (renditionReports) {
let foundIndex = -1;
for (let i = 0; i < renditionReports.length; i++) {
const attr = renditionReports[i];
let uri;
try {
uri = new self.URL(attr.URI, previous.url).href;
} catch (error) {
logger.warn(`Could not construct new URL for Rendition Report: ${error}`);
uri = attr.URI || '';
}
// Use exact match. Otherwise, the last partial match, if any, will be used
// (Playlist URI includes a query string that the Rendition Report does not)
if (uri === playlistUri) {
foundIndex = i;
break;
} else if (uri === playlistUri.substring(0, uri.length)) {
foundIndex = i;
}
}
if (foundIndex !== -1) {
const attr = renditionReports[foundIndex];
const msn = parseInt(attr['LAST-MSN']) || (previous == null ? void 0 : previous.lastPartSn);
let part = parseInt(attr['LAST-PART']) || (previous == null ? void 0 : previous.lastPartIndex);
if (this.hls.config.lowLatencyMode) {
const currentGoal = Math.min(previous.age - previous.partTarget, previous.targetduration);
if (part >= 0 && currentGoal > previous.partTarget) {
part += 1;
}
}
return new HlsUrlParameters(msn, part >= 0 ? part : undefined, HlsSkip.No);
}
}
}
loadPlaylist(hlsUrlParameters) {
if (this.requestScheduled === -1) {
this.requestScheduled = self.performance.now();
}
// Loading is handled by the subclasses
}
shouldLoadPlaylist(playlist) {
return this.canLoad && !!playlist && !!playlist.url && (!playlist.details || playlist.details.live);
}
shouldReloadPlaylist(playlist) {
return this.timer === -1 && this.requestScheduled === -1 && this.shouldLoadPlaylist(playlist);
}
playlistLoaded(index, data, previousDetails) {
const {
details,
stats
} = data;
// Set last updated date-time
const now = self.performance.now();
const elapsed = stats.loading.first ? Math.max(0, now - stats.loading.first) : 0;
details.advancedDateTime = Date.now() - elapsed;
// if current playlist is a live playlist, arm a timer to reload it
if (details.live || previousDetails != null && previousDetails.live) {
details.reloaded(previousDetails);
if (previousDetails) {
this.log(`live playlist ${index} ${details.advanced ? 'REFRESHED ' + details.lastPartSn + '-' + details.lastPartIndex : details.updated ? 'UPDATED' : 'MISSED'}`);
}
// Merge live playlists to adjust fragment starts and fill in delta playlist skipped segments
if (previousDetails && details.fragments.length > 0) {
mergeDetails(previousDetails, details);
}
if (!this.canLoad || !details.live) {
return;
}
let deliveryDirectives;
let msn = undefined;
let part = undefined;
if (details.canBlockReload && details.endSN && details.advanced) {
// Load level with LL-HLS delivery directives
const lowLatencyMode = this.hls.config.lowLatencyMode;
const lastPartSn = details.lastPartSn;
const endSn = details.endSN;
const lastPartIndex = details.lastPartIndex;
const hasParts = lastPartIndex !== -1;
const lastPart = lastPartSn === endSn;
// When low latency mode is disabled, we'll skip part requests once the last part index is found
const nextSnStartIndex = lowLatencyMode ? 0 : lastPartIndex;
if (hasParts) {
msn = lastPart ? endSn + 1 : lastPartSn;
part = lastPart ? nextSnStartIndex : lastPartIndex + 1;
} else {
msn = endSn + 1;
}
// Low-Latency CDN Tune-in: "age" header and time since load indicates we're behind by more than one part
// Update directives to obtain the Playlist that has the estimated additional duration of media
const lastAdvanced = details.age;
const cdnAge = lastAdvanced + details.ageHeader;
let currentGoal = Math.min(cdnAge - details.partTarget, details.targetduration * 1.5);
if (currentGoal > 0) {
if (previousDetails && currentGoal > previousDetails.tuneInGoal) {
// If we attempted to get the next or latest playlist update, but currentGoal increased,
// then we either can't catchup, or the "age" header cannot be trusted.
this.warn(`CDN Tune-in goal increased from: ${previousDetails.tuneInGoal} to: ${currentGoal} with playlist age: ${details.age}`);
currentGoal = 0;
} else {
const segments = Math.floor(currentGoal / details.targetduration);
msn += segments;
if (part !== undefined) {
const parts = Math.round(currentGoal % details.targetduration / details.partTarget);
part += parts;
}
this.log(`CDN Tune-in age: ${details.ageHeader}s last advanced ${lastAdvanced.toFixed(2)}s goal: ${currentGoal} skip sn ${segments} to part ${part}`);
}
details.tuneInGoal = currentGoal;
}
deliveryDirectives = this.getDeliveryDirectives(details, data.deliveryDirectives, msn, part);
if (lowLatencyMode || !lastPart) {
this.loadPlaylist(deliveryDirectives);
return;
}
} else if (details.canBlockReload || details.canSkipUntil) {
deliveryDirectives = this.getDeliveryDirectives(details, data.deliveryDirectives, msn, part);
}
const bufferInfo = this.hls.mainForwardBufferInfo;
const position = bufferInfo ? bufferInfo.end - bufferInfo.len : 0;
const distanceToLiveEdgeMs = (details.edge - position) * 1000;
const reloadInterval = computeReloadInterval(details, distanceToLiveEdgeMs);
if (details.updated && now > this.requestScheduled + reloadInterval) {
this.requestScheduled = stats.loading.start;
}
if (msn !== undefined && details.canBlockReload) {
this.requestScheduled = stats.loading.first + reloadInterval - (details.partTarget * 1000 || 1000);
} else if (this.requestScheduled === -1 || this.requestScheduled + reloadInterval < now) {
this.requestScheduled = now;
} else if (this.requestScheduled - now <= 0) {
this.requestScheduled += reloadInterval;
}
let estimatedTimeUntilUpdate = this.requestScheduled - now;
estimatedTimeUntilUpdate = Math.max(0, estimatedTimeUntilUpdate);
this.log(`reload live playlist ${index} in ${Math.round(estimatedTimeUntilUpdate)} ms`);
// this.log(
// `live reload ${details.updated ? 'REFRESHED' : 'MISSED'}
// reload in ${estimatedTimeUntilUpdate / 1000}
// round trip ${(stats.loading.end - stats.loading.start) / 1000}
// diff ${
// (reloadInterval -
// (estimatedTimeUntilUpdate +
// stats.loading.end -
// stats.loading.start)) /
// 1000
// }
// reload interval ${reloadInterval / 1000}
// target duration ${details.targetduration}
// distance to edge ${distanceToLiveEdgeMs / 1000}`
// );
this.timer = self.setTimeout(() => this.loadPlaylist(deliveryDirectives), estimatedTimeUntilUpdate);
} else {
this.clearTimer();
}
}
getDeliveryDirectives(details, previousDeliveryDirectives, msn, part) {
let skip = getSkipValue(details, msn);
if (previousDeliveryDirectives != null && previousDeliveryDirectives.skip && details.deltaUpdateFailed) {
msn = previousDeliveryDirectives.msn;
part = previousDeliveryDirectives.part;
skip = HlsSkip.No;
}
return new HlsUrlParameters(msn, part, skip);
}
checkRetry(errorEvent) {
const errorDetails = errorEvent.details;
const isTimeout = isTimeoutError(errorEvent);
const errorAction = errorEvent.errorAction;
const {
action,
retryCount = 0,
retryConfig
} = errorAction || {};
const retry = !!errorAction && !!retryConfig && (action === NetworkErrorAction.RetryRequest || !errorAction.resolved && action === NetworkErrorAction.SendAlternateToPenaltyBox);
if (retry) {
var _errorEvent$context;
this.requestScheduled = -1;
if (retryCount >= retryConfig.maxNumRetry) {
return false;
}
if (isTimeout && (_errorEvent$context = errorEvent.context) != null && _errorEvent$context.deliveryDirectives) {
// The LL-HLS request already timed out so retry immediately
this.warn(`Retrying playlist loading ${retryCount + 1}/${retryConfig.maxNumRetry} after "${errorDetails}" without delivery-directives`);
this.loadPlaylist();
} else {
const delay = getRetryDelay(retryConfig, retryCount);
// Schedule level/track reload
this.timer = self.setTimeout(() => this.loadPlaylist(), delay);
this.warn(`Retrying playlist loading ${retryCount + 1}/${retryConfig.maxNumRetry} after "${errorDetails}" in ${delay}ms`);
}
// `levelRetry = true` used to inform other controllers that a retry is happening
errorEvent.levelRetry = true;
errorAction.resolved = true;
}
return retry;
}
}
let chromeOrFirefox;
class LevelController extends BasePlaylistController {
constructor(hls, contentSteeringController) {
super(hls, '[level-controller]');
this._levels = [];
this._firstLevel = -1;
this._startLevel = void 0;
this.currentLevel = null;
this.currentLevelIndex = -1;
this.manualLevelIndex = -1;
this.steering = void 0;
this.onParsedComplete = void 0;
this.steering = contentSteeringController;
this._registerListeners();
}
_registerListeners() {
const {
hls
} = this;
hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.on(Events.MANIFEST_LOADED, this.onManifestLoaded, this);
hls.on(Events.LEVEL_LOADED, this.onLevelLoaded, this);
hls.on(Events.LEVELS_UPDATED, this.onLevelsUpdated, this);
hls.on(Events.AUDIO_TRACK_SWITCHED, this.onAudioTrackSwitched, this);
hls.on(Events.FRAG_LOADED, this.onFragLoaded, this);
hls.on(Events.ERROR, this.onError, this);
}
_unregisterListeners() {
const {
hls
} = this;
hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.off(Events.MANIFEST_LOADED, this.onManifestLoaded, this);
hls.off(Events.LEVEL_LOADED, this.onLevelLoaded, this);
hls.off(Events.LEVELS_UPDATED, this.onLevelsUpdated, this);
hls.off(Events.AUDIO_TRACK_SWITCHED, this.onAudioTrackSwitched, this);
hls.off(Events.FRAG_LOADED, this.onFragLoaded, this);
hls.off(Events.ERROR, this.onError, this);
}
destroy() {
this._unregisterListeners();
this.steering = null;
this.resetLevels();
super.destroy();
}
startLoad() {
const levels = this._levels;
// clean up live level details to force reload them, and reset load errors
levels.forEach(level => {
level.loadError = 0;
level.fragmentError = 0;
});
super.startLoad();
}
resetLevels() {
this._startLevel = undefined;
this.manualLevelIndex = -1;
this.currentLevelIndex = -1;
this.currentLevel = null;
this._levels = [];
}
onManifestLoading(event, data) {
this.resetLevels();
}
onManifestLoaded(event, data) {
const levels = [];
const levelSet = {};
let levelFromSet;
// regroup redundant levels together
data.levels.forEach(levelParsed => {
var _levelParsed$audioCod;
const attributes = levelParsed.attrs;
// erase audio codec info if browser does not support mp4a.40.34.
// demuxer will autodetect codec and fallback to mpeg/audio
if (((_levelParsed$audioCod = levelParsed.audioCodec) == null ? void 0 : _levelParsed$audioCod.indexOf('mp4a.40.34')) !== -1) {
chromeOrFirefox || (chromeOrFirefox = /chrome|firefox/i.test(navigator.userAgent));
if (chromeOrFirefox) {
levelParsed.audioCodec = undefined;
}
}
const {
AUDIO,
CODECS,
'FRAME-RATE': FRAMERATE,
'PATHWAY-ID': PATHWAY,
RESOLUTION,
SUBTITLES
} = attributes;
const contentSteeringPrefix = `${PATHWAY || '.'}-` ;
const levelKey = `${contentSteeringPrefix}${levelParsed.bitrate}-${RESOLUTION}-${FRAMERATE}-${CODECS}`;
levelFromSet = levelSet[levelKey];
if (!levelFromSet) {
levelFromSet = new Level(levelParsed);
levelSet[levelKey] = levelFromSet;
levels.push(levelFromSet);
} else {
levelFromSet.addFallback(levelParsed);
}
addGroupId(levelFromSet, 'audio', AUDIO);
addGroupId(levelFromSet, 'text', SUBTITLES);
});
this.filterAndSortMediaOptions(levels, data);
}
filterAndSortMediaOptions(unfilteredLevels, data) {
let audioTracks = [];
let subtitleTracks = [];
let resolutionFound = false;
let videoCodecFound = false;
let audioCodecFound = false;
// only keep levels with supported audio/video codecs
let levels = unfilteredLevels.filter(({
audioCodec,
videoCodec,
width,
height,
unknownCodecs
}) => {
resolutionFound || (resolutionFound = !!(width && height));
videoCodecFound || (videoCodecFound = !!videoCodec);
audioCodecFound || (audioCodecFound = !!audioCodec);
return !(unknownCodecs != null && unknownCodecs.length) && (!audioCodec || isCodecSupportedInMp4(audioCodec, 'audio')) && (!videoCodec || isCodecSupportedInMp4(videoCodec, 'video'));
});
// remove audio-only level if we also have levels with video codecs or RESOLUTION signalled
if ((resolutionFound || videoCodecFound) && audioCodecFound) {
levels = levels.filter(({
videoCodec,
width,
height
}) => !!videoCodec || !!(width && height));
}
if (levels.length === 0) {
// Dispatch error after MANIFEST_LOADED is done propagating
Promise.resolve().then(() => {
if (this.hls) {
const error = new Error('no level with compatible codecs found in manifest');
this.hls.trigger(Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.MANIFEST_INCOMPATIBLE_CODECS_ERROR,
fatal: true,
url: data.url,
error,
reason: error.message
});
}
});
return;
}
if (data.audioTracks) {
audioTracks = data.audioTracks.filter(track => !track.audioCodec || isCodecSupportedInMp4(track.audioCodec, 'audio'));
// Assign ids after filtering as array indices by group-id
assignTrackIdsByGroup(audioTracks);
}
if (data.subtitles) {
subtitleTracks = data.subtitles;
assignTrackIdsByGroup(subtitleTracks);
}
// start bitrate is the first bitrate of the manifest
const unsortedLevels = levels.slice(0);
// sort levels from lowest to highest
levels.sort((a, b) => {
if (a.attrs['HDCP-LEVEL'] !== b.attrs['HDCP-LEVEL']) {
return (a.attrs['HDCP-LEVEL'] || '') > (b.attrs['HDCP-LEVEL'] || '') ? 1 : -1;
}
if (a.bitrate !== b.bitrate) {
return a.bitrate - b.bitrate;
}
if (a.attrs['FRAME-RATE'] !== b.attrs['FRAME-RATE']) {
return a.attrs.decimalFloatingPoint('FRAME-RATE') - b.attrs.decimalFloatingPoint('FRAME-RATE');
}
if (a.attrs.SCORE !== b.attrs.SCORE) {
return a.attrs.decimalFloatingPoint('SCORE') - b.attrs.decimalFloatingPoint('SCORE');
}
if (resolutionFound && a.height !== b.height) {
return a.height - b.height;
}
return 0;
});
let firstLevelInPlaylist = unsortedLevels[0];
if (this.steering) {
levels = this.steering.filterParsedLevels(levels);
if (levels.length !== unsortedLevels.length) {
for (let i = 0; i < unsortedLevels.length; i++) {
if (unsortedLevels[i].pathwayId === levels[0].pathwayId) {
firstLevelInPlaylist = unsortedLevels[i];
break;
}
}
}
}
this._levels = levels;
// find index of first level in sorted levels
for (let i = 0; i < levels.length; i++) {
if (levels[i] === firstLevelInPlaylist) {
this._firstLevel = i;
this.log(`manifest loaded, ${levels.length} level(s) found, first bitrate: ${firstLevelInPlaylist.bitrate}`);
break;
}
}
// Audio is only alternate if manifest include a URI along with the audio group tag,
// and this is not an audio-only stream where levels contain audio-only
const audioOnly = audioCodecFound && !videoCodecFound;
const edata = {
levels,
audioTracks,
subtitleTracks,
sessionData: data.sessionData,
sessionKeys: data.sessionKeys,
firstLevel: this._firstLevel,
stats: data.stats,
audio: audioCodecFound,
video: videoCodecFound,
altAudio: !audioOnly && audioTracks.some(t => !!t.url)
};
this.hls.trigger(Events.MANIFEST_PARSED, edata);
// Initiate loading after all controllers have received MANIFEST_PARSED
if (this.hls.config.autoStartLoad || this.hls.forceStartLoad) {
this.hls.startLoad(this.hls.config.startPosition);
}
}
get levels() {
if (this._levels.length === 0) {
return null;
}
return this._levels;
}
get level() {
return this.currentLevelIndex;
}
set level(newLevel) {
const levels = this._levels;
if (levels.length === 0) {
return;
}
// check if level idx is valid
if (newLevel < 0 || newLevel >= levels.length) {
// invalid level id given, trigger error
const error = new Error('invalid level idx');
const fatal = newLevel < 0;
this.hls.trigger(Events.ERROR, {
type: ErrorTypes.OTHER_ERROR,
details: ErrorDetails.LEVEL_SWITCH_ERROR,
level: newLevel,
fatal,
error,
reason: error.message
});
if (fatal) {
return;
}
newLevel = Math.min(newLevel, levels.length - 1);
}
const lastLevelIndex = this.currentLevelIndex;
const lastLevel = this.currentLevel;
const lastPathwayId = lastLevel ? lastLevel.attrs['PATHWAY-ID'] : undefined;
const level = levels[newLevel];
const pathwayId = level.attrs['PATHWAY-ID'];
this.currentLevelIndex = newLevel;
this.currentLevel = level;
if (lastLevelIndex === newLevel && level.details && lastLevel && lastPathwayId === pathwayId) {
return;
}
this.log(`Switching to level ${newLevel}${pathwayId ? ' with Pathway ' + pathwayId : ''} from level ${lastLevelIndex}${lastPathwayId ? ' with Pathway ' + lastPathwayId : ''}`);
const levelSwitchingData = _extends({}, level, {
level: newLevel,
maxBitrate: level.maxBitrate,
attrs: level.attrs,
uri: level.uri,
urlId: level.urlId
});
// @ts-ignore
delete levelSwitchingData._attrs;
// @ts-ignore
delete levelSwitchingData._urlId;
this.hls.trigger(Events.LEVEL_SWITCHING, levelSwitchingData);
// check if we need to load playlist for this level
const levelDetails = level.details;
if (!levelDetails || levelDetails.live) {
// level not retrieved yet, or live playlist we need to (re)load it
const hlsUrlParameters = this.switchParams(level.uri, lastLevel == null ? void 0 : lastLevel.details);
this.loadPlaylist(hlsUrlParameters);
}
}
get manualLevel() {
return this.manualLevelIndex;
}
set manualLevel(newLevel) {
this.manualLevelIndex = newLevel;
if (this._startLevel === undefined) {
this._startLevel = newLevel;
}
if (newLevel !== -1) {
this.level = newLevel;
}
}
get firstLevel() {
return this._firstLevel;
}
set firstLevel(newLevel) {
this._firstLevel = newLevel;
}
get startLevel() {
// hls.startLevel takes precedence over config.startLevel
// if none of these values are defined, fallback on this._firstLevel (first quality level appearing in variant manifest)
if (this._startLevel === undefined) {
const configStartLevel = this.hls.config.startLevel;
if (configStartLevel !== undefined) {
return configStartLevel;
} else {
return this._firstLevel;
}
} else {
return this._startLevel;
}
}
set startLevel(newLevel) {
this._startLevel = newLevel;
}
onError(event, data) {
if (data.fatal || !data.context) {
return;
}
if (data.context.type === PlaylistContextType.LEVEL && data.context.level === this.level) {
this.checkRetry(data);
}
}
// reset errors on the successful load of a fragment
onFragLoaded(event, {
frag
}) {
if (frag !== undefined && frag.type === PlaylistLevelType.MAIN) {
const level = this._levels[frag.level];
if (level !== undefined) {
level.loadError = 0;
}
}
}
onLevelLoaded(event, data) {
var _data$deliveryDirecti2;
const {
level,
details
} = data;
const curLevel = this._levels[level];
if (!curLevel) {
var _data$deliveryDirecti;
this.warn(`Invalid level index ${level}`);
if ((_data$deliveryDirecti = data.deliveryDirectives) != null && _data$deliveryDirecti.skip) {
details.deltaUpdateFailed = true;
}
return;
}
// only process level loaded events matching with expected level
if (level === this.currentLevelIndex) {
// reset level load error counter on successful level loaded only if there is no issues with fragments
if (curLevel.fragmentError === 0) {
curLevel.loadError = 0;
}
this.playlistLoaded(level, data, curLevel.details);
} else if ((_data$deliveryDirecti2 = data.deliveryDirectives) != null && _data$deliveryDirecti2.skip) {
// received a delta playlist update that cannot be merged
details.deltaUpdateFailed = true;
}
}
onAudioTrackSwitched(event, data) {
const currentLevel = this.currentLevel;
if (!currentLevel) {
return;
}
const audioGroupId = this.hls.audioTracks[data.id].groupId;
if (currentLevel.audioGroupIds && currentLevel.audioGroupId !== audioGroupId) {
let urlId = -1;
for (let i = 0; i < currentLevel.audioGroupIds.length; i++) {
if (currentLevel.audioGroupIds[i] === audioGroupId) {
urlId = i;
break;
}
}
if (urlId !== -1 && urlId !== currentLevel.urlId) {
currentLevel.urlId = urlId;
if (this.canLoad) {
this.startLoad();
}
}
}
}
loadPlaylist(hlsUrlParameters) {
super.loadPlaylist();
const currentLevelIndex = this.currentLevelIndex;
const currentLevel = this.currentLevel;
if (currentLevel && this.shouldLoadPlaylist(currentLevel)) {
const id = currentLevel.urlId;
let url = currentLevel.uri;
if (hlsUrlParameters) {
try {
url = hlsUrlParameters.addDirectives(url);
} catch (error) {
this.warn(`Could not construct new URL with HLS Delivery Directives: ${error}`);
}
}
const pathwayId = currentLevel.attrs['PATHWAY-ID'];
this.log(`Loading level index ${currentLevelIndex}${(hlsUrlParameters == null ? void 0 : hlsUrlParameters.msn) !== undefined ? ' at sn ' + hlsUrlParameters.msn + ' part ' + hlsUrlParameters.part : ''} with${pathwayId ? ' Pathway ' + pathwayId : ''} URI ${id + 1}/${currentLevel.url.length} ${url}`);
// console.log('Current audio track group ID:', this.hls.audioTracks[this.hls.audioTrack].groupId);
// console.log('New video quality level audio group id:', levelObject.attrs.AUDIO, level);
this.clearTimer();
this.hls.trigger(Events.LEVEL_LOADING, {
url,
level: currentLevelIndex,
id,
deliveryDirectives: hlsUrlParameters || null
});
}
}
get nextLoadLevel() {
if (this.manualLevelIndex !== -1) {
return this.manualLevelIndex;
} else {
return this.hls.nextAutoLevel;
}
}
set nextLoadLevel(nextLevel) {
this.level = nextLevel;
if (this.manualLevelIndex === -1) {
this.hls.nextAutoLevel = nextLevel;
}
}
removeLevel(levelIndex, urlId) {
const filterLevelAndGroupByIdIndex = (url, id) => id !== urlId;
const levels = this._levels.filter((level, index) => {
if (index !== levelIndex) {
return true;
}
if (level.url.length > 1 && urlId !== undefined) {
level.url = level.url.filter(filterLevelAndGroupByIdIndex);
if (level.audioGroupIds) {
level.audioGroupIds = level.audioGroupIds.filter(filterLevelAndGroupByIdIndex);
}
if (level.textGroupIds) {
level.textGroupIds = level.textGroupIds.filter(filterLevelAndGroupByIdIndex);
}
level.urlId = 0;
return true;
}
if (this.steering) {
this.steering.removeLevel(level);
}
return false;
});
this.hls.trigger(Events.LEVELS_UPDATED, {
levels
});
}
onLevelsUpdated(event, {
levels
}) {
levels.forEach((level, index) => {
const {
details
} = level;
if (details != null && details.fragments) {
details.fragments.forEach(fragment => {
fragment.level = index;
});
}
});
this._levels = levels;
}
}
function addGroupId(level, type, id) {
if (!id) {
return;
}
if (type === 'audio') {
if (!level.audioGroupIds) {
level.audioGroupIds = [];
}
level.audioGroupIds[level.url.length - 1] = id;
} else if (type === 'text') {
if (!level.textGroupIds) {
level.textGroupIds = [];
}
level.textGroupIds[level.url.length - 1] = id;
}
}
function assignTrackIdsByGroup(tracks) {
const groups = {};
tracks.forEach(track => {
const groupId = track.groupId || '';
track.id = groups[groupId] = groups[groupId] || 0;
groups[groupId]++;
});
}
var FragmentState = {
NOT_LOADED: "NOT_LOADED",
APPENDING: "APPENDING",
PARTIAL: "PARTIAL",
OK: "OK"
};
class FragmentTracker {
constructor(hls) {
this.activePartLists = Object.create(null);
this.endListFragments = Object.create(null);
this.fragments = Object.create(null);
this.timeRanges = Object.create(null);
this.bufferPadding = 0.2;
this.hls = void 0;
this.hasGaps = false;
this.hls = hls;
this._registerListeners();
}
_registerListeners() {
const {
hls
} = this;
hls.on(Events.BUFFER_APPENDED, this.onBufferAppended, this);
hls.on(Events.FRAG_BUFFERED, this.onFragBuffered, this);
hls.on(Events.FRAG_LOADED, this.onFragLoaded, this);
}
_unregisterListeners() {
const {
hls
} = this;
hls.off(Events.BUFFER_APPENDED, this.onBufferAppended, this);
hls.off(Events.FRAG_BUFFERED, this.onFragBuffered, this);
hls.off(Events.FRAG_LOADED, this.onFragLoaded, this);
}
destroy() {
this._unregisterListeners();
// @ts-ignore
this.fragments =
// @ts-ignore
this.activePartLists =
// @ts-ignore
this.endListFragments = this.timeRanges = null;
}
/**
* Return a Fragment or Part with an appended range that matches the position and levelType
* Otherwise, return null
*/
getAppendedFrag(position, levelType) {
const activeParts = this.activePartLists[levelType];
if (activeParts) {
for (let i = activeParts.length; i--;) {
const activePart = activeParts[i];
if (!activePart) {
break;
}
const appendedPTS = activePart.end;
if (activePart.start <= position && appendedPTS !== null && position <= appendedPTS) {
return activePart;
}
}
}
return this.getBufferedFrag(position, levelType);
}
/**
* Return a buffered Fragment that matches the position and levelType.
* A buffered Fragment is one whose loading, parsing and appending is done (completed or "partial" meaning aborted).
* If not found any Fragment, return null
*/
getBufferedFrag(position, levelType) {
const {
fragments
} = this;
const keys = Object.keys(fragments);
for (let i = keys.length; i--;) {
const fragmentEntity = fragments[keys[i]];
if ((fragmentEntity == null ? void 0 : fragmentEntity.body.type) === levelType && fragmentEntity.buffered) {
const frag = fragmentEntity.body;
if (frag.start <= position && position <= frag.end) {
return frag;
}
}
}
return null;
}
/**
* Partial fragments effected by coded frame eviction will be removed
* The browser will unload parts of the buffer to free up memory for new buffer data
* Fragments will need to be reloaded when the buffer is freed up, removing partial fragments will allow them to reload(since there might be parts that are still playable)
*/
detectEvictedFragments(elementaryStream, timeRange, playlistType, appendedPart) {
if (this.timeRanges) {
this.timeRanges[elementaryStream] = timeRange;
}
// Check if any flagged fragments have been unloaded
// excluding anything newer than appendedPartSn
const appendedPartSn = (appendedPart == null ? void 0 : appendedPart.fragment.sn) || -1;
Object.keys(this.fragments).forEach(key => {
const fragmentEntity = this.fragments[key];
if (!fragmentEntity) {
return;
}
if (appendedPartSn >= fragmentEntity.body.sn) {
return;
}
if (!fragmentEntity.buffered && !fragmentEntity.loaded) {
if (fragmentEntity.body.type === playlistType) {
this.removeFragment(fragmentEntity.body);
}
return;
}
const esData = fragmentEntity.range[elementaryStream];
if (!esData) {
return;
}
esData.time.some(time => {
const isNotBuffered = !this.isTimeBuffered(time.startPTS, time.endPTS, timeRange);
if (isNotBuffered) {
// Unregister partial fragment as it needs to load again to be reused
this.removeFragment(fragmentEntity.body);
}
return isNotBuffered;
});
});
}
/**
* Checks if the fragment passed in is loaded in the buffer properly
* Partially loaded fragments will be registered as a partial fragment
*/
detectPartialFragments(data) {
const timeRanges = this.timeRanges;
const {
frag,
part
} = data;
if (!timeRanges || frag.sn === 'initSegment') {
return;
}
const fragKey = getFragmentKey(frag);
const fragmentEntity = this.fragments[fragKey];
if (!fragmentEntity || fragmentEntity.buffered && frag.gap) {
return;
}
const isFragHint = !frag.relurl;
Object.keys(timeRanges).forEach(elementaryStream => {
const streamInfo = frag.elementaryStreams[elementaryStream];
if (!streamInfo) {
return;
}
const timeRange = timeRanges[elementaryStream];
const partial = isFragHint || streamInfo.partial === true;
fragmentEntity.range[elementaryStream] = this.getBufferedTimes(frag, part, partial, timeRange);
});
fragmentEntity.loaded = null;
if (Object.keys(fragmentEntity.range).length) {
fragmentEntity.buffered = true;
const endList = fragmentEntity.body.endList = frag.endList || fragmentEntity.body.endList;
if (endList) {
this.endListFragments[fragmentEntity.body.type] = fragmentEntity;
}
if (!isPartial(fragmentEntity)) {
// Remove older fragment parts from lookup after frag is tracked as buffered
this.removeParts(frag.sn - 1, frag.type);
}
} else {
// remove fragment if nothing was appended
this.removeFragment(fragmentEntity.body);
}
}
removeParts(snToKeep, levelType) {
const activeParts = this.activePartLists[levelType];
if (!activeParts) {
return;
}
this.activePartLists[levelType] = activeParts.filter(part => part.fragment.sn >= snToKeep);
}
fragBuffered(frag, force) {
const fragKey = getFragmentKey(frag);
let fragmentEntity = this.fragments[fragKey];
if (!fragmentEntity && force) {
fragmentEntity = this.fragments[fragKey] = {
body: frag,
appendedPTS: null,
loaded: null,
buffered: false,
range: Object.create(null)
};
if (frag.gap) {
this.hasGaps = true;
}
}
if (fragmentEntity) {
fragmentEntity.loaded = null;
fragmentEntity.buffered = true;
}
}
getBufferedTimes(fragment, part, partial, timeRange) {
const buffered = {
time: [],
partial
};
const startPTS = fragment.start;
const endPTS = fragment.end;
const minEndPTS = fragment.minEndPTS || endPTS;
const maxStartPTS = fragment.maxStartPTS || startPTS;
for (let i = 0; i < timeRange.length; i++) {
const startTime = timeRange.start(i) - this.bufferPadding;
const endTime = timeRange.end(i) + this.bufferPadding;
if (maxStartPTS >= startTime && minEndPTS <= endTime) {
// Fragment is entirely contained in buffer
// No need to check the other timeRange times since it's completely playable
buffered.time.push({
startPTS: Math.max(startPTS, timeRange.start(i)),
endPTS: Math.min(endPTS, timeRange.end(i))
});
break;
} else if (startPTS < endTime && endPTS > startTime) {
buffered.partial = true;
// Check for intersection with buffer
// Get playable sections of the fragment
buffered.time.push({
startPTS: Math.max(startPTS, timeRange.start(i)),
endPTS: Math.min(endPTS, timeRange.end(i))
});
} else if (endPTS <= startTime) {
// No need to check the rest of the timeRange as it is in order
break;
}
}
return buffered;
}
/**
* Gets the partial fragment for a certain time
*/
getPartialFragment(time) {
let bestFragment = null;
let timePadding;
let startTime;
let endTime;
let bestOverlap = 0;
const {
bufferPadding,
fragments
} = this;
Object.keys(fragments).forEach(key => {
const fragmentEntity = fragments[key];
if (!fragmentEntity) {
return;
}
if (isPartial(fragmentEntity)) {
startTime = fragmentEntity.body.start - bufferPadding;
endTime = fragmentEntity.body.end + bufferPadding;
if (time >= startTime && time <= endTime) {
// Use the fragment that has the most padding from start and end time
timePadding = Math.min(time - startTime, endTime - time);
if (bestOverlap <= timePadding) {
bestFragment = fragmentEntity.body;
bestOverlap = timePadding;
}
}
}
});
return bestFragment;
}
isEndListAppended(type) {
const lastFragmentEntity = this.endListFragments[type];
return lastFragmentEntity !== undefined && (lastFragmentEntity.buffered || isPartial(lastFragmentEntity));
}
getState(fragment) {
const fragKey = getFragmentKey(fragment);
const fragmentEntity = this.fragments[fragKey];
if (fragmentEntity) {
if (!fragmentEntity.buffered) {
return FragmentState.APPENDING;
} else if (isPartial(fragmentEntity)) {
return FragmentState.PARTIAL;
} else {
return FragmentState.OK;
}
}
return FragmentState.NOT_LOADED;
}
isTimeBuffered(startPTS, endPTS, timeRange) {
let startTime;
let endTime;
for (let i = 0; i < timeRange.length; i++) {
startTime = timeRange.start(i) - this.bufferPadding;
endTime = timeRange.end(i) + this.bufferPadding;
if (startPTS >= startTime && endPTS <= endTime) {
return true;
}
if (endPTS <= startTime) {
// No need to check the rest of the timeRange as it is in order
return false;
}
}
return false;
}
onFragLoaded(event, data) {
const {
frag,
part
} = data;
// don't track initsegment (for which sn is not a number)
// don't track frags used for bitrateTest, they're irrelevant.
if (frag.sn === 'initSegment' || frag.bitrateTest) {
return;
}
// Fragment entity `loaded` FragLoadedData is null when loading parts
const loaded = part ? null : data;
const fragKey = getFragmentKey(frag);
this.fragments[fragKey] = {
body: frag,
appendedPTS: null,
loaded,
buffered: false,
range: Object.create(null)
};
}
onBufferAppended(event, data) {
const {
frag,
part,
timeRanges
} = data;
if (frag.sn === 'initSegment') {
return;
}
const playlistType = frag.type;
if (part) {
let activeParts = this.activePartLists[playlistType];
if (!activeParts) {
this.activePartLists[playlistType] = activeParts = [];
}
activeParts.push(part);
}
// Store the latest timeRanges loaded in the buffer
this.timeRanges = timeRanges;
Object.keys(timeRanges).forEach(elementaryStream => {
const timeRange = timeRanges[elementaryStream];
this.detectEvictedFragments(elementaryStream, timeRange, playlistType, part);
});
}
onFragBuffered(event, data) {
this.detectPartialFragments(data);
}
hasFragment(fragment) {
const fragKey = getFragmentKey(fragment);
return !!this.fragments[fragKey];
}
hasParts(type) {
var _this$activePartLists;
return !!((_this$activePartLists = this.activePartLists[type]) != null && _this$activePartLists.length);
}
removeFragmentsInRange(start, end, playlistType, withGapOnly, unbufferedOnly) {
if (withGapOnly && !this.hasGaps) {
return;
}
Object.keys(this.fragments).forEach(key => {
const fragmentEntity = this.fragments[key];
if (!fragmentEntity) {
return;
}
const frag = fragmentEntity.body;
if (frag.type !== playlistType || withGapOnly && !frag.gap) {
return;
}
if (frag.start < end && frag.end > start && (fragmentEntity.buffered || unbufferedOnly)) {
this.removeFragment(frag);
}
});
}
removeFragment(fragment) {
const fragKey = getFragmentKey(fragment);
fragment.stats.loaded = 0;
fragment.clearElementaryStreamInfo();
const activeParts = this.activePartLists[fragment.type];
if (activeParts) {
const snToRemove = fragment.sn;
this.activePartLists[fragment.type] = activeParts.filter(part => part.fragment.sn !== snToRemove);
}
delete this.fragments[fragKey];
if (fragment.endList) {
delete this.endListFragments[fragment.type];
}
}
removeAllFragments() {
this.fragments = Object.create(null);
this.endListFragments = Object.create(null);
this.activePartLists = Object.create(null);
this.hasGaps = false;
}
}
function isPartial(fragmentEntity) {
var _fragmentEntity$range, _fragmentEntity$range2, _fragmentEntity$range3;
return fragmentEntity.buffered && (fragmentEntity.body.gap || ((_fragmentEntity$range = fragmentEntity.range.video) == null ? void 0 : _fragmentEntity$range.partial) || ((_fragmentEntity$range2 = fragmentEntity.range.audio) == null ? void 0 : _fragmentEntity$range2.partial) || ((_fragmentEntity$range3 = fragmentEntity.range.audiovideo) == null ? void 0 : _fragmentEntity$range3.partial));
}
function getFragmentKey(fragment) {
return `${fragment.type}_${fragment.level}_${fragment.urlId}_${fragment.sn}`;
}
const MIN_CHUNK_SIZE = Math.pow(2, 17); // 128kb
class FragmentLoader {
constructor(config) {
this.config = void 0;
this.loader = null;
this.partLoadTimeout = -1;
this.config = config;
}
destroy() {
if (this.loader) {
this.loader.destroy();
this.loader = null;
}
}
abort() {
if (this.loader) {
// Abort the loader for current fragment. Only one may load at any given time
this.loader.abort();
}
}
load(frag, onProgress) {
const url = frag.url;
if (!url) {
return Promise.reject(new LoadError({
type: ErrorTypes.NETWORK_ERROR,
details: ErrorDetails.FRAG_LOAD_ERROR,
fatal: false,
frag,
error: new Error(`Fragment does not have a ${url ? 'part list' : 'url'}`),
networkDetails: null
}));
}
this.abort();
const config = this.config;
const FragmentILoader = config.fLoader;
const DefaultILoader = config.loader;
return new Promise((resolve, reject) => {
if (this.loader) {
this.loader.destroy();
}
if (frag.gap) {
if (frag.tagList.some(tags => tags[0] === 'GAP')) {
reject(createGapLoadError(frag));
return;
} else {
// Reset temporary treatment as GAP tag
frag.gap = false;
}
}
const loader = this.loader = frag.loader = FragmentILoader ? new FragmentILoader(config) : new DefaultILoader(config);
const loaderContext = createLoaderContext(frag);
const loadPolicy = getLoaderConfigWithoutReties(config.fragLoadPolicy.default);
const loaderConfig = {
loadPolicy,
timeout: loadPolicy.maxLoadTimeMs,
maxRetry: 0,
retryDelay: 0,
maxRetryDelay: 0,
highWaterMark: frag.sn === 'initSegment' ? Infinity : MIN_CHUNK_SIZE
};
// Assign frag stats to the loader's stats reference
frag.stats = loader.stats;
loader.load(loaderContext, loaderConfig, {
onSuccess: (response, stats, context, networkDetails) => {
this.resetLoader(frag, loader);
let payload = response.data;
if (context.resetIV && frag.decryptdata) {
frag.decryptdata.iv = new Uint8Array(payload.slice(0, 16));
payload = payload.slice(16);
}
resolve({
frag,
part: null,
payload,
networkDetails
});
},
onError: (response, context, networkDetails, stats) => {
this.resetLoader(frag, loader);
reject(new LoadError({
type: ErrorTypes.NETWORK_ERROR,
details: ErrorDetails.FRAG_LOAD_ERROR,
fatal: false,
frag,
response: _objectSpread2({
url,
data: undefined
}, response),
error: new Error(`HTTP Error ${response.code} ${response.text}`),
networkDetails,
stats
}));
},
onAbort: (stats, context, networkDetails) => {
this.resetLoader(frag, loader);
reject(new LoadError({
type: ErrorTypes.NETWORK_ERROR,
details: ErrorDetails.INTERNAL_ABORTED,
fatal: false,
frag,
error: new Error('Aborted'),
networkDetails,
stats
}));
},
onTimeout: (stats, context, networkDetails) => {
this.resetLoader(frag, loader);
reject(new LoadError({
type: ErrorTypes.NETWORK_ERROR,
details: ErrorDetails.FRAG_LOAD_TIMEOUT,
fatal: false,
frag,
error: new Error(`Timeout after ${loaderConfig.timeout}ms`),
networkDetails,
stats
}));
},
onProgress: (stats, context, data, networkDetails) => {
if (onProgress) {
onProgress({
frag,
part: null,
payload: data,
networkDetails
});
}
}
});
});
}
loadPart(frag, part, onProgress) {
this.abort();
const config = this.config;
const FragmentILoader = config.fLoader;
const DefaultILoader = config.loader;
return new Promise((resolve, reject) => {
if (this.loader) {
this.loader.destroy();
}
if (frag.gap || part.gap) {
reject(createGapLoadError(frag, part));
return;
}
const loader = this.loader = frag.loader = FragmentILoader ? new FragmentILoader(config) : new DefaultILoader(config);
const loaderContext = createLoaderContext(frag, part);
// Should we define another load policy for parts?
const loadPolicy = getLoaderConfigWithoutReties(config.fragLoadPolicy.default);
const loaderConfig = {
loadPolicy,
timeout: loadPolicy.maxLoadTimeMs,
maxRetry: 0,
retryDelay: 0,
maxRetryDelay: 0,
highWaterMark: MIN_CHUNK_SIZE
};
// Assign part stats to the loader's stats reference
part.stats = loader.stats;
loader.load(loaderContext, loaderConfig, {
onSuccess: (response, stats, context, networkDetails) => {
this.resetLoader(frag, loader);
this.updateStatsFromPart(frag, part);
const partLoadedData = {
frag,
part,
payload: response.data,
networkDetails
};
onProgress(partLoadedData);
resolve(partLoadedData);
},
onError: (response, context, networkDetails, stats) => {
this.resetLoader(frag, loader);
reject(new LoadError({
type: ErrorTypes.NETWORK_ERROR,
details: ErrorDetails.FRAG_LOAD_ERROR,
fatal: false,
frag,
part,
response: _objectSpread2({
url: loaderContext.url,
data: undefined
}, response),
error: new Error(`HTTP Error ${response.code} ${response.text}`),
networkDetails,
stats
}));
},
onAbort: (stats, context, networkDetails) => {
frag.stats.aborted = part.stats.aborted;
this.resetLoader(frag, loader);
reject(new LoadError({
type: ErrorTypes.NETWORK_ERROR,
details: ErrorDetails.INTERNAL_ABORTED,
fatal: false,
frag,
part,
error: new Error('Aborted'),
networkDetails,
stats
}));
},
onTimeout: (stats, context, networkDetails) => {
this.resetLoader(frag, loader);
reject(new LoadError({
type: ErrorTypes.NETWORK_ERROR,
details: ErrorDetails.FRAG_LOAD_TIMEOUT,
fatal: false,
frag,
part,
error: new Error(`Timeout after ${loaderConfig.timeout}ms`),
networkDetails,
stats
}));
}
});
});
}
updateStatsFromPart(frag, part) {
const fragStats = frag.stats;
const partStats = part.stats;
const partTotal = partStats.total;
fragStats.loaded += partStats.loaded;
if (partTotal) {
const estTotalParts = Math.round(frag.duration / part.duration);
const estLoadedParts = Math.min(Math.round(fragStats.loaded / partTotal), estTotalParts);
const estRemainingParts = estTotalParts - estLoadedParts;
const estRemainingBytes = estRemainingParts * Math.round(fragStats.loaded / estLoadedParts);
fragStats.total = fragStats.loaded + estRemainingBytes;
} else {
fragStats.total = Math.max(fragStats.loaded, fragStats.total);
}
const fragLoading = fragStats.loading;
const partLoading = partStats.loading;
if (fragLoading.start) {
// add to fragment loader latency
fragLoading.first += partLoading.first - partLoading.start;
} else {
fragLoading.start = partLoading.start;
fragLoading.first = partLoading.first;
}
fragLoading.end = partLoading.end;
}
resetLoader(frag, loader) {
frag.loader = null;
if (this.loader === loader) {
self.clearTimeout(this.partLoadTimeout);
this.loader = null;
}
loader.destroy();
}
}
function createLoaderContext(frag, part = null) {
const segment = part || frag;
const loaderContext = {
frag,
part,
responseType: 'arraybuffer',
url: segment.url,
headers: {},
rangeStart: 0,
rangeEnd: 0
};
const start = segment.byteRangeStartOffset;
const end = segment.byteRangeEndOffset;
if (isFiniteNumber(start) && isFiniteNumber(end)) {
var _frag$decryptdata;
let byteRangeStart = start;
let byteRangeEnd = end;
if (frag.sn === 'initSegment' && ((_frag$decryptdata = frag.decryptdata) == null ? void 0 : _frag$decryptdata.method) === 'AES-128') {
// MAP segment encrypted with method 'AES-128', when served with HTTP Range,
// has the unencrypted size specified in the range.
// Ref: https://tools.ietf.org/html/draft-pantos-hls-rfc8216bis-08#section-6.3.6
const fragmentLen = end - start;
if (fragmentLen % 16) {
byteRangeEnd = end + (16 - fragmentLen % 16);
}
if (start !== 0) {
loaderContext.resetIV = true;
byteRangeStart = start - 16;
}
}
loaderContext.rangeStart = byteRangeStart;
loaderContext.rangeEnd = byteRangeEnd;
}
return loaderContext;
}
function createGapLoadError(frag, part) {
const error = new Error(`GAP ${frag.gap ? 'tag' : 'attribute'} found`);
const errorData = {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.FRAG_GAP,
fatal: false,
frag,
error,
networkDetails: null
};
if (part) {
errorData.part = part;
}
(part ? part : frag).stats.aborted = true;
return new LoadError(errorData);
}
class LoadError extends Error {
constructor(data) {
super(data.error.message);
this.data = void 0;
this.data = data;
}
}
class KeyLoader {
constructor(config) {
this.config = void 0;
this.keyUriToKeyInfo = {};
this.emeController = null;
this.config = config;
}
abort(type) {
for (const uri in this.keyUriToKeyInfo) {
const loader = this.keyUriToKeyInfo[uri].loader;
if (loader) {
if (type && type !== loader.context.frag.type) {
return;
}
loader.abort();
}
}
}
detach() {
for (const uri in this.keyUriToKeyInfo) {
const keyInfo = this.keyUriToKeyInfo[uri];
// Remove cached EME keys on detach
if (keyInfo.mediaKeySessionContext || keyInfo.decryptdata.isCommonEncryption) {
delete this.keyUriToKeyInfo[uri];
}
}
}
destroy() {
this.detach();
for (const uri in this.keyUriToKeyInfo) {
const loader = this.keyUriToKeyInfo[uri].loader;
if (loader) {
loader.destroy();
}
}
this.keyUriToKeyInfo = {};
}
createKeyLoadError(frag, details = ErrorDetails.KEY_LOAD_ERROR, error, networkDetails, response) {
return new LoadError({
type: ErrorTypes.NETWORK_ERROR,
details,
fatal: false,
frag,
response,
error,
networkDetails
});
}
loadClear(loadingFrag, encryptedFragments) {
if (this.emeController && this.config.emeEnabled) {
// access key-system with nearest key on start (loaidng frag is unencrypted)
const {
sn,
cc
} = loadingFrag;
for (let i = 0; i < encryptedFragments.length; i++) {
const frag = encryptedFragments[i];
if (cc <= frag.cc && (sn === 'initSegment' || frag.sn === 'initSegment' || sn < frag.sn)) {
this.emeController.selectKeySystemFormat(frag).then(keySystemFormat => {
frag.setKeyFormat(keySystemFormat);
});
break;
}
}
}
}
load(frag) {
if (!frag.decryptdata && frag.encrypted && this.emeController) {
// Multiple keys, but none selected, resolve in eme-controller
return this.emeController.selectKeySystemFormat(frag).then(keySystemFormat => {
return this.loadInternal(frag, keySystemFormat);
});
}
return this.loadInternal(frag);
}
loadInternal(frag, keySystemFormat) {
var _keyInfo, _keyInfo2;
if (keySystemFormat) {
frag.setKeyFormat(keySystemFormat);
}
const decryptdata = frag.decryptdata;
if (!decryptdata) {
const error = new Error(keySystemFormat ? `Expected frag.decryptdata to be defined after setting format ${keySystemFormat}` : 'Missing decryption data on fragment in onKeyLoading');
return Promise.reject(this.createKeyLoadError(frag, ErrorDetails.KEY_LOAD_ERROR, error));
}
const uri = decryptdata.uri;
if (!uri) {
return Promise.reject(this.createKeyLoadError(frag, ErrorDetails.KEY_LOAD_ERROR, new Error(`Invalid key URI: "${uri}"`)));
}
let keyInfo = this.keyUriToKeyInfo[uri];
if ((_keyInfo = keyInfo) != null && _keyInfo.decryptdata.key) {
decryptdata.key = keyInfo.decryptdata.key;
return Promise.resolve({
frag,
keyInfo
});
}
// Return key load promise as long as it does not have a mediakey session with an unusable key status
if ((_keyInfo2 = keyInfo) != null && _keyInfo2.keyLoadPromise) {
var _keyInfo$mediaKeySess;
switch ((_keyInfo$mediaKeySess = keyInfo.mediaKeySessionContext) == null ? void 0 : _keyInfo$mediaKeySess.keyStatus) {
case undefined:
case 'status-pending':
case 'usable':
case 'usable-in-future':
return keyInfo.keyLoadPromise.then(keyLoadedData => {
// Return the correct fragment with updated decryptdata key and loaded keyInfo
decryptdata.key = keyLoadedData.keyInfo.decryptdata.key;
return {
frag,
keyInfo
};
});
}
// If we have a key session and status and it is not pending or usable, continue
// This will go back to the eme-controller for expired keys to get a new keyLoadPromise
}
// Load the key or return the loading promise
keyInfo = this.keyUriToKeyInfo[uri] = {
decryptdata,
keyLoadPromise: null,
loader: null,
mediaKeySessionContext: null
};
switch (decryptdata.method) {
case 'ISO-23001-7':
case 'SAMPLE-AES':
case 'SAMPLE-AES-CENC':
case 'SAMPLE-AES-CTR':
if (decryptdata.keyFormat === 'identity') {
// loadKeyHTTP handles http(s) and data URLs
return this.loadKeyHTTP(keyInfo, frag);
}
return this.loadKeyEME(keyInfo, frag);
case 'AES-128':
return this.loadKeyHTTP(keyInfo, frag);
default:
return Promise.reject(this.createKeyLoadError(frag, ErrorDetails.KEY_LOAD_ERROR, new Error(`Key supplied with unsupported METHOD: "${decryptdata.method}"`)));
}
}
loadKeyEME(keyInfo, frag) {
const keyLoadedData = {
frag,
keyInfo
};
if (this.emeController && this.config.emeEnabled) {
const keySessionContextPromise = this.emeController.loadKey(keyLoadedData);
if (keySessionContextPromise) {
return (keyInfo.keyLoadPromise = keySessionContextPromise.then(keySessionContext => {
keyInfo.mediaKeySessionContext = keySessionContext;
return keyLoadedData;
})).catch(error => {
// Remove promise for license renewal or retry
keyInfo.keyLoadPromise = null;
throw error;
});
}
}
return Promise.resolve(keyLoadedData);
}
loadKeyHTTP(keyInfo, frag) {
const config = this.config;
const Loader = config.loader;
const keyLoader = new Loader(config);
frag.keyLoader = keyInfo.loader = keyLoader;
return keyInfo.keyLoadPromise = new Promise((resolve, reject) => {
const loaderContext = {
keyInfo,
frag,
responseType: 'arraybuffer',
url: keyInfo.decryptdata.uri
};
// maxRetry is 0 so that instead of retrying the same key on the same variant multiple times,
// key-loader will trigger an error and rely on stream-controller to handle retry logic.
// this will also align retry logic with fragment-loader
const loadPolicy = config.keyLoadPolicy.default;
const loaderConfig = {
loadPolicy,
timeout: loadPolicy.maxLoadTimeMs,
maxRetry: 0,
retryDelay: 0,
maxRetryDelay: 0
};
const loaderCallbacks = {
onSuccess: (response, stats, context, networkDetails) => {
const {
frag,
keyInfo,
url: uri
} = context;
if (!frag.decryptdata || keyInfo !== this.keyUriToKeyInfo[uri]) {
return reject(this.createKeyLoadError(frag, ErrorDetails.KEY_LOAD_ERROR, new Error('after key load, decryptdata unset or changed'), networkDetails));
}
keyInfo.decryptdata.key = frag.decryptdata.key = new Uint8Array(response.data);
// detach fragment key loader on load success
frag.keyLoader = null;
keyInfo.loader = null;
resolve({
frag,
keyInfo
});
},
onError: (response, context, networkDetails, stats) => {
this.resetLoader(context);
reject(this.createKeyLoadError(frag, ErrorDetails.KEY_LOAD_ERROR, new Error(`HTTP Error ${response.code} loading key ${response.text}`), networkDetails, _objectSpread2({
url: loaderContext.url,
data: undefined
}, response)));
},
onTimeout: (stats, context, networkDetails) => {
this.resetLoader(context);
reject(this.createKeyLoadError(frag, ErrorDetails.KEY_LOAD_TIMEOUT, new Error('key loading timed out'), networkDetails));
},
onAbort: (stats, context, networkDetails) => {
this.resetLoader(context);
reject(this.createKeyLoadError(frag, ErrorDetails.INTERNAL_ABORTED, new Error('key loading aborted'), networkDetails));
}
};
keyLoader.load(loaderContext, loaderConfig, loaderCallbacks);
});
}
resetLoader(context) {
const {
frag,
keyInfo,
url: uri
} = context;
const loader = keyInfo.loader;
if (frag.keyLoader === loader) {
frag.keyLoader = null;
keyInfo.loader = null;
}
delete this.keyUriToKeyInfo[uri];
if (loader) {
loader.destroy();
}
}
}
/**
* @ignore
* Sub-class specialization of EventHandler base class.
*
* TaskLoop allows to schedule a task function being called (optionnaly repeatedly) on the main loop,
* scheduled asynchroneously, avoiding recursive calls in the same tick.
*
* The task itself is implemented in `doTick`. It can be requested and called for single execution
* using the `tick` method.
*
* It will be assured that the task execution method (`tick`) only gets called once per main loop "tick",
* no matter how often it gets requested for execution. Execution in further ticks will be scheduled accordingly.
*
* If further execution requests have already been scheduled on the next tick, it can be checked with `hasNextTick`,
* and cancelled with `clearNextTick`.
*
* The task can be scheduled as an interval repeatedly with a period as parameter (see `setInterval`, `clearInterval`).
*
* Sub-classes need to implement the `doTick` method which will effectively have the task execution routine.
*
* Further explanations:
*
* The baseclass has a `tick` method that will schedule the doTick call. It may be called synchroneously
* only for a stack-depth of one. On re-entrant calls, sub-sequent calls are scheduled for next main loop ticks.
*
* When the task execution (`tick` method) is called in re-entrant way this is detected and
* we are limiting the task execution per call stack to exactly one, but scheduling/post-poning further
* task processing on the next main loop iteration (also known as "next tick" in the Node/JS runtime lingo).
*/
class TaskLoop {
constructor() {
this._boundTick = void 0;
this._tickTimer = null;
this._tickInterval = null;
this._tickCallCount = 0;
this._boundTick = this.tick.bind(this);
}
destroy() {
this.onHandlerDestroying();
this.onHandlerDestroyed();
}
onHandlerDestroying() {
// clear all timers before unregistering from event bus
this.clearNextTick();
this.clearInterval();
}
onHandlerDestroyed() {}
hasInterval() {
return !!this._tickInterval;
}
hasNextTick() {
return !!this._tickTimer;
}
/**
* @param millis - Interval time (ms)
* @eturns True when interval has been scheduled, false when already scheduled (no effect)
*/
setInterval(millis) {
if (!this._tickInterval) {
this._tickCallCount = 0;
this._tickInterval = self.setInterval(this._boundTick, millis);
return true;
}
return false;
}
/**
* @returns True when interval was cleared, false when none was set (no effect)
*/
clearInterval() {
if (this._tickInterval) {
self.clearInterval(this._tickInterval);
this._tickInterval = null;
return true;
}
return false;
}
/**
* @returns True when timeout was cleared, false when none was set (no effect)
*/
clearNextTick() {
if (this._tickTimer) {
self.clearTimeout(this._tickTimer);
this._tickTimer = null;
return true;
}
return false;
}
/**
* Will call the subclass doTick implementation in this main loop tick
* or in the next one (via setTimeout(,0)) in case it has already been called
* in this tick (in case this is a re-entrant call).
*/
tick() {
this._tickCallCount++;
if (this._tickCallCount === 1) {
this.doTick();
// re-entrant call to tick from previous doTick call stack
// -> schedule a call on the next main loop iteration to process this task processing request
if (this._tickCallCount > 1) {
// make sure only one timer exists at any time at max
this.tickImmediate();
}
this._tickCallCount = 0;
}
}
tickImmediate() {
this.clearNextTick();
this._tickTimer = self.setTimeout(this._boundTick, 0);
}
/**
* For subclass to implement task logic
* @abstract
*/
doTick() {}
}
/**
* Provides methods dealing with buffer length retrieval for example.
*
* In general, a helper around HTML5 MediaElement TimeRanges gathered from `buffered` property.
*
* Also @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/buffered
*/
const noopBuffered = {
length: 0,
start: () => 0,
end: () => 0
};
class BufferHelper {
/**
* Return true if `media`'s buffered include `position`
*/
static isBuffered(media, position) {
try {
if (media) {
const buffered = BufferHelper.getBuffered(media);
for (let i = 0; i < buffered.length; i++) {
if (position >= buffered.start(i) && position <= buffered.end(i)) {
return true;
}
}
}
} catch (error) {
// this is to catch
// InvalidStateError: Failed to read the 'buffered' property from 'SourceBuffer':
// This SourceBuffer has been removed from the parent media source
}
return false;
}
static bufferInfo(media, pos, maxHoleDuration) {
try {
if (media) {
const vbuffered = BufferHelper.getBuffered(media);
const buffered = [];
let i;
for (i = 0; i < vbuffered.length; i++) {
buffered.push({
start: vbuffered.start(i),
end: vbuffered.end(i)
});
}
return this.bufferedInfo(buffered, pos, maxHoleDuration);
}
} catch (error) {
// this is to catch
// InvalidStateError: Failed to read the 'buffered' property from 'SourceBuffer':
// This SourceBuffer has been removed from the parent media source
}
return {
len: 0,
start: pos,
end: pos,
nextStart: undefined
};
}
static bufferedInfo(buffered, pos, maxHoleDuration) {
pos = Math.max(0, pos);
// sort on buffer.start/smaller end (IE does not always return sorted buffered range)
buffered.sort(function (a, b) {
const diff = a.start - b.start;
if (diff) {
return diff;
} else {
return b.end - a.end;
}
});
let buffered2 = [];
if (maxHoleDuration) {
// there might be some small holes between buffer time range
// consider that holes smaller than maxHoleDuration are irrelevant and build another
// buffer time range representations that discards those holes
for (let i = 0; i < buffered.length; i++) {
const buf2len = buffered2.length;
if (buf2len) {
const buf2end = buffered2[buf2len - 1].end;
// if small hole (value between 0 or maxHoleDuration ) or overlapping (negative)
if (buffered[i].start - buf2end < maxHoleDuration) {
// merge overlapping time ranges
// update lastRange.end only if smaller than item.end
// e.g. [ 1, 15] with [ 2,8] => [ 1,15] (no need to modify lastRange.end)
// whereas [ 1, 8] with [ 2,15] => [ 1,15] ( lastRange should switch from [1,8] to [1,15])
if (buffered[i].end > buf2end) {
buffered2[buf2len - 1].end = buffered[i].end;
}
} else {
// big hole
buffered2.push(buffered[i]);
}
} else {
// first value
buffered2.push(buffered[i]);
}
}
} else {
buffered2 = buffered;
}
let bufferLen = 0;
// bufferStartNext can possibly be undefined based on the conditional logic below
let bufferStartNext;
// bufferStart and bufferEnd are buffer boundaries around current video position
let bufferStart = pos;
let bufferEnd = pos;
for (let i = 0; i < buffered2.length; i++) {
const start = buffered2[i].start;
const end = buffered2[i].end;
// logger.log('buf start/end:' + buffered.start(i) + '/' + buffered.end(i));
if (pos + maxHoleDuration >= start && pos < end) {
// play position is inside this buffer TimeRange, retrieve end of buffer position and buffer length
bufferStart = start;
bufferEnd = end;
bufferLen = bufferEnd - pos;
} else if (pos + maxHoleDuration < start) {
bufferStartNext = start;
break;
}
}
return {
len: bufferLen,
start: bufferStart || 0,
end: bufferEnd || 0,
nextStart: bufferStartNext
};
}
/**
* Safe method to get buffered property.
* SourceBuffer.buffered may throw if SourceBuffer is removed from it's MediaSource
*/
static getBuffered(media) {
try {
return media.buffered;
} catch (e) {
logger.log('failed to get media.buffered', e);
return noopBuffered;
}
}
}
class ChunkMetadata {
constructor(level, sn, id, size = 0, part = -1, partial = false) {
this.level = void 0;
this.sn = void 0;
this.part = void 0;
this.id = void 0;
this.size = void 0;
this.partial = void 0;
this.transmuxing = getNewPerformanceTiming();
this.buffering = {
audio: getNewPerformanceTiming(),
video: getNewPerformanceTiming(),
audiovideo: getNewPerformanceTiming()
};
this.level = level;
this.sn = sn;
this.id = id;
this.size = size;
this.part = part;
this.partial = partial;
}
}
function getNewPerformanceTiming() {
return {
start: 0,
executeStart: 0,
executeEnd: 0,
end: 0
};
}
function findFirstFragWithCC(fragments, cc) {
let firstFrag = null;
for (let i = 0, len = fragments.length; i < len; i++) {
const currentFrag = fragments[i];
if (currentFrag && currentFrag.cc === cc) {
firstFrag = currentFrag;
break;
}
}
return firstFrag;
}
function shouldAlignOnDiscontinuities(lastFrag, lastLevel, details) {
if (lastLevel.details) {
if (details.endCC > details.startCC || lastFrag && lastFrag.cc < details.startCC) {
return true;
}
}
return false;
}
// Find the first frag in the previous level which matches the CC of the first frag of the new level
function findDiscontinuousReferenceFrag(prevDetails, curDetails, referenceIndex = 0) {
const prevFrags = prevDetails.fragments;
const curFrags = curDetails.fragments;
if (!curFrags.length || !prevFrags.length) {
logger.log('No fragments to align');
return;
}
const prevStartFrag = findFirstFragWithCC(prevFrags, curFrags[0].cc);
if (!prevStartFrag || prevStartFrag && !prevStartFrag.startPTS) {
logger.log('No frag in previous level to align on');
return;
}
return prevStartFrag;
}
function adjustFragmentStart(frag, sliding) {
if (frag) {
const start = frag.start + sliding;
frag.start = frag.startPTS = start;
frag.endPTS = start + frag.duration;
}
}
function adjustSlidingStart(sliding, details) {
// Update segments
const fragments = details.fragments;
for (let i = 0, len = fragments.length; i < len; i++) {
adjustFragmentStart(fragments[i], sliding);
}
// Update LL-HLS parts at the end of the playlist
if (details.fragmentHint) {
adjustFragmentStart(details.fragmentHint, sliding);
}
details.alignedSliding = true;
}
/**
* Using the parameters of the last level, this function computes PTS' of the new fragments so that they form a
* contiguous stream with the last fragments.
* The PTS of a fragment lets Hls.js know where it fits into a stream - by knowing every PTS, we know which fragment to
* download at any given time. PTS is normally computed when the fragment is demuxed, so taking this step saves us time
* and an extra download.
* @param lastFrag
* @param lastLevel
* @param details
*/
function alignStream(lastFrag, lastLevel, details) {
if (!lastLevel) {
return;
}
alignDiscontinuities(lastFrag, details, lastLevel);
if (!details.alignedSliding && lastLevel.details) {
// If the PTS wasn't figured out via discontinuity sequence that means there was no CC increase within the level.
// Aligning via Program Date Time should therefore be reliable, since PDT should be the same within the same
// discontinuity sequence.
alignPDT(details, lastLevel.details);
}
if (!details.alignedSliding && lastLevel.details && !details.skippedSegments) {
// Try to align on sn so that we pick a better start fragment.
// Do not perform this on playlists with delta updates as this is only to align levels on switch
// and adjustSliding only adjusts fragments after skippedSegments.
adjustSliding(lastLevel.details, details);
}
}
/**
* Computes the PTS if a new level's fragments using the PTS of a fragment in the last level which shares the same
* discontinuity sequence.
* @param lastFrag - The last Fragment which shares the same discontinuity sequence
* @param lastLevel - The details of the last loaded level
* @param details - The details of the new level
*/
function alignDiscontinuities(lastFrag, details, lastLevel) {
if (shouldAlignOnDiscontinuities(lastFrag, lastLevel, details)) {
const referenceFrag = findDiscontinuousReferenceFrag(lastLevel.details, details);
if (referenceFrag && isFiniteNumber(referenceFrag.start)) {
logger.log(`Adjusting PTS using last level due to CC increase within current level ${details.url}`);
adjustSlidingStart(referenceFrag.start, details);
}
}
}
/**
* Computes the PTS of a new level's fragments using the difference in Program Date Time from the last level.
* @param details - The details of the new level
* @param lastDetails - The details of the last loaded level
*/
function alignPDT(details, lastDetails) {
// This check protects the unsafe "!" usage below for null program date time access.
if (!lastDetails.fragments.length || !details.hasProgramDateTime || !lastDetails.hasProgramDateTime) {
return;
}
// if last level sliding is 1000 and its first frag PROGRAM-DATE-TIME is 2017-08-20 1:10:00 AM
// and if new details first frag PROGRAM DATE-TIME is 2017-08-20 1:10:08 AM
// then we can deduce that playlist B sliding is 1000+8 = 1008s
const lastPDT = lastDetails.fragments[0].programDateTime; // hasProgramDateTime check above makes this safe.
const newPDT = details.fragments[0].programDateTime;
// date diff is in ms. frag.start is in seconds
const sliding = (newPDT - lastPDT) / 1000 + lastDetails.fragments[0].start;
if (sliding && isFiniteNumber(sliding)) {
logger.log(`Adjusting PTS using programDateTime delta ${newPDT - lastPDT}ms, sliding:${sliding.toFixed(3)} ${details.url} `);
adjustSlidingStart(sliding, details);
}
}
/**
* Ensures appropriate time-alignment between renditions based on PDT. Unlike `alignPDT`, which adjusts
* the timeline based on the delta between PDTs of the 0th fragment of two playlists/`LevelDetails`,
* this function assumes the timelines represented in `refDetails` are accurate, including the PDTs,
* and uses the "wallclock"/PDT timeline as a cross-reference to `details`, adjusting the presentation
* times/timelines of `details` accordingly.
* Given the asynchronous nature of fetches and initial loads of live `main` and audio/subtitle tracks,
* the primary purpose of this function is to ensure the "local timelines" of audio/subtitle tracks
* are aligned to the main/video timeline, using PDT as the cross-reference/"anchor" that should
* be consistent across playlists, per the HLS spec.
* @param details - The details of the rendition you'd like to time-align (e.g. an audio rendition).
* @param refDetails - The details of the reference rendition with start and PDT times for alignment.
*/
function alignMediaPlaylistByPDT(details, refDetails) {
if (!details.hasProgramDateTime || !refDetails.hasProgramDateTime) {
return;
}
const fragments = details.fragments;
const refFragments = refDetails.fragments;
if (!fragments.length || !refFragments.length) {
return;
}
// Calculate a delta to apply to all fragments according to the delta in PDT times and start times
// of a fragment in the reference details, and a fragment in the target details of the same discontinuity.
// If a fragment of the same discontinuity was not found use the middle fragment of both.
const middleFrag = Math.round(refFragments.length / 2) - 1;
const refFrag = refFragments[middleFrag];
const frag = findFirstFragWithCC(fragments, refFrag.cc) || fragments[Math.round(fragments.length / 2) - 1];
const refPDT = refFrag.programDateTime;
const targetPDT = frag.programDateTime;
if (refPDT === null || targetPDT === null) {
return;
}
const delta = (targetPDT - refPDT) / 1000 - (frag.start - refFrag.start);
adjustSlidingStart(delta, details);
}
class AESCrypto {
constructor(subtle, iv) {
this.subtle = void 0;
this.aesIV = void 0;
this.subtle = subtle;
this.aesIV = iv;
}
decrypt(data, key) {
return this.subtle.decrypt({
name: 'AES-CBC',
iv: this.aesIV
}, key, data);
}
}
class FastAESKey {
constructor(subtle, key) {
this.subtle = void 0;
this.key = void 0;
this.subtle = subtle;
this.key = key;
}
expandKey() {
return this.subtle.importKey('raw', this.key, {
name: 'AES-CBC'
}, false, ['encrypt', 'decrypt']);
}
}
// PKCS7
function removePadding(array) {
const outputBytes = array.byteLength;
const paddingBytes = outputBytes && new DataView(array.buffer).getUint8(outputBytes - 1);
if (paddingBytes) {
return sliceUint8(array, 0, outputBytes - paddingBytes);
}
return array;
}
class AESDecryptor {
constructor() {
this.rcon = [0x0, 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];
this.subMix = [new Uint32Array(256), new Uint32Array(256), new Uint32Array(256), new Uint32Array(256)];
this.invSubMix = [new Uint32Array(256), new Uint32Array(256), new Uint32Array(256), new Uint32Array(256)];
this.sBox = new Uint32Array(256);
this.invSBox = new Uint32Array(256);
this.key = new Uint32Array(0);
this.ksRows = 0;
this.keySize = 0;
this.keySchedule = void 0;
this.invKeySchedule = void 0;
this.initTable();
}
// Using view.getUint32() also swaps the byte order.
uint8ArrayToUint32Array_(arrayBuffer) {
const view = new DataView(arrayBuffer);
const newArray = new Uint32Array(4);
for (let i = 0; i < 4; i++) {
newArray[i] = view.getUint32(i * 4);
}
return newArray;
}
initTable() {
const sBox = this.sBox;
const invSBox = this.invSBox;
const subMix = this.subMix;
const subMix0 = subMix[0];
const subMix1 = subMix[1];
const subMix2 = subMix[2];
const subMix3 = subMix[3];
const invSubMix = this.invSubMix;
const invSubMix0 = invSubMix[0];
const invSubMix1 = invSubMix[1];
const invSubMix2 = invSubMix[2];
const invSubMix3 = invSubMix[3];
const d = new Uint32Array(256);
let x = 0;
let xi = 0;
let i = 0;
for (i = 0; i < 256; i++) {
if (i < 128) {
d[i] = i << 1;
} else {
d[i] = i << 1 ^ 0x11b;
}
}
for (i = 0; i < 256; i++) {
let sx = xi ^ xi << 1 ^ xi << 2 ^ xi << 3 ^ xi << 4;
sx = sx >>> 8 ^ sx & 0xff ^ 0x63;
sBox[x] = sx;
invSBox[sx] = x;
// Compute multiplication
const x2 = d[x];
const x4 = d[x2];
const x8 = d[x4];
// Compute sub/invSub bytes, mix columns tables
let t = d[sx] * 0x101 ^ sx * 0x1010100;
subMix0[x] = t << 24 | t >>> 8;
subMix1[x] = t << 16 | t >>> 16;
subMix2[x] = t << 8 | t >>> 24;
subMix3[x] = t;
// Compute inv sub bytes, inv mix columns tables
t = x8 * 0x1010101 ^ x4 * 0x10001 ^ x2 * 0x101 ^ x * 0x1010100;
invSubMix0[sx] = t << 24 | t >>> 8;
invSubMix1[sx] = t << 16 | t >>> 16;
invSubMix2[sx] = t << 8 | t >>> 24;
invSubMix3[sx] = t;
// Compute next counter
if (!x) {
x = xi = 1;
} else {
x = x2 ^ d[d[d[x8 ^ x2]]];
xi ^= d[d[xi]];
}
}
}
expandKey(keyBuffer) {
// convert keyBuffer to Uint32Array
const key = this.uint8ArrayToUint32Array_(keyBuffer);
let sameKey = true;
let offset = 0;
while (offset < key.length && sameKey) {
sameKey = key[offset] === this.key[offset];
offset++;
}
if (sameKey) {
return;
}
this.key = key;
const keySize = this.keySize = key.length;
if (keySize !== 4 && keySize !== 6 && keySize !== 8) {
throw new Error('Invalid aes key size=' + keySize);
}
const ksRows = this.ksRows = (keySize + 6 + 1) * 4;
let ksRow;
let invKsRow;
const keySchedule = this.keySchedule = new Uint32Array(ksRows);
const invKeySchedule = this.invKeySchedule = new Uint32Array(ksRows);
const sbox = this.sBox;
const rcon = this.rcon;
const invSubMix = this.invSubMix;
const invSubMix0 = invSubMix[0];
const invSubMix1 = invSubMix[1];
const invSubMix2 = invSubMix[2];
const invSubMix3 = invSubMix[3];
let prev;
let t;
for (ksRow = 0; ksRow < ksRows; ksRow++) {
if (ksRow < keySize) {
prev = keySchedule[ksRow] = key[ksRow];
continue;
}
t = prev;
if (ksRow % keySize === 0) {
// Rot word
t = t << 8 | t >>> 24;
// Sub word
t = sbox[t >>> 24] << 24 | sbox[t >>> 16 & 0xff] << 16 | sbox[t >>> 8 & 0xff] << 8 | sbox[t & 0xff];
// Mix Rcon
t ^= rcon[ksRow / keySize | 0] << 24;
} else if (keySize > 6 && ksRow % keySize === 4) {
// Sub word
t = sbox[t >>> 24] << 24 | sbox[t >>> 16 & 0xff] << 16 | sbox[t >>> 8 & 0xff] << 8 | sbox[t & 0xff];
}
keySchedule[ksRow] = prev = (keySchedule[ksRow - keySize] ^ t) >>> 0;
}
for (invKsRow = 0; invKsRow < ksRows; invKsRow++) {
ksRow = ksRows - invKsRow;
if (invKsRow & 3) {
t = keySchedule[ksRow];
} else {
t = keySchedule[ksRow - 4];
}
if (invKsRow < 4 || ksRow <= 4) {
invKeySchedule[invKsRow] = t;
} else {
invKeySchedule[invKsRow] = invSubMix0[sbox[t >>> 24]] ^ invSubMix1[sbox[t >>> 16 & 0xff]] ^ invSubMix2[sbox[t >>> 8 & 0xff]] ^ invSubMix3[sbox[t & 0xff]];
}
invKeySchedule[invKsRow] = invKeySchedule[invKsRow] >>> 0;
}
}
// Adding this as a method greatly improves performance.
networkToHostOrderSwap(word) {
return word << 24 | (word & 0xff00) << 8 | (word & 0xff0000) >> 8 | word >>> 24;
}
decrypt(inputArrayBuffer, offset, aesIV) {
const nRounds = this.keySize + 6;
const invKeySchedule = this.invKeySchedule;
const invSBOX = this.invSBox;
const invSubMix = this.invSubMix;
const invSubMix0 = invSubMix[0];
const invSubMix1 = invSubMix[1];
const invSubMix2 = invSubMix[2];
const invSubMix3 = invSubMix[3];
const initVector = this.uint8ArrayToUint32Array_(aesIV);
let initVector0 = initVector[0];
let initVector1 = initVector[1];
let initVector2 = initVector[2];
let initVector3 = initVector[3];
const inputInt32 = new Int32Array(inputArrayBuffer);
const outputInt32 = new Int32Array(inputInt32.length);
let t0, t1, t2, t3;
let s0, s1, s2, s3;
let inputWords0, inputWords1, inputWords2, inputWords3;
let ksRow, i;
const swapWord = this.networkToHostOrderSwap;
while (offset < inputInt32.length) {
inputWords0 = swapWord(inputInt32[offset]);
inputWords1 = swapWord(inputInt32[offset + 1]);
inputWords2 = swapWord(inputInt32[offset + 2]);
inputWords3 = swapWord(inputInt32[offset + 3]);
s0 = inputWords0 ^ invKeySchedule[0];
s1 = inputWords3 ^ invKeySchedule[1];
s2 = inputWords2 ^ invKeySchedule[2];
s3 = inputWords1 ^ invKeySchedule[3];
ksRow = 4;
// Iterate through the rounds of decryption
for (i = 1; i < nRounds; i++) {
t0 = invSubMix0[s0 >>> 24] ^ invSubMix1[s1 >> 16 & 0xff] ^ invSubMix2[s2 >> 8 & 0xff] ^ invSubMix3[s3 & 0xff] ^ invKeySchedule[ksRow];
t1 = invSubMix0[s1 >>> 24] ^ invSubMix1[s2 >> 16 & 0xff] ^ invSubMix2[s3 >> 8 & 0xff] ^ invSubMix3[s0 & 0xff] ^ invKeySchedule[ksRow + 1];
t2 = invSubMix0[s2 >>> 24] ^ invSubMix1[s3 >> 16 & 0xff] ^ invSubMix2[s0 >> 8 & 0xff] ^ invSubMix3[s1 & 0xff] ^ invKeySchedule[ksRow + 2];
t3 = invSubMix0[s3 >>> 24] ^ invSubMix1[s0 >> 16 & 0xff] ^ invSubMix2[s1 >> 8 & 0xff] ^ invSubMix3[s2 & 0xff] ^ invKeySchedule[ksRow + 3];
// Update state
s0 = t0;
s1 = t1;
s2 = t2;
s3 = t3;
ksRow = ksRow + 4;
}
// Shift rows, sub bytes, add round key
t0 = invSBOX[s0 >>> 24] << 24 ^ invSBOX[s1 >> 16 & 0xff] << 16 ^ invSBOX[s2 >> 8 & 0xff] << 8 ^ invSBOX[s3 & 0xff] ^ invKeySchedule[ksRow];
t1 = invSBOX[s1 >>> 24] << 24 ^ invSBOX[s2 >> 16 & 0xff] << 16 ^ invSBOX[s3 >> 8 & 0xff] << 8 ^ invSBOX[s0 & 0xff] ^ invKeySchedule[ksRow + 1];
t2 = invSBOX[s2 >>> 24] << 24 ^ invSBOX[s3 >> 16 & 0xff] << 16 ^ invSBOX[s0 >> 8 & 0xff] << 8 ^ invSBOX[s1 & 0xff] ^ invKeySchedule[ksRow + 2];
t3 = invSBOX[s3 >>> 24] << 24 ^ invSBOX[s0 >> 16 & 0xff] << 16 ^ invSBOX[s1 >> 8 & 0xff] << 8 ^ invSBOX[s2 & 0xff] ^ invKeySchedule[ksRow + 3];
// Write
outputInt32[offset] = swapWord(t0 ^ initVector0);
outputInt32[offset + 1] = swapWord(t3 ^ initVector1);
outputInt32[offset + 2] = swapWord(t2 ^ initVector2);
outputInt32[offset + 3] = swapWord(t1 ^ initVector3);
// reset initVector to last 4 unsigned int
initVector0 = inputWords0;
initVector1 = inputWords1;
initVector2 = inputWords2;
initVector3 = inputWords3;
offset = offset + 4;
}
return outputInt32.buffer;
}
}
const CHUNK_SIZE = 16; // 16 bytes, 128 bits
class Decrypter {
constructor(config, {
removePKCS7Padding = true
} = {}) {
this.logEnabled = true;
this.removePKCS7Padding = void 0;
this.subtle = null;
this.softwareDecrypter = null;
this.key = null;
this.fastAesKey = null;
this.remainderData = null;
this.currentIV = null;
this.currentResult = null;
this.useSoftware = void 0;
this.useSoftware = config.enableSoftwareAES;
this.removePKCS7Padding = removePKCS7Padding;
// built in decryptor expects PKCS7 padding
if (removePKCS7Padding) {
try {
const browserCrypto = self.crypto;
if (browserCrypto) {
this.subtle = browserCrypto.subtle || browserCrypto.webkitSubtle;
}
} catch (e) {
/* no-op */
}
}
if (this.subtle === null) {
this.useSoftware = true;
}
}
destroy() {
this.subtle = null;
this.softwareDecrypter = null;
this.key = null;
this.fastAesKey = null;
this.remainderData = null;
this.currentIV = null;
this.currentResult = null;
}
isSync() {
return this.useSoftware;
}
flush() {
const {
currentResult,
remainderData
} = this;
if (!currentResult || remainderData) {
this.reset();
return null;
}
const data = new Uint8Array(currentResult);
this.reset();
if (this.removePKCS7Padding) {
return removePadding(data);
}
return data;
}
reset() {
this.currentResult = null;
this.currentIV = null;
this.remainderData = null;
if (this.softwareDecrypter) {
this.softwareDecrypter = null;
}
}
decrypt(data, key, iv) {
if (this.useSoftware) {
return new Promise((resolve, reject) => {
this.softwareDecrypt(new Uint8Array(data), key, iv);
const decryptResult = this.flush();
if (decryptResult) {
resolve(decryptResult.buffer);
} else {
reject(new Error('[softwareDecrypt] Failed to decrypt data'));
}
});
}
return this.webCryptoDecrypt(new Uint8Array(data), key, iv);
}
// Software decryption is progressive. Progressive decryption may not return a result on each call. Any cached
// data is handled in the flush() call
softwareDecrypt(data, key, iv) {
const {
currentIV,
currentResult,
remainderData
} = this;
this.logOnce('JS AES decrypt');
// The output is staggered during progressive parsing - the current result is cached, and emitted on the next call
// This is done in order to strip PKCS7 padding, which is found at the end of each segment. We only know we've reached
// the end on flush(), but by that time we have already received all bytes for the segment.
// Progressive decryption does not work with WebCrypto
if (remainderData) {
data = appendUint8Array(remainderData, data);
this.remainderData = null;
}
// Byte length must be a multiple of 16 (AES-128 = 128 bit blocks = 16 bytes)
const currentChunk = this.getValidChunk(data);
if (!currentChunk.length) {
return null;
}
if (currentIV) {
iv = currentIV;
}
let softwareDecrypter = this.softwareDecrypter;
if (!softwareDecrypter) {
softwareDecrypter = this.softwareDecrypter = new AESDecryptor();
}
softwareDecrypter.expandKey(key);
const result = currentResult;
this.currentResult = softwareDecrypter.decrypt(currentChunk.buffer, 0, iv);
this.currentIV = sliceUint8(currentChunk, -16).buffer;
if (!result) {
return null;
}
return result;
}
webCryptoDecrypt(data, key, iv) {
const subtle = this.subtle;
if (this.key !== key || !this.fastAesKey) {
this.key = key;
this.fastAesKey = new FastAESKey(subtle, key);
}
return this.fastAesKey.expandKey().then(aesKey => {
// decrypt using web crypto
if (!subtle) {
return Promise.reject(new Error('web crypto not initialized'));
}
this.logOnce('WebCrypto AES decrypt');
const crypto = new AESCrypto(subtle, new Uint8Array(iv));
return crypto.decrypt(data.buffer, aesKey);
}).catch(err => {
logger.warn(`[decrypter]: WebCrypto Error, disable WebCrypto API, ${err.name}: ${err.message}`);
return this.onWebCryptoError(data, key, iv);
});
}
onWebCryptoError(data, key, iv) {
this.useSoftware = true;
this.logEnabled = true;
this.softwareDecrypt(data, key, iv);
const decryptResult = this.flush();
if (decryptResult) {
return decryptResult.buffer;
}
throw new Error('WebCrypto and softwareDecrypt: failed to decrypt data');
}
getValidChunk(data) {
let currentChunk = data;
const splitPoint = data.length - data.length % CHUNK_SIZE;
if (splitPoint !== data.length) {
currentChunk = sliceUint8(data, 0, splitPoint);
this.remainderData = sliceUint8(data, splitPoint);
}
return currentChunk;
}
logOnce(msg) {
if (!this.logEnabled) {
return;
}
logger.log(`[decrypter]: ${msg}`);
this.logEnabled = false;
}
}
/**
* TimeRanges to string helper
*/
const TimeRanges = {
toString: function (r) {
let log = '';
const len = r.length;
for (let i = 0; i < len; i++) {
log += `[${r.start(i).toFixed(3)}-${r.end(i).toFixed(3)}]`;
}
return log;
}
};
const State = {
STOPPED: 'STOPPED',
IDLE: 'IDLE',
KEY_LOADING: 'KEY_LOADING',
FRAG_LOADING: 'FRAG_LOADING',
FRAG_LOADING_WAITING_RETRY: 'FRAG_LOADING_WAITING_RETRY',
WAITING_TRACK: 'WAITING_TRACK',
PARSING: 'PARSING',
PARSED: 'PARSED',
ENDED: 'ENDED',
ERROR: 'ERROR',
WAITING_INIT_PTS: 'WAITING_INIT_PTS',
WAITING_LEVEL: 'WAITING_LEVEL'
};
class BaseStreamController extends TaskLoop {
constructor(hls, fragmentTracker, keyLoader, logPrefix, playlistType) {
super();
this.hls = void 0;
this.fragPrevious = null;
this.fragCurrent = null;
this.fragmentTracker = void 0;
this.transmuxer = null;
this._state = State.STOPPED;
this.playlistType = void 0;
this.media = null;
this.mediaBuffer = null;
this.config = void 0;
this.bitrateTest = false;
this.lastCurrentTime = 0;
this.nextLoadPosition = 0;
this.startPosition = 0;
this.startTimeOffset = null;
this.loadedmetadata = false;
this.retryDate = 0;
this.levels = null;
this.fragmentLoader = void 0;
this.keyLoader = void 0;
this.levelLastLoaded = null;
this.startFragRequested = false;
this.decrypter = void 0;
this.initPTS = [];
this.onvseeking = null;
this.onvended = null;
this.logPrefix = '';
this.log = void 0;
this.warn = void 0;
this.playlistType = playlistType;
this.logPrefix = logPrefix;
this.log = logger.log.bind(logger, `${logPrefix}:`);
this.warn = logger.warn.bind(logger, `${logPrefix}:`);
this.hls = hls;
this.fragmentLoader = new FragmentLoader(hls.config);
this.keyLoader = keyLoader;
this.fragmentTracker = fragmentTracker;
this.config = hls.config;
this.decrypter = new Decrypter(hls.config);
hls.on(Events.MANIFEST_LOADED, this.onManifestLoaded, this);
}
doTick() {
this.onTickEnd();
}
onTickEnd() {}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
startLoad(startPosition) {}
stopLoad() {
this.fragmentLoader.abort();
this.keyLoader.abort(this.playlistType);
const frag = this.fragCurrent;
if (frag != null && frag.loader) {
frag.abortRequests();
this.fragmentTracker.removeFragment(frag);
}
this.resetTransmuxer();
this.fragCurrent = null;
this.fragPrevious = null;
this.clearInterval();
this.clearNextTick();
this.state = State.STOPPED;
}
_streamEnded(bufferInfo, levelDetails) {
// If playlist is live, there is another buffered range after the current range, nothing buffered, media is detached,
// of nothing loading/loaded return false
if (levelDetails.live || bufferInfo.nextStart || !bufferInfo.end || !this.media) {
return false;
}
const partList = levelDetails.partList;
// Since the last part isn't guaranteed to correspond to the last playlist segment for Low-Latency HLS,
// check instead if the last part is buffered.
if (partList != null && partList.length) {
const lastPart = partList[partList.length - 1];
// Checking the midpoint of the part for potential margin of error and related issues.
// NOTE: Technically I believe parts could yield content that is < the computed duration (including potential a duration of 0)
// and still be spec-compliant, so there may still be edge cases here. Likewise, there could be issues in end of stream
// part mismatches for independent audio and video playlists/segments.
const lastPartBuffered = BufferHelper.isBuffered(this.media, lastPart.start + lastPart.duration / 2);
return lastPartBuffered;
}
const playlistType = levelDetails.fragments[levelDetails.fragments.length - 1].type;
return this.fragmentTracker.isEndListAppended(playlistType);
}
getLevelDetails() {
if (this.levels && this.levelLastLoaded !== null) {
var _this$levels$this$lev;
return (_this$levels$this$lev = this.levels[this.levelLastLoaded]) == null ? void 0 : _this$levels$this$lev.details;
}
}
onMediaAttached(event, data) {
const media = this.media = this.mediaBuffer = data.media;
this.onvseeking = this.onMediaSeeking.bind(this);
this.onvended = this.onMediaEnded.bind(this);
media.addEventListener('seeking', this.onvseeking);
media.addEventListener('ended', this.onvended);
const config = this.config;
if (this.levels && config.autoStartLoad && this.state === State.STOPPED) {
this.startLoad(config.startPosition);
}
}
onMediaDetaching() {
const media = this.media;
if (media != null && media.ended) {
this.log('MSE detaching and video ended, reset startPosition');
this.startPosition = this.lastCurrentTime = 0;
}
// remove video listeners
if (media && this.onvseeking && this.onvended) {
media.removeEventListener('seeking', this.onvseeking);
media.removeEventListener('ended', this.onvended);
this.onvseeking = this.onvended = null;
}
if (this.keyLoader) {
this.keyLoader.detach();
}
this.media = this.mediaBuffer = null;
this.loadedmetadata = false;
this.fragmentTracker.removeAllFragments();
this.stopLoad();
}
onMediaSeeking() {
const {
config,
fragCurrent,
media,
mediaBuffer,
state
} = this;
const currentTime = media ? media.currentTime : 0;
const bufferInfo = BufferHelper.bufferInfo(mediaBuffer ? mediaBuffer : media, currentTime, config.maxBufferHole);
this.log(`media seeking to ${isFiniteNumber(currentTime) ? currentTime.toFixed(3) : currentTime}, state: ${state}`);
if (this.state === State.ENDED) {
this.resetLoadingState();
} else if (fragCurrent) {
// Seeking while frag load is in progress
const tolerance = config.maxFragLookUpTolerance;
const fragStartOffset = fragCurrent.start - tolerance;
const fragEndOffset = fragCurrent.start + fragCurrent.duration + tolerance;
// if seeking out of buffered range or into new one
if (!bufferInfo.len || fragEndOffset < bufferInfo.start || fragStartOffset > bufferInfo.end) {
const pastFragment = currentTime > fragEndOffset;
// if the seek position is outside the current fragment range
if (currentTime < fragStartOffset || pastFragment) {
if (pastFragment && fragCurrent.loader) {
this.log('seeking outside of buffer while fragment load in progress, cancel fragment load');
fragCurrent.abortRequests();
this.resetLoadingState();
}
this.fragPrevious = null;
}
}
}
if (media) {
// Remove gap fragments
this.fragmentTracker.removeFragmentsInRange(currentTime, Infinity, this.playlistType, true);
this.lastCurrentTime = currentTime;
}
// in case seeking occurs although no media buffered, adjust startPosition and nextLoadPosition to seek target
if (!this.loadedmetadata && !bufferInfo.len) {
this.nextLoadPosition = this.startPosition = currentTime;
}
// Async tick to speed up processing
this.tickImmediate();
}
onMediaEnded() {
// reset startPosition and lastCurrentTime to restart playback @ stream beginning
this.startPosition = this.lastCurrentTime = 0;
}
onManifestLoaded(event, data) {
this.startTimeOffset = data.startTimeOffset;
this.initPTS = [];
}
onHandlerDestroying() {
this.stopLoad();
super.onHandlerDestroying();
}
onHandlerDestroyed() {
this.state = State.STOPPED;
if (this.fragmentLoader) {
this.fragmentLoader.destroy();
}
if (this.keyLoader) {
this.keyLoader.destroy();
}
if (this.decrypter) {
this.decrypter.destroy();
}
this.hls = this.log = this.warn = this.decrypter = this.keyLoader = this.fragmentLoader = this.fragmentTracker = null;
super.onHandlerDestroyed();
}
loadFragment(frag, level, targetBufferTime) {
this._loadFragForPlayback(frag, level, targetBufferTime);
}
_loadFragForPlayback(frag, level, targetBufferTime) {
const progressCallback = data => {
if (this.fragContextChanged(frag)) {
this.warn(`Fragment ${frag.sn}${data.part ? ' p: ' + data.part.index : ''} of level ${frag.level} was dropped during download.`);
this.fragmentTracker.removeFragment(frag);
return;
}
frag.stats.chunkCount++;
this._handleFragmentLoadProgress(data);
};
this._doFragLoad(frag, level, targetBufferTime, progressCallback).then(data => {
if (!data) {
// if we're here we probably needed to backtrack or are waiting for more parts
return;
}
const state = this.state;
if (this.fragContextChanged(frag)) {
if (state === State.FRAG_LOADING || !this.fragCurrent && state === State.PARSING) {
this.fragmentTracker.removeFragment(frag);
this.state = State.IDLE;
}
return;
}
if ('payload' in data) {
this.log(`Loaded fragment ${frag.sn} of level ${frag.level}`);
this.hls.trigger(Events.FRAG_LOADED, data);
}
// Pass through the whole payload; controllers not implementing progressive loading receive data from this callback
this._handleFragmentLoadComplete(data);
}).catch(reason => {
if (this.state === State.STOPPED || this.state === State.ERROR) {
return;
}
this.warn(reason);
this.resetFragmentLoading(frag);
});
}
clearTrackerIfNeeded(frag) {
var _this$mediaBuffer;
const {
fragmentTracker
} = this;
const fragState = fragmentTracker.getState(frag);
if (fragState === FragmentState.APPENDING) {
// Lower the buffer size and try again
const playlistType = frag.type;
const bufferedInfo = this.getFwdBufferInfo(this.mediaBuffer, playlistType);
const minForwardBufferLength = Math.max(frag.duration, bufferedInfo ? bufferedInfo.len : this.config.maxBufferLength);
if (this.reduceMaxBufferLength(minForwardBufferLength)) {
fragmentTracker.removeFragment(frag);
}
} else if (((_this$mediaBuffer = this.mediaBuffer) == null ? void 0 : _this$mediaBuffer.buffered.length) === 0) {
// Stop gap for bad tracker / buffer flush behavior
fragmentTracker.removeAllFragments();
} else if (fragmentTracker.hasParts(frag.type)) {
// In low latency mode, remove fragments for which only some parts were buffered
fragmentTracker.detectPartialFragments({
frag,
part: null,
stats: frag.stats,
id: frag.type
});
if (fragmentTracker.getState(frag) === FragmentState.PARTIAL) {
fragmentTracker.removeFragment(frag);
}
}
}
checkLiveUpdate(details) {
if (details.updated && !details.live) {
// Live stream ended, update fragment tracker
const lastFragment = details.fragments[details.fragments.length - 1];
this.fragmentTracker.detectPartialFragments({
frag: lastFragment,
part: null,
stats: lastFragment.stats,
id: lastFragment.type
});
}
if (!details.fragments[0]) {
details.deltaUpdateFailed = true;
}
}
flushMainBuffer(startOffset, endOffset, type = null) {
if (!(startOffset - endOffset)) {
return;
}
// When alternate audio is playing, the audio-stream-controller is responsible for the audio buffer. Otherwise,
// passing a null type flushes both buffers
const flushScope = {
startOffset,
endOffset,
type
};
this.hls.trigger(Events.BUFFER_FLUSHING, flushScope);
}
_loadInitSegment(frag, level) {
this._doFragLoad(frag, level).then(data => {
if (!data || this.fragContextChanged(frag) || !this.levels) {
throw new Error('init load aborted');
}
return data;
}).then(data => {
const {
hls
} = this;
const {
payload
} = data;
const decryptData = frag.decryptdata;
// check to see if the payload needs to be decrypted
if (payload && payload.byteLength > 0 && decryptData && decryptData.key && decryptData.iv && decryptData.method === 'AES-128') {
const startTime = self.performance.now();
// decrypt init segment data
return this.decrypter.decrypt(new Uint8Array(payload), decryptData.key.buffer, decryptData.iv.buffer).catch(err => {
hls.trigger(Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.FRAG_DECRYPT_ERROR,
fatal: false,
error: err,
reason: err.message,
frag
});
throw err;
}).then(decryptedData => {
const endTime = self.performance.now();
hls.trigger(Events.FRAG_DECRYPTED, {
frag,
payload: decryptedData,
stats: {
tstart: startTime,
tdecrypt: endTime
}
});
data.payload = decryptedData;
return data;
});
}
return data;
}).then(data => {
const {
fragCurrent,
hls,
levels
} = this;
if (!levels) {
throw new Error('init load aborted, missing levels');
}
const stats = frag.stats;
this.state = State.IDLE;
level.fragmentError = 0;
frag.data = new Uint8Array(data.payload);
stats.parsing.start = stats.buffering.start = self.performance.now();
stats.parsing.end = stats.buffering.end = self.performance.now();
// Silence FRAG_BUFFERED event if fragCurrent is null
if (data.frag === fragCurrent) {
hls.trigger(Events.FRAG_BUFFERED, {
stats,
frag: fragCurrent,
part: null,
id: frag.type
});
}
this.tick();
}).catch(reason => {
if (this.state === State.STOPPED || this.state === State.ERROR) {
return;
}
this.warn(reason);
this.resetFragmentLoading(frag);
});
}
fragContextChanged(frag) {
const {
fragCurrent
} = this;
return !frag || !fragCurrent || frag.level !== fragCurrent.level || frag.sn !== fragCurrent.sn || frag.urlId !== fragCurrent.urlId;
}
fragBufferedComplete(frag, part) {
var _frag$startPTS, _frag$endPTS, _this$fragCurrent, _this$fragPrevious;
const media = this.mediaBuffer ? this.mediaBuffer : this.media;
this.log(`Buffered ${frag.type} sn: ${frag.sn}${part ? ' part: ' + part.index : ''} of ${this.playlistType === PlaylistLevelType.MAIN ? 'level' : 'track'} ${frag.level} (frag:[${((_frag$startPTS = frag.startPTS) != null ? _frag$startPTS : NaN).toFixed(3)}-${((_frag$endPTS = frag.endPTS) != null ? _frag$endPTS : NaN).toFixed(3)}] > buffer:${media ? TimeRanges.toString(BufferHelper.getBuffered(media)) : '(detached)'})`);
this.state = State.IDLE;
if (!media) {
return;
}
if (!this.loadedmetadata && frag.type == PlaylistLevelType.MAIN && media.buffered.length && ((_this$fragCurrent = this.fragCurrent) == null ? void 0 : _this$fragCurrent.sn) === ((_this$fragPrevious = this.fragPrevious) == null ? void 0 : _this$fragPrevious.sn)) {
this.loadedmetadata = true;
this.seekToStartPos();
}
this.tick();
}
seekToStartPos() {}
_handleFragmentLoadComplete(fragLoadedEndData) {
const {
transmuxer
} = this;
if (!transmuxer) {
return;
}
const {
frag,
part,
partsLoaded
} = fragLoadedEndData;
// If we did not load parts, or loaded all parts, we have complete (not partial) fragment data
const complete = !partsLoaded || partsLoaded.length === 0 || partsLoaded.some(fragLoaded => !fragLoaded);
const chunkMeta = new ChunkMetadata(frag.level, frag.sn, frag.stats.chunkCount + 1, 0, part ? part.index : -1, !complete);
transmuxer.flush(chunkMeta);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_handleFragmentLoadProgress(frag) {}
_doFragLoad(frag, level, targetBufferTime = null, progressCallback) {
var _frag$decryptdata;
const details = level == null ? void 0 : level.details;
if (!this.levels || !details) {
throw new Error(`frag load aborted, missing level${details ? '' : ' detail'}s`);
}
let keyLoadingPromise = null;
if (frag.encrypted && !((_frag$decryptdata = frag.decryptdata) != null && _frag$decryptdata.key)) {
this.log(`Loading key for ${frag.sn} of [${details.startSN}-${details.endSN}], ${this.logPrefix === '[stream-controller]' ? 'level' : 'track'} ${frag.level}`);
this.state = State.KEY_LOADING;
this.fragCurrent = frag;
keyLoadingPromise = this.keyLoader.load(frag).then(keyLoadedData => {
if (!this.fragContextChanged(keyLoadedData.frag)) {
this.hls.trigger(Events.KEY_LOADED, keyLoadedData);
if (this.state === State.KEY_LOADING) {
this.state = State.IDLE;
}
return keyLoadedData;
}
});
this.hls.trigger(Events.KEY_LOADING, {
frag
});
if (this.fragCurrent === null) {
keyLoadingPromise = Promise.reject(new Error(`frag load aborted, context changed in KEY_LOADING`));
}
} else if (!frag.encrypted && details.encryptedFragments.length) {
this.keyLoader.loadClear(frag, details.encryptedFragments);
}
targetBufferTime = Math.max(frag.start, targetBufferTime || 0);
if (this.config.lowLatencyMode && frag.sn !== 'initSegment') {
const partList = details.partList;
if (partList && progressCallback) {
if (targetBufferTime > frag.end && details.fragmentHint) {
frag = details.fragmentHint;
}
const partIndex = this.getNextPart(partList, frag, targetBufferTime);
if (partIndex > -1) {
const part = partList[partIndex];
this.log(`Loading part sn: ${frag.sn} p: ${part.index} cc: ${frag.cc} of playlist [${details.startSN}-${details.endSN}] parts [0-${partIndex}-${partList.length - 1}] ${this.logPrefix === '[stream-controller]' ? 'level' : 'track'}: ${frag.level}, target: ${parseFloat(targetBufferTime.toFixed(3))}`);
this.nextLoadPosition = part.start + part.duration;
this.state = State.FRAG_LOADING;
let _result;
if (keyLoadingPromise) {
_result = keyLoadingPromise.then(keyLoadedData => {
if (!keyLoadedData || this.fragContextChanged(keyLoadedData.frag)) {
return null;
}
return this.doFragPartsLoad(frag, part, level, progressCallback);
}).catch(error => this.handleFragLoadError(error));
} else {
_result = this.doFragPartsLoad(frag, part, level, progressCallback).catch(error => this.handleFragLoadError(error));
}
this.hls.trigger(Events.FRAG_LOADING, {
frag,
part,
targetBufferTime
});
if (this.fragCurrent === null) {
return Promise.reject(new Error(`frag load aborted, context changed in FRAG_LOADING parts`));
}
return _result;
} else if (!frag.url || this.loadedEndOfParts(partList, targetBufferTime)) {
// Fragment hint has no parts
return Promise.resolve(null);
}
}
}
this.log(`Loading fragment ${frag.sn} cc: ${frag.cc} ${details ? 'of [' + details.startSN + '-' + details.endSN + '] ' : ''}${this.logPrefix === '[stream-controller]' ? 'level' : 'track'}: ${frag.level}, target: ${parseFloat(targetBufferTime.toFixed(3))}`);
// Don't update nextLoadPosition for fragments which are not buffered
if (isFiniteNumber(frag.sn) && !this.bitrateTest) {
this.nextLoadPosition = frag.start + frag.duration;
}
this.state = State.FRAG_LOADING;
// Load key before streaming fragment data
const dataOnProgress = this.config.progressive;
let result;
if (dataOnProgress && keyLoadingPromise) {
result = keyLoadingPromise.then(keyLoadedData => {
if (!keyLoadedData || this.fragContextChanged(keyLoadedData == null ? void 0 : keyLoadedData.frag)) {
return null;
}
return this.fragmentLoader.load(frag, progressCallback);
}).catch(error => this.handleFragLoadError(error));
} else {
// load unencrypted fragment data with progress event,
// or handle fragment result after key and fragment are finished loading
result = Promise.all([this.fragmentLoader.load(frag, dataOnProgress ? progressCallback : undefined), keyLoadingPromise]).then(([fragLoadedData]) => {
if (!dataOnProgress && fragLoadedData && progressCallback) {
progressCallback(fragLoadedData);
}
return fragLoadedData;
}).catch(error => this.handleFragLoadError(error));
}
this.hls.trigger(Events.FRAG_LOADING, {
frag,
targetBufferTime
});
if (this.fragCurrent === null) {
return Promise.reject(new Error(`frag load aborted, context changed in FRAG_LOADING`));
}
return result;
}
doFragPartsLoad(frag, fromPart, level, progressCallback) {
return new Promise((resolve, reject) => {
var _level$details;
const partsLoaded = [];
const initialPartList = (_level$details = level.details) == null ? void 0 : _level$details.partList;
const loadPart = part => {
this.fragmentLoader.loadPart(frag, part, progressCallback).then(partLoadedData => {
partsLoaded[part.index] = partLoadedData;
const loadedPart = partLoadedData.part;
this.hls.trigger(Events.FRAG_LOADED, partLoadedData);
const nextPart = getPartWith(level, frag.sn, part.index + 1) || findPart(initialPartList, frag.sn, part.index + 1);
if (nextPart) {
loadPart(nextPart);
} else {
return resolve({
frag,
part: loadedPart,
partsLoaded
});
}
}).catch(reject);
};
loadPart(fromPart);
});
}
handleFragLoadError(error) {
if ('data' in error) {
const data = error.data;
if (error.data && data.details === ErrorDetails.INTERNAL_ABORTED) {
this.handleFragLoadAborted(data.frag, data.part);
} else {
this.hls.trigger(Events.ERROR, data);
}
} else {
this.hls.trigger(Events.ERROR, {
type: ErrorTypes.OTHER_ERROR,
details: ErrorDetails.INTERNAL_EXCEPTION,
err: error,
error,
fatal: true
});
}
return null;
}
_handleTransmuxerFlush(chunkMeta) {
const context = this.getCurrentContext(chunkMeta);
if (!context || this.state !== State.PARSING) {
if (!this.fragCurrent && this.state !== State.STOPPED && this.state !== State.ERROR) {
this.state = State.IDLE;
}
return;
}
const {
frag,
part,
level
} = context;
const now = self.performance.now();
frag.stats.parsing.end = now;
if (part) {
part.stats.parsing.end = now;
}
this.updateLevelTiming(frag, part, level, chunkMeta.partial);
}
getCurrentContext(chunkMeta) {
const {
levels,
fragCurrent
} = this;
const {
level: levelIndex,
sn,
part: partIndex
} = chunkMeta;
if (!(levels != null && levels[levelIndex])) {
this.warn(`Levels object was unset while buffering fragment ${sn} of level ${levelIndex}. The current chunk will not be buffered.`);
return null;
}
const level = levels[levelIndex];
const part = partIndex > -1 ? getPartWith(level, sn, partIndex) : null;
const frag = part ? part.fragment : getFragmentWithSN(level, sn, fragCurrent);
if (!frag) {
return null;
}
if (fragCurrent && fragCurrent !== frag) {
frag.stats = fragCurrent.stats;
}
return {
frag,
part,
level
};
}
bufferFragmentData(data, frag, part, chunkMeta, noBacktracking) {
var _buffer;
if (!data || this.state !== State.PARSING) {
return;
}
const {
data1,
data2
} = data;
let buffer = data1;
if (data1 && data2) {
// Combine the moof + mdat so that we buffer with a single append
buffer = appendUint8Array(data1, data2);
}
if (!((_buffer = buffer) != null && _buffer.length)) {
return;
}
const segment = {
type: data.type,
frag,
part,
chunkMeta,
parent: frag.type,
data: buffer
};
this.hls.trigger(Events.BUFFER_APPENDING, segment);
if (data.dropped && data.independent && !part) {
if (noBacktracking) {
return;
}
// Clear buffer so that we reload previous segments sequentially if required
this.flushBufferGap(frag);
}
}
flushBufferGap(frag) {
const media = this.media;
if (!media) {
return;
}
// If currentTime is not buffered, clear the back buffer so that we can backtrack as much as needed
if (!BufferHelper.isBuffered(media, media.currentTime)) {
this.flushMainBuffer(0, frag.start);
return;
}
// Remove back-buffer without interrupting playback to allow back tracking
const currentTime = media.currentTime;
const bufferInfo = BufferHelper.bufferInfo(media, currentTime, 0);
const fragDuration = frag.duration;
const segmentFraction = Math.min(this.config.maxFragLookUpTolerance * 2, fragDuration * 0.25);
const start = Math.max(Math.min(frag.start - segmentFraction, bufferInfo.end - segmentFraction), currentTime + segmentFraction);
if (frag.start - start > segmentFraction) {
this.flushMainBuffer(start, frag.start);
}
}
getFwdBufferInfo(bufferable, type) {
const pos = this.getLoadPosition();
if (!isFiniteNumber(pos)) {
return null;
}
return this.getFwdBufferInfoAtPos(bufferable, pos, type);
}
getFwdBufferInfoAtPos(bufferable, pos, type) {
const {
config: {
maxBufferHole
}
} = this;
const bufferInfo = BufferHelper.bufferInfo(bufferable, pos, maxBufferHole);
// Workaround flaw in getting forward buffer when maxBufferHole is smaller than gap at current pos
if (bufferInfo.len === 0 && bufferInfo.nextStart !== undefined) {
const bufferedFragAtPos = this.fragmentTracker.getBufferedFrag(pos, type);
if (bufferedFragAtPos && bufferInfo.nextStart < bufferedFragAtPos.end) {
return BufferHelper.bufferInfo(bufferable, pos, Math.max(bufferInfo.nextStart, maxBufferHole));
}
}
return bufferInfo;
}
getMaxBufferLength(levelBitrate) {
const {
config
} = this;
let maxBufLen;
if (levelBitrate) {
maxBufLen = Math.max(8 * config.maxBufferSize / levelBitrate, config.maxBufferLength);
} else {
maxBufLen = config.maxBufferLength;
}
return Math.min(maxBufLen, config.maxMaxBufferLength);
}
reduceMaxBufferLength(threshold) {
const config = this.config;
const minLength = threshold || config.maxBufferLength;
if (config.maxMaxBufferLength >= minLength) {
// reduce max buffer length as it might be too high. we do this to avoid loop flushing ...
config.maxMaxBufferLength /= 2;
this.warn(`Reduce max buffer length to ${config.maxMaxBufferLength}s`);
return true;
}
return false;
}
getAppendedFrag(position, playlistType = PlaylistLevelType.MAIN) {
const fragOrPart = this.fragmentTracker.getAppendedFrag(position, PlaylistLevelType.MAIN);
if (fragOrPart && 'fragment' in fragOrPart) {
return fragOrPart.fragment;
}
return fragOrPart;
}
getNextFragment(pos, levelDetails) {
const fragments = levelDetails.fragments;
const fragLen = fragments.length;
if (!fragLen) {
return null;
}
// find fragment index, contiguous with end of buffer position
const {
config
} = this;
const start = fragments[0].start;
let frag;
if (levelDetails.live) {
const initialLiveManifestSize = config.initialLiveManifestSize;
if (fragLen < initialLiveManifestSize) {
this.warn(`Not enough fragments to start playback (have: ${fragLen}, need: ${initialLiveManifestSize})`);
return null;
}
// The real fragment start times for a live stream are only known after the PTS range for that level is known.
// In order to discover the range, we load the best matching fragment for that level and demux it.
// Do not load using live logic if the starting frag is requested - we want to use getFragmentAtPosition() so that
// we get the fragment matching that start time
if (!levelDetails.PTSKnown && !this.startFragRequested && this.startPosition === -1) {
frag = this.getInitialLiveFragment(levelDetails, fragments);
this.startPosition = frag ? this.hls.liveSyncPosition || frag.start : pos;
}
} else if (pos <= start) {
// VoD playlist: if loadPosition before start of playlist, load first fragment
frag = fragments[0];
}
// If we haven't run into any special cases already, just load the fragment most closely matching the requested position
if (!frag) {
const end = config.lowLatencyMode ? levelDetails.partEnd : levelDetails.fragmentEnd;
frag = this.getFragmentAtPosition(pos, end, levelDetails);
}
return this.mapToInitFragWhenRequired(frag);
}
isLoopLoading(frag, targetBufferTime) {
const trackerState = this.fragmentTracker.getState(frag);
return (trackerState === FragmentState.OK || trackerState === FragmentState.PARTIAL && !!frag.gap) && this.nextLoadPosition > targetBufferTime;
}
getNextFragmentLoopLoading(frag, levelDetails, bufferInfo, playlistType, maxBufLen) {
const gapStart = frag.gap;
const nextFragment = this.getNextFragment(this.nextLoadPosition, levelDetails);
if (nextFragment === null) {
return nextFragment;
}
frag = nextFragment;
if (gapStart && frag && !frag.gap && bufferInfo.nextStart) {
// Media buffered after GAP tags should not make the next buffer timerange exceed forward buffer length
const nextbufferInfo = this.getFwdBufferInfoAtPos(this.mediaBuffer ? this.mediaBuffer : this.media, bufferInfo.nextStart, playlistType);
if (nextbufferInfo !== null && bufferInfo.len + nextbufferInfo.len >= maxBufLen) {
// Returning here might result in not finding an audio and video candiate to skip to
this.log(`buffer full after gaps in "${playlistType}" playlist starting at sn: ${frag.sn}`);
return null;
}
}
return frag;
}
mapToInitFragWhenRequired(frag) {
// If an initSegment is present, it must be buffered first
if (frag != null && frag.initSegment && !(frag != null && frag.initSegment.data) && !this.bitrateTest) {
return frag.initSegment;
}
return frag;
}
getNextPart(partList, frag, targetBufferTime) {
let nextPart = -1;
let contiguous = false;
let independentAttrOmitted = true;
for (let i = 0, len = partList.length; i < len; i++) {
const part = partList[i];
independentAttrOmitted = independentAttrOmitted && !part.independent;
if (nextPart > -1 && targetBufferTime < part.start) {
break;
}
const loaded = part.loaded;
if (loaded) {
nextPart = -1;
} else if ((contiguous || part.independent || independentAttrOmitted) && part.fragment === frag) {
nextPart = i;
}
contiguous = loaded;
}
return nextPart;
}
loadedEndOfParts(partList, targetBufferTime) {
const lastPart = partList[partList.length - 1];
return lastPart && targetBufferTime > lastPart.start && lastPart.loaded;
}
/*
This method is used find the best matching first fragment for a live playlist. This fragment is used to calculate the
"sliding" of the playlist, which is its offset from the start of playback. After sliding we can compute the real
start and end times for each fragment in the playlist (after which this method will not need to be called).
*/
getInitialLiveFragment(levelDetails, fragments) {
const fragPrevious = this.fragPrevious;
let frag = null;
if (fragPrevious) {
if (levelDetails.hasProgramDateTime) {
// Prefer using PDT, because it can be accurate enough to choose the correct fragment without knowing the level sliding
this.log(`Live playlist, switching playlist, load frag with same PDT: ${fragPrevious.programDateTime}`);
frag = findFragmentByPDT(fragments, fragPrevious.endProgramDateTime, this.config.maxFragLookUpTolerance);
}
if (!frag) {
// SN does not need to be accurate between renditions, but depending on the packaging it may be so.
const targetSN = fragPrevious.sn + 1;
if (targetSN >= levelDetails.startSN && targetSN <= levelDetails.endSN) {
const fragNext = fragments[targetSN - levelDetails.startSN];
// Ensure that we're staying within the continuity range, since PTS resets upon a new range
if (fragPrevious.cc === fragNext.cc) {
frag = fragNext;
this.log(`Live playlist, switching playlist, load frag with next SN: ${frag.sn}`);
}
}
// It's important to stay within the continuity range if available; otherwise the fragments in the playlist
// will have the wrong start times
if (!frag) {
frag = findFragWithCC(fragments, fragPrevious.cc);
if (frag) {
this.log(`Live playlist, switching playlist, load frag with same CC: ${frag.sn}`);
}
}
}
} else {
// Find a new start fragment when fragPrevious is null
const liveStart = this.hls.liveSyncPosition;
if (liveStart !== null) {
frag = this.getFragmentAtPosition(liveStart, this.bitrateTest ? levelDetails.fragmentEnd : levelDetails.edge, levelDetails);
}
}
return frag;
}
/*
This method finds the best matching fragment given the provided position.
*/
getFragmentAtPosition(bufferEnd, end, levelDetails) {
const {
config
} = this;
let {
fragPrevious
} = this;
let {
fragments,
endSN
} = levelDetails;
const {
fragmentHint
} = levelDetails;
const tolerance = config.maxFragLookUpTolerance;
const partList = levelDetails.partList;
const loadingParts = !!(config.lowLatencyMode && partList != null && partList.length && fragmentHint);
if (loadingParts && fragmentHint && !this.bitrateTest) {
// Include incomplete fragment with parts at end
fragments = fragments.concat(fragmentHint);
endSN = fragmentHint.sn;
}
let frag;
if (bufferEnd < end) {
const lookupTolerance = bufferEnd > end - tolerance ? 0 : tolerance;
// Remove the tolerance if it would put the bufferEnd past the actual end of stream
// Uses buffer and sequence number to calculate switch segment (required if using EXT-X-DISCONTINUITY-SEQUENCE)
frag = findFragmentByPTS(fragPrevious, fragments, bufferEnd, lookupTolerance);
} else {
// reach end of playlist
frag = fragments[fragments.length - 1];
}
if (frag) {
const curSNIdx = frag.sn - levelDetails.startSN;
// Move fragPrevious forward to support forcing the next fragment to load
// when the buffer catches up to a previously buffered range.
const fragState = this.fragmentTracker.getState(frag);
if (fragState === FragmentState.OK || fragState === FragmentState.PARTIAL && frag.gap) {
fragPrevious = frag;
}
if (fragPrevious && frag.sn === fragPrevious.sn && (!loadingParts || partList[0].fragment.sn > frag.sn)) {
// Force the next fragment to load if the previous one was already selected. This can occasionally happen with
// non-uniform fragment durations
const sameLevel = fragPrevious && frag.level === fragPrevious.level;
if (sameLevel) {
const nextFrag = fragments[curSNIdx + 1];
if (frag.sn < endSN && this.fragmentTracker.getState(nextFrag) !== FragmentState.OK) {
frag = nextFrag;
} else {
frag = null;
}
}
}
}
return frag;
}
synchronizeToLiveEdge(levelDetails) {
const {
config,
media
} = this;
if (!media) {
return;
}
const liveSyncPosition = this.hls.liveSyncPosition;
const currentTime = media.currentTime;
const start = levelDetails.fragments[0].start;
const end = levelDetails.edge;
const withinSlidingWindow = currentTime >= start - config.maxFragLookUpTolerance && currentTime <= end;
// Continue if we can seek forward to sync position or if current time is outside of sliding window
if (liveSyncPosition !== null && media.duration > liveSyncPosition && (currentTime < liveSyncPosition || !withinSlidingWindow)) {
// Continue if buffer is starving or if current time is behind max latency
const maxLatency = config.liveMaxLatencyDuration !== undefined ? config.liveMaxLatencyDuration : config.liveMaxLatencyDurationCount * levelDetails.targetduration;
if (!withinSlidingWindow && media.readyState < 4 || currentTime < end - maxLatency) {
if (!this.loadedmetadata) {
this.nextLoadPosition = liveSyncPosition;
}
// Only seek if ready and there is not a significant forward buffer available for playback
if (media.readyState) {
this.warn(`Playback: ${currentTime.toFixed(3)} is located too far from the end of live sliding playlist: ${end}, reset currentTime to : ${liveSyncPosition.toFixed(3)}`);
media.currentTime = liveSyncPosition;
}
}
}
}
alignPlaylists(details, previousDetails) {
const {
levels,
levelLastLoaded,
fragPrevious
} = this;
const lastLevel = levelLastLoaded !== null ? levels[levelLastLoaded] : null;
// FIXME: If not for `shouldAlignOnDiscontinuities` requiring fragPrevious.cc,
// this could all go in level-helper mergeDetails()
const length = details.fragments.length;
if (!length) {
this.warn(`No fragments in live playlist`);
return 0;
}
const slidingStart = details.fragments[0].start;
const firstLevelLoad = !previousDetails;
const aligned = details.alignedSliding && isFiniteNumber(slidingStart);
if (firstLevelLoad || !aligned && !slidingStart) {
alignStream(fragPrevious, lastLevel, details);
const alignedSlidingStart = details.fragments[0].start;
this.log(`Live playlist sliding: ${alignedSlidingStart.toFixed(2)} start-sn: ${previousDetails ? previousDetails.startSN : 'na'}->${details.startSN} prev-sn: ${fragPrevious ? fragPrevious.sn : 'na'} fragments: ${length}`);
return alignedSlidingStart;
}
return slidingStart;
}
waitForCdnTuneIn(details) {
// Wait for Low-Latency CDN Tune-in to get an updated playlist
const advancePartLimit = 3;
return details.live && details.canBlockReload && details.partTarget && details.tuneInGoal > Math.max(details.partHoldBack, details.partTarget * advancePartLimit);
}
setStartPosition(details, sliding) {
// compute start position if set to -1. use it straight away if value is defined
let startPosition = this.startPosition;
if (startPosition < sliding) {
startPosition = -1;
}
if (startPosition === -1 || this.lastCurrentTime === -1) {
// Use Playlist EXT-X-START:TIME-OFFSET when set
// Prioritize Multivariant Playlist offset so that main, audio, and subtitle stream-controller start times match
const offsetInMultivariantPlaylist = this.startTimeOffset !== null;
const startTimeOffset = offsetInMultivariantPlaylist ? this.startTimeOffset : details.startTimeOffset;
if (startTimeOffset !== null && isFiniteNumber(startTimeOffset)) {
startPosition = sliding + startTimeOffset;
if (startTimeOffset < 0) {
startPosition += details.totalduration;
}
startPosition = Math.min(Math.max(sliding, startPosition), sliding + details.totalduration);
this.log(`Start time offset ${startTimeOffset} found in ${offsetInMultivariantPlaylist ? 'multivariant' : 'media'} playlist, adjust startPosition to ${startPosition}`);
this.startPosition = startPosition;
} else if (details.live) {
// Leave this.startPosition at -1, so that we can use `getInitialLiveFragment` logic when startPosition has
// not been specified via the config or an as an argument to startLoad (#3736).
startPosition = this.hls.liveSyncPosition || sliding;
} else {
this.startPosition = startPosition = 0;
}
this.lastCurrentTime = startPosition;
}
this.nextLoadPosition = startPosition;
}
getLoadPosition() {
const {
media
} = this;
// if we have not yet loaded any fragment, start loading from start position
let pos = 0;
if (this.loadedmetadata && media) {
pos = media.currentTime;
} else if (this.nextLoadPosition) {
pos = this.nextLoadPosition;
}
return pos;
}
handleFragLoadAborted(frag, part) {
if (this.transmuxer && frag.sn !== 'initSegment' && frag.stats.aborted) {
this.warn(`Fragment ${frag.sn}${part ? ' part ' + part.index : ''} of level ${frag.level} was aborted`);
this.resetFragmentLoading(frag);
}
}
resetFragmentLoading(frag) {
if (!this.fragCurrent || !this.fragContextChanged(frag) && this.state !== State.FRAG_LOADING_WAITING_RETRY) {
this.state = State.IDLE;
}
}
onFragmentOrKeyLoadError(filterType, data) {
if (data.chunkMeta && !data.frag) {
const context = this.getCurrentContext(data.chunkMeta);
if (context) {
data.frag = context.frag;
}
}
const frag = data.frag;
// Handle frag error related to caller's filterType
if (!frag || frag.type !== filterType || !this.levels) {
return;
}
if (this.fragContextChanged(frag)) {
var _this$fragCurrent2;
this.warn(`Frag load error must match current frag to retry ${frag.url} > ${(_this$fragCurrent2 = this.fragCurrent) == null ? void 0 : _this$fragCurrent2.url}`);
return;
}
const gapTagEncountered = data.details === ErrorDetails.FRAG_GAP;
if (gapTagEncountered) {
this.fragmentTracker.fragBuffered(frag, true);
}
// keep retrying until the limit will be reached
const errorAction = data.errorAction;
const {
action,
retryCount = 0,
retryConfig
} = errorAction || {};
if (errorAction && action === NetworkErrorAction.RetryRequest && retryConfig) {
var _this$levelLastLoaded;
this.resetStartWhenNotLoaded((_this$levelLastLoaded = this.levelLastLoaded) != null ? _this$levelLastLoaded : frag.level);
const delay = getRetryDelay(retryConfig, retryCount);
this.warn(`Fragment ${frag.sn} of ${filterType} ${frag.level} errored with ${data.details}, retrying loading ${retryCount + 1}/${retryConfig.maxNumRetry} in ${delay}ms`);
errorAction.resolved = true;
this.retryDate = self.performance.now() + delay;
this.state = State.FRAG_LOADING_WAITING_RETRY;
} else if (retryConfig && errorAction) {
this.resetFragmentErrors(filterType);
if (retryCount < retryConfig.maxNumRetry) {
// Network retry is skipped when level switch is preferred
if (!gapTagEncountered) {
errorAction.resolved = true;
}
} else {
logger.warn(`${data.details} reached or exceeded max retry (${retryCount})`);
}
} else if ((errorAction == null ? void 0 : errorAction.action) === NetworkErrorAction.SendAlternateToPenaltyBox) {
this.state = State.WAITING_LEVEL;
} else {
this.state = State.ERROR;
}
// Perform next async tick sooner to speed up error action resolution
this.tickImmediate();
}
reduceLengthAndFlushBuffer(data) {
// if in appending state
if (this.state === State.PARSING || this.state === State.PARSED) {
const playlistType = data.parent;
const bufferedInfo = this.getFwdBufferInfo(this.mediaBuffer, playlistType);
// 0.5 : tolerance needed as some browsers stalls playback before reaching buffered end
// reduce max buf len if current position is buffered
const buffered = bufferedInfo && bufferedInfo.len > 0.5;
if (buffered) {
this.reduceMaxBufferLength(bufferedInfo.len);
}
const flushBuffer = !buffered;
if (flushBuffer) {
// current position is not buffered, but browser is still complaining about buffer full error
// this happens on IE/Edge, refer to https://github.com/video-dev/hls.js/pull/708
// in that case flush the whole audio buffer to recover
this.warn(`Buffer full error while media.currentTime is not buffered, flush ${playlistType} buffer`);
}
if (data.frag) {
this.fragmentTracker.removeFragment(data.frag);
this.nextLoadPosition = data.frag.start;
}
this.resetLoadingState();
return flushBuffer;
}
return false;
}
resetFragmentErrors(filterType) {
if (filterType === PlaylistLevelType.AUDIO) {
// Reset current fragment since audio track audio is essential and may not have a fail-over track
this.fragCurrent = null;
}
// Fragment errors that result in a level switch or redundant fail-over
// should reset the stream controller state to idle
if (!this.loadedmetadata) {
this.startFragRequested = false;
}
if (this.state !== State.STOPPED) {
this.state = State.IDLE;
}
}
afterBufferFlushed(media, bufferType, playlistType) {
if (!media) {
return;
}
// After successful buffer flushing, filter flushed fragments from bufferedFrags use mediaBuffered instead of media
// (so that we will check against video.buffered ranges in case of alt audio track)
const bufferedTimeRanges = BufferHelper.getBuffered(media);
this.fragmentTracker.detectEvictedFragments(bufferType, bufferedTimeRanges, playlistType);
if (this.state === State.ENDED) {
this.resetLoadingState();
}
}
resetLoadingState() {
this.log('Reset loading state');
this.fragCurrent = null;
this.fragPrevious = null;
this.state = State.IDLE;
}
resetStartWhenNotLoaded(level) {
// if loadedmetadata is not set, it means that first frag request failed
// in that case, reset startFragRequested flag
if (!this.loadedmetadata) {
this.startFragRequested = false;
const details = this.levels ? this.levels[level].details : null;
if (details != null && details.live) {
// Update the start position and return to IDLE to recover live start
this.startPosition = -1;
this.setStartPosition(details, 0);
this.resetLoadingState();
} else {
this.nextLoadPosition = this.startPosition;
}
}
}
resetWhenMissingContext(chunkMeta) {
var _this$levelLastLoaded2;
this.warn(`The loading context changed while buffering fragment ${chunkMeta.sn} of level ${chunkMeta.level}. This chunk will not be buffered.`);
this.removeUnbufferedFrags();
this.resetStartWhenNotLoaded((_this$levelLastLoaded2 = this.levelLastLoaded) != null ? _this$levelLastLoaded2 : chunkMeta.level);
this.resetLoadingState();
}
removeUnbufferedFrags(start = 0) {
this.fragmentTracker.removeFragmentsInRange(start, Infinity, this.playlistType, false, true);
}
updateLevelTiming(frag, part, level, partial) {
var _this$transmuxer;
const details = level.details;
if (!details) {
this.warn('level.details undefined');
return;
}
const parsed = Object.keys(frag.elementaryStreams).reduce((result, type) => {
const info = frag.elementaryStreams[type];
if (info) {
const parsedDuration = info.endPTS - info.startPTS;
if (parsedDuration <= 0) {
// Destroy the transmuxer after it's next time offset failed to advance because duration was <= 0.
// The new transmuxer will be configured with a time offset matching the next fragment start,
// preventing the timeline from shifting.
this.warn(`Could not parse fragment ${frag.sn} ${type} duration reliably (${parsedDuration})`);
return result || false;
}
const drift = partial ? 0 : updateFragPTSDTS(details, frag, info.startPTS, info.endPTS, info.startDTS, info.endDTS);
this.hls.trigger(Events.LEVEL_PTS_UPDATED, {
details,
level,
drift,
type,
frag,
start: info.startPTS,
end: info.endPTS
});
return true;
}
return result;
}, false);
if (parsed) {
level.fragmentError = 0;
} else if (((_this$transmuxer = this.transmuxer) == null ? void 0 : _this$transmuxer.error) === null) {
const error = new Error(`Found no media in fragment ${frag.sn} of level ${frag.level} resetting transmuxer to fallback to playlist timing`);
if (level.fragmentError === 0) {
// Mark and track the odd empty segment as a gap to avoid reloading
level.fragmentError++;
frag.gap = true;
this.fragmentTracker.removeFragment(frag);
this.fragmentTracker.fragBuffered(frag, true);
}
this.warn(error.message);
this.hls.trigger(Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.FRAG_PARSING_ERROR,
fatal: false,
error,
frag,
reason: `Found no media in msn ${frag.sn} of level "${level.url}"`
});
if (!this.hls) {
return;
}
this.resetTransmuxer();
// For this error fallthrough. Marking parsed will allow advancing to next fragment.
}
this.state = State.PARSED;
this.hls.trigger(Events.FRAG_PARSED, {
frag,
part
});
}
resetTransmuxer() {
if (this.transmuxer) {
this.transmuxer.destroy();
this.transmuxer = null;
}
}
recoverWorkerError(data) {
if (data.event === 'demuxerWorker') {
var _ref, _this$levelLastLoaded3, _this$fragCurrent3;
this.fragmentTracker.removeAllFragments();
this.resetTransmuxer();
this.resetStartWhenNotLoaded((_ref = (_this$levelLastLoaded3 = this.levelLastLoaded) != null ? _this$levelLastLoaded3 : (_this$fragCurrent3 = this.fragCurrent) == null ? void 0 : _this$fragCurrent3.level) != null ? _ref : 0);
this.resetLoadingState();
}
}
set state(nextState) {
const previousState = this._state;
if (previousState !== nextState) {
this._state = nextState;
this.log(`${previousState}->${nextState}`);
}
}
get state() {
return this._state;
}
}
function getSourceBuffer() {
return self.SourceBuffer || self.WebKitSourceBuffer;
}
/**
* @ignore
*/
function isSupported() {
const mediaSource = getMediaSource();
if (!mediaSource) {
return false;
}
const sourceBuffer = getSourceBuffer();
const isTypeSupported = mediaSource && typeof mediaSource.isTypeSupported === 'function' && mediaSource.isTypeSupported('video/mp4; codecs="avc1.42E01E,mp4a.40.2"');
// if SourceBuffer is exposed ensure its API is valid
// Older browsers do not expose SourceBuffer globally so checking SourceBuffer.prototype is impossible
const sourceBufferValidAPI = !sourceBuffer || sourceBuffer.prototype && typeof sourceBuffer.prototype.appendBuffer === 'function' && typeof sourceBuffer.prototype.remove === 'function';
return !!isTypeSupported && !!sourceBufferValidAPI;
}
/**
* @ignore
*/
function changeTypeSupported() {
var _sourceBuffer$prototy;
const sourceBuffer = getSourceBuffer();
return typeof (sourceBuffer == null ? void 0 : (_sourceBuffer$prototy = sourceBuffer.prototype) == null ? void 0 : _sourceBuffer$prototy.changeType) === 'function';
}
// ensure the worker ends up in the bundle
// If the worker should not be included this gets aliased to empty.js
function hasUMDWorker() {
return typeof __HLS_WORKER_BUNDLE__ === 'function';
}
function injectWorker() {
const blob = new self.Blob([`var exports={};var module={exports:exports};function define(f){f()};define.amd=true;(${__HLS_WORKER_BUNDLE__.toString()})(true);`], {
type: 'text/javascript'
});
const objectURL = self.URL.createObjectURL(blob);
const worker = new self.Worker(objectURL);
return {
worker,
objectURL
};
}
function loadWorker(path) {
const scriptURL = new self.URL(path, self.location.href).href;
const worker = new self.Worker(scriptURL);
return {
worker,
scriptURL
};
}
function dummyTrack(type = '', inputTimeScale = 90000) {
return {
type,
id: -1,
pid: -1,
inputTimeScale,
sequenceNumber: -1,
samples: [],
dropped: 0
};
}
class BaseAudioDemuxer {
constructor() {
this._audioTrack = void 0;
this._id3Track = void 0;
this.frameIndex = 0;
this.cachedData = null;
this.basePTS = null;
this.initPTS = null;
this.lastPTS = null;
}
resetInitSegment(initSegment, audioCodec, videoCodec, trackDuration) {
this._id3Track = {
type: 'id3',
id: 3,
pid: -1,
inputTimeScale: 90000,
sequenceNumber: 0,
samples: [],
dropped: 0
};
}
resetTimeStamp(deaultTimestamp) {
this.initPTS = deaultTimestamp;
this.resetContiguity();
}
resetContiguity() {
this.basePTS = null;
this.lastPTS = null;
this.frameIndex = 0;
}
canParse(data, offset) {
return false;
}
appendFrame(track, data, offset) {}
// feed incoming data to the front of the parsing pipeline
demux(data, timeOffset) {
if (this.cachedData) {
data = appendUint8Array(this.cachedData, data);
this.cachedData = null;
}
let id3Data = getID3Data(data, 0);
let offset = id3Data ? id3Data.length : 0;
let lastDataIndex;
const track = this._audioTrack;
const id3Track = this._id3Track;
const timestamp = id3Data ? getTimeStamp(id3Data) : undefined;
const length = data.length;
if (this.basePTS === null || this.frameIndex === 0 && isFiniteNumber(timestamp)) {
this.basePTS = initPTSFn(timestamp, timeOffset, this.initPTS);
this.lastPTS = this.basePTS;
}
if (this.lastPTS === null) {
this.lastPTS = this.basePTS;
}
// more expressive than alternative: id3Data?.length
if (id3Data && id3Data.length > 0) {
id3Track.samples.push({
pts: this.lastPTS,
dts: this.lastPTS,
data: id3Data,
type: MetadataSchema.audioId3,
duration: Number.POSITIVE_INFINITY
});
}
while (offset < length) {
if (this.canParse(data, offset)) {
const frame = this.appendFrame(track, data, offset);
if (frame) {
this.frameIndex++;
this.lastPTS = frame.sample.pts;
offset += frame.length;
lastDataIndex = offset;
} else {
offset = length;
}
} else if (canParse$2(data, offset)) {
// after a ID3.canParse, a call to ID3.getID3Data *should* always returns some data
id3Data = getID3Data(data, offset);
id3Track.samples.push({
pts: this.lastPTS,
dts: this.lastPTS,
data: id3Data,
type: MetadataSchema.audioId3,
duration: Number.POSITIVE_INFINITY
});
offset += id3Data.length;
lastDataIndex = offset;
} else {
offset++;
}
if (offset === length && lastDataIndex !== length) {
const partialData = sliceUint8(data, lastDataIndex);
if (this.cachedData) {
this.cachedData = appendUint8Array(this.cachedData, partialData);
} else {
this.cachedData = partialData;
}
}
}
return {
audioTrack: track,
videoTrack: dummyTrack(),
id3Track,
textTrack: dummyTrack()
};
}
demuxSampleAes(data, keyData, timeOffset) {
return Promise.reject(new Error(`[${this}] This demuxer does not support Sample-AES decryption`));
}
flush(timeOffset) {
// Parse cache in case of remaining frames.
const cachedData = this.cachedData;
if (cachedData) {
this.cachedData = null;
this.demux(cachedData, 0);
}
return {
audioTrack: this._audioTrack,
videoTrack: dummyTrack(),
id3Track: this._id3Track,
textTrack: dummyTrack()
};
}
destroy() {}
}
/**
* Initialize PTS
*
* use timestamp unless it is undefined, NaN or Infinity
*
*/
const initPTSFn = (timestamp, timeOffset, initPTS) => {
if (isFiniteNumber(timestamp)) {
return timestamp * 90;
}
const init90kHz = initPTS ? initPTS.baseTime * 90000 / initPTS.timescale : 0;
return timeOffset * 90000 + init90kHz;
};
/**
* ADTS parser helper
* @link https://wiki.multimedia.cx/index.php?title=ADTS
*/
function getAudioConfig(observer, data, offset, audioCodec) {
let adtsObjectType;
let adtsExtensionSamplingIndex;
let adtsChannelConfig;
let config;
const userAgent = navigator.userAgent.toLowerCase();
const manifestCodec = audioCodec;
const adtsSamplingRates = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
// byte 2
adtsObjectType = ((data[offset + 2] & 0xc0) >>> 6) + 1;
const adtsSamplingIndex = (data[offset + 2] & 0x3c) >>> 2;
if (adtsSamplingIndex > adtsSamplingRates.length - 1) {
observer.trigger(Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.FRAG_PARSING_ERROR,
fatal: true,
reason: `invalid ADTS sampling index:${adtsSamplingIndex}`
});
return;
}
adtsChannelConfig = (data[offset + 2] & 0x01) << 2;
// byte 3
adtsChannelConfig |= (data[offset + 3] & 0xc0) >>> 6;
logger.log(`manifest codec:${audioCodec}, ADTS type:${adtsObjectType}, samplingIndex:${adtsSamplingIndex}`);
// firefox: freq less than 24kHz = AAC SBR (HE-AAC)
if (/firefox/i.test(userAgent)) {
if (adtsSamplingIndex >= 6) {
adtsObjectType = 5;
config = new Array(4);
// HE-AAC uses SBR (Spectral Band Replication) , high frequencies are constructed from low frequencies
// there is a factor 2 between frame sample rate and output sample rate
// multiply frequency by 2 (see table below, equivalent to substract 3)
adtsExtensionSamplingIndex = adtsSamplingIndex - 3;
} else {
adtsObjectType = 2;
config = new Array(2);
adtsExtensionSamplingIndex = adtsSamplingIndex;
}
// Android : always use AAC
} else if (userAgent.indexOf('android') !== -1) {
adtsObjectType = 2;
config = new Array(2);
adtsExtensionSamplingIndex = adtsSamplingIndex;
} else {
/* for other browsers (Chrome/Vivaldi/Opera ...)
always force audio type to be HE-AAC SBR, as some browsers do not support audio codec switch properly (like Chrome ...)
*/
adtsObjectType = 5;
config = new Array(4);
// if (manifest codec is HE-AAC or HE-AACv2) OR (manifest codec not specified AND frequency less than 24kHz)
if (audioCodec && (audioCodec.indexOf('mp4a.40.29') !== -1 || audioCodec.indexOf('mp4a.40.5') !== -1) || !audioCodec && adtsSamplingIndex >= 6) {
// HE-AAC uses SBR (Spectral Band Replication) , high frequencies are constructed from low frequencies
// there is a factor 2 between frame sample rate and output sample rate
// multiply frequency by 2 (see table below, equivalent to substract 3)
adtsExtensionSamplingIndex = adtsSamplingIndex - 3;
} else {
// if (manifest codec is AAC) AND (frequency less than 24kHz AND nb channel is 1) OR (manifest codec not specified and mono audio)
// Chrome fails to play back with low frequency AAC LC mono when initialized with HE-AAC. This is not a problem with stereo.
if (audioCodec && audioCodec.indexOf('mp4a.40.2') !== -1 && (adtsSamplingIndex >= 6 && adtsChannelConfig === 1 || /vivaldi/i.test(userAgent)) || !audioCodec && adtsChannelConfig === 1) {
adtsObjectType = 2;
config = new Array(2);
}
adtsExtensionSamplingIndex = adtsSamplingIndex;
}
}
/* refer to http://wiki.multimedia.cx/index.php?title=MPEG-4_Audio#Audio_Specific_Config
ISO 14496-3 (AAC).pdf - Table 1.13 — Syntax of AudioSpecificConfig()
Audio Profile / Audio Object Type
0: Null
1: AAC Main
2: AAC LC (Low Complexity)
3: AAC SSR (Scalable Sample Rate)
4: AAC LTP (Long Term Prediction)
5: SBR (Spectral Band Replication)
6: AAC Scalable
sampling freq
0: 96000 Hz
1: 88200 Hz
2: 64000 Hz
3: 48000 Hz
4: 44100 Hz
5: 32000 Hz
6: 24000 Hz
7: 22050 Hz
8: 16000 Hz
9: 12000 Hz
10: 11025 Hz
11: 8000 Hz
12: 7350 Hz
13: Reserved
14: Reserved
15: frequency is written explictly
Channel Configurations
These are the channel configurations:
0: Defined in AOT Specifc Config
1: 1 channel: front-center
2: 2 channels: front-left, front-right
*/
// audioObjectType = profile => profile, the MPEG-4 Audio Object Type minus 1
config[0] = adtsObjectType << 3;
// samplingFrequencyIndex
config[0] |= (adtsSamplingIndex & 0x0e) >> 1;
config[1] |= (adtsSamplingIndex & 0x01) << 7;
// channelConfiguration
config[1] |= adtsChannelConfig << 3;
if (adtsObjectType === 5) {
// adtsExtensionSamplingIndex
config[1] |= (adtsExtensionSamplingIndex & 0x0e) >> 1;
config[2] = (adtsExtensionSamplingIndex & 0x01) << 7;
// adtsObjectType (force to 2, chrome is checking that object type is less than 5 ???
// https://chromium.googlesource.com/chromium/src.git/+/master/media/formats/mp4/aac.cc
config[2] |= 2 << 2;
config[3] = 0;
}
return {
config,
samplerate: adtsSamplingRates[adtsSamplingIndex],
channelCount: adtsChannelConfig,
codec: 'mp4a.40.' + adtsObjectType,
manifestCodec
};
}
function isHeaderPattern$1(data, offset) {
return data[offset] === 0xff && (data[offset + 1] & 0xf6) === 0xf0;
}
function getHeaderLength(data, offset) {
return data[offset + 1] & 0x01 ? 7 : 9;
}
function getFullFrameLength(data, offset) {
return (data[offset + 3] & 0x03) << 11 | data[offset + 4] << 3 | (data[offset + 5] & 0xe0) >>> 5;
}
function canGetFrameLength(data, offset) {
return offset + 5 < data.length;
}
function isHeader$1(data, offset) {
// Look for ADTS header | 1111 1111 | 1111 X00X | where X can be either 0 or 1
// Layer bits (position 14 and 15) in header should be always 0 for ADTS
// More info https://wiki.multimedia.cx/index.php?title=ADTS
return offset + 1 < data.length && isHeaderPattern$1(data, offset);
}
function canParse$1(data, offset) {
return canGetFrameLength(data, offset) && isHeaderPattern$1(data, offset) && getFullFrameLength(data, offset) <= data.length - offset;
}
function probe$1(data, offset) {
// same as isHeader but we also check that ADTS frame follows last ADTS frame
// or end of data is reached
if (isHeader$1(data, offset)) {
// ADTS header Length
const headerLength = getHeaderLength(data, offset);
if (offset + headerLength >= data.length) {
return false;
}
// ADTS frame Length
const frameLength = getFullFrameLength(data, offset);
if (frameLength <= headerLength) {
return false;
}
const newOffset = offset + frameLength;
return newOffset === data.length || isHeader$1(data, newOffset);
}
return false;
}
function initTrackConfig(track, observer, data, offset, audioCodec) {
if (!track.samplerate) {
const config = getAudioConfig(observer, data, offset, audioCodec);
if (!config) {
return;
}
track.config = config.config;
track.samplerate = config.samplerate;
track.channelCount = config.channelCount;
track.codec = config.codec;
track.manifestCodec = config.manifestCodec;
logger.log(`parsed codec:${track.codec}, rate:${config.samplerate}, channels:${config.channelCount}`);
}
}
function getFrameDuration(samplerate) {
return 1024 * 90000 / samplerate;
}
function parseFrameHeader(data, offset) {
// The protection skip bit tells us if we have 2 bytes of CRC data at the end of the ADTS header
const headerLength = getHeaderLength(data, offset);
if (offset + headerLength <= data.length) {
// retrieve frame size
const frameLength = getFullFrameLength(data, offset) - headerLength;
if (frameLength > 0) {
// logger.log(`AAC frame, offset/length/total/pts:${offset+headerLength}/${frameLength}/${data.byteLength}`);
return {
headerLength,
frameLength
};
}
}
}
function appendFrame$1(track, data, offset, pts, frameIndex) {
const frameDuration = getFrameDuration(track.samplerate);
const stamp = pts + frameIndex * frameDuration;
const header = parseFrameHeader(data, offset);
let unit;
if (header) {
const {
frameLength,
headerLength
} = header;
const _length = headerLength + frameLength;
const missing = Math.max(0, offset + _length - data.length);
// logger.log(`AAC frame ${frameIndex}, pts:${stamp} length@offset/total: ${frameLength}@${offset+headerLength}/${data.byteLength} missing: ${missing}`);
if (missing) {
unit = new Uint8Array(_length - headerLength);
unit.set(data.subarray(offset + headerLength, data.length), 0);
} else {
unit = data.subarray(offset + headerLength, offset + _length);
}
const _sample = {
unit,
pts: stamp
};
if (!missing) {
track.samples.push(_sample);
}
return {
sample: _sample,
length: _length,
missing
};
}
// overflow incomplete header
const length = data.length - offset;
unit = new Uint8Array(length);
unit.set(data.subarray(offset, data.length), 0);
const sample = {
unit,
pts: stamp
};
return {
sample,
length,
missing: -1
};
}
/**
* AAC demuxer
*/
class AACDemuxer extends BaseAudioDemuxer {
constructor(observer, config) {
super();
this.observer = void 0;
this.config = void 0;
this.observer = observer;
this.config = config;
}
resetInitSegment(initSegment, audioCodec, videoCodec, trackDuration) {
super.resetInitSegment(initSegment, audioCodec, videoCodec, trackDuration);
this._audioTrack = {
container: 'audio/adts',
type: 'audio',
id: 2,
pid: -1,
sequenceNumber: 0,
segmentCodec: 'aac',
samples: [],
manifestCodec: audioCodec,
duration: trackDuration,
inputTimeScale: 90000,
dropped: 0
};
}
// Source for probe info - https://wiki.multimedia.cx/index.php?title=ADTS
static probe(data) {
if (!data) {
return false;
}
// Check for the ADTS sync word
// Look for ADTS header | 1111 1111 | 1111 X00X | where X can be either 0 or 1
// Layer bits (position 14 and 15) in header should be always 0 for ADTS
// More info https://wiki.multimedia.cx/index.php?title=ADTS
const id3Data = getID3Data(data, 0) || [];
let offset = id3Data.length;
for (let length = data.length; offset < length; offset++) {
if (probe$1(data, offset)) {
logger.log('ADTS sync word found !');
return true;
}
}
return false;
}
canParse(data, offset) {
return canParse$1(data, offset);
}
appendFrame(track, data, offset) {
initTrackConfig(track, this.observer, data, offset, track.manifestCodec);
const frame = appendFrame$1(track, data, offset, this.basePTS, this.frameIndex);
if (frame && frame.missing === 0) {
return frame;
}
}
}
const emsgSchemePattern = /\/emsg[-/]ID3/i;
class MP4Demuxer {
constructor(observer, config) {
this.remainderData = null;
this.timeOffset = 0;
this.config = void 0;
this.videoTrack = void 0;
this.audioTrack = void 0;
this.id3Track = void 0;
this.txtTrack = void 0;
this.config = config;
}
resetTimeStamp() {}
resetInitSegment(initSegment, audioCodec, videoCodec, trackDuration) {
const videoTrack = this.videoTrack = dummyTrack('video', 1);
const audioTrack = this.audioTrack = dummyTrack('audio', 1);
const captionTrack = this.txtTrack = dummyTrack('text', 1);
this.id3Track = dummyTrack('id3', 1);
this.timeOffset = 0;
if (!(initSegment != null && initSegment.byteLength)) {
return;
}
const initData = parseInitSegment(initSegment);
if (initData.video) {
const {
id,
timescale,
codec
} = initData.video;
videoTrack.id = id;
videoTrack.timescale = captionTrack.timescale = timescale;
videoTrack.codec = codec;
}
if (initData.audio) {
const {
id,
timescale,
codec
} = initData.audio;
audioTrack.id = id;
audioTrack.timescale = timescale;
audioTrack.codec = codec;
}
captionTrack.id = RemuxerTrackIdConfig.text;
videoTrack.sampleDuration = 0;
videoTrack.duration = audioTrack.duration = trackDuration;
}
resetContiguity() {
this.remainderData = null;
}
static probe(data) {
// ensure we find a moof box in the first 16 kB
data = data.length > 16384 ? data.subarray(0, 16384) : data;
return findBox(data, ['moof']).length > 0;
}
demux(data, timeOffset) {
this.timeOffset = timeOffset;
// Load all data into the avc track. The CMAF remuxer will look for the data in the samples object; the rest of the fields do not matter
let videoSamples = data;
const videoTrack = this.videoTrack;
const textTrack = this.txtTrack;
if (this.config.progressive) {
// Split the bytestream into two ranges: one encompassing all data up until the start of the last moof, and everything else.
// This is done to guarantee that we're sending valid data to MSE - when demuxing progressively, we have no guarantee
// that the fetch loader gives us flush moof+mdat pairs. If we push jagged data to MSE, it will throw an exception.
if (this.remainderData) {
videoSamples = appendUint8Array(this.remainderData, data);
}
const segmentedData = segmentValidRange(videoSamples);
this.remainderData = segmentedData.remainder;
videoTrack.samples = segmentedData.valid || new Uint8Array();
} else {
videoTrack.samples = videoSamples;
}
const id3Track = this.extractID3Track(videoTrack, timeOffset);
textTrack.samples = parseSamples(timeOffset, videoTrack);
return {
videoTrack,
audioTrack: this.audioTrack,
id3Track,
textTrack: this.txtTrack
};
}
flush() {
const timeOffset = this.timeOffset;
const videoTrack = this.videoTrack;
const textTrack = this.txtTrack;
videoTrack.samples = this.remainderData || new Uint8Array();
this.remainderData = null;
const id3Track = this.extractID3Track(videoTrack, this.timeOffset);
textTrack.samples = parseSamples(timeOffset, videoTrack);
return {
videoTrack,
audioTrack: dummyTrack(),
id3Track,
textTrack: dummyTrack()
};
}
extractID3Track(videoTrack, timeOffset) {
const id3Track = this.id3Track;
if (videoTrack.samples.length) {
const emsgs = findBox(videoTrack.samples, ['emsg']);
if (emsgs) {
emsgs.forEach(data => {
const emsgInfo = parseEmsg(data);
if (emsgSchemePattern.test(emsgInfo.schemeIdUri)) {
const pts = isFiniteNumber(emsgInfo.presentationTime) ? emsgInfo.presentationTime / emsgInfo.timeScale : timeOffset + emsgInfo.presentationTimeDelta / emsgInfo.timeScale;
let duration = emsgInfo.eventDuration === 0xffffffff ? Number.POSITIVE_INFINITY : emsgInfo.eventDuration / emsgInfo.timeScale;
// Safari takes anything <= 0.001 seconds and maps it to Infinity
if (duration <= 0.001) {
duration = Number.POSITIVE_INFINITY;
}
const payload = emsgInfo.payload;
id3Track.samples.push({
data: payload,
len: payload.byteLength,
dts: pts,
pts: pts,
type: MetadataSchema.emsg,
duration: duration
});
}
});
}
}
return id3Track;
}
demuxSampleAes(data, keyData, timeOffset) {
return Promise.reject(new Error('The MP4 demuxer does not support SAMPLE-AES decryption'));
}
destroy() {}
}
/**
* MPEG parser helper
*/
let chromeVersion$1 = null;
const BitratesMap = [32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160];
const SamplingRateMap = [44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000];
const SamplesCoefficients = [
// MPEG 2.5
[0,
// Reserved
72,
// Layer3
144,
// Layer2
12 // Layer1
],
// Reserved
[0,
// Reserved
0,
// Layer3
0,
// Layer2
0 // Layer1
],
// MPEG 2
[0,
// Reserved
72,
// Layer3
144,
// Layer2
12 // Layer1
],
// MPEG 1
[0,
// Reserved
144,
// Layer3
144,
// Layer2
12 // Layer1
]];
const BytesInSlot = [0,
// Reserved
1,
// Layer3
1,
// Layer2
4 // Layer1
];
function appendFrame(track, data, offset, pts, frameIndex) {
// Using http://www.datavoyage.com/mpgscript/mpeghdr.htm as a reference
if (offset + 24 > data.length) {
return;
}
const header = parseHeader(data, offset);
if (header && offset + header.frameLength <= data.length) {
const frameDuration = header.samplesPerFrame * 90000 / header.sampleRate;
const stamp = pts + frameIndex * frameDuration;
const sample = {
unit: data.subarray(offset, offset + header.frameLength),
pts: stamp,
dts: stamp
};
track.config = [];
track.channelCount = header.channelCount;
track.samplerate = header.sampleRate;
track.samples.push(sample);
return {
sample,
length: header.frameLength,
missing: 0
};
}
}
function parseHeader(data, offset) {
const mpegVersion = data[offset + 1] >> 3 & 3;
const mpegLayer = data[offset + 1] >> 1 & 3;
const bitRateIndex = data[offset + 2] >> 4 & 15;
const sampleRateIndex = data[offset + 2] >> 2 & 3;
if (mpegVersion !== 1 && bitRateIndex !== 0 && bitRateIndex !== 15 && sampleRateIndex !== 3) {
const paddingBit = data[offset + 2] >> 1 & 1;
const channelMode = data[offset + 3] >> 6;
const columnInBitrates = mpegVersion === 3 ? 3 - mpegLayer : mpegLayer === 3 ? 3 : 4;
const bitRate = BitratesMap[columnInBitrates * 14 + bitRateIndex - 1] * 1000;
const columnInSampleRates = mpegVersion === 3 ? 0 : mpegVersion === 2 ? 1 : 2;
const sampleRate = SamplingRateMap[columnInSampleRates * 3 + sampleRateIndex];
const channelCount = channelMode === 3 ? 1 : 2; // If bits of channel mode are `11` then it is a single channel (Mono)
const sampleCoefficient = SamplesCoefficients[mpegVersion][mpegLayer];
const bytesInSlot = BytesInSlot[mpegLayer];
const samplesPerFrame = sampleCoefficient * 8 * bytesInSlot;
const frameLength = Math.floor(sampleCoefficient * bitRate / sampleRate + paddingBit) * bytesInSlot;
if (chromeVersion$1 === null) {
const userAgent = navigator.userAgent || '';
const result = userAgent.match(/Chrome\/(\d+)/i);
chromeVersion$1 = result ? parseInt(result[1]) : 0;
}
const needChromeFix = !!chromeVersion$1 && chromeVersion$1 <= 87;
if (needChromeFix && mpegLayer === 2 && bitRate >= 224000 && channelMode === 0) {
// Work around bug in Chromium by setting channelMode to dual-channel (01) instead of stereo (00)
data[offset + 3] = data[offset + 3] | 0x80;
}
return {
sampleRate,
channelCount,
frameLength,
samplesPerFrame
};
}
}
function isHeaderPattern(data, offset) {
return data[offset] === 0xff && (data[offset + 1] & 0xe0) === 0xe0 && (data[offset + 1] & 0x06) !== 0x00;
}
function isHeader(data, offset) {
// Look for MPEG header | 1111 1111 | 111X XYZX | where X can be either 0 or 1 and Y or Z should be 1
// Layer bits (position 14 and 15) in header should be always different from 0 (Layer I or Layer II or Layer III)
// More info http://www.mp3-tech.org/programmer/frame_header.html
return offset + 1 < data.length && isHeaderPattern(data, offset);
}
function canParse(data, offset) {
const headerSize = 4;
return isHeaderPattern(data, offset) && headerSize <= data.length - offset;
}
function probe(data, offset) {
// same as isHeader but we also check that MPEG frame follows last MPEG frame
// or end of data is reached
if (offset + 1 < data.length && isHeaderPattern(data, offset)) {
// MPEG header Length
const headerLength = 4;
// MPEG frame Length
const header = parseHeader(data, offset);
let frameLength = headerLength;
if (header != null && header.frameLength) {
frameLength = header.frameLength;
}
const newOffset = offset + frameLength;
return newOffset === data.length || isHeader(data, newOffset);
}
return false;
}
/**
* Parser for exponential Golomb codes, a variable-bitwidth number encoding scheme used by h264.
*/
class ExpGolomb {
constructor(data) {
this.data = void 0;
this.bytesAvailable = void 0;
this.word = void 0;
this.bitsAvailable = void 0;
this.data = data;
// the number of bytes left to examine in this.data
this.bytesAvailable = data.byteLength;
// the current word being examined
this.word = 0; // :uint
// the number of bits left to examine in the current word
this.bitsAvailable = 0; // :uint
}
// ():void
loadWord() {
const data = this.data;
const bytesAvailable = this.bytesAvailable;
const position = data.byteLength - bytesAvailable;
const workingBytes = new Uint8Array(4);
const availableBytes = Math.min(4, bytesAvailable);
if (availableBytes === 0) {
throw new Error('no bytes available');
}
workingBytes.set(data.subarray(position, position + availableBytes));
this.word = new DataView(workingBytes.buffer).getUint32(0);
// track the amount of this.data that has been processed
this.bitsAvailable = availableBytes * 8;
this.bytesAvailable -= availableBytes;
}
// (count:int):void
skipBits(count) {
let skipBytes; // :int
count = Math.min(count, this.bytesAvailable * 8 + this.bitsAvailable);
if (this.bitsAvailable > count) {
this.word <<= count;
this.bitsAvailable -= count;
} else {
count -= this.bitsAvailable;
skipBytes = count >> 3;
count -= skipBytes << 3;
this.bytesAvailable -= skipBytes;
this.loadWord();
this.word <<= count;
this.bitsAvailable -= count;
}
}
// (size:int):uint
readBits(size) {
let bits = Math.min(this.bitsAvailable, size); // :uint
const valu = this.word >>> 32 - bits; // :uint
if (size > 32) {
logger.error('Cannot read more than 32 bits at a time');
}
this.bitsAvailable -= bits;
if (this.bitsAvailable > 0) {
this.word <<= bits;
} else if (this.bytesAvailable > 0) {
this.loadWord();
} else {
throw new Error('no bits available');
}
bits = size - bits;
if (bits > 0 && this.bitsAvailable) {
return valu << bits | this.readBits(bits);
} else {
return valu;
}
}
// ():uint
skipLZ() {
let leadingZeroCount; // :uint
for (leadingZeroCount = 0; leadingZeroCount < this.bitsAvailable; ++leadingZeroCount) {
if ((this.word & 0x80000000 >>> leadingZeroCount) !== 0) {
// the first bit of working word is 1
this.word <<= leadingZeroCount;
this.bitsAvailable -= leadingZeroCount;
return leadingZeroCount;
}
}
// we exhausted word and still have not found a 1
this.loadWord();
return leadingZeroCount + this.skipLZ();
}
// ():void
skipUEG() {
this.skipBits(1 + this.skipLZ());
}
// ():void
skipEG() {
this.skipBits(1 + this.skipLZ());
}
// ():uint
readUEG() {
const clz = this.skipLZ(); // :uint
return this.readBits(clz + 1) - 1;
}
// ():int
readEG() {
const valu = this.readUEG(); // :int
if (0x01 & valu) {
// the number is odd if the low order bit is set
return 1 + valu >>> 1; // add 1 to make it even, and divide by 2
} else {
return -1 * (valu >>> 1); // divide by two then make it negative
}
}
// Some convenience functions
// :Boolean
readBoolean() {
return this.readBits(1) === 1;
}
// ():int
readUByte() {
return this.readBits(8);
}
// ():int
readUShort() {
return this.readBits(16);
}
// ():int
readUInt() {
return this.readBits(32);
}
/**
* Advance the ExpGolomb decoder past a scaling list. The scaling
* list is optionally transmitted as part of a sequence parameter
* set and is not relevant to transmuxing.
* @param count the number of entries in this scaling list
* @see Recommendation ITU-T H.264, Section 7.3.2.1.1.1
*/
skipScalingList(count) {
let lastScale = 8;
let nextScale = 8;
let deltaScale;
for (let j = 0; j < count; j++) {
if (nextScale !== 0) {
deltaScale = this.readEG();
nextScale = (lastScale + deltaScale + 256) % 256;
}
lastScale = nextScale === 0 ? lastScale : nextScale;
}
}
/**
* Read a sequence parameter set and return some interesting video
* properties. A sequence parameter set is the H264 metadata that
* describes the properties of upcoming video frames.
* @returns an object with configuration parsed from the
* sequence parameter set, including the dimensions of the
* associated video frames.
*/
readSPS() {
let frameCropLeftOffset = 0;
let frameCropRightOffset = 0;
let frameCropTopOffset = 0;
let frameCropBottomOffset = 0;
let numRefFramesInPicOrderCntCycle;
let scalingListCount;
let i;
const readUByte = this.readUByte.bind(this);
const readBits = this.readBits.bind(this);
const readUEG = this.readUEG.bind(this);
const readBoolean = this.readBoolean.bind(this);
const skipBits = this.skipBits.bind(this);
const skipEG = this.skipEG.bind(this);
const skipUEG = this.skipUEG.bind(this);
const skipScalingList = this.skipScalingList.bind(this);
readUByte();
const profileIdc = readUByte(); // profile_idc
readBits(5); // profileCompat constraint_set[0-4]_flag, u(5)
skipBits(3); // reserved_zero_3bits u(3),
readUByte(); // level_idc u(8)
skipUEG(); // seq_parameter_set_id
// some profiles have more optional data we don't need
if (profileIdc === 100 || profileIdc === 110 || profileIdc === 122 || profileIdc === 244 || profileIdc === 44 || profileIdc === 83 || profileIdc === 86 || profileIdc === 118 || profileIdc === 128) {
const chromaFormatIdc = readUEG();
if (chromaFormatIdc === 3) {
skipBits(1);
} // separate_colour_plane_flag
skipUEG(); // bit_depth_luma_minus8
skipUEG(); // bit_depth_chroma_minus8
skipBits(1); // qpprime_y_zero_transform_bypass_flag
if (readBoolean()) {
// seq_scaling_matrix_present_flag
scalingListCount = chromaFormatIdc !== 3 ? 8 : 12;
for (i = 0; i < scalingListCount; i++) {
if (readBoolean()) {
// seq_scaling_list_present_flag[ i ]
if (i < 6) {
skipScalingList(16);
} else {
skipScalingList(64);
}
}
}
}
}
skipUEG(); // log2_max_frame_num_minus4
const picOrderCntType = readUEG();
if (picOrderCntType === 0) {
readUEG(); // log2_max_pic_order_cnt_lsb_minus4
} else if (picOrderCntType === 1) {
skipBits(1); // delta_pic_order_always_zero_flag
skipEG(); // offset_for_non_ref_pic
skipEG(); // offset_for_top_to_bottom_field
numRefFramesInPicOrderCntCycle = readUEG();
for (i = 0; i < numRefFramesInPicOrderCntCycle; i++) {
skipEG();
} // offset_for_ref_frame[ i ]
}
skipUEG(); // max_num_ref_frames
skipBits(1); // gaps_in_frame_num_value_allowed_flag
const picWidthInMbsMinus1 = readUEG();
const picHeightInMapUnitsMinus1 = readUEG();
const frameMbsOnlyFlag = readBits(1);
if (frameMbsOnlyFlag === 0) {
skipBits(1);
} // mb_adaptive_frame_field_flag
skipBits(1); // direct_8x8_inference_flag
if (readBoolean()) {
// frame_cropping_flag
frameCropLeftOffset = readUEG();
frameCropRightOffset = readUEG();
frameCropTopOffset = readUEG();
frameCropBottomOffset = readUEG();
}
let pixelRatio = [1, 1];
if (readBoolean()) {
// vui_parameters_present_flag
if (readBoolean()) {
// aspect_ratio_info_present_flag
const aspectRatioIdc = readUByte();
switch (aspectRatioIdc) {
case 1:
pixelRatio = [1, 1];
break;
case 2:
pixelRatio = [12, 11];
break;
case 3:
pixelRatio = [10, 11];
break;
case 4:
pixelRatio = [16, 11];
break;
case 5:
pixelRatio = [40, 33];
break;
case 6:
pixelRatio = [24, 11];
break;
case 7:
pixelRatio = [20, 11];
break;
case 8:
pixelRatio = [32, 11];
break;
case 9:
pixelRatio = [80, 33];
break;
case 10:
pixelRatio = [18, 11];
break;
case 11:
pixelRatio = [15, 11];
break;
case 12:
pixelRatio = [64, 33];
break;
case 13:
pixelRatio = [160, 99];
break;
case 14:
pixelRatio = [4, 3];
break;
case 15:
pixelRatio = [3, 2];
break;
case 16:
pixelRatio = [2, 1];
break;
case 255:
{
pixelRatio = [readUByte() << 8 | readUByte(), readUByte() << 8 | readUByte()];
break;
}
}
}
}
return {
width: Math.ceil((picWidthInMbsMinus1 + 1) * 16 - frameCropLeftOffset * 2 - frameCropRightOffset * 2),
height: (2 - frameMbsOnlyFlag) * (picHeightInMapUnitsMinus1 + 1) * 16 - (frameMbsOnlyFlag ? 2 : 4) * (frameCropTopOffset + frameCropBottomOffset),
pixelRatio: pixelRatio
};
}
readSliceType() {
// skip NALu type
this.readUByte();
// discard first_mb_in_slice
this.readUEG();
// return slice_type
return this.readUEG();
}
}
/**
* SAMPLE-AES decrypter
*/
class SampleAesDecrypter {
constructor(observer, config, keyData) {
this.keyData = void 0;
this.decrypter = void 0;
this.keyData = keyData;
this.decrypter = new Decrypter(config, {
removePKCS7Padding: false
});
}
decryptBuffer(encryptedData) {
return this.decrypter.decrypt(encryptedData, this.keyData.key.buffer, this.keyData.iv.buffer);
}
// AAC - encrypt all full 16 bytes blocks starting from offset 16
decryptAacSample(samples, sampleIndex, callback) {
const curUnit = samples[sampleIndex].unit;
if (curUnit.length <= 16) {
// No encrypted portion in this sample (first 16 bytes is not
// encrypted, see https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/HLS_Sample_Encryption/Encryption/Encryption.html),
return;
}
const encryptedData = curUnit.subarray(16, curUnit.length - curUnit.length % 16);
const encryptedBuffer = encryptedData.buffer.slice(encryptedData.byteOffset, encryptedData.byteOffset + encryptedData.length);
this.decryptBuffer(encryptedBuffer).then(decryptedBuffer => {
const decryptedData = new Uint8Array(decryptedBuffer);
curUnit.set(decryptedData, 16);
if (!this.decrypter.isSync()) {
this.decryptAacSamples(samples, sampleIndex + 1, callback);
}
});
}
decryptAacSamples(samples, sampleIndex, callback) {
for (;; sampleIndex++) {
if (sampleIndex >= samples.length) {
callback();
return;
}
if (samples[sampleIndex].unit.length < 32) {
continue;
}
this.decryptAacSample(samples, sampleIndex, callback);
if (!this.decrypter.isSync()) {
return;
}
}
}
// AVC - encrypt one 16 bytes block out of ten, starting from offset 32
getAvcEncryptedData(decodedData) {
const encryptedDataLen = Math.floor((decodedData.length - 48) / 160) * 16 + 16;
const encryptedData = new Int8Array(encryptedDataLen);
let outputPos = 0;
for (let inputPos = 32; inputPos < decodedData.length - 16; inputPos += 160, outputPos += 16) {
encryptedData.set(decodedData.subarray(inputPos, inputPos + 16), outputPos);
}
return encryptedData;
}
getAvcDecryptedUnit(decodedData, decryptedData) {
const uint8DecryptedData = new Uint8Array(decryptedData);
let inputPos = 0;
for (let outputPos = 32; outputPos < decodedData.length - 16; outputPos += 160, inputPos += 16) {
decodedData.set(uint8DecryptedData.subarray(inputPos, inputPos + 16), outputPos);
}
return decodedData;
}
decryptAvcSample(samples, sampleIndex, unitIndex, callback, curUnit) {
const decodedData = discardEPB(curUnit.data);
const encryptedData = this.getAvcEncryptedData(decodedData);
this.decryptBuffer(encryptedData.buffer).then(decryptedBuffer => {
curUnit.data = this.getAvcDecryptedUnit(decodedData, decryptedBuffer);
if (!this.decrypter.isSync()) {
this.decryptAvcSamples(samples, sampleIndex, unitIndex + 1, callback);
}
});
}
decryptAvcSamples(samples, sampleIndex, unitIndex, callback) {
if (samples instanceof Uint8Array) {
throw new Error('Cannot decrypt samples of type Uint8Array');
}
for (;; sampleIndex++, unitIndex = 0) {
if (sampleIndex >= samples.length) {
callback();
return;
}
const curUnits = samples[sampleIndex].units;
for (;; unitIndex++) {
if (unitIndex >= curUnits.length) {
break;
}
const curUnit = curUnits[unitIndex];
if (curUnit.data.length <= 48 || curUnit.type !== 1 && curUnit.type !== 5) {
continue;
}
this.decryptAvcSample(samples, sampleIndex, unitIndex, callback, curUnit);
if (!this.decrypter.isSync()) {
return;
}
}
}
}
}
const PACKET_LENGTH = 188;
class TSDemuxer {
constructor(observer, config, typeSupported) {
this.observer = void 0;
this.config = void 0;
this.typeSupported = void 0;
this.sampleAes = null;
this.pmtParsed = false;
this.audioCodec = void 0;
this.videoCodec = void 0;
this._duration = 0;
this._pmtId = -1;
this._avcTrack = void 0;
this._audioTrack = void 0;
this._id3Track = void 0;
this._txtTrack = void 0;
this.aacOverFlow = null;
this.avcSample = null;
this.remainderData = null;
this.observer = observer;
this.config = config;
this.typeSupported = typeSupported;
}
static probe(data) {
const syncOffset = TSDemuxer.syncOffset(data);
if (syncOffset > 0) {
logger.warn(`MPEG2-TS detected but first sync word found @ offset ${syncOffset}`);
}
return syncOffset !== -1;
}
static syncOffset(data) {
const length = data.length;
let scanwindow = Math.min(PACKET_LENGTH * 5, data.length - PACKET_LENGTH) + 1;
let i = 0;
while (i < scanwindow) {
// a TS init segment should contain at least 2 TS packets: PAT and PMT, each starting with 0x47
let foundPat = false;
let packetStart = -1;
let tsPackets = 0;
for (let j = i; j < length; j += PACKET_LENGTH) {
if (data[j] === 0x47) {
tsPackets++;
if (packetStart === -1) {
packetStart = j;
// First sync word found at offset, increase scan length (#5251)
if (packetStart !== 0) {
scanwindow = Math.min(packetStart + PACKET_LENGTH * 99, data.length - PACKET_LENGTH) + 1;
}
}
if (!foundPat) {
foundPat = parsePID(data, j) === 0;
}
// Sync word found at 0 with 3 packets, or found at offset least 2 packets up to scanwindow (#5501)
if (foundPat && tsPackets > 1 && (packetStart === 0 && tsPackets > 2 || j + PACKET_LENGTH > scanwindow)) {
return packetStart;
}
} else if (tsPackets) {
// Exit if sync word found, but does not contain contiguous packets (#5501)
return -1;
} else {
break;
}
}
i++;
}
return -1;
}
/**
* Creates a track model internal to demuxer used to drive remuxing input
*/
static createTrack(type, duration) {
return {
container: type === 'video' || type === 'audio' ? 'video/mp2t' : undefined,
type,
id: RemuxerTrackIdConfig[type],
pid: -1,
inputTimeScale: 90000,
sequenceNumber: 0,
samples: [],
dropped: 0,
duration: type === 'audio' ? duration : undefined
};
}
/**
* Initializes a new init segment on the demuxer/remuxer interface. Needed for discontinuities/track-switches (or at stream start)
* Resets all internal track instances of the demuxer.
*/
resetInitSegment(initSegment, audioCodec, videoCodec, trackDuration) {
this.pmtParsed = false;
this._pmtId = -1;
this._avcTrack = TSDemuxer.createTrack('video');
this._audioTrack = TSDemuxer.createTrack('audio', trackDuration);
this._id3Track = TSDemuxer.createTrack('id3');
this._txtTrack = TSDemuxer.createTrack('text');
this._audioTrack.segmentCodec = 'aac';
// flush any partial content
this.aacOverFlow = null;
this.avcSample = null;
this.remainderData = null;
this.audioCodec = audioCodec;
this.videoCodec = videoCodec;
this._duration = trackDuration;
}
resetTimeStamp() {}
resetContiguity() {
const {
_audioTrack,
_avcTrack,
_id3Track
} = this;
if (_audioTrack) {
_audioTrack.pesData = null;
}
if (_avcTrack) {
_avcTrack.pesData = null;
}
if (_id3Track) {
_id3Track.pesData = null;
}
this.aacOverFlow = null;
this.avcSample = null;
this.remainderData = null;
}
demux(data, timeOffset, isSampleAes = false, flush = false) {
if (!isSampleAes) {
this.sampleAes = null;
}
let pes;
const videoTrack = this._avcTrack;
const audioTrack = this._audioTrack;
const id3Track = this._id3Track;
const textTrack = this._txtTrack;
let avcId = videoTrack.pid;
let avcData = videoTrack.pesData;
let audioId = audioTrack.pid;
let id3Id = id3Track.pid;
let audioData = audioTrack.pesData;
let id3Data = id3Track.pesData;
let unknownPID = null;
let pmtParsed = this.pmtParsed;
let pmtId = this._pmtId;
let len = data.length;
if (this.remainderData) {
data = appendUint8Array(this.remainderData, data);
len = data.length;
this.remainderData = null;
}
if (len < PACKET_LENGTH && !flush) {
this.remainderData = data;
return {
audioTrack,
videoTrack,
id3Track,
textTrack
};
}
const syncOffset = Math.max(0, TSDemuxer.syncOffset(data));
len -= (len - syncOffset) % PACKET_LENGTH;
if (len < data.byteLength && !flush) {
this.remainderData = new Uint8Array(data.buffer, len, data.buffer.byteLength - len);
}
// loop through TS packets
let tsPacketErrors = 0;
for (let start = syncOffset; start < len; start += PACKET_LENGTH) {
if (data[start] === 0x47) {
const stt = !!(data[start + 1] & 0x40);
const pid = parsePID(data, start);
const atf = (data[start + 3] & 0x30) >> 4;
// if an adaption field is present, its length is specified by the fifth byte of the TS packet header.
let offset;
if (atf > 1) {
offset = start + 5 + data[start + 4];
// continue if there is only adaptation field
if (offset === start + PACKET_LENGTH) {
continue;
}
} else {
offset = start + 4;
}
switch (pid) {
case avcId:
if (stt) {
if (avcData && (pes = parsePES(avcData))) {
this.parseAVCPES(videoTrack, textTrack, pes, false);
}
avcData = {
data: [],
size: 0
};
}
if (avcData) {
avcData.data.push(data.subarray(offset, start + PACKET_LENGTH));
avcData.size += start + PACKET_LENGTH - offset;
}
break;
case audioId:
if (stt) {
if (audioData && (pes = parsePES(audioData))) {
switch (audioTrack.segmentCodec) {
case 'aac':
this.parseAACPES(audioTrack, pes);
break;
case 'mp3':
this.parseMPEGPES(audioTrack, pes);
break;
}
}
audioData = {
data: [],
size: 0
};
}
if (audioData) {
audioData.data.push(data.subarray(offset, start + PACKET_LENGTH));
audioData.size += start + PACKET_LENGTH - offset;
}
break;
case id3Id:
if (stt) {
if (id3Data && (pes = parsePES(id3Data))) {
this.parseID3PES(id3Track, pes);
}
id3Data = {
data: [],
size: 0
};
}
if (id3Data) {
id3Data.data.push(data.subarray(offset, start + PACKET_LENGTH));
id3Data.size += start + PACKET_LENGTH - offset;
}
break;
case 0:
if (stt) {
offset += data[offset] + 1;
}
pmtId = this._pmtId = parsePAT(data, offset);
// logger.log('PMT PID:' + this._pmtId);
break;
case pmtId:
{
if (stt) {
offset += data[offset] + 1;
}
const parsedPIDs = parsePMT(data, offset, this.typeSupported, isSampleAes);
// only update track id if track PID found while parsing PMT
// this is to avoid resetting the PID to -1 in case
// track PID transiently disappears from the stream
// this could happen in case of transient missing audio samples for example
// NOTE this is only the PID of the track as found in TS,
// but we are not using this for MP4 track IDs.
avcId = parsedPIDs.avc;
if (avcId > 0) {
videoTrack.pid = avcId;
}
audioId = parsedPIDs.audio;
if (audioId > 0) {
audioTrack.pid = audioId;
audioTrack.segmentCodec = parsedPIDs.segmentCodec;
}
id3Id = parsedPIDs.id3;
if (id3Id > 0) {
id3Track.pid = id3Id;
}
if (unknownPID !== null && !pmtParsed) {
logger.warn(`MPEG-TS PMT found at ${start} after unknown PID '${unknownPID}'. Backtracking to sync byte @${syncOffset} to parse all TS packets.`);
unknownPID = null;
// we set it to -188, the += 188 in the for loop will reset start to 0
start = syncOffset - 188;
}
pmtParsed = this.pmtParsed = true;
break;
}
case 0x11:
case 0x1fff:
break;
default:
unknownPID = pid;
break;
}
} else {
tsPacketErrors++;
}
}
if (tsPacketErrors > 0) {
const error = new Error(`Found ${tsPacketErrors} TS packet/s that do not start with 0x47`);
this.observer.emit(Events.ERROR, Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.FRAG_PARSING_ERROR,
fatal: false,
error,
reason: error.message
});
}
videoTrack.pesData = avcData;
audioTrack.pesData = audioData;
id3Track.pesData = id3Data;
const demuxResult = {
audioTrack,
videoTrack,
id3Track,
textTrack
};
if (flush) {
this.extractRemainingSamples(demuxResult);
}
return demuxResult;
}
flush() {
const {
remainderData
} = this;
this.remainderData = null;
let result;
if (remainderData) {
result = this.demux(remainderData, -1, false, true);
} else {
result = {
videoTrack: this._avcTrack,
audioTrack: this._audioTrack,
id3Track: this._id3Track,
textTrack: this._txtTrack
};
}
this.extractRemainingSamples(result);
if (this.sampleAes) {
return this.decrypt(result, this.sampleAes);
}
return result;
}
extractRemainingSamples(demuxResult) {
const {
audioTrack,
videoTrack,
id3Track,
textTrack
} = demuxResult;
const avcData = videoTrack.pesData;
const audioData = audioTrack.pesData;
const id3Data = id3Track.pesData;
// try to parse last PES packets
let pes;
if (avcData && (pes = parsePES(avcData))) {
this.parseAVCPES(videoTrack, textTrack, pes, true);
videoTrack.pesData = null;
} else {
// either avcData null or PES truncated, keep it for next frag parsing
videoTrack.pesData = avcData;
}
if (audioData && (pes = parsePES(audioData))) {
switch (audioTrack.segmentCodec) {
case 'aac':
this.parseAACPES(audioTrack, pes);
break;
case 'mp3':
this.parseMPEGPES(audioTrack, pes);
break;
}
audioTrack.pesData = null;
} else {
if (audioData != null && audioData.size) {
logger.log('last AAC PES packet truncated,might overlap between fragments');
}
// either audioData null or PES truncated, keep it for next frag parsing
audioTrack.pesData = audioData;
}
if (id3Data && (pes = parsePES(id3Data))) {
this.parseID3PES(id3Track, pes);
id3Track.pesData = null;
} else {
// either id3Data null or PES truncated, keep it for next frag parsing
id3Track.pesData = id3Data;
}
}
demuxSampleAes(data, keyData, timeOffset) {
const demuxResult = this.demux(data, timeOffset, true, !this.config.progressive);
const sampleAes = this.sampleAes = new SampleAesDecrypter(this.observer, this.config, keyData);
return this.decrypt(demuxResult, sampleAes);
}
decrypt(demuxResult, sampleAes) {
return new Promise(resolve => {
const {
audioTrack,
videoTrack
} = demuxResult;
if (audioTrack.samples && audioTrack.segmentCodec === 'aac') {
sampleAes.decryptAacSamples(audioTrack.samples, 0, () => {
if (videoTrack.samples) {
sampleAes.decryptAvcSamples(videoTrack.samples, 0, 0, () => {
resolve(demuxResult);
});
} else {
resolve(demuxResult);
}
});
} else if (videoTrack.samples) {
sampleAes.decryptAvcSamples(videoTrack.samples, 0, 0, () => {
resolve(demuxResult);
});
}
});
}
destroy() {
this._duration = 0;
}
parseAVCPES(track, textTrack, pes, last) {
const units = this.parseAVCNALu(track, pes.data);
let avcSample = this.avcSample;
let push;
let spsfound = false;
// free pes.data to save up some memory
pes.data = null;
// if new NAL units found and last sample still there, let's push ...
// this helps parsing streams with missing AUD (only do this if AUD never found)
if (avcSample && units.length && !track.audFound) {
pushAccessUnit(avcSample, track);
avcSample = this.avcSample = createAVCSample(false, pes.pts, pes.dts, '');
}
units.forEach(unit => {
var _avcSample2;
switch (unit.type) {
// NDR
case 1:
{
let iskey = false;
push = true;
const data = unit.data;
// only check slice type to detect KF in case SPS found in same packet (any keyframe is preceded by SPS ...)
if (spsfound && data.length > 4) {
// retrieve slice type by parsing beginning of NAL unit (follow H264 spec, slice_header definition) to detect keyframe embedded in NDR
const sliceType = new ExpGolomb(data).readSliceType();
// 2 : I slice, 4 : SI slice, 7 : I slice, 9: SI slice
// SI slice : A slice that is coded using intra prediction only and using quantisation of the prediction samples.
// An SI slice can be coded such that its decoded samples can be constructed identically to an SP slice.
// I slice: A slice that is not an SI slice that is decoded using intra prediction only.
// if (sliceType === 2 || sliceType === 7) {
if (sliceType === 2 || sliceType === 4 || sliceType === 7 || sliceType === 9) {
iskey = true;
}
}
if (iskey) {
var _avcSample;
// if we have non-keyframe data already, that cannot belong to the same frame as a keyframe, so force a push
if ((_avcSample = avcSample) != null && _avcSample.frame && !avcSample.key) {
pushAccessUnit(avcSample, track);
avcSample = this.avcSample = null;
}
}
if (!avcSample) {
avcSample = this.avcSample = createAVCSample(true, pes.pts, pes.dts, '');
}
avcSample.frame = true;
avcSample.key = iskey;
break;
// IDR
}
case 5:
push = true;
// handle PES not starting with AUD
// if we have non-keyframe data already, that cannot belong to the same frame as a keyframe, so force a push
if ((_avcSample2 = avcSample) != null && _avcSample2.frame && !avcSample.key) {
pushAccessUnit(avcSample, track);
avcSample = this.avcSample = null;
}
if (!avcSample) {
avcSample = this.avcSample = createAVCSample(true, pes.pts, pes.dts, '');
}
avcSample.key = true;
avcSample.frame = true;
break;
// SEI
case 6:
{
push = true;
parseSEIMessageFromNALu(unit.data, 1, pes.pts, textTrack.samples);
break;
// SPS
}
case 7:
push = true;
spsfound = true;
if (!track.sps) {
const sps = unit.data;
const expGolombDecoder = new ExpGolomb(sps);
const config = expGolombDecoder.readSPS();
track.width = config.width;
track.height = config.height;
track.pixelRatio = config.pixelRatio;
track.sps = [sps];
track.duration = this._duration;
const codecarray = sps.subarray(1, 4);
let codecstring = 'avc1.';
for (let i = 0; i < 3; i++) {
let h = codecarray[i].toString(16);
if (h.length < 2) {
h = '0' + h;
}
codecstring += h;
}
track.codec = codecstring;
}
break;
// PPS
case 8:
push = true;
if (!track.pps) {
track.pps = [unit.data];
}
break;
// AUD
case 9:
push = false;
track.audFound = true;
if (avcSample) {
pushAccessUnit(avcSample, track);
}
avcSample = this.avcSample = createAVCSample(false, pes.pts, pes.dts, '');
break;
// Filler Data
case 12:
push = true;
break;
default:
push = false;
if (avcSample) {
avcSample.debug += 'unknown NAL ' + unit.type + ' ';
}
break;
}
if (avcSample && push) {
const units = avcSample.units;
units.push(unit);
}
});
// if last PES packet, push samples
if (last && avcSample) {
pushAccessUnit(avcSample, track);
this.avcSample = null;
}
}
getLastNalUnit(samples) {
var _avcSample3;
let avcSample = this.avcSample;
let lastUnit;
// try to fallback to previous sample if current one is empty
if (!avcSample || avcSample.units.length === 0) {
avcSample = samples[samples.length - 1];
}
if ((_avcSample3 = avcSample) != null && _avcSample3.units) {
const units = avcSample.units;
lastUnit = units[units.length - 1];
}
return lastUnit;
}
parseAVCNALu(track, array) {
const len = array.byteLength;
let state = track.naluState || 0;
const lastState = state;
const units = [];
let i = 0;
let value;
let overflow;
let unitType;
let lastUnitStart = -1;
let lastUnitType = 0;
// logger.log('PES:' + Hex.hexDump(array));
if (state === -1) {
// special use case where we found 3 or 4-byte start codes exactly at the end of previous PES packet
lastUnitStart = 0;
// NALu type is value read from offset 0
lastUnitType = array[0] & 0x1f;
state = 0;
i = 1;
}
while (i < len) {
value = array[i++];
// optimization. state 0 and 1 are the predominant case. let's handle them outside of the switch/case
if (!state) {
state = value ? 0 : 1;
continue;
}
if (state === 1) {
state = value ? 0 : 2;
continue;
}
// here we have state either equal to 2 or 3
if (!value) {
state = 3;
} else if (value === 1) {
if (lastUnitStart >= 0) {
const unit = {
data: array.subarray(lastUnitStart, i - state - 1),
type: lastUnitType
};
// logger.log('pushing NALU, type/size:' + unit.type + '/' + unit.data.byteLength);
units.push(unit);
} else {
// lastUnitStart is undefined => this is the first start code found in this PES packet
// first check if start code delimiter is overlapping between 2 PES packets,
// ie it started in last packet (lastState not zero)
// and ended at the beginning of this PES packet (i <= 4 - lastState)
const lastUnit = this.getLastNalUnit(track.samples);
if (lastUnit) {
if (lastState && i <= 4 - lastState) {
// start delimiter overlapping between PES packets
// strip start delimiter bytes from the end of last NAL unit
// check if lastUnit had a state different from zero
if (lastUnit.state) {
// strip last bytes
lastUnit.data = lastUnit.data.subarray(0, lastUnit.data.byteLength - lastState);
}
}
// If NAL units are not starting right at the beginning of the PES packet, push preceding data into previous NAL unit.
overflow = i - state - 1;
if (overflow > 0) {
// logger.log('first NALU found with overflow:' + overflow);
const tmp = new Uint8Array(lastUnit.data.byteLength + overflow);
tmp.set(lastUnit.data, 0);
tmp.set(array.subarray(0, overflow), lastUnit.data.byteLength);
lastUnit.data = tmp;
lastUnit.state = 0;
}
}
}
// check if we can read unit type
if (i < len) {
unitType = array[i] & 0x1f;
// logger.log('find NALU @ offset:' + i + ',type:' + unitType);
lastUnitStart = i;
lastUnitType = unitType;
state = 0;
} else {
// not enough byte to read unit type. let's read it on next PES parsing
state = -1;
}
} else {
state = 0;
}
}
if (lastUnitStart >= 0 && state >= 0) {
const unit = {
data: array.subarray(lastUnitStart, len),
type: lastUnitType,
state: state
};
units.push(unit);
// logger.log('pushing NALU, type/size/state:' + unit.type + '/' + unit.data.byteLength + '/' + state);
}
// no NALu found
if (units.length === 0) {
// append pes.data to previous NAL unit
const lastUnit = this.getLastNalUnit(track.samples);
if (lastUnit) {
const tmp = new Uint8Array(lastUnit.data.byteLength + array.byteLength);
tmp.set(lastUnit.data, 0);
tmp.set(array, lastUnit.data.byteLength);
lastUnit.data = tmp;
}
}
track.naluState = state;
return units;
}
parseAACPES(track, pes) {
let startOffset = 0;
const aacOverFlow = this.aacOverFlow;
let data = pes.data;
if (aacOverFlow) {
this.aacOverFlow = null;
const frameMissingBytes = aacOverFlow.missing;
const sampleLength = aacOverFlow.sample.unit.byteLength;
// logger.log(`AAC: append overflowing ${sampleLength} bytes to beginning of new PES`);
if (frameMissingBytes === -1) {
const tmp = new Uint8Array(sampleLength + data.byteLength);
tmp.set(aacOverFlow.sample.unit, 0);
tmp.set(data, sampleLength);
data = tmp;
} else {
const frameOverflowBytes = sampleLength - frameMissingBytes;
aacOverFlow.sample.unit.set(data.subarray(0, frameMissingBytes), frameOverflowBytes);
track.samples.push(aacOverFlow.sample);
startOffset = aacOverFlow.missing;
}
}
// look for ADTS header (0xFFFx)
let offset;
let len;
for (offset = startOffset, len = data.length; offset < len - 1; offset++) {
if (isHeader$1(data, offset)) {
break;
}
}
// if ADTS header does not start straight from the beginning of the PES payload, raise an error
if (offset !== startOffset) {
let reason;
const recoverable = offset < len - 1;
if (recoverable) {
reason = `AAC PES did not start with ADTS header,offset:${offset}`;
} else {
reason = 'No ADTS header found in AAC PES';
}
const error = new Error(reason);
logger.warn(`parsing error: ${reason}`);
this.observer.emit(Events.ERROR, Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.FRAG_PARSING_ERROR,
fatal: false,
levelRetry: recoverable,
error,
reason
});
if (!recoverable) {
return;
}
}
initTrackConfig(track, this.observer, data, offset, this.audioCodec);
let pts;
if (pes.pts !== undefined) {
pts = pes.pts;
} else if (aacOverFlow) {
// if last AAC frame is overflowing, we should ensure timestamps are contiguous:
// first sample PTS should be equal to last sample PTS + frameDuration
const frameDuration = getFrameDuration(track.samplerate);
pts = aacOverFlow.sample.pts + frameDuration;
} else {
logger.warn('[tsdemuxer]: AAC PES unknown PTS');
return;
}
// scan for aac samples
let frameIndex = 0;
let frame;
while (offset < len) {
frame = appendFrame$1(track, data, offset, pts, frameIndex);
offset += frame.length;
if (!frame.missing) {
frameIndex++;
for (; offset < len - 1; offset++) {
if (isHeader$1(data, offset)) {
break;
}
}
} else {
this.aacOverFlow = frame;
break;
}
}
}
parseMPEGPES(track, pes) {
const data = pes.data;
const length = data.length;
let frameIndex = 0;
let offset = 0;
const pts = pes.pts;
if (pts === undefined) {
logger.warn('[tsdemuxer]: MPEG PES unknown PTS');
return;
}
while (offset < length) {
if (isHeader(data, offset)) {
const frame = appendFrame(track, data, offset, pts, frameIndex);
if (frame) {
offset += frame.length;
frameIndex++;
} else {
// logger.log('Unable to parse Mpeg audio frame');
break;
}
} else {
// nothing found, keep looking
offset++;
}
}
}
parseID3PES(id3Track, pes) {
if (pes.pts === undefined) {
logger.warn('[tsdemuxer]: ID3 PES unknown PTS');
return;
}
const id3Sample = _extends({}, pes, {
type: this._avcTrack ? MetadataSchema.emsg : MetadataSchema.audioId3,
duration: Number.POSITIVE_INFINITY
});
id3Track.samples.push(id3Sample);
}
}
function createAVCSample(key, pts, dts, debug) {
return {
key,
frame: false,
pts,
dts,
units: [],
debug,
length: 0
};
}
function parsePID(data, offset) {
// pid is a 13-bit field starting at the last bit of TS[1]
return ((data[offset + 1] & 0x1f) << 8) + data[offset + 2];
}
function parsePAT(data, offset) {
// skip the PSI header and parse the first PMT entry
return (data[offset + 10] & 0x1f) << 8 | data[offset + 11];
}
function parsePMT(data, offset, typeSupported, isSampleAes) {
const result = {
audio: -1,
avc: -1,
id3: -1,
segmentCodec: 'aac'
};
const sectionLength = (data[offset + 1] & 0x0f) << 8 | data[offset + 2];
const tableEnd = offset + 3 + sectionLength - 4;
// to determine where the table is, we have to figure out how
// long the program info descriptors are
const programInfoLength = (data[offset + 10] & 0x0f) << 8 | data[offset + 11];
// advance the offset to the first entry in the mapping table
offset += 12 + programInfoLength;
while (offset < tableEnd) {
const pid = parsePID(data, offset);
switch (data[offset]) {
case 0xcf:
// SAMPLE-AES AAC
if (!isSampleAes) {
logger.log('ADTS AAC with AES-128-CBC frame encryption found in unencrypted stream');
break;
}
/* falls through */
case 0x0f:
// ISO/IEC 13818-7 ADTS AAC (MPEG-2 lower bit-rate audio)
// logger.log('AAC PID:' + pid);
if (result.audio === -1) {
result.audio = pid;
}
break;
// Packetized metadata (ID3)
case 0x15:
// logger.log('ID3 PID:' + pid);
if (result.id3 === -1) {
result.id3 = pid;
}
break;
case 0xdb:
// SAMPLE-AES AVC
if (!isSampleAes) {
logger.log('H.264 with AES-128-CBC slice encryption found in unencrypted stream');
break;
}
/* falls through */
case 0x1b:
// ITU-T Rec. H.264 and ISO/IEC 14496-10 (lower bit-rate video)
// logger.log('AVC PID:' + pid);
if (result.avc === -1) {
result.avc = pid;
}
break;
// ISO/IEC 11172-3 (MPEG-1 audio)
// or ISO/IEC 13818-3 (MPEG-2 halved sample rate audio)
case 0x03:
case 0x04:
// logger.log('MPEG PID:' + pid);
if (typeSupported.mpeg !== true && typeSupported.mp3 !== true) {
logger.log('MPEG audio found, not supported in this browser');
} else if (result.audio === -1) {
result.audio = pid;
result.segmentCodec = 'mp3';
}
break;
case 0x24:
logger.warn('Unsupported HEVC stream type found');
break;
}
// move to the next table entry
// skip past the elementary stream descriptors, if present
offset += ((data[offset + 3] & 0x0f) << 8 | data[offset + 4]) + 5;
}
return result;
}
function parsePES(stream) {
let i = 0;
let frag;
let pesLen;
let pesHdrLen;
let pesPts;
let pesDts;
const data = stream.data;
// safety check
if (!stream || stream.size === 0) {
return null;
}
// we might need up to 19 bytes to read PES header
// if first chunk of data is less than 19 bytes, let's merge it with following ones until we get 19 bytes
// usually only one merge is needed (and this is rare ...)
while (data[0].length < 19 && data.length > 1) {
const newData = new Uint8Array(data[0].length + data[1].length);
newData.set(data[0]);
newData.set(data[1], data[0].length);
data[0] = newData;
data.splice(1, 1);
}
// retrieve PTS/DTS from first fragment
frag = data[0];
const pesPrefix = (frag[0] << 16) + (frag[1] << 8) + frag[2];
if (pesPrefix === 1) {
pesLen = (frag[4] << 8) + frag[5];
// if PES parsed length is not zero and greater than total received length, stop parsing. PES might be truncated
// minus 6 : PES header size
if (pesLen && pesLen > stream.size - 6) {
return null;
}
const pesFlags = frag[7];
if (pesFlags & 0xc0) {
/* PES header described here : http://dvd.sourceforge.net/dvdinfo/pes-hdr.html
as PTS / DTS is 33 bit we cannot use bitwise operator in JS,
as Bitwise operators treat their operands as a sequence of 32 bits */
pesPts = (frag[9] & 0x0e) * 536870912 +
// 1 << 29
(frag[10] & 0xff) * 4194304 +
// 1 << 22
(frag[11] & 0xfe) * 16384 +
// 1 << 14
(frag[12] & 0xff) * 128 +
// 1 << 7
(frag[13] & 0xfe) / 2;
if (pesFlags & 0x40) {
pesDts = (frag[14] & 0x0e) * 536870912 +
// 1 << 29
(frag[15] & 0xff) * 4194304 +
// 1 << 22
(frag[16] & 0xfe) * 16384 +
// 1 << 14
(frag[17] & 0xff) * 128 +
// 1 << 7
(frag[18] & 0xfe) / 2;
if (pesPts - pesDts > 60 * 90000) {
logger.warn(`${Math.round((pesPts - pesDts) / 90000)}s delta between PTS and DTS, align them`);
pesPts = pesDts;
}
} else {
pesDts = pesPts;
}
}
pesHdrLen = frag[8];
// 9 bytes : 6 bytes for PES header + 3 bytes for PES extension
let payloadStartOffset = pesHdrLen + 9;
if (stream.size <= payloadStartOffset) {
return null;
}
stream.size -= payloadStartOffset;
// reassemble PES packet
const pesData = new Uint8Array(stream.size);
for (let j = 0, dataLen = data.length; j < dataLen; j++) {
frag = data[j];
let len = frag.byteLength;
if (payloadStartOffset) {
if (payloadStartOffset > len) {
// trim full frag if PES header bigger than frag
payloadStartOffset -= len;
continue;
} else {
// trim partial frag if PES header smaller than frag
frag = frag.subarray(payloadStartOffset);
len -= payloadStartOffset;
payloadStartOffset = 0;
}
}
pesData.set(frag, i);
i += len;
}
if (pesLen) {
// payload size : remove PES header + PES extension
pesLen -= pesHdrLen + 3;
}
return {
data: pesData,
pts: pesPts,
dts: pesDts,
len: pesLen
};
}
return null;
}
function pushAccessUnit(avcSample, avcTrack) {
if (avcSample.units.length && avcSample.frame) {
// if sample does not have PTS/DTS, patch with last sample PTS/DTS
if (avcSample.pts === undefined) {
const samples = avcTrack.samples;
const nbSamples = samples.length;
if (nbSamples) {
const lastSample = samples[nbSamples - 1];
avcSample.pts = lastSample.pts;
avcSample.dts = lastSample.dts;
} else {
// dropping samples, no timestamp found
avcTrack.dropped++;
return;
}
}
avcTrack.samples.push(avcSample);
}
if (avcSample.debug.length) {
logger.log(avcSample.pts + '/' + avcSample.dts + ':' + avcSample.debug);
}
}
/**
* MP3 demuxer
*/
class MP3Demuxer extends BaseAudioDemuxer {
resetInitSegment(initSegment, audioCodec, videoCodec, trackDuration) {
super.resetInitSegment(initSegment, audioCodec, videoCodec, trackDuration);
this._audioTrack = {
container: 'audio/mpeg',
type: 'audio',
id: 2,
pid: -1,
sequenceNumber: 0,
segmentCodec: 'mp3',
samples: [],
manifestCodec: audioCodec,
duration: trackDuration,
inputTimeScale: 90000,
dropped: 0
};
}
static probe(data) {
if (!data) {
return false;
}
// check if data contains ID3 timestamp and MPEG sync word
// Look for MPEG header | 1111 1111 | 111X XYZX | where X can be either 0 or 1 and Y or Z should be 1
// Layer bits (position 14 and 15) in header should be always different from 0 (Layer I or Layer II or Layer III)
// More info http://www.mp3-tech.org/programmer/frame_header.html
const id3Data = getID3Data(data, 0) || [];
let offset = id3Data.length;
for (let length = data.length; offset < length; offset++) {
if (probe(data, offset)) {
logger.log('MPEG Audio sync word found !');
return true;
}
}
return false;
}
canParse(data, offset) {
return canParse(data, offset);
}
appendFrame(track, data, offset) {
if (this.basePTS === null) {
return;
}
return appendFrame(track, data, offset, this.basePTS, this.frameIndex);
}
}
/**
* AAC helper
*/
class AAC$1 {
static getSilentFrame(codec, channelCount) {
switch (codec) {
case 'mp4a.40.2':
if (channelCount === 1) {
return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x23, 0x80]);
} else if (channelCount === 2) {
return new Uint8Array([0x21, 0x00, 0x49, 0x90, 0x02, 0x19, 0x00, 0x23, 0x80]);
} else if (channelCount === 3) {
return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x8e]);
} else if (channelCount === 4) {
return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x80, 0x2c, 0x80, 0x08, 0x02, 0x38]);
} else if (channelCount === 5) {
return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x82, 0x30, 0x04, 0x99, 0x00, 0x21, 0x90, 0x02, 0x38]);
} else if (channelCount === 6) {
return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x82, 0x30, 0x04, 0x99, 0x00, 0x21, 0x90, 0x02, 0x00, 0xb2, 0x00, 0x20, 0x08, 0xe0]);
}
break;
// handle HE-AAC below (mp4a.40.5 / mp4a.40.29)
default:
if (channelCount === 1) {
// ffmpeg -y -f lavfi -i "aevalsrc=0:d=0.05" -c:a libfdk_aac -profile:a aac_he -b:a 4k output.aac && hexdump -v -e '16/1 "0x%x," "\n"' -v output.aac
return new Uint8Array([0x1, 0x40, 0x22, 0x80, 0xa3, 0x4e, 0xe6, 0x80, 0xba, 0x8, 0x0, 0x0, 0x0, 0x1c, 0x6, 0xf1, 0xc1, 0xa, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5e]);
} else if (channelCount === 2) {
// ffmpeg -y -f lavfi -i "aevalsrc=0|0:d=0.05" -c:a libfdk_aac -profile:a aac_he_v2 -b:a 4k output.aac && hexdump -v -e '16/1 "0x%x," "\n"' -v output.aac
return new Uint8Array([0x1, 0x40, 0x22, 0x80, 0xa3, 0x5e, 0xe6, 0x80, 0xba, 0x8, 0x0, 0x0, 0x0, 0x0, 0x95, 0x0, 0x6, 0xf1, 0xa1, 0xa, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5e]);
} else if (channelCount === 3) {
// ffmpeg -y -f lavfi -i "aevalsrc=0|0|0:d=0.05" -c:a libfdk_aac -profile:a aac_he_v2 -b:a 4k output.aac && hexdump -v -e '16/1 "0x%x," "\n"' -v output.aac
return new Uint8Array([0x1, 0x40, 0x22, 0x80, 0xa3, 0x5e, 0xe6, 0x80, 0xba, 0x8, 0x0, 0x0, 0x0, 0x0, 0x95, 0x0, 0x6, 0xf1, 0xa1, 0xa, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5e]);
}
break;
}
return undefined;
}
}
/**
* Generate MP4 Box
*/
const UINT32_MAX = Math.pow(2, 32) - 1;
class MP4 {
static init() {
MP4.types = {
avc1: [],
// codingname
avcC: [],
btrt: [],
dinf: [],
dref: [],
esds: [],
ftyp: [],
hdlr: [],
mdat: [],
mdhd: [],
mdia: [],
mfhd: [],
minf: [],
moof: [],
moov: [],
mp4a: [],
'.mp3': [],
mvex: [],
mvhd: [],
pasp: [],
sdtp: [],
stbl: [],
stco: [],
stsc: [],
stsd: [],
stsz: [],
stts: [],
tfdt: [],
tfhd: [],
traf: [],
trak: [],
trun: [],
trex: [],
tkhd: [],
vmhd: [],
smhd: []
};
let i;
for (i in MP4.types) {
if (MP4.types.hasOwnProperty(i)) {
MP4.types[i] = [i.charCodeAt(0), i.charCodeAt(1), i.charCodeAt(2), i.charCodeAt(3)];
}
}
const videoHdlr = new Uint8Array([0x00,
// version 0
0x00, 0x00, 0x00,
// flags
0x00, 0x00, 0x00, 0x00,
// pre_defined
0x76, 0x69, 0x64, 0x65,
// handler_type: 'vide'
0x00, 0x00, 0x00, 0x00,
// reserved
0x00, 0x00, 0x00, 0x00,
// reserved
0x00, 0x00, 0x00, 0x00,
// reserved
0x56, 0x69, 0x64, 0x65, 0x6f, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'VideoHandler'
]);
const audioHdlr = new Uint8Array([0x00,
// version 0
0x00, 0x00, 0x00,
// flags
0x00, 0x00, 0x00, 0x00,
// pre_defined
0x73, 0x6f, 0x75, 0x6e,
// handler_type: 'soun'
0x00, 0x00, 0x00, 0x00,
// reserved
0x00, 0x00, 0x00, 0x00,
// reserved
0x00, 0x00, 0x00, 0x00,
// reserved
0x53, 0x6f, 0x75, 0x6e, 0x64, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'SoundHandler'
]);
MP4.HDLR_TYPES = {
video: videoHdlr,
audio: audioHdlr
};
const dref = new Uint8Array([0x00,
// version 0
0x00, 0x00, 0x00,
// flags
0x00, 0x00, 0x00, 0x01,
// entry_count
0x00, 0x00, 0x00, 0x0c,
// entry_size
0x75, 0x72, 0x6c, 0x20,
// 'url' type
0x00,
// version 0
0x00, 0x00, 0x01 // entry_flags
]);
const stco = new Uint8Array([0x00,
// version
0x00, 0x00, 0x00,
// flags
0x00, 0x00, 0x00, 0x00 // entry_count
]);
MP4.STTS = MP4.STSC = MP4.STCO = stco;
MP4.STSZ = new Uint8Array([0x00,
// version
0x00, 0x00, 0x00,
// flags
0x00, 0x00, 0x00, 0x00,
// sample_size
0x00, 0x00, 0x00, 0x00 // sample_count
]);
MP4.VMHD = new Uint8Array([0x00,
// version
0x00, 0x00, 0x01,
// flags
0x00, 0x00,
// graphicsmode
0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // opcolor
]);
MP4.SMHD = new Uint8Array([0x00,
// version
0x00, 0x00, 0x00,
// flags
0x00, 0x00,
// balance
0x00, 0x00 // reserved
]);
MP4.STSD = new Uint8Array([0x00,
// version 0
0x00, 0x00, 0x00,
// flags
0x00, 0x00, 0x00, 0x01]); // entry_count
const majorBrand = new Uint8Array([105, 115, 111, 109]); // isom
const avc1Brand = new Uint8Array([97, 118, 99, 49]); // avc1
const minorVersion = new Uint8Array([0, 0, 0, 1]);
MP4.FTYP = MP4.box(MP4.types.ftyp, majorBrand, minorVersion, majorBrand, avc1Brand);
MP4.DINF = MP4.box(MP4.types.dinf, MP4.box(MP4.types.dref, dref));
}
static box(type, ...payload) {
let size = 8;
let i = payload.length;
const len = i;
// calculate the total size we need to allocate
while (i--) {
size += payload[i].byteLength;
}
const result = new Uint8Array(size);
result[0] = size >> 24 & 0xff;
result[1] = size >> 16 & 0xff;
result[2] = size >> 8 & 0xff;
result[3] = size & 0xff;
result.set(type, 4);
// copy the payload into the result
for (i = 0, size = 8; i < len; i++) {
// copy payload[i] array @ offset size
result.set(payload[i], size);
size += payload[i].byteLength;
}
return result;
}
static hdlr(type) {
return MP4.box(MP4.types.hdlr, MP4.HDLR_TYPES[type]);
}
static mdat(data) {
return MP4.box(MP4.types.mdat, data);
}
static mdhd(timescale, duration) {
duration *= timescale;
const upperWordDuration = Math.floor(duration / (UINT32_MAX + 1));
const lowerWordDuration = Math.floor(duration % (UINT32_MAX + 1));
return MP4.box(MP4.types.mdhd, new Uint8Array([0x01,
// version 1
0x00, 0x00, 0x00,
// flags
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
// creation_time
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
// modification_time
timescale >> 24 & 0xff, timescale >> 16 & 0xff, timescale >> 8 & 0xff, timescale & 0xff,
// timescale
upperWordDuration >> 24, upperWordDuration >> 16 & 0xff, upperWordDuration >> 8 & 0xff, upperWordDuration & 0xff, lowerWordDuration >> 24, lowerWordDuration >> 16 & 0xff, lowerWordDuration >> 8 & 0xff, lowerWordDuration & 0xff, 0x55, 0xc4,
// 'und' language (undetermined)
0x00, 0x00]));
}
static mdia(track) {
return MP4.box(MP4.types.mdia, MP4.mdhd(track.timescale, track.duration), MP4.hdlr(track.type), MP4.minf(track));
}
static mfhd(sequenceNumber) {
return MP4.box(MP4.types.mfhd, new Uint8Array([0x00, 0x00, 0x00, 0x00,
// flags
sequenceNumber >> 24, sequenceNumber >> 16 & 0xff, sequenceNumber >> 8 & 0xff, sequenceNumber & 0xff // sequence_number
]));
}
static minf(track) {
if (track.type === 'audio') {
return MP4.box(MP4.types.minf, MP4.box(MP4.types.smhd, MP4.SMHD), MP4.DINF, MP4.stbl(track));
} else {
return MP4.box(MP4.types.minf, MP4.box(MP4.types.vmhd, MP4.VMHD), MP4.DINF, MP4.stbl(track));
}
}
static moof(sn, baseMediaDecodeTime, track) {
return MP4.box(MP4.types.moof, MP4.mfhd(sn), MP4.traf(track, baseMediaDecodeTime));
}
static moov(tracks) {
let i = tracks.length;
const boxes = [];
while (i--) {
boxes[i] = MP4.trak(tracks[i]);
}
return MP4.box.apply(null, [MP4.types.moov, MP4.mvhd(tracks[0].timescale, tracks[0].duration)].concat(boxes).concat(MP4.mvex(tracks)));
}
static mvex(tracks) {
let i = tracks.length;
const boxes = [];
while (i--) {
boxes[i] = MP4.trex(tracks[i]);
}
return MP4.box.apply(null, [MP4.types.mvex, ...boxes]);
}
static mvhd(timescale, duration) {
duration *= timescale;
const upperWordDuration = Math.floor(duration / (UINT32_MAX + 1));
const lowerWordDuration = Math.floor(duration % (UINT32_MAX + 1));
const bytes = new Uint8Array([0x01,
// version 1
0x00, 0x00, 0x00,
// flags
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
// creation_time
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
// modification_time
timescale >> 24 & 0xff, timescale >> 16 & 0xff, timescale >> 8 & 0xff, timescale & 0xff,
// timescale
upperWordDuration >> 24, upperWordDuration >> 16 & 0xff, upperWordDuration >> 8 & 0xff, upperWordDuration & 0xff, lowerWordDuration >> 24, lowerWordDuration >> 16 & 0xff, lowerWordDuration >> 8 & 0xff, lowerWordDuration & 0xff, 0x00, 0x01, 0x00, 0x00,
// 1.0 rate
0x01, 0x00,
// 1.0 volume
0x00, 0x00,
// reserved
0x00, 0x00, 0x00, 0x00,
// reserved
0x00, 0x00, 0x00, 0x00,
// reserved
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
// transformation: unity matrix
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// pre_defined
0xff, 0xff, 0xff, 0xff // next_track_ID
]);
return MP4.box(MP4.types.mvhd, bytes);
}
static sdtp(track) {
const samples = track.samples || [];
const bytes = new Uint8Array(4 + samples.length);
let i;
let flags;
// leave the full box header (4 bytes) all zero
// write the sample table
for (i = 0; i < samples.length; i++) {
flags = samples[i].flags;
bytes[i + 4] = flags.dependsOn << 4 | flags.isDependedOn << 2 | flags.hasRedundancy;
}
return MP4.box(MP4.types.sdtp, bytes);
}
static stbl(track) {
return MP4.box(MP4.types.stbl, MP4.stsd(track), MP4.box(MP4.types.stts, MP4.STTS), MP4.box(MP4.types.stsc, MP4.STSC), MP4.box(MP4.types.stsz, MP4.STSZ), MP4.box(MP4.types.stco, MP4.STCO));
}
static avc1(track) {
let sps = [];
let pps = [];
let i;
let data;
let len;
// assemble the SPSs
for (i = 0; i < track.sps.length; i++) {
data = track.sps[i];
len = data.byteLength;
sps.push(len >>> 8 & 0xff);
sps.push(len & 0xff);
// SPS
sps = sps.concat(Array.prototype.slice.call(data));
}
// assemble the PPSs
for (i = 0; i < track.pps.length; i++) {
data = track.pps[i];
len = data.byteLength;
pps.push(len >>> 8 & 0xff);
pps.push(len & 0xff);
pps = pps.concat(Array.prototype.slice.call(data));
}
const avcc = MP4.box(MP4.types.avcC, new Uint8Array([0x01,
// version
sps[3],
// profile
sps[4],
// profile compat
sps[5],
// level
0xfc | 3,
// lengthSizeMinusOne, hard-coded to 4 bytes
0xe0 | track.sps.length // 3bit reserved (111) + numOfSequenceParameterSets
].concat(sps).concat([track.pps.length // numOfPictureParameterSets
]).concat(pps))); // "PPS"
const width = track.width;
const height = track.height;
const hSpacing = track.pixelRatio[0];
const vSpacing = track.pixelRatio[1];
return MP4.box(MP4.types.avc1, new Uint8Array([0x00, 0x00, 0x00,
// reserved
0x00, 0x00, 0x00,
// reserved
0x00, 0x01,
// data_reference_index
0x00, 0x00,
// pre_defined
0x00, 0x00,
// reserved
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// pre_defined
width >> 8 & 0xff, width & 0xff,
// width
height >> 8 & 0xff, height & 0xff,
// height
0x00, 0x48, 0x00, 0x00,
// horizresolution
0x00, 0x48, 0x00, 0x00,
// vertresolution
0x00, 0x00, 0x00, 0x00,
// reserved
0x00, 0x01,
// frame_count
0x12, 0x64, 0x61, 0x69, 0x6c,
// dailymotion/hls.js
0x79, 0x6d, 0x6f, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x68, 0x6c, 0x73, 0x2e, 0x6a, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// compressorname
0x00, 0x18,
// depth = 24
0x11, 0x11]),
// pre_defined = -1
avcc, MP4.box(MP4.types.btrt, new Uint8Array([0x00, 0x1c, 0x9c, 0x80,
// bufferSizeDB
0x00, 0x2d, 0xc6, 0xc0,
// maxBitrate
0x00, 0x2d, 0xc6, 0xc0])),
// avgBitrate
MP4.box(MP4.types.pasp, new Uint8Array([hSpacing >> 24,
// hSpacing
hSpacing >> 16 & 0xff, hSpacing >> 8 & 0xff, hSpacing & 0xff, vSpacing >> 24,
// vSpacing
vSpacing >> 16 & 0xff, vSpacing >> 8 & 0xff, vSpacing & 0xff])));
}
static esds(track) {
const configlen = track.config.length;
return new Uint8Array([0x00,
// version 0
0x00, 0x00, 0x00,
// flags
0x03,
// descriptor_type
0x17 + configlen,
// length
0x00, 0x01,
// es_id
0x00,
// stream_priority
0x04,
// descriptor_type
0x0f + configlen,
// length
0x40,
// codec : mpeg4_audio
0x15,
// stream_type
0x00, 0x00, 0x00,
// buffer_size
0x00, 0x00, 0x00, 0x00,
// maxBitrate
0x00, 0x00, 0x00, 0x00,
// avgBitrate
0x05 // descriptor_type
].concat([configlen]).concat(track.config).concat([0x06, 0x01, 0x02])); // GASpecificConfig)); // length + audio config descriptor
}
static mp4a(track) {
const samplerate = track.samplerate;
return MP4.box(MP4.types.mp4a, new Uint8Array([0x00, 0x00, 0x00,
// reserved
0x00, 0x00, 0x00,
// reserved
0x00, 0x01,
// data_reference_index
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// reserved
0x00, track.channelCount,
// channelcount
0x00, 0x10,
// sampleSize:16bits
0x00, 0x00, 0x00, 0x00,
// reserved2
samplerate >> 8 & 0xff, samplerate & 0xff,
//
0x00, 0x00]), MP4.box(MP4.types.esds, MP4.esds(track)));
}
static mp3(track) {
const samplerate = track.samplerate;
return MP4.box(MP4.types['.mp3'], new Uint8Array([0x00, 0x00, 0x00,
// reserved
0x00, 0x00, 0x00,
// reserved
0x00, 0x01,
// data_reference_index
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// reserved
0x00, track.channelCount,
// channelcount
0x00, 0x10,
// sampleSize:16bits
0x00, 0x00, 0x00, 0x00,
// reserved2
samplerate >> 8 & 0xff, samplerate & 0xff,
//
0x00, 0x00]));
}
static stsd(track) {
if (track.type === 'audio') {
if (track.segmentCodec === 'mp3' && track.codec === 'mp3') {
return MP4.box(MP4.types.stsd, MP4.STSD, MP4.mp3(track));
}
return MP4.box(MP4.types.stsd, MP4.STSD, MP4.mp4a(track));
} else {
return MP4.box(MP4.types.stsd, MP4.STSD, MP4.avc1(track));
}
}
static tkhd(track) {
const id = track.id;
const duration = track.duration * track.timescale;
const width = track.width;
const height = track.height;
const upperWordDuration = Math.floor(duration / (UINT32_MAX + 1));
const lowerWordDuration = Math.floor(duration % (UINT32_MAX + 1));
return MP4.box(MP4.types.tkhd, new Uint8Array([0x01,
// version 1
0x00, 0x00, 0x07,
// flags
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
// creation_time
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
// modification_time
id >> 24 & 0xff, id >> 16 & 0xff, id >> 8 & 0xff, id & 0xff,
// track_ID
0x00, 0x00, 0x00, 0x00,
// reserved
upperWordDuration >> 24, upperWordDuration >> 16 & 0xff, upperWordDuration >> 8 & 0xff, upperWordDuration & 0xff, lowerWordDuration >> 24, lowerWordDuration >> 16 & 0xff, lowerWordDuration >> 8 & 0xff, lowerWordDuration & 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// reserved
0x00, 0x00,
// layer
0x00, 0x00,
// alternate_group
0x00, 0x00,
// non-audio track volume
0x00, 0x00,
// reserved
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
// transformation: unity matrix
width >> 8 & 0xff, width & 0xff, 0x00, 0x00,
// width
height >> 8 & 0xff, height & 0xff, 0x00, 0x00 // height
]));
}
static traf(track, baseMediaDecodeTime) {
const sampleDependencyTable = MP4.sdtp(track);
const id = track.id;
const upperWordBaseMediaDecodeTime = Math.floor(baseMediaDecodeTime / (UINT32_MAX + 1));
const lowerWordBaseMediaDecodeTime = Math.floor(baseMediaDecodeTime % (UINT32_MAX + 1));
return MP4.box(MP4.types.traf, MP4.box(MP4.types.tfhd, new Uint8Array([0x00,
// version 0
0x00, 0x00, 0x00,
// flags
id >> 24, id >> 16 & 0xff, id >> 8 & 0xff, id & 0xff // track_ID
])), MP4.box(MP4.types.tfdt, new Uint8Array([0x01,
// version 1
0x00, 0x00, 0x00,
// flags
upperWordBaseMediaDecodeTime >> 24, upperWordBaseMediaDecodeTime >> 16 & 0xff, upperWordBaseMediaDecodeTime >> 8 & 0xff, upperWordBaseMediaDecodeTime & 0xff, lowerWordBaseMediaDecodeTime >> 24, lowerWordBaseMediaDecodeTime >> 16 & 0xff, lowerWordBaseMediaDecodeTime >> 8 & 0xff, lowerWordBaseMediaDecodeTime & 0xff])), MP4.trun(track, sampleDependencyTable.length + 16 +
// tfhd
20 +
// tfdt
8 +
// traf header
16 +
// mfhd
8 +
// moof header
8),
// mdat header
sampleDependencyTable);
}
/**
* Generate a track box.
* @param track a track definition
*/
static trak(track) {
track.duration = track.duration || 0xffffffff;
return MP4.box(MP4.types.trak, MP4.tkhd(track), MP4.mdia(track));
}
static trex(track) {
const id = track.id;
return MP4.box(MP4.types.trex, new Uint8Array([0x00,
// version 0
0x00, 0x00, 0x00,
// flags
id >> 24, id >> 16 & 0xff, id >> 8 & 0xff, id & 0xff,
// track_ID
0x00, 0x00, 0x00, 0x01,
// default_sample_description_index
0x00, 0x00, 0x00, 0x00,
// default_sample_duration
0x00, 0x00, 0x00, 0x00,
// default_sample_size
0x00, 0x01, 0x00, 0x01 // default_sample_flags
]));
}
static trun(track, offset) {
const samples = track.samples || [];
const len = samples.length;
const arraylen = 12 + 16 * len;
const array = new Uint8Array(arraylen);
let i;
let sample;
let duration;
let size;
let flags;
let cts;
offset += 8 + arraylen;
array.set([track.type === 'video' ? 0x01 : 0x00,
// version 1 for video with signed-int sample_composition_time_offset
0x00, 0x0f, 0x01,
// flags
len >>> 24 & 0xff, len >>> 16 & 0xff, len >>> 8 & 0xff, len & 0xff,
// sample_count
offset >>> 24 & 0xff, offset >>> 16 & 0xff, offset >>> 8 & 0xff, offset & 0xff // data_offset
], 0);
for (i = 0; i < len; i++) {
sample = samples[i];
duration = sample.duration;
size = sample.size;
flags = sample.flags;
cts = sample.cts;
array.set([duration >>> 24 & 0xff, duration >>> 16 & 0xff, duration >>> 8 & 0xff, duration & 0xff,
// sample_duration
size >>> 24 & 0xff, size >>> 16 & 0xff, size >>> 8 & 0xff, size & 0xff,
// sample_size
flags.isLeading << 2 | flags.dependsOn, flags.isDependedOn << 6 | flags.hasRedundancy << 4 | flags.paddingValue << 1 | flags.isNonSync, flags.degradPrio & 0xf0 << 8, flags.degradPrio & 0x0f,
// sample_flags
cts >>> 24 & 0xff, cts >>> 16 & 0xff, cts >>> 8 & 0xff, cts & 0xff // sample_composition_time_offset
], 12 + 16 * i);
}
return MP4.box(MP4.types.trun, array);
}
static initSegment(tracks) {
if (!MP4.types) {
MP4.init();
}
const movie = MP4.moov(tracks);
const result = new Uint8Array(MP4.FTYP.byteLength + movie.byteLength);
result.set(MP4.FTYP);
result.set(movie, MP4.FTYP.byteLength);
return result;
}
}
MP4.types = void 0;
MP4.HDLR_TYPES = void 0;
MP4.STTS = void 0;
MP4.STSC = void 0;
MP4.STCO = void 0;
MP4.STSZ = void 0;
MP4.VMHD = void 0;
MP4.SMHD = void 0;
MP4.STSD = void 0;
MP4.FTYP = void 0;
MP4.DINF = void 0;
const MPEG_TS_CLOCK_FREQ_HZ = 90000;
function toTimescaleFromBase(baseTime, destScale, srcBase = 1, round = false) {
const result = baseTime * destScale * srcBase; // equivalent to `(value * scale) / (1 / base)`
return round ? Math.round(result) : result;
}
function toTimescaleFromScale(baseTime, destScale, srcScale = 1, round = false) {
return toTimescaleFromBase(baseTime, destScale, 1 / srcScale, round);
}
function toMsFromMpegTsClock(baseTime, round = false) {
return toTimescaleFromBase(baseTime, 1000, 1 / MPEG_TS_CLOCK_FREQ_HZ, round);
}
function toMpegTsClockFromTimescale(baseTime, srcScale = 1) {
return toTimescaleFromBase(baseTime, MPEG_TS_CLOCK_FREQ_HZ, 1 / srcScale);
}
const MAX_SILENT_FRAME_DURATION = 10 * 1000; // 10 seconds
const AAC_SAMPLES_PER_FRAME = 1024;
const MPEG_AUDIO_SAMPLE_PER_FRAME = 1152;
let chromeVersion = null;
let safariWebkitVersion = null;
class MP4Remuxer {
constructor(observer, config, typeSupported, vendor = '') {
this.observer = void 0;
this.config = void 0;
this.typeSupported = void 0;
this.ISGenerated = false;
this._initPTS = null;
this._initDTS = null;
this.nextAvcDts = null;
this.nextAudioPts = null;
this.videoSampleDuration = null;
this.isAudioContiguous = false;
this.isVideoContiguous = false;
this.observer = observer;
this.config = config;
this.typeSupported = typeSupported;
this.ISGenerated = false;
if (chromeVersion === null) {
const userAgent = navigator.userAgent || '';
const result = userAgent.match(/Chrome\/(\d+)/i);
chromeVersion = result ? parseInt(result[1]) : 0;
}
if (safariWebkitVersion === null) {
const result = navigator.userAgent.match(/Safari\/(\d+)/i);
safariWebkitVersion = result ? parseInt(result[1]) : 0;
}
}
destroy() {}
resetTimeStamp(defaultTimeStamp) {
logger.log('[mp4-remuxer]: initPTS & initDTS reset');
this._initPTS = this._initDTS = defaultTimeStamp;
}
resetNextTimestamp() {
logger.log('[mp4-remuxer]: reset next timestamp');
this.isVideoContiguous = false;
this.isAudioContiguous = false;
}
resetInitSegment() {
logger.log('[mp4-remuxer]: ISGenerated flag reset');
this.ISGenerated = false;
}
getVideoStartPts(videoSamples) {
let rolloverDetected = false;
const startPTS = videoSamples.reduce((minPTS, sample) => {
const delta = sample.pts - minPTS;
if (delta < -4294967296) {
// 2^32, see PTSNormalize for reasoning, but we're hitting a rollover here, and we don't want that to impact the timeOffset calculation
rolloverDetected = true;
return normalizePts(minPTS, sample.pts);
} else if (delta > 0) {
return minPTS;
} else {
return sample.pts;
}
}, videoSamples[0].pts);
if (rolloverDetected) {
logger.debug('PTS rollover detected');
}
return startPTS;
}
remux(audioTrack, videoTrack, id3Track, textTrack, timeOffset, accurateTimeOffset, flush, playlistType) {
let video;
let audio;
let initSegment;
let text;
let id3;
let independent;
let audioTimeOffset = timeOffset;
let videoTimeOffset = timeOffset;
// If we're remuxing audio and video progressively, wait until we've received enough samples for each track before proceeding.
// This is done to synchronize the audio and video streams. We know if the current segment will have samples if the "pid"
// parameter is greater than -1. The pid is set when the PMT is parsed, which contains the tracks list.
// However, if the initSegment has already been generated, or we've reached the end of a segment (flush),
// then we can remux one track without waiting for the other.
const hasAudio = audioTrack.pid > -1;
const hasVideo = videoTrack.pid > -1;
const length = videoTrack.samples.length;
const enoughAudioSamples = audioTrack.samples.length > 0;
const enoughVideoSamples = flush && length > 0 || length > 1;
const canRemuxAvc = (!hasAudio || enoughAudioSamples) && (!hasVideo || enoughVideoSamples) || this.ISGenerated || flush;
if (canRemuxAvc) {
if (!this.ISGenerated) {
initSegment = this.generateIS(audioTrack, videoTrack, timeOffset, accurateTimeOffset);
}
const isVideoContiguous = this.isVideoContiguous;
let firstKeyFrameIndex = -1;
let firstKeyFramePTS;
if (enoughVideoSamples) {
firstKeyFrameIndex = findKeyframeIndex(videoTrack.samples);
if (!isVideoContiguous && this.config.forceKeyFrameOnDiscontinuity) {
independent = true;
if (firstKeyFrameIndex > 0) {
logger.warn(`[mp4-remuxer]: Dropped ${firstKeyFrameIndex} out of ${length} video samples due to a missing keyframe`);
const startPTS = this.getVideoStartPts(videoTrack.samples);
videoTrack.samples = videoTrack.samples.slice(firstKeyFrameIndex);
videoTrack.dropped += firstKeyFrameIndex;
videoTimeOffset += (videoTrack.samples[0].pts - startPTS) / videoTrack.inputTimeScale;
firstKeyFramePTS = videoTimeOffset;
} else if (firstKeyFrameIndex === -1) {
logger.warn(`[mp4-remuxer]: No keyframe found out of ${length} video samples`);
independent = false;
}
}
}
if (this.ISGenerated) {
if (enoughAudioSamples && enoughVideoSamples) {
// timeOffset is expected to be the offset of the first timestamp of this fragment (first DTS)
// if first audio DTS is not aligned with first video DTS then we need to take that into account
// when providing timeOffset to remuxAudio / remuxVideo. if we don't do that, there might be a permanent / small
// drift between audio and video streams
const startPTS = this.getVideoStartPts(videoTrack.samples);
const tsDelta = normalizePts(audioTrack.samples[0].pts, startPTS) - startPTS;
const audiovideoTimestampDelta = tsDelta / videoTrack.inputTimeScale;
audioTimeOffset += Math.max(0, audiovideoTimestampDelta);
videoTimeOffset += Math.max(0, -audiovideoTimestampDelta);
}
// Purposefully remuxing audio before video, so that remuxVideo can use nextAudioPts, which is calculated in remuxAudio.
if (enoughAudioSamples) {
// if initSegment was generated without audio samples, regenerate it again
if (!audioTrack.samplerate) {
logger.warn('[mp4-remuxer]: regenerate InitSegment as audio detected');
initSegment = this.generateIS(audioTrack, videoTrack, timeOffset, accurateTimeOffset);
}
audio = this.remuxAudio(audioTrack, audioTimeOffset, this.isAudioContiguous, accurateTimeOffset, hasVideo || enoughVideoSamples || playlistType === PlaylistLevelType.AUDIO ? videoTimeOffset : undefined);
if (enoughVideoSamples) {
const audioTrackLength = audio ? audio.endPTS - audio.startPTS : 0;
// if initSegment was generated without video samples, regenerate it again
if (!videoTrack.inputTimeScale) {
logger.warn('[mp4-remuxer]: regenerate InitSegment as video detected');
initSegment = this.generateIS(audioTrack, videoTrack, timeOffset, accurateTimeOffset);
}
video = this.remuxVideo(videoTrack, videoTimeOffset, isVideoContiguous, audioTrackLength);
}
} else if (enoughVideoSamples) {
video = this.remuxVideo(videoTrack, videoTimeOffset, isVideoContiguous, 0);
}
if (video) {
video.firstKeyFrame = firstKeyFrameIndex;
video.independent = firstKeyFrameIndex !== -1;
video.firstKeyFramePTS = firstKeyFramePTS;
}
}
}
// Allow ID3 and text to remux, even if more audio/video samples are required
if (this.ISGenerated && this._initPTS && this._initDTS) {
if (id3Track.samples.length) {
id3 = flushTextTrackMetadataCueSamples(id3Track, timeOffset, this._initPTS, this._initDTS);
}
if (textTrack.samples.length) {
text = flushTextTrackUserdataCueSamples(textTrack, timeOffset, this._initPTS);
}
}
return {
audio,
video,
initSegment,
independent,
text,
id3
};
}
generateIS(audioTrack, videoTrack, timeOffset, accurateTimeOffset) {
const audioSamples = audioTrack.samples;
const videoSamples = videoTrack.samples;
const typeSupported = this.typeSupported;
const tracks = {};
const _initPTS = this._initPTS;
let computePTSDTS = !_initPTS || accurateTimeOffset;
let container = 'audio/mp4';
let initPTS;
let initDTS;
let timescale;
if (computePTSDTS) {
initPTS = initDTS = Infinity;
}
if (audioTrack.config && audioSamples.length) {
// let's use audio sampling rate as MP4 time scale.
// rationale is that there is a integer nb of audio frames per audio sample (1024 for AAC)
// using audio sampling rate here helps having an integer MP4 frame duration
// this avoids potential rounding issue and AV sync issue
audioTrack.timescale = audioTrack.samplerate;
switch (audioTrack.segmentCodec) {
case 'mp3':
if (typeSupported.mpeg) {
// Chrome and Safari
container = 'audio/mpeg';
audioTrack.codec = '';
} else if (typeSupported.mp3) {
// Firefox
audioTrack.codec = 'mp3';
}
break;
}
tracks.audio = {
id: 'audio',
container: container,
codec: audioTrack.codec,
initSegment: audioTrack.segmentCodec === 'mp3' && typeSupported.mpeg ? new Uint8Array(0) : MP4.initSegment([audioTrack]),
metadata: {
channelCount: audioTrack.channelCount
}
};
if (computePTSDTS) {
timescale = audioTrack.inputTimeScale;
if (!_initPTS || timescale !== _initPTS.timescale) {
// remember first PTS of this demuxing context. for audio, PTS = DTS
initPTS = initDTS = audioSamples[0].pts - Math.round(timescale * timeOffset);
} else {
computePTSDTS = false;
}
}
}
if (videoTrack.sps && videoTrack.pps && videoSamples.length) {
// let's use input time scale as MP4 video timescale
// we use input time scale straight away to avoid rounding issues on frame duration / cts computation
videoTrack.timescale = videoTrack.inputTimeScale;
tracks.video = {
id: 'main',
container: 'video/mp4',
codec: videoTrack.codec,
initSegment: MP4.initSegment([videoTrack]),
metadata: {
width: videoTrack.width,
height: videoTrack.height
}
};
if (computePTSDTS) {
timescale = videoTrack.inputTimeScale;
if (!_initPTS || timescale !== _initPTS.timescale) {
const startPTS = this.getVideoStartPts(videoSamples);
const startOffset = Math.round(timescale * timeOffset);
initDTS = Math.min(initDTS, normalizePts(videoSamples[0].dts, startPTS) - startOffset);
initPTS = Math.min(initPTS, startPTS - startOffset);
} else {
computePTSDTS = false;
}
}
}
if (Object.keys(tracks).length) {
this.ISGenerated = true;
if (computePTSDTS) {
this._initPTS = {
baseTime: initPTS,
timescale: timescale
};
this._initDTS = {
baseTime: initDTS,
timescale: timescale
};
} else {
initPTS = timescale = undefined;
}
return {
tracks,
initPTS,
timescale
};
}
}
remuxVideo(track, timeOffset, contiguous, audioTrackLength) {
const timeScale = track.inputTimeScale;
const inputSamples = track.samples;
const outputSamples = [];
const nbSamples = inputSamples.length;
const initPTS = this._initPTS;
let nextAvcDts = this.nextAvcDts;
let offset = 8;
let mp4SampleDuration = this.videoSampleDuration;
let firstDTS;
let lastDTS;
let minPTS = Number.POSITIVE_INFINITY;
let maxPTS = Number.NEGATIVE_INFINITY;
let sortSamples = false;
// if parsed fragment is contiguous with last one, let's use last DTS value as reference
if (!contiguous || nextAvcDts === null) {
const pts = timeOffset * timeScale;
const cts = inputSamples[0].pts - normalizePts(inputSamples[0].dts, inputSamples[0].pts);
// if not contiguous, let's use target timeOffset
nextAvcDts = pts - cts;
}
// PTS is coded on 33bits, and can loop from -2^32 to 2^32
// PTSNormalize will make PTS/DTS value monotonic, we use last known DTS value as reference value
const initTime = initPTS.baseTime * timeScale / initPTS.timescale;
for (let i = 0; i < nbSamples; i++) {
const sample = inputSamples[i];
sample.pts = normalizePts(sample.pts - initTime, nextAvcDts);
sample.dts = normalizePts(sample.dts - initTime, nextAvcDts);
if (sample.dts < inputSamples[i > 0 ? i - 1 : i].dts) {
sortSamples = true;
}
}
// sort video samples by DTS then PTS then demux id order
if (sortSamples) {
inputSamples.sort(function (a, b) {
const deltadts = a.dts - b.dts;
const deltapts = a.pts - b.pts;
return deltadts || deltapts;
});
}
// Get first/last DTS
firstDTS = inputSamples[0].dts;
lastDTS = inputSamples[inputSamples.length - 1].dts;
// Sample duration (as expected by trun MP4 boxes), should be the delta between sample DTS
// set this constant duration as being the avg delta between consecutive DTS.
const inputDuration = lastDTS - firstDTS;
const averageSampleDuration = inputDuration ? Math.round(inputDuration / (nbSamples - 1)) : mp4SampleDuration || track.inputTimeScale / 30;
// if fragment are contiguous, detect hole/overlapping between fragments
if (contiguous) {
// check timestamp continuity across consecutive fragments (this is to remove inter-fragment gap/hole)
const delta = firstDTS - nextAvcDts;
const foundHole = delta > averageSampleDuration;
const foundOverlap = delta < -1;
if (foundHole || foundOverlap) {
if (foundHole) {
logger.warn(`AVC: ${toMsFromMpegTsClock(delta, true)} ms (${delta}dts) hole between fragments detected, filling it`);
} else {
logger.warn(`AVC: ${toMsFromMpegTsClock(-delta, true)} ms (${delta}dts) overlapping between fragments detected`);
}
if (!foundOverlap || nextAvcDts >= inputSamples[0].pts) {
firstDTS = nextAvcDts;
const firstPTS = inputSamples[0].pts - delta;
inputSamples[0].dts = firstDTS;
inputSamples[0].pts = firstPTS;
logger.log(`Video: First PTS/DTS adjusted: ${toMsFromMpegTsClock(firstPTS, true)}/${toMsFromMpegTsClock(firstDTS, true)}, delta: ${toMsFromMpegTsClock(delta, true)} ms`);
}
}
}
firstDTS = Math.max(0, firstDTS);
let nbNalu = 0;
let naluLen = 0;
for (let i = 0; i < nbSamples; i++) {
// compute total/avc sample length and nb of NAL units
const sample = inputSamples[i];
const units = sample.units;
const nbUnits = units.length;
let sampleLen = 0;
for (let j = 0; j < nbUnits; j++) {
sampleLen += units[j].data.length;
}
naluLen += sampleLen;
nbNalu += nbUnits;
sample.length = sampleLen;
// ensure sample monotonic DTS
sample.dts = Math.max(sample.dts, firstDTS);
minPTS = Math.min(sample.pts, minPTS);
maxPTS = Math.max(sample.pts, maxPTS);
}
lastDTS = inputSamples[nbSamples - 1].dts;
/* concatenate the video data and construct the mdat in place
(need 8 more bytes to fill length and mpdat type) */
const mdatSize = naluLen + 4 * nbNalu + 8;
let mdat;
try {
mdat = new Uint8Array(mdatSize);
} catch (err) {
this.observer.emit(Events.ERROR, Events.ERROR, {
type: ErrorTypes.MUX_ERROR,
details: ErrorDetails.REMUX_ALLOC_ERROR,
fatal: false,
error: err,
bytes: mdatSize,
reason: `fail allocating video mdat ${mdatSize}`
});
return;
}
const view = new DataView(mdat.buffer);
view.setUint32(0, mdatSize);
mdat.set(MP4.types.mdat, 4);
let stretchedLastFrame = false;
let minDtsDelta = Number.POSITIVE_INFINITY;
let minPtsDelta = Number.POSITIVE_INFINITY;
let maxDtsDelta = Number.NEGATIVE_INFINITY;
let maxPtsDelta = Number.NEGATIVE_INFINITY;
for (let i = 0; i < nbSamples; i++) {
const avcSample = inputSamples[i];
const avcSampleUnits = avcSample.units;
let mp4SampleLength = 0;
// convert NALU bitstream to MP4 format (prepend NALU with size field)
for (let j = 0, nbUnits = avcSampleUnits.length; j < nbUnits; j++) {
const unit = avcSampleUnits[j];
const unitData = unit.data;
const unitDataLen = unit.data.byteLength;
view.setUint32(offset, unitDataLen);
offset += 4;
mdat.set(unitData, offset);
offset += unitDataLen;
mp4SampleLength += 4 + unitDataLen;
}
// expected sample duration is the Decoding Timestamp diff of consecutive samples
let ptsDelta;
if (i < nbSamples - 1) {
mp4SampleDuration = inputSamples[i + 1].dts - avcSample.dts;
ptsDelta = inputSamples[i + 1].pts - avcSample.pts;
} else {
const config = this.config;
const lastFrameDuration = i > 0 ? avcSample.dts - inputSamples[i - 1].dts : averageSampleDuration;
ptsDelta = i > 0 ? avcSample.pts - inputSamples[i - 1].pts : averageSampleDuration;
if (config.stretchShortVideoTrack && this.nextAudioPts !== null) {
// In some cases, a segment's audio track duration may exceed the video track duration.
// Since we've already remuxed audio, and we know how long the audio track is, we look to
// see if the delta to the next segment is longer than maxBufferHole.
// If so, playback would potentially get stuck, so we artificially inflate
// the duration of the last frame to minimize any potential gap between segments.
const gapTolerance = Math.floor(config.maxBufferHole * timeScale);
const deltaToFrameEnd = (audioTrackLength ? minPTS + audioTrackLength * timeScale : this.nextAudioPts) - avcSample.pts;
if (deltaToFrameEnd > gapTolerance) {
// We subtract lastFrameDuration from deltaToFrameEnd to try to prevent any video
// frame overlap. maxBufferHole should be >> lastFrameDuration anyway.
mp4SampleDuration = deltaToFrameEnd - lastFrameDuration;
if (mp4SampleDuration < 0) {
mp4SampleDuration = lastFrameDuration;
} else {
stretchedLastFrame = true;
}
logger.log(`[mp4-remuxer]: It is approximately ${deltaToFrameEnd / 90} ms to the next segment; using duration ${mp4SampleDuration / 90} ms for the last video frame.`);
} else {
mp4SampleDuration = lastFrameDuration;
}
} else {
mp4SampleDuration = lastFrameDuration;
}
}
const compositionTimeOffset = Math.round(avcSample.pts - avcSample.dts);
minDtsDelta = Math.min(minDtsDelta, mp4SampleDuration);
maxDtsDelta = Math.max(maxDtsDelta, mp4SampleDuration);
minPtsDelta = Math.min(minPtsDelta, ptsDelta);
maxPtsDelta = Math.max(maxPtsDelta, ptsDelta);
outputSamples.push(new Mp4Sample(avcSample.key, mp4SampleDuration, mp4SampleLength, compositionTimeOffset));
}
if (outputSamples.length) {
if (chromeVersion) {
if (chromeVersion < 70) {
// Chrome workaround, mark first sample as being a Random Access Point (keyframe) to avoid sourcebuffer append issue
// https://code.google.com/p/chromium/issues/detail?id=229412
const flags = outputSamples[0].flags;
flags.dependsOn = 2;
flags.isNonSync = 0;
}
} else if (safariWebkitVersion) {
// Fix for "CNN special report, with CC" in test-streams (Safari browser only)
// Ignore DTS when frame durations are irregular. Safari MSE does not handle this leading to gaps.
if (maxPtsDelta - minPtsDelta < maxDtsDelta - minDtsDelta && averageSampleDuration / maxDtsDelta < 0.025 && outputSamples[0].cts === 0) {
logger.warn('Found irregular gaps in sample duration. Using PTS instead of DTS to determine MP4 sample duration.');
let dts = firstDTS;
for (let i = 0, len = outputSamples.length; i < len; i++) {
const nextDts = dts + outputSamples[i].duration;
const pts = dts + outputSamples[i].cts;
if (i < len - 1) {
const nextPts = nextDts + outputSamples[i + 1].cts;
outputSamples[i].duration = nextPts - pts;
} else {
outputSamples[i].duration = i ? outputSamples[i - 1].duration : averageSampleDuration;
}
outputSamples[i].cts = 0;
dts = nextDts;
}
}
}
}
// next AVC sample DTS should be equal to last sample DTS + last sample duration (in PES timescale)
mp4SampleDuration = stretchedLastFrame || !mp4SampleDuration ? averageSampleDuration : mp4SampleDuration;
this.nextAvcDts = nextAvcDts = lastDTS + mp4SampleDuration;
this.videoSampleDuration = mp4SampleDuration;
this.isVideoContiguous = true;
const moof = MP4.moof(track.sequenceNumber++, firstDTS, _extends({}, track, {
samples: outputSamples
}));
const type = 'video';
const data = {
data1: moof,
data2: mdat,
startPTS: minPTS / timeScale,
endPTS: (maxPTS + mp4SampleDuration) / timeScale,
startDTS: firstDTS / timeScale,
endDTS: nextAvcDts / timeScale,
type,
hasAudio: false,
hasVideo: true,
nb: outputSamples.length,
dropped: track.dropped
};
track.samples = [];
track.dropped = 0;
return data;
}
remuxAudio(track, timeOffset, contiguous, accurateTimeOffset, videoTimeOffset) {
const inputTimeScale = track.inputTimeScale;
const mp4timeScale = track.samplerate ? track.samplerate : inputTimeScale;
const scaleFactor = inputTimeScale / mp4timeScale;
const mp4SampleDuration = track.segmentCodec === 'aac' ? AAC_SAMPLES_PER_FRAME : MPEG_AUDIO_SAMPLE_PER_FRAME;
const inputSampleDuration = mp4SampleDuration * scaleFactor;
const initPTS = this._initPTS;
const rawMPEG = track.segmentCodec === 'mp3' && this.typeSupported.mpeg;
const outputSamples = [];
const alignedWithVideo = videoTimeOffset !== undefined;
let inputSamples = track.samples;
let offset = rawMPEG ? 0 : 8;
let nextAudioPts = this.nextAudioPts || -1;
// window.audioSamples ? window.audioSamples.push(inputSamples.map(s => s.pts)) : (window.audioSamples = [inputSamples.map(s => s.pts)]);
// for audio samples, also consider consecutive fragments as being contiguous (even if a level switch occurs),
// for sake of clarity:
// consecutive fragments are frags with
// - less than 100ms gaps between new time offset (if accurate) and next expected PTS OR
// - less than 20 audio frames distance
// contiguous fragments are consecutive fragments from same quality level (same level, new SN = old SN + 1)
// this helps ensuring audio continuity
// and this also avoids audio glitches/cut when switching quality, or reporting wrong duration on first audio frame
const timeOffsetMpegTS = timeOffset * inputTimeScale;
const initTime = initPTS.baseTime * inputTimeScale / initPTS.timescale;
this.isAudioContiguous = contiguous = contiguous || inputSamples.length && nextAudioPts > 0 && (accurateTimeOffset && Math.abs(timeOffsetMpegTS - nextAudioPts) < 9000 || Math.abs(normalizePts(inputSamples[0].pts - initTime, timeOffsetMpegTS) - nextAudioPts) < 20 * inputSampleDuration);
// compute normalized PTS
inputSamples.forEach(function (sample) {
sample.pts = normalizePts(sample.pts - initTime, timeOffsetMpegTS);
});
if (!contiguous || nextAudioPts < 0) {
// filter out sample with negative PTS that are not playable anyway
// if we don't remove these negative samples, they will shift all audio samples forward.
// leading to audio overlap between current / next fragment
inputSamples = inputSamples.filter(sample => sample.pts >= 0);
// in case all samples have negative PTS, and have been filtered out, return now
if (!inputSamples.length) {
return;
}
if (videoTimeOffset === 0) {
// Set the start to 0 to match video so that start gaps larger than inputSampleDuration are filled with silence
nextAudioPts = 0;
} else if (accurateTimeOffset && !alignedWithVideo) {
// When not seeking, not live, and LevelDetails.PTSKnown, use fragment start as predicted next audio PTS
nextAudioPts = Math.max(0, timeOffsetMpegTS);
} else {
// if frags are not contiguous and if we cant trust time offset, let's use first sample PTS as next audio PTS
nextAudioPts = inputSamples[0].pts;
}
}
// If the audio track is missing samples, the frames seem to get "left-shifted" within the
// resulting mp4 segment, causing sync issues and leaving gaps at the end of the audio segment.
// In an effort to prevent this from happening, we inject frames here where there are gaps.
// When possible, we inject a silent frame; when that's not possible, we duplicate the last
// frame.
if (track.segmentCodec === 'aac') {
const maxAudioFramesDrift = this.config.maxAudioFramesDrift;
for (let i = 0, nextPts = nextAudioPts; i < inputSamples.length; i++) {
// First, let's see how far off this frame is from where we expect it to be
const sample = inputSamples[i];
const pts = sample.pts;
const delta = pts - nextPts;
const duration = Math.abs(1000 * delta / inputTimeScale);
// When remuxing with video, if we're overlapping by more than a duration, drop this sample to stay in sync
if (delta <= -maxAudioFramesDrift * inputSampleDuration && alignedWithVideo) {
if (i === 0) {
logger.warn(`Audio frame @ ${(pts / inputTimeScale).toFixed(3)}s overlaps nextAudioPts by ${Math.round(1000 * delta / inputTimeScale)} ms.`);
this.nextAudioPts = nextAudioPts = nextPts = pts;
}
} // eslint-disable-line brace-style
// Insert missing frames if:
// 1: We're more than maxAudioFramesDrift frame away
// 2: Not more than MAX_SILENT_FRAME_DURATION away
// 3: currentTime (aka nextPtsNorm) is not 0
// 4: remuxing with video (videoTimeOffset !== undefined)
else if (delta >= maxAudioFramesDrift * inputSampleDuration && duration < MAX_SILENT_FRAME_DURATION && alignedWithVideo) {
let missing = Math.round(delta / inputSampleDuration);
// Adjust nextPts so that silent samples are aligned with media pts. This will prevent media samples from
// later being shifted if nextPts is based on timeOffset and delta is not a multiple of inputSampleDuration.
nextPts = pts - missing * inputSampleDuration;
if (nextPts < 0) {
missing--;
nextPts += inputSampleDuration;
}
if (i === 0) {
this.nextAudioPts = nextAudioPts = nextPts;
}
logger.warn(`[mp4-remuxer]: Injecting ${missing} audio frame @ ${(nextPts / inputTimeScale).toFixed(3)}s due to ${Math.round(1000 * delta / inputTimeScale)} ms gap.`);
for (let j = 0; j < missing; j++) {
const newStamp = Math.max(nextPts, 0);
let fillFrame = AAC$1.getSilentFrame(track.manifestCodec || track.codec, track.channelCount);
if (!fillFrame) {
logger.log('[mp4-remuxer]: Unable to get silent frame for given audio codec; duplicating last frame instead.');
fillFrame = sample.unit.subarray();
}
inputSamples.splice(i, 0, {
unit: fillFrame,
pts: newStamp
});
nextPts += inputSampleDuration;
i++;
}
}
sample.pts = nextPts;
nextPts += inputSampleDuration;
}
}
let firstPTS = null;
let lastPTS = null;
let mdat;
let mdatSize = 0;
let sampleLength = inputSamples.length;
while (sampleLength--) {
mdatSize += inputSamples[sampleLength].unit.byteLength;
}
for (let j = 0, _nbSamples = inputSamples.length; j < _nbSamples; j++) {
const audioSample = inputSamples[j];
const unit = audioSample.unit;
let pts = audioSample.pts;
if (lastPTS !== null) {
// If we have more than one sample, set the duration of the sample to the "real" duration; the PTS diff with
// the previous sample
const prevSample = outputSamples[j - 1];
prevSample.duration = Math.round((pts - lastPTS) / scaleFactor);
} else {
if (contiguous && track.segmentCodec === 'aac') {
// set PTS/DTS to expected PTS/DTS
pts = nextAudioPts;
}
// remember first PTS of our audioSamples
firstPTS = pts;
if (mdatSize > 0) {
/* concatenate the audio data and construct the mdat in place
(need 8 more bytes to fill length and mdat type) */
mdatSize += offset;
try {
mdat = new Uint8Array(mdatSize);
} catch (err) {
this.observer.emit(Events.ERROR, Events.ERROR, {
type: ErrorTypes.MUX_ERROR,
details: ErrorDetails.REMUX_ALLOC_ERROR,
fatal: false,
error: err,
bytes: mdatSize,
reason: `fail allocating audio mdat ${mdatSize}`
});
return;
}
if (!rawMPEG) {
const view = new DataView(mdat.buffer);
view.setUint32(0, mdatSize);
mdat.set(MP4.types.mdat, 4);
}
} else {
// no audio samples
return;
}
}
mdat.set(unit, offset);
const unitLen = unit.byteLength;
offset += unitLen;
// Default the sample's duration to the computed mp4SampleDuration, which will either be 1024 for AAC or 1152 for MPEG
// In the case that we have 1 sample, this will be the duration. If we have more than one sample, the duration
// becomes the PTS diff with the previous sample
outputSamples.push(new Mp4Sample(true, mp4SampleDuration, unitLen, 0));
lastPTS = pts;
}
// We could end up with no audio samples if all input samples were overlapping with the previously remuxed ones
const nbSamples = outputSamples.length;
if (!nbSamples) {
return;
}
// The next audio sample PTS should be equal to last sample PTS + duration
const lastSample = outputSamples[outputSamples.length - 1];
this.nextAudioPts = nextAudioPts = lastPTS + scaleFactor * lastSample.duration;
// Set the track samples from inputSamples to outputSamples before remuxing
const moof = rawMPEG ? new Uint8Array(0) : MP4.moof(track.sequenceNumber++, firstPTS / scaleFactor, _extends({}, track, {
samples: outputSamples
}));
// Clear the track samples. This also clears the samples array in the demuxer, since the reference is shared
track.samples = [];
const start = firstPTS / inputTimeScale;
const end = nextAudioPts / inputTimeScale;
const type = 'audio';
const audioData = {
data1: moof,
data2: mdat,
startPTS: start,
endPTS: end,
startDTS: start,
endDTS: end,
type,
hasAudio: true,
hasVideo: false,
nb: nbSamples
};
this.isAudioContiguous = true;
return audioData;
}
remuxEmptyAudio(track, timeOffset, contiguous, videoData) {
const inputTimeScale = track.inputTimeScale;
const mp4timeScale = track.samplerate ? track.samplerate : inputTimeScale;
const scaleFactor = inputTimeScale / mp4timeScale;
const nextAudioPts = this.nextAudioPts;
// sync with video's timestamp
const initDTS = this._initDTS;
const init90kHz = initDTS.baseTime * 90000 / initDTS.timescale;
const startDTS = (nextAudioPts !== null ? nextAudioPts : videoData.startDTS * inputTimeScale) + init90kHz;
const endDTS = videoData.endDTS * inputTimeScale + init90kHz;
// one sample's duration value
const frameDuration = scaleFactor * AAC_SAMPLES_PER_FRAME;
// samples count of this segment's duration
const nbSamples = Math.ceil((endDTS - startDTS) / frameDuration);
// silent frame
const silentFrame = AAC$1.getSilentFrame(track.manifestCodec || track.codec, track.channelCount);
logger.warn('[mp4-remuxer]: remux empty Audio');
// Can't remux if we can't generate a silent frame...
if (!silentFrame) {
logger.trace('[mp4-remuxer]: Unable to remuxEmptyAudio since we were unable to get a silent frame for given audio codec');
return;
}
const samples = [];
for (let i = 0; i < nbSamples; i++) {
const stamp = startDTS + i * frameDuration;
samples.push({
unit: silentFrame,
pts: stamp,
dts: stamp
});
}
track.samples = samples;
return this.remuxAudio(track, timeOffset, contiguous, false);
}
}
function normalizePts(value, reference) {
let offset;
if (reference === null) {
return value;
}
if (reference < value) {
// - 2^33
offset = -8589934592;
} else {
// + 2^33
offset = 8589934592;
}
/* PTS is 33bit (from 0 to 2^33 -1)
if diff between value and reference is bigger than half of the amplitude (2^32) then it means that
PTS looping occured. fill the gap */
while (Math.abs(value - reference) > 4294967296) {
value += offset;
}
return value;
}
function findKeyframeIndex(samples) {
for (let i = 0; i < samples.length; i++) {
if (samples[i].key) {
return i;
}
}
return -1;
}
function flushTextTrackMetadataCueSamples(track, timeOffset, initPTS, initDTS) {
const length = track.samples.length;
if (!length) {
return;
}
const inputTimeScale = track.inputTimeScale;
for (let index = 0; index < length; index++) {
const sample = track.samples[index];
// setting id3 pts, dts to relative time
// using this._initPTS and this._initDTS to calculate relative time
sample.pts = normalizePts(sample.pts - initPTS.baseTime * inputTimeScale / initPTS.timescale, timeOffset * inputTimeScale) / inputTimeScale;
sample.dts = normalizePts(sample.dts - initDTS.baseTime * inputTimeScale / initDTS.timescale, timeOffset * inputTimeScale) / inputTimeScale;
}
const samples = track.samples;
track.samples = [];
return {
samples
};
}
function flushTextTrackUserdataCueSamples(track, timeOffset, initPTS) {
const length = track.samples.length;
if (!length) {
return;
}
const inputTimeScale = track.inputTimeScale;
for (let index = 0; index < length; index++) {
const sample = track.samples[index];
// setting text pts, dts to relative time
// using this._initPTS and this._initDTS to calculate relative time
sample.pts = normalizePts(sample.pts - initPTS.baseTime * inputTimeScale / initPTS.timescale, timeOffset * inputTimeScale) / inputTimeScale;
}
track.samples.sort((a, b) => a.pts - b.pts);
const samples = track.samples;
track.samples = [];
return {
samples
};
}
class Mp4Sample {
constructor(isKeyframe, duration, size, cts) {
this.size = void 0;
this.duration = void 0;
this.cts = void 0;
this.flags = void 0;
this.duration = duration;
this.size = size;
this.cts = cts;
this.flags = new Mp4SampleFlags(isKeyframe);
}
}
class Mp4SampleFlags {
constructor(isKeyframe) {
this.isLeading = 0;
this.isDependedOn = 0;
this.hasRedundancy = 0;
this.degradPrio = 0;
this.dependsOn = 1;
this.isNonSync = 1;
this.dependsOn = isKeyframe ? 2 : 1;
this.isNonSync = isKeyframe ? 0 : 1;
}
}
class PassThroughRemuxer {
constructor() {
this.emitInitSegment = false;
this.audioCodec = void 0;
this.videoCodec = void 0;
this.initData = void 0;
this.initPTS = null;
this.initTracks = void 0;
this.lastEndTime = null;
}
destroy() {}
resetTimeStamp(defaultInitPTS) {
this.initPTS = defaultInitPTS;
this.lastEndTime = null;
}
resetNextTimestamp() {
this.lastEndTime = null;
}
resetInitSegment(initSegment, audioCodec, videoCodec, decryptdata) {
this.audioCodec = audioCodec;
this.videoCodec = videoCodec;
this.generateInitSegment(patchEncyptionData(initSegment, decryptdata));
this.emitInitSegment = true;
}
generateInitSegment(initSegment) {
let {
audioCodec,
videoCodec
} = this;
if (!(initSegment != null && initSegment.byteLength)) {
this.initTracks = undefined;
this.initData = undefined;
return;
}
const initData = this.initData = parseInitSegment(initSegment);
// Get codec from initSegment or fallback to default
if (!audioCodec) {
audioCodec = getParsedTrackCodec(initData.audio, ElementaryStreamTypes.AUDIO);
}
if (!videoCodec) {
videoCodec = getParsedTrackCodec(initData.video, ElementaryStreamTypes.VIDEO);
}
const tracks = {};
if (initData.audio && initData.video) {
tracks.audiovideo = {
container: 'video/mp4',
codec: audioCodec + ',' + videoCodec,
initSegment,
id: 'main'
};
} else if (initData.audio) {
tracks.audio = {
container: 'audio/mp4',
codec: audioCodec,
initSegment,
id: 'audio'
};
} else if (initData.video) {
tracks.video = {
container: 'video/mp4',
codec: videoCodec,
initSegment,
id: 'main'
};
} else {
logger.warn('[passthrough-remuxer.ts]: initSegment does not contain moov or trak boxes.');
}
this.initTracks = tracks;
}
remux(audioTrack, videoTrack, id3Track, textTrack, timeOffset, accurateTimeOffset) {
var _initData, _initData2;
let {
initPTS,
lastEndTime
} = this;
const result = {
audio: undefined,
video: undefined,
text: textTrack,
id3: id3Track,
initSegment: undefined
};
// If we haven't yet set a lastEndDTS, or it was reset, set it to the provided timeOffset. We want to use the
// lastEndDTS over timeOffset whenever possible; during progressive playback, the media source will not update
// the media duration (which is what timeOffset is provided as) before we need to process the next chunk.
if (!isFiniteNumber(lastEndTime)) {
lastEndTime = this.lastEndTime = timeOffset || 0;
}
// The binary segment data is added to the videoTrack in the mp4demuxer. We don't check to see if the data is only
// audio or video (or both); adding it to video was an arbitrary choice.
const data = videoTrack.samples;
if (!(data != null && data.length)) {
return result;
}
const initSegment = {
initPTS: undefined,
timescale: 1
};
let initData = this.initData;
if (!((_initData = initData) != null && _initData.length)) {
this.generateInitSegment(data);
initData = this.initData;
}
if (!((_initData2 = initData) != null && _initData2.length)) {
// We can't remux if the initSegment could not be generated
logger.warn('[passthrough-remuxer.ts]: Failed to generate initSegment.');
return result;
}
if (this.emitInitSegment) {
initSegment.tracks = this.initTracks;
this.emitInitSegment = false;
}
const duration = getDuration(data, initData);
const startDTS = getStartDTS(initData, data);
const decodeTime = startDTS === null ? timeOffset : startDTS;
if (isInvalidInitPts(initPTS, decodeTime, timeOffset, duration) || initSegment.timescale !== initPTS.timescale && accurateTimeOffset) {
initSegment.initPTS = decodeTime - timeOffset;
if (initPTS && initPTS.timescale === 1) {
logger.warn(`Adjusting initPTS by ${initSegment.initPTS - initPTS.baseTime}`);
}
this.initPTS = initPTS = {
baseTime: initSegment.initPTS,
timescale: 1
};
}
const startTime = audioTrack ? decodeTime - initPTS.baseTime / initPTS.timescale : lastEndTime;
const endTime = startTime + duration;
offsetStartDTS(initData, data, initPTS.baseTime / initPTS.timescale);
if (duration > 0) {
this.lastEndTime = endTime;
} else {
logger.warn('Duration parsed from mp4 should be greater than zero');
this.resetNextTimestamp();
}
const hasAudio = !!initData.audio;
const hasVideo = !!initData.video;
let type = '';
if (hasAudio) {
type += 'audio';
}
if (hasVideo) {
type += 'video';
}
const track = {
data1: data,
startPTS: startTime,
startDTS: startTime,
endPTS: endTime,
endDTS: endTime,
type,
hasAudio,
hasVideo,
nb: 1,
dropped: 0
};
result.audio = track.type === 'audio' ? track : undefined;
result.video = track.type !== 'audio' ? track : undefined;
result.initSegment = initSegment;
result.id3 = flushTextTrackMetadataCueSamples(id3Track, timeOffset, initPTS, initPTS);
if (textTrack.samples.length) {
result.text = flushTextTrackUserdataCueSamples(textTrack, timeOffset, initPTS);
}
return result;
}
}
function isInvalidInitPts(initPTS, startDTS, timeOffset, duration) {
if (initPTS === null) {
return true;
}
// InitPTS is invalid when distance from program would be more than segment duration or a minimum of one second
const minDuration = Math.max(duration, 1);
const startTime = startDTS - initPTS.baseTime / initPTS.timescale;
return Math.abs(startTime - timeOffset) > minDuration;
}
function getParsedTrackCodec(track, type) {
const parsedCodec = track == null ? void 0 : track.codec;
if (parsedCodec && parsedCodec.length > 4) {
return parsedCodec;
}
// Since mp4-tools cannot parse full codec string (see 'TODO: Parse codec details'... in mp4-tools)
// Provide defaults based on codec type
// This allows for some playback of some fmp4 playlists without CODECS defined in manifest
if (parsedCodec === 'hvc1' || parsedCodec === 'hev1') {
return 'hvc1.1.6.L120.90';
}
if (parsedCodec === 'av01') {
return 'av01.0.04M.08';
}
if (parsedCodec === 'avc1' || type === ElementaryStreamTypes.VIDEO) {
return 'avc1.42e01e';
}
return 'mp4a.40.5';
}
let now;
// performance.now() not available on WebWorker, at least on Safari Desktop
try {
now = self.performance.now.bind(self.performance);
} catch (err) {
logger.debug('Unable to use Performance API on this environment');
now = typeof self !== 'undefined' && self.Date.now;
}
const muxConfig = [{
demux: MP4Demuxer,
remux: PassThroughRemuxer
}, {
demux: TSDemuxer,
remux: MP4Remuxer
}, {
demux: AACDemuxer,
remux: MP4Remuxer
}, {
demux: MP3Demuxer,
remux: MP4Remuxer
}];
class Transmuxer$1 {
constructor(observer, typeSupported, config, vendor, id) {
this.async = false;
this.observer = void 0;
this.typeSupported = void 0;
this.config = void 0;
this.vendor = void 0;
this.id = void 0;
this.demuxer = void 0;
this.remuxer = void 0;
this.decrypter = void 0;
this.probe = void 0;
this.decryptionPromise = null;
this.transmuxConfig = void 0;
this.currentTransmuxState = void 0;
this.observer = observer;
this.typeSupported = typeSupported;
this.config = config;
this.vendor = vendor;
this.id = id;
}
configure(transmuxConfig) {
this.transmuxConfig = transmuxConfig;
if (this.decrypter) {
this.decrypter.reset();
}
}
push(data, decryptdata, chunkMeta, state) {
const stats = chunkMeta.transmuxing;
stats.executeStart = now();
let uintData = new Uint8Array(data);
const {
currentTransmuxState,
transmuxConfig
} = this;
if (state) {
this.currentTransmuxState = state;
}
const {
contiguous,
discontinuity,
trackSwitch,
accurateTimeOffset,
timeOffset,
initSegmentChange
} = state || currentTransmuxState;
const {
audioCodec,
videoCodec,
defaultInitPts,
duration,
initSegmentData
} = transmuxConfig;
const keyData = getEncryptionType(uintData, decryptdata);
if (keyData && keyData.method === 'AES-128') {
const decrypter = this.getDecrypter();
// Software decryption is synchronous; webCrypto is not
if (decrypter.isSync()) {
// Software decryption is progressive. Progressive decryption may not return a result on each call. Any cached
// data is handled in the flush() call
let decryptedData = decrypter.softwareDecrypt(uintData, keyData.key.buffer, keyData.iv.buffer);
// For Low-Latency HLS Parts, decrypt in place, since part parsing is expected on push progress
const loadingParts = chunkMeta.part > -1;
if (loadingParts) {
decryptedData = decrypter.flush();
}
if (!decryptedData) {
stats.executeEnd = now();
return emptyResult(chunkMeta);
}
uintData = new Uint8Array(decryptedData);
} else {
this.decryptionPromise = decrypter.webCryptoDecrypt(uintData, keyData.key.buffer, keyData.iv.buffer).then(decryptedData => {
// Calling push here is important; if flush() is called while this is still resolving, this ensures that
// the decrypted data has been transmuxed
const result = this.push(decryptedData, null, chunkMeta);
this.decryptionPromise = null;
return result;
});
return this.decryptionPromise;
}
}
const resetMuxers = this.needsProbing(discontinuity, trackSwitch);
if (resetMuxers) {
const error = this.configureTransmuxer(uintData);
if (error) {
logger.warn(`[transmuxer] ${error.message}`);
this.observer.emit(Events.ERROR, Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.FRAG_PARSING_ERROR,
fatal: false,
error,
reason: error.message
});
stats.executeEnd = now();
return emptyResult(chunkMeta);
}
}
if (discontinuity || trackSwitch || initSegmentChange || resetMuxers) {
this.resetInitSegment(initSegmentData, audioCodec, videoCodec, duration, decryptdata);
}
if (discontinuity || initSegmentChange || resetMuxers) {
this.resetInitialTimestamp(defaultInitPts);
}
if (!contiguous) {
this.resetContiguity();
}
const result = this.transmux(uintData, keyData, timeOffset, accurateTimeOffset, chunkMeta);
const currentState = this.currentTransmuxState;
currentState.contiguous = true;
currentState.discontinuity = false;
currentState.trackSwitch = false;
stats.executeEnd = now();
return result;
}
// Due to data caching, flush calls can produce more than one TransmuxerResult (hence the Array type)
flush(chunkMeta) {
const stats = chunkMeta.transmuxing;
stats.executeStart = now();
const {
decrypter,
currentTransmuxState,
decryptionPromise
} = this;
if (decryptionPromise) {
// Upon resolution, the decryption promise calls push() and returns its TransmuxerResult up the stack. Therefore
// only flushing is required for async decryption
return decryptionPromise.then(() => {
return this.flush(chunkMeta);
});
}
const transmuxResults = [];
const {
timeOffset
} = currentTransmuxState;
if (decrypter) {
// The decrypter may have data cached, which needs to be demuxed. In this case we'll have two TransmuxResults
// This happens in the case that we receive only 1 push call for a segment (either for non-progressive downloads,
// or for progressive downloads with small segments)
const decryptedData = decrypter.flush();
if (decryptedData) {
// Push always returns a TransmuxerResult if decryptdata is null
transmuxResults.push(this.push(decryptedData, null, chunkMeta));
}
}
const {
demuxer,
remuxer
} = this;
if (!demuxer || !remuxer) {
// If probing failed, then Hls.js has been given content its not able to handle
stats.executeEnd = now();
return [emptyResult(chunkMeta)];
}
const demuxResultOrPromise = demuxer.flush(timeOffset);
if (isPromise(demuxResultOrPromise)) {
// Decrypt final SAMPLE-AES samples
return demuxResultOrPromise.then(demuxResult => {
this.flushRemux(transmuxResults, demuxResult, chunkMeta);
return transmuxResults;
});
}
this.flushRemux(transmuxResults, demuxResultOrPromise, chunkMeta);
return transmuxResults;
}
flushRemux(transmuxResults, demuxResult, chunkMeta) {
const {
audioTrack,
videoTrack,
id3Track,
textTrack
} = demuxResult;
const {
accurateTimeOffset,
timeOffset
} = this.currentTransmuxState;
logger.log(`[transmuxer.ts]: Flushed fragment ${chunkMeta.sn}${chunkMeta.part > -1 ? ' p: ' + chunkMeta.part : ''} of level ${chunkMeta.level}`);
const remuxResult = this.remuxer.remux(audioTrack, videoTrack, id3Track, textTrack, timeOffset, accurateTimeOffset, true, this.id);
transmuxResults.push({
remuxResult,
chunkMeta
});
chunkMeta.transmuxing.executeEnd = now();
}
resetInitialTimestamp(defaultInitPts) {
const {
demuxer,
remuxer
} = this;
if (!demuxer || !remuxer) {
return;
}
demuxer.resetTimeStamp(defaultInitPts);
remuxer.resetTimeStamp(defaultInitPts);
}
resetContiguity() {
const {
demuxer,
remuxer
} = this;
if (!demuxer || !remuxer) {
return;
}
demuxer.resetContiguity();
remuxer.resetNextTimestamp();
}
resetInitSegment(initSegmentData, audioCodec, videoCodec, trackDuration, decryptdata) {
const {
demuxer,
remuxer
} = this;
if (!demuxer || !remuxer) {
return;
}
demuxer.resetInitSegment(initSegmentData, audioCodec, videoCodec, trackDuration);
remuxer.resetInitSegment(initSegmentData, audioCodec, videoCodec, decryptdata);
}
destroy() {
if (this.demuxer) {
this.demuxer.destroy();
this.demuxer = undefined;
}
if (this.remuxer) {
this.remuxer.destroy();
this.remuxer = undefined;
}
}
transmux(data, keyData, timeOffset, accurateTimeOffset, chunkMeta) {
let result;
if (keyData && keyData.method === 'SAMPLE-AES') {
result = this.transmuxSampleAes(data, keyData, timeOffset, accurateTimeOffset, chunkMeta);
} else {
result = this.transmuxUnencrypted(data, timeOffset, accurateTimeOffset, chunkMeta);
}
return result;
}
transmuxUnencrypted(data, timeOffset, accurateTimeOffset, chunkMeta) {
const {
audioTrack,
videoTrack,
id3Track,
textTrack
} = this.demuxer.demux(data, timeOffset, false, !this.config.progressive);
const remuxResult = this.remuxer.remux(audioTrack, videoTrack, id3Track, textTrack, timeOffset, accurateTimeOffset, false, this.id);
return {
remuxResult,
chunkMeta
};
}
transmuxSampleAes(data, decryptData, timeOffset, accurateTimeOffset, chunkMeta) {
return this.demuxer.demuxSampleAes(data, decryptData, timeOffset).then(demuxResult => {
const remuxResult = this.remuxer.remux(demuxResult.audioTrack, demuxResult.videoTrack, demuxResult.id3Track, demuxResult.textTrack, timeOffset, accurateTimeOffset, false, this.id);
return {
remuxResult,
chunkMeta
};
});
}
configureTransmuxer(data) {
const {
config,
observer,
typeSupported,
vendor
} = this;
// probe for content type
let mux;
for (let i = 0, len = muxConfig.length; i < len; i++) {
if (muxConfig[i].demux.probe(data)) {
mux = muxConfig[i];
break;
}
}
if (!mux) {
return new Error('Failed to find demuxer by probing fragment data');
}
// so let's check that current remuxer and demuxer are still valid
const demuxer = this.demuxer;
const remuxer = this.remuxer;
const Remuxer = mux.remux;
const Demuxer = mux.demux;
if (!remuxer || !(remuxer instanceof Remuxer)) {
this.remuxer = new Remuxer(observer, config, typeSupported, vendor);
}
if (!demuxer || !(demuxer instanceof Demuxer)) {
this.demuxer = new Demuxer(observer, config, typeSupported);
this.probe = Demuxer.probe;
}
}
needsProbing(discontinuity, trackSwitch) {
// in case of continuity change, or track switch
// we might switch from content type (AAC container to TS container, or TS to fmp4 for example)
return !this.demuxer || !this.remuxer || discontinuity || trackSwitch;
}
getDecrypter() {
let decrypter = this.decrypter;
if (!decrypter) {
decrypter = this.decrypter = new Decrypter(this.config);
}
return decrypter;
}
}
function getEncryptionType(data, decryptData) {
let encryptionType = null;
if (data.byteLength > 0 && decryptData != null && decryptData.key != null && decryptData.iv !== null && decryptData.method != null) {
encryptionType = decryptData;
}
return encryptionType;
}
const emptyResult = chunkMeta => ({
remuxResult: {},
chunkMeta
});
function isPromise(p) {
return 'then' in p && p.then instanceof Function;
}
class TransmuxConfig {
constructor(audioCodec, videoCodec, initSegmentData, duration, defaultInitPts) {
this.audioCodec = void 0;
this.videoCodec = void 0;
this.initSegmentData = void 0;
this.duration = void 0;
this.defaultInitPts = void 0;
this.audioCodec = audioCodec;
this.videoCodec = videoCodec;
this.initSegmentData = initSegmentData;
this.duration = duration;
this.defaultInitPts = defaultInitPts || null;
}
}
class TransmuxState {
constructor(discontinuity, contiguous, accurateTimeOffset, trackSwitch, timeOffset, initSegmentChange) {
this.discontinuity = void 0;
this.contiguous = void 0;
this.accurateTimeOffset = void 0;
this.trackSwitch = void 0;
this.timeOffset = void 0;
this.initSegmentChange = void 0;
this.discontinuity = discontinuity;
this.contiguous = contiguous;
this.accurateTimeOffset = accurateTimeOffset;
this.trackSwitch = trackSwitch;
this.timeOffset = timeOffset;
this.initSegmentChange = initSegmentChange;
}
}
var eventemitter3 = {exports: {}};
(function (module) {
var has = Object.prototype.hasOwnProperty
, prefix = '~';
/**
* Constructor to create a storage for our `EE` objects.
* An `Events` instance is a plain object whose properties are event names.
*
* @constructor
* @private
*/
function Events() {}
//
// We try to not inherit from `Object.prototype`. In some engines creating an
// instance in this way is faster than calling `Object.create(null)` directly.
// If `Object.create(null)` is not supported we prefix the event names with a
// character to make sure that the built-in object properties are not
// overridden or used as an attack vector.
//
if (Object.create) {
Events.prototype = Object.create(null);
//
// This hack is needed because the `__proto__` property is still inherited in
// some old browsers like Android 4, iPhone 5.1, Opera 11 and Safari 5.
//
if (!new Events().__proto__) prefix = false;
}
/**
* Representation of a single event listener.
*
* @param {Function} fn The listener function.
* @param {*} context The context to invoke the listener with.
* @param {Boolean} [once=false] Specify if the listener is a one-time listener.
* @constructor
* @private
*/
function EE(fn, context, once) {
this.fn = fn;
this.context = context;
this.once = once || false;
}
/**
* Add a listener for a given event.
*
* @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
* @param {(String|Symbol)} event The event name.
* @param {Function} fn The listener function.
* @param {*} context The context to invoke the listener with.
* @param {Boolean} once Specify if the listener is a one-time listener.
* @returns {EventEmitter}
* @private
*/
function addListener(emitter, event, fn, context, once) {
if (typeof fn !== 'function') {
throw new TypeError('The listener must be a function');
}
var listener = new EE(fn, context || emitter, once)
, evt = prefix ? prefix + event : event;
if (!emitter._events[evt]) emitter._events[evt] = listener, emitter._eventsCount++;
else if (!emitter._events[evt].fn) emitter._events[evt].push(listener);
else emitter._events[evt] = [emitter._events[evt], listener];
return emitter;
}
/**
* Clear event by name.
*
* @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
* @param {(String|Symbol)} evt The Event name.
* @private
*/
function clearEvent(emitter, evt) {
if (--emitter._eventsCount === 0) emitter._events = new Events();
else delete emitter._events[evt];
}
/**
* Minimal `EventEmitter` interface that is molded against the Node.js
* `EventEmitter` interface.
*
* @constructor
* @public
*/
function EventEmitter() {
this._events = new Events();
this._eventsCount = 0;
}
/**
* Return an array listing the events for which the emitter has registered
* listeners.
*
* @returns {Array}
* @public
*/
EventEmitter.prototype.eventNames = function eventNames() {
var names = []
, events
, name;
if (this._eventsCount === 0) return names;
for (name in (events = this._events)) {
if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
}
if (Object.getOwnPropertySymbols) {
return names.concat(Object.getOwnPropertySymbols(events));
}
return names;
};
/**
* Return the listeners registered for a given event.
*
* @param {(String|Symbol)} event The event name.
* @returns {Array} The registered listeners.
* @public
*/
EventEmitter.prototype.listeners = function listeners(event) {
var evt = prefix ? prefix + event : event
, handlers = this._events[evt];
if (!handlers) return [];
if (handlers.fn) return [handlers.fn];
for (var i = 0, l = handlers.length, ee = new Array(l); i < l; i++) {
ee[i] = handlers[i].fn;
}
return ee;
};
/**
* Return the number of listeners listening to a given event.
*
* @param {(String|Symbol)} event The event name.
* @returns {Number} The number of listeners.
* @public
*/
EventEmitter.prototype.listenerCount = function listenerCount(event) {
var evt = prefix ? prefix + event : event
, listeners = this._events[evt];
if (!listeners) return 0;
if (listeners.fn) return 1;
return listeners.length;
};
/**
* Calls each of the listeners registered for a given event.
*
* @param {(String|Symbol)} event The event name.
* @returns {Boolean} `true` if the event had listeners, else `false`.
* @public
*/
EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
var evt = prefix ? prefix + event : event;
if (!this._events[evt]) return false;
var listeners = this._events[evt]
, len = arguments.length
, args
, i;
if (listeners.fn) {
if (listeners.once) this.removeListener(event, listeners.fn, undefined, true);
switch (len) {
case 1: return listeners.fn.call(listeners.context), true;
case 2: return listeners.fn.call(listeners.context, a1), true;
case 3: return listeners.fn.call(listeners.context, a1, a2), true;
case 4: return listeners.fn.call(listeners.context, a1, a2, a3), true;
case 5: return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
case 6: return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
}
for (i = 1, args = new Array(len -1); i < len; i++) {
args[i - 1] = arguments[i];
}
listeners.fn.apply(listeners.context, args);
} else {
var length = listeners.length
, j;
for (i = 0; i < length; i++) {
if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
switch (len) {
case 1: listeners[i].fn.call(listeners[i].context); break;
case 2: listeners[i].fn.call(listeners[i].context, a1); break;
case 3: listeners[i].fn.call(listeners[i].context, a1, a2); break;
case 4: listeners[i].fn.call(listeners[i].context, a1, a2, a3); break;
default:
if (!args) for (j = 1, args = new Array(len -1); j < len; j++) {
args[j - 1] = arguments[j];
}
listeners[i].fn.apply(listeners[i].context, args);
}
}
}
return true;
};
/**
* Add a listener for a given event.
*
* @param {(String|Symbol)} event The event name.
* @param {Function} fn The listener function.
* @param {*} [context=this] The context to invoke the listener with.
* @returns {EventEmitter} `this`.
* @public
*/
EventEmitter.prototype.on = function on(event, fn, context) {
return addListener(this, event, fn, context, false);
};
/**
* Add a one-time listener for a given event.
*
* @param {(String|Symbol)} event The event name.
* @param {Function} fn The listener function.
* @param {*} [context=this] The context to invoke the listener with.
* @returns {EventEmitter} `this`.
* @public
*/
EventEmitter.prototype.once = function once(event, fn, context) {
return addListener(this, event, fn, context, true);
};
/**
* Remove the listeners of a given event.
*
* @param {(String|Symbol)} event The event name.
* @param {Function} fn Only remove the listeners that match this function.
* @param {*} context Only remove the listeners that have this context.
* @param {Boolean} once Only remove one-time listeners.
* @returns {EventEmitter} `this`.
* @public
*/
EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
var evt = prefix ? prefix + event : event;
if (!this._events[evt]) return this;
if (!fn) {
clearEvent(this, evt);
return this;
}
var listeners = this._events[evt];
if (listeners.fn) {
if (
listeners.fn === fn &&
(!once || listeners.once) &&
(!context || listeners.context === context)
) {
clearEvent(this, evt);
}
} else {
for (var i = 0, events = [], length = listeners.length; i < length; i++) {
if (
listeners[i].fn !== fn ||
(once && !listeners[i].once) ||
(context && listeners[i].context !== context)
) {
events.push(listeners[i]);
}
}
//
// Reset the array, or remove it completely if we have no more listeners.
//
if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;
else clearEvent(this, evt);
}
return this;
};
/**
* Remove all listeners, or those of the specified event.
*
* @param {(String|Symbol)} [event] The event name.
* @returns {EventEmitter} `this`.
* @public
*/
EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
var evt;
if (event) {
evt = prefix ? prefix + event : event;
if (this._events[evt]) clearEvent(this, evt);
} else {
this._events = new Events();
this._eventsCount = 0;
}
return this;
};
//
// Alias methods names because people roll like that.
//
EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
EventEmitter.prototype.addListener = EventEmitter.prototype.on;
//
// Expose the prefix.
//
EventEmitter.prefixed = prefix;
//
// Allow `EventEmitter` to be imported as module namespace.
//
EventEmitter.EventEmitter = EventEmitter;
//
// Expose the module.
//
{
module.exports = EventEmitter;
}
} (eventemitter3));
var eventemitter3Exports = eventemitter3.exports;
var EventEmitter = /*@__PURE__*/getDefaultExportFromCjs(eventemitter3Exports);
const MediaSource$1 = getMediaSource() || {
isTypeSupported: () => false
};
class TransmuxerInterface {
constructor(hls, id, onTransmuxComplete, onFlush) {
this.error = null;
this.hls = void 0;
this.id = void 0;
this.observer = void 0;
this.frag = null;
this.part = null;
this.useWorker = void 0;
this.workerContext = null;
this.onwmsg = void 0;
this.transmuxer = null;
this.onTransmuxComplete = void 0;
this.onFlush = void 0;
const config = hls.config;
this.hls = hls;
this.id = id;
this.useWorker = !!config.enableWorker;
this.onTransmuxComplete = onTransmuxComplete;
this.onFlush = onFlush;
const forwardMessage = (ev, data) => {
data = data || {};
data.frag = this.frag;
data.id = this.id;
if (ev === Events.ERROR) {
this.error = data.error;
}
this.hls.trigger(ev, data);
};
// forward events to main thread
this.observer = new EventEmitter();
this.observer.on(Events.FRAG_DECRYPTED, forwardMessage);
this.observer.on(Events.ERROR, forwardMessage);
const typeSupported = {
mp4: MediaSource$1.isTypeSupported('video/mp4'),
mpeg: MediaSource$1.isTypeSupported('audio/mpeg'),
mp3: MediaSource$1.isTypeSupported('audio/mp4; codecs="mp3"')
};
// navigator.vendor is not always available in Web Worker
// refer to https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/navigator
const vendor = navigator.vendor;
if (this.useWorker && typeof Worker !== 'undefined') {
const canCreateWorker = config.workerPath || hasUMDWorker();
if (canCreateWorker) {
try {
if (config.workerPath) {
logger.log(`loading Web Worker ${config.workerPath} for "${id}"`);
this.workerContext = loadWorker(config.workerPath);
} else {
logger.log(`injecting Web Worker for "${id}"`);
this.workerContext = injectWorker();
}
this.onwmsg = ev => this.onWorkerMessage(ev);
const {
worker
} = this.workerContext;
worker.addEventListener('message', this.onwmsg);
worker.onerror = event => {
const error = new Error(`${event.message} (${event.filename}:${event.lineno})`);
config.enableWorker = false;
logger.warn(`Error in "${id}" Web Worker, fallback to inline`);
this.hls.trigger(Events.ERROR, {
type: ErrorTypes.OTHER_ERROR,
details: ErrorDetails.INTERNAL_EXCEPTION,
fatal: false,
event: 'demuxerWorker',
error
});
};
worker.postMessage({
cmd: 'init',
typeSupported: typeSupported,
vendor: vendor,
id: id,
config: JSON.stringify(config)
});
} catch (err) {
logger.warn(`Error setting up "${id}" Web Worker, fallback to inline`, err);
this.resetWorker();
this.error = null;
this.transmuxer = new Transmuxer$1(this.observer, typeSupported, config, vendor, id);
}
return;
}
}
this.transmuxer = new Transmuxer$1(this.observer, typeSupported, config, vendor, id);
}
resetWorker() {
if (this.workerContext) {
const {
worker,
objectURL
} = this.workerContext;
if (objectURL) {
// revoke the Object URL that was used to create transmuxer worker, so as not to leak it
self.URL.revokeObjectURL(objectURL);
}
worker.removeEventListener('message', this.onwmsg);
worker.onerror = null;
worker.terminate();
this.workerContext = null;
}
}
destroy() {
if (this.workerContext) {
this.resetWorker();
this.onwmsg = undefined;
} else {
const transmuxer = this.transmuxer;
if (transmuxer) {
transmuxer.destroy();
this.transmuxer = null;
}
}
const observer = this.observer;
if (observer) {
observer.removeAllListeners();
}
this.frag = null;
// @ts-ignore
this.observer = null;
// @ts-ignore
this.hls = null;
}
push(data, initSegmentData, audioCodec, videoCodec, frag, part, duration, accurateTimeOffset, chunkMeta, defaultInitPTS) {
var _frag$initSegment, _lastFrag$initSegment;
chunkMeta.transmuxing.start = self.performance.now();
const {
transmuxer
} = this;
const timeOffset = part ? part.start : frag.start;
// TODO: push "clear-lead" decrypt data for unencrypted fragments in streams with encrypted ones
const decryptdata = frag.decryptdata;
const lastFrag = this.frag;
const discontinuity = !(lastFrag && frag.cc === lastFrag.cc);
const trackSwitch = !(lastFrag && chunkMeta.level === lastFrag.level);
const snDiff = lastFrag ? chunkMeta.sn - lastFrag.sn : -1;
const partDiff = this.part ? chunkMeta.part - this.part.index : -1;
const progressive = snDiff === 0 && chunkMeta.id > 1 && chunkMeta.id === (lastFrag == null ? void 0 : lastFrag.stats.chunkCount);
const contiguous = !trackSwitch && (snDiff === 1 || snDiff === 0 && (partDiff === 1 || progressive && partDiff <= 0));
const now = self.performance.now();
if (trackSwitch || snDiff || frag.stats.parsing.start === 0) {
frag.stats.parsing.start = now;
}
if (part && (partDiff || !contiguous)) {
part.stats.parsing.start = now;
}
const initSegmentChange = !(lastFrag && ((_frag$initSegment = frag.initSegment) == null ? void 0 : _frag$initSegment.url) === ((_lastFrag$initSegment = lastFrag.initSegment) == null ? void 0 : _lastFrag$initSegment.url));
const state = new TransmuxState(discontinuity, contiguous, accurateTimeOffset, trackSwitch, timeOffset, initSegmentChange);
if (!contiguous || discontinuity || initSegmentChange) {
logger.log(`[transmuxer-interface, ${frag.type}]: Starting new transmux session for sn: ${chunkMeta.sn} p: ${chunkMeta.part} level: ${chunkMeta.level} id: ${chunkMeta.id}
discontinuity: ${discontinuity}
trackSwitch: ${trackSwitch}
contiguous: ${contiguous}
accurateTimeOffset: ${accurateTimeOffset}
timeOffset: ${timeOffset}
initSegmentChange: ${initSegmentChange}`);
const config = new TransmuxConfig(audioCodec, videoCodec, initSegmentData, duration, defaultInitPTS);
this.configureTransmuxer(config);
}
this.frag = frag;
this.part = part;
// Frags with sn of 'initSegment' are not transmuxed
if (this.workerContext) {
// post fragment payload as transferable objects for ArrayBuffer (no copy)
this.workerContext.worker.postMessage({
cmd: 'demux',
data,
decryptdata,
chunkMeta,
state
}, data instanceof ArrayBuffer ? [data] : []);
} else if (transmuxer) {
const transmuxResult = transmuxer.push(data, decryptdata, chunkMeta, state);
if (isPromise(transmuxResult)) {
transmuxer.async = true;
transmuxResult.then(data => {
this.handleTransmuxComplete(data);
}).catch(error => {
this.transmuxerError(error, chunkMeta, 'transmuxer-interface push error');
});
} else {
transmuxer.async = false;
this.handleTransmuxComplete(transmuxResult);
}
}
}
flush(chunkMeta) {
chunkMeta.transmuxing.start = self.performance.now();
const {
transmuxer
} = this;
if (this.workerContext) {
this.workerContext.worker.postMessage({
cmd: 'flush',
chunkMeta
});
} else if (transmuxer) {
let transmuxResult = transmuxer.flush(chunkMeta);
const asyncFlush = isPromise(transmuxResult);
if (asyncFlush || transmuxer.async) {
if (!isPromise(transmuxResult)) {
transmuxResult = Promise.resolve(transmuxResult);
}
transmuxResult.then(data => {
this.handleFlushResult(data, chunkMeta);
}).catch(error => {
this.transmuxerError(error, chunkMeta, 'transmuxer-interface flush error');
});
} else {
this.handleFlushResult(transmuxResult, chunkMeta);
}
}
}
transmuxerError(error, chunkMeta, reason) {
if (!this.hls) {
return;
}
this.error = error;
this.hls.trigger(Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.FRAG_PARSING_ERROR,
chunkMeta,
fatal: false,
error,
err: error,
reason
});
}
handleFlushResult(results, chunkMeta) {
results.forEach(result => {
this.handleTransmuxComplete(result);
});
this.onFlush(chunkMeta);
}
onWorkerMessage(ev) {
const data = ev.data;
const hls = this.hls;
switch (data.event) {
case 'init':
{
var _this$workerContext;
const objectURL = (_this$workerContext = this.workerContext) == null ? void 0 : _this$workerContext.objectURL;
if (objectURL) {
// revoke the Object URL that was used to create transmuxer worker, so as not to leak it
self.URL.revokeObjectURL(objectURL);
}
break;
}
case 'transmuxComplete':
{
this.handleTransmuxComplete(data.data);
break;
}
case 'flush':
{
this.onFlush(data.data);
break;
}
// pass logs from the worker thread to the main logger
case 'workerLog':
if (logger[data.data.logType]) {
logger[data.data.logType](data.data.message);
}
break;
default:
{
data.data = data.data || {};
data.data.frag = this.frag;
data.data.id = this.id;
hls.trigger(data.event, data.data);
break;
}
}
}
configureTransmuxer(config) {
const {
transmuxer
} = this;
if (this.workerContext) {
this.workerContext.worker.postMessage({
cmd: 'configure',
config
});
} else if (transmuxer) {
transmuxer.configure(config);
}
}
handleTransmuxComplete(result) {
result.chunkMeta.transmuxing.end = self.performance.now();
this.onTransmuxComplete(result);
}
}
const STALL_MINIMUM_DURATION_MS = 250;
const MAX_START_GAP_JUMP = 2.0;
const SKIP_BUFFER_HOLE_STEP_SECONDS = 0.1;
const SKIP_BUFFER_RANGE_START = 0.05;
class GapController {
constructor(config, media, fragmentTracker, hls) {
this.config = void 0;
this.media = null;
this.fragmentTracker = void 0;
this.hls = void 0;
this.nudgeRetry = 0;
this.stallReported = false;
this.stalled = null;
this.moved = false;
this.seeking = false;
this.config = config;
this.media = media;
this.fragmentTracker = fragmentTracker;
this.hls = hls;
}
destroy() {
this.media = null;
// @ts-ignore
this.hls = this.fragmentTracker = null;
}
/**
* Checks if the playhead is stuck within a gap, and if so, attempts to free it.
* A gap is an unbuffered range between two buffered ranges (or the start and the first buffered range).
*
* @param lastCurrentTime - Previously read playhead position
*/
poll(lastCurrentTime, activeFrag) {
const {
config,
media,
stalled
} = this;
if (media === null) {
return;
}
const {
currentTime,
seeking
} = media;
const seeked = this.seeking && !seeking;
const beginSeek = !this.seeking && seeking;
this.seeking = seeking;
// The playhead is moving, no-op
if (currentTime !== lastCurrentTime) {
this.moved = true;
if (stalled !== null) {
// The playhead is now moving, but was previously stalled
if (this.stallReported) {
const _stalledDuration = self.performance.now() - stalled;
logger.warn(`playback not stuck anymore @${currentTime}, after ${Math.round(_stalledDuration)}ms`);
this.stallReported = false;
}
this.stalled = null;
this.nudgeRetry = 0;
}
return;
}
// Clear stalled state when beginning or finishing seeking so that we don't report stalls coming out of a seek
if (beginSeek || seeked) {
this.stalled = null;
return;
}
// The playhead should not be moving
if (media.paused && !seeking || media.ended || media.playbackRate === 0 || !BufferHelper.getBuffered(media).length) {
return;
}
const bufferInfo = BufferHelper.bufferInfo(media, currentTime, 0);
const isBuffered = bufferInfo.len > 0;
const nextStart = bufferInfo.nextStart || 0;
// There is no playable buffer (seeked, waiting for buffer)
if (!isBuffered && !nextStart) {
return;
}
if (seeking) {
// Waiting for seeking in a buffered range to complete
const hasEnoughBuffer = bufferInfo.len > MAX_START_GAP_JUMP;
// Next buffered range is too far ahead to jump to while still seeking
const noBufferGap = !nextStart || activeFrag && activeFrag.start <= currentTime || nextStart - currentTime > MAX_START_GAP_JUMP && !this.fragmentTracker.getPartialFragment(currentTime);
if (hasEnoughBuffer || noBufferGap) {
return;
}
// Reset moved state when seeking to a point in or before a gap
this.moved = false;
}
// Skip start gaps if we haven't played, but the last poll detected the start of a stall
// The addition poll gives the browser a chance to jump the gap for us
if (!this.moved && this.stalled !== null) {
var _level$details;
// Jump start gaps within jump threshold
const startJump = Math.max(nextStart, bufferInfo.start || 0) - currentTime;
// When joining a live stream with audio tracks, account for live playlist window sliding by allowing
// a larger jump over start gaps caused by the audio-stream-controller buffering a start fragment
// that begins over 1 target duration after the video start position.
const level = this.hls.levels ? this.hls.levels[this.hls.currentLevel] : null;
const isLive = level == null ? void 0 : (_level$details = level.details) == null ? void 0 : _level$details.live;
const maxStartGapJump = isLive ? level.details.targetduration * 2 : MAX_START_GAP_JUMP;
const partialOrGap = this.fragmentTracker.getPartialFragment(currentTime);
if (startJump > 0 && (startJump <= maxStartGapJump || partialOrGap)) {
this._trySkipBufferHole(partialOrGap);
return;
}
}
// Start tracking stall time
const tnow = self.performance.now();
if (stalled === null) {
this.stalled = tnow;
return;
}
const stalledDuration = tnow - stalled;
if (!seeking && stalledDuration >= STALL_MINIMUM_DURATION_MS) {
// Report stalling after trying to fix
this._reportStall(bufferInfo);
if (!this.media) {
return;
}
}
const bufferedWithHoles = BufferHelper.bufferInfo(media, currentTime, config.maxBufferHole);
this._tryFixBufferStall(bufferedWithHoles, stalledDuration);
}
/**
* Detects and attempts to fix known buffer stalling issues.
* @param bufferInfo - The properties of the current buffer.
* @param stalledDurationMs - The amount of time Hls.js has been stalling for.
* @private
*/
_tryFixBufferStall(bufferInfo, stalledDurationMs) {
const {
config,
fragmentTracker,
media
} = this;
if (media === null) {
return;
}
const currentTime = media.currentTime;
const partial = fragmentTracker.getPartialFragment(currentTime);
if (partial) {
// Try to skip over the buffer hole caused by a partial fragment
// This method isn't limited by the size of the gap between buffered ranges
const targetTime = this._trySkipBufferHole(partial);
// we return here in this case, meaning
// the branch below only executes when we haven't seeked to a new position
if (targetTime || !this.media) {
return;
}
}
// if we haven't had to skip over a buffer hole of a partial fragment
// we may just have to "nudge" the playlist as the browser decoding/rendering engine
// needs to cross some sort of threshold covering all source-buffers content
// to start playing properly.
if ((bufferInfo.len > config.maxBufferHole || bufferInfo.nextStart && bufferInfo.nextStart - currentTime < config.maxBufferHole) && stalledDurationMs > config.highBufferWatchdogPeriod * 1000) {
logger.warn('Trying to nudge playhead over buffer-hole');
// Try to nudge currentTime over a buffer hole if we've been stalling for the configured amount of seconds
// We only try to jump the hole if it's under the configured size
// Reset stalled so to rearm watchdog timer
this.stalled = null;
this._tryNudgeBuffer();
}
}
/**
* Triggers a BUFFER_STALLED_ERROR event, but only once per stall period.
* @param bufferLen - The playhead distance from the end of the current buffer segment.
* @private
*/
_reportStall(bufferInfo) {
const {
hls,
media,
stallReported
} = this;
if (!stallReported && media) {
// Report stalled error once
this.stallReported = true;
const error = new Error(`Playback stalling at @${media.currentTime} due to low buffer (${JSON.stringify(bufferInfo)})`);
logger.warn(error.message);
hls.trigger(Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.BUFFER_STALLED_ERROR,
fatal: false,
error,
buffer: bufferInfo.len
});
}
}
/**
* Attempts to fix buffer stalls by jumping over known gaps caused by partial fragments
* @param partial - The partial fragment found at the current time (where playback is stalling).
* @private
*/
_trySkipBufferHole(partial) {
const {
config,
hls,
media
} = this;
if (media === null) {
return 0;
}
// Check if currentTime is between unbuffered regions of partial fragments
const currentTime = media.currentTime;
const bufferInfo = BufferHelper.bufferInfo(media, currentTime, 0);
const startTime = currentTime < bufferInfo.start ? bufferInfo.start : bufferInfo.nextStart;
if (startTime) {
const bufferStarved = bufferInfo.len <= config.maxBufferHole;
const waiting = bufferInfo.len > 0 && bufferInfo.len < 1 && media.readyState < 3;
const gapLength = startTime - currentTime;
if (gapLength > 0 && (bufferStarved || waiting)) {
// Only allow large gaps to be skipped if it is a start gap, or all fragments in skip range are partial
if (gapLength > config.maxBufferHole) {
const {
fragmentTracker
} = this;
let startGap = false;
if (currentTime === 0) {
const startFrag = fragmentTracker.getAppendedFrag(0, PlaylistLevelType.MAIN);
if (startFrag && startTime < startFrag.end) {
startGap = true;
}
}
if (!startGap) {
const startProvisioned = partial || fragmentTracker.getAppendedFrag(currentTime, PlaylistLevelType.MAIN);
if (startProvisioned) {
let moreToLoad = false;
let pos = startProvisioned.end;
while (pos < startTime) {
const provisioned = fragmentTracker.getPartialFragment(pos);
if (provisioned) {
pos += provisioned.duration;
} else {
moreToLoad = true;
break;
}
}
if (moreToLoad) {
return 0;
}
}
}
}
const targetTime = Math.max(startTime + SKIP_BUFFER_RANGE_START, currentTime + SKIP_BUFFER_HOLE_STEP_SECONDS);
logger.warn(`skipping hole, adjusting currentTime from ${currentTime} to ${targetTime}`);
this.moved = true;
this.stalled = null;
media.currentTime = targetTime;
if (partial && !partial.gap) {
const error = new Error(`fragment loaded with buffer holes, seeking from ${currentTime} to ${targetTime}`);
hls.trigger(Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.BUFFER_SEEK_OVER_HOLE,
fatal: false,
error,
reason: error.message,
frag: partial
});
}
return targetTime;
}
}
return 0;
}
/**
* Attempts to fix buffer stalls by advancing the mediaElement's current time by a small amount.
* @private
*/
_tryNudgeBuffer() {
const {
config,
hls,
media,
nudgeRetry
} = this;
if (media === null) {
return;
}
const currentTime = media.currentTime;
this.nudgeRetry++;
if (nudgeRetry < config.nudgeMaxRetry) {
const targetTime = currentTime + (nudgeRetry + 1) * config.nudgeOffset;
// playback stalled in buffered area ... let's nudge currentTime to try to overcome this
const error = new Error(`Nudging 'currentTime' from ${currentTime} to ${targetTime}`);
logger.warn(error.message);
media.currentTime = targetTime;
hls.trigger(Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.BUFFER_NUDGE_ON_STALL,
error,
fatal: false
});
} else {
const error = new Error(`Playhead still not moving while enough data buffered @${currentTime} after ${config.nudgeMaxRetry} nudges`);
logger.error(error.message);
hls.trigger(Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.BUFFER_STALLED_ERROR,
error,
fatal: true
});
}
}
}
const TICK_INTERVAL$2 = 100; // how often to tick in ms
class StreamController extends BaseStreamController {
constructor(hls, fragmentTracker, keyLoader) {
super(hls, fragmentTracker, keyLoader, '[stream-controller]', PlaylistLevelType.MAIN);
this.audioCodecSwap = false;
this.gapController = null;
this.level = -1;
this._forceStartLoad = false;
this.altAudio = false;
this.audioOnly = false;
this.fragPlaying = null;
this.onvplaying = null;
this.onvseeked = null;
this.fragLastKbps = 0;
this.couldBacktrack = false;
this.backtrackFragment = null;
this.audioCodecSwitch = false;
this.videoBuffer = null;
this._registerListeners();
}
_registerListeners() {
const {
hls
} = this;
hls.on(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
hls.on(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.on(Events.MANIFEST_PARSED, this.onManifestParsed, this);
hls.on(Events.LEVEL_LOADING, this.onLevelLoading, this);
hls.on(Events.LEVEL_LOADED, this.onLevelLoaded, this);
hls.on(Events.FRAG_LOAD_EMERGENCY_ABORTED, this.onFragLoadEmergencyAborted, this);
hls.on(Events.ERROR, this.onError, this);
hls.on(Events.AUDIO_TRACK_SWITCHING, this.onAudioTrackSwitching, this);
hls.on(Events.AUDIO_TRACK_SWITCHED, this.onAudioTrackSwitched, this);
hls.on(Events.BUFFER_CREATED, this.onBufferCreated, this);
hls.on(Events.BUFFER_FLUSHED, this.onBufferFlushed, this);
hls.on(Events.LEVELS_UPDATED, this.onLevelsUpdated, this);
hls.on(Events.FRAG_BUFFERED, this.onFragBuffered, this);
}
_unregisterListeners() {
const {
hls
} = this;
hls.off(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
hls.off(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.off(Events.MANIFEST_PARSED, this.onManifestParsed, this);
hls.off(Events.LEVEL_LOADED, this.onLevelLoaded, this);
hls.off(Events.FRAG_LOAD_EMERGENCY_ABORTED, this.onFragLoadEmergencyAborted, this);
hls.off(Events.ERROR, this.onError, this);
hls.off(Events.AUDIO_TRACK_SWITCHING, this.onAudioTrackSwitching, this);
hls.off(Events.AUDIO_TRACK_SWITCHED, this.onAudioTrackSwitched, this);
hls.off(Events.BUFFER_CREATED, this.onBufferCreated, this);
hls.off(Events.BUFFER_FLUSHED, this.onBufferFlushed, this);
hls.off(Events.LEVELS_UPDATED, this.onLevelsUpdated, this);
hls.off(Events.FRAG_BUFFERED, this.onFragBuffered, this);
}
onHandlerDestroying() {
this._unregisterListeners();
this.onMediaDetaching();
}
startLoad(startPosition) {
if (this.levels) {
const {
lastCurrentTime,
hls
} = this;
this.stopLoad();
this.setInterval(TICK_INTERVAL$2);
this.level = -1;
if (!this.startFragRequested) {
// determine load level
let startLevel = hls.startLevel;
if (startLevel === -1) {
if (hls.config.testBandwidth && this.levels.length > 1) {
// -1 : guess start Level by doing a bitrate test by loading first fragment of lowest quality level
startLevel = 0;
this.bitrateTest = true;
} else {
startLevel = hls.nextAutoLevel;
}
}
// set new level to playlist loader : this will trigger start level load
// hls.nextLoadLevel remains until it is set to a new value or until a new frag is successfully loaded
this.level = hls.nextLoadLevel = startLevel;
this.loadedmetadata = false;
}
// if startPosition undefined but lastCurrentTime set, set startPosition to last currentTime
if (lastCurrentTime > 0 && startPosition === -1) {
this.log(`Override startPosition with lastCurrentTime @${lastCurrentTime.toFixed(3)}`);
startPosition = lastCurrentTime;
}
this.state = State.IDLE;
this.nextLoadPosition = this.startPosition = this.lastCurrentTime = startPosition;
this.tick();
} else {
this._forceStartLoad = true;
this.state = State.STOPPED;
}
}
stopLoad() {
this._forceStartLoad = false;
super.stopLoad();
}
doTick() {
switch (this.state) {
case State.WAITING_LEVEL:
{
var _levels$level;
const {
levels,
level
} = this;
const details = levels == null ? void 0 : (_levels$level = levels[level]) == null ? void 0 : _levels$level.details;
if (details && (!details.live || this.levelLastLoaded === this.level)) {
if (this.waitForCdnTuneIn(details)) {
break;
}
this.state = State.IDLE;
break;
} else if (this.hls.nextLoadLevel !== this.level) {
this.state = State.IDLE;
break;
}
break;
}
case State.FRAG_LOADING_WAITING_RETRY:
{
var _this$media;
const now = self.performance.now();
const retryDate = this.retryDate;
// if current time is gt than retryDate, or if media seeking let's switch to IDLE state to retry loading
if (!retryDate || now >= retryDate || (_this$media = this.media) != null && _this$media.seeking) {
this.resetStartWhenNotLoaded(this.level);
this.state = State.IDLE;
}
}
break;
}
if (this.state === State.IDLE) {
this.doTickIdle();
}
this.onTickEnd();
}
onTickEnd() {
super.onTickEnd();
this.checkBuffer();
this.checkFragmentChanged();
}
doTickIdle() {
const {
hls,
levelLastLoaded,
levels,
media
} = this;
const {
config,
nextLoadLevel: level
} = hls;
// if start level not parsed yet OR
// if video not attached AND start fragment already requested OR start frag prefetch not enabled
// exit loop, as we either need more info (level not parsed) or we need media to be attached to load new fragment
if (levelLastLoaded === null || !media && (this.startFragRequested || !config.startFragPrefetch)) {
return;
}
// If the "main" level is audio-only but we are loading an alternate track in the same group, do not load anything
if (this.altAudio && this.audioOnly) {
return;
}
if (!(levels != null && levels[level])) {
return;
}
const levelInfo = levels[level];
// if buffer length is less than maxBufLen try to load a new fragment
const bufferInfo = this.getMainFwdBufferInfo();
if (bufferInfo === null) {
return;
}
const lastDetails = this.getLevelDetails();
if (lastDetails && this._streamEnded(bufferInfo, lastDetails)) {
const data = {};
if (this.altAudio) {
data.type = 'video';
}
this.hls.trigger(Events.BUFFER_EOS, data);
this.state = State.ENDED;
return;
}
// set next load level : this will trigger a playlist load if needed
if (hls.loadLevel !== level && hls.manualLevel === -1) {
this.log(`Adapting to level ${level} from level ${this.level}`);
}
this.level = hls.nextLoadLevel = level;
const levelDetails = levelInfo.details;
// if level info not retrieved yet, switch state and wait for level retrieval
// if live playlist, ensure that new playlist has been refreshed to avoid loading/try to load
// a useless and outdated fragment (that might even introduce load error if it is already out of the live playlist)
if (!levelDetails || this.state === State.WAITING_LEVEL || levelDetails.live && this.levelLastLoaded !== level) {
this.level = level;
this.state = State.WAITING_LEVEL;
return;
}
const bufferLen = bufferInfo.len;
// compute max Buffer Length that we could get from this load level, based on level bitrate. don't buffer more than 60 MB and more than 30s
const maxBufLen = this.getMaxBufferLength(levelInfo.maxBitrate);
// Stay idle if we are still with buffer margins
if (bufferLen >= maxBufLen) {
return;
}
if (this.backtrackFragment && this.backtrackFragment.start > bufferInfo.end) {
this.backtrackFragment = null;
}
const targetBufferTime = this.backtrackFragment ? this.backtrackFragment.start : bufferInfo.end;
let frag = this.getNextFragment(targetBufferTime, levelDetails);
// Avoid backtracking by loading an earlier segment in streams with segments that do not start with a key frame (flagged by `couldBacktrack`)
if (this.couldBacktrack && !this.fragPrevious && frag && frag.sn !== 'initSegment' && this.fragmentTracker.getState(frag) !== FragmentState.OK) {
var _this$backtrackFragme;
const backtrackSn = ((_this$backtrackFragme = this.backtrackFragment) != null ? _this$backtrackFragme : frag).sn;
const fragIdx = backtrackSn - levelDetails.startSN;
const backtrackFrag = levelDetails.fragments[fragIdx - 1];
if (backtrackFrag && frag.cc === backtrackFrag.cc) {
frag = backtrackFrag;
this.fragmentTracker.removeFragment(backtrackFrag);
}
} else if (this.backtrackFragment && bufferInfo.len) {
this.backtrackFragment = null;
}
// Avoid loop loading by using nextLoadPosition set for backtracking and skipping consecutive GAP tags
if (frag && this.isLoopLoading(frag, targetBufferTime)) {
const gapStart = frag.gap;
if (!gapStart) {
// Cleanup the fragment tracker before trying to find the next unbuffered fragment
const type = this.audioOnly && !this.altAudio ? ElementaryStreamTypes.AUDIO : ElementaryStreamTypes.VIDEO;
const mediaBuffer = (type === ElementaryStreamTypes.VIDEO ? this.videoBuffer : this.mediaBuffer) || this.media;
if (mediaBuffer) {
this.afterBufferFlushed(mediaBuffer, type, PlaylistLevelType.MAIN);
}
}
frag = this.getNextFragmentLoopLoading(frag, levelDetails, bufferInfo, PlaylistLevelType.MAIN, maxBufLen);
}
if (!frag) {
return;
}
if (frag.initSegment && !frag.initSegment.data && !this.bitrateTest) {
frag = frag.initSegment;
}
this.loadFragment(frag, levelInfo, targetBufferTime);
}
loadFragment(frag, level, targetBufferTime) {
// Check if fragment is not loaded
const fragState = this.fragmentTracker.getState(frag);
this.fragCurrent = frag;
if (fragState === FragmentState.NOT_LOADED || fragState === FragmentState.PARTIAL) {
if (frag.sn === 'initSegment') {
this._loadInitSegment(frag, level);
} else if (this.bitrateTest) {
this.log(`Fragment ${frag.sn} of level ${frag.level} is being downloaded to test bitrate and will not be buffered`);
this._loadBitrateTestFrag(frag, level);
} else {
this.startFragRequested = true;
super.loadFragment(frag, level, targetBufferTime);
}
} else {
this.clearTrackerIfNeeded(frag);
}
}
getBufferedFrag(position) {
return this.fragmentTracker.getBufferedFrag(position, PlaylistLevelType.MAIN);
}
followingBufferedFrag(frag) {
if (frag) {
// try to get range of next fragment (500ms after this range)
return this.getBufferedFrag(frag.end + 0.5);
}
return null;
}
/*
on immediate level switch :
- pause playback if playing
- cancel any pending load request
- and trigger a buffer flush
*/
immediateLevelSwitch() {
this.abortCurrentFrag();
this.flushMainBuffer(0, Number.POSITIVE_INFINITY);
}
/**
* try to switch ASAP without breaking video playback:
* in order to ensure smooth but quick level switching,
* we need to find the next flushable buffer range
* we should take into account new segment fetch time
*/
nextLevelSwitch() {
const {
levels,
media
} = this;
// ensure that media is defined and that metadata are available (to retrieve currentTime)
if (media != null && media.readyState) {
let fetchdelay;
const fragPlayingCurrent = this.getAppendedFrag(media.currentTime);
if (fragPlayingCurrent && fragPlayingCurrent.start > 1) {
// flush buffer preceding current fragment (flush until current fragment start offset)
// minus 1s to avoid video freezing, that could happen if we flush keyframe of current video ...
this.flushMainBuffer(0, fragPlayingCurrent.start - 1);
}
const levelDetails = this.getLevelDetails();
if (levelDetails != null && levelDetails.live) {
const bufferInfo = this.getMainFwdBufferInfo();
// Do not flush in live stream with low buffer
if (!bufferInfo || bufferInfo.len < levelDetails.targetduration * 2) {
return;
}
}
if (!media.paused && levels) {
// add a safety delay of 1s
const nextLevelId = this.hls.nextLoadLevel;
const nextLevel = levels[nextLevelId];
const fragLastKbps = this.fragLastKbps;
if (fragLastKbps && this.fragCurrent) {
fetchdelay = this.fragCurrent.duration * nextLevel.maxBitrate / (1000 * fragLastKbps) + 1;
} else {
fetchdelay = 0;
}
} else {
fetchdelay = 0;
}
// this.log('fetchdelay:'+fetchdelay);
// find buffer range that will be reached once new fragment will be fetched
const bufferedFrag = this.getBufferedFrag(media.currentTime + fetchdelay);
if (bufferedFrag) {
// we can flush buffer range following this one without stalling playback
const nextBufferedFrag = this.followingBufferedFrag(bufferedFrag);
if (nextBufferedFrag) {
// if we are here, we can also cancel any loading/demuxing in progress, as they are useless
this.abortCurrentFrag();
// start flush position is in next buffered frag. Leave some padding for non-independent segments and smoother playback.
const maxStart = nextBufferedFrag.maxStartPTS ? nextBufferedFrag.maxStartPTS : nextBufferedFrag.start;
const fragDuration = nextBufferedFrag.duration;
const startPts = Math.max(bufferedFrag.end, maxStart + Math.min(Math.max(fragDuration - this.config.maxFragLookUpTolerance, fragDuration * 0.5), fragDuration * 0.75));
this.flushMainBuffer(startPts, Number.POSITIVE_INFINITY);
}
}
}
}
abortCurrentFrag() {
const fragCurrent = this.fragCurrent;
this.fragCurrent = null;
this.backtrackFragment = null;
if (fragCurrent) {
fragCurrent.abortRequests();
this.fragmentTracker.removeFragment(fragCurrent);
}
switch (this.state) {
case State.KEY_LOADING:
case State.FRAG_LOADING:
case State.FRAG_LOADING_WAITING_RETRY:
case State.PARSING:
case State.PARSED:
this.state = State.IDLE;
break;
}
this.nextLoadPosition = this.getLoadPosition();
}
flushMainBuffer(startOffset, endOffset) {
super.flushMainBuffer(startOffset, endOffset, this.altAudio ? 'video' : null);
}
onMediaAttached(event, data) {
super.onMediaAttached(event, data);
const media = data.media;
this.onvplaying = this.onMediaPlaying.bind(this);
this.onvseeked = this.onMediaSeeked.bind(this);
media.addEventListener('playing', this.onvplaying);
media.addEventListener('seeked', this.onvseeked);
this.gapController = new GapController(this.config, media, this.fragmentTracker, this.hls);
}
onMediaDetaching() {
const {
media
} = this;
if (media && this.onvplaying && this.onvseeked) {
media.removeEventListener('playing', this.onvplaying);
media.removeEventListener('seeked', this.onvseeked);
this.onvplaying = this.onvseeked = null;
this.videoBuffer = null;
}
this.fragPlaying = null;
if (this.gapController) {
this.gapController.destroy();
this.gapController = null;
}
super.onMediaDetaching();
}
onMediaPlaying() {
// tick to speed up FRAG_CHANGED triggering
this.tick();
}
onMediaSeeked() {
const media = this.media;
const currentTime = media ? media.currentTime : null;
if (isFiniteNumber(currentTime)) {
this.log(`Media seeked to ${currentTime.toFixed(3)}`);
}
// If seeked was issued before buffer was appended do not tick immediately
const bufferInfo = this.getMainFwdBufferInfo();
if (bufferInfo === null || bufferInfo.len === 0) {
this.warn(`Main forward buffer length on "seeked" event ${bufferInfo ? bufferInfo.len : 'empty'})`);
return;
}
// tick to speed up FRAG_CHANGED triggering
this.tick();
}
onManifestLoading() {
// reset buffer on manifest loading
this.log('Trigger BUFFER_RESET');
this.hls.trigger(Events.BUFFER_RESET, undefined);
this.fragmentTracker.removeAllFragments();
this.couldBacktrack = false;
this.startPosition = this.lastCurrentTime = 0;
this.levels = this.fragPlaying = this.backtrackFragment = null;
this.altAudio = this.audioOnly = false;
}
onManifestParsed(event, data) {
let aac = false;
let heaac = false;
let codec;
data.levels.forEach(level => {
// detect if we have different kind of audio codecs used amongst playlists
codec = level.audioCodec;
if (codec) {
if (codec.indexOf('mp4a.40.2') !== -1) {
aac = true;
}
if (codec.indexOf('mp4a.40.5') !== -1) {
heaac = true;
}
}
});
this.audioCodecSwitch = aac && heaac && !changeTypeSupported();
if (this.audioCodecSwitch) {
this.log('Both AAC/HE-AAC audio found in levels; declaring level codec as HE-AAC');
}
this.levels = data.levels;
this.startFragRequested = false;
}
onLevelLoading(event, data) {
const {
levels
} = this;
if (!levels || this.state !== State.IDLE) {
return;
}
const level = levels[data.level];
if (!level.details || level.details.live && this.levelLastLoaded !== data.level || this.waitForCdnTuneIn(level.details)) {
this.state = State.WAITING_LEVEL;
}
}
onLevelLoaded(event, data) {
var _curLevel$details;
const {
levels
} = this;
const newLevelId = data.level;
const newDetails = data.details;
const duration = newDetails.totalduration;
if (!levels) {
this.warn(`Levels were reset while loading level ${newLevelId}`);
return;
}
this.log(`Level ${newLevelId} loaded [${newDetails.startSN},${newDetails.endSN}]${newDetails.lastPartSn ? `[part-${newDetails.lastPartSn}-${newDetails.lastPartIndex}]` : ''}, cc [${newDetails.startCC}, ${newDetails.endCC}] duration:${duration}`);
const curLevel = levels[newLevelId];
const fragCurrent = this.fragCurrent;
if (fragCurrent && (this.state === State.FRAG_LOADING || this.state === State.FRAG_LOADING_WAITING_RETRY)) {
if ((fragCurrent.level !== data.level || fragCurrent.urlId !== curLevel.urlId) && fragCurrent.loader) {
this.abortCurrentFrag();
}
}
let sliding = 0;
if (newDetails.live || (_curLevel$details = curLevel.details) != null && _curLevel$details.live) {
this.checkLiveUpdate(newDetails);
if (newDetails.deltaUpdateFailed) {
return;
}
sliding = this.alignPlaylists(newDetails, curLevel.details);
}
// override level info
curLevel.details = newDetails;
this.levelLastLoaded = newLevelId;
this.hls.trigger(Events.LEVEL_UPDATED, {
details: newDetails,
level: newLevelId
});
// only switch back to IDLE state if we were waiting for level to start downloading a new fragment
if (this.state === State.WAITING_LEVEL) {
if (this.waitForCdnTuneIn(newDetails)) {
// Wait for Low-Latency CDN Tune-in
return;
}
this.state = State.IDLE;
}
if (!this.startFragRequested) {
this.setStartPosition(newDetails, sliding);
} else if (newDetails.live) {
this.synchronizeToLiveEdge(newDetails);
}
// trigger handler right now
this.tick();
}
_handleFragmentLoadProgress(data) {
var _frag$initSegment;
const {
frag,
part,
payload
} = data;
const {
levels
} = this;
if (!levels) {
this.warn(`Levels were reset while fragment load was in progress. Fragment ${frag.sn} of level ${frag.level} will not be buffered`);
return;
}
const currentLevel = levels[frag.level];
const details = currentLevel.details;
if (!details) {
this.warn(`Dropping fragment ${frag.sn} of level ${frag.level} after level details were reset`);
this.fragmentTracker.removeFragment(frag);
return;
}
const videoCodec = currentLevel.videoCodec;
// time Offset is accurate if level PTS is known, or if playlist is not sliding (not live)
const accurateTimeOffset = details.PTSKnown || !details.live;
const initSegmentData = (_frag$initSegment = frag.initSegment) == null ? void 0 : _frag$initSegment.data;
const audioCodec = this._getAudioCodec(currentLevel);
// transmux the MPEG-TS data to ISO-BMFF segments
// this.log(`Transmuxing ${frag.sn} of [${details.startSN} ,${details.endSN}],level ${frag.level}, cc ${frag.cc}`);
const transmuxer = this.transmuxer = this.transmuxer || new TransmuxerInterface(this.hls, PlaylistLevelType.MAIN, this._handleTransmuxComplete.bind(this), this._handleTransmuxerFlush.bind(this));
const partIndex = part ? part.index : -1;
const partial = partIndex !== -1;
const chunkMeta = new ChunkMetadata(frag.level, frag.sn, frag.stats.chunkCount, payload.byteLength, partIndex, partial);
const initPTS = this.initPTS[frag.cc];
transmuxer.push(payload, initSegmentData, audioCodec, videoCodec, frag, part, details.totalduration, accurateTimeOffset, chunkMeta, initPTS);
}
onAudioTrackSwitching(event, data) {
// if any URL found on new audio track, it is an alternate audio track
const fromAltAudio = this.altAudio;
const altAudio = !!data.url;
// if we switch on main audio, ensure that main fragment scheduling is synced with media.buffered
// don't do anything if we switch to alt audio: audio stream controller is handling it.
// we will just have to change buffer scheduling on audioTrackSwitched
if (!altAudio) {
if (this.mediaBuffer !== this.media) {
this.log('Switching on main audio, use media.buffered to schedule main fragment loading');
this.mediaBuffer = this.media;
const fragCurrent = this.fragCurrent;
// we need to refill audio buffer from main: cancel any frag loading to speed up audio switch
if (fragCurrent) {
this.log('Switching to main audio track, cancel main fragment load');
fragCurrent.abortRequests();
this.fragmentTracker.removeFragment(fragCurrent);
}
// destroy transmuxer to force init segment generation (following audio switch)
this.resetTransmuxer();
// switch to IDLE state to load new fragment
this.resetLoadingState();
} else if (this.audioOnly) {
// Reset audio transmuxer so when switching back to main audio we're not still appending where we left off
this.resetTransmuxer();
}
const hls = this.hls;
// If switching from alt to main audio, flush all audio and trigger track switched
if (fromAltAudio) {
hls.trigger(Events.BUFFER_FLUSHING, {
startOffset: 0,
endOffset: Number.POSITIVE_INFINITY,
type: null
});
this.fragmentTracker.removeAllFragments();
}
hls.trigger(Events.AUDIO_TRACK_SWITCHED, data);
}
}
onAudioTrackSwitched(event, data) {
const trackId = data.id;
const altAudio = !!this.hls.audioTracks[trackId].url;
if (altAudio) {
const videoBuffer = this.videoBuffer;
// if we switched on alternate audio, ensure that main fragment scheduling is synced with video sourcebuffer buffered
if (videoBuffer && this.mediaBuffer !== videoBuffer) {
this.log('Switching on alternate audio, use video.buffered to schedule main fragment loading');
this.mediaBuffer = videoBuffer;
}
}
this.altAudio = altAudio;
this.tick();
}
onBufferCreated(event, data) {
const tracks = data.tracks;
let mediaTrack;
let name;
let alternate = false;
for (const type in tracks) {
const track = tracks[type];
if (track.id === 'main') {
name = type;
mediaTrack = track;
// keep video source buffer reference
if (type === 'video') {
const videoTrack = tracks[type];
if (videoTrack) {
this.videoBuffer = videoTrack.buffer;
}
}
} else {
alternate = true;
}
}
if (alternate && mediaTrack) {
this.log(`Alternate track found, use ${name}.buffered to schedule main fragment loading`);
this.mediaBuffer = mediaTrack.buffer;
} else {
this.mediaBuffer = this.media;
}
}
onFragBuffered(event, data) {
const {
frag,
part
} = data;
if (frag && frag.type !== PlaylistLevelType.MAIN) {
return;
}
if (this.fragContextChanged(frag)) {
// If a level switch was requested while a fragment was buffering, it will emit the FRAG_BUFFERED event upon completion
// Avoid setting state back to IDLE, since that will interfere with a level switch
this.warn(`Fragment ${frag.sn}${part ? ' p: ' + part.index : ''} of level ${frag.level} finished buffering, but was aborted. state: ${this.state}`);
if (this.state === State.PARSED) {
this.state = State.IDLE;
}
return;
}
const stats = part ? part.stats : frag.stats;
this.fragLastKbps = Math.round(8 * stats.total / (stats.buffering.end - stats.loading.first));
if (frag.sn !== 'initSegment') {
this.fragPrevious = frag;
}
this.fragBufferedComplete(frag, part);
}
onError(event, data) {
var _data$context;
if (data.fatal) {
this.state = State.ERROR;
return;
}
switch (data.details) {
case ErrorDetails.FRAG_GAP:
case ErrorDetails.FRAG_PARSING_ERROR:
case ErrorDetails.FRAG_DECRYPT_ERROR:
case ErrorDetails.FRAG_LOAD_ERROR:
case ErrorDetails.FRAG_LOAD_TIMEOUT:
case ErrorDetails.KEY_LOAD_ERROR:
case ErrorDetails.KEY_LOAD_TIMEOUT:
this.onFragmentOrKeyLoadError(PlaylistLevelType.MAIN, data);
break;
case ErrorDetails.LEVEL_LOAD_ERROR:
case ErrorDetails.LEVEL_LOAD_TIMEOUT:
case ErrorDetails.LEVEL_PARSING_ERROR:
// in case of non fatal error while loading level, if level controller is not retrying to load level, switch back to IDLE
if (!data.levelRetry && this.state === State.WAITING_LEVEL && ((_data$context = data.context) == null ? void 0 : _data$context.type) === PlaylistContextType.LEVEL) {
this.state = State.IDLE;
}
break;
case ErrorDetails.BUFFER_FULL_ERROR:
if (!data.parent || data.parent !== 'main') {
return;
}
if (this.reduceLengthAndFlushBuffer(data)) {
this.flushMainBuffer(0, Number.POSITIVE_INFINITY);
}
break;
case ErrorDetails.INTERNAL_EXCEPTION:
this.recoverWorkerError(data);
break;
}
}
// Checks the health of the buffer and attempts to resolve playback stalls.
checkBuffer() {
const {
media,
gapController
} = this;
if (!media || !gapController || !media.readyState) {
// Exit early if we don't have media or if the media hasn't buffered anything yet (readyState 0)
return;
}
if (this.loadedmetadata || !BufferHelper.getBuffered(media).length) {
// Resolve gaps using the main buffer, whose ranges are the intersections of the A/V sourcebuffers
const activeFrag = this.state !== State.IDLE ? this.fragCurrent : null;
gapController.poll(this.lastCurrentTime, activeFrag);
}
this.lastCurrentTime = media.currentTime;
}
onFragLoadEmergencyAborted() {
this.state = State.IDLE;
// if loadedmetadata is not set, it means that we are emergency switch down on first frag
// in that case, reset startFragRequested flag
if (!this.loadedmetadata) {
this.startFragRequested = false;
this.nextLoadPosition = this.startPosition;
}
this.tickImmediate();
}
onBufferFlushed(event, {
type
}) {
if (type !== ElementaryStreamTypes.AUDIO || this.audioOnly && !this.altAudio) {
const mediaBuffer = (type === ElementaryStreamTypes.VIDEO ? this.videoBuffer : this.mediaBuffer) || this.media;
this.afterBufferFlushed(mediaBuffer, type, PlaylistLevelType.MAIN);
}
}
onLevelsUpdated(event, data) {
this.levels = data.levels;
}
swapAudioCodec() {
this.audioCodecSwap = !this.audioCodecSwap;
}
/**
* Seeks to the set startPosition if not equal to the mediaElement's current time.
*/
seekToStartPos() {
const {
media
} = this;
if (!media) {
return;
}
const currentTime = media.currentTime;
let startPosition = this.startPosition;
// only adjust currentTime if different from startPosition or if startPosition not buffered
// at that stage, there should be only one buffered range, as we reach that code after first fragment has been buffered
if (startPosition >= 0 && currentTime < startPosition) {
if (media.seeking) {
this.log(`could not seek to ${startPosition}, already seeking at ${currentTime}`);
return;
}
const buffered = BufferHelper.getBuffered(media);
const bufferStart = buffered.length ? buffered.start(0) : 0;
const delta = bufferStart - startPosition;
if (delta > 0 && (delta < this.config.maxBufferHole || delta < this.config.maxFragLookUpTolerance)) {
this.log(`adjusting start position by ${delta} to match buffer start`);
startPosition += delta;
this.startPosition = startPosition;
}
this.log(`seek to target start position ${startPosition} from current time ${currentTime}`);
media.currentTime = startPosition;
}
}
_getAudioCodec(currentLevel) {
let audioCodec = this.config.defaultAudioCodec || currentLevel.audioCodec;
if (this.audioCodecSwap && audioCodec) {
this.log('Swapping audio codec');
if (audioCodec.indexOf('mp4a.40.5') !== -1) {
audioCodec = 'mp4a.40.2';
} else {
audioCodec = 'mp4a.40.5';
}
}
return audioCodec;
}
_loadBitrateTestFrag(frag, level) {
frag.bitrateTest = true;
this._doFragLoad(frag, level).then(data => {
const {
hls
} = this;
if (!data || this.fragContextChanged(frag)) {
return;
}
level.fragmentError = 0;
this.state = State.IDLE;
this.startFragRequested = false;
this.bitrateTest = false;
const stats = frag.stats;
// Bitrate tests fragments are neither parsed nor buffered
stats.parsing.start = stats.parsing.end = stats.buffering.start = stats.buffering.end = self.performance.now();
hls.trigger(Events.FRAG_LOADED, data);
frag.bitrateTest = false;
});
}
_handleTransmuxComplete(transmuxResult) {
var _id3$samples;
const id = 'main';
const {
hls
} = this;
const {
remuxResult,
chunkMeta
} = transmuxResult;
const context = this.getCurrentContext(chunkMeta);
if (!context) {
this.resetWhenMissingContext(chunkMeta);
return;
}
const {
frag,
part,
level
} = context;
const {
video,
text,
id3,
initSegment
} = remuxResult;
const {
details
} = level;
// The audio-stream-controller handles audio buffering if Hls.js is playing an alternate audio track
const audio = this.altAudio ? undefined : remuxResult.audio;
// Check if the current fragment has been aborted. We check this by first seeing if we're still playing the current level.
// If we are, subsequently check if the currently loading fragment (fragCurrent) has changed.
if (this.fragContextChanged(frag)) {
this.fragmentTracker.removeFragment(frag);
return;
}
this.state = State.PARSING;
if (initSegment) {
if (initSegment != null && initSegment.tracks) {
const mapFragment = frag.initSegment || frag;
this._bufferInitSegment(level, initSegment.tracks, mapFragment, chunkMeta);
hls.trigger(Events.FRAG_PARSING_INIT_SEGMENT, {
frag: mapFragment,
id,
tracks: initSegment.tracks
});
}
// This would be nice if Number.isFinite acted as a typeguard, but it doesn't. See: https://github.com/Microsoft/TypeScript/issues/10038
const initPTS = initSegment.initPTS;
const timescale = initSegment.timescale;
if (isFiniteNumber(initPTS)) {
this.initPTS[frag.cc] = {
baseTime: initPTS,
timescale
};
hls.trigger(Events.INIT_PTS_FOUND, {
frag,
id,
initPTS,
timescale
});
}
}
// Avoid buffering if backtracking this fragment
if (video && details && frag.sn !== 'initSegment') {
const prevFrag = details.fragments[frag.sn - 1 - details.startSN];
const isFirstFragment = frag.sn === details.startSN;
const isFirstInDiscontinuity = !prevFrag || frag.cc > prevFrag.cc;
if (remuxResult.independent !== false) {
const {
startPTS,
endPTS,
startDTS,
endDTS
} = video;
if (part) {
part.elementaryStreams[video.type] = {
startPTS,
endPTS,
startDTS,
endDTS
};
} else {
if (video.firstKeyFrame && video.independent && chunkMeta.id === 1 && !isFirstInDiscontinuity) {
this.couldBacktrack = true;
}
if (video.dropped && video.independent) {
// Backtrack if dropped frames create a gap after currentTime
const bufferInfo = this.getMainFwdBufferInfo();
const targetBufferTime = (bufferInfo ? bufferInfo.end : this.getLoadPosition()) + this.config.maxBufferHole;
const startTime = video.firstKeyFramePTS ? video.firstKeyFramePTS : startPTS;
if (!isFirstFragment && targetBufferTime < startTime - this.config.maxBufferHole && !isFirstInDiscontinuity) {
this.backtrack(frag);
return;
} else if (isFirstInDiscontinuity) {
// Mark segment with a gap to avoid loop loading
frag.gap = true;
}
// Set video stream start to fragment start so that truncated samples do not distort the timeline, and mark it partial
frag.setElementaryStreamInfo(video.type, frag.start, endPTS, frag.start, endDTS, true);
}
}
frag.setElementaryStreamInfo(video.type, startPTS, endPTS, startDTS, endDTS);
if (this.backtrackFragment) {
this.backtrackFragment = frag;
}
this.bufferFragmentData(video, frag, part, chunkMeta, isFirstFragment || isFirstInDiscontinuity);
} else if (isFirstFragment || isFirstInDiscontinuity) {
// Mark segment with a gap to avoid loop loading
frag.gap = true;
} else {
this.backtrack(frag);
return;
}
}
if (audio) {
const {
startPTS,
endPTS,
startDTS,
endDTS
} = audio;
if (part) {
part.elementaryStreams[ElementaryStreamTypes.AUDIO] = {
startPTS,
endPTS,
startDTS,
endDTS
};
}
frag.setElementaryStreamInfo(ElementaryStreamTypes.AUDIO, startPTS, endPTS, startDTS, endDTS);
this.bufferFragmentData(audio, frag, part, chunkMeta);
}
if (details && id3 != null && (_id3$samples = id3.samples) != null && _id3$samples.length) {
const emittedID3 = {
id,
frag,
details,
samples: id3.samples
};
hls.trigger(Events.FRAG_PARSING_METADATA, emittedID3);
}
if (details && text) {
const emittedText = {
id,
frag,
details,
samples: text.samples
};
hls.trigger(Events.FRAG_PARSING_USERDATA, emittedText);
}
}
_bufferInitSegment(currentLevel, tracks, frag, chunkMeta) {
if (this.state !== State.PARSING) {
return;
}
this.audioOnly = !!tracks.audio && !tracks.video;
// if audio track is expected to come from audio stream controller, discard any coming from main
if (this.altAudio && !this.audioOnly) {
delete tracks.audio;
}
// include levelCodec in audio and video tracks
const {
audio,
video,
audiovideo
} = tracks;
if (audio) {
let audioCodec = currentLevel.audioCodec;
const ua = navigator.userAgent.toLowerCase();
if (this.audioCodecSwitch) {
if (audioCodec) {
if (audioCodec.indexOf('mp4a.40.5') !== -1) {
audioCodec = 'mp4a.40.2';
} else {
audioCodec = 'mp4a.40.5';
}
}
// In the case that AAC and HE-AAC audio codecs are signalled in manifest,
// force HE-AAC, as it seems that most browsers prefers it.
// don't force HE-AAC if mono stream, or in Firefox
if (audio.metadata.channelCount !== 1 && ua.indexOf('firefox') === -1) {
audioCodec = 'mp4a.40.5';
}
}
// HE-AAC is broken on Android, always signal audio codec as AAC even if variant manifest states otherwise
if (ua.indexOf('android') !== -1 && audio.container !== 'audio/mpeg') {
// Exclude mpeg audio
audioCodec = 'mp4a.40.2';
this.log(`Android: force audio codec to ${audioCodec}`);
}
if (currentLevel.audioCodec && currentLevel.audioCodec !== audioCodec) {
this.log(`Swapping manifest audio codec "${currentLevel.audioCodec}" for "${audioCodec}"`);
}
audio.levelCodec = audioCodec;
audio.id = 'main';
this.log(`Init audio buffer, container:${audio.container}, codecs[selected/level/parsed]=[${audioCodec || ''}/${currentLevel.audioCodec || ''}/${audio.codec}]`);
}
if (video) {
video.levelCodec = currentLevel.videoCodec;
video.id = 'main';
this.log(`Init video buffer, container:${video.container}, codecs[level/parsed]=[${currentLevel.videoCodec || ''}/${video.codec}]`);
}
if (audiovideo) {
this.log(`Init audiovideo buffer, container:${audiovideo.container}, codecs[level/parsed]=[${currentLevel.attrs.CODECS || ''}/${audiovideo.codec}]`);
}
this.hls.trigger(Events.BUFFER_CODECS, tracks);
// loop through tracks that are going to be provided to bufferController
Object.keys(tracks).forEach(trackName => {
const track = tracks[trackName];
const initSegment = track.initSegment;
if (initSegment != null && initSegment.byteLength) {
this.hls.trigger(Events.BUFFER_APPENDING, {
type: trackName,
data: initSegment,
frag,
part: null,
chunkMeta,
parent: frag.type
});
}
});
// trigger handler right now
this.tick();
}
getMainFwdBufferInfo() {
return this.getFwdBufferInfo(this.mediaBuffer ? this.mediaBuffer : this.media, PlaylistLevelType.MAIN);
}
backtrack(frag) {
this.couldBacktrack = true;
// Causes findFragments to backtrack through fragments to find the keyframe
this.backtrackFragment = frag;
this.resetTransmuxer();
this.flushBufferGap(frag);
this.fragmentTracker.removeFragment(frag);
this.fragPrevious = null;
this.nextLoadPosition = frag.start;
this.state = State.IDLE;
}
checkFragmentChanged() {
const video = this.media;
let fragPlayingCurrent = null;
if (video && video.readyState > 1 && video.seeking === false) {
const currentTime = video.currentTime;
/* if video element is in seeked state, currentTime can only increase.
(assuming that playback rate is positive ...)
As sometimes currentTime jumps back to zero after a
media decode error, check this, to avoid seeking back to
wrong position after a media decode error
*/
if (BufferHelper.isBuffered(video, currentTime)) {
fragPlayingCurrent = this.getAppendedFrag(currentTime);
} else if (BufferHelper.isBuffered(video, currentTime + 0.1)) {
/* ensure that FRAG_CHANGED event is triggered at startup,
when first video frame is displayed and playback is paused.
add a tolerance of 100ms, in case current position is not buffered,
check if current pos+100ms is buffered and use that buffer range
for FRAG_CHANGED event reporting */
fragPlayingCurrent = this.getAppendedFrag(currentTime + 0.1);
}
if (fragPlayingCurrent) {
this.backtrackFragment = null;
const fragPlaying = this.fragPlaying;
const fragCurrentLevel = fragPlayingCurrent.level;
if (!fragPlaying || fragPlayingCurrent.sn !== fragPlaying.sn || fragPlaying.level !== fragCurrentLevel || fragPlayingCurrent.urlId !== fragPlaying.urlId) {
this.fragPlaying = fragPlayingCurrent;
this.hls.trigger(Events.FRAG_CHANGED, {
frag: fragPlayingCurrent
});
if (!fragPlaying || fragPlaying.level !== fragCurrentLevel) {
this.hls.trigger(Events.LEVEL_SWITCHED, {
level: fragCurrentLevel
});
}
}
}
}
}
get nextLevel() {
const frag = this.nextBufferedFrag;
if (frag) {
return frag.level;
}
return -1;
}
get currentFrag() {
const media = this.media;
if (media) {
return this.fragPlaying || this.getAppendedFrag(media.currentTime);
}
return null;
}
get currentProgramDateTime() {
const media = this.media;
if (media) {
const currentTime = media.currentTime;
const frag = this.currentFrag;
if (frag && isFiniteNumber(currentTime) && isFiniteNumber(frag.programDateTime)) {
const epocMs = frag.programDateTime + (currentTime - frag.start) * 1000;
return new Date(epocMs);
}
}
return null;
}
get currentLevel() {
const frag = this.currentFrag;
if (frag) {
return frag.level;
}
return -1;
}
get nextBufferedFrag() {
const frag = this.currentFrag;
if (frag) {
return this.followingBufferedFrag(frag);
}
return null;
}
get forceStartLoad() {
return this._forceStartLoad;
}
}
/*
* compute an Exponential Weighted moving average
* - https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
* - heavily inspired from shaka-player
*/
class EWMA {
// About half of the estimated value will be from the last |halfLife| samples by weight.
constructor(halfLife, estimate = 0, weight = 0) {
this.halfLife = void 0;
this.alpha_ = void 0;
this.estimate_ = void 0;
this.totalWeight_ = void 0;
this.halfLife = halfLife;
// Larger values of alpha expire historical data more slowly.
this.alpha_ = halfLife ? Math.exp(Math.log(0.5) / halfLife) : 0;
this.estimate_ = estimate;
this.totalWeight_ = weight;
}
sample(weight, value) {
const adjAlpha = Math.pow(this.alpha_, weight);
this.estimate_ = value * (1 - adjAlpha) + adjAlpha * this.estimate_;
this.totalWeight_ += weight;
}
getTotalWeight() {
return this.totalWeight_;
}
getEstimate() {
if (this.alpha_) {
const zeroFactor = 1 - Math.pow(this.alpha_, this.totalWeight_);
if (zeroFactor) {
return this.estimate_ / zeroFactor;
}
}
return this.estimate_;
}
}
/*
* EWMA Bandwidth Estimator
* - heavily inspired from shaka-player
* Tracks bandwidth samples and estimates available bandwidth.
* Based on the minimum of two exponentially-weighted moving averages with
* different half-lives.
*/
class EwmaBandWidthEstimator {
constructor(slow, fast, defaultEstimate, defaultTTFB = 100) {
this.defaultEstimate_ = void 0;
this.minWeight_ = void 0;
this.minDelayMs_ = void 0;
this.slow_ = void 0;
this.fast_ = void 0;
this.defaultTTFB_ = void 0;
this.ttfb_ = void 0;
this.defaultEstimate_ = defaultEstimate;
this.minWeight_ = 0.001;
this.minDelayMs_ = 50;
this.slow_ = new EWMA(slow);
this.fast_ = new EWMA(fast);
this.defaultTTFB_ = defaultTTFB;
this.ttfb_ = new EWMA(slow);
}
update(slow, fast) {
const {
slow_,
fast_,
ttfb_
} = this;
if (slow_.halfLife !== slow) {
this.slow_ = new EWMA(slow, slow_.getEstimate(), slow_.getTotalWeight());
}
if (fast_.halfLife !== fast) {
this.fast_ = new EWMA(fast, fast_.getEstimate(), fast_.getTotalWeight());
}
if (ttfb_.halfLife !== slow) {
this.ttfb_ = new EWMA(slow, ttfb_.getEstimate(), ttfb_.getTotalWeight());
}
}
sample(durationMs, numBytes) {
durationMs = Math.max(durationMs, this.minDelayMs_);
const numBits = 8 * numBytes;
// weight is duration in seconds
const durationS = durationMs / 1000;
// value is bandwidth in bits/s
const bandwidthInBps = numBits / durationS;
this.fast_.sample(durationS, bandwidthInBps);
this.slow_.sample(durationS, bandwidthInBps);
}
sampleTTFB(ttfb) {
// weight is frequency curve applied to TTFB in seconds
// (longer times have less weight with expected input under 1 second)
const seconds = ttfb / 1000;
const weight = Math.sqrt(2) * Math.exp(-Math.pow(seconds, 2) / 2);
this.ttfb_.sample(weight, Math.max(ttfb, 5));
}
canEstimate() {
return this.fast_.getTotalWeight() >= this.minWeight_;
}
getEstimate() {
if (this.canEstimate()) {
// console.log('slow estimate:'+ Math.round(this.slow_.getEstimate()));
// console.log('fast estimate:'+ Math.round(this.fast_.getEstimate()));
// Take the minimum of these two estimates. This should have the effect of
// adapting down quickly, but up more slowly.
return Math.min(this.fast_.getEstimate(), this.slow_.getEstimate());
} else {
return this.defaultEstimate_;
}
}
getEstimateTTFB() {
if (this.ttfb_.getTotalWeight() >= this.minWeight_) {
return this.ttfb_.getEstimate();
} else {
return this.defaultTTFB_;
}
}
destroy() {}
}
class AbrController {
constructor(hls) {
this.hls = void 0;
this.lastLevelLoadSec = 0;
this.lastLoadedFragLevel = 0;
this._nextAutoLevel = -1;
this.timer = -1;
this.onCheck = this._abandonRulesCheck.bind(this);
this.fragCurrent = null;
this.partCurrent = null;
this.bitrateTestDelay = 0;
this.bwEstimator = void 0;
this.hls = hls;
const config = hls.config;
this.bwEstimator = new EwmaBandWidthEstimator(config.abrEwmaSlowVoD, config.abrEwmaFastVoD, config.abrEwmaDefaultEstimate);
this.registerListeners();
}
registerListeners() {
const {
hls
} = this;
hls.on(Events.FRAG_LOADING, this.onFragLoading, this);
hls.on(Events.FRAG_LOADED, this.onFragLoaded, this);
hls.on(Events.FRAG_BUFFERED, this.onFragBuffered, this);
hls.on(Events.LEVEL_SWITCHING, this.onLevelSwitching, this);
hls.on(Events.LEVEL_LOADED, this.onLevelLoaded, this);
}
unregisterListeners() {
const {
hls
} = this;
hls.off(Events.FRAG_LOADING, this.onFragLoading, this);
hls.off(Events.FRAG_LOADED, this.onFragLoaded, this);
hls.off(Events.FRAG_BUFFERED, this.onFragBuffered, this);
hls.off(Events.LEVEL_SWITCHING, this.onLevelSwitching, this);
hls.off(Events.LEVEL_LOADED, this.onLevelLoaded, this);
}
destroy() {
this.unregisterListeners();
this.clearTimer();
// @ts-ignore
this.hls = this.onCheck = null;
this.fragCurrent = this.partCurrent = null;
}
onFragLoading(event, data) {
var _data$part;
const frag = data.frag;
if (this.ignoreFragment(frag)) {
return;
}
this.fragCurrent = frag;
this.partCurrent = (_data$part = data.part) != null ? _data$part : null;
this.clearTimer();
this.timer = self.setInterval(this.onCheck, 100);
}
onLevelSwitching(event, data) {
this.clearTimer();
}
getTimeToLoadFrag(timeToFirstByteSec, bandwidth, fragSizeBits, isSwitch) {
const fragLoadSec = timeToFirstByteSec + fragSizeBits / bandwidth;
const playlistLoadSec = isSwitch ? this.lastLevelLoadSec : 0;
return fragLoadSec + playlistLoadSec;
}
onLevelLoaded(event, data) {
const config = this.hls.config;
const {
total,
bwEstimate
} = data.stats;
// Total is the bytelength and bwEstimate in bits/sec
if (isFiniteNumber(total) && isFiniteNumber(bwEstimate)) {
this.lastLevelLoadSec = 8 * total / bwEstimate;
}
if (data.details.live) {
this.bwEstimator.update(config.abrEwmaSlowLive, config.abrEwmaFastLive);
} else {
this.bwEstimator.update(config.abrEwmaSlowVoD, config.abrEwmaFastVoD);
}
}
/*
This method monitors the download rate of the current fragment, and will downswitch if that fragment will not load
quickly enough to prevent underbuffering
*/
_abandonRulesCheck() {
const {
fragCurrent: frag,
partCurrent: part,
hls
} = this;
const {
autoLevelEnabled,
media
} = hls;
if (!frag || !media) {
return;
}
const now = performance.now();
const stats = part ? part.stats : frag.stats;
const duration = part ? part.duration : frag.duration;
const timeLoading = now - stats.loading.start;
// If frag loading is aborted, complete, or from lowest level, stop timer and return
if (stats.aborted || stats.loaded && stats.loaded === stats.total || frag.level === 0) {
this.clearTimer();
// reset forced auto level value so that next level will be selected
this._nextAutoLevel = -1;
return;
}
// This check only runs if we're in ABR mode and actually playing
if (!autoLevelEnabled || media.paused || !media.playbackRate || !media.readyState) {
return;
}
const bufferInfo = hls.mainForwardBufferInfo;
if (bufferInfo === null) {
return;
}
const ttfbEstimate = this.bwEstimator.getEstimateTTFB();
const playbackRate = Math.abs(media.playbackRate);
// To maintain stable adaptive playback, only begin monitoring frag loading after half or more of its playback duration has passed
if (timeLoading <= Math.max(ttfbEstimate, 1000 * (duration / (playbackRate * 2)))) {
return;
}
// bufferStarvationDelay is an estimate of the amount time (in seconds) it will take to exhaust the buffer
const bufferStarvationDelay = bufferInfo.len / playbackRate;
// Only downswitch if less than 2 fragment lengths are buffered
if (bufferStarvationDelay >= 2 * duration / playbackRate) {
return;
}
const ttfb = stats.loading.first ? stats.loading.first - stats.loading.start : -1;
const loadedFirstByte = stats.loaded && ttfb > -1;
const bwEstimate = this.bwEstimator.getEstimate();
const {
levels,
minAutoLevel
} = hls;
const level = levels[frag.level];
const expectedLen = stats.total || Math.max(stats.loaded, Math.round(duration * level.maxBitrate / 8));
let timeStreaming = timeLoading - ttfb;
if (timeStreaming < 1 && loadedFirstByte) {
timeStreaming = Math.min(timeLoading, stats.loaded * 8 / bwEstimate);
}
const loadRate = loadedFirstByte ? stats.loaded * 1000 / timeStreaming : 0;
// fragLoadDelay is an estimate of the time (in seconds) it will take to buffer the remainder of the fragment
const fragLoadedDelay = loadRate ? (expectedLen - stats.loaded) / loadRate : expectedLen * 8 / bwEstimate + ttfbEstimate / 1000;
// Only downswitch if the time to finish loading the current fragment is greater than the amount of buffer left
if (fragLoadedDelay <= bufferStarvationDelay) {
return;
}
const bwe = loadRate ? loadRate * 8 : bwEstimate;
let fragLevelNextLoadedDelay = Number.POSITIVE_INFINITY;
let nextLoadLevel;
// Iterate through lower level and try to find the largest one that avoids rebuffering
for (nextLoadLevel = frag.level - 1; nextLoadLevel > minAutoLevel; nextLoadLevel--) {
// compute time to load next fragment at lower level
// 8 = bits per byte (bps/Bps)
const levelNextBitrate = levels[nextLoadLevel].maxBitrate;
fragLevelNextLoadedDelay = this.getTimeToLoadFrag(ttfbEstimate / 1000, bwe, duration * levelNextBitrate, !levels[nextLoadLevel].details);
if (fragLevelNextLoadedDelay < bufferStarvationDelay) {
break;
}
}
// Only emergency switch down if it takes less time to load a new fragment at lowest level instead of continuing
// to load the current one
if (fragLevelNextLoadedDelay >= fragLoadedDelay) {
return;
}
// if estimated load time of new segment is completely unreasonable, ignore and do not emergency switch down
if (fragLevelNextLoadedDelay > duration * 10) {
return;
}
hls.nextLoadLevel = nextLoadLevel;
if (loadedFirstByte) {
// If there has been loading progress, sample bandwidth using loading time offset by minimum TTFB time
this.bwEstimator.sample(timeLoading - Math.min(ttfbEstimate, ttfb), stats.loaded);
} else {
// If there has been no loading progress, sample TTFB
this.bwEstimator.sampleTTFB(timeLoading);
}
this.clearTimer();
logger.warn(`[abr] Fragment ${frag.sn}${part ? ' part ' + part.index : ''} of level ${frag.level} is loading too slowly;
Time to underbuffer: ${bufferStarvationDelay.toFixed(3)} s
Estimated load time for current fragment: ${fragLoadedDelay.toFixed(3)} s
Estimated load time for down switch fragment: ${fragLevelNextLoadedDelay.toFixed(3)} s
TTFB estimate: ${ttfb}
Current BW estimate: ${isFiniteNumber(bwEstimate) ? (bwEstimate / 1024).toFixed(3) : 'Unknown'} Kb/s
New BW estimate: ${(this.bwEstimator.getEstimate() / 1024).toFixed(3)} Kb/s
Aborting and switching to level ${nextLoadLevel}`);
if (frag.loader) {
this.fragCurrent = this.partCurrent = null;
frag.abortRequests();
}
hls.trigger(Events.FRAG_LOAD_EMERGENCY_ABORTED, {
frag,
part,
stats
});
}
onFragLoaded(event, {
frag,
part
}) {
const stats = part ? part.stats : frag.stats;
if (frag.type === PlaylistLevelType.MAIN) {
this.bwEstimator.sampleTTFB(stats.loading.first - stats.loading.start);
}
if (this.ignoreFragment(frag)) {
return;
}
// stop monitoring bw once frag loaded
this.clearTimer();
// store level id after successful fragment load
this.lastLoadedFragLevel = frag.level;
// reset forced auto level value so that next level will be selected
this._nextAutoLevel = -1;
// compute level average bitrate
if (this.hls.config.abrMaxWithRealBitrate) {
const duration = part ? part.duration : frag.duration;
const level = this.hls.levels[frag.level];
const loadedBytes = (level.loaded ? level.loaded.bytes : 0) + stats.loaded;
const loadedDuration = (level.loaded ? level.loaded.duration : 0) + duration;
level.loaded = {
bytes: loadedBytes,
duration: loadedDuration
};
level.realBitrate = Math.round(8 * loadedBytes / loadedDuration);
}
if (frag.bitrateTest) {
const fragBufferedData = {
stats,
frag,
part,
id: frag.type
};
this.onFragBuffered(Events.FRAG_BUFFERED, fragBufferedData);
frag.bitrateTest = false;
}
}
onFragBuffered(event, data) {
const {
frag,
part
} = data;
const stats = part != null && part.stats.loaded ? part.stats : frag.stats;
if (stats.aborted) {
return;
}
if (this.ignoreFragment(frag)) {
return;
}
// Use the difference between parsing and request instead of buffering and request to compute fragLoadingProcessing;
// rationale is that buffer appending only happens once media is attached. This can happen when config.startFragPrefetch
// is used. If we used buffering in that case, our BW estimate sample will be very large.
const processingMs = stats.parsing.end - stats.loading.start - Math.min(stats.loading.first - stats.loading.start, this.bwEstimator.getEstimateTTFB());
this.bwEstimator.sample(processingMs, stats.loaded);
stats.bwEstimate = this.bwEstimator.getEstimate();
if (frag.bitrateTest) {
this.bitrateTestDelay = processingMs / 1000;
} else {
this.bitrateTestDelay = 0;
}
}
ignoreFragment(frag) {
// Only count non-alt-audio frags which were actually buffered in our BW calculations
return frag.type !== PlaylistLevelType.MAIN || frag.sn === 'initSegment';
}
clearTimer() {
self.clearInterval(this.timer);
}
// return next auto level
get nextAutoLevel() {
const forcedAutoLevel = this._nextAutoLevel;
const bwEstimator = this.bwEstimator;
// in case next auto level has been forced, and bw not available or not reliable, return forced value
if (forcedAutoLevel !== -1 && !bwEstimator.canEstimate()) {
return forcedAutoLevel;
}
// compute next level using ABR logic
let nextABRAutoLevel = this.getNextABRAutoLevel();
// use forced auto level when ABR selected level has errored
if (forcedAutoLevel !== -1) {
const levels = this.hls.levels;
if (levels.length > Math.max(forcedAutoLevel, nextABRAutoLevel) && levels[forcedAutoLevel].loadError <= levels[nextABRAutoLevel].loadError) {
return forcedAutoLevel;
}
}
// if forced auto level has been defined, use it to cap ABR computed quality level
if (forcedAutoLevel !== -1) {
nextABRAutoLevel = Math.min(forcedAutoLevel, nextABRAutoLevel);
}
return nextABRAutoLevel;
}
getNextABRAutoLevel() {
const {
fragCurrent,
partCurrent,
hls
} = this;
const {
maxAutoLevel,
config,
minAutoLevel,
media
} = hls;
const currentFragDuration = partCurrent ? partCurrent.duration : fragCurrent ? fragCurrent.duration : 0;
// playbackRate is the absolute value of the playback rate; if media.playbackRate is 0, we use 1 to load as
// if we're playing back at the normal rate.
const playbackRate = media && media.playbackRate !== 0 ? Math.abs(media.playbackRate) : 1.0;
const avgbw = this.bwEstimator ? this.bwEstimator.getEstimate() : config.abrEwmaDefaultEstimate;
// bufferStarvationDelay is the wall-clock time left until the playback buffer is exhausted.
const bufferInfo = hls.mainForwardBufferInfo;
const bufferStarvationDelay = (bufferInfo ? bufferInfo.len : 0) / playbackRate;
// First, look to see if we can find a level matching with our avg bandwidth AND that could also guarantee no rebuffering at all
let bestLevel = this.findBestLevel(avgbw, minAutoLevel, maxAutoLevel, bufferStarvationDelay, config.abrBandWidthFactor, config.abrBandWidthUpFactor);
if (bestLevel >= 0) {
return bestLevel;
}
logger.trace(`[abr] ${bufferStarvationDelay ? 'rebuffering expected' : 'buffer is empty'}, finding optimal quality level`);
// not possible to get rid of rebuffering ... let's try to find level that will guarantee less than maxStarvationDelay of rebuffering
// if no matching level found, logic will return 0
let maxStarvationDelay = currentFragDuration ? Math.min(currentFragDuration, config.maxStarvationDelay) : config.maxStarvationDelay;
let bwFactor = config.abrBandWidthFactor;
let bwUpFactor = config.abrBandWidthUpFactor;
if (!bufferStarvationDelay) {
// in case buffer is empty, let's check if previous fragment was loaded to perform a bitrate test
const bitrateTestDelay = this.bitrateTestDelay;
if (bitrateTestDelay) {
// if it is the case, then we need to adjust our max starvation delay using maxLoadingDelay config value
// max video loading delay used in automatic start level selection :
// in that mode ABR controller will ensure that video loading time (ie the time to fetch the first fragment at lowest quality level +
// the time to fetch the fragment at the appropriate quality level is less than ```maxLoadingDelay``` )
// cap maxLoadingDelay and ensure it is not bigger 'than bitrate test' frag duration
const maxLoadingDelay = currentFragDuration ? Math.min(currentFragDuration, config.maxLoadingDelay) : config.maxLoadingDelay;
maxStarvationDelay = maxLoadingDelay - bitrateTestDelay;
logger.trace(`[abr] bitrate test took ${Math.round(1000 * bitrateTestDelay)}ms, set first fragment max fetchDuration to ${Math.round(1000 * maxStarvationDelay)} ms`);
// don't use conservative factor on bitrate test
bwFactor = bwUpFactor = 1;
}
}
bestLevel = this.findBestLevel(avgbw, minAutoLevel, maxAutoLevel, bufferStarvationDelay + maxStarvationDelay, bwFactor, bwUpFactor);
return Math.max(bestLevel, 0);
}
findBestLevel(currentBw, minAutoLevel, maxAutoLevel, maxFetchDuration, bwFactor, bwUpFactor) {
var _level$details;
const {
fragCurrent,
partCurrent,
lastLoadedFragLevel: currentLevel
} = this;
const {
levels
} = this.hls;
const level = levels[currentLevel];
const live = !!(level != null && (_level$details = level.details) != null && _level$details.live);
const currentCodecSet = level == null ? void 0 : level.codecSet;
const currentFragDuration = partCurrent ? partCurrent.duration : fragCurrent ? fragCurrent.duration : 0;
const ttfbEstimateSec = this.bwEstimator.getEstimateTTFB() / 1000;
let levelSkippedMin = minAutoLevel;
let levelSkippedMax = -1;
for (let i = maxAutoLevel; i >= minAutoLevel; i--) {
const levelInfo = levels[i];
if (!levelInfo || currentCodecSet && levelInfo.codecSet !== currentCodecSet) {
if (levelInfo) {
levelSkippedMin = Math.min(i, levelSkippedMin);
levelSkippedMax = Math.max(i, levelSkippedMax);
}
continue;
}
if (levelSkippedMax !== -1) {
logger.trace(`[abr] Skipped level(s) ${levelSkippedMin}-${levelSkippedMax} with CODECS:"${levels[levelSkippedMax].attrs.CODECS}"; not compatible with "${level.attrs.CODECS}"`);
}
const levelDetails = levelInfo.details;
const avgDuration = (partCurrent ? levelDetails == null ? void 0 : levelDetails.partTarget : levelDetails == null ? void 0 : levelDetails.averagetargetduration) || currentFragDuration;
let adjustedbw;
// follow algorithm captured from stagefright :
// https://android.googlesource.com/platform/frameworks/av/+/master/media/libstagefright/httplive/LiveSession.cpp
// Pick the highest bandwidth stream below or equal to estimated bandwidth.
// consider only 80% of the available bandwidth, but if we are switching up,
// be even more conservative (70%) to avoid overestimating and immediately
// switching back.
if (i <= currentLevel) {
adjustedbw = bwFactor * currentBw;
} else {
adjustedbw = bwUpFactor * currentBw;
}
const bitrate = levels[i].maxBitrate;
const fetchDuration = this.getTimeToLoadFrag(ttfbEstimateSec, adjustedbw, bitrate * avgDuration, levelDetails === undefined);
logger.trace(`[abr] level:${i} adjustedbw-bitrate:${Math.round(adjustedbw - bitrate)} avgDuration:${avgDuration.toFixed(1)} maxFetchDuration:${maxFetchDuration.toFixed(1)} fetchDuration:${fetchDuration.toFixed(1)}`);
// if adjusted bw is greater than level bitrate AND
if (adjustedbw > bitrate && (
// fragment fetchDuration unknown OR live stream OR fragment fetchDuration less than max allowed fetch duration, then this level matches
// we don't account for max Fetch Duration for live streams, this is to avoid switching down when near the edge of live sliding window ...
// special case to support startLevel = -1 (bitrateTest) on live streams : in that case we should not exit loop so that findBestLevel will return -1
fetchDuration === 0 || !isFiniteNumber(fetchDuration) || live && !this.bitrateTestDelay || fetchDuration < maxFetchDuration)) {
// as we are looping from highest to lowest, this will return the best achievable quality level
return i;
}
}
// not enough time budget even with quality level 0 ... rebuffering might happen
return -1;
}
set nextAutoLevel(nextLevel) {
this._nextAutoLevel = nextLevel;
}
}
class ChunkCache {
constructor() {
this.chunks = [];
this.dataLength = 0;
}
push(chunk) {
this.chunks.push(chunk);
this.dataLength += chunk.length;
}
flush() {
const {
chunks,
dataLength
} = this;
let result;
if (!chunks.length) {
return new Uint8Array(0);
} else if (chunks.length === 1) {
result = chunks[0];
} else {
result = concatUint8Arrays(chunks, dataLength);
}
this.reset();
return result;
}
reset() {
this.chunks.length = 0;
this.dataLength = 0;
}
}
function concatUint8Arrays(chunks, dataLength) {
const result = new Uint8Array(dataLength);
let offset = 0;
for (let i = 0; i < chunks.length; i++) {
const chunk = chunks[i];
result.set(chunk, offset);
offset += chunk.length;
}
return result;
}
const TICK_INTERVAL$1 = 100; // how often to tick in ms
class AudioStreamController extends BaseStreamController {
constructor(hls, fragmentTracker, keyLoader) {
super(hls, fragmentTracker, keyLoader, '[audio-stream-controller]', PlaylistLevelType.AUDIO);
this.videoBuffer = null;
this.videoTrackCC = -1;
this.waitingVideoCC = -1;
this.bufferedTrack = null;
this.switchingTrack = null;
this.trackId = -1;
this.waitingData = null;
this.mainDetails = null;
this.bufferFlushed = false;
this.cachedTrackLoadedData = null;
this._registerListeners();
}
onHandlerDestroying() {
this._unregisterListeners();
this.mainDetails = null;
this.bufferedTrack = null;
this.switchingTrack = null;
}
_registerListeners() {
const {
hls
} = this;
hls.on(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
hls.on(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.on(Events.LEVEL_LOADED, this.onLevelLoaded, this);
hls.on(Events.AUDIO_TRACKS_UPDATED, this.onAudioTracksUpdated, this);
hls.on(Events.AUDIO_TRACK_SWITCHING, this.onAudioTrackSwitching, this);
hls.on(Events.AUDIO_TRACK_LOADED, this.onAudioTrackLoaded, this);
hls.on(Events.ERROR, this.onError, this);
hls.on(Events.BUFFER_RESET, this.onBufferReset, this);
hls.on(Events.BUFFER_CREATED, this.onBufferCreated, this);
hls.on(Events.BUFFER_FLUSHED, this.onBufferFlushed, this);
hls.on(Events.INIT_PTS_FOUND, this.onInitPtsFound, this);
hls.on(Events.FRAG_BUFFERED, this.onFragBuffered, this);
}
_unregisterListeners() {
const {
hls
} = this;
hls.off(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
hls.off(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.off(Events.LEVEL_LOADED, this.onLevelLoaded, this);
hls.off(Events.AUDIO_TRACKS_UPDATED, this.onAudioTracksUpdated, this);
hls.off(Events.AUDIO_TRACK_SWITCHING, this.onAudioTrackSwitching, this);
hls.off(Events.AUDIO_TRACK_LOADED, this.onAudioTrackLoaded, this);
hls.off(Events.ERROR, this.onError, this);
hls.off(Events.BUFFER_RESET, this.onBufferReset, this);
hls.off(Events.BUFFER_CREATED, this.onBufferCreated, this);
hls.off(Events.BUFFER_FLUSHED, this.onBufferFlushed, this);
hls.off(Events.INIT_PTS_FOUND, this.onInitPtsFound, this);
hls.off(Events.FRAG_BUFFERED, this.onFragBuffered, this);
}
// INIT_PTS_FOUND is triggered when the video track parsed in the stream-controller has a new PTS value
onInitPtsFound(event, {
frag,
id,
initPTS,
timescale
}) {
// Always update the new INIT PTS
// Can change due level switch
if (id === 'main') {
const cc = frag.cc;
this.initPTS[frag.cc] = {
baseTime: initPTS,
timescale
};
this.log(`InitPTS for cc: ${cc} found from main: ${initPTS}`);
this.videoTrackCC = cc;
// If we are waiting, tick immediately to unblock audio fragment transmuxing
if (this.state === State.WAITING_INIT_PTS) {
this.tick();
}
}
}
startLoad(startPosition) {
if (!this.levels) {
this.startPosition = startPosition;
this.state = State.STOPPED;
return;
}
const lastCurrentTime = this.lastCurrentTime;
this.stopLoad();
this.setInterval(TICK_INTERVAL$1);
if (lastCurrentTime > 0 && startPosition === -1) {
this.log(`Override startPosition with lastCurrentTime @${lastCurrentTime.toFixed(3)}`);
startPosition = lastCurrentTime;
this.state = State.IDLE;
} else {
this.loadedmetadata = false;
this.state = State.WAITING_TRACK;
}
this.nextLoadPosition = this.startPosition = this.lastCurrentTime = startPosition;
this.tick();
}
doTick() {
switch (this.state) {
case State.IDLE:
this.doTickIdle();
break;
case State.WAITING_TRACK:
{
var _levels$trackId;
const {
levels,
trackId
} = this;
const details = levels == null ? void 0 : (_levels$trackId = levels[trackId]) == null ? void 0 : _levels$trackId.details;
if (details) {
if (this.waitForCdnTuneIn(details)) {
break;
}
this.state = State.WAITING_INIT_PTS;
}
break;
}
case State.FRAG_LOADING_WAITING_RETRY:
{
var _this$media;
const now = performance.now();
const retryDate = this.retryDate;
// if current time is gt than retryDate, or if media seeking let's switch to IDLE state to retry loading
if (!retryDate || now >= retryDate || (_this$media = this.media) != null && _this$media.seeking) {
this.log('RetryDate reached, switch back to IDLE state');
this.resetStartWhenNotLoaded(this.trackId);
this.state = State.IDLE;
}
break;
}
case State.WAITING_INIT_PTS:
{
// Ensure we don't get stuck in the WAITING_INIT_PTS state if the waiting frag CC doesn't match any initPTS
const waitingData = this.waitingData;
if (waitingData) {
const {
frag,
part,
cache,
complete
} = waitingData;
if (this.initPTS[frag.cc] !== undefined) {
this.waitingData = null;
this.waitingVideoCC = -1;
this.state = State.FRAG_LOADING;
const payload = cache.flush();
const data = {
frag,
part,
payload,
networkDetails: null
};
this._handleFragmentLoadProgress(data);
if (complete) {
super._handleFragmentLoadComplete(data);
}
} else if (this.videoTrackCC !== this.waitingVideoCC) {
// Drop waiting fragment if videoTrackCC has changed since waitingFragment was set and initPTS was not found
this.log(`Waiting fragment cc (${frag.cc}) cancelled because video is at cc ${this.videoTrackCC}`);
this.clearWaitingFragment();
} else {
// Drop waiting fragment if an earlier fragment is needed
const pos = this.getLoadPosition();
const bufferInfo = BufferHelper.bufferInfo(this.mediaBuffer, pos, this.config.maxBufferHole);
const waitingFragmentAtPosition = fragmentWithinToleranceTest(bufferInfo.end, this.config.maxFragLookUpTolerance, frag);
if (waitingFragmentAtPosition < 0) {
this.log(`Waiting fragment cc (${frag.cc}) @ ${frag.start} cancelled because another fragment at ${bufferInfo.end} is needed`);
this.clearWaitingFragment();
}
}
} else {
this.state = State.IDLE;
}
}
}
this.onTickEnd();
}
clearWaitingFragment() {
const waitingData = this.waitingData;
if (waitingData) {
this.fragmentTracker.removeFragment(waitingData.frag);
this.waitingData = null;
this.waitingVideoCC = -1;
this.state = State.IDLE;
}
}
resetLoadingState() {
this.clearWaitingFragment();
super.resetLoadingState();
}
onTickEnd() {
const {
media
} = this;
if (!(media != null && media.readyState)) {
// Exit early if we don't have media or if the media hasn't buffered anything yet (readyState 0)
return;
}
this.lastCurrentTime = media.currentTime;
}
doTickIdle() {
const {
hls,
levels,
media,
trackId
} = this;
const config = hls.config;
if (!(levels != null && levels[trackId])) {
return;
}
// if video not attached AND
// start fragment already requested OR start frag prefetch not enabled
// exit loop
// => if media not attached but start frag prefetch is enabled and start frag not requested yet, we will not exit loop
if (!media && (this.startFragRequested || !config.startFragPrefetch)) {
return;
}
const levelInfo = levels[trackId];
const trackDetails = levelInfo.details;
if (!trackDetails || trackDetails.live && this.levelLastLoaded !== trackId || this.waitForCdnTuneIn(trackDetails)) {
this.state = State.WAITING_TRACK;
return;
}
const bufferable = this.mediaBuffer ? this.mediaBuffer : this.media;
if (this.bufferFlushed && bufferable) {
this.bufferFlushed = false;
this.afterBufferFlushed(bufferable, ElementaryStreamTypes.AUDIO, PlaylistLevelType.AUDIO);
}
const bufferInfo = this.getFwdBufferInfo(bufferable, PlaylistLevelType.AUDIO);
if (bufferInfo === null) {
return;
}
const {
bufferedTrack,
switchingTrack
} = this;
if (!switchingTrack && this._streamEnded(bufferInfo, trackDetails)) {
hls.trigger(Events.BUFFER_EOS, {
type: 'audio'
});
this.state = State.ENDED;
return;
}
const mainBufferInfo = this.getFwdBufferInfo(this.videoBuffer ? this.videoBuffer : this.media, PlaylistLevelType.MAIN);
const bufferLen = bufferInfo.len;
const maxBufLen = this.getMaxBufferLength(mainBufferInfo == null ? void 0 : mainBufferInfo.len);
// if buffer length is less than maxBufLen try to load a new fragment
if (bufferLen >= maxBufLen && !switchingTrack) {
return;
}
const fragments = trackDetails.fragments;
const start = fragments[0].start;
let targetBufferTime = bufferInfo.end;
if (switchingTrack && media) {
const pos = this.getLoadPosition();
if (bufferedTrack && switchingTrack.attrs !== bufferedTrack.attrs) {
targetBufferTime = pos;
}
// if currentTime (pos) is less than alt audio playlist start time, it means that alt audio is ahead of currentTime
if (trackDetails.PTSKnown && pos < start) {
// if everything is buffered from pos to start or if audio buffer upfront, let's seek to start
if (bufferInfo.end > start || bufferInfo.nextStart) {
this.log('Alt audio track ahead of main track, seek to start of alt audio track');
media.currentTime = start + 0.05;
}
}
}
let frag = this.getNextFragment(targetBufferTime, trackDetails);
let atGap = false;
// Avoid loop loading by using nextLoadPosition set for backtracking and skipping consecutive GAP tags
if (frag && this.isLoopLoading(frag, targetBufferTime)) {
atGap = !!frag.gap;
frag = this.getNextFragmentLoopLoading(frag, trackDetails, bufferInfo, PlaylistLevelType.MAIN, maxBufLen);
}
if (!frag) {
this.bufferFlushed = true;
return;
}
// Buffer audio up to one target duration ahead of main buffer
const atBufferSyncLimit = mainBufferInfo && frag.start > mainBufferInfo.end + trackDetails.targetduration;
if (atBufferSyncLimit ||
// Or wait for main buffer after buffing some audio
!(mainBufferInfo != null && mainBufferInfo.len) && bufferInfo.len) {
// Check fragment-tracker for main fragments since GAP segments do not show up in bufferInfo
const mainFrag = this.getAppendedFrag(frag.start, PlaylistLevelType.MAIN);
if (mainFrag === null) {
return;
}
// Bridge gaps in main buffer
atGap || (atGap = !!mainFrag.gap || !!atBufferSyncLimit && mainBufferInfo.len === 0);
if (atBufferSyncLimit && !atGap || atGap && bufferInfo.nextStart && bufferInfo.nextStart < mainFrag.end) {
return;
}
}
this.loadFragment(frag, levelInfo, targetBufferTime);
}
getMaxBufferLength(mainBufferLength) {
const maxConfigBuffer = super.getMaxBufferLength();
if (!mainBufferLength) {
return maxConfigBuffer;
}
return Math.min(Math.max(maxConfigBuffer, mainBufferLength), this.config.maxMaxBufferLength);
}
onMediaDetaching() {
this.videoBuffer = null;
super.onMediaDetaching();
}
onAudioTracksUpdated(event, {
audioTracks
}) {
this.resetTransmuxer();
this.levels = audioTracks.map(mediaPlaylist => new Level(mediaPlaylist));
}
onAudioTrackSwitching(event, data) {
// if any URL found on new audio track, it is an alternate audio track
const altAudio = !!data.url;
this.trackId = data.id;
const {
fragCurrent
} = this;
if (fragCurrent) {
fragCurrent.abortRequests();
this.removeUnbufferedFrags(fragCurrent.start);
}
this.resetLoadingState();
// destroy useless transmuxer when switching audio to main
if (!altAudio) {
this.resetTransmuxer();
} else {
// switching to audio track, start timer if not already started
this.setInterval(TICK_INTERVAL$1);
}
// should we switch tracks ?
if (altAudio) {
this.switchingTrack = data;
// main audio track are handled by stream-controller, just do something if switching to alt audio track
this.state = State.IDLE;
} else {
this.switchingTrack = null;
this.bufferedTrack = data;
this.state = State.STOPPED;
}
this.tick();
}
onManifestLoading() {
this.fragmentTracker.removeAllFragments();
this.startPosition = this.lastCurrentTime = 0;
this.bufferFlushed = false;
this.levels = this.mainDetails = this.waitingData = this.bufferedTrack = this.cachedTrackLoadedData = this.switchingTrack = null;
this.startFragRequested = false;
this.trackId = this.videoTrackCC = this.waitingVideoCC = -1;
}
onLevelLoaded(event, data) {
this.mainDetails = data.details;
if (this.cachedTrackLoadedData !== null) {
this.hls.trigger(Events.AUDIO_TRACK_LOADED, this.cachedTrackLoadedData);
this.cachedTrackLoadedData = null;
}
}
onAudioTrackLoaded(event, data) {
var _track$details;
if (this.mainDetails == null) {
this.cachedTrackLoadedData = data;
return;
}
const {
levels
} = this;
const {
details: newDetails,
id: trackId
} = data;
if (!levels) {
this.warn(`Audio tracks were reset while loading level ${trackId}`);
return;
}
this.log(`Track ${trackId} loaded [${newDetails.startSN},${newDetails.endSN}]${newDetails.lastPartSn ? `[part-${newDetails.lastPartSn}-${newDetails.lastPartIndex}]` : ''},duration:${newDetails.totalduration}`);
const track = levels[trackId];
let sliding = 0;
if (newDetails.live || (_track$details = track.details) != null && _track$details.live) {
this.checkLiveUpdate(newDetails);
const mainDetails = this.mainDetails;
if (newDetails.deltaUpdateFailed || !mainDetails) {
return;
}
if (!track.details && newDetails.hasProgramDateTime && mainDetails.hasProgramDateTime) {
// Make sure our audio rendition is aligned with the "main" rendition, using
// pdt as our reference times.
alignMediaPlaylistByPDT(newDetails, mainDetails);
sliding = newDetails.fragments[0].start;
} else {
sliding = this.alignPlaylists(newDetails, track.details);
}
}
track.details = newDetails;
this.levelLastLoaded = trackId;
// compute start position if we are aligned with the main playlist
if (!this.startFragRequested && (this.mainDetails || !newDetails.live)) {
this.setStartPosition(track.details, sliding);
}
// only switch back to IDLE state if we were waiting for track to start downloading a new fragment
if (this.state === State.WAITING_TRACK && !this.waitForCdnTuneIn(newDetails)) {
this.state = State.IDLE;
}
// trigger handler right now
this.tick();
}
_handleFragmentLoadProgress(data) {
var _frag$initSegment;
const {
frag,
part,
payload
} = data;
const {
config,
trackId,
levels
} = this;
if (!levels) {
this.warn(`Audio tracks were reset while fragment load was in progress. Fragment ${frag.sn} of level ${frag.level} will not be buffered`);
return;
}
const track = levels[trackId];
if (!track) {
this.warn('Audio track is undefined on fragment load progress');
return;
}
const details = track.details;
if (!details) {
this.warn('Audio track details undefined on fragment load progress');
this.removeUnbufferedFrags(frag.start);
return;
}
const audioCodec = config.defaultAudioCodec || track.audioCodec || 'mp4a.40.2';
let transmuxer = this.transmuxer;
if (!transmuxer) {
transmuxer = this.transmuxer = new TransmuxerInterface(this.hls, PlaylistLevelType.AUDIO, this._handleTransmuxComplete.bind(this), this._handleTransmuxerFlush.bind(this));
}
// Check if we have video initPTS
// If not we need to wait for it
const initPTS = this.initPTS[frag.cc];
const initSegmentData = (_frag$initSegment = frag.initSegment) == null ? void 0 : _frag$initSegment.data;
if (initPTS !== undefined) {
// this.log(`Transmuxing ${sn} of [${details.startSN} ,${details.endSN}],track ${trackId}`);
// time Offset is accurate if level PTS is known, or if playlist is not sliding (not live)
const accurateTimeOffset = false; // details.PTSKnown || !details.live;
const partIndex = part ? part.index : -1;
const partial = partIndex !== -1;
const chunkMeta = new ChunkMetadata(frag.level, frag.sn, frag.stats.chunkCount, payload.byteLength, partIndex, partial);
transmuxer.push(payload, initSegmentData, audioCodec, '', frag, part, details.totalduration, accurateTimeOffset, chunkMeta, initPTS);
} else {
this.log(`Unknown video PTS for cc ${frag.cc}, waiting for video PTS before demuxing audio frag ${frag.sn} of [${details.startSN} ,${details.endSN}],track ${trackId}`);
const {
cache
} = this.waitingData = this.waitingData || {
frag,
part,
cache: new ChunkCache(),
complete: false
};
cache.push(new Uint8Array(payload));
this.waitingVideoCC = this.videoTrackCC;
this.state = State.WAITING_INIT_PTS;
}
}
_handleFragmentLoadComplete(fragLoadedData) {
if (this.waitingData) {
this.waitingData.complete = true;
return;
}
super._handleFragmentLoadComplete(fragLoadedData);
}
onBufferReset( /* event: Events.BUFFER_RESET */
) {
// reset reference to sourcebuffers
this.mediaBuffer = this.videoBuffer = null;
this.loadedmetadata = false;
}
onBufferCreated(event, data) {
const audioTrack = data.tracks.audio;
if (audioTrack) {
this.mediaBuffer = audioTrack.buffer || null;
}
if (data.tracks.video) {
this.videoBuffer = data.tracks.video.buffer || null;
}
}
onFragBuffered(event, data) {
const {
frag,
part
} = data;
if (frag.type !== PlaylistLevelType.AUDIO) {
if (!this.loadedmetadata && frag.type === PlaylistLevelType.MAIN) {
const bufferable = this.videoBuffer || this.media;
if (bufferable) {
const bufferedTimeRanges = BufferHelper.getBuffered(bufferable);
if (bufferedTimeRanges.length) {
this.loadedmetadata = true;
}
}
}
return;
}
if (this.fragContextChanged(frag)) {
// If a level switch was requested while a fragment was buffering, it will emit the FRAG_BUFFERED event upon completion
// Avoid setting state back to IDLE or concluding the audio switch; otherwise, the switched-to track will not buffer
this.warn(`Fragment ${frag.sn}${part ? ' p: ' + part.index : ''} of level ${frag.level} finished buffering, but was aborted. state: ${this.state}, audioSwitch: ${this.switchingTrack ? this.switchingTrack.name : 'false'}`);
return;
}
if (frag.sn !== 'initSegment') {
this.fragPrevious = frag;
const track = this.switchingTrack;
if (track) {
this.bufferedTrack = track;
this.switchingTrack = null;
this.hls.trigger(Events.AUDIO_TRACK_SWITCHED, _objectSpread2({}, track));
}
}
this.fragBufferedComplete(frag, part);
}
onError(event, data) {
var _data$context;
if (data.fatal) {
this.state = State.ERROR;
return;
}
switch (data.details) {
case ErrorDetails.FRAG_GAP:
case ErrorDetails.FRAG_PARSING_ERROR:
case ErrorDetails.FRAG_DECRYPT_ERROR:
case ErrorDetails.FRAG_LOAD_ERROR:
case ErrorDetails.FRAG_LOAD_TIMEOUT:
case ErrorDetails.KEY_LOAD_ERROR:
case ErrorDetails.KEY_LOAD_TIMEOUT:
this.onFragmentOrKeyLoadError(PlaylistLevelType.AUDIO, data);
break;
case ErrorDetails.AUDIO_TRACK_LOAD_ERROR:
case ErrorDetails.AUDIO_TRACK_LOAD_TIMEOUT:
case ErrorDetails.LEVEL_PARSING_ERROR:
// in case of non fatal error while loading track, if not retrying to load track, switch back to IDLE
if (!data.levelRetry && this.state === State.WAITING_TRACK && ((_data$context = data.context) == null ? void 0 : _data$context.type) === PlaylistContextType.AUDIO_TRACK) {
this.state = State.IDLE;
}
break;
case ErrorDetails.BUFFER_FULL_ERROR:
if (!data.parent || data.parent !== 'audio') {
return;
}
if (this.reduceLengthAndFlushBuffer(data)) {
this.bufferedTrack = null;
super.flushMainBuffer(0, Number.POSITIVE_INFINITY, 'audio');
}
break;
case ErrorDetails.INTERNAL_EXCEPTION:
this.recoverWorkerError(data);
break;
}
}
onBufferFlushed(event, {
type
}) {
if (type === ElementaryStreamTypes.AUDIO) {
this.bufferFlushed = true;
if (this.state === State.ENDED) {
this.state = State.IDLE;
}
}
}
_handleTransmuxComplete(transmuxResult) {
var _id3$samples;
const id = 'audio';
const {
hls
} = this;
const {
remuxResult,
chunkMeta
} = transmuxResult;
const context = this.getCurrentContext(chunkMeta);
if (!context) {
this.resetWhenMissingContext(chunkMeta);
return;
}
const {
frag,
part,
level
} = context;
const {
details
} = level;
const {
audio,
text,
id3,
initSegment
} = remuxResult;
// Check if the current fragment has been aborted. We check this by first seeing if we're still playing the current level.
// If we are, subsequently check if the currently loading fragment (fragCurrent) has changed.
if (this.fragContextChanged(frag) || !details) {
this.fragmentTracker.removeFragment(frag);
return;
}
this.state = State.PARSING;
if (this.switchingTrack && audio) {
this.completeAudioSwitch(this.switchingTrack);
}
if (initSegment != null && initSegment.tracks) {
const mapFragment = frag.initSegment || frag;
this._bufferInitSegment(initSegment.tracks, mapFragment, chunkMeta);
hls.trigger(Events.FRAG_PARSING_INIT_SEGMENT, {
frag: mapFragment,
id,
tracks: initSegment.tracks
});
// Only flush audio from old audio tracks when PTS is known on new audio track
}
if (audio) {
const {
startPTS,
endPTS,
startDTS,
endDTS
} = audio;
if (part) {
part.elementaryStreams[ElementaryStreamTypes.AUDIO] = {
startPTS,
endPTS,
startDTS,
endDTS
};
}
frag.setElementaryStreamInfo(ElementaryStreamTypes.AUDIO, startPTS, endPTS, startDTS, endDTS);
this.bufferFragmentData(audio, frag, part, chunkMeta);
}
if (id3 != null && (_id3$samples = id3.samples) != null && _id3$samples.length) {
const emittedID3 = _extends({
id,
frag,
details
}, id3);
hls.trigger(Events.FRAG_PARSING_METADATA, emittedID3);
}
if (text) {
const emittedText = _extends({
id,
frag,
details
}, text);
hls.trigger(Events.FRAG_PARSING_USERDATA, emittedText);
}
}
_bufferInitSegment(tracks, frag, chunkMeta) {
if (this.state !== State.PARSING) {
return;
}
// delete any video track found on audio transmuxer
if (tracks.video) {
delete tracks.video;
}
// include levelCodec in audio and video tracks
const track = tracks.audio;
if (!track) {
return;
}
track.levelCodec = track.codec;
track.id = 'audio';
this.log(`Init audio buffer, container:${track.container}, codecs[parsed]=[${track.codec}]`);
this.hls.trigger(Events.BUFFER_CODECS, tracks);
const initSegment = track.initSegment;
if (initSegment != null && initSegment.byteLength) {
const segment = {
type: 'audio',
frag,
part: null,
chunkMeta,
parent: frag.type,
data: initSegment
};
this.hls.trigger(Events.BUFFER_APPENDING, segment);
}
// trigger handler right now
this.tick();
}
loadFragment(frag, track, targetBufferTime) {
// only load if fragment is not loaded or if in audio switch
const fragState = this.fragmentTracker.getState(frag);
this.fragCurrent = frag;
// we force a frag loading in audio switch as fragment tracker might not have evicted previous frags in case of quick audio switch
if (this.switchingTrack || fragState === FragmentState.NOT_LOADED || fragState === FragmentState.PARTIAL) {
var _track$details2;
if (frag.sn === 'initSegment') {
this._loadInitSegment(frag, track);
} else if ((_track$details2 = track.details) != null && _track$details2.live && !this.initPTS[frag.cc]) {
this.log(`Waiting for video PTS in continuity counter ${frag.cc} of live stream before loading audio fragment ${frag.sn} of level ${this.trackId}`);
this.state = State.WAITING_INIT_PTS;
} else {
this.startFragRequested = true;
super.loadFragment(frag, track, targetBufferTime);
}
} else {
this.clearTrackerIfNeeded(frag);
}
}
completeAudioSwitch(switchingTrack) {
const {
hls,
media,
bufferedTrack
} = this;
const bufferedAttributes = bufferedTrack == null ? void 0 : bufferedTrack.attrs;
const switchAttributes = switchingTrack.attrs;
if (media && bufferedAttributes && (bufferedAttributes.CHANNELS !== switchAttributes.CHANNELS || bufferedAttributes.NAME !== switchAttributes.NAME || bufferedAttributes.LANGUAGE !== switchAttributes.LANGUAGE)) {
this.log('Switching audio track : flushing all audio');
super.flushMainBuffer(0, Number.POSITIVE_INFINITY, 'audio');
}
this.bufferedTrack = switchingTrack;
this.switchingTrack = null;
hls.trigger(Events.AUDIO_TRACK_SWITCHED, _objectSpread2({}, switchingTrack));
}
}
class AudioTrackController extends BasePlaylistController {
constructor(hls) {
super(hls, '[audio-track-controller]');
this.tracks = [];
this.groupId = null;
this.tracksInGroup = [];
this.trackId = -1;
this.currentTrack = null;
this.selectDefaultTrack = true;
this.registerListeners();
}
registerListeners() {
const {
hls
} = this;
hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.on(Events.MANIFEST_PARSED, this.onManifestParsed, this);
hls.on(Events.LEVEL_LOADING, this.onLevelLoading, this);
hls.on(Events.LEVEL_SWITCHING, this.onLevelSwitching, this);
hls.on(Events.AUDIO_TRACK_LOADED, this.onAudioTrackLoaded, this);
hls.on(Events.ERROR, this.onError, this);
}
unregisterListeners() {
const {
hls
} = this;
hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.off(Events.MANIFEST_PARSED, this.onManifestParsed, this);
hls.off(Events.LEVEL_LOADING, this.onLevelLoading, this);
hls.off(Events.LEVEL_SWITCHING, this.onLevelSwitching, this);
hls.off(Events.AUDIO_TRACK_LOADED, this.onAudioTrackLoaded, this);
hls.off(Events.ERROR, this.onError, this);
}
destroy() {
this.unregisterListeners();
this.tracks.length = 0;
this.tracksInGroup.length = 0;
this.currentTrack = null;
super.destroy();
}
onManifestLoading() {
this.tracks = [];
this.groupId = null;
this.tracksInGroup = [];
this.trackId = -1;
this.currentTrack = null;
this.selectDefaultTrack = true;
}
onManifestParsed(event, data) {
this.tracks = data.audioTracks || [];
}
onAudioTrackLoaded(event, data) {
const {
id,
groupId,
details
} = data;
const trackInActiveGroup = this.tracksInGroup[id];
if (!trackInActiveGroup || trackInActiveGroup.groupId !== groupId) {
this.warn(`Track with id:${id} and group:${groupId} not found in active group ${trackInActiveGroup.groupId}`);
return;
}
const curDetails = trackInActiveGroup.details;
trackInActiveGroup.details = data.details;
this.log(`audio-track ${id} "${trackInActiveGroup.name}" lang:${trackInActiveGroup.lang} group:${groupId} loaded [${details.startSN}-${details.endSN}]`);
if (id === this.trackId) {
this.playlistLoaded(id, data, curDetails);
}
}
onLevelLoading(event, data) {
this.switchLevel(data.level);
}
onLevelSwitching(event, data) {
this.switchLevel(data.level);
}
switchLevel(levelIndex) {
const levelInfo = this.hls.levels[levelIndex];
if (!(levelInfo != null && levelInfo.audioGroupIds)) {
return;
}
const audioGroupId = levelInfo.audioGroupIds[levelInfo.urlId];
if (this.groupId !== audioGroupId) {
this.groupId = audioGroupId || null;
const audioTracks = this.tracks.filter(track => !audioGroupId || track.groupId === audioGroupId);
// Disable selectDefaultTrack if there are no default tracks
if (this.selectDefaultTrack && !audioTracks.some(track => track.default)) {
this.selectDefaultTrack = false;
}
this.tracksInGroup = audioTracks;
const audioTracksUpdated = {
audioTracks
};
this.log(`Updating audio tracks, ${audioTracks.length} track(s) found in group:${audioGroupId}`);
this.hls.trigger(Events.AUDIO_TRACKS_UPDATED, audioTracksUpdated);
this.selectInitialTrack();
} else if (this.shouldReloadPlaylist(this.currentTrack)) {
// Retry playlist loading if no playlist is or has been loaded yet
this.setAudioTrack(this.trackId);
}
}
onError(event, data) {
if (data.fatal || !data.context) {
return;
}
if (data.context.type === PlaylistContextType.AUDIO_TRACK && data.context.id === this.trackId && data.context.groupId === this.groupId) {
this.requestScheduled = -1;
this.checkRetry(data);
}
}
get audioTracks() {
return this.tracksInGroup;
}
get audioTrack() {
return this.trackId;
}
set audioTrack(newId) {
// If audio track is selected from API then don't choose from the manifest default track
this.selectDefaultTrack = false;
this.setAudioTrack(newId);
}
setAudioTrack(newId) {
const tracks = this.tracksInGroup;
// check if level idx is valid
if (newId < 0 || newId >= tracks.length) {
this.warn('Invalid id passed to audio-track controller');
return;
}
// stopping live reloading timer if any
this.clearTimer();
const lastTrack = this.currentTrack;
tracks[this.trackId];
const track = tracks[newId];
const {
groupId,
name
} = track;
this.log(`Switching to audio-track ${newId} "${name}" lang:${track.lang} group:${groupId}`);
this.trackId = newId;
this.currentTrack = track;
this.selectDefaultTrack = false;
this.hls.trigger(Events.AUDIO_TRACK_SWITCHING, _objectSpread2({}, track));
// Do not reload track unless live
if (track.details && !track.details.live) {
return;
}
const hlsUrlParameters = this.switchParams(track.url, lastTrack == null ? void 0 : lastTrack.details);
this.loadPlaylist(hlsUrlParameters);
}
selectInitialTrack() {
const audioTracks = this.tracksInGroup;
const trackId = this.findTrackId(this.currentTrack) | this.findTrackId(null);
if (trackId !== -1) {
this.setAudioTrack(trackId);
} else {
const error = new Error(`No track found for running audio group-ID: ${this.groupId} track count: ${audioTracks.length}`);
this.warn(error.message);
this.hls.trigger(Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.AUDIO_TRACK_LOAD_ERROR,
fatal: true,
error
});
}
}
findTrackId(currentTrack) {
const audioTracks = this.tracksInGroup;
for (let i = 0; i < audioTracks.length; i++) {
const track = audioTracks[i];
if (!this.selectDefaultTrack || track.default) {
if (!currentTrack || currentTrack.attrs['STABLE-RENDITION-ID'] !== undefined && currentTrack.attrs['STABLE-RENDITION-ID'] === track.attrs['STABLE-RENDITION-ID']) {
return track.id;
}
if (currentTrack.name === track.name && currentTrack.lang === track.lang) {
return track.id;
}
}
}
return -1;
}
loadPlaylist(hlsUrlParameters) {
super.loadPlaylist();
const audioTrack = this.tracksInGroup[this.trackId];
if (this.shouldLoadPlaylist(audioTrack)) {
const id = audioTrack.id;
const groupId = audioTrack.groupId;
let url = audioTrack.url;
if (hlsUrlParameters) {
try {
url = hlsUrlParameters.addDirectives(url);
} catch (error) {
this.warn(`Could not construct new URL with HLS Delivery Directives: ${error}`);
}
}
// track not retrieved yet, or live playlist we need to (re)load it
this.log(`loading audio-track playlist ${id} "${audioTrack.name}" lang:${audioTrack.lang} group:${groupId}`);
this.clearTimer();
this.hls.trigger(Events.AUDIO_TRACK_LOADING, {
url,
id,
groupId,
deliveryDirectives: hlsUrlParameters || null
});
}
}
}
function subtitleOptionsIdentical(trackList1, trackList2) {
if (trackList1.length !== trackList2.length) {
return false;
}
for (let i = 0; i < trackList1.length; i++) {
if (!subtitleAttributesIdentical(trackList1[i].attrs, trackList2[i].attrs)) {
return false;
}
}
return true;
}
function subtitleAttributesIdentical(attrs1, attrs2) {
// Media options with the same rendition ID must be bit identical
const stableRenditionId = attrs1['STABLE-RENDITION-ID'];
if (stableRenditionId) {
return stableRenditionId === attrs2['STABLE-RENDITION-ID'];
}
// When rendition ID is not present, compare attributes
return !['LANGUAGE', 'NAME', 'CHARACTERISTICS', 'AUTOSELECT', 'DEFAULT', 'FORCED'].some(subtitleAttribute => attrs1[subtitleAttribute] !== attrs2[subtitleAttribute]);
}
const TICK_INTERVAL = 500; // how often to tick in ms
class SubtitleStreamController extends BaseStreamController {
constructor(hls, fragmentTracker, keyLoader) {
super(hls, fragmentTracker, keyLoader, '[subtitle-stream-controller]', PlaylistLevelType.SUBTITLE);
this.levels = [];
this.currentTrackId = -1;
this.tracksBuffered = [];
this.mainDetails = null;
this._registerListeners();
}
onHandlerDestroying() {
this._unregisterListeners();
this.mainDetails = null;
}
_registerListeners() {
const {
hls
} = this;
hls.on(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
hls.on(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.on(Events.LEVEL_LOADED, this.onLevelLoaded, this);
hls.on(Events.ERROR, this.onError, this);
hls.on(Events.SUBTITLE_TRACKS_UPDATED, this.onSubtitleTracksUpdated, this);
hls.on(Events.SUBTITLE_TRACK_SWITCH, this.onSubtitleTrackSwitch, this);
hls.on(Events.SUBTITLE_TRACK_LOADED, this.onSubtitleTrackLoaded, this);
hls.on(Events.SUBTITLE_FRAG_PROCESSED, this.onSubtitleFragProcessed, this);
hls.on(Events.BUFFER_FLUSHING, this.onBufferFlushing, this);
hls.on(Events.FRAG_BUFFERED, this.onFragBuffered, this);
}
_unregisterListeners() {
const {
hls
} = this;
hls.off(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
hls.off(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.off(Events.LEVEL_LOADED, this.onLevelLoaded, this);
hls.off(Events.ERROR, this.onError, this);
hls.off(Events.SUBTITLE_TRACKS_UPDATED, this.onSubtitleTracksUpdated, this);
hls.off(Events.SUBTITLE_TRACK_SWITCH, this.onSubtitleTrackSwitch, this);
hls.off(Events.SUBTITLE_TRACK_LOADED, this.onSubtitleTrackLoaded, this);
hls.off(Events.SUBTITLE_FRAG_PROCESSED, this.onSubtitleFragProcessed, this);
hls.off(Events.BUFFER_FLUSHING, this.onBufferFlushing, this);
hls.off(Events.FRAG_BUFFERED, this.onFragBuffered, this);
}
startLoad(startPosition) {
this.stopLoad();
this.state = State.IDLE;
this.setInterval(TICK_INTERVAL);
this.nextLoadPosition = this.startPosition = this.lastCurrentTime = startPosition;
this.tick();
}
onManifestLoading() {
this.mainDetails = null;
this.fragmentTracker.removeAllFragments();
}
onMediaDetaching() {
this.tracksBuffered = [];
super.onMediaDetaching();
}
onLevelLoaded(event, data) {
this.mainDetails = data.details;
}
onSubtitleFragProcessed(event, data) {
const {
frag,
success
} = data;
this.fragPrevious = frag;
this.state = State.IDLE;
if (!success) {
return;
}
const buffered = this.tracksBuffered[this.currentTrackId];
if (!buffered) {
return;
}
// Create/update a buffered array matching the interface used by BufferHelper.bufferedInfo
// so we can re-use the logic used to detect how much has been buffered
let timeRange;
const fragStart = frag.start;
for (let i = 0; i < buffered.length; i++) {
if (fragStart >= buffered[i].start && fragStart <= buffered[i].end) {
timeRange = buffered[i];
break;
}
}
const fragEnd = frag.start + frag.duration;
if (timeRange) {
timeRange.end = fragEnd;
} else {
timeRange = {
start: fragStart,
end: fragEnd
};
buffered.push(timeRange);
}
this.fragmentTracker.fragBuffered(frag);
}
onBufferFlushing(event, data) {
const {
startOffset,
endOffset
} = data;
if (startOffset === 0 && endOffset !== Number.POSITIVE_INFINITY) {
const endOffsetSubtitles = endOffset - 1;
if (endOffsetSubtitles <= 0) {
return;
}
data.endOffsetSubtitles = Math.max(0, endOffsetSubtitles);
this.tracksBuffered.forEach(buffered => {
for (let i = 0; i < buffered.length;) {
if (buffered[i].end <= endOffsetSubtitles) {
buffered.shift();
continue;
} else if (buffered[i].start < endOffsetSubtitles) {
buffered[i].start = endOffsetSubtitles;
} else {
break;
}
i++;
}
});
this.fragmentTracker.removeFragmentsInRange(startOffset, endOffsetSubtitles, PlaylistLevelType.SUBTITLE);
}
}
onFragBuffered(event, data) {
if (!this.loadedmetadata && data.frag.type === PlaylistLevelType.MAIN) {
var _this$media;
if ((_this$media = this.media) != null && _this$media.buffered.length) {
this.loadedmetadata = true;
}
}
}
// If something goes wrong, proceed to next frag, if we were processing one.
onError(event, data) {
const frag = data.frag;
if ((frag == null ? void 0 : frag.type) === PlaylistLevelType.SUBTITLE) {
if (this.fragCurrent) {
this.fragCurrent.abortRequests();
}
if (this.state !== State.STOPPED) {
this.state = State.IDLE;
}
}
}
// Got all new subtitle levels.
onSubtitleTracksUpdated(event, {
subtitleTracks
}) {
if (subtitleOptionsIdentical(this.levels, subtitleTracks)) {
this.levels = subtitleTracks.map(mediaPlaylist => new Level(mediaPlaylist));
return;
}
this.tracksBuffered = [];
this.levels = subtitleTracks.map(mediaPlaylist => {
const level = new Level(mediaPlaylist);
this.tracksBuffered[level.id] = [];
return level;
});
this.fragmentTracker.removeFragmentsInRange(0, Number.POSITIVE_INFINITY, PlaylistLevelType.SUBTITLE);
this.fragPrevious = null;
this.mediaBuffer = null;
}
onSubtitleTrackSwitch(event, data) {
this.currentTrackId = data.id;
if (!this.levels.length || this.currentTrackId === -1) {
this.clearInterval();
return;
}
// Check if track has the necessary details to load fragments
const currentTrack = this.levels[this.currentTrackId];
if (currentTrack != null && currentTrack.details) {
this.mediaBuffer = this.mediaBufferTimeRanges;
} else {
this.mediaBuffer = null;
}
if (currentTrack) {
this.setInterval(TICK_INTERVAL);
}
}
// Got a new set of subtitle fragments.
onSubtitleTrackLoaded(event, data) {
var _track$details;
const {
details: newDetails,
id: trackId
} = data;
const {
currentTrackId,
levels
} = this;
if (!levels.length) {
return;
}
const track = levels[currentTrackId];
if (trackId >= levels.length || trackId !== currentTrackId || !track) {
return;
}
this.mediaBuffer = this.mediaBufferTimeRanges;
let sliding = 0;
if (newDetails.live || (_track$details = track.details) != null && _track$details.live) {
const mainDetails = this.mainDetails;
if (newDetails.deltaUpdateFailed || !mainDetails) {
return;
}
const mainSlidingStartFragment = mainDetails.fragments[0];
if (!track.details) {
if (newDetails.hasProgramDateTime && mainDetails.hasProgramDateTime) {
alignMediaPlaylistByPDT(newDetails, mainDetails);
sliding = newDetails.fragments[0].start;
} else if (mainSlidingStartFragment) {
// line up live playlist with main so that fragments in range are loaded
sliding = mainSlidingStartFragment.start;
addSliding(newDetails, sliding);
}
} else {
sliding = this.alignPlaylists(newDetails, track.details);
if (sliding === 0 && mainSlidingStartFragment) {
// realign with main when there is no overlap with last refresh
sliding = mainSlidingStartFragment.start;
addSliding(newDetails, sliding);
}
}
}
track.details = newDetails;
this.levelLastLoaded = trackId;
if (!this.startFragRequested && (this.mainDetails || !newDetails.live)) {
this.setStartPosition(track.details, sliding);
}
// trigger handler right now
this.tick();
// If playlist is misaligned because of bad PDT or drift, delete details to resync with main on reload
if (newDetails.live && !this.fragCurrent && this.media && this.state === State.IDLE) {
const foundFrag = findFragmentByPTS(null, newDetails.fragments, this.media.currentTime, 0);
if (!foundFrag) {
this.warn('Subtitle playlist not aligned with playback');
track.details = undefined;
}
}
}
_handleFragmentLoadComplete(fragLoadedData) {
const {
frag,
payload
} = fragLoadedData;
const decryptData = frag.decryptdata;
const hls = this.hls;
if (this.fragContextChanged(frag)) {
return;
}
// check to see if the payload needs to be decrypted
if (payload && payload.byteLength > 0 && decryptData && decryptData.key && decryptData.iv && decryptData.method === 'AES-128') {
const startTime = performance.now();
// decrypt the subtitles
this.decrypter.decrypt(new Uint8Array(payload), decryptData.key.buffer, decryptData.iv.buffer).catch(err => {
hls.trigger(Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.FRAG_DECRYPT_ERROR,
fatal: false,
error: err,
reason: err.message,
frag
});
throw err;
}).then(decryptedData => {
const endTime = performance.now();
hls.trigger(Events.FRAG_DECRYPTED, {
frag,
payload: decryptedData,
stats: {
tstart: startTime,
tdecrypt: endTime
}
});
}).catch(err => {
this.warn(`${err.name}: ${err.message}`);
this.state = State.IDLE;
});
}
}
doTick() {
if (!this.media) {
this.state = State.IDLE;
return;
}
if (this.state === State.IDLE) {
const {
currentTrackId,
levels
} = this;
const track = levels[currentTrackId];
if (!levels.length || !track || !track.details) {
return;
}
const {
config
} = this;
const currentTime = this.getLoadPosition();
const bufferedInfo = BufferHelper.bufferedInfo(this.tracksBuffered[this.currentTrackId] || [], currentTime, config.maxBufferHole);
const {
end: targetBufferTime,
len: bufferLen
} = bufferedInfo;
const mainBufferInfo = this.getFwdBufferInfo(this.media, PlaylistLevelType.MAIN);
const trackDetails = track.details;
const maxBufLen = this.getMaxBufferLength(mainBufferInfo == null ? void 0 : mainBufferInfo.len) + trackDetails.levelTargetDuration;
if (bufferLen > maxBufLen) {
return;
}
const fragments = trackDetails.fragments;
const fragLen = fragments.length;
const end = trackDetails.edge;
let foundFrag = null;
const fragPrevious = this.fragPrevious;
if (targetBufferTime < end) {
const tolerance = config.maxFragLookUpTolerance;
const lookupTolerance = targetBufferTime > end - tolerance ? 0 : tolerance;
foundFrag = findFragmentByPTS(fragPrevious, fragments, Math.max(fragments[0].start, targetBufferTime), lookupTolerance);
if (!foundFrag && fragPrevious && fragPrevious.start < fragments[0].start) {
foundFrag = fragments[0];
}
} else {
foundFrag = fragments[fragLen - 1];
}
if (!foundFrag) {
return;
}
foundFrag = this.mapToInitFragWhenRequired(foundFrag);
if (foundFrag.sn !== 'initSegment') {
// Load earlier fragment in same discontinuity to make up for misaligned playlists and cues that extend beyond end of segment
const curSNIdx = foundFrag.sn - trackDetails.startSN;
const prevFrag = fragments[curSNIdx - 1];
if (prevFrag && prevFrag.cc === foundFrag.cc && this.fragmentTracker.getState(prevFrag) === FragmentState.NOT_LOADED) {
foundFrag = prevFrag;
}
}
if (this.fragmentTracker.getState(foundFrag) === FragmentState.NOT_LOADED) {
// only load if fragment is not loaded
this.loadFragment(foundFrag, track, targetBufferTime);
}
}
}
getMaxBufferLength(mainBufferLength) {
const maxConfigBuffer = super.getMaxBufferLength();
if (!mainBufferLength) {
return maxConfigBuffer;
}
return Math.max(maxConfigBuffer, mainBufferLength);
}
loadFragment(frag, level, targetBufferTime) {
this.fragCurrent = frag;
if (frag.sn === 'initSegment') {
this._loadInitSegment(frag, level);
} else {
this.startFragRequested = true;
super.loadFragment(frag, level, targetBufferTime);
}
}
get mediaBufferTimeRanges() {
return new BufferableInstance(this.tracksBuffered[this.currentTrackId] || []);
}
}
class BufferableInstance {
constructor(timeranges) {
this.buffered = void 0;
const getRange = (name, index, length) => {
index = index >>> 0;
if (index > length - 1) {
throw new DOMException(`Failed to execute '${name}' on 'TimeRanges': The index provided (${index}) is greater than the maximum bound (${length})`);
}
return timeranges[index][name];
};
this.buffered = {
get length() {
return timeranges.length;
},
end(index) {
return getRange('end', index, timeranges.length);
},
start(index) {
return getRange('start', index, timeranges.length);
}
};
}
}
class SubtitleTrackController extends BasePlaylistController {
constructor(hls) {
super(hls, '[subtitle-track-controller]');
this.media = null;
this.tracks = [];
this.groupId = null;
this.tracksInGroup = [];
this.trackId = -1;
this.selectDefaultTrack = true;
this.queuedDefaultTrack = -1;
this.trackChangeListener = () => this.onTextTracksChanged();
this.asyncPollTrackChange = () => this.pollTrackChange(0);
this.useTextTrackPolling = false;
this.subtitlePollingInterval = -1;
this._subtitleDisplay = true;
this.registerListeners();
}
destroy() {
this.unregisterListeners();
this.tracks.length = 0;
this.tracksInGroup.length = 0;
this.trackChangeListener = this.asyncPollTrackChange = null;
super.destroy();
}
get subtitleDisplay() {
return this._subtitleDisplay;
}
set subtitleDisplay(value) {
this._subtitleDisplay = value;
if (this.trackId > -1) {
this.toggleTrackModes(this.trackId);
}
}
registerListeners() {
const {
hls
} = this;
hls.on(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
hls.on(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.on(Events.MANIFEST_PARSED, this.onManifestParsed, this);
hls.on(Events.LEVEL_LOADING, this.onLevelLoading, this);
hls.on(Events.LEVEL_SWITCHING, this.onLevelSwitching, this);
hls.on(Events.SUBTITLE_TRACK_LOADED, this.onSubtitleTrackLoaded, this);
hls.on(Events.ERROR, this.onError, this);
}
unregisterListeners() {
const {
hls
} = this;
hls.off(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
hls.off(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.off(Events.MANIFEST_PARSED, this.onManifestParsed, this);
hls.off(Events.LEVEL_LOADING, this.onLevelLoading, this);
hls.off(Events.LEVEL_SWITCHING, this.onLevelSwitching, this);
hls.off(Events.SUBTITLE_TRACK_LOADED, this.onSubtitleTrackLoaded, this);
hls.off(Events.ERROR, this.onError, this);
}
// Listen for subtitle track change, then extract the current track ID.
onMediaAttached(event, data) {
this.media = data.media;
if (!this.media) {
return;
}
if (this.queuedDefaultTrack > -1) {
this.subtitleTrack = this.queuedDefaultTrack;
this.queuedDefaultTrack = -1;
}
this.useTextTrackPolling = !(this.media.textTracks && 'onchange' in this.media.textTracks);
if (this.useTextTrackPolling) {
this.pollTrackChange(500);
} else {
this.media.textTracks.addEventListener('change', this.asyncPollTrackChange);
}
}
pollTrackChange(timeout) {
self.clearInterval(this.subtitlePollingInterval);
this.subtitlePollingInterval = self.setInterval(this.trackChangeListener, timeout);
}
onMediaDetaching() {
if (!this.media) {
return;
}
self.clearInterval(this.subtitlePollingInterval);
if (!this.useTextTrackPolling) {
this.media.textTracks.removeEventListener('change', this.asyncPollTrackChange);
}
if (this.trackId > -1) {
this.queuedDefaultTrack = this.trackId;
}
const textTracks = filterSubtitleTracks(this.media.textTracks);
// Clear loaded cues on media detachment from tracks
textTracks.forEach(track => {
clearCurrentCues(track);
});
// Disable all subtitle tracks before detachment so when reattached only tracks in that content are enabled.
this.subtitleTrack = -1;
this.media = null;
}
onManifestLoading() {
this.tracks = [];
this.groupId = null;
this.tracksInGroup = [];
this.trackId = -1;
this.selectDefaultTrack = true;
}
// Fired whenever a new manifest is loaded.
onManifestParsed(event, data) {
this.tracks = data.subtitleTracks;
}
onSubtitleTrackLoaded(event, data) {
const {
id,
details
} = data;
const {
trackId
} = this;
const currentTrack = this.tracksInGroup[trackId];
if (!currentTrack) {
this.warn(`Invalid subtitle track id ${id}`);
return;
}
const curDetails = currentTrack.details;
currentTrack.details = data.details;
this.log(`subtitle track ${id} loaded [${details.startSN}-${details.endSN}]`);
if (id === this.trackId) {
this.playlistLoaded(id, data, curDetails);
}
}
onLevelLoading(event, data) {
this.switchLevel(data.level);
}
onLevelSwitching(event, data) {
this.switchLevel(data.level);
}
switchLevel(levelIndex) {
const levelInfo = this.hls.levels[levelIndex];
if (!(levelInfo != null && levelInfo.textGroupIds)) {
return;
}
const textGroupId = levelInfo.textGroupIds[levelInfo.urlId];
const lastTrack = this.tracksInGroup ? this.tracksInGroup[this.trackId] : undefined;
if (this.groupId !== textGroupId) {
const subtitleTracks = this.tracks.filter(track => !textGroupId || track.groupId === textGroupId);
this.tracksInGroup = subtitleTracks;
const initialTrackId = this.findTrackId(lastTrack == null ? void 0 : lastTrack.name) || this.findTrackId();
this.groupId = textGroupId || null;
const subtitleTracksUpdated = {
subtitleTracks
};
this.log(`Updating subtitle tracks, ${subtitleTracks.length} track(s) found in "${textGroupId}" group-id`);
this.hls.trigger(Events.SUBTITLE_TRACKS_UPDATED, subtitleTracksUpdated);
if (initialTrackId !== -1) {
this.setSubtitleTrack(initialTrackId, lastTrack);
}
} else if (this.shouldReloadPlaylist(lastTrack)) {
// Retry playlist loading if no playlist is or has been loaded yet
this.setSubtitleTrack(this.trackId, lastTrack);
}
}
findTrackId(name) {
const textTracks = this.tracksInGroup;
for (let i = 0; i < textTracks.length; i++) {
const track = textTracks[i];
if (!this.selectDefaultTrack || track.default) {
if (!name || name === track.name) {
return track.id;
}
}
}
return -1;
}
onError(event, data) {
if (data.fatal || !data.context) {
return;
}
if (data.context.type === PlaylistContextType.SUBTITLE_TRACK && data.context.id === this.trackId && data.context.groupId === this.groupId) {
this.checkRetry(data);
}
}
/** get alternate subtitle tracks list from playlist **/
get subtitleTracks() {
return this.tracksInGroup;
}
/** get/set index of the selected subtitle track (based on index in subtitle track lists) **/
get subtitleTrack() {
return this.trackId;
}
set subtitleTrack(newId) {
this.selectDefaultTrack = false;
const lastTrack = this.tracksInGroup ? this.tracksInGroup[this.trackId] : undefined;
this.setSubtitleTrack(newId, lastTrack);
}
loadPlaylist(hlsUrlParameters) {
super.loadPlaylist();
const currentTrack = this.tracksInGroup[this.trackId];
if (this.shouldLoadPlaylist(currentTrack)) {
const id = currentTrack.id;
const groupId = currentTrack.groupId;
let url = currentTrack.url;
if (hlsUrlParameters) {
try {
url = hlsUrlParameters.addDirectives(url);
} catch (error) {
this.warn(`Could not construct new URL with HLS Delivery Directives: ${error}`);
}
}
this.log(`Loading subtitle playlist for id ${id}`);
this.hls.trigger(Events.SUBTITLE_TRACK_LOADING, {
url,
id,
groupId,
deliveryDirectives: hlsUrlParameters || null
});
}
}
/**
* Disables the old subtitleTrack and sets current mode on the next subtitleTrack.
* This operates on the DOM textTracks.
* A value of -1 will disable all subtitle tracks.
*/
toggleTrackModes(newId) {
const {
media,
trackId
} = this;
if (!media) {
return;
}
const textTracks = filterSubtitleTracks(media.textTracks);
const groupTracks = textTracks.filter(track => track.groupId === this.groupId);
if (newId === -1) {
[].slice.call(textTracks).forEach(track => {
track.mode = 'disabled';
});
} else {
const oldTrack = groupTracks[trackId];
if (oldTrack) {
oldTrack.mode = 'disabled';
}
}
const nextTrack = groupTracks[newId];
if (nextTrack) {
nextTrack.mode = this.subtitleDisplay ? 'showing' : 'hidden';
}
}
/**
* This method is responsible for validating the subtitle index and periodically reloading if live.
* Dispatches the SUBTITLE_TRACK_SWITCH event, which instructs the subtitle-stream-controller to load the selected track.
*/
setSubtitleTrack(newId, lastTrack) {
var _tracks$newId;
const tracks = this.tracksInGroup;
// setting this.subtitleTrack will trigger internal logic
// if media has not been attached yet, it will fail
// we keep a reference to the default track id
// and we'll set subtitleTrack when onMediaAttached is triggered
if (!this.media) {
this.queuedDefaultTrack = newId;
return;
}
if (this.trackId !== newId) {
this.toggleTrackModes(newId);
}
// exit if track id as already set or invalid
if (this.trackId === newId && (newId === -1 || (_tracks$newId = tracks[newId]) != null && _tracks$newId.details) || newId < -1 || newId >= tracks.length) {
return;
}
// stopping live reloading timer if any
this.clearTimer();
const track = tracks[newId];
this.log(`Switching to subtitle-track ${newId}` + (track ? ` "${track.name}" lang:${track.lang} group:${track.groupId}` : ''));
this.trackId = newId;
if (track) {
const {
id,
groupId = '',
name,
type,
url
} = track;
this.hls.trigger(Events.SUBTITLE_TRACK_SWITCH, {
id,
groupId,
name,
type,
url
});
const hlsUrlParameters = this.switchParams(track.url, lastTrack == null ? void 0 : lastTrack.details);
this.loadPlaylist(hlsUrlParameters);
} else {
// switch to -1
this.hls.trigger(Events.SUBTITLE_TRACK_SWITCH, {
id: newId
});
}
}
onTextTracksChanged() {
if (!this.useTextTrackPolling) {
self.clearInterval(this.subtitlePollingInterval);
}
// Media is undefined when switching streams via loadSource()
if (!this.media || !this.hls.config.renderTextTracksNatively) {
return;
}
let trackId = -1;
const tracks = filterSubtitleTracks(this.media.textTracks);
for (let id = 0; id < tracks.length; id++) {
if (tracks[id].mode === 'hidden') {
// Do not break in case there is a following track with showing.
trackId = id;
} else if (tracks[id].mode === 'showing') {
trackId = id;
break;
}
}
// Setting current subtitleTrack will invoke code.
if (this.subtitleTrack !== trackId) {
this.subtitleTrack = trackId;
}
}
}
function filterSubtitleTracks(textTrackList) {
const tracks = [];
for (let i = 0; i < textTrackList.length; i++) {
const track = textTrackList[i];
// Edge adds a track without a label; we don't want to use it
if ((track.kind === 'subtitles' || track.kind === 'captions') && track.label) {
tracks.push(textTrackList[i]);
}
}
return tracks;
}
class BufferOperationQueue {
constructor(sourceBufferReference) {
this.buffers = void 0;
this.queues = {
video: [],
audio: [],
audiovideo: []
};
this.buffers = sourceBufferReference;
}
append(operation, type) {
const queue = this.queues[type];
queue.push(operation);
if (queue.length === 1 && this.buffers[type]) {
this.executeNext(type);
}
}
insertAbort(operation, type) {
const queue = this.queues[type];
queue.unshift(operation);
this.executeNext(type);
}
appendBlocker(type) {
let execute;
const promise = new Promise(resolve => {
execute = resolve;
});
const operation = {
execute,
onStart: () => {},
onComplete: () => {},
onError: () => {}
};
this.append(operation, type);
return promise;
}
executeNext(type) {
const {
buffers,
queues
} = this;
const sb = buffers[type];
const queue = queues[type];
if (queue.length) {
const operation = queue[0];
try {
// Operations are expected to result in an 'updateend' event being fired. If not, the queue will lock. Operations
// which do not end with this event must call _onSBUpdateEnd manually
operation.execute();
} catch (e) {
logger.warn('[buffer-operation-queue]: Unhandled exception executing the current operation');
operation.onError(e);
// Only shift the current operation off, otherwise the updateend handler will do this for us
if (!(sb != null && sb.updating)) {
queue.shift();
this.executeNext(type);
}
}
}
}
shiftAndExecuteNext(type) {
this.queues[type].shift();
this.executeNext(type);
}
current(type) {
return this.queues[type][0];
}
}
const MediaSource = getMediaSource();
const VIDEO_CODEC_PROFILE_REPACE = /([ha]vc.)(?:\.[^.,]+)+/;
class BufferController {
// The level details used to determine duration, target-duration and live
// cache the self generated object url to detect hijack of video tag
// A queue of buffer operations which require the SourceBuffer to not be updating upon execution
// References to event listeners for each SourceBuffer, so that they can be referenced for event removal
// The number of BUFFER_CODEC events received before any sourceBuffers are created
// The total number of BUFFER_CODEC events received
// A reference to the attached media element
// A reference to the active media source
// Last MP3 audio chunk appended
// counters
constructor(hls) {
this.details = null;
this._objectUrl = null;
this.operationQueue = void 0;
this.listeners = void 0;
this.hls = void 0;
this.bufferCodecEventsExpected = 0;
this._bufferCodecEventsTotal = 0;
this.media = null;
this.mediaSource = null;
this.lastMpegAudioChunk = null;
this.appendError = 0;
this.tracks = {};
this.pendingTracks = {};
this.sourceBuffer = void 0;
// Keep as arrow functions so that we can directly reference these functions directly as event listeners
this._onMediaSourceOpen = () => {
const {
media,
mediaSource
} = this;
logger.log('[buffer-controller]: Media source opened');
if (media) {
media.removeEventListener('emptied', this._onMediaEmptied);
this.updateMediaElementDuration();
this.hls.trigger(Events.MEDIA_ATTACHED, {
media
});
}
if (mediaSource) {
// once received, don't listen anymore to sourceopen event
mediaSource.removeEventListener('sourceopen', this._onMediaSourceOpen);
}
this.checkPendingTracks();
};
this._onMediaSourceClose = () => {
logger.log('[buffer-controller]: Media source closed');
};
this._onMediaSourceEnded = () => {
logger.log('[buffer-controller]: Media source ended');
};
this._onMediaEmptied = () => {
const {
media,
_objectUrl
} = this;
if (media && media.src !== _objectUrl) {
logger.error(`Media element src was set while attaching MediaSource (${_objectUrl} > ${media.src})`);
}
};
this.hls = hls;
this._initSourceBuffer();
this.registerListeners();
}
hasSourceTypes() {
return this.getSourceBufferTypes().length > 0 || Object.keys(this.pendingTracks).length > 0;
}
destroy() {
this.unregisterListeners();
this.details = null;
this.lastMpegAudioChunk = null;
}
registerListeners() {
const {
hls
} = this;
hls.on(Events.MEDIA_ATTACHING, this.onMediaAttaching, this);
hls.on(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.on(Events.MANIFEST_PARSED, this.onManifestParsed, this);
hls.on(Events.BUFFER_RESET, this.onBufferReset, this);
hls.on(Events.BUFFER_APPENDING, this.onBufferAppending, this);
hls.on(Events.BUFFER_CODECS, this.onBufferCodecs, this);
hls.on(Events.BUFFER_EOS, this.onBufferEos, this);
hls.on(Events.BUFFER_FLUSHING, this.onBufferFlushing, this);
hls.on(Events.LEVEL_UPDATED, this.onLevelUpdated, this);
hls.on(Events.FRAG_PARSED, this.onFragParsed, this);
hls.on(Events.FRAG_CHANGED, this.onFragChanged, this);
}
unregisterListeners() {
const {
hls
} = this;
hls.off(Events.MEDIA_ATTACHING, this.onMediaAttaching, this);
hls.off(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
hls.off(Events.MANIFEST_PARSED, this.onManifestParsed, this);
hls.off(Events.BUFFER_RESET, this.onBufferReset, this);
hls.off(Events.BUFFER_APPENDING, this.onBufferAppending, this);
hls.off(Events.BUFFER_CODECS, this.onBufferCodecs, this);
hls.off(Events.BUFFER_EOS, this.onBufferEos, this);
hls.off(Events.BUFFER_FLUSHING, this.onBufferFlushing, this);
hls.off(Events.LEVEL_UPDATED, this.onLevelUpdated, this);
hls.off(Events.FRAG_PARSED, this.onFragParsed, this);
hls.off(Events.FRAG_CHANGED, this.onFragChanged, this);
}
_initSourceBuffer() {
this.sourceBuffer = {};
this.operationQueue = new BufferOperationQueue(this.sourceBuffer);
this.listeners = {
audio: [],
video: [],
audiovideo: []
};
this.lastMpegAudioChunk = null;
}
onManifestLoading() {
this.bufferCodecEventsExpected = this._bufferCodecEventsTotal = 0;
this.details = null;
}
onManifestParsed(event, data) {
// in case of alt audio 2 BUFFER_CODECS events will be triggered, one per stream controller
// sourcebuffers will be created all at once when the expected nb of tracks will be reached
// in case alt audio is not used, only one BUFFER_CODEC event will be fired from main stream controller
// it will contain the expected nb of source buffers, no need to compute it
let codecEvents = 2;
if (data.audio && !data.video || !data.altAudio || !true) {
codecEvents = 1;
}
this.bufferCodecEventsExpected = this._bufferCodecEventsTotal = codecEvents;
logger.log(`${this.bufferCodecEventsExpected} bufferCodec event(s) expected`);
}
onMediaAttaching(event, data) {
const media = this.media = data.media;
if (media && MediaSource) {
const ms = this.mediaSource = new MediaSource();
// MediaSource listeners are arrow functions with a lexical scope, and do not need to be bound
ms.addEventListener('sourceopen', this._onMediaSourceOpen);
ms.addEventListener('sourceended', this._onMediaSourceEnded);
ms.addEventListener('sourceclose', this._onMediaSourceClose);
// link video and media Source
media.src = self.URL.createObjectURL(ms);
// cache the locally generated object url
this._objectUrl = media.src;
media.addEventListener('emptied', this._onMediaEmptied);
}
}
onMediaDetaching() {
const {
media,
mediaSource,
_objectUrl
} = this;
if (mediaSource) {
logger.log('[buffer-controller]: media source detaching');
if (mediaSource.readyState === 'open') {
try {
// endOfStream could trigger exception if any sourcebuffer is in updating state
// we don't really care about checking sourcebuffer state here,
// as we are anyway detaching the MediaSource
// let's just avoid this exception to propagate
mediaSource.endOfStream();
} catch (err) {
logger.warn(`[buffer-controller]: onMediaDetaching: ${err.message} while calling endOfStream`);
}
}
// Clean up the SourceBuffers by invoking onBufferReset
this.onBufferReset();
mediaSource.removeEventListener('sourceopen', this._onMediaSourceOpen);
mediaSource.removeEventListener('sourceended', this._onMediaSourceEnded);
mediaSource.removeEventListener('sourceclose', this._onMediaSourceClose);
// Detach properly the MediaSource from the HTMLMediaElement as
// suggested in https://github.com/w3c/media-source/issues/53.
if (media) {
media.removeEventListener('emptied', this._onMediaEmptied);
if (_objectUrl) {
self.URL.revokeObjectURL(_objectUrl);
}
// clean up video tag src only if it's our own url. some external libraries might
// hijack the video tag and change its 'src' without destroying the Hls instance first
if (media.src === _objectUrl) {
media.removeAttribute('src');
media.load();
} else {
logger.warn('[buffer-controller]: media.src was changed by a third party - skip cleanup');
}
}
this.mediaSource = null;
this.media = null;
this._objectUrl = null;
this.bufferCodecEventsExpected = this._bufferCodecEventsTotal;
this.pendingTracks = {};
this.tracks = {};
}
this.hls.trigger(Events.MEDIA_DETACHED, undefined);
}
onBufferReset() {
this.getSourceBufferTypes().forEach(type => {
const sb = this.sourceBuffer[type];
try {
if (sb) {
this.removeBufferListeners(type);
if (this.mediaSource) {
this.mediaSource.removeSourceBuffer(sb);
}
// Synchronously remove the SB from the map before the next call in order to prevent an async function from
// accessing it
this.sourceBuffer[type] = undefined;
}
} catch (err) {
logger.warn(`[buffer-controller]: Failed to reset the ${type} buffer`, err);
}
});
this._initSourceBuffer();
}
onBufferCodecs(event, data) {
const sourceBufferCount = this.getSourceBufferTypes().length;
Object.keys(data).forEach(trackName => {
if (sourceBufferCount) {
// check if SourceBuffer codec needs to change
const track = this.tracks[trackName];
if (track && typeof track.buffer.changeType === 'function') {
const {
id,
codec,
levelCodec,
container,
metadata
} = data[trackName];
const currentCodec = (track.levelCodec || track.codec).replace(VIDEO_CODEC_PROFILE_REPACE, '$1');
const nextCodec = (levelCodec || codec).replace(VIDEO_CODEC_PROFILE_REPACE, '$1');
if (currentCodec !== nextCodec) {
const mimeType = `${container};codecs=${levelCodec || codec}`;
this.appendChangeType(trackName, mimeType);
logger.log(`[buffer-controller]: switching codec ${currentCodec} to ${nextCodec}`);
this.tracks[trackName] = {
buffer: track.buffer,
codec,
container,
levelCodec,
metadata,
id
};
}
}
} else {
// if source buffer(s) not created yet, appended buffer tracks in this.pendingTracks
this.pendingTracks[trackName] = data[trackName];
}
});
// if sourcebuffers already created, do nothing ...
if (sourceBufferCount) {
return;
}
this.bufferCodecEventsExpected = Math.max(this.bufferCodecEventsExpected - 1, 0);
if (this.mediaSource && this.mediaSource.readyState === 'open') {
this.checkPendingTracks();
}
}
appendChangeType(type, mimeType) {
const {
operationQueue
} = this;
const operation = {
execute: () => {
const sb = this.sourceBuffer[type];
if (sb) {
logger.log(`[buffer-controller]: changing ${type} sourceBuffer type to ${mimeType}`);
sb.changeType(mimeType);
}
operationQueue.shiftAndExecuteNext(type);
},
onStart: () => {},
onComplete: () => {},
onError: e => {
logger.warn(`[buffer-controller]: Failed to change ${type} SourceBuffer type`, e);
}
};
operationQueue.append(operation, type);
}
onBufferAppending(event, eventData) {
const {
hls,
operationQueue,
tracks
} = this;
const {
data,
type,
frag,
part,
chunkMeta
} = eventData;
const chunkStats = chunkMeta.buffering[type];
const bufferAppendingStart = self.performance.now();
chunkStats.start = bufferAppendingStart;
const fragBuffering = frag.stats.buffering;
const partBuffering = part ? part.stats.buffering : null;
if (fragBuffering.start === 0) {
fragBuffering.start = bufferAppendingStart;
}
if (partBuffering && partBuffering.start === 0) {
partBuffering.start = bufferAppendingStart;
}
// TODO: Only update timestampOffset when audio/mpeg fragment or part is not contiguous with previously appended
// Adjusting `SourceBuffer.timestampOffset` (desired point in the timeline where the next frames should be appended)
// in Chrome browser when we detect MPEG audio container and time delta between level PTS and `SourceBuffer.timestampOffset`
// is greater than 100ms (this is enough to handle seek for VOD or level change for LIVE videos).
// More info here: https://github.com/video-dev/hls.js/issues/332#issuecomment-257986486
const audioTrack = tracks.audio;
let checkTimestampOffset = false;
if (type === 'audio' && (audioTrack == null ? void 0 : audioTrack.container) === 'audio/mpeg') {
checkTimestampOffset = !this.lastMpegAudioChunk || chunkMeta.id === 1 || this.lastMpegAudioChunk.sn !== chunkMeta.sn;
this.lastMpegAudioChunk = chunkMeta;
}
const fragStart = frag.start;
const operation = {
execute: () => {
chunkStats.executeStart = self.performance.now();
if (checkTimestampOffset) {
const sb = this.sourceBuffer[type];
if (sb) {
const delta = fragStart - sb.timestampOffset;
if (Math.abs(delta) >= 0.1) {
logger.log(`[buffer-controller]: Updating audio SourceBuffer timestampOffset to ${fragStart} (delta: ${delta}) sn: ${frag.sn})`);
sb.timestampOffset = fragStart;
}
}
}
this.appendExecutor(data, type);
},
onStart: () => {
// logger.debug(`[buffer-controller]: ${type} SourceBuffer updatestart`);
},
onComplete: () => {
// logger.debug(`[buffer-controller]: ${type} SourceBuffer updateend`);
const end = self.performance.now();
chunkStats.executeEnd = chunkStats.end = end;
if (fragBuffering.first === 0) {
fragBuffering.first = end;
}
if (partBuffering && partBuffering.first === 0) {
partBuffering.first = end;
}
const {
sourceBuffer
} = this;
const timeRanges = {};
for (const type in sourceBuffer) {
timeRanges[type] = BufferHelper.getBuffered(sourceBuffer[type]);
}
this.appendError = 0;
this.hls.trigger(Events.BUFFER_APPENDED, {
type,
frag,
part,
chunkMeta,
parent: frag.type,
timeRanges
});
},
onError: err => {
// in case any error occured while appending, put back segment in segments table
logger.error(`[buffer-controller]: Error encountered while trying to append to the ${type} SourceBuffer`, err);
const event = {
type: ErrorTypes.MEDIA_ERROR,
parent: frag.type,
details: ErrorDetails.BUFFER_APPEND_ERROR,
frag,
part,
chunkMeta,
error: err,
err,
fatal: false
};
if (err.code === DOMException.QUOTA_EXCEEDED_ERR) {
// QuotaExceededError: http://www.w3.org/TR/html5/infrastructure.html#quotaexceedederror
// let's stop appending any segments, and report BUFFER_FULL_ERROR error
event.details = ErrorDetails.BUFFER_FULL_ERROR;
} else {
this.appendError++;
event.details = ErrorDetails.BUFFER_APPEND_ERROR;
/* with UHD content, we could get loop of quota exceeded error until
browser is able to evict some data from sourcebuffer. Retrying can help recover.
*/
if (this.appendError > hls.config.appendErrorMaxRetry) {
logger.error(`[buffer-controller]: Failed ${hls.config.appendErrorMaxRetry} times to append segment in sourceBuffer`);
event.fatal = true;
}
}
hls.trigger(Events.ERROR, event);
}
};
operationQueue.append(operation, type);
}
onBufferFlushing(event, data) {
const {
operationQueue
} = this;
const flushOperation = type => ({
execute: this.removeExecutor.bind(this, type, data.startOffset, data.endOffset),
onStart: () => {
// logger.debug(`[buffer-controller]: Started flushing ${data.startOffset} -> ${data.endOffset} for ${type} Source Buffer`);
},
onComplete: () => {
// logger.debug(`[buffer-controller]: Finished flushing ${data.startOffset} -> ${data.endOffset} for ${type} Source Buffer`);
this.hls.trigger(Events.BUFFER_FLUSHED, {
type
});
},
onError: e => {
logger.warn(`[buffer-controller]: Failed to remove from ${type} SourceBuffer`, e);
}
});
if (data.type) {
operationQueue.append(flushOperation(data.type), data.type);
} else {
this.getSourceBufferTypes().forEach(type => {
operationQueue.append(flushOperation(type), type);
});
}
}
onFragParsed(event, data) {
const {
frag,
part
} = data;
const buffersAppendedTo = [];
const elementaryStreams = part ? part.elementaryStreams : frag.elementaryStreams;
if (elementaryStreams[ElementaryStreamTypes.AUDIOVIDEO]) {
buffersAppendedTo.push('audiovideo');
} else {
if (elementaryStreams[ElementaryStreamTypes.AUDIO]) {
buffersAppendedTo.push('audio');
}
if (elementaryStreams[ElementaryStreamTypes.VIDEO]) {
buffersAppendedTo.push('video');
}
}
const onUnblocked = () => {
const now = self.performance.now();
frag.stats.buffering.end = now;
if (part) {
part.stats.buffering.end = now;
}
const stats = part ? part.stats : frag.stats;
this.hls.trigger(Events.FRAG_BUFFERED, {
frag,
part,
stats,
id: frag.type
});
};
if (buffersAppendedTo.length === 0) {
logger.warn(`Fragments must have at least one ElementaryStreamType set. type: ${frag.type} level: ${frag.level} sn: ${frag.sn}`);
}
this.blockBuffers(onUnblocked, buffersAppendedTo);
}
onFragChanged(event, data) {
this.flushBackBuffer();
}
// on BUFFER_EOS mark matching sourcebuffer(s) as ended and trigger checkEos()
// an undefined data.type will mark all buffers as EOS.
onBufferEos(event, data) {
const ended = this.getSourceBufferTypes().reduce((acc, type) => {
const sb = this.sourceBuffer[type];
if (sb && (!data.type || data.type === type)) {
sb.ending = true;
if (!sb.ended) {
sb.ended = true;
logger.log(`[buffer-controller]: ${type} sourceBuffer now EOS`);
}
}
return acc && !!(!sb || sb.ended);
}, true);
if (ended) {
logger.log(`[buffer-controller]: Queueing mediaSource.endOfStream()`);
this.blockBuffers(() => {
this.getSourceBufferTypes().forEach(type => {
const sb = this.sourceBuffer[type];
if (sb) {
sb.ending = false;
}
});
const {
mediaSource
} = this;
if (!mediaSource || mediaSource.readyState !== 'open') {
if (mediaSource) {
logger.info(`[buffer-controller]: Could not call mediaSource.endOfStream(). mediaSource.readyState: ${mediaSource.readyState}`);
}
return;
}
logger.log(`[buffer-controller]: Calling mediaSource.endOfStream()`);
// Allow this to throw and be caught by the enqueueing function
mediaSource.endOfStream();
});
}
}
onLevelUpdated(event, {
details
}) {
if (!details.fragments.length) {
return;
}
this.details = details;
if (this.getSourceBufferTypes().length) {
this.blockBuffers(this.updateMediaElementDuration.bind(this));
} else {
this.updateMediaElementDuration();
}
}
flushBackBuffer() {
const {
hls,
details,
media,
sourceBuffer
} = this;
if (!media || details === null) {
return;
}
const sourceBufferTypes = this.getSourceBufferTypes();
if (!sourceBufferTypes.length) {
return;
}
// Support for deprecated liveBackBufferLength
const backBufferLength = details.live && hls.config.liveBackBufferLength !== null ? hls.config.liveBackBufferLength : hls.config.backBufferLength;
if (!isFiniteNumber(backBufferLength) || backBufferLength < 0) {
return;
}
const currentTime = media.currentTime;
const targetDuration = details.levelTargetDuration;
const maxBackBufferLength = Math.max(backBufferLength, targetDuration);
const targetBackBufferPosition = Math.floor(currentTime / targetDuration) * targetDuration - maxBackBufferLength;
sourceBufferTypes.forEach(type => {
const sb = sourceBuffer[type];
if (sb) {
const buffered = BufferHelper.getBuffered(sb);
// when target buffer start exceeds actual buffer start
if (buffered.length > 0 && targetBackBufferPosition > buffered.start(0)) {
hls.trigger(Events.BACK_BUFFER_REACHED, {
bufferEnd: targetBackBufferPosition
});
// Support for deprecated event:
if (details.live) {
hls.trigger(Events.LIVE_BACK_BUFFER_REACHED, {
bufferEnd: targetBackBufferPosition
});
} else if (sb.ended && buffered.end(buffered.length - 1) - currentTime < targetDuration * 2) {
logger.info(`[buffer-controller]: Cannot flush ${type} back buffer while SourceBuffer is in ended state`);
return;
}
hls.trigger(Events.BUFFER_FLUSHING, {
startOffset: 0,
endOffset: targetBackBufferPosition,
type
});
}
}
});
}
/**
* Update Media Source duration to current level duration or override to Infinity if configuration parameter
* 'liveDurationInfinity` is set to `true`
* More details: https://github.com/video-dev/hls.js/issues/355
*/
updateMediaElementDuration() {
if (!this.details || !this.media || !this.mediaSource || this.mediaSource.readyState !== 'open') {
return;
}
const {
details,
hls,
media,
mediaSource
} = this;
const levelDuration = details.fragments[0].start + details.totalduration;
const mediaDuration = media.duration;
const msDuration = isFiniteNumber(mediaSource.duration) ? mediaSource.duration : 0;
if (details.live && hls.config.liveDurationInfinity) {
// Override duration to Infinity
logger.log('[buffer-controller]: Media Source duration is set to Infinity');
mediaSource.duration = Infinity;
this.updateSeekableRange(details);
} else if (levelDuration > msDuration && levelDuration > mediaDuration || !isFiniteNumber(mediaDuration)) {
// levelDuration was the last value we set.
// not using mediaSource.duration as the browser may tweak this value
// only update Media Source duration if its value increase, this is to avoid
// flushing already buffered portion when switching between quality level
logger.log(`[buffer-controller]: Updating Media Source duration to ${levelDuration.toFixed(3)}`);
mediaSource.duration = levelDuration;
}
}
updateSeekableRange(levelDetails) {
const mediaSource = this.mediaSource;
const fragments = levelDetails.fragments;
const len = fragments.length;
if (len && levelDetails.live && mediaSource != null && mediaSource.setLiveSeekableRange) {
const start = Math.max(0, fragments[0].start);
const end = Math.max(start, start + levelDetails.totalduration);
mediaSource.setLiveSeekableRange(start, end);
}
}
checkPendingTracks() {
const {
bufferCodecEventsExpected,
operationQueue,
pendingTracks
} = this;
// Check if we've received all of the expected bufferCodec events. When none remain, create all the sourceBuffers at once.
// This is important because the MSE spec allows implementations to throw QuotaExceededErrors if creating new sourceBuffers after
// data has been appended to existing ones.
// 2 tracks is the max (one for audio, one for video). If we've reach this max go ahead and create the buffers.
const pendingTracksCount = Object.keys(pendingTracks).length;
if (pendingTracksCount && !bufferCodecEventsExpected || pendingTracksCount === 2) {
// ok, let's create them now !
this.createSourceBuffers(pendingTracks);
this.pendingTracks = {};
// append any pending segments now !
const buffers = this.getSourceBufferTypes();
if (buffers.length) {
this.hls.trigger(Events.BUFFER_CREATED, {
tracks: this.tracks
});
buffers.forEach(type => {
operationQueue.executeNext(type);
});
} else {
const error = new Error('could not create source buffer for media codec(s)');
this.hls.trigger(Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.BUFFER_INCOMPATIBLE_CODECS_ERROR,
fatal: true,
error,
reason: error.message
});
}
}
}
createSourceBuffers(tracks) {
const {
sourceBuffer,
mediaSource
} = this;
if (!mediaSource) {
throw Error('createSourceBuffers called when mediaSource was null');
}
for (const trackName in tracks) {
if (!sourceBuffer[trackName]) {
const track = tracks[trackName];
if (!track) {
throw Error(`source buffer exists for track ${trackName}, however track does not`);
}
// use levelCodec as first priority
const codec = track.levelCodec || track.codec;
const mimeType = `${track.container};codecs=${codec}`;
logger.log(`[buffer-controller]: creating sourceBuffer(${mimeType})`);
try {
const sb = sourceBuffer[trackName] = mediaSource.addSourceBuffer(mimeType);
const sbName = trackName;
this.addBufferListener(sbName, 'updatestart', this._onSBUpdateStart);
this.addBufferListener(sbName, 'updateend', this._onSBUpdateEnd);
this.addBufferListener(sbName, 'error', this._onSBUpdateError);
this.tracks[trackName] = {
buffer: sb,
codec: codec,
container: track.container,
levelCodec: track.levelCodec,
metadata: track.metadata,
id: track.id
};
} catch (err) {
logger.error(`[buffer-controller]: error while trying to add sourceBuffer: ${err.message}`);
this.hls.trigger(Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.BUFFER_ADD_CODEC_ERROR,
fatal: false,
error: err,
mimeType: mimeType
});
}
}
}
}
_onSBUpdateStart(type) {
const {
operationQueue
} = this;
const operation = operationQueue.current(type);
operation.onStart();
}
_onSBUpdateEnd(type) {
const {
operationQueue
} = this;
const operation = operationQueue.current(type);
operation.onComplete();
operationQueue.shiftAndExecuteNext(type);
}
_onSBUpdateError(type, event) {
const error = new Error(`${type} SourceBuffer error`);
logger.error(`[buffer-controller]: ${error}`, event);
// according to http://www.w3.org/TR/media-source/#sourcebuffer-append-error
// SourceBuffer errors are not necessarily fatal; if so, the HTMLMediaElement will fire an error event
this.hls.trigger(Events.ERROR, {
type: ErrorTypes.MEDIA_ERROR,
details: ErrorDetails.BUFFER_APPENDING_ERROR,
error,
fatal: false
});
// updateend is always fired after error, so we'll allow that to shift the current operation off of the queue
const operation = this.operationQueue.current(type);
if (operation) {
operation.onError(event);
}
}
// This method must result in an updateend event; if remove is not called, _onSBUpdateEnd must be called manually
removeExecutor(type, startOffset, endOffset) {
const {
media,
mediaSource,
operationQueue,
sourceBuffer
} = this;
const sb = sourceBuffer[type];
if (!media || !mediaSource || !sb) {
logger.warn(`[buffer-controller]: Attempting to remove from the ${type} SourceBuffer, but it does not exist`);
operationQueue.shiftAndExecuteNext(type);
return;
}
const mediaDuration = isFiniteNumber(media.duration) ? media.duration : Infinity;
const msDuration = isFiniteNumber(mediaSource.duration) ? mediaSource.duration : Infinity;
const removeStart = Math.max(0, startOffset);
const removeEnd = Math.min(endOffset, mediaDuration, msDuration);
if (removeEnd > removeStart && !sb.ending) {
sb.ended = false;
logger.log(`[buffer-controller]: Removing [${removeStart},${removeEnd}] from the ${type} SourceBuffer`);
sb.remove(removeStart, removeEnd);
} else {
// Cycle the queue
operationQueue.shiftAndExecuteNext(type);
}
}
// This method must result in an updateend event; if append is not called, _onSBUpdateEnd must be called manually
appendExecutor(data, type) {
const {
operationQueue,
sourceBuffer
} = this;
const sb = sourceBuffer[type];
if (!sb) {
logger.warn(`[buffer-controller]: Attempting to append to the ${type} SourceBuffer, but it does not exist`);
operationQueue.shiftAndExecuteNext(type);
return;
}
sb.ended = false;
sb.appendBuffer(data);
}
// Enqueues an operation to each SourceBuffer queue which, upon execution, resolves a promise. When all promises
// resolve, the onUnblocked function is executed. Functions calling this method do not need to unblock the queue
// upon completion, since we already do it here
blockBuffers(onUnblocked, buffers = this.getSourceBufferTypes()) {
if (!buffers.length) {
logger.log('[buffer-controller]: Blocking operation requested, but no SourceBuffers exist');
Promise.resolve().then(onUnblocked);
return;
}
const {
operationQueue
} = this;
// logger.debug(`[buffer-controller]: Blocking ${buffers} SourceBuffer`);
const blockingOperations = buffers.map(type => operationQueue.appendBlocker(type));
Promise.all(blockingOperations).then(() => {
// logger.debug(`[buffer-controller]: Blocking operation resolved; unblocking ${buffers} SourceBuffer`);
onUnblocked();
buffers.forEach(type => {
const sb = this.sourceBuffer[type];
// Only cycle the queue if the SB is not updating. There's a bug in Chrome which sets the SB updating flag to
// true when changing the MediaSource duration (https://bugs.chromium.org/p/chromium/issues/detail?id=959359&can=2&q=mediasource%20duration)
// While this is a workaround, it's probably useful to have around
if (!(sb != null && sb.updating)) {
operationQueue.shiftAndExecuteNext(type);
}
});
});
}
getSourceBufferTypes() {
return Object.keys(this.sourceBuffer);
}
addBufferListener(type, event, fn) {
const buffer = this.sourceBuffer[type];
if (!buffer) {
return;
}
const listener = fn.bind(this, type);
this.listeners[type].push({
event,
listener
});
buffer.addEventListener(event, listener);
}
removeBufferListeners(type) {
const buffer = this.sourceBuffer[type];
if (!buffer) {
return;
}
this.listeners[type].forEach(l => {
buffer.removeEventListener(l.event, l.listener);
});
}
}
/**
*
* This code was ported from the dash.js project at:
* https://github.com/Dash-Industry-Forum/dash.js/blob/development/externals/cea608-parser.js
* https://github.com/Dash-Industry-Forum/dash.js/commit/8269b26a761e0853bb21d78780ed945144ecdd4d#diff-71bc295a2d6b6b7093a1d3290d53a4b2
*
* The original copyright appears below:
*
* The copyright in this software is being made available under the BSD License,
* included below. This software may be subject to other third party and contributor
* rights, including patent rights, and no such rights are granted under this license.
*
* Copyright (c) 2015-2016, DASH Industry Forum.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* 2. Neither the name of Dash Industry Forum nor the names of its
* contributors may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Exceptions from regular ASCII. CodePoints are mapped to UTF-16 codes
*/
const specialCea608CharsCodes = {
0x2a: 0xe1,
// lowercase a, acute accent
0x5c: 0xe9,
// lowercase e, acute accent
0x5e: 0xed,
// lowercase i, acute accent
0x5f: 0xf3,
// lowercase o, acute accent
0x60: 0xfa,
// lowercase u, acute accent
0x7b: 0xe7,
// lowercase c with cedilla
0x7c: 0xf7,
// division symbol
0x7d: 0xd1,
// uppercase N tilde
0x7e: 0xf1,
// lowercase n tilde
0x7f: 0x2588,
// Full block
// THIS BLOCK INCLUDES THE 16 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
// THAT COME FROM HI BYTE=0x11 AND LOW BETWEEN 0x30 AND 0x3F
// THIS MEANS THAT \x50 MUST BE ADDED TO THE VALUES
0x80: 0xae,
// Registered symbol (R)
0x81: 0xb0,
// degree sign
0x82: 0xbd,
// 1/2 symbol
0x83: 0xbf,
// Inverted (open) question mark
0x84: 0x2122,
// Trademark symbol (TM)
0x85: 0xa2,
// Cents symbol
0x86: 0xa3,
// Pounds sterling
0x87: 0x266a,
// Music 8'th note
0x88: 0xe0,
// lowercase a, grave accent
0x89: 0x20,
// transparent space (regular)
0x8a: 0xe8,
// lowercase e, grave accent
0x8b: 0xe2,
// lowercase a, circumflex accent
0x8c: 0xea,
// lowercase e, circumflex accent
0x8d: 0xee,
// lowercase i, circumflex accent
0x8e: 0xf4,
// lowercase o, circumflex accent
0x8f: 0xfb,
// lowercase u, circumflex accent
// THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
// THAT COME FROM HI BYTE=0x12 AND LOW BETWEEN 0x20 AND 0x3F
0x90: 0xc1,
// capital letter A with acute
0x91: 0xc9,
// capital letter E with acute
0x92: 0xd3,
// capital letter O with acute
0x93: 0xda,
// capital letter U with acute
0x94: 0xdc,
// capital letter U with diaresis
0x95: 0xfc,
// lowercase letter U with diaeresis
0x96: 0x2018,
// opening single quote
0x97: 0xa1,
// inverted exclamation mark
0x98: 0x2a,
// asterisk
0x99: 0x2019,
// closing single quote
0x9a: 0x2501,
// box drawings heavy horizontal
0x9b: 0xa9,
// copyright sign
0x9c: 0x2120,
// Service mark
0x9d: 0x2022,
// (round) bullet
0x9e: 0x201c,
// Left double quotation mark
0x9f: 0x201d,
// Right double quotation mark
0xa0: 0xc0,
// uppercase A, grave accent
0xa1: 0xc2,
// uppercase A, circumflex
0xa2: 0xc7,
// uppercase C with cedilla
0xa3: 0xc8,
// uppercase E, grave accent
0xa4: 0xca,
// uppercase E, circumflex
0xa5: 0xcb,
// capital letter E with diaresis
0xa6: 0xeb,
// lowercase letter e with diaresis
0xa7: 0xce,
// uppercase I, circumflex
0xa8: 0xcf,
// uppercase I, with diaresis
0xa9: 0xef,
// lowercase i, with diaresis
0xaa: 0xd4,
// uppercase O, circumflex
0xab: 0xd9,
// uppercase U, grave accent
0xac: 0xf9,
// lowercase u, grave accent
0xad: 0xdb,
// uppercase U, circumflex
0xae: 0xab,
// left-pointing double angle quotation mark
0xaf: 0xbb,
// right-pointing double angle quotation mark
// THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
// THAT COME FROM HI BYTE=0x13 AND LOW BETWEEN 0x20 AND 0x3F
0xb0: 0xc3,
// Uppercase A, tilde
0xb1: 0xe3,
// Lowercase a, tilde
0xb2: 0xcd,
// Uppercase I, acute accent
0xb3: 0xcc,
// Uppercase I, grave accent
0xb4: 0xec,
// Lowercase i, grave accent
0xb5: 0xd2,
// Uppercase O, grave accent
0xb6: 0xf2,
// Lowercase o, grave accent
0xb7: 0xd5,
// Uppercase O, tilde
0xb8: 0xf5,
// Lowercase o, tilde
0xb9: 0x7b,
// Open curly brace
0xba: 0x7d,
// Closing curly brace
0xbb: 0x5c,
// Backslash
0xbc: 0x5e,
// Caret
0xbd: 0x5f,
// Underscore
0xbe: 0x7c,
// Pipe (vertical line)
0xbf: 0x223c,
// Tilde operator
0xc0: 0xc4,
// Uppercase A, umlaut
0xc1: 0xe4,
// Lowercase A, umlaut
0xc2: 0xd6,
// Uppercase O, umlaut
0xc3: 0xf6,
// Lowercase o, umlaut
0xc4: 0xdf,
// Esszett (sharp S)
0xc5: 0xa5,
// Yen symbol
0xc6: 0xa4,
// Generic currency sign
0xc7: 0x2503,
// Box drawings heavy vertical
0xc8: 0xc5,
// Uppercase A, ring
0xc9: 0xe5,
// Lowercase A, ring
0xca: 0xd8,
// Uppercase O, stroke
0xcb: 0xf8,
// Lowercase o, strok
0xcc: 0x250f,
// Box drawings heavy down and right
0xcd: 0x2513,
// Box drawings heavy down and left
0xce: 0x2517,
// Box drawings heavy up and right
0xcf: 0x251b // Box drawings heavy up and left
};
/**
* Utils
*/
const getCharForByte = function getCharForByte(byte) {
let charCode = byte;
if (specialCea608CharsCodes.hasOwnProperty(byte)) {
charCode = specialCea608CharsCodes[byte];
}
return String.fromCharCode(charCode);
};
const NR_ROWS = 15;
const NR_COLS = 100;
// Tables to look up row from PAC data
const rowsLowCh1 = {
0x11: 1,
0x12: 3,
0x15: 5,
0x16: 7,
0x17: 9,
0x10: 11,
0x13: 12,
0x14: 14
};
const rowsHighCh1 = {
0x11: 2,
0x12: 4,
0x15: 6,
0x16: 8,
0x17: 10,
0x13: 13,
0x14: 15
};
const rowsLowCh2 = {
0x19: 1,
0x1a: 3,
0x1d: 5,
0x1e: 7,
0x1f: 9,
0x18: 11,
0x1b: 12,
0x1c: 14
};
const rowsHighCh2 = {
0x19: 2,
0x1a: 4,
0x1d: 6,
0x1e: 8,
0x1f: 10,
0x1b: 13,
0x1c: 15
};
const backgroundColors = ['white', 'green', 'blue', 'cyan', 'red', 'yellow', 'magenta', 'black', 'transparent'];
class CaptionsLogger {
constructor() {
this.time = null;
this.verboseLevel = 0;
}
log(severity, msg) {
if (this.verboseLevel >= severity) {
const m = typeof msg === 'function' ? msg() : msg;
logger.log(`${this.time} [${severity}] ${m}`);
}
}
}
const numArrayToHexArray = function numArrayToHexArray(numArray) {
const hexArray = [];
for (let j = 0; j < numArray.length; j++) {
hexArray.push(numArray[j].toString(16));
}
return hexArray;
};
class PenState {
constructor(foreground, underline, italics, background, flash) {
this.foreground = void 0;
this.underline = void 0;
this.italics = void 0;
this.background = void 0;
this.flash = void 0;
this.foreground = foreground || 'white';
this.underline = underline || false;
this.italics = italics || false;
this.background = background || 'black';
this.flash = flash || false;
}
reset() {
this.foreground = 'white';
this.underline = false;
this.italics = false;
this.background = 'black';
this.flash = false;
}
setStyles(styles) {
const attribs = ['foreground', 'underline', 'italics', 'background', 'flash'];
for (let i = 0; i < attribs.length; i++) {
const style = attribs[i];
if (styles.hasOwnProperty(style)) {
this[style] = styles[style];
}
}
}
isDefault() {
return this.foreground === 'white' && !this.underline && !this.italics && this.background === 'black' && !this.flash;
}
equals(other) {
return this.foreground === other.foreground && this.underline === other.underline && this.italics === other.italics && this.background === other.background && this.flash === other.flash;
}
copy(newPenState) {
this.foreground = newPenState.foreground;
this.underline = newPenState.underline;
this.italics = newPenState.italics;
this.background = newPenState.background;
this.flash = newPenState.flash;
}
toString() {
return 'color=' + this.foreground + ', underline=' + this.underline + ', italics=' + this.italics + ', background=' + this.background + ', flash=' + this.flash;
}
}
/**
* Unicode character with styling and background.
* @constructor
*/
class StyledUnicodeChar {
constructor(uchar, foreground, underline, italics, background, flash) {
this.uchar = void 0;
this.penState = void 0;
this.uchar = uchar || ' '; // unicode character
this.penState = new PenState(foreground, underline, italics, background, flash);
}
reset() {
this.uchar = ' ';
this.penState.reset();
}
setChar(uchar, newPenState) {
this.uchar = uchar;
this.penState.copy(newPenState);
}
setPenState(newPenState) {
this.penState.copy(newPenState);
}
equals(other) {
return this.uchar === other.uchar && this.penState.equals(other.penState);
}
copy(newChar) {
this.uchar = newChar.uchar;
this.penState.copy(newChar.penState);
}
isEmpty() {
return this.uchar === ' ' && this.penState.isDefault();
}
}
/**
* CEA-608 row consisting of NR_COLS instances of StyledUnicodeChar.
* @constructor
*/
class Row {
constructor(logger) {
this.chars = void 0;
this.pos = void 0;
this.currPenState = void 0;
this.cueStartTime = void 0;
this.logger = void 0;
this.chars = [];
for (let i = 0; i < NR_COLS; i++) {
this.chars.push(new StyledUnicodeChar());
}
this.logger = logger;
this.pos = 0;
this.currPenState = new PenState();
}
equals(other) {
let equal = true;
for (let i = 0; i < NR_COLS; i++) {
if (!this.chars[i].equals(other.chars[i])) {
equal = false;
break;
}
}
return equal;
}
copy(other) {
for (let i = 0; i < NR_COLS; i++) {
this.chars[i].copy(other.chars[i]);
}
}
isEmpty() {
let empty = true;
for (let i = 0; i < NR_COLS; i++) {
if (!this.chars[i].isEmpty()) {
empty = false;
break;
}
}
return empty;
}
/**
* Set the cursor to a valid column.
*/
setCursor(absPos) {
if (this.pos !== absPos) {
this.pos = absPos;
}
if (this.pos < 0) {
this.logger.log(3, 'Negative cursor position ' + this.pos);
this.pos = 0;
} else if (this.pos > NR_COLS) {
this.logger.log(3, 'Too large cursor position ' + this.pos);
this.pos = NR_COLS;
}
}
/**
* Move the cursor relative to current position.
*/
moveCursor(relPos) {
const newPos = this.pos + relPos;
if (relPos > 1) {
for (let i = this.pos + 1; i < newPos + 1; i++) {
this.chars[i].setPenState(this.currPenState);
}
}
this.setCursor(newPos);
}
/**
* Backspace, move one step back and clear character.
*/
backSpace() {
this.moveCursor(-1);
this.chars[this.pos].setChar(' ', this.currPenState);
}
insertChar(byte) {
if (byte >= 0x90) {
// Extended char
this.backSpace();
}
const char = getCharForByte(byte);
if (this.pos >= NR_COLS) {
this.logger.log(0, () => 'Cannot insert ' + byte.toString(16) + ' (' + char + ') at position ' + this.pos + '. Skipping it!');
return;
}
this.chars[this.pos].setChar(char, this.currPenState);
this.moveCursor(1);
}
clearFromPos(startPos) {
let i;
for (i = startPos; i < NR_COLS; i++) {
this.chars[i].reset();
}
}
clear() {
this.clearFromPos(0);
this.pos = 0;
this.currPenState.reset();
}
clearToEndOfRow() {
this.clearFromPos(this.pos);
}
getTextString() {
const chars = [];
let empty = true;
for (let i = 0; i < NR_COLS; i++) {
const char = this.chars[i].uchar;
if (char !== ' ') {
empty = false;
}
chars.push(char);
}
if (empty) {
return '';
} else {
return chars.join('');
}
}
setPenStyles(styles) {
this.currPenState.setStyles(styles);
const currChar = this.chars[this.pos];
currChar.setPenState(this.currPenState);
}
}
/**
* Keep a CEA-608 screen of 32x15 styled characters
* @constructor
*/
class CaptionScreen {
constructor(logger) {
this.rows = void 0;
this.currRow = void 0;
this.nrRollUpRows = void 0;
this.lastOutputScreen = void 0;
this.logger = void 0;
this.rows = [];
for (let i = 0; i < NR_ROWS; i++) {
this.rows.push(new Row(logger));
} // Note that we use zero-based numbering (0-14)
this.logger = logger;
this.currRow = NR_ROWS - 1;
this.nrRollUpRows = null;
this.lastOutputScreen = null;
this.reset();
}
reset() {
for (let i = 0; i < NR_ROWS; i++) {
this.rows[i].clear();
}
this.currRow = NR_ROWS - 1;
}
equals(other) {
let equal = true;
for (let i = 0; i < NR_ROWS; i++) {
if (!this.rows[i].equals(other.rows[i])) {
equal = false;
break;
}
}
return equal;
}
copy(other) {
for (let i = 0; i < NR_ROWS; i++) {
this.rows[i].copy(other.rows[i]);
}
}
isEmpty() {
let empty = true;
for (let i = 0; i < NR_ROWS; i++) {
if (!this.rows[i].isEmpty()) {
empty = false;
break;
}
}
return empty;
}
backSpace() {
const row = this.rows[this.currRow];
row.backSpace();
}
clearToEndOfRow() {
const row = this.rows[this.currRow];
row.clearToEndOfRow();
}
/**
* Insert a character (without styling) in the current row.
*/
insertChar(char) {
const row = this.rows[this.currRow];
row.insertChar(char);
}
setPen(styles) {
const row = this.rows[this.currRow];
row.setPenStyles(styles);
}
moveCursor(relPos) {
const row = this.rows[this.currRow];
row.moveCursor(relPos);
}
setCursor(absPos) {
this.logger.log(2, 'setCursor: ' + absPos);
const row = this.rows[this.currRow];
row.setCursor(absPos);
}
setPAC(pacData) {
this.logger.log(2, () => 'pacData = ' + JSON.stringify(pacData));
let newRow = pacData.row - 1;
if (this.nrRollUpRows && newRow < this.nrRollUpRows - 1) {
newRow = this.nrRollUpRows - 1;
}
// Make sure this only affects Roll-up Captions by checking this.nrRollUpRows
if (this.nrRollUpRows && this.currRow !== newRow) {
// clear all rows first
for (let i = 0; i < NR_ROWS; i++) {
this.rows[i].clear();
}
// Copy this.nrRollUpRows rows from lastOutputScreen and place it in the newRow location
// topRowIndex - the start of rows to copy (inclusive index)
const topRowIndex = this.currRow + 1 - this.nrRollUpRows;
// We only copy if the last position was already shown.
// We use the cueStartTime value to check this.
const lastOutputScreen = this.lastOutputScreen;
if (lastOutputScreen) {
const prevLineTime = lastOutputScreen.rows[topRowIndex].cueStartTime;
const time = this.logger.time;
if (prevLineTime && time !== null && prevLineTime < time) {
for (let i = 0; i < this.nrRollUpRows; i++) {
this.rows[newRow - this.nrRollUpRows + i + 1].copy(lastOutputScreen.rows[topRowIndex + i]);
}
}
}
}
this.currRow = newRow;
const row = this.rows[this.currRow];
if (pacData.indent !== null) {
const indent = pacData.indent;
const prevPos = Math.max(indent - 1, 0);
row.setCursor(pacData.indent);
pacData.color = row.chars[prevPos].penState.foreground;
}
const styles = {
foreground: pacData.color,
underline: pacData.underline,
italics: pacData.italics,
background: 'black',
flash: false
};
this.setPen(styles);
}
/**
* Set background/extra foreground, but first do back_space, and then insert space (backwards compatibility).
*/
setBkgData(bkgData) {
this.logger.log(2, () => 'bkgData = ' + JSON.stringify(bkgData));
this.backSpace();
this.setPen(bkgData);
this.insertChar(0x20); // Space
}
setRollUpRows(nrRows) {
this.nrRollUpRows = nrRows;
}
rollUp() {
if (this.nrRollUpRows === null) {
this.logger.log(3, 'roll_up but nrRollUpRows not set yet');
return; // Not properly setup
}
this.logger.log(1, () => this.getDisplayText());
const topRowIndex = this.currRow + 1 - this.nrRollUpRows;
const topRow = this.rows.splice(topRowIndex, 1)[0];
topRow.clear();
this.rows.splice(this.currRow, 0, topRow);
this.logger.log(2, 'Rolling up');
// this.logger.log(VerboseLevel.TEXT, this.get_display_text())
}
/**
* Get all non-empty rows with as unicode text.
*/
getDisplayText(asOneRow) {
asOneRow = asOneRow || false;
const displayText = [];
let text = '';
let rowNr = -1;
for (let i = 0; i < NR_ROWS; i++) {
const rowText = this.rows[i].getTextString();
if (rowText) {
rowNr = i + 1;
if (asOneRow) {
displayText.push('Row ' + rowNr + ": '" + rowText + "'");
} else {
displayText.push(rowText.trim());
}
}
}
if (displayText.length > 0) {
if (asOneRow) {
text = '[' + displayText.join(' | ') + ']';
} else {
text = displayText.join('\n');
}
}
return text;
}
getTextAndFormat() {
return this.rows;
}
}
// var modes = ['MODE_ROLL-UP', 'MODE_POP-ON', 'MODE_PAINT-ON', 'MODE_TEXT'];
class Cea608Channel {
constructor(channelNumber, outputFilter, logger) {
this.chNr = void 0;
this.outputFilter = void 0;
this.mode = void 0;
this.verbose = void 0;
this.displayedMemory = void 0;
this.nonDisplayedMemory = void 0;
this.lastOutputScreen = void 0;
this.currRollUpRow = void 0;
this.writeScreen = void 0;
this.cueStartTime = void 0;
this.logger = void 0;
this.chNr = channelNumber;
this.outputFilter = outputFilter;
this.mode = null;
this.verbose = 0;
this.displayedMemory = new CaptionScreen(logger);
this.nonDisplayedMemory = new CaptionScreen(logger);
this.lastOutputScreen = new CaptionScreen(logger);
this.currRollUpRow = this.displayedMemory.rows[NR_ROWS - 1];
this.writeScreen = this.displayedMemory;
this.mode = null;
this.cueStartTime = null; // Keeps track of where a cue started.
this.logger = logger;
}
reset() {
this.mode = null;
this.displayedMemory.reset();
this.nonDisplayedMemory.reset();
this.lastOutputScreen.reset();
this.outputFilter.reset();
this.currRollUpRow = this.displayedMemory.rows[NR_ROWS - 1];
this.writeScreen = this.displayedMemory;
this.mode = null;
this.cueStartTime = null;
}
getHandler() {
return this.outputFilter;
}
setHandler(newHandler) {
this.outputFilter = newHandler;
}
setPAC(pacData) {
this.writeScreen.setPAC(pacData);
}
setBkgData(bkgData) {
this.writeScreen.setBkgData(bkgData);
}
setMode(newMode) {
if (newMode === this.mode) {
return;
}
this.mode = newMode;
this.logger.log(2, () => 'MODE=' + newMode);
if (this.mode === 'MODE_POP-ON') {
this.writeScreen = this.nonDisplayedMemory;
} else {
this.writeScreen = this.displayedMemory;
this.writeScreen.reset();
}
if (this.mode !== 'MODE_ROLL-UP') {
this.displayedMemory.nrRollUpRows = null;
this.nonDisplayedMemory.nrRollUpRows = null;
}
this.mode = newMode;
}
insertChars(chars) {
for (let i = 0; i < chars.length; i++) {
this.writeScreen.insertChar(chars[i]);
}
const screen = this.writeScreen === this.displayedMemory ? 'DISP' : 'NON_DISP';
this.logger.log(2, () => screen + ': ' + this.writeScreen.getDisplayText(true));
if (this.mode === 'MODE_PAINT-ON' || this.mode === 'MODE_ROLL-UP') {
this.logger.log(1, () => 'DISPLAYED: ' + this.displayedMemory.getDisplayText(true));
this.outputDataUpdate();
}
}
ccRCL() {
// Resume Caption Loading (switch mode to Pop On)
this.logger.log(2, 'RCL - Resume Caption Loading');
this.setMode('MODE_POP-ON');
}
ccBS() {
// BackSpace
this.logger.log(2, 'BS - BackSpace');
if (this.mode === 'MODE_TEXT') {
return;
}
this.writeScreen.backSpace();
if (this.writeScreen === this.displayedMemory) {
this.outputDataUpdate();
}
}
ccAOF() {
// Reserved (formerly Alarm Off)
}
ccAON() {
// Reserved (formerly Alarm On)
}
ccDER() {
// Delete to End of Row
this.logger.log(2, 'DER- Delete to End of Row');
this.writeScreen.clearToEndOfRow();
this.outputDataUpdate();
}
ccRU(nrRows) {
// Roll-Up Captions-2,3,or 4 Rows
this.logger.log(2, 'RU(' + nrRows + ') - Roll Up');
this.writeScreen = this.displayedMemory;
this.setMode('MODE_ROLL-UP');
this.writeScreen.setRollUpRows(nrRows);
}
ccFON() {
// Flash On
this.logger.log(2, 'FON - Flash On');
this.writeScreen.setPen({
flash: true
});
}
ccRDC() {
// Resume Direct Captioning (switch mode to PaintOn)
this.logger.log(2, 'RDC - Resume Direct Captioning');
this.setMode('MODE_PAINT-ON');
}
ccTR() {
// Text Restart in text mode (not supported, however)
this.logger.log(2, 'TR');
this.setMode('MODE_TEXT');
}
ccRTD() {
// Resume Text Display in Text mode (not supported, however)
this.logger.log(2, 'RTD');
this.setMode('MODE_TEXT');
}
ccEDM() {
// Erase Displayed Memory
this.logger.log(2, 'EDM - Erase Displayed Memory');
this.displayedMemory.reset();
this.outputDataUpdate(true);
}
ccCR() {
// Carriage Return
this.logger.log(2, 'CR - Carriage Return');
this.writeScreen.rollUp();
this.outputDataUpdate(true);
}
ccENM() {
// Erase Non-Displayed Memory
this.logger.log(2, 'ENM - Erase Non-displayed Memory');
this.nonDisplayedMemory.reset();
}
ccEOC() {
// End of Caption (Flip Memories)
this.logger.log(2, 'EOC - End Of Caption');
if (this.mode === 'MODE_POP-ON') {
const tmp = this.displayedMemory;
this.displayedMemory = this.nonDisplayedMemory;
this.nonDisplayedMemory = tmp;
this.writeScreen = this.nonDisplayedMemory;
this.logger.log(1, () => 'DISP: ' + this.displayedMemory.getDisplayText());
}
this.outputDataUpdate(true);
}
ccTO(nrCols) {
// Tab Offset 1,2, or 3 columns
this.logger.log(2, 'TO(' + nrCols + ') - Tab Offset');
this.writeScreen.moveCursor(nrCols);
}
ccMIDROW(secondByte) {
// Parse MIDROW command
const styles = {
flash: false
};
styles.underline = secondByte % 2 === 1;
styles.italics = secondByte >= 0x2e;
if (!styles.italics) {
const colorIndex = Math.floor(secondByte / 2) - 0x10;
const colors = ['white', 'green', 'blue', 'cyan', 'red', 'yellow', 'magenta'];
styles.foreground = colors[colorIndex];
} else {
styles.foreground = 'white';
}
this.logger.log(2, 'MIDROW: ' + JSON.stringify(styles));
this.writeScreen.setPen(styles);
}
outputDataUpdate(dispatch = false) {
const time = this.logger.time;
if (time === null) {
return;
}
if (this.outputFilter) {
if (this.cueStartTime === null && !this.displayedMemory.isEmpty()) {
// Start of a new cue
this.cueStartTime = time;
} else {
if (!this.displayedMemory.equals(this.lastOutputScreen)) {
this.outputFilter.newCue(this.cueStartTime, time, this.lastOutputScreen);
if (dispatch && this.outputFilter.dispatchCue) {
this.outputFilter.dispatchCue();
}
this.cueStartTime = this.displayedMemory.isEmpty() ? null : time;
}
}
this.lastOutputScreen.copy(this.displayedMemory);
}
}
cueSplitAtTime(t) {
if (this.outputFilter) {
if (!this.displayedMemory.isEmpty()) {
if (this.outputFilter.newCue) {
this.outputFilter.newCue(this.cueStartTime, t, this.displayedMemory);
}
this.cueStartTime = t;
}
}
}
}
// Will be 1 or 2 when parsing captions
class Cea608Parser {
constructor(field, out1, out2) {
this.channels = void 0;
this.currentChannel = 0;
this.cmdHistory = void 0;
this.logger = void 0;
const logger = new CaptionsLogger();
this.channels = [null, new Cea608Channel(field, out1, logger), new Cea608Channel(field + 1, out2, logger)];
this.cmdHistory = createCmdHistory();
this.logger = logger;
}
getHandler(channel) {
return this.channels[channel].getHandler();
}
setHandler(channel, newHandler) {
this.channels[channel].setHandler(newHandler);
}
/**
* Add data for time t in forms of list of bytes (unsigned ints). The bytes are treated as pairs.
*/
addData(time, byteList) {
let cmdFound;
let a;
let b;
let charsFound = false;
this.logger.time = time;
for (let i = 0; i < byteList.length; i += 2) {
a = byteList[i] & 0x7f;
b = byteList[i + 1] & 0x7f;
if (a === 0 && b === 0) {
continue;
} else {
this.logger.log(3, '[' + numArrayToHexArray([byteList[i], byteList[i + 1]]) + '] -> (' + numArrayToHexArray([a, b]) + ')');
}
cmdFound = this.parseCmd(a, b);
if (!cmdFound) {
cmdFound = this.parseMidrow(a, b);
}
if (!cmdFound) {
cmdFound = this.parsePAC(a, b);
}
if (!cmdFound) {
cmdFound = this.parseBackgroundAttributes(a, b);
}
if (!cmdFound) {
charsFound = this.parseChars(a, b);
if (charsFound) {
const currChNr = this.currentChannel;
if (currChNr && currChNr > 0) {
const channel = this.channels[currChNr];
channel.insertChars(charsFound);
} else {
this.logger.log(2, 'No channel found yet. TEXT-MODE?');
}
}
}
if (!cmdFound && !charsFound) {
this.logger.log(2, "Couldn't parse cleaned data " + numArrayToHexArray([a, b]) + ' orig: ' + numArrayToHexArray([byteList[i], byteList[i + 1]]));
}
}
}
/**
* Parse Command.
* @returns True if a command was found
*/
parseCmd(a, b) {
const {
cmdHistory
} = this;
const cond1 = (a === 0x14 || a === 0x1c || a === 0x15 || a === 0x1d) && b >= 0x20 && b <= 0x2f;
const cond2 = (a === 0x17 || a === 0x1f) && b >= 0x21 && b <= 0x23;
if (!(cond1 || cond2)) {
return false;
}
if (hasCmdRepeated(a, b, cmdHistory)) {
setLastCmd(null, null, cmdHistory);
this.logger.log(3, 'Repeated command (' + numArrayToHexArray([a, b]) + ') is dropped');
return true;
}
const chNr = a === 0x14 || a === 0x15 || a === 0x17 ? 1 : 2;
const channel = this.channels[chNr];
if (a === 0x14 || a === 0x15 || a === 0x1c || a === 0x1d) {
if (b === 0x20) {
channel.ccRCL();
} else if (b === 0x21) {
channel.ccBS();
} else if (b === 0x22) {
channel.ccAOF();
} else if (b === 0x23) {
channel.ccAON();
} else if (b === 0x24) {
channel.ccDER();
} else if (b === 0x25) {
channel.ccRU(2);
} else if (b === 0x26) {
channel.ccRU(3);
} else if (b === 0x27) {
channel.ccRU(4);
} else if (b === 0x28) {
channel.ccFON();
} else if (b === 0x29) {
channel.ccRDC();
} else if (b === 0x2a) {
channel.ccTR();
} else if (b === 0x2b) {
channel.ccRTD();
} else if (b === 0x2c) {
channel.ccEDM();
} else if (b === 0x2d) {
channel.ccCR();
} else if (b === 0x2e) {
channel.ccENM();
} else if (b === 0x2f) {
channel.ccEOC();
}
} else {
// a == 0x17 || a == 0x1F
channel.ccTO(b - 0x20);
}
setLastCmd(a, b, cmdHistory);
this.currentChannel = chNr;
return true;
}
/**
* Parse midrow styling command
*/
parseMidrow(a, b) {
let chNr = 0;
if ((a === 0x11 || a === 0x19) && b >= 0x20 && b <= 0x2f) {
if (a === 0x11) {
chNr = 1;
} else {
chNr = 2;
}
if (chNr !== this.currentChannel) {
this.logger.log(0, 'Mismatch channel in midrow parsing');
return false;
}
const channel = this.channels[chNr];
if (!channel) {
return false;
}
channel.ccMIDROW(b);
this.logger.log(3, 'MIDROW (' + numArrayToHexArray([a, b]) + ')');
return true;
}
return false;
}
/**
* Parse Preable Access Codes (Table 53).
* @returns {Boolean} Tells if PAC found
*/
parsePAC(a, b) {
let row;
const cmdHistory = this.cmdHistory;
const case1 = (a >= 0x11 && a <= 0x17 || a >= 0x19 && a <= 0x1f) && b >= 0x40 && b <= 0x7f;
const case2 = (a === 0x10 || a === 0x18) && b >= 0x40 && b <= 0x5f;
if (!(case1 || case2)) {
return false;
}
if (hasCmdRepeated(a, b, cmdHistory)) {
setLastCmd(null, null, cmdHistory);
return true; // Repeated commands are dropped (once)
}
const chNr = a <= 0x17 ? 1 : 2;
if (b >= 0x40 && b <= 0x5f) {
row = chNr === 1 ? rowsLowCh1[a] : rowsLowCh2[a];
} else {
// 0x60 <= b <= 0x7F
row = chNr === 1 ? rowsHighCh1[a] : rowsHighCh2[a];
}
const channel = this.channels[chNr];
if (!channel) {
return false;
}
channel.setPAC(this.interpretPAC(row, b));
setLastCmd(a, b, cmdHistory);
this.currentChannel = chNr;
return true;
}
/**
* Interpret the second byte of the pac, and return the information.
* @returns pacData with style parameters
*/
interpretPAC(row, byte) {
let pacIndex;
const pacData = {
color: null,
italics: false,
indent: null,
underline: false,
row: row
};
if (byte > 0x5f) {
pacIndex = byte - 0x60;
} else {
pacIndex = byte - 0x40;
}
pacData.underline = (pacIndex & 1) === 1;
if (pacIndex <= 0xd) {
pacData.color = ['white', 'green', 'blue', 'cyan', 'red', 'yellow', 'magenta', 'white'][Math.floor(pacIndex / 2)];
} else if (pacIndex <= 0xf) {
pacData.italics = true;
pacData.color = 'white';
} else {
pacData.indent = Math.floor((pacIndex - 0x10) / 2) * 4;
}
return pacData; // Note that row has zero offset. The spec uses 1.
}
/**
* Parse characters.
* @returns An array with 1 to 2 codes corresponding to chars, if found. null otherwise.
*/
parseChars(a, b) {
let channelNr;
let charCodes = null;
let charCode1 = null;
if (a >= 0x19) {
channelNr = 2;
charCode1 = a - 8;
} else {
channelNr = 1;
charCode1 = a;
}
if (charCode1 >= 0x11 && charCode1 <= 0x13) {
// Special character
let oneCode;
if (charCode1 === 0x11) {
oneCode = b + 0x50;
} else if (charCode1 === 0x12) {
oneCode = b + 0x70;
} else {
oneCode = b + 0x90;
}
this.logger.log(2, "Special char '" + getCharForByte(oneCode) + "' in channel " + channelNr);
charCodes = [oneCode];
} else if (a >= 0x20 && a <= 0x7f) {
charCodes = b === 0 ? [a] : [a, b];
}
if (charCodes) {
const hexCodes = numArrayToHexArray(charCodes);
this.logger.log(3, 'Char codes = ' + hexCodes.join(','));
setLastCmd(a, b, this.cmdHistory);
}
return charCodes;
}
/**
* Parse extended background attributes as well as new foreground color black.
* @returns True if background attributes are found
*/
parseBackgroundAttributes(a, b) {
const case1 = (a === 0x10 || a === 0x18) && b >= 0x20 && b <= 0x2f;
const case2 = (a === 0x17 || a === 0x1f) && b >= 0x2d && b <= 0x2f;
if (!(case1 || case2)) {
return false;
}
let index;
const bkgData = {};
if (a === 0x10 || a === 0x18) {
index = Math.floor((b - 0x20) / 2);
bkgData.background = backgroundColors[index];
if (b % 2 === 1) {
bkgData.background = bkgData.background + '_semi';
}
} else if (b === 0x2d) {
bkgData.background = 'transparent';
} else {
bkgData.foreground = 'black';
if (b === 0x2f) {
bkgData.underline = true;
}
}
const chNr = a <= 0x17 ? 1 : 2;
const channel = this.channels[chNr];
channel.setBkgData(bkgData);
setLastCmd(a, b, this.cmdHistory);
return true;
}
/**
* Reset state of parser and its channels.
*/
reset() {
for (let i = 0; i < Object.keys(this.channels).length; i++) {
const channel = this.channels[i];
if (channel) {
channel.reset();
}
}
this.cmdHistory = createCmdHistory();
}
/**
* Trigger the generation of a cue, and the start of a new one if displayScreens are not empty.
*/
cueSplitAtTime(t) {
for (let i = 0; i < this.channels.length; i++) {
const channel = this.channels[i];
if (channel) {
channel.cueSplitAtTime(t);
}
}
}
}
function setLastCmd(a, b, cmdHistory) {
cmdHistory.a = a;
cmdHistory.b = b;
}
function hasCmdRepeated(a, b, cmdHistory) {
return cmdHistory.a === a && cmdHistory.b === b;
}
function createCmdHistory() {
return {
a: null,
b: null
};
}
class OutputFilter {
constructor(timelineController, trackName) {
this.timelineController = void 0;
this.cueRanges = [];
this.trackName = void 0;
this.startTime = null;
this.endTime = null;
this.screen = null;
this.timelineController = timelineController;
this.trackName = trackName;
}
dispatchCue() {
if (this.startTime === null) {
return;
}
this.timelineController.addCues(this.trackName, this.startTime, this.endTime, this.screen, this.cueRanges);
this.startTime = null;
}
newCue(startTime, endTime, screen) {
if (this.startTime === null || this.startTime > startTime) {
this.startTime = startTime;
}
this.endTime = endTime;
this.screen = screen;
this.timelineController.createCaptionsTrack(this.trackName);
}
reset() {
this.cueRanges = [];
this.startTime = null;
}
}
/**
* Copyright 2013 vtt.js Contributors
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var VTTCue = (function () {
if (typeof self !== 'undefined' && self.VTTCue) {
return self.VTTCue;
}
const AllowedDirections = ['', 'lr', 'rl'];
const AllowedAlignments = ['start', 'middle', 'end', 'left', 'right'];
function isAllowedValue(allowed, value) {
if (typeof value !== 'string') {
return false;
}
// necessary for assuring the generic conforms to the Array interface
if (!Array.isArray(allowed)) {
return false;
}
// reset the type so that the next narrowing works well
const lcValue = value.toLowerCase();
// use the allow list to narrow the type to a specific subset of strings
if (~allowed.indexOf(lcValue)) {
return lcValue;
}
return false;
}
function findDirectionSetting(value) {
return isAllowedValue(AllowedDirections, value);
}
function findAlignSetting(value) {
return isAllowedValue(AllowedAlignments, value);
}
function extend(obj, ...rest) {
let i = 1;
for (; i < arguments.length; i++) {
const cobj = arguments[i];
for (const p in cobj) {
obj[p] = cobj[p];
}
}
return obj;
}
function VTTCue(startTime, endTime, text) {
const cue = this;
const baseObj = {
enumerable: true
};
/**
* Shim implementation specific properties. These properties are not in
* the spec.
*/
// Lets us know when the VTTCue's data has changed in such a way that we need
// to recompute its display state. This lets us compute its display state
// lazily.
cue.hasBeenReset = false;
/**
* VTTCue and TextTrackCue properties
* http://dev.w3.org/html5/webvtt/#vttcue-interface
*/
let _id = '';
let _pauseOnExit = false;
let _startTime = startTime;
let _endTime = endTime;
let _text = text;
let _region = null;
let _vertical = '';
let _snapToLines = true;
let _line = 'auto';
let _lineAlign = 'start';
let _position = 50;
let _positionAlign = 'middle';
let _size = 50;
let _align = 'middle';
Object.defineProperty(cue, 'id', extend({}, baseObj, {
get: function () {
return _id;
},
set: function (value) {
_id = '' + value;
}
}));
Object.defineProperty(cue, 'pauseOnExit', extend({}, baseObj, {
get: function () {
return _pauseOnExit;
},
set: function (value) {
_pauseOnExit = !!value;
}
}));
Object.defineProperty(cue, 'startTime', extend({}, baseObj, {
get: function () {
return _startTime;
},
set: function (value) {
if (typeof value !== 'number') {
throw new TypeError('Start time must be set to a number.');
}
_startTime = value;
this.hasBeenReset = true;
}
}));
Object.defineProperty(cue, 'endTime', extend({}, baseObj, {
get: function () {
return _endTime;
},
set: function (value) {
if (typeof value !== 'number') {
throw new TypeError('End time must be set to a number.');
}
_endTime = value;
this.hasBeenReset = true;
}
}));
Object.defineProperty(cue, 'text', extend({}, baseObj, {
get: function () {
return _text;
},
set: function (value) {
_text = '' + value;
this.hasBeenReset = true;
}
}));
// todo: implement VTTRegion polyfill?
Object.defineProperty(cue, 'region', extend({}, baseObj, {
get: function () {
return _region;
},
set: function (value) {
_region = value;
this.hasBeenReset = true;
}
}));
Object.defineProperty(cue, 'vertical', extend({}, baseObj, {
get: function () {
return _vertical;
},
set: function (value) {
const setting = findDirectionSetting(value);
// Have to check for false because the setting an be an empty string.
if (setting === false) {
throw new SyntaxError('An invalid or illegal string was specified.');
}
_vertical = setting;
this.hasBeenReset = true;
}
}));
Object.defineProperty(cue, 'snapToLines', extend({}, baseObj, {
get: function () {
return _snapToLines;
},
set: function (value) {
_snapToLines = !!value;
this.hasBeenReset = true;
}
}));
Object.defineProperty(cue, 'line', extend({}, baseObj, {
get: function () {
return _line;
},
set: function (value) {
if (typeof value !== 'number' && value !== 'auto') {
throw new SyntaxError('An invalid number or illegal string was specified.');
}
_line = value;
this.hasBeenReset = true;
}
}));
Object.defineProperty(cue, 'lineAlign', extend({}, baseObj, {
get: function () {
return _lineAlign;
},
set: function (value) {
const setting = findAlignSetting(value);
if (!setting) {
throw new SyntaxError('An invalid or illegal string was specified.');
}
_lineAlign = setting;
this.hasBeenReset = true;
}
}));
Object.defineProperty(cue, 'position', extend({}, baseObj, {
get: function () {
return _position;
},
set: function (value) {
if (value < 0 || value > 100) {
throw new Error('Position must be between 0 and 100.');
}
_position = value;
this.hasBeenReset = true;
}
}));
Object.defineProperty(cue, 'positionAlign', extend({}, baseObj, {
get: function () {
return _positionAlign;
},
set: function (value) {
const setting = findAlignSetting(value);
if (!setting) {
throw new SyntaxError('An invalid or illegal string was specified.');
}
_positionAlign = setting;
this.hasBeenReset = true;
}
}));
Object.defineProperty(cue, 'size', extend({}, baseObj, {
get: function () {
return _size;
},
set: function (value) {
if (value < 0 || value > 100) {
throw new Error('Size must be between 0 and 100.');
}
_size = value;
this.hasBeenReset = true;
}
}));
Object.defineProperty(cue, 'align', extend({}, baseObj, {
get: function () {
return _align;
},
set: function (value) {
const setting = findAlignSetting(value);
if (!setting) {
throw new SyntaxError('An invalid or illegal string was specified.');
}
_align = setting;
this.hasBeenReset = true;
}
}));
/**
* Other