From d3a88573d788507f61d5078e90a976d4d2dab444 Mon Sep 17 00:00:00 2001 From: Vivian Lim Date: Tue, 20 Apr 2021 23:57:39 -0700 Subject: [PATCH] move around some things but now my debugger doesn't work for some inexplicable reason --- examples/ffmpeg_recorder.rs | 98 +++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 48 deletions(-) diff --git a/examples/ffmpeg_recorder.rs b/examples/ffmpeg_recorder.rs index 3d78e21..0d83ca8 100644 --- a/examples/ffmpeg_recorder.rs +++ b/examples/ffmpeg_recorder.rs @@ -29,6 +29,7 @@ struct MyEmulator { audio_filter: filter::Graph, sys_path: Option, frame_properties_locked: bool, + //octx: ffmpeg::format::context::Output, } fn video_filter( @@ -105,8 +106,8 @@ impl MyEmulator { pub fn new( core_path: impl AsRef, sys_path: &Option>, - mut video_encoder: ffmpeg::encoder::video::Video, - mut audio_encoder: ffmpeg::encoder::audio::Audio, + video_path: impl AsRef, + mut octx: ffmpeg::format::context::Output, ) -> Pin> { let lib = libloading::Library::new(core_path.as_ref()).unwrap(); let raw_retro = retro::loading::LibretroApi::from_library(lib).unwrap(); @@ -118,7 +119,32 @@ impl MyEmulator { let fps_int = av_info.timing.fps.round() as i32; let fps_int = if fps_int == 0 { 60 } else { fps_int }; + let detected_vcodec = octx.format().codec(&video_path, media::Type::Video); + let detected_acodec = octx.format().codec(&video_path, media::Type::Audio); + + let vcodec = ffmpeg::encoder::find(detected_vcodec).unwrap().video().unwrap(); + let acodec = ffmpeg::encoder::find(detected_acodec).unwrap().audio().unwrap(); + + let mut audio_encoder = octx.add_stream(acodec).unwrap().codec().encoder().audio().unwrap(); + //let mut video_encoder = octx.add_stream(vcodec).unwrap().codec().encoder().video().unwrap(); + let mut video_output = octx.add_stream(vcodec).unwrap(); + let mut video_encoder = video_output.codec().encoder().video().unwrap(); + /* + let mut audio_output = octx.add_stream(acodec).unwrap(); + let mut audio_encoder = audio_output.codec().encoder().audio().unwrap(); + */ + + video_encoder.set_bit_rate(64000); + video_encoder.set_format(video_encoder.codec().unwrap().video().unwrap().formats().unwrap().nth(0).unwrap()); + + audio_encoder.set_bit_rate(64000); + audio_encoder.set_rate(44100); + audio_encoder.set_format(audio_encoder.codec().unwrap().audio().unwrap().formats().unwrap().nth(0).unwrap()); + audio_encoder.set_channels(2); + audio_encoder.set_channel_layout(ChannelLayout::STEREO); + video_encoder.set_time_base(Rational::new(1, fps_int)); + video_output.set_time_base(Rational::new(1, fps_int)); video_encoder.set_frame_rate(av_info.timing.fps.into()); if av_info.geometry.base_height == 0 && av_info.geometry.base_width == 0 { av_info.geometry.base_width = 320; @@ -138,9 +164,11 @@ impl MyEmulator { let video_filter = video_filter(&video_encoder, &av_info, pix_fmt).unwrap(); let audio_filter = audio_filter(&audio_encoder, av_info.timing.sample_rate).unwrap(); - let video_encoder = video_encoder.open().unwrap(); - let mut audio_encoder = audio_encoder.open().unwrap(); + let video_encoder = video_encoder.open_as(vcodec).unwrap(); + video_output.set_parameters(&video_encoder); + let mut audio_encoder = audio_encoder.open_as(acodec).unwrap(); audio_encoder.set_rate(av_info.timing.sample_rate.round() as i32); + //audio_output.set_parameters(&audio_encoder); let emu = MyEmulator { retro, @@ -155,6 +183,7 @@ impl MyEmulator { audio_filter, sys_path: sys_path.as_ref().map(|x| x.as_ref().to_path_buf()), frame_properties_locked: false, + //octx, // AAA }; let mut pin_emu = Box::pin(emu); @@ -164,7 +193,7 @@ impl MyEmulator { pin_emu } - pub fn run(&mut self, octx: &mut format::context::Output, frame: i64) { + pub fn run(&mut self, frame: i64) { self.retro.run(); let mut vframe = self.video_frames.pop_front().unwrap(); @@ -181,29 +210,27 @@ impl MyEmulator { let mut out = ffmpeg::Packet::empty(); self.video_filter.get("in").unwrap().source().add(&vframe).unwrap(); self.audio_filter.get("in").unwrap().source().add(&aframe).unwrap(); - while let Ok(..) = self.video_filter.get("out").unwrap().sink().frame(&mut vframe) { + let mut filtered_vframe = frame::Video::empty(); + while self.video_filter.get("out").unwrap().sink().frame(&mut filtered_vframe).is_ok() { if self.video_filter.get("in").unwrap().source().failed_requests() > 0 { println!("🎥 failed to put filter input frame"); } - if self.video_filter.get("out").unwrap().source().failed_requests() > 0 { - println!("🎥 failed to get filter output frame"); - } vframe.set_pts(Some(frame)); - let encode_result = self.video_encoder.encode(&vframe, &mut out).unwrap(); + let encode_result = self.video_encoder.send_frame(&filtered_vframe).unwrap(); eprintln!("encoded: {:?}", encode_result); - if encode_result { + /*if encode_result { out.set_stream(0); out.write_interleaved(octx).unwrap(); } - - /* - while self.video_encoder.receive_packet(&mut encoded).is_ok() { - panic!("actually trying to write something!!"); - encoded.set_stream(0); // use stream index... - // rescale_ts?? - encoded.write_interleaved(&mut octx).unwrap(); - } */ + + let mut encoded_packet = ffmpeg::Packet::empty(); + while self.video_encoder.receive_packet(&mut encoded_packet).is_ok() { + //eprintln!("Attempting write: {:?}", encoded_packet); + encoded_packet.set_stream(0); // use stream index... + //encoded_packet.rescale_ts(Rational(1, 1), self.time_base); + //encoded_packet.write_interleaved(&mut self.octx).unwrap(); // AAA + } //eprintln!("packet: {:?}", &out.data()); //self.video_encoder.flush(&mut out).unwrap(); //eprintln!("flushed packet: {:?}", &out.data()); @@ -356,46 +383,21 @@ fn main() -> Fallible<()> { let mut octx = format::output(&opt.video)?; - let detected_vcodec = octx.format().codec(&opt.video, media::Type::Video); - let detected_acodec = octx.format().codec(&opt.video, media::Type::Audio); - - let vcodec = ffmpeg::encoder::find(detected_vcodec).unwrap().video()?; - let acodec = ffmpeg::encoder::find(detected_acodec).unwrap().audio()?; - - let mut video_encoder = octx.add_stream(vcodec)?.codec().encoder().video()?; - let mut audio_encoder = octx.add_stream(acodec)?.codec().encoder().audio()?; - - video_encoder.set_bit_rate(64000); - video_encoder.set_format(video_encoder.codec().unwrap().video()?.formats().unwrap().nth(0).unwrap()); - - audio_encoder.set_bit_rate(64000); - audio_encoder.set_rate(44100); - audio_encoder.set_format(audio_encoder.codec().unwrap().audio()?.formats().unwrap().nth(0).unwrap()); - audio_encoder.set_channels(2); - audio_encoder.set_channel_layout(ChannelLayout::STEREO); - - let mut emu = MyEmulator::new(opt.core, &opt.system, video_encoder, audio_encoder); + let mut emu = MyEmulator::new(opt.core, &opt.system, &opt.video, octx); emu.load_game(opt.rom); emu.frame_properties_locked = true; - octx.write_header().unwrap(); + //octx.write_header().unwrap(); //for frame in 0..60*10 { for frame in 0..120 { eprintln!("🖼️ frame: {}", frame); - emu.run(&mut octx, frame); + emu.run(frame); - let mut encoded = ffmpeg::Packet::empty(); - while emu.video_encoder.receive_packet(&mut encoded).is_ok() { - panic!("actually trying to write something!!"); - encoded.set_stream(0); // use stream index... - // rescale_ts?? - encoded.write_interleaved(&mut octx).unwrap(); - } } let mut packet = Packet::empty(); eprintln!("flushed: {:?}", emu.video_encoder.flush(&mut packet).unwrap()); - octx.write_trailer().unwrap(); + //octx.write_trailer().unwrap(); Ok(()) }