21
21
RTMP_CHUNK_ID_MAX = 65599 , /* 65535 + 64 */
22
22
23
23
RTMP_CHUNK_OFFSET = 64 ,
24
+ TIMESTAMP_24MAX = 0x00ffffff ,
24
25
};
25
26
26
27
@@ -121,9 +122,20 @@ static int decode_basic_hdr(struct rtmp_header *hdr, struct mbuf *mb)
121
122
}
122
123
123
124
124
- int rtmp_header_encode (struct mbuf * mb , const struct rtmp_header * hdr )
125
+ static uint32_t ts_24 (uint32_t ts )
126
+ {
127
+ return ts >= TIMESTAMP_24MAX ? TIMESTAMP_24MAX : ts ;
128
+ }
129
+
130
+
131
+ static uint32_t ts_ext (uint32_t ts )
132
+ {
133
+ return ts >= TIMESTAMP_24MAX ? ts : 0 ;
134
+ }
135
+
136
+
137
+ int rtmp_header_encode (struct mbuf * mb , struct rtmp_header * hdr )
125
138
{
126
- bool ext_ts ;
127
139
int err = 0 ;
128
140
129
141
if (!mb || !hdr )
@@ -136,39 +148,43 @@ int rtmp_header_encode(struct mbuf *mb, const struct rtmp_header *hdr)
136
148
switch (hdr -> format ) {
137
149
138
150
case 0 :
139
- ext_ts = (hdr -> timestamp >= 0x00ffffff );
151
+ hdr -> timestamp_ext = ts_ext (hdr -> timestamp );
140
152
141
- err |= mbuf_write_u24_hton (mb ,
142
- ext_ts ? 0xffffff : hdr -> timestamp );
153
+ err |= mbuf_write_u24_hton (mb , ts_24 (hdr -> timestamp ));
143
154
err |= mbuf_write_u24_hton (mb , hdr -> length );
144
155
err |= mbuf_write_u8 (mb , hdr -> type_id );
145
156
err |= mbuf_write_u32 (mb , sys_htoll (hdr -> stream_id ));
146
-
147
- if (ext_ts ) {
148
- err |= mbuf_write_u32 (mb , htonl (hdr -> timestamp ));
149
- }
150
157
break ;
151
158
152
159
case 1 :
153
- err |= mbuf_write_u24_hton (mb , hdr -> timestamp_delta );
160
+ hdr -> timestamp_ext = ts_ext (hdr -> timestamp_delta );
161
+
162
+ err |= mbuf_write_u24_hton (mb , ts_24 (hdr -> timestamp_delta ));
154
163
err |= mbuf_write_u24_hton (mb , hdr -> length );
155
164
err |= mbuf_write_u8 (mb , hdr -> type_id );
156
165
break ;
157
166
158
167
case 2 :
159
- err |= mbuf_write_u24_hton (mb , hdr -> timestamp_delta );
168
+ hdr -> timestamp_ext = ts_ext (hdr -> timestamp_delta );
169
+
170
+ err |= mbuf_write_u24_hton (mb , ts_24 (hdr -> timestamp_delta ));
160
171
break ;
161
172
162
173
case 3 :
163
174
break ;
164
175
}
165
176
177
+ if (hdr -> timestamp_ext ) {
178
+ err |= mbuf_write_u32 (mb , htonl (hdr -> timestamp_ext ));
179
+ }
180
+
166
181
return err ;
167
182
}
168
183
169
184
170
185
int rtmp_header_decode (struct rtmp_header * hdr , struct mbuf * mb )
171
186
{
187
+ uint32_t * timestamp_ext = NULL ;
172
188
int err ;
173
189
174
190
if (!hdr || !mb )
@@ -190,12 +206,6 @@ int rtmp_header_decode(struct rtmp_header *hdr, struct mbuf *mb)
190
206
hdr -> length = mbuf_read_u24_ntoh (mb );
191
207
hdr -> type_id = mbuf_read_u8 (mb );
192
208
hdr -> stream_id = sys_ltohl (mbuf_read_u32 (mb ));
193
-
194
- if (hdr -> timestamp == 0x00ffffff ) {
195
- if (mbuf_get_left (mb ) < 4 )
196
- return ENODATA ;
197
- hdr -> timestamp = ntohl (mbuf_read_u32 (mb ));
198
- }
199
209
break ;
200
210
201
211
case 1 :
@@ -219,6 +229,19 @@ int rtmp_header_decode(struct rtmp_header *hdr, struct mbuf *mb)
219
229
break ;
220
230
}
221
231
232
+ if (hdr -> timestamp == TIMESTAMP_24MAX )
233
+ timestamp_ext = & hdr -> timestamp ;
234
+ else if (hdr -> timestamp_delta == TIMESTAMP_24MAX )
235
+ timestamp_ext = & hdr -> timestamp_delta ;
236
+
237
+ if (timestamp_ext ) {
238
+ if (mbuf_get_left (mb ) < 4 )
239
+ return ENODATA ;
240
+
241
+ * timestamp_ext = ntohl (mbuf_read_u32 (mb ));
242
+ hdr -> ext_ts = true;
243
+ }
244
+
222
245
return 0 ;
223
246
}
224
247
0 commit comments