summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/camera/emscripten/SDL_camera_emscripten.c
blob: fa2a51144e4d3ec1777128e241fad97f780abb75 (plain)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
  Simple DirectMedia Layer
  Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"

#ifdef SDL_CAMERA_DRIVER_EMSCRIPTEN

#include "../SDL_syscamera.h"
#include "../SDL_camera_c.h"
#include "../../video/SDL_pixels_c.h"
#include "../../video/SDL_surface_c.h"

#include <emscripten/emscripten.h>

// just turn off clang-format for this whole file, this INDENT_OFF stuff on
//  each EM_ASM section is ugly.
/* *INDENT-OFF* */ // clang-format off

EM_JS_DEPS(sdlcamera, "$dynCall");

static bool EMSCRIPTENCAMERA_WaitDevice(SDL_Camera *device)
{
    SDL_assert(!"This shouldn't be called");  // we aren't using SDL's internal thread.
    return false;
}

static SDL_CameraFrameResult EMSCRIPTENCAMERA_AcquireFrame(SDL_Camera *device, SDL_Surface *frame, Uint64 *timestampNS)
{
    void *rgba = SDL_malloc(device->actual_spec.width * device->actual_spec.height * 4);
    if (!rgba) {
        return SDL_CAMERA_FRAME_ERROR;
    }

    *timestampNS = SDL_GetTicksNS();  // best we can do here.

    const int rc = MAIN_THREAD_EM_ASM_INT({
        const w = $0;
        const h = $1;
        const rgba = $2;
        const SDL3 = Module['SDL3'];
        if ((typeof(SDL3) === 'undefined') || (typeof(SDL3.camera) === 'undefined') || (typeof(SDL3.camera.ctx2d) === 'undefined')) {
            return 0;  // don't have something we need, oh well.
        }

        SDL3.camera.ctx2d.drawImage(SDL3.camera.video, 0, 0, w, h);
        const imgrgba = SDL3.camera.ctx2d.getImageData(0, 0, w, h).data;
        Module.HEAPU8.set(imgrgba, rgba);

        return 1;
    }, device->actual_spec.width, device->actual_spec.height, rgba);

    if (!rc) {
        SDL_free(rgba);
        return SDL_CAMERA_FRAME_ERROR;  // something went wrong, maybe shutting down; just don't return a frame.
    }

    frame->pixels = rgba;
    frame->pitch = device->actual_spec.width * 4;

    return SDL_CAMERA_FRAME_READY;
}

static void EMSCRIPTENCAMERA_ReleaseFrame(SDL_Camera *device, SDL_Surface *frame)
{
    SDL_free(frame->pixels);
}

static void EMSCRIPTENCAMERA_CloseDevice(SDL_Camera *device)
{
    if (device) {
        MAIN_THREAD_EM_ASM({
            const SDL3 = Module['SDL3'];
            if ((typeof(SDL3) === 'undefined') || (typeof(SDL3.camera) === 'undefined') || (typeof(SDL3.camera.stream) === 'undefined')) {
                return;  // camera was closed and/or subsystem was shut down, we're already done.
            }
            SDL3.camera.stream.getTracks().forEach(track => track.stop());  // stop all recording.
            SDL3.camera = {};  // dump our references to everything.
        });
        SDL_free(device->hidden);
        device->hidden = NULL;
    }
}

static int SDLEmscriptenCameraPermissionOutcome(SDL_Camera *device, int approved, int w, int h, int fps)
{
    if (approved) {
        device->actual_spec.format = SDL_PIXELFORMAT_RGBA32;
        device->actual_spec.width = w;
        device->actual_spec.height = h;
        device->actual_spec.framerate_numerator = fps;
        device->actual_spec.framerate_denominator = 1;

        if (!SDL_PrepareCameraSurfaces(device)) {
            // uhoh, we're in trouble. Probably ran out of memory.
            SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Camera could not prepare surfaces: %s ... revoking approval!", SDL_GetError());
            approved = 0;  // disconnecting the SDL camera might not be safe here, just mark it as denied by user.
        }
    }

    SDL_CameraPermissionOutcome(device, approved ? true : false);
    return approved;
}

static bool EMSCRIPTENCAMERA_OpenDevice(SDL_Camera *device, const SDL_CameraSpec *spec)
{
    MAIN_THREAD_EM_ASM({
        // Since we can't get actual specs until we make a move that prompts the user for
        // permission, we don't list any specs for the device and wrangle it during device open.
        const device = $0;
        const w = $1;
        const h = $2;
        const framerate_numerator = $3;
        const framerate_denominator = $4;
        const outcome = $5;
        const iterate = $6;

        const constraints = {};
        if ((w <= 0) || (h <= 0)) {
            constraints.video = true;   // didn't ask for anything, let the system choose.
        } else {
            constraints.video = {};  // asked for a specific thing: request it as "ideal" but take closest hardware will offer.
            constraints.video.width = w;
            constraints.video.height = h;
        }

        if ((framerate_numerator > 0) && (framerate_denominator > 0)) {
            var fps = framerate_numerator / framerate_denominator;
            constraints.video.frameRate = { ideal: fps };
        }

        function grabNextCameraFrame() {  // !!! FIXME: this (currently) runs as a requestAnimationFrame callback, for lack of a better option.
            const SDL3 = Module['SDL3'];
            if ((typeof(SDL3) === 'undefined') || (typeof(SDL3.camera) === 'undefined') || (typeof(SDL3.camera.stream) === 'undefined')) {
                return;  // camera was closed and/or subsystem was shut down, stop iterating here.
            }

            // time for a new frame from the camera?
            const nextframems = SDL3.camera.next_frame_time;
            const now = performance.now();
            if (now >= nextframems) {
                dynCall('vi', iterate, [device]);  // calls SDL_CameraThreadIterate, which will call our AcquireFrame implementation.

                // bump ahead but try to stay consistent on timing, in case we dropped frames.
                while (SDL3.camera.next_frame_time < now) {
                    SDL3.camera.next_frame_time += SDL3.camera.fpsincrms;
                }
            }

            requestAnimationFrame(grabNextCameraFrame);  // run this function again at the display framerate.  (!!! FIXME: would this be better as requestIdleCallback?)
        }

        navigator.mediaDevices.getUserMedia(constraints)
            .then((stream) => {
                const settings = stream.getVideoTracks()[0].getSettings();
                const actualw = settings.width;
                const actualh = settings.height;
                const actualfps = settings.frameRate;
                console.log("Camera is opened! Actual spec: (" + actualw + "x" + actualh + "), fps=" + actualfps);

                if (dynCall('iiiiii', outcome, [device, 1, actualw, actualh, actualfps])) {
                    const video = document.createElement("video");
                    video.width = actualw;
                    video.height = actualh;
                    video.style.display = 'none';    // we need to attach this to a hidden video node so we can read it as pixels.
                    video.srcObject = stream;

                    const canvas = document.createElement("canvas");
                    canvas.width = actualw;
                    canvas.height = actualh;
                    canvas.style.display = 'none';    // we need to attach this to a hidden video node so we can read it as pixels.

                    const ctx2d = canvas.getContext('2d');

                    const SDL3 = Module['SDL3'];
                    SDL3.camera.width = actualw;
                    SDL3.camera.height = actualh;
                    SDL3.camera.fps = actualfps;
                    SDL3.camera.fpsincrms = 1000.0 / actualfps;
                    SDL3.camera.stream = stream;
                    SDL3.camera.video = video;
                    SDL3.camera.canvas = canvas;
                    SDL3.camera.ctx2d = ctx2d;
                    SDL3.camera.next_frame_time = performance.now();

                    video.play();
                    video.addEventListener('loadedmetadata', () => {
                        grabNextCameraFrame();  // start this loop going.
                    });
                }
            })
            .catch((err) => {
                console.error("Tried to open camera but it threw an error! " + err.name + ": " +  err.message);
                dynCall('iiiiii', outcome, [device, 0, 0, 0, 0]);   // we call this a permission error, because it probably is.
            });
    }, device, spec->width, spec->height, spec->framerate_numerator, spec->framerate_denominator, SDLEmscriptenCameraPermissionOutcome, SDL_CameraThreadIterate);

    return true;  // the real work waits until the user approves a camera.
}

static void EMSCRIPTENCAMERA_FreeDeviceHandle(SDL_Camera *device)
{
    // no-op.
}

static void EMSCRIPTENCAMERA_Deinitialize(void)
{
    MAIN_THREAD_EM_ASM({
        if (typeof(Module['SDL3']) !== 'undefined') {
            Module['SDL3'].camera = undefined;
        }
    });
}

static void EMSCRIPTENCAMERA_DetectDevices(void)
{
    // `navigator.mediaDevices` is not defined if unsupported or not in a secure context!
    const int supported = MAIN_THREAD_EM_ASM_INT({ return (navigator.mediaDevices === undefined) ? 0 : 1; });

    // if we have support at all, report a single generic camera with no specs.
    //  We'll find out if there really _is_ a camera when we try to open it, but querying it for real here
    //  will pop up a user permission dialog warning them we're trying to access the camera, and we generally
    //  don't want that during SDL_Init().
    if (supported) {
        SDL_AddCamera("Web browser's camera", SDL_CAMERA_POSITION_UNKNOWN, 0, NULL, (void *) (size_t) 0x1);
    }
}

static bool EMSCRIPTENCAMERA_Init(SDL_CameraDriverImpl *impl)
{
    MAIN_THREAD_EM_ASM({
        if (typeof(Module['SDL3']) === 'undefined') {
            Module['SDL3'] = {};
        }
        Module['SDL3'].camera = {};
    });

    impl->DetectDevices = EMSCRIPTENCAMERA_DetectDevices;
    impl->OpenDevice = EMSCRIPTENCAMERA_OpenDevice;
    impl->CloseDevice = EMSCRIPTENCAMERA_CloseDevice;
    impl->WaitDevice = EMSCRIPTENCAMERA_WaitDevice;
    impl->AcquireFrame = EMSCRIPTENCAMERA_AcquireFrame;
    impl->ReleaseFrame = EMSCRIPTENCAMERA_ReleaseFrame;
    impl->FreeDeviceHandle = EMSCRIPTENCAMERA_FreeDeviceHandle;
    impl->Deinitialize = EMSCRIPTENCAMERA_Deinitialize;

    impl->ProvidesOwnCallbackThread = true;

    return true;
}

CameraBootStrap EMSCRIPTENCAMERA_bootstrap = {
    "emscripten", "SDL Emscripten MediaStream camera driver", EMSCRIPTENCAMERA_Init, false
};

/* *INDENT-ON* */ // clang-format on

#endif // SDL_CAMERA_DRIVER_EMSCRIPTEN