Files
lkm/stream/source_manager.go
DESKTOP-COJOJSE\lenovo c468f7388c 支持rtmpserver收流
2023-11-10 18:07:46 +08:00

52 lines
845 B
Go

package stream
import (
"fmt"
"sync"
)
type ISourceManager interface {
Add(source ISource) error
Find(id string) ISource
Remove(id string) (ISource, error)
}
var SourceManager ISourceManager
func init() {
SourceManager = &sourceMangerImpl{}
}
type sourceMangerImpl struct {
m sync.Map
}
func (s *sourceMangerImpl) Add(source ISource) error {
_, ok := s.m.LoadOrStore(source.Id(), source)
if ok {
return fmt.Errorf("the source %s has been exist", source.Id())
}
return nil
}
func (s *sourceMangerImpl) Find(id string) ISource {
value, ok := s.m.Load(id)
if ok {
return value.(ISource)
}
return nil
}
func (s *sourceMangerImpl) Remove(id string) (ISource, error) {
value, loaded := s.m.LoadAndDelete(id)
if loaded {
return value.(ISource), nil
}
return nil, fmt.Errorf("source with id %s was not find", id)
}