ZLMRTCClient.js 245 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546
  1. var ZLMRTCClient = (function (exports) {
  2. 'use strict';
  3. const Events$1 = {
  4. WEBRTC_NOT_SUPPORT: 'WEBRTC_NOT_SUPPORT',
  5. WEBRTC_ICE_CANDIDATE_ERROR: 'WEBRTC_ICE_CANDIDATE_ERROR',
  6. WEBRTC_OFFER_ANWSER_EXCHANGE_FAILED: 'WEBRTC_OFFER_ANWSER_EXCHANGE_FAILED',
  7. WEBRTC_ON_REMOTE_STREAMS: 'WEBRTC_ON_REMOTE_STREAMS',
  8. WEBRTC_ON_LOCAL_STREAM: 'WEBRTC_ON_LOCAL_STREAM'
  9. };
  10. const VERSION = '1.0.1';
  11. const BUILD_DATE = 'Mon Apr 05 2021 10:22:48 GMT+0800 (中国标准时间)';
  12. // Copyright (C) <2018> Intel Corporation
  13. //
  14. // SPDX-License-Identifier: Apache-2.0
  15. // eslint-disable-next-line require-jsdoc
  16. function isFirefox() {
  17. return window.navigator.userAgent.match('Firefox') !== null;
  18. } // eslint-disable-next-line require-jsdoc
  19. function isChrome() {
  20. return window.navigator.userAgent.match('Chrome') !== null;
  21. } // eslint-disable-next-line require-jsdoc
  22. function isEdge() {
  23. return window.navigator.userAgent.match(/Edge\/(\d+).(\d+)$/) !== null;
  24. } // eslint-disable-next-line require-jsdoc
  25. // Copyright (C) <2018> Intel Corporation
  26. /**
  27. * @class AudioSourceInfo
  28. * @classDesc Source info about an audio track. Values: 'mic', 'screen-cast', 'file', 'mixed'.
  29. * @memberOf Owt.Base
  30. * @readonly
  31. * @enum {string}
  32. */
  33. const AudioSourceInfo = {
  34. MIC: 'mic',
  35. SCREENCAST: 'screen-cast',
  36. FILE: 'file',
  37. MIXED: 'mixed'
  38. };
  39. /**
  40. * @class VideoSourceInfo
  41. * @classDesc Source info about a video track. Values: 'camera', 'screen-cast', 'file', 'mixed'.
  42. * @memberOf Owt.Base
  43. * @readonly
  44. * @enum {string}
  45. */
  46. const VideoSourceInfo = {
  47. CAMERA: 'camera',
  48. SCREENCAST: 'screen-cast',
  49. FILE: 'file',
  50. MIXED: 'mixed'
  51. };
  52. /**
  53. * @class TrackKind
  54. * @classDesc Kind of a track. Values: 'audio' for audio track, 'video' for video track, 'av' for both audio and video tracks.
  55. * @memberOf Owt.Base
  56. * @readonly
  57. * @enum {string}
  58. */
  59. const TrackKind = {
  60. /**
  61. * Audio tracks.
  62. * @type string
  63. */
  64. AUDIO: 'audio',
  65. /**
  66. * Video tracks.
  67. * @type string
  68. */
  69. VIDEO: 'video',
  70. /**
  71. * Both audio and video tracks.
  72. * @type string
  73. */
  74. AUDIO_AND_VIDEO: 'av'
  75. };
  76. /**
  77. * @class Resolution
  78. * @memberOf Owt.Base
  79. * @classDesc The Resolution defines the size of a rectangle.
  80. * @constructor
  81. * @param {number} width
  82. * @param {number} height
  83. */
  84. class Resolution {
  85. // eslint-disable-next-line require-jsdoc
  86. constructor(width, height) {
  87. /**
  88. * @member {number} width
  89. * @instance
  90. * @memberof Owt.Base.Resolution
  91. */
  92. this.width = width;
  93. /**
  94. * @member {number} height
  95. * @instance
  96. * @memberof Owt.Base.Resolution
  97. */
  98. this.height = height;
  99. }
  100. }
  101. /*
  102. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  103. *
  104. * Use of this source code is governed by a BSD-style license
  105. * that can be found in the LICENSE file in the root of the source
  106. * tree.
  107. */
  108. let logDisabled_ = true;
  109. let deprecationWarnings_ = true;
  110. /**
  111. * Extract browser version out of the provided user agent string.
  112. *
  113. * @param {!string} uastring userAgent string.
  114. * @param {!string} expr Regular expression used as match criteria.
  115. * @param {!number} pos position in the version string to be returned.
  116. * @return {!number} browser version.
  117. */
  118. function extractVersion(uastring, expr, pos) {
  119. const match = uastring.match(expr);
  120. return match && match.length >= pos && parseInt(match[pos], 10);
  121. }
  122. // Wraps the peerconnection event eventNameToWrap in a function
  123. // which returns the modified event object (or false to prevent
  124. // the event).
  125. function wrapPeerConnectionEvent(window, eventNameToWrap, wrapper) {
  126. if (!window.RTCPeerConnection) {
  127. return;
  128. }
  129. const proto = window.RTCPeerConnection.prototype;
  130. const nativeAddEventListener = proto.addEventListener;
  131. proto.addEventListener = function(nativeEventName, cb) {
  132. if (nativeEventName !== eventNameToWrap) {
  133. return nativeAddEventListener.apply(this, arguments);
  134. }
  135. const wrappedCallback = (e) => {
  136. const modifiedEvent = wrapper(e);
  137. if (modifiedEvent) {
  138. if (cb.handleEvent) {
  139. cb.handleEvent(modifiedEvent);
  140. } else {
  141. cb(modifiedEvent);
  142. }
  143. }
  144. };
  145. this._eventMap = this._eventMap || {};
  146. if (!this._eventMap[eventNameToWrap]) {
  147. this._eventMap[eventNameToWrap] = new Map();
  148. }
  149. this._eventMap[eventNameToWrap].set(cb, wrappedCallback);
  150. return nativeAddEventListener.apply(this, [nativeEventName,
  151. wrappedCallback]);
  152. };
  153. const nativeRemoveEventListener = proto.removeEventListener;
  154. proto.removeEventListener = function(nativeEventName, cb) {
  155. if (nativeEventName !== eventNameToWrap || !this._eventMap
  156. || !this._eventMap[eventNameToWrap]) {
  157. return nativeRemoveEventListener.apply(this, arguments);
  158. }
  159. if (!this._eventMap[eventNameToWrap].has(cb)) {
  160. return nativeRemoveEventListener.apply(this, arguments);
  161. }
  162. const unwrappedCb = this._eventMap[eventNameToWrap].get(cb);
  163. this._eventMap[eventNameToWrap].delete(cb);
  164. if (this._eventMap[eventNameToWrap].size === 0) {
  165. delete this._eventMap[eventNameToWrap];
  166. }
  167. if (Object.keys(this._eventMap).length === 0) {
  168. delete this._eventMap;
  169. }
  170. return nativeRemoveEventListener.apply(this, [nativeEventName,
  171. unwrappedCb]);
  172. };
  173. Object.defineProperty(proto, 'on' + eventNameToWrap, {
  174. get() {
  175. return this['_on' + eventNameToWrap];
  176. },
  177. set(cb) {
  178. if (this['_on' + eventNameToWrap]) {
  179. this.removeEventListener(eventNameToWrap,
  180. this['_on' + eventNameToWrap]);
  181. delete this['_on' + eventNameToWrap];
  182. }
  183. if (cb) {
  184. this.addEventListener(eventNameToWrap,
  185. this['_on' + eventNameToWrap] = cb);
  186. }
  187. },
  188. enumerable: true,
  189. configurable: true
  190. });
  191. }
  192. function disableLog(bool) {
  193. if (typeof bool !== 'boolean') {
  194. return new Error('Argument type: ' + typeof bool +
  195. '. Please use a boolean.');
  196. }
  197. logDisabled_ = bool;
  198. return (bool) ? 'adapter.js logging disabled' :
  199. 'adapter.js logging enabled';
  200. }
  201. /**
  202. * Disable or enable deprecation warnings
  203. * @param {!boolean} bool set to true to disable warnings.
  204. */
  205. function disableWarnings(bool) {
  206. if (typeof bool !== 'boolean') {
  207. return new Error('Argument type: ' + typeof bool +
  208. '. Please use a boolean.');
  209. }
  210. deprecationWarnings_ = !bool;
  211. return 'adapter.js deprecation warnings ' + (bool ? 'disabled' : 'enabled');
  212. }
  213. function log$1() {
  214. if (typeof window === 'object') {
  215. if (logDisabled_) {
  216. return;
  217. }
  218. if (typeof console !== 'undefined' && typeof console.log === 'function') {
  219. console.log.apply(console, arguments);
  220. }
  221. }
  222. }
  223. /**
  224. * Shows a deprecation warning suggesting the modern and spec-compatible API.
  225. */
  226. function deprecated(oldMethod, newMethod) {
  227. if (!deprecationWarnings_) {
  228. return;
  229. }
  230. console.warn(oldMethod + ' is deprecated, please use ' + newMethod +
  231. ' instead.');
  232. }
  233. /**
  234. * Browser detector.
  235. *
  236. * @return {object} result containing browser and version
  237. * properties.
  238. */
  239. function detectBrowser(window) {
  240. // Returned result object.
  241. const result = {browser: null, version: null};
  242. // Fail early if it's not a browser
  243. if (typeof window === 'undefined' || !window.navigator) {
  244. result.browser = 'Not a browser.';
  245. return result;
  246. }
  247. const {navigator} = window;
  248. if (navigator.mozGetUserMedia) { // Firefox.
  249. result.browser = 'firefox';
  250. result.version = extractVersion(navigator.userAgent,
  251. /Firefox\/(\d+)\./, 1);
  252. } else if (navigator.webkitGetUserMedia ||
  253. (window.isSecureContext === false && window.webkitRTCPeerConnection &&
  254. !window.RTCIceGatherer)) {
  255. // Chrome, Chromium, Webview, Opera.
  256. // Version matches Chrome/WebRTC version.
  257. // Chrome 74 removed webkitGetUserMedia on http as well so we need the
  258. // more complicated fallback to webkitRTCPeerConnection.
  259. result.browser = 'chrome';
  260. result.version = extractVersion(navigator.userAgent,
  261. /Chrom(e|ium)\/(\d+)\./, 2);
  262. } else if (navigator.mediaDevices &&
  263. navigator.userAgent.match(/Edge\/(\d+).(\d+)$/)) { // Edge.
  264. result.browser = 'edge';
  265. result.version = extractVersion(navigator.userAgent,
  266. /Edge\/(\d+).(\d+)$/, 2);
  267. } else if (window.RTCPeerConnection &&
  268. navigator.userAgent.match(/AppleWebKit\/(\d+)\./)) { // Safari.
  269. result.browser = 'safari';
  270. result.version = extractVersion(navigator.userAgent,
  271. /AppleWebKit\/(\d+)\./, 1);
  272. result.supportsUnifiedPlan = window.RTCRtpTransceiver &&
  273. 'currentDirection' in window.RTCRtpTransceiver.prototype;
  274. } else { // Default fallthrough: not supported.
  275. result.browser = 'Not a supported browser.';
  276. return result;
  277. }
  278. return result;
  279. }
  280. /**
  281. * Checks if something is an object.
  282. *
  283. * @param {*} val The something you want to check.
  284. * @return true if val is an object, false otherwise.
  285. */
  286. function isObject$1(val) {
  287. return Object.prototype.toString.call(val) === '[object Object]';
  288. }
  289. /**
  290. * Remove all empty objects and undefined values
  291. * from a nested object -- an enhanced and vanilla version
  292. * of Lodash's `compact`.
  293. */
  294. function compactObject(data) {
  295. if (!isObject$1(data)) {
  296. return data;
  297. }
  298. return Object.keys(data).reduce(function(accumulator, key) {
  299. const isObj = isObject$1(data[key]);
  300. const value = isObj ? compactObject(data[key]) : data[key];
  301. const isEmptyObject = isObj && !Object.keys(value).length;
  302. if (value === undefined || isEmptyObject) {
  303. return accumulator;
  304. }
  305. return Object.assign(accumulator, {[key]: value});
  306. }, {});
  307. }
  308. /* iterates the stats graph recursively. */
  309. function walkStats(stats, base, resultSet) {
  310. if (!base || resultSet.has(base.id)) {
  311. return;
  312. }
  313. resultSet.set(base.id, base);
  314. Object.keys(base).forEach(name => {
  315. if (name.endsWith('Id')) {
  316. walkStats(stats, stats.get(base[name]), resultSet);
  317. } else if (name.endsWith('Ids')) {
  318. base[name].forEach(id => {
  319. walkStats(stats, stats.get(id), resultSet);
  320. });
  321. }
  322. });
  323. }
  324. /* filter getStats for a sender/receiver track. */
  325. function filterStats(result, track, outbound) {
  326. const streamStatsType = outbound ? 'outbound-rtp' : 'inbound-rtp';
  327. const filteredResult = new Map();
  328. if (track === null) {
  329. return filteredResult;
  330. }
  331. const trackStats = [];
  332. result.forEach(value => {
  333. if (value.type === 'track' &&
  334. value.trackIdentifier === track.id) {
  335. trackStats.push(value);
  336. }
  337. });
  338. trackStats.forEach(trackStat => {
  339. result.forEach(stats => {
  340. if (stats.type === streamStatsType && stats.trackId === trackStat.id) {
  341. walkStats(result, stats, filteredResult);
  342. }
  343. });
  344. });
  345. return filteredResult;
  346. }
  347. /*
  348. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  349. *
  350. * Use of this source code is governed by a BSD-style license
  351. * that can be found in the LICENSE file in the root of the source
  352. * tree.
  353. */
  354. const logging = log$1;
  355. function shimGetUserMedia$3(window, browserDetails) {
  356. const navigator = window && window.navigator;
  357. if (!navigator.mediaDevices) {
  358. return;
  359. }
  360. const constraintsToChrome_ = function(c) {
  361. if (typeof c !== 'object' || c.mandatory || c.optional) {
  362. return c;
  363. }
  364. const cc = {};
  365. Object.keys(c).forEach(key => {
  366. if (key === 'require' || key === 'advanced' || key === 'mediaSource') {
  367. return;
  368. }
  369. const r = (typeof c[key] === 'object') ? c[key] : {ideal: c[key]};
  370. if (r.exact !== undefined && typeof r.exact === 'number') {
  371. r.min = r.max = r.exact;
  372. }
  373. const oldname_ = function(prefix, name) {
  374. if (prefix) {
  375. return prefix + name.charAt(0).toUpperCase() + name.slice(1);
  376. }
  377. return (name === 'deviceId') ? 'sourceId' : name;
  378. };
  379. if (r.ideal !== undefined) {
  380. cc.optional = cc.optional || [];
  381. let oc = {};
  382. if (typeof r.ideal === 'number') {
  383. oc[oldname_('min', key)] = r.ideal;
  384. cc.optional.push(oc);
  385. oc = {};
  386. oc[oldname_('max', key)] = r.ideal;
  387. cc.optional.push(oc);
  388. } else {
  389. oc[oldname_('', key)] = r.ideal;
  390. cc.optional.push(oc);
  391. }
  392. }
  393. if (r.exact !== undefined && typeof r.exact !== 'number') {
  394. cc.mandatory = cc.mandatory || {};
  395. cc.mandatory[oldname_('', key)] = r.exact;
  396. } else {
  397. ['min', 'max'].forEach(mix => {
  398. if (r[mix] !== undefined) {
  399. cc.mandatory = cc.mandatory || {};
  400. cc.mandatory[oldname_(mix, key)] = r[mix];
  401. }
  402. });
  403. }
  404. });
  405. if (c.advanced) {
  406. cc.optional = (cc.optional || []).concat(c.advanced);
  407. }
  408. return cc;
  409. };
  410. const shimConstraints_ = function(constraints, func) {
  411. if (browserDetails.version >= 61) {
  412. return func(constraints);
  413. }
  414. constraints = JSON.parse(JSON.stringify(constraints));
  415. if (constraints && typeof constraints.audio === 'object') {
  416. const remap = function(obj, a, b) {
  417. if (a in obj && !(b in obj)) {
  418. obj[b] = obj[a];
  419. delete obj[a];
  420. }
  421. };
  422. constraints = JSON.parse(JSON.stringify(constraints));
  423. remap(constraints.audio, 'autoGainControl', 'googAutoGainControl');
  424. remap(constraints.audio, 'noiseSuppression', 'googNoiseSuppression');
  425. constraints.audio = constraintsToChrome_(constraints.audio);
  426. }
  427. if (constraints && typeof constraints.video === 'object') {
  428. // Shim facingMode for mobile & surface pro.
  429. let face = constraints.video.facingMode;
  430. face = face && ((typeof face === 'object') ? face : {ideal: face});
  431. const getSupportedFacingModeLies = browserDetails.version < 66;
  432. if ((face && (face.exact === 'user' || face.exact === 'environment' ||
  433. face.ideal === 'user' || face.ideal === 'environment')) &&
  434. !(navigator.mediaDevices.getSupportedConstraints &&
  435. navigator.mediaDevices.getSupportedConstraints().facingMode &&
  436. !getSupportedFacingModeLies)) {
  437. delete constraints.video.facingMode;
  438. let matches;
  439. if (face.exact === 'environment' || face.ideal === 'environment') {
  440. matches = ['back', 'rear'];
  441. } else if (face.exact === 'user' || face.ideal === 'user') {
  442. matches = ['front'];
  443. }
  444. if (matches) {
  445. // Look for matches in label, or use last cam for back (typical).
  446. return navigator.mediaDevices.enumerateDevices()
  447. .then(devices => {
  448. devices = devices.filter(d => d.kind === 'videoinput');
  449. let dev = devices.find(d => matches.some(match =>
  450. d.label.toLowerCase().includes(match)));
  451. if (!dev && devices.length && matches.includes('back')) {
  452. dev = devices[devices.length - 1]; // more likely the back cam
  453. }
  454. if (dev) {
  455. constraints.video.deviceId = face.exact ? {exact: dev.deviceId} :
  456. {ideal: dev.deviceId};
  457. }
  458. constraints.video = constraintsToChrome_(constraints.video);
  459. logging('chrome: ' + JSON.stringify(constraints));
  460. return func(constraints);
  461. });
  462. }
  463. }
  464. constraints.video = constraintsToChrome_(constraints.video);
  465. }
  466. logging('chrome: ' + JSON.stringify(constraints));
  467. return func(constraints);
  468. };
  469. const shimError_ = function(e) {
  470. if (browserDetails.version >= 64) {
  471. return e;
  472. }
  473. return {
  474. name: {
  475. PermissionDeniedError: 'NotAllowedError',
  476. PermissionDismissedError: 'NotAllowedError',
  477. InvalidStateError: 'NotAllowedError',
  478. DevicesNotFoundError: 'NotFoundError',
  479. ConstraintNotSatisfiedError: 'OverconstrainedError',
  480. TrackStartError: 'NotReadableError',
  481. MediaDeviceFailedDueToShutdown: 'NotAllowedError',
  482. MediaDeviceKillSwitchOn: 'NotAllowedError',
  483. TabCaptureError: 'AbortError',
  484. ScreenCaptureError: 'AbortError',
  485. DeviceCaptureError: 'AbortError'
  486. }[e.name] || e.name,
  487. message: e.message,
  488. constraint: e.constraint || e.constraintName,
  489. toString() {
  490. return this.name + (this.message && ': ') + this.message;
  491. }
  492. };
  493. };
  494. const getUserMedia_ = function(constraints, onSuccess, onError) {
  495. shimConstraints_(constraints, c => {
  496. navigator.webkitGetUserMedia(c, onSuccess, e => {
  497. if (onError) {
  498. onError(shimError_(e));
  499. }
  500. });
  501. });
  502. };
  503. navigator.getUserMedia = getUserMedia_.bind(navigator);
  504. // Even though Chrome 45 has navigator.mediaDevices and a getUserMedia
  505. // function which returns a Promise, it does not accept spec-style
  506. // constraints.
  507. if (navigator.mediaDevices.getUserMedia) {
  508. const origGetUserMedia = navigator.mediaDevices.getUserMedia.
  509. bind(navigator.mediaDevices);
  510. navigator.mediaDevices.getUserMedia = function(cs) {
  511. return shimConstraints_(cs, c => origGetUserMedia(c).then(stream => {
  512. if (c.audio && !stream.getAudioTracks().length ||
  513. c.video && !stream.getVideoTracks().length) {
  514. stream.getTracks().forEach(track => {
  515. track.stop();
  516. });
  517. throw new DOMException('', 'NotFoundError');
  518. }
  519. return stream;
  520. }, e => Promise.reject(shimError_(e))));
  521. };
  522. }
  523. }
  524. /*
  525. * Copyright (c) 2018 The adapter.js project authors. All Rights Reserved.
  526. *
  527. * Use of this source code is governed by a BSD-style license
  528. * that can be found in the LICENSE file in the root of the source
  529. * tree.
  530. */
  531. function shimGetDisplayMedia$2(window, getSourceId) {
  532. if (window.navigator.mediaDevices &&
  533. 'getDisplayMedia' in window.navigator.mediaDevices) {
  534. return;
  535. }
  536. if (!(window.navigator.mediaDevices)) {
  537. return;
  538. }
  539. // getSourceId is a function that returns a promise resolving with
  540. // the sourceId of the screen/window/tab to be shared.
  541. if (typeof getSourceId !== 'function') {
  542. console.error('shimGetDisplayMedia: getSourceId argument is not ' +
  543. 'a function');
  544. return;
  545. }
  546. window.navigator.mediaDevices.getDisplayMedia =
  547. function getDisplayMedia(constraints) {
  548. return getSourceId(constraints)
  549. .then(sourceId => {
  550. const widthSpecified = constraints.video && constraints.video.width;
  551. const heightSpecified = constraints.video &&
  552. constraints.video.height;
  553. const frameRateSpecified = constraints.video &&
  554. constraints.video.frameRate;
  555. constraints.video = {
  556. mandatory: {
  557. chromeMediaSource: 'desktop',
  558. chromeMediaSourceId: sourceId,
  559. maxFrameRate: frameRateSpecified || 3
  560. }
  561. };
  562. if (widthSpecified) {
  563. constraints.video.mandatory.maxWidth = widthSpecified;
  564. }
  565. if (heightSpecified) {
  566. constraints.video.mandatory.maxHeight = heightSpecified;
  567. }
  568. return window.navigator.mediaDevices.getUserMedia(constraints);
  569. });
  570. };
  571. }
  572. /*
  573. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  574. *
  575. * Use of this source code is governed by a BSD-style license
  576. * that can be found in the LICENSE file in the root of the source
  577. * tree.
  578. */
  579. function shimMediaStream(window) {
  580. window.MediaStream = window.MediaStream || window.webkitMediaStream;
  581. }
  582. function shimOnTrack$1(window) {
  583. if (typeof window === 'object' && window.RTCPeerConnection && !('ontrack' in
  584. window.RTCPeerConnection.prototype)) {
  585. Object.defineProperty(window.RTCPeerConnection.prototype, 'ontrack', {
  586. get() {
  587. return this._ontrack;
  588. },
  589. set(f) {
  590. if (this._ontrack) {
  591. this.removeEventListener('track', this._ontrack);
  592. }
  593. this.addEventListener('track', this._ontrack = f);
  594. },
  595. enumerable: true,
  596. configurable: true
  597. });
  598. const origSetRemoteDescription =
  599. window.RTCPeerConnection.prototype.setRemoteDescription;
  600. window.RTCPeerConnection.prototype.setRemoteDescription =
  601. function setRemoteDescription() {
  602. if (!this._ontrackpoly) {
  603. this._ontrackpoly = (e) => {
  604. // onaddstream does not fire when a track is added to an existing
  605. // stream. But stream.onaddtrack is implemented so we use that.
  606. e.stream.addEventListener('addtrack', te => {
  607. let receiver;
  608. if (window.RTCPeerConnection.prototype.getReceivers) {
  609. receiver = this.getReceivers()
  610. .find(r => r.track && r.track.id === te.track.id);
  611. } else {
  612. receiver = {track: te.track};
  613. }
  614. const event = new Event('track');
  615. event.track = te.track;
  616. event.receiver = receiver;
  617. event.transceiver = {receiver};
  618. event.streams = [e.stream];
  619. this.dispatchEvent(event);
  620. });
  621. e.stream.getTracks().forEach(track => {
  622. let receiver;
  623. if (window.RTCPeerConnection.prototype.getReceivers) {
  624. receiver = this.getReceivers()
  625. .find(r => r.track && r.track.id === track.id);
  626. } else {
  627. receiver = {track};
  628. }
  629. const event = new Event('track');
  630. event.track = track;
  631. event.receiver = receiver;
  632. event.transceiver = {receiver};
  633. event.streams = [e.stream];
  634. this.dispatchEvent(event);
  635. });
  636. };
  637. this.addEventListener('addstream', this._ontrackpoly);
  638. }
  639. return origSetRemoteDescription.apply(this, arguments);
  640. };
  641. } else {
  642. // even if RTCRtpTransceiver is in window, it is only used and
  643. // emitted in unified-plan. Unfortunately this means we need
  644. // to unconditionally wrap the event.
  645. wrapPeerConnectionEvent(window, 'track', e => {
  646. if (!e.transceiver) {
  647. Object.defineProperty(e, 'transceiver',
  648. {value: {receiver: e.receiver}});
  649. }
  650. return e;
  651. });
  652. }
  653. }
  654. function shimGetSendersWithDtmf(window) {
  655. // Overrides addTrack/removeTrack, depends on shimAddTrackRemoveTrack.
  656. if (typeof window === 'object' && window.RTCPeerConnection &&
  657. !('getSenders' in window.RTCPeerConnection.prototype) &&
  658. 'createDTMFSender' in window.RTCPeerConnection.prototype) {
  659. const shimSenderWithDtmf = function(pc, track) {
  660. return {
  661. track,
  662. get dtmf() {
  663. if (this._dtmf === undefined) {
  664. if (track.kind === 'audio') {
  665. this._dtmf = pc.createDTMFSender(track);
  666. } else {
  667. this._dtmf = null;
  668. }
  669. }
  670. return this._dtmf;
  671. },
  672. _pc: pc
  673. };
  674. };
  675. // augment addTrack when getSenders is not available.
  676. if (!window.RTCPeerConnection.prototype.getSenders) {
  677. window.RTCPeerConnection.prototype.getSenders = function getSenders() {
  678. this._senders = this._senders || [];
  679. return this._senders.slice(); // return a copy of the internal state.
  680. };
  681. const origAddTrack = window.RTCPeerConnection.prototype.addTrack;
  682. window.RTCPeerConnection.prototype.addTrack =
  683. function addTrack(track, stream) {
  684. let sender = origAddTrack.apply(this, arguments);
  685. if (!sender) {
  686. sender = shimSenderWithDtmf(this, track);
  687. this._senders.push(sender);
  688. }
  689. return sender;
  690. };
  691. const origRemoveTrack = window.RTCPeerConnection.prototype.removeTrack;
  692. window.RTCPeerConnection.prototype.removeTrack =
  693. function removeTrack(sender) {
  694. origRemoveTrack.apply(this, arguments);
  695. const idx = this._senders.indexOf(sender);
  696. if (idx !== -1) {
  697. this._senders.splice(idx, 1);
  698. }
  699. };
  700. }
  701. const origAddStream = window.RTCPeerConnection.prototype.addStream;
  702. window.RTCPeerConnection.prototype.addStream = function addStream(stream) {
  703. this._senders = this._senders || [];
  704. origAddStream.apply(this, [stream]);
  705. stream.getTracks().forEach(track => {
  706. this._senders.push(shimSenderWithDtmf(this, track));
  707. });
  708. };
  709. const origRemoveStream = window.RTCPeerConnection.prototype.removeStream;
  710. window.RTCPeerConnection.prototype.removeStream =
  711. function removeStream(stream) {
  712. this._senders = this._senders || [];
  713. origRemoveStream.apply(this, [stream]);
  714. stream.getTracks().forEach(track => {
  715. const sender = this._senders.find(s => s.track === track);
  716. if (sender) { // remove sender
  717. this._senders.splice(this._senders.indexOf(sender), 1);
  718. }
  719. });
  720. };
  721. } else if (typeof window === 'object' && window.RTCPeerConnection &&
  722. 'getSenders' in window.RTCPeerConnection.prototype &&
  723. 'createDTMFSender' in window.RTCPeerConnection.prototype &&
  724. window.RTCRtpSender &&
  725. !('dtmf' in window.RTCRtpSender.prototype)) {
  726. const origGetSenders = window.RTCPeerConnection.prototype.getSenders;
  727. window.RTCPeerConnection.prototype.getSenders = function getSenders() {
  728. const senders = origGetSenders.apply(this, []);
  729. senders.forEach(sender => sender._pc = this);
  730. return senders;
  731. };
  732. Object.defineProperty(window.RTCRtpSender.prototype, 'dtmf', {
  733. get() {
  734. if (this._dtmf === undefined) {
  735. if (this.track.kind === 'audio') {
  736. this._dtmf = this._pc.createDTMFSender(this.track);
  737. } else {
  738. this._dtmf = null;
  739. }
  740. }
  741. return this._dtmf;
  742. }
  743. });
  744. }
  745. }
  746. function shimGetStats(window) {
  747. if (!window.RTCPeerConnection) {
  748. return;
  749. }
  750. const origGetStats = window.RTCPeerConnection.prototype.getStats;
  751. window.RTCPeerConnection.prototype.getStats = function getStats() {
  752. const [selector, onSucc, onErr] = arguments;
  753. // If selector is a function then we are in the old style stats so just
  754. // pass back the original getStats format to avoid breaking old users.
  755. if (arguments.length > 0 && typeof selector === 'function') {
  756. return origGetStats.apply(this, arguments);
  757. }
  758. // When spec-style getStats is supported, return those when called with
  759. // either no arguments or the selector argument is null.
  760. if (origGetStats.length === 0 && (arguments.length === 0 ||
  761. typeof selector !== 'function')) {
  762. return origGetStats.apply(this, []);
  763. }
  764. const fixChromeStats_ = function(response) {
  765. const standardReport = {};
  766. const reports = response.result();
  767. reports.forEach(report => {
  768. const standardStats = {
  769. id: report.id,
  770. timestamp: report.timestamp,
  771. type: {
  772. localcandidate: 'local-candidate',
  773. remotecandidate: 'remote-candidate'
  774. }[report.type] || report.type
  775. };
  776. report.names().forEach(name => {
  777. standardStats[name] = report.stat(name);
  778. });
  779. standardReport[standardStats.id] = standardStats;
  780. });
  781. return standardReport;
  782. };
  783. // shim getStats with maplike support
  784. const makeMapStats = function(stats) {
  785. return new Map(Object.keys(stats).map(key => [key, stats[key]]));
  786. };
  787. if (arguments.length >= 2) {
  788. const successCallbackWrapper_ = function(response) {
  789. onSucc(makeMapStats(fixChromeStats_(response)));
  790. };
  791. return origGetStats.apply(this, [successCallbackWrapper_,
  792. selector]);
  793. }
  794. // promise-support
  795. return new Promise((resolve, reject) => {
  796. origGetStats.apply(this, [
  797. function(response) {
  798. resolve(makeMapStats(fixChromeStats_(response)));
  799. }, reject]);
  800. }).then(onSucc, onErr);
  801. };
  802. }
  803. function shimSenderReceiverGetStats(window) {
  804. if (!(typeof window === 'object' && window.RTCPeerConnection &&
  805. window.RTCRtpSender && window.RTCRtpReceiver)) {
  806. return;
  807. }
  808. // shim sender stats.
  809. if (!('getStats' in window.RTCRtpSender.prototype)) {
  810. const origGetSenders = window.RTCPeerConnection.prototype.getSenders;
  811. if (origGetSenders) {
  812. window.RTCPeerConnection.prototype.getSenders = function getSenders() {
  813. const senders = origGetSenders.apply(this, []);
  814. senders.forEach(sender => sender._pc = this);
  815. return senders;
  816. };
  817. }
  818. const origAddTrack = window.RTCPeerConnection.prototype.addTrack;
  819. if (origAddTrack) {
  820. window.RTCPeerConnection.prototype.addTrack = function addTrack() {
  821. const sender = origAddTrack.apply(this, arguments);
  822. sender._pc = this;
  823. return sender;
  824. };
  825. }
  826. window.RTCRtpSender.prototype.getStats = function getStats() {
  827. const sender = this;
  828. return this._pc.getStats().then(result =>
  829. /* Note: this will include stats of all senders that
  830. * send a track with the same id as sender.track as
  831. * it is not possible to identify the RTCRtpSender.
  832. */
  833. filterStats(result, sender.track, true));
  834. };
  835. }
  836. // shim receiver stats.
  837. if (!('getStats' in window.RTCRtpReceiver.prototype)) {
  838. const origGetReceivers = window.RTCPeerConnection.prototype.getReceivers;
  839. if (origGetReceivers) {
  840. window.RTCPeerConnection.prototype.getReceivers =
  841. function getReceivers() {
  842. const receivers = origGetReceivers.apply(this, []);
  843. receivers.forEach(receiver => receiver._pc = this);
  844. return receivers;
  845. };
  846. }
  847. wrapPeerConnectionEvent(window, 'track', e => {
  848. e.receiver._pc = e.srcElement;
  849. return e;
  850. });
  851. window.RTCRtpReceiver.prototype.getStats = function getStats() {
  852. const receiver = this;
  853. return this._pc.getStats().then(result =>
  854. filterStats(result, receiver.track, false));
  855. };
  856. }
  857. if (!('getStats' in window.RTCRtpSender.prototype &&
  858. 'getStats' in window.RTCRtpReceiver.prototype)) {
  859. return;
  860. }
  861. // shim RTCPeerConnection.getStats(track).
  862. const origGetStats = window.RTCPeerConnection.prototype.getStats;
  863. window.RTCPeerConnection.prototype.getStats = function getStats() {
  864. if (arguments.length > 0 &&
  865. arguments[0] instanceof window.MediaStreamTrack) {
  866. const track = arguments[0];
  867. let sender;
  868. let receiver;
  869. let err;
  870. this.getSenders().forEach(s => {
  871. if (s.track === track) {
  872. if (sender) {
  873. err = true;
  874. } else {
  875. sender = s;
  876. }
  877. }
  878. });
  879. this.getReceivers().forEach(r => {
  880. if (r.track === track) {
  881. if (receiver) {
  882. err = true;
  883. } else {
  884. receiver = r;
  885. }
  886. }
  887. return r.track === track;
  888. });
  889. if (err || (sender && receiver)) {
  890. return Promise.reject(new DOMException(
  891. 'There are more than one sender or receiver for the track.',
  892. 'InvalidAccessError'));
  893. } else if (sender) {
  894. return sender.getStats();
  895. } else if (receiver) {
  896. return receiver.getStats();
  897. }
  898. return Promise.reject(new DOMException(
  899. 'There is no sender or receiver for the track.',
  900. 'InvalidAccessError'));
  901. }
  902. return origGetStats.apply(this, arguments);
  903. };
  904. }
  905. function shimAddTrackRemoveTrackWithNative(window) {
  906. // shim addTrack/removeTrack with native variants in order to make
  907. // the interactions with legacy getLocalStreams behave as in other browsers.
  908. // Keeps a mapping stream.id => [stream, rtpsenders...]
  909. window.RTCPeerConnection.prototype.getLocalStreams =
  910. function getLocalStreams() {
  911. this._shimmedLocalStreams = this._shimmedLocalStreams || {};
  912. return Object.keys(this._shimmedLocalStreams)
  913. .map(streamId => this._shimmedLocalStreams[streamId][0]);
  914. };
  915. const origAddTrack = window.RTCPeerConnection.prototype.addTrack;
  916. window.RTCPeerConnection.prototype.addTrack =
  917. function addTrack(track, stream) {
  918. if (!stream) {
  919. return origAddTrack.apply(this, arguments);
  920. }
  921. this._shimmedLocalStreams = this._shimmedLocalStreams || {};
  922. const sender = origAddTrack.apply(this, arguments);
  923. if (!this._shimmedLocalStreams[stream.id]) {
  924. this._shimmedLocalStreams[stream.id] = [stream, sender];
  925. } else if (this._shimmedLocalStreams[stream.id].indexOf(sender) === -1) {
  926. this._shimmedLocalStreams[stream.id].push(sender);
  927. }
  928. return sender;
  929. };
  930. const origAddStream = window.RTCPeerConnection.prototype.addStream;
  931. window.RTCPeerConnection.prototype.addStream = function addStream(stream) {
  932. this._shimmedLocalStreams = this._shimmedLocalStreams || {};
  933. stream.getTracks().forEach(track => {
  934. const alreadyExists = this.getSenders().find(s => s.track === track);
  935. if (alreadyExists) {
  936. throw new DOMException('Track already exists.',
  937. 'InvalidAccessError');
  938. }
  939. });
  940. const existingSenders = this.getSenders();
  941. origAddStream.apply(this, arguments);
  942. const newSenders = this.getSenders()
  943. .filter(newSender => existingSenders.indexOf(newSender) === -1);
  944. this._shimmedLocalStreams[stream.id] = [stream].concat(newSenders);
  945. };
  946. const origRemoveStream = window.RTCPeerConnection.prototype.removeStream;
  947. window.RTCPeerConnection.prototype.removeStream =
  948. function removeStream(stream) {
  949. this._shimmedLocalStreams = this._shimmedLocalStreams || {};
  950. delete this._shimmedLocalStreams[stream.id];
  951. return origRemoveStream.apply(this, arguments);
  952. };
  953. const origRemoveTrack = window.RTCPeerConnection.prototype.removeTrack;
  954. window.RTCPeerConnection.prototype.removeTrack =
  955. function removeTrack(sender) {
  956. this._shimmedLocalStreams = this._shimmedLocalStreams || {};
  957. if (sender) {
  958. Object.keys(this._shimmedLocalStreams).forEach(streamId => {
  959. const idx = this._shimmedLocalStreams[streamId].indexOf(sender);
  960. if (idx !== -1) {
  961. this._shimmedLocalStreams[streamId].splice(idx, 1);
  962. }
  963. if (this._shimmedLocalStreams[streamId].length === 1) {
  964. delete this._shimmedLocalStreams[streamId];
  965. }
  966. });
  967. }
  968. return origRemoveTrack.apply(this, arguments);
  969. };
  970. }
  971. function shimAddTrackRemoveTrack(window, browserDetails) {
  972. if (!window.RTCPeerConnection) {
  973. return;
  974. }
  975. // shim addTrack and removeTrack.
  976. if (window.RTCPeerConnection.prototype.addTrack &&
  977. browserDetails.version >= 65) {
  978. return shimAddTrackRemoveTrackWithNative(window);
  979. }
  980. // also shim pc.getLocalStreams when addTrack is shimmed
  981. // to return the original streams.
  982. const origGetLocalStreams = window.RTCPeerConnection.prototype
  983. .getLocalStreams;
  984. window.RTCPeerConnection.prototype.getLocalStreams =
  985. function getLocalStreams() {
  986. const nativeStreams = origGetLocalStreams.apply(this);
  987. this._reverseStreams = this._reverseStreams || {};
  988. return nativeStreams.map(stream => this._reverseStreams[stream.id]);
  989. };
  990. const origAddStream = window.RTCPeerConnection.prototype.addStream;
  991. window.RTCPeerConnection.prototype.addStream = function addStream(stream) {
  992. this._streams = this._streams || {};
  993. this._reverseStreams = this._reverseStreams || {};
  994. stream.getTracks().forEach(track => {
  995. const alreadyExists = this.getSenders().find(s => s.track === track);
  996. if (alreadyExists) {
  997. throw new DOMException('Track already exists.',
  998. 'InvalidAccessError');
  999. }
  1000. });
  1001. // Add identity mapping for consistency with addTrack.
  1002. // Unless this is being used with a stream from addTrack.
  1003. if (!this._reverseStreams[stream.id]) {
  1004. const newStream = new window.MediaStream(stream.getTracks());
  1005. this._streams[stream.id] = newStream;
  1006. this._reverseStreams[newStream.id] = stream;
  1007. stream = newStream;
  1008. }
  1009. origAddStream.apply(this, [stream]);
  1010. };
  1011. const origRemoveStream = window.RTCPeerConnection.prototype.removeStream;
  1012. window.RTCPeerConnection.prototype.removeStream =
  1013. function removeStream(stream) {
  1014. this._streams = this._streams || {};
  1015. this._reverseStreams = this._reverseStreams || {};
  1016. origRemoveStream.apply(this, [(this._streams[stream.id] || stream)]);
  1017. delete this._reverseStreams[(this._streams[stream.id] ?
  1018. this._streams[stream.id].id : stream.id)];
  1019. delete this._streams[stream.id];
  1020. };
  1021. window.RTCPeerConnection.prototype.addTrack =
  1022. function addTrack(track, stream) {
  1023. if (this.signalingState === 'closed') {
  1024. throw new DOMException(
  1025. 'The RTCPeerConnection\'s signalingState is \'closed\'.',
  1026. 'InvalidStateError');
  1027. }
  1028. const streams = [].slice.call(arguments, 1);
  1029. if (streams.length !== 1 ||
  1030. !streams[0].getTracks().find(t => t === track)) {
  1031. // this is not fully correct but all we can manage without
  1032. // [[associated MediaStreams]] internal slot.
  1033. throw new DOMException(
  1034. 'The adapter.js addTrack polyfill only supports a single ' +
  1035. ' stream which is associated with the specified track.',
  1036. 'NotSupportedError');
  1037. }
  1038. const alreadyExists = this.getSenders().find(s => s.track === track);
  1039. if (alreadyExists) {
  1040. throw new DOMException('Track already exists.',
  1041. 'InvalidAccessError');
  1042. }
  1043. this._streams = this._streams || {};
  1044. this._reverseStreams = this._reverseStreams || {};
  1045. const oldStream = this._streams[stream.id];
  1046. if (oldStream) {
  1047. // this is using odd Chrome behaviour, use with caution:
  1048. // https://bugs.chromium.org/p/webrtc/issues/detail?id=7815
  1049. // Note: we rely on the high-level addTrack/dtmf shim to
  1050. // create the sender with a dtmf sender.
  1051. oldStream.addTrack(track);
  1052. // Trigger ONN async.
  1053. Promise.resolve().then(() => {
  1054. this.dispatchEvent(new Event('negotiationneeded'));
  1055. });
  1056. } else {
  1057. const newStream = new window.MediaStream([track]);
  1058. this._streams[stream.id] = newStream;
  1059. this._reverseStreams[newStream.id] = stream;
  1060. this.addStream(newStream);
  1061. }
  1062. return this.getSenders().find(s => s.track === track);
  1063. };
  1064. // replace the internal stream id with the external one and
  1065. // vice versa.
  1066. function replaceInternalStreamId(pc, description) {
  1067. let sdp = description.sdp;
  1068. Object.keys(pc._reverseStreams || []).forEach(internalId => {
  1069. const externalStream = pc._reverseStreams[internalId];
  1070. const internalStream = pc._streams[externalStream.id];
  1071. sdp = sdp.replace(new RegExp(internalStream.id, 'g'),
  1072. externalStream.id);
  1073. });
  1074. return new RTCSessionDescription({
  1075. type: description.type,
  1076. sdp
  1077. });
  1078. }
  1079. function replaceExternalStreamId(pc, description) {
  1080. let sdp = description.sdp;
  1081. Object.keys(pc._reverseStreams || []).forEach(internalId => {
  1082. const externalStream = pc._reverseStreams[internalId];
  1083. const internalStream = pc._streams[externalStream.id];
  1084. sdp = sdp.replace(new RegExp(externalStream.id, 'g'),
  1085. internalStream.id);
  1086. });
  1087. return new RTCSessionDescription({
  1088. type: description.type,
  1089. sdp
  1090. });
  1091. }
  1092. ['createOffer', 'createAnswer'].forEach(function(method) {
  1093. const nativeMethod = window.RTCPeerConnection.prototype[method];
  1094. const methodObj = {[method]() {
  1095. const args = arguments;
  1096. const isLegacyCall = arguments.length &&
  1097. typeof arguments[0] === 'function';
  1098. if (isLegacyCall) {
  1099. return nativeMethod.apply(this, [
  1100. (description) => {
  1101. const desc = replaceInternalStreamId(this, description);
  1102. args[0].apply(null, [desc]);
  1103. },
  1104. (err) => {
  1105. if (args[1]) {
  1106. args[1].apply(null, err);
  1107. }
  1108. }, arguments[2]
  1109. ]);
  1110. }
  1111. return nativeMethod.apply(this, arguments)
  1112. .then(description => replaceInternalStreamId(this, description));
  1113. }};
  1114. window.RTCPeerConnection.prototype[method] = methodObj[method];
  1115. });
  1116. const origSetLocalDescription =
  1117. window.RTCPeerConnection.prototype.setLocalDescription;
  1118. window.RTCPeerConnection.prototype.setLocalDescription =
  1119. function setLocalDescription() {
  1120. if (!arguments.length || !arguments[0].type) {
  1121. return origSetLocalDescription.apply(this, arguments);
  1122. }
  1123. arguments[0] = replaceExternalStreamId(this, arguments[0]);
  1124. return origSetLocalDescription.apply(this, arguments);
  1125. };
  1126. // TODO: mangle getStats: https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamstats-streamidentifier
  1127. const origLocalDescription = Object.getOwnPropertyDescriptor(
  1128. window.RTCPeerConnection.prototype, 'localDescription');
  1129. Object.defineProperty(window.RTCPeerConnection.prototype,
  1130. 'localDescription', {
  1131. get() {
  1132. const description = origLocalDescription.get.apply(this);
  1133. if (description.type === '') {
  1134. return description;
  1135. }
  1136. return replaceInternalStreamId(this, description);
  1137. }
  1138. });
  1139. window.RTCPeerConnection.prototype.removeTrack =
  1140. function removeTrack(sender) {
  1141. if (this.signalingState === 'closed') {
  1142. throw new DOMException(
  1143. 'The RTCPeerConnection\'s signalingState is \'closed\'.',
  1144. 'InvalidStateError');
  1145. }
  1146. // We can not yet check for sender instanceof RTCRtpSender
  1147. // since we shim RTPSender. So we check if sender._pc is set.
  1148. if (!sender._pc) {
  1149. throw new DOMException('Argument 1 of RTCPeerConnection.removeTrack ' +
  1150. 'does not implement interface RTCRtpSender.', 'TypeError');
  1151. }
  1152. const isLocal = sender._pc === this;
  1153. if (!isLocal) {
  1154. throw new DOMException('Sender was not created by this connection.',
  1155. 'InvalidAccessError');
  1156. }
  1157. // Search for the native stream the senders track belongs to.
  1158. this._streams = this._streams || {};
  1159. let stream;
  1160. Object.keys(this._streams).forEach(streamid => {
  1161. const hasTrack = this._streams[streamid].getTracks()
  1162. .find(track => sender.track === track);
  1163. if (hasTrack) {
  1164. stream = this._streams[streamid];
  1165. }
  1166. });
  1167. if (stream) {
  1168. if (stream.getTracks().length === 1) {
  1169. // if this is the last track of the stream, remove the stream. This
  1170. // takes care of any shimmed _senders.
  1171. this.removeStream(this._reverseStreams[stream.id]);
  1172. } else {
  1173. // relying on the same odd chrome behaviour as above.
  1174. stream.removeTrack(sender.track);
  1175. }
  1176. this.dispatchEvent(new Event('negotiationneeded'));
  1177. }
  1178. };
  1179. }
  1180. function shimPeerConnection$2(window, browserDetails) {
  1181. if (!window.RTCPeerConnection && window.webkitRTCPeerConnection) {
  1182. // very basic support for old versions.
  1183. window.RTCPeerConnection = window.webkitRTCPeerConnection;
  1184. }
  1185. if (!window.RTCPeerConnection) {
  1186. return;
  1187. }
  1188. // shim implicit creation of RTCSessionDescription/RTCIceCandidate
  1189. if (browserDetails.version < 53) {
  1190. ['setLocalDescription', 'setRemoteDescription', 'addIceCandidate']
  1191. .forEach(function(method) {
  1192. const nativeMethod = window.RTCPeerConnection.prototype[method];
  1193. const methodObj = {[method]() {
  1194. arguments[0] = new ((method === 'addIceCandidate') ?
  1195. window.RTCIceCandidate :
  1196. window.RTCSessionDescription)(arguments[0]);
  1197. return nativeMethod.apply(this, arguments);
  1198. }};
  1199. window.RTCPeerConnection.prototype[method] = methodObj[method];
  1200. });
  1201. }
  1202. }
  1203. // Attempt to fix ONN in plan-b mode.
  1204. function fixNegotiationNeeded(window, browserDetails) {
  1205. wrapPeerConnectionEvent(window, 'negotiationneeded', e => {
  1206. const pc = e.target;
  1207. if (browserDetails.version < 72 || (pc.getConfiguration &&
  1208. pc.getConfiguration().sdpSemantics === 'plan-b')) {
  1209. if (pc.signalingState !== 'stable') {
  1210. return;
  1211. }
  1212. }
  1213. return e;
  1214. });
  1215. }
  1216. var chromeShim = /*#__PURE__*/Object.freeze({
  1217. __proto__: null,
  1218. shimMediaStream: shimMediaStream,
  1219. shimOnTrack: shimOnTrack$1,
  1220. shimGetSendersWithDtmf: shimGetSendersWithDtmf,
  1221. shimGetStats: shimGetStats,
  1222. shimSenderReceiverGetStats: shimSenderReceiverGetStats,
  1223. shimAddTrackRemoveTrackWithNative: shimAddTrackRemoveTrackWithNative,
  1224. shimAddTrackRemoveTrack: shimAddTrackRemoveTrack,
  1225. shimPeerConnection: shimPeerConnection$2,
  1226. fixNegotiationNeeded: fixNegotiationNeeded,
  1227. shimGetUserMedia: shimGetUserMedia$3,
  1228. shimGetDisplayMedia: shimGetDisplayMedia$2
  1229. });
  1230. /*
  1231. * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
  1232. *
  1233. * Use of this source code is governed by a BSD-style license
  1234. * that can be found in the LICENSE file in the root of the source
  1235. * tree.
  1236. */
  1237. // Edge does not like
  1238. // 1) stun: filtered after 14393 unless ?transport=udp is present
  1239. // 2) turn: that does not have all of turn:host:port?transport=udp
  1240. // 3) turn: with ipv6 addresses
  1241. // 4) turn: occurring muliple times
  1242. function filterIceServers$1(iceServers, edgeVersion) {
  1243. let hasTurn = false;
  1244. iceServers = JSON.parse(JSON.stringify(iceServers));
  1245. return iceServers.filter(server => {
  1246. if (server && (server.urls || server.url)) {
  1247. let urls = server.urls || server.url;
  1248. if (server.url && !server.urls) {
  1249. deprecated('RTCIceServer.url', 'RTCIceServer.urls');
  1250. }
  1251. const isString = typeof urls === 'string';
  1252. if (isString) {
  1253. urls = [urls];
  1254. }
  1255. urls = urls.filter(url => {
  1256. // filter STUN unconditionally.
  1257. if (url.indexOf('stun:') === 0) {
  1258. return false;
  1259. }
  1260. const validTurn = url.startsWith('turn') &&
  1261. !url.startsWith('turn:[') &&
  1262. url.includes('transport=udp');
  1263. if (validTurn && !hasTurn) {
  1264. hasTurn = true;
  1265. return true;
  1266. }
  1267. return validTurn && !hasTurn;
  1268. });
  1269. delete server.url;
  1270. server.urls = isString ? urls[0] : urls;
  1271. return !!urls.length;
  1272. }
  1273. });
  1274. }
  1275. function createCommonjsModule(fn) {
  1276. var module = { exports: {} };
  1277. return fn(module, module.exports), module.exports;
  1278. }
  1279. /* eslint-env node */
  1280. var sdp = createCommonjsModule(function (module) {
  1281. // SDP helpers.
  1282. var SDPUtils = {};
  1283. // Generate an alphanumeric identifier for cname or mids.
  1284. // TODO: use UUIDs instead? https://gist.github.com/jed/982883
  1285. SDPUtils.generateIdentifier = function() {
  1286. return Math.random().toString(36).substr(2, 10);
  1287. };
  1288. // The RTCP CNAME used by all peerconnections from the same JS.
  1289. SDPUtils.localCName = SDPUtils.generateIdentifier();
  1290. // Splits SDP into lines, dealing with both CRLF and LF.
  1291. SDPUtils.splitLines = function(blob) {
  1292. return blob.trim().split('\n').map(function(line) {
  1293. return line.trim();
  1294. });
  1295. };
  1296. // Splits SDP into sessionpart and mediasections. Ensures CRLF.
  1297. SDPUtils.splitSections = function(blob) {
  1298. var parts = blob.split('\nm=');
  1299. return parts.map(function(part, index) {
  1300. return (index > 0 ? 'm=' + part : part).trim() + '\r\n';
  1301. });
  1302. };
  1303. // returns the session description.
  1304. SDPUtils.getDescription = function(blob) {
  1305. var sections = SDPUtils.splitSections(blob);
  1306. return sections && sections[0];
  1307. };
  1308. // returns the individual media sections.
  1309. SDPUtils.getMediaSections = function(blob) {
  1310. var sections = SDPUtils.splitSections(blob);
  1311. sections.shift();
  1312. return sections;
  1313. };
  1314. // Returns lines that start with a certain prefix.
  1315. SDPUtils.matchPrefix = function(blob, prefix) {
  1316. return SDPUtils.splitLines(blob).filter(function(line) {
  1317. return line.indexOf(prefix) === 0;
  1318. });
  1319. };
  1320. // Parses an ICE candidate line. Sample input:
  1321. // candidate:702786350 2 udp 41819902 8.8.8.8 60769 typ relay raddr 8.8.8.8
  1322. // rport 55996"
  1323. SDPUtils.parseCandidate = function(line) {
  1324. var parts;
  1325. // Parse both variants.
  1326. if (line.indexOf('a=candidate:') === 0) {
  1327. parts = line.substring(12).split(' ');
  1328. } else {
  1329. parts = line.substring(10).split(' ');
  1330. }
  1331. var candidate = {
  1332. foundation: parts[0],
  1333. component: parseInt(parts[1], 10),
  1334. protocol: parts[2].toLowerCase(),
  1335. priority: parseInt(parts[3], 10),
  1336. ip: parts[4],
  1337. address: parts[4], // address is an alias for ip.
  1338. port: parseInt(parts[5], 10),
  1339. // skip parts[6] == 'typ'
  1340. type: parts[7]
  1341. };
  1342. for (var i = 8; i < parts.length; i += 2) {
  1343. switch (parts[i]) {
  1344. case 'raddr':
  1345. candidate.relatedAddress = parts[i + 1];
  1346. break;
  1347. case 'rport':
  1348. candidate.relatedPort = parseInt(parts[i + 1], 10);
  1349. break;
  1350. case 'tcptype':
  1351. candidate.tcpType = parts[i + 1];
  1352. break;
  1353. case 'ufrag':
  1354. candidate.ufrag = parts[i + 1]; // for backward compability.
  1355. candidate.usernameFragment = parts[i + 1];
  1356. break;
  1357. default: // extension handling, in particular ufrag
  1358. candidate[parts[i]] = parts[i + 1];
  1359. break;
  1360. }
  1361. }
  1362. return candidate;
  1363. };
  1364. // Translates a candidate object into SDP candidate attribute.
  1365. SDPUtils.writeCandidate = function(candidate) {
  1366. var sdp = [];
  1367. sdp.push(candidate.foundation);
  1368. sdp.push(candidate.component);
  1369. sdp.push(candidate.protocol.toUpperCase());
  1370. sdp.push(candidate.priority);
  1371. sdp.push(candidate.address || candidate.ip);
  1372. sdp.push(candidate.port);
  1373. var type = candidate.type;
  1374. sdp.push('typ');
  1375. sdp.push(type);
  1376. if (type !== 'host' && candidate.relatedAddress &&
  1377. candidate.relatedPort) {
  1378. sdp.push('raddr');
  1379. sdp.push(candidate.relatedAddress);
  1380. sdp.push('rport');
  1381. sdp.push(candidate.relatedPort);
  1382. }
  1383. if (candidate.tcpType && candidate.protocol.toLowerCase() === 'tcp') {
  1384. sdp.push('tcptype');
  1385. sdp.push(candidate.tcpType);
  1386. }
  1387. if (candidate.usernameFragment || candidate.ufrag) {
  1388. sdp.push('ufrag');
  1389. sdp.push(candidate.usernameFragment || candidate.ufrag);
  1390. }
  1391. return 'candidate:' + sdp.join(' ');
  1392. };
  1393. // Parses an ice-options line, returns an array of option tags.
  1394. // a=ice-options:foo bar
  1395. SDPUtils.parseIceOptions = function(line) {
  1396. return line.substr(14).split(' ');
  1397. };
  1398. // Parses an rtpmap line, returns RTCRtpCoddecParameters. Sample input:
  1399. // a=rtpmap:111 opus/48000/2
  1400. SDPUtils.parseRtpMap = function(line) {
  1401. var parts = line.substr(9).split(' ');
  1402. var parsed = {
  1403. payloadType: parseInt(parts.shift(), 10) // was: id
  1404. };
  1405. parts = parts[0].split('/');
  1406. parsed.name = parts[0];
  1407. parsed.clockRate = parseInt(parts[1], 10); // was: clockrate
  1408. parsed.channels = parts.length === 3 ? parseInt(parts[2], 10) : 1;
  1409. // legacy alias, got renamed back to channels in ORTC.
  1410. parsed.numChannels = parsed.channels;
  1411. return parsed;
  1412. };
  1413. // Generate an a=rtpmap line from RTCRtpCodecCapability or
  1414. // RTCRtpCodecParameters.
  1415. SDPUtils.writeRtpMap = function(codec) {
  1416. var pt = codec.payloadType;
  1417. if (codec.preferredPayloadType !== undefined) {
  1418. pt = codec.preferredPayloadType;
  1419. }
  1420. var channels = codec.channels || codec.numChannels || 1;
  1421. return 'a=rtpmap:' + pt + ' ' + codec.name + '/' + codec.clockRate +
  1422. (channels !== 1 ? '/' + channels : '') + '\r\n';
  1423. };
  1424. // Parses an a=extmap line (headerextension from RFC 5285). Sample input:
  1425. // a=extmap:2 urn:ietf:params:rtp-hdrext:toffset
  1426. // a=extmap:2/sendonly urn:ietf:params:rtp-hdrext:toffset
  1427. SDPUtils.parseExtmap = function(line) {
  1428. var parts = line.substr(9).split(' ');
  1429. return {
  1430. id: parseInt(parts[0], 10),
  1431. direction: parts[0].indexOf('/') > 0 ? parts[0].split('/')[1] : 'sendrecv',
  1432. uri: parts[1]
  1433. };
  1434. };
  1435. // Generates a=extmap line from RTCRtpHeaderExtensionParameters or
  1436. // RTCRtpHeaderExtension.
  1437. SDPUtils.writeExtmap = function(headerExtension) {
  1438. return 'a=extmap:' + (headerExtension.id || headerExtension.preferredId) +
  1439. (headerExtension.direction && headerExtension.direction !== 'sendrecv'
  1440. ? '/' + headerExtension.direction
  1441. : '') +
  1442. ' ' + headerExtension.uri + '\r\n';
  1443. };
  1444. // Parses an ftmp line, returns dictionary. Sample input:
  1445. // a=fmtp:96 vbr=on;cng=on
  1446. // Also deals with vbr=on; cng=on
  1447. SDPUtils.parseFmtp = function(line) {
  1448. var parsed = {};
  1449. var kv;
  1450. var parts = line.substr(line.indexOf(' ') + 1).split(';');
  1451. for (var j = 0; j < parts.length; j++) {
  1452. kv = parts[j].trim().split('=');
  1453. parsed[kv[0].trim()] = kv[1];
  1454. }
  1455. return parsed;
  1456. };
  1457. // Generates an a=ftmp line from RTCRtpCodecCapability or RTCRtpCodecParameters.
  1458. SDPUtils.writeFmtp = function(codec) {
  1459. var line = '';
  1460. var pt = codec.payloadType;
  1461. if (codec.preferredPayloadType !== undefined) {
  1462. pt = codec.preferredPayloadType;
  1463. }
  1464. if (codec.parameters && Object.keys(codec.parameters).length) {
  1465. var params = [];
  1466. Object.keys(codec.parameters).forEach(function(param) {
  1467. if (codec.parameters[param]) {
  1468. params.push(param + '=' + codec.parameters[param]);
  1469. } else {
  1470. params.push(param);
  1471. }
  1472. });
  1473. line += 'a=fmtp:' + pt + ' ' + params.join(';') + '\r\n';
  1474. }
  1475. return line;
  1476. };
  1477. // Parses an rtcp-fb line, returns RTCPRtcpFeedback object. Sample input:
  1478. // a=rtcp-fb:98 nack rpsi
  1479. SDPUtils.parseRtcpFb = function(line) {
  1480. var parts = line.substr(line.indexOf(' ') + 1).split(' ');
  1481. return {
  1482. type: parts.shift(),
  1483. parameter: parts.join(' ')
  1484. };
  1485. };
  1486. // Generate a=rtcp-fb lines from RTCRtpCodecCapability or RTCRtpCodecParameters.
  1487. SDPUtils.writeRtcpFb = function(codec) {
  1488. var lines = '';
  1489. var pt = codec.payloadType;
  1490. if (codec.preferredPayloadType !== undefined) {
  1491. pt = codec.preferredPayloadType;
  1492. }
  1493. if (codec.rtcpFeedback && codec.rtcpFeedback.length) {
  1494. // FIXME: special handling for trr-int?
  1495. codec.rtcpFeedback.forEach(function(fb) {
  1496. lines += 'a=rtcp-fb:' + pt + ' ' + fb.type +
  1497. (fb.parameter && fb.parameter.length ? ' ' + fb.parameter : '') +
  1498. '\r\n';
  1499. });
  1500. }
  1501. return lines;
  1502. };
  1503. // Parses an RFC 5576 ssrc media attribute. Sample input:
  1504. // a=ssrc:3735928559 cname:something
  1505. SDPUtils.parseSsrcMedia = function(line) {
  1506. var sp = line.indexOf(' ');
  1507. var parts = {
  1508. ssrc: parseInt(line.substr(7, sp - 7), 10)
  1509. };
  1510. var colon = line.indexOf(':', sp);
  1511. if (colon > -1) {
  1512. parts.attribute = line.substr(sp + 1, colon - sp - 1);
  1513. parts.value = line.substr(colon + 1);
  1514. } else {
  1515. parts.attribute = line.substr(sp + 1);
  1516. }
  1517. return parts;
  1518. };
  1519. SDPUtils.parseSsrcGroup = function(line) {
  1520. var parts = line.substr(13).split(' ');
  1521. return {
  1522. semantics: parts.shift(),
  1523. ssrcs: parts.map(function(ssrc) {
  1524. return parseInt(ssrc, 10);
  1525. })
  1526. };
  1527. };
  1528. // Extracts the MID (RFC 5888) from a media section.
  1529. // returns the MID or undefined if no mid line was found.
  1530. SDPUtils.getMid = function(mediaSection) {
  1531. var mid = SDPUtils.matchPrefix(mediaSection, 'a=mid:')[0];
  1532. if (mid) {
  1533. return mid.substr(6);
  1534. }
  1535. };
  1536. SDPUtils.parseFingerprint = function(line) {
  1537. var parts = line.substr(14).split(' ');
  1538. return {
  1539. algorithm: parts[0].toLowerCase(), // algorithm is case-sensitive in Edge.
  1540. value: parts[1]
  1541. };
  1542. };
  1543. // Extracts DTLS parameters from SDP media section or sessionpart.
  1544. // FIXME: for consistency with other functions this should only
  1545. // get the fingerprint line as input. See also getIceParameters.
  1546. SDPUtils.getDtlsParameters = function(mediaSection, sessionpart) {
  1547. var lines = SDPUtils.matchPrefix(mediaSection + sessionpart,
  1548. 'a=fingerprint:');
  1549. // Note: a=setup line is ignored since we use the 'auto' role.
  1550. // Note2: 'algorithm' is not case sensitive except in Edge.
  1551. return {
  1552. role: 'auto',
  1553. fingerprints: lines.map(SDPUtils.parseFingerprint)
  1554. };
  1555. };
  1556. // Serializes DTLS parameters to SDP.
  1557. SDPUtils.writeDtlsParameters = function(params, setupType) {
  1558. var sdp = 'a=setup:' + setupType + '\r\n';
  1559. params.fingerprints.forEach(function(fp) {
  1560. sdp += 'a=fingerprint:' + fp.algorithm + ' ' + fp.value + '\r\n';
  1561. });
  1562. return sdp;
  1563. };
  1564. // Parses a=crypto lines into
  1565. // https://rawgit.com/aboba/edgertc/master/msortc-rs4.html#dictionary-rtcsrtpsdesparameters-members
  1566. SDPUtils.parseCryptoLine = function(line) {
  1567. var parts = line.substr(9).split(' ');
  1568. return {
  1569. tag: parseInt(parts[0], 10),
  1570. cryptoSuite: parts[1],
  1571. keyParams: parts[2],
  1572. sessionParams: parts.slice(3),
  1573. };
  1574. };
  1575. SDPUtils.writeCryptoLine = function(parameters) {
  1576. return 'a=crypto:' + parameters.tag + ' ' +
  1577. parameters.cryptoSuite + ' ' +
  1578. (typeof parameters.keyParams === 'object'
  1579. ? SDPUtils.writeCryptoKeyParams(parameters.keyParams)
  1580. : parameters.keyParams) +
  1581. (parameters.sessionParams ? ' ' + parameters.sessionParams.join(' ') : '') +
  1582. '\r\n';
  1583. };
  1584. // Parses the crypto key parameters into
  1585. // https://rawgit.com/aboba/edgertc/master/msortc-rs4.html#rtcsrtpkeyparam*
  1586. SDPUtils.parseCryptoKeyParams = function(keyParams) {
  1587. if (keyParams.indexOf('inline:') !== 0) {
  1588. return null;
  1589. }
  1590. var parts = keyParams.substr(7).split('|');
  1591. return {
  1592. keyMethod: 'inline',
  1593. keySalt: parts[0],
  1594. lifeTime: parts[1],
  1595. mkiValue: parts[2] ? parts[2].split(':')[0] : undefined,
  1596. mkiLength: parts[2] ? parts[2].split(':')[1] : undefined,
  1597. };
  1598. };
  1599. SDPUtils.writeCryptoKeyParams = function(keyParams) {
  1600. return keyParams.keyMethod + ':'
  1601. + keyParams.keySalt +
  1602. (keyParams.lifeTime ? '|' + keyParams.lifeTime : '') +
  1603. (keyParams.mkiValue && keyParams.mkiLength
  1604. ? '|' + keyParams.mkiValue + ':' + keyParams.mkiLength
  1605. : '');
  1606. };
  1607. // Extracts all SDES paramters.
  1608. SDPUtils.getCryptoParameters = function(mediaSection, sessionpart) {
  1609. var lines = SDPUtils.matchPrefix(mediaSection + sessionpart,
  1610. 'a=crypto:');
  1611. return lines.map(SDPUtils.parseCryptoLine);
  1612. };
  1613. // Parses ICE information from SDP media section or sessionpart.
  1614. // FIXME: for consistency with other functions this should only
  1615. // get the ice-ufrag and ice-pwd lines as input.
  1616. SDPUtils.getIceParameters = function(mediaSection, sessionpart) {
  1617. var ufrag = SDPUtils.matchPrefix(mediaSection + sessionpart,
  1618. 'a=ice-ufrag:')[0];
  1619. var pwd = SDPUtils.matchPrefix(mediaSection + sessionpart,
  1620. 'a=ice-pwd:')[0];
  1621. if (!(ufrag && pwd)) {
  1622. return null;
  1623. }
  1624. return {
  1625. usernameFragment: ufrag.substr(12),
  1626. password: pwd.substr(10),
  1627. };
  1628. };
  1629. // Serializes ICE parameters to SDP.
  1630. SDPUtils.writeIceParameters = function(params) {
  1631. return 'a=ice-ufrag:' + params.usernameFragment + '\r\n' +
  1632. 'a=ice-pwd:' + params.password + '\r\n';
  1633. };
  1634. // Parses the SDP media section and returns RTCRtpParameters.
  1635. SDPUtils.parseRtpParameters = function(mediaSection) {
  1636. var description = {
  1637. codecs: [],
  1638. headerExtensions: [],
  1639. fecMechanisms: [],
  1640. rtcp: []
  1641. };
  1642. var lines = SDPUtils.splitLines(mediaSection);
  1643. var mline = lines[0].split(' ');
  1644. for (var i = 3; i < mline.length; i++) { // find all codecs from mline[3..]
  1645. var pt = mline[i];
  1646. var rtpmapline = SDPUtils.matchPrefix(
  1647. mediaSection, 'a=rtpmap:' + pt + ' ')[0];
  1648. if (rtpmapline) {
  1649. var codec = SDPUtils.parseRtpMap(rtpmapline);
  1650. var fmtps = SDPUtils.matchPrefix(
  1651. mediaSection, 'a=fmtp:' + pt + ' ');
  1652. // Only the first a=fmtp:<pt> is considered.
  1653. codec.parameters = fmtps.length ? SDPUtils.parseFmtp(fmtps[0]) : {};
  1654. codec.rtcpFeedback = SDPUtils.matchPrefix(
  1655. mediaSection, 'a=rtcp-fb:' + pt + ' ')
  1656. .map(SDPUtils.parseRtcpFb);
  1657. description.codecs.push(codec);
  1658. // parse FEC mechanisms from rtpmap lines.
  1659. switch (codec.name.toUpperCase()) {
  1660. case 'RED':
  1661. case 'ULPFEC':
  1662. description.fecMechanisms.push(codec.name.toUpperCase());
  1663. break;
  1664. }
  1665. }
  1666. }
  1667. SDPUtils.matchPrefix(mediaSection, 'a=extmap:').forEach(function(line) {
  1668. description.headerExtensions.push(SDPUtils.parseExtmap(line));
  1669. });
  1670. // FIXME: parse rtcp.
  1671. return description;
  1672. };
  1673. // Generates parts of the SDP media section describing the capabilities /
  1674. // parameters.
  1675. SDPUtils.writeRtpDescription = function(kind, caps) {
  1676. var sdp = '';
  1677. // Build the mline.
  1678. sdp += 'm=' + kind + ' ';
  1679. sdp += caps.codecs.length > 0 ? '9' : '0'; // reject if no codecs.
  1680. sdp += ' UDP/TLS/RTP/SAVPF ';
  1681. sdp += caps.codecs.map(function(codec) {
  1682. if (codec.preferredPayloadType !== undefined) {
  1683. return codec.preferredPayloadType;
  1684. }
  1685. return codec.payloadType;
  1686. }).join(' ') + '\r\n';
  1687. sdp += 'c=IN IP4 0.0.0.0\r\n';
  1688. sdp += 'a=rtcp:9 IN IP4 0.0.0.0\r\n';
  1689. // Add a=rtpmap lines for each codec. Also fmtp and rtcp-fb.
  1690. caps.codecs.forEach(function(codec) {
  1691. sdp += SDPUtils.writeRtpMap(codec);
  1692. sdp += SDPUtils.writeFmtp(codec);
  1693. sdp += SDPUtils.writeRtcpFb(codec);
  1694. });
  1695. var maxptime = 0;
  1696. caps.codecs.forEach(function(codec) {
  1697. if (codec.maxptime > maxptime) {
  1698. maxptime = codec.maxptime;
  1699. }
  1700. });
  1701. if (maxptime > 0) {
  1702. sdp += 'a=maxptime:' + maxptime + '\r\n';
  1703. }
  1704. sdp += 'a=rtcp-mux\r\n';
  1705. if (caps.headerExtensions) {
  1706. caps.headerExtensions.forEach(function(extension) {
  1707. sdp += SDPUtils.writeExtmap(extension);
  1708. });
  1709. }
  1710. // FIXME: write fecMechanisms.
  1711. return sdp;
  1712. };
  1713. // Parses the SDP media section and returns an array of
  1714. // RTCRtpEncodingParameters.
  1715. SDPUtils.parseRtpEncodingParameters = function(mediaSection) {
  1716. var encodingParameters = [];
  1717. var description = SDPUtils.parseRtpParameters(mediaSection);
  1718. var hasRed = description.fecMechanisms.indexOf('RED') !== -1;
  1719. var hasUlpfec = description.fecMechanisms.indexOf('ULPFEC') !== -1;
  1720. // filter a=ssrc:... cname:, ignore PlanB-msid
  1721. var ssrcs = SDPUtils.matchPrefix(mediaSection, 'a=ssrc:')
  1722. .map(function(line) {
  1723. return SDPUtils.parseSsrcMedia(line);
  1724. })
  1725. .filter(function(parts) {
  1726. return parts.attribute === 'cname';
  1727. });
  1728. var primarySsrc = ssrcs.length > 0 && ssrcs[0].ssrc;
  1729. var secondarySsrc;
  1730. var flows = SDPUtils.matchPrefix(mediaSection, 'a=ssrc-group:FID')
  1731. .map(function(line) {
  1732. var parts = line.substr(17).split(' ');
  1733. return parts.map(function(part) {
  1734. return parseInt(part, 10);
  1735. });
  1736. });
  1737. if (flows.length > 0 && flows[0].length > 1 && flows[0][0] === primarySsrc) {
  1738. secondarySsrc = flows[0][1];
  1739. }
  1740. description.codecs.forEach(function(codec) {
  1741. if (codec.name.toUpperCase() === 'RTX' && codec.parameters.apt) {
  1742. var encParam = {
  1743. ssrc: primarySsrc,
  1744. codecPayloadType: parseInt(codec.parameters.apt, 10)
  1745. };
  1746. if (primarySsrc && secondarySsrc) {
  1747. encParam.rtx = {ssrc: secondarySsrc};
  1748. }
  1749. encodingParameters.push(encParam);
  1750. if (hasRed) {
  1751. encParam = JSON.parse(JSON.stringify(encParam));
  1752. encParam.fec = {
  1753. ssrc: primarySsrc,
  1754. mechanism: hasUlpfec ? 'red+ulpfec' : 'red'
  1755. };
  1756. encodingParameters.push(encParam);
  1757. }
  1758. }
  1759. });
  1760. if (encodingParameters.length === 0 && primarySsrc) {
  1761. encodingParameters.push({
  1762. ssrc: primarySsrc
  1763. });
  1764. }
  1765. // we support both b=AS and b=TIAS but interpret AS as TIAS.
  1766. var bandwidth = SDPUtils.matchPrefix(mediaSection, 'b=');
  1767. if (bandwidth.length) {
  1768. if (bandwidth[0].indexOf('b=TIAS:') === 0) {
  1769. bandwidth = parseInt(bandwidth[0].substr(7), 10);
  1770. } else if (bandwidth[0].indexOf('b=AS:') === 0) {
  1771. // use formula from JSEP to convert b=AS to TIAS value.
  1772. bandwidth = parseInt(bandwidth[0].substr(5), 10) * 1000 * 0.95
  1773. - (50 * 40 * 8);
  1774. } else {
  1775. bandwidth = undefined;
  1776. }
  1777. encodingParameters.forEach(function(params) {
  1778. params.maxBitrate = bandwidth;
  1779. });
  1780. }
  1781. return encodingParameters;
  1782. };
  1783. // parses http://draft.ortc.org/#rtcrtcpparameters*
  1784. SDPUtils.parseRtcpParameters = function(mediaSection) {
  1785. var rtcpParameters = {};
  1786. // Gets the first SSRC. Note tha with RTX there might be multiple
  1787. // SSRCs.
  1788. var remoteSsrc = SDPUtils.matchPrefix(mediaSection, 'a=ssrc:')
  1789. .map(function(line) {
  1790. return SDPUtils.parseSsrcMedia(line);
  1791. })
  1792. .filter(function(obj) {
  1793. return obj.attribute === 'cname';
  1794. })[0];
  1795. if (remoteSsrc) {
  1796. rtcpParameters.cname = remoteSsrc.value;
  1797. rtcpParameters.ssrc = remoteSsrc.ssrc;
  1798. }
  1799. // Edge uses the compound attribute instead of reducedSize
  1800. // compound is !reducedSize
  1801. var rsize = SDPUtils.matchPrefix(mediaSection, 'a=rtcp-rsize');
  1802. rtcpParameters.reducedSize = rsize.length > 0;
  1803. rtcpParameters.compound = rsize.length === 0;
  1804. // parses the rtcp-mux attrіbute.
  1805. // Note that Edge does not support unmuxed RTCP.
  1806. var mux = SDPUtils.matchPrefix(mediaSection, 'a=rtcp-mux');
  1807. rtcpParameters.mux = mux.length > 0;
  1808. return rtcpParameters;
  1809. };
  1810. // parses either a=msid: or a=ssrc:... msid lines and returns
  1811. // the id of the MediaStream and MediaStreamTrack.
  1812. SDPUtils.parseMsid = function(mediaSection) {
  1813. var parts;
  1814. var spec = SDPUtils.matchPrefix(mediaSection, 'a=msid:');
  1815. if (spec.length === 1) {
  1816. parts = spec[0].substr(7).split(' ');
  1817. return {stream: parts[0], track: parts[1]};
  1818. }
  1819. var planB = SDPUtils.matchPrefix(mediaSection, 'a=ssrc:')
  1820. .map(function(line) {
  1821. return SDPUtils.parseSsrcMedia(line);
  1822. })
  1823. .filter(function(msidParts) {
  1824. return msidParts.attribute === 'msid';
  1825. });
  1826. if (planB.length > 0) {
  1827. parts = planB[0].value.split(' ');
  1828. return {stream: parts[0], track: parts[1]};
  1829. }
  1830. };
  1831. // SCTP
  1832. // parses draft-ietf-mmusic-sctp-sdp-26 first and falls back
  1833. // to draft-ietf-mmusic-sctp-sdp-05
  1834. SDPUtils.parseSctpDescription = function(mediaSection) {
  1835. var mline = SDPUtils.parseMLine(mediaSection);
  1836. var maxSizeLine = SDPUtils.matchPrefix(mediaSection, 'a=max-message-size:');
  1837. var maxMessageSize;
  1838. if (maxSizeLine.length > 0) {
  1839. maxMessageSize = parseInt(maxSizeLine[0].substr(19), 10);
  1840. }
  1841. if (isNaN(maxMessageSize)) {
  1842. maxMessageSize = 65536;
  1843. }
  1844. var sctpPort = SDPUtils.matchPrefix(mediaSection, 'a=sctp-port:');
  1845. if (sctpPort.length > 0) {
  1846. return {
  1847. port: parseInt(sctpPort[0].substr(12), 10),
  1848. protocol: mline.fmt,
  1849. maxMessageSize: maxMessageSize
  1850. };
  1851. }
  1852. var sctpMapLines = SDPUtils.matchPrefix(mediaSection, 'a=sctpmap:');
  1853. if (sctpMapLines.length > 0) {
  1854. var parts = SDPUtils.matchPrefix(mediaSection, 'a=sctpmap:')[0]
  1855. .substr(10)
  1856. .split(' ');
  1857. return {
  1858. port: parseInt(parts[0], 10),
  1859. protocol: parts[1],
  1860. maxMessageSize: maxMessageSize
  1861. };
  1862. }
  1863. };
  1864. // SCTP
  1865. // outputs the draft-ietf-mmusic-sctp-sdp-26 version that all browsers
  1866. // support by now receiving in this format, unless we originally parsed
  1867. // as the draft-ietf-mmusic-sctp-sdp-05 format (indicated by the m-line
  1868. // protocol of DTLS/SCTP -- without UDP/ or TCP/)
  1869. SDPUtils.writeSctpDescription = function(media, sctp) {
  1870. var output = [];
  1871. if (media.protocol !== 'DTLS/SCTP') {
  1872. output = [
  1873. 'm=' + media.kind + ' 9 ' + media.protocol + ' ' + sctp.protocol + '\r\n',
  1874. 'c=IN IP4 0.0.0.0\r\n',
  1875. 'a=sctp-port:' + sctp.port + '\r\n'
  1876. ];
  1877. } else {
  1878. output = [
  1879. 'm=' + media.kind + ' 9 ' + media.protocol + ' ' + sctp.port + '\r\n',
  1880. 'c=IN IP4 0.0.0.0\r\n',
  1881. 'a=sctpmap:' + sctp.port + ' ' + sctp.protocol + ' 65535\r\n'
  1882. ];
  1883. }
  1884. if (sctp.maxMessageSize !== undefined) {
  1885. output.push('a=max-message-size:' + sctp.maxMessageSize + '\r\n');
  1886. }
  1887. return output.join('');
  1888. };
  1889. // Generate a session ID for SDP.
  1890. // https://tools.ietf.org/html/draft-ietf-rtcweb-jsep-20#section-5.2.1
  1891. // recommends using a cryptographically random +ve 64-bit value
  1892. // but right now this should be acceptable and within the right range
  1893. SDPUtils.generateSessionId = function() {
  1894. return Math.random().toString().substr(2, 21);
  1895. };
  1896. // Write boilder plate for start of SDP
  1897. // sessId argument is optional - if not supplied it will
  1898. // be generated randomly
  1899. // sessVersion is optional and defaults to 2
  1900. // sessUser is optional and defaults to 'thisisadapterortc'
  1901. SDPUtils.writeSessionBoilerplate = function(sessId, sessVer, sessUser) {
  1902. var sessionId;
  1903. var version = sessVer !== undefined ? sessVer : 2;
  1904. if (sessId) {
  1905. sessionId = sessId;
  1906. } else {
  1907. sessionId = SDPUtils.generateSessionId();
  1908. }
  1909. var user = sessUser || 'thisisadapterortc';
  1910. // FIXME: sess-id should be an NTP timestamp.
  1911. return 'v=0\r\n' +
  1912. 'o=' + user + ' ' + sessionId + ' ' + version +
  1913. ' IN IP4 127.0.0.1\r\n' +
  1914. 's=-\r\n' +
  1915. 't=0 0\r\n';
  1916. };
  1917. SDPUtils.writeMediaSection = function(transceiver, caps, type, stream) {
  1918. var sdp = SDPUtils.writeRtpDescription(transceiver.kind, caps);
  1919. // Map ICE parameters (ufrag, pwd) to SDP.
  1920. sdp += SDPUtils.writeIceParameters(
  1921. transceiver.iceGatherer.getLocalParameters());
  1922. // Map DTLS parameters to SDP.
  1923. sdp += SDPUtils.writeDtlsParameters(
  1924. transceiver.dtlsTransport.getLocalParameters(),
  1925. type === 'offer' ? 'actpass' : 'active');
  1926. sdp += 'a=mid:' + transceiver.mid + '\r\n';
  1927. if (transceiver.direction) {
  1928. sdp += 'a=' + transceiver.direction + '\r\n';
  1929. } else if (transceiver.rtpSender && transceiver.rtpReceiver) {
  1930. sdp += 'a=sendrecv\r\n';
  1931. } else if (transceiver.rtpSender) {
  1932. sdp += 'a=sendonly\r\n';
  1933. } else if (transceiver.rtpReceiver) {
  1934. sdp += 'a=recvonly\r\n';
  1935. } else {
  1936. sdp += 'a=inactive\r\n';
  1937. }
  1938. if (transceiver.rtpSender) {
  1939. // spec.
  1940. var msid = 'msid:' + stream.id + ' ' +
  1941. transceiver.rtpSender.track.id + '\r\n';
  1942. sdp += 'a=' + msid;
  1943. // for Chrome.
  1944. sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].ssrc +
  1945. ' ' + msid;
  1946. if (transceiver.sendEncodingParameters[0].rtx) {
  1947. sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].rtx.ssrc +
  1948. ' ' + msid;
  1949. sdp += 'a=ssrc-group:FID ' +
  1950. transceiver.sendEncodingParameters[0].ssrc + ' ' +
  1951. transceiver.sendEncodingParameters[0].rtx.ssrc +
  1952. '\r\n';
  1953. }
  1954. }
  1955. // FIXME: this should be written by writeRtpDescription.
  1956. sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].ssrc +
  1957. ' cname:' + SDPUtils.localCName + '\r\n';
  1958. if (transceiver.rtpSender && transceiver.sendEncodingParameters[0].rtx) {
  1959. sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].rtx.ssrc +
  1960. ' cname:' + SDPUtils.localCName + '\r\n';
  1961. }
  1962. return sdp;
  1963. };
  1964. // Gets the direction from the mediaSection or the sessionpart.
  1965. SDPUtils.getDirection = function(mediaSection, sessionpart) {
  1966. // Look for sendrecv, sendonly, recvonly, inactive, default to sendrecv.
  1967. var lines = SDPUtils.splitLines(mediaSection);
  1968. for (var i = 0; i < lines.length; i++) {
  1969. switch (lines[i]) {
  1970. case 'a=sendrecv':
  1971. case 'a=sendonly':
  1972. case 'a=recvonly':
  1973. case 'a=inactive':
  1974. return lines[i].substr(2);
  1975. // FIXME: What should happen here?
  1976. }
  1977. }
  1978. if (sessionpart) {
  1979. return SDPUtils.getDirection(sessionpart);
  1980. }
  1981. return 'sendrecv';
  1982. };
  1983. SDPUtils.getKind = function(mediaSection) {
  1984. var lines = SDPUtils.splitLines(mediaSection);
  1985. var mline = lines[0].split(' ');
  1986. return mline[0].substr(2);
  1987. };
  1988. SDPUtils.isRejected = function(mediaSection) {
  1989. return mediaSection.split(' ', 2)[1] === '0';
  1990. };
  1991. SDPUtils.parseMLine = function(mediaSection) {
  1992. var lines = SDPUtils.splitLines(mediaSection);
  1993. var parts = lines[0].substr(2).split(' ');
  1994. return {
  1995. kind: parts[0],
  1996. port: parseInt(parts[1], 10),
  1997. protocol: parts[2],
  1998. fmt: parts.slice(3).join(' ')
  1999. };
  2000. };
  2001. SDPUtils.parseOLine = function(mediaSection) {
  2002. var line = SDPUtils.matchPrefix(mediaSection, 'o=')[0];
  2003. var parts = line.substr(2).split(' ');
  2004. return {
  2005. username: parts[0],
  2006. sessionId: parts[1],
  2007. sessionVersion: parseInt(parts[2], 10),
  2008. netType: parts[3],
  2009. addressType: parts[4],
  2010. address: parts[5]
  2011. };
  2012. };
  2013. // a very naive interpretation of a valid SDP.
  2014. SDPUtils.isValidSDP = function(blob) {
  2015. if (typeof blob !== 'string' || blob.length === 0) {
  2016. return false;
  2017. }
  2018. var lines = SDPUtils.splitLines(blob);
  2019. for (var i = 0; i < lines.length; i++) {
  2020. if (lines[i].length < 2 || lines[i].charAt(1) !== '=') {
  2021. return false;
  2022. }
  2023. // TODO: check the modifier a bit more.
  2024. }
  2025. return true;
  2026. };
  2027. // Expose public methods.
  2028. {
  2029. module.exports = SDPUtils;
  2030. }
  2031. });
  2032. /*
  2033. * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
  2034. *
  2035. * Use of this source code is governed by a BSD-style license
  2036. * that can be found in the LICENSE file in the root of the source
  2037. * tree.
  2038. */
  2039. function fixStatsType(stat) {
  2040. return {
  2041. inboundrtp: 'inbound-rtp',
  2042. outboundrtp: 'outbound-rtp',
  2043. candidatepair: 'candidate-pair',
  2044. localcandidate: 'local-candidate',
  2045. remotecandidate: 'remote-candidate'
  2046. }[stat.type] || stat.type;
  2047. }
  2048. function writeMediaSection(transceiver, caps, type, stream, dtlsRole) {
  2049. var sdp$1 = sdp.writeRtpDescription(transceiver.kind, caps);
  2050. // Map ICE parameters (ufrag, pwd) to SDP.
  2051. sdp$1 += sdp.writeIceParameters(
  2052. transceiver.iceGatherer.getLocalParameters());
  2053. // Map DTLS parameters to SDP.
  2054. sdp$1 += sdp.writeDtlsParameters(
  2055. transceiver.dtlsTransport.getLocalParameters(),
  2056. type === 'offer' ? 'actpass' : dtlsRole || 'active');
  2057. sdp$1 += 'a=mid:' + transceiver.mid + '\r\n';
  2058. if (transceiver.rtpSender && transceiver.rtpReceiver) {
  2059. sdp$1 += 'a=sendrecv\r\n';
  2060. } else if (transceiver.rtpSender) {
  2061. sdp$1 += 'a=sendonly\r\n';
  2062. } else if (transceiver.rtpReceiver) {
  2063. sdp$1 += 'a=recvonly\r\n';
  2064. } else {
  2065. sdp$1 += 'a=inactive\r\n';
  2066. }
  2067. if (transceiver.rtpSender) {
  2068. var trackId = transceiver.rtpSender._initialTrackId ||
  2069. transceiver.rtpSender.track.id;
  2070. transceiver.rtpSender._initialTrackId = trackId;
  2071. // spec.
  2072. var msid = 'msid:' + (stream ? stream.id : '-') + ' ' +
  2073. trackId + '\r\n';
  2074. sdp$1 += 'a=' + msid;
  2075. // for Chrome. Legacy should no longer be required.
  2076. sdp$1 += 'a=ssrc:' + transceiver.sendEncodingParameters[0].ssrc +
  2077. ' ' + msid;
  2078. // RTX
  2079. if (transceiver.sendEncodingParameters[0].rtx) {
  2080. sdp$1 += 'a=ssrc:' + transceiver.sendEncodingParameters[0].rtx.ssrc +
  2081. ' ' + msid;
  2082. sdp$1 += 'a=ssrc-group:FID ' +
  2083. transceiver.sendEncodingParameters[0].ssrc + ' ' +
  2084. transceiver.sendEncodingParameters[0].rtx.ssrc +
  2085. '\r\n';
  2086. }
  2087. }
  2088. // FIXME: this should be written by writeRtpDescription.
  2089. sdp$1 += 'a=ssrc:' + transceiver.sendEncodingParameters[0].ssrc +
  2090. ' cname:' + sdp.localCName + '\r\n';
  2091. if (transceiver.rtpSender && transceiver.sendEncodingParameters[0].rtx) {
  2092. sdp$1 += 'a=ssrc:' + transceiver.sendEncodingParameters[0].rtx.ssrc +
  2093. ' cname:' + sdp.localCName + '\r\n';
  2094. }
  2095. return sdp$1;
  2096. }
  2097. // Edge does not like
  2098. // 1) stun: filtered after 14393 unless ?transport=udp is present
  2099. // 2) turn: that does not have all of turn:host:port?transport=udp
  2100. // 3) turn: with ipv6 addresses
  2101. // 4) turn: occurring muliple times
  2102. function filterIceServers(iceServers, edgeVersion) {
  2103. var hasTurn = false;
  2104. iceServers = JSON.parse(JSON.stringify(iceServers));
  2105. return iceServers.filter(function(server) {
  2106. if (server && (server.urls || server.url)) {
  2107. var urls = server.urls || server.url;
  2108. if (server.url && !server.urls) {
  2109. console.warn('RTCIceServer.url is deprecated! Use urls instead.');
  2110. }
  2111. var isString = typeof urls === 'string';
  2112. if (isString) {
  2113. urls = [urls];
  2114. }
  2115. urls = urls.filter(function(url) {
  2116. var validTurn = url.indexOf('turn:') === 0 &&
  2117. url.indexOf('transport=udp') !== -1 &&
  2118. url.indexOf('turn:[') === -1 &&
  2119. !hasTurn;
  2120. if (validTurn) {
  2121. hasTurn = true;
  2122. return true;
  2123. }
  2124. return url.indexOf('stun:') === 0 && edgeVersion >= 14393 &&
  2125. url.indexOf('?transport=udp') === -1;
  2126. });
  2127. delete server.url;
  2128. server.urls = isString ? urls[0] : urls;
  2129. return !!urls.length;
  2130. }
  2131. });
  2132. }
  2133. // Determines the intersection of local and remote capabilities.
  2134. function getCommonCapabilities(localCapabilities, remoteCapabilities) {
  2135. var commonCapabilities = {
  2136. codecs: [],
  2137. headerExtensions: [],
  2138. fecMechanisms: []
  2139. };
  2140. var findCodecByPayloadType = function(pt, codecs) {
  2141. pt = parseInt(pt, 10);
  2142. for (var i = 0; i < codecs.length; i++) {
  2143. if (codecs[i].payloadType === pt ||
  2144. codecs[i].preferredPayloadType === pt) {
  2145. return codecs[i];
  2146. }
  2147. }
  2148. };
  2149. var rtxCapabilityMatches = function(lRtx, rRtx, lCodecs, rCodecs) {
  2150. var lCodec = findCodecByPayloadType(lRtx.parameters.apt, lCodecs);
  2151. var rCodec = findCodecByPayloadType(rRtx.parameters.apt, rCodecs);
  2152. return lCodec && rCodec &&
  2153. lCodec.name.toLowerCase() === rCodec.name.toLowerCase();
  2154. };
  2155. localCapabilities.codecs.forEach(function(lCodec) {
  2156. for (var i = 0; i < remoteCapabilities.codecs.length; i++) {
  2157. var rCodec = remoteCapabilities.codecs[i];
  2158. if (lCodec.name.toLowerCase() === rCodec.name.toLowerCase() &&
  2159. lCodec.clockRate === rCodec.clockRate) {
  2160. if (lCodec.name.toLowerCase() === 'rtx' &&
  2161. lCodec.parameters && rCodec.parameters.apt) {
  2162. // for RTX we need to find the local rtx that has a apt
  2163. // which points to the same local codec as the remote one.
  2164. if (!rtxCapabilityMatches(lCodec, rCodec,
  2165. localCapabilities.codecs, remoteCapabilities.codecs)) {
  2166. continue;
  2167. }
  2168. }
  2169. rCodec = JSON.parse(JSON.stringify(rCodec)); // deepcopy
  2170. // number of channels is the highest common number of channels
  2171. rCodec.numChannels = Math.min(lCodec.numChannels,
  2172. rCodec.numChannels);
  2173. // push rCodec so we reply with offerer payload type
  2174. commonCapabilities.codecs.push(rCodec);
  2175. // determine common feedback mechanisms
  2176. rCodec.rtcpFeedback = rCodec.rtcpFeedback.filter(function(fb) {
  2177. for (var j = 0; j < lCodec.rtcpFeedback.length; j++) {
  2178. if (lCodec.rtcpFeedback[j].type === fb.type &&
  2179. lCodec.rtcpFeedback[j].parameter === fb.parameter) {
  2180. return true;
  2181. }
  2182. }
  2183. return false;
  2184. });
  2185. // FIXME: also need to determine .parameters
  2186. // see https://github.com/openpeer/ortc/issues/569
  2187. break;
  2188. }
  2189. }
  2190. });
  2191. localCapabilities.headerExtensions.forEach(function(lHeaderExtension) {
  2192. for (var i = 0; i < remoteCapabilities.headerExtensions.length;
  2193. i++) {
  2194. var rHeaderExtension = remoteCapabilities.headerExtensions[i];
  2195. if (lHeaderExtension.uri === rHeaderExtension.uri) {
  2196. commonCapabilities.headerExtensions.push(rHeaderExtension);
  2197. break;
  2198. }
  2199. }
  2200. });
  2201. // FIXME: fecMechanisms
  2202. return commonCapabilities;
  2203. }
  2204. // is action=setLocalDescription with type allowed in signalingState
  2205. function isActionAllowedInSignalingState(action, type, signalingState) {
  2206. return {
  2207. offer: {
  2208. setLocalDescription: ['stable', 'have-local-offer'],
  2209. setRemoteDescription: ['stable', 'have-remote-offer']
  2210. },
  2211. answer: {
  2212. setLocalDescription: ['have-remote-offer', 'have-local-pranswer'],
  2213. setRemoteDescription: ['have-local-offer', 'have-remote-pranswer']
  2214. }
  2215. }[type][action].indexOf(signalingState) !== -1;
  2216. }
  2217. function maybeAddCandidate(iceTransport, candidate) {
  2218. // Edge's internal representation adds some fields therefore
  2219. // not all fieldѕ are taken into account.
  2220. var alreadyAdded = iceTransport.getRemoteCandidates()
  2221. .find(function(remoteCandidate) {
  2222. return candidate.foundation === remoteCandidate.foundation &&
  2223. candidate.ip === remoteCandidate.ip &&
  2224. candidate.port === remoteCandidate.port &&
  2225. candidate.priority === remoteCandidate.priority &&
  2226. candidate.protocol === remoteCandidate.protocol &&
  2227. candidate.type === remoteCandidate.type;
  2228. });
  2229. if (!alreadyAdded) {
  2230. iceTransport.addRemoteCandidate(candidate);
  2231. }
  2232. return !alreadyAdded;
  2233. }
  2234. function makeError(name, description) {
  2235. var e = new Error(description);
  2236. e.name = name;
  2237. // legacy error codes from https://heycam.github.io/webidl/#idl-DOMException-error-names
  2238. e.code = {
  2239. NotSupportedError: 9,
  2240. InvalidStateError: 11,
  2241. InvalidAccessError: 15,
  2242. TypeError: undefined,
  2243. OperationError: undefined
  2244. }[name];
  2245. return e;
  2246. }
  2247. var rtcpeerconnection = function(window, edgeVersion) {
  2248. // https://w3c.github.io/mediacapture-main/#mediastream
  2249. // Helper function to add the track to the stream and
  2250. // dispatch the event ourselves.
  2251. function addTrackToStreamAndFireEvent(track, stream) {
  2252. stream.addTrack(track);
  2253. stream.dispatchEvent(new window.MediaStreamTrackEvent('addtrack',
  2254. {track: track}));
  2255. }
  2256. function removeTrackFromStreamAndFireEvent(track, stream) {
  2257. stream.removeTrack(track);
  2258. stream.dispatchEvent(new window.MediaStreamTrackEvent('removetrack',
  2259. {track: track}));
  2260. }
  2261. function fireAddTrack(pc, track, receiver, streams) {
  2262. var trackEvent = new Event('track');
  2263. trackEvent.track = track;
  2264. trackEvent.receiver = receiver;
  2265. trackEvent.transceiver = {receiver: receiver};
  2266. trackEvent.streams = streams;
  2267. window.setTimeout(function() {
  2268. pc._dispatchEvent('track', trackEvent);
  2269. });
  2270. }
  2271. var RTCPeerConnection = function(config) {
  2272. var pc = this;
  2273. var _eventTarget = document.createDocumentFragment();
  2274. ['addEventListener', 'removeEventListener', 'dispatchEvent']
  2275. .forEach(function(method) {
  2276. pc[method] = _eventTarget[method].bind(_eventTarget);
  2277. });
  2278. this.canTrickleIceCandidates = null;
  2279. this.needNegotiation = false;
  2280. this.localStreams = [];
  2281. this.remoteStreams = [];
  2282. this._localDescription = null;
  2283. this._remoteDescription = null;
  2284. this.signalingState = 'stable';
  2285. this.iceConnectionState = 'new';
  2286. this.connectionState = 'new';
  2287. this.iceGatheringState = 'new';
  2288. config = JSON.parse(JSON.stringify(config || {}));
  2289. this.usingBundle = config.bundlePolicy === 'max-bundle';
  2290. if (config.rtcpMuxPolicy === 'negotiate') {
  2291. throw(makeError('NotSupportedError',
  2292. 'rtcpMuxPolicy \'negotiate\' is not supported'));
  2293. } else if (!config.rtcpMuxPolicy) {
  2294. config.rtcpMuxPolicy = 'require';
  2295. }
  2296. switch (config.iceTransportPolicy) {
  2297. case 'all':
  2298. case 'relay':
  2299. break;
  2300. default:
  2301. config.iceTransportPolicy = 'all';
  2302. break;
  2303. }
  2304. switch (config.bundlePolicy) {
  2305. case 'balanced':
  2306. case 'max-compat':
  2307. case 'max-bundle':
  2308. break;
  2309. default:
  2310. config.bundlePolicy = 'balanced';
  2311. break;
  2312. }
  2313. config.iceServers = filterIceServers(config.iceServers || [], edgeVersion);
  2314. this._iceGatherers = [];
  2315. if (config.iceCandidatePoolSize) {
  2316. for (var i = config.iceCandidatePoolSize; i > 0; i--) {
  2317. this._iceGatherers.push(new window.RTCIceGatherer({
  2318. iceServers: config.iceServers,
  2319. gatherPolicy: config.iceTransportPolicy
  2320. }));
  2321. }
  2322. } else {
  2323. config.iceCandidatePoolSize = 0;
  2324. }
  2325. this._config = config;
  2326. // per-track iceGathers, iceTransports, dtlsTransports, rtpSenders, ...
  2327. // everything that is needed to describe a SDP m-line.
  2328. this.transceivers = [];
  2329. this._sdpSessionId = sdp.generateSessionId();
  2330. this._sdpSessionVersion = 0;
  2331. this._dtlsRole = undefined; // role for a=setup to use in answers.
  2332. this._isClosed = false;
  2333. };
  2334. Object.defineProperty(RTCPeerConnection.prototype, 'localDescription', {
  2335. configurable: true,
  2336. get: function() {
  2337. return this._localDescription;
  2338. }
  2339. });
  2340. Object.defineProperty(RTCPeerConnection.prototype, 'remoteDescription', {
  2341. configurable: true,
  2342. get: function() {
  2343. return this._remoteDescription;
  2344. }
  2345. });
  2346. // set up event handlers on prototype
  2347. RTCPeerConnection.prototype.onicecandidate = null;
  2348. RTCPeerConnection.prototype.onaddstream = null;
  2349. RTCPeerConnection.prototype.ontrack = null;
  2350. RTCPeerConnection.prototype.onremovestream = null;
  2351. RTCPeerConnection.prototype.onsignalingstatechange = null;
  2352. RTCPeerConnection.prototype.oniceconnectionstatechange = null;
  2353. RTCPeerConnection.prototype.onconnectionstatechange = null;
  2354. RTCPeerConnection.prototype.onicegatheringstatechange = null;
  2355. RTCPeerConnection.prototype.onnegotiationneeded = null;
  2356. RTCPeerConnection.prototype.ondatachannel = null;
  2357. RTCPeerConnection.prototype._dispatchEvent = function(name, event) {
  2358. if (this._isClosed) {
  2359. return;
  2360. }
  2361. this.dispatchEvent(event);
  2362. if (typeof this['on' + name] === 'function') {
  2363. this['on' + name](event);
  2364. }
  2365. };
  2366. RTCPeerConnection.prototype._emitGatheringStateChange = function() {
  2367. var event = new Event('icegatheringstatechange');
  2368. this._dispatchEvent('icegatheringstatechange', event);
  2369. };
  2370. RTCPeerConnection.prototype.getConfiguration = function() {
  2371. return this._config;
  2372. };
  2373. RTCPeerConnection.prototype.getLocalStreams = function() {
  2374. return this.localStreams;
  2375. };
  2376. RTCPeerConnection.prototype.getRemoteStreams = function() {
  2377. return this.remoteStreams;
  2378. };
  2379. // internal helper to create a transceiver object.
  2380. // (which is not yet the same as the WebRTC 1.0 transceiver)
  2381. RTCPeerConnection.prototype._createTransceiver = function(kind, doNotAdd) {
  2382. var hasBundleTransport = this.transceivers.length > 0;
  2383. var transceiver = {
  2384. track: null,
  2385. iceGatherer: null,
  2386. iceTransport: null,
  2387. dtlsTransport: null,
  2388. localCapabilities: null,
  2389. remoteCapabilities: null,
  2390. rtpSender: null,
  2391. rtpReceiver: null,
  2392. kind: kind,
  2393. mid: null,
  2394. sendEncodingParameters: null,
  2395. recvEncodingParameters: null,
  2396. stream: null,
  2397. associatedRemoteMediaStreams: [],
  2398. wantReceive: true
  2399. };
  2400. if (this.usingBundle && hasBundleTransport) {
  2401. transceiver.iceTransport = this.transceivers[0].iceTransport;
  2402. transceiver.dtlsTransport = this.transceivers[0].dtlsTransport;
  2403. } else {
  2404. var transports = this._createIceAndDtlsTransports();
  2405. transceiver.iceTransport = transports.iceTransport;
  2406. transceiver.dtlsTransport = transports.dtlsTransport;
  2407. }
  2408. if (!doNotAdd) {
  2409. this.transceivers.push(transceiver);
  2410. }
  2411. return transceiver;
  2412. };
  2413. RTCPeerConnection.prototype.addTrack = function(track, stream) {
  2414. if (this._isClosed) {
  2415. throw makeError('InvalidStateError',
  2416. 'Attempted to call addTrack on a closed peerconnection.');
  2417. }
  2418. var alreadyExists = this.transceivers.find(function(s) {
  2419. return s.track === track;
  2420. });
  2421. if (alreadyExists) {
  2422. throw makeError('InvalidAccessError', 'Track already exists.');
  2423. }
  2424. var transceiver;
  2425. for (var i = 0; i < this.transceivers.length; i++) {
  2426. if (!this.transceivers[i].track &&
  2427. this.transceivers[i].kind === track.kind) {
  2428. transceiver = this.transceivers[i];
  2429. }
  2430. }
  2431. if (!transceiver) {
  2432. transceiver = this._createTransceiver(track.kind);
  2433. }
  2434. this._maybeFireNegotiationNeeded();
  2435. if (this.localStreams.indexOf(stream) === -1) {
  2436. this.localStreams.push(stream);
  2437. }
  2438. transceiver.track = track;
  2439. transceiver.stream = stream;
  2440. transceiver.rtpSender = new window.RTCRtpSender(track,
  2441. transceiver.dtlsTransport);
  2442. return transceiver.rtpSender;
  2443. };
  2444. RTCPeerConnection.prototype.addStream = function(stream) {
  2445. var pc = this;
  2446. if (edgeVersion >= 15025) {
  2447. stream.getTracks().forEach(function(track) {
  2448. pc.addTrack(track, stream);
  2449. });
  2450. } else {
  2451. // Clone is necessary for local demos mostly, attaching directly
  2452. // to two different senders does not work (build 10547).
  2453. // Fixed in 15025 (or earlier)
  2454. var clonedStream = stream.clone();
  2455. stream.getTracks().forEach(function(track, idx) {
  2456. var clonedTrack = clonedStream.getTracks()[idx];
  2457. track.addEventListener('enabled', function(event) {
  2458. clonedTrack.enabled = event.enabled;
  2459. });
  2460. });
  2461. clonedStream.getTracks().forEach(function(track) {
  2462. pc.addTrack(track, clonedStream);
  2463. });
  2464. }
  2465. };
  2466. RTCPeerConnection.prototype.removeTrack = function(sender) {
  2467. if (this._isClosed) {
  2468. throw makeError('InvalidStateError',
  2469. 'Attempted to call removeTrack on a closed peerconnection.');
  2470. }
  2471. if (!(sender instanceof window.RTCRtpSender)) {
  2472. throw new TypeError('Argument 1 of RTCPeerConnection.removeTrack ' +
  2473. 'does not implement interface RTCRtpSender.');
  2474. }
  2475. var transceiver = this.transceivers.find(function(t) {
  2476. return t.rtpSender === sender;
  2477. });
  2478. if (!transceiver) {
  2479. throw makeError('InvalidAccessError',
  2480. 'Sender was not created by this connection.');
  2481. }
  2482. var stream = transceiver.stream;
  2483. transceiver.rtpSender.stop();
  2484. transceiver.rtpSender = null;
  2485. transceiver.track = null;
  2486. transceiver.stream = null;
  2487. // remove the stream from the set of local streams
  2488. var localStreams = this.transceivers.map(function(t) {
  2489. return t.stream;
  2490. });
  2491. if (localStreams.indexOf(stream) === -1 &&
  2492. this.localStreams.indexOf(stream) > -1) {
  2493. this.localStreams.splice(this.localStreams.indexOf(stream), 1);
  2494. }
  2495. this._maybeFireNegotiationNeeded();
  2496. };
  2497. RTCPeerConnection.prototype.removeStream = function(stream) {
  2498. var pc = this;
  2499. stream.getTracks().forEach(function(track) {
  2500. var sender = pc.getSenders().find(function(s) {
  2501. return s.track === track;
  2502. });
  2503. if (sender) {
  2504. pc.removeTrack(sender);
  2505. }
  2506. });
  2507. };
  2508. RTCPeerConnection.prototype.getSenders = function() {
  2509. return this.transceivers.filter(function(transceiver) {
  2510. return !!transceiver.rtpSender;
  2511. })
  2512. .map(function(transceiver) {
  2513. return transceiver.rtpSender;
  2514. });
  2515. };
  2516. RTCPeerConnection.prototype.getReceivers = function() {
  2517. return this.transceivers.filter(function(transceiver) {
  2518. return !!transceiver.rtpReceiver;
  2519. })
  2520. .map(function(transceiver) {
  2521. return transceiver.rtpReceiver;
  2522. });
  2523. };
  2524. RTCPeerConnection.prototype._createIceGatherer = function(sdpMLineIndex,
  2525. usingBundle) {
  2526. var pc = this;
  2527. if (usingBundle && sdpMLineIndex > 0) {
  2528. return this.transceivers[0].iceGatherer;
  2529. } else if (this._iceGatherers.length) {
  2530. return this._iceGatherers.shift();
  2531. }
  2532. var iceGatherer = new window.RTCIceGatherer({
  2533. iceServers: this._config.iceServers,
  2534. gatherPolicy: this._config.iceTransportPolicy
  2535. });
  2536. Object.defineProperty(iceGatherer, 'state',
  2537. {value: 'new', writable: true}
  2538. );
  2539. this.transceivers[sdpMLineIndex].bufferedCandidateEvents = [];
  2540. this.transceivers[sdpMLineIndex].bufferCandidates = function(event) {
  2541. var end = !event.candidate || Object.keys(event.candidate).length === 0;
  2542. // polyfill since RTCIceGatherer.state is not implemented in
  2543. // Edge 10547 yet.
  2544. iceGatherer.state = end ? 'completed' : 'gathering';
  2545. if (pc.transceivers[sdpMLineIndex].bufferedCandidateEvents !== null) {
  2546. pc.transceivers[sdpMLineIndex].bufferedCandidateEvents.push(event);
  2547. }
  2548. };
  2549. iceGatherer.addEventListener('localcandidate',
  2550. this.transceivers[sdpMLineIndex].bufferCandidates);
  2551. return iceGatherer;
  2552. };
  2553. // start gathering from an RTCIceGatherer.
  2554. RTCPeerConnection.prototype._gather = function(mid, sdpMLineIndex) {
  2555. var pc = this;
  2556. var iceGatherer = this.transceivers[sdpMLineIndex].iceGatherer;
  2557. if (iceGatherer.onlocalcandidate) {
  2558. return;
  2559. }
  2560. var bufferedCandidateEvents =
  2561. this.transceivers[sdpMLineIndex].bufferedCandidateEvents;
  2562. this.transceivers[sdpMLineIndex].bufferedCandidateEvents = null;
  2563. iceGatherer.removeEventListener('localcandidate',
  2564. this.transceivers[sdpMLineIndex].bufferCandidates);
  2565. iceGatherer.onlocalcandidate = function(evt) {
  2566. if (pc.usingBundle && sdpMLineIndex > 0) {
  2567. // if we know that we use bundle we can drop candidates with
  2568. // ѕdpMLineIndex > 0. If we don't do this then our state gets
  2569. // confused since we dispose the extra ice gatherer.
  2570. return;
  2571. }
  2572. var event = new Event('icecandidate');
  2573. event.candidate = {sdpMid: mid, sdpMLineIndex: sdpMLineIndex};
  2574. var cand = evt.candidate;
  2575. // Edge emits an empty object for RTCIceCandidateComplete‥
  2576. var end = !cand || Object.keys(cand).length === 0;
  2577. if (end) {
  2578. // polyfill since RTCIceGatherer.state is not implemented in
  2579. // Edge 10547 yet.
  2580. if (iceGatherer.state === 'new' || iceGatherer.state === 'gathering') {
  2581. iceGatherer.state = 'completed';
  2582. }
  2583. } else {
  2584. if (iceGatherer.state === 'new') {
  2585. iceGatherer.state = 'gathering';
  2586. }
  2587. // RTCIceCandidate doesn't have a component, needs to be added
  2588. cand.component = 1;
  2589. // also the usernameFragment. TODO: update SDP to take both variants.
  2590. cand.ufrag = iceGatherer.getLocalParameters().usernameFragment;
  2591. var serializedCandidate = sdp.writeCandidate(cand);
  2592. event.candidate = Object.assign(event.candidate,
  2593. sdp.parseCandidate(serializedCandidate));
  2594. event.candidate.candidate = serializedCandidate;
  2595. event.candidate.toJSON = function() {
  2596. return {
  2597. candidate: event.candidate.candidate,
  2598. sdpMid: event.candidate.sdpMid,
  2599. sdpMLineIndex: event.candidate.sdpMLineIndex,
  2600. usernameFragment: event.candidate.usernameFragment
  2601. };
  2602. };
  2603. }
  2604. // update local description.
  2605. var sections = sdp.getMediaSections(pc._localDescription.sdp);
  2606. if (!end) {
  2607. sections[event.candidate.sdpMLineIndex] +=
  2608. 'a=' + event.candidate.candidate + '\r\n';
  2609. } else {
  2610. sections[event.candidate.sdpMLineIndex] +=
  2611. 'a=end-of-candidates\r\n';
  2612. }
  2613. pc._localDescription.sdp =
  2614. sdp.getDescription(pc._localDescription.sdp) +
  2615. sections.join('');
  2616. var complete = pc.transceivers.every(function(transceiver) {
  2617. return transceiver.iceGatherer &&
  2618. transceiver.iceGatherer.state === 'completed';
  2619. });
  2620. if (pc.iceGatheringState !== 'gathering') {
  2621. pc.iceGatheringState = 'gathering';
  2622. pc._emitGatheringStateChange();
  2623. }
  2624. // Emit candidate. Also emit null candidate when all gatherers are
  2625. // complete.
  2626. if (!end) {
  2627. pc._dispatchEvent('icecandidate', event);
  2628. }
  2629. if (complete) {
  2630. pc._dispatchEvent('icecandidate', new Event('icecandidate'));
  2631. pc.iceGatheringState = 'complete';
  2632. pc._emitGatheringStateChange();
  2633. }
  2634. };
  2635. // emit already gathered candidates.
  2636. window.setTimeout(function() {
  2637. bufferedCandidateEvents.forEach(function(e) {
  2638. iceGatherer.onlocalcandidate(e);
  2639. });
  2640. }, 0);
  2641. };
  2642. // Create ICE transport and DTLS transport.
  2643. RTCPeerConnection.prototype._createIceAndDtlsTransports = function() {
  2644. var pc = this;
  2645. var iceTransport = new window.RTCIceTransport(null);
  2646. iceTransport.onicestatechange = function() {
  2647. pc._updateIceConnectionState();
  2648. pc._updateConnectionState();
  2649. };
  2650. var dtlsTransport = new window.RTCDtlsTransport(iceTransport);
  2651. dtlsTransport.ondtlsstatechange = function() {
  2652. pc._updateConnectionState();
  2653. };
  2654. dtlsTransport.onerror = function() {
  2655. // onerror does not set state to failed by itself.
  2656. Object.defineProperty(dtlsTransport, 'state',
  2657. {value: 'failed', writable: true});
  2658. pc._updateConnectionState();
  2659. };
  2660. return {
  2661. iceTransport: iceTransport,
  2662. dtlsTransport: dtlsTransport
  2663. };
  2664. };
  2665. // Destroy ICE gatherer, ICE transport and DTLS transport.
  2666. // Without triggering the callbacks.
  2667. RTCPeerConnection.prototype._disposeIceAndDtlsTransports = function(
  2668. sdpMLineIndex) {
  2669. var iceGatherer = this.transceivers[sdpMLineIndex].iceGatherer;
  2670. if (iceGatherer) {
  2671. delete iceGatherer.onlocalcandidate;
  2672. delete this.transceivers[sdpMLineIndex].iceGatherer;
  2673. }
  2674. var iceTransport = this.transceivers[sdpMLineIndex].iceTransport;
  2675. if (iceTransport) {
  2676. delete iceTransport.onicestatechange;
  2677. delete this.transceivers[sdpMLineIndex].iceTransport;
  2678. }
  2679. var dtlsTransport = this.transceivers[sdpMLineIndex].dtlsTransport;
  2680. if (dtlsTransport) {
  2681. delete dtlsTransport.ondtlsstatechange;
  2682. delete dtlsTransport.onerror;
  2683. delete this.transceivers[sdpMLineIndex].dtlsTransport;
  2684. }
  2685. };
  2686. // Start the RTP Sender and Receiver for a transceiver.
  2687. RTCPeerConnection.prototype._transceive = function(transceiver,
  2688. send, recv) {
  2689. var params = getCommonCapabilities(transceiver.localCapabilities,
  2690. transceiver.remoteCapabilities);
  2691. if (send && transceiver.rtpSender) {
  2692. params.encodings = transceiver.sendEncodingParameters;
  2693. params.rtcp = {
  2694. cname: sdp.localCName,
  2695. compound: transceiver.rtcpParameters.compound
  2696. };
  2697. if (transceiver.recvEncodingParameters.length) {
  2698. params.rtcp.ssrc = transceiver.recvEncodingParameters[0].ssrc;
  2699. }
  2700. transceiver.rtpSender.send(params);
  2701. }
  2702. if (recv && transceiver.rtpReceiver && params.codecs.length > 0) {
  2703. // remove RTX field in Edge 14942
  2704. if (transceiver.kind === 'video'
  2705. && transceiver.recvEncodingParameters
  2706. && edgeVersion < 15019) {
  2707. transceiver.recvEncodingParameters.forEach(function(p) {
  2708. delete p.rtx;
  2709. });
  2710. }
  2711. if (transceiver.recvEncodingParameters.length) {
  2712. params.encodings = transceiver.recvEncodingParameters;
  2713. } else {
  2714. params.encodings = [{}];
  2715. }
  2716. params.rtcp = {
  2717. compound: transceiver.rtcpParameters.compound
  2718. };
  2719. if (transceiver.rtcpParameters.cname) {
  2720. params.rtcp.cname = transceiver.rtcpParameters.cname;
  2721. }
  2722. if (transceiver.sendEncodingParameters.length) {
  2723. params.rtcp.ssrc = transceiver.sendEncodingParameters[0].ssrc;
  2724. }
  2725. transceiver.rtpReceiver.receive(params);
  2726. }
  2727. };
  2728. RTCPeerConnection.prototype.setLocalDescription = function(description) {
  2729. var pc = this;
  2730. // Note: pranswer is not supported.
  2731. if (['offer', 'answer'].indexOf(description.type) === -1) {
  2732. return Promise.reject(makeError('TypeError',
  2733. 'Unsupported type "' + description.type + '"'));
  2734. }
  2735. if (!isActionAllowedInSignalingState('setLocalDescription',
  2736. description.type, pc.signalingState) || pc._isClosed) {
  2737. return Promise.reject(makeError('InvalidStateError',
  2738. 'Can not set local ' + description.type +
  2739. ' in state ' + pc.signalingState));
  2740. }
  2741. var sections;
  2742. var sessionpart;
  2743. if (description.type === 'offer') {
  2744. // VERY limited support for SDP munging. Limited to:
  2745. // * changing the order of codecs
  2746. sections = sdp.splitSections(description.sdp);
  2747. sessionpart = sections.shift();
  2748. sections.forEach(function(mediaSection, sdpMLineIndex) {
  2749. var caps = sdp.parseRtpParameters(mediaSection);
  2750. pc.transceivers[sdpMLineIndex].localCapabilities = caps;
  2751. });
  2752. pc.transceivers.forEach(function(transceiver, sdpMLineIndex) {
  2753. pc._gather(transceiver.mid, sdpMLineIndex);
  2754. });
  2755. } else if (description.type === 'answer') {
  2756. sections = sdp.splitSections(pc._remoteDescription.sdp);
  2757. sessionpart = sections.shift();
  2758. var isIceLite = sdp.matchPrefix(sessionpart,
  2759. 'a=ice-lite').length > 0;
  2760. sections.forEach(function(mediaSection, sdpMLineIndex) {
  2761. var transceiver = pc.transceivers[sdpMLineIndex];
  2762. var iceGatherer = transceiver.iceGatherer;
  2763. var iceTransport = transceiver.iceTransport;
  2764. var dtlsTransport = transceiver.dtlsTransport;
  2765. var localCapabilities = transceiver.localCapabilities;
  2766. var remoteCapabilities = transceiver.remoteCapabilities;
  2767. // treat bundle-only as not-rejected.
  2768. var rejected = sdp.isRejected(mediaSection) &&
  2769. sdp.matchPrefix(mediaSection, 'a=bundle-only').length === 0;
  2770. if (!rejected && !transceiver.rejected) {
  2771. var remoteIceParameters = sdp.getIceParameters(
  2772. mediaSection, sessionpart);
  2773. var remoteDtlsParameters = sdp.getDtlsParameters(
  2774. mediaSection, sessionpart);
  2775. if (isIceLite) {
  2776. remoteDtlsParameters.role = 'server';
  2777. }
  2778. if (!pc.usingBundle || sdpMLineIndex === 0) {
  2779. pc._gather(transceiver.mid, sdpMLineIndex);
  2780. if (iceTransport.state === 'new') {
  2781. iceTransport.start(iceGatherer, remoteIceParameters,
  2782. isIceLite ? 'controlling' : 'controlled');
  2783. }
  2784. if (dtlsTransport.state === 'new') {
  2785. dtlsTransport.start(remoteDtlsParameters);
  2786. }
  2787. }
  2788. // Calculate intersection of capabilities.
  2789. var params = getCommonCapabilities(localCapabilities,
  2790. remoteCapabilities);
  2791. // Start the RTCRtpSender. The RTCRtpReceiver for this
  2792. // transceiver has already been started in setRemoteDescription.
  2793. pc._transceive(transceiver,
  2794. params.codecs.length > 0,
  2795. false);
  2796. }
  2797. });
  2798. }
  2799. pc._localDescription = {
  2800. type: description.type,
  2801. sdp: description.sdp
  2802. };
  2803. if (description.type === 'offer') {
  2804. pc._updateSignalingState('have-local-offer');
  2805. } else {
  2806. pc._updateSignalingState('stable');
  2807. }
  2808. return Promise.resolve();
  2809. };
  2810. RTCPeerConnection.prototype.setRemoteDescription = function(description) {
  2811. var pc = this;
  2812. // Note: pranswer is not supported.
  2813. if (['offer', 'answer'].indexOf(description.type) === -1) {
  2814. return Promise.reject(makeError('TypeError',
  2815. 'Unsupported type "' + description.type + '"'));
  2816. }
  2817. if (!isActionAllowedInSignalingState('setRemoteDescription',
  2818. description.type, pc.signalingState) || pc._isClosed) {
  2819. return Promise.reject(makeError('InvalidStateError',
  2820. 'Can not set remote ' + description.type +
  2821. ' in state ' + pc.signalingState));
  2822. }
  2823. var streams = {};
  2824. pc.remoteStreams.forEach(function(stream) {
  2825. streams[stream.id] = stream;
  2826. });
  2827. var receiverList = [];
  2828. var sections = sdp.splitSections(description.sdp);
  2829. var sessionpart = sections.shift();
  2830. var isIceLite = sdp.matchPrefix(sessionpart,
  2831. 'a=ice-lite').length > 0;
  2832. var usingBundle = sdp.matchPrefix(sessionpart,
  2833. 'a=group:BUNDLE ').length > 0;
  2834. pc.usingBundle = usingBundle;
  2835. var iceOptions = sdp.matchPrefix(sessionpart,
  2836. 'a=ice-options:')[0];
  2837. if (iceOptions) {
  2838. pc.canTrickleIceCandidates = iceOptions.substr(14).split(' ')
  2839. .indexOf('trickle') >= 0;
  2840. } else {
  2841. pc.canTrickleIceCandidates = false;
  2842. }
  2843. sections.forEach(function(mediaSection, sdpMLineIndex) {
  2844. var lines = sdp.splitLines(mediaSection);
  2845. var kind = sdp.getKind(mediaSection);
  2846. // treat bundle-only as not-rejected.
  2847. var rejected = sdp.isRejected(mediaSection) &&
  2848. sdp.matchPrefix(mediaSection, 'a=bundle-only').length === 0;
  2849. var protocol = lines[0].substr(2).split(' ')[2];
  2850. var direction = sdp.getDirection(mediaSection, sessionpart);
  2851. var remoteMsid = sdp.parseMsid(mediaSection);
  2852. var mid = sdp.getMid(mediaSection) || sdp.generateIdentifier();
  2853. // Reject datachannels which are not implemented yet.
  2854. if (rejected || (kind === 'application' && (protocol === 'DTLS/SCTP' ||
  2855. protocol === 'UDP/DTLS/SCTP'))) {
  2856. // TODO: this is dangerous in the case where a non-rejected m-line
  2857. // becomes rejected.
  2858. pc.transceivers[sdpMLineIndex] = {
  2859. mid: mid,
  2860. kind: kind,
  2861. protocol: protocol,
  2862. rejected: true
  2863. };
  2864. return;
  2865. }
  2866. if (!rejected && pc.transceivers[sdpMLineIndex] &&
  2867. pc.transceivers[sdpMLineIndex].rejected) {
  2868. // recycle a rejected transceiver.
  2869. pc.transceivers[sdpMLineIndex] = pc._createTransceiver(kind, true);
  2870. }
  2871. var transceiver;
  2872. var iceGatherer;
  2873. var iceTransport;
  2874. var dtlsTransport;
  2875. var rtpReceiver;
  2876. var sendEncodingParameters;
  2877. var recvEncodingParameters;
  2878. var localCapabilities;
  2879. var track;
  2880. // FIXME: ensure the mediaSection has rtcp-mux set.
  2881. var remoteCapabilities = sdp.parseRtpParameters(mediaSection);
  2882. var remoteIceParameters;
  2883. var remoteDtlsParameters;
  2884. if (!rejected) {
  2885. remoteIceParameters = sdp.getIceParameters(mediaSection,
  2886. sessionpart);
  2887. remoteDtlsParameters = sdp.getDtlsParameters(mediaSection,
  2888. sessionpart);
  2889. remoteDtlsParameters.role = 'client';
  2890. }
  2891. recvEncodingParameters =
  2892. sdp.parseRtpEncodingParameters(mediaSection);
  2893. var rtcpParameters = sdp.parseRtcpParameters(mediaSection);
  2894. var isComplete = sdp.matchPrefix(mediaSection,
  2895. 'a=end-of-candidates', sessionpart).length > 0;
  2896. var cands = sdp.matchPrefix(mediaSection, 'a=candidate:')
  2897. .map(function(cand) {
  2898. return sdp.parseCandidate(cand);
  2899. })
  2900. .filter(function(cand) {
  2901. return cand.component === 1;
  2902. });
  2903. // Check if we can use BUNDLE and dispose transports.
  2904. if ((description.type === 'offer' || description.type === 'answer') &&
  2905. !rejected && usingBundle && sdpMLineIndex > 0 &&
  2906. pc.transceivers[sdpMLineIndex]) {
  2907. pc._disposeIceAndDtlsTransports(sdpMLineIndex);
  2908. pc.transceivers[sdpMLineIndex].iceGatherer =
  2909. pc.transceivers[0].iceGatherer;
  2910. pc.transceivers[sdpMLineIndex].iceTransport =
  2911. pc.transceivers[0].iceTransport;
  2912. pc.transceivers[sdpMLineIndex].dtlsTransport =
  2913. pc.transceivers[0].dtlsTransport;
  2914. if (pc.transceivers[sdpMLineIndex].rtpSender) {
  2915. pc.transceivers[sdpMLineIndex].rtpSender.setTransport(
  2916. pc.transceivers[0].dtlsTransport);
  2917. }
  2918. if (pc.transceivers[sdpMLineIndex].rtpReceiver) {
  2919. pc.transceivers[sdpMLineIndex].rtpReceiver.setTransport(
  2920. pc.transceivers[0].dtlsTransport);
  2921. }
  2922. }
  2923. if (description.type === 'offer' && !rejected) {
  2924. transceiver = pc.transceivers[sdpMLineIndex] ||
  2925. pc._createTransceiver(kind);
  2926. transceiver.mid = mid;
  2927. if (!transceiver.iceGatherer) {
  2928. transceiver.iceGatherer = pc._createIceGatherer(sdpMLineIndex,
  2929. usingBundle);
  2930. }
  2931. if (cands.length && transceiver.iceTransport.state === 'new') {
  2932. if (isComplete && (!usingBundle || sdpMLineIndex === 0)) {
  2933. transceiver.iceTransport.setRemoteCandidates(cands);
  2934. } else {
  2935. cands.forEach(function(candidate) {
  2936. maybeAddCandidate(transceiver.iceTransport, candidate);
  2937. });
  2938. }
  2939. }
  2940. localCapabilities = window.RTCRtpReceiver.getCapabilities(kind);
  2941. // filter RTX until additional stuff needed for RTX is implemented
  2942. // in adapter.js
  2943. if (edgeVersion < 15019) {
  2944. localCapabilities.codecs = localCapabilities.codecs.filter(
  2945. function(codec) {
  2946. return codec.name !== 'rtx';
  2947. });
  2948. }
  2949. sendEncodingParameters = transceiver.sendEncodingParameters || [{
  2950. ssrc: (2 * sdpMLineIndex + 2) * 1001
  2951. }];
  2952. // TODO: rewrite to use http://w3c.github.io/webrtc-pc/#set-associated-remote-streams
  2953. var isNewTrack = false;
  2954. if (direction === 'sendrecv' || direction === 'sendonly') {
  2955. isNewTrack = !transceiver.rtpReceiver;
  2956. rtpReceiver = transceiver.rtpReceiver ||
  2957. new window.RTCRtpReceiver(transceiver.dtlsTransport, kind);
  2958. if (isNewTrack) {
  2959. var stream;
  2960. track = rtpReceiver.track;
  2961. // FIXME: does not work with Plan B.
  2962. if (remoteMsid && remoteMsid.stream === '-') ; else if (remoteMsid) {
  2963. if (!streams[remoteMsid.stream]) {
  2964. streams[remoteMsid.stream] = new window.MediaStream();
  2965. Object.defineProperty(streams[remoteMsid.stream], 'id', {
  2966. get: function() {
  2967. return remoteMsid.stream;
  2968. }
  2969. });
  2970. }
  2971. Object.defineProperty(track, 'id', {
  2972. get: function() {
  2973. return remoteMsid.track;
  2974. }
  2975. });
  2976. stream = streams[remoteMsid.stream];
  2977. } else {
  2978. if (!streams.default) {
  2979. streams.default = new window.MediaStream();
  2980. }
  2981. stream = streams.default;
  2982. }
  2983. if (stream) {
  2984. addTrackToStreamAndFireEvent(track, stream);
  2985. transceiver.associatedRemoteMediaStreams.push(stream);
  2986. }
  2987. receiverList.push([track, rtpReceiver, stream]);
  2988. }
  2989. } else if (transceiver.rtpReceiver && transceiver.rtpReceiver.track) {
  2990. transceiver.associatedRemoteMediaStreams.forEach(function(s) {
  2991. var nativeTrack = s.getTracks().find(function(t) {
  2992. return t.id === transceiver.rtpReceiver.track.id;
  2993. });
  2994. if (nativeTrack) {
  2995. removeTrackFromStreamAndFireEvent(nativeTrack, s);
  2996. }
  2997. });
  2998. transceiver.associatedRemoteMediaStreams = [];
  2999. }
  3000. transceiver.localCapabilities = localCapabilities;
  3001. transceiver.remoteCapabilities = remoteCapabilities;
  3002. transceiver.rtpReceiver = rtpReceiver;
  3003. transceiver.rtcpParameters = rtcpParameters;
  3004. transceiver.sendEncodingParameters = sendEncodingParameters;
  3005. transceiver.recvEncodingParameters = recvEncodingParameters;
  3006. // Start the RTCRtpReceiver now. The RTPSender is started in
  3007. // setLocalDescription.
  3008. pc._transceive(pc.transceivers[sdpMLineIndex],
  3009. false,
  3010. isNewTrack);
  3011. } else if (description.type === 'answer' && !rejected) {
  3012. transceiver = pc.transceivers[sdpMLineIndex];
  3013. iceGatherer = transceiver.iceGatherer;
  3014. iceTransport = transceiver.iceTransport;
  3015. dtlsTransport = transceiver.dtlsTransport;
  3016. rtpReceiver = transceiver.rtpReceiver;
  3017. sendEncodingParameters = transceiver.sendEncodingParameters;
  3018. localCapabilities = transceiver.localCapabilities;
  3019. pc.transceivers[sdpMLineIndex].recvEncodingParameters =
  3020. recvEncodingParameters;
  3021. pc.transceivers[sdpMLineIndex].remoteCapabilities =
  3022. remoteCapabilities;
  3023. pc.transceivers[sdpMLineIndex].rtcpParameters = rtcpParameters;
  3024. if (cands.length && iceTransport.state === 'new') {
  3025. if ((isIceLite || isComplete) &&
  3026. (!usingBundle || sdpMLineIndex === 0)) {
  3027. iceTransport.setRemoteCandidates(cands);
  3028. } else {
  3029. cands.forEach(function(candidate) {
  3030. maybeAddCandidate(transceiver.iceTransport, candidate);
  3031. });
  3032. }
  3033. }
  3034. if (!usingBundle || sdpMLineIndex === 0) {
  3035. if (iceTransport.state === 'new') {
  3036. iceTransport.start(iceGatherer, remoteIceParameters,
  3037. 'controlling');
  3038. }
  3039. if (dtlsTransport.state === 'new') {
  3040. dtlsTransport.start(remoteDtlsParameters);
  3041. }
  3042. }
  3043. // If the offer contained RTX but the answer did not,
  3044. // remove RTX from sendEncodingParameters.
  3045. var commonCapabilities = getCommonCapabilities(
  3046. transceiver.localCapabilities,
  3047. transceiver.remoteCapabilities);
  3048. var hasRtx = commonCapabilities.codecs.filter(function(c) {
  3049. return c.name.toLowerCase() === 'rtx';
  3050. }).length;
  3051. if (!hasRtx && transceiver.sendEncodingParameters[0].rtx) {
  3052. delete transceiver.sendEncodingParameters[0].rtx;
  3053. }
  3054. pc._transceive(transceiver,
  3055. direction === 'sendrecv' || direction === 'recvonly',
  3056. direction === 'sendrecv' || direction === 'sendonly');
  3057. // TODO: rewrite to use http://w3c.github.io/webrtc-pc/#set-associated-remote-streams
  3058. if (rtpReceiver &&
  3059. (direction === 'sendrecv' || direction === 'sendonly')) {
  3060. track = rtpReceiver.track;
  3061. if (remoteMsid) {
  3062. if (!streams[remoteMsid.stream]) {
  3063. streams[remoteMsid.stream] = new window.MediaStream();
  3064. }
  3065. addTrackToStreamAndFireEvent(track, streams[remoteMsid.stream]);
  3066. receiverList.push([track, rtpReceiver, streams[remoteMsid.stream]]);
  3067. } else {
  3068. if (!streams.default) {
  3069. streams.default = new window.MediaStream();
  3070. }
  3071. addTrackToStreamAndFireEvent(track, streams.default);
  3072. receiverList.push([track, rtpReceiver, streams.default]);
  3073. }
  3074. } else {
  3075. // FIXME: actually the receiver should be created later.
  3076. delete transceiver.rtpReceiver;
  3077. }
  3078. }
  3079. });
  3080. if (pc._dtlsRole === undefined) {
  3081. pc._dtlsRole = description.type === 'offer' ? 'active' : 'passive';
  3082. }
  3083. pc._remoteDescription = {
  3084. type: description.type,
  3085. sdp: description.sdp
  3086. };
  3087. if (description.type === 'offer') {
  3088. pc._updateSignalingState('have-remote-offer');
  3089. } else {
  3090. pc._updateSignalingState('stable');
  3091. }
  3092. Object.keys(streams).forEach(function(sid) {
  3093. var stream = streams[sid];
  3094. if (stream.getTracks().length) {
  3095. if (pc.remoteStreams.indexOf(stream) === -1) {
  3096. pc.remoteStreams.push(stream);
  3097. var event = new Event('addstream');
  3098. event.stream = stream;
  3099. window.setTimeout(function() {
  3100. pc._dispatchEvent('addstream', event);
  3101. });
  3102. }
  3103. receiverList.forEach(function(item) {
  3104. var track = item[0];
  3105. var receiver = item[1];
  3106. if (stream.id !== item[2].id) {
  3107. return;
  3108. }
  3109. fireAddTrack(pc, track, receiver, [stream]);
  3110. });
  3111. }
  3112. });
  3113. receiverList.forEach(function(item) {
  3114. if (item[2]) {
  3115. return;
  3116. }
  3117. fireAddTrack(pc, item[0], item[1], []);
  3118. });
  3119. // check whether addIceCandidate({}) was called within four seconds after
  3120. // setRemoteDescription.
  3121. window.setTimeout(function() {
  3122. if (!(pc && pc.transceivers)) {
  3123. return;
  3124. }
  3125. pc.transceivers.forEach(function(transceiver) {
  3126. if (transceiver.iceTransport &&
  3127. transceiver.iceTransport.state === 'new' &&
  3128. transceiver.iceTransport.getRemoteCandidates().length > 0) {
  3129. console.warn('Timeout for addRemoteCandidate. Consider sending ' +
  3130. 'an end-of-candidates notification');
  3131. transceiver.iceTransport.addRemoteCandidate({});
  3132. }
  3133. });
  3134. }, 4000);
  3135. return Promise.resolve();
  3136. };
  3137. RTCPeerConnection.prototype.close = function() {
  3138. this.transceivers.forEach(function(transceiver) {
  3139. /* not yet
  3140. if (transceiver.iceGatherer) {
  3141. transceiver.iceGatherer.close();
  3142. }
  3143. */
  3144. if (transceiver.iceTransport) {
  3145. transceiver.iceTransport.stop();
  3146. }
  3147. if (transceiver.dtlsTransport) {
  3148. transceiver.dtlsTransport.stop();
  3149. }
  3150. if (transceiver.rtpSender) {
  3151. transceiver.rtpSender.stop();
  3152. }
  3153. if (transceiver.rtpReceiver) {
  3154. transceiver.rtpReceiver.stop();
  3155. }
  3156. });
  3157. // FIXME: clean up tracks, local streams, remote streams, etc
  3158. this._isClosed = true;
  3159. this._updateSignalingState('closed');
  3160. };
  3161. // Update the signaling state.
  3162. RTCPeerConnection.prototype._updateSignalingState = function(newState) {
  3163. this.signalingState = newState;
  3164. var event = new Event('signalingstatechange');
  3165. this._dispatchEvent('signalingstatechange', event);
  3166. };
  3167. // Determine whether to fire the negotiationneeded event.
  3168. RTCPeerConnection.prototype._maybeFireNegotiationNeeded = function() {
  3169. var pc = this;
  3170. if (this.signalingState !== 'stable' || this.needNegotiation === true) {
  3171. return;
  3172. }
  3173. this.needNegotiation = true;
  3174. window.setTimeout(function() {
  3175. if (pc.needNegotiation) {
  3176. pc.needNegotiation = false;
  3177. var event = new Event('negotiationneeded');
  3178. pc._dispatchEvent('negotiationneeded', event);
  3179. }
  3180. }, 0);
  3181. };
  3182. // Update the ice connection state.
  3183. RTCPeerConnection.prototype._updateIceConnectionState = function() {
  3184. var newState;
  3185. var states = {
  3186. 'new': 0,
  3187. closed: 0,
  3188. checking: 0,
  3189. connected: 0,
  3190. completed: 0,
  3191. disconnected: 0,
  3192. failed: 0
  3193. };
  3194. this.transceivers.forEach(function(transceiver) {
  3195. if (transceiver.iceTransport && !transceiver.rejected) {
  3196. states[transceiver.iceTransport.state]++;
  3197. }
  3198. });
  3199. newState = 'new';
  3200. if (states.failed > 0) {
  3201. newState = 'failed';
  3202. } else if (states.checking > 0) {
  3203. newState = 'checking';
  3204. } else if (states.disconnected > 0) {
  3205. newState = 'disconnected';
  3206. } else if (states.new > 0) {
  3207. newState = 'new';
  3208. } else if (states.connected > 0) {
  3209. newState = 'connected';
  3210. } else if (states.completed > 0) {
  3211. newState = 'completed';
  3212. }
  3213. if (newState !== this.iceConnectionState) {
  3214. this.iceConnectionState = newState;
  3215. var event = new Event('iceconnectionstatechange');
  3216. this._dispatchEvent('iceconnectionstatechange', event);
  3217. }
  3218. };
  3219. // Update the connection state.
  3220. RTCPeerConnection.prototype._updateConnectionState = function() {
  3221. var newState;
  3222. var states = {
  3223. 'new': 0,
  3224. closed: 0,
  3225. connecting: 0,
  3226. connected: 0,
  3227. completed: 0,
  3228. disconnected: 0,
  3229. failed: 0
  3230. };
  3231. this.transceivers.forEach(function(transceiver) {
  3232. if (transceiver.iceTransport && transceiver.dtlsTransport &&
  3233. !transceiver.rejected) {
  3234. states[transceiver.iceTransport.state]++;
  3235. states[transceiver.dtlsTransport.state]++;
  3236. }
  3237. });
  3238. // ICETransport.completed and connected are the same for this purpose.
  3239. states.connected += states.completed;
  3240. newState = 'new';
  3241. if (states.failed > 0) {
  3242. newState = 'failed';
  3243. } else if (states.connecting > 0) {
  3244. newState = 'connecting';
  3245. } else if (states.disconnected > 0) {
  3246. newState = 'disconnected';
  3247. } else if (states.new > 0) {
  3248. newState = 'new';
  3249. } else if (states.connected > 0) {
  3250. newState = 'connected';
  3251. }
  3252. if (newState !== this.connectionState) {
  3253. this.connectionState = newState;
  3254. var event = new Event('connectionstatechange');
  3255. this._dispatchEvent('connectionstatechange', event);
  3256. }
  3257. };
  3258. RTCPeerConnection.prototype.createOffer = function() {
  3259. var pc = this;
  3260. if (pc._isClosed) {
  3261. return Promise.reject(makeError('InvalidStateError',
  3262. 'Can not call createOffer after close'));
  3263. }
  3264. var numAudioTracks = pc.transceivers.filter(function(t) {
  3265. return t.kind === 'audio';
  3266. }).length;
  3267. var numVideoTracks = pc.transceivers.filter(function(t) {
  3268. return t.kind === 'video';
  3269. }).length;
  3270. // Determine number of audio and video tracks we need to send/recv.
  3271. var offerOptions = arguments[0];
  3272. if (offerOptions) {
  3273. // Reject Chrome legacy constraints.
  3274. if (offerOptions.mandatory || offerOptions.optional) {
  3275. throw new TypeError(
  3276. 'Legacy mandatory/optional constraints not supported.');
  3277. }
  3278. if (offerOptions.offerToReceiveAudio !== undefined) {
  3279. if (offerOptions.offerToReceiveAudio === true) {
  3280. numAudioTracks = 1;
  3281. } else if (offerOptions.offerToReceiveAudio === false) {
  3282. numAudioTracks = 0;
  3283. } else {
  3284. numAudioTracks = offerOptions.offerToReceiveAudio;
  3285. }
  3286. }
  3287. if (offerOptions.offerToReceiveVideo !== undefined) {
  3288. if (offerOptions.offerToReceiveVideo === true) {
  3289. numVideoTracks = 1;
  3290. } else if (offerOptions.offerToReceiveVideo === false) {
  3291. numVideoTracks = 0;
  3292. } else {
  3293. numVideoTracks = offerOptions.offerToReceiveVideo;
  3294. }
  3295. }
  3296. }
  3297. pc.transceivers.forEach(function(transceiver) {
  3298. if (transceiver.kind === 'audio') {
  3299. numAudioTracks--;
  3300. if (numAudioTracks < 0) {
  3301. transceiver.wantReceive = false;
  3302. }
  3303. } else if (transceiver.kind === 'video') {
  3304. numVideoTracks--;
  3305. if (numVideoTracks < 0) {
  3306. transceiver.wantReceive = false;
  3307. }
  3308. }
  3309. });
  3310. // Create M-lines for recvonly streams.
  3311. while (numAudioTracks > 0 || numVideoTracks > 0) {
  3312. if (numAudioTracks > 0) {
  3313. pc._createTransceiver('audio');
  3314. numAudioTracks--;
  3315. }
  3316. if (numVideoTracks > 0) {
  3317. pc._createTransceiver('video');
  3318. numVideoTracks--;
  3319. }
  3320. }
  3321. var sdp$1 = sdp.writeSessionBoilerplate(pc._sdpSessionId,
  3322. pc._sdpSessionVersion++);
  3323. pc.transceivers.forEach(function(transceiver, sdpMLineIndex) {
  3324. // For each track, create an ice gatherer, ice transport,
  3325. // dtls transport, potentially rtpsender and rtpreceiver.
  3326. var track = transceiver.track;
  3327. var kind = transceiver.kind;
  3328. var mid = transceiver.mid || sdp.generateIdentifier();
  3329. transceiver.mid = mid;
  3330. if (!transceiver.iceGatherer) {
  3331. transceiver.iceGatherer = pc._createIceGatherer(sdpMLineIndex,
  3332. pc.usingBundle);
  3333. }
  3334. var localCapabilities = window.RTCRtpSender.getCapabilities(kind);
  3335. // filter RTX until additional stuff needed for RTX is implemented
  3336. // in adapter.js
  3337. if (edgeVersion < 15019) {
  3338. localCapabilities.codecs = localCapabilities.codecs.filter(
  3339. function(codec) {
  3340. return codec.name !== 'rtx';
  3341. });
  3342. }
  3343. localCapabilities.codecs.forEach(function(codec) {
  3344. // work around https://bugs.chromium.org/p/webrtc/issues/detail?id=6552
  3345. // by adding level-asymmetry-allowed=1
  3346. if (codec.name === 'H264' &&
  3347. codec.parameters['level-asymmetry-allowed'] === undefined) {
  3348. codec.parameters['level-asymmetry-allowed'] = '1';
  3349. }
  3350. // for subsequent offers, we might have to re-use the payload
  3351. // type of the last offer.
  3352. if (transceiver.remoteCapabilities &&
  3353. transceiver.remoteCapabilities.codecs) {
  3354. transceiver.remoteCapabilities.codecs.forEach(function(remoteCodec) {
  3355. if (codec.name.toLowerCase() === remoteCodec.name.toLowerCase() &&
  3356. codec.clockRate === remoteCodec.clockRate) {
  3357. codec.preferredPayloadType = remoteCodec.payloadType;
  3358. }
  3359. });
  3360. }
  3361. });
  3362. localCapabilities.headerExtensions.forEach(function(hdrExt) {
  3363. var remoteExtensions = transceiver.remoteCapabilities &&
  3364. transceiver.remoteCapabilities.headerExtensions || [];
  3365. remoteExtensions.forEach(function(rHdrExt) {
  3366. if (hdrExt.uri === rHdrExt.uri) {
  3367. hdrExt.id = rHdrExt.id;
  3368. }
  3369. });
  3370. });
  3371. // generate an ssrc now, to be used later in rtpSender.send
  3372. var sendEncodingParameters = transceiver.sendEncodingParameters || [{
  3373. ssrc: (2 * sdpMLineIndex + 1) * 1001
  3374. }];
  3375. if (track) {
  3376. // add RTX
  3377. if (edgeVersion >= 15019 && kind === 'video' &&
  3378. !sendEncodingParameters[0].rtx) {
  3379. sendEncodingParameters[0].rtx = {
  3380. ssrc: sendEncodingParameters[0].ssrc + 1
  3381. };
  3382. }
  3383. }
  3384. if (transceiver.wantReceive) {
  3385. transceiver.rtpReceiver = new window.RTCRtpReceiver(
  3386. transceiver.dtlsTransport, kind);
  3387. }
  3388. transceiver.localCapabilities = localCapabilities;
  3389. transceiver.sendEncodingParameters = sendEncodingParameters;
  3390. });
  3391. // always offer BUNDLE and dispose on return if not supported.
  3392. if (pc._config.bundlePolicy !== 'max-compat') {
  3393. sdp$1 += 'a=group:BUNDLE ' + pc.transceivers.map(function(t) {
  3394. return t.mid;
  3395. }).join(' ') + '\r\n';
  3396. }
  3397. sdp$1 += 'a=ice-options:trickle\r\n';
  3398. pc.transceivers.forEach(function(transceiver, sdpMLineIndex) {
  3399. sdp$1 += writeMediaSection(transceiver, transceiver.localCapabilities,
  3400. 'offer', transceiver.stream, pc._dtlsRole);
  3401. sdp$1 += 'a=rtcp-rsize\r\n';
  3402. if (transceiver.iceGatherer && pc.iceGatheringState !== 'new' &&
  3403. (sdpMLineIndex === 0 || !pc.usingBundle)) {
  3404. transceiver.iceGatherer.getLocalCandidates().forEach(function(cand) {
  3405. cand.component = 1;
  3406. sdp$1 += 'a=' + sdp.writeCandidate(cand) + '\r\n';
  3407. });
  3408. if (transceiver.iceGatherer.state === 'completed') {
  3409. sdp$1 += 'a=end-of-candidates\r\n';
  3410. }
  3411. }
  3412. });
  3413. var desc = new window.RTCSessionDescription({
  3414. type: 'offer',
  3415. sdp: sdp$1
  3416. });
  3417. return Promise.resolve(desc);
  3418. };
  3419. RTCPeerConnection.prototype.createAnswer = function() {
  3420. var pc = this;
  3421. if (pc._isClosed) {
  3422. return Promise.reject(makeError('InvalidStateError',
  3423. 'Can not call createAnswer after close'));
  3424. }
  3425. if (!(pc.signalingState === 'have-remote-offer' ||
  3426. pc.signalingState === 'have-local-pranswer')) {
  3427. return Promise.reject(makeError('InvalidStateError',
  3428. 'Can not call createAnswer in signalingState ' + pc.signalingState));
  3429. }
  3430. var sdp$1 = sdp.writeSessionBoilerplate(pc._sdpSessionId,
  3431. pc._sdpSessionVersion++);
  3432. if (pc.usingBundle) {
  3433. sdp$1 += 'a=group:BUNDLE ' + pc.transceivers.map(function(t) {
  3434. return t.mid;
  3435. }).join(' ') + '\r\n';
  3436. }
  3437. sdp$1 += 'a=ice-options:trickle\r\n';
  3438. var mediaSectionsInOffer = sdp.getMediaSections(
  3439. pc._remoteDescription.sdp).length;
  3440. pc.transceivers.forEach(function(transceiver, sdpMLineIndex) {
  3441. if (sdpMLineIndex + 1 > mediaSectionsInOffer) {
  3442. return;
  3443. }
  3444. if (transceiver.rejected) {
  3445. if (transceiver.kind === 'application') {
  3446. if (transceiver.protocol === 'DTLS/SCTP') { // legacy fmt
  3447. sdp$1 += 'm=application 0 DTLS/SCTP 5000\r\n';
  3448. } else {
  3449. sdp$1 += 'm=application 0 ' + transceiver.protocol +
  3450. ' webrtc-datachannel\r\n';
  3451. }
  3452. } else if (transceiver.kind === 'audio') {
  3453. sdp$1 += 'm=audio 0 UDP/TLS/RTP/SAVPF 0\r\n' +
  3454. 'a=rtpmap:0 PCMU/8000\r\n';
  3455. } else if (transceiver.kind === 'video') {
  3456. sdp$1 += 'm=video 0 UDP/TLS/RTP/SAVPF 120\r\n' +
  3457. 'a=rtpmap:120 VP8/90000\r\n';
  3458. }
  3459. sdp$1 += 'c=IN IP4 0.0.0.0\r\n' +
  3460. 'a=inactive\r\n' +
  3461. 'a=mid:' + transceiver.mid + '\r\n';
  3462. return;
  3463. }
  3464. // FIXME: look at direction.
  3465. if (transceiver.stream) {
  3466. var localTrack;
  3467. if (transceiver.kind === 'audio') {
  3468. localTrack = transceiver.stream.getAudioTracks()[0];
  3469. } else if (transceiver.kind === 'video') {
  3470. localTrack = transceiver.stream.getVideoTracks()[0];
  3471. }
  3472. if (localTrack) {
  3473. // add RTX
  3474. if (edgeVersion >= 15019 && transceiver.kind === 'video' &&
  3475. !transceiver.sendEncodingParameters[0].rtx) {
  3476. transceiver.sendEncodingParameters[0].rtx = {
  3477. ssrc: transceiver.sendEncodingParameters[0].ssrc + 1
  3478. };
  3479. }
  3480. }
  3481. }
  3482. // Calculate intersection of capabilities.
  3483. var commonCapabilities = getCommonCapabilities(
  3484. transceiver.localCapabilities,
  3485. transceiver.remoteCapabilities);
  3486. var hasRtx = commonCapabilities.codecs.filter(function(c) {
  3487. return c.name.toLowerCase() === 'rtx';
  3488. }).length;
  3489. if (!hasRtx && transceiver.sendEncodingParameters[0].rtx) {
  3490. delete transceiver.sendEncodingParameters[0].rtx;
  3491. }
  3492. sdp$1 += writeMediaSection(transceiver, commonCapabilities,
  3493. 'answer', transceiver.stream, pc._dtlsRole);
  3494. if (transceiver.rtcpParameters &&
  3495. transceiver.rtcpParameters.reducedSize) {
  3496. sdp$1 += 'a=rtcp-rsize\r\n';
  3497. }
  3498. });
  3499. var desc = new window.RTCSessionDescription({
  3500. type: 'answer',
  3501. sdp: sdp$1
  3502. });
  3503. return Promise.resolve(desc);
  3504. };
  3505. RTCPeerConnection.prototype.addIceCandidate = function(candidate) {
  3506. var pc = this;
  3507. var sections;
  3508. if (candidate && !(candidate.sdpMLineIndex !== undefined ||
  3509. candidate.sdpMid)) {
  3510. return Promise.reject(new TypeError('sdpMLineIndex or sdpMid required'));
  3511. }
  3512. // TODO: needs to go into ops queue.
  3513. return new Promise(function(resolve, reject) {
  3514. if (!pc._remoteDescription) {
  3515. return reject(makeError('InvalidStateError',
  3516. 'Can not add ICE candidate without a remote description'));
  3517. } else if (!candidate || candidate.candidate === '') {
  3518. for (var j = 0; j < pc.transceivers.length; j++) {
  3519. if (pc.transceivers[j].rejected) {
  3520. continue;
  3521. }
  3522. pc.transceivers[j].iceTransport.addRemoteCandidate({});
  3523. sections = sdp.getMediaSections(pc._remoteDescription.sdp);
  3524. sections[j] += 'a=end-of-candidates\r\n';
  3525. pc._remoteDescription.sdp =
  3526. sdp.getDescription(pc._remoteDescription.sdp) +
  3527. sections.join('');
  3528. if (pc.usingBundle) {
  3529. break;
  3530. }
  3531. }
  3532. } else {
  3533. var sdpMLineIndex = candidate.sdpMLineIndex;
  3534. if (candidate.sdpMid) {
  3535. for (var i = 0; i < pc.transceivers.length; i++) {
  3536. if (pc.transceivers[i].mid === candidate.sdpMid) {
  3537. sdpMLineIndex = i;
  3538. break;
  3539. }
  3540. }
  3541. }
  3542. var transceiver = pc.transceivers[sdpMLineIndex];
  3543. if (transceiver) {
  3544. if (transceiver.rejected) {
  3545. return resolve();
  3546. }
  3547. var cand = Object.keys(candidate.candidate).length > 0 ?
  3548. sdp.parseCandidate(candidate.candidate) : {};
  3549. // Ignore Chrome's invalid candidates since Edge does not like them.
  3550. if (cand.protocol === 'tcp' && (cand.port === 0 || cand.port === 9)) {
  3551. return resolve();
  3552. }
  3553. // Ignore RTCP candidates, we assume RTCP-MUX.
  3554. if (cand.component && cand.component !== 1) {
  3555. return resolve();
  3556. }
  3557. // when using bundle, avoid adding candidates to the wrong
  3558. // ice transport. And avoid adding candidates added in the SDP.
  3559. if (sdpMLineIndex === 0 || (sdpMLineIndex > 0 &&
  3560. transceiver.iceTransport !== pc.transceivers[0].iceTransport)) {
  3561. if (!maybeAddCandidate(transceiver.iceTransport, cand)) {
  3562. return reject(makeError('OperationError',
  3563. 'Can not add ICE candidate'));
  3564. }
  3565. }
  3566. // update the remoteDescription.
  3567. var candidateString = candidate.candidate.trim();
  3568. if (candidateString.indexOf('a=') === 0) {
  3569. candidateString = candidateString.substr(2);
  3570. }
  3571. sections = sdp.getMediaSections(pc._remoteDescription.sdp);
  3572. sections[sdpMLineIndex] += 'a=' +
  3573. (cand.type ? candidateString : 'end-of-candidates')
  3574. + '\r\n';
  3575. pc._remoteDescription.sdp =
  3576. sdp.getDescription(pc._remoteDescription.sdp) +
  3577. sections.join('');
  3578. } else {
  3579. return reject(makeError('OperationError',
  3580. 'Can not add ICE candidate'));
  3581. }
  3582. }
  3583. resolve();
  3584. });
  3585. };
  3586. RTCPeerConnection.prototype.getStats = function(selector) {
  3587. if (selector && selector instanceof window.MediaStreamTrack) {
  3588. var senderOrReceiver = null;
  3589. this.transceivers.forEach(function(transceiver) {
  3590. if (transceiver.rtpSender &&
  3591. transceiver.rtpSender.track === selector) {
  3592. senderOrReceiver = transceiver.rtpSender;
  3593. } else if (transceiver.rtpReceiver &&
  3594. transceiver.rtpReceiver.track === selector) {
  3595. senderOrReceiver = transceiver.rtpReceiver;
  3596. }
  3597. });
  3598. if (!senderOrReceiver) {
  3599. throw makeError('InvalidAccessError', 'Invalid selector.');
  3600. }
  3601. return senderOrReceiver.getStats();
  3602. }
  3603. var promises = [];
  3604. this.transceivers.forEach(function(transceiver) {
  3605. ['rtpSender', 'rtpReceiver', 'iceGatherer', 'iceTransport',
  3606. 'dtlsTransport'].forEach(function(method) {
  3607. if (transceiver[method]) {
  3608. promises.push(transceiver[method].getStats());
  3609. }
  3610. });
  3611. });
  3612. return Promise.all(promises).then(function(allStats) {
  3613. var results = new Map();
  3614. allStats.forEach(function(stats) {
  3615. stats.forEach(function(stat) {
  3616. results.set(stat.id, stat);
  3617. });
  3618. });
  3619. return results;
  3620. });
  3621. };
  3622. // fix low-level stat names and return Map instead of object.
  3623. var ortcObjects = ['RTCRtpSender', 'RTCRtpReceiver', 'RTCIceGatherer',
  3624. 'RTCIceTransport', 'RTCDtlsTransport'];
  3625. ortcObjects.forEach(function(ortcObjectName) {
  3626. var obj = window[ortcObjectName];
  3627. if (obj && obj.prototype && obj.prototype.getStats) {
  3628. var nativeGetstats = obj.prototype.getStats;
  3629. obj.prototype.getStats = function() {
  3630. return nativeGetstats.apply(this)
  3631. .then(function(nativeStats) {
  3632. var mapStats = new Map();
  3633. Object.keys(nativeStats).forEach(function(id) {
  3634. nativeStats[id].type = fixStatsType(nativeStats[id]);
  3635. mapStats.set(id, nativeStats[id]);
  3636. });
  3637. return mapStats;
  3638. });
  3639. };
  3640. }
  3641. });
  3642. // legacy callback shims. Should be moved to adapter.js some days.
  3643. var methods = ['createOffer', 'createAnswer'];
  3644. methods.forEach(function(method) {
  3645. var nativeMethod = RTCPeerConnection.prototype[method];
  3646. RTCPeerConnection.prototype[method] = function() {
  3647. var args = arguments;
  3648. if (typeof args[0] === 'function' ||
  3649. typeof args[1] === 'function') { // legacy
  3650. return nativeMethod.apply(this, [arguments[2]])
  3651. .then(function(description) {
  3652. if (typeof args[0] === 'function') {
  3653. args[0].apply(null, [description]);
  3654. }
  3655. }, function(error) {
  3656. if (typeof args[1] === 'function') {
  3657. args[1].apply(null, [error]);
  3658. }
  3659. });
  3660. }
  3661. return nativeMethod.apply(this, arguments);
  3662. };
  3663. });
  3664. methods = ['setLocalDescription', 'setRemoteDescription', 'addIceCandidate'];
  3665. methods.forEach(function(method) {
  3666. var nativeMethod = RTCPeerConnection.prototype[method];
  3667. RTCPeerConnection.prototype[method] = function() {
  3668. var args = arguments;
  3669. if (typeof args[1] === 'function' ||
  3670. typeof args[2] === 'function') { // legacy
  3671. return nativeMethod.apply(this, arguments)
  3672. .then(function() {
  3673. if (typeof args[1] === 'function') {
  3674. args[1].apply(null);
  3675. }
  3676. }, function(error) {
  3677. if (typeof args[2] === 'function') {
  3678. args[2].apply(null, [error]);
  3679. }
  3680. });
  3681. }
  3682. return nativeMethod.apply(this, arguments);
  3683. };
  3684. });
  3685. // getStats is special. It doesn't have a spec legacy method yet we support
  3686. // getStats(something, cb) without error callbacks.
  3687. ['getStats'].forEach(function(method) {
  3688. var nativeMethod = RTCPeerConnection.prototype[method];
  3689. RTCPeerConnection.prototype[method] = function() {
  3690. var args = arguments;
  3691. if (typeof args[1] === 'function') {
  3692. return nativeMethod.apply(this, arguments)
  3693. .then(function() {
  3694. if (typeof args[1] === 'function') {
  3695. args[1].apply(null);
  3696. }
  3697. });
  3698. }
  3699. return nativeMethod.apply(this, arguments);
  3700. };
  3701. });
  3702. return RTCPeerConnection;
  3703. };
  3704. /*
  3705. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  3706. *
  3707. * Use of this source code is governed by a BSD-style license
  3708. * that can be found in the LICENSE file in the root of the source
  3709. * tree.
  3710. */
  3711. function shimGetUserMedia$2(window) {
  3712. const navigator = window && window.navigator;
  3713. const shimError_ = function(e) {
  3714. return {
  3715. name: {PermissionDeniedError: 'NotAllowedError'}[e.name] || e.name,
  3716. message: e.message,
  3717. constraint: e.constraint,
  3718. toString() {
  3719. return this.name;
  3720. }
  3721. };
  3722. };
  3723. // getUserMedia error shim.
  3724. const origGetUserMedia = navigator.mediaDevices.getUserMedia.
  3725. bind(navigator.mediaDevices);
  3726. navigator.mediaDevices.getUserMedia = function(c) {
  3727. return origGetUserMedia(c).catch(e => Promise.reject(shimError_(e)));
  3728. };
  3729. }
  3730. /*
  3731. * Copyright (c) 2018 The adapter.js project authors. All Rights Reserved.
  3732. *
  3733. * Use of this source code is governed by a BSD-style license
  3734. * that can be found in the LICENSE file in the root of the source
  3735. * tree.
  3736. */
  3737. function shimGetDisplayMedia$1(window) {
  3738. if (!('getDisplayMedia' in window.navigator)) {
  3739. return;
  3740. }
  3741. if (!(window.navigator.mediaDevices)) {
  3742. return;
  3743. }
  3744. if (window.navigator.mediaDevices &&
  3745. 'getDisplayMedia' in window.navigator.mediaDevices) {
  3746. return;
  3747. }
  3748. window.navigator.mediaDevices.getDisplayMedia =
  3749. window.navigator.getDisplayMedia.bind(window.navigator);
  3750. }
  3751. /*
  3752. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  3753. *
  3754. * Use of this source code is governed by a BSD-style license
  3755. * that can be found in the LICENSE file in the root of the source
  3756. * tree.
  3757. */
  3758. function shimPeerConnection$1(window, browserDetails) {
  3759. if (window.RTCIceGatherer) {
  3760. if (!window.RTCIceCandidate) {
  3761. window.RTCIceCandidate = function RTCIceCandidate(args) {
  3762. return args;
  3763. };
  3764. }
  3765. if (!window.RTCSessionDescription) {
  3766. window.RTCSessionDescription = function RTCSessionDescription(args) {
  3767. return args;
  3768. };
  3769. }
  3770. // this adds an additional event listener to MediaStrackTrack that signals
  3771. // when a tracks enabled property was changed. Workaround for a bug in
  3772. // addStream, see below. No longer required in 15025+
  3773. if (browserDetails.version < 15025) {
  3774. const origMSTEnabled = Object.getOwnPropertyDescriptor(
  3775. window.MediaStreamTrack.prototype, 'enabled');
  3776. Object.defineProperty(window.MediaStreamTrack.prototype, 'enabled', {
  3777. set(value) {
  3778. origMSTEnabled.set.call(this, value);
  3779. const ev = new Event('enabled');
  3780. ev.enabled = value;
  3781. this.dispatchEvent(ev);
  3782. }
  3783. });
  3784. }
  3785. }
  3786. // ORTC defines the DTMF sender a bit different.
  3787. // https://github.com/w3c/ortc/issues/714
  3788. if (window.RTCRtpSender && !('dtmf' in window.RTCRtpSender.prototype)) {
  3789. Object.defineProperty(window.RTCRtpSender.prototype, 'dtmf', {
  3790. get() {
  3791. if (this._dtmf === undefined) {
  3792. if (this.track.kind === 'audio') {
  3793. this._dtmf = new window.RTCDtmfSender(this);
  3794. } else if (this.track.kind === 'video') {
  3795. this._dtmf = null;
  3796. }
  3797. }
  3798. return this._dtmf;
  3799. }
  3800. });
  3801. }
  3802. // Edge currently only implements the RTCDtmfSender, not the
  3803. // RTCDTMFSender alias. See http://draft.ortc.org/#rtcdtmfsender2*
  3804. if (window.RTCDtmfSender && !window.RTCDTMFSender) {
  3805. window.RTCDTMFSender = window.RTCDtmfSender;
  3806. }
  3807. const RTCPeerConnectionShim = rtcpeerconnection(window,
  3808. browserDetails.version);
  3809. window.RTCPeerConnection = function RTCPeerConnection(config) {
  3810. if (config && config.iceServers) {
  3811. config.iceServers = filterIceServers$1(config.iceServers,
  3812. browserDetails.version);
  3813. log$1('ICE servers after filtering:', config.iceServers);
  3814. }
  3815. return new RTCPeerConnectionShim(config);
  3816. };
  3817. window.RTCPeerConnection.prototype = RTCPeerConnectionShim.prototype;
  3818. }
  3819. function shimReplaceTrack(window) {
  3820. // ORTC has replaceTrack -- https://github.com/w3c/ortc/issues/614
  3821. if (window.RTCRtpSender &&
  3822. !('replaceTrack' in window.RTCRtpSender.prototype)) {
  3823. window.RTCRtpSender.prototype.replaceTrack =
  3824. window.RTCRtpSender.prototype.setTrack;
  3825. }
  3826. }
  3827. var edgeShim = /*#__PURE__*/Object.freeze({
  3828. __proto__: null,
  3829. shimPeerConnection: shimPeerConnection$1,
  3830. shimReplaceTrack: shimReplaceTrack,
  3831. shimGetUserMedia: shimGetUserMedia$2,
  3832. shimGetDisplayMedia: shimGetDisplayMedia$1
  3833. });
  3834. /*
  3835. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  3836. *
  3837. * Use of this source code is governed by a BSD-style license
  3838. * that can be found in the LICENSE file in the root of the source
  3839. * tree.
  3840. */
  3841. function shimGetUserMedia$1(window, browserDetails) {
  3842. const navigator = window && window.navigator;
  3843. const MediaStreamTrack = window && window.MediaStreamTrack;
  3844. navigator.getUserMedia = function(constraints, onSuccess, onError) {
  3845. // Replace Firefox 44+'s deprecation warning with unprefixed version.
  3846. deprecated('navigator.getUserMedia',
  3847. 'navigator.mediaDevices.getUserMedia');
  3848. navigator.mediaDevices.getUserMedia(constraints).then(onSuccess, onError);
  3849. };
  3850. if (!(browserDetails.version > 55 &&
  3851. 'autoGainControl' in navigator.mediaDevices.getSupportedConstraints())) {
  3852. const remap = function(obj, a, b) {
  3853. if (a in obj && !(b in obj)) {
  3854. obj[b] = obj[a];
  3855. delete obj[a];
  3856. }
  3857. };
  3858. const nativeGetUserMedia = navigator.mediaDevices.getUserMedia.
  3859. bind(navigator.mediaDevices);
  3860. navigator.mediaDevices.getUserMedia = function(c) {
  3861. if (typeof c === 'object' && typeof c.audio === 'object') {
  3862. c = JSON.parse(JSON.stringify(c));
  3863. remap(c.audio, 'autoGainControl', 'mozAutoGainControl');
  3864. remap(c.audio, 'noiseSuppression', 'mozNoiseSuppression');
  3865. }
  3866. return nativeGetUserMedia(c);
  3867. };
  3868. if (MediaStreamTrack && MediaStreamTrack.prototype.getSettings) {
  3869. const nativeGetSettings = MediaStreamTrack.prototype.getSettings;
  3870. MediaStreamTrack.prototype.getSettings = function() {
  3871. const obj = nativeGetSettings.apply(this, arguments);
  3872. remap(obj, 'mozAutoGainControl', 'autoGainControl');
  3873. remap(obj, 'mozNoiseSuppression', 'noiseSuppression');
  3874. return obj;
  3875. };
  3876. }
  3877. if (MediaStreamTrack && MediaStreamTrack.prototype.applyConstraints) {
  3878. const nativeApplyConstraints =
  3879. MediaStreamTrack.prototype.applyConstraints;
  3880. MediaStreamTrack.prototype.applyConstraints = function(c) {
  3881. if (this.kind === 'audio' && typeof c === 'object') {
  3882. c = JSON.parse(JSON.stringify(c));
  3883. remap(c, 'autoGainControl', 'mozAutoGainControl');
  3884. remap(c, 'noiseSuppression', 'mozNoiseSuppression');
  3885. }
  3886. return nativeApplyConstraints.apply(this, [c]);
  3887. };
  3888. }
  3889. }
  3890. }
  3891. /*
  3892. * Copyright (c) 2018 The adapter.js project authors. All Rights Reserved.
  3893. *
  3894. * Use of this source code is governed by a BSD-style license
  3895. * that can be found in the LICENSE file in the root of the source
  3896. * tree.
  3897. */
  3898. function shimGetDisplayMedia(window, preferredMediaSource) {
  3899. if (window.navigator.mediaDevices &&
  3900. 'getDisplayMedia' in window.navigator.mediaDevices) {
  3901. return;
  3902. }
  3903. if (!(window.navigator.mediaDevices)) {
  3904. return;
  3905. }
  3906. window.navigator.mediaDevices.getDisplayMedia =
  3907. function getDisplayMedia(constraints) {
  3908. if (!(constraints && constraints.video)) {
  3909. const err = new DOMException('getDisplayMedia without video ' +
  3910. 'constraints is undefined');
  3911. err.name = 'NotFoundError';
  3912. // from https://heycam.github.io/webidl/#idl-DOMException-error-names
  3913. err.code = 8;
  3914. return Promise.reject(err);
  3915. }
  3916. if (constraints.video === true) {
  3917. constraints.video = {mediaSource: preferredMediaSource};
  3918. } else {
  3919. constraints.video.mediaSource = preferredMediaSource;
  3920. }
  3921. return window.navigator.mediaDevices.getUserMedia(constraints);
  3922. };
  3923. }
  3924. /*
  3925. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  3926. *
  3927. * Use of this source code is governed by a BSD-style license
  3928. * that can be found in the LICENSE file in the root of the source
  3929. * tree.
  3930. */
  3931. function shimOnTrack(window) {
  3932. if (typeof window === 'object' && window.RTCTrackEvent &&
  3933. ('receiver' in window.RTCTrackEvent.prototype) &&
  3934. !('transceiver' in window.RTCTrackEvent.prototype)) {
  3935. Object.defineProperty(window.RTCTrackEvent.prototype, 'transceiver', {
  3936. get() {
  3937. return {receiver: this.receiver};
  3938. }
  3939. });
  3940. }
  3941. }
  3942. function shimPeerConnection(window, browserDetails) {
  3943. if (typeof window !== 'object' ||
  3944. !(window.RTCPeerConnection || window.mozRTCPeerConnection)) {
  3945. return; // probably media.peerconnection.enabled=false in about:config
  3946. }
  3947. if (!window.RTCPeerConnection && window.mozRTCPeerConnection) {
  3948. // very basic support for old versions.
  3949. window.RTCPeerConnection = window.mozRTCPeerConnection;
  3950. }
  3951. if (browserDetails.version < 53) {
  3952. // shim away need for obsolete RTCIceCandidate/RTCSessionDescription.
  3953. ['setLocalDescription', 'setRemoteDescription', 'addIceCandidate']
  3954. .forEach(function(method) {
  3955. const nativeMethod = window.RTCPeerConnection.prototype[method];
  3956. const methodObj = {[method]() {
  3957. arguments[0] = new ((method === 'addIceCandidate') ?
  3958. window.RTCIceCandidate :
  3959. window.RTCSessionDescription)(arguments[0]);
  3960. return nativeMethod.apply(this, arguments);
  3961. }};
  3962. window.RTCPeerConnection.prototype[method] = methodObj[method];
  3963. });
  3964. }
  3965. const modernStatsTypes = {
  3966. inboundrtp: 'inbound-rtp',
  3967. outboundrtp: 'outbound-rtp',
  3968. candidatepair: 'candidate-pair',
  3969. localcandidate: 'local-candidate',
  3970. remotecandidate: 'remote-candidate'
  3971. };
  3972. const nativeGetStats = window.RTCPeerConnection.prototype.getStats;
  3973. window.RTCPeerConnection.prototype.getStats = function getStats() {
  3974. const [selector, onSucc, onErr] = arguments;
  3975. return nativeGetStats.apply(this, [selector || null])
  3976. .then(stats => {
  3977. if (browserDetails.version < 53 && !onSucc) {
  3978. // Shim only promise getStats with spec-hyphens in type names
  3979. // Leave callback version alone; misc old uses of forEach before Map
  3980. try {
  3981. stats.forEach(stat => {
  3982. stat.type = modernStatsTypes[stat.type] || stat.type;
  3983. });
  3984. } catch (e) {
  3985. if (e.name !== 'TypeError') {
  3986. throw e;
  3987. }
  3988. // Avoid TypeError: "type" is read-only, in old versions. 34-43ish
  3989. stats.forEach((stat, i) => {
  3990. stats.set(i, Object.assign({}, stat, {
  3991. type: modernStatsTypes[stat.type] || stat.type
  3992. }));
  3993. });
  3994. }
  3995. }
  3996. return stats;
  3997. })
  3998. .then(onSucc, onErr);
  3999. };
  4000. }
  4001. function shimSenderGetStats(window) {
  4002. if (!(typeof window === 'object' && window.RTCPeerConnection &&
  4003. window.RTCRtpSender)) {
  4004. return;
  4005. }
  4006. if (window.RTCRtpSender && 'getStats' in window.RTCRtpSender.prototype) {
  4007. return;
  4008. }
  4009. const origGetSenders = window.RTCPeerConnection.prototype.getSenders;
  4010. if (origGetSenders) {
  4011. window.RTCPeerConnection.prototype.getSenders = function getSenders() {
  4012. const senders = origGetSenders.apply(this, []);
  4013. senders.forEach(sender => sender._pc = this);
  4014. return senders;
  4015. };
  4016. }
  4017. const origAddTrack = window.RTCPeerConnection.prototype.addTrack;
  4018. if (origAddTrack) {
  4019. window.RTCPeerConnection.prototype.addTrack = function addTrack() {
  4020. const sender = origAddTrack.apply(this, arguments);
  4021. sender._pc = this;
  4022. return sender;
  4023. };
  4024. }
  4025. window.RTCRtpSender.prototype.getStats = function getStats() {
  4026. return this.track ? this._pc.getStats(this.track) :
  4027. Promise.resolve(new Map());
  4028. };
  4029. }
  4030. function shimReceiverGetStats(window) {
  4031. if (!(typeof window === 'object' && window.RTCPeerConnection &&
  4032. window.RTCRtpSender)) {
  4033. return;
  4034. }
  4035. if (window.RTCRtpSender && 'getStats' in window.RTCRtpReceiver.prototype) {
  4036. return;
  4037. }
  4038. const origGetReceivers = window.RTCPeerConnection.prototype.getReceivers;
  4039. if (origGetReceivers) {
  4040. window.RTCPeerConnection.prototype.getReceivers = function getReceivers() {
  4041. const receivers = origGetReceivers.apply(this, []);
  4042. receivers.forEach(receiver => receiver._pc = this);
  4043. return receivers;
  4044. };
  4045. }
  4046. wrapPeerConnectionEvent(window, 'track', e => {
  4047. e.receiver._pc = e.srcElement;
  4048. return e;
  4049. });
  4050. window.RTCRtpReceiver.prototype.getStats = function getStats() {
  4051. return this._pc.getStats(this.track);
  4052. };
  4053. }
  4054. function shimRemoveStream(window) {
  4055. if (!window.RTCPeerConnection ||
  4056. 'removeStream' in window.RTCPeerConnection.prototype) {
  4057. return;
  4058. }
  4059. window.RTCPeerConnection.prototype.removeStream =
  4060. function removeStream(stream) {
  4061. deprecated('removeStream', 'removeTrack');
  4062. this.getSenders().forEach(sender => {
  4063. if (sender.track && stream.getTracks().includes(sender.track)) {
  4064. this.removeTrack(sender);
  4065. }
  4066. });
  4067. };
  4068. }
  4069. function shimRTCDataChannel(window) {
  4070. // rename DataChannel to RTCDataChannel (native fix in FF60):
  4071. // https://bugzilla.mozilla.org/show_bug.cgi?id=1173851
  4072. if (window.DataChannel && !window.RTCDataChannel) {
  4073. window.RTCDataChannel = window.DataChannel;
  4074. }
  4075. }
  4076. function shimAddTransceiver(window) {
  4077. // https://github.com/webrtcHacks/adapter/issues/998#issuecomment-516921647
  4078. // Firefox ignores the init sendEncodings options passed to addTransceiver
  4079. // https://bugzilla.mozilla.org/show_bug.cgi?id=1396918
  4080. if (!(typeof window === 'object' && window.RTCPeerConnection)) {
  4081. return;
  4082. }
  4083. const origAddTransceiver = window.RTCPeerConnection.prototype.addTransceiver;
  4084. if (origAddTransceiver) {
  4085. window.RTCPeerConnection.prototype.addTransceiver =
  4086. function addTransceiver() {
  4087. this.setParametersPromises = [];
  4088. const initParameters = arguments[1];
  4089. const shouldPerformCheck = initParameters &&
  4090. 'sendEncodings' in initParameters;
  4091. if (shouldPerformCheck) {
  4092. // If sendEncodings params are provided, validate grammar
  4093. initParameters.sendEncodings.forEach((encodingParam) => {
  4094. if ('rid' in encodingParam) {
  4095. const ridRegex = /^[a-z0-9]{0,16}$/i;
  4096. if (!ridRegex.test(encodingParam.rid)) {
  4097. throw new TypeError('Invalid RID value provided.');
  4098. }
  4099. }
  4100. if ('scaleResolutionDownBy' in encodingParam) {
  4101. if (!(parseFloat(encodingParam.scaleResolutionDownBy) >= 1.0)) {
  4102. throw new RangeError('scale_resolution_down_by must be >= 1.0');
  4103. }
  4104. }
  4105. if ('maxFramerate' in encodingParam) {
  4106. if (!(parseFloat(encodingParam.maxFramerate) >= 0)) {
  4107. throw new RangeError('max_framerate must be >= 0.0');
  4108. }
  4109. }
  4110. });
  4111. }
  4112. const transceiver = origAddTransceiver.apply(this, arguments);
  4113. if (shouldPerformCheck) {
  4114. // Check if the init options were applied. If not we do this in an
  4115. // asynchronous way and save the promise reference in a global object.
  4116. // This is an ugly hack, but at the same time is way more robust than
  4117. // checking the sender parameters before and after the createOffer
  4118. // Also note that after the createoffer we are not 100% sure that
  4119. // the params were asynchronously applied so we might miss the
  4120. // opportunity to recreate offer.
  4121. const {sender} = transceiver;
  4122. const params = sender.getParameters();
  4123. if (!('encodings' in params) ||
  4124. // Avoid being fooled by patched getParameters() below.
  4125. (params.encodings.length === 1 &&
  4126. Object.keys(params.encodings[0]).length === 0)) {
  4127. params.encodings = initParameters.sendEncodings;
  4128. sender.sendEncodings = initParameters.sendEncodings;
  4129. this.setParametersPromises.push(sender.setParameters(params)
  4130. .then(() => {
  4131. delete sender.sendEncodings;
  4132. }).catch(() => {
  4133. delete sender.sendEncodings;
  4134. })
  4135. );
  4136. }
  4137. }
  4138. return transceiver;
  4139. };
  4140. }
  4141. }
  4142. function shimGetParameters(window) {
  4143. if (!(typeof window === 'object' && window.RTCRtpSender)) {
  4144. return;
  4145. }
  4146. const origGetParameters = window.RTCRtpSender.prototype.getParameters;
  4147. if (origGetParameters) {
  4148. window.RTCRtpSender.prototype.getParameters =
  4149. function getParameters() {
  4150. const params = origGetParameters.apply(this, arguments);
  4151. if (!('encodings' in params)) {
  4152. params.encodings = [].concat(this.sendEncodings || [{}]);
  4153. }
  4154. return params;
  4155. };
  4156. }
  4157. }
  4158. function shimCreateOffer(window) {
  4159. // https://github.com/webrtcHacks/adapter/issues/998#issuecomment-516921647
  4160. // Firefox ignores the init sendEncodings options passed to addTransceiver
  4161. // https://bugzilla.mozilla.org/show_bug.cgi?id=1396918
  4162. if (!(typeof window === 'object' && window.RTCPeerConnection)) {
  4163. return;
  4164. }
  4165. const origCreateOffer = window.RTCPeerConnection.prototype.createOffer;
  4166. window.RTCPeerConnection.prototype.createOffer = function createOffer() {
  4167. if (this.setParametersPromises && this.setParametersPromises.length) {
  4168. return Promise.all(this.setParametersPromises)
  4169. .then(() => {
  4170. return origCreateOffer.apply(this, arguments);
  4171. })
  4172. .finally(() => {
  4173. this.setParametersPromises = [];
  4174. });
  4175. }
  4176. return origCreateOffer.apply(this, arguments);
  4177. };
  4178. }
  4179. function shimCreateAnswer(window) {
  4180. // https://github.com/webrtcHacks/adapter/issues/998#issuecomment-516921647
  4181. // Firefox ignores the init sendEncodings options passed to addTransceiver
  4182. // https://bugzilla.mozilla.org/show_bug.cgi?id=1396918
  4183. if (!(typeof window === 'object' && window.RTCPeerConnection)) {
  4184. return;
  4185. }
  4186. const origCreateAnswer = window.RTCPeerConnection.prototype.createAnswer;
  4187. window.RTCPeerConnection.prototype.createAnswer = function createAnswer() {
  4188. if (this.setParametersPromises && this.setParametersPromises.length) {
  4189. return Promise.all(this.setParametersPromises)
  4190. .then(() => {
  4191. return origCreateAnswer.apply(this, arguments);
  4192. })
  4193. .finally(() => {
  4194. this.setParametersPromises = [];
  4195. });
  4196. }
  4197. return origCreateAnswer.apply(this, arguments);
  4198. };
  4199. }
  4200. var firefoxShim = /*#__PURE__*/Object.freeze({
  4201. __proto__: null,
  4202. shimOnTrack: shimOnTrack,
  4203. shimPeerConnection: shimPeerConnection,
  4204. shimSenderGetStats: shimSenderGetStats,
  4205. shimReceiverGetStats: shimReceiverGetStats,
  4206. shimRemoveStream: shimRemoveStream,
  4207. shimRTCDataChannel: shimRTCDataChannel,
  4208. shimAddTransceiver: shimAddTransceiver,
  4209. shimGetParameters: shimGetParameters,
  4210. shimCreateOffer: shimCreateOffer,
  4211. shimCreateAnswer: shimCreateAnswer,
  4212. shimGetUserMedia: shimGetUserMedia$1,
  4213. shimGetDisplayMedia: shimGetDisplayMedia
  4214. });
  4215. /*
  4216. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  4217. *
  4218. * Use of this source code is governed by a BSD-style license
  4219. * that can be found in the LICENSE file in the root of the source
  4220. * tree.
  4221. */
  4222. function shimLocalStreamsAPI(window) {
  4223. if (typeof window !== 'object' || !window.RTCPeerConnection) {
  4224. return;
  4225. }
  4226. if (!('getLocalStreams' in window.RTCPeerConnection.prototype)) {
  4227. window.RTCPeerConnection.prototype.getLocalStreams =
  4228. function getLocalStreams() {
  4229. if (!this._localStreams) {
  4230. this._localStreams = [];
  4231. }
  4232. return this._localStreams;
  4233. };
  4234. }
  4235. if (!('addStream' in window.RTCPeerConnection.prototype)) {
  4236. const _addTrack = window.RTCPeerConnection.prototype.addTrack;
  4237. window.RTCPeerConnection.prototype.addStream = function addStream(stream) {
  4238. if (!this._localStreams) {
  4239. this._localStreams = [];
  4240. }
  4241. if (!this._localStreams.includes(stream)) {
  4242. this._localStreams.push(stream);
  4243. }
  4244. // Try to emulate Chrome's behaviour of adding in audio-video order.
  4245. // Safari orders by track id.
  4246. stream.getAudioTracks().forEach(track => _addTrack.call(this, track,
  4247. stream));
  4248. stream.getVideoTracks().forEach(track => _addTrack.call(this, track,
  4249. stream));
  4250. };
  4251. window.RTCPeerConnection.prototype.addTrack =
  4252. function addTrack(track, ...streams) {
  4253. if (streams) {
  4254. streams.forEach((stream) => {
  4255. if (!this._localStreams) {
  4256. this._localStreams = [stream];
  4257. } else if (!this._localStreams.includes(stream)) {
  4258. this._localStreams.push(stream);
  4259. }
  4260. });
  4261. }
  4262. return _addTrack.apply(this, arguments);
  4263. };
  4264. }
  4265. if (!('removeStream' in window.RTCPeerConnection.prototype)) {
  4266. window.RTCPeerConnection.prototype.removeStream =
  4267. function removeStream(stream) {
  4268. if (!this._localStreams) {
  4269. this._localStreams = [];
  4270. }
  4271. const index = this._localStreams.indexOf(stream);
  4272. if (index === -1) {
  4273. return;
  4274. }
  4275. this._localStreams.splice(index, 1);
  4276. const tracks = stream.getTracks();
  4277. this.getSenders().forEach(sender => {
  4278. if (tracks.includes(sender.track)) {
  4279. this.removeTrack(sender);
  4280. }
  4281. });
  4282. };
  4283. }
  4284. }
  4285. function shimRemoteStreamsAPI(window) {
  4286. if (typeof window !== 'object' || !window.RTCPeerConnection) {
  4287. return;
  4288. }
  4289. if (!('getRemoteStreams' in window.RTCPeerConnection.prototype)) {
  4290. window.RTCPeerConnection.prototype.getRemoteStreams =
  4291. function getRemoteStreams() {
  4292. return this._remoteStreams ? this._remoteStreams : [];
  4293. };
  4294. }
  4295. if (!('onaddstream' in window.RTCPeerConnection.prototype)) {
  4296. Object.defineProperty(window.RTCPeerConnection.prototype, 'onaddstream', {
  4297. get() {
  4298. return this._onaddstream;
  4299. },
  4300. set(f) {
  4301. if (this._onaddstream) {
  4302. this.removeEventListener('addstream', this._onaddstream);
  4303. this.removeEventListener('track', this._onaddstreampoly);
  4304. }
  4305. this.addEventListener('addstream', this._onaddstream = f);
  4306. this.addEventListener('track', this._onaddstreampoly = (e) => {
  4307. e.streams.forEach(stream => {
  4308. if (!this._remoteStreams) {
  4309. this._remoteStreams = [];
  4310. }
  4311. if (this._remoteStreams.includes(stream)) {
  4312. return;
  4313. }
  4314. this._remoteStreams.push(stream);
  4315. const event = new Event('addstream');
  4316. event.stream = stream;
  4317. this.dispatchEvent(event);
  4318. });
  4319. });
  4320. }
  4321. });
  4322. const origSetRemoteDescription =
  4323. window.RTCPeerConnection.prototype.setRemoteDescription;
  4324. window.RTCPeerConnection.prototype.setRemoteDescription =
  4325. function setRemoteDescription() {
  4326. const pc = this;
  4327. if (!this._onaddstreampoly) {
  4328. this.addEventListener('track', this._onaddstreampoly = function(e) {
  4329. e.streams.forEach(stream => {
  4330. if (!pc._remoteStreams) {
  4331. pc._remoteStreams = [];
  4332. }
  4333. if (pc._remoteStreams.indexOf(stream) >= 0) {
  4334. return;
  4335. }
  4336. pc._remoteStreams.push(stream);
  4337. const event = new Event('addstream');
  4338. event.stream = stream;
  4339. pc.dispatchEvent(event);
  4340. });
  4341. });
  4342. }
  4343. return origSetRemoteDescription.apply(pc, arguments);
  4344. };
  4345. }
  4346. }
  4347. function shimCallbacksAPI(window) {
  4348. if (typeof window !== 'object' || !window.RTCPeerConnection) {
  4349. return;
  4350. }
  4351. const prototype = window.RTCPeerConnection.prototype;
  4352. const origCreateOffer = prototype.createOffer;
  4353. const origCreateAnswer = prototype.createAnswer;
  4354. const setLocalDescription = prototype.setLocalDescription;
  4355. const setRemoteDescription = prototype.setRemoteDescription;
  4356. const addIceCandidate = prototype.addIceCandidate;
  4357. prototype.createOffer =
  4358. function createOffer(successCallback, failureCallback) {
  4359. const options = (arguments.length >= 2) ? arguments[2] : arguments[0];
  4360. const promise = origCreateOffer.apply(this, [options]);
  4361. if (!failureCallback) {
  4362. return promise;
  4363. }
  4364. promise.then(successCallback, failureCallback);
  4365. return Promise.resolve();
  4366. };
  4367. prototype.createAnswer =
  4368. function createAnswer(successCallback, failureCallback) {
  4369. const options = (arguments.length >= 2) ? arguments[2] : arguments[0];
  4370. const promise = origCreateAnswer.apply(this, [options]);
  4371. if (!failureCallback) {
  4372. return promise;
  4373. }
  4374. promise.then(successCallback, failureCallback);
  4375. return Promise.resolve();
  4376. };
  4377. let withCallback = function(description, successCallback, failureCallback) {
  4378. const promise = setLocalDescription.apply(this, [description]);
  4379. if (!failureCallback) {
  4380. return promise;
  4381. }
  4382. promise.then(successCallback, failureCallback);
  4383. return Promise.resolve();
  4384. };
  4385. prototype.setLocalDescription = withCallback;
  4386. withCallback = function(description, successCallback, failureCallback) {
  4387. const promise = setRemoteDescription.apply(this, [description]);
  4388. if (!failureCallback) {
  4389. return promise;
  4390. }
  4391. promise.then(successCallback, failureCallback);
  4392. return Promise.resolve();
  4393. };
  4394. prototype.setRemoteDescription = withCallback;
  4395. withCallback = function(candidate, successCallback, failureCallback) {
  4396. const promise = addIceCandidate.apply(this, [candidate]);
  4397. if (!failureCallback) {
  4398. return promise;
  4399. }
  4400. promise.then(successCallback, failureCallback);
  4401. return Promise.resolve();
  4402. };
  4403. prototype.addIceCandidate = withCallback;
  4404. }
  4405. function shimGetUserMedia(window) {
  4406. const navigator = window && window.navigator;
  4407. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  4408. // shim not needed in Safari 12.1
  4409. const mediaDevices = navigator.mediaDevices;
  4410. const _getUserMedia = mediaDevices.getUserMedia.bind(mediaDevices);
  4411. navigator.mediaDevices.getUserMedia = (constraints) => {
  4412. return _getUserMedia(shimConstraints(constraints));
  4413. };
  4414. }
  4415. if (!navigator.getUserMedia && navigator.mediaDevices &&
  4416. navigator.mediaDevices.getUserMedia) {
  4417. navigator.getUserMedia = function getUserMedia(constraints, cb, errcb) {
  4418. navigator.mediaDevices.getUserMedia(constraints)
  4419. .then(cb, errcb);
  4420. }.bind(navigator);
  4421. }
  4422. }
  4423. function shimConstraints(constraints) {
  4424. if (constraints && constraints.video !== undefined) {
  4425. return Object.assign({},
  4426. constraints,
  4427. {video: compactObject(constraints.video)}
  4428. );
  4429. }
  4430. return constraints;
  4431. }
  4432. function shimRTCIceServerUrls(window) {
  4433. if (!window.RTCPeerConnection) {
  4434. return;
  4435. }
  4436. // migrate from non-spec RTCIceServer.url to RTCIceServer.urls
  4437. const OrigPeerConnection = window.RTCPeerConnection;
  4438. window.RTCPeerConnection =
  4439. function RTCPeerConnection(pcConfig, pcConstraints) {
  4440. if (pcConfig && pcConfig.iceServers) {
  4441. const newIceServers = [];
  4442. for (let i = 0; i < pcConfig.iceServers.length; i++) {
  4443. let server = pcConfig.iceServers[i];
  4444. if (!server.hasOwnProperty('urls') &&
  4445. server.hasOwnProperty('url')) {
  4446. deprecated('RTCIceServer.url', 'RTCIceServer.urls');
  4447. server = JSON.parse(JSON.stringify(server));
  4448. server.urls = server.url;
  4449. delete server.url;
  4450. newIceServers.push(server);
  4451. } else {
  4452. newIceServers.push(pcConfig.iceServers[i]);
  4453. }
  4454. }
  4455. pcConfig.iceServers = newIceServers;
  4456. }
  4457. return new OrigPeerConnection(pcConfig, pcConstraints);
  4458. };
  4459. window.RTCPeerConnection.prototype = OrigPeerConnection.prototype;
  4460. // wrap static methods. Currently just generateCertificate.
  4461. if ('generateCertificate' in OrigPeerConnection) {
  4462. Object.defineProperty(window.RTCPeerConnection, 'generateCertificate', {
  4463. get() {
  4464. return OrigPeerConnection.generateCertificate;
  4465. }
  4466. });
  4467. }
  4468. }
  4469. function shimTrackEventTransceiver(window) {
  4470. // Add event.transceiver member over deprecated event.receiver
  4471. if (typeof window === 'object' && window.RTCTrackEvent &&
  4472. 'receiver' in window.RTCTrackEvent.prototype &&
  4473. !('transceiver' in window.RTCTrackEvent.prototype)) {
  4474. Object.defineProperty(window.RTCTrackEvent.prototype, 'transceiver', {
  4475. get() {
  4476. return {receiver: this.receiver};
  4477. }
  4478. });
  4479. }
  4480. }
  4481. function shimCreateOfferLegacy(window) {
  4482. const origCreateOffer = window.RTCPeerConnection.prototype.createOffer;
  4483. window.RTCPeerConnection.prototype.createOffer =
  4484. function createOffer(offerOptions) {
  4485. if (offerOptions) {
  4486. if (typeof offerOptions.offerToReceiveAudio !== 'undefined') {
  4487. // support bit values
  4488. offerOptions.offerToReceiveAudio =
  4489. !!offerOptions.offerToReceiveAudio;
  4490. }
  4491. const audioTransceiver = this.getTransceivers().find(transceiver =>
  4492. transceiver.receiver.track.kind === 'audio');
  4493. if (offerOptions.offerToReceiveAudio === false && audioTransceiver) {
  4494. if (audioTransceiver.direction === 'sendrecv') {
  4495. if (audioTransceiver.setDirection) {
  4496. audioTransceiver.setDirection('sendonly');
  4497. } else {
  4498. audioTransceiver.direction = 'sendonly';
  4499. }
  4500. } else if (audioTransceiver.direction === 'recvonly') {
  4501. if (audioTransceiver.setDirection) {
  4502. audioTransceiver.setDirection('inactive');
  4503. } else {
  4504. audioTransceiver.direction = 'inactive';
  4505. }
  4506. }
  4507. } else if (offerOptions.offerToReceiveAudio === true &&
  4508. !audioTransceiver) {
  4509. this.addTransceiver('audio');
  4510. }
  4511. if (typeof offerOptions.offerToReceiveVideo !== 'undefined') {
  4512. // support bit values
  4513. offerOptions.offerToReceiveVideo =
  4514. !!offerOptions.offerToReceiveVideo;
  4515. }
  4516. const videoTransceiver = this.getTransceivers().find(transceiver =>
  4517. transceiver.receiver.track.kind === 'video');
  4518. if (offerOptions.offerToReceiveVideo === false && videoTransceiver) {
  4519. if (videoTransceiver.direction === 'sendrecv') {
  4520. if (videoTransceiver.setDirection) {
  4521. videoTransceiver.setDirection('sendonly');
  4522. } else {
  4523. videoTransceiver.direction = 'sendonly';
  4524. }
  4525. } else if (videoTransceiver.direction === 'recvonly') {
  4526. if (videoTransceiver.setDirection) {
  4527. videoTransceiver.setDirection('inactive');
  4528. } else {
  4529. videoTransceiver.direction = 'inactive';
  4530. }
  4531. }
  4532. } else if (offerOptions.offerToReceiveVideo === true &&
  4533. !videoTransceiver) {
  4534. this.addTransceiver('video');
  4535. }
  4536. }
  4537. return origCreateOffer.apply(this, arguments);
  4538. };
  4539. }
  4540. function shimAudioContext(window) {
  4541. if (typeof window !== 'object' || window.AudioContext) {
  4542. return;
  4543. }
  4544. window.AudioContext = window.webkitAudioContext;
  4545. }
  4546. var safariShim = /*#__PURE__*/Object.freeze({
  4547. __proto__: null,
  4548. shimLocalStreamsAPI: shimLocalStreamsAPI,
  4549. shimRemoteStreamsAPI: shimRemoteStreamsAPI,
  4550. shimCallbacksAPI: shimCallbacksAPI,
  4551. shimGetUserMedia: shimGetUserMedia,
  4552. shimConstraints: shimConstraints,
  4553. shimRTCIceServerUrls: shimRTCIceServerUrls,
  4554. shimTrackEventTransceiver: shimTrackEventTransceiver,
  4555. shimCreateOfferLegacy: shimCreateOfferLegacy,
  4556. shimAudioContext: shimAudioContext
  4557. });
  4558. /*
  4559. * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
  4560. *
  4561. * Use of this source code is governed by a BSD-style license
  4562. * that can be found in the LICENSE file in the root of the source
  4563. * tree.
  4564. */
  4565. function shimRTCIceCandidate(window) {
  4566. // foundation is arbitrarily chosen as an indicator for full support for
  4567. // https://w3c.github.io/webrtc-pc/#rtcicecandidate-interface
  4568. if (!window.RTCIceCandidate || (window.RTCIceCandidate && 'foundation' in
  4569. window.RTCIceCandidate.prototype)) {
  4570. return;
  4571. }
  4572. const NativeRTCIceCandidate = window.RTCIceCandidate;
  4573. window.RTCIceCandidate = function RTCIceCandidate(args) {
  4574. // Remove the a= which shouldn't be part of the candidate string.
  4575. if (typeof args === 'object' && args.candidate &&
  4576. args.candidate.indexOf('a=') === 0) {
  4577. args = JSON.parse(JSON.stringify(args));
  4578. args.candidate = args.candidate.substr(2);
  4579. }
  4580. if (args.candidate && args.candidate.length) {
  4581. // Augment the native candidate with the parsed fields.
  4582. const nativeCandidate = new NativeRTCIceCandidate(args);
  4583. const parsedCandidate = sdp.parseCandidate(args.candidate);
  4584. const augmentedCandidate = Object.assign(nativeCandidate,
  4585. parsedCandidate);
  4586. // Add a serializer that does not serialize the extra attributes.
  4587. augmentedCandidate.toJSON = function toJSON() {
  4588. return {
  4589. candidate: augmentedCandidate.candidate,
  4590. sdpMid: augmentedCandidate.sdpMid,
  4591. sdpMLineIndex: augmentedCandidate.sdpMLineIndex,
  4592. usernameFragment: augmentedCandidate.usernameFragment,
  4593. };
  4594. };
  4595. return augmentedCandidate;
  4596. }
  4597. return new NativeRTCIceCandidate(args);
  4598. };
  4599. window.RTCIceCandidate.prototype = NativeRTCIceCandidate.prototype;
  4600. // Hook up the augmented candidate in onicecandidate and
  4601. // addEventListener('icecandidate', ...)
  4602. wrapPeerConnectionEvent(window, 'icecandidate', e => {
  4603. if (e.candidate) {
  4604. Object.defineProperty(e, 'candidate', {
  4605. value: new window.RTCIceCandidate(e.candidate),
  4606. writable: 'false'
  4607. });
  4608. }
  4609. return e;
  4610. });
  4611. }
  4612. function shimMaxMessageSize(window, browserDetails) {
  4613. if (!window.RTCPeerConnection) {
  4614. return;
  4615. }
  4616. if (!('sctp' in window.RTCPeerConnection.prototype)) {
  4617. Object.defineProperty(window.RTCPeerConnection.prototype, 'sctp', {
  4618. get() {
  4619. return typeof this._sctp === 'undefined' ? null : this._sctp;
  4620. }
  4621. });
  4622. }
  4623. const sctpInDescription = function(description) {
  4624. if (!description || !description.sdp) {
  4625. return false;
  4626. }
  4627. const sections = sdp.splitSections(description.sdp);
  4628. sections.shift();
  4629. return sections.some(mediaSection => {
  4630. const mLine = sdp.parseMLine(mediaSection);
  4631. return mLine && mLine.kind === 'application'
  4632. && mLine.protocol.indexOf('SCTP') !== -1;
  4633. });
  4634. };
  4635. const getRemoteFirefoxVersion = function(description) {
  4636. // TODO: Is there a better solution for detecting Firefox?
  4637. const match = description.sdp.match(/mozilla...THIS_IS_SDPARTA-(\d+)/);
  4638. if (match === null || match.length < 2) {
  4639. return -1;
  4640. }
  4641. const version = parseInt(match[1], 10);
  4642. // Test for NaN (yes, this is ugly)
  4643. return version !== version ? -1 : version;
  4644. };
  4645. const getCanSendMaxMessageSize = function(remoteIsFirefox) {
  4646. // Every implementation we know can send at least 64 KiB.
  4647. // Note: Although Chrome is technically able to send up to 256 KiB, the
  4648. // data does not reach the other peer reliably.
  4649. // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=8419
  4650. let canSendMaxMessageSize = 65536;
  4651. if (browserDetails.browser === 'firefox') {
  4652. if (browserDetails.version < 57) {
  4653. if (remoteIsFirefox === -1) {
  4654. // FF < 57 will send in 16 KiB chunks using the deprecated PPID
  4655. // fragmentation.
  4656. canSendMaxMessageSize = 16384;
  4657. } else {
  4658. // However, other FF (and RAWRTC) can reassemble PPID-fragmented
  4659. // messages. Thus, supporting ~2 GiB when sending.
  4660. canSendMaxMessageSize = 2147483637;
  4661. }
  4662. } else if (browserDetails.version < 60) {
  4663. // Currently, all FF >= 57 will reset the remote maximum message size
  4664. // to the default value when a data channel is created at a later
  4665. // stage. :(
  4666. // See: https://bugzilla.mozilla.org/show_bug.cgi?id=1426831
  4667. canSendMaxMessageSize =
  4668. browserDetails.version === 57 ? 65535 : 65536;
  4669. } else {
  4670. // FF >= 60 supports sending ~2 GiB
  4671. canSendMaxMessageSize = 2147483637;
  4672. }
  4673. }
  4674. return canSendMaxMessageSize;
  4675. };
  4676. const getMaxMessageSize = function(description, remoteIsFirefox) {
  4677. // Note: 65536 bytes is the default value from the SDP spec. Also,
  4678. // every implementation we know supports receiving 65536 bytes.
  4679. let maxMessageSize = 65536;
  4680. // FF 57 has a slightly incorrect default remote max message size, so
  4681. // we need to adjust it here to avoid a failure when sending.
  4682. // See: https://bugzilla.mozilla.org/show_bug.cgi?id=1425697
  4683. if (browserDetails.browser === 'firefox'
  4684. && browserDetails.version === 57) {
  4685. maxMessageSize = 65535;
  4686. }
  4687. const match = sdp.matchPrefix(description.sdp,
  4688. 'a=max-message-size:');
  4689. if (match.length > 0) {
  4690. maxMessageSize = parseInt(match[0].substr(19), 10);
  4691. } else if (browserDetails.browser === 'firefox' &&
  4692. remoteIsFirefox !== -1) {
  4693. // If the maximum message size is not present in the remote SDP and
  4694. // both local and remote are Firefox, the remote peer can receive
  4695. // ~2 GiB.
  4696. maxMessageSize = 2147483637;
  4697. }
  4698. return maxMessageSize;
  4699. };
  4700. const origSetRemoteDescription =
  4701. window.RTCPeerConnection.prototype.setRemoteDescription;
  4702. window.RTCPeerConnection.prototype.setRemoteDescription =
  4703. function setRemoteDescription() {
  4704. this._sctp = null;
  4705. // Chrome decided to not expose .sctp in plan-b mode.
  4706. // As usual, adapter.js has to do an 'ugly worakaround'
  4707. // to cover up the mess.
  4708. if (browserDetails.browser === 'chrome' && browserDetails.version >= 76) {
  4709. const {sdpSemantics} = this.getConfiguration();
  4710. if (sdpSemantics === 'plan-b') {
  4711. Object.defineProperty(this, 'sctp', {
  4712. get() {
  4713. return typeof this._sctp === 'undefined' ? null : this._sctp;
  4714. },
  4715. enumerable: true,
  4716. configurable: true,
  4717. });
  4718. }
  4719. }
  4720. if (sctpInDescription(arguments[0])) {
  4721. // Check if the remote is FF.
  4722. const isFirefox = getRemoteFirefoxVersion(arguments[0]);
  4723. // Get the maximum message size the local peer is capable of sending
  4724. const canSendMMS = getCanSendMaxMessageSize(isFirefox);
  4725. // Get the maximum message size of the remote peer.
  4726. const remoteMMS = getMaxMessageSize(arguments[0], isFirefox);
  4727. // Determine final maximum message size
  4728. let maxMessageSize;
  4729. if (canSendMMS === 0 && remoteMMS === 0) {
  4730. maxMessageSize = Number.POSITIVE_INFINITY;
  4731. } else if (canSendMMS === 0 || remoteMMS === 0) {
  4732. maxMessageSize = Math.max(canSendMMS, remoteMMS);
  4733. } else {
  4734. maxMessageSize = Math.min(canSendMMS, remoteMMS);
  4735. }
  4736. // Create a dummy RTCSctpTransport object and the 'maxMessageSize'
  4737. // attribute.
  4738. const sctp = {};
  4739. Object.defineProperty(sctp, 'maxMessageSize', {
  4740. get() {
  4741. return maxMessageSize;
  4742. }
  4743. });
  4744. this._sctp = sctp;
  4745. }
  4746. return origSetRemoteDescription.apply(this, arguments);
  4747. };
  4748. }
  4749. function shimSendThrowTypeError(window) {
  4750. if (!(window.RTCPeerConnection &&
  4751. 'createDataChannel' in window.RTCPeerConnection.prototype)) {
  4752. return;
  4753. }
  4754. // Note: Although Firefox >= 57 has a native implementation, the maximum
  4755. // message size can be reset for all data channels at a later stage.
  4756. // See: https://bugzilla.mozilla.org/show_bug.cgi?id=1426831
  4757. function wrapDcSend(dc, pc) {
  4758. const origDataChannelSend = dc.send;
  4759. dc.send = function send() {
  4760. const data = arguments[0];
  4761. const length = data.length || data.size || data.byteLength;
  4762. if (dc.readyState === 'open' &&
  4763. pc.sctp && length > pc.sctp.maxMessageSize) {
  4764. throw new TypeError('Message too large (can send a maximum of ' +
  4765. pc.sctp.maxMessageSize + ' bytes)');
  4766. }
  4767. return origDataChannelSend.apply(dc, arguments);
  4768. };
  4769. }
  4770. const origCreateDataChannel =
  4771. window.RTCPeerConnection.prototype.createDataChannel;
  4772. window.RTCPeerConnection.prototype.createDataChannel =
  4773. function createDataChannel() {
  4774. const dataChannel = origCreateDataChannel.apply(this, arguments);
  4775. wrapDcSend(dataChannel, this);
  4776. return dataChannel;
  4777. };
  4778. wrapPeerConnectionEvent(window, 'datachannel', e => {
  4779. wrapDcSend(e.channel, e.target);
  4780. return e;
  4781. });
  4782. }
  4783. /* shims RTCConnectionState by pretending it is the same as iceConnectionState.
  4784. * See https://bugs.chromium.org/p/webrtc/issues/detail?id=6145#c12
  4785. * for why this is a valid hack in Chrome. In Firefox it is slightly incorrect
  4786. * since DTLS failures would be hidden. See
  4787. * https://bugzilla.mozilla.org/show_bug.cgi?id=1265827
  4788. * for the Firefox tracking bug.
  4789. */
  4790. function shimConnectionState(window) {
  4791. if (!window.RTCPeerConnection ||
  4792. 'connectionState' in window.RTCPeerConnection.prototype) {
  4793. return;
  4794. }
  4795. const proto = window.RTCPeerConnection.prototype;
  4796. Object.defineProperty(proto, 'connectionState', {
  4797. get() {
  4798. return {
  4799. completed: 'connected',
  4800. checking: 'connecting'
  4801. }[this.iceConnectionState] || this.iceConnectionState;
  4802. },
  4803. enumerable: true,
  4804. configurable: true
  4805. });
  4806. Object.defineProperty(proto, 'onconnectionstatechange', {
  4807. get() {
  4808. return this._onconnectionstatechange || null;
  4809. },
  4810. set(cb) {
  4811. if (this._onconnectionstatechange) {
  4812. this.removeEventListener('connectionstatechange',
  4813. this._onconnectionstatechange);
  4814. delete this._onconnectionstatechange;
  4815. }
  4816. if (cb) {
  4817. this.addEventListener('connectionstatechange',
  4818. this._onconnectionstatechange = cb);
  4819. }
  4820. },
  4821. enumerable: true,
  4822. configurable: true
  4823. });
  4824. ['setLocalDescription', 'setRemoteDescription'].forEach((method) => {
  4825. const origMethod = proto[method];
  4826. proto[method] = function() {
  4827. if (!this._connectionstatechangepoly) {
  4828. this._connectionstatechangepoly = e => {
  4829. const pc = e.target;
  4830. if (pc._lastConnectionState !== pc.connectionState) {
  4831. pc._lastConnectionState = pc.connectionState;
  4832. const newEvent = new Event('connectionstatechange', e);
  4833. pc.dispatchEvent(newEvent);
  4834. }
  4835. return e;
  4836. };
  4837. this.addEventListener('iceconnectionstatechange',
  4838. this._connectionstatechangepoly);
  4839. }
  4840. return origMethod.apply(this, arguments);
  4841. };
  4842. });
  4843. }
  4844. function removeExtmapAllowMixed(window, browserDetails) {
  4845. /* remove a=extmap-allow-mixed for webrtc.org < M71 */
  4846. if (!window.RTCPeerConnection) {
  4847. return;
  4848. }
  4849. if (browserDetails.browser === 'chrome' && browserDetails.version >= 71) {
  4850. return;
  4851. }
  4852. if (browserDetails.browser === 'safari' && browserDetails.version >= 605) {
  4853. return;
  4854. }
  4855. const nativeSRD = window.RTCPeerConnection.prototype.setRemoteDescription;
  4856. window.RTCPeerConnection.prototype.setRemoteDescription =
  4857. function setRemoteDescription(desc) {
  4858. if (desc && desc.sdp && desc.sdp.indexOf('\na=extmap-allow-mixed') !== -1) {
  4859. const sdp = desc.sdp.split('\n').filter((line) => {
  4860. return line.trim() !== 'a=extmap-allow-mixed';
  4861. }).join('\n');
  4862. // Safari enforces read-only-ness of RTCSessionDescription fields.
  4863. if (window.RTCSessionDescription &&
  4864. desc instanceof window.RTCSessionDescription) {
  4865. arguments[0] = new window.RTCSessionDescription({
  4866. type: desc.type,
  4867. sdp,
  4868. });
  4869. } else {
  4870. desc.sdp = sdp;
  4871. }
  4872. }
  4873. return nativeSRD.apply(this, arguments);
  4874. };
  4875. }
  4876. function shimAddIceCandidateNullOrEmpty(window, browserDetails) {
  4877. // Support for addIceCandidate(null or undefined)
  4878. // as well as addIceCandidate({candidate: "", ...})
  4879. // https://bugs.chromium.org/p/chromium/issues/detail?id=978582
  4880. // Note: must be called before other polyfills which change the signature.
  4881. if (!(window.RTCPeerConnection && window.RTCPeerConnection.prototype)) {
  4882. return;
  4883. }
  4884. const nativeAddIceCandidate =
  4885. window.RTCPeerConnection.prototype.addIceCandidate;
  4886. if (!nativeAddIceCandidate || nativeAddIceCandidate.length === 0) {
  4887. return;
  4888. }
  4889. window.RTCPeerConnection.prototype.addIceCandidate =
  4890. function addIceCandidate() {
  4891. if (!arguments[0]) {
  4892. if (arguments[1]) {
  4893. arguments[1].apply(null);
  4894. }
  4895. return Promise.resolve();
  4896. }
  4897. // Firefox 68+ emits and processes {candidate: "", ...}, ignore
  4898. // in older versions.
  4899. // Native support for ignoring exists for Chrome M77+.
  4900. // Safari ignores as well, exact version unknown but works in the same
  4901. // version that also ignores addIceCandidate(null).
  4902. if (((browserDetails.browser === 'chrome' && browserDetails.version < 78)
  4903. || (browserDetails.browser === 'firefox'
  4904. && browserDetails.version < 68)
  4905. || (browserDetails.browser === 'safari'))
  4906. && arguments[0] && arguments[0].candidate === '') {
  4907. return Promise.resolve();
  4908. }
  4909. return nativeAddIceCandidate.apply(this, arguments);
  4910. };
  4911. }
  4912. var commonShim = /*#__PURE__*/Object.freeze({
  4913. __proto__: null,
  4914. shimRTCIceCandidate: shimRTCIceCandidate,
  4915. shimMaxMessageSize: shimMaxMessageSize,
  4916. shimSendThrowTypeError: shimSendThrowTypeError,
  4917. shimConnectionState: shimConnectionState,
  4918. removeExtmapAllowMixed: removeExtmapAllowMixed,
  4919. shimAddIceCandidateNullOrEmpty: shimAddIceCandidateNullOrEmpty
  4920. });
  4921. /*
  4922. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  4923. *
  4924. * Use of this source code is governed by a BSD-style license
  4925. * that can be found in the LICENSE file in the root of the source
  4926. * tree.
  4927. */
  4928. // Shimming starts here.
  4929. function adapterFactory({window} = {}, options = {
  4930. shimChrome: true,
  4931. shimFirefox: true,
  4932. shimEdge: true,
  4933. shimSafari: true,
  4934. }) {
  4935. // Utils.
  4936. const logging = log$1;
  4937. const browserDetails = detectBrowser(window);
  4938. const adapter = {
  4939. browserDetails,
  4940. commonShim,
  4941. extractVersion: extractVersion,
  4942. disableLog: disableLog,
  4943. disableWarnings: disableWarnings
  4944. };
  4945. // Shim browser if found.
  4946. switch (browserDetails.browser) {
  4947. case 'chrome':
  4948. if (!chromeShim || !shimPeerConnection$2 ||
  4949. !options.shimChrome) {
  4950. logging('Chrome shim is not included in this adapter release.');
  4951. return adapter;
  4952. }
  4953. if (browserDetails.version === null) {
  4954. logging('Chrome shim can not determine version, not shimming.');
  4955. return adapter;
  4956. }
  4957. logging('adapter.js shimming chrome.');
  4958. // Export to the adapter global object visible in the browser.
  4959. adapter.browserShim = chromeShim;
  4960. // Must be called before shimPeerConnection.
  4961. shimAddIceCandidateNullOrEmpty(window, browserDetails);
  4962. shimGetUserMedia$3(window, browserDetails);
  4963. shimMediaStream(window);
  4964. shimPeerConnection$2(window, browserDetails);
  4965. shimOnTrack$1(window);
  4966. shimAddTrackRemoveTrack(window, browserDetails);
  4967. shimGetSendersWithDtmf(window);
  4968. shimGetStats(window);
  4969. shimSenderReceiverGetStats(window);
  4970. fixNegotiationNeeded(window, browserDetails);
  4971. shimRTCIceCandidate(window);
  4972. shimConnectionState(window);
  4973. shimMaxMessageSize(window, browserDetails);
  4974. shimSendThrowTypeError(window);
  4975. removeExtmapAllowMixed(window, browserDetails);
  4976. break;
  4977. case 'firefox':
  4978. if (!firefoxShim || !shimPeerConnection ||
  4979. !options.shimFirefox) {
  4980. logging('Firefox shim is not included in this adapter release.');
  4981. return adapter;
  4982. }
  4983. logging('adapter.js shimming firefox.');
  4984. // Export to the adapter global object visible in the browser.
  4985. adapter.browserShim = firefoxShim;
  4986. // Must be called before shimPeerConnection.
  4987. shimAddIceCandidateNullOrEmpty(window, browserDetails);
  4988. shimGetUserMedia$1(window, browserDetails);
  4989. shimPeerConnection(window, browserDetails);
  4990. shimOnTrack(window);
  4991. shimRemoveStream(window);
  4992. shimSenderGetStats(window);
  4993. shimReceiverGetStats(window);
  4994. shimRTCDataChannel(window);
  4995. shimAddTransceiver(window);
  4996. shimGetParameters(window);
  4997. shimCreateOffer(window);
  4998. shimCreateAnswer(window);
  4999. shimRTCIceCandidate(window);
  5000. shimConnectionState(window);
  5001. shimMaxMessageSize(window, browserDetails);
  5002. shimSendThrowTypeError(window);
  5003. break;
  5004. case 'edge':
  5005. if (!edgeShim || !shimPeerConnection$1 || !options.shimEdge) {
  5006. logging('MS edge shim is not included in this adapter release.');
  5007. return adapter;
  5008. }
  5009. logging('adapter.js shimming edge.');
  5010. // Export to the adapter global object visible in the browser.
  5011. adapter.browserShim = edgeShim;
  5012. shimGetUserMedia$2(window);
  5013. shimGetDisplayMedia$1(window);
  5014. shimPeerConnection$1(window, browserDetails);
  5015. shimReplaceTrack(window);
  5016. // the edge shim implements the full RTCIceCandidate object.
  5017. shimMaxMessageSize(window, browserDetails);
  5018. shimSendThrowTypeError(window);
  5019. break;
  5020. case 'safari':
  5021. if (!safariShim || !options.shimSafari) {
  5022. logging('Safari shim is not included in this adapter release.');
  5023. return adapter;
  5024. }
  5025. logging('adapter.js shimming safari.');
  5026. // Export to the adapter global object visible in the browser.
  5027. adapter.browserShim = safariShim;
  5028. // Must be called before shimCallbackAPI.
  5029. shimAddIceCandidateNullOrEmpty(window, browserDetails);
  5030. shimRTCIceServerUrls(window);
  5031. shimCreateOfferLegacy(window);
  5032. shimCallbacksAPI(window);
  5033. shimLocalStreamsAPI(window);
  5034. shimRemoteStreamsAPI(window);
  5035. shimTrackEventTransceiver(window);
  5036. shimGetUserMedia(window);
  5037. shimAudioContext(window);
  5038. shimRTCIceCandidate(window);
  5039. shimMaxMessageSize(window, browserDetails);
  5040. shimSendThrowTypeError(window);
  5041. removeExtmapAllowMixed(window, browserDetails);
  5042. break;
  5043. default:
  5044. logging('Unsupported browser!');
  5045. break;
  5046. }
  5047. return adapter;
  5048. }
  5049. /*
  5050. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  5051. *
  5052. * Use of this source code is governed by a BSD-style license
  5053. * that can be found in the LICENSE file in the root of the source
  5054. * tree.
  5055. */
  5056. adapterFactory({window: typeof window === 'undefined' ? undefined : window});
  5057. /**
  5058. * @class AudioTrackConstraints
  5059. * @classDesc Constraints for creating an audio MediaStreamTrack.
  5060. * @memberof Owt.Base
  5061. * @constructor
  5062. * @param {Owt.Base.AudioSourceInfo} source Source info of this audio track.
  5063. */
  5064. class AudioTrackConstraints {
  5065. // eslint-disable-next-line require-jsdoc
  5066. constructor(source) {
  5067. if (!Object.values(AudioSourceInfo).some(v => v === source)) {
  5068. throw new TypeError('Invalid source.');
  5069. }
  5070. /**
  5071. * @member {string} source
  5072. * @memberof Owt.Base.AudioTrackConstraints
  5073. * @desc Values could be "mic", "screen-cast", "file" or "mixed".
  5074. * @instance
  5075. */
  5076. this.source = source;
  5077. /**
  5078. * @member {string} deviceId
  5079. * @memberof Owt.Base.AudioTrackConstraints
  5080. * @desc Do not provide deviceId if source is not "mic".
  5081. * @instance
  5082. * @see https://w3c.github.io/mediacapture-main/#def-constraint-deviceId
  5083. */
  5084. this.deviceId = undefined;
  5085. }
  5086. }
  5087. /**
  5088. * @class VideoTrackConstraints
  5089. * @classDesc Constraints for creating a video MediaStreamTrack.
  5090. * @memberof Owt.Base
  5091. * @constructor
  5092. * @param {Owt.Base.VideoSourceInfo} source Source info of this video track.
  5093. */
  5094. class VideoTrackConstraints {
  5095. // eslint-disable-next-line require-jsdoc
  5096. constructor(source) {
  5097. if (!Object.values(VideoSourceInfo).some(v => v === source)) {
  5098. throw new TypeError('Invalid source.');
  5099. }
  5100. /**
  5101. * @member {string} source
  5102. * @memberof Owt.Base.VideoTrackConstraints
  5103. * @desc Values could be "camera", "screen-cast", "file" or "mixed".
  5104. * @instance
  5105. */
  5106. this.source = source;
  5107. /**
  5108. * @member {string} deviceId
  5109. * @memberof Owt.Base.VideoTrackConstraints
  5110. * @desc Do not provide deviceId if source is not "camera".
  5111. * @instance
  5112. * @see https://w3c.github.io/mediacapture-main/#def-constraint-deviceId
  5113. */
  5114. this.deviceId = undefined;
  5115. /**
  5116. * @member {Owt.Base.Resolution} resolution
  5117. * @memberof Owt.Base.VideoTrackConstraints
  5118. * @instance
  5119. */
  5120. this.resolution = undefined;
  5121. /**
  5122. * @member {number} frameRate
  5123. * @memberof Owt.Base.VideoTrackConstraints
  5124. * @instance
  5125. */
  5126. this.frameRate = undefined;
  5127. }
  5128. }
  5129. /**
  5130. * @class StreamConstraints
  5131. * @classDesc Constraints for creating a MediaStream from screen mic and camera.
  5132. * @memberof Owt.Base
  5133. * @constructor
  5134. * @param {?Owt.Base.AudioTrackConstraints} audioConstraints
  5135. * @param {?Owt.Base.VideoTrackConstraints} videoConstraints
  5136. */
  5137. class StreamConstraints {
  5138. // eslint-disable-next-line require-jsdoc
  5139. constructor(audioConstraints = false, videoConstraints = false) {
  5140. /**
  5141. * @member {Owt.Base.MediaStreamTrackDeviceConstraintsForAudio} audio
  5142. * @memberof Owt.Base.MediaStreamDeviceConstraints
  5143. * @instance
  5144. */
  5145. this.audio = audioConstraints;
  5146. /**
  5147. * @member {Owt.Base.MediaStreamTrackDeviceConstraintsForVideo} Video
  5148. * @memberof Owt.Base.MediaStreamDeviceConstraints
  5149. * @instance
  5150. */
  5151. this.video = videoConstraints;
  5152. }
  5153. } // eslint-disable-next-line require-jsdoc
  5154. function isVideoConstrainsForScreenCast(constraints) {
  5155. return typeof constraints.video === 'object' && constraints.video.source === VideoSourceInfo.SCREENCAST;
  5156. }
  5157. /**
  5158. * @class MediaStreamFactory
  5159. * @classDesc A factory to create MediaStream. You can also create MediaStream by yourself.
  5160. * @memberof Owt.Base
  5161. */
  5162. class MediaStreamFactory {
  5163. /**
  5164. * @function createMediaStream
  5165. * @static
  5166. * @desc Create a MediaStream with given constraints. If you want to create a MediaStream for screen cast, please make sure both audio and video's source are "screen-cast".
  5167. * @memberof Owt.Base.MediaStreamFactory
  5168. * @return {Promise<MediaStream, Error>} Return a promise that is resolved when stream is successfully created, or rejected if one of the following error happened:
  5169. * - One or more parameters cannot be satisfied.
  5170. * - Specified device is busy.
  5171. * - Cannot obtain necessary permission or operation is canceled by user.
  5172. * - Video source is screen cast, while audio source is not.
  5173. * - Audio source is screen cast, while video source is disabled.
  5174. * @param {Owt.Base.StreamConstraints} constraints
  5175. */
  5176. static createMediaStream(constraints) {
  5177. if (typeof constraints !== 'object' || !constraints.audio && !constraints.video) {
  5178. return Promise.reject(new TypeError('Invalid constrains'));
  5179. }
  5180. if (!isVideoConstrainsForScreenCast(constraints) && typeof constraints.audio === 'object' && constraints.audio.source === AudioSourceInfo.SCREENCAST) {
  5181. return Promise.reject(new TypeError('Cannot share screen without video.'));
  5182. }
  5183. if (isVideoConstrainsForScreenCast(constraints) && !isChrome() && !isFirefox()) {
  5184. return Promise.reject(new TypeError('Screen sharing only supports Chrome and Firefox.'));
  5185. }
  5186. if (isVideoConstrainsForScreenCast(constraints) && typeof constraints.audio === 'object' && constraints.audio.source !== AudioSourceInfo.SCREENCAST) {
  5187. return Promise.reject(new TypeError('Cannot capture video from screen cast while capture audio from' + ' other source.'));
  5188. } // Check and convert constraints.
  5189. if (!constraints.audio && !constraints.video) {
  5190. return Promise.reject(new TypeError('At least one of audio and video must be requested.'));
  5191. }
  5192. const mediaConstraints = Object.create({});
  5193. if (typeof constraints.audio === 'object' && constraints.audio.source === AudioSourceInfo.MIC) {
  5194. mediaConstraints.audio = Object.create({});
  5195. if (isEdge()) {
  5196. mediaConstraints.audio.deviceId = constraints.audio.deviceId;
  5197. } else {
  5198. mediaConstraints.audio.deviceId = {
  5199. exact: constraints.audio.deviceId
  5200. };
  5201. }
  5202. } else {
  5203. if (constraints.audio.source === AudioSourceInfo.SCREENCAST) {
  5204. mediaConstraints.audio = true;
  5205. } else {
  5206. mediaConstraints.audio = constraints.audio;
  5207. }
  5208. }
  5209. if (typeof constraints.video === 'object') {
  5210. mediaConstraints.video = Object.create({});
  5211. if (typeof constraints.video.frameRate === 'number') {
  5212. mediaConstraints.video.frameRate = constraints.video.frameRate;
  5213. }
  5214. if (constraints.video.resolution && constraints.video.resolution.width && constraints.video.resolution.height) {
  5215. if (constraints.video.source === VideoSourceInfo.SCREENCAST) {
  5216. mediaConstraints.video.width = constraints.video.resolution.width;
  5217. mediaConstraints.video.height = constraints.video.resolution.height;
  5218. } else {
  5219. mediaConstraints.video.width = Object.create({});
  5220. mediaConstraints.video.width.exact = constraints.video.resolution.width;
  5221. mediaConstraints.video.height = Object.create({});
  5222. mediaConstraints.video.height.exact = constraints.video.resolution.height;
  5223. }
  5224. }
  5225. if (typeof constraints.video.deviceId === 'string') {
  5226. mediaConstraints.video.deviceId = {
  5227. exact: constraints.video.deviceId
  5228. };
  5229. }
  5230. if (isFirefox() && constraints.video.source === VideoSourceInfo.SCREENCAST) {
  5231. mediaConstraints.video.mediaSource = 'screen';
  5232. }
  5233. } else {
  5234. mediaConstraints.video = constraints.video;
  5235. }
  5236. if (isVideoConstrainsForScreenCast(constraints)) {
  5237. return navigator.mediaDevices.getDisplayMedia(mediaConstraints);
  5238. } else {
  5239. return navigator.mediaDevices.getUserMedia(mediaConstraints);
  5240. }
  5241. }
  5242. }
  5243. // Copyright (C) <2018> Intel Corporation
  5244. var media = /*#__PURE__*/Object.freeze({
  5245. __proto__: null,
  5246. AudioTrackConstraints: AudioTrackConstraints,
  5247. VideoTrackConstraints: VideoTrackConstraints,
  5248. StreamConstraints: StreamConstraints,
  5249. MediaStreamFactory: MediaStreamFactory,
  5250. AudioSourceInfo: AudioSourceInfo,
  5251. VideoSourceInfo: VideoSourceInfo,
  5252. TrackKind: TrackKind,
  5253. Resolution: Resolution
  5254. });
  5255. let logger;
  5256. let errorLogger;
  5257. function setLogger() {
  5258. /*eslint-disable */
  5259. logger = console.log;
  5260. errorLogger = console.error;
  5261. /*eslint-enable */
  5262. }
  5263. function log(message, ...optionalParams) {
  5264. if (logger) {
  5265. logger(message, ...optionalParams);
  5266. }
  5267. }
  5268. function error(message, ...optionalParams) {
  5269. if (errorLogger) {
  5270. errorLogger(message, ...optionalParams);
  5271. }
  5272. }
  5273. class Event$1 {
  5274. constructor(type) {
  5275. this.listener = {};
  5276. this.type = type | '';
  5277. }
  5278. on(event, fn) {
  5279. if (!this.listener[event]) {
  5280. this.listener[event] = [];
  5281. }
  5282. this.listener[event].push(fn);
  5283. return true;
  5284. }
  5285. off(event, fn) {
  5286. if (this.listener[event]) {
  5287. var index = this.listener[event].indexOf(fn);
  5288. if (index > -1) {
  5289. this.listener[event].splice(index, 1);
  5290. }
  5291. return true;
  5292. }
  5293. return false;
  5294. }
  5295. offAll() {
  5296. this.listener = {};
  5297. }
  5298. dispatch(event, data) {
  5299. if (this.listener[event]) {
  5300. this.listener[event].map(each => {
  5301. each.apply(null, [data]);
  5302. });
  5303. return true;
  5304. }
  5305. return false;
  5306. }
  5307. }
  5308. var bind = function bind(fn, thisArg) {
  5309. return function wrap() {
  5310. var args = new Array(arguments.length);
  5311. for (var i = 0; i < args.length; i++) {
  5312. args[i] = arguments[i];
  5313. }
  5314. return fn.apply(thisArg, args);
  5315. };
  5316. };
  5317. /*global toString:true*/
  5318. // utils is a library of generic helper functions non-specific to axios
  5319. var toString = Object.prototype.toString;
  5320. /**
  5321. * Determine if a value is an Array
  5322. *
  5323. * @param {Object} val The value to test
  5324. * @returns {boolean} True if value is an Array, otherwise false
  5325. */
  5326. function isArray(val) {
  5327. return toString.call(val) === '[object Array]';
  5328. }
  5329. /**
  5330. * Determine if a value is undefined
  5331. *
  5332. * @param {Object} val The value to test
  5333. * @returns {boolean} True if the value is undefined, otherwise false
  5334. */
  5335. function isUndefined(val) {
  5336. return typeof val === 'undefined';
  5337. }
  5338. /**
  5339. * Determine if a value is a Buffer
  5340. *
  5341. * @param {Object} val The value to test
  5342. * @returns {boolean} True if value is a Buffer, otherwise false
  5343. */
  5344. function isBuffer(val) {
  5345. return val !== null && !isUndefined(val) && val.constructor !== null && !isUndefined(val.constructor)
  5346. && typeof val.constructor.isBuffer === 'function' && val.constructor.isBuffer(val);
  5347. }
  5348. /**
  5349. * Determine if a value is an ArrayBuffer
  5350. *
  5351. * @param {Object} val The value to test
  5352. * @returns {boolean} True if value is an ArrayBuffer, otherwise false
  5353. */
  5354. function isArrayBuffer(val) {
  5355. return toString.call(val) === '[object ArrayBuffer]';
  5356. }
  5357. /**
  5358. * Determine if a value is a FormData
  5359. *
  5360. * @param {Object} val The value to test
  5361. * @returns {boolean} True if value is an FormData, otherwise false
  5362. */
  5363. function isFormData(val) {
  5364. return (typeof FormData !== 'undefined') && (val instanceof FormData);
  5365. }
  5366. /**
  5367. * Determine if a value is a view on an ArrayBuffer
  5368. *
  5369. * @param {Object} val The value to test
  5370. * @returns {boolean} True if value is a view on an ArrayBuffer, otherwise false
  5371. */
  5372. function isArrayBufferView(val) {
  5373. var result;
  5374. if ((typeof ArrayBuffer !== 'undefined') && (ArrayBuffer.isView)) {
  5375. result = ArrayBuffer.isView(val);
  5376. } else {
  5377. result = (val) && (val.buffer) && (val.buffer instanceof ArrayBuffer);
  5378. }
  5379. return result;
  5380. }
  5381. /**
  5382. * Determine if a value is a String
  5383. *
  5384. * @param {Object} val The value to test
  5385. * @returns {boolean} True if value is a String, otherwise false
  5386. */
  5387. function isString(val) {
  5388. return typeof val === 'string';
  5389. }
  5390. /**
  5391. * Determine if a value is a Number
  5392. *
  5393. * @param {Object} val The value to test
  5394. * @returns {boolean} True if value is a Number, otherwise false
  5395. */
  5396. function isNumber(val) {
  5397. return typeof val === 'number';
  5398. }
  5399. /**
  5400. * Determine if a value is an Object
  5401. *
  5402. * @param {Object} val The value to test
  5403. * @returns {boolean} True if value is an Object, otherwise false
  5404. */
  5405. function isObject(val) {
  5406. return val !== null && typeof val === 'object';
  5407. }
  5408. /**
  5409. * Determine if a value is a plain Object
  5410. *
  5411. * @param {Object} val The value to test
  5412. * @return {boolean} True if value is a plain Object, otherwise false
  5413. */
  5414. function isPlainObject(val) {
  5415. if (toString.call(val) !== '[object Object]') {
  5416. return false;
  5417. }
  5418. var prototype = Object.getPrototypeOf(val);
  5419. return prototype === null || prototype === Object.prototype;
  5420. }
  5421. /**
  5422. * Determine if a value is a Date
  5423. *
  5424. * @param {Object} val The value to test
  5425. * @returns {boolean} True if value is a Date, otherwise false
  5426. */
  5427. function isDate(val) {
  5428. return toString.call(val) === '[object Date]';
  5429. }
  5430. /**
  5431. * Determine if a value is a File
  5432. *
  5433. * @param {Object} val The value to test
  5434. * @returns {boolean} True if value is a File, otherwise false
  5435. */
  5436. function isFile(val) {
  5437. return toString.call(val) === '[object File]';
  5438. }
  5439. /**
  5440. * Determine if a value is a Blob
  5441. *
  5442. * @param {Object} val The value to test
  5443. * @returns {boolean} True if value is a Blob, otherwise false
  5444. */
  5445. function isBlob(val) {
  5446. return toString.call(val) === '[object Blob]';
  5447. }
  5448. /**
  5449. * Determine if a value is a Function
  5450. *
  5451. * @param {Object} val The value to test
  5452. * @returns {boolean} True if value is a Function, otherwise false
  5453. */
  5454. function isFunction(val) {
  5455. return toString.call(val) === '[object Function]';
  5456. }
  5457. /**
  5458. * Determine if a value is a Stream
  5459. *
  5460. * @param {Object} val The value to test
  5461. * @returns {boolean} True if value is a Stream, otherwise false
  5462. */
  5463. function isStream(val) {
  5464. return isObject(val) && isFunction(val.pipe);
  5465. }
  5466. /**
  5467. * Determine if a value is a URLSearchParams object
  5468. *
  5469. * @param {Object} val The value to test
  5470. * @returns {boolean} True if value is a URLSearchParams object, otherwise false
  5471. */
  5472. function isURLSearchParams(val) {
  5473. return typeof URLSearchParams !== 'undefined' && val instanceof URLSearchParams;
  5474. }
  5475. /**
  5476. * Trim excess whitespace off the beginning and end of a string
  5477. *
  5478. * @param {String} str The String to trim
  5479. * @returns {String} The String freed of excess whitespace
  5480. */
  5481. function trim(str) {
  5482. return str.replace(/^\s*/, '').replace(/\s*$/, '');
  5483. }
  5484. /**
  5485. * Determine if we're running in a standard browser environment
  5486. *
  5487. * This allows axios to run in a web worker, and react-native.
  5488. * Both environments support XMLHttpRequest, but not fully standard globals.
  5489. *
  5490. * web workers:
  5491. * typeof window -> undefined
  5492. * typeof document -> undefined
  5493. *
  5494. * react-native:
  5495. * navigator.product -> 'ReactNative'
  5496. * nativescript
  5497. * navigator.product -> 'NativeScript' or 'NS'
  5498. */
  5499. function isStandardBrowserEnv() {
  5500. if (typeof navigator !== 'undefined' && (navigator.product === 'ReactNative' ||
  5501. navigator.product === 'NativeScript' ||
  5502. navigator.product === 'NS')) {
  5503. return false;
  5504. }
  5505. return (
  5506. typeof window !== 'undefined' &&
  5507. typeof document !== 'undefined'
  5508. );
  5509. }
  5510. /**
  5511. * Iterate over an Array or an Object invoking a function for each item.
  5512. *
  5513. * If `obj` is an Array callback will be called passing
  5514. * the value, index, and complete array for each item.
  5515. *
  5516. * If 'obj' is an Object callback will be called passing
  5517. * the value, key, and complete object for each property.
  5518. *
  5519. * @param {Object|Array} obj The object to iterate
  5520. * @param {Function} fn The callback to invoke for each item
  5521. */
  5522. function forEach(obj, fn) {
  5523. // Don't bother if no value provided
  5524. if (obj === null || typeof obj === 'undefined') {
  5525. return;
  5526. }
  5527. // Force an array if not already something iterable
  5528. if (typeof obj !== 'object') {
  5529. /*eslint no-param-reassign:0*/
  5530. obj = [obj];
  5531. }
  5532. if (isArray(obj)) {
  5533. // Iterate over array values
  5534. for (var i = 0, l = obj.length; i < l; i++) {
  5535. fn.call(null, obj[i], i, obj);
  5536. }
  5537. } else {
  5538. // Iterate over object keys
  5539. for (var key in obj) {
  5540. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  5541. fn.call(null, obj[key], key, obj);
  5542. }
  5543. }
  5544. }
  5545. }
  5546. /**
  5547. * Accepts varargs expecting each argument to be an object, then
  5548. * immutably merges the properties of each object and returns result.
  5549. *
  5550. * When multiple objects contain the same key the later object in
  5551. * the arguments list will take precedence.
  5552. *
  5553. * Example:
  5554. *
  5555. * ```js
  5556. * var result = merge({foo: 123}, {foo: 456});
  5557. * console.log(result.foo); // outputs 456
  5558. * ```
  5559. *
  5560. * @param {Object} obj1 Object to merge
  5561. * @returns {Object} Result of all merge properties
  5562. */
  5563. function merge(/* obj1, obj2, obj3, ... */) {
  5564. var result = {};
  5565. function assignValue(val, key) {
  5566. if (isPlainObject(result[key]) && isPlainObject(val)) {
  5567. result[key] = merge(result[key], val);
  5568. } else if (isPlainObject(val)) {
  5569. result[key] = merge({}, val);
  5570. } else if (isArray(val)) {
  5571. result[key] = val.slice();
  5572. } else {
  5573. result[key] = val;
  5574. }
  5575. }
  5576. for (var i = 0, l = arguments.length; i < l; i++) {
  5577. forEach(arguments[i], assignValue);
  5578. }
  5579. return result;
  5580. }
  5581. /**
  5582. * Extends object a by mutably adding to it the properties of object b.
  5583. *
  5584. * @param {Object} a The object to be extended
  5585. * @param {Object} b The object to copy properties from
  5586. * @param {Object} thisArg The object to bind function to
  5587. * @return {Object} The resulting value of object a
  5588. */
  5589. function extend(a, b, thisArg) {
  5590. forEach(b, function assignValue(val, key) {
  5591. if (thisArg && typeof val === 'function') {
  5592. a[key] = bind(val, thisArg);
  5593. } else {
  5594. a[key] = val;
  5595. }
  5596. });
  5597. return a;
  5598. }
  5599. /**
  5600. * Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
  5601. *
  5602. * @param {string} content with BOM
  5603. * @return {string} content value without BOM
  5604. */
  5605. function stripBOM(content) {
  5606. if (content.charCodeAt(0) === 0xFEFF) {
  5607. content = content.slice(1);
  5608. }
  5609. return content;
  5610. }
  5611. var utils = {
  5612. isArray: isArray,
  5613. isArrayBuffer: isArrayBuffer,
  5614. isBuffer: isBuffer,
  5615. isFormData: isFormData,
  5616. isArrayBufferView: isArrayBufferView,
  5617. isString: isString,
  5618. isNumber: isNumber,
  5619. isObject: isObject,
  5620. isPlainObject: isPlainObject,
  5621. isUndefined: isUndefined,
  5622. isDate: isDate,
  5623. isFile: isFile,
  5624. isBlob: isBlob,
  5625. isFunction: isFunction,
  5626. isStream: isStream,
  5627. isURLSearchParams: isURLSearchParams,
  5628. isStandardBrowserEnv: isStandardBrowserEnv,
  5629. forEach: forEach,
  5630. merge: merge,
  5631. extend: extend,
  5632. trim: trim,
  5633. stripBOM: stripBOM
  5634. };
  5635. function encode(val) {
  5636. return encodeURIComponent(val).
  5637. replace(/%3A/gi, ':').
  5638. replace(/%24/g, '$').
  5639. replace(/%2C/gi, ',').
  5640. replace(/%20/g, '+').
  5641. replace(/%5B/gi, '[').
  5642. replace(/%5D/gi, ']');
  5643. }
  5644. /**
  5645. * Build a URL by appending params to the end
  5646. *
  5647. * @param {string} url The base of the url (e.g., http://www.google.com)
  5648. * @param {object} [params] The params to be appended
  5649. * @returns {string} The formatted url
  5650. */
  5651. var buildURL = function buildURL(url, params, paramsSerializer) {
  5652. /*eslint no-param-reassign:0*/
  5653. if (!params) {
  5654. return url;
  5655. }
  5656. var serializedParams;
  5657. if (paramsSerializer) {
  5658. serializedParams = paramsSerializer(params);
  5659. } else if (utils.isURLSearchParams(params)) {
  5660. serializedParams = params.toString();
  5661. } else {
  5662. var parts = [];
  5663. utils.forEach(params, function serialize(val, key) {
  5664. if (val === null || typeof val === 'undefined') {
  5665. return;
  5666. }
  5667. if (utils.isArray(val)) {
  5668. key = key + '[]';
  5669. } else {
  5670. val = [val];
  5671. }
  5672. utils.forEach(val, function parseValue(v) {
  5673. if (utils.isDate(v)) {
  5674. v = v.toISOString();
  5675. } else if (utils.isObject(v)) {
  5676. v = JSON.stringify(v);
  5677. }
  5678. parts.push(encode(key) + '=' + encode(v));
  5679. });
  5680. });
  5681. serializedParams = parts.join('&');
  5682. }
  5683. if (serializedParams) {
  5684. var hashmarkIndex = url.indexOf('#');
  5685. if (hashmarkIndex !== -1) {
  5686. url = url.slice(0, hashmarkIndex);
  5687. }
  5688. url += (url.indexOf('?') === -1 ? '?' : '&') + serializedParams;
  5689. }
  5690. return url;
  5691. };
  5692. function InterceptorManager() {
  5693. this.handlers = [];
  5694. }
  5695. /**
  5696. * Add a new interceptor to the stack
  5697. *
  5698. * @param {Function} fulfilled The function to handle `then` for a `Promise`
  5699. * @param {Function} rejected The function to handle `reject` for a `Promise`
  5700. *
  5701. * @return {Number} An ID used to remove interceptor later
  5702. */
  5703. InterceptorManager.prototype.use = function use(fulfilled, rejected) {
  5704. this.handlers.push({
  5705. fulfilled: fulfilled,
  5706. rejected: rejected
  5707. });
  5708. return this.handlers.length - 1;
  5709. };
  5710. /**
  5711. * Remove an interceptor from the stack
  5712. *
  5713. * @param {Number} id The ID that was returned by `use`
  5714. */
  5715. InterceptorManager.prototype.eject = function eject(id) {
  5716. if (this.handlers[id]) {
  5717. this.handlers[id] = null;
  5718. }
  5719. };
  5720. /**
  5721. * Iterate over all the registered interceptors
  5722. *
  5723. * This method is particularly useful for skipping over any
  5724. * interceptors that may have become `null` calling `eject`.
  5725. *
  5726. * @param {Function} fn The function to call for each interceptor
  5727. */
  5728. InterceptorManager.prototype.forEach = function forEach(fn) {
  5729. utils.forEach(this.handlers, function forEachHandler(h) {
  5730. if (h !== null) {
  5731. fn(h);
  5732. }
  5733. });
  5734. };
  5735. var InterceptorManager_1 = InterceptorManager;
  5736. /**
  5737. * Transform the data for a request or a response
  5738. *
  5739. * @param {Object|String} data The data to be transformed
  5740. * @param {Array} headers The headers for the request or response
  5741. * @param {Array|Function} fns A single function or Array of functions
  5742. * @returns {*} The resulting transformed data
  5743. */
  5744. var transformData = function transformData(data, headers, fns) {
  5745. /*eslint no-param-reassign:0*/
  5746. utils.forEach(fns, function transform(fn) {
  5747. data = fn(data, headers);
  5748. });
  5749. return data;
  5750. };
  5751. var isCancel = function isCancel(value) {
  5752. return !!(value && value.__CANCEL__);
  5753. };
  5754. var normalizeHeaderName = function normalizeHeaderName(headers, normalizedName) {
  5755. utils.forEach(headers, function processHeader(value, name) {
  5756. if (name !== normalizedName && name.toUpperCase() === normalizedName.toUpperCase()) {
  5757. headers[normalizedName] = value;
  5758. delete headers[name];
  5759. }
  5760. });
  5761. };
  5762. /**
  5763. * Update an Error with the specified config, error code, and response.
  5764. *
  5765. * @param {Error} error The error to update.
  5766. * @param {Object} config The config.
  5767. * @param {string} [code] The error code (for example, 'ECONNABORTED').
  5768. * @param {Object} [request] The request.
  5769. * @param {Object} [response] The response.
  5770. * @returns {Error} The error.
  5771. */
  5772. var enhanceError = function enhanceError(error, config, code, request, response) {
  5773. error.config = config;
  5774. if (code) {
  5775. error.code = code;
  5776. }
  5777. error.request = request;
  5778. error.response = response;
  5779. error.isAxiosError = true;
  5780. error.toJSON = function toJSON() {
  5781. return {
  5782. // Standard
  5783. message: this.message,
  5784. name: this.name,
  5785. // Microsoft
  5786. description: this.description,
  5787. number: this.number,
  5788. // Mozilla
  5789. fileName: this.fileName,
  5790. lineNumber: this.lineNumber,
  5791. columnNumber: this.columnNumber,
  5792. stack: this.stack,
  5793. // Axios
  5794. config: this.config,
  5795. code: this.code
  5796. };
  5797. };
  5798. return error;
  5799. };
  5800. /**
  5801. * Create an Error with the specified message, config, error code, request and response.
  5802. *
  5803. * @param {string} message The error message.
  5804. * @param {Object} config The config.
  5805. * @param {string} [code] The error code (for example, 'ECONNABORTED').
  5806. * @param {Object} [request] The request.
  5807. * @param {Object} [response] The response.
  5808. * @returns {Error} The created error.
  5809. */
  5810. var createError = function createError(message, config, code, request, response) {
  5811. var error = new Error(message);
  5812. return enhanceError(error, config, code, request, response);
  5813. };
  5814. /**
  5815. * Resolve or reject a Promise based on response status.
  5816. *
  5817. * @param {Function} resolve A function that resolves the promise.
  5818. * @param {Function} reject A function that rejects the promise.
  5819. * @param {object} response The response.
  5820. */
  5821. var settle = function settle(resolve, reject, response) {
  5822. var validateStatus = response.config.validateStatus;
  5823. if (!response.status || !validateStatus || validateStatus(response.status)) {
  5824. resolve(response);
  5825. } else {
  5826. reject(createError(
  5827. 'Request failed with status code ' + response.status,
  5828. response.config,
  5829. null,
  5830. response.request,
  5831. response
  5832. ));
  5833. }
  5834. };
  5835. var cookies = (
  5836. utils.isStandardBrowserEnv() ?
  5837. // Standard browser envs support document.cookie
  5838. (function standardBrowserEnv() {
  5839. return {
  5840. write: function write(name, value, expires, path, domain, secure) {
  5841. var cookie = [];
  5842. cookie.push(name + '=' + encodeURIComponent(value));
  5843. if (utils.isNumber(expires)) {
  5844. cookie.push('expires=' + new Date(expires).toGMTString());
  5845. }
  5846. if (utils.isString(path)) {
  5847. cookie.push('path=' + path);
  5848. }
  5849. if (utils.isString(domain)) {
  5850. cookie.push('domain=' + domain);
  5851. }
  5852. if (secure === true) {
  5853. cookie.push('secure');
  5854. }
  5855. document.cookie = cookie.join('; ');
  5856. },
  5857. read: function read(name) {
  5858. var match = document.cookie.match(new RegExp('(^|;\\s*)(' + name + ')=([^;]*)'));
  5859. return (match ? decodeURIComponent(match[3]) : null);
  5860. },
  5861. remove: function remove(name) {
  5862. this.write(name, '', Date.now() - 86400000);
  5863. }
  5864. };
  5865. })() :
  5866. // Non standard browser env (web workers, react-native) lack needed support.
  5867. (function nonStandardBrowserEnv() {
  5868. return {
  5869. write: function write() {},
  5870. read: function read() { return null; },
  5871. remove: function remove() {}
  5872. };
  5873. })()
  5874. );
  5875. /**
  5876. * Determines whether the specified URL is absolute
  5877. *
  5878. * @param {string} url The URL to test
  5879. * @returns {boolean} True if the specified URL is absolute, otherwise false
  5880. */
  5881. var isAbsoluteURL = function isAbsoluteURL(url) {
  5882. // A URL is considered absolute if it begins with "<scheme>://" or "//" (protocol-relative URL).
  5883. // RFC 3986 defines scheme name as a sequence of characters beginning with a letter and followed
  5884. // by any combination of letters, digits, plus, period, or hyphen.
  5885. return /^([a-z][a-z\d\+\-\.]*:)?\/\//i.test(url);
  5886. };
  5887. /**
  5888. * Creates a new URL by combining the specified URLs
  5889. *
  5890. * @param {string} baseURL The base URL
  5891. * @param {string} relativeURL The relative URL
  5892. * @returns {string} The combined URL
  5893. */
  5894. var combineURLs = function combineURLs(baseURL, relativeURL) {
  5895. return relativeURL
  5896. ? baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '')
  5897. : baseURL;
  5898. };
  5899. /**
  5900. * Creates a new URL by combining the baseURL with the requestedURL,
  5901. * only when the requestedURL is not already an absolute URL.
  5902. * If the requestURL is absolute, this function returns the requestedURL untouched.
  5903. *
  5904. * @param {string} baseURL The base URL
  5905. * @param {string} requestedURL Absolute or relative URL to combine
  5906. * @returns {string} The combined full path
  5907. */
  5908. var buildFullPath = function buildFullPath(baseURL, requestedURL) {
  5909. if (baseURL && !isAbsoluteURL(requestedURL)) {
  5910. return combineURLs(baseURL, requestedURL);
  5911. }
  5912. return requestedURL;
  5913. };
  5914. // Headers whose duplicates are ignored by node
  5915. // c.f. https://nodejs.org/api/http.html#http_message_headers
  5916. var ignoreDuplicateOf = [
  5917. 'age', 'authorization', 'content-length', 'content-type', 'etag',
  5918. 'expires', 'from', 'host', 'if-modified-since', 'if-unmodified-since',
  5919. 'last-modified', 'location', 'max-forwards', 'proxy-authorization',
  5920. 'referer', 'retry-after', 'user-agent'
  5921. ];
  5922. /**
  5923. * Parse headers into an object
  5924. *
  5925. * ```
  5926. * Date: Wed, 27 Aug 2014 08:58:49 GMT
  5927. * Content-Type: application/json
  5928. * Connection: keep-alive
  5929. * Transfer-Encoding: chunked
  5930. * ```
  5931. *
  5932. * @param {String} headers Headers needing to be parsed
  5933. * @returns {Object} Headers parsed into an object
  5934. */
  5935. var parseHeaders = function parseHeaders(headers) {
  5936. var parsed = {};
  5937. var key;
  5938. var val;
  5939. var i;
  5940. if (!headers) { return parsed; }
  5941. utils.forEach(headers.split('\n'), function parser(line) {
  5942. i = line.indexOf(':');
  5943. key = utils.trim(line.substr(0, i)).toLowerCase();
  5944. val = utils.trim(line.substr(i + 1));
  5945. if (key) {
  5946. if (parsed[key] && ignoreDuplicateOf.indexOf(key) >= 0) {
  5947. return;
  5948. }
  5949. if (key === 'set-cookie') {
  5950. parsed[key] = (parsed[key] ? parsed[key] : []).concat([val]);
  5951. } else {
  5952. parsed[key] = parsed[key] ? parsed[key] + ', ' + val : val;
  5953. }
  5954. }
  5955. });
  5956. return parsed;
  5957. };
  5958. var isURLSameOrigin = (
  5959. utils.isStandardBrowserEnv() ?
  5960. // Standard browser envs have full support of the APIs needed to test
  5961. // whether the request URL is of the same origin as current location.
  5962. (function standardBrowserEnv() {
  5963. var msie = /(msie|trident)/i.test(navigator.userAgent);
  5964. var urlParsingNode = document.createElement('a');
  5965. var originURL;
  5966. /**
  5967. * Parse a URL to discover it's components
  5968. *
  5969. * @param {String} url The URL to be parsed
  5970. * @returns {Object}
  5971. */
  5972. function resolveURL(url) {
  5973. var href = url;
  5974. if (msie) {
  5975. // IE needs attribute set twice to normalize properties
  5976. urlParsingNode.setAttribute('href', href);
  5977. href = urlParsingNode.href;
  5978. }
  5979. urlParsingNode.setAttribute('href', href);
  5980. // urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils
  5981. return {
  5982. href: urlParsingNode.href,
  5983. protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '',
  5984. host: urlParsingNode.host,
  5985. search: urlParsingNode.search ? urlParsingNode.search.replace(/^\?/, '') : '',
  5986. hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '',
  5987. hostname: urlParsingNode.hostname,
  5988. port: urlParsingNode.port,
  5989. pathname: (urlParsingNode.pathname.charAt(0) === '/') ?
  5990. urlParsingNode.pathname :
  5991. '/' + urlParsingNode.pathname
  5992. };
  5993. }
  5994. originURL = resolveURL(window.location.href);
  5995. /**
  5996. * Determine if a URL shares the same origin as the current location
  5997. *
  5998. * @param {String} requestURL The URL to test
  5999. * @returns {boolean} True if URL shares the same origin, otherwise false
  6000. */
  6001. return function isURLSameOrigin(requestURL) {
  6002. var parsed = (utils.isString(requestURL)) ? resolveURL(requestURL) : requestURL;
  6003. return (parsed.protocol === originURL.protocol &&
  6004. parsed.host === originURL.host);
  6005. };
  6006. })() :
  6007. // Non standard browser envs (web workers, react-native) lack needed support.
  6008. (function nonStandardBrowserEnv() {
  6009. return function isURLSameOrigin() {
  6010. return true;
  6011. };
  6012. })()
  6013. );
  6014. var xhr = function xhrAdapter(config) {
  6015. return new Promise(function dispatchXhrRequest(resolve, reject) {
  6016. var requestData = config.data;
  6017. var requestHeaders = config.headers;
  6018. if (utils.isFormData(requestData)) {
  6019. delete requestHeaders['Content-Type']; // Let the browser set it
  6020. }
  6021. var request = new XMLHttpRequest();
  6022. // HTTP basic authentication
  6023. if (config.auth) {
  6024. var username = config.auth.username || '';
  6025. var password = config.auth.password ? unescape(encodeURIComponent(config.auth.password)) : '';
  6026. requestHeaders.Authorization = 'Basic ' + btoa(username + ':' + password);
  6027. }
  6028. var fullPath = buildFullPath(config.baseURL, config.url);
  6029. request.open(config.method.toUpperCase(), buildURL(fullPath, config.params, config.paramsSerializer), true);
  6030. // Set the request timeout in MS
  6031. request.timeout = config.timeout;
  6032. // Listen for ready state
  6033. request.onreadystatechange = function handleLoad() {
  6034. if (!request || request.readyState !== 4) {
  6035. return;
  6036. }
  6037. // The request errored out and we didn't get a response, this will be
  6038. // handled by onerror instead
  6039. // With one exception: request that using file: protocol, most browsers
  6040. // will return status as 0 even though it's a successful request
  6041. if (request.status === 0 && !(request.responseURL && request.responseURL.indexOf('file:') === 0)) {
  6042. return;
  6043. }
  6044. // Prepare the response
  6045. var responseHeaders = 'getAllResponseHeaders' in request ? parseHeaders(request.getAllResponseHeaders()) : null;
  6046. var responseData = !config.responseType || config.responseType === 'text' ? request.responseText : request.response;
  6047. var response = {
  6048. data: responseData,
  6049. status: request.status,
  6050. statusText: request.statusText,
  6051. headers: responseHeaders,
  6052. config: config,
  6053. request: request
  6054. };
  6055. settle(resolve, reject, response);
  6056. // Clean up request
  6057. request = null;
  6058. };
  6059. // Handle browser request cancellation (as opposed to a manual cancellation)
  6060. request.onabort = function handleAbort() {
  6061. if (!request) {
  6062. return;
  6063. }
  6064. reject(createError('Request aborted', config, 'ECONNABORTED', request));
  6065. // Clean up request
  6066. request = null;
  6067. };
  6068. // Handle low level network errors
  6069. request.onerror = function handleError() {
  6070. // Real errors are hidden from us by the browser
  6071. // onerror should only fire if it's a network error
  6072. reject(createError('Network Error', config, null, request));
  6073. // Clean up request
  6074. request = null;
  6075. };
  6076. // Handle timeout
  6077. request.ontimeout = function handleTimeout() {
  6078. var timeoutErrorMessage = 'timeout of ' + config.timeout + 'ms exceeded';
  6079. if (config.timeoutErrorMessage) {
  6080. timeoutErrorMessage = config.timeoutErrorMessage;
  6081. }
  6082. reject(createError(timeoutErrorMessage, config, 'ECONNABORTED',
  6083. request));
  6084. // Clean up request
  6085. request = null;
  6086. };
  6087. // Add xsrf header
  6088. // This is only done if running in a standard browser environment.
  6089. // Specifically not if we're in a web worker, or react-native.
  6090. if (utils.isStandardBrowserEnv()) {
  6091. // Add xsrf header
  6092. var xsrfValue = (config.withCredentials || isURLSameOrigin(fullPath)) && config.xsrfCookieName ?
  6093. cookies.read(config.xsrfCookieName) :
  6094. undefined;
  6095. if (xsrfValue) {
  6096. requestHeaders[config.xsrfHeaderName] = xsrfValue;
  6097. }
  6098. }
  6099. // Add headers to the request
  6100. if ('setRequestHeader' in request) {
  6101. utils.forEach(requestHeaders, function setRequestHeader(val, key) {
  6102. if (typeof requestData === 'undefined' && key.toLowerCase() === 'content-type') {
  6103. // Remove Content-Type if data is undefined
  6104. delete requestHeaders[key];
  6105. } else {
  6106. // Otherwise add header to the request
  6107. request.setRequestHeader(key, val);
  6108. }
  6109. });
  6110. }
  6111. // Add withCredentials to request if needed
  6112. if (!utils.isUndefined(config.withCredentials)) {
  6113. request.withCredentials = !!config.withCredentials;
  6114. }
  6115. // Add responseType to request if needed
  6116. if (config.responseType) {
  6117. try {
  6118. request.responseType = config.responseType;
  6119. } catch (e) {
  6120. // Expected DOMException thrown by browsers not compatible XMLHttpRequest Level 2.
  6121. // But, this can be suppressed for 'json' type as it can be parsed by default 'transformResponse' function.
  6122. if (config.responseType !== 'json') {
  6123. throw e;
  6124. }
  6125. }
  6126. }
  6127. // Handle progress if needed
  6128. if (typeof config.onDownloadProgress === 'function') {
  6129. request.addEventListener('progress', config.onDownloadProgress);
  6130. }
  6131. // Not all browsers support upload events
  6132. if (typeof config.onUploadProgress === 'function' && request.upload) {
  6133. request.upload.addEventListener('progress', config.onUploadProgress);
  6134. }
  6135. if (config.cancelToken) {
  6136. // Handle cancellation
  6137. config.cancelToken.promise.then(function onCanceled(cancel) {
  6138. if (!request) {
  6139. return;
  6140. }
  6141. request.abort();
  6142. reject(cancel);
  6143. // Clean up request
  6144. request = null;
  6145. });
  6146. }
  6147. if (!requestData) {
  6148. requestData = null;
  6149. }
  6150. // Send the request
  6151. request.send(requestData);
  6152. });
  6153. };
  6154. var DEFAULT_CONTENT_TYPE = {
  6155. 'Content-Type': 'application/x-www-form-urlencoded'
  6156. };
  6157. function setContentTypeIfUnset(headers, value) {
  6158. if (!utils.isUndefined(headers) && utils.isUndefined(headers['Content-Type'])) {
  6159. headers['Content-Type'] = value;
  6160. }
  6161. }
  6162. function getDefaultAdapter() {
  6163. var adapter;
  6164. if (typeof XMLHttpRequest !== 'undefined') {
  6165. // For browsers use XHR adapter
  6166. adapter = xhr;
  6167. } else if (typeof process !== 'undefined' && Object.prototype.toString.call(process) === '[object process]') {
  6168. // For node use HTTP adapter
  6169. adapter = xhr;
  6170. }
  6171. return adapter;
  6172. }
  6173. var defaults = {
  6174. adapter: getDefaultAdapter(),
  6175. transformRequest: [function transformRequest(data, headers) {
  6176. normalizeHeaderName(headers, 'Accept');
  6177. normalizeHeaderName(headers, 'Content-Type');
  6178. if (utils.isFormData(data) ||
  6179. utils.isArrayBuffer(data) ||
  6180. utils.isBuffer(data) ||
  6181. utils.isStream(data) ||
  6182. utils.isFile(data) ||
  6183. utils.isBlob(data)
  6184. ) {
  6185. return data;
  6186. }
  6187. if (utils.isArrayBufferView(data)) {
  6188. return data.buffer;
  6189. }
  6190. if (utils.isURLSearchParams(data)) {
  6191. setContentTypeIfUnset(headers, 'application/x-www-form-urlencoded;charset=utf-8');
  6192. return data.toString();
  6193. }
  6194. if (utils.isObject(data)) {
  6195. setContentTypeIfUnset(headers, 'application/json;charset=utf-8');
  6196. return JSON.stringify(data);
  6197. }
  6198. return data;
  6199. }],
  6200. transformResponse: [function transformResponse(data) {
  6201. /*eslint no-param-reassign:0*/
  6202. if (typeof data === 'string') {
  6203. try {
  6204. data = JSON.parse(data);
  6205. } catch (e) { /* Ignore */ }
  6206. }
  6207. return data;
  6208. }],
  6209. /**
  6210. * A timeout in milliseconds to abort a request. If set to 0 (default) a
  6211. * timeout is not created.
  6212. */
  6213. timeout: 0,
  6214. xsrfCookieName: 'XSRF-TOKEN',
  6215. xsrfHeaderName: 'X-XSRF-TOKEN',
  6216. maxContentLength: -1,
  6217. maxBodyLength: -1,
  6218. validateStatus: function validateStatus(status) {
  6219. return status >= 200 && status < 300;
  6220. }
  6221. };
  6222. defaults.headers = {
  6223. common: {
  6224. 'Accept': 'application/json, text/plain, */*'
  6225. }
  6226. };
  6227. utils.forEach(['delete', 'get', 'head'], function forEachMethodNoData(method) {
  6228. defaults.headers[method] = {};
  6229. });
  6230. utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
  6231. defaults.headers[method] = utils.merge(DEFAULT_CONTENT_TYPE);
  6232. });
  6233. var defaults_1 = defaults;
  6234. /**
  6235. * Throws a `Cancel` if cancellation has been requested.
  6236. */
  6237. function throwIfCancellationRequested(config) {
  6238. if (config.cancelToken) {
  6239. config.cancelToken.throwIfRequested();
  6240. }
  6241. }
  6242. /**
  6243. * Dispatch a request to the server using the configured adapter.
  6244. *
  6245. * @param {object} config The config that is to be used for the request
  6246. * @returns {Promise} The Promise to be fulfilled
  6247. */
  6248. var dispatchRequest = function dispatchRequest(config) {
  6249. throwIfCancellationRequested(config);
  6250. // Ensure headers exist
  6251. config.headers = config.headers || {};
  6252. // Transform request data
  6253. config.data = transformData(
  6254. config.data,
  6255. config.headers,
  6256. config.transformRequest
  6257. );
  6258. // Flatten headers
  6259. config.headers = utils.merge(
  6260. config.headers.common || {},
  6261. config.headers[config.method] || {},
  6262. config.headers
  6263. );
  6264. utils.forEach(
  6265. ['delete', 'get', 'head', 'post', 'put', 'patch', 'common'],
  6266. function cleanHeaderConfig(method) {
  6267. delete config.headers[method];
  6268. }
  6269. );
  6270. var adapter = config.adapter || defaults_1.adapter;
  6271. return adapter(config).then(function onAdapterResolution(response) {
  6272. throwIfCancellationRequested(config);
  6273. // Transform response data
  6274. response.data = transformData(
  6275. response.data,
  6276. response.headers,
  6277. config.transformResponse
  6278. );
  6279. return response;
  6280. }, function onAdapterRejection(reason) {
  6281. if (!isCancel(reason)) {
  6282. throwIfCancellationRequested(config);
  6283. // Transform response data
  6284. if (reason && reason.response) {
  6285. reason.response.data = transformData(
  6286. reason.response.data,
  6287. reason.response.headers,
  6288. config.transformResponse
  6289. );
  6290. }
  6291. }
  6292. return Promise.reject(reason);
  6293. });
  6294. };
  6295. /**
  6296. * Config-specific merge-function which creates a new config-object
  6297. * by merging two configuration objects together.
  6298. *
  6299. * @param {Object} config1
  6300. * @param {Object} config2
  6301. * @returns {Object} New object resulting from merging config2 to config1
  6302. */
  6303. var mergeConfig = function mergeConfig(config1, config2) {
  6304. // eslint-disable-next-line no-param-reassign
  6305. config2 = config2 || {};
  6306. var config = {};
  6307. var valueFromConfig2Keys = ['url', 'method', 'data'];
  6308. var mergeDeepPropertiesKeys = ['headers', 'auth', 'proxy', 'params'];
  6309. var defaultToConfig2Keys = [
  6310. 'baseURL', 'transformRequest', 'transformResponse', 'paramsSerializer',
  6311. 'timeout', 'timeoutMessage', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
  6312. 'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress', 'decompress',
  6313. 'maxContentLength', 'maxBodyLength', 'maxRedirects', 'transport', 'httpAgent',
  6314. 'httpsAgent', 'cancelToken', 'socketPath', 'responseEncoding'
  6315. ];
  6316. var directMergeKeys = ['validateStatus'];
  6317. function getMergedValue(target, source) {
  6318. if (utils.isPlainObject(target) && utils.isPlainObject(source)) {
  6319. return utils.merge(target, source);
  6320. } else if (utils.isPlainObject(source)) {
  6321. return utils.merge({}, source);
  6322. } else if (utils.isArray(source)) {
  6323. return source.slice();
  6324. }
  6325. return source;
  6326. }
  6327. function mergeDeepProperties(prop) {
  6328. if (!utils.isUndefined(config2[prop])) {
  6329. config[prop] = getMergedValue(config1[prop], config2[prop]);
  6330. } else if (!utils.isUndefined(config1[prop])) {
  6331. config[prop] = getMergedValue(undefined, config1[prop]);
  6332. }
  6333. }
  6334. utils.forEach(valueFromConfig2Keys, function valueFromConfig2(prop) {
  6335. if (!utils.isUndefined(config2[prop])) {
  6336. config[prop] = getMergedValue(undefined, config2[prop]);
  6337. }
  6338. });
  6339. utils.forEach(mergeDeepPropertiesKeys, mergeDeepProperties);
  6340. utils.forEach(defaultToConfig2Keys, function defaultToConfig2(prop) {
  6341. if (!utils.isUndefined(config2[prop])) {
  6342. config[prop] = getMergedValue(undefined, config2[prop]);
  6343. } else if (!utils.isUndefined(config1[prop])) {
  6344. config[prop] = getMergedValue(undefined, config1[prop]);
  6345. }
  6346. });
  6347. utils.forEach(directMergeKeys, function merge(prop) {
  6348. if (prop in config2) {
  6349. config[prop] = getMergedValue(config1[prop], config2[prop]);
  6350. } else if (prop in config1) {
  6351. config[prop] = getMergedValue(undefined, config1[prop]);
  6352. }
  6353. });
  6354. var axiosKeys = valueFromConfig2Keys
  6355. .concat(mergeDeepPropertiesKeys)
  6356. .concat(defaultToConfig2Keys)
  6357. .concat(directMergeKeys);
  6358. var otherKeys = Object
  6359. .keys(config1)
  6360. .concat(Object.keys(config2))
  6361. .filter(function filterAxiosKeys(key) {
  6362. return axiosKeys.indexOf(key) === -1;
  6363. });
  6364. utils.forEach(otherKeys, mergeDeepProperties);
  6365. return config;
  6366. };
  6367. /**
  6368. * Create a new instance of Axios
  6369. *
  6370. * @param {Object} instanceConfig The default config for the instance
  6371. */
  6372. function Axios(instanceConfig) {
  6373. this.defaults = instanceConfig;
  6374. this.interceptors = {
  6375. request: new InterceptorManager_1(),
  6376. response: new InterceptorManager_1()
  6377. };
  6378. }
  6379. /**
  6380. * Dispatch a request
  6381. *
  6382. * @param {Object} config The config specific for this request (merged with this.defaults)
  6383. */
  6384. Axios.prototype.request = function request(config) {
  6385. /*eslint no-param-reassign:0*/
  6386. // Allow for axios('example/url'[, config]) a la fetch API
  6387. if (typeof config === 'string') {
  6388. config = arguments[1] || {};
  6389. config.url = arguments[0];
  6390. } else {
  6391. config = config || {};
  6392. }
  6393. config = mergeConfig(this.defaults, config);
  6394. // Set config.method
  6395. if (config.method) {
  6396. config.method = config.method.toLowerCase();
  6397. } else if (this.defaults.method) {
  6398. config.method = this.defaults.method.toLowerCase();
  6399. } else {
  6400. config.method = 'get';
  6401. }
  6402. // Hook up interceptors middleware
  6403. var chain = [dispatchRequest, undefined];
  6404. var promise = Promise.resolve(config);
  6405. this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
  6406. chain.unshift(interceptor.fulfilled, interceptor.rejected);
  6407. });
  6408. this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
  6409. chain.push(interceptor.fulfilled, interceptor.rejected);
  6410. });
  6411. while (chain.length) {
  6412. promise = promise.then(chain.shift(), chain.shift());
  6413. }
  6414. return promise;
  6415. };
  6416. Axios.prototype.getUri = function getUri(config) {
  6417. config = mergeConfig(this.defaults, config);
  6418. return buildURL(config.url, config.params, config.paramsSerializer).replace(/^\?/, '');
  6419. };
  6420. // Provide aliases for supported request methods
  6421. utils.forEach(['delete', 'get', 'head', 'options'], function forEachMethodNoData(method) {
  6422. /*eslint func-names:0*/
  6423. Axios.prototype[method] = function(url, config) {
  6424. return this.request(mergeConfig(config || {}, {
  6425. method: method,
  6426. url: url,
  6427. data: (config || {}).data
  6428. }));
  6429. };
  6430. });
  6431. utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
  6432. /*eslint func-names:0*/
  6433. Axios.prototype[method] = function(url, data, config) {
  6434. return this.request(mergeConfig(config || {}, {
  6435. method: method,
  6436. url: url,
  6437. data: data
  6438. }));
  6439. };
  6440. });
  6441. var Axios_1 = Axios;
  6442. /**
  6443. * A `Cancel` is an object that is thrown when an operation is canceled.
  6444. *
  6445. * @class
  6446. * @param {string=} message The message.
  6447. */
  6448. function Cancel(message) {
  6449. this.message = message;
  6450. }
  6451. Cancel.prototype.toString = function toString() {
  6452. return 'Cancel' + (this.message ? ': ' + this.message : '');
  6453. };
  6454. Cancel.prototype.__CANCEL__ = true;
  6455. var Cancel_1 = Cancel;
  6456. /**
  6457. * A `CancelToken` is an object that can be used to request cancellation of an operation.
  6458. *
  6459. * @class
  6460. * @param {Function} executor The executor function.
  6461. */
  6462. function CancelToken(executor) {
  6463. if (typeof executor !== 'function') {
  6464. throw new TypeError('executor must be a function.');
  6465. }
  6466. var resolvePromise;
  6467. this.promise = new Promise(function promiseExecutor(resolve) {
  6468. resolvePromise = resolve;
  6469. });
  6470. var token = this;
  6471. executor(function cancel(message) {
  6472. if (token.reason) {
  6473. // Cancellation has already been requested
  6474. return;
  6475. }
  6476. token.reason = new Cancel_1(message);
  6477. resolvePromise(token.reason);
  6478. });
  6479. }
  6480. /**
  6481. * Throws a `Cancel` if cancellation has been requested.
  6482. */
  6483. CancelToken.prototype.throwIfRequested = function throwIfRequested() {
  6484. if (this.reason) {
  6485. throw this.reason;
  6486. }
  6487. };
  6488. /**
  6489. * Returns an object that contains a new `CancelToken` and a function that, when called,
  6490. * cancels the `CancelToken`.
  6491. */
  6492. CancelToken.source = function source() {
  6493. var cancel;
  6494. var token = new CancelToken(function executor(c) {
  6495. cancel = c;
  6496. });
  6497. return {
  6498. token: token,
  6499. cancel: cancel
  6500. };
  6501. };
  6502. var CancelToken_1 = CancelToken;
  6503. /**
  6504. * Syntactic sugar for invoking a function and expanding an array for arguments.
  6505. *
  6506. * Common use case would be to use `Function.prototype.apply`.
  6507. *
  6508. * ```js
  6509. * function f(x, y, z) {}
  6510. * var args = [1, 2, 3];
  6511. * f.apply(null, args);
  6512. * ```
  6513. *
  6514. * With `spread` this example can be re-written.
  6515. *
  6516. * ```js
  6517. * spread(function(x, y, z) {})([1, 2, 3]);
  6518. * ```
  6519. *
  6520. * @param {Function} callback
  6521. * @returns {Function}
  6522. */
  6523. var spread = function spread(callback) {
  6524. return function wrap(arr) {
  6525. return callback.apply(null, arr);
  6526. };
  6527. };
  6528. /**
  6529. * Determines whether the payload is an error thrown by Axios
  6530. *
  6531. * @param {*} payload The value to test
  6532. * @returns {boolean} True if the payload is an error thrown by Axios, otherwise false
  6533. */
  6534. var isAxiosError = function isAxiosError(payload) {
  6535. return (typeof payload === 'object') && (payload.isAxiosError === true);
  6536. };
  6537. /**
  6538. * Create an instance of Axios
  6539. *
  6540. * @param {Object} defaultConfig The default config for the instance
  6541. * @return {Axios} A new instance of Axios
  6542. */
  6543. function createInstance(defaultConfig) {
  6544. var context = new Axios_1(defaultConfig);
  6545. var instance = bind(Axios_1.prototype.request, context);
  6546. // Copy axios.prototype to instance
  6547. utils.extend(instance, Axios_1.prototype, context);
  6548. // Copy context to instance
  6549. utils.extend(instance, context);
  6550. return instance;
  6551. }
  6552. // Create the default instance to be exported
  6553. var axios$1 = createInstance(defaults_1);
  6554. // Expose Axios class to allow class inheritance
  6555. axios$1.Axios = Axios_1;
  6556. // Factory for creating new instances
  6557. axios$1.create = function create(instanceConfig) {
  6558. return createInstance(mergeConfig(axios$1.defaults, instanceConfig));
  6559. };
  6560. // Expose Cancel & CancelToken
  6561. axios$1.Cancel = Cancel_1;
  6562. axios$1.CancelToken = CancelToken_1;
  6563. axios$1.isCancel = isCancel;
  6564. // Expose all/spread
  6565. axios$1.all = function all(promises) {
  6566. return Promise.all(promises);
  6567. };
  6568. axios$1.spread = spread;
  6569. // Expose isAxiosError
  6570. axios$1.isAxiosError = isAxiosError;
  6571. var axios_1 = axios$1;
  6572. // Allow use of default import syntax in TypeScript
  6573. var _default = axios$1;
  6574. axios_1.default = _default;
  6575. var axios = axios_1;
  6576. class RTCEndpoint extends Event$1 {
  6577. constructor(options) {
  6578. super('RTCPusherPlayer');
  6579. this.TAG = '[RTCPusherPlayer]';
  6580. let defaults = {
  6581. element: '',
  6582. // html video element
  6583. debug: false,
  6584. // if output debug log
  6585. zlmsdpUrl: '',
  6586. simulecast: false,
  6587. useCamera: true,
  6588. audioEnable: true,
  6589. videoEnable: true,
  6590. recvOnly: false
  6591. };
  6592. this.options = Object.assign({}, defaults, options);
  6593. if (this.options.debug) {
  6594. setLogger();
  6595. }
  6596. this.e = {
  6597. onicecandidate: this._onIceCandidate.bind(this),
  6598. ontrack: this._onTrack.bind(this),
  6599. onicecandidateerror: this._onIceCandidateError.bind(this)
  6600. };
  6601. this._remoteStream = null;
  6602. this._localStream = null;
  6603. this.pc = new RTCPeerConnection(null);
  6604. this.pc.onicecandidate = this.e.onicecandidate;
  6605. this.pc.onicecandidateerror = this.e.onicecandidateerror;
  6606. this.pc.ontrack = this.e.ontrack;
  6607. if (!this.options.recvOnly && (this.options.audioEnable || this.options.videoEnable)) this.start();else this.receive();
  6608. }
  6609. receive() {
  6610. const AudioTransceiverInit = {
  6611. direction: 'recvonly',
  6612. sendEncodings: []
  6613. };
  6614. const VideoTransceiverInit = {
  6615. direction: 'recvonly',
  6616. sendEncodings: []
  6617. };
  6618. this.pc.addTransceiver('audio', AudioTransceiverInit);
  6619. this.pc.addTransceiver('video', VideoTransceiverInit);
  6620. this.pc.createOffer().then(desc => {
  6621. log(this.TAG, 'offer:', desc.sdp);
  6622. this.pc.setLocalDescription(desc).then(() => {
  6623. axios({
  6624. method: 'post',
  6625. url: this.options.zlmsdpUrl,
  6626. responseType: 'json',
  6627. data: desc.sdp,
  6628. headers: {
  6629. 'Content-Type': 'text/plain;charset=utf-8'
  6630. }
  6631. }).then(response => {
  6632. let ret = response.data; //JSON.parse(response.data);
  6633. if (ret.code != 0) {
  6634. // mean failed for offer/anwser exchange
  6635. this.dispatch(Events$1.WEBRTC_OFFER_ANWSER_EXCHANGE_FAILED, ret);
  6636. return;
  6637. }
  6638. let anwser = {};
  6639. anwser.sdp = ret.sdp;
  6640. anwser.type = 'answer';
  6641. log(this.TAG, 'answer:', ret.sdp);
  6642. this.pc.setRemoteDescription(anwser).then(() => {
  6643. log(this.TAG, 'set remote success');
  6644. }).catch(e => {
  6645. error(this.TAG, e);
  6646. });
  6647. });
  6648. });
  6649. }).catch(e => {
  6650. error(this.TAG, e);
  6651. });
  6652. }
  6653. start() {
  6654. let videoConstraints = false;
  6655. let audioConstraints = false;
  6656. if (this.options.useCamera) {
  6657. if (this.options.videoEnable) videoConstraints = new VideoTrackConstraints(VideoSourceInfo.CAMERA);
  6658. if (this.options.audioEnable) audioConstraints = new AudioTrackConstraints(AudioSourceInfo.MIC);
  6659. } else {
  6660. if (this.options.videoEnable) {
  6661. videoConstraints = new VideoTrackConstraints(VideoSourceInfo.SCREENCAST);
  6662. if (this.options.audioEnable) audioConstraints = new AudioTrackConstraints(AudioSourceInfo.SCREENCAST);
  6663. } else {
  6664. if (this.options.audioEnable) audioConstraints = new AudioTrackConstraints(AudioSourceInfo.MIC);else {
  6665. // error shared display media not only audio
  6666. error(this.TAG, 'error paramter');
  6667. }
  6668. }
  6669. }
  6670. MediaStreamFactory.createMediaStream(new StreamConstraints(audioConstraints, videoConstraints)).then(stream => {
  6671. this._localStream = stream;
  6672. this.dispatch(Events$1.WEBRTC_ON_LOCAL_STREAM, stream);
  6673. const AudioTransceiverInit = {
  6674. direction: 'sendrecv',
  6675. sendEncodings: []
  6676. };
  6677. const VideoTransceiverInit = {
  6678. direction: 'sendrecv',
  6679. sendEncodings: []
  6680. };
  6681. if (this.options.simulecast && stream.getVideoTracks().length > 0) {
  6682. VideoTransceiverInit.sendEncodings = [{
  6683. rid: 'q',
  6684. active: true,
  6685. scaleResolutionDownBy: 4.0
  6686. }, {
  6687. rid: 'h',
  6688. active: true,
  6689. scaleResolutionDownBy: 2.0
  6690. }, {
  6691. rid: 'f',
  6692. active: true
  6693. }];
  6694. }
  6695. if (stream.getAudioTracks().length > 0) {
  6696. this.pc.addTransceiver(stream.getAudioTracks()[0], AudioTransceiverInit);
  6697. } else {
  6698. AudioTransceiverInit.direction = 'recvonly';
  6699. this.pc.addTransceiver('audio', AudioTransceiverInit);
  6700. }
  6701. if (stream.getVideoTracks().length > 0) {
  6702. this.pc.addTransceiver(stream.getVideoTracks()[0], VideoTransceiverInit);
  6703. } else {
  6704. VideoTransceiverInit.direction = 'recvonly';
  6705. this.pc.addTransceiver('video', VideoTransceiverInit);
  6706. }
  6707. /*
  6708. stream.getTracks().forEach((track,idx)=>{
  6709. debug.log(this.TAG,track);
  6710. this.pc.addTrack(track);
  6711. });
  6712. */
  6713. this.pc.createOffer().then(desc => {
  6714. log(this.TAG, 'offer:', desc.sdp);
  6715. this.pc.setLocalDescription(desc).then(() => {
  6716. axios({
  6717. method: 'post',
  6718. url: this.options.zlmsdpUrl,
  6719. responseType: 'json',
  6720. data: desc.sdp,
  6721. headers: {
  6722. 'Content-Type': 'text/plain;charset=utf-8'
  6723. }
  6724. }).then(response => {
  6725. let ret = response.data; //JSON.parse(response.data);
  6726. if (ret.code != 0) {
  6727. // mean failed for offer/anwser exchange
  6728. this.dispatch(Events$1.WEBRTC_OFFER_ANWSER_EXCHANGE_FAILED, ret);
  6729. return;
  6730. }
  6731. let anwser = {};
  6732. anwser.sdp = ret.sdp;
  6733. anwser.type = 'answer';
  6734. log(this.TAG, 'answer:', ret.sdp);
  6735. this.pc.setRemoteDescription(anwser).then(() => {
  6736. log(this.TAG, 'set remote success');
  6737. }).catch(e => {
  6738. error(this.TAG, e);
  6739. });
  6740. });
  6741. });
  6742. }).catch(e => {
  6743. error(this.TAG, e);
  6744. });
  6745. }).catch(e => {
  6746. error(this.TAG, e);
  6747. }); //const offerOptions = {};
  6748. /*
  6749. if (typeof this.pc.addTransceiver === 'function') {
  6750. // |direction| seems not working on Safari.
  6751. this.pc.addTransceiver('audio', { direction: 'recvonly' });
  6752. this.pc.addTransceiver('video', { direction: 'recvonly' });
  6753. } else {
  6754. offerOptions.offerToReceiveAudio = true;
  6755. offerOptions.offerToReceiveVideo = true;
  6756. }
  6757. */
  6758. }
  6759. _onIceCandidate(event) {
  6760. if (event.candidate) {
  6761. log('Remote ICE candidate: \n ' + event.candidate.candidate); // Send the candidate to the remote peer
  6762. }
  6763. }
  6764. _onTrack(event) {
  6765. if (this.options.element && event.streams && event.streams.length > 0) {
  6766. this.options.element.srcObject = event.streams[0];
  6767. this._remoteStream = event.streams[0];
  6768. this.dispatch(Events$1.WEBRTC_ON_REMOTE_STREAMS, event);
  6769. } else {
  6770. error('element pararm is failed');
  6771. }
  6772. }
  6773. _onIceCandidateError(event) {
  6774. this.dispatch(Events$1.WEBRTC_ICE_CANDIDATE_ERROR, event);
  6775. }
  6776. close() {
  6777. if (this.pc) {
  6778. this.pc.close();
  6779. this.pc = null;
  6780. }
  6781. if (this.options) {
  6782. this.options = null;
  6783. }
  6784. if (this._localStream) {
  6785. this._localStream.getTracks().forEach((track, idx) => {
  6786. track.stop();
  6787. });
  6788. }
  6789. if (this._remoteStream) {
  6790. this._remoteStream.getTracks().forEach((track, idx) => {
  6791. track.stop();
  6792. });
  6793. }
  6794. }
  6795. get remoteStream() {
  6796. return this._remoteStream;
  6797. }
  6798. get localStream() {
  6799. return this._localStream;
  6800. }
  6801. }
  6802. console.log('build date:', BUILD_DATE);
  6803. console.log('version:', VERSION);
  6804. const Events = Events$1;
  6805. const Media = media;
  6806. const Endpoint = RTCEndpoint;
  6807. exports.Endpoint = Endpoint;
  6808. exports.Events = Events;
  6809. exports.Media = Media;
  6810. Object.defineProperty(exports, '__esModule', { value: true });
  6811. return exports;
  6812. }({}));
  6813. //# sourceMappingURL=ZLMRTCClient.js.map