Houjie
2025-04-11 1bf977929dd324f3ac64b70debd8a79443c54392
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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());
    });
};
class AbstractCameraCapability {
    constructor(name, track) {
        this.name = name;
        this.track = track;
    }
    isSupported() {
        if (!this.track.getCapabilities) {
            return false;
        }
        return this.name in this.track.getCapabilities();
    }
    apply(value) {
        let constraint = {};
        constraint[this.name] = value;
        let constraints = { advanced: [constraint] };
        return this.track.applyConstraints(constraints);
    }
    value() {
        let settings = this.track.getSettings();
        if (this.name in settings) {
            let settingValue = settings[this.name];
            return settingValue;
        }
        return null;
    }
}
class AbstractRangeCameraCapability extends AbstractCameraCapability {
    constructor(name, track) {
        super(name, track);
    }
    min() {
        return this.getCapabilities().min;
    }
    max() {
        return this.getCapabilities().max;
    }
    step() {
        return this.getCapabilities().step;
    }
    apply(value) {
        let constraint = {};
        constraint[this.name] = value;
        let constraints = { advanced: [constraint] };
        return this.track.applyConstraints(constraints);
    }
    getCapabilities() {
        this.failIfNotSupported();
        let capabilities = this.track.getCapabilities();
        let capability = capabilities[this.name];
        return {
            min: capability.min,
            max: capability.max,
            step: capability.step,
        };
    }
    failIfNotSupported() {
        if (!this.isSupported()) {
            throw new Error(`${this.name} capability not supported`);
        }
    }
}
class ZoomFeatureImpl extends AbstractRangeCameraCapability {
    constructor(track) {
        super("zoom", track);
    }
}
class TorchFeatureImpl extends AbstractCameraCapability {
    constructor(track) {
        super("torch", track);
    }
}
class CameraCapabilitiesImpl {
    constructor(track) {
        this.track = track;
    }
    zoomFeature() {
        return new ZoomFeatureImpl(this.track);
    }
    torchFeature() {
        return new TorchFeatureImpl(this.track);
    }
}
class RenderedCameraImpl {
    constructor(parentElement, mediaStream, callbacks) {
        this.isClosed = false;
        this.parentElement = parentElement;
        this.mediaStream = mediaStream;
        this.callbacks = callbacks;
        this.surface = this.createVideoElement(this.parentElement.clientWidth);
        parentElement.append(this.surface);
    }
    createVideoElement(width) {
        const videoElement = document.createElement("video");
        videoElement.style.width = `${width}px`;
        videoElement.style.display = "block";
        videoElement.muted = true;
        videoElement.setAttribute("muted", "true");
        videoElement.playsInline = true;
        return videoElement;
    }
    setupSurface() {
        this.surface.onabort = () => {
            throw "RenderedCameraImpl video surface onabort() called";
        };
        this.surface.onerror = () => {
            throw "RenderedCameraImpl video surface onerror() called";
        };
        let onVideoStart = () => {
            const videoWidth = this.surface.clientWidth;
            const videoHeight = this.surface.clientHeight;
            this.callbacks.onRenderSurfaceReady(videoWidth, videoHeight);
            this.surface.removeEventListener("playing", onVideoStart);
        };
        this.surface.addEventListener("playing", onVideoStart);
        this.surface.srcObject = this.mediaStream;
        this.surface.play();
    }
    static create(parentElement, mediaStream, options, callbacks) {
        return __awaiter(this, void 0, void 0, function* () {
            let renderedCamera = new RenderedCameraImpl(parentElement, mediaStream, callbacks);
            if (options.aspectRatio) {
                let aspectRatioConstraint = {
                    aspectRatio: options.aspectRatio
                };
                yield renderedCamera.getFirstTrackOrFail().applyConstraints(aspectRatioConstraint);
            }
            renderedCamera.setupSurface();
            return renderedCamera;
        });
    }
    failIfClosed() {
        if (this.isClosed) {
            throw "The RenderedCamera has already been closed.";
        }
    }
    getFirstTrackOrFail() {
        this.failIfClosed();
        if (this.mediaStream.getVideoTracks().length === 0) {
            throw "No video tracks found";
        }
        return this.mediaStream.getVideoTracks()[0];
    }
    pause() {
        this.failIfClosed();
        this.surface.pause();
    }
    resume(onResumeCallback) {
        this.failIfClosed();
        let $this = this;
        const onVideoResume = () => {
            setTimeout(onResumeCallback, 200);
            $this.surface.removeEventListener("playing", onVideoResume);
        };
        this.surface.addEventListener("playing", onVideoResume);
        this.surface.play();
    }
    isPaused() {
        this.failIfClosed();
        return this.surface.paused;
    }
    getSurface() {
        this.failIfClosed();
        return this.surface;
    }
    getRunningTrackCapabilities() {
        return this.getFirstTrackOrFail().getCapabilities();
    }
    getRunningTrackSettings() {
        return this.getFirstTrackOrFail().getSettings();
    }
    applyVideoConstraints(constraints) {
        return __awaiter(this, void 0, void 0, function* () {
            if ("aspectRatio" in constraints) {
                throw "Changing 'aspectRatio' in run-time is not yet supported.";
            }
            return this.getFirstTrackOrFail().applyConstraints(constraints);
        });
    }
    close() {
        if (this.isClosed) {
            return Promise.resolve();
        }
        let $this = this;
        return new Promise((resolve, _) => {
            let tracks = $this.mediaStream.getVideoTracks();
            const tracksToClose = tracks.length;
            var tracksClosed = 0;
            $this.mediaStream.getVideoTracks().forEach((videoTrack) => {
                $this.mediaStream.removeTrack(videoTrack);
                videoTrack.stop();
                ++tracksClosed;
                if (tracksClosed >= tracksToClose) {
                    $this.isClosed = true;
                    $this.parentElement.removeChild($this.surface);
                    resolve();
                }
            });
        });
    }
    getCapabilities() {
        return new CameraCapabilitiesImpl(this.getFirstTrackOrFail());
    }
}
export class CameraImpl {
    constructor(mediaStream) {
        this.mediaStream = mediaStream;
    }
    render(parentElement, options, callbacks) {
        return __awaiter(this, void 0, void 0, function* () {
            return RenderedCameraImpl.create(parentElement, this.mediaStream, options, callbacks);
        });
    }
    static create(videoConstraints) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!navigator.mediaDevices) {
                throw "navigator.mediaDevices not supported";
            }
            let constraints = {
                audio: false,
                video: videoConstraints
            };
            let mediaStream = yield navigator.mediaDevices.getUserMedia(constraints);
            return new CameraImpl(mediaStream);
        });
    }
}
//# sourceMappingURL=core-impl.js.map