Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Call not working #1895

Open
rahulchops93 opened this issue Dec 14, 2024 · 0 comments
Open

Call not working #1895

rahulchops93 opened this issue Dec 14, 2024 · 0 comments

Comments

@rahulchops93
Copy link

rahulchops93 commented Dec 14, 2024

Video Call not working. Can someone please help me out? @jaywink @manuroe @ismailgulek @SBiOSoftWhare @giomfo

import UIKit
import MatrixSDK
import JitsiMeetSDK

class ViewController: UIViewController {
    @IBOutlet weak var localView        : UIView!
    
    let callStack       = MXJingleCallStack()
    var accessToken     : String = ""
    var session         : MXSession!
    var activeCall            : MXCall!
    var createdRoom         : MXRoom!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        MXLogger.setSubLogName("MatrixCall")
        PermissionHandler.shared.requestCameraMicrophone()
        
        
        let credentials = MXCredentials(homeServer: "https://matrix-client.matrix.org",
                                        userId: "",
                                        accessToken: accessToken)
        credentials.deviceId = "isthisdeviceid"
        
        let rest = MXRestClient(credentials: credentials)
        session = MXSession(matrixRestClient: rest)
        
        // Set up the store
        let store = MXFileStore()
        session.setStore(store, completion: { response in
            switch response {
            case .success:
                print("Store set successfully")
                self.startSession()
            case .failure(let error):
                print("Failed to set store: \(error.localizedDescription)")
            }
        })
    }
    
    func startSession() {
        session.start(completion: { response in
            switch response {
            case .success(_):
                print("====== **** Matrix session started successfully. ****")
                self.session.enableVoIP(with: self.callStack)
            case .failure(let err):
                print("====== Error starting session: \(err.localizedDescription)")
            }
        })
    }
    
    @IBAction func openJitsiMeet(sender: Any?) {
        if self.createdRoom == nil {
            createRoomAndInviteUser(userId: "@rahuldeveloper2:matrix.org") { room in
                print("=== Room Created")
                self.createdRoom = room
                self.startCall()
            }
        } else {
            self.startCall()
        }
    }
    
    func startCall() {
        let config = MXCallKitConfiguration(name: "Abc", ringtoneName: nil, iconName: nil, supportsVideo: true)
        let callkit = MXCallKitAdapter(configuration: config)
        session.callManager.callKitAdapter = callkit
        if let callSt = session.callManager.callStack.createCall() as? MXJingleCallStackCall {
            callSt.selfVideoView = self.view
            callSt.remoteVideoView = localView
            
            self.startCapture(callSt: callSt)

            self.session.callManager.placeCall(inRoom: self.createdRoom.roomId, withVideo: true) { call in
                print("=== *** Call success")
                self.activeCall = call
                call.audioMuted = false
                call.videoMuted = false
            } failure: { error in
                print("=== **** \(error)")
            }
            
        }
    }
    
    func startCapture(callSt: MXJingleCallStackCall) {
        callSt.startCapturingMedia(withVideo: true) {
            print("startCapturingMedia success")
        } failure: { error in
            print("startCapturingMedia unsuccess: \(error)")
        }
    }
    
    func createRoomAndInviteUser(userId: String, completion: @escaping (MXRoom?) -> Void) {
        let roomCreationParameters = MXRoomCreationParameters()
        roomCreationParameters.inviteArray = [userId]
        roomCreationParameters.name = UUID().uuidString
        
        session.createRoom(parameters: roomCreationParameters) { response in
            switch response {
            case .success(let room):
                print("Room created successfully")
                completion(room)
            case .failure(let error):
                print("Failed to create room: \(error.localizedDescription)")
                completion(nil)
            }
        }
    }
}


// MARK: - CALL DELEGATE METHODS
extension ViewController: MXCallDelegate {
    func call(_ call: MXCall, stateDidChange state: MXCallState, reason event: MXEvent?) {
        switch state {
        case .connected: print("=== Call Connected")
        case .connecting: print("=== Call Connecting")
        case .createAnswer: print("=== Create Answer")
        case .ended: print("=== Call Ended")
        case .onHold: print("=== Call On Hold")
        case .ringing: print("=== Call Ringing")
        case .fledgling: print("=== Call fledgling")
        case .answeredElseWhere: print("=== Call Answered Elsewhere")
        case .createOffer: print("=== Create Offer")
        case .inviteExpired: print("=== Call Invite Expired")
        case .inviteSent: print("=== Call Invite Sent")
        case .remotelyOnHold: print("=== Call Remotely On Hold")
        case .waitLocalMedia: print("=== Call waitLocalMedia")
        default: print("=== \(state)")
        }
    }
    
    func call(_ call: MXCall, didEncounterError error: any Error, reason: __MXCallHangupReason) {
        print("Call Error: \(error)")
    }
    
    func callAudioOutputRouteTypeDidChange(_ call: MXCall) {
        print("=== callAudioOutputRouteTypeDidChange called")
    }
    
    func configureAudioSession() {
        let audioSession = AVAudioSession.sharedInstance()
        do {
            // Set audio session category
            try audioSession.setCategory(.playAndRecord, mode: .voiceChat, options: [.defaultToSpeaker])
            
            // Activate the audio session
            try audioSession.setActive(true)
            print("Audio session configured successfully")
        } catch {
            print("Failed to configure audio session: \(error.localizedDescription)")
        }
    }
}

extension ViewController: MXCallStackCallDelegate {
    func callStackCall(_ callStackCall: any MXCallStackCall, onICECandidateWithSdpMid sdpMid: String, sdpMLineIndex: Int, candidate: String) {
        print("=== onICECandidateWithSdpMid")
    }
    
    func callStackCall(_ callStackCall: any MXCallStackCall, onError error: (any Error)?) {
        print("=== callStackCall onError")
    }
    
    func callStackCallDidConnect(_ callStackCall: any MXCallStackCall) {
        print("=== callStackCallDidConnect")
    }
    
    func callStackCallDidRemotelyHold(_ callStackCall: any MXCallStackCall) {
        print("=== callStackCallDidRemotelyHold")
    }   
}
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant