-
Notifications
You must be signed in to change notification settings - Fork 42
/
direct_transport.go
120 lines (100 loc) · 3.92 KB
/
direct_transport.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
package mediasoup
import (
"github.com/go-logr/logr"
)
// DirectTransportOptions define options to create a DirectTransport.
type DirectTransportOptions struct {
// MaxMessageSize define maximum allowed size for direct messages sent from DataProducers.
// Default 262144.
MaxMessageSize uint32 `json:"maxMessageSize,omitempty"`
// AppData is custom application data.
AppData interface{} `json:"appData,omitempty"`
}
type directTransportData struct{}
// DirectTransport represents a direct connection between the mediasoup golang process and a Router
// instance in a mediasoup-worker subprocess.
//
// A direct transport can be used to directly send and receive data messages from/to golang by means
// of DataProducers and DataConsumers of type 'direct' created on a direct transport. Direct
// messages sent by a DataProducer in a direct transport can be consumed by endpoints connected
// through a SCTP capable transport (WebRtcTransport, PlainTransport, PipeTransport) and also by the
// golang application by means of a DataConsumer created on a DirectTransport (and vice-versa:
// messages sent over SCTP/DataChannel can be consumed by the golang application by means of a
// DataConsumer created on a DirectTransport).
// A direct transport can also be used to inject and directly consume RTP and RTCP packets in golang
// by using the producer.Send(rtpPacket) and consumer.On('rtp') API (plus
// directTransport.SendRtcp(rtcpPacket) and directTransport.On('rtcp') API).
//
// - @emits rtcp - (packet []byte)
// - @emits trace - (trace *TransportTraceEventData)
type DirectTransport struct {
ITransport
logger logr.Logger
internal internalData
channel *Channel
payloadChannel *PayloadChannel
onRtcp func([]byte)
}
func newDirectTransport(params transportParams) ITransport {
params.data = transportData{
transportType: TransportType_Direct,
}
params.logger = NewLogger("DirectTransport")
transport := &DirectTransport{
ITransport: newTransport(params),
logger: params.logger,
internal: params.internal,
channel: params.channel,
payloadChannel: params.payloadChannel,
}
transport.handleWorkerNotifications()
return transport
}
// Deprecated
//
// - @emits close
// - @emits newdataproducer - (dataProducer *DataProducer)
// - @emits newdataconsumer - (dataProducer *DataProducer)
// - @emits rtcp - (packet []byte)
// - @emits trace - (trace: *TransportTraceEventData)
func (transport *DirectTransport) Observer() IEventEmitter {
return transport.ITransport.Observer()
}
// Connect is a NO-OP method in DirectTransport.
func (transport *DirectTransport) Connect(TransportConnectOptions) error {
transport.logger.V(1).Info("connect()")
return nil
}
// SetMaxIncomingBitrate always returns error.
func (transport *DirectTransport) SetMaxIncomingBitrate(bitrate int) error {
return NewUnsupportedError("SetMaxIncomingBitrate() not implemented in DirectTransport")
}
// SendRtcp send RTCP packet.
func (transport *DirectTransport) SendRtcp(rtcpPacket []byte) error {
return transport.payloadChannel.Notify("transport.sendRtcp", transport.internal, "", rtcpPacket)
}
// OnRtcp set handler on "rtcp" event
func (transport *DirectTransport) OnRtcp(handler func(data []byte)) {
transport.onRtcp = handler
}
func (transport *DirectTransport) handleWorkerNotifications() {
transport.channel.Subscribe(transport.Id(), func(event string, data []byte) {
transport.ITransport.handleEvent(event, data)
})
transport.payloadChannel.Subscribe(transport.Id(), func(event string, data, payload []byte) {
switch event {
case "rtcp":
if transport.Closed() {
return
}
transport.SafeEmit("rtcp", payload)
// Emit observer event.
transport.Observer().SafeEmit("rtcp", payload)
if handler := transport.onRtcp; handler != nil {
handler(payload)
}
default:
transport.logger.Error(nil, "ignoring unknown event in payload channel listener", "event", event)
}
})
}