getusermedia_xorigin_container.html (1582B)
1 <html> 2 <head> 3 <meta charset="utf-8" /> 4 <title>GetUserMedia from cross-origin iframe: the container document</title> 5 </head> 6 <body> 7 <iframe 8 id="iframe_no_allow" 9 src="http://127.0.0.1:4245/assets/www/getusermedia_xorigin_iframe.html" 10 ></iframe> 11 <iframe 12 id="iframe" 13 allow="camera;microphone" 14 src="http://127.0.0.1:4245/assets/www/getusermedia_xorigin_iframe.html" 15 ></iframe> 16 <script> 17 "use strict"; 18 19 let iframeWindow; 20 let generation = 1; 21 22 async function Send(obj) { 23 obj.gen = generation++; 24 iframeWindow.postMessage(obj, "http://127.0.0.1:4245"); 25 while (true) { 26 const { 27 data: { gen, result }, 28 } = await new Promise(r => (window.onmessage = r)); 29 if (gen == obj.gen) { 30 return result; 31 } 32 } 33 } 34 35 function Start(constraints) { 36 if (iframeWindow) { 37 return "iframe mode already decided"; 38 } 39 iframeWindow = document.getElementById("iframe").contentWindow; 40 return Send({ name: "start", constraints }); 41 } 42 43 function StartNoAllow(constraints) { 44 if (iframeWindow) { 45 return "iframe mode already decided"; 46 } 47 iframeWindow = document.getElementById("iframe_no_allow").contentWindow; 48 return Send({ name: "start", constraints }); 49 } 50 51 async function Stop() { 52 const result = await Send({ name: "stop" }); 53 iframeWindow = undefined; 54 return result; 55 } 56 </script> 57 </body> 58 </html>