Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from streamlit_webrtc import webrtc_streamer, WebRtcMode, VideoTransformerBase
|
| 6 |
+
|
| 7 |
+
# Set wide layout and page title
|
| 8 |
+
st.set_page_config(page_title="Live Stream Broadcast", layout="wide")
|
| 9 |
+
|
| 10 |
+
# -----------------------------------------------------------------------------
|
| 11 |
+
# Custom CSS: Hide the timeline parts of the video controls (works in WebKit‑based browsers)
|
| 12 |
+
st.markdown(
|
| 13 |
+
"""
|
| 14 |
+
<style>
|
| 15 |
+
video::-webkit-media-controls-timeline {
|
| 16 |
+
display: none !important;
|
| 17 |
+
}
|
| 18 |
+
video::-webkit-media-controls-current-time-display {
|
| 19 |
+
display: none !important;
|
| 20 |
+
}
|
| 21 |
+
video::-webkit-media-controls-time-remaining-display {
|
| 22 |
+
display: none !important;
|
| 23 |
+
}
|
| 24 |
+
</style>
|
| 25 |
+
""",
|
| 26 |
+
unsafe_allow_html=True,
|
| 27 |
+
)
|
| 28 |
+
# -----------------------------------------------------------------------------
|
| 29 |
+
st.title("Live Streaming Space")
|
| 30 |
+
|
| 31 |
+
# -----------------------------------------------------------------------------
|
| 32 |
+
# A simple video transformer that “buffers” frames for 3 seconds.
|
| 33 |
+
# (In a more complex app you might encode these frames into a video segment file.)
|
| 34 |
+
class BufferingTransformer(VideoTransformerBase):
|
| 35 |
+
def __init__(self):
|
| 36 |
+
self.buffer = [] # List to hold frames
|
| 37 |
+
self.buffer_duration = 3.0 # Buffer duration in seconds
|
| 38 |
+
self.last_flush_time = time.time()
|
| 39 |
+
|
| 40 |
+
def transform(self, frame):
|
| 41 |
+
# Convert the frame (a VideoFrame object) into a NumPy array (BGR format)
|
| 42 |
+
img = frame.to_ndarray(format="bgr24")
|
| 43 |
+
self.buffer.append(img)
|
| 44 |
+
now = time.time()
|
| 45 |
+
if now - self.last_flush_time >= self.buffer_duration:
|
| 46 |
+
# Here you could (for example) encode/store the buffered frames.
|
| 47 |
+
# For this demo we just clear the buffer every 3 seconds.
|
| 48 |
+
self.buffer = []
|
| 49 |
+
self.last_flush_time = now
|
| 50 |
+
# Return the image unchanged for display
|
| 51 |
+
return img
|
| 52 |
+
|
| 53 |
+
# -----------------------------------------------------------------------------
|
| 54 |
+
# Sidebar: Password input for broadcasting. Only if you enter the correct password
|
| 55 |
+
# ("test123") do you get access to the broadcast controls.
|
| 56 |
+
password = st.sidebar.text_input("Enter broadcasting password", type="password")
|
| 57 |
+
if password == "test123":
|
| 58 |
+
st.sidebar.success("Authenticated for broadcasting!")
|
| 59 |
+
broadcast_mode = True
|
| 60 |
+
else:
|
| 61 |
+
broadcast_mode = False
|
| 62 |
+
|
| 63 |
+
# -----------------------------------------------------------------------------
|
| 64 |
+
if broadcast_mode:
|
| 65 |
+
st.sidebar.header("Broadcast Settings")
|
| 66 |
+
# (In a real app you might enumerate the actual connected cameras.
|
| 67 |
+
# Here we simply provide a dummy list of choices.)
|
| 68 |
+
camera_options = ["Camera 0", "Camera 1", "Camera 2"]
|
| 69 |
+
camera_choice = st.sidebar.selectbox("Select Camera", camera_options)
|
| 70 |
+
camera_index = int(camera_choice.split(" ")[-1])
|
| 71 |
+
|
| 72 |
+
st.write("### You are in **BROADCASTING** mode")
|
| 73 |
+
st.write("Your live stream is being sent to all viewers.")
|
| 74 |
+
|
| 75 |
+
# Start the WebRTC streamer in SENDONLY mode (broadcasting).
|
| 76 |
+
# The same key ("live_stream") is used so that viewers join the same “room.”
|
| 77 |
+
webrtc_ctx = webrtc_streamer(
|
| 78 |
+
key="live_stream",
|
| 79 |
+
mode=WebRtcMode.SENDONLY,
|
| 80 |
+
video_device_index=camera_index,
|
| 81 |
+
media_stream_constraints={"video": True, "audio": False},
|
| 82 |
+
video_transformer_factory=BufferingTransformer,
|
| 83 |
+
video_html_attrs={
|
| 84 |
+
"controls": True,
|
| 85 |
+
"style": {
|
| 86 |
+
"width": "100%",
|
| 87 |
+
"border": "2px solid #ccc",
|
| 88 |
+
"border-radius": "10px",
|
| 89 |
+
},
|
| 90 |
+
},
|
| 91 |
+
rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]},
|
| 92 |
+
)
|
| 93 |
+
else:
|
| 94 |
+
st.write("### Viewing Broadcast")
|
| 95 |
+
st.info("If you have the broadcasting password, enter it in the sidebar to broadcast your own stream.")
|
| 96 |
+
# In viewing mode, join the same “room” in RECVONLY mode so you can see the active stream.
|
| 97 |
+
webrtc_ctx = webrtc_streamer(
|
| 98 |
+
key="live_stream",
|
| 99 |
+
mode=WebRtcMode.RECVONLY,
|
| 100 |
+
media_stream_constraints={"video": True, "audio": False},
|
| 101 |
+
video_transformer_factory=BufferingTransformer,
|
| 102 |
+
video_html_attrs={
|
| 103 |
+
"controls": True,
|
| 104 |
+
"style": {
|
| 105 |
+
"width": "100%",
|
| 106 |
+
"border": "2px solid #ccc",
|
| 107 |
+
"border-radius": "10px",
|
| 108 |
+
},
|
| 109 |
+
},
|
| 110 |
+
rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]},
|
| 111 |
+
)
|