Commit Graph

69 Commits

Author SHA1 Message Date
Stefan Ric 82c55dffac voice/udp: Reflect upstream changes from Discord 2023-03-20 14:05:21 -07:00
diamondburned cd1a044bc8
voice: Fix voicegateway.SpeakingFlag being invalid
Thanks, @topisenpai!
2022-12-07 00:25:55 -08:00
Jason Chu 4db60bca9d
voice: Close previous UDP sessions on Continue (#358) 2022-12-03 19:39:43 -08:00
Tyler Stuyfzand 769215970e
session: Introduce SendGateway (#356)
* Support SendGateway to help with voice usage

* Remove unnecessary Gateway interface function

* Add clarification comment

Co-authored-by: Tyler <admin@meow.tf>
2022-11-25 11:16:13 -08:00
diamondburned c71f48c163
Revert "ws: Add LastAcknowledgedBeat"
This reverts commit 660d9893e1.

The commit introduces regular incorrect reconnections. It will be
tracked in another branch and re-merged later once fixed.
2022-04-12 10:55:42 -07:00
diamondburned 660d9893e1
ws: Add LastAcknowledgedBeat
This commit adds ws.Handler.LastAcknowledgedBeat to allow ws.Gateway to
monitor whether or not the server is still reachable.

It fixes #324.
2022-04-12 10:38:41 -07:00
diamondburned ae24217e34
voice: Allow setting udp.DialFunc
This commit fixes up the SetDialer method to accept a udp.DialFunc
instead of just a regular *net.Dialer, which is more flexible in that
the user can now control the UDP packet frequency properly.
2022-04-03 17:48:15 -07:00
diamondburned 6b835a58c7
*: Increase test timeout duration
See if CI passes this time.
2022-04-02 22:44:08 -07:00
diamondburned 40ff267a74
voice: Example should use JoinChannelAndSpeak 2022-01-18 21:36:58 -08:00
diamondburned c8f72547f7 voice: Refactor and fix up
This commit refactors a lot of voice's internals to be more stable and
handle more edge cases from Discord's voice servers. It should result in
an overall more stable voice connection.

A few helper functions have been added into voice.Session. Some fields
will have been broken and changed to accomodate for the refactor, as
well.

Below are some commits that have been squashed in:

    voice: Fix Speaking() panic on closed
    voice: StopSpeaking should not error out
        The rationale is added as a comment into the Speaking() method.
    voice: Add TestKickedOut
    voice: Fix region change disconnecting
2022-01-18 21:35:55 -08:00
diamondburned 4fa2363f8a
Revert "voice: Example should use JoinChannelAndSpeak"
This reverts commit 85619b2976. It was
accidentally merged into v3 instead of 238-branch.
2022-01-18 21:28:23 -08:00
diamondburned 85619b2976
voice: Example should use JoinChannelAndSpeak 2022-01-18 18:26:29 -08:00
diamondburned 54cadd2f45 gateway: Refactor for a better concurrent API
This commit refactors the whole package gateway as well as utils/ws
(formerly utils/wsutil) and voice/voicegateway. The new refactor
utilizes a design pattern involving a concurrent loop and an arriving
event channel.

An additional change was made to the way gateway events are typed.
Before, pretty much any type will satisfy a gateway event type, since
the actual type was just interface{}. The new refactor defines a
concrete interface that events can implement:

    type Event interface {
        Op() OpCode
        EventType() EventType
    }

Using this interface, the user can easily add custom gateway events
independently of the library without relying on string maps. This adds a
lot of type safety into the library and makes type-switching on Event
types much more reasonable.

Gateway error callbacks are also almost entirely removed in favor of
custom gateway events. A catch-all can easily be added like this:

    s.AddHandler(func(err error) {
        log.Println("gateway error:, err")
    })
2021-12-14 13:49:34 -08:00
diamondburned 123f8bc41f
voice: Fix init inconsistencies
This commit fixes a few subtle bugs in the voice package. It slightly
refactors the connecting and reconnecting of voice sessions.
2021-11-04 14:21:46 -07:00
diamondburned efde3f4ea6
state, handler: Refactor state storage and sync handlers
This commit refactors a lot of packages.

It refactors the handler package, removing the Synchronous field and
replacing it the AddSyncHandler API, which allows each handler to
control whether or not it should be ran synchronously independent of
other handlers. This is useful for libraries that need to guarantee the
incoming order of events.

It also refactors the store interfaces to accept more interfaces. This
is to make the API more consistent as well as reducing potential useless
copies. The public-facing state API should still be the same, so this
change will mostly concern users with their own store implementations.

Several miscellaneous functions (such as a few in package gateway) were
modified to be more suitable to other packages, but those functions
should rarely ever be used, anyway.

Several tests are also fixed within this commit, namely fixing state's
intents bug.
2021-11-03 15:16:02 -07:00
diamondburned af35c03b8e voice: Fix race condition, see #237 2021-06-30 00:19:30 -07:00
diamondburned 5b328bdab0 shard: Remake shard manager (#226) 2021-06-14 15:22:55 -07:00
Samuel Hernandez 40e1a3757d *: Migrated Go Modules to v3 2021-06-01 19:59:01 -07:00
Maximilian von Lindern df5954d2e1
all: Revert incorrect "GuildID" changes (#211) 2021-05-30 14:16:16 -07:00
Maximilian von Lindern d2b70ebfa7 all: Remove deprecated symbols 2021-05-29 22:17:44 -07:00
diamondburned 3b5a4ed94d Voice: Add packet correction for RTCP per RFC3350 2021-02-22 12:33:12 -08:00
diamondburned c6d8c741e8 CI: Add perseverance test for Gateway 2020-12-31 23:48:29 -08:00
diamondburned f1f052180b Heart: Improve thread-safety and pace responsiveness
This commit consists of these smaller commits:

    Gateway: SessionID to be a method for thread safety

	This commit breaks the SessionID field of the Gateway struct to
	be thread-safe by wrapping its access with a read-write mutex.
	As this is a bug fix, it is reasonable of a breaking change

    Heart: Allow later binding of event channel

    Voice: Use the new Heart API

    Heart: Fixed data races

    Heart: Allow changing pace, thread-safe Heartbeat
2020-12-31 01:32:45 -08:00
diamondburned b8994ed0da Voice: Remove state-keeping of sessions
This commit gets rid of all the code that previously managed different
voice sessions in different guilds. This is because there is rarely ever
a need for this, and most bots that need this could do their own
keeping.

This change, although removes some features off of the package, adds a
lot of clarity on what to do exactly when it comes to connecting to a
voice channel.

In order to make the migration process a bit easier, an example has been
added which guides through using the voice.Session API.
2020-11-30 19:12:20 -08:00
diamondburned 6727f0e728 Voice: Fixed voice tests for new JoinChannel 2020-11-30 17:04:11 -08:00
diamondburned 4df72dc891 Voice: JoinChannel no longer takes in an extra GuildID 2020-11-30 16:58:28 -08:00
diamondburned 1463d6e675 Voice: Rename NewVoice... to New 2020-11-30 16:46:43 -08:00
diamondburned 0d427cab71 Voice: Allow New without intents 2020-11-30 16:42:26 -08:00
Tyler Stuyfzand 75d6be7a9d
Voice: Add receive capability (#174)
* Resolve issue with copied v1 struct

* Speaking event patches, support Client Connect/Disconnect events

* Remove extra debug in heart.go

* Initial voice packet reading

* Resolve unallocated slices, use a static slice/array for decryption, split version/type

* Use separate slice for recvOpus, check return of secretbox.Open, and use constant for header size

* Update missing reference to packetHeaderSize

* Resolve decryption issues, add ReadPacket to session

* Update documentation for recvBuf/recvOpus

* Update comment for recvPacket's array
2020-11-29 17:32:18 -08:00
diamondburned 8356a8a3f6 Voice: Omit deprecated function in tests 2020-11-18 16:18:15 -08:00
diamondburned 6bdac16c2a Voice: Allow custom frame parameters; removed StopSpeaking
This comimt adds a method into the UDP connection to control the
internal frequency ticker which controls the speed of playback. For more
information, refer to (*voice/udp.Connection).ResetFrequency().

StopSpeaking is removed because it no longer works with variable
parameters. The functionality of that method was also arguably useless,
as it only sends silent frames.
2020-11-17 14:43:00 -08:00
diamondburned f4750292eb Voice: Minor concurrency improvements
This commit adds multiple thread safe guards to do better the
concurrency promises. However, it also omits completely those guarantees
in certain places that don't make sense to call concurrently. This is
mostly documented.

This commit also adds a small piece of code to concurrently run things
with the race detector.
2020-11-17 12:15:05 -08:00
diamondburned 1b8af1513e Voice: UDP now thread unsafe for performance
This commit omits thread safe guarantees in the UDP voice connection.
This is done because the UDP connection should not be shared across
threads, so those guarantees are now omitted for performance.

Session will still guard a UDP connection around a read mutex, because
Session itself has thread safe guarantees. This may change in the
future.
2020-11-17 12:15:05 -08:00
Tyler Stuyfzand 38fe1fafd0 Voice: Add UserID to SpeakingData 2020-11-17 12:15:05 -08:00
Tyler 59539403b3 Voice: Implement Voice Gateway events
Speaking event patches and support Client Connect/Disconnect events.
2020-11-17 12:15:05 -08:00
diamondburned d1242fc39a Voice: Remove Session from repository before disconnecting
This commit introduces this change so that Voice can be called from
other threads without waiting for one Session to disconnect itself.
2020-11-17 11:01:34 -08:00
Tyler Stuyfzand de7d7f62a4
Voice: Add UserID to SpeakingData (#161) 2020-11-15 01:37:56 -08:00
diamondburned f85aac09be Voice: Added intent adders into constructor 2020-11-12 18:55:05 -08:00
mavolin 88258b51c2 replace usage of discordapp.com with discord.com in docs 2020-11-03 10:14:53 -08:00
diamondburned 16c1658163 {,Voice}Gateway: Refactored Closing
This commit refactors both wsutil, the normal Gateway and the Voice
Gateway to have better closing behavior, which should assume less and
cover edge cases completely.
2020-10-30 11:02:37 -07:00
diamondburned 9c1088bf7c Voice: Fixed test error caused by Gateway API changes 2020-10-29 13:27:32 -07:00
diamondburned 2afcd1bc87 Merge Gateway regression fixes from v1 2020-10-29 11:32:54 -07:00
diamondburned f88d4e4b1e Gateway: Fixed wsutil build fail regression 2020-10-29 11:32:30 -07:00
diamondburned 75df94d9f4 *: Migrated Go Modules to v2 2020-10-28 15:39:59 -07:00
diamondburned 6c332ac145 {Voice,}Gateway: Fixed various race conditions
This commit fixes race conditions in both package voice, package
voicegateway and package gateway.

Originally, several race conditions exist when both the user's and the
pacemaker's goroutines both want to do several things to the websocket
connection. For example, the user's goroutine could be writing, and the
pacemaker's goroutine could trigger a reconnection. This is racey.

This issue is partially fixed by removing the pacer loop from package
heart and combining the ticker into the event (pacemaker) loop itself.

Technically, a race condition could still be triggered with care, but
the API itself never guaranteed any of those. As events are handled
using an internal loop into a channel, a race condition will not be
triggered just by handling events and writing to the websocket.
2020-10-22 10:47:27 -07:00
diamondburned 3312c66515 Voice: Made EventLoop a valid struct value instead of nil pointer 2020-08-19 21:32:40 -07:00
diamondburned de61fd912d wsutil: Made PacemakerLoop valid as zero-value 2020-08-19 21:30:57 -07:00
Maximilian von Lindern 1585797b52 *: Linting and typo fixes (#134)
* Linting and typo fixes

* Linting and typo fixes

* revert comma fix
2020-07-29 16:58:33 -07:00
Maximilian von Lindern 908ef96089 Discord: Uint64 typed Snowflakes (#132)
* Use typed Snowflakes if possible

* Discord: make Snowflakes uint64

* Fix errors that emerged because of new typing
2020-07-29 16:58:33 -07:00
Maximilian von Lindern 32789bb6e2 *: Separate utils and internal (#129)
* Utils: move package utils/heart to internal/heart

* Utils: move package utils/moreatomic to internal/moreatomic

* Utils: move package utils/zlib to internal/zlib
2020-07-29 16:58:33 -07:00