UNPKG

@phenixrts-tools/rtmp-push

Version:

Module to start and stop RTMP push

3 lines 13.7 kB
/*! For license information please see main.js.LICENSE.txt */ !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.RtmpPush=e():t.RtmpPush=e()}(global,(function(){return function(){"use strict";var t={d:function(e,r){for(var n in r)t.o(r,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:r[n]})},o:function(t,e){return Object.prototype.hasOwnProperty.call(t,e)}},e={};t.d(e,{default:function(){return s}});var r=require("child_process");function n(t){return n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},n(t)}function o(){o=function(){return t};var t={},e=Object.prototype,r=e.hasOwnProperty,i=Object.defineProperty||function(t,e,r){t[e]=r.value},c="function"==typeof Symbol?Symbol:{},a=c.iterator||"@@iterator",s=c.asyncIterator||"@@asyncIterator",u=c.toStringTag||"@@toStringTag";function f(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{f({},"")}catch(t){f=function(t,e,r){return t[e]=r}}function l(t,e,r,n){var o=e&&e.prototype instanceof d?e:d,c=Object.create(o.prototype),a=new L(n||[]);return i(c,"_invoke",{value:S(t,r,a)}),c}function h(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=l;var p={};function d(){}function v(){}function m(){}var g={};f(g,a,(function(){return this}));var y=Object.getPrototypeOf,w=y&&y(y(O([])));w&&w!==e&&r.call(w,a)&&(g=w);var P=m.prototype=d.prototype=Object.create(g);function b(t){["next","throw","return"].forEach((function(e){f(t,e,(function(t){return this._invoke(e,t)}))}))}function _(t,e){function o(i,c,a,s){var u=h(t[i],t,c);if("throw"!==u.type){var f=u.arg,l=f.value;return l&&"object"==n(l)&&r.call(l,"__await")?e.resolve(l.__await).then((function(t){o("next",t,a,s)}),(function(t){o("throw",t,a,s)})):e.resolve(l).then((function(t){f.value=t,a(f)}),(function(t){return o("throw",t,a,s)}))}s(u.arg)}var c;i(this,"_invoke",{value:function(t,r){function n(){return new e((function(e,n){o(t,r,e,n)}))}return c=c?c.then(n,n):n()}})}function S(t,e,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var c=r.delegate;if(c){var a=x(c,r);if(a){if(a===p)continue;return a}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var s=h(t,e,r);if("normal"===s.type){if(n=r.done?"completed":"suspendedYield",s.arg===p)continue;return{value:s.arg,done:r.done}}"throw"===s.type&&(n="completed",r.method="throw",r.arg=s.arg)}}}function x(t,e){var r=e.method,n=t.iterator[r];if(void 0===n)return e.delegate=null,"throw"===r&&t.iterator.return&&(e.method="return",e.arg=void 0,x(t,e),"throw"===e.method)||"return"!==r&&(e.method="throw",e.arg=new TypeError("The iterator does not provide a '"+r+"' method")),p;var o=h(n,t.iterator,e.arg);if("throw"===o.type)return e.method="throw",e.arg=o.arg,e.delegate=null,p;var i=o.arg;return i?i.done?(e[t.resultName]=i.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,p):i:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,p)}function E(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function k(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function L(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(E,this),this.reset(!0)}function O(t){if(t){var e=t[a];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,o=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return o.next=o}}return{next:T}}function T(){return{value:void 0,done:!0}}return v.prototype=m,i(P,"constructor",{value:m,configurable:!0}),i(m,"constructor",{value:v,configurable:!0}),v.displayName=f(m,u,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===v||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,m):(t.__proto__=m,f(t,u,"GeneratorFunction")),t.prototype=Object.create(P),t},t.awrap=function(t){return{__await:t}},b(_.prototype),f(_.prototype,s,(function(){return this})),t.AsyncIterator=_,t.async=function(e,r,n,o,i){void 0===i&&(i=Promise);var c=new _(l(e,r,n,o),i);return t.isGeneratorFunction(r)?c:c.next().then((function(t){return t.done?t.value:c.next()}))},b(P),f(P,u,"Generator"),f(P,a,(function(){return this})),f(P,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},t.values=O,L.prototype={constructor:L,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(k),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return c.type="throw",c.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],c=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var a=r.call(i,"catchLoc"),s=r.call(i,"finallyLoc");if(a&&s){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(a){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var c=i?i.completion:{};return c.type=t,c.arg=e,i?(this.method="next",this.next=i.finallyLoc,p):this.complete(c)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),p},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),k(r),p}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;k(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:O(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),p}},t}function i(t,e,r,n,o,i,c){try{var a=t[i](c),s=a.value}catch(t){return void r(t)}a.done?e(s):Promise.resolve(s).then(n,o)}function c(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var c=t.apply(e,r);function a(t){i(c,n,o,a,s,"next",t)}function s(t){i(c,n,o,a,s,"throw",t)}a(void 0)}))}}function a(t,e){for(var r=0;r<e.length;r++){var o=e[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,(void 0,i=function(t,e){if("object"!==n(t)||null===t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var o=r.call(t,"string");if("object"!==n(o))return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(o.key),"symbol"===n(i)?i:String(i)),o)}var i}var s=function(){function t(e,r){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this._mediaSourceUri=e,this._rtmpPushUri=r,this._isffmpegProcessKilled=!1}var e,n,i,s,u;return e=t,n=[{key:"start",value:(u=c(o().mark((function t(){var e,n,i,c,a,s,u,f,l,h=this;return o().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(t.prev=0,this._mediaSourceUri){t.next=3;break}throw new Error("mediaSourceUri was not provided.");case 3:if(this._rtmpPushUri){t.next=5;break}throw new Error("rtmpPushUri was not provided.");case 5:if(this._isValidUri(this._mediaSourceUri)){t.next=7;break}throw new Error("Provided mediaSourceUri [".concat(this._mediaSourceUri,"] is not a valid uri."));case 7:if(this._isValidRtmpUri(this._rtmpPushUri)){t.next=9;break}throw new Error("Invalid RTMP Push Uri");case 9:return f="-hide_banner -loglevel error -re -i ".concat(this._mediaSourceUri," -c:a copy -c:v copy -f flv ").concat(this._rtmpPushUri),this._mediaSourceUri.match(".txt")&&(f="-hide_banner -loglevel error -re -f concat -i ".concat(this._mediaSourceUri," -c:a copy -c:v copy -f flv ").concat(this._rtmpPushUri)),l=f.split(" "),console.log("[".concat((new Date).toISOString(),"] [RTMP Push] Starting ffmpeg with params [").concat(l,"]")),this._ffmpegProcess=(0,r.spawn)("ffmpeg",l),null===(e=this._ffmpegProcess)||void 0===e||null===(n=e.stdout)||void 0===n||n.on("data",(function(t){console.log("[".concat((new Date).toISOString(),"] [RTMP Push] [ffmpeg] process successfully started with stdout [").concat(t,"]"))})),null===(i=this._ffmpegProcess)||void 0===i||null===(c=i.stderr)||void 0===c||c.on("data",(function(t){throw new Error("[".concat((new Date).toISOString(),"] [RTMP Push] [ffmpeg] process has failed with stderr [").concat(t,"]"))})),null===(a=this._ffmpegProcess)||void 0===a||a.on("error",(function(t){var e;throw new Error("[".concat((new Date).toISOString(),"] [RTMP Push] [ffmpeg] process [").concat(null===(e=h._ffmpegProcess)||void 0===e?void 0:e.pid,"] could not start, error: ").concat(t))})),null===(s=this._ffmpegProcess)||void 0===s||s.on("close",(function(t){var e;h._isffmpegProcessKilled=!0,console.log("[".concat((new Date).toISOString(),"] [RTMP Push] [ffmpeg] process [").concat(null===(e=h._ffmpegProcess)||void 0===e?void 0:e.pid,"] closed stdio with code [").concat(t,"]"))})),null===(u=this._ffmpegProcess)||void 0===u||u.on("exit",(function(t){var e;console.log("[".concat((new Date).toISOString(),"] [RTMP Push] [ffmpeg] process [").concat(null===(e=h._ffmpegProcess)||void 0===e?void 0:e.pid,"] exited with code [").concat(t,"]"))})),console.log("[".concat((new Date).toISOString(),"] [RTMP Push] Stream is ready")),t.abrupt("return",this);case 23:return t.prev=23,t.t0=t.catch(0),console.error("[".concat((new Date).toISOString(),"] [RTMP Push] Start failed due to error - ").concat(t.t0)),t.abrupt("return",this);case 27:case"end":return t.stop()}}),t,this,[[0,23]])}))),function(){return u.apply(this,arguments)})},{key:"stop",value:(s=c(o().mark((function t(){return o().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(t.prev=0,this._ffmpegProcess){t.next=3;break}throw new Error("[RTMP Push] [ffmpeg] process was not found.");case 3:if(!0!==this._isffmpegProcessKilled){t.next=5;break}throw new Error("[RTMP Push] [ffmpeg] process [".concat(this._ffmpegProcess.pid,"] already killed."));case 5:return t.next=7,this._killProcessWithGivenSignal("SIGTERM");case 7:if(t.sent&&this._ffmpegProcess.killed){t.next=14;break}return t.next=11,this._killProcessWithGivenSignal("SIGKILL");case 11:if(t.sent&&this._ffmpegProcess.killed){t.next=14;break}throw new Error("[RTMP Push] [ffmpeg] process with processId [".concat(this._ffmpegProcess.pid,"] was not terminated."));case 14:t.next=19;break;case 16:t.prev=16,t.t0=t.catch(0),console.error("[".concat((new Date).toISOString(),"] [RTMP Push] Stop failed due to error - ").concat(t.t0));case 19:case"end":return t.stop()}}),t,this,[[0,16]])}))),function(){return s.apply(this,arguments)})},{key:"_isValidUri",value:function(t){try{return Boolean(new URL(t))}catch(t){return!1}}},{key:"_isValidRtmpUri",value:function(t){return new RegExp(/^(rtmp:\/\/ingest)|(?:phenixrts.com:80\/ingest)|(?:capabilities=)/).test(t)}},{key:"_killProcessWithGivenSignal",value:(i=c(o().mark((function t(e){var r,n,i,c,a=this;return o().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return console.log("[".concat((new Date).toISOString(),"] [RTMP Push] Killing [ffmpeg] process with processId [").concat(null===(r=this._ffmpegProcess)||void 0===r?void 0:r.pid,"] using signal [").concat(e,"]")),i=new Promise((function(t){n=setTimeout((function(){var r;console.log("[".concat((new Date).toISOString(),"] [RTMP Push] Killing timeout [").concat(3e3,"]ms was reached when attempting to kill [ffmpeg] process [").concat(null===(r=a._ffmpegProcess)||void 0===r?void 0:r.pid,"] with signal [").concat(e,"]")),t(!1)}),3e3)})),c=new Promise((function(t){var r,n,o;null===(r=a._ffmpegProcess)||void 0===r||r.kill(e),null===(n=a._ffmpegProcess)||void 0===n||n.on("close",(function(){t(!0)})),null===(o=a._ffmpegProcess)||void 0===o||o.on("error",(function(){t(!1)}))})),t.abrupt("return",Promise.race([i,c]).then((function(t){return t&&clearTimeout(n),t})));case 5:case"end":return t.stop()}}),t,this)}))),function(t){return i.apply(this,arguments)})}],n&&a(e.prototype,n),Object.defineProperty(e,"prototype",{writable:!1}),t}();return e.default}()})); //# sourceMappingURL=main.js.map