RelayrtcDocs
Get started
Documentation
React SDK

useDvrPlayback

Reusable seek, behind-live and return-live state for a media element.

React
import { useEffect, useRef } from 'react'
import { useDvrPlayback } from '@relayrtc/react'
 
export function DvrControls({ attach }: {
  attach: (element: HTMLVideoElement) => () => void
}) {
  const media = useRef<HTMLVideoElement>(null)
  const dvr = useDvrPlayback(media, true)
  useEffect(() => {
    if (media.current) return attach(media.current)
  }, [attach])
  return <section>
    <video ref={media} controls playsInline />
    <p role="status">{dvr.available ? (dvr.isLive ? 'Live' : 'Rewound') : 'Waiting for media'}</p>
    <button disabled={!dvr.available} onClick={() => dvr.rewind(30)}>Rewind</button>
    <button disabled={!dvr.available} onClick={() => dvr.goLive()}>Go live</button>
  </section>
}

The React SDK exposes useDvrPlayback(mediaRef, enabled, optionalOnExpired). It observes the browser's actual seekable ranges and returns start, end, position, behindLiveSeconds, isLive, paused, available, recoveredExpiredSeek, seek(seconds), rewind(seconds=30) and goLive(). Positions are browser media times, not REST run-buffer seconds. Gaps/out-of-window requests clamp to a safe available position; the optional expiry callback can refresh application state. Go live moves near the seekable edge; applications choose whether to resume a user pause.

The hook owns event listeners and a bounded one-second clock, and removes both on cleanup. It does not fetch grants, create a player or authorize viewers. The browser SDK exports the same DvrPlaybackController for applications without React. BroadcastPlayer integrates these controls with grant renewal and HLS recovery in mode=dvr. Live DVR explains expired windows and retention.