rvoip-session-core 0.1.26

Call session management 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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# Session-Core API Developer Guide

## Table of Contents

1. [Introduction]#introduction
2. [Quick Start]#quick-start
3. [Core Concepts]#core-concepts
4. [API Reference]#api-reference
5. [Common Patterns]#common-patterns
6. [Examples]#examples
7. [Best Practices]#best-practices
8. [Troubleshooting]#troubleshooting

## Introduction

The session-core API is the primary interface for building SIP/VoIP applications with rvoip. It provides a high-level, type-safe Rust API that abstracts the complexity of SIP protocol handling, media management, and call control.

### Key Features

- **Unified Interface**: Single `SessionCoordinator` manages all aspects
- **Event-Driven**: Async callbacks for call events
- **Type-Safe**: Leverages Rust's type system for compile-time safety
- **Flexible**: Support for both simple and complex use cases
- **Production-Ready**: Built for real-world telephony applications

## Quick Start

### Basic SIP Phone in 20 Lines

```rust
use rvoip_session_core::api::*;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<()> {
    // Create and configure the session coordinator
    let coordinator = SessionManagerBuilder::new()
        .with_sip_port(5060)
        .with_handler(Arc::new(AutoAnswerHandler))
        .build()
        .await?;
    
    // Start accepting incoming calls
    SessionControl::start(&coordinator).await?;
    
    // Make an outgoing call
    let session = SessionControl::create_outgoing_call(
        &coordinator,
        "sip:alice@ourserver.com",
        "sip:bob@example.com",
        None  // SDP will be auto-generated
    ).await?;
    
    println!("Call initiated: {}", session.id());
    
    // Keep running
    tokio::signal::ctrl_c().await?;
    SessionControl::stop(&coordinator).await?;
    Ok(())
}
```

## Core Concepts

### SessionCoordinator

The `SessionCoordinator` is the central hub that coordinates all components:

```text
                    SessionCoordinator
                           |
        +------------------+------------------+
        |                  |                  |
   DialogManager      MediaManager      ConferenceManager
        |                  |                  |
  TransactionMgr      RTP/RTCP          Bridges/Mixing
```

### Session Lifecycle

```text
Outgoing Call:
create_outgoing_call() → Initiating → Ringing → Active → Terminated
                          Cancelled

Incoming Call:
INVITE → on_incoming_call() → CallDecision → Active/Rejected
                                  Defer → Async Processing
```

### Call Decisions

When an incoming call arrives, your handler must return one of:

- `CallDecision::Accept(sdp)` - Accept with optional SDP answer
- `CallDecision::Reject(reason)` - Reject with reason
- `CallDecision::Defer` - Defer for async processing
- `CallDecision::Forward(target)` - Forward to another destination

## API Reference

### SessionManagerBuilder

Configure and build a SessionCoordinator:

```rust
let coordinator = SessionManagerBuilder::new()
    // Network Configuration
    .with_sip_port(5060)
    .with_media_ports(10000, 20000)
    .with_local_address("sip:pbx@192.168.1.100:5060")
    
    // Call Handling
    .with_handler(Arc::new(MyCallHandler))
    
    // Advanced Options
    .with_max_sessions(1000)
    .with_session_timeout(Duration::from_secs(3600))
    
    .build()
    .await?;
```

### SessionControl Trait

Main control operations:

```rust
// Call Management
SessionControl::create_outgoing_call(&coordinator, from, to, sdp).await?;
SessionControl::terminate_session(&coordinator, &session_id).await?;

// Call Features  
SessionControl::hold_session(&coordinator, &session_id).await?;
SessionControl::resume_session(&coordinator, &session_id).await?;
SessionControl::transfer_session(&coordinator, &session_id, target).await?;
SessionControl::send_dtmf(&coordinator, &session_id, "1234#").await?;

// Programmatic Call Handling
SessionControl::accept_incoming_call(&coordinator, &call, sdp).await?;
SessionControl::reject_incoming_call(&coordinator, &call, reason).await?;

// Monitoring
SessionControl::get_session(&coordinator, &session_id).await?;
SessionControl::list_active_sessions(&coordinator).await?;
SessionControl::get_stats(&coordinator).await?;
```

### MediaControl Trait

Media and quality management:

```rust
// SDP Negotiation
MediaControl::generate_sdp_offer(&coordinator, &session_id).await?;
MediaControl::generate_sdp_answer(&coordinator, &session_id, offer).await?;

// Media Flow
MediaControl::establish_media_flow(&coordinator, &session_id, "192.168.1.100:5004").await?;
MediaControl::start_audio_transmission(&coordinator, &session_id).await?;
MediaControl::stop_audio_transmission(&coordinator, &session_id).await?;

// Quality Monitoring
MediaControl::get_media_statistics(&coordinator, &session_id).await?;
MediaControl::start_statistics_monitoring(&coordinator, &session_id, interval).await?;
```

### CallHandler Trait

Implement to customize call handling:

```rust
#[async_trait]
impl CallHandler for MyHandler {
    async fn on_incoming_call(&self, call: IncomingCall) -> CallDecision {
        // Your logic here
        CallDecision::Accept(None)
    }
    
    async fn on_call_ended(&self, call: CallSession, reason: &str) {
        println!("Call {} ended: {}", call.id(), reason);
    }
    
    async fn on_call_established(&self, call: CallSession, local_sdp: Option<String>, remote_sdp: Option<String>) {
        println!("Call {} established", call.id());
    }
}
```

## Common Patterns

### Pattern 1: Simple Auto-Answer

```rust
struct AutoAnswerHandler;

#[async_trait]
impl CallHandler for AutoAnswerHandler {
    async fn on_incoming_call(&self, _call: IncomingCall) -> CallDecision {
        CallDecision::Accept(None)
    }
    
    async fn on_call_ended(&self, call: CallSession, reason: &str) {
        log::info!("Call {} ended: {}", call.id(), reason);
    }
}
```

### Pattern 2: Queue with Async Processing

```rust
struct QueueHandler {
    queue: Arc<Mutex<VecDeque<IncomingCall>>>,
}

#[async_trait]
impl CallHandler for QueueHandler {
    async fn on_incoming_call(&self, call: IncomingCall) -> CallDecision {
        self.queue.lock().unwrap().push_back(call);
        CallDecision::Defer
    }
    
    async fn on_call_ended(&self, call: CallSession, reason: &str) {
        // Update queue statistics
    }
}

// Process queue in background task
async fn process_queue(coordinator: Arc<SessionCoordinator>, handler: Arc<QueueHandler>) {
    loop {
        if let Some(call) = handler.queue.lock().unwrap().pop_front() {
            // Async processing: database lookup, authentication, etc.
            let result = authenticate_caller(&call.from).await;
            
            if result.is_authorized {
                let sdp = MediaControl::generate_sdp_answer(
                    &coordinator,
                    &call.id,
                    &call.sdp.unwrap()
                ).await.unwrap();
                
                SessionControl::accept_incoming_call(
                    &coordinator,
                    &call,
                    Some(sdp)
                ).await.unwrap();
            } else {
                SessionControl::reject_incoming_call(
                    &coordinator,
                    &call,
                    "Authentication failed"
                ).await.unwrap();
            }
        }
        
        tokio::time::sleep(Duration::from_millis(100)).await;
    }
}
```

### Pattern 3: Advanced Call Routing

```rust
struct SmartRouter {
    rules: Vec<RoutingRule>,
    default_action: CallDecision,
}

impl SmartRouter {
    fn route(&self, call: &IncomingCall) -> CallDecision {
        // Time-based routing
        let hour = Local::now().hour();
        if hour < 9 || hour >= 17 {
            return CallDecision::Forward("sip:afterhours@voicemail.com".to_string());
        }
        
        // VIP routing
        if self.is_vip(&call.from) {
            return CallDecision::Forward("sip:vip@priority.queue".to_string());
        }
        
        // Department routing
        for rule in &self.rules {
            if call.to.contains(&rule.pattern) {
                return CallDecision::Forward(rule.target.clone());
            }
        }
        
        self.default_action.clone()
    }
}
```

## Examples

### Example 1: Call Center Agent

```rust
async fn agent_example() -> Result<()> {
    // Configure for call center
    let coordinator = SessionManagerBuilder::new()
        .with_sip_port(5060)
        .with_handler(Arc::new(AgentHandler::new()))
        .build()
        .await?;
    
    SessionControl::start(&coordinator).await?;
    
    // Wait for calls to be routed to this agent
    // Calls are automatically answered by AgentHandler
    
    Ok(())
}

struct AgentHandler {
    agent_id: String,
    status: Arc<Mutex<AgentStatus>>,
}

#[async_trait]
impl CallHandler for AgentHandler {
    async fn on_incoming_call(&self, call: IncomingCall) -> CallDecision {
        let status = self.status.lock().unwrap().clone();
        
        match status {
            AgentStatus::Available => {
                *self.status.lock().unwrap() = AgentStatus::OnCall;
                CallDecision::Accept(None)
            }
            AgentStatus::OnBreak => {
                CallDecision::Reject("Agent on break".to_string())
            }
            AgentStatus::OnCall => {
                CallDecision::Reject("Agent busy".to_string())
            }
        }
    }
    
    async fn on_call_ended(&self, _call: CallSession, _reason: &str) {
        *self.status.lock().unwrap() = AgentStatus::Available;
    }
}
```

### Example 2: Conference Bridge

```rust
async fn conference_example(coordinator: Arc<SessionCoordinator>) -> Result<()> {
    // Create two calls
    let call1 = SessionControl::create_outgoing_call(
        &coordinator,
        "sip:conference@server.com",
        "sip:alice@example.com",
        None
    ).await?;
    
    let call2 = SessionControl::create_outgoing_call(
        &coordinator,
        "sip:conference@server.com",
        "sip:bob@example.com",
        None
    ).await?;
    
    // Wait for both to answer
    SessionControl::wait_for_answer(&coordinator, &call1.id, Duration::from_secs(30)).await?;
    SessionControl::wait_for_answer(&coordinator, &call2.id, Duration::from_secs(30)).await?;
    
    // Bridge them together
    let bridge_id = coordinator.bridge_sessions(&call1.id, &call2.id).await?;
    
    println!("Conference bridge created: {}", bridge_id);
    
    Ok(())
}
```

### Example 3: Quality Monitoring

```rust
async fn monitor_quality(coordinator: Arc<SessionCoordinator>, session_id: SessionId) -> Result<()> {
    // Start automatic monitoring
    MediaControl::start_statistics_monitoring(
        &coordinator,
        &session_id,
        Duration::from_secs(5)
    ).await?;
    
    // Manual quality checks
    let mut poor_quality_count = 0;
    
    loop {
        tokio::time::sleep(Duration::from_secs(10)).await;
        
        let stats = MediaControl::get_media_statistics(&coordinator, &session_id).await?;
        
        if let Some(stats) = stats {
            if let Some(quality) = stats.quality_metrics {
                let mos = quality.mos_score.unwrap_or(0.0);
                
                if mos < 3.0 {
                    poor_quality_count += 1;
                    log::warn!("Poor quality detected: MOS={:.1}", mos);
                    
                    if poor_quality_count >= 3 {
                        // Sustained poor quality - take action
                        notify_operations_team(&session_id, mos).await?;
                    }
                } else {
                    poor_quality_count = 0;
                }
                
                // Log metrics
                log::info!(
                    "Call {}: MOS={:.1}, Loss={:.1}%, Jitter={:.1}ms",
                    session_id,
                    mos,
                    quality.packet_loss_percent,
                    quality.jitter_ms
                );
            }
        }
        
        // Check if call is still active
        if let Ok(Some(session)) = SessionControl::get_session(&coordinator, &session_id).await {
            if session.state().is_final() {
                break;
            }
        }
    }
    
    Ok(())
}
```

## Best Practices

### 1. Resource Management

Always clean up sessions:

```rust
// Use a guard pattern
struct SessionGuard {
    coordinator: Arc<SessionCoordinator>,
    session_id: SessionId,
}

impl Drop for SessionGuard {
    fn drop(&mut self) {
        let coordinator = self.coordinator.clone();
        let session_id = self.session_id.clone();
        tokio::spawn(async move {
            let _ = SessionControl::terminate_session(&coordinator, &session_id).await;
        });
    }
}
```

### 2. Error Handling

Handle all error cases:

```rust
match SessionControl::create_outgoing_call(&coordinator, from, to, None).await {
    Ok(session) => {
        log::info!("Call created: {}", session.id());
        Ok(session)
    }
    Err(SessionError::InvalidUri(uri)) => {
        log::error!("Invalid URI: {}", uri);
        Err(AppError::BadRequest(format!("Invalid SIP URI: {}", uri)))
    }
    Err(SessionError::ResourceExhausted) => {
        log::error!("No available RTP ports");
        Err(AppError::ServiceUnavailable("System at capacity"))
    }
    Err(e) => {
        log::error!("Call failed: {}", e);
        Err(AppError::Internal(e.to_string()))
    }
}
```

### 3. Async Processing

Don't block in handlers:

```rust
// BAD - Blocks the event loop
impl CallHandler for BadHandler {
    async fn on_incoming_call(&self, call: IncomingCall) -> CallDecision {
        let result = expensive_database_query(&call.from).await; // Don't do this!
        if result.is_authorized {
            CallDecision::Accept(None)
        } else {
            CallDecision::Reject("Unauthorized")
        }
    }
}

// GOOD - Defer for async processing
impl CallHandler for GoodHandler {
    async fn on_incoming_call(&self, call: IncomingCall) -> CallDecision {
        self.pending.push(call);
        CallDecision::Defer
    }
}
```

### 4. Monitoring

Always monitor production calls:

```rust
// Set up monitoring for all calls
coordinator.set_on_session_created(|session_id| {
    tokio::spawn(monitor_quality(coordinator.clone(), session_id));
});
```

### 5. Testing

Write comprehensive tests:

```rust
#[tokio::test]
async fn test_call_rejection() {
    let coordinator = create_test_coordinator().await;
    let handler = Arc::new(RejectAllHandler);
    
    // Simulate incoming call
    let call = IncomingCall {
        id: SessionId::new(),
        from: "sip:test@example.com".to_string(),
        to: "sip:pbx@ourserver.com".to_string(),
        sdp: Some(test_sdp()),
        headers: HashMap::new(),
        received_at: Instant::now(),
    };
    
    let decision = handler.on_incoming_call(call).await;
    assert!(matches!(decision, CallDecision::Reject(_)));
}
```

## Troubleshooting

### Common Issues

1. **"No available RTP ports"**
   - Increase port range: `.with_media_ports(10000, 60000)`
   - Check firewall rules

2. **"Failed to bind SIP port"**
   - Port already in use: `lsof -i :5060`
   - Need root/sudo for ports < 1024

3. **"Media not established"**
   - Check NAT/firewall configuration
   - Verify SDP addresses are reachable
   - Enable STUN/TURN if behind NAT

4. **Poor call quality**
   - Monitor with `get_media_statistics()`
   - Check network bandwidth and latency
   - Verify codec compatibility

### Debug Logging

Enable detailed logs:

```rust
env_logger::Builder::from_env(env_logger::Env::default())
    .filter_module("rvoip_session_core", log::LevelFilter::Debug)
    .filter_module("rvoip_sip_core", log::LevelFilter::Trace)
    .init();
```

### Health Checks

Implement health monitoring:

```rust
async fn health_check(coordinator: &Arc<SessionCoordinator>) -> HealthStatus {
    let stats = SessionControl::get_stats(coordinator).await?;
    
    HealthStatus {
        active_calls: stats.active_sessions,
        total_calls: stats.total_sessions,
        failed_calls: stats.failed_sessions,
        uptime: coordinator.uptime(),
        sip_status: if coordinator.is_running() { "OK" } else { "DOWN" },
    }
}
```

## Next Steps

- Explore the [examples/]examples/ directory for complete applications
- Read the [COOKBOOK.md]COOKBOOK.md for common recipes
- Check [ARCHITECTURE.md]ARCHITECTURE.md for system design details
- Join our community for support and discussions

## API Stability

The session-core API follows semantic versioning:

- Types in `api::types` are stable
- Trait methods are stable (new methods get default implementations)
- Handler interfaces are stable
- Breaking changes require major version bump

We're committed to maintaining a stable API for production use.