WebRTC media source switcher

Forcefully chose a specific webrtc input/output audio/video device

2020-04-02 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         WebRTC media source switcher
// @namespace    https://daniil.it
// @version      0.1.3
// @description  Forcefully chose a specific webrtc input/output audio/video device
// @author       Daniil Gentili <[email protected]>
// @match        https://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var hidden = {
        enumerateDevices: navigator.mediaDevices.enumerateDevices.bind(navigator.mediaDevices),
        getUserMedia: navigator.mediaDevices.getUserMedia,
    }

    navigator.mediaDevices.getUserMedia = constraints => hidden.getUserMedia({audio: true, video: true}).then(() =>
        navigator.mediaDevices.enumerateDevices().then(devices => {
            devices.forEach(function(device) {
                if (device.kind == "videoinput" && constraints.video) {
                    if (constraints.video === true) constraints.video = {}
                    constraints.video.deviceId = {exact: device.deviceId}
                } else if (device.kind == "audioinput" && constraints.audio) {
                    if (constraints.audio === true) constraints.audio = {}
                    constraints.audio.deviceId = {exact: device.deviceId}
                }
            })
            console.log(constraints)
            return hidden.getUserMedia(constraints)
        })
    )
    navigator.mediaDevices.enumerateDevices = () => hidden.enumerateDevices().then(res => {
        alert("Called enumerateDevices");
        if (window.newRes) {
            return window.newRes;
        } else {
            window.newRes = [];
        }

        var temp = {};
        var tempLabels = {};
        for (var i = 0; i < res.length; i++) {
            if (!temp[res[i].kind]) {
                temp[res[i].kind] = [];
                tempLabels[res[i].kind] = "Choose a "+res[i].kind+" device:\n";
            }
            temp[res[i].kind].push(res[i])
            tempLabels[res[i].kind] += temp[res[i].kind].length+": "+res[i].label+"\n"
        }
        for (let [key, value] of Object.entries(tempLabels)) {
            var choice = prompt(value);
            window.newRes.push(temp[key][choice-1]);
        }

        return window.newRes;
    })
    /*
    for (let [key, value] of Object.entries(navigator.mediaDevices)) {
        navigator.mediaDevices[`__${key}`] = value;
        navigator.mediaDevices[key] = () => {
            alert(`Called ${key}`);
            return navigator.mediaDevices[`__${key}`](...arguments);
        }
    }*/
})();