Home Reference Source

src/is-supported.ts

  1. import { getMediaSource } from './utils/mediasource-helper';
  2. import { ExtendedSourceBuffer } from './types/buffer';
  3.  
  4. function getSourceBuffer(): typeof self.SourceBuffer {
  5. return self.SourceBuffer || (self as any).WebKitSourceBuffer;
  6. }
  7.  
  8. export function isSupported(): boolean {
  9. const mediaSource = getMediaSource();
  10. if (!mediaSource) {
  11. return false;
  12. }
  13. const sourceBuffer = getSourceBuffer();
  14. const isTypeSupported =
  15. mediaSource &&
  16. typeof mediaSource.isTypeSupported === 'function' &&
  17. mediaSource.isTypeSupported('video/mp4; codecs="avc1.42E01E,mp4a.40.2"');
  18.  
  19. // if SourceBuffer is exposed ensure its API is valid
  20. // safari and old version of Chrome doe not expose SourceBuffer globally so checking SourceBuffer.prototype is impossible
  21. const sourceBufferValidAPI =
  22. !sourceBuffer ||
  23. (sourceBuffer.prototype &&
  24. typeof sourceBuffer.prototype.appendBuffer === 'function' &&
  25. typeof sourceBuffer.prototype.remove === 'function');
  26. return !!isTypeSupported && !!sourceBufferValidAPI;
  27. }
  28.  
  29. export function changeTypeSupported(): boolean {
  30. const sourceBuffer = getSourceBuffer();
  31. return (
  32. typeof (sourceBuffer?.prototype as ExtendedSourceBuffer)?.changeType ===
  33. 'function'
  34. );
  35. }