|
7 | 7 |
|
8 | 8 | import {
|
9 | 9 | Camera,
|
| 10 | + CameraCapabilities, |
| 11 | + CameraCapability, |
| 12 | + RangeCameraCapability, |
10 | 13 | CameraRenderingOptions,
|
11 | 14 | RenderedCamera,
|
12 |
| - RenderingCallbacks |
| 15 | + RenderingCallbacks, |
| 16 | + BooleanCameraCapability |
13 | 17 | } from "./core";
|
14 | 18 |
|
| 19 | +/** Interface for a range value. */ |
| 20 | +interface RangeValue { |
| 21 | + min: number; |
| 22 | + max: number; |
| 23 | + step: number; |
| 24 | +} |
| 25 | + |
| 26 | +/** Abstract camera capability class. */ |
| 27 | +abstract class AbstractCameraCapability<T> implements CameraCapability<T> { |
| 28 | + protected readonly name: string; |
| 29 | + protected readonly track: MediaStreamTrack; |
| 30 | + |
| 31 | + constructor(name: string, track: MediaStreamTrack) { |
| 32 | + this.name = name; |
| 33 | + this.track = track; |
| 34 | + } |
| 35 | + |
| 36 | + public isSupported(): boolean { |
| 37 | + return this.name in this.track.getCapabilities(); |
| 38 | + } |
| 39 | + |
| 40 | + public apply(value: T): Promise<void> { |
| 41 | + let constraint: any = {}; |
| 42 | + constraint[this.name] = value; |
| 43 | + let constraints = { advanced: [ constraint ] }; |
| 44 | + return this.track.applyConstraints(constraints); |
| 45 | + } |
| 46 | + |
| 47 | + public value(): T | null { |
| 48 | + let settings: any = this.track.getSettings(); |
| 49 | + if (this.name in settings) { |
| 50 | + let settingValue = settings[this.name]; |
| 51 | + return settingValue; |
| 52 | + } |
| 53 | + |
| 54 | + return null; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +abstract class AbstractRangeCameraCapability extends AbstractCameraCapability<number> { |
| 59 | + constructor(name: string, track: MediaStreamTrack) { |
| 60 | + super(name, track); |
| 61 | + } |
| 62 | + |
| 63 | + public min(): number { |
| 64 | + return this.getCapabilities().min; |
| 65 | + } |
| 66 | + |
| 67 | + public max(): number { |
| 68 | + return this.getCapabilities().max; |
| 69 | + } |
| 70 | + |
| 71 | + public step(): number { |
| 72 | + return this.getCapabilities().step; |
| 73 | + } |
| 74 | + |
| 75 | + public apply(value: number): Promise<void> { |
| 76 | + let constraint: any = {}; |
| 77 | + constraint[this.name] = value; |
| 78 | + let constraints = {advanced: [ constraint ]}; |
| 79 | + return this.track.applyConstraints(constraints); |
| 80 | + } |
| 81 | + |
| 82 | + private getCapabilities(): RangeValue { |
| 83 | + this.failIfNotSupported(); |
| 84 | + let capabilities: any = this.track.getCapabilities(); |
| 85 | + let capability: any = capabilities[this.name]; |
| 86 | + return { |
| 87 | + min: capability.min, |
| 88 | + max: capability.max, |
| 89 | + step: capability.step, |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + private failIfNotSupported() { |
| 94 | + if (!this.isSupported()) { |
| 95 | + throw new Error(`${this.name} capability not supported`); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/** Zoom feature. */ |
| 101 | +class ZoomFeatureImpl extends AbstractRangeCameraCapability { |
| 102 | + constructor(track: MediaStreamTrack) { |
| 103 | + super("zoom", track); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/** Torch feature. */ |
| 108 | +class TorchFeatureImpl extends AbstractCameraCapability<boolean> { |
| 109 | + constructor(track: MediaStreamTrack) { |
| 110 | + super("torch", track); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +/** Implementation of {@link CameraCapabilities}. */ |
| 115 | +class CameraCapabilitiesImpl implements CameraCapabilities { |
| 116 | + private readonly track: MediaStreamTrack; |
| 117 | + |
| 118 | + constructor(track: MediaStreamTrack) { |
| 119 | + this.track = track; |
| 120 | + } |
| 121 | + |
| 122 | + zoomFeature(): RangeCameraCapability { |
| 123 | + return new ZoomFeatureImpl(this.track); |
| 124 | + } |
| 125 | + |
| 126 | + torchFeature(): BooleanCameraCapability { |
| 127 | + return new TorchFeatureImpl(this.track); |
| 128 | + } |
| 129 | +} |
| 130 | + |
15 | 131 | /** Implementation of {@link RenderedCamera}. */
|
16 | 132 | class RenderedCameraImpl implements RenderedCamera {
|
17 | 133 |
|
@@ -55,18 +171,18 @@ class RenderedCameraImpl implements RenderedCamera {
|
55 | 171 | throw "RenderedCameraImpl video surface onerror() called";
|
56 | 172 | };
|
57 | 173 |
|
58 |
| - this.surface.addEventListener("playing", () => this.onVideoStart()); |
| 174 | + let onVideoStart = () => { |
| 175 | + const videoWidth = this.surface.clientWidth; |
| 176 | + const videoHeight = this.surface.clientHeight; |
| 177 | + this.callbacks.onRenderSurfaceReady(videoWidth, videoHeight); |
| 178 | + this.surface.removeEventListener("playing", onVideoStart); |
| 179 | + }; |
| 180 | + |
| 181 | + this.surface.addEventListener("playing", onVideoStart); |
59 | 182 | this.surface.srcObject = this.mediaStream;
|
60 | 183 | this.surface.play();
|
61 | 184 | }
|
62 | 185 |
|
63 |
| - private onVideoStart() { |
64 |
| - const videoWidth = this.surface.clientWidth; |
65 |
| - const videoHeight = this.surface.clientHeight; |
66 |
| - this.callbacks.onRenderSurfaceReady(videoWidth, videoHeight); |
67 |
| - this.surface.removeEventListener("playing", this.onVideoStart); |
68 |
| - } |
69 |
| - |
70 | 186 | static async create(
|
71 | 187 | parentElement: HTMLElement,
|
72 | 188 | mediaStream: MediaStream,
|
@@ -177,6 +293,10 @@ class RenderedCameraImpl implements RenderedCamera {
|
177 | 293 |
|
178 | 294 | });
|
179 | 295 | }
|
| 296 | + |
| 297 | + getCapabilities(): CameraCapabilities { |
| 298 | + return new CameraCapabilitiesImpl(this.getFirstTrackOrFail()); |
| 299 | + } |
180 | 300 | //#endregion
|
181 | 301 | }
|
182 | 302 |
|
|
0 commit comments