-
Notifications
You must be signed in to change notification settings - Fork 69
/
movie.go
307 lines (284 loc) · 6.87 KB
/
movie.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
package op
import (
"context"
"errors"
"fmt"
"hash/crc32"
"net/url"
"sync/atomic"
"time"
"github.com/go-resty/resty/v2"
"github.com/synctv-org/synctv/internal/cache"
"github.com/synctv-org/synctv/internal/conf"
"github.com/synctv-org/synctv/internal/model"
"github.com/synctv-org/synctv/internal/settings"
"github.com/synctv-org/synctv/utils"
"github.com/zijiren233/livelib/av"
"github.com/zijiren233/livelib/container/flv"
"github.com/zijiren233/livelib/protocol/hls"
rtmpProto "github.com/zijiren233/livelib/protocol/rtmp"
"github.com/zijiren233/livelib/protocol/rtmp/core"
rtmps "github.com/zijiren233/livelib/server"
)
type Movie struct {
*model.Movie
channel atomic.Pointer[rtmps.Channel]
alistCache atomic.Pointer[cache.AlistMovieCache]
bilibiliCache atomic.Pointer[cache.BilibiliMovieCache]
embyCache atomic.Pointer[cache.EmbyMovieCache]
}
func (m *Movie) ExpireId() uint64 {
switch {
case m.Movie.Base.VendorInfo.Vendor == model.VendorAlist:
amcd, _ := m.AlistCache().Raw()
if amcd != nil && amcd.Ali != nil {
return uint64(m.AlistCache().Last())
}
fallthrough
default:
return uint64(crc32.ChecksumIEEE([]byte(m.Movie.ID)))
}
}
func (m *Movie) CheckExpired(expireId uint64) bool {
switch {
case m.Movie.Base.VendorInfo.Vendor == model.VendorAlist:
amcd, _ := m.AlistCache().Raw()
if amcd != nil && amcd.Ali != nil {
return time.Now().UnixNano()-int64(expireId) > m.AlistCache().MaxAge()
}
fallthrough
default:
return expireId != m.ExpireId()
}
}
func (m *Movie) ClearCache() error {
m.alistCache.Store(nil)
bmc := m.bilibiliCache.Swap(nil)
if bmc != nil {
bmc.NoSharedMovie.Clear()
}
emc := m.embyCache.Swap(nil)
if emc != nil {
u, err := LoadOrInitUserByID(m.CreatorID)
if err != nil {
return err
}
err = emc.Clear(context.Background(), u.Value().EmbyCache())
if err != nil {
return err
}
}
return nil
}
func (m *Movie) AlistCache() *cache.AlistMovieCache {
c := m.alistCache.Load()
if c == nil {
c = cache.NewAlistMovieCache(m.Movie)
if !m.alistCache.CompareAndSwap(nil, c) {
return m.AlistCache()
}
}
return c
}
func (m *Movie) BilibiliCache() *cache.BilibiliMovieCache {
c := m.bilibiliCache.Load()
if c == nil {
c = cache.NewBilibiliMovieCache(m.Movie)
if !m.bilibiliCache.CompareAndSwap(nil, c) {
return m.BilibiliCache()
}
}
return c
}
func (m *Movie) EmbyCache() *cache.EmbyMovieCache {
c := m.embyCache.Load()
if c == nil {
c = cache.NewEmbyMovieCache(m.Movie)
if !m.embyCache.CompareAndSwap(nil, c) {
return m.EmbyCache()
}
}
return c
}
func (m *Movie) Channel() (*rtmps.Channel, error) {
err := m.initChannel()
if err != nil {
return nil, err
}
return m.channel.Load(), nil
}
func genTsName() string {
return utils.SortUUID()
}
func (m *Movie) compareAndSwapInitChannel() *rtmps.Channel {
c := m.channel.Load()
if c == nil {
c = rtmps.NewChannel()
if !m.channel.CompareAndSwap(nil, c) {
return m.compareAndSwapInitChannel()
}
c.InitHlsPlayer(hls.WithGenTsNameFunc(genTsName))
}
return c
}
func (m *Movie) initChannel() error {
switch {
case m.Movie.Base.Live && m.Movie.Base.RtmpSource:
m.compareAndSwapInitChannel()
case m.Movie.Base.Live && m.Movie.Base.Proxy:
u, err := url.Parse(m.Movie.Base.Url)
if err != nil {
return err
}
switch u.Scheme {
case "rtmp":
c := m.compareAndSwapInitChannel()
err = c.InitHlsPlayer(hls.WithGenTsNameFunc(genTsName))
if err != nil {
return err
}
go func() {
for {
if c.Closed() {
return
}
cli := core.NewConnClient()
if err = cli.Start(m.Movie.Base.Url, av.PLAY); err != nil {
cli.Close()
time.Sleep(time.Second)
continue
}
if err := c.PushStart(rtmpProto.NewReader(cli)); err != nil {
cli.Close()
time.Sleep(time.Second)
}
}
}()
case "http", "https":
c := m.compareAndSwapInitChannel()
err := c.InitHlsPlayer(hls.WithGenTsNameFunc(genTsName))
if err != nil {
return err
}
go func() {
for {
if c.Closed() {
return
}
r := resty.New().R()
for k, v := range m.Movie.Base.Headers {
r.SetHeader(k, v)
}
// r.SetHeader("User-Agent", UserAgent)
resp, err := r.Get(m.Movie.Base.Url)
if err != nil {
time.Sleep(time.Second)
continue
}
if err := c.PushStart(flv.NewReader(resp.RawBody())); err != nil {
time.Sleep(time.Second)
}
resp.RawBody().Close()
}
}()
default:
return errors.New("unsupported scheme")
}
default:
return errors.New("this movie not support channel")
}
return nil
}
func (movie *Movie) Validate() error {
m := movie.Movie.Base
if m.VendorInfo.Vendor != "" {
err := movie.validateVendorMovie()
if err != nil {
return err
}
}
switch {
case m.RtmpSource && m.Proxy:
return errors.New("rtmp source and proxy can't be true at the same time")
case m.Live && m.RtmpSource:
if !conf.Conf.Server.Rtmp.Enable {
return errors.New("rtmp is not enabled")
}
case m.Live && m.Proxy:
if !settings.LiveProxy.Get() {
return errors.New("live proxy is not enabled")
}
u, err := url.Parse(m.Url)
if err != nil {
return err
}
if !settings.AllowProxyToLocal.Get() && utils.IsLocalIP(u.Host) {
return errors.New("local ip is not allowed")
}
switch u.Scheme {
case "rtmp":
case "http", "https":
default:
return errors.New("unsupported scheme")
}
case !m.Live && m.RtmpSource:
return errors.New("rtmp source can't be true when movie is not live")
case !m.Live && m.Proxy:
if !settings.MovieProxy.Get() {
return errors.New("movie proxy is not enabled")
}
if m.VendorInfo.Vendor != "" {
return nil
}
u, err := url.Parse(m.Url)
if err != nil {
return err
}
if !settings.AllowProxyToLocal.Get() && utils.IsLocalIP(u.Host) {
return errors.New("local ip is not allowed")
}
if u.Scheme != "http" && u.Scheme != "https" {
return errors.New("unsupported scheme")
}
case !m.Live && !m.Proxy, m.Live && !m.Proxy && !m.RtmpSource:
if m.VendorInfo.Vendor == "" {
u, err := url.Parse(m.Url)
if err != nil {
return err
}
if u.Scheme != "http" && u.Scheme != "https" {
return errors.New("unsupported scheme")
}
}
default:
return errors.New("unknown error")
}
return nil
}
func (movie *Movie) validateVendorMovie() error {
switch movie.Movie.Base.VendorInfo.Vendor {
case model.VendorBilibili:
return movie.Movie.Base.VendorInfo.Bilibili.Validate()
case model.VendorAlist:
return movie.Movie.Base.VendorInfo.Alist.Validate()
case model.VendorEmby:
return movie.Movie.Base.VendorInfo.Emby.Validate()
default:
return fmt.Errorf("vendor not implement validate")
}
}
func (m *Movie) Terminate() error {
c := m.channel.Swap(nil)
if c != nil {
err := c.Close()
if err != nil {
return err
}
}
return nil
}
func (m *Movie) Update(movie *model.BaseMovie) error {
m.Movie.Base = *movie
m.ClearCache()
return m.Terminate()
}