'use strict';var wG={TAB:0,Tf:1,Ji:2},xG=function(a){Cg("MediaRouter.CastStreaming.Start.Success",a,wG)};var yG=v("mr.mirror.cast.LogUploader"),AG=function(a,b,c){zG("raw_events.log.gz",a,b,c);return b?"https://crash.corp.google.com/samples?stbtiq="+b:""},zG=function(a,b,c,d){if(0==b.size)x(yG,"Trying to upload an empty file to Crash"),d&&d(null);else{var e=new FormData;e.append("prod","Cast");e.append("ver",chrome.runtime.getManifest().version);e.append(a,b);c&&e.append("comments",c);mj("https://clients2.google.com/cr/report",function(a){a=a.target;var b=null;wj(a)?(b=xj(a),x(yG,"Upload to Crash succeeded: "+
b)):x(yG,"Upload to Crash failed. HTTP status: "+a.Aa());d&&d(b)},e,void 0,3E4)}};var BG=function(){this.a=0;Xh(this)},DG=function(){CG||(CG=new BG);return CG},EG=function(){var a=DG(),b={fraction:.01,autoSubmitTimeLimitMillis:6048E5},c=b.autoSubmitTimeLimitMillis,d=Date.now();return a.a&&c&&d-a.a<c?!1:Math.random()<b.fraction};BG.prototype.ea=function(){return"mirror.cast.LogUtils"};BG.prototype.pa=function(){return[void 0,{lastAutoSubmitMillis:this.a}]};BG.prototype.ua=function(){var a=Vh(this);this.a=a&&a.lastAutoSubmitMillis||0};var CG=null;v("mr.mirror.cast.LogUtils");var FG=v("mr.NetworkUtils"),GG=function(a,b){return kg?new Promise(function(c,d){chrome.networkingPrivate.setWifiTDLSEnabledState(a,b,function(a){chrome.runtime.lastError?(w(FG,"Unable to set TDLS state: state = "+b+", error = "+chrome.runtime.lastError.message),d("Unable to set TDLS state to "+b+".")):(x(FG,"TDLS state changed: state = "+b+", status = "+a),c(a))})}):Promise.reject("TDLS feature not enabled.")};var HG=function(){this.a=Promise.resolve(1)},JG=function(a,b,c){return IG(a,function(a){return a==b},c)},KG=function(a,b){var c=[3,4];return IG(a,function(a){return-1!=c.indexOf(a)},b)},LG=function(a,b){a.a=a.a.catch(function(){return 1});return IG(a,function(){return!0},b)},IG=function(a,b,c){var d,e,f=new Promise(function(a,b){d=a;e=b});a.a=a.a.then(function(a){if(!b(a))return e(Error("Operation requires a different starting checkpoint than "+a)),Promise.resolve(a);var f=new MG(a),g;try{g=c(f)}catch(y){g=
Promise.reject(y)}return g.then(function(a){return d(a)},function(a){return e(a)}).then(function(){if(null===f.a)throw Error("A prior operation that started at "+(a+" did not complete."));return f.a})},function(a){e(a);throw a;});return f},MG=function(a){this.b=a;this.a=null},NG=function(a,b){a.a="number"===typeof b?b:a.b};var OG={fp:"OFFER",gm:"ANSWER",zp:"PRESENTATION",xn:"GET_STATUS",nq:"STATUS_RESPONSE",wn:"GET_CAPABILITIES",zm:"CAPABILITIES_RESPONSE",Yp:"RPC"};var PG=function(){this.capabilities=this.status=this.a=this.error=this.rpc=this.result=this.type=this.b=this.sessionId=null},SG=function(a){var b;try{if("string"!==typeof a)throw SyntaxError("Cannot parse non-string as JSON");var c;QG(JSON.parse(a),function(a){c=RG(a)},function(){throw Error("non-Object result from JSON parse");});return c}catch(d){b=d instanceof SyntaxError?"JSON parse error: "+d.message:"Type coercion error: "+d.message}"string"==typeof a?a="a string: "+a:a instanceof ArrayBuffer?
(a=new Uint8Array(a),a="an ArrayBuffer whose base64 is "+btoa(String.fromCharCode.apply(null,a))):a="of invalid data type "+typeof a;throw Error(b+". Input was "+a);},RG=function(a){var b=new PG;null!=a.sessionId&&(b.sessionId=String(a.sessionId));TG(a.seqNum,function(a){b.b=a},function(){throw Error('"seqNum" must be a number');});if("type"in a){for(var c=String(a.type).toUpperCase(),d=ja(Object.keys(OG)),e=d.next();!e.done;e=d.next())if(OG[e.value]==c){b.type=c;break}if(!b.type)throw Error('not a known message "type"');
}"result"in a&&(b.result=String(a.result));if("rpc"in a){if("string"!==typeof a.rpc)throw Error('"rpc" must be a String containing a base64 payload');b.rpc=new Uint8Array([].concat(la(atob(a.rpc))).map(function(a){return a.charCodeAt(0)}))}QG(a.error,function(a){b.error=UG(a)},function(){throw Error('"error" must be an Object');});QG(a.answer,function(a){b.a=VG(a)},function(){throw Error('"answer" must be an Object');});QG(a.status,function(a){b.status=WG(a)},function(){throw Error('"status" must be an Object');
});QG(a.capabilities,function(a){b.capabilities=XG(a)},function(){throw Error('"capabilities" must be an Object');});return b},QG=function(a,b,c){void 0!==a&&(a instanceof Object?b(a):c())},TG=function(a,b,c){void 0!==a&&("number"!==typeof a?c():b(a))},YG=function(a,b,c){void 0!==a&&(a instanceof Array&&a.every(function(a){return"number"===typeof a})?b(a):c())},ZG=function(a,b,c){void 0!==a&&(a instanceof Array?b(a.map(function(a){return String(a)})):c())},$G=function(){this.l=null;this.a=[];this.b=
[];this.f=this.g=this.h=null},VG=function(a){var b=new $G;TG(a.udpPort,function(a){b.l=a},function(){throw Error('"answer.udpPort" must be a number');});YG(a.sendIndexes,function(a){b.a=a},function(){throw Error('"answer.sendIndexes" must be an array of numbers');});YG(a.ssrcs,function(a){b.b=a},function(){throw Error('"answer.ssrcs" must be an array of numbers');});"IV"in a&&(b.h=String(a.IV));"receiverGetStatus"in a&&(b.g="true"==String(a.receiverGetStatus).toLowerCase());"castMode"in a&&(b.f=String(a.castMode));
return b},aH=function(){this.details=this.description=this.code=null},UG=function(a){var b=new aH;TG(a.code,function(a){b.code=a},function(){throw Error('"error.code" must be a number');});"description"in a&&(b.description=String(a.description));QG(a.details,function(a){b.details=a},function(){throw Error('"error.details" must be an Object');});return b},bH=function(){this.b=this.a=null},WG=function(a){var b=new bH;TG(a.wifiSnr,function(a){b.a=a},function(){throw Error('"status.wifiSnr" must be a number');
});YG(a.wifiSpeed,function(a){b.b=a},function(){throw Error('"status.wifiSpeed" must be an array of numbers');});return b},cH=function(){this.b=this.a=null},XG=function(a){var b=new cH;ZG(a.mediaCaps,function(a){b.a=a},function(){throw Error('"capabilities.mediaCaps" must be an array');});if("keySystems"in a){a=a.keySystems;if(!(a instanceof Array))throw Error('"capabilities.keySystems" must be an array');b.b=a.map(function(a){var b;QG(a,function(a){b=dH(a)},function(){throw Error('"capabilities.keySystems" entries must be *Objects');
});return b})}return b},eH=function(){this.f=this.m=this.h=this.l=this.v=this.a=this.w=this.b=this.initDataTypes=this.g=null},dH=function(a){var b=new eH;"keySystemName"in a&&(b.g=String(a.keySystemName));ZG(a.initDataTypes,function(a){b.initDataTypes=a},function(){throw Error('"capabilities.initDataTypes" must be an array');});ZG(a.codecs,function(a){b.b=a},function(){throw Error('"capabilities.codecs" must be an array');});ZG(a.secureCodecs,function(a){b.w=a},function(){throw Error('"capabilities.secureCodecs" must be an array');
});ZG(a.audioRobustness,function(a){b.a=a},function(){throw Error('"capabilities.audioRobustness" must be an array');});ZG(a.videoRobustness,function(a){b.v=a},function(){throw Error('"capabilities.videoRobustness" must be an array');});"persistentLicenseSessionSupport"in a&&(b.l=String(a.persistentLicenseSessionSupport));"persistentReleaseMessageSessionSupport"in a&&(b.h=String(a.persistentReleaseMessageSessionSupport));"persistentStateSupport"in a&&(b.m=String(a.persistentStateSupport));"distinctiveIdentifierSupport"in
a&&(b.f=String(a.distinctiveIdentifierSupport));return b};var fH=function(a){this.j=v("mr.mirror.cast.MessageDispatcher");this.g=a;this.a=null;this.b=new Map;this.f=0},gH=function(a,b,c){if(a.b.has(b))throw Error("Attempt to multiple-subscribe to the same response type: "+b);a.b.set(b,c);a.f=0;z(a.j,"Added subscriber for "+b+"-type messages.");a.a||(a.a=xp(a.g),a.a.onMessage=a.l.bind(a))},hH=function(a,b){a.b.delete(b)&&z(a.j,function(){return"Removed subscriber of "+b+"-type messages."});0==a.b.size&&a.a&&(a.a.Ba(),a.a=null)};
fH.prototype.sendMessage=function(a){return this.a?"RPC"==a.type?this.a.sendMessage(a,{namespace:"urn:x-cast:com.google.cast.remoting"}):this.a.sendMessage(a,{namespace:"urn:x-cast:com.google.cast.webrtc"}):Promise.reject(Error("Require at least one subscriber before sending messages."))};
var iH=function(a,b,c,d,e){var f=null,g=function(){hH(a,c);null!=f&&(clearTimeout(f),f=null)};try{gH(a,c,function(a){e(a)&&g()})}catch(m){e(null,m);return}f=setTimeout(function(){g();e(null,Error("timeout"))},d);a.sendMessage(b).catch(function(a){g();e(null,a)})};
fH.prototype.l=function(a){if(a&&"string"===typeof a.namespace_&&a.namespace_.startsWith("urn:x-cast:com.google.cast.")){var b;do{b=void 0;try{b=SG(a.data)}catch(d){b=d.message;break}if(b.type){var c=this.b.get(b.type);if(c)try{c(b);return}catch(d){b="Error thrown during delivery. Response was: "+(JSON.stringify(b)+". Error from subscriber callback ")+("was: "+d.message+".")}else b="Message was ignored: "+JSON.stringify(b)}else b="Message did not include response type: "+JSON.stringify(b)}while(0);
10>this.f?w(this.j,b):z(this.j,b);++this.f}};var jH=chrome.cast.streaming,lH=function(a,b,c,d,e){this.F=a.sessionId;this.v=a.Be;this.aa=a.Ce;this.f=b;this.K=c;this.B=d;this.H=kH(e,"onAnswer",this.v);this.J=kH(e,"onSessionStop",this.v);this.j=v("mr.mirror.cast.StreamingLaunchWorkflow");this.o=new HG;this.m=this.u=this.w=this.b=this.a=this.h=this.l=this.g=null};
lH.prototype.start=function(a,b,c){var d=this;if(!a&&!b)return Promise.reject(Error("No tracks to stream"));var e=a instanceof mH,f=b instanceof mH;(e&&b&&!f||f&&a&&!e)&&ab("Mixing remoting and non-remoting tracks");return JG(this.o,1,function(e){d.g=a;d.l=b;d.h=c;x(d.j,function(){return"Launching streaming for "+nH(d)+" "+("to a "+d.aa+".")});return oH(d).then(d.A.bind(d)).then(function(a){return pH(d,a).then(function(b){d.H();var c=qH(d,b,a);d.a=rH(d,d.a,c);d.b=rH(d,d.b,c);if(!d.a&&!d.b)throw Error("Receiver did not select any offers from: "+
JSON.stringify(a));d.u=!!b.g;d.m=function(a,b){a==d.a?d.h.jc("Audio stream (id="+a+") error: "+b):a==d.b&&d.h.jc("Video stream (id="+a+") error: "+b)};jH.rtpStream.onError.addListener(d.m);return sH(d,b,c)})}).then(function(){x(d.j,function(){return"Launched streaming for "+nH(d)});d.h.kd(d);NG(e,2);return{rg:null!=d.a?d.a.toString(16):null,ci:null!=d.b?d.b.toString(16):null}})})};
lH.prototype.stop=function(){var a=this;return LG(this.o,function(b){if(!a.g&&!a.l)return NG(b,1),Promise.resolve();x(a.j,function(){return"Stopping streaming for "+nH(a)+"."});a.m&&(jH.rtpStream.onError.removeListener(a.m),a.m=null);var c;a.h?(c=a.h.Md(),a.h=null):c=Promise.resolve();return c.then(function(){a.a&&(jH.rtpStream.stop(a.a),jH.rtpStream.destroy(a.a),a.a=null);a.b&&(jH.rtpStream.stop(a.b),jH.rtpStream.destroy(a.b),a.b=null);a.w&&(jH.udpTransport.destroy(a.w),a.w=null);a.J();x(a.j,function(){return"Stopped streaming for "+
nH(a)+"."});a.g=null;a.l=null;NG(b,1)})})};
var tH=function(a,b){var c=JSON.stringify(b);return Promise.all([a.a&&new Promise(function(b){return jH.rtpStream.getRawEvents(a.a,c,b)}),a.b&&new Promise(function(b){return jH.rtpStream.getRawEvents(a.b,c,b)})]).catch(function(b){a.j.error("Unexpected error when calling getRawEvents()",b);return[]}).then(function(a){return new Blob(a.filter(function(a){return!!a}),{type:"application/gzip"})})},uH=function(a){return Promise.all([a.a&&new Promise(function(b){return jH.rtpStream.getStats(a.a,b)}),a.b&&
new Promise(function(b){return jH.rtpStream.getStats(a.b,b)})]).catch(function(b){a.j.error("Unexpected error when calling getStats()",b);return[]}).then(function(a){return Object.assign.apply(Object,[].concat([{}],la(a.filter(function(a){return!!a}))))})},nH=function(a){var b;if(a.g&&a.l)b="audio+video ";else if(a.g)b="audio-only ";else if(a.l)b="video-only ";else return"stopped";return a.g instanceof mH||a.l instanceof mH?b+"remoting":b+"streaming"},oH=function(a){return new Promise(function(b){var c=
function(c,e,f){c&&!a.g&&(jH.rtpStream.destroy(c),c=null);e&&!a.l&&(jH.rtpStream.destroy(e),e=null);x(a.j,function(){return"Created Cast Streaming session: audioStreamId="+c+", videoStreamId="+e+"."});a.a=c;a.b=e;a.w=f;b()};a.g instanceof mH||a.l instanceof mH?jH.session.create(null,null,c):jH.session.create(a.g,a.l,c)})};
lH.prototype.A=function(){for(var a=Sj(),b=Sj(),c=[],d=ja([this.a,this.b]),e=d.next();!e.done;e=d.next())if(e=e.value)for(var f=e==this.a,g=f?127:96,m=f?Math.floor(499999*Math.random())+1:Math.floor(499999*Math.random())+500001,q=f?48E3:9E4,y=ja(jH.rtpStream.getSupportedParams(e)),D=y.next();!D.done;D=y.next())D=D.value,D.payload.payloadType=g,D.payload.maxLatency=this.f.maxLatencyMillis,D.payload.minLatency=this.f.minLatencyMillis,D.payload.animatedLatency=this.f.animatedLatencyMillis,D.payload.ssrc=
m,D.payload.clockRate=q,D.payload.aesKey=a,D.payload.aesIvMask=b,f?(D.payload.channels=2,D.payload.maxBitrate=this.f.audioBitrate,D.payload.maxFrameRate=100):(D.payload.minBitrate=this.f.minVideoBitrate,D.payload.maxBitrate=this.f.maxVideoBitrate,D.payload.maxFrameRate=this.f.maxFrameRate),c.push(new vH(e,D));return c};
var rH=function(a,b,c){b&&!c.some(function(a){return a.ad==b})&&(w(a.j,"Destroying RTP stream not selected by the receiver: id="+b),jH.rtpStream.destroy(b),b=null);return b},pH=function(a,b){return new Promise(function(c,d){for(var e=[],f=0;f<b.length;++f){var g=b[f].params,m={index:f,codecName:g.payload.codecName.toLowerCase(),rtpProfile:"cast",rtpPayloadType:g.payload.payloadType,ssrc:g.payload.ssrc,targetDelay:g.payload.animatedLatency,aesKey:g.payload.aesKey,aesIvMask:g.payload.aesIvMask,timeBase:"1/"+
g.payload.clockRate,receiverRtcpEventLog:a.f.enableLogging,rtpExtensions:["adaptive_playout_delay"]};a.f.dscpEnabled&&(m.receiverRtcpDscp=46);127==g.payload.payloadType?Object.assign(m,{type:"audio_source",bitRate:0<g.payload.maxBitrate?1E3*g.payload.maxBitrate:60*g.payload.maxFrameRate+g.payload.clockRate*g.payload.channels,sampleRate:g.payload.clockRate,channels:g.payload.channels}):Object.assign(m,{type:"video_source",renderMode:"video",maxFrameRate:Math.round(1E3*g.payload.maxFrameRate)+"/1000",
maxBitRate:1E3*g.payload.maxBitrate,resolutions:[{width:a.f.maxWidth,height:a.f.maxHeight}]});e.push(m)}var q=a.g instanceof mH||a.l instanceof mH?"remoting":"mirroring",y={type:"OFFER",sessionId:a.F,seqNum:Gj(a.K),offer:{castMode:q,receiverGetStatus:!0,supportedStreams:e}};x(a.j,function(){return"Sending OFFER message: "+JSON.stringify(y)});iH(a.B,y,"ANSWER",1E4,function(b,e){if(null==b)d(e);else if("ok"==b.result&&b.a){if(b.b!=y.seqNum)return w(a.j,"Ignoring ANSWER for OFFER with different seqNum: "+
JSON.stringify(b)),!1;((e=b.a.f)&&e!=q||!e&&"mirroring"!=q)&&a.j.error("Expected receiver to ANSWER with castMode="+q+", but got: "+e);z(a.j,function(){return"Received ANSWER: "+JSON.stringify(b)});c(b.a)}else d(Error("Non-OK ANSWER received: "+JSON.stringify(b)));return!0})})},qH=function(a,b,c){if(b.a.length!=b.b.length)return a.j.error("sendIndexes.length != ssrcs.length in ANSWER: "+JSON.stringify(b)),[];for(var d=[],e={},f=0;f<b.a.length;e={candidate:e.candidate},++f){var g=b.a[f];if(0>g||g>=
c.length)return a.j.error("Receiver selected invalid index ("+g+" < "+c.length+") in ANSWER: "+JSON.stringify(b)),[];e.candidate=c[g];if(d.some(function(a){return function(b){return b.ad==a.candidate.ad}}(e)))return a.j.error("Receiver selected same RTP stream twice in ANSWER: "+JSON.stringify(b)),[];e.candidate.params.payload.feedbackSsrc=b.b[g];if(d.some(function(a){return function(b){return b.params.payload.feedbackSsrc==a.candidate.params.payload.feedbackSsrc}}(e)))return a.j.error("Receiver provided same SSRC for two different RTP streams in ANSWER: "+
JSON.stringify(b)),[];d.push(e.candidate)}return d},sH=function(a,b,c){var d=null,e=function(){d&&(jH.rtpStream.onStarted.removeListener(d),d=null)};return(new Promise(function(e,g){var f=b.l||2344;x(a.j,function(){return"Starting RTP streams to receiver at "+(a.v+":"+f)+(" for selected offers: "+JSON.stringify(c))});var q=a.w||-1;a.f.dscpEnabled&&(x(a.j,"Enabled DSCP in sender."),jH.udpTransport.setOptions(q,{DSCP:!0}));jH.udpTransport.setDestination(q,{address:a.v,port:f});var y=new Set(c.map(function(a){return a.ad}));
d=function(a){y.delete(a);0==y.size&&e()};jH.rtpStream.onStarted.addListener(d);for(var q=ja(c),D=q.next();!D.done;D=q.next())D=D.value,jH.rtpStream.toggleLogging(D.ad,a.f.enableLogging),jH.rtpStream.start(D.ad,D.params);setTimeout(function(){g(Error("Timeout: RTP streams failed to start."))},1E4)})).then(e).catch(function(a){e();throw a;})},kH=function(a,b,c){var d=this;return a&&b in a?function(){try{a[b](c)}catch(e){d.j.error("Error from testHooks."+b,e)}}:function(){}},vH=function(a,b){this.ad=
a;this.params=b},mH=function(){};var xH=function(a,b,c,d,e,f){b=Math.floor(16777216*Math.random()).toString(16);b="000000".substring(b.length)+b;var g=a.mediaSource.replace("urn:x-org.chromium.media:source:tab:","urn:x-org.chromium.media:source:tab_content_remoting:");b=Ej("undefined","cast",b,g,!0,a.description,a.iconUrl);b.forDisplay=!1;b.offTheRecord=a.offTheRecord;b.kb=a.kb;this.u=b;this.o=new lH(this.u.kb,c,d,e,f);this.m=e;this.g=new HG;this.b=new wH;this.j=v("mr.mirror.cast.MediaRemoter");this.a=this.l=this.h=this.f=this.w=
null};h=xH.prototype;h.start=function(a,b){var c=this;return JG(this.g,1,function(d){c.w=a;c.f=b;c.h=To(c);NG(d,2);return Promise.resolve()})};h.stop=function(){var a=this;return LG(this.g,function(b){Uo(a);a.a=null;var c=a.l;a.l=null;a.h=null;a.f=null;a.w=null;return new Promise(function(d){window.setTimeout(function(){yH(a).then(function(){NG(b,1);d();c&&c()})},250)})})};h.Ib=function(){return this.u};
h.Ok=function(a){var b=this;z(this.j,function(){return"Processing control message: "+a});var c=a.split(":"),d=(c.find(function(a){return a.startsWith("session=")})||"").substring(8);if(0==d.length)return this.j.error("Missing valid 'session' field in control message: "+a),Promise.resolve();switch(c[0]){case "START_CAST_REMOTING":return zH(this,d).catch(function(a){b.j.error("Failed to start remoting",a);AH(b,d)});case "START_CAST_REMOTING_STREAMS":var e=c.some(function(a){return"audio=Y"==a}),c=c.some(function(a){return"video=Y"==
a});return BH(this,d,e,c).then(this.v.bind(this)).catch(function(a){b.j.error("Failed to start remoting streams",a);AH(b,d)});case "STOP_CAST_REMOTING":return CH(this,d).then(function(){b.v("STOPPED_CAST_REMOTING:session="+d)}).catch(function(a){b.j.error("Failed to stop remoting",a);AH(b,d)});default:this.j.error("Unknown control message: "+a)}return Promise.resolve()};h.vl=function(a){return DH(this.b,a)};h.kd=function(a){this.f&&this.f.kd(a)};h.Md=function(){return this.f?this.f.Md():Promise.resolve()};
h.jc=function(a,b){this.j.error("Error during streaming: "+a,b);null!=this.a&&AH(this,this.a)};
var zH=function(a,b){if(null!=a.a)return Promise.reject(Error("Attempt to start a second session ("+b+") during existing session ("+a.a+")."));a.a=b;var c=!1;x(a.j,function(){c=!0;return"Starting next media remoting session (id="+b+")."});c&&EH(a.b,function(b){return x(a.j,b)});FH(a.b);return JG(a.g,2,function(b){return(0,a.w)().then(function(c){a.l=c;gH(a.m,"RPC",function(b){if(b.rpc){var c=a.b;b=b.rpc;c.h&&(++c.m,c.b+=b.length,c.h(b))}});NG(b,3)}).catch(function(c){return yH(a).then(function(){NG(b);
throw c;})})})},BH=function(a,b,c,d){return a.a!=b?Promise.reject(Error("Cannot start streams for an unknown session ("+b+") during existing session ("+a.a+")")):JG(a.g,3,function(e){return a.o.start(c?new mH:null,d?new mH:null,a).then(function(c){var d="STARTED_CAST_REMOTING_STREAMS:session="+b;c.rg&&(d+=":audio_stream_id="+c.rg);c.ci&&(d+=":video_stream_id="+c.ci);GH(a.b,function(b){return a.m.sendMessage(b)},function(b){var c=a.h;c.a&&c.a(b)});NG(e,4);return d}).catch(function(b){return yH(a).then(function(){NG(e);
throw b;})})})},CH=function(a,b){if(a.a!=b)return Promise.reject(Error("Attempting to stop an unknown session ("+b+") during existing session ("+a.a+")."));a.a=null;return new Promise(function(b,d){KG(a.g,function(c){return yH(a).then(function(){NG(c,5);(0,a.l)().then(function(){return JG(a.g,5,function(a){NG(a,2);return Promise.resolve()})}).then(b,function(a){return d(a)});a.l=null})}).catch(function(a){return d(a)})})},yH=function(a){return a.o.stop().then(function(){hH(a.m,"RPC");HH(a.b);IH(a.b)})};
xH.prototype.v=function(a){this.h?(z(this.j,function(){return"Sending control message to browser: "+a}),Vo(this.h,a)):z(this.j,function(){return"Dropping control message to browser: "+a})};
var AH=function(a,b){a.v("FAILED_CAST_REMOTING:session="+b);a.stop()},Ro=function(){return zp(Wg.get("mr.ProviderManager")||null,"cast")},wH=function(){this.h=this.l=this.a=null;this.v=this.b=this.m=this.f=this.w=0;this.g=null},FH=function(a){a.a=[];JH(a,performance.now())},GH=function(a,b,c){a.l=b;a.h=c;a.a?(b=a.a,a.a=null,b.forEach(function(b){return DH(a,b.data).then(b.xl,b.Kh)})):JH(a,performance.now())},HH=function(a){if(a.a){var b=Error("Stop before delivering pending message");a.a.forEach(function(a){return a.Kh(b)});
a.a=null}a.l=null;a.h=null},DH=function(a,b){if(a.l){var c=btoa(String.fromCharCode.apply(null,b));++a.w;a.f+=b.length;return a.l({type:"RPC",rpc:c})}return a.a?new Promise(function(c,e){a.a.push({data:b,xl:c,Kh:e})}):Promise.reject(Error("RPC pipe not started"))},EH=function(a,b){IH(a);a.g=setInterval(function(){var c;if(a.a)c=a.a.length+" messages are waiting to send.";else{c=performance.now();var d=(c-a.v)/1E3,d="Over the past "+d.toFixed(1)+" seconds, sent "+(a.w+" messages ("+Math.round(a.f/
d)+" bytes/sec) and ")+("received "+a.m+" messages ("+Math.round(a.b/d)+" ")+"bytes/sec).";JH(a,c);c=d}b(c)},3E4)},IH=function(a){null!=a.g&&(clearInterval(a.g),a.g=null)},JH=function(a,b){a.w=0;a.f=0;a.m=0;a.b=0;a.v=b};var KH=function(a){return a&&a.getAudioTracks()&&0<a.getAudioTracks().length?a.getAudioTracks()[0]:null},LH=function(a){return a&&a.getVideoTracks()&&0<a.getVideoTracks().length?a.getVideoTracks()[0]:null};var MH=function(a,b,c,d,e){this.f=new lH(a,b,c,d,void 0===e?null:e);this.j=v("mr.mirror.cast.MediaStreamer");this.l=new HG;this.g=this.b=this.a=this.h=null};MH.prototype.start=function(a,b){var c=this;return JG(this.l,1,function(d){c.h=a;c.a=KH(a);c.a&&"ended"==c.a.readyState&&(c.a=null);c.b=LH(a);c.b&&"ended"==c.b.readyState&&(c.b=null);if(!c.a&&!c.b)return NG(d),Promise.reject(Error("No MediaStream tracks to stream."));c.g=b;return c.f.start(c.a,c.b,c.g).then(function(){return NG(d,2)})})};
MH.prototype.stop=function(){var a=this;return LG(this.l,function(b){return a.f.stop().then(function(){a.a=null;a.b=null;a.h=null;a.g=null;NG(b,1)})})};var NH=function(a){return JG(a.l,2,function(b){x(a.j,"Suspending media streaming...");return a.f.stop().then(function(){x(a.j,"Suspended media streaming.");NG(b,3)})})};
MH.prototype.resume=function(){var a=this;return JG(this.l,3,function(b){a.a&&"ended"==a.a.readyState&&(a.a=null);a.b&&"ended"==a.b.readyState&&(a.b=null);if(!a.a&&!a.b)return Promise.reject(Error("Cannot resume: All tracks have ended."));x(a.j,"Resuming media streaming...");return a.f.start(a.a,a.b,a.g).then(function(){x(a.j,"Resumed media streaming.");NG(b,2)})})};var OH=function(a,b,c){this.l=a;this.g=b;this.f=c;this.j=v("mr.mirror.cast.WifiStatusMonitor");this.a=null;this.b=[]};OH.prototype.start=function(){var a=this;null==this.a&&(z(this.j,"Starting Wifi Status Monitoring."),this.b=[],gH(this.f,"STATUS_RESPONSE",function(b){return PH(a,b)}),this.a=setInterval(function(){return QH(a)},12E4),QH(this))};OH.prototype.stop=function(){null!=this.a&&(z(this.j,"Stopping Wifi Status Monitoring."),clearInterval(this.a),this.a=null,hH(this.f,"STATUS_RESPONSE"))};
var PH=function(a,b){if(null!=a.a)if(b.status){var c={};null!=b.status.a&&(c.wifiSnr=b.status.a);null!=b.status.b&&(c.wifiSpeed=b.status.b[3]);0==Object.keys(c).length?w(a.j,function(){return"No status fields populated in response: "+JSON.stringify(b)}):(c.timestamp=Date.now(),30==a.b.length&&a.b.shift(),a.b.push(c),x(a.j,function(){return"Current Wifi status: "+JSON.stringify(c)}))}else w(a.j,function(){return"Ignoring response without status: "+JSON.stringify(b)})},QH=function(a){a.f.sendMessage({type:"GET_STATUS",
sessionId:a.l,seqNum:Gj(a.g),get_status:["wifiSnr","wifiSpeed"]})};var RH=function(a,b,c,d){this.B=b.Be;this.m={extVersion:chrome.runtime.getManifest().version,extChannel:"public",mirrorSettings:zh(a),sender:navigator.userAgent||"UNKNOWN",receiverProductName:b.Ce};this.A=c;this.u=d;this.g=this.b=this.w=this.h=this.l=this.v=this.f=null;this.a=[]};RH.prototype.kd=function(a){null!=this.b&&clearInterval(this.b);this.f=a;this.v=Date.now();this.b=setInterval(this.o.bind(this,a),9E5)};
RH.prototype.Md=function(){null!=this.b&&(clearInterval(this.b),this.b=null);if(null!=this.f){var a=this.o(this.f);this.f=null;return a}return Promise.resolve()};RH.prototype.jc=function(a,b){null==this.l&&(this.l=Date.now(),"function"===typeof a?this.h=a():"string"===typeof a&&(this.h=a),b&&"string"===typeof b.stack&&(this.w=b.stack))};
var TH=function(a,b){return(null==a.f?Promise.resolve():a.o(a.f)).then(function(){var c=b.map(function(b){b=SH(a,b);var c=b.map(function(a){return a.events}).filter(function(a){return null!=a}),d=["["];b.map(function(a){return a.Zc}).forEach(function(a,b){0<b&&d.push(",");d.push(a)});d.push("]");return{events:new Blob(c,{type:"application/gzip"}),Zc:new Blob(d,{type:"application/json"})}});a.a=[];return c})};
RH.prototype.o=function(a){var b=this;if(null!=this.g)return this.g;var c=Le(this.B).then(function(c){c={receiverVersion:c.a,receiverConnected:c.g,receiverOnEthernet:c.f,receiverHasUpdatePending:c.b,receiverUptimeSeconds:c.l};Object.assign(c,b.m);var d=Date.now();Object.assign(c,{startTime:b.v,endTime:d,activity:nH(a),receiverWifiStatus:Array.from(b.u.b)});b.v=d;null!=b.l&&(Object.assign(c,{streamingErrorTime:b.l,streamingErrorMessage:b.h,streamingErrorCause:b.w}),b.l=null,b.h=null,b.w=null);return c});
return(this.g=Promise.all([c.then(function(b){return tH(a,b)}),c,uH(a)]).then(function(a){var c=ja(a);a=c.next().value;var d=c.next().value,c=c.next().value;b.a.push({events:a,Zc:new Blob([JSON.stringify(Object.assign({tags:d},c))],{type:"application/json"})});b.a=SH(b,b.A);b.g=null}))||Promise.resolve()};
var SH=function(a,b){b-=2;for(var c=[],d=a.a.length-1;0<=d;--d){b-=a.a[d].Zc.size+1;if(0>b)break;c.push({events:null,Zc:a.a[d].Zc});if(null!=a.a[d].events){var e=a.a[d].events.size;b>=e&&(c[c.length-1].events=a.a[d].events,b-=e)}}return c.reverse()};var UH=function(a,b,c,d){d=void 0===d?null:d;wh.call(this,b);var e=b.kb;this.v=e.sessionId;this.K=e.Be;this.B=a;this.F=d;this.j=v("mr.mirror.cast.Session");this.w=new HG;this.m=new Fj("mirror.cast.SeqNumGenerator");this.h=new fH(b.id);this.l=new MH(e,this.B,this.m,this.h,this.F);this.g=null;this.a=new RH(a,e,c,new OH(this.v,this.m,this.h));this.f=!1;this.u=null};ka(UH,wh);h=UH.prototype;
h.start=function(a){var b=this;return JG(this.w,1,function(c){var d=new sg("MediaRouter.CastStreaming.Session.Launch");return VH(b).then(function(c){b.f=c;return b.l.start(a,b)}).then(function(){if(b.l.f.u){var a=b.a;a.m.tdlsIsOn=b.f;a.u.start();WH(b)}else b.a.m.tdlsIsOn=b.f;d.a();b.u=new yg("MediaRouter.CastStreaming.Session.Length");NG(c,2);return b})})};
h.stop=function(){var a=this;return LG(this.w,function(b){a.u&&(a.u.a(),a.u=null);a.a.u.stop();return a.l.stop().then(function(){return a.g?a.g.stop():Promise.resolve()}).then(function(){a.g=null;return a.f?XH(a):Promise.resolve()}).then(function(){a.f=!1;NG(b,4)})})};
h.Jh=function(){var a={sessionId:this.v,seqNum:Gj(this.m),type:"PRESENTATION",icons:[]};this.A&&(a.title=this.A);this.iconUrl&&!this.o&&a.icons.push({url:this.iconUrl});x(this.j,"Sending session metadata update to receiver: "+this.v);this.h.sendMessage(a)};h.kd=function(a){this.a.kd(a)};h.Md=function(){return this.a.Md()};h.jc=function(a,b){this.a.jc(a,b);this.j.error(a,b);this.stop()};
var YH=function(a,b){return TH(a.a,b)},VH=function(a){return a.B.useTdls?GG(a.K,!0).then(function(b){if("Connected"==b)return x(a.j,"Successfully enabled TDLS."),!0;w(a.j,"Did not enable TDLS: result="+b);return!1}).catch(function(b){w(a.j,"Error while calling enableTDLS()",b);return!1}):Promise.resolve(!1)},XH=function(a){return GG(a.K,!1).catch(function(b){return a.j.error("Error while turning TDLS back off",b)})},WH=function(a){ZH(a).then(function(b){(b.a||[]).includes("video")?$H(a):w(a.j,function(){return"Receiver incapable of Media Remoting: "+
JSON.stringify(b)})}).catch(function(b){w(a.j,"None/Invalid capabilites response. Media Remoting disabled.",b)})},ZH=function(a){return new Promise(function(b,c){var d={type:"GET_CAPABILITIES",sessionId:a.v,seqNum:Gj(a.m)};x(a.j,function(){return"Sending GET_CAPABILITIES message: "+JSON.stringify(d)});iH(a.h,d,"CAPABILITIES_RESPONSE",3E4,function(e,f){if(null==e)return c(f),!0;if("ok"!=e.result||!e.capabilities)return c(Error("Bad response: "+JSON.stringify(e))),!0;if(e.b!=d.seqNum)return x(a.j,function(){return"Ignoring CAPABILITIES_RESPONSE with different seqNum: "+
JSON.stringify(e)}),!1;z(a.j,function(){return"Received CAPABILITIES_RESPONSE: "+JSON.stringify(e)});b(e.capabilities);return!0})})},$H=function(a){JG(a.w,2,function(b){var c=a.b.kb.Ce||"<UNKNOWN>";if(!c.startsWith("Chromecast")&&!c.startsWith("Eureka Dongle"))return w(a.j,"HACK: Media Remoting disabled because the receiver model--"+('"'+c+'" according to discovery--is not a Chromecast.')),NG(b),Promise.resolve();a.g=new xH(a.b,0,a.B,a.m,a.h,a.F);return a.g.start(a.aa.bind(a),a).catch(function(b){a.j.error("Media Remoting start failed: "+
b.message,b)}).then(function(){return NG(b)})})};UH.prototype.aa=function(){var a=this;return JG(this.w,2,function(b){return new Promise(function(c,d){NH(a.l).then(function(){NG(b,3);c(a.H.bind(a))}).catch(function(b){a.jc("Failed to suspend MediaStreamer before starting remoting",b);d(b)})})})};
UH.prototype.H=function(){var a=this;return JG(this.w,3,function(b){return new Promise(function(c,d){a.l.resume().then(function(){NG(b,2);c()}).catch(function(b){a.jc("Failed resume MediaStreamer after ending remoting mode",b);d(b)})})})};var aI=function(){gh.call(this,"cast_streaming")};ka(aI,gh);h=aI.prototype;h.getName=function(){return"cast_streaming"};h.be=function(a,b){return new UH(a,b,Math.max(20969472,9351424),null)};h.Ae=function(){xG(0)};h.xe=function(){xG(1)};h.zf=function(){xG(2)};h.ye=function(){Bg("MediaRouter.CastStreaming.Session.End")};h.ze=function(a){Cg("MediaRouter.CastStreaming.Start.Failure",a,Eg)};h.Ed=function(){Bg("MediaRouter.CastStreaming.Stream.End")};
h.Qe=function(a){var b=this;return(new Promise(function(a){return chrome.metricsPrivate.getIsCrashReportingEnabled(a)})).then(function(c){var d=c&&EG(),e=[9351424];d&&e.push(20969472);return YH(a,e).then(function(a){var e=a[a.length-1];a=fi(a[0].events).catch(function(a){b.G.error("Failed to persist events Blob.",a)});d&&0<e.events.size?AG(e.events,void 0,b.mk.bind(b)):c&&zG("stats.json",e.Zc,void 0,void 0);return a})})};h.mk=function(a){a&&(DG().a=Date.now())};
h.gf=function(a){x(this.G,"Received message to upload logs for "+a);return this.a?YH(this.a,[20969472]).then(function(b){b=ja(b).next().value;return 0==b.events.size?"":AG(b.events,a)}):Promise.resolve(bI(this,a))};
var bI=function(a,b){var c;c=window.localStorage.getItem("mr.temp.mirror.cast.Service.eventsBlob");if(null==c||1>c.length)c=null;else{for(var d=new Uint16Array(c.length),e=0;e<c.length;++e)d[e]=c.charCodeAt(e);c=d.buffer;d=(new Uint8Array(c,c.byteLength-1,1))[0];c=new Uint8Array(c,0,c.byteLength-(0==d?2:1));c=new Blob([c],{type:"application/gzip"})}if(null!=c&&0!=c.size)return fi(new Blob),x(a.G,"Uploading saved logs for feedback."),AG(c,b)},cI=new aI;eh("mr.mirror.cast.Service",cI);
