Skip to content

Commit

Permalink
feat: Update HLS time to 3 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
richiemcilroy committed Jul 31, 2024
1 parent 0cf13bd commit 6f85456
Showing 1 changed file with 19 additions and 27 deletions.
46 changes: 19 additions & 27 deletions apps/desktop/src-tauri/src/media/mod.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
use std::{
path::{Path, PathBuf},
path::{ Path, PathBuf },
process::Stdio,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
time::{Duration, Instant},
sync::{ atomic::{ AtomicBool, Ordering }, Arc },
time::{ Duration, Instant },
};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::process::{Child, ChildStdin, Command};
use tokio::io::{ AsyncBufReadExt, AsyncWriteExt, BufReader };
use tokio::process::{ Child, ChildStdin, Command };
use tokio::sync::Mutex;
use tokio::task::JoinHandle;
use tracing::Level;

use crate::{
app::config,
recording::RecordingOptions,
utils::{create_named_pipe, ffmpeg_path_as_str},
utils::{ create_named_pipe, ffmpeg_path_as_str },
};

mod audio;
Expand Down Expand Up @@ -83,7 +80,7 @@ impl MediaRecorder {
recording_dir: &Path,
custom_device: Option<&str>,
max_screen_width: usize,
max_screen_height: usize,
max_screen_height: usize
) -> Result<(), String> {
if !scap::has_permission() {
tracing::warn!("Screen capturing permission not granted. Requesting permission...");
Expand All @@ -107,7 +104,7 @@ impl MediaRecorder {
let mut video_capturer = VideoCapturer::new(
max_screen_width,
max_screen_height,
self.should_stop.clone(),
self.should_stop.clone()
);
let adjusted_width = video_capturer.frame_width;
let adjusted_height = video_capturer.frame_height;
Expand All @@ -116,7 +113,9 @@ impl MediaRecorder {
audio_capturer.log_info();

match audio_capturer.start(audio_start_time.clone()) {
Ok(_) => self.audio_enabled = true,
Ok(_) => {
self.audio_enabled = true;
}
Err(error) => tracing::error!(error),
}
}
Expand Down Expand Up @@ -189,7 +188,7 @@ impl MediaRecorder {

ffmpeg_command
.args(["-f", "hls"])
.args(["-hls_time", "5", "-hls_playlist_type", "vod"])
.args(["-hls_time", "3", "-hls_playlist_type", "vod"])
.args(["-hls_flags", "independent_segments"])
.args(["-master_pl_name", "master.m3u8"])
.args(["-hls_segment_type", "mpegts"])
Expand All @@ -200,19 +199,13 @@ impl MediaRecorder {
.args(["-pix_fmt", "yuv420p", "-tune", "zerolatency"])
.args(["-vsync", "1", "-force_key_frames", "expr:gte(t,n_forced*3)"])
.args(["-movflags", "frag_keyframe+empty_moov"])
.args([
"-vf",
&format!("fps={fps},scale=in_range=full:out_range=limited"),
]);
.args(["-vf", &format!("fps={fps},scale=in_range=full:out_range=limited")]);

if self.audio_enabled {
ffmpeg_command
// audio
.args(["-codec:a", "aac", "-b:a", "128k", "-async", "1"])
.args([
"-af",
"aresample=async=1:min_hard_comp=0.100000:first_pts=0",
]);
.args(["-af", "aresample=async=1:min_hard_comp=0.100000:first_pts=0"]);
} else {
ffmpeg_command.args(["-an"]);
}
Expand All @@ -222,8 +215,7 @@ impl MediaRecorder {
tracing::trace!("Starting FFmpeg process...");

let (ffmpeg_child, ffmpeg_stdin) = self
.start_ffmpeg_process(ffmpeg_command)
.await
.start_ffmpeg_process(ffmpeg_command).await
.map_err(|e| e.to_string())?;
tracing::trace!("Ffmpeg process started");

Expand Down Expand Up @@ -298,7 +290,7 @@ impl MediaRecorder {

async fn start_ffmpeg_process(
&self,
cmd: Command,
cmd: Command
) -> Result<(Child, ChildStdin), std::io::Error> {
let mut video_process = start_recording_process(cmd).await.map_err(|e| {
tracing::error!("Failed to start video recording process: {}", e);
Expand All @@ -325,7 +317,7 @@ pub fn enumerate_audio_devices() -> Vec<String> {

#[tracing::instrument]
async fn start_recording_process(
mut cmd: Command,
mut cmd: Command
) -> Result<tokio::process::Child, std::io::Error> {
let mut process = cmd.stdin(Stdio::piped()).stderr(Stdio::piped()).spawn()?;

Expand All @@ -345,7 +337,7 @@ async fn start_recording_process(
#[tracing::instrument]
async fn wait_for_start_times(
audio_start_time: &Mutex<Option<Instant>>,
video_start_time: &Mutex<Option<Instant>>,
video_start_time: &Mutex<Option<Instant>>
) -> (Instant, Instant) {
loop {
let audio_start_locked = audio_start_time.lock().await;
Expand All @@ -370,7 +362,7 @@ pub enum TimeOffsetTarget {
#[tracing::instrument]
async fn create_time_offset_args(
audio_start_time: &Mutex<Option<Instant>>,
video_start_time: &Mutex<Option<Instant>>,
video_start_time: &Mutex<Option<Instant>>
) -> Option<(TimeOffsetTarget, Vec<String>)> {
let (audio_start, video_start) = wait_for_start_times(audio_start_time, video_start_time).await;
let duration_difference = if audio_start > video_start {
Expand Down

1 comment on commit 6f85456

@vercel
Copy link

@vercel vercel bot commented on 6f85456 Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.