Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

feat(cdp): add --cdp-screencast-fps option

SpringCorel0d6cee5ddede
diff --git a/README.md b/README.md
index 74bebb3e4..87e3774f4 100644
--- a/README.md
+++ b/README.md
@@ -241,6 +241,7 @@ enabled by default:
| --- | --- |
| Default | `LayoutPolicy::Mock` — deterministic geometry in a compatible format, with no real layout or paint |
| `--layout` | `LayoutPolicy::OnDemand` — real layout, geometry, hit-testing, coordinate input, screenshots, screencast |
+| `--cdp-screencast-fps <FPS>` | Set the CDP screencast rate from `1` to `60`; requires `--layout` (default: `1`) |
| `--resource` | Fetch all optional visual/media resource families |
| `--image`, `--font`, `--audio`, `--video`, `--media`, `--text-track` | Enable one specific optional resource family |
| `--profile-dir`, `--http-cache-dir`, `--cookie-file` | Selectively enable the persistence required by the workload |
diff --git a/docs/README.zh-CN.md b/docs/README.zh-CN.md
index 5cfa8a7bc..6e1cab09c 100644
--- a/docs/README.zh-CN.md
+++ b/docs/README.zh-CN.md
@@ -190,6 +190,7 @@ Moli 是 Lexmount 旗下的开源无头浏览器;Lexmount Browser 则是围绕
| --- | --- |
| 默认 | `LayoutPolicy::Mock`——返回确定性的、格式兼容的几何信息,不执行真实的布局或绘制 |
| `--layout` | `LayoutPolicy::OnDemand`——提供真实的布局、几何信息、命中测试、坐标输入、截图和屏幕串流 |
+| `--cdp-screencast-fps <FPS>` | 设置 `1` 到 `60` 的 CDP 屏幕串流帧率;需要同时启用 `--layout`(默认:`1`) |
| `--resource` | 拉取所有可选的视觉/媒体资源类别 |
| `--image`、`--font`、`--audio`、`--video`、`--media`、`--text-track` | 单独启用某一类可选资源 |
| `--profile-dir`、`--http-cache-dir`、`--cookie-file` | 按工作负载需要,选择性开启持久化能力 |
diff --git a/moli-protocol-server/src/cdp_scheduler.rs b/moli-protocol-server/src/cdp_scheduler.rs
index 7bdccc1ce..3215172a1 100644
--- a/moli-protocol-server/src/cdp_scheduler.rs
+++ b/moli-protocol-server/src/cdp_scheduler.rs
@@ -114,6 +114,7 @@ pub(crate) struct CdpScheduler {
runtime_command_output_barriers: RuntimeCommandOutputBarriers,
queues: SchedulerQueues,
page_screencasts: HashMap<Option<String>, PageScreencastSchedule>,
+ page_screencast_fps: u8,
}
#[derive(Clone, Copy)]
@@ -153,9 +154,10 @@ pub(crate) struct ScheduledPageScreencastFrame {
visual_state: RendererVisualStateToken,
}
-fn page_screencast_interval(every_nth_frame: u32) -> Duration {
+fn page_screencast_interval(fps: u8, every_nth_frame: u32) -> Duration {
+ debug_assert!((1..=60).contains(&fps));
debug_assert!(every_nth_frame > 0);
- Duration::from_secs(u64::from(every_nth_frame))
+ Duration::from_secs_f64(f64::from(every_nth_frame) / f64::from(fps))
}
fn next_page_screencast_deadline(now: TokioInstant, interval: Duration) -> TokioInstant {
@@ -501,7 +503,8 @@ impl CdpScheduler {
now: TokioInstant,
) {
let session_id = registration.session_id().map(str::to_owned);
- let interval = page_screencast_interval(registration.every_nth_frame());
+ let interval =
+ page_screencast_interval(self.page_screencast_fps, registration.every_nth_frame());
self.page_screencasts.insert(
session_id,
PageScreencastSchedule {
@@ -693,16 +696,22 @@ impl CdpScheduler {
output
}
- fn new(conn: CdpConnection) -> Self {
+ fn new_with_screencast_fps(conn: CdpConnection, page_screencast_fps: u8) -> Self {
Self {
conn,
pending_navigation_background_events: VecDeque::new(),
runtime_command_output_barriers: RuntimeCommandOutputBarriers::default(),
queues: SchedulerQueues::default(),
page_screencasts: HashMap::new(),
+ page_screencast_fps,
}
}
+ #[cfg(test)]
+ fn new(conn: CdpConnection) -> Self {
+ Self::new_with_screencast_fps(conn, 1)
+ }
+
pub(crate) fn new_with_initial_state_runtime_config(
initial_storage_partition: CdpInitialStoragePartition,
navigation_runtime_config: NavigationRuntimeConfig,
@@ -724,19 +733,22 @@ impl CdpScheduler {
navigation_runtime_config,
target_host_integration,
DefaultTargetRuntimeInitialization::Materialized,
+ 1,
)
}
- pub(crate) fn new_with_deferred_default_target_runtime(
+ pub(crate) fn new_with_deferred_default_target_runtime_and_screencast_fps(
initial_storage_partition: CdpInitialStoragePartition,
navigation_runtime_config: NavigationRuntimeConfig,
target_host_integration: Option<CdpTargetHostIntegration>,
+ page_screencast_fps: u8,
) -> (Self, CdpSchedulerEventReceivers) {
Self::new_with_default_target_runtime_initialization(
initial_storage_partition,
navigation_runtime_config,
target_host_integration,
DefaultTargetRuntimeInitialization::Deferred,
+ page_screencast_fps,
)
}
@@ -745,6 +757,7 @@ impl CdpScheduler {
navigation_runtime_config: NavigationRuntimeConfig,
target_host_integration: Option<CdpTargetHostIntegration>,
initialization: DefaultTargetRuntimeInitialization,
+ page_screencast_fps: u8,
) -> (Self, CdpSchedulerEventReceivers) {
let conn = match initialization {
DefaultTargetRuntimeInitialization::Materialized => {
@@ -760,7 +773,7 @@ impl CdpScheduler {
)
}
};
- let mut scheduler = Self::new(conn);
+ let mut scheduler = Self::new_with_screencast_fps(conn, page_screencast_fps);
if let Some(target_host_integration) = target_host_integration {
target_host_integration.install(&mut scheduler.conn);
}
diff --git a/moli-protocol-server/src/cdp_scheduler/tests.rs b/moli-protocol-server/src/cdp_scheduler/tests.rs
index b372b749f..f3261459c 100644
--- a/moli-protocol-server/src/cdp_scheduler/tests.rs
+++ b/moli-protocol-server/src/cdp_scheduler/tests.rs
@@ -40,23 +40,31 @@ use super::{
};
#[test]
-fn screencast_deadlines_are_one_hz_downsampled_without_catch_up() {
+fn screencast_deadlines_respect_configured_fps_without_catch_up() {
let started = tokio::time::Instant::now();
assert_eq!(
- page_screencast_interval(1),
+ page_screencast_interval(1, 1),
std::time::Duration::from_secs(1)
);
assert_eq!(
- page_screencast_interval(3),
+ page_screencast_interval(1, 3),
std::time::Duration::from_secs(3)
);
let completion = started + std::time::Duration::from_secs(10);
assert_eq!(
- next_page_screencast_deadline(completion, page_screencast_interval(1)),
+ next_page_screencast_deadline(completion, page_screencast_interval(1, 1)),
started + std::time::Duration::from_secs(11),
"an overdue capture schedules from completion instead of catching up"
);
+ assert_eq!(
+ page_screencast_interval(20, 1),
+ std::time::Duration::from_millis(50)
+ );
+ assert_eq!(
+ page_screencast_interval(20, 3),
+ std::time::Duration::from_millis(150)
+ );
}
#[test]
diff --git a/moli-protocol-server/src/config.rs b/moli-protocol-server/src/config.rs
index 455829e2c..ec46aaf60 100644
--- a/moli-protocol-server/src/config.rs
+++ b/moli-protocol-server/src/config.rs
@@ -4,6 +4,7 @@ pub struct ServerConfig {
pub host: String,
pub port: u16,
pub timeout_secs: u32,
+ pub cdp_screencast_fps: u8,
}
impl ServerConfig {
@@ -18,6 +19,7 @@ impl Default for ServerConfig {
host: "127.0.0.1".to_owned(),
port: 9222,
timeout_secs: 10,
+ cdp_screencast_fps: 1,
}
}
}
diff --git a/moli-protocol-server/src/protocol_server/cdp_owner.rs b/moli-protocol-server/src/protocol_server/cdp_owner.rs
index a8a438846..d5e71fdfd 100644
--- a/moli-protocol-server/src/protocol_server/cdp_owner.rs
+++ b/moli-protocol-server/src/protocol_server/cdp_owner.rs
@@ -57,6 +57,7 @@ struct CdpOwnerRuntimeConfig {
cookie_profile: SharedCookieProfile,
storage_partition: Arc<StoragePartitionState>,
navigation_runtime_config: NavigationRuntimeConfig,
+ cdp_screencast_fps: u8,
}
impl SharedCdpOwnerRegistry {
@@ -67,6 +68,7 @@ impl SharedCdpOwnerRegistry {
cookie_profile: SharedCookieProfile,
storage_partition: Arc<StoragePartitionState>,
navigation_runtime_config: NavigationRuntimeConfig,
+ cdp_screencast_fps: u8,
) -> Self {
Self {
inner: Arc::new(CdpOwnerRegistryInner {
@@ -77,6 +79,7 @@ impl SharedCdpOwnerRegistry {
cookie_profile,
storage_partition,
navigation_runtime_config,
+ cdp_screencast_fps,
},
state: Mutex::new(CdpOwnerRegistryState::default()),
shared_owner: Mutex::new(None),
@@ -133,6 +136,7 @@ impl SharedCdpOwnerRegistry {
receivers,
initial_storage_partition,
self.inner.config.navigation_runtime_config.clone(),
+ self.inner.config.cdp_screencast_fps,
Some(CdpTargetHostIntegration::new(
self.inner.config.target_id_allocator.clone(),
self.inner.config.tab_target_id_allocator.clone(),
@@ -200,16 +204,18 @@ fn spawn_owner_task(
receivers: CdpFrontendReceivers,
initial_storage_partition: CdpInitialStoragePartition,
navigation_runtime_config: NavigationRuntimeConfig,
+ cdp_screencast_fps: u8,
target_host_integration: Option<CdpTargetHostIntegration>,
owner_lifecycle: Option<CdpOwnerActorLifecycle>,
checkpoint_tx: mpsc::UnboundedSender<CdpCookieSnapshot>,
) -> oneshot::Receiver<()> {
spawn_protocol_local_task("cdp-owner", move || async move {
let (scheduler, scheduler_receivers) =
- CdpScheduler::new_with_deferred_default_target_runtime(
+ CdpScheduler::new_with_deferred_default_target_runtime_and_screencast_fps(
initial_storage_partition,
navigation_runtime_config,
target_host_integration,
+ cdp_screencast_fps,
);
let actor = spawn_cdp_scheduler_actor(
scheduler,
diff --git a/moli-protocol-server/src/protocol_server/mod.rs b/moli-protocol-server/src/protocol_server/mod.rs
index bf93f07c9..cf460ecf9 100644
--- a/moli-protocol-server/src/protocol_server/mod.rs
+++ b/moli-protocol-server/src/protocol_server/mod.rs
@@ -155,6 +155,7 @@ impl ProtocolServer {
addr,
self.storage_partition.clone(),
self.navigation_runtime_config.clone(),
+ self.config.cdp_screencast_fps,
)?;
let cdp_owner_registry = app_state.cdp_owner_registry.clone();
@@ -770,6 +771,7 @@ impl AppState {
subframe_loading_enabled,
LayoutPolicy::default(),
),
+ 1,
)
}
@@ -777,12 +779,14 @@ impl AppState {
addr: SocketAddr,
storage_partition: Arc<StoragePartitionState>,
navigation_runtime_config: NavigationRuntimeConfig,
+ cdp_screencast_fps: u8,
) -> anyhow::Result<Self> {
Ok(Self::from_parts(
addr,
SharedCookieProfile::from_storage_partition(storage_partition.clone()),
storage_partition,
navigation_runtime_config,
+ cdp_screencast_fps,
))
}
@@ -791,6 +795,7 @@ impl AppState {
cookie_profile: SharedCookieProfile,
storage_partition: Arc<StoragePartitionState>,
navigation_runtime_config: NavigationRuntimeConfig,
+ cdp_screencast_fps: u8,
) -> Self {
let cdp_agent_host_directory = SharedCdpAgentHostDirectory::default();
let cdp_target_id_allocator = Arc::new(AtomicU64::new(0));
@@ -802,6 +807,7 @@ impl AppState {
cookie_profile.clone(),
storage_partition.clone(),
navigation_runtime_config.clone(),
+ cdp_screencast_fps,
);
Self {
browser_ws_url: format!("ws://{addr}/devtools/browser/{DEFAULT_BROWSER_ID}"),
diff --git a/moli-protocol-server/src/protocol_server/tests/mod.rs b/moli-protocol-server/src/protocol_server/tests/mod.rs
index b09f9ded5..77b6b17dd 100644
--- a/moli-protocol-server/src/protocol_server/tests/mod.rs
+++ b/moli-protocol-server/src/protocol_server/tests/mod.rs
@@ -275,6 +275,7 @@ fn protocol_server_test_state(
protocol_server_test_fetch_config(fetch_config),
optional_resource_fetch_mask,
),
+ 1,
)
.expect("test app state should initialize")
}
@@ -435,6 +436,7 @@ async fn spawn_test_protocol_server_with_runtime_config(
addr,
storage_partition,
navigation_runtime_config,
+ 1,
)
.expect("test app state should initialize");
let server = tokio::spawn(async move {
diff --git a/moli/src/cli.rs b/moli/src/cli.rs
index 97d3ceabb..8ef3667d9 100644
--- a/moli/src/cli.rs
+++ b/moli/src/cli.rs
@@ -443,6 +443,15 @@ pub struct ServeArgs {
#[arg(short, long, default_value_t = 10)]
pub timeout: u32,
+ /// Set the DevTools screencast frame rate. Only valid with --layout.
+ #[arg(
+ long,
+ value_name = "FPS",
+ value_parser = clap::value_parser!(u8).range(1..=60),
+ requires = "layout"
+ )]
+ pub cdp_screencast_fps: Option<u8>,
+
#[command(flatten)]
pub common: CommonArgs,
}
diff --git a/moli/src/config.rs b/moli/src/config.rs
index e9e6f659d..ec459dd90 100644
--- a/moli/src/config.rs
+++ b/moli/src/config.rs
@@ -63,6 +63,9 @@ impl AppConfig {
config.server.host = args.host.clone();
config.server.port = args.port;
config.server.timeout_secs = args.timeout;
+ if let Some(fps) = args.cdp_screencast_fps {
+ config.server.cdp_screencast_fps = fps;
+ }
}
Commands::Import(_) => {}
}
diff --git a/moli/tests/cli.rs b/moli/tests/cli.rs
index c0203a46f..f04595aae 100644
--- a/moli/tests/cli.rs
+++ b/moli/tests/cli.rs
@@ -614,6 +614,7 @@ fn infers_serve_mode_when_called_without_args() {
host: "127.0.0.1".to_owned(),
port: 9222,
timeout: 10,
+ cdp_screencast_fps: None,
common: CommonArgs::default(),
}))
);
@@ -639,6 +640,7 @@ fn parses_serve_flags_with_explicit_command() {
host: "0.0.0.0".to_owned(),
port: 9333,
timeout: 42,
+ cdp_screencast_fps: None,
common: CommonArgs::default(),
}))
);
@@ -1568,6 +1570,30 @@ fn layout_selects_on_demand_policy_for_fetch_and_serve() {
}
}
+#[test]
+fn cdp_screencast_fps_requires_layout_and_accepts_one_through_sixty() {
+ for fps in ["1", "30", "60"] {
+ let cli = Cli::try_parse_from(normalize_args_for_compat([
+ "moli",
+ "serve",
+ "--layout",
+ "--cdp-screencast-fps",
+ fps,
+ ]))
+ .unwrap();
+ let config = AppConfig::from_cli(&cli).unwrap();
+ assert_eq!(config.server.cdp_screencast_fps, fps.parse::<u8>().unwrap());
+ }
+
+ for args in [
+ vec!["moli", "serve", "--cdp-screencast-fps", "30"],
+ vec!["moli", "serve", "--layout", "--cdp-screencast-fps", "0"],
+ vec!["moli", "serve", "--layout", "--cdp-screencast-fps", "61"],
+ ] {
+ assert!(Cli::try_parse_from(normalize_args_for_compat(args)).is_err());
+ }
+}
+
#[test]
fn env_flags_are_fallbacks_and_cli_flags_take_priority() {
for (case, env_value, expected) in [