Skip to content

Commit

Permalink
fix video for multiple clients
Browse files Browse the repository at this point in the history
  • Loading branch information
marci1175 committed Aug 17, 2024
1 parent a498113 commit e853412
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 368 deletions.
5 changes: 4 additions & 1 deletion desktop/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>>

fn generate_emoji_header() -> Result<(), Box<dyn std::error::Error>>
{
let path_to_output = PathBuf::from(format!("{}\\emoji_header.rs", dbg!(std::env::var("OUT_DIR")?)));
let path_to_output = PathBuf::from(format!(
"{}\\emoji_header.rs",
dbg!(std::env::var("OUT_DIR")?)
));

//This will get written to the output file
let mut content = String::new();
Expand Down
2 changes: 1 addition & 1 deletion desktop/build_info.matthias_build
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024.08.17. 19:53
2024.08.17. 21:52
21 changes: 12 additions & 9 deletions desktop/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,15 +479,18 @@ impl backend::Application

ui.horizontal(|ui| {
ui.label("Microphone volume precentage");
self.client_ui.microphone_volume.fetch_update(std::sync::atomic::Ordering::SeqCst, std::sync::atomic::Ordering::SeqCst, |mut value| {
ui.add(Slider::new(
&mut value,
50..=500,
));

Some(value)
});

self.client_ui
.microphone_volume
.fetch_update(
std::sync::atomic::Ordering::SeqCst,
std::sync::atomic::Ordering::SeqCst,
|mut value| {
ui.add(Slider::new(&mut value, 50..=500));

Some(value)
},
)
.unwrap_or_default();
});
});
}
Expand Down
15 changes: 8 additions & 7 deletions desktop/src/app/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,10 @@ impl ClientMessage
.to_str()
.unwrap()
.to_string()
.split(".").next().unwrap().to_string(),
.split(".")
.next()
.unwrap()
.to_string(),
),
bytes: std::fs::read(file_path).unwrap_or_default(),
}),
Expand Down Expand Up @@ -1846,7 +1849,7 @@ pub enum ServerMessageType

/// This message holds the State of the Voip service
#[strum_discriminants(strum(message = "Voip state"))]
VoipState(ServerVoipState),
VoipState(ServerVoipState),
}

/// The types of message the server can "send"
Expand Down Expand Up @@ -2144,10 +2147,10 @@ pub enum VoipEvent
ImageDisconnected,
}


/// This struct holds all important information, when informing clients / servers about a ```VoipEvent```
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)]
pub struct ServerVoipEvent {
pub struct ServerVoipEvent
{
/// The uuid of the user who has initiated this event
pub uuid: String,
/// The event the user has initiated
Expand Down Expand Up @@ -2335,7 +2338,6 @@ impl Voip
*camera_handle = Some(Webcam::new_def_auto_detect().unwrap());
});


Ok(())
}

Expand Down Expand Up @@ -2364,7 +2366,6 @@ impl Voip

*camera_handle = None;
});

}

/// Starts a video call when this function is called.
Expand Down Expand Up @@ -3517,4 +3518,4 @@ pub fn get_image_header(
}

Ok(())
}
}
23 changes: 14 additions & 9 deletions desktop/src/app/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ impl Application

//Create audio chunks
let audio_chunks = playbackable_audio.chunks(30000);


tracing::info!("Sending uuid: {uuid}");

//Avoid sending too much data (If there is more recorded we just iterate over the chunks and not send them at once)
for chunk in audio_chunks {
voip.send_audio(uuid.clone(), chunk.to_vec(), &decryption_key).await.unwrap();
Expand Down Expand Up @@ -589,7 +591,7 @@ impl Application
}
},
}
}
},
_ => {
let message = msg.message.clone();

Expand Down Expand Up @@ -721,7 +723,7 @@ impl Application
},
Err(_err) => {
tracing::error!("{_err}");
}
},
}
},
}
Expand Down Expand Up @@ -753,19 +755,20 @@ impl Application
}
}

fn add_message(&mut self, message: super::backend::ServerOutput) {
fn add_message(&mut self, message: super::backend::ServerOutput)
{
//Allocate Message vec for the new message
self.client_ui
.incoming_messages
.reaction_list
.push(MessageReaction::default());

//We can append the missing messages sent from the server, to the self.client_ui.incoming_msg.struct_list vector
self.client_ui
.incoming_messages
.message_list
.push(message.clone());

//Callback
self.client_ui.extension.event_call_extensions(
crate::app::lua::EventCall::OnChatRecive,
Expand Down Expand Up @@ -867,8 +870,10 @@ async fn recive_server_relay(
)
.unwrap();

dbg!(&uuid);

//Make sure to verify that the UUID we are parsing is really a uuid, because if its not we know we have parsed the bytes in an incorrect order
uuid::Uuid::parse_str(uuid.trim())
uuid::Uuid::parse_str(&uuid)
.map_err(|err| anyhow::Error::msg(format!("Error: {}, in uuid {}", err, uuid)))?;

if let Some(mut image_header) = image_buffer.get_mut(&uuid) {
Expand All @@ -895,10 +900,10 @@ async fn recive_server_relay(

//Define uri
let uri = format!("bytes://video_stream:{uuid}");

//Drain earlier ImageHeaders (and the current one), because a new one has arrived
image_header.drain(index..=index);

//Its important to drop image header, so that we dont deadlock, due to me accessing the image_buffer later
drop(image_header);

Expand Down
12 changes: 6 additions & 6 deletions desktop/src/app/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,8 @@ pub fn create_client_voip_manager(
//THIS IS UNUSED AND SHOULD BE REMOVED
let _uuid_bytes = message_bytes[message_bytes.len() - UUID_BYTE_OFFSET..message_bytes.len() - IDENTIFICATOR_BYTE_OFFSET].to_vec();

let author_uuid = String::from_utf8(_uuid_bytes).unwrap();

if let Some(mut image_header) = image_buffer.get_mut(&uuid) {
if let Some((index, _, contents)) = image_header.get_full_mut(&identificator) {

Expand All @@ -479,7 +481,7 @@ pub fn create_client_voip_manager(
if contents.iter().all(|(_, value)| value.is_some()) {
let contents_clone = contents.clone();
tokio::spawn(async move {


//Combine the image part bytes
let image_bytes: Vec<u8> = contents_clone.iter().flat_map(|(_, value)| {
Expand All @@ -501,14 +503,12 @@ pub fn create_client_voip_manager(
.collect::<Vec<u8>>(),
);


for connected_client in voip_connected_clients.iter() {
let uuid = connected_client.key();
let socket_addr = connected_client.value();

//Create header message
let header_message =
ImageHeader::new(uuid.clone(), image_parts.clone(), identificator.clone());
ImageHeader::new(author_uuid.clone(), image_parts.clone(), identificator.clone());

// Send image header
send_bytes(
Expand All @@ -522,7 +522,7 @@ pub fn create_client_voip_manager(

//Send image parts
//We have already sent the image header
send_image_parts(image_parts_tuple.clone(), uuid.clone(), &key, identificator.clone(), socket.clone(), *socket_addr)
send_image_parts(image_parts_tuple.clone(), author_uuid.clone(), &key, identificator.clone(), socket.clone(), *socket_addr)
.await.unwrap();
}
});
Expand Down Expand Up @@ -1052,7 +1052,7 @@ impl MessageService
}
},
super::backend::ClientVoipRequest::ImageDisconnected => {
if let Some(voip) = &mut self.voip {
if let Some(voip) = &mut self.voip {
voip.image_buffer.remove(&req.uuid);
}
else {
Expand Down
5 changes: 3 additions & 2 deletions desktop/src/app/ui/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ impl Application
.connected_clients
{
egui::TopBottomPanel::new(egui::panel::TopBottomSide::Top, "voip_connected_users")
.resizable(true)
.show(ctx, |ui| {
let uuid = self.opened_user_information.uuid.clone();

Expand All @@ -184,13 +185,13 @@ impl Application
ui.horizontal_centered(|ui| {
if voip.enable_microphone.load(Relaxed) {
if ui.add(ImageButton::new(egui::include_image!(
"../../../icons/record_off.png"
"../../../icons/record.png"
))).clicked() {
voip.enable_microphone.store(false, Relaxed);
}
}
else if ui.add(ImageButton::new(egui::include_image!(
"../../../icons/record.png"
"../../../icons/record_off.png"
))).clicked() {
voip.enable_microphone.store(true, Relaxed);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ use std::{
f32,
io::{BufWriter, Cursor},
sync::{
atomic::{AtomicBool, AtomicI64},
atomic::{AtomicBool, AtomicI64, Ordering::Relaxed},
mpsc::{self, Receiver},
Arc, Mutex,
},
thread::JoinHandle,
time::Duration,
};
use std::sync::atomic::Ordering::Relaxed;

pub const VOIP_PACKET_BUFFER_LENGHT_MS: usize = 35;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ impl Application
);
});
},

crate::app::backend::VoipEvent::ImageConnected => unreachable!(),
crate::app::backend::VoipEvent::ImageDisconnected => unreachable!(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,9 @@ impl Application
tokio::spawn(async move {
let bytes = audio_recording_with_recv(
rx,
microphone_precentage.load(std::sync::atomic::Ordering::Relaxed) as f32,
microphone_precentage
.load(std::sync::atomic::Ordering::Relaxed)
as f32,
)
.unwrap();

Expand Down
3 changes: 2 additions & 1 deletion desktop/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ async fn main() -> eframe::Result<()>
#[cfg(debug_assertions)]
{
console_subscriber::init();

std::env::set_var("RUST_BACKTRACE", "1");
std::env::set_var("RUST_LOG", "INFO");
}

//set custom panic hook
Expand Down
2 changes: 1 addition & 1 deletion mobile/build_info.matthias_build
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024.08.17. 19:52
2024.08.17. 21:52
7 changes: 4 additions & 3 deletions mobile/src/app/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ impl Default for Application

// voip_connection_reciver: Arc::new(voip_connection_reciver),
// voip_connection_sender,

autosync_shutdown_token: CancellationToken::new(),
server_connected_clients_profile: Arc::new(DashMap::new()),
opened_user_information: UserInformation::default(),
Expand Down Expand Up @@ -828,7 +827,6 @@ pub struct Client

// #[serde(skip)]
// pub voip: Option<Voip>,

/// This entry contains the volume precentage of the microphone, this is modified in the settings
pub microphone_volume: Arc<Mutex<f32>>,
}
Expand Down Expand Up @@ -1240,7 +1238,10 @@ impl ClientMessage
.to_str()
.unwrap()
.to_string()
.split(".").next().unwrap().to_string(),
.split(".")
.next()
.unwrap()
.to_string(),
),
bytes: std::fs::read(file_path).unwrap_or_default(),
}),
Expand Down
3 changes: 0 additions & 3 deletions mobile/src/app/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use std::io::{BufReader, Cursor};

use crate::app::backend::{decrypt_aes256_bytes, MessageBuffer, UdpMessageType};


/// Sends connection request to the specified server handle, returns the server's response, this function does not create a new thread, and may block
pub async fn connect_to_server(
mut connection: TcpStream,
Expand Down Expand Up @@ -116,8 +115,6 @@ impl Application
// let camera_handle = voip_clone.camera_handle.clone();
// let voice_recording_shutdown = self.voip_video_shutdown_token.clone();



// self.voip_thread.get_or_insert_with(|| {
// let reciver_socket_part = voip.socket.clone();
// let microphone_precentage = self.client_ui.microphone_volume.clone();
Expand Down
9 changes: 6 additions & 3 deletions mobile/src/app/ui/client.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use egui::{
load::LoadError, vec2, Align, Align2, Area, Color32, FontFamily, FontId, Id, Image, Layout, Pos2, RichText, Sense, Stroke,
load::LoadError, vec2, Align, Align2, Area, Color32, FontFamily, FontId, Id, Image, Layout,
Pos2, RichText, Sense, Stroke,
};
use rodio::Decoder;

use crate::app::backend::{display_error_message, ClientMessage, ConnectionState,
use crate::app::backend::{
display_error_message,
ClientMessage,
ConnectionState,
// Voip
};

Expand Down Expand Up @@ -89,7 +93,6 @@ impl Application
self.toasts.clone(),
);
}

}
});

Expand Down
Loading

0 comments on commit e853412

Please sign in to comment.