import React, { useState, useEffect, useRef } from "react"; import Hls from "hls.js"; const API_URL = "http://localhost:8081/start"; // Replace with your actual API endpoint const RtspStream: React.FC = () => { const [rtspUrl, setRtspUrl] = useState(""); const [cameraName, setCameraName] = useState(""); const [m3u8Url, setM3u8Url] = useState(null); const [loading, setLoading] = useState(false); // Loading state const videoRef = useRef(null); useEffect(() => { if (m3u8Url && videoRef.current) { if (Hls.isSupported()) { const hls = new Hls(); hls.loadSource(m3u8Url); hls.attachMedia(videoRef.current); } else if ( videoRef.current.canPlayType("application/vnd.apple.mpegurl") ) { videoRef.current.src = m3u8Url; } } }, [m3u8Url]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); // Set loading to true when submitting try { const response = await fetch(API_URL, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ uri: rtspUrl, alias: cameraName, }), }); if (!response.ok) { throw new Error("Failed to fetch stream URL"); } const data = await response.json(); console.log("Stream data:", data); setM3u8Url(`http://localhost:8081${data?.uri}`); } catch (error) { console.error("Error fetching stream:", error); alert("Failed to load stream."); } finally { setLoading(false); // Reset loading state after API response } }; return (

RTSP Stream

setRtspUrl(e.target.value)} placeholder="Enter RTSP URL" style={{ width: "80%", padding: "8px", marginBottom: "10px" }} required />
setCameraName(e.target.value)} placeholder="Enter Camera Name" style={{ width: "80%", padding: "8px", marginBottom: "10px" }} required />
{loading && (

Stream is starting...

)} {m3u8Url && !loading && (
); }; export default RtspStream;