Skip to content

Commit f579cd0

Browse files
committed
Expose participant active event
1 parent ab25c30 commit f579cd0

5 files changed

Lines changed: 62 additions & 5 deletions

File tree

livekit/src/proto.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ impl From<participant_info::Kind> for participant::ParticipantKind {
148148
}
149149
}
150150

151+
impl From<participant_info::State> for participant::ParticipantState {
152+
fn from(value: participant_info::State) -> Self {
153+
match value {
154+
participant_info::State::Joining => participant::ParticipantState::Joining,
155+
participant_info::State::Joined => participant::ParticipantState::Joined,
156+
participant_info::State::Active => participant::ParticipantState::Active,
157+
participant_info::State::Disconnected => participant::ParticipantState::Disconnected
158+
}
159+
}
160+
}
161+
151162
impl From<ChatMessage> for RoomChatMessage {
152163
fn from(proto_msg: ChatMessage) -> Self {
153164
RoomChatMessage {

livekit/src/room/mod.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub use self::{
4545
};
4646
pub use crate::rtc_engine::SimulateScenario;
4747
use crate::{
48-
participant::ConnectionQuality,
48+
participant::{ConnectionQuality, ParticipantState},
4949
prelude::*,
5050
registered_audio_filter_plugins,
5151
rtc_engine::{
@@ -86,7 +86,16 @@ pub enum RoomError {
8686
#[derive(Clone, Debug)]
8787
#[non_exhaustive]
8888
pub enum RoomEvent {
89+
/// Remote participant joined the room.
90+
///
91+
/// This event is fired immediately after a participant joins before
92+
/// it is able to receive data messages. To send data messages in response
93+
/// to a participant joining, respond to the [`Self::ParticipantActive`] event instead.
94+
///
8995
ParticipantConnected(RemoteParticipant),
96+
/// Remote participant is active and ready to receive data messages.
97+
ParticipantActive(RemoteParticipant),
98+
/// Remote participant disconnected from the room.
9099
ParticipantDisconnected(RemoteParticipant),
91100
LocalTrackPublished {
92101
publication: LocalTrackPublication,
@@ -493,6 +502,7 @@ impl Room {
493502
let local_participant = LocalParticipant::new(
494503
rtc_engine.clone(),
495504
pi.kind().into(),
505+
pi.state().into(),
496506
pi.sid.try_into().unwrap(),
497507
pi.identity.into(),
498508
pi.name,
@@ -639,6 +649,7 @@ impl Room {
639649
let pi = pi.clone();
640650
inner.create_participant(
641651
pi.kind().into(),
652+
pi.state().into(),
642653
pi.sid.try_into().unwrap(),
643654
pi.identity.into(),
644655
pi.name,
@@ -976,14 +987,19 @@ impl RoomSession {
976987
// disconnected
977988
}
978989
} else if let Some(remote_participant) = remote_participant {
990+
let already_active = remote_participant.is_active();
979991
remote_participant.update_info(pi.clone());
992+
if !already_active && remote_participant.is_active() {
993+
self.dispatcher.dispatch(&RoomEvent::ParticipantActive(remote_participant.clone()));
994+
}
980995
participants.push(Participant::Remote(remote_participant));
981996
} else {
982997
// Create a new participant
983998
let remote_participant = {
984999
let pi = pi.clone();
9851000
self.create_participant(
9861001
pi.kind().into(),
1002+
pi.state().into(),
9871003
pi.sid.try_into().unwrap(),
9881004
pi.identity.into(),
9891005
pi.name,
@@ -1547,6 +1563,7 @@ impl RoomSession {
15471563
fn create_participant(
15481564
self: &Arc<Self>,
15491565
kind: ParticipantKind,
1566+
state: ParticipantState,
15501567
sid: ParticipantSid,
15511568
identity: ParticipantIdentity,
15521569
name: String,
@@ -1556,6 +1573,7 @@ impl RoomSession {
15561573
let participant = RemoteParticipant::new(
15571574
self.rtc_engine.clone(),
15581575
kind,
1576+
state,
15591577
sid.clone(),
15601578
identity.clone(),
15611579
name,

livekit/src/room/participant/local_participant.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ use std::{
2222
time::Duration,
2323
};
2424

25-
use super::{ConnectionQuality, ParticipantInner, ParticipantKind, ParticipantTrackPermission};
25+
use super::{
26+
ConnectionQuality, ParticipantInner, ParticipantKind, ParticipantState,
27+
ParticipantTrackPermission,
28+
};
2629
use crate::{
2730
data_stream::{
2831
ByteStreamInfo, ByteStreamWriter, StreamByteOptions, StreamResult, StreamTextOptions,
@@ -106,6 +109,7 @@ impl LocalParticipant {
106109
pub(crate) fn new(
107110
rtc_engine: Arc<RtcEngine>,
108111
kind: ParticipantKind,
112+
state: ParticipantState,
109113
sid: ParticipantSid,
110114
identity: ParticipantIdentity,
111115
name: String,
@@ -114,7 +118,7 @@ impl LocalParticipant {
114118
encryption_type: EncryptionType,
115119
) -> Self {
116120
Self {
117-
inner: super::new_inner(rtc_engine, sid, identity, name, metadata, attributes, kind),
121+
inner: super::new_inner(rtc_engine, sid, identity, name, metadata, attributes, kind, state),
118122
local: Arc::new(LocalInfo {
119123
events: LocalEvents::default(),
120124
encryption_type,
@@ -665,6 +669,10 @@ impl LocalParticipant {
665669
self.inner.info.read().name.clone()
666670
}
667671

672+
pub fn is_active(&self) -> bool {
673+
self.inner.info.read().state == ParticipantState::Active
674+
}
675+
668676
pub fn metadata(&self) -> String {
669677
self.inner.info.read().metadata.clone()
670678
}

livekit/src/room/participant/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ pub enum ParticipantKind {
4646
Agent,
4747
}
4848

49+
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
50+
pub(crate) enum ParticipantState {
51+
Joining,
52+
Joined,
53+
Active,
54+
Disconnected
55+
}
56+
4957
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
5058
pub enum DisconnectReason {
5159
UnknownReason,
@@ -78,6 +86,7 @@ impl Participant {
7886
pub fn sid(self: &Self) -> ParticipantSid;
7987
pub fn identity(self: &Self) -> ParticipantIdentity;
8088
pub fn name(self: &Self) -> String;
89+
pub fn is_active(self: &Self) -> bool;
8190
pub fn metadata(self: &Self) -> String;
8291
pub fn attributes(self: &Self) -> HashMap<String, String>;
8392
pub fn is_speaking(self: &Self) -> bool;
@@ -114,6 +123,7 @@ struct ParticipantInfo {
114123
pub audio_level: f32,
115124
pub connection_quality: ConnectionQuality,
116125
pub kind: ParticipantKind,
126+
pub state: ParticipantState,
117127
pub disconnect_reason: DisconnectReason,
118128
}
119129

@@ -154,6 +164,7 @@ pub(super) fn new_inner(
154164
metadata: String,
155165
attributes: HashMap<String, String>,
156166
kind: ParticipantKind,
167+
state: ParticipantState
157168
) -> Arc<ParticipantInner> {
158169
Arc::new(ParticipantInner {
159170
rtc_engine,
@@ -164,6 +175,7 @@ pub(super) fn new_inner(
164175
metadata,
165176
attributes,
166177
kind,
178+
state,
167179
speaking: false,
168180
audio_level: 0.0,
169181
connection_quality: ConnectionQuality::Excellent,
@@ -180,6 +192,7 @@ pub(super) fn update_info(
180192
new_info: proto::ParticipantInfo,
181193
) {
182194
let mut info = inner.info.write();
195+
info.state = new_info.state().into();
183196
info.disconnect_reason = new_info.disconnect_reason().into();
184197
info.kind = new_info.kind().into();
185198
info.sid = new_info.sid.try_into().unwrap();

livekit/src/room/participant/remote_participant.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use livekit_protocol as proto;
2424
use livekit_runtime::timeout;
2525
use parking_lot::Mutex;
2626

27-
use super::{ConnectionQuality, ParticipantInner, ParticipantKind, TrackKind};
27+
use super::{ConnectionQuality, ParticipantInner, ParticipantKind, ParticipantState, TrackKind};
2828
use crate::{prelude::*, rtc_engine::RtcEngine, track::TrackError};
2929

3030
const ADD_TRACK_TIMEOUT: Duration = Duration::from_secs(5);
@@ -71,6 +71,7 @@ impl RemoteParticipant {
7171
pub(crate) fn new(
7272
rtc_engine: Arc<RtcEngine>,
7373
kind: ParticipantKind,
74+
state: ParticipantState,
7475
sid: ParticipantSid,
7576
identity: ParticipantIdentity,
7677
name: String,
@@ -79,7 +80,9 @@ impl RemoteParticipant {
7980
auto_subscribe: bool,
8081
) -> Self {
8182
Self {
82-
inner: super::new_inner(rtc_engine, sid, identity, name, metadata, attributes, kind),
83+
inner: super::new_inner(
84+
rtc_engine, sid, identity, name, metadata, attributes, kind, state,
85+
),
8386
remote: Arc::new(RemoteInfo { events: Default::default(), auto_subscribe }),
8487
}
8588
}
@@ -450,6 +453,10 @@ impl RemoteParticipant {
450453
self.inner.info.read().name.clone()
451454
}
452455

456+
pub fn is_active(&self) -> bool {
457+
self.inner.info.read().state == ParticipantState::Active
458+
}
459+
453460
pub fn metadata(&self) -> String {
454461
self.inner.info.read().metadata.clone()
455462
}

0 commit comments

Comments
 (0)