@ashetm/ng-mediastream
Version:
``@ashetm/ng-mediastream`` is a library that simplify manipulation of API ``MediaStream``.
324 lines (309 loc) • 13.1 kB
JavaScript
import * as i0 from '@angular/core';
import { InjectionToken, Injectable, Inject, Injector, makeEnvironmentProviders } from '@angular/core';
import { BehaviorSubject, combineLatest, from } from 'rxjs';
import { switchMap, map } from 'rxjs/operators';
class AMediastream {
constructor() {
this.NAVIGATOR = window.navigator;
this.MEDIA_DEVICES = this.NAVIGATOR.mediaDevices;
}
}
class AMediastreamDevice extends AMediastream {
constructor() {
super(...arguments);
this._currentMediaDeviceSubject$ = new BehaviorSubject(null);
this.currentMediaDevice$ = this._currentMediaDeviceSubject$;
}
}
class AMediastreamService extends AMediastreamDevice {
get mediastreamConstraints() {
return this._mediaConstraints;
}
constructor(_mediaConstraints) {
super();
this._mediaConstraints = _mediaConstraints;
this._toggleMediaStreamSubject = new BehaviorSubject(true);
}
_buildMediaConstraints(device) {
switch (true) {
case device && typeof this._mediaConstraints === 'object':
return {
...this._mediaConstraints,
deviceId: device.deviceId
};
case device
&& typeof this._mediaConstraints === 'boolean'
&& this._mediaConstraints:
return {
deviceId: device.deviceId
};
case device
&& typeof this._mediaConstraints === 'boolean'
&& !this._mediaConstraints:
return this._mediaConstraints;
default:
return {
...this._mediaConstraints
};
}
}
}
class AMediastreamCameraService extends AMediastreamService {
constructor() {
super(...arguments);
this._userMedia$ = combineLatest([
this._currentMediaDeviceSubject$
]).pipe(switchMap(([device]) => from(this.MEDIA_DEVICES.getUserMedia({
audio: false,
video: this._buildMediaConstraints(device)
}))));
}
disableCamera() {
this._toggleMediaStreamSubject.next(false);
}
enableCamera() {
this._toggleMediaStreamSubject.next(true);
}
toggleCamera() {
const value = this._toggleMediaStreamSubject.getValue();
this._toggleMediaStreamSubject.next(!value);
}
updateCameraDevice(device) {
this._currentMediaDeviceSubject$.next(device);
}
}
const MEDIASTREAM_TOKEN_CAMERA_CONSTRAINTS = new InjectionToken('MEDIASTREAM_CAMERA_CONSTRAINTS');
const MEDIASTREAM_TOKEN_MICROPHONE_CONSTRAINTS = new InjectionToken('MEDIASTREAM_MICROPHONE_CONSTRAINTS');
class MediastreamCameraService extends AMediastreamCameraService {
constructor(__mediastreamCameraConstraints) {
super(__mediastreamCameraConstraints);
this.__mediastreamCameraConstraints = __mediastreamCameraConstraints;
this.mediastream$ = combineLatest([
this._toggleMediaStreamSubject.asObservable(),
this._userMedia$
]).pipe(map(([toggleCamera, media]) => {
media.getAudioTracks()
.map((track) => {
track.enabled = toggleCamera;
return track;
});
return media;
}));
}
static { this.ɵfac = function MediastreamCameraService_Factory(t) { return new (t || MediastreamCameraService)(i0.ɵɵinject(MEDIASTREAM_TOKEN_CAMERA_CONSTRAINTS)); }; }
static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: MediastreamCameraService, factory: MediastreamCameraService.ɵfac }); }
}
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(MediastreamCameraService, [{
type: Injectable
}], function () { return [{ type: undefined, decorators: [{
type: Inject,
args: [MEDIASTREAM_TOKEN_CAMERA_CONSTRAINTS]
}] }]; }, null); })();
class AMediastreamMicrophoneService extends AMediastreamService {
constructor() {
super(...arguments);
this._userMedia$ = combineLatest([
this._currentMediaDeviceSubject$
]).pipe(switchMap(([device]) => from(this.MEDIA_DEVICES.getUserMedia({
audio: this._buildMediaConstraints(device),
video: false
}))));
}
disableMicrophone() {
this._toggleMediaStreamSubject.next(false);
}
enableMicrophone() {
this._toggleMediaStreamSubject.next(true);
}
toggleMicrophone() {
const value = this._toggleMediaStreamSubject.getValue();
this._toggleMediaStreamSubject.next(!value);
}
updateMicrophoneDevice(device) {
this._currentMediaDeviceSubject$.next(device);
}
}
class MediastreamMicrophoneService extends AMediastreamMicrophoneService {
constructor(__mediastreamMicrophoneConstraints) {
super(__mediastreamMicrophoneConstraints);
this.__mediastreamMicrophoneConstraints = __mediastreamMicrophoneConstraints;
this.mediastream$ = combineLatest([
this._toggleMediaStreamSubject.asObservable(),
this._userMedia$
]).pipe(map(([toggleMicrophone, media]) => {
media.getAudioTracks()
.map((track) => {
track.enabled = toggleMicrophone;
return track;
});
return media;
}));
}
static { this.ɵfac = function MediastreamMicrophoneService_Factory(t) { return new (t || MediastreamMicrophoneService)(i0.ɵɵinject(MEDIASTREAM_TOKEN_MICROPHONE_CONSTRAINTS)); }; }
static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: MediastreamMicrophoneService, factory: MediastreamMicrophoneService.ɵfac }); }
}
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(MediastreamMicrophoneService, [{
type: Injectable
}], function () { return [{ type: undefined, decorators: [{
type: Inject,
args: [MEDIASTREAM_TOKEN_MICROPHONE_CONSTRAINTS]
}] }]; }, null); })();
class MediastreamCameraMicrophoneService {
get mediastreamCameraConstraints() {
return this._mediastreamCameraService.mediastreamConstraints;
}
get mediastreamMicrophoneConstraints() {
return this._mediastreamMicrophoneService.mediastreamConstraints;
}
constructor(_mediastreamCameraService, _mediastreamMicrophoneService) {
this._mediastreamCameraService = _mediastreamCameraService;
this._mediastreamMicrophoneService = _mediastreamMicrophoneService;
this.currentCameraMediaDevice$ = this._mediastreamCameraService.currentMediaDevice$;
this.currentMicrophoneMediaDevice$ = this._mediastreamMicrophoneService.currentMediaDevice$;
this.mediastream$ = combineLatest([
this._mediastreamCameraService.mediastream$,
this._mediastreamMicrophoneService.mediastream$
]).pipe(map(([mediastreamCamera, mediastreamMicrophone]) => {
const mediastreamCameraTracks = mediastreamCamera.getVideoTracks();
const mediastreamMicrophoneTracks = mediastreamMicrophone.getAudioTracks();
const mediastreamTracks = [
...mediastreamCameraTracks,
...mediastreamMicrophoneTracks
];
return new MediaStream(mediastreamTracks);
}));
}
disableCamera() {
this._mediastreamCameraService.disableCamera();
}
disableMicrophone() {
this._mediastreamMicrophoneService.disableMicrophone();
}
enableCamera() {
this._mediastreamCameraService.enableCamera();
}
enableMicrophone() {
this._mediastreamMicrophoneService.enableMicrophone();
}
toggleCamera() {
this._mediastreamCameraService.toggleCamera();
}
toggleMicrophone() {
this._mediastreamMicrophoneService.toggleMicrophone();
}
updateCameraDevice(device) {
this._mediastreamCameraService.updateCameraDevice(device);
}
updateMicrophoneDevice(device) {
this._mediastreamMicrophoneService.updateMicrophoneDevice(device);
}
static { this.ɵfac = function MediastreamCameraMicrophoneService_Factory(t) { return new (t || MediastreamCameraMicrophoneService)(i0.ɵɵinject(MediastreamCameraService), i0.ɵɵinject(MediastreamMicrophoneService)); }; }
static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: MediastreamCameraMicrophoneService, factory: MediastreamCameraMicrophoneService.ɵfac }); }
}
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(MediastreamCameraMicrophoneService, [{
type: Injectable
}], function () { return [{ type: MediastreamCameraService }, { type: MediastreamMicrophoneService }]; }, null); })();
class MediastreamService extends AMediastream {
constructor(_injector) {
super();
this._injector = _injector;
}
async getCameraDevices() {
return (await this.getDevices()).filter((device) => device.kind === 'videoinput');
}
async getDevices() {
return await this.MEDIA_DEVICES.enumerateDevices();
}
async getMicrophoneDevices() {
return (await this.getDevices()).filter((device) => device.kind === 'audioinput');
}
async hasCameraDevices() {
return (await this.getCameraDevices()).length !== 0;
}
async hasMicrophoneDevices() {
return (await this.getMicrophoneDevices()).length !== 0;
}
async requestCamera(constraints) {
const cameraConstraints = constraints || true;
const injector = Injector.create({
providers: [
{
provide: MediastreamCameraService,
useClass: MediastreamCameraService
}, {
provide: MEDIASTREAM_TOKEN_CAMERA_CONSTRAINTS,
useValue: cameraConstraints
}
],
parent: this._injector
});
return await this.MEDIA_DEVICES.getUserMedia({
audio: false,
video: cameraConstraints
}).then(() => injector.get(MediastreamCameraService));
}
async requestCameraAndMicrophone(constraints) {
const cameraConstraints = constraints?.video || true;
const microphoneConstraints = constraints?.audio || true;
const injector = Injector.create({
providers: [
{
provide: MediastreamCameraService,
useClass: MediastreamCameraService
}, {
provide: MediastreamCameraMicrophoneService,
useClass: MediastreamCameraMicrophoneService
}, {
provide: MediastreamMicrophoneService,
useClass: MediastreamMicrophoneService
}, {
provide: MEDIASTREAM_TOKEN_CAMERA_CONSTRAINTS,
useValue: cameraConstraints
}, {
provide: MEDIASTREAM_TOKEN_MICROPHONE_CONSTRAINTS,
useValue: microphoneConstraints
}
],
parent: this._injector
});
return await this.MEDIA_DEVICES.getUserMedia({
audio: microphoneConstraints,
video: cameraConstraints
}).then(() => injector.get(MediastreamCameraMicrophoneService));
}
async requestMicrophone(constraints) {
const microphoneConstraints = constraints || true;
const injector = Injector.create({
providers: [
{
provide: MediastreamMicrophoneService,
useClass: MediastreamMicrophoneService
}, {
provide: MEDIASTREAM_TOKEN_MICROPHONE_CONSTRAINTS,
useValue: microphoneConstraints
}
],
parent: this._injector
});
return await this.MEDIA_DEVICES.getUserMedia({
audio: microphoneConstraints,
video: false
}).then(() => injector.get(MediastreamMicrophoneService));
}
static { this.ɵfac = function MediastreamService_Factory(t) { return new (t || MediastreamService)(i0.ɵɵinject(i0.Injector)); }; }
static { this.ɵprov = /*@__PURE__*/ i0.ɵɵdefineInjectable({ token: MediastreamService, factory: MediastreamService.ɵfac }); }
}
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(MediastreamService, [{
type: Injectable
}], function () { return [{ type: i0.Injector }]; }, null); })();
const provideMediaStream = () => {
return makeEnvironmentProviders([MediastreamService]);
};
/*
* Public API Surface of mediastream
*/
/**
* Generated bundle index. Do not edit.
*/
export { MediastreamCameraMicrophoneService, MediastreamCameraService, MediastreamMicrophoneService, MediastreamService, provideMediaStream };
//# sourceMappingURL=ashetm-ng-mediastream.mjs.map