I'm trying to use the HTTP Media Source Extension API in React + MUI. I have a "camera driver" class that maintains a MediaSource, and am linking it to a video element.
I'm currently doing this by maintaining the driver as react state, created in an effect. And referring to its url = URL.createObjectURL(this.src); from the video element after the driver gets set.
function App() {
// ...
const [driver, setDriver] = React.useState<null | Driver>(null);
React.useEffect(() => {
// ...
setDriver(driver);
}, [camera]);
const videoElement = <video src={driver?.url} autoPlay muted />;
return (/* ... */ videoElement /* ... */);
Full working reproduction here.
My code seems to work in Chrome and Safari but fails in Firefox 118.0.1 with the following error in console:
Security Error: Content at http://localhost:3000/ may not load data from blob:http://localhost:3000/26794ddf-bbc7-4aec-8eec-d2bc0cd61eef.
Why?
If I instead use a React.useRef<HTMLVideoElement>(null), and update videoRef.current!.src to point to the driver's URL, it seems to work. But I'd like to understand why, so I can know what I'm doing wrong and reliably avoid this problem as I modify and extend this software...
edit:
I've also tried adding a src/setupProxy.js that sets Content-Security-Policy:
// https://stackoverflow.com/a/68172973/23584
module.exports = function(app) {
app.use((req, res, next) => {
res.set({
'Content-Security-Policy': "default-src 'self' 'unsafe-eval' 'unsafe-inline' blob:;"
});
next();
});
};
See exact diff.
I can see in Firefox dev tool's network tab that the header value I set is making it through. Still get the error though.
If I use the older React 17-like ReactDOM.render, everything works. exact diff But obviously I can't stay there forever; I should be using the newer ReactDOM.createRoot(...).render(...) instead.
i am not really much expercienced with the reactjs but think you should consider the following thing as a try
instead of using URL.createObjectURL(this.src) to create the blob URL, use the URL.createObjectURL() method directly on the video file itself so that it ensures that the blob URL is created within the same origin as the video file. 2.assign the blob URL to the src attribute of the video element using videoRef.current.src instead of driver?.url. so pleas give it a try with the above approach.
import React, { useEffect, useRef } from 'react';
please run it and let me know if any.