diff --git a/build/types/core b/build/types/core index 4031b533a2..20808d172c 100644 --- a/build/types/core +++ b/build/types/core @@ -46,6 +46,7 @@ +../../lib/net/backoff.js +../../lib/net/networking_engine.js ++../../lib/net/networking_utils.js +../../lib/player.js diff --git a/lib/media/manifest_parser.js b/lib/media/manifest_parser.js index caf339de90..264d4950dc 100644 --- a/lib/media/manifest_parser.js +++ b/lib/media/manifest_parser.js @@ -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'); @@ -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]; @@ -180,27 +182,6 @@ shaka.media.ManifestParser = class { } - /** - * @param {string} uri - * @param {!shaka.net.NetworkingEngine} netEngine - * @param {shaka.extern.RetryParameters} retryParams - * @return {!Promise.} - */ - 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} diff --git a/lib/net/networking_utils.js b/lib/net/networking_utils.js new file mode 100644 index 0000000000..6108c8e5f3 --- /dev/null +++ b/lib/net/networking_utils.js @@ -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.} + */ + 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() : ''; + } +}; diff --git a/lib/player.js b/lib/player.js index f422b78c50..faf5ba5f02 100644 --- a/lib/player.js +++ b/lib/player.js @@ -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'); @@ -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) {} diff --git a/test/media/manifest_parser_unit.js b/test/net/networking_utils_unit.js similarity index 92% rename from test/media/manifest_parser_unit.js rename to test/net/networking_utils_unit.js index 3f9bb1c2a1..38d18cb51e 100644 --- a/test/media/manifest_parser_unit.js +++ b/test/net/networking_utils_unit.js @@ -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');