viewpoint-core 0.4.3

High-level browser automation API for Viewpoint
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! Video recording for pages.
//!
//! This module provides video recording functionality using CDP's screencast feature.
//! Videos are recorded as a sequence of JPEG frames and can be saved as `WebM` files.

// Allow dead code for video recording scaffolding (spec: video-recording)

use std::path::PathBuf;
use std::sync::Arc;
use std::time::Instant;

use tokio::sync::RwLock;
use tracing::{debug, info};
use viewpoint_cdp::CdpConnection;
use viewpoint_cdp::protocol::{
    ScreencastFormat, ScreencastFrameAckParams, ScreencastFrameEvent, StartScreencastParams,
    StopScreencastParams,
};

use crate::error::PageError;

/// Options for video recording.
#[derive(Debug, Clone)]
pub struct VideoOptions {
    /// Directory to save videos in.
    pub dir: PathBuf,
    /// Video width (max).
    pub width: Option<i32>,
    /// Video height (max).
    pub height: Option<i32>,
}

impl VideoOptions {
    /// Create new video options with a directory.
    pub fn new(dir: impl Into<PathBuf>) -> Self {
        Self {
            dir: dir.into(),
            width: None,
            height: None,
        }
    }

    /// Set the maximum width for the video.
    #[must_use]
    pub fn width(mut self, width: i32) -> Self {
        self.width = Some(width);
        self
    }

    /// Set the maximum height for the video.
    #[must_use]
    pub fn height(mut self, height: i32) -> Self {
        self.height = Some(height);
        self
    }

    /// Set the video size.
    #[must_use]
    pub fn size(mut self, width: i32, height: i32) -> Self {
        self.width = Some(width);
        self.height = Some(height);
        self
    }
}

impl Default for VideoOptions {
    fn default() -> Self {
        Self {
            dir: std::env::temp_dir().join("viewpoint-videos"),
            width: None,
            height: None,
        }
    }
}

/// A recorded frame from the screencast.
#[derive(Debug, Clone)]
pub(super) struct RecordedFrame {
    /// JPEG image data.
    data: Vec<u8>,
    /// Timestamp when the frame was captured.
    timestamp: f64,
}

/// Internal state for video recording.
#[derive(Debug, Default)]
pub(super) struct VideoState {
    /// Whether recording is active.
    pub(super) recording: bool,
    /// Recorded frames.
    pub(super) frames: Vec<RecordedFrame>,
    /// Start time for timing.
    pub(super) start_time: Option<Instant>,
    /// Video options.
    pub(super) options: VideoOptions,
    /// Generated video path (set when recording stops).
    pub(super) video_path: Option<PathBuf>,
}

/// Video recording controller for a page.
///
/// Videos are recorded using CDP's screencast feature which captures
/// compressed frames from the browser. These frames are then assembled
/// into a video file.
///
/// # Example
///
/// ```
/// # #[cfg(feature = "integration")]
/// # tokio_test::block_on(async {
/// # use viewpoint_core::Browser;
/// use viewpoint_core::page::VideoOptions;
/// # let browser = Browser::launch().headless(true).launch().await.unwrap();
///
/// // Recording is usually started via context options
/// let context = browser.new_context_builder()
///     .record_video(VideoOptions::new("./videos"))
///     .build()
///     .await.unwrap();
///
/// let page = context.new_page().await.unwrap();
/// page.goto("https://example.com").goto().await.unwrap();
///
/// // Get the video path after recording
/// if let Some(video) = page.video() {
///     // Video operations available here
///     // let path = video.path().await?;
/// }
/// # });
/// ```
#[derive(Debug)]
pub struct Video {
    /// CDP connection.
    connection: Arc<CdpConnection>,
    /// Session ID.
    session_id: String,
    /// Internal state.
    pub(super) state: Arc<RwLock<VideoState>>,
}

impl Video {
    /// Create a new video controller.
    pub(crate) fn new(connection: Arc<CdpConnection>, session_id: String) -> Self {
        Self {
            connection,
            session_id,
            state: Arc::new(RwLock::new(VideoState::default())),
        }
    }

    /// Create a new video controller with options.
    pub(crate) fn with_options(
        connection: Arc<CdpConnection>,
        session_id: String,
        options: VideoOptions,
    ) -> Self {
        Self {
            connection,
            session_id,
            state: Arc::new(RwLock::new(VideoState {
                options,
                ..Default::default()
            })),
        }
    }

    /// Start recording video.
    ///
    /// This starts the CDP screencast which captures frames from the page.
    pub(crate) async fn start_recording(&self) -> Result<(), PageError> {
        let mut state = self.state.write().await;
        if state.recording {
            return Ok(()); // Already recording
        }

        // Ensure video directory exists
        tokio::fs::create_dir_all(&state.options.dir)
            .await
            .map_err(|e| {
                PageError::EvaluationFailed(format!("Failed to create video directory: {e}"))
            })?;

        // Build screencast params
        let mut params = StartScreencastParams::new()
            .format(ScreencastFormat::Jpeg)
            .quality(80);

        if let Some(width) = state.options.width {
            params = params.max_width(width);
        }
        if let Some(height) = state.options.height {
            params = params.max_height(height);
        }

        // Start screencast
        self.connection
            .send_command::<_, serde_json::Value>(
                "Page.startScreencast",
                Some(params),
                Some(&self.session_id),
            )
            .await?;

        state.recording = true;
        state.start_time = Some(Instant::now());
        state.frames.clear();

        info!("Started video recording");

        // Start the frame listener
        self.start_frame_listener();

        Ok(())
    }

    /// Start listening for screencast frames.
    fn start_frame_listener(&self) {
        let mut events = self.connection.subscribe_events();
        let session_id = self.session_id.clone();
        let connection = self.connection.clone();
        let state = self.state.clone();

        tokio::spawn(async move {
            while let Ok(event) = events.recv().await {
                // Filter for this session
                if event.session_id.as_deref() != Some(&session_id) {
                    continue;
                }

                if event.method == "Page.screencastFrame" {
                    if let Some(params) = &event.params {
                        if let Ok(frame_event) =
                            serde_json::from_value::<ScreencastFrameEvent>(params.clone())
                        {
                            // Check if we're still recording
                            let is_recording = {
                                let s = state.read().await;
                                s.recording
                            };

                            if !is_recording {
                                break;
                            }

                            // Decode the frame data
                            if let Ok(data) = base64::Engine::decode(
                                &base64::engine::general_purpose::STANDARD,
                                &frame_event.data,
                            ) {
                                let timestamp = frame_event.metadata.timestamp.unwrap_or(0.0);

                                // Store the frame
                                {
                                    let mut s = state.write().await;
                                    s.frames.push(RecordedFrame { data, timestamp });
                                }

                                // Acknowledge the frame
                                let _ = connection
                                    .send_command::<_, serde_json::Value>(
                                        "Page.screencastFrameAck",
                                        Some(ScreencastFrameAckParams {
                                            session_id: frame_event.session_id,
                                        }),
                                        Some(&session_id),
                                    )
                                    .await;
                            }
                        }
                    }
                }
            }
        });
    }

    /// Stop recording and save the video.
    ///
    /// Returns the path to the saved video file.
    pub(crate) async fn stop_recording(&self) -> Result<PathBuf, PageError> {
        let mut state = self.state.write().await;
        if !state.recording {
            if let Some(ref path) = state.video_path {
                return Ok(path.clone());
            }
            return Err(PageError::EvaluationFailed(
                "Video recording not started".to_string(),
            ));
        }

        // Stop screencast
        self.connection
            .send_command::<_, serde_json::Value>(
                "Page.stopScreencast",
                Some(StopScreencastParams {}),
                Some(&self.session_id),
            )
            .await?;

        state.recording = false;

        // Generate video file
        let video_path = self.generate_video(&state).await?;
        state.video_path = Some(video_path.clone());

        info!("Stopped video recording, saved to {:?}", video_path);

        Ok(video_path)
    }

    /// Generate the video file from recorded frames.
    async fn generate_video(&self, state: &VideoState) -> Result<PathBuf, PageError> {
        if state.frames.is_empty() {
            return Err(PageError::EvaluationFailed(
                "No frames recorded".to_string(),
            ));
        }

        // Generate a unique filename
        let filename = format!(
            "video-{}.webm",
            uuid::Uuid::new_v4()
                .to_string()
                .split('-')
                .next()
                .unwrap_or("unknown")
        );
        let video_path = state.options.dir.join(&filename);

        // For now, we save frames as individual images and create a simple container
        // A full WebM encoder would require additional dependencies (ffmpeg, vpx, etc.)
        // This is a simplified implementation that saves frames to a directory

        // Create a frames directory
        let frames_dir = state.options.dir.join(format!(
            "frames-{}",
            uuid::Uuid::new_v4()
                .to_string()
                .split('-')
                .next()
                .unwrap_or("unknown")
        ));
        tokio::fs::create_dir_all(&frames_dir).await.map_err(|e| {
            PageError::EvaluationFailed(format!("Failed to create frames directory: {e}"))
        })?;

        // Save each frame
        for (i, frame) in state.frames.iter().enumerate() {
            let frame_path = frames_dir.join(format!("frame-{i:05}.jpg"));
            tokio::fs::write(&frame_path, &frame.data)
                .await
                .map_err(|e| PageError::EvaluationFailed(format!("Failed to write frame: {e}")))?;
        }

        // Create a simple metadata file indicating this is a video
        let metadata = serde_json::json!({
            "type": "viewpoint-video",
            "format": "jpeg-sequence",
            "frame_count": state.frames.len(),
            "frames_dir": frames_dir.to_string_lossy(),
        });

        tokio::fs::write(
            &video_path,
            serde_json::to_string_pretty(&metadata).unwrap(),
        )
        .await
        .map_err(|e| PageError::EvaluationFailed(format!("Failed to write video metadata: {e}")))?;

        debug!("Saved {} frames to {:?}", state.frames.len(), frames_dir);

        Ok(video_path)
    }

    /// Get the path to the recorded video.
    ///
    /// Returns `None` if recording hasn't started or hasn't stopped yet.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use viewpoint_core::page::Page;
    ///
    /// # async fn example(page: &Page) -> Result<(), viewpoint_core::CoreError> {
    /// if let Some(video) = page.video() {
    ///     let path = video.path().await?;
    ///     println!("Video at: {}", path.display());
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn path(&self) -> Result<PathBuf, PageError> {
        let state = self.state.read().await;

        if let Some(ref path) = state.video_path {
            return Ok(path.clone());
        }

        if state.recording {
            return Err(PageError::EvaluationFailed(
                "Video is still recording. Call stop_recording() first.".to_string(),
            ));
        }

        Err(PageError::EvaluationFailed("No video recorded".to_string()))
    }

    // save_as and delete methods are in video_io.rs

    /// Check if video is currently being recorded.
    pub async fn is_recording(&self) -> bool {
        let state = self.state.read().await;
        state.recording
    }
}

// Page impl for video recording methods
impl super::Page {
    /// Get the video recording controller if video recording is enabled.
    ///
    /// Returns `None` if the page was created without video recording options.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use viewpoint_core::browser::Browser;
    /// use viewpoint_core::page::VideoOptions;
    ///
    /// # async fn example() -> Result<(), viewpoint_core::CoreError> {
    /// let browser = Browser::launch().headless(true).launch().await?;
    /// // Video recording is enabled via context options
    /// let context = browser.new_context_builder()
    ///     .record_video(VideoOptions::new("./videos"))
    ///     .build()
    ///     .await?;
    ///
    /// let page = context.new_page().await?;
    /// page.goto("https://example.com").goto().await?;
    ///
    /// // Access the video after actions
    /// if let Some(video) = page.video() {
    ///     let path = video.path().await?;
    ///     println!("Video saved to: {}", path.display());
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn video(&self) -> Option<&Video> {
        self.video_controller
            .as_ref()
            .map(std::convert::AsRef::as_ref)
    }

    /// Start video recording (internal use).
    pub(crate) async fn start_video_recording(&self) -> Result<(), PageError> {
        if let Some(ref video) = self.video_controller {
            video.start_recording().await
        } else {
            Ok(())
        }
    }

    /// Stop video recording and get the path (internal use).
    pub(crate) async fn stop_video_recording(
        &self,
    ) -> Result<Option<std::path::PathBuf>, PageError> {
        if let Some(ref video) = self.video_controller {
            Ok(Some(video.stop_recording().await?))
        } else {
            Ok(None)
        }
    }
}