UNPKG

avcon-sdk-srs

Version:

Avcon SRS SDK

2 lines (1 loc) 7.2 kB
(function(s,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(s=typeof globalThis<"u"?globalThis:s||self,r(s["avcon-srs-sdk"]={}))})(this,function(s){"use strict";var R=Object.defineProperty;var A=(s,r,h)=>r in s?R(s,r,{enumerable:!0,configurable:!0,writable:!0,value:h}):s[r]=h;var d=(s,r,h)=>A(s,typeof r!="symbol"?r+"":r,h);const r={code:-1,message:"Invalid JSON response"};function h(t){if(!t.ok)throw t;return t.json().then(e=>{if(e.code)throw e;return e})}function m(t,e){return fetch(t.apiUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({api:t.apiUrl,tid:t.tid,streamurl:t.streamUrl,sdp:e.sdp,clientip:null})}).then(h).catch(i=>{if(i instanceof SyntaxError)return r;throw i})}function v(t,e){const{schema:i,httpApiPort:a,httpsApiPort:c}=C.getConfig(),n=new URL(t.replace(/^(rtmp|webrtc|rtc)/,i)),l=n.port||n.protocol==="https"?c:a,b=e.endsWith("/")?e:e+"/";return{apiUrl:`${n.protocol}//${n.hostname}:${l}${b}`,streamUrl:t,tid:(new Date().getTime()*Math.random()*100).toString(16).slice(0,7)}}class u extends Error{constructor(e,i){super(),this.name=e,this.message=i,this.stack=new Error().stack}}var g=(t=>(t.Normal="normal",t.Screen="screen",t))(g||{});async function p(t={audio:!0,video:!0},e="normal"){if(!navigator.mediaDevices&&window.location.protocol==="http:"&&window.location.hostname!=="localhost")throw new u("HttpsRequiredError","Please use HTTPS or localhost to publish, read https://github.com/ossrs/srs/issues/2762#issuecomment-983147576");const i=e==="normal"?await navigator.mediaDevices.getUserMedia(t):await navigator.mediaDevices.getDisplayMedia(t),a=i.getAudioTracks()[0],c=i.getVideoTracks()[0];return{audioTrack:a,videoTrack:c,stream:i}}var o=(t=>(t[t.Pendding=1]="Pendding",t[t.Connecting=2]="Connecting",t[t.Connected=3]="Connected",t[t.Disconnected=4]="Disconnected",t))(o||{});class T{constructor(e){d(this,"constraints");d(this,"status",o.Pendding);d(this,"RTCPeerConnection",new RTCPeerConnection);d(this,"stream",new MediaStream);d(this,"audioSender",null);d(this,"videoSender",null);this.constraints=e||{audio:!0,video:!0}}init(){this.status=o.Pendding,this.stream=new MediaStream,this.RTCPeerConnection=new RTCPeerConnection,this.audioSender=null,this.videoSender=null}close(){this.status=o.Disconnected,this.stream.getTracks().forEach(e=>e.stop()),this.RTCPeerConnection&&this.RTCPeerConnection.close(),this.RTCPeerConnection=null,this.audioSender=null,this.videoSender=null}setConstraints(e){this.constraints=e}async getWebRTCStats(e){if(this.RTCPeerConnection)try{return await this.RTCPeerConnection.getStats(e)}catch(i){console.error("获取WebRTC数据统计失败:",i)}}getConstraints(){return this.constraints}getStatus(){return this.status}setVideoContentHind(e,i){return"contentHint"in e?(e.contentHint=i,e.contentHint!==i?(console.warn("设置contentHint失败",i),!1):!0):(console.warn("浏览器不支持contentHint,不设置contentHint"),!1)}async setRTCDescription(e,i){if(this.RTCPeerConnection){this.status=o.Connecting;const a=await this.RTCPeerConnection.createOffer();await this.RTCPeerConnection.setLocalDescription(a);const c=v(e,i);try{const n=await m(c,a);return await this.RTCPeerConnection.setRemoteDescription({type:"answer",sdp:n.sdp}),this.status=o.Connected,n}catch{throw new u("Avcon SRS Sdk Publish Error","信令获取失败")}}}}class w extends T{constructor(e){super(e)}async publish(e,i,a="detail"){if(!(this.status===o.Pendding||this.status===o.Disconnected))throw new u("Avcon SRS Sdk Error","当前SrsPublisher实例处于连接状态,请先调用close()方法关闭连接,再重新调用publish()方法");i&&this.setConstraints(i),this.RTCPeerConnection||this.init();const{stream:c,audioTrack:n,videoTrack:l}=await p(this.constraints);return l&&(this.RTCPeerConnection.addTransceiver("video",{direction:"sendonly"}),this.setVideoContentHind(l,a),this.stream.addTrack(l),this.videoSender=this.RTCPeerConnection.addTrack(l,this.stream)),n&&(this.RTCPeerConnection.addTransceiver("audio",{direction:"sendonly"}),this.stream.addTrack(n),this.audioSender=this.RTCPeerConnection.addTrack(n,this.stream)),await this.setRTCDescription(e,C.getConfig().publishApi),c}closeAudio(){this.stream.getAudioTracks()[0]&&(this.stream.getAudioTracks()[0].enabled=!1)}closeVideo(){this.stream.getVideoTracks()[0]&&(this.stream.getVideoTracks()[0].enabled=!1)}openAudio(){this.stream.getAudioTracks()[0]&&(this.stream.getAudioTracks()[0].enabled=!0)}openVideo(){this.stream.getVideoTracks()[0]&&(this.stream.getVideoTracks()[0].enabled=!0)}async changeAudioTrack(e){const i=this.stream.getAudioTracks()[0];i&&(i.stop(),this.stream.removeTrack(i)),this.stream.addTrack(e),this.audioSender&&await this.audioSender.replaceTrack(e)}async changeVideoTrack(e){const i=this.stream.getVideoTracks()[0];i&&(i.stop(),this.stream.removeTrack(i)),this.stream.addTrack(e),this.videoSender&&await this.videoSender.replaceTrack(e)}async rePublishVideo(){const{videoTrack:e}=await p(this.constraints);this.changeVideoTrack(e)}}class S extends T{constructor(e={video:{displaySurface:"browser"},audio:{suppressLocalAudioPlayback:!1},preferCurrentTab:!1}){super(e)}async publish(e,i){if(!(this.status===o.Pendding||this.status===o.Disconnected))throw new u("Avcon SRS Sdk Error","当前SrsPublisher实例处于连接状态,请先调用close()方法关闭连接,再重新调用publish()方法");i&&this.setConstraints(i),this.RTCPeerConnection||this.init(),this.RTCPeerConnection.addTransceiver("audio",{direction:"sendonly"}),this.RTCPeerConnection.addTransceiver("video",{direction:"sendonly"});const{videoTrack:a,stream:c,audioTrack:n}=await p(this.constraints,g.Screen);return this.stream=c,this.RTCPeerConnection.addTrack(a),n&&this.RTCPeerConnection.addTrack(n),await this.setRTCDescription(e,C.getConfig().publishApi),c}}class y extends T{constructor(){super(),this.RTCPeerConnection.ontrack=e=>{this.stream.addTrack(e.track)}}async play(e){if(!(this.status===o.Pendding||this.status===o.Disconnected))throw new u("Avcon SRS Sdk Error","当前SrsPlayer实例处于连接状态,请先调用close()方法关闭连接,再重新调用play()方法");return this.RTCPeerConnection||(this.init(),this.RTCPeerConnection.ontrack=i=>{this.stream.addTrack(i.track)}),this.status=o.Connecting,this.RTCPeerConnection.addTransceiver("audio",{direction:"recvonly"}),this.RTCPeerConnection.addTransceiver("video",{direction:"recvonly"}),await this.setRTCDescription(e,C.getConfig().playApi)}}const f={schema:"https",httpApiPort:1985,httpsApiPort:1990,publishApi:"/rtc/v1/publish/",playApi:"/rtc/v1/play/"};var P=(t=>(t[t.Pendding=1]="Pendding",t[t.Connecting=2]="Connecting",t[t.Connected=3]="Connected",t[t.Disconnected=4]="Disconnected",t))(P||{});class k{constructor(e){d(this,"sdkConfig");this.sdkConfig=e}setConfig(e){this.sdkConfig={...f,...e}}getConfig(){return this.sdkConfig}createPublisher(e){return new w(e)}createScreenPublisher(e){return new S(e)}createPlayer(){return new y}}const C=new k(f);s.AcvconSrsSdk=k,s.RTCInstanceStatus=P,s.default=C,Object.defineProperties(s,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});