diff options
| author | Zeyla Hellyer <[email protected]> | 2017-08-18 16:14:32 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-08-18 17:52:53 -0700 |
| commit | e4113570967a1fb13539efbfa2cf7285b19d95ba (patch) | |
| tree | 06bb020fcfaa5f8323454dfb525272314a900186 /src/voice | |
| parent | Move Clippy lints to a cfg_attr (diff) | |
| download | serenity-e4113570967a1fb13539efbfa2cf7285b19d95ba.tar.xz serenity-e4113570967a1fb13539efbfa2cf7285b19d95ba.zip | |
Apply rustfmt
Diffstat (limited to 'src/voice')
| -rw-r--r-- | src/voice/connection.rs | 97 | ||||
| -rw-r--r-- | src/voice/streamer.rs | 14 |
2 files changed, 61 insertions, 50 deletions
diff --git a/src/voice/connection.rs b/src/voice/connection.rs index 7cc8b3a..2d1e8f5 100644 --- a/src/voice/connection.rs +++ b/src/voice/connection.rs @@ -1,11 +1,6 @@ use byteorder::{BigEndian, LittleEndian, ReadBytesExt, WriteBytesExt}; -use opus::{ - packet as opus_packet, - Application as CodingMode, - Channels, - Decoder as OpusDecoder, - Encoder as OpusEncoder, -}; +use opus::{Application as CodingMode, Channels, Decoder as OpusDecoder, Encoder as OpusEncoder, + packet as opus_packet}; use sodiumoxide::crypto::secretbox::{self, Key, Nonce}; use std::collections::HashMap; use std::io::Write; @@ -16,7 +11,7 @@ use std::thread::{self, Builder as ThreadBuilder, JoinHandle}; use std::time::Duration; use super::audio::{AudioReceiver, AudioSource, HEADER_LEN, SAMPLE_RATE}; use super::connection_info::ConnectionInfo; -use super::{payload, VoiceError, CRYPTO_MODE}; +use super::{CRYPTO_MODE, VoiceError, payload}; use websocket::client::Url as WebsocketUrl; use websocket::sync::client::ClientBuilder; use websocket::sync::stream::{AsTcpStream, TcpStream, TlsStream}; @@ -115,27 +110,27 @@ impl Connection { // Find the position in the bytes that contains the first byte of 0, // indicating the "end of the address". - let index = bytes - .iter() - .skip(4) - .position(|&x| x == 0) - .ok_or(Error::Voice(VoiceError::FindingByte))?; + let index = bytes.iter().skip(4).position(|&x| x == 0).ok_or( + Error::Voice( + VoiceError::FindingByte, + ), + )?; let pos = 4 + index; let addr = String::from_utf8_lossy(&bytes[4..pos]); let port_pos = len - 2; let port = (&bytes[port_pos..]).read_u16::<LittleEndian>()?; - client - .send_json(&payload::build_select_protocol(addr, port))?; + client.send_json( + &payload::build_select_protocol(addr, port), + )?; } let key = encryption_key(&mut client)?; - let _ = client - .stream_ref() - .as_tcp() - .set_read_timeout(Some(Duration::from_millis(25))); + let _ = client.stream_ref().as_tcp().set_read_timeout( + Some(Duration::from_millis(25)), + ); let mutexed_client = Arc::new(Mutex::new(client)); let thread_items = start_threads(mutexed_client.clone(), &udp)?; @@ -183,17 +178,21 @@ impl Connection { let timestamp = handle.read_u32::<BigEndian>()?; let ssrc = handle.read_u32::<BigEndian>()?; - nonce.0[..HEADER_LEN] - .clone_from_slice(&packet[..HEADER_LEN]); + nonce.0[..HEADER_LEN].clone_from_slice( + &packet[..HEADER_LEN], + ); - if let Ok(decrypted) = - secretbox::open(&packet[HEADER_LEN..], &nonce, &self.key) { + if let Ok(decrypted) = secretbox::open( + &packet[HEADER_LEN..], + &nonce, + &self.key, + ) { let channels = opus_packet::get_nb_channels(&decrypted)?; let entry = - self.decoder_map.entry((ssrc, channels)).or_insert_with( - || OpusDecoder::new(SAMPLE_RATE, channels).unwrap(), - ); + self.decoder_map.entry((ssrc, channels)).or_insert_with(|| { + OpusDecoder::new(SAMPLE_RATE, channels).unwrap() + }); let len = entry.decode(&decrypted, &mut buffer, false)?; @@ -201,8 +200,13 @@ impl Connection { let b = if is_stereo { len * 2 } else { len }; - receiver - .voice_packet(ssrc, seq, timestamp, is_stereo, &buffer[..b]); + receiver.voice_packet( + ssrc, + seq, + timestamp, + is_stereo, + &buffer[..b], + ); } }, ReceiverStatus::Websocket(VoiceEvent::Speaking(ev)) => { @@ -223,10 +227,9 @@ impl Connection { // Send the voice websocket keepalive if it's time if self.keepalive_timer.check() { - self.client - .lock() - .unwrap() - .send_json(&payload::build_keepalive())?; + self.client.lock().unwrap().send_json( + &payload::build_keepalive(), + )?; } // Send UDP keepalive if it's time @@ -284,14 +287,17 @@ impl Connection { cursor.write_u32::<BigEndian>(self.ssrc)?; } - nonce.0[..HEADER_LEN] - .clone_from_slice(&packet[..HEADER_LEN]); + nonce.0[..HEADER_LEN].clone_from_slice( + &packet[..HEADER_LEN], + ); let sl_index = packet.len() - 16; let buffer_len = if self.encoder_stereo { 960 * 2 } else { 960 }; - let len = self.encoder - .encode(&buffer[..buffer_len], &mut packet[HEADER_LEN..sl_index])?; + let len = self.encoder.encode( + &buffer[..buffer_len], + &mut packet[HEADER_LEN..sl_index], + )?; let crypted = { let slice = &packet[HEADER_LEN..HEADER_LEN + len]; secretbox::seal(slice, &nonce, &self.key) @@ -353,10 +359,11 @@ impl Connection { self.speaking = speaking; - self.client - .lock() - .unwrap() - .send_json(&payload::build_speaking(speaking)) + self.client.lock().unwrap().send_json( + &payload::build_speaking( + speaking, + ), + ) } } @@ -376,8 +383,9 @@ fn generate_url(endpoint: &mut String) -> Result<WebsocketUrl> { endpoint.truncate(len - 3); } - WebsocketUrl::parse(&format!("wss://{}", endpoint)) - .or(Err(Error::Voice(VoiceError::EndpointUrl))) + WebsocketUrl::parse(&format!("wss://{}", endpoint)).or(Err( + Error::Voice(VoiceError::EndpointUrl), + )) } #[inline] @@ -389,8 +397,9 @@ fn encryption_key(client: &mut Client) -> Result<Key> { return Err(Error::Voice(VoiceError::VoiceModeInvalid)); } - return Key::from_slice(&ready.secret_key) - .ok_or(Error::Voice(VoiceError::KeyGen)); + return Key::from_slice(&ready.secret_key).ok_or(Error::Voice( + VoiceError::KeyGen, + )); }, VoiceEvent::Unknown(op, value) => { debug!( diff --git a/src/voice/streamer.rs b/src/voice/streamer.rs index c8400f0..3f40bae 100644 --- a/src/voice/streamer.rs +++ b/src/voice/streamer.rs @@ -101,9 +101,11 @@ pub fn ytdl(uri: &str) -> Result<Box<AudioSource>> { }; let uri = match obj.remove("url") { - Some(v) => match v { - Value::String(uri) => uri, - other => return Err(Error::Voice(VoiceError::YouTubeDLUrl(other))), + Some(v) => { + match v { + Value::String(uri) => uri, + other => return Err(Error::Voice(VoiceError::YouTubeDLUrl(other))), + } }, None => return Err(Error::Voice(VoiceError::YouTubeDLUrl(Value::Object(obj)))), }; @@ -129,9 +131,9 @@ fn is_stereo(path: &OsStr) -> Result<bool> { .ok_or(Error::Voice(VoiceError::Streams))?; let check = streams.iter().any(|stream| { - let channels = stream - .as_object() - .and_then(|m| m.get("channels").and_then(|v| v.as_i64())); + let channels = stream.as_object().and_then(|m| { + m.get("channels").and_then(|v| v.as_i64()) + }); channels == Some(2) }); |