2006-05-19 19:24:35 +00:00
|
|
|
//
|
2006-05-20 22:35:40 +00:00
|
|
|
// Application.cs: Framework initialization for GStreamer
|
2006-05-19 19:24:35 +00:00
|
|
|
//
|
2006-05-20 22:35:40 +00:00
|
|
|
// Authors:
|
|
|
|
// Aaron Bockover (abockover@novell.com)
|
|
|
|
// Alp Toker (alp@atoker.com)
|
2006-05-19 19:24:35 +00:00
|
|
|
//
|
2009-04-08 08:16:48 +00:00
|
|
|
// Copyright (C) 2002 Alp Toker
|
|
|
|
// Copyright (C) 2006 Novell, Inc.
|
2009-05-13 12:35:59 +00:00
|
|
|
// Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
|
2006-05-19 19:24:35 +00:00
|
|
|
//
|
|
|
|
|
2006-05-20 22:35:40 +00:00
|
|
|
using System;
|
2009-05-13 12:35:59 +00:00
|
|
|
using System.Reflection;
|
|
|
|
using System.IO;
|
2006-05-20 22:35:40 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2009-12-24 16:59:18 +00:00
|
|
|
using System.Collections.Generic;
|
2006-05-20 22:35:40 +00:00
|
|
|
|
2009-04-07 09:27:20 +00:00
|
|
|
namespace Gst {
|
2009-05-13 12:35:59 +00:00
|
|
|
|
|
|
|
[AttributeUsage (AttributeTargets.Enum | AttributeTargets.Class | AttributeTargets.Struct) ]
|
|
|
|
public sealed class GTypeNameAttribute : Attribute {
|
|
|
|
string type_name;
|
|
|
|
|
|
|
|
public GTypeNameAttribute (string gtype_name) {
|
|
|
|
this.type_name = gtype_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string TypeName {
|
|
|
|
get {
|
|
|
|
return type_name;
|
2009-09-20 08:19:49 +00:00
|
|
|
} set {
|
2009-05-13 12:35:59 +00:00
|
|
|
type_name = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-07 09:27:20 +00:00
|
|
|
public static class Application {
|
|
|
|
public static void Init() {
|
|
|
|
IntPtr argv = new IntPtr (0);
|
|
|
|
int argc = 0;
|
2006-05-20 22:35:40 +00:00
|
|
|
|
2009-04-07 09:27:20 +00:00
|
|
|
gst_init (ref argc, ref argv);
|
2009-05-31 19:10:45 +00:00
|
|
|
gst_controller_init (ref argc, ref argv);
|
2009-10-03 07:29:45 +00:00
|
|
|
gst_pb_utils_init ();
|
2009-04-21 17:27:05 +00:00
|
|
|
RegisterManagedTypes ();
|
2009-04-07 09:27:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void Init (string progname, ref string [] args) {
|
|
|
|
FullInit (progname, ref args, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void InitCheck (string progname, ref string [] args) {
|
|
|
|
FullInit (progname, ref args, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Deinit() {
|
|
|
|
gst_deinit();
|
|
|
|
}
|
|
|
|
|
2009-12-24 22:30:50 +00:00
|
|
|
private static Dictionary<int,bool> AssemblyTypesInCache = new Dictionary<int,bool> ();
|
|
|
|
private static Dictionary<string,Type> TypeCache = new Dictionary<string,Type> ();
|
2009-12-24 16:59:18 +00:00
|
|
|
|
2009-12-24 22:30:50 +00:00
|
|
|
// Recursively check for types with GTypeNameAttribute and put them in TypeCache,
|
|
|
|
// but only gstreamer-sharp is in the chain of referenced assemblies.
|
|
|
|
private static bool PutAssemblyTypesInCache (Assembly asm)
|
2009-12-24 16:59:18 +00:00
|
|
|
{
|
2009-12-24 22:30:50 +00:00
|
|
|
bool result;
|
2009-12-24 16:59:18 +00:00
|
|
|
|
|
|
|
// If already visited, return immediately
|
2009-12-24 22:30:50 +00:00
|
|
|
if (AssemblyTypesInCache.TryGetValue(asm.GetHashCode (), out result))
|
|
|
|
return result;
|
2009-12-24 16:59:18 +00:00
|
|
|
|
2009-12-24 22:30:50 +00:00
|
|
|
result = false;
|
|
|
|
AssemblyTypesInCache.Add (asm.GetHashCode (), false);
|
2009-12-24 16:59:18 +00:00
|
|
|
|
|
|
|
// Result is true for gstreamer-sharp or if a referenced assembly results in true
|
2009-12-24 22:30:50 +00:00
|
|
|
if (asm.GetName().Name == "gstreamer-sharp")
|
2009-12-24 16:59:18 +00:00
|
|
|
result = true;
|
|
|
|
foreach (AssemblyName ref_name in asm.GetReferencedAssemblies ()) {
|
|
|
|
try {
|
2009-12-24 22:30:50 +00:00
|
|
|
result = result | PutAssemblyTypesInCache (Assembly.Load (ref_name));
|
2009-12-24 16:59:18 +00:00
|
|
|
} catch {
|
|
|
|
/* Failure to load a referenced assembly is not an error */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-24 22:30:50 +00:00
|
|
|
// Add types with GTypeNameAttribute in TypeCache
|
|
|
|
if (result) {
|
|
|
|
AssemblyTypesInCache[asm.GetHashCode ()] = true;
|
2009-12-26 11:23:48 +00:00
|
|
|
Type[] ts;
|
|
|
|
try {
|
|
|
|
ts = asm.GetTypes ();
|
|
|
|
} catch (ReflectionTypeLoadException e) {
|
|
|
|
ts = e.Types;
|
|
|
|
}
|
2009-12-24 22:30:50 +00:00
|
|
|
foreach (Type t in ts) {
|
2009-12-26 11:23:48 +00:00
|
|
|
if (t != null && t.IsDefined (typeof (GTypeNameAttribute), false)) {
|
2009-12-24 22:30:50 +00:00
|
|
|
GTypeNameAttribute gattr = (GTypeNameAttribute) Attribute.GetCustomAttribute (t, typeof (GTypeNameAttribute), false);
|
|
|
|
TypeCache[gattr.TypeName] = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-24 16:59:18 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2009-08-05 14:57:20 +00:00
|
|
|
private static System.Type GstResolveType (Gst.GLib.GType gtype, string gtype_name) {
|
2009-12-24 22:30:50 +00:00
|
|
|
// Make sure all loaded assemblies are in the TypeCache
|
2009-05-13 12:35:59 +00:00
|
|
|
Assembly[] assemblies = (Assembly[]) AppDomain.CurrentDomain.GetAssemblies ().Clone ();
|
|
|
|
foreach (Assembly asm in assemblies) {
|
2009-12-24 22:30:50 +00:00
|
|
|
PutAssemblyTypesInCache (asm);
|
2009-05-13 12:35:59 +00:00
|
|
|
}
|
|
|
|
|
2009-12-24 22:30:50 +00:00
|
|
|
// Return the managed type
|
|
|
|
if (TypeCache.ContainsKey (gtype_name))
|
|
|
|
return TypeCache[gtype_name];
|
2009-05-13 12:35:59 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2009-04-21 17:27:05 +00:00
|
|
|
private static void RegisterManagedTypes() {
|
2009-08-05 14:57:20 +00:00
|
|
|
Gst.GLib.GType.ResolveType += GstResolveType;
|
|
|
|
|
|
|
|
Gst.GLib.GType.Register (Fraction.GType, typeof (Fraction));
|
|
|
|
Gst.GLib.GType.Register (IntRange.GType, typeof (IntRange));
|
|
|
|
Gst.GLib.GType.Register (DoubleRange.GType, typeof (DoubleRange));
|
|
|
|
Gst.GLib.GType.Register (FractionRange.GType, typeof (FractionRange));
|
|
|
|
Gst.GLib.GType.Register (Fourcc.GType, typeof (Fourcc));
|
|
|
|
Gst.GLib.GType.Register (Date.GType, typeof (Date));
|
|
|
|
Gst.GLib.GType.Register (List.GType, typeof (List));
|
|
|
|
Gst.GLib.GType.Register (Array.GType, typeof (Array));
|
|
|
|
Gst.GLib.GType.Register (Caps.GType, typeof (Caps));
|
|
|
|
Gst.GLib.GType.Register (Structure.GType, typeof (Structure));
|
|
|
|
Gst.GLib.GType.Register (TagList.GType, typeof (TagList));
|
|
|
|
Gst.GLib.GType.Register (MiniObject.GType, typeof (MiniObject));
|
2009-12-24 11:53:51 +00:00
|
|
|
Gst.GLib.GType.Register (Bus.GType, typeof (Bus));
|
|
|
|
Gst.GLib.GType.Register (Pad.GType, typeof (Pad));
|
|
|
|
Gst.GLib.GType.Register (GhostPad.GType, typeof (GhostPad));
|
|
|
|
Gst.GLib.GType.Register (Message.GType, typeof (Message));
|
|
|
|
Gst.GLib.GType.Register (SystemClock.GType, typeof (SystemClock));
|
2009-05-08 15:23:55 +00:00
|
|
|
|
|
|
|
GtkSharp.GstreamerSharp.ObjectManager.Initialize ();
|
2009-04-21 17:27:05 +00:00
|
|
|
}
|
|
|
|
|
2009-04-07 09:27:20 +00:00
|
|
|
private static void FullInit (string progname, ref string [] args, bool check) {
|
|
|
|
string [] progargs = new string[args.Length + 1];
|
|
|
|
|
|
|
|
progargs[0] = progname;
|
|
|
|
args.CopyTo (progargs, 1);
|
|
|
|
|
2009-08-05 14:57:20 +00:00
|
|
|
Gst.GLib.Argv argv = new Gst.GLib.Argv (progargs);
|
2009-04-07 09:27:20 +00:00
|
|
|
IntPtr argv_ptr = argv.Handle;
|
|
|
|
int argc = progargs.Length;
|
|
|
|
|
|
|
|
if (check) {
|
|
|
|
IntPtr error_ptr;
|
|
|
|
bool result = gst_init_check (ref argc, ref argv_ptr, out error_ptr);
|
|
|
|
|
|
|
|
if (error_ptr != IntPtr.Zero) {
|
2009-08-05 14:57:20 +00:00
|
|
|
throw new Gst.GLib.GException (error_ptr);
|
2009-04-07 09:27:20 +00:00
|
|
|
} else if (!result) {
|
|
|
|
throw new ApplicationException ("gst_init_check() failed: Reason unknown");
|
2006-05-20 22:35:40 +00:00
|
|
|
}
|
2009-04-07 09:27:20 +00:00
|
|
|
} else {
|
|
|
|
gst_init (ref argc, ref argv_ptr);
|
|
|
|
}
|
2009-04-07 09:02:08 +00:00
|
|
|
|
2009-04-07 09:27:20 +00:00
|
|
|
if (argv_ptr != argv.Handle) {
|
|
|
|
string init_call = check ? "gst_init_check()" : "gst_init()";
|
|
|
|
throw new ApplicationException (init_call + " returned a new argv handle");
|
|
|
|
}
|
2006-05-20 22:35:40 +00:00
|
|
|
|
2009-05-31 19:10:45 +00:00
|
|
|
gst_controller_init (ref argc, ref argv_ptr);
|
2009-10-03 07:29:45 +00:00
|
|
|
gst_pb_utils_init ();
|
2009-05-31 19:10:45 +00:00
|
|
|
|
2009-04-07 09:27:20 +00:00
|
|
|
if (argc <= 1) {
|
|
|
|
args = new string[0];
|
|
|
|
} else {
|
|
|
|
progargs = argv.GetArgs (argc);
|
|
|
|
args = new string[argc - 1];
|
2009-04-11 15:52:50 +00:00
|
|
|
System.Array.Copy (progargs, 1, args, 0, argc - 1);
|
2009-04-07 09:27:20 +00:00
|
|
|
}
|
2009-04-21 17:27:05 +00:00
|
|
|
RegisterManagedTypes ();
|
2006-05-20 22:35:40 +00:00
|
|
|
}
|
2009-04-07 09:27:20 +00:00
|
|
|
|
2009-09-20 08:19:49 +00:00
|
|
|
[DllImport ("libgstreamer-0.10.dll") ]
|
2009-04-07 09:27:20 +00:00
|
|
|
private static extern void gst_init (ref int argc, ref IntPtr argv);
|
|
|
|
|
2009-09-20 08:19:49 +00:00
|
|
|
[DllImport ("libgstreamer-0.10.dll") ]
|
2009-04-07 09:27:20 +00:00
|
|
|
private static extern bool gst_init_check (ref int argc, ref IntPtr argv, out IntPtr error);
|
|
|
|
|
2009-09-20 08:19:49 +00:00
|
|
|
[DllImport ("libgstcontroller-0.10.dll") ]
|
2009-05-31 19:10:45 +00:00
|
|
|
private static extern void gst_controller_init (ref int argc, ref IntPtr argv);
|
|
|
|
|
2009-10-03 07:29:45 +00:00
|
|
|
[DllImport ("libgstpbutils-0.10.dll") ]
|
|
|
|
private static extern void gst_pb_utils_init ();
|
|
|
|
|
2009-09-20 08:19:49 +00:00
|
|
|
[DllImport ("libgstreamer-0.10.dll") ]
|
2009-04-07 09:27:20 +00:00
|
|
|
private static extern void gst_deinit();
|
|
|
|
}
|
2006-05-19 19:24:35 +00:00
|
|
|
}
|