2

I am trying to record every new session/user added to RTCMultiConnection.
i am using the following demo url in application https://rtcmulticonnection.herokuapp.com/demos/Audio+Video+TextChat+FileSharing.html

Now i have added the following cdn reference to the code. https://cdn.webrtc-experiment.com/RecordRTC.js

and this is the code i am working with but connection.streams[event.streamid].startRecording(); is not working.

// ..................RTCMultiConnection Code............. // ...................................................... var connection = new RTCMultiConnection(); var btnStopRec = document.getElementById("btnStopRecording"); connection.socketURL = 'https://rtcmulticonnection.herokuapp.com:443/'; connection.enableFileSharing = true; connection.session = { audio: true, video: true, data: true, }; connection.sdpConstraints.mandatory = { OfferToReceiveAudio: true, OfferToReceiveVideo: true, }; connection.onstream = function (event) { document.body.appendChild(event.mediaElement); console.log("stream recording starts") connection.streams[event.streamid].startRecording(); console.log("stream recording started") }

Khaan
  • 759
  • 7
  • 23

1 Answers1

3

I included all possible situations in a single snippet, below. Please take only the code that you need:

// global object that contains multiple recorders
var recorders = {};

// auto start recorder as soon as stream starts/begins
connection.onstream = function(event) {
    document.body.appendChild(event.mediaElement);

    recorders[event.streamid] = RecordRTC(event.stream, {
        type: 'video'
    });

    recorders[event.streamid].startRecording();
};

// auto stop recorder as soon as stream stops/ends
connection.onstreamended = function(event) {
    if (recorders[event.streamid]) {
        recorders[event.streamid].stopRecording(function() {
            var blob = recorders[event.streamid].getBlob();
            var url = URL.createObjectURL(blob);
            window.open(url);

            delete recorders[streamid]; // clear
        });
    }

    if (event.mediaElement.parentNode) {
        event.mediaElement.parentNode.removeChild(event.mediaElement);
    }
};

// stop single recorder
document.getElementById('manually-stop-single-recording').onclick = function() {
    var streamid = prompt('Enter streamid');
    recorders[streamid].stopRecording(function() {
        var blob = recorders[streamid].getBlob();
        var url = URL.createObjectURL(blob);
        window.open(url);

        delete recorders[streamid]; // clear
    });
};

// stop all recorders
document.getElementById('manually-stop-all-recordings').onclick = function() {
    Object.keys(recorders).forEach(function(streamid) {
        recorders[streamid].stopRecording(function() {
            var blob = recorders[streamid].getBlob();
            var url = URL.createObjectURL(blob);
            window.open(url);

            delete recorders[streamid]; // clear
        });
    });
};

// record outside onstream event
// i.e. start recording anytime manually
document.getElementById('record-stream-outside-the-onstream-event').onclick = function() {
    var streamid = prompt('Enter streamid');
    var stream = connection.streamEvents[streamid].stream;

    recorders[streamid] = RecordRTC(stream, {
        type: 'video'
    });

    recorders[streamid].startRecording();
};
Muaz Khan
  • 7,138
  • 9
  • 41
  • 77