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

Call not initiative - Matrix sdk #1872

Open
meenakshisundaramnatarajan opened this issue Jul 8, 2024 · 0 comments
Open

Call not initiative - Matrix sdk #1872

meenakshisundaramnatarajan opened this issue Jul 8, 2024 · 0 comments

Comments

@meenakshisundaramnatarajan

Hi,
I've created a new demo project within myself. I included the 'MatrixSDK' in my pod, and then I implemented the call functionality. Everything seems to be set up correctly. However, when I tried making a call from iOS to a web application, the call wasn't answered. Below, I've shared my code. Could you please review it and let me know your thoughts?

//
// ViewController.swift
// DemoAudiocall
//
// Created by Sundar-63336 on 06/07/24.
//

import UIKit
import MatrixSDK

class ViewController: UIViewController {

var mxSession: MXSession!
var call: MXCall?
var roomId: String?

@IBOutlet weak var audiobtn: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    self.initialSetup()
}

func initialSetup() {
    let credentials = MXCredentials(homeServer: "https://matrix.org", userId: "@dineshkumar.e:matrix.org", accessToken: "syt_ZGluZXNoa3VtYXIuZQ_UpdiCiOwMNBCIvTXIEPs_16eMAS")
    
    mxSession = MXSession(matrixRestClient: MXRestClient(credentials: credentials))
    
    mxSession.start { [weak self] response in
        guard let self = self else { return }
        
        switch response {
        case .success:
            print("Matrix session started successfully")
            self.enableCallSupport()
        case .failure(let error):
            print("Failed to start Matrix session: \(error)")
        }
    }
}

func enableCallSupport() {
    mxSession.enableVoIP(with: self)
    if let callManager = mxSession.callManager {
        print("Call manager initialized successfully")
    } else {
        print("Failed to initialize call manager")
    }
}

func initializeCallManager(with userId: String) {
    guard let callManager = mxSession.callManager else {
        print("Call manager is not initialized")
        return
    }
    
    if let roomId = roomId {
        // Use the existing room if it's already created
        placeCall(in: roomId, to: userId)
    } else {
        // Otherwise, create a new room and then place the call
        createRoomAndInviteUser(userId: userId) { [weak self] room in
            guard let self = self, let room = room else {
                print("Failed to create or find the room")
                return
            }
            
            self.roomId = room.roomId // Store the room ID for future use
            self.placeCall(in: room.roomId, to: userId)
        }
    }
}

func createRoomAndInviteUser(userId: String, completion: @escaping (MXRoom?) -> Void) {
       let roomCreationParameters = MXRoomCreationParameters()
       roomCreationParameters.inviteArray = [userId]
       
       mxSession.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)
           }
       }
   }

func placeCall(in roomId: String, to userId: String) {
    guard let callManager = mxSession.callManager else {
        print("Call manager is not initialized")
        return
    }
    
    let isVideoCall = false
    
    callManager.placeCall(inRoom: roomId, withVideo: isVideoCall, success: { [weak self] call in
        guard let self = self else { return }
        
        self.call = call
        self.setupCallDelegate()
        print("Call started successfully")
        
    }) { error in
        if let error = error {
            print("Error placing call: \(error.localizedDescription)")
        } else {
            print("Error placing call: unknown error")
        }
    }
}

@IBAction func audiobtnTapped(_ sender: Any) {
    let userIdToCall = "@meenakshisundaram:matrix.org"
    self.initializeCallManager(with: userIdToCall)
}

func setupCallDelegate() {
    call?.delegate = self
}

}

extension ViewController: MXCallDelegate,MXCallStack {
func createCall() -> (any MXCallStackCall)? {
guard let callManager = mxSession.callManager else {
print("Call manager is not initialized")
return nil
}

// let roomId = "!ijZKIzpnHteGBNJXXm:matrix.org"
let roomId = roomId ?? "Nil"
let isVideoCall = false

    var createdCall: MXCallStackCall?
    
    callManager.placeCall(inRoom: roomId, withVideo: isVideoCall, success: { (call) in
        createdCall = call as? any MXCallStackCall
        self.call = call
        self.setupCallDelegate()
        print("Call created successfully")
    }) { (error) in
        print("Error creating call: \(error?.localizedDescription ?? "Unknown error")")
    }
    
    return createdCall
}

func call(_ call: MXCall, stateDidChange state: MXCallState, reason event: MXEvent?) {
    switch state {
    case .connecting:
        print("Connecting...")
    case .connected:
        print("Call connected")
        // Handle UI updates or start audio streaming
    case .ringing:
        print("Ringing...")
    case .ended:
        print("Call ended")
        // Handle UI updates or cleanup
    default:
        break
    }
}

func call(_ call: MXCall!, didEncounterError error: Error!) {
    print("Call error: \(error.localizedDescription)")
}

}

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

1 participant