Skip to content

Instantly share code, notes, and snippets.

@rolfbjarne
Created January 27, 2012 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rolfbjarne/b2a1263177cafd5c954b to your computer and use it in GitHub Desktop.
Save rolfbjarne/b2a1263177cafd5c954b to your computer and use it in GitHub Desktop.
commit b7fe66a7bf368a30b3de1675fc2b0a2fd69645ae
Author: Rolf Bjarne Kvinge <rolf@xamarin.com>
Date: Fri Jan 27 23:51:03 2012 +0100
Add a lot of CMTime utility functions. Fixes #2816.
diff --git a/src/CoreMedia/CoreMedia.cs b/src/CoreMedia/CoreMedia.cs
index 4298989..8398757 100644
--- a/src/CoreMedia/CoreMedia.cs
+++ b/src/CoreMedia/CoreMedia.cs
@@ -55,6 +55,14 @@ namespace MonoMac.CoreMedia {
TimeFlags = Flags.Valid;
TimeEpoch = 0;
}
+
+ public CMTime (long value, int timescale, long epoch)
+ {
+ Value = value;
+ TimeScale = timescale;
+ TimeFlags = Flags.Valid;
+ TimeEpoch = epoch;
+ }
public bool IsInvalid {
get {
@@ -79,8 +87,175 @@ namespace MonoMac.CoreMedia {
return (TimeFlags & kNegative) == kNegative;
}
}
-
- // TODO: kCMTimeValueKey, TimeScaleKey, TimeEpockKey, TimeFlagsKey
+
+ [DllImport(Constants.CoreMediaLibrary)]
+ extern static CMTime CMTimeAbsoluteValue (CMTime time);
+
+ public CMTime AbsoluteValue {
+ get {
+ return CMTimeAbsoluteValue (this);
+ }
+ }
+
+ [DllImport(Constants.CoreMediaLibrary)]
+ extern static int CMTimeCompare (CMTime time1, CMTime time2);
+ public static int Compare (ref CMTime time1, ref CMTime time2)
+ {
+ return CMTimeCompare (time1, time2);
+ }
+
+ public static bool operator == (CMTime time1, CMTime time2)
+ {
+ return Compare (ref time1, ref time2) == 0;
+ }
+
+ public static bool operator != (CMTime time1, CMTime time2)
+ {
+ return !(time1 == time2);
+ }
+
+ public override bool Equals (object obj)
+ {
+ if (!(obj is CMTime))
+ return false;
+
+ CMTime other = (CMTime) obj;
+ return other == this;
+ }
+
+ public override int GetHashCode ()
+ {
+ return Value.GetHashCode () ^ TimeScale.GetHashCode () ^ TimeFlags.GetHashCode () ^ TimeEpoch.GetHashCode ();
+ }
+
+ [DllImport(Constants.CoreMediaLibrary)]
+ extern static CMTime CMTimeAdd (CMTime addend1, CMTime addend2);
+ public static CMTime Add (ref CMTime time1, ref CMTime time2)
+ {
+ return CMTimeAdd (time1, time2);
+ }
+
+ [DllImport(Constants.CoreMediaLibrary)]
+ extern static CMTime CMTimeSubtract (CMTime minuend, CMTime subtraend);
+ public static CMTime Subtract (ref CMTime minuend, ref CMTime subtraend)
+ {
+ return CMTimeSubtract (minuend, subtraend);
+ }
+
+ [DllImport(Constants.CoreMediaLibrary)]
+ extern static CMTime CMTimeMultiply (CMTime time, int multiplier);
+ public static CMTime Multiply (ref CMTime time, int multiplier)
+ {
+ return CMTimeMultiply (time, multiplier);
+ }
+
+ [DllImport(Constants.CoreMediaLibrary)]
+ extern static CMTime CMTimeMultiplyByFloat64 (CMTime time, double multiplier);
+ public static CMTime Multiply (ref CMTime time, double multiplier)
+ {
+ return CMTimeMultiplyByFloat64 (time, multiplier);
+ }
+
+ public static CMTime operator + (CMTime time1, CMTime time2)
+ {
+ return Add (ref time1, ref time2);
+ }
+
+ public static CMTime operator - (CMTime minuend, CMTime subtraend)
+ {
+ return Subtract (ref minuend, ref subtraend);
+ }
+
+ public static CMTime operator * (CMTime time, int multiplier)
+ {
+ return Multiply (ref time, multiplier);
+ }
+
+ public static CMTime operator * (CMTime time, double multiplier)
+ {
+ return Multiply (ref time, multiplier);
+ }
+
+ [DllImport(Constants.CoreMediaLibrary)]
+ extern static CMTime CMTimeConvertScale (CMTime time, int newScale, CMTimeRoundingMethod method);
+ public CMTime ConvertScale (int newScale, CMTimeRoundingMethod method)
+ {
+ return CMTimeConvertScale (this, newScale, method);
+ }
+
+ [DllImport(Constants.CoreMediaLibrary)]
+ extern static double CMTimeGetSeconds (CMTime time);
+ public double Seconds {
+ get {
+ return CMTimeGetSeconds (this);
+ }
+ }
+
+ [DllImport(Constants.CoreMediaLibrary)]
+ extern static CMTime CMTimeMakeWithSeconds (double seconds, int preferredTimeScale);
+ public static CMTime FromSeconds (double seconds, int preferredTimeScale)
+ {
+ return CMTimeMakeWithSeconds (seconds, preferredTimeScale);
+ }
+
+ [DllImport(Constants.CoreMediaLibrary)]
+ extern static CMTime CMTimeMaximum (CMTime time1, CMTime time2);
+ public static CMTime GetMaximum (CMTime time1, CMTime time2)
+ {
+ return CMTimeMaximum (time1, time2);
+ }
+
+ [DllImport(Constants.CoreMediaLibrary)]
+ extern static CMTime CMTimeMinimum (CMTime time1, CMTime time2);
+ public static CMTime GetMinimum (CMTime time1, CMTime time2)
+ {
+ return CMTimeMinimum (time1, time2);
+ }
+
+ public readonly static NSString ValueKey;
+ public readonly static NSString ScaleKey;
+ public readonly static NSString EpochKey;
+ public readonly static NSString FlagsKey;
+
+ static CMTime ()
+ {
+ var lib = Dlfcn.dlopen (Constants.CoreMediaLibrary, 0);
+ if (lib != IntPtr.Zero) {
+ try {
+ ValueKey = Dlfcn.GetStringConstant (lib, "kCMTimeValueKey");
+ ScaleKey = Dlfcn.GetStringConstant (lib, "kCMTimeScaleKey");
+ EpochKey = Dlfcn.GetStringConstant (lib, "kCMTimeEpochKey");
+ FlagsKey = Dlfcn.GetStringConstant (lib, "kCMTimeFlagsKey");
+ } finally {
+ Dlfcn.dlclose (lib);
+ }
+ }
+ }
+
+ [DllImport(Constants.CoreMediaLibrary)]
+ extern static IntPtr CMTimeCopyAsDictionary (CMTime time, IntPtr allocator);
+ public IntPtr AsDictionary {
+ get {
+ return CMTimeCopyAsDictionary (this, IntPtr.Zero);
+ }
+ }
+
+ [DllImport(Constants.CoreMediaLibrary)]
+ extern static IntPtr CMTimeCopyDescription (IntPtr allocator, CMTime time);
+ public string Description {
+ get {
+ return NSString.FromHandle (CMTimeCopyDescription (IntPtr.Zero, this)).ToString ();
+ }
+ }
+
+ [DllImport(Constants.CoreMediaLibrary)]
+ extern static CMTime CMTimeMakeFromDictionary (IntPtr dict);
+ public static CMTime FromDictionary (IntPtr dict)
+ {
+ return CMTimeMakeFromDictionary (dict);
+ }
+
+ // Should we also bind CMTimeShow?
}
public enum CMMediaType : uint
@@ -95,6 +270,17 @@ namespace MonoMac.CoreMedia {
TimedMetadata = 1953326452, // 'tmet'
}
+ public enum CMTimeRoundingMethod
+ {
+ RoundHalfAwayFromZero = 1,
+ RoundTowardZero = 2,
+ RoundAwayFromZero = 3,
+ QuickTime = 4,
+ RoundTowardPositiveInfinity = 5,
+ RoundTowardNegativeInfinity = 6,
+ Default = RoundHalfAwayFromZero,
+ }
+
[StructLayout(LayoutKind.Sequential)]
struct CMSampleTimingInfo
{
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment