source_buffer_stream_unittest.cc 202 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "media/filters/source_buffer_stream.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/bind.h"
  11. #include "base/callback_helpers.h"
  12. #include "base/logging.h"
  13. #include "base/numerics/safe_conversions.h"
  14. #include "base/strings/string_number_conversions.h"
  15. #include "base/strings/string_split.h"
  16. #include "base/strings/string_util.h"
  17. #include "base/test/scoped_feature_list.h"
  18. #include "base/time/time.h"
  19. #include "media/base/data_buffer.h"
  20. #include "media/base/decoder_buffer.h"
  21. #include "media/base/media_log.h"
  22. #include "media/base/media_switches.h"
  23. #include "media/base/media_util.h"
  24. #include "media/base/mock_media_log.h"
  25. #include "media/base/test_helpers.h"
  26. #include "media/base/text_track_config.h"
  27. #include "media/base/timestamp_constants.h"
  28. #include "media/base/webvtt_util.h"
  29. #include "media/filters/source_buffer_range.h"
  30. #include "testing/gtest/include/gtest/gtest.h"
  31. using ::testing::HasSubstr;
  32. using ::testing::InSequence;
  33. using ::testing::StrictMock;
  34. namespace {
  35. enum class TimeGranularity { kMicrosecond, kMillisecond };
  36. } // namespace
  37. namespace media {
  38. typedef StreamParser::BufferQueue BufferQueue;
  39. static const int kDefaultFramesPerSecond = 30;
  40. static const int kDefaultKeyframesPerSecond = 6;
  41. static const uint8_t kDataA = 0x11;
  42. static const uint8_t kDataB = 0x33;
  43. static const int kDataSize = 1;
  44. // Matchers for verifying common media log entry strings.
  45. MATCHER_P(ContainsTrackBufferExhaustionSkipLog, skip_milliseconds, "") {
  46. return CONTAINS_STRING(arg,
  47. "Media append that overlapped current playback "
  48. "position may cause time gap in playing VIDEO stream "
  49. "because the next keyframe is " +
  50. base::NumberToString(skip_milliseconds) +
  51. "ms beyond last overlapped frame. Media may "
  52. "appear temporarily frozen.");
  53. }
  54. #define EXPECT_STATUS_FOR_STREAM_OP(status_suffix, operation) \
  55. { EXPECT_EQ(SourceBufferStreamStatus::status_suffix, stream_->operation); }
  56. class SourceBufferStreamTest : public testing::Test {
  57. public:
  58. SourceBufferStreamTest(const SourceBufferStreamTest&) = delete;
  59. SourceBufferStreamTest& operator=(const SourceBufferStreamTest&) = delete;
  60. protected:
  61. SourceBufferStreamTest() {
  62. video_config_ = TestVideoConfig::Normal();
  63. SetStreamInfo(kDefaultFramesPerSecond, kDefaultKeyframesPerSecond);
  64. ResetStream<>(video_config_);
  65. }
  66. template <typename ConfigT>
  67. void ResetStream(const ConfigT& config) {
  68. stream_ = std::make_unique<SourceBufferStream>(config, &media_log_);
  69. }
  70. void SetMemoryLimit(size_t buffers_of_data) {
  71. stream_->set_memory_limit(buffers_of_data * kDataSize);
  72. }
  73. void SetStreamInfo(int frames_per_second, int keyframes_per_second) {
  74. frames_per_second_ = frames_per_second;
  75. keyframes_per_second_ = keyframes_per_second;
  76. frame_duration_ = ConvertToFrameDuration(frames_per_second);
  77. }
  78. void SetTextStream() {
  79. video_config_ = TestVideoConfig::Invalid();
  80. TextTrackConfig config(kTextSubtitles, "", "", "");
  81. ResetStream<>(config);
  82. SetStreamInfo(2, 2);
  83. }
  84. void SetAudioStream() {
  85. video_config_ = TestVideoConfig::Invalid();
  86. audio_config_.Initialize(AudioCodec::kVorbis, kSampleFormatPlanarF32,
  87. CHANNEL_LAYOUT_STEREO, 1000, EmptyExtraData(),
  88. EncryptionScheme::kUnencrypted, base::TimeDelta(),
  89. 0);
  90. ResetStream<>(audio_config_);
  91. // Equivalent to 2ms per frame.
  92. SetStreamInfo(500, 500);
  93. }
  94. void NewCodedFrameGroupAppend(int starting_position, int number_of_buffers) {
  95. AppendBuffers(starting_position, number_of_buffers, true, base::TimeDelta(),
  96. &kDataA, kDataSize);
  97. }
  98. void NewCodedFrameGroupAppend(int starting_position,
  99. int number_of_buffers,
  100. const uint8_t* data) {
  101. AppendBuffers(starting_position, number_of_buffers, true, base::TimeDelta(),
  102. data, kDataSize);
  103. }
  104. void NewCodedFrameGroupAppend_OffsetFirstBuffer(
  105. int starting_position,
  106. int number_of_buffers,
  107. base::TimeDelta first_buffer_offset) {
  108. AppendBuffers(starting_position, number_of_buffers, true,
  109. first_buffer_offset, &kDataA, kDataSize);
  110. }
  111. void AppendBuffers(int starting_position, int number_of_buffers) {
  112. AppendBuffers(starting_position, number_of_buffers, false,
  113. base::TimeDelta(), &kDataA, kDataSize);
  114. }
  115. void AppendBuffers(int starting_position,
  116. int number_of_buffers,
  117. const uint8_t* data) {
  118. AppendBuffers(starting_position, number_of_buffers, false,
  119. base::TimeDelta(), data, kDataSize);
  120. }
  121. void NewCodedFrameGroupAppend(const std::string& buffers_to_append) {
  122. AppendBuffers(buffers_to_append, true, kNoTimestamp, false);
  123. }
  124. void NewCodedFrameGroupAppend(base::TimeDelta start_timestamp,
  125. const std::string& buffers_to_append) {
  126. AppendBuffers(buffers_to_append, true, start_timestamp, false);
  127. }
  128. void AppendBuffers(const std::string& buffers_to_append) {
  129. AppendBuffers(buffers_to_append, false, kNoTimestamp, false);
  130. }
  131. void NewCodedFrameGroupAppendOneByOne(const std::string& buffers_to_append) {
  132. AppendBuffers(buffers_to_append, true, kNoTimestamp, true);
  133. }
  134. void AppendBuffersOneByOne(const std::string& buffers_to_append) {
  135. AppendBuffers(buffers_to_append, false, kNoTimestamp, true);
  136. }
  137. void Seek(int position) { stream_->Seek(position * frame_duration_); }
  138. void SeekToTimestampMs(int64_t timestamp_ms) {
  139. stream_->Seek(base::Milliseconds(timestamp_ms));
  140. }
  141. bool GarbageCollect(base::TimeDelta media_time, int new_data_size) {
  142. return stream_->GarbageCollectIfNeeded(media_time, new_data_size);
  143. }
  144. bool GarbageCollectWithPlaybackAtBuffer(int position, int new_data_buffers) {
  145. return GarbageCollect(position * frame_duration_,
  146. new_data_buffers * kDataSize);
  147. }
  148. void RemoveInMs(int start, int end, int duration) {
  149. Remove(base::Milliseconds(start), base::Milliseconds(end),
  150. base::Milliseconds(duration));
  151. }
  152. void Remove(base::TimeDelta start, base::TimeDelta end,
  153. base::TimeDelta duration) {
  154. stream_->Remove(start, end, duration);
  155. }
  156. void SignalStartOfCodedFrameGroup(base::TimeDelta start_timestamp) {
  157. stream_->OnStartOfCodedFrameGroup(start_timestamp);
  158. }
  159. int GetRemovalRangeInMs(int start, int end, int bytes_to_free,
  160. int* removal_end) {
  161. base::TimeDelta removal_end_timestamp = base::Milliseconds(*removal_end);
  162. int bytes_removed = stream_->GetRemovalRange(
  163. base::Milliseconds(start), base::Milliseconds(end), bytes_to_free,
  164. &removal_end_timestamp);
  165. *removal_end = removal_end_timestamp.InMilliseconds();
  166. return bytes_removed;
  167. }
  168. void CheckExpectedRanges(const std::string& expected) {
  169. Ranges<base::TimeDelta> r = stream_->GetBufferedTime();
  170. std::stringstream ss;
  171. ss << "{ ";
  172. for (size_t i = 0; i < r.size(); ++i) {
  173. int64_t start = r.start(i).IntDiv(frame_duration_);
  174. int64_t end = r.end(i).IntDiv(frame_duration_) - 1;
  175. ss << "[" << start << "," << end << ") ";
  176. }
  177. ss << "}";
  178. EXPECT_EQ(expected, ss.str());
  179. }
  180. void CheckExpectedRangesByTimestamp(
  181. const std::string& expected,
  182. TimeGranularity granularity = TimeGranularity::kMillisecond) {
  183. Ranges<base::TimeDelta> r = stream_->GetBufferedTime();
  184. std::stringstream ss;
  185. ss << "{ ";
  186. for (size_t i = 0; i < r.size(); ++i) {
  187. auto conversion = (granularity == TimeGranularity::kMillisecond)
  188. ? &base::TimeDelta::InMilliseconds
  189. : &base::TimeDelta::InMicroseconds;
  190. int64_t start = (r.start(i).*conversion)();
  191. int64_t end = (r.end(i).*conversion)();
  192. ss << "[" << start << "," << end << ") ";
  193. }
  194. ss << "}";
  195. EXPECT_EQ(expected, ss.str());
  196. }
  197. void CheckExpectedRangeEndTimes(const std::string& expected) {
  198. std::stringstream ss;
  199. ss << "{ ";
  200. for (const auto& r : stream_->ranges_) {
  201. base::TimeDelta highest_pts = r->GetEndTimestamp();
  202. base::TimeDelta end_time = r->GetBufferedEndTimestamp();
  203. ss << "<" << highest_pts.InMilliseconds() << ","
  204. << end_time.InMilliseconds() << "> ";
  205. }
  206. ss << "}";
  207. EXPECT_EQ(expected, ss.str());
  208. }
  209. void CheckIsNextInPTSSequenceWithFirstRange(int64_t pts_in_ms,
  210. bool expectation) {
  211. ASSERT_GE(stream_->ranges_.size(), 1u);
  212. const auto& range_ptr = *(stream_->ranges_.begin());
  213. EXPECT_EQ(expectation, range_ptr->IsNextInPresentationSequence(
  214. base::Milliseconds(pts_in_ms)));
  215. }
  216. void CheckExpectedBuffers(
  217. int starting_position, int ending_position) {
  218. CheckExpectedBuffers(starting_position, ending_position, false, NULL, 0);
  219. }
  220. void CheckExpectedBuffers(
  221. int starting_position, int ending_position, bool expect_keyframe) {
  222. CheckExpectedBuffers(starting_position, ending_position, expect_keyframe,
  223. NULL, 0);
  224. }
  225. void CheckExpectedBuffers(int starting_position,
  226. int ending_position,
  227. const uint8_t* data) {
  228. CheckExpectedBuffers(starting_position, ending_position, false, data,
  229. kDataSize);
  230. }
  231. void CheckExpectedBuffers(int starting_position,
  232. int ending_position,
  233. const uint8_t* data,
  234. bool expect_keyframe) {
  235. CheckExpectedBuffers(starting_position, ending_position, expect_keyframe,
  236. data, kDataSize);
  237. }
  238. void CheckExpectedBuffers(int starting_position,
  239. int ending_position,
  240. bool expect_keyframe,
  241. const uint8_t* expected_data,
  242. int expected_size) {
  243. int current_position = starting_position;
  244. for (; current_position <= ending_position; current_position++) {
  245. scoped_refptr<StreamParserBuffer> buffer;
  246. SourceBufferStreamStatus status = stream_->GetNextBuffer(&buffer);
  247. EXPECT_NE(status, SourceBufferStreamStatus::kConfigChange);
  248. if (status != SourceBufferStreamStatus::kSuccess)
  249. break;
  250. if (expect_keyframe && current_position == starting_position)
  251. EXPECT_TRUE(buffer->is_key_frame());
  252. if (expected_data) {
  253. const uint8_t* actual_data = buffer->data();
  254. const int actual_size = buffer->data_size();
  255. EXPECT_EQ(expected_size, actual_size);
  256. for (int i = 0; i < std::min(actual_size, expected_size); i++) {
  257. EXPECT_EQ(expected_data[i], actual_data[i]);
  258. }
  259. }
  260. EXPECT_EQ(
  261. base::ClampFloor(buffer->GetDecodeTimestamp() / frame_duration_),
  262. current_position);
  263. }
  264. EXPECT_EQ(ending_position + 1, current_position);
  265. }
  266. void CheckExpectedBuffers(
  267. const std::string& expected,
  268. TimeGranularity granularity = TimeGranularity::kMillisecond) {
  269. std::vector<std::string> timestamps = base::SplitString(
  270. expected, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
  271. std::stringstream ss;
  272. const SourceBufferStreamType type = stream_->GetType();
  273. for (size_t i = 0; i < timestamps.size(); i++) {
  274. scoped_refptr<StreamParserBuffer> buffer;
  275. SourceBufferStreamStatus status = stream_->GetNextBuffer(&buffer);
  276. if (i > 0)
  277. ss << " ";
  278. if (status == SourceBufferStreamStatus::kConfigChange) {
  279. switch (type) {
  280. case SourceBufferStreamType::kVideo:
  281. stream_->GetCurrentVideoDecoderConfig();
  282. break;
  283. case SourceBufferStreamType::kAudio:
  284. stream_->GetCurrentAudioDecoderConfig();
  285. break;
  286. case SourceBufferStreamType::kText:
  287. stream_->GetCurrentTextTrackConfig();
  288. break;
  289. }
  290. EXPECT_EQ("C", timestamps[i]);
  291. ss << "C";
  292. continue;
  293. }
  294. EXPECT_EQ(SourceBufferStreamStatus::kSuccess, status);
  295. if (status != SourceBufferStreamStatus::kSuccess)
  296. break;
  297. if (granularity == TimeGranularity::kMillisecond)
  298. ss << buffer->timestamp().InMilliseconds();
  299. else
  300. ss << buffer->timestamp().InMicroseconds();
  301. if (buffer->GetDecodeTimestamp() !=
  302. DecodeTimestamp::FromPresentationTime(buffer->timestamp())) {
  303. if (granularity == TimeGranularity::kMillisecond)
  304. ss << "|" << buffer->GetDecodeTimestamp().InMilliseconds();
  305. else
  306. ss << "|" << buffer->GetDecodeTimestamp().InMicroseconds();
  307. }
  308. // Check duration if expected timestamp contains it.
  309. if (timestamps[i].find('D') != std::string::npos) {
  310. ss << "D" << buffer->duration().InMilliseconds();
  311. }
  312. // Check duration estimation if expected timestamp contains it.
  313. if (timestamps[i].find('E') != std::string::npos &&
  314. buffer->is_duration_estimated()) {
  315. ss << "E";
  316. }
  317. // Handle preroll buffers.
  318. if (base::EndsWith(timestamps[i], "P", base::CompareCase::SENSITIVE)) {
  319. ASSERT_TRUE(buffer->is_key_frame());
  320. scoped_refptr<StreamParserBuffer> preroll_buffer;
  321. preroll_buffer.swap(buffer);
  322. // When a preroll buffer is encountered we should be able to request one
  323. // more buffer. The first buffer should match the timestamp and config
  324. // of the second buffer, except that its discard_padding() should be its
  325. // duration.
  326. EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&buffer));
  327. ASSERT_EQ(buffer->GetConfigId(), preroll_buffer->GetConfigId());
  328. ASSERT_EQ(buffer->track_id(), preroll_buffer->track_id());
  329. ASSERT_EQ(buffer->timestamp(), preroll_buffer->timestamp());
  330. ASSERT_EQ(buffer->GetDecodeTimestamp(),
  331. preroll_buffer->GetDecodeTimestamp());
  332. ASSERT_EQ(kInfiniteDuration, preroll_buffer->discard_padding().first);
  333. ASSERT_EQ(base::TimeDelta(), preroll_buffer->discard_padding().second);
  334. ASSERT_TRUE(buffer->is_key_frame());
  335. ss << "P";
  336. } else if (buffer->is_key_frame()) {
  337. ss << "K";
  338. }
  339. }
  340. EXPECT_EQ(expected, ss.str());
  341. }
  342. void CheckNoNextBuffer() {
  343. scoped_refptr<StreamParserBuffer> buffer;
  344. EXPECT_STATUS_FOR_STREAM_OP(kNeedBuffer, GetNextBuffer(&buffer));
  345. }
  346. void CheckEOSReached() {
  347. scoped_refptr<StreamParserBuffer> buffer;
  348. EXPECT_STATUS_FOR_STREAM_OP(kEndOfStream, GetNextBuffer(&buffer));
  349. }
  350. void CheckVideoConfig(const VideoDecoderConfig& config) {
  351. const VideoDecoderConfig& actual = stream_->GetCurrentVideoDecoderConfig();
  352. EXPECT_TRUE(actual.Matches(config))
  353. << "Expected: " << config.AsHumanReadableString()
  354. << "\nActual: " << actual.AsHumanReadableString();
  355. }
  356. void CheckAudioConfig(const AudioDecoderConfig& config) {
  357. const AudioDecoderConfig& actual = stream_->GetCurrentAudioDecoderConfig();
  358. EXPECT_TRUE(actual.Matches(config))
  359. << "Expected: " << config.AsHumanReadableString()
  360. << "\nActual: " << actual.AsHumanReadableString();
  361. }
  362. base::TimeDelta frame_duration() const { return frame_duration_; }
  363. StrictMock<MockMediaLog> media_log_;
  364. std::unique_ptr<SourceBufferStream> stream_;
  365. VideoDecoderConfig video_config_;
  366. AudioDecoderConfig audio_config_;
  367. private:
  368. DemuxerStream::Type GetStreamType() {
  369. switch (stream_->GetType()) {
  370. case SourceBufferStreamType::kAudio:
  371. return DemuxerStream::AUDIO;
  372. case SourceBufferStreamType::kVideo:
  373. return DemuxerStream::VIDEO;
  374. case SourceBufferStreamType::kText:
  375. return DemuxerStream::TEXT;
  376. default:
  377. NOTREACHED();
  378. return DemuxerStream::UNKNOWN;
  379. }
  380. }
  381. base::TimeDelta ConvertToFrameDuration(int frames_per_second) {
  382. return base::Seconds(1) / frames_per_second;
  383. }
  384. void AppendBuffers(int starting_position,
  385. int number_of_buffers,
  386. bool begin_coded_frame_group,
  387. base::TimeDelta first_buffer_offset,
  388. const uint8_t* data,
  389. int size) {
  390. if (begin_coded_frame_group) {
  391. stream_->OnStartOfCodedFrameGroup(starting_position * frame_duration_);
  392. }
  393. int keyframe_interval = frames_per_second_ / keyframes_per_second_;
  394. BufferQueue queue;
  395. for (int i = 0; i < number_of_buffers; i++) {
  396. int position = starting_position + i;
  397. bool is_keyframe = position % keyframe_interval == 0;
  398. // Track ID is meaningless to these tests.
  399. scoped_refptr<StreamParserBuffer> buffer = StreamParserBuffer::CopyFrom(
  400. data, size, is_keyframe, GetStreamType(), 0);
  401. base::TimeDelta timestamp = frame_duration_ * position;
  402. if (i == 0)
  403. timestamp += first_buffer_offset;
  404. buffer->SetDecodeTimestamp(
  405. DecodeTimestamp::FromPresentationTime(timestamp));
  406. // Simulate an IBB...BBP pattern in which all B-frames reference both
  407. // the I- and P-frames. For a GOP with playback order 12345, this would
  408. // result in a decode timestamp order of 15234.
  409. base::TimeDelta presentation_timestamp;
  410. if (is_keyframe) {
  411. presentation_timestamp = timestamp;
  412. } else if ((position - 1) % keyframe_interval == 0) {
  413. // This is the P-frame (first frame following the I-frame)
  414. presentation_timestamp =
  415. (timestamp + frame_duration_ * (keyframe_interval - 2));
  416. } else {
  417. presentation_timestamp = timestamp - frame_duration_;
  418. }
  419. buffer->set_timestamp(presentation_timestamp);
  420. buffer->set_duration(frame_duration_);
  421. queue.push_back(buffer);
  422. }
  423. if (!queue.empty())
  424. stream_->Append(queue);
  425. }
  426. void UpdateLastBufferDuration(DecodeTimestamp current_dts,
  427. BufferQueue* buffers) {
  428. if (buffers->empty() || buffers->back()->duration().is_positive())
  429. return;
  430. DecodeTimestamp last_dts = buffers->back()->GetDecodeTimestamp();
  431. DCHECK(current_dts >= last_dts);
  432. buffers->back()->set_duration(current_dts - last_dts);
  433. }
  434. // StringToBufferQueue() allows for the generation of StreamParserBuffers from
  435. // coded strings of timestamps separated by spaces.
  436. //
  437. // Supported syntax (options must be in this order):
  438. // pp[u][|dd[u]][Dzz][E][P][K]
  439. //
  440. // pp:
  441. // Generates a StreamParserBuffer with decode and presentation timestamp xx.
  442. // E.g., "0 1 2 3".
  443. // pp is interpreted as milliseconds, unless suffixed with "u", in which case
  444. // pp is interpreted as microseconds.
  445. //
  446. // pp|dd:
  447. // Generates a StreamParserBuffer with presentation timestamp pp and decode
  448. // timestamp dd. E.g., "0|0 3|1 1|2 2|3". dd is interpreted as milliseconds,
  449. // unless suffixed with "u", in which case dd is interpreted as microseconds.
  450. //
  451. // Dzz[u]
  452. // Explicitly describe the duration of the buffer. zz specifies the duration
  453. // in milliseconds (or in microseconds if suffixed with "u"). If the duration
  454. // isn't specified with this syntax, the duration is derived using the
  455. // timestamp delta between this buffer and the next buffer. If not specified,
  456. // the final buffer will simply copy the duration of the previous buffer. If
  457. // the queue only contains 1 buffer then the duration must be explicitly
  458. // specified with this format.
  459. // E.g. "0D10 10D20"
  460. //
  461. // E:
  462. // Indicates that the buffer should be marked as containing an *estimated*
  463. // duration. E.g., "0D20E 20 25E 30"
  464. //
  465. // P:
  466. // Indicates the buffer with will also have a preroll buffer
  467. // associated with it. The preroll buffer will just be dummy data.
  468. // E.g. "0P 5 10"
  469. //
  470. // K:
  471. // Indicates the buffer is a keyframe. E.g., "0K 1|2K 2|4D2K 6 8".
  472. BufferQueue StringToBufferQueue(const std::string& buffers_to_append) {
  473. std::vector<std::string> timestamps = base::SplitString(
  474. buffers_to_append, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
  475. CHECK_GT(timestamps.size(), 0u);
  476. BufferQueue buffers;
  477. for (size_t i = 0; i < timestamps.size(); i++) {
  478. bool is_keyframe = false;
  479. bool has_preroll = false;
  480. bool is_duration_estimated = false;
  481. if (base::EndsWith(timestamps[i], "K", base::CompareCase::SENSITIVE)) {
  482. is_keyframe = true;
  483. // Remove the "K" off of the token.
  484. timestamps[i] = timestamps[i].substr(0, timestamps[i].length() - 1);
  485. }
  486. // Handle preroll buffers.
  487. if (base::EndsWith(timestamps[i], "P", base::CompareCase::SENSITIVE)) {
  488. is_keyframe = true;
  489. has_preroll = true;
  490. // Remove the "P" off of the token.
  491. timestamps[i] = timestamps[i].substr(0, timestamps[i].length() - 1);
  492. }
  493. if (base::EndsWith(timestamps[i], "E", base::CompareCase::SENSITIVE)) {
  494. is_duration_estimated = true;
  495. // Remove the "E" off of the token.
  496. timestamps[i] = timestamps[i].substr(0, timestamps[i].length() - 1);
  497. }
  498. int duration_in_us = -1;
  499. size_t duration_pos = timestamps[i].find('D');
  500. if (duration_pos != std::string::npos) {
  501. bool is_duration_us = false; // Default to millisecond interpretation.
  502. if (base::EndsWith(timestamps[i], "u", base::CompareCase::SENSITIVE)) {
  503. is_duration_us = true;
  504. timestamps[i] = timestamps[i].substr(0, timestamps[i].length() - 1);
  505. }
  506. CHECK(base::StringToInt(timestamps[i].substr(duration_pos + 1),
  507. &duration_in_us));
  508. if (!is_duration_us)
  509. duration_in_us *= base::Time::kMicrosecondsPerMillisecond;
  510. timestamps[i] = timestamps[i].substr(0, duration_pos);
  511. }
  512. std::vector<std::string> buffer_timestamp_strings = base::SplitString(
  513. timestamps[i], "|", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
  514. if (buffer_timestamp_strings.size() == 1)
  515. buffer_timestamp_strings.push_back(buffer_timestamp_strings[0]);
  516. CHECK_EQ(2u, buffer_timestamp_strings.size());
  517. std::vector<base::TimeDelta> buffer_timestamps;
  518. // Parse PTS, then DTS into TimeDeltas.
  519. for (size_t j = 0; j < 2; ++j) {
  520. int us = 0;
  521. bool is_us = false; // Default to millisecond interpretation.
  522. if (base::EndsWith(buffer_timestamp_strings[j], "u",
  523. base::CompareCase::SENSITIVE)) {
  524. is_us = true;
  525. buffer_timestamp_strings[j] = buffer_timestamp_strings[j].substr(
  526. 0, buffer_timestamp_strings[j].length() - 1);
  527. }
  528. CHECK(base::StringToInt(buffer_timestamp_strings[j], &us));
  529. if (!is_us)
  530. us *= base::Time::kMicrosecondsPerMillisecond;
  531. buffer_timestamps.push_back(base::Microseconds(us));
  532. }
  533. // Create buffer. Track ID is meaningless to these tests
  534. scoped_refptr<StreamParserBuffer> buffer = StreamParserBuffer::CopyFrom(
  535. &kDataA, kDataSize, is_keyframe, GetStreamType(), 0);
  536. buffer->set_timestamp(buffer_timestamps[0]);
  537. if (is_duration_estimated)
  538. buffer->set_is_duration_estimated(true);
  539. if (buffer_timestamps[1] != buffer_timestamps[0]) {
  540. buffer->SetDecodeTimestamp(
  541. DecodeTimestamp::FromPresentationTime(buffer_timestamps[1]));
  542. }
  543. if (duration_in_us >= 0)
  544. buffer->set_duration(base::Microseconds(duration_in_us));
  545. // Simulate preroll buffers by just generating another buffer and sticking
  546. // it as the preroll.
  547. if (has_preroll) {
  548. scoped_refptr<StreamParserBuffer> preroll_buffer =
  549. StreamParserBuffer::CopyFrom(&kDataA, kDataSize, is_keyframe,
  550. GetStreamType(), 0);
  551. preroll_buffer->set_duration(frame_duration_);
  552. buffer->SetPrerollBuffer(preroll_buffer);
  553. }
  554. UpdateLastBufferDuration(buffer->GetDecodeTimestamp(), &buffers);
  555. buffers.push_back(buffer);
  556. }
  557. // If the last buffer doesn't have a duration, assume it is the
  558. // same as the second to last buffer.
  559. if (buffers.size() >= 2 && buffers.back()->duration() == kNoTimestamp) {
  560. buffers.back()->set_duration(
  561. buffers[buffers.size() - 2]->duration());
  562. }
  563. return buffers;
  564. }
  565. void AppendBuffers(const std::string& buffers_to_append,
  566. bool start_new_coded_frame_group,
  567. base::TimeDelta coded_frame_group_start_timestamp,
  568. bool one_by_one) {
  569. BufferQueue buffers = StringToBufferQueue(buffers_to_append);
  570. if (start_new_coded_frame_group) {
  571. base::TimeDelta start_timestamp = coded_frame_group_start_timestamp;
  572. base::TimeDelta buffers_start_timestamp = buffers[0]->timestamp();
  573. if (start_timestamp == kNoTimestamp)
  574. start_timestamp = buffers_start_timestamp;
  575. else
  576. ASSERT_TRUE(start_timestamp <= buffers_start_timestamp);
  577. stream_->OnStartOfCodedFrameGroup(start_timestamp);
  578. }
  579. if (!one_by_one) {
  580. stream_->Append(buffers);
  581. return;
  582. }
  583. // Append each buffer one by one.
  584. for (size_t i = 0; i < buffers.size(); i++) {
  585. BufferQueue wrapper;
  586. wrapper.push_back(buffers[i]);
  587. stream_->Append(wrapper);
  588. }
  589. }
  590. int frames_per_second_;
  591. int keyframes_per_second_;
  592. base::TimeDelta frame_duration_;
  593. };
  594. TEST_F(SourceBufferStreamTest, Append_SingleRange) {
  595. // Append 15 buffers at positions 0 through 14.
  596. NewCodedFrameGroupAppend(0, 15);
  597. // Check expected range.
  598. CheckExpectedRanges("{ [0,14) }");
  599. // Check buffers in range.
  600. Seek(0);
  601. CheckExpectedBuffers(0, 14);
  602. }
  603. TEST_F(SourceBufferStreamTest, Append_SingleRange_OneBufferAtATime) {
  604. // Append 15 buffers starting at position 0, one buffer at a time.
  605. NewCodedFrameGroupAppend(0, 1);
  606. for (int i = 1; i < 15; i++)
  607. AppendBuffers(i, 1);
  608. // Check expected range.
  609. CheckExpectedRanges("{ [0,14) }");
  610. // Check buffers in range.
  611. Seek(0);
  612. CheckExpectedBuffers(0, 14);
  613. }
  614. TEST_F(SourceBufferStreamTest, Append_DisjointRanges) {
  615. // Append 5 buffers at positions 0 through 4.
  616. NewCodedFrameGroupAppend(0, 5);
  617. // Append 10 buffers at positions 15 through 24.
  618. NewCodedFrameGroupAppend(15, 10);
  619. // Check expected ranges.
  620. CheckExpectedRanges("{ [0,4) [15,24) }");
  621. // Check buffers in ranges.
  622. Seek(0);
  623. CheckExpectedBuffers(0, 4);
  624. Seek(15);
  625. CheckExpectedBuffers(15, 24);
  626. }
  627. TEST_F(SourceBufferStreamTest, Append_AdjacentRanges) {
  628. // Append 10 buffers at positions 0 through 9.
  629. NewCodedFrameGroupAppend(0, 10);
  630. // Append 11 buffers at positions 15 through 25.
  631. NewCodedFrameGroupAppend(15, 11);
  632. // Append 5 buffers at positions 10 through 14 to bridge the gap.
  633. NewCodedFrameGroupAppend(10, 5);
  634. // Check expected range.
  635. CheckExpectedRanges("{ [0,25) }");
  636. // Check buffers in range.
  637. Seek(0);
  638. CheckExpectedBuffers(0, 25);
  639. }
  640. TEST_F(SourceBufferStreamTest, Complete_Overlap) {
  641. // Append 5 buffers at positions 5 through 9.
  642. NewCodedFrameGroupAppend(5, 5);
  643. // Append 15 buffers at positions 0 through 14.
  644. NewCodedFrameGroupAppend(0, 15);
  645. // Check expected range.
  646. CheckExpectedRanges("{ [0,14) }");
  647. // Check buffers in range.
  648. Seek(0);
  649. CheckExpectedBuffers(0, 14);
  650. }
  651. TEST_F(SourceBufferStreamTest,
  652. Complete_Overlap_AfterGroupTimestampAndBeforeFirstBufferTimestamp) {
  653. // Append a coded frame group with a start timestamp of 0, but the first
  654. // buffer starts at 30ms. This can happen in muxed content where the
  655. // audio starts before the first frame.
  656. NewCodedFrameGroupAppend(base::Milliseconds(0), "30K 60K 90K 120K");
  657. CheckExpectedRangesByTimestamp("{ [0,150) }");
  658. // Completely overlap the old buffers, with a coded frame group that starts
  659. // after the old coded frame group start timestamp, but before the timestamp
  660. // of the first buffer in the coded frame group.
  661. NewCodedFrameGroupAppend("20K 50K 80K 110D10K");
  662. // Verify that the buffered ranges are updated properly and we don't crash.
  663. CheckExpectedRangesByTimestamp("{ [0,150) }");
  664. SeekToTimestampMs(0);
  665. CheckExpectedBuffers("20K 50K 80K 110K 120K");
  666. }
  667. TEST_F(SourceBufferStreamTest, Complete_Overlap_EdgeCase) {
  668. // Make each frame a keyframe so that it's okay to overlap frames at any point
  669. // (instead of needing to respect keyframe boundaries).
  670. SetStreamInfo(30, 30);
  671. // Append 6 buffers at positions 6 through 11.
  672. NewCodedFrameGroupAppend(6, 6);
  673. // Append 8 buffers at positions 5 through 12.
  674. NewCodedFrameGroupAppend(5, 8);
  675. // Check expected range.
  676. CheckExpectedRanges("{ [5,12) }");
  677. // Check buffers in range.
  678. Seek(5);
  679. CheckExpectedBuffers(5, 12);
  680. }
  681. TEST_F(SourceBufferStreamTest, Start_Overlap) {
  682. // Append 10 buffers at positions 5 through 14.
  683. NewCodedFrameGroupAppend(5, 5);
  684. // Append 6 buffers at positions 10 through 15.
  685. NewCodedFrameGroupAppend(10, 6);
  686. // Check expected range.
  687. CheckExpectedRanges("{ [5,15) }");
  688. // Check buffers in range.
  689. Seek(5);
  690. CheckExpectedBuffers(5, 15);
  691. }
  692. TEST_F(SourceBufferStreamTest, End_Overlap) {
  693. // Append 10 buffers at positions 10 through 19.
  694. NewCodedFrameGroupAppend(10, 10);
  695. // Append 10 buffers at positions 5 through 14.
  696. NewCodedFrameGroupAppend(5, 10);
  697. // Check expected range.
  698. CheckExpectedRanges("{ [5,19) }");
  699. // Check buffers in range.
  700. Seek(5);
  701. CheckExpectedBuffers(5, 19);
  702. }
  703. // Using position based test API:
  704. // DTS : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
  705. // PTS : 0 4 1 2 3 5 9 6 7 8 10 14 11 12 13 15 19 16 17 18 20
  706. // old  : A a a a a A a a a a
  707. // new : B b b b b B b b
  708. // after: B b b b b B b b A a a a a
  709. TEST_F(SourceBufferStreamTest, End_Overlap_Several) {
  710. // Append 10 buffers at positions 10 through 19 (DTS and PTS).
  711. NewCodedFrameGroupAppend(10, 10, &kDataA);
  712. // Append 8 buffers at positions 5 through 12 (DTS); 5 through 14 (PTS) with
  713. // partial second GOP.
  714. NewCodedFrameGroupAppend(5, 8, &kDataB);
  715. // Check expected ranges: stream should not have kept buffers at DTS 13,14;
  716. // PTS 12,13 because the keyframe on which they depended (10, PTS=DTS) was
  717. // overwritten. Note that partial second GOP of B includes PTS [10,14), DTS
  718. // [10,12). These are continuous with the overlapped original range's next GOP
  719. // at (15, PTS=DTS).
  720. // Unlike the rest of the position based test API used in this case, these
  721. // range expectation strings are the actual timestamps (divided by
  722. // frame_duration_).
  723. CheckExpectedRanges("{ [5,19) }");
  724. // Check buffers in range.
  725. Seek(5);
  726. CheckExpectedBuffers(5, 12, &kDataB);
  727. // No seek is necessary (1 continuous range).
  728. CheckExpectedBuffers(15, 19, &kDataA);
  729. CheckNoNextBuffer();
  730. }
  731. // Test an end overlap edge case where a single buffer overlaps the
  732. // beginning of a range.
  733. // old : 0K 30 60 90 120K 150
  734. // new : 0K
  735. // after: 0K 120K 150
  736. // track:
  737. TEST_F(SourceBufferStreamTest, End_Overlap_SingleBuffer) {
  738. // Seek to start of stream.
  739. SeekToTimestampMs(0);
  740. NewCodedFrameGroupAppend("0K 30 60 90 120K 150");
  741. CheckExpectedRangesByTimestamp("{ [0,180) }");
  742. NewCodedFrameGroupAppend("0D30K");
  743. CheckExpectedRangesByTimestamp("{ [0,180) }");
  744. CheckExpectedBuffers("0K 120K 150");
  745. CheckNoNextBuffer();
  746. }
  747. // Using position based test API:
  748. // DTS : 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
  749. // PTS : 0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0
  750. // old  : A a A a A a
  751. // new : B b b b b B b b b b B b b b b B b b b b B b b b b B b b b b
  752. // after == new
  753. TEST_F(SourceBufferStreamTest, Complete_Overlap_Several) {
  754. // Append 2 buffers at positions 5 through 6 (DTS); 5 through 9 (PTS) partial
  755. // GOP.
  756. NewCodedFrameGroupAppend(5, 2, &kDataA);
  757. // Append 2 buffers at positions 15 through 16 (DTS); 15 through 19 (PTS)
  758. // partial GOP.
  759. NewCodedFrameGroupAppend(15, 2, &kDataA);
  760. // Append 2 buffers at positions 25 through 26 (DTS); 25 through 29 (PTS)
  761. // partial GOP.
  762. NewCodedFrameGroupAppend(25, 2, &kDataA);
  763. // Check expected ranges. Unlike the rest of the position based test API used
  764. // in this case, these range expectation strings are the actual timestamps
  765. // (divided by frame_duration_).
  766. CheckExpectedRanges("{ [5,9) [15,19) [25,29) }");
  767. // Append buffers at positions 0 through 29 (DTS and PTS).
  768. NewCodedFrameGroupAppend(0, 30, &kDataB);
  769. // Check expected range.
  770. CheckExpectedRanges("{ [0,29) }");
  771. // Check buffers in range.
  772. Seek(0);
  773. CheckExpectedBuffers(0, 29, &kDataB);
  774. }
  775. // Using position based test API:
  776. // DTS:0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
  777. // PTS:0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9
  778. // old: A a A a A a A a
  779. // new:B b b b b B b b b b B b b b b B b b b b B b b b b B b b b b B b b b b
  780. TEST_F(SourceBufferStreamTest, Complete_Overlap_Several_Then_Merge) {
  781. // Append 2 buffers at positions 5 through 6 (DTS); 5 through 9 (PTS) partial
  782. // GOP.
  783. NewCodedFrameGroupAppend(5, 2, &kDataA);
  784. // Append 2 buffers at positions 10 through 11 (DTS); 15 through 19 (PTS)
  785. // partial GOP.
  786. NewCodedFrameGroupAppend(15, 2, &kDataA);
  787. // Append 2 buffers at positions 25 through 26 (DTS); 25 through 29 (PTS)
  788. // partial GOP.
  789. NewCodedFrameGroupAppend(25, 2, &kDataA);
  790. // Append 2 buffers at positions 35 through 36 (DTS); 35 through 39 (PTS)
  791. // partial GOP.
  792. NewCodedFrameGroupAppend(35, 2, &kDataA);
  793. // Append buffers at positions 0 through 34 (DTS and PTS).
  794. NewCodedFrameGroupAppend(0, 35, &kDataB);
  795. // Check expected ranges. Unlike the rest of the position based test API used
  796. // in this case, these range expectation strings are the actual timestamps
  797. // (divided by frame_duration_).
  798. CheckExpectedRanges("{ [0,39) }");
  799. // Check buffers in range.
  800. Seek(0);
  801. CheckExpectedBuffers(0, 34, &kDataB);
  802. CheckExpectedBuffers(35, 36, &kDataA);
  803. }
  804. TEST_F(SourceBufferStreamTest, Complete_Overlap_Selected) {
  805. // Append 10 buffers at positions 5 through 14.
  806. NewCodedFrameGroupAppend(5, 10, &kDataA);
  807. // Seek to buffer at position 5.
  808. Seek(5);
  809. // Replace old data with new data.
  810. NewCodedFrameGroupAppend(5, 10, &kDataB);
  811. // Check ranges are correct.
  812. CheckExpectedRanges("{ [5,14) }");
  813. // Check that data has been replaced with new data.
  814. CheckExpectedBuffers(5, 14, &kDataB);
  815. }
  816. // This test is testing that a client can append data to SourceBufferStream that
  817. // overlaps the range from which the client is currently grabbing buffers. We
  818. // would expect that the SourceBufferStream would return old data until it hits
  819. // the keyframe of the new data, after which it will return the new data.
  820. TEST_F(SourceBufferStreamTest, Complete_Overlap_Selected_TrackBuffer) {
  821. // Append 10 buffers at positions 5 through 14.
  822. NewCodedFrameGroupAppend(5, 10, &kDataA);
  823. // Seek to buffer at position 5 and get next buffer.
  824. Seek(5);
  825. CheckExpectedBuffers(5, 5, &kDataA);
  826. // Do a complete overlap by appending 20 buffers at positions 0 through 19.
  827. NewCodedFrameGroupAppend(0, 20, &kDataB);
  828. // Check range is correct.
  829. CheckExpectedRanges("{ [0,19) }");
  830. // Expect old data up until next keyframe in new data.
  831. CheckExpectedBuffers(6, 9, &kDataA);
  832. CheckExpectedBuffers(10, 10, &kDataB, true);
  833. // Expect rest of data to be new.
  834. CheckExpectedBuffers(11, 19, &kDataB);
  835. // Seek back to beginning; all data should be new.
  836. Seek(0);
  837. CheckExpectedBuffers(0, 19, &kDataB);
  838. // Check range continues to be correct.
  839. CheckExpectedRanges("{ [0,19) }");
  840. }
  841. TEST_F(SourceBufferStreamTest, Complete_Overlap_Selected_EdgeCase) {
  842. // Append 10 buffers at positions 5 through 14.
  843. NewCodedFrameGroupAppend(5, 10, &kDataA);
  844. // Seek to buffer at position 5 and get next buffer.
  845. Seek(5);
  846. CheckExpectedBuffers(5, 5, &kDataA);
  847. // Replace existing data with new data.
  848. NewCodedFrameGroupAppend(5, 10, &kDataB);
  849. // Check ranges are correct.
  850. CheckExpectedRanges("{ [5,14) }");
  851. // Expect old data up until next keyframe in new data.
  852. CheckExpectedBuffers(6, 9, &kDataA);
  853. CheckExpectedBuffers(10, 10, &kDataB, true);
  854. // Expect rest of data to be new.
  855. CheckExpectedBuffers(11, 14, &kDataB);
  856. // Seek back to beginning; all data should be new.
  857. Seek(5);
  858. CheckExpectedBuffers(5, 14, &kDataB);
  859. // Check range continues to be correct.
  860. CheckExpectedRanges("{ [5,14) }");
  861. }
  862. TEST_F(SourceBufferStreamTest, Complete_Overlap_Selected_Multiple) {
  863. static const uint8_t kDataC = 0x55;
  864. static const uint8_t kDataD = 0x77;
  865. // Append 5 buffers at positions 5 through 9.
  866. NewCodedFrameGroupAppend(5, 5, &kDataA);
  867. // Seek to buffer at position 5 and get next buffer.
  868. Seek(5);
  869. CheckExpectedBuffers(5, 5, &kDataA);
  870. // Replace existing data with new data.
  871. NewCodedFrameGroupAppend(5, 5, &kDataB);
  872. // Then replace it again with different data.
  873. NewCodedFrameGroupAppend(5, 5, &kDataC);
  874. // Now append 5 new buffers at positions 10 through 14.
  875. NewCodedFrameGroupAppend(10, 5, &kDataC);
  876. // Now replace all the data entirely.
  877. NewCodedFrameGroupAppend(5, 10, &kDataD);
  878. // Expect buffers 6 through 9 to be DataA, and the remaining
  879. // buffers to be kDataD.
  880. CheckExpectedBuffers(6, 9, &kDataA);
  881. CheckExpectedBuffers(10, 14, &kDataD);
  882. // At this point we cannot fulfill request.
  883. CheckNoNextBuffer();
  884. // Seek back to beginning; all data should be new.
  885. Seek(5);
  886. CheckExpectedBuffers(5, 14, &kDataD);
  887. }
  888. TEST_F(SourceBufferStreamTest, Start_Overlap_Selected) {
  889. // Append 10 buffers at positions 0 through 9.
  890. NewCodedFrameGroupAppend(0, 10, &kDataA);
  891. // Seek to position 5, then add buffers to overlap data at that position.
  892. Seek(5);
  893. NewCodedFrameGroupAppend(5, 10, &kDataB);
  894. // Check expected range.
  895. CheckExpectedRanges("{ [0,14) }");
  896. // Because we seeked to a keyframe, the next buffers should all be new data.
  897. CheckExpectedBuffers(5, 14, &kDataB);
  898. // Make sure all data is correct.
  899. Seek(0);
  900. CheckExpectedBuffers(0, 4, &kDataA);
  901. CheckExpectedBuffers(5, 14, &kDataB);
  902. }
  903. TEST_F(SourceBufferStreamTest, Start_Overlap_Selected_TrackBuffer) {
  904. // Append 15 buffers at positions 0 through 14.
  905. NewCodedFrameGroupAppend(0, 15, &kDataA);
  906. // Seek to 10 and get buffer.
  907. Seek(10);
  908. CheckExpectedBuffers(10, 10, &kDataA);
  909. // Now append 10 buffers of new data at positions 10 through 19.
  910. NewCodedFrameGroupAppend(10, 10, &kDataB);
  911. // Check expected range.
  912. CheckExpectedRanges("{ [0,19) }");
  913. // The next 4 buffers should be a from the old buffer, followed by a keyframe
  914. // from the new data.
  915. CheckExpectedBuffers(11, 14, &kDataA);
  916. CheckExpectedBuffers(15, 15, &kDataB, true);
  917. // The rest of the buffers should be new data.
  918. CheckExpectedBuffers(16, 19, &kDataB);
  919. // Now seek to the beginning; positions 0 through 9 should be the original
  920. // data, positions 10 through 19 should be the new data.
  921. Seek(0);
  922. CheckExpectedBuffers(0, 9, &kDataA);
  923. CheckExpectedBuffers(10, 19, &kDataB);
  924. // Make sure range is still correct.
  925. CheckExpectedRanges("{ [0,19) }");
  926. }
  927. TEST_F(SourceBufferStreamTest, Start_Overlap_Selected_EdgeCase) {
  928. // Append 10 buffers at positions 5 through 14.
  929. NewCodedFrameGroupAppend(5, 10, &kDataA);
  930. Seek(10);
  931. CheckExpectedBuffers(10, 10, &kDataA);
  932. // Now replace the last 5 buffers with new data.
  933. NewCodedFrameGroupAppend(10, 5, &kDataB);
  934. // The next 4 buffers should be the origial data, held in the track buffer.
  935. CheckExpectedBuffers(11, 14, &kDataA);
  936. // The next buffer is at position 15, so we should fail to fulfill the
  937. // request.
  938. CheckNoNextBuffer();
  939. // Now append data at 15 through 19 and check to make sure it's correct.
  940. NewCodedFrameGroupAppend(15, 5, &kDataB);
  941. CheckExpectedBuffers(15, 19, &kDataB);
  942. // Seek to beginning of buffered range and check buffers.
  943. Seek(5);
  944. CheckExpectedBuffers(5, 9, &kDataA);
  945. CheckExpectedBuffers(10, 19, &kDataB);
  946. // Check expected range.
  947. CheckExpectedRanges("{ [5,19) }");
  948. }
  949. // This test covers the case where new buffers end-overlap an existing, selected
  950. // range, and the next buffer is a keyframe that's being overlapped by new
  951. // buffers.
  952. // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
  953. // old  : *A*a a a a A a a a a
  954. // new  : B b b b b B b b b b
  955. // after: B b b b b*B*b b b b A a a a a
  956. TEST_F(SourceBufferStreamTest, End_Overlap_Selected) {
  957. // Append 10 buffers at positions 5 through 14.
  958. NewCodedFrameGroupAppend(5, 10, &kDataA);
  959. // Seek to position 5.
  960. Seek(5);
  961. // Now append 10 buffers at positions 0 through 9.
  962. NewCodedFrameGroupAppend(0, 10, &kDataB);
  963. // Check expected range.
  964. CheckExpectedRanges("{ [0,14) }");
  965. // Because we seeked to a keyframe, the next buffers should be new.
  966. CheckExpectedBuffers(5, 9, &kDataB);
  967. // Make sure all data is correct.
  968. Seek(0);
  969. CheckExpectedBuffers(0, 9, &kDataB);
  970. CheckExpectedBuffers(10, 14, &kDataA);
  971. }
  972. // This test covers the case where new buffers end-overlap an existing, selected
  973. // range, and the next buffer in the range is after the newly appended buffers.
  974. //
  975. // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
  976. // old  : |A a a a a A a a*a*a|
  977. // new  : B b b b b B b b b b
  978. // after: |B b b b b B b b b b A a a*a*a|
  979. TEST_F(SourceBufferStreamTest, End_Overlap_Selected_AfterEndOfNew_1) {
  980. // Append 10 buffers at positions 5 through 14.
  981. NewCodedFrameGroupAppend(5, 10, &kDataA);
  982. // Seek to position 10, then move to position 13.
  983. Seek(10);
  984. CheckExpectedBuffers(10, 12, &kDataA);
  985. // Now append 10 buffers at positions 0 through 9.
  986. NewCodedFrameGroupAppend(0, 10, &kDataB);
  987. // Check expected range.
  988. CheckExpectedRanges("{ [0,14) }");
  989. // Make sure rest of data is as expected.
  990. CheckExpectedBuffers(13, 14, &kDataA);
  991. // Make sure all data is correct.
  992. Seek(0);
  993. CheckExpectedBuffers(0, 9, &kDataB);
  994. CheckExpectedBuffers(10, 14, &kDataA);
  995. }
  996. // Using position based test API:
  997. // This test covers the case where new buffers end-overlap an existing, selected
  998. // range, and the next buffer in the range is after the newly appended buffers.
  999. //
  1000. // DTS : 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
  1001. // PTS : 0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0
  1002. // old  : A a a a a A a a*a*a
  1003. // new  : B b b b b B b b
  1004. // after: B b b b b B b b A a a*a*a
  1005. TEST_F(SourceBufferStreamTest, End_Overlap_Selected_AfterEndOfNew_2) {
  1006. // Append 10 buffers at positions 5 through 14 (DTS and PTS, 2 full GOPs)
  1007. NewCodedFrameGroupAppend(5, 10, &kDataA);
  1008. // Seek to position 10, then move to position 13.
  1009. Seek(10);
  1010. CheckExpectedBuffers(10, 12, &kDataA);
  1011. // Now append 8 buffers at positions 0 through 7 (DTS); 0 through 9 (PTS) with
  1012. // partial second GOP.
  1013. NewCodedFrameGroupAppend(0, 8, &kDataB);
  1014. // Check expected ranges: stream should not have kept buffers at DTS 8,9;
  1015. // PTS 7,8 because the keyframe on which they depended (5, PTS=DTS) was
  1016. // overwritten. Note that partial second GOP of B includes PTS [5,9), DTS
  1017. // [5,7). These are continuous with the overlapped original range's next GOP
  1018. // at (10, PTS=DTS).
  1019. // Unlike the rest of the position based test API used in this case, these
  1020. // range expectation strings are the actual timestamps (divided by
  1021. // frame_duration_).
  1022. CheckExpectedRanges("{ [0,14) }");
  1023. // Make sure rest of data is as expected.
  1024. CheckExpectedBuffers(13, 14, &kDataA);
  1025. // Make sure all data is correct.
  1026. Seek(0);
  1027. CheckExpectedBuffers(0, 7, &kDataB);
  1028. // No seek should be necessary (1 continuous range).
  1029. CheckExpectedBuffers(10, 14, &kDataA);
  1030. CheckNoNextBuffer();
  1031. }
  1032. // Using position based test API:
  1033. // This test covers the case where new buffers end-overlap an existing, selected
  1034. // range, and the next buffer in the range is after the newly appended buffers.
  1035. //
  1036. // DTS : 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
  1037. // PTS : 0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0
  1038. // old  : A a a*a*a A a a a a
  1039. // new  : B b b b b B b b
  1040. // after: B b b b b B b b A a a a a
  1041. // track: a a
  1042. TEST_F(SourceBufferStreamTest, End_Overlap_Selected_AfterEndOfNew_3) {
  1043. // Append 10 buffers at positions 5 through 14 (DTS and PTS, 2 full GOPs)
  1044. NewCodedFrameGroupAppend(5, 10, &kDataA);
  1045. // Seek to position 5, then move to position 8.
  1046. Seek(5);
  1047. CheckExpectedBuffers(5, 7, &kDataA);
  1048. // Now append 8 buffers at positions 0 through 7 (DTS); 0 through 9 (PTS) with
  1049. // partial second GOP.
  1050. NewCodedFrameGroupAppend(0, 8, &kDataB);
  1051. // Check expected ranges: stream should not have kept buffers at DTS 8,9;
  1052. // PTS 7,8 because the keyframe on which they depended (5, PTS=DTS) was
  1053. // overwritten. However, they were in the GOP being read from, so were put
  1054. // into the track buffer. Note that partial second GOP of B includes PTS
  1055. // [5,9), DTS [5,7). These are continuous with the overlapped original range's
  1056. // next GOP at (10, PTS=DTS).
  1057. // Unlike the rest of the position based test API used in this case, these
  1058. // range expectation strings are the actual timestamps (divided by
  1059. // frame_duration_).
  1060. CheckExpectedRanges("{ [0,14) }");
  1061. // Check for data in the track buffer.
  1062. CheckExpectedBuffers(8, 9, &kDataA);
  1063. // The buffer immediately after the track buffer should be a keyframe.
  1064. CheckExpectedBuffers(10, 10, &kDataA, true);
  1065. // Make sure all data is correct.
  1066. Seek(0);
  1067. CheckExpectedBuffers(0, 7, &kDataB);
  1068. // No seek should be necessary (1 continuous range).
  1069. CheckExpectedBuffers(10, 14, &kDataA);
  1070. CheckNoNextBuffer();
  1071. }
  1072. // This test covers the case where new buffers end-overlap an existing, selected
  1073. // range, and the next buffer in the range is overlapped by the new buffers.
  1074. //
  1075. // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
  1076. // old  : |A a a*a*a A a a a a|
  1077. // new  : B b b b b B b b b b
  1078. // after: |B b b b b B b b b b A a a a a|
  1079. // track: |a a|
  1080. TEST_F(SourceBufferStreamTest, End_Overlap_Selected_OverlappedByNew_1) {
  1081. // Append 10 buffers at positions 5 through 14.
  1082. NewCodedFrameGroupAppend(5, 10, &kDataA);
  1083. // Seek to position 5, then move to position 8.
  1084. Seek(5);
  1085. CheckExpectedBuffers(5, 7, &kDataA);
  1086. // Now append 10 buffers at positions 0 through 9.
  1087. NewCodedFrameGroupAppend(0, 10, &kDataB);
  1088. // Check expected range.
  1089. CheckExpectedRanges("{ [0,14) }");
  1090. // Check for data in the track buffer.
  1091. CheckExpectedBuffers(8, 9, &kDataA);
  1092. // The buffer immediately after the track buffer should be a keyframe.
  1093. CheckExpectedBuffers(10, 10, &kDataA, true);
  1094. // Make sure all data is correct.
  1095. Seek(0);
  1096. CheckExpectedBuffers(0, 9, &kDataB);
  1097. CheckExpectedBuffers(10, 14, &kDataA);
  1098. }
  1099. // Using position based test API:
  1100. // This test covers the case where new buffers end-overlap an existing, selected
  1101. // range, and the next buffer in the range is overlapped by the new buffers.
  1102. //
  1103. // DTS : 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
  1104. // PTS : 0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0
  1105. // old  : A*a*a a a A a a a a
  1106. // new  : B b b b b B b
  1107. // after: B b b b b B b A a a a a
  1108. // track: a a a a
  1109. TEST_F(SourceBufferStreamTest, End_Overlap_Selected_OverlappedByNew_2) {
  1110. // Append 10 buffers at positions 5 through 14 (PTS and DTS, 2 full GOPs).
  1111. NewCodedFrameGroupAppend(5, 10, &kDataA);
  1112. // Seek to position 5, then move to position 6.
  1113. Seek(5);
  1114. CheckExpectedBuffers(5, 5, &kDataA);
  1115. // Now append 7 buffers at positions 0 through 6 (DTS); 0 through 9 (PTS) with
  1116. // partial second GOP.
  1117. NewCodedFrameGroupAppend(0, 7, &kDataB);
  1118. // Check expected ranges: stream should not have kept buffers at DTS 7,8,9;
  1119. // PTS 6,7,8 because the keyframe on which they depended (5, PTS=DTS) was
  1120. // overwritten. However, they were in the GOP being read from, so were put
  1121. // into the track buffer. Note that partial second GOP of B includes PTS
  1122. // [5,9), DTS [5,6). These are continuous with the overlapped original range's
  1123. // next GOP at (10, PTS=DTS).
  1124. // Unlike the rest of the position based test API used in this case, these
  1125. // range expectation strings are the actual timestamps (divided by
  1126. // frame_duration_).
  1127. CheckExpectedRanges("{ [0,14) }");
  1128. // Check for data in the track buffer.
  1129. CheckExpectedBuffers(6, 9, &kDataA);
  1130. // The buffer immediately after the track buffer should be a keyframe.
  1131. CheckExpectedBuffers(10, 10, &kDataA, true);
  1132. // Make sure all data is correct.
  1133. Seek(0);
  1134. CheckExpectedBuffers(0, 6, &kDataB);
  1135. // No seek should be necessary (1 continuous range).
  1136. CheckExpectedBuffers(10, 14, &kDataA);
  1137. CheckNoNextBuffer();
  1138. }
  1139. // Using position based test API:
  1140. // This test covers the case where new buffers end-overlap an existing, selected
  1141. // range, and the next buffer in the range is overlapped by the new buffers.
  1142. // In this particular case, the next keyframe after the track buffer is in the
  1143. // range with the new buffers.
  1144. //
  1145. // DTS : 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
  1146. // PTS : 0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0
  1147. // old  : A*a*a a a A a a a a A a a a a
  1148. // new  : B b b b b B b b b b B
  1149. // after: B b b b b B b b b b B A a a a a
  1150. // track: a a a a
  1151. TEST_F(SourceBufferStreamTest, End_Overlap_Selected_OverlappedByNew_3) {
  1152. // Append 15 buffers at positions 5 through 19 (PTS and DTS, 3 full GOPs).
  1153. NewCodedFrameGroupAppend(5, 15, &kDataA);
  1154. // Seek to position 5, then move to position 6.
  1155. Seek(5);
  1156. CheckExpectedBuffers(5, 5, &kDataA);
  1157. // Now append 11 buffers at positions 0 through 10 (PTS and DTS, 2 full GOPs
  1158. // and just the keyframe of a third GOP).
  1159. NewCodedFrameGroupAppend(0, 11, &kDataB);
  1160. // Check expected ranges: stream should not have kept buffers at 11-14 (DTS
  1161. // and PTS) because the keyframe on which they depended (10, PTS=DTS) was
  1162. // overwritten. The GOP being read from was overwritten, so track buffer
  1163. // should contain DTS 6-9 (PTS 9,6,7,8). Note that the partial third GOP of B
  1164. // includes (10, PTS=DTS). This partial GOP is continuous with the overlapped
  1165. // original range's next GOP at (15, PTS=DTS).
  1166. // Unlike the rest of the position based test API used in this case, these
  1167. // range expectation strings are the actual timestamps (divided by
  1168. // frame_duration_).
  1169. CheckExpectedRanges("{ [0,19) }");
  1170. // Check for data in the track buffer.
  1171. CheckExpectedBuffers(6, 9, &kDataA);
  1172. // The buffer immediately after the track buffer should be a keyframe
  1173. // from the new data.
  1174. CheckExpectedBuffers(10, 10, &kDataB, true);
  1175. // Make sure all data is correct.
  1176. Seek(0);
  1177. CheckExpectedBuffers(0, 10, &kDataB);
  1178. // No seek should be necessary (1 continuous range).
  1179. CheckExpectedBuffers(15, 19, &kDataA);
  1180. }
  1181. // This test covers the case where new buffers end-overlap an existing, selected
  1182. // range, and there is no keyframe after the end of the new buffers.
  1183. // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
  1184. // old  : |A*a*a a a|
  1185. // new  : B b b b b B
  1186. // after: |B b b b b B|
  1187. // track: |a a a a|
  1188. TEST_F(SourceBufferStreamTest, End_Overlap_Selected_NoKeyframeAfterNew) {
  1189. // Append 5 buffers at positions 5 through 9.
  1190. NewCodedFrameGroupAppend(5, 5, &kDataA);
  1191. // Seek to position 5, then move to position 6.
  1192. Seek(5);
  1193. CheckExpectedBuffers(5, 5, &kDataA);
  1194. // Now append 6 buffers at positions 0 through 5.
  1195. NewCodedFrameGroupAppend(0, 6, &kDataB);
  1196. // Check expected range.
  1197. CheckExpectedRanges("{ [0,5) }");
  1198. // Check for data in the track buffer.
  1199. CheckExpectedBuffers(6, 9, &kDataA);
  1200. // Now there's no data to fulfill the request.
  1201. CheckNoNextBuffer();
  1202. // Let's fill in the gap, buffers 6 through 10.
  1203. AppendBuffers(6, 5, &kDataB);
  1204. // We should be able to get the next buffer.
  1205. CheckExpectedBuffers(10, 10, &kDataB);
  1206. }
  1207. // Using position based test API:
  1208. // This test covers the case where new buffers end-overlap an existing, selected
  1209. // range, and there is no keyframe after the end of the new buffers, then more
  1210. // buffers end-overlap the beginning.
  1211. // DTS : 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
  1212. // PTS : 0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0
  1213. // old  : A a a a a A*a*
  1214. // new  : B b b b b B b b b b B
  1215. // after: B b b b b B b b b b B
  1216. // new  : A a a a a A
  1217. // after: A a a a a A B b b b b B
  1218. // track: a
  1219. // new : B b b b b B
  1220. // after: A a a a a A B b b b b B b b b b B
  1221. TEST_F(SourceBufferStreamTest, End_Overlap_Selected_NoKeyframeAfterNew2) {
  1222. // Append 7 buffers at positions 10 through 16 (DTS); 10 through 19 (PTS) with
  1223. // a partial second GOP.
  1224. NewCodedFrameGroupAppend(10, 7, &kDataA);
  1225. // Seek to position 15, then move to position 16.
  1226. Seek(15);
  1227. CheckExpectedBuffers(15, 15, &kDataA);
  1228. // Now append 11 buffers at positions 5 through 15 (PTS and DTS), 2 full GOPs
  1229. // and just the keyframe of a third GOP.
  1230. NewCodedFrameGroupAppend(5, 11, &kDataB);
  1231. // Check expected ranges: stream should not have kept buffer at DTS 16, PTS 19
  1232. // because the keyframe it depended on (15, PTS=DTS) was overwritten.
  1233. // The GOP being read from was overwritten, so track buffer
  1234. // should contain DTS 16, PTS 19.
  1235. // Unlike the rest of the position based test API used in this case,
  1236. // CheckExpectedRanges() uses expectation strings containing actual timestamps
  1237. // (divided by frame_duration_).
  1238. CheckExpectedRanges("{ [5,15) }");
  1239. // Now do another end-overlap. Append one full GOP plus keyframe of 2nd. Note
  1240. // that this new keyframe at (5, PTS=DTS) is continuous with the overlapped
  1241. // range's next GOP (B) at (10, PTS=DTS).
  1242. NewCodedFrameGroupAppend(0, 6, &kDataA);
  1243. CheckExpectedRanges("{ [0,15) }");
  1244. // Check for data in the track buffer.
  1245. CheckExpectedBuffers(16, 16, &kDataA);
  1246. // Now there's no data to fulfill the request.
  1247. CheckNoNextBuffer();
  1248. // Add data to the end of the range in the position just read from the track
  1249. // buffer. The stream should not be able to fulfill the next read
  1250. // until we've added a keyframe continuous beyond this point.
  1251. NewCodedFrameGroupAppend(15, 1, &kDataB);
  1252. CheckNoNextBuffer();
  1253. for (int i = 16; i <= 19; i++) {
  1254. AppendBuffers(i, 1, &kDataB);
  1255. CheckNoNextBuffer();
  1256. }
  1257. // Now append a keyframe at PTS=DTS=20.
  1258. AppendBuffers(20, 1, &kDataB);
  1259. // The buffer at position 16 (PTS 19) in track buffer is adjacent to the next
  1260. // keyframe, so no warning should be emitted on that track buffer exhaustion.
  1261. // We should be able to get the next buffer (no longer from the track buffer).
  1262. CheckExpectedBuffers(20, 20, &kDataB, true);
  1263. CheckNoNextBuffer();
  1264. // Make sure all data is correct.
  1265. CheckExpectedRanges("{ [0,20) }");
  1266. Seek(0);
  1267. CheckExpectedBuffers(0, 5, &kDataA);
  1268. // No seek should be necessary (1 continuous range).
  1269. CheckExpectedBuffers(10, 20, &kDataB);
  1270. CheckNoNextBuffer();
  1271. }
  1272. // This test covers the case where new buffers end-overlap an existing, selected
  1273. // range, and the next keyframe in a separate range.
  1274. // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
  1275. // old  : |A*a*a a a| |A a a a a|
  1276. // new  : B b b b b B
  1277. // after: |B b b b b B| |A a a a a|
  1278. // track: |a a a a|
  1279. TEST_F(SourceBufferStreamTest, End_Overlap_Selected_NoKeyframeAfterNew3) {
  1280. // Append 5 buffers at positions 5 through 9.
  1281. NewCodedFrameGroupAppend(5, 5, &kDataA);
  1282. // Append 5 buffers at positions 15 through 19.
  1283. NewCodedFrameGroupAppend(15, 5, &kDataA);
  1284. // Check expected range.
  1285. CheckExpectedRanges("{ [5,9) [15,19) }");
  1286. // Seek to position 5, then move to position 6.
  1287. Seek(5);
  1288. CheckExpectedBuffers(5, 5, &kDataA);
  1289. // Now append 6 buffers at positions 0 through 5.
  1290. NewCodedFrameGroupAppend(0, 6, &kDataB);
  1291. // Check expected range.
  1292. CheckExpectedRanges("{ [0,5) [15,19) }");
  1293. // Check for data in the track buffer.
  1294. CheckExpectedBuffers(6, 9, &kDataA);
  1295. // Now there's no data to fulfill the request.
  1296. CheckNoNextBuffer();
  1297. // Let's fill in the gap, buffers 6 through 14.
  1298. AppendBuffers(6, 9, &kDataB);
  1299. // Check expected range.
  1300. CheckExpectedRanges("{ [0,19) }");
  1301. // We should be able to get the next buffer.
  1302. CheckExpectedBuffers(10, 14, &kDataB);
  1303. // We should be able to get the next buffer.
  1304. CheckExpectedBuffers(15, 19, &kDataA);
  1305. }
  1306. // This test covers the case when new buffers overlap the middle of a selected
  1307. // range. This tests the case when there is precise overlap of an existing GOP,
  1308. // and the next buffer is a keyframe.
  1309. // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
  1310. // old  : A a a a a*A*a a a a A a a a a
  1311. // new  : B b b b b
  1312. // after: A a a a a*B*b b b b A a a a a
  1313. TEST_F(SourceBufferStreamTest, Middle_Overlap_Selected_1) {
  1314. // Append 15 buffers at positions 0 through 14.
  1315. NewCodedFrameGroupAppend(0, 15, &kDataA);
  1316. // Seek to position 5.
  1317. Seek(5);
  1318. // Now append 5 buffers at positions 5 through 9.
  1319. NewCodedFrameGroupAppend(5, 5, &kDataB);
  1320. // Check expected range.
  1321. CheckExpectedRanges("{ [0,14) }");
  1322. // Check for next data; should be new data.
  1323. CheckExpectedBuffers(5, 9, &kDataB);
  1324. // Make sure all data is correct.
  1325. Seek(0);
  1326. CheckExpectedBuffers(0, 4, &kDataA);
  1327. CheckExpectedBuffers(5, 9, &kDataB);
  1328. CheckExpectedBuffers(10, 14, &kDataA);
  1329. CheckNoNextBuffer();
  1330. }
  1331. // This test covers the case when new buffers overlap the middle of a selected
  1332. // range. This tests the case when there is precise overlap of an existing GOP,
  1333. // and the next buffer is a non-keyframe in a GOP after the new buffers.
  1334. // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
  1335. // old  : A a a a a A a a a a A*a*a a a
  1336. // new  : B b b b b
  1337. // after: A a a a a B b b b b A*a*a a a
  1338. TEST_F(SourceBufferStreamTest, Middle_Overlap_Selected_2) {
  1339. // Append 15 buffers at positions 0 through 14.
  1340. NewCodedFrameGroupAppend(0, 15, &kDataA);
  1341. // Seek to 10 then move to position 11.
  1342. Seek(10);
  1343. CheckExpectedBuffers(10, 10, &kDataA);
  1344. // Now append 5 buffers at positions 5 through 9.
  1345. NewCodedFrameGroupAppend(5, 5, &kDataB);
  1346. // Check expected range.
  1347. CheckExpectedRanges("{ [0,14) }");
  1348. // Make sure data is correct.
  1349. CheckExpectedBuffers(11, 14, &kDataA);
  1350. CheckNoNextBuffer();
  1351. Seek(0);
  1352. CheckExpectedBuffers(0, 4, &kDataA);
  1353. CheckExpectedBuffers(5, 9, &kDataB);
  1354. CheckExpectedBuffers(10, 14, &kDataA);
  1355. CheckNoNextBuffer();
  1356. }
  1357. // This test covers the case when new buffers overlap the middle of a selected
  1358. // range. This tests the case when only a partial GOP is appended, that append
  1359. // is merged into the overlapped range, and the next buffer is before the new
  1360. // buffers.
  1361. // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
  1362. // old  : A a*a*a a A a a a a A a a a a
  1363. // new  : B
  1364. // after: A a*a*a a B A a a a a
  1365. TEST_F(SourceBufferStreamTest, Middle_Overlap_Selected_3) {
  1366. // Append 15 buffers at positions 0 through 14.
  1367. NewCodedFrameGroupAppend(0, 15, &kDataA);
  1368. // Seek to beginning then move to position 2.
  1369. Seek(0);
  1370. CheckExpectedBuffers(0, 1, &kDataA);
  1371. // Now append 1 buffer at position 5 (just the keyframe of a GOP).
  1372. NewCodedFrameGroupAppend(5, 1, &kDataB);
  1373. // Check expected range.
  1374. CheckExpectedRanges("{ [0,14) }");
  1375. // Make sure data is correct.
  1376. CheckExpectedBuffers(2, 4, &kDataA);
  1377. CheckExpectedBuffers(5, 5, &kDataB);
  1378. // No seek should be necessary (1 continuous range).
  1379. CheckExpectedBuffers(10, 14, &kDataA);
  1380. CheckNoNextBuffer();
  1381. // Seek to the beginning and recheck data in case track buffer erroneously
  1382. // became involved.
  1383. Seek(0);
  1384. CheckExpectedBuffers(0, 4, &kDataA);
  1385. CheckExpectedBuffers(5, 5, &kDataB);
  1386. CheckExpectedBuffers(10, 14, &kDataA);
  1387. CheckNoNextBuffer();
  1388. }
  1389. // This test covers the case when new buffers overlap the middle of a selected
  1390. // range. This tests the case when only a partial GOP is appended, and the next
  1391. // buffer is after the new buffers, and comes from the track buffer until the
  1392. // next GOP in the original buffers.
  1393. // index: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
  1394. // old  : A a a a a A a a*a*a A a a a a
  1395. // new  : B
  1396. // after: A a a a a B A a a a a
  1397. // track: a a
  1398. TEST_F(SourceBufferStreamTest, Middle_Overlap_Selected_4) {
  1399. // Append 15 buffers at positions 0 through 14.
  1400. NewCodedFrameGroupAppend(0, 15, &kDataA);
  1401. // Seek to 5 then move to position 8.
  1402. Seek(5);
  1403. CheckExpectedBuffers(5, 7, &kDataA);
  1404. // Now append 1 buffer at position 5.
  1405. NewCodedFrameGroupAppend(5, 1, &kDataB);
  1406. // Check expected range.
  1407. CheckExpectedRanges("{ [0,14) }");
  1408. // Buffers 8 and 9 should be in the track buffer.
  1409. CheckExpectedBuffers(8, 9, &kDataA);
  1410. // The buffer immediately after the track buffer should be a keyframe.
  1411. CheckExpectedBuffers(10, 10, &kDataA, true);
  1412. // Make sure all data is correct.
  1413. Seek(0);
  1414. CheckExpectedBuffers(0, 4, &kDataA);
  1415. CheckExpectedBuffers(5, 5, &kDataB);
  1416. // No seek should be necessary (1 continuous range).
  1417. CheckExpectedBuffers(10, 14, &kDataA);
  1418. CheckNoNextBuffer();
  1419. }
  1420. TEST_F(SourceBufferStreamTest, Overlap_OneByOne) {
  1421. // Append 5 buffers starting at 10ms, 30ms apart.
  1422. NewCodedFrameGroupAppendOneByOne("10K 40 70 100 130");
  1423. // The range ends at 160, accounting for the last buffer's duration.
  1424. CheckExpectedRangesByTimestamp("{ [10,160) }");
  1425. // Overlap with 10 buffers starting at the beginning, appended one at a
  1426. // time.
  1427. NewCodedFrameGroupAppend(0, 1, &kDataB);
  1428. for (int i = 1; i < 10; i++)
  1429. AppendBuffers(i, 1, &kDataB);
  1430. // All data should be replaced.
  1431. Seek(0);
  1432. CheckExpectedRanges("{ [0,9) }");
  1433. CheckExpectedBuffers(0, 9, &kDataB);
  1434. }
  1435. TEST_F(SourceBufferStreamTest, Overlap_OneByOne_DeleteGroup) {
  1436. NewCodedFrameGroupAppendOneByOne("10K 40 70 100 130K");
  1437. CheckExpectedRangesByTimestamp("{ [10,160) }");
  1438. // Seek to 130ms.
  1439. SeekToTimestampMs(130);
  1440. // Overlap with a new coded frame group from 0 to 130ms.
  1441. NewCodedFrameGroupAppendOneByOne("0K 120D10");
  1442. // Next buffer should still be 130ms.
  1443. CheckExpectedBuffers("130K");
  1444. // Check the final buffers is correct.
  1445. SeekToTimestampMs(0);
  1446. CheckExpectedBuffers("0K 120 130K");
  1447. }
  1448. TEST_F(SourceBufferStreamTest, Overlap_OneByOne_BetweenCodedFrameGroups) {
  1449. // Append 5 buffers starting at 110ms, 30ms apart.
  1450. NewCodedFrameGroupAppendOneByOne("110K 140 170 200 230");
  1451. CheckExpectedRangesByTimestamp("{ [110,260) }");
  1452. // Now append 2 coded frame groups from 0ms to 210ms, 30ms apart. Note that
  1453. // the
  1454. // old keyframe 110ms falls in between these two groups.
  1455. NewCodedFrameGroupAppendOneByOne("0K 30 60 90");
  1456. NewCodedFrameGroupAppendOneByOne("120K 150 180 210");
  1457. CheckExpectedRangesByTimestamp("{ [0,240) }");
  1458. // Check the final buffers is correct; the keyframe at 110ms should be
  1459. // deleted.
  1460. SeekToTimestampMs(0);
  1461. CheckExpectedBuffers("0K 30 60 90 120K 150 180 210");
  1462. }
  1463. // old : 10K 40 *70* 100K 125 130K
  1464. // new : 0K 30 60 90 120K
  1465. // after: 0K 30 60 90 *120K* 130K
  1466. TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer) {
  1467. EXPECT_MEDIA_LOG(ContainsTrackBufferExhaustionSkipLog(50));
  1468. NewCodedFrameGroupAppendOneByOne("10K 40 70 100K 125 130D30K");
  1469. CheckExpectedRangesByTimestamp("{ [10,160) }");
  1470. // Seek to 70ms.
  1471. SeekToTimestampMs(70);
  1472. CheckExpectedBuffers("10K 40");
  1473. // Overlap with a new coded frame group from 0 to 130ms.
  1474. NewCodedFrameGroupAppendOneByOne("0K 30 60 90 120D10K");
  1475. CheckExpectedRangesByTimestamp("{ [0,160) }");
  1476. // Should return frame 70ms from the track buffer, then switch
  1477. // to the new data at 120K, then switch back to the old data at 130K. The
  1478. // frame at 125ms that depended on keyframe 100ms should have been deleted.
  1479. CheckExpectedBuffers("70 120K 130K");
  1480. // Check the final result: should not include data from the track buffer.
  1481. SeekToTimestampMs(0);
  1482. CheckExpectedBuffers("0K 30 60 90 120K 130K");
  1483. }
  1484. // Overlap the next keyframe after the end of the track buffer with a new
  1485. // keyframe.
  1486. // old : 10K 40 *70* 100K 125 130K
  1487. // new : 0K 30 60 90 120K
  1488. // after: 0K 30 60 90 *120K* 130K
  1489. // track: 70
  1490. // new : 110K 130
  1491. // after: 0K 30 60 90 *110K* 130
  1492. TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer2) {
  1493. EXPECT_MEDIA_LOG(ContainsTrackBufferExhaustionSkipLog(40));
  1494. NewCodedFrameGroupAppendOneByOne("10K 40 70 100K 125 130D30K");
  1495. CheckExpectedRangesByTimestamp("{ [10,160) }");
  1496. // Seek to 70ms.
  1497. SeekToTimestampMs(70);
  1498. CheckExpectedBuffers("10K 40");
  1499. // Overlap with a new coded frame group from 0 to 120ms; 70ms and 100ms go in
  1500. // track
  1501. // buffer.
  1502. NewCodedFrameGroupAppendOneByOne("0K 30 60 90 120D10K");
  1503. CheckExpectedRangesByTimestamp("{ [0,160) }");
  1504. // Now overlap the keyframe at 120ms.
  1505. NewCodedFrameGroupAppendOneByOne("110K 130");
  1506. // Should return frame 70ms from the track buffer. Then it should
  1507. // return the keyframe after the track buffer, which is at 110ms.
  1508. CheckExpectedBuffers("70 110K 130");
  1509. }
  1510. // Overlap the next keyframe after the end of the track buffer without a
  1511. // new keyframe.
  1512. // old : 10K 40 *70* 100K 125 130K
  1513. // new : 0K 30 60 90 120K
  1514. // after: 0K 30 60 90 *120K* 130K
  1515. // track: 70
  1516. // new : 50K 80 110 140
  1517. // after: 0K 30 50K 80 110 140 * (waiting for keyframe)
  1518. // track: 70
  1519. TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer3) {
  1520. EXPECT_MEDIA_LOG(ContainsTrackBufferExhaustionSkipLog(80));
  1521. NewCodedFrameGroupAppendOneByOne("10K 40 70 100K 125 130D30K");
  1522. CheckExpectedRangesByTimestamp("{ [10,160) }");
  1523. // Seek to 70ms.
  1524. SeekToTimestampMs(70);
  1525. CheckExpectedBuffers("10K 40");
  1526. // Overlap with a new coded frame group from 0 to 120ms; 70ms goes in track
  1527. // buffer.
  1528. NewCodedFrameGroupAppendOneByOne("0K 30 60 90 120D10K");
  1529. CheckExpectedRangesByTimestamp("{ [0,160) }");
  1530. // Now overlap the keyframe at 120ms and 130ms.
  1531. NewCodedFrameGroupAppendOneByOne("50K 80 110 140");
  1532. CheckExpectedRangesByTimestamp("{ [0,170) }");
  1533. // Should have all the buffers from the track buffer, then stall.
  1534. CheckExpectedBuffers("70");
  1535. CheckNoNextBuffer();
  1536. // Appending a keyframe should fulfill the read.
  1537. AppendBuffersOneByOne("150D30K");
  1538. CheckExpectedBuffers("150K");
  1539. CheckNoNextBuffer();
  1540. }
  1541. // Overlap the next keyframe after the end of the track buffer with a keyframe
  1542. // that comes before the end of the track buffer.
  1543. // old : 10K 40 *70* 100K 125 130K
  1544. // new : 0K 30 60 90 120K
  1545. // after: 0K 30 60 90 *120K* 130K
  1546. // track: 70
  1547. // new : 80K 110 140
  1548. // after: 0K 30 60 *80K* 110 140
  1549. // track: 70
  1550. TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer4) {
  1551. NewCodedFrameGroupAppendOneByOne("10K 40 70 100K 125 130D30K");
  1552. CheckExpectedRangesByTimestamp("{ [10,160) }");
  1553. // Seek to 70ms.
  1554. SeekToTimestampMs(70);
  1555. CheckExpectedBuffers("10K 40");
  1556. // Overlap with a new coded frame group from 0 to 120ms; 70ms and 100ms go in
  1557. // track
  1558. // buffer.
  1559. NewCodedFrameGroupAppendOneByOne("0K 30 60 90 120D10K");
  1560. CheckExpectedRangesByTimestamp("{ [0,160) }");
  1561. // Now append a keyframe at 80ms.
  1562. NewCodedFrameGroupAppendOneByOne("80K 110 140");
  1563. CheckExpectedBuffers("70 80K 110 140");
  1564. CheckNoNextBuffer();
  1565. }
  1566. // Overlap the next keyframe after the end of the track buffer with a keyframe
  1567. // that comes before the end of the track buffer, when the selected stream was
  1568. // waiting for the next keyframe.
  1569. // old : 10K 40 *70* 100K
  1570. // new : 0K 30 60 90 120
  1571. // after: 0K 30 60 90 120 * (waiting for keyframe)
  1572. // track: 70
  1573. // new : 80K 110 140
  1574. // after: 0K 30 60 *80K* 110 140
  1575. // track: 70
  1576. TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer5) {
  1577. NewCodedFrameGroupAppendOneByOne("10K 40 70 100K");
  1578. CheckExpectedRangesByTimestamp("{ [10,130) }");
  1579. // Seek to 70ms.
  1580. SeekToTimestampMs(70);
  1581. CheckExpectedBuffers("10K 40");
  1582. // Overlap with a new coded frame group from 0 to 120ms; 70ms goes in track
  1583. // buffer.
  1584. NewCodedFrameGroupAppendOneByOne("0K 30 60 90 120");
  1585. CheckExpectedRangesByTimestamp("{ [0,150) }");
  1586. // Now append a keyframe at 80ms.
  1587. NewCodedFrameGroupAppendOneByOne("80K 110 140");
  1588. CheckExpectedBuffers("70 80K 110 140");
  1589. CheckNoNextBuffer();
  1590. }
  1591. // Test that appending to a different range while there is data in
  1592. // the track buffer doesn't affect the selected range or track buffer state.
  1593. // old : 10K 40 *70* 100K 125 130K ... 200K 230
  1594. // new : 0K 30 60 90 120K
  1595. // after: 0K 30 60 90 *120K* 130K ... 200K 230
  1596. // track: 70
  1597. // old : 0K 30 60 90 *120K* 130K ... 200K 230
  1598. // new : 260K 290
  1599. // after: 0K 30 60 90 *120K* 130K ... 200K 230 260K 290
  1600. // track: 70
  1601. TEST_F(SourceBufferStreamTest, Overlap_OneByOne_TrackBuffer6) {
  1602. EXPECT_MEDIA_LOG(ContainsTrackBufferExhaustionSkipLog(50));
  1603. NewCodedFrameGroupAppendOneByOne("10K 40 70 100K 125 130D30K");
  1604. NewCodedFrameGroupAppendOneByOne("200K 230");
  1605. CheckExpectedRangesByTimestamp("{ [10,160) [200,260) }");
  1606. // Seek to 70ms.
  1607. SeekToTimestampMs(70);
  1608. CheckExpectedBuffers("10K 40");
  1609. // Overlap with a new coded frame group from 0 to 120ms.
  1610. NewCodedFrameGroupAppendOneByOne("0K 30 60 90 120D10K");
  1611. CheckExpectedRangesByTimestamp("{ [0,160) [200,260) }");
  1612. // Verify that 70 gets read out of the track buffer.
  1613. CheckExpectedBuffers("70");
  1614. // Append more data to the unselected range.
  1615. NewCodedFrameGroupAppendOneByOne("260K 290");
  1616. CheckExpectedRangesByTimestamp("{ [0,160) [200,320) }");
  1617. CheckExpectedBuffers("120K 130K");
  1618. CheckNoNextBuffer();
  1619. // Check the final result: should not include data from the track buffer.
  1620. SeekToTimestampMs(0);
  1621. CheckExpectedBuffers("0K 30 60 90 120K 130K");
  1622. CheckNoNextBuffer();
  1623. }
  1624. // Test that overlap-appending with a GOP that begins with time of next track
  1625. // buffer frame drops that track buffer frame and buffers the new GOP correctly.
  1626. // append : 10K 40 70 100
  1627. // read the first two buffers
  1628. // after : 10K 40 *70* 100
  1629. //
  1630. // append : 0K 30 60 90 120
  1631. // after : 0K 30 60 90 120
  1632. // track : *70* 100
  1633. //
  1634. // read the buffer at 70ms from track
  1635. // after : 0K 30 60 90 120
  1636. // track : *100*
  1637. //
  1638. // append : 100K 130
  1639. // after : 0K 30 60 90 *100K* 130
  1640. // track : (empty)
  1641. // 100K, not 100, should be the next buffer read.
  1642. TEST_F(SourceBufferStreamTest,
  1643. Overlap_That_Prunes_All_of_Previous_TrackBuffer) {
  1644. NewCodedFrameGroupAppend("10K 40 70 100");
  1645. CheckExpectedRangesByTimestamp("{ [10,130) }");
  1646. // Seek to 70ms.
  1647. SeekToTimestampMs(70);
  1648. CheckExpectedBuffers("10K 40");
  1649. // Overlap with a new coded frame group from 0 to 120ms, leaving the original
  1650. // nonkeyframes at 70ms and 100ms in the track buffer.
  1651. NewCodedFrameGroupAppend("0K 30 60 90 120");
  1652. CheckExpectedRangesByTimestamp("{ [0,150) }");
  1653. // Verify that 70 gets read out of the track buffer, leaving the nonkeyframe
  1654. // at 100ms in the track buffer.
  1655. CheckExpectedBuffers("70");
  1656. // Overlap with a coded frame group having a keyframe at 100ms. This should
  1657. // clear the track buffer and serve that keyframe, not the original
  1658. // nonkeyframe at time 100ms on the next read call.
  1659. NewCodedFrameGroupAppend("100K 130");
  1660. CheckExpectedRangesByTimestamp("{ [0,160) }");
  1661. CheckExpectedBuffers("100K 130");
  1662. CheckNoNextBuffer();
  1663. // Check the final result: should not include data from the track buffer.
  1664. SeekToTimestampMs(0);
  1665. CheckExpectedBuffers("0K 30 60 90 100K 130");
  1666. CheckNoNextBuffer();
  1667. }
  1668. TEST_F(SourceBufferStreamTest, Seek_Keyframe) {
  1669. // Append 6 buffers at positions 0 through 5.
  1670. NewCodedFrameGroupAppend(0, 6);
  1671. // Seek to beginning.
  1672. Seek(0);
  1673. CheckExpectedBuffers(0, 5, true);
  1674. }
  1675. TEST_F(SourceBufferStreamTest, Seek_NonKeyframe) {
  1676. // Append 15 buffers at positions 0 through 14.
  1677. NewCodedFrameGroupAppend(0, 15);
  1678. // Seek to buffer at position 13.
  1679. Seek(13);
  1680. // Expect seeking back to the nearest keyframe.
  1681. CheckExpectedBuffers(10, 14, true);
  1682. // Seek to buffer at position 3.
  1683. Seek(3);
  1684. // Expect seeking back to the nearest keyframe.
  1685. CheckExpectedBuffers(0, 3, true);
  1686. }
  1687. TEST_F(SourceBufferStreamTest, Seek_NotBuffered) {
  1688. // Seek to beginning.
  1689. SeekToTimestampMs(0);
  1690. // Try to get buffer; nothing's appended.
  1691. CheckNoNextBuffer();
  1692. // Append 1 buffer at time 0, duration 10ms.
  1693. NewCodedFrameGroupAppend("0D10K");
  1694. // Confirm we can read it back.
  1695. SeekToTimestampMs(0);
  1696. CheckExpectedBuffers("0K");
  1697. // Try to get buffer out of range.
  1698. SeekToTimestampMs(10);
  1699. CheckNoNextBuffer();
  1700. }
  1701. TEST_F(SourceBufferStreamTest, Seek_InBetweenTimestamps) {
  1702. // Append 10 buffers at positions 0 through 9.
  1703. NewCodedFrameGroupAppend(0, 10);
  1704. base::TimeDelta bump = frame_duration() / 4;
  1705. CHECK(bump.is_positive());
  1706. // Seek to buffer a little after position 5.
  1707. stream_->Seek(5 * frame_duration() + bump);
  1708. CheckExpectedBuffers(5, 5, true);
  1709. // Seek to buffer a little before position 5.
  1710. stream_->Seek(5 * frame_duration() - bump);
  1711. CheckExpectedBuffers(0, 0, true);
  1712. }
  1713. // This test will do a complete overlap of an existing range in order to add
  1714. // buffers to the track buffers. Then the test does a seek to another part of
  1715. // the stream. The SourceBufferStream should clear its internal track buffer in
  1716. // response to the Seek().
  1717. TEST_F(SourceBufferStreamTest, Seek_After_TrackBuffer_Filled) {
  1718. // Append 10 buffers at positions 5 through 14.
  1719. NewCodedFrameGroupAppend(5, 10, &kDataA);
  1720. // Seek to buffer at position 5 and get next buffer.
  1721. Seek(5);
  1722. CheckExpectedBuffers(5, 5, &kDataA);
  1723. // Do a complete overlap by appending 20 buffers at positions 0 through 19.
  1724. NewCodedFrameGroupAppend(0, 20, &kDataB);
  1725. // Check range is correct.
  1726. CheckExpectedRanges("{ [0,19) }");
  1727. // Seek to beginning; all data should be new.
  1728. Seek(0);
  1729. CheckExpectedBuffers(0, 19, &kDataB);
  1730. // Check range continues to be correct.
  1731. CheckExpectedRanges("{ [0,19) }");
  1732. }
  1733. TEST_F(SourceBufferStreamTest, Seek_StartOfGroup) {
  1734. base::TimeDelta bump = frame_duration() / 4;
  1735. CHECK(bump.is_positive());
  1736. // Append 5 buffers at position (5 + |bump|) through 9, where the coded frame
  1737. // group begins at position 5.
  1738. Seek(5);
  1739. NewCodedFrameGroupAppend_OffsetFirstBuffer(5, 5, bump);
  1740. scoped_refptr<StreamParserBuffer> buffer;
  1741. // GetNextBuffer() should return the next buffer at position (5 + |bump|).
  1742. EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&buffer));
  1743. EXPECT_EQ(buffer->GetDecodeTimestamp(),
  1744. DecodeTimestamp::FromPresentationTime(5 * frame_duration() + bump));
  1745. // Check rest of buffers.
  1746. CheckExpectedBuffers(6, 9);
  1747. // Seek to position 15.
  1748. Seek(15);
  1749. // Append 5 buffers at positions (15 + |bump|) through 19, where the coded
  1750. // frame group begins at 15.
  1751. NewCodedFrameGroupAppend_OffsetFirstBuffer(15, 5, bump);
  1752. // GetNextBuffer() should return the next buffer at position (15 + |bump|).
  1753. EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&buffer));
  1754. EXPECT_EQ(buffer->GetDecodeTimestamp(), DecodeTimestamp::FromPresentationTime(
  1755. 15 * frame_duration() + bump));
  1756. // Check rest of buffers.
  1757. CheckExpectedBuffers(16, 19);
  1758. }
  1759. TEST_F(SourceBufferStreamTest, Seek_BeforeStartOfGroup) {
  1760. // Append 10 buffers at positions 5 through 14.
  1761. NewCodedFrameGroupAppend(5, 10);
  1762. // Seek to a time before the first buffer in the range.
  1763. Seek(0);
  1764. // Should return buffers from the beginning of the range.
  1765. CheckExpectedBuffers(5, 14);
  1766. }
  1767. TEST_F(SourceBufferStreamTest, OldSeekPoint_CompleteOverlap) {
  1768. // Append 5 buffers at positions 0 through 4.
  1769. NewCodedFrameGroupAppend(0, 4);
  1770. // Append 5 buffers at positions 10 through 14, and seek to the beginning of
  1771. // this range.
  1772. NewCodedFrameGroupAppend(10, 5);
  1773. Seek(10);
  1774. // Now seek to the beginning of the first range.
  1775. Seek(0);
  1776. // Completely overlap the old seek point.
  1777. NewCodedFrameGroupAppend(5, 15);
  1778. // The GetNextBuffer() call should respect the 2nd seek point.
  1779. CheckExpectedBuffers(0, 0);
  1780. }
  1781. TEST_F(SourceBufferStreamTest, OldSeekPoint_CompleteOverlap_Pending) {
  1782. // Append 2 buffers at positions 0 through 1.
  1783. NewCodedFrameGroupAppend(0, 2);
  1784. // Append 5 buffers at positions 15 through 19 and seek to beginning of the
  1785. // range.
  1786. NewCodedFrameGroupAppend(15, 5);
  1787. Seek(15);
  1788. // Now seek position 5.
  1789. Seek(5);
  1790. // Completely overlap the old seek point.
  1791. NewCodedFrameGroupAppend(10, 15);
  1792. // The seek at position 5 should still be pending.
  1793. CheckNoNextBuffer();
  1794. }
  1795. TEST_F(SourceBufferStreamTest, OldSeekPoint_MiddleOverlap) {
  1796. // Append 1 buffer at position 0, duration 10ms.
  1797. NewCodedFrameGroupAppend("0D10K");
  1798. // Append 3 IPBBB GOPs starting at 50ms.
  1799. NewCodedFrameGroupAppend(
  1800. "50K 90|60 60|70 70|80 80|90 "
  1801. "100K 140|110 110|120 120|130 130|140 "
  1802. "150K 190|160 160|170 170|180 180|190");
  1803. SeekToTimestampMs(150);
  1804. // Now seek to the beginning of the stream.
  1805. SeekToTimestampMs(0);
  1806. // Overlap the middle of the last range with a partial GOP, just a keyframe.
  1807. NewCodedFrameGroupAppend("100D10K");
  1808. CheckExpectedRangesByTimestamp("{ [0,10) [50,200) }");
  1809. // The GetNextBuffer() call should respect the 2nd seek point.
  1810. CheckExpectedBuffers("0K");
  1811. CheckNoNextBuffer();
  1812. // Check the data in the second range.
  1813. SeekToTimestampMs(50);
  1814. CheckExpectedBuffers(
  1815. "50K 90|60 60|70 70|80 80|90 100K 150K 190|160 160|170 170|180 180|190");
  1816. CheckNoNextBuffer();
  1817. }
  1818. TEST_F(SourceBufferStreamTest, OldSeekPoint_MiddleOverlap_Pending) {
  1819. // Append 1 buffer at position 0, duration 10ms.
  1820. NewCodedFrameGroupAppend("0D10K");
  1821. // Append 3 IPBBB GOPs starting at 50ms. Then seek to 150ms.
  1822. NewCodedFrameGroupAppend(
  1823. "50K 90|60 60|70 70|80 80|90 "
  1824. "100K 140|110 110|120 120|130 130|140 "
  1825. "150K 190|160 160|170 170|180 180|190");
  1826. SeekToTimestampMs(150);
  1827. // Now seek to unbuffered time 20ms.
  1828. SeekToTimestampMs(20);
  1829. // Overlap the middle of the last range with a partial GOP, just a keyframe.
  1830. NewCodedFrameGroupAppend("100D10K");
  1831. CheckExpectedRangesByTimestamp("{ [0,10) [50,200) }");
  1832. // The seek to 20ms should still be pending.
  1833. CheckNoNextBuffer();
  1834. // Check the data in both ranges.
  1835. SeekToTimestampMs(0);
  1836. CheckExpectedBuffers("0K");
  1837. CheckNoNextBuffer();
  1838. SeekToTimestampMs(50);
  1839. CheckExpectedBuffers(
  1840. "50K 90|60 60|70 70|80 80|90 100K 150K 190|160 160|170 170|180 180|190");
  1841. CheckNoNextBuffer();
  1842. }
  1843. TEST_F(SourceBufferStreamTest, OldSeekPoint_StartOverlap) {
  1844. // Append 2 buffers at positions 0 through 1.
  1845. NewCodedFrameGroupAppend(0, 2);
  1846. // Append 15 buffers at positions 5 through 19 and seek to position 15.
  1847. NewCodedFrameGroupAppend(5, 15);
  1848. Seek(15);
  1849. // Now seek to the beginning of the stream.
  1850. Seek(0);
  1851. // Start overlap the old seek point.
  1852. NewCodedFrameGroupAppend(10, 10);
  1853. // The GetNextBuffer() call should respect the 2nd seek point.
  1854. CheckExpectedBuffers(0, 0);
  1855. }
  1856. TEST_F(SourceBufferStreamTest, OldSeekPoint_StartOverlap_Pending) {
  1857. // Append 2 buffers at positions 0 through 1.
  1858. NewCodedFrameGroupAppend(0, 2);
  1859. // Append 15 buffers at positions 10 through 24 and seek to position 20.
  1860. NewCodedFrameGroupAppend(10, 15);
  1861. Seek(20);
  1862. // Now seek to position 5.
  1863. Seek(5);
  1864. // Start overlap the old seek point.
  1865. NewCodedFrameGroupAppend(15, 10);
  1866. // The seek at time 0 should still be pending.
  1867. CheckNoNextBuffer();
  1868. }
  1869. TEST_F(SourceBufferStreamTest, OldSeekPoint_EndOverlap) {
  1870. // Append 5 buffers at positions 0 through 4.
  1871. NewCodedFrameGroupAppend(0, 4);
  1872. // Append 15 buffers at positions 10 through 24 and seek to start of range.
  1873. NewCodedFrameGroupAppend(10, 15);
  1874. Seek(10);
  1875. // Now seek to the beginning of the stream.
  1876. Seek(0);
  1877. // End overlap the old seek point.
  1878. NewCodedFrameGroupAppend(5, 10);
  1879. // The GetNextBuffer() call should respect the 2nd seek point.
  1880. CheckExpectedBuffers(0, 0);
  1881. }
  1882. TEST_F(SourceBufferStreamTest, OldSeekPoint_EndOverlap_Pending) {
  1883. // Append 2 buffers at positions 0 through 1.
  1884. NewCodedFrameGroupAppend(0, 2);
  1885. // Append 15 buffers at positions 15 through 29 and seek to start of range.
  1886. NewCodedFrameGroupAppend(15, 15);
  1887. Seek(15);
  1888. // Now seek to position 5
  1889. Seek(5);
  1890. // End overlap the old seek point.
  1891. NewCodedFrameGroupAppend(10, 10);
  1892. // The seek at time 5 should still be pending.
  1893. CheckNoNextBuffer();
  1894. }
  1895. TEST_F(SourceBufferStreamTest, GetNextBuffer_AfterMerges) {
  1896. // Append 5 buffers at positions 10 through 14.
  1897. NewCodedFrameGroupAppend(10, 5);
  1898. // Seek to buffer at position 12.
  1899. Seek(12);
  1900. // Append 5 buffers at positions 5 through 9.
  1901. NewCodedFrameGroupAppend(5, 5);
  1902. // Make sure ranges are merged.
  1903. CheckExpectedRanges("{ [5,14) }");
  1904. // Make sure the next buffer is correct.
  1905. CheckExpectedBuffers(10, 10);
  1906. // Append 5 buffers at positions 15 through 19.
  1907. NewCodedFrameGroupAppend(15, 5);
  1908. CheckExpectedRanges("{ [5,19) }");
  1909. // Make sure the remaining next buffers are correct.
  1910. CheckExpectedBuffers(11, 14);
  1911. }
  1912. TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenAppend) {
  1913. // Append 4 buffers at positions 0 through 3.
  1914. NewCodedFrameGroupAppend(0, 4);
  1915. // Seek to buffer at position 0 and get all buffers.
  1916. Seek(0);
  1917. CheckExpectedBuffers(0, 3);
  1918. // Next buffer is at position 4, so should not be able to fulfill request.
  1919. CheckNoNextBuffer();
  1920. // Append 2 buffers at positions 4 through 5.
  1921. AppendBuffers(4, 2);
  1922. CheckExpectedBuffers(4, 5);
  1923. }
  1924. // This test covers the case where new buffers start-overlap a range whose next
  1925. // buffer is not buffered.
  1926. TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenStartOverlap) {
  1927. // Append 10 buffers at positions 0 through 9 and exhaust the buffers.
  1928. NewCodedFrameGroupAppend(0, 10, &kDataA);
  1929. Seek(0);
  1930. CheckExpectedBuffers(0, 9, &kDataA);
  1931. // Next buffer is at position 10, so should not be able to fulfill request.
  1932. CheckNoNextBuffer();
  1933. // Append 6 buffers at positons 5 through 10. This is to test that doing a
  1934. // start-overlap successfully fulfills the read at position 10, even though
  1935. // position 10 was unbuffered.
  1936. NewCodedFrameGroupAppend(5, 6, &kDataB);
  1937. CheckExpectedBuffers(10, 10, &kDataB);
  1938. // Then add 5 buffers from positions 11 though 15.
  1939. AppendBuffers(11, 5, &kDataB);
  1940. // Check the next 4 buffers are correct, which also effectively seeks to
  1941. // position 15.
  1942. CheckExpectedBuffers(11, 14, &kDataB);
  1943. // Replace the next buffer at position 15 with another start overlap.
  1944. NewCodedFrameGroupAppend(15, 2, &kDataA);
  1945. CheckExpectedBuffers(15, 16, &kDataA);
  1946. }
  1947. // Tests a start overlap that occurs right at the timestamp of the last output
  1948. // buffer that was returned by GetNextBuffer(). This test verifies that
  1949. // GetNextBuffer() skips to second GOP in the newly appended data instead
  1950. // of returning two buffers with the same timestamp.
  1951. TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenStartOverlap2) {
  1952. NewCodedFrameGroupAppend("0K 30 60 90 120");
  1953. Seek(0);
  1954. CheckExpectedBuffers("0K 30 60 90 120");
  1955. CheckNoNextBuffer();
  1956. // Append a keyframe with the same timestamp as the last buffer output.
  1957. NewCodedFrameGroupAppend("120D30K");
  1958. CheckNoNextBuffer();
  1959. // Append the rest of the coded frame group and make sure that buffers are
  1960. // returned from the first GOP after 120.
  1961. AppendBuffers("150 180 210K 240");
  1962. CheckExpectedBuffers("210K 240");
  1963. // Seek to the beginning and verify the contents of the source buffer.
  1964. Seek(0);
  1965. CheckExpectedBuffers("0K 30 60 90 120K 150 180 210K 240");
  1966. CheckNoNextBuffer();
  1967. }
  1968. // This test covers the case where new buffers completely overlap a range
  1969. // whose next buffer is not buffered.
  1970. TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenCompleteOverlap) {
  1971. // Append 5 buffers at positions 10 through 14 and exhaust the buffers.
  1972. NewCodedFrameGroupAppend(10, 5, &kDataA);
  1973. Seek(10);
  1974. CheckExpectedBuffers(10, 14, &kDataA);
  1975. // Next buffer is at position 15, so should not be able to fulfill request.
  1976. CheckNoNextBuffer();
  1977. // Do a complete overlap and test that this successfully fulfills the read
  1978. // at position 15.
  1979. NewCodedFrameGroupAppend(5, 11, &kDataB);
  1980. CheckExpectedBuffers(15, 15, &kDataB);
  1981. // Then add 5 buffers from positions 16 though 20.
  1982. AppendBuffers(16, 5, &kDataB);
  1983. // Check the next 4 buffers are correct, which also effectively seeks to
  1984. // position 20.
  1985. CheckExpectedBuffers(16, 19, &kDataB);
  1986. // Do a complete overlap and replace the buffer at position 20.
  1987. NewCodedFrameGroupAppend(0, 21, &kDataA);
  1988. CheckExpectedBuffers(20, 20, &kDataA);
  1989. }
  1990. // This test covers the case where a range is stalled waiting for its next
  1991. // buffer, then an end-overlap causes the end of the range to be deleted.
  1992. TEST_F(SourceBufferStreamTest, GetNextBuffer_ExhaustThenEndOverlap) {
  1993. // Append 5 buffers at positions 10 through 14 and exhaust the buffers.
  1994. NewCodedFrameGroupAppend(10, 5, &kDataA);
  1995. Seek(10);
  1996. CheckExpectedBuffers(10, 14, &kDataA);
  1997. CheckExpectedRanges("{ [10,14) }");
  1998. // Next buffer is at position 15, so should not be able to fulfill request.
  1999. CheckNoNextBuffer();
  2000. // Do an end overlap that causes the latter half of the range to be deleted.
  2001. NewCodedFrameGroupAppend(5, 6, &kDataB);
  2002. CheckNoNextBuffer();
  2003. CheckExpectedRanges("{ [5,10) }");
  2004. // Fill in the gap. Getting the next buffer should still stall at position 15.
  2005. for (int i = 11; i <= 14; i++) {
  2006. AppendBuffers(i, 1, &kDataB);
  2007. CheckNoNextBuffer();
  2008. }
  2009. // Append the buffer at position 15 and check to make sure all is correct.
  2010. AppendBuffers(15, 1);
  2011. CheckExpectedBuffers(15, 15);
  2012. CheckExpectedRanges("{ [5,15) }");
  2013. }
  2014. // This test is testing the "next buffer" logic after a complete overlap. In
  2015. // this scenario, when the track buffer is exhausted, there is no buffered data
  2016. // to fulfill the request. The SourceBufferStream should be able to fulfill the
  2017. // request when the data is later appended, and should not lose track of the
  2018. // "next buffer" position.
  2019. TEST_F(SourceBufferStreamTest, GetNextBuffer_Overlap_Selected_Complete) {
  2020. // Append 5 buffers at positions 5 through 9.
  2021. NewCodedFrameGroupAppend(5, 5, &kDataA);
  2022. // Seek to buffer at position 5 and get next buffer.
  2023. Seek(5);
  2024. CheckExpectedBuffers(5, 5, &kDataA);
  2025. // Replace existing data with new data.
  2026. NewCodedFrameGroupAppend(5, 5, &kDataB);
  2027. // Expect old data up until next keyframe in new data.
  2028. CheckExpectedBuffers(6, 9, &kDataA);
  2029. // Next buffer is at position 10, so should not be able to fulfill the
  2030. // request.
  2031. CheckNoNextBuffer();
  2032. // Now add 5 new buffers at positions 10 through 14.
  2033. AppendBuffers(10, 5, &kDataB);
  2034. CheckExpectedBuffers(10, 14, &kDataB);
  2035. }
  2036. TEST_F(SourceBufferStreamTest, PresentationTimestampIndependence) {
  2037. // Append 20 buffers at position 0.
  2038. NewCodedFrameGroupAppend(0, 20);
  2039. Seek(0);
  2040. int last_keyframe_idx = -1;
  2041. base::TimeDelta last_keyframe_presentation_timestamp;
  2042. base::TimeDelta last_p_frame_presentation_timestamp;
  2043. // Check for IBB...BBP pattern.
  2044. for (int i = 0; i < 20; i++) {
  2045. scoped_refptr<StreamParserBuffer> buffer;
  2046. EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&buffer));
  2047. if (buffer->is_key_frame()) {
  2048. EXPECT_EQ(DecodeTimestamp::FromPresentationTime(buffer->timestamp()),
  2049. buffer->GetDecodeTimestamp());
  2050. last_keyframe_idx = i;
  2051. last_keyframe_presentation_timestamp = buffer->timestamp();
  2052. } else if (i == last_keyframe_idx + 1) {
  2053. ASSERT_NE(last_keyframe_idx, -1);
  2054. last_p_frame_presentation_timestamp = buffer->timestamp();
  2055. EXPECT_LT(last_keyframe_presentation_timestamp,
  2056. last_p_frame_presentation_timestamp);
  2057. } else {
  2058. EXPECT_GT(buffer->timestamp(), last_keyframe_presentation_timestamp);
  2059. EXPECT_LT(buffer->timestamp(), last_p_frame_presentation_timestamp);
  2060. EXPECT_LT(DecodeTimestamp::FromPresentationTime(buffer->timestamp()),
  2061. buffer->GetDecodeTimestamp());
  2062. }
  2063. }
  2064. }
  2065. TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteFront) {
  2066. // Set memory limit to 20 buffers.
  2067. SetMemoryLimit(20);
  2068. // Append 20 buffers at positions 0 through 19.
  2069. NewCodedFrameGroupAppend(0, 1, &kDataA);
  2070. for (int i = 1; i < 20; i++)
  2071. AppendBuffers(i, 1, &kDataA);
  2072. // GC should be a no-op, since we are just under memory limit.
  2073. EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(0, 0));
  2074. CheckExpectedRanges("{ [0,19) }");
  2075. Seek(0);
  2076. CheckExpectedBuffers(0, 19, &kDataA);
  2077. // Seek to the middle of the stream.
  2078. Seek(10);
  2079. // We are about to append 5 new buffers and current playback position is 10,
  2080. // so the GC algorithm should be able to delete some old data from the front.
  2081. EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(10, 5));
  2082. CheckExpectedRanges("{ [5,19) }");
  2083. // Append 5 buffers to the end of the stream.
  2084. AppendBuffers(20, 5, &kDataA);
  2085. CheckExpectedRanges("{ [5,24) }");
  2086. CheckExpectedBuffers(10, 24, &kDataA);
  2087. Seek(5);
  2088. CheckExpectedBuffers(5, 9, &kDataA);
  2089. }
  2090. TEST_F(SourceBufferStreamTest,
  2091. GarbageCollection_DeleteFront_PreserveSeekedGOP) {
  2092. // Set memory limit to 15 buffers.
  2093. SetMemoryLimit(15);
  2094. NewCodedFrameGroupAppend("0K 10 20 30 40 50K 60 70 80 90");
  2095. NewCodedFrameGroupAppend("1000K 1010 1020 1030 1040");
  2096. // GC should be a no-op, since we are just under memory limit.
  2097. EXPECT_TRUE(GarbageCollect(base::TimeDelta(), 0));
  2098. CheckExpectedRangesByTimestamp("{ [0,100) [1000,1050) }");
  2099. // Seek to the near the end of the first range
  2100. SeekToTimestampMs(95);
  2101. // We are about to append 7 new buffers and current playback position is at
  2102. // the end of the last GOP in the first range, so the GC algorithm should be
  2103. // able to delete some old data from the front, but must not collect the last
  2104. // GOP in that first range. Neither can it collect the last appended GOP
  2105. // (which is the entire second range), so GC should return false since it
  2106. // couldn't collect enough.
  2107. EXPECT_FALSE(GarbageCollect(base::Milliseconds(95), 7));
  2108. CheckExpectedRangesByTimestamp("{ [50,100) [1000,1050) }");
  2109. }
  2110. TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteFrontGOPsAtATime) {
  2111. // Set memory limit to 20 buffers.
  2112. SetMemoryLimit(20);
  2113. // Append 20 buffers at positions 0 through 19.
  2114. NewCodedFrameGroupAppend(0, 20, &kDataA);
  2115. // Seek to position 10.
  2116. Seek(10);
  2117. CheckExpectedRanges("{ [0,19) }");
  2118. // Add one buffer to put the memory over the cap.
  2119. EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(10, 1));
  2120. AppendBuffers(20, 1, &kDataA);
  2121. // GC should have deleted the first 5 buffers so that the range still begins
  2122. // with a keyframe.
  2123. CheckExpectedRanges("{ [5,20) }");
  2124. CheckExpectedBuffers(10, 20, &kDataA);
  2125. Seek(5);
  2126. CheckExpectedBuffers(5, 9, &kDataA);
  2127. }
  2128. TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteBack) {
  2129. // Set memory limit to 5 buffers.
  2130. SetMemoryLimit(5);
  2131. // Append 5 buffers at positions 15 through 19.
  2132. NewCodedFrameGroupAppend(15, 5, &kDataA);
  2133. EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(0, 0));
  2134. // Append 5 buffers at positions 0 through 4.
  2135. NewCodedFrameGroupAppend(0, 5, &kDataA);
  2136. CheckExpectedRanges("{ [0,4) [15,19) }");
  2137. // Seek to position 0.
  2138. Seek(0);
  2139. EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(0, 0));
  2140. // Should leave the first 5 buffers from 0 to 4.
  2141. CheckExpectedRanges("{ [0,4) }");
  2142. CheckExpectedBuffers(0, 4, &kDataA);
  2143. }
  2144. TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteFrontAndBack) {
  2145. // Set memory limit to 3 buffers.
  2146. SetMemoryLimit(3);
  2147. // Seek to position 15.
  2148. Seek(15);
  2149. // Append 40 buffers at positions 0 through 39.
  2150. NewCodedFrameGroupAppend(0, 40, &kDataA);
  2151. // GC will try to keep data between current playback position and last append
  2152. // position. This will ensure that the last append position is 19 and will
  2153. // allow GC algorithm to collect data outside of the range [15,19)
  2154. NewCodedFrameGroupAppend(15, 5, &kDataA);
  2155. CheckExpectedRanges("{ [0,39) }");
  2156. // Should leave the GOP containing the current playback position 15 and the
  2157. // last append position 19. GC returns false, since we are still above limit.
  2158. EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(15, 0));
  2159. CheckExpectedRanges("{ [15,19) }");
  2160. CheckExpectedBuffers(15, 19, &kDataA);
  2161. CheckNoNextBuffer();
  2162. }
  2163. TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteSeveralRanges) {
  2164. // Append 5 buffers at positions 0 through 4.
  2165. NewCodedFrameGroupAppend(0, 5);
  2166. // Append 5 buffers at positions 10 through 14.
  2167. NewCodedFrameGroupAppend(10, 5);
  2168. // Append 5 buffers at positions 20 through 24.
  2169. NewCodedFrameGroupAppend(20, 5);
  2170. // Append 5 buffers at positions 40 through 44.
  2171. NewCodedFrameGroupAppend(40, 5);
  2172. CheckExpectedRanges("{ [0,4) [10,14) [20,24) [40,44) }");
  2173. // Seek to position 20.
  2174. Seek(20);
  2175. CheckExpectedBuffers(20, 20);
  2176. // Set memory limit to 1 buffer.
  2177. SetMemoryLimit(1);
  2178. // Append 5 buffers at positions 30 through 34.
  2179. NewCodedFrameGroupAppend(30, 5);
  2180. // We will have more than 1 buffer left, GC will fail
  2181. EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(20, 0));
  2182. // Should have deleted all buffer ranges before the current buffer and after
  2183. // last GOP
  2184. CheckExpectedRanges("{ [20,24) [30,34) }");
  2185. CheckExpectedBuffers(21, 24);
  2186. CheckNoNextBuffer();
  2187. // Continue appending into the last range to make sure it didn't break.
  2188. AppendBuffers(35, 10);
  2189. EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(20, 0));
  2190. // Should save everything between read head and last appended
  2191. CheckExpectedRanges("{ [20,24) [30,44) }");
  2192. // Make sure appending before and after the ranges didn't somehow break.
  2193. SetMemoryLimit(100);
  2194. NewCodedFrameGroupAppend(0, 10);
  2195. EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(20, 0));
  2196. CheckExpectedRanges("{ [0,9) [20,24) [30,44) }");
  2197. Seek(0);
  2198. CheckExpectedBuffers(0, 9);
  2199. NewCodedFrameGroupAppend(90, 10);
  2200. EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(0, 0));
  2201. CheckExpectedRanges("{ [0,9) [20,24) [30,44) [90,99) }");
  2202. Seek(30);
  2203. CheckExpectedBuffers(30, 44);
  2204. CheckNoNextBuffer();
  2205. Seek(90);
  2206. CheckExpectedBuffers(90, 99);
  2207. CheckNoNextBuffer();
  2208. }
  2209. TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteAfterLastAppend) {
  2210. // Set memory limit to 10 buffers.
  2211. SetMemoryLimit(10);
  2212. // Append 1 GOP starting at 310ms, 30ms apart.
  2213. NewCodedFrameGroupAppend("310K 340 370");
  2214. // Append 2 GOPs starting at 490ms, 30ms apart.
  2215. NewCodedFrameGroupAppend("490K 520 550 580K 610 640");
  2216. EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(0, 0));
  2217. CheckExpectedRangesByTimestamp("{ [310,400) [490,670) }");
  2218. // Seek to the GOP at 580ms.
  2219. SeekToTimestampMs(580);
  2220. // Append 2 GOPs before the existing ranges.
  2221. // So the ranges before GC are "{ [100,280) [310,400) [490,670) }".
  2222. NewCodedFrameGroupAppend("100K 130 160 190K 220 250K");
  2223. EXPECT_TRUE(GarbageCollect(base::Milliseconds(580), 0));
  2224. // Should save the newly appended GOPs.
  2225. CheckExpectedRangesByTimestamp("{ [100,280) [580,670) }");
  2226. }
  2227. TEST_F(SourceBufferStreamTest, GarbageCollection_DeleteAfterLastAppendMerged) {
  2228. // Set memory limit to 10 buffers.
  2229. SetMemoryLimit(10);
  2230. // Append 3 GOPs starting at 400ms, 30ms apart.
  2231. NewCodedFrameGroupAppend("400K 430 460 490K 520 550 580K 610 640");
  2232. // Seek to the GOP at 580ms.
  2233. SeekToTimestampMs(580);
  2234. // Append 2 GOPs starting at 220ms, and they will be merged with the existing
  2235. // range. So the range before GC is "{ [220,670) }".
  2236. NewCodedFrameGroupAppend("220K 250 280 310K 340 370");
  2237. EXPECT_TRUE(GarbageCollect(base::Milliseconds(580), 0));
  2238. // Should save the newly appended GOPs.
  2239. CheckExpectedRangesByTimestamp("{ [220,400) [580,670) }");
  2240. }
  2241. TEST_F(SourceBufferStreamTest, GarbageCollection_NoSeek) {
  2242. // Set memory limit to 20 buffers.
  2243. SetMemoryLimit(20);
  2244. // Append 25 buffers at positions 0 through 24.
  2245. NewCodedFrameGroupAppend(0, 25, &kDataA);
  2246. // If playback is still in the first GOP (starting at 0), GC should fail.
  2247. EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(2, 0));
  2248. CheckExpectedRanges("{ [0,24) }");
  2249. // As soon as playback position moves past the first GOP, it should be removed
  2250. // and after removing the first GOP we are under memory limit.
  2251. EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(5, 0));
  2252. CheckExpectedRanges("{ [5,24) }");
  2253. CheckNoNextBuffer();
  2254. Seek(5);
  2255. CheckExpectedBuffers(5, 24, &kDataA);
  2256. }
  2257. TEST_F(SourceBufferStreamTest, GarbageCollection_PendingSeek) {
  2258. // Append 10 buffers at positions 0 through 9.
  2259. NewCodedFrameGroupAppend(0, 10, &kDataA);
  2260. // Append 5 buffers at positions 25 through 29.
  2261. NewCodedFrameGroupAppend(25, 5, &kDataA);
  2262. // Seek to position 15.
  2263. Seek(15);
  2264. CheckNoNextBuffer();
  2265. CheckExpectedRanges("{ [0,9) [25,29) }");
  2266. // Set memory limit to 5 buffers.
  2267. SetMemoryLimit(5);
  2268. // Append 5 buffers as positions 30 to 34 to trigger GC.
  2269. AppendBuffers(30, 5, &kDataA);
  2270. EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(30, 0));
  2271. // The current algorithm will delete from the beginning until the memory is
  2272. // under cap.
  2273. CheckExpectedRanges("{ [30,34) }");
  2274. // Expand memory limit again so that GC won't be triggered.
  2275. SetMemoryLimit(100);
  2276. // Append data to fulfill seek.
  2277. EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(30, 5));
  2278. NewCodedFrameGroupAppend(15, 5, &kDataA);
  2279. // Check to make sure all is well.
  2280. CheckExpectedRanges("{ [15,19) [30,34) }");
  2281. CheckExpectedBuffers(15, 19, &kDataA);
  2282. Seek(30);
  2283. CheckExpectedBuffers(30, 34, &kDataA);
  2284. }
  2285. TEST_F(SourceBufferStreamTest, GarbageCollection_NeedsMoreData) {
  2286. // Set memory limit to 15 buffers.
  2287. SetMemoryLimit(15);
  2288. // Append 10 buffers at positions 0 through 9.
  2289. NewCodedFrameGroupAppend(0, 10, &kDataA);
  2290. // Advance next buffer position to 10.
  2291. Seek(0);
  2292. EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(0, 0));
  2293. CheckExpectedBuffers(0, 9, &kDataA);
  2294. CheckNoNextBuffer();
  2295. // Append 20 buffers at positions 15 through 34.
  2296. NewCodedFrameGroupAppend(15, 20, &kDataA);
  2297. CheckExpectedRanges("{ [0,9) [15,34) }");
  2298. // GC should save the keyframe before the next buffer position and the data
  2299. // closest to the next buffer position. It will also save all buffers from
  2300. // next buffer to the last GOP appended, which overflows limit and leads to
  2301. // failure.
  2302. EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(5, 0));
  2303. CheckExpectedRanges("{ [5,9) [15,34) }");
  2304. // Now fulfill the seek at position 10. This will make GC delete the data
  2305. // before position 10 to keep it within cap.
  2306. NewCodedFrameGroupAppend(10, 5, &kDataA);
  2307. EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(10, 0));
  2308. CheckExpectedRanges("{ [10,24) }");
  2309. CheckExpectedBuffers(10, 24, &kDataA);
  2310. }
  2311. // Using position based test API:
  2312. // DTS : 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
  2313. // PTS : 0 4 1 2 3 5 9 6 7 8 0 4 1 2 3 5 9 6 7 8 0 4 1 2 3
  2314. // old  : A a a a a A a a a a A a a a a*A*a a
  2315. // -- Garbage Collect --
  2316. // after: A a a
  2317. // -- Read one buffer --
  2318. // after: A*a*a
  2319. // new : B b b b b B b b b b B b b b b B b b b b
  2320. // track: *a*a
  2321. // -- Garbage Collect --
  2322. // after: B b b b b
  2323. // track: *a*a
  2324. // -- Read 2 buffers -> exhausts track buffer
  2325. // after: B b b b b
  2326. // (awaiting next keyframe after GOP at position 15)
  2327. // new : *B*b b b b
  2328. // after: B b b b b*B*b b b b
  2329. // -- Garbage Collect --
  2330. // after: *B*b b b b
  2331. TEST_F(SourceBufferStreamTest, GarbageCollection_TrackBuffer) {
  2332. // Set memory limit to 3 buffers.
  2333. SetMemoryLimit(3);
  2334. // Seek to position 15.
  2335. Seek(15);
  2336. // Append 18 buffers at positions 0 through 17 (DTS), 0 through 19 (PTS) with
  2337. // partial 4th GOP.
  2338. NewCodedFrameGroupAppend(0, 18, &kDataA);
  2339. EXPECT_TRUE(GarbageCollectWithPlaybackAtBuffer(15, 0));
  2340. // GC should leave GOP containing seek position (15,16,17 DTS; 15,19,16 PTS).
  2341. // Unlike the rest of the position based test API used in this case,
  2342. // CheckExpectedRanges() uses expectation strings containing actual timestamps
  2343. // (divided by frame_duration_).
  2344. CheckExpectedRanges("{ [15,19) }");
  2345. // Move next buffer position to 16.
  2346. CheckExpectedBuffers(15, 15, &kDataA);
  2347. // Completely overlap the existing buffers with 4 full GOPs (0-19, PTS and
  2348. // DTS).
  2349. NewCodedFrameGroupAppend(0, 20, &kDataB);
  2350. // Final GOP [15,19) contains 5 buffers, which is more than memory limit of
  2351. // 3 buffers set at the beginning of the test, so GC will fail.
  2352. EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(15, 0));
  2353. // Because buffers 16,17 (DTS), 19,16 (PTS) are not keyframes, they are moved
  2354. // to the track buffer upon overlap. The source buffer (i.e. not the track
  2355. // buffer) is now waiting for the next keyframe beyond GOP that survived GC.
  2356. CheckExpectedRanges("{ [15,19) }"); // Source buffer
  2357. CheckExpectedBuffers(16, 17, &kDataA); // Exhaust the track buffer
  2358. CheckNoNextBuffer(); // Confirms the source buffer is awaiting next keyframe.
  2359. // Now add a 5-frame GOP at position 20-24 (PTS and DTS).
  2360. AppendBuffers(20, 5, &kDataB);
  2361. // 5 buffers in final GOP, GC will fail
  2362. EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(20, 0));
  2363. // Should garbage collect such that there are 5 frames remaining, starting at
  2364. // the keyframe.
  2365. CheckExpectedRanges("{ [20,24) }");
  2366. // The buffer at position 16 (PTS 19) in track buffer was adjacent
  2367. // to the next keyframe (PTS=DTS=20), so no warning should be emitted on that
  2368. // track buffer exhaustion even though the last frame read out of track buffer
  2369. // before exhaustion was position 17 (PTS 16).
  2370. CheckExpectedBuffers(20, 24, &kDataB);
  2371. CheckNoNextBuffer();
  2372. }
  2373. // Test GC preserves data starting at first GOP containing playback position.
  2374. TEST_F(SourceBufferStreamTest, GarbageCollection_SaveDataAtPlaybackPosition) {
  2375. // Set memory limit to 30 buffers = 1 second of data.
  2376. SetMemoryLimit(30);
  2377. // And append 300 buffers = 10 seconds of data.
  2378. NewCodedFrameGroupAppend(0, 300, &kDataA);
  2379. CheckExpectedRanges("{ [0,299) }");
  2380. // Playback position at 0, all data must be preserved.
  2381. EXPECT_FALSE(GarbageCollect(base::Milliseconds(0), 0));
  2382. CheckExpectedRanges("{ [0,299) }");
  2383. // Playback position at 1 sec, the first second of data [0,29) should be
  2384. // collected, since we are way over memory limit.
  2385. EXPECT_FALSE(GarbageCollect(base::Milliseconds(1000), 0));
  2386. CheckExpectedRanges("{ [30,299) }");
  2387. // Playback position at 1.1 sec, no new data can be collected, since the
  2388. // playback position is still in the first GOP of buffered data.
  2389. EXPECT_FALSE(GarbageCollect(base::Milliseconds(1100), 0));
  2390. CheckExpectedRanges("{ [30,299) }");
  2391. // Playback position at 5.166 sec, just at the very end of GOP corresponding
  2392. // to buffer range 150-155, which should be preserved.
  2393. EXPECT_FALSE(GarbageCollect(base::Milliseconds(5166), 0));
  2394. CheckExpectedRanges("{ [150,299) }");
  2395. // Playback position at 5.167 sec, just past the end of GOP corresponding to
  2396. // buffer range 150-155, it should be garbage collected now.
  2397. EXPECT_FALSE(GarbageCollect(base::Milliseconds(5167), 0));
  2398. CheckExpectedRanges("{ [155,299) }");
  2399. // Playback at 9.0 sec, we can now successfully collect all data except the
  2400. // last second and we are back under memory limit of 30 buffers, so GCIfNeeded
  2401. // should return true.
  2402. EXPECT_TRUE(GarbageCollect(base::Milliseconds(9000), 0));
  2403. CheckExpectedRanges("{ [270,299) }");
  2404. // Playback at 9.999 sec, GC succeeds, since we are under memory limit even
  2405. // without removing any data.
  2406. EXPECT_TRUE(GarbageCollect(base::Milliseconds(9999), 0));
  2407. CheckExpectedRanges("{ [270,299) }");
  2408. // Playback at 15 sec, this should never happen during regular playback in
  2409. // browser, since this position has no data buffered, but it should still
  2410. // cause no problems to GC algorithm, so test it just in case.
  2411. EXPECT_TRUE(GarbageCollect(base::Milliseconds(15000), 0));
  2412. CheckExpectedRanges("{ [270,299) }");
  2413. }
  2414. // Test saving the last GOP appended when this GOP is the only GOP in its range.
  2415. TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP) {
  2416. // Set memory limit to 3 and make sure the 4-byte GOP is not garbage
  2417. // collected.
  2418. SetMemoryLimit(3);
  2419. NewCodedFrameGroupAppend("0K 30 60 90");
  2420. EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(0, 0));
  2421. CheckExpectedRangesByTimestamp("{ [0,120) }");
  2422. // Make sure you can continue appending data to this GOP; again, GC should not
  2423. // wipe out anything.
  2424. AppendBuffers("120D30");
  2425. EXPECT_FALSE(GarbageCollectWithPlaybackAtBuffer(0, 0));
  2426. CheckExpectedRangesByTimestamp("{ [0,150) }");
  2427. // Append a 2nd range after this without triggering GC.
  2428. NewCodedFrameGroupAppend("200K 230 260 290K 320 350");
  2429. CheckExpectedRangesByTimestamp("{ [0,150) [200,380) }");
  2430. // Seek to 290ms.
  2431. SeekToTimestampMs(290);
  2432. // Now append a GOP in a separate range after the selected range and trigger
  2433. // GC. Because it is after 290ms, this tests that the GOP is saved when
  2434. // deleting from the back.
  2435. NewCodedFrameGroupAppend("500K 530 560 590");
  2436. EXPECT_FALSE(GarbageCollect(base::Milliseconds(290), 0));
  2437. // Should save GOPs between 290ms and the last GOP appended.
  2438. CheckExpectedRangesByTimestamp("{ [290,380) [500,620) }");
  2439. // Continue appending to this GOP after GC.
  2440. AppendBuffers("620D30");
  2441. EXPECT_FALSE(GarbageCollect(base::Milliseconds(290), 0));
  2442. CheckExpectedRangesByTimestamp("{ [290,380) [500,650) }");
  2443. }
  2444. // Test saving the last GOP appended when this GOP is in the middle of a
  2445. // non-selected range.
  2446. TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Middle) {
  2447. // Append 3 GOPs starting at 0ms, 30ms apart.
  2448. NewCodedFrameGroupAppend("0K 30 60 90K 120 150 180K 210 240");
  2449. CheckExpectedRangesByTimestamp("{ [0,270) }");
  2450. // Now set the memory limit to 1 and overlap the middle of the range with a
  2451. // new GOP.
  2452. SetMemoryLimit(1);
  2453. NewCodedFrameGroupAppend("80K 110 140");
  2454. // This whole GOP should be saved after GC, which will fail due to GOP being
  2455. // larger than 1 buffer
  2456. EXPECT_FALSE(GarbageCollect(base::Milliseconds(80), 0));
  2457. CheckExpectedRangesByTimestamp("{ [80,170) }");
  2458. // We should still be able to continue appending data to GOP
  2459. AppendBuffers("170D30");
  2460. EXPECT_FALSE(GarbageCollect(base::Milliseconds(80), 0));
  2461. CheckExpectedRangesByTimestamp("{ [80,200) }");
  2462. // Append a 2nd range after this range, without triggering GC.
  2463. NewCodedFrameGroupAppend("400K 430 460 490K 520 550 580K 610 640");
  2464. CheckExpectedRangesByTimestamp("{ [80,200) [400,670) }");
  2465. // Seek to 80ms to make the first range the selected range.
  2466. SeekToTimestampMs(80);
  2467. // Now append a GOP in the middle of the second range and trigger GC. Because
  2468. // it is after the selected range, this tests that the GOP is saved when
  2469. // deleting from the back.
  2470. NewCodedFrameGroupAppend("500K 530 560 590");
  2471. EXPECT_FALSE(GarbageCollect(base::Milliseconds(80), 0));
  2472. // Should save the GOPs between the seek point and GOP that was last appended
  2473. CheckExpectedRangesByTimestamp("{ [80,200) [400,620) }");
  2474. // Continue appending to this GOP after GC.
  2475. AppendBuffers("620D30");
  2476. EXPECT_FALSE(GarbageCollect(base::Milliseconds(80), 0));
  2477. CheckExpectedRangesByTimestamp("{ [80,200) [400,650) }");
  2478. }
  2479. // Test saving the last GOP appended when the GOP containing the next buffer is
  2480. // adjacent to the last GOP appended.
  2481. TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Selected1) {
  2482. // Append 3 GOPs at 0ms, 90ms, and 180ms.
  2483. NewCodedFrameGroupAppend("0K 30 60 90K 120 150 180K 210 240");
  2484. CheckExpectedRangesByTimestamp("{ [0,270) }");
  2485. // Seek to the GOP at 90ms.
  2486. SeekToTimestampMs(90);
  2487. // Set the memory limit to 1, then overlap the GOP at 0.
  2488. SetMemoryLimit(1);
  2489. NewCodedFrameGroupAppend("0K 30 60");
  2490. // GC should save the GOP at 0ms and 90ms, and will fail since GOP larger
  2491. // than 1 buffer
  2492. EXPECT_FALSE(GarbageCollect(base::Milliseconds(90), 0));
  2493. CheckExpectedRangesByTimestamp("{ [0,180) }");
  2494. // Seek to 0 and check all buffers.
  2495. SeekToTimestampMs(0);
  2496. CheckExpectedBuffers("0K 30 60 90K 120 150");
  2497. CheckNoNextBuffer();
  2498. // Now seek back to 90ms and append a GOP at 180ms.
  2499. SeekToTimestampMs(90);
  2500. NewCodedFrameGroupAppend("180K 210 240");
  2501. // Should save the GOP at 90ms and the GOP at 180ms.
  2502. EXPECT_FALSE(GarbageCollect(base::Milliseconds(90), 0));
  2503. CheckExpectedRangesByTimestamp("{ [90,270) }");
  2504. CheckExpectedBuffers("90K 120 150 180K 210 240");
  2505. CheckNoNextBuffer();
  2506. }
  2507. // Test saving the last GOP appended when it is at the beginning or end of the
  2508. // selected range. This tests when the last GOP appended is before or after the
  2509. // GOP containing the next buffer, but not directly adjacent to this GOP.
  2510. TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Selected2) {
  2511. // Append 4 GOPs starting at positions 0ms, 90ms, 180ms, 270ms.
  2512. NewCodedFrameGroupAppend("0K 30 60 90K 120 150 180K 210 240 270K 300 330");
  2513. CheckExpectedRangesByTimestamp("{ [0,360) }");
  2514. // Seek to the last GOP at 270ms.
  2515. SeekToTimestampMs(270);
  2516. // Set the memory limit to 1, then overlap the GOP at 90ms.
  2517. SetMemoryLimit(1);
  2518. NewCodedFrameGroupAppend("90K 120 150");
  2519. // GC will save data in the range where the most recent append has happened
  2520. // [0; 180) and the range where the next read position is [270;360)
  2521. EXPECT_FALSE(GarbageCollect(base::Milliseconds(270), 0));
  2522. CheckExpectedRangesByTimestamp("{ [0,180) [270,360) }");
  2523. // Add 3 GOPs to the end of the selected range at 360ms, 450ms, and 540ms.
  2524. NewCodedFrameGroupAppend("360K 390 420 450K 480 510 540K 570 600");
  2525. CheckExpectedRangesByTimestamp("{ [0,180) [270,630) }");
  2526. // Overlap the GOP at 450ms and garbage collect to test deleting from the
  2527. // back.
  2528. NewCodedFrameGroupAppend("450K 480 510");
  2529. EXPECT_FALSE(GarbageCollect(base::Milliseconds(270), 0));
  2530. // Should save GOPs from GOP at 270ms to GOP at 450ms.
  2531. CheckExpectedRangesByTimestamp("{ [270,540) }");
  2532. }
  2533. // Test saving the last GOP appended when it is the same as the GOP containing
  2534. // the next buffer.
  2535. TEST_F(SourceBufferStreamTest, GarbageCollection_SaveAppendGOP_Selected3) {
  2536. // Seek to start of stream.
  2537. SeekToTimestampMs(0);
  2538. // Append 3 GOPs starting at 0ms, 90ms, 180ms.
  2539. NewCodedFrameGroupAppend("0K 30 60 90K 120 150 180K 210 240");
  2540. CheckExpectedRangesByTimestamp("{ [0,270) }");
  2541. // Set the memory limit to 1 then begin appending the start of a GOP starting
  2542. // at 0ms.
  2543. SetMemoryLimit(1);
  2544. NewCodedFrameGroupAppend("0K 30");
  2545. // GC should save the newly appended GOP, which is also the next GOP that
  2546. // will be returned from the seek request.
  2547. EXPECT_FALSE(GarbageCollect(base::Milliseconds(0), 0));
  2548. CheckExpectedRangesByTimestamp("{ [0,60) }");
  2549. // Check the buffers in the range.
  2550. CheckExpectedBuffers("0K 30");
  2551. CheckNoNextBuffer();
  2552. // Continue appending to this buffer.
  2553. AppendBuffers("60 90");
  2554. // GC should still save the rest of this GOP and should be able to fulfill
  2555. // the read.
  2556. EXPECT_FALSE(GarbageCollect(base::Milliseconds(0), 0));
  2557. CheckExpectedRangesByTimestamp("{ [0,120) }");
  2558. CheckExpectedBuffers("60 90");
  2559. CheckNoNextBuffer();
  2560. }
  2561. // Test the performance of garbage collection.
  2562. TEST_F(SourceBufferStreamTest, GarbageCollection_Performance) {
  2563. // Force |keyframes_per_second_| to be equal to kDefaultFramesPerSecond.
  2564. SetStreamInfo(kDefaultFramesPerSecond, kDefaultFramesPerSecond);
  2565. const int kBuffersToKeep = 1000;
  2566. SetMemoryLimit(kBuffersToKeep);
  2567. int buffers_appended = 0;
  2568. NewCodedFrameGroupAppend(0, kBuffersToKeep);
  2569. buffers_appended += kBuffersToKeep;
  2570. const int kBuffersToAppend = 1000;
  2571. const int kGarbageCollections = 3;
  2572. for (int i = 0; i < kGarbageCollections; ++i) {
  2573. AppendBuffers(buffers_appended, kBuffersToAppend);
  2574. buffers_appended += kBuffersToAppend;
  2575. }
  2576. }
  2577. TEST_F(SourceBufferStreamTest, GarbageCollection_MediaTimeAfterLastAppendTime) {
  2578. // Set memory limit to 10 buffers.
  2579. SetMemoryLimit(10);
  2580. // Append 12 buffers. The duration of the last buffer is 30
  2581. NewCodedFrameGroupAppend("0K 30 60 90 120K 150 180 210K 240 270 300K 330D30");
  2582. CheckExpectedRangesByTimestamp("{ [0,360) }");
  2583. // Do a garbage collection with the media time higher than the timestamp of
  2584. // the last appended buffer (330), but still within buffered ranges, taking
  2585. // into account the duration of the last frame (timestamp of the last frame is
  2586. // 330, duration is 30, so the latest valid buffered position is 330+30=360).
  2587. EXPECT_TRUE(GarbageCollect(base::Milliseconds(360), 0));
  2588. // GC should collect one GOP from the front to bring us back under memory
  2589. // limit of 10 buffers.
  2590. CheckExpectedRangesByTimestamp("{ [120,360) }");
  2591. }
  2592. TEST_F(SourceBufferStreamTest,
  2593. GarbageCollection_MediaTimeOutsideOfStreamBufferedRange) {
  2594. // Set memory limit to 10 buffers.
  2595. SetMemoryLimit(10);
  2596. // Append 12 buffers.
  2597. NewCodedFrameGroupAppend("0K 30 60 90 120K 150 180 210K 240 270 300K 330");
  2598. CheckExpectedRangesByTimestamp("{ [0,360) }");
  2599. // Seek in order to set the stream read position to 330 an ensure that the
  2600. // stream selects the buffered range.
  2601. SeekToTimestampMs(330);
  2602. // Do a garbage collection with the media time outside the buffered ranges
  2603. // (this might happen when there's both audio and video streams, audio stream
  2604. // buffered range is longer than the video stream buffered range, since
  2605. // media::Pipeline uses audio stream as a time source in that case, it might
  2606. // return a media_time that is slightly outside of video buffered range). In
  2607. // those cases the GC algorithm should clamp the media_time value to the
  2608. // buffered ranges to work correctly (see crbug.com/563292).
  2609. EXPECT_TRUE(GarbageCollect(base::Milliseconds(361), 0));
  2610. // GC should collect one GOP from the front to bring us back under memory
  2611. // limit of 10 buffers.
  2612. CheckExpectedRangesByTimestamp("{ [120,360) }");
  2613. }
  2614. TEST_F(SourceBufferStreamTest, GetRemovalRange_BytesToFree) {
  2615. // Append 2 GOPs starting at 300ms, 30ms apart.
  2616. NewCodedFrameGroupAppend("300K 330 360 390K 420 450");
  2617. // Append 2 GOPs starting at 600ms, 30ms apart.
  2618. NewCodedFrameGroupAppend("600K 630 660 690K 720 750");
  2619. // Append 2 GOPs starting at 900ms, 30ms apart.
  2620. NewCodedFrameGroupAppend("900K 930 960 990K 1020 1050");
  2621. CheckExpectedRangesByTimestamp("{ [300,480) [600,780) [900,1080) }");
  2622. int remove_range_end = -1;
  2623. int bytes_removed = -1;
  2624. // Size 0.
  2625. bytes_removed = GetRemovalRangeInMs(300, 1080, 0, &remove_range_end);
  2626. EXPECT_EQ(-1, remove_range_end);
  2627. EXPECT_EQ(0, bytes_removed);
  2628. // Smaller than the size of GOP.
  2629. bytes_removed = GetRemovalRangeInMs(300, 1080, 1, &remove_range_end);
  2630. EXPECT_EQ(390, remove_range_end);
  2631. // Remove as the size of GOP.
  2632. EXPECT_EQ(3, bytes_removed);
  2633. // The same size with a GOP.
  2634. bytes_removed = GetRemovalRangeInMs(300, 1080, 3, &remove_range_end);
  2635. EXPECT_EQ(390, remove_range_end);
  2636. EXPECT_EQ(3, bytes_removed);
  2637. // The same size with a range.
  2638. bytes_removed = GetRemovalRangeInMs(300, 1080, 6, &remove_range_end);
  2639. EXPECT_EQ(480, remove_range_end);
  2640. EXPECT_EQ(6, bytes_removed);
  2641. // A frame larger than a range.
  2642. bytes_removed = GetRemovalRangeInMs(300, 1080, 7, &remove_range_end);
  2643. EXPECT_EQ(690, remove_range_end);
  2644. EXPECT_EQ(9, bytes_removed);
  2645. // The same size with two ranges.
  2646. bytes_removed = GetRemovalRangeInMs(300, 1080, 12, &remove_range_end);
  2647. EXPECT_EQ(780, remove_range_end);
  2648. EXPECT_EQ(12, bytes_removed);
  2649. // Larger than two ranges.
  2650. bytes_removed = GetRemovalRangeInMs(300, 1080, 14, &remove_range_end);
  2651. EXPECT_EQ(990, remove_range_end);
  2652. EXPECT_EQ(15, bytes_removed);
  2653. // The same size with the whole ranges.
  2654. bytes_removed = GetRemovalRangeInMs(300, 1080, 18, &remove_range_end);
  2655. EXPECT_EQ(1080, remove_range_end);
  2656. EXPECT_EQ(18, bytes_removed);
  2657. // Larger than the whole ranges.
  2658. bytes_removed = GetRemovalRangeInMs(300, 1080, 20, &remove_range_end);
  2659. EXPECT_EQ(1080, remove_range_end);
  2660. EXPECT_EQ(18, bytes_removed);
  2661. }
  2662. TEST_F(SourceBufferStreamTest, GetRemovalRange_Range) {
  2663. // Append 2 GOPs starting at 300ms, 30ms apart.
  2664. NewCodedFrameGroupAppend("300K 330 360 390K 420 450");
  2665. // Append 2 GOPs starting at 600ms, 30ms apart.
  2666. NewCodedFrameGroupAppend("600K 630 660 690K 720 750");
  2667. // Append 2 GOPs starting at 900ms, 30ms apart.
  2668. NewCodedFrameGroupAppend("900K 930 960 990K 1020 1050");
  2669. CheckExpectedRangesByTimestamp("{ [300,480) [600,780) [900,1080) }");
  2670. int remove_range_end = -1;
  2671. int bytes_removed = -1;
  2672. // Within a GOP and no keyframe.
  2673. bytes_removed = GetRemovalRangeInMs(630, 660, 20, &remove_range_end);
  2674. EXPECT_EQ(-1, remove_range_end);
  2675. EXPECT_EQ(0, bytes_removed);
  2676. // Across a GOP and no keyframe.
  2677. bytes_removed = GetRemovalRangeInMs(630, 750, 20, &remove_range_end);
  2678. EXPECT_EQ(-1, remove_range_end);
  2679. EXPECT_EQ(0, bytes_removed);
  2680. // The same size with a range.
  2681. bytes_removed = GetRemovalRangeInMs(600, 780, 20, &remove_range_end);
  2682. EXPECT_EQ(780, remove_range_end);
  2683. EXPECT_EQ(6, bytes_removed);
  2684. // One frame larger than a range.
  2685. bytes_removed = GetRemovalRangeInMs(570, 810, 20, &remove_range_end);
  2686. EXPECT_EQ(780, remove_range_end);
  2687. EXPECT_EQ(6, bytes_removed);
  2688. // Facing the other ranges.
  2689. bytes_removed = GetRemovalRangeInMs(480, 900, 20, &remove_range_end);
  2690. EXPECT_EQ(780, remove_range_end);
  2691. EXPECT_EQ(6, bytes_removed);
  2692. // In the midle of the other ranges, but not including any GOP.
  2693. bytes_removed = GetRemovalRangeInMs(420, 960, 20, &remove_range_end);
  2694. EXPECT_EQ(780, remove_range_end);
  2695. EXPECT_EQ(6, bytes_removed);
  2696. // In the middle of the other ranges.
  2697. bytes_removed = GetRemovalRangeInMs(390, 990, 20, &remove_range_end);
  2698. EXPECT_EQ(990, remove_range_end);
  2699. EXPECT_EQ(12, bytes_removed);
  2700. // A frame smaller than the whole ranges.
  2701. bytes_removed = GetRemovalRangeInMs(330, 1050, 20, &remove_range_end);
  2702. EXPECT_EQ(990, remove_range_end);
  2703. EXPECT_EQ(12, bytes_removed);
  2704. // The same with the whole ranges.
  2705. bytes_removed = GetRemovalRangeInMs(300, 1080, 20, &remove_range_end);
  2706. EXPECT_EQ(1080, remove_range_end);
  2707. EXPECT_EQ(18, bytes_removed);
  2708. // Larger than the whole ranges.
  2709. bytes_removed = GetRemovalRangeInMs(270, 1110, 20, &remove_range_end);
  2710. EXPECT_EQ(1080, remove_range_end);
  2711. EXPECT_EQ(18, bytes_removed);
  2712. }
  2713. TEST_F(SourceBufferStreamTest, ConfigChange_Basic) {
  2714. VideoDecoderConfig new_config = TestVideoConfig::Large();
  2715. ASSERT_FALSE(new_config.Matches(video_config_));
  2716. Seek(0);
  2717. CheckVideoConfig(video_config_);
  2718. // Append 5 buffers at positions 0 through 4
  2719. NewCodedFrameGroupAppend(0, 5, &kDataA);
  2720. CheckVideoConfig(video_config_);
  2721. // Signal a config change.
  2722. stream_->UpdateVideoConfig(new_config, false);
  2723. // Make sure updating the config doesn't change anything since new_config
  2724. // should not be associated with the buffer GetNextBuffer() will return.
  2725. CheckVideoConfig(video_config_);
  2726. // Append 5 buffers at positions 5 through 9.
  2727. NewCodedFrameGroupAppend(5, 5, &kDataB);
  2728. // Consume the buffers associated with the initial config.
  2729. scoped_refptr<StreamParserBuffer> buffer;
  2730. for (int i = 0; i < 5; i++) {
  2731. EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&buffer));
  2732. CheckVideoConfig(video_config_);
  2733. }
  2734. // Verify the next attempt to get a buffer will signal that a config change
  2735. // has happened.
  2736. EXPECT_STATUS_FOR_STREAM_OP(kConfigChange, GetNextBuffer(&buffer));
  2737. // Verify that the new config is now returned.
  2738. CheckVideoConfig(new_config);
  2739. // Consume the remaining buffers associated with the new config.
  2740. for (int i = 0; i < 5; i++) {
  2741. CheckVideoConfig(new_config);
  2742. EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&buffer));
  2743. }
  2744. }
  2745. TEST_F(SourceBufferStreamTest, ConfigChange_Seek) {
  2746. scoped_refptr<StreamParserBuffer> buffer;
  2747. VideoDecoderConfig new_config = TestVideoConfig::Large();
  2748. Seek(0);
  2749. NewCodedFrameGroupAppend(0, 5, &kDataA);
  2750. stream_->UpdateVideoConfig(new_config, false);
  2751. NewCodedFrameGroupAppend(5, 5, &kDataB);
  2752. // Seek to the start of the buffers with the new config and make sure a
  2753. // config change is signalled.
  2754. CheckVideoConfig(video_config_);
  2755. Seek(5);
  2756. CheckVideoConfig(video_config_);
  2757. EXPECT_STATUS_FOR_STREAM_OP(kConfigChange, GetNextBuffer(&buffer));
  2758. CheckVideoConfig(new_config);
  2759. CheckExpectedBuffers(5, 9, &kDataB);
  2760. // Seek to the start which has a different config. Don't fetch any buffers and
  2761. // seek back to buffers with the current config. Make sure a config change
  2762. // isn't signalled in this case.
  2763. CheckVideoConfig(new_config);
  2764. Seek(0);
  2765. Seek(7);
  2766. CheckExpectedBuffers(5, 9, &kDataB);
  2767. // Seek to the start and make sure a config change is signalled.
  2768. CheckVideoConfig(new_config);
  2769. Seek(0);
  2770. CheckVideoConfig(new_config);
  2771. EXPECT_STATUS_FOR_STREAM_OP(kConfigChange, GetNextBuffer(&buffer));
  2772. CheckVideoConfig(video_config_);
  2773. CheckExpectedBuffers(0, 4, &kDataA);
  2774. }
  2775. TEST_F(SourceBufferStreamTest, SetExplicitDuration) {
  2776. // Append 3 discontinuous partial GOPs.
  2777. NewCodedFrameGroupAppend("50K 90|60");
  2778. NewCodedFrameGroupAppend("150K 190|160");
  2779. NewCodedFrameGroupAppend("250K 290|260");
  2780. CheckExpectedRangesByTimestamp("{ [50,100) [150,200) [250,300) }");
  2781. // Set duration to be 80ms. Truncates the buffered data after 80ms.
  2782. stream_->OnSetDuration(base::Milliseconds(80));
  2783. // The simulated P-frame at PTS 90ms should have been
  2784. // removed by the duration truncation. Only the frame at PTS 50ms should
  2785. // remain.
  2786. CheckExpectedRangesByTimestamp("{ [50,60) }");
  2787. // Adding data past the previous duration should still work.
  2788. NewCodedFrameGroupAppend("0D50K 50 100K");
  2789. CheckExpectedRangesByTimestamp("{ [0,150) }");
  2790. }
  2791. TEST_F(SourceBufferStreamTest, SetExplicitDuration_EdgeCase) {
  2792. // Append 10 buffers at positions 10 through 19.
  2793. NewCodedFrameGroupAppend(10, 10);
  2794. // Append 5 buffers at positions 25 through 29.
  2795. NewCodedFrameGroupAppend(25, 5);
  2796. // Check expected ranges.
  2797. CheckExpectedRanges("{ [10,19) [25,29) }");
  2798. // Set duration to be right before buffer 25.
  2799. stream_->OnSetDuration(frame_duration() * 25);
  2800. // Should truncate the last range.
  2801. CheckExpectedRanges("{ [10,19) }");
  2802. }
  2803. TEST_F(SourceBufferStreamTest, SetExplicitDuration_EdgeCase2) {
  2804. // This test requires specific relative proportions for fudge room, append
  2805. // size, and duration truncation amounts. See details at:
  2806. // https://codereview.chromium.org/2385423002
  2807. // Append buffers with first buffer establishing max_inter_buffer_distance
  2808. // of 5 ms. This translates to a fudge room (2 x max_interbuffer_distance) of
  2809. // 10 ms.
  2810. NewCodedFrameGroupAppend("0K 5K 9D4K");
  2811. CheckExpectedRangesByTimestamp("{ [0,13) }");
  2812. // Trim off last 2 buffers, totaling 8 ms. Notably less than the current fudge
  2813. // room of 10 ms.
  2814. stream_->OnSetDuration(base::Milliseconds(5));
  2815. // Verify truncation.
  2816. CheckExpectedRangesByTimestamp("{ [0,5) }");
  2817. // Append new buffers just beyond the fudge-room allowance of 10ms.
  2818. AppendBuffers("11K 15K");
  2819. // Verify new append creates a gap.
  2820. CheckExpectedRangesByTimestamp("{ [0,5) [11,19) }");
  2821. }
  2822. TEST_F(SourceBufferStreamTest, RemoveWithinFudgeRoom) {
  2823. // This test requires specific relative proportions for fudge room, append
  2824. // size, and removal amounts. See details at:
  2825. // https://codereview.chromium.org/2385423002
  2826. // Append buffers with first buffer establishing max_inter_buffer_distance
  2827. // of 5 ms. This translates to a fudge room (2 x max_interbuffer_distance) of
  2828. // 10 ms.
  2829. NewCodedFrameGroupAppend("0K 5K 9D4K");
  2830. CheckExpectedRangesByTimestamp("{ [0,13) }");
  2831. // Trim off last 2 buffers, totaling 8 ms. Notably less than the current fudge
  2832. // room of 10 ms.
  2833. RemoveInMs(5, 13, 13);
  2834. // Verify removal.
  2835. CheckExpectedRangesByTimestamp("{ [0,5) }");
  2836. // Append new buffers just beyond the fudge-room allowance of 10ms.
  2837. AppendBuffers("11K 15K");
  2838. // Verify new append creates a gap.
  2839. CheckExpectedRangesByTimestamp("{ [0,5) [11,19) }");
  2840. }
  2841. TEST_F(SourceBufferStreamTest, SetExplicitDuration_DeletePartialRange) {
  2842. // Append IPBBB GOPs into 3 discontinuous ranges.
  2843. NewCodedFrameGroupAppend("0K 40|10 10|20 20|30 30|40");
  2844. NewCodedFrameGroupAppend(
  2845. "100K 140|110 110|120 120|130 130|140 "
  2846. "150K 190|160 160|170 170|180 180|190");
  2847. NewCodedFrameGroupAppend("250K 290|260 260|270 270|280 280|290");
  2848. // Check expected ranges.
  2849. CheckExpectedRangesByTimestamp("{ [0,50) [100,200) [250,300) }");
  2850. stream_->OnSetDuration(base::Milliseconds(140));
  2851. // The B-frames at PTS 110-130 were in the GOP in decode order after
  2852. // the simulated P-frame at PTS 140 which was truncated, so those B-frames
  2853. // are also removed.
  2854. CheckExpectedRangesByTimestamp("{ [0,50) [100,110) }");
  2855. }
  2856. TEST_F(SourceBufferStreamTest, SetExplicitDuration_DeleteSelectedRange) {
  2857. // Append 3 discontinuous partial GOPs.
  2858. NewCodedFrameGroupAppend("50K 90|60");
  2859. NewCodedFrameGroupAppend("150K 190|160");
  2860. NewCodedFrameGroupAppend("250K 290|260");
  2861. CheckExpectedRangesByTimestamp("{ [50,100) [150,200) [250,300) }");
  2862. SeekToTimestampMs(150);
  2863. // Set duration to 50ms.
  2864. stream_->OnSetDuration(base::Milliseconds(50));
  2865. // Expect everything to be deleted, and should not have next buffer anymore.
  2866. CheckNoNextBuffer();
  2867. CheckExpectedRangesByTimestamp("{ }");
  2868. // Appending data 0ms through 250ms should not fulfill the seek.
  2869. // (If the duration is set to be something smaller than the current seek
  2870. // point, which had been 150ms, then the seek point is reset and the
  2871. // SourceBufferStream waits for a new seek request. Therefore even if the data
  2872. // is re-appended, it should not fulfill the old seek.)
  2873. NewCodedFrameGroupAppend("0K 50K 100K 150K 200K");
  2874. CheckNoNextBuffer();
  2875. CheckExpectedRangesByTimestamp("{ [0,250) }");
  2876. }
  2877. TEST_F(SourceBufferStreamTest, SetExplicitDuration_DeletePartialSelectedRange) {
  2878. // Append 5 buffers at positions 0 through 4.
  2879. NewCodedFrameGroupAppend(0, 5);
  2880. // Append 20 buffers at positions 10 through 29.
  2881. NewCodedFrameGroupAppend(10, 20);
  2882. // Check expected ranges.
  2883. CheckExpectedRanges("{ [0,4) [10,29) }");
  2884. // Seek to position 10.
  2885. Seek(10);
  2886. // Set duration to be between buffers 24 and 25.
  2887. stream_->OnSetDuration(frame_duration() * 25);
  2888. // Should truncate the data after 24.
  2889. CheckExpectedRanges("{ [0,4) [10,24) }");
  2890. // The seek position should not be lost.
  2891. CheckExpectedBuffers(10, 10);
  2892. // Now set the duration immediately after buffer 10.
  2893. stream_->OnSetDuration(frame_duration() * 11);
  2894. // Seek position should be reset.
  2895. CheckNoNextBuffer();
  2896. CheckExpectedRanges("{ [0,4) [10,10) }");
  2897. }
  2898. // Test the case where duration is set while the stream parser buffers
  2899. // already start passing the data to decoding pipeline. Selected range,
  2900. // when invalidated by getting truncated, should be updated to NULL
  2901. // accordingly so that successive append operations keep working.
  2902. TEST_F(SourceBufferStreamTest, SetExplicitDuration_UpdateSelectedRange) {
  2903. // Seek to start of stream.
  2904. SeekToTimestampMs(0);
  2905. NewCodedFrameGroupAppend("0K 30 60 90");
  2906. // Read out the first few buffers.
  2907. CheckExpectedBuffers("0K 30");
  2908. // Set duration to be right before buffer 1.
  2909. stream_->OnSetDuration(base::Milliseconds(60));
  2910. // Verify that there is no next buffer.
  2911. CheckNoNextBuffer();
  2912. // We should be able to append new buffers at this point.
  2913. NewCodedFrameGroupAppend("120K 150");
  2914. CheckExpectedRangesByTimestamp("{ [0,60) [120,180) }");
  2915. }
  2916. TEST_F(SourceBufferStreamTest,
  2917. SetExplicitDuration_AfterGroupTimestampAndBeforeFirstBufferTimestamp) {
  2918. NewCodedFrameGroupAppend("0K 30K 60K");
  2919. // Append a coded frame group with a start timestamp of 200, but the first
  2920. // buffer starts at 230ms. This can happen in muxed content where the
  2921. // audio starts before the first frame.
  2922. NewCodedFrameGroupAppend(base::Milliseconds(200), "230K 260K 290K 320K");
  2923. NewCodedFrameGroupAppend("400K 430K 460K");
  2924. CheckExpectedRangesByTimestamp("{ [0,90) [200,350) [400,490) }");
  2925. stream_->OnSetDuration(base::Milliseconds(120));
  2926. // Verify that the buffered ranges are updated properly and we don't crash.
  2927. CheckExpectedRangesByTimestamp("{ [0,90) }");
  2928. }
  2929. TEST_F(SourceBufferStreamTest, SetExplicitDuration_MarkEOS) {
  2930. // Append 1 full and 1 partial GOP: IPBBBIPBB
  2931. NewCodedFrameGroupAppend(
  2932. "0K 40|10 10|20 20|30 30|40 "
  2933. "50K 90|60 60|70 70|80");
  2934. CheckExpectedRangesByTimestamp("{ [0,100) }");
  2935. SeekToTimestampMs(50);
  2936. // Set duration to be before the seeked to position.
  2937. // This will result in truncation of the selected range and a reset
  2938. // of NextBufferPosition.
  2939. stream_->OnSetDuration(base::Milliseconds(40));
  2940. // The P-frame at PTS 40ms was removed, so its dependent B-frames at PTS 10-30
  2941. // were also removed.
  2942. CheckExpectedRangesByTimestamp("{ [0,10) }");
  2943. // Mark EOS reached.
  2944. stream_->MarkEndOfStream();
  2945. // Expect EOS to be reached.
  2946. CheckEOSReached();
  2947. }
  2948. TEST_F(SourceBufferStreamTest, SetExplicitDuration_MarkEOS_IsSeekPending) {
  2949. // Append 1 full and 1 partial GOP: IPBBBIPBB
  2950. NewCodedFrameGroupAppend(
  2951. "0K 40|10 10|20 20|30 30|40 "
  2952. "50K 90|60 60|70 70|80");
  2953. CheckExpectedRangesByTimestamp("{ [0,100) }");
  2954. // Seek to 100ms will result in a pending seek.
  2955. SeekToTimestampMs(100);
  2956. // Set duration to be before the seeked to position.
  2957. // This will result in truncation of the selected range and a reset
  2958. // of NextBufferPosition.
  2959. stream_->OnSetDuration(base::Milliseconds(40));
  2960. // The P-frame at PTS 40ms was removed, so its dependent B-frames at PTS 10-30
  2961. // were also removed.
  2962. CheckExpectedRangesByTimestamp("{ [0,10) }");
  2963. EXPECT_TRUE(stream_->IsSeekPending());
  2964. // Mark EOS reached.
  2965. stream_->MarkEndOfStream();
  2966. EXPECT_FALSE(stream_->IsSeekPending());
  2967. }
  2968. // Test the case were the current playback position is at the end of the
  2969. // buffered data and several overlaps occur.
  2970. TEST_F(SourceBufferStreamTest, OverlapWhileWaitingForMoreData) {
  2971. // Seek to start of stream.
  2972. SeekToTimestampMs(0);
  2973. NewCodedFrameGroupAppend("0K 30 60 90 120K 150");
  2974. CheckExpectedRangesByTimestamp("{ [0,180) }");
  2975. // Read all the buffered data.
  2976. CheckExpectedBuffers("0K 30 60 90 120K 150");
  2977. CheckNoNextBuffer();
  2978. // Append data over the current GOP so that a keyframe is needed before
  2979. // playback can continue from the current position.
  2980. NewCodedFrameGroupAppend("120K 150");
  2981. CheckExpectedRangesByTimestamp("{ [0,180) }");
  2982. // Append buffers that replace the first GOP with a partial GOP.
  2983. NewCodedFrameGroupAppend("0K 30");
  2984. CheckExpectedRangesByTimestamp("{ [0,180) }");
  2985. // Append buffers that complete that partial GOP.
  2986. AppendBuffers("60 90");
  2987. CheckExpectedRangesByTimestamp("{ [0,180) }");
  2988. // Verify that we still don't have a next buffer.
  2989. CheckNoNextBuffer();
  2990. // Add more data to the end and verify that this new data is read correctly.
  2991. NewCodedFrameGroupAppend("180K 210");
  2992. CheckExpectedRangesByTimestamp("{ [0,240) }");
  2993. CheckExpectedBuffers("180K 210");
  2994. CheckNoNextBuffer();
  2995. }
  2996. // Verify that a single coded frame at the current read position unblocks the
  2997. // read even if the frame is buffered after the previously read position is
  2998. // removed.
  2999. TEST_F(SourceBufferStreamTest, AfterRemove_SingleFrameRange_Unblocks_Read) {
  3000. Seek(0);
  3001. NewCodedFrameGroupAppend("0K 30 60 90D30");
  3002. CheckExpectedRangesByTimestamp("{ [0,120) }");
  3003. CheckExpectedBuffers("0K 30 60 90");
  3004. CheckNoNextBuffer();
  3005. RemoveInMs(0, 120, 120);
  3006. CheckExpectedRangesByTimestamp("{ }");
  3007. NewCodedFrameGroupAppend("120D30K");
  3008. CheckExpectedRangesByTimestamp("{ [120,150) }");
  3009. CheckExpectedBuffers("120K");
  3010. CheckNoNextBuffer();
  3011. }
  3012. // Verify that multiple short (relative to max-inter-buffer-distance * 2) coded
  3013. // frames at the current read position unblock the read even if the frames are
  3014. // buffered after the previously read position is removed.
  3015. TEST_F(SourceBufferStreamTest, AfterRemove_TinyFrames_Unblock_Read_1) {
  3016. Seek(0);
  3017. NewCodedFrameGroupAppend("0K 30 60 90D30");
  3018. CheckExpectedRangesByTimestamp("{ [0,120) }");
  3019. CheckExpectedBuffers("0K 30 60 90");
  3020. CheckNoNextBuffer();
  3021. RemoveInMs(0, 120, 120);
  3022. CheckExpectedRangesByTimestamp("{ }");
  3023. NewCodedFrameGroupAppend("120D1K 121D1");
  3024. CheckExpectedRangesByTimestamp("{ [120,122) }");
  3025. CheckExpectedBuffers("120K 121");
  3026. CheckNoNextBuffer();
  3027. }
  3028. // Verify that multiple short (relative to max-inter-buffer-distance * 2) coded
  3029. // frames starting at the fudge room boundary unblock the read even if the
  3030. // frames are buffered after the previously read position is removed.
  3031. TEST_F(SourceBufferStreamTest, AfterRemove_TinyFrames_Unblock_Read_2) {
  3032. Seek(0);
  3033. NewCodedFrameGroupAppend("0K 30 60 90D30");
  3034. CheckExpectedRangesByTimestamp("{ [0,120) }");
  3035. CheckExpectedBuffers("0K 30 60 90");
  3036. CheckNoNextBuffer();
  3037. RemoveInMs(0, 120, 120);
  3038. CheckExpectedRangesByTimestamp("{ }");
  3039. NewCodedFrameGroupAppend("150D1K 151D1");
  3040. CheckExpectedRangesByTimestamp("{ [150,152) }");
  3041. CheckExpectedBuffers("150K 151");
  3042. CheckNoNextBuffer();
  3043. }
  3044. // Verify that coded frames starting after the fudge room boundary do not
  3045. // unblock the read when buffered after the previously read position is removed.
  3046. TEST_F(SourceBufferStreamTest, AfterRemove_BeyondFudge_Stalled) {
  3047. Seek(0);
  3048. NewCodedFrameGroupAppend("0K 30 60 90D30");
  3049. CheckExpectedRangesByTimestamp("{ [0,120) }");
  3050. CheckExpectedBuffers("0K 30 60 90");
  3051. CheckNoNextBuffer();
  3052. RemoveInMs(0, 120, 120);
  3053. CheckExpectedRangesByTimestamp("{ }");
  3054. NewCodedFrameGroupAppend("151D1K 152D1");
  3055. CheckExpectedRangesByTimestamp("{ [151,153) }");
  3056. CheckNoNextBuffer();
  3057. }
  3058. // Verify that non-keyframes with the same timestamp in the same
  3059. // append are handled correctly.
  3060. TEST_F(SourceBufferStreamTest, SameTimestamp_Video_SingleAppend) {
  3061. Seek(0);
  3062. NewCodedFrameGroupAppend("0K 30 30 60 90 120K 150");
  3063. CheckExpectedBuffers("0K 30 30 60 90 120K 150");
  3064. }
  3065. // Verify that a non-keyframe followed by a keyframe with the same timestamp
  3066. // is allowed.
  3067. TEST_F(SourceBufferStreamTest, SameTimestamp_Video_SingleAppend2) {
  3068. Seek(0);
  3069. NewCodedFrameGroupAppend("0K 30 30K 60");
  3070. CheckExpectedBuffers("0K 30 30K 60");
  3071. }
  3072. // Verify that non-keyframes with the same timestamp can occur
  3073. // in different appends.
  3074. TEST_F(SourceBufferStreamTest, SameTimestamp_Video_TwoAppends) {
  3075. Seek(0);
  3076. NewCodedFrameGroupAppend("0K 30D0");
  3077. AppendBuffers("30 60 90 120K 150");
  3078. CheckExpectedBuffers("0K 30 30 60 90 120K 150");
  3079. }
  3080. // Verify that a non-keyframe followed by a keyframe with the same timestamp
  3081. // is allowed.
  3082. TEST_F(SourceBufferStreamTest, SameTimestamp_Video_TwoAppends2) {
  3083. Seek(0);
  3084. NewCodedFrameGroupAppend("0K 30D0");
  3085. AppendBuffers("30K 60");
  3086. CheckExpectedBuffers("0K 30 30K 60");
  3087. }
  3088. // Verify that a keyframe followed by a non-keyframe with the same timestamp
  3089. // is allowed.
  3090. TEST_F(SourceBufferStreamTest, SameTimestamp_VideoKeyFrame_TwoAppends) {
  3091. Seek(0);
  3092. NewCodedFrameGroupAppend("0K 30D0K");
  3093. AppendBuffers("30 60");
  3094. CheckExpectedBuffers("0K 30K 30 60");
  3095. }
  3096. TEST_F(SourceBufferStreamTest, SameTimestamp_VideoKeyFrame_SingleAppend) {
  3097. Seek(0);
  3098. NewCodedFrameGroupAppend("0K 30K 30 60");
  3099. CheckExpectedBuffers("0K 30K 30 60");
  3100. }
  3101. TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Overlap_1) {
  3102. Seek(0);
  3103. NewCodedFrameGroupAppend("0K 30 60 60 90 120K 150");
  3104. NewCodedFrameGroupAppend("60K 91 121K 151");
  3105. CheckExpectedBuffers("0K 30 60K 91 121K 151");
  3106. }
  3107. TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Overlap_2) {
  3108. Seek(0);
  3109. NewCodedFrameGroupAppend("0K 30 60 60 90 120K 150");
  3110. NewCodedFrameGroupAppend("0K 30 61");
  3111. CheckExpectedBuffers("0K 30 61 120K 150");
  3112. }
  3113. TEST_F(SourceBufferStreamTest, SameTimestamp_Video_Overlap_3) {
  3114. Seek(0);
  3115. NewCodedFrameGroupAppend("0K 20 40 60 80 100K 101 102 103K");
  3116. NewCodedFrameGroupAppend("0K 20 40 60 80 90D0");
  3117. CheckExpectedBuffers("0K 20 40 60 80 90 100K 101 102 103K");
  3118. AppendBuffers("90 110K 150");
  3119. Seek(0);
  3120. CheckExpectedBuffers("0K 20 40 60 80 90 90 110K 150");
  3121. CheckNoNextBuffer();
  3122. CheckExpectedRangesByTimestamp("{ [0,190) }");
  3123. }
  3124. // Test all the valid same timestamp cases for audio.
  3125. TEST_F(SourceBufferStreamTest, SameTimestamp_Audio) {
  3126. AudioDecoderConfig config(AudioCodec::kMP3, kSampleFormatF32,
  3127. CHANNEL_LAYOUT_STEREO, 44100, EmptyExtraData(),
  3128. EncryptionScheme::kUnencrypted);
  3129. ResetStream<>(config);
  3130. Seek(0);
  3131. NewCodedFrameGroupAppend("0K 0K 30K 30K");
  3132. CheckExpectedBuffers("0K 0K 30K 30K");
  3133. }
  3134. // If seeking past any existing range and the seek is pending
  3135. // because no data has been provided for that position,
  3136. // the stream position can be considered as the end of stream.
  3137. TEST_F(SourceBufferStreamTest, EndSelected_During_PendingSeek) {
  3138. // Append 15 buffers at positions 0 through 14.
  3139. NewCodedFrameGroupAppend(0, 15);
  3140. Seek(20);
  3141. EXPECT_TRUE(stream_->IsSeekPending());
  3142. stream_->MarkEndOfStream();
  3143. EXPECT_FALSE(stream_->IsSeekPending());
  3144. }
  3145. // If there is a pending seek between 2 existing ranges,
  3146. // the end of the stream has not been reached.
  3147. TEST_F(SourceBufferStreamTest, EndNotSelected_During_PendingSeek) {
  3148. // Append:
  3149. // - 10 buffers at positions 0 through 9.
  3150. // - 10 buffers at positions 30 through 39
  3151. NewCodedFrameGroupAppend(0, 10);
  3152. NewCodedFrameGroupAppend(30, 10);
  3153. Seek(20);
  3154. EXPECT_TRUE(stream_->IsSeekPending());
  3155. stream_->MarkEndOfStream();
  3156. EXPECT_TRUE(stream_->IsSeekPending());
  3157. }
  3158. // Removing exact start & end of a range.
  3159. TEST_F(SourceBufferStreamTest, Remove_WholeRange1) {
  3160. Seek(0);
  3161. NewCodedFrameGroupAppend("10K 40 70K 100 130K");
  3162. CheckExpectedRangesByTimestamp("{ [10,160) }");
  3163. RemoveInMs(10, 160, 160);
  3164. CheckExpectedRangesByTimestamp("{ }");
  3165. }
  3166. // Removal range starts before range and ends exactly at end.
  3167. TEST_F(SourceBufferStreamTest, Remove_WholeRange2) {
  3168. Seek(0);
  3169. NewCodedFrameGroupAppend("10K 40 70K 100 130K");
  3170. CheckExpectedRangesByTimestamp("{ [10,160) }");
  3171. RemoveInMs(0, 160, 160);
  3172. CheckExpectedRangesByTimestamp("{ }");
  3173. }
  3174. // Removal range starts at the start of a range and ends beyond the
  3175. // range end.
  3176. TEST_F(SourceBufferStreamTest, Remove_WholeRange3) {
  3177. Seek(0);
  3178. NewCodedFrameGroupAppend("10K 40 70K 100 130K");
  3179. CheckExpectedRangesByTimestamp("{ [10,160) }");
  3180. RemoveInMs(10, 200, 200);
  3181. CheckExpectedRangesByTimestamp("{ }");
  3182. }
  3183. // Removal range starts before range start and ends after the range end.
  3184. TEST_F(SourceBufferStreamTest, Remove_WholeRange4) {
  3185. Seek(0);
  3186. NewCodedFrameGroupAppend("10K 40 70K 100 130K");
  3187. CheckExpectedRangesByTimestamp("{ [10,160) }");
  3188. RemoveInMs(0, 200, 200);
  3189. CheckExpectedRangesByTimestamp("{ }");
  3190. }
  3191. // Removes multiple ranges.
  3192. TEST_F(SourceBufferStreamTest, Remove_WholeRange5) {
  3193. Seek(0);
  3194. NewCodedFrameGroupAppend("10K 40 70K 100 130K");
  3195. NewCodedFrameGroupAppend("1000K 1030 1060K 1090 1120K");
  3196. NewCodedFrameGroupAppend("2000K 2030 2060K 2090 2120K");
  3197. CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) [2000,2150) }");
  3198. RemoveInMs(10, 3000, 3000);
  3199. CheckExpectedRangesByTimestamp("{ }");
  3200. }
  3201. // Verifies a [0-infinity) range removes everything.
  3202. TEST_F(SourceBufferStreamTest, Remove_ZeroToInfinity) {
  3203. Seek(0);
  3204. NewCodedFrameGroupAppend("10K 40 70K 100 130K");
  3205. NewCodedFrameGroupAppend("1000K 1030 1060K 1090 1120K");
  3206. NewCodedFrameGroupAppend("2000K 2030 2060K 2090 2120K");
  3207. CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) [2000,2150) }");
  3208. Remove(base::TimeDelta(), kInfiniteDuration, kInfiniteDuration);
  3209. CheckExpectedRangesByTimestamp("{ }");
  3210. }
  3211. // Removal range starts at the beginning of the range and ends in the
  3212. // middle of the range. This test verifies that full GOPs are removed.
  3213. TEST_F(SourceBufferStreamTest, Remove_Partial1) {
  3214. Seek(0);
  3215. NewCodedFrameGroupAppend("10K 40 70K 100 130K");
  3216. NewCodedFrameGroupAppend("1000K 1030 1060K 1090 1120K");
  3217. CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) }");
  3218. RemoveInMs(0, 80, 2200);
  3219. CheckExpectedRangesByTimestamp("{ [130,160) [1000,1150) }");
  3220. }
  3221. // Removal range starts in the middle of a range and ends at the exact
  3222. // end of the range.
  3223. TEST_F(SourceBufferStreamTest, Remove_Partial2) {
  3224. Seek(0);
  3225. NewCodedFrameGroupAppend("10K 40 70K 100 130K");
  3226. NewCodedFrameGroupAppend("1000K 1030 1060K 1090 1120K");
  3227. CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) }");
  3228. RemoveInMs(40, 160, 2200);
  3229. CheckExpectedRangesByTimestamp("{ [10,40) [1000,1150) }");
  3230. }
  3231. // Removal range starts and ends within a range.
  3232. TEST_F(SourceBufferStreamTest, Remove_Partial3) {
  3233. Seek(0);
  3234. NewCodedFrameGroupAppend("10K 40 70K 100 130K");
  3235. NewCodedFrameGroupAppend("1000K 1030 1060K 1090 1120K");
  3236. CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) }");
  3237. RemoveInMs(40, 120, 2200);
  3238. CheckExpectedRangesByTimestamp("{ [10,40) [130,160) [1000,1150) }");
  3239. }
  3240. // Removal range starts in the middle of one range and ends in the
  3241. // middle of another range.
  3242. TEST_F(SourceBufferStreamTest, Remove_Partial4) {
  3243. Seek(0);
  3244. NewCodedFrameGroupAppend("10K 40 70K 100 130K");
  3245. NewCodedFrameGroupAppend("1000K 1030 1060K 1090 1120K");
  3246. NewCodedFrameGroupAppend("2000K 2030 2060K 2090 2120K");
  3247. CheckExpectedRangesByTimestamp("{ [10,160) [1000,1150) [2000,2150) }");
  3248. RemoveInMs(40, 2030, 2200);
  3249. CheckExpectedRangesByTimestamp("{ [10,40) [2060,2150) }");
  3250. }
  3251. // Test behavior when the current position is removed and new buffers
  3252. // are appended over the removal range.
  3253. TEST_F(SourceBufferStreamTest, Remove_CurrentPosition) {
  3254. Seek(0);
  3255. NewCodedFrameGroupAppend("0K 30 60 90K 120 150 180K 210 240 270K 300 330");
  3256. CheckExpectedRangesByTimestamp("{ [0,360) }");
  3257. CheckExpectedBuffers("0K 30 60 90K 120");
  3258. // Remove a range that includes the next buffer (i.e., 150).
  3259. RemoveInMs(150, 210, 360);
  3260. CheckExpectedRangesByTimestamp("{ [0,150) [270,360) }");
  3261. // Verify that no next buffer is returned.
  3262. CheckNoNextBuffer();
  3263. // Append some buffers to fill the gap that was created.
  3264. NewCodedFrameGroupAppend("120K 150 180 210K 240");
  3265. CheckExpectedRangesByTimestamp("{ [0,360) }");
  3266. // Verify that buffers resume at the next keyframe after the
  3267. // current position.
  3268. CheckExpectedBuffers("210K 240 270K 300 330");
  3269. }
  3270. // Test behavior when buffers in the selected range before the current position
  3271. // are removed.
  3272. TEST_F(SourceBufferStreamTest, Remove_BeforeCurrentPosition) {
  3273. Seek(0);
  3274. NewCodedFrameGroupAppend("0K 30 60 90K 120 150 180K 210 240 270K 300 330");
  3275. CheckExpectedRangesByTimestamp("{ [0,360) }");
  3276. CheckExpectedBuffers("0K 30 60 90K 120");
  3277. // Remove a range that is before the current playback position.
  3278. RemoveInMs(0, 90, 360);
  3279. CheckExpectedRangesByTimestamp("{ [90,360) }");
  3280. CheckExpectedBuffers("150 180K 210 240 270K 300 330");
  3281. }
  3282. // Test removing the preliminary portion for the current coded frame group being
  3283. // appended.
  3284. TEST_F(SourceBufferStreamTest, Remove_MidGroup) {
  3285. Seek(0);
  3286. NewCodedFrameGroupAppend("0K 30 60 90 120K 150 180 210");
  3287. CheckExpectedRangesByTimestamp("{ [0,240) }");
  3288. // Partially replace the first GOP, then read its keyframe.
  3289. NewCodedFrameGroupAppend("0K 30");
  3290. CheckExpectedBuffers("0K");
  3291. CheckExpectedRangesByTimestamp("{ [0,240) }");
  3292. // Remove the partial GOP that we're in the middle of reading.
  3293. RemoveInMs(0, 60, 240);
  3294. // Verify that there is no next buffer since it was removed and the remaining
  3295. // buffered range is beyond the current position.
  3296. CheckNoNextBuffer();
  3297. CheckExpectedRangesByTimestamp("{ [120,240) }");
  3298. // Continue appending frames for the current GOP.
  3299. AppendBuffers("60 90");
  3300. // Verify that the non-keyframes are not added.
  3301. CheckExpectedRangesByTimestamp("{ [120,240) }");
  3302. // Finish the previous GOP and start the next one.
  3303. AppendBuffers("120 150K 180");
  3304. // Verify that new GOP replaces the existing GOP.
  3305. CheckExpectedRangesByTimestamp("{ [150,210) }");
  3306. SeekToTimestampMs(150);
  3307. CheckExpectedBuffers("150K 180");
  3308. CheckNoNextBuffer();
  3309. }
  3310. // Test removing the current GOP being appended, while not removing
  3311. // the entire range the GOP belongs to.
  3312. TEST_F(SourceBufferStreamTest, Remove_GOPBeingAppended) {
  3313. Seek(0);
  3314. NewCodedFrameGroupAppend("0K 30 60 90 120K 150 180");
  3315. CheckExpectedRangesByTimestamp("{ [0,210) }");
  3316. // Remove the current GOP being appended.
  3317. RemoveInMs(120, 150, 240);
  3318. CheckExpectedRangesByTimestamp("{ [0,120) }");
  3319. // Continue appending the current GOP and the next one.
  3320. AppendBuffers("210 240K 270 300");
  3321. // Verify that the non-keyframe in the previous GOP does
  3322. // not effect any existing ranges and a new range is started at the
  3323. // beginning of the next GOP.
  3324. CheckExpectedRangesByTimestamp("{ [0,120) [240,330) }");
  3325. // Verify the buffers in the ranges.
  3326. CheckExpectedBuffers("0K 30 60 90");
  3327. CheckNoNextBuffer();
  3328. SeekToTimestampMs(240);
  3329. CheckExpectedBuffers("240K 270 300");
  3330. }
  3331. TEST_F(SourceBufferStreamTest, Remove_WholeGOPBeingAppended) {
  3332. SeekToTimestampMs(1000);
  3333. NewCodedFrameGroupAppend("1000K 1030 1060 1090");
  3334. CheckExpectedRangesByTimestamp("{ [1000,1120) }");
  3335. // Remove the keyframe of the current GOP being appended.
  3336. RemoveInMs(1000, 1030, 1120);
  3337. CheckExpectedRangesByTimestamp("{ }");
  3338. // Continue appending the current GOP.
  3339. AppendBuffers("1210 1240");
  3340. CheckExpectedRangesByTimestamp("{ }");
  3341. // Append the beginning of the next GOP.
  3342. AppendBuffers("1270K 1300");
  3343. // Verify that the new range is started at the
  3344. // beginning of the next GOP.
  3345. CheckExpectedRangesByTimestamp("{ [1270,1330) }");
  3346. // Verify the buffers in the ranges.
  3347. CheckNoNextBuffer();
  3348. SeekToTimestampMs(1270);
  3349. CheckExpectedBuffers("1270K 1300");
  3350. }
  3351. TEST_F(SourceBufferStreamTest,
  3352. Remove_PreviousAppendDestroyedAndOverwriteExistingRange) {
  3353. SeekToTimestampMs(90);
  3354. NewCodedFrameGroupAppend("90K 120 150");
  3355. CheckExpectedRangesByTimestamp("{ [90,180) }");
  3356. // Append a coded frame group before the previously appended data.
  3357. NewCodedFrameGroupAppend("0K 30 60");
  3358. // Verify that the ranges get merged.
  3359. CheckExpectedRangesByTimestamp("{ [0,180) }");
  3360. // Remove the data from the last append.
  3361. RemoveInMs(0, 90, 360);
  3362. CheckExpectedRangesByTimestamp("{ [90,180) }");
  3363. // Append a new coded frame group that follows the removed group and
  3364. // starts at the beginning of the range left over from the
  3365. // remove.
  3366. NewCodedFrameGroupAppend("90K 121 151");
  3367. CheckExpectedBuffers("90K 121 151");
  3368. }
  3369. TEST_F(SourceBufferStreamTest, Remove_GapAtBeginningOfGroup) {
  3370. Seek(0);
  3371. // Append a coded frame group that has a gap at the beginning of it.
  3372. NewCodedFrameGroupAppend(base::Milliseconds(0), "30K 60 90 120K 150");
  3373. CheckExpectedRangesByTimestamp("{ [0,180) }");
  3374. // Remove the gap that doesn't contain any buffers.
  3375. RemoveInMs(0, 10, 180);
  3376. CheckExpectedRangesByTimestamp("{ [10,180) }");
  3377. // Verify we still get the first buffer still since only part of
  3378. // the gap was removed.
  3379. // TODO(acolwell/wolenetz): Consider not returning a buffer at this
  3380. // point since the current seek position has been explicitly
  3381. // removed but didn't happen to remove any buffers.
  3382. // http://crbug.com/384016
  3383. CheckExpectedBuffers("30K");
  3384. // Remove a range that includes the first GOP.
  3385. RemoveInMs(0, 60, 180);
  3386. // Verify that no buffer is returned because the current buffer
  3387. // position has been removed.
  3388. CheckNoNextBuffer();
  3389. CheckExpectedRangesByTimestamp("{ [120,180) }");
  3390. }
  3391. TEST_F(SourceBufferStreamTest,
  3392. OverlappingAppendRangeMembership_OneMicrosecond_Video) {
  3393. NewCodedFrameGroupAppend("10D20K");
  3394. CheckExpectedRangesByTimestamp("{ [10000,30000) }",
  3395. TimeGranularity::kMicrosecond);
  3396. // Append a buffer 1 microsecond earlier, with estimated duration.
  3397. NewCodedFrameGroupAppend("9999uD20EK");
  3398. CheckExpectedRangesByTimestamp("{ [9999,30000) }",
  3399. TimeGranularity::kMicrosecond);
  3400. // Append that same buffer again, but without any discontinuity signalled / no
  3401. // new coded frame group.
  3402. AppendBuffers("9999uD20EK");
  3403. CheckExpectedRangesByTimestamp("{ [9999,30000) }",
  3404. TimeGranularity::kMicrosecond);
  3405. Seek(0);
  3406. CheckExpectedBuffers("9999K 9999K 10000K", TimeGranularity::kMicrosecond);
  3407. CheckNoNextBuffer();
  3408. }
  3409. TEST_F(SourceBufferStreamTest,
  3410. OverlappingAppendRangeMembership_TwoMicroseconds_Video) {
  3411. NewCodedFrameGroupAppend("10D20K");
  3412. CheckExpectedRangesByTimestamp("{ [10000,30000) }",
  3413. TimeGranularity::kMicrosecond);
  3414. // Append an exactly abutting buffer 2us earlier.
  3415. NewCodedFrameGroupAppend("9998uD20EK");
  3416. CheckExpectedRangesByTimestamp("{ [9998,30000) }",
  3417. TimeGranularity::kMicrosecond);
  3418. // Append that same buffer again, but without any discontinuity signalled / no
  3419. // new coded frame group.
  3420. AppendBuffers("9998uD20EK");
  3421. CheckExpectedRangesByTimestamp("{ [9998,30000) }",
  3422. TimeGranularity::kMicrosecond);
  3423. Seek(0);
  3424. CheckExpectedBuffers("9998K 9998K 10000K", TimeGranularity::kMicrosecond);
  3425. CheckNoNextBuffer();
  3426. }
  3427. TEST_F(SourceBufferStreamTest, Text_Append_SingleRange) {
  3428. SetTextStream();
  3429. NewCodedFrameGroupAppend("0K 500K 1000K");
  3430. CheckExpectedRangesByTimestamp("{ [0,1500) }");
  3431. Seek(0);
  3432. CheckExpectedBuffers("0K 500K 1000K");
  3433. }
  3434. TEST_F(SourceBufferStreamTest, Text_Append_DisjointAfter) {
  3435. SetTextStream();
  3436. NewCodedFrameGroupAppend("0K 500K 1000K");
  3437. CheckExpectedRangesByTimestamp("{ [0,1500) }");
  3438. NewCodedFrameGroupAppend("3000K 3500K 4000K");
  3439. CheckExpectedRangesByTimestamp("{ [0,4500) }");
  3440. Seek(0);
  3441. CheckExpectedBuffers("0K 500K 1000K 3000K 3500K 4000K");
  3442. }
  3443. TEST_F(SourceBufferStreamTest, Text_Append_DisjointBefore) {
  3444. SetTextStream();
  3445. NewCodedFrameGroupAppend("3000K 3500K 4000K");
  3446. CheckExpectedRangesByTimestamp("{ [3000,4500) }");
  3447. NewCodedFrameGroupAppend("0K 500K 1000K");
  3448. CheckExpectedRangesByTimestamp("{ [0,4500) }");
  3449. Seek(0);
  3450. CheckExpectedBuffers("0K 500K 1000K 3000K 3500K 4000K");
  3451. }
  3452. TEST_F(SourceBufferStreamTest, Text_CompleteOverlap) {
  3453. SetTextStream();
  3454. NewCodedFrameGroupAppend("3000K 3500K 4000K");
  3455. CheckExpectedRangesByTimestamp("{ [3000,4500) }");
  3456. NewCodedFrameGroupAppend(
  3457. "0K 501K 1001K 1501K 2001K 2501K "
  3458. "3001K 3501K 4001K 4501K 5001K");
  3459. CheckExpectedRangesByTimestamp("{ [0,5501) }");
  3460. Seek(0);
  3461. CheckExpectedBuffers("0K 501K 1001K 1501K 2001K 2501K "
  3462. "3001K 3501K 4001K 4501K 5001K");
  3463. }
  3464. TEST_F(SourceBufferStreamTest, Text_OverlapAfter) {
  3465. SetTextStream();
  3466. NewCodedFrameGroupAppend("0K 500K 1000K 1500K 2000K");
  3467. CheckExpectedRangesByTimestamp("{ [0,2500) }");
  3468. NewCodedFrameGroupAppend("1499K 2001K 2501K 3001K");
  3469. CheckExpectedRangesByTimestamp("{ [0,3501) }");
  3470. Seek(0);
  3471. CheckExpectedBuffers("0K 500K 1000K 1499K 2001K 2501K 3001K");
  3472. }
  3473. TEST_F(SourceBufferStreamTest, Text_OverlapBefore) {
  3474. SetTextStream();
  3475. NewCodedFrameGroupAppend("1500K 2000K 2500K 3000K 3500K");
  3476. CheckExpectedRangesByTimestamp("{ [1500,4000) }");
  3477. NewCodedFrameGroupAppend("0K 501K 1001K 1501K 2001K");
  3478. CheckExpectedRangesByTimestamp("{ [0,4000) }");
  3479. Seek(0);
  3480. CheckExpectedBuffers("0K 501K 1001K 1501K 2001K 3000K 3500K");
  3481. }
  3482. TEST_F(SourceBufferStreamTest, Audio_SpliceTrimmingForOverlap) {
  3483. SetAudioStream();
  3484. Seek(0);
  3485. NewCodedFrameGroupAppend("0K 2K 4K 6K 8K 10K 12K");
  3486. CheckExpectedRangesByTimestamp("{ [0,14) }");
  3487. // Note that duration of frame at time 10 is verified to be 2 ms.
  3488. CheckExpectedBuffers("0K 2K 4K 6K 8K 10D2K 12K");
  3489. CheckNoNextBuffer();
  3490. // Append new group with front slightly overlapping existing buffer at 10ms.
  3491. EXPECT_MEDIA_LOG(TrimmedSpliceOverlap(11000, 10000, 1000));
  3492. NewCodedFrameGroupAppend("11K 13K 15K 17K");
  3493. // Cross-fade splicing is no longer implemented. Instead we should expect
  3494. // wholly overlapped buffers to be removed (12K). If a buffer is partially
  3495. // overlapped (e.g. last millisecond of 10K), the existing buffer should be
  3496. // trimmed to perfectly abut the newly appended buffers.
  3497. Seek(0);
  3498. CheckExpectedRangesByTimestamp("{ [0,19) }");
  3499. CheckExpectedBuffers("0K 2K 4K 6K 8K 10D1K 11D2K 13K 15K 17K");
  3500. CheckNoNextBuffer();
  3501. }
  3502. // Test that a splice is not created if an end timestamp and start timestamp
  3503. // perfectly overlap.
  3504. TEST_F(SourceBufferStreamTest, Audio_SpliceFrame_NoSplice) {
  3505. SetAudioStream();
  3506. Seek(0);
  3507. // Add 10 frames across 2 *non-overlapping* appends.
  3508. NewCodedFrameGroupAppend("0K 2K 4K 6K 8K 10K");
  3509. NewCodedFrameGroupAppend("12K 14K 16K 18K");
  3510. // Manually inspect the buffers at the no-splice boundary to verify duration
  3511. // and lack of discard padding (set when splicing).
  3512. scoped_refptr<StreamParserBuffer> buffer;
  3513. const DecoderBuffer::DiscardPadding kEmptyDiscardPadding;
  3514. for (int i = 0; i < 10; i++) {
  3515. // Verify buffer timestamps and durations are preserved and no buffers have
  3516. // discard padding (indicating no splice trimming).
  3517. EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&buffer));
  3518. EXPECT_EQ(base::Milliseconds(i * 2), buffer->timestamp());
  3519. EXPECT_EQ(base::Milliseconds(2), buffer->duration());
  3520. EXPECT_EQ(kEmptyDiscardPadding, buffer->discard_padding());
  3521. }
  3522. CheckNoNextBuffer();
  3523. }
  3524. TEST_F(SourceBufferStreamTest, Audio_NoSpliceForBadOverlap) {
  3525. SetAudioStream();
  3526. Seek(0);
  3527. // Add 2 frames with matching PTS and ov, where the duration of the first
  3528. // frame suggests that it overlaps the second frame. The overlap is within a
  3529. // coded frame group (bad content), so no splicing is expected.
  3530. NewCodedFrameGroupAppend("0D10K 0D10K");
  3531. CheckExpectedRangesByTimestamp("{ [0,10) }");
  3532. CheckExpectedBuffers("0D10K 0D10K");
  3533. CheckNoNextBuffer();
  3534. Seek(0);
  3535. // Add a new frame in a separate coded frame group that falls into the
  3536. // overlap of the two existing frames. Splicing should not be performed since
  3537. // the content is poorly muxed. We can't know which frame to splice when the
  3538. // content is already messed up.
  3539. EXPECT_MEDIA_LOG(NoSpliceForBadMux(2, 2000));
  3540. NewCodedFrameGroupAppend("2D10K");
  3541. CheckExpectedRangesByTimestamp("{ [0,12) }");
  3542. CheckExpectedBuffers("0D10K 0D10K 2D10K");
  3543. CheckNoNextBuffer();
  3544. }
  3545. TEST_F(SourceBufferStreamTest, Audio_NoSpliceForEstimatedDuration) {
  3546. SetAudioStream();
  3547. Seek(0);
  3548. // Append two buffers, the latter having estimated duration.
  3549. NewCodedFrameGroupAppend("0D10K 10D10EK");
  3550. CheckExpectedRangesByTimestamp("{ [0,20) }");
  3551. CheckExpectedBuffers("0D10K 10D10EK");
  3552. CheckNoNextBuffer();
  3553. Seek(0);
  3554. // Add a new frame in a separate coded frame group that falls in the middle of
  3555. // the second buffer. In spite of the overlap, no splice should be performed
  3556. // due to the overlapped buffer having estimated duration.
  3557. NewCodedFrameGroupAppend("15D10K");
  3558. CheckExpectedRangesByTimestamp("{ [0,25) }");
  3559. CheckExpectedBuffers("0D10K 10D10EK 15D10K");
  3560. CheckNoNextBuffer();
  3561. }
  3562. TEST_F(SourceBufferStreamTest, Audio_SpliceTrimming_ExistingTrimming) {
  3563. const base::TimeDelta kDuration = base::Milliseconds(4);
  3564. const base::TimeDelta kNoDiscard = base::TimeDelta();
  3565. const bool is_keyframe = true;
  3566. SetAudioStream();
  3567. Seek(0);
  3568. // Make two BufferQueues with a mix of buffers containing start/end discard.
  3569. // Buffer PTS and duration have been adjusted to reflect discard. A_buffers
  3570. // will be appended first, then B_buffers. The start of B will overlap A
  3571. // to generate a splice.
  3572. BufferQueue A_buffers;
  3573. BufferQueue B_buffers;
  3574. // Buffer A1: PTS = 0, front discard = 2ms, duration = 2ms.
  3575. scoped_refptr<StreamParserBuffer> bufferA1 = StreamParserBuffer::CopyFrom(
  3576. &kDataA, kDataSize, is_keyframe, DemuxerStream::AUDIO, 0);
  3577. bufferA1->set_timestamp(base::Milliseconds(0));
  3578. bufferA1->set_duration(kDuration / 2);
  3579. const DecoderBuffer::DiscardPadding discardA1 =
  3580. std::make_pair(kDuration / 2, kNoDiscard);
  3581. bufferA1->set_discard_padding(discardA1);
  3582. A_buffers.push_back(bufferA1);
  3583. // Buffer A2: PTS = 2, end discard = 2ms, duration = 2ms.
  3584. scoped_refptr<StreamParserBuffer> bufferA2 = StreamParserBuffer::CopyFrom(
  3585. &kDataA, kDataSize, is_keyframe, DemuxerStream::AUDIO, 0);
  3586. bufferA2->set_timestamp(base::Milliseconds(2));
  3587. bufferA2->set_duration(kDuration / 2);
  3588. const DecoderBuffer::DiscardPadding discardA2 =
  3589. std::make_pair(kNoDiscard, kDuration / 2);
  3590. bufferA2->set_discard_padding(discardA2);
  3591. A_buffers.push_back(bufferA2);
  3592. // Buffer B1: PTS = 3, front discard = 2ms, duration = 2ms.
  3593. scoped_refptr<StreamParserBuffer> bufferB1 = StreamParserBuffer::CopyFrom(
  3594. &kDataA, kDataSize, is_keyframe, DemuxerStream::AUDIO, 0);
  3595. bufferB1->set_timestamp(base::Milliseconds(3));
  3596. bufferB1->set_duration(kDuration / 2);
  3597. const DecoderBuffer::DiscardPadding discardB1 =
  3598. std::make_pair(kDuration / 2, kNoDiscard);
  3599. bufferB1->set_discard_padding(discardB1);
  3600. B_buffers.push_back(bufferB1);
  3601. // Buffer B2: PTS = 5, no discard padding, duration = 4ms.
  3602. scoped_refptr<StreamParserBuffer> bufferB2 = StreamParserBuffer::CopyFrom(
  3603. &kDataA, kDataSize, is_keyframe, DemuxerStream::AUDIO, 0);
  3604. bufferB2->set_timestamp(base::Milliseconds(5));
  3605. bufferB2->set_duration(kDuration);
  3606. B_buffers.push_back(bufferB2);
  3607. // Append buffers, trigger splice trimming.
  3608. stream_->OnStartOfCodedFrameGroup(bufferA1->timestamp());
  3609. stream_->Append(A_buffers);
  3610. EXPECT_MEDIA_LOG(TrimmedSpliceOverlap(3000, 2000, 1000));
  3611. stream_->Append(B_buffers);
  3612. // Verify buffers.
  3613. scoped_refptr<StreamParserBuffer> read_buffer;
  3614. // Buffer A1 was not spliced, should be unchanged.
  3615. EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&read_buffer));
  3616. EXPECT_EQ(base::Milliseconds(0), read_buffer->timestamp());
  3617. EXPECT_EQ(kDuration / 2, read_buffer->duration());
  3618. EXPECT_EQ(discardA1, read_buffer->discard_padding());
  3619. // Buffer A2 was overlapped by buffer B1 1ms. Splice trimming should trim A2's
  3620. // duration and increase its discard padding by 1ms.
  3621. const base::TimeDelta overlap = base::Milliseconds(1);
  3622. EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&read_buffer));
  3623. EXPECT_EQ(base::Milliseconds(2), read_buffer->timestamp());
  3624. EXPECT_EQ((kDuration / 2) - overlap, read_buffer->duration());
  3625. const DecoderBuffer::DiscardPadding overlap_discard =
  3626. std::make_pair(discardA2.first, discardA2.second + overlap);
  3627. EXPECT_EQ(overlap_discard, read_buffer->discard_padding());
  3628. // Buffer B1 is overlapping A2, but B1 should be unchanged - splice trimming
  3629. // only modifies the earlier buffer (A1).
  3630. EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&read_buffer));
  3631. EXPECT_EQ(base::Milliseconds(3), read_buffer->timestamp());
  3632. EXPECT_EQ(kDuration / 2, read_buffer->duration());
  3633. EXPECT_EQ(discardB1, read_buffer->discard_padding());
  3634. // Buffer B2 is not spliced, should be unchanged.
  3635. EXPECT_STATUS_FOR_STREAM_OP(kSuccess, GetNextBuffer(&read_buffer));
  3636. EXPECT_EQ(base::Milliseconds(5), read_buffer->timestamp());
  3637. EXPECT_EQ(kDuration, read_buffer->duration());
  3638. EXPECT_EQ(std::make_pair(kNoDiscard, kNoDiscard),
  3639. read_buffer->discard_padding());
  3640. CheckNoNextBuffer();
  3641. }
  3642. TEST_F(SourceBufferStreamTest, Audio_SpliceFrame_NoMillisecondSplices) {
  3643. EXPECT_MEDIA_LOG(SkippingSpliceTooLittleOverlap(1250, 250));
  3644. video_config_ = TestVideoConfig::Invalid();
  3645. audio_config_.Initialize(
  3646. AudioCodec::kVorbis, kSampleFormatPlanarF32, CHANNEL_LAYOUT_STEREO, 4000,
  3647. EmptyExtraData(), EncryptionScheme::kUnencrypted, base::TimeDelta(), 0);
  3648. ResetStream<>(audio_config_);
  3649. // Equivalent to 0.5ms per frame.
  3650. SetStreamInfo(2000, 2000);
  3651. Seek(0);
  3652. // Append four buffers with a 0.5ms duration each.
  3653. NewCodedFrameGroupAppend(0, 4);
  3654. CheckExpectedRangesByTimestamp("{ [0,2) }");
  3655. // Overlap the range [0, 2) with [1.25, 2); this results in an overlap of
  3656. // 0.25ms between the original buffer at time 1.0 and the new buffer at time
  3657. // 1.25.
  3658. NewCodedFrameGroupAppend_OffsetFirstBuffer(2, 2, base::Milliseconds(0.25));
  3659. CheckExpectedRangesByTimestamp("{ [0,2) }");
  3660. // A splice frame should not be generated since it requires at least 1ms of
  3661. // data to crossfade.
  3662. CheckExpectedBuffers("0K 0K 1K 1K");
  3663. CheckNoNextBuffer();
  3664. }
  3665. TEST_F(SourceBufferStreamTest, Audio_PrerollFrame) {
  3666. Seek(0);
  3667. NewCodedFrameGroupAppend("0K 3P 6K");
  3668. CheckExpectedBuffers("0K 3P 6K");
  3669. CheckNoNextBuffer();
  3670. }
  3671. TEST_F(SourceBufferStreamTest, Audio_ConfigChangeWithPreroll) {
  3672. AudioDecoderConfig new_config(AudioCodec::kVorbis, kSampleFormatPlanarF32,
  3673. CHANNEL_LAYOUT_MONO, 2000, EmptyExtraData(),
  3674. EncryptionScheme::kUnencrypted);
  3675. SetAudioStream();
  3676. Seek(0);
  3677. // Append some audio using the default configuration.
  3678. CheckAudioConfig(audio_config_);
  3679. NewCodedFrameGroupAppend("0K 3K 6K");
  3680. // Update the configuration.
  3681. stream_->UpdateAudioConfig(new_config, false);
  3682. // We haven't read any buffers at this point, so the config for the next
  3683. // buffer at time 0 should still be the original config.
  3684. CheckAudioConfig(audio_config_);
  3685. // Append new audio containing preroll and using the new config.
  3686. EXPECT_MEDIA_LOG(TrimmedSpliceOverlap(7000, 6000, 2000));
  3687. NewCodedFrameGroupAppend("7P 8K");
  3688. // Check buffers from the first append.
  3689. CheckExpectedBuffers("0K 3K 6K");
  3690. // Verify the next attempt to get a buffer will signal that a config change
  3691. // has happened.
  3692. scoped_refptr<StreamParserBuffer> buffer;
  3693. EXPECT_STATUS_FOR_STREAM_OP(kConfigChange, GetNextBuffer(&buffer));
  3694. // Verify upcoming buffers will use the new config.
  3695. CheckAudioConfig(new_config);
  3696. // Check buffers from the second append, including preroll.
  3697. // CheckExpectedBuffers("6P 7K 8K");
  3698. CheckExpectedBuffers("7P 8K");
  3699. CheckNoNextBuffer();
  3700. }
  3701. TEST_F(SourceBufferStreamTest, Audio_Opus_SeekToJustBeforeRangeStart) {
  3702. // Seek to a time within the fudge room of seekability to a buffered Opus
  3703. // audio frame's range, but before the range's start. Use small seek_preroll
  3704. // in case the associated logic to check same config in the preroll time
  3705. // interval requires a nonzero seek_preroll value.
  3706. video_config_ = TestVideoConfig::Invalid();
  3707. audio_config_.Initialize(AudioCodec::kOpus, kSampleFormatPlanarF32,
  3708. CHANNEL_LAYOUT_STEREO, 1000, EmptyExtraData(),
  3709. EncryptionScheme::kUnencrypted,
  3710. base::Milliseconds(10), 0);
  3711. ResetStream<>(audio_config_);
  3712. // Equivalent to 1s per frame.
  3713. SetStreamInfo(1, 1);
  3714. Seek(0);
  3715. // Append a buffer at 1.5 seconds, with duration 1 second, increasing the
  3716. // fudge room to 2 * 1 seconds. The pending seek to time 0 should be satisfied
  3717. // with this buffer's range, because that seek time is within the fudge room
  3718. // of 2.
  3719. NewCodedFrameGroupAppend("1500D1000K");
  3720. CheckExpectedRangesByTimestamp("{ [1500,2500) }");
  3721. CheckExpectedBuffers("1500K");
  3722. CheckNoNextBuffer();
  3723. }
  3724. TEST_F(SourceBufferStreamTest, BFrames) {
  3725. Seek(0);
  3726. NewCodedFrameGroupAppend("0K 120|30 30|60 60|90 90|120");
  3727. CheckExpectedRangesByTimestamp("{ [0,150) }");
  3728. CheckExpectedBuffers("0K 120|30 30|60 60|90 90|120");
  3729. CheckNoNextBuffer();
  3730. }
  3731. TEST_F(SourceBufferStreamTest, RemoveShouldAlwaysExcludeEnd) {
  3732. NewCodedFrameGroupAppend("10D2K 12D2 14D2");
  3733. CheckExpectedRangesByTimestamp("{ [10,16) }");
  3734. // Start new coded frame group, appending KF to abut the start of previous
  3735. // group.
  3736. NewCodedFrameGroupAppend("0D10K");
  3737. Seek(0);
  3738. CheckExpectedRangesByTimestamp("{ [0,16) }");
  3739. CheckExpectedBuffers("0K 10K 12 14");
  3740. CheckNoNextBuffer();
  3741. // Append another buffer with the same timestamp as the last KF. This triggers
  3742. // special logic that allows two buffers to have the same timestamp. When
  3743. // preparing for this new append, there is no reason to remove the later GOP
  3744. // starting at timestamp 10. This verifies the fix for http://crbug.com/469325
  3745. // where the decision *not* to remove the start of the overlapped range was
  3746. // erroneously triggering buffers with a timestamp matching the end
  3747. // of the append (and any later dependent frames) to be removed.
  3748. AppendBuffers("0D10");
  3749. Seek(0);
  3750. CheckExpectedRangesByTimestamp("{ [0,16) }");
  3751. CheckExpectedBuffers("0K 0 10K 12 14");
  3752. CheckNoNextBuffer();
  3753. }
  3754. TEST_F(SourceBufferStreamTest, RefinedDurationEstimates_BackOverlap) {
  3755. // Append a few buffers, the last one having estimated duration.
  3756. NewCodedFrameGroupAppend("0K 5 10 20D10E");
  3757. CheckExpectedRangesByTimestamp("{ [0,30) }");
  3758. Seek(0);
  3759. CheckExpectedBuffers("0K 5 10 20D10E");
  3760. CheckNoNextBuffer();
  3761. // Append a buffer to the end that overlaps the *back* of the existing range.
  3762. // This should trigger the estimated duration to be recomputed as a timestamp
  3763. // delta.
  3764. AppendBuffers("25D10");
  3765. CheckExpectedRangesByTimestamp("{ [0,35) }");
  3766. Seek(0);
  3767. // The duration of the buffer at time 20 has changed from 10ms to 5ms.
  3768. CheckExpectedBuffers("0K 5 10 20D5E 25");
  3769. CheckNoNextBuffer();
  3770. // If the last buffer is removed, the adjusted duration should remain at 5ms.
  3771. RemoveInMs(25, 35, 35);
  3772. CheckExpectedRangesByTimestamp("{ [0,25) }");
  3773. Seek(0);
  3774. CheckExpectedBuffers("0K 5 10 20D5E");
  3775. CheckNoNextBuffer();
  3776. }
  3777. TEST_F(SourceBufferStreamTest, RefinedDurationEstimates_FrontOverlap) {
  3778. // Append a few buffers.
  3779. NewCodedFrameGroupAppend("10K 15 20D5");
  3780. CheckExpectedRangesByTimestamp("{ [10,25) }");
  3781. SeekToTimestampMs(10);
  3782. CheckExpectedBuffers("10K 15 20");
  3783. CheckNoNextBuffer();
  3784. // Append new buffers, where the last has estimated duration that overlaps the
  3785. // *front* of the existing range. The overlap should trigger refinement of the
  3786. // estimated duration from 7ms to 5ms.
  3787. NewCodedFrameGroupAppend("0K 5D7E");
  3788. CheckExpectedRangesByTimestamp("{ [0,25) }");
  3789. Seek(0);
  3790. CheckExpectedBuffers("0K 5D5E 10K 15 20");
  3791. CheckNoNextBuffer();
  3792. // If the overlapped buffer at timestamp 10 is removed, the adjusted duration
  3793. // should remain adjusted.
  3794. RemoveInMs(10, 20, 25);
  3795. CheckExpectedRangesByTimestamp("{ [0,10) }");
  3796. Seek(0);
  3797. CheckExpectedBuffers("0K 5D5E");
  3798. CheckNoNextBuffer();
  3799. }
  3800. TEST_F(SourceBufferStreamTest, SeekToStartSatisfiedUpToThreshold) {
  3801. NewCodedFrameGroupAppend("999K 1010 1020D10");
  3802. CheckExpectedRangesByTimestamp("{ [999,1030) }");
  3803. SeekToTimestampMs(0);
  3804. CheckExpectedBuffers("999K 1010 1020D10");
  3805. CheckNoNextBuffer();
  3806. }
  3807. TEST_F(SourceBufferStreamTest, SeekToStartUnsatisfiedBeyondThreshold) {
  3808. NewCodedFrameGroupAppend("1000K 1010 1020D10");
  3809. CheckExpectedRangesByTimestamp("{ [1000,1030) }");
  3810. SeekToTimestampMs(0);
  3811. CheckNoNextBuffer();
  3812. }
  3813. TEST_F(SourceBufferStreamTest,
  3814. ReSeekToStartSatisfiedUpToThreshold_SameTimestamps) {
  3815. // Append a few buffers.
  3816. NewCodedFrameGroupAppend("999K 1010 1020D10");
  3817. CheckExpectedRangesByTimestamp("{ [999,1030) }");
  3818. // Don't read any buffers between Seek and Remove.
  3819. SeekToTimestampMs(0);
  3820. RemoveInMs(999, 1030, 1030);
  3821. CheckExpectedRangesByTimestamp("{ }");
  3822. CheckNoNextBuffer();
  3823. // Append buffers at the original timestamps and verify no stall.
  3824. NewCodedFrameGroupAppend("999K 1010 1020D10");
  3825. CheckExpectedRangesByTimestamp("{ [999,1030) }");
  3826. CheckExpectedBuffers("999K 1010 1020D10");
  3827. CheckNoNextBuffer();
  3828. }
  3829. TEST_F(SourceBufferStreamTest,
  3830. ReSeekToStartSatisfiedUpToThreshold_EarlierTimestamps) {
  3831. // Append a few buffers.
  3832. NewCodedFrameGroupAppend("999K 1010 1020D10");
  3833. CheckExpectedRangesByTimestamp("{ [999,1030) }");
  3834. // Don't read any buffers between Seek and Remove.
  3835. SeekToTimestampMs(0);
  3836. RemoveInMs(999, 1030, 1030);
  3837. CheckExpectedRangesByTimestamp("{ }");
  3838. CheckNoNextBuffer();
  3839. // Append buffers before the original timestamps and verify no stall (the
  3840. // re-seek to time 0 should still be satisfied with the new buffers).
  3841. NewCodedFrameGroupAppend("500K 510 520D10");
  3842. CheckExpectedRangesByTimestamp("{ [500,530) }");
  3843. CheckExpectedBuffers("500K 510 520D10");
  3844. CheckNoNextBuffer();
  3845. }
  3846. TEST_F(SourceBufferStreamTest,
  3847. ReSeekToStartSatisfiedUpToThreshold_LaterTimestamps) {
  3848. // Append a few buffers.
  3849. NewCodedFrameGroupAppend("500K 510 520D10");
  3850. CheckExpectedRangesByTimestamp("{ [500,530) }");
  3851. // Don't read any buffers between Seek and Remove.
  3852. SeekToTimestampMs(0);
  3853. RemoveInMs(500, 530, 530);
  3854. CheckExpectedRangesByTimestamp("{ }");
  3855. CheckNoNextBuffer();
  3856. // Append buffers beginning after original timestamps, but still below the
  3857. // start threshold, and verify no stall (the re-seek to time 0 should still be
  3858. // satisfied with the new buffers).
  3859. NewCodedFrameGroupAppend("999K 1010 1020D10");
  3860. CheckExpectedRangesByTimestamp("{ [999,1030) }");
  3861. CheckExpectedBuffers("999K 1010 1020D10");
  3862. CheckNoNextBuffer();
  3863. }
  3864. TEST_F(SourceBufferStreamTest, ReSeekBeyondStartThreshold_SameTimestamps) {
  3865. // Append a few buffers.
  3866. NewCodedFrameGroupAppend("1000K 1010 1020D10");
  3867. CheckExpectedRangesByTimestamp("{ [1000,1030) }");
  3868. // Don't read any buffers between Seek and Remove.
  3869. SeekToTimestampMs(1000);
  3870. RemoveInMs(1000, 1030, 1030);
  3871. CheckExpectedRangesByTimestamp("{ }");
  3872. CheckNoNextBuffer();
  3873. // Append buffers at the original timestamps and verify no stall.
  3874. NewCodedFrameGroupAppend("1000K 1010 1020D10");
  3875. CheckExpectedRangesByTimestamp("{ [1000,1030) }");
  3876. CheckExpectedBuffers("1000K 1010 1020D10");
  3877. CheckNoNextBuffer();
  3878. }
  3879. TEST_F(SourceBufferStreamTest, ReSeekBeyondThreshold_EarlierTimestamps) {
  3880. // Append a few buffers.
  3881. NewCodedFrameGroupAppend("2000K 2010 2020D10");
  3882. CheckExpectedRangesByTimestamp("{ [2000,2030) }");
  3883. // Don't read any buffers between Seek and Remove.
  3884. SeekToTimestampMs(2000);
  3885. RemoveInMs(2000, 2030, 2030);
  3886. CheckExpectedRangesByTimestamp("{ }");
  3887. CheckNoNextBuffer();
  3888. // Append buffers before the original timestamps and verify no stall (the
  3889. // re-seek to time 2 seconds should still be satisfied with the new buffers
  3890. // and should emit preroll from last keyframe).
  3891. NewCodedFrameGroupAppend("1080K 1090 2000D10");
  3892. CheckExpectedRangesByTimestamp("{ [1080,2010) }");
  3893. CheckExpectedBuffers("1080K 1090 2000D10");
  3894. CheckNoNextBuffer();
  3895. }
  3896. TEST_F(SourceBufferStreamTest, ConfigChange_ReSeek) {
  3897. // Append a few buffers, with a config change in the middle.
  3898. VideoDecoderConfig new_config = TestVideoConfig::Large();
  3899. NewCodedFrameGroupAppend("2000K 2010 2020D10");
  3900. stream_->UpdateVideoConfig(new_config, false);
  3901. NewCodedFrameGroupAppend("2030K 2040 2050D10");
  3902. CheckExpectedRangesByTimestamp("{ [2000,2060) }");
  3903. // Read the config change, but don't read any non-config-change buffer between
  3904. // Seek and Remove.
  3905. scoped_refptr<StreamParserBuffer> buffer;
  3906. CheckVideoConfig(video_config_);
  3907. SeekToTimestampMs(2030);
  3908. CheckVideoConfig(video_config_);
  3909. EXPECT_STATUS_FOR_STREAM_OP(kConfigChange, GetNextBuffer(&buffer));
  3910. CheckVideoConfig(new_config);
  3911. // Trigger the re-seek.
  3912. RemoveInMs(2030, 2060, 2060);
  3913. CheckExpectedRangesByTimestamp("{ [2000,2030) }");
  3914. CheckNoNextBuffer();
  3915. // Append buffers at the original timestamps and verify no stall or redundant
  3916. // signalling of config change.
  3917. NewCodedFrameGroupAppend("2030K 2040 2050D10");
  3918. CheckVideoConfig(new_config);
  3919. CheckExpectedRangesByTimestamp("{ [2000,2060) }");
  3920. CheckExpectedBuffers("2030K 2040 2050D10");
  3921. CheckNoNextBuffer();
  3922. CheckVideoConfig(new_config);
  3923. // Seek to the start of buffered and verify config changes and buffers.
  3924. SeekToTimestampMs(2000);
  3925. CheckVideoConfig(new_config);
  3926. ASSERT_FALSE(new_config.Matches(video_config_));
  3927. EXPECT_STATUS_FOR_STREAM_OP(kConfigChange, GetNextBuffer(&buffer));
  3928. CheckVideoConfig(video_config_);
  3929. CheckExpectedBuffers("2000K 2010 2020D10");
  3930. CheckVideoConfig(video_config_);
  3931. EXPECT_STATUS_FOR_STREAM_OP(kConfigChange, GetNextBuffer(&buffer));
  3932. CheckVideoConfig(new_config);
  3933. CheckExpectedBuffers("2030K 2040 2050D10");
  3934. CheckNoNextBuffer();
  3935. CheckVideoConfig(new_config);
  3936. }
  3937. TEST_F(SourceBufferStreamTest, TrackBuffer_ExhaustionWithSkipForward) {
  3938. NewCodedFrameGroupAppend("0K 10 20 30 40");
  3939. // Read the first 4 buffers, so next buffer is at time 40.
  3940. Seek(0);
  3941. CheckExpectedRangesByTimestamp("{ [0,50) }");
  3942. CheckExpectedBuffers("0K 10 20 30");
  3943. // Overlap-append, populating track buffer with timestamp 40 from original
  3944. // append. Confirm there could be a large jump in time until the next key
  3945. // frame after exhausting the track buffer.
  3946. NewCodedFrameGroupAppend(
  3947. "31K 41 51 61 71 81 91 101 111 121 "
  3948. "131K 141");
  3949. CheckExpectedRangesByTimestamp("{ [0,151) }");
  3950. // Confirm the large jump occurs and warning log is generated.
  3951. // If this test is changed, update
  3952. // TrackBufferExhaustion_ImmediateNewTrackBuffer accordingly.
  3953. EXPECT_MEDIA_LOG(ContainsTrackBufferExhaustionSkipLog(91));
  3954. CheckExpectedBuffers("40 131K 141");
  3955. CheckNoNextBuffer();
  3956. }
  3957. TEST_F(SourceBufferStreamTest,
  3958. TrackBuffer_ExhaustionAndImmediateNewTrackBuffer) {
  3959. NewCodedFrameGroupAppend("0K 10 20 30 40");
  3960. // Read the first 4 buffers, so next buffer is at time 40.
  3961. Seek(0);
  3962. CheckExpectedRangesByTimestamp("{ [0,50) }");
  3963. CheckExpectedBuffers("0K 10 20 30");
  3964. // Overlap-append
  3965. NewCodedFrameGroupAppend(
  3966. "31K 41 51 61 71 81 91 101 111 121 "
  3967. "131K 141");
  3968. CheckExpectedRangesByTimestamp("{ [0,151) }");
  3969. // Exhaust the track buffer, but don't read any of the overlapping append yet.
  3970. CheckExpectedBuffers("40");
  3971. // Selected range's next buffer is now the 131K buffer from the overlapping
  3972. // append. (See TrackBuffer_ExhaustionWithSkipForward for that verification.)
  3973. // Do another overlap-append to immediately create another track buffer and
  3974. // verify both track buffer exhaustions skip forward and emit log warnings.
  3975. NewCodedFrameGroupAppend(
  3976. "22K 32 42 52 62 72 82 92 102 112 122K 132 142 152K 162");
  3977. CheckExpectedRangesByTimestamp("{ [0,172) }");
  3978. InSequence s;
  3979. EXPECT_MEDIA_LOG(ContainsTrackBufferExhaustionSkipLog(91));
  3980. EXPECT_MEDIA_LOG(ContainsTrackBufferExhaustionSkipLog(11));
  3981. CheckExpectedBuffers("131K 141 152K 162");
  3982. CheckNoNextBuffer();
  3983. }
  3984. TEST_F(
  3985. SourceBufferStreamTest,
  3986. AdjacentCodedFrameGroupContinuation_NoGapCreatedByTinyGapInGroupContinuation) {
  3987. NewCodedFrameGroupAppend("0K 10 20K 30 40K 50D10");
  3988. CheckExpectedRangesByTimestamp("{ [0,60) }");
  3989. // Continue appending to the previously started coded frame group, albeit with
  3990. // a tiny (1ms) gap. This gap should *NOT* produce a buffered range gap.
  3991. AppendBuffers("61K 71D10");
  3992. CheckExpectedRangesByTimestamp("{ [0,81) }");
  3993. }
  3994. TEST_F(SourceBufferStreamTest,
  3995. AdjacentCodedFrameGroupContinuation_NoGapCreatedPrefixRemoved) {
  3996. NewCodedFrameGroupAppend("0K 10 20K 30 40K 50D10");
  3997. CheckExpectedRangesByTimestamp("{ [0,60) }");
  3998. RemoveInMs(0, 35, 60);
  3999. CheckExpectedRangesByTimestamp("{ [40,60) }");
  4000. // Continue appending to the previously started coded frame group, albeit with
  4001. // a tiny (1ms) gap. This gap should *NOT* produce a buffered range gap.
  4002. AppendBuffers("61K 71D10");
  4003. CheckExpectedRangesByTimestamp("{ [40,81) }");
  4004. }
  4005. TEST_F(SourceBufferStreamTest,
  4006. AdjacentNewCodedFrameGroupContinuation_NoGapCreatedPrefixRemoved) {
  4007. NewCodedFrameGroupAppend("0K 10 20K 30 40K 50D10");
  4008. CheckExpectedRangesByTimestamp("{ [0,60) }");
  4009. RemoveInMs(0, 35, 60);
  4010. CheckExpectedRangesByTimestamp("{ [40,60) }");
  4011. // Continue appending, with a new coded frame group, albeit with
  4012. // a tiny (1ms) gap. This gap should *NOT* produce a buffered range gap.
  4013. // This test demonstrates the "pre-relaxation" behavior, where a new "media
  4014. // segment" (now a new "coded frame group") was signaled at every media
  4015. // segment boundary.
  4016. NewCodedFrameGroupAppend("61K 71D10");
  4017. CheckExpectedRangesByTimestamp("{ [40,81) }");
  4018. }
  4019. TEST_F(SourceBufferStreamTest,
  4020. StartCodedFrameGroup_RemoveThenAppendMoreMuchLater) {
  4021. NewCodedFrameGroupAppend("1000K 1010 1020 1030K 1040 1050 1060K 1070 1080");
  4022. NewCodedFrameGroupAppend("0K 10 20");
  4023. CheckExpectedRangesByTimestamp("{ [0,30) [1000,1090) }");
  4024. SignalStartOfCodedFrameGroup(base::Milliseconds(1070));
  4025. CheckExpectedRangesByTimestamp("{ [0,30) [1000,1090) }");
  4026. RemoveInMs(1030, 1050, 1090);
  4027. CheckExpectedRangesByTimestamp("{ [0,30) [1000,1030) [1060,1090) }");
  4028. // We've signalled that we're about to do some appends to a coded frame group
  4029. // which starts at time 1070ms. Note that the first frame, if any ever,
  4030. // appended to this SourceBufferStream for that coded frame group must have a
  4031. // decode timestamp >= 1070ms (it can be significantly in the future).
  4032. // Regardless, that appended frame must be buffered into the same existing
  4033. // range as current [1060,1090), since the new coded frame group's start of
  4034. // 1070ms is within that range.
  4035. AppendBuffers("2000K 2010");
  4036. CheckExpectedRangesByTimestamp("{ [0,30) [1000,1030) [1060,2020) }");
  4037. SeekToTimestampMs(1060);
  4038. CheckExpectedBuffers("1060K 2000K 2010");
  4039. CheckNoNextBuffer();
  4040. }
  4041. TEST_F(SourceBufferStreamTest,
  4042. StartCodedFrameGroup_InExisting_AppendMuchLater) {
  4043. NewCodedFrameGroupAppend("0K 10 20 30K 40 50");
  4044. SignalStartOfCodedFrameGroup(base::Milliseconds(45));
  4045. CheckExpectedRangesByTimestamp("{ [0,60) }");
  4046. AppendBuffers("2000K 2010");
  4047. CheckExpectedRangesByTimestamp("{ [0,2020) }");
  4048. Seek(0);
  4049. CheckExpectedBuffers("0K 10 20 30K 40 2000K 2010");
  4050. CheckNoNextBuffer();
  4051. }
  4052. TEST_F(SourceBufferStreamTest,
  4053. StartCodedFrameGroup_InExisting_RemoveGOP_ThenAppend_1) {
  4054. NewCodedFrameGroupAppend("0K 10 20 30K 40 50");
  4055. SignalStartOfCodedFrameGroup(base::Milliseconds(30));
  4056. RemoveInMs(30, 60, 60);
  4057. CheckExpectedRangesByTimestamp("{ [0,30) }");
  4058. AppendBuffers("2000K 2010");
  4059. CheckExpectedRangesByTimestamp("{ [0,2020) }");
  4060. Seek(0);
  4061. CheckExpectedBuffers("0K 10 20 2000K 2010");
  4062. CheckNoNextBuffer();
  4063. }
  4064. TEST_F(SourceBufferStreamTest,
  4065. StartCodedFrameGroup_InExisting_RemoveGOP_ThenAppend_2) {
  4066. NewCodedFrameGroupAppend("0K 10 20 30K 40 50");
  4067. // Though we signal 45ms, it's adjusted internally (due to detected overlap)
  4068. // to be 40.001ms (which is just beyond the highest buffered timestamp at or
  4069. // before 45ms) to help prevent potential discontinuity across the front of
  4070. // the overlapping append.
  4071. SignalStartOfCodedFrameGroup(base::Milliseconds(45));
  4072. RemoveInMs(30, 60, 60);
  4073. CheckExpectedRangesByTimestamp("{ [0,30) }");
  4074. AppendBuffers("2000K 2010");
  4075. CheckExpectedRangesByTimestamp("{ [0,30) [40,2020) }");
  4076. Seek(0);
  4077. CheckExpectedBuffers("0K 10 20");
  4078. CheckNoNextBuffer();
  4079. SeekToTimestampMs(40);
  4080. CheckExpectedBuffers("2000K 2010");
  4081. CheckNoNextBuffer();
  4082. SeekToTimestampMs(1000);
  4083. CheckExpectedBuffers("2000K 2010");
  4084. CheckNoNextBuffer();
  4085. }
  4086. TEST_F(SourceBufferStreamTest,
  4087. StartCodedFrameGroup_InExisting_RemoveMostRecentAppend_ThenAppend_1) {
  4088. NewCodedFrameGroupAppend("0K 10 20 30K 40 50");
  4089. SignalStartOfCodedFrameGroup(base::Milliseconds(45));
  4090. RemoveInMs(50, 60, 60);
  4091. CheckExpectedRangesByTimestamp("{ [0,50) }");
  4092. AppendBuffers("2000K 2010");
  4093. CheckExpectedRangesByTimestamp("{ [0,2020) }");
  4094. Seek(0);
  4095. CheckExpectedBuffers("0K 10 20 30K 40 2000K 2010");
  4096. CheckNoNextBuffer();
  4097. }
  4098. TEST_F(SourceBufferStreamTest,
  4099. StartCodedFrameGroup_InExisting_RemoveMostRecentAppend_ThenAppend_2) {
  4100. NewCodedFrameGroupAppend("0K 10 20 30K 40 50");
  4101. SignalStartOfCodedFrameGroup(base::Milliseconds(50));
  4102. RemoveInMs(50, 60, 60);
  4103. CheckExpectedRangesByTimestamp("{ [0,50) }");
  4104. AppendBuffers("2000K 2010");
  4105. CheckExpectedRangesByTimestamp("{ [0,2020) }");
  4106. Seek(0);
  4107. CheckExpectedBuffers("0K 10 20 30K 40 2000K 2010");
  4108. CheckNoNextBuffer();
  4109. }
  4110. TEST_F(SourceBufferStreamTest, GetHighestPresentationTimestamp) {
  4111. EXPECT_EQ(base::TimeDelta(), stream_->GetHighestPresentationTimestamp());
  4112. NewCodedFrameGroupAppend("0K 10K");
  4113. EXPECT_EQ(base::Milliseconds(10), stream_->GetHighestPresentationTimestamp());
  4114. RemoveInMs(0, 10, 20);
  4115. EXPECT_EQ(base::Milliseconds(10), stream_->GetHighestPresentationTimestamp());
  4116. RemoveInMs(10, 20, 20);
  4117. EXPECT_EQ(base::TimeDelta(), stream_->GetHighestPresentationTimestamp());
  4118. NewCodedFrameGroupAppend("0K 10K");
  4119. EXPECT_EQ(base::Milliseconds(10), stream_->GetHighestPresentationTimestamp());
  4120. RemoveInMs(10, 20, 20);
  4121. EXPECT_EQ(base::TimeDelta(), stream_->GetHighestPresentationTimestamp());
  4122. }
  4123. TEST_F(SourceBufferStreamTest, GarbageCollectionUnderMemoryPressure) {
  4124. SetMemoryLimit(16);
  4125. NewCodedFrameGroupAppend("0K 1 2 3K 4 5 6K 7 8 9K 10 11 12K 13 14 15K");
  4126. CheckExpectedRangesByTimestamp("{ [0,16) }");
  4127. // This feature is disabled by default, so by default memory pressure
  4128. // notification takes no effect and the memory limits and won't remove
  4129. // anything from buffered ranges, since we are under the limit of 20 bytes.
  4130. stream_->OnMemoryPressure(
  4131. base::Milliseconds(0),
  4132. base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, false);
  4133. EXPECT_TRUE(GarbageCollect(base::Milliseconds(8), 0));
  4134. CheckExpectedRangesByTimestamp("{ [0,16) }");
  4135. // Now enable the feature (on top of any overrides already in
  4136. // |scoped_feature_list_|.)
  4137. base::test::ScopedFeatureList scoped_feature_list;
  4138. scoped_feature_list.InitAndEnableFeature(kMemoryPressureBasedSourceBufferGC);
  4139. // Verify that effective MSE memory limit is reduced under memory pressure.
  4140. stream_->OnMemoryPressure(
  4141. base::Milliseconds(0),
  4142. base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, false);
  4143. // Effective memory limit is now 8 buffers, but we still will not collect any
  4144. // data between the current playback position 3 and last append position 15.
  4145. EXPECT_TRUE(GarbageCollect(base::Milliseconds(4), 0));
  4146. CheckExpectedRangesByTimestamp("{ [3,16) }");
  4147. // As playback proceeds further to time 9 we should be able to collect
  4148. // enough data to bring us back under memory limit of 8 buffers.
  4149. EXPECT_TRUE(GarbageCollect(base::Milliseconds(9), 0));
  4150. CheckExpectedRangesByTimestamp("{ [9,16) }");
  4151. // If memory pressure becomes critical, the garbage collection algorithm
  4152. // becomes even more aggressive and collects everything up to the current
  4153. // playback position.
  4154. stream_->OnMemoryPressure(
  4155. base::Milliseconds(0),
  4156. base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, false);
  4157. EXPECT_TRUE(GarbageCollect(base::Milliseconds(13), 0));
  4158. CheckExpectedRangesByTimestamp("{ [12,16) }");
  4159. // But even under critical memory pressure the MSE memory limit imposed by the
  4160. // memory pressure is soft, i.e. we should be able to append more data
  4161. // successfully up to the hard limit of 16 bytes.
  4162. NewCodedFrameGroupAppend("16K 17 18 19 20 21 22 23 24 25 26 27");
  4163. CheckExpectedRangesByTimestamp("{ [12,28) }");
  4164. EXPECT_TRUE(GarbageCollect(base::Milliseconds(13), 0));
  4165. CheckExpectedRangesByTimestamp("{ [12,28) }");
  4166. }
  4167. TEST_F(SourceBufferStreamTest, InstantGarbageCollectionUnderMemoryPressure) {
  4168. SetMemoryLimit(16);
  4169. NewCodedFrameGroupAppend("0K 1 2 3K 4 5 6K 7 8 9K 10 11 12K 13 14 15K");
  4170. CheckExpectedRangesByTimestamp("{ [0,16) }");
  4171. // Verify that garbage collection happens immediately on critical memory
  4172. // pressure notification, even without explicit GarbageCollect invocation,
  4173. // when the immediate GC is allowed.
  4174. // First, enable the feature (on top of any overrides already in
  4175. // |scoped_feature_list_|.)
  4176. base::test::ScopedFeatureList scoped_feature_list;
  4177. scoped_feature_list.InitAndEnableFeature(kMemoryPressureBasedSourceBufferGC);
  4178. stream_->OnMemoryPressure(
  4179. base::Milliseconds(7),
  4180. base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, true);
  4181. CheckExpectedRangesByTimestamp("{ [6,16) }");
  4182. stream_->OnMemoryPressure(
  4183. base::Milliseconds(9),
  4184. base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, true);
  4185. CheckExpectedRangesByTimestamp("{ [9,16) }");
  4186. }
  4187. TEST_F(SourceBufferStreamTest, GCFromFrontThenExplicitRemoveFromMiddleToEnd) {
  4188. // Attempts to exercise SourceBufferRange::GetBufferIndexAt() after its
  4189. // |keyframe_map_index_base_| has been increased, and when there is a GOP
  4190. // following the search timestamp. GC followed by an explicit remove may
  4191. // trigger that code path.
  4192. SetMemoryLimit(10);
  4193. // Append 3 IBPPP GOPs in one continuous range.
  4194. NewCodedFrameGroupAppend(
  4195. "0K 40|10 10|20 20|30 30|40 "
  4196. "50K 90|60 60|70 70|80 80|90 "
  4197. "100K 140|110 110|120 120|130 130|140");
  4198. CheckExpectedRangesByTimestamp("{ [0,150) }");
  4199. // Seek to the second GOP's keyframe to allow GC to collect all of the first
  4200. // GOP (ostensibly increasing SourceBufferRange's |keyframe_map_index_base_|).
  4201. SeekToTimestampMs(50);
  4202. GarbageCollect(base::Milliseconds(50), 0);
  4203. CheckExpectedRangesByTimestamp("{ [50,150) }");
  4204. // Remove from the middle of the first remaining GOP to the end of the range.
  4205. RemoveInMs(60, 150, 150);
  4206. CheckExpectedRangesByTimestamp("{ [50,60) }");
  4207. }
  4208. TEST_F(SourceBufferStreamTest, BFrames_WithoutEditList) {
  4209. // Simulates B-frame content where MP4 edit lists are not used to shift PTS so
  4210. // it matches DTS. From acolwell@chromium.org in https://crbug.com/398130
  4211. Seek(0);
  4212. NewCodedFrameGroupAppend(base::Milliseconds(60),
  4213. "60|0K 180|30 90|60 120|90 150|120");
  4214. CheckExpectedRangesByTimestamp("{ [60,210) }");
  4215. CheckExpectedBuffers("60|0K 180|30 90|60 120|90 150|120");
  4216. CheckNoNextBuffer();
  4217. }
  4218. TEST_F(SourceBufferStreamTest, OverlapSameTimestampWithinSameGOP) {
  4219. // We use distinct appends here to make sure the intended frame durations
  4220. // are respected by the test helpers (which the OneByOne helper doesn't
  4221. // respect always). We need granular appends of this GOP for at least the
  4222. // append of PTS=DTS=30, below.
  4223. NewCodedFrameGroupAppend("0|0D10K");
  4224. AppendBuffers("30|10D0");
  4225. AppendBuffers("20|20D10");
  4226. // The following should *not* remove the PTS frame 30, above.
  4227. AppendBuffers("30|30 40|40");
  4228. Seek(0);
  4229. CheckExpectedRangesByTimestamp("{ [0,50) }");
  4230. CheckExpectedBuffers("0K 30|10 20 30 40");
  4231. }
  4232. struct VideoEndTimeCase {
  4233. // Times in Milliseconds
  4234. int64_t new_frame_pts;
  4235. int64_t new_frame_duration;
  4236. int64_t expected_highest_pts;
  4237. int64_t expected_end_time;
  4238. };
  4239. TEST_F(SourceBufferStreamTest, VideoRangeEndTimeCases) {
  4240. // With a basic range containing just a single keyframe [10,20), verify
  4241. // various keyframe overlap append cases' results on the range end time.
  4242. const VideoEndTimeCase kCases[] = {
  4243. {0, 10, 10, 20},
  4244. {20, 1, 20, 21},
  4245. {15, 3, 15, 18},
  4246. {15, 5, 15, 20},
  4247. {15, 8, 15, 23},
  4248. // Cases where the new frame removes the previous frame:
  4249. {10, 3, 10, 13},
  4250. {10, 10, 10, 20},
  4251. {10, 13, 10, 23},
  4252. {5, 8, 5, 13},
  4253. {5, 15, 5, 20},
  4254. {5, 20, 5, 25}};
  4255. for (const auto& c : kCases) {
  4256. RemoveInMs(0, 100, 100);
  4257. NewCodedFrameGroupAppend("10D10K");
  4258. CheckExpectedRangesByTimestamp("{ [10,20) }");
  4259. CheckExpectedRangeEndTimes("{ <10,20> }");
  4260. std::stringstream ss;
  4261. ss << c.new_frame_pts << "D" << c.new_frame_duration << "K";
  4262. DVLOG(1) << "Appending " << ss.str();
  4263. NewCodedFrameGroupAppend(ss.str());
  4264. std::stringstream expected;
  4265. expected << "{ <" << c.expected_highest_pts << "," << c.expected_end_time
  4266. << "> }";
  4267. CheckExpectedRangeEndTimes(expected.str());
  4268. }
  4269. }
  4270. struct AudioEndTimeCase {
  4271. // Times in Milliseconds
  4272. int64_t new_frame_pts;
  4273. int64_t new_frame_duration;
  4274. int64_t expected_highest_pts;
  4275. int64_t expected_end_time;
  4276. bool expect_splice;
  4277. };
  4278. TEST_F(SourceBufferStreamTest, AudioRangeEndTimeCases) {
  4279. // With a basic range containing just a single keyframe [10,20), verify
  4280. // various keyframe overlap append cases' results on the range end time.
  4281. const AudioEndTimeCase kCases[] = {
  4282. {0, 10, 10, 20, false},
  4283. {20, 1, 20, 21, false},
  4284. {15, 3, 15, 18, true},
  4285. {15, 5, 15, 20, true},
  4286. {15, 8, 15, 23, true},
  4287. // Cases where the new frame removes the previous frame:
  4288. {10, 3, 10, 13, false},
  4289. {10, 10, 10, 20, false},
  4290. {10, 13, 10, 23, false},
  4291. {5, 8, 5, 13, false},
  4292. {5, 15, 5, 20, false},
  4293. {5, 20, 5, 25, false}};
  4294. SetAudioStream();
  4295. for (const auto& c : kCases) {
  4296. InSequence s;
  4297. RemoveInMs(0, 100, 100);
  4298. NewCodedFrameGroupAppend("10D10K");
  4299. CheckExpectedRangesByTimestamp("{ [10,20) }");
  4300. CheckExpectedRangeEndTimes("{ <10,20> }");
  4301. std::stringstream ss;
  4302. ss << c.new_frame_pts << "D" << c.new_frame_duration << "K";
  4303. if (c.expect_splice) {
  4304. EXPECT_MEDIA_LOG(TrimmedSpliceOverlap(c.new_frame_pts * 1000, 10000,
  4305. (20 - c.new_frame_pts) * 1000));
  4306. }
  4307. DVLOG(1) << "Appending " << ss.str();
  4308. NewCodedFrameGroupAppend(ss.str());
  4309. std::stringstream expected;
  4310. expected << "{ <" << c.expected_highest_pts << "," << c.expected_end_time
  4311. << "> }";
  4312. CheckExpectedRangeEndTimes(expected.str());
  4313. }
  4314. }
  4315. TEST_F(SourceBufferStreamTest, SameTimestampEstimatedDurations_Video) {
  4316. // Start a coded frame group with a frame having a non-estimated duration.
  4317. NewCodedFrameGroupAppend("10D10K");
  4318. // In the same coded frame group, append a same-timestamp frame with estimated
  4319. // duration smaller than the first frame. (This can happen at least if there
  4320. // was an intervening init segment resetting the estimation logic.) This
  4321. // second frame need not be a keyframe. We use a non-keyframe here to
  4322. // differentiate the buffers in CheckExpectedBuffers(), below.
  4323. AppendBuffers("10D9E");
  4324. // The next append, which triggered https://crbug.com/761567, didn't need to
  4325. // be with same timestamp as the earlier ones; it just needs to be in the same
  4326. // buffered range. Also, it doesn't need to be a keyframe, have an estimated
  4327. // duration, nor be in the same coded frame group to trigger that issue.
  4328. NewCodedFrameGroupAppend("11D10K");
  4329. Seek(0);
  4330. CheckExpectedRangesByTimestamp("{ [10,21) }");
  4331. CheckExpectedRangeEndTimes("{ <11,21> }");
  4332. CheckExpectedBuffers("10K 10 11K");
  4333. CheckNoNextBuffer();
  4334. }
  4335. TEST_F(SourceBufferStreamTest, RangeIsNextInPTS_Simple) {
  4336. // Append a simple GOP where DTS==PTS, perform basic PTS continuity checks.
  4337. NewCodedFrameGroupAppend("10D10K");
  4338. CheckIsNextInPTSSequenceWithFirstRange(9, false);
  4339. CheckIsNextInPTSSequenceWithFirstRange(10, true);
  4340. CheckIsNextInPTSSequenceWithFirstRange(20, true);
  4341. CheckIsNextInPTSSequenceWithFirstRange(30, true);
  4342. CheckIsNextInPTSSequenceWithFirstRange(31, false);
  4343. }
  4344. TEST_F(SourceBufferStreamTest, RangeIsNextInPTS_OutOfOrder) {
  4345. // Append a GOP where DTS != PTS such that a timestamp used as DTS would not
  4346. // be continuous, but used as PTS is, and verify PTS continuity.
  4347. NewCodedFrameGroupAppend("1000|0K 1120|30 1030|60 1060|90 1090|120");
  4348. CheckIsNextInPTSSequenceWithFirstRange(0, false);
  4349. CheckIsNextInPTSSequenceWithFirstRange(30, false);
  4350. CheckIsNextInPTSSequenceWithFirstRange(60, false);
  4351. CheckIsNextInPTSSequenceWithFirstRange(90, false);
  4352. CheckIsNextInPTSSequenceWithFirstRange(120, false);
  4353. CheckIsNextInPTSSequenceWithFirstRange(150, false);
  4354. CheckIsNextInPTSSequenceWithFirstRange(1000, false);
  4355. CheckIsNextInPTSSequenceWithFirstRange(1030, false);
  4356. CheckIsNextInPTSSequenceWithFirstRange(1060, false);
  4357. CheckIsNextInPTSSequenceWithFirstRange(1090, false);
  4358. CheckIsNextInPTSSequenceWithFirstRange(1119, false);
  4359. CheckIsNextInPTSSequenceWithFirstRange(1120, true);
  4360. CheckIsNextInPTSSequenceWithFirstRange(1150, true);
  4361. CheckIsNextInPTSSequenceWithFirstRange(1180, true);
  4362. CheckIsNextInPTSSequenceWithFirstRange(1181, false);
  4363. }
  4364. TEST_F(SourceBufferStreamTest, RangeCoalescenceOnFudgeRoomIncrease_1) {
  4365. // Change the fudge room (by increasing frame duration) and verify coalescence
  4366. // behavior.
  4367. NewCodedFrameGroupAppend("0K 10K");
  4368. NewCodedFrameGroupAppend("100K 110K");
  4369. NewCodedFrameGroupAppend("500K 510K");
  4370. CheckExpectedRangesByTimestamp("{ [0,20) [100,120) [500,520) }");
  4371. // Increase the fudge room almost enough to merge the first two buffered
  4372. // ranges.
  4373. NewCodedFrameGroupAppend("1000D44K");
  4374. CheckExpectedRangesByTimestamp("{ [0,20) [100,120) [500,520) [1000,1044) }");
  4375. // Increase the fudge room again to merge the first two buffered ranges.
  4376. NewCodedFrameGroupAppend("2000D45K");
  4377. CheckExpectedRangesByTimestamp(
  4378. "{ [0,120) [500,520) [1000,1044) [2000,2045) }");
  4379. SeekToTimestampMs(0);
  4380. CheckExpectedBuffers("0K 10K 100K 110K");
  4381. CheckNoNextBuffer();
  4382. SeekToTimestampMs(500);
  4383. CheckExpectedBuffers("500K 510K");
  4384. CheckNoNextBuffer();
  4385. SeekToTimestampMs(1000);
  4386. CheckExpectedBuffers("1000K");
  4387. CheckNoNextBuffer();
  4388. SeekToTimestampMs(2000);
  4389. CheckExpectedBuffers("2000K");
  4390. CheckNoNextBuffer();
  4391. }
  4392. TEST_F(SourceBufferStreamTest, RangeCoalescenceOnFudgeRoomIncrease_2) {
  4393. // Change the fudge room (by increasing frame duration) and verify coalescence
  4394. // behavior.
  4395. NewCodedFrameGroupAppend("0K 10K");
  4396. NewCodedFrameGroupAppend("40K 50K 60K");
  4397. CheckExpectedRangesByTimestamp("{ [0,20) [40,70) }");
  4398. // Increase the fudge room to merge the first two buffered ranges.
  4399. NewCodedFrameGroupAppend("1000D20K");
  4400. CheckExpectedRangesByTimestamp("{ [0,70) [1000,1020) }");
  4401. // Try to trigger unsorted ranges, as might occur if the first two buffered
  4402. // ranges were not correctly coalesced.
  4403. NewCodedFrameGroupAppend("45D10K");
  4404. CheckExpectedRangesByTimestamp("{ [0,70) [1000,1020) }");
  4405. SeekToTimestampMs(0);
  4406. CheckExpectedBuffers("0K 10K 40K 45K 60K");
  4407. CheckNoNextBuffer();
  4408. SeekToTimestampMs(1000);
  4409. CheckExpectedBuffers("1000K");
  4410. CheckNoNextBuffer();
  4411. }
  4412. TEST_F(SourceBufferStreamTest, NoRangeGapWhenIncrementallyOverlapped) {
  4413. // Append 2 SAP-Type-1 GOPs continuous in DTS and PTS interval and with frame
  4414. // durations and number of frames per GOP such that the first keyframe by
  4415. // itself would not be considered "adjacent" to the second GOP by our fudge
  4416. // room logic alone, but we now adjust the range start times occurring during
  4417. // an overlap to enable overlap appends to remain continuous with the
  4418. // remainder of the overlapped range, if any. Then incrementally reappend
  4419. // each frame of the first GOP.
  4420. NewCodedFrameGroupAppend("0K 10 20 30 40 50K 60 70 80 90");
  4421. Seek(0);
  4422. CheckExpectedRangesByTimestamp("{ [0,100) }");
  4423. CheckExpectedRangeEndTimes("{ <90,100> }");
  4424. CheckExpectedBuffers("0K 10 20 30 40 50K 60 70 80 90");
  4425. CheckNoNextBuffer();
  4426. NewCodedFrameGroupAppend("0D10K"); // Replaces first GOP with 1 frame.
  4427. Seek(0);
  4428. CheckExpectedRangesByTimestamp("{ [0,100) }");
  4429. CheckExpectedRangeEndTimes("{ <90,100> }");
  4430. CheckExpectedBuffers("0K 50K 60 70 80 90");
  4431. CheckNoNextBuffer();
  4432. // Add more of the replacement GOP.
  4433. AppendBuffers("10 20");
  4434. Seek(0);
  4435. CheckExpectedRangesByTimestamp("{ [0,100) }");
  4436. CheckExpectedRangeEndTimes("{ <90,100> }");
  4437. CheckExpectedBuffers("0K 10 20 50K 60 70 80 90");
  4438. CheckNoNextBuffer();
  4439. // Add more of the replacement GOP.
  4440. AppendBuffers("30D10");
  4441. Seek(0);
  4442. CheckExpectedRangesByTimestamp("{ [0,100) }");
  4443. CheckExpectedRangeEndTimes("{ <90,100> }");
  4444. CheckExpectedBuffers("0K 10 20 30 50K 60 70 80 90");
  4445. CheckNoNextBuffer();
  4446. // Complete the replacement GOP.
  4447. AppendBuffers("40D10");
  4448. Seek(0);
  4449. CheckExpectedRangesByTimestamp("{ [0,100) }");
  4450. CheckExpectedRangeEndTimes("{ <90,100> }");
  4451. CheckExpectedBuffers("0K 10 20 30 40 50K 60 70 80 90");
  4452. CheckNoNextBuffer();
  4453. }
  4454. TEST_F(SourceBufferStreamTest, AllowIncrementalAppendsToCoalesceRangeGap) {
  4455. // Append a SAP-Type-1 GOP with a coded frame group start time far before the
  4456. // timestamp of the first GOP (beyond any fudge room possible in this test).
  4457. // This simulates one of multiple muxed tracks with jagged start times
  4458. // following a discontinuity.
  4459. // Then incrementally append a preceding SAP-Type-1 GOP with frames that
  4460. // eventually are adjacent within fudge room of the first appended GOP's group
  4461. // start time and observe the buffered range and demux gap coalesces. Finally,
  4462. // incrementally append more frames of that preceding GOP to fill in the
  4463. // timeline to abut the first appended GOP's keyframe timestamp and observe no
  4464. // further buffered range change or discontinuity.
  4465. NewCodedFrameGroupAppend(base::Milliseconds(100), "150K 160");
  4466. SeekToTimestampMs(100);
  4467. CheckExpectedRangesByTimestamp("{ [100,170) }");
  4468. CheckExpectedRangeEndTimes("{ <160,170> }");
  4469. CheckExpectedBuffers("150K 160");
  4470. CheckNoNextBuffer();
  4471. NewCodedFrameGroupAppend("70D10K");
  4472. SeekToTimestampMs(70);
  4473. CheckExpectedRangesByTimestamp("{ [70,80) [100,170) }");
  4474. CheckExpectedRangeEndTimes("{ <70,80> <160,170> }");
  4475. CheckExpectedBuffers("70K");
  4476. CheckNoNextBuffer();
  4477. SeekToTimestampMs(100);
  4478. CheckExpectedBuffers("150K 160");
  4479. CheckNoNextBuffer();
  4480. AppendBuffers("80D10"); // 80ms is just close enough to 100ms to coalesce.
  4481. SeekToTimestampMs(70);
  4482. CheckExpectedRangesByTimestamp("{ [70,170) }");
  4483. CheckExpectedRangeEndTimes("{ <160,170> }");
  4484. CheckExpectedBuffers("70K 80 150K 160");
  4485. CheckNoNextBuffer();
  4486. AppendBuffers("90D10");
  4487. SeekToTimestampMs(70);
  4488. CheckExpectedRangesByTimestamp("{ [70,170) }");
  4489. CheckExpectedRangeEndTimes("{ <160,170> }");
  4490. CheckExpectedBuffers("70K 80 90 150K 160");
  4491. CheckNoNextBuffer();
  4492. AppendBuffers("100 110 120");
  4493. SeekToTimestampMs(70);
  4494. CheckExpectedRangesByTimestamp("{ [70,170) }");
  4495. CheckExpectedRangeEndTimes("{ <160,170> }");
  4496. CheckExpectedBuffers("70K 80 90 100 110 120 150K 160");
  4497. CheckNoNextBuffer();
  4498. AppendBuffers("130D10");
  4499. SeekToTimestampMs(70);
  4500. CheckExpectedRangesByTimestamp("{ [70,170) }");
  4501. CheckExpectedRangeEndTimes("{ <160,170> }");
  4502. CheckExpectedBuffers("70K 80 90 100 110 120 130 150K 160");
  4503. CheckNoNextBuffer();
  4504. AppendBuffers("140D10");
  4505. SeekToTimestampMs(70);
  4506. CheckExpectedRangesByTimestamp("{ [70,170) }");
  4507. CheckExpectedRangeEndTimes("{ <160,170> }");
  4508. CheckExpectedBuffers("70K 80 90 100 110 120 130 140 150K 160");
  4509. CheckNoNextBuffer();
  4510. }
  4511. TEST_F(SourceBufferStreamTest, PreciselyOverlapLastAudioFrameAppended_1) {
  4512. // Appends an audio frame, A, which is then immediately followed by a
  4513. // subsequent frame, B. Then appends a new frame, C, which precisely overlaps
  4514. // frame B, and verifies that there is exactly 1 buffered range resulting.
  4515. SetAudioStream();
  4516. // Frame A
  4517. NewCodedFrameGroupAppend("0D10K");
  4518. SeekToTimestampMs(0);
  4519. CheckExpectedRangesByTimestamp("{ [0,10) }");
  4520. CheckExpectedRangeEndTimes("{ <0,10> }");
  4521. CheckExpectedBuffers("0K");
  4522. CheckNoNextBuffer();
  4523. // Frame B
  4524. NewCodedFrameGroupAppend("10D10K");
  4525. SeekToTimestampMs(0);
  4526. CheckExpectedRangesByTimestamp("{ [0,20) }");
  4527. CheckExpectedRangeEndTimes("{ <10,20> }");
  4528. CheckExpectedBuffers("0K 10K");
  4529. CheckNoNextBuffer();
  4530. // Frame C.
  4531. // Though DTS is continuous per MSE spec, FrameProcessor signals new CFG more
  4532. // granularly, including in this case.
  4533. NewCodedFrameGroupAppend("10D10K");
  4534. SeekToTimestampMs(0);
  4535. CheckExpectedRangesByTimestamp("{ [0,20) }");
  4536. CheckExpectedRangeEndTimes("{ <10,20> }");
  4537. CheckExpectedBuffers("0K 10K");
  4538. CheckNoNextBuffer();
  4539. }
  4540. TEST_F(SourceBufferStreamTest, PreciselyOverlapLastAudioFrameAppended_2) {
  4541. // Appends an audio frame, A, which is then splice-trim-truncated by a
  4542. // subsequent frame, B. Then appends a new frame, C, which precisely overlaps
  4543. // frame B, and verifies that there is exactly 1 buffered range resulting.
  4544. SetAudioStream();
  4545. // Frame A
  4546. NewCodedFrameGroupAppend("0D100K");
  4547. SeekToTimestampMs(0);
  4548. CheckExpectedRangesByTimestamp("{ [0,100) }");
  4549. CheckExpectedRangeEndTimes("{ <0,100> }");
  4550. CheckExpectedBuffers("0K");
  4551. CheckNoNextBuffer();
  4552. // Frame B
  4553. EXPECT_MEDIA_LOG(TrimmedSpliceOverlap(60000, 0, 40000));
  4554. NewCodedFrameGroupAppend("60D10K");
  4555. SeekToTimestampMs(0);
  4556. CheckExpectedRangesByTimestamp("{ [0,70) }");
  4557. CheckExpectedRangeEndTimes("{ <60,70> }");
  4558. CheckExpectedBuffers("0K 60K");
  4559. CheckNoNextBuffer();
  4560. // Frame C.
  4561. // Though DTS is continuous per MSE spec, FrameProcessor signals new CFG more
  4562. // granularly, including in this case.
  4563. NewCodedFrameGroupAppend("60D10K");
  4564. SeekToTimestampMs(0);
  4565. CheckExpectedRangesByTimestamp("{ [0,70) }");
  4566. CheckExpectedRangeEndTimes("{ <60,70> }");
  4567. CheckExpectedBuffers("0K 60K");
  4568. CheckNoNextBuffer();
  4569. }
  4570. TEST_F(SourceBufferStreamTest, ZeroDurationBuffersThenIncreasingFudgeRoom) {
  4571. // Appends some zero duration buffers to result in disjoint buffered ranges.
  4572. // Verifies that increasing the fudge room allows those that become within
  4573. // adjacency threshold to merge, including those for which the new fudge room
  4574. // is well more than sufficient to let them be adjacent.
  4575. SetAudioStream();
  4576. NewCodedFrameGroupAppend("0uD0K");
  4577. CheckExpectedRangesByTimestamp("{ [0,1) }", TimeGranularity::kMicrosecond);
  4578. NewCodedFrameGroupAppend("1uD0K");
  4579. CheckExpectedRangesByTimestamp("{ [0,2) }", TimeGranularity::kMicrosecond);
  4580. // Initial fudge room allows for up to 2ms gap to coalesce.
  4581. NewCodedFrameGroupAppend("5000uD0K");
  4582. CheckExpectedRangesByTimestamp("{ [0,2) [5000,5001) }",
  4583. TimeGranularity::kMicrosecond);
  4584. NewCodedFrameGroupAppend("2002uD0K");
  4585. CheckExpectedRangesByTimestamp("{ [0,2) [2002,2003) [5000,5001) }",
  4586. TimeGranularity::kMicrosecond);
  4587. // Grow the fudge room enough to coalesce the first two ranges.
  4588. NewCodedFrameGroupAppend("8000uD1001uK");
  4589. CheckExpectedRangesByTimestamp("{ [0,2003) [5000,5001) [8000,9001) }",
  4590. TimeGranularity::kMicrosecond);
  4591. // Append a buffer with duration 4ms, much larger than previous buffers. This
  4592. // grows the fudge room to 8ms (2 * 4ms). Expect that the first three ranges
  4593. // are retroactively merged due to being adjacent per the new, larger fudge
  4594. // room.
  4595. NewCodedFrameGroupAppend("100D4K");
  4596. CheckExpectedRangesByTimestamp("{ [0,9001) [100000,104000) }",
  4597. TimeGranularity::kMicrosecond);
  4598. SeekToTimestampMs(0);
  4599. CheckExpectedBuffers("0K 1K 2002K 5000K 8000K",
  4600. TimeGranularity::kMicrosecond);
  4601. CheckNoNextBuffer();
  4602. SeekToTimestampMs(100);
  4603. CheckExpectedBuffers("100K");
  4604. CheckNoNextBuffer();
  4605. }
  4606. TEST_F(SourceBufferStreamTest, NonZeroDurationBuffersThenIncreasingFudgeRoom) {
  4607. // Verifies that a single fudge room increase which merges more than 2
  4608. // previously disjoint ranges in a row performs the merging correctly.
  4609. NewCodedFrameGroupAppend("0D10K");
  4610. NewCodedFrameGroupAppend("50D10K");
  4611. NewCodedFrameGroupAppend("100D10K");
  4612. NewCodedFrameGroupAppend("150D10K");
  4613. NewCodedFrameGroupAppend("500D10K");
  4614. CheckExpectedRangesByTimestamp(
  4615. "{ [0,10) [50,60) [100,110) [150,160) [500,510) }");
  4616. NewCodedFrameGroupAppend("600D30K");
  4617. CheckExpectedRangesByTimestamp("{ [0,160) [500,510) [600,630) }");
  4618. SeekToTimestampMs(0);
  4619. CheckExpectedBuffers("0K 50K 100K 150K");
  4620. CheckNoNextBuffer();
  4621. SeekToTimestampMs(500);
  4622. CheckExpectedBuffers("500K");
  4623. CheckNoNextBuffer();
  4624. SeekToTimestampMs(600);
  4625. CheckExpectedBuffers("600K");
  4626. CheckNoNextBuffer();
  4627. }
  4628. TEST_F(SourceBufferStreamTest, SapType2WithNonkeyframePtsInEarlierRange) {
  4629. // Buffer a standalone GOP [0,10).
  4630. NewCodedFrameGroupAppend("0D10K");
  4631. CheckExpectedRangesByTimestamp("{ [0,10) }");
  4632. // Following discontinuity (simulated by DTS gap, signalled by new coded frame
  4633. // group with time beyond fudge room of [0,10)), buffer 2 new GOPs in a later
  4634. // range: a SAP-2 GOP with a nonkeyframe with PTS belonging to the first
  4635. // range, and a subsequent minimal GOP.
  4636. NewCodedFrameGroupAppend("30D10K 1|40D10");
  4637. CheckExpectedRangesByTimestamp("{ [0,10) [30,40) }");
  4638. NewCodedFrameGroupAppend("40|50D10K");
  4639. // Verify that there are two distinct ranges, and that the SAP-2 nonkeyframe
  4640. // is buffered as part of the second range's first GOP.
  4641. CheckExpectedRangesByTimestamp("{ [0,10) [30,50) }");
  4642. SeekToTimestampMs(0);
  4643. CheckExpectedBuffers("0K");
  4644. CheckNoNextBuffer();
  4645. SeekToTimestampMs(30);
  4646. CheckExpectedBuffers("30K 1|40 40|50K");
  4647. CheckNoNextBuffer();
  4648. }
  4649. TEST_F(SourceBufferStreamTest,
  4650. MergeAllowedIfRangeEndTimeWithEstimatedDurationMatchesNextRangeStart) {
  4651. // Tests the edge case where fudge room is not increased when an estimated
  4652. // duration is increased due to overlap appends, causing two ranges to not be
  4653. // within fudge room of each other (nor merged), yet abutting each other.
  4654. // Append a GOP that has fudge room as its interval (e.g. 2 frames of same
  4655. // duration >= minimum 1ms).
  4656. NewCodedFrameGroupAppend("0D10K 10D10");
  4657. CheckExpectedRangesByTimestamp("{ [0,20) }");
  4658. // Trigger a DTS discontinuity so later 21ms append also is discontinuous and
  4659. // retains 10ms*2 fudge room.
  4660. NewCodedFrameGroupAppend("100D10K");
  4661. CheckExpectedRangesByTimestamp("{ [0,20) [100,110) }");
  4662. // Append another keyframe that starts within fudge room distance of the
  4663. // non-keyframe in the GOP appended, above.
  4664. NewCodedFrameGroupAppend("21D10K");
  4665. CheckExpectedRangesByTimestamp("{ [0,31) [100,110) }");
  4666. // Overlap-append the original GOP with a single estimated-duration keyframe.
  4667. // Though its timestamp is not within fudge room of the next keyframe, that
  4668. // next keyframe at time 21ms was in the overlapped range and is retained in
  4669. // the result of the overlap append's range.
  4670. NewCodedFrameGroupAppend("0D10EK");
  4671. CheckExpectedRangesByTimestamp("{ [0,31) [100,110) }");
  4672. // That new keyframe at time 0 now has derived estimated duration 21ms. That
  4673. // increased estimated duration did *not* increase the fudge room (which is
  4674. // still 2 * 10ms = 20ms.) So the next line, which splices in a new frame at
  4675. // time 21 causes the estimated keyframe at time 0 to not have a timestamp
  4676. // within fudge room of the new range that starts right at 21ms, the same time
  4677. // that ends the first buffered range, requiring CanAppendBuffersToEnd to
  4678. // handle this scenario specifically.
  4679. NewCodedFrameGroupAppend("21D10K");
  4680. CheckExpectedRangesByTimestamp("{ [0,31) [100,110) }");
  4681. SeekToTimestampMs(0);
  4682. CheckExpectedBuffers("0D21EK 21D10K");
  4683. CheckNoNextBuffer();
  4684. SeekToTimestampMs(100);
  4685. CheckExpectedBuffers("100D10K");
  4686. CheckNoNextBuffer();
  4687. }
  4688. } // namespace media