3

I have managed to setup my webcam to point to a specific location on the Firebase Database and broadcast a video using WebRTC.

I do this as follows in Javascript (and display in my HTML):

<video id="yourVideo" autoplay muted playsinline></video>
...

var database = firebase.database().ref('node_on_firebase');
var yourVideo = document.getElementById("yourVideo");
var friendsVideo = document.getElementById("friendsVideo");
var yourId = Math.floor(Math.random()*1000000000);
var servers = {'iceServers': [{'urls': 'stun:stun.services.mozilla.com'}, {'urls': 'stun:stun.l.google.com:19302'}, {'urls': 'turn:numb.viagenie.ca','credential': 'webrtc','username': 'websitebeaver@mail.com'}]};

var pc = new RTCPeerConnection(servers);
pc.onicecandidate = (event => event.candidate?sendMessage(yourId, JSON.stringify({'ice': event.candidate})):console.log("Sent All Ice") );
pc.onaddstream = (event => friendsVideo.srcObject = event.stream);

function sendMessage(senderId, data) {
    var msg = database.push({ sender: senderId, message: data });
    msg.remove();
}


function readMessage(data) {
    // works
    var msg    = JSON.parse(data.val().message);
    var sender = data.val().sender;

    if (sender != yourId) {

        if (msg.ice != undefined)
            pc.addIceCandidate(new RTCIceCandidate(msg.ice));

        else if (msg.sdp.type == "offer")
            pc.setRemoteDescription(new RTCSessionDescription(msg.sdp))
            .then(() => pc.createAnswer())
            .then(answer => pc.setLocalDescription(answer))
            .then(() => sendMessage(yourId, JSON.stringify({'sdp': pc.localDescription})));

        else if (msg.sdp.type == "answer")
            pc.setRemoteDescription(new RTCSessionDescription(msg.sdp));

    }
};

database.on('child_added', readMessage);

function closeMyFace() {
    yourVideo.srcObject.getTracks().forEach(track => track.stop());
}

function showMyFace() {
    navigator.mediaDevices.getUserMedia({audio:false, video:true}).
    then(function(stream){
        pc.addStream(stream)
        yourVideo.srcObject = stream
    })
    .catch(function(error){
        console.log(error)
    })
}

function showFriendsFace() {
    pc.createOffer()
    .then(offer => pc.setLocalDescription(offer) )
    .then(() => sendMessage(yourId, JSON.stringify({'sdp': pc.localDescription})) );
}

However, how do I download/stream this video and process the video in chunks, ideally in a Python script?

WJA
  • 6,676
  • 16
  • 85
  • 152
  • In case not already aware, I believe your current setup is using Firebase only for the connection control and setup. The video itself is streamed peer-to-peer, and not through firebase. Do you intend to download/stream the video in real-time, or can the download/processing be done after the video has been captured? – kaliatech Jul 29 '19 at 12:24

1 Answers1

1

If you intend to download/process the video while it is streaming, then your (python) client will need to create its own RTCPeerConnection so that it can also receive the video stream. I believe that would not be trivial in python, though probably easier on other platforms. More info: WebRTC Python implementation

If your use case allows you to process the video after the recording is complete (or at least, your use case is okay with significant latency), then could have the javascript client upload the data as it received or later in batch (from friendsVideo stream in the example above), possibly in chunks, to a location where your custom (python) client could then download and process.

Although not related to RTCPeerConnection, you can search here on SO for other users that have used firebase for streaming video (with mixed results). Again though, that is somewhat different from what you are trying to do with RTCPeerConnection. Example: Firebase Storage Video Streaming

kaliatech
  • 17,579
  • 5
  • 72
  • 84