From f8d992fa91257a79cde8c071c326a2ac272238b4 Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Thu, 26 Mar 2020 17:53:15 +1100 Subject: [PATCH] Fix data only connections in safari from working and fixed sync bug --- src/helpers/shared.js | 21 +++++++++++++++++++++ src/helpers/useSession.js | 8 ++++++++ 2 files changed, 29 insertions(+) diff --git a/src/helpers/shared.js b/src/helpers/shared.js index 1e641cb..4117e5f 100644 --- a/src/helpers/shared.js +++ b/src/helpers/shared.js @@ -8,3 +8,24 @@ export function omit(obj, keys) { } return tmp; } + +/** + * This asks for audio permission on safari based devices + * this is needed to fix a implementation detail in safari + * where webRTC data channels cannot be opened without first + * using getUserMedia see: + * https://github.com/webrtc/samples/issues/1123 + * https://bugs.webkit.org/show_bug.cgi?id=189503 + * https://github.com/w3c/webrtc-nv-use-cases/issues/58 + */ +export async function enableDataConnectionForSafari() { + if ( + navigator.userAgent.match(/iPad/i) || + navigator.userAgent.match(/iPhone/i) || + (navigator.userAgent.match(/Safari/i) && + !navigator.userAgent.match(/Chrome/i)) + ) { + let stream = await navigator.mediaDevices.getUserMedia({ audio: true }); + stream.getTracks().forEach(t => t.stop()); + } +} diff --git a/src/helpers/useSession.js b/src/helpers/useSession.js index 549da6e..a370400 100644 --- a/src/helpers/useSession.js +++ b/src/helpers/useSession.js @@ -1,6 +1,8 @@ import { useEffect, useState } from "react"; import Peer from "peerjs"; +import { enableDataConnectionForSafari } from "./shared"; + function useSession(onConnectionOpen, onConnectionSync) { const [peerId, setPeerId] = useState(null); const [peer, setPeer] = useState(null); @@ -29,6 +31,7 @@ function useSession(onConnectionOpen, onConnectionSync) { function handleOpen(id) { console.log("Peer open", id); setPeerId(id); + enableDataConnectionForSafari(); } function handleConnection(connection) { @@ -106,6 +109,11 @@ function useSession(onConnectionOpen, onConnectionSync) { metadata: { sync: false } }); addConnection(syncConnection); + syncConnection.on("open", () => { + if (onConnectionOpen) { + onConnectionOpen(syncConnection); + } + }); } } });