Adds media selection to links and webrtc html pages
This commit is contained in:
+40
-14
@@ -19,36 +19,61 @@
|
||||
<body>
|
||||
<video id="video" autoplay controls playsinline muted></video>
|
||||
<script>
|
||||
function PeerConnection(userMedia) {
|
||||
async function PeerConnection(media) {
|
||||
const pc = new RTCPeerConnection({
|
||||
iceServers: [{urls: 'stun:stun.l.google.com:19302'}]
|
||||
})
|
||||
|
||||
document.getElementById('video').srcObject = new MediaStream([
|
||||
pc.addTransceiver('video', {direction: 'recvonly'}).receiver.track,
|
||||
pc.addTransceiver('audio', {direction: 'recvonly'}).receiver.track
|
||||
])
|
||||
const localTracks = []
|
||||
|
||||
if (userMedia) {
|
||||
userMedia.getTracks().forEach(track => {
|
||||
if (/video|audio/.test(media)) {
|
||||
const tracks = ['video', 'audio']
|
||||
.filter(kind => media.indexOf(kind) >= 0)
|
||||
.map(kind => pc.addTransceiver(kind, {direction: 'recvonly'}).receiver.track)
|
||||
localTracks.push(...tracks)
|
||||
}
|
||||
|
||||
if (/camera|microphone/.test(media)) {
|
||||
const tracks = await getMediaTracks('user', {
|
||||
video: media.indexOf('camera') >= 0,
|
||||
audio: media.indexOf('microphone') >= 0,
|
||||
})
|
||||
tracks.forEach(track => {
|
||||
pc.addTransceiver(track, {direction: 'sendonly'})
|
||||
if (track.kind === 'video') localTracks.push(track)
|
||||
})
|
||||
}
|
||||
|
||||
if (media.indexOf('display') >= 0) {
|
||||
const tracks = await getMediaTracks('display', {
|
||||
video: true,
|
||||
audio: media.indexOf('speaker') >= 0,
|
||||
})
|
||||
tracks.forEach(track => {
|
||||
pc.addTransceiver(track, {direction: 'sendonly'})
|
||||
if (track.kind === 'video') localTracks.push(track)
|
||||
})
|
||||
}
|
||||
|
||||
document.getElementById('video').srcObject = new MediaStream(localTracks)
|
||||
|
||||
return pc
|
||||
}
|
||||
|
||||
async function userMedia() {
|
||||
async function getMediaTracks(media, constraints) {
|
||||
try {
|
||||
return await navigator.mediaDevices.getUserMedia({audio: true})
|
||||
const stream = media === 'user'
|
||||
? await navigator.mediaDevices.getUserMedia(constraints)
|
||||
: await navigator.mediaDevices.getDisplayMedia(constraints)
|
||||
return stream.getTracks()
|
||||
} catch (e) {
|
||||
return null
|
||||
console.warn(e)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
async function connect() {
|
||||
const pc = PeerConnection(await userMedia())
|
||||
|
||||
async function connect(media) {
|
||||
const pc = await PeerConnection(media)
|
||||
const url = new URL('api/ws' + location.search, location.href)
|
||||
const ws = new WebSocket('ws' + url.toString().substring(4))
|
||||
|
||||
@@ -75,7 +100,8 @@
|
||||
})
|
||||
}
|
||||
|
||||
connect()
|
||||
const media = new URLSearchParams(location.search).get('media')
|
||||
connect(media || 'video+audio')
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user