Skip to content

Commit

Permalink
audio #388
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlt8 committed Oct 8, 2023
1 parent 77f8139 commit 5e5719e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
23 changes: 10 additions & 13 deletions app/wyzebridge/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ def get_ffmpeg_cmd(
- list of str: complete ffmpeg command that is ready to run as subprocess.
"""

flags = "-fflags +flush_packets+nobuffer -flags +low_delay+global_header"
flags = "-fflags +flush_packets+nobuffer+genpts -flags +low_delay+global_header"
livestream = get_livestream_cmd(uri)
audio_in = "-f lavfi -i anullsrc=cl=mono" if livestream else ""
audio_out = "aac"
thread_queue = "-thread_queue_size 1k -analyzeduration 32 -probesize 32"
thread_queue = "-thread_queue_size 64 -analyzeduration 32 -probesize 32"
if audio and "codec" in audio:
audio_in += f"{thread_queue} -f {audio['codec']} -ac 1 -ar {audio['rate']} -i /tmp/{uri}_audio.pipe"
audio_in = f"{thread_queue} -f {audio['codec']} -ac 1 -ar {audio['rate']} -i /tmp/{uri}_audio.pipe"
audio_out = audio["codec_out"] or "copy"
a_filter = env_bool("AUDIO_FILTER", "volume=5")
a_filter = env_bool("AUDIO_FILTER", "volume=5") + ",adelay=0|0"
a_options = ["-compression_level", "4", "-filter:a", a_filter]
rtsp_transport = "udp" if "udp" in env_bool("MTX_PROTOCOLS") else "tcp"
fio_cmd = "use_fifo=1:fifo_options=queue_size=250\\\:attempt_recovery=1\\\:drop_pkts_on_overflow=1"
rss_cmd = f"[{fio_cmd}:{{}}f=rtsp:{rtsp_transport=:}:bsfs/v=dump_extra=freq=k]rtsp://0.0.0.0:8554/{uri}"
fio_cmd = "use_fifo=1:fifo_options=attempt_recovery=1\\\:drop_pkts_on_overflow=1:"
rss_cmd = f"[{fio_cmd}{{}}f=rtsp:{rtsp_transport=:}:bsfs=dump_extra=freq=e]rtsp://0.0.0.0:8554/{uri}"
rtsp_ss = rss_cmd.format("")
if env_cam("AUDIO_STREAM", uri, style="original") and audio:
rtsp_ss += "|" + rss_cmd.format("select=a:") + "_audio"
Expand All @@ -56,15 +56,12 @@ def get_ffmpeg_cmd(
+ (["-hwaccel", h264_enc] if h264_enc in {"vaapi", "qsv"} else [])
+ ["-f", vcodec, "-i", "pipe:0"]
+ audio_in.split()
+ ["-c:v"]
+ ["-map", "0:v", "-c:v"]
+ re_encode_video(uri, is_vertical)
+ (["-c:a", audio_out] if audio_in else [])
+ (["-map", "1:a", "-c:a", audio_out] if audio_in else [])
+ (a_options if audio and audio_out != "copy" else [])
+ ["-fps_mode", "passthrough", "-flush_packets", "1"]
+ ["-copyts", "-copytb", "0", "-async", "1"]
+ ["-bsf:a", "setts='TS-STARTPTS'"]
+ ["-map", "0:v"]
+ (["-map", "1:a"] if audio_in else [])
+ ["-fps_mode", "drop", "-async", "0", "-flush_packets", "1"]
+ ["-muxdelay", "0", "-copytb", "1"]
+ ["-f", "tee"]
+ [rtsp_ss + get_record_cmd(uri, audio_out, record) + livestream]
)
Expand Down
6 changes: 6 additions & 0 deletions app/wyzecam/iotc.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ def recv_bridge_frame(self, timeout: int = 15, fps: int = 15) -> Iterator[bytes]
warnings.warn("Still waiting for first frame. Updating frame size.")
last["key_time"] = last["time"] = time.time()
self.update_frame_size_rate()
yield b""
continue
self.state = WyzeIOTCSessionState.CONNECTING_FAILED
raise Exception(f"Stream did not receive a frame for over {timeout}s")
Expand All @@ -500,26 +501,31 @@ def recv_bridge_frame(self, timeout: int = 15, fps: int = 15) -> Iterator[bytes]
if errno < 0:
time.sleep(sleep_interval)
if errno == tutk.AV_ER_DATA_NOREADY:
yield b""
continue
if errno in (
tutk.AV_ER_INCOMPLETE_FRAME,
tutk.AV_ER_LOSED_THIS_FRAME,
):
warnings.warn(str(tutk.TutkError(errno).name))
yield b""
continue
raise tutk.TutkError(errno)
if not frame_data:
yield b""
continue
assert frame_info is not None, "Got no frame info without an error!"
if frame_info.frame_size not in ignore_res:
if last["key_frame"] == 0:
warnings.warn(
f"Skipping smaller frame at start of stream (frame_size={frame_info.frame_size})"
)
yield b""
continue
warnings.warn(f"Wrong resolution (frame_size={frame_info.frame_size})")
self.update_frame_size_rate()
last |= {"key_frame": 0, "key_time": 0, "time": time.time()}
yield b""
continue
if frame_info.is_keyframe:
last |= {"key_frame": frame_info.frame_no, "key_time": time.time()}
Expand Down

0 comments on commit 5e5719e

Please sign in to comment.