ethand91 / mediasoup-ios-client

Mediasoup 3 iOS Client
ISC License
131 stars 65 forks source link

Error on MediasoupDevice.createSendTransport #60

Closed NullIsOne closed 4 years ago

NullIsOne commented 4 years ago

I'm trying to create sendTransport and got error.

*** Terminating app due to uncaught exception 'RuntimeException', reason: '[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal'

I think that problem could be somewhere in iceParameters, iceCandidates or dtlsParameters... I'm using Codable approach and short extension to encode this parameters before using.

var jsonEncoded: String {
        guard let jsonData = try? JSONEncoder().encode(self) else {
            fatalError("Encoding error")
        }
        return String(data: jsonData, encoding: .utf8)!
  }

Nothing special. And this encoding worked with MediasoupDevice.load method.. Can someone help me resolve this issue?

NullIsOne commented 4 years ago

Example of encoded parameters

"{\"fingerprints\":[{\"value\":\"F1:D5:34:79:DE:20:F4:CC:13:6F:17:23:4A:E7:73:87:6D:62:77:7E\",\"algorithm\":\"sha-1\"},{\"value\":\"DA:FC:B4:E0:1E:64:DC:7A:EB:58:25:70:08:C1:CE:21:F1:D1:33:62:49:80:5A:8B:4F:42:FC:72\",\"algorithm\":\"sha-224\"},{\"value\":\"28:28:CD:72:86:ED:9F:DF:81:28:D2:FB:F7:70:B5:2A:BB:3D:EB:D2:21:93:66:56:30:C7:2F:5C:64:EC:D8:A6\",\"algorithm\":\"sha-256\"},{\"value\":\"66:CE:65:15:D0:55:AC:87:93:0D:BA:7B:AA:9D:6A:9B:BF:BC:24:AC:73:40:92:6B:BC:2A:F9:DD:DA:67:D3:26:D6:F6:64:B9:3B:B8:73:90:19:15:C2:98:F7:5A:FC:2A\",\"algorithm\":\"sha-384\"},{\"value\":\"30:A0:BE:99:E3:0A:A3:22:91:32:42:65:FD:25:08:EA:EB:2B:2B:4C:32:A7:81:FE:8B:8A:67:A8:C0:F8:45:BF:3D:7A:EB:62:5B:8F:63:5F:94:FE:0A:6F:97:11:62:E9:84:48:D0:D9:62:B6:72:97:94:B4:9B:0C:71:06:96:11\",\"algorithm\":\"sha-512\"}],\"role\":\"auto\"}"    
"[{\"port\":48397,\"protocol\":\"tcp\",\"tcpType\":\"passive\",\"type\":\"host\",\"foundation\":\"tcpcandidate\",\"priority\":1076302079,\"ip\":\"00.000.000.000\"}]"   
"{\"iceLite\":true,\"usernameFragment\":\"xxxxxxxxxxxx\",\"password\":\"xxxxxxxxxxxxxxxxxxxxxx\"}"  
ethand91 commented 4 years ago

Yes it could be a problem with the parameters.

json.exception.parse_error.101

This error indicates a syntax error while deserializing a JSON text. The error message describes that an unexpected token (character) was encountered, and the member byte indicates the error position. 

Might be worth creating an issue over at https://github.com/nlohmann/json

NullIsOne commented 4 years ago

Understood Maybe it will be duplicate of https://github.com/nlohmann/json/issues/1075 Anyway this issue was not fixed.. =(

ethand91 commented 4 years ago

I managed to get it work in the sample using "SwiftyJson" to get the JSON object and then pass it as an argument via the object's description.

https://github.com/ethand91/mediasoup-ios-client-sample/blob/master/mediasoup-ios-cient-sample/room/RoomClient.swift

NullIsOne commented 4 years ago

I've tried it.. no luck =(

ethand91 commented 4 years ago

How are the parameters being passed from the server? Is it an object or a string?

NullIsOne commented 4 years ago

They passed as object. We're using Socket.io as signalling. In Swift terms parameters passed as [Any], then we use second element and decoding this Any to

public struct TransportResponse: Codable {
    public let dtlsParameters: JSON
    public let iceCandidates: JSON
    public let iceParameters: JSON
    public let id: String
}

Where dtlsParameters, iceCandidates and iceParameters is objects according to https://mediasoup.org/documentation/v3/mediasoup/api/#WebRtcTransportIceParameters

ethand91 commented 4 years ago

How are you passing them as arguments to createSendTransport?

NullIsOne commented 4 years ago

I've tried two approaches First described in topic Second, as SwiftyJSON.description, as you suggested

ethand91 commented 4 years ago

Don't know if it will help but this is how I did it: https://github.com/ethand91/mediasoup-ios-client-sample/blob/master/mediasoup-ios-cient-sample/room/RoomClient.swift [createWebRtcTransport]

btw I'm assuming createRecvTransport also fails?

NullIsOne commented 4 years ago

btw I'm assuming createRecvTransport also fails?

yes

MartinP7r commented 4 years ago

Hi @NullIsOne
I'm also using Socket.io for signaling and got the createSendTransport working as in the sample below.
Maybe you can try it like this (without using Codable) once and see if it works.
Maybe your socket.io server side is not passing valid json in the first place?

socket.emitWithAck(/*...*/).timingOut(/*...*/) { [weak self] transportData in
            guard let self = self else { return }
            let id: String = transportData["id"].stringValue
            let iceParams = transportData["iceParameters"].description
            let iceCandidates = transportData["iceCandidates"].description
            let dtlsParams = transportData["dtlsParameters"].description
            self.sendTransport = self.device.createSendTransport(self,
                                                                 id: id,
                                                                 iceParameters: iceParams,
                                                                 iceCandidates: iceCandidates,
                                                                 dtlsParameters: dtlsParams)
// other stuff
        }
NullIsOne commented 4 years ago

I tried this too. @MartinP7r which exact versions of Socket.io and SwiftyJSON are you using in this working solution? Maybe it could help me

MartinP7r commented 4 years ago

Latest for both.
Can't tell you the numbers since I can't access my project right now, but I ran pod update on it today and it was still working for the create transport call.

edit:

ethand91 commented 4 years ago

Closing for now. Please reopen if the issue persists.

NullIsOne commented 4 years ago

Sorry. I had a mistake in debugging. Transport created successfully, but call to transport.produce has failed, because I passed [] to encodings.. No crash if I pass nil. transport.produce(producerListener!, track: videoTrack, encodings: nil, codecOptions: nil) I was confused by encodings: [Any]! annotation. In swift it means that nullable values are not allowed here.

Btw, Issue could stay closed. I will use my workaround for sometime, and add proper encodings array little bit later.