-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
169 lines (151 loc) · 4.2 KB
/
main.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
package main
import (
"bytes"
"context"
"errors"
"fmt"
"image"
"net/http"
"runtime"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/kirides/go-d3d/d3d11"
"github.com/kirides/go-d3d/examples/framelimiter"
"github.com/kirides/go-d3d/outputduplication"
"github.com/kirides/go-d3d/win"
"github.com/kbinani/screenshot"
"github.com/mattn/go-mjpeg"
)
func main() {
n := screenshot.NumActiveDisplays()
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
http.HandleFunc("/watch", func(w http.ResponseWriter, r *http.Request) {
screen := r.URL.Query().Get("screen")
if screen == "" {
screen = "0"
}
screenNo, err := strconv.Atoi(screen)
if err != nil {
w.WriteHeader(500)
return
}
if screenNo >= n || screenNo < 0 {
screenNo = 0
}
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(`<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> Screen ` + strconv.Itoa(screenNo) + `</title>
</head>
<body style="margin:0">
<img src="/mjpeg` + strconv.Itoa(screenNo) + `" style="max-width: 100vw; max-height: 100vh;object-fit: contain;display: block;margin: 0 auto;" />
</body>`))
})
framerate := 25
for i := 0; i < screenshot.NumActiveDisplays(); i++ {
fmt.Fprintf(os.Stderr, "Registering stream %d\n", i)
stream := mjpeg.NewStream()
defer stream.Close()
go streamDisplayDXGI(ctx, i, framerate, stream)
http.HandleFunc(fmt.Sprintf("/mjpeg%d", i), stream.ServeHTTP)
}
go func() {
http.ListenAndServe("127.0.0.1:8023", nil)
}()
<-ctx.Done()
<-time.After(time.Second)
}
// Capture using IDXGIOutputDuplication
//
// https://docs.microsoft.com/en-us/windows/win32/api/dxgi1_2/nn-dxgi1_2-idxgioutputduplication
func streamDisplayDXGI(ctx context.Context, n int, framerate int, out *mjpeg.Stream) {
max := screenshot.NumActiveDisplays()
if n >= max {
fmt.Printf("Not enough displays\n")
return
}
// Keep this thread, so windows/d3d11/dxgi can use their threadlocal caches, if any
runtime.LockOSThread()
// Make thread PerMonitorV2 Dpi aware if supported on OS
// allows to let windows handle BGRA -> RGBA conversion and possibly more things
if win.IsValidDpiAwarenessContext(win.DpiAwarenessContextPerMonitorAwareV2) {
_, err := win.SetThreadDpiAwarenessContext(win.DpiAwarenessContextPerMonitorAwareV2)
if err != nil {
fmt.Printf("Could not set thread DPI awareness to PerMonitorAwareV2. %v\n", err)
} else {
fmt.Printf("Enabled PerMonitorAwareV2 DPI awareness.\n")
}
}
// Setup D3D11 stuff
device, deviceCtx, err := d3d11.NewD3D11Device()
if err != nil {
fmt.Printf("Could not create D3D11 Device. %v\n", err)
return
}
defer device.Release()
defer deviceCtx.Release()
var ddup *outputduplication.OutputDuplicator
defer func() {
if ddup != nil {
ddup.Release()
ddup = nil
}
}()
buf := &bufferFlusher{Buffer: bytes.Buffer{}}
opts := jpegQuality(50)
limiter := framelimiter.New(framerate)
lastBounds := image.Rectangle{}
var imgBuf *image.RGBA
for {
select {
case <-ctx.Done():
return
default:
limiter.Wait()
}
// create output duplication if doesn't exist yet (maybe due to resolution change)
if ddup == nil {
ddup, err = outputduplication.NewIDXGIOutputDuplication(device, deviceCtx, uint(n))
if err != nil {
fmt.Printf("err: %v\n", err)
continue
}
bounds, err := ddup.GetBounds()
if err != nil {
return
}
if bounds != lastBounds {
lastBounds = bounds
imgBuf = image.NewRGBA(lastBounds)
}
}
// Grab an image.RGBA from the current output presenter
err = ddup.GetImage(imgBuf, 999)
if err != nil {
if errors.Is(err, outputduplication.ErrNoImageYet) {
// don't update
continue
}
fmt.Printf("Err ddup.GetImage: %v\n", err)
// Retry with new ddup, can occur when changing resolution
ddup.Release()
ddup = nil
continue
}
buf.Reset()
encodeJpeg(buf, imgBuf, opts)
out.Update(buf.Bytes())
}
}
// Workaround for jpeg.Encode(), which requires a Flush()
// method to not call `bufio.NewWriter`
type bufferFlusher struct {
bytes.Buffer
}
func (*bufferFlusher) Flush() error { return nil }