Skip to content

Commit

Permalink
feat: Add 'downloadcompleted' event (#7609)
Browse files Browse the repository at this point in the history
Closes #7608
  • Loading branch information
vlazh authored Nov 18, 2024
1 parent 0fc5316 commit 13186bd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
25 changes: 22 additions & 3 deletions lib/net/networking_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ shaka.net.NetworkingEngine = class extends shaka.util.FakeEventTarget {
* of whether the switching is allowed.
* @param {shaka.net.NetworkingEngine.OnHeadersReceived=} onHeadersReceived
* Called when the headers are received for a download.
* @param {shaka.net.NetworkingEngine.OnDownloadCompleted=}
* onDownloadCompleted Called when a download completed successfully.
* @param {shaka.net.NetworkingEngine.OnDownloadFailed=} onDownloadFailed
* Called when a download fails, for any reason.
* @param {shaka.net.NetworkingEngine.OnRequest=} onRequest
Expand All @@ -62,8 +64,8 @@ shaka.net.NetworkingEngine = class extends shaka.util.FakeEventTarget {
* @param {shaka.net.NetworkingEngine.OnResponse=} onResponse
* Called when receive the response
*/
constructor(onProgressUpdated, onHeadersReceived, onDownloadFailed,
onRequest, onRetry, onResponse) {
constructor(onProgressUpdated, onHeadersReceived, onDownloadCompleted,
onDownloadFailed, onRequest, onRetry, onResponse) {
super();

/** @private {boolean} */
Expand All @@ -84,6 +86,9 @@ shaka.net.NetworkingEngine = class extends shaka.util.FakeEventTarget {
/** @private {?shaka.net.NetworkingEngine.OnHeadersReceived} */
this.onHeadersReceived_ = onHeadersReceived || null;

/** @private {?shaka.net.NetworkingEngine.OnDownloadCompleted} */
this.onDownloadCompleted_ = onDownloadCompleted || null;

/** @private {?shaka.net.NetworkingEngine.OnDownloadFailed} */
this.onDownloadFailed_ = onDownloadFailed || null;

Expand Down Expand Up @@ -635,7 +640,9 @@ shaka.net.NetworkingEngine = class extends shaka.util.FakeEventTarget {
this.onHeadersReceived_(response.headers, request, type);
}
}

if (this.onDownloadCompleted_) {
this.onDownloadCompleted_(request, response);
}
return responseAndGotProgress;
}, (error) => {
if (connectionTimer) {
Expand Down Expand Up @@ -979,6 +986,18 @@ shaka.net.NetworkingEngine.OnHeadersReceived;
*/
shaka.net.NetworkingEngine.onProgressUpdated;

/**
* @typedef {function(
* !shaka.extern.Request,
* !shaka.extern.Response)}
*
* @description
* A callback function that notifies the player when a download completed
* successfully.
* @export
*/
shaka.net.NetworkingEngine.OnDownloadCompleted;

/**
* @typedef {function(
* !shaka.extern.Request,
Expand Down
13 changes: 11 additions & 2 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -3469,6 +3469,15 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
}
});
};
/** @type {shaka.net.NetworkingEngine.OnDownloadCompleted} */
const onDownloadCompleted_ = (request, response) => {
// Release a 'downloadcompleted' event.
const name = shaka.util.FakeEvent.EventName.DownloadCompleted;
const data = new Map()
.set('request', request)
.set('response', response);
dispatchEvent(shaka.Player.makeEvent_(name, data));
};
/** @type {shaka.net.NetworkingEngine.OnDownloadFailed} */
const onDownloadFailed_ = (request, error, httpResponseCode, aborted) => {
// Release a 'downloadfailed' event.
Expand Down Expand Up @@ -3510,8 +3519,8 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
};

return new shaka.net.NetworkingEngine(
onProgressUpdated_, onHeadersReceived_, onDownloadFailed_, onRequest_,
onRetry_, onResponse_);
onProgressUpdated_, onHeadersReceived_, onDownloadCompleted_,
onDownloadFailed_, onRequest_, onRetry_, onResponse_);
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/util/fake_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ shaka.util.FakeEvent.EventName = {
AudioTrackChanged: 'audiotrackchanged',
Buffering: 'buffering',
Complete: 'complete',
DownloadCompleted: 'downloadcompleted',
DownloadFailed: 'downloadfailed',
DownloadHeadersReceived: 'downloadheadersreceived',
DrmSessionUpdate: 'drmsessionupdate',
Expand Down
2 changes: 1 addition & 1 deletion test/util/cmcd_manager_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe('CmcdManager', () => {
cmcd.applyData(type, request, context);
}

return new NetworkingEngine(undefined, undefined, undefined,
return new NetworkingEngine(undefined, undefined, undefined, undefined,
onRequest);
}

Expand Down

0 comments on commit 13186bd

Please sign in to comment.