On HLS Live streaming or mp4 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 setPlayerInteractor .
import com.p2p.core.p2p.PlayerInteractor;
P2pEngine.getInstance().setPlayerInteractor(new PlayerInteractor() {
@Override
public long onBufferedDuration() {
// Exoplayer in milliseconds
return player.getBufferedPosition() - player.getCurrentPosition();
}
});
Dynamic Url 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 urls play the same live/vod but have different paths on them. For example, example.com/live/channel-live1/playlist.m3u8 and example.com/live/channel-live2/playlist.m3u8. In this case, you can format a common channelId for them.
// 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.init(getApplicationContext(), token, config);
Interconnect with other platform should ensure that both have the same channelIdPrefix and channelId.
HLS & DASH & MP4
// If ts address in m3u8 is relative, you need to set the segmentid
P2pEngine.getInstance().setHlsSegmentIdGenerator(new HlsSegmentIdGenerator() {
@Override
public String onSegmentId(String streamId, long sn, String segmentUrl, String range) {
return return "" + 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);
File download
String fileId = extractFileIdFromUrl(urlString); // extractFileIdFromUrl is a function defined by yourself, you just need to extract file id from url
P2pEngine.getInstance().downloadFile(url, url, fileId);
Dynamic Media File Path Issue
Like dynamic m3u8/mpd path issue, you should format a common segmentId for the same media file. You can override the segment ID like this:
/*
streamId: The id of stream
sn: The serial number of segment
segmentUrl: The url of segment
range: Byte range of segment
*/
// For HLS
P2pEngine.getInstance().setHlsSegmentIdGenerator(new HlsSegmentIdGenerator() {
@Override
public String onSegmentId(String streamId, long sn, String segmentUrl, String range) {
return extractIdFromSegment(segmentUrl, range);
}
});
// For DASH
P2pEngine.getInstance().setDashSegmentIdGenerator(new DashSegmentIdGenerator() {
@Override
public String onSegmentId(String streamId, String segmentUrl, String range) {
return extractIdFromSegment(segmentUrl, range);
}
});
Setup HTTP headers
Some HTTP requests need to add header information such as User-Agent for Anti-Leech or statistical requirements. It can be set via setHttpHeaders :
Map headers = new HashMap();
headers.put("User-Agent", "XXX");
engine.setHttpHeadersForHls(headers); // For hls playback
// engine.setHttpHeadersForDash(headers); // For dash playback
// engine.setHttpHeadersForMp4(headers); // For mp4 playback
// engine.setHttpHeadersForFile(headers); // For file download
P2P First Strategy
Since it takes time to establish a P2P connection, the first few pieces are downloaded using HTTP by default. Giving the peers extra time to download initial segments will increase the p2p ratio. However, it may cause delay. It is recommended to turn on the hot channel.
P2pConfig config = new P2pConfig.Builder()
.waitForPeer(true)
.waitForPeerTimeout(4500) // Http will be forcibly enabled if there is no peers or waitForPeerTimeout is timed out
.build();