Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
galene
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
nexedi
galene
Commits
89780b86
Commit
89780b86
authored
Jul 29, 2021
by
Juliusz Chroboczek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move packet parsing code into its own package.
parent
81782751
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
101 additions
and
53 deletions
+101
-53
codecs/codecs.go
codecs/codecs.go
+32
-32
codecs/codecs_test.go
codecs/codecs_test.go
+46
-0
rtpconn/rtpconn.go
rtpconn/rtpconn.go
+21
-20
rtpconn/rtpreader.go
rtpconn/rtpreader.go
+2
-1
No files found.
rtpconn/codec
.go
→
codecs/codecs
.go
View file @
89780b86
package
rtpconn
package
codecs
import
(
"errors"
...
...
@@ -8,11 +8,14 @@ import (
"github.com/pion/rtp/codecs"
)
// isKeyframe determines if packet is the start of a keyframe.
var
errTruncated
=
errors
.
New
(
"truncated packet"
)
var
errUnsupportedCodec
=
errors
.
New
(
"unsupported codec"
)
// Keyframe determines if packet is the start of a keyframe.
// It returns (true, true) if that is the case, (false, true) if that is
// definitely not the case, and (false, false) if the information cannot
// be determined.
func
is
Keyframe
(
codec
string
,
packet
*
rtp
.
Packet
)
(
bool
,
bool
)
{
func
Keyframe
(
codec
string
,
packet
*
rtp
.
Packet
)
(
bool
,
bool
)
{
if
strings
.
EqualFold
(
codec
,
"video/vp8"
)
{
var
vp8
codecs
.
VP8Packet
_
,
err
:=
vp8
.
Unmarshal
(
packet
.
Payload
)
...
...
@@ -179,29 +182,26 @@ func isKeyframe(codec string, packet *rtp.Packet) (bool, bool) {
return
false
,
false
}
var
errTruncated
=
errors
.
New
(
"truncated packet"
)
var
errUnsupportedCodec
=
errors
.
New
(
"unsupported codec"
)
type
packetFlags
struct
{
seqno
uint16
start
bool
pid
uint16
// only if it needs rewriting
tid
uint8
sid
uint8
tidupsync
bool
sidsync
bool
sidnonreference
bool
discardable
bool
type
Flags
struct
{
Seqno
uint16
Start
bool
Pid
uint16
// only if it needs rewriting
Tid
uint8
Sid
uint8
TidUpSync
bool
SidSync
bool
SidNonReference
bool
Discardable
bool
}
func
getPacketFlags
(
codec
string
,
buf
[]
byte
)
(
packet
Flags
,
error
)
{
func
PacketFlags
(
codec
string
,
buf
[]
byte
)
(
Flags
,
error
)
{
if
len
(
buf
)
<
12
{
return
packet
Flags
{},
errTruncated
return
Flags
{},
errTruncated
}
var
flags
packet
Flags
var
flags
Flags
flags
.
s
eqno
=
(
uint16
(
buf
[
2
])
<<
8
)
|
uint16
(
buf
[
3
])
flags
.
S
eqno
=
(
uint16
(
buf
[
2
])
<<
8
)
|
uint16
(
buf
[
3
])
if
strings
.
EqualFold
(
codec
,
"video/vp8"
)
{
var
packet
rtp
.
Packet
...
...
@@ -215,11 +215,11 @@ func getPacketFlags(codec string, buf []byte) (packetFlags, error) {
return
flags
,
err
}
flags
.
s
tart
=
vp8
.
S
==
1
&&
vp8
.
PID
==
0
flags
.
p
id
=
vp8
.
PictureID
flags
.
t
id
=
vp8
.
TID
flags
.
tidups
ync
=
vp8
.
Y
==
1
flags
.
d
iscardable
=
vp8
.
N
==
1
flags
.
S
tart
=
vp8
.
S
==
1
&&
vp8
.
PID
==
0
flags
.
P
id
=
vp8
.
PictureID
flags
.
T
id
=
vp8
.
TID
flags
.
TidUpS
ync
=
vp8
.
Y
==
1
flags
.
D
iscardable
=
vp8
.
N
==
1
return
flags
,
nil
}
else
if
strings
.
EqualFold
(
codec
,
"video/vp9"
)
{
var
packet
rtp
.
Packet
...
...
@@ -232,19 +232,19 @@ func getPacketFlags(codec string, buf []byte) (packetFlags, error) {
if
err
!=
nil
{
return
flags
,
err
}
flags
.
s
tart
=
vp9
.
B
flags
.
t
id
=
vp9
.
TID
flags
.
s
id
=
vp9
.
SID
flags
.
tidups
ync
=
vp9
.
U
flags
.
sids
ync
=
vp9
.
P
flags
.
S
tart
=
vp9
.
B
flags
.
T
id
=
vp9
.
TID
flags
.
S
id
=
vp9
.
SID
flags
.
TidUpS
ync
=
vp9
.
U
flags
.
SidS
ync
=
vp9
.
P
// not yet in pion/rtp
flags
.
sidnonr
eference
=
(
packet
.
Payload
[
0
]
&
0x01
)
!=
0
flags
.
SidNonR
eference
=
(
packet
.
Payload
[
0
]
&
0x01
)
!=
0
return
flags
,
nil
}
return
flags
,
nil
}
func
r
ewritePacket
(
codec
string
,
data
[]
byte
,
seqno
uint16
,
delta
uint16
)
error
{
func
R
ewritePacket
(
codec
string
,
data
[]
byte
,
seqno
uint16
,
delta
uint16
)
error
{
if
len
(
data
)
<
12
{
return
errTruncated
}
...
...
rtpconn/codec
_test.go
→
codecs/codecs
_test.go
View file @
89780b86
package
rtpconn
package
codecs
import
(
"testing"
...
...
@@ -16,13 +16,13 @@ var vp8 = []byte{
func
TestPacketFlags
(
t
*
testing
.
T
)
{
buf
:=
append
([]
byte
{},
vp8
...
)
flags
,
err
:=
get
PacketFlags
(
"video/vp8"
,
buf
)
if
flags
.
seqno
!=
42
||
!
flags
.
start
||
flags
.
p
id
!=
57
||
flags
.
sid
!=
0
||
flags
.
t
id
!=
0
||
flags
.
tidupsync
||
flags
.
d
iscardable
||
err
!=
nil
{
flags
,
err
:=
PacketFlags
(
"video/vp8"
,
buf
)
if
flags
.
Seqno
!=
42
||
!
flags
.
Start
||
flags
.
P
id
!=
57
||
flags
.
Sid
!=
0
||
flags
.
T
id
!=
0
||
flags
.
TidUpSync
||
flags
.
D
iscardable
||
err
!=
nil
{
t
.
Errorf
(
"Got %v, %v, %v, %v, %v, %v (%v)"
,
flags
.
seqno
,
flags
.
start
,
flags
.
pid
,
flags
.
s
id
,
flags
.
tidupsync
,
flags
.
d
iscardable
,
err
,
flags
.
Seqno
,
flags
.
Start
,
flags
.
Pid
,
flags
.
S
id
,
flags
.
TidUpSync
,
flags
.
D
iscardable
,
err
,
)
}
}
...
...
@@ -30,17 +30,17 @@ func TestPacketFlags(t *testing.T) {
func
TestRewrite
(
t
*
testing
.
T
)
{
for
i
:=
uint16
(
0
);
i
<
0x7fff
;
i
++
{
buf
:=
append
([]
byte
{},
vp8
...
)
err
:=
r
ewritePacket
(
"video/vp8"
,
buf
,
i
,
i
)
err
:=
R
ewritePacket
(
"video/vp8"
,
buf
,
i
,
i
)
if
err
!=
nil
{
t
.
Errorf
(
"rewrite: %v"
,
err
)
continue
}
flags
,
err
:=
get
PacketFlags
(
"video/vp8"
,
buf
)
if
err
!=
nil
||
flags
.
s
eqno
!=
i
||
flags
.
pid
!=
(
57
+
i
)
&
0x7FFF
{
flags
,
err
:=
PacketFlags
(
"video/vp8"
,
buf
)
if
err
!=
nil
||
flags
.
S
eqno
!=
i
||
flags
.
Pid
!=
(
57
+
i
)
&
0x7FFF
{
t
.
Errorf
(
"Expected %v %v, got %v %v (%v)"
,
i
,
(
57
+
i
)
&
0x7FFF
,
flags
.
seqno
,
flags
.
p
id
,
err
)
i
,
(
57
+
i
)
&
0x7FFF
,
flags
.
Seqno
,
flags
.
P
id
,
err
)
}
}
}
rtpconn/rtpconn.go
View file @
89780b86
...
...
@@ -14,6 +14,7 @@ import (
"github.com/pion/sdp/v3"
"github.com/pion/webrtc/v3"
"github.com/jech/galene/codecs"
"github.com/jech/galene/conn"
"github.com/jech/galene/estimator"
"github.com/jech/galene/group"
...
...
@@ -213,59 +214,59 @@ var packetBufPool = sync.Pool{
func
(
down
*
rtpDownTrack
)
Write
(
buf
[]
byte
)
(
int
,
error
)
{
codec
:=
down
.
remote
.
Codec
()
.
MimeType
flags
,
err
:=
get
PacketFlags
(
codec
,
buf
)
flags
,
err
:=
codecs
.
PacketFlags
(
codec
,
buf
)
if
err
!=
nil
{
return
0
,
err
}
layer
:=
down
.
getLayerInfo
()
if
flags
.
tid
>
layer
.
maxTid
||
flags
.
s
id
>
layer
.
maxSid
{
if
flags
.
t
id
>
layer
.
maxTid
{
if
flags
.
Tid
>
layer
.
maxTid
||
flags
.
S
id
>
layer
.
maxSid
{
if
flags
.
T
id
>
layer
.
maxTid
{
if
layer
.
tid
==
layer
.
maxTid
{
layer
.
wantedTid
=
flags
.
t
id
layer
.
tid
=
flags
.
t
id
layer
.
wantedTid
=
flags
.
T
id
layer
.
tid
=
flags
.
T
id
}
layer
.
maxTid
=
flags
.
t
id
layer
.
maxTid
=
flags
.
T
id
}
if
flags
.
s
id
>
layer
.
maxSid
{
if
flags
.
S
id
>
layer
.
maxSid
{
if
layer
.
sid
==
layer
.
maxSid
{
layer
.
wantedSid
=
flags
.
s
id
layer
.
sid
=
flags
.
s
id
layer
.
wantedSid
=
flags
.
S
id
layer
.
sid
=
flags
.
S
id
}
layer
.
maxSid
=
flags
.
s
id
layer
.
maxSid
=
flags
.
S
id
}
down
.
setLayerInfo
(
layer
)
down
.
adjustLayer
()
}
if
flags
.
s
tart
&&
(
layer
.
tid
!=
layer
.
wantedTid
)
{
if
layer
.
wantedTid
<
layer
.
tid
||
flags
.
tidups
ync
{
if
flags
.
S
tart
&&
(
layer
.
tid
!=
layer
.
wantedTid
)
{
if
layer
.
wantedTid
<
layer
.
tid
||
flags
.
TidUpS
ync
{
layer
.
tid
=
layer
.
wantedTid
down
.
setLayerInfo
(
layer
)
}
}
if
flags
.
s
tart
&&
(
layer
.
sid
!=
layer
.
wantedSid
)
{
if
flags
.
sids
ync
{
if
flags
.
S
tart
&&
(
layer
.
sid
!=
layer
.
wantedSid
)
{
if
flags
.
SidS
ync
{
layer
.
sid
=
layer
.
wantedTid
down
.
setLayerInfo
(
layer
)
}
}
if
flags
.
tid
>
layer
.
tid
||
flags
.
s
id
>
layer
.
sid
||
(
flags
.
sid
<
layer
.
sid
&&
flags
.
sidnonr
eference
)
{
ok
:=
down
.
packetmap
.
Drop
(
flags
.
seqno
,
flags
.
p
id
)
if
flags
.
Tid
>
layer
.
tid
||
flags
.
S
id
>
layer
.
sid
||
(
flags
.
Sid
<
layer
.
sid
&&
flags
.
SidNonR
eference
)
{
ok
:=
down
.
packetmap
.
Drop
(
flags
.
Seqno
,
flags
.
P
id
)
if
ok
{
return
0
,
nil
}
}
ok
,
newseqno
,
piddelta
:=
down
.
packetmap
.
Map
(
flags
.
seqno
,
flags
.
p
id
)
ok
,
newseqno
,
piddelta
:=
down
.
packetmap
.
Map
(
flags
.
Seqno
,
flags
.
P
id
)
if
!
ok
{
return
0
,
nil
}
if
newseqno
==
flags
.
s
eqno
&&
piddelta
==
0
{
if
newseqno
==
flags
.
S
eqno
&&
piddelta
==
0
{
return
down
.
write
(
buf
)
}
...
...
@@ -274,7 +275,7 @@ func (down *rtpDownTrack) Write(buf []byte) (int, error) {
buf2
:=
ibuf2
.
([]
byte
)
n
:=
copy
(
buf2
,
buf
)
err
=
r
ewritePacket
(
codec
,
buf2
[
:
n
],
newseqno
,
piddelta
)
err
=
codecs
.
R
ewritePacket
(
codec
,
buf2
[
:
n
],
newseqno
,
piddelta
)
if
err
!=
nil
{
return
0
,
err
}
...
...
rtpconn/rtpreader.go
View file @
89780b86
...
...
@@ -8,6 +8,7 @@ import (
"github.com/pion/rtp"
"github.com/pion/webrtc/v3"
"github.com/jech/galene/codecs"
"github.com/jech/galene/packetcache"
"github.com/jech/galene/rtptime"
)
...
...
@@ -74,7 +75,7 @@ func readLoop(track *rtpUpTrack) {
track
.
jitter
.
Accumulate
(
packet
.
Timestamp
)
kf
,
kfKnown
:=
is
Keyframe
(
codec
.
MimeType
,
&
packet
)
kf
,
kfKnown
:=
codecs
.
Keyframe
(
codec
.
MimeType
,
&
packet
)
if
kf
||
!
kfKnown
{
kfNeeded
=
false
}
...
...
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