-
Notifications
You must be signed in to change notification settings - Fork 458
/
t_http_an__.go
167 lines (143 loc) · 3.68 KB
/
t_http_an__.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
// Copyright 2022, Chef. All rights reserved.
// https://github.com/q191201771/lal
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package base
// t_http_an__.go
//
// http-api和http-notify的共用部分
//
const (
// AudioCodecAac StatGroup.AudioCodec
AudioCodecAac = "AAC"
AudioCodecG711U = "PCMU"
AudioCodecG711A = "PCMA"
AudioCodecOpus = "OPUS"
// VideoCodecAvc StatGroup.VideoCodec
VideoCodecAvc = "H264"
VideoCodecHevc = "H265"
)
type LalInfo struct {
ServerId string `json:"server_id"`
BinInfo string `json:"bin_info"`
LalVersion string `json:"lal_version"`
ApiVersion string `json:"api_version"`
NotifyVersion string `json:"notify_version"`
WebUiVersion string `json:"WebUiVersion"`
StartTime string `json:"start_time"`
}
type StatGroup struct {
StreamName string `json:"stream_name"`
AppName string `json:"app_name"`
AudioCodec string `json:"audio_codec"`
VideoCodec string `json:"video_codec"`
VideoWidth int `json:"video_width"`
VideoHeight int `json:"video_height"`
StatPub StatPub `json:"pub"`
StatSubs []StatSub `json:"subs"` // TODO(chef): [opt] 增加数量字段,因为这里不一定全部放入
StatPull StatPull `json:"pull"`
Fps []RecordPerSec `json:"in_frame_per_sec"`
}
type RecordPerSec struct {
UnixSec int64 `json:"unix_sec"`
V uint32 `json:"v"`
}
type StatSession struct {
SessionId string `json:"session_id"`
Protocol string `json:"protocol"`
BaseType string `json:"base_type"`
RemoteAddr string `json:"remote_addr"`
StartTime string `json:"start_time"`
ReadBytesSum uint64 `json:"read_bytes_sum"`
WroteBytesSum uint64 `json:"wrote_bytes_sum"`
BitrateKbits int `json:"bitrate_kbits"`
ReadBitrateKbits int `json:"read_bitrate_kbits"`
WriteBitrateKbits int `json:"write_bitrate_kbits"`
typ SessionType
}
type StatPub struct {
StatSession
}
type StatSub struct {
StatSession
}
type StatPull struct {
StatSession
}
type PeriodRecord struct {
ringBuf []RecordPerSec
nRecord int
}
// ---------------------------------------------------------------------------------------------------------------------
func Session2StatPub(session ISession) StatPub {
return StatPub{
session.GetStat(),
}
}
func Session2StatSub(session ISession) StatSub {
return StatSub{
session.GetStat(),
}
}
func Session2StatPull(session ISession) StatPull {
return StatPull{
session.GetStat(),
}
}
// GetFpsFrom
//
// @note result s.Fps is not ordered
func (s *StatGroup) GetFpsFrom(p *PeriodRecord, nowUnixSec int64) {
if s.Fps == nil || cap(s.Fps) < p.nRecord {
s.Fps = make([]RecordPerSec, p.nRecord)
} else {
s.Fps = s.Fps[0:p.nRecord]
}
nRecord := 0
p.nRecord = 0
for idx, record := range p.ringBuf {
if record.UnixSec == 0 {
continue
}
if record.UnixSec == nowUnixSec {
// value at nowUnixSec not completely recorded
p.nRecord++
continue
}
s.Fps[nRecord] = record
nRecord++
p.ringBuf[idx].UnixSec = 0
}
s.Fps = s.Fps[0:nRecord]
}
func NewPeriodRecord(bufSize int) PeriodRecord {
return PeriodRecord{
ringBuf: make([]RecordPerSec, bufSize),
nRecord: 0,
}
}
func (p *PeriodRecord) Add(unixSec int64, v uint32) {
var index int64
var record RecordPerSec
index = unixSec % int64(len(p.ringBuf))
record = p.ringBuf[index]
if record.UnixSec == unixSec {
p.ringBuf[index].V = record.V + v
} else {
if record.UnixSec == 0 {
p.nRecord++
}
p.ringBuf[index].UnixSec = unixSec
p.ringBuf[index].V = v
}
return
}
func (p *PeriodRecord) Clear() {
for idx := range p.ringBuf {
p.ringBuf[idx].UnixSec = 0
p.ringBuf[idx].V = 0
}
}