'use strict';var cBa={TAB:0,Rm:1,Ds:2},Y$=function(a){Kb("MediaRouter.WebRtc.Start.Success",a,cBa)};var Z$=function(a,b){Xj.call(this,b);this.H=a;this.l=new pb;this.g=lw(b.id);this.m=new pb;this.D=!1;this.s=null;this.F=!1;this.u=this.C=null;dBa(this);eBa(this);this.g.sendMessage(new hs("GET_TURN_CREDENTIALS"))};t(Z$,Xj);
Z$.prototype.start=function(a){var b=this;return this.l.promise.then(function(c){if(c.g)return Promise.reject(new cj("Mirroring already started"));if(b.s)return Promise.reject(new cj("Session permanently stopped"));b.C=new Ab("MediaRouter.WebRtc.Session.Launch");c.fa.addStream(a);c.start();return b.m.promise})};
Z$.prototype.stop=function(){var a=this;this.m.reject(new cj("Session stop requested."));this.u&&(this.u.end(),this.u=null);if(this.s)return this.s;this.F=this.D=!1;this.C=null;return this.s=this.l.promise.then(function(b){b.stop()}).then(function(){return a.g.dispose()}).catch(function(b){a.g.dispose();throw b;})};
var dBa=function(a){a.g.onMessage=function(b){if(!b.type)throw Error("Message has no type.");switch(b.type){case "TURN_CREDENTIALS":a.l.resolve(new xv(a.h.id,b.data.credentials));break;case "ANSWER":a.l.promise.then(function(c){Fv(c,b.data)});break;case "KNOCK_ANSWER":a.F=!0;a.l.promise.then(function(c){Fv(c,b.data)});break;case "STOP":a.m.reject(new cj("Stop signal received"));a.stop();break;default:throw new cj("Unknown message type: "+b.type);}}},eBa=function(a){a.l.promise.then(function(b){Bv(b,
function(c){a.g.sendMessage(new hs("OFFER",new js(c,a.H)))});Cv(b,function(c){c=JSON.parse(c);if(!c.type)throw Error("Invalid message");a.g.sendMessage(new hs(c.type,c.data))});yv(b,function(){a.D=!0;a.g.sendMessage(new hs("SESSION_START_SUCCESS"));!a.F&&a.C&&a.C.end();a.C=null;a.u=new Gb("MediaRouter.WebRtc.Session.Length");a.m.resolve(a)});Av(b,function(){a.g.sendMessage(new hs("SESSION_END"))});zv(b,function(c){a.D||a.m.reject(c);a.g.sendMessage(new hs("SESSION_FAILURE"))})})};var $$=function(){Lj.call(this,"webrtc")};t($$,Lj);k=$$.prototype;k.oi=function(a,b){return new Z$(a,b)};k.Oh=function(){Y$(0)};k.Lh=function(){Y$(1)};k.kj=function(){Y$(2)};k.Mh=function(){Jb("MediaRouter.WebRtc.Session.End")};k.Bg=function(a){Kb("MediaRouter.WebRtc.Start.Failure",a,bj)};k.Nh=function(){Jb("MediaRouter.WebRtc.Stream.End")};var fBa=new $$;Cj("mr.mirror.webrtc.WebRtcService",fBa);
