Skip to content

Commit

Permalink
feat(net): Add shaka.net.NetworkingUtils (#5756)
Browse files Browse the repository at this point in the history
  • Loading branch information
avelad authored Oct 10, 2023
1 parent 3096378 commit be96fd0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 25 deletions.
1 change: 1 addition & 0 deletions build/types/core
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

+../../lib/net/backoff.js
+../../lib/net/networking_engine.js
+../../lib/net/networking_utils.js

+../../lib/player.js

Expand Down
25 changes: 3 additions & 22 deletions lib/media/manifest_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ goog.provide('shaka.media.ManifestParser');
goog.require('goog.Uri');
goog.require('shaka.log');
goog.require('shaka.net.NetworkingEngine');
goog.require('shaka.net.NetworkingUtils');
goog.require('shaka.util.Error');
goog.require('shaka.util.Platform');

Expand Down Expand Up @@ -159,7 +160,8 @@ shaka.media.ManifestParser = class {
}

if (!mimeType) {
mimeType = await ManifestParser.getMimeType(uri, netEngine, retryParams);
mimeType = await shaka.net.NetworkingUtils.getMimeType(
uri, netEngine, retryParams);

if (mimeType) {
const factory = shaka.media.ManifestParser.parsersByMime[mimeType];
Expand All @@ -180,27 +182,6 @@ shaka.media.ManifestParser = class {
}


/**
* @param {string} uri
* @param {!shaka.net.NetworkingEngine} netEngine
* @param {shaka.extern.RetryParameters} retryParams
* @return {!Promise.<string>}
*/
static async getMimeType(uri, netEngine, retryParams) {
const type = shaka.net.NetworkingEngine.RequestType.MANIFEST;

const request = shaka.net.NetworkingEngine.makeRequest([uri], retryParams);
request.method = 'HEAD';

const response = await netEngine.request(type, request).promise;

// https://bit.ly/2K9s9kf says this header should always be available,
// but just to be safe:
const mimeType = response.headers['content-type'];
return mimeType ? mimeType.toLowerCase().split(';').shift() : '';
}


/**
* @param {string} uri
* @return {string}
Expand Down
35 changes: 35 additions & 0 deletions lib/net/networking_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

goog.provide('shaka.net.NetworkingUtils');

goog.require('shaka.net.NetworkingEngine');


/**
* @summary Networking utility functions.
*/
shaka.net.NetworkingUtils = class {
/**
* @param {string} uri
* @param {!shaka.net.NetworkingEngine} netEngine
* @param {shaka.extern.RetryParameters} retryParams
* @return {!Promise.<string>}
*/
static async getMimeType(uri, netEngine, retryParams) {
const type = shaka.net.NetworkingEngine.RequestType.MANIFEST;

const request = shaka.net.NetworkingEngine.makeRequest([uri], retryParams);
request.method = 'HEAD';

const response = await netEngine.request(type, request).promise;

// https://bit.ly/2K9s9kf says this header should always be available,
// but just to be safe:
const mimeType = response.headers['content-type'];
return mimeType ? mimeType.toLowerCase().split(';').shift() : '';
}
};
3 changes: 2 additions & 1 deletion lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ goog.require('shaka.media.SrcEqualsPlayhead');
goog.require('shaka.media.StreamingEngine');
goog.require('shaka.media.TimeRangesUtils');
goog.require('shaka.net.NetworkingEngine');
goog.require('shaka.net.NetworkingUtils');
goog.require('shaka.routing.Walker');
goog.require('shaka.text.SimpleTextDisplayer');
goog.require('shaka.text.TextEngine');
Expand Down Expand Up @@ -5151,7 +5152,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
goog.asserts.assert(
this.networkingEngine_, 'Need networking engine.');
// eslint-disable-next-line require-atomic-updates
mimeType = await shaka.media.ManifestParser.getMimeType(uri,
mimeType = await shaka.net.NetworkingUtils.getMimeType(uri,
this.networkingEngine_,
this.config_.streaming.retryParameters);
} catch (error) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
const testGetMimeType = async (expertedMimeType, contentType) => {
const netEngine = new shaka.test.FakeNetworkingEngine()
.setHeaders('dummy://foo', {'content-type': contentType});
const mimeType = await shaka.media.ManifestParser
const mimeType = await shaka.net.NetworkingUtils
.getMimeType('dummy://foo', netEngine,
shaka.net.NetworkingEngine.defaultRetryParameters());
expect(mimeType).toBe(expertedMimeType);
};

describe('ManifestParser', () => {
describe('NetworkingUtils', () => {
describe('getMimeType', () => {
it('test correct mimeType', () => {
testGetMimeType('application/dash+xml', 'application/dash+xml');
Expand Down

0 comments on commit be96fd0

Please sign in to comment.