File size: 1,166 Bytes
df80275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import asyncio
import logging
from typing import List

import av
import streamlit as st
from streamlit_webrtc import WebRtcMode, webrtc_streamer

logger = logging.getLogger(__name__)


delay = st.slider("Delay", 0.0, 5.0, 1.0, 0.05)


async def queued_video_frames_callback(
    frames: List[av.VideoFrame],
) -> List[av.VideoFrame]:
    logger.debug("Delay: %f", delay)
    # A standalone `await ...` is interpreted as an expression and
    # the Streamlit magic's target, which leads implicit calls of `st.write`.
    # To prevent it, fix it as `_ = await ...`, a statement.
    # See https://discuss.streamlit.io/t/issue-with-asyncio-run-in-streamlit/7745/15
    await asyncio.sleep(delay)
    return frames


async def queued_audio_frames_callback(
    frames: List[av.AudioFrame],
) -> List[av.AudioFrame]:
    await asyncio.sleep(delay)
    return frames


webrtc_streamer(
    key="delay",
    mode=WebRtcMode.SENDRECV,
    rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]},
    queued_video_frames_callback=queued_video_frames_callback,
    queued_audio_frames_callback=queued_audio_frames_callback,
    async_processing=True,
)