Skip to content

Conversation

renoanzhou
Copy link

Problem

The current code uses the in operator to detect if MediaSource and ManagedMediaSource exist. This approach has a potential issue: if someone sets window.MediaSource = undefined, using the in operator would still return true because the property exists, even though its value is undefined.

Solution

Change the detection method from using the in operator to typeof ... === 'function' checks to ensure:

  1. More accurately verify if objects are available
  2. Confirm objects are actual callable constructor functions
  3. Avoid false positives when properties exist but have invalid values

Changes

-        this._useManagedMediaSource = ('ManagedMediaSource' in self) && !('MediaSource' in self);
+        this._useManagedMediaSource = (typeof self.ManagedMediaSource === 'function') && !(typeof self.MediaSource === 'function');

Context

This is particularly important when third-party players use patterns like:

window.MediaSource = window.MediaSource || window.WebKitMediaSource

which could create problems with the previous detection method if one of the values is undefined.

Testing

Verified this change works correctly in all supported browser environments and properly handles cases where MediaSource constructor is set to undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant