dashJS目录结构解析
目录结构
DashJs播放器控制栏类=>contrib/akamai/controllbar/ControlBar.js
/**
* @module ControlBar
* @param {object=} dashjsMediaPlayer - dashjs reference
* @param {boolean=} displayUTCTimeCodes - true if time is displayed in UTC format, false otherwise
*/
var ControlBar = function (dashjsMediaPlayer, displayUTCTimeCodes) {
// to do
}
提供DashJs对播放器的事件处理如:播放,暂停,全屏,声音控制等
DashJS源码上启动服务自带项目 samples/index.html
git上获取源码启动服务后展示的项目源码地址
DashJs核心类 src/core
提供DashJs核心处理类,包括错误类,通信类,日志类,工具类,版本,Debug等
核心类之错误类 src/core/errors/Errors.js 和 src/core/errors/ErrorsBase.js
Errors.js 提供全局错误CODE和MESSAGE
ErrorsBase.js Errors.js基类
//ErrorsBase.js
/**
* @class
* @ignore
*/
class ErrorsBase {
extend (errors, config) {
if (!errors) return;
let override = config ? config.override : false;
let publicOnly = config ? config.publicOnly : false;
for (const err in errors) {
if (!errors.hasOwnProperty(err) || (this[err] && !override)) continue;
if (publicOnly && errors[err].indexOf('public_') === -1) continue;
this[err] = errors[err];
}
}
}
export default ErrorsBase;
核心类之事件类 src/core/events/coreEvents.js,src/core/events/EventsBase.js和 src/core/events/Events.js
coreEvents.js 事件常量定义
EventsBase.js 事件基类
Events.js 继承于coreEvents.js的类 变相给coreEvents.js类改名
核心类之Debug日志类 src/core/Debug.js
定义日志级别,处理打印对应的日志
function setup() {
showLogTimestamp = true;
showCalleeName = true;
startTime = new Date().getTime();
if (typeof window !== 'undefined' && window.console) {
logFn[LOG_LEVEL_FATAL] = getLogFn(window.console.error);
logFn[LOG_LEVEL_ERROR] = getLogFn(window.console.error);
logFn[LOG_LEVEL_WARNING] = getLogFn(window.console.warn);
logFn[LOG_LEVEL_INFO] = getLogFn(window.console.info);
logFn[LOG_LEVEL_DEBUG] = getLogFn(window.console.debug);
}
}
核心类之EventBus事件通信类 src/core/EventBus.js
function EventBus() {
function on(type, listener, scope, options = {}){
//to do
}
function off(type, listener, scope){
//to do
}
function trigger(type, payload = {}, filters = {}){
//to do
}
function getHandlerIdx(type, listener, scope) {
//to do
}
function reset() {
//to do
}
}
核心类之FactoryMaker工厂类 src/core/FactoryMaker.js
采用工厂设计模式处理全局类的注册
function getSingletonFactory(classConstructor) {
let factory = getFactoryByName(
classConstructor.__dashjs_factory_name,
singletonFactories
);
if (!factory) {
factory = function (context) {
let instance;
if (context === undefined) {
context = {};
}
return {
getInstance: function () {
// If we don't have an instance yet check for one on the context
if (!instance) {
instance = getSingletonInstance(
context,
classConstructor.__dashjs_factory_name
);
}
// If there's no instance on the context then create one
if (!instance) {
instance = merge(
classConstructor,
context,
arguments
);
singletonContexts.push({
name: classConstructor.__dashjs_factory_name,
context: context,
instance: instance,
});
}
return instance;
},
};
};
singletonFactories[classConstructor.__dashjs_factory_name] =
factory; // store factory
}
return factory;
}
核心类之Settings设置类 src/core/Settings.js
提供DashJs的默认设置项,可以调用update更新,reset还原,get获取
//to do
instance = {
get: get,
update: update,
reset: reset
};
return instance;
核心类之Utils工具类 src/core/Utils.js
提供DashJs常用函数 mixin ,clone ,addAditionalQueryParameterToUrl,parseHttpHeaders,generateUuid, generateHashCode,getRelativeUrl
class Utils {
static mixin(dest, source, copy) {
let s;
let empty = {};
if (dest) {
for (let name in source) {
if (source.hasOwnProperty(name)) {
s = source[name];
if (!(name in dest) || (dest[name] !== s && (!(name in empty) || empty[name] !== s))) {
if (typeof dest[name] === 'object' && dest[name] !== null) {
dest[name] = Utils.mixin(dest[name], s, copy);
} else {
dest[name] = copy(s);
}
}
}
}
}
return dest;
}
static clone(src) {
if (!src || typeof src !== 'object') {
return src; // anything
}
let r;
if (src instanceof Array) {
// array
r = [];
for (let i = 0, l = src.length; i < l; ++i) {
if (i in src) {
r.push(Utils.clone(src[i]));
}
}
} else {
r = {};
}
return Utils.mixin(r, src, Utils.clone);
}
static addAditionalQueryParameterToUrl(url, params) {
try {
if (!params || params.length === 0) {
return url;
}
let modifiedUrl = new URL(url);
params.forEach((param) => {
if (param.key && param.value) {
modifiedUrl.searchParams.set(param.key, param.value);
}
});
return modifiedUrl.href;
} catch (e) {
return url;
}
}
static parseHttpHeaders(headerStr) {
let headers = {};
if (!headerStr) {
return headers;
}
// Trim headerStr to fix a MS Edge bug with xhr.getAllResponseHeaders method
// which send a string starting with a "\n" character
let headerPairs = headerStr.trim().split('\u000d\u000a');
for (let i = 0, ilen = headerPairs.length; i < ilen; i++) {
let headerPair = headerPairs[i];
let index = headerPair.indexOf('\u003a\u0020');
if (index > 0) {
headers[headerPair.substring(0, index)] = headerPair.substring(index + 2);
}
}
return headers;
}
static generateUuid() {
let dt = new Date().getTime();
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = (dt + Math.random() * 16) % 16 | 0;
dt = Math.floor(dt / 16);
return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
return uuid;
}
static generateHashCode(string) {
let hash = 0;
if (string.length === 0) {
return hash;
}
for (let i = 0; i < string.length; i++) {
const chr = string.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0;
}
return hash;
}
/**
* Compares both urls and returns a relative url (target relative to original)
* @param {string} original
* @param {string} target
* @return {string|*}
*/
static getRelativeUrl(originalUrl, targetUrl) {
try {
const original = new URL(originalUrl);
const target = new URL(targetUrl);
// Unify the protocol to compare the origins
original.protocol = target.protocol;
if (original.origin !== target.origin) {
return targetUrl;
}
// Use the relative path implementation of the path library. We need to cut off the actual filename in the end to get the relative path
let relativePath = path.relative(original.pathname.substr(0, original.pathname.lastIndexOf('/')), target.pathname.substr(0, target.pathname.lastIndexOf('/')));
// In case the relative path is empty (both path are equal) return the filename only. Otherwise add a slash in front of the filename
const startIndexOffset = relativePath.length === 0 ? 1 : 0;
relativePath += target.pathname.substr(target.pathname.lastIndexOf('/') + startIndexOffset, target.pathname.length - 1);
// Build the other candidate, e.g. the 'host relative' path that starts with "/", and return the shortest of the two candidates.
if (target.pathname.length < relativePath.length) {
return target.pathname;
}
return relativePath;
} catch (e) {
return targetUrl
}
}
}
核心类之Version版本类 src/core/Version.js
返回当前DashJS版本信息
const VERSION = '__VERSION__';
export function getVersionString() {
return VERSION;
}