-
Notifications
You must be signed in to change notification settings - Fork 3
/
rtsp_mixin.go
155 lines (129 loc) · 3.49 KB
/
rtsp_mixin.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
package api
import (
"fmt"
"github.com/ReolinkCameraAPI/reolinkapigo/internal/pkg/enum"
"github.com/ReolinkCameraAPI/reolinkapigo/internal/pkg/network/rtsp"
"github.com/deepch/vdk/av"
)
type RtspMixin struct {
Host string
Username string
Password string
}
type RtspStream struct {
Frame []av.CodecData
Err error
}
func (rm *RtspMixin) OpenRtspStream(profile enum.RtspProfile, rtspOpts ...rtsp.OptionRtspClient) *rtsp.RtspClient {
endpoint := fmt.Sprintf("//h264Preview_01_%s", profile.Value())
opts := []rtsp.OptionRtspClient{
rtsp.WithEndpoint(endpoint),
rtsp.WithUsername(rm.Username),
rtsp.WithPassword(rm.Password),
}
// append external options over internal so that they are overwritten by the external configs.
opts = append(opts, rtspOpts...)
rtspClient := rtsp.NewRtspClient(rm.Host, opts...)
go rtspClient.OpenStream()
return rtspClient
}
// Open an RTSP stream using GoCV (openCv 4)
// This function returns a channel containing the frames as it is received from the camera
// https://adaickalavan.github.io/portfolio/rtsp_video_streaming/#gsc.tab=0
/*func (rm *RtspMixin) OpenRtspStream(port *int, profile enum.RtspProfile,
protocol *network.Protocol) func(handler *network.RestHandler) <-chan *RtspStream {
return func(handler *network.RestHandler) <-chan *RtspStream {
// creating unbuffered channel due to wanting frames in order
// this will block the next frame from being accessible
stream := make(chan *RtspStream)
go func() {
rtspUrl := fmt.Sprintf("%s:%s@%s", handler.Username, handler.Password, handler.Host)
if port != nil {
rtspUrl = fmt.Sprintf("%s:%d", rtspUrl, port)
} else {
rtspUrl = fmt.Sprintf("%s:%d", rtspUrl, 554)
}
rtspUrl = fmt.Sprintf("rtsp://%s//h264Preview_01_%s", rtspUrl, profile)
capture, err := gocv.OpenVideoCaptureWithAPI(rtspUrl, gocv.VideoCaptureFFmpeg)
if err != nil {
stream <- &RtspStream{
Frame: nil,
Err: err,
}
}
frame := gocv.NewMat()
for {
if !capture.IsOpened() {
close(stream)
return
}
if !capture.Read(&frame) {
continue
}
if frame.Empty() {
continue
}
stream <- &RtspStream{
Frame: &frame,
Err: nil,
}
}
}()
return stream
}
}*/
//func (rm *RtspMixin) rtspStream(port *int, profile enum.RtspProfile,
// protocol *network.Protocol) func(handler *network.RestHandler) {
// return func(handler *network.RestHandler) {
//
// // creating unbuffered channel due to wanting frames in order
// // this will block the next frame from being accessible
// stream := make(chan *RtspStream)
//
// rtspUrl := fmt.Sprintf("%s:%s@%s", rm.Username, rm.Password, handler.Host)
//
// if port != nil {
// rtspUrl = fmt.Sprintf("%s:%d", rtspUrl, port)
// } else {
// rtspUrl = fmt.Sprintf("%s:%d", rtspUrl, 554)
// }
//
// rtspUrl = fmt.Sprintf("rtsp://%s//h264Preview_01_%s", rtspUrl, profile.Value())
//
// go func() {
//
// for {
// rtsp.DebugRtp = true
// session, err := rtsp.Dial(rtspUrl)
//
// if err != nil {
// log.Println(err)
// time.Sleep(5 * time.Second)
// continue
// }
//
// session.RtpKeepAliveTimeout = 10 * time.Second
// if err != nil {
// log.Println(err)
// time.Sleep(5 * time.Second)
// continue
// }
//
// codec, err := session.Streams()
// if err != nil {
// log.Println(err)
// time.Sleep(5 * time.Second)
// continue
// }
//
// stream <- &RtspStream{
// Frame: codec,
// Err: nil,
// }
//
// }
//
// }()
//
// }
//}