1"use strict";(self.webpackChunkswarmcloud_docs=self.webpackChunkswarmcloud_docs||[]).push([[2279],{1620:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>c,contentTitle:()=>s,default:()=>d,frontMatter:()=>p,metadata:()=>l,toc:()=>g});n(6540);var r=n(5680);function a(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(e,t){return t=null!=t?t:{},Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):function(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,r)}return n}(Object(t)).forEach(function(n){Object.defineProperty(e,n,Object.getOwnPropertyDescriptor(t,n))}),e}function i(e,t){if(null==e)return{};var n,r,a=function(e,t){if(null==e)return{};var n,r,a={},o=Object.keys(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||(a[n]=e[n]);return a}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(a[n]=e[n])}return a}const p={title:"API & Config",sidebar_position:3},s=void 0,l={unversionedId:"API",id:"API",title:"API & Config",description:"P2P Configuration",source:"@site/docs/rn/API.mdx",sourceDirName:".",slug:"/API",permalink:"/rn/API",draft:!1,tags:[],version:"current",sidebarPosition:3,frontMatter:{title:"API & Config",sidebar_position:3},sidebar:"tutorialSidebar",previous:{title:"Usage",permalink:"/rn/"}},c={},g=[{value:"P2P Configuration",id:"p2p-configuration",level:2},{value:"P2pEngine",id:"p2pengine",level:2},{value:"Switch Stream URL",id:"switch-stream-url",level:3},{value:"P2pEngine API",id:"p2pengine-api",level:3},{value:"P2P Statistics",id:"p2p-statistics",level:3},{value:"Dynamic m3u8 Path Support",id:"dynamic-m3u8-path-support",level:3},{value:"Setup HTTP headers",id:"setup-http-headers",level:3}],m={toc:g},u="wrapper";function d(e){var{components:t}=e,n=i(e,["components"]);return(0,r.yg)(u,o(function(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{},r=Object.keys(n);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(n).filter(function(e){return Object.getOwnPropertyDescriptor(n,e).enumerable}))),r.forEach(function(t){a(e,t,n[t])})}return e}({},m,n),{components:t,mdxType:"MDXLayout"}),(0,r.yg)("h2",{id:"p2p-configuration"},"P2P Configuration"),(0,r.yg)("p",null,"See ",(0,r.yg)("a",{parentName:"p",href:"/android/API"},"Android"),", ",(0,r.yg)("a",{parentName:"p",href:"/ios/API"},"iOS")," Configuration and ",(0,r.yg)("a",{parentName:"p",href:"https://github.com/swarm-cloud/react-native-swarmcloud/blob/main/src/index.tsx#L114"},"Type Definition")),(0,r.yg)("h2",{id:"p2pengine"},"P2pEngine"),(0,r.yg)("p",null,"Instantiate the P2pEngine singleton with a single call:"),(0,r.yg)("pre",null,(0,r.yg)("code",{parentName:"pre",className:"language-typescript"},"import { initP2pEngine } from 'react-native-swarmcloud';\ninitP2pEngine(token, config)\n")),(0,r.yg)("p",null,"Parameter description:"),(0,r.yg)("br",null),(0,r.yg)("table",null,(0,r.yg)("thead",{parentName:"table"},(0,r.yg)("tr",{parentName:"thead"},(0,r.yg)("th",{parentName:"tr",align:"center"},"param"),(0,r.yg)("th",{parentName:"tr",align:"center"},"type"),(0,r.yg)("th",{parentName:"tr",align:"center"},"required"),(0,r.yg)("th",{parentName:"tr",align:"center"},"description"))),(0,r.yg)("tbody",{parentName:"table"},(0,r.yg)("tr",{parentName:"tbody"},(0,r.yg)("td",{parentName:"tr",align:"center"},(0,r.yg)("strong",{parentName:"td"},(0,r.yg)("em",{parentName:"strong"},"token"))),(0,r.yg)("td",{parentName:"tr",align:"center"},"string"),(0,r.yg)("td",{parentName:"tr",align:"center"},"Yes"),(0,r.yg)("td",{parentName:"tr",align:"center"},"Token assigned by SwarmCloud")),(0,r.yg)("tr",{parentName:"tbody"},(0,r.yg)("td",{parentName:"tr",align:"center"},(0,r.yg)("strong",{parentName:"td"},(0,r.yg)("em",{parentName:"strong"},"config"))),(0,r.yg)("td",{parentName:"tr",align:"center"},"P2pConfig"),(0,r.yg)("td",{parentName:"tr",align:"center"},"Yes"),(0,r.yg)("td",{parentName:"tr",align:"center"},"Custom configuration")))),(0,r.yg)("h3",{id:"switch-stream-url"},"Switch Stream URL"),(0,r.yg)("p",null,"When switching to a new stream URL, pass the URL through ",(0,r.yg)("strong",{parentName:"p"},(0,r.yg)("em",{parentName:"strong"},"parseStreamURL"))," before passing it (m3u8) to the player:"),(0,r.yg)("pre",null,(0,r.yg)("code",{parentName:"pre",className:"language-typescript"},"import { parseStreamURL } from 'react-native-swarmcloud';\nconst parsedUrl = parseStreamURL(url);
1\n")),(0,r.yg)("h3",{id:"p2pengine-api"},"P2pEngine API"),(0,r.yg)("pre",null,(0,r.yg)("code",{parentName:"pre",className:"language-typescript"},"/// The version of SDK.\ngetSDKVersion(): string\n\n/// Create a new instance with token and the specified config.\ninitP2pEngine(token: string, config: P2pConfig): Promise<void>\n\n/// Get parsed local stream url by passing the original stream url(m3u8) .\nparseStreamURL(url: string, videoId: string | undefined): string;\n\n/// Set HTTP Headers while requesting ts and m3u8 dynamically.\nsetHttpHeadersForHls(headers: Object): void;\n\n/// Set HTTP Headers while requesting Dash files dynamically.\nsetHttpHeadersForDash(headers: Object): void;\n\n/// Get the connection state of p2p engine.\nisConnected(): boolean;\n\n/// Restart p2p engine.\nrestartP2p(): Promise<void>;\n\n/// Stop p2p and free used resources.\nstopP2p(): Promise<void>;\n\n/// Get the peer ID of p2p engine.\ngetPeerId(): string | undefined;\n")),(0,r.yg)("h3",{id:"p2p-statistics"},"P2P Statistics"),(0,r.yg)("p",null,"Please see ",(0,r.yg)("a",{parentName:"p",href:"https://github.com/swarm-cloud/react-native-swarmcloud/blob/main/example/src/Stats.tsx"},"example")),(0,r.yg)("admonition",{type:"note"},(0,r.yg)("p",{parentName:"admonition"},"The unit of download and upload is KB.")),(0,r.yg)("h3",{id:"dynamic-m3u8-path-support"},"Dynamic m3u8 Path Support"),(0,r.yg)("p",null,"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."),(0,r.yg)("pre",null,(0,r.yg)("code",{parentName:"pre",className:"language-typescript"},"const videoId = extractVideoIdFromUrl(originalUrl); /// extractVideoIdFromUrl is a function defined by yourself, you just need to extract video id from url\nconst url = parseStreamURL(originalUrl, videoId);\n")),(0,r.yg)("admonition",{type:"note"},(0,r.yg)("p",{parentName:"admonition"},"Interconnect with other platform should ensure that both have the same token and channelId.")),(0,r.yg)("h3",{id:"setup-http-headers"},"Setup HTTP headers"),(0,r.yg)("p",null,"Some HTTP requests need to add header information such as ",(0,r.yg)("strong",{parentName:"p"},(0,r.yg)("em",{parentName:"strong"},"referer"))," or ",(0,r.yg)("strong",{parentName:"p"},(0,r.yg)("em",{parentName:"strong"},"User-Agent"))," for Anti-Leech or statistical requirements. It can be set via ",(0,r.yg)("strong",{parentName:"p"},(0,r.yg)("em",{parentName:"strong"},"httpHeadersForHls"))," :"),(0,r.yg)("pre",null,(0,r.yg)("code",{parentName:"pre",className:"language-typescript"},'import { setHttpHeadersForHls } from \'react-native-swarmcloud\';\nsetHttpHeadersForHls({\n "referer": "XXX",\n "User-Agent": "XXX",\n});\n')))}d.isMDXComponent=!0},5680:(e,t,n)=>{n.d(t,{xA:()=>c,yg:()=>d});var r=n(6540);function a(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,r)}return n}function i(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?o(Object(n),!0).forEach(function(t){a(e,t,n[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):o(Object(n)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))})}return e}function p(e,t){if(null==e)return{};var n,r,a=function(e,t){if(null==e)return{};var n,r,a={},o=Object.keys(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||(a[n]=e[n]);return a}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(a[n]=e[n])}return a}var s=r.createContext({}),l=function(e){var t=r.useContext(s),n=t;return e&&(n="function"==typeof e?e(t):i(i({},t),e)),n},c=function(e){var t=l(e.components);return r.createElement(s.Provider,{value:t},e.children)},g="mdxType",m={inlineCode:"code",wrapper:function(e){var t=e.children;return r.createElement(r.Fragment,{},t)}},u=r.forwardRef(function(e,t){var n=e.components,a=e.mdxType,o=e.originalType,s=e.parentName,c=p(e,["components","mdxType","originalType","parentName"]),g=l(n),u=a,d=g["".concat(s,".").concat(u)]||g[u]||m[u]||o;return n?r.createElement(d,i(i({ref:t},c),{},{components:n}
1)):r.createElement(d,i({ref:t},c))});function d(e,t){var n=arguments,a=t&&t.mdxType;if("string"==typeof e||a){var o=n.length,i=new Array(o);i[0]=u;var p={};for(var s in t)hasOwnProperty.call(t,s)&&(p[s]=t[s]);p.originalType=e,p[g]="string"==typeof e?e:a,i[1]=p;for(var l=2;l<o;l++)i[l]=n[l];return r.createElement.apply(null,i)}return r.createElement.apply(null,n)}u.displayName="MDXCreateElement"}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.