Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot screenshare from within iframe Cross domain. InvalidStateError #6212

Closed
andyryan opened this issue Oct 17, 2017 · 12 comments
Closed

Cannot screenshare from within iframe Cross domain. InvalidStateError #6212

andyryan opened this issue Oct 17, 2017 · 12 comments
Assignees

Comments

@andyryan
Copy link

I have an app that is connecting to a webapp across domains through an iframe. This embedded app is trying to initialise a screen share but the getUserMedia request fails with InvalidStateError.

I have tried using both the chooseDesktopMedia and DesktopCaptureMonitor(and yes I am remembering to use registerStream functions and both lead to the same error on getUserMedia.

Note: this flow works correctly on Chrome. Even cross domain.

@Christywl
Copy link
Contributor

Could you please share your sample to reproduce this issue?

@andyryan
Copy link
Author

I can't share the actual code due to IP, but heres roughly what it's doing.

Parent frame

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script type="text/javascript" src="demo.js"></script>
    <link rel="stylesheet" type="text/css" href="demo.css" >
</head>
<body>
    <iframe src="{some other domain}" style="width:100%; height:400px;"></iframe>
</body>
</html>

embedded iframe

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script type="text/javascript" src="demo.js"></script>
    <link rel="stylesheet" type="text/css" href="demo.css" >
</head>
<body>
    <div class="screen-share-demo">
        <video id="screen-share" autoplay></video>
        <div class="tools">
        <button onclick="chooseDesktopMedia()">screen.chooseDesktopMedia</button>
        </div>
    </div>
</body>
</html>

demo.js

function chooseDesktopMedia(){
    if(nw.Screen.Init){
        nw.Screen.Init();
    }
    nw.Screen.chooseDesktopMedia(["screen"],function(streamId){
        if(streamId) {
            var conf = {
                mandatory: {
                    chromeMediaSource: 'desktop',
                    chromeMediaSourceId: streamId,
                    maxWidth: 1920,
                    maxHeight: 1080
                },
                optional: []
            };
            console.log("[nw.chooseDesktopMedia] stream id:" + streamId);
            showScreenShare({audio: false, video: conf});
        }
    })
}

function showScreenShare(conf){
    var ve = document.getElementById("screen-share");

   navigator.mediaDevices.getUserMedia(conf)
    .then(function(stream){
        var url = window.URL.createObjectURL(stream);
        ve.src = url;
    })
    .catch(function(e){
        console.log(e);
        alert(e);
    });

}

NOTE: bug only occurs when the iframe src is on a separate domain

@andyryan
Copy link
Author

Tried something new today. I tried getting the iFrame to request that the parent frame get the media source ID and passing that back to the iframe and the iframe initiating screen sharing. Still no luck.

Another thing I have tried is the --disable-web-security and --no-sandbox chromium launch flags and they don't work either

@rogerwang
Copy link
Member

@andyryan could you please extract some minimal reproducible case from the app. It would be best if the same code works in Chrome but breaks in NW. PS, you can use nw --url= to test any web page.

@andyryan
Copy link
Author

Does the above code not reproduce?

@Christywl
Copy link
Contributor

I try to reproduce this with the codes above:

  1. Set up a http-server(npm) and put the iframe html and demo.js in this server.
  2. Replace src to my server address http://*****:8080/child.html in index.html.
  3. Add chromium-args in package.json due to my server is http, or it will throw NotSupportedError: Only secure origins are allowed:
{
    "name": "6212",
    "main": "index.html",
    "nodejs":true,
    "chromium-args": "--unsafely-treat-insecure-origin-as-secure=http://*****:8080",
    "node-remote": "<all_urls>"
}
  1. Launch the app with nwjs-sdk-v0.26.0, and click the screen.chooseDesktopMedia button:
    image
    note: please ignore demo.css error.

@andyryan , is the result same as yours? And please let me know if there are some wrong steps.

@andyryan
Copy link
Author

Yes thats what I'm seeing precisely. Thanks for reproducing.

One thing that may be of note. I've managed to work around this by using a reverse proxy to give the appearance that the contents of the iframe and from the same domain. This seems to have ben successful.

However I also tried changing the document.domain property so both were the same (as they only had different subdomains in the first place) however that alone did not seem to fix the problem

@rogerwang
Copy link
Member

@Christywl since OP said "this flow works correctly on Chrome. Even cross domain.", could you please try it in Chrome browser?

@Christywl
Copy link
Contributor

I have no idea about how to check it on chrome browser. I remove nw and modify the codes to chrome.desktopCapture.chooseDesktopMedia and open index.html with chrome, but it returns undefined.
@andyryan , could you please kindly tell me how to check this on Chrome?

@andyryan
Copy link
Author

Screensharing with Chrome is only possible from an extension which must be called from your app

@rogerwang rogerwang added the WIP label Oct 31, 2017
@rogerwang
Copy link
Member

This is fixed in git and will be available in the next nightly build.

@rogerwang
Copy link
Member

rogerwang added a commit that referenced this issue Nov 1, 2017
rogerwang added a commit that referenced this issue Nov 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants