WindowsMediaPlayer.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine;
  5. namespace RenderHeads.Media.AVProVideo
  6. {
  7. public class WindowsMediaPlayer : BaseMediaPlayer
  8. {
  9. public WindowsMediaPlayer(Windows.VideoApi videoApi, bool useHardwareDecoding, bool useTextureMips, bool hintAlphaChannel, bool useLowLatency, string audioDeviceOutputName, bool useUnityAudio, bool forceResample, List<string> preferredFilters)
  10. {
  11. this.SetOptions(videoApi, useHardwareDecoding, useTextureMips, hintAlphaChannel, useLowLatency, audioDeviceOutputName, useUnityAudio, forceResample, preferredFilters);
  12. }
  13. public static bool InitialisePlatform()
  14. {
  15. if (!WindowsMediaPlayer._isInitialised)
  16. {
  17. try
  18. {
  19. if (!WindowsMediaPlayer.Native.Init(QualitySettings.activeColorSpace == ColorSpace.Linear, true))
  20. {
  21. Debug.LogError("[AVProVideo] Failing to initialise platform");
  22. }
  23. else
  24. {
  25. WindowsMediaPlayer._isInitialised = true;
  26. WindowsMediaPlayer._version = WindowsMediaPlayer.GetPluginVersion();
  27. WindowsMediaPlayer._nativeFunction_UpdateAllTextures = WindowsMediaPlayer.Native.GetRenderEventFunc_UpdateAllTextures();
  28. WindowsMediaPlayer._nativeFunction_FreeTextures = WindowsMediaPlayer.Native.GetRenderEventFunc_FreeTextures();
  29. WindowsMediaPlayer._nativeFunction_ExtractFrame = WindowsMediaPlayer.Native.GetRenderEventFunc_WaitForNewFrame();
  30. }
  31. }
  32. catch (DllNotFoundException ex)
  33. {
  34. Debug.LogError("[AVProVideo] Failed to load DLL. " + ex.Message);
  35. }
  36. }
  37. return WindowsMediaPlayer._isInitialised;
  38. }
  39. public static void DeinitPlatform()
  40. {
  41. WindowsMediaPlayer.Native.Deinit();
  42. WindowsMediaPlayer._isInitialised = false;
  43. }
  44. public override int GetNumAudioChannels()
  45. {
  46. return WindowsMediaPlayer.Native.GetAudioChannelCount(this._instance);
  47. }
  48. public void SetOptions(Windows.VideoApi videoApi, bool useHardwareDecoding, bool useTextureMips, bool hintAlphaChannel, bool useLowLatency, string audioDeviceOutputName, bool useUnityAudio, bool forceResample, List<string> preferredFilters)
  49. {
  50. this._videoApi = videoApi;
  51. this._useHardwareDecoding = useHardwareDecoding;
  52. this._useTextureMips = useTextureMips;
  53. this._hintAlphaChannel = hintAlphaChannel;
  54. this._useLowLatency = useLowLatency;
  55. this._audioDeviceOutputName = audioDeviceOutputName;
  56. if (!string.IsNullOrEmpty(this._audioDeviceOutputName))
  57. {
  58. this._audioDeviceOutputName = this._audioDeviceOutputName.Trim();
  59. }
  60. this._useUnityAudio = useUnityAudio;
  61. this._forceAudioResample = forceResample;
  62. this._preferredFilters = preferredFilters;
  63. if (this._preferredFilters != null)
  64. {
  65. for (int i = 0; i < this._preferredFilters.Count; i++)
  66. {
  67. if (!string.IsNullOrEmpty(this._preferredFilters[i]))
  68. {
  69. this._preferredFilters[i] = this._preferredFilters[i].Trim();
  70. }
  71. }
  72. }
  73. }
  74. public override string GetVersion()
  75. {
  76. return WindowsMediaPlayer._version;
  77. }
  78. public override bool OpenVideoFromFile(string path, long offset, string httpHeaderJson)
  79. {
  80. this.CloseVideo();
  81. uint numFilters = 0U;
  82. IntPtr[] array = null;
  83. if (this._preferredFilters != null && this._preferredFilters.Count > 0)
  84. {
  85. numFilters = (uint)this._preferredFilters.Count;
  86. array = new IntPtr[this._preferredFilters.Count];
  87. for (int i = 0; i < array.Length; i++)
  88. {
  89. array[i] = Marshal.StringToHGlobalUni(this._preferredFilters[i]);
  90. }
  91. }
  92. this._instance = WindowsMediaPlayer.Native.OpenSource(this._instance, path, (int)this._videoApi, this._useHardwareDecoding, this._useTextureMips, this._hintAlphaChannel, this._useLowLatency, this._audioDeviceOutputName, this._useUnityAudio, this._forceAudioResample, AudioSettings.outputSampleRate, array, numFilters, (int)this._audioChannelMode);
  93. if (array != null)
  94. {
  95. for (int j = 0; j < array.Length; j++)
  96. {
  97. Marshal.FreeHGlobal(array[j]);
  98. }
  99. }
  100. if (this._instance == IntPtr.Zero)
  101. {
  102. this.DisplayLoadFailureSuggestion(path);
  103. return false;
  104. }
  105. WindowsMediaPlayer.Native.SetUnityAudioEnabled(this._instance, this._useUnityAudio);
  106. return true;
  107. }
  108. public override bool OpenVideoFromBuffer(byte[] buffer)
  109. {
  110. this.CloseVideo();
  111. IntPtr[] array;
  112. if (this._preferredFilters.Count == 0)
  113. {
  114. array = null;
  115. }
  116. else
  117. {
  118. array = new IntPtr[this._preferredFilters.Count];
  119. for (int i = 0; i < array.Length; i++)
  120. {
  121. array[i] = Marshal.StringToHGlobalUni(this._preferredFilters[i]);
  122. }
  123. }
  124. this._instance = WindowsMediaPlayer.Native.OpenSourceFromBuffer(this._instance, buffer, (ulong)((long)buffer.Length), (int)this._videoApi, this._useHardwareDecoding, this._useTextureMips, this._hintAlphaChannel, this._useLowLatency, this._audioDeviceOutputName, this._useUnityAudio, array, (uint)this._preferredFilters.Count);
  125. if (array != null)
  126. {
  127. for (int j = 0; j < array.Length; j++)
  128. {
  129. Marshal.FreeHGlobal(array[j]);
  130. }
  131. }
  132. if (this._instance == IntPtr.Zero)
  133. {
  134. return false;
  135. }
  136. WindowsMediaPlayer.Native.SetUnityAudioEnabled(this._instance, this._useUnityAudio);
  137. return true;
  138. }
  139. private void DisplayLoadFailureSuggestion(string path)
  140. {
  141. bool flag = this._videoApi == Windows.VideoApi.DirectShow || SystemInfo.operatingSystem.Contains("Windows 7") || SystemInfo.operatingSystem.Contains("Windows Vista") || SystemInfo.operatingSystem.Contains("Windows XP");
  142. if (flag && path.Contains(".mp4"))
  143. {
  144. Debug.LogWarning("[AVProVideo] The native Windows DirectShow H.264 decoder doesn't support videos with resolution above 1920x1080. You may need to reduce your video resolution, switch to another codec (such as DivX or Hap), or install 3rd party DirectShow codec (eg LAV Filters). This shouldn't be a problem for Windows 8 and above as it has a native limitation of 3840x2160.");
  145. }
  146. }
  147. public override void CloseVideo()
  148. {
  149. this._width = 0;
  150. this._height = 0;
  151. this._frameRate = 0f;
  152. this._hasAudio = (this._hasVideo = false);
  153. this._hasMetaData = false;
  154. this._canPlay = false;
  155. this._isPaused = false;
  156. this._isPlaying = false;
  157. this._bLoop = false;
  158. this._audioMuted = false;
  159. this._volume = 1f;
  160. this._balance = 0f;
  161. this._lastFrameCount = 0;
  162. this._displayRate = 0f;
  163. this._displayRateTimer = 0f;
  164. this._queueSetAudioTrackIndex = -1;
  165. this._supportsLinearColorSpace = true;
  166. this._lastError = ErrorCode.None;
  167. this._nativeTexture = IntPtr.Zero;
  168. if (this._texture != null)
  169. {
  170. UnityEngine.Object.Destroy(this._texture);
  171. this._texture = null;
  172. }
  173. if (this._instance != IntPtr.Zero)
  174. {
  175. WindowsMediaPlayer.Native.CloseSource(this._instance);
  176. this._instance = IntPtr.Zero;
  177. }
  178. WindowsMediaPlayer.IssueRenderThreadEvent(WindowsMediaPlayer.Native.RenderThreadEvent.FreeTextures);
  179. }
  180. public override void SetLooping(bool looping)
  181. {
  182. this._bLoop = looping;
  183. WindowsMediaPlayer.Native.SetLooping(this._instance, looping);
  184. }
  185. public override bool IsLooping()
  186. {
  187. return this._bLoop;
  188. }
  189. public override bool HasMetaData()
  190. {
  191. return this._hasMetaData;
  192. }
  193. public override bool HasAudio()
  194. {
  195. return this._hasAudio;
  196. }
  197. public override bool HasVideo()
  198. {
  199. return this._hasVideo;
  200. }
  201. public override bool CanPlay()
  202. {
  203. return this._canPlay;
  204. }
  205. public override void Play()
  206. {
  207. this._isPlaying = true;
  208. this._isPaused = false;
  209. WindowsMediaPlayer.Native.Play(this._instance);
  210. }
  211. public override void Pause()
  212. {
  213. this._isPlaying = false;
  214. this._isPaused = true;
  215. WindowsMediaPlayer.Native.Pause(this._instance);
  216. }
  217. public override void Stop()
  218. {
  219. this._isPlaying = false;
  220. this._isPaused = false;
  221. WindowsMediaPlayer.Native.Pause(this._instance);
  222. }
  223. public override bool IsSeeking()
  224. {
  225. return WindowsMediaPlayer.Native.IsSeeking(this._instance);
  226. }
  227. public override bool IsPlaying()
  228. {
  229. return this._isPlaying;
  230. }
  231. public override bool IsPaused()
  232. {
  233. return this._isPaused;
  234. }
  235. public override bool IsFinished()
  236. {
  237. return WindowsMediaPlayer.Native.IsFinished(this._instance);
  238. }
  239. public override bool IsBuffering()
  240. {
  241. return WindowsMediaPlayer.Native.IsBuffering(this._instance);
  242. }
  243. public override float GetDurationMs()
  244. {
  245. return WindowsMediaPlayer.Native.GetDuration(this._instance) * 1000f;
  246. }
  247. public override int GetVideoWidth()
  248. {
  249. return this._width;
  250. }
  251. public override int GetVideoHeight()
  252. {
  253. return this._height;
  254. }
  255. public override float GetVideoFrameRate()
  256. {
  257. return this._frameRate;
  258. }
  259. public override float GetVideoDisplayRate()
  260. {
  261. return this._displayRate;
  262. }
  263. public override Texture GetTexture(int index)
  264. {
  265. Texture result = null;
  266. if (WindowsMediaPlayer.Native.GetTextureFrameCount(this._instance) > 0)
  267. {
  268. result = this._texture;
  269. }
  270. return result;
  271. }
  272. public override int GetTextureFrameCount()
  273. {
  274. return WindowsMediaPlayer.Native.GetTextureFrameCount(this._instance);
  275. }
  276. public override long GetTextureTimeStamp()
  277. {
  278. return WindowsMediaPlayer.Native.GetTextureTimeStamp(this._instance);
  279. }
  280. public override bool RequiresVerticalFlip()
  281. {
  282. return this._isTextureTopDown;
  283. }
  284. public override void Rewind()
  285. {
  286. this.Seek(0f);
  287. }
  288. public override void Seek(float timeMs)
  289. {
  290. WindowsMediaPlayer.Native.SetCurrentTime(this._instance, timeMs / 1000f, false);
  291. }
  292. public override void SeekFast(float timeMs)
  293. {
  294. WindowsMediaPlayer.Native.SetCurrentTime(this._instance, timeMs / 1000f, true);
  295. }
  296. public override float GetCurrentTimeMs()
  297. {
  298. return WindowsMediaPlayer.Native.GetCurrentTime(this._instance) * 1000f;
  299. }
  300. public override void SetPlaybackRate(float rate)
  301. {
  302. WindowsMediaPlayer.Native.SetPlaybackRate(this._instance, rate);
  303. }
  304. public override float GetPlaybackRate()
  305. {
  306. return WindowsMediaPlayer.Native.GetPlaybackRate(this._instance);
  307. }
  308. public override float GetBufferingProgress()
  309. {
  310. return WindowsMediaPlayer.Native.GetBufferingProgress(this._instance);
  311. }
  312. public override int GetBufferedTimeRangeCount()
  313. {
  314. return this._bufferedTimeRangeCount;
  315. }
  316. public override bool GetBufferedTimeRange(int index, ref float startTimeMs, ref float endTimeMs)
  317. {
  318. bool result = false;
  319. if (index >= 0 && index < this._bufferedTimeRangeCount)
  320. {
  321. result = true;
  322. startTimeMs = 1000f * this._bufferedTimeRanges[index * 2];
  323. endTimeMs = 1000f * this._bufferedTimeRanges[index * 2 + 1];
  324. }
  325. return result;
  326. }
  327. public override void MuteAudio(bool bMuted)
  328. {
  329. this._audioMuted = bMuted;
  330. WindowsMediaPlayer.Native.SetMuted(this._instance, this._audioMuted);
  331. }
  332. public override bool IsMuted()
  333. {
  334. return this._audioMuted;
  335. }
  336. public override void SetVolume(float volume)
  337. {
  338. this._volume = volume;
  339. WindowsMediaPlayer.Native.SetVolume(this._instance, volume);
  340. }
  341. public override float GetVolume()
  342. {
  343. return this._volume;
  344. }
  345. public override void SetBalance(float balance)
  346. {
  347. this._balance = balance;
  348. WindowsMediaPlayer.Native.SetBalance(this._instance, balance);
  349. }
  350. public override float GetBalance()
  351. {
  352. return this._balance;
  353. }
  354. public override int GetAudioTrackCount()
  355. {
  356. return WindowsMediaPlayer.Native.GetAudioTrackCount(this._instance);
  357. }
  358. public override int GetCurrentAudioTrack()
  359. {
  360. return WindowsMediaPlayer.Native.GetAudioTrack(this._instance);
  361. }
  362. public override void SetAudioTrack(int index)
  363. {
  364. this._queueSetAudioTrackIndex = index;
  365. }
  366. public override int GetVideoTrackCount()
  367. {
  368. int result = 0;
  369. if (this.HasVideo())
  370. {
  371. result = 1;
  372. }
  373. return result;
  374. }
  375. public override bool IsPlaybackStalled()
  376. {
  377. return WindowsMediaPlayer.Native.IsPlaybackStalled(this._instance);
  378. }
  379. public override string GetCurrentAudioTrackId()
  380. {
  381. return string.Empty;
  382. }
  383. public override int GetCurrentAudioTrackBitrate()
  384. {
  385. return 0;
  386. }
  387. public override int GetCurrentVideoTrack()
  388. {
  389. return 0;
  390. }
  391. public override void SetVideoTrack(int index)
  392. {
  393. }
  394. public override string GetCurrentVideoTrackId()
  395. {
  396. return string.Empty;
  397. }
  398. public override int GetCurrentVideoTrackBitrate()
  399. {
  400. return 0;
  401. }
  402. public override bool WaitForNextFrame(Camera dummyCamera, int previousFrameCount)
  403. {
  404. WindowsMediaPlayer.Native.StartExtractFrame(this._instance);
  405. WindowsMediaPlayer.IssueRenderThreadEvent(WindowsMediaPlayer.Native.RenderThreadEvent.WaitForNewFrame);
  406. dummyCamera.Render();
  407. WindowsMediaPlayer.Native.WaitForExtract(this._instance);
  408. return previousFrameCount != WindowsMediaPlayer.Native.GetTextureFrameCount(this._instance);
  409. }
  410. public override void SetAudioChannelMode(Audio360ChannelMode channelMode)
  411. {
  412. this._audioChannelMode = channelMode;
  413. WindowsMediaPlayer.Native.SetAudioChannelMode(this._instance, (int)channelMode);
  414. }
  415. public override void SetAudioHeadRotation(Quaternion q)
  416. {
  417. WindowsMediaPlayer.Native.SetHeadOrientation(this._instance, q.x, q.y, q.z, q.w);
  418. }
  419. public override void ResetAudioHeadRotation()
  420. {
  421. WindowsMediaPlayer.Native.SetHeadOrientation(this._instance, Quaternion.identity.x, Quaternion.identity.y, Quaternion.identity.z, Quaternion.identity.w);
  422. }
  423. public override void SetAudioFocusEnabled(bool enabled)
  424. {
  425. WindowsMediaPlayer.Native.SetAudioFocusEnabled(this._instance, enabled);
  426. }
  427. public override void SetAudioFocusProperties(float offFocusLevel, float widthDegrees)
  428. {
  429. WindowsMediaPlayer.Native.SetAudioFocusProps(this._instance, offFocusLevel, widthDegrees);
  430. }
  431. public override void SetAudioFocusRotation(Quaternion q)
  432. {
  433. WindowsMediaPlayer.Native.SetAudioFocusRotation(this._instance, q.x, q.y, q.z, q.w);
  434. }
  435. public override void ResetAudioFocus()
  436. {
  437. WindowsMediaPlayer.Native.SetAudioFocusEnabled(this._instance, false);
  438. WindowsMediaPlayer.Native.SetAudioFocusProps(this._instance, 0f, 90f);
  439. WindowsMediaPlayer.Native.SetAudioFocusRotation(this._instance, 0f, 0f, 0f, 1f);
  440. }
  441. public override void Update()
  442. {
  443. WindowsMediaPlayer.Native.Update(this._instance);
  444. this._lastError = (ErrorCode)WindowsMediaPlayer.Native.GetLastErrorCode(this._instance);
  445. if (this._queueSetAudioTrackIndex >= 0 && this._hasAudio)
  446. {
  447. WindowsMediaPlayer.Native.SetAudioTrack(this._instance, this._queueSetAudioTrackIndex);
  448. this._queueSetAudioTrackIndex = -1;
  449. }
  450. this._bufferedTimeRangeCount = WindowsMediaPlayer.Native.GetBufferedRanges(this._instance, this._bufferedTimeRanges, this._bufferedTimeRanges.Length / 2);
  451. if (this._bufferedTimeRangeCount > this._bufferedTimeRanges.Length / 2)
  452. {
  453. this._bufferedTimeRanges = new float[this._bufferedTimeRangeCount * 2];
  454. this._bufferedTimeRangeCount = WindowsMediaPlayer.Native.GetBufferedRanges(this._instance, this._bufferedTimeRanges, this._bufferedTimeRanges.Length / 2);
  455. }
  456. this.UpdateSubtitles();
  457. if (!this._canPlay)
  458. {
  459. if (!this._hasMetaData && WindowsMediaPlayer.Native.HasMetaData(this._instance))
  460. {
  461. if (WindowsMediaPlayer.Native.HasVideo(this._instance))
  462. {
  463. this._width = WindowsMediaPlayer.Native.GetWidth(this._instance);
  464. this._height = WindowsMediaPlayer.Native.GetHeight(this._instance);
  465. this._frameRate = WindowsMediaPlayer.Native.GetFrameRate(this._instance);
  466. if (this._width > 0 && this._height > 0)
  467. {
  468. this._hasVideo = true;
  469. if (Mathf.Max(this._width, this._height) > SystemInfo.maxTextureSize)
  470. {
  471. Debug.LogError(string.Format("[AVProVideo] Video dimensions ({0}x{1}) larger than maxTextureSize ({2} for current build target)", this._width, this._height, SystemInfo.maxTextureSize));
  472. this._width = (this._height = 0);
  473. this._hasVideo = false;
  474. }
  475. }
  476. if (this._hasVideo && WindowsMediaPlayer.Native.HasAudio(this._instance))
  477. {
  478. this._hasAudio = true;
  479. }
  480. }
  481. else if (WindowsMediaPlayer.Native.HasAudio(this._instance))
  482. {
  483. this._hasAudio = true;
  484. }
  485. if (this._hasVideo || this._hasAudio)
  486. {
  487. this._hasMetaData = true;
  488. }
  489. this._playerDescription = Marshal.PtrToStringAnsi(WindowsMediaPlayer.Native.GetPlayerDescription(this._instance));
  490. this._supportsLinearColorSpace = !this._playerDescription.Contains("MF-MediaEngine-Hardware");
  491. Helper.LogInfo(string.Concat(new object[]
  492. {
  493. "Using playback path: ",
  494. this._playerDescription,
  495. " (",
  496. this._width,
  497. "x",
  498. this._height,
  499. "@",
  500. this.GetVideoFrameRate().ToString("F2"),
  501. ")"
  502. }), null);
  503. if (this._hasVideo)
  504. {
  505. this.OnTextureSizeChanged();
  506. }
  507. }
  508. if (this._hasMetaData)
  509. {
  510. this._canPlay = WindowsMediaPlayer.Native.CanPlay(this._instance);
  511. }
  512. }
  513. if (this._hasVideo)
  514. {
  515. IntPtr texturePointer = WindowsMediaPlayer.Native.GetTexturePointer(this._instance);
  516. if (this._texture != null && this._nativeTexture != IntPtr.Zero && this._nativeTexture != texturePointer)
  517. {
  518. this._width = WindowsMediaPlayer.Native.GetWidth(this._instance);
  519. this._height = WindowsMediaPlayer.Native.GetHeight(this._instance);
  520. if (texturePointer == IntPtr.Zero || this._width != this._texture.width || this._height != this._texture.height)
  521. {
  522. if (this._width != this._texture.width || this._height != this._texture.height)
  523. {
  524. Helper.LogInfo(string.Concat(new object[]
  525. {
  526. "Texture size changed: ",
  527. this._width,
  528. " X ",
  529. this._height
  530. }), null);
  531. this.OnTextureSizeChanged();
  532. }
  533. this._nativeTexture = IntPtr.Zero;
  534. UnityEngine.Object.Destroy(this._texture);
  535. this._texture = null;
  536. }
  537. else if (this._nativeTexture != texturePointer)
  538. {
  539. this._texture.UpdateExternalTexture(texturePointer);
  540. this._nativeTexture = texturePointer;
  541. }
  542. }
  543. if (this._textureQuality != QualitySettings.masterTextureLimit)
  544. {
  545. if (this._texture != null && this._nativeTexture != IntPtr.Zero && this._texture.GetNativeTexturePtr() == IntPtr.Zero)
  546. {
  547. this._texture.UpdateExternalTexture(this._nativeTexture);
  548. }
  549. this._textureQuality = QualitySettings.masterTextureLimit;
  550. }
  551. if (this._texture == null && this._width > 0 && this._height > 0 && texturePointer != IntPtr.Zero)
  552. {
  553. this._isTextureTopDown = WindowsMediaPlayer.Native.IsTextureTopDown(this._instance);
  554. this._texture = Texture2D.CreateExternalTexture(this._width, this._height, TextureFormat.RGBA32, this._useTextureMips, false, texturePointer);
  555. if (this._texture != null)
  556. {
  557. this._texture.name = "AVProVideo";
  558. this._nativeTexture = texturePointer;
  559. this.ApplyTextureProperties(this._texture);
  560. }
  561. else
  562. {
  563. Debug.LogError("[AVProVideo] Failed to create texture");
  564. }
  565. }
  566. }
  567. }
  568. private void OnTextureSizeChanged()
  569. {
  570. if ((this._width == 720 || this._height == 480) && this._playerDescription.Contains("DirectShow"))
  571. {
  572. Debug.LogWarning("[AVProVideo] If video fails to play then it may be due to the resolution being higher than 1920x1080 which is the limitation of the Microsoft DirectShow H.264 decoder.\nTo resolve this you can either use Windows 8 or above (and disable 'Force DirectShow' option), resize your video, use a different codec (such as Hap or DivX), or install a 3rd party H.264 decoder such as LAV Filters.");
  573. }
  574. else if ((this._width <= 1920 && this._height <= 1080) || this._playerDescription.Contains("MF-MediaEngine-Software"))
  575. {
  576. }
  577. }
  578. private void UpdateDisplayFrameRate()
  579. {
  580. this._displayRateTimer += Time.deltaTime;
  581. if (this._displayRateTimer >= 0.5f)
  582. {
  583. int textureFrameCount = WindowsMediaPlayer.Native.GetTextureFrameCount(this._instance);
  584. this._displayRate = (float)(textureFrameCount - this._lastFrameCount) / this._displayRateTimer;
  585. this._displayRateTimer -= 0.5f;
  586. if (this._displayRateTimer >= 0.5f)
  587. {
  588. this._displayRateTimer = 0f;
  589. }
  590. this._lastFrameCount = textureFrameCount;
  591. }
  592. }
  593. public override void Render()
  594. {
  595. this.UpdateDisplayFrameRate();
  596. WindowsMediaPlayer.IssueRenderThreadEvent(WindowsMediaPlayer.Native.RenderThreadEvent.UpdateAllTextures);
  597. }
  598. public override void Dispose()
  599. {
  600. this.CloseVideo();
  601. }
  602. public override void GrabAudio(float[] buffer, int floatCount, int channelCount)
  603. {
  604. WindowsMediaPlayer.Native.GrabAudio(this._instance, buffer, floatCount, channelCount);
  605. }
  606. public override bool PlayerSupportsLinearColorSpace()
  607. {
  608. return this._supportsLinearColorSpace;
  609. }
  610. private static void IssueRenderThreadEvent(WindowsMediaPlayer.Native.RenderThreadEvent renderEvent)
  611. {
  612. if (renderEvent == WindowsMediaPlayer.Native.RenderThreadEvent.UpdateAllTextures)
  613. {
  614. GL.IssuePluginEvent(WindowsMediaPlayer._nativeFunction_UpdateAllTextures, 0);
  615. }
  616. else if (renderEvent == WindowsMediaPlayer.Native.RenderThreadEvent.FreeTextures)
  617. {
  618. GL.IssuePluginEvent(WindowsMediaPlayer._nativeFunction_FreeTextures, 0);
  619. }
  620. else if (renderEvent == WindowsMediaPlayer.Native.RenderThreadEvent.WaitForNewFrame)
  621. {
  622. GL.IssuePluginEvent(WindowsMediaPlayer._nativeFunction_ExtractFrame, 0);
  623. }
  624. }
  625. private static string GetPluginVersion()
  626. {
  627. return Marshal.PtrToStringAnsi(WindowsMediaPlayer.Native.GetPluginVersion());
  628. }
  629. public override void OnEnable()
  630. {
  631. base.OnEnable();
  632. if (this._texture != null && this._nativeTexture != IntPtr.Zero && this._texture.GetNativeTexturePtr() == IntPtr.Zero)
  633. {
  634. this._texture.UpdateExternalTexture(this._nativeTexture);
  635. }
  636. this._textureQuality = QualitySettings.masterTextureLimit;
  637. }
  638. private bool _forceAudioResample = true;
  639. private bool _useUnityAudio;
  640. private string _audioDeviceOutputName = string.Empty;
  641. private List<string> _preferredFilters = new List<string>();
  642. private Audio360ChannelMode _audioChannelMode;
  643. private bool _isPlaying;
  644. private bool _isPaused;
  645. private bool _audioMuted;
  646. private float _volume = 1f;
  647. private float _balance;
  648. private bool _bLoop;
  649. private bool _canPlay;
  650. private bool _hasMetaData;
  651. private int _width;
  652. private int _height;
  653. private float _frameRate;
  654. private bool _hasAudio;
  655. private bool _hasVideo;
  656. private bool _isTextureTopDown = true;
  657. private IntPtr _nativeTexture = IntPtr.Zero;
  658. private Texture2D _texture;
  659. private IntPtr _instance = IntPtr.Zero;
  660. private float _displayRateTimer;
  661. private int _lastFrameCount;
  662. private float _displayRate = 1f;
  663. private Windows.VideoApi _videoApi;
  664. private bool _useHardwareDecoding = true;
  665. private bool _useTextureMips;
  666. private bool _hintAlphaChannel;
  667. private bool _useLowLatency;
  668. private int _queueSetAudioTrackIndex = -1;
  669. private bool _supportsLinearColorSpace = true;
  670. private int _bufferedTimeRangeCount;
  671. private float[] _bufferedTimeRanges = new float[0];
  672. private static bool _isInitialised;
  673. private static string _version = "Plug-in not yet initialised";
  674. private static IntPtr _nativeFunction_UpdateAllTextures;
  675. private static IntPtr _nativeFunction_FreeTextures;
  676. private static IntPtr _nativeFunction_ExtractFrame;
  677. private int _textureQuality = QualitySettings.masterTextureLimit;
  678. private struct Native
  679. {
  680. [DllImport("AVProVideo")]
  681. public static extern bool Init(bool linearColorSpace, bool isD3D11NoSingleThreaded);
  682. [DllImport("AVProVideo")]
  683. public static extern void Deinit();
  684. [DllImport("AVProVideo")]
  685. public static extern IntPtr GetPluginVersion();
  686. [DllImport("AVProVideo")]
  687. public static extern bool IsTrialVersion();
  688. [DllImport("AVProVideo")]
  689. public static extern IntPtr OpenSource(IntPtr instance, [MarshalAs(UnmanagedType.LPWStr)] string path, int videoApiIndex, bool useHardwareDecoding, bool generateTextureMips, bool hintAlphaChannel, bool useLowLatency, [MarshalAs(UnmanagedType.LPWStr)] string forceAudioOutputDeviceName, bool useUnityAudio, bool forceResample, int sampleRate, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr)] IntPtr[] preferredFilter, uint numFilters, int audioChannelMode);
  690. [DllImport("AVProVideo")]
  691. public static extern IntPtr OpenSourceFromBuffer(IntPtr instance, byte[] buffer, ulong bufferLength, int videoApiIndex, bool useHardwareDecoding, bool generateTextureMips, bool hintAlphaChannel, bool useLowLatency, [MarshalAs(UnmanagedType.LPWStr)] string forceAudioOutputDeviceName, bool useUnityAudio, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr)] IntPtr[] preferredFilter, uint numFilters);
  692. [DllImport("AVProVideo")]
  693. public static extern void CloseSource(IntPtr instance);
  694. [DllImport("AVProVideo")]
  695. public static extern IntPtr GetPlayerDescription(IntPtr instance);
  696. [DllImport("AVProVideo")]
  697. public static extern int GetLastErrorCode(IntPtr instance);
  698. [DllImport("AVProVideo")]
  699. public static extern void Play(IntPtr instance);
  700. [DllImport("AVProVideo")]
  701. public static extern void Pause(IntPtr instance);
  702. [DllImport("AVProVideo")]
  703. public static extern void SetMuted(IntPtr instance, bool muted);
  704. [DllImport("AVProVideo")]
  705. public static extern void SetVolume(IntPtr instance, float volume);
  706. [DllImport("AVProVideo")]
  707. public static extern void SetBalance(IntPtr instance, float volume);
  708. [DllImport("AVProVideo")]
  709. public static extern void SetLooping(IntPtr instance, bool looping);
  710. [DllImport("AVProVideo")]
  711. public static extern bool HasVideo(IntPtr instance);
  712. [DllImport("AVProVideo")]
  713. public static extern bool HasAudio(IntPtr instance);
  714. [DllImport("AVProVideo")]
  715. public static extern int GetWidth(IntPtr instance);
  716. [DllImport("AVProVideo")]
  717. public static extern int GetHeight(IntPtr instance);
  718. [DllImport("AVProVideo")]
  719. public static extern float GetFrameRate(IntPtr instance);
  720. [DllImport("AVProVideo")]
  721. public static extern float GetDuration(IntPtr instance);
  722. [DllImport("AVProVideo")]
  723. public static extern int GetAudioTrackCount(IntPtr instance);
  724. [DllImport("AVProVideo")]
  725. public static extern bool IsPlaybackStalled(IntPtr instance);
  726. [DllImport("AVProVideo")]
  727. public static extern bool HasMetaData(IntPtr instance);
  728. [DllImport("AVProVideo")]
  729. public static extern bool CanPlay(IntPtr instance);
  730. [DllImport("AVProVideo")]
  731. public static extern bool IsSeeking(IntPtr instance);
  732. [DllImport("AVProVideo")]
  733. public static extern bool IsFinished(IntPtr instance);
  734. [DllImport("AVProVideo")]
  735. public static extern bool IsBuffering(IntPtr instance);
  736. [DllImport("AVProVideo")]
  737. public static extern float GetCurrentTime(IntPtr instance);
  738. [DllImport("AVProVideo")]
  739. public static extern void SetCurrentTime(IntPtr instance, float time, bool fast);
  740. [DllImport("AVProVideo")]
  741. public static extern float GetPlaybackRate(IntPtr instance);
  742. [DllImport("AVProVideo")]
  743. public static extern void SetPlaybackRate(IntPtr instance, float rate);
  744. [DllImport("AVProVideo")]
  745. public static extern int GetAudioTrack(IntPtr instance);
  746. [DllImport("AVProVideo")]
  747. public static extern void SetAudioTrack(IntPtr instance, int index);
  748. [DllImport("AVProVideo")]
  749. public static extern float GetBufferingProgress(IntPtr instance);
  750. [DllImport("AVProVideo")]
  751. public static extern int GetBufferedRanges(IntPtr instance, float[] timeArray, int arrayCount);
  752. [DllImport("AVProVideo")]
  753. public static extern void StartExtractFrame(IntPtr instance);
  754. [DllImport("AVProVideo")]
  755. public static extern void WaitForExtract(IntPtr instance);
  756. [DllImport("AVProVideo")]
  757. public static extern void Update(IntPtr instance);
  758. [DllImport("AVProVideo")]
  759. public static extern IntPtr GetTexturePointer(IntPtr instance);
  760. [DllImport("AVProVideo")]
  761. public static extern bool IsTextureTopDown(IntPtr instance);
  762. [DllImport("AVProVideo")]
  763. public static extern int GetTextureFrameCount(IntPtr instance);
  764. [DllImport("AVProVideo")]
  765. public static extern long GetTextureTimeStamp(IntPtr instance);
  766. [DllImport("AVProVideo")]
  767. public static extern IntPtr GetRenderEventFunc_UpdateAllTextures();
  768. [DllImport("AVProVideo")]
  769. public static extern IntPtr GetRenderEventFunc_FreeTextures();
  770. [DllImport("AVProVideo")]
  771. public static extern IntPtr GetRenderEventFunc_WaitForNewFrame();
  772. [DllImport("AVProVideo")]
  773. public static extern void SetUnityAudioEnabled(IntPtr instance, bool enabled);
  774. [DllImport("AVProVideo")]
  775. public static extern void GrabAudio(IntPtr instance, float[] buffer, int floatCount, int channelCount);
  776. [DllImport("AVProVideo")]
  777. public static extern int GetAudioChannelCount(IntPtr instance);
  778. [DllImport("AVProVideo")]
  779. public static extern int SetAudioChannelMode(IntPtr instance, int channelMode);
  780. [DllImport("AVProVideo")]
  781. public static extern void SetHeadOrientation(IntPtr instance, float x, float y, float z, float w);
  782. [DllImport("AVProVideo")]
  783. public static extern void SetAudioFocusEnabled(IntPtr instance, bool enabled);
  784. [DllImport("AVProVideo")]
  785. public static extern void SetAudioFocusProps(IntPtr instance, float offFocusLevel, float widthDegrees);
  786. [DllImport("AVProVideo")]
  787. public static extern void SetAudioFocusRotation(IntPtr instance, float x, float y, float z, float w);
  788. public const int PluginID = 262537216;
  789. public enum RenderThreadEvent
  790. {
  791. UpdateAllTextures,
  792. FreeTextures,
  793. WaitForNewFrame
  794. }
  795. }
  796. }
  797. }