From 28e64ef4ce8e8acfc5717b42e2fe17e83aee0a80 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Tue, 3 Dec 2024 23:51:11 -0800 Subject: [PATCH 1/4] Support mute/unmute for local tracks --- livekit-rtc/livekit/rtc/track.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/livekit-rtc/livekit/rtc/track.py b/livekit-rtc/livekit/rtc/track.py index de870481..6231da9e 100644 --- a/livekit-rtc/livekit/rtc/track.py +++ b/livekit-rtc/livekit/rtc/track.py @@ -80,6 +80,18 @@ def create_audio_track(name: str, source: "AudioSource") -> "LocalAudioTrack": resp = FfiClient.instance.request(req) return LocalAudioTrack(resp.create_audio_track.track) + def mute(self): + req = proto_ffi.FfiRequest() + req.local_track_mute.track_handle = self._ffi_handle.handle + req.local_track_mute.mute = True + FfiClient.instance.request(req) + + def unmute(self): + req = proto_ffi.FfiRequest() + req.local_track_mute.track_handle = self._ffi_handle.handle + req.local_track_mute.mute = False + FfiClient.instance.request(req) + def __repr__(self) -> str: return f"rtc.LocalAudioTrack(sid={self.sid}, name={self.name})" @@ -97,6 +109,18 @@ def create_video_track(name: str, source: "VideoSource") -> "LocalVideoTrack": resp = FfiClient.instance.request(req) return LocalVideoTrack(resp.create_video_track.track) + def mute(self): + req = proto_ffi.FfiRequest() + req.local_track_mute.track_handle = self._ffi_handle.handle + req.local_track_mute.mute = True + FfiClient.instance.request(req) + + def unmute(self): + req = proto_ffi.FfiRequest() + req.local_track_mute.track_handle = self._ffi_handle.handle + req.local_track_mute.mute = False + FfiClient.instance.request(req) + def __repr__(self) -> str: return f"rtc.LocalVideoTrack(sid={self.sid}, name={self.name})" From 92dda3b58337646b3caf059413f73c4c90396a81 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Tue, 3 Dec 2024 23:52:35 -0800 Subject: [PATCH 2/4] fix formatting --- examples/room_example.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/examples/room_example.py b/examples/room_example.py index b13cfb07..67db8444 100644 --- a/examples/room_example.py +++ b/examples/room_example.py @@ -7,6 +7,7 @@ TOKEN = os.environ.get("LIVEKIT_TOKEN") URL = os.environ.get("LIVEKIT_URL") + async def main(): logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) @@ -14,8 +15,9 @@ async def main(): @room.on("participant_connected") def on_participant_connected(participant: rtc.RemoteParticipant): - logging.info( - "participant connected: %s %s", participant.sid, participant.identity) + logger.info( + "participant connected: %s %s", participant.sid, participant.identity + ) async def receive_frames(stream: rtc.VideoStream): async for frame in stream: @@ -24,8 +26,12 @@ async def receive_frames(stream: rtc.VideoStream): # track_subscribed is emitted whenever the local participant is subscribed to a new track @room.on("track_subscribed") - def on_track_subscribed(track: rtc.Track, publication: rtc.RemoteTrackPublication, participant: rtc.RemoteParticipant): - logging.info("track subscribed: %s", publication.sid) + def on_track_subscribed( + track: rtc.Track, + publication: rtc.RemoteTrackPublication, + participant: rtc.RemoteParticipant, + ): + logger.info("track subscribed: %s", publication.sid) if track.kind == rtc.TrackKind.KIND_VIDEO: video_stream = rtc.VideoStream(track) asyncio.ensure_future(receive_frames(video_stream)) @@ -33,7 +39,7 @@ def on_track_subscribed(track: rtc.Track, publication: rtc.RemoteTrackPublicatio # By default, autosubscribe is enabled. The participant will be subscribed to # all published tracks in the room await room.connect(URL, TOKEN) - logging.info("connected to room %s", room.name) + logger.info("connected to room %s", room.name) for identity, participant in room.remote_participants.items(): print(f"identity: {identity}") @@ -43,7 +49,8 @@ def on_track_subscribed(track: rtc.Track, publication: rtc.RemoteTrackPublicatio print(f"participant identity: {participant.identity}") print(f"participant name: {participant.name}") print(f"participant kind: {participant.kind}") - print(f"participant track publications: {participant.track_publications}") + print(f"participant track publications: { + participant.track_publications}") for tid, publication in participant.track_publications.items(): print(f"\ttrack id: {tid}") print(f"\t\ttrack publication: {publication}") @@ -52,7 +59,6 @@ def on_track_subscribed(track: rtc.Track, publication: rtc.RemoteTrackPublicatio print(f"\t\ttrack source: {publication.source}") print(f"participant metadata: {participant.metadata}") - if __name__ == "__main__": @@ -62,4 +68,3 @@ def on_track_subscribed(track: rtc.Track, publication: rtc.RemoteTrackPublicatio exit(1) asyncio.run(main()) - From 87a8d296bd1500d7d2fe1de5488210b15954a698 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Wed, 4 Dec 2024 00:31:51 -0800 Subject: [PATCH 3/4] set immediately to muted --- examples/room_example.py | 6 ++++-- livekit-rtc/livekit/rtc/track.py | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/room_example.py b/examples/room_example.py index 67db8444..f71ca963 100644 --- a/examples/room_example.py +++ b/examples/room_example.py @@ -49,8 +49,10 @@ def on_track_subscribed( print(f"participant identity: {participant.identity}") print(f"participant name: {participant.name}") print(f"participant kind: {participant.kind}") - print(f"participant track publications: { - participant.track_publications}") + print( + f"participant track publications: { + participant.track_publications}" + ) for tid, publication in participant.track_publications.items(): print(f"\ttrack id: {tid}") print(f"\t\ttrack publication: {publication}") diff --git a/livekit-rtc/livekit/rtc/track.py b/livekit-rtc/livekit/rtc/track.py index 6231da9e..6c24607c 100644 --- a/livekit-rtc/livekit/rtc/track.py +++ b/livekit-rtc/livekit/rtc/track.py @@ -114,12 +114,14 @@ def mute(self): req.local_track_mute.track_handle = self._ffi_handle.handle req.local_track_mute.mute = True FfiClient.instance.request(req) + self._info.muted = True def unmute(self): req = proto_ffi.FfiRequest() req.local_track_mute.track_handle = self._ffi_handle.handle req.local_track_mute.mute = False FfiClient.instance.request(req) + self._info.muted = False def __repr__(self) -> str: return f"rtc.LocalVideoTrack(sid={self.sid}, name={self.name})" From 4d5ed9c9c3450b5aa8c4d2de05a2a12918fbdd39 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Wed, 4 Dec 2024 00:35:12 -0800 Subject: [PATCH 4/4] missed a spot --- livekit-rtc/livekit/rtc/track.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/livekit-rtc/livekit/rtc/track.py b/livekit-rtc/livekit/rtc/track.py index 6c24607c..8a6fe692 100644 --- a/livekit-rtc/livekit/rtc/track.py +++ b/livekit-rtc/livekit/rtc/track.py @@ -85,12 +85,14 @@ def mute(self): req.local_track_mute.track_handle = self._ffi_handle.handle req.local_track_mute.mute = True FfiClient.instance.request(req) + self._info.muted = True def unmute(self): req = proto_ffi.FfiRequest() req.local_track_mute.track_handle = self._ffi_handle.handle req.local_track_mute.mute = False FfiClient.instance.request(req) + self._info.muted = False def __repr__(self) -> str: return f"rtc.LocalAudioTrack(sid={self.sid}, name={self.name})"