@supunlakmal/hooks
Version:
A collection of reusable React hooks
33 lines (32 loc) • 1.5 kB
TypeScript
interface UseMediaStreamOptions {
/** Media constraints to pass to getUserMedia (e.g., { video: true, audio: true }) */
constraints: MediaStreamConstraints;
/** Optional callback when the stream is successfully obtained */
onStream?: (stream: MediaStream) => void;
/** Optional callback when there's an error accessing the media devices */
onError?: (error: Error) => void;
/** If true, automatically stops the stream tracks when the component unmounts. Defaults to true. */
autoStop?: boolean;
}
interface UseMediaStreamReturn {
/** The active MediaStream object, or null if not yet obtained or error occurred. */
stream: MediaStream | null;
/** Indicates if the stream is currently active/being requested. */
isActive: boolean;
/** Function to explicitly start requesting the media stream. */
startStream: () => Promise<void>;
/** Function to explicitly stop the media stream tracks. */
stopStream: () => void;
/** Error object if getUserMedia failed, null otherwise. */
error: Error | null;
/** Indicates if the getUserMedia API is supported by the browser. */
isSupported: boolean;
}
/**
* Hook to manage access to user's media devices (camera, microphone) using getUserMedia.
*
* @param options Configuration options including media constraints.
* @returns State and controls for the media stream.
*/
export declare function useMediaStream(options: UseMediaStreamOptions): UseMediaStreamReturn;
export {};