-
Notifications
You must be signed in to change notification settings - Fork 101
/
speed.go
286 lines (238 loc) · 6.44 KB
/
speed.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package m3u8d
import (
"fmt"
"github.com/orestonce/m3u8d/mformat"
"io"
"strconv"
"strings"
"sync"
"time"
)
type SpeedStatus struct {
Locker sync.Mutex
IsRunning bool
speedBeginTime time.Time
speedIdAlloc uint32
totalBlockCount int64
doneBlockCount int64
doneBlockMap map[time.Time]downOneUnit
doingBlockMap map[uint32]doingOneUnit
tsNotWriteReasonMap map[string]tsNotWriteReasonUnit
progressPercent int
progressBarTitle string
ProgressBarShow bool
lastDrawProgress time.Time
errMsg string
saveFileTo string
isSkipped bool
}
type tsNotWriteReasonUnit struct {
fileName string
downloadUrl string
reason string
}
type downOneUnit struct {
byteCount int64
blockCount int64
}
type doingOneUnit struct {
startTime time.Time
doneBytes int
}
func (this *SpeedStatus) clearStatusNoLock() {
this.speedBeginTime = time.Time{}
this.totalBlockCount = 0
this.doneBlockCount = 0
this.speedIdAlloc = 0
this.doneBlockMap = map[time.Time]downOneUnit{}
this.doingBlockMap = map[uint32]doingOneUnit{}
this.tsNotWriteReasonMap = map[string]tsNotWriteReasonUnit{}
this.progressPercent = 0
this.progressBarTitle = ""
this.ProgressBarShow = false
this.errMsg = ""
this.saveFileTo = ""
this.isSkipped = false
}
func (this *SpeedStatus) DrawProgressBar(total int, current int) {
if total == 0 {
return
}
proportion := float32(current) / float32(total)
this.Locker.Lock()
this.progressPercent = int(proportion * 100)
title := this.progressBarTitle
if this.ProgressBarShow {
if this.lastDrawProgress.IsZero() || time.Now().Sub(this.lastDrawProgress).Milliseconds() > 100 {
width := 50
pos := int(proportion * float32(width))
fmt.Printf(title+" %s%*s %6.2f%%\r", strings.Repeat("■", pos), width-pos, "", proportion*100)
}
}
this.Locker.Unlock()
}
func (this *SpeedStatus) SpeedResetTotalBlockCount(count int) {
this.Locker.Lock()
defer this.Locker.Unlock()
this.totalBlockCount = int64(count)
this.doneBlockCount = 0
}
func (this *SpeedStatus) SpeedAdd1Block(now time.Time, byteCount int) {
this.Locker.Lock()
unit := this.doneBlockMap[now]
unit.byteCount += int64(byteCount)
unit.blockCount++
if this.doneBlockMap == nil {
this.doneBlockMap = map[time.Time]downOneUnit{}
}
this.doneBlockMap[now] = unit
this.doneBlockCount++
cur := this.doneBlockCount
total := this.totalBlockCount
this.Locker.Unlock()
if total > 0 {
this.DrawProgressBar(int(total), int(cur))
}
}
func (this *SpeedStatus) SpeedResetBytes() {
this.Locker.Lock()
defer this.Locker.Unlock()
this.speedBeginTime = time.Now()
this.totalBlockCount = 0
this.doneBlockCount = 0
this.doneBlockMap = map[time.Time]downOneUnit{}
}
type SpeedInfo struct {
BytePerSecond int
BytePerSecondText string
RemainTime int
RemainTimeText string
}
func (obj SpeedInfo) ToString() string {
var text string
if obj.BytePerSecondText == "" {
return ""
}
text += "速度 " + obj.BytePerSecondText
if obj.RemainTimeText != "" {
text += ", 剩余时间 " + obj.RemainTimeText
}
return text
}
func (this *SpeedStatus) SpeedRecent5sGetAndUpdate() (speed SpeedInfo) {
this.Locker.Lock()
defer this.Locker.Unlock()
now := time.Now()
if this.speedBeginTime.IsZero() || now.Sub(this.speedBeginTime) < time.Second { // 1s以内, 暂时不计算速度
return speed
}
const secondCount = 5
expireTime := now.Add(-secondCount * time.Second) // 5秒内的
var total downOneUnit
for ct, v := range this.doneBlockMap {
if ct.Before(expireTime) {
delete(this.doneBlockMap, ct)
continue
}
total.byteCount += v.byteCount
total.blockCount += v.blockCount
}
realSecond := now.Sub(this.speedBeginTime).Seconds()
if realSecond > secondCount {
realSecond = secondCount
}
bytePerSecond := float64(total.byteCount) / realSecond
bytePerSecond = this.addBytePerSecondWithDoing(now, bytePerSecond)
speed.BytePerSecond = int(bytePerSecond)
if bytePerSecond < 1024 {
speed.BytePerSecondText = strconv.Itoa(int(bytePerSecond)) + " B/s"
} else {
bytePerSecond = bytePerSecond / 1024
if bytePerSecond < 1024 {
speed.BytePerSecondText = strconv.Itoa(int(bytePerSecond)) + " KB/s"
} else {
bytePerSecond = bytePerSecond / 1024
speed.BytePerSecondText = strconv.FormatFloat(bytePerSecond, 'f', 2, 64) + " MB/s"
}
}
if this.totalBlockCount > 0 && total.blockCount > 0 && this.doneBlockCount < this.totalBlockCount {
secondPerBlock := realSecond / float64(total.blockCount)
speed.RemainTime = int(secondPerBlock * float64(this.totalBlockCount-this.doneBlockCount))
speed.RemainTimeText = fmt.Sprintf("%02d:%02d", speed.RemainTime/60, speed.RemainTime%60)
}
return speed
}
func (this *SpeedStatus) GetPercent() (percent int) {
this.Locker.Lock()
defer this.Locker.Unlock()
return this.progressPercent
}
func (this *SpeedStatus) GetTitle() (title string) {
this.Locker.Lock()
defer this.Locker.Unlock()
return this.progressBarTitle
}
func (this *SpeedStatus) SetProgressBarTitle(title string) {
this.Locker.Lock()
this.progressBarTitle = title
this.Locker.Unlock()
}
func (this *SpeedStatus) SpeedReadAll(r io.Reader) (b []byte, err error) {
b = make([]byte, 0, 512)
var n int
var unit doingOneUnit
unit.startTime = time.Now()
var id uint32
this.Locker.Lock()
this.speedIdAlloc++
id = this.speedIdAlloc
this.Locker.Unlock()
for {
n, err = r.Read(b[len(b):cap(b)])
b = b[:len(b)+n]
if err != nil {
if err == io.EOF {
err = nil
}
this.Locker.Lock()
delete(this.doingBlockMap, id)
this.Locker.Unlock()
return b, err
}
if len(b) == cap(b) {
// Add more capacity (let append pick how much).
b = append(b, 0)[:len(b)]
}
unit.doneBytes = len(b)
this.Locker.Lock()
if this.doingBlockMap == nil {
this.doingBlockMap = map[uint32]doingOneUnit{}
}
this.doingBlockMap[id] = unit
this.Locker.Unlock()
}
}
func (this *SpeedStatus) addBytePerSecondWithDoing(now time.Time, bytePerSecond float64) float64 {
sum := bytePerSecond
for _, one := range this.doingBlockMap {
second := now.Sub(one.startTime).Seconds()
if second < 1 {
continue
}
sum += float64(one.doneBytes) / second
}
return sum
}
func (this *SpeedStatus) setTsNotWriteReason(ts *mformat.TsInfo, reason string) {
this.Locker.Lock()
defer this.Locker.Unlock()
unit := tsNotWriteReasonUnit{
fileName: ts.Name,
downloadUrl: ts.Url,
reason: reason,
}
if this.tsNotWriteReasonMap == nil {
this.tsNotWriteReasonMap = map[string]tsNotWriteReasonUnit{}
}
this.tsNotWriteReasonMap[unit.fileName] = unit
}