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

How can i create producer? #25

Open
Raonshi opened this issue Nov 9, 2023 · 3 comments
Open

How can i create producer? #25

Raonshi opened this issue Nov 9, 2023 · 3 comments

Comments

@Raonshi
Copy link

Raonshi commented Nov 9, 2023

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
Copy link
Collaborator

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
Copy link
Author

Raonshi commented Nov 14, 2023

@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
Copy link

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~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants