@@ -28,30 +28,58 @@ First byte in payload (rtp payload header):
28
28
29
29
const h264Debug = debug ( 'msl:h264depay' )
30
30
31
- export function h264depay (
32
- buffered : Buffer ,
33
- rtp : RtpMessage ,
34
- callback : ( msg : H264Message ) => void ,
35
- ) {
36
- const rtpPayload = payload ( rtp . data )
37
- const type = rtpPayload [ 0 ] & 0x1f
31
+ export class H264DepayParser {
32
+ private _buffer : Buffer
38
33
39
- if ( type === 28 ) {
40
- /* FU-A NALU */ const fuIndicator = rtpPayload [ 0 ]
41
- const fuHeader = rtpPayload [ 1 ]
42
- const startBit = ! ! ( fuHeader >> 7 )
43
- const nalType = fuHeader & 0x1f
44
- const nal = ( fuIndicator & 0xe0 ) | nalType
45
- const stopBit = fuHeader & 64
46
- if ( startBit ) {
47
- return Buffer . concat ( [
48
- Buffer . from ( [ 0 , 0 , 0 , 0 , nal ] ) ,
49
- rtpPayload . slice ( 2 ) ,
50
- ] )
51
- } else if ( stopBit ) {
52
- /* receieved end bit */ const h264frame = Buffer . concat ( [
53
- buffered ,
54
- rtpPayload . slice ( 2 ) ,
34
+ constructor ( ) {
35
+ this . _buffer = Buffer . alloc ( 0 )
36
+ }
37
+
38
+ parse ( rtp : RtpMessage ) : H264Message | null {
39
+ const rtpPayload = payload ( rtp . data )
40
+ const type = rtpPayload [ 0 ] & 0x1f
41
+
42
+ if ( type === 28 ) {
43
+ /* FU-A NALU */ const fuIndicator = rtpPayload [ 0 ]
44
+ const fuHeader = rtpPayload [ 1 ]
45
+ const startBit = ! ! ( fuHeader >> 7 )
46
+ const nalType = fuHeader & 0x1f
47
+ const nal = ( fuIndicator & 0xe0 ) | nalType
48
+ const stopBit = fuHeader & 64
49
+ if ( startBit ) {
50
+ this . _buffer = Buffer . concat ( [
51
+ Buffer . from ( [ 0 , 0 , 0 , 0 , nal ] ) ,
52
+ rtpPayload . slice ( 2 ) ,
53
+ ] )
54
+ return null
55
+ } else if ( stopBit ) {
56
+ /* receieved end bit */ const h264frame = Buffer . concat ( [
57
+ this . _buffer ,
58
+ rtpPayload . slice ( 2 ) ,
59
+ ] )
60
+ h264frame . writeUInt32BE ( h264frame . length - 4 , 0 )
61
+ const msg : H264Message = {
62
+ data : h264frame ,
63
+ type : MessageType . H264 ,
64
+ timestamp : timestamp ( rtp . data ) ,
65
+ ntpTimestamp : rtp . ntpTimestamp ,
66
+ payloadType : payloadType ( rtp . data ) ,
67
+ nalType : nalType ,
68
+ }
69
+ this . _buffer = Buffer . alloc ( 0 )
70
+ return msg
71
+ } else {
72
+ // Put the received data on the buffer and cut the header bytes
73
+ this . _buffer = Buffer . concat ( [ this . _buffer , rtpPayload . slice ( 2 ) ] )
74
+ return null
75
+ }
76
+ } else if (
77
+ ( type === NAL_TYPES . NON_IDR_PICTURE || type === NAL_TYPES . IDR_PICTURE ) &&
78
+ this . _buffer . length === 0
79
+ ) {
80
+ /* Single NALU */ const h264frame = Buffer . concat ( [
81
+ Buffer . from ( [ 0 , 0 , 0 , 0 ] ) ,
82
+ rtpPayload ,
55
83
] )
56
84
h264frame . writeUInt32BE ( h264frame . length - 4 , 0 )
57
85
const msg : H264Message = {
@@ -60,37 +88,16 @@ export function h264depay(
60
88
timestamp : timestamp ( rtp . data ) ,
61
89
ntpTimestamp : rtp . ntpTimestamp ,
62
90
payloadType : payloadType ( rtp . data ) ,
63
- nalType : nalType ,
91
+ nalType : type ,
64
92
}
65
- callback ( msg )
66
- return Buffer . alloc ( 0 )
93
+ this . _buffer = Buffer . alloc ( 0 )
94
+ return msg
67
95
} else {
68
- // Put the received data on the buffer and cut the header bytes
69
- return Buffer . concat ( [ buffered , rtpPayload . slice ( 2 ) ] )
70
- }
71
- } else if (
72
- ( type === NAL_TYPES . NON_IDR_PICTURE || type === NAL_TYPES . IDR_PICTURE ) &&
73
- buffered . length === 0
74
- ) {
75
- /* Single NALU */ const h264frame = Buffer . concat ( [
76
- Buffer . from ( [ 0 , 0 , 0 , 0 ] ) ,
77
- rtpPayload ,
78
- ] )
79
- h264frame . writeUInt32BE ( h264frame . length - 4 , 0 )
80
- const msg : H264Message = {
81
- data : h264frame ,
82
- type : MessageType . H264 ,
83
- timestamp : timestamp ( rtp . data ) ,
84
- ntpTimestamp : rtp . ntpTimestamp ,
85
- payloadType : payloadType ( rtp . data ) ,
86
- nalType : type ,
96
+ h264Debug (
97
+ `H264depayComponent can only extract types 1,5 and 28, got ${ type } ` ,
98
+ )
99
+ this . _buffer = Buffer . alloc ( 0 )
100
+ return null
87
101
}
88
- callback ( msg )
89
- return Buffer . alloc ( 0 )
90
- } else {
91
- h264Debug (
92
- `H264depayComponent can only extract types 1,5 and 28, got ${ type } ` ,
93
- )
94
- return Buffer . alloc ( 0 )
95
102
}
96
103
}
0 commit comments