'use strict';var Fx={TAB:0,sg:1,pj:2},Gx=function(a){ib("MediaRouter.CastStreaming.Start.Success",a,Fx)};var Hx=Pa("mr.mirror.cast.LogUploader"),Jx=function(a,b,c){Ix("raw_events.log.gz",a,b,c);return b?"https://crash.corp.google.com/samples?stbtiq="+b:""},Ix=function(a,b,c,d){if(0==b.size)Hx.info("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);Oj("https://clients2.google.com/cr/report",function(a){a=a.target;var b=null;Yj(a)?(b=Zj(a),Hx.info("Upload to Crash succeeded: "+
b)):Hx.info("Upload to Crash failed. HTTP status: "+a.Ca());d&&d(b)},"POST",e,void 0,3E4)}};var Kx=function(){this.a=0;Jg(this)},Mx=function(){Lx||(Lx=new Kx);return Lx},Nx=function(){var a=Mx(),b={fraction:.01,autoSubmitTimeLimitMillis:6048E5},c=b.autoSubmitTimeLimitMillis,d=Date.now();return a.a&&c&&d-a.a<c?!1:Math.random()<b.fraction};Kx.prototype.ha=function(){return"mirror.cast.LogUtils"};Kx.prototype.xa=function(){return[void 0,{lastAutoSubmitMillis:this.a}]};Kx.prototype.Da=function(){var a=Hg(this);this.a=a&&a.lastAutoSubmitMillis||0};var Lx=null;Pa("mr.mirror.cast.LogUtils");var Ox=Pa("mr.NetworkUtils"),Px=function(a,b){return of?new Promise(function(c,d){chrome.networkingPrivate.setWifiTDLSEnabledState(a,b,function(a){chrome.runtime.lastError?(v(Ox,"Unable to set TDLS state: state = "+b+", error = "+chrome.runtime.lastError.message),d("Unable to set TDLS state to "+b+".")):(Ox.info("TDLS state changed: state = "+b+", status = "+a),c(a))})}):Promise.reject("TDLS feature not enabled.")};var Qx=function(){this.a=Promise.resolve(1)},Sx=function(a,b,c){return Rx(a,function(a){return a==b},c)},Tx=function(a,b){var c=[3,4];return Rx(a,function(a){return-1!=c.indexOf(a)},b)},Ux=function(a,b){a.a=a.a.catch(function(){return 1});return Rx(a,function(){return!0},b)},Rx=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 Vx(a);try{var g=c(f)}catch(u){g=
Promise.reject(u)}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},Vx=function(a){this.b=a;this.a=null},Wx=function(a,b){a.a="number"===typeof b?b:a.b};var Xx={jq:"OFFER",cn:"ANSWER",Dq:"PRESENTATION",wo:"GET_STATUS",rr:"STATUS_RESPONSE",vo:"GET_CAPABILITIES",yn:"CAPABILITIES_RESPONSE",dr:"RPC"};var Yx=function(){this.capabilities=this.status=this.a=this.error=this.rpc=this.result=this.type=this.b=this.sessionId=null},ay=function(a){try{if("string"!==typeof a)throw SyntaxError("Cannot parse non-string as JSON");var b;Zx(JSON.parse(a),function(a){b=$x(a)},function(){throw Error("non-Object result from JSON parse");});return b}catch(d){var c=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(c+". Input was "+a);},$x=function(a){var b=new Yx;null!=a.sessionId&&(b.sessionId=String(a.sessionId));by(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(Xx)),e=d.next();!e.done;e=d.next())if(Xx[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(k(atob(a.rpc))).map(function(a){return a.charCodeAt(0)}))}Zx(a.error,function(a){b.error=cy(a)},function(){throw Error('"error" must be an Object');});Zx(a.answer,function(a){b.a=dy(a)},function(){throw Error('"answer" must be an Object');});Zx(a.status,function(a){b.status=ey(a)},function(){throw Error('"status" must be an Object');
});Zx(a.capabilities,function(a){b.capabilities=fy(a)},function(){throw Error('"capabilities" must be an Object');});return b},Zx=function(a,b,c){void 0!==a&&(a instanceof Object?b(a):c())},by=function(a,b,c){void 0!==a&&("number"!==typeof a?c():b(a))},gy=function(a,b,c){void 0!==a&&(a instanceof Array&&a.every(function(a){return"number"===typeof a})?b(a):c())},hy=function(a,b,c){void 0!==a&&(a instanceof Array?b(a.map(function(a){return String(a)})):c())},iy=function(){this.l=null;this.a=[];this.b=
[];this.f=this.g=this.h=null},dy=function(a){var b=new iy;by(a.udpPort,function(a){b.l=a},function(){throw Error('"answer.udpPort" must be a number');});gy(a.sendIndexes,function(a){b.a=a},function(){throw Error('"answer.sendIndexes" must be an array of numbers');});gy(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},jy=function(){this.details=this.description=this.code=null},cy=function(a){var b=new jy;by(a.code,function(a){b.code=a},function(){throw Error('"error.code" must be a number');});"description"in a&&(b.description=String(a.description));Zx(a.details,function(a){b.details=a},function(){throw Error('"error.details" must be an Object');});return b},ky=function(){this.b=this.a=null},ey=function(a){var b=new ky;by(a.wifiSnr,function(a){b.a=a},function(){throw Error('"status.wifiSnr" must be a number');
});gy(a.wifiSpeed,function(a){b.b=a},function(){throw Error('"status.wifiSpeed" must be an array of numbers');});return b},ly=function(){this.b=this.a=null},fy=function(a){var b=new ly;hy(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;Zx(a,function(a){b=my(a)},function(){throw Error('"capabilities.keySystems" entries must be *Objects');
});return b})}return b},ny=function(){this.f=this.m=this.h=this.l=this.v=this.a=this.u=this.b=this.initDataTypes=this.g=null},my=function(a){var b=new ny;"keySystemName"in a&&(b.g=String(a.keySystemName));hy(a.initDataTypes,function(a){b.initDataTypes=a},function(){throw Error('"capabilities.initDataTypes" must be an array');});hy(a.codecs,function(a){b.b=a},function(){throw Error('"capabilities.codecs" must be an array');});hy(a.secureCodecs,function(a){b.u=a},function(){throw Error('"capabilities.secureCodecs" must be an array');
});hy(a.audioRobustness,function(a){b.a=a},function(){throw Error('"capabilities.audioRobustness" must be an array');});hy(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 oy=function(a){this.j=Pa("mr.mirror.cast.MessageDispatcher");this.g=a;this.a=null;this.b=new Map;this.f=0},py=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;w(a.j,"Added subscriber for "+b+"-type messages.");a.a||(a.a=Qr(a.g),a.a.onMessage=a.l.bind(a))},qy=function(a,b){a.b.delete(b)&&w(a.j,function(){return"Removed subscriber of "+b+"-type messages."});0==a.b.size&&a.a&&(a.a.wa(),a.a=null)};
oy.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 ry=function(a,b,c,d,e){var f=null,g=function(){qy(a,c);null!=f&&(clearTimeout(f),f=null)};try{py(a,c,function(a){e(a)&&g()})}catch(l){e(null,l);return}f=setTimeout(function(){g();e(null,Error("timeout"))},d);a.sendMessage(b).catch(function(a){g();e(null,a)})};
oy.prototype.l=function(a){if(a&&"string"===typeof a.namespace_&&a.namespace_.startsWith("urn:x-cast:com.google.cast.")){do{var b=void 0;try{b=ay(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?v(this.j,b):w(this.j,b);++this.f}};var sy=chrome.cast.streaming,uy=function(a,b,c,d,e){this.X=a.sessionId;this.v=a.Yd;this.F=a.Zd;this.f=b;this.I=c;this.B=d;this.H=ty(e,"onAnswer",this.v);this.K=ty(e,"onSessionStop",this.v);this.j=Pa("mr.mirror.cast.StreamingLaunchWorkflow");this.s=new Qx;this.m=this.w=this.u=this.b=this.a=this.h=this.l=this.g=null};
uy.prototype.start=function(a,b,c){var d=this;if(!a&&!b)return Promise.reject(Error("No tracks to stream"));var e=a instanceof vy,f=b instanceof vy;(e&&b&&!f||f&&a&&!e)&&Hb("Mixing remoting and non-remoting tracks");return Sx(this.s,1,function(e){d.g=a;d.l=b;d.h=c;d.j.info(function(){return"Launching streaming for "+wy(d)+" "+("to a "+d.F+".")});return xy(d).then(d.A.bind(d)).then(function(a){return yy(d,a).then(function(b){d.H();var c=zy(d,b,a);d.a=Ay(d,d.a,c);d.b=Ay(d,d.b,c);if(!d.a&&!d.b)throw Error("Receiver did not select any offers from: "+
JSON.stringify(a));d.w=!!b.g;d.m=function(a,b){a==d.a?d.h.vc("Audio stream (id="+a+") error: "+b):a==d.b&&d.h.vc("Video stream (id="+a+") error: "+b)};sy.rtpStream.onError.addListener(d.m);return By(d,b,c)})}).then(function(){d.j.info(function(){return"Launched streaming for "+wy(d)});d.h.yd(d);Wx(e,2);return{zk:d.a,Fm:d.b}})})};
uy.prototype.stop=function(){var a=this;return Ux(this.s,function(b){if(!a.g&&!a.l)return Wx(b,1),Promise.resolve();a.j.info(function(){return"Stopping streaming for "+wy(a)+"."});a.m&&(sy.rtpStream.onError.removeListener(a.m),a.m=null);if(a.h){var c=a.h.ce();a.h=null}else c=Promise.resolve();return c.then(function(){a.a&&(sy.rtpStream.stop(a.a),sy.rtpStream.destroy(a.a),a.a=null);a.b&&(sy.rtpStream.stop(a.b),sy.rtpStream.destroy(a.b),a.b=null);a.u&&(sy.udpTransport.destroy(a.u),a.u=null);a.K();a.j.info(function(){return"Stopped streaming for "+
wy(a)+"."});a.g=null;a.l=null;Wx(b,1)})})};
var Cy=function(a,b){var c=JSON.stringify(b);return Promise.all([a.a&&new Promise(function(b){return sy.rtpStream.getRawEvents(a.a,c,b)}),a.b&&new Promise(function(b){return sy.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"})})},Dy=function(a){return Promise.all([a.a&&new Promise(function(b){return sy.rtpStream.getStats(a.a,b)}),a.b&&
new Promise(function(b){return sy.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([{}],k(a.filter(function(a){return!!a}))))})},wy=function(a){if(a.g&&a.l)var b="audio+video ";else if(a.g)b="audio-only ";else if(a.l)b="video-only ";else return"stopped";return a.g instanceof vy||a.l instanceof vy?b+"remoting":b+"streaming"},xy=function(a){return new Promise(function(b){var c=
function(c,e,f){c&&!a.g&&(sy.rtpStream.destroy(c),c=null);e&&!a.l&&(sy.rtpStream.destroy(e),e=null);a.j.info(function(){return"Created Cast Streaming session: audioStreamId="+c+", videoStreamId="+e+"."});a.a=c;a.b=e;a.u=f;b()};a.g instanceof vy||a.l instanceof vy?sy.session.create(null,null,c):sy.session.create(a.g,a.l,c)})};
uy.prototype.A=function(){for(var a=Tu(),b=Tu(),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,l=f?Math.floor(499999*Math.random())+1:Math.floor(499999*Math.random())+500001,r=f?48E3:9E4,u=ja(sy.rtpStream.getSupportedParams(e)),z=u.next();!z.done;z=u.next())z=z.value,z.payload.payloadType=g,z.payload.maxLatency=this.f.maxLatencyMillis,z.payload.minLatency=this.f.minLatencyMillis,z.payload.animatedLatency=this.f.animatedLatencyMillis,z.payload.ssrc=
l,z.payload.clockRate=r,z.payload.aesKey=a,z.payload.aesIvMask=b,f?(z.payload.channels=2,z.payload.maxBitrate=this.f.audioBitrate,z.payload.maxFrameRate=100):(z.payload.minBitrate=this.f.minVideoBitrate,z.payload.maxBitrate=this.f.maxVideoBitrate,z.payload.maxFrameRate=this.f.maxFrameRate),c.push(new Ey(e,z));return c};
var Ay=function(a,b,c){b&&!c.some(function(a){return a.od==b})&&(v(a.j,"Destroying RTP stream not selected by the receiver: id="+b),sy.rtpStream.destroy(b),b=null);return b},yy=function(a,b){return new Promise(function(c,d){for(var e=[],f=0;f<b.length;++f){var g=b[f].params,l={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&&(l.receiverRtcpDscp=46);127==g.payload.payloadType?Object.assign(l,{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(l,{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(l)}var r=a.g instanceof vy||a.l instanceof vy?"remoting":"mirroring",u={type:"OFFER",sessionId:a.X,seqNum:ik(a.I),offer:{castMode:r,receiverGetStatus:!0,supportedStreams:e}};a.j.info(function(){return"Sending OFFER message: "+JSON.stringify(u)});ry(a.B,u,"ANSWER",1E4,function(b,e){if(null==b)d(e);else if("ok"==b.result&&b.a){if(b.b!=u.seqNum)return v(a.j,"Ignoring ANSWER for OFFER with different seqNum: "+
JSON.stringify(b)),!1;((e=b.a.f)&&e!=r||!e&&"mirroring"!=r)&&a.j.error("Expected receiver to ANSWER with castMode="+r+", but got: "+e);w(a.j,function(){return"Received ANSWER: "+JSON.stringify(b)});c(b.a)}else d(Error("Non-OK ANSWER received: "+JSON.stringify(b)));return!0})})},zy=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.od==a.candidate.od}}(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},By=function(a,b,c){var d=null,e=function(){d&&(sy.rtpStream.onStarted.removeListener(d),d=null)};return(new Promise(function(e,g){var f=b.l||2344;a.j.info(function(){return"Starting RTP streams to receiver at "+(a.v+":"+f)+(" for selected offers: "+JSON.stringify(c))});var r=a.u||-1;a.f.dscpEnabled&&(a.j.info("Enabled DSCP in sender."),sy.udpTransport.setOptions(r,{DSCP:!0}));sy.udpTransport.setDestination(r,{address:a.v,port:f});var u=new Set(c.map(function(a){return a.od}));
d=function(a){u.delete(a);0==u.size&&e()};sy.rtpStream.onStarted.addListener(d);r=ja(c);for(var z=r.next();!z.done;z=r.next())z=z.value,sy.rtpStream.toggleLogging(z.od,a.f.enableLogging),sy.rtpStream.start(z.od,z.params);setTimeout(function(){g(Error("Timeout: RTP streams failed to start."))},1E4)})).then(e).catch(function(a){e();throw a;})},ty=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(){}},Ey=function(a,b){this.od=
a;this.params=b},vy=function(){};var Hy=function(a,b,c,d,e,f){this.h=a;this.A=Fy(b,this.h.sb);this.w=new uy(this.h.sb,c,d,e,f);this.v=e;this.g=new Qx;this.b=new Gy;this.u=new mojo.Binding(mojo.MirrorServiceRemoter,this,null);this.j=Pa("mr.mirror.cast.MediaRemoter");this.m=this.a=this.l=this.f=this.s=null};
Hy.prototype.Ga=function(a,b){var c=this;return Sx(this.g,1,function(d){c.s=a;c.f=b;var e=c.u.createInterfacePtrAndBind();c.u.setConnectionErrorHandler(function(){c.j.info("Remoter mojo pipe connection error.");Iy(c)});c.a=new mojo.MirrorServiceRemotingSourcePtr;var f=Kf(c.h.mediaSource||"");if(!f)throw Error("Failed to parse tab ID from source:\n          "+c.h.mediaSource);c.j.info("Connecting remoter to browser: tabId="+f);(Nf.get("mr.ProviderManager")||null).onMediaRemoterCreated(f,e,mojo.makeRequest(c.a));
c.a.ptr.setConnectionErrorHandler(function(){c.j.info("RemotingSource mojo pipe connection error.");Iy(c)});c.a.onSinkAvailable(c.A);Wx(d,2);return Promise.resolve()})};var Iy=function(a){return Ux(a.g,function(b){a.a&&(a.a.ptr.reset(),a.a=null);var c=a.l;a.l=null;a.f=null;a.s=null;a.u.close();return new Promise(function(d){window.setTimeout(function(){Jy(a).then(function(){Wx(b,1);d();c&&c()})},250)})})};Hy.prototype.mm=function(a){Ky(this.b,a)};Hy.prototype.sendMessageToSink=Hy.prototype.mm;
Hy.prototype.yd=function(a){this.f&&this.f.yd(a)};Hy.prototype.ce=function(){return this.f?this.f.ce():Promise.resolve()};Hy.prototype.vc=function(a,b){this.j.error("Error during streaming: "+a,b);if(this.a)this.a.onError();Iy(this)};
Hy.prototype.start=function(){var a=this,b=!1;this.j.info(function(){b=!0;return"Starting next media remoting session."});b&&Ly(this.b,function(b){return a.j.info(b)});My(this.b);Sx(this.g,2,function(b){return(0,a.s)().then(function(c){a.l=c;py(a.v,"RPC",function(b){if(b.rpc){var c=a.b;b=b.rpc;c.h&&(++c.m,c.b+=b.length,c.h(b))}});Wx(b,3)}).catch(function(c){return Jy(a).then(function(){Wx(b);throw c;})})}).then(function(){a.j.info("Remoting started successfully.")}).catch(function(b){a.j.error("Failed to start remoting",
b);a.a.onError()})};Hy.prototype.start=Hy.prototype.start;
Hy.prototype.wm=function(a,b){var c=this;return Sx(this.g,3,function(d){return c.w.start(a?new vy:null,b?new vy:null,c).then(function(a){Ny(c.b,function(a){return c.v.sendMessage(a)},function(a){c.a.onMessageFromSink(a)});Wx(d,4);return{audio_stream_id:a.zk||-1,video_stream_id:a.Fm||-1}}).catch(function(a){return Jy(c).then(function(){Wx(d);throw a;})})}).catch(function(a){c.j.error("Failed to start remoting streams",a);Iy(c);return{audio_stream_id:-1,video_stream_id:-1}})};
Hy.prototype.startDataStreams=Hy.prototype.wm;Hy.prototype.stop=function(a){var b=this;Tx(this.g,function(c){b.a.onStopped(a);return Jy(b).then(function(){b.j.info("Remoting stopped.");Wx(c,5);(0,b.l)().then(function(){return Sx(b.g,5,function(a){if(b.a)b.a.onSinkAvailable(b.A);Wx(a,2);return Promise.resolve()})}).catch(function(a){throw a;});b.l=null})}).catch(function(a){b.j.error("Failed to stop remoting: ",a);Iy(b)})};Hy.prototype.stop=Hy.prototype.stop;
Hy.prototype.Jk=function(){null===this.m&&(this.m=de(this.h.sb.Yd).then(function(a){return a.b||!1}));return this.m.then(function(a){return{rate:(a?1E7:2E6)/8}})};Hy.prototype.estimateTransmissionCapacity=Hy.prototype.Jk;
var Jy=function(a){return a.w.stop().then(function(){qy(a.v,"RPC");Oy(a.b);Py(a.b)})},Fy=function(a,b){var c=this,d=new mojo.RemotingSinkMetadata;d.features=[];d.friendly_name=b.vm||"";d.audio_capabilities=[];d.video_capabilities=[];var e=mojo.RemotingSinkAudioCapability,f=mojo.RemotingSinkVideoCapability,g=d.audio_capabilities,l=d.video_capabilities,r=b.Zd||"";(a.a||[]).forEach(function(a){switch(a){case "audio":g.push(e.CODEC_BASELINE_SET);break;case "aac":g.push(e.CODEC_AAC);break;case "opus":g.push(e.CODEC_OPUS);
break;case "video":l.push(f.CODEC_BASELINE_SET);break;case "4k":l.push(f.SUPPORT_4K);break;case "h264":l.push(f.CODEC_H264);break;case "vp8":l.push(f.CODEC_VP8);break;case "vp9":r.startsWith("Chromecast Ultra")&&l.push(f.CODEC_VP9);break;case "hevc":r.startsWith("Chromecast Ultra")&&l.push(f.CODEC_HEVC);break;default:c.j.info("Unknown mediaCap name: "+a)}});return d},Gy=function(){this.h=this.l=this.a=null;this.v=this.b=this.m=this.f=this.u=0;this.g=null},My=function(a){a.a=[];Qy(a,performance.now())},
Ny=function(a,b,c){a.l=b;a.h=c;a.a?(b=a.a,a.a=null,b.forEach(function(b){return Ky(a,b.data).then(b.om,b.mi)})):Qy(a,performance.now())},Oy=function(a){if(a.a){var b=Error("Stop before delivering pending message");a.a.forEach(function(a){return a.mi(b)});a.a=null}a.l=null;a.h=null},Ky=function(a,b){if(a.l){var c=btoa(String.fromCharCode.apply(null,b));++a.u;a.f+=b.length;return a.l({type:"RPC",rpc:c})}return a.a?new Promise(function(c,e){a.a.push({data:b,om:c,mi:e})}):Promise.reject(Error("RPC pipe not started"))},
Ly=function(a,b){Py(a);a.g=setInterval(function(){if(a.a)var 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.u+" messages ("+Math.round(a.f/d)+" bytes/sec) and ")+("received "+a.m+" messages ("+Math.round(a.b/d)+" ")+"bytes/sec).";Qy(a,c);c=d}b(c)},3E4)},Py=function(a){null!=a.g&&(clearInterval(a.g),a.g=null)},Qy=function(a,b){a.u=0;a.f=0;a.m=0;a.b=0;a.v=b};var Ry=function(a){return a&&a.getAudioTracks()&&0<a.getAudioTracks().length?a.getAudioTracks()[0]:null},Sy=function(a){return a&&a.getVideoTracks()&&0<a.getVideoTracks().length?a.getVideoTracks()[0]:null};var Ty=function(a,b,c,d,e){this.f=new uy(a,b,c,d,void 0===e?null:e);this.j=Pa("mr.mirror.cast.MediaStreamer");this.l=new Qx;this.g=this.b=this.a=this.h=null};Ty.prototype.start=function(a,b){var c=this;return Sx(this.l,1,function(d){c.h=a;c.a=Ry(a);c.a&&"ended"==c.a.readyState&&(c.a=null);c.b=Sy(a);c.b&&"ended"==c.b.readyState&&(c.b=null);if(!c.a&&!c.b)return Wx(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 Wx(d,2)})})};
Ty.prototype.stop=function(){var a=this;return Ux(this.l,function(b){return a.f.stop().then(function(){a.a=null;a.b=null;a.h=null;a.g=null;Wx(b,1)})})};var Uy=function(a){return Sx(a.l,2,function(b){a.j.info("Suspending media streaming...");return a.f.stop().then(function(){a.j.info("Suspended media streaming.");Wx(b,3)})})};
Ty.prototype.resume=function(){var a=this;return Sx(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."));a.j.info("Resuming media streaming...");return a.f.start(a.a,a.b,a.g).then(function(){a.j.info("Resumed media streaming.");Wx(b,2)})})};var Vy=function(a,b,c){this.l=a;this.g=b;this.f=c;this.j=Pa("mr.mirror.cast.WifiStatusMonitor");this.a=null;this.b=[]};Vy.prototype.start=function(){var a=this;null==this.a&&(w(this.j,"Starting Wifi Status Monitoring."),this.b=[],py(this.f,"STATUS_RESPONSE",function(b){return Wy(a,b)}),this.a=setInterval(function(){return Xy(a)},12E4),Xy(this))};Vy.prototype.stop=function(){null!=this.a&&(w(this.j,"Stopping Wifi Status Monitoring."),clearInterval(this.a),this.a=null,qy(this.f,"STATUS_RESPONSE"))};
var Wy=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?v(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),a.j.info(function(){return"Current Wifi status: "+JSON.stringify(c)}))}else v(a.j,function(){return"Ignoring response without status: "+JSON.stringify(b)})},Xy=function(a){a.f.sendMessage({type:"GET_STATUS",
sessionId:a.l,seqNum:ik(a.g),get_status:["wifiSnr","wifiSpeed"]})};var Yy=function(a,b,c,d){this.B=b.Yd;this.m={extVersion:chrome.runtime.getManifest().version,extChannel:"public",mirrorSettings:jg(a),sender:navigator.userAgent||"UNKNOWN",receiverProductName:b.Zd};this.A=c;this.w=d;this.g=this.b=this.u=this.h=this.l=this.v=this.f=null;this.a=[]};Yy.prototype.yd=function(a){null!=this.b&&clearInterval(this.b);this.f=a;this.v=Date.now();this.b=setInterval(this.s.bind(this,a),9E5)};
Yy.prototype.ce=function(){null!=this.b&&(clearInterval(this.b),this.b=null);if(null!=this.f){var a=this.s(this.f);this.f=null;return a}return Promise.resolve()};Yy.prototype.vc=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.u=b.stack))};
var $y=function(a,b){return(null==a.f?Promise.resolve():a.s(a.f)).then(function(){var c=b.map(function(b){b=Zy(a,b);var c=b.map(function(a){return a.events}).filter(function(a){return null!=a}),d=["["];b.map(function(a){return a.nd}).forEach(function(a,b){0<b&&d.push(",");d.push(a)});d.push("]");return{events:new Blob(c,{type:"application/gzip"}),nd:new Blob(d,{type:"application/json"})}});a.a=[];return c})};
Yy.prototype.s=function(a){var b=this;if(null!=this.g)return this.g;var c=de(this.B).then(function(c){c={receiverVersion:c.a,receiverConnected:c.g,receiverOnEthernet:c.b,receiverHasUpdatePending:c.f,receiverUptimeSeconds:c.l};Object.assign(c,b.m);var d=Date.now();Object.assign(c,{startTime:b.v,endTime:d,activity:wy(a),receiverWifiStatus:Array.from(b.w.b)});b.v=d;null!=b.l&&(Object.assign(c,{streamingErrorTime:b.l,streamingErrorMessage:b.h,streamingErrorCause:b.u}),b.l=null,b.h=null,b.u=null);return c});
return(this.g=Promise.all([c.then(function(b){return Cy(a,b)}),c,Dy(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,nd:new Blob([JSON.stringify(Object.assign({tags:d},c))],{type:"application/json"})});b.a=Zy(b,b.A);b.g=null}))||Promise.resolve()};
var Zy=function(a,b){b-=2;for(var c=[],d=a.a.length-1;0<=d;--d){b-=a.a[d].nd.size+1;if(0>b)break;c.push({events:null,nd:a.a[d].nd});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 az=function(a,b,c,d){d=void 0===d?null:d;gg.call(this,b);var e=b.sb;this.v=e.sessionId;this.I=e.Yd;this.A=a;this.X=d;this.j=Pa("mr.mirror.cast.Session");this.u=new Qx;this.m=new hk("mirror.cast.SeqNumGenerator");this.h=new oy(b.id);this.l=new Ty(e,this.A,this.m,this.h,this.X);this.g=null;this.a=new Yy(a,e,c,new Vy(this.v,this.m,this.h));this.f=!1;this.s=null};qa(az,gg);h=az.prototype;
h.start=function(a){var b=this;return Sx(this.u,1,function(c){var d=new Ya("MediaRouter.CastStreaming.Session.Launch");return bz(b).then(function(c){b.f=c;return b.l.start(a,b)}).then(function(){if(b.l.f.w){var a=b.a;a.m.tdlsIsOn=b.f;a.w.start();cz(b)}else b.a.m.tdlsIsOn=b.f;d.a();b.s=new db("MediaRouter.CastStreaming.Session.Length");Wx(c,2);return b})})};
h.stop=function(){var a=this;return Ux(this.u,function(b){a.s&&(a.s.a(),a.s=null);a.a.w.stop();return a.l.stop().then(function(){return a.g?Iy(a.g):Promise.resolve()}).then(function(){a.g=null;return a.f?dz(a):Promise.resolve()}).then(function(){a.f=!1;Wx(b,4)})})};
h.li=function(){var a={sessionId:this.v,seqNum:ik(this.m),type:"PRESENTATION",icons:[]};this.B&&(a.title=this.B);this.iconUrl&&!this.w&&a.icons.push({url:this.iconUrl});this.j.info("Sending session metadata update to receiver: "+this.v);this.h.sendMessage(a)};h.yd=function(a){this.a.yd(a)};h.ce=function(){return this.a.ce()};h.vc=function(a,b){this.a.vc(a,b);this.j.error(a,b);this.stop()};
var ez=function(a,b){return $y(a.a,b)},bz=function(a){return a.A.useTdls?Px(a.I,!0).then(function(b){if("Connected"==b)return a.j.info("Successfully enabled TDLS."),!0;v(a.j,"Did not enable TDLS: result="+b);return!1}).catch(function(b){v(a.j,"Error while calling enableTDLS()",b);return!1}):Promise.resolve(!1)},dz=function(a){return Px(a.I,!1).catch(function(b){return a.j.error("Error while turning TDLS back off",b)})},cz=function(a){fz(a).then(function(b){(b.a||[]).includes("video")?gz(a,b):v(a.j,
function(){return"Receiver incapable of Media Remoting: "+JSON.stringify(b)})}).catch(function(b){v(a.j,"None/Invalid capabilites response. Media Remoting disabled.",b)})},fz=function(a){return new Promise(function(b,c){var d={type:"GET_CAPABILITIES",sessionId:a.v,seqNum:ik(a.m)};a.j.info(function(){return"Sending GET_CAPABILITIES message: "+JSON.stringify(d)});ry(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 a.j.info(function(){return"Ignoring CAPABILITIES_RESPONSE with different seqNum: "+JSON.stringify(e)}),!1;w(a.j,function(){return"Received CAPABILITIES_RESPONSE: "+JSON.stringify(e)});b(e.capabilities);return!0})})},gz=function(a,b){Sx(a.u,2,function(c){var d=a.b.sb.Zd||"<UNKNOWN>";if(!d.startsWith("Chromecast")&&!d.startsWith("Eureka Dongle"))return v(a.j,"HACK: Media Remoting disabled because the receiver model--"+('"'+d+'" according to discovery--is not a Chromecast.')),
Wx(c),Promise.resolve();a.g=new Hy(a.b,b,a.A,a.m,a.h,a.X);return a.g.Ga(a.F.bind(a),a).catch(function(b){a.j.error("Media Remoting start failed: "+b.message,b)}).then(function(){return Wx(c)})})};az.prototype.F=function(){var a=this;return Sx(this.u,2,function(b){return new Promise(function(c,d){Uy(a.l).then(function(){Wx(b,3);c(a.H.bind(a))}).catch(function(b){a.vc("Failed to suspend MediaStreamer before starting remoting",b);d(b)})})})};
az.prototype.H=function(){var a=this;return Sx(this.u,3,function(b){return new Promise(function(c,d){a.l.resume().then(function(){Wx(b,2);c()}).catch(function(b){a.vc("Failed resume MediaStreamer after ending remoting mode",b);d(b)})})})};var hz=function(){Xf.call(this,"cast_streaming")};qa(hz,Xf);h=hz.prototype;h.getName=function(){return"cast_streaming"};h.te=function(a,b){return new az(a,b,Math.max(20969472,9351424),null)};h.Ue=function(){Gx(0)};h.Re=function(){Gx(1)};h.Wf=function(){Gx(2)};h.Se=function(){hb("MediaRouter.CastStreaming.Session.End")};h.Te=function(a){ib("MediaRouter.CastStreaming.Start.Failure",a,uf)};h.Td=function(){hb("MediaRouter.CastStreaming.Stream.End")};
h.kf=function(a){var b=this;return(new Promise(function(a){return chrome.metricsPrivate.getIsCrashReportingEnabled(a)})).then(function(c){var d=c&&Nx(),e=[9351424];d&&e.push(20969472);return ez(a,e).then(function(a){var e=a[a.length-1];a=Sg(a[0].events).catch(function(a){b.G.error("Failed to persist events Blob.",a)});d&&0<e.events.size?Jx(e.events,void 0,b.gl.bind(b)):c&&Ix("stats.json",e.nd,void 0,void 0);return a})})};h.gl=function(a){a&&(Mx().a=Date.now())};
h.Ff=function(a){this.G.info("Received message to upload logs for "+a);return this.a?ez(this.a,[20969472]).then(function(b){b=ja(b).next().value;return 0==b.events.size?"":Jx(b.events,a)}):Promise.resolve(iz(this,a))};
var iz=function(a,b){var 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 Sg(new Blob),a.G.info("Uploading saved logs for feedback."),Jx(c,b)},jz=new hz;Wf("mr.mirror.cast.Service",jz);
