-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathpacket_trailer_h264_parser.go
More file actions
69 lines (61 loc) · 1.9 KB
/
Copy pathpacket_trailer_h264_parser.go
File metadata and controls
69 lines (61 loc) · 1.9 KB
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
// Copyright 2026 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lksdk
// parseH264SEIPacketTrailer parses H264 SEI NAL units (type 6) carrying
// user_data_unregistered messages with an LKTS packet trailer and returns
// the embedded FrameMetadata when detected.
//
// Expected payload format (after the 1-byte NAL header):
//
// payloadType = 5 (user_data_unregistered)
// payloadSize = variable
// UUID = 16 bytes (3fa85f64-5717-4562-b3fc-2c963f66afa6)
// trailer = LKTS TLV-encoded packet trailer (XOR'd with 0xFF)
func parseH264SEIPacketTrailer(nalData []byte) (FrameMetadata, bool) {
if len(nalData) < 2 {
return FrameMetadata{}, false
}
// Skip 1-byte NAL header.
payload := nalData[1:]
i := 0
// Parse payloadType (can be extended with 0xFF bytes).
payloadType := 0
for i < len(payload) && payload[i] == 0xFF {
payloadType += 255
i++
}
if i >= len(payload) {
return FrameMetadata{}, false
}
payloadType += int(payload[i])
i++
if payloadType != 5 {
return FrameMetadata{}, false
}
// Parse payloadSize (can be extended with 0xFF bytes).
payloadSize := 0
for i < len(payload) && payload[i] == 0xFF {
payloadSize += 255
i++
}
if i >= len(payload) {
return FrameMetadata{}, false
}
payloadSize += int(payload[i])
i++
if len(payload) < i+payloadSize {
return FrameMetadata{}, false
}
return parseSEIUserData(payload[i : i+payloadSize])
}