P2P Streaming Engine

vuePress-theme-reco    2018 - 2021
P2P Streaming Engine P2P Streaming Engine

Choose mode

  • dark
  • auto
  • light
Documents
  • Introduction
  • Web SDK

    • Hls.js
    • Dash.js
    • Shaka-Player
    • MP4
    • Downloader
  • Android SDK
  • iOS SDK
  • Flutter SDK
Pricing
Contact Us
Partnership
Console
GitHub
语言
  • English
  • 简体中文

Documents
  • Introduction
  • Web SDK

    • Hls.js
    • Dash.js
    • Shaka-Player
    • MP4
    • Downloader
  • Android SDK
  • iOS SDK
  • Flutter SDK
Pricing
Contact Us
Partnership
Console
GitHub
语言
  • English
  • 简体中文
  • Introduction
  • FAQ
  • Tracking Service
  • Signaling Service
  • P2P Optimization
  • Console

    • Domain/AppId Binding
    • Data Analysis
    • P2P Control
    • Restful API
  • Hls.js SDK

    • Introduction
    • Usage
    • Player Integration
    • API & Config
    • CDN
    • Change Log
  • Android SDK

    • Introduction
    • Usage
    • API & Config
    • Change Log
  • iOS SDK

    • Introduction
    • Usage
    • API & Config
    • Change Log
  • Flutter SDK

    • Introduction
    • Usage
    • API & Config
    • Change Log
  • Shaka-Player SDK

    • Introduction
    • Usage
    • Player Integration
    • API & Config
    • Change Log
  • Web MP4 SDK

    • Introduction
    • Usage
    • Player Integration
    • API & Config
    • Change Log
  • Dash.js SDK

    • Introduction
    • Usage
    • Player Integration
    • API & Config
    • Change Log
  • Web Downloader

    • Introduction
    • Usage
    • API & Config
    • Change Log
  • Android SDK 1.x

    • Introduction
    • Usage
    • API & Config
      • P2P Configuration
      • P2pEngine
      • Advanced Usage
    • Change Log
  • More

    • Design

API & Config

vuePress-theme-reco    2018 - 2021

API & Config


# P2P Configuration

A P2pConfig can be obtained via its builder, the parameters below is the default values:

P2pConfig config = new P2pConfig.Builder()
    .logEnabled(false)                                // Enable or disable log
    .logLevel(LogLevel.WARN)                          // Print log level
    .announce("https://tracker.cdnbye.com/v1")        // The address of tracker server
    .wsSignalerAddr("wss://signal.cdnbye.com")        // The address of signal server
    .downloadTimeout(15_000, TimeUnit.MILLISECONDS)   // TS file download timeout by HTTP
    .dcDownloadTimeout(6_000, TimeUnit.MILLISECONDS)  // Max download timeout for WebRTC datachannel
    .localPort(0)                                     // The port for local http server(default is random port)
    .diskCacheLimit(1024*1024*1024)                   // The max size of binary data that can be stored in the disk cache for VOD(Set to 0 will disable disk cache)
    .memoryCacheCountLimit(30)                        // The max count of ts files that can be stored in the memory cache
    .p2pEnabled(true)                                 // Enable or disable p2p engine
    .wifiOnly(false)                                  // Only allow uploading on Wi-Fi and Ethernet.
    .withTag("unknown")                               // User defined tag which is presented in console
    .webRTCConfig(null)                               // Providing options to configure WebRTC connections
    .maxPeerConnections(15)                           // Max peer connections at the same time
    .useHttpRange(true)                               // Use HTTP ranges requests where it is possible. Allows to continue (and not start over) aborted P2P downloads over HTTP
    .httpHeaders(null)                                // Set HTTP Headers while requesting ts and m3u8. 
    .channelIdPrefix(null)                            // Required while using customized channelId(5 < length < 15), recommended to set it as the unique identifier of your organization
    .isSetTopBox(false)                               // Set it as true if SDK is running on set-top box
    .fastStartup(true)                                // The first several TS request directly from the server instead of the proxy server to improve the starting speed
    .playlistMaxRetries(2)                            // Max retries when p2p engine request m3u8 or mpd failed
    .build();  
P2pEngine.initEngine(getApplicationContext(), token, config);

# P2pEngine

Instantiate P2pEngine,which is a singleton:

P2pEngine engine = P2pEngine.initEngine(Context context, String token, P2pConfig config);

Explanation:

param type required description
context Context Yes The instance of Application Context is recommended.
token String Yes Token assigned by CDNBye.
config P2pConfig No Custom configuration.

# Switch Stream URL

When Switching to a new stream URL, before passing new stream url(m3u8) to the player, pass that URL through P2pEngine instance:

String newParsedURL = P2pEngine.getInstance().parseStreamUrl(url);

# P2pEngine API

# P2pEngine.Version

Current SDK version.

# P2pEngine.protocolVersion

Get the version of P2P protocol,only have the same protocol version as another platform can both interconnect with each other.

# P2pEngine.getInstance()

Get the singleton of P2pEngine.

# engine.parseStreamUrl(String url)

Convert original playback address (m3u8) to the address of the local proxy server.

# engine.parseStreamUrl(String url, String videoId)

Convert original playback address (m3u8) to the address of the local proxy server, and pass video ID for making channel ID.

# engine.isConnected()

Check if connected with CDNBye backend.

# engine.stopP2p()

Disable engine to stop p2p and free used resources.

# engine.restartP2p()

Resume P2P if it has been stopped.

# engine.getPeerId()

Get the peer ID of this engine.

# engine.setHttpHeaders(Map<String, String> headers)

Set HTTP Headers while requesting ts and m3u8 dynamically.

# engine.shutdown()

Stop P2P and shut down the proxy server.

# P2P Statistics

Add a observer P2pStatisticsListener to observe downloading statistics:

engine.addP2pStatisticsListener(new P2pStatisticsListener() {
            @Override
            public void onHttpDownloaded(long value) {
            }

            @Override
            public void onP2pDownloaded(long value) {
            }

            @Override
            public void onP2pUploaded(long value) {
            }

            @Override
            public void onPeers(List<String> peers) {
            }
            
            @Override
            public void onServerConnected(boolean connected) {
            }
        });

The unit of download and upload is KB.

# Advanced Usage

# Improve Live Streaming Performance

On Live streaming, to improve performance, we recommend to tell p2p engine the duration from the playback time to the end of the buffered interval. In order to do so, you need to use callback setPlayStats .

import com.cdnbye.sdk.PlayerStatsCallback;

P2pEngine.getInstance().setPlayStats(new PlayerStatsCallback() {
    @Override
    public long onBufferedDuration() {
        // Exoplayer in milliseconds
        return player.getBufferedPosition() - player.getCurrentPosition();
    }
});

# Dynamic m3u8 Path Issue

The channelId is an identifier used by our backend to match peers that are watching the same content. It is an optional parameter, and by default, we generate channelId from the content URL by removing any query parameters and protocol from it. Some m3u8 urls play the same live/vod but have different paths on them. For example, example.com/clientId1/streamId.m3u8 and example.com/clientId2/streamId.m3u8. In this case, you can format a common channelId for them.

  • For SDK version >= 1.10
// Set channelIdPrefix in p2pConfig before setting channelId! Connectivity with other platform should have the same channelIdPrefix.
P2pConfig config = new P2pConfig.Builder() 
    .channelIdPrefix(YOUR_UNIQUE_ID)                           
    .build();  
P2pEngine.initEngine(getApplicationContext(), token, config);
// If ts address in m3u8 is relative, you need to set the segmentid
P2pEngine.getInstance().setSegmentId(new SegmentIdCallback() {
    @Override
    public String onSegmentId(int level, long sn, String segmentUrl) {
        return String.format("%d-%d", level, sn);
    }
});
String videoId = extractVideoIdFromUrl(urlString);     // extractVideoIdFromUrl is a function defined by yourself, you just need to extract video id from url
String parsedUrl = P2pEngine.getInstance().parseStreamUrl(urlString, videoId);
  • For SDK version < 1.10
import com.cdnbye.sdk.ChannelIdCallback;

// Set channelIdPrefix in p2pConfig before setting channelId! Connectivity with other platform should have the same channelIdPrefix.
P2pConfig config = new P2pConfig.Builder() 
    .channelIdPrefix(YOUR_UNIQUE_ID)                           
    .build();  
P2pEngine.initEngine(getApplicationContext(), token, config);
// If ts address in m3u8 is relative, you need to set the segmentid
P2pEngine.getInstance().setSegmentId(new SegmentIdCallback() {
    @Override
    public String onSegmentId(int level, long sn, String segmentUrl) {
        return String.format("%d-%d", level, sn);
    }
});
P2pEngine.getInstance().setChannelId(new ChannelIdCallback() {
    @Override
    public String onChannelId(String urlString) {
        return extractVideoIdFromUrl(urlString);      // extractVideoIdFromUrl is a function defined by yourself, you just need to extract video id from url and return
    }
});

Interconnect with other platform should ensure that both have the same channelIdPrefix and channelId.

# Dynamic ts path issue

Like dynamic m3u8 path issue, you should format a common segmentId for the same ts file. You can override the segment ID like this:

/*
    level: The quality level
    sn: The serial number of segment
    segmentUrl: The url of segment
*/
engine.setSegmentId(new SegmentIdCallback() {
    @Override
    public String onSegmentId(int level, long sn, String segmentUrl) {
        return format(segmentUrl);
    }
});

# Setup HTTP headers

Some HTTP requests need to add header information such as referer or User-Agent for Anti-Leech or statistical requirements. It can be set via setHttpHeaders :

Map headers = new HashMap();
headers.put("referer", "XXX");
headers.put("User-Agent", "XXX");
engine.setHttpHeaders(headers);

# Switch Signal Address

Sometimes you need to change signal address in runtime to avoid overload of signal server:

P2pConfig config = new P2pConfig.Builder()
        .wsSignalerAddr("wss://yoursignal2.com")
        .build();
P2pEngine.getInstance().setConfig(config);

Please note that it will reset all fields of config in P2pEngine.

# Use Your Own STUN or TURN Server

STUN (Session Traversal Utilities for NAT) allows clients to discover their public IP address and the type of NAT they are behind. This information is used to establish the media connection. Although there are default STUN servers in this SDK, you can replace them with your own via P2PConfig. TURN (Traversal Using Relays around NAT) server is used to relay traffic if direct connection fails. You can config your TURN server in the same way as STUN.

import org.webrtc.PeerConnection;
import org.webrtc.PeerConnection.RTCConfiguration;

List<PeerConnection.IceServer> iceServers = new ArrayList<>();
iceServers.add(PeerConnection.IceServer.builder(YOUR_STUN_OR_TURN_SERVER).createIceServer());
RTCConfiguration rtcConfig = new RTCConfiguration(iceServers);
P2pConfig config = new P2pConfig.Builder()
    .webRTCConfig(rtcConfig)
    .build();