You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

audioRecorder.js 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* global APP, 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. }
  87. else if(MediaRecorder.isTypeSupported(AUDIO_OGG)) {
  88. return AUDIO_OGG;
  89. }
  90. else {
  91. throw new Error("unable to create a MediaRecorder with the" +
  92. "right mimetype!");
  93. }
  94. }
  95. /**
  96. * main exported object of the file, holding all
  97. * relevant functions and variables for the outside world
  98. * @param jitsiConference the jitsiConference which this object
  99. * is going to record
  100. */
  101. var audioRecorder = function(jitsiConference){
  102. // array of TrackRecorders, where each trackRecorder
  103. // holds the JitsiTrack, MediaRecorder and recorder data
  104. this.recorders = [];
  105. //get which file type is supported by the current browser
  106. this.fileType = determineCorrectFileType();
  107. //boolean flag for active recording
  108. this.isRecording = false;
  109. //the jitsiconference the object is recording
  110. this.jitsiConference = jitsiConference;
  111. };
  112. /**
  113. * Add the the exported module so that it can be accessed by other files
  114. */
  115. audioRecorder.determineCorrectFileType = determineCorrectFileType;
  116. /**
  117. * Adds a new TrackRecorder object to the array.
  118. *
  119. * @param track the track potentially holding an audio stream
  120. */
  121. audioRecorder.prototype.addTrack = function (track) {
  122. if(track.isAudioTrack()) {
  123. //create the track recorder
  124. var trackRecorder = instantiateTrackRecorder(track);
  125. //push it to the local array of all recorders
  126. this.recorders.push(trackRecorder);
  127. //update the name of the trackRecorders
  128. this.updateNames();
  129. //if we're already recording, immediately start recording this new track
  130. if(this.isRecording){
  131. startRecorder(trackRecorder);
  132. }
  133. }
  134. };
  135. /**
  136. * Notifies the module that a specific track has stopped, e.g participant left
  137. * the conference.
  138. * if the recording has not started yet, the TrackRecorder will be removed from
  139. * the array. If the recording has started, the recorder will stop recording
  140. * but not removed from the array so that the recorded stream can still be
  141. * accessed
  142. *
  143. * @param jitsiTrack the JitsiTrack to remove from the recording session
  144. */
  145. audioRecorder.prototype.removeTrack = function(jitsiTrack){
  146. var array = this.recorders;
  147. var i;
  148. for(i = 0; i < array.length; i++) {
  149. if(array[i].track.getParticipantId() === jitsiTrack.getParticipantId()){
  150. var recorderToRemove = array[i];
  151. if(this.isRecording){
  152. stopRecorder(recorderToRemove);
  153. }
  154. else {
  155. //remove the TrackRecorder from the array
  156. array.splice(i, 1);
  157. }
  158. }
  159. }
  160. //make sure the names are up to date
  161. this.updateNames();
  162. };
  163. /**
  164. * Tries to update the name value of all TrackRecorder in the array.
  165. * If it hasn't changed,it will keep the exiting name. If it changes to a
  166. * undefined value, the old value will also be kept.
  167. */
  168. audioRecorder.prototype.updateNames = function(){
  169. var conference = this.jitsiConference;
  170. this.recorders.forEach(function(trackRecorder){
  171. if(trackRecorder.track.isLocal()){
  172. trackRecorder.name = "the transcriber";
  173. }
  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. }
  272. else if(typeof(webkitMediaStream) !== 'undefined') {
  273. return new webkitMediaStream();
  274. }
  275. else {
  276. throw new Error("cannot create a clean mediaStream");
  277. }
  278. }
  279. /**
  280. * export the main object audioRecorder
  281. */
  282. module.exports = audioRecorder;