restructure and add a multicast_frontend
This commit is contained in:
52
src/auracast/server/multicast_frontend.py
Normal file
52
src/auracast/server/multicast_frontend.py
Normal file
@@ -0,0 +1,52 @@
|
||||
# frontend/app.py
|
||||
import streamlit as st
|
||||
import requests
|
||||
|
||||
# Global: desired packetization time in ms for Opus (should match backend)
|
||||
PTIME = 40
|
||||
BACKEND_URL = "http://localhost:5000"
|
||||
|
||||
|
||||
st.title("🎙️ WebRTC mic → backend demo")
|
||||
st.markdown("Click start and speak; watch your backend logs to see incoming RTP.")
|
||||
|
||||
component = f"""
|
||||
<button id='go'>Start microphone</button>
|
||||
<script>
|
||||
const go = document.getElementById('go');
|
||||
go.onclick = async () => {{
|
||||
go.disabled = true;
|
||||
const pc = new RTCPeerConnection(); // No STUN needed for localhost
|
||||
const stream = await navigator.mediaDevices.getUserMedia({{audio:true}});
|
||||
stream.getTracks().forEach(t => pc.addTrack(t, stream));
|
||||
|
||||
// --- WebRTC offer/answer exchange ---
|
||||
const offer = await pc.createOffer()
|
||||
// Patch SDP offer to include a=ptime using global PTIME
|
||||
let sdp = offer.sdp;
|
||||
const ptime_line = 'a=ptime:{PTIME}';
|
||||
const maxptime_line = 'a=maxptime:{PTIME}';
|
||||
if (sdp.includes('a=sendrecv')) {{
|
||||
sdp = sdp.replace('a=sendrecv', 'a=sendrecv\\n' + ptime_line + '\\n' + maxptime_line);
|
||||
}} else {{
|
||||
sdp += '\\n' + ptime_line + '\\n' + maxptime_line;
|
||||
}}
|
||||
const patched_offer = new RTCSessionDescription({{sdp, type: offer.type}})
|
||||
await pc.setLocalDescription(patched_offer)
|
||||
|
||||
// Send offer to backend
|
||||
const response = await fetch(
|
||||
"{BACKEND_URL}/offer",
|
||||
{{
|
||||
method: 'POST',
|
||||
headers: {{'Content-Type':'application/json'}},
|
||||
body: JSON.stringify({{sdp: pc.localDescription.sdp, type: pc.localDescription.type}})
|
||||
}}
|
||||
)
|
||||
const answer = await response.json()
|
||||
await pc.setRemoteDescription(new RTCSessionDescription({{sdp: answer.sdp, type: answer.type}}))
|
||||
}};
|
||||
</script>
|
||||
"""
|
||||
st.components.v1.html(component, height=80)
|
||||
|
||||
Reference in New Issue
Block a user