jiti-meet/react/features/local-recording/recording/OggAdapter.js

104 lines
2.4 KiB
JavaScript
Raw Normal View History

import { RecordingAdapter } from './RecordingAdapter';
import { downloadBlob, timestampString } from './Utils';
const logger = require('jitsi-meet-logger').getLogger(__filename);
/**
2018-07-10 11:20:36 +00:00
* Recording adapter that uses {@code MediaRecorder} (default browser encoding
* with Opus codec).
*/
export class OggAdapter extends RecordingAdapter {
/**
* Instance of MediaRecorder.
*/
_mediaRecorder = null;
/**
2018-07-10 11:20:36 +00:00
* Initialization promise.
*/
2018-07-10 11:20:36 +00:00
_initPromise = null;
/**
* Implements {@link RecordingAdapter#start()}.
*
* @inheritdoc
*/
start() {
2018-07-10 11:20:36 +00:00
if (!this._initPromise) {
this._initPromise = this._initialize();
}
return this._initPromise.then(() =>
new Promise(resolve => {
this._mediaRecorder.start();
resolve();
})
);
}
/**
* Implements {@link RecordingAdapter#stop()}.
*
* @inheritdoc
*/
stop() {
return new Promise(
resolve => {
this._mediaRecorder.onstop = () => resolve();
this._mediaRecorder.stop();
}
);
}
/**
* Implements {@link RecordingAdapter#download()}.
*
* @inheritdoc
*/
download() {
if (this._recordedData !== null) {
const audioURL = window.URL.createObjectURL(this._recordedData);
downloadBlob(audioURL, `recording${timestampString()}.ogg`);
}
2018-07-10 11:20:36 +00:00
}
/**
* Initialize the adapter.
*
* @private
* @returns {Promise}
*/
_initialize() {
if (this._mediaRecorder) {
return Promise.resolve();
}
2018-07-10 11:20:36 +00:00
return new Promise((resolve, error) => {
this._getAudioStream(0)
.then(stream => {
this._mediaRecorder = new MediaRecorder(stream);
this._mediaRecorder.ondataavailable
= e => this._saveMediaData(e.data);
resolve();
})
.catch(err => {
logger.error(`Error calling getUserMedia(): ${err}`);
error();
});
});
}
/**
2018-07-10 11:20:36 +00:00
* Callback for storing the encoded data.
*
* @private
2018-07-10 11:20:36 +00:00
* @param {Blob} data - Encoded data.
* @returns {void}
*/
_saveMediaData(data) {
this._recordedData = data;
}
}