Skip to main content

active_call/callrecord/
mod.rs

1use crate::CallOption;
2use crate::{
3    call::ActiveCallType,
4    config::{CallRecordConfig, S3Vendor},
5};
6use anyhow::Result;
7use chrono::{DateTime, Local, Utc};
8use futures::stream::{FuturesUnordered, StreamExt};
9use object_store::PutPayload;
10use object_store::{
11    ObjectStore, ObjectStoreExt, aws::AmazonS3Builder, azure::MicrosoftAzureBuilder,
12    gcp::GoogleCloudStorageBuilder, path::Path as ObjectPath,
13};
14use reqwest;
15use serde::{Deserialize, Serialize};
16use serde_with::skip_serializing_none;
17use std::{
18    collections::HashMap, future::Future, path::Path, pin::Pin, str::FromStr, sync::Arc,
19    time::Instant,
20};
21use tokio::{fs::File, io::AsyncWriteExt};
22use tokio_util::sync::CancellationToken;
23use tracing::{info, warn};
24
25pub type CallRecordSender = tokio::sync::mpsc::UnboundedSender<CallRecord>;
26pub type CallRecordReceiver = tokio::sync::mpsc::UnboundedReceiver<CallRecord>;
27
28pub type FnSaveCallRecord = Arc<
29    Box<
30        dyn Fn(
31                CancellationToken,
32                Arc<dyn CallRecordFormatter>,
33                Arc<CallRecordConfig>,
34                CallRecord,
35            ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>>
36            + Send
37            + Sync,
38    >,
39>;
40
41#[derive(Debug, Clone, Serialize, Deserialize)]
42#[serde(rename_all = "camelCase")]
43pub enum CallRecordEventType {
44    Event,
45    Command,
46    Sip,
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize)]
50#[serde(rename_all = "camelCase")]
51pub struct CallRecordEvent {
52    pub r#type: CallRecordEventType,
53    pub timestamp: u64,
54    pub content: String,
55}
56
57impl CallRecordEvent {
58    pub async fn write<T: Serialize>(r#type: CallRecordEventType, obj: T, file: &mut File) {
59        let content = match serde_json::to_string(&obj) {
60            Ok(s) => s,
61            Err(_) => return,
62        };
63        let event = Self {
64            r#type,
65            timestamp: crate::media::get_timestamp(),
66            content,
67        };
68        match serde_json::to_string(&event) {
69            Ok(line) => {
70                file.write_all(format!("{}\n", line).as_bytes()).await.ok();
71            }
72            Err(_) => {}
73        }
74    }
75}
76
77#[skip_serializing_none]
78#[derive(Debug, Clone, Serialize, Deserialize, Default)]
79#[serde(rename_all = "camelCase")]
80pub struct CallRecord {
81    pub call_type: ActiveCallType,
82    pub option: Option<CallOption>,
83    pub call_id: String,
84    pub start_time: DateTime<Utc>,
85    pub ring_time: Option<DateTime<Utc>>,
86    pub answer_time: Option<DateTime<Utc>>,
87    pub end_time: DateTime<Utc>,
88    pub caller: String,
89    pub callee: String,
90    pub status_code: u16,
91    pub hangup_reason: Option<CallRecordHangupReason>,
92    #[serde(skip_serializing_if = "Vec::is_empty")]
93    #[serde(default)]
94    pub hangup_messages: Vec<CallRecordHangupMessage>,
95    #[serde(skip_serializing_if = "Vec::is_empty")]
96    #[serde(default)]
97    pub recorder: Vec<CallRecordMedia>,
98    pub extras: Option<HashMap<String, serde_json::Value>>,
99    pub dump_event_file: Option<String>,
100    pub refer_callrecord: Option<Box<CallRecord>>,
101    /// SIP Call-IDs of every dialog belonging to this session (primary leg,
102    /// bridge legs, REFER transfer legs), resolved by the sipflow capture
103    /// ([`crate::sipflow`]) so post-call hooks can merge all legs' signaling.
104    #[serde(default, skip_serializing_if = "Vec::is_empty")]
105    pub sip_call_ids: Vec<String>,
106}
107
108#[derive(Debug, Clone, Serialize, Deserialize)]
109#[serde(rename_all = "camelCase")]
110pub struct CallRecordMedia {
111    pub track_id: String,
112    pub path: String,
113    pub size: u64,
114    #[serde(skip_serializing_if = "Option::is_none")]
115    pub extra: Option<HashMap<String, serde_json::Value>>,
116}
117
118#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
119#[serde(rename_all = "camelCase")]
120pub enum CallRecordHangupReason {
121    ByCaller,
122    ByCallee,
123    ByRefer,
124    BySystem,
125    Autohangup,
126    InactivityTimeout,
127    NoAnswer,
128    NoBalance,
129    AnswerMachine,
130    ServerUnavailable,
131    Canceled,
132    Rejected,
133    Failed,
134    Other(String),
135}
136
137#[derive(Debug, Clone, Serialize, Deserialize)]
138#[serde(rename_all = "camelCase")]
139pub struct CallRecordHangupMessage {
140    pub code: u16,
141    #[serde(skip_serializing_if = "Option::is_none")]
142    pub reason: Option<String>,
143    #[serde(skip_serializing_if = "Option::is_none")]
144    pub target: Option<String>,
145}
146
147impl FromStr for CallRecordHangupReason {
148    type Err = anyhow::Error;
149
150    fn from_str(s: &str) -> Result<Self, Self::Err> {
151        match s.to_lowercase().as_str() {
152            "caller" => Ok(Self::ByCaller),
153            "callee" => Ok(Self::ByCallee),
154            "refer" => Ok(Self::ByRefer),
155            "system" => Ok(Self::BySystem),
156            "autohangup" => Ok(Self::Autohangup),
157            "inactivitytimeout" => Ok(Self::InactivityTimeout),
158            "noanswer" => Ok(Self::NoAnswer),
159            "nobalance" => Ok(Self::NoBalance),
160            "answermachine" => Ok(Self::AnswerMachine),
161            "serverunavailable" => Ok(Self::ServerUnavailable),
162            "canceled" => Ok(Self::Canceled),
163            "rejected" => Ok(Self::Rejected),
164            "failed" => Ok(Self::Failed),
165            _ => Ok(Self::Other(s.to_string())),
166        }
167    }
168}
169impl ToString for CallRecordHangupReason {
170    fn to_string(&self) -> String {
171        match self {
172            Self::ByCaller => "caller".to_string(),
173            Self::ByCallee => "callee".to_string(),
174            Self::ByRefer => "refer".to_string(),
175            Self::BySystem => "system".to_string(),
176            Self::Autohangup => "autohangup".to_string(),
177            Self::InactivityTimeout => "inactivityTimeout".to_string(),
178            Self::NoAnswer => "noAnswer".to_string(),
179            Self::NoBalance => "noBalance".to_string(),
180            Self::AnswerMachine => "answerMachine".to_string(),
181            Self::ServerUnavailable => "serverUnavailable".to_string(),
182            Self::Canceled => "canceled".to_string(),
183            Self::Rejected => "rejected".to_string(),
184            Self::Failed => "failed".to_string(),
185            Self::Other(s) => s.to_string(),
186        }
187    }
188}
189
190pub fn default_cdr_file_name(record: &CallRecord) -> String {
191    format!(
192        "{}_{}.json",
193        record
194            .start_time
195            .with_timezone(&Local)
196            .format("%Y%m%d-%H%M%S"),
197        record.call_id
198    )
199}
200
201pub trait CallRecordFormatter: Send + Sync {
202    fn format(&self, record: &CallRecord) -> Result<String> {
203        Ok(serde_json::to_string(record)?)
204    }
205    fn format_file_name(&self, record: &CallRecord) -> String;
206    fn format_dump_events_path(&self, record: &CallRecord) -> String;
207    fn format_media_path(&self, record: &CallRecord, media: &CallRecordMedia) -> String;
208}
209
210/// Post-processing hooks run by the [`CallRecordManager`] around the saver
211/// (single-record counterpart of rustpbx's `CallRecordHook`). Implement
212/// either method; both default to no-ops. Heavy work should be spawned.
213#[async_trait::async_trait]
214pub trait CallRecordHook: Send + Sync {
215    /// Runs before the record is handed to the saver; may mutate the record
216    /// (e.g. enrich fields that must appear in the persisted document).
217    async fn on_record_enrich(&self, _record: &mut CallRecord) -> Result<()> {
218        Ok(())
219    }
220
221    /// Runs after the saver finished (successfully or not).
222    async fn on_record_completed(&self, _record: &mut CallRecord) -> Result<()> {
223        Ok(())
224    }
225}
226
227pub struct DefaultCallRecordFormatter {
228    pub root: String,
229}
230
231impl Default for DefaultCallRecordFormatter {
232    fn default() -> Self {
233        Self {
234            root: "./config/cdr".to_string(),
235        }
236    }
237}
238
239impl DefaultCallRecordFormatter {
240    pub fn new_with_config(config: &CallRecordConfig) -> Self {
241        let root = match config {
242            CallRecordConfig::Local { root } => root.clone(),
243            CallRecordConfig::S3 { root, .. } => root.clone(),
244            _ => "./config/cdr".to_string(),
245        };
246        Self { root }
247    }
248}
249
250impl CallRecordFormatter for DefaultCallRecordFormatter {
251    fn format_file_name(&self, record: &CallRecord) -> String {
252        let trimmed_root = self.root.trim_end_matches('/');
253        let file_name = default_cdr_file_name(record);
254        if trimmed_root.is_empty() {
255            file_name
256        } else {
257            format!(
258                "{}/{}/{}",
259                trimmed_root,
260                record.start_time.with_timezone(&Local).format("%Y%m%d"),
261                file_name
262            )
263        }
264    }
265
266    fn format_dump_events_path(&self, record: &CallRecord) -> String {
267        format!(
268            "{}/{}/{}.jsonl",
269            self.root.trim_end_matches('/'),
270            record.start_time.with_timezone(&Local).format("%Y%m%d"),
271            record.call_id
272        )
273    }
274
275    fn format_media_path(&self, record: &CallRecord, media: &CallRecordMedia) -> String {
276        let file_name = Path::new(&media.path)
277            .file_name()
278            .unwrap_or_else(|| std::ffi::OsStr::new("unknown"))
279            .to_string_lossy()
280            .to_string();
281
282        format!(
283            "{}/{}/{}_{}_{}",
284            self.root.trim_end_matches('/'),
285            record.start_time.with_timezone(&Local).format("%Y%m%d"),
286            record.call_id,
287            media.track_id,
288            file_name
289        )
290    }
291}
292
293pub fn build_object_store_from_s3(
294    vendor: &S3Vendor,
295    bucket: &str,
296    region: &str,
297    access_key: &str,
298    secret_key: &str,
299    endpoint: &str,
300) -> Result<Arc<dyn ObjectStore>> {
301    let store: Arc<dyn ObjectStore> = match vendor {
302        S3Vendor::AWS => {
303            let builder = AmazonS3Builder::new()
304                .with_bucket_name(bucket)
305                .with_region(region)
306                .with_access_key_id(access_key)
307                .with_secret_access_key(secret_key);
308
309            let instance = if !endpoint.is_empty() {
310                builder.with_endpoint(endpoint).build()?
311            } else {
312                builder.build()?
313            };
314            Arc::new(instance)
315        }
316        S3Vendor::GCP => {
317            let instance = GoogleCloudStorageBuilder::new()
318                .with_bucket_name(bucket)
319                .with_service_account_key(secret_key)
320                .build()?;
321            Arc::new(instance)
322        }
323        S3Vendor::Azure => {
324            let instance = MicrosoftAzureBuilder::new()
325                .with_container_name(bucket)
326                .with_account(access_key)
327                .with_access_key(secret_key)
328                .build()?;
329            Arc::new(instance)
330        }
331        S3Vendor::Aliyun | S3Vendor::Tencent | S3Vendor::Minio | S3Vendor::DigitalOcean => {
332            let instance = AmazonS3Builder::new()
333                .with_bucket_name(bucket)
334                .with_region(region)
335                .with_access_key_id(access_key)
336                .with_secret_access_key(secret_key)
337                .with_endpoint(endpoint)
338                .with_virtual_hosted_style_request(false)
339                .build()?;
340            Arc::new(instance)
341        }
342    };
343
344    Ok(store)
345}
346
347pub struct CallRecordManager {
348    pub max_concurrent: usize,
349    pub sender: CallRecordSender,
350    config: Arc<CallRecordConfig>,
351    cancel_token: CancellationToken,
352    receiver: CallRecordReceiver,
353    saver_fn: FnSaveCallRecord,
354    formatter: Arc<dyn CallRecordFormatter>,
355    hooks: Vec<Arc<dyn CallRecordHook>>,
356}
357
358pub struct CallRecordManagerBuilder {
359    pub cancel_token: Option<CancellationToken>,
360    pub config: Option<CallRecordConfig>,
361    pub max_concurrent: Option<usize>,
362    saver_fn: Option<FnSaveCallRecord>,
363    formatter: Option<Arc<dyn CallRecordFormatter>>,
364    hooks: Vec<Arc<dyn CallRecordHook>>,
365}
366
367impl CallRecordManagerBuilder {
368    pub fn new() -> Self {
369        Self {
370            cancel_token: None,
371            config: None,
372            max_concurrent: None,
373            saver_fn: None,
374            formatter: None,
375            hooks: Vec::new(),
376        }
377    }
378
379    pub fn with_cancel_token(mut self, cancel_token: CancellationToken) -> Self {
380        self.cancel_token = Some(cancel_token);
381        self
382    }
383
384    pub fn with_config(mut self, config: CallRecordConfig) -> Self {
385        self.config = Some(config);
386        self
387    }
388
389    pub fn with_saver(mut self, saver: FnSaveCallRecord) -> Self {
390        self.saver_fn = Some(saver);
391        self
392    }
393
394    pub fn with_formatter(mut self, formatter: Arc<dyn CallRecordFormatter>) -> Self {
395        self.formatter = Some(formatter);
396        self
397    }
398
399    pub fn with_max_concurrent(mut self, max_concurrent: usize) -> Self {
400        self.max_concurrent = Some(max_concurrent);
401        self
402    }
403
404    /// Appends a post-processing hook (see [`CallRecordHook`]).
405    pub fn with_hook(mut self, hook: Arc<dyn CallRecordHook>) -> Self {
406        self.hooks.push(hook);
407        self
408    }
409
410    pub fn build(self) -> CallRecordManager {
411        let cancel_token = self.cancel_token.unwrap_or_default();
412        let config = Arc::new(self.config.unwrap_or_default());
413        let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
414        let saver_fn = self
415            .saver_fn
416            .unwrap_or_else(|| Arc::new(Box::new(CallRecordManager::default_saver)));
417        let formatter = self
418            .formatter
419            .unwrap_or_else(|| Arc::new(DefaultCallRecordFormatter::default()));
420        let max_concurrent = self.max_concurrent.unwrap_or(64);
421
422        match config.as_ref() {
423            CallRecordConfig::Local { root } => {
424                if !Path::new(&root).exists() {
425                    match std::fs::create_dir_all(&root) {
426                        Ok(_) => {
427                            info!("CallRecordManager created directory: {}", root);
428                        }
429                        Err(e) => {
430                            warn!("CallRecordManager failed to create directory: {}", e);
431                        }
432                    }
433                }
434            }
435            _ => {}
436        }
437
438        CallRecordManager {
439            max_concurrent,
440            cancel_token,
441            sender,
442            receiver,
443            config,
444            saver_fn,
445            formatter,
446            hooks: self.hooks,
447        }
448    }
449}
450
451impl CallRecordManager {
452    fn default_saver(
453        _cancel_token: CancellationToken,
454        formatter: Arc<dyn CallRecordFormatter>,
455        config: Arc<CallRecordConfig>,
456        record: CallRecord,
457    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> {
458        Box::pin(async move {
459            let mut record = record;
460            let start_time = Instant::now();
461            let result = match config.as_ref() {
462                CallRecordConfig::Local { .. } => {
463                    Self::save_local_record(formatter.clone(), &mut record).await
464                }
465                CallRecordConfig::S3 {
466                    vendor,
467                    bucket,
468                    region,
469                    access_key,
470                    secret_key,
471                    endpoint,
472                    with_media,
473                    keep_media_copy,
474                    part_size_mb,
475                    delete_after_upload,
476                    ..
477                } => {
478                    Self::save_with_s3_like(
479                        formatter.clone(),
480                        vendor,
481                        bucket,
482                        region,
483                        access_key,
484                        secret_key,
485                        endpoint,
486                        with_media,
487                        keep_media_copy,
488                        part_size_mb,
489                        delete_after_upload,
490                        &record,
491                    )
492                    .await
493                }
494                CallRecordConfig::Http {
495                    url,
496                    headers,
497                    with_media,
498                    keep_media_copy,
499                    part_size_mb,
500                    delete_after_upload,
501                } => {
502                    Self::save_with_http(
503                        formatter.clone(),
504                        url,
505                        headers,
506                        with_media,
507                        keep_media_copy,
508                        part_size_mb,
509                        delete_after_upload,
510                        &record,
511                    )
512                    .await
513                }
514            };
515            let file_name = match result {
516                Ok(file_name) => file_name,
517                Err(e) => {
518                    warn!("Failed to save call record: {}", e);
519                    return Err(e);
520                }
521            };
522            let elapsed = start_time.elapsed();
523            info!(
524                ?elapsed,
525                call_id = record.call_id,
526                file_name,
527                "CallRecordManager saved"
528            );
529            Ok(())
530        })
531    }
532
533    async fn save_local_record(
534        formatter: Arc<dyn CallRecordFormatter>,
535        record: &mut CallRecord,
536    ) -> Result<String> {
537        let file_content = formatter.format(record)?;
538        let file_name = formatter.format_file_name(record);
539
540        // Ensure parent directory exists
541        if let Some(parent) = Path::new(&file_name).parent() {
542            tokio::fs::create_dir_all(parent).await.map_err(|e| {
543                anyhow::anyhow!("Failed to create parent directory for {}: {}", file_name, e)
544            })?;
545        }
546
547        let mut file = File::create(&file_name).await.map_err(|e| {
548            anyhow::anyhow!("Failed to create call record file {}: {}", file_name, e)
549        })?;
550        file.write_all(file_content.as_bytes()).await?;
551        file.flush().await?;
552        Ok(file_name.to_string())
553    }
554
555    /// Stream a local file into the object store without reading the whole file
556/// into memory. `part_size` bounds the in-memory buffer; the buffered writer
557/// spills full parts as multipart uploads (files smaller than the buffer are
558/// still a single PUT). Returns the file size in bytes.
559async fn upload_file_streaming(
560    store: std::sync::Arc<dyn object_store::ObjectStore>,
561    local_path: &std::path::Path,
562    key: &ObjectPath,
563    part_size: usize,
564) -> Result<u64> {
565    use tokio::io::AsyncReadExt;
566    let size = tokio::fs::metadata(local_path).await?.len();
567    let mut file = tokio::fs::File::open(local_path).await?;
568    let mut writer = object_store::buffered::BufWriter::with_capacity(store, key.clone(), part_size);
569    let mut buf = vec![0u8; 64 * 1024];
570    loop {
571        let n = file.read(&mut buf).await?;
572        if n == 0 {
573            break;
574        }
575        writer.write_all(&buf[..n]).await?;
576    }
577    // flush + complete the multipart upload
578    tokio::io::AsyncWriteExt::shutdown(&mut writer).await?;
579    Ok(size)
580}
581
582pub async fn save_with_http(
583        formatter: Arc<dyn CallRecordFormatter>,
584        url: &String,
585        headers: &Option<HashMap<String, String>>,
586        with_media: &Option<bool>,
587        keep_media_copy: &Option<bool>,
588        part_size_mb: &Option<u64>,
589        delete_after_upload: &Option<bool>,
590        record: &CallRecord,
591    ) -> Result<String> {
592        let _ = part_size_mb; // HTTP path streams by file length; part size is S3-only
593        let mut uploaded_paths: Vec<String> = Vec::new();
594        let client = reqwest::Client::new();
595        // Serialize call record to JSON
596        let call_log_json = formatter.format(record)?;
597        // Create multipart form
598        let mut form = reqwest::multipart::Form::new().text("calllog.json", call_log_json);
599
600        // Add media files if with_media is true
601        if with_media.unwrap_or(false) {
602            for media in &record.recorder {
603                if std::path::Path::new(&media.path).exists() {
604                    let file_name = std::path::Path::new(&media.path)
605                        .file_name()
606                        .unwrap_or_else(|| std::ffi::OsStr::new("unknown"))
607                        .to_string_lossy()
608                        .to_string();
609                    match tokio::fs::File::open(&media.path).await {
610                        Ok(file) => {
611                            let len = file.metadata().await.map(|m| m.len()).unwrap_or(0);
612                            let part = match reqwest::multipart::Part::stream_with_length(
613                                reqwest::Body::wrap_stream(tokio_util::io::ReaderStream::new(file)),
614                                len,
615                            )
616                            .file_name(file_name)
617                            .mime_str("application/octet-stream")
618                            {
619                                Ok(p) => p,
620                                Err(e) => {
621                                    warn!("failed to set mime on media part: {}", e);
622                                    continue;
623                                }
624                            };
625                            form = form.part(format!("media_{}", media.track_id), part);
626                            uploaded_paths.push(media.path.clone());
627                        }
628                        Err(e) => {
629                            warn!("Failed to open media file {}: {}", media.path, e);
630                        }
631                    }
632                }
633            }
634            if let Some(dump_events_file) = &record.dump_event_file {
635                if Path::new(&dump_events_file).exists() {
636                    let file_name = Path::new(&dump_events_file)
637                        .file_name()
638                        .unwrap_or_else(|| std::ffi::OsStr::new("unknown"))
639                        .to_string_lossy()
640                        .to_string();
641                    match tokio::fs::File::open(&dump_events_file).await {
642                        Ok(file) => {
643                            let len = file.metadata().await.map(|m| m.len()).unwrap_or(0);
644                            if let Ok(part) = reqwest::multipart::Part::stream_with_length(
645                                reqwest::Body::wrap_stream(tokio_util::io::ReaderStream::new(file)),
646                                len,
647                            )
648                            .file_name(file_name.clone())
649                            .mime_str("application/octet-stream")
650                            {
651                                form = form.part(format!("dump_events_{}", file_name), part);
652                                uploaded_paths.push(dump_events_file.clone());
653                            }
654                        }
655                        Err(e) => {
656                            warn!("Failed to open events file {}: {}", dump_events_file, e);
657                        }
658                    }
659                }
660            }
661        }
662        let mut request = client.post(url).multipart(form);
663        if let Some(headers_map) = headers {
664            for (key, value) in headers_map {
665                request = request.header(key, value);
666            }
667        }
668        let response = request.send().await?;
669        if response.status().is_success() {
670            let response_text = response.text().await.unwrap_or_default();
671
672            // Delete local copies only after a successful upload.
673            let delete_local = delete_after_upload.unwrap_or(true) && !keep_media_copy.unwrap_or(false);
674            if delete_local {
675                for p in &uploaded_paths {
676                    let path = Path::new(p);
677                    if path.exists() {
678                        if let Err(e) = tokio::fs::remove_file(path).await {
679                            warn!(path = %p, "failed to delete local file after HTTP upload: {}", e);
680                        }
681                    }
682                }
683            }
684            Ok(format!("HTTP upload successful: {}", response_text))
685        } else {
686            Err(anyhow::anyhow!(
687                "HTTP upload failed with status: {} - {}",
688                response.status(),
689                response.text().await.unwrap_or_default()
690            ))
691        }
692    }
693
694    pub async fn save_with_s3_like(
695        formatter: Arc<dyn CallRecordFormatter>,
696        vendor: &S3Vendor,
697        bucket: &String,
698        region: &String,
699        access_key: &String,
700        secret_key: &String,
701        endpoint: &String,
702        with_media: &Option<bool>,
703        keep_media_copy: &Option<bool>,
704        part_size_mb: &Option<u64>,
705        delete_after_upload: &Option<bool>,
706        record: &CallRecord,
707    ) -> Result<String> {
708        let start_time = Instant::now();
709        let part_size = part_size_mb.map(|m| (m * 1024 * 1024) as usize).unwrap_or(5 * 1024 * 1024);
710        let object_store =
711            build_object_store_from_s3(vendor, bucket, region, access_key, secret_key, endpoint)?;
712
713        // Serialize call record to JSON
714        let call_log_json = formatter.format(record)?;
715        // Upload call log JSON
716        let filename = formatter.format_file_name(record);
717        let json_path = ObjectPath::from(filename.clone());
718        let buf_size = call_log_json.len();
719        let json_uploaded = match object_store
720            .put(&json_path, PutPayload::from(call_log_json))
721            .await
722        {
723            Ok(_) => {
724                info!(
725                    elapsed = start_time.elapsed().as_secs_f64(),
726                    %json_path,
727                    buf_size,
728                    "upload call record"
729                );
730                true
731            }
732            Err(e) => {
733                warn!(
734                   %json_path,
735                    "failed to upload call record: {}", e
736                );
737                false
738            }
739        };
740        // Upload media files if with_media is true
741        let mut uploaded_paths: Vec<String> = Vec::new();
742        if with_media.unwrap_or(false) {
743            let mut media_files = vec![];
744            for media in &record.recorder {
745                if Path::new(&media.path).exists() {
746                    let media_path = ObjectPath::from(formatter.format_media_path(record, media));
747                    media_files.push((media.path.clone(), media_path));
748                }
749            }
750            if let Some(dump_events_file) = &record.dump_event_file {
751                if Path::new(&dump_events_file).exists() {
752                    let dump_events_path =
753                        ObjectPath::from(formatter.format_dump_events_path(record));
754                    media_files.push((dump_events_file.clone(), dump_events_path));
755                }
756            }
757            for (path, media_path) in &media_files {
758                let start_time = Instant::now();
759                match Self::upload_file_streaming(
760                    object_store.clone(),
761                    std::path::Path::new(path),
762                    media_path,
763                    part_size,
764                )
765                .await
766                {
767                    Ok(buf_size) => {
768                        info!(
769                            elapsed = start_time.elapsed().as_secs_f64(),
770                            %media_path,
771                            buf_size,
772                            "upload media file"
773                        );
774                        uploaded_paths.push(path.clone());
775                    }
776                    Err(e) => {
777                        warn!(%media_path,"failed to upload media file: {}", e);
778                    }
779                }
780            }
781        }
782        // Delete local copies only for files whose upload succeeded.
783        let delete_local = delete_after_upload.unwrap_or(true) && !keep_media_copy.unwrap_or(false);
784        if delete_local {
785            for p in &uploaded_paths {
786                let path = Path::new(p);
787                if path.exists() {
788                    if let Err(e) = tokio::fs::remove_file(path).await {
789                        warn!(path = %p, "failed to delete local media after upload: {}", e);
790                    }
791                }
792            }
793            if json_uploaded {
794                let path = Path::new(&filename);
795                if path.exists() {
796                    if let Err(e) = tokio::fs::remove_file(path).await {
797                        warn!(path = %filename, "failed to delete local record after upload: {}", e);
798                    }
799                }
800            }
801        }
802
803        Ok(format!(
804            "{}/{}",
805            endpoint.trim_end_matches('/'),
806            json_path.to_string().trim_start_matches('/')
807        ))
808    }
809
810    pub async fn serve(&mut self) {
811        let token = self.cancel_token.clone();
812        info!("CallRecordManager serving");
813        tokio::select! {
814            _ = token.cancelled() => {}
815            _ = self.recv_loop() => {}
816        }
817        info!("CallRecordManager served");
818    }
819
820    async fn recv_loop(&mut self) -> Result<()> {
821        let mut futures = FuturesUnordered::new();
822        loop {
823            let limit = self.max_concurrent - futures.len();
824            if limit == 0 {
825                if let Some(_) = futures.next().await {}
826                continue;
827            }
828            let mut buffer = Vec::with_capacity(limit);
829            if self.receiver.recv_many(&mut buffer, limit).await == 0 {
830                break;
831            }
832
833            for mut record in buffer {
834                let cancel_token_ref = self.cancel_token.clone();
835                let save_fn_ref = self.saver_fn.clone();
836                let config_ref = self.config.clone();
837                let formatter_ref = self.formatter.clone();
838                let hooks = self.hooks.clone();
839
840                futures.push(async move {
841                    for hook in &hooks {
842                        if let Err(e) = hook.on_record_enrich(&mut record).await {
843                            warn!(
844                                call_id = record.call_id,
845                                "callrecord on_record_enrich hook failed: {}", e
846                            );
847                        }
848                    }
849                    // The saver consumes the record; keep a copy for the
850                    // post-save hooks (records are small).
851                    let result =
852                        save_fn_ref(cancel_token_ref, formatter_ref, config_ref, record.clone())
853                            .await;
854                    for hook in &hooks {
855                        if let Err(e) = hook.on_record_completed(&mut record).await {
856                            warn!(
857                                call_id = record.call_id,
858                                "callrecord on_record_completed hook failed: {}", e
859                            );
860                        }
861                    }
862                    if let Err(e) = result {
863                        warn!("Failed to save call record: {}", e);
864                    }
865                });
866            }
867            while let Some(_) = futures.next().await {}
868        }
869        Ok(())
870    }
871}
872
873#[cfg(test)]
874mod tests {
875    use super::*;
876
877    /// Records the order hook/saver invocations happen in.
878    struct OrderingHook {
879        events: std::sync::Arc<std::sync::Mutex<Vec<String>>>,
880        name: &'static str,
881    }
882
883    #[async_trait::async_trait]
884    impl CallRecordHook for OrderingHook {
885        async fn on_record_enrich(&self, record: &mut CallRecord) -> Result<()> {
886            self.events
887                .lock()
888                .unwrap()
889                .push(format!("{}:enrich", self.name));
890            // Enrich must be able to mutate the persisted record.
891            record.sip_call_ids.push(format!("from-{}", self.name));
892            Ok(())
893        }
894
895        async fn on_record_completed(&self, _record: &mut CallRecord) -> Result<()> {
896            self.events
897                .lock()
898                .unwrap()
899                .push(format!("{}:completed", self.name));
900            Ok(())
901        }
902    }
903
904    fn sample_record() -> CallRecord {
905        CallRecord {
906            call_type: crate::call::ActiveCallType::Sip,
907            option: None,
908            call_id: "s.test".into(),
909            start_time: Utc::now(),
910            ring_time: None,
911            answer_time: None,
912            end_time: Utc::now(),
913            caller: "a".into(),
914            callee: "b".into(),
915            status_code: 200,
916            hangup_reason: None,
917            hangup_messages: Vec::new(),
918            recorder: Vec::new(),
919            extras: None,
920            dump_event_file: None,
921            refer_callrecord: None,
922            sip_call_ids: Vec::new(),
923        }
924    }
925
926    #[tokio::test]
927    async fn hooks_run_around_the_saver() {
928        let events = Arc::new(std::sync::Mutex::new(Vec::new()));
929        let saved: Arc<std::sync::Mutex<Vec<String>>> = Arc::new(std::sync::Mutex::new(Vec::new()));
930
931        let saver_saved = saved.clone();
932        let saver: FnSaveCallRecord = Arc::new(Box::new(
933            move |_token, _formatter, _config, record| {
934                let saver_saved = saver_saved.clone();
935                Box::pin(async move {
936                    saver_saved.lock().unwrap().push(record.call_id.clone());
937                    Ok(())
938                })
939            },
940        ));
941
942        // Build the manager field-by-field so the test can close the
943        // channel after sending (swap out the manager's own sender clone).
944        let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
945        let mut manager = CallRecordManager {
946            max_concurrent: 4,
947            sender,
948            config: Arc::new(CallRecordConfig::default()),
949            cancel_token: CancellationToken::new(),
950            receiver,
951            saver_fn: saver,
952            formatter: Arc::new(DefaultCallRecordFormatter::default()),
953            hooks: vec![Arc::new(OrderingHook {
954                events: events.clone(),
955                name: "h1",
956            })],
957        };
958
959        manager.sender.send(sample_record()).unwrap();
960        let real_sender = std::mem::replace(
961            &mut manager.sender,
962            tokio::sync::mpsc::unbounded_channel().0,
963        );
964        drop(real_sender);
965        manager.recv_loop().await.unwrap();
966
967        let timeline = events.lock().unwrap().clone();
968        assert_eq!(
969            timeline,
970            vec!["h1:enrich".to_string(), "h1:completed".to_string()]
971        );
972        // The saver saw the enriched record.
973        assert_eq!(saved.lock().unwrap().len(), 1);
974    }
975}