Add callbacks for when MCU connection is established/list.

This commit is contained in:
Joachim Bauch 2020-08-07 10:23:47 +02:00
parent 5a553fcc2d
commit f4d4d5fb4d
Failed to extract signature
3 changed files with 54 additions and 3 deletions

View file

@ -22,6 +22,8 @@
package signaling package signaling
import ( import (
"fmt"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
@ -31,8 +33,12 @@ const (
McuTypeDefault = McuTypeJanus McuTypeDefault = McuTypeJanus
) )
var (
ErrNotConnected = fmt.Errorf("Not connected")
)
type McuListener interface { type McuListener interface {
Session PublicId() string
OnIceCandidate(client McuClient, candidate interface{}) OnIceCandidate(client McuClient, candidate interface{})
OnIceCompleted(client McuClient) OnIceCompleted(client McuClient)
@ -45,6 +51,9 @@ type Mcu interface {
Start() error Start() error
Stop() Stop()
SetOnConnected(func())
SetOnDisconnected(func())
GetStats() interface{} GetStats() interface{}
NewPublisher(ctx context.Context, listener McuListener, id string, streamType string) (McuPublisher, error) NewPublisher(ctx context.Context, listener McuListener, id string, streamType string) (McuPublisher, error)

View file

@ -28,6 +28,7 @@ import (
"reflect" "reflect"
"strconv" "strconv"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/dlintw/goconf" "github.com/dlintw/goconf"
@ -64,8 +65,6 @@ var (
videoPublisherUserId: streamTypeVideo, videoPublisherUserId: streamTypeVideo,
screenPublisherUserId: streamTypeScreen, screenPublisherUserId: streamTypeScreen,
} }
ErrNotConnected = fmt.Errorf("Not connected")
) )
func getPluginValue(data janus.PluginData, pluginName string, key string) interface{} { func getPluginValue(data janus.PluginData, pluginName string, key string) interface{} {
@ -161,8 +160,13 @@ type mcuJanus struct {
reconnectInterval time.Duration reconnectInterval time.Duration
connectedSince time.Time connectedSince time.Time
onConnected atomic.Value
onDisconnected atomic.Value
} }
func emptyOnConnected() {}
func emptyOnDisconnected() {}
func NewMcuJanus(url string, config *goconf.ConfigFile, nats NatsClient) (Mcu, error) { func NewMcuJanus(url string, config *goconf.ConfigFile, nats NatsClient) (Mcu, error) {
maxStreamBitrate, _ := config.GetInt("mcu", "maxstreambitrate") maxStreamBitrate, _ := config.GetInt("mcu", "maxstreambitrate")
if maxStreamBitrate <= 0 { if maxStreamBitrate <= 0 {
@ -190,6 +194,9 @@ func NewMcuJanus(url string, config *goconf.ConfigFile, nats NatsClient) (Mcu, e
reconnectInterval: initialReconnectInterval, reconnectInterval: initialReconnectInterval,
} }
mcu.onConnected.Store(emptyOnConnected)
mcu.onDisconnected.Store(emptyOnDisconnected)
mcu.reconnectTimer = time.AfterFunc(mcu.reconnectInterval, mcu.doReconnect) mcu.reconnectTimer = time.AfterFunc(mcu.reconnectInterval, mcu.doReconnect)
mcu.reconnectTimer.Stop() mcu.reconnectTimer.Stop()
if err := mcu.reconnect(); err != nil { if err := mcu.reconnect(); err != nil {
@ -269,6 +276,7 @@ func (m *mcuJanus) scheduleReconnect(err error) {
func (m *mcuJanus) ConnectionInterrupted() { func (m *mcuJanus) ConnectionInterrupted() {
m.scheduleReconnect(nil) m.scheduleReconnect(nil)
m.notifyOnDisconnected()
} }
func (m *mcuJanus) Start() error { func (m *mcuJanus) Start() error {
@ -314,6 +322,8 @@ func (m *mcuJanus) Start() error {
log.Println("Created Janus handle", m.handle.Id) log.Println("Created Janus handle", m.handle.Id)
go m.run() go m.run()
m.notifyOnConnected()
return nil return nil
} }
@ -349,6 +359,32 @@ func (m *mcuJanus) Stop() {
m.reconnectTimer.Stop() m.reconnectTimer.Stop()
} }
func (m *mcuJanus) SetOnConnected(f func()) {
if f == nil {
f = emptyOnConnected
}
m.onConnected.Store(f)
}
func (m *mcuJanus) notifyOnConnected() {
f := m.onConnected.Load().(func())
f()
}
func (m *mcuJanus) SetOnDisconnected(f func()) {
if f == nil {
f = emptyOnDisconnected
}
m.onDisconnected.Store(f)
}
func (m *mcuJanus) notifyOnDisconnected() {
f := m.onDisconnected.Load().(func())
f()
}
type mcuJanusConnectionStats struct { type mcuJanusConnectionStats struct {
Url string `json:"url"` Url string `json:"url"`
Connected bool `json:"connected"` Connected bool `json:"connected"`

View file

@ -41,6 +41,12 @@ func (m *TestMCU) Start() error {
func (m *TestMCU) Stop() { func (m *TestMCU) Stop() {
} }
func (m *TestMCU) SetOnConnected(f func()) {
}
func (m *TestMCU) SetOnDisconnected(f func()) {
}
func (m *TestMCU) GetStats() interface{} { func (m *TestMCU) GetStats() interface{} {
return nil return nil
} }