Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ paths:
schema:
$ref: "#/components/schemas/Health"

/time:
get:
operationId: getServerTime
summary: The server's clock
description: |
For clients to measure how far their clock is from the server's: a
few requests, keeping the quickest round trip. Playback positions
are stamped with server time, so every screen in a room keeps the
same beat only if they agree on it.
tags: [system]
security: []
responses:
"200":
description: The time as the server answered
content:
application/json:
schema:
$ref: "#/components/schemas/ServerTime"

# --- Signup and sign-in ---------------------------------------------------

/invites/{code}:
Expand Down Expand Up @@ -5108,6 +5127,15 @@ components:
type: string
example: 0.1.0

ServerTime:
type: object
required: [ms]
properties:
ms:
type: integer
format: int64
description: The server's clock, in ms since the epoch.

Error:
type: object
required: [code, message]
Expand Down Expand Up @@ -8900,6 +8928,14 @@ components:
type: integer
format: int64
minimum: 0
at:
type: integer
format: int64
description: |
When `positionMs` was read, in ms since the epoch on the server's
clock (see `/time`). The server moves the position on by the time
the report took to arrive; without it, the report counts as read
on arrival.
error:
type: string
maxLength: 500
Expand Down
4 changes: 3 additions & 1 deletion server/internal/httpapi/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ type env struct {

func newEnv(t *testing.T) *env {
t.Helper()
e := &env{t: t, now: time.Date(2026, 10, 5, 12, 0, 0, 0, time.UTC)}
// The fake clock starts at the real time: the client's cookie jar runs
// on the real clock, and drops a session cookie that expired by it.
e := &env{t: t, now: time.Now().UTC().Truncate(time.Hour)}
var h http.Handler
e.srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { h.ServeHTTP(w, r) }))
t.Cleanup(e.srv.Close)
Expand Down
77 changes: 77 additions & 0 deletions server/internal/httpapi/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions server/internal/httpapi/playback.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ func (s *Server) ReportPlayback(ctx context.Context, req ReportPlaybackRequestOb
if req.Body.Error != nil {
rep.Error = *req.Body.Error
}
if req.Body.At != nil {
rep.At = time.UnixMilli(*req.Body.At)
}
np, err := s.Playback.Report(ctx, req.RoomId, userID, rep)
if err != nil {
return nil, err
Expand Down
6 changes: 6 additions & 0 deletions server/internal/httpapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,19 @@ func (s *Server) GetHealth(context.Context, GetHealthRequestObject) (GetHealthRe
return GetHealth200JSONResponse{Status: HealthStatusOk, Version: s.Version}, nil
}

// GetServerTime tells a client the server's clock, to set its own by.
func (s *Server) GetServerTime(context.Context, GetServerTimeRequestObject) (GetServerTimeResponseObject, error) {
return GetServerTime200JSONResponse{Ms: time.Now().UnixMilli()}, nil
}

// --- Request context ----------------------------------------------------------

// publicOps can be called without a session. Everything else requires one.
// Keys are operation IDs as the generated code spells them (capitalized).
// TestPublicOpsMatchSpec keeps this in sync with `security: []` in the spec.
var publicOps = map[string]bool{
"GetHealth": true,
"GetServerTime": true,
"GetInvite": true,
"Signup": true,
"BeginPasskeySignup": true,
Expand Down
12 changes: 12 additions & 0 deletions server/internal/playback/playback.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ const (
StatePaused = "paused"
)

// maxReportAge bounds how far a speaker's report is moved on for its trip.
const maxReportAge = 2 * time.Second

// Sessions opens provider sessions for links. It's links.Service in
// production. Songs play through the link of whoever queued them.
type Sessions interface {
Expand Down Expand Up @@ -985,6 +988,9 @@ type Report struct {
ItemID string
Event string
Position time.Duration
// At is when Position was read, on the server's clock; zero if the
// speaker didn't say. The position is moved on by the report's trip.
At time.Time
// Error describes an EventError, for the notice.
Error string
}
Expand All @@ -1004,6 +1010,12 @@ func (e *Engine) Report(ctx context.Context, roomID, userID string, rep Report)
}
now := e.cfg.Now()
p.LastSeen = now
// A report takes a moment to arrive, longer when the one before it is
// slow: left as it is, everyone's beat would lag the speaker's by that.
// Bounded, in case the speaker's clock is off after all.
if !rep.At.IsZero() && rep.Event != EventPaused {
rep.Position += min(max(now.Sub(rep.At), 0), maxReportAge)
}
stale := r.np.Item == nil || r.np.Item.ID != rep.ItemID || r.np.Driver != string(provider.PlaybackStream)
// Progress is every few seconds; only a stale one says something's off
// (the speaker is on another song than the room).
Expand Down
21 changes: 21 additions & 0 deletions server/internal/playback/playback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,27 @@ func TestStreamPlayback(t *testing.T) {
if np = e.report(e.alice, "phone", playback.EventProgress, 3500*time.Millisecond); np.Position != 3500*time.Millisecond {
t.Errorf("after progress report: %v", np.Position)
}
// A report read 80 ms before it arrived counts from when it was read…
read := func(pos, ago time.Duration) rooms.NowPlaying {
t.Helper()
np, err := e.p.Report(ctx, e.room.ID, e.alice.ID, playback.Report{
DeviceID: "phone", ItemID: e.np().Item.ID, Event: playback.EventProgress, Position: pos, At: e.clock().Add(-ago),
})
if err != nil {
t.Fatal(err)
}
return np
}
if np = read(3500*time.Millisecond, 80*time.Millisecond); np.Position != 3580*time.Millisecond {
t.Errorf("report read 80ms ago: %v", np.Position)
}
// …but a speaker clock that's way off moves it by 2 s at most, and never back.
if np = read(3500*time.Millisecond, time.Minute); np.Position != 5500*time.Millisecond {
t.Errorf("report read a minute ago: %v", np.Position)
}
if np = read(3500*time.Millisecond, -time.Second); np.Position != 3500*time.Millisecond {
t.Errorf("report from the future: %v", np.Position)
}

// Pause, seek and play each bump the revision for the speaker.
if np, _ = e.command(e.bob, playback.Command{Action: playback.ActionPause}); np.State != playback.StatePaused || np.Revision != rev+1 {
Expand Down
58 changes: 58 additions & 0 deletions web/src/api/schema.gen.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion web/src/components/shell/beat-lab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ function Beat() {
setTaps(tap() ?? 0)
}}
>
Tap on the beat {taps > 0 && taps < 3 ? `(${taps})` : ''}
Tap on the beat {taps > 0 && taps < 4 ? `(${taps})` : ''}
</Button>
<p className="text-caption text-muted-foreground">
Start on a &ldquo;one&rdquo; and tap at least 4 times. Fixes this song on this device.
Expand Down
Loading
Loading