1 /* Demo Recorder for MAEMO 5
2 * Copyright (C) 2010 Dru Moore <usr@dru-id.co.uk>
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2,
5 * or (at your option) any later version, as published by the Free
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 public class EncodePipeline : Gst.Pipeline {
24 Gst.Element resampler;
26 Gst.Element converter;
30 //string encoder_element;
32 Gst.Query duration_query;
33 Gst.Query position_query;
37 int same_position_counter;
39 public signal void position_duration(int64 position, int64 duration);
40 public signal void end_of_stream();
41 public signal void stream_error(string msg);
43 public EncodePipeline(string name, string location, Gst.Element src, Gst.Element encoder, Gst.Caps caps) {
45 this.location = location;
48 this.encoder = encoder;
49 //set the tag handling delegate
50 //this.handle_tags_delegate = this.handle_tags;
51 //set some default values
54 this.construct_pipeline();
56 duration_query = new Gst.Query.duration(Gst.Format.TIME);
57 position_query = new Gst.Query.position(Gst.Format.TIME);
60 public bool set_src(Gst.Element src) {
61 this.set_state(Gst.State.NULL);
62 if (null != this.src) {
67 this.src.link(this.queue);
71 public void clear_src() {
72 if (null != this.src) {
73 this.set_state(Gst.State.NULL);
74 this.src.unlink(this.queue);
75 this.remove(this.src);
80 public bool set_encoder(Gst.Element encoder) {
81 this.set_state(Gst.State.NULL);
82 if (null != this.encoder) {
85 this.encoder = encoder;
86 this.add(this.encoder);
87 this.converter.link(this.encoder);
88 this.encoder.link(this.filesink);
92 public void clear_encoder() {
93 if (null != this.encoder) {
94 this.set_state(Gst.State.NULL);
95 this.converter.unlink(this.encoder);
96 this.encoder.unlink(this.filesink);
97 this.remove(this.encoder);
102 public bool set_location(string location) {
103 this.set_state(Gst.State.NULL);
104 this.location = location;
105 this.filesink.set_property("location", location);
109 public string get_location() {
110 return this.location;
113 public void get_duration_info() {
114 bool duration_result,position_result;
115 Gst.Format format = Gst.Format.TIME;
116 duration_result = this.query(this.duration_query);
117 position_result = this.query(this.position_query);
118 if ( duration_result && position_result ) {
119 this.duration_query.parse_duration(out format, out this.duration);
120 this.position_query.parse_position(out format, out this.position);
121 if (old_position == this.position) {
122 ++same_position_counter;
124 old_position = this.position;
125 this.position_duration(this.position,this.duration);
127 (this.position >= this.duration) ||
128 ((5 < same_position_counter) && ((this.position + 100 * Time.Nanoseconds.MILLISECOND) >= this.duration))
130 this.end_of_stream();
135 public void encode() {
136 this.set_state(Gst.State.PLAYING);
140 this.end_of_stream();
141 this.set_state(Gst.State.READY);
144 private void construct_pipeline() {
147 if (null != this.src) {
151 this.queue = Gst.ElementFactory.make("queue", "buffer");
152 this.add(this.queue);
154 this.converter = Gst.ElementFactory.make("audioconvert", "converter");
155 this.add(this.converter);
158 //this.resampler = Gst.ElementFactory.make("audioresample", "resampler");
159 //this.resampler.set_property("quality", 10);
160 //this.add(resampler);
161 //this.encoder = Gst.ElementFactory.make(encoder_element, "encoder");
162 this.add(this.encoder);
163 if (null != this.encoder) {
164 // stdout.printf("Encoder added!\n");
167 this.filesink = Gst.ElementFactory.make("filesink", "fileoutput");
168 if (null != this.filesink) {
169 this.filesink.set_property("location", location);
170 // stdout.printf("Location set!\n");
172 this.add(this.filesink);
173 // stdout.printf("Filesink added!\n");
177 if (null != this.src && this.src.link(this.queue)) {
178 // stdout.printf("Source linked!\n");
181 if (this.queue.link(this.converter)) {
182 // stdout.printf("Queue linked!\n");
186 if (this.converter.link(this.resampler)) {
187 stdout.printf("Converter linked!\n");
190 if (this.resampler.link_filtered(this.encoder, caps)) {
191 stdout.printf("Resampler linked!\n");
195 if (this.converter.link(this.encoder)) {
196 // stdout.printf("Converter linked!\n");
199 if (this.encoder.link(this.filesink)) {
200 // stdout.printf("Encoder linked!\n");