-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
197 lines (166 loc) · 3.87 KB
/
Copy pathapp.go
File metadata and controls
197 lines (166 loc) · 3.87 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
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
package main
import (
"context"
"errors"
"fmt"
"path/filepath"
rt "runtime"
"sync"
"github.com/jaesung9507/playgo/stream"
"github.com/jaesung9507/playgo/stream/format/fmp4"
"github.com/jaesung9507/playgo/stream/route"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
streamClient stream.Client
mp4Muxer *fmp4.Muxer
streamCtx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
func (a *App) SetAlwaysOnTop(b bool) {
runtime.WindowSetAlwaysOnTop(a.ctx, b)
}
func (a *App) OpenFile() string {
pattern := route.SupportedFilePatterns()
filePath, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Open File",
Filters: []runtime.FileFilter{
{
DisplayName: fmt.Sprintf("Videos (%s)", pattern),
Pattern: pattern,
},
},
})
if err != nil {
a.MsgBox(err.Error())
return ""
}
if len(filePath) > 0 {
switch rt.GOOS {
case "windows":
filePath = "file:///" + filepath.ToSlash(filePath)
default:
filePath = "file://" + filePath
}
}
return filePath
}
func (a *App) Quit() {
runtime.Quit(a.ctx)
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
if screens, _ := runtime.ScreenGetAll(ctx); len(screens) > 0 {
primaryScreen := screens[0]
var width, height int
if primaryScreen.Size.Width >= primaryScreen.Size.Height {
width = min(int(float64(primaryScreen.Size.Width)*0.5), 1024)
} else {
width = min(int(float64(primaryScreen.Size.Height)*0.5), 1024)
}
height = int(float64(width) * 0.75)
runtime.WindowSetSize(ctx, width, height)
runtime.WindowCenter(ctx)
}
runtime.EventsOn(a.ctx, "OnUpdateEnd", func(optionalData ...any) {
a.wg.Add(1)
a.streamLoop()
})
}
func (a *App) MsgBox(msg string) {
_, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "PlayGo",
Message: msg,
Buttons: []string{"OK"},
})
}
func (a *App) CloseStream() {
if a.cancel != nil {
a.cancel()
a.cancel = nil
}
a.wg.Wait()
if a.streamClient != nil {
a.streamClient.Close()
a.streamClient = nil
}
if a.mp4Muxer != nil {
a.mp4Muxer = nil
}
}
func (a *App) initStream(client stream.Client, muxer *fmp4.Muxer) {
a.streamClient = client
a.mp4Muxer = muxer
}
func (a *App) streamLoop() {
defer a.wg.Done()
if a.streamClient != nil && a.mp4Muxer != nil {
defer runtime.EventsEmit(a.ctx, "OnStreamStop")
for {
select {
case <-a.streamCtx.Done():
return
case <-a.streamClient.CloseCh():
return
case packet := <-a.streamClient.PacketQueue():
switch rt.GOOS {
case "linux":
packet.CompositionTime = 0
}
if buf, _ := a.mp4Muxer.WritePacket(*packet); buf != nil {
runtime.EventsEmit(a.ctx, "OnFrame", buf)
}
}
}
}
}
func (a *App) PlayStream(url string) (result bool) {
a.streamCtx, a.cancel = context.WithCancel(a.ctx)
c, err := route.NewClient(url)
if err != nil {
a.MsgBox(err.Error())
return false
}
defer func() {
if !result {
c.Close()
}
}()
if err = stream.Dial(a.streamCtx, c); err != nil {
if !errors.Is(err, context.Canceled) {
a.MsgBox(err.Error())
}
return false
}
codecData, err := stream.CodecData(a.streamCtx, c)
if err != nil {
if !errors.Is(err, context.Canceled) {
a.MsgBox(err.Error())
}
return false
}
if len(codecData) <= 0 {
a.MsgBox("not found codec info")
return false
}
muxer := fmp4.NewMuxer()
meta, init, err := muxer.WriteHeader(codecData)
if err != nil {
a.MsgBox(err.Error())
return false
}
secured, trusted, secureInfo := c.Secure()
runtime.EventsEmit(a.ctx, "OnSecureInfo", secured, trusted, secureInfo)
a.initStream(c, muxer)
runtime.EventsEmit(a.ctx, "OnInit", meta, init)
return true
}