Adding first code drop
[demorecorder] / src / EncodePipeline.vala
diff --git a/src/EncodePipeline.vala b/src/EncodePipeline.vala
new file mode 100644 (file)
index 0000000..00e3bdf
--- /dev/null
@@ -0,0 +1,208 @@
+/*  Demo Recorder for MAEMO 5
+*   Copyright (C) 2010 Dru Moore <usr@dru-id.co.uk>
+*   This program is free software; you can redistribute it and/or modify
+*   it under the terms of the GNU General Public License version 2,
+*   or (at your option) any later version, as published by the Free
+*   Software Foundation
+*
+*   This program is distributed in the hope that it will be useful,
+*   but WITHOUT ANY WARRANTY; without even the implied warranty of
+*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+*   GNU General Public License for more details
+*
+*   You should have received a copy of the GNU General Public
+*   License along with this program; if not, write to the
+*   Free Software Foundation, Inc.,
+*   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+namespace IdWorks {
+  
+  public class EncodePipeline : Gst.Pipeline {
+    
+    Gst.Element src;
+    Gst.Element queue;
+    Gst.Element resampler;
+    Gst.Element encoder;
+    Gst.Element converter;
+    Gst.Element filesink;
+    //string name;
+    string location;
+    //string encoder_element;
+    Gst.Caps caps;
+    Gst.Query duration_query;
+    Gst.Query position_query;
+    int64 position;
+    int64 old_position;
+    int64 duration;
+    int same_position_counter;
+    
+    public signal void position_duration(int64 position, int64 duration);
+    public signal void end_of_stream();
+    public signal void stream_error(string msg);
+    
+    public EncodePipeline(string name, string location, Gst.Element src, Gst.Element encoder, Gst.Caps caps) {
+      this.name = name;
+      this.location = location;
+      this.caps = caps;
+      this.src = src;
+      this.encoder = encoder;
+      //set the tag handling delegate
+      //this.handle_tags_delegate = this.handle_tags;
+      //set some default values
+      this.position=0;
+      this.duration=0;
+      this.construct_pipeline();
+      //define the queries
+      duration_query = new Gst.Query.duration(Gst.Format.TIME);
+      position_query = new Gst.Query.position(Gst.Format.TIME);
+    }
+    
+    public bool set_src(Gst.Element src) {
+      this.set_state(Gst.State.NULL);
+      if (null != this.src) {
+        clear_src();
+      }
+      this.src = src;
+      this.add(this.src);
+      this.src.link(this.queue);
+      return true;
+    }
+    
+    public void clear_src() {
+      if (null != this.src) {
+        this.set_state(Gst.State.NULL);
+        this.src.unlink(this.queue);
+        this.remove(this.src);
+        this.src = null;
+      }
+    }
+    
+    public bool set_encoder(Gst.Element encoder) {
+      this.set_state(Gst.State.NULL);
+      if (null != this.encoder) {
+        clear_encoder();
+      }
+      this.encoder = encoder;
+      this.add(this.encoder);
+      this.converter.link(this.encoder);
+      this.encoder.link(this.filesink);
+      return true;
+    }
+    
+    public void clear_encoder() {
+      if (null != this.encoder) {
+        this.set_state(Gst.State.NULL);
+        this.converter.unlink(this.encoder);
+        this.encoder.unlink(this.filesink);
+        this.remove(this.encoder);
+        this.encoder = null;
+      }
+    }
+    
+    public bool set_location(string location) {
+      this.set_state(Gst.State.NULL);
+      this.location = location;
+      this.filesink.set_property("location", location);
+      return true;
+    }
+    
+    public string get_location() {
+      return this.location;
+    }
+    
+    public void get_duration_info() {
+      bool duration_result,position_result;
+      Gst.Format format = Gst.Format.TIME;
+      duration_result = this.query(this.duration_query);
+      position_result = this.query(this.position_query);
+      if ( duration_result && position_result  ) {
+        this.duration_query.parse_duration(out format, out this.duration);
+        this.position_query.parse_position(out format, out this.position);
+        if (old_position == this.position) {
+          ++same_position_counter;
+        }
+        old_position = this.position;
+        this.position_duration(this.position,this.duration);
+        if (
+          (this.position >= this.duration) ||
+          ((5 < same_position_counter) && ((this.position + 100 * Time.Nanoseconds.MILLISECOND) >= this.duration))
+          ) {
+          this.end_of_stream();
+        }
+      }
+    }
+    
+    public void encode() {
+      this.set_state(Gst.State.PLAYING);
+    }
+    
+    public void stop() {
+      this.end_of_stream();
+      this.set_state(Gst.State.READY);
+    }
+    
+    private void construct_pipeline() {
+      ///this.name= name;
+      
+      if (null != this.src) {
+        this.add(this.src);
+      }
+      
+      this.queue = Gst.ElementFactory.make("queue", "buffer");
+      this.add(this.queue);
+      
+      this.converter = Gst.ElementFactory.make("audioconvert", "converter");
+      this.add(this.converter);
+      
+      
+      //this.resampler = Gst.ElementFactory.make("audioresample", "resampler");
+      //this.resampler.set_property("quality", 10);
+      //this.add(resampler);
+      //this.encoder = Gst.ElementFactory.make(encoder_element, "encoder");
+      this.add(this.encoder);
+      if (null != this.encoder) {
+//         stdout.printf("Encoder added!\n");
+//         stdout.flush();
+      }
+      this.filesink = Gst.ElementFactory.make("filesink", "fileoutput");
+      if (null != this.filesink) {
+        this.filesink.set_property("location", location);
+//         stdout.printf("Location set!\n");
+//         stdout.flush();
+        this.add(this.filesink);
+//         stdout.printf("Filesink added!\n");
+//         stdout.flush();
+      }
+      
+      if (null != this.src && this.src.link(this.queue)) {
+//         stdout.printf("Source linked!\n");
+//         stdout.flush();
+      }
+      if (this.queue.link(this.converter)) {
+//         stdout.printf("Queue linked!\n");
+//         stdout.flush();
+      }
+      /*
+      if (this.converter.link(this.resampler)) {
+        stdout.printf("Converter linked!\n");
+        stdout.flush();
+      }
+      if (this.resampler.link_filtered(this.encoder, caps)) {
+        stdout.printf("Resampler linked!\n");
+        stdout.flush();
+      }
+      */
+      if (this.converter.link(this.encoder)) {
+//         stdout.printf("Converter linked!\n");
+//         stdout.flush();
+      }
+      if (this.encoder.link(this.filesink)) {
+//         stdout.printf("Encoder linked!\n");
+//         stdout.flush();
+      }
+    }
+    
+    
+  }
+  
+}
\ No newline at end of file