Commit Graph

31 Commits

Author SHA1 Message Date
Kyle Simpson
47cf0b27eb Chore: clippy appeasement 2025-02-21 14:10:57 +00:00
Kyle Simpson
17993bc0d0 Fix: pass mid-connection WS events to task (#269)
Discord will send `GatewayEvent::Speaking` (opcode 5) messages
after the Hello+Ready exchange, but will happily interleave
them with crypto mode negotiation. We were previously not expecting
such messages and dropping them -- this hurts receive-based bots'
ability to map SSRCs to UserIds when joining a call with existing
users.

This PR feeds all unexpected messages into the WS task directly,
which will handle them once all tasks are fully started.
2024-11-26 00:51:52 +00:00
Cycle Five
d681b71b1f Input: Add ytdl search (#210)
* Add ytdl search

* fix fmt

* Remove compose, add tests, return AuxMetadata

* fix parsing of AuxMetadata and better test

* Fix playability of `YoutubeDl::new_search`

Refactors such that parsing of (ND)JSON is handled in only one location
now, which allows us to greatly simplify the actual `search` method. The
main change is that any `new_search` is now instantly playable.

---------

Co-authored-by: Kyle Simpson <kyleandrew.simpson@gmail.com>
2023-12-12 08:28:13 +00:00
Kyle Simpson
22ceb175b6 Chore: Fixup examples, bump version pre-push
Takes the chance to bring shared deps into the example workspace while we're here. Release notes, tags etc will follow in time.
2023-11-27 21:16:53 +00:00
Kyle Simpson
1487da175c Chore: Upgrade serenity to 0.12.0-rc 2023-11-20 00:03:53 +00:00
Gnome!
509743fd40 Fix compiling with latest serenity (#199) 2023-11-20 00:02:58 +00:00
Kyle Simpson
3daf11f5d1 Driver: Implement audio scheduler (#179)
This PR implements a custom scheduler for audio threads, which reduces thread use and (often) memory consumption.

To save threads and memory (e.g., packet buffer allocations), Songbird parks Mixer tasks which do not have any live Tracks.
These are now all co-located on a single async 'Idle' task.
This task is responsible for managing UDP keepalive messages for each task, maintaining event state, and executing any Mixer task messages.
Whenever any message arrives which adds a `Track`, the mixer task is moved to a live thread.
The Idle task inspects task counts and execution time on each thread, choosing the first live thread with room, and creating a new one if needed.

Each live thread is responsible for running as many live mixers as it can in a single tick every 20ms: this currently defaults to 16 mixers per thread, but is user-configurable.
A live thread also stores RTP packet blocks to be written into by each sub-task.
Each live thread has a conservative limit of 18ms that it will aim to stay under: if all work takes longer than this, it will offload the task with the highest mixing cost once per tick onto another (possibly new) live worker thread.
2023-11-20 00:02:57 +00:00
Kyle Simpson
fdd0d830c7 Deps: Move to published symphonia v0.5.2 from git
Symphonia v0.5.2 has all the API changes and fixes we were depending on, which leaves next one step closer to publishable.

Tested using `cargo make ready`.
2023-11-20 00:02:56 +00:00
Kyle Simpson
f2fbbfeb25 Gateway: Simplify return value of join/join_gateway (#157)
Replaces the annoying dual-return (i.e., created `Call` *and* `Result<x>`) with a single `Return<Call/ConnectionInfo>`. Users are now informed via that a `Call` is created -- thus, cleanup in event of connection failure is now their responsibility.

Tested using `cargo make ready`.

Closes #65.
2023-11-20 00:02:55 +00:00
Kyle Simpson
c60c454cf5 Driver/receive: Implement audio reorder/jitter buffer (#156)
This PR Introduces a new `VoiceTick` event which collects and reorders all RTP packets to smooth over network instability, as well as to synchronise user audio streams. Raw packet events have been moved to `RtpPacket`, while `SpeakingUpdate`s have been removed as they can be easily computed using the `silent`/`speaking` audio maps included in each event.

Closes #146.
2023-11-20 00:02:55 +00:00
Kyle Simpson
b7e40ab5e4 Deps: Move symphonia back to mainline repo.
Temporary measure until symphonia 0.5.2 is released. Should allow us to merge #143 now that all our API additions and fixes are in.
2023-11-20 00:02:55 +00:00
Kyle Simpson
646190eaf8 Deps: Update Twilight -> v0.14 2023-11-20 00:02:55 +00:00
Kyle Simpson
2277595be4 Driver: Split receive into its own feature (#141)
Adds the "receive" feature, which is disabled by default. When this is disabled, the UDP receive task is not compiled and not run, and as an optimisation the UDP receive buffer size is set to 0. All related events are also removed.

This also removes the UDP Tx task, and moves packet and keepalive sends back into the mixer thread. This allows us to entirely remove channels and various allocations between the mixer and an async task created only for sending data (i.e., fewer memcopies).

If "receive" is enabled, UDP sends are now non-blocking due to technical constraints -- failure to send is non-fatal, but *will* drop affected packets. Given that blocking on a UDP send indicates that the OS cannot clear send buffers fast enough, this should alleviate OS load.

Closes #131.
2023-11-20 00:02:54 +00:00
Kyle Simpson
8cc7a22b0b Driver/Input: Migrate audio backend to Symphonia (#89)
This extensive PR rewrites the internal mixing logic of the driver to use symphonia for parsing and decoding audio data, and rubato to resample audio. Existing logic to decode DCA and Opus formats/data have been reworked as plugins for symphonia. The main benefit is that we no longer need to keep yt-dlp and ffmpeg processes alive, saving a lot of memory and CPU: all decoding can be done in Rust! In exchange, we now need to do a lot of the HTTP handling and resumption ourselves, but this is still a huge net positive.

`Input`s have been completely reworked such that all default (non-cached) sources are lazy by default, and are no longer covered by a special-case `Restartable`. These now span a gamut from a `Compose` (lazy), to a live source, to a fully `Parsed` source. As mixing is still sync, this includes adapters for `AsyncRead`/`AsyncSeek`, and HTTP streams.

`Track`s have been reworked so that they only contain initialisation state for each track. `TrackHandles` are only created once a `Track`/`Input` has been handed over to the driver, replacing `create_player` and related functions. `TrackHandle::action` now acts on a `View` of (im)mutable state, and can request seeks/readying via `Action`.

Per-track event handling has also been improved -- we can now determine and propagate the reason behind individual track errors due to the new backend. Some `TrackHandle` commands (seek etc.) benefit from this, and now use internal callbacks to signal completion.

Due to associated PRs on felixmcfelix/songbird from avid testers, this includes general clippy tweaks, API additions, and other repo-wide cleanup. Thanks go out to the below co-authors.

Co-authored-by: Gnome! <45660393+GnomedDev@users.noreply.github.com>
Co-authored-by: Alakh <36898190+alakhpc@users.noreply.github.com>
2023-11-19 23:58:34 +00:00
Kyle Simpson
a85a1f08e1 Chore: Update to serenity 0.11 2022-07-22 15:41:18 +01:00
Kyle Simpson
d3a40fe691 Examples: support new Serenity Intents init
Fixes up the serenity examples to account for a bunch of API changes on `next`/v0.11.

Tested using `cargo make ready`.
2022-07-22 15:41:18 +01:00
Kyle Simpson
f1ed41ea28 Examples: Fix serenity-next cache accesses (#99)
Serenity's cache design has changed, this (finally) prevents the examples from failing to compile on CI.
2022-07-22 15:41:18 +01:00
Kyle Simpson
652ec1f293 Docs: fix ClientConnect to recommend SpeakingStateUpdate 2022-02-13 18:53:33 +00:00
Kyle Simpson
c464fcc38d Events: Deprecate ClientConnect (#112)
Discord no longer send these websocket payloads, users should instead rely on the main part of their bot for determining actual connection events, or `SpeakingUpdate`s for SSRC mapping.

Closes #104.
2022-02-13 12:06:50 +00:00
Luukas Pörtfors
62ecfe68d6 Examples: Fix unmatched quotation mark in comment. (#101) 2021-10-18 10:59:57 +01:00
Kyle Simpson
dad48ca835 Driver: Fix incorrect leave behaviour in Drop handler
Sending poison messages should suffice to kill the voice session: attempting to `.leave()`. Fixes #88.

This was tested using `cargo make ready` and the modified `serenity/voice/` example.
2021-08-17 12:14:50 +01:00
Kyle Simpson
27f26ade99 Events: Break out and non-exhaust context body structs (#54)
This PR makes many of the types under `EventContext` separate `#[non_exhaustive]` structs. This makes it more feasible to add further information to connection and packet events as required in future. On this note, driver (re)connection events now include the SSRC supplied by Discord and the domain name which was connected to.

In addition, this fixes global timed events to return a list of all live tracks, and extensively details/documents events at a high level.

This was tested using `cargo make ready`.
2021-07-01 11:30:01 +01:00
Kyle Simpson
1bfee1b989 Driver: Move Bitrate import out of crate root. (#53)
This is a simple organisational change which moves `crate::Bitrate` to `crate::driver::Bitrate` to slightly clean up the crate root.

This has been tested using `cargo make ready`.
2021-07-01 11:30:01 +01:00
Kyle Simpson
1fc3dc2259 Gateway: Add connection timeout, add Config to gateway. (#51)
This change fixes tasks hanging due to rare cases of messages being lost between full Discord reconnections by placing a configurable timeout on the `ConnectionInfo` responses. This is a companion fix to [serenity#1255](https://github.com/serenity-rs/serenity/pull/1255). To make this doable, `Config`s are now used by all versions of `Songbird`/`Call`, and relevant functions are  added to simplify setup with configuration. These are now non-exhaustive, correcting an earlier oversight. For future extensibility, this PR moves the return type of `join`/`join_gateway` into a custom future (no longer leaking flume's `RecvFut` type).

Additionally, this fixes the Makefile's feature sets for driver/gateway-only compilation.

This is a breaking change in:
* the return types of `join`/`join_gateway`
* moving `crate::driver::Config` -> `crate::Config`,
* `Config` and `JoinError` becoming `#[non_breaking]`.

This was tested via `cargo make ready`, and by testing `examples/serenity/voice_receive` with various timeout settings.
2021-07-01 11:30:01 +01:00
Kyle Simpson
b9a926c125 Break reference cycle in voice storage example (#44)
Changes a stored `Arc` pointer to the call (used to queue up further tracks in response to events) into a `Weak`, as the event handler's strong pointer would keep the Call and Driver objects alive. This would have caused an unintentional resource leak of threads/tasks. This was found by some internal profiling in search of #42.
2021-03-11 22:58:30 +00:00
Kyle Simpson
12776fc6f8 Release v0.1.0 (#34)
* Update dependencies, draft initial structure for changelog.

* Full changelog!
2021-01-08 11:42:07 +00:00
Kyle Simpson
53ab9dac03 Chore: Bump to published serenity. 2021-01-06 23:12:24 +00:00
Kyle Simpson
f05b7414a0 Songbird: Tokio 1.0 (#36)
Migrates to the new version of tokio, requiring channel and sleep changes in a few locations. Additionally points to the in-tree v0.3 version of twilight.
2021-01-06 13:01:14 +00:00
Kyle Simpson
03ae0e7628 Input: Allow Restartable sources to be lazy
This change is made with queue users in mind. Since sources
of this kind *know* how to (re)create themselves, they can
avoid being created at all until needed.

This also adds machinery to preload tracks *before* they are
needed, for gapless playback on queues and so on. Queues
make use of the event system to do this.
2020-12-28 17:17:57 +00:00
Kyle Simpson
de652250d8 TrackQueues: Convenience methods and extension (#7)
* Adds a uuid field to tracks and handles to make it easier to identify and match event sources after the fact.
* Adds optional feature "builtin-queue" to expose a queue on every driver, as a convenience for users who can guarantee they'll need a queue for every driver/call.
* Adds methods to queues to allow access to the currently running track handle, remove a specified queue entry, as well as to mutate the underlying queue from a closure.
2020-11-16 08:57:54 +00:00
Alex M. M
f5bf54a63d Move examples from the Serenity repository 2020-11-13 16:20:57 +01:00