Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into panick-once-saved
Browse files Browse the repository at this point in the history
  • Loading branch information
gferraro committed Feb 24, 2025
2 parents 7e46867 + 99ef009 commit eb9edf5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
33 changes: 16 additions & 17 deletions src/camera_transfer_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,22 @@ is 'dtoverlay=spi0-1cs,cs0_pin=8' set in your config.txt?"
return Err(());
}

// this seems to happen when save_audio fails...
let attempts = 5;
for i in 0..attempts{
let res = pin.set_interrupt(Trigger::RisingEdge, None);
match res{
Ok(())=> break,
Err(e)=>{
if i == attempts-1 {
error!("Unable to set pi ping interrupt: {e}");
return Err(());
}
info!("Pin interrupts failed,trying again {}",e);
sleep(Duration::from_millis(1));
}
}

}
// this seems to happen when save_audio fails...
let attempts = 100;
for i in 0..attempts{
let res = pin.set_interrupt(Trigger::RisingEdge, None);
match res{
Ok(())=> break,
Err(e)=>{
if i == attempts-1 {
error!("Unable to set pi ping interrupt: {e}");
return Err(());
}
info!("Pin interrupts failed,trying again {}",e);
sleep(Duration::from_millis(100));
}
}
}
return Ok(pin)
}

Expand Down
28 changes: 22 additions & 6 deletions src/save_audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use byteorder::LittleEndian;
use byteorder::{ByteOrder, WriteBytesExt};
use chrono::{DateTime, Utc};
use log::{error, info};
use std::io::Cursor;
use std::io::{Cursor, Read};
use std::io::Write;
use std::process::{Command, Stdio};
use std::{fs, thread};
Expand All @@ -28,11 +28,13 @@ fn wav_header(audio_length: usize, sample_rate: u32) -> [u8; 44] {

// fmt block (24 bytes)
cursor.write_all(b"fmt ").unwrap();

// Size of format data after this point (minus "fmt " and 16u32, i.e. 8 bytes)
cursor.write_u32::<LittleEndian>(16).unwrap();
cursor.write_u16::<LittleEndian>(format_pcm).unwrap();
cursor.write_u16::<LittleEndian>(num_channels).unwrap();
cursor.write_u32::<LittleEndian>(sample_rate).unwrap();

cursor.write_u32::<LittleEndian>(bytes_per_second).unwrap();
cursor.write_u16::<LittleEndian>(bytes_per_block).unwrap();
cursor.write_u16::<LittleEndian>(bits_per_sample).unwrap();
Expand Down Expand Up @@ -66,11 +68,12 @@ pub fn save_audio_file_to_disk(mut audio_bytes: Vec<u8>, device_config: DeviceCo
.expect(&format!("Failed to create debug output directory {}", debug_dir));
let output_path: String = format!(
"{}/{}.raw",
output_dir,
debug_dir,
recording_date_time.format("%Y-%m-%d--%H-%M-%S")
);
fs::write(&output_path, &audio_bytes).unwrap();
fs::write(&output_path, &audio_bytes).unwrap();
}


let output_path: String =
format!("{}/{}.aac", output_dir, recording_date_time.format("%Y-%m-%d--%H-%M-%S"));
Expand All @@ -94,6 +97,7 @@ pub fn save_audio_file_to_disk(mut audio_bytes: Vec<u8>, device_config: DeviceCo
let sample_rate = 48000;
let duration_seconds = audio_bytes[12..].len() as f32 / sample_rate as f32 / 2.0;
let duration = format!("duration={}", duration_seconds);
let sr = format!("originalSampleRate={}", original_sample_rate);
let is_test_recording = duration_seconds < 3.0;
let mut args = Vec::from([
"-i",
Expand Down Expand Up @@ -126,6 +130,8 @@ pub fn save_audio_file_to_disk(mut audio_bytes: Vec<u8>, device_config: DeviceCo
&location_timestamp,
"-metadata",
&location_accuracy,
"-metadata",
&sr,
]);
if is_test_recording {
args.push("-metadata");
Expand All @@ -135,6 +141,7 @@ pub fn save_audio_file_to_disk(mut audio_bytes: Vec<u8>, device_config: DeviceCo
args.push("mp4");
args.push(&output_path);
info!("Saving AAC file with args {:#?}", args);

// Now transcode with ffmpeg – we create an aac stream in an m4a wrapper in order
// to support adding metadata tags.
let mut cmd = match Command::new("ffmpeg")
Expand Down Expand Up @@ -164,16 +171,24 @@ pub fn save_audio_file_to_disk(mut audio_bytes: Vec<u8>, device_config: DeviceCo
.write_all(&wav_header(audio_data.len(), sample_rate))
.expect("Failed to write WAV header to stdin");
stdin.write_all(audio_data).expect("Failed to write audio data to stdin");
// Explicitly close stdin to signal EOF to ffmpeg
// Explicitly close stdin to signal EOF to ffmpeg
stdin.flush().expect("Failed to flush stdin");
}

match cmd.wait() {
Ok(exit_status) => {
if exit_status.success() {
info!("Saved AAC file {}", output_path);
} else {
error!("Failed transcoding {} to AAC", output_path);
let mut stderr = match cmd.stderr.take() {
Some(stderr) => stderr,
None => {
error!("Failed to open stderr for ffmpeg process");
return;
}
};
let mut buffer = String::new();
let _ = stderr.read_to_string(&mut buffer);
error!("Failed transcoding {} to AAC ffmpeg output {}", output_path,buffer);
}
}
Err(e) => {
Expand All @@ -189,3 +204,4 @@ pub fn save_audio_file_to_disk(mut audio_bytes: Vec<u8>, device_config: DeviceCo
},
);
}

6 changes: 1 addition & 5 deletions src/save_cptv.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use crate::cptv_header::{decode_cptv_header_streaming, CptvHeader};
use chrono::{DateTime, Utc};
use flate2::read::GzEncoder;
use flate2::read::MultiGzDecoder;
use flate2::Compression;
use log::{error, info};
use std::io::prelude::*;
use std::{fs, thread};
use thread_priority::{ThreadBuilderExt, ThreadPriority};

Expand Down Expand Up @@ -38,7 +34,7 @@ pub fn save_cptv_file_to_disk(mut cptv_bytes: Vec<u8>, output_dir: &str) ->Resul
// let mut new_cptv_bytes = Vec::new();
// encoder.read_to_end(&mut new_cptv_bytes).unwrap();

// // Only use the re-compressed file if it actually got smaller.
// Only use the re-compressed file if it actually got smaller.
// if new_cptv_bytes.len() > cptv_bytes.len() {
// new_cptv_bytes = cptv_bytes;
// }
Expand Down

0 comments on commit eb9edf5

Please sign in to comment.