Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

audioRecorder.js 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* global MediaRecorder, MediaStream, webkitMediaStream */
  2. var RecordingResult = require("./recordingResult");
  3. /**
  4. * Possible audio formats MIME types
  5. */
  6. var AUDIO_WEBM = "audio/webm"; // Supported in chrome
  7. var AUDIO_OGG = "audio/ogg"; // Supported in firefox
  8. /**
  9. * A TrackRecorder object holds all the information needed for recording a
  10. * single JitsiTrack (either remote or local)
  11. * @param track The JitsiTrack the object is going to hold
  12. */
  13. var TrackRecorder = function(track) {
  14. // The JitsiTrack holding the stream
  15. this.track = track;
  16. // The MediaRecorder recording the stream
  17. this.recorder = null;
  18. // The array of data chunks recorded from the stream
  19. // acts as a buffer until the data is stored on disk
  20. this.data = null;
  21. // the name of the person of the JitsiTrack. This can be undefined and/or
  22. // not unique
  23. this.name = null;
  24. // the time of the start of the recording
  25. this.startTime = null;
  26. };
  27. /**
  28. * Starts the recording of a JitsiTrack in a TrackRecorder object.
  29. * This will also define the timestamp and try to update the name
  30. * @param trackRecorder the TrackRecorder to start
  31. */
  32. function startRecorder(trackRecorder) {
  33. if(trackRecorder.recorder === undefined) {
  34. throw new Error("Passed an object to startRecorder which is not a " +
  35. "TrackRecorder object");
  36. }
  37. trackRecorder.recorder.start();
  38. trackRecorder.startTime = new Date();
  39. }
  40. /**
  41. * Stops the recording of a JitsiTrack in a TrackRecorder object.
  42. * This will also try to update the name
  43. * @param trackRecorder the TrackRecorder to stop
  44. */
  45. function stopRecorder(trackRecorder) {
  46. if(trackRecorder.recorder === undefined) {
  47. throw new Error("Passed an object to stopRecorder which is not a " +
  48. "TrackRecorder object");
  49. }
  50. trackRecorder.recorder.stop();
  51. }
  52. /**
  53. * Creates a TrackRecorder object. Also creates the MediaRecorder and
  54. * data array for the trackRecorder.
  55. * @param track the JitsiTrack holding the audio MediaStream(s)
  56. */
  57. function instantiateTrackRecorder(track) {
  58. var trackRecorder = new TrackRecorder(track);
  59. // Create a new stream which only holds the audio track
  60. var originalStream = trackRecorder.track.getOriginalStream();
  61. var stream = createEmptyStream();
  62. originalStream.getAudioTracks().forEach(function(track) {
  63. stream.addTrack(track);
  64. });
  65. // Create the MediaRecorder
  66. trackRecorder.recorder = new MediaRecorder(stream,
  67. {mimeType: audioRecorder.fileType});
  68. // array for holding the recorder data. Resets it when
  69. // audio already has been recorder once
  70. trackRecorder.data = [];
  71. // function handling a dataEvent, e.g the stream gets new data
  72. trackRecorder.recorder.ondataavailable = function(dataEvent) {
  73. if(dataEvent.data.size > 0) {
  74. trackRecorder.data.push(dataEvent.data);
  75. }
  76. };
  77. return trackRecorder;
  78. }
  79. /**
  80. * Determines which kind of audio recording the browser supports
  81. * chrome supports "audio/webm" and firefox supports "audio/ogg"
  82. */
  83. function determineCorrectFileType() {
  84. if(MediaRecorder.isTypeSupported(AUDIO_WEBM)) {
  85. return AUDIO_WEBM;
  86. } else if(MediaRecorder.isTypeSupported(AUDIO_OGG)) {
  87. return AUDIO_OGG;
  88. } else {
  89. throw new Error("unable to create a MediaRecorder with the" +
  90. "right mimetype!");
  91. }
  92. }
  93. /**
  94. * main exported object of the file, holding all
  95. * relevant functions and variables for the outside world
  96. * @param jitsiConference the jitsiConference which this object
  97. * is going to record
  98. */
  99. var audioRecorder = function(jitsiConference) {
  100. // array of TrackRecorders, where each trackRecorder
  101. // holds the JitsiTrack, MediaRecorder and recorder data
  102. this.recorders = [];
  103. // get which file type is supported by the current browser
  104. this.fileType = determineCorrectFileType();
  105. // boolean flag for active recording
  106. this.isRecording = false;
  107. // the jitsiconference the object is recording
  108. this.jitsiConference = jitsiConference;
  109. };
  110. /**
  111. * Add the the exported module so that it can be accessed by other files
  112. */
  113. audioRecorder.determineCorrectFileType = determineCorrectFileType;
  114. /**
  115. * Adds a new TrackRecorder object to the array.
  116. *
  117. * @param track the track potentially holding an audio stream
  118. */
  119. audioRecorder.prototype.addTrack = function(track) {
  120. if(track.isAudioTrack()) {
  121. // create the track recorder
  122. var trackRecorder = instantiateTrackRecorder(track);
  123. // push it to the local array of all recorders
  124. this.recorders.push(trackRecorder);
  125. // update the name of the trackRecorders
  126. this.updateNames();
  127. // If we're already recording, immediately start recording this new
  128. // track.
  129. if(this.isRecording) {
  130. startRecorder(trackRecorder);
  131. }
  132. }
  133. };
  134. /**
  135. * Notifies the module that a specific track has stopped, e.g participant left
  136. * the conference.
  137. * if the recording has not started yet, the TrackRecorder will be removed from
  138. * the array. If the recording has started, the recorder will stop recording
  139. * but not removed from the array so that the recorded stream can still be
  140. * accessed
  141. *
  142. * @param {JitsiTrack} track the JitsiTrack to remove from the recording session
  143. */
  144. audioRecorder.prototype.removeTrack = function(track) {
  145. if(track.isVideoTrack()) {
  146. return;
  147. }
  148. var array = this.recorders;
  149. var i;
  150. for(i = 0; i < array.length; i++) {
  151. if(array[i].track.getParticipantId() === track.getParticipantId()) {
  152. var recorderToRemove = array[i];
  153. if(this.isRecording) {
  154. stopRecorder(recorderToRemove);
  155. } else {
  156. // remove the TrackRecorder from the array
  157. array.splice(i, 1);
  158. }
  159. }
  160. }
  161. // make sure the names are up to date
  162. this.updateNames();
  163. };
  164. /**
  165. * Tries to update the name value of all TrackRecorder in the array.
  166. * If it hasn't changed,it will keep the exiting name. If it changes to a
  167. * undefined value, the old value will also be kept.
  168. */
  169. audioRecorder.prototype.updateNames = function() {
  170. var conference = this.jitsiConference;
  171. this.recorders.forEach(function(trackRecorder) {
  172. if(trackRecorder.track.isLocal()) {
  173. trackRecorder.name = "the transcriber";
  174. } else {
  175. var id = trackRecorder.track.getParticipantId();
  176. var participant = conference.getParticipantById(id);
  177. var newName = participant.getDisplayName();
  178. if(newName !== 'undefined') {
  179. trackRecorder.name = newName;
  180. }
  181. }
  182. });
  183. };
  184. /**
  185. * Starts the audio recording of every local and remote track
  186. */
  187. audioRecorder.prototype.start = function() {
  188. if(this.isRecording) {
  189. throw new Error("audiorecorder is already recording");
  190. }
  191. // set boolean isRecording flag to true so if new participants join the
  192. // conference, that track can instantly start recording as well
  193. this.isRecording = true;
  194. // start all the mediaRecorders
  195. this.recorders.forEach(function(trackRecorder) {
  196. startRecorder(trackRecorder);
  197. });
  198. // log that recording has started
  199. console.log("Started the recording of the audio. There are currently " +
  200. this.recorders.length + " recorders active.");
  201. };
  202. /**
  203. * Stops the audio recording of every local and remote track
  204. */
  205. audioRecorder.prototype.stop = function() {
  206. // set the boolean flag to false
  207. this.isRecording = false;
  208. // stop all recorders
  209. this.recorders.forEach(function(trackRecorder) {
  210. stopRecorder(trackRecorder);
  211. });
  212. console.log("stopped recording");
  213. };
  214. /**
  215. * link hacking to download all recorded audio streams
  216. */
  217. audioRecorder.prototype.download = function() {
  218. var t = this;
  219. this.recorders.forEach(function(trackRecorder) {
  220. var blob = new Blob(trackRecorder.data, {type: t.fileType});
  221. var url = URL.createObjectURL(blob);
  222. var a = document.createElement('a');
  223. document.body.appendChild(a);
  224. a.style = "display: none";
  225. a.href = url;
  226. a.download = 'test.' + t.fileType.split("/")[1];
  227. a.click();
  228. window.URL.revokeObjectURL(url);
  229. });
  230. };
  231. /**
  232. * returns the audio files of all recorders as an array of objects,
  233. * which include the name of the owner of the track and the starting time stamp
  234. * @returns {Array} an array of RecordingResult objects
  235. */
  236. audioRecorder.prototype.getRecordingResults = function() {
  237. if(this.isRecording) {
  238. throw new Error("cannot get blobs because the AudioRecorder is still" +
  239. "recording!");
  240. }
  241. // make sure the names are up to date before sending them off
  242. this.updateNames();
  243. var array = [];
  244. var t = this;
  245. this.recorders.forEach(function(recorder) {
  246. array.push(
  247. new RecordingResult(
  248. new Blob(recorder.data, {type: t.fileType}),
  249. recorder.name,
  250. recorder.startTime)
  251. );
  252. });
  253. return array;
  254. };
  255. /**
  256. * Gets the mime type of the recorder audio
  257. * @returns {String} the mime type of the recorder audio
  258. */
  259. audioRecorder.prototype.getFileType = function() {
  260. return this.fileType;
  261. };
  262. /**
  263. * Creates a empty MediaStream object which can be used
  264. * to add MediaStreamTracks to
  265. * @returns MediaStream
  266. */
  267. function createEmptyStream() {
  268. // Firefox supports the MediaStream object, Chrome webkitMediaStream
  269. if(typeof MediaStream !== 'undefined') {
  270. return new MediaStream();
  271. } else if(typeof webkitMediaStream !== 'undefined') {
  272. return new webkitMediaStream(); // eslint-disable-line new-cap
  273. } else {
  274. throw new Error("cannot create a clean mediaStream");
  275. }
  276. }
  277. /**
  278. * export the main object audioRecorder
  279. */
  280. module.exports = audioRecorder;