OLD | NEW |
(Empty) | |
| 1 /** |
| 2 * Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. |
| 5 */ |
| 6 |
| 7 /** |
| 8 * Aggregate target bits per second for encoding of the Audio track. |
| 9 * @private |
| 10 */ |
| 11 var gAudioBitsPerSecond = 3000000; |
| 12 |
| 13 /** |
| 14 * The duration of the audio recording in milliseconds. |
| 15 * @private |
| 16 */ |
| 17 var gCaptureDuration = 0; |
| 18 |
| 19 /** |
| 20 * The recorded audio encoded in Base64. |
| 21 * @private |
| 22 */ |
| 23 var gAudioBase64 = ''; |
| 24 |
| 25 /** |
| 26 * Chunks of the audio recorded by MediaRecorded as they become available. |
| 27 * @private |
| 28 */ |
| 29 var gChunks = []; |
| 30 |
| 31 /** |
| 32 * A string to be returned to the test about the current status of capture. |
| 33 */ |
| 34 var gCapturingStatus = 'capturing-not-started'; |
| 35 |
| 36 /** |
| 37 * Starts the audio capturing. |
| 38 * |
| 39 * @param {Number} The duration of the capture in seconds. |
| 40 */ |
| 41 function startAudioCapture(duration) { |
| 42 console.log('started-capture'); |
| 43 var inputElement = document.getElementById('remote-view'); |
| 44 |
| 45 // |audioBitsPerSecond| must set to a large number to throw as little |
| 46 // information away as possible. |
| 47 var mediaRecorderOptions = { |
| 48 audioBitsPerSecond: gAudioBitsPerSecond, |
| 49 mimeType: 'audio/webm', |
| 50 }; |
| 51 var stream = getStreamFromElement_(inputElement); |
| 52 gCaptureDuration = 1000 * duration; |
| 53 |
| 54 var mediaRecorder = new MediaRecorder(stream, mediaRecorderOptions); |
| 55 |
| 56 mediaRecorder.ondataavailable = function(recording) { |
| 57 gChunks.push(recording.data); |
| 58 } |
| 59 mediaRecorder.onstop = function() { |
| 60 console.log(gChunks.length); |
| 61 var audioBlob = new Blob(gChunks, {type: "audio/webm"}); |
| 62 gChunks = []; |
| 63 var reader = new FileReader(); |
| 64 reader.onloadend = function() { |
| 65 gAudioBase64 = reader.result.substr(reader.result.indexOf(',') + 1); |
| 66 gCapturingStatus = 'done-capturing'; |
| 67 debug('done-capturing'); |
| 68 } |
| 69 reader.readAsDataURL(audioBlob); |
| 70 } |
| 71 |
| 72 mediaRecorder.start(); |
| 73 setTimeout(function() { mediaRecorder.stop(); }, gCaptureDuration); |
| 74 gCapturingStatus = 'still-capturing'; |
| 75 } |
| 76 |
| 77 /** |
| 78 * Returns the audio recorded by RecordMedia encoded in Base64. |
| 79 */ |
| 80 function getRecordedAudioAsBase64() { |
| 81 silentReturnToTest(gAudioBase64); |
| 82 } |
| 83 |
| 84 /** |
| 85 * Queries if we're done with the capturing yet. |
| 86 */ |
| 87 function doneCapturingAudio() { |
| 88 returnToTest(gCapturingStatus); |
| 89 } |
| 90 |
| 91 /** |
| 92 * Returns the stream from the input element to be attached to MediaRecorder. |
| 93 * @private |
| 94 */ |
| 95 function getStreamFromElement_(element) { |
| 96 if (element.srcObject !== undefined) { |
| 97 return element.srcObject; |
| 98 } else if (element.src !== undefined) { |
| 99 return element.src; |
| 100 } else { |
| 101 failTest('Error extracting stream from element.'); |
| 102 } |
| 103 } |
OLD | NEW |