@scrypted/prebuffer-mixin 0.1.157 → 0.1.161
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +1 -1
- package/dist/main.nodejs.js +1 -1
- package/dist/plugin.zip +0 -0
- package/package.json +3 -2
- package/src/main.ts +129 -33
- package/src/rfc4571.ts +84 -0
package/.vscode/settings.json
CHANGED
package/dist/main.nodejs.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
(()=>{var e={454:(e,t,i)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.AutoenableMixinProvider=void 0;var r=function(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var i=n(t);if(i&&i.has(e))return i.get(e);var r={},o=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var s in e)if("default"!==s&&Object.prototype.hasOwnProperty.call(e,s)){var a=o?Object.getOwnPropertyDescriptor(e,s):null;a&&(a.get||a.set)?Object.defineProperty(r,s,a):r[s]=e[s]}r.default=e,i&&i.set(e,r);return r}(i(510));function n(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,i=new WeakMap;return(n=function(e){return e?i:t})(e)}const{systemManager:o}=r.default,s="v4";class AutoenableMixinProvider extends r.ScryptedDeviceBase{constructor(e){var t,i,n;super(e),n={},(i="hasEnabledMixin")in(t=this)?Object.defineProperty(t,i,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[i]=n;try{this.hasEnabledMixin=JSON.parse(this.storage.getItem("hasEnabledMixin"))}catch(e){this.hasEnabledMixin={}}this.pluginsComponent=o.getComponent("plugins"),o.listen((async(e,t,i)=>{t.eventInterface!==r.ScryptedInterface.ScryptedDevice||t.property||this.maybeEnableMixin(e)}));for(const e of Object.keys(o.getSystemState())){const t=o.getDeviceById(e);this.maybeEnableMixin(t)}}async shouldEnableMixin(e){return!0}async maybeEnableMixin(e){var t;if(!e||null!==(t=e.mixins)&&void 0!==t&&t.includes(this.id))return;if(this.hasEnabledMixin[e.id]===s)return;if(!await this.canMixin(e.type,e.interfaces))return;if(!await this.shouldEnableMixin(e))return;this.log.i("auto enabling mixin for "+e.name);const i=(e.mixins||[]).slice();i.push(this.id);const r=await this.pluginsComponent;await r.setMixins(e.id,i),this.setHasEnabledMixin(e.id)}setHasEnabledMixin(e){this.hasEnabledMixin[e]!==s&&(this.hasEnabledMixin[e]=s,this.storage.setItem("hasEnabledMixin",JSON.stringify(this.hasEnabledMixin)))}}t.AutoenableMixinProvider=AutoenableMixinProvider},201:(e,t,i)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.createParserRebroadcaster=async function(e,t,i){return await g(Object.assign({},i,{connect:(i,r)=>(e.on(t,i),()=>{r()})}))},t.createRebroadcaster=g,t.handleRebroadcasterClient=v,t.parseAudioCodec=f,t.parseResolution=p,t.parseVideoCodec=h,t.startParserSession=async function(e,t){const{console:i}=t;let r,a=!0;const u=new s.EventEmitter;let m,g,v,y,S;u.on("error",(e=>i.error("rebroadcast error",e)));const b=new Promise(((e,t)=>{y=e,S=t}));function P(){var e;a&&(u.emit("killed"),u.emit("error",new Error("killed"))),a=!1,null==D||D.kill(),setTimeout((()=>null==D?void 0:D.kill("SIGKILL")),1e3),null===(e=S)||void 0===e||e(new Error("ffmpeg was killed before connecting to the rebroadcast session")),clearTimeout(r)}const M=e.inputArguments.slice();r=setTimeout(P,3e4);const w=e=>{let r;function n(){i.error("timeout waiting for data, killing parser session",e),P()}function o(){t.timeout&&(clearTimeout(r),r=setTimeout(n,t.timeout))}return u.once("killed",(()=>clearTimeout(r))),o(),{resetActivityTimer:o}};let O=!1;const x=["pipe","pipe","pipe"];let I=3;for(const e of Object.keys(t.parsers)){const r=t.parsers[e];if(r.parseDatagram){O=!0;const t=d.default.createSocket("udp4"),i=await(0,o.bindZero)(t),n=d.default.createSocket("udp4");await(0,o.bind)(n,i.port+1),u.once("killed",(()=>{t.close(),n.close()})),M.push(...r.outputArguments,i.url.replace("udp://","rtp://"));const{resetActivityTimer:s}=w(e);(async()=>{for await(const a of r.parseDatagram(t,parseInt(null===(i=v)||void 0===i?void 0:i[2]),parseInt(null===(n=v)||void 0===n?void 0:n[3]))){var i,n,o;null===(o=y)||void 0===o||o(void 0),u.emit(e,a),s()}})(),(async()=>{for await(const a of r.parseDatagram(n,parseInt(null===(t=v)||void 0===t?void 0:t[2]),parseInt(null===(i=v)||void 0===i?void 0:i[3]),"rtcp")){var t,i,o;null===(o=y)||void 0===o||o(void 0),u.emit(e,a),s()}})()}else if(r.tcpProtocol){const t=await(0,o.listenZeroSingleClient)(),n=new URL(r.tcpProtocol);n.port=t.port.toString(),M.push(...r.outputArguments,n.toString());const{resetActivityTimer:s}=w(e);(async()=>{const n=await t.clientPromise;try{for await(const t of r.parse(n,parseInt(null===(o=v)||void 0===o?void 0:o[2]),parseInt(null===(a=v)||void 0===a?void 0:a[3]))){var o,a,c;null===(c=y)||void 0===c||c(void 0),u.emit(e,t),s()}}catch(e){i.error("rebroadcast parse error",e),P()}})()}else M.push(...r.outputArguments,"pipe:"+I++),x.push("pipe")}O&&(M.push("-sdp_file","pipe:"+I++),x.push("pipe"));M.unshift("-hide_banner"),(0,c.safePrintFFmpegArguments)(i,M);const D=n.default.spawn(await l.getFFmpegPath(),M,{stdio:x});let C;(0,c.ffmpegLogInitialOutput)(i,D,void 0,null==t?void 0:t.storage),D.on("exit",P),C=O?new Promise((e=>{const t=[];D.stdio[I-1].on("data",(i=>{t.push(i),e(t)}))})):Promise.resolve([]);let A=0;return Object.keys(t.parsers).forEach((async e=>{const r=t.parsers[e];if(!r.parse||r.tcpProtocol)return;const n=D.stdio[3+A];A++;try{const{resetActivityTimer:t}=w(e);for await(const i of r.parse(n,parseInt(null===(o=v)||void 0===o?void 0:o[2]),parseInt(null===(s=v)||void 0===s?void 0:s[3]))){var o,s,a;null===(a=y)||void 0===a||a(void 0),u.emit(e,i),t()}}catch(e){i.error("rebroadcast parse error",e),P()}})),f(D).then((e=>m=e)),h(D).then((e=>g=e)),p(D).then((e=>v=e)),await b,y=void 0,S=void 0,clearTimeout(r),{sdp:C,inputAudioCodec:m,inputVideoCodec:g,inputVideoResolution:v,isActive:()=>a,kill:P,mediaStreamOptions:e.mediaStreamOptions||{id:void 0,name:void 0},on(e,t){return u.on(e,t),this},once(e,t){return u.once(e,t),this},removeListener(e,t){return u.removeListener(e,t),this}}};var r=i(808),n=u(i(81)),o=i(769),s=i(361),a=u(i(510)),c=i(833),d=u(i(891));function u(e){return e&&e.__esModule?e:{default:e}}const{mediaManager:l}=a.default;async function p(e){return new Promise((t=>{const i=r=>{const n=r.toString(),o=/(([0-9]{2,5})x([0-9]{2,5}))/.exec(n);o&&(e.stdout.removeListener("data",i),e.stderr.removeListener("data",i),t(o))};e.stdout.on("data",i),e.stderr.on("data",i)}))}async function m(e,t){return new Promise((i=>{const r=n=>{const o=n.toString(),s=o.indexOf(`${t}: `);if(-1!==s){const n=o.substring(s+t.length+1).trim();let a=n.indexOf(" ");const c=n.indexOf(",");-1!==a&&c<a&&(a=c),-1!==a&&(e.stdout.removeListener("data",r),e.stderr.removeListener("data",r),i(n.substring(0,a)))}};e.stdout.on("data",r),e.stderr.on("data",r)}))}async function h(e){return m(e,"Video")}async function f(e){return m(e,"Audio")}async function g(e){let t,i=0;const n=()=>{null!=e&&e.idle&&(clearTimeout(t),t=setTimeout((()=>{0!==i?n():e.idle.callback()}),e.idle.timeout))};n();const s=(0,r.createServer)((t=>{v(t,e),t.once("close",(()=>{n(),i--})),n(),i++})),a=await(0,o.listenZero)(s);return{server:s,port:a,url:`tcp://127.0.0.1:${a}`,get clients(){return i}}}async function v(e,t){const i=await e;let r=!0;const n=()=>{i.removeAllListeners(),i.destroy();const e=o;o=void 0,null==e||e()};let o=null==t?void 0:t.connect((e=>{r&&(r=!1,e.startStream&&i.write(e.startStream));for(const t of e.chunks)i.write(t);return i.writableLength}),n);i.once("close",(()=>{n()})),i.on("error",(e=>{var i;return null==t||null===(i=t.console)||void 0===i?void 0:i.log("client stream ended")}))}},769:(e,t,i)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.bind=async function(e,t){return e.bind(t),await(0,n.once)(e,"listening"),{port:t,url:`udp://127.0.0.1:${t}`}},t.bindUdp=c,t.bindZero=async function(e){return c(e,0)},t.createBindUdp=d,t.createBindZero=async function(){return d(0)},t.listenZero=a,t.listenZeroSingleClient=async function(){const e=new r.default.Server,t=await a(e),i=new Promise(((t,i)=>{const r=setTimeout((()=>{i(new Error("timeout waiting for client"))}),3e4);e.on("connection",(i=>{e.close(),clearTimeout(r),t(i)}))}));return{url:`tcp://127.0.0.1:${t}`,port:t,clientPromise:i}};var r=s(i(808)),n=i(361),o=s(i(891));function s(e){return e&&e.__esModule?e:{default:e}}async function a(e){return e.listen(0),await(0,n.once)(e,"listening"),e.address().port}async function c(e,t){e.bind(t),await(0,n.once)(e,"listening");const i=e.address().port;return{port:i,url:`udp://127.0.0.1:${i}`}}async function d(e){const t=o.default.createSocket("udp4"),{port:i,url:r}=await c(t,e);return{server:t,port:i,url:r}}},833:(e,t,i)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=i(568);Object.keys(r).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===r[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return r[e]}}))}))},961:(e,t)=>{"use strict";async function i(e,t){if(!t)return Buffer.alloc(0);{const i=e.read(t);if(i)return i}return new Promise(((i,r)=>{const n=()=>{const r=e.read(t);r&&(s(),i(r))},o=()=>{s(),r(new Error(`stream ended during read for minimum ${t} bytes`))},s=()=>{e.removeListener("readable",n),e.removeListener("end",o)};e.on("readable",n),e.on("end",o)}))}Object.defineProperty(t,"__esModule",{value:!0}),t.readLength=i,t.readLine=async function(e){return n(e,r)},t.readUntil=n;const r="\n".charCodeAt(0);async function n(e,t){const r=[];let n=0;for(;;){const o=await i(e,1);if(!o)throw new Error("end of stream");if(o[0]===t)break;r[n++]=o[0]}return Buffer.from(r).toString()}},175:(e,t,i)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.RtspServer=void 0,t.createRtspParser=function(){let e;return{container:"rtsp",tcpProtocol:"rtsp://127.0.0.1/"+(0,n.randomBytes)(8).toString("hex"),inputArguments:["-rtsp_transport","tcp"],outputArguments:["-rtsp_transport","tcp","-vcodec","copy","-acodec","copy","-f","rtsp"],findSyncFrame:s,sdp:new Promise((t=>e=t)),async*parse(t,i,r){const n=new RtspServer(t);await n.handleSetup(),e(n.sdp);for await(const{type:e,rtcp:t,header:o,packet:s}of n.handleRecord())yield{chunks:[o,s],type:e,width:i,height:r}}}};var r=i(961),n=i(113);function o(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}function s(e){return e}class RtspServer{constructor(e,t,i){o(this,"videoChannel",0),o(this,"audioChannel",2),o(this,"udpPorts",{video:0,audio:0}),this.client=e,this.sdp=t,this.udp=i,this.session=(0,n.randomBytes)(4).toString("hex")}async handleSetup(){let e=[];for(;;){let i=await(0,r.readLine)(this.client);if(i=i.trim(),i)e.push(i);else{var t;if(null===(t=this.console)||void 0===t||t.log(e.join("\n")),!await this.headers(e))break;e=[]}}}async handlePlayback(){return this.handleSetup()}async*handleRecord(){for(;;){const e=await(0,r.readLength)(this.client,4),t=e.readUInt16BE(2),i=await(0,r.readLength)(this.client,t),n=e.readUInt8(1);yield{type:n-n%2===this.videoChannel?"video":"audio",rtcp:n%2==1,header:e,packet:i}}}send(e,t){const i=Buffer.alloc(4);i.writeUInt8(36,0),i.writeUInt8(t,1),i.writeUInt16BE(e.length,2),this.client.write(i),this.client.write(Buffer.from(e))}sendUdp(e,t,i){this.udp.send(t,i?e+1:e,"127.0.0.1")}sendVideo(e,t){this.udp?this.sendUdp(this.udpPorts.video,e,t):this.send(e,t?this.videoChannel+1:this.videoChannel)}sendAudio(e,t){this.udp?this.sendUdp(this.udpPorts.audio,e,t):this.send(e,t?this.audioChannel+1:this.audioChannel)}options(e,t){const i={Public:"DESCRIBE, OPTIONS, PAUSE, PLAY, SETUP, TEARDOWN, ANNOUNCE, RECORD"};this.respond(200,"OK",t,i)}describe(e,t){const i={};i["Content-Base"]=e,i["Content-Type"]="application/sdp",this.respond(200,"OK",t,i,Buffer.from(this.sdp))}setup(e,t){const i={},r=t.transport;if(i.Transport=t.transport,i.Session=this.session,r.includes("UDP")){const t=r.match(/.*?client_port=([0-9]+)-([0-9]+)/),[i,n,o]=t;e.includes("audio")?this.udpPorts.audio=parseInt(n):this.udpPorts.video=parseInt(n)}this.respond(200,"OK",t,i)}play(e,t){const i={};i["RTP-Info"]=`url=${e}/trackID=0;seq=0;rtptime=0,url=${e}/trackID=1;seq=0;rtptime=0`,i.Range="npt=now-",i.Session=this.session,this.respond(200,"OK",t,i)}async announce(e,t){const i=parseInt(t["content-length"]),n=await(0,r.readLength)(this.client,i);this.sdp=n.toString();const o={};o.Session=this.session,this.respond(200,"OK",t,o)}async record(e,t){const i={};i.Session=this.session,this.respond(200,"OK",t,i)}async headers(e){let[t,i]=e[0].split(" ",2);t=t.toLowerCase();const r=function(e){const t={};for(const i of e.slice(1)){const e=i.indexOf(":");let r="";-1!==e&&(r=i.substring(e+1).trim()),t[i.substring(0,e).toLowerCase()]=r}return t}(e);if(this[t])return await this[t](i,r),"play"!==t&&"record"!==t;this.respond(400,"Bad Request",r,{})}respond(e,t,i,r,n){let o=`RTSP/1.0 ${e} ${t}\r\n`;i.cseq&&(r.CSeq=i.cseq),n&&(r["Content-Length"]=n.length.toString());for(const[e,t]of Object.entries(r))o+=`${e}: ${t}\r\n`;o+="\r\n",this.client.write(o),n&&this.client.write(n)}}t.RtspServer=RtspServer},567:(e,t,i)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.SettingsMixinDeviceBase=void 0;var r=function(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var i=n(t);if(i&&i.has(e))return i.get(e);var r={},o=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var s in e)if("default"!==s&&Object.prototype.hasOwnProperty.call(e,s)){var a=o?Object.getOwnPropertyDescriptor(e,s):null;a&&(a.get||a.set)?Object.defineProperty(r,s,a):r[s]=e[s]}r.default=e,i&&i.set(e,r);return r}(i(510));function n(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,i=new WeakMap;return(n=function(e){return e?i:t})(e)}const{deviceManager:o}=r.default;class SettingsMixinDeviceBase extends r.MixinDeviceBase{constructor(e,t,i){super(e,i.mixinDeviceInterfaces,t,i.providerNativeId,i.mixinStorageSuffix),this.settingsGroup=i.group,this.settingsGroupKey=i.groupKey,process.nextTick((()=>o.onMixinEvent(this.id,this,r.ScryptedInterface.Settings,null)))}async getSettings(){const e=this.mixinDeviceInterfaces.includes(r.ScryptedInterface.Settings)?this.mixinDevice.getSettings():void 0,t=this.getMixinSettings(),i=[];try{const t=await e||[];i.push(...t)}catch(e){const t=this.name,r=`${t} Extension settings failed to load.`;this.console.error(r,e),i.push({key:Math.random().toString(),title:t,value:"Settings Error",group:"Errors",description:r,readonly:!0})}try{const e=await t||[];for(const t of e)t.group=t.group||this.settingsGroup,t.key=this.settingsGroupKey+":"+t.key;i.push(...e)}catch(e){const t=o.getDeviceState(this.mixinProviderNativeId).name,r=`${t} Extension settings failed to load.`;this.console.error(r,e),i.push({key:Math.random().toString(),title:t,value:"Settings Error",group:"Errors",description:r,readonly:!0})}return i}async putSetting(e,t){const i=this.settingsGroupKey+":";if(null==e||!e.startsWith(i))return this.mixinDevice.putSetting(e,t);await this.putMixinSetting(e.substring(i.length),t),o.onMixinEvent(this.id,this,r.ScryptedInterface.Settings,null)}release(){o.onMixinEvent(this.id,this,r.ScryptedInterface.Settings,null)}}t.SettingsMixinDeviceBase=SettingsMixinDeviceBase},129:(e,t,i)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.PIXEL_FORMAT_YUV420P=t.PIXEL_FORMAT_RGB24=void 0,t.createDgramParser=a,t.createFragmentedMp4Parser=function(e){return{container:"mp4",outputArguments:[...(null==e?void 0:e.vcodec)||[],...(null==e?void 0:e.acodec)||[],"-movflags","frag_keyframe+empty_moov+default_base_moof","-f","mp4"],async*parse(e){const t=c(e);let i,r,n;for await(const e of t)i?r||(r=e):i=e,yield{startStream:n,chunks:[e.header,e.data],type:e.type},i&&r&&!n&&(n=Buffer.concat([i.header,i.data,r.header,r.data]))},findSyncFrame:o}},t.createMpegTsParser=function(e){return{container:"mpegts",outputArguments:[...(null==e?void 0:e.vcodec)||[],...(null==e?void 0:e.acodec)||[],"-f","mpegts"],parse:s(188,(e=>{if(71!=e[0])throw new Error("Invalid sync byte in mpeg-ts packet. Terminating stream.")})),findSyncFrame(e){for(let t=0;t<e.length;t++){const i=e[t];for(let r=0;r<i.chunks.length;r++){const n=i.chunks[r];let o=0;for(;o+188<n.length;){const i=n.subarray(o,o+188);if(256==((31&i[1])<<8|i[2])&&32&i[3]&&i[4]>0&&64&i[5])return e.slice(t);o+=188}}}return e}}},t.createRawVideoParser=function(e){var t;const i=(null===(t=e)||void 0===t?void 0:t.pixelFormat)||d;let r;e=e||{};const{size:s,everyNFrames:a}=e;s&&(r=`scale=${s.width}:${s.height}`);a&&a>1&&(r?r+=",":r="",r+=`select=not(mod(n\\,${a}))`);return{container:"rawvideo",outputArguments:[...r?["-vf",r]:[],"-an","-vcodec","rawvideo","-pix_fmt",i.name,"-f","rawvideo"],async*parse(e,t,r){if(!t||!r)throw new Error("error parsing rawvideo, unknown width and height");t=(null==s?void 0:s.width)||t,r=(null==s?void 0:s.height)||r;const o=i.computeLength(t,r);for(;;){const i=await(0,n.readLength)(e,o);yield{chunks:[i],width:t,height:r}}},findSyncFrame:o}},t.createRtpParser=function(...e){return{container:"rtsp",inputArguments:["-v","verbose","-rtsp_transport","tcp"],outputArguments:[...e,"-f","rtp"],parseDatagram:a(),findSyncFrame:o}},t.parseFragmentedMP4=c;var r=i(361),n=i(961);function o(e){return e}function s(e,t){return async function*(i){let n=[],o=0;for(;;){const s=i.read();if(!s){await(0,r.once)(i,"readable");continue}if(n.push(s),o+=s.length,o<e)continue;const a=Buffer.concat(n);null==t||t(a);const c=a.length%e,d=a.slice(0,a.length-c),u=a.slice(a.length-c);n=[u],o=u.length,yield{chunks:[d]}}}}function a(){return async function*(e,t,i,n){for(;;){const[t]=await(0,r.once)(e,"message");yield{chunks:[t],type:n}}}}async function*c(e){for(;;){const t=await(0,n.readLength)(e,8),i=t.readInt32BE(0)-8,r=t.slice(4).toString(),o=await(0,n.readLength)(e,i);yield{header:t,length:i,type:r,data:o}}}const d={name:"yuv420p",computeLength:(e,t)=>e*t*1.5};t.PIXEL_FORMAT_YUV420P=d;t.PIXEL_FORMAT_RGB24={name:"rgb24",computeLength:(e,t)=>e*t*3}},510:(e,t,i)=>{"use strict";var r=Object.create?function(e,t,i,r){void 0===r&&(r=i),Object.defineProperty(e,r,{enumerable:!0,get:function(){return t[i]}})}:function(e,t,i,r){void 0===r&&(r=i),e[r]=t[i]},n=function(e,t){for(var i in e)"default"===i||Object.prototype.hasOwnProperty.call(t,i)||r(t,e,i)};Object.defineProperty(t,"__esModule",{value:!0}),t.MixinDeviceBase=t.ScryptedDeviceBase=void 0,n(i(632),t);const o=i(632);class ScryptedDeviceBase extends o.DeviceBase{constructor(e){super(),this.nativeId=e}get storage(){return this._storage||(this._storage=deviceManager.getDeviceStorage(this.nativeId)),this._storage}get log(){return this._log||(this._log=deviceManager.getDeviceLogger(this.nativeId)),this._log}get console(){return this._console||(this._console=deviceManager.getDeviceConsole(this.nativeId)),this._console}_lazyLoadDeviceState(){this._deviceState||(this.nativeId?this._deviceState=deviceManager.getDeviceState(this.nativeId):this._deviceState=deviceManager.getDeviceState())}onDeviceEvent(e,t){return deviceManager.onDeviceEvent(this.nativeId,e,t)}}t.ScryptedDeviceBase=ScryptedDeviceBase;class MixinDeviceBase extends o.DeviceBase{constructor(e,t,i,r,n){super(),this.mixinDevice=e,this.mixinDeviceInterfaces=t,this.mixinProviderNativeId=r,this._mixinStorageSuffix=n,this._listeners=new Set,this._deviceState=i}get storage(){if(!this._storage){const e=this._mixinStorageSuffix,t=this.id+(e?":"+e:"");this._storage=deviceManager.getMixinStorage(t,this.mixinProviderNativeId)}return this._storage}get console(){return this._console||(deviceManager.getMixinConsole?this._console=deviceManager.getMixinConsole(this.id,this.mixinProviderNativeId):this._console=deviceManager.getDeviceConsole(this.mixinProviderNativeId)),this._console}onDeviceEvent(e,t){return deviceManager.onMixinEvent(this.id,this,e,t)}_lazyLoadDeviceState(){}manageListener(e){this._listeners.add(e)}release(){for(const e of this._listeners)e.removeListener()}}t.MixinDeviceBase=MixinDeviceBase,function(){function e(e){return function(){return this._lazyLoadDeviceState(),this._deviceState[e]}}function t(e){return function(t){this._lazyLoadDeviceState(),this._deviceState[e]=t}}for(var i of Object.values(o.ScryptedInterfaceProperty))Object.defineProperty(ScryptedDeviceBase.prototype,i,{set:t(i),get:e(i)}),Object.defineProperty(MixinDeviceBase.prototype,i,{set:t(i),get:e(i)})}();let s={};try{s=Object.assign(s,{log:deviceManager.getDeviceLogger(void 0),deviceManager,endpointManager,mediaManager,systemManager,pluginHostAPI})}catch(e){console.error("sdk initialization error, import @scrypted/sdk/types instead",e)}t.default=s},632:(e,t)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.ScryptedMimeTypes=t.ScryptedInterface=t.MediaPlayerState=t.LockState=t.ThermostatMode=t.TemperatureUnit=t.FanMode=t.HumidityMode=t.ScryptedDeviceType=t.ScryptedInterfaceDescriptors=t.ScryptedInterfaceProperty=t.DeviceBase=void 0;t.DeviceBase=class DeviceBase{},function(e){e.id="id",e.info="info",e.interfaces="interfaces",e.mixins="mixins",e.name="name",e.providedInterfaces="providedInterfaces",e.providedName="providedName",e.providedRoom="providedRoom",e.providedType="providedType",e.providerId="providerId",e.room="room",e.type="type",e.on="on",e.brightness="brightness",e.colorTemperature="colorTemperature",e.rgb="rgb",e.hsv="hsv",e.running="running",e.paused="paused",e.docked="docked",e.thermostatActiveMode="thermostatActiveMode",e.thermostatAvailableModes="thermostatAvailableModes",e.thermostatMode="thermostatMode",e.thermostatSetpoint="thermostatSetpoint",e.thermostatSetpointHigh="thermostatSetpointHigh",e.thermostatSetpointLow="thermostatSetpointLow",e.temperature="temperature",e.temperatureUnit="temperatureUnit",e.humidity="humidity",e.lockState="lockState",e.entryOpen="entryOpen",e.batteryLevel="batteryLevel",e.online="online",e.updateAvailable="updateAvailable",e.fromMimeType="fromMimeType",e.toMimeType="toMimeType",e.binaryState="binaryState",e.intrusionDetected="intrusionDetected",e.powerDetected="powerDetected",e.audioDetected="audioDetected",e.motionDetected="motionDetected",e.ambientLight="ambientLight",e.occupied="occupied",e.flooded="flooded",e.ultraviolet="ultraviolet",e.luminance="luminance",e.position="position",e.humiditySetting="humiditySetting",e.fan="fan"}(t.ScryptedInterfaceProperty||(t.ScryptedInterfaceProperty={})),t.ScryptedInterfaceDescriptors={ScryptedDevice:{name:"ScryptedDevice",methods:["listen","probe","setName","setRoom","setType"],properties:["id","info","interfaces","mixins","name","providedInterfaces","providedName","providedRoom","providedType","providerId","room","type"]},ScryptedPlugin:{name:"ScryptedPlugin",methods:["getPluginJson"],properties:[]},OnOff:{name:"OnOff",methods:["turnOff","turnOn"],properties:["on"]},Brightness:{name:"Brightness",methods:["setBrightness"],properties:["brightness"]},ColorSettingTemperature:{name:"ColorSettingTemperature",methods:["getTemperatureMaxK","getTemperatureMinK","setColorTemperature"],properties:["colorTemperature"]},ColorSettingRgb:{name:"ColorSettingRgb",methods:["setRgb"],properties:["rgb"]},ColorSettingHsv:{name:"ColorSettingHsv",methods:["setHsv"],properties:["hsv"]},Notifier:{name:"Notifier",methods:["sendNotification"],properties:[]},StartStop:{name:"StartStop",methods:["start","stop"],properties:["running"]},Pause:{name:"Pause",methods:["pause","resume"],properties:["paused"]},Dock:{name:"Dock",methods:["dock"],properties:["docked"]},TemperatureSetting:{name:"TemperatureSetting",methods:["setThermostatMode","setThermostatSetpoint","setThermostatSetpointHigh","setThermostatSetpointLow"],properties:["thermostatActiveMode","thermostatAvailableModes","thermostatMode","thermostatSetpoint","thermostatSetpointHigh","thermostatSetpointLow"]},Thermometer:{name:"Thermometer",methods:["setTemperatureUnit"],properties:["temperature","temperatureUnit"]},HumiditySensor:{name:"HumiditySensor",methods:[],properties:["humidity"]},Camera:{name:"Camera",methods:["getPictureOptions","takePicture"],properties:[]},VideoCamera:{name:"VideoCamera",methods:["getVideoStream","getVideoStreamOptions"],properties:[]},VideoCameraConfiguration:{name:"VideoCameraConfiguration",methods:["setVideoStreamOptions"],properties:[]},Intercom:{name:"Intercom",methods:["startIntercom","stopIntercom"],properties:[]},Lock:{name:"Lock",methods:["lock","unlock"],properties:["lockState"]},PasswordStore:{name:"PasswordStore",methods:["addPassword","getPasswords","removePassword"],properties:[]},Authenticator:{name:"Authenticator",methods:["checkPassword"],properties:[]},Scene:{name:"Scene",methods:["activate","deactivate","isReversible"],properties:[]},Entry:{name:"Entry",methods:["closeEntry","openEntry"],properties:[]},EntrySensor:{name:"EntrySensor",methods:[],properties:["entryOpen"]},DeviceProvider:{name:"DeviceProvider",methods:["getDevice"],properties:[]},DeviceDiscovery:{name:"DeviceDiscovery",methods:["discoverDevices"],properties:[]},DeviceCreator:{name:"DeviceCreator",methods:["createDevice","getCreateDeviceSettings"],properties:[]},Battery:{name:"Battery",methods:[],properties:["batteryLevel"]},Refresh:{name:"Refresh",methods:["getRefreshFrequency","refresh"],properties:[]},MediaPlayer:{name:"MediaPlayer",methods:["getMediaStatus","load","seek","skipNext","skipPrevious"],properties:[]},Online:{name:"Online",methods:[],properties:["online"]},SoftwareUpdate:{name:"SoftwareUpdate",methods:["checkForUpdate","installUpdate"],properties:["updateAvailable"]},BufferConverter:{name:"BufferConverter",methods:["convert"],properties:["fromMimeType","toMimeType"]},Settings:{name:"Settings",methods:["getSettings","putSetting"],properties:[]},BinarySensor:{name:"BinarySensor",methods:[],properties:["binaryState"]},IntrusionSensor:{name:"IntrusionSensor",methods:[],properties:["intrusionDetected"]},PowerSensor:{name:"PowerSensor",methods:[],properties:["powerDetected"]},AudioSensor:{name:"AudioSensor",methods:[],properties:["audioDetected"]},MotionSensor:{name:"MotionSensor",methods:[],properties:["motionDetected"]},AmbientLightSensor:{name:"AmbientLightSensor",methods:[],properties:["ambientLight"]},OccupancySensor:{name:"OccupancySensor",methods:[],properties:["occupied"]},FloodSensor:{name:"FloodSensor",methods:[],properties:["flooded"]},UltravioletSensor:{name:"UltravioletSensor",methods:[],properties:["ultraviolet"]},LuminanceSensor:{name:"LuminanceSensor",methods:[],properties:["luminance"]},PositionSensor:{name:"PositionSensor",methods:[],properties:["position"]},Readme:{name:"Readme",methods:["getReadmeMarkdown"],properties:[]},OauthClient:{name:"OauthClient",methods:["getOauthUrl","onOauthCallback"],properties:[]},MixinProvider:{name:"MixinProvider",methods:["canMixin","getMixin","releaseMixin"],properties:[]},HttpRequestHandler:{name:"HttpRequestHandler",methods:["onRequest"],properties:[]},EngineIOHandler:{name:"EngineIOHandler",methods:["onConnection"],properties:[]},PushHandler:{name:"PushHandler",methods:["onPush"],properties:[]},Program:{name:"Program",methods:["run"],properties:[]},Scriptable:{name:"Scriptable",methods:["eval","loadScripts","saveScript"],properties:[]},ObjectDetector:{name:"ObjectDetector",methods:["getDetectionInput","getObjectTypes"],properties:[]},ObjectDetection:{name:"ObjectDetection",methods:["detectObjects","getDetectionModel"],properties:[]},HumiditySetting:{name:"HumiditySetting",methods:["setHumidity"],properties:["humiditySetting"]},Fan:{name:"Fan",methods:["setFan"],properties:["fan"]}},function(e){e.Builtin="Builtin",e.Camera="Camera",e.Fan="Fan",e.Light="Light",e.Switch="Switch",e.Outlet="Outlet",e.Sensor="Sensor",e.Scene="Scene",e.Program="Program",e.Automation="Automation",e.Vacuum="Vacuum",e.Notifier="Notifier",e.Thermostat="Thermostat",e.Lock="Lock",e.PasswordControl="PasswordControl",e.Display="Display",e.Speaker="Speaker",e.Event="Event",e.Entry="Entry",e.Garage="Garage",e.DeviceProvider="DeviceProvider",e.DataSource="DataSource",e.API="API",e.Doorbell="Doorbell",e.Irrigation="Irrigation",e.Valve="Valve",e.Person="Person",e.Unknown="Unknown"}(t.ScryptedDeviceType||(t.ScryptedDeviceType={})),function(e){e.Humidify="Humidify",e.Dehumidify="Dehumidify",e.Auto="Auto",e.Off="Off"}(t.HumidityMode||(t.HumidityMode={})),function(e){e.Auto="Auto",e.Manual="Manual"}(t.FanMode||(t.FanMode={})),function(e){e.C="C",e.F="F"}(t.TemperatureUnit||(t.TemperatureUnit={})),function(e){e.Off="Off",e.Cool="Cool",e.Heat="Heat",e.HeatCool="HeatCool",e.Auto="Auto",e.FanOnly="FanOnly",e.Purifier="Purifier",e.Eco="Eco",e.Dry="Dry",e.On="On"}(t.ThermostatMode||(t.ThermostatMode={})),function(e){e.Locked="Locked",e.Unlocked="Unlocked",e.Jammed="Jammed"}(t.LockState||(t.LockState={})),function(e){e.Idle="Idle",e.Playing="Playing",e.Paused="Paused",e.Buffering="Buffering"}(t.MediaPlayerState||(t.MediaPlayerState={})),function(e){e.ScryptedDevice="ScryptedDevice",e.ScryptedPlugin="ScryptedPlugin",e.OnOff="OnOff",e.Brightness="Brightness",e.ColorSettingTemperature="ColorSettingTemperature",e.ColorSettingRgb="ColorSettingRgb",e.ColorSettingHsv="ColorSettingHsv",e.Notifier="Notifier",e.StartStop="StartStop",e.Pause="Pause",e.Dock="Dock",e.TemperatureSetting="TemperatureSetting",e.Thermometer="Thermometer",e.HumiditySensor="HumiditySensor",e.Camera="Camera",e.VideoCamera="VideoCamera",e.VideoCameraConfiguration="VideoCameraConfiguration",e.Intercom="Intercom",e.Lock="Lock",e.PasswordStore="PasswordStore",e.Authenticator="Authenticator",e.Scene="Scene",e.Entry="Entry",e.EntrySensor="EntrySensor",e.DeviceProvider="DeviceProvider",e.DeviceDiscovery="DeviceDiscovery",e.DeviceCreator="DeviceCreator",e.Battery="Battery",e.Refresh="Refresh",e.MediaPlayer="MediaPlayer",e.Online="Online",e.SoftwareUpdate="SoftwareUpdate",e.BufferConverter="BufferConverter",e.Settings="Settings",e.BinarySensor="BinarySensor",e.IntrusionSensor="IntrusionSensor",e.PowerSensor="PowerSensor",e.AudioSensor="AudioSensor",e.MotionSensor="MotionSensor",e.AmbientLightSensor="AmbientLightSensor",e.OccupancySensor="OccupancySensor",e.FloodSensor="FloodSensor",e.UltravioletSensor="UltravioletSensor",e.LuminanceSensor="LuminanceSensor",e.PositionSensor="PositionSensor",e.Readme="Readme",e.OauthClient="OauthClient",e.MixinProvider="MixinProvider",e.HttpRequestHandler="HttpRequestHandler",e.EngineIOHandler="EngineIOHandler",e.PushHandler="PushHandler",e.Program="Program",e.Scriptable="Scriptable",e.ObjectDetector="ObjectDetector",e.ObjectDetection="ObjectDetection",e.HumiditySetting="HumiditySetting",e.Fan="Fan"}(t.ScryptedInterface||(t.ScryptedInterface={})),function(e){e.AcceptUrlParameter="accept-url",e.Url="text/x-uri",e.InsecureLocalUrl="text/x-insecure-local-uri",e.LocalUrl="text/x-local-uri",e.PushEndpoint="text/x-push-endpoint",e.MediaStreamUrl="text/x-media-url",e.FFmpegInput="x-scrypted/x-ffmpeg-input",e.RTCAVSignalingPrefix="x-scrypted-rtc-signaling-",e.RTCAVOffer="x-scrypted/x-rtc-av-offer",e.RTCAVAnswer="x-scrypted/x-rtc-av-answer",e.RTCAVSignalingOfferSetup="x-scrypted/x-rtc-av-signalling-offer-setup",e.SchemePrefix="x-scrypted/x-scrypted-scheme-",e.MediaObject="x-scrypted/x-scrypted-media-object"}(t.ScryptedMimeTypes||(t.ScryptedMimeTypes={}))},568:(e,t,i)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.ffmpegLogInitialOutput=function(e,t,i,r){var s,a;const c=!!n.default.env.SCRYPTED_FFMPEG_NOISY||!(null==r||!r.getItem("SCRYPTED_FFMPEG_NOISY"));function d(e){const r=n=>{const s=n.toString();for(const e of o)if(-1!==s.indexOf(e))return;if(!(c||i||-1===s.indexOf("frame=")&&-1===s.indexOf("size=")))return e(s),e("video/audio detected, discarding further input"),t.stdout.removeListener("data",r),void t.stderr.removeListener("data",r);e(s)};return r}null===(s=t.stdout)||void 0===s||s.on("data",d(e.log)),null===(a=t.stderr)||void 0===a||a.on("data",d(e.error)),t.on("exit",(()=>e.log("ffmpeg exited")))},t.safePrintFFmpegArguments=function(e,t){const i=[];for(const e of t)try{const t=new URL(e);i.push(`${t.protocol}[REDACTED]`)}catch(t){i.push(e)}e.log(i.join(" "))};var r,n=(r=i(282))&&r.__esModule?r:{default:r};const o=["decode_slice_header error","no frame!","non-existing PPS"]},81:e=>{"use strict";e.exports=require("child_process")},113:e=>{"use strict";e.exports=require("crypto")},891:e=>{"use strict";e.exports=require("dgram")},361:e=>{"use strict";e.exports=require("events")},808:e=>{"use strict";e.exports=require("net")},282:e=>{"use strict";e.exports=require("process")}},t={};function i(r){var n=t[r];if(void 0!==n)return n.exports;var o=t[r]={exports:{}};return e[r](o,o.exports,i),o.exports}var r={};(()=>{"use strict";var e=r;Object.defineProperty(e,"__esModule",{value:!0}),e.default=void 0;var t=function(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var i=l(t);if(i&&i.has(e))return i.get(e);var r={},n=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var o in e)if("default"!==o&&Object.prototype.hasOwnProperty.call(e,o)){var s=n?Object.getOwnPropertyDescriptor(e,o):null;s&&(s.get||s.set)?Object.defineProperty(r,o,s):r[o]=e[o]}r.default=e,i&&i.set(e,r);return r}(i(510)),n=i(361),o=i(567),s=i(201),a=i(129),c=i(454),d=i(769),u=i(175);function l(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,i=new WeakMap;return(l=function(e){return e?i:t})(e)}function p(e,t,i){return t in e?Object.defineProperty(e,t,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[t]=i,e}const{mediaManager:m,log:h,systemManager:f,deviceManager:g}=t.default,v=1e4,y="prebufferDuration",S="sendKeyframe",b="Default",P="AAC or No Audio",M=`${P} (Copy)`,w="Compatible Audio",O="Other Audio",x=["aac","mp3","mp2","opus"],I="-fflags +genpts",D=[P,w,O],C=["mpegts","mp4","rtsp"];class PrebufferSession{constructor(e,t,i,r){p(this,"prebuffers",{mp4:[],mpegts:[],rtsp:[]}),p(this,"detectedIdrInterval",0),p(this,"prevIdr",0),p(this,"audioDisabled",!1),p(this,"activeClients",0),this.mixin=e,this.streamName=t,this.streamId=i,this.stopInactive=r,this.storage=e.storage,this.console=e.console,this.mixinDevice=e.mixinDevice,this.audioConfigurationKey="audioConfiguration-"+this.streamId,this.ffmpegInputArgumentsKey="ffmpegInputArguments-"+this.streamId,this.rebroadcastModeKey="rebroadcastMode-"+this.streamId,this.lastDetectedAudioCodecKey="lastDetectedAudioCodec-"+this.streamId}clearPrebuffers(){this.prebuffers.mp4=[],this.prebuffers.mpegts=[],this.prebuffers.rtsp=[]}ensurePrebufferSession(){this.parserSessionPromise||this.mixin.released||(this.console.log(this.streamName,"prebuffer session started"),this.parserSessionPromise=this.startPrebufferSession(),this.parserSessionPromise.catch((()=>this.parserSessionPromise=void 0)))}getAudioConfig(){let e=this.storage.getItem(this.audioConfigurationKey)||"";D.find((t=>e.startsWith(t)))||(e="");const t=-1!==e.indexOf(P),i=-1!==e.indexOf(w),r=-1!==e.indexOf(O);return{isUsingDefaultAudioConfig:!(t||i||r),aacAudio:t,compatibleAudio:i,reencodeAudio:r}}async getMixinSettings(){const e=[],t=this.parserSession;let i=0,r=0;for(const e of this.prebuffers.mp4){r=r||e.time;for(const t of e.chunk.chunks)i+=t.byteLength}const n=Date.now()-r,o=Math.round(i/n*8),s=this.streamName?`Rebroadcast: ${this.streamName}`:"Rebroadcast";var a,c,d;(e.push({title:"Audio Codec Transcoding",group:s,description:"Configuring your camera to output AAC, MP3, MP2, or Opus is recommended. PCM/G711 cameras should set this to Transcode.",type:"string",key:this.audioConfigurationKey,value:this.storage.getItem(this.audioConfigurationKey)||b,choices:[b,M,"Compatible Audio (Copy)","Other Audio (Transcode)"]},{title:"FFmpeg Input Arguments Prefix",group:s,description:"Optional/Advanced: Additional input arguments to pass to the ffmpeg command. These will be placed before the input arguments.",key:this.ffmpegInputArgumentsKey,value:this.storage.getItem(this.ffmpegInputArgumentsKey),placeholder:I,choices:[I,"-use_wallclock_as_timestamps 1","-v verbose"],combobox:!0},{title:"Rebroadcast Mode",group:s,description:"THIS FEATURE IS IN TESTING. DO NOT CHANGE THIS FROM MPEG-TS. The stream format to use when rebroadcasting.",placeholder:"MPEG-TS",choices:["MPEG-TS","RTSP"],key:this.rebroadcastModeKey,value:this.storage.getItem(this.rebroadcastModeKey)||"MPEG-TS"}),t)?e.push({key:"detectedResolution",group:s,title:"Detected Resolution and Bitrate",readonly:!0,value:`${(null==t||null===(a=t.inputVideoResolution)||void 0===a?void 0:a[0])||"unknown"} @ ${o||"unknown"} Kb/s`,description:"Configuring your camera to 1920x1080, 2000Kb/S, Variable Bit Rate, is recommended."},{key:"detectedCodec",group:s,title:"Detected Video/Audio Codecs",readonly:!0,value:((null==t||null===(c=t.inputVideoCodec)||void 0===c?void 0:c.toString())||"unknown")+"/"+((null==t||null===(d=t.inputAudioCodec)||void 0===d?void 0:d.toString())||"unknown"),description:"Configuring your camera to H264 video and AAC/MP3/MP2/Opus audio is recommended."},{key:"detectedKeyframe",group:s,title:"Detected Keyframe Interval",description:"Configuring your camera to 4 seconds is recommended (IDR aka Frame Interval = FPS * 4 seconds).",readonly:!0,value:((this.detectedIdrInterval||0)/1e3).toString()||"none"}):e.push({title:"Status",group:s,key:"status",description:"Rebroadcast is currently idle and will be started automatically on demand.",value:"Idle",readonly:!0});return e}async startPrebufferSession(){var e,i,r,n,o,c,d;this.prebuffers.mp4=[],this.prebuffers.mpegts=[],this.prebuffers.rtsp=[];const l=parseInt(this.storage.getItem(y))||v;let p;try{p=(await this.mixinDevice.getVideoStreamOptions()).find((e=>e.id===this.streamId))}catch(e){}const f=null===(null===(e=p)||void 0===e?void 0:e.audio),g=null===(i=p)||void 0===i||null===(r=i.audio)||void 0===r?void 0:r.codec,{isUsingDefaultAudioConfig:S,aacAudio:b,compatibleAudio:P,reencodeAudio:M}=this.getAudioConfig();let w=this.storage.getItem(this.lastDetectedAudioCodecKey)||void 0;"null"===w&&(w=null);let O=!1;f||g||!S||void 0!==w||(this.console.warn("Camera did not report an audio codec, muting the audio stream and probing the codec."),O=!0),!f&&g&&void 0!==w&&w!==g&&this.console.warn("Audio codec plugin reported vs detected mismatch",g,w);const D=void 0===w?null==g?void 0:g.toLowerCase():null===(n=w)||void 0===n?void 0:n.toLowerCase(),A=!x.includes(D);O||!1===(null===(o=p)||void 0===o?void 0:o.userConfigurable)||f||A&&(S&&h.a(`${this.mixin.name} is using the ${D} audio codec. Configuring your Camera to use AAC, MP3, MP2, or Opus audio is recommended. If this is not possible, Select 'Transcode Audio' in the camera stream's Rebroadcast settings to suppress this alert.`),this.console.warn("Configure your camera to output AAC, MP3, MP2, or Opus audio. Suboptimal audio codec in use:",D));const k=await this.mixinDevice.getVideoStream(p),T=await m.convertMediaObjectToBuffer(k,t.ScryptedMimeTypes.FFmpegInput),_=JSON.parse(T.toString()),E=["-bsf:a","aac_adtstoasc"],R=[];let L;this.audioDisabled=!1;const B=null===w;let j=!1;var F;!O&&S&&A&&(!1===(null===(F=p)||void 0===F?void 0:F.userConfigurable)?this.console.log("camera reports it is not user configurable. transcoding due to incompatible codec",D):this.console.log("camera audio transcoding due to incompatible codec. configure the camera to use a compatible codec if possible."),j=!0);if(f||O)L=["-an"],this.audioDisabled=!0;else if(M||j)L=["-bsf:a","aac_adtstoasc","-acodec","libfdk_aac","-ar","8k","-b:a","100k","-bufsize","400k","-ac","1","-profile:a","aac_low","-flags","+global_header"];else if(b||B)L=["-acodec","copy"],L.push(...E);else if(P)L=["-acodec","copy"],L.push(...R);else{L=["-acodec","copy"];const e="aac"===D?E:R;L.push(...e)}const H=["-vcodec","copy"],V={console:this.console,timeout:6e4,parsers:{mp4:(0,a.createFragmentedMp4Parser)({vcodec:H,acodec:L})}};if("RTSP"===this.storage.getItem(this.rebroadcastModeKey)){const e=(0,u.createRtspParser)();this.sdp=e.sdp,V.parsers.rtsp=e}else V.parsers.mpegts=(0,a.createMpegTsParser)({vcodec:H,acodec:L});this.parsers=V.parsers;const N=this.storage.getItem(this.ffmpegInputArgumentsKey)||I;_.inputArguments.unshift(...N.split(" ")),this.storage.removeItem(this.lastDetectedAudioCodecKey);const U=await(0,s.startParserSession)(_,V);if(U.inputAudioCodec?x.includes(null===(c=U.inputAudioCodec)||void 0===c?void 0:c.toLowerCase())?this.console.log("Detected audio codec is mp4/mpegts compatible.",U.inputAudioCodec):this.console.log("Detected audio codec is not mp4/mpegts compatible.",U.inputAudioCodec):this.console.log("No audio stream detected."),this.storage.setItem(this.lastDetectedAudioCodecKey,U.inputAudioCodec||"null"),"h264"!==U.inputVideoCodec&&this.console.error("Video codec is not h264. If there are errors, try changing your camera's encoder output."),O)return this.console.warn("Audio probe complete, ending rebroadcast session and restarting with detected codecs."),U.kill(),this.startPrebufferSession();if(this.parserSession=U,null!==(d=_.mediaStreamOptions)&&void 0!==d&&d.refreshAt){let e,i=_.mediaStreamOptions;const r=async()=>{if(!U.isActive)return;const e=await this.mixinDevice.getVideoStream(i),r=await m.convertMediaObjectToBuffer(e,t.ScryptedMimeTypes.FFmpegInput),o=JSON.parse(r.toString());i=o.mediaStreamOptions,n(o)},n=t=>{const i=t.mediaStreamOptions.refreshAt-Date.now()-3e4;this.console.log("refreshing media stream in",i),e=setTimeout(r,i)};n(_),U.once("killed",(()=>clearTimeout(e)))}U.once("killed",(()=>{this.parserSessionPromise=void 0,this.parserSession===U&&(this.parserSession=void 0)}));for(const e of C){let t=0;U.on(e,(i=>{const r=this.prebuffers[e],n=Date.now();for("mdat"===i.type&&(this.prevIdr&&(this.detectedIdrInterval=n-this.prevIdr),this.prevIdr=n),r.push({time:n,chunk:i});r.length&&r[0].time<n-l;)r.shift(),t++;t>1e3&&(this.prebuffers[e]=r.slice(),t=0)}))}return U}printActiveClients(){this.console.log(this.streamName,"active rebroadcast clients:",this.activeClients)}inactivityCheck(e,t){this.printActiveClients(),this.stopInactive&&(this.activeClients||this.inactivityTimeout&&!t||(clearTimeout(this.inactivityTimeout),this.inactivityTimeout=setTimeout((()=>{this.activeClients||(this.console.log(this.streamName,"terminating rebroadcast due to inactivity"),e.kill())}),3e4)))}async getVideoStream(e){var t,i;this.ensurePrebufferSession();const r=await this.parserSessionPromise,n="false"!==this.storage.getItem(S),o=(null==e?void 0:e.prebuffer)||(n?1.5*Math.max(4e3,this.detectedIdrInterval||4e3):0);this.console.log(this.streamName,"prebuffer request started");const a="RTSP"===this.storage.getItem(this.rebroadcastModeKey)?"rtsp":"mpegts",c=this.parsers[null==e?void 0:e.container]?null==e?void 0:e.container:a,l=Object.assign({},r.mediaStreamOptions);l.prebuffer=o;const{reencodeAudio:p}=this.getAudioConfig();this.audioDisabled?l.audio=null:l.audio=p?{codec:"aac",encoder:"libfdk_aac",profile:"aac_low"}:{codec:null==r?void 0:r.inputAudioCodec},l.video&&null!==(t=r.inputVideoResolution)&&void 0!==t&&t[2]&&null!==(i=r.inputVideoResolution)&&void 0!==i&&i[3]&&Object.assign(l.video,{width:parseInt(r.inputVideoResolution[2]),height:parseInt(r.inputVideoResolution[3])});const h=Date.now();let f=0;const g=this.prebuffers[c];for(const e of g)if(!(e.time<h-o))for(const t of e.chunk.chunks)f+=t.length;const v=Math.max(5e5,f).toString(),y=await(async t=>{const i=this.prebuffers[t];let n,a;if("rtsp"===t){this.sdp.then((e=>console.log(e)));const e=await(0,d.listenZeroSingleClient)();n=e.clientPromise.then((async e=>{let t=await this.sdp;const i=new u.RtspServer(e,t);return await i.handlePlayback(),e})),a=e.url.replace("tcp://","rtsp://")}else{const e=await(0,d.listenZeroSingleClient)();n=e.clientPromise,a=`tcp://127.0.0.1:${e.port}`}return(0,s.handleRebroadcasterClient)(n,{console:this.console,connect:(n,s)=>{this.activeClients++,this.printActiveClients();const a=Date.now(),c=e=>{n(e)>1e8&&(this.console.log("more than 100MB has been buffered, did downstream die? killing connection.",this.streamName),d())},d=()=>{s(),this.console.log(this.streamName,"prebuffer request ended"),r.removeListener(t,c),r.removeListener("killed",d)};r.on(t,c),r.once("killed",d);for(const e of i)e.time<a-o||c(e.chunk);return()=>{this.activeClients--,this.inactivityCheck(r,!1!==(null==e?void 0:e.refresh)),d()}}}),a})(c),b={url:y,container:c,inputArguments:["-analyzeduration","0","-probesize",v,...this.parsers[c].inputArguments||[],"-f",this.parsers[c].container,"-i",y],mediaStreamOptions:l};return m.createFFmpegMediaObject(b)}}class PrebufferMixin extends o.SettingsMixinDeviceBase{constructor(e,t,i,r){super(e,i,{providerNativeId:r,mixinDeviceInterfaces:t,group:"Prebuffer Settings",groupKey:"prebuffer"}),p(this,"released",!1),p(this,"sessions",new Map),this.delayStart()}delayStart(){this.console.log("prebuffer sessions starting in 5 seconds"),setTimeout((()=>this.ensurePrebufferSessions()),5e3)}async getVideoStream(e){await this.ensurePrebufferSessions();const t=null==e?void 0:e.id;let i=this.sessions.get(t);return!i||null!=e&&e.directMediaStream?this.mixinDevice.getVideoStream(e):(i.ensurePrebufferSession(),await i.parserSessionPromise,i=this.sessions.get(t),i?i.getVideoStream(e):this.mixinDevice.getVideoStream(e))}async ensurePrebufferSessions(){const e=await this.mixinDevice.getVideoStreamOptions(),i=this.getEnabledMediaStreamOptions(e),r=i?i.map((e=>e.id)):[void 0],o=(null==e?void 0:e.map((e=>e.id)))||[void 0];if("true"!==this.storage.getItem("warnedCloud")){(null==e?void 0:e.find((e=>"cloud"===e.source)))&&(this.storage.setItem("warnedCloud","true"),h.a(`${this.name} is a cloud camera. Prebuffering maintains a persistent stream and will not enabled by default. You must enable the Prebuffer stream manually.`))}const s=this.mixinDeviceInterfaces.includes(t.ScryptedInterface.Battery);let a=0;const c=o.length;for(const t of o){let i=this.sessions.get(t);if(!i){var d;const o=null==e?void 0:e.find((e=>e.id===t));null!=o&&o.prebuffer&&h.a(`Prebuffer is already available on ${this.name}. If this is a grouped device, disable the Rebroadcast extension.`);const u=null==o?void 0:o.name,l=!r.includes(t);if(i=new PrebufferSession(this,u,t,s||l),this.sessions.set(t,i),t===(null==e||null===(d=e[0])||void 0===d?void 0:d.id)&&this.sessions.set(void 0,i),s){this.console.log("camera is battery powered, prebuffering and rebroadcasting will only work on demand.");continue}if(l){this.console.log("stream",u,"will be rebroadcast on demand.");continue}(async()=>{for(;this.sessions.get(t)===i&&!this.released;){i.ensurePrebufferSession();try{const e=await i.parserSessionPromise;a++,this.online=a==c,await(0,n.once)(e,"killed"),this.console.error("prebuffer session ended")}catch(e){this.console.error("prebuffer session ended with error",e)}finally{a--,this.online=a==c}this.console.log("restarting prebuffer session in 5 seconds"),await new Promise((e=>setTimeout(e,5e3)))}this.console.log("exiting prebuffer session (released or restarted with new configuration)")})()}}g.onMixinEvent(this.id,this.mixinProviderNativeId,t.ScryptedInterface.Settings,void 0)}async getMixinSettings(){const e=[];try{const t=await this.mixinDevice.getVideoStreamOptions(),i=this.getEnabledMediaStreamOptions(t);(null==t?void 0:t.length)>0&&e.push({title:"Prebuffered Streams",description:"The streams to prebuffer. Enable only as necessary to reduce traffic.",key:"enabledStreams",value:i.map((e=>e.name||"")),choices:t.map((e=>e.name)),multiple:!0})}catch(e){throw this.console.error("error in getVideoStreamOptions",e),e}e.push({title:"Prebuffer Duration",description:"Duration of the prebuffer in milliseconds.",type:"number",key:y,value:this.storage.getItem(y)||v.toString()},{title:"Start at Previous Keyframe",description:"Start live streams from the previous key frame. Improves startup time.",type:"boolean",key:S,value:("false"!==this.storage.getItem(S)).toString()});for(const t of new Set([...this.sessions.values()]))if(t)try{e.push(...await t.getMixinSettings())}catch(e){throw this.console.error("error in prebuffer session getMixinSettings",e),e}return e}async putMixinSetting(e,t){const i=this.sessions;this.sessions=new Map,"enabledStreams"===e?this.storage.setItem(e,JSON.stringify(t)):this.storage.setItem(e,t.toString());for(const e of i.values()){var r;null==e||null===(r=e.parserSessionPromise)||void 0===r||r.then((e=>e.kill()))}this.ensurePrebufferSessions()}getEnabledMediaStreamOptions(e){if(!e)return;try{const t=JSON.parse(this.storage.getItem("enabledStreams"));return e.filter((e=>t.includes(e.name)))}catch(e){}const t=e.find((e=>"cloud"!==e.source));return t?[t]:[]}async getVideoStreamOptions(){const e=await this.mixinDevice.getVideoStreamOptions()||[];let t=this.getEnabledMediaStreamOptions(e);const i=parseInt(this.storage.getItem(y))||v;for(const n of e){var r;(null!==(r=this.sessions.get(n.id))&&void 0!==r&&r.parserSession||t.includes(n))&&(n.prebuffer=i)}return e}release(){this.console.log("prebuffer releasing if started"),this.released=!0;for(const t of this.sessions.values()){var e;t&&(t.clearPrebuffers(),null===(e=t.parserSessionPromise)||void 0===e||e.then((e=>{this.console.log("prebuffer released"),e.kill(),t.clearPrebuffers()})))}}}class PrebufferProvider extends c.AutoenableMixinProvider{constructor(e){super(e);for(const e of Object.keys(f.getSystemState())){var t;const i=f.getDeviceById(e);null!==(t=i.mixins)&&void 0!==t&&t.includes(this.id)&&i.getVideoStreamOptions()}const i=function(){var e=new Date;return e.setHours(24),e.setMinutes(0),e.setSeconds(0),e.setMilliseconds(0),e.getTime()-(new Date).getTime()}()+72e5;this.log.i(`Rebroadcaster scheduled for restart at 2AM: ${Math.round(i/1e3/60)} minutes`),setTimeout((()=>g.requestRestart()),i)}async canMixin(e,i){return i.includes(t.ScryptedInterface.VideoCamera)?[t.ScryptedInterface.VideoCamera,t.ScryptedInterface.Settings,t.ScryptedInterface.Online]:null}async getMixin(e,t,i){return this.setHasEnabledMixin(i.id),new PrebufferMixin(e,t,i,this.nativeId)}async releaseMixin(e,t){t.online=!0,t.release()}}var A=new PrebufferProvider;e.default=A})();var n=exports="undefined"==typeof exports?{}:exports;for(var o in r)n[o]=r[o];r.__esModule&&Object.defineProperty(n,"__esModule",{value:!0})})();
|
|
1
|
+
(()=>{var e={454:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.AutoenableMixinProvider=void 0;var i=function(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var r=n(t);if(r&&r.has(e))return r.get(e);var i={},o=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var s in e)if("default"!==s&&Object.prototype.hasOwnProperty.call(e,s)){var a=o?Object.getOwnPropertyDescriptor(e,s):null;a&&(a.get||a.set)?Object.defineProperty(i,s,a):i[s]=e[s]}i.default=e,r&&r.set(e,i);return i}(r(510));function n(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,r=new WeakMap;return(n=function(e){return e?r:t})(e)}const{systemManager:o}=i.default,s="v4";class AutoenableMixinProvider extends i.ScryptedDeviceBase{constructor(e){var t,r,n;super(e),n={},(r="hasEnabledMixin")in(t=this)?Object.defineProperty(t,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[r]=n;try{this.hasEnabledMixin=JSON.parse(this.storage.getItem("hasEnabledMixin"))}catch(e){this.hasEnabledMixin={}}this.pluginsComponent=o.getComponent("plugins"),o.listen((async(e,t,r)=>{t.eventInterface!==i.ScryptedInterface.ScryptedDevice||t.property||this.maybeEnableMixin(e)}));for(const e of Object.keys(o.getSystemState())){const t=o.getDeviceById(e);this.maybeEnableMixin(t)}}async shouldEnableMixin(e){return!0}async maybeEnableMixin(e){var t;if(!e||null!==(t=e.mixins)&&void 0!==t&&t.includes(this.id))return;if(this.hasEnabledMixin[e.id]===s)return;if(!await this.canMixin(e.type,e.interfaces))return;if(!await this.shouldEnableMixin(e))return;this.log.i("auto enabling mixin for "+e.name);const r=(e.mixins||[]).slice();r.push(this.id);const i=await this.pluginsComponent;await i.setMixins(e.id,r),this.setHasEnabledMixin(e.id)}setHasEnabledMixin(e){this.hasEnabledMixin[e]!==s&&(this.hasEnabledMixin[e]=s,this.storage.setItem("hasEnabledMixin",JSON.stringify(this.hasEnabledMixin)))}}t.AutoenableMixinProvider=AutoenableMixinProvider},201:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.createParserRebroadcaster=async function(e,t,r){return await g(Object.assign({},r,{connect:(r,i)=>(e.on(t,r),()=>{i()})}))},t.createRebroadcaster=g,t.handleRebroadcasterClient=v,t.parseAudioCodec=f,t.parseResolution=p,t.parseVideoCodec=h,t.startParserSession=async function(e,t){const{console:r}=t;let i,a=!0;const u=new s.EventEmitter;let m,g,v,y,S;u.on("error",(e=>r.error("rebroadcast error",e)));const b=new Promise(((e,t)=>{y=e,S=t}));function P(){var e;a&&(u.emit("killed"),u.emit("error",new Error("killed"))),a=!1,null==D||D.kill(),setTimeout((()=>null==D?void 0:D.kill("SIGKILL")),1e3),null===(e=S)||void 0===e||e(new Error("ffmpeg was killed before connecting to the rebroadcast session")),clearTimeout(i)}const M=e.inputArguments.slice();i=setTimeout(P,3e4);const w=e=>{let i;function n(){r.error("timeout waiting for data, killing parser session",e),P()}function o(){t.timeout&&(clearTimeout(i),i=setTimeout(n,t.timeout))}return u.once("killed",(()=>clearTimeout(i))),o(),{resetActivityTimer:o}};let I=!1;const O=["pipe","pipe","pipe"];let x=3;for(const e of Object.keys(t.parsers)){const i=t.parsers[e];if(i.parseDatagram){I=!0;const t=d.default.createSocket("udp4"),r=await(0,o.bindZero)(t),n=d.default.createSocket("udp4");await(0,o.bind)(n,r.port+1),u.once("killed",(()=>{t.close(),n.close()})),M.push(...i.outputArguments,r.url.replace("udp://","rtp://"));const{resetActivityTimer:s}=w(e);(async()=>{for await(const a of i.parseDatagram(t,parseInt(null===(r=v)||void 0===r?void 0:r[2]),parseInt(null===(n=v)||void 0===n?void 0:n[3]))){var r,n,o;null===(o=y)||void 0===o||o(void 0),u.emit(e,a),s()}})(),(async()=>{for await(const a of i.parseDatagram(n,parseInt(null===(t=v)||void 0===t?void 0:t[2]),parseInt(null===(r=v)||void 0===r?void 0:r[3]),"rtcp")){var t,r,o;null===(o=y)||void 0===o||o(void 0),u.emit(e,a),s()}})()}else if(i.tcpProtocol){const t=await(0,o.listenZeroSingleClient)(),n=new URL(i.tcpProtocol);n.port=t.port.toString(),M.push(...i.outputArguments,n.toString());const{resetActivityTimer:s}=w(e);(async()=>{const n=await t.clientPromise;try{for await(const t of i.parse(n,parseInt(null===(o=v)||void 0===o?void 0:o[2]),parseInt(null===(a=v)||void 0===a?void 0:a[3]))){var o,a,c;null===(c=y)||void 0===c||c(void 0),u.emit(e,t),s()}}catch(e){r.error("rebroadcast parse error",e),P()}})()}else M.push(...i.outputArguments,"pipe:"+x++),O.push("pipe")}I&&(M.push("-sdp_file","pipe:"+x++),O.push("pipe"));M.unshift("-hide_banner"),(0,c.safePrintFFmpegArguments)(r,M);const D=n.default.spawn(await l.getFFmpegPath(),M,{stdio:O});let C;(0,c.ffmpegLogInitialOutput)(r,D,void 0,null==t?void 0:t.storage),D.on("exit",P),C=I?new Promise((e=>{const t=[];D.stdio[x-1].on("data",(r=>{t.push(r),e(t)}))})):Promise.resolve([]);let A=0;return Object.keys(t.parsers).forEach((async e=>{const i=t.parsers[e];if(!i.parse||i.tcpProtocol)return;const n=D.stdio[3+A];A++;try{const{resetActivityTimer:t}=w(e);for await(const r of i.parse(n,parseInt(null===(o=v)||void 0===o?void 0:o[2]),parseInt(null===(s=v)||void 0===s?void 0:s[3]))){var o,s,a;null===(a=y)||void 0===a||a(void 0),u.emit(e,r),t()}}catch(e){r.error("rebroadcast parse error",e),P()}})),f(D).then((e=>m=e)),h(D).then((e=>g=e)),p(D).then((e=>v=e)),await b,y=void 0,S=void 0,clearTimeout(i),{sdp:C,inputAudioCodec:m,inputVideoCodec:g,inputVideoResolution:v,isActive:()=>a,kill:P,mediaStreamOptions:e.mediaStreamOptions||{id:void 0,name:void 0},on(e,t){return u.on(e,t),this},once(e,t){return u.once(e,t),this},removeListener(e,t){return u.removeListener(e,t),this}}};var i=r(808),n=u(r(81)),o=r(769),s=r(361),a=u(r(510)),c=r(833),d=u(r(891));function u(e){return e&&e.__esModule?e:{default:e}}const{mediaManager:l}=a.default;async function p(e){return new Promise((t=>{const r=i=>{const n=i.toString(),o=/(([0-9]{2,5})x([0-9]{2,5}))/.exec(n);o&&(e.stdout.removeListener("data",r),e.stderr.removeListener("data",r),t(o))};e.stdout.on("data",r),e.stderr.on("data",r)}))}async function m(e,t){return new Promise((r=>{const i=n=>{const o=n.toString(),s=o.indexOf(`${t}: `);if(-1!==s){const n=o.substring(s+t.length+1).trim();let a=n.indexOf(" ");const c=n.indexOf(",");-1!==a&&c<a&&(a=c),-1!==a&&(e.stdout.removeListener("data",i),e.stderr.removeListener("data",i),r(n.substring(0,a)))}};e.stdout.on("data",i),e.stderr.on("data",i)}))}async function h(e){return m(e,"Video")}async function f(e){return m(e,"Audio")}async function g(e){let t,r=0;const n=()=>{null!=e&&e.idle&&(clearTimeout(t),t=setTimeout((()=>{0!==r?n():e.idle.callback()}),e.idle.timeout))};n();const s=(0,i.createServer)((t=>{v(t,e),t.once("close",(()=>{n(),r--})),n(),r++})),a=await(0,o.listenZero)(s);return{server:s,port:a,url:`tcp://127.0.0.1:${a}`,get clients(){return r}}}async function v(e,t){const r=await e;let i=!0;const n=()=>{r.removeAllListeners(),r.destroy();const e=o;o=void 0,null==e||e()};let o=null==t?void 0:t.connect((e=>{i&&(i=!1,e.startStream&&r.write(e.startStream));for(const t of e.chunks)r.write(t);return r.writableLength}),n);r.once("close",(()=>{n()})),r.on("error",(e=>{var r;return null==t||null===(r=t.console)||void 0===r?void 0:r.log("client stream ended")}))}},769:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.bind=async function(e,t){return e.bind(t),await(0,n.once)(e,"listening"),{port:t,url:`udp://127.0.0.1:${t}`}},t.bindUdp=c,t.bindZero=async function(e){return c(e,0)},t.createBindUdp=d,t.createBindZero=async function(){return d(0)},t.listenZero=a,t.listenZeroSingleClient=async function(){const e=new i.default.Server,t=await a(e),r=new Promise(((t,r)=>{const i=setTimeout((()=>{r(new Error("timeout waiting for client"))}),3e4);e.on("connection",(r=>{e.close(),clearTimeout(i),t(r)}))}));return{url:`tcp://127.0.0.1:${t}`,port:t,clientPromise:r}};var i=s(r(808)),n=r(361),o=s(r(891));function s(e){return e&&e.__esModule?e:{default:e}}async function a(e){return e.listen(0),await(0,n.once)(e,"listening"),e.address().port}async function c(e,t){e.bind(t),await(0,n.once)(e,"listening");const r=e.address().port;return{port:r,url:`udp://127.0.0.1:${r}`}}async function d(e){const t=o.default.createSocket("udp4"),{port:r,url:i}=await c(t,e);return{server:t,port:r,url:i}}},833:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0});var i=r(568);Object.keys(i).forEach((function(e){"default"!==e&&"__esModule"!==e&&(e in t&&t[e]===i[e]||Object.defineProperty(t,e,{enumerable:!0,get:function(){return i[e]}}))}))},961:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.readLength=n,t.readLine=async function(e){return s(e,o)},t.readString=async function(e){let t="";return(e=await e).on("data",(e=>{t+=e.toString()})),e.resume(),await(0,i.once)(e,"end"),t},t.readUntil=s;var i=r(361);async function n(e,t){if(!t)return Buffer.alloc(0);{const r=e.read(t);if(r)return r}return new Promise(((r,i)=>{const n=()=>{const i=e.read(t);i&&(s(),r(i))},o=()=>{s(),i(new Error(`stream ended during read for minimum ${t} bytes`))},s=()=>{e.removeListener("readable",n),e.removeListener("end",o)};e.on("readable",n),e.on("end",o)}))}const o="\n".charCodeAt(0);async function s(e,t){const r=[];let i=0;for(;;){const o=await n(e,1);if(!o)throw new Error("end of stream");if(o[0]===t)break;r[i++]=o[0]}return Buffer.from(r).toString()}},175:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.RtspServer=t.RTSP_FRAME_MAGIC=void 0,t.createRtspParser=function(){let e;return{container:"rtsp",tcpProtocol:"rtsp://127.0.0.1/"+(0,n.randomBytes)(8).toString("hex"),inputArguments:["-rtsp_transport","tcp"],outputArguments:["-rtsp_transport","tcp","-vcodec","copy","-acodec","copy","-f","rtsp"],findSyncFrame:s,sdp:new Promise((t=>e=t)),async*parse(t,r,i){const n=new RtspServer(t);await n.handleSetup(),e(n.sdp);for await(const{type:e,rtcp:t,header:o,packet:s}of n.handleRecord())yield{chunks:[o,s],type:e,width:r,height:i}}}};var i=r(961),n=r(113);function o(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function s(e){return e}t.RTSP_FRAME_MAGIC=36;class RtspServer{constructor(e,t,r){o(this,"videoChannel",0),o(this,"audioChannel",2),o(this,"udpPorts",{video:0,audio:0}),this.client=e,this.sdp=t,this.udp=r,this.session=(0,n.randomBytes)(4).toString("hex"),t&&(t=t.trim())}async handleSetup(){let e=[];for(;;){let t=await(0,i.readLine)(this.client);if(t=t.trim(),t)e.push(t);else{if(!await this.headers(e))break;e=[]}}}async handlePlayback(){return this.handleSetup()}async*handleRecord(){for(;;){const e=await(0,i.readLength)(this.client,4),t=e.readUInt16BE(2),r=await(0,i.readLength)(this.client,t),n=e.readUInt8(1);yield{type:n-n%2===this.videoChannel?"video":"audio",rtcp:n%2==1,header:e,packet:r}}}send(e,t){const r=Buffer.alloc(4);r.writeUInt8(36,0),r.writeUInt8(t,1),r.writeUInt16BE(e.length,2),this.client.write(r),this.client.write(Buffer.from(e))}sendUdp(e,t,r){this.udp.send(t,r?e+1:e,"127.0.0.1")}sendVideo(e,t){this.udp?this.sendUdp(this.udpPorts.video,e,t):this.send(e,t?this.videoChannel+1:this.videoChannel)}sendAudio(e,t){this.udp?this.sendUdp(this.udpPorts.audio,e,t):this.send(e,t?this.audioChannel+1:this.audioChannel)}options(e,t){const r={Public:"DESCRIBE, OPTIONS, PAUSE, PLAY, SETUP, TEARDOWN, ANNOUNCE, RECORD"};this.respond(200,"OK",t,r)}describe(e,t){const r={};r["Content-Base"]=e,r["Content-Type"]="application/sdp",this.respond(200,"OK",t,r,Buffer.from(this.sdp))}setup(e,t){const r={},i=t.transport;if(r.Transport=t.transport,r.Session=this.session,i.includes("UDP")){const t=i.match(/.*?client_port=([0-9]+)-([0-9]+)/),[r,n,o]=t;e.includes("audio")?this.udpPorts.audio=parseInt(n):this.udpPorts.video=parseInt(n)}else if(i.includes("TCP")){const t=i.match(/.*?interleaved=([0-9]+)-([0-9]+)/);if(t){const r=parseInt(t[1]);parseInt(t[2]);e.includes("audio")?this.audioChannel=r:this.videoChannel=r}}this.respond(200,"OK",t,r)}play(e,t){const r={};r["RTP-Info"]=`url=${e}/trackID=0;seq=0;rtptime=0,url=${e}/trackID=1;seq=0;rtptime=0`,r.Range="npt=now-",r.Session=this.session,this.respond(200,"OK",t,r)}async announce(e,t){const r=parseInt(t["content-length"]),n=await(0,i.readLength)(this.client,r);this.sdp=n.toString();const o={};o.Session=this.session,this.respond(200,"OK",t,o)}async record(e,t){const r={};r.Session=this.session,this.respond(200,"OK",t,r)}async teardown(e,t){const r={};r.Session=this.session,this.respond(200,"OK",t,r)}async headers(e){var t,r;null===(t=this.console)||void 0===t||t.log("request header",e.join("\n"));let[i,n]=e[0].split(" ",2);i=i.toLowerCase();const o=function(e){const t={};for(const r of e.slice(1)){const e=r.indexOf(":");let i="";-1!==e&&(i=r.substring(e+1).trim()),t[r.substring(0,e).toLowerCase()]=i}return t}(e);if(null===(r=this.console)||void 0===r||r.log("request headers",o),this[i])return await this[i](n,o),"play"!==i&&"record"!==i;this.respond(400,"Bad Request",o,{})}respond(e,t,r,i,n){var o;let s=`RTSP/1.0 ${e} ${t}\r\n`;r.cseq&&(i.CSeq=r.cseq),n&&(i["Content-Length"]=n.length.toString());for(const[e,t]of Object.entries(i))s+=`${e}: ${t}\r\n`;null===(o=this.console)||void 0===o||o.log("response header",s),s+="\r\n",this.client.write(s),n&&this.client.write(n)}}t.RtspServer=RtspServer},567:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.SettingsMixinDeviceBase=void 0;var i=function(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var r=n(t);if(r&&r.has(e))return r.get(e);var i={},o=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var s in e)if("default"!==s&&Object.prototype.hasOwnProperty.call(e,s)){var a=o?Object.getOwnPropertyDescriptor(e,s):null;a&&(a.get||a.set)?Object.defineProperty(i,s,a):i[s]=e[s]}i.default=e,r&&r.set(e,i);return i}(r(510));function n(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,r=new WeakMap;return(n=function(e){return e?r:t})(e)}const{deviceManager:o}=i.default;class SettingsMixinDeviceBase extends i.MixinDeviceBase{constructor(e,t,r){super(e,r.mixinDeviceInterfaces,t,r.providerNativeId,r.mixinStorageSuffix),this.settingsGroup=r.group,this.settingsGroupKey=r.groupKey,process.nextTick((()=>o.onMixinEvent(this.id,this,i.ScryptedInterface.Settings,null)))}async getSettings(){const e=this.mixinDeviceInterfaces.includes(i.ScryptedInterface.Settings)?this.mixinDevice.getSettings():void 0,t=this.getMixinSettings(),r=[];try{const t=await e||[];r.push(...t)}catch(e){const t=this.name,i=`${t} Extension settings failed to load.`;this.console.error(i,e),r.push({key:Math.random().toString(),title:t,value:"Settings Error",group:"Errors",description:i,readonly:!0})}try{const e=await t||[];for(const t of e)t.group=t.group||this.settingsGroup,t.key=this.settingsGroupKey+":"+t.key;r.push(...e)}catch(e){const t=o.getDeviceState(this.mixinProviderNativeId).name,i=`${t} Extension settings failed to load.`;this.console.error(i,e),r.push({key:Math.random().toString(),title:t,value:"Settings Error",group:"Errors",description:i,readonly:!0})}return r}async putSetting(e,t){const r=this.settingsGroupKey+":";if(null==e||!e.startsWith(r))return this.mixinDevice.putSetting(e,t);await this.putMixinSetting(e.substring(r.length),t),o.onMixinEvent(this.id,this,i.ScryptedInterface.Settings,null)}release(){o.onMixinEvent(this.id,this,i.ScryptedInterface.Settings,null)}}t.SettingsMixinDeviceBase=SettingsMixinDeviceBase},129:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.PIXEL_FORMAT_YUV420P=t.PIXEL_FORMAT_RGB24=void 0,t.createDgramParser=a,t.createFragmentedMp4Parser=function(e){return{container:"mp4",outputArguments:[...(null==e?void 0:e.vcodec)||[],...(null==e?void 0:e.acodec)||[],"-movflags","frag_keyframe+empty_moov+default_base_moof","-f","mp4"],async*parse(e){const t=c(e);let r,i,n;for await(const e of t)r?i||(i=e):r=e,yield{startStream:n,chunks:[e.header,e.data],type:e.type},r&&i&&!n&&(n=Buffer.concat([r.header,r.data,i.header,i.data]))},findSyncFrame:o}},t.createMpegTsParser=function(e){return{container:"mpegts",outputArguments:[...(null==e?void 0:e.vcodec)||[],...(null==e?void 0:e.acodec)||[],"-f","mpegts"],parse:s(188,(e=>{if(71!=e[0])throw new Error("Invalid sync byte in mpeg-ts packet. Terminating stream.")})),findSyncFrame(e){for(let t=0;t<e.length;t++){const r=e[t];for(let i=0;i<r.chunks.length;i++){const n=r.chunks[i];let o=0;for(;o+188<n.length;){const r=n.subarray(o,o+188);if(256==((31&r[1])<<8|r[2])&&32&r[3]&&r[4]>0&&64&r[5])return e.slice(t);o+=188}}}return e}}},t.createRawVideoParser=function(e){var t;const r=(null===(t=e)||void 0===t?void 0:t.pixelFormat)||d;let i;e=e||{};const{size:s,everyNFrames:a}=e;s&&(i=`scale=${s.width}:${s.height}`);a&&a>1&&(i?i+=",":i="",i+=`select=not(mod(n\\,${a}))`);return{container:"rawvideo",outputArguments:[...i?["-vf",i]:[],"-an","-vcodec","rawvideo","-pix_fmt",r.name,"-f","rawvideo"],async*parse(e,t,i){if(!t||!i)throw new Error("error parsing rawvideo, unknown width and height");t=(null==s?void 0:s.width)||t,i=(null==s?void 0:s.height)||i;const o=r.computeLength(t,i);for(;;){const r=await(0,n.readLength)(e,o);yield{chunks:[r],width:t,height:i}}},findSyncFrame:o}},t.createRtpParser=function(...e){return{container:"rtsp",inputArguments:["-v","verbose","-rtsp_transport","tcp"],outputArguments:[...e,"-f","rtp"],parseDatagram:a(),findSyncFrame:o}},t.parseFragmentedMP4=c;var i=r(361),n=r(961);function o(e){return e}function s(e,t){return async function*(r){let n=[],o=0;for(;;){const s=r.read();if(!s){await(0,i.once)(r,"readable");continue}if(n.push(s),o+=s.length,o<e)continue;const a=Buffer.concat(n);null==t||t(a);const c=a.length%e,d=a.slice(0,a.length-c),u=a.slice(a.length-c);n=[u],o=u.length,yield{chunks:[d]}}}}function a(){return async function*(e,t,r,n){for(;;){const[t]=await(0,i.once)(e,"message");yield{chunks:[t],type:n}}}}async function*c(e){for(;;){const t=await(0,n.readLength)(e,8),r=t.readInt32BE(0)-8,i=t.slice(4).toString(),o=await(0,n.readLength)(e,r);yield{header:t,length:r,type:i,data:o}}}const d={name:"yuv420p",computeLength:(e,t)=>e*t*1.5};t.PIXEL_FORMAT_YUV420P=d;t.PIXEL_FORMAT_RGB24={name:"rgb24",computeLength:(e,t)=>e*t*3}},653:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.startRFC4571Parser=async function(e,t){var r,n;let c=!0;const u=new o.EventEmitter,l=await d.convertMediaObjectToJSON(e,"x-scrypted/x-rfc4571"),{url:p,sdp:m}=l,h=parseInt(null===(r=m.match(/m=audio.* ([0-9]+)/))||void 0===r?void 0:r[1]),f=parseInt(null===(n=m.match(/m=video.* ([0-9]+)/))||void 0===n?void 0:n[1]),g=new URL(p);if(!g.protocol.startsWith("tcp"))throw new Error("rfc4751 url must be tcp");const v=s.default.connect(parseInt(g.port),g.hostname),y=()=>{c&&(u.emit("killed"),u.emit("error",new Error("killed"))),c=!1,v.destroy()};return v.on("close",y),v.on("error",y),(async()=>{for(;;){const e=await(0,i.readLength)(v,2),t=e.readInt16BE(0),r=await(0,i.readLength)(v,t),n=127&r[1],o=Buffer.alloc(2);o[0]=a.RTSP_FRAME_MAGIC,n===h?o[1]=0:n===f&&(o[1]=2);const s={chunks:[o,e,r]};u.emit("rtsp",s)}})().finally(y),{sdp:Promise.resolve([Buffer.from(m)]),inputAudioCodec:"pcm",inputVideoCodec:"h264",inputVideoResolution:void 0,isActive:()=>c,kill:y,mediaStreamOptions:l.mediaStreamOptions,on(e,t){return u.on(e,t),this},once(e,t){return u.once(e,t),this},removeListener(e,t){return u.removeListener(e,t),this}}};var i=r(961),n=c(r(510)),o=r(781),s=c(r(808)),a=r(175);function c(e){return e&&e.__esModule?e:{default:e}}const{mediaManager:d}=n.default},510:(e,t,r)=>{"use strict";var i=Object.create?function(e,t,r,i){void 0===i&&(i=r),Object.defineProperty(e,i,{enumerable:!0,get:function(){return t[r]}})}:function(e,t,r,i){void 0===i&&(i=r),e[i]=t[r]},n=function(e,t){for(var r in e)"default"===r||Object.prototype.hasOwnProperty.call(t,r)||i(t,e,r)};Object.defineProperty(t,"__esModule",{value:!0}),t.MixinDeviceBase=t.ScryptedDeviceBase=void 0,n(r(632),t);const o=r(632);class ScryptedDeviceBase extends o.DeviceBase{constructor(e){super(),this.nativeId=e}get storage(){return this._storage||(this._storage=deviceManager.getDeviceStorage(this.nativeId)),this._storage}get log(){return this._log||(this._log=deviceManager.getDeviceLogger(this.nativeId)),this._log}get console(){return this._console||(this._console=deviceManager.getDeviceConsole(this.nativeId)),this._console}_lazyLoadDeviceState(){this._deviceState||(this.nativeId?this._deviceState=deviceManager.getDeviceState(this.nativeId):this._deviceState=deviceManager.getDeviceState())}onDeviceEvent(e,t){return deviceManager.onDeviceEvent(this.nativeId,e,t)}}t.ScryptedDeviceBase=ScryptedDeviceBase;class MixinDeviceBase extends o.DeviceBase{constructor(e,t,r,i,n){super(),this.mixinDevice=e,this.mixinDeviceInterfaces=t,this.mixinProviderNativeId=i,this._mixinStorageSuffix=n,this._listeners=new Set,this._deviceState=r}get storage(){if(!this._storage){const e=this._mixinStorageSuffix,t=this.id+(e?":"+e:"");this._storage=deviceManager.getMixinStorage(t,this.mixinProviderNativeId)}return this._storage}get console(){return this._console||(deviceManager.getMixinConsole?this._console=deviceManager.getMixinConsole(this.id,this.mixinProviderNativeId):this._console=deviceManager.getDeviceConsole(this.mixinProviderNativeId)),this._console}onDeviceEvent(e,t){return deviceManager.onMixinEvent(this.id,this,e,t)}_lazyLoadDeviceState(){}manageListener(e){this._listeners.add(e)}release(){for(const e of this._listeners)e.removeListener()}}t.MixinDeviceBase=MixinDeviceBase,function(){function e(e){return function(){return this._lazyLoadDeviceState(),this._deviceState[e]}}function t(e){return function(t){this._lazyLoadDeviceState(),this._deviceState[e]=t}}for(var r of Object.values(o.ScryptedInterfaceProperty))Object.defineProperty(ScryptedDeviceBase.prototype,r,{set:t(r),get:e(r)}),Object.defineProperty(MixinDeviceBase.prototype,r,{set:t(r),get:e(r)})}();let s={};try{s=Object.assign(s,{log:deviceManager.getDeviceLogger(void 0),deviceManager,endpointManager,mediaManager,systemManager,pluginHostAPI})}catch(e){console.error("sdk initialization error, import @scrypted/types or use @scrypted/web-sdk instead",e)}t.default=s},632:(e,t)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.ScryptedMimeTypes=t.ScryptedInterface=t.MediaPlayerState=t.LockState=t.ThermostatMode=t.TemperatureUnit=t.FanMode=t.HumidityMode=t.ScryptedDeviceType=t.ScryptedInterfaceDescriptors=t.ScryptedInterfaceProperty=t.DeviceBase=void 0;t.DeviceBase=class DeviceBase{},function(e){e.id="id",e.info="info",e.interfaces="interfaces",e.mixins="mixins",e.name="name",e.providedInterfaces="providedInterfaces",e.providedName="providedName",e.providedRoom="providedRoom",e.providedType="providedType",e.providerId="providerId",e.room="room",e.type="type",e.on="on",e.brightness="brightness",e.colorTemperature="colorTemperature",e.rgb="rgb",e.hsv="hsv",e.running="running",e.paused="paused",e.docked="docked",e.thermostatActiveMode="thermostatActiveMode",e.thermostatAvailableModes="thermostatAvailableModes",e.thermostatMode="thermostatMode",e.thermostatSetpoint="thermostatSetpoint",e.thermostatSetpointHigh="thermostatSetpointHigh",e.thermostatSetpointLow="thermostatSetpointLow",e.temperature="temperature",e.temperatureUnit="temperatureUnit",e.humidity="humidity",e.lockState="lockState",e.entryOpen="entryOpen",e.batteryLevel="batteryLevel",e.online="online",e.updateAvailable="updateAvailable",e.fromMimeType="fromMimeType",e.toMimeType="toMimeType",e.binaryState="binaryState",e.intrusionDetected="intrusionDetected",e.powerDetected="powerDetected",e.audioDetected="audioDetected",e.motionDetected="motionDetected",e.ambientLight="ambientLight",e.occupied="occupied",e.flooded="flooded",e.ultraviolet="ultraviolet",e.luminance="luminance",e.position="position",e.humiditySetting="humiditySetting",e.fan="fan"}(t.ScryptedInterfaceProperty||(t.ScryptedInterfaceProperty={})),t.ScryptedInterfaceDescriptors={ScryptedDevice:{name:"ScryptedDevice",methods:["listen","probe","setName","setRoom","setType"],properties:["id","info","interfaces","mixins","name","providedInterfaces","providedName","providedRoom","providedType","providerId","room","type"]},ScryptedPlugin:{name:"ScryptedPlugin",methods:["getPluginJson"],properties:[]},OnOff:{name:"OnOff",methods:["turnOff","turnOn"],properties:["on"]},Brightness:{name:"Brightness",methods:["setBrightness"],properties:["brightness"]},ColorSettingTemperature:{name:"ColorSettingTemperature",methods:["getTemperatureMaxK","getTemperatureMinK","setColorTemperature"],properties:["colorTemperature"]},ColorSettingRgb:{name:"ColorSettingRgb",methods:["setRgb"],properties:["rgb"]},ColorSettingHsv:{name:"ColorSettingHsv",methods:["setHsv"],properties:["hsv"]},Notifier:{name:"Notifier",methods:["sendNotification"],properties:[]},StartStop:{name:"StartStop",methods:["start","stop"],properties:["running"]},Pause:{name:"Pause",methods:["pause","resume"],properties:["paused"]},Dock:{name:"Dock",methods:["dock"],properties:["docked"]},TemperatureSetting:{name:"TemperatureSetting",methods:["setThermostatMode","setThermostatSetpoint","setThermostatSetpointHigh","setThermostatSetpointLow"],properties:["thermostatActiveMode","thermostatAvailableModes","thermostatMode","thermostatSetpoint","thermostatSetpointHigh","thermostatSetpointLow"]},Thermometer:{name:"Thermometer",methods:["setTemperatureUnit"],properties:["temperature","temperatureUnit"]},HumiditySensor:{name:"HumiditySensor",methods:[],properties:["humidity"]},Camera:{name:"Camera",methods:["getPictureOptions","takePicture"],properties:[]},VideoCamera:{name:"VideoCamera",methods:["getVideoStream","getVideoStreamOptions"],properties:[]},VideoCameraConfiguration:{name:"VideoCameraConfiguration",methods:["setVideoStreamOptions"],properties:[]},Intercom:{name:"Intercom",methods:["startIntercom","stopIntercom"],properties:[]},Lock:{name:"Lock",methods:["lock","unlock"],properties:["lockState"]},PasswordStore:{name:"PasswordStore",methods:["addPassword","getPasswords","removePassword"],properties:[]},Authenticator:{name:"Authenticator",methods:["checkPassword"],properties:[]},Scene:{name:"Scene",methods:["activate","deactivate","isReversible"],properties:[]},Entry:{name:"Entry",methods:["closeEntry","openEntry"],properties:[]},EntrySensor:{name:"EntrySensor",methods:[],properties:["entryOpen"]},DeviceProvider:{name:"DeviceProvider",methods:["getDevice"],properties:[]},DeviceDiscovery:{name:"DeviceDiscovery",methods:["discoverDevices"],properties:[]},DeviceCreator:{name:"DeviceCreator",methods:["createDevice","getCreateDeviceSettings"],properties:[]},Battery:{name:"Battery",methods:[],properties:["batteryLevel"]},Refresh:{name:"Refresh",methods:["getRefreshFrequency","refresh"],properties:[]},MediaPlayer:{name:"MediaPlayer",methods:["getMediaStatus","load","seek","skipNext","skipPrevious"],properties:[]},Online:{name:"Online",methods:[],properties:["online"]},SoftwareUpdate:{name:"SoftwareUpdate",methods:["checkForUpdate","installUpdate"],properties:["updateAvailable"]},BufferConverter:{name:"BufferConverter",methods:["convert"],properties:["fromMimeType","toMimeType"]},Settings:{name:"Settings",methods:["getSettings","putSetting"],properties:[]},BinarySensor:{name:"BinarySensor",methods:[],properties:["binaryState"]},IntrusionSensor:{name:"IntrusionSensor",methods:[],properties:["intrusionDetected"]},PowerSensor:{name:"PowerSensor",methods:[],properties:["powerDetected"]},AudioSensor:{name:"AudioSensor",methods:[],properties:["audioDetected"]},MotionSensor:{name:"MotionSensor",methods:[],properties:["motionDetected"]},AmbientLightSensor:{name:"AmbientLightSensor",methods:[],properties:["ambientLight"]},OccupancySensor:{name:"OccupancySensor",methods:[],properties:["occupied"]},FloodSensor:{name:"FloodSensor",methods:[],properties:["flooded"]},UltravioletSensor:{name:"UltravioletSensor",methods:[],properties:["ultraviolet"]},LuminanceSensor:{name:"LuminanceSensor",methods:[],properties:["luminance"]},PositionSensor:{name:"PositionSensor",methods:[],properties:["position"]},Readme:{name:"Readme",methods:["getReadmeMarkdown"],properties:[]},OauthClient:{name:"OauthClient",methods:["getOauthUrl","onOauthCallback"],properties:[]},MixinProvider:{name:"MixinProvider",methods:["canMixin","getMixin","releaseMixin"],properties:[]},HttpRequestHandler:{name:"HttpRequestHandler",methods:["onRequest"],properties:[]},EngineIOHandler:{name:"EngineIOHandler",methods:["onConnection"],properties:[]},PushHandler:{name:"PushHandler",methods:["onPush"],properties:[]},Program:{name:"Program",methods:["run"],properties:[]},Scriptable:{name:"Scriptable",methods:["eval","loadScripts","saveScript"],properties:[]},ObjectDetector:{name:"ObjectDetector",methods:["getDetectionInput","getObjectTypes"],properties:[]},ObjectDetection:{name:"ObjectDetection",methods:["detectObjects","getDetectionModel"],properties:[]},HumiditySetting:{name:"HumiditySetting",methods:["setHumidity"],properties:["humiditySetting"]},Fan:{name:"Fan",methods:["setFan"],properties:["fan"]}},function(e){e.Builtin="Builtin",e.Camera="Camera",e.Fan="Fan",e.Light="Light",e.Switch="Switch",e.Outlet="Outlet",e.Sensor="Sensor",e.Scene="Scene",e.Program="Program",e.Automation="Automation",e.Vacuum="Vacuum",e.Notifier="Notifier",e.Thermostat="Thermostat",e.Lock="Lock",e.PasswordControl="PasswordControl",e.Display="Display",e.Speaker="Speaker",e.Event="Event",e.Entry="Entry",e.Garage="Garage",e.DeviceProvider="DeviceProvider",e.DataSource="DataSource",e.API="API",e.Doorbell="Doorbell",e.Irrigation="Irrigation",e.Valve="Valve",e.Person="Person",e.Unknown="Unknown"}(t.ScryptedDeviceType||(t.ScryptedDeviceType={})),function(e){e.Humidify="Humidify",e.Dehumidify="Dehumidify",e.Auto="Auto",e.Off="Off"}(t.HumidityMode||(t.HumidityMode={})),function(e){e.Auto="Auto",e.Manual="Manual"}(t.FanMode||(t.FanMode={})),function(e){e.C="C",e.F="F"}(t.TemperatureUnit||(t.TemperatureUnit={})),function(e){e.Off="Off",e.Cool="Cool",e.Heat="Heat",e.HeatCool="HeatCool",e.Auto="Auto",e.FanOnly="FanOnly",e.Purifier="Purifier",e.Eco="Eco",e.Dry="Dry",e.On="On"}(t.ThermostatMode||(t.ThermostatMode={})),function(e){e.Locked="Locked",e.Unlocked="Unlocked",e.Jammed="Jammed"}(t.LockState||(t.LockState={})),function(e){e.Idle="Idle",e.Playing="Playing",e.Paused="Paused",e.Buffering="Buffering"}(t.MediaPlayerState||(t.MediaPlayerState={})),function(e){e.ScryptedDevice="ScryptedDevice",e.ScryptedPlugin="ScryptedPlugin",e.OnOff="OnOff",e.Brightness="Brightness",e.ColorSettingTemperature="ColorSettingTemperature",e.ColorSettingRgb="ColorSettingRgb",e.ColorSettingHsv="ColorSettingHsv",e.Notifier="Notifier",e.StartStop="StartStop",e.Pause="Pause",e.Dock="Dock",e.TemperatureSetting="TemperatureSetting",e.Thermometer="Thermometer",e.HumiditySensor="HumiditySensor",e.Camera="Camera",e.VideoCamera="VideoCamera",e.VideoCameraConfiguration="VideoCameraConfiguration",e.Intercom="Intercom",e.Lock="Lock",e.PasswordStore="PasswordStore",e.Authenticator="Authenticator",e.Scene="Scene",e.Entry="Entry",e.EntrySensor="EntrySensor",e.DeviceProvider="DeviceProvider",e.DeviceDiscovery="DeviceDiscovery",e.DeviceCreator="DeviceCreator",e.Battery="Battery",e.Refresh="Refresh",e.MediaPlayer="MediaPlayer",e.Online="Online",e.SoftwareUpdate="SoftwareUpdate",e.BufferConverter="BufferConverter",e.Settings="Settings",e.BinarySensor="BinarySensor",e.IntrusionSensor="IntrusionSensor",e.PowerSensor="PowerSensor",e.AudioSensor="AudioSensor",e.MotionSensor="MotionSensor",e.AmbientLightSensor="AmbientLightSensor",e.OccupancySensor="OccupancySensor",e.FloodSensor="FloodSensor",e.UltravioletSensor="UltravioletSensor",e.LuminanceSensor="LuminanceSensor",e.PositionSensor="PositionSensor",e.Readme="Readme",e.OauthClient="OauthClient",e.MixinProvider="MixinProvider",e.HttpRequestHandler="HttpRequestHandler",e.EngineIOHandler="EngineIOHandler",e.PushHandler="PushHandler",e.Program="Program",e.Scriptable="Scriptable",e.ObjectDetector="ObjectDetector",e.ObjectDetection="ObjectDetection",e.HumiditySetting="HumiditySetting",e.Fan="Fan"}(t.ScryptedInterface||(t.ScryptedInterface={})),function(e){e.AcceptUrlParameter="accept-url",e.Url="text/x-uri",e.InsecureLocalUrl="text/x-insecure-local-uri",e.LocalUrl="text/x-local-uri",e.PushEndpoint="text/x-push-endpoint",e.MediaStreamUrl="text/x-media-url",e.FFmpegInput="x-scrypted/x-ffmpeg-input",e.RTCAVSignalingPrefix="x-scrypted-rtc-signaling-",e.RTCAVOffer="x-scrypted/x-rtc-av-offer",e.RTCAVAnswer="x-scrypted/x-rtc-av-answer",e.RTCAVSignalingOfferSetup="x-scrypted/x-rtc-av-signalling-offer-setup",e.SchemePrefix="x-scrypted/x-scrypted-scheme-",e.MediaObject="x-scrypted/x-scrypted-media-object"}(t.ScryptedMimeTypes||(t.ScryptedMimeTypes={}))},568:(e,t,r)=>{"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.ffmpegLogInitialOutput=function(e,t,r,i){var s,a;const c=!!n.default.env.SCRYPTED_FFMPEG_NOISY||!(null==i||!i.getItem("SCRYPTED_FFMPEG_NOISY"));function d(e){const i=n=>{const s=n.toString();for(const e of o)if(-1!==s.indexOf(e))return;if(!(c||r||-1===s.indexOf("frame=")&&-1===s.indexOf("size=")))return e(s),e("video/audio detected, discarding further input"),t.stdout.removeListener("data",i),void t.stderr.removeListener("data",i);e(s)};return i}null===(s=t.stdout)||void 0===s||s.on("data",d(e.log)),null===(a=t.stderr)||void 0===a||a.on("data",d(e.error)),t.on("exit",(()=>e.log("ffmpeg exited")))},t.safePrintFFmpegArguments=function(e,t){const r=[];for(const e of t)try{const t=new URL(e);r.push(`${t.protocol}[REDACTED]`)}catch(t){r.push(e)}e.log(r.join(" "))};var i,n=(i=r(282))&&i.__esModule?i:{default:i};const o=["decode_slice_header error","no frame!","non-existing PPS"]},81:e=>{"use strict";e.exports=require("child_process")},113:e=>{"use strict";e.exports=require("crypto")},891:e=>{"use strict";e.exports=require("dgram")},361:e=>{"use strict";e.exports=require("events")},808:e=>{"use strict";e.exports=require("net")},282:e=>{"use strict";e.exports=require("process")},781:e=>{"use strict";e.exports=require("stream")}},t={};function r(i){var n=t[i];if(void 0!==n)return n.exports;var o=t[i]={exports:{}};return e[i](o,o.exports,r),o.exports}var i={};(()=>{"use strict";var e=i;Object.defineProperty(e,"__esModule",{value:!0}),e.default=void 0;var t,n=function(e,t){if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var r=f(t);if(r&&r.has(e))return r.get(e);var i={},n=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var o in e)if("default"!==o&&Object.prototype.hasOwnProperty.call(e,o)){var s=n?Object.getOwnPropertyDescriptor(e,o):null;s&&(s.get||s.set)?Object.defineProperty(i,o,s):i[o]=e[o]}i.default=e,r&&r.set(e,i);return i}(r(510)),o=r(361),s=r(567),a=r(201),c=r(129),d=r(454),u=r(769),l=r(175),p=(t=r(808))&&t.__esModule?t:{default:t},m=r(961),h=r(653);function f(e){if("function"!=typeof WeakMap)return null;var t=new WeakMap,r=new WeakMap;return(f=function(e){return e?r:t})(e)}function g(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}const{mediaManager:v,log:y,systemManager:S,deviceManager:b}=n.default,P=1e4,M="prebufferDuration",w="sendKeyframe",I="Default",O="AAC or No Audio",x=`${O} (Copy)`,D="Compatible Audio",C="Other Audio",A=["aac","mp3","mp2","opus"],k="-fflags +genpts",T=[O,D,C],_=["mpegts","mp4","rtsp"];class PrebufferSession{constructor(e,t,r,i){g(this,"prebuffers",{mp4:[],mpegts:[],rtsp:[]}),g(this,"detectedIdrInterval",0),g(this,"prevIdr",0),g(this,"audioDisabled",!1),g(this,"activeClients",0),this.mixin=e,this.streamName=t,this.streamId=r,this.stopInactive=i,this.storage=e.storage,this.console=e.console,this.mixinDevice=e.mixinDevice,this.audioConfigurationKey="audioConfiguration-"+this.streamId,this.ffmpegInputArgumentsKey="ffmpegInputArguments-"+this.streamId,this.rebroadcastModeKey="rebroadcastMode-"+this.streamId,this.lastDetectedAudioCodecKey="lastDetectedAudioCodec-"+this.streamId}clearPrebuffers(){this.prebuffers.mp4=[],this.prebuffers.mpegts=[],this.prebuffers.rtsp=[]}ensurePrebufferSession(){this.parserSessionPromise||this.mixin.released||(this.console.log(this.streamName,"prebuffer session started"),this.parserSessionPromise=this.startPrebufferSession(),this.parserSessionPromise.catch((()=>this.parserSessionPromise=void 0)))}getAudioConfig(){let e=this.storage.getItem(this.audioConfigurationKey)||"";T.find((t=>e.startsWith(t)))||(e="");const t=-1!==e.indexOf(O),r=-1!==e.indexOf(D),i=-1!==e.indexOf(C);return{isUsingDefaultAudioConfig:!(t||r||i),aacAudio:t,compatibleAudio:r,reencodeAudio:i}}async getMixinSettings(){const e=[],t=this.parserSession;let r=0,i=0;for(const e of this.prebuffers.mp4){i=i||e.time;for(const t of e.chunk.chunks)r+=t.byteLength}const n=Date.now()-i,o=Math.round(r/n*8),s=this.streamName?`Rebroadcast: ${this.streamName}`:"Rebroadcast";var a,c,d;(e.push({title:"Audio Codec Transcoding",group:s,description:"Configuring your camera to output AAC, MP3, MP2, or Opus is recommended. PCM/G711 cameras should set this to Transcode.",type:"string",key:this.audioConfigurationKey,value:this.storage.getItem(this.audioConfigurationKey)||I,choices:[I,x,"Compatible Audio (Copy)","Other Audio (Transcode)"]},{title:"FFmpeg Input Arguments Prefix",group:s,description:"Optional/Advanced: Additional input arguments to pass to the ffmpeg command. These will be placed before the input arguments.",key:this.ffmpegInputArgumentsKey,value:this.storage.getItem(this.ffmpegInputArgumentsKey),placeholder:k,choices:[k,"-use_wallclock_as_timestamps 1","-v verbose"],combobox:!0},{title:"Rebroadcast Mode",group:s,description:"THIS FEATURE IS IN TESTING. DO NOT CHANGE THIS FROM MPEG-TS. The stream format to use when rebroadcasting.",placeholder:"MPEG-TS",choices:["MPEG-TS","RTSP","RTSP+MP4"],key:this.rebroadcastModeKey,value:this.storage.getItem(this.rebroadcastModeKey)||"MPEG-TS"}),t)?e.push({key:"detectedResolution",group:s,title:"Detected Resolution and Bitrate",readonly:!0,value:`${(null==t||null===(a=t.inputVideoResolution)||void 0===a?void 0:a[0])||"unknown"} @ ${o||"unknown"} Kb/s`,description:"Configuring your camera to 1920x1080, 2000Kb/S, Variable Bit Rate, is recommended."},{key:"detectedCodec",group:s,title:"Detected Video/Audio Codecs",readonly:!0,value:((null==t||null===(c=t.inputVideoCodec)||void 0===c?void 0:c.toString())||"unknown")+"/"+((null==t||null===(d=t.inputAudioCodec)||void 0===d?void 0:d.toString())||"unknown"),description:"Configuring your camera to H264 video and AAC/MP3/MP2/Opus audio is recommended."},{key:"detectedKeyframe",group:s,title:"Detected Keyframe Interval",description:"Configuring your camera to 4 seconds is recommended (IDR aka Frame Interval = FPS * 4 seconds).",readonly:!0,value:((this.detectedIdrInterval||0)/1e3).toString()||"none"}):e.push({title:"Status",group:s,key:"status",description:"Rebroadcast is currently idle and will be started automatically on demand.",value:"Idle",readonly:!0});return e}getRebroadcastMode(){const e=this.storage.getItem(this.rebroadcastModeKey),t=null==e?void 0:e.startsWith("RTSP");return{rtspMode:null==e?void 0:e.startsWith("RTSP"),mp4Mode:!t||(null==e?void 0:e.includes("MP4"))}}async startPrebufferSession(){var e,t,r,i,o,s,d;this.prebuffers.mp4=[],this.prebuffers.mpegts=[],this.prebuffers.rtsp=[];const u=parseInt(this.storage.getItem(M))||P;let p;try{p=(await this.mixinDevice.getVideoStreamOptions()).find((e=>e.id===this.streamId))}catch(e){}const m=null===(null===(e=p)||void 0===e?void 0:e.audio),f=null===(t=p)||void 0===t||null===(r=t.audio)||void 0===r?void 0:r.codec,{isUsingDefaultAudioConfig:g,aacAudio:S,compatibleAudio:b,reencodeAudio:w}=this.getAudioConfig();let I=this.storage.getItem(this.lastDetectedAudioCodecKey)||void 0;"null"===I&&(I=null);let O=!1;m||f||!g||void 0!==I||(this.console.warn("Camera did not report an audio codec, muting the audio stream and probing the codec."),O=!0),!m&&f&&void 0!==I&&I!==f&&this.console.warn("Audio codec plugin reported vs detected mismatch",f,I);const x=void 0===I?null==f?void 0:f.toLowerCase():null===(i=I)||void 0===i?void 0:i.toLowerCase(),{rtspMode:D,mp4Mode:C}=this.getRebroadcastMode(),T=!D||C,E=!A.includes(x);!T||O||!1===(null===(o=p)||void 0===o?void 0:o.userConfigurable)||m||E&&(g&&y.a(`${this.mixin.name} is using the ${x} audio codec. Configuring your Camera to use AAC, MP3, MP2, or Opus audio is recommended. If this is not possible, Select 'Transcode Audio' in the camera stream's Rebroadcast settings to suppress this alert.`),this.console.warn("Configure your camera to output AAC, MP3, MP2, or Opus audio. Suboptimal audio codec in use:",x));const R=["-bsf:a","aac_adtstoasc"],L=[];let B;this.audioDisabled=!1;const j=null===I;let F=!1;var V;!O&&g&&E&&(!1===(null===(V=p)||void 0===V?void 0:V.userConfigurable)?this.console.log("camera reports it is not user configurable. transcoding due to incompatible codec",x):this.console.log("camera audio transcoding due to incompatible codec. configure the camera to use a compatible codec if possible."),F=!0);if(m||O)B=["-an"],this.audioDisabled=!0;else if(w||F)B=["-bsf:a","aac_adtstoasc","-acodec","libfdk_aac","-ar","8k","-b:a","100k","-bufsize","400k","-ac","1","-profile:a","aac_low","-flags","+global_header"];else if(S||j)B=["-acodec","copy"],B.push(...R);else if(b)B=["-acodec","copy"],B.push(...L);else{B=["-acodec","copy"];const e="aac"===x?R:L;B.push(...e)}const H=["-vcodec","copy"],N={console:this.console,timeout:6e4,parsers:{}};if(this.parsers=N.parsers,this.console.log("rebroadcast mode:",D?"rtsp":"mpegts"),D){const e=(0,l.createRtspParser)();this.sdp=e.sdp,N.parsers.rtsp=e}else N.parsers.mpegts=(0,c.createMpegTsParser)({vcodec:H,acodec:B});C&&(N.parsers.mp4=(0,c.createFragmentedMp4Parser)({vcodec:H,acodec:B}));const U=await this.mixinDevice.getVideoStream(p),K="x-scrypted/x-rfc4571"===U.mimeType;let $,q;if(this.storage.removeItem(this.lastDetectedAudioCodecKey),D&&K&&!C)$=await(0,h.startRFC4571Parser)(U,N),this.sdp=$.sdp.then((e=>Buffer.concat(e).toString()));else{const e=await v.convertMediaObjectToBuffer(U,n.ScryptedMimeTypes.FFmpegInput),t=JSON.parse(e.toString());q=t.mediaStreamOptions;const r=this.storage.getItem(this.ffmpegInputArgumentsKey)||k;t.inputArguments.unshift(...r.split(" ")),$=await(0,a.startParserSession)(t,N)}if($.inputAudioCodec?A.includes(null===(s=$.inputAudioCodec)||void 0===s?void 0:s.toLowerCase())?this.console.log("Detected audio codec is mp4/mpegts compatible.",$.inputAudioCodec):this.console.log("Detected audio codec is not mp4/mpegts compatible.",$.inputAudioCodec):this.console.log("No audio stream detected."),this.storage.setItem(this.lastDetectedAudioCodecKey,$.inputAudioCodec||"null"),"h264"!==$.inputVideoCodec&&this.console.error("Video codec is not h264. If there are errors, try changing your camera's encoder output."),O)return this.console.warn("Audio probe complete, ending rebroadcast session and restarting with detected codecs."),$.kill(),this.startPrebufferSession();if(this.parserSession=$,null!==(d=q)&&void 0!==d&&d.refreshAt){let e,t=q;const r=async()=>{if(!$.isActive)return;const e=await this.mixinDevice.getVideoStream(t),r=await v.convertMediaObjectToBuffer(e,n.ScryptedMimeTypes.FFmpegInput),o=JSON.parse(r.toString());t=o.mediaStreamOptions,i(t)},i=t=>{const i=t.refreshAt-Date.now()-3e4;this.console.log("refreshing media stream in",i),e=setTimeout(r,i)};i(t),$.once("killed",(()=>clearTimeout(e)))}$.once("killed",(()=>{this.parserSessionPromise=void 0,this.parserSession===$&&(this.parserSession=void 0)}));for(const e of _){let t=0;$.on(e,(r=>{const i=this.prebuffers[e],n=Date.now();for("mdat"===r.type&&(this.prevIdr&&(this.detectedIdrInterval=n-this.prevIdr),this.prevIdr=n),i.push({time:n,chunk:r});i.length&&i[0].time<n-u;)i.shift(),t++;t>1e3&&(this.prebuffers[e]=i.slice(),t=0)}))}return $}printActiveClients(){this.console.log(this.streamName,"active rebroadcast clients:",this.activeClients)}inactivityCheck(e,t){this.printActiveClients(),this.stopInactive&&(this.activeClients||this.inactivityTimeout&&!t||(clearTimeout(this.inactivityTimeout),this.inactivityTimeout=setTimeout((()=>{this.activeClients||(this.console.log(this.streamName,"terminating rebroadcast due to inactivity"),e.kill())}),3e4)))}async getVideoStream(e){var t,r;this.ensurePrebufferSession();const i=await this.parserSessionPromise,n="false"!==this.storage.getItem(w),o=(null==e?void 0:e.prebuffer)||(n?1.5*Math.max(4e3,this.detectedIdrInterval||4e3):0);this.console.log(this.streamName,"prebuffer request started");const{rtspMode:s,mp4Mode:c}=this.getRebroadcastMode(),d=s?"rtsp":"mpegts",p=this.parsers[null==e?void 0:e.container]?null==e?void 0:e.container:d,m=Object.assign({},i.mediaStreamOptions);m.prebuffer=o;const{reencodeAudio:h}=this.getAudioConfig();s&&"rtsp"===p?m.audio={codec:null==i?void 0:i.inputAudioCodec}:this.audioDisabled?m.audio=null:m.audio=h?{codec:"aac",encoder:"libfdk_aac",profile:"aac_low"}:{codec:null==i?void 0:i.inputAudioCodec},m.video&&null!==(t=i.inputVideoResolution)&&void 0!==t&&t[2]&&null!==(r=i.inputVideoResolution)&&void 0!==r&&r[3]&&Object.assign(m.video,{width:parseInt(i.inputVideoResolution[2]),height:parseInt(i.inputVideoResolution[3])});const f=Date.now();let g=0;const y=this.prebuffers[p];for(const e of y)if(!(e.time<f-o))for(const t of e.chunk.chunks)g+=t.length;const S=Math.max(5e5,g).toString(),b=await(async t=>{const r=this.prebuffers[t];let n,s;if("rtsp"===t){this.sdp.then((e=>console.log(e)));const e=await(0,u.listenZeroSingleClient)();n=e.clientPromise.then((async e=>{let t=await this.sdp;const r=new l.RtspServer(e,t);return r.console=this.console,await r.handlePlayback(),e})),s=e.url.replace("tcp://","rtsp://")}else{const e=await(0,u.listenZeroSingleClient)();n=e.clientPromise,s=`tcp://127.0.0.1:${e.port}`}return(0,a.handleRebroadcasterClient)(n,{console:this.console,connect:(n,s)=>{this.activeClients++,this.printActiveClients();const a=Date.now(),c=e=>{n(e)>1e8&&(this.console.log("more than 100MB has been buffered, did downstream die? killing connection.",this.streamName),d())},d=()=>{s(),this.console.log(this.streamName,"prebuffer request ended"),i.removeListener(t,c),i.removeListener("killed",d)};i.on(t,c),i.once("killed",d);for(const e of r)e.time<a-o||c(e.chunk);return()=>{this.activeClients--,this.inactivityCheck(i,!1!==(null==e?void 0:e.refresh)),d()}}}),s})(p),P={url:b,container:p,inputArguments:["-analyzeduration","0","-probesize",S,...this.parsers[p].inputArguments||[],"-f",this.parsers[p].container,"-i",b],mediaStreamOptions:m};return v.createFFmpegMediaObject(P)}}class PrebufferMixin extends s.SettingsMixinDeviceBase{constructor(e,t,r,i){super(e,r,{providerNativeId:i,mixinDeviceInterfaces:t,group:"Prebuffer Settings",groupKey:"prebuffer"}),g(this,"released",!1),g(this,"sessions",new Map),this.delayStart()}delayStart(){this.console.log("prebuffer sessions starting in 5 seconds"),setTimeout((()=>this.ensurePrebufferSessions()),5e3)}async getVideoStream(e){await this.ensurePrebufferSessions();const t=null==e?void 0:e.id;let r=this.sessions.get(t);return!r||null!=e&&e.directMediaStream?this.mixinDevice.getVideoStream(e):(r.ensurePrebufferSession(),await r.parserSessionPromise,r=this.sessions.get(t),r?r.getVideoStream(e):this.mixinDevice.getVideoStream(e))}async ensurePrebufferSessions(){const e=await this.mixinDevice.getVideoStreamOptions(),t=this.getEnabledMediaStreamOptions(e),r=t?t.map((e=>e.id)):[void 0],i=(null==e?void 0:e.map((e=>e.id)))||[void 0];if("true"!==this.storage.getItem("warnedCloud")){(null==e?void 0:e.find((e=>"cloud"===e.source)))&&(this.storage.setItem("warnedCloud","true"),y.a(`${this.name} is a cloud camera. Prebuffering maintains a persistent stream and will not enabled by default. You must enable the Prebuffer stream manually.`))}const s=this.mixinDeviceInterfaces.includes(n.ScryptedInterface.Battery);let a=0;const c=i.length;for(const t of i){let i=this.sessions.get(t);if(!i){var d;const n=null==e?void 0:e.find((e=>e.id===t));null!=n&&n.prebuffer&&y.a(`Prebuffer is already available on ${this.name}. If this is a grouped device, disable the Rebroadcast extension.`);const u=null==n?void 0:n.name,l=!r.includes(t);if(i=new PrebufferSession(this,u,t,s||l),this.sessions.set(t,i),t===(null==e||null===(d=e[0])||void 0===d?void 0:d.id)&&this.sessions.set(void 0,i),s){this.console.log("camera is battery powered, prebuffering and rebroadcasting will only work on demand.");continue}if(l){this.console.log("stream",u,"will be rebroadcast on demand.");continue}(async()=>{for(;this.sessions.get(t)===i&&!this.released;){i.ensurePrebufferSession();try{const e=await i.parserSessionPromise;a++,this.online=a==c,await(0,o.once)(e,"killed"),this.console.error("prebuffer session ended")}catch(e){this.console.error("prebuffer session ended with error",e)}finally{a--,this.online=a==c}this.console.log("restarting prebuffer session in 5 seconds"),await new Promise((e=>setTimeout(e,5e3)))}this.console.log("exiting prebuffer session (released or restarted with new configuration)")})()}}b.onMixinEvent(this.id,this.mixinProviderNativeId,n.ScryptedInterface.Settings,void 0)}async getMixinSettings(){const e=[];try{const t=await this.mixinDevice.getVideoStreamOptions(),r=this.getEnabledMediaStreamOptions(t);(null==t?void 0:t.length)>0&&e.push({title:"Prebuffered Streams",description:"The streams to prebuffer. Enable only as necessary to reduce traffic.",key:"enabledStreams",value:r.map((e=>e.name||"")),choices:t.map((e=>e.name)),multiple:!0})}catch(e){throw this.console.error("error in getVideoStreamOptions",e),e}e.push({title:"Prebuffer Duration",description:"Duration of the prebuffer in milliseconds.",type:"number",key:M,value:this.storage.getItem(M)||P.toString()},{title:"Start at Previous Keyframe",description:"Start live streams from the previous key frame. Improves startup time.",type:"boolean",key:w,value:("false"!==this.storage.getItem(w)).toString()});for(const t of new Set([...this.sessions.values()]))if(t)try{e.push(...await t.getMixinSettings())}catch(e){throw this.console.error("error in prebuffer session getMixinSettings",e),e}return e}async putMixinSetting(e,t){const r=this.sessions;this.sessions=new Map,"enabledStreams"===e?this.storage.setItem(e,JSON.stringify(t)):this.storage.setItem(e,t.toString());for(const e of r.values()){var i;null==e||null===(i=e.parserSessionPromise)||void 0===i||i.then((e=>e.kill()))}this.ensurePrebufferSessions()}getEnabledMediaStreamOptions(e){if(!e)return;try{const t=JSON.parse(this.storage.getItem("enabledStreams"));return e.filter((e=>t.includes(e.name)))}catch(e){}const t=e.find((e=>"cloud"!==e.source));return t?[t]:[]}async getVideoStreamOptions(){const e=await this.mixinDevice.getVideoStreamOptions()||[];let t=this.getEnabledMediaStreamOptions(e);const r=parseInt(this.storage.getItem(M))||P;for(const n of e){var i;(null!==(i=this.sessions.get(n.id))&&void 0!==i&&i.parserSession||t.includes(n))&&(n.prebuffer=r)}return e}release(){this.console.log("prebuffer releasing if started"),this.released=!0;for(const t of this.sessions.values()){var e;t&&(t.clearPrebuffers(),null===(e=t.parserSessionPromise)||void 0===e||e.then((e=>{this.console.log("prebuffer released"),e.kill(),t.clearPrebuffers()})))}}}class PrebufferProvider extends d.AutoenableMixinProvider{constructor(e){super(e),this.fromMimeType="x-scrypted/x-rfc4571",this.toMimeType=n.ScryptedMimeTypes.FFmpegInput;for(const e of Object.keys(S.getSystemState())){var t;const r=S.getDeviceById(e);null!==(t=r.mixins)&&void 0!==t&&t.includes(this.id)&&r.getVideoStreamOptions()}const r=function(){var e=new Date;return e.setHours(24),e.setMinutes(0),e.setSeconds(0),e.setMilliseconds(0),e.getTime()-(new Date).getTime()}()+72e5;this.log.i(`Rebroadcaster scheduled for restart at 2AM: ${Math.round(r/1e3/60)} minutes`),setTimeout((()=>b.requestRestart()),r)}async convert(e,t,r){var i,n;const o=JSON.parse(e.toString()),{url:s,sdp:a}=o,c=parseInt(null===(i=a.match(/m=audio.* ([0-9]+)/))||void 0===i?void 0:i[1]),d=parseInt(null===(n=a.match(/m=video.* ([0-9]+)/))||void 0===n?void 0:n[1]),h=new URL(s);if(!h.protocol.startsWith("tcp"))throw new Error("rfc4751 url must be tcp");const{clientPromise:f,url:g}=await(0,u.listenZeroSingleClient)(),v={url:g,inputArguments:["-rtsp_transport","tcp","-max_delay","1000000","-i",g.replace("tcp","rtsp")]};return f.then((async e=>{const t=new l.RtspServer(e,a);t.console=this.console,await t.handlePlayback();const r=p.default.connect(parseInt(h.port),h.hostname);for(e.on("close",(()=>{r.destroy()})),r.on("close",(()=>{e.destroy()}));;){const i=(await(0,m.readLength)(r,2)).readInt16BE(0),n=await(0,m.readLength)(r,i),o=127&n[1];if(o===c)t.sendAudio(n,!1);else{if(o!==d)throw e.destroy(),r.destroy(),new Error("unknown payload type "+o);t.sendVideo(n,!1)}}})),Buffer.from(JSON.stringify(v))}async canMixin(e,t){return t.includes(n.ScryptedInterface.VideoCamera)?[n.ScryptedInterface.VideoCamera,n.ScryptedInterface.Settings,n.ScryptedInterface.Online]:null}async getMixin(e,t,r){return this.setHasEnabledMixin(r.id),new PrebufferMixin(e,t,r,this.nativeId)}async releaseMixin(e,t){t.online=!0,t.release()}}var E=new PrebufferProvider;e.default=E})();var n=exports="undefined"==typeof exports?{}:exports;for(var o in i)n[o]=i[o];i.__esModule&&Object.defineProperty(n,"__esModule",{value:!0})})();
|
|
2
2
|
//# sourceMappingURL=main.nodejs.js.map
|
package/dist/plugin.zip
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scrypted/prebuffer-mixin",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.161",
|
|
4
4
|
"description": "Rebroadcast and Prebuffer for VideoCameras.",
|
|
5
5
|
"author": "Scrypted",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -24,7 +24,8 @@
|
|
|
24
24
|
"singleInstance": true,
|
|
25
25
|
"type": "API",
|
|
26
26
|
"interfaces": [
|
|
27
|
-
"MixinProvider"
|
|
27
|
+
"MixinProvider",
|
|
28
|
+
"BufferConverter"
|
|
28
29
|
]
|
|
29
30
|
},
|
|
30
31
|
"dependencies": {
|
package/src/main.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
import { MixinProvider, ScryptedDeviceType, ScryptedInterface, MediaObject, VideoCamera, MediaStreamOptions, Settings, Setting, ScryptedMimeTypes, FFMpegInput, RequestMediaStreamOptions } from '@scrypted/sdk';
|
|
2
|
+
import { MixinProvider, ScryptedDeviceType, ScryptedInterface, MediaObject, VideoCamera, MediaStreamOptions, Settings, Setting, ScryptedMimeTypes, FFMpegInput, RequestMediaStreamOptions, BufferConverter, ResponseMediaStreamOptions } from '@scrypted/sdk';
|
|
3
3
|
import sdk from '@scrypted/sdk';
|
|
4
4
|
import { once } from 'events';
|
|
5
5
|
import { SettingsMixinDeviceBase } from "../../../common/src/settings-mixin";
|
|
@@ -9,6 +9,9 @@ import { AutoenableMixinProvider } from '@scrypted/common/src/autoenable-mixin-p
|
|
|
9
9
|
import { listenZeroSingleClient } from '@scrypted/common/src/listen-cluster';
|
|
10
10
|
import { createRtspParser, RtspServer } from '../../../common/src/rtsp-server';
|
|
11
11
|
import { Duplex } from 'stream';
|
|
12
|
+
import net from 'net';
|
|
13
|
+
import { readLength } from '@scrypted/common/src/read-stream';
|
|
14
|
+
import { startRFC4571Parser } from './rfc4571';
|
|
12
15
|
|
|
13
16
|
const { mediaManager, log, systemManager, deviceManager } = sdk;
|
|
14
17
|
|
|
@@ -172,6 +175,7 @@ class PrebufferSession {
|
|
|
172
175
|
choices: [
|
|
173
176
|
'MPEG-TS',
|
|
174
177
|
'RTSP',
|
|
178
|
+
'RTSP+MP4',
|
|
175
179
|
],
|
|
176
180
|
key: this.rebroadcastModeKey,
|
|
177
181
|
value: this.storage.getItem(this.rebroadcastModeKey) || 'MPEG-TS',
|
|
@@ -222,6 +226,15 @@ class PrebufferSession {
|
|
|
222
226
|
return settings;
|
|
223
227
|
}
|
|
224
228
|
|
|
229
|
+
getRebroadcastMode() {
|
|
230
|
+
const mode = this.storage.getItem(this.rebroadcastModeKey);
|
|
231
|
+
const rtspMode = mode?.startsWith('RTSP');
|
|
232
|
+
return {
|
|
233
|
+
rtspMode: mode?.startsWith('RTSP'),
|
|
234
|
+
mp4Mode: !rtspMode || mode?.includes('MP4'),
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
225
238
|
async startPrebufferSession() {
|
|
226
239
|
this.prebuffers.mp4 = [];
|
|
227
240
|
this.prebuffers.mpegts = [];
|
|
@@ -267,10 +280,14 @@ class PrebufferSession {
|
|
|
267
280
|
? advertisedAudioCodec?.toLowerCase()
|
|
268
281
|
: detectedAudioCodec?.toLowerCase();
|
|
269
282
|
|
|
283
|
+
// rtsp mode can handle any codec, and its generally better to allow it do that.
|
|
284
|
+
const { rtspMode, mp4Mode } = this.getRebroadcastMode();
|
|
285
|
+
const nonRtsp = !rtspMode || mp4Mode;
|
|
286
|
+
|
|
270
287
|
// after probing the audio codec is complete, alert the user with appropriate instructions.
|
|
271
288
|
// assume the codec is user configurable unless the camera explictly reports otherwise.
|
|
272
289
|
const audioIncompatible = !COMPATIBLE_AUDIO_CODECS.includes(assumedAudioCodec);
|
|
273
|
-
if (!probingAudioCodec && mso?.userConfigurable !== false && !audioSoftMuted) {
|
|
290
|
+
if (nonRtsp && !probingAudioCodec && mso?.userConfigurable !== false && !audioSoftMuted) {
|
|
274
291
|
if (audioIncompatible) {
|
|
275
292
|
// show an alert that rebroadcast needs an explicit setting by the user.
|
|
276
293
|
if (isUsingDefaultAudioConfig) {
|
|
@@ -289,10 +306,6 @@ class PrebufferSession {
|
|
|
289
306
|
}
|
|
290
307
|
}
|
|
291
308
|
|
|
292
|
-
const mo = await this.mixinDevice.getVideoStream(mso);
|
|
293
|
-
const moBuffer = await mediaManager.convertMediaObjectToBuffer(mo, ScryptedMimeTypes.FFmpegInput);
|
|
294
|
-
const ffmpegInput = JSON.parse(moBuffer.toString()) as FFMpegInput;
|
|
295
|
-
|
|
296
309
|
// aac needs to have the adts header stripped for mpegts and mp4.
|
|
297
310
|
// use this filter sparingly as it prevents ffmpeg from starting on a mismatch.
|
|
298
311
|
// however, not using it on an aac stream also prevents ffmpeg from parsing.
|
|
@@ -374,14 +387,11 @@ class PrebufferSession {
|
|
|
374
387
|
console: this.console,
|
|
375
388
|
timeout: 60000,
|
|
376
389
|
parsers: {
|
|
377
|
-
mp4: createFragmentedMp4Parser({
|
|
378
|
-
vcodec,
|
|
379
|
-
acodec,
|
|
380
|
-
}),
|
|
381
390
|
},
|
|
382
391
|
};
|
|
392
|
+
this.parsers = rbo.parsers;
|
|
383
393
|
|
|
384
|
-
|
|
394
|
+
this.console.log('rebroadcast mode:', rtspMode ? 'rtsp' : 'mpegts');
|
|
385
395
|
if (!rtspMode) {
|
|
386
396
|
rbo.parsers.mpegts = createMpegTsParser({
|
|
387
397
|
vcodec,
|
|
@@ -394,17 +404,37 @@ class PrebufferSession {
|
|
|
394
404
|
rbo.parsers.rtsp = parser;
|
|
395
405
|
}
|
|
396
406
|
|
|
397
|
-
|
|
407
|
+
if (mp4Mode) {
|
|
408
|
+
rbo.parsers.mp4 = createFragmentedMp4Parser({
|
|
409
|
+
vcodec,
|
|
410
|
+
acodec,
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
const mo = await this.mixinDevice.getVideoStream(mso);
|
|
415
|
+
const isRfc4571 = mo.mimeType === 'x-scrypted/x-rfc4571';
|
|
398
416
|
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
ffmpegInput.inputArguments.unshift(...extraInputArguments.split(' '));
|
|
417
|
+
let session: ParserSession<PrebufferParsers>;
|
|
418
|
+
let sessionMso: ResponseMediaStreamOptions;
|
|
402
419
|
|
|
403
420
|
// before launching the parser session, clear out the last detected codec.
|
|
404
421
|
// an erroneous cached codec could cause ffmpeg to fail to start.
|
|
405
422
|
this.storage.removeItem(this.lastDetectedAudioCodecKey);
|
|
406
423
|
|
|
407
|
-
|
|
424
|
+
if (rtspMode && isRfc4571 && !mp4Mode) {
|
|
425
|
+
session = await startRFC4571Parser(mo, rbo);
|
|
426
|
+
this.sdp = session.sdp.then(buffers => Buffer.concat(buffers).toString());
|
|
427
|
+
}
|
|
428
|
+
else {
|
|
429
|
+
const moBuffer = await mediaManager.convertMediaObjectToBuffer(mo, ScryptedMimeTypes.FFmpegInput);
|
|
430
|
+
const ffmpegInput = JSON.parse(moBuffer.toString()) as FFMpegInput;
|
|
431
|
+
sessionMso = ffmpegInput.mediaStreamOptions;
|
|
432
|
+
|
|
433
|
+
// create missing pts from dts so mpegts and mp4 muxing does not fail
|
|
434
|
+
const extraInputArguments = this.storage.getItem(this.ffmpegInputArgumentsKey) || DEFAULT_FFMPEG_INPUT_ARGUMENTS;
|
|
435
|
+
ffmpegInput.inputArguments.unshift(...extraInputArguments.split(' '));
|
|
436
|
+
session = await startParserSession(ffmpegInput, rbo);
|
|
437
|
+
}
|
|
408
438
|
|
|
409
439
|
if (!session.inputAudioCodec) {
|
|
410
440
|
this.console.log('No audio stream detected.');
|
|
@@ -432,8 +462,8 @@ class PrebufferSession {
|
|
|
432
462
|
this.parserSession = session;
|
|
433
463
|
|
|
434
464
|
// cloud streams need a periodic token refresh.
|
|
435
|
-
if (
|
|
436
|
-
let mso =
|
|
465
|
+
if (sessionMso?.refreshAt) {
|
|
466
|
+
let mso = sessionMso;
|
|
437
467
|
let refreshTimeout: NodeJS.Timeout;
|
|
438
468
|
|
|
439
469
|
const refreshStream = async () => {
|
|
@@ -442,18 +472,18 @@ class PrebufferSession {
|
|
|
442
472
|
const mo = await this.mixinDevice.getVideoStream(mso);
|
|
443
473
|
const moBuffer = await mediaManager.convertMediaObjectToBuffer(mo, ScryptedMimeTypes.FFmpegInput);
|
|
444
474
|
const ffmpegInput = JSON.parse(moBuffer.toString()) as FFMpegInput;
|
|
445
|
-
mso = ffmpegInput.mediaStreamOptions
|
|
475
|
+
mso = ffmpegInput.mediaStreamOptions;
|
|
446
476
|
|
|
447
|
-
scheduleRefresh(
|
|
477
|
+
scheduleRefresh(mso);
|
|
448
478
|
};
|
|
449
479
|
|
|
450
|
-
const scheduleRefresh = (
|
|
451
|
-
const when =
|
|
480
|
+
const scheduleRefresh = (refreshMso: ResponseMediaStreamOptions) => {
|
|
481
|
+
const when = refreshMso.refreshAt - Date.now() - 30000;
|
|
452
482
|
this.console.log('refreshing media stream in', when);
|
|
453
483
|
refreshTimeout = setTimeout(refreshStream, when);
|
|
454
484
|
}
|
|
455
485
|
|
|
456
|
-
scheduleRefresh(
|
|
486
|
+
scheduleRefresh(mso);
|
|
457
487
|
session.once('killed', () => clearTimeout(refreshTimeout));
|
|
458
488
|
}
|
|
459
489
|
|
|
@@ -546,6 +576,7 @@ class PrebufferSession {
|
|
|
546
576
|
socketPromise = client.clientPromise.then(async (socket) => {
|
|
547
577
|
let sdp = await this.sdp;
|
|
548
578
|
const server = new RtspServer(socket, sdp);
|
|
579
|
+
server.console = this.console;
|
|
549
580
|
await server.handlePlayback();
|
|
550
581
|
return socket;
|
|
551
582
|
})
|
|
@@ -611,7 +642,7 @@ class PrebufferSession {
|
|
|
611
642
|
return containerUrl;
|
|
612
643
|
}
|
|
613
644
|
|
|
614
|
-
const rtspMode = this.
|
|
645
|
+
const { rtspMode, mp4Mode } = this.getRebroadcastMode();
|
|
615
646
|
const defaultContainer = rtspMode ? 'rtsp' : 'mpegts';
|
|
616
647
|
|
|
617
648
|
const container: PrebufferParsers = this.parsers[options?.container] ? options?.container as PrebufferParsers : defaultContainer;
|
|
@@ -622,17 +653,25 @@ class PrebufferSession {
|
|
|
622
653
|
|
|
623
654
|
const { reencodeAudio } = this.getAudioConfig();
|
|
624
655
|
|
|
625
|
-
if (
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
656
|
+
if (!rtspMode || container !== 'rtsp') {
|
|
657
|
+
if (this.audioDisabled) {
|
|
658
|
+
mediaStreamOptions.audio = null;
|
|
659
|
+
}
|
|
660
|
+
else if (reencodeAudio) {
|
|
661
|
+
mediaStreamOptions.audio = {
|
|
662
|
+
codec: 'aac',
|
|
663
|
+
encoder: 'libfdk_aac',
|
|
664
|
+
profile: 'aac_low',
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
else {
|
|
668
|
+
mediaStreamOptions.audio = {
|
|
669
|
+
codec: session?.inputAudioCodec,
|
|
670
|
+
}
|
|
633
671
|
}
|
|
634
672
|
}
|
|
635
673
|
else {
|
|
674
|
+
// rtsp mode never transcodes.
|
|
636
675
|
mediaStreamOptions.audio = {
|
|
637
676
|
codec: session?.inputAudioCodec,
|
|
638
677
|
}
|
|
@@ -910,10 +949,13 @@ function millisUntilMidnight() {
|
|
|
910
949
|
return (midnight.getTime() - new Date().getTime());
|
|
911
950
|
}
|
|
912
951
|
|
|
913
|
-
class PrebufferProvider extends AutoenableMixinProvider implements MixinProvider {
|
|
952
|
+
class PrebufferProvider extends AutoenableMixinProvider implements MixinProvider, BufferConverter {
|
|
914
953
|
constructor(nativeId?: string) {
|
|
915
954
|
super(nativeId);
|
|
916
955
|
|
|
956
|
+
this.fromMimeType = 'x-scrypted/x-rfc4571';
|
|
957
|
+
this.toMimeType = ScryptedMimeTypes.FFmpegInput;
|
|
958
|
+
|
|
917
959
|
// trigger the prebuffer.
|
|
918
960
|
for (const id of Object.keys(systemManager.getSystemState())) {
|
|
919
961
|
const device = systemManager.getDeviceById<VideoCamera>(id);
|
|
@@ -929,6 +971,60 @@ class PrebufferProvider extends AutoenableMixinProvider implements MixinProvider
|
|
|
929
971
|
setTimeout(() => deviceManager.requestRestart(), twoAM);
|
|
930
972
|
}
|
|
931
973
|
|
|
974
|
+
async convert(data: Buffer, fromMimeType: string, toMimeType: string): Promise<Buffer> {
|
|
975
|
+
const json = JSON.parse(data.toString());
|
|
976
|
+
const { url, sdp } = json;
|
|
977
|
+
|
|
978
|
+
const audioPt = parseInt((sdp as string).match(/m=audio.* ([0-9]+)/)?.[1]);
|
|
979
|
+
const videoPt = parseInt((sdp as string).match(/m=video.* ([0-9]+)/)?.[1]);
|
|
980
|
+
const u = new URL(url);
|
|
981
|
+
if (!u.protocol.startsWith('tcp'))
|
|
982
|
+
throw new Error('rfc4751 url must be tcp');
|
|
983
|
+
const { clientPromise, url: clientUrl } = await listenZeroSingleClient();
|
|
984
|
+
const ffmpeg: FFMpegInput = {
|
|
985
|
+
url: clientUrl,
|
|
986
|
+
inputArguments: [
|
|
987
|
+
"-rtsp_transport", "tcp",
|
|
988
|
+
"-max_delay", "1000000",
|
|
989
|
+
'-i', clientUrl.replace('tcp', 'rtsp'),
|
|
990
|
+
]
|
|
991
|
+
};
|
|
992
|
+
|
|
993
|
+
clientPromise.then(async (client) => {
|
|
994
|
+
const rtsp = new RtspServer(client, sdp);
|
|
995
|
+
rtsp.console = this.console;
|
|
996
|
+
await rtsp.handlePlayback();
|
|
997
|
+
const socket = net.connect(parseInt(u.port), u.hostname);
|
|
998
|
+
|
|
999
|
+
client.on('close', () => {
|
|
1000
|
+
socket.destroy();
|
|
1001
|
+
});
|
|
1002
|
+
socket.on('close', () => {
|
|
1003
|
+
client.destroy();
|
|
1004
|
+
})
|
|
1005
|
+
|
|
1006
|
+
while (true) {
|
|
1007
|
+
const header = await readLength(socket, 2);
|
|
1008
|
+
const length = header.readInt16BE(0);
|
|
1009
|
+
const data = await readLength(socket, length);
|
|
1010
|
+
const pt = data[1] & 0x7f;
|
|
1011
|
+
if (pt === audioPt) {
|
|
1012
|
+
rtsp.sendAudio(data, false);
|
|
1013
|
+
}
|
|
1014
|
+
else if (pt === videoPt) {
|
|
1015
|
+
rtsp.sendVideo(data, false);
|
|
1016
|
+
}
|
|
1017
|
+
else {
|
|
1018
|
+
client.destroy();
|
|
1019
|
+
socket.destroy();
|
|
1020
|
+
throw new Error('unknown payload type ' + pt);
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
})
|
|
1024
|
+
|
|
1025
|
+
return Buffer.from(JSON.stringify(ffmpeg));
|
|
1026
|
+
}
|
|
1027
|
+
|
|
932
1028
|
async canMixin(type: ScryptedDeviceType, interfaces: string[]): Promise<string[]> {
|
|
933
1029
|
if (!interfaces.includes(ScryptedInterface.VideoCamera))
|
|
934
1030
|
return null;
|
package/src/rfc4571.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { ParserOptions, ParserSession } from "@scrypted/common/src/ffmpeg-rebroadcast";
|
|
2
|
+
import { readLength } from "@scrypted/common/src/read-stream";
|
|
3
|
+
import sdk, { MediaObject } from "@scrypted/sdk";
|
|
4
|
+
import { EventEmitter } from "stream";
|
|
5
|
+
import net from 'net';
|
|
6
|
+
import { StreamChunk } from "@scrypted/common/src/stream-parser";
|
|
7
|
+
import { RTSP_FRAME_MAGIC } from "@scrypted/common/src/rtsp-server";
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
const { mediaManager } = sdk;
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
export async function startRFC4571Parser(mediaObject: MediaObject, options: ParserOptions<"rtsp">): Promise<ParserSession<"rtsp">> {
|
|
14
|
+
let isActive = true;
|
|
15
|
+
const events = new EventEmitter();
|
|
16
|
+
|
|
17
|
+
const json = await mediaManager.convertMediaObjectToJSON<any>(mediaObject, 'x-scrypted/x-rfc4571');
|
|
18
|
+
const { url, sdp } = json;
|
|
19
|
+
|
|
20
|
+
const audioPt = parseInt((sdp as string).match(/m=audio.* ([0-9]+)/)?.[1]);
|
|
21
|
+
const videoPt = parseInt((sdp as string).match(/m=video.* ([0-9]+)/)?.[1]);
|
|
22
|
+
const u = new URL(url);
|
|
23
|
+
if (!u.protocol.startsWith('tcp'))
|
|
24
|
+
throw new Error('rfc4751 url must be tcp');
|
|
25
|
+
|
|
26
|
+
const socket = net.connect(parseInt(u.port), u.hostname);
|
|
27
|
+
|
|
28
|
+
const kill = () => {
|
|
29
|
+
if (isActive) {
|
|
30
|
+
events.emit('killed');
|
|
31
|
+
events.emit('error', new Error('killed'));
|
|
32
|
+
}
|
|
33
|
+
isActive = false;
|
|
34
|
+
socket.destroy();
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
socket.on('close', kill);
|
|
38
|
+
socket.on('error', kill);
|
|
39
|
+
|
|
40
|
+
(async () => {
|
|
41
|
+
while (true) {
|
|
42
|
+
const header = await readLength(socket, 2);
|
|
43
|
+
const length = header.readInt16BE(0);
|
|
44
|
+
const data = await readLength(socket, length);
|
|
45
|
+
const pt = data[1] & 0x7f;
|
|
46
|
+
const prefix = Buffer.alloc(2);
|
|
47
|
+
prefix[0] = RTSP_FRAME_MAGIC;
|
|
48
|
+
if (pt === audioPt) {
|
|
49
|
+
prefix[1] = 0;
|
|
50
|
+
}
|
|
51
|
+
else if (pt === videoPt) {
|
|
52
|
+
prefix[1] = 2;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const chunk: StreamChunk = {
|
|
56
|
+
chunks: [prefix, header, data],
|
|
57
|
+
}
|
|
58
|
+
events.emit('rtsp', chunk);
|
|
59
|
+
}
|
|
60
|
+
})()
|
|
61
|
+
.finally(kill);
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
sdp: Promise.resolve([Buffer.from(sdp)]),
|
|
65
|
+
inputAudioCodec: 'pcm',
|
|
66
|
+
inputVideoCodec: 'h264',
|
|
67
|
+
inputVideoResolution: undefined,
|
|
68
|
+
isActive() { return isActive },
|
|
69
|
+
kill,
|
|
70
|
+
mediaStreamOptions: json.mediaStreamOptions,
|
|
71
|
+
on(event: string, cb: any) {
|
|
72
|
+
events.on(event, cb);
|
|
73
|
+
return this;
|
|
74
|
+
},
|
|
75
|
+
once(event: any, cb: any) {
|
|
76
|
+
events.once(event, cb);
|
|
77
|
+
return this;
|
|
78
|
+
},
|
|
79
|
+
removeListener(event, cb) {
|
|
80
|
+
events.removeListener(event, cb);
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|