2020-05-12 09:46:20 +02:00
|
|
|
/**
|
|
|
|
* Standalone signaling server for the Nextcloud Spreed app.
|
|
|
|
* Copyright (C) 2019 struktur AG
|
|
|
|
*
|
|
|
|
* @author Joachim Bauch <bauch@struktur.de>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package signaling
|
|
|
|
|
|
|
|
import (
|
2020-08-31 13:58:28 +02:00
|
|
|
"context"
|
2020-05-12 09:46:20 +02:00
|
|
|
"fmt"
|
2021-11-08 12:06:59 +01:00
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2020-05-12 09:46:20 +02:00
|
|
|
|
2020-08-31 13:07:03 +02:00
|
|
|
"github.com/dlintw/goconf"
|
2020-05-12 09:46:20 +02:00
|
|
|
)
|
|
|
|
|
2022-01-10 14:30:35 +01:00
|
|
|
const (
|
|
|
|
TestMaxBitrateScreen = 12345678
|
|
|
|
TestMaxBitrateVideo = 23456789
|
|
|
|
)
|
|
|
|
|
2020-05-12 09:46:20 +02:00
|
|
|
type TestMCU struct {
|
2021-11-08 12:06:59 +01:00
|
|
|
mu sync.Mutex
|
|
|
|
publishers map[string]*TestMCUPublisher
|
2020-05-12 09:46:20 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:06:59 +01:00
|
|
|
func NewTestMCU() (*TestMCU, error) {
|
|
|
|
return &TestMCU{
|
|
|
|
publishers: make(map[string]*TestMCUPublisher),
|
|
|
|
}, nil
|
2020-05-12 09:46:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TestMCU) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TestMCU) Stop() {
|
|
|
|
}
|
|
|
|
|
2020-08-31 13:07:03 +02:00
|
|
|
func (m *TestMCU) Reload(config *goconf.ConfigFile) {
|
|
|
|
}
|
|
|
|
|
2020-08-07 10:23:47 +02:00
|
|
|
func (m *TestMCU) SetOnConnected(f func()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TestMCU) SetOnDisconnected(f func()) {
|
|
|
|
}
|
|
|
|
|
2020-05-28 16:02:04 +02:00
|
|
|
func (m *TestMCU) GetStats() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:06:59 +01:00
|
|
|
func (m *TestMCU) NewPublisher(ctx context.Context, listener McuListener, id string, streamType string, bitrate int, mediaTypes MediaType, initiator McuInitiator) (McuPublisher, error) {
|
2022-01-10 14:30:35 +01:00
|
|
|
var maxBitrate int
|
|
|
|
if streamType == streamTypeScreen {
|
|
|
|
maxBitrate = TestMaxBitrateScreen
|
|
|
|
} else {
|
|
|
|
maxBitrate = TestMaxBitrateVideo
|
|
|
|
}
|
|
|
|
if bitrate <= 0 {
|
|
|
|
bitrate = maxBitrate
|
|
|
|
} else if bitrate > maxBitrate {
|
|
|
|
bitrate = maxBitrate
|
|
|
|
}
|
2021-11-08 12:06:59 +01:00
|
|
|
pub := &TestMCUPublisher{
|
|
|
|
TestMCUClient: TestMCUClient{
|
|
|
|
id: id,
|
|
|
|
streamType: streamType,
|
|
|
|
},
|
|
|
|
|
|
|
|
mediaTypes: mediaTypes,
|
2022-01-10 14:30:35 +01:00
|
|
|
bitrate: bitrate,
|
2021-11-08 12:06:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
m.publishers[id] = pub
|
|
|
|
return pub, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TestMCU) GetPublishers() map[string]*TestMCUPublisher {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
result := make(map[string]*TestMCUPublisher, len(m.publishers))
|
|
|
|
for id, pub := range m.publishers {
|
|
|
|
result[id] = pub
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TestMCU) GetPublisher(id string) *TestMCUPublisher {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
return m.publishers[id]
|
2020-05-12 09:46:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TestMCU) NewSubscriber(ctx context.Context, listener McuListener, publisher string, streamType string) (McuSubscriber, error) {
|
|
|
|
return nil, fmt.Errorf("Not implemented")
|
|
|
|
}
|
2021-11-08 12:06:59 +01:00
|
|
|
|
|
|
|
type TestMCUClient struct {
|
|
|
|
closed int32
|
|
|
|
|
|
|
|
id string
|
|
|
|
streamType string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestMCUClient) Id() string {
|
|
|
|
return c.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestMCUClient) StreamType() string {
|
|
|
|
return c.streamType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestMCUClient) Close(ctx context.Context) {
|
|
|
|
log.Printf("Close MCU client %s", c.id)
|
|
|
|
atomic.StoreInt32(&c.closed, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TestMCUClient) isClosed() bool {
|
|
|
|
return atomic.LoadInt32(&c.closed) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestMCUPublisher struct {
|
|
|
|
TestMCUClient
|
|
|
|
|
|
|
|
mediaTypes MediaType
|
2022-01-10 14:30:35 +01:00
|
|
|
bitrate int
|
2021-11-08 12:06:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *TestMCUPublisher) HasMedia(mt MediaType) bool {
|
|
|
|
return (p.mediaTypes & mt) == mt
|
|
|
|
}
|
|
|
|
|
2022-04-05 19:27:37 +02:00
|
|
|
func (p *TestMCUPublisher) SetMedia(mt MediaType) {
|
|
|
|
p.mediaTypes = mt
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:06:59 +01:00
|
|
|
func (p *TestMCUPublisher) SendMessage(ctx context.Context, message *MessageClientMessage, data *MessageClientMessageData, callback func(error, map[string]interface{})) {
|
|
|
|
go func() {
|
|
|
|
if p.isClosed() {
|
|
|
|
callback(fmt.Errorf("Already closed"), nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch data.Type {
|
|
|
|
case "offer":
|
|
|
|
sdp := data.Payload["sdp"]
|
|
|
|
if sdp, ok := sdp.(string); ok {
|
|
|
|
if sdp == MockSdpOfferAudioOnly {
|
|
|
|
callback(nil, map[string]interface{}{
|
|
|
|
"type": "answer",
|
|
|
|
"sdp": MockSdpAnswerAudioOnly,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
} else if sdp == MockSdpOfferAudioAndVideo {
|
|
|
|
callback(nil, map[string]interface{}{
|
|
|
|
"type": "answer",
|
|
|
|
"sdp": MockSdpAnswerAudioAndVideo,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback(fmt.Errorf("Offer payload %+v is not implemented", data.Payload), nil)
|
|
|
|
default:
|
|
|
|
callback(fmt.Errorf("Message type %s is not implemented", data.Type), nil)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|