Client VCData (SSRC -> UserID)
Can use this to store per guild and per channel information May also store a list of visible users/those that have talked too while Alan was in the chat
This commit is contained in:
340
src/vc/mod.rs
340
src/vc/mod.rs
@@ -1,20 +1,41 @@
|
|||||||
use std::{sync::Arc, collections::HashMap};
|
use std::{collections::HashMap, sync::Arc};
|
||||||
|
|
||||||
use serenity::{async_trait,
|
use serenity::{
|
||||||
model::{prelude::{ChannelId, Guild}, user::User},
|
async_trait,
|
||||||
prelude::{Context, Mutex, RwLock, TypeMapKey}, Client};
|
model::prelude::{ChannelId, Guild, GuildId},
|
||||||
use songbird::{EventHandler, Event, EventContext,
|
prelude::{Context, Mutex, RwLock, TypeMapKey}, Client,
|
||||||
model::{payload::{Speaking, ClientDisconnect}, id::UserId}, ffmpeg, create_player, Call, CoreEvent, events::context_data::ConnectData};
|
};
|
||||||
|
|
||||||
struct Receiver {
|
use songbird::{
|
||||||
call : Arc<Mutex<Call>>,
|
create_player,
|
||||||
users : Arc<RwLock<HashMap<u32, Option<UserId>>>>,
|
events::context_data::{ConnectData, DisconnectData},
|
||||||
|
ffmpeg,
|
||||||
|
model::{
|
||||||
|
id::UserId,
|
||||||
|
payload::{ClientDisconnect, Speaking},
|
||||||
|
},
|
||||||
|
Call, CoreEvent, Event, EventContext, EventHandler,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Eq, Hash)]
|
||||||
|
struct CallLocation {
|
||||||
|
guild: GuildId,
|
||||||
|
channel: ChannelId
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Receiver {
|
impl PartialEq for CallLocation {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.guild == other.guild && self.channel == other.channel
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct VoiceData {
|
||||||
|
call: Arc<Mutex<Call>>,
|
||||||
|
users: Arc<RwLock<HashMap<u32, Option<UserId>>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VoiceData {
|
||||||
pub fn new(call: Arc<Mutex<Call>>) -> Self {
|
pub fn new(call: Arc<Mutex<Call>>) -> Self {
|
||||||
// You can manage state here, such as a buffer of audio packet bytes so
|
|
||||||
// you can later store them in intervals.
|
|
||||||
Self {
|
Self {
|
||||||
call,
|
call,
|
||||||
users: Arc::new(RwLock::new(HashMap::default()))
|
users: Arc::new(RwLock::new(HashMap::default()))
|
||||||
@@ -22,106 +43,132 @@ impl Receiver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct VCUser;
|
struct VCData {
|
||||||
|
loc: Arc<CallLocation>,
|
||||||
|
data: Arc<RwLock<VoiceData>>
|
||||||
|
}
|
||||||
|
|
||||||
impl TypeMapKey for VCUser {
|
impl TypeMapKey for VCData {
|
||||||
type Value = Arc<RwLock<HashMap<String, VCUser>>>;
|
type Value = Arc<RwLock<HashMap<CallLocation, VCData>>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl VCData {
|
||||||
|
pub fn new(loc: CallLocation, data: VoiceData) -> Self {
|
||||||
|
// You can manage state here, such as a buffer of audio packet bytes so
|
||||||
|
// you can later store them in intervals.
|
||||||
|
VCData {
|
||||||
|
loc: Arc::new(loc),
|
||||||
|
data: Arc::new(RwLock::new(data))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn clone(&self) -> Self {
|
||||||
|
VCData {
|
||||||
|
loc: self.loc.clone(),
|
||||||
|
data: self.data.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn init(client: &Client) {
|
pub async fn init(client: &Client) {
|
||||||
let mut data = client.data.write().await;
|
let mut data = client.data.write().await;
|
||||||
data.insert::<VCUser>(Arc::new(RwLock::new(HashMap::default())));
|
data.insert::<VCData>(Arc::new(RwLock::new(HashMap::default())))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl EventHandler for Receiver {
|
impl EventHandler for VCData {
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> {
|
async fn act(&self, ctx: &EventContext<'_>) -> Option<Event> {
|
||||||
use EventContext as Ctx;
|
use EventContext as Ctx;
|
||||||
match ctx {
|
match ctx {
|
||||||
Ctx::SpeakingStateUpdate(
|
Ctx::SpeakingStateUpdate(Speaking {
|
||||||
Speaking {speaking, ssrc, user_id, ..}
|
speaking,
|
||||||
) => {
|
ssrc,
|
||||||
// Discord voice calls use RTP, where every sender uses a randomly allocated
|
user_id,
|
||||||
// *Synchronisation Source* (SSRC) to allow receivers to tell which audio
|
..
|
||||||
// stream a received packet belongs to. As this number is not derived from
|
}) => {
|
||||||
// the sender's user_id, only Discord Voice Gateway messages like this one
|
// Discord voice calls use RTP, where every sender uses a randomly allocated
|
||||||
// inform us about which random SSRC a user has been allocated. Future voice
|
// *Synchronisation Source* (SSRC) to allow receivers to tell which audio
|
||||||
// packets will contain *only* the SSRC.
|
// stream a received packet belongs to. As this number is not derived from
|
||||||
//
|
// the sender's user_id, only Discord Voice Gateway messages like this one
|
||||||
// You can implement logic here so that you can differentiate users'
|
// inform us about which random SSRC a user has been allocated. Future voice
|
||||||
// SSRCs and map the SSRC to the User ID and maintain this state.
|
// packets will contain *only* the SSRC.
|
||||||
// Using this map, you can map the `ssrc` in `voice_packet`
|
//
|
||||||
// to the user ID and handle their audio packets separately.
|
// You can implement logic here so that you can differentiate users'
|
||||||
{
|
// SSRCs and map the SSRC to the User ID and maintain this state.
|
||||||
let mut users = self.users.write().await;
|
// Using this map, you can map the `ssrc` in `voice_packet`
|
||||||
users.insert(ssrc.clone(), user_id.clone());
|
// to the user ID and handle their audio packets separately.
|
||||||
}
|
{
|
||||||
println!(
|
let data = self.data.write().await;
|
||||||
"\n\n\nSpeaking state update: user {:?} has SSRC {:?}, using {:?}\n\n\n",
|
let mut users = data.users.write().await;
|
||||||
user_id,
|
users.insert(ssrc.clone(), user_id.clone());
|
||||||
ssrc,
|
}
|
||||||
speaking,
|
println!(
|
||||||
);
|
"\n\n\nSpeaking state update: user {:?} has SSRC {:?}, using {:?}\n\n\n",
|
||||||
},
|
user_id, ssrc, speaking,
|
||||||
Ctx::SpeakingUpdate(data) => {
|
);
|
||||||
// You can implement logic here which reacts to a user starting
|
}
|
||||||
// or stopping speaking, and to map their SSRC to User ID.
|
Ctx::SpeakingUpdate(data) => {
|
||||||
let users = self.users.read().await;
|
// You can implement logic here which reacts to a user starting
|
||||||
println!(
|
// or stopping speaking, and to map their SSRC to User ID.
|
||||||
"Source {}/{:?} has {} speaking.",
|
let vcdata = self.data.read().await;
|
||||||
data.ssrc,
|
let users = vcdata.users.read().await;
|
||||||
users.get(&data.ssrc),
|
println!(
|
||||||
if data.speaking {"started"} else {"stopped"},
|
"Source {}/{:?} has {} speaking.",
|
||||||
);
|
data.ssrc,
|
||||||
},
|
users.get(&data.ssrc),
|
||||||
Ctx::VoicePacket(data) => {
|
if data.speaking { "started" } else { "stopped" },
|
||||||
// An event which fires for every received audio packet,
|
);
|
||||||
// containing the decoded data.
|
}
|
||||||
if let Some(audio) = data.audio {
|
Ctx::VoicePacket(data) => {
|
||||||
// println!("Audio packet's first 5 samples: {:?}", audio.get(..5.min(audio.len())));
|
// An event which fires for every received audio packet,
|
||||||
// println!(
|
// containing the decoded data.
|
||||||
// "Audio packet sequence {:05} has {:04} bytes (decompressed from {}), SSRC {}",
|
if let Some(audio) = data.audio {
|
||||||
// data.packet.sequence.0,
|
// println!("Audio packet's first 5 samples: {:?}", audio.get(..5.min(audio.len())));
|
||||||
// audio.len() * std::mem::size_of::<i16>(),
|
// println!(
|
||||||
// data.packet.payload.len(),
|
// "Audio packet sequence {:05} has {:04} bytes (decompressed from {}), SSRC {}",
|
||||||
// data.packet.ssrc,
|
// data.packet.sequence.0,
|
||||||
// );
|
// audio.len() * std::mem::size_of::<i16>(),
|
||||||
} else {
|
// data.packet.payload.len(),
|
||||||
// println!("RTP packet, but no audio. Driver may not be configured to decode.");
|
// data.packet.ssrc,
|
||||||
}
|
// );
|
||||||
},
|
} else {
|
||||||
Ctx::RtcpPacket(data) => {
|
// println!("RTP packet, but no audio. Driver may not be configured to decode.");
|
||||||
// An event which fires for every received rtcp packet,
|
}
|
||||||
// containing the call statistics and reporting information.
|
}
|
||||||
// println!("RTCP packet received: {:?}", data.packet);
|
Ctx::RtcpPacket(data) => {
|
||||||
},
|
// An event which fires for every received rtcp packet,
|
||||||
Ctx::ClientDisconnect(
|
// containing the call statistics and reporting information.
|
||||||
ClientDisconnect {user_id, ..}
|
// println!("RTCP packet received: {:?}", data.packet);
|
||||||
) => {
|
}
|
||||||
// You can implement your own logic here to handle a user who has left the
|
Ctx::ClientDisconnect(ClientDisconnect { user_id, .. }) => {
|
||||||
// voice channel e.g., finalise processing of statistics etc.
|
// You can implement your own logic here to handle a user who has left the
|
||||||
// You will typically need to map the User ID to their SSRC; observed when
|
// voice channel e.g., finalise processing of statistics etc.
|
||||||
// first speaking.
|
// You will typically need to map the User ID to their SSRC; observed when
|
||||||
|
// first speaking.
|
||||||
|
|
||||||
println!("Client disconnected: user {:?}", user_id);
|
println!("Client disconnected: user {:?}", user_id);
|
||||||
},
|
}
|
||||||
Ctx::DriverConnect(
|
Ctx::DriverConnect(ConnectData { channel_id, .. }) => {
|
||||||
ConnectData { channel_id, ..}
|
println!("VoiceDriver is connected.");
|
||||||
) => {
|
}
|
||||||
println!("VoiceDriver is connected.");
|
Ctx::DriverDisconnect(DisconnectData {
|
||||||
},
|
channel_id,
|
||||||
_ => {
|
guild_id,
|
||||||
// We won't be registering this struct for any more event classes.
|
..
|
||||||
unimplemented!()
|
}) => {
|
||||||
|
// TODO: Remove data from GuildVoiceData
|
||||||
}
|
}
|
||||||
}
|
_ => {
|
||||||
|
// We won't be registering this struct for any more event classes.
|
||||||
|
unimplemented!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn playFile(call: Arc<Mutex<Call>>, file: String) {
|
pub async fn play_file(call: Arc<Mutex<Call>>, file: String) {
|
||||||
let mut call = call.lock().await;
|
let mut call = call.lock().await;
|
||||||
let ff_src = ffmpeg(file).await.expect("Unable to find file.");
|
let ff_src = ffmpeg(file).await.expect("Unable to find file.");
|
||||||
let (audio, handle) = create_player(ff_src);
|
let (audio, handle) = create_player(ff_src);
|
||||||
@@ -132,54 +179,71 @@ pub async fn join(ctx: Context, guild: Guild, cid: ChannelId) -> Option<Arc<Mute
|
|||||||
let manager = songbird::get(&ctx).await.expect("Songbird: intialization");
|
let manager = songbird::get(&ctx).await.expect("Songbird: intialization");
|
||||||
let (call, status) = manager.join(guild.id, cid).await;
|
let (call, status) = manager.join(guild.id, cid).await;
|
||||||
match status {
|
match status {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let call_handle = call.clone();
|
let vc_data: VCData = VCData::new(
|
||||||
{
|
CallLocation {
|
||||||
let mut call = call.lock().await;
|
guild: guild.id,
|
||||||
|
channel: cid
|
||||||
|
}, VoiceData::new(
|
||||||
|
call.clone()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
{
|
||||||
|
let data = ctx.data.read().await;
|
||||||
|
match data.get::<VCData>() {
|
||||||
|
Some(vc_guild) => {
|
||||||
|
let mut vc_guild = vc_guild.write().await;
|
||||||
|
vc_guild.insert(CallLocation {
|
||||||
|
guild: guild.id,
|
||||||
|
channel: cid
|
||||||
|
}, vc_data.clone());
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
println!("VoiceData for client hasn't been initialized");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let call_handle = call.clone();
|
||||||
|
{
|
||||||
|
let mut call = call.lock().await;
|
||||||
|
|
||||||
call.add_global_event(
|
call.add_global_event(
|
||||||
CoreEvent::SpeakingStateUpdate.into(),
|
CoreEvent::SpeakingStateUpdate.into(),
|
||||||
Receiver::new(call_handle.clone()),
|
vc_data.clone()
|
||||||
);
|
);
|
||||||
|
|
||||||
call.add_global_event(
|
call.add_global_event(
|
||||||
CoreEvent::SpeakingUpdate.into(),
|
CoreEvent::SpeakingUpdate.into(),
|
||||||
Receiver::new(call_handle.clone()),
|
vc_data.clone()
|
||||||
);
|
);
|
||||||
|
|
||||||
call.add_global_event(
|
call.add_global_event(
|
||||||
CoreEvent::VoicePacket.into(),
|
CoreEvent::VoicePacket.into(),
|
||||||
Receiver::new(call_handle.clone()),
|
vc_data.clone()
|
||||||
);
|
);
|
||||||
|
|
||||||
call.add_global_event(
|
call.add_global_event(
|
||||||
CoreEvent::RtcpPacket.into(),
|
CoreEvent::RtcpPacket.into(),
|
||||||
Receiver::new(call_handle.clone()),
|
vc_data.clone()
|
||||||
);
|
);
|
||||||
|
|
||||||
call.add_global_event(
|
call.add_global_event(
|
||||||
CoreEvent::ClientDisconnect.into(),
|
CoreEvent::ClientDisconnect.into(),
|
||||||
Receiver::new(call_handle.clone()),
|
vc_data.clone()
|
||||||
);
|
);
|
||||||
|
|
||||||
call.add_global_event(
|
call.add_global_event(
|
||||||
CoreEvent::ClientDisconnect.into(),
|
CoreEvent::DriverConnect.into(),
|
||||||
Receiver::new(call_handle.clone()),
|
vc_data.clone()
|
||||||
);
|
);
|
||||||
|
}
|
||||||
call.add_global_event(
|
let ding_src = std::env::var("DING_SOUND").expect("DING not found in DING_SOUND");
|
||||||
CoreEvent::DriverConnect.into(),
|
play_file(call_handle, ding_src).await;
|
||||||
Receiver::new(call_handle.clone()),
|
return Some(call);
|
||||||
);
|
}
|
||||||
}
|
Err(_err) => {
|
||||||
let ding_src =
|
println!("Error joining channel");
|
||||||
std::env::var("DING_SOUND").expect("DING not found in DING_SOUND");
|
}
|
||||||
playFile(call_handle, ding_src).await;
|
|
||||||
return Some(call);
|
|
||||||
}
|
|
||||||
Err(_err) => {
|
|
||||||
println!("Error joining channel");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user