From f4c925b33921f727ec9bd7817a55f2d6dacf5d44 Mon Sep 17 00:00:00 2001 From: Sangwon Oh Date: Wed, 1 Feb 2023 18:42:55 +0900 Subject: [PATCH] Improve input device detection --- .gitignore | 2 ++ package.json | 2 +- src/OvenLiveKit.js | 82 +++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 76 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index ab70b55..1f81464 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +dev + ### Node ### # Logs logs diff --git a/package.json b/package.json index 7ff51d4..e3ed442 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ovenlivekit", - "version": "1.0.2", + "version": "1.0.3", "description": "OvenLiveKit for Web is an open source JavaScript SDK suite for live streaming from web browsers to OvenMediaEngine.", "main": "dist/OvenLiveKit.min.js", "scripts": { diff --git a/src/OvenLiveKit.js b/src/OvenLiveKit.js index 4019e38..c9562d5 100644 --- a/src/OvenLiveKit.js +++ b/src/OvenLiveKit.js @@ -79,22 +79,27 @@ function removeFormat(sdp, formatNumber) { return newLines.join('\r\n') } -async function getStreamForDeviceCheck() { +async function getStreamForDeviceCheck(type) { // High resolution video constraints makes browser to get maximum resolution of video device. const constraints = { - audio: { deviceId: undefined }, - video: { deviceId: undefined, width: 1920, height: 1080 } }; + if (type === 'both') { + constraints.audio = true; + constraints.video = true; + } else if (type === 'audio') { + constraints.audio = true; + } else if (type === 'video') { + constraints.video = true; + } + return await navigator.mediaDevices.getUserMedia(constraints); } async function getDevices() { return await navigator.mediaDevices.enumerateDevices(); - - } function gotDevices(deviceInfos) { @@ -426,6 +431,40 @@ function addMethod(instance) { return lines.join('\r\n') } + function appendOrientation(sdp) { + + const lines = sdp.split('\r\n'); + const payloads = []; + + for (let i = 0; i < lines.length; i++) { + + if (lines[i].indexOf('m=video') === 0) { + + let tokens = lines[i].split(' ') + + for (let j = 3; j < tokens.length; j++) { + + payloads.push(tokens[j]); + } + + break; + } + } + + for (let i = 0; i < payloads.length; i++) { + + for (let j = 0; j < lines.length; j++) { + + if (lines[j].indexOf('a=rtpmap:' + payloads[i]) === 0) { + + lines[j] += '\r\na=extmap:' + payloads[i] + ' urn:3gpp:video-orientation'; + } + } + } + + return lines.join('\r\n') + } + function createPeerConnection(id, peerId, offer, candidates, iceServers) { let peerConnectionConfig = {}; @@ -552,15 +591,14 @@ function addMethod(instance) { offer.sdp = appendFmtp(offer.sdp); } + // offer.sdp = appendOrientation(offer.sdp); + peerConnection.setRemoteDescription(new RTCSessionDescription(offer)) .then(function () { peerConnection.createAnswer() .then(function (answer) { - console.log(logHeader, 'Answer') - console.log(answer.sdp) - if (checkIOSVersion() >= 15) { const formatNumber = getFormatNumber(answer.sdp, 'H264'); @@ -769,7 +807,33 @@ OvenLiveKit.create = function (options) { OvenLiveKit.getDevices = async function () { - await getStreamForDeviceCheck(); + try { + // First check both audio and video sources are available. + await getStreamForDeviceCheck('both'); + } catch (e) { + + console.warn(logHeader, 'Can not find Video and Audio devices', e); + + let videoFound = null; + let audioFound = null; + + try { + videoFound = await getStreamForDeviceCheck('video'); + } catch (e) { + console.warn(logHeader, 'Can not find Video devices', e); + } + + try { + audioFound = await getStreamForDeviceCheck('audio'); + } catch (e) { + console.warn(logHeader, 'Can not find Audio devices', e); + } + + if (!videoFound && !audioFound) { + throw new Error('No input devices were found.'); + } + } + const deviceInfos = await getDevices(); return gotDevices(deviceInfos) };