Coverage Report

Created: 2026-12-01 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gpac/src/jsmods/scene_js.c
Line
Count
Source
1
/*
2
 *      GPAC - Multimedia Framework C SDK
3
 *
4
 *      Authors: Jean Le Feuvre
5
 *      Copyright (c) Telecom ParisTech 2007-2025
6
 *      All rights reserved
7
 *
8
 *  This file is part of GPAC / JavaScript Compositor extensions
9
 *
10
 *  GPAC is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU Lesser General Public License as published by
12
 *  the Free Software Foundation; either version 2, or (at your option)
13
 *  any later version.
14
 *
15
 *  GPAC is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU Lesser General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU Lesser General Public
21
 *  License along with this library; see the file COPYING.  If not, write to
22
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
 *
24
 */
25
26
27
#include <gpac/setup.h>
28
29
#if defined(GPAC_HAS_QJS) && !defined(GPAC_DISABLE_COMPOSITOR)
30
31
/*base SVG type*/
32
#include <gpac/nodes_svg.h>
33
#include <gpac/nodes_mpeg4.h>
34
#include <gpac/nodes_x3d.h>
35
/*dom events*/
36
#include <gpac/events.h>
37
38
#include <gpac/download.h>
39
#include <gpac/network.h>
40
#include <gpac/xml.h>
41
42
43
#include <gpac/internal/scenegraph_dev.h>
44
45
#include "../scenegraph/qjs_common.h"
46
47
#include <gpac/internal/compositor_dev.h>
48
49
typedef struct
50
{
51
  GF_Compositor *compositor;
52
  JSValue evt_fun;
53
  GF_FSEventListener evt_filter;
54
  GF_Event *evt;
55
56
  JSContext *c;
57
  JSValue evt_filter_obj, evt_obj, scene_obj;
58
59
  GF_SystemRTInfo rti;
60
61
  //list of config files for storage
62
  GF_List *storages;
63
64
  GF_List *event_queue;
65
  GF_Mutex *event_mx;
66
67
  Bool owns_fs_api;
68
} GF_SCENEJSExt;
69
70
enum {
71
  GJS_OM_PROP_ID=1,
72
  GJS_OM_PROP_NB_RES,
73
  GJS_OM_PROP_URL,
74
  GJS_OM_PROP_DUR,
75
  GJS_OM_PROP_CLOCK,
76
  GJS_OM_PROP_DRIFT,
77
  GJS_OM_PROP_STATUS,
78
  GJS_OM_PROP_BUFFER,
79
  GJS_OM_PROP_DB_COUNT,
80
  GJS_OM_PROP_CB_COUNT,
81
  GJS_OM_PROP_CB_CAP,
82
  GJS_OM_PROP_TYPE,
83
  GJS_OM_PROP_SAMPLERATE,
84
  GJS_OM_PROP_CHANNELS,
85
  GJS_OM_PROP_LANG,
86
  GJS_OM_PROP_WIDTH,
87
  GJS_OM_PROP_HEIGHT,
88
  GJS_OM_PROP_PIXELFORMAT,
89
  GJS_OM_PROP_PAR,
90
  GJS_OM_PROP_DEC_FRAMES,
91
  GJS_OM_PROP_DROP_FRAMES,
92
  GJS_OM_PROP_DEC_TIME_MAX,
93
  GJS_OM_PROP_DEC_TIME_TOTAL,
94
  GJS_OM_PROP_AVG_RATE,
95
  GJS_OM_PROP_MAX_RATE,
96
  GJS_OM_PROP_SERVICE_HANDLER,
97
  GJS_OM_PROP_CODEC,
98
  GJS_OM_PROP_NB_VIEWS,
99
  GJS_OM_PROP_NB_QUALITIES,
100
  GJS_OM_PROP_MAX_BUFFER,
101
  GJS_OM_PROP_MIN_BUFFER,
102
  GJS_OM_PROP_FRAME_DUR,
103
  GJS_OM_PROP_NB_IRAP,
104
  GJS_OM_PROP_IRAP_DEC_TIME,
105
  GJS_OM_PROP_IRAP_MAX_TIME,
106
  GJS_OM_PROP_SERVICE_ID,
107
  GJS_OM_PROP_SELECTED_SERVICE,
108
  GJS_OM_PROP_BANDWIDTH_DOWN,
109
  GJS_OM_PROP_NB_HTTP,
110
  GJS_OM_PROP_TIMESHIFT_DEPTH,
111
  GJS_OM_PROP_TIMESHIFT_TIME,
112
  GJS_OM_PROP_IS_ADDON,
113
  GJS_OM_PROP_MAIN_ADDON_ON,
114
  GJS_OM_PROP_IS_OVER,
115
  GJS_OM_PROP_IS_VR_SCENE,
116
  GJS_OM_PROP_DYNAMIC_SCENE,
117
  GJS_OM_PROP_SERVICE_NAME,
118
  GJS_OM_PROP_NTP_DIFF,
119
  GJS_OM_PROP_MAIN_ADDON_URL,
120
  GJS_OM_PROP_REVERSE_PLAYBACK,
121
  GJS_OM_PROP_SCALABLE_ENHANCEMENT,
122
  GJS_OM_PROP_MAIN_ADDON_MEDIATIME,
123
  GJS_OM_PROP_DEPENDENT_GROUPS,
124
  GJS_OM_PROP_DISABLED,
125
  GJS_OM_PROP_NTP_SENDER_DIFF,
126
  GJS_OM_PROP_BUFFERING,
127
  GJS_OM_PROP_FORCED_SUB,
128
};
129
130
enum {
131
  GJS_SCENE_PROP_FULLSCREEN=1,
132
  GJS_SCENE_PROP_CURRENT_PATH,
133
  GJS_SCENE_PROP_VOLUME,
134
  GJS_SCENE_PROP_NAVIGATION,
135
  GJS_SCENE_PROP_NAVIGATION_TYPE,
136
  GJS_SCENE_PROP_HARDWARE_YUV,
137
  GJS_SCENE_PROP_HARDWARE_RGB,
138
  GJS_SCENE_PROP_HARDWARE_RGBA,
139
  GJS_SCENE_PROP_HARDWARE_STRETCH,
140
  GJS_SCENE_PROP_SCREEN_WIDTH,
141
  GJS_SCENE_PROP_SCREEN_HEIGHT,
142
  GJS_SCENE_PROP_FPS,
143
  GJS_SCENE_PROP_CAPTION,
144
  GJS_SCENE_PROP_FOCUS_HIGHLIGHT,
145
  GJS_SCENE_PROP_DPI_X,
146
  GJS_SCENE_PROP_DPI_Y,
147
  GJS_SCENE_PROP_SENSORS_ACTIVE,
148
  GJS_SCENE_PROP_SIM_FPS,
149
  GJS_SCENE_PROP_HAS_OPENGL,
150
  GJS_SCENE_PROP_ZOOM,
151
  GJS_SCENE_PROP_TEXT_SEL,
152
  GJS_SCENE_PROP_DISP_ORIENTATION,
153
154
};
155
156
enum {
157
  GJS_EVT_PROP_KEYCODE = 1,
158
  GJS_EVT_PROP_MOUSE_X,
159
  GJS_EVT_PROP_MOUSE_Y,
160
  GJS_EVT_PROP_PICKED,
161
  GJS_EVT_PROP_WHEEL,
162
  GJS_EVT_PROP_BUTTON,
163
  GJS_EVT_PROP_TYPE,
164
  GJS_EVT_PROP_NAME,
165
  GJS_EVT_PROP_HWKEY,
166
  GJS_EVT_PROP_TARGET_URL,
167
  GJS_EVT_PROP_FILES,
168
};
169
170
static JSClassID scene_class_id = 0;
171
static JSClassID odm_class_id = 0;
172
static JSClassID gpacevt_class_id = 0;
173
static JSClassID any_class_id = 0;
174
175
static void scenejs_finalize(JSRuntime *rt, JSValue obj);
176
177
static void scenejs_gc_mark(JSRuntime *rt, JSValueConst val, JS_MarkFunc *mark_func)
178
0
{
179
0
  GF_SCENEJSExt *ext = JS_GetOpaque(val, scene_class_id);
180
0
  if (ext) {
181
0
    JS_MarkValue(rt, ext->evt_fun, mark_func);
182
0
  }
183
0
}
184
185
JSClassDef sceneClass = {
186
  "JSSCENE",
187
  .finalizer = scenejs_finalize,
188
  .gc_mark = scenejs_gc_mark
189
};
190
JSClassDef gpacEvtClass = {
191
  "GPACEVT"
192
};
193
JSClassDef odmClass = {
194
  "MediaObject"
195
};
196
JSClassDef anyClass = {
197
  "GPACOBJECT"
198
};
199
200
201
static GF_Compositor *scenejs_get_compositor(JSContext *c, JSValue obj)
202
0
{
203
0
  GF_SCENEJSExt *ext = JS_GetOpaque(obj, scene_class_id);
204
0
  return ext ? ext->compositor : NULL;
205
0
}
206
207
static JSValue scenejs_getProperty(JSContext *ctx, JSValueConst this_val, int prop_id)
208
0
{
209
0
  Bool bval;
210
0
  JSValue ret;
211
0
  char *str;
212
0
  GF_SCENEJSExt *ext = JS_GetOpaque(this_val, scene_class_id);
213
0
  GF_Compositor *compositor = ext ? ext->compositor : NULL;
214
0
  if (!ext || !compositor) return GF_JS_EXCEPTION(ctx);
215
216
0
  switch (prop_id) {
217
218
0
  case GJS_SCENE_PROP_FULLSCREEN:
219
0
    return JS_NewBool(ctx, compositor->fullscreen);
220
221
0
  case GJS_SCENE_PROP_CURRENT_PATH:
222
0
    str = gf_url_concatenate(compositor->root_scene->root_od->scene_ns->url, "");
223
0
    if (!str) str = gf_strdup("");
224
0
    ret = JS_NewString(ctx, str);
225
0
    gf_free(str);
226
0
    return ret;
227
228
0
  case GJS_SCENE_PROP_VOLUME:
229
0
    return JS_NewInt32(ctx, gf_sc_get_option(compositor, GF_OPT_AUDIO_VOLUME));
230
231
0
  case GJS_SCENE_PROP_NAVIGATION:
232
0
    return JS_NewInt32(ctx, gf_sc_get_option(compositor, GF_OPT_NAVIGATION));
233
234
0
  case GJS_SCENE_PROP_NAVIGATION_TYPE:
235
0
    return JS_NewInt32(ctx, gf_sc_get_option(compositor, GF_OPT_NAVIGATION_TYPE) );
236
237
0
  case GJS_SCENE_PROP_HARDWARE_YUV:
238
0
    return JS_NewBool(ctx, (compositor->video_out->hw_caps & GF_VIDEO_HW_HAS_YUV) ? 1 : 0 );
239
240
0
  case GJS_SCENE_PROP_HARDWARE_RGB:
241
0
    return JS_NewBool(ctx, (compositor->video_out->hw_caps & GF_VIDEO_HW_HAS_RGB) ? 1 : 0 );
242
243
0
  case GJS_SCENE_PROP_HARDWARE_RGBA:
244
0
    bval = (compositor->video_out->hw_caps & GF_VIDEO_HW_HAS_RGBA) ? 1 : 0;
245
#ifndef GPAC_DISABLE_3D
246
    if (compositor->hybrid_opengl || compositor->is_opengl) bval = 1;
247
#endif
248
0
    return JS_NewBool(ctx, bval);
249
250
0
  case GJS_SCENE_PROP_HARDWARE_STRETCH:
251
0
    return JS_NewBool(ctx, (compositor->video_out->hw_caps & GF_VIDEO_HW_HAS_STRETCH) ? 1 : 0 );
252
253
0
  case GJS_SCENE_PROP_SCREEN_WIDTH:
254
0
    if (compositor->osize.x)
255
0
      return JS_NewInt32(ctx, compositor->osize.x);
256
0
    return JS_NewInt32(ctx, compositor->video_out->max_screen_width);
257
258
0
  case GJS_SCENE_PROP_SCREEN_HEIGHT:
259
0
    if (compositor->osize.y)
260
0
      return JS_NewInt32(ctx, compositor->osize.y);
261
0
    return JS_NewInt32(ctx, compositor->video_out->max_screen_height);
262
263
0
  case GJS_SCENE_PROP_FPS:
264
0
    return JS_NewFloat64(ctx, gf_sc_get_fps(compositor, 0) );
265
266
0
  case GJS_SCENE_PROP_SIM_FPS:
267
0
    return JS_NewFloat64(ctx, ((Double)compositor->fps.den)/compositor->fps.num);
268
269
0
  case GJS_SCENE_PROP_HAS_OPENGL:
270
0
    return JS_NewBool(ctx, (compositor->ogl != GF_SC_GLMODE_OFF) ? 1 : 0);
271
272
0
  case GJS_SCENE_PROP_DPI_X:
273
0
    return JS_NewInt32(ctx, compositor->video_out->dpi_x);
274
275
0
  case GJS_SCENE_PROP_DPI_Y:
276
0
    return JS_NewInt32(ctx, compositor->video_out->dpi_y);
277
278
0
  case GJS_SCENE_PROP_SENSORS_ACTIVE:
279
0
    return JS_NewBool(ctx, compositor->orientation_sensors_active);
280
281
0
  case GJS_SCENE_PROP_FOCUS_HIGHLIGHT:
282
0
    return JS_NewBool(ctx, !compositor->disable_focus_highlight);
283
284
0
  case GJS_SCENE_PROP_ZOOM:
285
0
    if (compositor->root_scene && compositor->root_scene->graph->script_action) {
286
0
      GF_JSAPIParam jspar;
287
0
      memset(&jspar, 0, sizeof(GF_JSAPIParam));
288
0
      compositor->root_scene->graph->script_action(compositor->root_scene->graph->script_action_cbck, GF_JSAPI_OP_GET_SCALE, NULL, &jspar);
289
#ifdef GPAC_ENABLE_COVERAGE
290
      if (gf_sys_is_cov_mode()) {
291
        compositor->root_scene->graph->script_action(compositor->root_scene->graph->script_action_cbck, GF_JSAPI_OP_GET_VIEWPORT, NULL, &jspar);
292
      }
293
#endif
294
0
      return JS_NewFloat64(ctx, FIX2FLT(jspar.val) );
295
0
    } else {
296
0
      return JS_NewFloat64(ctx, 1.0 );
297
0
    }
298
0
    break;
299
0
  case GJS_SCENE_PROP_TEXT_SEL:
300
0
    str = (char *) gf_sc_get_selected_text(compositor);
301
0
    if (!str) str = "";
302
0
    return JS_NewString(ctx, str);
303
304
305
0
  case GJS_SCENE_PROP_DISP_ORIENTATION:
306
0
    return JS_NewInt32(ctx, compositor->disp_ori);
307
0
  }
308
0
  return JS_UNDEFINED;
309
0
}
310
311
312
static JSValue scenejs_setProperty(JSContext *ctx, JSValueConst this_val, JSValueConst value, int magic)
313
0
{
314
0
#ifndef GPAC_CONFIG_IOS
315
0
  Bool bval;
316
0
#endif
317
0
  s32 ival;
318
0
  Double dval;
319
0
  const char *prop_val;
320
0
  GF_SCENEJSExt *ext = JS_GetOpaque(this_val, scene_class_id);
321
0
  GF_Compositor *compositor = ext ? ext->compositor : NULL;
322
0
  if (!ext || !compositor) return GF_JS_EXCEPTION(ctx);
323
324
0
  switch (magic) {
325
0
  case GJS_SCENE_PROP_CAPTION:
326
0
  {
327
0
    GF_Event evt;
328
0
    if (!JS_IsString(value)) return GF_JS_EXCEPTION(ctx);
329
0
    prop_val = JS_ToCString(ctx, value);
330
0
    evt.type = GF_EVENT_SET_CAPTION;
331
0
    if (prop_val && !strnicmp(prop_val, "gpac://", 7)) {
332
0
      evt.caption.caption = prop_val + 7;
333
0
    } else {
334
0
      evt.caption.caption = prop_val;
335
0
    }
336
0
    gf_sc_user_event(compositor, &evt);
337
0
    JS_FreeCString(ctx, prop_val);
338
0
  }
339
0
    break;
340
0
  case GJS_SCENE_PROP_FULLSCREEN:
341
    /*no fullscreen for iOS (always on)*/
342
0
#ifndef GPAC_CONFIG_IOS
343
0
    bval = JS_ToBool(ctx, value);
344
0
    if (compositor->fullscreen != bval) {
345
0
      gf_sc_set_option(compositor, GF_OPT_FULLSCREEN, bval);
346
0
    }
347
0
#endif
348
0
    break;
349
0
  case GJS_SCENE_PROP_VOLUME:
350
0
    if (!JS_ToFloat64(ctx, &dval, value)) {
351
0
      gf_sc_set_option(compositor, GF_OPT_AUDIO_VOLUME, (u32) dval);
352
0
    } else if (!JS_ToInt32(ctx, &ival, value)) {
353
0
      gf_sc_set_option(compositor, GF_OPT_AUDIO_VOLUME, (u32) ival);
354
0
    }
355
0
    break;
356
357
0
  case GJS_SCENE_PROP_NAVIGATION:
358
0
    if (JS_ToInt32(ctx, &ival, value)) return GF_JS_EXCEPTION(ctx);
359
0
    gf_sc_set_option(compositor, GF_OPT_NAVIGATION, (u32) ival);
360
0
    break;
361
0
  case GJS_SCENE_PROP_NAVIGATION_TYPE:
362
0
    gf_sc_set_option(compositor, GF_OPT_NAVIGATION_TYPE, 0);
363
0
    break;
364
0
  case GJS_SCENE_PROP_FOCUS_HIGHLIGHT:
365
0
    compositor->disable_focus_highlight = !JS_ToBool(ctx, value);
366
0
    break;
367
0
  case GJS_SCENE_PROP_SENSORS_ACTIVE:
368
0
  {
369
0
    GF_Event evt;
370
0
    compositor->orientation_sensors_active = JS_ToBool(ctx, value);
371
    //send activation for sensors
372
0
    memset(&evt, 0, sizeof(GF_Event));
373
0
    evt.type = GF_EVENT_SENSOR_REQUEST;
374
0
    evt.activate_sensor.activate = compositor->orientation_sensors_active;
375
0
    evt.activate_sensor.sensor_type = GF_EVENT_SENSOR_ORIENTATION;
376
0
    if (gf_sc_send_event(compositor, &evt) == GF_FALSE) {
377
0
      compositor->orientation_sensors_active = GF_FALSE;
378
0
    }
379
0
  }
380
0
    break;
381
0
  case GJS_SCENE_PROP_DISP_ORIENTATION:
382
0
    if (JS_ToInt32(ctx, &ival, value)) return GF_JS_EXCEPTION(ctx);
383
0
    if (compositor->video_out && compositor->video_out->ProcessEvent) {
384
0
      GF_Event evt;
385
0
      memset(&evt, 0, sizeof(GF_Event));
386
0
      evt.size.type = GF_EVENT_SET_ORIENTATION;
387
0
      evt.size.orientation = ival;
388
0
      compositor->video_out->ProcessEvent(compositor->video_out, &evt);
389
0
    }
390
0
    break;
391
0
  }
392
0
  return JS_UNDEFINED;
393
0
}
394
395
static JSValue scenejs_get_option(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
396
0
{
397
0
  const char *opt = NULL;
398
0
  const char *sec_name, *key_name;
399
0
  char arg_val[GF_PROP_DUMP_ARG_SIZE];
400
0
  s32 idx = -1;
401
0
  GF_Compositor *compositor = scenejs_get_compositor(ctx, this_val);
402
0
  if (!compositor) return GF_JS_EXCEPTION(ctx);
403
0
  if (argc < 2) return GF_JS_EXCEPTION(ctx);
404
0
  if (!JS_IsString(argv[0])) return GF_JS_EXCEPTION(ctx);
405
0
  if (!JS_IsString(argv[1]) && !JS_IsNumber(argv[1])) return GF_JS_EXCEPTION(ctx);
406
407
0
  sec_name = JS_ToCString(ctx, argv[0]);
408
0
  key_name = NULL;
409
0
  if (JS_IsNumber(argv[1])) {
410
0
    JS_ToInt32(ctx, &idx, argv[1]);
411
0
  } else if (JS_IsString(argv[1]) ) {
412
0
    key_name = JS_ToCString(ctx, argv[1]);
413
0
  }
414
415
0
  if (sec_name && !stricmp(sec_name, "core") && key_name && !strcmp(key_name, "version")) {
416
0
    opt = gf_gpac_version();
417
0
  } else if (sec_name && key_name) {
418
0
    if (!strcmp(sec_name, "Compositor")) {
419
0
      opt = gf_filter_get_arg_str(compositor->filter, key_name, arg_val);
420
0
    } else {
421
0
      opt = gf_opts_get_key(sec_name, key_name);
422
0
    }
423
0
  } else if (sec_name && (idx>=0)) {
424
0
    opt = gf_opts_get_key_name(sec_name, idx);
425
0
  } else {
426
0
    opt = NULL;
427
0
  }
428
429
0
  JS_FreeCString(ctx, key_name);
430
0
  JS_FreeCString(ctx, sec_name);
431
432
0
  if (opt) {
433
0
    return JS_NewString(ctx, opt);
434
0
  }
435
0
  return JS_NULL;
436
0
}
437
438
static JSValue scenejs_set_option(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
439
0
{
440
0
  const char *sec_name, *key_name, *key_val;
441
0
  GF_Compositor *compositor = scenejs_get_compositor(ctx, this_val);
442
0
  if (!compositor) return GF_JS_EXCEPTION(ctx);
443
0
  if (argc < 3) return GF_JS_EXCEPTION(ctx);
444
0
  if (!JS_IsString(argv[0])) return GF_JS_EXCEPTION(ctx);
445
0
  if (!JS_IsString(argv[1])) return GF_JS_EXCEPTION(ctx);
446
447
0
  sec_name = JS_ToCString(ctx, argv[0]);
448
0
  key_name = JS_ToCString(ctx, argv[1]);
449
0
  key_val = NULL;
450
0
  if (JS_IsString(argv[2]))
451
0
    key_val = JS_ToCString(ctx, argv[2]);
452
453
0
  if (!strcmp(sec_name, "Compositor")) {
454
0
    gf_filter_send_update(compositor->filter, NULL, key_name, key_val, 0);
455
0
  } else {
456
0
    gf_opts_set_key(sec_name, key_name, key_val);
457
0
  }
458
0
  JS_FreeCString(ctx, sec_name);
459
0
  JS_FreeCString(ctx, key_name);
460
0
  if (key_val) {
461
0
    JS_FreeCString(ctx, key_val);
462
0
  }
463
0
  return JS_UNDEFINED;
464
0
}
465
466
467
static JSValue scenejs_set_back_color(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
468
0
{
469
0
  u32 r, g, b, a, i;
470
0
  GF_Compositor *compositor = scenejs_get_compositor(ctx, this_val);
471
0
  if (!compositor) return GF_JS_EXCEPTION(ctx);
472
0
  if (argc < 3) return GF_JS_EXCEPTION(ctx);
473
0
  r = g = b = 0;
474
0
  a = 255;
475
0
  for (i=0; i<(u32) argc; i++) {
476
0
    Double d;
477
0
    u32 v;
478
0
    if (! JS_ToFloat64(ctx, &d, argv[i])) {
479
0
    } else {
480
0
      return GF_JS_EXCEPTION(ctx);
481
0
    }
482
0
    d*=255;
483
0
    v = 0;
484
0
    if (d>0) {
485
0
      v = (u32) d;
486
0
      if (v>255)  v = 255;
487
0
    }
488
0
    if (i==0) r = v;
489
0
    else if (i==1) g = v;
490
0
    else if (i==2) b = v;
491
0
    else if (i==3) a = v;
492
0
  }
493
0
  compositor->back_color = compositor->bc = GF_COL_ARGB(a, r, g, b);
494
0
  gf_sc_invalidate(compositor, NULL);
495
0
  return JS_UNDEFINED;
496
0
}
497
498
499
static JSValue scenejs_switch_quality(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
500
0
{
501
0
  GF_Compositor *compositor = scenejs_get_compositor(ctx, this_val);
502
0
  if (!compositor) return GF_JS_EXCEPTION(ctx);
503
0
  if (argc < 1) return GF_JS_EXCEPTION(ctx);
504
505
0
  if (!JS_IsBool(argv[0]))
506
0
    return GF_JS_EXCEPTION(ctx);
507
0
  Bool up = JS_ToBool(ctx, argv[0]) ? GF_TRUE : GF_FALSE;
508
0
  gf_scene_switch_quality(compositor->root_scene, up);
509
0
  return JS_UNDEFINED;
510
0
}
511
512
513
#if 0 //unused
514
static JSValue scenejs_reload(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
515
{
516
  GF_Event evt;
517
  GF_Compositor *compositor = scenejs_get_compositor(ctx, this_val);
518
  if (!compositor) return GF_JS_EXCEPTION(ctx);
519
520
  memset(&evt, 0, sizeof(GF_Event));
521
  evt.type = GF_EVENT_RELOAD;
522
  gf_filter_ui_event(compositor->filter, &evt);
523
  return JS_UNDEFINED;
524
}
525
#endif
526
527
static JSValue scenejs_navigation_supported(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
528
0
{
529
0
  u32 type;
530
0
  GF_Compositor *compositor = scenejs_get_compositor(ctx, this_val);
531
0
  if (!compositor) return GF_JS_EXCEPTION(ctx);
532
0
  if (argc < 1) return GF_JS_EXCEPTION(ctx);
533
534
0
  if (! JS_IsInteger(argv[0]) ) {
535
0
    return JS_NewBool(ctx, 0);
536
0
  }
537
0
  if (JS_ToInt32(ctx, &type, argv[0]))
538
0
    return GF_JS_EXCEPTION(ctx);
539
0
  return JS_NewBool(ctx, gf_sc_navigation_supported(compositor, type) ? 1 : 0 );
540
0
}
541
542
static JSValue scenejs_set_size(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
543
0
{
544
0
  Bool override_size_info = 0;
545
0
  u32 w, h;
546
0
  GF_Compositor *compositor = scenejs_get_compositor(ctx, this_val);
547
0
  if (!compositor) return GF_JS_EXCEPTION(ctx);
548
0
  if (argc<2) return GF_JS_EXCEPTION(ctx);
549
550
0
  w = h = 0;
551
0
  if (JS_ToInt32(ctx, &w, argv[0]))
552
0
    return GF_JS_EXCEPTION(ctx);
553
0
  if (JS_ToInt32(ctx, &h, argv[1]))
554
0
    return GF_JS_EXCEPTION(ctx);
555
556
0
  if (argc > 2)
557
0
    override_size_info = JS_ToBool(ctx, argv[2]);
558
559
0
  if (w && h) {
560
0
    GF_Event evt;
561
0
    if (override_size_info) {
562
0
      compositor->scene_width = w;
563
0
      compositor->scene_height = h;
564
0
      compositor->has_size_info = 1;
565
0
      compositor->recompute_ar = 1;
566
0
      return JS_UNDEFINED;
567
0
    }
568
0
    if (compositor->os_wnd) {
569
0
      evt.type = GF_EVENT_SCENE_SIZE;
570
0
      evt.size.width = w;
571
0
      evt.size.height = h;
572
0
      gf_sc_send_event(compositor, &evt);
573
0
    } else {
574
0
      gf_sc_set_size(compositor, w, h);
575
0
    }
576
0
  } else if (override_size_info) {
577
0
    compositor->has_size_info = 0;
578
0
    compositor->recompute_ar = 1;
579
0
  }
580
581
0
  return JS_UNDEFINED;
582
0
}
583
584
585
static JSValue scenejs_exit(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
586
0
{
587
0
  GF_Event evt;
588
0
  GF_Compositor *compositor = scenejs_get_compositor(ctx, this_val);
589
0
  memset(&evt, 0, sizeof(GF_Event));
590
0
  evt.type = GF_EVENT_QUIT;
591
0
  if (argc)
592
0
    JS_ToInt32(ctx, (s32 *) &evt.message.error, argv[0]);
593
    
594
0
  gf_sc_send_event(compositor, &evt);
595
0
  return JS_UNDEFINED;
596
0
}
597
598
static JSValue scenejs_set_3d(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
599
0
{
600
0
  Bool type_3d;
601
0
  GF_Compositor *compositor = scenejs_get_compositor(ctx, this_val);
602
0
  if (!argc) return GF_JS_EXCEPTION(ctx);
603
0
  type_3d = JS_ToBool(ctx, argv[0]);
604
605
0
  if (compositor->inherit_type_3d != type_3d) {
606
0
    compositor->inherit_type_3d = type_3d;
607
0
    compositor->root_visual_setup = 0;
608
0
    gf_sc_reset_graphics(compositor);
609
0
  }
610
0
  return JS_UNDEFINED;
611
0
}
612
613
static JSValue scenejs_move_window(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
614
0
{
615
0
  GF_Event evt;
616
0
  GF_Compositor *compositor = scenejs_get_compositor(ctx, this_val);
617
0
  if (argc < 2) return GF_JS_EXCEPTION(ctx);
618
619
0
  evt.type = GF_EVENT_MOVE;
620
0
  evt.move.relative = 1;
621
0
  if (JS_ToInt32(ctx, &evt.move.x, argv[0]))
622
0
    return GF_JS_EXCEPTION(ctx);
623
0
  if (JS_ToInt32(ctx, &evt.move.y, argv[1]))
624
0
    return GF_JS_EXCEPTION(ctx);
625
626
0
  if (argc ==3) {
627
0
    evt.move.relative = JS_ToBool(ctx, argv[2]);
628
0
  }
629
0
  compositor->video_out->ProcessEvent(compositor->video_out, &evt);
630
0
  return JS_UNDEFINED;
631
0
}
632
633
634
#if 0 //unused
635
static JSValue scenejs_get_scene_time(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
636
{
637
  GF_SceneGraph *sg = NULL;
638
  GF_Compositor *compositor = scenejs_get_compositor(ctx, this_val);
639
  if (!compositor) return GF_JS_EXCEPTION(ctx);
640
641
  if (!argc || !JS_IsObject(argv[0]) ) {
642
    sg = compositor->root_scene->graph;
643
  } else {
644
    GF_Node *n = gf_sg_js_get_node(c, JSVAL_TO_OBJECT(argv[0]));
645
    sg = n ? n->sgprivate->scenegraph : compositor->root_scene->graph;
646
  }
647
  return JS_NewFloat64(ctx, sg->GetSceneTime(sg->userpriv) );
648
}
649
#endif
650
651
static JSValue scenejs_trigger_gc(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
652
0
{
653
0
  GF_SceneGraph *sg;
654
0
  GF_Compositor *compositor = scenejs_get_compositor(ctx, this_val);
655
0
  sg = compositor->root_scene->graph;
656
0
  sg->trigger_gc = GF_TRUE;
657
0
  return JS_UNDEFINED;
658
0
}
659
660
static GF_FilterPid *gjs_enum_pids(void *udta, u32 *idx)
661
0
{
662
0
  GF_Scene *scene = (GF_Scene *)udta;
663
0
  GF_ObjectManager *odm = gf_list_get(scene->resources, *idx);
664
0
  if (odm) return odm->pid;
665
0
  return NULL;
666
0
}
667
668
static JSValue odm_getProperty(JSContext *ctx, JSValueConst this_val, int magic)
669
0
{
670
0
  GF_ObjectManager *odm = JS_GetOpaque(this_val, odm_class_id);
671
0
  GF_MediaInfo odi;
672
0
  char *str;
673
0
  GF_Scene *scene;
674
0
  Double dval;
675
0
  u32 i, count;
676
677
0
  switch (magic) {
678
0
  case GJS_OM_PROP_ID:
679
0
    return JS_NewInt32(ctx, odm->ID);
680
0
  case GJS_OM_PROP_NB_RES:
681
0
    return JS_NewInt32(ctx, odm->subscene ? gf_list_count(odm->subscene->resources) : 0);
682
0
  case GJS_OM_PROP_URL:
683
0
    if (odm->scene_ns)
684
0
      return JS_NewString(ctx, odm->scene_ns->url);
685
0
    return JS_NULL;
686
0
  case GJS_OM_PROP_DUR:
687
0
    return JS_NewFloat64(ctx, odm->duration / 1000.0);
688
0
  case GJS_OM_PROP_CLOCK:
689
0
    gf_odm_get_object_info(odm, &odi);
690
0
    return JS_NewFloat64(ctx, odi.current_time);
691
0
  case GJS_OM_PROP_DRIFT:
692
0
    gf_odm_get_object_info(odm, &odi);
693
0
    return JS_NewInt32(ctx, odi.clock_drift);
694
0
  case GJS_OM_PROP_STATUS:
695
0
    gf_odm_get_object_info(odm, &odi);
696
0
    if (odi.status==0) str = "Stopped";
697
0
    else if (odi.status==1) str = "Playing";
698
0
    else if (odi.status==2) str = "Paused";
699
0
    else if (odi.status==3) str = "Not Setup";
700
0
    else str = "Setup Failed";
701
0
    return JS_NewString(ctx, str);
702
0
  case GJS_OM_PROP_BUFFER:
703
0
    gf_odm_get_object_info(odm, &odi);
704
0
    return JS_NewInt32(ctx, odi.buffer);
705
0
  case GJS_OM_PROP_DB_COUNT:
706
0
    gf_odm_get_object_info(odm, &odi);
707
0
    return JS_NewInt32(ctx, odi.db_unit_count);
708
0
  case GJS_OM_PROP_CB_COUNT:
709
0
    gf_odm_get_object_info(odm, &odi);
710
0
    return JS_NewInt32(ctx, odi.cb_unit_count);
711
0
  case GJS_OM_PROP_CB_CAP:
712
0
    gf_odm_get_object_info(odm, &odi);
713
0
    return JS_NewInt32(ctx, odi.cb_max_count);
714
0
  case GJS_OM_PROP_TYPE:
715
0
    if (odm->type==GF_STREAM_SCENE) str = "Scene";
716
0
    else if (odm->type==GF_STREAM_OD) str = "OD";
717
0
    else if (odm->type==GF_STREAM_VISUAL) {
718
      //handle all sparse video as text (we could check the original stream type)
719
0
      if (odm->flags & GF_ODM_IS_SPARSE) str = "Text";
720
0
      else str = "Video";
721
0
    }
722
0
    else if (odm->type==GF_STREAM_AUDIO) str = "Audio";
723
0
    else if (odm->type==GF_STREAM_TEXT) str = "Text";
724
0
    else if (odm->subscene) str = "Subscene";
725
0
    else str = (char*) gf_stream_type_name(odm->type);
726
0
    return JS_NewString(ctx, str);
727
0
  case GJS_OM_PROP_SAMPLERATE:
728
0
    return JS_NewInt32(ctx, odm->mo ? odm->mo->sample_rate : 0);
729
0
  case GJS_OM_PROP_CHANNELS:
730
0
    return JS_NewInt32(ctx, odm->mo ? odm->mo->num_channels : 0);
731
0
  case GJS_OM_PROP_LANG:
732
0
    gf_odm_get_object_info(odm, &odi);
733
0
    if (odi.lang_code) return JS_NewString(ctx, odi.lang_code);
734
0
    else if (odi.lang) return JS_NewString(ctx, gf_4cc_to_str(odi.lang) );
735
0
    return JS_NewString(ctx, "und");
736
0
  case GJS_OM_PROP_WIDTH:
737
0
    return JS_NewInt32(ctx, odm->mo ? odm->mo->width : 0);
738
0
  case GJS_OM_PROP_HEIGHT:
739
0
    return JS_NewInt32(ctx, odm->mo ? odm->mo->height : 0);
740
0
  case GJS_OM_PROP_PIXELFORMAT:
741
0
    return JS_NewString(ctx, odm->mo ? gf_4cc_to_str(odm->mo->pixelformat) : "none");
742
0
  case GJS_OM_PROP_PAR:
743
0
    if (odm->mo && odm->mo->pixel_ar) {
744
0
      char szPar[50];
745
0
      sprintf(szPar, "%d:%d", (odm->mo->pixel_ar>>16)&0xFF, (odm->mo->pixel_ar)&0xFF );
746
0
      return JS_NewString(ctx, szPar);
747
0
    } else if (odm->mo && odm->mo->width) {
748
0
      return JS_NewString(ctx, "1:1");
749
0
    }
750
0
    return JS_NULL;
751
0
  case GJS_OM_PROP_DEC_FRAMES:
752
0
    gf_odm_get_object_info(odm, &odi);
753
0
    return JS_NewInt32(ctx, odi.nb_dec_frames);
754
0
  case GJS_OM_PROP_DROP_FRAMES:
755
0
    gf_odm_get_object_info(odm, &odi);
756
0
    return JS_NewInt32(ctx, odi.nb_dropped);
757
0
  case GJS_OM_PROP_DEC_TIME_MAX:
758
0
    gf_odm_get_object_info(odm, &odi);
759
0
    return JS_NewInt32(ctx, odi.max_dec_time);
760
0
  case GJS_OM_PROP_DEC_TIME_TOTAL:
761
0
    gf_odm_get_object_info(odm, &odi);
762
0
    return JS_NewInt64(ctx, odi.total_dec_time);
763
0
  case GJS_OM_PROP_AVG_RATE:
764
0
    gf_odm_get_object_info(odm, &odi);
765
0
    return JS_NewInt32(ctx, odi.avg_bitrate);
766
0
  case GJS_OM_PROP_MAX_RATE:
767
0
    gf_odm_get_object_info(odm, &odi);
768
0
    return JS_NewInt32(ctx, odi.max_bitrate);
769
0
  case GJS_OM_PROP_SERVICE_HANDLER:
770
0
    gf_odm_get_object_info(odm, &odi);
771
0
    return JS_NewString(ctx, odi.service_handler ? odi.service_handler : "unloaded");
772
0
  case GJS_OM_PROP_CODEC:
773
0
    gf_odm_get_object_info(odm, &odi);
774
0
    return JS_NewString(ctx, odi.codec_name ? odi.codec_name : "unloaded");
775
0
  case GJS_OM_PROP_NB_VIEWS:
776
0
    gf_odm_get_object_info(odm, &odi);
777
0
    return JS_NewInt32(ctx, (odi.nb_views>1) ? odi.nb_views : 0);
778
0
  case GJS_OM_PROP_NB_QUALITIES:
779
    //use HAS qualities
780
0
    if (odm->pid) {
781
0
      u32 nb_qualities = 0;
782
0
      GF_PropertyEntry *pe=NULL;
783
0
      const GF_PropertyValue *prop = gf_filter_pid_get_info_str(odm->pid, "has:qualities", &pe);
784
0
      if (prop) nb_qualities = prop->value.string_list.nb_items;
785
0
      gf_filter_release_property(pe);
786
787
0
      if (nb_qualities)
788
0
        return JS_NewInt32(ctx, nb_qualities);
789
0
    }
790
    //use input channels
791
0
    if (odm->extra_pids) {
792
0
      return JS_NewInt32(ctx, 1 + gf_list_count(odm->extra_pids));
793
0
    }
794
    //use number of scalable addons
795
0
    if (odm->upper_layer_odm) {
796
0
      count = 0;
797
0
      while (odm) {
798
0
        odm = odm->upper_layer_odm;
799
0
        count++;
800
0
      }
801
0
      return JS_NewInt32(ctx, count);
802
0
    }
803
0
    return JS_NewInt32(ctx, 1);
804
805
0
  case GJS_OM_PROP_MAX_BUFFER:
806
0
    gf_odm_get_object_info(odm, &odi);
807
0
    return JS_NewInt32(ctx, odi.max_buffer);
808
0
  case GJS_OM_PROP_MIN_BUFFER:
809
0
    gf_odm_get_object_info(odm, &odi);
810
0
    return JS_NewInt32(ctx, odi.min_buffer);
811
0
  case GJS_OM_PROP_FRAME_DUR:
812
0
    gf_odm_get_object_info(odm, &odi);
813
0
    return JS_NewInt32(ctx, odi.au_duration);
814
0
  case GJS_OM_PROP_NB_IRAP:
815
0
    gf_odm_get_object_info(odm, &odi);
816
0
    return JS_NewInt32(ctx, odi.nb_iraps);
817
0
  case GJS_OM_PROP_IRAP_DEC_TIME:
818
0
    gf_odm_get_object_info(odm, &odi);
819
0
    return JS_NewInt64(ctx, odi.irap_total_dec_time);
820
0
  case GJS_OM_PROP_IRAP_MAX_TIME:
821
0
    gf_odm_get_object_info(odm, &odi);
822
0
    return JS_NewInt32(ctx, odi.irap_max_dec_time);
823
0
  case GJS_OM_PROP_SERVICE_ID:
824
0
    return JS_NewInt32(ctx, odm->ServiceID);
825
0
  case GJS_OM_PROP_SELECTED_SERVICE:
826
0
    return JS_NewInt32(ctx,  (!odm->addon && odm->subscene) ? odm->subscene->selected_service_id : odm->parentscene->selected_service_id);
827
0
    break;
828
0
  case GJS_OM_PROP_BANDWIDTH_DOWN:
829
0
    if (odm->scene_ns->source_filter) {
830
0
      JSValue ret;
831
0
      GF_PropertyEntry *pe=NULL;
832
0
      const GF_PropertyValue *prop = gf_filter_get_info(odm->scene_ns->source_filter, GF_PROP_PID_DOWN_RATE, &pe);
833
0
      ret = JS_NewInt32(ctx, prop ? prop->value.uint/1000 : 0);
834
0
      gf_filter_release_property(pe);
835
0
      return ret;
836
0
    }
837
0
    return JS_NewInt32(ctx, 0);
838
839
0
  case GJS_OM_PROP_NB_HTTP:
840
0
    if (odm->scene_ns->source_filter) {
841
0
      return JS_NewInt32(ctx, gf_filter_count_source_by_protocol(odm->scene_ns->source_filter, "http", GF_TRUE, gjs_enum_pids, odm->subscene ? odm->subscene : odm->parentscene ) );
842
0
    }
843
0
    return JS_NewInt32(ctx, 0);
844
0
  case GJS_OM_PROP_TIMESHIFT_DEPTH:
845
0
    if ((s32) odm->timeshift_depth > 0) {
846
0
      return JS_NewFloat64(ctx, ((Double) odm->timeshift_depth) / 1000.0 );
847
0
    }
848
0
    return JS_NewFloat64(ctx, 0.0);
849
850
0
  case GJS_OM_PROP_TIMESHIFT_TIME:
851
0
    dval = 0.0;
852
0
    if (!odm->timeshift_depth) {
853
0
      return JS_NewInt32(ctx, 0);
854
0
    }
855
856
0
    scene = odm->subscene ? odm->subscene : odm->parentscene;
857
    //we may need to check the main addon for timeshifting
858
0
    if (scene->main_addon_selected) {
859
0
      count = gf_list_count(scene->resources);
860
0
      for (i=0; i < count; i++) {
861
0
        GF_ObjectManager *an_odm = gf_list_get(scene->resources, i);
862
0
        if (an_odm && an_odm->addon && (an_odm->addon->addon_type==GF_ADDON_TYPE_MAIN)) {
863
0
          odm = an_odm;
864
0
        }
865
0
      }
866
0
    }
867
868
0
    if (odm->timeshift_depth) {
869
0
      GF_FilterPid *pid = odm->pid;
870
0
      if (!pid && odm->subscene) {
871
0
        odm = gf_list_get(odm->subscene->resources, 0);
872
0
        pid = odm->pid;
873
0
      }
874
0
      if (pid) {
875
0
        GF_PropertyEntry *pe=NULL;
876
0
        const GF_PropertyValue *p = gf_filter_pid_get_info(pid, GF_PROP_PID_TIMESHIFT_TIME, &pe);
877
0
        if (p) dval = p->value.number;
878
0
        gf_filter_release_property(pe);
879
0
      }
880
0
    } else if (scene->main_addon_selected) {
881
0
      GF_Clock *ck = scene->root_od->ck;
882
0
      if (ck) {
883
0
        u64 now = gf_clock_time_absolute(ck) ;
884
0
        u64 live = scene->obj_clock_at_main_activation + gf_sys_clock() - scene->sys_clock_at_main_activation;
885
0
        dval = ((Double) live) / 1000.0;
886
0
        dval -= ((Double) now) / 1000.0;
887
888
0
        if (dval<0) {
889
0
          GF_Event evt;
890
0
          memset(&evt, 0, sizeof(evt));
891
0
          evt.type = GF_EVENT_TIMESHIFT_UNDERRUN;
892
0
          gf_sc_send_event(scene->compositor, &evt);
893
0
          dval=0;
894
0
        } else if (dval && dval*1000 > scene->timeshift_depth) {
895
0
          GF_Event evt;
896
0
          memset(&evt, 0, sizeof(evt));
897
0
          evt.type = GF_EVENT_TIMESHIFT_OVERFLOW;
898
0
          gf_sc_send_event(scene->compositor, &evt);
899
0
          dval=scene->timeshift_depth;
900
0
          dval/=1000;
901
0
        }
902
0
      }
903
0
    }
904
0
    return JS_NewFloat64(ctx, dval);
905
906
0
  case GJS_OM_PROP_IS_ADDON:
907
0
    return JS_NewBool(ctx,  (odm->addon || (!odm->subscene && odm->parentscene->root_od->addon)) );
908
909
0
  case GJS_OM_PROP_MAIN_ADDON_ON:
910
0
    scene = odm->subscene ? odm->subscene : odm->parentscene;
911
0
    return JS_NewBool(ctx,  scene->main_addon_selected );
912
0
  case GJS_OM_PROP_IS_OVER:
913
0
    scene = odm->subscene ? odm->subscene : odm->parentscene;
914
0
    return JS_NewBool(ctx,  gf_sc_is_over(scene->compositor, scene->graph));
915
916
0
  case GJS_OM_PROP_DYNAMIC_SCENE:
917
0
    return JS_NewBool(ctx, odm->subscene && odm->subscene->is_dynamic_scene);
918
919
0
  case GJS_OM_PROP_SERVICE_NAME:
920
0
    if (odm->pid) {
921
0
      GF_PropertyEntry *pe=NULL;
922
0
      JSValue ret = JS_NULL;
923
0
      const GF_PropertyValue *p = gf_filter_pid_get_info(odm->pid, GF_PROP_PID_SERVICE_NAME, &pe);
924
0
      if (p && p->value.string)
925
0
        ret = JS_NewString(ctx, p->value.string);
926
0
      gf_filter_release_property(pe);
927
0
      return ret;
928
0
    }
929
0
    return JS_NULL;
930
931
0
  case GJS_OM_PROP_NTP_DIFF:
932
0
    gf_odm_get_object_info(odm, &odi);
933
0
    return JS_NewInt32(ctx, odi.ntp_diff);
934
935
0
  case GJS_OM_PROP_NTP_SENDER_DIFF:
936
0
    if (odm->last_drawn_frame_ntp_sender && odm->last_drawn_frame_ntp_receive) {
937
0
      s32 diff = gf_net_ntp_diff_ms(odm->last_drawn_frame_ntp_receive, odm->last_drawn_frame_ntp_sender);
938
0
      return JS_NewInt32(ctx, diff);
939
0
    }
940
0
    return JS_NULL;
941
942
0
  case GJS_OM_PROP_MAIN_ADDON_URL:
943
0
    scene = odm->subscene ? odm->subscene : odm->parentscene;
944
0
    count = gf_list_count(scene->resources);
945
0
    for (i=0; i < count; i++) {
946
0
      GF_ObjectManager *an_odm = gf_list_get(scene->resources, i);
947
0
      if (an_odm && an_odm->addon && (an_odm->addon->addon_type==GF_ADDON_TYPE_MAIN)) {
948
0
        if (!strstr(an_odm->addon->url, "://")) {
949
0
          char szURL[GF_MAX_PATH];
950
0
          sprintf(szURL, "gpac://%s", an_odm->addon->url);
951
0
          return JS_NewString(ctx, szURL);
952
0
        } else {
953
0
          return JS_NewString(ctx, an_odm->addon->url);
954
0
        }
955
0
      }
956
0
    }
957
0
    return JS_NULL;
958
959
0
  case GJS_OM_PROP_REVERSE_PLAYBACK:
960
0
    if (odm->pid) {
961
0
      const GF_PropertyValue *p = gf_filter_pid_get_property(odm->pid, GF_PROP_PID_PLAYBACK_MODE);
962
0
      if (p && p->value.uint==GF_PLAYBACK_MODE_REWIND)
963
0
        return JS_NewBool(ctx, GF_TRUE);
964
0
    }
965
0
    return JS_NewBool(ctx, GF_FALSE );
966
967
0
  case GJS_OM_PROP_SCALABLE_ENHANCEMENT:
968
0
    if (odm->lower_layer_odm) return JS_TRUE;
969
0
    if (odm->subscene && !odm->pid && odm->addon && !gf_list_count(odm->subscene->resources))
970
0
      return JS_TRUE;
971
0
    return JS_FALSE;
972
973
0
  case GJS_OM_PROP_MAIN_ADDON_MEDIATIME:
974
0
    scene = odm->subscene ? odm->subscene : odm->parentscene;
975
0
    count = gf_list_count(scene->resources);
976
977
0
    if (! scene->main_addon_selected) count = 0;
978
979
0
    for (i=0; i < count; i++) {
980
0
      GF_ObjectManager *an_odm = gf_list_get(scene->resources, i);
981
0
      if (an_odm && an_odm->addon && (an_odm->addon->addon_type==GF_ADDON_TYPE_MAIN)) {
982
0
        if (an_odm->duration) {
983
0
          Double now = gf_clock_time_absolute(scene->root_od->ck) / 1000.0;
984
0
          now -= ((Double) an_odm->addon->media_pts) / 90000.0;
985
0
          now += ((Double) an_odm->addon->media_timestamp) / an_odm->addon->media_timescale;
986
0
          return JS_NewFloat64(ctx, now);
987
0
        }
988
0
      }
989
0
    }
990
0
    return JS_NewFloat64(ctx, -1);
991
992
0
  case GJS_OM_PROP_DEPENDENT_GROUPS:
993
0
    if (odm->pid) {
994
0
      GF_PropertyEntry *pe=NULL;
995
0
      const GF_PropertyValue *p = gf_filter_pid_get_info_str(odm->pid, "has:group_deps", &pe);
996
0
      if (p) {
997
0
        u32 v = p->value.uint;
998
0
        gf_filter_release_property(pe);
999
0
        return JS_NewInt32(ctx, v);
1000
0
      }
1001
0
    }
1002
0
    return JS_NewInt32(ctx, 0);
1003
1004
0
  case GJS_OM_PROP_IS_VR_SCENE:
1005
0
    return JS_NewBool(ctx, odm->subscene && odm->subscene->vr_type);
1006
0
  case GJS_OM_PROP_DISABLED:
1007
0
    return JS_NewBool(ctx, odm->redirect_url ? 1 : 0);
1008
0
  case GJS_OM_PROP_BUFFERING:
1009
0
    if (odm->parentscene)
1010
0
      return JS_NewBool(ctx, odm->parentscene->nb_buffering ? 1 : 0);
1011
0
    return JS_NewBool(ctx, odm->subscene->nb_buffering ? 1 : 0);
1012
0
  case GJS_OM_PROP_FORCED_SUB:
1013
0
    if (odm->pid) {
1014
0
      if (odm->flags & GF_ODM_SUB_FORCED) return JS_NewInt32(ctx, 3);
1015
0
      const GF_PropertyValue *p = gf_filter_pid_get_property(odm->pid, GF_PROP_PID_FORCED_SUB);
1016
0
      if (p) return JS_NewInt32(ctx, p->value.uint);
1017
0
    }
1018
0
    return JS_NewInt32(ctx, 0);
1019
0
  }
1020
0
  return JS_UNDEFINED;
1021
0
}
1022
1023
static JSValue gjs_odm_get_quality(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
1024
0
{
1025
0
  GF_ObjectManager *odm = JS_GetOpaque(this_val, odm_class_id);
1026
0
  const GF_PropertyValue *prop;
1027
0
  GF_PropertyEntry *pe=NULL;
1028
0
  char *qdesc;
1029
0
  const char *id="", *mime="", *codec="";
1030
0
  u32 sr=0, ch=0, w=0, h=0, bw=0, par_n=1, par_d=1, tile_adaptation_mode=0,dependent_group_index=0;
1031
0
  Bool ilced=GF_FALSE, disabled=GF_FALSE, selected=GF_FALSE, automatic=GF_FALSE;
1032
0
  Double fps=30.0;
1033
0
  u32 ssr = 0;
1034
0
  s32 idx;
1035
0
  s32 dep_idx=0;
1036
1037
0
  if (!odm) return GF_JS_EXCEPTION(ctx);
1038
0
  if (argc<1) return GF_JS_EXCEPTION(ctx);
1039
0
  if (JS_ToInt32(ctx, &idx, argv[0]))
1040
0
    return GF_JS_EXCEPTION(ctx);
1041
1042
0
  if ((argc>=2) && JS_ToInt32(ctx, &dep_idx, argv[1]))
1043
0
    return GF_JS_EXCEPTION(ctx);
1044
1045
0
  if (!odm->pid) return JS_NULL;
1046
1047
0
  if (dep_idx) {
1048
0
    char szName[100];
1049
0
    sprintf(szName, "has:deps_%d_qualities", dep_idx);
1050
0
    prop = gf_filter_pid_get_info_str(odm->pid, szName, &pe);
1051
0
  } else {
1052
0
    prop = gf_filter_pid_get_info_str(odm->pid, "has:qualities", &pe);
1053
0
  }
1054
0
  if (!prop || (prop->type!=GF_PROP_STRING_LIST)) {
1055
0
    gf_filter_release_property(pe);
1056
0
    return JS_NULL;
1057
0
  }
1058
0
  qdesc = NULL;
1059
1060
0
  if (idx < (s32) prop->value.string_list.nb_items)
1061
0
    qdesc = prop->value.string_list.vals[idx];
1062
1063
0
  if (!qdesc) {
1064
0
    gf_filter_release_property(pe);
1065
0
    return JS_NULL;
1066
0
  }
1067
0
  JSValue a = JS_NewObject(ctx);
1068
0
  if (JS_IsException(a))
1069
0
    return a;
1070
1071
0
  while (qdesc) {
1072
0
    char *sep=strstr(qdesc, "::");
1073
0
    if (sep) sep[0]=0;
1074
1075
0
    if (!strncmp(qdesc, "id=", 3)) {
1076
0
      id = NULL;
1077
0
      JS_SetPropertyStr(ctx, a, "ID", JS_NewString(ctx, qdesc+3) );
1078
0
    } else if (!strncmp(qdesc, "mime=", 5)) {
1079
0
      mime = NULL;
1080
0
      JS_SetPropertyStr(ctx, a, "mime", JS_NewString(ctx, qdesc+5));
1081
0
    } else if (!strncmp(qdesc, "codec=", 6)) {
1082
0
      codec = NULL;
1083
0
      JS_SetPropertyStr(ctx, a, "codec", JS_NewString(ctx, qdesc+6));
1084
0
    }
1085
0
    else if (!strncmp(qdesc, "bw=", 3)) bw = atoi(qdesc+3);
1086
0
    else if (!strncmp(qdesc, "w=", 2)) w = atoi(qdesc+2);
1087
0
    else if (!strncmp(qdesc, "h=", 2)) h = atoi(qdesc+2);
1088
0
    else if (!strncmp(qdesc, "sr=", 3)) sr = atoi(qdesc+3);
1089
0
    else if (!strncmp(qdesc, "ch=", 3)) ch = atoi(qdesc+3);
1090
0
    else if (!strcmp(qdesc, "interlaced")) ilced = GF_TRUE;
1091
0
    else if (!strcmp(qdesc, "disabled")) disabled = GF_TRUE;
1092
0
    else if (!strcmp(qdesc, "selected")) selected = GF_TRUE;
1093
0
    else if (!strncmp(qdesc, "fps=", 4)) {
1094
0
      u32 fd=25, fn=1;
1095
0
      if (sscanf(qdesc, "fps=%d/%d", &fn, &fd) != 2) {
1096
0
        fd=1;
1097
0
        sscanf(qdesc, "fps=%d", &fn);
1098
0
      }
1099
0
      fps = ((Double)fn) / fd;
1100
0
    }
1101
0
    else if (!strncmp(qdesc, "sar=", 4)) {
1102
0
      sscanf(qdesc, "sar=%d/%d", &par_n, &par_d);
1103
0
    }
1104
0
    else if (!strncmp(qdesc, "ssr=", 4)) {
1105
0
      sscanf(qdesc, "ssr=%u", &ssr);
1106
0
    }
1107
0
    if (!sep) break;
1108
0
    sep[0]=':';
1109
0
    qdesc = sep+2;
1110
0
  }
1111
1112
0
  if (!dep_idx) {
1113
0
    prop = gf_filter_pid_get_info_str(odm->pid, "has:selected", &pe);
1114
0
    if (prop && (prop->value.uint==idx))
1115
0
      selected = GF_TRUE;
1116
0
    prop = gf_filter_pid_get_info_str(odm->pid, "has:auto", &pe);
1117
0
    if (prop && prop->value.boolean)
1118
0
      automatic = GF_TRUE;
1119
0
    prop = gf_filter_pid_get_info_str(odm->pid, "has:tilemode", &pe);
1120
0
    if (prop) tile_adaptation_mode = prop->value.uint;
1121
0
  } else if (dep_idx>0) {
1122
0
    prop = gf_filter_pid_get_info_str(odm->pid, "has:deps_selected", &pe);
1123
0
    if (prop && (prop->type==GF_PROP_SINT_LIST)) {
1124
0
      if ((u32) dep_idx<=prop->value.sint_list.nb_items) {
1125
0
        s32 sel = prop->value.sint_list.vals[dep_idx-1];
1126
0
        if ((sel>=0) && (idx==sel)) {
1127
0
          selected = GF_TRUE;
1128
0
        }
1129
0
      }
1130
0
    }
1131
0
  }
1132
1133
0
  gf_filter_release_property(pe);
1134
1135
0
  if (id)
1136
0
    JS_SetPropertyStr(ctx, a, "ID", JS_NewString(ctx, id));
1137
0
  if (mime)
1138
0
    JS_SetPropertyStr(ctx, a, "mime", JS_NewString(ctx, mime));
1139
0
  if (codec)
1140
0
    JS_SetPropertyStr(ctx, a, "codec", JS_NewString(ctx, codec));
1141
1142
0
  JS_SetPropertyStr(ctx, a, "width", JS_NewInt32(ctx, w));
1143
0
  JS_SetPropertyStr(ctx, a, "height", JS_NewInt32(ctx, h));
1144
0
  JS_SetPropertyStr(ctx, a, "bandwidth", JS_NewInt32(ctx, bw));
1145
0
  JS_SetPropertyStr(ctx, a, "interlaced", JS_NewBool(ctx, ilced));
1146
0
  JS_SetPropertyStr(ctx, a, "fps", JS_NewFloat64(ctx, fps) );
1147
0
  JS_SetPropertyStr(ctx, a, "samplerate", JS_NewInt32(ctx, sr));
1148
0
  JS_SetPropertyStr(ctx, a, "channels", JS_NewInt32(ctx, ch));
1149
0
  JS_SetPropertyStr(ctx, a, "par_num", JS_NewInt32(ctx, par_n));
1150
0
  JS_SetPropertyStr(ctx, a, "par_den", JS_NewInt32(ctx, par_d));
1151
0
  JS_SetPropertyStr(ctx, a, "ssr", JS_NewInt32(ctx, ssr));
1152
0
  JS_SetPropertyStr(ctx, a, "disabled", JS_NewBool(ctx, disabled));
1153
0
  JS_SetPropertyStr(ctx, a, "is_selected", JS_NewBool(ctx, selected));
1154
0
  JS_SetPropertyStr(ctx, a, "automatic", JS_NewBool(ctx, automatic));
1155
0
  if (!dep_idx) {
1156
0
    JS_SetPropertyStr(ctx, a, "tile_mode", JS_NewInt32(ctx, tile_adaptation_mode));
1157
0
    JS_SetPropertyStr(ctx, a, "dependent_groups", JS_NewInt32(ctx, dependent_group_index));
1158
0
  }
1159
1160
0
  return a;
1161
0
}
1162
1163
static JSValue gjs_odm_get_srd(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
1164
0
{
1165
0
  GF_ObjectManager *odm = JS_GetOpaque(this_val, odm_class_id);
1166
0
  s32 x, y, w, h;
1167
1168
0
  if (!odm) return GF_JS_EXCEPTION(ctx);
1169
1170
0
  x = y = w = h = 0;
1171
0
  if (argc) {
1172
0
    u32 srdw, srdh;
1173
0
    const GF_PropertyValue *p;
1174
0
    GF_PropertyEntry *pe=NULL;
1175
0
    s32 dep_idx;
1176
0
    if (JS_ToInt32(ctx, &dep_idx, argv[0]) )
1177
0
      return GF_JS_EXCEPTION(ctx);
1178
0
    if (dep_idx<=0)
1179
0
      return GF_JS_EXCEPTION(ctx);
1180
0
    dep_idx--;
1181
0
    p = gf_filter_pid_get_info_str(odm->pid, "has:groups_srd", &pe);
1182
0
    if (p && (p->type==GF_PROP_STRING_LIST)) {
1183
0
      char *srd;
1184
0
      if ((u32) dep_idx>=p->value.string_list.nb_items) {
1185
0
        gf_filter_release_property(pe);
1186
0
        return GF_JS_EXCEPTION(ctx);
1187
0
      }
1188
0
      srd = p->value.string_list.vals[dep_idx];
1189
0
      sscanf(srd, "%dx%dx%dx%d@%dx%d", &x, &y, &w, &h, &srdw, &srdh);
1190
0
    }
1191
0
    gf_filter_release_property(pe);
1192
0
  } else if (odm && odm->mo && odm->mo->srd_w && odm->mo->srd_h) {
1193
0
    x = odm->mo->srd_x;
1194
0
    y = odm->mo->srd_y;
1195
0
    w = odm->mo->srd_w;
1196
0
    h = odm->mo->srd_h;
1197
0
  }
1198
1199
0
  if (w && h) {
1200
0
    JSValue a = JS_NewObject(ctx);
1201
0
    if (JS_IsException(a)) return a;
1202
1203
0
    JS_SetPropertyStr(ctx, a, "x", JS_NewInt32(ctx, x));
1204
0
    JS_SetPropertyStr(ctx, a, "y", JS_NewInt32(ctx, y));
1205
0
    JS_SetPropertyStr(ctx, a, "w", JS_NewInt32(ctx, w));
1206
0
    JS_SetPropertyStr(ctx, a, "h", JS_NewInt32(ctx, h));
1207
0
    return a;
1208
0
  }
1209
0
  return JS_NULL;
1210
0
}
1211
1212
GF_Filter *jsff_get_filter(JSContext *c, JSValue this_val);
1213
1214
static JSValue gjs_odm_in_parent_chain(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
1215
0
{
1216
0
  Bool res;
1217
0
  GF_Filter *f;
1218
0
  GF_Scene *scene;
1219
0
  GF_ObjectManager *odm = JS_GetOpaque(this_val, odm_class_id);
1220
0
  if (!odm || !argc) return GF_JS_EXCEPTION(ctx);
1221
1222
0
  f = jsff_get_filter(ctx, argv[0]);
1223
0
  if (!f) return GF_JS_EXCEPTION(ctx);
1224
1225
0
  scene = odm->subscene ? odm->subscene : odm->parentscene;
1226
0
  if (!scene) return GF_JS_EXCEPTION(ctx);
1227
0
  if (gf_filter_in_parent_chain(f, scene->compositor->filter))
1228
0
    return JS_FALSE;
1229
1230
0
  if (odm->pid) {
1231
0
    res = gf_filter_pid_is_filter_in_parents(odm->pid, f);
1232
0
  } else {
1233
0
    res = gf_filter_in_parent_chain(f, odm->scene_ns->source_filter);
1234
0
  }
1235
0
  return res ? JS_TRUE : JS_FALSE;
1236
0
}
1237
1238
static JSValue gjs_odm_select_quality(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
1239
0
{
1240
0
  s32 idx = -1;
1241
0
  s32 tile_mode = -1;
1242
0
  u32 dep_idx = 0;
1243
0
  GF_FilterEvent evt;
1244
0
  GF_ObjectManager *odm = JS_GetOpaque(this_val, odm_class_id);
1245
1246
0
  if (!odm || !odm->pid) return GF_JS_EXCEPTION(ctx);
1247
1248
0
  if ((argc>=1) && JS_IsString(argv[0])) {
1249
0
    const char *ID = JS_ToCString(ctx, argv[0]);
1250
0
    if (!strcmp(ID, "auto")) {
1251
0
      idx = -1;
1252
0
    } else {
1253
0
      idx = atoi(ID);
1254
0
    }
1255
0
    JS_FreeCString(ctx, ID);
1256
1257
0
    if (argc>=2) {
1258
0
      if (JS_ToInt32(ctx, &dep_idx, argv[1]))
1259
0
        return GF_JS_EXCEPTION(ctx);
1260
0
    }
1261
0
  }
1262
0
  else if ((argc==1) && JS_IsInteger(argv[0])) {
1263
0
    if (JS_ToInt32(ctx, &tile_mode, argv[0]))
1264
0
      return GF_JS_EXCEPTION(ctx);
1265
0
    if (tile_mode<0)
1266
0
      return JS_UNDEFINED;
1267
0
  }
1268
1269
0
  GF_FEVT_INIT(evt, GF_FEVT_QUALITY_SWITCH, odm->pid);
1270
1271
0
  if (tile_mode>=0) {
1272
0
    evt.quality_switch.set_tile_mode_plus_one = 1 + tile_mode;
1273
0
  } else {
1274
0
    evt.quality_switch.dependent_group_index = dep_idx;
1275
0
    evt.quality_switch.q_idx = idx;
1276
0
  }
1277
0
  gf_filter_pid_send_event(odm->pid, &evt);
1278
0
  return JS_UNDEFINED;
1279
0
}
1280
1281
static JSValue gjs_odm_disable_main_addon(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
1282
0
{
1283
0
  GF_ObjectManager *odm = JS_GetOpaque(this_val, odm_class_id);
1284
0
  if (!odm) return GF_JS_EXCEPTION(ctx);
1285
0
  if (!odm->subscene || !odm->subscene->main_addon_selected) return JS_UNDEFINED;
1286
1287
0
  gf_scene_resume_live(odm->subscene);
1288
0
  return JS_UNDEFINED;
1289
0
}
1290
1291
static JSValue gjs_odm_select_service(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
1292
0
{
1293
0
  u32 sid;
1294
0
  GF_ObjectManager *odm = JS_GetOpaque(this_val, odm_class_id);
1295
0
  if (!odm) return GF_JS_EXCEPTION(ctx);
1296
0
  if (argc<1) return GF_JS_EXCEPTION(ctx);
1297
1298
0
  if (JS_ToInt32(ctx, &sid, argv[0]))
1299
0
    return GF_JS_EXCEPTION(ctx);
1300
1301
0
  gf_scene_set_service_id(odm->subscene ? odm->subscene : odm->parentscene, sid);
1302
0
  return JS_UNDEFINED;
1303
0
}
1304
1305
static JSValue gjs_odm_select(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
1306
0
{
1307
0
  GF_ObjectManager *odm = JS_GetOpaque(this_val, odm_class_id);
1308
0
  if (!odm) return GF_JS_EXCEPTION(ctx);
1309
1310
0
#ifndef GPAC_DISABLE_COMPOSITOR
1311
0
  gf_scene_select_object(odm->parentscene, odm);
1312
0
#endif
1313
0
  return JS_UNDEFINED;
1314
0
}
1315
1316
static JSValue gjs_odm_get_resource(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
1317
0
{
1318
0
  GF_ObjectManager *an_odm = NULL;
1319
0
  u32 idx;
1320
0
  GF_ObjectManager *odm = JS_GetOpaque(this_val, odm_class_id);
1321
1322
0
  if (!odm) return GF_JS_EXCEPTION(ctx);
1323
0
  if (argc<1) return GF_JS_EXCEPTION(ctx);
1324
0
  if (JS_ToInt32(ctx, &idx, argv[0])) return GF_JS_EXCEPTION(ctx);
1325
1326
0
  if (odm->subscene) {
1327
0
    an_odm = gf_list_get(odm->subscene->resources, idx);
1328
0
  }
1329
0
  if (an_odm && an_odm->scene_ns) {
1330
0
    JSValue anobj = JS_NewObjectClass(ctx, odm_class_id);
1331
0
    JS_SetOpaque(anobj, an_odm);
1332
0
    return anobj;
1333
0
  }
1334
0
  return JS_NULL;
1335
0
}
1336
1337
static JSValue gjs_odm_addon_layout(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
1338
0
{
1339
0
  u32 pos, size;
1340
0
  GF_ObjectManager *odm = JS_GetOpaque(this_val, odm_class_id);
1341
0
  if (!odm) return GF_JS_EXCEPTION(ctx);
1342
0
  if (argc<2) return GF_JS_EXCEPTION(ctx);
1343
0
  if (JS_ToInt32(ctx, &pos, argv[0])) return GF_JS_EXCEPTION(ctx);
1344
0
  if (JS_ToInt32(ctx, &size, argv[1])) return GF_JS_EXCEPTION(ctx);
1345
0
  if (odm->subscene)
1346
0
    gf_scene_set_addon_layout_info(odm->subscene, pos, size);
1347
0
  return JS_UNDEFINED;
1348
0
}
1349
1350
static void do_enable_addon(GF_ObjectManager *odm, char *addon_url, Bool enable_if_defined, Bool disable_if_defined )
1351
0
{
1352
0
  if (addon_url) {
1353
0
    GF_AssociatedContentLocation addon_info;
1354
0
    memset(&addon_info, 0, sizeof(GF_AssociatedContentLocation));
1355
0
    addon_info.external_URL = addon_url;
1356
0
    addon_info.timeline_id = -100;
1357
0
    addon_info.enable_if_defined = enable_if_defined;
1358
0
    addon_info.disable_if_defined = disable_if_defined;
1359
0
    gf_scene_register_associated_media(odm->subscene ? odm->subscene : odm->parentscene, &addon_info);
1360
0
  }
1361
0
}
1362
1363
static JSValue gjs_odm_enable_addon(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
1364
0
{
1365
0
  Bool do_disable = GF_FALSE;
1366
0
  const char *addon_url = NULL;
1367
0
  GF_ObjectManager *odm = JS_GetOpaque(this_val, odm_class_id);
1368
0
  if (!odm || !argc) return GF_JS_EXCEPTION(ctx);
1369
1370
0
  if (! JS_IsString(argv[0]) ) {
1371
#ifdef GPAC_ENABLE_COVERAGE
1372
    if (gf_sys_is_cov_mode()) {
1373
      do_enable_addon(odm, NULL, GF_TRUE, GF_FALSE);
1374
    }
1375
#endif
1376
0
    return JS_UNDEFINED;
1377
0
  }
1378
0
  if (argc==2)
1379
0
    do_disable = JS_ToBool(ctx, argv[1]);
1380
0
  addon_url = JS_ToCString(ctx, argv[0]);
1381
0
  if (addon_url) {
1382
0
    do_enable_addon(odm, (char *) addon_url, GF_TRUE, do_disable);
1383
0
  }
1384
0
  JS_FreeCString(ctx, addon_url);
1385
0
  return JS_UNDEFINED;
1386
0
}
1387
1388
static JSValue gjs_odm_declare_addon(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
1389
0
{
1390
0
  const char *addon_url = NULL;
1391
0
  GF_ObjectManager *odm = JS_GetOpaque(this_val, odm_class_id);
1392
0
  if (!odm || !argc) return GF_JS_EXCEPTION(ctx);
1393
0
  if (! JS_IsString(argv[0]) ) return GF_JS_EXCEPTION(ctx);
1394
1395
0
  addon_url = JS_ToCString(ctx, argv[0]);
1396
0
  if (addon_url && strcmp(addon_url, "gtest")) {
1397
0
    do_enable_addon(odm, (char *)addon_url, GF_FALSE, GF_FALSE);
1398
0
  }
1399
0
  JS_FreeCString(ctx, addon_url);
1400
0
  return JS_UNDEFINED;
1401
0
}
1402
1403
static JSValue gjs_odm_get_chapters(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
1404
0
{
1405
0
  GF_ObjectManager *odm = JS_GetOpaque(this_val, odm_class_id);
1406
0
  if (!odm) return GF_JS_EXCEPTION(ctx);
1407
1408
0
  if (odm->subscene) odm = gf_list_get(odm->subscene->resources, 0);
1409
0
  if (!odm || ! odm->pid) return JS_NULL;
1410
1411
0
  const GF_PropertyValue *times = gf_filter_pid_get_property(odm->pid, GF_PROP_PID_CHAP_TIMES);
1412
0
  const GF_PropertyValue *names = gf_filter_pid_get_property(odm->pid, GF_PROP_PID_CHAP_NAMES);
1413
0
  if (!times || !names || (times->value.uint_list.nb_items!=times->value.string_list.nb_items))
1414
0
    return JS_NULL;
1415
1416
0
  JSValue ret = JS_NewArray(ctx);
1417
0
  u32 i, count=times->value.uint_list.nb_items;
1418
0
  for (i=0; i<count; i++) {
1419
0
    char *name;
1420
0
    JSValue obj = JS_NewObject(ctx);
1421
0
    JS_SetPropertyStr(ctx, obj, "start", JS_NewInt32(ctx, times->value.uint_list.vals[i]));
1422
0
    name = names->value.string_list.vals[i];
1423
0
    if (!name) name = "";
1424
0
    JS_SetPropertyStr(ctx, obj, "name", JS_NewString(ctx, name));
1425
0
    JS_SetPropertyUint32(ctx, ret, i, obj);
1426
0
  }
1427
0
  return ret;
1428
0
}
1429
1430
static JSValue scenejs_get_object_manager(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
1431
0
{
1432
0
  JSValue anobj;
1433
0
  u32 i, count;
1434
0
  GF_ObjectManager *odm = NULL;
1435
0
  const char *service_url = NULL;
1436
0
  GF_SCENEJSExt *sjs = JS_GetOpaque(this_val, scene_class_id);
1437
0
  GF_Compositor *compositor = scenejs_get_compositor(ctx, this_val);
1438
0
  GF_Scene *scene = compositor->root_scene;
1439
0
  if (!sjs) return GF_JS_EXCEPTION(ctx);
1440
1441
0
  if (JS_IsString(argv[0]) ) {
1442
0
    const char *url;
1443
0
    char *an_url;
1444
0
    u32 url_len;
1445
0
    url = service_url = JS_ToCString(ctx, argv[0]);
1446
0
    if (!service_url) {
1447
0
      return JS_NULL;
1448
0
    }
1449
0
    if (!strncmp(service_url, "gpac://", 7)) url = service_url + 7;
1450
0
    if (!strncmp(service_url, "file://", 7)) url = service_url + 7;
1451
0
    url_len = (u32) strlen(url);
1452
0
    an_url = strchr(url, '#');
1453
0
    if (an_url) url_len -= (u32) strlen(an_url);
1454
1455
0
    count = gf_list_count(scene->resources);
1456
0
    for (i=0; i<count; i++) {
1457
0
      odm = gf_list_get(scene->resources, i);
1458
0
      if (odm->scene_ns) {
1459
0
        an_url = odm->scene_ns->url;
1460
0
        if (!strncmp(an_url, "gpac://", 7)) an_url = an_url + 7;
1461
0
        if (!strncmp(an_url, "file://", 7)) an_url = an_url + 7;
1462
0
        if (!strncmp(an_url, url, url_len))
1463
0
          break;
1464
1465
0
        if (strstr(an_url, "://")) {
1466
0
          char *pc_url = gf_url_percent_encode(an_url);
1467
0
          if (!strncmp(pc_url, url, url_len)) {
1468
0
            gf_free(pc_url);
1469
0
            break;
1470
0
          }
1471
0
          gf_free(pc_url);
1472
0
        }
1473
0
      }
1474
0
      odm = NULL;
1475
0
    }
1476
0
  }
1477
1478
0
  JS_FreeCString(ctx, service_url);
1479
1480
0
  if (!odm) return JS_NULL;
1481
1482
0
  anobj = JS_NewObjectClass(ctx, odm_class_id);
1483
0
  if (JS_IsException(anobj)) return anobj;
1484
1485
0
  JS_SetOpaque(anobj, odm);
1486
0
  return anobj;
1487
0
}
1488
1489
1490
static JSValue gpacevt_getProperty(JSContext *ctx, JSValueConst this_val, int magic)
1491
0
{
1492
0
  GF_SCENEJSExt *sjs = JS_GetOpaque(this_val, gpacevt_class_id);
1493
0
  if (!sjs || !sjs->evt) return GF_JS_EXCEPTION(ctx);
1494
0
  GF_Event *evt = sjs->evt;
1495
1496
0
  switch (magic) {
1497
0
  case GJS_EVT_PROP_KEYCODE:
1498
0
#ifndef GPAC_DISABLE_SVG
1499
0
    return JS_NewString(ctx, gf_dom_get_key_name(evt->key.key_code) );
1500
#else
1501
    return JS_NULL;
1502
#endif
1503
1504
0
  case GJS_EVT_PROP_MOUSE_X:
1505
0
    return JS_NewInt32(ctx, evt->mouse.x);
1506
0
  case GJS_EVT_PROP_MOUSE_Y:
1507
0
    return JS_NewInt32(ctx, evt->mouse.y);
1508
0
  case GJS_EVT_PROP_PICKED:
1509
0
    if (sjs->compositor->hit_appear) return JS_NewBool(ctx, 1);
1510
0
    else if (gf_list_count(sjs->compositor->previous_sensors) ) return JS_NewBool(ctx, 1);
1511
0
    else if (sjs->compositor->text_selection) return JS_NewBool(ctx, 1);
1512
0
    else return JS_NewBool(ctx, 0);
1513
1514
0
  case GJS_EVT_PROP_WHEEL:
1515
0
    return JS_NewFloat64(ctx, FIX2FLT(evt->mouse.wheel_pos));
1516
0
  case GJS_EVT_PROP_BUTTON:
1517
0
    return JS_NewInt32(ctx,  evt->mouse.button);
1518
0
  case GJS_EVT_PROP_TYPE:
1519
0
    return JS_NewInt32(ctx, evt->type);
1520
0
  case GJS_EVT_PROP_NAME:
1521
0
#ifndef GPAC_DISABLE_SVG
1522
0
    return JS_NewString(ctx, gf_dom_event_get_name(evt->type) );
1523
#else
1524
    return JS_NULL;
1525
#endif
1526
1527
0
  case GJS_EVT_PROP_HWKEY:
1528
0
    return JS_NewInt32(ctx, evt->key.hw_code);
1529
0
  case GJS_EVT_PROP_TARGET_URL:
1530
0
    return JS_NewString(ctx, evt->navigate.to_url);
1531
0
  case GJS_EVT_PROP_FILES:
1532
0
  {
1533
0
    u32 i, idx;
1534
0
    JSValue files_array = JS_NewArray(ctx);
1535
0
    idx=0;
1536
0
    for (i=0; i<evt->open_file.nb_files; i++) {
1537
0
      if (evt->open_file.files[i]) {
1538
0
        JS_SetPropertyUint32(ctx, files_array, idx, JS_NewString(ctx, evt->open_file.files[i]) );
1539
0
        idx++;
1540
0
      }
1541
0
    }
1542
0
    return files_array;
1543
0
  }
1544
0
  }
1545
0
  return JS_UNDEFINED;
1546
0
}
1547
1548
static Bool gjs_event_filter_process(GF_SCENEJSExt *sjs, GF_Event *evt)
1549
0
{
1550
0
  s32 res;
1551
0
  JSValue rval;
1552
1553
0
  sjs->evt = evt;
1554
0
  JS_SetOpaque(sjs->evt_obj, sjs);
1555
0
  rval = JS_Call(sjs->c, sjs->evt_fun, sjs->evt_filter_obj, 1, &sjs->evt_obj);
1556
0
  JS_SetOpaque(sjs->evt_obj, NULL);
1557
0
  sjs->evt = NULL;
1558
1559
0
  res = 0;
1560
0
  if (JS_IsBool(rval))
1561
0
    res = JS_ToBool(sjs->c, rval);
1562
0
  else if (JS_IsNumber(rval))
1563
0
    JS_ToInt32(sjs->c, &res, rval);
1564
1565
0
  JS_FreeValue(sjs->c, rval);
1566
0
  return res ? GF_TRUE : GF_FALSE;
1567
0
}
1568
1569
static Bool gjs_event_filter(void *udta, GF_Event *evt, Bool consumed_by_compositor)
1570
0
{
1571
0
  u32 lock_fail;
1572
0
  Bool res;
1573
0
  GF_SCENEJSExt *sjs = (GF_SCENEJSExt *)udta;
1574
0
  if (consumed_by_compositor) return 0;
1575
1576
0
  if (sjs->evt != NULL) return 0;
1577
1578
0
  lock_fail=0;
1579
0
  res = gf_mx_try_lock(sjs->compositor->mx);
1580
0
  if (!res) {
1581
0
    lock_fail=1;
1582
0
  } else {
1583
0
    res = gf_js_try_lock(sjs->c);
1584
0
    if (!res) lock_fail=2;
1585
0
  }
1586
0
  if (lock_fail) {
1587
0
    GF_Event *evt_clone;
1588
0
    gf_mx_p(sjs->event_mx);
1589
0
    evt_clone = gf_malloc(sizeof(GF_Event));
1590
0
    memcpy(evt_clone, evt, sizeof(GF_Event));
1591
0
    gf_list_add(sjs->event_queue, evt_clone);
1592
0
    GF_LOG(GF_LOG_INFO, GF_LOG_COMPOSE, ("[SCENEJS] Couldn't lock % mutex, queuing event\n", (lock_fail==2) ? "JavaScript" : "Compositor"));
1593
0
    gf_mx_v(sjs->event_mx);
1594
1595
0
    if (lock_fail==2){
1596
0
      gf_mx_v(sjs->compositor->mx);
1597
0
    }
1598
0
    return 0;
1599
0
  }
1600
1601
0
  gf_mx_p(sjs->event_mx);
1602
0
  while (gf_list_count(sjs->event_queue)) {
1603
0
    GF_Event *an_evt = (GF_Event *) gf_list_pop_front(sjs->event_queue);
1604
0
    gjs_event_filter_process(sjs, an_evt);
1605
0
    gf_free(an_evt);
1606
0
  }
1607
0
  gf_mx_v(sjs->event_mx);
1608
1609
0
  res = gjs_event_filter_process(sjs, evt);
1610
1611
0
  gf_mx_v(sjs->compositor->mx);
1612
0
  gf_js_lock(sjs->c, 0);
1613
0
  return res;
1614
0
}
1615
1616
static JSValue scenejs_set_event_filter(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
1617
0
{
1618
0
  GF_SCENEJSExt *sjs = JS_GetOpaque(this_val, scene_class_id);
1619
0
  if (!sjs || !argc)
1620
0
    return GF_JS_EXCEPTION(ctx);
1621
1622
0
  if (!JS_IsNull(argv[0]) && !JS_IsUndefined(argv[0]) && !JS_IsFunction(ctx, argv[0]))
1623
0
    return GF_JS_EXCEPTION(ctx);
1624
1625
0
  JS_FreeValue(sjs->c, sjs->evt_fun);
1626
0
  sjs->evt_fun = JS_DupValue(ctx, argv[0]);
1627
0
  sjs->evt_filter_obj = this_val;
1628
0
  sjs->c = ctx;
1629
0
  sjs->evt_filter.udta = sjs;
1630
0
  sjs->evt_filter.on_event = gjs_event_filter;
1631
1632
0
  gf_filter_add_event_listener(sjs->compositor->filter, &sjs->evt_filter);
1633
0
  return JS_UNDEFINED;
1634
0
}
1635
1636
GF_Node *gf_sg_js_get_node(struct JSContext *c, JSValue obj);
1637
1638
static JSValue scenejs_set_focus(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
1639
0
{
1640
0
  GF_Node *elt;
1641
0
  GF_Compositor *compositor = scenejs_get_compositor(ctx, this_val);
1642
0
  if (!compositor || !argc) return GF_JS_EXCEPTION(ctx);
1643
1644
0
  if (JS_IsNull(argv[0])) {
1645
0
    gf_sc_focus_switch_ring(compositor, 0, NULL, 0);
1646
0
    return JS_UNDEFINED;
1647
0
  }
1648
1649
0
  if (JS_IsString(argv[0])) {
1650
0
    const char *focus_type = JS_ToCString(ctx, argv[0]);
1651
0
    if (!stricmp(focus_type, "previous")) {
1652
0
      gf_sc_focus_switch_ring(compositor, 1, NULL, 0);
1653
0
    }
1654
0
    else if (!stricmp(focus_type, "next")) {
1655
0
      gf_sc_focus_switch_ring(compositor, 0, NULL, 0);
1656
0
    }
1657
0
    JS_FreeCString(ctx, focus_type);
1658
0
  } else if (JS_IsObject(argv[0])) {
1659
0
    elt = gf_sg_js_get_node(ctx, argv[0]);
1660
0
    if (!elt) return GF_JS_EXCEPTION(ctx);
1661
0
    gf_sc_focus_switch_ring(compositor, 0, elt, 2);
1662
0
  }
1663
0
  return JS_UNDEFINED;
1664
0
}
1665
1666
#if 0 //unused
1667
static JSValue scenejs_get_scene(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
1668
{
1669
  GF_Node *elt;
1670
  u32 w, h;
1671
  JSValue scene_obj;
1672
#ifndef GPAC_DISABLE_SCENEGRAPH
1673
  GF_SceneGraph *sg;
1674
#endif
1675
  GF_Scene *scene=NULL;
1676
  GF_SCENEJSExt *sjs = (GF_SCENEJSExt *)JS_GetOpaque(this_val, scene_class_id);
1677
  if (!sjs || !argc || !JS_IsObject(argv[0])) return GF_JS_EXCEPTION(ctx);
1678
1679
  elt = gf_sg_js_get_node(ctx, argv[0]);
1680
  if (!elt) return GF_JS_EXCEPTION(ctx);
1681
  switch (elt->sgprivate->tag) {
1682
#ifndef GPAC_DISABLE_VRML
1683
  case TAG_MPEG4_Inline:
1684
    scene = (GF_Scene *)gf_node_get_private(elt);
1685
    break;
1686
#endif
1687
1688
#ifndef GPAC_DISABLE_X3D
1689
  case TAG_X3D_Inline:
1690
    scene = (GF_Scene *)gf_node_get_private(elt);
1691
    break;
1692
#endif
1693
1694
#ifndef GPAC_DISABLE_SVG
1695
  case TAG_SVG_animation:
1696
    sg = gf_sc_animation_get_scenegraph(elt);
1697
    scene = (GF_Scene *)gf_sg_get_private(sg);
1698
    break;
1699
#endif
1700
  default:
1701
    return GF_JS_EXCEPTION(ctx);
1702
  }
1703
  if (!scene) return GF_JS_EXCEPTION(ctx);
1704
1705
1706
  scene_obj = JS_NewObjectClass(ctx, any_class_id);
1707
  if (JS_IsException(scene_obj)) return scene_obj;
1708
  JS_SetOpaque(scene_obj, scene);
1709
  gf_sg_get_scene_size_info(scene->graph, &w, &h);
1710
  JS_SetPropertyStr(ctx, scene_obj, "width", JS_NewInt32(ctx, w));
1711
  JS_SetPropertyStr(ctx, scene_obj, "height", JS_NewInt32(ctx, h));
1712
  JS_SetPropertyStr(ctx, scene_obj, "connected", JS_NewBool(ctx, scene->graph ? 1 : 0));
1713
  return scene_obj;
1714
}
1715
#endif
1716
1717
static JSValue scenejs_show_keyboard(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
1718
0
{
1719
0
  GF_Compositor *compositor = scenejs_get_compositor(ctx, this_val);
1720
0
  if (!compositor || !argc) return GF_JS_EXCEPTION(ctx);
1721
1722
0
  GF_Event evt;
1723
0
  memset(&evt, 0, sizeof(GF_Event));
1724
0
  evt.type = JS_ToBool(ctx, argv[0]) ? GF_EVENT_TEXT_EDITING_START : GF_EVENT_TEXT_EDITING_END;
1725
0
  gf_sc_user_event(compositor, &evt);
1726
0
  return JS_UNDEFINED;
1727
0
}
1728
1729
static const JSCFunctionListEntry scenejs_evt_funcs[] = {
1730
  JS_CGETSET_MAGIC_DEF("keycode", gpacevt_getProperty, NULL, GJS_EVT_PROP_KEYCODE),
1731
  JS_CGETSET_MAGIC_DEF("mouse_x", gpacevt_getProperty, NULL, GJS_EVT_PROP_MOUSE_X),
1732
  JS_CGETSET_MAGIC_DEF("mouse_y", gpacevt_getProperty, NULL, GJS_EVT_PROP_MOUSE_Y),
1733
  JS_CGETSET_MAGIC_DEF("picked", gpacevt_getProperty, NULL, GJS_EVT_PROP_PICKED),
1734
  JS_CGETSET_MAGIC_DEF("wheel", gpacevt_getProperty, NULL, GJS_EVT_PROP_WHEEL),
1735
  JS_CGETSET_MAGIC_DEF("button", gpacevt_getProperty, NULL, GJS_EVT_PROP_BUTTON),
1736
  JS_CGETSET_MAGIC_DEF("type", gpacevt_getProperty, NULL, GJS_EVT_PROP_TYPE),
1737
  JS_CGETSET_MAGIC_DEF("name", gpacevt_getProperty, NULL, GJS_EVT_PROP_NAME),
1738
  JS_CGETSET_MAGIC_DEF("hwkey", gpacevt_getProperty, NULL, GJS_EVT_PROP_HWKEY),
1739
  JS_CGETSET_MAGIC_DEF("url", gpacevt_getProperty, NULL, GJS_EVT_PROP_TARGET_URL),
1740
  JS_CGETSET_MAGIC_DEF("dropfiles", gpacevt_getProperty, NULL, GJS_EVT_PROP_FILES),
1741
};
1742
1743
static const JSCFunctionListEntry scenejs_funcs[] = {
1744
  JS_CGETSET_MAGIC_DEF("fullscreen", scenejs_getProperty, scenejs_setProperty, GJS_SCENE_PROP_FULLSCREEN),
1745
  JS_CGETSET_MAGIC_DEF("current_path", scenejs_getProperty, NULL, GJS_SCENE_PROP_CURRENT_PATH),
1746
  JS_CGETSET_MAGIC_DEF("volume", scenejs_getProperty, scenejs_setProperty, GJS_SCENE_PROP_VOLUME),
1747
  JS_CGETSET_MAGIC_DEF("navigation", scenejs_getProperty, scenejs_setProperty, GJS_SCENE_PROP_NAVIGATION),
1748
  JS_CGETSET_MAGIC_DEF("navigation_type", scenejs_getProperty, scenejs_setProperty, GJS_SCENE_PROP_NAVIGATION_TYPE),
1749
  JS_CGETSET_MAGIC_DEF("hardware_yuv", scenejs_getProperty, NULL, GJS_SCENE_PROP_HARDWARE_YUV),
1750
  JS_CGETSET_MAGIC_DEF("hardware_rgb", scenejs_getProperty, NULL, GJS_SCENE_PROP_HARDWARE_RGB),
1751
  JS_CGETSET_MAGIC_DEF("hardware_rgba", scenejs_getProperty, NULL, GJS_SCENE_PROP_HARDWARE_RGBA),
1752
  JS_CGETSET_MAGIC_DEF("hardware_stretch", scenejs_getProperty, NULL, GJS_SCENE_PROP_HARDWARE_STRETCH),
1753
  JS_CGETSET_MAGIC_DEF("screen_width", scenejs_getProperty, NULL, GJS_SCENE_PROP_SCREEN_WIDTH),
1754
  JS_CGETSET_MAGIC_DEF("screen_height", scenejs_getProperty, NULL, GJS_SCENE_PROP_SCREEN_HEIGHT),
1755
  JS_CGETSET_MAGIC_DEF("fps", scenejs_getProperty, NULL, GJS_SCENE_PROP_FPS),
1756
  JS_CGETSET_MAGIC_DEF("sim_fps", scenejs_getProperty, NULL, GJS_SCENE_PROP_SIM_FPS),
1757
  JS_CGETSET_MAGIC_DEF("has_opengl", scenejs_getProperty, NULL, GJS_SCENE_PROP_HAS_OPENGL),
1758
  JS_CGETSET_MAGIC_DEF("caption", scenejs_getProperty, scenejs_setProperty, GJS_SCENE_PROP_CAPTION),
1759
  JS_CGETSET_MAGIC_DEF("focus_highlight", scenejs_getProperty, scenejs_setProperty, GJS_SCENE_PROP_FOCUS_HIGHLIGHT),
1760
  JS_CGETSET_MAGIC_DEF("dpi_x", scenejs_getProperty, NULL, GJS_SCENE_PROP_DPI_X),
1761
  JS_CGETSET_MAGIC_DEF("dpi_y", scenejs_getProperty, NULL, GJS_SCENE_PROP_DPI_Y),
1762
  JS_CGETSET_MAGIC_DEF("sensors_active", scenejs_getProperty, scenejs_setProperty, GJS_SCENE_PROP_SENSORS_ACTIVE),
1763
  JS_CGETSET_MAGIC_DEF("zoom", scenejs_getProperty, NULL, GJS_SCENE_PROP_ZOOM),
1764
  JS_CGETSET_MAGIC_DEF("text_selection", scenejs_getProperty, NULL, GJS_SCENE_PROP_TEXT_SEL),
1765
  JS_CGETSET_MAGIC_DEF("orientation", scenejs_getProperty, scenejs_setProperty, GJS_SCENE_PROP_DISP_ORIENTATION),
1766
  JS_CFUNC_DEF("get_option", 0, scenejs_get_option),
1767
  JS_CFUNC_DEF("set_option", 0, scenejs_set_option),
1768
  JS_CFUNC_DEF("set_size", 0, scenejs_set_size),
1769
  JS_CFUNC_DEF("exit", 0, scenejs_exit),
1770
  JS_CFUNC_DEF("set_3d", 0, scenejs_set_3d),
1771
  JS_CFUNC_DEF("move_window", 0, scenejs_move_window),
1772
  JS_CFUNC_DEF("set_event_filter", 0, scenejs_set_event_filter),
1773
  JS_CFUNC_DEF("set_focus", 0, scenejs_set_focus),
1774
  JS_CFUNC_DEF("show_keyboard", 0, scenejs_show_keyboard),
1775
  JS_CFUNC_DEF("trigger_gc", 0, scenejs_trigger_gc),
1776
  JS_CFUNC_DEF("get_object_manager", 0, scenejs_get_object_manager),
1777
  JS_CFUNC_DEF("switch_quality", 0, scenejs_switch_quality),
1778
  JS_CFUNC_DEF("navigation_supported", 0, scenejs_navigation_supported),
1779
  JS_CFUNC_DEF("set_back_color", 0, scenejs_set_back_color)
1780
};
1781
1782
static const JSCFunctionListEntry odm_funcs[] = {
1783
  JS_CGETSET_MAGIC_DEF("ID", odm_getProperty, NULL, GJS_OM_PROP_ID),
1784
  JS_CGETSET_MAGIC_DEF("nb_resources", odm_getProperty, NULL, GJS_OM_PROP_NB_RES),
1785
  JS_CGETSET_MAGIC_DEF("service_url", odm_getProperty, NULL, GJS_OM_PROP_URL),
1786
  JS_CGETSET_MAGIC_DEF("duration", odm_getProperty, NULL, GJS_OM_PROP_DUR),
1787
  JS_CGETSET_MAGIC_DEF("clock_time", odm_getProperty, NULL, GJS_OM_PROP_CLOCK),
1788
  JS_CGETSET_MAGIC_DEF("clock_drift", odm_getProperty, NULL, GJS_OM_PROP_DRIFT),
1789
  JS_CGETSET_MAGIC_DEF("status", odm_getProperty, NULL, GJS_OM_PROP_STATUS),
1790
  JS_CGETSET_MAGIC_DEF("buffer", odm_getProperty, NULL, GJS_OM_PROP_BUFFER),
1791
  JS_CGETSET_MAGIC_DEF("db_unit_count", odm_getProperty, NULL, GJS_OM_PROP_DB_COUNT),
1792
  JS_CGETSET_MAGIC_DEF("cb_unit_count", odm_getProperty, NULL, GJS_OM_PROP_CB_COUNT),
1793
  JS_CGETSET_MAGIC_DEF("cb_capacity", odm_getProperty, NULL, GJS_OM_PROP_CB_CAP),
1794
  JS_CGETSET_MAGIC_DEF("type", odm_getProperty, NULL, GJS_OM_PROP_TYPE),
1795
  JS_CGETSET_MAGIC_DEF("samplerate", odm_getProperty, NULL, GJS_OM_PROP_SAMPLERATE),
1796
  JS_CGETSET_MAGIC_DEF("channels", odm_getProperty, NULL, GJS_OM_PROP_CHANNELS),
1797
  JS_CGETSET_MAGIC_DEF("lang", odm_getProperty, NULL, GJS_OM_PROP_LANG),
1798
  JS_CGETSET_MAGIC_DEF("width", odm_getProperty, NULL, GJS_OM_PROP_WIDTH),
1799
  JS_CGETSET_MAGIC_DEF("height", odm_getProperty, NULL, GJS_OM_PROP_HEIGHT),
1800
  JS_CGETSET_MAGIC_DEF("pixelformt", odm_getProperty, NULL, GJS_OM_PROP_PIXELFORMAT),
1801
  JS_CGETSET_MAGIC_DEF("par", odm_getProperty, NULL, GJS_OM_PROP_PAR),
1802
  JS_CGETSET_MAGIC_DEF("dec_frames", odm_getProperty, NULL, GJS_OM_PROP_DEC_FRAMES),
1803
  JS_CGETSET_MAGIC_DEF("drop_frames", odm_getProperty, NULL, GJS_OM_PROP_DROP_FRAMES),
1804
  JS_CGETSET_MAGIC_DEF("max_dec_time", odm_getProperty, NULL, GJS_OM_PROP_DEC_TIME_MAX),
1805
  JS_CGETSET_MAGIC_DEF("total_dec_time", odm_getProperty, NULL, GJS_OM_PROP_DEC_TIME_TOTAL),
1806
  JS_CGETSET_MAGIC_DEF("avg_bitrate", odm_getProperty, NULL, GJS_OM_PROP_AVG_RATE),
1807
  JS_CGETSET_MAGIC_DEF("max_bitrate", odm_getProperty, NULL, GJS_OM_PROP_MAX_RATE),
1808
  JS_CGETSET_MAGIC_DEF("service_handler", odm_getProperty, NULL, GJS_OM_PROP_SERVICE_HANDLER),
1809
  JS_CGETSET_MAGIC_DEF("codec", odm_getProperty, NULL, GJS_OM_PROP_CODEC),
1810
  JS_CGETSET_MAGIC_DEF("nb_views", odm_getProperty, NULL, GJS_OM_PROP_NB_VIEWS),
1811
  JS_CGETSET_MAGIC_DEF("nb_qualities", odm_getProperty, NULL, GJS_OM_PROP_NB_QUALITIES),
1812
  JS_CGETSET_MAGIC_DEF("max_buffer", odm_getProperty, NULL, GJS_OM_PROP_MAX_BUFFER),
1813
  JS_CGETSET_MAGIC_DEF("min_buffer", odm_getProperty, NULL, GJS_OM_PROP_MIN_BUFFER),
1814
  JS_CGETSET_MAGIC_DEF("frame_duration", odm_getProperty, NULL, GJS_OM_PROP_FRAME_DUR),
1815
  JS_CGETSET_MAGIC_DEF("irap_frames", odm_getProperty, NULL, GJS_OM_PROP_NB_IRAP),
1816
  JS_CGETSET_MAGIC_DEF("irap_dec_time", odm_getProperty, NULL, GJS_OM_PROP_IRAP_DEC_TIME),
1817
  JS_CGETSET_MAGIC_DEF("irap_max_time", odm_getProperty, NULL, GJS_OM_PROP_IRAP_MAX_TIME),
1818
  JS_CGETSET_MAGIC_DEF("service_id", odm_getProperty, NULL, GJS_OM_PROP_SERVICE_ID),
1819
  JS_CGETSET_MAGIC_DEF("selected_service", odm_getProperty, NULL, GJS_OM_PROP_SELECTED_SERVICE),
1820
  JS_CGETSET_MAGIC_DEF("bandwidth_down", odm_getProperty, NULL, GJS_OM_PROP_BANDWIDTH_DOWN),
1821
  JS_CGETSET_MAGIC_DEF("nb_http", odm_getProperty, NULL, GJS_OM_PROP_NB_HTTP),
1822
  JS_CGETSET_MAGIC_DEF("timeshift_depth", odm_getProperty, NULL, GJS_OM_PROP_TIMESHIFT_DEPTH),
1823
  JS_CGETSET_MAGIC_DEF("timeshift_time", odm_getProperty, NULL, GJS_OM_PROP_TIMESHIFT_TIME),
1824
  JS_CGETSET_MAGIC_DEF("is_addon", odm_getProperty, NULL, GJS_OM_PROP_IS_ADDON),
1825
  JS_CGETSET_MAGIC_DEF("main_addon_on", odm_getProperty, NULL, GJS_OM_PROP_MAIN_ADDON_ON),
1826
  JS_CGETSET_MAGIC_DEF("is_over", odm_getProperty, NULL, GJS_OM_PROP_IS_OVER),
1827
  JS_CGETSET_MAGIC_DEF("dynamic_scene", odm_getProperty, NULL, GJS_OM_PROP_DYNAMIC_SCENE),
1828
  JS_CGETSET_MAGIC_DEF("service_name", odm_getProperty, NULL, GJS_OM_PROP_SERVICE_NAME),
1829
  JS_CGETSET_MAGIC_DEF("ntp_diff", odm_getProperty, NULL, GJS_OM_PROP_NTP_DIFF),
1830
  JS_CGETSET_MAGIC_DEF("ntp_sender_diff", odm_getProperty, NULL, GJS_OM_PROP_NTP_SENDER_DIFF),
1831
  JS_CGETSET_MAGIC_DEF("main_addon_url", odm_getProperty, NULL, GJS_OM_PROP_MAIN_ADDON_URL),
1832
  JS_CGETSET_MAGIC_DEF("reverse_playback_supported", odm_getProperty, NULL, GJS_OM_PROP_REVERSE_PLAYBACK),
1833
  JS_CGETSET_MAGIC_DEF("scalable_enhancement", odm_getProperty, NULL, GJS_OM_PROP_SCALABLE_ENHANCEMENT),
1834
  JS_CGETSET_MAGIC_DEF("main_addon_media_time", odm_getProperty, NULL, GJS_OM_PROP_MAIN_ADDON_MEDIATIME),
1835
  JS_CGETSET_MAGIC_DEF("dependent_groups", odm_getProperty, NULL, GJS_OM_PROP_DEPENDENT_GROUPS),
1836
  JS_CGETSET_MAGIC_DEF("vr_scene", odm_getProperty, NULL, GJS_OM_PROP_IS_VR_SCENE),
1837
  JS_CGETSET_MAGIC_DEF("disabled", odm_getProperty, NULL, GJS_OM_PROP_DISABLED),
1838
  JS_CGETSET_MAGIC_DEF("buffering", odm_getProperty, NULL, GJS_OM_PROP_BUFFERING),
1839
  JS_CGETSET_MAGIC_DEF("forced", odm_getProperty, NULL, GJS_OM_PROP_FORCED_SUB),
1840
1841
  JS_CFUNC_DEF("declare_addon", 0, gjs_odm_declare_addon),
1842
  JS_CFUNC_DEF("enable_addon", 0, gjs_odm_enable_addon),
1843
  JS_CFUNC_DEF("addon_layout", 0, gjs_odm_addon_layout),
1844
  JS_CFUNC_DEF("get_resource", 0, gjs_odm_get_resource),
1845
  JS_CFUNC_DEF("get_quality", 0, gjs_odm_get_quality),
1846
  JS_CFUNC_DEF("select_service", 0, gjs_odm_select_service),
1847
  JS_CFUNC_DEF("select_quality", 0, gjs_odm_select_quality),
1848
  JS_CFUNC_DEF("disable_main_addon", 0, gjs_odm_disable_main_addon),
1849
  JS_CFUNC_DEF("select", 0, gjs_odm_select),
1850
  JS_CFUNC_DEF("get_srd", 0, gjs_odm_get_srd),
1851
  JS_CFUNC_DEF("in_parent_chain", 0, gjs_odm_in_parent_chain),
1852
  JS_CFUNC_DEF("get_chapters", 0, gjs_odm_get_chapters),
1853
};
1854
1855
#include "../filter_core/filter_session.h"
1856
1857
//we must unload API before the session is destroyed, so not in the finalizer which can be called in gf_sys_close when runtime is destroyeds
1858
void scenejs_unload(JSContext *c, JSValue global_obj)
1859
0
{
1860
0
  JSValue js_sess = JS_GetPropertyStr(c, global_obj, "__scene_js");
1861
0
  GF_SCENEJSExt *sjs = JS_GetOpaque(js_sess, scene_class_id);
1862
0
  if (sjs) {
1863
0
    if (sjs->owns_fs_api && sjs->compositor && sjs->compositor->filter) {
1864
0
      gf_fs_unload_script(sjs->compositor->filter->session, NULL);
1865
0
    }
1866
0
    if (sjs->evt_filter.udta) {
1867
0
      if (sjs->owns_fs_api && sjs->compositor)
1868
0
        gf_filter_remove_event_listener(sjs->compositor->filter, &sjs->evt_filter);
1869
0
      sjs->evt_filter.udta = NULL;
1870
0
    }
1871
0
  }
1872
0
  JS_FreeValue(c, js_sess);
1873
1874
0
}
1875
static void scenejs_finalize(JSRuntime *rt, JSValue obj)
1876
0
{
1877
0
  GF_SCENEJSExt *sjs = JS_GetOpaque(obj, scene_class_id);
1878
0
  if (!sjs) return;
1879
1880
0
  JS_SetOpaque(obj, NULL);
1881
1882
0
  while (gf_list_count(sjs->storages)) {
1883
0
    GF_Config *cfg = (GF_Config *) gf_list_pop_back(sjs->storages);
1884
0
    gf_cfg_discard_changes(cfg);
1885
0
    gf_cfg_del(cfg);
1886
0
  }
1887
0
  gf_list_del(sjs->storages);
1888
1889
0
  while (gf_list_count(sjs->event_queue)) {
1890
0
    GF_Event *evt = (GF_Event *) gf_list_pop_back(sjs->event_queue);
1891
0
    gf_free(evt);
1892
0
  }
1893
0
  gf_list_del(sjs->event_queue);
1894
0
  gf_mx_del(sjs->event_mx);
1895
1896
  /*if we destroy the script context holding the gpac event filter (only one for the time being), remove the filter*/
1897
0
  JS_FreeValueRT(rt, sjs->evt_fun);
1898
1899
0
  gf_free(sjs);
1900
0
}
1901
1902
static int js_scene_init(JSContext *c, JSModuleDef *m)
1903
0
{
1904
0
  GF_JSAPIParam par;
1905
0
  GF_SCENEJSExt *sjs;
1906
0
  GF_SAFEALLOC(sjs, GF_SCENEJSExt);
1907
0
  GF_SceneGraph *scene;
1908
0
  if (!sjs) {
1909
0
    return -1;
1910
0
  }
1911
0
  sjs->storages = gf_list_new();
1912
0
  sjs->event_queue = gf_list_new();
1913
0
  sjs->event_mx = gf_mx_new("GPACJSEvt");
1914
0
  sjs->evt_fun = JS_UNDEFINED;
1915
0
  sjs->scene_obj = JS_UNDEFINED;
1916
0
  sjs->evt_obj = JS_UNDEFINED;
1917
1918
0
  scene = JS_GetContextOpaque(c);
1919
0
  if (!scene) return -1;
1920
0
  if (scene->__reserved_null) {
1921
0
    GF_Node *n = JS_GetContextOpaque(c);
1922
0
    scene = n->sgprivate->scenegraph;
1923
0
  }
1924
1925
0
  if (!scene_class_id) {
1926
0
    JS_NewClassID(&scene_class_id);
1927
0
    JS_NewClass(JS_GetRuntime(c), scene_class_id, &sceneClass);
1928
1929
0
    JS_NewClassID(&odm_class_id);
1930
0
    JS_NewClass(JS_GetRuntime(c), odm_class_id, &odmClass);
1931
0
  }
1932
0
  JSValue proto = JS_NewObjectClass(c, odm_class_id);
1933
0
  JS_SetPropertyFunctionList(c, proto, odm_funcs, countof(odm_funcs));
1934
0
  JS_SetClassProto(c, odm_class_id, proto);
1935
1936
0
  JS_NewClassID(&gpacevt_class_id);
1937
0
  JS_NewClass(JS_GetRuntime(c), gpacevt_class_id, &gpacEvtClass);
1938
1939
0
  JS_NewClassID(&any_class_id);
1940
0
  JS_NewClass(JS_GetRuntime(c), any_class_id, &anyClass);
1941
1942
0
  JSValue global = JS_GetGlobalObject(c);
1943
1944
0
  sjs->scene_obj = JS_NewObjectClass(c, scene_class_id);
1945
0
  JS_SetPropertyFunctionList(c, sjs->scene_obj, scenejs_funcs, countof(scenejs_funcs));
1946
0
  JS_SetOpaque(sjs->scene_obj, sjs);
1947
0
  JS_SetPropertyStr(c, global, "__scene_js", JS_DupValue(c, sjs->scene_obj) );
1948
1949
0
  if (scene->script_action) {
1950
0
    if (scene->script_action(scene->script_action_cbck, GF_JSAPI_OP_GET_COMPOSITOR, scene->RootNode, &par)) {
1951
0
      sjs->compositor = par.compositor;
1952
0
    }
1953
0
  }
1954
0
  if (sjs->compositor && sjs->compositor->filter) {
1955
0
    GF_Err gf_fs_load_js_api(JSContext *c, GF_FilterSession *fs);
1956
0
    GF_FilterSession *fs = sjs->compositor->filter->session;
1957
1958
    //don't check error code, this may fail if global JS has been set but the script may still run
1959
0
    if (gf_fs_load_js_api(c, fs) == GF_OK) {
1960
0
      scene->attached_session = fs;
1961
0
      sjs->owns_fs_api = GF_TRUE;
1962
0
    }
1963
0
  }
1964
1965
0
  sjs->evt_obj = JS_NewObjectClass(c, gpacevt_class_id);
1966
0
  JS_SetPropertyFunctionList(c, sjs->evt_obj, scenejs_evt_funcs, countof(scenejs_evt_funcs));
1967
0
  JS_SetOpaque(sjs->evt_obj, NULL);
1968
0
  JS_SetPropertyStr(c, global, "gpacevt", sjs->evt_obj);
1969
1970
0
#define DECLARE_CONST(_val) \
1971
0
  JS_SetPropertyStr(c, global, #_val, JS_NewInt32(c, _val));
1972
1973
0
  DECLARE_CONST(GF_EVENT_CLICK);
1974
0
  DECLARE_CONST(GF_EVENT_MOUSEUP);
1975
0
  DECLARE_CONST(GF_EVENT_MOUSEDOWN);
1976
0
  DECLARE_CONST(GF_EVENT_MOUSEMOVE);
1977
0
  DECLARE_CONST(GF_EVENT_MOUSEWHEEL);
1978
0
  DECLARE_CONST(GF_EVENT_DBLCLICK);
1979
0
  DECLARE_CONST(GF_EVENT_KEYUP);
1980
0
  DECLARE_CONST(GF_EVENT_KEYDOWN);
1981
0
  DECLARE_CONST(GF_EVENT_TEXTINPUT);
1982
0
  DECLARE_CONST(GF_EVENT_CONNECT);
1983
0
  DECLARE_CONST(GF_EVENT_NAVIGATE_INFO);
1984
0
  DECLARE_CONST(GF_EVENT_NAVIGATE);
1985
0
  DECLARE_CONST(GF_EVENT_DROPFILE);
1986
0
  DECLARE_CONST(GF_EVENT_ADDON_DETECTED);
1987
0
  DECLARE_CONST(GF_EVENT_QUALITY_SWITCHED);
1988
0
  DECLARE_CONST(GF_EVENT_TIMESHIFT_DEPTH);
1989
0
  DECLARE_CONST(GF_EVENT_TIMESHIFT_UPDATE);
1990
0
  DECLARE_CONST(GF_EVENT_TIMESHIFT_OVERFLOW);
1991
0
  DECLARE_CONST(GF_EVENT_TIMESHIFT_UNDERRUN);
1992
0
  DECLARE_CONST(GF_EVENT_QUIT);
1993
0
  DECLARE_CONST(GF_EVENT_MAIN_ADDON_STATE);
1994
0
  DECLARE_CONST(GF_EVENT_SCENE_SIZE);
1995
1996
0
  DECLARE_CONST(GF_NAVIGATE_NONE);
1997
0
  DECLARE_CONST(GF_NAVIGATE_WALK);
1998
0
  DECLARE_CONST(GF_NAVIGATE_FLY);
1999
0
  DECLARE_CONST(GF_NAVIGATE_PAN);
2000
0
  DECLARE_CONST(GF_NAVIGATE_GAME);
2001
0
  DECLARE_CONST(GF_NAVIGATE_SLIDE);
2002
0
  DECLARE_CONST(GF_NAVIGATE_EXAMINE);
2003
0
  DECLARE_CONST(GF_NAVIGATE_ORBIT);
2004
0
  DECLARE_CONST(GF_NAVIGATE_VR);
2005
2006
0
  DECLARE_CONST(GF_NAVIGATE_TYPE_NONE);
2007
0
  DECLARE_CONST(GF_NAVIGATE_TYPE_2D);
2008
0
  DECLARE_CONST(GF_NAVIGATE_TYPE_3D);
2009
2010
0
  JS_FreeValue(c, global);
2011
2012
0
  JS_SetModuleExport(c, m, "scene", sjs->scene_obj);
2013
0
  return 0;
2014
0
}
2015
2016
2017
void qjs_module_init_scenejs(JSContext *ctx)
2018
0
{
2019
0
  JSModuleDef *m;
2020
0
  m = JS_NewCModule(ctx, "scenejs", js_scene_init);
2021
0
  if (!m) return;
2022
2023
0
  JS_AddModuleExport(ctx, m, "scene");
2024
0
  return;
2025
0
}
2026
2027
2028
#endif
2029