cdp

package
v1.42.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package cdp provides per-session Chrome DevTools Protocol (CDP) browser streaming. Each session owns a dedicated CDP port that Chrome listens on; the manager connects to Chrome via its CDP WebSocket endpoint and subscribes to Page.screencastFrame events to deliver a live JPEG frame stream.

On Linux, Xvfb still provides the virtual display; on macOS, Chrome runs on the real display. CDP streaming works on both platforms, making it the cross-platform complement to the Linux-only VNC implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CDPConfig

type CDPConfig struct {
	// SessionID is the identifier of the owning session; used to create
	// per-session temporary directories for wrapper scripts.
	SessionID string
	// ChromePath is the resolved real Chrome binary path (not the wrapper script).
	// When empty, New() returns a noopCDPManager.
	ChromePath string

	// ScreencastQuality is the JPEG compression quality (1–100). Default: 70.
	ScreencastQuality int
	// ScreencastMaxWidth is the maximum frame width in pixels. Default: 1280.
	ScreencastMaxWidth int
	// ScreencastMaxHeight is the maximum frame height in pixels. Default: 800.
	ScreencastMaxHeight int
	// ScreencastMaxFPS is a hint for the frame-rate cap. Default: 15.
	// Currently influences the everyNthFrame parameter sent to Chrome.
	ScreencastMaxFPS int
}

CDPConfig holds configuration for the CDP stream manager. Populated from config.BrowserPassthroughConfig and passed to New().

type CDPState

type CDPState struct {
	// Status is the current operational status.
	Status CDPStatus
	// Port is the allocated CDP TCP port on localhost, or 0 if unavailable.
	Port int
}

CDPState is the Go-native (non-proto) runtime state of the CDP subsystem. It is periodically mapped to the proto CDPState by the session service layer.

type CDPStatus

type CDPStatus int

CDPStatus represents the operational state of the CDP subsystem for a session.

const (
	// CDPStatusUnspecified is the zero value; not yet initialized.
	CDPStatusUnspecified CDPStatus = iota
	// CDPStatusWaiting means polling for Chrome on the allocated CDP port.
	CDPStatusWaiting
	// CDPStatusStreaming means connected to Chrome and receiving screencast frames.
	CDPStatusStreaming
	// CDPStatusNoBrowser means the CDP port is allocated but Chrome has not yet
	// been detected (port allocated, Chrome not yet launched or not yet ready).
	CDPStatusNoBrowser
	// CDPStatusUnavailable means CDP is not available for this session — either
	// no Chrome binary was found, or startup failed.
	CDPStatusUnavailable
)

func (CDPStatus) String

func (s CDPStatus) String() string

String returns a human-readable name for the CDPStatus.

type CDPStreamManager

type CDPStreamManager interface {
	// Allocate reserves a free TCP port and writes Chrome wrapper scripts.
	// Must be called before the tmux session is created so CDP_PORT can be
	// injected into the session environment via ExtraEnv.
	Allocate() error

	// Start begins polling for Chrome on the allocated CDP port and subscribes
	// to screencast frames. Non-blocking: starts a goroutine. Should be called
	// after the tmux session is started so Chrome has already been launched.
	Start(ctx context.Context) error

	// Stop cancels the polling/streaming goroutines, closes the CDP WebSocket,
	// and removes the temporary wrapper-script directory.
	Stop()

	// State returns a snapshot of the current CDP state.
	State() CDPState

	// Port returns the allocated CDP TCP port, or 0 if unavailable.
	Port() int

	// WrapperDir returns the path to the directory containing the Chrome wrapper
	// scripts. Returns "" before Allocate() or when CDP is unavailable.
	WrapperDir() string

	// LatestFrame returns the most recent JPEG frame bytes, or nil if no frame
	// has been received yet.
	LatestFrame() []byte

	// DispatchInput forwards a raw JSON input message from the browser client to
	// Chrome via CDP (e.g. Input.dispatchMouseEvent / Input.dispatchKeyEvent).
	// The message should be a JSON object with "method" and "params" fields.
	DispatchInput(msg []byte) error

	// SetStateChangeCallback registers a callback that is invoked (in a goroutine)
	// each time the CDP state changes. Replaces any previously registered callback.
	SetStateChangeCallback(func(CDPState))

	// ReconcileOrphans removes wrapper-script directories under the cdp-bins path
	// whose session ID does not appear in activeSessionIDs. This cleans up
	// directories left behind by sessions that were deleted without calling Stop().
	ReconcileOrphans(activeSessionIDs []string) error
}

CDPStreamManager manages the Chrome DevTools Protocol screencast stream for one session. It allocates a TCP port, writes Chrome wrapper scripts, polls for Chrome's CDP endpoint, and subscribes to Page.screencastFrame events.

The interface is satisfied by both the real implementation (cdpStreamManager) and the no-op implementation (noopCDPManager). The session layer holds this interface so it can be swapped out when Chrome is unavailable.

func New

func New(cfg CDPConfig) CDPStreamManager

New returns a CDPStreamManager appropriate for the current session. If cfg.ChromePath is empty (Chrome not found), a noopCDPManager is returned. cfg.SessionID must be set before calling New.

type DepsResult

type DepsResult struct {
	// Available is true when a Chrome/Chromium binary was found.
	// Only when Available is true will New() return a live manager.
	Available bool
	// ChromePath is the absolute path to the first Chrome binary found.
	// Empty when Available is false.
	ChromePath string
	// Reason is a human-readable explanation when Available is false.
	Reason string
}

DepsResult holds the outcome of a CDP dependency check.

func CheckDependencies

func CheckDependencies() DepsResult

CheckDependencies checks whether a Chrome or Chromium binary is present on the current host. The result is cached after the first call so subsequent calls are free.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL