'use strict';var yra={TAB:0,wk:1,so:2},Y$=function(a){Cb("MediaRouter.WebRtc.Start.Success",a,yra)};var Z$=function(a,c){rj.call(this,c);this.C=a;this.g=new Db;this.b=kx(c.id);this.w=new Db;this.D=!1;this.m=null;this.s=!1;this.o=this.h=null;zra(this);Ara(this);this.b.sendMessage(new xj("GET_TURN_CREDENTIALS"))};q(Z$,rj);
Z$.prototype.start=function(a){var c=this;return this.g.b.then(function(b){if(b.b)return Promise.reject(new vi("Mirroring already started"));if(c.m)return Promise.reject(new vi("Session permanently stopped"));c.h=new sb("MediaRouter.WebRtc.Session.Launch");b.ja.addStream(a);b.start();return c.w.b})};
Z$.prototype.stop=function(){var a=this;this.w.reject(new vi("Session stop requested."));this.o&&(this.o.end(),this.o=null);if(this.m)return this.m;this.s=this.D=!1;this.h=null;return this.m=this.g.b.then(function(c){c.stop()}).then(function(){return a.b.Za()}).catch(function(c){a.b.Za();throw c;})};
var zra=function(a){a.b.onMessage=function(c){if(!c.type)throw Error("Message has no type.");switch(c.type){case "TURN_CREDENTIALS":a.g.resolve(new Cj(a.f.id,c.data.credentials));break;case "ANSWER":a.g.b.then(function(b){Kj(b,c.data)});break;case "KNOCK_ANSWER":a.s=!0;a.g.b.then(function(b){Kj(b,c.data)});break;case "STOP":a.w.reject(new vi("Stop signal received"));a.stop();break;default:throw new vi("Unknown message type: "+c.type);}}},Ara=function(a){a.g.b.then(function(c){Gj(c,function(b){a.b.sendMessage(new xj("OFFER",
new zj(b,a.C,Aj)))});Hj(c,function(b){b=yj(b);a.b.sendMessage(b)});Dj(c,function(){a.D=!0;a.b.sendMessage(new xj("SESSION_START_SUCCESS"));!a.s&&a.h&&a.h.end();a.h=null;a.o=new yb("MediaRouter.WebRtc.Session.Length");a.w.resolve(a)});Fj(c,function(){a.b.sendMessage(new xj("SESSION_END"))});Ej(c,function(b){a.D||a.w.reject(b);a.b.sendMessage(new xj("SESSION_FAILURE"))})})};var $$=function(){gj.call(this,"webrtc")};q($$,gj);h=$$.prototype;h.xh=function(a,c){return new Z$(a,c)};h.dh=function(){Y$(0)};h.Yg=function(){Y$(1)};h.Xh=function(){Y$(2)};h.Zg=function(){Bb("MediaRouter.WebRtc.Session.End")};h.ah=function(a){Cb("MediaRouter.WebRtc.Start.Failure",a,ui)};h.bh=function(){Bb("MediaRouter.WebRtc.Stream.End")};var Bra=new $$;Xi("mr.mirror.webrtc.WebRtcService",Bra);
