rvoip-rtp-core 0.2.4

RTP/RTCP protocol implementation for the rvoip stack
Documentation
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! Payload Type Registry
//!
//! This module provides a centralized, RFC 3551-compliant payload type registry
//! to replace hardcoded payload type logic across the codebase.

use std::collections::HashMap;
use std::sync::OnceLock;
use crate::api::common::frame::MediaFrameType;

/// Payload type information
#[derive(Debug, Clone, PartialEq)]
pub struct PayloadTypeInfo {
    /// Payload type number
    pub payload_type: u8,
    /// Media type (Audio, Video, Data)
    pub media_type: MediaFrameType,
    /// Codec name (e.g., "PCMU", "H264", "VP8")
    pub codec_name: String,
    /// Clock rate in Hz
    pub clock_rate: u32,
    /// Number of channels (for audio)
    pub channels: Option<u8>,
    /// Whether this is a static or dynamic payload type
    pub is_dynamic: bool,
    /// RFC reference
    pub rfc_reference: Option<String>,
}

/// Global payload type registry
static PAYLOAD_REGISTRY: OnceLock<PayloadTypeRegistry> = OnceLock::new();

/// Payload type registry implementation
#[derive(Debug)]
pub struct PayloadTypeRegistry {
    /// Static payload types (0-95)
    static_types: HashMap<u8, PayloadTypeInfo>,
    /// Dynamic payload type mappings (96-127)
    dynamic_types: HashMap<u8, PayloadTypeInfo>,
}

impl PayloadTypeRegistry {
    /// Create a new registry with RFC 3551 static payload types
    pub fn new() -> Self {
        let mut registry = Self {
            static_types: HashMap::new(),
            dynamic_types: HashMap::new(),
        };

        registry.populate_rfc3551_static_types();
        registry
    }

    /// Populate RFC 3551 static payload types
    fn populate_rfc3551_static_types(&mut self) {
        // Audio payload types (RFC 3551)
        self.add_static_type(PayloadTypeInfo {
            payload_type: 0,
            media_type: MediaFrameType::Audio,
            codec_name: "PCMU".to_string(),
            clock_rate: 8000,
            channels: Some(1),
            is_dynamic: false,
            rfc_reference: Some("RFC 3551".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 3,
            media_type: MediaFrameType::Audio,
            codec_name: "GSM".to_string(),
            clock_rate: 8000,
            channels: Some(1),
            is_dynamic: false,
            rfc_reference: Some("RFC 3551".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 4,
            media_type: MediaFrameType::Audio,
            codec_name: "G723".to_string(),
            clock_rate: 8000,
            channels: Some(1),
            is_dynamic: false,
            rfc_reference: Some("RFC 3551".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 5,
            media_type: MediaFrameType::Audio,
            codec_name: "DVI4".to_string(),
            clock_rate: 8000,
            channels: Some(1),
            is_dynamic: false,
            rfc_reference: Some("RFC 3551".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 6,
            media_type: MediaFrameType::Audio,
            codec_name: "DVI4".to_string(),
            clock_rate: 16000,
            channels: Some(1),
            is_dynamic: false,
            rfc_reference: Some("RFC 3551".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 7,
            media_type: MediaFrameType::Audio,
            codec_name: "LPC".to_string(),
            clock_rate: 8000,
            channels: Some(1),
            is_dynamic: false,
            rfc_reference: Some("RFC 3551".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 8,
            media_type: MediaFrameType::Audio,
            codec_name: "PCMA".to_string(),
            clock_rate: 8000,
            channels: Some(1),
            is_dynamic: false,
            rfc_reference: Some("RFC 3551".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 9,
            media_type: MediaFrameType::Audio,
            codec_name: "G722".to_string(),
            clock_rate: 8000,
            channels: Some(1),
            is_dynamic: false,
            rfc_reference: Some("RFC 3551".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 10,
            media_type: MediaFrameType::Audio,
            codec_name: "L16".to_string(),
            clock_rate: 44100,
            channels: Some(2),
            is_dynamic: false,
            rfc_reference: Some("RFC 3551".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 11,
            media_type: MediaFrameType::Audio,
            codec_name: "L16".to_string(),
            clock_rate: 44100,
            channels: Some(1),
            is_dynamic: false,
            rfc_reference: Some("RFC 3551".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 12,
            media_type: MediaFrameType::Audio,
            codec_name: "QCELP".to_string(),
            clock_rate: 8000,
            channels: Some(1),
            is_dynamic: false,
            rfc_reference: Some("RFC 3551".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 13,
            media_type: MediaFrameType::Audio,
            codec_name: "CN".to_string(),
            clock_rate: 8000,
            channels: Some(1),
            is_dynamic: false,
            rfc_reference: Some("RFC 3389".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 14,
            media_type: MediaFrameType::Audio,
            codec_name: "MPA".to_string(),
            clock_rate: 90000,
            channels: None,
            is_dynamic: false,
            rfc_reference: Some("RFC 3551".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 15,
            media_type: MediaFrameType::Audio,
            codec_name: "G728".to_string(),
            clock_rate: 8000,
            channels: Some(1),
            is_dynamic: false,
            rfc_reference: Some("RFC 3551".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 16,
            media_type: MediaFrameType::Audio,
            codec_name: "DVI4".to_string(),
            clock_rate: 11025,
            channels: Some(1),
            is_dynamic: false,
            rfc_reference: Some("RFC 3551".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 17,
            media_type: MediaFrameType::Audio,
            codec_name: "DVI4".to_string(),
            clock_rate: 22050,
            channels: Some(1),
            is_dynamic: false,
            rfc_reference: Some("RFC 3551".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 18,
            media_type: MediaFrameType::Audio,
            codec_name: "G729".to_string(),
            clock_rate: 8000,
            channels: Some(1),
            is_dynamic: false,
            rfc_reference: Some("RFC 3551".to_string()),
        });

        // Video payload types (RFC 3551)
        self.add_static_type(PayloadTypeInfo {
            payload_type: 24,
            media_type: MediaFrameType::Video,
            codec_name: "unassigned".to_string(),
            clock_rate: 0,
            channels: None,
            is_dynamic: false,
            rfc_reference: Some("RFC 3551".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 25,
            media_type: MediaFrameType::Video,
            codec_name: "CelB".to_string(),
            clock_rate: 90000,
            channels: None,
            is_dynamic: false,
            rfc_reference: Some("RFC 2029".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 26,
            media_type: MediaFrameType::Video,
            codec_name: "JPEG".to_string(),
            clock_rate: 90000,
            channels: None,
            is_dynamic: false,
            rfc_reference: Some("RFC 2435".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 28,
            media_type: MediaFrameType::Video,
            codec_name: "nv".to_string(),
            clock_rate: 90000,
            channels: None,
            is_dynamic: false,
            rfc_reference: Some("RFC 3551".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 31,
            media_type: MediaFrameType::Video,
            codec_name: "H261".to_string(),
            clock_rate: 90000,
            channels: None,
            is_dynamic: false,
            rfc_reference: Some("RFC 4587".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 32,
            media_type: MediaFrameType::Video,
            codec_name: "MPV".to_string(),
            clock_rate: 90000,
            channels: None,
            is_dynamic: false,
            rfc_reference: Some("RFC 2250".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 33,
            media_type: MediaFrameType::Video,
            codec_name: "MP2T".to_string(),
            clock_rate: 90000,
            channels: None,
            is_dynamic: false,
            rfc_reference: Some("RFC 2250".to_string()),
        });

        self.add_static_type(PayloadTypeInfo {
            payload_type: 34,
            media_type: MediaFrameType::Video,
            codec_name: "H263".to_string(),
            clock_rate: 90000,
            channels: None,
            is_dynamic: false,
            rfc_reference: Some("RFC 4629".to_string()),
        });

        // Common dynamic payload types (these are just examples, actual mappings would be negotiated)
        self.add_dynamic_type(PayloadTypeInfo {
            payload_type: 96,
            media_type: MediaFrameType::Video,
            codec_name: "H264".to_string(),
            clock_rate: 90000,
            channels: None,
            is_dynamic: true,
            rfc_reference: Some("RFC 6184".to_string()),
        });

        self.add_dynamic_type(PayloadTypeInfo {
            payload_type: 97,
            media_type: MediaFrameType::Video,
            codec_name: "VP8".to_string(),
            clock_rate: 90000,
            channels: None,
            is_dynamic: true,
            rfc_reference: Some("RFC 7741".to_string()),
        });

        self.add_dynamic_type(PayloadTypeInfo {
            payload_type: 98,
            media_type: MediaFrameType::Video,
            codec_name: "VP9".to_string(),
            clock_rate: 90000,
            channels: None,
            is_dynamic: true,
            rfc_reference: Some("draft-ietf-payload-vp9".to_string()),
        });

        self.add_dynamic_type(PayloadTypeInfo {
            payload_type: 111,
            media_type: MediaFrameType::Audio,
            codec_name: "OPUS".to_string(),
            clock_rate: 48000,
            channels: Some(2),
            is_dynamic: true,
            rfc_reference: Some("RFC 7587".to_string()),
        });
    }

    /// Add a static payload type
    fn add_static_type(&mut self, info: PayloadTypeInfo) {
        self.static_types.insert(info.payload_type, info);
    }

    /// Add a dynamic payload type
    fn add_dynamic_type(&mut self, info: PayloadTypeInfo) {
        self.dynamic_types.insert(info.payload_type, info);
    }

    /// Register a dynamic payload type mapping
    pub fn register_dynamic_payload(&mut self, info: PayloadTypeInfo) -> Result<(), String> {
        if info.payload_type < 96 || info.payload_type > 127 {
            return Err(format!("Dynamic payload types must be in range 96-127, got {}", info.payload_type));
        }

        self.dynamic_types.insert(info.payload_type, info);
        Ok(())
    }

    /// Get payload type information
    pub fn get_payload_info(&self, payload_type: u8) -> Option<&PayloadTypeInfo> {
        // First check static types
        if let Some(info) = self.static_types.get(&payload_type) {
            return Some(info);
        }

        // Then check dynamic types
        self.dynamic_types.get(&payload_type)
    }

    /// Determine media frame type from payload type
    pub fn get_media_frame_type(&self, payload_type: u8) -> MediaFrameType {
        if let Some(info) = self.get_payload_info(payload_type) {
            info.media_type
        } else {
            // RFC 3551 fallback for unregistered payload types
            match payload_type {
                0..=23 => MediaFrameType::Audio,     // Audio range
                24..=34 => MediaFrameType::Video,    // Video range
                35..=71 => MediaFrameType::Data,     // Unassigned - treat as data
                72..=76 => MediaFrameType::Data,     // Reserved for RTCP conflict avoidance
                77..=95 => MediaFrameType::Data,     // Unassigned - treat as data
                96..=127 => MediaFrameType::Data,    // Dynamic - default to data if not registered
                _ => MediaFrameType::Data,           // Invalid - treat as data
            }
        }
    }

    /// Get codec name for payload type
    pub fn get_codec_name(&self, payload_type: u8) -> String {
        if let Some(info) = self.get_payload_info(payload_type) {
            info.codec_name.clone()
        } else {
            format!("Unknown-{}", payload_type)
        }
    }

    /// Check if payload type is dynamic
    pub fn is_dynamic_payload_type(payload_type: u8) -> bool {
        payload_type >= 96 && payload_type <= 127
    }

    /// Check if payload type is static
    pub fn is_static_payload_type(payload_type: u8) -> bool {
        payload_type <= 95
    }

    /// List all registered payload types
    pub fn list_all_payload_types(&self) -> Vec<&PayloadTypeInfo> {
        let mut types: Vec<&PayloadTypeInfo> = self.static_types.values().collect();
        types.extend(self.dynamic_types.values());
        types.sort_by_key(|info| info.payload_type);
        types
    }
}

/// Get the global payload type registry
pub fn get_global_registry() -> &'static PayloadTypeRegistry {
    PAYLOAD_REGISTRY.get_or_init(|| PayloadTypeRegistry::new())
}

/// Get media frame type for payload type (convenience function)
pub fn get_media_frame_type(payload_type: u8) -> MediaFrameType {
    get_global_registry().get_media_frame_type(payload_type)
}

/// Get codec name for payload type (convenience function)
pub fn get_codec_name(payload_type: u8) -> String {
    get_global_registry().get_codec_name(payload_type)
}

/// Get payload type information (convenience function)
pub fn get_payload_info(payload_type: u8) -> Option<&'static PayloadTypeInfo> {
    get_global_registry().get_payload_info(payload_type)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_rfc3551_audio_types() {
        let registry = PayloadTypeRegistry::new();

        // Test PCMU
        let pcmu = registry.get_payload_info(0).unwrap();
        assert_eq!(pcmu.codec_name, "PCMU");
        assert_eq!(pcmu.media_type, MediaFrameType::Audio);
        assert_eq!(pcmu.clock_rate, 8000);

        // Test PCMA
        let pcma = registry.get_payload_info(8).unwrap();
        assert_eq!(pcma.codec_name, "PCMA");
        assert_eq!(pcma.media_type, MediaFrameType::Audio);
        assert_eq!(pcma.clock_rate, 8000);
    }

    #[test]
    fn test_rfc3551_video_types() {
        let registry = PayloadTypeRegistry::new();

        // Test H261
        let h261 = registry.get_payload_info(31).unwrap();
        assert_eq!(h261.codec_name, "H261");
        assert_eq!(h261.media_type, MediaFrameType::Video);
        assert_eq!(h261.clock_rate, 90000);
    }

    #[test]
    fn test_dynamic_types() {
        let registry = PayloadTypeRegistry::new();

        // Test H264 (common dynamic type)
        let h264 = registry.get_payload_info(96).unwrap();
        assert_eq!(h264.codec_name, "H264");
        assert_eq!(h264.media_type, MediaFrameType::Video);
        assert!(h264.is_dynamic);
    }

    #[test]
    fn test_media_frame_type_fallback() {
        let registry = PayloadTypeRegistry::new();

        // Test unregistered audio range
        assert_eq!(registry.get_media_frame_type(1), MediaFrameType::Audio);

        // Test unregistered video range
        assert_eq!(registry.get_media_frame_type(27), MediaFrameType::Video);

        // Test unassigned range
        assert_eq!(registry.get_media_frame_type(50), MediaFrameType::Data);

        // Test dynamic range (unregistered)
        assert_eq!(registry.get_media_frame_type(100), MediaFrameType::Data);
    }
}