Skip to main content

Geofencing

When a user joins a channel, Video SDK automatically connects them to the closest geographical region of Agora SD-RTN™. However, to meet the laws and regulations of your region, you may want to filter in or filter out connections to a specific geographical region. Agora geofencing enables you to control and customize data routing in your app by specifying the Agora SD-RTN™ region users connect to.

Understand the tech

This section shows you how to enable geofencing in your app. The following figure shows the workflow you implement for geofencing:

Network Geofencing

Prerequisites

In order to follow this procedure you must have implemented the SDK quickstart project.

Project setup

In order to create the environment necessary to implement geofencing in your app, open the SDK quickstart Broadcast Streaming project you created previously.

Implement geofencing in your app

This section shows how to use the Video SDK to implement geofencing in your app.

To enable geofencing in your app, set the mAreaCode property of config to a region for geofencing. In /app/java/com.example.<projectname>/MainActivity, locate the setupVideoSDKEngine method and add the following code before config.mAppId = appId;:


_2
// Your app will only connect to Agora SD-RTN located in North America.
_2
config.mAreaCode = RtcEngineConfig.AreaCode.AREA_CODE_NA;

If your app fails to connect to the specified region of Agora SD-RTN™, instead of connection to another Agora SD-RTN™ region, Video SDK throws an error. If a firewall is deployed in your network environment, ensure that you:

  • Whitelist certain domains
  • Allow all IP addresses
  • Open the firewall ports defined in Use Cloud Proxy

Test your implementation

To ensure that you have implemented geofencing in your app:

  1. Generate a temporary token in Agora Console .

  2. In your browser, navigate to the Agora web demo and update App ID, Channel, and Token with the values for your temporary token, then click Join.

  1. In Android Studio, in app/java/com.example.<projectname>/MainActivity, update appId, channelName and token with the values for your temporary token.

  2. Connect a physical Android device to your development device.

  3. In Android Studio, click Run app. A moment later you see the project installed on your device. If this is the first time you run the project, you need to grant microphone and camera access to your app.

  1. Select an option and click Join to start a session.

    • When you join as a Host, the local video is published and played in the app.
    • When you join as Audience, the remote stream is subscribed and played. You see your app to the specified region Agora SD-RTN™ and opens a channel.
  1. Try excluding a region of Agora SD-RTN™.

    In /app/java/com.example.<projectname>/MainActivity, locate the setupVideoSDKEngine method and replace config.mAreaCode = RtcEngineConfig.AreaCode.AREA_CODE_NA with the following code:


    _2
    // Exclude Mainland China from the regions for connection
    _2
    config.mAreaCode = AREA_CODE_GLOB ^ AREA_CODE_CN

Reference

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

  • When the user's location is different from the specified region, the public network spanning between the user's location and the specified region might have a poor quality, which can make the audio and video experience less than optimal.
  • To specify multiple regions for the connection, you can perform bitwise operations on the area codes. To include a region, use the | operator, to exclude a region use ^.

Geofencing area codes

The supported area codes are:

  • AREA_CODE_GLOB: Global (Default)
  • AREA_CODE_CN: Mainland China
  • AREA_CODE_NA: North America
  • AREA_CODE_EU: Europe
  • AREA_CODE_AS: Asia, excluding Mainland China
  • AREA_CODE_JP: Japan
  • AREA_CODE_IN: India

The area codes support bitwise operation.

API reference