Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
sfu
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alain Takoudjou
sfu
Commits
44ae9f82
Commit
44ae9f82
authored
Oct 04, 2020
by
Juliusz Chroboczek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remodularise packet cache.
parent
a233f10b
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
127 additions
and
98 deletions
+127
-98
packetcache/packetcache.go
packetcache/packetcache.go
+122
-93
packetcache/packetcache_test.go
packetcache/packetcache_test.go
+5
-5
No files found.
packetcache/packetcache.go
View file @
44ae9f82
...
@@ -6,7 +6,7 @@ import (
...
@@ -6,7 +6,7 @@ import (
)
)
const
BufSize
=
1500
const
BufSize
=
1500
const
max
Keyf
rame
=
1024
const
max
F
rame
=
1024
type
entry
struct
{
type
entry
struct
{
seqno
uint16
seqno
uint16
...
@@ -14,6 +14,17 @@ type entry struct {
...
@@ -14,6 +14,17 @@ type entry struct {
buf
[
BufSize
]
byte
buf
[
BufSize
]
byte
}
}
type
bitmap
struct
{
valid
bool
first
uint16
bitmap
uint32
}
type
frame
struct
{
timestamp
uint32
entries
[]
entry
}
type
Cache
struct
{
type
Cache
struct
{
mu
sync
.
Mutex
mu
sync
.
Mutex
//stats
//stats
...
@@ -24,13 +35,10 @@ type Cache struct {
...
@@ -24,13 +35,10 @@ type Cache struct {
lost
uint32
lost
uint32
totalLost
uint32
totalLost
uint32
// bitmap
// bitmap
bitmapValid
bool
bitmap
bitmap
first
uint16
bitmap
uint32
// buffered keyframe
// buffered keyframe
kfTimestamp
uint32
keyframe
frame
kfEntries
[]
entry
// the actual cache
// packet cache
tail
uint16
tail
uint16
entries
[]
entry
entries
[]
entry
}
}
...
@@ -56,33 +64,116 @@ func seqnoInvalid(seqno, reference uint16) bool {
...
@@ -56,33 +64,116 @@ func seqnoInvalid(seqno, reference uint16) bool {
return
false
return
false
}
}
// Set a bit in the bitmap, shifting first if necessary.
// set sets a bit in the bitmap, shifting if necessary
func
(
cache
*
Cache
)
set
(
seqno
uint16
)
{
func
(
bitmap
*
bitmap
)
set
(
seqno
uint16
)
{
if
!
cache
.
bitmapValid
||
seqnoInvalid
(
seqno
,
cache
.
first
)
{
if
!
bitmap
.
valid
||
seqnoInvalid
(
seqno
,
bitmap
.
first
)
{
cache
.
first
=
seqno
bitmap
.
first
=
seqno
cache
.
bitmap
=
1
bitmap
.
bitmap
=
1
cache
.
bitmapValid
=
true
bitmap
.
valid
=
true
return
}
if
((
seqno
-
bitmap
.
first
)
&
0x8000
)
!=
0
{
return
}
if
seqno
-
bitmap
.
first
>=
32
{
shift
:=
seqno
-
bitmap
.
first
-
31
bitmap
.
bitmap
>>=
shift
bitmap
.
first
+=
shift
}
if
(
bitmap
.
bitmap
&
1
)
==
1
{
ones
:=
bits
.
TrailingZeros32
(
^
bitmap
.
bitmap
)
bitmap
.
bitmap
>>=
ones
bitmap
.
first
+=
uint16
(
ones
)
}
bitmap
.
bitmap
|=
(
1
<<
uint16
(
seqno
-
bitmap
.
first
))
return
}
// BitmapGet shifts up to 17 bits out of the bitmap. It returns a boolean
// indicating if any were 0, the index of the first 0 bit, and a bitmap
// indicating any 0 bits after the first one.
func
(
cache
*
Cache
)
BitmapGet
(
next
uint16
)
(
bool
,
uint16
,
uint16
)
{
cache
.
mu
.
Lock
()
defer
cache
.
mu
.
Unlock
()
return
cache
.
bitmap
.
get
(
next
)
}
func
(
bitmap
*
bitmap
)
get
(
next
uint16
)
(
bool
,
uint16
,
uint16
)
{
first
:=
bitmap
.
first
count
:=
next
-
first
if
(
count
&
0x8000
)
!=
0
||
count
==
0
{
// next is in the past
return
false
,
first
,
0
}
if
count
>
17
{
count
=
17
}
bm
:=
(
^
bitmap
.
bitmap
)
&
^
((
^
uint32
(
0
))
<<
count
)
bitmap
.
bitmap
>>=
count
bitmap
.
first
+=
count
if
bm
==
0
{
return
false
,
first
,
0
}
if
(
bm
&
1
)
==
0
{
count
:=
bits
.
TrailingZeros32
(
bm
)
bm
>>=
count
first
+=
uint16
(
count
)
}
return
true
,
first
,
uint16
(
bm
>>
1
)
}
func
(
frame
*
frame
)
store
(
seqno
uint16
,
timestamp
uint32
,
first
bool
,
data
[]
byte
)
{
if
first
{
if
frame
.
timestamp
!=
timestamp
{
frame
.
timestamp
=
timestamp
frame
.
entries
=
frame
.
entries
[
:
0
]
}
}
else
if
len
(
frame
.
entries
)
>
0
{
if
frame
.
timestamp
!=
timestamp
{
return
}
}
else
{
return
return
}
}
if
((
seqno
-
cache
.
first
)
&
0x8000
)
!=
0
{
i
:=
0
for
i
<
len
(
frame
.
entries
)
{
if
frame
.
entries
[
i
]
.
seqno
>=
seqno
{
break
}
i
++
}
if
i
<
len
(
frame
.
entries
)
&&
frame
.
entries
[
i
]
.
seqno
==
seqno
{
// duplicate
return
return
}
}
if
seqno
-
cache
.
first
>=
32
{
if
len
(
frame
.
entries
)
>=
maxFrame
{
shift
:=
seqno
-
cache
.
first
-
31
// overflow
cache
.
bitmap
>>=
shift
return
cache
.
first
+=
shift
}
}
if
(
cache
.
bitmap
&
1
)
==
1
{
e
:=
entry
{
ones
:=
bits
.
TrailingZeros32
(
^
cache
.
bitmap
)
seqno
:
seqno
,
cache
.
bitmap
>>=
ones
length
:
uint16
(
len
(
data
)),
cache
.
first
+=
uint16
(
ones
)
}
}
copy
(
e
.
buf
[
:
],
data
)
cache
.
bitmap
|=
(
1
<<
uint16
(
seqno
-
cache
.
first
))
if
i
>=
len
(
frame
.
entries
)
{
frame
.
entries
=
append
(
frame
.
entries
,
e
)
return
return
}
frame
.
entries
=
append
(
frame
.
entries
,
entry
{})
copy
(
frame
.
entries
[
i
+
1
:
],
frame
.
entries
[
i
:
])
frame
.
entries
[
i
]
=
e
}
}
// Store a packet, setting bitmap at the same time
// Store a packet, setting bitmap at the same time
...
@@ -108,38 +199,9 @@ func (cache *Cache) Store(seqno uint16, timestamp uint32, keyframe bool, buf []b
...
@@ -108,38 +199,9 @@ func (cache *Cache) Store(seqno uint16, timestamp uint32, keyframe bool, buf []b
}
}
}
}
}
}
cache
.
set
(
seqno
)
cache
.
bitmap
.
set
(
seqno
)
doit
:=
false
cache
.
keyframe
.
store
(
seqno
,
timestamp
,
keyframe
,
buf
)
if
keyframe
{
if
cache
.
kfTimestamp
!=
timestamp
{
cache
.
kfTimestamp
=
timestamp
cache
.
kfEntries
=
cache
.
kfEntries
[
:
0
]
}
doit
=
true
}
else
if
len
(
cache
.
kfEntries
)
>
0
{
doit
=
cache
.
kfTimestamp
==
timestamp
}
if
doit
{
i
:=
0
for
i
<
len
(
cache
.
kfEntries
)
{
if
cache
.
kfEntries
[
i
]
.
seqno
>=
seqno
{
break
}
i
++
}
if
i
>=
len
(
cache
.
kfEntries
)
||
cache
.
kfEntries
[
i
]
.
seqno
!=
seqno
{
if
len
(
cache
.
kfEntries
)
>=
maxKeyframe
{
cache
.
kfEntries
=
cache
.
kfEntries
[
:
maxKeyframe
-
1
]
}
cache
.
kfEntries
=
append
(
cache
.
kfEntries
,
entry
{})
copy
(
cache
.
kfEntries
[
i
+
1
:
],
cache
.
kfEntries
[
i
:
])
}
cache
.
kfEntries
[
i
]
.
seqno
=
seqno
cache
.
kfEntries
[
i
]
.
length
=
uint16
(
len
(
buf
))
copy
(
cache
.
kfEntries
[
i
]
.
buf
[
:
],
buf
)
}
i
:=
cache
.
tail
i
:=
cache
.
tail
cache
.
entries
[
i
]
.
seqno
=
seqno
cache
.
entries
[
i
]
.
seqno
=
seqno
...
@@ -147,7 +209,7 @@ func (cache *Cache) Store(seqno uint16, timestamp uint32, keyframe bool, buf []b
...
@@ -147,7 +209,7 @@ func (cache *Cache) Store(seqno uint16, timestamp uint32, keyframe bool, buf []b
cache
.
entries
[
i
]
.
length
=
uint16
(
len
(
buf
))
cache
.
entries
[
i
]
.
length
=
uint16
(
len
(
buf
))
cache
.
tail
=
(
i
+
1
)
%
uint16
(
len
(
cache
.
entries
))
cache
.
tail
=
(
i
+
1
)
%
uint16
(
len
(
cache
.
entries
))
return
cache
.
first
,
i
return
cache
.
bitmap
.
first
,
i
}
}
func
(
cache
*
Cache
)
Expect
(
n
int
)
{
func
(
cache
*
Cache
)
Expect
(
n
int
)
{
...
@@ -176,7 +238,7 @@ func (cache *Cache) Get(seqno uint16, result []byte) uint16 {
...
@@ -176,7 +238,7 @@ func (cache *Cache) Get(seqno uint16, result []byte) uint16 {
cache
.
mu
.
Lock
()
cache
.
mu
.
Lock
()
defer
cache
.
mu
.
Unlock
()
defer
cache
.
mu
.
Unlock
()
n
:=
get
(
seqno
,
cache
.
k
fE
ntries
,
result
)
n
:=
get
(
seqno
,
cache
.
k
eyframe
.
e
ntries
,
result
)
if
n
>
0
{
if
n
>
0
{
return
n
return
n
}
}
...
@@ -209,11 +271,11 @@ func (cache *Cache) Keyframe() (uint32, []uint16) {
...
@@ -209,11 +271,11 @@ func (cache *Cache) Keyframe() (uint32, []uint16) {
cache
.
mu
.
Lock
()
cache
.
mu
.
Lock
()
defer
cache
.
mu
.
Unlock
()
defer
cache
.
mu
.
Unlock
()
seqnos
:=
make
([]
uint16
,
len
(
cache
.
k
fE
ntries
))
seqnos
:=
make
([]
uint16
,
len
(
cache
.
k
eyframe
.
e
ntries
))
for
i
:=
range
cache
.
k
fE
ntries
{
for
i
:=
range
cache
.
k
eyframe
.
e
ntries
{
seqnos
[
i
]
=
cache
.
k
fE
ntries
[
i
]
.
seqno
seqnos
[
i
]
=
cache
.
k
eyframe
.
e
ntries
[
i
]
.
seqno
}
}
return
cache
.
k
fT
imestamp
,
seqnos
return
cache
.
k
eyframe
.
t
imestamp
,
seqnos
}
}
func
(
cache
*
Cache
)
resize
(
capacity
int
)
{
func
(
cache
*
Cache
)
resize
(
capacity
int
)
{
...
@@ -269,39 +331,6 @@ func (cache *Cache) ResizeCond(capacity int) bool {
...
@@ -269,39 +331,6 @@ func (cache *Cache) ResizeCond(capacity int) bool {
return
true
return
true
}
}
// Shift up to 17 bits out of the bitmap. Return a boolean indicating if
// any were 0, the index of the first 0 bit, and a bitmap indicating any
// 0 bits after the first one.
func
(
cache
*
Cache
)
BitmapGet
(
next
uint16
)
(
bool
,
uint16
,
uint16
)
{
cache
.
mu
.
Lock
()
defer
cache
.
mu
.
Unlock
()
first
:=
cache
.
first
count
:=
next
-
first
if
(
count
&
0x8000
)
!=
0
||
count
==
0
{
// next is in the past
return
false
,
first
,
0
}
if
count
>
17
{
count
=
17
}
bitmap
:=
(
^
cache
.
bitmap
)
&
^
((
^
uint32
(
0
))
<<
count
)
cache
.
bitmap
>>=
count
cache
.
first
+=
count
if
bitmap
==
0
{
return
false
,
first
,
0
}
if
(
bitmap
&
1
)
==
0
{
count
:=
bits
.
TrailingZeros32
(
bitmap
)
bitmap
>>=
count
first
+=
uint16
(
count
)
}
return
true
,
first
,
uint16
(
bitmap
>>
1
)
}
func
(
cache
*
Cache
)
GetStats
(
reset
bool
)
(
uint32
,
uint32
,
uint32
,
uint32
)
{
func
(
cache
*
Cache
)
GetStats
(
reset
bool
)
(
uint32
,
uint32
,
uint32
,
uint32
)
{
cache
.
mu
.
Lock
()
cache
.
mu
.
Lock
()
defer
cache
.
mu
.
Unlock
()
defer
cache
.
mu
.
Unlock
()
...
...
packetcache/packetcache_test.go
View file @
44ae9f82
...
@@ -223,8 +223,8 @@ func TestBitmap(t *testing.T) {
...
@@ -223,8 +223,8 @@ func TestBitmap(t *testing.T) {
}
}
value
>>=
uint16
(
first
-
42
)
value
>>=
uint16
(
first
-
42
)
if
uint32
(
value
)
!=
cache
.
bitmap
{
if
uint32
(
value
)
!=
cache
.
bitmap
.
bitmap
{
t
.
Errorf
(
"Got %b, expected %b"
,
cache
.
bitmap
,
value
)
t
.
Errorf
(
"Got %b, expected %b"
,
cache
.
bitmap
.
bitmap
,
value
)
}
}
}
}
...
@@ -245,8 +245,8 @@ func TestBitmapWrap(t *testing.T) {
...
@@ -245,8 +245,8 @@ func TestBitmapWrap(t *testing.T) {
}
}
value
>>=
uint16
(
first
-
42
)
value
>>=
uint16
(
first
-
42
)
if
uint32
(
value
)
!=
cache
.
bitmap
{
if
uint32
(
value
)
!=
cache
.
bitmap
.
bitmap
{
t
.
Errorf
(
"Got %b, expected %b"
,
cache
.
bitmap
,
value
)
t
.
Errorf
(
"Got %b, expected %b"
,
cache
.
bitmap
.
bitmap
,
value
)
}
}
}
}
...
@@ -263,7 +263,7 @@ func TestBitmapGet(t *testing.T) {
...
@@ -263,7 +263,7 @@ func TestBitmapGet(t *testing.T) {
}
}
pos
:=
uint16
(
42
)
pos
:=
uint16
(
42
)
for
cache
.
bitmap
!=
0
{
for
cache
.
bitmap
.
bitmap
!=
0
{
found
,
first
,
bitmap
:=
cache
.
BitmapGet
(
42
+
65
)
found
,
first
,
bitmap
:=
cache
.
BitmapGet
(
42
+
65
)
if
first
<
pos
||
first
>=
pos
+
64
{
if
first
<
pos
||
first
>=
pos
+
64
{
t
.
Errorf
(
"First is %v, pos is %v"
,
first
,
pos
)
t
.
Errorf
(
"First is %v, pos is %v"
,
first
,
pos
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment