Fix: get nil channel and gen url in backend

zijiren233-patch-1
zijiren233 3 years ago
parent e265e28498
commit 3afba40e52

@ -51,30 +51,33 @@ func (m *Movie) AlistCache() *cache.AlistMovieCache {
}
func (m *Movie) Channel() (*rtmps.Channel, error) {
return m.channel.Load(), m.initChannel()
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, bool) {
if m.channel.Load() == nil {
c := rtmps.NewChannel()
func (m *Movie) compareAndSwapInitChannel() *rtmps.Channel {
c := m.channel.Load()
if c == nil {
c = rtmps.NewChannel()
if !m.channel.CompareAndSwap(nil, c) {
return nil, false
return m.compareAndSwapInitChannel()
}
return c, true
c.InitHlsPlayer(hls.WithGenTsNameFunc(genTsName))
}
return nil, false
return c
}
func (m *Movie) initChannel() error {
switch {
case m.Movie.Base.Live && m.Movie.Base.RtmpSource:
if c, ok := m.compareAndSwapInitChannel(); ok {
return c.InitHlsPlayer(hls.WithGenTsNameFunc(genTsName))
}
m.compareAndSwapInitChannel()
case m.Movie.Base.Live && m.Movie.Base.Proxy:
u, err := url.Parse(m.Movie.Base.Url)
if err != nil {
@ -82,10 +85,7 @@ func (m *Movie) initChannel() error {
}
switch u.Scheme {
case "rtmp":
c, ok := m.compareAndSwapInitChannel()
if !ok {
return nil
}
c := m.compareAndSwapInitChannel()
err = c.InitHlsPlayer(hls.WithGenTsNameFunc(genTsName))
if err != nil {
return err
@ -108,10 +108,7 @@ func (m *Movie) initChannel() error {
}
}()
case "http", "https":
c, ok := m.compareAndSwapInitChannel()
if !ok {
return nil
}
c := m.compareAndSwapInitChannel()
c.InitHlsPlayer(hls.WithGenTsNameFunc(genTsName))
go func() {
for {

@ -96,15 +96,11 @@ func (m *movies) GetChannel(id string) (*rtmps.Channel, error) {
if id == "" {
return nil, errors.New("channel name is nil")
}
m.init()
m.lock.RLock()
defer m.lock.RUnlock()
for e := m.list.Front(); e != nil; e = e.Next() {
if e.Value.Movie.ID == id {
return e.Value.Channel()
}
movie, err := m.GetMovieByID(id)
if err != nil {
return nil, err
}
return nil, errors.New("channel not found")
return movie.Channel()
}
func (m *movies) Update(movieId string, movie *model.BaseMovie) error {

@ -111,8 +111,15 @@ func genCurrentResp(current *op.Current) *model.CurrentMovieResp {
c.Movie.Base.Type = utils.GetUrlExtension(c.Movie.Base.Url)
}
// hide url and headers when proxy
if c.Movie.Base.Proxy {
c.Movie.Base.Url = ""
if c.Movie.Base.RtmpSource || c.Movie.Base.Live && c.Movie.Base.Proxy {
t := c.Movie.Base.Type
if t != "flv" && t != "m3u8" {
t = "m3u8"
}
c.Movie.Base.Url = fmt.Sprintf("/api/movie/live/%s.%s", current.Movie.ID, t)
c.Movie.Base.Headers = nil
} else if c.Movie.Base.Proxy {
c.Movie.Base.Url = fmt.Sprintf("/api/movie/proxy/%s/%s", current.Movie.RoomID, current.Movie.ID)
c.Movie.Base.Headers = nil
}
return c

Loading…
Cancel
Save