/
stream.go
177 lines (152 loc) 路 4.01 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
package dahua
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/ItsNotGoodName/ipcmanview/internal/core"
"github.com/ItsNotGoodName/ipcmanview/internal/mediamtx"
"github.com/ItsNotGoodName/ipcmanview/internal/models"
"github.com/ItsNotGoodName/ipcmanview/internal/repo"
"github.com/ItsNotGoodName/ipcmanview/pkg/dahuarpc"
"github.com/ItsNotGoodName/ipcmanview/pkg/dahuarpc/modules/encode"
)
func UpdateStream(ctx context.Context, stream repo.DahuaStream, arg repo.DahuaUpdateStreamParams) (repo.DahuaStream, error) {
if _, err := core.AssertAdmin(ctx); err != nil {
return repo.DahuaStream{}, err
}
return app.DB.C().DahuaUpdateStream(ctx, arg)
}
func DeleteStream(ctx context.Context, stream repo.DahuaStream) error {
if _, err := core.AssertAdmin(ctx); err != nil {
return err
}
if stream.Internal {
return fmt.Errorf("cannot delete internal stream")
}
return app.DB.C().DahuaDeleteStream(ctx, stream.ID)
}
func SupportStream(feature models.DahuaFeature) bool {
return feature.EQ(models.DahuaFeature_Camera)
}
// SyncStreams fetches streams from device and sync them with database.
func SyncStreams(ctx context.Context, deviceID int64, conn dahuarpc.Conn) error {
if _, err := core.AssertAdmin(ctx); err != nil {
return err
}
caps, err := encode.GetCaps(ctx, conn, 1)
if err != nil {
return err
}
subtypes := 1
if caps.MaxExtraStream > 0 && caps.MaxExtraStream < 10 {
subtypes += caps.MaxExtraStream
}
for channelIndex, device := range caps.VideoEncodeDevices {
names := make([]string, subtypes)
for i, v := range device.SupportDynamicBitrate {
if i < len(names) {
names[i] = v.Stream
}
}
args := []upsertStreamsParams{}
for i := 0; i < subtypes; i++ {
arg := upsertStreamsParams{
Channel: int64(channelIndex + 1),
Subtype: int64(i),
Name: names[i],
}
args = append(args, arg)
}
err := upsertStreams(ctx, deviceID, args)
if err != nil {
return err
}
}
return nil
}
type upsertStreamsParams struct {
Channel int64
Subtype int64
Name string
}
func upsertStreams(ctx context.Context, deviceID int64, arg []upsertStreamsParams) error {
tx, err := app.DB.BeginTx(ctx, true)
if err != nil {
return err
}
defer tx.Rollback()
err = tx.C().DahuaUpdateStreamForInternal(ctx, deviceID)
if err != nil {
return err
}
ids := make([]int64, 0, len(arg))
for _, arg := range arg {
id, err := tx.C().DahuaCreateStreamForInternal(ctx, repo.DahuaCreateStreamForInternalParams{
DeviceID: deviceID,
Channel: arg.Channel,
Subtype: arg.Subtype,
Name: arg.Name,
})
if err != nil {
return err
}
ids = append(ids, id)
}
return tx.Commit()
}
// PushStreams pushes streams to mediamtx
func PushStreams(ctx context.Context, deviceID int64) error {
if _, err := core.AssertAdmin(ctx); err != nil {
return err
}
device, err := GetDevice(ctx, deviceID)
if err != nil {
return err
}
streams, err := app.DB.C().DahuaListStreamsByDevice(ctx, deviceID)
if err != nil {
return err
}
for _, stream := range streams {
name := app.MediamtxConfig.DahuaEmbedPath(stream)
rtspURL := GetLiveRTSPURL(GetLiveRTSPURLParams{
Username: device.Username,
Password: device.Password,
Host: device.Ip,
Port: 554,
Channel: int(stream.Channel),
Subtype: int(stream.Subtype),
})
rtspTransport := "tcp"
pathConf := mediamtx.PathConf{
Source: &rtspURL,
RtspTransport: &rtspTransport,
}
rsp, err := app.MediamtxClient.ConfigPathsGet(ctx, name)
if err != nil {
return err
}
res, err := mediamtx.ParseConfigPathsGetResponse(rsp)
if err != nil {
return err
}
switch res.StatusCode() {
case http.StatusOK:
rsp, err := app.MediamtxClient.ConfigPathsPatch(ctx, name, pathConf)
if err != nil {
return err
}
rsp.Body.Close()
case http.StatusNotFound, http.StatusInternalServerError:
rsp, err := app.MediamtxClient.ConfigPathsAdd(ctx, name, pathConf)
if err != nil {
return err
}
rsp.Body.Close()
default:
return errors.New(string(res.Body))
}
}
return nil
}