Skip to content
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

Rust Rocket Backend / WebRTC H264 Camera Streaming #5

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

megustaloscheetos
Copy link

@megustaloscheetos megustaloscheetos commented Oct 3, 2024

For issue #4, a basic finished version of the Rust backend I have been working on. Notably, the backend streams H264 video to web browser clients through WebRTC. Of which, multiple clients can be handled at once without issue. The backend also includes a basic api to set the resolution and frame rate configuration at which the stream is sent (there are example api calls commented out in the react-app App.js file). But that should be all with regards to the actual backend! However, at the moment, this PR is mainly to add something that is usable at a basic capacity. Without a doubt, I do think that my code could be improved through review as I do use things such as boxed errors, however, this is something that I am looking for guidance with!

@megustaloscheetos megustaloscheetos linked an issue Oct 3, 2024 that may be closed by this pull request
4 tasks
@onkoe
Copy link
Member

onkoe commented Oct 24, 2024

Thank you for your work, Mizael! A lot of my suggestions have to do with the formatting and redundancies. These can be detected with cargo fmt and cargo clippy. Could you please run them?

You can also configure your editor to use these automatically:

Please run these tools and ping me again when complete. Thanks again!

@onkoe
Copy link
Member

onkoe commented Nov 7, 2024

This is very good! Just needs a few more (mostly documentation) changes.

One more suggestion: my_arc.clone() is typically less clear than Arc::clone(my_arc). Please consider using it in the future. Don't worry about it now, though :)

Comment on lines +104 to +108
"{}x{} @{}fps",
self.width, self.height, self.frame_interval.denominator
)
}
}
Copy link
Member

Choose a reason for hiding this comment

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

As the Clippy lint suggests, you're better off using fmt::Display to get the ToString trait on a type. Most generic functions look for Display, not ToString, so this advice is generally useful.

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"{}x{} @{}fps",
self.width, self.height, self.frame_interval.denominator
)
}
}
impl core::fmt::Display for CameraMode {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"{}x{} @{}fps",
self.width, self.height, self.frame_interval.denominator
)
}
}

Copy link
Member

Choose a reason for hiding this comment

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

Above is a typical conversion from ToString. The write! macro is pretty nifty here!

}
}

// Handles communication between CameraThreadHandle(s) and WebRTC clients.
Copy link
Member

Choose a reason for hiding this comment

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

You may have intended to use a doc comment here:

Suggested change
// Handles communication between CameraThreadHandle(s) and WebRTC clients.
/// Handles communication between CameraThreadHandle(s) and WebRTC clients.

}

// IMPORTANT: Everything in here runs within the tokio runtime!
pub async fn add_client(
Copy link
Member

Choose a reason for hiding this comment

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

I appreciate the notice above. However, the exact meaning of a "client" might be unclear far into the future. Please consider adding doc comments to this method.

Comment on lines +193 to +194
"video".to_owned(),
"webrtc".to_owned(),
Copy link
Member

Choose a reason for hiding this comment

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

If you create multiple streams under the same IDs, what happens? I'm actually not sure, but I'm concerned that it may result in unexpected behavior. Please make sure the constant IDs are intentional.

Remember: it's very important that folks can understand this later on. May and June are going to be chock-full of debugging, so we want to get it out of the way now! :D

camera_path,
manual_shutdown_needed,
cam_mode_tx,
current_mode: *modes.last().ok_or("No Last Mode")?,
Copy link
Member

Choose a reason for hiding this comment

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

Be careful with errors like this! These can lack context at competition.

In a future PR, I'll add tracing to the project to help better log the camera/server state. For now, you might want to add something like "Error creating camera thread: failed to initialize camera". (this also goes for the other usage ~20 lines up)

return;
}

// Used to prevent having to constantly lock the c_tx_sink mutex.
Copy link
Member

Choose a reason for hiding this comment

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

This might be 2% more idiomatic? ;D

Suggested change
// Used to prevent having to constantly lock the c_tx_sink mutex.
while !c_manual_shutdown_needed.load(Ordering::SeqCst) {

Copy link
Member

Choose a reason for hiding this comment

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

lol not sure why it replaced the comment. it swaps out the loop {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Camera UI Backend
2 participants