Usage
Environment Configuration
Developing Environment
- Android Studio,download
Turn on Java 8 support
If not enabled already, you also need to turn on Java 8 support in application's build.gradle, by adding the following to the android section:
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
Add Uses Permission
Add relevant uses 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 HTTP Request
Starting with Android 9 (API level 28), cleartext support is disabled by default. There are 2 solutions:
(1) set ***targetSdkVersion*** under 27
(2) Add the attribute below in ***app/src/main/AndroidManifest.xml*** to indicate that the app intends to use cleartext HTTP:
<application
...
android:usesCleartextTraffic="true"
...
/>
Enable LargeHeap
P2P related application can be memory intensive, setting the optional android:largeHeap="true" flag in the AndroidManifest.xml file is recommended to ensure that your application has enough heap memory allocated.
<application
...
android:largeHeap="true"
...
/>
Proguard Configuration
Please add the following code in 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 sentence 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.cdnbye:datachannel_native:1.0.211")
implementation("com.cdnbye:p2p_engine:latest.release")
}
Download the latest version SDK swarmcloud.jar and datachannel.aar,then copy it to app/libs .
Besides add new dependencies in module's build.gradle file as shown below:
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")
}
Simulator may not work as expected, please test on real device.
Quick Start
We recommend calling P2pEngine.initEngine in the instance of the Application class right 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
.insertTimeOffsetTag(0.0)
.isSetTopBox(false) // Set it as true if SDK is running on set-top box
.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)
.isSetTopBox(false) // Set it as true if SDK is running on set-top box
.build();
P2pEngine.init(this, YOUR_TOKEN, config);
}
}
Where YOUR_TOKEN is your Customer ID. Please replace it by your own token obtained from console, click here for more information.
Playback Address
When initializing a media player (or any other video player) instance, before passing it a URL, pass that URL through P2P Engine.
- 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 your are using Exoplayer, it's recommended to setup 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 completed example can be found here
Migrating from v2
Some major changes and enhancements of v3:
- The package name is renamed from com.cdnbye. to com.p2pengine.
- The minimum requirement of API level is now 21+ instead of 19+
- The requirement of okhttp version is 4.5+
- One of the gradle dependencies
fastjson
is replaced bygson
:
// implementation 'com.alibaba:fastjson:1.2.58' // Remove this dependency
implementation("com.google.code.gson:gson:2.9.0") // Add this dependency
- The APIs for Mp4/MPEG-Dash/FIle download are removed
- Some fields of P2pConfig are removed, like "waitForPeer", "waitForPeerTimeout"
Troubleshooting Steps when P2P doesn't work
Click here