-
Notifications
You must be signed in to change notification settings - Fork 105
Media Devices #493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chenosaurus
wants to merge
35
commits into
main
Choose a base branch
from
dc/media_devices
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Media Devices #493
Changes from 14 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
e00dcc8
init media devices
chenosaurus 8f13bbd
add MediaDevices to rtc/__init__.py
chenosaurus cd9d873
clean up examples
chenosaurus b58dd7d
fix syntax to create inputstream
chenosaurus 825e9d5
fix audio output thru mixer
chenosaurus 74582ec
remove unused import
chenosaurus 9b2f466
fix linter error
chenosaurus efb5473
ruff format
chenosaurus 7f1d59e
allow AudioMixer to unwrap AudioFrameEvent
chenosaurus c8f8c0c
rename dir to match convention
chenosaurus 30ee183
rename methods to be more clear
chenosaurus 89fb1ba
update example
chenosaurus c48e1eb
update comments
chenosaurus 72f546f
ruff format
chenosaurus ef56542
clean up input stream creation
chenosaurus 236fad1
add missing dep
chenosaurus 7cc6efb
remove mapping
chenosaurus 1ba7f9f
make apm internal
chenosaurus 7e0df4f
add db meter
chenosaurus 8458783
fix lint issues
chenosaurus ca27e5f
display room name
chenosaurus 846538f
move audio mixer inside of MediaDevices for ease of playback
chenosaurus 58483ac
remove unused import
chenosaurus c8ca2eb
adding to readme for MediaDevices usage
chenosaurus 15d104e
format
chenosaurus 4a85e01
Merge branch 'main' into dc/media_devices
chenosaurus 7a3e04b
revert changes to audio mixer as we no longer need it to handle Audio…
chenosaurus 723627c
format
chenosaurus 78468ea
fix comment
chenosaurus 2b32567
fix media devices lint
chenosaurus 1abe8c5
clean up media devices
chenosaurus 8e9dc24
format
chenosaurus e0e99ae
add example script to list audio devices
chenosaurus a831e67
media devices should import sounddevice lazily
chenosaurus 29f79f6
format
chenosaurus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import os | ||
| import asyncio | ||
| import logging | ||
| from dotenv import load_dotenv, find_dotenv | ||
|
|
||
| from livekit import api, rtc | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| logging.basicConfig(level=logging.INFO) | ||
|
|
||
| # Load environment variables from a .env file if present | ||
| load_dotenv(find_dotenv()) | ||
|
|
||
| url = os.getenv("LIVEKIT_URL") | ||
| api_key = os.getenv("LIVEKIT_API_KEY") | ||
| api_secret = os.getenv("LIVEKIT_API_SECRET") | ||
| if not url or not api_key or not api_secret: | ||
| raise RuntimeError("LIVEKIT_URL and LIVEKIT_TOKEN must be set in env") | ||
|
|
||
| room = rtc.Room() | ||
|
|
||
| devices = rtc.MediaDevices() | ||
|
|
||
| # Open microphone with AEC and prepare a player for remote audio feeding AEC reverse stream | ||
| mic = devices.open_input(enable_aec=True) | ||
| player = devices.open_output(apm_for_reverse=mic.apm) | ||
|
|
||
| # Mixer for all remote audio streams | ||
| mixer = rtc.AudioMixer(sample_rate=48000, num_channels=1) | ||
|
|
||
| # Track stream bookkeeping for cleanup | ||
| streams_by_pub: dict[str, rtc.AudioStream] = {} | ||
| streams_by_participant: dict[str, set[rtc.AudioStream]] = {} | ||
|
|
||
| async def _remove_stream( | ||
| stream: rtc.AudioStream, participant_sid: str | None = None, pub_sid: str | None = None | ||
| ) -> None: | ||
| try: | ||
| mixer.remove_stream(stream) | ||
| except Exception: | ||
| pass | ||
| try: | ||
| await stream.aclose() | ||
| except Exception: | ||
| pass | ||
| if participant_sid and participant_sid in streams_by_participant: | ||
| streams_by_participant.get(participant_sid, set()).discard(stream) | ||
| if not streams_by_participant.get(participant_sid): | ||
| streams_by_participant.pop(participant_sid, None) | ||
| if pub_sid is not None: | ||
| streams_by_pub.pop(pub_sid, None) | ||
|
|
||
| def on_track_subscribed( | ||
| track: rtc.Track, | ||
| publication: rtc.RemoteTrackPublication, | ||
| participant: rtc.RemoteParticipant, | ||
| ): | ||
| if track.kind == rtc.TrackKind.KIND_AUDIO: | ||
| stream = rtc.AudioStream(track, sample_rate=48000, num_channels=1) | ||
| streams_by_pub[publication.sid] = stream | ||
| streams_by_participant.setdefault(participant.sid, set()).add(stream) | ||
| mixer.add_stream(stream) | ||
| logging.info("subscribed to audio from %s", participant.identity) | ||
|
|
||
| room.on("track_subscribed", on_track_subscribed) | ||
|
|
||
| def on_track_unsubscribed( | ||
| track: rtc.Track, | ||
| publication: rtc.RemoteTrackPublication, | ||
| participant: rtc.RemoteParticipant, | ||
| ): | ||
| stream = streams_by_pub.get(publication.sid) | ||
| if stream is not None: | ||
| asyncio.create_task(_remove_stream(stream, participant.sid, publication.sid)) | ||
| logging.info("unsubscribed from audio of %s", participant.identity) | ||
|
|
||
| room.on("track_unsubscribed", on_track_unsubscribed) | ||
|
|
||
| def on_track_unpublished( | ||
| publication: rtc.RemoteTrackPublication, participant: rtc.RemoteParticipant | ||
| ): | ||
| stream = streams_by_pub.get(publication.sid) | ||
| if stream is not None: | ||
| asyncio.create_task(_remove_stream(stream, participant.sid, publication.sid)) | ||
| logging.info("track unpublished: %s from %s", publication.sid, participant.identity) | ||
|
|
||
| room.on("track_unpublished", on_track_unpublished) | ||
|
|
||
| def on_participant_disconnected(participant: rtc.RemoteParticipant): | ||
| streams = list(streams_by_participant.pop(participant.sid, set())) | ||
| for stream in streams: | ||
| # Best-effort discover publication sid | ||
| pub_sid = None | ||
| for k, v in list(streams_by_pub.items()): | ||
| if v is stream: | ||
| pub_sid = k | ||
| break | ||
| asyncio.create_task(_remove_stream(stream, participant.sid, pub_sid)) | ||
| logging.info("participant disconnected: %s", participant.identity) | ||
|
|
||
| room.on("participant_disconnected", on_participant_disconnected) | ||
|
|
||
| token = ( | ||
| api.AccessToken(api_key, api_secret) | ||
| .with_identity("local-audio") | ||
| .with_name("Local Audio") | ||
| .with_grants( | ||
| api.VideoGrants( | ||
| room_join=True, | ||
| room="local-audio", | ||
| ) | ||
| ) | ||
| .to_jwt() | ||
| ) | ||
|
|
||
| try: | ||
| await room.connect(url, token) | ||
| logging.info("connected to room %s", room.name) | ||
|
|
||
| # Publish microphone | ||
| track = rtc.LocalAudioTrack.create_audio_track("mic", mic.source) | ||
| pub_opts = rtc.TrackPublishOptions() | ||
| pub_opts.source = rtc.TrackSource.SOURCE_MICROPHONE | ||
| await room.local_participant.publish_track(track, pub_opts) | ||
| logging.info("published local microphone") | ||
|
|
||
| # Start playing mixed remote audio | ||
| asyncio.create_task(player.play(mixer)) | ||
|
|
||
| # Run until Ctrl+C | ||
| while True: | ||
| await asyncio.sleep(1) | ||
| except KeyboardInterrupt: | ||
| pass | ||
| finally: | ||
| await mic.aclose() | ||
| await mixer.aclose() | ||
| await player.aclose() | ||
| try: | ||
| await room.disconnect() | ||
| except Exception: | ||
| pass | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import os | ||
| import asyncio | ||
| import logging | ||
| from dotenv import load_dotenv, find_dotenv | ||
|
|
||
| from livekit import api, rtc | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| logging.basicConfig(level=logging.INFO) | ||
|
|
||
| # Load environment variables from a .env file if present | ||
| load_dotenv(find_dotenv()) | ||
|
|
||
| url = os.getenv("LIVEKIT_URL") | ||
| api_key = os.getenv("LIVEKIT_API_KEY") | ||
| api_secret = os.getenv("LIVEKIT_API_SECRET") | ||
| if not url or not api_key or not api_secret: | ||
| raise RuntimeError( | ||
| "LIVEKIT_URL and LIVEKIT_API_KEY and LIVEKIT_API_SECRET must be set in env" | ||
| ) | ||
|
|
||
| room = rtc.Room() | ||
|
|
||
| # Create media devices helper and open default microphone with AEC enabled | ||
| devices = rtc.MediaDevices() | ||
| mic = devices.open_input(enable_aec=True) | ||
|
|
||
| token = ( | ||
| api.AccessToken(api_key, api_secret) | ||
| .with_identity("local-audio") | ||
| .with_name("Local Audio") | ||
| .with_grants( | ||
| api.VideoGrants( | ||
| room_join=True, | ||
| room="local-audio", | ||
| ) | ||
| ) | ||
| .to_jwt() | ||
| ) | ||
|
|
||
| try: | ||
| await room.connect(url, token) | ||
| logging.info("connected to room %s", room.name) | ||
|
|
||
| track = rtc.LocalAudioTrack.create_audio_track("mic", mic.source) | ||
| pub_opts = rtc.TrackPublishOptions() | ||
| pub_opts.source = rtc.TrackSource.SOURCE_MICROPHONE | ||
| await room.local_participant.publish_track(track, pub_opts) | ||
| logging.info("published local microphone") | ||
|
|
||
| # Run until Ctrl+C | ||
| while True: | ||
| await asyncio.sleep(1) | ||
| except KeyboardInterrupt: | ||
| pass | ||
| finally: | ||
| await mic.aclose() | ||
| try: | ||
| await room.disconnect() | ||
| except Exception: | ||
| pass | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -184,6 +184,10 @@ async def _get_contribution( | |
| except StopAsyncIteration: | ||
| exhausted = True | ||
| break | ||
| # AudioStream may yield either AudioFrame or AudioFrameEvent; unwrap if needed | ||
|
||
| if hasattr(frame, "frame"): | ||
| frame = frame.frame # type: ignore[assignment] | ||
|
|
||
| new_data = np.frombuffer(frame.data.tobytes(), dtype=np.int16).reshape( | ||
| -1, self._num_channels | ||
| ) | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should abstract further down, and directly allow to add a AudioTrack
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you mean we should add a
add_trackandremove_trackmethod to the AudioMixer class?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But maybe more like player.add_track
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm yea that would make sense to add to media_devices to avoid users having to deal w/ a mixer to just play tracks back. Lemme add this.