stasel / WebRTC

Unofficial distribution of up to date WebRTC framework binaries for iOS and macOS
Other
359 stars 76 forks source link

Crashes at when starting : Fatal error: init(coder:) has not been implemented #1

Closed lucas-goldner closed 3 years ago

lucas-goldner commented 3 years ago

Hey Stasel, I am very thankful for this implementation of WebRTC on IOS. I am currently trying to build your WebRTCIOS App as a viewcontroller for my superapp. I have refactored the webrtcclient inilizising into my viewcontroller as well as the signnalclient and websocketProvider. I have literally copy pasted your code and just renamed a few files and classes bcs they conflicted with my other Websockets. Do you have any idea what I am doing wrong here ? It crashes at because of Fatal error: init(coder:) has not been implemented

import UIKit
import WebRTC

class waitingViewController: UIViewController {

    private let signalClient: SignalingClient
    private var webRTCClient: WebRTCClient = WebRTCClient(iceServers: Config.default.webRTCIceServers)
    let webSocketProvider: WebSocketProvider  = WorkoutSocket(url: Config.default.signalingServerUrl)

    @IBOutlet weak var videoView: UIView!
    @IBAction func StartTalkButton(_ sender: Any) {

    }

    private var signalingConnected: Bool = false {
        didSet {
            DispatchQueue.main.async {
                if self.signalingConnected {
                    print("Connected")
//                    self.signalingStatusLabel?.text = "Connected"
//                    self.signalingStatusLabel?.textColor = UIColor.green
                }
                else {
                    print("Disconnected")
//                    self.signalingStatusLabel?.text = "Not connected"
//                    self.signalingStatusLabel?.textColor = UIColor.red
                }
            }
        }
    }

    private var hasLocalSdp: Bool = false {
        didSet {
//            DispatchQueue.main.async {
//                self.localSdpStatusLabel?.text = self.hasLocalSdp ? "✅" : "❌"
//            }
        }
    }

    private var localCandidateCount: Int = 0 {
        didSet {
//            DispatchQueue.main.async {
//                self.localCandidatesLabel?.text = "\(self.localCandidateCount)"
//            }
        }
    }

    private var hasRemoteSdp: Bool = false {
        didSet {
//            DispatchQueue.main.async {
//                self.remoteSdpStatusLabel?.text = self.hasRemoteSdp ? "✅" : "❌"
//            }
        }
    }

    private var remoteCandidateCount: Int = 0 {
        didSet {
//            DispatchQueue.main.async {
//                self.remoteCandidatesLabel?.text = "\(self.remoteCandidateCount)"
//            }
        }
    }

    private var speakerOn: Bool = false {
        didSet {
//            let title = "Speaker: \(self.speakerOn ? "On" : "Off" )"
//            self.speakerButton?.setTitle(title, for: .normal)
        }
    }

    private var mute: Bool = false {
        didSet {
//            let title = "Mute: \(self.mute ? "on" : "off")"
//            self.muteButton?.setTitle(title, for: .normal)
        }
    }

    init(signalClient: SignalingClient, webRTCClient: WebRTCClient) {
        self.signalClient = signalClient
        self.webRTCClient = webRTCClient
        super.init(nibName: String(describing: waitingViewController.self), bundle: Bundle.main)
    }

    @available(*, unavailable)
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "WebRTC Demo"
        self.signalingConnected = false
        self.hasLocalSdp = false
        self.hasRemoteSdp = false
        self.localCandidateCount = 0
        self.remoteCandidateCount = 0
        self.speakerOn = false
        self.webRTCClient.delegate = self
        self.signalClient.delegate = self
        self.signalClient.connect()
    }
}

extension waitingViewController: SignalClientDelegate {
    func signalClient(_ signalClient: SignalingClient, didReceiveRemoteSdp sdp: RTCSessionDescription) {
        print("Received remote sdp")
        self.webRTCClient.set(remoteSdp: sdp) { (error) in
            self.hasRemoteSdp = true
        }
    }

    func signalClientDidConnect(_ signalClient: SignalingClient) {
        self.signalingConnected = true
    }

    func signalClientDidDisconnect(_ signalClient: SignalingClient) {
        self.signalingConnected = false
    }

    func signalClient(_ signalClient: SignalingClient, didReceiveCandidate candidate: RTCIceCandidate) {
        print("Received remote candidate")
        self.remoteCandidateCount += 1
        self.webRTCClient.set(remoteCandidate: candidate)
    }
}

extension waitingViewController: WebRTCClientDelegate {

    func webRTCClient(_ client: WebRTCClient, didDiscoverLocalCandidate candidate: RTCIceCandidate) {
        print("discovered local candidate")
        self.localCandidateCount += 1
        self.signalClient.send(candidate: candidate)
    }

    func webRTCClient(_ client: WebRTCClient, didChangeConnectionState state: RTCIceConnectionState) {
        let textColor: UIColor
        switch state {
        case .connected, .completed:
            textColor = .green
        case .disconnected:
            textColor = .orange
        case .failed, .closed:
            textColor = .red
        case .new, .checking, .count:
            textColor = .black
        @unknown default:
            textColor = .black
        }
        DispatchQueue.main.async {
//            self.webRTCStatusLabel?.text = state.description.capitalized
//            self.webRTCStatusLabel?.textColor = textColor
        }
    }

    func webRTCClient(_ client: WebRTCClient, didReceiveData data: Data) {
        DispatchQueue.main.async {
            let message = String(data: data, encoding: .utf8) ?? "(Binary: \(data.count) bytes)"
            let alert = UIAlertController(title: "Message from WebRTC", message: message, preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
            self.present(alert, animated: true, completion: nil)
        }
    }
}
lucas-goldner commented 3 years ago

Sorry my bad found the issue it works