Skip to content
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

Implement Media Sink Wants in Voice #898

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions voice/src/main/kotlin/gateway/Command.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public sealed class Command {
composite.encodeSerializableElement(descriptor, 0, OpCode.Serializer, OpCode.Heartbeat)
composite.encodeLongElement(descriptor, 1, value.nonce)
}
is MediaSinkWants -> {
composite.encodeSerializableElement(descriptor, 0, OpCode.Serializer, OpCode.MediaSinkWants)
composite.encodeSerializableElement(descriptor, 1, MediaSinkWants.serializer(), value)
}
is SendSpeaking -> {
composite.encodeSerializableElement(descriptor, 0, OpCode.Serializer, OpCode.Speaking)
composite.encodeSerializableElement(descriptor, 1, SendSpeaking.serializer(), value)
Expand Down Expand Up @@ -72,6 +76,12 @@ public data class SendSpeaking(
val ssrc: UInt
) : Command()

@KordVoice
@Serializable
public data class MediaSinkWants(
val any: Int
viztea marked this conversation as resolved.
Show resolved Hide resolved
) : Command()

@KordVoice
@Serializable
public data class SelectProtocol(
Expand Down
3 changes: 2 additions & 1 deletion voice/src/main/kotlin/gateway/DefaultVoiceGateway.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public data class DefaultVoiceGatewayData(
val sessionId: String,
val client: HttpClient,
val reconnectRetry: Retry,
val isDeaf: Boolean,
val eventFlow: MutableSharedFlow<VoiceEvent>
)

Expand Down Expand Up @@ -196,7 +197,7 @@ public class DefaultVoiceGateway(
val copy = command.copy(data = command.data.copy(address = "ip"))
"Voice Gateway >>> ${Json.encodeToString(Command.SerializationStrategy, copy)}"
}
is Heartbeat, is Resume, is SendSpeaking -> "Voice Gateway >>> $json"
else -> "Voice Gateway >>> $json"
viztea marked this conversation as resolved.
Show resolved Hide resolved
}
}
socket.send(Frame.Text(json))
Expand Down
2 changes: 2 additions & 0 deletions voice/src/main/kotlin/gateway/DefaultVoiceGatewayBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class DefaultVoiceGatewayBuilder(
public var client: HttpClient? = null
public var reconnectRetry: Retry? = null
public var eventFlow: MutableSharedFlow<VoiceEvent> = MutableSharedFlow(extraBufferCapacity = Int.MAX_VALUE)
public var isDeaf: Boolean = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VoiceConnectionBuilder already has the selfDeaf option, can't this be used instead?

Copy link
Contributor Author

@viztea viztea Dec 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose. I think the VoiceConnectionBuilder could get cleaned up quite a bit though.

Like I state in the PR description, you can set selfDeaf and a streams implementation.

Since selfDeaf is just a UI update right now it's not that much of an issue but if it gets updated to actually prevent voice receive it might be a good idea to set it depending on the streams implementation so that there isn't any confusion.

And maybe add some more functionality to it as well:

interface Streams {
	suspend fun setDeafen(value: Boolean)
	
	suspend fun setMute(ssrc: UInt, value: Boolean)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a fair solution. Makes the deafening much behavior much more consistent, especially with this addition. Streams#setDeafen might not be needed, as it isn't provided for the UI deafen either. I could see the use for a VoiceConnection#(set)Deafen which would UI deafen and media sink deafen (if streams are open).


public fun build(): DefaultVoiceGateway {
val client = client ?: HttpClient(CIO) {
Expand All @@ -37,6 +38,7 @@ public class DefaultVoiceGatewayBuilder(
sessionId,
client,
retry,
isDeaf,
eventFlow
)

Expand Down
3 changes: 2 additions & 1 deletion voice/src/main/kotlin/gateway/OpCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public enum class OpCode(public val code: Int) {
Resume(7),
Hello(8),
Resumed(9),
ClientDisconnect(13);
ClientDisconnect(13),
MediaSinkWants(15);

internal object Serializer : KSerializer<OpCode> {
override val descriptor: SerialDescriptor
Expand Down
1 change: 1 addition & 0 deletions voice/src/main/kotlin/gateway/handler/HandshakeHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ internal class HandshakeHandler(
on<Hello> {
data.reconnectRetry.reset()
send(identify)
send(MediaSinkWants(if (data.isDeaf) 0 else 100))
}
}
}
Loading