AppleMediaPlayerExtensions.cs 9.3 KB

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