# Pre-call tests (/en/realtime-media/interactive-live-streaming/build/optimize-quality-and-connection/pre-call-tests/macos)

> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).

In video calling implementations that demand high communication quality, pre-call detection helps identify and troubleshoot issues beforehand, ensuring seamless real-time interaction.

      
  
      
  
      
    This page shows you how to use Video SDK to run pre-call tests to identify and troubleshoot communication quality issues in your app.

    ## Understand the tech [#understand-the-tech-2]

    Pre-call testing typically covers two aspects:

    * **Equipment quality test**

      To test whether the local microphone, speaker, and camera are working properly, you run an echo test. The basic process of conducting an echo test is as follows:

      **Pre-call echo test**

    ![Echo Test](https://assets-docs.agora.io/images/video-sdk/pre-call-echo-test.svg)

    * **Network quality analysis**

      The quality of the last mile network affects the smoothness and clarity of the audio and video that the user sends and receives. Last mile refers to the last leg of communication network between the edge server of the Agora SDRTN® and the end-user devices. Network quality analysis enables you to get feedback on the available bandwidth, packet loss rate, network jitter, and round-trip delay of the upstream and downstream last-mile networks. The following figure shows the basic process of running a last-mile probe test.

      **Pre-call last mile test**

    ![Last Mile Test](https://assets-docs.agora.io/images/video-sdk/pre-call-last-mile-test.svg)

    <CalloutContainer type="info">
      <CalloutDescription>
        Best practice is to run the device test first and then perform a network test.
      </CalloutDescription>
    </CalloutContainer>

    ## Prerequisites [#prerequisites-2]

    Ensure that you have implemented the [SDK quickstart](../../index.mdx) project and that your app has obtained permissions to use the relevant devices.

    ## Implement pre-call testing [#implement-pre-call-testing-2]

    This section shows you how to implement pre-call testing in your project.

    ### Equipment quality test [#equipment-quality-test-2]

    The SDK provides the `startEchoTest(withConfig:)` method to test the network connection and whether the audio and video devices are working properly. Refer to the following steps to implement the device quality test:

    1. Before joining a channel, call `startEchoTest(withConfig:)` with `AgoraEchoTestConfiguration`. Specify the channel name, whether to test audio or video, and the time interval for the echo.

    2. After starting the test, face the camera and speak into the microphone. The user's audio or video is played back after a short delay. If the playback is normal, it means that the user's devices and upstream and downstream network are working normally.

    3. To stop the test, call `stopEchoTest`, and then call `joinChannel` to join a channel.
       <CalloutContainer type="info">
         <CalloutDescription>
           Using `startEchoTest(withConfig:)` \[3/3] to test an audio device and a video device at the same time may result in a brief audio-video desynchronization of the test results.
         </CalloutDescription>
       </CalloutContainer>

    To implement running an echo test in your app, refer to the following code:

    ```swift
    // Test audio device
    @IBAction func doAudioTest(sender: UIButton) {
     let testConfig = AgoraEchoTestConfiguration()
     // Set the interval for returning test results
     testConfig.intervalInSeconds = 10
     // Enable audio device test
     testConfig.enableAudio = true
     // Disable video device test
     testConfig.enableVideo = false
     // Identify each test by channel name
     testConfig.channelId = "AudioEchoTest" + "\(Int.random(in: 1...1000))"
     // Start the audio device test
     let ret = agoraKit.startEchoTest(withConfig: testConfig)
    }

    // Test video device
    @IBAction func doVideoTest(_ sender: UIButton) {
     let testConfig = AgoraEchoTestConfiguration()
     // Set the delay for video frame
     testConfig.intervalInSeconds = 2
     // Disable audio device test
     testConfig.enableAudio = false
     // Enable video device test
     testConfig.enableVideo = true
     // Identify each test by channel name
     testConfig.channelId = "VideoEchoTest" + "\(Int.random(in: 1...1000))"
     // Set the view for rendering local user's video
     testConfig.view = videoCanvasView
     // Start the video device test
     let ret = agoraKit.startEchoTest(withConfig: testConfig)
    }
    ```

    ### Network quality analysis [#network-quality-analysis-2]

    The SDK provides the `startLastmileProbeTest` method to probe the last-mile network quality before joining a channel. The method returns information about the network quality score and network statistics. Take the following steps to run a last-mile network quality probe test:

    1. Before joining a channel or switching user roles, call `startLastmileProbeTest` to start network test. Set the probe configuration and the expected maximum bitrate in `LastmileProbeConfig`.

    2. After calling this method, the SDK triggers the following callbacks:

       * `onLastmileQuality`: This callback is triggered two seconds after `startLastmileProbeTest` is called. It provides feedback on the upstream and downstream network quality through a subjective `quality` score.

       * `onLastmileProbeResult`: This callback is triggered 30 seconds after `startLastmileProbeTest` is called. It returns objective real-time network statistics, including `packetLossRate`, network `jitter`, and `availableBandwidth`.

    3. Call `stopLastmileProbeTest` to stop last-mile network testing.

    To implement network quality testing in your app, refer to the following code:

    ```swift
    // Create a network probe configuration object
    let config = AgoraLastmileProbeConfig()
    // Probe uplink network
    config.probeUplink = true;
    // Probe downlink network
    config.probeDownlink = true;
    // Expected uplink bitrate (bps). Value range is [100000,5000000]
    config.expectedUplinkBitrate = 100000;
    // Expected downlink bitrate (bps). Value range is [100000,5000000]
    config.expectedDownlinkBitrate = 100000;

    // Start the network quality probe
    agoraKit.startLastmileProbeTest(config)

    // Define callback functions to receive network quality statistics
    func rtcEngine(_ engine: AgoraRtcEngineKit, lastmileQuality quality: AgoraNetworkQuality) {
      lastmileResultLabel.text = "Quality: \(quality.description())"
    }
    func rtcEngine(_ engine: AgoraRtcEngineKit, lastmileProbeTest result: AgoraLastmileProbeResult) {
     // Round-Trip Time (RTT)
     let rtt = "Rtt: \(result.rtt)ms"
     // Downlink network bandwidth
     let downlinkBandWidth = "DownlinkAvailableBandwidth: \(result.downlinkReport.availableBandwidth)Kbps"
     // Downlink jitter buffer
     let downlinkJitter = "DownlinkJitter: \(result.downlinkReport.jitter)ms"
     // Downlink packet loss rate
     let downlinkLoss = "DownlinkLoss: \(result.downlinkReport.packetLossRate)%"
     // Uplink network bandwidth
     let uplinkBandwidth = "UplinkAvailableBandwidth: \(result.uplinkReport.availableBandwidth)Kbps"
     // Uplink jitter buffer
     let uplinkJitter = "UplinkJitter: \(result.uplinkReport.jitter)ms"
     // Uplink packet loss rate
     let uplinkLoss = "UplinkLoss: \(result.uplinkReport.packetLossRate)%"

     lastmileProbResultLabel.text = [rtt, downlinkBandWidth, downlinkJitter, downlinkLoss,uplinkBandwidth,uplinkJitter,uplinkLoss].joined(separator: "\n")
    }

    // Stop the network quality probe
    agoraKit.stopLastmileProbeTest()
    ```

    ## Reference [#reference-2]

    This section contains content that completes the information on this page, or points you to documentation that explains other aspects to this product.

    ### Troubleshooting device and network issues [#troubleshooting-device-and-network-issues-2]

    If you encounter problems while running pre-call tests, first ensure that you have implemented the API calls properly. To troubleshoot device and network issues, refer to the following table:

    | Problem                                                                           | Solution                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
    | :-------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
    | Can't hear sound when testing audio devices.                                      | * Check that the recording device and the playback device are working properly, and are not occupied by other programs.
    * Check that the network connection is normal.                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
    | Cannot see the screen when testing video devices.                                 | - Check that the video device is working properly and not occupied by other programs.
    - Check whether the network connection is normal.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
    | Poor uplink network quality detected (packet loss > 5%; network jitter > 100ms)   | * Check that the local network is working properly.
    * Ensure that the bitrate of the published audio and video streams does not exceed the available uplink bandwidth by reducing the resolution or lowering the frame rate.                                                                                                                                                                                                                                                                                                                                                                                            |
    | Poor downlink network quality detected (packet loss > 5%; network jitter > 100ms) | - Check that the local network is working properly.
    - Ensure that the total bandwidth of the local subscription does not exceed the available downstream bandwidth by:
      * Reducing the number of subscribed audio and video streams on the receiving end or reducing the bitrate of published audio and video streams on the sending end.
      * Enabling dual-stream mode on the sending side and requesting to receive small streams on the receiving side to reduce bandwidth consumption.
      * Enabling the video stream fallback function or the multiple streams by priority fallback function at the receiving end. |

    ### Sample project [#sample-project-2]

    Agora provides an open-source [PreCallTest](https://github.com/AgoraIO/API-Examples/blob/main/macOS/APIExample/Examples/Advanced/PrecallTest/PrecallTest.swift) sample project for your reference. Download and explore this project for a more detailed example.

    ### API reference [#api-reference-2]

    * [`startEchoTest(withConfig:)`](https://api-ref.agora.io/en/video-sdk/macos/4.x/documentation/agorartckit/agorartcenginekit/startechotest\(withconfig:\))
    * [`stopEchoTest()`](https://api-ref.agora.io/en/video-sdk/macos/4.x/documentation/agorartckit/agorartcenginekit/stopechotest\(\))
    * [`startLastmileProbeTest(_:)`](https://api-ref.agora.io/en/video-sdk/macos/4.x/documentation/agorartckit/agorartcenginekit/startlastmileprobetest\(_:\))
    * [`stopLastmileProbeTest()`](https://api-ref.agora.io/en/video-sdk/macos/4.x/documentation/agorartckit/agorartcenginekit/stoplastmileprobetest\(\))
    * [`rtcEngine(_:lastmileQuality:)`](https://api-ref.agora.io/en/video-sdk/macos/4.x/documentation/agorartckit/agorartcenginedelegate/rtcengine\(_\:lastmilequality:\))
    * [`rtcEngine(_:lastmileProbeTest:)`](https://api-ref.agora.io/en/video-sdk/macos/4.x/documentation/agorartckit/agorartcenginedelegate/rtcengine\(_\:lastmileprobetest:\))

    
  
      
  
      
  
      
  
      
  
      
  
      
  
      
  
      
  
