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 ? ( - + ) : (