-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
output.go
267 lines (210 loc) · 5.52 KB
/
output.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
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
//
// obs-teleport. OBS Studio plugin.
// Copyright (C) 2021-2024 Florian Zwoch <fzwoch@gmail.com>
//
// This file is part of obs-teleport.
//
// obs-teleport is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// obs-teleport is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with obs-teleport. If not, see <http://www.gnu.org/licenses/>.
//
package main
//
// #include <obs-module.h>
//
import "C"
import (
"bytes"
"net"
"runtime/cgo"
"strconv"
"sync"
"time"
"unsafe"
)
type teleportOutput struct {
sync.Mutex
sync.WaitGroup
Announcer
Sender
pool *Pool
done chan any
output *C.obs_output_t
queue []*Packet
laggedFrames int
}
//export output_get_name
func output_get_name(type_data C.uintptr_t) *C.char {
return frontend_str
}
//export output_create
func output_create(settings *C.obs_data_t, output *C.obs_output_t) C.uintptr_t {
h := &teleportOutput{
output: output,
pool: NewPool(10),
}
return C.uintptr_t(cgo.NewHandle(h))
}
//export output_destroy
func output_destroy(data C.uintptr_t) {
cgo.Handle(data).Delete()
}
//export output_start
func output_start(data C.uintptr_t) C.bool {
h := cgo.Handle(data).Value().(*teleportOutput)
if !C.obs_output_can_begin_data_capture(h.output, 0) {
return false
}
video := C.obs_output_video(h.output)
info := C.video_output_get_info(video)
switch info.format {
case C.VIDEO_FORMAT_I420:
case C.VIDEO_FORMAT_I444:
case C.VIDEO_FORMAT_BGRA:
default:
scale_info := C.struct_video_scale_info{
format: C.VIDEO_FORMAT_I420,
width: info.width,
height: info.height,
_range: info._range,
colorspace: info.colorspace,
}
C.obs_output_set_video_conversion(h.output, &scale_info)
}
h.done = make(chan any)
h.laggedFrames = 0
h.Add(1)
go h.outputLoop()
C.obs_output_begin_data_capture(h.output, 0)
return true
}
//export output_stop
func output_stop(data C.uintptr_t, ts C.uint64_t) {
h := cgo.Handle(data).Value().(*teleportOutput)
// hack: prevent deadlock, why called multiple times?
if h.done == nil {
return
}
C.obs_output_end_data_capture(h.output)
h.done <- nil
h.Wait()
close(h.done)
h.done = nil
}
//export output_raw_video
func output_raw_video(data C.uintptr_t, frame *C.struct_video_data) {
h := cgo.Handle(data).Value().(*teleportOutput)
if h.SenderGetNumConns() == 0 {
return
}
p := &Packet{
Header: Header{
Timestamp: uint64(frame.timestamp),
},
ImageBuffer: h.pool.Get().(*bytes.Buffer),
}
settings := C.obs_source_get_settings(dummy)
p.Quality = int(C.obs_data_get_int(settings, quality_str))
C.obs_data_release(settings)
video := C.obs_output_video(h.output)
info := C.video_output_get_info(video)
format := info.format
switch info.format {
case C.VIDEO_FORMAT_I420:
case C.VIDEO_FORMAT_I444:
case C.VIDEO_FORMAT_BGRA:
default:
format = C.VIDEO_FORMAT_I420
}
p.ToImage(C.obs_output_get_width(h.output), C.obs_output_get_height(h.output), format, frame.data)
if p.Image == nil {
return
}
C.video_format_get_parameters(info.colorspace, info._range, (*C.float)(unsafe.Pointer(&p.ImageHeader.ColorMatrix[0])), (*C.float)(unsafe.Pointer(&p.ImageHeader.ColorRangeMin[0])), (*C.float)(unsafe.Pointer(&p.ImageHeader.ColorRangeMax[0])))
h.Lock()
h.queue = append(h.queue, p)
queueSize := time.Duration(h.queue[len(h.queue)-1].Header.Timestamp - h.queue[0].Header.Timestamp)
if queueSize > time.Second {
blog(C.LOG_WARNING, "encoder queue exceeded: "+queueSize.String())
h.laggedFrames++
}
h.Unlock()
h.Add(1)
go func(p *Packet) {
defer h.Done()
p.ToJPEG(h.pool)
h.Lock()
defer h.Unlock()
p.DoneProcessing = true
for len(h.queue) > 0 && h.queue[0].DoneProcessing {
h.SenderSend(h.queue[0].Buffer)
h.pool.Put(h.queue[0].ImageBuffer)
h.queue[0] = nil
h.queue = h.queue[1:]
}
}(p)
}
//export output_raw_audio
func output_raw_audio(data C.uintptr_t, frames *C.struct_audio_data) {
h := cgo.Handle(data).Value().(*teleportOutput)
if h.SenderGetNumConns() == 0 {
return
}
audio := C.obs_output_audio(h.output)
info := C.audio_output_get_info(audio)
p := Packet{
Header: Header{
Timestamp: uint64(frames.timestamp),
},
}
p.ToWAVE(info, frames.frames, frames.data)
h.SenderSend(p.Buffer)
}
//export output_get_dropped_frames
func output_get_dropped_frames(data C.uintptr_t) C.int {
h := cgo.Handle(data).Value().(*teleportOutput)
h.Lock()
defer h.Unlock()
return C.int(h.laggedFrames)
}
func (h *teleportOutput) outputLoop() {
defer h.Done()
defer h.SenderClose()
settings := C.obs_source_get_settings(dummy)
name := C.GoString(C.obs_data_get_string(settings, identifier_str))
listenPort := int(C.obs_data_get_int(settings, port_str))
C.obs_data_release(settings)
l, err := net.Listen("tcp", ":"+strconv.Itoa(listenPort))
if err != nil {
panic(err)
}
defer l.Close()
h.Add(1)
go func() {
defer h.Done()
for {
c, err := l.Accept()
if err != nil {
break
}
h.SenderAdd(c)
}
}()
_, p, err := net.SplitHostPort(l.Addr().String())
if err != nil {
panic(err)
}
port, _ := strconv.Atoi(p)
h.StartAnnouncer(name, port, true)
defer h.StopAnnouncer()
<-h.done
}