From 8fcb2d1554aa1fe7dc1e4bb085e6c0db84dcd30f Mon Sep 17 00:00:00 2001 From: ed Date: Wed, 7 Sep 2022 21:08:09 +0200 Subject: [PATCH] defer actx until needed (audioplayer, uploads) and try to be less reliant on the actx speedhack for upload performance --- copyparty/web/browser.js | 52 ++++++++++++++++++++++++++++++---------- copyparty/web/up2k.js | 22 ++++++++++++++--- copyparty/web/util.js | 2 +- 3 files changed, 59 insertions(+), 17 deletions(-) diff --git a/copyparty/web/browser.js b/copyparty/web/browser.js index 63d81a5e..a7b0ed6a 100644 --- a/copyparty/web/browser.js +++ b/copyparty/web/browser.js @@ -1013,7 +1013,6 @@ function set_files_html(html) { var ACtx = window.AudioContext || window.webkitAudioContext, - actx = ACtx && new ACtx(), hash0 = location.hash, mp; @@ -1907,8 +1906,36 @@ function ev_play(e) { } -var audio_eq = (function () { - var r = { +var actx = null, + audio_eq = null; + +function start_actx() { + // bonus: speedhack for unfocused file hashing (removes 1sec delay on subtle.digest resolves) + if (!actx) { + if (!ACtx) + return; + + actx = new ACtx(); + console.log('actx created'); + } + try { + if (actx.state == 'suspended') { + actx.resume(); + setTimeout(function () { + console.log('actx is ' + actx.state); + }, 500); + } + } + catch (ex) { + console.log('actx start failed; ' + ex); + } +} + +function create_eq() { + if (audio_eq) + return start_actx(); + + var r = audio_eq = { "en": false, "bands": [31.25, 62.5, 125, 250, 500, 1000, 2000, 4000, 8000, 16000], "gains": [4, 3, 2, 1, 0, 0, 1, 2, 3, 4], @@ -1919,6 +1946,7 @@ var audio_eq = (function () { "acst": {} }; + start_actx(); if (!actx) ebi('audio_eq').parentNode.style.display = 'none'; @@ -2179,8 +2207,7 @@ var audio_eq = (function () { bcfg_bind(r, 'en', 'au_eq', false, r.apply); r.draw(); - return r; -})(); +} // plays the tid'th audio file on the page @@ -2258,6 +2285,7 @@ function play(tid, is_ev, seek) { else mp.au.src = mp.au.rsrc = url; + create_eq(); audio_eq.apply(); setTimeout(function () { @@ -2277,12 +2305,6 @@ function play(tid, is_ev, seek) { if (window.thegrid) thegrid.loadsel(); - try { - if (actx.state == 'suspended') - actx.resume(); - } - catch (ex) { } - try { mp.au.play(); if (mp.au.paused) @@ -6197,7 +6219,9 @@ ebi('files').onclick = ebi('docul').onclick = function (e) { function reload_mp() { if (mp && mp.au) { - audio_eq.stop(); + if (audio_eq) + audio_eq.stop(); + mp.au.pause(); mp.au = null; mpl.unbuffer(); @@ -6208,7 +6232,9 @@ function reload_mp() { plays[a].parentNode.innerHTML = '-'; mp = new MPlayer(); - audio_eq.acst = {}; + if (audio_eq) + audio_eq.acst = {}; + setTimeout(pbar.onresize, 1); } diff --git a/copyparty/web/up2k.js b/copyparty/web/up2k.js index 61be1cdb..c4eadda7 100644 --- a/copyparty/web/up2k.js +++ b/copyparty/web/up2k.js @@ -809,7 +809,7 @@ function up2k_init(subtle) { bcfg_bind(uc, 'turbo', 'u2turbo', turbolvl > 1, draw_turbo, false); bcfg_bind(uc, 'datechk', 'u2tdate', turbolvl < 3, null, false); bcfg_bind(uc, 'az', 'u2sort', u2sort.indexOf('n') + 1, set_u2sort, false); - bcfg_bind(uc, 'hashw', 'hashw', !!window.WebAssembly && (!HTTPS || !CHROME || MOBILE), set_hashw, false); + bcfg_bind(uc, 'hashw', 'hashw', !!window.WebAssembly && (!subtle || !CHROME || MOBILE), set_hashw, false); var st = { "files": [], @@ -882,6 +882,7 @@ function up2k_init(subtle) { set_fsearch(); function nav() { + start_actx(); ebi('file' + fdom_ctr).click(); } ebi('u2btn').onclick = nav; @@ -1044,6 +1045,7 @@ function up2k_init(subtle) { } dst.push([fobj, fobj.name]); } + start_actx(); // good enough for chrome; not firefox return read_dirs(null, [], dirs, good_files, nil_files, bad_files); } @@ -1149,6 +1151,7 @@ function up2k_init(subtle) { msg += L.u_just1; return modal.alert(msg, function () { + start_actx(); gotallfiles(good_files, nil_files, []); }); } @@ -1160,8 +1163,10 @@ function up2k_init(subtle) { msg += L.u_just1; return modal.confirm(msg, function () { + start_actx(); gotallfiles(good_files.concat(nil_files), [], []); }, function () { + start_actx(); gotallfiles(good_files, [], []); }); } @@ -1178,14 +1183,16 @@ function up2k_init(subtle) { if (uc.ask_up && !uc.fsearch) return modal.confirm(msg.join('') + '', function () { + start_actx(); up_them(good_files); - toast.inf(15, L.u_unpt); + toast.inf(15, L.u_unpt, L.u_unpt); }, null); up_them(good_files); } function up_them(good_files) { + start_actx(); var evpath = get_evpath(), draw_each = good_files.length < 50; @@ -1267,6 +1274,13 @@ function up2k_init(subtle) { pvis.changecard(pvis.act); } ebi('u2tabw').className = 'ye'; + + setTimeout(function () { + if (!actx || actx.state != 'suspended' || toast.tag == L.u_unpt) + return; + + toast.warn(30, "
please click this text to
unlock full upload speed
"); + }, 500); } function more_one_file() { @@ -1485,6 +1499,7 @@ function up2k_init(subtle) { if (!is_busy) { uptoast(); + //throw console.hist.join('\n'); } else { timer.add(donut.do); @@ -1706,7 +1721,8 @@ function up2k_init(subtle) { pvis.setab(t.n, nchunks); pvis.move(t.n, 'bz'); - if (nchunks > 1 && hws.length && uc.hashw) + if (hws.length && uc.hashw && (nchunks > 1 || document.visibilityState == 'hidden')) + // resolving subtle.digest w/o worker takes 1sec on blur if the actx hack breaks return wexec_hash(t, chunksize, nchunks); var segm_next = function () { diff --git a/copyparty/web/util.js b/copyparty/web/util.js index 147606d6..56c7cad2 100644 --- a/copyparty/web/util.js +++ b/copyparty/web/util.js @@ -134,7 +134,7 @@ function vis_exh(msg, url, lineNo, columnNo, error) { '

you hit a bug!

', '

try to reset copyparty settings if you are stuck here, or ignore this / ignore all

', '

please send me a screenshot arigathanks gozaimuch: github issue or ed#2644

', - '

' + esc(url + ' @' + lineNo + ':' + columnNo), '
' + esc(String(msg)) + '

', + '

' + esc(url + ' @' + lineNo + ':' + columnNo), '
' + esc(String(msg)).replace(/\n/g, '
') + '

', '

UA: ' + esc(navigator.userAgent + '') ];