From: Zeeshan Ali (Khattak) Date: Sun, 14 Dec 2008 20:21:43 +0000 (+0000) Subject: Implement Test audio item. X-Git-Tag: RYGEL_0_2~136 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1ecfadd88b8b454f3059948368139535321ee5ce;p=profile%2Fivi%2Frygel.git Implement Test audio item. It creates a small gstreamer pipeline and feeds the data produced from that pipeline to http response messages. svn path=/trunk/; revision=337 --- diff --git a/src/plugins/test/rygel-test-audio-item.vala b/src/plugins/test/rygel-test-audio-item.vala index f9bbbee..6967a4d 100644 --- a/src/plugins/test/rygel-test-audio-item.vala +++ b/src/plugins/test/rygel-test-audio-item.vala @@ -26,25 +26,145 @@ using Rygel; using GUPnP; -using DBus; +using Gee; +using Gst; /** * Represents Test audio item. */ public class Rygel.TestAudioItem : Rygel.MediaItem { + const string TEST_PATH = "/test.wav"; + const string TEST_MIMETYPE = "audio/x-wav"; + const string TEST_AUTHOR = "Zeeshan Ali (Khattak)"; + + private Streamer streamer; + private HashMap streams; + public TestAudioItem (string id, string parent_id, string title, Streamer streamer) { base (id, parent_id, title, MediaItem.AUDIO_CLASS); + this.mime = TEST_MIMETYPE; + this.author = TEST_AUTHOR; + this.uri = streamer.create_uri_for_path (TEST_PATH); + this.streams = new HashMap (); + + this.streamer = streamer; + + streamer.stream_available += this.on_stream_available; + streamer.add_stream_candidate (TEST_PATH, TEST_MIMETYPE); + } + + private void on_stream_available (Streamer streamer, + Stream stream, + string path) { + if (path != TEST_PATH) { + /* Not our path and therefore not interesting. */ + return; + } + + StreamContext context; + + try { + context = new StreamContext (stream, "RygelStreamer"); + } catch (Error error) { + critical ("Error creating stream context: %s", error.message); + + return; + } + + context.set_state (State.PLAYING); + stream.eos += on_eos; + + this.streams.set (stream, context); + } + + private void on_eos (Stream stream) { + StreamContext context = this.streams.get (stream); + if (context == null) + return; + + /* We don't need to wait for state change since downstream state changes + * are guaranteed to be synchronous. + */ + context.set_state (State.NULL); + + /* Remove the associated context. */ + this.streams.remove (stream); + } +} + +private class StreamContext : Pipeline { + public Stream stream; + + private AsyncQueue buffers; + + public StreamContext (Stream stream, + string name) throws Error { + this.stream = stream; + this.name = name; + + this.buffers = new AsyncQueue (); + + this.prepare_pipeline (); + } + + private void prepare_pipeline () throws Error { + dynamic Element src = ElementFactory.make ("audiotestsrc", null); + dynamic Element encoder = ElementFactory.make ("wavenc", null); + dynamic Element sink = ElementFactory.make ("appsink", null); + + if (src == null || encoder == null || sink == null) { + //throw new Something.Error ("Required plugin missing"); + return; + } + + sink.emit_signals = true; + sink.new_buffer += this.on_new_buffer; + sink.new_preroll += this.on_new_preroll; + + this.add_many (src, encoder, sink); + src.link_many (encoder, sink); } - public override void serialize (DIDLLiteWriter didl_writer) { - this.mime = "audio/wav"; - this.author = "Zeeshan Ali (Khattak)"; - //this.uri = this.uri_from_path (path); + private void on_new_buffer (dynamic Element sink) { + Buffer buffer = null; + + GLib.Signal.emit_by_name (sink, "pull-buffer", out buffer); + if (buffer == null) { + critical ("Failed to get buffer from pipeline"); + return; + } + + this.queue_buffer (buffer); + } + + private void on_new_preroll (dynamic Element sink) { + Buffer buffer = null; + + GLib.Signal.emit_by_name (sink, "pull-preroll", out buffer); + if (buffer == null) { + critical ("Failed to get buffer from pipeline"); + return; + } + + this.queue_buffer (buffer); + } + + private void queue_buffer (Buffer buffer) { + this.buffers.push (buffer); + Idle.add_full (Priority.HIGH_IDLE, this.idle_handler); + } + + private bool idle_handler () { + var buffer = this.buffers.pop (); + + if (buffer != null) { + this.stream.push_data (buffer.data, buffer.size); + } - base.serialize (didl_writer); + return false; } }