Usage
# Environment Configuration
# Developing Environment
- Android Studio,download (opens new window)
# 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 (Only required by HLS, DASH and MP4)
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.cdnbye.**
-keep class com.cdnbye.**{*;}
-keep interface com.cdnbye.**{*;}
-dontwarn org.webrtc.**
-keep class org.webrtc.**{*;}
# Import SDK
# Download SDK
Download the latest version SDK (opens new window),then copy it to app/libs .
Download the WebRTC library with datachannel only webrtc.aar (opens new window),then copy it to app/libs .
# Modify build.gradle
Add the following code to your application's build.gradle app/build.gradle :
android {
repositories {
flatDir {
dirs 'libs'
}
}
}
Besides add new dependencies in app/build.gradle as shown below:
dependencies {
implementation 'com.alibaba:fastjson:1.2.58'
implementation 'com.orhanobut:logger:2.2.0'
implementation 'com.squareup.okhttp3:okhttp:3.12.13' // Or version 4.5+ to support message compression
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation(name:'webrtc', ext:'aar')
}
dependencies {
implementation 'com.alibaba:fastjson:1.2.58'
implementation 'org.java-websocket:Java-WebSocket:1.5.1' // Need latest version to avoid crash
implementation 'com.orhanobut:logger:2.2.0'
implementation 'com.squareup.okhttp3:okhttp:3.12.13' // Or version 4.5+ to support signal compression
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation(name:'webrtc', ext:'aar')
}
If you are publishing app to Google Play, you may be rejected because the WebRTC version is too old. Please replace webrtc.aar with another lib in app/build.gradle :
implementation 'com.dafruits:webrtc:106.0.0'
# Quick Start
We recommend calling P2pEngine.initEngine in the instance of the Application class right after the application is created.
# Import P2pEngine
import com.cdnbye.sdk.P2pEngine;
# Initialize P2pEngine
public class MyApplication extends android.app.Application {
@Override
public void onCreate() {
super.onCreate();
P2pConfig config = new P2pConfig.Builder()
.announceLocation(AnnounceLocation.Europe) // Set HongKong or USA if you changed zone
.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, ExoPlayer (opens new window) is highly recommended) instance, before passing it a URL, pass that URL through CDNBye P2P Engine.
private void onPlay(){
String parsedUrl = P2pEngine.getInstance().parseStreamUrl("https://your_stream");
mediaPlayer.play(parsedUrl);
}
# Setup Player Interactor For Exoplayer
If your are using Exoplayer, it's recommended to setup PlayerInteractor for live streaming.
P2pEngine.getInstance().setPlayerInteractor(new PlayerInteractor() {
public long onBufferedDuration() {
// Exoplayer in milliseconds
return player.getBufferedPosition() - player.getCurrentPosition();
}
});
# Demo
A completed example can be found here (opens new window)
# Troubleshooting Steps when P2P doesn't work
Click here