mediasoup-client-swift
mediasoup-client-swift copied to clipboard
How can i create producer?
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) {
}
}
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)")
}
@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.
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~