Skip to content

Commit 1d1bcb0

Browse files
committedNov 1, 2024
Code refactoring for UnmarshalSDP
1 parent 3f5f132 commit 1d1bcb0

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed
 

‎pkg/rtsp/helpers.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ func UnmarshalSDP(rawSDP []byte) ([]*core.Media, error) {
2828
sd := &sdp.SessionDescription{}
2929
if err := sd.Unmarshal(rawSDP); err != nil {
3030
// fix multiple `s=` https://github.com/AlexxIT/WebRTC/issues/417
31-
re, _ := regexp.Compile("\ns=[^\n]+")
32-
rawSDP = re.ReplaceAll(rawSDP, nil)
31+
rawSDP = regexp.MustCompile("\ns=[^\n]+").ReplaceAll(rawSDP, nil)
3332

3433
// fix SDP header for some cameras
3534
if i := bytes.Index(rawSDP, []byte("\nm=")); i > 0 {
@@ -38,12 +37,11 @@ func UnmarshalSDP(rawSDP []byte) ([]*core.Media, error) {
3837

3938
// Fix invalid media type (errSDPInvalidValue) caused by
4039
// some TP-LINK IP camera, e.g. TL-IPC44GW
41-
m := regexp.MustCompile("m=[^ ]+ ")
42-
for _, i := range m.FindAll(rawSDP, -1) {
43-
switch string(i[2 : len(i)-1]) {
40+
for _, b := range regexp.MustCompile("m=[^ ]+ ").FindAll(rawSDP, -1) {
41+
switch string(b[2 : len(b)-1]) {
4442
case "audio", "video", "application":
4543
default:
46-
rawSDP = bytes.Replace(rawSDP, i, []byte("m=application "), 1)
44+
rawSDP = bytes.Replace(rawSDP, b, []byte("m=application "), 1)
4745
}
4846
}
4947

0 commit comments

Comments
 (0)
Please sign in to comment.