-
Notifications
You must be signed in to change notification settings - Fork 7
Updating Small WebRTC transport to Pipecat Client v1.1.0 #27
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| val libraryVersion = "1.0.3" | ||
| val libraryVersion = "1.1.0" | ||
|
|
||
| plugins { | ||
| alias(libs.plugins.android.library) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package ai.pipecat.client.small_webrtc_transport | ||
|
|
||
| import ai.pipecat.client.PipecatClient | ||
|
|
||
| typealias PipecatClientSmallWebRTC = PipecatClient<SmallWebRTCTransport, SmallWebRTCTransportConnectParams> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package ai.pipecat.client.small_webrtc_transport | ||
|
|
||
| import ai.pipecat.client.types.Value | ||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| internal data class SmallWebRTCStartBotResult( | ||
| val sessionId: String, | ||
| val iceConfig: Value = Value.Null | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,17 +5,18 @@ import ai.pipecat.client.result.RTVIError | |
| import ai.pipecat.client.result.resolvedPromiseErr | ||
| import ai.pipecat.client.result.resolvedPromiseOk | ||
| import ai.pipecat.client.result.withPromise | ||
| import ai.pipecat.client.transport.AuthBundle | ||
| import ai.pipecat.client.transport.MsgClientToServer | ||
| import ai.pipecat.client.transport.MsgServerToClient | ||
| import ai.pipecat.client.transport.Transport | ||
| import ai.pipecat.client.transport.TransportContext | ||
| import ai.pipecat.client.transport.TransportFactory | ||
| import ai.pipecat.client.types.APIRequest | ||
| import ai.pipecat.client.types.MediaDeviceId | ||
| import ai.pipecat.client.types.MediaDeviceInfo | ||
| import ai.pipecat.client.types.Participant | ||
| import ai.pipecat.client.types.ParticipantId | ||
| import ai.pipecat.client.types.TransportState | ||
| import ai.pipecat.client.types.Value | ||
| import ai.pipecat.client.utils.ThreadRef | ||
| import android.annotation.SuppressLint | ||
| import android.content.Context | ||
| import android.media.AudioManager | ||
|
|
@@ -37,11 +38,9 @@ private val LOCAL_PARTICIPANT = Participant( | |
| local = true | ||
| ) | ||
|
|
||
| class SmallWebRTCTransport internal constructor( | ||
| private val transportContext: TransportContext, | ||
| androidContext: Context, | ||
| private val serverUrl: String, | ||
| ) : Transport() { | ||
| class SmallWebRTCTransport( | ||
| context: Context, | ||
| ) : Transport<SmallWebRTCTransportConnectParams>() { | ||
|
|
||
| companion object { | ||
| private const val TAG = "SmallWebRTCTransport" | ||
|
|
@@ -64,30 +63,30 @@ class SmallWebRTCTransport internal constructor( | |
| val Rear = MediaDeviceInfo(id = MediaDeviceId("cam-rear"), name = "Rear Camera") | ||
| } | ||
|
|
||
| class Factory( | ||
| private val androidContext: Context, | ||
| private val serverUrl: String | ||
| ) : TransportFactory { | ||
| override fun createTransport(context: TransportContext): Transport { | ||
| return SmallWebRTCTransport(context, androidContext, serverUrl) | ||
| } | ||
| } | ||
| private lateinit var transportContext: TransportContext | ||
| private lateinit var thread: ThreadRef | ||
|
|
||
| private var state = TransportState.Disconnected | ||
|
|
||
| private val appContext = androidContext.applicationContext | ||
| private val thread = transportContext.thread | ||
| private val appContext = context.applicationContext | ||
|
|
||
| private var client: WebRTCClient? = null | ||
| private var selectedCam = CameraMode.Front | ||
|
|
||
| override fun initialize(ctx: TransportContext) { | ||
| transportContext = ctx | ||
| thread = ctx.thread | ||
| } | ||
|
|
||
| override fun initDevices(): Future<Unit, RTVIError> = resolvedPromiseOk(thread, Unit) | ||
|
|
||
| @SuppressLint("MissingPermission") | ||
| override fun connect(authBundle: AuthBundle?): Future<Unit, RTVIError> = | ||
| override fun connect( | ||
| transportParams: SmallWebRTCTransportConnectParams | ||
| ): Future<Unit, RTVIError> = | ||
| thread.runOnThreadReturningFuture { | ||
|
|
||
| Log.i(TAG, "connect(${authBundle})") | ||
| Log.i(TAG, "connect(${transportParams})") | ||
|
|
||
| if (client != null) { | ||
| return@runOnThreadReturningFuture resolvedPromiseErr( | ||
|
|
@@ -114,7 +113,7 @@ class SmallWebRTCTransport internal constructor( | |
| disconnect() | ||
| } | ||
|
|
||
| "renegotiate" -> negotiate() | ||
| "renegotiate" -> negotiate(transportParams) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there any reason this should use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't look like there's any way for this to change in the meantime -- the type is immutable and we don't change the |
||
| } | ||
|
|
||
| } else { | ||
|
|
@@ -139,7 +138,8 @@ class SmallWebRTCTransport internal constructor( | |
| } else { | ||
| null | ||
| }, | ||
| initialMicEnabled = transportContext.options.enableMic | ||
| initialMicEnabled = transportContext.options.enableMic, | ||
| rtviProtocolVersion = transportContext.protocolVersion | ||
| ) | ||
| } catch (e: Exception) { | ||
| return@runOnThreadReturningFuture resolvedPromiseErr( | ||
|
|
@@ -148,18 +148,17 @@ class SmallWebRTCTransport internal constructor( | |
| ) | ||
| } | ||
|
|
||
| negotiate() | ||
| negotiate(transportParams) | ||
| } | ||
|
|
||
| private fun negotiate() = withPromise<Unit, RTVIError>(thread) { promise -> | ||
| private fun negotiate( | ||
| connectParams: SmallWebRTCTransportConnectParams | ||
| ) = withPromise<Unit, RTVIError>(thread) { promise -> | ||
|
|
||
| MainScope().launch { | ||
|
|
||
| try { | ||
| client?.negotiateConnection( | ||
| url = serverUrl, | ||
| restartPc = false | ||
| ) | ||
| client?.negotiateConnection(connectParams) | ||
|
|
||
| val cb = transportContext.callbacks | ||
| setState(TransportState.Connected) | ||
|
|
@@ -174,6 +173,24 @@ class SmallWebRTCTransport internal constructor( | |
| } | ||
| } | ||
|
|
||
| override fun deserializeConnectParams( | ||
| json: String, | ||
| startBotRequest: APIRequest | ||
| ): SmallWebRTCTransportConnectParams { | ||
| val startBotResult = JSON_INSTANCE.decodeFromString<SmallWebRTCStartBotResult>(json) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we should also add a check to make sure the |
||
|
|
||
| return SmallWebRTCTransportConnectParams( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| webrtcRequestParams = APIRequest( | ||
| endpoint = startBotRequest.endpoint.replace( | ||
| "/start", | ||
| "/sessions/${startBotResult.sessionId}/api/offer" | ||
| ), | ||
| requestData = Value.Object(), | ||
| headers = startBotRequest.headers | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| override fun disconnect(): Future<Unit, RTVIError> = thread.runOnThreadReturningFuture { | ||
| withPromise(thread) { promise -> | ||
|
|
||
|
|
@@ -185,6 +202,7 @@ class SmallWebRTCTransport internal constructor( | |
| if (clientRef != null) { | ||
| clientRef.dispose() | ||
| setState(TransportState.Disconnected) | ||
| transportContext.onConnectionEnd() | ||
| transportContext.callbacks.onDisconnected() | ||
| } | ||
| promise.resolveOk(Unit) | ||
|
|
@@ -256,8 +274,6 @@ class SmallWebRTCTransport internal constructor( | |
| override fun enableMic(enable: Boolean): Future<Unit, RTVIError> = client?.setMicEnabled(enable) | ||
| ?: resolvedPromiseErr(thread, RTVIError.TransportNotInitialized) | ||
|
|
||
| override fun expiry() = null | ||
|
|
||
| override fun enableCam(enable: Boolean): Future<Unit, RTVIError> = | ||
| client?.setCamMode(if (enable) selectedCam else null) | ||
| ?: resolvedPromiseErr(thread, RTVIError.TransportNotInitialized) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package ai.pipecat.client.small_webrtc_transport | ||
|
|
||
| import ai.pipecat.client.types.APIRequest | ||
|
|
||
| data class SmallWebRTCTransportConnectParams( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also add the |
||
| val webrtcRequestParams: APIRequest | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| package ai.pipecat.client.small_webrtc_transport | ||
|
|
||
| val SMALL_WEBRTC_TRANSPORT_VERSION: String = ai.pipecat.client.small_webrtc_transport.BuildConfig.VERSION_NAME |
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.
This
iceConfigmaybe it would be nice to create a specific type for it. For example this is the one for Web:Where
RTCIceServeris this: https://udn.realityripple.com/docs/Web/API/RTCIceServer