GUACAMOLE-732: Retrieve audio stream via Promise for newer versions of getUserMedia().

This commit is contained in:
Michael Jumper
2019-12-23 20:40:34 -08:00
parent 062edda07b
commit e93d3e00f2

View File

@@ -412,18 +412,15 @@ Guacamole.RawAudioRecorder = function RawAudioRecorder(stream, mimetype) {
};
/**
* Requests access to the user's microphone and begins capturing audio. All
* received audio data is resampled as necessary and forwarded to the
* Guacamole stream underlying this Guacamole.RawAudioRecorder. This
* function must be invoked ONLY ONCE per instance of
* Guacamole.RawAudioRecorder.
* getUserMedia() callback which handles successful retrieval of an
* audio stream (successful start of recording).
*
* @private
* @param {MediaStream} stream
* A MediaStream which provides access to audio data read from the
* user's local audio input device.
*/
var beginAudioCapture = function beginAudioCapture() {
// Attempt to retrieve an audio input stream from the browser
navigator.mediaDevices.getUserMedia({ 'audio' : true }, function streamReceived(stream) {
var streamReceived = function streamReceived(stream) {
// Create processing node which receives appropriately-sized audio buffers
processor = context.createScriptProcessor(BUFFER_SIZE, format.channels, format.channels);
@@ -441,7 +438,16 @@ Guacamole.RawAudioRecorder = function RawAudioRecorder(stream, mimetype) {
// Save stream for later cleanup
mediaStream = stream;
}, function streamDenied() {
};
/**
* getUserMedia() callback which handles audio recording denial. The
* underlying Guacamole output stream is closed, and the failure to
* record is noted using onerror.
*
* @private
*/
var streamDenied = function streamDenied() {
// Simply end stream if audio access is not allowed
writer.sendEnd();
@@ -450,7 +456,28 @@ Guacamole.RawAudioRecorder = function RawAudioRecorder(stream, mimetype) {
if (recorder.onerror)
recorder.onerror();
});
};
/**
* Requests access to the user's microphone and begins capturing audio. All
* received audio data is resampled as necessary and forwarded to the
* Guacamole stream underlying this Guacamole.RawAudioRecorder. This
* function must be invoked ONLY ONCE per instance of
* Guacamole.RawAudioRecorder.
*
* @private
*/
var beginAudioCapture = function beginAudioCapture() {
// Attempt to retrieve an audio input stream from the browser
var promise = navigator.mediaDevices.getUserMedia({
'audio' : true
}, streamReceived, streamDenied);
// Handle stream creation/rejection via Promise for newer versions of
// getUserMedia()
if (promise && promise.then)
promise.then(streamReceived, streamDenied);
};