stream listeners as pointers.

This commit is contained in:
Miroslav Šedivý 2021-02-06 17:05:25 +01:00
parent 5d66dcdb7b
commit 417a3d1692
3 changed files with 19 additions and 15 deletions

View File

@ -20,7 +20,7 @@ type StreamManagerCtx struct {
pipelineStr string pipelineStr string
pipeline *gst.Pipeline pipeline *gst.Pipeline
sample chan types.Sample sample chan types.Sample
listeners map[uintptr]func(sample types.Sample) listeners map[uintptr]*func(sample types.Sample)
emitMu sync.Mutex emitMu sync.Mutex
emitUpdate chan bool emitUpdate chan bool
emitStop chan bool emitStop chan bool
@ -32,7 +32,7 @@ func streamNew(codec codec.RTPCodec, pipelineStr string) *StreamManagerCtx {
logger: log.With().Str("module", "capture").Str("submodule", "stream").Logger(), logger: log.With().Str("module", "capture").Str("submodule", "stream").Logger(),
codec: codec, codec: codec,
pipelineStr: pipelineStr, pipelineStr: pipelineStr,
listeners: map[uintptr]func(sample types.Sample){}, listeners: map[uintptr]*func(sample types.Sample){},
emitUpdate: make(chan bool), emitUpdate: make(chan bool),
emitStop: make(chan bool), emitStop: make(chan bool),
started: false, started: false,
@ -51,7 +51,7 @@ func streamNew(codec codec.RTPCodec, pipelineStr string) *StreamManagerCtx {
case sample := <-manager.sample: case sample := <-manager.sample:
manager.emitMu.Lock() manager.emitMu.Lock()
for _, emit := range manager.listeners { for _, emit := range manager.listeners {
emit(sample) (*emit)(sample)
} }
manager.emitMu.Unlock() manager.emitMu.Unlock()
} }
@ -72,20 +72,24 @@ func (manager *StreamManagerCtx) Codec() codec.RTPCodec {
return manager.codec return manager.codec
} }
func (manager *StreamManagerCtx) AddListener(listener func(sample types.Sample)) { func (manager *StreamManagerCtx) AddListener(listener *func(sample types.Sample)) {
manager.emitMu.Lock() manager.emitMu.Lock()
defer manager.emitMu.Unlock() defer manager.emitMu.Unlock()
ptr := reflect.ValueOf(listener).Pointer() if listener != nil {
manager.listeners[ptr] = listener ptr := reflect.ValueOf(listener).Pointer()
manager.listeners[ptr] = listener
}
} }
func (manager *StreamManagerCtx) RemoveListener(listener func(sample types.Sample)) { func (manager *StreamManagerCtx) RemoveListener(listener *func(sample types.Sample)) {
manager.emitMu.Lock() manager.emitMu.Lock()
defer manager.emitMu.Unlock() defer manager.emitMu.Unlock()
ptr := reflect.ValueOf(listener).Pointer() if listener != nil {
delete(manager.listeners, ptr) ptr := reflect.ValueOf(listener).Pointer()
delete(manager.listeners, ptr)
}
} }
func (manager *StreamManagerCtx) Start() error { func (manager *StreamManagerCtx) Start() error {

View File

@ -23,8 +23,8 @@ type ScreencastManager interface {
type StreamManager interface { type StreamManager interface {
Codec() codec.RTPCodec Codec() codec.RTPCodec
AddListener(listener func(sample Sample)) AddListener(listener *func(sample Sample))
RemoveListener(listener func(sample Sample)) RemoveListener(listener *func(sample Sample))
Start() error Start() error
Stop() Stop()

View File

@ -55,9 +55,9 @@ func (manager *WebRTCManagerCtx) Start() {
} }
} }
audio.AddListener(listener) audio.AddListener(&listener)
manager.unsubscribe = append(manager.unsubscribe, func(){ manager.unsubscribe = append(manager.unsubscribe, func(){
audio.RemoveListener(listener) audio.RemoveListener(&listener)
}) })
videoIDs := manager.capture.VideoIDs() videoIDs := manager.capture.VideoIDs()
@ -82,9 +82,9 @@ func (manager *WebRTCManagerCtx) Start() {
} }
} }
video.AddListener(listener) video.AddListener(&listener)
manager.unsubscribe = append(manager.unsubscribe, func(){ manager.unsubscribe = append(manager.unsubscribe, func(){
video.RemoveListener(listener) video.RemoveListener(&listener)
}) })
manager.videoTracks[videoID] = track manager.videoTracks[videoID] = track