forked from yutopp/go-rtmp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
stream_handler.go
213 lines (176 loc) · 5.56 KB
/
stream_handler.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
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
//
// 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 (
"fmt"
"sync"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/yutopp/go-rtmp/internal"
"github.com/yutopp/go-rtmp/message"
)
type streamState int
const (
streamStateUnknown streamState = iota
streamStateServerNotConnected
streamStateServerConnected
streamStateServerInactive
streamStateServerPublish
streamStateServerPlay
streamStateClientNotConnected
streamStateClientConnected
)
func (s streamState) String() string {
switch s {
case streamStateServerNotConnected:
return "NotConnected(Server)"
case streamStateServerConnected:
return "Connected(Server)"
case streamStateServerInactive:
return "Inactive(Server)"
case streamStateServerPublish:
return "Publish(Server)"
case streamStateServerPlay:
return "Play(Server)"
case streamStateClientNotConnected:
return "NotConnected(Client)"
case streamStateClientConnected:
return "Connected(Client)"
default:
return "<Unknown>"
}
}
// streamHandler A handler per streams.
// It holds a handler for each states and processes messages sent to the stream
type streamHandler struct {
stream *Stream
handler stateHandler // A handler for each states
state streamState
loggerEntry *logrus.Entry
m sync.Mutex
}
// newEntryHandler Create an incomplete new instance of entryHandler.
// msgHandler fields must be assigned by a caller of this function
func newStreamHandler(s *Stream) *streamHandler {
return &streamHandler{
stream: s,
}
}
func (h *streamHandler) Handle(chunkStreamID int, timestamp uint32, msg message.Message) error {
l := h.Logger()
switch msg := msg.(type) {
case *message.DataMessage:
return h.handleData(chunkStreamID, timestamp, msg)
case *message.CommandMessage:
return h.handleCommand(chunkStreamID, timestamp, msg)
case *message.SetChunkSize:
l.Infof("Handle SetChunkSize: Msg = %#v", msg)
return h.stream.streamer().PeerState().SetChunkSize(msg.ChunkSize)
case *message.WinAckSize:
l.Infof("Handle WinAckSize: Msg = %#v", msg)
return h.stream.streamer().PeerState().SetAckWindowSize(msg.Size)
default:
err := h.handler.onMessage(chunkStreamID, timestamp, msg)
if err == internal.ErrPassThroughMsg {
return h.stream.userHandler().OnUnknownMessage(timestamp, msg)
}
return err
}
}
func (h *streamHandler) ChangeState(state streamState) {
h.m.Lock()
defer h.m.Unlock()
prevState := h.State()
switch state {
case streamStateUnknown:
return // DO NOTHING
case streamStateServerNotConnected:
h.handler = &serverControlNotConnectedHandler{sh: h}
case streamStateServerConnected:
h.handler = &serverControlConnectedHandler{sh: h}
case streamStateServerInactive:
h.handler = &serverDataInactiveHandler{sh: h}
case streamStateServerPublish:
h.handler = &serverDataPublishHandler{sh: h}
case streamStateServerPlay:
h.handler = &serverDataPlayHandler{sh: h}
case streamStateClientNotConnected:
h.handler = &clientControlNotConnectedHandler{sh: h}
// case streamStateClientConnected:
// h.handler = &serverControlConnectedHandler{sh: h}
default:
panic("Unexpected")
}
h.state = state
l := h.Logger()
l.Infof("Change state: From = %s, To = %s", prevState, h.State())
}
func (h *streamHandler) State() streamState {
return h.state
}
func (h *streamHandler) Logger() *logrus.Entry {
if h.loggerEntry == nil {
h.loggerEntry = h.stream.logger().WithFields(logrus.Fields{
"stream_id": h.stream.streamID,
})
}
h.loggerEntry.Data["state"] = h.State()
return h.loggerEntry
}
func (h *streamHandler) handleData(
chunkStreamID int,
timestamp uint32,
dataMsg *message.DataMessage,
) error {
bodyDecoder := message.DataBodyDecoderFor(dataMsg.Name)
amfDec := message.NewAMFDecoder(dataMsg.Body, dataMsg.Encoding)
var value message.AMFConvertible
if err := bodyDecoder(dataMsg.Body, amfDec, &value); err != nil {
return err
}
err := h.handler.onData(chunkStreamID, timestamp, dataMsg, value)
if err == internal.ErrPassThroughMsg {
return h.stream.userHandler().OnUnknownDataMessage(timestamp, dataMsg)
}
return err
}
func (h *streamHandler) handleCommand(
chunkStreamID int,
timestamp uint32,
cmdMsg *message.CommandMessage,
) error {
switch cmdMsg.CommandName {
case "_result", "_error":
t, err := h.stream.transactions.At(cmdMsg.TransactionID)
if err != nil {
return errors.Wrap(err, "Got response to the unexpected transaction")
}
// Set result (NOTE: should use a mutex for it?)
t.Reply(cmdMsg.CommandName, cmdMsg.Encoding, cmdMsg.Body)
// Remove transacaction because this transaction is resolved
if err := h.stream.transactions.Delete(cmdMsg.TransactionID); err != nil {
return errors.Wrap(err, "Unexpected behavior: transaction is not found")
}
return nil
// TODO: Support onStatus
}
amfDec := message.NewAMFDecoder(cmdMsg.Body, cmdMsg.Encoding)
bodyDecoder := message.CmdBodyDecoderFor(cmdMsg.CommandName, cmdMsg.TransactionID)
fmt.Printf("handleCommand amfDec %+v\n", amfDec)
fmt.Printf("handleCommand bodyDecoder %+v\n", bodyDecoder)
fmt.Printf("handleCommand cmdMsg %+v\n", cmdMsg)
fmt.Printf("handleCommand cmdMsg.Body %+v\n", cmdMsg.Body)
var value message.AMFConvertible
if err := bodyDecoder(cmdMsg.Body, amfDec, &value); err != nil {
return err
}
err := h.handler.onCommand(chunkStreamID, timestamp, cmdMsg, value)
if err == internal.ErrPassThroughMsg {
return h.stream.userHandler().OnUnknownCommandMessage(timestamp, cmdMsg)
}
return err
}