Not able to return a desired value inside a overidden method using coroutines in kotlin

Viewed 46

I am new to kotlin and coroutines.I have been working on a client-server part of an android app.I am using mediasoup-client-android library for this.

I am trying to intialize the sendtransports using createSendTransport() method.This method does have a sendTransportListener which has abstract methods.One of them being is onProduce() which returns a producerId which is a String. awaitEmit() is an asynchronous action and I need this in onProduce().To use awaitEmit() I used coroutines.But I need String as return type instead of Deferred.Is there any other way to implement the mentioned logic? Below is my code

class RoomClient {
 suspend fun initTransports(device: Device) {
    coroutineScope {
        val id: String?
        val iceParameters: String?
        val iceCandidates: String?
        val dtlsParameters: String?
        val sctpParameters: String?
        try {
            val params = JSONObject()
            params.put("forceTcp",false)
            params.put("rtpCapabilities", this@RoomClient.device?.rtpCapabilities)
            val res = socket?.awaitEmit("createWebRtcTransport",params)
            val data = res?.get(0) as JSONObject
            if (data.has("error")) {
                Log.d(TAG, data.getString("error"))
                return@coroutineScope
            }
            id = data.optString("id")
            iceParameters = data.optString("iceParameters")
            iceCandidates = data.optString("iceCandidates")
            dtlsParameters = data.optString("dtlsParameters")
            sctpParameters = data.optString("sctpParameters")
        } catch (e: Throwable) {
            Log.e(TAG, "${e.message}")
            return@coroutineScope
        }
        val sendTransportListener: SendTransport.Listener = object : SendTransport.Listener {
            private val listenerTAG = TAG.toString() + "_ProducerTrans"
            override fun onProduce(
                transport: Transport,
                kind: String?,
                rtpParameters: String?,
                appData: String?
            ): String? {
                this@coroutineScope.async{
                    var producerId: String? = null
                    Log.d(listenerTAG, "onProduce() ")
                    val producerDeferred = launch {
                    val params = JSONObject("""{"producerTransportId": transport.id, "kind": kind, "rtpParameters": rtpParameters,"appData": appData}""")
                    val res = socket?.awaitEmit("produce",params)
                    val data = res?.get(0) as JSONObject
                    producerId = data.getString("producer_Id")
                    }
                    producerDeferred.join()
                    Log.d(listenerTAG, "producerId inside the coroutine: $producerId"
                    }
                return@async producerId
            }
        }
        this@RoomClient.producerTransport = device.createSendTransport(
            sendTransportListener, id,
            iceParameters,
            iceCandidates,
            dtlsParameters
        )

    }
}

}

And also I am not sure about the way coroutines are used here.Please correct me If I have missed something central.

0 Answers
Related