Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor EventBus #3372

Merged
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/Debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ function Debug(config) {
}

// send log event regardless of log level
eventBus.trigger(Events.LOG, {message: message, level: level});
eventBus.trigger(Events.LOG, { message: message, level: level });
}

instance = {
Expand Down
17 changes: 15 additions & 2 deletions src/core/EventBus.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ function EventBus() {
priority: priority
};

if (scope && scope.getStreamId) {
handler.streamId = scope.getStreamId();
}
if (scope && scope.getType) {
handler.mediaType = scope.getType();
}

const inserted = handlers[type].some((item , idx) => {
if (item && priority > item.priority ) {
handlers[type].splice(idx, 0, handler);
Expand All @@ -75,7 +82,7 @@ function EventBus() {
handlers[type][idx] = null;
}

function trigger(type, payload) {
function trigger(type, payload = {}, filters = {}) {
if (!type || !handlers[type]) return;

payload = payload || {};
Expand All @@ -85,7 +92,13 @@ function EventBus() {
payload.type = type;

handlers[type] = handlers[type].filter((item) => item);
handlers[type].forEach( handler => handler && handler.callback.call(handler.scope, payload) );
const eventHandlers = handlers[type].filter(item => {
if (filters.streamId && item.streamId && item.streamId !== filters.streamId) return false;
if (filters.mediaType && item.mediaType && item.mediaType !== filters.mediaType) return false;
return true;
});

eventHandlers.forEach(handler => handler && handler.callback.call(handler.scope, payload));
}

function getHandlerIdx(type, listener, scope) {
Expand Down
39 changes: 24 additions & 15 deletions src/dash/DashHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ function DashHandler(config) {
segmentsController.initialize(isDynamic);
}

function getStreamId() {
return streamInfo.id;
}

function getType() {
return type;
}
Expand Down Expand Up @@ -183,25 +187,23 @@ function DashHandler(config) {
dashMetrics.updateManifestUpdateInfo({presentationStartTime: liveEdge});
}

function onRepresentationUpdateStarted(eventObj) {
if (eventObj.sender.getType() !== getType()) return;

processRepresentation(eventObj.representation);
function onRepresentationUpdateStarted(e) {
processRepresentation(e.representation);
}

function processRepresentation(voRepresentation) {
const hasInitialization = voRepresentation.hasInitialization();
const hasSegments = voRepresentation.hasSegments();

//if representation has initialization and segments information, REPRESENTATION_UPDATE_COMPLETED can be triggered immediately
//otherwise, it means that a request has to be made to get initialization and/or segments informations
// If representation has initialization and segments information, REPRESENTATION_UPDATE_COMPLETED can be triggered immediately
// otherwise, it means that a request has to be made to get initialization and/or segments informations
if (hasInitialization && hasSegments) {
eventBus.trigger(events.REPRESENTATION_UPDATE_COMPLETED, {
sender: instance,
representation: voRepresentation
});
eventBus.trigger(events.REPRESENTATION_UPDATE_COMPLETED,
{ representation: voRepresentation },
{ streamId: streamInfo.id, mediaType: type }
);
} else {
segmentsController.update(voRepresentation, getType(), selectedMimeType, hasInitialization, hasSegments);
segmentsController.update(voRepresentation, selectedMimeType, hasInitialization, hasSegments);
}
}

Expand Down Expand Up @@ -360,11 +362,14 @@ function DashHandler(config) {
const representation = e.representation;
if (!representation.segments) return;

eventBus.trigger(events.REPRESENTATION_UPDATE_COMPLETED, {sender: this, representation: representation});
eventBus.trigger(events.REPRESENTATION_UPDATE_COMPLETED,
{ representation: representation },
{ streamId: streamInfo.id, mediaType: type }
);
}

function onSegmentsLoaded(e) {
if (e.error || (getType() !== e.mediaType)) return;
if (e.error) return;

const fragments = e.segments;
const representation = e.representation;
Expand Down Expand Up @@ -417,7 +422,10 @@ function DashHandler(config) {
return;
}

eventBus.trigger(events.REPRESENTATION_UPDATE_COMPLETED, {sender: this, representation: representation});
eventBus.trigger(events.REPRESENTATION_UPDATE_COMPLETED,
{ representation: representation },
{ streamId: streamInfo.id, mediaType: type }
);
}

function onDynamicStreamCompleted() {
Expand All @@ -427,7 +435,8 @@ function DashHandler(config) {

instance = {
initialize: initialize,
getType: getType, //need to be public in order to be used by logger
getStreamId: getStreamId,
getType: getType,
getStreamInfo: getStreamInfo,
getInitRequest: getInitRequest,
getRequestForSegment: getRequestForSegment,
Expand Down
53 changes: 31 additions & 22 deletions src/dash/SegmentBaseLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function SegmentBaseLoader() {
}
}

function loadInitialization(representation, loadingInfo) {
function loadInitialization(streamId, mediaType, representation, loadingInfo) {
checkConfig();
let initRange = null;
const baseUrl = representation ? baseURLController.resolve(representation.path) : null;
Expand All @@ -151,7 +151,7 @@ function SegmentBaseLoader() {
searching: false,
bytesLoaded: 0,
bytesToLoad: 1500,
mediaType: representation && representation.adaptation ? representation.adaptation.type : null
mediaType: mediaType
};

logger.debug('Start searching for initialization.');
Expand All @@ -166,23 +166,29 @@ function SegmentBaseLoader() {
representation.range = initRange;
// note that we don't explicitly set rep.initialization as this
// will be computed when all BaseURLs are resolved later
eventBus.trigger(events.INITIALIZATION_LOADED, {representation: representation});
eventBus.trigger(events.INITIALIZATION_LOADED,
{ representation: representation },
{ streamId: streamId, mediaType: mediaType }
);
} else {
info.range.end = info.bytesLoaded + info.bytesToLoad;
loadInitialization(representation, info);
loadInitialization(streamId, mediaType, representation, info);
}
};

const onerror = function () {
eventBus.trigger(events.INITIALIZATION_LOADED, {representation: representation});
eventBus.trigger(events.INITIALIZATION_LOADED,
{ representation: representation },
{ streamId: streamId, mediaType: mediaType }
);
};

urlLoader.load({request: request, success: onload, error: onerror});

logger.debug('Perform init search: ' + info.url);
}

function loadSegments(representation, type, range, callback, loadingInfo) {
function loadSegments(streamId, mediaType, representation, range, callback, loadingInfo) {
checkConfig();
if (range && (range.start === undefined || range.end === undefined)) {
const parts = range ? range.toString().split('-') : null;
Expand All @@ -201,7 +207,7 @@ function SegmentBaseLoader() {
searching: !hasRange,
bytesLoaded: loadingInfo ? loadingInfo.bytesLoaded : 0,
bytesToLoad: 1500,
mediaType: representation && representation.adaptation ? representation.adaptation.type : null
mediaType: mediaType
};

const request = getFragmentRequest(info);
Expand All @@ -220,7 +226,7 @@ function SegmentBaseLoader() {
info.range.end = info.range.start + (sidx.size || extraBytes);
} else if (loadedLength < info.bytesLoaded) {
// if we have reached a search limit or if we have reached the end of the file we have to stop trying to find sidx
callback(null, representation, type);
callback(streamId, mediaType, null, representation);
return;
} else {
const lastBox = isoFile.getLastBox();
Expand All @@ -232,7 +238,7 @@ function SegmentBaseLoader() {
info.range.end += extraBytes;
}
}
loadSegments(representation, type, info.range, callback, info);
loadSegments(streamId, mediaType, representation, info.range, callback, info);
} else {
const ref = sidx.references;
let loadMultiSidx,
Expand All @@ -250,7 +256,7 @@ function SegmentBaseLoader() {
let segs = [];
let count = 0;
let offset = (sidx.offset || info.range.start) + sidx.size;
const tmpCallback = function (result) {
const tmpCallback = function (streamId, mediaType, result) {
if (result) {
segs = segs.concat(result);
count++;
Expand All @@ -260,10 +266,10 @@ function SegmentBaseLoader() {
segs.sort(function (a, b) {
return a.startTime - b.startTime < 0 ? -1 : 0;
});
callback(segs, representation, type);
callback(streamId, mediaType, segs, representation);
}
} else {
callback(null, representation, type);
callback(streamId, mediaType, null, representation);
}
};

Expand All @@ -272,19 +278,19 @@ function SegmentBaseLoader() {
se = offset + ref[j].referenced_size - 1;
offset = offset + ref[j].referenced_size;
r = {start: ss, end: se};
loadSegments(representation, null, r, tmpCallback, info);
loadSegments(streamId, mediaType, representation, r, tmpCallback, info);
}

} else {
logger.debug('Parsing segments from SIDX. representation ' + representation.adaptation.type + ' - id: ' + representation.id + ' for range : ' + info.range.start + ' - ' + info.range.end);
logger.debug('Parsing segments from SIDX. representation ' + mediaType + ' - id: ' + representation.id + ' for range : ' + info.range.start + ' - ' + info.range.end);
segments = getSegmentsForSidx(sidx, info);
callback(segments, representation, type);
callback(streamId, mediaType, segments, representation);
}
}
};

const onerror = function () {
callback(null, representation, type);
callback(streamId, mediaType, null, representation);
};

urlLoader.load({request: request, success: onload, error: onerror});
Expand Down Expand Up @@ -340,12 +346,15 @@ function SegmentBaseLoader() {
return request;
}

function onLoaded(segments, representation, type) {
if (segments) {
eventBus.trigger(events.SEGMENTS_LOADED, {segments: segments, representation: representation, mediaType: type});
} else {
eventBus.trigger(events.SEGMENTS_LOADED, {segments: null, representation: representation, mediaType: type, error: new DashJSError(errors.SEGMENT_BASE_LOADER_ERROR_CODE, errors.SEGMENT_BASE_LOADER_ERROR_MESSAGE)});
}
function onLoaded(streamId, mediaType, segments, representation) {
eventBus.trigger(events.SEGMENTS_LOADED,
{
segments: segments,
representation: representation,
error: segments ? undefined : new DashJSError(errors.SEGMENT_BASE_LOADER_ERROR_CODE, errors.SEGMENT_BASE_LOADER_ERROR_MESSAGE)
},
{ streamId: streamId, mediaType: mediaType }
);
}

instance = {
Expand Down
46 changes: 21 additions & 25 deletions src/dash/WebmSegmentBaseLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ function WebmSegmentBaseLoader() {
}
}

function loadInitialization(representation, loadingInfo) {
function loadInitialization(streamId, mediaType, representation, loadingInfo) {
checkConfig();
let request = null;
let baseUrl = representation ? baseURLController.resolve(representation.path) : null;
Expand All @@ -317,7 +317,7 @@ function WebmSegmentBaseLoader() {
request: request,
url: baseUrl ? baseUrl.url : undefined,
init: true,
mediaType: representation && representation.adaptation ? representation.adaptation.type : null
mediaType: mediaType
};

logger.info('Start loading initialization.');
Expand All @@ -327,15 +327,17 @@ function WebmSegmentBaseLoader() {
const onload = function () {
// note that we don't explicitly set rep.initialization as this
// will be computed when all BaseURLs are resolved later
eventBus.trigger(events.INITIALIZATION_LOADED, {
representation: representation
});
eventBus.trigger(events.INITIALIZATION_LOADED,
{ representation: representation },
{ streamId: streamId, mediaType: mediaType }
);
};

const onloadend = function () {
eventBus.trigger(events.INITIALIZATION_LOADED, {
representation: representation
});
eventBus.trigger(events.INITIALIZATION_LOADED,
{ representation: representation },
{ streamId: streamId, mediaType: mediaType }
);
};

urlLoader.load({
Expand All @@ -347,7 +349,7 @@ function WebmSegmentBaseLoader() {
logger.debug('Perform init load: ' + info.url);
}

function loadSegments(representation, type, theRange, callback) {
function loadSegments(streamId, mediaType, representation, theRange, callback) {
checkConfig();
let request = null;
let baseUrl = representation ? baseURLController.resolve(representation.path) : null;
Expand All @@ -363,7 +365,7 @@ function WebmSegmentBaseLoader() {
request: request,
url: media,
init: false,
mediaType: representation && representation.adaptation ? representation.adaptation.type : null
mediaType: mediaType
};

callback = !callback ? onLoaded : callback;
Expand All @@ -376,12 +378,12 @@ function WebmSegmentBaseLoader() {

const onload = function (response) {
parseEbmlHeader(response, media, theRange, function (segments) {
callback(segments, representation, type);
callback(streamId, mediaType, segments, representation);
});
};

const onloadend = function () {
callback(null, representation, type);
callback(streamId, mediaType, null, representation);
};

urlLoader.load({
Expand All @@ -391,21 +393,15 @@ function WebmSegmentBaseLoader() {
});
}

function onLoaded(segments, representation, type) {
if (segments) {
eventBus.trigger(events.SEGMENTS_LOADED, {
function onLoaded(streamId, mediaType, segments, representation) {
eventBus.trigger(events.SEGMENTS_LOADED,
{
segments: segments,
representation: representation,
mediaType: type
});
} else {
eventBus.trigger(events.SEGMENTS_LOADED, {
segments: null,
representation: representation,
mediaType: type,
error: new DashJSError(errors.SEGMENT_BASE_LOADER_ERROR_CODE, errors.SEGMENT_BASE_LOADER_ERROR_MESSAGE)
});
}
error: segments ? undefined : new DashJSError(errors.SEGMENT_BASE_LOADER_ERROR_CODE, errors.SEGMENT_BASE_LOADER_ERROR_MESSAGE)
},
{ streamId: streamId, mediaType: mediaType }
);
}

function getFragmentRequest(info) {
Expand Down