123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695 |
- using UnityEngine;
- using System.Runtime.InteropServices;
- using System;
- using System.IO;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine.UI;
- #if !UNITY_WEBPLAYER && !UNITY_WEBGL && !UNITY_WP8 && !UNITY_WP8_1
- using FFmpeg.AutoGen;
- using System.Threading;
- #endif
- #if UNITY_EDITOR || UNITY_STANDALONE
- public unsafe class MediaPlayerCtrl : MonoBehaviour {
- #else
- public class MediaPlayerCtrl : MonoBehaviour
- {
- #endif
- const int BUFFER_SIZE = 256;
- public string m_strFileName;
- public GameObject[] m_TargetMaterial = null;
- private Texture2D m_VideoTexture = null;
- private Texture2D m_VideoTextureDummy = null;
- private Texture2D m_VideoTextureY;
- private Texture2D m_VideoTextureU;
- private Texture2D m_VideoTextureV;
- private Texture2D m_VideoTextureDummyY;
- private Texture2D m_VideoTextureDummyU;
- private Texture2D m_VideoTextureDummyV;
- private MEDIAPLAYER_STATE m_CurrentState;
- private int m_iCurrentSeekPosition;
- private float m_fVolume = 1.0f;
- private int m_iWidth;
- private int m_iHeight;
- private float m_fSpeed = 1.0f;
- public bool m_bFullScreen = false;//Please use only in FullScreen prefab.
- public bool m_bSupportRockchip = true; //Using a device support Rochchip or Low-end devices
- //(Reason 1 : Not directly play in StreamingAssets)
- //(Reason 2 : Video buffer is RGB565 only supported)
- public bool m_bPC_FastMode = false;
- public delegate void VideoEnd();
- public delegate void VideoReady();
- public delegate void VideoError(MEDIAPLAYER_ERROR errorCode, MEDIAPLAYER_ERROR errorCodeExtra);
- public delegate void VideoFirstFrameReady();
- public delegate void VideoResize ();
- public VideoResize OnResize;
- public VideoReady OnReady;
- public VideoEnd OnEnd;
- public VideoError OnVideoError;
- public VideoFirstFrameReady OnVideoFirstFrameReady;
- private IntPtr m_texPtr;
- private IntPtr m_texPtrY;
- private IntPtr m_texPtrU;
- private IntPtr m_texPtrV;
- public Shader m_shaderYUV;
- private int m_iPauseFrame;
- #if UNITY_ANDROID && !UNITY_EDITOR && (UNITY_5 || UNITY_5_3_OR_NEWER)
- [DllImport ("BlueDoveMediaRender")]
- private static extern void InitNDK();
- #if UNITY_5_2 || UNITY_5_3_OR_NEWER
- [DllImport ("BlueDoveMediaRender")]
- private static extern IntPtr EasyMovieTextureRender();
- #endif
- #endif
- #if (UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN) && !UNITY_EDITOR_OSX
- [DllImport ("EasyMovieTexture")]
- private static extern int SetTexture();
- [DllImport ("EasyMovieTexture")]
- private static extern void ReleaseTexture(int iID);
- [DllImport ("EasyMovieTexture")]
- 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);
- [DllImport("EasyMovieTexture")]
- private static extern IntPtr GetRenderEventFunc();
- #endif
- #if (UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX )
- [DllImport ("EasyMovieTextureRender")]
- private static extern int SetTexture();
- [DllImport ("EasyMovieTextureRender")]
- private static extern void ReleaseTexture(int iID);
- [DllImport ("EasyMovieTextureRender")]
- 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);
- [DllImport("EasyMovieTextureRender")]
- private static extern IntPtr GetRenderEventFunc();
- #endif
- private int m_iAndroidMgrID;
- private bool m_bIsFirstFrameReady;
- public enum MEDIAPLAYER_ERROR
- {
- MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK = 200,
- MEDIA_ERROR_IO = -1004,
- MEDIA_ERROR_MALFORMED = -1007,
- MEDIA_ERROR_TIMED_OUT = -110,
- MEDIA_ERROR_UNSUPPORTED = -1010,
- MEDIA_ERROR_SERVER_DIED = 100,
- MEDIA_ERROR_UNKNOWN = 1
- }
- public enum MEDIAPLAYER_STATE
- {
- NOT_READY = 0,
- READY = 1,
- END = 2,
- PLAYING = 3,
- PAUSED = 4,
- STOPPED = 5,
- ERROR = 6
- }
- public enum MEDIA_SCALE
- {
- SCALE_X_TO_Y = 0,
- SCALE_X_TO_Z = 1,
- SCALE_Y_TO_X = 2,
- SCALE_Y_TO_Z = 3,
- SCALE_Z_TO_X = 4,
- SCALE_Z_TO_Y = 5,
- SCALE_X_TO_Y_2 = 6,
- }
- bool m_bFirst = false;
- public MEDIA_SCALE m_ScaleValue;
- public GameObject[] m_objResize = null;
- public bool m_bLoop = false;
- public bool m_bAutoPlay = true;
- private bool m_bInit = false;
- #if !UNITY_WEBPLAYER && !UNITY_WEBGL && !UNITY_WP8 && !UNITY_WP8_1
- static MediaPlayerCtrl()
- {
- #if UNITY_EDITOR
- String currentPath = Environment.GetEnvironmentVariable ("PATH", EnvironmentVariableTarget.Process);
- String dllPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Assets" + Path.DirectorySeparatorChar + "Plugins";
- if (currentPath.Contains (dllPath) == false) {
- Environment.SetEnvironmentVariable ("PATH", currentPath + Path.PathSeparator + dllPath, EnvironmentVariableTarget.Process);
- }
- dllPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Assets" + Path.DirectorySeparatorChar + "Plugins" + Path.DirectorySeparatorChar + "x64";
- if (currentPath.Contains (dllPath) == false) {
- Environment.SetEnvironmentVariable ("PATH", currentPath + Path.PathSeparator + dllPath, EnvironmentVariableTarget.Process);
- }
- dllPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "Assets" + Path.DirectorySeparatorChar + "Plugins" + Path.DirectorySeparatorChar + "x86";
- if (currentPath.Contains (dllPath) == false) {
- Environment.SetEnvironmentVariable ("PATH", currentPath + Path.PathSeparator + dllPath, EnvironmentVariableTarget.Process);
- }
- #endif
- }
- void Awake()
- {
- if(m_bInit == false)
- {
- InitPlayer();
- }
-
- }
- // Use this for initialization
- void Start()
- {
-
- }
- void InitPlayer()
- {
- #if UNITY_STANDALONE
- String currentPath = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process);
- String dllPath = Application.dataPath + Path.DirectorySeparatorChar + "Plugins";
- if(currentPath.Contains(dllPath) == false)
- {
- Environment.SetEnvironmentVariable("PATH", currentPath + Path.PathSeparator + dllPath, EnvironmentVariableTarget.Process);
- }
- #endif
- if (SystemInfo.deviceModel.Contains("rockchip"))
- {
- m_bSupportRockchip = true;
- }
- else
- {
- m_bSupportRockchip = false;
- }
- #if UNITY_IPHONE || UNITY_TVOS || UNITY_EDITOR || UNITY_STANDALONE
- if (m_TargetMaterial != null)
- {
- for (int iIndex = 0; iIndex < m_TargetMaterial.Length; iIndex++)
- {
- if (m_TargetMaterial[iIndex] != null)
- {
- if (m_TargetMaterial[iIndex].GetComponent<MeshFilter>() != null)
- {
- Vector2[] vec2UVs = m_TargetMaterial[iIndex].GetComponent<MeshFilter>().mesh.uv;
- for (int i = 0; i < vec2UVs.Length; i++)
- {
- vec2UVs[i] = new Vector2(vec2UVs[i].x, 1.0f - vec2UVs[i].y);
- }
- m_TargetMaterial[iIndex].GetComponent<MeshFilter>().mesh.uv = vec2UVs;
- }
- if (m_TargetMaterial[iIndex].GetComponent<RawImage>() != null)
- {
- m_TargetMaterial[iIndex].GetComponent<RawImage>().uvRect = new Rect(0, 1, 1, -1);
- }
- }
- }
- }
- #endif
- #if UNITY_STANDALONE || UNITY_EDITOR
- //RegisterDebugCallback(new DebugCallback(DebugMethod));
- //threadVideo = new Thread(ThreadUpdate);
- //threadVideo.Start();
- if (m_queuePacketVideo == null)
- m_queuePacketVideo = new Queue<IntPtr>();
- if (m_queuePacketSound == null)
- m_queuePacketSound = new Queue<IntPtr>();
- #endif
- #if UNITY_ANDROID && !UNITY_EDITOR
- #if UNITY_5 || UNITY_5_3_OR_NEWER
- if( SystemInfo.graphicsMultiThreaded == true)
- InitNDK();
- #endif
- m_iAndroidMgrID = Call_InitNDK();
- #endif
- Call_SetUnityActivity();
- #if UNITY_ANDROID
- if (Application.dataPath.Contains(".obb"))
- {
- Call_SetSplitOBB(true, Application.dataPath);
- }
- else
- {
- Call_SetSplitOBB(false, null);
- }
- #endif
- m_bInit = true;
- }
- void OnApplicationQuit()
- {
- //if (System.IO.Directory.Exists(Application.persistentDataPath + "/Data") == true)
- // System.IO.Directory.Delete(Application.persistentDataPath + "/Data", true);
- }
- bool m_bCheckFBO = false;
- void OnDisable()
- {
- if (GetCurrentState() == MEDIAPLAYER_STATE.PLAYING)
- {
- Pause();
- }
- }
- void OnEnable()
- {
- if (GetCurrentState() == MEDIAPLAYER_STATE.PAUSED)
- {
- Play();
- }
- }
- void Update()
- {
-
- if (string.IsNullOrEmpty(m_strFileName))
- {
- return;
- }
- if (checkNewActions)
- {
- checkNewActions = false;
- CheckThreading ();
- }
- if(m_bAutoPlay == true)
- {
- if (m_bFirst == false)
- {
- string strName = m_strFileName.Trim();
- #if UNITY_IPHONE || UNITY_TVOS
- /*if (strName.StartsWith("http",StringComparison.OrdinalIgnoreCase))
- {
- StartCoroutine( DownloadStreamingVideoAndLoad(strName) );
- }
- else*/
- {
- Call_Load(strName,0);
- }
- #elif UNITY_ANDROID
- if (m_bSupportRockchip)
- {
- Call_SetRockchip(m_bSupportRockchip);
- if (strName.Contains("://"))
- {
- Call_Load(strName, 0);
- }
- else
- {
- //Call_Load(strName,0);
- StartCoroutine(CopyStreamingAssetVideoAndLoad(strName));
- }
- }
- else
- {
- Call_Load(strName, 0);
- }
- #elif UNITY_STANDALONE
- Call_Load(strName,0);
- #endif
- Call_SetLooping(m_bLoop);
- m_bFirst = true;
- }
- }
- else
- {
- m_bFirst = true;
- }
-
- if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED)
- {
- if (m_bCheckFBO == false) {
- if (Call_GetVideoWidth () <= 0 || Call_GetVideoHeight () <= 0) {
- return;
- }
- m_iWidth = Call_GetVideoWidth ();
- m_iHeight = Call_GetVideoHeight ();
- Resize ();
- if (m_VideoTexture != null) {
- //Destroy(m_VideoTexture);
- if (m_VideoTextureDummy != null) {
- Destroy (m_VideoTextureDummy);
- m_VideoTextureDummy = null;
- }
-
- m_VideoTextureDummy = m_VideoTexture;
- m_VideoTexture = null;
- }
- if (m_VideoTextureY != null) {
- if (m_VideoTextureDummyY != null) {
- Destroy (m_VideoTextureDummyY);
- m_VideoTextureDummyY = null;
- }
- m_VideoTextureDummyY = m_VideoTextureY;
- m_VideoTextureY = null;
- }
- if (m_VideoTextureU != null) {
- if (m_VideoTextureDummyU != null) {
- Destroy (m_VideoTextureDummyU);
- m_VideoTextureDummyU = null;
- }
- m_VideoTextureDummyU = m_VideoTextureU;
- m_VideoTextureU = null;
- }
- if (m_VideoTextureV != null) {
- if (m_VideoTextureDummyV != null) {
- Destroy (m_VideoTextureDummyV);
- m_VideoTextureDummyV = null;
- }
- m_VideoTextureDummyV = m_VideoTextureV;
- m_VideoTextureV = null;
- }
- #if UNITY_ANDROID || UNITY_EDITOR || UNITY_STANDALONE
- #if UNITY_EDITOR || UNITY_STANDALONE
- if(m_bPC_FastMode == true)
- {
- m_VideoTextureY = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.Alpha8, false);
- m_VideoTextureU = new Texture2D (Call_GetVideoWidth () / 2, Call_GetVideoHeight () / 2, TextureFormat.Alpha8, false);
- m_VideoTextureV = new Texture2D (Call_GetVideoWidth () / 2, Call_GetVideoHeight () / 2, TextureFormat.Alpha8, false);
- m_VideoTextureY.filterMode = FilterMode.Bilinear;
- m_VideoTextureY.wrapMode = TextureWrapMode.Clamp;
- m_VideoTextureU.filterMode = FilterMode.Bilinear;
- m_VideoTextureU.wrapMode = TextureWrapMode.Clamp;
- m_VideoTextureV.filterMode = FilterMode.Bilinear;
- m_VideoTextureV.wrapMode = TextureWrapMode.Clamp;
-
- }
- else
- {
- if (m_bSupportRockchip) {
- m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGB565, false);
- } else {
- m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGBA32, false);
- }
- m_VideoTexture.filterMode = FilterMode.Bilinear;
- m_VideoTexture.wrapMode = TextureWrapMode.Clamp;
- }
- if(m_bPC_FastMode == true)
- {
- m_texPtrY = m_VideoTextureY.GetNativeTexturePtr ();
- m_texPtrU = m_VideoTextureU.GetNativeTexturePtr ();
- m_texPtrV = m_VideoTextureV.GetNativeTexturePtr ();
- }
- else
- {
- m_texPtr = m_VideoTexture.GetNativeTexturePtr ();
- }
- #else
- if (m_bSupportRockchip) {
- m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGB565, false);
-
- } else {
- m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGBA32, false);
- }
- m_VideoTexture.filterMode = FilterMode.Bilinear;
- m_VideoTexture.wrapMode = TextureWrapMode.Clamp;
- m_texPtr = m_VideoTexture.GetNativeTexturePtr ();
- #endif
- #if UNITY_5_2 || UNITY_5_3_OR_NEWER
- Call_SetUnityTexture((int)m_texPtr);
- #else
- Call_SetUnityTexture (m_VideoTexture.GetNativeTextureID ());
- #endif
- #endif
- Call_SetWindowSize ();
- m_bCheckFBO = true;
- if (OnResize != null)
- OnResize ();
- } else {
- if (Call_GetVideoWidth () != m_iWidth || Call_GetVideoHeight () != m_iHeight) {
- m_iWidth = Call_GetVideoWidth ();
- m_iHeight = Call_GetVideoHeight ();
- if (OnResize != null)
- OnResize ();
- ResizeTexture ();
- }
- }
- Call_UpdateVideoTexture();
- m_iCurrentSeekPosition = Call_GetSeekPosition();
- }
- if (m_CurrentState != Call_GetStatus())
- {
- m_CurrentState = Call_GetStatus();
- if (m_CurrentState == MEDIAPLAYER_STATE.READY)
- {
- if(m_bPause)
- {
- m_bPause = false;
- Call_SetSeekPosition(m_iPauseFrame);
- }
- else if(m_bPlaying)
- {
- m_bPlaying = false;
- Call_SetSeekPosition(m_iPauseFrame);
- Call_Play(m_iPauseFrame);
- //StartCoroutine(DelayPlay());
- }
- else
- {
-
- if (OnReady != null)
- OnReady();
- if (m_bAutoPlay)
- Call_Play(0);
- if (m_bReadyPlay)
- {
- Call_Play(0);
- m_bReadyPlay = false;
- }
- SetVolume(m_fVolume);
- }
-
- }
- else if (m_CurrentState == MEDIAPLAYER_STATE.END)
- {
- if (OnEnd != null)
- OnEnd();
- if (m_bLoop == true)
- {
- Call_Play(0);
- }
- }
- else if (m_CurrentState == MEDIAPLAYER_STATE.ERROR)
- {
- OnError((MEDIAPLAYER_ERROR)Call_GetError(), (MEDIAPLAYER_ERROR)Call_GetErrorExtra());
- }
- }
- GL.InvalidateState ();
- }
- public void DeleteVideoTexture()
- {
- if (m_VideoTextureDummy != null)
- {
- Destroy(m_VideoTextureDummy);
- m_VideoTextureDummy = null;
- }
- if (m_VideoTexture != null)
- {
- Destroy(m_VideoTexture);
- m_VideoTexture = null;
- }
- if (m_VideoTextureDummyY != null)
- {
- Destroy(m_VideoTextureDummyY);
- m_VideoTextureDummyY = null;
- }
- if (m_VideoTextureY != null)
- {
- Destroy(m_VideoTextureY);
- m_VideoTextureY = null;
- }
- if (m_VideoTextureDummyU != null)
- {
- Destroy(m_VideoTextureDummyU);
- m_VideoTextureDummyU = null;
- }
- if (m_VideoTextureU != null)
- {
- Destroy(m_VideoTextureU);
- m_VideoTextureU = null;
- }
- if (m_VideoTextureDummyV != null)
- {
- Destroy(m_VideoTextureDummyV);
- m_VideoTextureDummyV = null;
- }
- if (m_VideoTextureV != null)
- {
- Destroy(m_VideoTextureV);
- m_VideoTextureV = null;
- }
- }
- public void ResizeTexture()
- {
- Debug.Log ("ResizeTexture " + m_iWidth + " " + m_iHeight);
- if (m_iWidth == 0 || m_iHeight == 0)
- return;
- if (m_VideoTexture != null) {
- //Destroy(m_VideoTexture);
- if (m_VideoTextureDummy != null) {
- Destroy (m_VideoTextureDummy);
- m_VideoTextureDummy = null;
- }
- m_VideoTextureDummy = m_VideoTexture;
- m_VideoTexture = null;
- }
- if (m_VideoTextureY != null) {
- if (m_VideoTextureDummyY != null) {
- Destroy (m_VideoTextureDummyY);
- m_VideoTextureDummyY = null;
- }
- m_VideoTextureDummyY = m_VideoTextureY;
- m_VideoTextureY = null;
- }
- if (m_VideoTextureU != null) {
- if (m_VideoTextureDummyU != null) {
- Destroy (m_VideoTextureDummyU);
- m_VideoTextureDummyU = null;
- }
- m_VideoTextureDummyU = m_VideoTextureU;
- m_VideoTextureU = null;
- }
- if (m_VideoTextureV != null) {
- if (m_VideoTextureDummyV != null) {
- Destroy (m_VideoTextureDummyV);
- m_VideoTextureDummyV = null;
- }
- m_VideoTextureDummyV = m_VideoTextureV;
- m_VideoTextureV = null;
- }
- #if UNITY_IPHONE && !UNITY_EDITOR
- _videoTexture =null;
- bFirstIOS = true;
- #endif
- #if UNITY_ANDROID || UNITY_EDITOR || UNITY_STANDALONE
- #if UNITY_EDITOR || UNITY_STANDALONE
-
-
-
- if (m_bPC_FastMode == true)
- {
- m_VideoTextureY = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.Alpha8, false);
- m_VideoTextureU = new Texture2D (Call_GetVideoWidth () / 2, Call_GetVideoHeight () / 2, TextureFormat.Alpha8, false);
- m_VideoTextureV = new Texture2D (Call_GetVideoWidth () / 2, Call_GetVideoHeight () / 2, TextureFormat.Alpha8, false);
- }
- else
- {
- if (m_bSupportRockchip) {
- m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGB565, false);
- } else {
- m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGBA32, false);
- }
- }
- if( m_VideoTexture != null)
- {
- m_VideoTexture.filterMode = FilterMode.Bilinear;
- m_VideoTexture.wrapMode = TextureWrapMode.Clamp;
- }
-
- if(m_bPC_FastMode == true)
- {
- m_texPtrY = m_VideoTextureY.GetNativeTexturePtr ();
- m_texPtrU = m_VideoTextureU.GetNativeTexturePtr ();
- m_texPtrV = m_VideoTextureV.GetNativeTexturePtr ();
- }
- else
- {
- m_texPtr = m_VideoTexture.GetNativeTexturePtr ();
- }
- #else
- if (m_bSupportRockchip) {
- m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGB565, false);
-
- } else {
- m_VideoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGBA32, false);
- }
- m_VideoTexture.filterMode = FilterMode.Bilinear;
- m_VideoTexture.wrapMode = TextureWrapMode.Clamp;
- m_texPtr = m_VideoTexture.GetNativeTexturePtr ();
- #endif
- #if UNITY_5_2 || UNITY_5_3_OR_NEWER
- Call_SetUnityTexture((int)m_texPtr);
- #else
- Call_SetUnityTexture (m_VideoTexture.GetNativeTextureID ());
- #endif
- #endif
- Call_SetWindowSize ();
- }
- public void Resize()
- {
- if (m_CurrentState != MEDIAPLAYER_STATE.PLAYING)
- return;
- if (Call_GetVideoWidth() <= 0 || Call_GetVideoHeight() <= 0)
- {
- return;
- }
- if (m_objResize != null)
- {
- int iScreenWidth = Screen.width;
- int iScreenHeight = Screen.height;
- float fRatioScreen = (float)iScreenHeight / (float)iScreenWidth;
- int iWidth = Call_GetVideoWidth();
- int iHeight = Call_GetVideoHeight();
- float fRatio = (float)iHeight / (float)iWidth;
- float fRatioResult = fRatioScreen / fRatio;
- for (int i = 0; i < m_objResize.Length; i++)
- {
- if (m_objResize[i] == null)
- continue;
- if (m_bFullScreen)
- {
- m_objResize[i].transform.localScale = new Vector3(20.0f / fRatioScreen, 20.0f / fRatioScreen, 1.0f);
- if (fRatio < 1.0f)
- {
- if (fRatioScreen < 1.0f)
- {
- if (fRatio > fRatioScreen)
- {
- m_objResize[i].transform.localScale *= fRatioResult;
- }
- }
- m_ScaleValue = MEDIA_SCALE.SCALE_X_TO_Y;
- }
- else
- {
- if (fRatioScreen > 1.0f) {
- if (fRatio >= fRatioScreen) {
- m_objResize [i].transform.localScale *= fRatioResult;
- }
- } else {
- m_objResize [i].transform.localScale *= fRatioResult;
- }
- m_ScaleValue = MEDIA_SCALE.SCALE_X_TO_Y;
- }
- }
- if (m_ScaleValue == MEDIA_SCALE.SCALE_X_TO_Y)
- {
- m_objResize[i].transform.localScale
- = new Vector3(m_objResize[i].transform.localScale.x
- , m_objResize[i].transform.localScale.x * fRatio
- , m_objResize[i].transform.localScale.z);
- }
- else if (m_ScaleValue == MEDIA_SCALE.SCALE_X_TO_Y_2)
- {
- m_objResize[i].transform.localScale
- = new Vector3(m_objResize[i].transform.localScale.x
- , m_objResize[i].transform.localScale.x * fRatio / 2.0f
- , m_objResize[i].transform.localScale.z);
- }
- else if (m_ScaleValue == MEDIA_SCALE.SCALE_X_TO_Z)
- {
- m_objResize[i].transform.localScale
- = new Vector3(m_objResize[i].transform.localScale.x
- , m_objResize[i].transform.localScale.y
- , m_objResize[i].transform.localScale.x * fRatio);
- }
- else if (m_ScaleValue == MEDIA_SCALE.SCALE_Y_TO_X)
- {
- m_objResize[i].transform.localScale
- = new Vector3(m_objResize[i].transform.localScale.y / fRatio
- , m_objResize[i].transform.localScale.y
- , m_objResize[i].transform.localScale.z);
- }
- else if (m_ScaleValue == MEDIA_SCALE.SCALE_Y_TO_Z)
- {
- m_objResize[i].transform.localScale
- = new Vector3(m_objResize[i].transform.localScale.x
- , m_objResize[i].transform.localScale.y
- , m_objResize[i].transform.localScale.y / fRatio);
- }
- else if (m_ScaleValue == MEDIA_SCALE.SCALE_Z_TO_X)
- {
- m_objResize[i].transform.localScale
- = new Vector3(m_objResize[i].transform.localScale.z * fRatio
- , m_objResize[i].transform.localScale.y
- , m_objResize[i].transform.localScale.z);
- }
- else if (m_ScaleValue == MEDIA_SCALE.SCALE_Z_TO_Y)
- {
- m_objResize[i].transform.localScale
- = new Vector3(m_objResize[i].transform.localScale.x
- , m_objResize[i].transform.localScale.z * fRatio
- , m_objResize[i].transform.localScale.z);
- }
- else
- {
- m_objResize[i].transform.localScale
- = new Vector3(m_objResize[i].transform.localScale.x, m_objResize[i].transform.localScale.y, m_objResize[i].transform.localScale.z);
- }
- }
- }
- }
- //The error code is the following sites related documents.
- //http://developer.android.com/reference/android/media/MediaPlayer.OnErrorListener.html
- void OnError(MEDIAPLAYER_ERROR iCode, MEDIAPLAYER_ERROR iCodeExtra)
- {
- string strError = "";
- switch (iCode)
- {
- case MEDIAPLAYER_ERROR.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
- strError = "MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK";
- break;
- case MEDIAPLAYER_ERROR.MEDIA_ERROR_SERVER_DIED:
- strError = "MEDIA_ERROR_SERVER_DIED";
- break;
- case MEDIAPLAYER_ERROR.MEDIA_ERROR_UNKNOWN:
- strError = "MEDIA_ERROR_UNKNOWN";
- break;
- default:
- strError = "Unknown error " + iCode;
- break;
- }
- strError += " ";
- switch (iCodeExtra)
- {
- case MEDIAPLAYER_ERROR.MEDIA_ERROR_IO:
- strError += "MEDIA_ERROR_IO";
- break;
- case MEDIAPLAYER_ERROR.MEDIA_ERROR_MALFORMED:
- strError += "MEDIA_ERROR_MALFORMED";
- break;
- case MEDIAPLAYER_ERROR.MEDIA_ERROR_TIMED_OUT:
- strError += "MEDIA_ERROR_TIMED_OUT";
- break;
- case MEDIAPLAYER_ERROR.MEDIA_ERROR_UNSUPPORTED:
- strError += "MEDIA_ERROR_UNSUPPORTED";
- break;
- default:
- strError = "Unknown error " + iCode;
- break;
- }
- Debug.LogError(strError);
- if (OnVideoError != null)
- {
- OnVideoError(iCode, iCodeExtra);
- }
- }
- void OnDestroy()
- {
- #if UNITY_ANDROID
- //Call_Reset();
- #endif
- Call_UnLoad();
- DeleteVideoTexture ();
-
- Call_Destroy();
- }
- bool m_bPause = false;
- bool m_bPlaying = false;
- void OnApplicationPause(bool bPause)
- {
- Debug.Log("ApplicationPause : " + bPause);
- if(m_bPlaying == false && m_bPause == false)
- if (m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.NOT_READY)
- return;
- if (bPause == true)
- {
- if (m_CurrentState == MEDIAPLAYER_STATE.PAUSED)
- {
- m_bPause = true;
- }
- #if (UNITY_IPHONE || UNITY_TVOS) && !UNITY_EDITOR
- if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING)
- {
- m_iPauseFrame = m_iCurrentSeekPosition;
- Stop();
- //Pause();
- }
- else
- {
- m_iPauseFrame = 0;
- }
-
- #elif UNITY_ANDROID && !UNITY_EDITOR
- m_bPlaying = true;
- m_iPauseFrame = m_iCurrentSeekPosition;
- UnLoad();
- #else
- Call_Pause();
- #endif
- }
- else
- {
- #if (UNITY_IPHONE || UNITY_TVOS) && !UNITY_EDITOR
- if (m_iPauseFrame != 0)
- {
-
- Call_Play(m_iPauseFrame);
- //Call_Play();
- //Call_RePlay();
- if (m_bPause == true)
- {
- Call_Pause();
- m_bPause = false;
- }
- }
- #elif UNITY_ANDROID && !UNITY_EDITOR
- Load(m_strFileName);
- #else
- Call_RePlay();
- #endif
- }
- }
- public MEDIAPLAYER_STATE GetCurrentState()
- {
- return m_CurrentState;
- }
- public Texture2D GetVideoTexture()
- {
- return m_VideoTexture;
- }
- bool m_bReadyPlay = false;
- public void Play()
- {
-
- if (m_CurrentState == MEDIAPLAYER_STATE.PAUSED)
- {
-
- Call_RePlay ();
- }else if ( m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
- {
- Load(m_strFileName);
- }
- else if (m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.STOPPED || m_CurrentState == MEDIAPLAYER_STATE.END)
- {
-
- Call_Play (0);
- }
- else if (m_CurrentState == MEDIAPLAYER_STATE.NOT_READY)
- {
- if (string.IsNullOrEmpty(m_strFileName) == false)
- {
-
- Load(m_strFileName);
- }
- m_bReadyPlay = true;
- }
-
- }
- public void Stop()
- {
- if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING)
- Call_Pause();
- #if UNITY_STANDALONE || UNITY_EDITOR
- UnLoad();
- #else
- SeekTo(0);
- #endif
- m_CurrentState = MEDIAPLAYER_STATE.STOPPED;
- m_iCurrentSeekPosition = 0;
- }
- public void Pause()
- {
- if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING)
- {
- Call_Pause();
- m_CurrentState = MEDIAPLAYER_STATE.PAUSED;
- }
-
- }
- public void Load(string strFileName)
- {
- if (m_bInit == false)
- {
- InitPlayer();
- }
- if (GetCurrentState() != MEDIAPLAYER_STATE.NOT_READY)
- UnLoad();
-
- //m_bReadyPlay = false;
- m_bIsFirstFrameReady = false;
- //m_bFirst = false;
- m_bCheckFBO = false;
- m_strFileName = strFileName;
- if (m_bInit == false)
- return;
- Call_Load(strFileName,0);
- m_CurrentState = MEDIAPLAYER_STATE.NOT_READY;
- }
- public void SetVolume(float fVolume)
- {
- 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)
- {
- m_fVolume = fVolume;
- Call_SetVolume(fVolume);
- }
- }
- //return milisecond
- public int GetSeekPosition()
- {
- if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.END)
- return m_iCurrentSeekPosition;
- else
- return 0;
- }
- public void SeekTo(int iSeek)
- {
- 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)
- Call_SetSeekPosition(iSeek);
- }
- /// <summary>
- /// Sets the speed.
- /// Experimental API( PC&iOS support, support from Android version 6.0 or later)
- /// </summary>
- /// <param name="fSpeed">video playback speed.</param>
- public void SetSpeed(float fSpeed)
- {
- 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) {
- m_fSpeed = fSpeed;
- Call_SetSpeed (fSpeed);
- }
- }
- //Gets the duration of the file.
- //Returns
- //the duration in milliseconds, if no duration is available (for example, if streaming live content), -1 is returned.
- public int GetDuration()
- {
- 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)
- return Call_GetDuration();
- else
- return 0;
- }
- public float GetSeekBarValue()
- {
- 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)
- {
- if (GetDuration () == 0) {
- return 0;
- }
- return (float)GetSeekPosition() / (float)GetDuration() ;
- }
- else
- return 0;
- }
- public void SetSeekBarValue(float fValue)
- {
- 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)
- {
- if (GetDuration () == 0) {
- return;
- }
- SeekTo( (int)((float)GetDuration() * fValue) );
- }
- else
- return;
- }
-
- //Get update status in buffering a media stream received through progressive HTTP download.
- //The received buffering percentage indicates how much of the content has been buffered or played.
- //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.
- //the percentage (0-100) of the content that has been buffered or played thus far
- public int GetCurrentSeekPercent()
- {
- if (m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.END || m_CurrentState == MEDIAPLAYER_STATE.READY)
- return Call_GetCurrentSeekPercent();
- else
- return 0;
- }
- public int GetVideoWidth()
- {
- return Call_GetVideoWidth();
- }
- public int GetVideoHeight()
- {
- return Call_GetVideoHeight();
- }
- public void UnLoad()
- {
- m_bCheckFBO = false;
- #if UNITY_ANDROID
- //Call_Reset();
- #endif
- Call_UnLoad();
- m_CurrentState = MEDIAPLAYER_STATE.NOT_READY;
- }
- public void SelectSoundTrack(int iIndex)
- {
- Call_SelectSoundTrack(iIndex);
- }
- public int[] GetSoundTrack()
- {
- return Call_GetSoundTrack();
- }
- #if !UNITY_EDITOR && !UNITY_STANDALONE && !UNITY_WEBGL
- #if UNITY_ANDROID
- private AndroidJavaObject javaObj = null;
- private AndroidJavaObject GetJavaObject()
- {
- if (javaObj == null)
- {
- javaObj = new AndroidJavaObject("com.EasyMovieTexture.EasyMovieTexture");
- }
- return javaObj;
- }
- private void Call_Destroy()
- {
- #if UNITY_5 || UNITY_5_3_OR_NEWER
- if( SystemInfo.graphicsMultiThreaded == true)
- {
- #if UNITY_5_2 || UNITY_5_3_OR_NEWER
- GL.IssuePluginEvent(EasyMovieTextureRender(), 5 + m_iAndroidMgrID * 10 + 7000);
- #else
- GL.IssuePluginEvent(5 + m_iAndroidMgrID * 10 + 7000);
- #endif
- }
- else
- {
- GetJavaObject().Call("Destroy");
- }
- #else
- GetJavaObject().Call("Destroy");
- #endif
- }
- private void Call_UnLoad()
- {
- #if UNITY_5 || UNITY_5_3_OR_NEWER
- if( SystemInfo.graphicsMultiThreaded == true)
- {
- #if UNITY_5_2 || UNITY_5_3_OR_NEWER
- GL.IssuePluginEvent(EasyMovieTextureRender(), 4 + m_iAndroidMgrID * 10 + 7000);
- #else
- GL.IssuePluginEvent(4 + m_iAndroidMgrID * 10 + 7000);
- #endif
- }
- else
- {
- GetJavaObject().Call("UnLoad");
- }
- #else
- GetJavaObject().Call("UnLoad");
- #endif
- }
- private bool Call_Load(string strFileName, int iSeek)
- {
- #if UNITY_5 || UNITY_5_3_OR_NEWER
- if( SystemInfo.graphicsMultiThreaded == true)
- {
- GetJavaObject().Call("NDK_SetFileName", strFileName);
- #if UNITY_5_2 || UNITY_5_3_OR_NEWER
- GL.IssuePluginEvent(EasyMovieTextureRender(), 1 + m_iAndroidMgrID * 10 + 7000);
- #else
- GL.IssuePluginEvent(1+ m_iAndroidMgrID * 10 + 7000);
- #endif
- Call_SetNotReady();
- return true;
- }
- else
- {
- GetJavaObject().Call("NDK_SetFileName", strFileName);
- if (GetJavaObject().Call<bool>("Load"))
- {
- return true;
- }
- else
- {
- OnError(MEDIAPLAYER_ERROR.MEDIA_ERROR_UNKNOWN, MEDIAPLAYER_ERROR.MEDIA_ERROR_UNKNOWN);
- return false;
- }
- }
- #else
- GetJavaObject().Call("NDK_SetFileName", strFileName);
- if (GetJavaObject().Call<bool>("Load"))
- {
- return true;
- }
- else
- {
- OnError(MEDIAPLAYER_ERROR.MEDIA_ERROR_UNKNOWN, MEDIAPLAYER_ERROR.MEDIA_ERROR_UNKNOWN);
- return false;
- }
- #endif
- }
- private void Call_UpdateVideoTexture()
- {
- if( Call_IsUpdateFrame() == false)
- return;
- if(m_VideoTextureDummy != null)
- {
- Destroy(m_VideoTextureDummy);
- m_VideoTextureDummy = null;
- }
- for( int i = 0; i < m_TargetMaterial.Length; i++)
- {
- if(m_TargetMaterial[i])
- {
- if(m_TargetMaterial[i].GetComponent<MeshRenderer>()!= null)
- {
- if(m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTexture != m_VideoTexture)
- {
- m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTexture = m_VideoTexture;
- }
- }
- if(m_TargetMaterial[i].GetComponent<RawImage>()!= null)
- {
- if(m_TargetMaterial[i].GetComponent<RawImage>().texture != m_VideoTexture)
- {
- m_TargetMaterial[i].GetComponent<RawImage>().texture = m_VideoTexture;
- }
- }
- }
- }
- #if UNITY_5 || UNITY_5_3_OR_NEWER
- if( SystemInfo.graphicsMultiThreaded == true)
- {
- #if UNITY_5_2 || UNITY_5_3_OR_NEWER
- GL.IssuePluginEvent(EasyMovieTextureRender(), 3 + m_iAndroidMgrID * 10 + 7000);
- #else
- GL.IssuePluginEvent(3+ m_iAndroidMgrID * 10 + 7000);
- #endif
- }
- else
- {
- GetJavaObject().Call("UpdateVideoTexture");
- }
- #else
- GetJavaObject().Call("UpdateVideoTexture");
- #endif
- if (!m_bIsFirstFrameReady)
- {
- m_bIsFirstFrameReady = true;
- if (OnVideoFirstFrameReady != null)
- {
- OnVideoFirstFrameReady();
- //OnVideoFirstFrameReady = null;
- }
- }
- }
- private void Call_SetVolume(float fVolume)
- {
- GetJavaObject().Call("SetVolume",fVolume);
- }
- private void Call_SetSeekPosition(int iSeek)
- {
- GetJavaObject().Call("SetSeekPosition",iSeek);
- }
- private int Call_GetSeekPosition()
- {
- return GetJavaObject().Call<int>("GetSeekPosition");
- }
- private void Call_Play(int iSeek)
- {
- GetJavaObject().Call("Play",iSeek);
- }
- private void Call_Reset()
- {
- GetJavaObject().Call("Reset");
- }
- private void Call_Stop()
- {
- GetJavaObject().Call("Stop");
- }
- private void Call_RePlay()
- {
- GetJavaObject().Call("RePlay");
- }
- private void Call_Pause()
- {
- GetJavaObject().Call("Pause");
- }
- private int Call_InitNDK()
- {
- return GetJavaObject().Call<int>("InitNative",GetJavaObject());
- }
- private int Call_GetVideoWidth()
- {
- return GetJavaObject().Call<int>("GetVideoWidth");
- }
- private int Call_GetVideoHeight()
- {
- return GetJavaObject().Call<int>("GetVideoHeight");
- }
- private bool Call_IsUpdateFrame()
- {
- return GetJavaObject().Call<bool>("IsUpdateFrame");
- }
- private void Call_SetUnityTexture(int iTextureID)
- {
- GetJavaObject().Call("SetUnityTexture",iTextureID);
- }
- private void Call_SetWindowSize()
- {
- #if UNITY_5 || UNITY_5_3_OR_NEWER
- if( SystemInfo.graphicsMultiThreaded == true)
- {
- #if UNITY_5_2 || UNITY_5_3_OR_NEWER
- GL.IssuePluginEvent(EasyMovieTextureRender(), 2 + m_iAndroidMgrID * 10 + 7000);
- #else
- GL.IssuePluginEvent(2+ m_iAndroidMgrID * 10 + 7000);
- #endif
- }
- else
- {
- GetJavaObject().Call("SetWindowSize");
- }
- #else
- GetJavaObject().Call("SetWindowSize");
- #endif
- }
- private void Call_SetLooping(bool bLoop)
- {
- GetJavaObject().Call("SetLooping",bLoop);
- }
- private void Call_SetRockchip(bool bValue)
- {
- GetJavaObject().Call("SetRockchip",bValue);
- }
- private int Call_GetDuration()
- {
- return GetJavaObject().Call<int>("GetDuration");
- }
- private int Call_GetCurrentSeekPercent()
- {
- return GetJavaObject().Call<int>("GetCurrentSeekPercent");
- }
- private int Call_GetError()
- {
- return GetJavaObject().Call<int>("GetError");
- }
- private void Call_SetSplitOBB(bool bValue, string strOBBName)
- {
- GetJavaObject().Call("SetSplitOBB",bValue,strOBBName);
- }
- private int Call_GetErrorExtra()
- {
- return GetJavaObject().Call<int>("GetErrorExtra");
- }
- private void Call_SetUnityActivity()
- {
- AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
- AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
- GetJavaObject().Call("SetUnityActivity", jo);
- #if UNITY_5 || UNITY_5_3_OR_NEWER
- if( SystemInfo.graphicsMultiThreaded == true)
- {
- #if UNITY_5_2 || UNITY_5_3_OR_NEWER
- GL.IssuePluginEvent(EasyMovieTextureRender(), 0 + m_iAndroidMgrID * 10 + 7000);
- #else
- GL.IssuePluginEvent(0+ m_iAndroidMgrID * 10 + 7000);
- #endif
- }
- else
- {
- Call_InitJniManager();
- }
- #else
- Call_InitJniManager();
- #endif
- }
- private void Call_SetNotReady()
- {
- GetJavaObject().Call("SetNotReady");
- }
- private void Call_InitJniManager()
- {
- GetJavaObject().Call("InitJniManager");
- }
- private void Call_SetSpeed(float fSpeed)
- {
- using (AndroidJavaClass buildVersion = new AndroidJavaClass("android.os.Build$VERSION"))
- {
- int sdkVersion = buildVersion.GetStatic<int>("SDK_INT");
- if(sdkVersion >= 23 ) //Android 6.0
- {
- GetJavaObject().Call("SetSpeed",fSpeed);
- }
- }
- }
- private MEDIAPLAYER_STATE Call_GetStatus()
- {
- return (MEDIAPLAYER_STATE)GetJavaObject().Call<int>("GetStatus");
- }
- private void Call_SelectSoundTrack(int iIndex)
- {
- GetJavaObject().Call("SelectTrack",iIndex);
- }
- private int[] Call_GetSoundTrack()
- {
- return GetJavaObject().Call<int []>("GetSoundTrack");
- }
- #elif UNITY_IPHONE || UNITY_TVOS
- [DllImport("__Internal")]
- private static extern int VideoPlayerPluginCreateInstance();
- [DllImport("__Internal")]
- private static extern void VideoPlayerPluginDestroyInstance(int iID);
- [DllImport("__Internal")]
- private static extern bool VideoPlayerPluginCanOutputToTexture(string videoURL);
- [DllImport("__Internal")]
- private static extern void VideoPlayerPluginSetLoop(int iID, bool bLoop);
- [DllImport("__Internal")]
- private static extern void VideoPlayerPluginSetVolume(int iID, float fVolume);
- [DllImport("__Internal")]
- private static extern bool VideoPlayerPluginPlayerReady(int iID);
- [DllImport("__Internal")]
- private static extern float VideoPlayerPluginDurationSeconds(int iID);
- [DllImport("__Internal")]
- private static extern void VideoPlayerPluginExtents(int iID,ref int width, ref int height);
- [DllImport("__Internal")]
- private static extern IntPtr VideoPlayerPluginCurFrameTexture(int iID);
- [DllImport("__Internal")]
- private static extern void VideoPlayerPluginLoadVideo(int iID,string videoURL);
- [DllImport("__Internal")]
- private static extern void VideoPlayerPluginPlayVideo(int iID);
- [DllImport("__Internal")]
- private static extern void VideoPlayerPluginPauseVideo(int iID);
- [DllImport("__Internal")]
- private static extern void VideoPlayerPluginResumeVideo(int iID);
- [DllImport("__Internal")]
- private static extern void VideoPlayerPluginRewindVideo(int iID);
- [DllImport("__Internal")]
- private static extern void VideoPlayerPluginSeekToVideo(int iID,float time);
- [DllImport("__Internal")]
- private static extern float VideoPlayerPluginCurTimeSeconds(int iID);
- [DllImport("__Internal")]
- private static extern bool VideoPlayerPluginIsPlaying(int iID);
- [DllImport("__Internal")]
- private static extern void VideoPlayerPluginStopVideo(int iID);
- [DllImport("__Internal")]
- private static extern bool VideoPlayerPluginFinish(int iID);
- [DllImport("__Internal")]
- private static extern bool VideoPlayerPluginError(int iID);
- [DllImport("__Internal")]
- private static extern void VideoPlayerPluginSetTexture(int iID,int iTextureID);
- [DllImport("__Internal")]
- private static extern void VideoPlayerPluginSetSpeed(int iID,float fSpeed);
- [DllImport("__Internal")]
- private static extern void VideoPlayerPluginSetAudioTrack(int iID,int trackID);
- [DllImport("__Internal")]
- private static extern int VideoPlayerPluginGetAudioTrack(int iID);
- [DllImport("__Internal")]
- private static extern int VideoPlayerPluginGetCurrentSeekPercent(int iID);
- int m_iID = -1;
- /// <summary>
- /// ???????????????true??????? /// </summary>
- public bool ready
- {
- get
- {
- return VideoPlayerPluginPlayerReady(m_iID);
- }
- }
- /// <summary>
- ///
- /// </summary>
- public float duration
- {
- get
- {
- return VideoPlayerPluginDurationSeconds(m_iID);
- }
- }
- public float currentTime
- {
- get
- {
- return VideoPlayerPluginCurTimeSeconds(m_iID);
- }
- }
- public bool isPlaying
- {
- get
- {
- return VideoPlayerPluginIsPlaying(m_iID);
- }
- }
- public Vector2 videoSize
- {
- get
- {
- int width = 0, height = 0;
- VideoPlayerPluginExtents(m_iID,ref width, ref height);
- return new Vector2(width, height);
- }
- }
- private Texture2D _videoTexture;
- public Texture2D videoTexture
- {
- get
- {
- IntPtr nativeTex = ready ? VideoPlayerPluginCurFrameTexture(m_iID) : (IntPtr)0;
- if (nativeTex != (IntPtr)0)
- {
- if (_videoTexture == null)
- {
- #if UNITY_5 || UNITY_5_3_OR_NEWER
- if(SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Metal)
- _videoTexture = new Texture2D (Call_GetVideoWidth (), Call_GetVideoHeight (), TextureFormat.RGBA32, false);
- else
- _videoTexture = Texture2D.CreateExternalTexture((int)videoSize.x, (int)videoSize.y, TextureFormat.RGBA32,
- false, false, (IntPtr)nativeTex);
- #else
- _videoTexture = Texture2D.CreateExternalTexture((int)videoSize.x, (int)videoSize.y, TextureFormat.RGBA32,
- false, false, (IntPtr)nativeTex);
- #endif
- _videoTexture.filterMode = FilterMode.Bilinear;
- _videoTexture.wrapMode = TextureWrapMode.Clamp;
- }
- _videoTexture.UpdateExternalTexture((IntPtr)nativeTex);
- }
- /* else
- {
- if(_videoTexture != null)
- {
- Destroy(_videoTexture);
- }
- _videoTexture = null;
- }*/
- return _videoTexture;
- }
- }
- private void Call_Destroy()
- {
- VideoPlayerPluginDestroyInstance(m_iID);
- if(_videoTexture != null)
- Destroy(_videoTexture);
- _videoTexture = null;
- m_iID = -1;
- }
- private void Call_UnLoad()
- {
- VideoPlayerPluginStopVideo(m_iID);
- VideoPlayerPluginDestroyInstance(m_iID);
- }
- private bool Call_Load(string strFileName, int iSeek)
- {
- if( m_iID == -1)
- {
- Call_SetUnityActivity();
- }
- bFirstIOS = true;
- /*if(_videoTexture != null)
- {
- Destroy(_videoTexture);
- }*/
- _videoTexture = null;
- if (VideoPlayerPluginCanOutputToTexture(strFileName))
- {
- VideoPlayerPluginLoadVideo(m_iID,strFileName);
- }
- return true;
- }
- bool bFirstIOS = false;
- private void Call_UpdateVideoTexture()
- {
- if( m_CurrentState == MEDIAPLAYER_STATE.PLAYING || m_CurrentState == MEDIAPLAYER_STATE.PAUSED)
- {
- if( videoTexture == null)
- return;
- if(bFirstIOS == true)
- {
- bFirstIOS = false;
- return;
- }
- if(bFirstIOS == false)
- {
- for( int i = 0; i < m_TargetMaterial.Length; i++)
- {
- if(m_TargetMaterial[i])
- {
- if(m_TargetMaterial[i].GetComponent<MeshRenderer>() != null)
- m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTexture = _videoTexture;
- }
- if(m_TargetMaterial[i])
- {
- if(m_TargetMaterial[i].GetComponent<RawImage>() != null)
- m_TargetMaterial[i].GetComponent<RawImage>().texture= _videoTexture;
- }
- }
- if(m_VideoTextureDummy != null)
- {
- Destroy(m_VideoTextureDummy);
- m_VideoTextureDummy = null;
- }
- }
- m_VideoTexture = _videoTexture;
- }
- if (!m_bIsFirstFrameReady)
- {
- m_bIsFirstFrameReady = true;
- if (OnVideoFirstFrameReady != null)
- {
- OnVideoFirstFrameReady();
- //OnVideoFirstFrameReady = null;
- }
- }
- }
- private void Call_SetVolume(float fVolume)
- {
- VideoPlayerPluginSetVolume(m_iID, fVolume);
- }
- private void Call_SetSeekPosition(int iSeek)
- {
- float fSeek = (float)iSeek / 1000.0f;
- VideoPlayerPluginSeekToVideo(m_iID,fSeek);
- }
- private int Call_GetSeekPosition()
- {
- float fSeek = VideoPlayerPluginCurTimeSeconds(m_iID);
- return (int)(fSeek * 1000.0f);
- }
- private void Call_Play(int iSeek)
- {
- float fSeek = (float)iSeek / 1000.0f;
- if( isPlaying == true )
- {
- VideoPlayerPluginSeekToVideo(m_iID,fSeek);
- }
- else
- {
- if( m_CurrentState != MEDIAPLAYER_STATE.READY)
- VideoPlayerPluginSeekToVideo(m_iID,fSeek);
- VideoPlayerPluginPlayVideo(m_iID);
- }
- if( m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
- m_CurrentState = MEDIAPLAYER_STATE.PLAYING;
- }
- private void Call_Reset()
- {
- }
- private void Call_Stop()
- {
- VideoPlayerPluginStopVideo(m_iID);
- }
- private void Call_RePlay()
- {
- VideoPlayerPluginResumeVideo(m_iID);
- m_CurrentState = MEDIAPLAYER_STATE.PLAYING;
- }
- private void Call_Pause()
- {
- m_CurrentState = MEDIAPLAYER_STATE.PAUSED;
- VideoPlayerPluginPauseVideo(m_iID);
- }
- private int Call_GetVideoWidth()
- {
- return (int)videoSize.x;
- }
- private int Call_GetVideoHeight()
- {
- return (int)videoSize.y;
- }
- private void Call_SetUnityTexture(int iTextureID)
- {
- VideoPlayerPluginSetTexture(m_iID,iTextureID);
- }
- private void Call_SetWindowSize()
- {
- }
- private void Call_SetLooping(bool bLoop)
- {
- VideoPlayerPluginSetLoop(m_iID, bLoop);
- }
- public void Call_SetUnityActivity()
- {
- m_iID = VideoPlayerPluginCreateInstance();
- }
- private int Call_GetError()
- {
- return 0;
- }
- private int Call_GetErrorExtra()
- {
- return 0;
- }
- private int Call_GetDuration()
- {
- return (int)(duration * 1000);
- }
- private int Call_GetCurrentSeekPercent()
- {
- return VideoPlayerPluginGetCurrentSeekPercent(m_iID);
- }
- private void Call_SetSpeed(float fSpeed)
- {
- VideoPlayerPluginSetSpeed(m_iID,fSpeed);
- }
- private MEDIAPLAYER_STATE Call_GetStatus()
- {
- if( VideoPlayerPluginFinish(m_iID) )
- {
- return MEDIAPLAYER_STATE.END;
- }
- if( VideoPlayerPluginError(m_iID) )
- {
- return MEDIAPLAYER_STATE.ERROR;
- }
- if( m_CurrentState == MEDIAPLAYER_STATE.PAUSED || m_CurrentState == MEDIAPLAYER_STATE.STOPPED)
- {
- return m_CurrentState;
- }
- else if(isPlaying)
- {
- return MEDIAPLAYER_STATE.PLAYING;
- }
- else if(ready)
- {
- return MEDIAPLAYER_STATE.READY;
- }
- return m_CurrentState;
- }
- private void Call_SelectSoundTrack(int iIndex)
- {
- VideoPlayerPluginSetAudioTrack(m_iID,iIndex);
- }
- private int[] Call_GetSoundTrack()
- {
- if(VideoPlayerPluginGetAudioTrack(m_iID) !=0)
- {
- int [] info = new int[VideoPlayerPluginGetAudioTrack(m_iID)];
- for(int i =0; i < info.Length; i++)
- {
- info[i] = i;
- }
- return info;
- }
- else
- {
- return null;
- }
-
- }
- #endif
- #else
- AVFrame* pConvertedFrame = null;
- sbyte* pConvertedFrameBuffer = null;
- SwsContext* pConvertContext = null;
- AVCodecContext* pCodecContext = null;
- AVCodecContext* pAudioCodecContext = null;
- AVFrame* pDecodedFrame = null;
- AVFrame* pDecodedAudioFrame = null;
- AVFormatContext* pFormatContext = null;
- AVPacket* pPacket = null;
- AVStream* pStream = null;
- AVStream* pStreamAudio = null;
- Queue<IntPtr> m_queuePacketVideo;
- Queue<IntPtr> m_queuePacketSound;
- int iStreamAudioIndex;
- int iStreamIndex;
- AudioClip audioClip;
- AudioSource audioSource;
- long iSoundCount = 0;
- int iInitCount = 0;
- double pts;
- bool bVideoFirstFrameReady = false;
- int m_iID;
- private void Call_Destroy()
- {
- if (loader != null) {
- while (loader.IsAlive == true) {
-
- loader.Abort ();
- }
- loader = null;
- }
- if( threadVideo != null)
- {
- while(threadVideo.IsAlive == true)
- {
- threadVideo.Abort();
- }
- threadVideo = null;
- }
- if (threadVideo2 != null)
- {
- while (threadVideo2.IsAlive == true)
- {
- threadVideo2.Abort();
- }
- threadVideo2 = null;
- }
- ffmpeg.avformat_network_deinit ();
- ReleaseTexture (m_iID);
- }
- private void Call_UnLoad()
- {
- m_CurrentState = MEDIAPLAYER_STATE.NOT_READY;
- m_bSetTextureFirst = false;
- if (loader != null) {
- while (loader.IsAlive == true) {
-
- loader.Abort ();
-
- }
- loader = null;
- }
-
- if( threadVideo != null)
- {
- while(threadVideo.IsAlive == true)
- {
- threadVideo.Abort();
-
- }
- threadVideo = null;
- }
- if (threadVideo2 != null)
- {
- while (threadVideo2.IsAlive == true)
- {
- threadVideo2.Abort();
- }
- threadVideo2 = null;
- }
- if (listAudio != null)
- {
- listAudio.Clear();
- listAudio = null;
- }
- if (listVideo != null)
- {
- listVideo.Clear();
- listVideo = null;
- }
- if (listAudioPts != null)
- {
- listAudioPts.Clear();
- listAudioPts = null;
- }
- if (listAudioPtsTime != null)
- {
- listAudioPtsTime.Clear();
- listAudioPtsTime = null;
- }
- if (listVideoPts != null)
- {
- listVideoPts.Clear();
- listVideoPts = null;
- }
- fCurrentSeekTime = 0.0f;
- fLastFrameTime = 0.0f;
- if (pPacket != null)
- {
- ffmpeg.av_free_packet(pPacket);
- Marshal.FreeCoTaskMem((IntPtr)pPacket);
- pPacket = null;
- }
- int iCount = m_queuePacketVideo.Count;
- for (int i = 0; i < iCount; i++)
- {
- if (m_queuePacketVideo.Count > 0)
- pPacket = (AVPacket*)m_queuePacketVideo.Dequeue();
- if (pPacket != null)
- {
- ffmpeg.av_free_packet(pPacket);
- Marshal.FreeCoTaskMem((IntPtr)pPacket);
- pPacket = null;
- }
- }
- iCount = m_queuePacketSound.Count;
- for (int i = 0; i < iCount; i++)
- {
- if (m_queuePacketSound.Count > 0)
- pPacket = (AVPacket*)m_queuePacketSound.Dequeue();
- if (pPacket != null)
- {
- ffmpeg.av_free_packet(pPacket);
- Marshal.FreeCoTaskMem((IntPtr)pPacket);
- pPacket = null;
- }
- }
- if (pConvertedFrame != null)
- {
- ffmpeg.av_free(pConvertedFrame);
- pConvertedFrame = null;
- }
- if(pConvertedFrameBuffer != null)
- {
- ffmpeg.av_free(pConvertedFrameBuffer);
- pConvertedFrameBuffer = null;
- }
- if(pConvertContext != null)
- {
- ffmpeg.sws_freeContext(pConvertContext);
- pConvertContext = null;
- }
- if(pDecodedFrame != null)
- {
- ffmpeg.av_free(pDecodedFrame);
- pDecodedFrame = null;
- }
- if (pDecodedAudioFrame != null)
- ffmpeg.av_free (pDecodedAudioFrame);
- pDecodedAudioFrame = null;
- if(pCodecContext != null)
- ffmpeg.avcodec_close(pCodecContext);
- pCodecContext = null;
- if (pAudioCodecContext != null)
- ffmpeg.avcodec_close (pAudioCodecContext);
- pAudioCodecContext = null;
- if (pFormatContext != null) {
- AVFormatContext* ppFormatContext = pFormatContext;
- ffmpeg.avformat_close_input (&ppFormatContext);
- }
- pFormatContext = null;
- if (audioSource != null) {
- audioSource.Stop();
- }
- if ( audioClip != null)
- {
- Destroy (audioClip);
- audioClip = null;
- }
- }
- Thread loader = null;
- private unsafe bool Call_Load(string strFileName, int iSeek)
- {
- if(m_queuePacketVideo == null)
- m_queuePacketVideo = new Queue<IntPtr>();
- if (m_queuePacketSound == null)
- m_queuePacketSound = new Queue<IntPtr>();
- fCurrentSeekTime = 0.0f;
- fLastFrameTime = 0.0f;
- iSoundCount = 0;
- iInitCount= 0;
- bSeekTo = true;
- bEnd = false;
- bEndCheck = false;
- if (audioSource != null) {
- audioSource.Stop();
- audioSource.time = 0.0f;
- }
- if ( audioClip != null)
- {
- Destroy (audioClip);
- audioClip = null;
- }
- pFormatContext = ffmpeg.avformat_alloc_context();
-
- if( strFileName.Contains("://") == false)
- {
- strFileName = Application.streamingAssetsPath + "/" + strFileName;
- Debug.Log(strFileName);
- }
- else if( strFileName.Contains("file://") == true)
- {
- strFileName = strFileName.Replace("file://", "");
- }
-
- loader = new Thread ( () => {
- AVFormatContext* ppFomatContext = ffmpeg.avformat_alloc_context();
- ppFomatContext->flags |= ffmpeg.AVFMT_FLAG_NONBLOCK;
- ppFomatContext->flags |= ffmpeg.AVIO_FLAG_NONBLOCK;
- //ppFomatContext->max_interleave_delta = 100000000;
- ppFomatContext->probesize = 50000000;
- // ppFomatContext->max_delay = 1000000; //0.1 secs, default 5 secs
- ///ppFomatContext->probe_score = 100;
- ppFomatContext->max_analyze_duration = 10000000; //0.1 secs
- // ppFomatContext->max_chunk_size = 100;
- // ppFomatContext->max_chunk_duration = 1000000;
- //Debug.Log(ppFomatContext->probesize.ToString() + " " + ppFomatContext->max_analyze_duration.ToString());
-
-
- if (ffmpeg.avformat_open_input(&ppFomatContext, strFileName, null, null) != 0)
- {
- pFormatContext = null;
- m_CurrentState = MEDIAPLAYER_STATE.ERROR;
- Debug.Log("Could not open file");
- throw new ApplicationException(@"Could not open file");
- }
- // ffmpeg.av_dict_free(&options);
- pFormatContext = ppFomatContext;
- if (ffmpeg.avformat_find_stream_info(pFormatContext, null) != 0)
- {
- m_CurrentState = MEDIAPLAYER_STATE.ERROR;
- Debug.Log("Could not find stream info");
- throw new ApplicationException(@"Could not find stream info");
- }
-
- AddActionForUnityMainThread( () => {
- LoadVideoPart2 ();
- });
-
- });
- loader.Priority = System.Threading.ThreadPriority.AboveNormal;
- loader.IsBackground = true;
- loader.Start ();
- return true;
- }
- bool bDirect9 = false;
- void LoadVideoPart2 ()
- {
- pStream = null;
- pStreamAudio = null;
- bool bFindVideo = false;
- bool bFindAudio = false;
- for (var i = 0; i < (pFormatContext)->nb_streams; i++)
- {
- if ((pFormatContext)->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO)
- {
- if(bFindVideo == false)
- {
- bFindVideo = true;
- pStream = (pFormatContext)->streams[i];
- iStreamIndex = i;
- Debug.Log("Video" + iStreamIndex);
- }
- }
- else if((pFormatContext)->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_AUDIO)
- {
- if (bFindAudio == false)
- {
- bFindAudio = true;
- pStreamAudio = (pFormatContext)->streams[i];
- iStreamAudioIndex = i;
- }
- }
- }
- if (pStream == null)
- {
- m_CurrentState = MEDIAPLAYER_STATE.ERROR;
- Debug.Log("Could not found video stream");
- throw new ApplicationException(@"Could not found video stream");
- }
- if (pStreamAudio == null)
- {
- //m_CurrentState = MEDIAPLAYER_STATE.ERROR;
- Debug.Log("Could not found Audio stream" + bFindAudio);
- //throw new ApplicationException(@"Could not found audio stream");
- }
-
- var codecContext = *pStream->codec;
- m_iWidth = codecContext.width;
- m_iHeight = codecContext.height;
- var sourcePixFmt = codecContext.pix_fmt;
- var codecId = codecContext.codec_id;
- var convertToPixFmt = AVPixelFormat.AV_PIX_FMT_RGBA;
- #if UNITY_5
- if( SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Direct3D9)
- {
- bDirect9 = true;
- convertToPixFmt = AVPixelFormat.AV_PIX_FMT_BGRA;
- }
- #endif
- pConvertContext = ffmpeg.sws_getContext(m_iWidth, m_iHeight, sourcePixFmt,
- m_iWidth, m_iHeight, convertToPixFmt,
- ffmpeg.SWS_FAST_BILINEAR, null, null, null);
- if (pConvertContext == null)
- {
- m_CurrentState = MEDIAPLAYER_STATE.ERROR;
- Debug.Log("Could not initialize the conversion context");
- throw new ApplicationException(@"Could not initialize the conversion context");
- }
- //pConvertedFrame = ffmpeg.avcodec_alloc_frame();
- pConvertedFrame = ffmpeg.av_frame_alloc();
- var convertedFrameBufferSize = ffmpeg.avpicture_get_size(convertToPixFmt, m_iWidth, m_iHeight);
- pConvertedFrameBuffer = (sbyte*)ffmpeg.av_malloc((ulong)convertedFrameBufferSize);
- AVPicture* tempPicture = (AVPicture*)pConvertedFrame;
- ffmpeg.avpicture_fill(tempPicture , pConvertedFrameBuffer, convertToPixFmt, m_iWidth, m_iHeight);
- var pCodec = ffmpeg.avcodec_find_decoder(codecId);
- if (pCodec == null)
- {
- m_CurrentState = MEDIAPLAYER_STATE.ERROR;
- Debug.Log("Unsupported codec");
- throw new ApplicationException(@"Unsupported codec");
- }
- pCodecContext = pStream->codec;
- pCodecContext->hwaccel = ff_find_hwaccel(pCodecContext->codec_id ,sourcePixFmt);
- if ((pCodec->capabilities & ffmpeg.AV_CODEC_CAP_TRUNCATED) == ffmpeg.AV_CODEC_CAP_TRUNCATED)
- {
- pCodecContext->flags |= ffmpeg.AV_CODEC_FLAG_TRUNCATED;
- }
- pCodecContext->flags |= ffmpeg.AV_CODEC_FLAG_LOW_DELAY; // cuts off 100ms of latency in h264 -- a must-have
- pCodecContext->flags2 |= ffmpeg.AV_CODEC_FLAG2_FAST;
- if (ffmpeg.avcodec_open2(pCodecContext, pCodec, null) < 0)
- {
- m_CurrentState = MEDIAPLAYER_STATE.ERROR;
- Debug.Log("Could not open codec");
- throw new ApplicationException(@"Could not open codec");
- }
- if(pStreamAudio != null)
- {
- var codecAudioContext = *pStreamAudio->codec;
- var pAudioCodec = ffmpeg.avcodec_find_decoder(codecAudioContext.codec_id);
- if (pAudioCodec == null) {
- m_CurrentState = MEDIAPLAYER_STATE.ERROR;
- Debug.Log("Unsupported codec");
- throw new ApplicationException(@"Unsupported codec");
- }
- pAudioCodecContext = pStreamAudio->codec;
- ///> Initialize Codec Context as Decoder
- if (ffmpeg.avcodec_open2(pAudioCodecContext, pAudioCodec, null) < 0) {
- m_CurrentState = MEDIAPLAYER_STATE.ERROR;
- Debug.Log("Could not open codec");
- throw new ApplicationException(@"Could not open codec");
- }
- }
- pDecodedFrame = ffmpeg.av_frame_alloc();
- pDecodedAudioFrame = ffmpeg.av_frame_alloc();
- listAudio = new List<float[]>();
- listVideo = new Queue<byte[]>();
- listAudioPts = new List<double>();
- listAudioPtsTime = new List<double>();
- listVideoPts = new Queue<float>();
- if( m_strFileName.StartsWith("rtsp",StringComparison.OrdinalIgnoreCase) == true)
- {
- }
- else
- {
- action = Interrupt1;
- //pFormatContext->interrupt_callback.callback = Marshal.GetFunctionPointerForDelegate(action);
- }
- bVideoFirstFrameReady = false;
- threadVideo = new Thread(ThreadUpdate);
- threadVideo.IsBackground = true;
- threadVideo.Start();
- threadVideo2 = new Thread(ThreadUpdate2);
- threadVideo2.IsBackground = true;
- threadVideo2.Start();
- if (m_bAutoPlay == true || m_bReadyPlay == true)
- {
- m_bReadyPlay = false;
- if ( OnReady != null)
- OnReady();
- m_CurrentState = MEDIAPLAYER_STATE.PLAYING;
- }
- else
- {
- m_CurrentState = MEDIAPLAYER_STATE.READY;
- if( OnReady != null)
- OnReady();
- }
- loader = null;
- }
- AVHWAccel *ff_find_hwaccel( AVCodecID codec_id, AVPixelFormat pix_fmt)
- {
- AVHWAccel *hwaccel=null;
- while((hwaccel= ffmpeg.av_hwaccel_next(hwaccel))!=null){
- if ( hwaccel->id == codec_id
- && hwaccel->pix_fmt == pix_fmt)
- return hwaccel;
- }
- return null;
- }
- public delegate void VideoInterrupt();
- VideoInterrupt action = null;
- bool bInterrupt = false;
-
- public void Interrupt1()
- {
- //pFormatContext->interrupt_callback.callback = (IntPtr)null;
- bInterrupt = true;
- // threadVideo.Suspend();
-
- //UpdateVideo();
- //ffmpeg.av_read_pause(pFormatContext);
- // ffmpeg.av_read_play(pFormatContext);
- // Debug.Log ("Interrupt1");
- //SeekTo(Call_GetSeekPosition());
- //Call_Pause ();
- }
- static double av_q2d(AVRational a){
- return a.num / (double) a.den;
- }
- float fLastFrameTime = 0.0f;
- float fCurrentSeekTime = 0.0f;
- float[] fAudioData;
- bool bEnd = false;
- Thread threadVideo;
- Thread threadVideo2;
- List<float[]> listAudio;
- Queue<byte[]> listVideo;
- List<double> listAudioPts;
- List<double> listAudioPtsTime;
- Queue<float> listVideoPts;
- private static void DebugMethod(string message)
- {
- Debug.Log("EasyMovieTexture: " + message);
- }
- private void ThreadUpdate2()
- {
- while(true)
- {
- while (listVideo.Count > 30 || bEnd == true)
- {
-
- UpdateSound();
- Thread.Sleep(1);
- }
- UpdateVideo();
- UpdateSound();
- UpdateSound();
- Thread.Sleep(1);
- }
-
- }
- private void ThreadUpdate()
- {
- /*lock (listVideo)
- {
- if (listVideo != null)
- {
- listVideo.Clear();
- }
- }
- lock (listVideoPts)
- {
- if (listVideoPts != null)
- {
- listVideoPts.Clear();
- }
- }*/
- while (true)
- {
- if (m_CurrentState == MEDIAPLAYER_STATE.ERROR)
- break;
-
- if (listVideo != null)
- {
- if (bInterrupt == true)
- {
- bInterrupt = false;
- //UpdateVideo();
- //pFormatContext->interrupt_callback.callback = Marshal.GetFunctionPointerForDelegate(action);
- // ReadPacket();
-
- }
- if (m_queuePacketVideo.Count < BUFFER_SIZE)
- {
- ReadPacket();
- }
- while (listVideo.Count >30 || bEnd == true )
- {
- if (m_queuePacketVideo.Count < BUFFER_SIZE)
- {
- ReadPacket();
- }
- //UpdateSound();
- Thread.Sleep(1);
-
- }
- }
- // UpdateVideo();
-
-
- //UpdateSound();
-
-
- Thread.Sleep(1);
- //Thread.Sleep(5);
- }
- }
- bool bBuffering = true;
- bool bEndCheck = false;
- bool bReadPacket = false;
- AVPacket* Packet2 = null;
- private void ReadPacket()
- {
-
- AVPacket* Packet = (AVPacket*)Marshal.AllocCoTaskMem(sizeof(AVPacket));
-
- ffmpeg.av_init_packet(Packet);
-
- int ret = ffmpeg.av_read_frame(pFormatContext, Packet);
- if (ret < 0)
- {
- if (ret == -541478725)
- {
- bEndCheck = true;
- lock (m_queuePacketVideo)
- {
- if (m_queuePacketVideo.Count < 3)
- {
- bEnd = true;
- if (Packet != null)
- {
- ffmpeg.av_free_packet(Packet);
- Marshal.FreeCoTaskMem((IntPtr)Packet);
- Packet = null;
- }
- //threadVideo.Abort();
- return;
- }
- }
- }
- else
- {
-
- if (Packet != null)
- {
- ffmpeg.av_free_packet(Packet);
- Marshal.FreeCoTaskMem((IntPtr)Packet);
- Packet = null;
- }
-
- //throw new ApplicationException(@"Could not read frame");
- Debug.Log("Could not read frame");
- }
-
- }
- else
- {
- if (Packet->stream_index == iStreamIndex)
- {
- lock(m_queuePacketVideo)
- {
- m_queuePacketVideo.Enqueue((IntPtr)Packet);
- }
- }
-
- else
- {
- lock (m_queuePacketSound)
- {
- m_queuePacketSound.Enqueue((IntPtr)Packet);
- }
- }
-
- }
- if (bEndCheck == true)
- {
- if (m_queuePacketVideo.Count < 3)
- {
- Debug.Log("End");
- bEnd = true;
- if (Packet != null)
- {
- ffmpeg.av_free_packet(Packet);
- Marshal.FreeCoTaskMem((IntPtr)Packet);
- Packet = null;
- }
- //threadVideo.Abort();
- bEndCheck = false;
- return;
- }
- }
- }
- private void UpdateSound()
- {
-
- var gotSound = 0;
-
- if (m_CurrentState != MEDIAPLAYER_STATE.PAUSED)
- {
- if (m_queuePacketSound.Count > 0)
- pPacket = (AVPacket*)m_queuePacketSound.Dequeue();
- if (pPacket != null)
- {
-
- if (pStreamAudio != null)
- {
-
- if (pPacket->stream_index == iStreamAudioIndex)
- {
-
- int iAudioLen = ffmpeg.avcodec_decode_audio4(pAudioCodecContext, pDecodedAudioFrame, &gotSound, pPacket);
- if (iAudioLen >= 0)
- {
- if (gotSound == 1)
- {
- int iDataSize = ffmpeg.av_samples_get_buffer_size(null, pAudioCodecContext->channels, pDecodedAudioFrame->nb_samples, pAudioCodecContext->sample_fmt, 1);
- int iDataSize2 = ffmpeg.av_samples_get_buffer_size(null, pAudioCodecContext->channels, pDecodedAudioFrame->nb_samples, AVSampleFormat.AV_SAMPLE_FMT_FLT, 1);
- //if( pAudioCodecContext->sample_fmt != AVSampleFormat.AV_SAMPLE_FMT_FLT)
- {
- //for(int i = 0; i < pAudioCodecContext->channels; i++)
- {
- sbyte* outData = (sbyte*)Marshal.AllocCoTaskMem(iDataSize2 * sizeof(sbyte)); ;
- SwrContext* pAudioCvtContext = null;
- if (pAudioCodecContext->channel_layout == 0)
- {
- long layout = ffmpeg.av_get_default_channel_layout(pAudioCodecContext->channels);
- pAudioCvtContext = ffmpeg.swr_alloc_set_opts(null, (long)layout, AVSampleFormat.AV_SAMPLE_FMT_FLT, pAudioCodecContext->sample_rate
- , (long)layout, pAudioCodecContext->sample_fmt, pAudioCodecContext->sample_rate, 0, (void*)0);
- }
- else
- {
- pAudioCvtContext = ffmpeg.swr_alloc_set_opts(null, (long)pAudioCodecContext->channel_layout, AVSampleFormat.AV_SAMPLE_FMT_FLT, pAudioCodecContext->sample_rate
- , (long)pAudioCodecContext->channel_layout, pAudioCodecContext->sample_fmt, pAudioCodecContext->sample_rate, 0, (void*)0);
- }
- int error = 0;
- if ((error = ffmpeg.swr_init(pAudioCvtContext)) < 0)
- {
- Debug.Log("error " + error);
- }
- ffmpeg.swr_convert(pAudioCvtContext, &outData, iDataSize2, pDecodedAudioFrame->extended_data, pDecodedAudioFrame->nb_samples);
- sbyte* soundFrameAddress = outData;
- var soundBufferPtr = new IntPtr(soundFrameAddress);
- byte[] buffer = new byte[iDataSize2];
- Marshal.Copy(soundBufferPtr, buffer, 0, iDataSize2);
- if ((ulong)pPacket->dts != ffmpeg.AV_NOPTS_VALUE)
- {
- pts = ffmpeg.av_frame_get_best_effort_timestamp(pDecodedAudioFrame);
- }
- else
- {
- pts = 0;
- }
- pts *= av_q2d(pStreamAudio->time_base);
- if (bSeekTo == true)
- {
- double value = pts * (double)pDecodedAudioFrame->sample_rate;
- //Debug.Log(value + " " + pts + " " + GetDuration() + " " + pDecodedAudioFrame->pkt_duration);
- iSoundCount = (long)value;
- fCurrentSeekTime = (float)pts;
- fLastFrameTime = fCurrentSeekTime;
- bSeekTo = false;
- }
- //Debug.Log(pts);
- while (pts > 300.0f * (iInitCount + 1))
- {
-
-
- iSoundCount -= (long)(300.0 * (double)pDecodedAudioFrame->sample_rate);
-
- //Debug.Log(iSoundCount);
- iInitCount++;
- }
- //Debug.Log ("sound " + iSoundCount);
- //Debug.Log (pDecodedAudioFrame->pkt_dts + " " +pDecodedAudioFrame->pkt_duration + " " + pDecodedAudioFrame->pkt_pos + " " + pDecodedAudioFrame->pkt_pts + " " + pts);
- fAudioData = new float[buffer.Length / 4];
- Buffer.BlockCopy(buffer, 0, fAudioData, 0, buffer.Length);
- lock (listAudio)
- {
- listAudio.Add(fAudioData);
- lock (listAudioPts)
- {
- lock (listAudioPtsTime)
- {
- iSoundCount += iDataSize2 / 4 / pDecodedAudioFrame->channels;
- listAudioPts.Add(iSoundCount);
- listAudioPtsTime.Add(pts);
- }
- }
- }
- //Debug.Log ( iDataSize + " "+ pts + " " + pAudioCodecContext->sample_rate + " " + pDecodedAudioFrame->sample_rate + " "+ pDecodedAudioFrame->nb_samples + " " + pDecodedAudioFrame->pkt_pts + " " + pDecodedAudioFrame->pkt_pos);
- //Debug.Log ("sound decode time " + pts);
- //audioClip.SetData(fAudioData,(int)(pAudioCodecContext->sample_rate * pts ) );
- //audioClip.SetData(fAudioData,(int)(pDecodedAudioFrame->pkt_pts ) );
- ffmpeg.swr_free(&pAudioCvtContext);
- Marshal.FreeCoTaskMem((IntPtr)outData);
- }
- }
- }
- }
- }
-
- }
-
- }
- if (pPacket != null)
- {
- ffmpeg.av_free_packet(pPacket);
- Marshal.FreeCoTaskMem((IntPtr)pPacket);
- pPacket = null;
- }
-
- }
- }
- private void UpdateVideo()
- {
- var gotPicture = 0;
-
- if (m_queuePacketVideo.Count < 3)
- {
- bBuffering = true;
-
- }
- if(m_queuePacketVideo.Count > 10 || bEndCheck == true)
- {
- bBuffering = false;
-
- }
-
- if(bBuffering == true)
- {
-
- return;
- }
-
- if ( m_CurrentState != MEDIAPLAYER_STATE.PAUSED)
- {
- if(m_queuePacketVideo.Count > 0)
- pPacket = (AVPacket*)m_queuePacketVideo.Dequeue();
- if (pPacket != null)
- {
- if (pPacket->stream_index == iStreamIndex) {
-
-
- var size = ffmpeg.avcodec_decode_video2 (pCodecContext, pDecodedFrame, &gotPicture, pPacket);
- if (size < 0) {
- //throw new ApplicationException (string.Format (@"Error while decoding frame "));
- Debug.Log("Error while decoding frame");
- return;
-
- }
- if ((ulong)pPacket->dts != ffmpeg.AV_NOPTS_VALUE) {
- pts = ffmpeg.av_frame_get_best_effort_timestamp (pDecodedFrame);
- } else {
- pts = 0;
- }
-
- pts *= av_q2d (pStream->time_base);
- if (gotPicture == 1) {
- //Debug.Log("Video " + pts);
- if (pts > 0) {
- if (listVideo.Count > 2)
- {
-
- if (!m_bIsFirstFrameReady) {
- //Debug.Log("FistReady" + listVideo.Count);
- m_bIsFirstFrameReady = true;
- bVideoFirstFrameReady = true;
- }
- }
- }
- //Debug.Log ("Video " + pts);
- //fLastFrameTime = (float)pts;
- sbyte** src = &pDecodedFrame->data0;
- sbyte** dst = &pConvertedFrame->data0;
- int* srcStride = pDecodedFrame->linesize;
- int* dstStride = pConvertedFrame->linesize;
- int iLineSize = srcStride[0] ;
- int iLineSize1 = srcStride[1] ;
- int iLineSize2 = srcStride[2] ;
- if (pCodecContext->width != m_iWidth)
- {
- Debug.Log(pCodecContext->width + " " + pCodecContext->height);
- lock (listVideo)
- {
- listVideo.Clear();
- lock (listVideoPts)
- {
- listVideoPts.Clear();
- }
- }
- if (pConvertedFrame != null)
- {
- ffmpeg.av_free(pConvertedFrame);
- pConvertedFrame = null;
- }
- if (pConvertedFrameBuffer != null)
- {
- ffmpeg.av_free(pConvertedFrameBuffer);
- pConvertedFrameBuffer = null;
- }
- if (pConvertContext != null)
- {
- ffmpeg.sws_freeContext(pConvertContext);
- pConvertContext = null;
- }
- var convertToPixFmt = AVPixelFormat.AV_PIX_FMT_RGBA;
- #if UNITY_5
- if (bDirect9)
- {
- convertToPixFmt = AVPixelFormat.AV_PIX_FMT_BGRA;
- }
- #endif
- var codecContext = *pStream->codec;
-
-
- var sourcePixFmt = codecContext.pix_fmt;
- var codecId = codecContext.codec_id;
- pConvertContext = ffmpeg.sws_getContext(pCodecContext->width, pCodecContext->height, sourcePixFmt,
- pCodecContext->width, pCodecContext->height, convertToPixFmt,
- ffmpeg.SWS_FAST_BILINEAR, null, null, null);
- if (pConvertContext == null)
- {
- m_CurrentState = MEDIAPLAYER_STATE.ERROR;
- Debug.Log("Could not initialize the conversion context");
- throw new ApplicationException(@"Could not initialize the conversion context");
- }
- //pConvertedFrame = ffmpeg.avcodec_alloc_frame();
- pConvertedFrame = ffmpeg.av_frame_alloc();
- var convertedFrameBufferSize = ffmpeg.avpicture_get_size(convertToPixFmt, pCodecContext->width, pCodecContext->height);
- pConvertedFrameBuffer = (sbyte*)ffmpeg.av_malloc((ulong)convertedFrameBufferSize);
- AVPicture* tempPicture = (AVPicture*)pConvertedFrame;
- ffmpeg.avpicture_fill(tempPicture, pConvertedFrameBuffer, convertToPixFmt, pCodecContext->width, pCodecContext->height);
- }
- else
- {
- if (m_bPC_FastMode == true)
- {
- int iVideoSize = m_iWidth * m_iHeight + (2 * m_iWidth / 2 * m_iHeight / 2);
- byte[] buffer = new byte[iVideoSize];
- if (iLineSize != m_iWidth)
- {
- sbyte* convertedFrameAddress;
- IntPtr imageBufferPtr;
- for (int i = 0; i < m_iHeight; i++)
- {
- convertedFrameAddress = pDecodedFrame->data0 + iLineSize * i;
- imageBufferPtr = new IntPtr(convertedFrameAddress);
- Marshal.Copy(imageBufferPtr, buffer, m_iWidth * i, m_iWidth);
- }
- for (int i = 0; i < m_iHeight / 2; i++)
- {
- convertedFrameAddress = pDecodedFrame->data1 + iLineSize1 * i;
- imageBufferPtr = new IntPtr(convertedFrameAddress);
- Marshal.Copy(imageBufferPtr, buffer, m_iWidth * m_iHeight + m_iWidth / 2 * i, m_iWidth / 2);
- convertedFrameAddress = pDecodedFrame->data2 + iLineSize2 * i;
- imageBufferPtr = new IntPtr(convertedFrameAddress);
- Marshal.Copy(imageBufferPtr, buffer, m_iWidth * m_iHeight + m_iWidth / 2 * m_iHeight / 2 + m_iWidth / 2 * i, m_iWidth / 2);
- }
- }
- else
- {
- sbyte* convertedFrameAddress = pDecodedFrame->data0;
- var imageBufferPtr = new IntPtr(convertedFrameAddress);
- Marshal.Copy(imageBufferPtr, buffer, 0, m_iWidth * m_iHeight);
- convertedFrameAddress = pDecodedFrame->data1;
- imageBufferPtr = new IntPtr(convertedFrameAddress);
- Marshal.Copy(imageBufferPtr, buffer, m_iWidth * m_iHeight, m_iWidth / 2 * m_iHeight / 2);
- convertedFrameAddress = pDecodedFrame->data2;
- imageBufferPtr = new IntPtr(convertedFrameAddress);
- Marshal.Copy(imageBufferPtr, buffer, m_iWidth * m_iHeight + m_iWidth / 2 * m_iHeight / 2, m_iWidth / 2 * m_iHeight / 2);
- }
- lock (listVideo)
- {
- listVideo.Enqueue(buffer);
- lock (listVideoPts)
- {
- listVideoPts.Enqueue((float)pts);
- }
- }
- }
- else
- {
- Debug.Log(iLineSize.ToString());
- ffmpeg.sws_scale(pConvertContext, src, srcStride, 0, m_iHeight, dst, dstStride);
- sbyte* convertedFrameAddress = pConvertedFrame->data0;
- var imageBufferPtr = new IntPtr(convertedFrameAddress);
- byte[] buffer = new byte[4 * m_iWidth * m_iHeight];
- Marshal.Copy(imageBufferPtr, buffer, 0, 4 * m_iWidth * m_iHeight);
- lock (listVideo)
- {
- listVideo.Enqueue(buffer);
- lock (listVideoPts)
- {
- listVideoPts.Enqueue((float)pts);
- }
- }
- }
- }
-
- }
-
-
- }
-
- }
- if (pPacket != null)
- {
-
- ffmpeg.av_free_packet(pPacket);
- Marshal.FreeCoTaskMem((IntPtr)pPacket);
- pPacket = null;
- }
- }
- }
- int iSoundBufferCount = 0;
- /*void OnAudioSetPosition(int newPosition)
- {
- //Debug.Log (newPosition);
- //Debug.Log("Pts" + listAudioPtsTime[0]);
- }*/
- void OnAudioRead(float[] data)
- {
- //Debug.Log (listAudio.Count);
- if (listAudio.Count < 3)
- {
- return;
- }
- //Debug.Log (data.Length + " " + listAudio[1].Length);
- if (iSoundBufferCount == 0) {
- iSoundBufferCount = listAudio[0].Length;
- }
- if (iSoundBufferCount < data.Length) {
- Array.Copy (listAudio [0], listAudio [0].Length - iSoundBufferCount, data, 0, iSoundBufferCount);
- //Debug.Log (iSoundBufferCount + " " + data.Length + " " + listAudio [1].Length);
- if ((data.Length - iSoundBufferCount) > listAudio [1].Length) {
- Array.Copy (listAudio [1], 0, data, 0, listAudio [1].Length);
- Array.Copy (listAudio [2], 0, data, iSoundBufferCount + listAudio [1].Length, data.Length - iSoundBufferCount - listAudio [1].Length);
- iSoundBufferCount = listAudio[2].Length - (data.Length - iSoundBufferCount - listAudio [1].Length);
- listAudio.RemoveAt (0);
- listAudioPts.RemoveAt (0);
- listAudioPtsTime.RemoveAt (0);
- } else {
- Array.Copy (listAudio [1], 0, data, iSoundBufferCount, data.Length - iSoundBufferCount);
- iSoundBufferCount = listAudio[1].Length - (data.Length - iSoundBufferCount);
- }
- listAudio.RemoveAt (0);
- listAudioPts.RemoveAt (0);
- listAudioPtsTime.RemoveAt (0);
- } else {
- Array.Copy (listAudio [0], listAudio [0].Length - iSoundBufferCount, data,0, data.Length);
- iSoundBufferCount -= data.Length;
- }
- if (iSoundBufferCount == 0) {
- listAudio.RemoveAt (0);
- listAudioPts.RemoveAt (0);
- listAudioPtsTime.RemoveAt (0);
- iSoundBufferCount = listAudio[0].Length;
- }
- }
- private int m_LastAudioPartSetted = -1;
- Material material = null;
- bool m_bSetTextureFirst = false;
- private void Call_UpdateVideoTexture()
- {
-
- if (bEnd == true && listVideo.Count == 0 )
- {
- m_CurrentState = MEDIAPLAYER_STATE.END;
- if (OnEnd != null)
- {
- OnEnd();
- //return;
- }
- if (m_bLoop == true)
- {
- Destroy (audioClip);
- audioClip = null;
- Load (m_strFileName);
- bEnd = false;
- return;
- }
-
- bEnd = false;
- return;
- }
- /*if (bInterrupt == true)
- {
- if (audioSource != null)
- {
- audioSource.Pause();
- }
- }
- else*/
- {
- if (audioSource != null && m_CurrentState == MEDIAPLAYER_STATE.PLAYING && m_bIsFirstFrameReady == true)
- {
- //if( audioSource.isPlaying == false/* && audioSource.time > 0.02f*/)
- // audioSource.Play();
- }
- }
- if (threadVideo == null && m_CurrentState != MEDIAPLAYER_STATE.END && m_CurrentState != MEDIAPLAYER_STATE.NOT_READY && m_CurrentState != MEDIAPLAYER_STATE.ERROR)
- {
- threadVideo = new Thread(ThreadUpdate);
- threadVideo.IsBackground = true;
- threadVideo.Start();
- }
- if (threadVideo2 == null && m_CurrentState != MEDIAPLAYER_STATE.END && m_CurrentState != MEDIAPLAYER_STATE.NOT_READY && m_CurrentState != MEDIAPLAYER_STATE.ERROR )
- {
-
- threadVideo2 = new Thread(ThreadUpdate2);
- threadVideo2.IsBackground = true;
- threadVideo2.Start();
- }
- if ( m_CurrentState == MEDIAPLAYER_STATE.PLAYING && m_bIsFirstFrameReady == true )
- {
-
- if( bBuffering == true)
- {
- if (audioSource != null)
- audioSource.Pause();
- }
- if (listVideo.Count > 0 && bBuffering == false)
- {
-
- fCurrentSeekTime += Time.deltaTime * m_fSpeed;
- }
-
- }
- //Debug.Log("last " + fLastFrameTime + " " + fCurrentSeekTime + " " + listVideo.Count + " " + m_queuePacketVideo.Count);
-
- if ( fLastFrameTime > fCurrentSeekTime - 0.2f )
- {
- for (int i = 0; i < listAudio.Count; i++)
- {
- if( listAudioPtsTime.Count > i)
- {
- if(audioSource == null && (int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + ((float)Call_GetDuration() / 1000.0f))) > 0)
- {
- audioSource = gameObject.AddComponent<AudioSource>();
- audioSource.volume = m_fVolume;
- }
- }
- if(audioClip == null && audioSource != null )
- {
- /*if ((float)Call_GetDuration() <= 0)
- {
- audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + 300.0f)),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,true,OnAudioRead);
- }
- else*/
- {
- #if UNITY_5
- audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * 300.0f),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,false);
- #else
- audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * 300.0f),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,false,false);
- #endif
- }
- audioSource.clip = audioClip;
- }
- if (audioSource != null && Call_GetDuration() >0)
- {
- if( listAudioPts.Count > i)
- {
- if (listAudioPts [i] >= 0) {
- {
- /*if( listAudioPts[i] > (int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + ((float)Call_GetDuration() / 1000.0f))))
- {
- audioClip.SetData(listAudio[i],(int)(((double)pAudioCodecContext->sample_rate) * listAudioPtsTime[i] ) );
- }
- else*/
- {
- //Debug.Log(audioSource.time + " " + (float)listAudioPtsTime[i] + " " +listAudioPts [i] +" " + fLastFrameTime + " " + listAudio[i].Length);
- audioClip.SetData(listAudio[i],(int)(listAudioPts[i] % (pAudioCodecContext->sample_rate * 300.0f)) );
- // audioClip.SetData(listAudio[i], (int)(listAudioPtsTime[i] % 300.0 * (double)pAudioCodecContext->sample_rate));
- }
- }
- }
- }
- }
- }
- if (audioSource != null && audioSource.isPlaying && Call_GetDuration() >0) {
- listAudio.Clear();
- listAudioPts.Clear();
- listAudioPtsTime.Clear();
- }
- }
- else
- {
- //do
- {
- #if (UNITY_5_2 || UNITY_5_3_OR_NEWER)
-
- if( listVideo.Count > 0)
- {
-
- if(audioSource != null)
- if (audioSource.isPlaying == false )
- audioSource.Play();
- if (m_bPC_FastMode == true)
- {
- if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Direct3D11)
- {
- m_texPtrY = m_VideoTextureY.GetNativeTexturePtr();
- m_texPtrU = m_VideoTextureU.GetNativeTexturePtr();
- m_texPtrV = m_VideoTextureV.GetNativeTexturePtr();
- }
-
-
- SetTextureFromUnity (m_iID,(IntPtr)0,m_texPtrY,m_texPtrU,m_texPtrV, m_iWidth, m_iHeight, listVideo.Dequeue());
- if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Metal)
- {
- GL.IssuePluginEvent(GetRenderEventFunc(), 8000 + m_iID);
- }
- else
- {
- GL.IssuePluginEvent(GetRenderEventFunc(), 7000 + m_iID);
- }
-
- }
- else
- {
- if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Direct3D11)
- {
- m_texPtr = m_VideoTexture.GetNativeTexturePtr();
- }
-
- SetTextureFromUnity (m_iID,m_texPtr,(IntPtr)0,(IntPtr)0,(IntPtr)0, m_iWidth, m_iHeight, listVideo.Dequeue());
- if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Metal)
- {
- GL.IssuePluginEvent(GetRenderEventFunc(), 8000 + m_iID);
- }
- else
- {
- GL.IssuePluginEvent(GetRenderEventFunc(), 7000 + m_iID);
- }
- }
- }
- if( listVideoPts.Count > 0)
- {
- float fpts = listVideoPts.Dequeue();
- if (fpts > fCurrentSeekTime + 2.0f)
- {
-
- fCurrentSeekTime = fpts;
- double value = pts * (double)pDecodedAudioFrame->sample_rate;
- //Debug.Log(value + " " + pts + " " + GetDuration() + " " + pDecodedAudioFrame->pkt_duration);
- iSoundCount = (long)value;
- }
- //Debug.Log(fpts + " " + audioSource.time);
- /* if (fpts > fCurrentSeekTime)
- {
- fCurrentSeekTime = fpts;
- }*/
- if (fLastFrameTime == 0)
- {
-
- {
- if(fpts<0)
- {
- fLastFrameTime = 0;
- }
- else
- {
- //Debug.Log(fpts);
- //fCurrentSeekTime = fpts;
- fLastFrameTime = fpts;
- }
- if( audioSource != null && Call_GetDuration() >0 )
- {
- //audioSource.time = fLastFrameTime%300.0f;
- }
- }
- }
- else
- {
- if(fpts<=0)
- fLastFrameTime = fCurrentSeekTime - 0.05f;
- else
- fLastFrameTime = fpts;
-
- }
- }
- if( audioSource != null && Call_GetDuration() >= 0)
- {
- if( audioSource.time - fLastFrameTime%300.0f > 0.1f)
- {
- //Debug.Log("sync1 " + audioSource.time + " " + fLastFrameTime);
- audioSource.time = fLastFrameTime%300.0f;
- }
- else if( audioSource.time - fLastFrameTime%300.0f < -0.1f)
- {
- //Debug.Log("sync2");
- audioSource.time = fLastFrameTime%300.0f;
- }
- }
- #else
- //var imageBufferPtr = new IntPtr (convertedFrameAddress);
- if( listVideo.Count > 0)
- {
- m_VideoTexture.LoadRawTextureData(listVideo.Dequeue());
- m_VideoTexture.Apply();
- }
- if( listVideoPts.Count > 0)
- {
- float fpts = listVideoPts.Dequeue();
- if(fLastFrameTime == 0)
- {
- if( fpts > fCurrentSeekTime)
- {
- fLastFrameTime = fCurrentSeekTime;
- }
- }
- else
- {
- fLastFrameTime = fpts;
- }
- }
- if( audioSource != null)
- {
- /*if( audioSource.time - fLastFrameTime > 0)
- {
- //Debug.Log("sync1");
- audioSource.time = fLastFrameTime;
- }
- else if( audioSource.time - fLastFrameTime < -0.2f)
- {
- //Debug.Log("sync2");
- audioSource.time = fLastFrameTime;
- }*/
- }
- #endif
- //if (m_bSetTextureFirst == false)
- {
- m_bSetTextureFirst = true;
- if (m_shaderYUV == null)
- {
- m_shaderYUV = Shader.Find("Unlit/Unlit_YUV");
- }
- if (material == null)
- {
- material = new Material(m_shaderYUV);
- }
- if (m_TargetMaterial != null)
- {
- for (int i = 0; i < m_TargetMaterial.Length; i++)
- {
- if (m_TargetMaterial[i] == null)
- continue;
- if (m_TargetMaterial[i].GetComponent<MeshRenderer>() != null)
- {
- if (m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTexture != m_VideoTexture)
- {
-
- m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTexture = m_VideoTexture;
- }
- }
- if (m_bPC_FastMode == true)
- {
- if (m_TargetMaterial[i].GetComponent<MeshRenderer>() != null)
- {
- material.mainTextureScale = m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTextureScale;
- material.mainTextureOffset = m_TargetMaterial[i].GetComponent<MeshRenderer>().material.mainTextureOffset;
- m_TargetMaterial[i].GetComponent<MeshRenderer>().material = material;
- if (m_TargetMaterial[i].GetComponent<MeshRenderer>().material.GetTexture("_Y") != m_VideoTextureY)
- {
-
- m_TargetMaterial[i].GetComponent<MeshRenderer>().material.SetTexture("_Y", m_VideoTextureY);
- }
- if (m_TargetMaterial[i].GetComponent<MeshRenderer>().material.GetTexture("_U") != m_VideoTextureU)
- {
-
- m_TargetMaterial[i].GetComponent<MeshRenderer>().material.SetTexture("_U", m_VideoTextureU);
- }
- if (m_TargetMaterial[i].GetComponent<MeshRenderer>().material.GetTexture("_V") != m_VideoTextureV)
- {
-
- m_TargetMaterial[i].GetComponent<MeshRenderer>().material.SetTexture("_V", m_VideoTextureV);
- }
- }
- }
- if (m_TargetMaterial[i].GetComponent<RawImage>() != null)
- {
- if (m_TargetMaterial[i].GetComponent<RawImage>().texture != m_VideoTexture)
- {
- m_TargetMaterial[i].GetComponent<RawImage>().texture = m_VideoTexture;
- }
- }
- if (m_bPC_FastMode == true)
- {
- if (m_TargetMaterial[i].GetComponent<RawImage>() != null)
- {
- m_TargetMaterial[i].GetComponent<RawImage>().material = material;
- if (m_TargetMaterial[i].GetComponent<RawImage>().material.GetTexture("_Y") != m_VideoTextureY)
- {
- m_TargetMaterial[i].GetComponent<RawImage>().material.SetTexture("_Y", m_VideoTextureY);
- }
- if (m_TargetMaterial[i].GetComponent<RawImage>().material.GetTexture("_U") != m_VideoTextureU)
- {
- m_TargetMaterial[i].GetComponent<RawImage>().material.SetTexture("_U", m_VideoTextureU);
- }
- if (m_TargetMaterial[i].GetComponent<RawImage>().material.GetTexture("_V") != m_VideoTextureV)
- {
- m_TargetMaterial[i].GetComponent<RawImage>().material.SetTexture("_V", m_VideoTextureV);
- }
- }
- }
- }
- }
- }
-
- if (bVideoFirstFrameReady == true) {
- if (OnVideoFirstFrameReady != null) {
- OnVideoFirstFrameReady ();
- bVideoFirstFrameReady = false;
- }
- if (listAudio.Count > 0)
- {
- if (audioSource == null /*&& (int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + ((float)Call_GetDuration() / 1000.0f))) > 0*/)
- {
- audioSource = gameObject.AddComponent<AudioSource>();
- }
- if (audioClip == null && audioSource != null)
- {
- /*if ((float)Call_GetDuration() <= 0)
- {
- audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + 300.0f)),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,true,OnAudioRead);
- }
- else*/
- {
- /*if (Call_GetDuration () > 1500000) {
- audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + 10.0f)),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,false);
- }
- else{
- audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime[i] + 300.0f)),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,false);
- }*/
- #if UNITY_5
-
- audioClip = AudioClip.Create("videoAudio", (int)((float)pAudioCodecContext->sample_rate * 300.0f), pAudioCodecContext->channels, pAudioCodecContext->sample_rate, false);
- #else
- audioClip = AudioClip.Create("videoAudio",(int)((float)pAudioCodecContext->sample_rate * 300.0f),pAudioCodecContext->channels,pAudioCodecContext->sample_rate,false,false);
- #endif
- }
- audioSource.clip = audioClip;
- //audioSource.Play();
- //AudioSource.PlayClipAtPoint(audioClip,new Vector3(0,0,0));
- }
- }
- // Just to be sure I guess - don't know what migh happen if I seek in video etc...
- if (m_LastAudioPartSetted > listAudio.Count)
- {
- m_LastAudioPartSetted = -1;
- }
- // using m_LastAudioPartSetted - 1 again to be sure - what if the last part of audio gets updated/appended? IDK...
- for (int i = Math.Max(0, m_LastAudioPartSetted - 1); i < listAudio.Count; i++) {
-
- m_LastAudioPartSetted = i;
- if (audioSource != null && Call_GetDuration() >0 || Call_GetDuration() == 0) {
- if (listAudioPts.Count > i) {
-
- if (listAudioPts [i] >= 0) {
-
- {
- /*if (listAudioPts [i] > (int)((float)pAudioCodecContext->sample_rate * ((float)listAudioPtsTime [i] + ((float)Call_GetDuration () / 1000.0f)))) {
- audioClip.SetData (listAudio [i], (int)(((double)pAudioCodecContext->sample_rate) * listAudioPtsTime [i]));
- } else*/ {
- //Debug.Log(audioSource.time + " " + (float)listAudioPtsTime[i] + " " +listAudioPts [i] +" " + fLastFrameTime + " " + listAudio[i].Length);
- audioClip.SetData(listAudio[i],(int)(listAudioPts[i] % (pAudioCodecContext->sample_rate * 300.0f)) );
- }
- }
- }
- }
- //if(audioSource.isPlaying == false)
- //audioSource.Play();
- }
- }
- }
- }//while(fLastFrameTime < fCurrentSeekTime );
- //Debug.Log(listAudio.Count);
- }
- }
- private void Call_SetVolume(float fVolume)
- {
- if (audioSource != null)
- audioSource.volume = fVolume;
- }
- bool bSeekTo = false;
- private void Call_SetSeekPosition(int iSeek)
- {
-
- if ( threadVideo != null)
- {
- while(threadVideo.IsAlive == true)
- {
- threadVideo.Abort();
- }
- threadVideo = null;
- }
- if (threadVideo2 != null)
- {
- while (threadVideo2.IsAlive == true)
- {
- threadVideo2.Abort();
- }
- threadVideo2 = null;
- }
- bSeekTo = true;
- iInitCount = 0;
-
- long seek_target = (long)iSeek * 1000;
- Debug.Log (seek_target);
- seek_target= ffmpeg.av_rescale_q(seek_target, ffmpeg.av_get_time_base_q() , pStream->time_base);
- Debug.Log (seek_target);
-
- //int seek_flags = iSeek - (int)(fLastFrameTime * 1000.0f) < 0 ? ffmpeg.AVSEEK_FLAG_BACKWARD : ffmpeg.AVSEEK_FLAG_BACKWARD;
- if(ffmpeg.av_seek_frame(pFormatContext, iStreamIndex,
- seek_target, ffmpeg.AVSEEK_FLAG_BACKWARD) < 0) {
- //error
-
- } else {
- /* handle packet queues... more later... */
- }
- fCurrentSeekTime = (float)iSeek / 1000.0f;
- //fLastFrameTime = (float)iSeek / 1000.0f;
- listVideo.Clear();
- listVideoPts.Clear();
- listAudio.Clear();
- listAudioPts.Clear();
- listAudioPtsTime.Clear();
-
- int iCount = m_queuePacketVideo.Count;
- for (int i = 0; i < iCount; i++)
- {
-
- if (m_queuePacketVideo.Count > 0)
- pPacket = (AVPacket*)m_queuePacketVideo.Dequeue();
- if (pPacket != null)
- {
- ffmpeg.av_free_packet(pPacket);
- Marshal.FreeCoTaskMem((IntPtr)pPacket);
- pPacket = null;
- }
- }
-
- iCount = m_queuePacketSound.Count;
- for (int i = 0; i < iCount; i++)
- {
- if (m_queuePacketSound.Count > 0)
- pPacket = (AVPacket*)m_queuePacketSound.Dequeue();
- if (pPacket != null)
- {
- ffmpeg.av_free_packet(pPacket);
- Marshal.FreeCoTaskMem((IntPtr)pPacket);
- pPacket = null;
- }
- }
-
- Destroy(audioClip);
- audioClip = null;
-
- ffmpeg.avcodec_flush_buffers (pCodecContext);
-
- //Debug.Log (fLastFrameTime + " " + fCurrentSeekTime);
- }
- private int Call_GetSeekPosition()
- {
- return (int)(fCurrentSeekTime * 1000.0f);
- }
- private void Call_Play(int iSeek)
- {
- if (m_CurrentState == MEDIAPLAYER_STATE.READY || m_CurrentState == MEDIAPLAYER_STATE.END || m_CurrentState == MEDIAPLAYER_STATE.PAUSED) {
- SeekTo (iSeek);
- if (audioSource != null) {
-
- audioSource.time = (float) iSeek / 1000.0f;
- }
-
- bEnd = false;
- m_CurrentState = MEDIAPLAYER_STATE.PLAYING;
- }
- }
- private void Call_Reset()
- {
- }
- private void Call_Stop()
- {
- UnLoad();
- m_CurrentState = MEDIAPLAYER_STATE.STOPPED;
- }
- private void Call_RePlay()
- {
- if (audioSource != null) {
- //if( audioSource.isPlaying == false)
- // audioSource.Play();
- }
- if(m_CurrentState == MEDIAPLAYER_STATE.PAUSED)
- m_CurrentState = MEDIAPLAYER_STATE.PLAYING;
- }
- private void Call_Pause()
- {
- if (audioSource != null) {
- audioSource.Pause();
- }
- m_CurrentState = MEDIAPLAYER_STATE.PAUSED;
- }
- private int Call_GetVideoWidth()
- {
- //Debug.Log (m_iWidth);
- if(pCodecContext != null)
- return pCodecContext->width;
- return m_iWidth;
- }
- private int Call_GetVideoHeight()
- {
- if (pCodecContext != null)
- return pCodecContext->height;
- //Debug.Log (m_iHeight);
- return m_iHeight;
- }
- private void Call_SetUnityTexture(int iTextureID)
- {
- }
- private void Call_SetWindowSize()
- {
- }
- private void Call_SetLooping(bool bLoop)
- {
- }
- private void Call_SetRockchip(bool bValue)
- {
- }
- public void Call_SetUnityActivity()
- {
- ffmpeg.av_register_all();
- ffmpeg.avcodec_register_all();
- ffmpeg.avformat_network_init();
- m_iID = SetTexture ();
- }
- private int Call_GetError()
- {
- return 0;
- }
- private int Call_GetErrorExtra()
- {
- return 0;
- }
- private int Call_GetDuration()
- {
- //Debug.Log ((int)(pFormatContext->duration / 1000));
- if (pFormatContext != null)
- return (int)(pFormatContext->duration / 1000);
- return 0;
- }
- private int Call_GetCurrentSeekPercent()
- {
- if (pStream != null)
- {
- return (int)(((double)fCurrentSeekTime + ((double)m_queuePacketVideo.Count / av_q2d(pStream->avg_frame_rate))) / (Call_GetDuration() / 1000.0) * 100);
- }
- return -1;
-
- }
- private void Call_SetSplitOBB(bool bValue, string strOBBName)
- {
- }
- private void Call_SelectSoundTrack(int iIndex)
- {
- }
- private int[] Call_GetSoundTrack()
- {
- return null;
- }
- private void Call_SetSpeed(float fSpeed)
- {
- if (audioSource != null) {
- audioSource.pitch = fSpeed;
- }
- }
- private MEDIAPLAYER_STATE Call_GetStatus()
- {
- return m_CurrentState;
- }
- #endif // !UNITY_EDITOR
- public IEnumerator DownloadStreamingVideoAndLoad(string strURL)
- {
- strURL = strURL.Trim();
- Debug.Log("DownloadStreamingVideo : " + strURL);
- WWW www = new WWW(strURL);
- yield return www;
- if (string.IsNullOrEmpty(www.error))
- {
- if (System.IO.Directory.Exists(Application.persistentDataPath + "/Data") == false)
- System.IO.Directory.CreateDirectory(Application.persistentDataPath + "/Data");
- string write_path = Application.persistentDataPath + "/Data" + strURL.Substring(strURL.LastIndexOf("/"));
- /* if(System.IO.File.Exists(write_path) == true)
- {
- Debug.Log("Delete : " + write_path);
- System.IO.File.Delete(write_path);
- }
- */
- System.IO.File.WriteAllBytes(write_path, www.bytes);
- Load("file://" + write_path);
- }
- else
- {
- Debug.Log(www.error);
- }
- www.Dispose();
- www = null;
- Resources.UnloadUnusedAssets();
- }
- public IEnumerator DownloadStreamingVideoAndLoad2(string strURL)
- {
- strURL = strURL.Trim();
- string write_path = Application.persistentDataPath + "/Data" + strURL.Substring(strURL.LastIndexOf("/"));
- if (System.IO.File.Exists(write_path) == true)
- {
- Load("file://" + write_path);
- }
- else
- {
- WWW www = new WWW(strURL);
-
- yield return www;
- if (string.IsNullOrEmpty(www.error))
- {
- if (System.IO.Directory.Exists(Application.persistentDataPath + "/Data") == false)
- System.IO.Directory.CreateDirectory(Application.persistentDataPath + "/Data");
- System.IO.File.WriteAllBytes(write_path, www.bytes);
- Load("file://" + write_path);
- }
- else
- {
- Debug.Log(www.error);
- }
- www.Dispose();
- www = null;
- Resources.UnloadUnusedAssets();
- }
- }
- IEnumerator CopyStreamingAssetVideoAndLoad(string strURL)
- {
- strURL = strURL.Trim();
- string write_path = Application.persistentDataPath + "/" + strURL;
- if (System.IO.File.Exists(write_path) == false)
- {
- Debug.Log("CopyStreamingAssetVideoAndLoad : " + strURL);
- WWW www = new WWW(Application.streamingAssetsPath + "/" + strURL);
- yield return www;
- if (string.IsNullOrEmpty(www.error))
- {
- Debug.Log(write_path);
- System.IO.File.WriteAllBytes(write_path, www.bytes);
- Load("file://" + write_path);
- }
- else
- {
- Debug.Log(www.error);
- }
- www.Dispose();
- www = null;
- }
- else
- {
- Load("file://" + write_path);
- }
- }
- #endif
- #region UnityMainThread adapter
- private List<Action> unityMainThreadActionList = new List<Action>();
- private bool checkNewActions = false;
- private object thisLock = new object();
- /// <summary>
- /// Execute UnityMainThread operations from unityMainThreadActionList List
- /// </summary>
- void CheckThreading()
- {
- lock (thisLock)
- {
- if (unityMainThreadActionList.Count > 0)
- {
- foreach(var a in unityMainThreadActionList)
- {
- a();
- }
- unityMainThreadActionList.Clear ();
- }
- }
- }
- /// <summary>
- /// Add a sequence of Code enclosed into the Action (a). It triggers checkNewActions to execute code into the Unity Thread Update
- /// </summary>
- /// <param name="a">The alpha component.</param>
- void AddActionForUnityMainThread(Action a)
- {
- lock (thisLock)
- {
- unityMainThreadActionList.Add(a);
- }
- checkNewActions = true;
- }
- #endregion
- }
|