Skip to content

Commit

Permalink
Fix React Native createObjectURL polyfill incompatibility (#1845)
Browse files Browse the repository at this point in the history
React Native introduces its own polyfill for window.URL.createObjectURL which is not compatible with Shaka. Encapsulating a reference to the original function inside of the MediaSourceEngine circumvents this issue.

Closes #1842
  • Loading branch information
nitro404 authored and joeyparrish committed Mar 20, 2019
1 parent f1c11e4 commit 9c224c1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 11 additions & 1 deletion lib/media/media_source_engine.js
Expand Up @@ -93,6 +93,16 @@ shaka.media.MediaSourceEngine = function(
};


/**
* Internal reference to window.URL.createObjectURL function to avoid
* compatibility issues with other libraries and frameworks such as React
* Native. For use in unit tests only, not meant for external use.
*
* @type {function(?):string}
*/
shaka.media.MediaSourceEngine.createObjectURL = window.URL.createObjectURL;


/**
* Create a MediaSource object, attach it to the video element, and return it.
* Resolves the given promise when the MediaSource is ready.
Expand All @@ -107,7 +117,7 @@ shaka.media.MediaSourceEngine.prototype.createMediaSource = function(p) {

// Set up MediaSource on the video element.
this.eventManager_.listenOnce(mediaSource, 'sourceopen', p.resolve);
this.video_.src = window.URL.createObjectURL(mediaSource);
this.video_.src = shaka.media.MediaSourceEngine.createObjectURL(mediaSource);

return mediaSource;
};
Expand Down
8 changes: 5 additions & 3 deletions test/media/media_source_engine_unit.js
Expand Up @@ -161,7 +161,8 @@ describe('MediaSourceEngine', function() {
});

describe('constructor', function() {
const originalCreateObjectURL = window.URL.createObjectURL;
const originalCreateObjectURL =
shaka.media.MediaSourceEngine.createObjectURL;
const originalMediaSource = window.MediaSource;
/** @type {jasmine.Spy} */
let createObjectURLSpy;
Expand All @@ -176,7 +177,8 @@ describe('MediaSourceEngine', function() {

createObjectURLSpy = jasmine.createSpy('createObjectURL');
createObjectURLSpy.and.returnValue('blob:foo');
window.URL.createObjectURL = Util.spyFunc(createObjectURLSpy);
shaka.media.MediaSourceEngine.createObjectURL =
Util.spyFunc(createObjectURLSpy);

let mediaSourceSpy = jasmine.createSpy('MediaSource').and.callFake(() => {
return mockMediaSource;
Expand All @@ -187,7 +189,7 @@ describe('MediaSourceEngine', function() {
});

afterAll(function() {
window.URL.createObjectURL = originalCreateObjectURL;
shaka.media.MediaSourceEngine.createObjectURL = originalCreateObjectURL;
window.MediaSource = originalMediaSource;
});

Expand Down

0 comments on commit 9c224c1

Please sign in to comment.