From 09976a14a46b36bb05f2a60e60e0dd86f5c3c391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?halwu=28=E5=90=B4=E6=B5=A9=E9=BA=9F=29?= Date: Tue, 30 May 2017 17:28:41 +0800 Subject: [PATCH] fix: publisher close course hls server panic --- protocol/hls/{ts_cache.go => cache.go} | 42 -------------------------- protocol/hls/hls.go | 4 +++ protocol/hls/item.go | 18 +++++++++++ 3 files changed, 22 insertions(+), 42 deletions(-) rename protocol/hls/{ts_cache.go => cache.go} (70%) create mode 100644 protocol/hls/item.go diff --git a/protocol/hls/ts_cache.go b/protocol/hls/cache.go similarity index 70% rename from protocol/hls/ts_cache.go rename to protocol/hls/cache.go index e8c38d5..42fb0ee 100755 --- a/protocol/hls/ts_cache.go +++ b/protocol/hls/cache.go @@ -8,31 +8,6 @@ import ( "sync" ) -type TSCache struct { - entrys map[string]*TSCacheItem -} - -func NewTSCache() *TSCache { - return &TSCache{ - entrys: make(map[string]*TSCacheItem), - } -} - -func (cache *TSCache) Set(key string, e *TSCacheItem) { - v, ok := cache.entrys[key] - if !ok { - cache.entrys[key] = e - } - if v.ID() != e.ID() { - cache.entrys[key] = e - } -} - -func (cache *TSCache) Get(key string) *TSCacheItem { - v := cache.entrys[key] - return v -} - const ( maxTSCacheNum = 3 ) @@ -108,20 +83,3 @@ func (tcCacheItem *TSCacheItem) GetItem(key string) (TSItem, error) { } return item, nil } - -type TSItem struct { - Name string - SeqNum int - Duration int - Data []byte -} - -func NewTSItem(name string, duration, seqNum int, b []byte) TSItem { - var item TSItem - item.Name = name - item.SeqNum = seqNum - item.Duration = duration - item.Data = make([]byte, len(b)) - copy(item.Data, b) - return item -} diff --git a/protocol/hls/hls.go b/protocol/hls/hls.go index fa5020e..f10eecb 100755 --- a/protocol/hls/hls.go +++ b/protocol/hls/hls.go @@ -104,6 +104,10 @@ func (server *Server) handle(w http.ResponseWriter, r *http.Request) { return } tsCache := conn.GetCacheInc() + if tsCache == nil { + http.Error(w, ErrNoPublisher.Error(), http.StatusForbidden) + return + } body, err := tsCache.GenM3U8PlayList() if err != nil { log.Println("GenM3U8PlayList error: ", err) diff --git a/protocol/hls/item.go b/protocol/hls/item.go new file mode 100644 index 0000000..7bf47f6 --- /dev/null +++ b/protocol/hls/item.go @@ -0,0 +1,18 @@ +package hls + +type TSItem struct { + Name string + SeqNum int + Duration int + Data []byte +} + +func NewTSItem(name string, duration, seqNum int, b []byte) TSItem { + var item TSItem + item.Name = name + item.SeqNum = seqNum + item.Duration = duration + item.Data = make([]byte, len(b)) + copy(item.Data, b) + return item +}