From 3cad3b1e29cfc76116399e3475113b59ee22e312 Mon Sep 17 00:00:00 2001 From: viv Date: Sun, 1 Aug 2021 00:08:18 -0700 Subject: [PATCH] Don't try to write an audio frame if there aren't any planes --- examples/ffmpeg_recorder.rs | 57 +++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/examples/ffmpeg_recorder.rs b/examples/ffmpeg_recorder.rs index 2496ed3..c63057a 100644 --- a/examples/ffmpeg_recorder.rs +++ b/examples/ffmpeg_recorder.rs @@ -308,43 +308,44 @@ static bool ffmpeg_init_config(struct ff_config_param *params, } } - // Assumption: if there is a video frame, there is an audio frame. let mut aframe = frame::Audio::new( format::Sample::I16(format::sample::Type::Packed), self.audio_buf.len(), ChannelLayout::STEREO ); - aframe.set_channels(2); - aframe.set_rate(44100); - aframe.set_pts(Some(frame)); - let aplane: &mut [(i16, i16)] = aframe.plane_mut(0); - eprintln!("Audio buffer length {} -> {}", self.audio_buf.len(), aplane.len()); - aplane.copy_from_slice(self.audio_buf.as_ref()); - //eprintln!("src: {:?}, dest: {:?}", self.audio_buf, aplane); - self.audio_buf.clear(); + if aframe.planes() > 0 { + aframe.set_channels(2); + aframe.set_rate(44100); + aframe.set_pts(Some(frame)); + let aplane: &mut [(i16, i16)] = aframe.plane_mut(0); + eprintln!("Audio buffer length {} -> {}", self.audio_buf.len(), aplane.len()); + aplane.copy_from_slice(self.audio_buf.as_ref()); + //eprintln!("src: {:?}, dest: {:?}", self.audio_buf, aplane); + self.audio_buf.clear(); - eprintln!("frame audio: {:?}", aframe); + eprintln!("frame audio: {:?}", aframe); - eprintln!("🎞 queue frame pts {:?}", aframe.pts()); - self.audio_filter.get("in").unwrap().source().add(&aframe).unwrap(); + eprintln!("🎞 queue frame pts {:?}", aframe.pts()); + self.audio_filter.get("in").unwrap().source().add(&aframe).unwrap(); - let mut filtered_aframe = frame::Audio::empty(); - loop { - match self.audio_filter.get("out").unwrap().sink().frame(&mut filtered_aframe) { - Ok(..) => { - eprintln!("🔊 Got filtered audio frame {:?} pts {:?}", filtered_aframe, filtered_aframe.pts()); - if self.audio_filter.get("in").unwrap().source().failed_requests() > 0 { - println!("🎥 failed to put filter input frame"); + let mut filtered_aframe = frame::Audio::empty(); + loop { + match self.audio_filter.get("out").unwrap().sink().frame(&mut filtered_aframe) { + Ok(..) => { + eprintln!("🔊 Got filtered audio frame {:?} pts {:?}", filtered_aframe, filtered_aframe.pts()); + if self.audio_filter.get("in").unwrap().source().failed_requests() > 0 { + println!("🎥 failed to put filter input frame"); + } + //let faplane: &[f32] = filtered_aframe.plane(0); + //filtered_aframe.set_pts(Some(frame)); + + self.audio_encoder.send_frame(&filtered_aframe).unwrap(); + self.receive_and_write_packets(EncoderToWriteFrom::Audio); + }, + Err(e) => { + eprintln!("Error getting filtered audio frame: {:?}", e); + break; } - //let faplane: &[f32] = filtered_aframe.plane(0); - //filtered_aframe.set_pts(Some(frame)); - - self.audio_encoder.send_frame(&filtered_aframe).unwrap(); - self.receive_and_write_packets(EncoderToWriteFrom::Audio); - }, - Err(e) => { - eprintln!("Error getting filtered audio frame: {:?}", e); - break; } } }