PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/CoreMedia/CMFormatDescription.cs

https://github.com/kjpou1/maccore
C# | 428 lines | 335 code | 73 blank | 20 comment | 40 complexity | 0fb2cd0b76696720eb4d709b88aa4289 MD5 | raw file
Possible License(s): Apache-2.0
  1. //
  2. // CMFormatDescription.cs: Implements the managed CMFormatDescription
  3. //
  4. // Authors:
  5. // Miguel de Icaza (miguel@xamarin.com)
  6. // Frank Krueger
  7. // Mono Team
  8. // Marek Safar (marek.safar@gmail.com)
  9. //
  10. // Copyright 2010 Novell, Inc
  11. // Copyright 2012 Xamarin Inc
  12. //
  13. using System;
  14. using System.Runtime.InteropServices;
  15. using System.Drawing;
  16. using MonoMac;
  17. using MonoMac.Foundation;
  18. using MonoMac.CoreFoundation;
  19. using MonoMac.ObjCRuntime;
  20. using MonoMac.CoreVideo;
  21. using MonoMac.AudioToolbox;
  22. namespace MonoMac.CoreMedia {
  23. public enum CMFormatDescriptionError {
  24. None = 0,
  25. InvalidParameter = -12710,
  26. AllocationFailed = -12711,
  27. }
  28. [Since (4,0)]
  29. public class CMFormatDescription : INativeObject, IDisposable {
  30. internal IntPtr handle;
  31. internal CMFormatDescription (IntPtr handle)
  32. {
  33. this.handle = handle;
  34. }
  35. [Preserve (Conditional=true)]
  36. internal CMFormatDescription (IntPtr handle, bool owns)
  37. {
  38. if (!owns)
  39. CFObject.CFRetain (handle);
  40. this.handle = handle;
  41. }
  42. ~CMFormatDescription ()
  43. {
  44. Dispose (false);
  45. }
  46. public void Dispose ()
  47. {
  48. Dispose (true);
  49. GC.SuppressFinalize (this);
  50. }
  51. public IntPtr Handle {
  52. get { return handle; }
  53. }
  54. protected virtual void Dispose (bool disposing)
  55. {
  56. if (handle != IntPtr.Zero){
  57. CFObject.CFRelease (handle);
  58. handle = IntPtr.Zero;
  59. }
  60. }
  61. /*[DllImport(Constants.CoreMediaLibrary)]
  62. extern static CFPropertyListRef CMFormatDescriptionGetExtension (
  63. CMFormatDescriptionRef desc,
  64. CFStringRef extensionKey
  65. );*/
  66. [DllImport(Constants.CoreMediaLibrary)]
  67. extern static IntPtr CMFormatDescriptionGetExtensions (IntPtr handle);
  68. #if !COREBUILD
  69. public NSDictionary GetExtensions ()
  70. {
  71. var cfDictRef = CMFormatDescriptionGetExtensions (handle);
  72. if (cfDictRef == IntPtr.Zero)
  73. {
  74. return null;
  75. }
  76. else
  77. {
  78. return (NSDictionary) Runtime.GetNSObject (cfDictRef);
  79. }
  80. }
  81. #endif
  82. [DllImport(Constants.CoreMediaLibrary)]
  83. extern static uint CMFormatDescriptionGetMediaSubType (IntPtr handle);
  84. public uint MediaSubType
  85. {
  86. get
  87. {
  88. return CMFormatDescriptionGetMediaSubType (handle);
  89. }
  90. }
  91. public AudioFormatType AudioFormatType {
  92. get {
  93. return MediaType == CMMediaType.Audio ? (AudioFormatType) MediaSubType : 0;
  94. }
  95. }
  96. public CMSubtitleFormatType SubtitleFormatType {
  97. get {
  98. return MediaType == CMMediaType.Subtitle ? (CMSubtitleFormatType) MediaSubType : 0;
  99. }
  100. }
  101. public CMClosedCaptionFormatType ClosedCaptionFormatType {
  102. get {
  103. return MediaType == CMMediaType.ClosedCaption ? (CMClosedCaptionFormatType) MediaSubType : 0;
  104. }
  105. }
  106. public CMMuxedStreamType MuxedStreamType {
  107. get {
  108. return MediaType == CMMediaType.Muxed ? (CMMuxedStreamType) MediaSubType : 0;
  109. }
  110. }
  111. public CMVideoCodecType VideoCodecType {
  112. get {
  113. return MediaType == CMMediaType.Video ? (CMVideoCodecType) MediaSubType : 0;
  114. }
  115. }
  116. public CMMetadataFormatType MetadataFormatType {
  117. get {
  118. return MediaType == CMMediaType.Metadata ? (CMMetadataFormatType) MediaSubType : 0;
  119. }
  120. }
  121. public CMTimeCodeFormatType TimeCodeFormatType {
  122. get {
  123. return MediaType == CMMediaType.TimeCode ? (CMTimeCodeFormatType) MediaSubType : 0;
  124. }
  125. }
  126. [DllImport(Constants.CoreMediaLibrary)]
  127. extern static CMMediaType CMFormatDescriptionGetMediaType (IntPtr handle);
  128. public CMMediaType MediaType
  129. {
  130. get
  131. {
  132. return CMFormatDescriptionGetMediaType (handle);
  133. }
  134. }
  135. [DllImport(Constants.CoreMediaLibrary)]
  136. extern static int CMFormatDescriptionGetTypeID ();
  137. public static int GetTypeID ()
  138. {
  139. return CMFormatDescriptionGetTypeID ();
  140. }
  141. #if !COREBUILD
  142. [DllImport (Constants.CoreMediaLibrary)]
  143. extern static CMFormatDescriptionError CMFormatDescriptionCreate (IntPtr allocator, CMMediaType mediaType, uint mediaSubtype, IntPtr extensions, out IntPtr handle);
  144. public static CMFormatDescription Create (CMMediaType mediaType, uint mediaSubtype, out CMFormatDescriptionError error)
  145. {
  146. IntPtr handle;
  147. error = CMFormatDescriptionCreate (IntPtr.Zero, mediaType, mediaSubtype, IntPtr.Zero, out handle);
  148. if (error != CMFormatDescriptionError.None)
  149. return null;
  150. return Create (mediaType, handle, true);
  151. }
  152. public static CMFormatDescription Create (IntPtr handle, bool owns)
  153. {
  154. return Create (CMFormatDescriptionGetMediaType (handle), handle, owns);
  155. }
  156. static CMFormatDescription Create (CMMediaType type, IntPtr handle, bool owns)
  157. {
  158. switch (type) {
  159. case CMMediaType.Video:
  160. return new CMVideoFormatDescription (handle);
  161. case CMMediaType.Audio:
  162. return new CMAudioFormatDescription (handle);
  163. default:
  164. return new CMFormatDescription (handle);
  165. }
  166. }
  167. [DllImport (Constants.CoreMediaLibrary)]
  168. extern static IntPtr CMAudioFormatDescriptionGetStreamBasicDescription (IntPtr handle);
  169. public AudioStreamBasicDescription? AudioStreamBasicDescription {
  170. get {
  171. var ret = CMAudioFormatDescriptionGetStreamBasicDescription (handle);
  172. if (ret != IntPtr.Zero){
  173. unsafe {
  174. return *((AudioStreamBasicDescription *) ret);
  175. }
  176. }
  177. return null;
  178. }
  179. }
  180. [DllImport (Constants.CoreMediaLibrary)]
  181. extern static IntPtr CMAudioFormatDescriptionGetChannelLayout (IntPtr handle, out int size);
  182. public AudioChannelLayout AudioChannelLayout {
  183. get {
  184. int size;
  185. var res = CMAudioFormatDescriptionGetChannelLayout (handle, out size);
  186. if (res == IntPtr.Zero || size == 0)
  187. return null;
  188. return AudioChannelLayout.FromHandle (res);
  189. }
  190. }
  191. [DllImport (Constants.CoreMediaLibrary)]
  192. extern static IntPtr CMAudioFormatDescriptionGetFormatList (IntPtr handle, out int size);
  193. public AudioFormat [] AudioFormats {
  194. get {
  195. unsafe {
  196. int size;
  197. var v = CMAudioFormatDescriptionGetFormatList (handle, out size);
  198. if (v == IntPtr.Zero)
  199. return null;
  200. var items = size / sizeof (AudioFormat);
  201. var ret = new AudioFormat [items];
  202. var ptr = (AudioFormat *) v;
  203. for (int i = 0; i < items; i++)
  204. ret [i] = ptr [i];
  205. return ret;
  206. }
  207. }
  208. }
  209. [DllImport (Constants.CoreMediaLibrary)]
  210. extern static IntPtr CMAudioFormatDescriptionGetMagicCookie (IntPtr handle, out int size);
  211. public byte [] AudioMagicCookie {
  212. get {
  213. int size;
  214. var h = CMAudioFormatDescriptionGetMagicCookie (handle, out size);
  215. if (h == IntPtr.Zero)
  216. return null;
  217. var result = new byte [size];
  218. for (int i = 0; i < size; i++)
  219. result [i] = Marshal.ReadByte (h, i);
  220. return result;
  221. }
  222. }
  223. [DllImport (Constants.CoreMediaLibrary)]
  224. extern static IntPtr CMAudioFormatDescriptionGetMostCompatibleFormat (IntPtr handle);
  225. public AudioFormat AudioMostCompatibleFormat {
  226. get {
  227. unsafe {
  228. var ret = (AudioFormat *) CMAudioFormatDescriptionGetMostCompatibleFormat (handle);
  229. if (ret == null)
  230. return new AudioFormat ();
  231. return *ret;
  232. }
  233. }
  234. }
  235. [DllImport (Constants.CoreMediaLibrary)]
  236. extern static IntPtr CMAudioFormatDescriptionGetRichestDecodableFormat (IntPtr handle);
  237. public AudioFormat AudioRichestDecodableFormat {
  238. get {
  239. unsafe {
  240. var ret = (AudioFormat *) CMAudioFormatDescriptionGetRichestDecodableFormat (handle);
  241. if (ret == null)
  242. return new AudioFormat ();
  243. return *ret;
  244. }
  245. }
  246. }
  247. [DllImport (Constants.CoreMediaLibrary)]
  248. internal extern static Size CMVideoFormatDescriptionGetDimensions (IntPtr handle);
  249. [Advice ("Use CMVideoFormatDescription")]
  250. public Size VideoDimensions {
  251. get {
  252. return CMVideoFormatDescriptionGetDimensions (handle);
  253. }
  254. }
  255. [DllImport (Constants.CoreMediaLibrary)]
  256. internal extern static RectangleF CMVideoFormatDescriptionGetCleanAperture (IntPtr handle, bool originIsAtTopLeft);
  257. [Advice ("Use CMVideoFormatDescription")]
  258. public RectangleF GetVideoCleanAperture (bool originIsAtTopLeft)
  259. {
  260. return CMVideoFormatDescriptionGetCleanAperture (handle, originIsAtTopLeft);
  261. }
  262. [DllImport (Constants.CoreMediaLibrary)]
  263. extern static IntPtr CMVideoFormatDescriptionGetExtensionKeysCommonWithImageBuffers ();
  264. // Belongs to CMVideoFormatDescription
  265. public static NSObject [] GetExtensionKeysCommonWithImageBuffers ()
  266. {
  267. var arr = CMVideoFormatDescriptionGetExtensionKeysCommonWithImageBuffers ();
  268. return NSArray.ArrayFromHandle<NSString> (arr);
  269. }
  270. [DllImport (Constants.CoreMediaLibrary)]
  271. internal extern static SizeF CMVideoFormatDescriptionGetPresentationDimensions (IntPtr handle, bool usePixelAspectRatio, bool useCleanAperture);
  272. [Advice ("Use CMVideoFormatDescription")]
  273. public SizeF GetVideoPresentationDimensions (bool usePixelAspectRatio, bool useCleanAperture)
  274. {
  275. return CMVideoFormatDescriptionGetPresentationDimensions (handle, usePixelAspectRatio, useCleanAperture);
  276. }
  277. [DllImport (Constants.CoreMediaLibrary)]
  278. extern static int CMVideoFormatDescriptionMatchesImageBuffer (IntPtr handle, IntPtr imageBufferRef);
  279. // Belongs to CMVideoFormatDescription
  280. public bool VideoMatchesImageBuffer (CVImageBuffer imageBuffer)
  281. {
  282. if (imageBuffer == null)
  283. throw new ArgumentNullException ("imageBuffer");
  284. return CMVideoFormatDescriptionMatchesImageBuffer (handle, imageBuffer.Handle) != 0;
  285. }
  286. #endif
  287. }
  288. [Since (4,0)]
  289. public class CMAudioFormatDescription : CMFormatDescription {
  290. internal CMAudioFormatDescription (IntPtr handle)
  291. : base (handle)
  292. {
  293. }
  294. internal CMAudioFormatDescription (IntPtr handle, bool owns)
  295. : base (handle, owns)
  296. {
  297. }
  298. // TODO: Move more audio specific methods here
  299. }
  300. [Since (4,0)]
  301. public class CMVideoFormatDescription : CMFormatDescription {
  302. internal CMVideoFormatDescription (IntPtr handle)
  303. : base (handle)
  304. {
  305. }
  306. internal CMVideoFormatDescription (IntPtr handle, bool owns)
  307. : base (handle, owns)
  308. {
  309. }
  310. [DllImport (Constants.CoreMediaLibrary)]
  311. static extern CMFormatDescriptionError CMVideoFormatDescriptionCreate (IntPtr allocator,
  312. CMVideoCodecType codecType,
  313. int width, int height,
  314. IntPtr extensions,
  315. out IntPtr outDesc);
  316. public CMVideoFormatDescription (CMVideoCodecType codecType, Size size)
  317. : base (IntPtr.Zero)
  318. {
  319. var error = CMVideoFormatDescriptionCreate (IntPtr.Zero, codecType, size.Width, size.Height, IntPtr.Zero, out handle);
  320. if (error != CMFormatDescriptionError.None)
  321. throw new ArgumentException (error.ToString ());
  322. }
  323. #if !COREBUILD
  324. public Size Dimensions {
  325. get {
  326. return CMVideoFormatDescriptionGetDimensions (handle);
  327. }
  328. }
  329. [DllImport (Constants.CoreMediaLibrary)]
  330. static extern CMFormatDescriptionError CMVideoFormatDescriptionCreateForImageBuffer (IntPtr allocator,
  331. IntPtr imageBuffer,
  332. out IntPtr outDesc);
  333. public static CMVideoFormatDescription CreateForImageBuffer (CVImageBuffer imageBuffer, out CMFormatDescriptionError error)
  334. {
  335. if (imageBuffer == null)
  336. throw new ArgumentNullException ("imageBuffer");
  337. IntPtr desc;
  338. error = CMVideoFormatDescriptionCreateForImageBuffer (IntPtr.Zero, imageBuffer.handle, out desc);
  339. if (error != CMFormatDescriptionError.None)
  340. return null;
  341. return new CMVideoFormatDescription (desc, true);
  342. }
  343. [DllImport (Constants.CoreMediaLibrary)]
  344. extern static RectangleF CMVideoFormatDescriptionGetCleanAperture (IntPtr handle, bool originIsAtTopLeft);
  345. public RectangleF GetCleanAperture (bool originIsAtTopLeft)
  346. {
  347. return CMVideoFormatDescriptionGetCleanAperture (handle, originIsAtTopLeft);
  348. }
  349. public SizeF GetPresentationDimensions (bool usePixelAspectRatio, bool useCleanAperture)
  350. {
  351. return CMVideoFormatDescriptionGetPresentationDimensions (handle, usePixelAspectRatio, useCleanAperture);
  352. }
  353. #endif
  354. }
  355. }