AppleMediaPlayer+Native.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. public sealed partial class AppleMediaPlayer
  11. {
  12. internal partial struct Native
  13. {
  14. #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
  15. private const string PluginName = "AVProVideo";
  16. #elif UNITY_IOS || UNITY_TVOS
  17. private const string PluginName = "__Internal";
  18. #endif
  19. // Video settings
  20. internal enum AVPPlayerVideoPixelFormat: int
  21. {
  22. Invalid,
  23. Bgra,
  24. YCbCr420
  25. }
  26. [Flags]
  27. internal enum AVPPlayerVideoOutputSettingsFlags: int
  28. {
  29. None = 0,
  30. LinearColorSpace = 1 << 0,
  31. GenerateMipmaps = 1 << 1,
  32. }
  33. // Audio settings
  34. internal enum AVPPlayerAudioOutputMode : int
  35. {
  36. SystemDirect,
  37. Unity,
  38. SystemDirectWithCapture,
  39. }
  40. // Network settings
  41. [Flags]
  42. internal enum AVPPlayerNetworkSettingsFlags: int
  43. {
  44. None = 0,
  45. PlayWithoutBuffering = 1 << 0,
  46. UseSinglePlayerItem = 1 << 1,
  47. }
  48. [StructLayout(LayoutKind.Sequential)]
  49. internal struct AVPPlayerSettings
  50. {
  51. // Video
  52. internal AVPPlayerVideoPixelFormat pixelFormat;
  53. internal AVPPlayerVideoOutputSettingsFlags videoFlags;
  54. internal float preferredMaximumResolution_width;
  55. internal float preferredMaximumResolution_height;
  56. internal float maximumPlaybackRate;
  57. // Audio
  58. internal AVPPlayerAudioOutputMode audioOutputMode;
  59. internal int sampleRate;
  60. internal int bufferLength;
  61. internal int audioFlags;
  62. // Network
  63. internal double preferredPeakBitRate;
  64. internal double preferredForwardBufferDuration;
  65. internal AVPPlayerNetworkSettingsFlags networkFlags;
  66. }
  67. [Flags]
  68. internal enum AVPPlayerStatus : int
  69. {
  70. Unknown = 0,
  71. ReadyToPlay = 1 << 0,
  72. Playing = 1 << 1,
  73. Paused = 1 << 2,
  74. Finished = 1 << 3,
  75. Seeking = 1 << 4,
  76. Buffering = 1 << 5,
  77. Stalled = 1 << 6,
  78. ExternalPlaybackActive = 1 << 7,
  79. Cached = 1 << 8,
  80. FinishedSeeking = 1 << 9,
  81. UpdatedAssetInfo = 1 << 16,
  82. UpdatedTexture = 1 << 17,
  83. UpdatedBufferedTimeRanges = 1 << 18,
  84. UpdatedSeekableTimeRanges = 1 << 19,
  85. UpdatedText = 1 << 20,
  86. HasVideo = 1 << 24,
  87. HasAudio = 1 << 25,
  88. HasText = 1 << 26,
  89. HasMetadata = 1 << 27,
  90. Failed = 1 << 31
  91. }
  92. [Flags]
  93. internal enum AVPPlayerFlags : int
  94. {
  95. None = 0,
  96. Looping = 1 << 0,
  97. Muted = 1 << 1,
  98. AllowExternalPlayback = 1 << 2,
  99. ResumePlayback = 1 << 16, // iOS only, resumes playback after audio session route change
  100. Dirty = 1 << 31
  101. }
  102. internal enum AVPPlayerExternalPlaybackVideoGravity : int
  103. {
  104. Resize,
  105. ResizeAspect,
  106. ResizeAspectFill
  107. };
  108. [StructLayout(LayoutKind.Sequential)]
  109. internal struct AVPPlayerSize
  110. {
  111. internal float width;
  112. internal float height;
  113. }
  114. [StructLayout(LayoutKind.Sequential)]
  115. internal struct AVPAffineTransform
  116. {
  117. internal float a;
  118. internal float b;
  119. internal float c;
  120. internal float d;
  121. internal float tx;
  122. internal float ty;
  123. }
  124. [Flags]
  125. internal enum AVPPlayerAssetFlags : int
  126. {
  127. None = 0,
  128. CompatibleWithAirPlay = 1 << 0,
  129. };
  130. [StructLayout(LayoutKind.Sequential)]
  131. internal struct AVPPlayerAssetInfo
  132. {
  133. internal double duration;
  134. internal AVPPlayerSize dimensions;
  135. internal float frameRate;
  136. internal int videoTrackCount;
  137. internal int audioTrackCount;
  138. internal int textTrackCount;
  139. internal AVPPlayerAssetFlags flags;
  140. }
  141. [Flags]
  142. internal enum AVPPlayerTrackFlags: int
  143. {
  144. Default = 1 << 0,
  145. }
  146. internal enum AVPPlayerVideoTrackStereoMode: int
  147. {
  148. Unknown,
  149. Monoscopic,
  150. StereoscopicTopBottom,
  151. StereoscopicLeftRight,
  152. StereoscopicCustom,
  153. StereoscopicRightLeft,
  154. }
  155. [Flags]
  156. internal enum AVPPlayerVideoTrackFlags: int
  157. {
  158. HasAlpha = 1 << 0,
  159. }
  160. [StructLayout(LayoutKind.Sequential)]
  161. internal struct AVPPlayerVideoTrackInfo
  162. {
  163. [MarshalAs(UnmanagedType.LPWStr)] internal string name;
  164. [MarshalAs(UnmanagedType.LPWStr)] internal string language;
  165. internal int trackId;
  166. internal float estimatedDataRate;
  167. internal uint codecSubtype;
  168. internal AVPPlayerTrackFlags flags;
  169. internal AVPPlayerSize dimensions;
  170. internal float frameRate;
  171. internal AVPAffineTransform transform;
  172. internal AVPPlayerVideoTrackStereoMode stereoMode;
  173. internal int bitsPerComponent;
  174. internal AVPPlayerVideoTrackFlags videoTrackFlags;
  175. internal Matrix4x4 yCbCrTransform;
  176. }
  177. [StructLayout(LayoutKind.Sequential)]
  178. internal struct AVPPlayerAudioTrackInfo
  179. {
  180. [MarshalAs(UnmanagedType.LPWStr)] internal string name;
  181. [MarshalAs(UnmanagedType.LPWStr)] internal string language;
  182. internal int trackId;
  183. internal float estimatedDataRate;
  184. internal uint codecSubtype;
  185. internal AVPPlayerTrackFlags flags;
  186. internal double sampleRate;
  187. internal uint channelCount;
  188. internal uint channelLayoutTag;
  189. internal AudioChannelMaskFlags channelBitmap;
  190. }
  191. [StructLayout(LayoutKind.Sequential)]
  192. internal struct AVPPlayerTextTrackInfo
  193. {
  194. [MarshalAs(UnmanagedType.LPWStr)] internal string name;
  195. [MarshalAs(UnmanagedType.LPWStr)] internal string language;
  196. internal int trackId;
  197. internal float estimatedDataRate;
  198. internal uint codecSubtype;
  199. internal AVPPlayerTrackFlags flags;
  200. }
  201. [StructLayout(LayoutKind.Sequential)]
  202. internal struct AVPPlayerTimeRange
  203. {
  204. internal double start;
  205. internal double duration;
  206. };
  207. [StructLayout(LayoutKind.Sequential)]
  208. internal struct AVPPlayerState
  209. {
  210. internal AVPPlayerStatus status;
  211. internal double currentTime;
  212. internal double currentDate;
  213. internal int selectedVideoTrack;
  214. internal int selectedAudioTrack;
  215. internal int selectedTextTrack;
  216. internal int bufferedTimeRangesCount;
  217. internal int seekableTimeRangesCount;
  218. internal int audioCaptureBufferedSamplesCount;
  219. }
  220. internal enum AVPPlayerTextureFormat: int
  221. {
  222. Unknown,
  223. BGRA8,
  224. R8,
  225. RG8,
  226. BC1,
  227. BC3,
  228. BC4,
  229. BC5,
  230. BC7,
  231. BGR10A2,
  232. R16,
  233. RG16,
  234. BGR10XR,
  235. }
  236. [StructLayout(LayoutKind.Sequential)]
  237. internal struct AVPPlayerTexturePlane
  238. {
  239. internal IntPtr plane;
  240. internal int width;
  241. internal int height;
  242. internal AVPPlayerTextureFormat textureFormat;
  243. }
  244. [Flags]
  245. internal enum AVPPlayerTextureFlags: int
  246. {
  247. None = 0,
  248. Flipped = 1 << 0,
  249. Linear = 1 << 1,
  250. Mipmapped = 1 << 2,
  251. }
  252. internal enum AVPPlayerTextureYCbCrMatrix: int
  253. {
  254. Identity,
  255. ITU_R_601,
  256. ITU_R_709,
  257. }
  258. [StructLayout(LayoutKind.Sequential)]
  259. internal struct AVPPlayerTexture
  260. {
  261. [MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
  262. internal AVPPlayerTexturePlane[] planes;
  263. internal long itemTime;
  264. internal int frameCount;
  265. internal int planeCount;
  266. internal AVPPlayerTextureFlags flags;
  267. internal AVPPlayerTextureYCbCrMatrix YCbCrMatrix;
  268. };
  269. [StructLayout(LayoutKind.Sequential)]
  270. internal struct AVPPlayerText
  271. {
  272. internal IntPtr buffer;
  273. internal long itemTime;
  274. internal int length;
  275. internal int sequence;
  276. };
  277. internal enum AVPPlayerTrackType: int
  278. {
  279. Video,
  280. Audio,
  281. Text
  282. };
  283. #if !UNITY_EDITOR && (UNITY_IOS || UNITY_TVOS)
  284. [DllImport(PluginName)]
  285. internal static extern void AVPPluginBootstrap();
  286. #endif
  287. [DllImport(PluginName)]
  288. private static extern IntPtr AVPPluginGetVersionStringPointer();
  289. internal static string GetPluginVersion()
  290. {
  291. return System.Runtime.InteropServices.Marshal.PtrToStringAnsi(AVPPluginGetVersionStringPointer());
  292. }
  293. [DllImport(PluginName)]
  294. internal static extern IntPtr AVPPluginMakePlayer(Native.AVPPlayerSettings settings);
  295. [DllImport(PluginName)]
  296. internal static extern IntPtr AVPPlayerRelease(IntPtr player);
  297. [DllImport(PluginName)]
  298. internal static extern void AVPPlayerGetState(IntPtr player, ref AVPPlayerState state);
  299. [DllImport(PluginName)]
  300. internal static extern void AVPPlayerSetFlags(IntPtr player, int flags);
  301. [DllImport(PluginName)]
  302. internal static extern void AVPPlayerGetAssetInfo(IntPtr player, ref AVPPlayerAssetInfo info);
  303. [DllImport(PluginName)]
  304. internal static extern void AVPPlayerGetVideoTrackInfo(IntPtr player, int index, ref AVPPlayerVideoTrackInfo info);
  305. [DllImport(PluginName)]
  306. internal static extern void AVPPlayerGetAudioTrackInfo(IntPtr player, int index, ref AVPPlayerAudioTrackInfo info);
  307. [DllImport(PluginName)]
  308. internal static extern void AVPPlayerGetTextTrackInfo(IntPtr player, int index, ref AVPPlayerTextTrackInfo info);
  309. [DllImport(PluginName)]
  310. internal static extern void AVPPlayerGetBufferedTimeRanges(IntPtr player, AVPPlayerTimeRange[] ranges, int count);
  311. [DllImport(PluginName)]
  312. internal static extern void AVPPlayerGetSeekableTimeRanges(IntPtr player, AVPPlayerTimeRange[] ranges, int count);
  313. [DllImport(PluginName)]
  314. internal static extern void AVPPlayerGetTexture(IntPtr player, ref AVPPlayerTexture texture);
  315. [DllImport(PluginName)]
  316. internal static extern void AVPPlayerGetText(IntPtr player, ref AVPPlayerText text);
  317. [DllImport(PluginName)]
  318. internal static extern void AVPPlayerSetPlayerSettings(IntPtr player, AVPPlayerSettings settings);
  319. [DllImport(PluginName)]
  320. [return: MarshalAs(UnmanagedType.U1)]
  321. internal static extern bool AVPPlayerOpenURL(IntPtr player, string url, string headers);
  322. [DllImport(PluginName)]
  323. internal static extern void AVPPlayerClose(IntPtr player);
  324. [DllImport(PluginName)]
  325. internal static extern int AVPPlayerGetAudio(IntPtr player, float[] buffer, int length);
  326. [DllImport(PluginName)]
  327. internal static extern void AVPPlayerSetRate(IntPtr player, float rate);
  328. [DllImport(PluginName)]
  329. internal static extern void AVPPlayerSetVolume(IntPtr player, float volume);
  330. [DllImport(PluginName)]
  331. internal static extern void AVPPlayerSetExternalPlaybackVideoGravity(IntPtr player, AVPPlayerExternalPlaybackVideoGravity gravity);
  332. [DllImport(PluginName)]
  333. internal static extern void AVPPlayerSeek(IntPtr player, double toTime, double toleranceBefore, double toleranceAfter);
  334. [DllImport(PluginName)]
  335. internal static extern void AVPPlayerSetKeyServerAuthToken(IntPtr player, string token);
  336. [DllImport(PluginName)]
  337. internal static extern void AVPPlayerSetKeyServerURL(IntPtr player, string url);
  338. [DllImport(PluginName)]
  339. internal static extern void AVPPlayerSetDecryptionKey(IntPtr player, byte[] key, int length);
  340. [DllImport(PluginName)]
  341. [return: MarshalAs(UnmanagedType.I1)]
  342. internal static extern bool AVPPlayerSetTrack(IntPtr player, AVPPlayerTrackType type, int index);
  343. #if !UNITY_EDITOR && UNITY_IOS
  344. public struct MediaCachingOptions
  345. {
  346. public double minimumRequiredBitRate;
  347. public float minimumRequiredResolution_width;
  348. public float minimumRequiredResolution_height;
  349. public string title;
  350. public IntPtr artwork;
  351. public int artworkLength;
  352. }
  353. [DllImport(PluginName)]
  354. public static extern void AVPPluginCacheMediaForURL(string url, string headers, MediaCachingOptions options);
  355. [DllImport(PluginName)]
  356. public static extern void AVPPluginCancelDownloadOfMediaForURL(string url);
  357. [DllImport(PluginName)]
  358. public static extern void AVPPluginRemoveCachedMediaForURL(string url);
  359. [DllImport(PluginName)]
  360. public static extern int AVPPluginGetCachedMediaStatusForURL(string url, ref float progress);
  361. #endif
  362. }
  363. }
  364. }
  365. #endif