Skip to main content

Usage

Environment Configuration

Development Environment

Enable Java 8 Support

If not already enabled, turn on Java 8 support in your application's build.gradle file by adding the following to the android section:

android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

Add Required Permissions

Add the following permissions in app/src/main/AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Allow Localhost Connections

Update the AndroidManifest.xml file:

<application
...
android:networkSecurityConfig="@xml/network_security_config"
...
/>

Then create or update the res/xml/network_security_config.xml file to allow localhost connections:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">127.0.0.1</domain>
</domain-config>
</network-security-config>

Enable LargeHeap

P2P-related operations can be memory-intensive. We recommend setting the optional android:largeHeap="true" flag in the AndroidManifest.xml file to ensure your application has sufficient heap memory allocated.

<application
...
android:largeHeap="true"
...
/>

Proguard Configuration

Add the following rules to proguard-rules.pro:

-dontwarn com.p2pengine.**
-keep class com.p2pengine.**{*;}
-keep interface com.p2pengine.**{*;}
-keep class com.cdnbye.libdc.**{*;}
-keep interface com.cdnbye.libdc.**{*;}
-keep class com.snapchat.djinni.**{*;}

Import SDK

To integrate the SDK, first set up the SwarmCloud Maven repository in the project's build.gradle file:

allprojects {
repositories {
// ... other repositories
maven {
url 'https://maven.swarmcloud.net/repository/maven-releases/'
}
}
}

Then add the following dependencies inside the dependencies section of the module's build.gradle file:

dependencies {
implementation("com.squareup.okhttp3:okhttp:3.12.13") // Or any okhttp newer version
implementation("com.orhanobut:logger:2.2.0")
implementation("com.google.code.gson:gson:2.9.0")
implementation("com.swarmcloud:datachannel_native:latest.release")
implementation("com.swarmcloud:p2p_engine:latest.release")
}
tip

The emulator may not behave as expected — please test on a physical device.

Quick Start

We recommend calling P2pEngine.init inside the onCreate method of your Application class, immediately after the application is created.

Import P2pEngine

import com.p2pengine.sdk.P2pEngine

Initialize P2pEngine

class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
val config = P2pConfig.Builder()
.trackerZone(TrackerZone.Europe) // Set HongKong or USA if you changed zone
.build()
P2pEngine.init(this, YOUR_TOKEN, config)
}
}

YOUR_TOKEN represents your Customer ID. Replace it with the token obtained from your console; click here for more information.

Playback Address

When initializing a media player (or any other video player) instance, pass the playback URL through the P2P Engine before handing it to the player.

private void onPlay(){
val parsedUrl = P2pEngine.instance!!.parseStreamUrl(YOUR_STREAM)
mediaPlayer.play(parsedUrl)
}

Setup Player Interactor For Exoplayer

If you're using ExoPlayer, we recommend setting up PlayerInteractor for live streaming.

P2pEngine.instance?.setPlayerInteractor(object : PlayerInteractor() {
override fun onBufferedDuration(): Long {
// Exoplayer in milliseconds
return if (player != null) {
player!!.bufferedPosition - player!!.currentPosition
} else {
-1
}
}
})

Demo

A complete example project can be found here

Troubleshooting Steps when P2P doesn't work

See here for details.