Source: lib/net/networking_utils.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.net.NetworkingUtils');
  7. goog.require('goog.Uri');
  8. goog.require('shaka.net.NetworkingEngine');
  9. goog.require('shaka.util.Error');
  10. /**
  11. * @summary Networking utility functions.
  12. */
  13. shaka.net.NetworkingUtils = class {
  14. /**
  15. * @param {string} uri
  16. * @param {!shaka.net.NetworkingEngine} netEngine
  17. * @param {shaka.extern.RetryParameters} retryParams
  18. * @return {!Promise.<string>}
  19. */
  20. static async getMimeType(uri, netEngine, retryParams) {
  21. const extension = shaka.net.NetworkingUtils.getExtension_(uri);
  22. let mimeType =
  23. shaka.net.NetworkingUtils.EXTENSIONS_TO_MIME_TYPES_[extension];
  24. if (mimeType) {
  25. return mimeType;
  26. }
  27. const type = shaka.net.NetworkingEngine.RequestType.MANIFEST;
  28. const request = shaka.net.NetworkingEngine.makeRequest([uri], retryParams);
  29. try {
  30. request.method = 'HEAD';
  31. const response = await netEngine.request(type, request).promise;
  32. mimeType = response.headers['content-type'];
  33. } catch (error) {
  34. if (error &&
  35. (error.code == shaka.util.Error.Code.HTTP_ERROR ||
  36. error.code == shaka.util.Error.Code.BAD_HTTP_STATUS)) {
  37. request.method = 'GET';
  38. const response = await netEngine.request(type, request).promise;
  39. mimeType = response.headers['content-type'];
  40. }
  41. }
  42. // https://bit.ly/2K9s9kf says this header should always be available,
  43. // but just to be safe:
  44. return mimeType ? mimeType.toLowerCase().split(';').shift() : '';
  45. }
  46. /**
  47. * @param {string} uri
  48. * @return {string}
  49. * @private
  50. */
  51. static getExtension_(uri) {
  52. const uriObj = new goog.Uri(uri);
  53. const uriPieces = uriObj.getPath().split('/');
  54. const uriFilename = uriPieces.pop();
  55. const filenamePieces = uriFilename.split('.');
  56. // Only one piece means there is no extension.
  57. if (filenamePieces.length == 1) {
  58. return '';
  59. }
  60. return filenamePieces.pop().toLowerCase();
  61. }
  62. };
  63. /**
  64. * @const {!Object.<string, string>}
  65. * @private
  66. */
  67. shaka.net.NetworkingUtils.EXTENSIONS_TO_MIME_TYPES_ = {
  68. 'mp4': 'video/mp4',
  69. 'm4v': 'video/mp4',
  70. 'm4a': 'audio/mp4',
  71. 'webm': 'video/webm',
  72. 'weba': 'audio/webm',
  73. 'mkv': 'video/webm', // Chromium browsers supports it.
  74. 'ts': 'video/mp2t',
  75. 'ogv': 'video/ogg',
  76. 'ogg': 'audio/ogg',
  77. 'mpg': 'video/mpeg',
  78. 'mpeg': 'video/mpeg',
  79. 'm3u8': 'application/x-mpegurl',
  80. 'mpd': 'application/dash+xml',
  81. 'ism': 'application/vnd.ms-sstr+xml',
  82. 'mp3': 'audio/mpeg',
  83. 'aac': 'audio/aac',
  84. 'flac': 'audio/flac',
  85. 'wav': 'audio/wav',
  86. 'sbv': 'text/x-subviewer',
  87. 'srt': 'text/srt',
  88. 'vtt': 'text/vtt',
  89. 'webvtt': 'text/vtt',
  90. 'ttml': 'application/ttml+xml',
  91. 'lrc': 'application/x-subtitle-lrc',
  92. 'ssa': 'text/x-ssa',
  93. 'ass': 'text/x-ssa',
  94. };