-
Notifications
You must be signed in to change notification settings - Fork 74
/
subscriber.go
134 lines (130 loc) · 3.4 KB
/
subscriber.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
package rtsp
import (
"fmt"
"github.com/bluenviron/gortsplib/v4"
"github.com/bluenviron/gortsplib/v4/pkg/description"
"github.com/bluenviron/gortsplib/v4/pkg/format"
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4audio"
. "m7s.live/engine/v4"
"m7s.live/engine/v4/codec"
"m7s.live/engine/v4/track"
)
type RTSPSubscriber struct {
Subscriber
RTSPIO
}
func (s *RTSPSubscriber) OnEvent(event any) {
switch v := event.(type) {
case *track.Video:
if s.Video != nil {
return
}
switch v.CodecID {
case codec.CodecID_H264:
s.videoTrack = &description.Media{
Type: description.MediaTypeVideo,
Formats: []format.Format{&format.H264{
PacketizationMode: 1,
PayloadTyp: v.PayloadType,
SPS: v.ParamaterSets[0],
PPS: v.ParamaterSets[1],
}},
}
case codec.CodecID_H265:
s.videoTrack = &description.Media{
Type: description.MediaTypeVideo,
Formats: []format.Format{&format.H265{
PayloadTyp: v.PayloadType,
VPS: v.ParamaterSets[0],
SPS: v.ParamaterSets[1],
PPS: v.ParamaterSets[2],
}},
}
case codec.CodecID_AV1:
var idx, profile, tail int
idx = int(v.ParamaterSets[1][0])
profile = int(v.ParamaterSets[1][1])
tail = int(v.ParamaterSets[1][2])
s.videoTrack = &description.Media{
Type: description.MediaTypeVideo,
Formats: []format.Format{&format.AV1{
PayloadTyp: v.PayloadType,
LevelIdx: &idx,
Profile: &profile,
Tier: &tail,
}},
}
}
if s.videoTrack != nil {
s.tracks = append(s.tracks, s.videoTrack)
s.AddTrack(v)
}
case *track.Audio:
if s.Audio != nil {
return
}
switch v.CodecID {
case codec.CodecID_AAC:
f := v.AACFormat
if f == nil {
f = &format.MPEG4Audio{
PayloadTyp: v.PayloadType,
Config: &mpeg4audio.Config{
Type: mpeg4audio.ObjectTypeAACLC,
SampleRate: int(v.SampleRate),
ChannelCount: int(v.Channels),
},
SizeLength: v.AACDecoder.SizeLength,
IndexLength: v.AACDecoder.IndexLength,
IndexDeltaLength: v.AACDecoder.IndexDeltaLength,
}
}
s.audioTrack = &description.Media{
Type: description.MediaTypeAudio,
Formats: []format.Format{f},
}
case codec.CodecID_PCMA:
s.audioTrack = &description.Media{
Type: description.MediaTypeAudio,
Formats: []format.Format{&format.Generic{
PayloadTyp: v.PayloadType,
ClockRat: int(v.SampleRate),
RTPMa: fmt.Sprintf("PCMA/%d", v.SampleRate),
}},
}
case codec.CodecID_PCMU:
s.audioTrack = &description.Media{
Type: description.MediaTypeAudio,
Formats: []format.Format{&format.Generic{
PayloadTyp: v.PayloadType,
ClockRat: int(v.SampleRate),
RTPMa: fmt.Sprintf("PCMU/%d", v.SampleRate),
}},
}
case codec.CodecID_OPUS:
s.audioTrack = &description.Media{
Type: description.MediaTypeAudio,
Formats: []format.Format{&format.Opus{
PayloadTyp: v.PayloadType,
}},
}
}
if s.audioTrack != nil {
s.tracks = append(s.tracks, s.audioTrack)
s.AddTrack(v)
}
case ISubscriber:
s.session = &description.Session{
Medias: s.tracks,
}
if s.server != nil {
s.stream = gortsplib.NewServerStream(s.server, s.session)
}
case VideoRTP:
s.stream.WritePacketRTP(s.videoTrack, v.Packet)
case AudioRTP:
s.stream.WritePacketRTP(s.audioTrack, v.Packet)
default:
s.Subscriber.OnEvent(event)
}
}