This repository has been archived by the owner on May 26, 2022. It is now read-only.
/
display_prod.go
183 lines (161 loc) · 4.85 KB
/
display_prod.go
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
//go:build !test
// +build !test
package display
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"github.com/chromedp/cdproto/runtime"
"github.com/chromedp/chromedp"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/livekit-recorder/pkg/config"
)
const (
startRecording = "START_RECORDING"
endRecording = "END_RECORDING"
)
type Display struct {
xvfb *exec.Cmd
chromeCancel context.CancelFunc
startChan chan struct{}
endChan chan struct{}
}
func Launch(conf *config.Config, url string, opts *livekit.RecordingOptions, isTemplate bool) (*Display, error) {
d := &Display{
startChan: make(chan struct{}),
endChan: make(chan struct{}),
}
if err := d.launchXvfb(conf.Display, opts.Width, opts.Height, opts.Depth); err != nil {
return nil, err
}
if err := d.launchChrome(conf, url, opts.Width, opts.Height, isTemplate); err != nil {
return nil, err
}
return d, nil
}
func (d *Display) launchXvfb(display string, width, height, depth int32) error {
dims := fmt.Sprintf("%dx%dx%d", width, height, depth)
logger.Debugw("launching xvfb", "dims", dims)
xvfb := exec.Command("Xvfb", display, "-screen", "0", dims, "-ac", "-nolisten", "tcp")
if err := xvfb.Start(); err != nil {
return err
}
d.xvfb = xvfb
return nil
}
func (d *Display) launchChrome(conf *config.Config, url string, width, height int32, isTemplate bool) error {
logger.Debugw("launching chrome", "url", url)
opts := []chromedp.ExecAllocatorOption{
chromedp.NoFirstRun,
chromedp.NoDefaultBrowserCheck,
chromedp.DisableGPU,
chromedp.NoSandbox,
// puppeteer default behavior
chromedp.Flag("disable-infobars", true),
chromedp.Flag("excludeSwitches", "enable-automation"),
chromedp.Flag("disable-background-networking", true),
chromedp.Flag("enable-features", "NetworkService,NetworkServiceInProcess"),
chromedp.Flag("disable-background-timer-throttling", true),
chromedp.Flag("disable-backgrounding-occluded-windows", true),
chromedp.Flag("disable-breakpad", true),
chromedp.Flag("disable-client-side-phishing-detection", true),
chromedp.Flag("disable-default-apps", true),
chromedp.Flag("disable-dev-shm-usage", true),
chromedp.Flag("disable-extensions", true),
chromedp.Flag("disable-features", "site-per-process,TranslateUI,BlinkGenPropertyTrees"),
chromedp.Flag("disable-hang-monitor", true),
chromedp.Flag("disable-ipc-flooding-protection", true),
chromedp.Flag("disable-popup-blocking", true),
chromedp.Flag("disable-prompt-on-repost", true),
chromedp.Flag("disable-renderer-backgrounding", true),
chromedp.Flag("disable-sync", true),
chromedp.Flag("force-color-profile", "srgb"),
chromedp.Flag("metrics-recording-only", true),
chromedp.Flag("safebrowsing-disable-auto-update", true),
chromedp.Flag("password-store", "basic"),
chromedp.Flag("use-mock-keychain", true),
// custom args
chromedp.Flag("kiosk", true),
chromedp.Flag("enable-automation", false),
chromedp.Flag("autoplay-policy", "no-user-gesture-required"),
chromedp.Flag("window-position", "0,0"),
chromedp.Flag("window-size", fmt.Sprintf("%d,%d", width, height)),
chromedp.Flag("display", conf.Display),
}
if conf.Insecure {
opts = append(opts,
chromedp.Flag("disable-web-security", true),
chromedp.Flag("allow-running-insecure-content", true),
)
}
allocCtx, _ := chromedp.NewExecAllocator(context.Background(), opts...)
ctx, cancel := chromedp.NewContext(allocCtx)
d.chromeCancel = cancel
chromedp.ListenTarget(ctx, func(ev interface{}) {
switch ev := ev.(type) {
case *runtime.EventConsoleAPICalled:
args := make([]string, 0, len(ev.Args))
for _, arg := range ev.Args {
var val interface{}
err := json.Unmarshal(arg.Value, &val)
if err != nil {
continue
}
msg := fmt.Sprint(val)
args = append(args, msg)
switch msg {
case startRecording:
close(d.startChan)
case endRecording:
close(d.endChan)
default:
}
}
logger.Debugw(fmt.Sprintf("chrome console %s", ev.Type.String()), "msg", strings.Join(args, " "))
}
})
var err error
var errString string
if isTemplate {
err = chromedp.Run(ctx,
chromedp.Navigate(url),
chromedp.Evaluate(`
if (document.querySelector('div.error')) {
document.querySelector('div.error').innerText;
} else {
''
}`, &errString,
),
)
} else {
err = chromedp.Run(ctx, chromedp.Navigate(url))
}
if err == nil && errString != "" {
err = errors.New(errString)
}
return err
}
func (d *Display) RoomStarted() chan struct{} {
return d.startChan
}
func (d *Display) RoomEnded() chan struct{} {
return d.endChan
}
func (d *Display) Close() {
if d.chromeCancel != nil {
d.chromeCancel()
d.chromeCancel = nil
}
if d.xvfb != nil {
err := d.xvfb.Process.Signal(os.Interrupt)
if err != nil {
logger.Errorw("failed to kill xvfb", err)
}
d.xvfb = nil
}
}