Nimbin[12]?Web & Tools / Browser / commits / 96b2544

Browser git · main

Minimal browser in C (mini and big variant)

browser c · first commit 2026-08-25 · last commit 2026-11-12 (12 days ago) · synced 1 hour ago · upstream: github.com/nimbin2/Browser

C 95.6% Markdown 4.2%
git clone https://git.christianimmanuel.de/web-tools/Browser.gitwget https://git.christianimmanuel.de/web-tools/Browser/archive/Browser.tar.gz

Fix loop short videos from script, not WebKit's loop attribute

Christian Immanuel · 2026-11-10 19:01 · 96b2544e4bda1c92f762badc3b0e1de6a68c30de

 Makefile      |  2 +-
 README.md     | 24 ++++++++++++++++++++-
 browser-big.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 91 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile
index ab8d52b..f3e80af 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ CC      ?= gcc
 CFLAGS  ?= -O2 -Wall -Wextra -rdynamic
 PREFIX  ?= /usr
 
-VERSION  := 4.43.0
+VERSION  := 4.44.0
 SOURCES  := browser_core.c browser_core.h browser-mini.c browser-big.c Makefile
 BUILD_ID := $(shell cat $(SOURCES) 2>/dev/null | md5sum | cut -c1-7)
 
diff --git a/README.md b/README.md
index 52a9fe9..f06ee49 100644
--- a/README.md
+++ b/README.md
@@ -30,7 +30,7 @@ make                # build both
 make install        # into /usr/bin (PREFIX=... to change)
 make uninstall
 make clean
-make version        # 4.43.0 (build …), md5 of the sources
+make version        # 4.44.0 (build …), md5 of the sources
 ```
 
 ## Usage
@@ -268,3 +268,25 @@ enough. The process slept rather than spun, and no watchdog saw it.
 The suspend is off by default now. Offscreen videos keep decoding, which
 costs CPU on a long scroller. `--media-suspend`, or `media_suspend = yes`,
 hands it back to WebKit.
+
+## When a looping clip flickers or sticks on one frame
+
+A second, unrelated bug with the same pages. WebKit's `loop` attribute
+wedges on a short clip: it seeks back to zero at the end and the seek
+never finishes, so `currentTime` stops, `seeking` stays true, and the
+element keeps showing one stale frame.
+
+A seek from script completes every time. So the attribute is taken off
+and the loop runs in the shim, which is on by default. Measured on a
+1.7 s clip over 24 s:
+
+| | loops | `currentTime` | `seeking` |
+| --- | --- | --- | --- |
+| default | 12 | advancing | false |
+| `--no-loop-fix` | 1 | stuck at 0 | true |
+
+`--rtc-trace` logs a `loop-fix` line per element. `--no-loop-fix`, or
+`loop_fix = no`, leaves it to WebKit.
+
+Native `loop` fires no `ended` event and this does, so the shim stops
+the event rather than letting a page see one it never expected.
diff --git a/browser-big.c b/browser-big.c
index 1daee51..385941c 100644
--- a/browser-big.c
+++ b/browser-big.c
@@ -43,6 +43,7 @@ static guint    g_capture_watch;
 
 /* camera / media behaviour */
 static gboolean g_cam_fix = TRUE;          /* install the shim (--no-cam-fix: nothing) */
+static gboolean g_loop_fix = TRUE;         /* loop short videos from script, not WebKit */
 /*
  * The shim carries more than camera repairs: --rtc-trace, --web-compat,
  * --no-mic and the offer fixes all ride in it. Installing it must not
@@ -310,6 +311,57 @@ static const char *SHIM_JS =
 "}"
 "if(C.watchdog)watchdog();"
 
+/* ---- looping video ----
+ *
+ * WebKit's own loop attribute wedges on a short clip. At the end it
+ * seeks back to zero and that seek never completes: currentTime stops,
+ * seeking stays true, and the element keeps presenting one stale frame,
+ * which on a page of looping clips reads as flicker. A seek issued from
+ * script does complete - a 1.7s clip looped eleven times without once
+ * reporting seeking - so take the attribute off and do the loop here.
+ *
+ * Videos arrive as the page scrolls, and a clip that short can wedge
+ * before any poll would reach it, so they are caught three ways: a scan
+ * now, a MutationObserver for inserted elements and for the attribute
+ * being set later, and play() itself, which anything looping must call.
+ *
+ * Native loop fires no ended event, and this does, so the event is
+ * stopped here rather than left to reach a page that never expected
+ * one. --no-loop-fix turns the whole thing off.
+ */
+"if(C.loopFix){(function(){"
+"  function fix(v){"
+"    if(v.__bbLoop)return;"
+"    if(!v.loop&&!v.hasAttribute('loop'))return;"
+"    v.__bbLoop=1;"
+"    try{v.loop=false;v.removeAttribute('loop');}catch(e){}"
+"    v.addEventListener('ended',function(e){"
+"      try{e.stopImmediatePropagation();}catch(x){}"
+"      try{v.currentTime=0;var p=v.play();if(p&&p.catch)p.catch(function(){});}catch(x){}"
+"    },true);"
+"    post({ev:'loop-fix',dur:Math.round((v.duration||0)*100)/100,"
+"          src:String(v.currentSrc||v.src||'').slice(-48)});"
+"  }"
+"  function scan(r){"
+"    if(!r)return;"
+"    if(r.tagName==='VIDEO')fix(r);"
+"    if(r.querySelectorAll){var l=r.querySelectorAll('video');"
+"      for(var i=0;i<l.length;i++)fix(l[i]);}"
+"  }"
+"  scan(document);"
+"  try{new MutationObserver(function(ms){"
+"    for(var i=0;i<ms.length;i++){var m=ms[i];"
+"      if(m.type==='attributes'){fix(m.target);continue;}"
+"      for(var j=0;m.addedNodes&&j<m.addedNodes.length;j++)scan(m.addedNodes[j]);"
+"    }"
+"  }).observe(document.documentElement||document,"
+"             {childList:true,subtree:true,attributes:true,attributeFilter:['loop']});"
+"  }catch(e){}"
+"  try{var LP=HTMLMediaElement.prototype,oP=LP.play;"
+"    LP.play=function(){if(this.tagName==='VIDEO')fix(this);"
+"                       return oP.apply(this,arguments);};}catch(e){}"
+"})();}"
+
 /* ---- getUserMedia ---- */
 "var md=navigator.mediaDevices;"
 "if(!C.camfix||!md||!md.getUserMedia){post({ev:'shim',camfix:false});return;}"
@@ -1862,6 +1914,7 @@ on_script_message (WebKitUserContentManager *ucm, JSCValue *value, gpointer u)
         ev_is (s, "play-rejected") || ev_is (s, "pt-fixed") ||
         ev_is (s, "mic-pin") || ev_is (s, "cam-pin") ||
         ev_is (s, "video-attach") || ev_is (s, "mic-swapped") ||
+        ev_is (s, "loop-fix") ||
         ev_is (s, "mic-swap-failed") || ev_is (s, "video-audio-split") ||
         ev_is (s, "video-audio-rejoin") || ev_is (s, "gum-fps-loosened") ||
         ev_is (s, "rtc-wrapped")   || ev_is (s, "rtc-missing") ||
@@ -2049,7 +2102,7 @@ build_shim (void)
         "maxW:%d,maxH:%d,maxFps:%d,"
         "ssrcFix:%s,codecs:%s,rtcTrace:%s,paramsFix:%s,silentSplit:%s,"
         "compat:%s,noMic:%s,fpsLoose:%s,"
-        "match:%s,micMatch:%s,watchdog:%s,stall:%d,debug:%s};",
+        "match:%s,micMatch:%s,watchdog:%s,stall:%d,loopFix:%s,debug:%s};",
         g_cam_fix        ? "true" : "false",
         (g_cam_repair && g_cam_relax)      ? "true" : "false",
         (g_cam_repair && g_cam_keepalive)  ? "true" : "false",
@@ -2067,6 +2120,7 @@ build_shim (void)
         match, micmatch,
         g_media_watchdog ? "true" : "false",
         g_stall_timeout,
+        g_loop_fix       ? "true" : "false",
         g_shim_debug     ? "true" : "false");
 
     char *js = g_strconcat (cfg, SHIM_JS, NULL);
@@ -2338,6 +2392,11 @@ big_usage_options (GString *s)
 "webrtc (the repairs below are on by default):\n"
 "  --no-fix-webrtc     turn off --sdp-ssrc-fix, --rtc-params-fix and the\n"
 "                      silent-audio split at once (fix_webrtc = no)\n"
+"  --no-loop-fix       leave looping videos to WebKit. On by default:\n"
+"                      WebKit's loop attribute wedges mid-seek on a short\n"
+"                      clip, so the element freezes on one frame. The fix\n"
+"                      takes the attribute off and loops from script\n"
+"                      (loop_fix = no)\n"
 "  --rtc-params-fix    supply RTCRtpSendParameters.codecs when a library\n"
 "                      omits it. WebKit requires it and Firefox does not,\n"
 "                      so a library on its Firefox path throws\n"
@@ -2525,6 +2584,7 @@ big_parse_arg (int argc, char **argv, int *i)
         return TRUE;
     }
     if (!strcmp (a, "--no-fix-webrtc"))  { set_fix_webrtc (FALSE); return TRUE; }
+    if (!strcmp (a, "--no-loop-fix"))    { g_loop_fix = FALSE; return TRUE; }
     if (!strcmp (a, "--no-cam-size-fix") || !strcmp (a, "--no-framerate-fix")) {
         g_fps_loose = FALSE;
         return TRUE;
@@ -2673,6 +2733,11 @@ truthy_value (const char *v)
 static gboolean
 big_cfg_set (const char *key, const char *value)
 {
+    /* loop_fix = no  (the same as --no-loop-fix) */
+    if (!g_ascii_strcasecmp (key, "loop_fix")) {
+        g_loop_fix = truthy_value (value);
+        return TRUE;
+    }
     /*
      * cam_share = yes
      *
@@ -3059,7 +3124,7 @@ big_startup (void)
     if (g_media_debug)
         dump_gstreamer_env ();
 
-    if (g_cam_fix || g_media_watchdog)
+    if (g_cam_fix || g_media_watchdog || g_loop_fix)
         g_shim_js = build_shim ();
 
     /* runs once the main loop starts, i.e. after the window is up */