API & Config
P2P Configuration
See Android, iOS Configuration and Type Definition
P2pEngine
Instantiate P2pEngine, just need to call once:
import { initP2pEngine } from 'react-native-swarmcloud';
initP2pEngine(token, config)
Explanation:
param | type | required | description |
---|---|---|---|
token | string | Yes | Token assigned by SwarmCloud |
config | P2pConfig | Yes | 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 parseStreamURL :
import { parseStreamURL } from 'react-native-swarmcloud';
const parsedUrl = parseStreamURL(url);
P2pEngine API
/// The version of SDK.
getSDKVersion(): string
/// Create a new instance with token and the specified config.
initP2pEngine(token: string, config: P2pConfig): Promise<void>
/// Get parsed local stream url by passing the original stream url(m3u8) .
parseStreamURL(url: string, videoId: string | undefined): string;
/// Set HTTP Headers while requesting ts and m3u8 dynamically.
setHttpHeadersForHls(headers: Object): void;
/// Set HTTP Headers while requesting Dash files dynamically.
setHttpHeadersForDash(headers: Object): void;
/// Get the connection state of p2p engine.
isConnected(): boolean;
/// Restart p2p engine.
restartP2p(): Promise<void>;
/// Stop p2p and free used resources.
stopP2p(): Promise<void>;
/// Get the peer ID of p2p engine.
getPeerId(): string | undefined;
P2P Statistics
Please see example
The unit of download and upload is KB.
Dynamic m3u8 Path Support
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.
const videoId = extractVideoIdFromUrl(originalUrl); /// extractVideoIdFromUrl is a function defined by yourself, you just need to extract video id from url
const url = parseStreamURL(originalUrl, videoId);
Interconnect with other platform should ensure that both have the same token and channelId.
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 httpHeadersForHls :
import { setHttpHeadersForHls } from 'react-native-swarmcloud';
setHttpHeadersForHls({
"referer": "XXX",
"User-Agent": "XXX",
});