active-call 0.3.80

A SIP/WebRTC voice agent
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
use super::{SynthesisClient, SynthesisOption, SynthesisType};
use crate::synthesis::{Subtitle, SynthesisEvent};
use anyhow::Result;
use async_trait::async_trait;
use aws_lc_rs::hmac;
use base64::{Engine, prelude::BASE64_STANDARD};
use chrono::Duration;
use futures::{
    SinkExt, Stream, StreamExt, future,
    stream::{self, BoxStream, SplitSink},
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use tokio::{
    net::TcpStream,
    sync::{Notify, mpsc},
};
use tokio_stream::wrappers::UnboundedReceiverStream;
use tokio_tungstenite::{
    MaybeTlsStream, WebSocketStream, connect_async, tungstenite::protocol::Message,
};
use tracing::{debug, warn};
use urlencoding;
use uuid::Uuid;

const HOST: &str = "tts.cloud.tencent.com";
const NON_STREAMING_PATH: &str = "/stream_ws";
const STREAMING_PATH: &str = "/stream_wsv2";

type WsStream = WebSocketStream<MaybeTlsStream<TcpStream>>;
type WsSink = SplitSink<WsStream, Message>;

#[derive(Debug, Serialize)]
struct WebSocketRequest {
    session_id: String,
    message_id: String,
    action: String,
    data: String,
}

impl WebSocketRequest {
    fn synthesis_action(session_id: &str, text: &str) -> Self {
        let message_id = Uuid::new_v4().to_string();
        Self {
            session_id: session_id.to_string(),
            message_id,
            action: "ACTION_SYNTHESIS".to_string(),
            data: text.to_string(),
        }
    }

    fn complete_action(session_id: &str) -> Self {
        let message_id = Uuid::new_v4().to_string();
        Self {
            session_id: session_id.to_string(),
            message_id: message_id.to_string(),
            action: "ACTION_COMPLETE".to_string(),
            data: "".to_string(),
        }
    }
}

#[derive(Debug, Deserialize)]
struct WebSocketResponse {
    code: i32,
    message: String,
    r#final: i32,
    result: WebSocketResult,
    ready: u32,
    heartbeat: u32,
}

#[derive(Debug, Deserialize)]
struct WebSocketResult {
    subtitles: Option<Vec<TencentSubtitle>>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub(super) struct TencentSubtitle {
    text: String,
    begin_time: u32,
    end_time: u32,
    begin_index: u32,
    end_index: u32,
}

impl From<&TencentSubtitle> for Subtitle {
    fn from(subtitle: &TencentSubtitle) -> Self {
        Subtitle::new(
            subtitle.text.clone(),
            subtitle.begin_time,
            subtitle.end_time,
            subtitle.begin_index,
            subtitle.end_index,
        )
    }
}

// construct request url
// for non-streaming client, text is Some
// session_id is used for tencent cloud tts service, not the session_id of media session
fn construct_request_url(option: &SynthesisOption, session_id: &str, text: Option<&str>) -> String {
    let streaming = text.is_none();
    let action = if !streaming {
        "TextToStreamAudioWS"
    } else {
        "TextToStreamAudioWSv2"
    };
    let secret_id = option.secret_id.clone().unwrap_or_default();
    let secret_key = option.secret_key.clone().unwrap_or_default();
    let app_id = option.app_id.clone().unwrap_or_default();
    let volume = option.volume.unwrap_or(0);
    let speed = option.speed.unwrap_or(0.0);
    let codec = option.codec.clone().unwrap_or_else(|| "pcm".to_string());
    let sample_rate = option.samplerate.unwrap_or(16000);
    let now = chrono::Utc::now();
    let timestamp = now.timestamp();
    let tomorrow = now + Duration::days(1);
    let expired = tomorrow.timestamp();
    let expired_str = expired.to_string();
    let sample_rate_str = sample_rate.to_string();
    let speed_str = speed.to_string();
    let timestamp_str = timestamp.to_string();
    let volume_str = volume.to_string();
    let voice_type = option
        .speaker
        .clone()
        .unwrap_or_else(|| "101001".to_string());
    let mut query_params = vec![
        ("Action", action),
        ("AppId", &app_id),
        ("SecretId", &secret_id),
        ("Timestamp", &timestamp_str),
        ("Expired", &expired_str),
        ("SessionId", &session_id),
        ("VoiceType", &voice_type),
        ("Volume", &volume_str),
        ("Speed", &speed_str),
        ("SampleRate", &sample_rate_str),
        ("Codec", &codec),
        ("EnableSubtitle", "true"),
    ];

    if let Some(text) = text {
        query_params.push(("Text", text));
    }

    // Sort query parameters by key
    query_params.sort_by(|a, b| a.0.cmp(b.0));

    // Build query string without URL encoding
    let query_string = query_params
        .iter()
        .map(|(k, v)| format!("{}={}", k, v))
        .collect::<Vec<_>>()
        .join("&");

    let path = if streaming {
        STREAMING_PATH
    } else {
        NON_STREAMING_PATH
    };

    let string_to_sign = format!("GET{}{}?{}", HOST, path, query_string);
    // Calculate signature using HMAC-SHA1
    let key = hmac::Key::new(hmac::HMAC_SHA1_FOR_LEGACY_USE_ONLY, secret_key.as_bytes());
    let tag = hmac::sign(&key, string_to_sign.as_bytes());
    let signature: String = BASE64_STANDARD.encode(tag.as_ref());
    // URL encode parameters for final URL
    let encoded_query_string = query_params
        .iter()
        .map(|(k, v)| format!("{}={}", k, urlencoding::encode(v)))
        .collect::<Vec<_>>()
        .join("&");

    format!(
        "wss://{}{}?{}&Signature={}",
        HOST,
        path,
        encoded_query_string,
        urlencoding::encode(&signature)
    )
}

// convert websocket to event stream
// text and cmd_seq and cache key are used for non-streaming mode (realtime client)
// text is for debuging purpose
fn ws_to_event_stream<T>(
    ws_stream: T,
) -> impl Stream<Item = Result<SynthesisEvent>> + Send + 'static
where
    T: Stream<Item = Result<Message, tokio_tungstenite::tungstenite::Error>>
        + Send
        + Unpin
        + 'static,
{
    let notify = Arc::new(Notify::new());
    let notify_clone = notify.clone();
    ws_stream
        .take_until(notify.notified_owned())
        .filter_map(move |message| {
            let notify = notify_clone.clone();
            async move {
                match message {
                    Ok(Message::Binary(data)) => Some(Ok(SynthesisEvent::AudioChunk(data))),
                    Ok(Message::Text(text)) => {
                        let response: WebSocketResponse =
                            serde_json::from_str(&text).expect("Tencent TTS API changed!");

                        if response.code != 0 {
                            notify.notify_one();
                            return Some(Err(anyhow::anyhow!(
                                "Tencent TTS error, code: {}, message: {}",
                                response.code,
                                response.message
                            )));
                        }

                        if response.heartbeat == 1 {
                            return None;
                        }

                        if let Some(subtitles) = response.result.subtitles {
                            let subtitles: Vec<Subtitle> =
                                subtitles.iter().map(Into::into).collect();
                            return Some(Ok(SynthesisEvent::Subtitles(subtitles)));
                        }

                        // final == 1 means the synthesis is finished, should close the websocket
                        if response.r#final == 1 {
                            notify.notify_one();
                            return Some(Ok(SynthesisEvent::Finished));
                        }

                        None
                    }
                    Ok(Message::Close(_)) => {
                        notify.notify_one();
                        warn!("Tencent TTS closed by remote");
                        None
                    }
                    Err(e) => {
                        notify.notify_one();
                        Some(Err(anyhow::anyhow!("Tencent TTS websocket error: {:?}", e)))
                    }
                    _ => None,
                }
            }
        })
}

// tencent cloud realtime tts client, non-streaming
// https://cloud.tencent.com/document/product/1073/94308
// each tts command have one websocket connection, with different session_id
pub struct RealTimeClient {
    option: SynthesisOption,
    //item: (text, option), drop tx if `end_of_stream`
    tx: Option<mpsc::UnboundedSender<(String, Option<usize>, Option<SynthesisOption>)>>,
}

impl RealTimeClient {
    fn new(option: SynthesisOption) -> Self {
        Self { option, tx: None }
    }
}

#[async_trait]
impl SynthesisClient for RealTimeClient {
    fn provider(&self) -> SynthesisType {
        SynthesisType::TencentCloud
    }

    async fn start(
        &mut self,
    ) -> Result<BoxStream<'static, (Option<usize>, Result<SynthesisEvent>)>> {
        // Tencent cloud alow 10 - 20 concurrent websocket connections for default setting, dependent on voice type
        // set the number more higher will lead to waiting for unordered results longer
        let (tx, rx) = mpsc::unbounded_channel();
        self.tx = Some(tx);
        let client_option = self.option.clone();
        let max_concurrent_tasks = client_option.max_concurrent_tasks.unwrap_or(1);
        let stream = UnboundedReceiverStream::new(rx)
            .flat_map_unordered(max_concurrent_tasks, move |(text, cmd_seq, option)| {
                // each reequest have its own session_id
                let session_id = Uuid::new_v4().to_string();
                let option = client_option.merge_with(option);
                let url = construct_request_url(&option, &session_id, Some(&text));
                stream::once(connect_async(url))
                    .flat_map(move |res| match res {
                        Ok((ws_stream, _)) => ws_to_event_stream(ws_stream).boxed(),
                        Err(e) => stream::once(future::ready(Err(e.into()))).boxed(),
                    })
                    .map(move |x| (cmd_seq, x))
                    .boxed()
            })
            .boxed();
        Ok(stream)
    }

    async fn synthesize(
        &mut self,
        text: &str,
        cmd_seq: Option<usize>,
        option: Option<SynthesisOption>,
    ) -> Result<()> {
        if let Some(tx) = &self.tx {
            tx.send((text.to_string(), cmd_seq, option))?;
        } else {
            return Err(anyhow::anyhow!("TencentCloud TTS: missing client sender"));
        };

        Ok(())
    }

    async fn stop(&mut self) -> Result<()> {
        self.tx.take();
        Ok(())
    }
}

// tencent cloud streaming tts client
// https://cloud.tencent.com/document/product/1073/108595
// all the tts commands with same play_id belong to same websocket connection
struct StreamingClient {
    session_id: String,
    option: SynthesisOption,
    sink: Option<WsSink>,
}

impl StreamingClient {
    pub fn new(option: SynthesisOption) -> Self {
        let session_id = Uuid::new_v4().to_string();
        Self {
            session_id,
            option,
            sink: None,
        }
    }
}

impl StreamingClient {
    async fn connect(&self) -> Result<WsStream> {
        let url = construct_request_url(&self.option, &self.session_id, None);
        let (mut ws_stream, _) = connect_async(url).await?;
        // waiting for ready = 1
        while let Some(message) = ws_stream.next().await {
            match message {
                Ok(Message::Text(text)) => {
                    let response = serde_json::from_str::<WebSocketResponse>(&text)?;
                    if response.ready == 1 {
                        debug!("TencentCloud TTS streaming client connected");
                        return Ok(ws_stream);
                    }

                    if response.code != 0 {
                        return Err(anyhow::anyhow!(
                            "TencentCloud TTS streaming client connecting failed: code: {}, message: {}",
                            response.code,
                            response.message
                        ));
                    }
                }
                Ok(Message::Close { .. }) => {
                    return Err(anyhow::anyhow!(
                        "TencentCloud TTS streaming client connecting failed: closed by remote"
                    ));
                }
                Err(e) => {
                    return Err(anyhow::anyhow!(
                        "TencentCloud TTS streaming client connecting failed: websocket error: {}",
                        e
                    ));
                }
                _ => {}
            }
        }

        Err(anyhow::anyhow!(
            "TencentCloud TTS streaming client connecting failed"
        ))
    }
}

#[async_trait]
impl SynthesisClient for StreamingClient {
    fn provider(&self) -> SynthesisType {
        SynthesisType::TencentCloud
    }

    async fn start(
        &mut self,
    ) -> Result<BoxStream<'static, (Option<usize>, Result<SynthesisEvent>)>> {
        let stream = self.connect().await?;
        let (ws_sink, ws_stream) = stream.split();
        self.sink = Some(ws_sink);
        let stream = ws_to_event_stream(ws_stream)
            .map(move |event| (None, event))
            .boxed();
        Ok(stream)
    }

    async fn synthesize(
        &mut self,
        text: &str,
        _cmd_seq: Option<usize>,
        _option: Option<SynthesisOption>,
    ) -> Result<()> {
        if let Some(sink) = &mut self.sink {
            let request = WebSocketRequest::synthesis_action(&self.session_id, &text);
            let data = serde_json::to_string(&request)?;
            sink.send(Message::Text(data.into())).await?;

            Ok(())
        } else {
            Err(anyhow::anyhow!("TencentCloud TTS streaming: missing sink"))
        }
    }

    async fn stop(&mut self) -> Result<()> {
        if let Some(sink) = &mut self.sink {
            let request = WebSocketRequest::complete_action(&self.session_id);
            let data = serde_json::to_string(&request)?;
            sink.send(Message::Text(data.into())).await?;
        }

        Ok(())
    }
}

pub struct TencentCloudTtsClient;

impl TencentCloudTtsClient {
    pub fn create(streaming: bool, option: &SynthesisOption) -> Result<Box<dyn SynthesisClient>> {
        if streaming {
            Ok(Box::new(StreamingClient::new(option.clone())))
        } else {
            Ok(Box::new(RealTimeClient::new(option.clone())))
        }
    }
}