@skyway-sdk/core
Version:
The official Next Generation JavaScript SDK for SkyWay
113 lines • 4.37 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.emptyVideoTrack = exports.emptyAudioTrack = exports.LocalMediaStreamBase = void 0;
const common_1 = require("@skyway-sdk/common");
const base_1 = require("../base");
const base_2 = require("./base");
const logger = new common_1.Logger('packages/core/src/media/stream/local/media.ts');
class LocalMediaStreamBase extends base_2.LocalStreamBase {
constructor(track, contentType,
/**@internal */
options = {}) {
super(contentType);
/**@description [japanese] PublicationのDisable/EnableなどでStreamのtrackが更新された時に発火するイベント */
this.onTrackUpdated = new common_1.Event();
/**
* @description [japanese] streamが破棄された時に発火するイベント (例. 画面共有が終了したときなど)
* @example [japanese] ハンドリング例
* const publication = await member.publish(audio);
audio.onDestroyed.once(async () => {
await member.unpublish(publication);
});
* */
this.onDestroyed = new common_1.Event();
this._disposer = new common_1.EventDisposer();
this._trackConstraints = {};
/**@internal */
this._replacingTrack = false;
/**@internal */
this._onReplacingTrackDone = new common_1.Event();
/**@internal */
this._onEnableChanged = new common_1.Event();
this._track = track;
this._listenTrackEvent();
this._options = options;
// iOS safari 15はgetConstraintsがバグってるのでここで入れておく
this._trackConstraints = Object.assign({}, options);
}
/**@internal */
get trackConstraints() {
return this._trackConstraints;
}
toJSON() {
const base = super.toJSON();
return Object.assign(Object.assign({}, base), { trackConstraints: this.trackConstraints, _options: this._options });
}
get track() {
return this._track;
}
/**
* @description [english] Attach the stream to the element.
* @description [japanese] streamをelementに適用する.
*/
attach(element) {
this._element = element;
(0, base_1.attachElement)(element, this._track);
}
/**
* @description [english] Detach the stream from the element.
* @description [japanese] elementからstreamを取り除く.
*/
detach() {
if (this._element) {
(0, base_1.detachElement)(this._element, this._track);
this._element = undefined;
}
}
/**@internal */
_disable(kind) {
if (this._options.stopTrackWhenDisabled) {
this._trackConstraints = Object.assign(Object.assign({}, this.trackConstraints), this.track.getConstraints());
this.track.stop();
}
else {
this._oldTrack = this.track;
}
const track = kind === 'video' ? exports.emptyVideoTrack : exports.emptyAudioTrack;
track.enabled = false;
this._onEnableChanged.emit(track);
this._updateTrack(track);
}
/**@internal */
_updateTrack(track) {
this._track = track;
if (this._element) {
this.attach(this._element);
}
this.onTrackUpdated.emit(track);
this._listenTrackEvent();
}
_listenTrackEvent() {
const onended = () => {
logger.info('onDestroyed', this.toJSON());
this.onDestroyed.emit();
};
this._track.addEventListener('ended', onended);
this._disposer.push(() => this._track.removeEventListener('ended', onended));
}
/**
* @description [japanese] Streamを解放します。
* カメラやマイクなどのデバイスを解放するためにはそのデバイスに関連するすべてのStreamを解放する必要があります
*/
release() {
this._disposer.dispose();
this._track.stop();
}
}
exports.LocalMediaStreamBase = LocalMediaStreamBase;
const createEmptyTrack = new RTCPeerConnection();
/**@internal */
exports.emptyAudioTrack = createEmptyTrack.addTransceiver('audio').receiver.track;
/**@internal */
exports.emptyVideoTrack = createEmptyTrack.addTransceiver('video').receiver.track;
//# sourceMappingURL=media.js.map