Advertisement
krot

RTC recorders

Jun 13th, 2018
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // global object that contains multiple recorders
  2. var recorders = {};
  3.  
  4. // auto start recorder as soon as stream starts/begins
  5. connection.onstream = function(event) {
  6.     document.body.appendChild(event.mediaElement);
  7.  
  8.     recorders[event.streamid] = RecordRTC(event.stream, {
  9.         type: 'video'
  10.     });
  11.  
  12.     recorders[event.streamid].startRecording();
  13. };
  14.  
  15. // auto stop recorder as soon as stream stops/ends
  16. connection.onstreamended = function(event) {
  17.     if (recorders[event.streamid]) {
  18.         recorders[event.streamid].stopRecording(function() {
  19.             var blob = recorders[event.streamid].getBlob();
  20.             var url = URL.createObjectURL(blob);
  21.             window.open(url);
  22.  
  23.             delete recorders[streamid]; // clear
  24.         });
  25.     }
  26.  
  27.     if (event.mediaElement.parentNode) {
  28.         event.mediaElement.parentNode.removeChild(event.mediaElement);
  29.     }
  30. };
  31.  
  32. // stop single recorder
  33. document.getElementById('manually-stop-single-recording').onclick = function() {
  34.     var streamid = prompt('Enter streamid');
  35.     recorders[streamid].stopRecording(function() {
  36.         var blob = recorders[streamid].getBlob();
  37.         var url = URL.createObjectURL(blob);
  38.         window.open(url);
  39.  
  40.         delete recorders[streamid]; // clear
  41.     });
  42. };
  43.  
  44. // stop all recorders
  45. document.getElementById('manually-stop-all-recordings').onclick = function() {
  46.     Object.keys(recorders).forEach(function(streamid) {
  47.         recorders[streamid].stopRecording(function() {
  48.             var blob = recorders[streamid].getBlob();
  49.             var url = URL.createObjectURL(blob);
  50.             window.open(url);
  51.  
  52.             delete recorders[streamid]; // clear
  53.         });
  54.     });
  55. };
  56.  
  57. // record outside onstream event
  58. // i.e. start recording anytime manually
  59. document.getElementById('record-stream-outside-the-onstream-event').onclick = function() {
  60.     var streamid = prompt('Enter streamid');
  61.     var stream = connection.streamEvents[streamid].stream;
  62.  
  63.     recorders[streamid] = RecordRTC(stream, {
  64.         type: 'video'
  65.     });
  66.  
  67.     recorders[streamid].startRecording();
  68. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement