Usage
Environment Configuration
Development Environment
- Android Studio, download
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
- By Mavel
- By Local File
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")
}
Download the latest SDK release (swarmcloud.jar and datachannel.aar), then copy the files into app/libs .
Also add the following dependencies to the module's build.gradle file:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'])
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")
}
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
- kotlin
- java
import com.p2pengine.sdk.P2pEngine
import com.p2pengine.sdk.P2pEngine;
Initialize P2pEngine
- kotlin
- java
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)
}
}
class MyApplication extends android.app.Application {
protected void onCreate() {
super.onCreate();
P2pConfig config = new P2pConfig.Builder()
.trackerZone(TrackerZone.Europe) // Set HongKong or USA if you changed zone
.insertTimeOffsetTag(0.0)
.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.
- kotlin
- java
private void onPlay(){
val parsedUrl = P2pEngine.instance!!.parseStreamUrl(YOUR_STREAM)
mediaPlayer.play(parsedUrl)
}
private void onPlay(){
String parsedUrl = P2pEngine.getInstance().parseStreamUrl(YOUR_STREAM);
mediaPlayer.play(parsedUrl);
}
Setup Player Interactor For Exoplayer
If you're using ExoPlayer, we recommend setting up PlayerInteractor for live streaming.
- kotlin
- java
P2pEngine.instance?.setPlayerInteractor(object : PlayerInteractor() {
override fun onBufferedDuration(): Long {
// Exoplayer in milliseconds
return if (player != null) {
player!!.bufferedPosition - player!!.currentPosition
} else {
-1
}
}
})
P2pEngine.getInstance().setPlayerInteractor(new PlayerInteractor() {
public long onBufferedDuration() {
// Exoplayer in milliseconds
if (play != null) {
return player.getBufferedPosition() - player.getCurrentPosition();
}
return -1;
}
});
Demo
A complete example project can be found here
Troubleshooting Steps when P2P doesn't work
See here for details.