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
//! AudioSocket is a simple TCP-based protocol for sending and receiving real-time audio streams.
//!
//! This crate is a port of a [Go library](https://github.com/CyCoreSystems/audiosocket).
//!
//! ```
//! use std::{convert::TryFrom, str::FromStr};
//!
//! use audiosocket::{Message, RawMessage, Type};
//! use uuid::Uuid;
//!
//! let recv = [
//!     // Message contains a stream identifier.
//!     1u8,
//!
//!     // Payload length is 16 bytes.
//!     0,
//!     16,
//!
//!     // Payload with UUID.
//!     4,
//!     54,
//!     67,
//!     12,
//!     43,
//!     2,
//!     98,
//!     76,
//!     32,
//!     50,
//!     87,
//!     5,
//!     1,
//!     33,
//!     43,
//!     87
//! ];
//!
//! let raw_message = RawMessage::try_from(&recv[..]).unwrap();
//! assert_eq!(*raw_message.message_type(), Type::Identifier);
//!
//! let message = Message::try_from(raw_message).unwrap();
//! assert_eq!(message, Message::Identifier(Uuid::from_str("0436430c-2b02-624c-2032-570501212b57").unwrap()))
//! ```

use std::{
    array::TryFromSliceError,
    convert::{TryFrom, TryInto},
    mem::size_of,
    num::TryFromIntError,
};

use num_enum::{IntoPrimitive, TryFromPrimitive, TryFromPrimitiveError};
use thiserror::Error;
use uuid::{Error as UuidError, Uuid};

/// Library errors.
///
/// Note that this list doesn't contain Asterisk errors,
/// that are sent via AudioSocket messages (use [`ErrorType`] for that purpose).
#[derive(Error, Debug)]
pub enum AudioSocketError {
    #[error("Message contains empty payload, despite having type that doesn't support it")]
    EmptyPayload,

    #[error("UUID provided in payload is invalid")]
    InvalidIdentifier(UuidError),

    #[error("Payload contained invalid message type")]
    IncorrectMessageType(TryFromPrimitiveError<Type>),

    #[error("Payload contained invalid error code")]
    IncorrectErrorCode(TryFromPrimitiveError<ErrorType>),

    #[error("Provided slice doesn't contain correct AudioSocket message")]
    InvalidRawMessage,

    #[error("AudioSocket message contains invalid length")]
    IncorrectLengthProvided(TryFromSliceError),

    #[error("Unable to cast payload length to 16-bit integer")]
    LengthIsTooLarge(TryFromIntError),
}

/// AudioSocket message type.
#[repr(u8)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, IntoPrimitive, TryFromPrimitive)]
pub enum Type {
    /// Message indicates that a connection was closed.
    ///
    /// Closing socket also works.
    Terminate = 0x00,

    /// Payload of current message contains UUID of the stream.
    Identifier = 0x01,

    /// Message indicates presence of silence on the line.
    Silence = 0x02,

    /// Payload of current message possibly contains audio itself.
    Audio = 0x10,

    /// Current message possibly contains error in payload.
    Error = 0xff,
}

/// Type of error, that message may contain.
#[repr(u8)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, IntoPrimitive, TryFromPrimitive)]
pub enum ErrorType {
    /// No error is present.
    None = 0x00,

    /// Call has hung up.
    Hangup = 0x01,

    /// Asterisk had an error trying to forward an audio frame.
    FrameForwarding = 0x02,

    /// Asterisk had a memory error.
    Memory = 0x04,
}

/// AudioSocket raw message.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub struct RawMessage<'s> {
    /// Type of current message.
    message_type: Type,

    /// Message payload, with `length` max size.
    ///
    /// May be empty, in case if message is an error.
    payload: Option<&'s [u8]>,
}

impl<'s> RawMessage<'s> {
    /// Create [`RawMessage`] from raw parts.
    ///
    /// You have to ensure that payload size is <= `length`.
    pub fn from_parts(message_type: Type, payload: Option<&'s [u8]>) -> Self {
        RawMessage {
            message_type,
            payload
        }
    }

    /// Get [`RawMessage`] type.
    pub fn message_type(&self) -> &Type {
        &self.message_type
    }

    /// Get [`RawMessage`] payload.
    pub fn payload(&self) -> &Option<&[u8]> {
        &self.payload
    }
}

impl<'s> TryFrom<&'s [u8]> for RawMessage<'s> {
    type Error = AudioSocketError;

    fn try_from(value: &'s [u8]) -> Result<Self, Self::Error> {
        let message_type = value
            .get(0)
            .ok_or(AudioSocketError::InvalidRawMessage)
            .map(|message_type| Type::try_from_primitive(*message_type))?
            .map_err(AudioSocketError::IncorrectMessageType)?;

        let length = value
            .get(1..=2)
            .ok_or(AudioSocketError::InvalidRawMessage)
            .map(|length| -> Result<u16, TryFromSliceError> {
                Ok(u16::from_be_bytes(length.try_into()?))
            })?
            .map_err(AudioSocketError::IncorrectLengthProvided)?;

        Ok(RawMessage::from_parts(message_type, value.get(3..usize::from(length + 3))))
    }
}

/// AudioSocket message.
///
/// You may use [`RawMessage`] to obtain one.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Message<'r> {
    /// Message indicates that a connection was closed.
    ///
    /// Closing socket also works.
    Terminate,

    /// Message contains UUID of current stream.
    Identifier(Uuid),

    /// Message indicates presence of silence on the line.
    Silence,

    /// Message possibly contains signed linear, 16-bit, 8kHz, mono PCM (little-endian) audio payload.
    Audio(Option<&'r [u8]>),

    /// Message indicates Asterisk error, and possibly contains [`ErrorType`].
    Error(Option<ErrorType>),
}

impl<'s> TryFrom<RawMessage<'s>> for Message<'s> {
    type Error = AudioSocketError;

    fn try_from(raw: RawMessage<'s>) -> Result<Self, <Message as TryFrom<RawMessage>>::Error> {
        match raw.message_type {
            Type::Terminate => Ok(Message::Terminate),
            Type::Identifier => {
                let id = raw.payload.ok_or(AudioSocketError::EmptyPayload)?;

                Ok(Message::Identifier(
                    Uuid::from_slice(&id).map_err(AudioSocketError::InvalidIdentifier)?,
                ))
            }
            Type::Silence => Ok(Message::Silence),
            Type::Audio => Ok(Message::Audio(
                raw.payload,
            )),
            Type::Error => {
                if let Some(code) = raw
                    .payload
                    .map(|payload| payload.first())
                    .flatten()
                    .copied()
                {
                    Ok(Message::Error(Some(
                        ErrorType::try_from_primitive(code)
                            .map_err(AudioSocketError::IncorrectErrorCode)?,
                    )))
                } else {
                    Ok(Message::Error(None))
                }
            }
        }
    }
}

impl TryFrom<Message<'_>> for Vec<u8> {
    type Error = AudioSocketError;

    fn try_from(message: Message<'_>) -> Result<Self, Self::Error> {
        match message {
            Message::Terminate => Ok(Vec::from([Type::Terminate.into(), 0x00, 0x00])),
            Message::Identifier(id) => {
                let mut buf =
                    Vec::with_capacity(size_of::<u8>() + size_of::<u16>() + size_of::<u128>());

                buf.push(Type::Identifier.into());
                buf.extend_from_slice(&u16::try_from(size_of::<u128>()).unwrap().to_be_bytes());
                buf.extend_from_slice(id.as_bytes());

                Ok(buf)
            }
            Message::Silence => Ok(Vec::from([Type::Silence.into(), 0x00, 0x00])),
            Message::Audio(a) => {
                let len = a.as_ref().map(|audio| audio.len()).unwrap_or(0);
                let mut buf = Vec::with_capacity(size_of::<u8>() + size_of::<u16>() + len);

                buf.push(Type::Audio.into());
                buf.extend_from_slice(
                    &u16::try_from(len)
                        .map_err(AudioSocketError::LengthIsTooLarge)?
                        .to_be_bytes(),
                );

                if let Some(audio) = a {
                    buf.extend_from_slice(audio);
                }

                Ok(buf)
            }
            Message::Error(e) => match e {
                Some(error) => Ok(Vec::from([Type::Error.into(), 0x00, 0x01, error.into()])),
                None => Ok(Vec::from([Type::Error.into(), 0x00, 0x00])),
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{
        convert::{TryFrom, TryInto},
        str::FromStr,
    };

    use uuid::Uuid;

    use crate::{ErrorType, Message, RawMessage};

    #[test]
    fn terminate_cast_test() {
        let terminate = [0u8, 0u8, 0u8];

        let msg: Message = RawMessage::try_from(&terminate[..])
            .unwrap()
            .try_into()
            .unwrap();

        assert_eq!(msg, Message::Terminate);
        assert_eq!(
            TryInto::<Vec<u8>>::try_into(msg).unwrap(),
            terminate.to_vec()
        );
    }

    #[test]
    fn identifier_cast_test() {
        let identifier = [
            1u8, 0, 16, 4, 54, 67, 12, 43, 2, 98, 76, 32, 50, 87, 5, 1, 33, 43, 87,
        ];

        let msg: Message = RawMessage::try_from(&identifier[..])
            .unwrap()
            .try_into()
            .unwrap();

        assert_eq!(
            msg,
            Message::Identifier(Uuid::from_str("0436430c-2b02-624c-2032-570501212b57").unwrap())
        );
        assert_eq!(
            TryInto::<Vec<u8>>::try_into(msg).unwrap(),
            identifier.to_vec()
        );
    }

    #[test]
    fn silence_cast_test() {
        let silence = [2u8, 0u8, 0u8];

        let msg: Message = RawMessage::try_from(&silence[..])
            .unwrap()
            .try_into()
            .unwrap();

        assert_eq!(msg, Message::Silence);
        assert_eq!(TryInto::<Vec<u8>>::try_into(msg).unwrap(), silence.to_vec());
    }

    #[test]
    fn audio_cast_test() {
        let audio = [
            0x10, 0u8, 8u8,
            // Though those are not valid sound bytes, we can still use them,
            // as we don't check for audio validity.
            0, 1, 2, 3, 4, 5, 6, 7,
        ];

        let msg: Message = RawMessage::try_from(&audio[..])
            .unwrap()
            .try_into()
            .unwrap();

        assert_eq!(msg, Message::Audio(Some(&[0, 1, 2, 3, 4, 5, 6, 7])));
        assert_eq!(TryInto::<Vec<u8>>::try_into(msg).unwrap(), audio.to_vec());
    }

    #[test]
    fn error_cast_test() {
        let error = [0xffu8, 0, 1, 1];

        let msg: Message = RawMessage::try_from(&error[..])
            .unwrap()
            .try_into()
            .unwrap();

        assert_eq!(msg, Message::Error(Some(ErrorType::Hangup)));
        assert_eq!(TryInto::<Vec<u8>>::try_into(msg).unwrap(), error.to_vec());
    }
}