boardcast + interval tick.
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
use std::{sync::Arc, time::Duration};
|
||||
use std::{collections::HashMap, sync::Arc, time::Duration};
|
||||
|
||||
use paris::{info, warn};
|
||||
use tauri::async_runtime::{Mutex, RwLock};
|
||||
@ -6,9 +6,10 @@ use tokio::{sync::watch, time::sleep};
|
||||
|
||||
use crate::{
|
||||
ambient_light::{config, ConfigManager},
|
||||
led_color::LedColor,
|
||||
rpc::MqttRpc,
|
||||
screenshot::Screenshot,
|
||||
screenshot_manager::ScreenshotManager, led_color::LedColor,
|
||||
screenshot::{self, Screenshot},
|
||||
screenshot_manager::ScreenshotManager,
|
||||
};
|
||||
|
||||
use itertools::Itertools;
|
||||
@ -57,67 +58,76 @@ impl LedColorsPublisher {
|
||||
let configs = config_receiver.borrow().clone();
|
||||
let configs = Self::get_colors_configs(&configs).await;
|
||||
|
||||
let mut some_screenshot_receiver_is_none = false;
|
||||
if let Err(err) = configs {
|
||||
warn!("Failed to get configs: {}", err);
|
||||
sleep(Duration::from_millis(100)).await;
|
||||
continue;
|
||||
}
|
||||
|
||||
let configs = configs.unwrap();
|
||||
|
||||
let mut merged_screenshot_receiver =
|
||||
screenshot_manager.clone_merged_screenshot_rx().await;
|
||||
|
||||
let mut screenshots = HashMap::new();
|
||||
|
||||
loop {
|
||||
let mut screenshots = Vec::new();
|
||||
let screenshot = merged_screenshot_receiver.recv().await;
|
||||
|
||||
for rx in configs.screenshot_receivers.to_owned() {
|
||||
let mut rx = rx.lock_owned().await;
|
||||
if rx.is_none() {
|
||||
some_screenshot_receiver_is_none = true;
|
||||
warn!("screenshot receiver is none");
|
||||
continue;
|
||||
}
|
||||
|
||||
let rx = rx.as_mut().unwrap();
|
||||
|
||||
if let Err(err) = rx.changed().await {
|
||||
warn!("rx changed error: {}", err);
|
||||
continue;
|
||||
}
|
||||
// log::info!("screenshot updated");
|
||||
|
||||
let screenshot = rx.borrow().clone();
|
||||
|
||||
screenshots.push(screenshot);
|
||||
}
|
||||
|
||||
let colors = screenshot_manager
|
||||
.get_all_colors(&configs.sample_point_groups, &screenshots)
|
||||
.await;
|
||||
|
||||
let sorted_colors =
|
||||
ScreenshotManager::get_sorted_colors(&colors, &configs.mappers).await;
|
||||
|
||||
match colors_tx.send(colors) {
|
||||
Ok(_) => {
|
||||
// log::info!("colors updated");
|
||||
}
|
||||
Err(_) => {
|
||||
warn!("colors update failed");
|
||||
if let Err(err) = screenshot {
|
||||
match err {
|
||||
tokio::sync::broadcast::error::RecvError::Closed => {
|
||||
warn!("closed");
|
||||
continue;
|
||||
},
|
||||
tokio::sync::broadcast::error::RecvError::Lagged(_) => {
|
||||
warn!("lagged");
|
||||
continue;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
match sorted_colors_tx.send(sorted_colors) {
|
||||
Ok(_) => {
|
||||
// log::info!("colors updated");
|
||||
}
|
||||
Err(_) => {
|
||||
warn!("colors update failed");
|
||||
}
|
||||
}
|
||||
let screenshot = screenshot.unwrap();
|
||||
// log::info!("got screenshot: {:?}", screenshot.display_id);
|
||||
|
||||
if some_screenshot_receiver_is_none {
|
||||
info!("some screenshot receiver is none. reload.");
|
||||
sleep(Duration::from_millis(1000)).await;
|
||||
break;
|
||||
}
|
||||
screenshots.insert(screenshot.display_id, screenshot);
|
||||
|
||||
if config_receiver.has_changed().unwrap_or(true) {
|
||||
info!("config changed. reload.");
|
||||
sleep(Duration::from_millis(100)).await;
|
||||
break;
|
||||
if screenshots.len() == configs.sample_point_groups.len() {
|
||||
{
|
||||
let screenshots = configs
|
||||
.sample_point_groups
|
||||
.iter()
|
||||
.map(|strip| screenshots.get(&strip.display_id).unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let colors = screenshot_manager
|
||||
.get_all_colors(&configs.sample_point_groups, &screenshots)
|
||||
.await;
|
||||
|
||||
let sorted_colors =
|
||||
ScreenshotManager::get_sorted_colors(&colors, &configs.mappers)
|
||||
.await;
|
||||
|
||||
match colors_tx.send(colors) {
|
||||
Ok(_) => {
|
||||
// log::info!("colors updated");
|
||||
}
|
||||
Err(_) => {
|
||||
warn!("colors update failed");
|
||||
}
|
||||
}
|
||||
|
||||
match sorted_colors_tx.send(sorted_colors) {
|
||||
Ok(_) => {
|
||||
// log::info!("colors updated");
|
||||
}
|
||||
Err(_) => {
|
||||
warn!("colors update failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
screenshots.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -158,8 +168,9 @@ impl LedColorsPublisher {
|
||||
pub async fn clone_sorted_colors_receiver(&self) -> watch::Receiver<Vec<u8>> {
|
||||
self.sorted_colors_rx.read().await.clone()
|
||||
}
|
||||
|
||||
pub async fn get_colors_configs(configs: &LedStripConfigGroup) -> AllColorConfig {
|
||||
pub async fn get_colors_configs(
|
||||
configs: &LedStripConfigGroup,
|
||||
) -> anyhow::Result<AllColorConfig> {
|
||||
let screenshot_manager = ScreenshotManager::global().await;
|
||||
|
||||
let channels = screenshot_manager.channels.read().await;
|
||||
@ -180,11 +191,13 @@ impl LedColorsPublisher {
|
||||
let display_id = *display_id;
|
||||
|
||||
let channel = channels.get(&display_id);
|
||||
let channel = match channel {
|
||||
Some(channel) => Some(channel.clone()),
|
||||
None => None,
|
||||
};
|
||||
local_rx_list.push(Arc::new(Mutex::new(channel.clone())));
|
||||
if channel.is_none() {
|
||||
anyhow::bail!("no channel for display_id: {}", display_id);
|
||||
}
|
||||
|
||||
let channel_rx = channel.unwrap().clone();
|
||||
|
||||
local_rx_list.push(channel.unwrap().clone());
|
||||
|
||||
let led_strip_configs: Vec<_> = configs
|
||||
.strips
|
||||
@ -192,39 +205,31 @@ impl LedColorsPublisher {
|
||||
.filter(|c| c.display_id == display_id)
|
||||
.collect();
|
||||
|
||||
let rx = channel;
|
||||
if rx.is_none() {
|
||||
warn!("no channel for display_id: {}", display_id);
|
||||
continue;
|
||||
}
|
||||
|
||||
if led_strip_configs.len() == 0 {
|
||||
warn!("no led strip config for display_id: {}", display_id);
|
||||
continue;
|
||||
}
|
||||
let mut rx = rx.unwrap().to_owned();
|
||||
let rx = channel_rx.to_owned();
|
||||
|
||||
if rx.changed().await.is_ok() {
|
||||
let screenshot = rx.borrow().clone();
|
||||
log::info!("screenshot updated: {:?}", display_id);
|
||||
let screenshot = rx.borrow().clone();
|
||||
log::debug!("screenshot updated: {:?}", display_id);
|
||||
|
||||
let points: Vec<_> = led_strip_configs
|
||||
.iter()
|
||||
.map(|config| screenshot.get_sample_points(&config))
|
||||
.flatten()
|
||||
.collect();
|
||||
let points: Vec<_> = led_strip_configs
|
||||
.iter()
|
||||
.map(|config| screenshot.get_sample_points(&config))
|
||||
.flatten()
|
||||
.collect();
|
||||
|
||||
let colors_config = config::SamplePointConfig { display_id, points };
|
||||
let colors_config = config::SamplePointConfig { display_id, points };
|
||||
|
||||
colors_configs.push(colors_config);
|
||||
}
|
||||
colors_configs.push(colors_config);
|
||||
}
|
||||
|
||||
return AllColorConfig {
|
||||
return Ok(AllColorConfig {
|
||||
sample_point_groups: colors_configs,
|
||||
mappers,
|
||||
screenshot_receivers: local_rx_list,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
pub async fn clone_colors_receiver(&self) -> watch::Receiver<Vec<LedColor>> {
|
||||
@ -232,8 +237,9 @@ impl LedColorsPublisher {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct AllColorConfig {
|
||||
pub sample_point_groups: Vec<SamplePointConfig>,
|
||||
pub mappers: Vec<config::SamplePointMapper>,
|
||||
pub screenshot_receivers: Vec<Arc<Mutex<Option<watch::Receiver<Screenshot>>>>>,
|
||||
pub screenshot_receivers: Vec<watch::Receiver<Screenshot>>,
|
||||
}
|
||||
|
@ -57,21 +57,6 @@ fn list_display_info() -> Result<String, String> {
|
||||
Ok(json_str)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn subscribe_encoded_screenshot_updated(
|
||||
window: tauri::Window,
|
||||
display_id: u32,
|
||||
) -> Result<(), String> {
|
||||
let screenshot_manager = ScreenshotManager::global().await;
|
||||
screenshot_manager
|
||||
.subscribe_encoded_screenshot_updated(window, display_id)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
error!("subscribe_encoded_screenshot_updated: {}", err);
|
||||
err.to_string()
|
||||
})
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn read_led_strip_configs() -> Result<LedStripConfigGroup, String> {
|
||||
let config = ambient_light::LedStripConfigGroup::read_config()
|
||||
@ -201,7 +186,6 @@ async fn main() {
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
greet,
|
||||
list_display_info,
|
||||
subscribe_encoded_screenshot_updated,
|
||||
read_led_strip_configs,
|
||||
write_led_strip_configs,
|
||||
get_led_strips_sample_points,
|
||||
|
@ -3,14 +3,15 @@ use std::{collections::HashMap, sync::Arc};
|
||||
use core_graphics::display::{
|
||||
kCGNullWindowID, kCGWindowImageDefault, kCGWindowListOptionOnScreenOnly, CGDisplay,
|
||||
};
|
||||
use paris::{error, info, warn};
|
||||
use tauri::{async_runtime::RwLock, Window};
|
||||
use tokio::sync::{watch, OnceCell};
|
||||
use paris::warn;
|
||||
use tauri::async_runtime::RwLock;
|
||||
use tokio::sync::{broadcast, watch, OnceCell};
|
||||
use tokio::time::{self, Duration};
|
||||
|
||||
use crate::{
|
||||
ambient_light::{SamplePointConfig, SamplePointMapper},
|
||||
led_color::LedColor,
|
||||
screenshot::{ScreenSamplePoints, Screenshot, ScreenshotPayload},
|
||||
screenshot::{ScreenSamplePoints, Screenshot},
|
||||
};
|
||||
|
||||
pub fn take_screenshot(display_id: u32, scale_factor: f32) -> anyhow::Result<Screenshot> {
|
||||
@ -54,7 +55,8 @@ pub fn take_screenshot(display_id: u32, scale_factor: f32) -> anyhow::Result<Scr
|
||||
|
||||
pub struct ScreenshotManager {
|
||||
pub channels: Arc<RwLock<HashMap<u32, watch::Receiver<Screenshot>>>>,
|
||||
encode_listeners: Arc<RwLock<HashMap<u32, Vec<Window>>>>,
|
||||
merged_screenshot_rx: Arc<RwLock<broadcast::Receiver<Screenshot>>>,
|
||||
merged_screenshot_tx: Arc<RwLock<broadcast::Sender<Screenshot>>>,
|
||||
}
|
||||
|
||||
impl ScreenshotManager {
|
||||
@ -64,10 +66,11 @@ impl ScreenshotManager {
|
||||
SCREENSHOT_MANAGER
|
||||
.get_or_init(|| async {
|
||||
let channels = Arc::new(RwLock::new(HashMap::new()));
|
||||
let encode_listeners = Arc::new(RwLock::new(HashMap::new()));
|
||||
let (merged_screenshot_tx, merged_screenshot_rx) = broadcast::channel(2);
|
||||
Self {
|
||||
channels,
|
||||
encode_listeners,
|
||||
merged_screenshot_rx: Arc::new(RwLock::new(merged_screenshot_rx)),
|
||||
merged_screenshot_tx: Arc::new(RwLock::new(merged_screenshot_tx)),
|
||||
}
|
||||
})
|
||||
.await
|
||||
@ -83,6 +86,7 @@ impl ScreenshotManager {
|
||||
|
||||
fn start_one(&self, display_id: u32, scale_factor: f32) -> anyhow::Result<()> {
|
||||
let channels = self.channels.to_owned();
|
||||
let merged_screenshot_tx = self.merged_screenshot_tx.clone();
|
||||
tokio::spawn(async move {
|
||||
let screenshot = take_screenshot(display_id, scale_factor);
|
||||
|
||||
@ -90,126 +94,45 @@ impl ScreenshotManager {
|
||||
warn!("take_screenshot_loop: {}", screenshot.err().unwrap());
|
||||
return;
|
||||
}
|
||||
let mut interval = time::interval(Duration::from_millis(33));
|
||||
|
||||
let screenshot = screenshot.unwrap();
|
||||
let (tx, rx) = watch::channel(screenshot);
|
||||
let (screenshot_tx, screenshot_rx) = watch::channel(screenshot);
|
||||
{
|
||||
let channels = channels.clone();
|
||||
let mut channels = channels.write().await;
|
||||
channels.insert(display_id, rx);
|
||||
channels.insert(display_id, screenshot_rx.clone());
|
||||
}
|
||||
|
||||
let merged_screenshot_tx = merged_screenshot_tx.read().await.clone();
|
||||
|
||||
loop {
|
||||
Self::take_screenshot_loop(display_id, scale_factor, &tx).await;
|
||||
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
|
||||
// interval.tick().await;
|
||||
Self::take_screenshot_loop(
|
||||
display_id,
|
||||
scale_factor,
|
||||
&screenshot_tx,
|
||||
&merged_screenshot_tx,
|
||||
)
|
||||
.await;
|
||||
tokio::time::sleep(Duration::from_millis(20)).await;
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn subscribe_encoded_screenshot_updated(
|
||||
&self,
|
||||
window: Window,
|
||||
display_id: u32,
|
||||
) -> anyhow::Result<()> {
|
||||
let channels = self.channels.to_owned();
|
||||
let encode_listeners = self.encode_listeners.to_owned();
|
||||
// log::info!("subscribe_encoded_screenshot_updated. {}", display_id);
|
||||
|
||||
{
|
||||
let encode_listeners = encode_listeners.read().await;
|
||||
let listening_windows = encode_listeners.get(&display_id);
|
||||
if listening_windows.is_some() && listening_windows.unwrap().contains(&window) {
|
||||
log::debug!("subscribe_encoded_screenshot_updated: already listening. display#{}, window#{}", display_id, window.label());
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
{
|
||||
encode_listeners
|
||||
.write()
|
||||
.await
|
||||
.entry(display_id)
|
||||
.or_default()
|
||||
.push(window);
|
||||
}
|
||||
|
||||
tokio::spawn(async move {
|
||||
info!("subscribe_encoded_screenshot_updated: start");
|
||||
let channels = channels.read().await;
|
||||
let rx = channels.get(&display_id);
|
||||
if rx.is_none() {
|
||||
error!(
|
||||
"subscribe_encoded_screenshot_updated: can not find display_id {}",
|
||||
display_id
|
||||
);
|
||||
return;
|
||||
}
|
||||
let mut rx = rx.unwrap().clone();
|
||||
loop {
|
||||
if let Err(err) = rx.changed().await {
|
||||
error!(
|
||||
"subscribe_encoded_screenshot_updated: can not wait rx {}",
|
||||
err
|
||||
);
|
||||
break;
|
||||
}
|
||||
let encode_listeners = encode_listeners.read().await;
|
||||
let windows = encode_listeners.get(&display_id);
|
||||
if windows.is_none() || windows.unwrap().is_empty() {
|
||||
info!("subscribe_encoded_screenshot_updated: no listener, stop");
|
||||
break;
|
||||
}
|
||||
let screenshot = rx.borrow().clone();
|
||||
// let base64_image = Self::encode_screenshot_to_base64(&screenshot).await;
|
||||
let height = screenshot.height;
|
||||
let width = screenshot.width;
|
||||
|
||||
// if base64_image.is_err() {
|
||||
// error!(
|
||||
// "subscribe_encoded_screenshot_updated: encode_screenshot_to_base64 error {}",
|
||||
// base64_image.err().unwrap()
|
||||
// );
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// let base64_image = base64_image.unwrap();
|
||||
for window in windows.unwrap().into_iter() {
|
||||
// let base64_image = base64_image.clone();
|
||||
let payload = ScreenshotPayload {
|
||||
display_id,
|
||||
// base64_image,
|
||||
height,
|
||||
width,
|
||||
};
|
||||
if let Err(err) = window.emit("encoded-screenshot-updated", payload) {
|
||||
error!("subscribe_encoded_screenshot_updated: emit error {}", err)
|
||||
} else {
|
||||
log::debug!(
|
||||
"subscribe_encoded_screenshot_updated: emit success. display#{}",
|
||||
display_id
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn unsubscribe_encoded_screenshot_updated(&self, display_id: u32) -> anyhow::Result<()> {
|
||||
let channels = self.channels.to_owned();
|
||||
let mut channels = channels.write().await;
|
||||
channels.remove(&display_id);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn take_screenshot_loop(
|
||||
display_id: u32,
|
||||
scale_factor: f32,
|
||||
tx: &watch::Sender<Screenshot>,
|
||||
screenshot_tx: &watch::Sender<Screenshot>,
|
||||
merged_screenshot_tx: &broadcast::Sender<Screenshot>,
|
||||
) {
|
||||
let screenshot = take_screenshot(display_id, scale_factor);
|
||||
if let Ok(screenshot) = screenshot {
|
||||
tx.send(screenshot).unwrap();
|
||||
// log::info!("take_screenshot_loop: send success. display#{}", display_id)
|
||||
screenshot_tx.send(screenshot.clone()).unwrap();
|
||||
merged_screenshot_tx.send(screenshot).unwrap();
|
||||
log::debug!("take_screenshot_loop: send success. display#{}", display_id)
|
||||
} else {
|
||||
warn!("take_screenshot_loop: {}", screenshot.err().unwrap());
|
||||
}
|
||||
@ -218,7 +141,7 @@ impl ScreenshotManager {
|
||||
pub async fn get_all_colors(
|
||||
&self,
|
||||
configs: &Vec<SamplePointConfig>,
|
||||
screenshots: &Vec<Screenshot>,
|
||||
screenshots: &Vec<&Screenshot>,
|
||||
) -> Vec<LedColor> {
|
||||
let mut all_colors = vec![];
|
||||
|
||||
@ -287,4 +210,8 @@ impl ScreenshotManager {
|
||||
});
|
||||
global_colors
|
||||
}
|
||||
|
||||
pub async fn clone_merged_screenshot_rx(&self) -> broadcast::Receiver<Screenshot> {
|
||||
self.merged_screenshot_tx.read().await.subscribe()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user