PlatformMediaPlayerExtensions.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //-----------------------------------------------------------------------------
  2. // Copyright 2015-2024 RenderHeads Ltd. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. #if UNITY_2017_2_OR_NEWER && (UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || (!UNITY_EDITOR && (UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS || UNITY_ANDROID)))
  5. using System;
  6. using System.Runtime.InteropServices;
  7. using UnityEngine;
  8. namespace RenderHeads.Media.AVProVideo
  9. {
  10. internal static class PlatformMediaPlayerExtensions
  11. {
  12. // AVPPlayerStatus
  13. internal static bool IsReadyToPlay(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  14. {
  15. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.ReadyToPlay) == PlatformMediaPlayer.Native.AVPPlayerStatus.ReadyToPlay;
  16. }
  17. internal static bool IsPlaying(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  18. {
  19. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.Playing) == PlatformMediaPlayer.Native.AVPPlayerStatus.Playing;
  20. }
  21. internal static bool IsPaused(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  22. {
  23. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.Paused) == PlatformMediaPlayer.Native.AVPPlayerStatus.Paused;
  24. }
  25. internal static bool IsFinished(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  26. {
  27. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.Finished) == PlatformMediaPlayer.Native.AVPPlayerStatus.Finished;
  28. }
  29. internal static bool IsSeeking(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  30. {
  31. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.Seeking) == PlatformMediaPlayer.Native.AVPPlayerStatus.Seeking;
  32. }
  33. internal static bool IsBuffering(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  34. {
  35. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.Buffering) == PlatformMediaPlayer.Native.AVPPlayerStatus.Buffering;
  36. }
  37. internal static bool IsStalled(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  38. {
  39. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.Stalled) == PlatformMediaPlayer.Native.AVPPlayerStatus.Stalled;
  40. }
  41. internal static bool IsExternalPlaybackActive(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  42. {
  43. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.ExternalPlaybackActive) == PlatformMediaPlayer.Native.AVPPlayerStatus.ExternalPlaybackActive;
  44. }
  45. internal static bool IsCached(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  46. {
  47. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.Cached) == PlatformMediaPlayer.Native.AVPPlayerStatus.Cached;
  48. }
  49. internal static bool HasFinishedSeeking(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  50. {
  51. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.FinishedSeeking) == PlatformMediaPlayer.Native.AVPPlayerStatus.FinishedSeeking;
  52. }
  53. internal static bool HasUpdatedAssetInfo(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  54. {
  55. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.UpdatedAssetInfo) == PlatformMediaPlayer.Native.AVPPlayerStatus.UpdatedAssetInfo;
  56. }
  57. internal static bool HasUpdatedTexture(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  58. {
  59. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.UpdatedTexture) == PlatformMediaPlayer.Native.AVPPlayerStatus.UpdatedTexture;
  60. }
  61. internal static bool HasUpdatedTextureTransform(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  62. {
  63. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.UpdatedTextureTransform) == PlatformMediaPlayer.Native.AVPPlayerStatus.UpdatedTextureTransform;
  64. }
  65. internal static bool HasUpdatedBufferedTimeRanges(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  66. {
  67. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.UpdatedBufferedTimeRanges) == PlatformMediaPlayer.Native.AVPPlayerStatus.UpdatedBufferedTimeRanges;
  68. }
  69. internal static bool HasUpdatedSeekableTimeRanges(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  70. {
  71. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.UpdatedSeekableTimeRanges) == PlatformMediaPlayer.Native.AVPPlayerStatus.UpdatedSeekableTimeRanges;
  72. }
  73. internal static bool HasUpdatedText(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  74. {
  75. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.UpdatedText) == PlatformMediaPlayer.Native.AVPPlayerStatus.UpdatedText;
  76. }
  77. internal static bool HasVideo(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  78. {
  79. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.HasVideo) == PlatformMediaPlayer.Native.AVPPlayerStatus.HasVideo;
  80. }
  81. internal static bool HasAudio(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  82. {
  83. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.HasAudio) == PlatformMediaPlayer.Native.AVPPlayerStatus.HasAudio;
  84. }
  85. internal static bool HasText(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  86. {
  87. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.HasText) == PlatformMediaPlayer.Native.AVPPlayerStatus.HasText;
  88. }
  89. internal static bool HasMetadata(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  90. {
  91. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.HasMetadata) == PlatformMediaPlayer.Native.AVPPlayerStatus.HasMetadata;
  92. }
  93. internal static bool HasFailed(this PlatformMediaPlayer.Native.AVPPlayerStatus status)
  94. {
  95. return (status & PlatformMediaPlayer.Native.AVPPlayerStatus.Failed) == PlatformMediaPlayer.Native.AVPPlayerStatus.Failed;
  96. }
  97. // AVPPlayerFlags
  98. internal static bool IsLooping(this PlatformMediaPlayer.Native.AVPPlayerFlags flags)
  99. {
  100. return (flags & PlatformMediaPlayer.Native.AVPPlayerFlags.Looping) == PlatformMediaPlayer.Native.AVPPlayerFlags.Looping;
  101. }
  102. internal static PlatformMediaPlayer.Native.AVPPlayerFlags SetLooping(this PlatformMediaPlayer.Native.AVPPlayerFlags flags, bool b)
  103. {
  104. if (flags.IsLooping() ^ b)
  105. {
  106. flags = (b ? flags | PlatformMediaPlayer.Native.AVPPlayerFlags.Looping
  107. : flags & ~PlatformMediaPlayer.Native.AVPPlayerFlags.Looping) | PlatformMediaPlayer.Native.AVPPlayerFlags.Dirty;
  108. }
  109. return flags;
  110. }
  111. internal static bool IsMuted(this PlatformMediaPlayer.Native.AVPPlayerFlags flags)
  112. {
  113. return (flags & PlatformMediaPlayer.Native.AVPPlayerFlags.Muted) == PlatformMediaPlayer.Native.AVPPlayerFlags.Muted;
  114. }
  115. internal static PlatformMediaPlayer.Native.AVPPlayerFlags SetMuted(this PlatformMediaPlayer.Native.AVPPlayerFlags flags, bool b)
  116. {
  117. if (flags.IsMuted() ^ b)
  118. {
  119. flags = (b ? flags | PlatformMediaPlayer.Native.AVPPlayerFlags.Muted
  120. : flags & ~PlatformMediaPlayer.Native.AVPPlayerFlags.Muted) | PlatformMediaPlayer.Native.AVPPlayerFlags.Dirty;
  121. }
  122. return flags;
  123. }
  124. internal static bool IsExternalPlaybackAllowed(this PlatformMediaPlayer.Native.AVPPlayerFlags flags)
  125. {
  126. return (flags & PlatformMediaPlayer.Native.AVPPlayerFlags.AllowExternalPlayback) == PlatformMediaPlayer.Native.AVPPlayerFlags.AllowExternalPlayback;
  127. }
  128. internal static PlatformMediaPlayer.Native.AVPPlayerFlags SetAllowExternalPlayback(this PlatformMediaPlayer.Native.AVPPlayerFlags flags, bool b)
  129. {
  130. if (flags.IsExternalPlaybackAllowed() ^ b)
  131. {
  132. flags = (b ? flags | PlatformMediaPlayer.Native.AVPPlayerFlags.AllowExternalPlayback
  133. : flags & ~PlatformMediaPlayer.Native.AVPPlayerFlags.AllowExternalPlayback) | PlatformMediaPlayer.Native.AVPPlayerFlags.Dirty;
  134. }
  135. return flags;
  136. }
  137. internal static bool ResumePlayback(this PlatformMediaPlayer.Native.AVPPlayerFlags flags)
  138. {
  139. return (flags & PlatformMediaPlayer.Native.AVPPlayerFlags.ResumePlayback) == PlatformMediaPlayer.Native.AVPPlayerFlags.ResumePlayback;
  140. }
  141. internal static PlatformMediaPlayer.Native.AVPPlayerFlags SetResumePlayback(this PlatformMediaPlayer.Native.AVPPlayerFlags flags, bool b)
  142. {
  143. if (flags.ResumePlayback() ^ b)
  144. {
  145. flags = (b ? flags | PlatformMediaPlayer.Native.AVPPlayerFlags.ResumePlayback
  146. : flags & ~PlatformMediaPlayer.Native.AVPPlayerFlags.ResumePlayback) | PlatformMediaPlayer.Native.AVPPlayerFlags.Dirty;
  147. }
  148. return flags;
  149. }
  150. internal static bool IsDirty(this PlatformMediaPlayer.Native.AVPPlayerFlags flags)
  151. {
  152. return (flags & PlatformMediaPlayer.Native.AVPPlayerFlags.Dirty) == PlatformMediaPlayer.Native.AVPPlayerFlags.Dirty;
  153. }
  154. internal static PlatformMediaPlayer.Native.AVPPlayerFlags SetDirty(this PlatformMediaPlayer.Native.AVPPlayerFlags flags, bool b)
  155. {
  156. if (flags.IsDirty() ^ b)
  157. {
  158. flags = b ? flags | PlatformMediaPlayer.Native.AVPPlayerFlags.Dirty : flags & ~PlatformMediaPlayer.Native.AVPPlayerFlags.Dirty;
  159. }
  160. return flags;
  161. }
  162. // MARK: AVPPlayerAssetFlags
  163. internal static bool IsCompatibleWithAirPlay(this PlatformMediaPlayer.Native.AVPPlayerAssetFlags flags)
  164. {
  165. return (flags & PlatformMediaPlayer.Native.AVPPlayerAssetFlags.CompatibleWithAirPlay) == PlatformMediaPlayer.Native.AVPPlayerAssetFlags.CompatibleWithAirPlay;
  166. }
  167. // MARK: AVPPlayerTrackFlags
  168. internal static bool IsDefault(this PlatformMediaPlayer.Native.AVPPlayerTrackFlags flags)
  169. {
  170. return (flags & PlatformMediaPlayer.Native.AVPPlayerTrackFlags.Default) == PlatformMediaPlayer.Native.AVPPlayerTrackFlags.Default;
  171. }
  172. // AVPPlayerTextureFlags
  173. internal static bool IsFlipped(this PlatformMediaPlayer.Native.AVPPlayerTextureFlags flags)
  174. {
  175. return (flags & PlatformMediaPlayer.Native.AVPPlayerTextureFlags.Flipped) == PlatformMediaPlayer.Native.AVPPlayerTextureFlags.Flipped;
  176. }
  177. internal static bool IsLinear(this PlatformMediaPlayer.Native.AVPPlayerTextureFlags flags)
  178. {
  179. return (flags & PlatformMediaPlayer.Native.AVPPlayerTextureFlags.Linear) == PlatformMediaPlayer.Native.AVPPlayerTextureFlags.Linear;
  180. }
  181. internal static bool IsMipmapped(this PlatformMediaPlayer.Native.AVPPlayerTextureFlags flags)
  182. {
  183. return (flags & PlatformMediaPlayer.Native.AVPPlayerTextureFlags.Mipmapped) == PlatformMediaPlayer.Native.AVPPlayerTextureFlags.Mipmapped;
  184. }
  185. }
  186. }
  187. #endif