|
import time |
|
import cv2 |
|
import numpy as np |
|
import streamlit as st |
|
from streamlit_webrtc import webrtc_streamer, WebRtcMode, VideoTransformerBase |
|
|
|
|
|
st.set_page_config(page_title="Live Stream Broadcast", layout="wide") |
|
|
|
|
|
|
|
st.markdown( |
|
""" |
|
<style> |
|
video::-webkit-media-controls-timeline { |
|
display: none !important; |
|
} |
|
video::-webkit-media-controls-current-time-display { |
|
display: none !important; |
|
} |
|
video::-webkit-media-controls-time-remaining-display { |
|
display: none !important; |
|
} |
|
</style> |
|
""", |
|
unsafe_allow_html=True, |
|
) |
|
|
|
st.title("Live Streaming Space") |
|
|
|
|
|
|
|
|
|
class BufferingTransformer(VideoTransformerBase): |
|
def __init__(self): |
|
self.buffer = [] |
|
self.buffer_duration = 3.0 |
|
self.last_flush_time = time.time() |
|
|
|
def transform(self, frame): |
|
|
|
img = frame.to_ndarray(format="bgr24") |
|
self.buffer.append(img) |
|
now = time.time() |
|
if now - self.last_flush_time >= self.buffer_duration: |
|
|
|
|
|
self.buffer = [] |
|
self.last_flush_time = now |
|
|
|
return img |
|
|
|
|
|
|
|
|
|
password = st.sidebar.text_input("Enter broadcasting password", type="password") |
|
if password == "test123": |
|
st.sidebar.success("Authenticated for broadcasting!") |
|
broadcast_mode = True |
|
else: |
|
broadcast_mode = False |
|
|
|
|
|
if broadcast_mode: |
|
st.sidebar.header("Broadcast Settings") |
|
|
|
|
|
camera_options = ["Camera 0", "Camera 1", "Camera 2"] |
|
camera_choice = st.sidebar.selectbox("Select Camera", camera_options) |
|
camera_index = int(camera_choice.split(" ")[-1]) |
|
|
|
st.write("### You are in **BROADCASTING** mode") |
|
st.write("Your live stream is being sent to all viewers.") |
|
|
|
|
|
|
|
webrtc_ctx = webrtc_streamer( |
|
key="live_stream", |
|
mode=WebRtcMode.SENDONLY, |
|
video_device_index=camera_index, |
|
media_stream_constraints={"video": True, "audio": False}, |
|
video_transformer_factory=BufferingTransformer, |
|
video_html_attrs={ |
|
"controls": True, |
|
"style": { |
|
"width": "100%", |
|
"border": "2px solid #ccc", |
|
"border-radius": "10px", |
|
}, |
|
}, |
|
rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]}, |
|
) |
|
else: |
|
st.write("### Viewing Broadcast") |
|
st.info("If you have the broadcasting password, enter it in the sidebar to broadcast your own stream.") |
|
|
|
webrtc_ctx = webrtc_streamer( |
|
key="live_stream", |
|
mode=WebRtcMode.RECVONLY, |
|
media_stream_constraints={"video": True, "audio": False}, |
|
video_transformer_factory=BufferingTransformer, |
|
video_html_attrs={ |
|
"controls": True, |
|
"style": { |
|
"width": "100%", |
|
"border": "2px solid #ccc", |
|
"border-radius": "10px", |
|
}, |
|
}, |
|
rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]}, |
|
) |
|
|