-
Notifications
You must be signed in to change notification settings - Fork 85
/
streams.go
92 lines (71 loc) · 1.85 KB
/
streams.go
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
//
// Copyright (c) 2018- yutopp ([email protected])
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
//
package rtmp
import (
"sync"
"github.com/pkg/errors"
)
// ControlStreamID StreamID 0 is a control stream
const ControlStreamID = 0
type streams struct {
streams map[uint32]*Stream
m sync.Mutex
conn *Conn
}
func newStreams(conn *Conn) *streams {
return &streams{
streams: make(map[uint32]*Stream),
conn: conn,
}
}
func (ss *streams) Create(streamID uint32) (*Stream, error) {
ss.m.Lock()
defer ss.m.Unlock()
_, ok := ss.streams[streamID]
if ok {
return nil, errors.Errorf("Stream already exists: StreamID = %d", streamID)
}
if len(ss.streams) >= ss.conn.config.ControlState.MaxMessageStreams {
return nil, errors.Errorf(
"Creating message streams limit exceeded: Limit = %d",
ss.conn.config.ControlState.MaxMessageStreams,
)
}
ss.streams[streamID] = newStream(streamID, ss.conn)
return ss.streams[streamID], nil
}
func (ss *streams) CreateIfAvailable() (*Stream, error) {
for i := 0; i < ss.conn.config.ControlState.MaxMessageStreams; i++ {
s, err := ss.Create(uint32(i))
if err != nil {
continue
}
return s, nil
}
return nil, errors.Errorf(
"Creating streams limit exceeded: Limit = %d",
ss.conn.config.ControlState.MaxMessageStreams,
)
}
func (ss *streams) Delete(streamID uint32) error {
ss.m.Lock()
defer ss.m.Unlock()
s, ok := ss.streams[streamID]
if !ok {
return errors.Errorf("Stream not exists: StreamID = %d", streamID)
}
delete(ss.streams, s.streamID)
s.assumeClosed()
return nil
}
func (ss *streams) At(streamID uint32) (*Stream, error) {
stream, ok := ss.streams[streamID]
if !ok {
return nil, errors.Errorf("Stream is not found: StreamID = %d", streamID)
}
return stream, nil
}