-
Notifications
You must be signed in to change notification settings - Fork 10
/
stream.go
204 lines (190 loc) · 8.58 KB
/
stream.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
package videoserver
import (
"time"
"github.com/deepch/vdk/format/rtspv2"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
const (
pingDuration = 15 * time.Second
pingDurationRestart = pingDuration + 1*time.Second
dialTimeoutDuration = 33 * time.Second
readTimeoutDuration = 33 * time.Second
)
type StopSignal uint8
const (
STOP_SIGNAL_ERR = StopSignal(iota)
STOP_SIGNAL_NO_VIDEO
STOP_SIGNAL_DISCONNECT
STOP_SIGNAL_STOP_DIAL
)
// runStream runs RTSP grabbing process
func (app *Application) runStream(streamID uuid.UUID, url string, hlsEnabled, archiveEnabled bool, streamVerboseLevel VerboseLevel) error {
var stopHlsCast, stopMP4Cast chan StopSignal
if hlsEnabled {
stopHlsCast = make(chan StopSignal, 1)
}
if archiveEnabled {
stopMP4Cast = make(chan StopSignal, 1)
}
errorSignal := make(chan error, 1)
if streamVerboseLevel > VERBOSE_NONE {
log.Info().Str("scope", SCOPE_STREAMING).Str("event", EVENT_STREAMING_DIAL).Str("stream_id", streamID.String()).Str("stream_url", url).Bool("hls_enabled", hlsEnabled).Msg("Trying to dial")
}
session, err := rtspv2.Dial(rtspv2.RTSPClientOptions{
URL: url,
DisableAudio: true,
DialTimeout: dialTimeoutDuration,
ReadWriteTimeout: readTimeoutDuration,
Debug: false,
})
if err != nil {
return errors.Wrapf(err, "Can't connect to stream '%s'", url)
}
defer func() {
if streamVerboseLevel > VERBOSE_NONE {
log.Info().Str("scope", SCOPE_STREAMING).Str("event", EVENT_STREAMING_DIAL).Str("stream_id", streamID.String()).Str("stream_url", url).Msg("Closing connection")
}
if hlsEnabled {
stopHlsCast <- STOP_SIGNAL_STOP_DIAL
}
if archiveEnabled {
stopMP4Cast <- STOP_SIGNAL_STOP_DIAL
}
session.Close()
}()
if len(session.CodecData) != 0 {
if streamVerboseLevel > VERBOSE_NONE {
log.Info().Str("scope", SCOPE_STREAMING).Str("event", EVENT_STREAMING_CODEC_MET).Str("stream_id", streamID.String()).Str("stream_url", url).Bool("hls_enabled", hlsEnabled).Any("codec_data", session.CodecData).Msg("Found codec. Adding this one")
}
err = app.Streams.AddCodecForStream(streamID, session.CodecData)
if err != nil {
return errors.Wrapf(err, "Can't update codec data for stream %s on empty codecs", streamID)
}
if streamVerboseLevel > VERBOSE_NONE {
log.Info().Str("scope", SCOPE_STREAMING).Str("event", EVENT_STREAMING_STATUS_UPDATE).Str("stream_id", streamID.String()).Str("stream_url", url).Bool("hls_enabled", hlsEnabled).Msg("Update stream status")
}
err = app.Streams.UpdateStreamStatus(streamID, true)
if err != nil {
return errors.Wrapf(err, "Can't update status for stream %s on empty codecs", streamID)
}
}
isAudioOnly := false
if len(session.CodecData) == 1 {
if session.CodecData[0].Type().IsAudio() {
if streamVerboseLevel > VERBOSE_NONE {
log.Info().Str("scope", SCOPE_STREAMING).Str("event", EVENT_STREAMING_AUDIO_MET).Str("stream_id", streamID.String()).Str("stream_url", url).Bool("hls_enabled", hlsEnabled).Msg("Only audio")
}
isAudioOnly = true
}
}
if hlsEnabled {
if streamVerboseLevel > VERBOSE_NONE {
log.Info().Str("scope", SCOPE_STREAMING).Str("event", EVENT_STREAMING_HLS_CAST).Str("stream_id", streamID.String()).Str("stream_url", url).Msg("Need to start casting for HLS")
}
err = app.startHlsCast(streamID, stopHlsCast)
if err != nil {
if streamVerboseLevel > VERBOSE_NONE {
log.Warn().Str("scope", SCOPE_STREAMING).Str("event", EVENT_STREAMING_HLS_CAST).Str("stream_id", streamID.String()).Str("stream_url", url).Msg("Can't start HLS casting")
}
}
}
if archiveEnabled {
if streamVerboseLevel > VERBOSE_NONE {
log.Info().Str("scope", SCOPE_STREAMING).Str("event", EVENT_STREAMING_MP4_CAST).Str("stream_id", streamID.String()).Str("stream_url", url).Msg("Need to start casting to MP4 archive")
}
archive := app.Streams.GetStreamArchiveStorage(streamID)
if archive == nil {
log.Warn().Str("scope", SCOPE_STREAMING).Str("event", EVENT_STREAMING_MP4_CAST).Str("stream_id", streamID.String()).Str("stream_url", url).Msg("Empty archive configuration for the given stream")
} else {
err = app.startMP4Cast(archive, streamID, stopMP4Cast, errorSignal, streamVerboseLevel)
if err != nil {
if streamVerboseLevel > VERBOSE_NONE {
log.Warn().Str("scope", SCOPE_STREAMING).Str("event", EVENT_STREAMING_MP4_CAST).Str("stream_id", streamID.String()).Str("stream_url", url).Msg("Can't start MP4 archive process")
}
}
}
}
pingStream := time.NewTimer(pingDuration)
for {
select {
case <-pingStream.C:
log.Error().Err(ErrStreamHasNoVideo).Str("scope", SCOPE_STREAMING).Str("event", EVENT_STREAMING_EXIT_SIGNAL).Str("stream_id", streamID.String()).Str("stream_url", url).Msg("Stream has no video")
if hlsEnabled {
stopHlsCast <- STOP_SIGNAL_NO_VIDEO
}
if archiveEnabled {
stopMP4Cast <- STOP_SIGNAL_NO_VIDEO
}
return errors.Wrapf(ErrStreamHasNoVideo, "URL is '%s'", url)
case signals := <-session.Signals:
switch signals {
case rtspv2.SignalCodecUpdate:
if streamVerboseLevel > VERBOSE_NONE {
log.Info().Str("scope", SCOPE_STREAMING).Str("event", EVENT_STREAMING_CODEC_UPDATE_SIGNAL).Str("stream_id", streamID.String()).Str("stream_url", url).Any("codec_data", session.CodecData).Msg("Recieved update codec signal")
}
err = app.Streams.AddCodecForStream(streamID, session.CodecData)
if err != nil {
return errors.Wrapf(err, "Can't update codec data for stream %s on codecs update signal", streamID)
}
err = app.Streams.UpdateStreamStatus(streamID, true)
if err != nil {
return errors.Wrapf(err, "Can't update status for stream %s after codecs update", streamID)
}
case rtspv2.SignalStreamRTPStop:
if streamVerboseLevel > VERBOSE_NONE {
log.Info().Str("scope", SCOPE_STREAMING).Str("event", EVENT_STREAMING_STOP_SIGNAL).Str("stream_id", streamID.String()).Str("stream_url", url).Msg("Recieved stop signal")
}
if hlsEnabled {
stopHlsCast <- STOP_SIGNAL_DISCONNECT
}
if archiveEnabled {
stopMP4Cast <- STOP_SIGNAL_DISCONNECT
}
err = app.Streams.UpdateStreamStatus(streamID, false)
if err != nil {
return errors.Wrapf(err, "Can't update status for stream %s after RTP stops", streamID)
}
return errors.Wrapf(ErrStreamDisconnected, "URL is '%s'", url)
default:
log.Info().Str("warn", SCOPE_STREAMING).Str("event", EVENT_STREAMING_UNKNOWN_SIGNAL).Str("stream_id", streamID.String()).Str("stream_url", url).Int("signal", signals).Msg("Other signal")
}
case packetAV := <-session.OutgoingPacketQueue:
if streamVerboseLevel > VERBOSE_ADD {
log.Info().Str("scope", SCOPE_STREAMING).Str("event", EVENT_STREAMING_PACKET_SIGNAL).Str("stream_id", streamID.String()).Str("stream_url", url).Msg("Recieved outgoing packet from queue")
}
if isAudioOnly || packetAV.IsKeyFrame {
if streamVerboseLevel > VERBOSE_ADD {
log.Info().Str("scope", SCOPE_STREAMING).Str("event", EVENT_STREAMING_PACKET_SIGNAL).Str("stream_id", streamID.String()).Str("stream_url", url).Bool("only_audio", isAudioOnly).Bool("is_keyframe", packetAV.IsKeyFrame).Msg("Need to reset ping for stream")
}
pingStream.Reset(pingDurationRestart)
}
if streamVerboseLevel > VERBOSE_ADD {
log.Info().Str("scope", SCOPE_STREAMING).Str("event", EVENT_STREAMING_PACKET_SIGNAL).Str("stream_id", streamID.String()).Str("stream_url", url).Bool("only_audio", isAudioOnly).Bool("is_keyframe", packetAV.IsKeyFrame).Msg("Casting packet")
}
err = app.Streams.CastPacket(streamID, *packetAV, hlsEnabled, archiveEnabled)
if err != nil {
if hlsEnabled {
if streamVerboseLevel > VERBOSE_NONE {
log.Info().Str("scope", SCOPE_STREAMING).Str("event", EVENT_STREAMING_PACKET_SIGNAL).Str("stream_id", streamID.String()).Str("stream_url", url).Bool("only_audio", isAudioOnly).Bool("is_keyframe", packetAV.IsKeyFrame).Msg("Need to stop HLS cast")
}
stopHlsCast <- STOP_SIGNAL_ERR
}
if archiveEnabled {
if streamVerboseLevel > VERBOSE_NONE {
log.Info().Str("scope", SCOPE_STREAMING).Str("event", EVENT_STREAMING_PACKET_SIGNAL).Str("stream_id", streamID.String()).Str("stream_url", url).Bool("only_audio", isAudioOnly).Bool("is_keyframe", packetAV.IsKeyFrame).Msg("Need to stop MP4 cast")
}
stopMP4Cast <- STOP_SIGNAL_ERR
}
errStatus := app.Streams.UpdateStreamStatus(streamID, false)
if errStatus != nil {
return errors.Wrapf(err, "Can't update status for stream %s after casting", streamID)
}
return errors.Wrapf(err, "Can't cast packet %s (%s)", streamID, url)
}
case errS := <-errorSignal:
return errors.Wrapf(errS, "Recieved error signal from MP4 casting")
}
}
}