From c7b2b5ac2b74684e0e6816a9e72091f630bf2128 Mon Sep 17 00:00:00 2001 From: Rishi Balakrishnan Date: Mon, 5 Oct 2026 21:15:16 -0400 Subject: [PATCH] fix(live): show the offline state when a stream is not live (#209) The native player only re-checked its 10s offline threshold on a new error event, and retries with the same source can produce none, so the overlay never appeared. Use a timer like the web player, and show the offline state immediately when the player is failing and the websocket already says the stream is not live (ended, or its last segment is stale). Date segments by their startTime so the stale segment in the connect burst no longer reads as live. --- src/features/streamplace/LivePlayer.tsx | 31 +++++++++++------ src/features/streamplace/LivePlayer.web.tsx | 20 +++++++++-- .../streamplace/__tests__/live-state.test.ts | 33 +++++++++++++++++++ src/features/streamplace/live-state.ts | 18 ++++++++-- .../streamplace/useStreamplaceLive.ts | 7 +++- src/screens/StreamplaceWatch/index.tsx | 2 +- 6 files changed, 94 insertions(+), 17 deletions(-) diff --git a/src/features/streamplace/LivePlayer.tsx b/src/features/streamplace/LivePlayer.tsx index 523ba0a83..1926ac526 100644 --- a/src/features/streamplace/LivePlayer.tsx +++ b/src/features/streamplace/LivePlayer.tsx @@ -1,4 +1,4 @@ -import {useEffect, useRef, useState} from 'react' +import {useEffect, useState} from 'react' import {View} from 'react-native' import {useVideoPlayer, VideoView} from 'expo-video' import {useIsFocused} from '@react-navigation/native' @@ -10,16 +10,21 @@ import {livePlaylistUrl} from './url' const RETRY_MS = 2000 const OFFLINE_AFTER_MS = 10_000 -export function LivePlayer({actor}: {actor: string}) { +export function LivePlayer({ + actor, + notLive = false, +}: { + actor: string + notLive?: boolean +}) { const source = livePlaylistUrl(actor) const player = useVideoPlayer(source, p => { p.play() }) - const failingSince = useRef(null) - const [offline, setOffline] = useState(false) + const [failing, setFailing] = useState(false) + const [timedOut, setTimedOut] = useState(false) const reload = () => { - failingSince.current = null player.replace(source) player.play() } @@ -35,15 +40,20 @@ export function LivePlayer({actor}: {actor: string}) { useEffect(() => { let retry: ReturnType | undefined + let offlineTimer: ReturnType | undefined const sub = player.addListener('statusChange', ({status}) => { if (status === 'readyToPlay') { - failingSince.current = null - setOffline(false) + clearTimeout(offlineTimer) + offlineTimer = undefined + setFailing(false) + setTimedOut(false) return } if (status !== 'error') return - if (failingSince.current === null) failingSince.current = Date.now() - setOffline(Date.now() - failingSince.current > OFFLINE_AFTER_MS) + setFailing(true) + if (!offlineTimer) { + offlineTimer = setTimeout(() => setTimedOut(true), OFFLINE_AFTER_MS) + } clearTimeout(retry) retry = setTimeout(() => { player.replace(source) @@ -53,6 +63,7 @@ export function LivePlayer({actor}: {actor: string}) { return () => { sub.remove() clearTimeout(retry) + clearTimeout(offlineTimer) } }, [player, source]) @@ -66,7 +77,7 @@ export function LivePlayer({actor}: {actor: string}) { contentFit="contain" accessibilityIgnoresInvertColors /> - {offline && } + {failing && (notLive || timedOut) && } ) } diff --git a/src/features/streamplace/LivePlayer.web.tsx b/src/features/streamplace/LivePlayer.web.tsx index 05fe05ec5..23caf007f 100644 --- a/src/features/streamplace/LivePlayer.web.tsx +++ b/src/features/streamplace/LivePlayer.web.tsx @@ -10,9 +10,16 @@ const RETRY_MS = 2000 const OFFLINE_AFTER_MS = 10_000 const MAX_MEDIA_RECOVERIES = 2 -export function LivePlayer({actor}: {actor: string}) { +export function LivePlayer({ + actor, + notLive = false, +}: { + actor: string + notLive?: boolean +}) { const ref = useRef(null) const [offline, setOffline] = useState(false) + const [failing, setFailing] = useState(false) const [attempt, setAttempt] = useState(0) useFocusEffect( @@ -35,6 +42,7 @@ export function LivePlayer({actor}: {actor: string}) { setOffline(false) const onFailure = (reload: () => void) => { + setFailing(true) if (!offlineTimer) { offlineTimer = setTimeout(() => setOffline(true), OFFLINE_AFTER_MS) } @@ -48,6 +56,7 @@ export function LivePlayer({actor}: {actor: string}) { clearTimeout(offlineTimer) offlineTimer = undefined setOffline(false) + setFailing(false) } video.addEventListener('playing', onPlaying) @@ -87,7 +96,10 @@ export function LivePlayer({actor}: {actor: string}) { } }) .catch(() => { - if (!disposed) setOffline(true) + if (!disposed) { + setFailing(true) + setOffline(true) + } }) return () => { @@ -112,7 +124,9 @@ export function LivePlayer({actor}: {actor: string}) { playsInline style={{width: '100%', height: '100%', backgroundColor: 'black'}} /> - {offline && setAttempt(n => n + 1)} />} + {failing && (notLive || offline) && ( + setAttempt(n => n + 1)} /> + )} ) } diff --git a/src/features/streamplace/__tests__/live-state.test.ts b/src/features/streamplace/__tests__/live-state.test.ts index a239a8f02..3e7ef21bd 100644 --- a/src/features/streamplace/__tests__/live-state.test.ts +++ b/src/features/streamplace/__tests__/live-state.test.ts @@ -1,5 +1,6 @@ import { EMPTY_LIVE_STATE, + isKnownNotLiveAt, isLiveAt, type LiveState, reduceLiveEvent, @@ -115,3 +116,35 @@ it('returns the same state for unknown or malformed events', () => { ).toBe(EMPTY_LIVE_STATE) expect(reduceLiveEvent(EMPTY_LIVE_STATE, 'garbage', 0)).toBe(EMPTY_LIVE_STATE) }) + +it('dates a segment by its startTime so an old one from the burst is not live', () => { + const now = Date.parse('2026-10-05T12:00:00Z') + const s = apply( + [{$type: 'place.stream.segment', startTime: '2026-10-05T09:00:00Z'}], + now, + ) + expect(isLiveAt(s, now)).toBe(false) + expect(isKnownNotLiveAt(s, now)).toBe(true) +}) + +it('treats a fresh segment as live and never dates one in the future', () => { + const now = Date.parse('2026-10-05T12:00:00Z') + const s = apply( + [{$type: 'place.stream.segment', startTime: '2026-10-05T12:00:30Z'}], + now, + ) + expect(s.lastSegmentAt).toBe(now) + expect(isLiveAt(s, now + 5000)).toBe(true) + expect(isKnownNotLiveAt(s, now + 5000)).toBe(false) +}) + +it('knows an ended stream is not live, and knows nothing before any events', () => { + const ended = apply([ + { + $type: 'place.stream.livestream#livestreamView', + record: {title: 'x', endedAt: '2026-04-10T20:38:30Z'}, + }, + ]) + expect(isKnownNotLiveAt(ended, 1000)).toBe(true) + expect(isKnownNotLiveAt(EMPTY_LIVE_STATE, 1000)).toBe(false) +}) diff --git a/src/features/streamplace/live-state.ts b/src/features/streamplace/live-state.ts index f370a95fa..57db8965f 100644 --- a/src/features/streamplace/live-state.ts +++ b/src/features/streamplace/live-state.ts @@ -74,8 +74,14 @@ export function reduceLiveEvent( return typeof e.count === 'number' ? {...state, viewerCount: e.count} : state - case 'place.stream.segment': - return {...state, lastSegmentAt: now} + case 'place.stream.segment': { + const started = + typeof e.startTime === 'string' ? Date.parse(e.startTime) : NaN + return { + ...state, + lastSegmentAt: Number.isFinite(started) ? Math.min(started, now) : now, + } + } default: return state } @@ -89,6 +95,14 @@ export function isLiveAt(state: LiveState, now: number) { ) } +export function isKnownNotLiveAt(state: LiveState, now: number) { + return ( + !!state.endedAt || + (state.lastSegmentAt !== undefined && + now - state.lastSegmentAt >= SEGMENT_STALE_MS) + ) +} + function onMessage(state: LiveState, e: Raw): LiveState { if (typeof e.uri !== 'string') return state if (e.deleted === true) { diff --git a/src/features/streamplace/useStreamplaceLive.ts b/src/features/streamplace/useStreamplaceLive.ts index 3743e9f54..f01c9256f 100644 --- a/src/features/streamplace/useStreamplaceLive.ts +++ b/src/features/streamplace/useStreamplaceLive.ts @@ -3,6 +3,7 @@ import {useEffect, useReducer, useState} from 'react' import {logger} from '#/logger' import { EMPTY_LIVE_STATE, + isKnownNotLiveAt, isLiveAt, type LiveState, reduceLiveEvent, @@ -56,5 +57,9 @@ export function useStreamplaceLive(actor: string | undefined) { return () => clearInterval(id) }, []) - return {...state, isLive: isLiveAt(state, now)} + return { + ...state, + isLive: isLiveAt(state, now), + notLive: isKnownNotLiveAt(state, now), + } } diff --git a/src/screens/StreamplaceWatch/index.tsx b/src/screens/StreamplaceWatch/index.tsx index 64c9abddc..f94111119 100644 --- a/src/screens/StreamplaceWatch/index.tsx +++ b/src/screens/StreamplaceWatch/index.tsx @@ -180,7 +180,7 @@ function Watch({actor}: {actor: string}) { IS_NATIVE && {paddingBottom: typing ? keyboardHeight : bottom}, ]}> {did ? ( - + ) : (