You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

adapter.screenshare.js 155KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282
  1. /*! adapterjs - v0.14.0 - 2016-10-03 */
  2. var console = require("jitsi-meet-logger").getLogger(__filename);
  3. // Adapter's interface.
  4. var AdapterJS = AdapterJS || {};
  5. // Browserify compatibility
  6. if(typeof exports !== 'undefined') {
  7. module.exports = AdapterJS;
  8. }
  9. AdapterJS.options = AdapterJS.options || {};
  10. // uncomment to get virtual webcams
  11. // AdapterJS.options.getAllCams = true;
  12. // uncomment to prevent the install prompt when the plugin in not yet installed
  13. // AdapterJS.options.hidePluginInstallPrompt = true;
  14. // AdapterJS version
  15. AdapterJS.VERSION = '0.14.0';
  16. // This function will be called when the WebRTC API is ready to be used
  17. // Whether it is the native implementation (Chrome, Firefox, Opera) or
  18. // the plugin
  19. // You may Override this function to synchronise the start of your application
  20. // with the WebRTC API being ready.
  21. // If you decide not to override use this synchronisation, it may result in
  22. // an extensive CPU usage on the plugin start (once per tab loaded)
  23. // Params:
  24. // - isUsingPlugin: true is the WebRTC plugin is being used, false otherwise
  25. //
  26. AdapterJS.onwebrtcready = AdapterJS.onwebrtcready || function(isUsingPlugin) {
  27. // The WebRTC API is ready.
  28. // Override me and do whatever you want here
  29. };
  30. // New interface to store multiple callbacks, private
  31. AdapterJS._onwebrtcreadies = [];
  32. // Sets a callback function to be called when the WebRTC interface is ready.
  33. // The first argument is the function to callback.\
  34. // Throws an error if the first argument is not a function
  35. AdapterJS.webRTCReady = function (callback) {
  36. if (typeof callback !== 'function') {
  37. throw new Error('Callback provided is not a function');
  38. }
  39. if (true === AdapterJS.onwebrtcreadyDone) {
  40. // All WebRTC interfaces are ready, just call the callback
  41. callback(null !== AdapterJS.WebRTCPlugin.plugin);
  42. } else {
  43. // will be triggered automatically when your browser/plugin is ready.
  44. AdapterJS._onwebrtcreadies.push(callback);
  45. }
  46. };
  47. // Plugin namespace
  48. AdapterJS.WebRTCPlugin = AdapterJS.WebRTCPlugin || {};
  49. // The object to store plugin information
  50. /* jshint ignore:start */
  51. AdapterJS.WebRTCPlugin.pluginInfo = AdapterJS.WebRTCPlugin.pluginInfo || {
  52. prefix : 'Tem',
  53. plugName : 'TemWebRTCPlugin',
  54. pluginId : 'plugin0',
  55. type : 'application/x-temwebrtcplugin',
  56. onload : '__TemWebRTCReady0',
  57. portalLink : 'http://skylink.io/plugin/',
  58. downloadLink : null, //set below
  59. companyName: 'Temasys',
  60. downloadLinks : {
  61. mac: 'http://bit.ly/webrtcpluginpkg',
  62. win: 'http://bit.ly/webrtcpluginmsi'
  63. }
  64. };
  65. if(typeof AdapterJS.WebRTCPlugin.pluginInfo.downloadLinks !== "undefined" && AdapterJS.WebRTCPlugin.pluginInfo.downloadLinks !== null) {
  66. if(!!navigator.platform.match(/^Mac/i)) {
  67. AdapterJS.WebRTCPlugin.pluginInfo.downloadLink = AdapterJS.WebRTCPlugin.pluginInfo.downloadLinks.mac;
  68. }
  69. else if(!!navigator.platform.match(/^Win/i)) {
  70. AdapterJS.WebRTCPlugin.pluginInfo.downloadLink = AdapterJS.WebRTCPlugin.pluginInfo.downloadLinks.win;
  71. }
  72. }
  73. /* jshint ignore:end */
  74. AdapterJS.WebRTCPlugin.TAGS = {
  75. NONE : 'none',
  76. AUDIO : 'audio',
  77. VIDEO : 'video'
  78. };
  79. // Unique identifier of each opened page
  80. AdapterJS.WebRTCPlugin.pageId = Math.random().toString(36).slice(2);
  81. // Use this whenever you want to call the plugin.
  82. AdapterJS.WebRTCPlugin.plugin = null;
  83. // Set log level for the plugin once it is ready.
  84. // The different values are
  85. // This is an asynchronous function that will run when the plugin is ready
  86. AdapterJS.WebRTCPlugin.setLogLevel = null;
  87. // Defines webrtc's JS interface according to the plugin's implementation.
  88. // Define plugin Browsers as WebRTC Interface.
  89. AdapterJS.WebRTCPlugin.defineWebRTCInterface = null;
  90. // This function detects whether or not a plugin is installed.
  91. // Checks if Not IE (firefox, for example), else if it's IE,
  92. // we're running IE and do something. If not it is not supported.
  93. AdapterJS.WebRTCPlugin.isPluginInstalled = null;
  94. // Lets adapter.js wait until the the document is ready before injecting the plugin
  95. AdapterJS.WebRTCPlugin.pluginInjectionInterval = null;
  96. // Inject the HTML DOM object element into the page.
  97. AdapterJS.WebRTCPlugin.injectPlugin = null;
  98. // States of readiness that the plugin goes through when
  99. // being injected and stated
  100. AdapterJS.WebRTCPlugin.PLUGIN_STATES = {
  101. NONE : 0, // no plugin use
  102. INITIALIZING : 1, // Detected need for plugin
  103. INJECTING : 2, // Injecting plugin
  104. INJECTED: 3, // Plugin element injected but not usable yet
  105. READY: 4 // Plugin ready to be used
  106. };
  107. // Current state of the plugin. You cannot use the plugin before this is
  108. // equal to AdapterJS.WebRTCPlugin.PLUGIN_STATES.READY
  109. AdapterJS.WebRTCPlugin.pluginState = AdapterJS.WebRTCPlugin.PLUGIN_STATES.NONE;
  110. // True is AdapterJS.onwebrtcready was already called, false otherwise
  111. // Used to make sure AdapterJS.onwebrtcready is only called once
  112. AdapterJS.onwebrtcreadyDone = false;
  113. // Log levels for the plugin.
  114. // To be set by calling AdapterJS.WebRTCPlugin.setLogLevel
  115. /*
  116. Log outputs are prefixed in some cases.
  117. INFO: Information reported by the plugin.
  118. ERROR: Errors originating from within the plugin.
  119. WEBRTC: Error originating from within the libWebRTC library
  120. */
  121. // From the least verbose to the most verbose
  122. AdapterJS.WebRTCPlugin.PLUGIN_LOG_LEVELS = {
  123. NONE : 'NONE',
  124. ERROR : 'ERROR',
  125. WARNING : 'WARNING',
  126. INFO: 'INFO',
  127. VERBOSE: 'VERBOSE',
  128. SENSITIVE: 'SENSITIVE'
  129. };
  130. // Does a waiting check before proceeding to load the plugin.
  131. AdapterJS.WebRTCPlugin.WaitForPluginReady = null;
  132. // This methid will use an interval to wait for the plugin to be ready.
  133. AdapterJS.WebRTCPlugin.callWhenPluginReady = null;
  134. // !!!! WARNING: DO NOT OVERRIDE THIS FUNCTION. !!!
  135. // This function will be called when plugin is ready. It sends necessary
  136. // details to the plugin.
  137. // The function will wait for the document to be ready and the set the
  138. // plugin state to AdapterJS.WebRTCPlugin.PLUGIN_STATES.READY,
  139. // indicating that it can start being requested.
  140. // This function is not in the IE/Safari condition brackets so that
  141. // TemPluginLoaded function might be called on Chrome/Firefox.
  142. // This function is the only private function that is not encapsulated to
  143. // allow the plugin method to be called.
  144. __TemWebRTCReady0 = function () {
  145. if (document.readyState === 'complete') {
  146. AdapterJS.WebRTCPlugin.pluginState = AdapterJS.WebRTCPlugin.PLUGIN_STATES.READY;
  147. AdapterJS.maybeThroughWebRTCReady();
  148. } else {
  149. var timer = setInterval(function () {
  150. if (document.readyState === 'complete') {
  151. // TODO: update comments, we wait for the document to be ready
  152. clearInterval(timer);
  153. AdapterJS.WebRTCPlugin.pluginState = AdapterJS.WebRTCPlugin.PLUGIN_STATES.READY;
  154. AdapterJS.maybeThroughWebRTCReady();
  155. }
  156. }, 100);
  157. }
  158. };
  159. AdapterJS.maybeThroughWebRTCReady = function() {
  160. if (!AdapterJS.onwebrtcreadyDone) {
  161. AdapterJS.onwebrtcreadyDone = true;
  162. // If new interface for multiple callbacks used
  163. if (AdapterJS._onwebrtcreadies.length) {
  164. AdapterJS._onwebrtcreadies.forEach(function (callback) {
  165. if (typeof(callback) === 'function') {
  166. callback(AdapterJS.WebRTCPlugin.plugin !== null);
  167. }
  168. });
  169. // Else if no callbacks on new interface assuming user used old(deprecated) way to set callback through AdapterJS.onwebrtcready = ...
  170. } else if (typeof(AdapterJS.onwebrtcready) === 'function') {
  171. AdapterJS.onwebrtcready(AdapterJS.WebRTCPlugin.plugin !== null);
  172. }
  173. }
  174. };
  175. // Text namespace
  176. AdapterJS.TEXT = {
  177. PLUGIN: {
  178. REQUIRE_INSTALLATION: 'This website requires you to install a WebRTC-enabling plugin ' +
  179. 'to work on this browser.',
  180. NOT_SUPPORTED: 'Your browser does not support WebRTC.',
  181. BUTTON: 'Install Now'
  182. },
  183. REFRESH: {
  184. REQUIRE_REFRESH: 'Please refresh page',
  185. BUTTON: 'Refresh Page'
  186. }
  187. };
  188. // The result of ice connection states.
  189. // - starting: Ice connection is starting.
  190. // - checking: Ice connection is checking.
  191. // - connected Ice connection is connected.
  192. // - completed Ice connection is connected.
  193. // - done Ice connection has been completed.
  194. // - disconnected Ice connection has been disconnected.
  195. // - failed Ice connection has failed.
  196. // - closed Ice connection is closed.
  197. AdapterJS._iceConnectionStates = {
  198. starting : 'starting',
  199. checking : 'checking',
  200. connected : 'connected',
  201. completed : 'connected',
  202. done : 'completed',
  203. disconnected : 'disconnected',
  204. failed : 'failed',
  205. closed : 'closed'
  206. };
  207. //The IceConnection states that has been fired for each peer.
  208. AdapterJS._iceConnectionFiredStates = [];
  209. // Check if WebRTC Interface is defined.
  210. AdapterJS.isDefined = null;
  211. // This function helps to retrieve the webrtc detected browser information.
  212. // This sets:
  213. // - webrtcDetectedBrowser: The browser agent name.
  214. // - webrtcDetectedVersion: The browser version.
  215. // - webrtcMinimumVersion: The minimum browser version still supported by AJS.
  216. // - webrtcDetectedType: The types of webRTC support.
  217. // - 'moz': Mozilla implementation of webRTC.
  218. // - 'webkit': WebKit implementation of webRTC.
  219. // - 'plugin': Using the plugin implementation.
  220. AdapterJS.parseWebrtcDetectedBrowser = function () {
  221. var hasMatch = null;
  222. // Detect Opera (8.0+)
  223. if ((!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0) {
  224. hasMatch = navigator.userAgent.match(/OPR\/(\d+)/i) || [];
  225. webrtcDetectedBrowser = 'opera';
  226. webrtcDetectedVersion = parseInt(hasMatch[1] || '0', 10);
  227. webrtcMinimumVersion = 26;
  228. webrtcDetectedType = 'webkit';
  229. webrtcDetectedDCSupport = 'SCTP'; // Opera 20+ uses Chrome 33
  230. // Detect Bowser on iOS
  231. } else if (navigator.userAgent.match(/Bowser\/[0-9.]*/g)) {
  232. hasMatch = navigator.userAgent.match(/Bowser\/[0-9.]*/g) || [];
  233. var chromiumVersion = parseInt((navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./i) || [])[2] || '0', 10);
  234. webrtcDetectedBrowser = 'bowser';
  235. webrtcDetectedVersion = parseFloat((hasMatch[0] || '0/0').split('/')[1], 10);
  236. webrtcMinimumVersion = 0;
  237. webrtcDetectedType = 'webkit';
  238. webrtcDetectedDCSupport = chromiumVersion > 30 ? 'SCTP' : 'RTP';
  239. // Detect Opera on iOS (does not support WebRTC yet)
  240. } else if (navigator.userAgent.indexOf('OPiOS') > 0) {
  241. hasMatch = navigator.userAgent.match(/OPiOS\/([0-9]+)\./);
  242. // Browser which do not support webrtc yet
  243. webrtcDetectedBrowser = 'opera';
  244. webrtcDetectedVersion = parseInt(hasMatch[1] || '0', 10);
  245. webrtcMinimumVersion = 0;
  246. webrtcDetectedType = null;
  247. webrtcDetectedDCSupport = null;
  248. // Detect Chrome on iOS (does not support WebRTC yet)
  249. } else if (navigator.userAgent.indexOf('CriOS') > 0) {
  250. hasMatch = navigator.userAgent.match(/CriOS\/([0-9]+)\./) || [];
  251. webrtcDetectedBrowser = 'chrome';
  252. webrtcDetectedVersion = parseInt(hasMatch[1] || '0', 10);
  253. webrtcMinimumVersion = 0;
  254. webrtcDetectedType = null;
  255. webrtcDetectedDCSupport = null;
  256. // Detect Firefox on iOS (does not support WebRTC yet)
  257. } else if (navigator.userAgent.indexOf('FxiOS') > 0) {
  258. hasMatch = navigator.userAgent.match(/FxiOS\/([0-9]+)\./) || [];
  259. // Browser which do not support webrtc yet
  260. webrtcDetectedBrowser = 'firefox';
  261. webrtcDetectedVersion = parseInt(hasMatch[1] || '0', 10);
  262. webrtcMinimumVersion = 0;
  263. webrtcDetectedType = null;
  264. webrtcDetectedDCSupport = null;
  265. // Detect IE (6-11)
  266. } else if (/*@cc_on!@*/false || !!document.documentMode) {
  267. hasMatch = /\brv[ :]+(\d+)/g.exec(navigator.userAgent) || [];
  268. webrtcDetectedBrowser = 'IE';
  269. webrtcDetectedVersion = parseInt(hasMatch[1], 10);
  270. webrtcMinimumVersion = 9;
  271. webrtcDetectedType = 'plugin';
  272. webrtcDetectedDCSupport = 'SCTP';
  273. if (!webrtcDetectedVersion) {
  274. hasMatch = /\bMSIE[ :]+(\d+)/g.exec(navigator.userAgent) || [];
  275. webrtcDetectedVersion = parseInt(hasMatch[1] || '0', 10);
  276. }
  277. // Detect Edge (20+)
  278. } else if (!!window.StyleMedia || navigator.userAgent.match(/Edge\/(\d+).(\d+)$/)) {
  279. hasMatch = navigator.userAgent.match(/Edge\/(\d+).(\d+)$/) || [];
  280. // Previous webrtc/adapter uses minimum version as 10547 but checking in the Edge release history,
  281. // It's close to 13.10547 and ObjectRTC API is fully supported in that version
  282. webrtcDetectedBrowser = 'edge';
  283. webrtcDetectedVersion = parseFloat((hasMatch[0] || '0/0').split('/')[1], 10);
  284. webrtcMinimumVersion = 13.10547;
  285. webrtcDetectedType = 'ms';
  286. webrtcDetectedDCSupport = null;
  287. // Detect Firefox (1.0+)
  288. // Placed before Safari check to ensure Firefox on Android is detected
  289. } else if (typeof InstallTrigger !== 'undefined' || navigator.userAgent.indexOf('irefox') > 0) {
  290. hasMatch = navigator.userAgent.match(/Firefox\/([0-9]+)\./) || [];
  291. webrtcDetectedBrowser = 'firefox';
  292. webrtcDetectedVersion = parseInt(hasMatch[1] || '0', 10);
  293. webrtcMinimumVersion = 31;
  294. webrtcDetectedType = 'moz';
  295. webrtcDetectedDCSupport = 'SCTP';
  296. // Detect Chrome (1+ and mobile)
  297. // Placed before Safari check to ensure Chrome on Android is detected
  298. } else if ((!!window.chrome && !!window.chrome.webstore) || navigator.userAgent.indexOf('Chrom') > 0) {
  299. hasMatch = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./i) || [];
  300. webrtcDetectedBrowser = 'chrome';
  301. webrtcDetectedVersion = parseInt(hasMatch[2] || '0', 10);
  302. webrtcMinimumVersion = 38;
  303. webrtcDetectedType = 'webkit';
  304. webrtcDetectedDCSupport = webrtcDetectedVersion > 30 ? 'SCTP' : 'RTP'; // Chrome 31+ supports SCTP without flags
  305. // Detect Safari
  306. } else if (/^((?!chrome|android).)*safari/i.test(navigator.userAgent)) {
  307. hasMatch = navigator.userAgent.match(/version\/(\d+)/i) || [];
  308. var isMobile = navigator.userAgent.match(/(iPhone|iPad)/gi) || [];
  309. webrtcDetectedBrowser = 'safari';
  310. webrtcDetectedVersion = parseInt(hasMatch[1] || '0', 10);
  311. webrtcMinimumVersion = 7;
  312. webrtcDetectedType = isMobile.length === 0 ? 'plugin' : null;
  313. webrtcDetectedDCSupport = isMobile.length === 0 ? 'SCTP' : null;
  314. }
  315. window.webrtcDetectedBrowser = webrtcDetectedBrowser;
  316. window.webrtcDetectedVersion = webrtcDetectedVersion;
  317. window.webrtcMinimumVersion = webrtcMinimumVersion;
  318. window.webrtcDetectedType = webrtcDetectedType; // Scope it to window for better consistency
  319. window.webrtcDetectedDCSupport = webrtcDetectedDCSupport; // Scope it to window for better consistency
  320. };
  321. AdapterJS.addEvent = function(elem, evnt, func) {
  322. if (elem.addEventListener) { // W3C DOM
  323. elem.addEventListener(evnt, func, false);
  324. } else if (elem.attachEvent) {// OLD IE DOM
  325. elem.attachEvent('on'+evnt, func);
  326. } else { // No much to do
  327. elem[evnt] = func;
  328. }
  329. };
  330. AdapterJS.renderNotificationBar = function (text, buttonText, buttonLink, openNewTab, displayRefreshBar) {
  331. // only inject once the page is ready
  332. if (document.readyState !== 'complete') {
  333. return;
  334. }
  335. var w = window;
  336. var i = document.createElement('iframe');
  337. i.name = 'adapterjs-alert';
  338. i.style.position = 'fixed';
  339. i.style.top = '-41px';
  340. i.style.left = 0;
  341. i.style.right = 0;
  342. i.style.width = '100%';
  343. i.style.height = '40px';
  344. i.style.backgroundColor = '#ffffe1';
  345. i.style.border = 'none';
  346. i.style.borderBottom = '1px solid #888888';
  347. i.style.zIndex = '9999999';
  348. if(typeof i.style.webkitTransition === 'string') {
  349. i.style.webkitTransition = 'all .5s ease-out';
  350. } else if(typeof i.style.transition === 'string') {
  351. i.style.transition = 'all .5s ease-out';
  352. }
  353. document.body.appendChild(i);
  354. var c = (i.contentWindow) ? i.contentWindow :
  355. (i.contentDocument.document) ? i.contentDocument.document : i.contentDocument;
  356. c.document.open();
  357. c.document.write('<span style="display: inline-block; font-family: Helvetica, Arial,' +
  358. 'sans-serif; font-size: .9rem; padding: 4px; vertical-align: ' +
  359. 'middle; cursor: default;">' + text + '</span>');
  360. if(buttonText && buttonLink) {
  361. c.document.write('<button id="okay">' + buttonText + '</button><button id="cancel">Cancel</button>');
  362. c.document.close();
  363. // On click on okay
  364. AdapterJS.addEvent(c.document.getElementById('okay'), 'click', function(e) {
  365. if (!!displayRefreshBar) {
  366. AdapterJS.renderNotificationBar(AdapterJS.TEXT.EXTENSION ?
  367. AdapterJS.TEXT.EXTENSION.REQUIRE_REFRESH : AdapterJS.TEXT.REFRESH.REQUIRE_REFRESH,
  368. AdapterJS.TEXT.REFRESH.BUTTON, 'javascript:location.reload()'); // jshint ignore:line
  369. }
  370. window.open(buttonLink, !!openNewTab ? '_blank' : '_top');
  371. e.preventDefault();
  372. try {
  373. e.cancelBubble = true;
  374. } catch(error) { }
  375. var pluginInstallInterval = setInterval(function(){
  376. if(! isIE) {
  377. navigator.plugins.refresh(false);
  378. }
  379. AdapterJS.WebRTCPlugin.isPluginInstalled(
  380. AdapterJS.WebRTCPlugin.pluginInfo.prefix,
  381. AdapterJS.WebRTCPlugin.pluginInfo.plugName,
  382. AdapterJS.WebRTCPlugin.pluginInfo.type,
  383. function() { // plugin now installed
  384. clearInterval(pluginInstallInterval);
  385. AdapterJS.WebRTCPlugin.defineWebRTCInterface();
  386. },
  387. function() {
  388. // still no plugin detected, nothing to do
  389. });
  390. } , 500);
  391. });
  392. // On click on Cancel
  393. AdapterJS.addEvent(c.document.getElementById('cancel'), 'click', function(e) {
  394. w.document.body.removeChild(i);
  395. });
  396. } else {
  397. c.document.close();
  398. }
  399. setTimeout(function() {
  400. if(typeof i.style.webkitTransform === 'string') {
  401. i.style.webkitTransform = 'translateY(40px)';
  402. } else if(typeof i.style.transform === 'string') {
  403. i.style.transform = 'translateY(40px)';
  404. } else {
  405. i.style.top = '0px';
  406. }
  407. }, 300);
  408. };
  409. // -----------------------------------------------------------
  410. // Detected webrtc implementation. Types are:
  411. // - 'moz': Mozilla implementation of webRTC.
  412. // - 'webkit': WebKit implementation of webRTC.
  413. // - 'plugin': Using the plugin implementation.
  414. webrtcDetectedType = null;
  415. // Set the settings for creating DataChannels, MediaStream for
  416. // Cross-browser compability.
  417. // - This is only for SCTP based support browsers.
  418. // the 'urls' attribute.
  419. checkMediaDataChannelSettings =
  420. function (peerBrowserAgent, peerBrowserVersion, callback, constraints) {
  421. if (typeof callback !== 'function') {
  422. return;
  423. }
  424. var beOfferer = true;
  425. var isLocalFirefox = webrtcDetectedBrowser === 'firefox';
  426. // Nightly version does not require MozDontOfferDataChannel for interop
  427. var isLocalFirefoxInterop = webrtcDetectedType === 'moz' && webrtcDetectedVersion > 30;
  428. var isPeerFirefox = peerBrowserAgent === 'firefox';
  429. var isPeerFirefoxInterop = peerBrowserAgent === 'firefox' &&
  430. ((peerBrowserVersion) ? (peerBrowserVersion > 30) : false);
  431. // Resends an updated version of constraints for MozDataChannel to work
  432. // If other userAgent is firefox and user is firefox, remove MozDataChannel
  433. if ((isLocalFirefox && isPeerFirefox) || (isLocalFirefoxInterop)) {
  434. try {
  435. delete constraints.mandatory.MozDontOfferDataChannel;
  436. } catch (error) {
  437. console.error('Failed deleting MozDontOfferDataChannel');
  438. console.error(error);
  439. }
  440. } else if ((isLocalFirefox && !isPeerFirefox)) {
  441. constraints.mandatory.MozDontOfferDataChannel = true;
  442. }
  443. if (!isLocalFirefox) {
  444. // temporary measure to remove Moz* constraints in non Firefox browsers
  445. for (var prop in constraints.mandatory) {
  446. if (constraints.mandatory.hasOwnProperty(prop)) {
  447. if (prop.indexOf('Moz') !== -1) {
  448. delete constraints.mandatory[prop];
  449. }
  450. }
  451. }
  452. }
  453. // Firefox (not interopable) cannot offer DataChannel as it will cause problems to the
  454. // interopability of the media stream
  455. if (isLocalFirefox && !isPeerFirefox && !isLocalFirefoxInterop) {
  456. beOfferer = false;
  457. }
  458. callback(beOfferer, constraints);
  459. };
  460. // Handles the differences for all browsers ice connection state output.
  461. // - Tested outcomes are:
  462. // - Chrome (offerer) : 'checking' > 'completed' > 'completed'
  463. // - Chrome (answerer) : 'checking' > 'connected'
  464. // - Firefox (offerer) : 'checking' > 'connected'
  465. // - Firefox (answerer): 'checking' > 'connected'
  466. checkIceConnectionState = function (peerId, iceConnectionState, callback) {
  467. if (typeof callback !== 'function') {
  468. console.warn('No callback specified in checkIceConnectionState. Aborted.');
  469. return;
  470. }
  471. peerId = (peerId) ? peerId : 'peer';
  472. if (!AdapterJS._iceConnectionFiredStates[peerId] ||
  473. iceConnectionState === AdapterJS._iceConnectionStates.disconnected ||
  474. iceConnectionState === AdapterJS._iceConnectionStates.failed ||
  475. iceConnectionState === AdapterJS._iceConnectionStates.closed) {
  476. AdapterJS._iceConnectionFiredStates[peerId] = [];
  477. }
  478. iceConnectionState = AdapterJS._iceConnectionStates[iceConnectionState];
  479. if (AdapterJS._iceConnectionFiredStates[peerId].indexOf(iceConnectionState) < 0) {
  480. AdapterJS._iceConnectionFiredStates[peerId].push(iceConnectionState);
  481. if (iceConnectionState === AdapterJS._iceConnectionStates.connected) {
  482. setTimeout(function () {
  483. AdapterJS._iceConnectionFiredStates[peerId]
  484. .push(AdapterJS._iceConnectionStates.done);
  485. callback(AdapterJS._iceConnectionStates.done);
  486. }, 1000);
  487. }
  488. callback(iceConnectionState);
  489. }
  490. return;
  491. };
  492. // Firefox:
  493. // - Creates iceServer from the url for Firefox.
  494. // - Create iceServer with stun url.
  495. // - Create iceServer with turn url.
  496. // - Ignore the transport parameter from TURN url for FF version <=27.
  497. // - Return null for createIceServer if transport=tcp.
  498. // - FF 27 and above supports transport parameters in TURN url,
  499. // - So passing in the full url to create iceServer.
  500. // Chrome:
  501. // - Creates iceServer from the url for Chrome M33 and earlier.
  502. // - Create iceServer with stun url.
  503. // - Chrome M28 & above uses below TURN format.
  504. // Plugin:
  505. // - Creates Ice Server for Plugin Browsers
  506. // - If Stun - Create iceServer with stun url.
  507. // - Else - Create iceServer with turn url
  508. // - This is a WebRTC Function
  509. createIceServer = null;
  510. // Firefox:
  511. // - Creates IceServers for Firefox
  512. // - Use .url for FireFox.
  513. // - Multiple Urls support
  514. // Chrome:
  515. // - Creates iceServers from the urls for Chrome M34 and above.
  516. // - .urls is supported since Chrome M34.
  517. // - Multiple Urls support
  518. // Plugin:
  519. // - Creates Ice Servers for Plugin Browsers
  520. // - Multiple Urls support
  521. // - This is a WebRTC Function
  522. createIceServers = null;
  523. //------------------------------------------------------------
  524. //The RTCPeerConnection object.
  525. RTCPeerConnection = null;
  526. // Creates RTCSessionDescription object for Plugin Browsers
  527. RTCSessionDescription = (typeof RTCSessionDescription === 'function') ?
  528. RTCSessionDescription : null;
  529. // Creates RTCIceCandidate object for Plugin Browsers
  530. RTCIceCandidate = (typeof RTCIceCandidate === 'function') ?
  531. RTCIceCandidate : null;
  532. // Get UserMedia (only difference is the prefix).
  533. // Code from Adam Barth.
  534. getUserMedia = null;
  535. // Attach a media stream to an element.
  536. attachMediaStream = null;
  537. // Re-attach a media stream to an element.
  538. reattachMediaStream = null;
  539. // Detected browser agent name. Types are:
  540. // - 'firefox': Firefox browser.
  541. // - 'chrome': Chrome browser.
  542. // - 'opera': Opera browser.
  543. // - 'safari': Safari browser.
  544. // - 'IE' - Internet Explorer browser.
  545. webrtcDetectedBrowser = null;
  546. // Detected browser version.
  547. webrtcDetectedVersion = null;
  548. // The minimum browser version still supported by AJS.
  549. webrtcMinimumVersion = null;
  550. // Check for browser types and react accordingly
  551. if ( (navigator.mozGetUserMedia ||
  552. navigator.webkitGetUserMedia ||
  553. (navigator.mediaDevices &&
  554. navigator.userAgent.match(/Edge\/(\d+).(\d+)$/)))
  555. && !((navigator.userAgent.match(/android/ig) || []).length === 0 &&
  556.     (navigator.userAgent.match(/chrome/ig) || []).length === 0 && navigator.userAgent.indexOf('Safari/') > 0)) {
  557. ///////////////////////////////////////////////////////////////////
  558. // INJECTION OF GOOGLE'S ADAPTER.JS CONTENT
  559. /* jshint ignore:start */
  560. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.adapter = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  561. /* eslint-env node */
  562. 'use strict';
  563. // SDP helpers.
  564. var SDPUtils = {};
  565. // Generate an alphanumeric identifier for cname or mids.
  566. // TODO: use UUIDs instead? https://gist.github.com/jed/982883
  567. SDPUtils.generateIdentifier = function() {
  568. return Math.random().toString(36).substr(2, 10);
  569. };
  570. // The RTCP CNAME used by all peerconnections from the same JS.
  571. SDPUtils.localCName = SDPUtils.generateIdentifier();
  572. // Splits SDP into lines, dealing with both CRLF and LF.
  573. SDPUtils.splitLines = function(blob) {
  574. return blob.trim().split('\n').map(function(line) {
  575. return line.trim();
  576. });
  577. };
  578. // Splits SDP into sessionpart and mediasections. Ensures CRLF.
  579. SDPUtils.splitSections = function(blob) {
  580. var parts = blob.split('\nm=');
  581. return parts.map(function(part, index) {
  582. return (index > 0 ? 'm=' + part : part).trim() + '\r\n';
  583. });
  584. };
  585. // Returns lines that start with a certain prefix.
  586. SDPUtils.matchPrefix = function(blob, prefix) {
  587. return SDPUtils.splitLines(blob).filter(function(line) {
  588. return line.indexOf(prefix) === 0;
  589. });
  590. };
  591. // Parses an ICE candidate line. Sample input:
  592. // candidate:702786350 2 udp 41819902 8.8.8.8 60769 typ relay raddr 8.8.8.8
  593. // rport 55996"
  594. SDPUtils.parseCandidate = function(line) {
  595. var parts;
  596. // Parse both variants.
  597. if (line.indexOf('a=candidate:') === 0) {
  598. parts = line.substring(12).split(' ');
  599. } else {
  600. parts = line.substring(10).split(' ');
  601. }
  602. var candidate = {
  603. foundation: parts[0],
  604. component: parts[1],
  605. protocol: parts[2].toLowerCase(),
  606. priority: parseInt(parts[3], 10),
  607. ip: parts[4],
  608. port: parseInt(parts[5], 10),
  609. // skip parts[6] == 'typ'
  610. type: parts[7]
  611. };
  612. for (var i = 8; i < parts.length; i += 2) {
  613. switch (parts[i]) {
  614. case 'raddr':
  615. candidate.relatedAddress = parts[i + 1];
  616. break;
  617. case 'rport':
  618. candidate.relatedPort = parseInt(parts[i + 1], 10);
  619. break;
  620. case 'tcptype':
  621. candidate.tcpType = parts[i + 1];
  622. break;
  623. default: // Unknown extensions are silently ignored.
  624. break;
  625. }
  626. }
  627. return candidate;
  628. };
  629. // Translates a candidate object into SDP candidate attribute.
  630. SDPUtils.writeCandidate = function(candidate) {
  631. var sdp = [];
  632. sdp.push(candidate.foundation);
  633. sdp.push(candidate.component);
  634. sdp.push(candidate.protocol.toUpperCase());
  635. sdp.push(candidate.priority);
  636. sdp.push(candidate.ip);
  637. sdp.push(candidate.port);
  638. var type = candidate.type;
  639. sdp.push('typ');
  640. sdp.push(type);
  641. if (type !== 'host' && candidate.relatedAddress &&
  642. candidate.relatedPort) {
  643. sdp.push('raddr');
  644. sdp.push(candidate.relatedAddress); // was: relAddr
  645. sdp.push('rport');
  646. sdp.push(candidate.relatedPort); // was: relPort
  647. }
  648. if (candidate.tcpType && candidate.protocol.toLowerCase() === 'tcp') {
  649. sdp.push('tcptype');
  650. sdp.push(candidate.tcpType);
  651. }
  652. return 'candidate:' + sdp.join(' ');
  653. };
  654. // Parses an rtpmap line, returns RTCRtpCoddecParameters. Sample input:
  655. // a=rtpmap:111 opus/48000/2
  656. SDPUtils.parseRtpMap = function(line) {
  657. var parts = line.substr(9).split(' ');
  658. var parsed = {
  659. payloadType: parseInt(parts.shift(), 10) // was: id
  660. };
  661. parts = parts[0].split('/');
  662. parsed.name = parts[0];
  663. parsed.clockRate = parseInt(parts[1], 10); // was: clockrate
  664. // was: channels
  665. parsed.numChannels = parts.length === 3 ? parseInt(parts[2], 10) : 1;
  666. return parsed;
  667. };
  668. // Generate an a=rtpmap line from RTCRtpCodecCapability or
  669. // RTCRtpCodecParameters.
  670. SDPUtils.writeRtpMap = function(codec) {
  671. var pt = codec.payloadType;
  672. if (codec.preferredPayloadType !== undefined) {
  673. pt = codec.preferredPayloadType;
  674. }
  675. return 'a=rtpmap:' + pt + ' ' + codec.name + '/' + codec.clockRate +
  676. (codec.numChannels !== 1 ? '/' + codec.numChannels : '') + '\r\n';
  677. };
  678. // Parses an a=extmap line (headerextension from RFC 5285). Sample input:
  679. // a=extmap:2 urn:ietf:params:rtp-hdrext:toffset
  680. SDPUtils.parseExtmap = function(line) {
  681. var parts = line.substr(9).split(' ');
  682. return {
  683. id: parseInt(parts[0], 10),
  684. uri: parts[1]
  685. };
  686. };
  687. // Generates a=extmap line from RTCRtpHeaderExtensionParameters or
  688. // RTCRtpHeaderExtension.
  689. SDPUtils.writeExtmap = function(headerExtension) {
  690. return 'a=extmap:' + (headerExtension.id || headerExtension.preferredId) +
  691. ' ' + headerExtension.uri + '\r\n';
  692. };
  693. // Parses an ftmp line, returns dictionary. Sample input:
  694. // a=fmtp:96 vbr=on;cng=on
  695. // Also deals with vbr=on; cng=on
  696. SDPUtils.parseFmtp = function(line) {
  697. var parsed = {};
  698. var kv;
  699. var parts = line.substr(line.indexOf(' ') + 1).split(';');
  700. for (var j = 0; j < parts.length; j++) {
  701. kv = parts[j].trim().split('=');
  702. parsed[kv[0].trim()] = kv[1];
  703. }
  704. return parsed;
  705. };
  706. // Generates an a=ftmp line from RTCRtpCodecCapability or RTCRtpCodecParameters.
  707. SDPUtils.writeFmtp = function(codec) {
  708. var line = '';
  709. var pt = codec.payloadType;
  710. if (codec.preferredPayloadType !== undefined) {
  711. pt = codec.preferredPayloadType;
  712. }
  713. if (codec.parameters && Object.keys(codec.parameters).length) {
  714. var params = [];
  715. Object.keys(codec.parameters).forEach(function(param) {
  716. params.push(param + '=' + codec.parameters[param]);
  717. });
  718. line += 'a=fmtp:' + pt + ' ' + params.join(';') + '\r\n';
  719. }
  720. return line;
  721. };
  722. // Parses an rtcp-fb line, returns RTCPRtcpFeedback object. Sample input:
  723. // a=rtcp-fb:98 nack rpsi
  724. SDPUtils.parseRtcpFb = function(line) {
  725. var parts = line.substr(line.indexOf(' ') + 1).split(' ');
  726. return {
  727. type: parts.shift(),
  728. parameter: parts.join(' ')
  729. };
  730. };
  731. // Generate a=rtcp-fb lines from RTCRtpCodecCapability or RTCRtpCodecParameters.
  732. SDPUtils.writeRtcpFb = function(codec) {
  733. var lines = '';
  734. var pt = codec.payloadType;
  735. if (codec.preferredPayloadType !== undefined) {
  736. pt = codec.preferredPayloadType;
  737. }
  738. if (codec.rtcpFeedback && codec.rtcpFeedback.length) {
  739. // FIXME: special handling for trr-int?
  740. codec.rtcpFeedback.forEach(function(fb) {
  741. lines += 'a=rtcp-fb:' + pt + ' ' + fb.type +
  742. (fb.parameter && fb.parameter.length ? ' ' + fb.parameter : '') +
  743. '\r\n';
  744. });
  745. }
  746. return lines;
  747. };
  748. // Parses an RFC 5576 ssrc media attribute. Sample input:
  749. // a=ssrc:3735928559 cname:something
  750. SDPUtils.parseSsrcMedia = function(line) {
  751. var sp = line.indexOf(' ');
  752. var parts = {
  753. ssrc: parseInt(line.substr(7, sp - 7), 10)
  754. };
  755. var colon = line.indexOf(':', sp);
  756. if (colon > -1) {
  757. parts.attribute = line.substr(sp + 1, colon - sp - 1);
  758. parts.value = line.substr(colon + 1);
  759. } else {
  760. parts.attribute = line.substr(sp + 1);
  761. }
  762. return parts;
  763. };
  764. // Extracts DTLS parameters from SDP media section or sessionpart.
  765. // FIXME: for consistency with other functions this should only
  766. // get the fingerprint line as input. See also getIceParameters.
  767. SDPUtils.getDtlsParameters = function(mediaSection, sessionpart) {
  768. var lines = SDPUtils.splitLines(mediaSection);
  769. // Search in session part, too.
  770. lines = lines.concat(SDPUtils.splitLines(sessionpart));
  771. var fpLine = lines.filter(function(line) {
  772. return line.indexOf('a=fingerprint:') === 0;
  773. })[0].substr(14);
  774. // Note: a=setup line is ignored since we use the 'auto' role.
  775. var dtlsParameters = {
  776. role: 'auto',
  777. fingerprints: [{
  778. algorithm: fpLine.split(' ')[0],
  779. value: fpLine.split(' ')[1]
  780. }]
  781. };
  782. return dtlsParameters;
  783. };
  784. // Serializes DTLS parameters to SDP.
  785. SDPUtils.writeDtlsParameters = function(params, setupType) {
  786. var sdp = 'a=setup:' + setupType + '\r\n';
  787. params.fingerprints.forEach(function(fp) {
  788. sdp += 'a=fingerprint:' + fp.algorithm + ' ' + fp.value + '\r\n';
  789. });
  790. return sdp;
  791. };
  792. // Parses ICE information from SDP media section or sessionpart.
  793. // FIXME: for consistency with other functions this should only
  794. // get the ice-ufrag and ice-pwd lines as input.
  795. SDPUtils.getIceParameters = function(mediaSection, sessionpart) {
  796. var lines = SDPUtils.splitLines(mediaSection);
  797. // Search in session part, too.
  798. lines = lines.concat(SDPUtils.splitLines(sessionpart));
  799. var iceParameters = {
  800. usernameFragment: lines.filter(function(line) {
  801. return line.indexOf('a=ice-ufrag:') === 0;
  802. })[0].substr(12),
  803. password: lines.filter(function(line) {
  804. return line.indexOf('a=ice-pwd:') === 0;
  805. })[0].substr(10)
  806. };
  807. return iceParameters;
  808. };
  809. // Serializes ICE parameters to SDP.
  810. SDPUtils.writeIceParameters = function(params) {
  811. return 'a=ice-ufrag:' + params.usernameFragment + '\r\n' +
  812. 'a=ice-pwd:' + params.password + '\r\n';
  813. };
  814. // Parses the SDP media section and returns RTCRtpParameters.
  815. SDPUtils.parseRtpParameters = function(mediaSection) {
  816. var description = {
  817. codecs: [],
  818. headerExtensions: [],
  819. fecMechanisms: [],
  820. rtcp: []
  821. };
  822. var lines = SDPUtils.splitLines(mediaSection);
  823. var mline = lines[0].split(' ');
  824. for (var i = 3; i < mline.length; i++) { // find all codecs from mline[3..]
  825. var pt = mline[i];
  826. var rtpmapline = SDPUtils.matchPrefix(
  827. mediaSection, 'a=rtpmap:' + pt + ' ')[0];
  828. if (rtpmapline) {
  829. var codec = SDPUtils.parseRtpMap(rtpmapline);
  830. var fmtps = SDPUtils.matchPrefix(
  831. mediaSection, 'a=fmtp:' + pt + ' ');
  832. // Only the first a=fmtp:<pt> is considered.
  833. codec.parameters = fmtps.length ? SDPUtils.parseFmtp(fmtps[0]) : {};
  834. codec.rtcpFeedback = SDPUtils.matchPrefix(
  835. mediaSection, 'a=rtcp-fb:' + pt + ' ')
  836. .map(SDPUtils.parseRtcpFb);
  837. description.codecs.push(codec);
  838. // parse FEC mechanisms from rtpmap lines.
  839. switch (codec.name.toUpperCase()) {
  840. case 'RED':
  841. case 'ULPFEC':
  842. description.fecMechanisms.push(codec.name.toUpperCase());
  843. break;
  844. default: // only RED and ULPFEC are recognized as FEC mechanisms.
  845. break;
  846. }
  847. }
  848. }
  849. SDPUtils.matchPrefix(mediaSection, 'a=extmap:').forEach(function(line) {
  850. description.headerExtensions.push(SDPUtils.parseExtmap(line));
  851. });
  852. // FIXME: parse rtcp.
  853. return description;
  854. };
  855. // Generates parts of the SDP media section describing the capabilities /
  856. // parameters.
  857. SDPUtils.writeRtpDescription = function(kind, caps) {
  858. var sdp = '';
  859. // Build the mline.
  860. sdp += 'm=' + kind + ' ';
  861. sdp += caps.codecs.length > 0 ? '9' : '0'; // reject if no codecs.
  862. sdp += ' UDP/TLS/RTP/SAVPF ';
  863. sdp += caps.codecs.map(function(codec) {
  864. if (codec.preferredPayloadType !== undefined) {
  865. return codec.preferredPayloadType;
  866. }
  867. return codec.payloadType;
  868. }).join(' ') + '\r\n';
  869. sdp += 'c=IN IP4 0.0.0.0\r\n';
  870. sdp += 'a=rtcp:9 IN IP4 0.0.0.0\r\n';
  871. // Add a=rtpmap lines for each codec. Also fmtp and rtcp-fb.
  872. caps.codecs.forEach(function(codec) {
  873. sdp += SDPUtils.writeRtpMap(codec);
  874. sdp += SDPUtils.writeFmtp(codec);
  875. sdp += SDPUtils.writeRtcpFb(codec);
  876. });
  877. // FIXME: add headerExtensions, fecMechanismş and rtcp.
  878. sdp += 'a=rtcp-mux\r\n';
  879. return sdp;
  880. };
  881. // Parses the SDP media section and returns an array of
  882. // RTCRtpEncodingParameters.
  883. SDPUtils.parseRtpEncodingParameters = function(mediaSection) {
  884. var encodingParameters = [];
  885. var description = SDPUtils.parseRtpParameters(mediaSection);
  886. var hasRed = description.fecMechanisms.indexOf('RED') !== -1;
  887. var hasUlpfec = description.fecMechanisms.indexOf('ULPFEC') !== -1;
  888. // filter a=ssrc:... cname:, ignore PlanB-msid
  889. var ssrcs = SDPUtils.matchPrefix(mediaSection, 'a=ssrc:')
  890. .map(function(line) {
  891. return SDPUtils.parseSsrcMedia(line);
  892. })
  893. .filter(function(parts) {
  894. return parts.attribute === 'cname';
  895. });
  896. var primarySsrc = ssrcs.length > 0 && ssrcs[0].ssrc;
  897. var secondarySsrc;
  898. var flows = SDPUtils.matchPrefix(mediaSection, 'a=ssrc-group:FID')
  899. .map(function(line) {
  900. var parts = line.split(' ');
  901. parts.shift();
  902. return parts.map(function(part) {
  903. return parseInt(part, 10);
  904. });
  905. });
  906. if (flows.length > 0 && flows[0].length > 1 && flows[0][0] === primarySsrc) {
  907. secondarySsrc = flows[0][1];
  908. }
  909. description.codecs.forEach(function(codec) {
  910. if (codec.name.toUpperCase() === 'RTX' && codec.parameters.apt) {
  911. var encParam = {
  912. ssrc: primarySsrc,
  913. codecPayloadType: parseInt(codec.parameters.apt, 10),
  914. rtx: {
  915. payloadType: codec.payloadType,
  916. ssrc: secondarySsrc
  917. }
  918. };
  919. encodingParameters.push(encParam);
  920. if (hasRed) {
  921. encParam = JSON.parse(JSON.stringify(encParam));
  922. encParam.fec = {
  923. ssrc: secondarySsrc,
  924. mechanism: hasUlpfec ? 'red+ulpfec' : 'red'
  925. };
  926. encodingParameters.push(encParam);
  927. }
  928. }
  929. });
  930. if (encodingParameters.length === 0 && primarySsrc) {
  931. encodingParameters.push({
  932. ssrc: primarySsrc
  933. });
  934. }
  935. // we support both b=AS and b=TIAS but interpret AS as TIAS.
  936. var bandwidth = SDPUtils.matchPrefix(mediaSection, 'b=');
  937. if (bandwidth.length) {
  938. if (bandwidth[0].indexOf('b=TIAS:') === 0) {
  939. bandwidth = parseInt(bandwidth[0].substr(7), 10);
  940. } else if (bandwidth[0].indexOf('b=AS:') === 0) {
  941. bandwidth = parseInt(bandwidth[0].substr(5), 10);
  942. }
  943. encodingParameters.forEach(function(params) {
  944. params.maxBitrate = bandwidth;
  945. });
  946. }
  947. return encodingParameters;
  948. };
  949. SDPUtils.writeSessionBoilerplate = function() {
  950. // FIXME: sess-id should be an NTP timestamp.
  951. return 'v=0\r\n' +
  952. 'o=thisisadapterortc 8169639915646943137 2 IN IP4 127.0.0.1\r\n' +
  953. 's=-\r\n' +
  954. 't=0 0\r\n';
  955. };
  956. SDPUtils.writeMediaSection = function(transceiver, caps, type, stream) {
  957. var sdp = SDPUtils.writeRtpDescription(transceiver.kind, caps);
  958. // Map ICE parameters (ufrag, pwd) to SDP.
  959. sdp += SDPUtils.writeIceParameters(
  960. transceiver.iceGatherer.getLocalParameters());
  961. // Map DTLS parameters to SDP.
  962. sdp += SDPUtils.writeDtlsParameters(
  963. transceiver.dtlsTransport.getLocalParameters(),
  964. type === 'offer' ? 'actpass' : 'active');
  965. sdp += 'a=mid:' + transceiver.mid + '\r\n';
  966. if (transceiver.rtpSender && transceiver.rtpReceiver) {
  967. sdp += 'a=sendrecv\r\n';
  968. } else if (transceiver.rtpSender) {
  969. sdp += 'a=sendonly\r\n';
  970. } else if (transceiver.rtpReceiver) {
  971. sdp += 'a=recvonly\r\n';
  972. } else {
  973. sdp += 'a=inactive\r\n';
  974. }
  975. // FIXME: for RTX there might be multiple SSRCs. Not implemented in Edge yet.
  976. if (transceiver.rtpSender) {
  977. var msid = 'msid:' + stream.id + ' ' +
  978. transceiver.rtpSender.track.id + '\r\n';
  979. sdp += 'a=' + msid;
  980. sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].ssrc +
  981. ' ' + msid;
  982. }
  983. // FIXME: this should be written by writeRtpDescription.
  984. sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].ssrc +
  985. ' cname:' + SDPUtils.localCName + '\r\n';
  986. return sdp;
  987. };
  988. // Gets the direction from the mediaSection or the sessionpart.
  989. SDPUtils.getDirection = function(mediaSection, sessionpart) {
  990. // Look for sendrecv, sendonly, recvonly, inactive, default to sendrecv.
  991. var lines = SDPUtils.splitLines(mediaSection);
  992. for (var i = 0; i < lines.length; i++) {
  993. switch (lines[i]) {
  994. case 'a=sendrecv':
  995. case 'a=sendonly':
  996. case 'a=recvonly':
  997. case 'a=inactive':
  998. return lines[i].substr(2);
  999. default:
  1000. // FIXME: What should happen here?
  1001. }
  1002. }
  1003. if (sessionpart) {
  1004. return SDPUtils.getDirection(sessionpart);
  1005. }
  1006. return 'sendrecv';
  1007. };
  1008. // Expose public methods.
  1009. module.exports = SDPUtils;
  1010. },{}],2:[function(require,module,exports){
  1011. /*
  1012. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  1013. *
  1014. * Use of this source code is governed by a BSD-style license
  1015. * that can be found in the LICENSE file in the root of the source
  1016. * tree.
  1017. */
  1018. /* eslint-env node */
  1019. 'use strict';
  1020. // Shimming starts here.
  1021. (function() {
  1022. // Utils.
  1023. var logging = require('./utils').log;
  1024. var browserDetails = require('./utils').browserDetails;
  1025. // Export to the adapter global object visible in the browser.
  1026. module.exports.browserDetails = browserDetails;
  1027. module.exports.extractVersion = require('./utils').extractVersion;
  1028. module.exports.disableLog = require('./utils').disableLog;
  1029. // Uncomment the line below if you want logging to occur, including logging
  1030. // for the switch statement below. Can also be turned on in the browser via
  1031. // adapter.disableLog(false), but then logging from the switch statement below
  1032. // will not appear.
  1033. // require('./utils').disableLog(false);
  1034. // Browser shims.
  1035. var chromeShim = require('./chrome/chrome_shim') || null;
  1036. var edgeShim = require('./edge/edge_shim') || null;
  1037. var firefoxShim = require('./firefox/firefox_shim') || null;
  1038. var safariShim = require('./safari/safari_shim') || null;
  1039. // Shim browser if found.
  1040. switch (browserDetails.browser) {
  1041. case 'opera': // fallthrough as it uses chrome shims
  1042. case 'chrome':
  1043. if (!chromeShim || !chromeShim.shimPeerConnection) {
  1044. logging('Chrome shim is not included in this adapter release.');
  1045. return;
  1046. }
  1047. logging('adapter.js shimming chrome.');
  1048. // Export to the adapter global object visible in the browser.
  1049. module.exports.browserShim = chromeShim;
  1050. chromeShim.shimGetUserMedia();
  1051. chromeShim.shimMediaStream();
  1052. chromeShim.shimSourceObject();
  1053. chromeShim.shimPeerConnection();
  1054. chromeShim.shimOnTrack();
  1055. break;
  1056. case 'firefox':
  1057. if (!firefoxShim || !firefoxShim.shimPeerConnection) {
  1058. logging('Firefox shim is not included in this adapter release.');
  1059. return;
  1060. }
  1061. logging('adapter.js shimming firefox.');
  1062. // Export to the adapter global object visible in the browser.
  1063. module.exports.browserShim = firefoxShim;
  1064. firefoxShim.shimGetUserMedia();
  1065. firefoxShim.shimSourceObject();
  1066. firefoxShim.shimPeerConnection();
  1067. firefoxShim.shimOnTrack();
  1068. break;
  1069. case 'edge':
  1070. if (!edgeShim || !edgeShim.shimPeerConnection) {
  1071. logging('MS edge shim is not included in this adapter release.');
  1072. return;
  1073. }
  1074. logging('adapter.js shimming edge.');
  1075. // Export to the adapter global object visible in the browser.
  1076. module.exports.browserShim = edgeShim;
  1077. edgeShim.shimGetUserMedia();
  1078. edgeShim.shimPeerConnection();
  1079. break;
  1080. case 'safari':
  1081. if (!safariShim) {
  1082. logging('Safari shim is not included in this adapter release.');
  1083. return;
  1084. }
  1085. logging('adapter.js shimming safari.');
  1086. // Export to the adapter global object visible in the browser.
  1087. module.exports.browserShim = safariShim;
  1088. safariShim.shimGetUserMedia();
  1089. break;
  1090. default:
  1091. logging('Unsupported browser!');
  1092. }
  1093. })();
  1094. },{"./chrome/chrome_shim":3,"./edge/edge_shim":5,"./firefox/firefox_shim":7,"./safari/safari_shim":9,"./utils":10}],3:[function(require,module,exports){
  1095. /*
  1096. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  1097. *
  1098. * Use of this source code is governed by a BSD-style license
  1099. * that can be found in the LICENSE file in the root of the source
  1100. * tree.
  1101. */
  1102. /* eslint-env node */
  1103. 'use strict';
  1104. var logging = require('../utils.js').log;
  1105. var browserDetails = require('../utils.js').browserDetails;
  1106. var chromeShim = {
  1107. shimMediaStream: function() {
  1108. window.MediaStream = window.MediaStream || window.webkitMediaStream;
  1109. },
  1110. shimOnTrack: function() {
  1111. if (typeof window === 'object' && window.RTCPeerConnection && !('ontrack' in
  1112. window.RTCPeerConnection.prototype)) {
  1113. Object.defineProperty(window.RTCPeerConnection.prototype, 'ontrack', {
  1114. get: function() {
  1115. return this._ontrack;
  1116. },
  1117. set: function(f) {
  1118. var self = this;
  1119. if (this._ontrack) {
  1120. this.removeEventListener('track', this._ontrack);
  1121. this.removeEventListener('addstream', this._ontrackpoly);
  1122. }
  1123. this.addEventListener('track', this._ontrack = f);
  1124. this.addEventListener('addstream', this._ontrackpoly = function(e) {
  1125. // onaddstream does not fire when a track is added to an existing
  1126. // stream. But stream.onaddtrack is implemented so we use that.
  1127. e.stream.addEventListener('addtrack', function(te) {
  1128. var event = new Event('track');
  1129. event.track = te.track;
  1130. event.receiver = {track: te.track};
  1131. event.streams = [e.stream];
  1132. self.dispatchEvent(event);
  1133. });
  1134. e.stream.getTracks().forEach(function(track) {
  1135. var event = new Event('track');
  1136. event.track = track;
  1137. event.receiver = {track: track};
  1138. event.streams = [e.stream];
  1139. this.dispatchEvent(event);
  1140. }.bind(this));
  1141. }.bind(this));
  1142. }
  1143. });
  1144. }
  1145. },
  1146. shimSourceObject: function() {
  1147. if (typeof window === 'object') {
  1148. if (window.HTMLMediaElement &&
  1149. !('srcObject' in window.HTMLMediaElement.prototype)) {
  1150. // Shim the srcObject property, once, when HTMLMediaElement is found.
  1151. Object.defineProperty(window.HTMLMediaElement.prototype, 'srcObject', {
  1152. get: function() {
  1153. return this._srcObject;
  1154. },
  1155. set: function(stream) {
  1156. var self = this;
  1157. // Use _srcObject as a private property for this shim
  1158. this._srcObject = stream;
  1159. if (this.src) {
  1160. URL.revokeObjectURL(this.src);
  1161. }
  1162. if (!stream) {
  1163. this.src = '';
  1164. return;
  1165. }
  1166. this.src = URL.createObjectURL(stream);
  1167. // We need to recreate the blob url when a track is added or
  1168. // removed. Doing it manually since we want to avoid a recursion.
  1169. stream.addEventListener('addtrack', function() {
  1170. if (self.src) {
  1171. URL.revokeObjectURL(self.src);
  1172. }
  1173. self.src = URL.createObjectURL(stream);
  1174. });
  1175. stream.addEventListener('removetrack', function() {
  1176. if (self.src) {
  1177. URL.revokeObjectURL(self.src);
  1178. }
  1179. self.src = URL.createObjectURL(stream);
  1180. });
  1181. }
  1182. });
  1183. }
  1184. }
  1185. },
  1186. shimPeerConnection: function() {
  1187. // The RTCPeerConnection object.
  1188. window.RTCPeerConnection = function(pcConfig, pcConstraints) {
  1189. // Translate iceTransportPolicy to iceTransports,
  1190. // see https://code.google.com/p/webrtc/issues/detail?id=4869
  1191. logging('PeerConnection');
  1192. if (pcConfig && pcConfig.iceTransportPolicy) {
  1193. pcConfig.iceTransports = pcConfig.iceTransportPolicy;
  1194. }
  1195. var pc = new webkitRTCPeerConnection(pcConfig, pcConstraints);
  1196. var origGetStats = pc.getStats.bind(pc);
  1197. pc.getStats = function(selector, successCallback, errorCallback) {
  1198. var self = this;
  1199. var args = arguments;
  1200. // If selector is a function then we are in the old style stats so just
  1201. // pass back the original getStats format to avoid breaking old users.
  1202. if (arguments.length > 0 && typeof selector === 'function') {
  1203. return origGetStats(selector, successCallback);
  1204. }
  1205. var fixChromeStats_ = function(response) {
  1206. var standardReport = {};
  1207. var reports = response.result();
  1208. reports.forEach(function(report) {
  1209. var standardStats = {
  1210. id: report.id,
  1211. timestamp: report.timestamp,
  1212. type: report.type
  1213. };
  1214. report.names().forEach(function(name) {
  1215. standardStats[name] = report.stat(name);
  1216. });
  1217. standardReport[standardStats.id] = standardStats;
  1218. });
  1219. return standardReport;
  1220. };
  1221. // shim getStats with maplike support
  1222. var makeMapStats = function(stats, legacyStats) {
  1223. var map = new Map(Object.keys(stats).map(function(key) {
  1224. return[key, stats[key]];
  1225. }));
  1226. legacyStats = legacyStats || stats;
  1227. Object.keys(legacyStats).forEach(function(key) {
  1228. map[key] = legacyStats[key];
  1229. });
  1230. return map;
  1231. };
  1232. if (arguments.length >= 2) {
  1233. var successCallbackWrapper_ = function(response) {
  1234. args[1](makeMapStats(fixChromeStats_(response)));
  1235. };
  1236. return origGetStats.apply(this, [successCallbackWrapper_,
  1237. arguments[0]]);
  1238. }
  1239. // promise-support
  1240. return new Promise(function(resolve, reject) {
  1241. if (args.length === 1 && typeof selector === 'object') {
  1242. origGetStats.apply(self, [
  1243. function(response) {
  1244. resolve(makeMapStats(fixChromeStats_(response)));
  1245. }, reject]);
  1246. } else {
  1247. // Preserve legacy chrome stats only on legacy access of stats obj
  1248. origGetStats.apply(self, [
  1249. function(response) {
  1250. resolve(makeMapStats(fixChromeStats_(response),
  1251. response.result()));
  1252. }, reject]);
  1253. }
  1254. }).then(successCallback, errorCallback);
  1255. };
  1256. return pc;
  1257. };
  1258. window.RTCPeerConnection.prototype = webkitRTCPeerConnection.prototype;
  1259. // wrap static methods. Currently just generateCertificate.
  1260. if (webkitRTCPeerConnection.generateCertificate) {
  1261. Object.defineProperty(window.RTCPeerConnection, 'generateCertificate', {
  1262. get: function() {
  1263. return webkitRTCPeerConnection.generateCertificate;
  1264. }
  1265. });
  1266. }
  1267. ['createOffer', 'createAnswer'].forEach(function(method) {
  1268. var nativeMethod = webkitRTCPeerConnection.prototype[method];
  1269. webkitRTCPeerConnection.prototype[method] = function() {
  1270. var self = this;
  1271. if (arguments.length < 1 || (arguments.length === 1 &&
  1272. typeof arguments[0] === 'object')) {
  1273. var opts = arguments.length === 1 ? arguments[0] : undefined;
  1274. return new Promise(function(resolve, reject) {
  1275. nativeMethod.apply(self, [resolve, reject, opts]);
  1276. });
  1277. }
  1278. return nativeMethod.apply(this, arguments);
  1279. };
  1280. });
  1281. // add promise support -- natively available in Chrome 51
  1282. if (browserDetails.version < 51) {
  1283. ['setLocalDescription', 'setRemoteDescription', 'addIceCandidate']
  1284. .forEach(function(method) {
  1285. var nativeMethod = webkitRTCPeerConnection.prototype[method];
  1286. webkitRTCPeerConnection.prototype[method] = function() {
  1287. var args = arguments;
  1288. var self = this;
  1289. var promise = new Promise(function(resolve, reject) {
  1290. nativeMethod.apply(self, [args[0], resolve, reject]);
  1291. });
  1292. if (args.length < 2) {
  1293. return promise;
  1294. }
  1295. return promise.then(function() {
  1296. args[1].apply(null, []);
  1297. },
  1298. function(err) {
  1299. if (args.length >= 3) {
  1300. args[2].apply(null, [err]);
  1301. }
  1302. });
  1303. };
  1304. });
  1305. }
  1306. // shim implicit creation of RTCSessionDescription/RTCIceCandidate
  1307. ['setLocalDescription', 'setRemoteDescription', 'addIceCandidate']
  1308. .forEach(function(method) {
  1309. var nativeMethod = webkitRTCPeerConnection.prototype[method];
  1310. webkitRTCPeerConnection.prototype[method] = function() {
  1311. arguments[0] = new ((method === 'addIceCandidate') ?
  1312. RTCIceCandidate : RTCSessionDescription)(arguments[0]);
  1313. return nativeMethod.apply(this, arguments);
  1314. };
  1315. });
  1316. // support for addIceCandidate(null)
  1317. var nativeAddIceCandidate =
  1318. RTCPeerConnection.prototype.addIceCandidate;
  1319. RTCPeerConnection.prototype.addIceCandidate = function() {
  1320. return arguments[0] === null ? Promise.resolve()
  1321. : nativeAddIceCandidate.apply(this, arguments);
  1322. };
  1323. }
  1324. };
  1325. // Expose public methods.
  1326. module.exports = {
  1327. shimMediaStream: chromeShim.shimMediaStream,
  1328. shimOnTrack: chromeShim.shimOnTrack,
  1329. shimSourceObject: chromeShim.shimSourceObject,
  1330. shimPeerConnection: chromeShim.shimPeerConnection,
  1331. shimGetUserMedia: require('./getusermedia')
  1332. };
  1333. },{"../utils.js":10,"./getusermedia":4}],4:[function(require,module,exports){
  1334. /*
  1335. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  1336. *
  1337. * Use of this source code is governed by a BSD-style license
  1338. * that can be found in the LICENSE file in the root of the source
  1339. * tree.
  1340. */
  1341. /* eslint-env node */
  1342. 'use strict';
  1343. var logging = require('../utils.js').log;
  1344. // Expose public methods.
  1345. module.exports = function() {
  1346. var constraintsToChrome_ = function(c) {
  1347. if (typeof c !== 'object' || c.mandatory || c.optional) {
  1348. return c;
  1349. }
  1350. var cc = {};
  1351. Object.keys(c).forEach(function(key) {
  1352. if (key === 'require' || key === 'advanced' || key === 'mediaSource') {
  1353. return;
  1354. }
  1355. var r = (typeof c[key] === 'object') ? c[key] : {ideal: c[key]};
  1356. if (r.exact !== undefined && typeof r.exact === 'number') {
  1357. r.min = r.max = r.exact;
  1358. }
  1359. var oldname_ = function(prefix, name) {
  1360. if (prefix) {
  1361. return prefix + name.charAt(0).toUpperCase() + name.slice(1);
  1362. }
  1363. return (name === 'deviceId') ? 'sourceId' : name;
  1364. };
  1365. if (r.ideal !== undefined) {
  1366. cc.optional = cc.optional || [];
  1367. var oc = {};
  1368. if (typeof r.ideal === 'number') {
  1369. oc[oldname_('min', key)] = r.ideal;
  1370. cc.optional.push(oc);
  1371. oc = {};
  1372. oc[oldname_('max', key)] = r.ideal;
  1373. cc.optional.push(oc);
  1374. } else {
  1375. oc[oldname_('', key)] = r.ideal;
  1376. cc.optional.push(oc);
  1377. }
  1378. }
  1379. if (r.exact !== undefined && typeof r.exact !== 'number') {
  1380. cc.mandatory = cc.mandatory || {};
  1381. cc.mandatory[oldname_('', key)] = r.exact;
  1382. } else {
  1383. ['min', 'max'].forEach(function(mix) {
  1384. if (r[mix] !== undefined) {
  1385. cc.mandatory = cc.mandatory || {};
  1386. cc.mandatory[oldname_(mix, key)] = r[mix];
  1387. }
  1388. });
  1389. }
  1390. });
  1391. if (c.advanced) {
  1392. cc.optional = (cc.optional || []).concat(c.advanced);
  1393. }
  1394. return cc;
  1395. };
  1396. var shimConstraints_ = function(constraints, func) {
  1397. constraints = JSON.parse(JSON.stringify(constraints));
  1398. if (constraints && constraints.audio) {
  1399. constraints.audio = constraintsToChrome_(constraints.audio);
  1400. }
  1401. if (constraints && typeof constraints.video === 'object') {
  1402. // Shim facingMode for mobile, where it defaults to "user".
  1403. var face = constraints.video.facingMode;
  1404. face = face && ((typeof face === 'object') ? face : {ideal: face});
  1405. if ((face && (face.exact === 'user' || face.exact === 'environment' ||
  1406. face.ideal === 'user' || face.ideal === 'environment')) &&
  1407. !(navigator.mediaDevices.getSupportedConstraints &&
  1408. navigator.mediaDevices.getSupportedConstraints().facingMode)) {
  1409. delete constraints.video.facingMode;
  1410. if (face.exact === 'environment' || face.ideal === 'environment') {
  1411. // Look for "back" in label, or use last cam (typically back cam).
  1412. return navigator.mediaDevices.enumerateDevices()
  1413. .then(function(devices) {
  1414. devices = devices.filter(function(d) {
  1415. return d.kind === 'videoinput';
  1416. });
  1417. var back = devices.find(function(d) {
  1418. return d.label.toLowerCase().indexOf('back') !== -1;
  1419. }) || (devices.length && devices[devices.length - 1]);
  1420. if (back) {
  1421. constraints.video.deviceId = face.exact ? {exact: back.deviceId} :
  1422. {ideal: back.deviceId};
  1423. }
  1424. constraints.video = constraintsToChrome_(constraints.video);
  1425. logging('chrome: ' + JSON.stringify(constraints));
  1426. return func(constraints);
  1427. });
  1428. }
  1429. }
  1430. constraints.video = constraintsToChrome_(constraints.video);
  1431. }
  1432. logging('chrome: ' + JSON.stringify(constraints));
  1433. return func(constraints);
  1434. };
  1435. var shimError_ = function(e) {
  1436. return {
  1437. name: {
  1438. PermissionDeniedError: 'NotAllowedError',
  1439. ConstraintNotSatisfiedError: 'OverconstrainedError'
  1440. }[e.name] || e.name,
  1441. message: e.message,
  1442. constraint: e.constraintName,
  1443. toString: function() {
  1444. return this.name + (this.message && ': ') + this.message;
  1445. }
  1446. };
  1447. };
  1448. var getUserMedia_ = function(constraints, onSuccess, onError) {
  1449. shimConstraints_(constraints, function(c) {
  1450. navigator.webkitGetUserMedia(c, onSuccess, function(e) {
  1451. onError(shimError_(e));
  1452. });
  1453. });
  1454. };
  1455. navigator.getUserMedia = getUserMedia_;
  1456. // Returns the result of getUserMedia as a Promise.
  1457. var getUserMediaPromise_ = function(constraints) {
  1458. return new Promise(function(resolve, reject) {
  1459. navigator.getUserMedia(constraints, resolve, reject);
  1460. });
  1461. };
  1462. if (!navigator.mediaDevices) {
  1463. navigator.mediaDevices = {
  1464. getUserMedia: getUserMediaPromise_,
  1465. enumerateDevices: function() {
  1466. return new Promise(function(resolve) {
  1467. var kinds = {audio: 'audioinput', video: 'videoinput'};
  1468. return MediaStreamTrack.getSources(function(devices) {
  1469. resolve(devices.map(function(device) {
  1470. return {label: device.label,
  1471. kind: kinds[device.kind],
  1472. deviceId: device.id,
  1473. groupId: ''};
  1474. }));
  1475. });
  1476. });
  1477. }
  1478. };
  1479. }
  1480. // A shim for getUserMedia method on the mediaDevices object.
  1481. // TODO(KaptenJansson) remove once implemented in Chrome stable.
  1482. if (!navigator.mediaDevices.getUserMedia) {
  1483. navigator.mediaDevices.getUserMedia = function(constraints) {
  1484. return getUserMediaPromise_(constraints);
  1485. };
  1486. } else {
  1487. // Even though Chrome 45 has navigator.mediaDevices and a getUserMedia
  1488. // function which returns a Promise, it does not accept spec-style
  1489. // constraints.
  1490. var origGetUserMedia = navigator.mediaDevices.getUserMedia.
  1491. bind(navigator.mediaDevices);
  1492. navigator.mediaDevices.getUserMedia = function(cs) {
  1493. return shimConstraints_(cs, function(c) {
  1494. return origGetUserMedia(c).catch(function(e) {
  1495. return Promise.reject(shimError_(e));
  1496. });
  1497. });
  1498. };
  1499. }
  1500. // Dummy devicechange event methods.
  1501. // TODO(KaptenJansson) remove once implemented in Chrome stable.
  1502. if (typeof navigator.mediaDevices.addEventListener === 'undefined') {
  1503. navigator.mediaDevices.addEventListener = function() {
  1504. logging('Dummy mediaDevices.addEventListener called.');
  1505. };
  1506. }
  1507. if (typeof navigator.mediaDevices.removeEventListener === 'undefined') {
  1508. navigator.mediaDevices.removeEventListener = function() {
  1509. logging('Dummy mediaDevices.removeEventListener called.');
  1510. };
  1511. }
  1512. };
  1513. },{"../utils.js":10}],5:[function(require,module,exports){
  1514. /*
  1515. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  1516. *
  1517. * Use of this source code is governed by a BSD-style license
  1518. * that can be found in the LICENSE file in the root of the source
  1519. * tree.
  1520. */
  1521. /* eslint-env node */
  1522. 'use strict';
  1523. var SDPUtils = require('sdp');
  1524. var browserDetails = require('../utils').browserDetails;
  1525. var edgeShim = {
  1526. shimPeerConnection: function() {
  1527. if (window.RTCIceGatherer) {
  1528. // ORTC defines an RTCIceCandidate object but no constructor.
  1529. // Not implemented in Edge.
  1530. if (!window.RTCIceCandidate) {
  1531. window.RTCIceCandidate = function(args) {
  1532. return args;
  1533. };
  1534. }
  1535. // ORTC does not have a session description object but
  1536. // other browsers (i.e. Chrome) that will support both PC and ORTC
  1537. // in the future might have this defined already.
  1538. if (!window.RTCSessionDescription) {
  1539. window.RTCSessionDescription = function(args) {
  1540. return args;
  1541. };
  1542. }
  1543. }
  1544. window.RTCPeerConnection = function(config) {
  1545. var self = this;
  1546. var _eventTarget = document.createDocumentFragment();
  1547. ['addEventListener', 'removeEventListener', 'dispatchEvent']
  1548. .forEach(function(method) {
  1549. self[method] = _eventTarget[method].bind(_eventTarget);
  1550. });
  1551. this.onicecandidate = null;
  1552. this.onaddstream = null;
  1553. this.ontrack = null;
  1554. this.onremovestream = null;
  1555. this.onsignalingstatechange = null;
  1556. this.oniceconnectionstatechange = null;
  1557. this.onnegotiationneeded = null;
  1558. this.ondatachannel = null;
  1559. this.localStreams = [];
  1560. this.remoteStreams = [];
  1561. this.getLocalStreams = function() {
  1562. return self.localStreams;
  1563. };
  1564. this.getRemoteStreams = function() {
  1565. return self.remoteStreams;
  1566. };
  1567. this.localDescription = new RTCSessionDescription({
  1568. type: '',
  1569. sdp: ''
  1570. });
  1571. this.remoteDescription = new RTCSessionDescription({
  1572. type: '',
  1573. sdp: ''
  1574. });
  1575. this.signalingState = 'stable';
  1576. this.iceConnectionState = 'new';
  1577. this.iceGatheringState = 'new';
  1578. this.iceOptions = {
  1579. gatherPolicy: 'all',
  1580. iceServers: []
  1581. };
  1582. if (config && config.iceTransportPolicy) {
  1583. switch (config.iceTransportPolicy) {
  1584. case 'all':
  1585. case 'relay':
  1586. this.iceOptions.gatherPolicy = config.iceTransportPolicy;
  1587. break;
  1588. case 'none':
  1589. // FIXME: remove once implementation and spec have added this.
  1590. throw new TypeError('iceTransportPolicy "none" not supported');
  1591. default:
  1592. // don't set iceTransportPolicy.
  1593. break;
  1594. }
  1595. }
  1596. this.usingBundle = config && config.bundlePolicy === 'max-bundle';
  1597. if (config && config.iceServers) {
  1598. // Edge does not like
  1599. // 1) stun:
  1600. // 2) turn: that does not have all of turn:host:port?transport=udp
  1601. // 3) turn: with ipv6 addresses
  1602. var iceServers = JSON.parse(JSON.stringify(config.iceServers));
  1603. this.iceOptions.iceServers = iceServers.filter(function(server) {
  1604. if (server && server.urls) {
  1605. var urls = server.urls;
  1606. if (typeof urls === 'string') {
  1607. urls = [urls];
  1608. }
  1609. urls = urls.filter(function(url) {
  1610. return (url.indexOf('turn:') === 0 &&
  1611. url.indexOf('transport=udp') !== -1 &&
  1612. url.indexOf('turn:[') === -1) ||
  1613. (url.indexOf('stun:') === 0 &&
  1614. browserDetails.version >= 14393);
  1615. })[0];
  1616. return !!urls;
  1617. }
  1618. return false;
  1619. });
  1620. }
  1621. // per-track iceGathers, iceTransports, dtlsTransports, rtpSenders, ...
  1622. // everything that is needed to describe a SDP m-line.
  1623. this.transceivers = [];
  1624. // since the iceGatherer is currently created in createOffer but we
  1625. // must not emit candidates until after setLocalDescription we buffer
  1626. // them in this array.
  1627. this._localIceCandidatesBuffer = [];
  1628. };
  1629. window.RTCPeerConnection.prototype._emitBufferedCandidates = function() {
  1630. var self = this;
  1631. var sections = SDPUtils.splitSections(self.localDescription.sdp);
  1632. // FIXME: need to apply ice candidates in a way which is async but
  1633. // in-order
  1634. this._localIceCandidatesBuffer.forEach(function(event) {
  1635. var end = !event.candidate || Object.keys(event.candidate).length === 0;
  1636. if (end) {
  1637. for (var j = 1; j < sections.length; j++) {
  1638. if (sections[j].indexOf('\r\na=end-of-candidates\r\n') === -1) {
  1639. sections[j] += 'a=end-of-candidates\r\n';
  1640. }
  1641. }
  1642. } else if (event.candidate.candidate.indexOf('typ endOfCandidates')
  1643. === -1) {
  1644. sections[event.candidate.sdpMLineIndex + 1] +=
  1645. 'a=' + event.candidate.candidate + '\r\n';
  1646. }
  1647. self.localDescription.sdp = sections.join('');
  1648. self.dispatchEvent(event);
  1649. if (self.onicecandidate !== null) {
  1650. self.onicecandidate(event);
  1651. }
  1652. if (!event.candidate && self.iceGatheringState !== 'complete') {
  1653. var complete = self.transceivers.every(function(transceiver) {
  1654. return transceiver.iceGatherer &&
  1655. transceiver.iceGatherer.state === 'completed';
  1656. });
  1657. if (complete) {
  1658. self.iceGatheringState = 'complete';
  1659. }
  1660. }
  1661. });
  1662. this._localIceCandidatesBuffer = [];
  1663. };
  1664. window.RTCPeerConnection.prototype.addStream = function(stream) {
  1665. // Clone is necessary for local demos mostly, attaching directly
  1666. // to two different senders does not work (build 10547).
  1667. this.localStreams.push(stream.clone());
  1668. this._maybeFireNegotiationNeeded();
  1669. };
  1670. window.RTCPeerConnection.prototype.removeStream = function(stream) {
  1671. var idx = this.localStreams.indexOf(stream);
  1672. if (idx > -1) {
  1673. this.localStreams.splice(idx, 1);
  1674. this._maybeFireNegotiationNeeded();
  1675. }
  1676. };
  1677. window.RTCPeerConnection.prototype.getSenders = function() {
  1678. return this.transceivers.filter(function(transceiver) {
  1679. return !!transceiver.rtpSender;
  1680. })
  1681. .map(function(transceiver) {
  1682. return transceiver.rtpSender;
  1683. });
  1684. };
  1685. window.RTCPeerConnection.prototype.getReceivers = function() {
  1686. return this.transceivers.filter(function(transceiver) {
  1687. return !!transceiver.rtpReceiver;
  1688. })
  1689. .map(function(transceiver) {
  1690. return transceiver.rtpReceiver;
  1691. });
  1692. };
  1693. // Determines the intersection of local and remote capabilities.
  1694. window.RTCPeerConnection.prototype._getCommonCapabilities =
  1695. function(localCapabilities, remoteCapabilities) {
  1696. var commonCapabilities = {
  1697. codecs: [],
  1698. headerExtensions: [],
  1699. fecMechanisms: []
  1700. };
  1701. localCapabilities.codecs.forEach(function(lCodec) {
  1702. for (var i = 0; i < remoteCapabilities.codecs.length; i++) {
  1703. var rCodec = remoteCapabilities.codecs[i];
  1704. if (lCodec.name.toLowerCase() === rCodec.name.toLowerCase() &&
  1705. lCodec.clockRate === rCodec.clockRate &&
  1706. lCodec.numChannels === rCodec.numChannels) {
  1707. // push rCodec so we reply with offerer payload type
  1708. commonCapabilities.codecs.push(rCodec);
  1709. // determine common feedback mechanisms
  1710. rCodec.rtcpFeedback = rCodec.rtcpFeedback.filter(function(fb) {
  1711. for (var j = 0; j < lCodec.rtcpFeedback.length; j++) {
  1712. if (lCodec.rtcpFeedback[j].type === fb.type &&
  1713. lCodec.rtcpFeedback[j].parameter === fb.parameter) {
  1714. return true;
  1715. }
  1716. }
  1717. return false;
  1718. });
  1719. // FIXME: also need to determine .parameters
  1720. // see https://github.com/openpeer/ortc/issues/569
  1721. break;
  1722. }
  1723. }
  1724. });
  1725. localCapabilities.headerExtensions
  1726. .forEach(function(lHeaderExtension) {
  1727. for (var i = 0; i < remoteCapabilities.headerExtensions.length;
  1728. i++) {
  1729. var rHeaderExtension = remoteCapabilities.headerExtensions[i];
  1730. if (lHeaderExtension.uri === rHeaderExtension.uri) {
  1731. commonCapabilities.headerExtensions.push(rHeaderExtension);
  1732. break;
  1733. }
  1734. }
  1735. });
  1736. // FIXME: fecMechanisms
  1737. return commonCapabilities;
  1738. };
  1739. // Create ICE gatherer, ICE transport and DTLS transport.
  1740. window.RTCPeerConnection.prototype._createIceAndDtlsTransports =
  1741. function(mid, sdpMLineIndex) {
  1742. var self = this;
  1743. var iceGatherer = new RTCIceGatherer(self.iceOptions);
  1744. var iceTransport = new RTCIceTransport(iceGatherer);
  1745. iceGatherer.onlocalcandidate = function(evt) {
  1746. var event = new Event('icecandidate');
  1747. event.candidate = {sdpMid: mid, sdpMLineIndex: sdpMLineIndex};
  1748. var cand = evt.candidate;
  1749. var end = !cand || Object.keys(cand).length === 0;
  1750. // Edge emits an empty object for RTCIceCandidateComplete‥
  1751. if (end) {
  1752. // polyfill since RTCIceGatherer.state is not implemented in
  1753. // Edge 10547 yet.
  1754. if (iceGatherer.state === undefined) {
  1755. iceGatherer.state = 'completed';
  1756. }
  1757. // Emit a candidate with type endOfCandidates to make the samples
  1758. // work. Edge requires addIceCandidate with this empty candidate
  1759. // to start checking. The real solution is to signal
  1760. // end-of-candidates to the other side when getting the null
  1761. // candidate but some apps (like the samples) don't do that.
  1762. event.candidate.candidate =
  1763. 'candidate:1 1 udp 1 0.0.0.0 9 typ endOfCandidates';
  1764. } else {
  1765. // RTCIceCandidate doesn't have a component, needs to be added
  1766. cand.component = iceTransport.component === 'RTCP' ? 2 : 1;
  1767. event.candidate.candidate = SDPUtils.writeCandidate(cand);
  1768. }
  1769. // update local description.
  1770. var sections = SDPUtils.splitSections(self.localDescription.sdp);
  1771. if (event.candidate.candidate.indexOf('typ endOfCandidates')
  1772. === -1) {
  1773. sections[event.candidate.sdpMLineIndex + 1] +=
  1774. 'a=' + event.candidate.candidate + '\r\n';
  1775. } else {
  1776. sections[event.candidate.sdpMLineIndex + 1] +=
  1777. 'a=end-of-candidates\r\n';
  1778. }
  1779. self.localDescription.sdp = sections.join('');
  1780. var complete = self.transceivers.every(function(transceiver) {
  1781. return transceiver.iceGatherer &&
  1782. transceiver.iceGatherer.state === 'completed';
  1783. });
  1784. // Emit candidate if localDescription is set.
  1785. // Also emits null candidate when all gatherers are complete.
  1786. switch (self.iceGatheringState) {
  1787. case 'new':
  1788. self._localIceCandidatesBuffer.push(event);
  1789. if (end && complete) {
  1790. self._localIceCandidatesBuffer.push(
  1791. new Event('icecandidate'));
  1792. }
  1793. break;
  1794. case 'gathering':
  1795. self._emitBufferedCandidates();
  1796. self.dispatchEvent(event);
  1797. if (self.onicecandidate !== null) {
  1798. self.onicecandidate(event);
  1799. }
  1800. if (complete) {
  1801. self.dispatchEvent(new Event('icecandidate'));
  1802. if (self.onicecandidate !== null) {
  1803. self.onicecandidate(new Event('icecandidate'));
  1804. }
  1805. self.iceGatheringState = 'complete';
  1806. }
  1807. break;
  1808. case 'complete':
  1809. // should not happen... currently!
  1810. break;
  1811. default: // no-op.
  1812. break;
  1813. }
  1814. };
  1815. iceTransport.onicestatechange = function() {
  1816. self._updateConnectionState();
  1817. };
  1818. var dtlsTransport = new RTCDtlsTransport(iceTransport);
  1819. dtlsTransport.ondtlsstatechange = function() {
  1820. self._updateConnectionState();
  1821. };
  1822. dtlsTransport.onerror = function() {
  1823. // onerror does not set state to failed by itself.
  1824. dtlsTransport.state = 'failed';
  1825. self._updateConnectionState();
  1826. };
  1827. return {
  1828. iceGatherer: iceGatherer,
  1829. iceTransport: iceTransport,
  1830. dtlsTransport: dtlsTransport
  1831. };
  1832. };
  1833. // Start the RTP Sender and Receiver for a transceiver.
  1834. window.RTCPeerConnection.prototype._transceive = function(transceiver,
  1835. send, recv) {
  1836. var params = this._getCommonCapabilities(transceiver.localCapabilities,
  1837. transceiver.remoteCapabilities);
  1838. if (send && transceiver.rtpSender) {
  1839. params.encodings = transceiver.sendEncodingParameters;
  1840. params.rtcp = {
  1841. cname: SDPUtils.localCName
  1842. };
  1843. if (transceiver.recvEncodingParameters.length) {
  1844. params.rtcp.ssrc = transceiver.recvEncodingParameters[0].ssrc;
  1845. }
  1846. transceiver.rtpSender.send(params);
  1847. }
  1848. if (recv && transceiver.rtpReceiver) {
  1849. params.encodings = transceiver.recvEncodingParameters;
  1850. params.rtcp = {
  1851. cname: transceiver.cname
  1852. };
  1853. if (transceiver.sendEncodingParameters.length) {
  1854. params.rtcp.ssrc = transceiver.sendEncodingParameters[0].ssrc;
  1855. }
  1856. transceiver.rtpReceiver.receive(params);
  1857. }
  1858. };
  1859. window.RTCPeerConnection.prototype.setLocalDescription =
  1860. function(description) {
  1861. var self = this;
  1862. var sections;
  1863. var sessionpart;
  1864. if (description.type === 'offer') {
  1865. // FIXME: What was the purpose of this empty if statement?
  1866. // if (!this._pendingOffer) {
  1867. // } else {
  1868. if (this._pendingOffer) {
  1869. // VERY limited support for SDP munging. Limited to:
  1870. // * changing the order of codecs
  1871. sections = SDPUtils.splitSections(description.sdp);
  1872. sessionpart = sections.shift();
  1873. sections.forEach(function(mediaSection, sdpMLineIndex) {
  1874. var caps = SDPUtils.parseRtpParameters(mediaSection);
  1875. self._pendingOffer[sdpMLineIndex].localCapabilities = caps;
  1876. });
  1877. this.transceivers = this._pendingOffer;
  1878. delete this._pendingOffer;
  1879. }
  1880. } else if (description.type === 'answer') {
  1881. sections = SDPUtils.splitSections(self.remoteDescription.sdp);
  1882. sessionpart = sections.shift();
  1883. var isIceLite = SDPUtils.matchPrefix(sessionpart,
  1884. 'a=ice-lite').length > 0;
  1885. sections.forEach(function(mediaSection, sdpMLineIndex) {
  1886. var transceiver = self.transceivers[sdpMLineIndex];
  1887. var iceGatherer = transceiver.iceGatherer;
  1888. var iceTransport = transceiver.iceTransport;
  1889. var dtlsTransport = transceiver.dtlsTransport;
  1890. var localCapabilities = transceiver.localCapabilities;
  1891. var remoteCapabilities = transceiver.remoteCapabilities;
  1892. var rejected = mediaSection.split('\n', 1)[0]
  1893. .split(' ', 2)[1] === '0';
  1894. if (!rejected && !transceiver.isDatachannel) {
  1895. var remoteIceParameters = SDPUtils.getIceParameters(
  1896. mediaSection, sessionpart);
  1897. if (isIceLite) {
  1898. var cands = SDPUtils.matchPrefix(mediaSection, 'a=candidate:')
  1899. .map(function(cand) {
  1900. return SDPUtils.parseCandidate(cand);
  1901. })
  1902. .filter(function(cand) {
  1903. return cand.component === '1';
  1904. });
  1905. // ice-lite only includes host candidates in the SDP so we can
  1906. // use setRemoteCandidates (which implies an
  1907. // RTCIceCandidateComplete)
  1908. if (cands.length) {
  1909. iceTransport.setRemoteCandidates(cands);
  1910. }
  1911. }
  1912. var remoteDtlsParameters = SDPUtils.getDtlsParameters(
  1913. mediaSection, sessionpart);
  1914. if (isIceLite) {
  1915. remoteDtlsParameters.role = 'server';
  1916. }
  1917. if (!self.usingBundle || sdpMLineIndex === 0) {
  1918. iceTransport.start(iceGatherer, remoteIceParameters,
  1919. isIceLite ? 'controlling' : 'controlled');
  1920. dtlsTransport.start(remoteDtlsParameters);
  1921. }
  1922. // Calculate intersection of capabilities.
  1923. var params = self._getCommonCapabilities(localCapabilities,
  1924. remoteCapabilities);
  1925. // Start the RTCRtpSender. The RTCRtpReceiver for this
  1926. // transceiver has already been started in setRemoteDescription.
  1927. self._transceive(transceiver,
  1928. params.codecs.length > 0,
  1929. false);
  1930. }
  1931. });
  1932. }
  1933. this.localDescription = {
  1934. type: description.type,
  1935. sdp: description.sdp
  1936. };
  1937. switch (description.type) {
  1938. case 'offer':
  1939. this._updateSignalingState('have-local-offer');
  1940. break;
  1941. case 'answer':
  1942. this._updateSignalingState('stable');
  1943. break;
  1944. default:
  1945. throw new TypeError('unsupported type "' + description.type +
  1946. '"');
  1947. }
  1948. // If a success callback was provided, emit ICE candidates after it
  1949. // has been executed. Otherwise, emit callback after the Promise is
  1950. // resolved.
  1951. var hasCallback = arguments.length > 1 &&
  1952. typeof arguments[1] === 'function';
  1953. if (hasCallback) {
  1954. var cb = arguments[1];
  1955. window.setTimeout(function() {
  1956. cb();
  1957. if (self.iceGatheringState === 'new') {
  1958. self.iceGatheringState = 'gathering';
  1959. }
  1960. self._emitBufferedCandidates();
  1961. }, 0);
  1962. }
  1963. var p = Promise.resolve();
  1964. p.then(function() {
  1965. if (!hasCallback) {
  1966. if (self.iceGatheringState === 'new') {
  1967. self.iceGatheringState = 'gathering';
  1968. }
  1969. // Usually candidates will be emitted earlier.
  1970. window.setTimeout(self._emitBufferedCandidates.bind(self), 500);
  1971. }
  1972. });
  1973. return p;
  1974. };
  1975. window.RTCPeerConnection.prototype.setRemoteDescription =
  1976. function(description) {
  1977. var self = this;
  1978. var stream = new MediaStream();
  1979. var receiverList = [];
  1980. var sections = SDPUtils.splitSections(description.sdp);
  1981. var sessionpart = sections.shift();
  1982. var isIceLite = SDPUtils.matchPrefix(sessionpart,
  1983. 'a=ice-lite').length > 0;
  1984. this.usingBundle = SDPUtils.matchPrefix(sessionpart,
  1985. 'a=group:BUNDLE ').length > 0;
  1986. sections.forEach(function(mediaSection, sdpMLineIndex) {
  1987. var lines = SDPUtils.splitLines(mediaSection);
  1988. var mline = lines[0].substr(2).split(' ');
  1989. var kind = mline[0];
  1990. var rejected = mline[1] === '0';
  1991. var direction = SDPUtils.getDirection(mediaSection, sessionpart);
  1992. var mid = SDPUtils.matchPrefix(mediaSection, 'a=mid:');
  1993. if (mid.length) {
  1994. mid = mid[0].substr(6);
  1995. } else {
  1996. mid = SDPUtils.generateIdentifier();
  1997. }
  1998. // Reject datachannels which are not implemented yet.
  1999. if (kind === 'application' && mline[2] === 'DTLS/SCTP') {
  2000. self.transceivers[sdpMLineIndex] = {
  2001. mid: mid,
  2002. isDatachannel: true
  2003. };
  2004. return;
  2005. }
  2006. var transceiver;
  2007. var iceGatherer;
  2008. var iceTransport;
  2009. var dtlsTransport;
  2010. var rtpSender;
  2011. var rtpReceiver;
  2012. var sendEncodingParameters;
  2013. var recvEncodingParameters;
  2014. var localCapabilities;
  2015. var track;
  2016. // FIXME: ensure the mediaSection has rtcp-mux set.
  2017. var remoteCapabilities = SDPUtils.parseRtpParameters(mediaSection);
  2018. var remoteIceParameters;
  2019. var remoteDtlsParameters;
  2020. if (!rejected) {
  2021. remoteIceParameters = SDPUtils.getIceParameters(mediaSection,
  2022. sessionpart);
  2023. remoteDtlsParameters = SDPUtils.getDtlsParameters(mediaSection,
  2024. sessionpart);
  2025. remoteDtlsParameters.role = 'client';
  2026. }
  2027. recvEncodingParameters =
  2028. SDPUtils.parseRtpEncodingParameters(mediaSection);
  2029. var cname;
  2030. // Gets the first SSRC. Note that with RTX there might be multiple
  2031. // SSRCs.
  2032. var remoteSsrc = SDPUtils.matchPrefix(mediaSection, 'a=ssrc:')
  2033. .map(function(line) {
  2034. return SDPUtils.parseSsrcMedia(line);
  2035. })
  2036. .filter(function(obj) {
  2037. return obj.attribute === 'cname';
  2038. })[0];
  2039. if (remoteSsrc) {
  2040. cname = remoteSsrc.value;
  2041. }
  2042. var isComplete = SDPUtils.matchPrefix(mediaSection,
  2043. 'a=end-of-candidates', sessionpart).length > 0;
  2044. var cands = SDPUtils.matchPrefix(mediaSection, 'a=candidate:')
  2045. .map(function(cand) {
  2046. return SDPUtils.parseCandidate(cand);
  2047. })
  2048. .filter(function(cand) {
  2049. return cand.component === '1';
  2050. });
  2051. if (description.type === 'offer' && !rejected) {
  2052. var transports = self.usingBundle && sdpMLineIndex > 0 ? {
  2053. iceGatherer: self.transceivers[0].iceGatherer,
  2054. iceTransport: self.transceivers[0].iceTransport,
  2055. dtlsTransport: self.transceivers[0].dtlsTransport
  2056. } : self._createIceAndDtlsTransports(mid, sdpMLineIndex);
  2057. if (isComplete) {
  2058. transports.iceTransport.setRemoteCandidates(cands);
  2059. }
  2060. localCapabilities = RTCRtpReceiver.getCapabilities(kind);
  2061. sendEncodingParameters = [{
  2062. ssrc: (2 * sdpMLineIndex + 2) * 1001
  2063. }];
  2064. rtpReceiver = new RTCRtpReceiver(transports.dtlsTransport, kind);
  2065. track = rtpReceiver.track;
  2066. receiverList.push([track, rtpReceiver]);
  2067. // FIXME: not correct when there are multiple streams but that is
  2068. // not currently supported in this shim.
  2069. stream.addTrack(track);
  2070. // FIXME: look at direction.
  2071. if (self.localStreams.length > 0 &&
  2072. self.localStreams[0].getTracks().length >= sdpMLineIndex) {
  2073. var localTrack;
  2074. if (kind === 'audio') {
  2075. localTrack = self.localStreams[0].getAudioTracks()[0];
  2076. } else if (kind === 'video') {
  2077. localTrack = self.localStreams[0].getVideoTracks()[0];
  2078. }
  2079. if (localTrack) {
  2080. rtpSender = new RTCRtpSender(localTrack,
  2081. transports.dtlsTransport);
  2082. }
  2083. }
  2084. self.transceivers[sdpMLineIndex] = {
  2085. iceGatherer: transports.iceGatherer,
  2086. iceTransport: transports.iceTransport,
  2087. dtlsTransport: transports.dtlsTransport,
  2088. localCapabilities: localCapabilities,
  2089. remoteCapabilities: remoteCapabilities,
  2090. rtpSender: rtpSender,
  2091. rtpReceiver: rtpReceiver,
  2092. kind: kind,
  2093. mid: mid,
  2094. cname: cname,
  2095. sendEncodingParameters: sendEncodingParameters,
  2096. recvEncodingParameters: recvEncodingParameters
  2097. };
  2098. // Start the RTCRtpReceiver now. The RTPSender is started in
  2099. // setLocalDescription.
  2100. self._transceive(self.transceivers[sdpMLineIndex],
  2101. false,
  2102. direction === 'sendrecv' || direction === 'sendonly');
  2103. } else if (description.type === 'answer' && !rejected) {
  2104. transceiver = self.transceivers[sdpMLineIndex];
  2105. iceGatherer = transceiver.iceGatherer;
  2106. iceTransport = transceiver.iceTransport;
  2107. dtlsTransport = transceiver.dtlsTransport;
  2108. rtpSender = transceiver.rtpSender;
  2109. rtpReceiver = transceiver.rtpReceiver;
  2110. sendEncodingParameters = transceiver.sendEncodingParameters;
  2111. localCapabilities = transceiver.localCapabilities;
  2112. self.transceivers[sdpMLineIndex].recvEncodingParameters =
  2113. recvEncodingParameters;
  2114. self.transceivers[sdpMLineIndex].remoteCapabilities =
  2115. remoteCapabilities;
  2116. self.transceivers[sdpMLineIndex].cname = cname;
  2117. if ((isIceLite || isComplete) && cands.length) {
  2118. iceTransport.setRemoteCandidates(cands);
  2119. }
  2120. if (!self.usingBundle || sdpMLineIndex === 0) {
  2121. iceTransport.start(iceGatherer, remoteIceParameters,
  2122. 'controlling');
  2123. dtlsTransport.start(remoteDtlsParameters);
  2124. }
  2125. self._transceive(transceiver,
  2126. direction === 'sendrecv' || direction === 'recvonly',
  2127. direction === 'sendrecv' || direction === 'sendonly');
  2128. if (rtpReceiver &&
  2129. (direction === 'sendrecv' || direction === 'sendonly')) {
  2130. track = rtpReceiver.track;
  2131. receiverList.push([track, rtpReceiver]);
  2132. stream.addTrack(track);
  2133. } else {
  2134. // FIXME: actually the receiver should be created later.
  2135. delete transceiver.rtpReceiver;
  2136. }
  2137. }
  2138. });
  2139. this.remoteDescription = {
  2140. type: description.type,
  2141. sdp: description.sdp
  2142. };
  2143. switch (description.type) {
  2144. case 'offer':
  2145. this._updateSignalingState('have-remote-offer');
  2146. break;
  2147. case 'answer':
  2148. this._updateSignalingState('stable');
  2149. break;
  2150. default:
  2151. throw new TypeError('unsupported type "' + description.type +
  2152. '"');
  2153. }
  2154. if (stream.getTracks().length) {
  2155. self.remoteStreams.push(stream);
  2156. window.setTimeout(function() {
  2157. var event = new Event('addstream');
  2158. event.stream = stream;
  2159. self.dispatchEvent(event);
  2160. if (self.onaddstream !== null) {
  2161. window.setTimeout(function() {
  2162. self.onaddstream(event);
  2163. }, 0);
  2164. }
  2165. receiverList.forEach(function(item) {
  2166. var track = item[0];
  2167. var receiver = item[1];
  2168. var trackEvent = new Event('track');
  2169. trackEvent.track = track;
  2170. trackEvent.receiver = receiver;
  2171. trackEvent.streams = [stream];
  2172. self.dispatchEvent(event);
  2173. if (self.ontrack !== null) {
  2174. window.setTimeout(function() {
  2175. self.ontrack(trackEvent);
  2176. }, 0);
  2177. }
  2178. });
  2179. }, 0);
  2180. }
  2181. if (arguments.length > 1 && typeof arguments[1] === 'function') {
  2182. window.setTimeout(arguments[1], 0);
  2183. }
  2184. return Promise.resolve();
  2185. };
  2186. window.RTCPeerConnection.prototype.close = function() {
  2187. this.transceivers.forEach(function(transceiver) {
  2188. /* not yet
  2189. if (transceiver.iceGatherer) {
  2190. transceiver.iceGatherer.close();
  2191. }
  2192. */
  2193. if (transceiver.iceTransport) {
  2194. transceiver.iceTransport.stop();
  2195. }
  2196. if (transceiver.dtlsTransport) {
  2197. transceiver.dtlsTransport.stop();
  2198. }
  2199. if (transceiver.rtpSender) {
  2200. transceiver.rtpSender.stop();
  2201. }
  2202. if (transceiver.rtpReceiver) {
  2203. transceiver.rtpReceiver.stop();
  2204. }
  2205. });
  2206. // FIXME: clean up tracks, local streams, remote streams, etc
  2207. this._updateSignalingState('closed');
  2208. };
  2209. // Update the signaling state.
  2210. window.RTCPeerConnection.prototype._updateSignalingState =
  2211. function(newState) {
  2212. this.signalingState = newState;
  2213. var event = new Event('signalingstatechange');
  2214. this.dispatchEvent(event);
  2215. if (this.onsignalingstatechange !== null) {
  2216. this.onsignalingstatechange(event);
  2217. }
  2218. };
  2219. // Determine whether to fire the negotiationneeded event.
  2220. window.RTCPeerConnection.prototype._maybeFireNegotiationNeeded =
  2221. function() {
  2222. // Fire away (for now).
  2223. var event = new Event('negotiationneeded');
  2224. this.dispatchEvent(event);
  2225. if (this.onnegotiationneeded !== null) {
  2226. this.onnegotiationneeded(event);
  2227. }
  2228. };
  2229. // Update the connection state.
  2230. window.RTCPeerConnection.prototype._updateConnectionState = function() {
  2231. var self = this;
  2232. var newState;
  2233. var states = {
  2234. 'new': 0,
  2235. closed: 0,
  2236. connecting: 0,
  2237. checking: 0,
  2238. connected: 0,
  2239. completed: 0,
  2240. failed: 0
  2241. };
  2242. this.transceivers.forEach(function(transceiver) {
  2243. states[transceiver.iceTransport.state]++;
  2244. states[transceiver.dtlsTransport.state]++;
  2245. });
  2246. // ICETransport.completed and connected are the same for this purpose.
  2247. states.connected += states.completed;
  2248. newState = 'new';
  2249. if (states.failed > 0) {
  2250. newState = 'failed';
  2251. } else if (states.connecting > 0 || states.checking > 0) {
  2252. newState = 'connecting';
  2253. } else if (states.disconnected > 0) {
  2254. newState = 'disconnected';
  2255. } else if (states.new > 0) {
  2256. newState = 'new';
  2257. } else if (states.connected > 0 || states.completed > 0) {
  2258. newState = 'connected';
  2259. }
  2260. if (newState !== self.iceConnectionState) {
  2261. self.iceConnectionState = newState;
  2262. var event = new Event('iceconnectionstatechange');
  2263. this.dispatchEvent(event);
  2264. if (this.oniceconnectionstatechange !== null) {
  2265. this.oniceconnectionstatechange(event);
  2266. }
  2267. }
  2268. };
  2269. window.RTCPeerConnection.prototype.createOffer = function() {
  2270. var self = this;
  2271. if (this._pendingOffer) {
  2272. throw new Error('createOffer called while there is a pending offer.');
  2273. }
  2274. var offerOptions;
  2275. if (arguments.length === 1 && typeof arguments[0] !== 'function') {
  2276. offerOptions = arguments[0];
  2277. } else if (arguments.length === 3) {
  2278. offerOptions = arguments[2];
  2279. }
  2280. var tracks = [];
  2281. var numAudioTracks = 0;
  2282. var numVideoTracks = 0;
  2283. // Default to sendrecv.
  2284. if (this.localStreams.length) {
  2285. numAudioTracks = this.localStreams[0].getAudioTracks().length;
  2286. numVideoTracks = this.localStreams[0].getVideoTracks().length;
  2287. }
  2288. // Determine number of audio and video tracks we need to send/recv.
  2289. if (offerOptions) {
  2290. // Reject Chrome legacy constraints.
  2291. if (offerOptions.mandatory || offerOptions.optional) {
  2292. throw new TypeError(
  2293. 'Legacy mandatory/optional constraints not supported.');
  2294. }
  2295. if (offerOptions.offerToReceiveAudio !== undefined) {
  2296. numAudioTracks = offerOptions.offerToReceiveAudio;
  2297. }
  2298. if (offerOptions.offerToReceiveVideo !== undefined) {
  2299. numVideoTracks = offerOptions.offerToReceiveVideo;
  2300. }
  2301. }
  2302. if (this.localStreams.length) {
  2303. // Push local streams.
  2304. this.localStreams[0].getTracks().forEach(function(track) {
  2305. tracks.push({
  2306. kind: track.kind,
  2307. track: track,
  2308. wantReceive: track.kind === 'audio' ?
  2309. numAudioTracks > 0 : numVideoTracks > 0
  2310. });
  2311. if (track.kind === 'audio') {
  2312. numAudioTracks--;
  2313. } else if (track.kind === 'video') {
  2314. numVideoTracks--;
  2315. }
  2316. });
  2317. }
  2318. // Create M-lines for recvonly streams.
  2319. while (numAudioTracks > 0 || numVideoTracks > 0) {
  2320. if (numAudioTracks > 0) {
  2321. tracks.push({
  2322. kind: 'audio',
  2323. wantReceive: true
  2324. });
  2325. numAudioTracks--;
  2326. }
  2327. if (numVideoTracks > 0) {
  2328. tracks.push({
  2329. kind: 'video',
  2330. wantReceive: true
  2331. });
  2332. numVideoTracks--;
  2333. }
  2334. }
  2335. var sdp = SDPUtils.writeSessionBoilerplate();
  2336. var transceivers = [];
  2337. tracks.forEach(function(mline, sdpMLineIndex) {
  2338. // For each track, create an ice gatherer, ice transport,
  2339. // dtls transport, potentially rtpsender and rtpreceiver.
  2340. var track = mline.track;
  2341. var kind = mline.kind;
  2342. var mid = SDPUtils.generateIdentifier();
  2343. var transports = self.usingBundle && sdpMLineIndex > 0 ? {
  2344. iceGatherer: transceivers[0].iceGatherer,
  2345. iceTransport: transceivers[0].iceTransport,
  2346. dtlsTransport: transceivers[0].dtlsTransport
  2347. } : self._createIceAndDtlsTransports(mid, sdpMLineIndex);
  2348. var localCapabilities = RTCRtpSender.getCapabilities(kind);
  2349. var rtpSender;
  2350. var rtpReceiver;
  2351. // generate an ssrc now, to be used later in rtpSender.send
  2352. var sendEncodingParameters = [{
  2353. ssrc: (2 * sdpMLineIndex + 1) * 1001
  2354. }];
  2355. if (track) {
  2356. rtpSender = new RTCRtpSender(track, transports.dtlsTransport);
  2357. }
  2358. if (mline.wantReceive) {
  2359. rtpReceiver = new RTCRtpReceiver(transports.dtlsTransport, kind);
  2360. }
  2361. transceivers[sdpMLineIndex] = {
  2362. iceGatherer: transports.iceGatherer,
  2363. iceTransport: transports.iceTransport,
  2364. dtlsTransport: transports.dtlsTransport,
  2365. localCapabilities: localCapabilities,
  2366. remoteCapabilities: null,
  2367. rtpSender: rtpSender,
  2368. rtpReceiver: rtpReceiver,
  2369. kind: kind,
  2370. mid: mid,
  2371. sendEncodingParameters: sendEncodingParameters,
  2372. recvEncodingParameters: null
  2373. };
  2374. });
  2375. if (this.usingBundle) {
  2376. sdp += 'a=group:BUNDLE ' + transceivers.map(function(t) {
  2377. return t.mid;
  2378. }).join(' ') + '\r\n';
  2379. }
  2380. tracks.forEach(function(mline, sdpMLineIndex) {
  2381. var transceiver = transceivers[sdpMLineIndex];
  2382. sdp += SDPUtils.writeMediaSection(transceiver,
  2383. transceiver.localCapabilities, 'offer', self.localStreams[0]);
  2384. });
  2385. this._pendingOffer = transceivers;
  2386. var desc = new RTCSessionDescription({
  2387. type: 'offer',
  2388. sdp: sdp
  2389. });
  2390. if (arguments.length && typeof arguments[0] === 'function') {
  2391. window.setTimeout(arguments[0], 0, desc);
  2392. }
  2393. return Promise.resolve(desc);
  2394. };
  2395. window.RTCPeerConnection.prototype.createAnswer = function() {
  2396. var self = this;
  2397. var sdp = SDPUtils.writeSessionBoilerplate();
  2398. if (this.usingBundle) {
  2399. sdp += 'a=group:BUNDLE ' + this.transceivers.map(function(t) {
  2400. return t.mid;
  2401. }).join(' ') + '\r\n';
  2402. }
  2403. this.transceivers.forEach(function(transceiver) {
  2404. if (transceiver.isDatachannel) {
  2405. sdp += 'm=application 0 DTLS/SCTP 5000\r\n' +
  2406. 'c=IN IP4 0.0.0.0\r\n' +
  2407. 'a=mid:' + transceiver.mid + '\r\n';
  2408. return;
  2409. }
  2410. // Calculate intersection of capabilities.
  2411. var commonCapabilities = self._getCommonCapabilities(
  2412. transceiver.localCapabilities,
  2413. transceiver.remoteCapabilities);
  2414. sdp += SDPUtils.writeMediaSection(transceiver, commonCapabilities,
  2415. 'answer', self.localStreams[0]);
  2416. });
  2417. var desc = new RTCSessionDescription({
  2418. type: 'answer',
  2419. sdp: sdp
  2420. });
  2421. if (arguments.length && typeof arguments[0] === 'function') {
  2422. window.setTimeout(arguments[0], 0, desc);
  2423. }
  2424. return Promise.resolve(desc);
  2425. };
  2426. window.RTCPeerConnection.prototype.addIceCandidate = function(candidate) {
  2427. if (candidate === null) {
  2428. this.transceivers.forEach(function(transceiver) {
  2429. transceiver.iceTransport.addRemoteCandidate({});
  2430. });
  2431. } else {
  2432. var mLineIndex = candidate.sdpMLineIndex;
  2433. if (candidate.sdpMid) {
  2434. for (var i = 0; i < this.transceivers.length; i++) {
  2435. if (this.transceivers[i].mid === candidate.sdpMid) {
  2436. mLineIndex = i;
  2437. break;
  2438. }
  2439. }
  2440. }
  2441. var transceiver = this.transceivers[mLineIndex];
  2442. if (transceiver) {
  2443. var cand = Object.keys(candidate.candidate).length > 0 ?
  2444. SDPUtils.parseCandidate(candidate.candidate) : {};
  2445. // Ignore Chrome's invalid candidates since Edge does not like them.
  2446. if (cand.protocol === 'tcp' && (cand.port === 0 || cand.port === 9)) {
  2447. return;
  2448. }
  2449. // Ignore RTCP candidates, we assume RTCP-MUX.
  2450. if (cand.component !== '1') {
  2451. return;
  2452. }
  2453. // A dirty hack to make samples work.
  2454. if (cand.type === 'endOfCandidates') {
  2455. cand = {};
  2456. }
  2457. transceiver.iceTransport.addRemoteCandidate(cand);
  2458. // update the remoteDescription.
  2459. var sections = SDPUtils.splitSections(this.remoteDescription.sdp);
  2460. sections[mLineIndex + 1] += (cand.type ? candidate.candidate.trim()
  2461. : 'a=end-of-candidates') + '\r\n';
  2462. this.remoteDescription.sdp = sections.join('');
  2463. }
  2464. }
  2465. if (arguments.length > 1 && typeof arguments[1] === 'function') {
  2466. window.setTimeout(arguments[1], 0);
  2467. }
  2468. return Promise.resolve();
  2469. };
  2470. window.RTCPeerConnection.prototype.getStats = function() {
  2471. var promises = [];
  2472. this.transceivers.forEach(function(transceiver) {
  2473. ['rtpSender', 'rtpReceiver', 'iceGatherer', 'iceTransport',
  2474. 'dtlsTransport'].forEach(function(method) {
  2475. if (transceiver[method]) {
  2476. promises.push(transceiver[method].getStats());
  2477. }
  2478. });
  2479. });
  2480. var cb = arguments.length > 1 && typeof arguments[1] === 'function' &&
  2481. arguments[1];
  2482. return new Promise(function(resolve) {
  2483. // shim getStats with maplike support
  2484. var results = new Map();
  2485. Promise.all(promises).then(function(res) {
  2486. res.forEach(function(result) {
  2487. Object.keys(result).forEach(function(id) {
  2488. results.set(id, result[id]);
  2489. results[id] = result[id];
  2490. });
  2491. });
  2492. if (cb) {
  2493. window.setTimeout(cb, 0, results);
  2494. }
  2495. resolve(results);
  2496. });
  2497. });
  2498. };
  2499. }
  2500. };
  2501. // Expose public methods.
  2502. module.exports = {
  2503. shimPeerConnection: edgeShim.shimPeerConnection,
  2504. shimGetUserMedia: require('./getusermedia')
  2505. };
  2506. },{"../utils":10,"./getusermedia":6,"sdp":1}],6:[function(require,module,exports){
  2507. /*
  2508. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  2509. *
  2510. * Use of this source code is governed by a BSD-style license
  2511. * that can be found in the LICENSE file in the root of the source
  2512. * tree.
  2513. */
  2514. /* eslint-env node */
  2515. 'use strict';
  2516. // Expose public methods.
  2517. module.exports = function() {
  2518. var shimError_ = function(e) {
  2519. return {
  2520. name: {PermissionDeniedError: 'NotAllowedError'}[e.name] || e.name,
  2521. message: e.message,
  2522. constraint: e.constraint,
  2523. toString: function() {
  2524. return this.name;
  2525. }
  2526. };
  2527. };
  2528. // getUserMedia error shim.
  2529. var origGetUserMedia = navigator.mediaDevices.getUserMedia.
  2530. bind(navigator.mediaDevices);
  2531. navigator.mediaDevices.getUserMedia = function(c) {
  2532. return origGetUserMedia(c).catch(function(e) {
  2533. return Promise.reject(shimError_(e));
  2534. });
  2535. };
  2536. };
  2537. },{}],7:[function(require,module,exports){
  2538. /*
  2539. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  2540. *
  2541. * Use of this source code is governed by a BSD-style license
  2542. * that can be found in the LICENSE file in the root of the source
  2543. * tree.
  2544. */
  2545. /* eslint-env node */
  2546. 'use strict';
  2547. var browserDetails = require('../utils').browserDetails;
  2548. var firefoxShim = {
  2549. shimOnTrack: function() {
  2550. if (typeof window === 'object' && window.RTCPeerConnection && !('ontrack' in
  2551. window.RTCPeerConnection.prototype)) {
  2552. Object.defineProperty(window.RTCPeerConnection.prototype, 'ontrack', {
  2553. get: function() {
  2554. return this._ontrack;
  2555. },
  2556. set: function(f) {
  2557. if (this._ontrack) {
  2558. this.removeEventListener('track', this._ontrack);
  2559. this.removeEventListener('addstream', this._ontrackpoly);
  2560. }
  2561. this.addEventListener('track', this._ontrack = f);
  2562. this.addEventListener('addstream', this._ontrackpoly = function(e) {
  2563. e.stream.getTracks().forEach(function(track) {
  2564. var event = new Event('track');
  2565. event.track = track;
  2566. event.receiver = {track: track};
  2567. event.streams = [e.stream];
  2568. this.dispatchEvent(event);
  2569. }.bind(this));
  2570. }.bind(this));
  2571. }
  2572. });
  2573. }
  2574. },
  2575. shimSourceObject: function() {
  2576. // Firefox has supported mozSrcObject since FF22, unprefixed in 42.
  2577. if (typeof window === 'object') {
  2578. if (window.HTMLMediaElement &&
  2579. !('srcObject' in window.HTMLMediaElement.prototype)) {
  2580. // Shim the srcObject property, once, when HTMLMediaElement is found.
  2581. Object.defineProperty(window.HTMLMediaElement.prototype, 'srcObject', {
  2582. get: function() {
  2583. return this.mozSrcObject;
  2584. },
  2585. set: function(stream) {
  2586. this.mozSrcObject = stream;
  2587. }
  2588. });
  2589. }
  2590. }
  2591. },
  2592. shimPeerConnection: function() {
  2593. if (typeof window !== 'object' || !(window.RTCPeerConnection ||
  2594. window.mozRTCPeerConnection)) {
  2595. return; // probably media.peerconnection.enabled=false in about:config
  2596. }
  2597. // The RTCPeerConnection object.
  2598. if (!window.RTCPeerConnection) {
  2599. window.RTCPeerConnection = function(pcConfig, pcConstraints) {
  2600. if (browserDetails.version < 38) {
  2601. // .urls is not supported in FF < 38.
  2602. // create RTCIceServers with a single url.
  2603. if (pcConfig && pcConfig.iceServers) {
  2604. var newIceServers = [];
  2605. for (var i = 0; i < pcConfig.iceServers.length; i++) {
  2606. var server = pcConfig.iceServers[i];
  2607. if (server.hasOwnProperty('urls')) {
  2608. for (var j = 0; j < server.urls.length; j++) {
  2609. var newServer = {
  2610. url: server.urls[j]
  2611. };
  2612. if (server.urls[j].indexOf('turn') === 0) {
  2613. newServer.username = server.username;
  2614. newServer.credential = server.credential;
  2615. }
  2616. newIceServers.push(newServer);
  2617. }
  2618. } else {
  2619. newIceServers.push(pcConfig.iceServers[i]);
  2620. }
  2621. }
  2622. pcConfig.iceServers = newIceServers;
  2623. }
  2624. }
  2625. return new mozRTCPeerConnection(pcConfig, pcConstraints);
  2626. };
  2627. window.RTCPeerConnection.prototype = mozRTCPeerConnection.prototype;
  2628. // wrap static methods. Currently just generateCertificate.
  2629. if (mozRTCPeerConnection.generateCertificate) {
  2630. Object.defineProperty(window.RTCPeerConnection, 'generateCertificate', {
  2631. get: function() {
  2632. return mozRTCPeerConnection.generateCertificate;
  2633. }
  2634. });
  2635. }
  2636. window.RTCSessionDescription = mozRTCSessionDescription;
  2637. window.RTCIceCandidate = mozRTCIceCandidate;
  2638. }
  2639. // shim away need for obsolete RTCIceCandidate/RTCSessionDescription.
  2640. ['setLocalDescription', 'setRemoteDescription', 'addIceCandidate']
  2641. .forEach(function(method) {
  2642. var nativeMethod = RTCPeerConnection.prototype[method];
  2643. RTCPeerConnection.prototype[method] = function() {
  2644. arguments[0] = new ((method === 'addIceCandidate') ?
  2645. RTCIceCandidate : RTCSessionDescription)(arguments[0]);
  2646. return nativeMethod.apply(this, arguments);
  2647. };
  2648. });
  2649. // support for addIceCandidate(null)
  2650. var nativeAddIceCandidate =
  2651. RTCPeerConnection.prototype.addIceCandidate;
  2652. RTCPeerConnection.prototype.addIceCandidate = function() {
  2653. return arguments[0] === null ? Promise.resolve()
  2654. : nativeAddIceCandidate.apply(this, arguments);
  2655. };
  2656. // shim getStats with maplike support
  2657. var makeMapStats = function(stats) {
  2658. var map = new Map();
  2659. Object.keys(stats).forEach(function(key) {
  2660. map.set(key, stats[key]);
  2661. map[key] = stats[key];
  2662. });
  2663. return map;
  2664. };
  2665. var nativeGetStats = RTCPeerConnection.prototype.getStats;
  2666. RTCPeerConnection.prototype.getStats = function(selector, onSucc, onErr) {
  2667. return nativeGetStats.apply(this, [selector || null])
  2668. .then(function(stats) {
  2669. return makeMapStats(stats);
  2670. })
  2671. .then(onSucc, onErr);
  2672. };
  2673. }
  2674. };
  2675. // Expose public methods.
  2676. module.exports = {
  2677. shimOnTrack: firefoxShim.shimOnTrack,
  2678. shimSourceObject: firefoxShim.shimSourceObject,
  2679. shimPeerConnection: firefoxShim.shimPeerConnection,
  2680. shimGetUserMedia: require('./getusermedia')
  2681. };
  2682. },{"../utils":10,"./getusermedia":8}],8:[function(require,module,exports){
  2683. /*
  2684. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  2685. *
  2686. * Use of this source code is governed by a BSD-style license
  2687. * that can be found in the LICENSE file in the root of the source
  2688. * tree.
  2689. */
  2690. /* eslint-env node */
  2691. 'use strict';
  2692. var logging = require('../utils').log;
  2693. var browserDetails = require('../utils').browserDetails;
  2694. // Expose public methods.
  2695. module.exports = function() {
  2696. var shimError_ = function(e) {
  2697. return {
  2698. name: {
  2699. SecurityError: 'NotAllowedError',
  2700. PermissionDeniedError: 'NotAllowedError'
  2701. }[e.name] || e.name,
  2702. message: {
  2703. 'The operation is insecure.': 'The request is not allowed by the ' +
  2704. 'user agent or the platform in the current context.'
  2705. }[e.message] || e.message,
  2706. constraint: e.constraint,
  2707. toString: function() {
  2708. return this.name + (this.message && ': ') + this.message;
  2709. }
  2710. };
  2711. };
  2712. // getUserMedia constraints shim.
  2713. var getUserMedia_ = function(constraints, onSuccess, onError) {
  2714. var constraintsToFF37_ = function(c) {
  2715. if (typeof c !== 'object' || c.require) {
  2716. return c;
  2717. }
  2718. var require = [];
  2719. Object.keys(c).forEach(function(key) {
  2720. if (key === 'require' || key === 'advanced' || key === 'mediaSource') {
  2721. return;
  2722. }
  2723. var r = c[key] = (typeof c[key] === 'object') ?
  2724. c[key] : {ideal: c[key]};
  2725. if (r.min !== undefined ||
  2726. r.max !== undefined || r.exact !== undefined) {
  2727. require.push(key);
  2728. }
  2729. if (r.exact !== undefined) {
  2730. if (typeof r.exact === 'number') {
  2731. r. min = r.max = r.exact;
  2732. } else {
  2733. c[key] = r.exact;
  2734. }
  2735. delete r.exact;
  2736. }
  2737. if (r.ideal !== undefined) {
  2738. c.advanced = c.advanced || [];
  2739. var oc = {};
  2740. if (typeof r.ideal === 'number') {
  2741. oc[key] = {min: r.ideal, max: r.ideal};
  2742. } else {
  2743. oc[key] = r.ideal;
  2744. }
  2745. c.advanced.push(oc);
  2746. delete r.ideal;
  2747. if (!Object.keys(r).length) {
  2748. delete c[key];
  2749. }
  2750. }
  2751. });
  2752. if (require.length) {
  2753. c.require = require;
  2754. }
  2755. return c;
  2756. };
  2757. constraints = JSON.parse(JSON.stringify(constraints));
  2758. if (browserDetails.version < 38) {
  2759. logging('spec: ' + JSON.stringify(constraints));
  2760. if (constraints.audio) {
  2761. constraints.audio = constraintsToFF37_(constraints.audio);
  2762. }
  2763. if (constraints.video) {
  2764. constraints.video = constraintsToFF37_(constraints.video);
  2765. }
  2766. logging('ff37: ' + JSON.stringify(constraints));
  2767. }
  2768. return navigator.mozGetUserMedia(constraints, onSuccess, function(e) {
  2769. onError(shimError_(e));
  2770. });
  2771. };
  2772. // Returns the result of getUserMedia as a Promise.
  2773. var getUserMediaPromise_ = function(constraints) {
  2774. return new Promise(function(resolve, reject) {
  2775. getUserMedia_(constraints, resolve, reject);
  2776. });
  2777. };
  2778. // Shim for mediaDevices on older versions.
  2779. if (!navigator.mediaDevices) {
  2780. navigator.mediaDevices = {getUserMedia: getUserMediaPromise_,
  2781. addEventListener: function() { },
  2782. removeEventListener: function() { }
  2783. };
  2784. }
  2785. navigator.mediaDevices.enumerateDevices =
  2786. navigator.mediaDevices.enumerateDevices || function() {
  2787. return new Promise(function(resolve) {
  2788. var infos = [
  2789. {kind: 'audioinput', deviceId: 'default', label: '', groupId: ''},
  2790. {kind: 'videoinput', deviceId: 'default', label: '', groupId: ''}
  2791. ];
  2792. resolve(infos);
  2793. });
  2794. };
  2795. if (browserDetails.version < 41) {
  2796. // Work around http://bugzil.la/1169665
  2797. var orgEnumerateDevices =
  2798. navigator.mediaDevices.enumerateDevices.bind(navigator.mediaDevices);
  2799. navigator.mediaDevices.enumerateDevices = function() {
  2800. return orgEnumerateDevices().then(undefined, function(e) {
  2801. if (e.name === 'NotFoundError') {
  2802. return [];
  2803. }
  2804. throw e;
  2805. });
  2806. };
  2807. }
  2808. if (browserDetails.version < 49) {
  2809. var origGetUserMedia = navigator.mediaDevices.getUserMedia.
  2810. bind(navigator.mediaDevices);
  2811. navigator.mediaDevices.getUserMedia = function(c) {
  2812. return origGetUserMedia(c).catch(function(e) {
  2813. return Promise.reject(shimError_(e));
  2814. });
  2815. };
  2816. }
  2817. navigator.getUserMedia = function(constraints, onSuccess, onError) {
  2818. if (browserDetails.version < 44) {
  2819. return getUserMedia_(constraints, onSuccess, onError);
  2820. }
  2821. // Replace Firefox 44+'s deprecation warning with unprefixed version.
  2822. console.warn('navigator.getUserMedia has been replaced by ' +
  2823. 'navigator.mediaDevices.getUserMedia');
  2824. navigator.mediaDevices.getUserMedia(constraints).then(onSuccess, onError);
  2825. };
  2826. };
  2827. },{"../utils":10}],9:[function(require,module,exports){
  2828. /*
  2829. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  2830. *
  2831. * Use of this source code is governed by a BSD-style license
  2832. * that can be found in the LICENSE file in the root of the source
  2833. * tree.
  2834. */
  2835. 'use strict';
  2836. var safariShim = {
  2837. // TODO: DrAlex, should be here, double check against LayoutTests
  2838. // shimOnTrack: function() { },
  2839. // TODO: once the back-end for the mac port is done, add.
  2840. // TODO: check for webkitGTK+
  2841. // shimPeerConnection: function() { },
  2842. shimGetUserMedia: function() {
  2843. navigator.getUserMedia = navigator.webkitGetUserMedia;
  2844. }
  2845. };
  2846. // Expose public methods.
  2847. module.exports = {
  2848. shimGetUserMedia: safariShim.shimGetUserMedia
  2849. // TODO
  2850. // shimOnTrack: safariShim.shimOnTrack,
  2851. // shimPeerConnection: safariShim.shimPeerConnection
  2852. };
  2853. },{}],10:[function(require,module,exports){
  2854. /*
  2855. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  2856. *
  2857. * Use of this source code is governed by a BSD-style license
  2858. * that can be found in the LICENSE file in the root of the source
  2859. * tree.
  2860. */
  2861. /* eslint-env node */
  2862. 'use strict';
  2863. var logDisabled_ = true;
  2864. // Utility methods.
  2865. var utils = {
  2866. disableLog: function(bool) {
  2867. if (typeof bool !== 'boolean') {
  2868. return new Error('Argument type: ' + typeof bool +
  2869. '. Please use a boolean.');
  2870. }
  2871. logDisabled_ = bool;
  2872. return (bool) ? 'adapter.js logging disabled' :
  2873. 'adapter.js logging enabled';
  2874. },
  2875. log: function() {
  2876. if (typeof window === 'object') {
  2877. if (logDisabled_) {
  2878. return;
  2879. }
  2880. if (typeof console !== 'undefined' && typeof console.log === 'function') {
  2881. console.log.apply(console, arguments);
  2882. }
  2883. }
  2884. },
  2885. /**
  2886. * Extract browser version out of the provided user agent string.
  2887. *
  2888. * @param {!string} uastring userAgent string.
  2889. * @param {!string} expr Regular expression used as match criteria.
  2890. * @param {!number} pos position in the version string to be returned.
  2891. * @return {!number} browser version.
  2892. */
  2893. extractVersion: function(uastring, expr, pos) {
  2894. var match = uastring.match(expr);
  2895. return match && match.length >= pos && parseInt(match[pos], 10);
  2896. },
  2897. /**
  2898. * Browser detector.
  2899. *
  2900. * @return {object} result containing browser and version
  2901. * properties.
  2902. */
  2903. detectBrowser: function() {
  2904. // Returned result object.
  2905. var result = {};
  2906. result.browser = null;
  2907. result.version = null;
  2908. // Fail early if it's not a browser
  2909. if (typeof window === 'undefined' || !window.navigator) {
  2910. result.browser = 'Not a browser.';
  2911. return result;
  2912. }
  2913. // Firefox.
  2914. if (navigator.mozGetUserMedia) {
  2915. result.browser = 'firefox';
  2916. result.version = this.extractVersion(navigator.userAgent,
  2917. /Firefox\/([0-9]+)\./, 1);
  2918. // all webkit-based browsers
  2919. } else if (navigator.webkitGetUserMedia) {
  2920. // Chrome, Chromium, Webview, Opera, all use the chrome shim for now
  2921. if (window.webkitRTCPeerConnection) {
  2922. result.browser = 'chrome';
  2923. result.version = this.extractVersion(navigator.userAgent,
  2924. /Chrom(e|ium)\/([0-9]+)\./, 2);
  2925. // Safari or unknown webkit-based
  2926. // for the time being Safari has support for MediaStreams but not webRTC
  2927. } else {
  2928. // Safari UA substrings of interest for reference:
  2929. // - webkit version: AppleWebKit/602.1.25 (also used in Op,Cr)
  2930. // - safari UI version: Version/9.0.3 (unique to Safari)
  2931. // - safari UI webkit version: Safari/601.4.4 (also used in Op,Cr)
  2932. //
  2933. // if the webkit version and safari UI webkit versions are equals,
  2934. // ... this is a stable version.
  2935. //
  2936. // only the internal webkit version is important today to know if
  2937. // media streams are supported
  2938. //
  2939. if (navigator.userAgent.match(/Version\/(\d+).(\d+)/)) {
  2940. result.browser = 'safari';
  2941. result.version = this.extractVersion(navigator.userAgent,
  2942. /AppleWebKit\/([0-9]+)\./, 1);
  2943. // unknown webkit-based browser
  2944. } else {
  2945. result.browser = 'Unsupported webkit-based browser ' +
  2946. 'with GUM support but no WebRTC support.';
  2947. return result;
  2948. }
  2949. }
  2950. // Edge.
  2951. } else if (navigator.mediaDevices &&
  2952. navigator.userAgent.match(/Edge\/(\d+).(\d+)$/)) {
  2953. result.browser = 'edge';
  2954. result.version = this.extractVersion(navigator.userAgent,
  2955. /Edge\/(\d+).(\d+)$/, 2);
  2956. // Default fallthrough: not supported.
  2957. } else {
  2958. result.browser = 'Not a supported browser.';
  2959. return result;
  2960. }
  2961. return result;
  2962. }
  2963. };
  2964. // Export.
  2965. module.exports = {
  2966. log: utils.log,
  2967. disableLog: utils.disableLog,
  2968. browserDetails: utils.detectBrowser(),
  2969. extractVersion: utils.extractVersion
  2970. };
  2971. },{}]},{},[2])(2)
  2972. });
  2973. /* jshint ignore:end */
  2974. // END OF INJECTION OF GOOGLE'S ADAPTER.JS CONTENT
  2975. ///////////////////////////////////////////////////////////////////
  2976. AdapterJS.parseWebrtcDetectedBrowser();
  2977. ///////////////////////////////////////////////////////////////////
  2978. // EXTENSION FOR CHROME, FIREFOX AND EDGE
  2979. // Includes legacy functions
  2980. // -- createIceServer
  2981. // -- createIceServers
  2982. // -- MediaStreamTrack.getSources
  2983. //
  2984. // and additional shims
  2985. // -- attachMediaStream
  2986. // -- reattachMediaStream
  2987. // -- requestUserMedia
  2988. // -- a call to AdapterJS.maybeThroughWebRTCReady (notifies WebRTC is ready)
  2989. // Add support for legacy functions createIceServer and createIceServers
  2990. if ( navigator.mozGetUserMedia ) {
  2991. // Shim for MediaStreamTrack.getSources.
  2992. MediaStreamTrack.getSources = function(successCb) {
  2993. setTimeout(function() {
  2994. var infos = [
  2995. { kind: 'audio', id: 'default', label:'', facing:'' },
  2996. { kind: 'video', id: 'default', label:'', facing:'' }
  2997. ];
  2998. successCb(infos);
  2999. }, 0);
  3000. };
  3001. // Attach a media stream to an element.
  3002. attachMediaStream = function(element, stream) {
  3003. element.srcObject = stream;
  3004. return element;
  3005. };
  3006. reattachMediaStream = function(to, from) {
  3007. to.srcObject = from.srcObject;
  3008. return to;
  3009. };
  3010. createIceServer = function (url, username, password) {
  3011. console.warn('createIceServer is deprecated. It should be replaced with an application level implementation.');
  3012. // Note: Google's import of AJS will auto-reverse to 'url': '...' for FF < 38
  3013. var iceServer = null;
  3014. var urlParts = url.split(':');
  3015. if (urlParts[0].indexOf('stun') === 0) {
  3016. iceServer = { urls : [url] };
  3017. } else if (urlParts[0].indexOf('turn') === 0) {
  3018. if (webrtcDetectedVersion < 27) {
  3019. var turnUrlParts = url.split('?');
  3020. if (turnUrlParts.length === 1 ||
  3021. turnUrlParts[1].indexOf('transport=udp') === 0) {
  3022. iceServer = {
  3023. urls : [turnUrlParts[0]],
  3024. credential : password,
  3025. username : username
  3026. };
  3027. }
  3028. } else {
  3029. iceServer = {
  3030. urls : [url],
  3031. credential : password,
  3032. username : username
  3033. };
  3034. }
  3035. }
  3036. return iceServer;
  3037. };
  3038. createIceServers = function (urls, username, password) {
  3039. console.warn('createIceServers is deprecated. It should be replaced with an application level implementation.');
  3040. var iceServers = [];
  3041. for (i = 0; i < urls.length; i++) {
  3042. var iceServer = createIceServer(urls[i], username, password);
  3043. if (iceServer !== null) {
  3044. iceServers.push(iceServer);
  3045. }
  3046. }
  3047. return iceServers;
  3048. };
  3049. } else if ( navigator.webkitGetUserMedia ) {
  3050. // Attach a media stream to an element.
  3051. attachMediaStream = function(element, stream) {
  3052. if (webrtcDetectedVersion >= 43) {
  3053. element.srcObject = stream;
  3054. } else if (typeof element.src !== 'undefined') {
  3055. element.src = URL.createObjectURL(stream);
  3056. } else {
  3057. console.error('Error attaching stream to element.');
  3058. // logging('Error attaching stream to element.');
  3059. }
  3060. return element;
  3061. };
  3062. reattachMediaStream = function(to, from) {
  3063. if (webrtcDetectedVersion >= 43) {
  3064. to.srcObject = from.srcObject;
  3065. } else {
  3066. to.src = from.src;
  3067. }
  3068. return to;
  3069. };
  3070. createIceServer = function (url, username, password) {
  3071. console.warn('createIceServer is deprecated. It should be replaced with an application level implementation.');
  3072. var iceServer = null;
  3073. var urlParts = url.split(':');
  3074. if (urlParts[0].indexOf('stun') === 0) {
  3075. iceServer = { 'url' : url };
  3076. } else if (urlParts[0].indexOf('turn') === 0) {
  3077. iceServer = {
  3078. 'url' : url,
  3079. 'credential' : password,
  3080. 'username' : username
  3081. };
  3082. }
  3083. return iceServer;
  3084. };
  3085. createIceServers = function (urls, username, password) {
  3086. console.warn('createIceServers is deprecated. It should be replaced with an application level implementation.');
  3087. var iceServers = [];
  3088. if (webrtcDetectedVersion >= 34) {
  3089. iceServers = {
  3090. 'urls' : urls,
  3091. 'credential' : password,
  3092. 'username' : username
  3093. };
  3094. } else {
  3095. for (i = 0; i < urls.length; i++) {
  3096. var iceServer = createIceServer(urls[i], username, password);
  3097. if (iceServer !== null) {
  3098. iceServers.push(iceServer);
  3099. }
  3100. }
  3101. }
  3102. return iceServers;
  3103. };
  3104. } else if (navigator.mediaDevices && navigator.userAgent.match(/Edge\/(\d+).(\d+)$/)) {
  3105. // Attach a media stream to an element.
  3106. attachMediaStream = function(element, stream) {
  3107. element.srcObject = stream;
  3108. return element;
  3109. };
  3110. reattachMediaStream = function(to, from) {
  3111. to.srcObject = from.srcObject;
  3112. return to;
  3113. };
  3114. }
  3115. // Need to override attachMediaStream and reattachMediaStream
  3116. // to support the plugin's logic
  3117. attachMediaStream_base = attachMediaStream;
  3118. if (webrtcDetectedBrowser === 'opera') {
  3119. attachMediaStream_base = function (element, stream) {
  3120. if (webrtcDetectedVersion > 38) {
  3121. element.srcObject = stream;
  3122. } else if (typeof element.src !== 'undefined') {
  3123. element.src = URL.createObjectURL(stream);
  3124. }
  3125. // Else it doesn't work
  3126. };
  3127. }
  3128. attachMediaStream = function (element, stream) {
  3129. if ((webrtcDetectedBrowser === 'chrome' ||
  3130. webrtcDetectedBrowser === 'opera') &&
  3131. !stream) {
  3132. // Chrome does not support "src = null"
  3133. element.src = '';
  3134. } else {
  3135. attachMediaStream_base(element, stream);
  3136. }
  3137. return element;
  3138. };
  3139. reattachMediaStream_base = reattachMediaStream;
  3140. reattachMediaStream = function (to, from) {
  3141. reattachMediaStream_base(to, from);
  3142. return to;
  3143. };
  3144. // Propagate attachMediaStream and gUM in window and AdapterJS
  3145. window.attachMediaStream = attachMediaStream;
  3146. window.reattachMediaStream = reattachMediaStream;
  3147. window.getUserMedia = function(constraints, onSuccess, onFailure) {
  3148. navigator.getUserMedia(constraints, onSuccess, onFailure);
  3149. };
  3150. AdapterJS.attachMediaStream = attachMediaStream;
  3151. AdapterJS.reattachMediaStream = reattachMediaStream;
  3152. AdapterJS.getUserMedia = getUserMedia;
  3153. // Removed Google defined promises when promise is not defined
  3154. if (typeof Promise === 'undefined') {
  3155. requestUserMedia = null;
  3156. }
  3157. AdapterJS.maybeThroughWebRTCReady();
  3158. // END OF EXTENSION OF CHROME, FIREFOX AND EDGE
  3159. ///////////////////////////////////////////////////////////////////
  3160. } else { // TRY TO USE PLUGIN
  3161. ///////////////////////////////////////////////////////////////////
  3162. // WEBRTC PLUGIN SHIM
  3163. // Will automatically check if the plugin is available and inject it
  3164. // into the DOM if it is.
  3165. // When the plugin is not available, will prompt a banner to suggest installing it
  3166. // Use AdapterJS.options.hidePluginInstallPrompt to prevent this banner from popping
  3167. //
  3168. // Shims the follwing:
  3169. // -- getUserMedia
  3170. // -- MediaStreamTrack
  3171. // -- MediaStreamTrack.getSources
  3172. // -- RTCPeerConnection
  3173. // -- RTCSessionDescription
  3174. // -- RTCIceCandidate
  3175. // -- createIceServer
  3176. // -- createIceServers
  3177. // -- attachMediaStream
  3178. // -- reattachMediaStream
  3179. // -- webrtcDetectedBrowser
  3180. // -- webrtcDetectedVersion
  3181. // IE 9 is not offering an implementation of console.log until you open a console
  3182. if (typeof console !== 'object' || typeof console.log !== 'function') {
  3183. /* jshint -W020 */
  3184. console = {} || console;
  3185. // Implemented based on console specs from MDN
  3186. // You may override these functions
  3187. console.log = function (arg) {};
  3188. console.info = function (arg) {};
  3189. console.error = function (arg) {};
  3190. console.dir = function (arg) {};
  3191. console.exception = function (arg) {};
  3192. console.trace = function (arg) {};
  3193. console.warn = function (arg) {};
  3194. console.count = function (arg) {};
  3195. console.debug = function (arg) {};
  3196. console.count = function (arg) {};
  3197. console.time = function (arg) {};
  3198. console.timeEnd = function (arg) {};
  3199. console.group = function (arg) {};
  3200. console.groupCollapsed = function (arg) {};
  3201. console.groupEnd = function (arg) {};
  3202. /* jshint +W020 */
  3203. }
  3204. AdapterJS.parseWebrtcDetectedBrowser();
  3205. isIE = webrtcDetectedBrowser === 'IE';
  3206. /* jshint -W035 */
  3207. AdapterJS.WebRTCPlugin.WaitForPluginReady = function() {
  3208. while (AdapterJS.WebRTCPlugin.pluginState !== AdapterJS.WebRTCPlugin.PLUGIN_STATES.READY) {
  3209. /* empty because it needs to prevent the function from running. */
  3210. }
  3211. };
  3212. /* jshint +W035 */
  3213. AdapterJS.WebRTCPlugin.callWhenPluginReady = function (callback) {
  3214. if (AdapterJS.WebRTCPlugin.pluginState === AdapterJS.WebRTCPlugin.PLUGIN_STATES.READY) {
  3215. // Call immediately if possible
  3216. // Once the plugin is set, the code will always take this path
  3217. callback();
  3218. } else {
  3219. // otherwise start a 100ms interval
  3220. var checkPluginReadyState = setInterval(function () {
  3221. if (AdapterJS.WebRTCPlugin.pluginState === AdapterJS.WebRTCPlugin.PLUGIN_STATES.READY) {
  3222. clearInterval(checkPluginReadyState);
  3223. callback();
  3224. }
  3225. }, 100);
  3226. }
  3227. };
  3228. AdapterJS.WebRTCPlugin.setLogLevel = function(logLevel) {
  3229. AdapterJS.WebRTCPlugin.callWhenPluginReady(function() {
  3230. AdapterJS.WebRTCPlugin.plugin.setLogLevel(logLevel);
  3231. });
  3232. };
  3233. AdapterJS.WebRTCPlugin.injectPlugin = function () {
  3234. // only inject once the page is ready
  3235. if (document.readyState !== 'complete') {
  3236. return;
  3237. }
  3238. // Prevent multiple injections
  3239. if (AdapterJS.WebRTCPlugin.pluginState !== AdapterJS.WebRTCPlugin.PLUGIN_STATES.INITIALIZING) {
  3240. return;
  3241. }
  3242. AdapterJS.WebRTCPlugin.pluginState = AdapterJS.WebRTCPlugin.PLUGIN_STATES.INJECTING;
  3243. if (webrtcDetectedBrowser === 'IE' && webrtcDetectedVersion <= 10) {
  3244. var frag = document.createDocumentFragment();
  3245. AdapterJS.WebRTCPlugin.plugin = document.createElement('div');
  3246. AdapterJS.WebRTCPlugin.plugin.innerHTML = '<object id="' +
  3247. AdapterJS.WebRTCPlugin.pluginInfo.pluginId + '" type="' +
  3248. AdapterJS.WebRTCPlugin.pluginInfo.type + '" ' + 'width="1" height="1">' +
  3249. '<param name="pluginId" value="' +
  3250. AdapterJS.WebRTCPlugin.pluginInfo.pluginId + '" /> ' +
  3251. '<param name="windowless" value="false" /> ' +
  3252. '<param name="pageId" value="' + AdapterJS.WebRTCPlugin.pageId + '" /> ' +
  3253. '<param name="onload" value="' + AdapterJS.WebRTCPlugin.pluginInfo.onload + '" />' +
  3254. '<param name="tag" value="' + AdapterJS.WebRTCPlugin.TAGS.NONE + '" />' +
  3255. // uncomment to be able to use virtual cams
  3256. (AdapterJS.options.getAllCams ? '<param name="forceGetAllCams" value="True" />':'') +
  3257. '</object>';
  3258. while (AdapterJS.WebRTCPlugin.plugin.firstChild) {
  3259. frag.appendChild(AdapterJS.WebRTCPlugin.plugin.firstChild);
  3260. }
  3261. document.body.appendChild(frag);
  3262. // Need to re-fetch the plugin
  3263. AdapterJS.WebRTCPlugin.plugin =
  3264. document.getElementById(AdapterJS.WebRTCPlugin.pluginInfo.pluginId);
  3265. } else {
  3266. // Load Plugin
  3267. AdapterJS.WebRTCPlugin.plugin = document.createElement('object');
  3268. AdapterJS.WebRTCPlugin.plugin.id =
  3269. AdapterJS.WebRTCPlugin.pluginInfo.pluginId;
  3270. // IE will only start the plugin if it's ACTUALLY visible
  3271. if (isIE) {
  3272. AdapterJS.WebRTCPlugin.plugin.width = '1px';
  3273. AdapterJS.WebRTCPlugin.plugin.height = '1px';
  3274. } else { // The size of the plugin on Safari should be 0x0px
  3275. // so that the autorisation prompt is at the top
  3276. AdapterJS.WebRTCPlugin.plugin.width = '0px';
  3277. AdapterJS.WebRTCPlugin.plugin.height = '0px';
  3278. }
  3279. AdapterJS.WebRTCPlugin.plugin.type = AdapterJS.WebRTCPlugin.pluginInfo.type;
  3280. AdapterJS.WebRTCPlugin.plugin.innerHTML = '<param name="onload" value="' +
  3281. AdapterJS.WebRTCPlugin.pluginInfo.onload + '">' +
  3282. '<param name="pluginId" value="' +
  3283. AdapterJS.WebRTCPlugin.pluginInfo.pluginId + '">' +
  3284. '<param name="windowless" value="false" /> ' +
  3285. (AdapterJS.options.getAllCams ? '<param name="forceGetAllCams" value="True" />':'') +
  3286. '<param name="pageId" value="' + AdapterJS.WebRTCPlugin.pageId + '">' +
  3287. '<param name="tag" value="' + AdapterJS.WebRTCPlugin.TAGS.NONE + '" />';
  3288. document.body.appendChild(AdapterJS.WebRTCPlugin.plugin);
  3289. }
  3290. AdapterJS.WebRTCPlugin.pluginState = AdapterJS.WebRTCPlugin.PLUGIN_STATES.INJECTED;
  3291. };
  3292. AdapterJS.WebRTCPlugin.isPluginInstalled =
  3293. function (comName, plugName, plugType, installedCb, notInstalledCb) {
  3294. if (!isIE) {
  3295. var pluginArray = navigator.mimeTypes;
  3296. for (var i = 0; i < pluginArray.length; i++) {
  3297. if (pluginArray[i].type.indexOf(plugType) >= 0) {
  3298. installedCb();
  3299. return;
  3300. }
  3301. }
  3302. notInstalledCb();
  3303. } else {
  3304. try {
  3305. var axo = new ActiveXObject(comName + '.' + plugName);
  3306. } catch (e) {
  3307. notInstalledCb();
  3308. return;
  3309. }
  3310. installedCb();
  3311. }
  3312. };
  3313. AdapterJS.WebRTCPlugin.defineWebRTCInterface = function () {
  3314. if (AdapterJS.WebRTCPlugin.pluginState ===
  3315. AdapterJS.WebRTCPlugin.PLUGIN_STATES.READY) {
  3316. console.error('AdapterJS - WebRTC interface has already been defined');
  3317. return;
  3318. }
  3319. AdapterJS.WebRTCPlugin.pluginState = AdapterJS.WebRTCPlugin.PLUGIN_STATES.INITIALIZING;
  3320. AdapterJS.isDefined = function (variable) {
  3321. return variable !== null && variable !== undefined;
  3322. };
  3323. createIceServer = function (url, username, password) {
  3324. var iceServer = null;
  3325. var urlParts = url.split(':');
  3326. if (urlParts[0].indexOf('stun') === 0) {
  3327. iceServer = {
  3328. 'url' : url,
  3329. 'hasCredentials' : false
  3330. };
  3331. } else if (urlParts[0].indexOf('turn') === 0) {
  3332. iceServer = {
  3333. 'url' : url,
  3334. 'hasCredentials' : true,
  3335. 'credential' : password,
  3336. 'username' : username
  3337. };
  3338. }
  3339. return iceServer;
  3340. };
  3341. createIceServers = function (urls, username, password) {
  3342. var iceServers = [];
  3343. for (var i = 0; i < urls.length; ++i) {
  3344. iceServers.push(createIceServer(urls[i], username, password));
  3345. }
  3346. return iceServers;
  3347. };
  3348. RTCSessionDescription = function (info) {
  3349. AdapterJS.WebRTCPlugin.WaitForPluginReady();
  3350. return AdapterJS.WebRTCPlugin.plugin.
  3351. ConstructSessionDescription(info.type, info.sdp);
  3352. };
  3353. RTCPeerConnection = function (servers, constraints) {
  3354. // Validate server argumenr
  3355. if (!(servers === undefined ||
  3356. servers === null ||
  3357. Array.isArray(servers.iceServers))) {
  3358. throw new Error('Failed to construct \'RTCPeerConnection\': Malformed RTCConfiguration');
  3359. }
  3360. // Validate constraints argument
  3361. if (typeof constraints !== 'undefined' && constraints !== null) {
  3362. var invalidConstraits = false;
  3363. invalidConstraits |= typeof constraints !== 'object';
  3364. invalidConstraits |= constraints.hasOwnProperty('mandatory') &&
  3365. constraints.mandatory !== undefined &&
  3366. constraints.mandatory !== null &&
  3367. constraints.mandatory.constructor !== Object;
  3368. invalidConstraits |= constraints.hasOwnProperty('optional') &&
  3369. constraints.optional !== undefined &&
  3370. constraints.optional !== null &&
  3371. !Array.isArray(constraints.optional);
  3372. if (invalidConstraits) {
  3373. throw new Error('Failed to construct \'RTCPeerConnection\': Malformed constraints object');
  3374. }
  3375. }
  3376. // Call relevant PeerConnection constructor according to plugin version
  3377. AdapterJS.WebRTCPlugin.WaitForPluginReady();
  3378. // RTCPeerConnection prototype from the old spec
  3379. var iceServers = null;
  3380. if (servers && Array.isArray(servers.iceServers)) {
  3381. iceServers = servers.iceServers;
  3382. for (var i = 0; i < iceServers.length; i++) {
  3383. // Legacy plugin versions compatibility
  3384. if (iceServers[i].urls && !iceServers[i].url) {
  3385. iceServers[i].url = iceServers[i].urls;
  3386. }
  3387. iceServers[i].hasCredentials = AdapterJS.
  3388. isDefined(iceServers[i].username) &&
  3389. AdapterJS.isDefined(iceServers[i].credential);
  3390. }
  3391. }
  3392. if (AdapterJS.WebRTCPlugin.plugin.PEER_CONNECTION_VERSION &&
  3393. AdapterJS.WebRTCPlugin.plugin.PEER_CONNECTION_VERSION > 1) {
  3394. // RTCPeerConnection prototype from the new spec
  3395. if (iceServers) {
  3396. servers.iceServers = iceServers;
  3397. }
  3398. return AdapterJS.WebRTCPlugin.plugin.PeerConnection(servers);
  3399. } else {
  3400. var mandatory = (constraints && constraints.mandatory) ?
  3401. constraints.mandatory : null;
  3402. var optional = (constraints && constraints.optional) ?
  3403. constraints.optional : null;
  3404. return AdapterJS.WebRTCPlugin.plugin.
  3405. PeerConnection(AdapterJS.WebRTCPlugin.pageId,
  3406. iceServers, mandatory, optional);
  3407. }
  3408. };
  3409. MediaStreamTrack = function(){};
  3410. MediaStreamTrack.getSources = function (callback) {
  3411. AdapterJS.WebRTCPlugin.callWhenPluginReady(function() {
  3412. AdapterJS.WebRTCPlugin.plugin.GetSources(callback);
  3413. });
  3414. };
  3415. // getUserMedia constraints shim.
  3416. // Copied from Chrome
  3417. var constraintsToPlugin = function(c) {
  3418. if (typeof c !== 'object' || c.mandatory || c.optional) {
  3419. return c;
  3420. }
  3421. var cc = {};
  3422. Object.keys(c).forEach(function(key) {
  3423. if (key === 'require' || key === 'advanced' || key === 'mediaSource') {
  3424. return;
  3425. }
  3426. var r = (typeof c[key] === 'object') ? c[key] : {ideal: c[key]};
  3427. if (r.exact !== undefined && typeof r.exact === 'number') {
  3428. r.min = r.max = r.exact;
  3429. }
  3430. var oldname = function(prefix, name) {
  3431. if (prefix) {
  3432. return prefix + name.charAt(0).toUpperCase() + name.slice(1);
  3433. }
  3434. return (name === 'deviceId') ? 'sourceId' : name;
  3435. };
  3436. if (r.ideal !== undefined) {
  3437. cc.optional = cc.optional || [];
  3438. var oc = {};
  3439. if (typeof r.ideal === 'number') {
  3440. oc[oldname('min', key)] = r.ideal;
  3441. cc.optional.push(oc);
  3442. oc = {};
  3443. oc[oldname('max', key)] = r.ideal;
  3444. cc.optional.push(oc);
  3445. } else {
  3446. oc[oldname('', key)] = r.ideal;
  3447. cc.optional.push(oc);
  3448. }
  3449. }
  3450. if (r.exact !== undefined && typeof r.exact !== 'number') {
  3451. cc.mandatory = cc.mandatory || {};
  3452. cc.mandatory[oldname('', key)] = r.exact;
  3453. } else {
  3454. ['min', 'max'].forEach(function(mix) {
  3455. if (r[mix] !== undefined) {
  3456. cc.mandatory = cc.mandatory || {};
  3457. cc.mandatory[oldname(mix, key)] = r[mix];
  3458. }
  3459. });
  3460. }
  3461. });
  3462. if (c.advanced) {
  3463. cc.optional = (cc.optional || []).concat(c.advanced);
  3464. }
  3465. return cc;
  3466. };
  3467. getUserMedia = function (constraints, successCallback, failureCallback) {
  3468. var cc = {};
  3469. cc.audio = constraints.audio ?
  3470. constraintsToPlugin(constraints.audio) : false;
  3471. cc.video = constraints.video ?
  3472. constraintsToPlugin(constraints.video) : false;
  3473. AdapterJS.WebRTCPlugin.callWhenPluginReady(function() {
  3474. AdapterJS.WebRTCPlugin.plugin.
  3475. getUserMedia(cc, successCallback, failureCallback);
  3476. });
  3477. };
  3478. window.navigator.getUserMedia = getUserMedia;
  3479. // Defined mediaDevices when promises are available
  3480. if ( !navigator.mediaDevices &&
  3481. typeof Promise !== 'undefined') {
  3482. requestUserMedia = function(constraints) {
  3483. return new Promise(function(resolve, reject) {
  3484. getUserMedia(constraints, resolve, reject);
  3485. });
  3486. };
  3487. navigator.mediaDevices = {getUserMedia: requestUserMedia,
  3488. enumerateDevices: function() {
  3489. return new Promise(function(resolve) {
  3490. var kinds = {audio: 'audioinput', video: 'videoinput'};
  3491. return MediaStreamTrack.getSources(function(devices) {
  3492. resolve(devices.map(function(device) {
  3493. return {label: device.label,
  3494. kind: kinds[device.kind],
  3495. id: device.id,
  3496. deviceId: device.id,
  3497. groupId: ''};
  3498. }));
  3499. });
  3500. });
  3501. }};
  3502. }
  3503. attachMediaStream = function (element, stream) {
  3504. if (!element || !element.parentNode) {
  3505. return;
  3506. }
  3507. var streamId;
  3508. if (stream === null) {
  3509. streamId = '';
  3510. } else {
  3511. if (typeof stream.enableSoundTracks !== 'undefined') {
  3512. stream.enableSoundTracks(true);
  3513. }
  3514. streamId = stream.id;
  3515. }
  3516. var elementId = element.id.length === 0 ? Math.random().toString(36).slice(2) : element.id;
  3517. var nodeName = element.nodeName.toLowerCase();
  3518. if (nodeName !== 'object') { // not a plugin <object> tag yet
  3519. var tag;
  3520. switch(nodeName) {
  3521. case 'audio':
  3522. tag = AdapterJS.WebRTCPlugin.TAGS.AUDIO;
  3523. break;
  3524. case 'video':
  3525. tag = AdapterJS.WebRTCPlugin.TAGS.VIDEO;
  3526. break;
  3527. default:
  3528. tag = AdapterJS.WebRTCPlugin.TAGS.NONE;
  3529. }
  3530. var frag = document.createDocumentFragment();
  3531. var temp = document.createElement('div');
  3532. var classHTML = '';
  3533. if (element.className) {
  3534. classHTML = 'class="' + element.className + '" ';
  3535. } else if (element.attributes && element.attributes['class']) {
  3536. classHTML = 'class="' + element.attributes['class'].value + '" ';
  3537. }
  3538. temp.innerHTML = '<object id="' + elementId + '" ' + classHTML +
  3539. 'type="' + AdapterJS.WebRTCPlugin.pluginInfo.type + '">' +
  3540. '<param name="pluginId" value="' + elementId + '" /> ' +
  3541. '<param name="pageId" value="' + AdapterJS.WebRTCPlugin.pageId + '" /> ' +
  3542. '<param name="windowless" value="true" /> ' +
  3543. '<param name="streamId" value="' + streamId + '" /> ' +
  3544. '<param name="tag" value="' + tag + '" /> ' +
  3545. '</object>';
  3546. while (temp.firstChild) {
  3547. frag.appendChild(temp.firstChild);
  3548. }
  3549. var height = '';
  3550. var width = '';
  3551. if (element.clientWidth || element.clientHeight) {
  3552. width = element.clientWidth;
  3553. height = element.clientHeight;
  3554. }
  3555. else if (element.width || element.height) {
  3556. width = element.width;
  3557. height = element.height;
  3558. }
  3559. element.parentNode.insertBefore(frag, element);
  3560. frag = document.getElementById(elementId);
  3561. frag.width = width;
  3562. frag.height = height;
  3563. element.parentNode.removeChild(element);
  3564. } else { // already an <object> tag, just change the stream id
  3565. var children = element.children;
  3566. for (var i = 0; i !== children.length; ++i) {
  3567. if (children[i].name === 'streamId') {
  3568. children[i].value = streamId;
  3569. break;
  3570. }
  3571. }
  3572. element.setStreamId(streamId);
  3573. }
  3574. var newElement = document.getElementById(elementId);
  3575. AdapterJS.forwardEventHandlers(newElement, element, Object.getPrototypeOf(element));
  3576. return newElement;
  3577. };
  3578. reattachMediaStream = function (to, from) {
  3579. var stream = null;
  3580. var children = from.children;
  3581. for (var i = 0; i !== children.length; ++i) {
  3582. if (children[i].name === 'streamId') {
  3583. AdapterJS.WebRTCPlugin.WaitForPluginReady();
  3584. stream = AdapterJS.WebRTCPlugin.plugin
  3585. .getStreamWithId(AdapterJS.WebRTCPlugin.pageId, children[i].value);
  3586. break;
  3587. }
  3588. }
  3589. if (stream !== null) {
  3590. return attachMediaStream(to, stream);
  3591. } else {
  3592. console.log('Could not find the stream associated with this element');
  3593. }
  3594. };
  3595. // Propagate attachMediaStream and gUM in window and AdapterJS
  3596. window.attachMediaStream = attachMediaStream;
  3597. window.reattachMediaStream = reattachMediaStream;
  3598. window.getUserMedia = getUserMedia;
  3599. AdapterJS.attachMediaStream = attachMediaStream;
  3600. AdapterJS.reattachMediaStream = reattachMediaStream;
  3601. AdapterJS.getUserMedia = getUserMedia;
  3602. AdapterJS.forwardEventHandlers = function (destElem, srcElem, prototype) {
  3603. properties = Object.getOwnPropertyNames( prototype );
  3604. for(var prop in properties) {
  3605. if (prop) {
  3606. propName = properties[prop];
  3607. if (typeof propName.slice === 'function' &&
  3608. propName.slice(0,2) === 'on' &&
  3609. typeof srcElem[propName] === 'function') {
  3610. AdapterJS.addEvent(destElem, propName.slice(2), srcElem[propName]);
  3611. }
  3612. }
  3613. }
  3614. var subPrototype = Object.getPrototypeOf(prototype);
  3615. if(!!subPrototype) {
  3616. AdapterJS.forwardEventHandlers(destElem, srcElem, subPrototype);
  3617. }
  3618. };
  3619. RTCIceCandidate = function (candidate) {
  3620. if (!candidate.sdpMid) {
  3621. candidate.sdpMid = '';
  3622. }
  3623. AdapterJS.WebRTCPlugin.WaitForPluginReady();
  3624. return AdapterJS.WebRTCPlugin.plugin.ConstructIceCandidate(
  3625. candidate.sdpMid, candidate.sdpMLineIndex, candidate.candidate
  3626. );
  3627. };
  3628. // inject plugin
  3629. AdapterJS.addEvent(document, 'readystatechange', AdapterJS.WebRTCPlugin.injectPlugin);
  3630. AdapterJS.WebRTCPlugin.injectPlugin();
  3631. };
  3632. // This function will be called if the plugin is needed (browser different
  3633. // from Chrome or Firefox), but the plugin is not installed.
  3634. AdapterJS.WebRTCPlugin.pluginNeededButNotInstalledCb = AdapterJS.WebRTCPlugin.pluginNeededButNotInstalledCb ||
  3635. function() {
  3636. AdapterJS.addEvent(document,
  3637. 'readystatechange',
  3638. AdapterJS.WebRTCPlugin.pluginNeededButNotInstalledCbPriv);
  3639. AdapterJS.WebRTCPlugin.pluginNeededButNotInstalledCbPriv();
  3640. };
  3641. AdapterJS.WebRTCPlugin.pluginNeededButNotInstalledCbPriv = function () {
  3642. if (AdapterJS.options.hidePluginInstallPrompt) {
  3643. return;
  3644. }
  3645. var downloadLink = AdapterJS.WebRTCPlugin.pluginInfo.downloadLink;
  3646. if(downloadLink) { // if download link
  3647. var popupString;
  3648. if (AdapterJS.WebRTCPlugin.pluginInfo.portalLink) { // is portal link
  3649. popupString = 'This website requires you to install the ' +
  3650. ' <a href="' + AdapterJS.WebRTCPlugin.pluginInfo.portalLink +
  3651. '" target="_blank">' + AdapterJS.WebRTCPlugin.pluginInfo.companyName +
  3652. ' WebRTC Plugin</a>' +
  3653. ' to work on this browser.';
  3654. } else { // no portal link, just print a generic explanation
  3655. popupString = AdapterJS.TEXT.PLUGIN.REQUIRE_INSTALLATION;
  3656. }
  3657. AdapterJS.renderNotificationBar(popupString, AdapterJS.TEXT.PLUGIN.BUTTON, downloadLink);
  3658. } else { // no download link, just print a generic explanation
  3659. AdapterJS.renderNotificationBar(AdapterJS.TEXT.PLUGIN.NOT_SUPPORTED);
  3660. }
  3661. };
  3662. // Try to detect the plugin and act accordingly
  3663. AdapterJS.WebRTCPlugin.isPluginInstalled(
  3664. AdapterJS.WebRTCPlugin.pluginInfo.prefix,
  3665. AdapterJS.WebRTCPlugin.pluginInfo.plugName,
  3666. AdapterJS.WebRTCPlugin.pluginInfo.type,
  3667. AdapterJS.WebRTCPlugin.defineWebRTCInterface,
  3668. AdapterJS.WebRTCPlugin.pluginNeededButNotInstalledCb);
  3669. // END OF WEBRTC PLUGIN SHIM
  3670. ///////////////////////////////////////////////////////////////////
  3671. }
  3672. (function () {
  3673. 'use strict';
  3674. var baseGetUserMedia = null;
  3675. AdapterJS.TEXT.EXTENSION = {
  3676. REQUIRE_INSTALLATION_FF: 'To enable screensharing you need to install the Skylink WebRTC tools Firefox Add-on.',
  3677. REQUIRE_INSTALLATION_CHROME: 'To enable screensharing you need to install the Skylink WebRTC tools Chrome Extension.',
  3678. REQUIRE_REFRESH: 'Please refresh this page after the Skylink WebRTC tools extension has been installed.',
  3679. BUTTON_FF: 'Install Now',
  3680. BUTTON_CHROME: 'Go to Chrome Web Store'
  3681. };
  3682. var clone = function(obj) {
  3683. if (null === obj || 'object' !== typeof obj) {
  3684. return obj;
  3685. }
  3686. var copy = obj.constructor();
  3687. for (var attr in obj) {
  3688. if (obj.hasOwnProperty(attr)) {
  3689. copy[attr] = obj[attr];
  3690. }
  3691. }
  3692. return copy;
  3693. };
  3694. if (window.navigator.mozGetUserMedia) {
  3695. baseGetUserMedia = window.navigator.getUserMedia;
  3696. navigator.getUserMedia = function (constraints, successCb, failureCb) {
  3697. if (constraints && constraints.video && !!constraints.video.mediaSource) {
  3698. // intercepting screensharing requests
  3699. // Invalid mediaSource for firefox, only "screen" and "window" are supported
  3700. if (constraints.video.mediaSource !== 'screen' && constraints.video.mediaSource !== 'window') {
  3701. failureCb(new Error('GetUserMedia: Only "screen" and "window" are supported as mediaSource constraints'));
  3702. return;
  3703. }
  3704. var updatedConstraints = clone(constraints);
  3705. //constraints.video.mediaSource = constraints.video.mediaSource;
  3706. updatedConstraints.video.mozMediaSource = updatedConstraints.video.mediaSource;
  3707. // so generally, it requires for document.readyState to be completed before the getUserMedia could be invoked.
  3708. // strange but this works anyway
  3709. var checkIfReady = setInterval(function () {
  3710. if (document.readyState === 'complete') {
  3711. clearInterval(checkIfReady);
  3712. baseGetUserMedia(updatedConstraints, successCb, function (error) {
  3713. if (['PermissionDeniedError', 'SecurityError'].indexOf(error.name) > -1 && window.parent.location.protocol === 'https:') {
  3714. AdapterJS.renderNotificationBar(AdapterJS.TEXT.EXTENSION.REQUIRE_INSTALLATION_FF,
  3715. AdapterJS.TEXT.EXTENSION.BUTTON_FF,
  3716. 'https://addons.mozilla.org/en-US/firefox/addon/skylink-webrtc-tools/', true, true);
  3717. } else {
  3718. failureCb(error);
  3719. }
  3720. });
  3721. }
  3722. }, 1);
  3723. } else { // regular GetUserMediaRequest
  3724. baseGetUserMedia(constraints, successCb, failureCb);
  3725. }
  3726. };
  3727. AdapterJS.getUserMedia = window.getUserMedia = navigator.getUserMedia;
  3728. /* Comment out to prevent recursive errors
  3729. navigator.mediaDevices.getUserMedia = function(constraints) {
  3730. return new Promise(function(resolve, reject) {
  3731. window.getUserMedia(constraints, resolve, reject);
  3732. });
  3733. };*/
  3734. } else if (window.navigator.webkitGetUserMedia && window.webrtcDetectedBrowser !== 'safari') {
  3735. baseGetUserMedia = window.navigator.getUserMedia;
  3736. navigator.getUserMedia = function (constraints, successCb, failureCb) {
  3737. if (constraints && constraints.video && !!constraints.video.mediaSource) {
  3738. if (window.webrtcDetectedBrowser !== 'chrome') {
  3739. // This is Opera, which does not support screensharing
  3740. failureCb(new Error('Current browser does not support screensharing'));
  3741. return;
  3742. }
  3743. // would be fine since no methods
  3744. var updatedConstraints = clone(constraints);
  3745. var chromeCallback = function(error, sourceId) {
  3746. if(!error) {
  3747. updatedConstraints.video.mandatory = updatedConstraints.video.mandatory || {};
  3748. updatedConstraints.video.mandatory.chromeMediaSource = 'desktop';
  3749. updatedConstraints.video.mandatory.maxWidth = window.screen.width > 1920 ? window.screen.width : 1920;
  3750. updatedConstraints.video.mandatory.maxHeight = window.screen.height > 1080 ? window.screen.height : 1080;
  3751. if (sourceId) {
  3752. updatedConstraints.video.mandatory.chromeMediaSourceId = sourceId;
  3753. }
  3754. delete updatedConstraints.video.mediaSource;
  3755. baseGetUserMedia(updatedConstraints, successCb, failureCb);
  3756. } else { // GUM failed
  3757. if (error === 'permission-denied') {
  3758. failureCb(new Error('Permission denied for screen retrieval'));
  3759. } else {
  3760. // NOTE(J-O): I don't think we ever pass in here.
  3761. // A failure to capture the screen does not lead here.
  3762. failureCb(new Error('Failed retrieving selected screen'));
  3763. }
  3764. }
  3765. };
  3766. var onIFrameCallback = function (event) {
  3767. if (!event.data) {
  3768. return;
  3769. }
  3770. if (event.data.chromeMediaSourceId) {
  3771. if (event.data.chromeMediaSourceId === 'PermissionDeniedError') {
  3772. chromeCallback('permission-denied');
  3773. } else {
  3774. chromeCallback(null, event.data.chromeMediaSourceId);
  3775. }
  3776. }
  3777. if (event.data.chromeExtensionStatus) {
  3778. if (event.data.chromeExtensionStatus === 'not-installed') {
  3779. AdapterJS.renderNotificationBar(AdapterJS.TEXT.EXTENSION.REQUIRE_INSTALLATION_CHROME,
  3780. AdapterJS.TEXT.EXTENSION.BUTTON_CHROME,
  3781. event.data.data, true, true);
  3782. } else {
  3783. chromeCallback(event.data.chromeExtensionStatus, null);
  3784. }
  3785. }
  3786. // this event listener is no more needed
  3787. window.removeEventListener('message', onIFrameCallback);
  3788. };
  3789. window.addEventListener('message', onIFrameCallback);
  3790. postFrameMessage({
  3791. captureSourceId: true
  3792. });
  3793. } else {
  3794. baseGetUserMedia(constraints, successCb, failureCb);
  3795. }
  3796. };
  3797. AdapterJS.getUserMedia = window.getUserMedia = navigator.getUserMedia;
  3798. navigator.mediaDevices.getUserMedia = function(constraints) {
  3799. return new Promise(function(resolve, reject) {
  3800. window.getUserMedia(constraints, resolve, reject);
  3801. });
  3802. };
  3803. } else if (navigator.mediaDevices && navigator.userAgent.match(/Edge\/(\d+).(\d+)$/)) {
  3804. // nothing here because edge does not support screensharing
  3805. console.warn('Edge does not support screensharing feature in getUserMedia');
  3806. } else {
  3807. baseGetUserMedia = window.navigator.getUserMedia;
  3808. navigator.getUserMedia = function (constraints, successCb, failureCb) {
  3809. if (constraints && constraints.video && !!constraints.video.mediaSource) {
  3810. // would be fine since no methods
  3811. var updatedConstraints = clone(constraints);
  3812. // wait for plugin to be ready
  3813. AdapterJS.WebRTCPlugin.callWhenPluginReady(function() {
  3814. // check if screensharing feature is available
  3815. if (!!AdapterJS.WebRTCPlugin.plugin.HasScreensharingFeature &&
  3816. !!AdapterJS.WebRTCPlugin.plugin.isScreensharingAvailable) {
  3817. // set the constraints
  3818. updatedConstraints.video.optional = updatedConstraints.video.optional || [];
  3819. updatedConstraints.video.optional.push({
  3820. sourceId: AdapterJS.WebRTCPlugin.plugin.screensharingKey || 'Screensharing'
  3821. });
  3822. delete updatedConstraints.video.mediaSource;
  3823. } else {
  3824. failureCb(new Error('Your version of the WebRTC plugin does not support screensharing'));
  3825. return;
  3826. }
  3827. baseGetUserMedia(updatedConstraints, successCb, failureCb);
  3828. });
  3829. } else {
  3830. baseGetUserMedia(constraints, successCb, failureCb);
  3831. }
  3832. };
  3833. AdapterJS.getUserMedia = getUserMedia =
  3834. window.getUserMedia = navigator.getUserMedia;
  3835. if ( navigator.mediaDevices &&
  3836. typeof Promise !== 'undefined') {
  3837. navigator.mediaDevices.getUserMedia = requestUserMedia;
  3838. }
  3839. }
  3840. // For chrome, use an iframe to load the screensharing extension
  3841. // in the correct domain.
  3842. // Modify here for custom screensharing extension in chrome
  3843. if (window.webrtcDetectedBrowser === 'chrome') {
  3844. var iframe = document.createElement('iframe');
  3845. iframe.onload = function() {
  3846. iframe.isLoaded = true;
  3847. };
  3848. iframe.src = 'https://cdn.temasys.com.sg/skylink/extensions/detectRTC.html';
  3849. iframe.style.display = 'none';
  3850. (document.body || document.documentElement).appendChild(iframe);
  3851. var postFrameMessage = function (object) { // jshint ignore:line
  3852. object = object || {};
  3853. if (!iframe.isLoaded) {
  3854. setTimeout(function () {
  3855. iframe.contentWindow.postMessage(object, '*');
  3856. }, 100);
  3857. return;
  3858. }
  3859. iframe.contentWindow.postMessage(object, '*');
  3860. };
  3861. } else if (window.webrtcDetectedBrowser === 'opera') {
  3862. console.warn('Opera does not support screensharing feature in getUserMedia');
  3863. }
  3864. })();