Skip to content

Commit 754a042

Browse files
committed
fix: offline clients can't load webrtc shim
Issue: #197
1 parent 673e273 commit 754a042

File tree

5 files changed

+58
-13
lines changed

5 files changed

+58
-13
lines changed

package-lock.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"dependencies": {
3232
"callforth": "^0.3.1",
3333
"core-js": "^3.6.5",
34-
"vue": "^2.6.11"
34+
"vue": "^2.6.11",
35+
"webrtc-adapter": "7.7.0"
3536
},
3637
"devDependencies": {
3738
"@vue/cli-plugin-babel": "~4.4.0",

src/misc/camera.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { StreamApiNotSupportedError, InsecureContextError } from "./errors.js";
22
import { imageDataFromVideo } from "./image-data.js";
33
import { eventOn, timeout } from "callforth";
4-
import { indempotent } from "./util.js"
4+
import shimGetUserMedia from "./shimGetUserMedia"
55

66
class Camera {
77
constructor(videoEl, stream) {
@@ -64,15 +64,6 @@ const narrowDownFacingMode = async camera => {
6464
}
6565
};
6666

67-
const applyWebRTCShim = indempotent(() => {
68-
const script = document.createElement("script");
69-
script.src = "https://webrtc.github.io/adapter/adapter-7.6.3.js";
70-
71-
document.head.appendChild(script);
72-
73-
return eventOn(script, "load");
74-
});
75-
7667
export default async function(videoEl, { camera, torch }) {
7768
// At least in Chrome `navigator.mediaDevices` is undefined when the page is
7869
// loaded using HTTP rather than HTTPS. Thus `STREAM_API_NOT_SUPPORTED` is
@@ -90,7 +81,7 @@ export default async function(videoEl, { camera, torch }) {
9081

9182
// This is a browser API only shim. It patches the global window object which
9283
// is not available during SSR. So we lazily apply this shim at runtime.
93-
await applyWebRTCShim()
84+
await shimGetUserMedia()
9485

9586
const constraints = {
9687
audio: false,

src/misc/shimGetUserMedia.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { shimGetUserMedia as shimGetUserMediaChrome } from 'webrtc-adapter/src/js/chrome/getusermedia';
2+
import { shimGetUserMedia as shimGetUserMediaEdge } from 'webrtc-adapter/src/js/edge/getusermedia';
3+
import { shimGetUserMedia as shimGetUserMediaFirefox } from 'webrtc-adapter/src/js/firefox/getusermedia';
4+
import { shimGetUserMedia as shimGetUserMediaSafari } from 'webrtc-adapter/src/js/safari/safari_shim';
5+
import { detectBrowser } from 'webrtc-adapter/src/js/utils';
6+
7+
import { StreamApiNotSupportedError } from './errors';
8+
import { indempotent } from './util';
9+
10+
export default indempotent(() => {
11+
const { browser } = detectBrowser(window);
12+
13+
switch (browser) {
14+
case 'chrome':
15+
shimGetUserMediaChrome(window);
16+
break;
17+
case 'firefox':
18+
shimGetUserMediaFirefox(window);
19+
break;
20+
case 'edge':
21+
shimGetUserMediaEdge(window);
22+
break;
23+
case 'safari':
24+
shimGetUserMediaSafari(window);
25+
break;
26+
default:
27+
throw new StreamApiNotSupportedError()
28+
}
29+
})

vue.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module.exports = {
22
css: { extract: false },
33

4-
lintOnSave: false
4+
lintOnSave: false,
5+
6+
transpileDependencies: ['webrtc-adapter']
57
};

0 commit comments

Comments
 (0)