MediaPlayerCtrl.cs 108 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695
  1. using UnityEngine;
  2. using System.Runtime.InteropServices;
  3. using System;
  4. using System.IO;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine.UI;
  8. #if !UNITY_WEBPLAYER && !UNITY_WEBGL && !UNITY_WP8 && !UNITY_WP8_1
  9. using FFmpeg.AutoGen;
  10. using System.Threading;
  11. #endif
  12. #if UNITY_EDITOR || UNITY_STANDALONE
  13. public unsafe class MediaPlayerCtrl : MonoBehaviour {
  14. #else
  15. public class MediaPlayerCtrl : MonoBehaviour
  16. {
  17. #endif
  18. const int BUFFER_SIZE = 256;
  19. public string m_strFileName;
  20. public GameObject[] m_TargetMaterial = null;
  21. private Texture2D m_VideoTexture = null;
  22. private Texture2D m_VideoTextureDummy = null;
  23. private Texture2D m_VideoTextureY;
  24. private Texture2D m_VideoTextureU;
  25. private Texture2D m_VideoTextureV;
  26. private Texture2D m_VideoTextureDummyY;
  27. private Texture2D m_VideoTextureDummyU;
  28. private Texture2D m_VideoTextureDummyV;
  29. private MEDIAPLAYER_STATE m_CurrentState;
  30. private int m_iCurrentSeekPosition;
  31. private float m_fVolume = 1.0f;
  32. private int m_iWidth;
  33. private int m_iHeight;
  34. private float m_fSpeed = 1.0f;
  35. public bool m_bFullScreen = false;//Please use only in FullScreen prefab.
  36. public bool m_bSupportRockchip = true; //Using a device support Rochchip or Low-end devices
  37. //(Reason 1 : Not directly play in StreamingAssets)
  38. //(Reason 2 : Video buffer is RGB565 only supported)
  39. public bool m_bPC_FastMode = false;
  40. public delegate void VideoEnd();
  41. public delegate void VideoReady();
  42. public delegate void VideoError(MEDIAPLAYER_ERROR errorCode, MEDIAPLAYER_ERROR errorCodeExtra);
  43. public delegate void VideoFirstFrameReady();
  44. public delegate void VideoResize ();
  45. public VideoResize OnResize;
  46. public VideoReady OnReady;
  47. public VideoEnd OnEnd;
  48. public VideoError OnVideoError;
  49. public VideoFirstFrameReady OnVideoFirstFrameReady;
  50. private IntPtr m_texPtr;
  51. private IntPtr m_texPtrY;
  52. private IntPtr m_texPtrU;
  53. private IntPtr m_texPtrV;
  54. public Shader m_shaderYUV;
  55. private int m_iPauseFrame;
  56. #if UNITY_ANDROID && !UNITY_EDITOR && (UNITY_5 || UNITY_5_3_OR_NEWER)
  57. [DllImport ("BlueDoveMediaRender")]
  58. private static extern void InitNDK();
  59. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  60. [DllImport ("BlueDoveMediaRender")]
  61. private static extern IntPtr EasyMovieTextureRender();
  62. #endif
  63. #endif
  64. #if (UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN) && !UNITY_EDITOR_OSX
  65. [DllImport ("EasyMovieTexture")]
  66. private static extern int SetTexture();
  67. [DllImport ("EasyMovieTexture")]
  68. private static extern void ReleaseTexture(int iID);
  69. [DllImport ("EasyMovieTexture")]
  70. private static extern void SetTextureFromUnity(int iID,System.IntPtr texture,System.IntPtr textureY,System.IntPtr textureU,System.IntPtr textureV, int w, int h,byte[] data);
  71. [DllImport("EasyMovieTexture")]
  72. private static extern IntPtr GetRenderEventFunc();
  73. #endif
  74. #if (UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX )
  75. [DllImport ("EasyMovieTextureRender")]
  76. private static extern int SetTexture();
  77. [DllImport ("EasyMovieTextureRender")]
  78. private static extern void ReleaseTexture(int iID);
  79. [DllImport ("EasyMovieTextureRender")]
  80. private static extern void SetTextureFromUnity(int iID,System.IntPtr texture,System.IntPtr textureY,System.IntPtr textureU,System.IntPtr textureV, int w, int h,byte[] data);
  81. [DllImport("EasyMovieTextureRender")]
  82. private static extern IntPtr GetRenderEventFunc();
  83. #endif
  84. private int m_iAndroidMgrID;
  85. private bool m_bIsFirstFrameReady;
  86. public enum MEDIAPLAYER_ERROR
  87. {
  88. MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK = 200,
  89. MEDIA_ERROR_IO = -1004,
  90. MEDIA_ERROR_MALFORMED = -1007,
  91. MEDIA_ERROR_TIMED_OUT = -110,
  92. MEDIA_ERROR_UNSUPPORTED = -1010,
  93. MEDIA_ERROR_SERVER_DIED = 100,
  94. MEDIA_ERROR_UNKNOWN = 1
  95. }
  96. public enum MEDIAPLAYER_STATE
  97. {
  98. NOT_READY = 0,
  99. READY = 1,
  100. END = 2,
  101. PLAYING = 3,
  102. PAUSED = 4,
  103. STOPPED = 5,
  104. ERROR = 6
  105. }
  106. public enum MEDIA_SCALE
  107. {
  108. SCALE_X_TO_Y = 0,
  109. SCALE_X_TO_Z = 1,
  110. SCALE_Y_TO_X = 2,
  111. SCALE_Y_TO_Z = 3,
  112. SCALE_Z_TO_X = 4,
  113. SCALE_Z_TO_Y = 5,
  114. SCALE_X_TO_Y_2 = 6,
  115. }
  116. bool m_bFirst = false;
  117. public MEDIA_SCALE m_ScaleValue;
  118. public GameObject[] m_objResize = null;
  119. public bool m_bLoop = false;
  120. public bool m_bAutoPlay = true;
  121. private bool m_bInit = false;
  122. #if !UNITY_WEBPLAYER && !UNITY_WEBGL && !UNITY_WP8 && !UNITY_WP8_1
  123. static MediaPlayerCtrl()
  124. {
  125. #if UNITY_EDITOR
  126. String currentPath = Environment.GetEnvironmentVariable ("PATH", EnvironmentVariableTarget.Process);
  127. String dllPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Assets" + Path.DirectorySeparatorChar + "Plugins";
  128. if (currentPath.Contains (dllPath) == false) {
  129. Environment.SetEnvironmentVariable ("PATH", currentPath + Path.PathSeparator + dllPath, EnvironmentVariableTarget.Process);
  130. }
  131. dllPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Assets" + Path.DirectorySeparatorChar + "Plugins" + Path.DirectorySeparatorChar + "x64";
  132. if (currentPath.Contains (dllPath) == false) {
  133. Environment.SetEnvironmentVariable ("PATH", currentPath + Path.PathSeparator + dllPath, EnvironmentVariableTarget.Process);
  134. }
  135. dllPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Assets" + Path.DirectorySeparatorChar + "Plugins" + Path.DirectorySeparatorChar + "x86";
  136. if (currentPath.Contains (dllPath) == false) {
  137. Environment.SetEnvironmentVariable ("PATH", currentPath + Path.PathSeparator + dllPath, EnvironmentVariableTarget.Process);
  138. }
  139. #endif
  140. }
  141. void Awake()
  142. {
  143. if(m_bInit == false)
  144. {
  145. InitPlayer();
  146. }
  147. }
  148. // Use this for initialization
  149. void Start()
  150. {
  151. }
  152. void InitPlayer()
  153. {
  154. #if UNITY_STANDALONE
  155. String currentPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process);
  156. String dllPath = Application.dataPath + Path.DirectorySeparatorChar + "Plugins";
  157. if(currentPath.Contains(dllPath) == false)
  158. {
  159. Environment.SetEnvironmentVariable("PATH", currentPath + Path.PathSeparator + dllPath, EnvironmentVariableTarget.Process);
  160. }
  161. #endif
  162. if (SystemInfo.deviceModel.Contains("rockchip"))
  163. {
  164. m_bSupportRockchip = true;
  165. }
  166. else
  167. {
  168. m_bSupportRockchip = false;
  169. }
  170. #if UNITY_IPHONE || UNITY_TVOS || UNITY_EDITOR || UNITY_STANDALONE
  171. if (m_TargetMaterial != null)
  172. {
  173. for (int iIndex = 0; iIndex < m_TargetMaterial.Length; iIndex++)
  174. {
  175. if (m_TargetMaterial[iIndex] != null)
  176. {
  177. if (m_TargetMaterial[iIndex].GetComponent<MeshFilter>() != null)
  178. {
  179. Vector2[] vec2UVs = m_TargetMaterial[iIndex].GetComponent<MeshFilter>().mesh.uv;
  180. for (int i = 0; i < vec2UVs.Length; i++)
  181. {
  182. vec2UVs[i] = new Vector2(vec2UVs[i].x, 1.0f - vec2UVs[i].y);
  183. }
  184. m_TargetMaterial[iIndex].GetComponent<MeshFilter>().mesh.uv = vec2UVs;
  185. }
  186. if (m_TargetMaterial[iIndex].GetComponent<RawImage>() != null)
  187. {
  188. m_TargetMaterial[iIndex].GetComponent<RawImage>().uvRect = new Rect(0, 1, 1, -1);
  189. }
  190. }
  191. }
  192. }
  193. #endif
  194. #if UNITY_STANDALONE || UNITY_EDITOR
  195. //RegisterDebugCallback(new DebugCallback(DebugMethod));
  196. //threadVideo = new Thread(ThreadUpdate);
  197. //threadVideo.Start();
  198. if (m_queuePacketVideo == null)
  199. m_queuePacketVideo = new Queue<IntPtr>();
  200. if (m_queuePacketSound == null)
  201. m_queuePacketSound = new Queue<IntPtr>();
  202. #endif
  203. #if UNITY_ANDROID && !UNITY_EDITOR
  204. #if UNITY_5 || UNITY_5_3_OR_NEWER
  205. if( SystemInfo.graphicsMultiThreaded == true)
  206. InitNDK();
  207. #endif
  208. m_iAndroidMgrID = Call_InitNDK();
  209. #endif
  210. Call_SetUnityActivity();
  211. #if UNITY_ANDROID
  212. if (Application.dataPath.Contains(".obb"))
  213. {
  214. Call_SetSplitOBB(true, Application.dataPath);
  215. }
  216. else
  217. {
  218. Call_SetSplitOBB(false, null);
  219. }
  220. #endif
  221. m_bInit = true;
  222. }
  223. void OnApplicationQuit()
  224. {
  225. //if (System.IO.Directory.Exists(Application.persistentDataPath + "/Data") == true)
  226. // System.IO.Directory.Delete(Application.persistentDataPath + "/Data", true);
  227. }
  228. bool m_bCheckFBO = false;
  229. void OnDisable()
  230. {
  231. if (GetCurrentState() == MEDIAPLAYER_STATE.PLAYING)
  232. {
  233. Pause();
  234. }
  235. }
  236. void OnEnable()
  237. {
  238. if (GetCurrentState() == MEDIAPLAYER_STATE.PAUSED)
  239. {
  240. Play();
  241. }
  242. }
  243. void Update()
  244. {
  245. if (string.IsNullOrEmpty(m_strFileName))
  246. {
  247. return;
  248. }
  249. if (checkNewActions)
  250. {
  251. checkNewActions = false;
  252. CheckThreading ();
  253. }
  254. if(m_bAutoPlay == true)
  255. {
  256. if (m_bFirst == false)
  257. {
  258. string strName = m_strFileName.Trim();
  259. #if UNITY_IPHONE || UNITY_TVOS
  260. /*if (strName.StartsWith("http",StringComparison.OrdinalIgnoreCase))
  261. {
  262. StartCoroutine( DownloadStreamingVideoAndLoad(strName) );
  263. }
  264. else*/
  265. {
  266. Call_Load(strName,0);
  267. }
  268. #elif UNITY_ANDROID
  269. if (m_bSupportRockchip)
  270. {
  271. Call_SetRockchip(m_bSupportRockchip);
  272. if (strName.Contains("://"))
  273. {
  274. Call_Load(strName, 0);
  275. }
  276. else
  277. {
  278. //Call_Load(strName,0);
  279. StartCoroutine(CopyStreamingAssetVideoAndLoad(strName));
  280. }
  281. }
  282. else
  283. {
  284. Call_Load(strName, 0);
  285. }
  286. #elif UNITY_STANDALONE
  287. Call_Load(strName,0);
  288. #endif
  289. Call_SetLooping(m_bLoop);
  290. m_bFirst = true;
  291. }
  292. }
  293. else
  294. {
  295. m_bFirst = true;
  296. }
  297. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED)
  298. {
  299. if (m_bCheckFBO == false) {
  300. if (Call_GetVideoWidth () <= 0 || Call_GetVideoHeight () <= 0) {
  301. return;
  302. }
  303. m_iWidth = Call_GetVideoWidth ();
  304. m_iHeight = Call_GetVideoHeight ();
  305. Resize ();
  306. if (m_VideoTexture != null) {
  307. //Destroy(m_VideoTexture);
  308. if (m_VideoTextureDummy != null) {
  309. Destroy (m_VideoTextureDummy);
  310. m_VideoTextureDummy = null;
  311. }
  312. m_VideoTextureDummy = m_VideoTexture;
  313. m_VideoTexture = null;
  314. }
  315. if (m_VideoTextureY != null) {
  316. if (m_VideoTextureDummyY != null) {
  317. Destroy (m_VideoTextureDummyY);
  318. m_VideoTextureDummyY = null;
  319. }
  320. m_VideoTextureDummyY = m_VideoTextureY;
  321. m_VideoTextureY = null;
  322. }
  323. if (m_VideoTextureU != null) {
  324. if (m_VideoTextureDummyU != null) {
  325. Destroy (m_VideoTextureDummyU);
  326. m_VideoTextureDummyU = null;
  327. }
  328. m_VideoTextureDummyU = m_VideoTextureU;
  329. m_VideoTextureU = null;
  330. }
  331. if (m_VideoTextureV != null) {
  332. if (m_VideoTextureDummyV != null) {
  333. Destroy (m_VideoTextureDummyV);
  334. m_VideoTextureDummyV = null;
  335. }
  336. m_VideoTextureDummyV = m_VideoTextureV;
  337. m_VideoTextureV = null;
  338. }
  339. #if UNITY_ANDROID || UNITY_EDITOR || UNITY_STANDALONE
  340. #if UNITY_EDITOR || UNITY_STANDALONE
  341. if(m_bPC_FastMode == true)
  342. {
  343. m_VideoTextureY = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.Alpha8, false);
  344. m_VideoTextureU = new Texture2D (Call_GetVideoWidth () / 2, Call_GetVideoHeight () / 2, TextureFormat.Alpha8, false);
  345. m_VideoTextureV = new Texture2D (Call_GetVideoWidth () / 2, Call_GetVideoHeight () / 2, TextureFormat.Alpha8, false);
  346. m_VideoTextureY.filterMode = FilterMode.Bilinear;
  347. m_VideoTextureY.wrapMode = TextureWrapMode.Clamp;
  348. m_VideoTextureU.filterMode = FilterMode.Bilinear;
  349. m_VideoTextureU.wrapMode = TextureWrapMode.Clamp;
  350. m_VideoTextureV.filterMode = FilterMode.Bilinear;
  351. m_VideoTextureV.wrapMode = TextureWrapMode.Clamp;
  352. }
  353. else
  354. {
  355. if (m_bSupportRockchip) {
  356. m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGB565, false);
  357. } else {
  358. m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGBA32, false);
  359. }
  360. m_VideoTexture.filterMode = FilterMode.Bilinear;
  361. m_VideoTexture.wrapMode = TextureWrapMode.Clamp;
  362. }
  363. if(m_bPC_FastMode == true)
  364. {
  365. m_texPtrY = m_VideoTextureY.GetNativeTexturePtr ();
  366. m_texPtrU = m_VideoTextureU.GetNativeTexturePtr ();
  367. m_texPtrV = m_VideoTextureV.GetNativeTexturePtr ();
  368. }
  369. else
  370. {
  371. m_texPtr = m_VideoTexture.GetNativeTexturePtr ();
  372. }
  373. #else
  374. if (m_bSupportRockchip) {
  375. m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGB565, false);
  376. } else {
  377. m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGBA32, false);
  378. }
  379. m_VideoTexture.filterMode = FilterMode.Bilinear;
  380. m_VideoTexture.wrapMode = TextureWrapMode.Clamp;
  381. m_texPtr = m_VideoTexture.GetNativeTexturePtr ();
  382. #endif
  383. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  384. Call_SetUnityTexture((int)m_texPtr);
  385. #else
  386. Call_SetUnityTexture (m_VideoTexture.GetNativeTextureID ());
  387. #endif
  388. #endif
  389. Call_SetWindowSize ();
  390. m_bCheckFBO = true;
  391. if (OnResize != null)
  392. OnResize ();
  393. } else {
  394. if (Call_GetVideoWidth () != m_iWidth || Call_GetVideoHeight () != m_iHeight) {
  395. m_iWidth = Call_GetVideoWidth ();
  396. m_iHeight = Call_GetVideoHeight ();
  397. if (OnResize != null)
  398. OnResize ();
  399. ResizeTexture ();
  400. }
  401. }
  402. Call_UpdateVideoTexture();
  403. m_iCurrentSeekPosition = Call_GetSeekPosition();
  404. }
  405. if (m_CurrentState != Call_GetStatus())
  406. {
  407. m_CurrentState = Call_GetStatus();
  408. if (m_CurrentState == MEDIAPLAYER_STATE.READY)
  409. {
  410. if(m_bPause)
  411. {
  412. m_bPause = false;
  413. Call_SetSeekPosition(m_iPauseFrame);
  414. }
  415. else if(m_bPlaying)
  416. {
  417. m_bPlaying = false;
  418. Call_SetSeekPosition(m_iPauseFrame);
  419. Call_Play(m_iPauseFrame);
  420. //StartCoroutine(DelayPlay());
  421. }
  422. else
  423. {
  424. if (OnReady != null)
  425. OnReady();
  426. if (m_bAutoPlay)
  427. Call_Play(0);
  428. if (m_bReadyPlay)
  429. {
  430. Call_Play(0);
  431. m_bReadyPlay = false;
  432. }
  433. SetVolume(m_fVolume);
  434. }
  435. }
  436. else if (m_CurrentState == MEDIAPLAYER_STATE.END)
  437. {
  438. if (OnEnd != null)
  439. OnEnd();
  440. if (m_bLoop == true)
  441. {
  442. Call_Play(0);
  443. }
  444. }
  445. else if (m_CurrentState == MEDIAPLAYER_STATE.ERROR)
  446. {
  447. OnError((MEDIAPLAYER_ERROR)Call_GetError(), (MEDIAPLAYER_ERROR)Call_GetErrorExtra());
  448. }
  449. }
  450. GL.InvalidateState ();
  451. }
  452. public void DeleteVideoTexture()
  453. {
  454. if (m_VideoTextureDummy != null)
  455. {
  456. Destroy(m_VideoTextureDummy);
  457. m_VideoTextureDummy = null;
  458. }
  459. if (m_VideoTexture != null)
  460. {
  461. Destroy(m_VideoTexture);
  462. m_VideoTexture = null;
  463. }
  464. if (m_VideoTextureDummyY != null)
  465. {
  466. Destroy(m_VideoTextureDummyY);
  467. m_VideoTextureDummyY = null;
  468. }
  469. if (m_VideoTextureY != null)
  470. {
  471. Destroy(m_VideoTextureY);
  472. m_VideoTextureY = null;
  473. }
  474. if (m_VideoTextureDummyU != null)
  475. {
  476. Destroy(m_VideoTextureDummyU);
  477. m_VideoTextureDummyU = null;
  478. }
  479. if (m_VideoTextureU != null)
  480. {
  481. Destroy(m_VideoTextureU);
  482. m_VideoTextureU = null;
  483. }
  484. if (m_VideoTextureDummyV != null)
  485. {
  486. Destroy(m_VideoTextureDummyV);
  487. m_VideoTextureDummyV = null;
  488. }
  489. if (m_VideoTextureV != null)
  490. {
  491. Destroy(m_VideoTextureV);
  492. m_VideoTextureV = null;
  493. }
  494. }
  495. public void ResizeTexture()
  496. {
  497. Debug.Log ("ResizeTexture " + m_iWidth + " " + m_iHeight);
  498. if (m_iWidth == 0 || m_iHeight == 0)
  499. return;
  500. if (m_VideoTexture != null) {
  501. //Destroy(m_VideoTexture);
  502. if (m_VideoTextureDummy != null) {
  503. Destroy (m_VideoTextureDummy);
  504. m_VideoTextureDummy = null;
  505. }
  506. m_VideoTextureDummy = m_VideoTexture;
  507. m_VideoTexture = null;
  508. }
  509. if (m_VideoTextureY != null) {
  510. if (m_VideoTextureDummyY != null) {
  511. Destroy (m_VideoTextureDummyY);
  512. m_VideoTextureDummyY = null;
  513. }
  514. m_VideoTextureDummyY = m_VideoTextureY;
  515. m_VideoTextureY = null;
  516. }
  517. if (m_VideoTextureU != null) {
  518. if (m_VideoTextureDummyU != null) {
  519. Destroy (m_VideoTextureDummyU);
  520. m_VideoTextureDummyU = null;
  521. }
  522. m_VideoTextureDummyU = m_VideoTextureU;
  523. m_VideoTextureU = null;
  524. }
  525. if (m_VideoTextureV != null) {
  526. if (m_VideoTextureDummyV != null) {
  527. Destroy (m_VideoTextureDummyV);
  528. m_VideoTextureDummyV = null;
  529. }
  530. m_VideoTextureDummyV = m_VideoTextureV;
  531. m_VideoTextureV = null;
  532. }
  533. #if UNITY_IPHONE && !UNITY_EDITOR
  534. _videoTexture =null;
  535. bFirstIOS = true;
  536. #endif
  537. #if UNITY_ANDROID || UNITY_EDITOR || UNITY_STANDALONE
  538. #if UNITY_EDITOR || UNITY_STANDALONE
  539. if (m_bPC_FastMode == true)
  540. {
  541. m_VideoTextureY = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.Alpha8, false);
  542. m_VideoTextureU = new Texture2D (Call_GetVideoWidth () / 2, Call_GetVideoHeight () / 2, TextureFormat.Alpha8, false);
  543. m_VideoTextureV = new Texture2D (Call_GetVideoWidth () / 2, Call_GetVideoHeight () / 2, TextureFormat.Alpha8, false);
  544. }
  545. else
  546. {
  547. if (m_bSupportRockchip) {
  548. m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGB565, false);
  549. } else {
  550. m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGBA32, false);
  551. }
  552. }
  553. if( m_VideoTexture != null)
  554. {
  555. m_VideoTexture.filterMode = FilterMode.Bilinear;
  556. m_VideoTexture.wrapMode = TextureWrapMode.Clamp;
  557. }
  558. if(m_bPC_FastMode == true)
  559. {
  560. m_texPtrY = m_VideoTextureY.GetNativeTexturePtr ();
  561. m_texPtrU = m_VideoTextureU.GetNativeTexturePtr ();
  562. m_texPtrV = m_VideoTextureV.GetNativeTexturePtr ();
  563. }
  564. else
  565. {
  566. m_texPtr = m_VideoTexture.GetNativeTexturePtr ();
  567. }
  568. #else
  569. if (m_bSupportRockchip) {
  570. m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGB565, false);
  571. } else {
  572. m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGBA32, false);
  573. }
  574. m_VideoTexture.filterMode = FilterMode.Bilinear;
  575. m_VideoTexture.wrapMode = TextureWrapMode.Clamp;
  576. m_texPtr = m_VideoTexture.GetNativeTexturePtr ();
  577. #endif
  578. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  579. Call_SetUnityTexture((int)m_texPtr);
  580. #else
  581. Call_SetUnityTexture (m_VideoTexture.GetNativeTextureID ());
  582. #endif
  583. #endif
  584. Call_SetWindowSize ();
  585. }
  586. public void Resize()
  587. {
  588. if (m_CurrentState != MEDIAPLAYER_STATE.PLAYING)
  589. return;
  590. if (Call_GetVideoWidth() <= 0 || Call_GetVideoHeight() <= 0)
  591. {
  592. return;
  593. }
  594. if (m_objResize != null)
  595. {
  596. int iScreenWidth = Screen.width;
  597. int iScreenHeight = Screen.height;
  598. float fRatioScreen = (float)iScreenHeight / (float)iScreenWidth;
  599. int iWidth = Call_GetVideoWidth();
  600. int iHeight = Call_GetVideoHeight();
  601. float fRatio = (float)iHeight / (float)iWidth;
  602. float fRatioResult = fRatioScreen / fRatio;
  603. for (int i = 0; i < m_objResize.Length; i++)
  604. {
  605. if (m_objResize[i] == null)
  606. continue;
  607. if (m_bFullScreen)
  608. {
  609. m_objResize[i].transform.localScale = new Vector3(20.0f / fRatioScreen, 20.0f / fRatioScreen, 1.0f);
  610. if (fRatio < 1.0f)
  611. {
  612. if (fRatioScreen < 1.0f)
  613. {
  614. if (fRatio > fRatioScreen)
  615. {
  616. m_objResize[i].transform.localScale *= fRatioResult;
  617. }
  618. }
  619. m_ScaleValue = MEDIA_SCALE.SCALE_X_TO_Y;
  620. }
  621. else
  622. {
  623. if (fRatioScreen > 1.0f) {
  624. if (fRatio >= fRatioScreen) {
  625. m_objResize [i].transform.localScale *= fRatioResult;
  626. }
  627. } else {
  628. m_objResize [i].transform.localScale *= fRatioResult;
  629. }
  630. m_ScaleValue = MEDIA_SCALE.SCALE_X_TO_Y;
  631. }
  632. }
  633. if (m_ScaleValue == MEDIA_SCALE.SCALE_X_TO_Y)
  634. {
  635. m_objResize[i].transform.localScale
  636. = new Vector3(m_objResize[i].transform.localScale.x
  637. , m_objResize[i].transform.localScale.x * fRatio
  638. , m_objResize[i].transform.localScale.z);
  639. }
  640. else if (m_ScaleValue == MEDIA_SCALE.SCALE_X_TO_Y_2)
  641. {
  642. m_objResize[i].transform.localScale
  643. = new Vector3(m_objResize[i].transform.localScale.x
  644. , m_objResize[i].transform.localScale.x * fRatio / 2.0f
  645. , m_objResize[i].transform.localScale.z);
  646. }
  647. else if (m_ScaleValue == MEDIA_SCALE.SCALE_X_TO_Z)
  648. {
  649. m_objResize[i].transform.localScale
  650. = new Vector3(m_objResize[i].transform.localScale.x
  651. , m_objResize[i].transform.localScale.y
  652. , m_objResize[i].transform.localScale.x * fRatio);
  653. }
  654. else if (m_ScaleValue == MEDIA_SCALE.SCALE_Y_TO_X)
  655. {
  656. m_objResize[i].transform.localScale
  657. = new Vector3(m_objResize[i].transform.localScale.y / fRatio
  658. , m_objResize[i].transform.localScale.y
  659. , m_objResize[i].transform.localScale.z);
  660. }
  661. else if (m_ScaleValue == MEDIA_SCALE.SCALE_Y_TO_Z)
  662. {
  663. m_objResize[i].transform.localScale
  664. = new Vector3(m_objResize[i].transform.localScale.x
  665. , m_objResize[i].transform.localScale.y
  666. , m_objResize[i].transform.localScale.y / fRatio);
  667. }
  668. else if (m_ScaleValue == MEDIA_SCALE.SCALE_Z_TO_X)
  669. {
  670. m_objResize[i].transform.localScale
  671. = new Vector3(m_objResize[i].transform.localScale.z * fRatio
  672. , m_objResize[i].transform.localScale.y
  673. , m_objResize[i].transform.localScale.z);
  674. }
  675. else if (m_ScaleValue == MEDIA_SCALE.SCALE_Z_TO_Y)
  676. {
  677. m_objResize[i].transform.localScale
  678. = new Vector3(m_objResize[i].transform.localScale.x
  679. , m_objResize[i].transform.localScale.z * fRatio
  680. , m_objResize[i].transform.localScale.z);
  681. }
  682. else
  683. {
  684. m_objResize[i].transform.localScale
  685. = new Vector3(m_objResize[i].transform.localScale.x, m_objResize[i].transform.localScale.y, m_objResize[i].transform.localScale.z);
  686. }
  687. }
  688. }
  689. }
  690. //The error code is the following sites related documents.
  691. //http://developer.android.com/reference/android/media/MediaPlayer.OnErrorListener.html
  692. void OnError(MEDIAPLAYER_ERROR iCode, MEDIAPLAYER_ERROR iCodeExtra)
  693. {
  694. string strError = "";
  695. switch (iCode)
  696. {
  697. case MEDIAPLAYER_ERROR.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
  698. strError = "MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK";
  699. break;
  700. case MEDIAPLAYER_ERROR.MEDIA_ERROR_SERVER_DIED:
  701. strError = "MEDIA_ERROR_SERVER_DIED";
  702. break;
  703. case MEDIAPLAYER_ERROR.MEDIA_ERROR_UNKNOWN:
  704. strError = "MEDIA_ERROR_UNKNOWN";
  705. break;
  706. default:
  707. strError = "Unknown error " + iCode;
  708. break;
  709. }
  710. strError += " ";
  711. switch (iCodeExtra)
  712. {
  713. case MEDIAPLAYER_ERROR.MEDIA_ERROR_IO:
  714. strError += "MEDIA_ERROR_IO";
  715. break;
  716. case MEDIAPLAYER_ERROR.MEDIA_ERROR_MALFORMED:
  717. strError += "MEDIA_ERROR_MALFORMED";
  718. break;
  719. case MEDIAPLAYER_ERROR.MEDIA_ERROR_TIMED_OUT:
  720. strError += "MEDIA_ERROR_TIMED_OUT";
  721. break;
  722. case MEDIAPLAYER_ERROR.MEDIA_ERROR_UNSUPPORTED:
  723. strError += "MEDIA_ERROR_UNSUPPORTED";
  724. break;
  725. default:
  726. strError = "Unknown error " + iCode;
  727. break;
  728. }
  729. Debug.LogError(strError);
  730. if (OnVideoError != null)
  731. {
  732. OnVideoError(iCode, iCodeExtra);
  733. }
  734. }
  735. void OnDestroy()
  736. {
  737. #if UNITY_ANDROID
  738. //Call_Reset();
  739. #endif
  740. Call_UnLoad();
  741. DeleteVideoTexture ();
  742. Call_Destroy();
  743. }
  744. bool m_bPause = false;
  745. bool m_bPlaying = false;
  746. void OnApplicationPause(bool bPause)
  747. {
  748. Debug.Log("ApplicationPause : " + bPause);
  749. if(m_bPlaying == false && m_bPause == false)
  750. if (m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.NOT_READY)
  751. return;
  752. if (bPause == true)
  753. {
  754. if (m_CurrentState == MEDIAPLAYER_STATE.PAUSED)
  755. {
  756. m_bPause = true;
  757. }
  758. #if (UNITY_IPHONE || UNITY_TVOS) && !UNITY_EDITOR
  759. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING)
  760. {
  761. m_iPauseFrame = m_iCurrentSeekPosition;
  762. Stop();
  763. //Pause();
  764. }
  765. else
  766. {
  767. m_iPauseFrame = 0;
  768. }
  769. #elif UNITY_ANDROID && !UNITY_EDITOR
  770. m_bPlaying = true;
  771. m_iPauseFrame = m_iCurrentSeekPosition;
  772. UnLoad();
  773. #else
  774. Call_Pause();
  775. #endif
  776. }
  777. else
  778. {
  779. #if (UNITY_IPHONE || UNITY_TVOS) && !UNITY_EDITOR
  780. if (m_iPauseFrame != 0)
  781. {
  782. Call_Play(m_iPauseFrame);
  783. //Call_Play();
  784. //Call_RePlay();
  785. if (m_bPause == true)
  786. {
  787. Call_Pause();
  788. m_bPause = false;
  789. }
  790. }
  791. #elif UNITY_ANDROID && !UNITY_EDITOR
  792. Load(m_strFileName);
  793. #else
  794. Call_RePlay();
  795. #endif
  796. }
  797. }
  798. public MEDIAPLAYER_STATE GetCurrentState()
  799. {
  800. return m_CurrentState;
  801. }
  802. public Texture2D GetVideoTexture()
  803. {
  804. return m_VideoTexture;
  805. }
  806. bool m_bReadyPlay = false;
  807. public void Play()
  808. {
  809. if (m_CurrentState == MEDIAPLAYER_STATE.PAUSED)
  810. {
  811. Call_RePlay ();
  812. }else if ( m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
  813. {
  814. Load(m_strFileName);
  815. }
  816. else if (m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.STOPPED || m_CurrentState == MEDIAPLAYER_STATE.END)
  817. {
  818. Call_Play (0);
  819. }
  820. else if (m_CurrentState == MEDIAPLAYER_STATE.NOT_READY)
  821. {
  822. if (string.IsNullOrEmpty(m_strFileName) == false)
  823. {
  824. Load(m_strFileName);
  825. }
  826. m_bReadyPlay = true;
  827. }
  828. }
  829. public void Stop()
  830. {
  831. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING)
  832. Call_Pause();
  833. #if UNITY_STANDALONE || UNITY_EDITOR
  834. UnLoad();
  835. #else
  836. SeekTo(0);
  837. #endif
  838. m_CurrentState = MEDIAPLAYER_STATE.STOPPED;
  839. m_iCurrentSeekPosition = 0;
  840. }
  841. public void Pause()
  842. {
  843. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING)
  844. {
  845. Call_Pause();
  846. m_CurrentState = MEDIAPLAYER_STATE.PAUSED;
  847. }
  848. }
  849. public void Load(string strFileName)
  850. {
  851. if (m_bInit == false)
  852. {
  853. InitPlayer();
  854. }
  855. if (GetCurrentState() != MEDIAPLAYER_STATE.NOT_READY)
  856. UnLoad();
  857. //m_bReadyPlay = false;
  858. m_bIsFirstFrameReady = false;
  859. //m_bFirst = false;
  860. m_bCheckFBO = false;
  861. m_strFileName = strFileName;
  862. if (m_bInit == false)
  863. return;
  864. Call_Load(strFileName,0);
  865. m_CurrentState = MEDIAPLAYER_STATE.NOT_READY;
  866. }
  867. public void SetVolume(float fVolume)
  868. {
  869. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.END || m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
  870. {
  871. m_fVolume = fVolume;
  872. Call_SetVolume(fVolume);
  873. }
  874. }
  875. //return milisecond
  876. public int GetSeekPosition()
  877. {
  878. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.END)
  879. return m_iCurrentSeekPosition;
  880. else
  881. return 0;
  882. }
  883. public void SeekTo(int iSeek)
  884. {
  885. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.END || m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
  886. Call_SetSeekPosition(iSeek);
  887. }
  888. /// <summary>
  889. /// Sets the speed.
  890. /// Experimental API( PC&iOS support, support from Android version 6.0 or later)
  891. /// </summary>
  892. /// <param name="fSpeed">video playback speed.</param>
  893. public void SetSpeed(float fSpeed)
  894. {
  895. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.END || m_CurrentState == MEDIAPLAYER_STATE.STOPPED) {
  896. m_fSpeed = fSpeed;
  897. Call_SetSpeed (fSpeed);
  898. }
  899. }
  900. //Gets the duration of the file.
  901. //Returns
  902. //the duration in milliseconds, if no duration is available (for example, if streaming live content), -1 is returned.
  903. public int GetDuration()
  904. {
  905. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.END || m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
  906. return Call_GetDuration();
  907. else
  908. return 0;
  909. }
  910. public float GetSeekBarValue()
  911. {
  912. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.END || m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
  913. {
  914. if (GetDuration () == 0) {
  915. return 0;
  916. }
  917. return (float)GetSeekPosition() / (float)GetDuration() ;
  918. }
  919. else
  920. return 0;
  921. }
  922. public void SetSeekBarValue(float fValue)
  923. {
  924. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.END || m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
  925. {
  926. if (GetDuration () == 0) {
  927. return;
  928. }
  929. SeekTo( (int)((float)GetDuration() * fValue) );
  930. }
  931. else
  932. return;
  933. }
  934. //Get update status in buffering a media stream received through progressive HTTP download.
  935. //The received buffering percentage indicates how much of the content has been buffered or played.
  936. //For example a buffering update of 80 percent when half the content has already been played indicates that the next 30 percent of the content to play has been buffered.
  937. //the percentage (0-100) of the content that has been buffered or played thus far
  938. public int GetCurrentSeekPercent()
  939. {
  940. if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.END || m_CurrentState == MEDIAPLAYER_STATE.READY)
  941. return Call_GetCurrentSeekPercent();
  942. else
  943. return 0;
  944. }
  945. public int GetVideoWidth()
  946. {
  947. return Call_GetVideoWidth();
  948. }
  949. public int GetVideoHeight()
  950. {
  951. return Call_GetVideoHeight();
  952. }
  953. public void UnLoad()
  954. {
  955. m_bCheckFBO = false;
  956. #if UNITY_ANDROID
  957. //Call_Reset();
  958. #endif
  959. Call_UnLoad();
  960. m_CurrentState = MEDIAPLAYER_STATE.NOT_READY;
  961. }
  962. public void SelectSoundTrack(int iIndex)
  963. {
  964. Call_SelectSoundTrack(iIndex);
  965. }
  966. public int[] GetSoundTrack()
  967. {
  968. return Call_GetSoundTrack();
  969. }
  970. #if !UNITY_EDITOR && !UNITY_STANDALONE && !UNITY_WEBGL
  971. #if UNITY_ANDROID
  972. private AndroidJavaObject javaObj = null;
  973. private AndroidJavaObject GetJavaObject()
  974. {
  975. if (javaObj == null)
  976. {
  977. javaObj = new AndroidJavaObject("com.EasyMovieTexture.EasyMovieTexture");
  978. }
  979. return javaObj;
  980. }
  981. private void Call_Destroy()
  982. {
  983. #if UNITY_5 || UNITY_5_3_OR_NEWER
  984. if( SystemInfo.graphicsMultiThreaded == true)
  985. {
  986. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  987. GL.IssuePluginEvent(EasyMovieTextureRender(), 5 + m_iAndroidMgrID * 10 + 7000);
  988. #else
  989. GL.IssuePluginEvent(5 + m_iAndroidMgrID * 10 + 7000);
  990. #endif
  991. }
  992. else
  993. {
  994. GetJavaObject().Call("Destroy");
  995. }
  996. #else
  997. GetJavaObject().Call("Destroy");
  998. #endif
  999. }
  1000. private void Call_UnLoad()
  1001. {
  1002. #if UNITY_5 || UNITY_5_3_OR_NEWER
  1003. if( SystemInfo.graphicsMultiThreaded == true)
  1004. {
  1005. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  1006. GL.IssuePluginEvent(EasyMovieTextureRender(), 4 + m_iAndroidMgrID * 10 + 7000);
  1007. #else
  1008. GL.IssuePluginEvent(4 + m_iAndroidMgrID * 10 + 7000);
  1009. #endif
  1010. }
  1011. else
  1012. {
  1013. GetJavaObject().Call("UnLoad");
  1014. }
  1015. #else
  1016. GetJavaObject().Call("UnLoad");
  1017. #endif
  1018. }
  1019. private bool Call_Load(string strFileName, int iSeek)
  1020. {
  1021. #if UNITY_5 || UNITY_5_3_OR_NEWER
  1022. if( SystemInfo.graphicsMultiThreaded == true)
  1023. {
  1024. GetJavaObject().Call("NDK_SetFileName", strFileName);
  1025. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  1026. GL.IssuePluginEvent(EasyMovieTextureRender(), 1 + m_iAndroidMgrID * 10 + 7000);
  1027. #else
  1028. GL.IssuePluginEvent(1+ m_iAndroidMgrID * 10 + 7000);
  1029. #endif
  1030. Call_SetNotReady();
  1031. return true;
  1032. }
  1033. else
  1034. {
  1035. GetJavaObject().Call("NDK_SetFileName", strFileName);
  1036. if (GetJavaObject().Call<bool>("Load"))
  1037. {
  1038. return true;
  1039. }
  1040. else
  1041. {
  1042. OnError(MEDIAPLAYER_ERROR.MEDIA_ERROR_UNKNOWN, MEDIAPLAYER_ERROR.MEDIA_ERROR_UNKNOWN);
  1043. return false;
  1044. }
  1045. }
  1046. #else
  1047. GetJavaObject().Call("NDK_SetFileName", strFileName);
  1048. if (GetJavaObject().Call<bool>("Load"))
  1049. {
  1050. return true;
  1051. }
  1052. else
  1053. {
  1054. OnError(MEDIAPLAYER_ERROR.MEDIA_ERROR_UNKNOWN, MEDIAPLAYER_ERROR.MEDIA_ERROR_UNKNOWN);
  1055. return false;
  1056. }
  1057. #endif
  1058. }
  1059. private void Call_UpdateVideoTexture()
  1060. {
  1061. if( Call_IsUpdateFrame() == false)
  1062. return;
  1063. if(m_VideoTextureDummy != null)
  1064. {
  1065. Destroy(m_VideoTextureDummy);
  1066. m_VideoTextureDummy = null;
  1067. }
  1068. for( int i = 0; i < m_TargetMaterial.Length; i++)
  1069. {
  1070. if(m_TargetMaterial[i])
  1071. {
  1072. if(m_TargetMaterial[i].GetComponent<MeshRenderer>()!= null)
  1073. {
  1074. if(m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTexture != m_VideoTexture)
  1075. {
  1076. m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTexture = m_VideoTexture;
  1077. }
  1078. }
  1079. if(m_TargetMaterial[i].GetComponent<RawImage>()!= null)
  1080. {
  1081. if(m_TargetMaterial[i].GetComponent<RawImage>().texture != m_VideoTexture)
  1082. {
  1083. m_TargetMaterial[i].GetComponent<RawImage>().texture = m_VideoTexture;
  1084. }
  1085. }
  1086. }
  1087. }
  1088. #if UNITY_5 || UNITY_5_3_OR_NEWER
  1089. if( SystemInfo.graphicsMultiThreaded == true)
  1090. {
  1091. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  1092. GL.IssuePluginEvent(EasyMovieTextureRender(), 3 + m_iAndroidMgrID * 10 + 7000);
  1093. #else
  1094. GL.IssuePluginEvent(3+ m_iAndroidMgrID * 10 + 7000);
  1095. #endif
  1096. }
  1097. else
  1098. {
  1099. GetJavaObject().Call("UpdateVideoTexture");
  1100. }
  1101. #else
  1102. GetJavaObject().Call("UpdateVideoTexture");
  1103. #endif
  1104. if (!m_bIsFirstFrameReady)
  1105. {
  1106. m_bIsFirstFrameReady = true;
  1107. if (OnVideoFirstFrameReady != null)
  1108. {
  1109. OnVideoFirstFrameReady();
  1110. //OnVideoFirstFrameReady = null;
  1111. }
  1112. }
  1113. }
  1114. private void Call_SetVolume(float fVolume)
  1115. {
  1116. GetJavaObject().Call("SetVolume",fVolume);
  1117. }
  1118. private void Call_SetSeekPosition(int iSeek)
  1119. {
  1120. GetJavaObject().Call("SetSeekPosition",iSeek);
  1121. }
  1122. private int Call_GetSeekPosition()
  1123. {
  1124. return GetJavaObject().Call<int>("GetSeekPosition");
  1125. }
  1126. private void Call_Play(int iSeek)
  1127. {
  1128. GetJavaObject().Call("Play",iSeek);
  1129. }
  1130. private void Call_Reset()
  1131. {
  1132. GetJavaObject().Call("Reset");
  1133. }
  1134. private void Call_Stop()
  1135. {
  1136. GetJavaObject().Call("Stop");
  1137. }
  1138. private void Call_RePlay()
  1139. {
  1140. GetJavaObject().Call("RePlay");
  1141. }
  1142. private void Call_Pause()
  1143. {
  1144. GetJavaObject().Call("Pause");
  1145. }
  1146. private int Call_InitNDK()
  1147. {
  1148. return GetJavaObject().Call<int>("InitNative",GetJavaObject());
  1149. }
  1150. private int Call_GetVideoWidth()
  1151. {
  1152. return GetJavaObject().Call<int>("GetVideoWidth");
  1153. }
  1154. private int Call_GetVideoHeight()
  1155. {
  1156. return GetJavaObject().Call<int>("GetVideoHeight");
  1157. }
  1158. private bool Call_IsUpdateFrame()
  1159. {
  1160. return GetJavaObject().Call<bool>("IsUpdateFrame");
  1161. }
  1162. private void Call_SetUnityTexture(int iTextureID)
  1163. {
  1164. GetJavaObject().Call("SetUnityTexture",iTextureID);
  1165. }
  1166. private void Call_SetWindowSize()
  1167. {
  1168. #if UNITY_5 || UNITY_5_3_OR_NEWER
  1169. if( SystemInfo.graphicsMultiThreaded == true)
  1170. {
  1171. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  1172. GL.IssuePluginEvent(EasyMovieTextureRender(), 2 + m_iAndroidMgrID * 10 + 7000);
  1173. #else
  1174. GL.IssuePluginEvent(2+ m_iAndroidMgrID * 10 + 7000);
  1175. #endif
  1176. }
  1177. else
  1178. {
  1179. GetJavaObject().Call("SetWindowSize");
  1180. }
  1181. #else
  1182. GetJavaObject().Call("SetWindowSize");
  1183. #endif
  1184. }
  1185. private void Call_SetLooping(bool bLoop)
  1186. {
  1187. GetJavaObject().Call("SetLooping",bLoop);
  1188. }
  1189. private void Call_SetRockchip(bool bValue)
  1190. {
  1191. GetJavaObject().Call("SetRockchip",bValue);
  1192. }
  1193. private int Call_GetDuration()
  1194. {
  1195. return GetJavaObject().Call<int>("GetDuration");
  1196. }
  1197. private int Call_GetCurrentSeekPercent()
  1198. {
  1199. return GetJavaObject().Call<int>("GetCurrentSeekPercent");
  1200. }
  1201. private int Call_GetError()
  1202. {
  1203. return GetJavaObject().Call<int>("GetError");
  1204. }
  1205. private void Call_SetSplitOBB(bool bValue, string strOBBName)
  1206. {
  1207. GetJavaObject().Call("SetSplitOBB",bValue,strOBBName);
  1208. }
  1209. private int Call_GetErrorExtra()
  1210. {
  1211. return GetJavaObject().Call<int>("GetErrorExtra");
  1212. }
  1213. private void Call_SetUnityActivity()
  1214. {
  1215. AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  1216. AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
  1217. GetJavaObject().Call("SetUnityActivity", jo);
  1218. #if UNITY_5 || UNITY_5_3_OR_NEWER
  1219. if( SystemInfo.graphicsMultiThreaded == true)
  1220. {
  1221. #if UNITY_5_2 || UNITY_5_3_OR_NEWER
  1222. GL.IssuePluginEvent(EasyMovieTextureRender(), 0 + m_iAndroidMgrID * 10 + 7000);
  1223. #else
  1224. GL.IssuePluginEvent(0+ m_iAndroidMgrID * 10 + 7000);
  1225. #endif
  1226. }
  1227. else
  1228. {
  1229. Call_InitJniManager();
  1230. }
  1231. #else
  1232. Call_InitJniManager();
  1233. #endif
  1234. }
  1235. private void Call_SetNotReady()
  1236. {
  1237. GetJavaObject().Call("SetNotReady");
  1238. }
  1239. private void Call_InitJniManager()
  1240. {
  1241. GetJavaObject().Call("InitJniManager");
  1242. }
  1243. private void Call_SetSpeed(float fSpeed)
  1244. {
  1245. using (AndroidJavaClass buildVersion = new AndroidJavaClass("android.os.Build$VERSION"))
  1246. {
  1247. int sdkVersion = buildVersion.GetStatic<int>("SDK_INT");
  1248. if(sdkVersion >= 23 ) //Android 6.0
  1249. {
  1250. GetJavaObject().Call("SetSpeed",fSpeed);
  1251. }
  1252. }
  1253. }
  1254. private MEDIAPLAYER_STATE Call_GetStatus()
  1255. {
  1256. return (MEDIAPLAYER_STATE)GetJavaObject().Call<int>("GetStatus");
  1257. }
  1258. private void Call_SelectSoundTrack(int iIndex)
  1259. {
  1260. GetJavaObject().Call("SelectTrack",iIndex);
  1261. }
  1262. private int[] Call_GetSoundTrack()
  1263. {
  1264. return GetJavaObject().Call<int []>("GetSoundTrack");
  1265. }
  1266. #elif UNITY_IPHONE || UNITY_TVOS
  1267. [DllImport("__Internal")]
  1268. private static extern int VideoPlayerPluginCreateInstance();
  1269. [DllImport("__Internal")]
  1270. private static extern void VideoPlayerPluginDestroyInstance(int iID);
  1271. [DllImport("__Internal")]
  1272. private static extern bool VideoPlayerPluginCanOutputToTexture(string videoURL);
  1273. [DllImport("__Internal")]
  1274. private static extern void VideoPlayerPluginSetLoop(int iID, bool bLoop);
  1275. [DllImport("__Internal")]
  1276. private static extern void VideoPlayerPluginSetVolume(int iID, float fVolume);
  1277. [DllImport("__Internal")]
  1278. private static extern bool VideoPlayerPluginPlayerReady(int iID);
  1279. [DllImport("__Internal")]
  1280. private static extern float VideoPlayerPluginDurationSeconds(int iID);
  1281. [DllImport("__Internal")]
  1282. private static extern void VideoPlayerPluginExtents(int iID,ref int width, ref int height);
  1283. [DllImport("__Internal")]
  1284. private static extern IntPtr VideoPlayerPluginCurFrameTexture(int iID);
  1285. [DllImport("__Internal")]
  1286. private static extern void VideoPlayerPluginLoadVideo(int iID,string videoURL);
  1287. [DllImport("__Internal")]
  1288. private static extern void VideoPlayerPluginPlayVideo(int iID);
  1289. [DllImport("__Internal")]
  1290. private static extern void VideoPlayerPluginPauseVideo(int iID);
  1291. [DllImport("__Internal")]
  1292. private static extern void VideoPlayerPluginResumeVideo(int iID);
  1293. [DllImport("__Internal")]
  1294. private static extern void VideoPlayerPluginRewindVideo(int iID);
  1295. [DllImport("__Internal")]
  1296. private static extern void VideoPlayerPluginSeekToVideo(int iID,float time);
  1297. [DllImport("__Internal")]
  1298. private static extern float VideoPlayerPluginCurTimeSeconds(int iID);
  1299. [DllImport("__Internal")]
  1300. private static extern bool VideoPlayerPluginIsPlaying(int iID);
  1301. [DllImport("__Internal")]
  1302. private static extern void VideoPlayerPluginStopVideo(int iID);
  1303. [DllImport("__Internal")]
  1304. private static extern bool VideoPlayerPluginFinish(int iID);
  1305. [DllImport("__Internal")]
  1306. private static extern bool VideoPlayerPluginError(int iID);
  1307. [DllImport("__Internal")]
  1308. private static extern void VideoPlayerPluginSetTexture(int iID,int iTextureID);
  1309. [DllImport("__Internal")]
  1310. private static extern void VideoPlayerPluginSetSpeed(int iID,float fSpeed);
  1311. [DllImport("__Internal")]
  1312. private static extern void VideoPlayerPluginSetAudioTrack(int iID,int trackID);
  1313. [DllImport("__Internal")]
  1314. private static extern int VideoPlayerPluginGetAudioTrack(int iID);
  1315. [DllImport("__Internal")]
  1316. private static extern int VideoPlayerPluginGetCurrentSeekPercent(int iID);
  1317. int m_iID = -1;
  1318. /// <summary>
  1319. /// ???????????????true??????? /// </summary>
  1320. public bool ready
  1321. {
  1322. get
  1323. {
  1324. return VideoPlayerPluginPlayerReady(m_iID);
  1325. }
  1326. }
  1327. /// <summary>
  1328. ///
  1329. /// </summary>
  1330. public float duration
  1331. {
  1332. get
  1333. {
  1334. return VideoPlayerPluginDurationSeconds(m_iID);
  1335. }
  1336. }
  1337. public float currentTime
  1338. {
  1339. get
  1340. {
  1341. return VideoPlayerPluginCurTimeSeconds(m_iID);
  1342. }
  1343. }
  1344. public bool isPlaying
  1345. {
  1346. get
  1347. {
  1348. return VideoPlayerPluginIsPlaying(m_iID);
  1349. }
  1350. }
  1351. public Vector2 videoSize
  1352. {
  1353. get
  1354. {
  1355. int width = 0, height = 0;
  1356. VideoPlayerPluginExtents(m_iID,ref width, ref height);
  1357. return new Vector2(width, height);
  1358. }
  1359. }
  1360. private Texture2D _videoTexture;
  1361. public Texture2D videoTexture
  1362. {
  1363. get
  1364. {
  1365. IntPtr nativeTex = ready ? VideoPlayerPluginCurFrameTexture(m_iID) : (IntPtr)0;
  1366. if (nativeTex != (IntPtr)0)
  1367. {
  1368. if (_videoTexture == null)
  1369. {
  1370. #if UNITY_5 || UNITY_5_3_OR_NEWER
  1371. if(SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Metal)
  1372. _videoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGBA32, false);
  1373. else
  1374. _videoTexture = Texture2D.CreateExternalTexture((int)videoSize.x, (int)videoSize.y, TextureFormat.RGBA32,
  1375. false, false, (IntPtr)nativeTex);
  1376. #else
  1377. _videoTexture = Texture2D.CreateExternalTexture((int)videoSize.x, (int)videoSize.y, TextureFormat.RGBA32,
  1378. false, false, (IntPtr)nativeTex);
  1379. #endif
  1380. _videoTexture.filterMode = FilterMode.Bilinear;
  1381. _videoTexture.wrapMode = TextureWrapMode.Clamp;
  1382. }
  1383. _videoTexture.UpdateExternalTexture((IntPtr)nativeTex);
  1384. }
  1385. /* else
  1386. {
  1387. if(_videoTexture != null)
  1388. {
  1389. Destroy(_videoTexture);
  1390. }
  1391. _videoTexture = null;
  1392. }*/
  1393. return _videoTexture;
  1394. }
  1395. }
  1396. private void Call_Destroy()
  1397. {
  1398. VideoPlayerPluginDestroyInstance(m_iID);
  1399. if(_videoTexture != null)
  1400. Destroy(_videoTexture);
  1401. _videoTexture = null;
  1402. m_iID = -1;
  1403. }
  1404. private void Call_UnLoad()
  1405. {
  1406. VideoPlayerPluginStopVideo(m_iID);
  1407. VideoPlayerPluginDestroyInstance(m_iID);
  1408. }
  1409. private bool Call_Load(string strFileName, int iSeek)
  1410. {
  1411. if( m_iID == -1)
  1412. {
  1413. Call_SetUnityActivity();
  1414. }
  1415. bFirstIOS = true;
  1416. /*if(_videoTexture != null)
  1417. {
  1418. Destroy(_videoTexture);
  1419. }*/
  1420. _videoTexture = null;
  1421. if (VideoPlayerPluginCanOutputToTexture(strFileName))
  1422. {
  1423. VideoPlayerPluginLoadVideo(m_iID,strFileName);
  1424. }
  1425. return true;
  1426. }
  1427. bool bFirstIOS = false;
  1428. private void Call_UpdateVideoTexture()
  1429. {
  1430. if( m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED)
  1431. {
  1432. if( videoTexture == null)
  1433. return;
  1434. if(bFirstIOS == true)
  1435. {
  1436. bFirstIOS = false;
  1437. return;
  1438. }
  1439. if(bFirstIOS == false)
  1440. {
  1441. for( int i = 0; i < m_TargetMaterial.Length; i++)
  1442. {
  1443. if(m_TargetMaterial[i])
  1444. {
  1445. if(m_TargetMaterial[i].GetComponent<MeshRenderer>() != null)
  1446. m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTexture = _videoTexture;
  1447. }
  1448. if(m_TargetMaterial[i])
  1449. {
  1450. if(m_TargetMaterial[i].GetComponent<RawImage>() != null)
  1451. m_TargetMaterial[i].GetComponent<RawImage>().texture= _videoTexture;
  1452. }
  1453. }
  1454. if(m_VideoTextureDummy != null)
  1455. {
  1456. Destroy(m_VideoTextureDummy);
  1457. m_VideoTextureDummy = null;
  1458. }
  1459. }
  1460. m_VideoTexture = _videoTexture;
  1461. }
  1462. if (!m_bIsFirstFrameReady)
  1463. {
  1464. m_bIsFirstFrameReady = true;
  1465. if (OnVideoFirstFrameReady != null)
  1466. {
  1467. OnVideoFirstFrameReady();
  1468. //OnVideoFirstFrameReady = null;
  1469. }
  1470. }
  1471. }
  1472. private void Call_SetVolume(float fVolume)
  1473. {
  1474. VideoPlayerPluginSetVolume(m_iID, fVolume);
  1475. }
  1476. private void Call_SetSeekPosition(int iSeek)
  1477. {
  1478. float fSeek = (float)iSeek / 1000.0f;
  1479. VideoPlayerPluginSeekToVideo(m_iID,fSeek);
  1480. }
  1481. private int Call_GetSeekPosition()
  1482. {
  1483. float fSeek = VideoPlayerPluginCurTimeSeconds(m_iID);
  1484. return (int)(fSeek * 1000.0f);
  1485. }
  1486. private void Call_Play(int iSeek)
  1487. {
  1488. float fSeek = (float)iSeek / 1000.0f;
  1489. if( isPlaying == true )
  1490. {
  1491. VideoPlayerPluginSeekToVideo(m_iID,fSeek);
  1492. }
  1493. else
  1494. {
  1495. if( m_CurrentState != MEDIAPLAYER_STATE.READY)
  1496. VideoPlayerPluginSeekToVideo(m_iID,fSeek);
  1497. VideoPlayerPluginPlayVideo(m_iID);
  1498. }
  1499. if( m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
  1500. m_CurrentState = MEDIAPLAYER_STATE.PLAYING;
  1501. }
  1502. private void Call_Reset()
  1503. {
  1504. }
  1505. private void Call_Stop()
  1506. {
  1507. VideoPlayerPluginStopVideo(m_iID);
  1508. }
  1509. private void Call_RePlay()
  1510. {
  1511. VideoPlayerPluginResumeVideo(m_iID);
  1512. m_CurrentState = MEDIAPLAYER_STATE.PLAYING;
  1513. }
  1514. private void Call_Pause()
  1515. {
  1516. m_CurrentState = MEDIAPLAYER_STATE.PAUSED;
  1517. VideoPlayerPluginPauseVideo(m_iID);
  1518. }
  1519. private int Call_GetVideoWidth()
  1520. {
  1521. return (int)videoSize.x;
  1522. }
  1523. private int Call_GetVideoHeight()
  1524. {
  1525. return (int)videoSize.y;
  1526. }
  1527. private void Call_SetUnityTexture(int iTextureID)
  1528. {
  1529. VideoPlayerPluginSetTexture(m_iID,iTextureID);
  1530. }
  1531. private void Call_SetWindowSize()
  1532. {
  1533. }
  1534. private void Call_SetLooping(bool bLoop)
  1535. {
  1536. VideoPlayerPluginSetLoop(m_iID, bLoop);
  1537. }
  1538. public void Call_SetUnityActivity()
  1539. {
  1540. m_iID = VideoPlayerPluginCreateInstance();
  1541. }
  1542. private int Call_GetError()
  1543. {
  1544. return 0;
  1545. }
  1546. private int Call_GetErrorExtra()
  1547. {
  1548. return 0;
  1549. }
  1550. private int Call_GetDuration()
  1551. {
  1552. return (int)(duration * 1000);
  1553. }
  1554. private int Call_GetCurrentSeekPercent()
  1555. {
  1556. return VideoPlayerPluginGetCurrentSeekPercent(m_iID);
  1557. }
  1558. private void Call_SetSpeed(float fSpeed)
  1559. {
  1560. VideoPlayerPluginSetSpeed(m_iID,fSpeed);
  1561. }
  1562. private MEDIAPLAYER_STATE Call_GetStatus()
  1563. {
  1564. if( VideoPlayerPluginFinish(m_iID) )
  1565. {
  1566. return MEDIAPLAYER_STATE.END;
  1567. }
  1568. if( VideoPlayerPluginError(m_iID) )
  1569. {
  1570. return MEDIAPLAYER_STATE.ERROR;
  1571. }
  1572. if( m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
  1573. {
  1574. return m_CurrentState;
  1575. }
  1576. else if(isPlaying)
  1577. {
  1578. return MEDIAPLAYER_STATE.PLAYING;
  1579. }
  1580. else if(ready)
  1581. {
  1582. return MEDIAPLAYER_STATE.READY;
  1583. }
  1584. return m_CurrentState;
  1585. }
  1586. private void Call_SelectSoundTrack(int iIndex)
  1587. {
  1588. VideoPlayerPluginSetAudioTrack(m_iID,iIndex);
  1589. }
  1590. private int[] Call_GetSoundTrack()
  1591. {
  1592. if(VideoPlayerPluginGetAudioTrack(m_iID) !=0)
  1593. {
  1594. int [] info = new int[VideoPlayerPluginGetAudioTrack(m_iID)];
  1595. for(int i =0; i < info.Length; i++)
  1596. {
  1597. info[i] = i;
  1598. }
  1599. return info;
  1600. }
  1601. else
  1602. {
  1603. return null;
  1604. }
  1605. }
  1606. #endif
  1607. #else
  1608. AVFrame* pConvertedFrame = null;
  1609. sbyte* pConvertedFrameBuffer = null;
  1610. SwsContext* pConvertContext = null;
  1611. AVCodecContext* pCodecContext = null;
  1612. AVCodecContext* pAudioCodecContext = null;
  1613. AVFrame* pDecodedFrame = null;
  1614. AVFrame* pDecodedAudioFrame = null;
  1615. AVFormatContext* pFormatContext = null;
  1616. AVPacket* pPacket = null;
  1617. AVStream* pStream = null;
  1618. AVStream* pStreamAudio = null;
  1619. Queue<IntPtr> m_queuePacketVideo;
  1620. Queue<IntPtr> m_queuePacketSound;
  1621. int iStreamAudioIndex;
  1622. int iStreamIndex;
  1623. AudioClip audioClip;
  1624. AudioSource audioSource;
  1625. long iSoundCount = 0;
  1626. int iInitCount = 0;
  1627. double pts;
  1628. bool bVideoFirstFrameReady = false;
  1629. int m_iID;
  1630. private void Call_Destroy()
  1631. {
  1632. if (loader != null) {
  1633. while (loader.IsAlive == true) {
  1634. loader.Abort ();
  1635. }
  1636. loader = null;
  1637. }
  1638. if( threadVideo != null)
  1639. {
  1640. while(threadVideo.IsAlive == true)
  1641. {
  1642. threadVideo.Abort();
  1643. }
  1644. threadVideo = null;
  1645. }
  1646. if (threadVideo2 != null)
  1647. {
  1648. while (threadVideo2.IsAlive == true)
  1649. {
  1650. threadVideo2.Abort();
  1651. }
  1652. threadVideo2 = null;
  1653. }
  1654. ffmpeg.avformat_network_deinit ();
  1655. ReleaseTexture (m_iID);
  1656. }
  1657. private void Call_UnLoad()
  1658. {
  1659. m_CurrentState = MEDIAPLAYER_STATE.NOT_READY;
  1660. m_bSetTextureFirst = false;
  1661. if (loader != null) {
  1662. while (loader.IsAlive == true) {
  1663. loader.Abort ();
  1664. }
  1665. loader = null;
  1666. }
  1667. if( threadVideo != null)
  1668. {
  1669. while(threadVideo.IsAlive == true)
  1670. {
  1671. threadVideo.Abort();
  1672. }
  1673. threadVideo = null;
  1674. }
  1675. if (threadVideo2 != null)
  1676. {
  1677. while (threadVideo2.IsAlive == true)
  1678. {
  1679. threadVideo2.Abort();
  1680. }
  1681. threadVideo2 = null;
  1682. }
  1683. if (listAudio != null)
  1684. {
  1685. listAudio.Clear();
  1686. listAudio = null;
  1687. }
  1688. if (listVideo != null)
  1689. {
  1690. listVideo.Clear();
  1691. listVideo = null;
  1692. }
  1693. if (listAudioPts != null)
  1694. {
  1695. listAudioPts.Clear();
  1696. listAudioPts = null;
  1697. }
  1698. if (listAudioPtsTime != null)
  1699. {
  1700. listAudioPtsTime.Clear();
  1701. listAudioPtsTime = null;
  1702. }
  1703. if (listVideoPts != null)
  1704. {
  1705. listVideoPts.Clear();
  1706. listVideoPts = null;
  1707. }
  1708. fCurrentSeekTime = 0.0f;
  1709. fLastFrameTime = 0.0f;
  1710. if (pPacket != null)
  1711. {
  1712. ffmpeg.av_free_packet(pPacket);
  1713. Marshal.FreeCoTaskMem((IntPtr)pPacket);
  1714. pPacket = null;
  1715. }
  1716. int iCount = m_queuePacketVideo.Count;
  1717. for (int i = 0; i < iCount; i++)
  1718. {
  1719. if (m_queuePacketVideo.Count > 0)
  1720. pPacket = (AVPacket*)m_queuePacketVideo.Dequeue();
  1721. if (pPacket != null)
  1722. {
  1723. ffmpeg.av_free_packet(pPacket);
  1724. Marshal.FreeCoTaskMem((IntPtr)pPacket);
  1725. pPacket = null;
  1726. }
  1727. }
  1728. iCount = m_queuePacketSound.Count;
  1729. for (int i = 0; i < iCount; i++)
  1730. {
  1731. if (m_queuePacketSound.Count > 0)
  1732. pPacket = (AVPacket*)m_queuePacketSound.Dequeue();
  1733. if (pPacket != null)
  1734. {
  1735. ffmpeg.av_free_packet(pPacket);
  1736. Marshal.FreeCoTaskMem((IntPtr)pPacket);
  1737. pPacket = null;
  1738. }
  1739. }
  1740. if (pConvertedFrame != null)
  1741. {
  1742. ffmpeg.av_free(pConvertedFrame);
  1743. pConvertedFrame = null;
  1744. }
  1745. if(pConvertedFrameBuffer != null)
  1746. {
  1747. ffmpeg.av_free(pConvertedFrameBuffer);
  1748. pConvertedFrameBuffer = null;
  1749. }
  1750. if(pConvertContext != null)
  1751. {
  1752. ffmpeg.sws_freeContext(pConvertContext);
  1753. pConvertContext = null;
  1754. }
  1755. if(pDecodedFrame != null)
  1756. {
  1757. ffmpeg.av_free(pDecodedFrame);
  1758. pDecodedFrame = null;
  1759. }
  1760. if (pDecodedAudioFrame != null)
  1761. ffmpeg.av_free (pDecodedAudioFrame);
  1762. pDecodedAudioFrame = null;
  1763. if(pCodecContext != null)
  1764. ffmpeg.avcodec_close(pCodecContext);
  1765. pCodecContext = null;
  1766. if (pAudioCodecContext != null)
  1767. ffmpeg.avcodec_close (pAudioCodecContext);
  1768. pAudioCodecContext = null;
  1769. if (pFormatContext != null) {
  1770. AVFormatContext* ppFormatContext = pFormatContext;
  1771. ffmpeg.avformat_close_input (&ppFormatContext);
  1772. }
  1773. pFormatContext = null;
  1774. if (audioSource != null) {
  1775. audioSource.Stop();
  1776. }
  1777. if ( audioClip != null)
  1778. {
  1779. Destroy (audioClip);
  1780. audioClip = null;
  1781. }
  1782. }
  1783. Thread loader = null;
  1784. private unsafe bool Call_Load(string strFileName, int iSeek)
  1785. {
  1786. if(m_queuePacketVideo == null)
  1787. m_queuePacketVideo = new Queue<IntPtr>();
  1788. if (m_queuePacketSound == null)
  1789. m_queuePacketSound = new Queue<IntPtr>();
  1790. fCurrentSeekTime = 0.0f;
  1791. fLastFrameTime = 0.0f;
  1792. iSoundCount = 0;
  1793. iInitCount= 0;
  1794. bSeekTo = true;
  1795. bEnd = false;
  1796. bEndCheck = false;
  1797. if (audioSource != null) {
  1798. audioSource.Stop();
  1799. audioSource.time = 0.0f;
  1800. }
  1801. if ( audioClip != null)
  1802. {
  1803. Destroy (audioClip);
  1804. audioClip = null;
  1805. }
  1806. pFormatContext = ffmpeg.avformat_alloc_context();
  1807. if( strFileName.Contains("://") == false)
  1808. {
  1809. strFileName = Application.streamingAssetsPath + "/" + strFileName;
  1810. Debug.Log(strFileName);
  1811. }
  1812. else if( strFileName.Contains("file://") == true)
  1813. {
  1814. strFileName = strFileName.Replace("file://", "");
  1815. }
  1816. loader = new Thread ( () => {
  1817. AVFormatContext* ppFomatContext = ffmpeg.avformat_alloc_context();
  1818. ppFomatContext->flags |= ffmpeg.AVFMT_FLAG_NONBLOCK;
  1819. ppFomatContext->flags |= ffmpeg.AVIO_FLAG_NONBLOCK;
  1820. //ppFomatContext->max_interleave_delta = 100000000;
  1821. ppFomatContext->probesize = 50000000;
  1822. // ppFomatContext->max_delay = 1000000; //0.1 secs, default 5 secs
  1823. ///ppFomatContext->probe_score = 100;
  1824. ppFomatContext->max_analyze_duration = 10000000; //0.1 secs
  1825. // ppFomatContext->max_chunk_size = 100;
  1826. // ppFomatContext->max_chunk_duration = 1000000;
  1827. //Debug.Log(ppFomatContext->probesize.ToString() + " " + ppFomatContext->max_analyze_duration.ToString());
  1828. if (ffmpeg.avformat_open_input(&ppFomatContext, strFileName, null, null) != 0)
  1829. {
  1830. pFormatContext = null;
  1831. m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  1832. Debug.Log("Could not open file");
  1833. throw new ApplicationException(@"Could not open file");
  1834. }
  1835. // ffmpeg.av_dict_free(&options);
  1836. pFormatContext = ppFomatContext;
  1837. if (ffmpeg.avformat_find_stream_info(pFormatContext, null) != 0)
  1838. {
  1839. m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  1840. Debug.Log("Could not find stream info");
  1841. throw new ApplicationException(@"Could not find stream info");
  1842. }
  1843. AddActionForUnityMainThread( () => {
  1844. LoadVideoPart2 ();
  1845. });
  1846. });
  1847. loader.Priority = System.Threading.ThreadPriority.AboveNormal;
  1848. loader.IsBackground = true;
  1849. loader.Start ();
  1850. return true;
  1851. }
  1852. bool bDirect9 = false;
  1853. void LoadVideoPart2 ()
  1854. {
  1855. pStream = null;
  1856. pStreamAudio = null;
  1857. bool bFindVideo = false;
  1858. bool bFindAudio = false;
  1859. for (var i = 0; i < (pFormatContext)->nb_streams; i++)
  1860. {
  1861. if ((pFormatContext)->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO)
  1862. {
  1863. if(bFindVideo == false)
  1864. {
  1865. bFindVideo = true;
  1866. pStream = (pFormatContext)->streams[i];
  1867. iStreamIndex = i;
  1868. Debug.Log("Video" + iStreamIndex);
  1869. }
  1870. }
  1871. else if((pFormatContext)->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_AUDIO)
  1872. {
  1873. if (bFindAudio == false)
  1874. {
  1875. bFindAudio = true;
  1876. pStreamAudio = (pFormatContext)->streams[i];
  1877. iStreamAudioIndex = i;
  1878. }
  1879. }
  1880. }
  1881. if (pStream == null)
  1882. {
  1883. m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  1884. Debug.Log("Could not found video stream");
  1885. throw new ApplicationException(@"Could not found video stream");
  1886. }
  1887. if (pStreamAudio == null)
  1888. {
  1889. //m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  1890. Debug.Log("Could not found Audio stream" + bFindAudio);
  1891. //throw new ApplicationException(@"Could not found audio stream");
  1892. }
  1893. var codecContext = *pStream->codec;
  1894. m_iWidth = codecContext.width;
  1895. m_iHeight = codecContext.height;
  1896. var sourcePixFmt = codecContext.pix_fmt;
  1897. var codecId = codecContext.codec_id;
  1898. var convertToPixFmt = AVPixelFormat.AV_PIX_FMT_RGBA;
  1899. #if UNITY_5
  1900. if( SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Direct3D9)
  1901. {
  1902. bDirect9 = true;
  1903. convertToPixFmt = AVPixelFormat.AV_PIX_FMT_BGRA;
  1904. }
  1905. #endif
  1906. pConvertContext = ffmpeg.sws_getContext(m_iWidth, m_iHeight, sourcePixFmt,
  1907. m_iWidth, m_iHeight, convertToPixFmt,
  1908. ffmpeg.SWS_FAST_BILINEAR, null, null, null);
  1909. if (pConvertContext == null)
  1910. {
  1911. m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  1912. Debug.Log("Could not initialize the conversion context");
  1913. throw new ApplicationException(@"Could not initialize the conversion context");
  1914. }
  1915. //pConvertedFrame = ffmpeg.avcodec_alloc_frame();
  1916. pConvertedFrame = ffmpeg.av_frame_alloc();
  1917. var convertedFrameBufferSize = ffmpeg.avpicture_get_size(convertToPixFmt, m_iWidth, m_iHeight);
  1918. pConvertedFrameBuffer = (sbyte*)ffmpeg.av_malloc((ulong)convertedFrameBufferSize);
  1919. AVPicture* tempPicture = (AVPicture*)pConvertedFrame;
  1920. ffmpeg.avpicture_fill(tempPicture , pConvertedFrameBuffer, convertToPixFmt, m_iWidth, m_iHeight);
  1921. var pCodec = ffmpeg.avcodec_find_decoder(codecId);
  1922. if (pCodec == null)
  1923. {
  1924. m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  1925. Debug.Log("Unsupported codec");
  1926. throw new ApplicationException(@"Unsupported codec");
  1927. }
  1928. pCodecContext = pStream->codec;
  1929. pCodecContext->hwaccel = ff_find_hwaccel(pCodecContext->codec_id ,sourcePixFmt);
  1930. if ((pCodec->capabilities & ffmpeg.AV_CODEC_CAP_TRUNCATED) == ffmpeg.AV_CODEC_CAP_TRUNCATED)
  1931. {
  1932. pCodecContext->flags |= ffmpeg.AV_CODEC_FLAG_TRUNCATED;
  1933. }
  1934. pCodecContext->flags |= ffmpeg.AV_CODEC_FLAG_LOW_DELAY; // cuts off 100ms of latency in h264 -- a must-have
  1935. pCodecContext->flags2 |= ffmpeg.AV_CODEC_FLAG2_FAST;
  1936. if (ffmpeg.avcodec_open2(pCodecContext, pCodec, null) < 0)
  1937. {
  1938. m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  1939. Debug.Log("Could not open codec");
  1940. throw new ApplicationException(@"Could not open codec");
  1941. }
  1942. if(pStreamAudio != null)
  1943. {
  1944. var codecAudioContext = *pStreamAudio->codec;
  1945. var pAudioCodec = ffmpeg.avcodec_find_decoder(codecAudioContext.codec_id);
  1946. if (pAudioCodec == null) {
  1947. m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  1948. Debug.Log("Unsupported codec");
  1949. throw new ApplicationException(@"Unsupported codec");
  1950. }
  1951. pAudioCodecContext = pStreamAudio->codec;
  1952. ///> Initialize Codec Context as Decoder
  1953. if (ffmpeg.avcodec_open2(pAudioCodecContext, pAudioCodec, null) < 0) {
  1954. m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  1955. Debug.Log("Could not open codec");
  1956. throw new ApplicationException(@"Could not open codec");
  1957. }
  1958. }
  1959. pDecodedFrame = ffmpeg.av_frame_alloc();
  1960. pDecodedAudioFrame = ffmpeg.av_frame_alloc();
  1961. listAudio = new List<float[]>();
  1962. listVideo = new Queue<byte[]>();
  1963. listAudioPts = new List<double>();
  1964. listAudioPtsTime = new List<double>();
  1965. listVideoPts = new Queue<float>();
  1966. if( m_strFileName.StartsWith("rtsp",StringComparison.OrdinalIgnoreCase) == true)
  1967. {
  1968. }
  1969. else
  1970. {
  1971. action = Interrupt1;
  1972. //pFormatContext->interrupt_callback.callback = Marshal.GetFunctionPointerForDelegate(action);
  1973. }
  1974. bVideoFirstFrameReady = false;
  1975. threadVideo = new Thread(ThreadUpdate);
  1976. threadVideo.IsBackground = true;
  1977. threadVideo.Start();
  1978. threadVideo2 = new Thread(ThreadUpdate2);
  1979. threadVideo2.IsBackground = true;
  1980. threadVideo2.Start();
  1981. if (m_bAutoPlay == true || m_bReadyPlay == true)
  1982. {
  1983. m_bReadyPlay = false;
  1984. if ( OnReady != null)
  1985. OnReady();
  1986. m_CurrentState = MEDIAPLAYER_STATE.PLAYING;
  1987. }
  1988. else
  1989. {
  1990. m_CurrentState = MEDIAPLAYER_STATE.READY;
  1991. if( OnReady != null)
  1992. OnReady();
  1993. }
  1994. loader = null;
  1995. }
  1996. AVHWAccel *ff_find_hwaccel( AVCodecID codec_id, AVPixelFormat pix_fmt)
  1997. {
  1998. AVHWAccel *hwaccel=null;
  1999. while((hwaccel= ffmpeg.av_hwaccel_next(hwaccel))!=null){
  2000. if ( hwaccel->id == codec_id
  2001. && hwaccel->pix_fmt == pix_fmt)
  2002. return hwaccel;
  2003. }
  2004. return null;
  2005. }
  2006. public delegate void VideoInterrupt();
  2007. VideoInterrupt action = null;
  2008. bool bInterrupt = false;
  2009. public void Interrupt1()
  2010. {
  2011. //pFormatContext->interrupt_callback.callback = (IntPtr)null;
  2012. bInterrupt = true;
  2013. // threadVideo.Suspend();
  2014. //UpdateVideo();
  2015. //ffmpeg.av_read_pause(pFormatContext);
  2016. // ffmpeg.av_read_play(pFormatContext);
  2017. // Debug.Log ("Interrupt1");
  2018. //SeekTo(Call_GetSeekPosition());
  2019. //Call_Pause ();
  2020. }
  2021. static double av_q2d(AVRational a){
  2022. return a.num / (double) a.den;
  2023. }
  2024. float fLastFrameTime = 0.0f;
  2025. float fCurrentSeekTime = 0.0f;
  2026. float[] fAudioData;
  2027. bool bEnd = false;
  2028. Thread threadVideo;
  2029. Thread threadVideo2;
  2030. List<float[]> listAudio;
  2031. Queue<byte[]> listVideo;
  2032. List<double> listAudioPts;
  2033. List<double> listAudioPtsTime;
  2034. Queue<float> listVideoPts;
  2035. private static void DebugMethod(string message)
  2036. {
  2037. Debug.Log("EasyMovieTexture: " + message);
  2038. }
  2039. private void ThreadUpdate2()
  2040. {
  2041. while(true)
  2042. {
  2043. while (listVideo.Count > 30 || bEnd == true)
  2044. {
  2045. UpdateSound();
  2046. Thread.Sleep(1);
  2047. }
  2048. UpdateVideo();
  2049. UpdateSound();
  2050. UpdateSound();
  2051. Thread.Sleep(1);
  2052. }
  2053. }
  2054. private void ThreadUpdate()
  2055. {
  2056. /*lock (listVideo)
  2057. {
  2058. if (listVideo != null)
  2059. {
  2060. listVideo.Clear();
  2061. }
  2062. }
  2063. lock (listVideoPts)
  2064. {
  2065. if (listVideoPts != null)
  2066. {
  2067. listVideoPts.Clear();
  2068. }
  2069. }*/
  2070. while (true)
  2071. {
  2072. if (m_CurrentState == MEDIAPLAYER_STATE.ERROR)
  2073. break;
  2074. if (listVideo != null)
  2075. {
  2076. if (bInterrupt == true)
  2077. {
  2078. bInterrupt = false;
  2079. //UpdateVideo();
  2080. //pFormatContext->interrupt_callback.callback = Marshal.GetFunctionPointerForDelegate(action);
  2081. // ReadPacket();
  2082. }
  2083. if (m_queuePacketVideo.Count < BUFFER_SIZE)
  2084. {
  2085. ReadPacket();
  2086. }
  2087. while (listVideo.Count >30 || bEnd == true )
  2088. {
  2089. if (m_queuePacketVideo.Count < BUFFER_SIZE)
  2090. {
  2091. ReadPacket();
  2092. }
  2093. //UpdateSound();
  2094. Thread.Sleep(1);
  2095. }
  2096. }
  2097. // UpdateVideo();
  2098. //UpdateSound();
  2099. Thread.Sleep(1);
  2100. //Thread.Sleep(5);
  2101. }
  2102. }
  2103. bool bBuffering = true;
  2104. bool bEndCheck = false;
  2105. bool bReadPacket = false;
  2106. AVPacket* Packet2 = null;
  2107. private void ReadPacket()
  2108. {
  2109. AVPacket* Packet = (AVPacket*)Marshal.AllocCoTaskMem(sizeof(AVPacket));
  2110. ffmpeg.av_init_packet(Packet);
  2111. int ret = ffmpeg.av_read_frame(pFormatContext, Packet);
  2112. if (ret < 0)
  2113. {
  2114. if (ret == -541478725)
  2115. {
  2116. bEndCheck = true;
  2117. lock (m_queuePacketVideo)
  2118. {
  2119. if (m_queuePacketVideo.Count < 3)
  2120. {
  2121. bEnd = true;
  2122. if (Packet != null)
  2123. {
  2124. ffmpeg.av_free_packet(Packet);
  2125. Marshal.FreeCoTaskMem((IntPtr)Packet);
  2126. Packet = null;
  2127. }
  2128. //threadVideo.Abort();
  2129. return;
  2130. }
  2131. }
  2132. }
  2133. else
  2134. {
  2135. if (Packet != null)
  2136. {
  2137. ffmpeg.av_free_packet(Packet);
  2138. Marshal.FreeCoTaskMem((IntPtr)Packet);
  2139. Packet = null;
  2140. }
  2141. //throw new ApplicationException(@"Could not read frame");
  2142. Debug.Log("Could not read frame");
  2143. }
  2144. }
  2145. else
  2146. {
  2147. if (Packet->stream_index == iStreamIndex)
  2148. {
  2149. lock(m_queuePacketVideo)
  2150. {
  2151. m_queuePacketVideo.Enqueue((IntPtr)Packet);
  2152. }
  2153. }
  2154. else
  2155. {
  2156. lock (m_queuePacketSound)
  2157. {
  2158. m_queuePacketSound.Enqueue((IntPtr)Packet);
  2159. }
  2160. }
  2161. }
  2162. if (bEndCheck == true)
  2163. {
  2164. if (m_queuePacketVideo.Count < 3)
  2165. {
  2166. Debug.Log("End");
  2167. bEnd = true;
  2168. if (Packet != null)
  2169. {
  2170. ffmpeg.av_free_packet(Packet);
  2171. Marshal.FreeCoTaskMem((IntPtr)Packet);
  2172. Packet = null;
  2173. }
  2174. //threadVideo.Abort();
  2175. bEndCheck = false;
  2176. return;
  2177. }
  2178. }
  2179. }
  2180. private void UpdateSound()
  2181. {
  2182. var gotSound = 0;
  2183. if (m_CurrentState != MEDIAPLAYER_STATE.PAUSED)
  2184. {
  2185. if (m_queuePacketSound.Count > 0)
  2186. pPacket = (AVPacket*)m_queuePacketSound.Dequeue();
  2187. if (pPacket != null)
  2188. {
  2189. if (pStreamAudio != null)
  2190. {
  2191. if (pPacket->stream_index == iStreamAudioIndex)
  2192. {
  2193. int iAudioLen = ffmpeg.avcodec_decode_audio4(pAudioCodecContext, pDecodedAudioFrame, &gotSound, pPacket);
  2194. if (iAudioLen >= 0)
  2195. {
  2196. if (gotSound == 1)
  2197. {
  2198. int iDataSize = ffmpeg.av_samples_get_buffer_size(null, pAudioCodecContext->channels, pDecodedAudioFrame->nb_samples, pAudioCodecContext->sample_fmt, 1);
  2199. int iDataSize2 = ffmpeg.av_samples_get_buffer_size(null, pAudioCodecContext->channels, pDecodedAudioFrame->nb_samples, AVSampleFormat.AV_SAMPLE_FMT_FLT, 1);
  2200. //if( pAudioCodecContext->sample_fmt != AVSampleFormat.AV_SAMPLE_FMT_FLT)
  2201. {
  2202. //for(int i = 0; i < pAudioCodecContext->channels; i++)
  2203. {
  2204. sbyte* outData = (sbyte*)Marshal.AllocCoTaskMem(iDataSize2 * sizeof(sbyte)); ;
  2205. SwrContext* pAudioCvtContext = null;
  2206. if (pAudioCodecContext->channel_layout == 0)
  2207. {
  2208. long layout = ffmpeg.av_get_default_channel_layout(pAudioCodecContext->channels);
  2209. pAudioCvtContext = ffmpeg.swr_alloc_set_opts(null, (long)layout, AVSampleFormat.AV_SAMPLE_FMT_FLT, pAudioCodecContext->sample_rate
  2210. , (long)layout, pAudioCodecContext->sample_fmt, pAudioCodecContext->sample_rate, 0, (void*)0);
  2211. }
  2212. else
  2213. {
  2214. pAudioCvtContext = ffmpeg.swr_alloc_set_opts(null, (long)pAudioCodecContext->channel_layout, AVSampleFormat.AV_SAMPLE_FMT_FLT, pAudioCodecContext->sample_rate
  2215. , (long)pAudioCodecContext->channel_layout, pAudioCodecContext->sample_fmt, pAudioCodecContext->sample_rate, 0, (void*)0);
  2216. }
  2217. int error = 0;
  2218. if ((error = ffmpeg.swr_init(pAudioCvtContext)) < 0)
  2219. {
  2220. Debug.Log("error " + error);
  2221. }
  2222. ffmpeg.swr_convert(pAudioCvtContext, &outData, iDataSize2, pDecodedAudioFrame->extended_data, pDecodedAudioFrame->nb_samples);
  2223. sbyte* soundFrameAddress = outData;
  2224. var soundBufferPtr = new IntPtr(soundFrameAddress);
  2225. byte[] buffer = new byte[iDataSize2];
  2226. Marshal.Copy(soundBufferPtr, buffer, 0, iDataSize2);
  2227. if ((ulong)pPacket->dts != ffmpeg.AV_NOPTS_VALUE)
  2228. {
  2229. pts = ffmpeg.av_frame_get_best_effort_timestamp(pDecodedAudioFrame);
  2230. }
  2231. else
  2232. {
  2233. pts = 0;
  2234. }
  2235. pts *= av_q2d(pStreamAudio->time_base);
  2236. if (bSeekTo == true)
  2237. {
  2238. double value = pts * (double)pDecodedAudioFrame->sample_rate;
  2239. //Debug.Log(value + " " + pts + " " + GetDuration() + " " + pDecodedAudioFrame->pkt_duration);
  2240. iSoundCount = (long)value;
  2241. fCurrentSeekTime = (float)pts;
  2242. fLastFrameTime = fCurrentSeekTime;
  2243. bSeekTo = false;
  2244. }
  2245. //Debug.Log(pts);
  2246. while (pts > 300.0f * (iInitCount + 1))
  2247. {
  2248. iSoundCount -= (long)(300.0 * (double)pDecodedAudioFrame->sample_rate);
  2249. //Debug.Log(iSoundCount);
  2250. iInitCount++;
  2251. }
  2252. //Debug.Log ("sound " + iSoundCount);
  2253. //Debug.Log (pDecodedAudioFrame->pkt_dts + " " +pDecodedAudioFrame->pkt_duration + " " + pDecodedAudioFrame->pkt_pos + " " + pDecodedAudioFrame->pkt_pts + " " + pts);
  2254. fAudioData = new float[buffer.Length / 4];
  2255. Buffer.BlockCopy(buffer, 0, fAudioData, 0, buffer.Length);
  2256. lock (listAudio)
  2257. {
  2258. listAudio.Add(fAudioData);
  2259. lock (listAudioPts)
  2260. {
  2261. lock (listAudioPtsTime)
  2262. {
  2263. iSoundCount += iDataSize2 / 4 / pDecodedAudioFrame->channels;
  2264. listAudioPts.Add(iSoundCount);
  2265. listAudioPtsTime.Add(pts);
  2266. }
  2267. }
  2268. }
  2269. //Debug.Log ( iDataSize + " "+ pts + " " + pAudioCodecContext->sample_rate + " " + pDecodedAudioFrame->sample_rate + " "+ pDecodedAudioFrame->nb_samples + " " + pDecodedAudioFrame->pkt_pts + " " + pDecodedAudioFrame->pkt_pos);
  2270. //Debug.Log ("sound decode time " + pts);
  2271. //audioClip.SetData(fAudioData,(int)(pAudioCodecContext->sample_rate * pts ) );
  2272. //audioClip.SetData(fAudioData,(int)(pDecodedAudioFrame->pkt_pts ) );
  2273. ffmpeg.swr_free(&pAudioCvtContext);
  2274. Marshal.FreeCoTaskMem((IntPtr)outData);
  2275. }
  2276. }
  2277. }
  2278. }
  2279. }
  2280. }
  2281. }
  2282. if (pPacket != null)
  2283. {
  2284. ffmpeg.av_free_packet(pPacket);
  2285. Marshal.FreeCoTaskMem((IntPtr)pPacket);
  2286. pPacket = null;
  2287. }
  2288. }
  2289. }
  2290. private void UpdateVideo()
  2291. {
  2292. var gotPicture = 0;
  2293. if (m_queuePacketVideo.Count < 3)
  2294. {
  2295. bBuffering = true;
  2296. }
  2297. if(m_queuePacketVideo.Count > 10 || bEndCheck == true)
  2298. {
  2299. bBuffering = false;
  2300. }
  2301. if(bBuffering == true)
  2302. {
  2303. return;
  2304. }
  2305. if ( m_CurrentState != MEDIAPLAYER_STATE.PAUSED)
  2306. {
  2307. if(m_queuePacketVideo.Count > 0)
  2308. pPacket = (AVPacket*)m_queuePacketVideo.Dequeue();
  2309. if (pPacket != null)
  2310. {
  2311. if (pPacket->stream_index == iStreamIndex) {
  2312. var size = ffmpeg.avcodec_decode_video2 (pCodecContext, pDecodedFrame, &gotPicture, pPacket);
  2313. if (size < 0) {
  2314. //throw new ApplicationException (string.Format (@"Error while decoding frame "));
  2315. Debug.Log("Error while decoding frame");
  2316. return;
  2317. }
  2318. if ((ulong)pPacket->dts != ffmpeg.AV_NOPTS_VALUE) {
  2319. pts = ffmpeg.av_frame_get_best_effort_timestamp (pDecodedFrame);
  2320. } else {
  2321. pts = 0;
  2322. }
  2323. pts *= av_q2d (pStream->time_base);
  2324. if (gotPicture == 1) {
  2325. //Debug.Log("Video " + pts);
  2326. if (pts > 0) {
  2327. if (listVideo.Count > 2)
  2328. {
  2329. if (!m_bIsFirstFrameReady) {
  2330. //Debug.Log("FistReady" + listVideo.Count);
  2331. m_bIsFirstFrameReady = true;
  2332. bVideoFirstFrameReady = true;
  2333. }
  2334. }
  2335. }
  2336. //Debug.Log ("Video " + pts);
  2337. //fLastFrameTime = (float)pts;
  2338. sbyte** src = &pDecodedFrame->data0;
  2339. sbyte** dst = &pConvertedFrame->data0;
  2340. int* srcStride = pDecodedFrame->linesize;
  2341. int* dstStride = pConvertedFrame->linesize;
  2342. int iLineSize = srcStride[0] ;
  2343. int iLineSize1 = srcStride[1] ;
  2344. int iLineSize2 = srcStride[2] ;
  2345. if (pCodecContext->width != m_iWidth)
  2346. {
  2347. Debug.Log(pCodecContext->width + " " + pCodecContext->height);
  2348. lock (listVideo)
  2349. {
  2350. listVideo.Clear();
  2351. lock (listVideoPts)
  2352. {
  2353. listVideoPts.Clear();
  2354. }
  2355. }
  2356. if (pConvertedFrame != null)
  2357. {
  2358. ffmpeg.av_free(pConvertedFrame);
  2359. pConvertedFrame = null;
  2360. }
  2361. if (pConvertedFrameBuffer != null)
  2362. {
  2363. ffmpeg.av_free(pConvertedFrameBuffer);
  2364. pConvertedFrameBuffer = null;
  2365. }
  2366. if (pConvertContext != null)
  2367. {
  2368. ffmpeg.sws_freeContext(pConvertContext);
  2369. pConvertContext = null;
  2370. }
  2371. var convertToPixFmt = AVPixelFormat.AV_PIX_FMT_RGBA;
  2372. #if UNITY_5
  2373. if (bDirect9)
  2374. {
  2375. convertToPixFmt = AVPixelFormat.AV_PIX_FMT_BGRA;
  2376. }
  2377. #endif
  2378. var codecContext = *pStream->codec;
  2379. var sourcePixFmt = codecContext.pix_fmt;
  2380. var codecId = codecContext.codec_id;
  2381. pConvertContext = ffmpeg.sws_getContext(pCodecContext->width, pCodecContext->height, sourcePixFmt,
  2382. pCodecContext->width, pCodecContext->height, convertToPixFmt,
  2383. ffmpeg.SWS_FAST_BILINEAR, null, null, null);
  2384. if (pConvertContext == null)
  2385. {
  2386. m_CurrentState = MEDIAPLAYER_STATE.ERROR;
  2387. Debug.Log("Could not initialize the conversion context");
  2388. throw new ApplicationException(@"Could not initialize the conversion context");
  2389. }
  2390. //pConvertedFrame = ffmpeg.avcodec_alloc_frame();
  2391. pConvertedFrame = ffmpeg.av_frame_alloc();
  2392. var convertedFrameBufferSize = ffmpeg.avpicture_get_size(convertToPixFmt, pCodecContext->width, pCodecContext->height);
  2393. pConvertedFrameBuffer = (sbyte*)ffmpeg.av_malloc((ulong)convertedFrameBufferSize);
  2394. AVPicture* tempPicture = (AVPicture*)pConvertedFrame;
  2395. ffmpeg.avpicture_fill(tempPicture, pConvertedFrameBuffer, convertToPixFmt, pCodecContext->width, pCodecContext->height);
  2396. }
  2397. else
  2398. {
  2399. if (m_bPC_FastMode == true)
  2400. {
  2401. int iVideoSize = m_iWidth * m_iHeight + (2 * m_iWidth / 2 * m_iHeight / 2);
  2402. byte[] buffer = new byte[iVideoSize];
  2403. if (iLineSize != m_iWidth)
  2404. {
  2405. sbyte* convertedFrameAddress;
  2406. IntPtr imageBufferPtr;
  2407. for (int i = 0; i < m_iHeight; i++)
  2408. {
  2409. convertedFrameAddress = pDecodedFrame->data0 + iLineSize * i;
  2410. imageBufferPtr = new IntPtr(convertedFrameAddress);
  2411. Marshal.Copy(imageBufferPtr, buffer, m_iWidth * i, m_iWidth);
  2412. }
  2413. for (int i = 0; i < m_iHeight / 2; i++)
  2414. {
  2415. convertedFrameAddress = pDecodedFrame->data1 + iLineSize1 * i;
  2416. imageBufferPtr = new IntPtr(convertedFrameAddress);
  2417. Marshal.Copy(imageBufferPtr, buffer, m_iWidth * m_iHeight + m_iWidth / 2 * i, m_iWidth / 2);
  2418. convertedFrameAddress = pDecodedFrame->data2 + iLineSize2 * i;
  2419. imageBufferPtr = new IntPtr(convertedFrameAddress);
  2420. Marshal.Copy(imageBufferPtr, buffer, m_iWidth * m_iHeight + m_iWidth / 2 * m_iHeight / 2 + m_iWidth / 2 * i, m_iWidth / 2);
  2421. }
  2422. }
  2423. else
  2424. {
  2425. sbyte* convertedFrameAddress = pDecodedFrame->data0;
  2426. var imageBufferPtr = new IntPtr(convertedFrameAddress);
  2427. Marshal.Copy(imageBufferPtr, buffer, 0, m_iWidth * m_iHeight);
  2428. convertedFrameAddress = pDecodedFrame->data1;
  2429. imageBufferPtr = new IntPtr(convertedFrameAddress);
  2430. Marshal.Copy(imageBufferPtr, buffer, m_iWidth * m_iHeight, m_iWidth / 2 * m_iHeight / 2);
  2431. convertedFrameAddress = pDecodedFrame->data2;
  2432. imageBufferPtr = new IntPtr(convertedFrameAddress);
  2433. Marshal.Copy(imageBufferPtr, buffer, m_iWidth * m_iHeight + m_iWidth / 2 * m_iHeight / 2, m_iWidth / 2 * m_iHeight / 2);
  2434. }
  2435. lock (listVideo)
  2436. {
  2437. listVideo.Enqueue(buffer);
  2438. lock (listVideoPts)
  2439. {
  2440. listVideoPts.Enqueue((float)pts);
  2441. }
  2442. }
  2443. }
  2444. else
  2445. {
  2446. Debug.Log(iLineSize.ToString());
  2447. ffmpeg.sws_scale(pConvertContext, src, srcStride, 0, m_iHeight, dst, dstStride);
  2448. sbyte* convertedFrameAddress = pConvertedFrame->data0;
  2449. var imageBufferPtr = new IntPtr(convertedFrameAddress);
  2450. byte[] buffer = new byte[4 * m_iWidth * m_iHeight];
  2451. Marshal.Copy(imageBufferPtr, buffer, 0, 4 * m_iWidth * m_iHeight);
  2452. lock (listVideo)
  2453. {
  2454. listVideo.Enqueue(buffer);
  2455. lock (listVideoPts)
  2456. {
  2457. listVideoPts.Enqueue((float)pts);
  2458. }
  2459. }
  2460. }
  2461. }
  2462. }
  2463. }
  2464. }
  2465. if (pPacket != null)
  2466. {
  2467. ffmpeg.av_free_packet(pPacket);
  2468. Marshal.FreeCoTaskMem((IntPtr)pPacket);
  2469. pPacket = null;
  2470. }
  2471. }
  2472. }
  2473. int iSoundBufferCount = 0;
  2474. /*void OnAudioSetPosition(int newPosition)
  2475. {
  2476. //Debug.Log (newPosition);
  2477. //Debug.Log("Pts" + listAudioPtsTime[0]);
  2478. }*/
  2479. void OnAudioRead(float[] data)
  2480. {
  2481. //Debug.Log (listAudio.Count);
  2482. if (listAudio.Count < 3)
  2483. {
  2484. return;
  2485. }
  2486. //Debug.Log (data.Length + " " + listAudio[1].Length);
  2487. if (iSoundBufferCount == 0) {
  2488. iSoundBufferCount = listAudio[0].Length;
  2489. }
  2490. if (iSoundBufferCount < data.Length) {
  2491. Array.Copy (listAudio [0], listAudio [0].Length - iSoundBufferCount, data, 0, iSoundBufferCount);
  2492. //Debug.Log (iSoundBufferCount + " " + data.Length + " " + listAudio [1].Length);
  2493. if ((data.Length - iSoundBufferCount) > listAudio [1].Length) {
  2494. Array.Copy (listAudio [1], 0, data, 0, listAudio [1].Length);
  2495. Array.Copy (listAudio [2], 0, data, iSoundBufferCount + listAudio [1].Length, data.Length - iSoundBufferCount - listAudio [1].Length);
  2496. iSoundBufferCount = listAudio[2].Length - (data.Length - iSoundBufferCount - listAudio [1].Length);
  2497. listAudio.RemoveAt (0);
  2498. listAudioPts.RemoveAt (0);
  2499. listAudioPtsTime.RemoveAt (0);
  2500. } else {
  2501. Array.Copy (listAudio [1], 0, data, iSoundBufferCount, data.Length - iSoundBufferCount);
  2502. iSoundBufferCount = listAudio[1].Length - (data.Length - iSoundBufferCount);
  2503. }
  2504. listAudio.RemoveAt (0);
  2505. listAudioPts.RemoveAt (0);
  2506. listAudioPtsTime.RemoveAt (0);
  2507. } else {
  2508. Array.Copy (listAudio [0], listAudio [0].Length - iSoundBufferCount, data,0, data.Length);
  2509. iSoundBufferCount -= data.Length;
  2510. }
  2511. if (iSoundBufferCount == 0) {
  2512. listAudio.RemoveAt (0);
  2513. listAudioPts.RemoveAt (0);
  2514. listAudioPtsTime.RemoveAt (0);
  2515. iSoundBufferCount = listAudio[0].Length;
  2516. }
  2517. }
  2518. private int m_LastAudioPartSetted = -1;
  2519. Material material = null;
  2520. bool m_bSetTextureFirst = false;
  2521. private void Call_UpdateVideoTexture()
  2522. {
  2523. if (bEnd == true && listVideo.Count == 0 )
  2524. {
  2525. m_CurrentState = MEDIAPLAYER_STATE.END;
  2526. if (OnEnd != null)
  2527. {
  2528. OnEnd();
  2529. //return;
  2530. }
  2531. if (m_bLoop == true)
  2532. {
  2533. Destroy (audioClip);
  2534. audioClip = null;
  2535. Load (m_strFileName);
  2536. bEnd = false;
  2537. return;
  2538. }
  2539. bEnd = false;
  2540. return;
  2541. }
  2542. /*if (bInterrupt == true)
  2543. {
  2544. if (audioSource != null)
  2545. {
  2546. audioSource.Pause();
  2547. }
  2548. }
  2549. else*/
  2550. {
  2551. if (audioSource != null && m_CurrentState == MEDIAPLAYER_STATE.PLAYING && m_bIsFirstFrameReady == true)
  2552. {
  2553. //if( audioSource.isPlaying == false/* && audioSource.time > 0.02f*/)
  2554. // audioSource.Play();
  2555. }
  2556. }
  2557. if (threadVideo == null && m_CurrentState != MEDIAPLAYER_STATE.END && m_CurrentState != MEDIAPLAYER_STATE.NOT_READY && m_CurrentState != MEDIAPLAYER_STATE.ERROR)
  2558. {
  2559. threadVideo = new Thread(ThreadUpdate);
  2560. threadVideo.IsBackground = true;
  2561. threadVideo.Start();
  2562. }
  2563. if (threadVideo2 == null && m_CurrentState != MEDIAPLAYER_STATE.END && m_CurrentState != MEDIAPLAYER_STATE.NOT_READY && m_CurrentState != MEDIAPLAYER_STATE.ERROR )
  2564. {
  2565. threadVideo2 = new Thread(ThreadUpdate2);
  2566. threadVideo2.IsBackground = true;
  2567. threadVideo2.Start();
  2568. }
  2569. if ( m_CurrentState == MEDIAPLAYER_STATE.PLAYING && m_bIsFirstFrameReady == true )
  2570. {
  2571. if( bBuffering == true)
  2572. {
  2573. if (audioSource != null)
  2574. audioSource.Pause();
  2575. }
  2576. if (listVideo.Count > 0 && bBuffering == false)
  2577. {
  2578. fCurrentSeekTime += Time.deltaTime * m_fSpeed;
  2579. }
  2580. }
  2581. //Debug.Log("last " + fLastFrameTime + " " + fCurrentSeekTime + " " + listVideo.Count + " " + m_queuePacketVideo.Count);
  2582. if ( fLastFrameTime > fCurrentSeekTime - 0.2f )
  2583. {
  2584. for (int i = 0; i < listAudio.Count; i++)
  2585. {
  2586. if( listAudioPtsTime.Count > i)
  2587. {
  2588. if(audioSource == null && (int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + ((float)Call_GetDuration() / 1000.0f))) > 0)
  2589. {
  2590. audioSource = gameObject.AddComponent<AudioSource>();
  2591. audioSource.volume = m_fVolume;
  2592. }
  2593. }
  2594. if(audioClip == null && audioSource != null )
  2595. {
  2596. /*if ((float)Call_GetDuration() <= 0)
  2597. {
  2598. audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + 300.0f)),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,true,OnAudioRead);
  2599. }
  2600. else*/
  2601. {
  2602. #if UNITY_5
  2603. audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * 300.0f),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,false);
  2604. #else
  2605. audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * 300.0f),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,false,false);
  2606. #endif
  2607. }
  2608. audioSource.clip = audioClip;
  2609. }
  2610. if (audioSource != null && Call_GetDuration() >0)
  2611. {
  2612. if( listAudioPts.Count > i)
  2613. {
  2614. if (listAudioPts [i] >= 0) {
  2615. {
  2616. /*if( listAudioPts[i] > (int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + ((float)Call_GetDuration() / 1000.0f))))
  2617. {
  2618. audioClip.SetData(listAudio[i],(int)(((double)pAudioCodecContext->sample_rate) * listAudioPtsTime[i] ) );
  2619. }
  2620. else*/
  2621. {
  2622. //Debug.Log(audioSource.time + " " + (float)listAudioPtsTime[i] + " " +listAudioPts [i] +" " + fLastFrameTime + " " + listAudio[i].Length);
  2623. audioClip.SetData(listAudio[i],(int)(listAudioPts[i] % (pAudioCodecContext->sample_rate * 300.0f)) );
  2624. // audioClip.SetData(listAudio[i], (int)(listAudioPtsTime[i] % 300.0 * (double)pAudioCodecContext->sample_rate));
  2625. }
  2626. }
  2627. }
  2628. }
  2629. }
  2630. }
  2631. if (audioSource != null && audioSource.isPlaying && Call_GetDuration() >0) {
  2632. listAudio.Clear();
  2633. listAudioPts.Clear();
  2634. listAudioPtsTime.Clear();
  2635. }
  2636. }
  2637. else
  2638. {
  2639. //do
  2640. {
  2641. #if (UNITY_5_2 || UNITY_5_3_OR_NEWER)
  2642. if( listVideo.Count > 0)
  2643. {
  2644. if(audioSource != null)
  2645. if (audioSource.isPlaying == false )
  2646. audioSource.Play();
  2647. if (m_bPC_FastMode == true)
  2648. {
  2649. if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Direct3D11)
  2650. {
  2651. m_texPtrY = m_VideoTextureY.GetNativeTexturePtr();
  2652. m_texPtrU = m_VideoTextureU.GetNativeTexturePtr();
  2653. m_texPtrV = m_VideoTextureV.GetNativeTexturePtr();
  2654. }
  2655. SetTextureFromUnity (m_iID,(IntPtr)0,m_texPtrY,m_texPtrU,m_texPtrV, m_iWidth, m_iHeight, listVideo.Dequeue());
  2656. if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Metal)
  2657. {
  2658. GL.IssuePluginEvent(GetRenderEventFunc(), 8000 + m_iID);
  2659. }
  2660. else
  2661. {
  2662. GL.IssuePluginEvent(GetRenderEventFunc(), 7000 + m_iID);
  2663. }
  2664. }
  2665. else
  2666. {
  2667. if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Direct3D11)
  2668. {
  2669. m_texPtr = m_VideoTexture.GetNativeTexturePtr();
  2670. }
  2671. SetTextureFromUnity (m_iID,m_texPtr,(IntPtr)0,(IntPtr)0,(IntPtr)0, m_iWidth, m_iHeight, listVideo.Dequeue());
  2672. if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Metal)
  2673. {
  2674. GL.IssuePluginEvent(GetRenderEventFunc(), 8000 + m_iID);
  2675. }
  2676. else
  2677. {
  2678. GL.IssuePluginEvent(GetRenderEventFunc(), 7000 + m_iID);
  2679. }
  2680. }
  2681. }
  2682. if( listVideoPts.Count > 0)
  2683. {
  2684. float fpts = listVideoPts.Dequeue();
  2685. if (fpts > fCurrentSeekTime + 2.0f)
  2686. {
  2687. fCurrentSeekTime = fpts;
  2688. double value = pts * (double)pDecodedAudioFrame->sample_rate;
  2689. //Debug.Log(value + " " + pts + " " + GetDuration() + " " + pDecodedAudioFrame->pkt_duration);
  2690. iSoundCount = (long)value;
  2691. }
  2692. //Debug.Log(fpts + " " + audioSource.time);
  2693. /* if (fpts > fCurrentSeekTime)
  2694. {
  2695. fCurrentSeekTime = fpts;
  2696. }*/
  2697. if (fLastFrameTime == 0)
  2698. {
  2699. {
  2700. if(fpts<0)
  2701. {
  2702. fLastFrameTime = 0;
  2703. }
  2704. else
  2705. {
  2706. //Debug.Log(fpts);
  2707. //fCurrentSeekTime = fpts;
  2708. fLastFrameTime = fpts;
  2709. }
  2710. if( audioSource != null && Call_GetDuration() >0 )
  2711. {
  2712. //audioSource.time = fLastFrameTime%300.0f;
  2713. }
  2714. }
  2715. }
  2716. else
  2717. {
  2718. if(fpts<=0)
  2719. fLastFrameTime = fCurrentSeekTime - 0.05f;
  2720. else
  2721. fLastFrameTime = fpts;
  2722. }
  2723. }
  2724. if( audioSource != null && Call_GetDuration() >= 0)
  2725. {
  2726. if( audioSource.time - fLastFrameTime%300.0f > 0.1f)
  2727. {
  2728. //Debug.Log("sync1 " + audioSource.time + " " + fLastFrameTime);
  2729. audioSource.time = fLastFrameTime%300.0f;
  2730. }
  2731. else if( audioSource.time - fLastFrameTime%300.0f < -0.1f)
  2732. {
  2733. //Debug.Log("sync2");
  2734. audioSource.time = fLastFrameTime%300.0f;
  2735. }
  2736. }
  2737. #else
  2738. //var imageBufferPtr = new IntPtr (convertedFrameAddress);
  2739. if( listVideo.Count > 0)
  2740. {
  2741. m_VideoTexture.LoadRawTextureData(listVideo.Dequeue());
  2742. m_VideoTexture.Apply();
  2743. }
  2744. if( listVideoPts.Count > 0)
  2745. {
  2746. float fpts = listVideoPts.Dequeue();
  2747. if(fLastFrameTime == 0)
  2748. {
  2749. if( fpts > fCurrentSeekTime)
  2750. {
  2751. fLastFrameTime = fCurrentSeekTime;
  2752. }
  2753. }
  2754. else
  2755. {
  2756. fLastFrameTime = fpts;
  2757. }
  2758. }
  2759. if( audioSource != null)
  2760. {
  2761. /*if( audioSource.time - fLastFrameTime > 0)
  2762. {
  2763. //Debug.Log("sync1");
  2764. audioSource.time = fLastFrameTime;
  2765. }
  2766. else if( audioSource.time - fLastFrameTime < -0.2f)
  2767. {
  2768. //Debug.Log("sync2");
  2769. audioSource.time = fLastFrameTime;
  2770. }*/
  2771. }
  2772. #endif
  2773. //if (m_bSetTextureFirst == false)
  2774. {
  2775. m_bSetTextureFirst = true;
  2776. if (m_shaderYUV == null)
  2777. {
  2778. m_shaderYUV = Shader.Find("Unlit/Unlit_YUV");
  2779. }
  2780. if (material == null)
  2781. {
  2782. material = new Material(m_shaderYUV);
  2783. }
  2784. if (m_TargetMaterial != null)
  2785. {
  2786. for (int i = 0; i < m_TargetMaterial.Length; i++)
  2787. {
  2788. if (m_TargetMaterial[i] == null)
  2789. continue;
  2790. if (m_TargetMaterial[i].GetComponent<MeshRenderer>() != null)
  2791. {
  2792. if (m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTexture != m_VideoTexture)
  2793. {
  2794. m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTexture = m_VideoTexture;
  2795. }
  2796. }
  2797. if (m_bPC_FastMode == true)
  2798. {
  2799. if (m_TargetMaterial[i].GetComponent<MeshRenderer>() != null)
  2800. {
  2801. material.mainTextureScale = m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTextureScale;
  2802. material.mainTextureOffset = m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTextureOffset;
  2803. m_TargetMaterial[i].GetComponent<MeshRenderer>().material = material;
  2804. if (m_TargetMaterial[i].GetComponent<MeshRenderer>().material.GetTexture("_Y") != m_VideoTextureY)
  2805. {
  2806. m_TargetMaterial[i].GetComponent<MeshRenderer>().material.SetTexture("_Y", m_VideoTextureY);
  2807. }
  2808. if (m_TargetMaterial[i].GetComponent<MeshRenderer>().material.GetTexture("_U") != m_VideoTextureU)
  2809. {
  2810. m_TargetMaterial[i].GetComponent<MeshRenderer>().material.SetTexture("_U", m_VideoTextureU);
  2811. }
  2812. if (m_TargetMaterial[i].GetComponent<MeshRenderer>().material.GetTexture("_V") != m_VideoTextureV)
  2813. {
  2814. m_TargetMaterial[i].GetComponent<MeshRenderer>().material.SetTexture("_V", m_VideoTextureV);
  2815. }
  2816. }
  2817. }
  2818. if (m_TargetMaterial[i].GetComponent<RawImage>() != null)
  2819. {
  2820. if (m_TargetMaterial[i].GetComponent<RawImage>().texture != m_VideoTexture)
  2821. {
  2822. m_TargetMaterial[i].GetComponent<RawImage>().texture = m_VideoTexture;
  2823. }
  2824. }
  2825. if (m_bPC_FastMode == true)
  2826. {
  2827. if (m_TargetMaterial[i].GetComponent<RawImage>() != null)
  2828. {
  2829. m_TargetMaterial[i].GetComponent<RawImage>().material = material;
  2830. if (m_TargetMaterial[i].GetComponent<RawImage>().material.GetTexture("_Y") != m_VideoTextureY)
  2831. {
  2832. m_TargetMaterial[i].GetComponent<RawImage>().material.SetTexture("_Y", m_VideoTextureY);
  2833. }
  2834. if (m_TargetMaterial[i].GetComponent<RawImage>().material.GetTexture("_U") != m_VideoTextureU)
  2835. {
  2836. m_TargetMaterial[i].GetComponent<RawImage>().material.SetTexture("_U", m_VideoTextureU);
  2837. }
  2838. if (m_TargetMaterial[i].GetComponent<RawImage>().material.GetTexture("_V") != m_VideoTextureV)
  2839. {
  2840. m_TargetMaterial[i].GetComponent<RawImage>().material.SetTexture("_V", m_VideoTextureV);
  2841. }
  2842. }
  2843. }
  2844. }
  2845. }
  2846. }
  2847. if (bVideoFirstFrameReady == true) {
  2848. if (OnVideoFirstFrameReady != null) {
  2849. OnVideoFirstFrameReady ();
  2850. bVideoFirstFrameReady = false;
  2851. }
  2852. if (listAudio.Count > 0)
  2853. {
  2854. if (audioSource == null /*&& (int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + ((float)Call_GetDuration() / 1000.0f))) > 0*/)
  2855. {
  2856. audioSource = gameObject.AddComponent<AudioSource>();
  2857. }
  2858. if (audioClip == null && audioSource != null)
  2859. {
  2860. /*if ((float)Call_GetDuration() <= 0)
  2861. {
  2862. audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + 300.0f)),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,true,OnAudioRead);
  2863. }
  2864. else*/
  2865. {
  2866. /*if (Call_GetDuration () > 1500000) {
  2867. audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + 10.0f)),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,false);
  2868. }
  2869. else{
  2870. audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + 300.0f)),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,false);
  2871. }*/
  2872. #if UNITY_5
  2873. audioClip = AudioClip.Create("videoAudio", (int)((float)pAudioCodecContext->sample_rate * 300.0f), pAudioCodecContext->channels, pAudioCodecContext->sample_rate, false);
  2874. #else
  2875. audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * 300.0f),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,false,false);
  2876. #endif
  2877. }
  2878. audioSource.clip = audioClip;
  2879. //audioSource.Play();
  2880. //AudioSource.PlayClipAtPoint(audioClip,new Vector3(0,0,0));
  2881. }
  2882. }
  2883. // Just to be sure I guess - don't know what migh happen if I seek in video etc...
  2884. if (m_LastAudioPartSetted > listAudio.Count)
  2885. {
  2886. m_LastAudioPartSetted = -1;
  2887. }
  2888. // using m_LastAudioPartSetted - 1 again to be sure - what if the last part of audio gets updated/appended? IDK...
  2889. for (int i = Math.Max(0, m_LastAudioPartSetted - 1); i < listAudio.Count; i++) {
  2890. m_LastAudioPartSetted = i;
  2891. if (audioSource != null && Call_GetDuration() >0 || Call_GetDuration() == 0) {
  2892. if (listAudioPts.Count > i) {
  2893. if (listAudioPts [i] >= 0) {
  2894. {
  2895. /*if (listAudioPts [i] > (int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime [i] + ((float)Call_GetDuration () / 1000.0f)))) {
  2896. audioClip.SetData (listAudio [i], (int)(((double)pAudioCodecContext->sample_rate) * listAudioPtsTime [i]));
  2897. } else*/ {
  2898. //Debug.Log(audioSource.time + " " + (float)listAudioPtsTime[i] + " " +listAudioPts [i] +" " + fLastFrameTime + " " + listAudio[i].Length);
  2899. audioClip.SetData(listAudio[i],(int)(listAudioPts[i] % (pAudioCodecContext->sample_rate * 300.0f)) );
  2900. }
  2901. }
  2902. }
  2903. }
  2904. //if(audioSource.isPlaying == false)
  2905. //audioSource.Play();
  2906. }
  2907. }
  2908. }
  2909. }//while(fLastFrameTime < fCurrentSeekTime );
  2910. //Debug.Log(listAudio.Count);
  2911. }
  2912. }
  2913. private void Call_SetVolume(float fVolume)
  2914. {
  2915. if (audioSource != null)
  2916. audioSource.volume = fVolume;
  2917. }
  2918. bool bSeekTo = false;
  2919. private void Call_SetSeekPosition(int iSeek)
  2920. {
  2921. if ( threadVideo != null)
  2922. {
  2923. while(threadVideo.IsAlive == true)
  2924. {
  2925. threadVideo.Abort();
  2926. }
  2927. threadVideo = null;
  2928. }
  2929. if (threadVideo2 != null)
  2930. {
  2931. while (threadVideo2.IsAlive == true)
  2932. {
  2933. threadVideo2.Abort();
  2934. }
  2935. threadVideo2 = null;
  2936. }
  2937. bSeekTo = true;
  2938. iInitCount = 0;
  2939. long seek_target = (long)iSeek * 1000;
  2940. Debug.Log (seek_target);
  2941. seek_target= ffmpeg.av_rescale_q(seek_target, ffmpeg.av_get_time_base_q() , pStream->time_base);
  2942. Debug.Log (seek_target);
  2943. //int seek_flags = iSeek - (int)(fLastFrameTime * 1000.0f) < 0 ? ffmpeg.AVSEEK_FLAG_BACKWARD : ffmpeg.AVSEEK_FLAG_BACKWARD;
  2944. if(ffmpeg.av_seek_frame(pFormatContext, iStreamIndex,
  2945. seek_target, ffmpeg.AVSEEK_FLAG_BACKWARD) < 0) {
  2946. //error
  2947. } else {
  2948. /* handle packet queues... more later... */
  2949. }
  2950. fCurrentSeekTime = (float)iSeek / 1000.0f;
  2951. //fLastFrameTime = (float)iSeek / 1000.0f;
  2952. listVideo.Clear();
  2953. listVideoPts.Clear();
  2954. listAudio.Clear();
  2955. listAudioPts.Clear();
  2956. listAudioPtsTime.Clear();
  2957. int iCount = m_queuePacketVideo.Count;
  2958. for (int i = 0; i < iCount; i++)
  2959. {
  2960. if (m_queuePacketVideo.Count > 0)
  2961. pPacket = (AVPacket*)m_queuePacketVideo.Dequeue();
  2962. if (pPacket != null)
  2963. {
  2964. ffmpeg.av_free_packet(pPacket);
  2965. Marshal.FreeCoTaskMem((IntPtr)pPacket);
  2966. pPacket = null;
  2967. }
  2968. }
  2969. iCount = m_queuePacketSound.Count;
  2970. for (int i = 0; i < iCount; i++)
  2971. {
  2972. if (m_queuePacketSound.Count > 0)
  2973. pPacket = (AVPacket*)m_queuePacketSound.Dequeue();
  2974. if (pPacket != null)
  2975. {
  2976. ffmpeg.av_free_packet(pPacket);
  2977. Marshal.FreeCoTaskMem((IntPtr)pPacket);
  2978. pPacket = null;
  2979. }
  2980. }
  2981. Destroy(audioClip);
  2982. audioClip = null;
  2983. ffmpeg.avcodec_flush_buffers (pCodecContext);
  2984. //Debug.Log (fLastFrameTime + " " + fCurrentSeekTime);
  2985. }
  2986. private int Call_GetSeekPosition()
  2987. {
  2988. return (int)(fCurrentSeekTime * 1000.0f);
  2989. }
  2990. private void Call_Play(int iSeek)
  2991. {
  2992. if (m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.END || m_CurrentState == MEDIAPLAYER_STATE.PAUSED) {
  2993. SeekTo (iSeek);
  2994. if (audioSource != null) {
  2995. audioSource.time = (float) iSeek / 1000.0f;
  2996. }
  2997. bEnd = false;
  2998. m_CurrentState = MEDIAPLAYER_STATE.PLAYING;
  2999. }
  3000. }
  3001. private void Call_Reset()
  3002. {
  3003. }
  3004. private void Call_Stop()
  3005. {
  3006. UnLoad();
  3007. m_CurrentState = MEDIAPLAYER_STATE.STOPPED;
  3008. }
  3009. private void Call_RePlay()
  3010. {
  3011. if (audioSource != null) {
  3012. //if( audioSource.isPlaying == false)
  3013. // audioSource.Play();
  3014. }
  3015. if(m_CurrentState == MEDIAPLAYER_STATE.PAUSED)
  3016. m_CurrentState = MEDIAPLAYER_STATE.PLAYING;
  3017. }
  3018. private void Call_Pause()
  3019. {
  3020. if (audioSource != null) {
  3021. audioSource.Pause();
  3022. }
  3023. m_CurrentState = MEDIAPLAYER_STATE.PAUSED;
  3024. }
  3025. private int Call_GetVideoWidth()
  3026. {
  3027. //Debug.Log (m_iWidth);
  3028. if(pCodecContext != null)
  3029. return pCodecContext->width;
  3030. return m_iWidth;
  3031. }
  3032. private int Call_GetVideoHeight()
  3033. {
  3034. if (pCodecContext != null)
  3035. return pCodecContext->height;
  3036. //Debug.Log (m_iHeight);
  3037. return m_iHeight;
  3038. }
  3039. private void Call_SetUnityTexture(int iTextureID)
  3040. {
  3041. }
  3042. private void Call_SetWindowSize()
  3043. {
  3044. }
  3045. private void Call_SetLooping(bool bLoop)
  3046. {
  3047. }
  3048. private void Call_SetRockchip(bool bValue)
  3049. {
  3050. }
  3051. public void Call_SetUnityActivity()
  3052. {
  3053. ffmpeg.av_register_all();
  3054. ffmpeg.avcodec_register_all();
  3055. ffmpeg.avformat_network_init();
  3056. m_iID = SetTexture ();
  3057. }
  3058. private int Call_GetError()
  3059. {
  3060. return 0;
  3061. }
  3062. private int Call_GetErrorExtra()
  3063. {
  3064. return 0;
  3065. }
  3066. private int Call_GetDuration()
  3067. {
  3068. //Debug.Log ((int)(pFormatContext->duration / 1000));
  3069. if (pFormatContext != null)
  3070. return (int)(pFormatContext->duration / 1000);
  3071. return 0;
  3072. }
  3073. private int Call_GetCurrentSeekPercent()
  3074. {
  3075. if (pStream != null)
  3076. {
  3077. return (int)(((double)fCurrentSeekTime + ((double)m_queuePacketVideo.Count / av_q2d(pStream->avg_frame_rate))) / (Call_GetDuration() / 1000.0) * 100);
  3078. }
  3079. return -1;
  3080. }
  3081. private void Call_SetSplitOBB(bool bValue, string strOBBName)
  3082. {
  3083. }
  3084. private void Call_SelectSoundTrack(int iIndex)
  3085. {
  3086. }
  3087. private int[] Call_GetSoundTrack()
  3088. {
  3089. return null;
  3090. }
  3091. private void Call_SetSpeed(float fSpeed)
  3092. {
  3093. if (audioSource != null) {
  3094. audioSource.pitch = fSpeed;
  3095. }
  3096. }
  3097. private MEDIAPLAYER_STATE Call_GetStatus()
  3098. {
  3099. return m_CurrentState;
  3100. }
  3101. #endif // !UNITY_EDITOR
  3102. public IEnumerator DownloadStreamingVideoAndLoad(string strURL)
  3103. {
  3104. strURL = strURL.Trim();
  3105. Debug.Log("DownloadStreamingVideo : " + strURL);
  3106. WWW www = new WWW(strURL);
  3107. yield return www;
  3108. if (string.IsNullOrEmpty(www.error))
  3109. {
  3110. if (System.IO.Directory.Exists(Application.persistentDataPath + "/Data") == false)
  3111. System.IO.Directory.CreateDirectory(Application.persistentDataPath + "/Data");
  3112. string write_path = Application.persistentDataPath + "/Data" + strURL.Substring(strURL.LastIndexOf("/"));
  3113. /* if(System.IO.File.Exists(write_path) == true)
  3114. {
  3115. Debug.Log("Delete : " + write_path);
  3116. System.IO.File.Delete(write_path);
  3117. }
  3118. */
  3119. System.IO.File.WriteAllBytes(write_path, www.bytes);
  3120. Load("file://" + write_path);
  3121. }
  3122. else
  3123. {
  3124. Debug.Log(www.error);
  3125. }
  3126. www.Dispose();
  3127. www = null;
  3128. Resources.UnloadUnusedAssets();
  3129. }
  3130. public IEnumerator DownloadStreamingVideoAndLoad2(string strURL)
  3131. {
  3132. strURL = strURL.Trim();
  3133. string write_path = Application.persistentDataPath + "/Data" + strURL.Substring(strURL.LastIndexOf("/"));
  3134. if (System.IO.File.Exists(write_path) == true)
  3135. {
  3136. Load("file://" + write_path);
  3137. }
  3138. else
  3139. {
  3140. WWW www = new WWW(strURL);
  3141. yield return www;
  3142. if (string.IsNullOrEmpty(www.error))
  3143. {
  3144. if (System.IO.Directory.Exists(Application.persistentDataPath + "/Data") == false)
  3145. System.IO.Directory.CreateDirectory(Application.persistentDataPath + "/Data");
  3146. System.IO.File.WriteAllBytes(write_path, www.bytes);
  3147. Load("file://" + write_path);
  3148. }
  3149. else
  3150. {
  3151. Debug.Log(www.error);
  3152. }
  3153. www.Dispose();
  3154. www = null;
  3155. Resources.UnloadUnusedAssets();
  3156. }
  3157. }
  3158. IEnumerator CopyStreamingAssetVideoAndLoad(string strURL)
  3159. {
  3160. strURL = strURL.Trim();
  3161. string write_path = Application.persistentDataPath + "/" + strURL;
  3162. if (System.IO.File.Exists(write_path) == false)
  3163. {
  3164. Debug.Log("CopyStreamingAssetVideoAndLoad : " + strURL);
  3165. WWW www = new WWW(Application.streamingAssetsPath + "/" + strURL);
  3166. yield return www;
  3167. if (string.IsNullOrEmpty(www.error))
  3168. {
  3169. Debug.Log(write_path);
  3170. System.IO.File.WriteAllBytes(write_path, www.bytes);
  3171. Load("file://" + write_path);
  3172. }
  3173. else
  3174. {
  3175. Debug.Log(www.error);
  3176. }
  3177. www.Dispose();
  3178. www = null;
  3179. }
  3180. else
  3181. {
  3182. Load("file://" + write_path);
  3183. }
  3184. }
  3185. #endif
  3186. #region UnityMainThread adapter
  3187. private List<Action> unityMainThreadActionList = new List<Action>();
  3188. private bool checkNewActions = false;
  3189. private object thisLock = new object();
  3190. /// <summary>
  3191. /// Execute UnityMainThread operations from unityMainThreadActionList List
  3192. /// </summary>
  3193. void CheckThreading()
  3194. {
  3195. lock (thisLock)
  3196. {
  3197. if (unityMainThreadActionList.Count > 0)
  3198. {
  3199. foreach(var a in unityMainThreadActionList)
  3200. {
  3201. a();
  3202. }
  3203. unityMainThreadActionList.Clear ();
  3204. }
  3205. }
  3206. }
  3207. /// <summary>
  3208. /// Add a sequence of Code enclosed into the Action (a). It triggers checkNewActions to execute code into the Unity Thread Update
  3209. /// </summary>
  3210. /// <param name="a">The alpha component.</param>
  3211. void AddActionForUnityMainThread(Action a)
  3212. {
  3213. lock (thisLock)
  3214. {
  3215. unityMainThreadActionList.Add(a);
  3216. }
  3217. checkNewActions = true;
  3218. }
  3219. #endregion
  3220. }