1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
/**
* 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 (
"context"
"fmt"
"log"
"sync"
"sync/atomic"
"github.com/dlintw/goconf"
)
const (
TestMaxBitrateScreen = 12345678
TestMaxBitrateVideo = 23456789
)
type TestMCU struct {
mu sync.Mutex
publishers map[string]*TestMCUPublisher
subscribers map[string]*TestMCUSubscriber
}
func NewTestMCU() (*TestMCU, error) {
return &TestMCU{
publishers: make(map[string]*TestMCUPublisher),
subscribers: make(map[string]*TestMCUSubscriber),
}, nil
}
func (m *TestMCU) Start() error {
return nil
}
func (m *TestMCU) Stop() {
}
func (m *TestMCU) Reload(config *goconf.ConfigFile) {
}
func (m *TestMCU) SetOnConnected(f func()) {
}
func (m *TestMCU) SetOnDisconnected(f func()) {
}
func (m *TestMCU) GetStats() interface{} {
return nil
}
func (m *TestMCU) NewPublisher(ctx context.Context, listener McuListener, id string, sid string, streamType StreamType, bitrate int, mediaTypes MediaType, initiator McuInitiator) (McuPublisher, error) {
var maxBitrate int
if streamType == StreamTypeScreen {
maxBitrate = TestMaxBitrateScreen
} else {
maxBitrate = TestMaxBitrateVideo
}
if bitrate <= 0 {
bitrate = maxBitrate
} else if bitrate > maxBitrate {
bitrate = maxBitrate
}
pub := &TestMCUPublisher{
TestMCUClient: TestMCUClient{
id: id,
sid: sid,
streamType: streamType,
},
mediaTypes: mediaTypes,
bitrate: bitrate,
}
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]
}
func (m *TestMCU) NewSubscriber(ctx context.Context, listener McuListener, publisher string, streamType StreamType) (McuSubscriber, error) {
m.mu.Lock()
defer m.mu.Unlock()
pub := m.publishers[publisher]
if pub == nil {
return nil, fmt.Errorf("Waiting for publisher not implemented yet")
}
id := newRandomString(8)
sub := &TestMCUSubscriber{
TestMCUClient: TestMCUClient{
id: id,
streamType: streamType,
},
publisher: pub,
}
return sub, nil
}
type TestMCUClient struct {
closed atomic.Bool
id string
sid string
streamType StreamType
}
func (c *TestMCUClient) Id() string {
return c.id
}
func (c *TestMCUClient) Sid() string {
return c.sid
}
func (c *TestMCUClient) StreamType() StreamType {
return c.streamType
}
func (c *TestMCUClient) MaxBitrate() int {
return 0
}
func (c *TestMCUClient) Close(ctx context.Context) {
if c.closed.CompareAndSwap(false, true) {
log.Printf("Close MCU client %s", c.id)
}
}
func (c *TestMCUClient) isClosed() bool {
return c.closed.Load()
}
type TestMCUPublisher struct {
TestMCUClient
mediaTypes MediaType
bitrate int
sdp string
}
func (p *TestMCUPublisher) HasMedia(mt MediaType) bool {
return (p.mediaTypes & mt) == mt
}
func (p *TestMCUPublisher) SetMedia(mt MediaType) {
p.mediaTypes = mt
}
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 {
p.sdp = sdp
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)
}
}()
}
type TestMCUSubscriber struct {
TestMCUClient
publisher *TestMCUPublisher
}
func (s *TestMCUSubscriber) Publisher() string {
return s.publisher.id
}
func (s *TestMCUSubscriber) SendMessage(ctx context.Context, message *MessageClientMessage, data *MessageClientMessageData, callback func(error, map[string]interface{})) {
go func() {
if s.isClosed() {
callback(fmt.Errorf("Already closed"), nil)
return
}
switch data.Type {
case "requestoffer":
fallthrough
case "sendoffer":
sdp := s.publisher.sdp
if sdp == "" {
callback(fmt.Errorf("Publisher not sending (no SDP)"), nil)
return
}
callback(nil, map[string]interface{}{
"type": "offer",
"sdp": sdp,
})
default:
callback(fmt.Errorf("Message type %s is not implemented", data.Type), nil)
}
}()
}
|