server: warning cleanup

This commit is contained in:
Lily Tsuru 2024-10-15 06:45:40 -04:00
parent 3c2d4cee6f
commit 2e4e810e5f
9 changed files with 61 additions and 37 deletions

View file

@ -5,7 +5,6 @@ mod video;
mod transport;
use anyhow::Context;
use async_trait::async_trait;
use cudarc::driver::CudaDevice;
@ -14,15 +13,14 @@ use retro_thread::{spawn_retro_thread, RetroEvent};
use transport::websocket::WebsocketTransport;
use transport::{Transport, TransportReciever};
use video::cuda_gl::safe::GraphicsResource;
use video::encoder_thread;
use video::encoder_thread::EncodeThreadInput;
use video::{encoder_thread, ffmpeg};
use std::{
sync::{Arc, Mutex},
time::Duration,
};
use std::net::SocketAddr;
use tokio::sync::{
mpsc::{self, error::TryRecvError},
Mutex as TokioMutex,
@ -198,12 +196,21 @@ async fn main() -> anyhow::Result<()> {
.name("retro_event_rx".into())
.spawn(move || {
// load game
/*
let _ = retro_input_event_tx.blocking_send(retro_thread::RetroInEvent::LoadCore(
"cores/swanstation_libretro.so".into(),
));
let _ = retro_input_event_tx.blocking_send(retro_thread::RetroInEvent::LoadGame(
"roms/merged/nmv2/jagb/nmv2jagb.cue".into(),
));
*/
let _ = retro_input_event_tx.blocking_send(retro_thread::RetroInEvent::LoadCore(
"cores/pcsx2_libretro.so".into(),
));
let _ = retro_input_event_tx.blocking_send(retro_thread::RetroInEvent::LoadGame(
"/data/sda/lily/ISOs/Sony PlayStation 2/ztx-hl.bin".into(),
));
// start the libretro thread looping now that we're alive
let _ = retro_input_event_tx.blocking_send(retro_thread::RetroInEvent::Start);

View file

@ -217,21 +217,18 @@ impl FrontendInterface for RetroState {
self.software_framebuffer.resize(Size { width, height });
}
unsafe {
}
// map to cuda
// register the FBO's texture to our cuda interop resource
{
let mut locked = self
.cuda_resource
.lock()
.expect("YOU MOTHERFUCKER PISS GO COUNT YOUR DICK");
.expect("Failed to lock CUDA resource");
locked.device().bind_to_thread().expect("fuck");
locked.device().bind_to_thread().expect("Failed to bind CUDA device to thread");
locked
.register(self.gl_framebuffer.texture_id(), gl::TEXTURE_2D)
.expect("you fucking asswater");
.expect("Failed to register OpenGL texture with CUDA Graphics resource");
}
let _ = self.event_tx.blocking_send(RetroEvent::Resize {
@ -375,6 +372,8 @@ impl FrontendInterface for RetroState {
self.software_framebuffer.clear();
self.gl_rendering = true;
tracing::info!("Rendering with OpenGL via EGL");
return Some(HwGlInitData {
get_proc_address: gpu::egl::GetProcAddress as *mut std::ffi::c_void,
});

View file

@ -6,16 +6,12 @@ use std::sync::Arc;
use rand::distributions::DistString;
use std::net::SocketAddr;
use tokio::sync::{
broadcast,
mpsc::{self, error::TryRecvError},
Mutex as TokioMutex,
};
use tokio::sync::broadcast;
use axum::{
extract::{
connect_info::ConnectInfo,
ws::{self, Message, WebSocket, WebSocketUpgrade},
ws::{Message, WebSocket, WebSocketUpgrade},
State,
},
response::IntoResponse,
@ -34,7 +30,7 @@ async fn ws_handler<T: TransportReciever + Sync + Send + 'static>(
ConnectInfo(addr): ConnectInfo<SocketAddr>,
State(state): State<Arc<T>>,
mut broadcast_rx: broadcast::Receiver<TransportMessage>,
broadcast_rx: broadcast::Receiver<TransportMessage>,
) -> impl IntoResponse {
// finalize the upgrade process by returning upgrade callback.
// we can customize the callback by sending additional info such as address.
@ -44,7 +40,7 @@ async fn ws_handler<T: TransportReciever + Sync + Send + 'static>(
/// Actual websocket statemachine (one will be spawned per connection)
async fn handle_socket<T: TransportReciever + Sync + Send + 'static>(
socket: WebSocket,
who: SocketAddr,
_who: SocketAddr, // FIXME
state: Arc<T>,
mut broadcast_rx: broadcast::Receiver<TransportMessage>,
) {
@ -56,7 +52,7 @@ async fn handle_socket<T: TransportReciever + Sync + Send + 'static>(
let recv_clone = Arc::clone(&state);
state.on_connect(&username).await;
let _ = state.on_connect(&username).await;
let mut send_task = tokio::spawn(async move {
while let Ok(msg) = broadcast_rx.recv().await {
@ -80,7 +76,7 @@ async fn handle_socket<T: TransportReciever + Sync + Send + 'static>(
while let Some(Ok(msg)) = receiver.next().await {
match msg {
Message::Text(msg) => {
recv_clone.on_message(&username_clone, &msg).await;
let _ = recv_clone.on_message(&username_clone, &msg).await;
}
Message::Close(_) => break,
_ => {}
@ -98,7 +94,7 @@ async fn handle_socket<T: TransportReciever + Sync + Send + 'static>(
}
}
state.on_leave(&username).await;
let _ = state.on_leave(&username).await;
}
impl WebsocketTransport {

View file

@ -1,3 +1,4 @@
#[allow(non_snake_case)]
pub mod sys;
use sys::*;

View file

@ -1,6 +1,4 @@
use cudarc::driver::{result as cuda_result, safe as cuda_safe, sys as cuda_sys, CudaDevice};
use super::sys;
use cudarc::driver::{result as cuda_result, sys as cuda_sys, CudaDevice};
use std::sync::Arc;
@ -49,7 +47,9 @@ impl MappedGraphicsResource {
Ok(array)
}
pub fn get_device_pointer(&mut self) -> Result<cuda_sys::CUdeviceptr, cuda_result::DriverError> {
pub fn get_device_pointer(
&mut self,
) -> Result<cuda_sys::CUdeviceptr, cuda_result::DriverError> {
assert!(
!self.resource.is_null(),
"do not call GraphicsResource::get_mapped_array if no resource is actually registered"
@ -138,8 +138,6 @@ impl GraphicsResource {
self.resource = std::ptr::null_mut();
Ok(())
}
}
impl Drop for GraphicsResource {

View file

@ -193,13 +193,14 @@ impl H264Encoder {
video_encoder_context.set_format(ffmpeg::format::Pixel::CUDA);
video_encoder_context.set_qmin(35);
video_encoder_context.set_qmax(30);
video_encoder_context.set_qmax(38);
unsafe {
// FIXME: this currently breaks the avbufferref system a bit
(*video_encoder_context.as_mut_ptr()).hw_frames_ctx = hw_frame_context.as_raw_mut();
(*video_encoder_context.as_mut_ptr()).hw_frames_ctx =
ffmpeg::sys::av_buffer_ref(hw_frame_context.as_raw_mut());
(*video_encoder_context.as_mut_ptr()).hw_device_ctx =
hw_frame_context.as_device_context_mut();
ffmpeg::sys::av_buffer_ref(hw_frame_context.as_device_context_mut());
}
// set h264_nvenc options
@ -269,7 +270,6 @@ impl H264Encoder {
(*frame.as_mut_ptr()).height = encoder.height() as i32;
(*frame.as_mut_ptr()).hw_frames_ctx = hw_context.as_raw_mut();
hw_context.get_buffer(&mut frame)?;
hw_context.get_buffer(&mut frame)?;
(*frame.as_mut_ptr()).linesize[0] = (*frame.as_ptr()).width * 4;

View file

@ -30,6 +30,16 @@ impl CudaDeviceContext {
// }
}
impl Drop for CudaDeviceContext {
fn drop(&mut self) {
unsafe {
if !self.buffer.is_null() {
ffmpeg::sys::av_buffer_unref(&mut self.buffer);
}
}
}
}
pub struct CudaDeviceContextBuilder {
buffer: *mut ffmpeg::sys::AVBufferRef,
}

View file

@ -32,15 +32,17 @@ impl HwFrameContext {
}
pub fn as_device_context_mut(&mut self) -> &mut ffmpeg::sys::AVBufferRef {
unsafe {
self._cuda_device_context.as_raw_mut()
}
}
/// call once to allocate frame
pub fn get_buffer(&mut self, frame: &mut ffmpeg::frame::Video) -> Result<(), ffmpeg::Error> {
unsafe {
super::check_ret(ffmpeg::sys::av_hwframe_get_buffer(self.buffer, frame.as_mut_ptr(), 0))?;
super::check_ret(ffmpeg::sys::av_hwframe_get_buffer(
self.buffer,
frame.as_mut_ptr(),
0,
))?;
}
Ok(())
@ -53,6 +55,16 @@ impl HwFrameContext {
unsafe impl Send for HwFrameContext {}
impl Drop for HwFrameContext {
fn drop(&mut self) {
unsafe {
if !self.buffer.is_null() {
ffmpeg::sys::av_buffer_unref(&mut self.buffer);
}
}
}
}
pub struct HwFrameContextBuilder {
cuda_device_context: CudaDeviceContext,
buffer: *mut ffmpeg::sys::AVBufferRef,

View file

@ -7,6 +7,7 @@ pub use ffmpeg as ffmpeg;
pub mod hwdevice;
pub mod hwframe;
#[allow(unused)] // FIXME
pub mod encoder_thread;
pub mod cuda_gl;