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