Repository navigation
fix(cap-muxer): finish the open fragment when the recorder dies #2644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
92e23ce
fix(cap-muxer): finish the open fragment when the recorder dies
richiemcilroy a9d2996
test(cap-muxer): name the encoded packet tuple
richiemcilroy 109aa5e
test(cap-muxer): always log the EOF warning in the orphan test
richiemcilroy d394f61
Merge branch 'main' into fix/cap-muxer-survives-recorder-crash
richiemcilroy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| //! The recorder can die while cap-muxer runs. Its stdin then reaches EOF and | ||
| //! the pipe the recorder read cap-muxer's stderr through is closed; cap-muxer | ||
| //! must still finish the fragment it has open. | ||
|
|
||
| use std::{ | ||
| io::Write, | ||
| process::{Command, Stdio}, | ||
| }; | ||
|
|
||
| use cap_muxer_protocol::{ | ||
| Frame, InitVideo, PACKET_FLAG_KEYFRAME, Packet, STREAM_INDEX_VIDEO, StartParams, write_frame, | ||
| write_packet, | ||
| }; | ||
| use ffmpeg::{codec, format, frame}; | ||
|
|
||
| const WIDTH: u32 = 320; | ||
| const HEIGHT: u32 = 240; | ||
| const FPS: i32 = 30; | ||
|
|
||
| type EncodedPacket = (i64, i64, bool, Vec<u8>); | ||
|
|
||
| fn encode(count: i64, gop: u32) -> (Vec<u8>, Vec<EncodedPacket>) { | ||
| ffmpeg::init().unwrap(); | ||
| let codec = codec::encoder::find_by_name("libx264").expect("libx264"); | ||
| let mut context = codec::context::Context::new_with_codec(codec) | ||
| .encoder() | ||
| .video() | ||
| .unwrap(); | ||
| context.set_width(WIDTH); | ||
| context.set_height(HEIGHT); | ||
| context.set_format(format::Pixel::YUV420P); | ||
| context.set_time_base((1, FPS)); | ||
| context.set_frame_rate(Some((FPS, 1))); | ||
| context.set_gop(gop); | ||
| context.set_max_b_frames(0); | ||
| context.set_flags(codec::Flags::GLOBAL_HEADER); | ||
| let mut options = ffmpeg::Dictionary::new(); | ||
| options.set("preset", "ultrafast"); | ||
| options.set("tune", "zerolatency"); | ||
| let mut encoder = context.open_as_with(codec, options).unwrap(); | ||
| let extradata = unsafe { | ||
| let ctx = encoder.as_ptr(); | ||
| std::slice::from_raw_parts((*ctx).extradata, (*ctx).extradata_size as usize).to_vec() | ||
| }; | ||
| let mut packets = Vec::new(); | ||
| let mut packet = ffmpeg::Packet::empty(); | ||
| let mut drain = |encoder: &mut ffmpeg::encoder::Video, packets: &mut Vec<_>| { | ||
| while encoder.receive_packet(&mut packet).is_ok() { | ||
| packets.push(( | ||
| packet.pts().unwrap(), | ||
| packet.dts().unwrap(), | ||
| packet.is_key(), | ||
| packet.data().unwrap().to_vec(), | ||
| )); | ||
| } | ||
| }; | ||
| for index in 0..count { | ||
| let mut picture = frame::Video::new(format::Pixel::YUV420P, WIDTH, HEIGHT); | ||
| for plane in 0..3 { | ||
| picture.data_mut(plane).fill((index * 3 % 255) as u8); | ||
| } | ||
| picture.set_pts(Some(index)); | ||
| encoder.send_frame(&picture).unwrap(); | ||
| drain(&mut encoder, &mut packets); | ||
| } | ||
| encoder.send_eof().unwrap(); | ||
| drain(&mut encoder, &mut packets); | ||
| (extradata, packets) | ||
| } | ||
|
|
||
| #[test] | ||
| fn an_orphaned_muxer_finishes_its_open_fragment() { | ||
| let (extradata, packets) = encode(75, 30); | ||
| let directory = tempfile::tempdir().unwrap(); | ||
| let output = directory.path().join("display"); | ||
| let mut child = Command::new(env!("CARGO_BIN_EXE_cap-muxer")) | ||
| .env("CAP_MUXER_LOG", "warn") | ||
| .stdin(Stdio::piped()) | ||
| .stdout(Stdio::null()) | ||
| .stderr(Stdio::piped()) | ||
| .spawn() | ||
| .unwrap(); | ||
| let mut stdin = child.stdin.take().unwrap(); | ||
| write_frame( | ||
| &mut stdin, | ||
| &Frame::InitVideo(InitVideo { | ||
| codec: "libx264".into(), | ||
| width: WIDTH, | ||
| height: HEIGHT, | ||
| frame_rate_num: FPS, | ||
| frame_rate_den: 1, | ||
| time_base_num: 1, | ||
| time_base_den: FPS, | ||
| extradata, | ||
| segment_duration_ms: 1000, | ||
| }), | ||
| ) | ||
| .unwrap(); | ||
| write_frame( | ||
| &mut stdin, | ||
| &Frame::Start(StartParams { | ||
| output_directory: output.to_string_lossy().into_owned(), | ||
| init_segment_name: "init.mp4".into(), | ||
| media_segment_pattern: "segment_$Number%03d$.m4s".into(), | ||
| }), | ||
| ) | ||
| .unwrap(); | ||
| for (pts, dts, key, data) in &packets { | ||
| write_packet( | ||
| &mut stdin, | ||
| &Packet { | ||
| stream_index: STREAM_INDEX_VIDEO, | ||
| pts: *pts, | ||
| dts: *dts, | ||
| duration: 1, | ||
| flags: if *key { PACKET_FLAG_KEYFRAME } else { 0 }, | ||
| data, | ||
| }, | ||
| ) | ||
| .unwrap(); | ||
| } | ||
| stdin.flush().unwrap(); | ||
| // The recorder dies: its end of the stderr pipe closes, then stdin. | ||
| drop(child.stderr.take()); | ||
| drop(stdin); | ||
| let status = child.wait().unwrap(); | ||
| assert_eq!(status.code(), Some(0), "cap-muxer must finish, not panic"); | ||
|
|
||
| let mut segments: Vec<_> = std::fs::read_dir(&output) | ||
| .unwrap() | ||
| .flatten() | ||
| .map(|entry| entry.file_name().to_string_lossy().into_owned()) | ||
| .filter(|name| name.starts_with("segment_")) | ||
| .collect(); | ||
| segments.sort(); | ||
| assert_eq!( | ||
| segments, | ||
| ["segment_001.m4s", "segment_002.m4s", "segment_003.m4s"], | ||
| "every fragment, including the one open when the recorder died, is finished" | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.