UNPKG

@skyway-sdk/core

Version:

The official Next Generation JavaScript SDK for SkyWay

89 lines 4.03 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LocalAudioStream = void 0; const common_1 = require("@skyway-sdk/common"); const errors_1 = require("../../../errors"); const util_1 = require("../../../util"); const audioLevel_1 = require("../audioLevel"); const media_1 = require("./media"); const log = new common_1.Logger('packages/core/src/media/stream/local/audio.ts'); class LocalAudioStream extends media_1.LocalMediaStreamBase { constructor(track, options = {}) { super(track, 'audio', options); this.contentType = 'audio'; this._isEnabled = true; this._promiseQueue = new common_1.PromiseQueue(); if (track.kind !== 'audio') { throw (0, util_1.createError)({ operationName: 'LocalAudioStream.constructor', path: log.prefix, info: errors_1.errors.invalidTrackKind, payload: { track }, }); } } /**@internal */ setEnabled(enabled) { return __awaiter(this, void 0, void 0, function* () { yield this._promiseQueue.push(() => __awaiter(this, void 0, void 0, function* () { // mute if (this._isEnabled === true && enabled === false) { this._isEnabled = enabled; this._disable('audio'); log.debug('stopped'); } // unmute else if (this._isEnabled === false && enabled === true) { this._isEnabled = enabled; if (this._options.stopTrackWhenDisabled) { const track = this._options.isDisplayMedia === true ? yield this.enableDisplay() : yield this.enableMic(); this._updateTrack(track); this._onEnableChanged.emit(track); } else if (this._oldTrack) { this._updateTrack(this._oldTrack); this._onEnableChanged.emit(this._oldTrack); } log.debug('resumed'); } })); }); } enableMic() { return __awaiter(this, void 0, void 0, function* () { const [track] = (yield navigator.mediaDevices.getUserMedia({ audio: this.trackConstraints, })).getAudioTracks(); return track; }); } enableDisplay() { return __awaiter(this, void 0, void 0, function* () { const [track] = (yield navigator.mediaDevices.getDisplayMedia({ audio: this.trackConstraints, })).getAudioTracks(); return track; }); } /**@description [japanese] 直近100msにおける最大音量を取得する(値の範囲:0-1) */ getAudioLevel() { // 不要なリソース生成を行わないように初回実行時にAudioLevelインスタンスを生成する if (this._audioLevel === undefined) { this._audioLevel = new audioLevel_1.AudioLevel(this.track); } return this._isEnabled ? this._audioLevel.calculate() : 0; } } exports.LocalAudioStream = LocalAudioStream; //# sourceMappingURL=audio.js.map