From 5097ae8adc2f7e973e483a70f74d2912cf054f95 Mon Sep 17 00:00:00 2001 From: Khaled Mohammed Date: Tue, 29 Aug 2006 07:28:18 +0000 Subject: [PATCH] Added a new sample - sample/MetaData.cs git-svn-id: svn://anonsvn.mono-project.com/source/branches/abock/gstreamer-sharp@64506 e3ebcda4-bce8-0310-ba0a-eca2169e7518 --- ChangeLog | 4 ++ sample/Makefile.am | 3 + sample/MetaData.cs | 153 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 sample/MetaData.cs diff --git a/ChangeLog b/ChangeLog index 4d6b269c4a..36e64f589c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2006-08-29 Khaled Mohammed + * sample/MetaData.cs: a sample file which extracts tag information + from media files and displays them to console. + 2006-08-25 Khaled Mohammed * sample/QueueExample.cs: a sample file showcasing use of the queue element diff --git a/sample/Makefile.am b/sample/Makefile.am index a0c729d5fe..d968ff6858 100644 --- a/sample/Makefile.am +++ b/sample/Makefile.am @@ -22,6 +22,9 @@ typefind.exe: $(srcdir)/TypeFind.cs $(assemblies) queueexample.exe: $(srcdir)/QueueExample.cs $(assemblies) $(CSC) -out:$@ $(GLIBSHARP_LIBS) $(references) $(srcdir)/QueueExample.cs +metadata.exe: $(srcdir)/MetaData.cs $(assemblies) + $(CSC) -out:$@ $(GLIBSHARP_LIBS) $(references) $(srcdir)/MetaData.cs + link: ln -sf $(top_builddir)/gstreamer-sharp/gstreamer-sharp.dll gstreamer-sharp.dll ln -sf $(top_builddir)/gstreamer-sharp/gstreamer-sharp.dll.config gstreamer-sharp.dll.config diff --git a/sample/MetaData.cs b/sample/MetaData.cs new file mode 100644 index 0000000000..8931222773 --- /dev/null +++ b/sample/MetaData.cs @@ -0,0 +1,153 @@ +// +// Authors +// Khaled Mohammed (khaled.mohammed@gmail.com) +// +// (C) 2006 +// + +using Gst; +using System; + +public class MetaData +{ + + static Element pipeline = null; + static Element source = null; + + static void PrintTag( TagList list, string tag) { + uint count = list.GetTagSize(tag); + + for(uint i =0; i < count; i++) + { + string str; + if(Tag.GetGType(tag) == GLib.GType.String) { + if(!list.GetStringIndex(tag, i, out str)) + Console.Error.WriteLine("g_assert_not_reached()???"); + } else { + str = (String) list.GetValueIndex(tag, i).Val; + } + + if(i == 0) + Console.WriteLine("{0}:\t {1}", Tag.GetNick(tag), str); + else + Console.WriteLine("\t{0}", str); + } + } + + static bool MessageLoop(Element element, TagList tags) + { + Bus bus = element.Bus; + bool done = false; + + while(!done) { + Message message = bus.Pop(); + if(message == null) + break; + + switch(message.Type) { + case MessageType.Error: + message.Dispose(); + return true; + case MessageType.Eos: + message.Dispose(); + return true; + case MessageType.Tag: { + TagList new_tags = new TagList(); + message.ParseTag(new_tags); + if(tags != null) { + tags = tags.Merge(new_tags, TagMergeMode.KeepAll); + } + else { + tags = new_tags; + } + new_tags.Dispose(); + break; + } + default: + break; + } + message.Dispose(); + } + bus.Dispose(); + return true; + } + + static void MakePipeline() + { + Element decodebin; + + if(pipeline != null) { + pipeline.Dispose(); + } + + pipeline = new Pipeline(String.Empty); + source = ElementFactory.Make("filesrc", "source"); + decodebin = ElementFactory.Make("decodebin", "decodebin"); + + Bin bin = (Bin) pipeline; + bin.AddMany(source, decodebin); + source.Link(decodebin); + decodebin.Dispose(); + } + + public static void Main(string [] args) + { + Application.Init(); + + if(args.Length < 1) + { + Console.Error.WriteLine("Please give filenames to read metadata from\n\n"); + return; + } + + MakePipeline(); + + int i=-1; + while(++i < args.Length) + { + State state, pending; + TagList tags = null; + + string filename = args[i]; + source.SetProperty("location", filename); + + StateChangeReturn sret = pipeline.SetState(State.Paused); + + if(sret == StateChangeReturn.Async) { + if(StateChangeReturn.Success != pipeline.GetState(out state, out pending, Clock.Second * 5)) { + Console.Error.WriteLine("State change failed for {0}. Aborting\n", filename); + break; + } + } else if(sret != StateChangeReturn.Success) { + Console.Error.WriteLine("{0} - Could not read file\n", filename); + continue; + } + + if(!MessageLoop(pipeline, tags)) { + Console.Error.WriteLine("Failed in message reading for {0}", args[i]); + } + + if(tags != null) { + Console.WriteLine("Metadata for {0}:", args[i]); + tags.Foreach(PrintTag); + tags.Dispose(); + tags = null; + } else Console.Error.WriteLine("No metadata found for {0}", args[0]); + + sret = pipeline.SetState(State.Null); + + if(StateChangeReturn.Async == sret) { + if(StateChangeReturn.Failure == pipeline.GetState(out state, out pending, Clock.TimeNone)) { + Console.Error.WriteLine("State change failed. Aborting"); + } + } + } + + if(pipeline != null) + { + pipeline.Dispose(); + } + + } +} +