VLprojects / mediasoup-client-swift

Swift wrapper for libmediasoupclient
MIT License
42 stars 17 forks source link

How can i create producer? #25

Open Raonshi opened 11 months ago

Raonshi commented 11 months ago

Hello. I just have a problem about creating Producer. I'm making video chat application using MQTT and Mediasoup.

When I call the "SendTransport.createProducer()" function, it returns nil always. I don't know why.

Could you give me some advices?

Here are my sample code

// Mediasoup Class
private func generateSendTransport(params: [String:Any]) {
    if(device == nil){
        return
    }

    do{
        let transportParams = try? JSONSerialization.data(withJSONObject: params)
        if(transportParams == nil){
            return
        }

        let decoded = try? JSONDecoder().decode(TransportParams.self, from: transportParams!)
        if(decoded == nil){
            return
        }

        let iceParameters = try JSONEncoder().encode(decoded!.iceParameters)
        let iceCandidates = try JSONEncoder().encode(decoded!.iceCandidates)
        let dtlsParameters = try JSONEncoder().encode(decoded!.dtlsParameters)
        let sctpParameters = try JSONEncoder().encode(decoded!.sctpParameters)
        let appData = try JSONEncoder().encode(decoded!.appData)

        sendTransport = try device!.createSendTransport(
            id: decoded!.id,
            iceParameters: String(data: iceParameters, encoding: .utf8)!,
            iceCandidates: String(data: iceCandidates, encoding: .utf8)!,
            dtlsParameters: String(data: dtlsParameters, encoding: .utf8)!,
            sctpParameters: String(data: sctpParameters, encoding: .utf8)!,
            appData: nil)
        sendTransport?.delegate = self

        DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + 1.0) {[self] in
            produce()
        }
    }catch{
        return
    }
  }

private func produce() {
      videoProducer = try? sendTransport?.createProducer(for: videoTrack!, encodings: nil, codecOptions: nil, codec: nil, appData: nil)
      audioProducer = try? sendTransport?.createProducer(for: audioTrack!, encodings: nil, codecOptions: nil, codec: nil, appData: nil)
}
// End of Mediasoup class

// Delegate of Transport
extension RfUnityRoom : SendTransportDelegate {
    func onProduce(transport: Mediasoup.Transport, kind: Mediasoup.MediaKind, rtpParameters: String, appData: String, callback: @escaping (String?) -> Void) {        
        let me = getMe()
        if(me == nil){
            return
        }
        mqtt?.sendMessage(topic: reqTopic, request: [
            "type": "produce",
            "payload": [
                "conferenceID": roomId,
                "endpointID": me!.uuid,
                "transportId": transport.id,
                "rtpParameters": rtpParameters,
                "kind": kind,
                "appData": appData,
            ] as[String:Any],
        ])
    }

    func onProduceData(transport: Mediasoup.Transport, sctpParameters: String, label: String, protocol dataProtocol: String, appData: String, callback: @escaping (String?) -> Void) {
    }

    func onConnect(transport: Mediasoup.Transport, dtlsParameters: String) {
        let me = getMe()
        if(me == nil){
            return
        }

        mqtt?.sendMessage(topic: reqTopic, request: [
            "type": "connectWebRtcTransport",
            "payload": [
                "conferenceID": roomId,
                "endpointID": me!.uuid,
                "transportId": transport.id,
                "dtlsParameters": dtlsParameters,
            ] as [String:Any],
        ])
    }

    func onConnectionStateChange(transport: Mediasoup.Transport, connectionState: Mediasoup.TransportConnectionState) {
    }
}
fedulvtubudul commented 10 months ago

Hello.

When I call the "SendTransport.createProducer()" function, it returns nil always. I don't know why.

createProducer method does not ever returns nil, it just cannot do that. To see that, you should start with implementing a proper errors handling. It will give you useful information for further steps.

do {
    videoProducer = try sendTransport?.createProducer(for: videoTrack!, encodings: nil, codecOptions: nil, codec: nil, appData: nil)
    audioProducer = try sendTransport?.createProducer(for: audioTrack!, encodings: nil, codecOptions: nil, codec: nil, appData: nil)
} catch {
    print("Failed to create producer with error \(error)")
}
Raonshi commented 10 months ago

@fedulvtubudul

Hello. Thanks for your advice. Actually, I solved this issue last night. I just removed every code in onProduce event except callback function. After that, it works well.

But, i don't know why it works well. To analyze this issue, I can share more detail information.

The answer of your comment is, I receviced MediasoupClientErrorDomain Code=3 message. this error doesn't have any userInfo value.

RyanTokManMokMTM commented 8 months ago

I received the same error as you when i'm creating the producer unknown: Error Domain=MediasoupClientErrorDomain Code=3 "The associated promise has been destructed prior to the associated state becoming ready." UserInfo={NSLocalizedDescription=The associated promise has been destructed prior to the associated state becoming ready.}

But no idea how to solve this~