UNPKG

cione-comp-lib

Version:

`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`

2 lines (1 loc) 2.84 kB
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}}),exports.SrsRtcPlayerAsync=function(){var e={play:async function(t){var r=e.__internal.prepareUrl(t);e.pc.addTransceiver("audio",{direction:"recvonly"}),e.pc.addTransceiver("video",{direction:"recvonly"});var a=await e.pc.createOffer();await e.pc.setLocalDescription(a);var n=await new Promise((function(e,t){var n={api:r.apiUrl,tid:r.tid,streamurl:r.streamUrl,clientip:null,sdp:a.sdp};fetch(r.apiUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)}).then((e=>e.json())).then((r=>{r.code?t(r):e(r)})).catch((e=>{t(e)}))}));return await e.pc.setRemoteDescription(new RTCSessionDescription({type:"answer",sdp:n.sdp})),n.simulator=r.schema+"//"+r.urlObject.server+":"+r.port+"/rtc/v1/nack/",n},close:function(){e.pc&&e.pc.close(),e.pc=null},ontrack:function(t){e.stream.addTrack(t.track)}};return e.__internal={defaultPath:"/rtc/v1/play/",prepareUrl:function(t){var r=e.__internal.parse(t),a=r.user_query.schema;a=a?a+":":window.location.protocol;var n=r.port||1985;"https:"===a&&(n=r.port||443);var s=r.user_query.play||e.__internal.defaultPath;for(var i in s.lastIndexOf("/")!==s.length-1&&(s+="/"),o=a+"//"+r.server+":"+n+s,r.user_query)"api"!==i&&"play"!==i&&(o+="&"+i+"="+r.user_query[i]);var o=o.replace(s+"&",s+"?");return{apiUrl:o,streamUrl:r.url,schema:a,urlObject:r,port:n,tid:Number(parseInt((new Date).getTime()*Math.random()*100)).toString(16).substr(0,7)}},parse:function(t){var r=document.createElement("a");r.href=t.replace("rtmp://","http://").replace("webrtc://","http://").replace("rtc://","http://");var a=r.hostname,n=r.pathname.substr(1,r.pathname.lastIndexOf("/")-1),s=r.pathname.substr(r.pathname.lastIndexOf("/")+1);if((n=n.replace("...vhost...","?vhost=")).indexOf("?")>=0){var i=n.substr(n.indexOf("?"));n=n.substr(0,n.indexOf("?")),i.indexOf("vhost=")>0&&(a=i.substr(i.indexOf("vhost=")+"vhost=".length)).indexOf("&")>0&&(a=a.substr(0,a.indexOf("&")))}if(r.hostname===a){/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/.test(r.hostname)&&(a="__defaultVhost__")}var o="rtmp";t.indexOf("://")>0&&(o=t.substr(0,t.indexOf("://")));var p=r.port;p||("http"===o?p=80:"https"===o?p=443:"rtmp"===o&&(p=1935));var c={url:t,schema:o,server:r.hostname,port:p,vhost:a,app:n,stream:s};return e.__internal.fill_query(r.search,c),c.port||"webrtc"!==o&&"rtc"!==o||("https"===c.user_query.schema||0===window.location.href.indexOf("https://")?c.port=443:c.port=1985),c},fill_query:function(e,t){if(t.user_query={},0!==e.length){e.indexOf("?")>=0&&(e=e.split("?")[1]);for(var r=e.split("&"),a=0;a<r.length;a++){var n=r[a].split("=");t[n[0]]=n[1],t.user_query[n[0]]=n[1]}t.domain&&(t.vhost=t.domain)}}},e.pc=new RTCPeerConnection(null),e.stream=new MediaStream,e.pc.ontrack=function(t){e.ontrack&&e.ontrack(t)},e};