/
internal.go
319 lines (275 loc) · 7.94 KB
/
internal.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
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
package video
import (
"bytes"
"fmt"
"image"
"image/color"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"log"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"github.com/go-gst/go-gst/gst"
"github.com/go-gst/go-gst/gst/app"
streamer "github.com/metal3d/fyne-streamer"
"github.com/metal3d/fyne-streamer/internal/utils"
)
// prerollFunc is called when the pipeline is prerolling. This is a callback on the appsink.
func (v *Viewer) prerollFunc(appSink *app.Sink) gst.FlowReturn {
caps, err := appSink.Element.GetPads()
if len(caps) == 0 && err != nil {
return gst.FlowError
}
c := caps[0].GetCurrentCaps()
ww, err := c.GetStructureAt(0).GetValue("width")
if err != nil {
log.Printf("Error: %v", err)
}
hh, err := c.GetStructureAt(0).GetValue("height")
if err != nil {
log.Printf("Error: %v", err)
}
if ww, ok := ww.(int); ok {
v.width = ww
}
if hh, ok := hh.(int); ok {
v.height = hh
}
// call the callback
if v.onPreRoll != nil {
v.onPreRoll()
}
return gst.FlowOK
}
func (v *Viewer) reset() {
if v.pipeline != nil {
//BUG: do not use v.SetState(gst.StateNull)
if err := v.pipeline.SetState(gst.StateNull); err != nil {
fyne.LogError("Failed to set pipeline to null", err)
} else {
v.pipeline.Clear()
}
}
v.frame.Image = nil
v.duration = 0
}
func (v *Viewer) registerElements() error {
v.appSink = nil
var err error
_, err = v.pipeline.GetElementByName(streamer.InputElementName)
if err != nil {
log.Printf("Warning: Failed to find "+streamer.InputElementName+" element: %v", err)
}
_, err = v.pipeline.GetElementByName(streamer.VideoRateElementName)
if err != nil {
log.Printf("Warning: Failed to find "+streamer.VideoRateElementName+" element: %v", err)
}
_, err = v.pipeline.GetElementByName(streamer.ImageEncoderElementName)
if err != nil {
log.Printf("Warning: Failed to find "+streamer.ImageEncoderElementName+" element: %v", err)
}
_, err = v.pipeline.GetElementByName(streamer.DecodeElementName)
if err != nil {
log.Printf("Warning: Failed to find "+streamer.DecodeElementName+" element: %v", err)
}
appelement, err := v.pipeline.GetElementByName(streamer.AppSinkElementName)
if err != nil || appelement == nil {
fyne.LogError("Failed to find the appsink element", err)
return fmt.Errorf("Failed to find the mandatory %s element %w", streamer.AppSinkElementName, err)
}
v.appSink = app.SinkFromElement(appelement)
v.appSink.SetCallbacks(&app.SinkCallbacks{
EOSFunc: v.eosFunc,
NewPrerollFunc: v.prerollFunc,
NewSampleFunc: v.newSampleFunc,
})
return nil
}
// eosFunc is called when the pipeline is at the end of the stream. This is a callback on the appsink.
func (v *Viewer) eosFunc(appSink *app.Sink) {
if v.onEOS != nil {
v.onEOS()
}
err := v.SetState(gst.StatePaused)
if err != nil {
fyne.LogError("Failed to set pipeline to paused", err)
}
// TODO: this is a workaround to avoid a crash when the pipeline is stopped and to make it restartable.
time.AfterFunc(time.Millisecond*100, func() {
v.Seek(0)
time.AfterFunc(time.Millisecond*100, func() {
img, err := v.getCurrentFrame(v.appSink, true)
if err != gst.FlowOK {
log.Println("error getting first frame", err)
return
}
v.frame.Image = img
v.frame.Refresh()
})
})
}
// newSampleFunc is called when a new sample is available. This is a callback on the appsink.
// The sample is a jpeg image and is decoded to the internal frameView canvas.Image.
// It is refreshed as soon as the frame is OK.
func (v *Viewer) newSampleFunc(appSink *app.Sink) gst.FlowReturn {
img, ret := v.getCurrentFrame(appSink, false)
if ret != gst.FlowOK {
return ret
}
func() { // refresh the frame in a goroutine
v.frame.Image = img
v.frame.Refresh()
}()
_, pos := v.pipeline.QueryPosition(gst.FormatTime)
if v.onNewFrame != nil {
// get the current time of the pipeline
go v.onNewFrame(time.Duration(float64(pos)))
}
return ret
}
func (v *Viewer) getCurrentFrame(appSink *app.Sink, latest bool) (image.Image, gst.FlowReturn) {
var sample *gst.Sample
if !latest {
sample = appSink.PullSample()
} else {
sample = appSink.GetLastSample()
}
if sample == nil {
return nil, gst.FlowEOS
}
buffer := sample.GetBuffer() // Get the buffer from the sample
if buffer == nil {
return nil, gst.FlowError
}
defer buffer.Unmap()
samples := buffer.Map(gst.MapRead).AsUint8Slice()
if samples == nil {
return nil, gst.FlowError
}
// the sample is a jpeg
reader := bytes.NewReader(samples)
img, _, err := image.Decode(reader)
if err != nil {
fyne.LogError("Failed to decode image: %w", err)
return nil, gst.FlowError
}
return img, gst.FlowOK
}
// resync the pipeline with the parent state. This seems to fix some problem
// on leaveing paused state. But not always...
// At this time, this function is called on SetState and Seek methods.
func (v *Viewer) resync() {
// resync the pipeline
if v.pipeline == nil {
return
}
go func() {
elements, _ := v.pipeline.GetElements()
for _, e := range elements {
done := e.SyncStateWithParent()
if !done {
log.Printf("Warning: Failed to sync state with parent for element %s", e.GetName())
}
}
}()
}
// SetState sets the state of the pipeline. It is a blocking call with a timeout of 50ms. This is a workaround
// because the pipeline.SetState can block forever if the pipeline is not in a good state.
func (v *Viewer) setState(state gst.State) error {
if v.pipeline == nil {
return fmt.Errorf("No pipeline")
}
// change the state
err := v.pipeline.SetState(state)
if err != nil {
fyne.LogError("Failed to set state", err)
return err
}
// wait for the state to be set
done := make(chan error)
go func(done chan error) {
var err error
defer func() {
done <- err
}()
for {
select {
case <-time.Tick(1000 * time.Millisecond):
err = fmt.Errorf("timeout waiting for state %s", state)
return
case <-time.After(1 * time.Millisecond):
current_state := v.pipeline.GetCurrentState()
if current_state == state {
return
}
}
}
}(done)
return <-done
}
func (v *Viewer) setCurrentWindowFinder(w fyne.CanvasObject) {
v.currentWindowFinder = func() fyne.Window {
return utils.GetWindowForElement(w)
}
}
// fullscreenOff sets the video widget to windowed mode.
func (v *Viewer) fullscreenOff() {
if v.fullscreenWindow == nil {
fyne.LogError("Could not find fullscreen window", nil)
return
}
v.fullscreenWindow.Close()
if v.currentWindow == nil {
fyne.LogError("Could not find current window", nil)
return
}
}
// fullscreenOn sets the video widget to fullscreen.
func (v *Viewer) fullscreenOn() {
if v.currentWindow == nil {
v.currentWindow = v.currentWindowFinder()
if v.currentWindow == nil {
fyne.LogError("Could not find current window", nil)
return
}
}
fsWindow := fyne.CurrentApp().NewWindow("")
v.fullscreenWindow = fsWindow
// ESC or F key to exit fullscreen
fsWindow.Canvas().SetOnTypedKey(func(k *fyne.KeyEvent) {
if k.Name == fyne.KeyEscape || k.Name == fyne.KeyF {
v.fullscreenOff()
}
})
// set the content of the fullscreen window to the video widget
if v.originalViewerWidget == nil {
v.originalViewerWidget = v
}
// make a black rectangle to fill the background
background := canvas.NewRectangle(color.Black)
// and set the content of the fullscreen window to the video widget + black background
fsWindow.SetContent(
container.NewStack(
background,
v.originalViewerWidget,
),
)
// on close, we show the app window and refresh the content
fsWindow.SetOnClosed(func() {
v.currentWindow.Show()
v.currentWindow.Content().Refresh()
// BUG: this is a workaround to force the cursor to go back to the right position. It make a little flicker, but it works...
if pos, err := v.CurrentPosition(); err == nil {
v.Seek(pos)
}
})
// hide the current window and show the fullscreen window
v.currentWindow.Hide()
// make the new window fullscreen, unpadded and show it
fsWindow.SetFullScreen(true)
fsWindow.SetPadded(false)
fsWindow.Show()
}