feat: Jam genauer synchron (Tempo-Feinabgleich, Messzeitpunkt, Spul-Vorhalt)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -17,6 +17,7 @@ import okhttp3.WebSocketListener
|
|||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
import kotlin.math.abs
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Playtube Jam: Verbindung zum Jam-Server und gemeinsamer Zustand fuer [JamActivity] (Anzeige)
|
* Playtube Jam: Verbindung zum Jam-Server und gemeinsamer Zustand fuer [JamActivity] (Anzeige)
|
||||||
@@ -382,11 +383,15 @@ object JamHub {
|
|||||||
fun syncPlayer(probe: JSONObject): Actions {
|
fun syncPlayer(probe: JSONObject): Actions {
|
||||||
val s = state ?: return Actions(null, emptyList())
|
val s = state ?: return Actions(null, emptyList())
|
||||||
val now = SystemClock.elapsedRealtime()
|
val now = SystemClock.elapsedRealtime()
|
||||||
val want = JamSync.target(s, clock.serverNow(System.currentTimeMillis().toDouble()))
|
// Soll-Position zum Zeitpunkt der Messung in der Seite (nicht erst nach der Antwort).
|
||||||
|
val wall = System.currentTimeMillis().toDouble()
|
||||||
|
val sampled = probe.optDouble("now", wall).takeIf { it.isFinite() && abs(it - wall) < 5000 } ?: wall
|
||||||
|
val want = JamSync.target(s, clock.serverNow(sampled))
|
||||||
val local = JamSync.Local(
|
val local = JamSync.Local(
|
||||||
videoId = probe.optString("id").takeIf { it.isNotEmpty() && it != "null" && !probe.isNull("id") },
|
videoId = probe.optString("id").takeIf { it.isNotEmpty() && it != "null" && !probe.isNull("id") },
|
||||||
pos = probe.optDouble("t", 0.0),
|
pos = probe.optDouble("t", 0.0),
|
||||||
paused = probe.optBoolean("paused", true),
|
paused = probe.optBoolean("paused", true),
|
||||||
|
rate = probe.optDouble("rate", 1.0).takeIf { it.isFinite() && it > 0 } ?: 1.0,
|
||||||
)
|
)
|
||||||
val duration = probe.optDouble("d", 0.0)
|
val duration = probe.optDouble("d", 0.0)
|
||||||
val scripts = ArrayList<String>()
|
val scripts = ArrayList<String>()
|
||||||
@@ -477,6 +482,7 @@ object JamHub {
|
|||||||
expectUntil = now + 3000
|
expectUntil = now + 3000
|
||||||
scripts.add(pause)
|
scripts.add(pause)
|
||||||
}
|
}
|
||||||
|
is JamSync.Step.Rate -> scripts.add(JamSync.rateScript(step.rate))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Actions(load, scripts)
|
return Actions(load, scripts)
|
||||||
|
|||||||
@@ -13,7 +13,25 @@ import kotlin.math.max
|
|||||||
* paused ? position : position + (T - ref) / 1000. Negativ heisst "startet gleich".
|
* paused ? position : position + (T - ref) / 1000. Negativ heisst "startet gleich".
|
||||||
*/
|
*/
|
||||||
object JamSync {
|
object JamSync {
|
||||||
const val TOLERANCE_S = 1.5
|
const val TOLERANCE_S = 1.0
|
||||||
|
|
||||||
|
/** Beim Spulen etwas vorhalten: bis das Video an der neuen Stelle spielt, vergeht Zeit. */
|
||||||
|
const val SEEK_LEAD_S = 0.2
|
||||||
|
private const val NUDGE_START_S = 0.08
|
||||||
|
private const val NUDGE_STOP_S = 0.03
|
||||||
|
private const val NUDGE_GAIN = 0.25
|
||||||
|
private const val NUDGE_MAX = 0.08
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wiedergabetempo, das eine kleine Abweichung [drift] (eigene minus Jam-Position, s) ausgleicht -
|
||||||
|
* unhoerbar, die Tonhoehe bleibt. Einmal angefangen ([current] != 1) bis fast 0 ausgleichen.
|
||||||
|
*/
|
||||||
|
fun nudgeRate(drift: Double, current: Double): Double {
|
||||||
|
val threshold = if (abs(current - 1.0) > 0.001) NUDGE_STOP_S else NUDGE_START_S
|
||||||
|
if (!drift.isFinite() || abs(drift) < threshold) return 1.0
|
||||||
|
val rate = (1.0 - NUDGE_GAIN * drift).coerceIn(1.0 - NUDGE_MAX, 1.0 + NUDGE_MAX)
|
||||||
|
return Math.round(rate * 1000.0) / 1000.0
|
||||||
|
}
|
||||||
private const val CODE_ALPHABET = "ABCDEFGHJKMNPQRSTUVWXYZ23456789"
|
private const val CODE_ALPHABET = "ABCDEFGHJKMNPQRSTUVWXYZ23456789"
|
||||||
private val VIDEO_ID = Regex("^[A-Za-z0-9_-]{11}$")
|
private val VIDEO_ID = Regex("^[A-Za-z0-9_-]{11}$")
|
||||||
|
|
||||||
@@ -207,13 +225,14 @@ object JamSync {
|
|||||||
return Target(item.videoId, item.id, positionAt(state.playback, serverNow), state.playback.paused, item.dur)
|
return Target(item.videoId, item.id, positionAt(state.playback, serverNow), state.playback.paused, item.dur)
|
||||||
}
|
}
|
||||||
|
|
||||||
data class Local(val videoId: String?, val pos: Double, val paused: Boolean)
|
data class Local(val videoId: String?, val pos: Double, val paused: Boolean, val rate: Double = 1.0)
|
||||||
|
|
||||||
sealed class Step {
|
sealed class Step {
|
||||||
data class Load(val videoId: String, val pos: Double) : Step()
|
data class Load(val videoId: String, val pos: Double) : Step()
|
||||||
data class Seek(val pos: Double) : Step()
|
data class Seek(val pos: Double) : Step()
|
||||||
data object Play : Step()
|
data object Play : Step()
|
||||||
data object Pause : Step()
|
data object Pause : Step()
|
||||||
|
data class Rate(val rate: Double) : Step()
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Was der eigene Player tun soll, damit er zum Jam passt. */
|
/** Was der eigene Player tun soll, damit er zum Jam passt. */
|
||||||
@@ -233,9 +252,14 @@ object JamSync {
|
|||||||
if (local.pos > 0.5) steps.add(Step.Seek(0.0))
|
if (local.pos > 0.5) steps.add(Step.Seek(0.0))
|
||||||
return steps
|
return steps
|
||||||
}
|
}
|
||||||
if (abs(local.pos - want.pos) > tolerance && !(nearEnd && local.pos > want.pos)) steps.add(Step.Seek(want.pos))
|
val drift = local.pos - want.pos
|
||||||
|
val seek = abs(drift) > tolerance && !(nearEnd && local.pos > want.pos)
|
||||||
|
if (seek) steps.add(Step.Seek(if (want.paused) want.pos else want.pos + SEEK_LEAD_S))
|
||||||
if (want.paused && !local.paused) steps.add(Step.Pause)
|
if (want.paused && !local.paused) steps.add(Step.Pause)
|
||||||
if (!want.paused && local.paused) steps.add(Step.Play)
|
if (!want.paused && local.paused) steps.add(Step.Play)
|
||||||
|
// Feinabgleich ueber das Tempo, solange beide normal laufen.
|
||||||
|
val desired = if (!seek && !want.paused && !local.paused && !nearEnd) nudgeRate(drift, local.rate) else 1.0
|
||||||
|
if (abs(desired - local.rate) > 0.004) steps.add(Step.Rate(desired))
|
||||||
return steps
|
return steps
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,6 +295,8 @@ object JamSync {
|
|||||||
d: api('getDuration') && player.getDuration() > 0 ? player.getDuration() : ((video && isFinite(video.duration)) ? video.duration : 0),
|
d: api('getDuration') && player.getDuration() > 0 ? player.getDuration() : ((video && isFinite(video.duration)) ? video.duration : 0),
|
||||||
paused: paused,
|
paused: paused,
|
||||||
ended: state === 0 || (video ? video.ended : false),
|
ended: state === 0 || (video ? video.ended : false),
|
||||||
|
rate: video && video.playbackRate > 0 ? video.playbackRate : 1,
|
||||||
|
now: Date.now(),
|
||||||
ad: ad,
|
ad: ad,
|
||||||
error: error,
|
error: error,
|
||||||
title: meta && meta.title ? meta.title : null,
|
title: meta && meta.title ? meta.title : null,
|
||||||
@@ -288,6 +314,14 @@ object JamSync {
|
|||||||
"var v=p&&p.querySelector('video')||document.querySelector('video');if(v){v.$prop();}})();"
|
"var v=p&&p.querySelector('video')||document.querySelector('video');if(v){v.$prop();}})();"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Wiedergabetempo des Players (Feinabgleich, Tonhoehe bleibt). */
|
||||||
|
fun rateScript(rate: Double): String {
|
||||||
|
val r = if (rate.isFinite()) rate.coerceIn(0.5, 2.0) else 1.0
|
||||||
|
return "(function(){var p=document.getElementById('movie_player');" +
|
||||||
|
"var v=p&&p.querySelector('video')||document.querySelector('video');" +
|
||||||
|
"if(v){v.preservesPitch=true;v.playbackRate=$r;}})();"
|
||||||
|
}
|
||||||
|
|
||||||
fun seekScript(seconds: Double): String {
|
fun seekScript(seconds: Double): String {
|
||||||
val s = if (seconds.isFinite()) max(0.0, seconds) else 0.0
|
val s = if (seconds.isFinite()) max(0.0, seconds) else 0.0
|
||||||
return "(function(){var p=document.getElementById('movie_player');var s=$s;" +
|
return "(function(){var p=document.getElementById('movie_player');var s=$s;" +
|
||||||
|
|||||||
@@ -42,6 +42,22 @@ class JamSyncTest {
|
|||||||
assertNull(JamSync.videoIdFromSharedText("kein Link hier"))
|
assertNull(JamSync.videoIdFromSharedText("kein Link hier"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `gleicht kleine Abweichungen ueber das Tempo aus`() {
|
||||||
|
val want = JamSync.Target("aaaaaaaaaaa", "i", 30.0, false, 200.0)
|
||||||
|
fun local(pos: Double, rate: Double = 1.0, paused: Boolean = false) = JamSync.Local("aaaaaaaaaaa", pos, paused, rate)
|
||||||
|
assertEquals(listOf(JamSync.Step.Rate(0.92)), JamSync.decide(want, local(30.4)))
|
||||||
|
assertEquals(listOf(JamSync.Step.Rate(1.05)), JamSync.decide(want, local(29.8)))
|
||||||
|
assertEquals(listOf(JamSync.Step.Rate(1.0)), JamSync.decide(want, local(30.01, rate = 1.05)))
|
||||||
|
assertEquals(emptyList<JamSync.Step>(), JamSync.decide(want, local(30.05)))
|
||||||
|
assertEquals(
|
||||||
|
listOf(JamSync.Step.Seek(30.0 + JamSync.SEEK_LEAD_S), JamSync.Step.Rate(1.0)),
|
||||||
|
JamSync.decide(want, local(34.0, rate = 1.08)),
|
||||||
|
)
|
||||||
|
assertEquals(0.988, JamSync.nudgeRate(0.05, 1.02), 1e-9)
|
||||||
|
assertEquals(1.0, JamSync.nudgeRate(0.05, 1.0), 1e-9)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `liest Playlist-Links`() {
|
fun `liest Playlist-Links`() {
|
||||||
assertEquals("PLabc_1", JamSync.parsePlaylistId("https://music.youtube.com/playlist?list=PLabc_1"))
|
assertEquals("PLabc_1", JamSync.parsePlaylistId("https://music.youtube.com/playlist?list=PLabc_1"))
|
||||||
@@ -110,8 +126,8 @@ class JamSyncTest {
|
|||||||
@Test
|
@Test
|
||||||
fun `spult erst ab der Toleranz`() {
|
fun `spult erst ab der Toleranz`() {
|
||||||
val id = "aaaaaaaaaaa"
|
val id = "aaaaaaaaaaa"
|
||||||
assertTrue(JamSync.decide(want(30.0), local(id, 31.2, false)).isEmpty())
|
assertTrue(JamSync.decide(want(30.0), local(id, 30.05, false)).isEmpty())
|
||||||
assertEquals(listOf(Step.Seek(30.0)), JamSync.decide(want(30.0), local(id, 34.0, false)))
|
assertEquals(listOf(Step.Seek(30.0 + JamSync.SEEK_LEAD_S)), JamSync.decide(want(30.0), local(id, 34.0, false)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user