Add a generic GstIterator wrapper and use that

This commit is contained in:
Sebastian Dröge 2009-05-09 19:29:40 +02:00
parent 5436b22683
commit 4338c97169
5 changed files with 101 additions and 249 deletions

View file

@ -51,12 +51,6 @@ public Gst.Element GetByInterface (System.Type type) {
return GetByInterface (t);
}
[DllImport ("gstreamer-0.10.dll") ]
static extern int gst_iterator_next (IntPtr iterator, out IntPtr elem);
[DllImport ("gstreamer-0.10.dll") ]
static extern void gst_iterator_resync (IntPtr iterator);
[DllImport ("gstreamer-0.10.dll") ]
static extern void gst_iterator_free (IntPtr iterator);
[DllImport ("gstreamer-0.10.dll") ]
static extern IntPtr gst_bin_iterate_elements (IntPtr bin);
[DllImport ("gstreamer-0.10.dll") ]
@ -70,112 +64,38 @@ static extern IntPtr gst_bin_iterate_sources (IntPtr bin);
[DllImport ("gstreamer-0.10.dll") ]
static extern IntPtr gst_bin_iterate_all_by_interface (IntPtr bin, IntPtr gtype);
private class ElementEnumerable : IEnumerable {
private class ElementEnumerator : IEnumerator {
Hashtable seen = new Hashtable ();
IntPtr iterator;
private object current = null;
public object Current {
get {
return current;
}
}
public bool MoveNext () {
IntPtr raw_ret;
bool retry = false;
if (iterator == IntPtr.Zero)
return false;
do {
int ret = gst_iterator_next (iterator, out raw_ret);
switch (ret) {
case 0:
return false;
case 1:
if (seen.Contains (raw_ret)) {
retry = true;
break;
}
seen.Add (raw_ret, null);
current = GLib.Object.GetObject (raw_ret, true);
return true;
case 2:
gst_iterator_resync (iterator);
break;
default:
case 3:
throw new Exception ("Error while iterating bin");
}
} while (retry);
return false;
}
public void Reset () {
seen.Clear ();
if (iterator != IntPtr.Zero)
gst_iterator_resync (iterator);
}
public ElementEnumerator (IntPtr iterator) {
this.iterator = iterator;
}
}
private IntPtr iterator;
private ElementEnumerator enumerator;
public ElementEnumerable (IntPtr iterator) {
this.iterator = iterator;
this.enumerator = new ElementEnumerator (iterator);
}
public IEnumerator GetEnumerator () {
return enumerator;
}
~ElementEnumerable () {
if (iterator != IntPtr.Zero)
gst_iterator_free (iterator);
}
}
public IEnumerable Elements {
get {
return new ElementEnumerable (gst_bin_iterate_elements (Handle));
return new Enumerable (gst_bin_iterate_elements (Handle));
}
}
public IEnumerable ElementsRecurse {
get {
return new ElementEnumerable (gst_bin_iterate_recurse (Handle));
return new Enumerable (gst_bin_iterate_recurse (Handle));
}
}
public IEnumerable ElementsSorted {
get {
return new ElementEnumerable (gst_bin_iterate_sorted (Handle));
return new Enumerable (gst_bin_iterate_sorted (Handle));
}
}
public IEnumerable SinkElements {
get {
return new ElementEnumerable (gst_bin_iterate_sinks (Handle));
return new Enumerable (gst_bin_iterate_sinks (Handle));
}
}
public IEnumerable SourceElements {
get {
return new ElementEnumerable (gst_bin_iterate_sources (Handle));
return new Enumerable (gst_bin_iterate_sources (Handle));
}
}
public IEnumerable GetAllByInterface (GLib.GType type) {
return new ElementEnumerable (gst_bin_iterate_all_by_interface (Handle, type.Val));
return new Enumerable (gst_bin_iterate_all_by_interface (Handle, type.Val));
}
public IEnumerable GetAllByInterface (System.Type type) {

View file

@ -25,12 +25,6 @@ public static void UnlinkMany (params Element [] elements) {
}
}
[DllImport ("gstreamer-0.10.dll") ]
static extern int gst_iterator_next (IntPtr iterator, out IntPtr elem);
[DllImport ("gstreamer-0.10.dll") ]
static extern void gst_iterator_resync (IntPtr iterator);
[DllImport ("gstreamer-0.10.dll") ]
static extern void gst_iterator_free (IntPtr iterator);
[DllImport ("gstreamer-0.10.dll") ]
static extern IntPtr gst_element_iterate_pads (IntPtr element);
[DllImport ("gstreamer-0.10.dll") ]
@ -38,95 +32,21 @@ static extern IntPtr gst_element_iterate_src_pads (IntPtr element);
[DllImport ("gstreamer-0.10.dll") ]
static extern IntPtr gst_element_iterate_sink_pads (IntPtr element);
private class PadEnumerable : IEnumerable {
private class PadEnumerator : IEnumerator {
Hashtable seen = new Hashtable ();
IntPtr iterator;
private object current = null;
public object Current {
get {
return current;
}
}
public bool MoveNext () {
IntPtr raw_ret;
bool retry = false;
if (iterator == IntPtr.Zero)
return false;
do {
int ret = gst_iterator_next (iterator, out raw_ret);
switch (ret) {
case 0:
return false;
case 1:
if (seen.Contains (raw_ret)) {
retry = true;
break;
}
seen.Add (raw_ret, null);
current = GLib.Object.GetObject (raw_ret, true);
return true;
case 2:
gst_iterator_resync (iterator);
break;
default:
case 3:
throw new Exception ("Error while iterating pads");
}
} while (retry);
return false;
}
public void Reset () {
seen.Clear ();
if (iterator != IntPtr.Zero)
gst_iterator_resync (iterator);
}
public PadEnumerator (IntPtr iterator) {
this.iterator = iterator;
}
}
private IntPtr iterator;
private PadEnumerator enumerator;
public PadEnumerable (IntPtr iterator) {
this.iterator = iterator;
this.enumerator = new PadEnumerator (iterator);
}
public IEnumerator GetEnumerator () {
return enumerator;
}
~PadEnumerable () {
if (iterator != IntPtr.Zero)
gst_iterator_free (iterator);
}
}
public IEnumerable Pads {
get {
return new PadEnumerable (gst_element_iterate_pads (Handle));
return new Enumerable (gst_element_iterate_pads (Handle));
}
}
public IEnumerable SinkPads {
get {
return new PadEnumerable (gst_element_iterate_sink_pads (Handle));
return new Enumerable (gst_element_iterate_sink_pads (Handle));
}
}
public IEnumerable SrcPads {
get {
return new PadEnumerable (gst_element_iterate_src_pads (Handle));
return new Enumerable (gst_element_iterate_src_pads (Handle));
}
}

View file

@ -0,0 +1,90 @@
using System;
using System.Collections;
using System.Runtime.InteropServices;
using GLib;
namespace Gst {
internal class Enumerable : IEnumerable {
private class Enumerator : IEnumerator {
Hashtable seen = new Hashtable ();
IntPtr iterator;
private object current = null;
public object Current {
get {
return current;
}
}
public bool MoveNext () {
IntPtr raw_ret;
bool retry = false;
if (iterator == IntPtr.Zero)
return false;
do {
int ret = gst_iterator_next (iterator, out raw_ret);
switch (ret) {
case 0:
return false;
case 1:
if (seen.Contains (raw_ret)) {
retry = true;
break;
}
seen.Add (raw_ret, null);
current = GLib.Object.GetObject (raw_ret, true);
return true;
case 2:
gst_iterator_resync (iterator);
retry = true;
break;
default:
case 3:
throw new Exception ("Error while iterating pads");
}
} while (retry);
return false;
}
public void Reset () {
seen.Clear ();
if (iterator != IntPtr.Zero)
gst_iterator_resync (iterator);
}
public Enumerator (IntPtr iterator) {
this.iterator = iterator;
}
}
private IntPtr iterator;
private Enumerator enumerator;
public Enumerable (IntPtr iterator) {
this.iterator = iterator;
this.enumerator = new Enumerator (iterator);
}
public IEnumerator GetEnumerator () {
return enumerator;
}
~Enumerable () {
if (iterator != IntPtr.Zero)
gst_iterator_free (iterator);
}
[DllImport ("gstreamer-0.10.dll") ]
static extern int gst_iterator_next (IntPtr iterator, out IntPtr elem);
[DllImport ("gstreamer-0.10.dll") ]
static extern void gst_iterator_resync (IntPtr iterator);
[DllImport ("gstreamer-0.10.dll") ]
static extern void gst_iterator_free (IntPtr iterator);
}
}

View file

@ -44,6 +44,7 @@ sources = \
AssemblyInfo.cs \
GError.cs \
Value.cs \
Iterator.cs \
GstSharp.PadQueryTypeFunctionNative.cs \
PadQueryTypeFunction.cs \
TypeFindDelegates.cs \

View file

@ -431,94 +431,15 @@ public bool StreamTryLock () {
return g_static_rec_mutex_trylock (StreamLockPtr);
}
[DllImport ("gstreamer-0.10.dll") ]
static extern int gst_iterator_next (IntPtr iterator, out IntPtr elem);
[DllImport ("gstreamer-0.10.dll") ]
static extern void gst_iterator_resync (IntPtr iterator);
[DllImport ("gstreamer-0.10.dll") ]
static extern void gst_iterator_free (IntPtr iterator);
[DllImport ("gstreamer-0.10.dll") ]
static extern IntPtr gst_pad_iterate_internal_links (IntPtr pad);
[DllImport ("gstreamer-0.10.dll") ]
static extern IntPtr gst_pad_iterate_internal_links_default (IntPtr pad);
private class PadEnumerable : IEnumerable {
private class PadEnumerator : IEnumerator {
Hashtable seen = new Hashtable ();
IntPtr iterator;
private object current = null;
public object Current {
get {
return current;
}
}
public bool MoveNext () {
IntPtr raw_ret;
bool retry = false;
if (iterator == IntPtr.Zero)
return false;
do {
int ret = gst_iterator_next (iterator, out raw_ret);
switch (ret) {
case 0:
return false;
case 1:
if (seen.Contains (raw_ret)) {
retry = true;
break;
}
seen.Add (raw_ret, null);
current = GLib.Object.GetObject (raw_ret, true);
return true;
case 2:
gst_iterator_resync (iterator);
break;
default:
case 3:
throw new Exception ("Error while iterating pads");
}
} while (retry);
return false;
}
public void Reset () {
seen.Clear ();
if (iterator != IntPtr.Zero)
gst_iterator_resync (iterator);
}
public PadEnumerator (IntPtr iterator) {
this.iterator = iterator;
}
}
private IntPtr iterator;
private PadEnumerator enumerator;
public PadEnumerable (IntPtr iterator) {
this.iterator = iterator;
this.enumerator = new PadEnumerator (iterator);
}
public IEnumerator GetEnumerator () {
return enumerator;
}
~PadEnumerable () {
if (iterator != IntPtr.Zero)
gst_iterator_free (iterator);
}
}
public IEnumerable InternalLinks {
get {
return new PadEnumerable (gst_pad_iterate_internal_links (Handle));
return new Enumerable (gst_pad_iterate_internal_links (Handle));
}
}