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

Support getUserMedia working on iOS safari and Mac OS X safari #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions examples/lobbys/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
<body>
<p>Select a lobby to join!</p>
<div id="list"></div>
<div id="localVideo"></div>
<div id="localVideo">
<video id="video-local" width="80%" playsinline autoplay muted controls style="display:none;"></video>
</div>
<div id="remoteVideos"></div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>
Expand All @@ -26,7 +28,7 @@
const roomContainer = document.getElementById('list')
const localVideoContainer = document.getElementById('localVideo')
const remoteVideoContainer = document.getElementById('remoteVideos')

const localVideoElement = document.getElementById("video-local");
// creates a DOM element to allow the user to see/join rooms
function createRoomElement (id) {
const element = document.createElement('div')
Expand All @@ -41,6 +43,7 @@
function createVideoElement(container, mediaStream, muted=false) {
const videoElement = document.createElement('video')
videoElement.autoplay = true
videoElement.playsInline = true
videoElement.srcObject = mediaStream
videoElement.muted = muted
container.appendChild(videoElement)
Expand Down Expand Up @@ -98,10 +101,10 @@
}
signalClient.addListener('discover', onRoomPeers)
}

// request local webcam
navigator.getUserMedia({ audio: true, video: true }, (localStream) => {
const videoElement = createVideoElement(localVideoContainer, localStream, true) // display local video
function userMediaProcess(localStream, localVideoElement) {
localVideoElement.style.display = "block"
localVideoElement.srcObject = localStream;
signalClient.discover(null) // begin discovering rooms

signalClient.on('request', async (request) => {
Expand All @@ -118,6 +121,22 @@
roomElement.addEventListener('click', () => joinRoom(roomID, localStream)) // register a click handler to join room
})
})
}, () => alert('No webcam access!'))
}

// request local webcam
try {
navigator.mediaDevices.getUserMedia({
video: true,
audio: false
}).then(localStream => {
userMediaProcess(localStream, localVideoElement);
}).catch(err => {
alert("No webcam access!");
});
}
catch (err) {
alert("No webcam access!");
}

</script>
</html>