Repository navigation
Expand file tree
/
Copy pathscreencast.go
More file actions
134 lines (124 loc) · 3.78 KB
/
Copy pathscreencast.go
File metadata and controls
134 lines (124 loc) · 3.78 KB
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
package playwright
import (
"encoding/base64"
"errors"
"sync"
)
type screencastImpl struct {
page *pageImpl
started bool
savePath *string
artifact *artifactImpl
mu sync.Mutex // guards onFrame, read on the dispatcher goroutine
onFrame func(OnFrame)
listening bool
}
func (s *screencastImpl) Start(options ...ScreencastStartOptions) error {
if s.started {
return errors.New("Screencast is already started")
}
s.started = true
overrides := map[string]any{}
if len(options) == 1 {
if options[0].OnFrame != nil {
s.mu.Lock()
s.onFrame = options[0].OnFrame
s.mu.Unlock()
// Register the channel listener once and dispatch through the
// mutable onFrame field, mirroring upstream. This avoids leaking a
// listener (and duplicate dispatch) on every Start/Stop cycle.
if !s.listening {
s.listening = true
s.page.channel.On("screencastFrame", func(params map[string]any) {
frameID, hasFrameID := params["frameId"].(float64)
s.mu.Lock()
onFrame := s.onFrame
s.mu.Unlock()
s.page.channel.CreateTask(func() {
// The server applies backpressure until every valid frame is
// acknowledged, including when no handler is active or the
// handler panics or exits its goroutine.
if hasFrameID {
defer s.page.channel.SendNoReplyInternalWithTimeout("screencastFrameAck", Float(0), map[string]any{
"frameId": frameID,
})
}
if onFrame == nil {
return
}
dataStr, _ := params["data"].(string)
data, _ := base64.StdEncoding.DecodeString(dataStr)
frame := OnFrame{Data: data}
if ts, ok := params["timestamp"].(float64); ok {
frame.Timestamp = ts
}
if vw, ok := params["viewportWidth"].(float64); ok {
frame.ViewportWidth = int(vw)
}
if vh, ok := params["viewportHeight"].(float64); ok {
frame.ViewportHeight = int(vh)
}
onFrame(frame)
})
})
}
overrides["sendFrames"] = true
options[0].OnFrame = nil // don't serialize the callback
}
if options[0].Path != nil {
overrides["record"] = true
s.savePath = options[0].Path
}
}
result, err := s.page.channel.SendReturnAsDict("screencastStart", options, overrides)
if err != nil {
return err
}
if artifactChannel := fromNullableChannel(result["artifact"]); artifactChannel != nil {
s.artifact = artifactChannel.(*artifactImpl)
}
return nil
}
func (s *screencastImpl) Stop() error {
s.started = false
s.mu.Lock()
s.onFrame = nil
s.mu.Unlock()
if _, err := s.page.channel.Send("screencastStop"); err != nil {
return err
}
if s.savePath != nil && s.artifact != nil {
if err := s.artifact.SaveAs(*s.savePath); err != nil {
return err
}
}
s.artifact = nil
s.savePath = nil
return nil
}
func (s *screencastImpl) ShowActions(options ...ScreencastShowActionsOptions) error {
_, err := s.page.channel.Send("screencastShowActions", options)
return err
}
func (s *screencastImpl) HideActions() error {
_, err := s.page.channel.Send("screencastHideActions")
return err
}
func (s *screencastImpl) ShowOverlay(html string, options ...ScreencastShowOverlayOptions) error {
overrides := map[string]any{"html": html}
_, err := s.page.channel.Send("screencastShowOverlay", options, overrides)
return err
}
func (s *screencastImpl) ShowChapter(title string, options ...ScreencastShowChapterOptions) error {
overrides := map[string]any{"title": title}
_, err := s.page.channel.Send("screencastChapter", options, overrides)
return err
}
func (s *screencastImpl) ShowOverlays() error {
_, err := s.page.channel.Send("screencastSetOverlayVisible", map[string]any{"visible": true})
return err
}
func (s *screencastImpl) HideOverlays() error {
_, err := s.page.channel.Send("screencastSetOverlayVisible", map[string]any{"visible": false})
return err
}